events.c 81 KB

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