wpa_supplicant.c 78 KB

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