wpa_supplicant.c 71 KB

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