events.c 81 KB

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