events.c 93 KB

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