events.c 103 KB

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