wpa_supplicant.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  8. * This file implements functions for registering and unregistering
  9. * %wpa_supplicant interfaces. In addition, this file contains number of
  10. * functions for managing network connections.
  11. */
  12. #include "includes.h"
  13. #include "common.h"
  14. #include "crypto/random.h"
  15. #include "crypto/sha1.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "eap_peer/eap.h"
  18. #include "eap_server/eap_methods.h"
  19. #include "rsn_supp/wpa.h"
  20. #include "eloop.h"
  21. #include "config.h"
  22. #include "utils/ext_password.h"
  23. #include "l2_packet/l2_packet.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "ctrl_iface.h"
  27. #include "pcsc_funcs.h"
  28. #include "common/version.h"
  29. #include "rsn_supp/preauth.h"
  30. #include "rsn_supp/pmksa_cache.h"
  31. #include "common/wpa_ctrl.h"
  32. #include "common/ieee802_11_defs.h"
  33. #include "p2p/p2p.h"
  34. #include "blacklist.h"
  35. #include "wpas_glue.h"
  36. #include "wps_supplicant.h"
  37. #include "ibss_rsn.h"
  38. #include "sme.h"
  39. #include "gas_query.h"
  40. #include "ap.h"
  41. #include "p2p_supplicant.h"
  42. #include "wifi_display.h"
  43. #include "notify.h"
  44. #include "bgscan.h"
  45. #include "autoscan.h"
  46. #include "bss.h"
  47. #include "scan.h"
  48. #include "offchannel.h"
  49. #include "hs20_supplicant.h"
  50. const char *wpa_supplicant_version =
  51. "wpa_supplicant v" VERSION_STR "\n"
  52. "Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> and contributors";
  53. const char *wpa_supplicant_license =
  54. "This software may be distributed under the terms of the BSD license.\n"
  55. "See README for more details.\n"
  56. #ifdef EAP_TLS_OPENSSL
  57. "\nThis product includes software developed by the OpenSSL Project\n"
  58. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  59. #endif /* EAP_TLS_OPENSSL */
  60. ;
  61. #ifndef CONFIG_NO_STDOUT_DEBUG
  62. /* Long text divided into parts in order to fit in C89 strings size limits. */
  63. const char *wpa_supplicant_full_license1 =
  64. "";
  65. const char *wpa_supplicant_full_license2 =
  66. "This software may be distributed under the terms of the BSD license.\n"
  67. "\n"
  68. "Redistribution and use in source and binary forms, with or without\n"
  69. "modification, are permitted provided that the following conditions are\n"
  70. "met:\n"
  71. "\n";
  72. const char *wpa_supplicant_full_license3 =
  73. "1. Redistributions of source code must retain the above copyright\n"
  74. " notice, this list of conditions and the following disclaimer.\n"
  75. "\n"
  76. "2. Redistributions in binary form must reproduce the above copyright\n"
  77. " notice, this list of conditions and the following disclaimer in the\n"
  78. " documentation and/or other materials provided with the distribution.\n"
  79. "\n";
  80. const char *wpa_supplicant_full_license4 =
  81. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  82. " names of its contributors may be used to endorse or promote products\n"
  83. " derived from this software without specific prior written permission.\n"
  84. "\n"
  85. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  86. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  87. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  88. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  89. const char *wpa_supplicant_full_license5 =
  90. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  91. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  92. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  93. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  94. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  95. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  96. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  97. "\n";
  98. #endif /* CONFIG_NO_STDOUT_DEBUG */
  99. extern int wpa_debug_level;
  100. extern int wpa_debug_show_keys;
  101. extern int wpa_debug_timestamp;
  102. extern struct wpa_driver_ops *wpa_drivers[];
  103. /* Configure default/group WEP keys for static WEP */
  104. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  105. {
  106. int i, set = 0;
  107. for (i = 0; i < NUM_WEP_KEYS; i++) {
  108. if (ssid->wep_key_len[i] == 0)
  109. continue;
  110. set = 1;
  111. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  112. i, i == ssid->wep_tx_keyidx, NULL, 0,
  113. ssid->wep_key[i], ssid->wep_key_len[i]);
  114. }
  115. return set;
  116. }
  117. static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  118. struct wpa_ssid *ssid)
  119. {
  120. u8 key[32];
  121. size_t keylen;
  122. enum wpa_alg alg;
  123. u8 seq[6] = { 0 };
  124. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  125. * sending unicast and multicast packets. */
  126. if (ssid->mode != WPAS_MODE_IBSS) {
  127. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  128. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  129. return -1;
  130. }
  131. if (!ssid->psk_set) {
  132. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  133. "WPA-None");
  134. return -1;
  135. }
  136. switch (wpa_s->group_cipher) {
  137. case WPA_CIPHER_CCMP:
  138. os_memcpy(key, ssid->psk, 16);
  139. keylen = 16;
  140. alg = WPA_ALG_CCMP;
  141. break;
  142. case WPA_CIPHER_GCMP:
  143. os_memcpy(key, ssid->psk, 16);
  144. keylen = 16;
  145. alg = WPA_ALG_GCMP;
  146. break;
  147. case WPA_CIPHER_TKIP:
  148. /* WPA-None uses the same Michael MIC key for both TX and RX */
  149. os_memcpy(key, ssid->psk, 16 + 8);
  150. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  151. keylen = 32;
  152. alg = WPA_ALG_TKIP;
  153. break;
  154. default:
  155. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  156. "WPA-None", wpa_s->group_cipher);
  157. return -1;
  158. }
  159. /* TODO: should actually remember the previously used seq#, both for TX
  160. * and RX from each STA.. */
  161. return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  162. }
  163. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  164. {
  165. struct wpa_supplicant *wpa_s = eloop_ctx;
  166. const u8 *bssid = wpa_s->bssid;
  167. if (is_zero_ether_addr(bssid))
  168. bssid = wpa_s->pending_bssid;
  169. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  170. MAC2STR(bssid));
  171. wpa_blacklist_add(wpa_s, bssid);
  172. wpa_sm_notify_disassoc(wpa_s->wpa);
  173. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  174. wpa_s->reassociate = 1;
  175. /*
  176. * If we timed out, the AP or the local radio may be busy.
  177. * So, wait a second until scanning again.
  178. */
  179. wpa_supplicant_req_scan(wpa_s, 1, 0);
  180. #ifdef CONFIG_P2P
  181. if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
  182. wpa_s->global->p2p != NULL) {
  183. wpa_s->global->p2p_cb_on_scan_complete = 0;
  184. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  185. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  186. "continued after timed out authentication");
  187. }
  188. }
  189. #endif /* CONFIG_P2P */
  190. }
  191. /**
  192. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  193. * @wpa_s: Pointer to wpa_supplicant data
  194. * @sec: Number of seconds after which to time out authentication
  195. * @usec: Number of microseconds after which to time out authentication
  196. *
  197. * This function is used to schedule a timeout for the current authentication
  198. * attempt.
  199. */
  200. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  201. int sec, int usec)
  202. {
  203. if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
  204. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  205. return;
  206. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  207. "%d usec", sec, usec);
  208. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  209. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  210. }
  211. /**
  212. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  213. * @wpa_s: Pointer to wpa_supplicant data
  214. *
  215. * This function is used to cancel authentication timeout scheduled with
  216. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  217. * been completed.
  218. */
  219. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  220. {
  221. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  222. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  223. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  224. }
  225. /**
  226. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  227. * @wpa_s: Pointer to wpa_supplicant data
  228. *
  229. * This function is used to configure EAPOL state machine based on the selected
  230. * authentication mode.
  231. */
  232. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  233. {
  234. #ifdef IEEE8021X_EAPOL
  235. struct eapol_config eapol_conf;
  236. struct wpa_ssid *ssid = wpa_s->current_ssid;
  237. #ifdef CONFIG_IBSS_RSN
  238. if (ssid->mode == WPAS_MODE_IBSS &&
  239. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  240. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  241. /*
  242. * RSN IBSS authentication is per-STA and we can disable the
  243. * per-BSSID EAPOL authentication.
  244. */
  245. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  246. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  247. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  248. return;
  249. }
  250. #endif /* CONFIG_IBSS_RSN */
  251. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  252. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  253. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  254. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  255. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  256. else
  257. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  258. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  259. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  260. eapol_conf.accept_802_1x_keys = 1;
  261. eapol_conf.required_keys = 0;
  262. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  263. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  264. }
  265. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  266. eapol_conf.required_keys |=
  267. EAPOL_REQUIRE_KEY_BROADCAST;
  268. }
  269. if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  270. eapol_conf.required_keys = 0;
  271. }
  272. if (wpa_s->conf)
  273. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  274. eapol_conf.workaround = ssid->eap_workaround;
  275. eapol_conf.eap_disabled =
  276. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  277. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  278. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  279. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  280. #endif /* IEEE8021X_EAPOL */
  281. }
  282. /**
  283. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  284. * @wpa_s: Pointer to wpa_supplicant data
  285. * @ssid: Configuration data for the network
  286. *
  287. * This function is used to configure WPA state machine and related parameters
  288. * to a mode where WPA is not enabled. This is called as part of the
  289. * authentication configuration when the selected network does not use WPA.
  290. */
  291. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  292. struct wpa_ssid *ssid)
  293. {
  294. int i;
  295. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  296. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  297. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  298. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  299. else
  300. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  301. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  302. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  303. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  304. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  305. wpa_s->group_cipher = WPA_CIPHER_NONE;
  306. wpa_s->mgmt_group_cipher = 0;
  307. for (i = 0; i < NUM_WEP_KEYS; i++) {
  308. if (ssid->wep_key_len[i] > 5) {
  309. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  310. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  311. break;
  312. } else if (ssid->wep_key_len[i] > 0) {
  313. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  314. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  315. break;
  316. }
  317. }
  318. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  319. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  320. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  321. wpa_s->pairwise_cipher);
  322. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  323. #ifdef CONFIG_IEEE80211W
  324. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  325. wpa_s->mgmt_group_cipher);
  326. #endif /* CONFIG_IEEE80211W */
  327. pmksa_cache_clear_current(wpa_s->wpa);
  328. }
  329. void free_hw_features(struct wpa_supplicant *wpa_s)
  330. {
  331. int i;
  332. if (wpa_s->hw.modes == NULL)
  333. return;
  334. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  335. os_free(wpa_s->hw.modes[i].channels);
  336. os_free(wpa_s->hw.modes[i].rates);
  337. }
  338. os_free(wpa_s->hw.modes);
  339. wpa_s->hw.modes = NULL;
  340. }
  341. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  342. {
  343. bgscan_deinit(wpa_s);
  344. autoscan_deinit(wpa_s);
  345. scard_deinit(wpa_s->scard);
  346. wpa_s->scard = NULL;
  347. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  348. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  349. l2_packet_deinit(wpa_s->l2);
  350. wpa_s->l2 = NULL;
  351. if (wpa_s->l2_br) {
  352. l2_packet_deinit(wpa_s->l2_br);
  353. wpa_s->l2_br = NULL;
  354. }
  355. if (wpa_s->conf != NULL) {
  356. struct wpa_ssid *ssid;
  357. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  358. wpas_notify_network_removed(wpa_s, ssid);
  359. }
  360. os_free(wpa_s->confname);
  361. wpa_s->confname = NULL;
  362. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  363. eapol_sm_deinit(wpa_s->eapol);
  364. wpa_s->eapol = NULL;
  365. rsn_preauth_deinit(wpa_s->wpa);
  366. #ifdef CONFIG_TDLS
  367. wpa_tdls_deinit(wpa_s->wpa);
  368. #endif /* CONFIG_TDLS */
  369. pmksa_candidate_free(wpa_s->wpa);
  370. wpa_sm_deinit(wpa_s->wpa);
  371. wpa_s->wpa = NULL;
  372. wpa_blacklist_clear(wpa_s);
  373. wpa_bss_deinit(wpa_s);
  374. wpa_supplicant_cancel_scan(wpa_s);
  375. wpa_supplicant_cancel_auth_timeout(wpa_s);
  376. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  377. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  378. eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
  379. wpa_s, NULL);
  380. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  381. wpas_wps_deinit(wpa_s);
  382. wpabuf_free(wpa_s->pending_eapol_rx);
  383. wpa_s->pending_eapol_rx = NULL;
  384. #ifdef CONFIG_IBSS_RSN
  385. ibss_rsn_deinit(wpa_s->ibss_rsn);
  386. wpa_s->ibss_rsn = NULL;
  387. #endif /* CONFIG_IBSS_RSN */
  388. sme_deinit(wpa_s);
  389. #ifdef CONFIG_AP
  390. wpa_supplicant_ap_deinit(wpa_s);
  391. #endif /* CONFIG_AP */
  392. #ifdef CONFIG_P2P
  393. wpas_p2p_deinit(wpa_s);
  394. #endif /* CONFIG_P2P */
  395. #ifdef CONFIG_OFFCHANNEL
  396. offchannel_deinit(wpa_s);
  397. #endif /* CONFIG_OFFCHANNEL */
  398. wpa_supplicant_cancel_sched_scan(wpa_s);
  399. os_free(wpa_s->next_scan_freqs);
  400. wpa_s->next_scan_freqs = NULL;
  401. gas_query_deinit(wpa_s->gas);
  402. wpa_s->gas = NULL;
  403. free_hw_features(wpa_s);
  404. os_free(wpa_s->bssid_filter);
  405. wpa_s->bssid_filter = NULL;
  406. os_free(wpa_s->disallow_aps_bssid);
  407. wpa_s->disallow_aps_bssid = NULL;
  408. os_free(wpa_s->disallow_aps_ssid);
  409. wpa_s->disallow_aps_ssid = NULL;
  410. wnm_bss_keep_alive_deinit(wpa_s);
  411. ext_password_deinit(wpa_s->ext_pw);
  412. wpa_s->ext_pw = NULL;
  413. wpabuf_free(wpa_s->last_gas_resp);
  414. os_free(wpa_s->last_scan_res);
  415. wpa_s->last_scan_res = NULL;
  416. }
  417. /**
  418. * wpa_clear_keys - Clear keys configured for the driver
  419. * @wpa_s: Pointer to wpa_supplicant data
  420. * @addr: Previously used BSSID or %NULL if not available
  421. *
  422. * This function clears the encryption keys that has been previously configured
  423. * for the driver.
  424. */
  425. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  426. {
  427. if (wpa_s->keys_cleared) {
  428. /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
  429. * timing issues with keys being cleared just before new keys
  430. * are set or just after association or something similar. This
  431. * shows up in group key handshake failing often because of the
  432. * client not receiving the first encrypted packets correctly.
  433. * Skipping some of the extra key clearing steps seems to help
  434. * in completing group key handshake more reliably. */
  435. wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
  436. "skip key clearing");
  437. return;
  438. }
  439. /* MLME-DELETEKEYS.request */
  440. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  441. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  442. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  443. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  444. #ifdef CONFIG_IEEE80211W
  445. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  446. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  447. #endif /* CONFIG_IEEE80211W */
  448. if (addr) {
  449. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  450. 0);
  451. /* MLME-SETPROTECTION.request(None) */
  452. wpa_drv_mlme_setprotection(
  453. wpa_s, addr,
  454. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  455. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  456. }
  457. wpa_s->keys_cleared = 1;
  458. }
  459. /**
  460. * wpa_supplicant_state_txt - Get the connection state name as a text string
  461. * @state: State (wpa_state; WPA_*)
  462. * Returns: The state name as a printable text string
  463. */
  464. const char * wpa_supplicant_state_txt(enum wpa_states state)
  465. {
  466. switch (state) {
  467. case WPA_DISCONNECTED:
  468. return "DISCONNECTED";
  469. case WPA_INACTIVE:
  470. return "INACTIVE";
  471. case WPA_INTERFACE_DISABLED:
  472. return "INTERFACE_DISABLED";
  473. case WPA_SCANNING:
  474. return "SCANNING";
  475. case WPA_AUTHENTICATING:
  476. return "AUTHENTICATING";
  477. case WPA_ASSOCIATING:
  478. return "ASSOCIATING";
  479. case WPA_ASSOCIATED:
  480. return "ASSOCIATED";
  481. case WPA_4WAY_HANDSHAKE:
  482. return "4WAY_HANDSHAKE";
  483. case WPA_GROUP_HANDSHAKE:
  484. return "GROUP_HANDSHAKE";
  485. case WPA_COMPLETED:
  486. return "COMPLETED";
  487. default:
  488. return "UNKNOWN";
  489. }
  490. }
  491. #ifdef CONFIG_BGSCAN
  492. static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
  493. {
  494. if (wpas_driver_bss_selection(wpa_s))
  495. return;
  496. if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
  497. return;
  498. bgscan_deinit(wpa_s);
  499. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  500. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  501. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  502. "bgscan");
  503. /*
  504. * Live without bgscan; it is only used as a roaming
  505. * optimization, so the initial connection is not
  506. * affected.
  507. */
  508. } else {
  509. struct wpa_scan_results *scan_res;
  510. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  511. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
  512. 0);
  513. if (scan_res) {
  514. bgscan_notify_scan(wpa_s, scan_res);
  515. wpa_scan_results_free(scan_res);
  516. }
  517. }
  518. } else
  519. wpa_s->bgscan_ssid = NULL;
  520. }
  521. static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
  522. {
  523. if (wpa_s->bgscan_ssid != NULL) {
  524. bgscan_deinit(wpa_s);
  525. wpa_s->bgscan_ssid = NULL;
  526. }
  527. }
  528. #endif /* CONFIG_BGSCAN */
  529. static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
  530. {
  531. if (autoscan_init(wpa_s, 0))
  532. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
  533. }
  534. static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
  535. {
  536. autoscan_deinit(wpa_s);
  537. }
  538. void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
  539. {
  540. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  541. wpa_s->wpa_state == WPA_SCANNING) {
  542. autoscan_deinit(wpa_s);
  543. wpa_supplicant_start_autoscan(wpa_s);
  544. }
  545. }
  546. /**
  547. * wpa_supplicant_set_state - Set current connection state
  548. * @wpa_s: Pointer to wpa_supplicant data
  549. * @state: The new connection state
  550. *
  551. * This function is called whenever the connection state changes, e.g.,
  552. * association is completed for WPA/WPA2 4-Way Handshake is started.
  553. */
  554. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  555. enum wpa_states state)
  556. {
  557. enum wpa_states old_state = wpa_s->wpa_state;
  558. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  559. wpa_supplicant_state_txt(wpa_s->wpa_state),
  560. wpa_supplicant_state_txt(state));
  561. if (state != WPA_SCANNING)
  562. wpa_supplicant_notify_scanning(wpa_s, 0);
  563. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  564. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  565. struct wpa_ssid *ssid = wpa_s->current_ssid;
  566. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  567. MACSTR " completed %s [id=%d id_str=%s]",
  568. MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
  569. "(reauth)" : "(auth)",
  570. ssid ? ssid->id : -1,
  571. ssid && ssid->id_str ? ssid->id_str : "");
  572. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  573. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  574. wpa_s->new_connection = 0;
  575. wpa_s->reassociated_connection = 1;
  576. wpa_drv_set_operstate(wpa_s, 1);
  577. #ifndef IEEE8021X_EAPOL
  578. wpa_drv_set_supp_port(wpa_s, 1);
  579. #endif /* IEEE8021X_EAPOL */
  580. wpa_s->after_wps = 0;
  581. #ifdef CONFIG_P2P
  582. wpas_p2p_completed(wpa_s);
  583. #endif /* CONFIG_P2P */
  584. sme_sched_obss_scan(wpa_s, 1);
  585. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  586. state == WPA_ASSOCIATED) {
  587. wpa_s->new_connection = 1;
  588. wpa_drv_set_operstate(wpa_s, 0);
  589. #ifndef IEEE8021X_EAPOL
  590. wpa_drv_set_supp_port(wpa_s, 0);
  591. #endif /* IEEE8021X_EAPOL */
  592. sme_sched_obss_scan(wpa_s, 0);
  593. }
  594. wpa_s->wpa_state = state;
  595. #ifdef CONFIG_BGSCAN
  596. if (state == WPA_COMPLETED)
  597. wpa_supplicant_start_bgscan(wpa_s);
  598. else
  599. wpa_supplicant_stop_bgscan(wpa_s);
  600. #endif /* CONFIG_BGSCAN */
  601. if (state == WPA_AUTHENTICATING)
  602. wpa_supplicant_stop_autoscan(wpa_s);
  603. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  604. wpa_supplicant_start_autoscan(wpa_s);
  605. if (wpa_s->wpa_state != old_state) {
  606. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  607. if (wpa_s->wpa_state == WPA_COMPLETED ||
  608. old_state == WPA_COMPLETED)
  609. wpas_notify_auth_changed(wpa_s);
  610. }
  611. }
  612. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  613. {
  614. int pending = 0;
  615. #ifdef CONFIG_WPS
  616. struct wpa_supplicant *wpa_s = global->ifaces;
  617. while (wpa_s) {
  618. if (wpas_wps_terminate_pending(wpa_s) == 1)
  619. pending = 1;
  620. wpa_s = wpa_s->next;
  621. }
  622. #endif /* CONFIG_WPS */
  623. if (pending)
  624. return;
  625. eloop_terminate();
  626. }
  627. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  628. {
  629. struct wpa_global *global = signal_ctx;
  630. wpa_supplicant_terminate_proc(global);
  631. }
  632. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  633. {
  634. enum wpa_states old_state = wpa_s->wpa_state;
  635. wpa_s->pairwise_cipher = 0;
  636. wpa_s->group_cipher = 0;
  637. wpa_s->mgmt_group_cipher = 0;
  638. wpa_s->key_mgmt = 0;
  639. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  640. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  641. if (wpa_s->wpa_state != old_state)
  642. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  643. }
  644. /**
  645. * wpa_supplicant_reload_configuration - Reload configuration data
  646. * @wpa_s: Pointer to wpa_supplicant data
  647. * Returns: 0 on success or -1 if configuration parsing failed
  648. *
  649. * This function can be used to request that the configuration data is reloaded
  650. * (e.g., after configuration file change). This function is reloading
  651. * configuration only for one interface, so this may need to be called multiple
  652. * times if %wpa_supplicant is controlling multiple interfaces and all
  653. * interfaces need reconfiguration.
  654. */
  655. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  656. {
  657. struct wpa_config *conf;
  658. int reconf_ctrl;
  659. int old_ap_scan;
  660. if (wpa_s->confname == NULL)
  661. return -1;
  662. conf = wpa_config_read(wpa_s->confname);
  663. if (conf == NULL) {
  664. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  665. "file '%s' - exiting", wpa_s->confname);
  666. return -1;
  667. }
  668. conf->changed_parameters = (unsigned int) -1;
  669. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  670. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  671. os_strcmp(conf->ctrl_interface,
  672. wpa_s->conf->ctrl_interface) != 0);
  673. if (reconf_ctrl && wpa_s->ctrl_iface) {
  674. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  675. wpa_s->ctrl_iface = NULL;
  676. }
  677. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  678. if (wpa_s->current_ssid) {
  679. wpa_supplicant_deauthenticate(wpa_s,
  680. WLAN_REASON_DEAUTH_LEAVING);
  681. }
  682. /*
  683. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  684. * pkcs11_engine_path, pkcs11_module_path.
  685. */
  686. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  687. /*
  688. * Clear forced success to clear EAP state for next
  689. * authentication.
  690. */
  691. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  692. }
  693. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  694. wpa_sm_set_config(wpa_s->wpa, NULL);
  695. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  696. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  697. rsn_preauth_deinit(wpa_s->wpa);
  698. old_ap_scan = wpa_s->conf->ap_scan;
  699. wpa_config_free(wpa_s->conf);
  700. wpa_s->conf = conf;
  701. if (old_ap_scan != wpa_s->conf->ap_scan)
  702. wpas_notify_ap_scan_changed(wpa_s);
  703. if (reconf_ctrl)
  704. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  705. wpa_supplicant_update_config(wpa_s);
  706. wpa_supplicant_clear_status(wpa_s);
  707. if (wpa_supplicant_enabled_networks(wpa_s)) {
  708. wpa_s->reassociate = 1;
  709. wpa_supplicant_req_scan(wpa_s, 0, 0);
  710. }
  711. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  712. return 0;
  713. }
  714. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  715. {
  716. struct wpa_global *global = signal_ctx;
  717. struct wpa_supplicant *wpa_s;
  718. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  719. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  720. sig);
  721. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  722. wpa_supplicant_terminate_proc(global);
  723. }
  724. }
  725. }
  726. enum wpa_cipher cipher_suite2driver(int cipher)
  727. {
  728. switch (cipher) {
  729. case WPA_CIPHER_NONE:
  730. return CIPHER_NONE;
  731. case WPA_CIPHER_WEP40:
  732. return CIPHER_WEP40;
  733. case WPA_CIPHER_WEP104:
  734. return CIPHER_WEP104;
  735. case WPA_CIPHER_CCMP:
  736. return CIPHER_CCMP;
  737. case WPA_CIPHER_GCMP:
  738. return CIPHER_GCMP;
  739. case WPA_CIPHER_TKIP:
  740. default:
  741. return CIPHER_TKIP;
  742. }
  743. }
  744. enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
  745. {
  746. switch (key_mgmt) {
  747. case WPA_KEY_MGMT_NONE:
  748. return KEY_MGMT_NONE;
  749. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  750. return KEY_MGMT_802_1X_NO_WPA;
  751. case WPA_KEY_MGMT_IEEE8021X:
  752. return KEY_MGMT_802_1X;
  753. case WPA_KEY_MGMT_WPA_NONE:
  754. return KEY_MGMT_WPA_NONE;
  755. case WPA_KEY_MGMT_FT_IEEE8021X:
  756. return KEY_MGMT_FT_802_1X;
  757. case WPA_KEY_MGMT_FT_PSK:
  758. return KEY_MGMT_FT_PSK;
  759. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  760. return KEY_MGMT_802_1X_SHA256;
  761. case WPA_KEY_MGMT_PSK_SHA256:
  762. return KEY_MGMT_PSK_SHA256;
  763. case WPA_KEY_MGMT_WPS:
  764. return KEY_MGMT_WPS;
  765. case WPA_KEY_MGMT_PSK:
  766. default:
  767. return KEY_MGMT_PSK;
  768. }
  769. }
  770. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  771. struct wpa_ssid *ssid,
  772. struct wpa_ie_data *ie)
  773. {
  774. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  775. if (ret) {
  776. if (ret == -2) {
  777. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  778. "from association info");
  779. }
  780. return -1;
  781. }
  782. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  783. "cipher suites");
  784. if (!(ie->group_cipher & ssid->group_cipher)) {
  785. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  786. "cipher 0x%x (mask 0x%x) - reject",
  787. ie->group_cipher, ssid->group_cipher);
  788. return -1;
  789. }
  790. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  791. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  792. "cipher 0x%x (mask 0x%x) - reject",
  793. ie->pairwise_cipher, ssid->pairwise_cipher);
  794. return -1;
  795. }
  796. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  797. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  798. "management 0x%x (mask 0x%x) - reject",
  799. ie->key_mgmt, ssid->key_mgmt);
  800. return -1;
  801. }
  802. #ifdef CONFIG_IEEE80211W
  803. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  804. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  805. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  806. "that does not support management frame protection - "
  807. "reject");
  808. return -1;
  809. }
  810. #endif /* CONFIG_IEEE80211W */
  811. return 0;
  812. }
  813. /**
  814. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  815. * @wpa_s: Pointer to wpa_supplicant data
  816. * @bss: Scan results for the selected BSS, or %NULL if not available
  817. * @ssid: Configuration data for the selected network
  818. * @wpa_ie: Buffer for the WPA/RSN IE
  819. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  820. * used buffer length in case the functions returns success.
  821. * Returns: 0 on success or -1 on failure
  822. *
  823. * This function is used to configure authentication and encryption parameters
  824. * based on the network configuration and scan result for the selected BSS (if
  825. * available).
  826. */
  827. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  828. struct wpa_bss *bss, struct wpa_ssid *ssid,
  829. u8 *wpa_ie, size_t *wpa_ie_len)
  830. {
  831. struct wpa_ie_data ie;
  832. int sel, proto;
  833. const u8 *bss_wpa, *bss_rsn;
  834. if (bss) {
  835. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  836. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  837. } else
  838. bss_wpa = bss_rsn = NULL;
  839. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  840. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  841. (ie.group_cipher & ssid->group_cipher) &&
  842. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  843. (ie.key_mgmt & ssid->key_mgmt)) {
  844. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  845. proto = WPA_PROTO_RSN;
  846. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  847. wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
  848. (ie.group_cipher & ssid->group_cipher) &&
  849. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  850. (ie.key_mgmt & ssid->key_mgmt)) {
  851. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  852. proto = WPA_PROTO_WPA;
  853. } else if (bss) {
  854. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  855. return -1;
  856. } else {
  857. if (ssid->proto & WPA_PROTO_RSN)
  858. proto = WPA_PROTO_RSN;
  859. else
  860. proto = WPA_PROTO_WPA;
  861. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  862. os_memset(&ie, 0, sizeof(ie));
  863. ie.group_cipher = ssid->group_cipher;
  864. ie.pairwise_cipher = ssid->pairwise_cipher;
  865. ie.key_mgmt = ssid->key_mgmt;
  866. #ifdef CONFIG_IEEE80211W
  867. ie.mgmt_group_cipher =
  868. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  869. WPA_CIPHER_AES_128_CMAC : 0;
  870. #endif /* CONFIG_IEEE80211W */
  871. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  872. "based on configuration");
  873. } else
  874. proto = ie.proto;
  875. }
  876. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  877. "pairwise %d key_mgmt %d proto %d",
  878. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  879. #ifdef CONFIG_IEEE80211W
  880. if (ssid->ieee80211w) {
  881. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  882. ie.mgmt_group_cipher);
  883. }
  884. #endif /* CONFIG_IEEE80211W */
  885. wpa_s->wpa_proto = proto;
  886. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  887. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  888. !!(ssid->proto & WPA_PROTO_RSN));
  889. if (bss || !wpa_s->ap_ies_from_associnfo) {
  890. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  891. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  892. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  893. bss_rsn ? 2 + bss_rsn[1] : 0))
  894. return -1;
  895. }
  896. sel = ie.group_cipher & ssid->group_cipher;
  897. if (sel & WPA_CIPHER_CCMP) {
  898. wpa_s->group_cipher = WPA_CIPHER_CCMP;
  899. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
  900. } else if (sel & WPA_CIPHER_GCMP) {
  901. wpa_s->group_cipher = WPA_CIPHER_GCMP;
  902. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK GCMP");
  903. } else if (sel & WPA_CIPHER_TKIP) {
  904. wpa_s->group_cipher = WPA_CIPHER_TKIP;
  905. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
  906. } else if (sel & WPA_CIPHER_WEP104) {
  907. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  908. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
  909. } else if (sel & WPA_CIPHER_WEP40) {
  910. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  911. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
  912. } else {
  913. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  914. "cipher");
  915. return -1;
  916. }
  917. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  918. if (sel & WPA_CIPHER_CCMP) {
  919. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  920. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
  921. } else if (sel & WPA_CIPHER_GCMP) {
  922. wpa_s->pairwise_cipher = WPA_CIPHER_GCMP;
  923. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK GCMP");
  924. } else if (sel & WPA_CIPHER_TKIP) {
  925. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  926. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
  927. } else if (sel & WPA_CIPHER_NONE) {
  928. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  929. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
  930. } else {
  931. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  932. "cipher");
  933. return -1;
  934. }
  935. sel = ie.key_mgmt & ssid->key_mgmt;
  936. #ifdef CONFIG_SAE
  937. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
  938. sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
  939. #endif /* CONFIG_SAE */
  940. if (0) {
  941. #ifdef CONFIG_IEEE80211R
  942. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  943. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  944. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  945. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  946. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  947. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  948. #endif /* CONFIG_IEEE80211R */
  949. #ifdef CONFIG_SAE
  950. } else if (sel & WPA_KEY_MGMT_SAE) {
  951. wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
  952. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
  953. } else if (sel & WPA_KEY_MGMT_FT_SAE) {
  954. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
  955. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
  956. #endif /* CONFIG_SAE */
  957. #ifdef CONFIG_IEEE80211W
  958. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  959. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  960. wpa_dbg(wpa_s, MSG_DEBUG,
  961. "WPA: using KEY_MGMT 802.1X with SHA256");
  962. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  963. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  964. wpa_dbg(wpa_s, MSG_DEBUG,
  965. "WPA: using KEY_MGMT PSK with SHA256");
  966. #endif /* CONFIG_IEEE80211W */
  967. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  968. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  969. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  970. } else if (sel & WPA_KEY_MGMT_PSK) {
  971. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  972. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  973. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  974. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  975. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  976. } else {
  977. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  978. "authenticated key management type");
  979. return -1;
  980. }
  981. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  982. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  983. wpa_s->pairwise_cipher);
  984. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  985. #ifdef CONFIG_IEEE80211W
  986. sel = ie.mgmt_group_cipher;
  987. if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  988. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  989. sel = 0;
  990. if (sel & WPA_CIPHER_AES_128_CMAC) {
  991. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  992. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  993. "AES-128-CMAC");
  994. } else {
  995. wpa_s->mgmt_group_cipher = 0;
  996. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  997. }
  998. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  999. wpa_s->mgmt_group_cipher);
  1000. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
  1001. #endif /* CONFIG_IEEE80211W */
  1002. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  1003. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  1004. return -1;
  1005. }
  1006. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
  1007. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
  1008. #ifndef CONFIG_NO_PBKDF2
  1009. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  1010. ssid->passphrase) {
  1011. u8 psk[PMK_LEN];
  1012. pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
  1013. 4096, psk, PMK_LEN);
  1014. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1015. psk, PMK_LEN);
  1016. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  1017. }
  1018. #endif /* CONFIG_NO_PBKDF2 */
  1019. #ifdef CONFIG_EXT_PASSWORD
  1020. if (ssid->ext_psk) {
  1021. struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
  1022. ssid->ext_psk);
  1023. char pw_str[64 + 1];
  1024. u8 psk[PMK_LEN];
  1025. if (pw == NULL) {
  1026. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
  1027. "found from external storage");
  1028. return -1;
  1029. }
  1030. if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
  1031. wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
  1032. "PSK length %d in external storage",
  1033. (int) wpabuf_len(pw));
  1034. ext_password_free(pw);
  1035. return -1;
  1036. }
  1037. os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
  1038. pw_str[wpabuf_len(pw)] = '\0';
  1039. #ifndef CONFIG_NO_PBKDF2
  1040. if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
  1041. {
  1042. pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
  1043. 4096, psk, PMK_LEN);
  1044. os_memset(pw_str, 0, sizeof(pw_str));
  1045. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
  1046. "external passphrase)",
  1047. psk, PMK_LEN);
  1048. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  1049. } else
  1050. #endif /* CONFIG_NO_PBKDF2 */
  1051. if (wpabuf_len(pw) == 2 * PMK_LEN) {
  1052. if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
  1053. wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
  1054. "Invalid PSK hex string");
  1055. os_memset(pw_str, 0, sizeof(pw_str));
  1056. ext_password_free(pw);
  1057. return -1;
  1058. }
  1059. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  1060. } else {
  1061. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
  1062. "PSK available");
  1063. os_memset(pw_str, 0, sizeof(pw_str));
  1064. ext_password_free(pw);
  1065. return -1;
  1066. }
  1067. os_memset(pw_str, 0, sizeof(pw_str));
  1068. ext_password_free(pw);
  1069. }
  1070. #endif /* CONFIG_EXT_PASSWORD */
  1071. } else
  1072. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  1073. return 0;
  1074. }
  1075. /**
  1076. * wpa_supplicant_associate - Request association
  1077. * @wpa_s: Pointer to wpa_supplicant data
  1078. * @bss: Scan results for the selected BSS, or %NULL if not available
  1079. * @ssid: Configuration data for the selected network
  1080. *
  1081. * This function is used to request %wpa_supplicant to associate with a BSS.
  1082. */
  1083. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  1084. struct wpa_bss *bss, struct wpa_ssid *ssid)
  1085. {
  1086. u8 wpa_ie[200];
  1087. size_t wpa_ie_len;
  1088. int use_crypt, ret, i, bssid_changed;
  1089. int algs = WPA_AUTH_ALG_OPEN;
  1090. enum wpa_cipher cipher_pairwise, cipher_group;
  1091. struct wpa_driver_associate_params params;
  1092. int wep_keys_set = 0;
  1093. struct wpa_driver_capa capa;
  1094. int assoc_failed = 0;
  1095. struct wpa_ssid *old_ssid;
  1096. #ifdef CONFIG_HT_OVERRIDES
  1097. struct ieee80211_ht_capabilities htcaps;
  1098. struct ieee80211_ht_capabilities htcaps_mask;
  1099. #endif /* CONFIG_HT_OVERRIDES */
  1100. #ifdef CONFIG_IBSS_RSN
  1101. ibss_rsn_deinit(wpa_s->ibss_rsn);
  1102. wpa_s->ibss_rsn = NULL;
  1103. #endif /* CONFIG_IBSS_RSN */
  1104. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  1105. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1106. #ifdef CONFIG_AP
  1107. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  1108. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  1109. "mode");
  1110. return;
  1111. }
  1112. if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
  1113. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1114. return;
  1115. }
  1116. wpa_s->current_bss = bss;
  1117. #else /* CONFIG_AP */
  1118. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  1119. "the build");
  1120. #endif /* CONFIG_AP */
  1121. return;
  1122. }
  1123. #ifdef CONFIG_TDLS
  1124. if (bss)
  1125. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  1126. bss->ie_len);
  1127. #endif /* CONFIG_TDLS */
  1128. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1129. ssid->mode == IEEE80211_MODE_INFRA) {
  1130. sme_authenticate(wpa_s, bss, ssid);
  1131. return;
  1132. }
  1133. os_memset(&params, 0, sizeof(params));
  1134. wpa_s->reassociate = 0;
  1135. if (bss && !wpas_driver_bss_selection(wpa_s)) {
  1136. #ifdef CONFIG_IEEE80211R
  1137. const u8 *ie, *md = NULL;
  1138. #endif /* CONFIG_IEEE80211R */
  1139. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1140. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1141. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1142. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1143. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1144. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1145. if (bssid_changed)
  1146. wpas_notify_bssid_changed(wpa_s);
  1147. #ifdef CONFIG_IEEE80211R
  1148. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1149. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1150. md = ie + 2;
  1151. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1152. if (md) {
  1153. /* Prepare for the next transition */
  1154. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1155. }
  1156. #endif /* CONFIG_IEEE80211R */
  1157. #ifdef CONFIG_WPS
  1158. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1159. wpa_s->conf->ap_scan == 2 &&
  1160. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1161. /* Use ap_scan==1 style network selection to find the network
  1162. */
  1163. wpa_s->scan_req = 2;
  1164. wpa_s->reassociate = 1;
  1165. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1166. return;
  1167. #endif /* CONFIG_WPS */
  1168. } else {
  1169. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1170. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1171. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1172. }
  1173. wpa_supplicant_cancel_sched_scan(wpa_s);
  1174. wpa_supplicant_cancel_scan(wpa_s);
  1175. /* Starting new association, so clear the possibly used WPA IE from the
  1176. * previous association. */
  1177. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1178. #ifdef IEEE8021X_EAPOL
  1179. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1180. if (ssid->leap) {
  1181. if (ssid->non_leap == 0)
  1182. algs = WPA_AUTH_ALG_LEAP;
  1183. else
  1184. algs |= WPA_AUTH_ALG_LEAP;
  1185. }
  1186. }
  1187. #endif /* IEEE8021X_EAPOL */
  1188. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1189. if (ssid->auth_alg) {
  1190. algs = ssid->auth_alg;
  1191. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1192. "0x%x", algs);
  1193. }
  1194. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1195. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1196. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  1197. int try_opportunistic;
  1198. try_opportunistic = ssid->proactive_key_caching &&
  1199. (ssid->proto & WPA_PROTO_RSN);
  1200. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1201. wpa_s->current_ssid,
  1202. try_opportunistic) == 0)
  1203. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  1204. wpa_ie_len = sizeof(wpa_ie);
  1205. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1206. wpa_ie, &wpa_ie_len)) {
  1207. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1208. "key management and encryption suites");
  1209. return;
  1210. }
  1211. } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
  1212. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
  1213. /*
  1214. * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
  1215. * use non-WPA since the scan results did not indicate that the
  1216. * AP is using WPA or WPA2.
  1217. */
  1218. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1219. wpa_ie_len = 0;
  1220. wpa_s->wpa_proto = 0;
  1221. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  1222. wpa_ie_len = sizeof(wpa_ie);
  1223. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1224. wpa_ie, &wpa_ie_len)) {
  1225. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1226. "key management and encryption suites (no "
  1227. "scan results)");
  1228. return;
  1229. }
  1230. #ifdef CONFIG_WPS
  1231. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1232. struct wpabuf *wps_ie;
  1233. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1234. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1235. wpa_ie_len = wpabuf_len(wps_ie);
  1236. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1237. } else
  1238. wpa_ie_len = 0;
  1239. wpabuf_free(wps_ie);
  1240. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1241. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1242. params.wps = WPS_MODE_PRIVACY;
  1243. else
  1244. params.wps = WPS_MODE_OPEN;
  1245. wpa_s->wpa_proto = 0;
  1246. #endif /* CONFIG_WPS */
  1247. } else {
  1248. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1249. wpa_ie_len = 0;
  1250. wpa_s->wpa_proto = 0;
  1251. }
  1252. #ifdef CONFIG_P2P
  1253. if (wpa_s->global->p2p) {
  1254. u8 *pos;
  1255. size_t len;
  1256. int res;
  1257. pos = wpa_ie + wpa_ie_len;
  1258. len = sizeof(wpa_ie) - wpa_ie_len;
  1259. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  1260. ssid->p2p_group);
  1261. if (res >= 0)
  1262. wpa_ie_len += res;
  1263. }
  1264. wpa_s->cross_connect_disallowed = 0;
  1265. if (bss) {
  1266. struct wpabuf *p2p;
  1267. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1268. if (p2p) {
  1269. wpa_s->cross_connect_disallowed =
  1270. p2p_get_cross_connect_disallowed(p2p);
  1271. wpabuf_free(p2p);
  1272. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1273. "connection",
  1274. wpa_s->cross_connect_disallowed ?
  1275. "disallows" : "allows");
  1276. }
  1277. }
  1278. #endif /* CONFIG_P2P */
  1279. #ifdef CONFIG_HS20
  1280. if (wpa_s->conf->hs20) {
  1281. struct wpabuf *hs20;
  1282. hs20 = wpabuf_alloc(20);
  1283. if (hs20) {
  1284. wpas_hs20_add_indication(hs20);
  1285. os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(hs20),
  1286. wpabuf_len(hs20));
  1287. wpa_ie_len += wpabuf_len(hs20);
  1288. wpabuf_free(hs20);
  1289. }
  1290. }
  1291. #endif /* CONFIG_HS20 */
  1292. #ifdef CONFIG_INTERWORKING
  1293. if (wpa_s->conf->interworking) {
  1294. u8 *pos = wpa_ie;
  1295. if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  1296. pos += 2 + pos[1];
  1297. os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
  1298. wpa_ie_len += 6;
  1299. *pos++ = WLAN_EID_EXT_CAPAB;
  1300. *pos++ = 4;
  1301. *pos++ = 0x00;
  1302. *pos++ = 0x00;
  1303. *pos++ = 0x00;
  1304. *pos++ = 0x80; /* Bit 31 - Interworking */
  1305. }
  1306. #endif /* CONFIG_INTERWORKING */
  1307. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1308. use_crypt = 1;
  1309. cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
  1310. cipher_group = cipher_suite2driver(wpa_s->group_cipher);
  1311. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1312. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1313. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  1314. use_crypt = 0;
  1315. if (wpa_set_wep_keys(wpa_s, ssid)) {
  1316. use_crypt = 1;
  1317. wep_keys_set = 1;
  1318. }
  1319. }
  1320. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  1321. use_crypt = 0;
  1322. #ifdef IEEE8021X_EAPOL
  1323. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1324. if ((ssid->eapol_flags &
  1325. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  1326. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  1327. !wep_keys_set) {
  1328. use_crypt = 0;
  1329. } else {
  1330. /* Assume that dynamic WEP-104 keys will be used and
  1331. * set cipher suites in order for drivers to expect
  1332. * encryption. */
  1333. cipher_pairwise = cipher_group = CIPHER_WEP104;
  1334. }
  1335. }
  1336. #endif /* IEEE8021X_EAPOL */
  1337. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1338. /* Set the key before (and later after) association */
  1339. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1340. }
  1341. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1342. if (bss) {
  1343. params.ssid = bss->ssid;
  1344. params.ssid_len = bss->ssid_len;
  1345. if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
  1346. wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
  1347. MACSTR " freq=%u MHz based on scan results "
  1348. "(bssid_set=%d)",
  1349. MAC2STR(bss->bssid), bss->freq,
  1350. ssid->bssid_set);
  1351. params.bssid = bss->bssid;
  1352. params.freq = bss->freq;
  1353. }
  1354. } else {
  1355. params.ssid = ssid->ssid;
  1356. params.ssid_len = ssid->ssid_len;
  1357. }
  1358. if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
  1359. wpa_s->conf->ap_scan == 2) {
  1360. params.bssid = ssid->bssid;
  1361. params.fixed_bssid = 1;
  1362. }
  1363. if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
  1364. params.freq == 0)
  1365. params.freq = ssid->frequency; /* Initial channel for IBSS */
  1366. params.wpa_ie = wpa_ie;
  1367. params.wpa_ie_len = wpa_ie_len;
  1368. params.pairwise_suite = cipher_pairwise;
  1369. params.group_suite = cipher_group;
  1370. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  1371. params.wpa_proto = wpa_s->wpa_proto;
  1372. params.auth_alg = algs;
  1373. params.mode = ssid->mode;
  1374. params.bg_scan_period = ssid->bg_scan_period;
  1375. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1376. if (ssid->wep_key_len[i])
  1377. params.wep_key[i] = ssid->wep_key[i];
  1378. params.wep_key_len[i] = ssid->wep_key_len[i];
  1379. }
  1380. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1381. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1382. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  1383. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  1384. params.passphrase = ssid->passphrase;
  1385. if (ssid->psk_set)
  1386. params.psk = ssid->psk;
  1387. }
  1388. params.drop_unencrypted = use_crypt;
  1389. #ifdef CONFIG_IEEE80211W
  1390. params.mgmt_frame_protection = ssid->ieee80211w;
  1391. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  1392. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1393. struct wpa_ie_data ie;
  1394. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1395. ie.capabilities &
  1396. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1397. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1398. "MFP: require MFP");
  1399. params.mgmt_frame_protection =
  1400. MGMT_FRAME_PROTECTION_REQUIRED;
  1401. }
  1402. }
  1403. #endif /* CONFIG_IEEE80211W */
  1404. params.p2p = ssid->p2p_group;
  1405. if (wpa_s->parent->set_sta_uapsd)
  1406. params.uapsd = wpa_s->parent->sta_uapsd;
  1407. else
  1408. params.uapsd = -1;
  1409. #ifdef CONFIG_HT_OVERRIDES
  1410. os_memset(&htcaps, 0, sizeof(htcaps));
  1411. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  1412. params.htcaps = (u8 *) &htcaps;
  1413. params.htcaps_mask = (u8 *) &htcaps_mask;
  1414. wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
  1415. #endif /* CONFIG_HT_OVERRIDES */
  1416. ret = wpa_drv_associate(wpa_s, &params);
  1417. if (ret < 0) {
  1418. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1419. "failed");
  1420. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1421. /*
  1422. * The driver is known to mean what is saying, so we
  1423. * can stop right here; the association will not
  1424. * succeed.
  1425. */
  1426. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1427. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1428. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1429. return;
  1430. }
  1431. /* try to continue anyway; new association will be tried again
  1432. * after timeout */
  1433. assoc_failed = 1;
  1434. }
  1435. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1436. /* Set the key after the association just in case association
  1437. * cleared the previously configured key. */
  1438. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1439. /* No need to timeout authentication since there is no key
  1440. * management. */
  1441. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1442. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1443. #ifdef CONFIG_IBSS_RSN
  1444. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1445. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1446. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1447. /*
  1448. * RSN IBSS authentication is per-STA and we can disable the
  1449. * per-BSSID authentication.
  1450. */
  1451. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1452. #endif /* CONFIG_IBSS_RSN */
  1453. } else {
  1454. /* Timeout for IEEE 802.11 authentication and association */
  1455. int timeout = 60;
  1456. if (assoc_failed) {
  1457. /* give IBSS a bit more time */
  1458. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1459. } else if (wpa_s->conf->ap_scan == 1) {
  1460. /* give IBSS a bit more time */
  1461. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1462. }
  1463. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1464. }
  1465. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1466. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1467. /* Set static WEP keys again */
  1468. wpa_set_wep_keys(wpa_s, ssid);
  1469. }
  1470. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1471. /*
  1472. * Do not allow EAP session resumption between different
  1473. * network configurations.
  1474. */
  1475. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1476. }
  1477. old_ssid = wpa_s->current_ssid;
  1478. wpa_s->current_ssid = ssid;
  1479. wpa_s->current_bss = bss;
  1480. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1481. wpa_supplicant_initiate_eapol(wpa_s);
  1482. if (old_ssid != wpa_s->current_ssid)
  1483. wpas_notify_network_changed(wpa_s);
  1484. }
  1485. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1486. const u8 *addr)
  1487. {
  1488. struct wpa_ssid *old_ssid;
  1489. wpa_clear_keys(wpa_s, addr);
  1490. old_ssid = wpa_s->current_ssid;
  1491. wpa_supplicant_mark_disassoc(wpa_s);
  1492. wpa_sm_set_config(wpa_s->wpa, NULL);
  1493. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1494. if (old_ssid != wpa_s->current_ssid)
  1495. wpas_notify_network_changed(wpa_s);
  1496. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1497. }
  1498. /**
  1499. * wpa_supplicant_disassociate - Disassociate the current connection
  1500. * @wpa_s: Pointer to wpa_supplicant data
  1501. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1502. *
  1503. * This function is used to request %wpa_supplicant to disassociate with the
  1504. * current AP.
  1505. */
  1506. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1507. int reason_code)
  1508. {
  1509. u8 *addr = NULL;
  1510. union wpa_event_data event;
  1511. int zero_addr = 0;
  1512. wpa_dbg(wpa_s, MSG_DEBUG, "Request to disassociate - bssid=" MACSTR
  1513. " pending_bssid=" MACSTR " reason=%d state=%s",
  1514. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  1515. reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
  1516. if (!is_zero_ether_addr(wpa_s->bssid))
  1517. addr = wpa_s->bssid;
  1518. else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
  1519. (wpa_s->wpa_state == WPA_AUTHENTICATING ||
  1520. wpa_s->wpa_state == WPA_ASSOCIATING))
  1521. addr = wpa_s->pending_bssid;
  1522. else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  1523. /*
  1524. * When using driver-based BSS selection, we may not know the
  1525. * BSSID with which we are currently trying to associate. We
  1526. * need to notify the driver of this disconnection even in such
  1527. * a case, so use the all zeros address here.
  1528. */
  1529. addr = wpa_s->bssid;
  1530. zero_addr = 1;
  1531. }
  1532. if (addr) {
  1533. wpa_drv_disassociate(wpa_s, addr, reason_code);
  1534. os_memset(&event, 0, sizeof(event));
  1535. event.disassoc_info.reason_code = (u16) reason_code;
  1536. event.disassoc_info.locally_generated = 1;
  1537. wpa_supplicant_event(wpa_s, EVENT_DISASSOC, &event);
  1538. if (zero_addr)
  1539. addr = NULL;
  1540. }
  1541. wpa_supplicant_clear_connection(wpa_s, addr);
  1542. }
  1543. /**
  1544. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1545. * @wpa_s: Pointer to wpa_supplicant data
  1546. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1547. *
  1548. * This function is used to request %wpa_supplicant to deauthenticate from the
  1549. * current AP.
  1550. */
  1551. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1552. int reason_code)
  1553. {
  1554. u8 *addr = NULL;
  1555. union wpa_event_data event;
  1556. int zero_addr = 0;
  1557. wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
  1558. " pending_bssid=" MACSTR " reason=%d state=%s",
  1559. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  1560. reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
  1561. if (!is_zero_ether_addr(wpa_s->bssid))
  1562. addr = wpa_s->bssid;
  1563. else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
  1564. (wpa_s->wpa_state == WPA_AUTHENTICATING ||
  1565. wpa_s->wpa_state == WPA_ASSOCIATING))
  1566. addr = wpa_s->pending_bssid;
  1567. else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  1568. /*
  1569. * When using driver-based BSS selection, we may not know the
  1570. * BSSID with which we are currently trying to associate. We
  1571. * need to notify the driver of this disconnection even in such
  1572. * a case, so use the all zeros address here.
  1573. */
  1574. addr = wpa_s->bssid;
  1575. zero_addr = 1;
  1576. }
  1577. if (addr) {
  1578. wpa_drv_deauthenticate(wpa_s, addr, reason_code);
  1579. os_memset(&event, 0, sizeof(event));
  1580. event.deauth_info.reason_code = (u16) reason_code;
  1581. event.deauth_info.locally_generated = 1;
  1582. wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
  1583. if (zero_addr)
  1584. addr = NULL;
  1585. }
  1586. wpa_supplicant_clear_connection(wpa_s, addr);
  1587. }
  1588. /**
  1589. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1590. * @wpa_s: wpa_supplicant structure for a network interface
  1591. * @ssid: wpa_ssid structure for a configured network or %NULL
  1592. *
  1593. * Enables the specified network or all networks if no network specified.
  1594. */
  1595. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1596. struct wpa_ssid *ssid)
  1597. {
  1598. struct wpa_ssid *other_ssid;
  1599. int was_disabled;
  1600. if (ssid == NULL) {
  1601. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1602. other_ssid = other_ssid->next) {
  1603. if (other_ssid->disabled == 2)
  1604. continue; /* do not change persistent P2P group
  1605. * data */
  1606. if (other_ssid == wpa_s->current_ssid &&
  1607. other_ssid->disabled)
  1608. wpa_s->reassociate = 1;
  1609. was_disabled = other_ssid->disabled;
  1610. other_ssid->disabled = 0;
  1611. if (was_disabled)
  1612. wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
  1613. if (was_disabled != other_ssid->disabled)
  1614. wpas_notify_network_enabled_changed(
  1615. wpa_s, other_ssid);
  1616. }
  1617. if (wpa_s->reassociate)
  1618. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1619. } else if (ssid->disabled && ssid->disabled != 2) {
  1620. if (wpa_s->current_ssid == NULL) {
  1621. /*
  1622. * Try to reassociate since there is no current
  1623. * configuration and a new network was made available.
  1624. */
  1625. wpa_s->reassociate = 1;
  1626. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1627. }
  1628. was_disabled = ssid->disabled;
  1629. ssid->disabled = 0;
  1630. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  1631. if (was_disabled != ssid->disabled)
  1632. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1633. }
  1634. }
  1635. /**
  1636. * wpa_supplicant_disable_network - Mark a configured network as disabled
  1637. * @wpa_s: wpa_supplicant structure for a network interface
  1638. * @ssid: wpa_ssid structure for a configured network or %NULL
  1639. *
  1640. * Disables the specified network or all networks if no network specified.
  1641. */
  1642. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  1643. struct wpa_ssid *ssid)
  1644. {
  1645. struct wpa_ssid *other_ssid;
  1646. int was_disabled;
  1647. if (ssid == NULL) {
  1648. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1649. other_ssid = other_ssid->next) {
  1650. was_disabled = other_ssid->disabled;
  1651. if (was_disabled == 2)
  1652. continue; /* do not change persistent P2P group
  1653. * data */
  1654. other_ssid->disabled = 1;
  1655. if (was_disabled != other_ssid->disabled)
  1656. wpas_notify_network_enabled_changed(
  1657. wpa_s, other_ssid);
  1658. }
  1659. if (wpa_s->current_ssid)
  1660. wpa_supplicant_disassociate(
  1661. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1662. } else if (ssid->disabled != 2) {
  1663. if (ssid == wpa_s->current_ssid)
  1664. wpa_supplicant_disassociate(
  1665. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1666. was_disabled = ssid->disabled;
  1667. ssid->disabled = 1;
  1668. if (was_disabled != ssid->disabled)
  1669. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1670. }
  1671. }
  1672. /**
  1673. * wpa_supplicant_select_network - Attempt association with a network
  1674. * @wpa_s: wpa_supplicant structure for a network interface
  1675. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  1676. */
  1677. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  1678. struct wpa_ssid *ssid)
  1679. {
  1680. struct wpa_ssid *other_ssid;
  1681. int disconnected = 0;
  1682. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
  1683. wpa_supplicant_disassociate(
  1684. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1685. disconnected = 1;
  1686. }
  1687. if (ssid)
  1688. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  1689. /*
  1690. * Mark all other networks disabled or mark all networks enabled if no
  1691. * network specified.
  1692. */
  1693. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1694. other_ssid = other_ssid->next) {
  1695. int was_disabled = other_ssid->disabled;
  1696. if (was_disabled == 2)
  1697. continue; /* do not change persistent P2P group data */
  1698. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  1699. if (was_disabled && !other_ssid->disabled)
  1700. wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
  1701. if (was_disabled != other_ssid->disabled)
  1702. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  1703. }
  1704. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
  1705. /* We are already associated with the selected network */
  1706. wpa_printf(MSG_DEBUG, "Already associated with the "
  1707. "selected network - do nothing");
  1708. return;
  1709. }
  1710. if (ssid)
  1711. wpa_s->current_ssid = ssid;
  1712. wpa_s->connect_without_scan = NULL;
  1713. wpa_s->disconnected = 0;
  1714. wpa_s->reassociate = 1;
  1715. wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
  1716. if (ssid)
  1717. wpas_notify_network_selected(wpa_s, ssid);
  1718. }
  1719. /**
  1720. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  1721. * @wpa_s: wpa_supplicant structure for a network interface
  1722. * @ap_scan: AP scan mode
  1723. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  1724. *
  1725. */
  1726. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  1727. {
  1728. int old_ap_scan;
  1729. if (ap_scan < 0 || ap_scan > 2)
  1730. return -1;
  1731. #ifdef ANDROID
  1732. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  1733. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  1734. wpa_s->wpa_state < WPA_COMPLETED) {
  1735. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  1736. "associating", wpa_s->conf->ap_scan, ap_scan);
  1737. return 0;
  1738. }
  1739. #endif /* ANDROID */
  1740. old_ap_scan = wpa_s->conf->ap_scan;
  1741. wpa_s->conf->ap_scan = ap_scan;
  1742. if (old_ap_scan != wpa_s->conf->ap_scan)
  1743. wpas_notify_ap_scan_changed(wpa_s);
  1744. return 0;
  1745. }
  1746. /**
  1747. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  1748. * @wpa_s: wpa_supplicant structure for a network interface
  1749. * @expire_age: Expiration age in seconds
  1750. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  1751. *
  1752. */
  1753. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  1754. unsigned int bss_expire_age)
  1755. {
  1756. if (bss_expire_age < 10) {
  1757. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  1758. bss_expire_age);
  1759. return -1;
  1760. }
  1761. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  1762. bss_expire_age);
  1763. wpa_s->conf->bss_expiration_age = bss_expire_age;
  1764. return 0;
  1765. }
  1766. /**
  1767. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  1768. * @wpa_s: wpa_supplicant structure for a network interface
  1769. * @expire_count: number of scans after which an unseen BSS is reclaimed
  1770. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  1771. *
  1772. */
  1773. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  1774. unsigned int bss_expire_count)
  1775. {
  1776. if (bss_expire_count < 1) {
  1777. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  1778. bss_expire_count);
  1779. return -1;
  1780. }
  1781. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  1782. bss_expire_count);
  1783. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  1784. return 0;
  1785. }
  1786. /**
  1787. * wpa_supplicant_set_scan_interval - Set scan interval
  1788. * @wpa_s: wpa_supplicant structure for a network interface
  1789. * @scan_interval: scan interval in seconds
  1790. * Returns: 0 if succeed or -1 if scan_interval has an invalid value
  1791. *
  1792. */
  1793. int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
  1794. int scan_interval)
  1795. {
  1796. if (scan_interval < 0) {
  1797. wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
  1798. scan_interval);
  1799. return -1;
  1800. }
  1801. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
  1802. scan_interval);
  1803. wpa_s->scan_interval = scan_interval;
  1804. return 0;
  1805. }
  1806. /**
  1807. * wpa_supplicant_set_debug_params - Set global debug params
  1808. * @global: wpa_global structure
  1809. * @debug_level: debug level
  1810. * @debug_timestamp: determines if show timestamp in debug data
  1811. * @debug_show_keys: determines if show keys in debug data
  1812. * Returns: 0 if succeed or -1 if debug_level has wrong value
  1813. */
  1814. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  1815. int debug_timestamp, int debug_show_keys)
  1816. {
  1817. int old_level, old_timestamp, old_show_keys;
  1818. /* check for allowed debuglevels */
  1819. if (debug_level != MSG_EXCESSIVE &&
  1820. debug_level != MSG_MSGDUMP &&
  1821. debug_level != MSG_DEBUG &&
  1822. debug_level != MSG_INFO &&
  1823. debug_level != MSG_WARNING &&
  1824. debug_level != MSG_ERROR)
  1825. return -1;
  1826. old_level = wpa_debug_level;
  1827. old_timestamp = wpa_debug_timestamp;
  1828. old_show_keys = wpa_debug_show_keys;
  1829. wpa_debug_level = debug_level;
  1830. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  1831. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  1832. if (wpa_debug_level != old_level)
  1833. wpas_notify_debug_level_changed(global);
  1834. if (wpa_debug_timestamp != old_timestamp)
  1835. wpas_notify_debug_timestamp_changed(global);
  1836. if (wpa_debug_show_keys != old_show_keys)
  1837. wpas_notify_debug_show_keys_changed(global);
  1838. return 0;
  1839. }
  1840. /**
  1841. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1842. * @wpa_s: Pointer to wpa_supplicant data
  1843. * Returns: A pointer to the current network structure or %NULL on failure
  1844. */
  1845. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1846. {
  1847. struct wpa_ssid *entry;
  1848. u8 ssid[MAX_SSID_LEN];
  1849. int res;
  1850. size_t ssid_len;
  1851. u8 bssid[ETH_ALEN];
  1852. int wired;
  1853. res = wpa_drv_get_ssid(wpa_s, ssid);
  1854. if (res < 0) {
  1855. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1856. "driver");
  1857. return NULL;
  1858. }
  1859. ssid_len = res;
  1860. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1861. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  1862. "driver");
  1863. return NULL;
  1864. }
  1865. wired = wpa_s->conf->ap_scan == 0 &&
  1866. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  1867. entry = wpa_s->conf->ssid;
  1868. while (entry) {
  1869. if (!wpas_network_disabled(wpa_s, entry) &&
  1870. ((ssid_len == entry->ssid_len &&
  1871. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1872. (!entry->bssid_set ||
  1873. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1874. return entry;
  1875. #ifdef CONFIG_WPS
  1876. if (!wpas_network_disabled(wpa_s, entry) &&
  1877. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  1878. (entry->ssid == NULL || entry->ssid_len == 0) &&
  1879. (!entry->bssid_set ||
  1880. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1881. return entry;
  1882. #endif /* CONFIG_WPS */
  1883. if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
  1884. entry->ssid_len == 0 &&
  1885. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  1886. return entry;
  1887. entry = entry->next;
  1888. }
  1889. return NULL;
  1890. }
  1891. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  1892. {
  1893. struct wpa_global *global = wpa_s->global;
  1894. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  1895. global->drv_priv[i] = wpa_drivers[i]->global_init();
  1896. if (global->drv_priv[i] == NULL) {
  1897. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  1898. "'%s'", wpa_drivers[i]->name);
  1899. return -1;
  1900. }
  1901. }
  1902. wpa_s->driver = wpa_drivers[i];
  1903. wpa_s->global_drv_priv = global->drv_priv[i];
  1904. return 0;
  1905. }
  1906. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1907. const char *name)
  1908. {
  1909. int i;
  1910. size_t len;
  1911. const char *pos, *driver = name;
  1912. if (wpa_s == NULL)
  1913. return -1;
  1914. if (wpa_drivers[0] == NULL) {
  1915. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  1916. "wpa_supplicant");
  1917. return -1;
  1918. }
  1919. if (name == NULL) {
  1920. /* default to first driver in the list */
  1921. return select_driver(wpa_s, 0);
  1922. }
  1923. do {
  1924. pos = os_strchr(driver, ',');
  1925. if (pos)
  1926. len = pos - driver;
  1927. else
  1928. len = os_strlen(driver);
  1929. for (i = 0; wpa_drivers[i]; i++) {
  1930. if (os_strlen(wpa_drivers[i]->name) == len &&
  1931. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  1932. 0) {
  1933. /* First driver that succeeds wins */
  1934. if (select_driver(wpa_s, i) == 0)
  1935. return 0;
  1936. }
  1937. }
  1938. driver = pos + 1;
  1939. } while (pos);
  1940. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  1941. return -1;
  1942. }
  1943. /**
  1944. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  1945. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  1946. * with struct wpa_driver_ops::init()
  1947. * @src_addr: Source address of the EAPOL frame
  1948. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  1949. * @len: Length of the EAPOL data
  1950. *
  1951. * This function is called for each received EAPOL frame. Most driver
  1952. * interfaces rely on more generic OS mechanism for receiving frames through
  1953. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  1954. * take care of received EAPOL frames and deliver them to the core supplicant
  1955. * code by calling this function.
  1956. */
  1957. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1958. const u8 *buf, size_t len)
  1959. {
  1960. struct wpa_supplicant *wpa_s = ctx;
  1961. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1962. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1963. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  1964. /*
  1965. * There is possible race condition between receiving the
  1966. * association event and the EAPOL frame since they are coming
  1967. * through different paths from the driver. In order to avoid
  1968. * issues in trying to process the EAPOL frame before receiving
  1969. * association information, lets queue it for processing until
  1970. * the association event is received.
  1971. */
  1972. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  1973. "of received EAPOL frame");
  1974. wpabuf_free(wpa_s->pending_eapol_rx);
  1975. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  1976. if (wpa_s->pending_eapol_rx) {
  1977. os_get_time(&wpa_s->pending_eapol_rx_time);
  1978. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  1979. ETH_ALEN);
  1980. }
  1981. return;
  1982. }
  1983. #ifdef CONFIG_AP
  1984. if (wpa_s->ap_iface) {
  1985. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  1986. return;
  1987. }
  1988. #endif /* CONFIG_AP */
  1989. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1990. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  1991. "no key management is configured");
  1992. return;
  1993. }
  1994. if (wpa_s->eapol_received == 0 &&
  1995. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  1996. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1997. wpa_s->wpa_state != WPA_COMPLETED) &&
  1998. (wpa_s->current_ssid == NULL ||
  1999. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  2000. /* Timeout for completing IEEE 802.1X and WPA authentication */
  2001. wpa_supplicant_req_auth_timeout(
  2002. wpa_s,
  2003. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  2004. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  2005. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  2006. 70 : 10, 0);
  2007. }
  2008. wpa_s->eapol_received++;
  2009. if (wpa_s->countermeasures) {
  2010. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  2011. "EAPOL packet");
  2012. return;
  2013. }
  2014. #ifdef CONFIG_IBSS_RSN
  2015. if (wpa_s->current_ssid &&
  2016. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  2017. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  2018. return;
  2019. }
  2020. #endif /* CONFIG_IBSS_RSN */
  2021. /* Source address of the incoming EAPOL frame could be compared to the
  2022. * current BSSID. However, it is possible that a centralized
  2023. * Authenticator could be using another MAC address than the BSSID of
  2024. * an AP, so just allow any address to be used for now. The replies are
  2025. * still sent to the current BSSID (if available), though. */
  2026. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  2027. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  2028. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  2029. return;
  2030. wpa_drv_poll(wpa_s);
  2031. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  2032. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  2033. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  2034. /*
  2035. * Set portValid = TRUE here since we are going to skip 4-way
  2036. * handshake processing which would normally set portValid. We
  2037. * need this to allow the EAPOL state machines to be completed
  2038. * without going through EAPOL-Key handshake.
  2039. */
  2040. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  2041. }
  2042. }
  2043. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  2044. {
  2045. if (wpa_s->driver->send_eapol) {
  2046. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  2047. if (addr)
  2048. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  2049. } else if (!(wpa_s->drv_flags &
  2050. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  2051. l2_packet_deinit(wpa_s->l2);
  2052. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  2053. wpa_drv_get_mac_addr(wpa_s),
  2054. ETH_P_EAPOL,
  2055. wpa_supplicant_rx_eapol, wpa_s, 0);
  2056. if (wpa_s->l2 == NULL)
  2057. return -1;
  2058. } else {
  2059. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  2060. if (addr)
  2061. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  2062. }
  2063. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  2064. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  2065. return -1;
  2066. }
  2067. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  2068. MAC2STR(wpa_s->own_addr));
  2069. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  2070. return 0;
  2071. }
  2072. static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
  2073. const u8 *buf, size_t len)
  2074. {
  2075. struct wpa_supplicant *wpa_s = ctx;
  2076. const struct l2_ethhdr *eth;
  2077. if (len < sizeof(*eth))
  2078. return;
  2079. eth = (const struct l2_ethhdr *) buf;
  2080. if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
  2081. !(eth->h_dest[0] & 0x01)) {
  2082. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  2083. " (bridge - not for this interface - ignore)",
  2084. MAC2STR(src_addr), MAC2STR(eth->h_dest));
  2085. return;
  2086. }
  2087. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  2088. " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
  2089. wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
  2090. len - sizeof(*eth));
  2091. }
  2092. /**
  2093. * wpa_supplicant_driver_init - Initialize driver interface parameters
  2094. * @wpa_s: Pointer to wpa_supplicant data
  2095. * Returns: 0 on success, -1 on failure
  2096. *
  2097. * This function is called to initialize driver interface parameters.
  2098. * wpa_drv_init() must have been called before this function to initialize the
  2099. * driver interface.
  2100. */
  2101. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  2102. {
  2103. static int interface_count = 0;
  2104. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  2105. return -1;
  2106. if (wpa_s->bridge_ifname[0]) {
  2107. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  2108. "interface '%s'", wpa_s->bridge_ifname);
  2109. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  2110. wpa_s->own_addr,
  2111. ETH_P_EAPOL,
  2112. wpa_supplicant_rx_eapol_bridge,
  2113. wpa_s, 1);
  2114. if (wpa_s->l2_br == NULL) {
  2115. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  2116. "connection for the bridge interface '%s'",
  2117. wpa_s->bridge_ifname);
  2118. return -1;
  2119. }
  2120. }
  2121. wpa_clear_keys(wpa_s, NULL);
  2122. /* Make sure that TKIP countermeasures are not left enabled (could
  2123. * happen if wpa_supplicant is killed during countermeasures. */
  2124. wpa_drv_set_countermeasures(wpa_s, 0);
  2125. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  2126. wpa_drv_flush_pmkid(wpa_s);
  2127. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  2128. wpa_s->prev_scan_wildcard = 0;
  2129. if (wpa_supplicant_enabled_networks(wpa_s)) {
  2130. if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
  2131. 100000))
  2132. wpa_supplicant_req_scan(wpa_s, interface_count,
  2133. 100000);
  2134. interface_count++;
  2135. } else
  2136. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  2137. return 0;
  2138. }
  2139. static int wpa_supplicant_daemon(const char *pid_file)
  2140. {
  2141. wpa_printf(MSG_DEBUG, "Daemonize..");
  2142. return os_daemonize(pid_file);
  2143. }
  2144. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  2145. {
  2146. struct wpa_supplicant *wpa_s;
  2147. wpa_s = os_zalloc(sizeof(*wpa_s));
  2148. if (wpa_s == NULL)
  2149. return NULL;
  2150. wpa_s->scan_req = 1;
  2151. wpa_s->scan_interval = 5;
  2152. wpa_s->new_connection = 1;
  2153. wpa_s->parent = wpa_s;
  2154. wpa_s->sched_scanning = 0;
  2155. return wpa_s;
  2156. }
  2157. #ifdef CONFIG_HT_OVERRIDES
  2158. static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
  2159. struct ieee80211_ht_capabilities *htcaps,
  2160. struct ieee80211_ht_capabilities *htcaps_mask,
  2161. const char *ht_mcs)
  2162. {
  2163. /* parse ht_mcs into hex array */
  2164. int i;
  2165. const char *tmp = ht_mcs;
  2166. char *end = NULL;
  2167. /* If ht_mcs is null, do not set anything */
  2168. if (!ht_mcs)
  2169. return 0;
  2170. /* This is what we are setting in the kernel */
  2171. os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
  2172. wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
  2173. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  2174. errno = 0;
  2175. long v = strtol(tmp, &end, 16);
  2176. if (errno == 0) {
  2177. wpa_msg(wpa_s, MSG_DEBUG,
  2178. "htcap value[%i]: %ld end: %p tmp: %p",
  2179. i, v, end, tmp);
  2180. if (end == tmp)
  2181. break;
  2182. htcaps->supported_mcs_set[i] = v;
  2183. tmp = end;
  2184. } else {
  2185. wpa_msg(wpa_s, MSG_ERROR,
  2186. "Failed to parse ht-mcs: %s, error: %s\n",
  2187. ht_mcs, strerror(errno));
  2188. return -1;
  2189. }
  2190. }
  2191. /*
  2192. * If we were able to parse any values, then set mask for the MCS set.
  2193. */
  2194. if (i) {
  2195. os_memset(&htcaps_mask->supported_mcs_set, 0xff,
  2196. IEEE80211_HT_MCS_MASK_LEN - 1);
  2197. /* skip the 3 reserved bits */
  2198. htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
  2199. 0x1f;
  2200. }
  2201. return 0;
  2202. }
  2203. static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
  2204. struct ieee80211_ht_capabilities *htcaps,
  2205. struct ieee80211_ht_capabilities *htcaps_mask,
  2206. int disabled)
  2207. {
  2208. u16 msk;
  2209. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
  2210. if (disabled == -1)
  2211. return 0;
  2212. msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
  2213. htcaps_mask->ht_capabilities_info |= msk;
  2214. if (disabled)
  2215. htcaps->ht_capabilities_info &= msk;
  2216. else
  2217. htcaps->ht_capabilities_info |= msk;
  2218. return 0;
  2219. }
  2220. static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
  2221. struct ieee80211_ht_capabilities *htcaps,
  2222. struct ieee80211_ht_capabilities *htcaps_mask,
  2223. int factor)
  2224. {
  2225. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
  2226. if (factor == -1)
  2227. return 0;
  2228. if (factor < 0 || factor > 3) {
  2229. wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
  2230. "Must be 0-3 or -1", factor);
  2231. return -EINVAL;
  2232. }
  2233. htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
  2234. htcaps->a_mpdu_params &= ~0x3;
  2235. htcaps->a_mpdu_params |= factor & 0x3;
  2236. return 0;
  2237. }
  2238. static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
  2239. struct ieee80211_ht_capabilities *htcaps,
  2240. struct ieee80211_ht_capabilities *htcaps_mask,
  2241. int density)
  2242. {
  2243. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
  2244. if (density == -1)
  2245. return 0;
  2246. if (density < 0 || density > 7) {
  2247. wpa_msg(wpa_s, MSG_ERROR,
  2248. "ampdu_density: %d out of range. Must be 0-7 or -1.",
  2249. density);
  2250. return -EINVAL;
  2251. }
  2252. htcaps_mask->a_mpdu_params |= 0x1C;
  2253. htcaps->a_mpdu_params &= ~(0x1C);
  2254. htcaps->a_mpdu_params |= (density << 2) & 0x1C;
  2255. return 0;
  2256. }
  2257. static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
  2258. struct ieee80211_ht_capabilities *htcaps,
  2259. struct ieee80211_ht_capabilities *htcaps_mask,
  2260. int disabled)
  2261. {
  2262. /* Masking these out disables HT40 */
  2263. u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
  2264. HT_CAP_INFO_SHORT_GI40MHZ);
  2265. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
  2266. if (disabled)
  2267. htcaps->ht_capabilities_info &= ~msk;
  2268. else
  2269. htcaps->ht_capabilities_info |= msk;
  2270. htcaps_mask->ht_capabilities_info |= msk;
  2271. return 0;
  2272. }
  2273. void wpa_supplicant_apply_ht_overrides(
  2274. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2275. struct wpa_driver_associate_params *params)
  2276. {
  2277. struct ieee80211_ht_capabilities *htcaps;
  2278. struct ieee80211_ht_capabilities *htcaps_mask;
  2279. if (!ssid)
  2280. return;
  2281. params->disable_ht = ssid->disable_ht;
  2282. if (!params->htcaps || !params->htcaps_mask)
  2283. return;
  2284. htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
  2285. htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
  2286. wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
  2287. wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
  2288. ssid->disable_max_amsdu);
  2289. wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
  2290. wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
  2291. wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
  2292. }
  2293. #endif /* CONFIG_HT_OVERRIDES */
  2294. static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
  2295. {
  2296. #ifdef PCSC_FUNCS
  2297. size_t len;
  2298. if (!wpa_s->conf->pcsc_reader)
  2299. return 0;
  2300. wpa_s->scard = scard_init(SCARD_TRY_BOTH, wpa_s->conf->pcsc_reader);
  2301. if (!wpa_s->scard)
  2302. return 1;
  2303. if (wpa_s->conf->pcsc_pin &&
  2304. scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
  2305. scard_deinit(wpa_s->scard);
  2306. wpa_s->scard = NULL;
  2307. wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
  2308. return -1;
  2309. }
  2310. len = sizeof(wpa_s->imsi) - 1;
  2311. if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
  2312. scard_deinit(wpa_s->scard);
  2313. wpa_s->scard = NULL;
  2314. wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
  2315. return -1;
  2316. }
  2317. wpa_s->imsi[len] = '\0';
  2318. wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
  2319. wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
  2320. wpa_s->imsi, wpa_s->mnc_len);
  2321. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  2322. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  2323. #endif /* PCSC_FUNCS */
  2324. return 0;
  2325. }
  2326. int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
  2327. {
  2328. char *val, *pos;
  2329. ext_password_deinit(wpa_s->ext_pw);
  2330. wpa_s->ext_pw = NULL;
  2331. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
  2332. if (!wpa_s->conf->ext_password_backend)
  2333. return 0;
  2334. val = os_strdup(wpa_s->conf->ext_password_backend);
  2335. if (val == NULL)
  2336. return -1;
  2337. pos = os_strchr(val, ':');
  2338. if (pos)
  2339. *pos++ = '\0';
  2340. wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
  2341. wpa_s->ext_pw = ext_password_init(val, pos);
  2342. os_free(val);
  2343. if (wpa_s->ext_pw == NULL) {
  2344. wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
  2345. return -1;
  2346. }
  2347. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
  2348. return 0;
  2349. }
  2350. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  2351. struct wpa_interface *iface)
  2352. {
  2353. const char *ifname, *driver;
  2354. struct wpa_driver_capa capa;
  2355. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  2356. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  2357. iface->confname ? iface->confname : "N/A",
  2358. iface->driver ? iface->driver : "default",
  2359. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  2360. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  2361. if (iface->confname) {
  2362. #ifdef CONFIG_BACKEND_FILE
  2363. wpa_s->confname = os_rel2abs_path(iface->confname);
  2364. if (wpa_s->confname == NULL) {
  2365. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  2366. "for configuration file '%s'.",
  2367. iface->confname);
  2368. return -1;
  2369. }
  2370. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  2371. iface->confname, wpa_s->confname);
  2372. #else /* CONFIG_BACKEND_FILE */
  2373. wpa_s->confname = os_strdup(iface->confname);
  2374. #endif /* CONFIG_BACKEND_FILE */
  2375. wpa_s->conf = wpa_config_read(wpa_s->confname);
  2376. if (wpa_s->conf == NULL) {
  2377. wpa_printf(MSG_ERROR, "Failed to read or parse "
  2378. "configuration '%s'.", wpa_s->confname);
  2379. return -1;
  2380. }
  2381. /*
  2382. * Override ctrl_interface and driver_param if set on command
  2383. * line.
  2384. */
  2385. if (iface->ctrl_interface) {
  2386. os_free(wpa_s->conf->ctrl_interface);
  2387. wpa_s->conf->ctrl_interface =
  2388. os_strdup(iface->ctrl_interface);
  2389. }
  2390. if (iface->driver_param) {
  2391. os_free(wpa_s->conf->driver_param);
  2392. wpa_s->conf->driver_param =
  2393. os_strdup(iface->driver_param);
  2394. }
  2395. } else
  2396. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  2397. iface->driver_param);
  2398. if (wpa_s->conf == NULL) {
  2399. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  2400. return -1;
  2401. }
  2402. if (iface->ifname == NULL) {
  2403. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  2404. return -1;
  2405. }
  2406. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  2407. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  2408. iface->ifname);
  2409. return -1;
  2410. }
  2411. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  2412. if (iface->bridge_ifname) {
  2413. if (os_strlen(iface->bridge_ifname) >=
  2414. sizeof(wpa_s->bridge_ifname)) {
  2415. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  2416. "name '%s'.", iface->bridge_ifname);
  2417. return -1;
  2418. }
  2419. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  2420. sizeof(wpa_s->bridge_ifname));
  2421. }
  2422. /* RSNA Supplicant Key Management - INITIALIZE */
  2423. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  2424. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  2425. /* Initialize driver interface and register driver event handler before
  2426. * L2 receive handler so that association events are processed before
  2427. * EAPOL-Key packets if both become available for the same select()
  2428. * call. */
  2429. driver = iface->driver;
  2430. next_driver:
  2431. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  2432. return -1;
  2433. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  2434. if (wpa_s->drv_priv == NULL) {
  2435. const char *pos;
  2436. pos = driver ? os_strchr(driver, ',') : NULL;
  2437. if (pos) {
  2438. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  2439. "driver interface - try next driver wrapper");
  2440. driver = pos + 1;
  2441. goto next_driver;
  2442. }
  2443. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  2444. "interface");
  2445. return -1;
  2446. }
  2447. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  2448. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  2449. "driver_param '%s'", wpa_s->conf->driver_param);
  2450. return -1;
  2451. }
  2452. ifname = wpa_drv_get_ifname(wpa_s);
  2453. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  2454. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  2455. "interface name with '%s'", ifname);
  2456. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  2457. }
  2458. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  2459. return -1;
  2460. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  2461. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  2462. NULL);
  2463. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  2464. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  2465. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  2466. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  2467. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2468. "dot11RSNAConfigPMKLifetime");
  2469. return -1;
  2470. }
  2471. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  2472. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  2473. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  2474. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2475. "dot11RSNAConfigPMKReauthThreshold");
  2476. return -1;
  2477. }
  2478. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  2479. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  2480. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  2481. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2482. "dot11RSNAConfigSATimeout");
  2483. return -1;
  2484. }
  2485. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  2486. &wpa_s->hw.num_modes,
  2487. &wpa_s->hw.flags);
  2488. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  2489. wpa_s->drv_capa_known = 1;
  2490. wpa_s->drv_flags = capa.flags;
  2491. wpa_s->drv_enc = capa.enc;
  2492. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  2493. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  2494. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  2495. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  2496. wpa_s->max_match_sets = capa.max_match_sets;
  2497. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  2498. wpa_s->max_stations = capa.max_stations;
  2499. }
  2500. if (wpa_s->max_remain_on_chan == 0)
  2501. wpa_s->max_remain_on_chan = 1000;
  2502. if (wpa_supplicant_driver_init(wpa_s) < 0)
  2503. return -1;
  2504. #ifdef CONFIG_TDLS
  2505. if (wpa_tdls_init(wpa_s->wpa))
  2506. return -1;
  2507. #endif /* CONFIG_TDLS */
  2508. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  2509. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  2510. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  2511. return -1;
  2512. }
  2513. if (wpas_wps_init(wpa_s))
  2514. return -1;
  2515. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  2516. return -1;
  2517. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2518. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  2519. if (wpa_s->ctrl_iface == NULL) {
  2520. wpa_printf(MSG_ERROR,
  2521. "Failed to initialize control interface '%s'.\n"
  2522. "You may have another wpa_supplicant process "
  2523. "already running or the file was\n"
  2524. "left by an unclean termination of wpa_supplicant "
  2525. "in which case you will need\n"
  2526. "to manually remove this file before starting "
  2527. "wpa_supplicant again.\n",
  2528. wpa_s->conf->ctrl_interface);
  2529. return -1;
  2530. }
  2531. wpa_s->gas = gas_query_init(wpa_s);
  2532. if (wpa_s->gas == NULL) {
  2533. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  2534. return -1;
  2535. }
  2536. #ifdef CONFIG_P2P
  2537. if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  2538. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  2539. return -1;
  2540. }
  2541. #endif /* CONFIG_P2P */
  2542. if (wpa_bss_init(wpa_s) < 0)
  2543. return -1;
  2544. if (pcsc_reader_init(wpa_s) < 0)
  2545. return -1;
  2546. if (wpas_init_ext_pw(wpa_s) < 0)
  2547. return -1;
  2548. return 0;
  2549. }
  2550. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  2551. int notify, int terminate)
  2552. {
  2553. if (wpa_s->drv_priv) {
  2554. wpa_supplicant_deauthenticate(wpa_s,
  2555. WLAN_REASON_DEAUTH_LEAVING);
  2556. wpa_drv_set_countermeasures(wpa_s, 0);
  2557. wpa_clear_keys(wpa_s, NULL);
  2558. }
  2559. wpa_supplicant_cleanup(wpa_s);
  2560. #ifdef CONFIG_P2P
  2561. if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
  2562. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
  2563. "the management interface is being removed");
  2564. wpas_p2p_deinit_global(wpa_s->global);
  2565. }
  2566. #endif /* CONFIG_P2P */
  2567. if (wpa_s->drv_priv)
  2568. wpa_drv_deinit(wpa_s);
  2569. if (notify)
  2570. wpas_notify_iface_removed(wpa_s);
  2571. if (terminate)
  2572. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
  2573. if (wpa_s->ctrl_iface) {
  2574. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  2575. wpa_s->ctrl_iface = NULL;
  2576. }
  2577. if (wpa_s->conf != NULL) {
  2578. wpa_config_free(wpa_s->conf);
  2579. wpa_s->conf = NULL;
  2580. }
  2581. }
  2582. /**
  2583. * wpa_supplicant_add_iface - Add a new network interface
  2584. * @global: Pointer to global data from wpa_supplicant_init()
  2585. * @iface: Interface configuration options
  2586. * Returns: Pointer to the created interface or %NULL on failure
  2587. *
  2588. * This function is used to add new network interfaces for %wpa_supplicant.
  2589. * This can be called before wpa_supplicant_run() to add interfaces before the
  2590. * main event loop has been started. In addition, new interfaces can be added
  2591. * dynamically while %wpa_supplicant is already running. This could happen,
  2592. * e.g., when a hotplug network adapter is inserted.
  2593. */
  2594. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  2595. struct wpa_interface *iface)
  2596. {
  2597. struct wpa_supplicant *wpa_s;
  2598. struct wpa_interface t_iface;
  2599. struct wpa_ssid *ssid;
  2600. if (global == NULL || iface == NULL)
  2601. return NULL;
  2602. wpa_s = wpa_supplicant_alloc();
  2603. if (wpa_s == NULL)
  2604. return NULL;
  2605. wpa_s->global = global;
  2606. t_iface = *iface;
  2607. if (global->params.override_driver) {
  2608. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  2609. "('%s' -> '%s')",
  2610. iface->driver, global->params.override_driver);
  2611. t_iface.driver = global->params.override_driver;
  2612. }
  2613. if (global->params.override_ctrl_interface) {
  2614. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  2615. "ctrl_interface ('%s' -> '%s')",
  2616. iface->ctrl_interface,
  2617. global->params.override_ctrl_interface);
  2618. t_iface.ctrl_interface =
  2619. global->params.override_ctrl_interface;
  2620. }
  2621. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  2622. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  2623. iface->ifname);
  2624. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  2625. os_free(wpa_s);
  2626. return NULL;
  2627. }
  2628. /* Notify the control interfaces about new iface */
  2629. if (wpas_notify_iface_added(wpa_s)) {
  2630. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  2631. os_free(wpa_s);
  2632. return NULL;
  2633. }
  2634. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2635. wpas_notify_network_added(wpa_s, ssid);
  2636. wpa_s->next = global->ifaces;
  2637. global->ifaces = wpa_s;
  2638. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  2639. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2640. return wpa_s;
  2641. }
  2642. /**
  2643. * wpa_supplicant_remove_iface - Remove a network interface
  2644. * @global: Pointer to global data from wpa_supplicant_init()
  2645. * @wpa_s: Pointer to the network interface to be removed
  2646. * Returns: 0 if interface was removed, -1 if interface was not found
  2647. *
  2648. * This function can be used to dynamically remove network interfaces from
  2649. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  2650. * addition, this function is used to remove all remaining interfaces when
  2651. * %wpa_supplicant is terminated.
  2652. */
  2653. int wpa_supplicant_remove_iface(struct wpa_global *global,
  2654. struct wpa_supplicant *wpa_s,
  2655. int terminate)
  2656. {
  2657. struct wpa_supplicant *prev;
  2658. /* Remove interface from the global list of interfaces */
  2659. prev = global->ifaces;
  2660. if (prev == wpa_s) {
  2661. global->ifaces = wpa_s->next;
  2662. } else {
  2663. while (prev && prev->next != wpa_s)
  2664. prev = prev->next;
  2665. if (prev == NULL)
  2666. return -1;
  2667. prev->next = wpa_s->next;
  2668. }
  2669. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  2670. if (global->p2p_group_formation == wpa_s)
  2671. global->p2p_group_formation = NULL;
  2672. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  2673. os_free(wpa_s);
  2674. return 0;
  2675. }
  2676. /**
  2677. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  2678. * @wpa_s: Pointer to the network interface
  2679. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  2680. */
  2681. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  2682. {
  2683. const char *eapol_method;
  2684. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  2685. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2686. return "NO-EAP";
  2687. }
  2688. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  2689. if (eapol_method == NULL)
  2690. return "UNKNOWN-EAP";
  2691. return eapol_method;
  2692. }
  2693. /**
  2694. * wpa_supplicant_get_iface - Get a new network interface
  2695. * @global: Pointer to global data from wpa_supplicant_init()
  2696. * @ifname: Interface name
  2697. * Returns: Pointer to the interface or %NULL if not found
  2698. */
  2699. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  2700. const char *ifname)
  2701. {
  2702. struct wpa_supplicant *wpa_s;
  2703. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2704. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2705. return wpa_s;
  2706. }
  2707. return NULL;
  2708. }
  2709. #ifndef CONFIG_NO_WPA_MSG
  2710. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  2711. {
  2712. struct wpa_supplicant *wpa_s = ctx;
  2713. if (wpa_s == NULL)
  2714. return NULL;
  2715. return wpa_s->ifname;
  2716. }
  2717. #endif /* CONFIG_NO_WPA_MSG */
  2718. /**
  2719. * wpa_supplicant_init - Initialize %wpa_supplicant
  2720. * @params: Parameters for %wpa_supplicant
  2721. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  2722. *
  2723. * This function is used to initialize %wpa_supplicant. After successful
  2724. * initialization, the returned data pointer can be used to add and remove
  2725. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  2726. */
  2727. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  2728. {
  2729. struct wpa_global *global;
  2730. int ret, i;
  2731. if (params == NULL)
  2732. return NULL;
  2733. #ifdef CONFIG_DRIVER_NDIS
  2734. {
  2735. void driver_ndis_init_ops(void);
  2736. driver_ndis_init_ops();
  2737. }
  2738. #endif /* CONFIG_DRIVER_NDIS */
  2739. #ifndef CONFIG_NO_WPA_MSG
  2740. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  2741. #endif /* CONFIG_NO_WPA_MSG */
  2742. wpa_debug_open_file(params->wpa_debug_file_path);
  2743. if (params->wpa_debug_syslog)
  2744. wpa_debug_open_syslog();
  2745. if (params->wpa_debug_tracing) {
  2746. ret = wpa_debug_open_linux_tracing();
  2747. if (ret) {
  2748. wpa_printf(MSG_ERROR,
  2749. "Failed to enable trace logging");
  2750. return NULL;
  2751. }
  2752. }
  2753. ret = eap_register_methods();
  2754. if (ret) {
  2755. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  2756. if (ret == -2)
  2757. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  2758. "the same EAP type.");
  2759. return NULL;
  2760. }
  2761. global = os_zalloc(sizeof(*global));
  2762. if (global == NULL)
  2763. return NULL;
  2764. dl_list_init(&global->p2p_srv_bonjour);
  2765. dl_list_init(&global->p2p_srv_upnp);
  2766. global->params.daemonize = params->daemonize;
  2767. global->params.wait_for_monitor = params->wait_for_monitor;
  2768. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  2769. if (params->pid_file)
  2770. global->params.pid_file = os_strdup(params->pid_file);
  2771. if (params->ctrl_interface)
  2772. global->params.ctrl_interface =
  2773. os_strdup(params->ctrl_interface);
  2774. if (params->override_driver)
  2775. global->params.override_driver =
  2776. os_strdup(params->override_driver);
  2777. if (params->override_ctrl_interface)
  2778. global->params.override_ctrl_interface =
  2779. os_strdup(params->override_ctrl_interface);
  2780. wpa_debug_level = global->params.wpa_debug_level =
  2781. params->wpa_debug_level;
  2782. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  2783. params->wpa_debug_show_keys;
  2784. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  2785. params->wpa_debug_timestamp;
  2786. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  2787. if (eloop_init()) {
  2788. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  2789. wpa_supplicant_deinit(global);
  2790. return NULL;
  2791. }
  2792. random_init(params->entropy_file);
  2793. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  2794. if (global->ctrl_iface == NULL) {
  2795. wpa_supplicant_deinit(global);
  2796. return NULL;
  2797. }
  2798. if (wpas_notify_supplicant_initialized(global)) {
  2799. wpa_supplicant_deinit(global);
  2800. return NULL;
  2801. }
  2802. for (i = 0; wpa_drivers[i]; i++)
  2803. global->drv_count++;
  2804. if (global->drv_count == 0) {
  2805. wpa_printf(MSG_ERROR, "No drivers enabled");
  2806. wpa_supplicant_deinit(global);
  2807. return NULL;
  2808. }
  2809. global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
  2810. if (global->drv_priv == NULL) {
  2811. wpa_supplicant_deinit(global);
  2812. return NULL;
  2813. }
  2814. #ifdef CONFIG_WIFI_DISPLAY
  2815. if (wifi_display_init(global) < 0) {
  2816. wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
  2817. wpa_supplicant_deinit(global);
  2818. return NULL;
  2819. }
  2820. #endif /* CONFIG_WIFI_DISPLAY */
  2821. return global;
  2822. }
  2823. /**
  2824. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  2825. * @global: Pointer to global data from wpa_supplicant_init()
  2826. * Returns: 0 after successful event loop run, -1 on failure
  2827. *
  2828. * This function starts the main event loop and continues running as long as
  2829. * there are any remaining events. In most cases, this function is running as
  2830. * long as the %wpa_supplicant process in still in use.
  2831. */
  2832. int wpa_supplicant_run(struct wpa_global *global)
  2833. {
  2834. struct wpa_supplicant *wpa_s;
  2835. if (global->params.daemonize &&
  2836. wpa_supplicant_daemon(global->params.pid_file))
  2837. return -1;
  2838. if (global->params.wait_for_monitor) {
  2839. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  2840. if (wpa_s->ctrl_iface)
  2841. wpa_supplicant_ctrl_iface_wait(
  2842. wpa_s->ctrl_iface);
  2843. }
  2844. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  2845. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  2846. eloop_run();
  2847. return 0;
  2848. }
  2849. /**
  2850. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  2851. * @global: Pointer to global data from wpa_supplicant_init()
  2852. *
  2853. * This function is called to deinitialize %wpa_supplicant and to free all
  2854. * allocated resources. Remaining network interfaces will also be removed.
  2855. */
  2856. void wpa_supplicant_deinit(struct wpa_global *global)
  2857. {
  2858. int i;
  2859. if (global == NULL)
  2860. return;
  2861. #ifdef CONFIG_WIFI_DISPLAY
  2862. wifi_display_deinit(global);
  2863. #endif /* CONFIG_WIFI_DISPLAY */
  2864. #ifdef CONFIG_P2P
  2865. wpas_p2p_deinit_global(global);
  2866. #endif /* CONFIG_P2P */
  2867. while (global->ifaces)
  2868. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  2869. if (global->ctrl_iface)
  2870. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  2871. wpas_notify_supplicant_deinitialized(global);
  2872. eap_peer_unregister_methods();
  2873. #ifdef CONFIG_AP
  2874. eap_server_unregister_methods();
  2875. #endif /* CONFIG_AP */
  2876. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  2877. if (!global->drv_priv[i])
  2878. continue;
  2879. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  2880. }
  2881. os_free(global->drv_priv);
  2882. random_deinit();
  2883. eloop_destroy();
  2884. if (global->params.pid_file) {
  2885. os_daemonize_terminate(global->params.pid_file);
  2886. os_free(global->params.pid_file);
  2887. }
  2888. os_free(global->params.ctrl_interface);
  2889. os_free(global->params.override_driver);
  2890. os_free(global->params.override_ctrl_interface);
  2891. os_free(global->p2p_disallow_freq);
  2892. os_free(global);
  2893. wpa_debug_close_syslog();
  2894. wpa_debug_close_file();
  2895. wpa_debug_close_linux_tracing();
  2896. }
  2897. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  2898. {
  2899. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  2900. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2901. char country[3];
  2902. country[0] = wpa_s->conf->country[0];
  2903. country[1] = wpa_s->conf->country[1];
  2904. country[2] = '\0';
  2905. if (wpa_drv_set_country(wpa_s, country) < 0) {
  2906. wpa_printf(MSG_ERROR, "Failed to set country code "
  2907. "'%s'", country);
  2908. }
  2909. }
  2910. if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
  2911. wpas_init_ext_pw(wpa_s);
  2912. #ifdef CONFIG_WPS
  2913. wpas_wps_update_config(wpa_s);
  2914. #endif /* CONFIG_WPS */
  2915. #ifdef CONFIG_P2P
  2916. wpas_p2p_update_config(wpa_s);
  2917. #endif /* CONFIG_P2P */
  2918. wpa_s->conf->changed_parameters = 0;
  2919. }
  2920. static void add_freq(int *freqs, int *num_freqs, int freq)
  2921. {
  2922. int i;
  2923. for (i = 0; i < *num_freqs; i++) {
  2924. if (freqs[i] == freq)
  2925. return;
  2926. }
  2927. freqs[*num_freqs] = freq;
  2928. (*num_freqs)++;
  2929. }
  2930. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  2931. {
  2932. struct wpa_bss *bss, *cbss;
  2933. const int max_freqs = 10;
  2934. int *freqs;
  2935. int num_freqs = 0;
  2936. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  2937. if (freqs == NULL)
  2938. return NULL;
  2939. cbss = wpa_s->current_bss;
  2940. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2941. if (bss == cbss)
  2942. continue;
  2943. if (bss->ssid_len == cbss->ssid_len &&
  2944. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  2945. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  2946. add_freq(freqs, &num_freqs, bss->freq);
  2947. if (num_freqs == max_freqs)
  2948. break;
  2949. }
  2950. }
  2951. if (num_freqs == 0) {
  2952. os_free(freqs);
  2953. freqs = NULL;
  2954. }
  2955. return freqs;
  2956. }
  2957. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2958. {
  2959. int timeout;
  2960. int count;
  2961. int *freqs = NULL;
  2962. /*
  2963. * Remove possible authentication timeout since the connection failed.
  2964. */
  2965. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  2966. /*
  2967. * Add the failed BSSID into the blacklist and speed up next scan
  2968. * attempt if there could be other APs that could accept association.
  2969. * The current blacklist count indicates how many times we have tried
  2970. * connecting to this AP and multiple attempts mean that other APs are
  2971. * either not available or has already been tried, so that we can start
  2972. * increasing the delay here to avoid constant scanning.
  2973. */
  2974. count = wpa_blacklist_add(wpa_s, bssid);
  2975. if (count == 1 && wpa_s->current_bss) {
  2976. /*
  2977. * This BSS was not in the blacklist before. If there is
  2978. * another BSS available for the same ESS, we should try that
  2979. * next. Otherwise, we may as well try this one once more
  2980. * before allowing other, likely worse, ESSes to be considered.
  2981. */
  2982. freqs = get_bss_freqs_in_ess(wpa_s);
  2983. if (freqs) {
  2984. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  2985. "has been seen; try it next");
  2986. wpa_blacklist_add(wpa_s, bssid);
  2987. /*
  2988. * On the next scan, go through only the known channels
  2989. * used in this ESS based on previous scans to speed up
  2990. * common load balancing use case.
  2991. */
  2992. os_free(wpa_s->next_scan_freqs);
  2993. wpa_s->next_scan_freqs = freqs;
  2994. }
  2995. }
  2996. switch (count) {
  2997. case 1:
  2998. timeout = 100;
  2999. break;
  3000. case 2:
  3001. timeout = 500;
  3002. break;
  3003. case 3:
  3004. timeout = 1000;
  3005. break;
  3006. default:
  3007. timeout = 5000;
  3008. }
  3009. /*
  3010. * TODO: if more than one possible AP is available in scan results,
  3011. * could try the other ones before requesting a new scan.
  3012. */
  3013. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  3014. 1000 * (timeout % 1000));
  3015. #ifdef CONFIG_P2P
  3016. if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
  3017. wpa_s->global->p2p != NULL) {
  3018. wpa_s->global->p2p_cb_on_scan_complete = 0;
  3019. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  3020. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  3021. "continued after failed association");
  3022. }
  3023. }
  3024. #endif /* CONFIG_P2P */
  3025. }
  3026. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  3027. {
  3028. return wpa_s->conf->ap_scan == 2 ||
  3029. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  3030. }
  3031. #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
  3032. int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
  3033. struct wpa_ssid *ssid,
  3034. const char *field,
  3035. const char *value)
  3036. {
  3037. #ifdef IEEE8021X_EAPOL
  3038. struct eap_peer_config *eap = &ssid->eap;
  3039. wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
  3040. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
  3041. (const u8 *) value, os_strlen(value));
  3042. switch (wpa_supplicant_ctrl_req_from_string(field)) {
  3043. case WPA_CTRL_REQ_EAP_IDENTITY:
  3044. os_free(eap->identity);
  3045. eap->identity = (u8 *) os_strdup(value);
  3046. eap->identity_len = os_strlen(value);
  3047. eap->pending_req_identity = 0;
  3048. if (ssid == wpa_s->current_ssid)
  3049. wpa_s->reassociate = 1;
  3050. break;
  3051. case WPA_CTRL_REQ_EAP_PASSWORD:
  3052. os_free(eap->password);
  3053. eap->password = (u8 *) os_strdup(value);
  3054. eap->password_len = os_strlen(value);
  3055. eap->pending_req_password = 0;
  3056. if (ssid == wpa_s->current_ssid)
  3057. wpa_s->reassociate = 1;
  3058. break;
  3059. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  3060. os_free(eap->new_password);
  3061. eap->new_password = (u8 *) os_strdup(value);
  3062. eap->new_password_len = os_strlen(value);
  3063. eap->pending_req_new_password = 0;
  3064. if (ssid == wpa_s->current_ssid)
  3065. wpa_s->reassociate = 1;
  3066. break;
  3067. case WPA_CTRL_REQ_EAP_PIN:
  3068. os_free(eap->pin);
  3069. eap->pin = os_strdup(value);
  3070. eap->pending_req_pin = 0;
  3071. if (ssid == wpa_s->current_ssid)
  3072. wpa_s->reassociate = 1;
  3073. break;
  3074. case WPA_CTRL_REQ_EAP_OTP:
  3075. os_free(eap->otp);
  3076. eap->otp = (u8 *) os_strdup(value);
  3077. eap->otp_len = os_strlen(value);
  3078. os_free(eap->pending_req_otp);
  3079. eap->pending_req_otp = NULL;
  3080. eap->pending_req_otp_len = 0;
  3081. break;
  3082. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  3083. os_free(eap->private_key_passwd);
  3084. eap->private_key_passwd = (u8 *) os_strdup(value);
  3085. eap->pending_req_passphrase = 0;
  3086. if (ssid == wpa_s->current_ssid)
  3087. wpa_s->reassociate = 1;
  3088. break;
  3089. default:
  3090. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
  3091. return -1;
  3092. }
  3093. return 0;
  3094. #else /* IEEE8021X_EAPOL */
  3095. wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
  3096. return -1;
  3097. #endif /* IEEE8021X_EAPOL */
  3098. }
  3099. #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
  3100. int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  3101. {
  3102. int i;
  3103. unsigned int drv_enc;
  3104. if (ssid == NULL)
  3105. return 1;
  3106. if (ssid->disabled)
  3107. return 1;
  3108. if (wpa_s && wpa_s->drv_capa_known)
  3109. drv_enc = wpa_s->drv_enc;
  3110. else
  3111. drv_enc = (unsigned int) -1;
  3112. for (i = 0; i < NUM_WEP_KEYS; i++) {
  3113. size_t len = ssid->wep_key_len[i];
  3114. if (len == 0)
  3115. continue;
  3116. if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
  3117. continue;
  3118. if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
  3119. continue;
  3120. if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
  3121. continue;
  3122. return 1; /* invalid WEP key */
  3123. }
  3124. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  3125. !ssid->ext_psk)
  3126. return 1;
  3127. return 0;
  3128. }
  3129. int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
  3130. {
  3131. if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
  3132. return 1;
  3133. if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
  3134. return 0;
  3135. return -1;
  3136. }
  3137. void wpas_auth_failed(struct wpa_supplicant *wpa_s)
  3138. {
  3139. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3140. int dur;
  3141. struct os_time now;
  3142. if (ssid == NULL) {
  3143. wpa_printf(MSG_DEBUG, "Authentication failure but no known "
  3144. "SSID block");
  3145. return;
  3146. }
  3147. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  3148. return;
  3149. ssid->auth_failures++;
  3150. if (ssid->auth_failures > 50)
  3151. dur = 300;
  3152. else if (ssid->auth_failures > 20)
  3153. dur = 120;
  3154. else if (ssid->auth_failures > 10)
  3155. dur = 60;
  3156. else if (ssid->auth_failures > 5)
  3157. dur = 30;
  3158. else if (ssid->auth_failures > 1)
  3159. dur = 20;
  3160. else
  3161. dur = 10;
  3162. os_get_time(&now);
  3163. if (now.sec + dur <= ssid->disabled_until.sec)
  3164. return;
  3165. ssid->disabled_until.sec = now.sec + dur;
  3166. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
  3167. "id=%d ssid=\"%s\" auth_failures=%u duration=%d",
  3168. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  3169. ssid->auth_failures, dur);
  3170. }
  3171. void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
  3172. struct wpa_ssid *ssid, int clear_failures)
  3173. {
  3174. if (ssid == NULL)
  3175. return;
  3176. if (ssid->disabled_until.sec) {
  3177. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
  3178. "id=%d ssid=\"%s\"",
  3179. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  3180. }
  3181. ssid->disabled_until.sec = 0;
  3182. ssid->disabled_until.usec = 0;
  3183. if (clear_failures)
  3184. ssid->auth_failures = 0;
  3185. }
  3186. int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
  3187. {
  3188. size_t i;
  3189. if (wpa_s->disallow_aps_bssid == NULL)
  3190. return 0;
  3191. for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
  3192. if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
  3193. bssid, ETH_ALEN) == 0)
  3194. return 1;
  3195. }
  3196. return 0;
  3197. }
  3198. int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
  3199. size_t ssid_len)
  3200. {
  3201. size_t i;
  3202. if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
  3203. return 0;
  3204. for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
  3205. struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
  3206. if (ssid_len == s->ssid_len &&
  3207. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  3208. return 1;
  3209. }
  3210. return 0;
  3211. }