events.c 96 KB

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