events.c 84 KB

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