events.c 97 KB

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