wpa_supplicant.c 71 KB

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