events.c 107 KB

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