wpa_supplicant.c 81 KB

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