events.c 84 KB

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