wpa_supplicant.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. *
  14. * This file implements functions for registering and unregistering
  15. * %wpa_supplicant interfaces. In addition, this file contains number of
  16. * functions for managing network connections.
  17. */
  18. #include "includes.h"
  19. #include "common.h"
  20. #include "crypto/random.h"
  21. #include "crypto/sha1.h"
  22. #include "eapol_supp/eapol_supp_sm.h"
  23. #include "eap_peer/eap.h"
  24. #include "eap_server/eap_methods.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "eloop.h"
  27. #include "config.h"
  28. #include "l2_packet/l2_packet.h"
  29. #include "wpa_supplicant_i.h"
  30. #include "driver_i.h"
  31. #include "ctrl_iface.h"
  32. #include "pcsc_funcs.h"
  33. #include "common/version.h"
  34. #include "rsn_supp/preauth.h"
  35. #include "rsn_supp/pmksa_cache.h"
  36. #include "common/wpa_ctrl.h"
  37. #include "mlme.h"
  38. #include "common/ieee802_11_defs.h"
  39. #include "p2p/p2p.h"
  40. #include "blacklist.h"
  41. #include "wpas_glue.h"
  42. #include "wps_supplicant.h"
  43. #include "ibss_rsn.h"
  44. #include "sme.h"
  45. #include "gas_query.h"
  46. #include "ap.h"
  47. #include "p2p_supplicant.h"
  48. #include "notify.h"
  49. #include "bgscan.h"
  50. #include "bss.h"
  51. #include "scan.h"
  52. #include "offchannel.h"
  53. const char *wpa_supplicant_version =
  54. "wpa_supplicant v" VERSION_STR "\n"
  55. "Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi> and contributors";
  56. const char *wpa_supplicant_license =
  57. "This program is free software. You can distribute it and/or modify it\n"
  58. "under the terms of the GNU General Public License version 2.\n"
  59. "\n"
  60. "Alternatively, this software may be distributed under the terms of the\n"
  61. "BSD license. See README and COPYING for more details.\n"
  62. #ifdef EAP_TLS_OPENSSL
  63. "\nThis product includes software developed by the OpenSSL Project\n"
  64. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  65. #endif /* EAP_TLS_OPENSSL */
  66. ;
  67. #ifndef CONFIG_NO_STDOUT_DEBUG
  68. /* Long text divided into parts in order to fit in C89 strings size limits. */
  69. const char *wpa_supplicant_full_license1 =
  70. "This program is free software; you can redistribute it and/or modify\n"
  71. "it under the terms of the GNU General Public License version 2 as\n"
  72. "published by the Free Software Foundation.\n"
  73. "\n"
  74. "This program is distributed in the hope that it will be useful,\n"
  75. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  76. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  77. "GNU General Public License for more details.\n"
  78. "\n";
  79. const char *wpa_supplicant_full_license2 =
  80. "You should have received a copy of the GNU General Public License\n"
  81. "along with this program; if not, write to the Free Software\n"
  82. "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
  83. "\n"
  84. "Alternatively, this software may be distributed under the terms of the\n"
  85. "BSD license.\n"
  86. "\n"
  87. "Redistribution and use in source and binary forms, with or without\n"
  88. "modification, are permitted provided that the following conditions are\n"
  89. "met:\n"
  90. "\n";
  91. const char *wpa_supplicant_full_license3 =
  92. "1. Redistributions of source code must retain the above copyright\n"
  93. " notice, this list of conditions and the following disclaimer.\n"
  94. "\n"
  95. "2. Redistributions in binary form must reproduce the above copyright\n"
  96. " notice, this list of conditions and the following disclaimer in the\n"
  97. " documentation and/or other materials provided with the distribution.\n"
  98. "\n";
  99. const char *wpa_supplicant_full_license4 =
  100. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  101. " names of its contributors may be used to endorse or promote products\n"
  102. " derived from this software without specific prior written permission.\n"
  103. "\n"
  104. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  105. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  106. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  107. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  108. const char *wpa_supplicant_full_license5 =
  109. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  110. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  111. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  112. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  113. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  114. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  115. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  116. "\n";
  117. #endif /* CONFIG_NO_STDOUT_DEBUG */
  118. extern int wpa_debug_level;
  119. extern int wpa_debug_show_keys;
  120. extern int wpa_debug_timestamp;
  121. extern struct wpa_driver_ops *wpa_drivers[];
  122. /* Configure default/group WEP keys for static WEP */
  123. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  124. {
  125. int i, set = 0;
  126. for (i = 0; i < NUM_WEP_KEYS; i++) {
  127. if (ssid->wep_key_len[i] == 0)
  128. continue;
  129. set = 1;
  130. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  131. i, i == ssid->wep_tx_keyidx, NULL, 0,
  132. ssid->wep_key[i], ssid->wep_key_len[i]);
  133. }
  134. return set;
  135. }
  136. static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  137. struct wpa_ssid *ssid)
  138. {
  139. u8 key[32];
  140. size_t keylen;
  141. enum wpa_alg alg;
  142. u8 seq[6] = { 0 };
  143. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  144. * sending unicast and multicast packets. */
  145. if (ssid->mode != WPAS_MODE_IBSS) {
  146. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  147. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  148. return -1;
  149. }
  150. if (!ssid->psk_set) {
  151. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  152. "WPA-None");
  153. return -1;
  154. }
  155. switch (wpa_s->group_cipher) {
  156. case WPA_CIPHER_CCMP:
  157. os_memcpy(key, ssid->psk, 16);
  158. keylen = 16;
  159. alg = WPA_ALG_CCMP;
  160. break;
  161. case WPA_CIPHER_TKIP:
  162. /* WPA-None uses the same Michael MIC key for both TX and RX */
  163. os_memcpy(key, ssid->psk, 16 + 8);
  164. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  165. keylen = 32;
  166. alg = WPA_ALG_TKIP;
  167. break;
  168. default:
  169. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  170. "WPA-None", wpa_s->group_cipher);
  171. return -1;
  172. }
  173. /* TODO: should actually remember the previously used seq#, both for TX
  174. * and RX from each STA.. */
  175. return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  176. }
  177. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  178. {
  179. struct wpa_supplicant *wpa_s = eloop_ctx;
  180. const u8 *bssid = wpa_s->bssid;
  181. if (is_zero_ether_addr(bssid))
  182. bssid = wpa_s->pending_bssid;
  183. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  184. MAC2STR(bssid));
  185. wpa_blacklist_add(wpa_s, bssid);
  186. wpa_sm_notify_disassoc(wpa_s->wpa);
  187. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  188. wpa_s->reassociate = 1;
  189. /*
  190. * If we timed out, the AP or the local radio may be busy.
  191. * So, wait a second until scanning again.
  192. */
  193. wpa_supplicant_req_scan(wpa_s, 1, 0);
  194. }
  195. /**
  196. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  197. * @wpa_s: Pointer to wpa_supplicant data
  198. * @sec: Number of seconds after which to time out authentication
  199. * @usec: Number of microseconds after which to time out authentication
  200. *
  201. * This function is used to schedule a timeout for the current authentication
  202. * attempt.
  203. */
  204. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  205. int sec, int usec)
  206. {
  207. if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
  208. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  209. return;
  210. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  211. "%d usec", sec, usec);
  212. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  213. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  214. }
  215. /**
  216. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  217. * @wpa_s: Pointer to wpa_supplicant data
  218. *
  219. * This function is used to cancel authentication timeout scheduled with
  220. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  221. * been completed.
  222. */
  223. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  224. {
  225. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  226. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  227. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  228. }
  229. /**
  230. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  231. * @wpa_s: Pointer to wpa_supplicant data
  232. *
  233. * This function is used to configure EAPOL state machine based on the selected
  234. * authentication mode.
  235. */
  236. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  237. {
  238. #ifdef IEEE8021X_EAPOL
  239. struct eapol_config eapol_conf;
  240. struct wpa_ssid *ssid = wpa_s->current_ssid;
  241. #ifdef CONFIG_IBSS_RSN
  242. if (ssid->mode == WPAS_MODE_IBSS &&
  243. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  244. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  245. /*
  246. * RSN IBSS authentication is per-STA and we can disable the
  247. * per-BSSID EAPOL authentication.
  248. */
  249. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  250. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  251. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  252. return;
  253. }
  254. #endif /* CONFIG_IBSS_RSN */
  255. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  256. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  257. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  258. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  259. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  260. else
  261. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  262. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  263. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  264. eapol_conf.accept_802_1x_keys = 1;
  265. eapol_conf.required_keys = 0;
  266. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  267. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  268. }
  269. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  270. eapol_conf.required_keys |=
  271. EAPOL_REQUIRE_KEY_BROADCAST;
  272. }
  273. if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  274. eapol_conf.required_keys = 0;
  275. }
  276. if (wpa_s->conf)
  277. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  278. eapol_conf.workaround = ssid->eap_workaround;
  279. eapol_conf.eap_disabled =
  280. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  281. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  282. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  283. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  284. #endif /* IEEE8021X_EAPOL */
  285. }
  286. /**
  287. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  288. * @wpa_s: Pointer to wpa_supplicant data
  289. * @ssid: Configuration data for the network
  290. *
  291. * This function is used to configure WPA state machine and related parameters
  292. * to a mode where WPA is not enabled. This is called as part of the
  293. * authentication configuration when the selected network does not use WPA.
  294. */
  295. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  296. struct wpa_ssid *ssid)
  297. {
  298. int i;
  299. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  300. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  301. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  302. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  303. else
  304. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  305. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  306. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  307. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  308. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  309. wpa_s->group_cipher = WPA_CIPHER_NONE;
  310. wpa_s->mgmt_group_cipher = 0;
  311. for (i = 0; i < NUM_WEP_KEYS; i++) {
  312. if (ssid->wep_key_len[i] > 5) {
  313. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  314. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  315. break;
  316. } else if (ssid->wep_key_len[i] > 0) {
  317. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  318. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  319. break;
  320. }
  321. }
  322. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  323. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  324. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  325. wpa_s->pairwise_cipher);
  326. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  327. #ifdef CONFIG_IEEE80211W
  328. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  329. wpa_s->mgmt_group_cipher);
  330. #endif /* CONFIG_IEEE80211W */
  331. pmksa_cache_clear_current(wpa_s->wpa);
  332. }
  333. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  334. {
  335. bgscan_deinit(wpa_s);
  336. scard_deinit(wpa_s->scard);
  337. wpa_s->scard = NULL;
  338. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  339. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  340. l2_packet_deinit(wpa_s->l2);
  341. wpa_s->l2 = NULL;
  342. if (wpa_s->l2_br) {
  343. l2_packet_deinit(wpa_s->l2_br);
  344. wpa_s->l2_br = NULL;
  345. }
  346. if (wpa_s->ctrl_iface) {
  347. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  348. wpa_s->ctrl_iface = NULL;
  349. }
  350. if (wpa_s->conf != NULL) {
  351. struct wpa_ssid *ssid;
  352. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  353. wpas_notify_network_removed(wpa_s, ssid);
  354. wpa_config_free(wpa_s->conf);
  355. wpa_s->conf = NULL;
  356. }
  357. os_free(wpa_s->confname);
  358. wpa_s->confname = NULL;
  359. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  360. eapol_sm_deinit(wpa_s->eapol);
  361. wpa_s->eapol = NULL;
  362. rsn_preauth_deinit(wpa_s->wpa);
  363. #ifdef CONFIG_TDLS
  364. wpa_tdls_deinit(wpa_s->wpa);
  365. #endif /* CONFIG_TDLS */
  366. pmksa_candidate_free(wpa_s->wpa);
  367. wpa_sm_deinit(wpa_s->wpa);
  368. wpa_s->wpa = NULL;
  369. wpa_blacklist_clear(wpa_s);
  370. wpa_bss_deinit(wpa_s);
  371. wpa_supplicant_cancel_scan(wpa_s);
  372. wpa_supplicant_cancel_auth_timeout(wpa_s);
  373. ieee80211_sta_deinit(wpa_s);
  374. wpas_wps_deinit(wpa_s);
  375. wpabuf_free(wpa_s->pending_eapol_rx);
  376. wpa_s->pending_eapol_rx = NULL;
  377. #ifdef CONFIG_IBSS_RSN
  378. ibss_rsn_deinit(wpa_s->ibss_rsn);
  379. wpa_s->ibss_rsn = NULL;
  380. #endif /* CONFIG_IBSS_RSN */
  381. sme_deinit(wpa_s);
  382. #ifdef CONFIG_AP
  383. wpa_supplicant_ap_deinit(wpa_s);
  384. #endif /* CONFIG_AP */
  385. #ifdef CONFIG_P2P
  386. wpas_p2p_deinit(wpa_s);
  387. #endif /* CONFIG_P2P */
  388. #ifdef CONFIG_OFFCHANNEL
  389. offchannel_deinit(wpa_s);
  390. #endif /* CONFIG_OFFCHANNEL */
  391. wpa_supplicant_cancel_sched_scan(wpa_s);
  392. os_free(wpa_s->next_scan_freqs);
  393. wpa_s->next_scan_freqs = NULL;
  394. gas_query_deinit(wpa_s->gas);
  395. wpa_s->gas = NULL;
  396. }
  397. /**
  398. * wpa_clear_keys - Clear keys configured for the driver
  399. * @wpa_s: Pointer to wpa_supplicant data
  400. * @addr: Previously used BSSID or %NULL if not available
  401. *
  402. * This function clears the encryption keys that has been previously configured
  403. * for the driver.
  404. */
  405. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  406. {
  407. if (wpa_s->keys_cleared) {
  408. /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
  409. * timing issues with keys being cleared just before new keys
  410. * are set or just after association or something similar. This
  411. * shows up in group key handshake failing often because of the
  412. * client not receiving the first encrypted packets correctly.
  413. * Skipping some of the extra key clearing steps seems to help
  414. * in completing group key handshake more reliably. */
  415. wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
  416. "skip key clearing");
  417. return;
  418. }
  419. /* MLME-DELETEKEYS.request */
  420. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  421. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  422. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  423. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  424. #ifdef CONFIG_IEEE80211W
  425. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  426. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  427. #endif /* CONFIG_IEEE80211W */
  428. if (addr) {
  429. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  430. 0);
  431. /* MLME-SETPROTECTION.request(None) */
  432. wpa_drv_mlme_setprotection(
  433. wpa_s, addr,
  434. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  435. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  436. }
  437. wpa_s->keys_cleared = 1;
  438. }
  439. /**
  440. * wpa_supplicant_state_txt - Get the connection state name as a text string
  441. * @state: State (wpa_state; WPA_*)
  442. * Returns: The state name as a printable text string
  443. */
  444. const char * wpa_supplicant_state_txt(enum wpa_states state)
  445. {
  446. switch (state) {
  447. case WPA_DISCONNECTED:
  448. return "DISCONNECTED";
  449. case WPA_INACTIVE:
  450. return "INACTIVE";
  451. case WPA_INTERFACE_DISABLED:
  452. return "INTERFACE_DISABLED";
  453. case WPA_SCANNING:
  454. return "SCANNING";
  455. case WPA_AUTHENTICATING:
  456. return "AUTHENTICATING";
  457. case WPA_ASSOCIATING:
  458. return "ASSOCIATING";
  459. case WPA_ASSOCIATED:
  460. return "ASSOCIATED";
  461. case WPA_4WAY_HANDSHAKE:
  462. return "4WAY_HANDSHAKE";
  463. case WPA_GROUP_HANDSHAKE:
  464. return "GROUP_HANDSHAKE";
  465. case WPA_COMPLETED:
  466. return "COMPLETED";
  467. default:
  468. return "UNKNOWN";
  469. }
  470. }
  471. #ifdef CONFIG_BGSCAN
  472. static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
  473. {
  474. if (wpas_driver_bss_selection(wpa_s))
  475. return;
  476. if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
  477. return;
  478. bgscan_deinit(wpa_s);
  479. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  480. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  481. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  482. "bgscan");
  483. /*
  484. * Live without bgscan; it is only used as a roaming
  485. * optimization, so the initial connection is not
  486. * affected.
  487. */
  488. } else
  489. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  490. } else
  491. wpa_s->bgscan_ssid = NULL;
  492. }
  493. static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
  494. {
  495. if (wpa_s->bgscan_ssid != NULL) {
  496. bgscan_deinit(wpa_s);
  497. wpa_s->bgscan_ssid = NULL;
  498. }
  499. }
  500. #endif /* CONFIG_BGSCAN */
  501. /**
  502. * wpa_supplicant_set_state - Set current connection state
  503. * @wpa_s: Pointer to wpa_supplicant data
  504. * @state: The new connection state
  505. *
  506. * This function is called whenever the connection state changes, e.g.,
  507. * association is completed for WPA/WPA2 4-Way Handshake is started.
  508. */
  509. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  510. enum wpa_states state)
  511. {
  512. enum wpa_states old_state = wpa_s->wpa_state;
  513. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  514. wpa_supplicant_state_txt(wpa_s->wpa_state),
  515. wpa_supplicant_state_txt(state));
  516. if (state != WPA_SCANNING)
  517. wpa_supplicant_notify_scanning(wpa_s, 0);
  518. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  519. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  520. struct wpa_ssid *ssid = wpa_s->current_ssid;
  521. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  522. MACSTR " completed %s [id=%d id_str=%s]",
  523. MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
  524. "(reauth)" : "(auth)",
  525. ssid ? ssid->id : -1,
  526. ssid && ssid->id_str ? ssid->id_str : "");
  527. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  528. wpa_s->new_connection = 0;
  529. wpa_s->reassociated_connection = 1;
  530. wpa_drv_set_operstate(wpa_s, 1);
  531. #ifndef IEEE8021X_EAPOL
  532. wpa_drv_set_supp_port(wpa_s, 1);
  533. #endif /* IEEE8021X_EAPOL */
  534. wpa_s->after_wps = 0;
  535. #ifdef CONFIG_P2P
  536. wpas_p2p_completed(wpa_s);
  537. #endif /* CONFIG_P2P */
  538. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  539. state == WPA_ASSOCIATED) {
  540. wpa_s->new_connection = 1;
  541. wpa_drv_set_operstate(wpa_s, 0);
  542. #ifndef IEEE8021X_EAPOL
  543. wpa_drv_set_supp_port(wpa_s, 0);
  544. #endif /* IEEE8021X_EAPOL */
  545. }
  546. wpa_s->wpa_state = state;
  547. #ifdef CONFIG_BGSCAN
  548. if (state == WPA_COMPLETED)
  549. wpa_supplicant_start_bgscan(wpa_s);
  550. else
  551. wpa_supplicant_stop_bgscan(wpa_s);
  552. #endif /* CONFIG_BGSCAN */
  553. if (wpa_s->wpa_state != old_state) {
  554. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  555. if (wpa_s->wpa_state == WPA_COMPLETED ||
  556. old_state == WPA_COMPLETED)
  557. wpas_notify_auth_changed(wpa_s);
  558. }
  559. }
  560. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  561. {
  562. int pending = 0;
  563. #ifdef CONFIG_WPS
  564. struct wpa_supplicant *wpa_s = global->ifaces;
  565. while (wpa_s) {
  566. if (wpas_wps_terminate_pending(wpa_s) == 1)
  567. pending = 1;
  568. wpa_s = wpa_s->next;
  569. }
  570. #endif /* CONFIG_WPS */
  571. if (pending)
  572. return;
  573. eloop_terminate();
  574. }
  575. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  576. {
  577. struct wpa_global *global = signal_ctx;
  578. struct wpa_supplicant *wpa_s;
  579. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  580. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
  581. "received", sig);
  582. }
  583. wpa_supplicant_terminate_proc(global);
  584. }
  585. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  586. {
  587. enum wpa_states old_state = wpa_s->wpa_state;
  588. wpa_s->pairwise_cipher = 0;
  589. wpa_s->group_cipher = 0;
  590. wpa_s->mgmt_group_cipher = 0;
  591. wpa_s->key_mgmt = 0;
  592. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  593. wpa_s->wpa_state = WPA_DISCONNECTED;
  594. if (wpa_s->wpa_state != old_state)
  595. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  596. }
  597. /**
  598. * wpa_supplicant_reload_configuration - Reload configuration data
  599. * @wpa_s: Pointer to wpa_supplicant data
  600. * Returns: 0 on success or -1 if configuration parsing failed
  601. *
  602. * This function can be used to request that the configuration data is reloaded
  603. * (e.g., after configuration file change). This function is reloading
  604. * configuration only for one interface, so this may need to be called multiple
  605. * times if %wpa_supplicant is controlling multiple interfaces and all
  606. * interfaces need reconfiguration.
  607. */
  608. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  609. {
  610. struct wpa_config *conf;
  611. int reconf_ctrl;
  612. int old_ap_scan;
  613. if (wpa_s->confname == NULL)
  614. return -1;
  615. conf = wpa_config_read(wpa_s->confname);
  616. if (conf == NULL) {
  617. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  618. "file '%s' - exiting", wpa_s->confname);
  619. return -1;
  620. }
  621. conf->changed_parameters = (unsigned int) -1;
  622. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  623. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  624. os_strcmp(conf->ctrl_interface,
  625. wpa_s->conf->ctrl_interface) != 0);
  626. if (reconf_ctrl && wpa_s->ctrl_iface) {
  627. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  628. wpa_s->ctrl_iface = NULL;
  629. }
  630. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  631. if (wpa_s->current_ssid) {
  632. wpa_supplicant_deauthenticate(wpa_s,
  633. WLAN_REASON_DEAUTH_LEAVING);
  634. }
  635. /*
  636. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  637. * pkcs11_engine_path, pkcs11_module_path.
  638. */
  639. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  640. /*
  641. * Clear forced success to clear EAP state for next
  642. * authentication.
  643. */
  644. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  645. }
  646. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  647. wpa_sm_set_config(wpa_s->wpa, NULL);
  648. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  649. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  650. rsn_preauth_deinit(wpa_s->wpa);
  651. old_ap_scan = wpa_s->conf->ap_scan;
  652. wpa_config_free(wpa_s->conf);
  653. wpa_s->conf = conf;
  654. if (old_ap_scan != wpa_s->conf->ap_scan)
  655. wpas_notify_ap_scan_changed(wpa_s);
  656. if (reconf_ctrl)
  657. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  658. wpa_supplicant_update_config(wpa_s);
  659. wpa_supplicant_clear_status(wpa_s);
  660. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  661. wpa_s->reassociate = 1;
  662. wpa_supplicant_req_scan(wpa_s, 0, 0);
  663. }
  664. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  665. return 0;
  666. }
  667. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  668. {
  669. struct wpa_global *global = signal_ctx;
  670. struct wpa_supplicant *wpa_s;
  671. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  672. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  673. sig);
  674. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  675. wpa_supplicant_terminate_proc(global);
  676. }
  677. }
  678. }
  679. enum wpa_cipher cipher_suite2driver(int cipher)
  680. {
  681. switch (cipher) {
  682. case WPA_CIPHER_NONE:
  683. return CIPHER_NONE;
  684. case WPA_CIPHER_WEP40:
  685. return CIPHER_WEP40;
  686. case WPA_CIPHER_WEP104:
  687. return CIPHER_WEP104;
  688. case WPA_CIPHER_CCMP:
  689. return CIPHER_CCMP;
  690. case WPA_CIPHER_TKIP:
  691. default:
  692. return CIPHER_TKIP;
  693. }
  694. }
  695. enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
  696. {
  697. switch (key_mgmt) {
  698. case WPA_KEY_MGMT_NONE:
  699. return KEY_MGMT_NONE;
  700. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  701. return KEY_MGMT_802_1X_NO_WPA;
  702. case WPA_KEY_MGMT_IEEE8021X:
  703. return KEY_MGMT_802_1X;
  704. case WPA_KEY_MGMT_WPA_NONE:
  705. return KEY_MGMT_WPA_NONE;
  706. case WPA_KEY_MGMT_FT_IEEE8021X:
  707. return KEY_MGMT_FT_802_1X;
  708. case WPA_KEY_MGMT_FT_PSK:
  709. return KEY_MGMT_FT_PSK;
  710. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  711. return KEY_MGMT_802_1X_SHA256;
  712. case WPA_KEY_MGMT_PSK_SHA256:
  713. return KEY_MGMT_PSK_SHA256;
  714. case WPA_KEY_MGMT_WPS:
  715. return KEY_MGMT_WPS;
  716. case WPA_KEY_MGMT_PSK:
  717. default:
  718. return KEY_MGMT_PSK;
  719. }
  720. }
  721. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  722. struct wpa_ssid *ssid,
  723. struct wpa_ie_data *ie)
  724. {
  725. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  726. if (ret) {
  727. if (ret == -2) {
  728. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  729. "from association info");
  730. }
  731. return -1;
  732. }
  733. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  734. "cipher suites");
  735. if (!(ie->group_cipher & ssid->group_cipher)) {
  736. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  737. "cipher 0x%x (mask 0x%x) - reject",
  738. ie->group_cipher, ssid->group_cipher);
  739. return -1;
  740. }
  741. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  742. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  743. "cipher 0x%x (mask 0x%x) - reject",
  744. ie->pairwise_cipher, ssid->pairwise_cipher);
  745. return -1;
  746. }
  747. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  748. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  749. "management 0x%x (mask 0x%x) - reject",
  750. ie->key_mgmt, ssid->key_mgmt);
  751. return -1;
  752. }
  753. #ifdef CONFIG_IEEE80211W
  754. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  755. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  756. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  757. "that does not support management frame protection - "
  758. "reject");
  759. return -1;
  760. }
  761. #endif /* CONFIG_IEEE80211W */
  762. return 0;
  763. }
  764. /**
  765. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  766. * @wpa_s: Pointer to wpa_supplicant data
  767. * @bss: Scan results for the selected BSS, or %NULL if not available
  768. * @ssid: Configuration data for the selected network
  769. * @wpa_ie: Buffer for the WPA/RSN IE
  770. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  771. * used buffer length in case the functions returns success.
  772. * Returns: 0 on success or -1 on failure
  773. *
  774. * This function is used to configure authentication and encryption parameters
  775. * based on the network configuration and scan result for the selected BSS (if
  776. * available).
  777. */
  778. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  779. struct wpa_bss *bss, struct wpa_ssid *ssid,
  780. u8 *wpa_ie, size_t *wpa_ie_len)
  781. {
  782. struct wpa_ie_data ie;
  783. int sel, proto;
  784. const u8 *bss_wpa, *bss_rsn;
  785. if (bss) {
  786. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  787. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  788. } else
  789. bss_wpa = bss_rsn = NULL;
  790. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  791. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  792. (ie.group_cipher & ssid->group_cipher) &&
  793. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  794. (ie.key_mgmt & ssid->key_mgmt)) {
  795. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  796. proto = WPA_PROTO_RSN;
  797. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  798. wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
  799. (ie.group_cipher & ssid->group_cipher) &&
  800. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  801. (ie.key_mgmt & ssid->key_mgmt)) {
  802. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  803. proto = WPA_PROTO_WPA;
  804. } else if (bss) {
  805. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  806. return -1;
  807. } else {
  808. if (ssid->proto & WPA_PROTO_RSN)
  809. proto = WPA_PROTO_RSN;
  810. else
  811. proto = WPA_PROTO_WPA;
  812. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  813. os_memset(&ie, 0, sizeof(ie));
  814. ie.group_cipher = ssid->group_cipher;
  815. ie.pairwise_cipher = ssid->pairwise_cipher;
  816. ie.key_mgmt = ssid->key_mgmt;
  817. #ifdef CONFIG_IEEE80211W
  818. ie.mgmt_group_cipher =
  819. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  820. WPA_CIPHER_AES_128_CMAC : 0;
  821. #endif /* CONFIG_IEEE80211W */
  822. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  823. "based on configuration");
  824. } else
  825. proto = ie.proto;
  826. }
  827. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  828. "pairwise %d key_mgmt %d proto %d",
  829. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  830. #ifdef CONFIG_IEEE80211W
  831. if (ssid->ieee80211w) {
  832. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  833. ie.mgmt_group_cipher);
  834. }
  835. #endif /* CONFIG_IEEE80211W */
  836. wpa_s->wpa_proto = proto;
  837. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  838. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  839. !!(ssid->proto & WPA_PROTO_RSN));
  840. if (bss || !wpa_s->ap_ies_from_associnfo) {
  841. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  842. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  843. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  844. bss_rsn ? 2 + bss_rsn[1] : 0))
  845. return -1;
  846. }
  847. sel = ie.group_cipher & ssid->group_cipher;
  848. if (sel & WPA_CIPHER_CCMP) {
  849. wpa_s->group_cipher = WPA_CIPHER_CCMP;
  850. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
  851. } else if (sel & WPA_CIPHER_TKIP) {
  852. wpa_s->group_cipher = WPA_CIPHER_TKIP;
  853. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
  854. } else if (sel & WPA_CIPHER_WEP104) {
  855. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  856. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
  857. } else if (sel & WPA_CIPHER_WEP40) {
  858. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  859. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
  860. } else {
  861. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  862. "cipher");
  863. return -1;
  864. }
  865. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  866. if (sel & WPA_CIPHER_CCMP) {
  867. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  868. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
  869. } else if (sel & WPA_CIPHER_TKIP) {
  870. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  871. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
  872. } else if (sel & WPA_CIPHER_NONE) {
  873. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  874. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
  875. } else {
  876. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  877. "cipher");
  878. return -1;
  879. }
  880. sel = ie.key_mgmt & ssid->key_mgmt;
  881. if (0) {
  882. #ifdef CONFIG_IEEE80211R
  883. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  884. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  885. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  886. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  887. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  888. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  889. #endif /* CONFIG_IEEE80211R */
  890. #ifdef CONFIG_IEEE80211W
  891. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  892. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  893. wpa_dbg(wpa_s, MSG_DEBUG,
  894. "WPA: using KEY_MGMT 802.1X with SHA256");
  895. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  896. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  897. wpa_dbg(wpa_s, MSG_DEBUG,
  898. "WPA: using KEY_MGMT PSK with SHA256");
  899. #endif /* CONFIG_IEEE80211W */
  900. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  901. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  902. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  903. } else if (sel & WPA_KEY_MGMT_PSK) {
  904. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  905. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  906. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  907. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  908. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  909. } else {
  910. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  911. "authenticated key management type");
  912. return -1;
  913. }
  914. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  915. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  916. wpa_s->pairwise_cipher);
  917. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  918. #ifdef CONFIG_IEEE80211W
  919. sel = ie.mgmt_group_cipher;
  920. if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  921. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  922. sel = 0;
  923. if (sel & WPA_CIPHER_AES_128_CMAC) {
  924. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  925. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  926. "AES-128-CMAC");
  927. } else {
  928. wpa_s->mgmt_group_cipher = 0;
  929. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  930. }
  931. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  932. wpa_s->mgmt_group_cipher);
  933. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
  934. #endif /* CONFIG_IEEE80211W */
  935. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  936. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  937. return -1;
  938. }
  939. if (ssid->key_mgmt &
  940. (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
  941. {
  942. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
  943. #ifndef CONFIG_NO_PBKDF2
  944. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  945. ssid->passphrase) {
  946. u8 psk[PMK_LEN];
  947. pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
  948. bss->ssid_len, 4096, psk, PMK_LEN);
  949. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  950. psk, PMK_LEN);
  951. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  952. }
  953. #endif /* CONFIG_NO_PBKDF2 */
  954. } else
  955. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  956. return 0;
  957. }
  958. /**
  959. * wpa_supplicant_associate - Request association
  960. * @wpa_s: Pointer to wpa_supplicant data
  961. * @bss: Scan results for the selected BSS, or %NULL if not available
  962. * @ssid: Configuration data for the selected network
  963. *
  964. * This function is used to request %wpa_supplicant to associate with a BSS.
  965. */
  966. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  967. struct wpa_bss *bss, struct wpa_ssid *ssid)
  968. {
  969. u8 wpa_ie[200];
  970. size_t wpa_ie_len;
  971. int use_crypt, ret, i, bssid_changed;
  972. int algs = WPA_AUTH_ALG_OPEN;
  973. enum wpa_cipher cipher_pairwise, cipher_group;
  974. struct wpa_driver_associate_params params;
  975. int wep_keys_set = 0;
  976. struct wpa_driver_capa capa;
  977. int assoc_failed = 0;
  978. struct wpa_ssid *old_ssid;
  979. #ifdef CONFIG_IBSS_RSN
  980. ibss_rsn_deinit(wpa_s->ibss_rsn);
  981. wpa_s->ibss_rsn = NULL;
  982. #endif /* CONFIG_IBSS_RSN */
  983. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  984. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  985. #ifdef CONFIG_AP
  986. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  987. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  988. "mode");
  989. return;
  990. }
  991. wpa_supplicant_create_ap(wpa_s, ssid);
  992. wpa_s->current_bss = bss;
  993. #else /* CONFIG_AP */
  994. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  995. "the build");
  996. #endif /* CONFIG_AP */
  997. return;
  998. }
  999. #ifdef CONFIG_TDLS
  1000. if (bss)
  1001. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  1002. bss->ie_len);
  1003. #endif /* CONFIG_TDLS */
  1004. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1005. ssid->mode == IEEE80211_MODE_INFRA) {
  1006. sme_authenticate(wpa_s, bss, ssid);
  1007. return;
  1008. }
  1009. os_memset(&params, 0, sizeof(params));
  1010. wpa_s->reassociate = 0;
  1011. if (bss && !wpas_driver_bss_selection(wpa_s)) {
  1012. #ifdef CONFIG_IEEE80211R
  1013. const u8 *ie, *md = NULL;
  1014. #endif /* CONFIG_IEEE80211R */
  1015. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1016. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1017. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1018. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1019. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1020. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1021. if (bssid_changed)
  1022. wpas_notify_bssid_changed(wpa_s);
  1023. #ifdef CONFIG_IEEE80211R
  1024. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1025. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1026. md = ie + 2;
  1027. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1028. if (md) {
  1029. /* Prepare for the next transition */
  1030. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1031. }
  1032. #endif /* CONFIG_IEEE80211R */
  1033. #ifdef CONFIG_WPS
  1034. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1035. wpa_s->conf->ap_scan == 2 &&
  1036. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1037. /* Use ap_scan==1 style network selection to find the network
  1038. */
  1039. wpa_s->scan_req = 2;
  1040. wpa_s->reassociate = 1;
  1041. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1042. return;
  1043. #endif /* CONFIG_WPS */
  1044. } else {
  1045. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1046. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1047. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1048. }
  1049. wpa_supplicant_cancel_sched_scan(wpa_s);
  1050. wpa_supplicant_cancel_scan(wpa_s);
  1051. /* Starting new association, so clear the possibly used WPA IE from the
  1052. * previous association. */
  1053. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1054. #ifdef IEEE8021X_EAPOL
  1055. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1056. if (ssid->leap) {
  1057. if (ssid->non_leap == 0)
  1058. algs = WPA_AUTH_ALG_LEAP;
  1059. else
  1060. algs |= WPA_AUTH_ALG_LEAP;
  1061. }
  1062. }
  1063. #endif /* IEEE8021X_EAPOL */
  1064. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1065. if (ssid->auth_alg) {
  1066. algs = ssid->auth_alg;
  1067. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1068. "0x%x", algs);
  1069. }
  1070. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1071. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1072. (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  1073. WPA_KEY_MGMT_FT_IEEE8021X |
  1074. WPA_KEY_MGMT_FT_PSK |
  1075. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  1076. WPA_KEY_MGMT_PSK_SHA256))) {
  1077. int try_opportunistic;
  1078. try_opportunistic = ssid->proactive_key_caching &&
  1079. (ssid->proto & WPA_PROTO_RSN);
  1080. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1081. wpa_s->current_ssid,
  1082. try_opportunistic) == 0)
  1083. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  1084. wpa_ie_len = sizeof(wpa_ie);
  1085. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1086. wpa_ie, &wpa_ie_len)) {
  1087. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1088. "key management and encryption suites");
  1089. return;
  1090. }
  1091. } else if (ssid->key_mgmt &
  1092. (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  1093. WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
  1094. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
  1095. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  1096. wpa_ie_len = sizeof(wpa_ie);
  1097. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1098. wpa_ie, &wpa_ie_len)) {
  1099. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1100. "key management and encryption suites (no "
  1101. "scan results)");
  1102. return;
  1103. }
  1104. #ifdef CONFIG_WPS
  1105. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1106. struct wpabuf *wps_ie;
  1107. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1108. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1109. wpa_ie_len = wpabuf_len(wps_ie);
  1110. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1111. } else
  1112. wpa_ie_len = 0;
  1113. wpabuf_free(wps_ie);
  1114. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1115. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1116. params.wps = WPS_MODE_PRIVACY;
  1117. else
  1118. params.wps = WPS_MODE_OPEN;
  1119. wpa_s->wpa_proto = 0;
  1120. #endif /* CONFIG_WPS */
  1121. } else {
  1122. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1123. wpa_ie_len = 0;
  1124. wpa_s->wpa_proto = 0;
  1125. }
  1126. #ifdef CONFIG_P2P
  1127. if (wpa_s->global->p2p) {
  1128. u8 *pos;
  1129. size_t len;
  1130. int res;
  1131. int p2p_group;
  1132. p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
  1133. pos = wpa_ie + wpa_ie_len;
  1134. len = sizeof(wpa_ie) - wpa_ie_len;
  1135. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
  1136. if (res >= 0)
  1137. wpa_ie_len += res;
  1138. }
  1139. wpa_s->cross_connect_disallowed = 0;
  1140. if (bss) {
  1141. struct wpabuf *p2p;
  1142. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1143. if (p2p) {
  1144. wpa_s->cross_connect_disallowed =
  1145. p2p_get_cross_connect_disallowed(p2p);
  1146. wpabuf_free(p2p);
  1147. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1148. "connection",
  1149. wpa_s->cross_connect_disallowed ?
  1150. "disallows" : "allows");
  1151. }
  1152. }
  1153. #endif /* CONFIG_P2P */
  1154. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1155. use_crypt = 1;
  1156. cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
  1157. cipher_group = cipher_suite2driver(wpa_s->group_cipher);
  1158. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1159. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1160. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  1161. use_crypt = 0;
  1162. if (wpa_set_wep_keys(wpa_s, ssid)) {
  1163. use_crypt = 1;
  1164. wep_keys_set = 1;
  1165. }
  1166. }
  1167. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  1168. use_crypt = 0;
  1169. #ifdef IEEE8021X_EAPOL
  1170. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1171. if ((ssid->eapol_flags &
  1172. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  1173. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  1174. !wep_keys_set) {
  1175. use_crypt = 0;
  1176. } else {
  1177. /* Assume that dynamic WEP-104 keys will be used and
  1178. * set cipher suites in order for drivers to expect
  1179. * encryption. */
  1180. cipher_pairwise = cipher_group = CIPHER_WEP104;
  1181. }
  1182. }
  1183. #endif /* IEEE8021X_EAPOL */
  1184. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1185. /* Set the key before (and later after) association */
  1186. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1187. }
  1188. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1189. if (bss) {
  1190. params.ssid = bss->ssid;
  1191. params.ssid_len = bss->ssid_len;
  1192. if (!wpas_driver_bss_selection(wpa_s)) {
  1193. params.bssid = bss->bssid;
  1194. params.freq = bss->freq;
  1195. }
  1196. } else {
  1197. params.ssid = ssid->ssid;
  1198. params.ssid_len = ssid->ssid_len;
  1199. }
  1200. if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
  1201. params.freq == 0)
  1202. params.freq = ssid->frequency; /* Initial channel for IBSS */
  1203. params.wpa_ie = wpa_ie;
  1204. params.wpa_ie_len = wpa_ie_len;
  1205. params.pairwise_suite = cipher_pairwise;
  1206. params.group_suite = cipher_group;
  1207. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  1208. params.wpa_proto = wpa_s->wpa_proto;
  1209. params.auth_alg = algs;
  1210. params.mode = ssid->mode;
  1211. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1212. if (ssid->wep_key_len[i])
  1213. params.wep_key[i] = ssid->wep_key[i];
  1214. params.wep_key_len[i] = ssid->wep_key_len[i];
  1215. }
  1216. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1217. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1218. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  1219. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  1220. params.passphrase = ssid->passphrase;
  1221. if (ssid->psk_set)
  1222. params.psk = ssid->psk;
  1223. }
  1224. params.drop_unencrypted = use_crypt;
  1225. #ifdef CONFIG_IEEE80211W
  1226. params.mgmt_frame_protection = ssid->ieee80211w;
  1227. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  1228. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1229. struct wpa_ie_data ie;
  1230. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1231. ie.capabilities &
  1232. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1233. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1234. "MFP: require MFP");
  1235. params.mgmt_frame_protection =
  1236. MGMT_FRAME_PROTECTION_REQUIRED;
  1237. }
  1238. }
  1239. #endif /* CONFIG_IEEE80211W */
  1240. params.p2p = ssid->p2p_group;
  1241. if (wpa_s->parent->set_sta_uapsd)
  1242. params.uapsd = wpa_s->parent->sta_uapsd;
  1243. else
  1244. params.uapsd = -1;
  1245. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1246. ret = ieee80211_sta_associate(wpa_s, &params);
  1247. else
  1248. ret = wpa_drv_associate(wpa_s, &params);
  1249. if (ret < 0) {
  1250. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1251. "failed");
  1252. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1253. /*
  1254. * The driver is known to mean what is saying, so we
  1255. * can stop right here; the association will not
  1256. * succeed.
  1257. */
  1258. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1259. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1260. return;
  1261. }
  1262. /* try to continue anyway; new association will be tried again
  1263. * after timeout */
  1264. assoc_failed = 1;
  1265. }
  1266. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1267. /* Set the key after the association just in case association
  1268. * cleared the previously configured key. */
  1269. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1270. /* No need to timeout authentication since there is no key
  1271. * management. */
  1272. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1273. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1274. #ifdef CONFIG_IBSS_RSN
  1275. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1276. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1277. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1278. /*
  1279. * RSN IBSS authentication is per-STA and we can disable the
  1280. * per-BSSID authentication.
  1281. */
  1282. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1283. #endif /* CONFIG_IBSS_RSN */
  1284. } else {
  1285. /* Timeout for IEEE 802.11 authentication and association */
  1286. int timeout = 60;
  1287. if (assoc_failed) {
  1288. /* give IBSS a bit more time */
  1289. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1290. } else if (wpa_s->conf->ap_scan == 1) {
  1291. /* give IBSS a bit more time */
  1292. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1293. }
  1294. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1295. }
  1296. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1297. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1298. /* Set static WEP keys again */
  1299. wpa_set_wep_keys(wpa_s, ssid);
  1300. }
  1301. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1302. /*
  1303. * Do not allow EAP session resumption between different
  1304. * network configurations.
  1305. */
  1306. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1307. }
  1308. old_ssid = wpa_s->current_ssid;
  1309. wpa_s->current_ssid = ssid;
  1310. wpa_s->current_bss = bss;
  1311. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1312. wpa_supplicant_initiate_eapol(wpa_s);
  1313. if (old_ssid != wpa_s->current_ssid)
  1314. wpas_notify_network_changed(wpa_s);
  1315. }
  1316. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1317. const u8 *addr)
  1318. {
  1319. struct wpa_ssid *old_ssid;
  1320. wpa_clear_keys(wpa_s, addr);
  1321. wpa_supplicant_mark_disassoc(wpa_s);
  1322. old_ssid = wpa_s->current_ssid;
  1323. wpa_s->current_ssid = NULL;
  1324. wpa_s->current_bss = NULL;
  1325. wpa_sm_set_config(wpa_s->wpa, NULL);
  1326. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1327. if (old_ssid != wpa_s->current_ssid)
  1328. wpas_notify_network_changed(wpa_s);
  1329. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1330. }
  1331. /**
  1332. * wpa_supplicant_disassociate - Disassociate the current connection
  1333. * @wpa_s: Pointer to wpa_supplicant data
  1334. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1335. *
  1336. * This function is used to request %wpa_supplicant to disassociate with the
  1337. * current AP.
  1338. */
  1339. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1340. int reason_code)
  1341. {
  1342. u8 *addr = NULL;
  1343. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1344. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1345. ieee80211_sta_disassociate(wpa_s, reason_code);
  1346. else
  1347. wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
  1348. addr = wpa_s->bssid;
  1349. }
  1350. wpa_supplicant_clear_connection(wpa_s, addr);
  1351. }
  1352. /**
  1353. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1354. * @wpa_s: Pointer to wpa_supplicant data
  1355. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1356. *
  1357. * This function is used to request %wpa_supplicant to deauthenticate from the
  1358. * current AP.
  1359. */
  1360. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1361. int reason_code)
  1362. {
  1363. u8 *addr = NULL;
  1364. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1365. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1366. ieee80211_sta_deauthenticate(wpa_s, reason_code);
  1367. else
  1368. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
  1369. reason_code);
  1370. addr = wpa_s->bssid;
  1371. }
  1372. wpa_supplicant_clear_connection(wpa_s, addr);
  1373. }
  1374. /**
  1375. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1376. * @wpa_s: wpa_supplicant structure for a network interface
  1377. * @ssid: wpa_ssid structure for a configured network or %NULL
  1378. *
  1379. * Enables the specified network or all networks if no network specified.
  1380. */
  1381. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1382. struct wpa_ssid *ssid)
  1383. {
  1384. struct wpa_ssid *other_ssid;
  1385. int was_disabled;
  1386. if (ssid == NULL) {
  1387. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1388. other_ssid = other_ssid->next) {
  1389. if (other_ssid->disabled == 2)
  1390. continue; /* do not change persistent P2P group
  1391. * data */
  1392. if (other_ssid == wpa_s->current_ssid &&
  1393. other_ssid->disabled)
  1394. wpa_s->reassociate = 1;
  1395. was_disabled = other_ssid->disabled;
  1396. other_ssid->disabled = 0;
  1397. if (was_disabled != other_ssid->disabled)
  1398. wpas_notify_network_enabled_changed(
  1399. wpa_s, other_ssid);
  1400. }
  1401. if (wpa_s->reassociate)
  1402. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1403. } else if (ssid->disabled && ssid->disabled != 2) {
  1404. if (wpa_s->current_ssid == NULL) {
  1405. /*
  1406. * Try to reassociate since there is no current
  1407. * configuration and a new network was made available.
  1408. */
  1409. wpa_s->reassociate = 1;
  1410. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1411. }
  1412. was_disabled = ssid->disabled;
  1413. ssid->disabled = 0;
  1414. if (was_disabled != ssid->disabled)
  1415. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1416. }
  1417. }
  1418. /**
  1419. * wpa_supplicant_disable_network - Mark a configured network as disabled
  1420. * @wpa_s: wpa_supplicant structure for a network interface
  1421. * @ssid: wpa_ssid structure for a configured network or %NULL
  1422. *
  1423. * Disables the specified network or all networks if no network specified.
  1424. */
  1425. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  1426. struct wpa_ssid *ssid)
  1427. {
  1428. struct wpa_ssid *other_ssid;
  1429. int was_disabled;
  1430. if (ssid == NULL) {
  1431. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1432. other_ssid = other_ssid->next) {
  1433. was_disabled = other_ssid->disabled;
  1434. if (was_disabled == 2)
  1435. continue; /* do not change persistent P2P group
  1436. * data */
  1437. other_ssid->disabled = 1;
  1438. if (was_disabled != other_ssid->disabled)
  1439. wpas_notify_network_enabled_changed(
  1440. wpa_s, other_ssid);
  1441. }
  1442. if (wpa_s->current_ssid)
  1443. wpa_supplicant_disassociate(
  1444. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1445. } else if (ssid->disabled != 2) {
  1446. if (ssid == wpa_s->current_ssid)
  1447. wpa_supplicant_disassociate(
  1448. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1449. was_disabled = ssid->disabled;
  1450. ssid->disabled = 1;
  1451. if (was_disabled != ssid->disabled)
  1452. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1453. }
  1454. }
  1455. /**
  1456. * wpa_supplicant_select_network - Attempt association with a network
  1457. * @wpa_s: wpa_supplicant structure for a network interface
  1458. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  1459. */
  1460. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  1461. struct wpa_ssid *ssid)
  1462. {
  1463. struct wpa_ssid *other_ssid;
  1464. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
  1465. wpa_supplicant_disassociate(
  1466. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1467. /*
  1468. * Mark all other networks disabled or mark all networks enabled if no
  1469. * network specified.
  1470. */
  1471. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1472. other_ssid = other_ssid->next) {
  1473. int was_disabled = other_ssid->disabled;
  1474. if (was_disabled == 2)
  1475. continue; /* do not change persistent P2P group data */
  1476. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  1477. if (was_disabled != other_ssid->disabled)
  1478. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  1479. }
  1480. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
  1481. /* We are already associated with the selected network */
  1482. wpa_printf(MSG_DEBUG, "Already associated with the "
  1483. "selected network - do nothing");
  1484. return;
  1485. }
  1486. wpa_s->connect_without_scan = NULL;
  1487. wpa_s->disconnected = 0;
  1488. wpa_s->reassociate = 1;
  1489. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1490. if (ssid)
  1491. wpas_notify_network_selected(wpa_s, ssid);
  1492. }
  1493. /**
  1494. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  1495. * @wpa_s: wpa_supplicant structure for a network interface
  1496. * @ap_scan: AP scan mode
  1497. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  1498. *
  1499. */
  1500. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  1501. {
  1502. int old_ap_scan;
  1503. if (ap_scan < 0 || ap_scan > 2)
  1504. return -1;
  1505. old_ap_scan = wpa_s->conf->ap_scan;
  1506. wpa_s->conf->ap_scan = ap_scan;
  1507. if (old_ap_scan != wpa_s->conf->ap_scan)
  1508. wpas_notify_ap_scan_changed(wpa_s);
  1509. return 0;
  1510. }
  1511. /**
  1512. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  1513. * @wpa_s: wpa_supplicant structure for a network interface
  1514. * @expire_age: Expiration age in seconds
  1515. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  1516. *
  1517. */
  1518. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  1519. unsigned int bss_expire_age)
  1520. {
  1521. if (bss_expire_age < 10) {
  1522. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  1523. bss_expire_age);
  1524. return -1;
  1525. }
  1526. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  1527. bss_expire_age);
  1528. wpa_s->conf->bss_expiration_age = bss_expire_age;
  1529. return 0;
  1530. }
  1531. /**
  1532. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  1533. * @wpa_s: wpa_supplicant structure for a network interface
  1534. * @expire_count: number of scans after which an unseen BSS is reclaimed
  1535. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  1536. *
  1537. */
  1538. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  1539. unsigned int bss_expire_count)
  1540. {
  1541. if (bss_expire_count < 1) {
  1542. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  1543. bss_expire_count);
  1544. return -1;
  1545. }
  1546. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  1547. bss_expire_count);
  1548. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  1549. return 0;
  1550. }
  1551. /**
  1552. * wpa_supplicant_set_debug_params - Set global debug params
  1553. * @global: wpa_global structure
  1554. * @debug_level: debug level
  1555. * @debug_timestamp: determines if show timestamp in debug data
  1556. * @debug_show_keys: determines if show keys in debug data
  1557. * Returns: 0 if succeed or -1 if debug_level has wrong value
  1558. */
  1559. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  1560. int debug_timestamp, int debug_show_keys)
  1561. {
  1562. int old_level, old_timestamp, old_show_keys;
  1563. /* check for allowed debuglevels */
  1564. if (debug_level != MSG_EXCESSIVE &&
  1565. debug_level != MSG_MSGDUMP &&
  1566. debug_level != MSG_DEBUG &&
  1567. debug_level != MSG_INFO &&
  1568. debug_level != MSG_WARNING &&
  1569. debug_level != MSG_ERROR)
  1570. return -1;
  1571. old_level = wpa_debug_level;
  1572. old_timestamp = wpa_debug_timestamp;
  1573. old_show_keys = wpa_debug_show_keys;
  1574. wpa_debug_level = debug_level;
  1575. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  1576. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  1577. if (wpa_debug_level != old_level)
  1578. wpas_notify_debug_level_changed(global);
  1579. if (wpa_debug_timestamp != old_timestamp)
  1580. wpas_notify_debug_timestamp_changed(global);
  1581. if (wpa_debug_show_keys != old_show_keys)
  1582. wpas_notify_debug_show_keys_changed(global);
  1583. return 0;
  1584. }
  1585. /**
  1586. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1587. * @wpa_s: Pointer to wpa_supplicant data
  1588. * Returns: A pointer to the current network structure or %NULL on failure
  1589. */
  1590. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1591. {
  1592. struct wpa_ssid *entry;
  1593. u8 ssid[MAX_SSID_LEN];
  1594. int res;
  1595. size_t ssid_len;
  1596. u8 bssid[ETH_ALEN];
  1597. int wired;
  1598. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  1599. if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
  1600. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1601. "MLME");
  1602. return NULL;
  1603. }
  1604. } else {
  1605. res = wpa_drv_get_ssid(wpa_s, ssid);
  1606. if (res < 0) {
  1607. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1608. "driver");
  1609. return NULL;
  1610. }
  1611. ssid_len = res;
  1612. }
  1613. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1614. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  1615. else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1616. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  1617. "driver");
  1618. return NULL;
  1619. }
  1620. wired = wpa_s->conf->ap_scan == 0 &&
  1621. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  1622. entry = wpa_s->conf->ssid;
  1623. while (entry) {
  1624. if (!entry->disabled &&
  1625. ((ssid_len == entry->ssid_len &&
  1626. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1627. (!entry->bssid_set ||
  1628. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1629. return entry;
  1630. #ifdef CONFIG_WPS
  1631. if (!entry->disabled &&
  1632. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  1633. (entry->ssid == NULL || entry->ssid_len == 0) &&
  1634. (!entry->bssid_set ||
  1635. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1636. return entry;
  1637. #endif /* CONFIG_WPS */
  1638. if (!entry->disabled && entry->bssid_set &&
  1639. entry->ssid_len == 0 &&
  1640. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  1641. return entry;
  1642. entry = entry->next;
  1643. }
  1644. return NULL;
  1645. }
  1646. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1647. const char *name)
  1648. {
  1649. int i;
  1650. size_t len;
  1651. const char *pos, *driver = name;
  1652. if (wpa_s == NULL)
  1653. return -1;
  1654. if (wpa_drivers[0] == NULL) {
  1655. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  1656. "wpa_supplicant");
  1657. return -1;
  1658. }
  1659. if (name == NULL) {
  1660. /* default to first driver in the list */
  1661. wpa_s->driver = wpa_drivers[0];
  1662. wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
  1663. return 0;
  1664. }
  1665. do {
  1666. pos = os_strchr(driver, ',');
  1667. if (pos)
  1668. len = pos - driver;
  1669. else
  1670. len = os_strlen(driver);
  1671. for (i = 0; wpa_drivers[i]; i++) {
  1672. if (os_strlen(wpa_drivers[i]->name) == len &&
  1673. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  1674. 0) {
  1675. wpa_s->driver = wpa_drivers[i];
  1676. wpa_s->global_drv_priv =
  1677. wpa_s->global->drv_priv[i];
  1678. return 0;
  1679. }
  1680. }
  1681. driver = pos + 1;
  1682. } while (pos);
  1683. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  1684. return -1;
  1685. }
  1686. /**
  1687. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  1688. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  1689. * with struct wpa_driver_ops::init()
  1690. * @src_addr: Source address of the EAPOL frame
  1691. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  1692. * @len: Length of the EAPOL data
  1693. *
  1694. * This function is called for each received EAPOL frame. Most driver
  1695. * interfaces rely on more generic OS mechanism for receiving frames through
  1696. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  1697. * take care of received EAPOL frames and deliver them to the core supplicant
  1698. * code by calling this function.
  1699. */
  1700. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1701. const u8 *buf, size_t len)
  1702. {
  1703. struct wpa_supplicant *wpa_s = ctx;
  1704. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1705. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1706. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  1707. /*
  1708. * There is possible race condition between receiving the
  1709. * association event and the EAPOL frame since they are coming
  1710. * through different paths from the driver. In order to avoid
  1711. * issues in trying to process the EAPOL frame before receiving
  1712. * association information, lets queue it for processing until
  1713. * the association event is received.
  1714. */
  1715. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  1716. "of received EAPOL frame");
  1717. wpabuf_free(wpa_s->pending_eapol_rx);
  1718. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  1719. if (wpa_s->pending_eapol_rx) {
  1720. os_get_time(&wpa_s->pending_eapol_rx_time);
  1721. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  1722. ETH_ALEN);
  1723. }
  1724. return;
  1725. }
  1726. #ifdef CONFIG_AP
  1727. if (wpa_s->ap_iface) {
  1728. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  1729. return;
  1730. }
  1731. #endif /* CONFIG_AP */
  1732. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1733. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  1734. "no key management is configured");
  1735. return;
  1736. }
  1737. if (wpa_s->eapol_received == 0 &&
  1738. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  1739. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1740. wpa_s->wpa_state != WPA_COMPLETED) &&
  1741. (wpa_s->current_ssid == NULL ||
  1742. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  1743. /* Timeout for completing IEEE 802.1X and WPA authentication */
  1744. wpa_supplicant_req_auth_timeout(
  1745. wpa_s,
  1746. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1747. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  1748. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  1749. 70 : 10, 0);
  1750. }
  1751. wpa_s->eapol_received++;
  1752. if (wpa_s->countermeasures) {
  1753. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  1754. "EAPOL packet");
  1755. return;
  1756. }
  1757. #ifdef CONFIG_IBSS_RSN
  1758. if (wpa_s->current_ssid &&
  1759. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  1760. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  1761. return;
  1762. }
  1763. #endif /* CONFIG_IBSS_RSN */
  1764. /* Source address of the incoming EAPOL frame could be compared to the
  1765. * current BSSID. However, it is possible that a centralized
  1766. * Authenticator could be using another MAC address than the BSSID of
  1767. * an AP, so just allow any address to be used for now. The replies are
  1768. * still sent to the current BSSID (if available), though. */
  1769. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  1770. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  1771. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  1772. return;
  1773. wpa_drv_poll(wpa_s);
  1774. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  1775. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  1776. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1777. /*
  1778. * Set portValid = TRUE here since we are going to skip 4-way
  1779. * handshake processing which would normally set portValid. We
  1780. * need this to allow the EAPOL state machines to be completed
  1781. * without going through EAPOL-Key handshake.
  1782. */
  1783. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1784. }
  1785. }
  1786. /**
  1787. * wpa_supplicant_driver_init - Initialize driver interface parameters
  1788. * @wpa_s: Pointer to wpa_supplicant data
  1789. * Returns: 0 on success, -1 on failure
  1790. *
  1791. * This function is called to initialize driver interface parameters.
  1792. * wpa_drv_init() must have been called before this function to initialize the
  1793. * driver interface.
  1794. */
  1795. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  1796. {
  1797. static int interface_count = 0;
  1798. if (wpa_s->driver->send_eapol) {
  1799. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1800. if (addr)
  1801. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1802. } else if (!(wpa_s->drv_flags &
  1803. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  1804. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  1805. wpa_drv_get_mac_addr(wpa_s),
  1806. ETH_P_EAPOL,
  1807. wpa_supplicant_rx_eapol, wpa_s, 0);
  1808. if (wpa_s->l2 == NULL)
  1809. return -1;
  1810. } else {
  1811. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1812. if (addr)
  1813. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1814. }
  1815. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  1816. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  1817. return -1;
  1818. }
  1819. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  1820. MAC2STR(wpa_s->own_addr));
  1821. if (wpa_s->bridge_ifname[0]) {
  1822. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  1823. "interface '%s'", wpa_s->bridge_ifname);
  1824. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  1825. wpa_s->own_addr,
  1826. ETH_P_EAPOL,
  1827. wpa_supplicant_rx_eapol, wpa_s,
  1828. 0);
  1829. if (wpa_s->l2_br == NULL) {
  1830. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  1831. "connection for the bridge interface '%s'",
  1832. wpa_s->bridge_ifname);
  1833. return -1;
  1834. }
  1835. }
  1836. wpa_clear_keys(wpa_s, NULL);
  1837. /* Make sure that TKIP countermeasures are not left enabled (could
  1838. * happen if wpa_supplicant is killed during countermeasures. */
  1839. wpa_drv_set_countermeasures(wpa_s, 0);
  1840. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  1841. wpa_drv_flush_pmkid(wpa_s);
  1842. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  1843. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  1844. if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
  1845. 100000))
  1846. wpa_supplicant_req_scan(wpa_s, interface_count,
  1847. 100000);
  1848. interface_count++;
  1849. } else
  1850. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  1851. return 0;
  1852. }
  1853. static int wpa_supplicant_daemon(const char *pid_file)
  1854. {
  1855. wpa_printf(MSG_DEBUG, "Daemonize..");
  1856. return os_daemonize(pid_file);
  1857. }
  1858. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  1859. {
  1860. struct wpa_supplicant *wpa_s;
  1861. wpa_s = os_zalloc(sizeof(*wpa_s));
  1862. if (wpa_s == NULL)
  1863. return NULL;
  1864. wpa_s->scan_req = 1;
  1865. wpa_s->scan_interval = 5;
  1866. wpa_s->new_connection = 1;
  1867. wpa_s->parent = wpa_s;
  1868. wpa_s->sched_scanning = 0;
  1869. return wpa_s;
  1870. }
  1871. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  1872. struct wpa_interface *iface)
  1873. {
  1874. const char *ifname, *driver;
  1875. struct wpa_driver_capa capa;
  1876. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  1877. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  1878. iface->confname ? iface->confname : "N/A",
  1879. iface->driver ? iface->driver : "default",
  1880. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  1881. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  1882. if (iface->confname) {
  1883. #ifdef CONFIG_BACKEND_FILE
  1884. wpa_s->confname = os_rel2abs_path(iface->confname);
  1885. if (wpa_s->confname == NULL) {
  1886. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  1887. "for configuration file '%s'.",
  1888. iface->confname);
  1889. return -1;
  1890. }
  1891. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  1892. iface->confname, wpa_s->confname);
  1893. #else /* CONFIG_BACKEND_FILE */
  1894. wpa_s->confname = os_strdup(iface->confname);
  1895. #endif /* CONFIG_BACKEND_FILE */
  1896. wpa_s->conf = wpa_config_read(wpa_s->confname);
  1897. if (wpa_s->conf == NULL) {
  1898. wpa_printf(MSG_ERROR, "Failed to read or parse "
  1899. "configuration '%s'.", wpa_s->confname);
  1900. return -1;
  1901. }
  1902. /*
  1903. * Override ctrl_interface and driver_param if set on command
  1904. * line.
  1905. */
  1906. if (iface->ctrl_interface) {
  1907. os_free(wpa_s->conf->ctrl_interface);
  1908. wpa_s->conf->ctrl_interface =
  1909. os_strdup(iface->ctrl_interface);
  1910. }
  1911. if (iface->driver_param) {
  1912. os_free(wpa_s->conf->driver_param);
  1913. wpa_s->conf->driver_param =
  1914. os_strdup(iface->driver_param);
  1915. }
  1916. } else
  1917. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  1918. iface->driver_param);
  1919. if (wpa_s->conf == NULL) {
  1920. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  1921. return -1;
  1922. }
  1923. if (iface->ifname == NULL) {
  1924. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  1925. return -1;
  1926. }
  1927. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  1928. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  1929. iface->ifname);
  1930. return -1;
  1931. }
  1932. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  1933. if (iface->bridge_ifname) {
  1934. if (os_strlen(iface->bridge_ifname) >=
  1935. sizeof(wpa_s->bridge_ifname)) {
  1936. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  1937. "name '%s'.", iface->bridge_ifname);
  1938. return -1;
  1939. }
  1940. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  1941. sizeof(wpa_s->bridge_ifname));
  1942. }
  1943. /* RSNA Supplicant Key Management - INITIALIZE */
  1944. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1945. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1946. /* Initialize driver interface and register driver event handler before
  1947. * L2 receive handler so that association events are processed before
  1948. * EAPOL-Key packets if both become available for the same select()
  1949. * call. */
  1950. driver = iface->driver;
  1951. next_driver:
  1952. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  1953. return -1;
  1954. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  1955. if (wpa_s->drv_priv == NULL) {
  1956. const char *pos;
  1957. pos = driver ? os_strchr(driver, ',') : NULL;
  1958. if (pos) {
  1959. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  1960. "driver interface - try next driver wrapper");
  1961. driver = pos + 1;
  1962. goto next_driver;
  1963. }
  1964. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  1965. "interface");
  1966. return -1;
  1967. }
  1968. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  1969. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  1970. "driver_param '%s'", wpa_s->conf->driver_param);
  1971. return -1;
  1972. }
  1973. ifname = wpa_drv_get_ifname(wpa_s);
  1974. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  1975. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  1976. "interface name with '%s'", ifname);
  1977. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  1978. }
  1979. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  1980. return -1;
  1981. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  1982. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  1983. NULL);
  1984. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  1985. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  1986. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  1987. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  1988. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  1989. "dot11RSNAConfigPMKLifetime");
  1990. return -1;
  1991. }
  1992. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  1993. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  1994. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  1995. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  1996. "dot11RSNAConfigPMKReauthThreshold");
  1997. return -1;
  1998. }
  1999. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  2000. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  2001. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  2002. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2003. "dot11RSNAConfigSATimeout");
  2004. return -1;
  2005. }
  2006. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  2007. wpa_s->drv_flags = capa.flags;
  2008. if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  2009. if (ieee80211_sta_init(wpa_s))
  2010. return -1;
  2011. }
  2012. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  2013. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  2014. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  2015. wpa_s->max_match_sets = capa.max_match_sets;
  2016. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  2017. wpa_s->max_stations = capa.max_stations;
  2018. }
  2019. if (wpa_s->max_remain_on_chan == 0)
  2020. wpa_s->max_remain_on_chan = 1000;
  2021. if (wpa_supplicant_driver_init(wpa_s) < 0)
  2022. return -1;
  2023. #ifdef CONFIG_TDLS
  2024. if (wpa_tdls_init(wpa_s->wpa))
  2025. return -1;
  2026. #endif /* CONFIG_TDLS */
  2027. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  2028. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  2029. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  2030. return -1;
  2031. }
  2032. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  2033. if (wpas_wps_init(wpa_s))
  2034. return -1;
  2035. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  2036. return -1;
  2037. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2038. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  2039. if (wpa_s->ctrl_iface == NULL) {
  2040. wpa_printf(MSG_ERROR,
  2041. "Failed to initialize control interface '%s'.\n"
  2042. "You may have another wpa_supplicant process "
  2043. "already running or the file was\n"
  2044. "left by an unclean termination of wpa_supplicant "
  2045. "in which case you will need\n"
  2046. "to manually remove this file before starting "
  2047. "wpa_supplicant again.\n",
  2048. wpa_s->conf->ctrl_interface);
  2049. return -1;
  2050. }
  2051. wpa_s->gas = gas_query_init(wpa_s);
  2052. if (wpa_s->gas == NULL) {
  2053. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  2054. return -1;
  2055. }
  2056. #ifdef CONFIG_P2P
  2057. if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  2058. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  2059. return -1;
  2060. }
  2061. #endif /* CONFIG_P2P */
  2062. if (wpa_bss_init(wpa_s) < 0)
  2063. return -1;
  2064. return 0;
  2065. }
  2066. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  2067. int notify)
  2068. {
  2069. if (wpa_s->drv_priv) {
  2070. wpa_supplicant_deauthenticate(wpa_s,
  2071. WLAN_REASON_DEAUTH_LEAVING);
  2072. wpa_drv_set_countermeasures(wpa_s, 0);
  2073. wpa_clear_keys(wpa_s, NULL);
  2074. }
  2075. wpa_supplicant_cleanup(wpa_s);
  2076. if (notify)
  2077. wpas_notify_iface_removed(wpa_s);
  2078. if (wpa_s->drv_priv)
  2079. wpa_drv_deinit(wpa_s);
  2080. }
  2081. /**
  2082. * wpa_supplicant_add_iface - Add a new network interface
  2083. * @global: Pointer to global data from wpa_supplicant_init()
  2084. * @iface: Interface configuration options
  2085. * Returns: Pointer to the created interface or %NULL on failure
  2086. *
  2087. * This function is used to add new network interfaces for %wpa_supplicant.
  2088. * This can be called before wpa_supplicant_run() to add interfaces before the
  2089. * main event loop has been started. In addition, new interfaces can be added
  2090. * dynamically while %wpa_supplicant is already running. This could happen,
  2091. * e.g., when a hotplug network adapter is inserted.
  2092. */
  2093. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  2094. struct wpa_interface *iface)
  2095. {
  2096. struct wpa_supplicant *wpa_s;
  2097. struct wpa_interface t_iface;
  2098. struct wpa_ssid *ssid;
  2099. if (global == NULL || iface == NULL)
  2100. return NULL;
  2101. wpa_s = wpa_supplicant_alloc();
  2102. if (wpa_s == NULL)
  2103. return NULL;
  2104. wpa_s->global = global;
  2105. t_iface = *iface;
  2106. if (global->params.override_driver) {
  2107. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  2108. "('%s' -> '%s')",
  2109. iface->driver, global->params.override_driver);
  2110. t_iface.driver = global->params.override_driver;
  2111. }
  2112. if (global->params.override_ctrl_interface) {
  2113. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  2114. "ctrl_interface ('%s' -> '%s')",
  2115. iface->ctrl_interface,
  2116. global->params.override_ctrl_interface);
  2117. t_iface.ctrl_interface =
  2118. global->params.override_ctrl_interface;
  2119. }
  2120. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  2121. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  2122. iface->ifname);
  2123. wpa_supplicant_deinit_iface(wpa_s, 0);
  2124. os_free(wpa_s);
  2125. return NULL;
  2126. }
  2127. /* Notify the control interfaces about new iface */
  2128. if (wpas_notify_iface_added(wpa_s)) {
  2129. wpa_supplicant_deinit_iface(wpa_s, 1);
  2130. os_free(wpa_s);
  2131. return NULL;
  2132. }
  2133. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2134. wpas_notify_network_added(wpa_s, ssid);
  2135. wpa_s->next = global->ifaces;
  2136. global->ifaces = wpa_s;
  2137. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  2138. return wpa_s;
  2139. }
  2140. /**
  2141. * wpa_supplicant_remove_iface - Remove a network interface
  2142. * @global: Pointer to global data from wpa_supplicant_init()
  2143. * @wpa_s: Pointer to the network interface to be removed
  2144. * Returns: 0 if interface was removed, -1 if interface was not found
  2145. *
  2146. * This function can be used to dynamically remove network interfaces from
  2147. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  2148. * addition, this function is used to remove all remaining interfaces when
  2149. * %wpa_supplicant is terminated.
  2150. */
  2151. int wpa_supplicant_remove_iface(struct wpa_global *global,
  2152. struct wpa_supplicant *wpa_s)
  2153. {
  2154. struct wpa_supplicant *prev;
  2155. /* Remove interface from the global list of interfaces */
  2156. prev = global->ifaces;
  2157. if (prev == wpa_s) {
  2158. global->ifaces = wpa_s->next;
  2159. } else {
  2160. while (prev && prev->next != wpa_s)
  2161. prev = prev->next;
  2162. if (prev == NULL)
  2163. return -1;
  2164. prev->next = wpa_s->next;
  2165. }
  2166. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  2167. if (global->p2p_group_formation == wpa_s)
  2168. global->p2p_group_formation = NULL;
  2169. wpa_supplicant_deinit_iface(wpa_s, 1);
  2170. os_free(wpa_s);
  2171. return 0;
  2172. }
  2173. /**
  2174. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  2175. * @wpa_s: Pointer to the network interface
  2176. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  2177. */
  2178. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  2179. {
  2180. const char *eapol_method;
  2181. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  2182. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2183. return "NO-EAP";
  2184. }
  2185. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  2186. if (eapol_method == NULL)
  2187. return "UNKNOWN-EAP";
  2188. return eapol_method;
  2189. }
  2190. /**
  2191. * wpa_supplicant_get_iface - Get a new network interface
  2192. * @global: Pointer to global data from wpa_supplicant_init()
  2193. * @ifname: Interface name
  2194. * Returns: Pointer to the interface or %NULL if not found
  2195. */
  2196. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  2197. const char *ifname)
  2198. {
  2199. struct wpa_supplicant *wpa_s;
  2200. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2201. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2202. return wpa_s;
  2203. }
  2204. return NULL;
  2205. }
  2206. #ifndef CONFIG_NO_WPA_MSG
  2207. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  2208. {
  2209. struct wpa_supplicant *wpa_s = ctx;
  2210. if (wpa_s == NULL)
  2211. return NULL;
  2212. return wpa_s->ifname;
  2213. }
  2214. #endif /* CONFIG_NO_WPA_MSG */
  2215. /**
  2216. * wpa_supplicant_init - Initialize %wpa_supplicant
  2217. * @params: Parameters for %wpa_supplicant
  2218. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  2219. *
  2220. * This function is used to initialize %wpa_supplicant. After successful
  2221. * initialization, the returned data pointer can be used to add and remove
  2222. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  2223. */
  2224. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  2225. {
  2226. struct wpa_global *global;
  2227. int ret, i;
  2228. if (params == NULL)
  2229. return NULL;
  2230. #ifndef CONFIG_NO_WPA_MSG
  2231. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  2232. #endif /* CONFIG_NO_WPA_MSG */
  2233. wpa_debug_open_file(params->wpa_debug_file_path);
  2234. if (params->wpa_debug_syslog)
  2235. wpa_debug_open_syslog();
  2236. ret = eap_register_methods();
  2237. if (ret) {
  2238. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  2239. if (ret == -2)
  2240. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  2241. "the same EAP type.");
  2242. return NULL;
  2243. }
  2244. global = os_zalloc(sizeof(*global));
  2245. if (global == NULL)
  2246. return NULL;
  2247. dl_list_init(&global->p2p_srv_bonjour);
  2248. dl_list_init(&global->p2p_srv_upnp);
  2249. global->params.daemonize = params->daemonize;
  2250. global->params.wait_for_monitor = params->wait_for_monitor;
  2251. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  2252. if (params->pid_file)
  2253. global->params.pid_file = os_strdup(params->pid_file);
  2254. if (params->ctrl_interface)
  2255. global->params.ctrl_interface =
  2256. os_strdup(params->ctrl_interface);
  2257. if (params->override_driver)
  2258. global->params.override_driver =
  2259. os_strdup(params->override_driver);
  2260. if (params->override_ctrl_interface)
  2261. global->params.override_ctrl_interface =
  2262. os_strdup(params->override_ctrl_interface);
  2263. wpa_debug_level = global->params.wpa_debug_level =
  2264. params->wpa_debug_level;
  2265. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  2266. params->wpa_debug_show_keys;
  2267. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  2268. params->wpa_debug_timestamp;
  2269. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  2270. if (eloop_init()) {
  2271. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  2272. wpa_supplicant_deinit(global);
  2273. return NULL;
  2274. }
  2275. random_init(params->entropy_file);
  2276. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  2277. if (global->ctrl_iface == NULL) {
  2278. wpa_supplicant_deinit(global);
  2279. return NULL;
  2280. }
  2281. if (wpas_notify_supplicant_initialized(global)) {
  2282. wpa_supplicant_deinit(global);
  2283. return NULL;
  2284. }
  2285. for (i = 0; wpa_drivers[i]; i++)
  2286. global->drv_count++;
  2287. if (global->drv_count == 0) {
  2288. wpa_printf(MSG_ERROR, "No drivers enabled");
  2289. wpa_supplicant_deinit(global);
  2290. return NULL;
  2291. }
  2292. global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
  2293. if (global->drv_priv == NULL) {
  2294. wpa_supplicant_deinit(global);
  2295. return NULL;
  2296. }
  2297. for (i = 0; wpa_drivers[i]; i++) {
  2298. if (!wpa_drivers[i]->global_init)
  2299. continue;
  2300. global->drv_priv[i] = wpa_drivers[i]->global_init();
  2301. if (global->drv_priv[i] == NULL) {
  2302. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  2303. "'%s'", wpa_drivers[i]->name);
  2304. wpa_supplicant_deinit(global);
  2305. return NULL;
  2306. }
  2307. }
  2308. return global;
  2309. }
  2310. /**
  2311. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  2312. * @global: Pointer to global data from wpa_supplicant_init()
  2313. * Returns: 0 after successful event loop run, -1 on failure
  2314. *
  2315. * This function starts the main event loop and continues running as long as
  2316. * there are any remaining events. In most cases, this function is running as
  2317. * long as the %wpa_supplicant process in still in use.
  2318. */
  2319. int wpa_supplicant_run(struct wpa_global *global)
  2320. {
  2321. struct wpa_supplicant *wpa_s;
  2322. if (global->params.daemonize &&
  2323. wpa_supplicant_daemon(global->params.pid_file))
  2324. return -1;
  2325. if (global->params.wait_for_monitor) {
  2326. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  2327. if (wpa_s->ctrl_iface)
  2328. wpa_supplicant_ctrl_iface_wait(
  2329. wpa_s->ctrl_iface);
  2330. }
  2331. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  2332. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  2333. eloop_run();
  2334. return 0;
  2335. }
  2336. /**
  2337. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  2338. * @global: Pointer to global data from wpa_supplicant_init()
  2339. *
  2340. * This function is called to deinitialize %wpa_supplicant and to free all
  2341. * allocated resources. Remaining network interfaces will also be removed.
  2342. */
  2343. void wpa_supplicant_deinit(struct wpa_global *global)
  2344. {
  2345. int i;
  2346. if (global == NULL)
  2347. return;
  2348. #ifdef CONFIG_P2P
  2349. wpas_p2p_deinit_global(global);
  2350. #endif /* CONFIG_P2P */
  2351. while (global->ifaces)
  2352. wpa_supplicant_remove_iface(global, global->ifaces);
  2353. if (global->ctrl_iface)
  2354. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  2355. wpas_notify_supplicant_deinitialized(global);
  2356. eap_peer_unregister_methods();
  2357. #ifdef CONFIG_AP
  2358. eap_server_unregister_methods();
  2359. #endif /* CONFIG_AP */
  2360. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  2361. if (!global->drv_priv[i])
  2362. continue;
  2363. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  2364. }
  2365. os_free(global->drv_priv);
  2366. random_deinit();
  2367. eloop_destroy();
  2368. if (global->params.pid_file) {
  2369. os_daemonize_terminate(global->params.pid_file);
  2370. os_free(global->params.pid_file);
  2371. }
  2372. os_free(global->params.ctrl_interface);
  2373. os_free(global->params.override_driver);
  2374. os_free(global->params.override_ctrl_interface);
  2375. os_free(global);
  2376. wpa_debug_close_syslog();
  2377. wpa_debug_close_file();
  2378. }
  2379. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  2380. {
  2381. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  2382. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2383. char country[3];
  2384. country[0] = wpa_s->conf->country[0];
  2385. country[1] = wpa_s->conf->country[1];
  2386. country[2] = '\0';
  2387. if (wpa_drv_set_country(wpa_s, country) < 0) {
  2388. wpa_printf(MSG_ERROR, "Failed to set country code "
  2389. "'%s'", country);
  2390. }
  2391. }
  2392. #ifdef CONFIG_WPS
  2393. wpas_wps_update_config(wpa_s);
  2394. #endif /* CONFIG_WPS */
  2395. #ifdef CONFIG_P2P
  2396. wpas_p2p_update_config(wpa_s);
  2397. #endif /* CONFIG_P2P */
  2398. wpa_s->conf->changed_parameters = 0;
  2399. }
  2400. void ieee80211_sta_free_hw_features(struct hostapd_hw_modes *hw_features,
  2401. size_t num_hw_features)
  2402. {
  2403. size_t i;
  2404. if (hw_features == NULL)
  2405. return;
  2406. for (i = 0; i < num_hw_features; i++) {
  2407. os_free(hw_features[i].channels);
  2408. os_free(hw_features[i].rates);
  2409. }
  2410. os_free(hw_features);
  2411. }
  2412. static void add_freq(int *freqs, int *num_freqs, int freq)
  2413. {
  2414. int i;
  2415. for (i = 0; i < *num_freqs; i++) {
  2416. if (freqs[i] == freq)
  2417. return;
  2418. }
  2419. freqs[*num_freqs] = freq;
  2420. (*num_freqs)++;
  2421. }
  2422. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  2423. {
  2424. struct wpa_bss *bss, *cbss;
  2425. const int max_freqs = 10;
  2426. int *freqs;
  2427. int num_freqs = 0;
  2428. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  2429. if (freqs == NULL)
  2430. return NULL;
  2431. cbss = wpa_s->current_bss;
  2432. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2433. if (bss == cbss)
  2434. continue;
  2435. if (bss->ssid_len == cbss->ssid_len &&
  2436. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  2437. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  2438. add_freq(freqs, &num_freqs, bss->freq);
  2439. if (num_freqs == max_freqs)
  2440. break;
  2441. }
  2442. }
  2443. if (num_freqs == 0) {
  2444. os_free(freqs);
  2445. freqs = NULL;
  2446. }
  2447. return freqs;
  2448. }
  2449. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2450. {
  2451. int timeout;
  2452. int count;
  2453. int *freqs = NULL;
  2454. /*
  2455. * Add the failed BSSID into the blacklist and speed up next scan
  2456. * attempt if there could be other APs that could accept association.
  2457. * The current blacklist count indicates how many times we have tried
  2458. * connecting to this AP and multiple attempts mean that other APs are
  2459. * either not available or has already been tried, so that we can start
  2460. * increasing the delay here to avoid constant scanning.
  2461. */
  2462. count = wpa_blacklist_add(wpa_s, bssid);
  2463. if (count == 1 && wpa_s->current_bss) {
  2464. /*
  2465. * This BSS was not in the blacklist before. If there is
  2466. * another BSS available for the same ESS, we should try that
  2467. * next. Otherwise, we may as well try this one once more
  2468. * before allowing other, likely worse, ESSes to be considered.
  2469. */
  2470. freqs = get_bss_freqs_in_ess(wpa_s);
  2471. if (freqs) {
  2472. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  2473. "has been seen; try it next");
  2474. wpa_blacklist_add(wpa_s, bssid);
  2475. /*
  2476. * On the next scan, go through only the known channels
  2477. * used in this ESS based on previous scans to speed up
  2478. * common load balancing use case.
  2479. */
  2480. os_free(wpa_s->next_scan_freqs);
  2481. wpa_s->next_scan_freqs = freqs;
  2482. }
  2483. }
  2484. switch (count) {
  2485. case 1:
  2486. timeout = 100;
  2487. break;
  2488. case 2:
  2489. timeout = 500;
  2490. break;
  2491. case 3:
  2492. timeout = 1000;
  2493. break;
  2494. default:
  2495. timeout = 5000;
  2496. }
  2497. /*
  2498. * TODO: if more than one possible AP is available in scan results,
  2499. * could try the other ones before requesting a new scan.
  2500. */
  2501. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  2502. 1000 * (timeout % 1000));
  2503. }
  2504. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  2505. {
  2506. return wpa_s->conf->ap_scan == 2 ||
  2507. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  2508. }