events.c 82 KB

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