events.c 78 KB

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