wpa_supplicant.c 111 KB

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