wpa_supplicant.c 73 KB

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