events.c 80 KB

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