events.c 113 KB

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