events.c 91 KB

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