wpa_supplicant.c 73 KB

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