events.c 108 KB

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