events.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eapol_supp/eapol_supp_sm.h"
  11. #include "rsn_supp/wpa.h"
  12. #include "eloop.h"
  13. #include "config.h"
  14. #include "l2_packet/l2_packet.h"
  15. #include "wpa_supplicant_i.h"
  16. #include "driver_i.h"
  17. #include "pcsc_funcs.h"
  18. #include "rsn_supp/preauth.h"
  19. #include "rsn_supp/pmksa_cache.h"
  20. #include "common/wpa_ctrl.h"
  21. #include "eap_peer/eap.h"
  22. #include "ap/hostapd.h"
  23. #include "p2p/p2p.h"
  24. #include "notify.h"
  25. #include "common/ieee802_11_defs.h"
  26. #include "common/ieee802_11_common.h"
  27. #include "crypto/random.h"
  28. #include "blacklist.h"
  29. #include "wpas_glue.h"
  30. #include "wps_supplicant.h"
  31. #include "ibss_rsn.h"
  32. #include "sme.h"
  33. #include "gas_query.h"
  34. #include "p2p_supplicant.h"
  35. #include "bgscan.h"
  36. #include "ap.h"
  37. #include "bss.h"
  38. #include "scan.h"
  39. #include "offchannel.h"
  40. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  41. {
  42. struct wpa_ssid *ssid, *old_ssid;
  43. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
  44. return 0;
  45. wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
  46. "information");
  47. ssid = wpa_supplicant_get_ssid(wpa_s);
  48. if (ssid == NULL) {
  49. wpa_msg(wpa_s, MSG_INFO,
  50. "No network configuration found for the current AP");
  51. return -1;
  52. }
  53. if (ssid->disabled) {
  54. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
  55. return -1;
  56. }
  57. wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
  58. "current AP");
  59. if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  60. u8 wpa_ie[80];
  61. size_t wpa_ie_len = sizeof(wpa_ie);
  62. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  63. wpa_ie, &wpa_ie_len);
  64. } else {
  65. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  66. }
  67. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  68. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  69. old_ssid = wpa_s->current_ssid;
  70. wpa_s->current_ssid = ssid;
  71. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  72. wpa_supplicant_initiate_eapol(wpa_s);
  73. if (old_ssid != wpa_s->current_ssid)
  74. wpas_notify_network_changed(wpa_s);
  75. return 0;
  76. }
  77. void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
  78. {
  79. struct wpa_supplicant *wpa_s = eloop_ctx;
  80. if (wpa_s->countermeasures) {
  81. wpa_s->countermeasures = 0;
  82. wpa_drv_set_countermeasures(wpa_s, 0);
  83. wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
  84. wpa_supplicant_req_scan(wpa_s, 0, 0);
  85. }
  86. }
  87. void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
  88. {
  89. int bssid_changed;
  90. #ifdef CONFIG_IBSS_RSN
  91. ibss_rsn_deinit(wpa_s->ibss_rsn);
  92. wpa_s->ibss_rsn = NULL;
  93. #endif /* CONFIG_IBSS_RSN */
  94. #ifdef CONFIG_AP
  95. wpa_supplicant_ap_deinit(wpa_s);
  96. #endif /* CONFIG_AP */
  97. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  98. return;
  99. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  100. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  101. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  102. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  103. #ifdef CONFIG_P2P
  104. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  105. #endif /* CONFIG_P2P */
  106. wpa_s->current_bss = NULL;
  107. wpa_s->assoc_freq = 0;
  108. #ifdef CONFIG_IEEE80211R
  109. #ifdef CONFIG_SME
  110. if (wpa_s->sme.ft_ies)
  111. sme_update_ft_ies(wpa_s, NULL, NULL, 0);
  112. #endif /* CONFIG_SME */
  113. #endif /* CONFIG_IEEE80211R */
  114. if (bssid_changed)
  115. wpas_notify_bssid_changed(wpa_s);
  116. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  117. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  118. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  119. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  120. wpa_s->ap_ies_from_associnfo = 0;
  121. }
  122. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  123. {
  124. struct wpa_ie_data ie;
  125. int pmksa_set = -1;
  126. size_t i;
  127. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  128. ie.pmkid == NULL)
  129. return;
  130. for (i = 0; i < ie.num_pmkid; i++) {
  131. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  132. ie.pmkid + i * PMKID_LEN,
  133. NULL, NULL, 0);
  134. if (pmksa_set == 0) {
  135. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  136. break;
  137. }
  138. }
  139. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
  140. "PMKSA cache", pmksa_set == 0 ? "" : "not ");
  141. }
  142. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  143. union wpa_event_data *data)
  144. {
  145. if (data == NULL) {
  146. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
  147. "event");
  148. return;
  149. }
  150. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  151. " index=%d preauth=%d",
  152. MAC2STR(data->pmkid_candidate.bssid),
  153. data->pmkid_candidate.index,
  154. data->pmkid_candidate.preauth);
  155. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  156. data->pmkid_candidate.index,
  157. data->pmkid_candidate.preauth);
  158. }
  159. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  160. {
  161. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  162. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  163. return 0;
  164. #ifdef IEEE8021X_EAPOL
  165. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  166. wpa_s->current_ssid &&
  167. !(wpa_s->current_ssid->eapol_flags &
  168. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  169. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  170. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  171. * plaintext or static WEP keys). */
  172. return 0;
  173. }
  174. #endif /* IEEE8021X_EAPOL */
  175. return 1;
  176. }
  177. /**
  178. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  179. * @wpa_s: pointer to wpa_supplicant data
  180. * @ssid: Configuration data for the network
  181. * Returns: 0 on success, -1 on failure
  182. *
  183. * This function is called when starting authentication with a network that is
  184. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  185. */
  186. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  187. struct wpa_ssid *ssid)
  188. {
  189. #ifdef IEEE8021X_EAPOL
  190. #ifdef PCSC_FUNCS
  191. int aka = 0, sim = 0, type;
  192. if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
  193. return 0;
  194. if (ssid->eap.eap_methods == NULL) {
  195. sim = 1;
  196. aka = 1;
  197. } else {
  198. struct eap_method_type *eap = ssid->eap.eap_methods;
  199. while (eap->vendor != EAP_VENDOR_IETF ||
  200. eap->method != EAP_TYPE_NONE) {
  201. if (eap->vendor == EAP_VENDOR_IETF) {
  202. if (eap->method == EAP_TYPE_SIM)
  203. sim = 1;
  204. else if (eap->method == EAP_TYPE_AKA)
  205. aka = 1;
  206. }
  207. eap++;
  208. }
  209. }
  210. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  211. sim = 0;
  212. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
  213. aka = 0;
  214. if (!sim && !aka) {
  215. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
  216. "use SIM, but neither EAP-SIM nor EAP-AKA are "
  217. "enabled");
  218. return 0;
  219. }
  220. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
  221. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  222. if (sim && aka)
  223. type = SCARD_TRY_BOTH;
  224. else if (aka)
  225. type = SCARD_USIM_ONLY;
  226. else
  227. type = SCARD_GSM_SIM_ONLY;
  228. wpa_s->scard = scard_init(type);
  229. if (wpa_s->scard == NULL) {
  230. wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
  231. "(pcsc-lite)");
  232. return -1;
  233. }
  234. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  235. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  236. #endif /* PCSC_FUNCS */
  237. #endif /* IEEE8021X_EAPOL */
  238. return 0;
  239. }
  240. #ifndef CONFIG_NO_SCAN_PROCESSING
  241. static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
  242. struct wpa_ssid *ssid)
  243. {
  244. int i, privacy = 0;
  245. if (ssid->mixed_cell)
  246. return 1;
  247. #ifdef CONFIG_WPS
  248. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  249. return 1;
  250. #endif /* CONFIG_WPS */
  251. for (i = 0; i < NUM_WEP_KEYS; i++) {
  252. if (ssid->wep_key_len[i]) {
  253. privacy = 1;
  254. break;
  255. }
  256. }
  257. #ifdef IEEE8021X_EAPOL
  258. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  259. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  260. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  261. privacy = 1;
  262. #endif /* IEEE8021X_EAPOL */
  263. if (wpa_key_mgmt_wpa(ssid->key_mgmt))
  264. privacy = 1;
  265. if (bss->caps & IEEE80211_CAP_PRIVACY)
  266. return privacy;
  267. return !privacy;
  268. }
  269. static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
  270. struct wpa_ssid *ssid,
  271. struct wpa_scan_res *bss)
  272. {
  273. struct wpa_ie_data ie;
  274. int proto_match = 0;
  275. const u8 *rsn_ie, *wpa_ie;
  276. int ret;
  277. int wep_ok;
  278. ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
  279. if (ret >= 0)
  280. return ret;
  281. /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
  282. wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
  283. (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  284. ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
  285. (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
  286. rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  287. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  288. proto_match++;
  289. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  290. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
  291. "failed");
  292. break;
  293. }
  294. if (wep_ok &&
  295. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  296. {
  297. wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
  298. "in RSN IE");
  299. return 1;
  300. }
  301. if (!(ie.proto & ssid->proto)) {
  302. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
  303. "mismatch");
  304. break;
  305. }
  306. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  307. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
  308. "cipher mismatch");
  309. break;
  310. }
  311. if (!(ie.group_cipher & ssid->group_cipher)) {
  312. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
  313. "cipher mismatch");
  314. break;
  315. }
  316. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  317. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
  318. "mismatch");
  319. break;
  320. }
  321. #ifdef CONFIG_IEEE80211W
  322. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  323. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  324. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
  325. "frame protection");
  326. break;
  327. }
  328. #endif /* CONFIG_IEEE80211W */
  329. wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
  330. return 1;
  331. }
  332. wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  333. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  334. proto_match++;
  335. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  336. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
  337. "failed");
  338. break;
  339. }
  340. if (wep_ok &&
  341. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  342. {
  343. wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
  344. "in WPA IE");
  345. return 1;
  346. }
  347. if (!(ie.proto & ssid->proto)) {
  348. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
  349. "mismatch");
  350. break;
  351. }
  352. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  353. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
  354. "cipher mismatch");
  355. break;
  356. }
  357. if (!(ie.group_cipher & ssid->group_cipher)) {
  358. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
  359. "cipher mismatch");
  360. break;
  361. }
  362. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  363. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
  364. "mismatch");
  365. break;
  366. }
  367. wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
  368. return 1;
  369. }
  370. if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
  371. wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
  372. wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
  373. return 0;
  374. }
  375. if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  376. wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
  377. return 1;
  378. }
  379. wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
  380. "WPA/WPA2");
  381. return 0;
  382. }
  383. static int freq_allowed(int *freqs, int freq)
  384. {
  385. int i;
  386. if (freqs == NULL)
  387. return 1;
  388. for (i = 0; freqs[i]; i++)
  389. if (freqs[i] == freq)
  390. return 1;
  391. return 0;
  392. }
  393. static int ht_supported(const struct hostapd_hw_modes *mode)
  394. {
  395. if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
  396. /*
  397. * The driver did not indicate whether it supports HT. Assume
  398. * it does to avoid connection issues.
  399. */
  400. return 1;
  401. }
  402. /*
  403. * IEEE Std 802.11n-2009 20.1.1:
  404. * An HT non-AP STA shall support all EQM rates for one spatial stream.
  405. */
  406. return mode->mcs_set[0] == 0xff;
  407. }
  408. static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_scan_res *bss)
  409. {
  410. const struct hostapd_hw_modes *mode = NULL, *modes;
  411. const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
  412. const u8 *rate_ie;
  413. int i, j, k;
  414. if (bss->freq == 0)
  415. return 1; /* Cannot do matching without knowing band */
  416. modes = wpa_s->hw.modes;
  417. if (modes == NULL) {
  418. /*
  419. * The driver does not provide any additional information
  420. * about the utilized hardware, so allow the connection attempt
  421. * to continue.
  422. */
  423. return 1;
  424. }
  425. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  426. for (j = 0; j < modes[i].num_channels; j++) {
  427. int freq = modes[i].channels[j].freq;
  428. if (freq == bss->freq) {
  429. if (mode &&
  430. mode->mode == HOSTAPD_MODE_IEEE80211G)
  431. break; /* do not allow 802.11b replace
  432. * 802.11g */
  433. mode = &modes[i];
  434. break;
  435. }
  436. }
  437. }
  438. if (mode == NULL)
  439. return 0;
  440. for (i = 0; i < (int) sizeof(scan_ie); i++) {
  441. rate_ie = wpa_scan_get_ie(bss, scan_ie[i]);
  442. if (rate_ie == NULL)
  443. continue;
  444. for (j = 2; j < rate_ie[1] + 2; j++) {
  445. int flagged = !!(rate_ie[j] & 0x80);
  446. int r = (rate_ie[j] & 0x7f) * 5;
  447. /*
  448. * IEEE Std 802.11n-2009 7.3.2.2:
  449. * The new BSS Membership selector value is encoded
  450. * like a legacy basic rate, but it is not a rate and
  451. * only indicates if the BSS members are required to
  452. * support the mandatory features of Clause 20 [HT PHY]
  453. * in order to join the BSS.
  454. */
  455. if (flagged && ((rate_ie[j] & 0x7f) ==
  456. BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
  457. if (!ht_supported(mode)) {
  458. wpa_dbg(wpa_s, MSG_DEBUG,
  459. " hardware does not support "
  460. "HT PHY");
  461. return 0;
  462. }
  463. continue;
  464. }
  465. if (!flagged)
  466. continue;
  467. /* check for legacy basic rates */
  468. for (k = 0; k < mode->num_rates; k++) {
  469. if (mode->rates[k] == r)
  470. break;
  471. }
  472. if (k == mode->num_rates) {
  473. /*
  474. * IEEE Std 802.11-2007 7.3.2.2 demands that in
  475. * order to join a BSS all required rates
  476. * have to be supported by the hardware.
  477. */
  478. wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
  479. "not support required rate %d.%d Mbps",
  480. r / 10, r % 10);
  481. return 0;
  482. }
  483. }
  484. }
  485. return 1;
  486. }
  487. static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
  488. int i, struct wpa_scan_res *bss,
  489. struct wpa_ssid *group)
  490. {
  491. const u8 *ssid_;
  492. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  493. int wpa;
  494. struct wpa_blacklist *e;
  495. const u8 *ie;
  496. struct wpa_ssid *ssid;
  497. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  498. ssid_ = ie ? ie + 2 : (u8 *) "";
  499. ssid_len = ie ? ie[1] : 0;
  500. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  501. wpa_ie_len = ie ? ie[1] : 0;
  502. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  503. rsn_ie_len = ie ? ie[1] : 0;
  504. wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  505. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
  506. i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
  507. wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
  508. wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
  509. e = wpa_blacklist_get(wpa_s, bss->bssid);
  510. if (e) {
  511. int limit = 1;
  512. if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
  513. /*
  514. * When only a single network is enabled, we can
  515. * trigger blacklisting on the first failure. This
  516. * should not be done with multiple enabled networks to
  517. * avoid getting forced to move into a worse ESS on
  518. * single error if there are no other BSSes of the
  519. * current ESS.
  520. */
  521. limit = 0;
  522. }
  523. if (e->count > limit) {
  524. wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
  525. "(count=%d limit=%d)", e->count, limit);
  526. return NULL;
  527. }
  528. }
  529. if (ssid_len == 0) {
  530. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
  531. return NULL;
  532. }
  533. wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
  534. for (ssid = group; ssid; ssid = ssid->pnext) {
  535. int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
  536. if (ssid->disabled) {
  537. wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
  538. continue;
  539. }
  540. #ifdef CONFIG_WPS
  541. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
  542. wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
  543. "(WPS)");
  544. continue;
  545. }
  546. if (wpa && ssid->ssid_len == 0 &&
  547. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  548. check_ssid = 0;
  549. if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  550. /* Only allow wildcard SSID match if an AP
  551. * advertises active WPS operation that matches
  552. * with our mode. */
  553. check_ssid = 1;
  554. if (ssid->ssid_len == 0 &&
  555. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  556. check_ssid = 0;
  557. }
  558. #endif /* CONFIG_WPS */
  559. if (ssid->bssid_set && ssid->ssid_len == 0 &&
  560. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
  561. check_ssid = 0;
  562. if (check_ssid &&
  563. (ssid_len != ssid->ssid_len ||
  564. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  565. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
  566. continue;
  567. }
  568. if (ssid->bssid_set &&
  569. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
  570. wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
  571. continue;
  572. }
  573. if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  574. continue;
  575. if (!wpa &&
  576. !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  577. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  578. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
  579. wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
  580. "not allowed");
  581. continue;
  582. }
  583. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  584. wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
  585. "mismatch");
  586. continue;
  587. }
  588. if (bss->caps & IEEE80211_CAP_IBSS) {
  589. wpa_dbg(wpa_s, MSG_DEBUG, " skip - IBSS (adhoc) "
  590. "network");
  591. continue;
  592. }
  593. if (!freq_allowed(ssid->freq_list, bss->freq)) {
  594. wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
  595. "allowed");
  596. continue;
  597. }
  598. if (!rate_match(wpa_s, bss)) {
  599. wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
  600. "not match");
  601. continue;
  602. }
  603. #ifdef CONFIG_P2P
  604. /*
  605. * TODO: skip the AP if its P2P IE has Group Formation
  606. * bit set in the P2P Group Capability Bitmap and we
  607. * are not in Group Formation with that device.
  608. */
  609. #endif /* CONFIG_P2P */
  610. /* Matching configuration found */
  611. return ssid;
  612. }
  613. /* No matching configuration found */
  614. return NULL;
  615. }
  616. static struct wpa_bss *
  617. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  618. struct wpa_scan_results *scan_res,
  619. struct wpa_ssid *group,
  620. struct wpa_ssid **selected_ssid)
  621. {
  622. size_t i;
  623. wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
  624. group->priority);
  625. for (i = 0; i < scan_res->num; i++) {
  626. struct wpa_scan_res *bss = scan_res->res[i];
  627. const u8 *ie, *ssid;
  628. u8 ssid_len;
  629. *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
  630. if (!*selected_ssid)
  631. continue;
  632. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  633. ssid = ie ? ie + 2 : (u8 *) "";
  634. ssid_len = ie ? ie[1] : 0;
  635. wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
  636. " ssid='%s'",
  637. MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  638. return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
  639. }
  640. return NULL;
  641. }
  642. static struct wpa_bss *
  643. wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  644. struct wpa_scan_results *scan_res,
  645. struct wpa_ssid **selected_ssid)
  646. {
  647. struct wpa_bss *selected = NULL;
  648. int prio;
  649. while (selected == NULL) {
  650. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  651. selected = wpa_supplicant_select_bss(
  652. wpa_s, scan_res, wpa_s->conf->pssid[prio],
  653. selected_ssid);
  654. if (selected)
  655. break;
  656. }
  657. if (selected == NULL && wpa_s->blacklist &&
  658. !wpa_s->countermeasures) {
  659. wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
  660. "blacklist and try again");
  661. wpa_blacklist_clear(wpa_s);
  662. wpa_s->blacklist_cleared++;
  663. } else if (selected == NULL)
  664. break;
  665. }
  666. return selected;
  667. }
  668. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  669. int timeout_sec, int timeout_usec)
  670. {
  671. if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
  672. /*
  673. * No networks are enabled; short-circuit request so
  674. * we don't wait timeout seconds before transitioning
  675. * to INACTIVE state.
  676. */
  677. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  678. return;
  679. }
  680. wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
  681. }
  682. int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  683. struct wpa_bss *selected,
  684. struct wpa_ssid *ssid)
  685. {
  686. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  687. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  688. "PBC session overlap");
  689. #ifdef CONFIG_P2P
  690. if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
  691. return -1;
  692. #endif /* CONFIG_P2P */
  693. #ifdef CONFIG_WPS
  694. wpas_wps_cancel(wpa_s);
  695. #endif /* CONFIG_WPS */
  696. return -1;
  697. }
  698. /*
  699. * Do not trigger new association unless the BSSID has changed or if
  700. * reassociation is requested. If we are in process of associating with
  701. * the selected BSSID, do not trigger new attempt.
  702. */
  703. if (wpa_s->reassociate ||
  704. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  705. ((wpa_s->wpa_state != WPA_ASSOCIATING &&
  706. wpa_s->wpa_state != WPA_AUTHENTICATING) ||
  707. os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
  708. 0))) {
  709. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  710. wpa_supplicant_req_new_scan(wpa_s, 10, 0);
  711. return 0;
  712. }
  713. wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
  714. "reassociate: %d selected: "MACSTR " bssid: " MACSTR
  715. " pending: " MACSTR " wpa_state: %s",
  716. wpa_s->reassociate, MAC2STR(selected->bssid),
  717. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  718. wpa_supplicant_state_txt(wpa_s->wpa_state));
  719. wpa_supplicant_associate(wpa_s, selected, ssid);
  720. } else {
  721. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
  722. "selected AP");
  723. }
  724. return 0;
  725. }
  726. static struct wpa_ssid *
  727. wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
  728. {
  729. int prio;
  730. struct wpa_ssid *ssid;
  731. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  732. for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
  733. {
  734. if (ssid->disabled)
  735. continue;
  736. if (ssid->mode == IEEE80211_MODE_IBSS ||
  737. ssid->mode == IEEE80211_MODE_AP)
  738. return ssid;
  739. }
  740. }
  741. return NULL;
  742. }
  743. /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
  744. * on BSS added and BSS changed events */
  745. static void wpa_supplicant_rsn_preauth_scan_results(
  746. struct wpa_supplicant *wpa_s)
  747. {
  748. struct wpa_bss *bss;
  749. if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
  750. return;
  751. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  752. const u8 *ssid, *rsn;
  753. ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  754. if (ssid == NULL)
  755. continue;
  756. rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  757. if (rsn == NULL)
  758. continue;
  759. rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
  760. }
  761. }
  762. static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
  763. struct wpa_bss *selected,
  764. struct wpa_ssid *ssid,
  765. struct wpa_scan_results *scan_res)
  766. {
  767. size_t i;
  768. struct wpa_scan_res *current_bss = NULL;
  769. int min_diff;
  770. if (wpa_s->reassociate)
  771. return 1; /* explicit request to reassociate */
  772. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  773. return 1; /* we are not associated; continue */
  774. if (wpa_s->current_ssid == NULL)
  775. return 1; /* unknown current SSID */
  776. if (wpa_s->current_ssid != ssid)
  777. return 1; /* different network block */
  778. if (wpas_driver_bss_selection(wpa_s))
  779. return 0; /* Driver-based roaming */
  780. for (i = 0; i < scan_res->num; i++) {
  781. struct wpa_scan_res *res = scan_res->res[i];
  782. const u8 *ie;
  783. if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  784. continue;
  785. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  786. if (ie == NULL)
  787. continue;
  788. if (ie[1] != wpa_s->current_ssid->ssid_len ||
  789. os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
  790. continue;
  791. current_bss = res;
  792. break;
  793. }
  794. if (!current_bss)
  795. return 1; /* current BSS not seen in scan results */
  796. wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
  797. wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
  798. MAC2STR(current_bss->bssid), current_bss->level);
  799. wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
  800. MAC2STR(selected->bssid), selected->level);
  801. if (wpa_s->current_ssid->bssid_set &&
  802. os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
  803. 0) {
  804. wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
  805. "has preferred BSSID");
  806. return 1;
  807. }
  808. min_diff = 2;
  809. if (current_bss->level < 0) {
  810. if (current_bss->level < -85)
  811. min_diff = 1;
  812. else if (current_bss->level < -80)
  813. min_diff = 2;
  814. else if (current_bss->level < -75)
  815. min_diff = 3;
  816. else if (current_bss->level < -70)
  817. min_diff = 4;
  818. else
  819. min_diff = 5;
  820. }
  821. if (abs(current_bss->level - selected->level) < min_diff) {
  822. wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
  823. "in signal level");
  824. return 0;
  825. }
  826. return 1;
  827. }
  828. /* Return < 0 if no scan results could be fetched. */
  829. static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  830. union wpa_event_data *data)
  831. {
  832. struct wpa_bss *selected;
  833. struct wpa_ssid *ssid = NULL;
  834. struct wpa_scan_results *scan_res;
  835. int ap = 0;
  836. #ifdef CONFIG_AP
  837. if (wpa_s->ap_iface)
  838. ap = 1;
  839. #endif /* CONFIG_AP */
  840. wpa_supplicant_notify_scanning(wpa_s, 0);
  841. #ifdef CONFIG_P2P
  842. if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
  843. wpa_s->global->p2p != NULL) {
  844. wpa_s->p2p_cb_on_scan_complete = 0;
  845. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  846. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  847. "stopped scan processing");
  848. return -1;
  849. }
  850. }
  851. #endif /* CONFIG_P2P */
  852. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  853. data ? &data->scan_info :
  854. NULL, 1);
  855. if (scan_res == NULL) {
  856. if (wpa_s->conf->ap_scan == 2 || ap)
  857. return -1;
  858. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
  859. "scanning again");
  860. wpa_supplicant_req_new_scan(wpa_s, 1, 0);
  861. return -1;
  862. }
  863. #ifndef CONFIG_NO_RANDOM_POOL
  864. size_t i, num;
  865. num = scan_res->num;
  866. if (num > 10)
  867. num = 10;
  868. for (i = 0; i < num; i++) {
  869. u8 buf[5];
  870. struct wpa_scan_res *res = scan_res->res[i];
  871. buf[0] = res->bssid[5];
  872. buf[1] = res->qual & 0xff;
  873. buf[2] = res->noise & 0xff;
  874. buf[3] = res->level & 0xff;
  875. buf[4] = res->tsf & 0xff;
  876. random_add_randomness(buf, sizeof(buf));
  877. }
  878. #endif /* CONFIG_NO_RANDOM_POOL */
  879. if (wpa_s->scan_res_handler) {
  880. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  881. struct wpa_scan_results *scan_res);
  882. scan_res_handler = wpa_s->scan_res_handler;
  883. wpa_s->scan_res_handler = NULL;
  884. scan_res_handler(wpa_s, scan_res);
  885. wpa_scan_results_free(scan_res);
  886. return 0;
  887. }
  888. if (ap) {
  889. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
  890. #ifdef CONFIG_AP
  891. if (wpa_s->ap_iface->scan_cb)
  892. wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
  893. #endif /* CONFIG_AP */
  894. wpa_scan_results_free(scan_res);
  895. return 0;
  896. }
  897. wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
  898. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  899. wpas_notify_scan_results(wpa_s);
  900. wpas_notify_scan_done(wpa_s, 1);
  901. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
  902. wpa_scan_results_free(scan_res);
  903. return 0;
  904. }
  905. if (wpa_s->disconnected) {
  906. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  907. wpa_scan_results_free(scan_res);
  908. return 0;
  909. }
  910. if (!wpas_driver_bss_selection(wpa_s) &&
  911. bgscan_notify_scan(wpa_s, scan_res) == 1) {
  912. wpa_scan_results_free(scan_res);
  913. return 0;
  914. }
  915. selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
  916. if (selected) {
  917. int skip;
  918. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
  919. scan_res);
  920. wpa_scan_results_free(scan_res);
  921. if (skip) {
  922. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  923. return 0;
  924. }
  925. if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
  926. wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
  927. return -1;
  928. }
  929. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  930. } else {
  931. wpa_scan_results_free(scan_res);
  932. wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
  933. ssid = wpa_supplicant_pick_new_network(wpa_s);
  934. if (ssid) {
  935. wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
  936. wpa_supplicant_associate(wpa_s, NULL, ssid);
  937. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  938. } else {
  939. int timeout_sec = wpa_s->scan_interval;
  940. int timeout_usec = 0;
  941. #ifdef CONFIG_P2P
  942. if (wpa_s->p2p_in_provisioning) {
  943. /*
  944. * Use shorter wait during P2P Provisioning
  945. * state to speed up group formation.
  946. */
  947. timeout_sec = 0;
  948. timeout_usec = 250000;
  949. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  950. timeout_usec);
  951. return 0;
  952. }
  953. #endif /* CONFIG_P2P */
  954. if (wpa_supplicant_req_sched_scan(wpa_s))
  955. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  956. timeout_usec);
  957. }
  958. }
  959. return 0;
  960. }
  961. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  962. union wpa_event_data *data)
  963. {
  964. const char *rn, *rn2;
  965. struct wpa_supplicant *ifs;
  966. if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
  967. /*
  968. * If no scan results could be fetched, then no need to
  969. * notify those interfaces that did not actually request
  970. * this scan.
  971. */
  972. return;
  973. }
  974. /*
  975. * Check other interfaces to see if they have the same radio-name. If
  976. * so, they get updated with this same scan info.
  977. */
  978. if (!wpa_s->driver->get_radio_name)
  979. return;
  980. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  981. if (rn == NULL || rn[0] == '\0')
  982. return;
  983. wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
  984. "sharing same radio (%s) in event_scan_results", rn);
  985. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  986. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  987. continue;
  988. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  989. if (rn2 && os_strcmp(rn, rn2) == 0) {
  990. wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
  991. "sibling", ifs->ifname);
  992. _wpa_supplicant_event_scan_results(ifs, data);
  993. }
  994. }
  995. }
  996. #endif /* CONFIG_NO_SCAN_PROCESSING */
  997. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  998. union wpa_event_data *data)
  999. {
  1000. int l, len, found = 0, wpa_found, rsn_found;
  1001. const u8 *p;
  1002. wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
  1003. if (data->assoc_info.req_ies)
  1004. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  1005. data->assoc_info.req_ies_len);
  1006. if (data->assoc_info.resp_ies) {
  1007. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  1008. data->assoc_info.resp_ies_len);
  1009. #ifdef CONFIG_TDLS
  1010. wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
  1011. data->assoc_info.resp_ies_len);
  1012. #endif /* CONFIG_TDLS */
  1013. }
  1014. if (data->assoc_info.beacon_ies)
  1015. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  1016. data->assoc_info.beacon_ies,
  1017. data->assoc_info.beacon_ies_len);
  1018. if (data->assoc_info.freq)
  1019. wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
  1020. data->assoc_info.freq);
  1021. p = data->assoc_info.req_ies;
  1022. l = data->assoc_info.req_ies_len;
  1023. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  1024. while (p && l >= 2) {
  1025. len = p[1] + 2;
  1026. if (len > l) {
  1027. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1028. p, l);
  1029. break;
  1030. }
  1031. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1032. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  1033. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  1034. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  1035. break;
  1036. found = 1;
  1037. wpa_find_assoc_pmkid(wpa_s);
  1038. break;
  1039. }
  1040. l -= len;
  1041. p += len;
  1042. }
  1043. if (!found && data->assoc_info.req_ies)
  1044. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1045. #ifdef CONFIG_IEEE80211R
  1046. #ifdef CONFIG_SME
  1047. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  1048. u8 bssid[ETH_ALEN];
  1049. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1050. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1051. data->assoc_info.resp_ies,
  1052. data->assoc_info.resp_ies_len,
  1053. bssid) < 0) {
  1054. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1055. "Reassociation Response failed");
  1056. wpa_supplicant_deauthenticate(
  1057. wpa_s, WLAN_REASON_INVALID_IE);
  1058. return -1;
  1059. }
  1060. }
  1061. p = data->assoc_info.resp_ies;
  1062. l = data->assoc_info.resp_ies_len;
  1063. #ifdef CONFIG_WPS_STRICT
  1064. if (p && wpa_s->current_ssid &&
  1065. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  1066. struct wpabuf *wps;
  1067. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  1068. if (wps == NULL) {
  1069. wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
  1070. "include WPS IE in (Re)Association Response");
  1071. return -1;
  1072. }
  1073. if (wps_validate_assoc_resp(wps) < 0) {
  1074. wpabuf_free(wps);
  1075. wpa_supplicant_deauthenticate(
  1076. wpa_s, WLAN_REASON_INVALID_IE);
  1077. return -1;
  1078. }
  1079. wpabuf_free(wps);
  1080. }
  1081. #endif /* CONFIG_WPS_STRICT */
  1082. /* Go through the IEs and make a copy of the MDIE, if present. */
  1083. while (p && l >= 2) {
  1084. len = p[1] + 2;
  1085. if (len > l) {
  1086. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1087. p, l);
  1088. break;
  1089. }
  1090. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  1091. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  1092. wpa_s->sme.ft_used = 1;
  1093. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  1094. MOBILITY_DOMAIN_ID_LEN);
  1095. break;
  1096. }
  1097. l -= len;
  1098. p += len;
  1099. }
  1100. #endif /* CONFIG_SME */
  1101. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  1102. data->assoc_info.resp_ies_len);
  1103. #endif /* CONFIG_IEEE80211R */
  1104. /* WPA/RSN IE from Beacon/ProbeResp */
  1105. p = data->assoc_info.beacon_ies;
  1106. l = data->assoc_info.beacon_ies_len;
  1107. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  1108. */
  1109. wpa_found = rsn_found = 0;
  1110. while (p && l >= 2) {
  1111. len = p[1] + 2;
  1112. if (len > l) {
  1113. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  1114. p, l);
  1115. break;
  1116. }
  1117. if (!wpa_found &&
  1118. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1119. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  1120. wpa_found = 1;
  1121. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  1122. }
  1123. if (!rsn_found &&
  1124. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  1125. rsn_found = 1;
  1126. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  1127. }
  1128. l -= len;
  1129. p += len;
  1130. }
  1131. if (!wpa_found && data->assoc_info.beacon_ies)
  1132. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  1133. if (!rsn_found && data->assoc_info.beacon_ies)
  1134. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  1135. if (wpa_found || rsn_found)
  1136. wpa_s->ap_ies_from_associnfo = 1;
  1137. if (wpa_s->assoc_freq && data->assoc_info.freq &&
  1138. wpa_s->assoc_freq != data->assoc_info.freq) {
  1139. wpa_printf(MSG_DEBUG, "Operating frequency changed from "
  1140. "%u to %u MHz",
  1141. wpa_s->assoc_freq, data->assoc_info.freq);
  1142. wpa_supplicant_update_scan_results(wpa_s);
  1143. }
  1144. wpa_s->assoc_freq = data->assoc_info.freq;
  1145. return 0;
  1146. }
  1147. static struct wpa_bss * wpa_supplicant_get_new_bss(
  1148. struct wpa_supplicant *wpa_s, const u8 *bssid)
  1149. {
  1150. struct wpa_bss *bss = NULL;
  1151. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1152. if (ssid->ssid_len > 0)
  1153. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  1154. if (!bss)
  1155. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1156. return bss;
  1157. }
  1158. static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
  1159. {
  1160. const u8 *bss_wpa = NULL, *bss_rsn = NULL;
  1161. if (!wpa_s->current_bss || !wpa_s->current_ssid)
  1162. return -1;
  1163. if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
  1164. return 0;
  1165. bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1166. WPA_IE_VENDOR_TYPE);
  1167. bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
  1168. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  1169. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  1170. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  1171. bss_rsn ? 2 + bss_rsn[1] : 0))
  1172. return -1;
  1173. return 0;
  1174. }
  1175. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  1176. union wpa_event_data *data)
  1177. {
  1178. u8 bssid[ETH_ALEN];
  1179. int ft_completed;
  1180. int bssid_changed;
  1181. struct wpa_driver_capa capa;
  1182. #ifdef CONFIG_AP
  1183. if (wpa_s->ap_iface) {
  1184. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  1185. data->assoc_info.addr,
  1186. data->assoc_info.req_ies,
  1187. data->assoc_info.req_ies_len,
  1188. data->assoc_info.reassoc);
  1189. return;
  1190. }
  1191. #endif /* CONFIG_AP */
  1192. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  1193. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  1194. return;
  1195. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  1196. if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  1197. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
  1198. wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  1199. MACSTR, MAC2STR(bssid));
  1200. random_add_randomness(bssid, ETH_ALEN);
  1201. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  1202. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  1203. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1204. if (bssid_changed)
  1205. wpas_notify_bssid_changed(wpa_s);
  1206. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  1207. wpa_clear_keys(wpa_s, bssid);
  1208. }
  1209. if (wpa_supplicant_select_config(wpa_s) < 0) {
  1210. wpa_supplicant_disassociate(
  1211. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1212. return;
  1213. }
  1214. if (wpa_s->current_ssid) {
  1215. struct wpa_bss *bss = NULL;
  1216. bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
  1217. if (!bss) {
  1218. wpa_supplicant_update_scan_results(wpa_s);
  1219. /* Get the BSS from the new scan results */
  1220. bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
  1221. }
  1222. if (bss)
  1223. wpa_s->current_bss = bss;
  1224. }
  1225. if (wpa_s->conf->ap_scan == 1 &&
  1226. wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
  1227. if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
  1228. wpa_msg(wpa_s, MSG_WARNING,
  1229. "WPA/RSN IEs not updated");
  1230. }
  1231. }
  1232. #ifdef CONFIG_SME
  1233. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  1234. wpa_s->sme.prev_bssid_set = 1;
  1235. #endif /* CONFIG_SME */
  1236. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  1237. if (wpa_s->current_ssid) {
  1238. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  1239. * initialized before association, but for other modes,
  1240. * initialize PC/SC here, if the current configuration needs
  1241. * smartcard or SIM/USIM. */
  1242. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  1243. }
  1244. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  1245. if (wpa_s->l2)
  1246. l2_packet_notify_auth_start(wpa_s->l2);
  1247. /*
  1248. * Set portEnabled first to FALSE in order to get EAP state machine out
  1249. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  1250. * state machine may transit to AUTHENTICATING state based on obsolete
  1251. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  1252. * AUTHENTICATED without ever giving chance to EAP state machine to
  1253. * reset the state.
  1254. */
  1255. if (!ft_completed) {
  1256. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1257. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1258. }
  1259. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  1260. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  1261. /* 802.1X::portControl = Auto */
  1262. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1263. wpa_s->eapol_received = 0;
  1264. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1265. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1266. (wpa_s->current_ssid &&
  1267. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1268. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1269. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1270. } else if (!ft_completed) {
  1271. /* Timeout for receiving the first EAPOL packet */
  1272. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1273. }
  1274. wpa_supplicant_cancel_scan(wpa_s);
  1275. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1276. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1277. /*
  1278. * We are done; the driver will take care of RSN 4-way
  1279. * handshake.
  1280. */
  1281. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1282. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1283. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1284. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1285. } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1286. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1287. /*
  1288. * The driver will take care of RSN 4-way handshake, so we need
  1289. * to allow EAPOL supplicant to complete its work without
  1290. * waiting for WPA supplicant.
  1291. */
  1292. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1293. } else if (ft_completed) {
  1294. /*
  1295. * FT protocol completed - make sure EAPOL state machine ends
  1296. * up in authenticated.
  1297. */
  1298. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1299. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1300. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1301. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1302. }
  1303. if (wpa_s->pending_eapol_rx) {
  1304. struct os_time now, age;
  1305. os_get_time(&now);
  1306. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1307. if (age.sec == 0 && age.usec < 100000 &&
  1308. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1309. 0) {
  1310. wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
  1311. "frame that was received just before "
  1312. "association notification");
  1313. wpa_supplicant_rx_eapol(
  1314. wpa_s, wpa_s->pending_eapol_rx_src,
  1315. wpabuf_head(wpa_s->pending_eapol_rx),
  1316. wpabuf_len(wpa_s->pending_eapol_rx));
  1317. }
  1318. wpabuf_free(wpa_s->pending_eapol_rx);
  1319. wpa_s->pending_eapol_rx = NULL;
  1320. }
  1321. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1322. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1323. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1324. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  1325. /* Set static WEP keys again */
  1326. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1327. }
  1328. #ifdef CONFIG_IBSS_RSN
  1329. if (wpa_s->current_ssid &&
  1330. wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
  1331. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1332. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
  1333. wpa_s->ibss_rsn == NULL) {
  1334. wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
  1335. if (!wpa_s->ibss_rsn) {
  1336. wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
  1337. wpa_supplicant_deauthenticate(
  1338. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1339. return;
  1340. }
  1341. ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
  1342. }
  1343. #endif /* CONFIG_IBSS_RSN */
  1344. }
  1345. static int disconnect_reason_recoverable(u16 reason_code)
  1346. {
  1347. return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
  1348. reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
  1349. reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
  1350. }
  1351. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
  1352. u16 reason_code,
  1353. int locally_generated)
  1354. {
  1355. const u8 *bssid;
  1356. int authenticating;
  1357. u8 prev_pending_bssid[ETH_ALEN];
  1358. struct wpa_bss *fast_reconnect = NULL;
  1359. struct wpa_ssid *fast_reconnect_ssid = NULL;
  1360. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1361. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1362. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1363. /*
  1364. * At least Host AP driver and a Prism3 card seemed to be
  1365. * generating streams of disconnected events when configuring
  1366. * IBSS for WPA-None. Ignore them for now.
  1367. */
  1368. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
  1369. "IBSS/WPA-None mode");
  1370. return;
  1371. }
  1372. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  1373. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1374. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1375. "pre-shared key may be incorrect");
  1376. }
  1377. if (!wpa_s->auto_reconnect_disabled ||
  1378. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  1379. wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
  1380. "reconnect (wps=%d wpa_state=%d)",
  1381. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
  1382. wpa_s->wpa_state);
  1383. if (wpa_s->wpa_state == WPA_COMPLETED &&
  1384. wpa_s->current_ssid &&
  1385. wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  1386. !locally_generated &&
  1387. disconnect_reason_recoverable(reason_code)) {
  1388. /*
  1389. * It looks like the AP has dropped association with
  1390. * us, but could allow us to get back in. Try to
  1391. * reconnect to the same BSS without full scan to save
  1392. * time for some common cases.
  1393. */
  1394. fast_reconnect = wpa_s->current_bss;
  1395. fast_reconnect_ssid = wpa_s->current_ssid;
  1396. } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
  1397. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1398. else
  1399. wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
  1400. "immediate scan");
  1401. } else {
  1402. wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
  1403. "try to re-connect");
  1404. wpa_s->reassociate = 0;
  1405. wpa_s->disconnected = 1;
  1406. wpa_supplicant_cancel_sched_scan(wpa_s);
  1407. }
  1408. bssid = wpa_s->bssid;
  1409. if (is_zero_ether_addr(bssid))
  1410. bssid = wpa_s->pending_bssid;
  1411. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  1412. wpas_connection_failed(wpa_s, bssid);
  1413. wpa_sm_notify_disassoc(wpa_s->wpa);
  1414. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  1415. " reason=%d",
  1416. MAC2STR(bssid), reason_code);
  1417. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1418. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
  1419. wpa_s->keys_cleared = 0;
  1420. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1421. }
  1422. wpa_supplicant_mark_disassoc(wpa_s);
  1423. if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1424. sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
  1425. if (fast_reconnect) {
  1426. #ifndef CONFIG_NO_SCAN_PROCESSING
  1427. wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
  1428. if (wpa_supplicant_connect(wpa_s, fast_reconnect,
  1429. fast_reconnect_ssid) < 0) {
  1430. /* Recover through full scan */
  1431. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1432. }
  1433. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1434. }
  1435. }
  1436. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1437. void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
  1438. {
  1439. struct wpa_supplicant *wpa_s = eloop_ctx;
  1440. if (!wpa_s->pending_mic_error_report)
  1441. return;
  1442. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
  1443. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1444. wpa_s->pending_mic_error_report = 0;
  1445. }
  1446. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1447. static void
  1448. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1449. union wpa_event_data *data)
  1450. {
  1451. int pairwise;
  1452. struct os_time t;
  1453. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1454. pairwise = (data && data->michael_mic_failure.unicast);
  1455. os_get_time(&t);
  1456. if ((wpa_s->last_michael_mic_error &&
  1457. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1458. wpa_s->pending_mic_error_report) {
  1459. if (wpa_s->pending_mic_error_report) {
  1460. /*
  1461. * Send the pending MIC error report immediately since
  1462. * we are going to start countermeasures and AP better
  1463. * do the same.
  1464. */
  1465. wpa_sm_key_request(wpa_s->wpa, 1,
  1466. wpa_s->pending_mic_error_pairwise);
  1467. }
  1468. /* Send the new MIC error report immediately since we are going
  1469. * to start countermeasures and AP better do the same.
  1470. */
  1471. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1472. /* initialize countermeasures */
  1473. wpa_s->countermeasures = 1;
  1474. wpa_blacklist_add(wpa_s, wpa_s->bssid);
  1475. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1476. /*
  1477. * Need to wait for completion of request frame. We do not get
  1478. * any callback for the message completion, so just wait a
  1479. * short while and hope for the best. */
  1480. os_sleep(0, 10000);
  1481. wpa_drv_set_countermeasures(wpa_s, 1);
  1482. wpa_supplicant_deauthenticate(wpa_s,
  1483. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1484. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1485. wpa_s, NULL);
  1486. eloop_register_timeout(60, 0,
  1487. wpa_supplicant_stop_countermeasures,
  1488. wpa_s, NULL);
  1489. /* TODO: mark the AP rejected for 60 second. STA is
  1490. * allowed to associate with another AP.. */
  1491. } else {
  1492. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1493. if (wpa_s->mic_errors_seen) {
  1494. /*
  1495. * Reduce the effectiveness of Michael MIC error
  1496. * reports as a means for attacking against TKIP if
  1497. * more than one MIC failure is noticed with the same
  1498. * PTK. We delay the transmission of the reports by a
  1499. * random time between 0 and 60 seconds in order to
  1500. * force the attacker wait 60 seconds before getting
  1501. * the information on whether a frame resulted in a MIC
  1502. * failure.
  1503. */
  1504. u8 rval[4];
  1505. int sec;
  1506. if (os_get_random(rval, sizeof(rval)) < 0)
  1507. sec = os_random() % 60;
  1508. else
  1509. sec = WPA_GET_BE32(rval) % 60;
  1510. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
  1511. "report %d seconds", sec);
  1512. wpa_s->pending_mic_error_report = 1;
  1513. wpa_s->pending_mic_error_pairwise = pairwise;
  1514. eloop_cancel_timeout(
  1515. wpa_supplicant_delayed_mic_error_report,
  1516. wpa_s, NULL);
  1517. eloop_register_timeout(
  1518. sec, os_random() % 1000000,
  1519. wpa_supplicant_delayed_mic_error_report,
  1520. wpa_s, NULL);
  1521. } else {
  1522. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1523. }
  1524. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1525. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1526. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1527. }
  1528. wpa_s->last_michael_mic_error = t.sec;
  1529. wpa_s->mic_errors_seen++;
  1530. }
  1531. #ifdef CONFIG_TERMINATE_ONLASTIF
  1532. static int any_interfaces(struct wpa_supplicant *head)
  1533. {
  1534. struct wpa_supplicant *wpa_s;
  1535. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1536. if (!wpa_s->interface_removed)
  1537. return 1;
  1538. return 0;
  1539. }
  1540. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1541. static void
  1542. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1543. union wpa_event_data *data)
  1544. {
  1545. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1546. return;
  1547. switch (data->interface_status.ievent) {
  1548. case EVENT_INTERFACE_ADDED:
  1549. if (!wpa_s->interface_removed)
  1550. break;
  1551. wpa_s->interface_removed = 0;
  1552. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
  1553. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1554. wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
  1555. "driver after interface was added");
  1556. }
  1557. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1558. break;
  1559. case EVENT_INTERFACE_REMOVED:
  1560. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
  1561. wpa_s->interface_removed = 1;
  1562. wpa_supplicant_mark_disassoc(wpa_s);
  1563. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  1564. l2_packet_deinit(wpa_s->l2);
  1565. wpa_s->l2 = NULL;
  1566. #ifdef CONFIG_IBSS_RSN
  1567. ibss_rsn_deinit(wpa_s->ibss_rsn);
  1568. wpa_s->ibss_rsn = NULL;
  1569. #endif /* CONFIG_IBSS_RSN */
  1570. #ifdef CONFIG_TERMINATE_ONLASTIF
  1571. /* check if last interface */
  1572. if (!any_interfaces(wpa_s->global->ifaces))
  1573. eloop_terminate();
  1574. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1575. break;
  1576. }
  1577. }
  1578. #ifdef CONFIG_PEERKEY
  1579. static void
  1580. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1581. union wpa_event_data *data)
  1582. {
  1583. if (data == NULL)
  1584. return;
  1585. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1586. }
  1587. #endif /* CONFIG_PEERKEY */
  1588. #ifdef CONFIG_TDLS
  1589. static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
  1590. union wpa_event_data *data)
  1591. {
  1592. if (data == NULL)
  1593. return;
  1594. switch (data->tdls.oper) {
  1595. case TDLS_REQUEST_SETUP:
  1596. wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
  1597. break;
  1598. case TDLS_REQUEST_TEARDOWN:
  1599. wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
  1600. data->tdls.reason_code);
  1601. break;
  1602. }
  1603. }
  1604. #endif /* CONFIG_TDLS */
  1605. #ifdef CONFIG_IEEE80211R
  1606. static void
  1607. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1608. union wpa_event_data *data)
  1609. {
  1610. if (data == NULL)
  1611. return;
  1612. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1613. data->ft_ies.ies_len,
  1614. data->ft_ies.ft_action,
  1615. data->ft_ies.target_ap,
  1616. data->ft_ies.ric_ies,
  1617. data->ft_ies.ric_ies_len) < 0) {
  1618. /* TODO: prevent MLME/driver from trying to associate? */
  1619. }
  1620. }
  1621. #endif /* CONFIG_IEEE80211R */
  1622. #ifdef CONFIG_IBSS_RSN
  1623. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1624. union wpa_event_data *data)
  1625. {
  1626. struct wpa_ssid *ssid;
  1627. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  1628. return;
  1629. if (data == NULL)
  1630. return;
  1631. ssid = wpa_s->current_ssid;
  1632. if (ssid == NULL)
  1633. return;
  1634. if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
  1635. return;
  1636. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1637. }
  1638. #endif /* CONFIG_IBSS_RSN */
  1639. #ifdef CONFIG_IEEE80211R
  1640. static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
  1641. size_t len)
  1642. {
  1643. const u8 *sta_addr, *target_ap_addr;
  1644. u16 status;
  1645. wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
  1646. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1647. return; /* only SME case supported for now */
  1648. if (len < 1 + 2 * ETH_ALEN + 2)
  1649. return;
  1650. if (data[0] != 2)
  1651. return; /* Only FT Action Response is supported for now */
  1652. sta_addr = data + 1;
  1653. target_ap_addr = data + 1 + ETH_ALEN;
  1654. status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
  1655. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
  1656. MACSTR " TargetAP " MACSTR " status %u",
  1657. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  1658. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  1659. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
  1660. " in FT Action Response", MAC2STR(sta_addr));
  1661. return;
  1662. }
  1663. if (status) {
  1664. wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
  1665. "failure (status code %d)", status);
  1666. /* TODO: report error to FT code(?) */
  1667. return;
  1668. }
  1669. if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
  1670. len - (1 + 2 * ETH_ALEN + 2), 1,
  1671. target_ap_addr, NULL, 0) < 0)
  1672. return;
  1673. #ifdef CONFIG_SME
  1674. {
  1675. struct wpa_bss *bss;
  1676. bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
  1677. if (bss)
  1678. wpa_s->sme.freq = bss->freq;
  1679. wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
  1680. sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
  1681. WLAN_AUTH_FT);
  1682. }
  1683. #endif /* CONFIG_SME */
  1684. }
  1685. #endif /* CONFIG_IEEE80211R */
  1686. static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
  1687. struct unprot_deauth *e)
  1688. {
  1689. #ifdef CONFIG_IEEE80211W
  1690. wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
  1691. "dropped: " MACSTR " -> " MACSTR
  1692. " (reason code %u)",
  1693. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  1694. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  1695. #endif /* CONFIG_IEEE80211W */
  1696. }
  1697. static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
  1698. struct unprot_disassoc *e)
  1699. {
  1700. #ifdef CONFIG_IEEE80211W
  1701. wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
  1702. "dropped: " MACSTR " -> " MACSTR
  1703. " (reason code %u)",
  1704. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  1705. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  1706. #endif /* CONFIG_IEEE80211W */
  1707. }
  1708. static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
  1709. {
  1710. u8 action, mode;
  1711. const u8 *pos, *end;
  1712. if (rx->data == NULL || rx->len == 0)
  1713. return;
  1714. pos = rx->data;
  1715. end = pos + rx->len;
  1716. action = *pos++;
  1717. wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
  1718. action, MAC2STR(rx->sa));
  1719. switch (action) {
  1720. case WNM_BSS_TRANS_MGMT_REQ:
  1721. if (pos + 5 > end)
  1722. break;
  1723. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
  1724. "Request: dialog_token=%u request_mode=0x%x "
  1725. "disassoc_timer=%u validity_interval=%u",
  1726. pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
  1727. mode = pos[1];
  1728. pos += 5;
  1729. if (mode & 0x08)
  1730. pos += 12; /* BSS Termination Duration */
  1731. if (mode & 0x10) {
  1732. char url[256];
  1733. if (pos + 1 > end || pos + 1 + pos[0] > end) {
  1734. wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
  1735. "Transition Management Request "
  1736. "(URL)");
  1737. break;
  1738. }
  1739. os_memcpy(url, pos + 1, pos[0]);
  1740. url[pos[0]] = '\0';
  1741. wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
  1742. "Imminent - session_info_url=%s", url);
  1743. }
  1744. break;
  1745. }
  1746. }
  1747. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  1748. union wpa_event_data *data)
  1749. {
  1750. struct wpa_supplicant *wpa_s = ctx;
  1751. u16 reason_code = 0;
  1752. int locally_generated = 0;
  1753. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  1754. event != EVENT_INTERFACE_ENABLED &&
  1755. event != EVENT_INTERFACE_STATUS &&
  1756. event != EVENT_SCHED_SCAN_STOPPED) {
  1757. wpa_dbg(wpa_s, MSG_DEBUG,
  1758. "Ignore event %s (%d) while interface is disabled",
  1759. event_to_string(event), event);
  1760. return;
  1761. }
  1762. #ifndef CONFIG_NO_STDOUT_DEBUG
  1763. {
  1764. int level = MSG_DEBUG;
  1765. if (event == EVENT_RX_MGMT && data && data->rx_mgmt.frame &&
  1766. data->rx_mgmt.frame_len >= 24) {
  1767. const struct ieee80211_hdr *hdr;
  1768. u16 fc;
  1769. hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
  1770. fc = le_to_host16(hdr->frame_control);
  1771. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  1772. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  1773. level = MSG_EXCESSIVE;
  1774. }
  1775. wpa_dbg(wpa_s, level, "Event %s (%d) received",
  1776. event_to_string(event), event);
  1777. }
  1778. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1779. switch (event) {
  1780. case EVENT_AUTH:
  1781. sme_event_auth(wpa_s, data);
  1782. break;
  1783. case EVENT_ASSOC:
  1784. wpa_supplicant_event_assoc(wpa_s, data);
  1785. break;
  1786. case EVENT_DISASSOC:
  1787. wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
  1788. if (data) {
  1789. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
  1790. data->disassoc_info.reason_code,
  1791. data->disassoc_info.locally_generated ?
  1792. " (locally generated)" : "");
  1793. if (data->disassoc_info.addr)
  1794. wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
  1795. MAC2STR(data->disassoc_info.addr));
  1796. }
  1797. #ifdef CONFIG_AP
  1798. if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
  1799. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1800. data->disassoc_info.addr);
  1801. break;
  1802. }
  1803. if (wpa_s->ap_iface) {
  1804. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
  1805. "AP mode");
  1806. break;
  1807. }
  1808. #endif /* CONFIG_AP */
  1809. if (data) {
  1810. reason_code = data->disassoc_info.reason_code;
  1811. locally_generated =
  1812. data->disassoc_info.locally_generated;
  1813. wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
  1814. data->disassoc_info.ie,
  1815. data->disassoc_info.ie_len);
  1816. #ifdef CONFIG_P2P
  1817. wpas_p2p_disassoc_notif(
  1818. wpa_s, data->disassoc_info.addr, reason_code,
  1819. data->disassoc_info.ie,
  1820. data->disassoc_info.ie_len);
  1821. #endif /* CONFIG_P2P */
  1822. }
  1823. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1824. sme_event_disassoc(wpa_s, data);
  1825. /* fall through */
  1826. case EVENT_DEAUTH:
  1827. if (event == EVENT_DEAUTH) {
  1828. wpa_dbg(wpa_s, MSG_DEBUG,
  1829. "Deauthentication notification");
  1830. if (data) {
  1831. reason_code = data->deauth_info.reason_code;
  1832. locally_generated =
  1833. data->deauth_info.locally_generated;
  1834. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
  1835. data->deauth_info.reason_code,
  1836. data->deauth_info.locally_generated ?
  1837. " (locally generated)" : "");
  1838. if (data->deauth_info.addr) {
  1839. wpa_dbg(wpa_s, MSG_DEBUG, " * address "
  1840. MACSTR,
  1841. MAC2STR(data->deauth_info.
  1842. addr));
  1843. }
  1844. wpa_hexdump(MSG_DEBUG,
  1845. "Deauthentication frame IE(s)",
  1846. data->deauth_info.ie,
  1847. data->deauth_info.ie_len);
  1848. #ifdef CONFIG_P2P
  1849. wpas_p2p_deauth_notif(
  1850. wpa_s, data->deauth_info.addr,
  1851. reason_code,
  1852. data->deauth_info.ie,
  1853. data->deauth_info.ie_len);
  1854. #endif /* CONFIG_P2P */
  1855. }
  1856. }
  1857. #ifdef CONFIG_AP
  1858. if (wpa_s->ap_iface && data && data->deauth_info.addr) {
  1859. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1860. data->deauth_info.addr);
  1861. break;
  1862. }
  1863. if (wpa_s->ap_iface) {
  1864. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
  1865. "AP mode");
  1866. break;
  1867. }
  1868. #endif /* CONFIG_AP */
  1869. wpa_supplicant_event_disassoc(wpa_s, reason_code,
  1870. locally_generated);
  1871. break;
  1872. case EVENT_MICHAEL_MIC_FAILURE:
  1873. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1874. break;
  1875. #ifndef CONFIG_NO_SCAN_PROCESSING
  1876. case EVENT_SCAN_RESULTS:
  1877. wpa_supplicant_event_scan_results(wpa_s, data);
  1878. break;
  1879. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1880. case EVENT_ASSOCINFO:
  1881. wpa_supplicant_event_associnfo(wpa_s, data);
  1882. break;
  1883. case EVENT_INTERFACE_STATUS:
  1884. wpa_supplicant_event_interface_status(wpa_s, data);
  1885. break;
  1886. case EVENT_PMKID_CANDIDATE:
  1887. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1888. break;
  1889. #ifdef CONFIG_PEERKEY
  1890. case EVENT_STKSTART:
  1891. wpa_supplicant_event_stkstart(wpa_s, data);
  1892. break;
  1893. #endif /* CONFIG_PEERKEY */
  1894. #ifdef CONFIG_TDLS
  1895. case EVENT_TDLS:
  1896. wpa_supplicant_event_tdls(wpa_s, data);
  1897. break;
  1898. #endif /* CONFIG_TDLS */
  1899. #ifdef CONFIG_IEEE80211R
  1900. case EVENT_FT_RESPONSE:
  1901. wpa_supplicant_event_ft_response(wpa_s, data);
  1902. break;
  1903. #endif /* CONFIG_IEEE80211R */
  1904. #ifdef CONFIG_IBSS_RSN
  1905. case EVENT_IBSS_RSN_START:
  1906. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1907. break;
  1908. #endif /* CONFIG_IBSS_RSN */
  1909. case EVENT_ASSOC_REJECT:
  1910. if (data->assoc_reject.bssid)
  1911. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1912. "bssid=" MACSTR " status_code=%u",
  1913. MAC2STR(data->assoc_reject.bssid),
  1914. data->assoc_reject.status_code);
  1915. else
  1916. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1917. "status_code=%u",
  1918. data->assoc_reject.status_code);
  1919. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1920. sme_event_assoc_reject(wpa_s, data);
  1921. break;
  1922. case EVENT_AUTH_TIMED_OUT:
  1923. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1924. sme_event_auth_timed_out(wpa_s, data);
  1925. break;
  1926. case EVENT_ASSOC_TIMED_OUT:
  1927. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1928. sme_event_assoc_timed_out(wpa_s, data);
  1929. break;
  1930. case EVENT_TX_STATUS:
  1931. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
  1932. " type=%d stype=%d",
  1933. MAC2STR(data->tx_status.dst),
  1934. data->tx_status.type, data->tx_status.stype);
  1935. #ifdef CONFIG_AP
  1936. if (wpa_s->ap_iface == NULL) {
  1937. #ifdef CONFIG_OFFCHANNEL
  1938. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1939. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  1940. offchannel_send_action_tx_status(
  1941. wpa_s, data->tx_status.dst,
  1942. data->tx_status.data,
  1943. data->tx_status.data_len,
  1944. data->tx_status.ack ?
  1945. OFFCHANNEL_SEND_ACTION_SUCCESS :
  1946. OFFCHANNEL_SEND_ACTION_NO_ACK);
  1947. #endif /* CONFIG_OFFCHANNEL */
  1948. break;
  1949. }
  1950. #endif /* CONFIG_AP */
  1951. #ifdef CONFIG_OFFCHANNEL
  1952. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
  1953. MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
  1954. /*
  1955. * Catch TX status events for Action frames we sent via group
  1956. * interface in GO mode.
  1957. */
  1958. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1959. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  1960. os_memcmp(wpa_s->parent->pending_action_dst,
  1961. data->tx_status.dst, ETH_ALEN) == 0) {
  1962. offchannel_send_action_tx_status(
  1963. wpa_s->parent, data->tx_status.dst,
  1964. data->tx_status.data,
  1965. data->tx_status.data_len,
  1966. data->tx_status.ack ?
  1967. OFFCHANNEL_SEND_ACTION_SUCCESS :
  1968. OFFCHANNEL_SEND_ACTION_NO_ACK);
  1969. break;
  1970. }
  1971. #endif /* CONFIG_OFFCHANNEL */
  1972. #ifdef CONFIG_AP
  1973. switch (data->tx_status.type) {
  1974. case WLAN_FC_TYPE_MGMT:
  1975. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1976. data->tx_status.data_len,
  1977. data->tx_status.stype,
  1978. data->tx_status.ack);
  1979. break;
  1980. case WLAN_FC_TYPE_DATA:
  1981. ap_tx_status(wpa_s, data->tx_status.dst,
  1982. data->tx_status.data,
  1983. data->tx_status.data_len,
  1984. data->tx_status.ack);
  1985. break;
  1986. }
  1987. #endif /* CONFIG_AP */
  1988. break;
  1989. #ifdef CONFIG_AP
  1990. case EVENT_EAPOL_TX_STATUS:
  1991. ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
  1992. data->eapol_tx_status.data,
  1993. data->eapol_tx_status.data_len,
  1994. data->eapol_tx_status.ack);
  1995. break;
  1996. case EVENT_DRIVER_CLIENT_POLL_OK:
  1997. ap_client_poll_ok(wpa_s, data->client_poll.addr);
  1998. break;
  1999. case EVENT_RX_FROM_UNKNOWN:
  2000. if (wpa_s->ap_iface == NULL)
  2001. break;
  2002. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
  2003. data->rx_from_unknown.wds);
  2004. break;
  2005. case EVENT_RX_MGMT:
  2006. if (wpa_s->ap_iface == NULL) {
  2007. #ifdef CONFIG_P2P
  2008. u16 fc, stype;
  2009. const struct ieee80211_mgmt *mgmt;
  2010. mgmt = (const struct ieee80211_mgmt *)
  2011. data->rx_mgmt.frame;
  2012. fc = le_to_host16(mgmt->frame_control);
  2013. stype = WLAN_FC_GET_STYPE(fc);
  2014. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  2015. data->rx_mgmt.frame_len > 24) {
  2016. const u8 *src = mgmt->sa;
  2017. const u8 *ie = mgmt->u.probe_req.variable;
  2018. size_t ie_len = data->rx_mgmt.frame_len -
  2019. (mgmt->u.probe_req.variable -
  2020. data->rx_mgmt.frame);
  2021. wpas_p2p_probe_req_rx(wpa_s, src, mgmt->da,
  2022. mgmt->bssid, ie, ie_len);
  2023. break;
  2024. }
  2025. #endif /* CONFIG_P2P */
  2026. wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
  2027. "management frame in non-AP mode");
  2028. break;
  2029. }
  2030. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  2031. break;
  2032. #endif /* CONFIG_AP */
  2033. case EVENT_RX_ACTION:
  2034. wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
  2035. " Category=%u DataLen=%d freq=%d MHz",
  2036. MAC2STR(data->rx_action.sa),
  2037. data->rx_action.category, (int) data->rx_action.len,
  2038. data->rx_action.freq);
  2039. #ifdef CONFIG_IEEE80211R
  2040. if (data->rx_action.category == WLAN_ACTION_FT) {
  2041. ft_rx_action(wpa_s, data->rx_action.data,
  2042. data->rx_action.len);
  2043. break;
  2044. }
  2045. #endif /* CONFIG_IEEE80211R */
  2046. #ifdef CONFIG_IEEE80211W
  2047. #ifdef CONFIG_SME
  2048. if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
  2049. sme_sa_query_rx(wpa_s, data->rx_action.sa,
  2050. data->rx_action.data,
  2051. data->rx_action.len);
  2052. break;
  2053. }
  2054. #endif /* CONFIG_SME */
  2055. #endif /* CONFIG_IEEE80211W */
  2056. #ifdef CONFIG_GAS
  2057. if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
  2058. gas_query_rx(wpa_s->gas, data->rx_action.da,
  2059. data->rx_action.sa, data->rx_action.bssid,
  2060. data->rx_action.data, data->rx_action.len,
  2061. data->rx_action.freq) == 0)
  2062. break;
  2063. #endif /* CONFIG_GAS */
  2064. if (data->rx_action.category == WLAN_ACTION_WNM) {
  2065. wnm_action_rx(wpa_s, &data->rx_action);
  2066. break;
  2067. }
  2068. #ifdef CONFIG_TDLS
  2069. if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
  2070. data->rx_action.len >= 4 &&
  2071. data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
  2072. wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
  2073. "Response from " MACSTR,
  2074. MAC2STR(data->rx_action.sa));
  2075. break;
  2076. }
  2077. #endif /* CONFIG_TDLS */
  2078. #ifdef CONFIG_P2P
  2079. wpas_p2p_rx_action(wpa_s, data->rx_action.da,
  2080. data->rx_action.sa,
  2081. data->rx_action.bssid,
  2082. data->rx_action.category,
  2083. data->rx_action.data,
  2084. data->rx_action.len, data->rx_action.freq);
  2085. #endif /* CONFIG_P2P */
  2086. break;
  2087. case EVENT_RX_PROBE_REQ:
  2088. if (data->rx_probe_req.sa == NULL ||
  2089. data->rx_probe_req.ie == NULL)
  2090. break;
  2091. #ifdef CONFIG_AP
  2092. if (wpa_s->ap_iface) {
  2093. hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
  2094. data->rx_probe_req.sa,
  2095. data->rx_probe_req.da,
  2096. data->rx_probe_req.bssid,
  2097. data->rx_probe_req.ie,
  2098. data->rx_probe_req.ie_len);
  2099. break;
  2100. }
  2101. #endif /* CONFIG_AP */
  2102. #ifdef CONFIG_P2P
  2103. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  2104. data->rx_probe_req.da,
  2105. data->rx_probe_req.bssid,
  2106. data->rx_probe_req.ie,
  2107. data->rx_probe_req.ie_len);
  2108. #endif /* CONFIG_P2P */
  2109. break;
  2110. case EVENT_REMAIN_ON_CHANNEL:
  2111. #ifdef CONFIG_OFFCHANNEL
  2112. offchannel_remain_on_channel_cb(
  2113. wpa_s, data->remain_on_channel.freq,
  2114. data->remain_on_channel.duration);
  2115. #endif /* CONFIG_OFFCHANNEL */
  2116. #ifdef CONFIG_P2P
  2117. wpas_p2p_remain_on_channel_cb(
  2118. wpa_s, data->remain_on_channel.freq,
  2119. data->remain_on_channel.duration);
  2120. #endif /* CONFIG_P2P */
  2121. break;
  2122. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  2123. #ifdef CONFIG_OFFCHANNEL
  2124. offchannel_cancel_remain_on_channel_cb(
  2125. wpa_s, data->remain_on_channel.freq);
  2126. #endif /* CONFIG_OFFCHANNEL */
  2127. #ifdef CONFIG_P2P
  2128. wpas_p2p_cancel_remain_on_channel_cb(
  2129. wpa_s, data->remain_on_channel.freq);
  2130. #endif /* CONFIG_P2P */
  2131. break;
  2132. #ifdef CONFIG_P2P
  2133. case EVENT_P2P_DEV_FOUND: {
  2134. struct p2p_peer_info peer_info;
  2135. os_memset(&peer_info, 0, sizeof(peer_info));
  2136. if (data->p2p_dev_found.dev_addr)
  2137. os_memcpy(peer_info.p2p_device_addr,
  2138. data->p2p_dev_found.dev_addr, ETH_ALEN);
  2139. if (data->p2p_dev_found.pri_dev_type)
  2140. os_memcpy(peer_info.pri_dev_type,
  2141. data->p2p_dev_found.pri_dev_type,
  2142. sizeof(peer_info.pri_dev_type));
  2143. if (data->p2p_dev_found.dev_name)
  2144. os_strlcpy(peer_info.device_name,
  2145. data->p2p_dev_found.dev_name,
  2146. sizeof(peer_info.device_name));
  2147. peer_info.config_methods = data->p2p_dev_found.config_methods;
  2148. peer_info.dev_capab = data->p2p_dev_found.dev_capab;
  2149. peer_info.group_capab = data->p2p_dev_found.group_capab;
  2150. /*
  2151. * FIX: new_device=1 is not necessarily correct. We should
  2152. * maintain a P2P peer database in wpa_supplicant and update
  2153. * this information based on whether the peer is truly new.
  2154. */
  2155. wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
  2156. break;
  2157. }
  2158. case EVENT_P2P_GO_NEG_REQ_RX:
  2159. wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
  2160. data->p2p_go_neg_req_rx.dev_passwd_id);
  2161. break;
  2162. case EVENT_P2P_GO_NEG_COMPLETED:
  2163. wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
  2164. break;
  2165. case EVENT_P2P_PROV_DISC_REQUEST:
  2166. wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
  2167. data->p2p_prov_disc_req.config_methods,
  2168. data->p2p_prov_disc_req.dev_addr,
  2169. data->p2p_prov_disc_req.pri_dev_type,
  2170. data->p2p_prov_disc_req.dev_name,
  2171. data->p2p_prov_disc_req.supp_config_methods,
  2172. data->p2p_prov_disc_req.dev_capab,
  2173. data->p2p_prov_disc_req.group_capab,
  2174. NULL, 0);
  2175. break;
  2176. case EVENT_P2P_PROV_DISC_RESPONSE:
  2177. wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
  2178. data->p2p_prov_disc_resp.config_methods);
  2179. break;
  2180. case EVENT_P2P_SD_REQUEST:
  2181. wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
  2182. data->p2p_sd_req.sa,
  2183. data->p2p_sd_req.dialog_token,
  2184. data->p2p_sd_req.update_indic,
  2185. data->p2p_sd_req.tlvs,
  2186. data->p2p_sd_req.tlvs_len);
  2187. break;
  2188. case EVENT_P2P_SD_RESPONSE:
  2189. wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
  2190. data->p2p_sd_resp.update_indic,
  2191. data->p2p_sd_resp.tlvs,
  2192. data->p2p_sd_resp.tlvs_len);
  2193. break;
  2194. #endif /* CONFIG_P2P */
  2195. case EVENT_EAPOL_RX:
  2196. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  2197. data->eapol_rx.data,
  2198. data->eapol_rx.data_len);
  2199. break;
  2200. case EVENT_SIGNAL_CHANGE:
  2201. bgscan_notify_signal_change(
  2202. wpa_s, data->signal_change.above_threshold,
  2203. data->signal_change.current_signal,
  2204. data->signal_change.current_noise,
  2205. data->signal_change.current_txrate);
  2206. break;
  2207. case EVENT_INTERFACE_ENABLED:
  2208. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
  2209. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  2210. wpa_supplicant_update_mac_addr(wpa_s);
  2211. #ifdef CONFIG_AP
  2212. if (!wpa_s->ap_iface) {
  2213. wpa_supplicant_set_state(wpa_s,
  2214. WPA_DISCONNECTED);
  2215. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2216. } else
  2217. wpa_supplicant_set_state(wpa_s,
  2218. WPA_COMPLETED);
  2219. #else /* CONFIG_AP */
  2220. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2221. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2222. #endif /* CONFIG_AP */
  2223. }
  2224. break;
  2225. case EVENT_INTERFACE_DISABLED:
  2226. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
  2227. wpa_supplicant_mark_disassoc(wpa_s);
  2228. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  2229. break;
  2230. case EVENT_CHANNEL_LIST_CHANGED:
  2231. if (wpa_s->drv_priv == NULL)
  2232. break; /* Ignore event during drv initialization */
  2233. #ifdef CONFIG_P2P
  2234. wpas_p2p_update_channel_list(wpa_s);
  2235. #endif /* CONFIG_P2P */
  2236. break;
  2237. case EVENT_INTERFACE_UNAVAILABLE:
  2238. #ifdef CONFIG_P2P
  2239. wpas_p2p_interface_unavailable(wpa_s);
  2240. #endif /* CONFIG_P2P */
  2241. break;
  2242. case EVENT_BEST_CHANNEL:
  2243. wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
  2244. "(%d %d %d)",
  2245. data->best_chan.freq_24, data->best_chan.freq_5,
  2246. data->best_chan.freq_overall);
  2247. wpa_s->best_24_freq = data->best_chan.freq_24;
  2248. wpa_s->best_5_freq = data->best_chan.freq_5;
  2249. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  2250. #ifdef CONFIG_P2P
  2251. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  2252. data->best_chan.freq_5,
  2253. data->best_chan.freq_overall);
  2254. #endif /* CONFIG_P2P */
  2255. break;
  2256. case EVENT_UNPROT_DEAUTH:
  2257. wpa_supplicant_event_unprot_deauth(wpa_s,
  2258. &data->unprot_deauth);
  2259. break;
  2260. case EVENT_UNPROT_DISASSOC:
  2261. wpa_supplicant_event_unprot_disassoc(wpa_s,
  2262. &data->unprot_disassoc);
  2263. break;
  2264. case EVENT_STATION_LOW_ACK:
  2265. #ifdef CONFIG_AP
  2266. if (wpa_s->ap_iface && data)
  2267. hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
  2268. data->low_ack.addr);
  2269. #endif /* CONFIG_AP */
  2270. #ifdef CONFIG_TDLS
  2271. if (data)
  2272. wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
  2273. #endif /* CONFIG_TDLS */
  2274. break;
  2275. case EVENT_IBSS_PEER_LOST:
  2276. #ifdef CONFIG_IBSS_RSN
  2277. ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
  2278. #endif /* CONFIG_IBSS_RSN */
  2279. break;
  2280. case EVENT_DRIVER_GTK_REKEY:
  2281. if (os_memcmp(data->driver_gtk_rekey.bssid,
  2282. wpa_s->bssid, ETH_ALEN))
  2283. break;
  2284. if (!wpa_s->wpa)
  2285. break;
  2286. wpa_sm_update_replay_ctr(wpa_s->wpa,
  2287. data->driver_gtk_rekey.replay_ctr);
  2288. break;
  2289. case EVENT_SCHED_SCAN_STOPPED:
  2290. wpa_s->sched_scanning = 0;
  2291. wpa_supplicant_notify_scanning(wpa_s, 0);
  2292. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2293. break;
  2294. /*
  2295. * If we timed out, start a new sched scan to continue
  2296. * searching for more SSIDs.
  2297. */
  2298. if (wpa_s->sched_scan_timed_out)
  2299. wpa_supplicant_req_sched_scan(wpa_s);
  2300. break;
  2301. case EVENT_WPS_BUTTON_PUSHED:
  2302. #ifdef CONFIG_WPS
  2303. wpas_wps_start_pbc(wpa_s, NULL, 0);
  2304. #endif /* CONFIG_WPS */
  2305. break;
  2306. default:
  2307. wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
  2308. break;
  2309. }
  2310. }