wpa_supplicant.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2012, 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. * This file implements functions for registering and unregistering
  9. * %wpa_supplicant interfaces. In addition, this file contains number of
  10. * functions for managing network connections.
  11. */
  12. #include "includes.h"
  13. #include "common.h"
  14. #include "crypto/random.h"
  15. #include "crypto/sha1.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "eap_peer/eap.h"
  18. #include "eap_server/eap_methods.h"
  19. #include "rsn_supp/wpa.h"
  20. #include "eloop.h"
  21. #include "config.h"
  22. #include "utils/ext_password.h"
  23. #include "l2_packet/l2_packet.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "ctrl_iface.h"
  27. #include "pcsc_funcs.h"
  28. #include "common/version.h"
  29. #include "rsn_supp/preauth.h"
  30. #include "rsn_supp/pmksa_cache.h"
  31. #include "common/wpa_ctrl.h"
  32. #include "common/ieee802_11_defs.h"
  33. #include "p2p/p2p.h"
  34. #include "blacklist.h"
  35. #include "wpas_glue.h"
  36. #include "wps_supplicant.h"
  37. #include "ibss_rsn.h"
  38. #include "sme.h"
  39. #include "gas_query.h"
  40. #include "ap.h"
  41. #include "p2p_supplicant.h"
  42. #include "wifi_display.h"
  43. #include "notify.h"
  44. #include "bgscan.h"
  45. #include "autoscan.h"
  46. #include "bss.h"
  47. #include "scan.h"
  48. #include "offchannel.h"
  49. #include "hs20_supplicant.h"
  50. const char *wpa_supplicant_version =
  51. "wpa_supplicant v" VERSION_STR "\n"
  52. "Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> and contributors";
  53. const char *wpa_supplicant_license =
  54. "This software may be distributed under the terms of the BSD license.\n"
  55. "See README for more details.\n"
  56. #ifdef EAP_TLS_OPENSSL
  57. "\nThis product includes software developed by the OpenSSL Project\n"
  58. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  59. #endif /* EAP_TLS_OPENSSL */
  60. ;
  61. #ifndef CONFIG_NO_STDOUT_DEBUG
  62. /* Long text divided into parts in order to fit in C89 strings size limits. */
  63. const char *wpa_supplicant_full_license1 =
  64. "";
  65. const char *wpa_supplicant_full_license2 =
  66. "This software may be distributed under the terms of the BSD license.\n"
  67. "\n"
  68. "Redistribution and use in source and binary forms, with or without\n"
  69. "modification, are permitted provided that the following conditions are\n"
  70. "met:\n"
  71. "\n";
  72. const char *wpa_supplicant_full_license3 =
  73. "1. Redistributions of source code must retain the above copyright\n"
  74. " notice, this list of conditions and the following disclaimer.\n"
  75. "\n"
  76. "2. Redistributions in binary form must reproduce the above copyright\n"
  77. " notice, this list of conditions and the following disclaimer in the\n"
  78. " documentation and/or other materials provided with the distribution.\n"
  79. "\n";
  80. const char *wpa_supplicant_full_license4 =
  81. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  82. " names of its contributors may be used to endorse or promote products\n"
  83. " derived from this software without specific prior written permission.\n"
  84. "\n"
  85. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  86. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  87. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  88. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  89. const char *wpa_supplicant_full_license5 =
  90. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  91. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  92. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  93. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  94. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  95. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  96. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  97. "\n";
  98. #endif /* CONFIG_NO_STDOUT_DEBUG */
  99. extern int wpa_debug_level;
  100. extern int wpa_debug_show_keys;
  101. extern int wpa_debug_timestamp;
  102. extern struct wpa_driver_ops *wpa_drivers[];
  103. /* Configure default/group WEP keys for static WEP */
  104. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  105. {
  106. int i, set = 0;
  107. for (i = 0; i < NUM_WEP_KEYS; i++) {
  108. if (ssid->wep_key_len[i] == 0)
  109. continue;
  110. set = 1;
  111. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  112. i, i == ssid->wep_tx_keyidx, NULL, 0,
  113. ssid->wep_key[i], ssid->wep_key_len[i]);
  114. }
  115. return set;
  116. }
  117. static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  118. struct wpa_ssid *ssid)
  119. {
  120. u8 key[32];
  121. size_t keylen;
  122. enum wpa_alg alg;
  123. u8 seq[6] = { 0 };
  124. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  125. * sending unicast and multicast packets. */
  126. if (ssid->mode != WPAS_MODE_IBSS) {
  127. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  128. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  129. return -1;
  130. }
  131. if (!ssid->psk_set) {
  132. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  133. "WPA-None");
  134. return -1;
  135. }
  136. switch (wpa_s->group_cipher) {
  137. case WPA_CIPHER_CCMP:
  138. os_memcpy(key, ssid->psk, 16);
  139. keylen = 16;
  140. alg = WPA_ALG_CCMP;
  141. break;
  142. case WPA_CIPHER_GCMP:
  143. os_memcpy(key, ssid->psk, 16);
  144. keylen = 16;
  145. alg = WPA_ALG_GCMP;
  146. break;
  147. case WPA_CIPHER_TKIP:
  148. /* WPA-None uses the same Michael MIC key for both TX and RX */
  149. os_memcpy(key, ssid->psk, 16 + 8);
  150. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  151. keylen = 32;
  152. alg = WPA_ALG_TKIP;
  153. break;
  154. default:
  155. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  156. "WPA-None", wpa_s->group_cipher);
  157. return -1;
  158. }
  159. /* TODO: should actually remember the previously used seq#, both for TX
  160. * and RX from each STA.. */
  161. return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  162. }
  163. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  164. {
  165. struct wpa_supplicant *wpa_s = eloop_ctx;
  166. const u8 *bssid = wpa_s->bssid;
  167. if (is_zero_ether_addr(bssid))
  168. bssid = wpa_s->pending_bssid;
  169. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  170. MAC2STR(bssid));
  171. wpa_blacklist_add(wpa_s, bssid);
  172. wpa_sm_notify_disassoc(wpa_s->wpa);
  173. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  174. wpa_s->reassociate = 1;
  175. /*
  176. * If we timed out, the AP or the local radio may be busy.
  177. * So, wait a second until scanning again.
  178. */
  179. wpa_supplicant_req_scan(wpa_s, 1, 0);
  180. #ifdef CONFIG_P2P
  181. if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
  182. wpa_s->global->p2p != NULL) {
  183. wpa_s->global->p2p_cb_on_scan_complete = 0;
  184. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  185. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  186. "continued after timed out authentication");
  187. }
  188. }
  189. #endif /* CONFIG_P2P */
  190. }
  191. /**
  192. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  193. * @wpa_s: Pointer to wpa_supplicant data
  194. * @sec: Number of seconds after which to time out authentication
  195. * @usec: Number of microseconds after which to time out authentication
  196. *
  197. * This function is used to schedule a timeout for the current authentication
  198. * attempt.
  199. */
  200. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  201. int sec, int usec)
  202. {
  203. if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
  204. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  205. return;
  206. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  207. "%d usec", sec, usec);
  208. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  209. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  210. }
  211. /**
  212. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  213. * @wpa_s: Pointer to wpa_supplicant data
  214. *
  215. * This function is used to cancel authentication timeout scheduled with
  216. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  217. * been completed.
  218. */
  219. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  220. {
  221. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  222. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  223. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  224. }
  225. /**
  226. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  227. * @wpa_s: Pointer to wpa_supplicant data
  228. *
  229. * This function is used to configure EAPOL state machine based on the selected
  230. * authentication mode.
  231. */
  232. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  233. {
  234. #ifdef IEEE8021X_EAPOL
  235. struct eapol_config eapol_conf;
  236. struct wpa_ssid *ssid = wpa_s->current_ssid;
  237. #ifdef CONFIG_IBSS_RSN
  238. if (ssid->mode == WPAS_MODE_IBSS &&
  239. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  240. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  241. /*
  242. * RSN IBSS authentication is per-STA and we can disable the
  243. * per-BSSID EAPOL authentication.
  244. */
  245. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  246. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  247. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  248. return;
  249. }
  250. #endif /* CONFIG_IBSS_RSN */
  251. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  252. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  253. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  254. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  255. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  256. else
  257. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  258. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  259. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  260. eapol_conf.accept_802_1x_keys = 1;
  261. eapol_conf.required_keys = 0;
  262. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  263. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  264. }
  265. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  266. eapol_conf.required_keys |=
  267. EAPOL_REQUIRE_KEY_BROADCAST;
  268. }
  269. if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  270. eapol_conf.required_keys = 0;
  271. }
  272. if (wpa_s->conf)
  273. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  274. eapol_conf.workaround = ssid->eap_workaround;
  275. eapol_conf.eap_disabled =
  276. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  277. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  278. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  279. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  280. #endif /* IEEE8021X_EAPOL */
  281. }
  282. /**
  283. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  284. * @wpa_s: Pointer to wpa_supplicant data
  285. * @ssid: Configuration data for the network
  286. *
  287. * This function is used to configure WPA state machine and related parameters
  288. * to a mode where WPA is not enabled. This is called as part of the
  289. * authentication configuration when the selected network does not use WPA.
  290. */
  291. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  292. struct wpa_ssid *ssid)
  293. {
  294. int i;
  295. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  296. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  297. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  298. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  299. else
  300. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  301. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  302. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  303. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  304. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  305. wpa_s->group_cipher = WPA_CIPHER_NONE;
  306. wpa_s->mgmt_group_cipher = 0;
  307. for (i = 0; i < NUM_WEP_KEYS; i++) {
  308. if (ssid->wep_key_len[i] > 5) {
  309. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  310. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  311. break;
  312. } else if (ssid->wep_key_len[i] > 0) {
  313. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  314. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  315. break;
  316. }
  317. }
  318. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  319. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  320. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  321. wpa_s->pairwise_cipher);
  322. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  323. #ifdef CONFIG_IEEE80211W
  324. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  325. wpa_s->mgmt_group_cipher);
  326. #endif /* CONFIG_IEEE80211W */
  327. pmksa_cache_clear_current(wpa_s->wpa);
  328. }
  329. void free_hw_features(struct wpa_supplicant *wpa_s)
  330. {
  331. int i;
  332. if (wpa_s->hw.modes == NULL)
  333. return;
  334. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  335. os_free(wpa_s->hw.modes[i].channels);
  336. os_free(wpa_s->hw.modes[i].rates);
  337. }
  338. os_free(wpa_s->hw.modes);
  339. wpa_s->hw.modes = NULL;
  340. }
  341. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  342. {
  343. bgscan_deinit(wpa_s);
  344. autoscan_deinit(wpa_s);
  345. scard_deinit(wpa_s->scard);
  346. wpa_s->scard = NULL;
  347. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  348. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  349. l2_packet_deinit(wpa_s->l2);
  350. wpa_s->l2 = NULL;
  351. if (wpa_s->l2_br) {
  352. l2_packet_deinit(wpa_s->l2_br);
  353. wpa_s->l2_br = NULL;
  354. }
  355. if (wpa_s->conf != NULL) {
  356. struct wpa_ssid *ssid;
  357. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  358. wpas_notify_network_removed(wpa_s, ssid);
  359. }
  360. os_free(wpa_s->confname);
  361. wpa_s->confname = NULL;
  362. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  363. eapol_sm_deinit(wpa_s->eapol);
  364. wpa_s->eapol = NULL;
  365. rsn_preauth_deinit(wpa_s->wpa);
  366. #ifdef CONFIG_TDLS
  367. wpa_tdls_deinit(wpa_s->wpa);
  368. #endif /* CONFIG_TDLS */
  369. pmksa_candidate_free(wpa_s->wpa);
  370. wpa_sm_deinit(wpa_s->wpa);
  371. wpa_s->wpa = NULL;
  372. wpa_blacklist_clear(wpa_s);
  373. wpa_bss_deinit(wpa_s);
  374. wpa_supplicant_cancel_scan(wpa_s);
  375. wpa_supplicant_cancel_auth_timeout(wpa_s);
  376. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  377. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  378. eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
  379. wpa_s, NULL);
  380. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  381. wpas_wps_deinit(wpa_s);
  382. wpabuf_free(wpa_s->pending_eapol_rx);
  383. wpa_s->pending_eapol_rx = NULL;
  384. #ifdef CONFIG_IBSS_RSN
  385. ibss_rsn_deinit(wpa_s->ibss_rsn);
  386. wpa_s->ibss_rsn = NULL;
  387. #endif /* CONFIG_IBSS_RSN */
  388. sme_deinit(wpa_s);
  389. #ifdef CONFIG_AP
  390. wpa_supplicant_ap_deinit(wpa_s);
  391. #endif /* CONFIG_AP */
  392. #ifdef CONFIG_P2P
  393. wpas_p2p_deinit(wpa_s);
  394. #endif /* CONFIG_P2P */
  395. #ifdef CONFIG_OFFCHANNEL
  396. offchannel_deinit(wpa_s);
  397. #endif /* CONFIG_OFFCHANNEL */
  398. wpa_supplicant_cancel_sched_scan(wpa_s);
  399. os_free(wpa_s->next_scan_freqs);
  400. wpa_s->next_scan_freqs = NULL;
  401. gas_query_deinit(wpa_s->gas);
  402. wpa_s->gas = NULL;
  403. free_hw_features(wpa_s);
  404. os_free(wpa_s->bssid_filter);
  405. wpa_s->bssid_filter = NULL;
  406. os_free(wpa_s->disallow_aps_bssid);
  407. wpa_s->disallow_aps_bssid = NULL;
  408. os_free(wpa_s->disallow_aps_ssid);
  409. wpa_s->disallow_aps_ssid = NULL;
  410. wnm_bss_keep_alive_deinit(wpa_s);
  411. ext_password_deinit(wpa_s->ext_pw);
  412. wpa_s->ext_pw = NULL;
  413. wpabuf_free(wpa_s->last_gas_resp);
  414. os_free(wpa_s->last_scan_res);
  415. wpa_s->last_scan_res = NULL;
  416. }
  417. /**
  418. * wpa_clear_keys - Clear keys configured for the driver
  419. * @wpa_s: Pointer to wpa_supplicant data
  420. * @addr: Previously used BSSID or %NULL if not available
  421. *
  422. * This function clears the encryption keys that has been previously configured
  423. * for the driver.
  424. */
  425. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  426. {
  427. if (wpa_s->keys_cleared) {
  428. /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
  429. * timing issues with keys being cleared just before new keys
  430. * are set or just after association or something similar. This
  431. * shows up in group key handshake failing often because of the
  432. * client not receiving the first encrypted packets correctly.
  433. * Skipping some of the extra key clearing steps seems to help
  434. * in completing group key handshake more reliably. */
  435. wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
  436. "skip key clearing");
  437. return;
  438. }
  439. /* MLME-DELETEKEYS.request */
  440. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  441. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  442. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  443. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  444. #ifdef CONFIG_IEEE80211W
  445. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  446. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  447. #endif /* CONFIG_IEEE80211W */
  448. if (addr) {
  449. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  450. 0);
  451. /* MLME-SETPROTECTION.request(None) */
  452. wpa_drv_mlme_setprotection(
  453. wpa_s, addr,
  454. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  455. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  456. }
  457. wpa_s->keys_cleared = 1;
  458. }
  459. /**
  460. * wpa_supplicant_state_txt - Get the connection state name as a text string
  461. * @state: State (wpa_state; WPA_*)
  462. * Returns: The state name as a printable text string
  463. */
  464. const char * wpa_supplicant_state_txt(enum wpa_states state)
  465. {
  466. switch (state) {
  467. case WPA_DISCONNECTED:
  468. return "DISCONNECTED";
  469. case WPA_INACTIVE:
  470. return "INACTIVE";
  471. case WPA_INTERFACE_DISABLED:
  472. return "INTERFACE_DISABLED";
  473. case WPA_SCANNING:
  474. return "SCANNING";
  475. case WPA_AUTHENTICATING:
  476. return "AUTHENTICATING";
  477. case WPA_ASSOCIATING:
  478. return "ASSOCIATING";
  479. case WPA_ASSOCIATED:
  480. return "ASSOCIATED";
  481. case WPA_4WAY_HANDSHAKE:
  482. return "4WAY_HANDSHAKE";
  483. case WPA_GROUP_HANDSHAKE:
  484. return "GROUP_HANDSHAKE";
  485. case WPA_COMPLETED:
  486. return "COMPLETED";
  487. default:
  488. return "UNKNOWN";
  489. }
  490. }
  491. #ifdef CONFIG_BGSCAN
  492. static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
  493. {
  494. if (wpas_driver_bss_selection(wpa_s))
  495. return;
  496. if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
  497. return;
  498. bgscan_deinit(wpa_s);
  499. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  500. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  501. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  502. "bgscan");
  503. /*
  504. * Live without bgscan; it is only used as a roaming
  505. * optimization, so the initial connection is not
  506. * affected.
  507. */
  508. } else {
  509. struct wpa_scan_results *scan_res;
  510. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  511. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
  512. 0);
  513. if (scan_res) {
  514. bgscan_notify_scan(wpa_s, scan_res);
  515. wpa_scan_results_free(scan_res);
  516. }
  517. }
  518. } else
  519. wpa_s->bgscan_ssid = NULL;
  520. }
  521. static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
  522. {
  523. if (wpa_s->bgscan_ssid != NULL) {
  524. bgscan_deinit(wpa_s);
  525. wpa_s->bgscan_ssid = NULL;
  526. }
  527. }
  528. #endif /* CONFIG_BGSCAN */
  529. static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
  530. {
  531. if (autoscan_init(wpa_s, 0))
  532. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
  533. }
  534. static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
  535. {
  536. autoscan_deinit(wpa_s);
  537. }
  538. void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
  539. {
  540. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  541. wpa_s->wpa_state == WPA_SCANNING) {
  542. autoscan_deinit(wpa_s);
  543. wpa_supplicant_start_autoscan(wpa_s);
  544. }
  545. }
  546. /**
  547. * wpa_supplicant_set_state - Set current connection state
  548. * @wpa_s: Pointer to wpa_supplicant data
  549. * @state: The new connection state
  550. *
  551. * This function is called whenever the connection state changes, e.g.,
  552. * association is completed for WPA/WPA2 4-Way Handshake is started.
  553. */
  554. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  555. enum wpa_states state)
  556. {
  557. enum wpa_states old_state = wpa_s->wpa_state;
  558. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  559. wpa_supplicant_state_txt(wpa_s->wpa_state),
  560. wpa_supplicant_state_txt(state));
  561. if (state != WPA_SCANNING)
  562. wpa_supplicant_notify_scanning(wpa_s, 0);
  563. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  564. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  565. struct wpa_ssid *ssid = wpa_s->current_ssid;
  566. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  567. MACSTR " completed %s [id=%d id_str=%s]",
  568. MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
  569. "(reauth)" : "(auth)",
  570. ssid ? ssid->id : -1,
  571. ssid && ssid->id_str ? ssid->id_str : "");
  572. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  573. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  574. wpa_s->new_connection = 0;
  575. wpa_s->reassociated_connection = 1;
  576. wpa_drv_set_operstate(wpa_s, 1);
  577. #ifndef IEEE8021X_EAPOL
  578. wpa_drv_set_supp_port(wpa_s, 1);
  579. #endif /* IEEE8021X_EAPOL */
  580. wpa_s->after_wps = 0;
  581. #ifdef CONFIG_P2P
  582. wpas_p2p_completed(wpa_s);
  583. #endif /* CONFIG_P2P */
  584. sme_sched_obss_scan(wpa_s, 1);
  585. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  586. state == WPA_ASSOCIATED) {
  587. wpa_s->new_connection = 1;
  588. wpa_drv_set_operstate(wpa_s, 0);
  589. #ifndef IEEE8021X_EAPOL
  590. wpa_drv_set_supp_port(wpa_s, 0);
  591. #endif /* IEEE8021X_EAPOL */
  592. sme_sched_obss_scan(wpa_s, 0);
  593. }
  594. wpa_s->wpa_state = state;
  595. #ifdef CONFIG_BGSCAN
  596. if (state == WPA_COMPLETED)
  597. wpa_supplicant_start_bgscan(wpa_s);
  598. else
  599. wpa_supplicant_stop_bgscan(wpa_s);
  600. #endif /* CONFIG_BGSCAN */
  601. if (state == WPA_AUTHENTICATING)
  602. wpa_supplicant_stop_autoscan(wpa_s);
  603. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  604. wpa_supplicant_start_autoscan(wpa_s);
  605. if (wpa_s->wpa_state != old_state) {
  606. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  607. if (wpa_s->wpa_state == WPA_COMPLETED ||
  608. old_state == WPA_COMPLETED)
  609. wpas_notify_auth_changed(wpa_s);
  610. }
  611. }
  612. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  613. {
  614. int pending = 0;
  615. #ifdef CONFIG_WPS
  616. struct wpa_supplicant *wpa_s = global->ifaces;
  617. while (wpa_s) {
  618. if (wpas_wps_terminate_pending(wpa_s) == 1)
  619. pending = 1;
  620. wpa_s = wpa_s->next;
  621. }
  622. #endif /* CONFIG_WPS */
  623. if (pending)
  624. return;
  625. eloop_terminate();
  626. }
  627. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  628. {
  629. struct wpa_global *global = signal_ctx;
  630. wpa_supplicant_terminate_proc(global);
  631. }
  632. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  633. {
  634. enum wpa_states old_state = wpa_s->wpa_state;
  635. wpa_s->pairwise_cipher = 0;
  636. wpa_s->group_cipher = 0;
  637. wpa_s->mgmt_group_cipher = 0;
  638. wpa_s->key_mgmt = 0;
  639. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  640. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  641. if (wpa_s->wpa_state != old_state)
  642. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  643. }
  644. /**
  645. * wpa_supplicant_reload_configuration - Reload configuration data
  646. * @wpa_s: Pointer to wpa_supplicant data
  647. * Returns: 0 on success or -1 if configuration parsing failed
  648. *
  649. * This function can be used to request that the configuration data is reloaded
  650. * (e.g., after configuration file change). This function is reloading
  651. * configuration only for one interface, so this may need to be called multiple
  652. * times if %wpa_supplicant is controlling multiple interfaces and all
  653. * interfaces need reconfiguration.
  654. */
  655. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  656. {
  657. struct wpa_config *conf;
  658. int reconf_ctrl;
  659. int old_ap_scan;
  660. if (wpa_s->confname == NULL)
  661. return -1;
  662. conf = wpa_config_read(wpa_s->confname);
  663. if (conf == NULL) {
  664. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  665. "file '%s' - exiting", wpa_s->confname);
  666. return -1;
  667. }
  668. conf->changed_parameters = (unsigned int) -1;
  669. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  670. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  671. os_strcmp(conf->ctrl_interface,
  672. wpa_s->conf->ctrl_interface) != 0);
  673. if (reconf_ctrl && wpa_s->ctrl_iface) {
  674. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  675. wpa_s->ctrl_iface = NULL;
  676. }
  677. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  678. if (wpa_s->current_ssid) {
  679. wpa_supplicant_deauthenticate(wpa_s,
  680. WLAN_REASON_DEAUTH_LEAVING);
  681. }
  682. /*
  683. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  684. * pkcs11_engine_path, pkcs11_module_path.
  685. */
  686. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  687. /*
  688. * Clear forced success to clear EAP state for next
  689. * authentication.
  690. */
  691. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  692. }
  693. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  694. wpa_sm_set_config(wpa_s->wpa, NULL);
  695. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  696. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  697. rsn_preauth_deinit(wpa_s->wpa);
  698. old_ap_scan = wpa_s->conf->ap_scan;
  699. wpa_config_free(wpa_s->conf);
  700. wpa_s->conf = conf;
  701. if (old_ap_scan != wpa_s->conf->ap_scan)
  702. wpas_notify_ap_scan_changed(wpa_s);
  703. if (reconf_ctrl)
  704. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  705. wpa_supplicant_update_config(wpa_s);
  706. wpa_supplicant_clear_status(wpa_s);
  707. if (wpa_supplicant_enabled_networks(wpa_s)) {
  708. wpa_s->reassociate = 1;
  709. wpa_supplicant_req_scan(wpa_s, 0, 0);
  710. }
  711. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  712. return 0;
  713. }
  714. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  715. {
  716. struct wpa_global *global = signal_ctx;
  717. struct wpa_supplicant *wpa_s;
  718. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  719. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  720. sig);
  721. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  722. wpa_supplicant_terminate_proc(global);
  723. }
  724. }
  725. }
  726. enum wpa_cipher cipher_suite2driver(int cipher)
  727. {
  728. switch (cipher) {
  729. case WPA_CIPHER_NONE:
  730. return CIPHER_NONE;
  731. case WPA_CIPHER_WEP40:
  732. return CIPHER_WEP40;
  733. case WPA_CIPHER_WEP104:
  734. return CIPHER_WEP104;
  735. case WPA_CIPHER_CCMP:
  736. return CIPHER_CCMP;
  737. case WPA_CIPHER_GCMP:
  738. return CIPHER_GCMP;
  739. case WPA_CIPHER_TKIP:
  740. default:
  741. return CIPHER_TKIP;
  742. }
  743. }
  744. enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
  745. {
  746. switch (key_mgmt) {
  747. case WPA_KEY_MGMT_NONE:
  748. return KEY_MGMT_NONE;
  749. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  750. return KEY_MGMT_802_1X_NO_WPA;
  751. case WPA_KEY_MGMT_IEEE8021X:
  752. return KEY_MGMT_802_1X;
  753. case WPA_KEY_MGMT_WPA_NONE:
  754. return KEY_MGMT_WPA_NONE;
  755. case WPA_KEY_MGMT_FT_IEEE8021X:
  756. return KEY_MGMT_FT_802_1X;
  757. case WPA_KEY_MGMT_FT_PSK:
  758. return KEY_MGMT_FT_PSK;
  759. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  760. return KEY_MGMT_802_1X_SHA256;
  761. case WPA_KEY_MGMT_PSK_SHA256:
  762. return KEY_MGMT_PSK_SHA256;
  763. case WPA_KEY_MGMT_WPS:
  764. return KEY_MGMT_WPS;
  765. case WPA_KEY_MGMT_PSK:
  766. default:
  767. return KEY_MGMT_PSK;
  768. }
  769. }
  770. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  771. struct wpa_ssid *ssid,
  772. struct wpa_ie_data *ie)
  773. {
  774. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  775. if (ret) {
  776. if (ret == -2) {
  777. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  778. "from association info");
  779. }
  780. return -1;
  781. }
  782. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  783. "cipher suites");
  784. if (!(ie->group_cipher & ssid->group_cipher)) {
  785. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  786. "cipher 0x%x (mask 0x%x) - reject",
  787. ie->group_cipher, ssid->group_cipher);
  788. return -1;
  789. }
  790. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  791. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  792. "cipher 0x%x (mask 0x%x) - reject",
  793. ie->pairwise_cipher, ssid->pairwise_cipher);
  794. return -1;
  795. }
  796. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  797. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  798. "management 0x%x (mask 0x%x) - reject",
  799. ie->key_mgmt, ssid->key_mgmt);
  800. return -1;
  801. }
  802. #ifdef CONFIG_IEEE80211W
  803. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  804. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  805. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  806. "that does not support management frame protection - "
  807. "reject");
  808. return -1;
  809. }
  810. #endif /* CONFIG_IEEE80211W */
  811. return 0;
  812. }
  813. /**
  814. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  815. * @wpa_s: Pointer to wpa_supplicant data
  816. * @bss: Scan results for the selected BSS, or %NULL if not available
  817. * @ssid: Configuration data for the selected network
  818. * @wpa_ie: Buffer for the WPA/RSN IE
  819. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  820. * used buffer length in case the functions returns success.
  821. * Returns: 0 on success or -1 on failure
  822. *
  823. * This function is used to configure authentication and encryption parameters
  824. * based on the network configuration and scan result for the selected BSS (if
  825. * available).
  826. */
  827. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  828. struct wpa_bss *bss, struct wpa_ssid *ssid,
  829. u8 *wpa_ie, size_t *wpa_ie_len)
  830. {
  831. struct wpa_ie_data ie;
  832. int sel, proto;
  833. const u8 *bss_wpa, *bss_rsn;
  834. if (bss) {
  835. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  836. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  837. } else
  838. bss_wpa = bss_rsn = NULL;
  839. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  840. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  841. (ie.group_cipher & ssid->group_cipher) &&
  842. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  843. (ie.key_mgmt & ssid->key_mgmt)) {
  844. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  845. proto = WPA_PROTO_RSN;
  846. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  847. wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
  848. (ie.group_cipher & ssid->group_cipher) &&
  849. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  850. (ie.key_mgmt & ssid->key_mgmt)) {
  851. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  852. proto = WPA_PROTO_WPA;
  853. } else if (bss) {
  854. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  855. return -1;
  856. } else {
  857. if (ssid->proto & WPA_PROTO_RSN)
  858. proto = WPA_PROTO_RSN;
  859. else
  860. proto = WPA_PROTO_WPA;
  861. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  862. os_memset(&ie, 0, sizeof(ie));
  863. ie.group_cipher = ssid->group_cipher;
  864. ie.pairwise_cipher = ssid->pairwise_cipher;
  865. ie.key_mgmt = ssid->key_mgmt;
  866. #ifdef CONFIG_IEEE80211W
  867. ie.mgmt_group_cipher =
  868. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  869. WPA_CIPHER_AES_128_CMAC : 0;
  870. #endif /* CONFIG_IEEE80211W */
  871. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  872. "based on configuration");
  873. } else
  874. proto = ie.proto;
  875. }
  876. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  877. "pairwise %d key_mgmt %d proto %d",
  878. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  879. #ifdef CONFIG_IEEE80211W
  880. if (ssid->ieee80211w) {
  881. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  882. ie.mgmt_group_cipher);
  883. }
  884. #endif /* CONFIG_IEEE80211W */
  885. wpa_s->wpa_proto = proto;
  886. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  887. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  888. !!(ssid->proto & WPA_PROTO_RSN));
  889. if (bss || !wpa_s->ap_ies_from_associnfo) {
  890. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  891. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  892. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  893. bss_rsn ? 2 + bss_rsn[1] : 0))
  894. return -1;
  895. }
  896. sel = ie.group_cipher & ssid->group_cipher;
  897. if (sel & WPA_CIPHER_CCMP) {
  898. wpa_s->group_cipher = WPA_CIPHER_CCMP;
  899. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
  900. } else if (sel & WPA_CIPHER_GCMP) {
  901. wpa_s->group_cipher = WPA_CIPHER_GCMP;
  902. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK GCMP");
  903. } else if (sel & WPA_CIPHER_TKIP) {
  904. wpa_s->group_cipher = WPA_CIPHER_TKIP;
  905. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
  906. } else if (sel & WPA_CIPHER_WEP104) {
  907. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  908. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
  909. } else if (sel & WPA_CIPHER_WEP40) {
  910. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  911. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
  912. } else {
  913. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  914. "cipher");
  915. return -1;
  916. }
  917. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  918. if (sel & WPA_CIPHER_CCMP) {
  919. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  920. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
  921. } else if (sel & WPA_CIPHER_GCMP) {
  922. wpa_s->pairwise_cipher = WPA_CIPHER_GCMP;
  923. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK GCMP");
  924. } else if (sel & WPA_CIPHER_TKIP) {
  925. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  926. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
  927. } else if (sel & WPA_CIPHER_NONE) {
  928. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  929. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
  930. } else {
  931. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  932. "cipher");
  933. return -1;
  934. }
  935. sel = ie.key_mgmt & ssid->key_mgmt;
  936. if (0) {
  937. #ifdef CONFIG_IEEE80211R
  938. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  939. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  940. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  941. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  942. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  943. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  944. #endif /* CONFIG_IEEE80211R */
  945. #ifdef CONFIG_IEEE80211W
  946. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  947. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  948. wpa_dbg(wpa_s, MSG_DEBUG,
  949. "WPA: using KEY_MGMT 802.1X with SHA256");
  950. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  951. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  952. wpa_dbg(wpa_s, MSG_DEBUG,
  953. "WPA: using KEY_MGMT PSK with SHA256");
  954. #endif /* CONFIG_IEEE80211W */
  955. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  956. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  957. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  958. } else if (sel & WPA_KEY_MGMT_PSK) {
  959. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  960. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  961. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  962. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  963. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  964. } else {
  965. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  966. "authenticated key management type");
  967. return -1;
  968. }
  969. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  970. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  971. wpa_s->pairwise_cipher);
  972. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  973. #ifdef CONFIG_IEEE80211W
  974. sel = ie.mgmt_group_cipher;
  975. if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  976. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  977. sel = 0;
  978. if (sel & WPA_CIPHER_AES_128_CMAC) {
  979. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  980. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  981. "AES-128-CMAC");
  982. } else {
  983. wpa_s->mgmt_group_cipher = 0;
  984. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  985. }
  986. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  987. wpa_s->mgmt_group_cipher);
  988. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
  989. #endif /* CONFIG_IEEE80211W */
  990. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  991. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  992. return -1;
  993. }
  994. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
  995. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
  996. #ifndef CONFIG_NO_PBKDF2
  997. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  998. ssid->passphrase) {
  999. u8 psk[PMK_LEN];
  1000. pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
  1001. 4096, psk, PMK_LEN);
  1002. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1003. psk, PMK_LEN);
  1004. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  1005. }
  1006. #endif /* CONFIG_NO_PBKDF2 */
  1007. #ifdef CONFIG_EXT_PASSWORD
  1008. if (ssid->ext_psk) {
  1009. struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
  1010. ssid->ext_psk);
  1011. char pw_str[64 + 1];
  1012. u8 psk[PMK_LEN];
  1013. if (pw == NULL) {
  1014. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
  1015. "found from external storage");
  1016. return -1;
  1017. }
  1018. if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
  1019. wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
  1020. "PSK length %d in external storage",
  1021. (int) wpabuf_len(pw));
  1022. ext_password_free(pw);
  1023. return -1;
  1024. }
  1025. os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
  1026. pw_str[wpabuf_len(pw)] = '\0';
  1027. #ifndef CONFIG_NO_PBKDF2
  1028. if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
  1029. {
  1030. pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
  1031. 4096, psk, PMK_LEN);
  1032. os_memset(pw_str, 0, sizeof(pw_str));
  1033. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
  1034. "external passphrase)",
  1035. psk, PMK_LEN);
  1036. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  1037. } else
  1038. #endif /* CONFIG_NO_PBKDF2 */
  1039. if (wpabuf_len(pw) == 2 * PMK_LEN) {
  1040. if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
  1041. wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
  1042. "Invalid PSK hex string");
  1043. os_memset(pw_str, 0, sizeof(pw_str));
  1044. ext_password_free(pw);
  1045. return -1;
  1046. }
  1047. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  1048. } else {
  1049. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
  1050. "PSK available");
  1051. os_memset(pw_str, 0, sizeof(pw_str));
  1052. ext_password_free(pw);
  1053. return -1;
  1054. }
  1055. os_memset(pw_str, 0, sizeof(pw_str));
  1056. ext_password_free(pw);
  1057. }
  1058. #endif /* CONFIG_EXT_PASSWORD */
  1059. } else
  1060. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  1061. return 0;
  1062. }
  1063. /**
  1064. * wpa_supplicant_associate - Request association
  1065. * @wpa_s: Pointer to wpa_supplicant data
  1066. * @bss: Scan results for the selected BSS, or %NULL if not available
  1067. * @ssid: Configuration data for the selected network
  1068. *
  1069. * This function is used to request %wpa_supplicant to associate with a BSS.
  1070. */
  1071. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  1072. struct wpa_bss *bss, struct wpa_ssid *ssid)
  1073. {
  1074. u8 wpa_ie[200];
  1075. size_t wpa_ie_len;
  1076. int use_crypt, ret, i, bssid_changed;
  1077. int algs = WPA_AUTH_ALG_OPEN;
  1078. enum wpa_cipher cipher_pairwise, cipher_group;
  1079. struct wpa_driver_associate_params params;
  1080. int wep_keys_set = 0;
  1081. struct wpa_driver_capa capa;
  1082. int assoc_failed = 0;
  1083. struct wpa_ssid *old_ssid;
  1084. #ifdef CONFIG_HT_OVERRIDES
  1085. struct ieee80211_ht_capabilities htcaps;
  1086. struct ieee80211_ht_capabilities htcaps_mask;
  1087. #endif /* CONFIG_HT_OVERRIDES */
  1088. #ifdef CONFIG_IBSS_RSN
  1089. ibss_rsn_deinit(wpa_s->ibss_rsn);
  1090. wpa_s->ibss_rsn = NULL;
  1091. #endif /* CONFIG_IBSS_RSN */
  1092. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  1093. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1094. #ifdef CONFIG_AP
  1095. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  1096. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  1097. "mode");
  1098. return;
  1099. }
  1100. if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
  1101. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1102. return;
  1103. }
  1104. wpa_s->current_bss = bss;
  1105. #else /* CONFIG_AP */
  1106. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  1107. "the build");
  1108. #endif /* CONFIG_AP */
  1109. return;
  1110. }
  1111. #ifdef CONFIG_TDLS
  1112. if (bss)
  1113. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  1114. bss->ie_len);
  1115. #endif /* CONFIG_TDLS */
  1116. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1117. ssid->mode == IEEE80211_MODE_INFRA) {
  1118. sme_authenticate(wpa_s, bss, ssid);
  1119. return;
  1120. }
  1121. os_memset(&params, 0, sizeof(params));
  1122. wpa_s->reassociate = 0;
  1123. if (bss && !wpas_driver_bss_selection(wpa_s)) {
  1124. #ifdef CONFIG_IEEE80211R
  1125. const u8 *ie, *md = NULL;
  1126. #endif /* CONFIG_IEEE80211R */
  1127. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1128. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1129. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1130. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1131. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1132. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1133. if (bssid_changed)
  1134. wpas_notify_bssid_changed(wpa_s);
  1135. #ifdef CONFIG_IEEE80211R
  1136. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1137. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1138. md = ie + 2;
  1139. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1140. if (md) {
  1141. /* Prepare for the next transition */
  1142. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1143. }
  1144. #endif /* CONFIG_IEEE80211R */
  1145. #ifdef CONFIG_WPS
  1146. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1147. wpa_s->conf->ap_scan == 2 &&
  1148. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1149. /* Use ap_scan==1 style network selection to find the network
  1150. */
  1151. wpa_s->scan_req = 2;
  1152. wpa_s->reassociate = 1;
  1153. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1154. return;
  1155. #endif /* CONFIG_WPS */
  1156. } else {
  1157. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1158. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1159. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1160. }
  1161. wpa_supplicant_cancel_sched_scan(wpa_s);
  1162. wpa_supplicant_cancel_scan(wpa_s);
  1163. /* Starting new association, so clear the possibly used WPA IE from the
  1164. * previous association. */
  1165. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1166. #ifdef IEEE8021X_EAPOL
  1167. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1168. if (ssid->leap) {
  1169. if (ssid->non_leap == 0)
  1170. algs = WPA_AUTH_ALG_LEAP;
  1171. else
  1172. algs |= WPA_AUTH_ALG_LEAP;
  1173. }
  1174. }
  1175. #endif /* IEEE8021X_EAPOL */
  1176. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1177. if (ssid->auth_alg) {
  1178. algs = ssid->auth_alg;
  1179. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1180. "0x%x", algs);
  1181. }
  1182. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1183. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1184. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  1185. int try_opportunistic;
  1186. try_opportunistic = ssid->proactive_key_caching &&
  1187. (ssid->proto & WPA_PROTO_RSN);
  1188. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1189. wpa_s->current_ssid,
  1190. try_opportunistic) == 0)
  1191. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  1192. wpa_ie_len = sizeof(wpa_ie);
  1193. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1194. wpa_ie, &wpa_ie_len)) {
  1195. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1196. "key management and encryption suites");
  1197. return;
  1198. }
  1199. } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
  1200. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
  1201. /*
  1202. * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
  1203. * use non-WPA since the scan results did not indicate that the
  1204. * AP is using WPA or WPA2.
  1205. */
  1206. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1207. wpa_ie_len = 0;
  1208. wpa_s->wpa_proto = 0;
  1209. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  1210. wpa_ie_len = sizeof(wpa_ie);
  1211. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1212. wpa_ie, &wpa_ie_len)) {
  1213. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1214. "key management and encryption suites (no "
  1215. "scan results)");
  1216. return;
  1217. }
  1218. #ifdef CONFIG_WPS
  1219. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1220. struct wpabuf *wps_ie;
  1221. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1222. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1223. wpa_ie_len = wpabuf_len(wps_ie);
  1224. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1225. } else
  1226. wpa_ie_len = 0;
  1227. wpabuf_free(wps_ie);
  1228. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1229. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1230. params.wps = WPS_MODE_PRIVACY;
  1231. else
  1232. params.wps = WPS_MODE_OPEN;
  1233. wpa_s->wpa_proto = 0;
  1234. #endif /* CONFIG_WPS */
  1235. } else {
  1236. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1237. wpa_ie_len = 0;
  1238. wpa_s->wpa_proto = 0;
  1239. }
  1240. #ifdef CONFIG_P2P
  1241. if (wpa_s->global->p2p) {
  1242. u8 *pos;
  1243. size_t len;
  1244. int res;
  1245. pos = wpa_ie + wpa_ie_len;
  1246. len = sizeof(wpa_ie) - wpa_ie_len;
  1247. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  1248. ssid->p2p_group);
  1249. if (res >= 0)
  1250. wpa_ie_len += res;
  1251. }
  1252. wpa_s->cross_connect_disallowed = 0;
  1253. if (bss) {
  1254. struct wpabuf *p2p;
  1255. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1256. if (p2p) {
  1257. wpa_s->cross_connect_disallowed =
  1258. p2p_get_cross_connect_disallowed(p2p);
  1259. wpabuf_free(p2p);
  1260. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1261. "connection",
  1262. wpa_s->cross_connect_disallowed ?
  1263. "disallows" : "allows");
  1264. }
  1265. }
  1266. #endif /* CONFIG_P2P */
  1267. #ifdef CONFIG_HS20
  1268. if (wpa_s->conf->hs20) {
  1269. struct wpabuf *hs20;
  1270. hs20 = wpabuf_alloc(20);
  1271. if (hs20) {
  1272. wpas_hs20_add_indication(hs20);
  1273. os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(hs20),
  1274. wpabuf_len(hs20));
  1275. wpa_ie_len += wpabuf_len(hs20);
  1276. wpabuf_free(hs20);
  1277. }
  1278. }
  1279. #endif /* CONFIG_HS20 */
  1280. #ifdef CONFIG_INTERWORKING
  1281. if (wpa_s->conf->interworking) {
  1282. u8 *pos = wpa_ie;
  1283. if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  1284. pos += 2 + pos[1];
  1285. os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
  1286. wpa_ie_len += 6;
  1287. *pos++ = WLAN_EID_EXT_CAPAB;
  1288. *pos++ = 4;
  1289. *pos++ = 0x00;
  1290. *pos++ = 0x00;
  1291. *pos++ = 0x00;
  1292. *pos++ = 0x80; /* Bit 31 - Interworking */
  1293. }
  1294. #endif /* CONFIG_INTERWORKING */
  1295. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1296. use_crypt = 1;
  1297. cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
  1298. cipher_group = cipher_suite2driver(wpa_s->group_cipher);
  1299. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1300. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1301. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  1302. use_crypt = 0;
  1303. if (wpa_set_wep_keys(wpa_s, ssid)) {
  1304. use_crypt = 1;
  1305. wep_keys_set = 1;
  1306. }
  1307. }
  1308. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  1309. use_crypt = 0;
  1310. #ifdef IEEE8021X_EAPOL
  1311. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1312. if ((ssid->eapol_flags &
  1313. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  1314. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  1315. !wep_keys_set) {
  1316. use_crypt = 0;
  1317. } else {
  1318. /* Assume that dynamic WEP-104 keys will be used and
  1319. * set cipher suites in order for drivers to expect
  1320. * encryption. */
  1321. cipher_pairwise = cipher_group = CIPHER_WEP104;
  1322. }
  1323. }
  1324. #endif /* IEEE8021X_EAPOL */
  1325. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1326. /* Set the key before (and later after) association */
  1327. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1328. }
  1329. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1330. if (bss) {
  1331. params.ssid = bss->ssid;
  1332. params.ssid_len = bss->ssid_len;
  1333. if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
  1334. wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
  1335. MACSTR " freq=%u MHz based on scan results "
  1336. "(bssid_set=%d)",
  1337. MAC2STR(bss->bssid), bss->freq,
  1338. ssid->bssid_set);
  1339. params.bssid = bss->bssid;
  1340. params.freq = bss->freq;
  1341. }
  1342. } else {
  1343. params.ssid = ssid->ssid;
  1344. params.ssid_len = ssid->ssid_len;
  1345. }
  1346. if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
  1347. wpa_s->conf->ap_scan == 2) {
  1348. params.bssid = ssid->bssid;
  1349. params.fixed_bssid = 1;
  1350. }
  1351. if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
  1352. params.freq == 0)
  1353. params.freq = ssid->frequency; /* Initial channel for IBSS */
  1354. params.wpa_ie = wpa_ie;
  1355. params.wpa_ie_len = wpa_ie_len;
  1356. params.pairwise_suite = cipher_pairwise;
  1357. params.group_suite = cipher_group;
  1358. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  1359. params.wpa_proto = wpa_s->wpa_proto;
  1360. params.auth_alg = algs;
  1361. params.mode = ssid->mode;
  1362. params.bg_scan_period = ssid->bg_scan_period;
  1363. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1364. if (ssid->wep_key_len[i])
  1365. params.wep_key[i] = ssid->wep_key[i];
  1366. params.wep_key_len[i] = ssid->wep_key_len[i];
  1367. }
  1368. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1369. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1370. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  1371. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  1372. params.passphrase = ssid->passphrase;
  1373. if (ssid->psk_set)
  1374. params.psk = ssid->psk;
  1375. }
  1376. params.drop_unencrypted = use_crypt;
  1377. #ifdef CONFIG_IEEE80211W
  1378. params.mgmt_frame_protection = ssid->ieee80211w;
  1379. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  1380. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1381. struct wpa_ie_data ie;
  1382. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1383. ie.capabilities &
  1384. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1385. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1386. "MFP: require MFP");
  1387. params.mgmt_frame_protection =
  1388. MGMT_FRAME_PROTECTION_REQUIRED;
  1389. }
  1390. }
  1391. #endif /* CONFIG_IEEE80211W */
  1392. params.p2p = ssid->p2p_group;
  1393. if (wpa_s->parent->set_sta_uapsd)
  1394. params.uapsd = wpa_s->parent->sta_uapsd;
  1395. else
  1396. params.uapsd = -1;
  1397. #ifdef CONFIG_HT_OVERRIDES
  1398. os_memset(&htcaps, 0, sizeof(htcaps));
  1399. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  1400. params.htcaps = (u8 *) &htcaps;
  1401. params.htcaps_mask = (u8 *) &htcaps_mask;
  1402. wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
  1403. #endif /* CONFIG_HT_OVERRIDES */
  1404. ret = wpa_drv_associate(wpa_s, &params);
  1405. if (ret < 0) {
  1406. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1407. "failed");
  1408. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1409. /*
  1410. * The driver is known to mean what is saying, so we
  1411. * can stop right here; the association will not
  1412. * succeed.
  1413. */
  1414. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1415. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1416. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1417. return;
  1418. }
  1419. /* try to continue anyway; new association will be tried again
  1420. * after timeout */
  1421. assoc_failed = 1;
  1422. }
  1423. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1424. /* Set the key after the association just in case association
  1425. * cleared the previously configured key. */
  1426. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1427. /* No need to timeout authentication since there is no key
  1428. * management. */
  1429. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1430. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1431. #ifdef CONFIG_IBSS_RSN
  1432. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1433. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1434. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1435. /*
  1436. * RSN IBSS authentication is per-STA and we can disable the
  1437. * per-BSSID authentication.
  1438. */
  1439. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1440. #endif /* CONFIG_IBSS_RSN */
  1441. } else {
  1442. /* Timeout for IEEE 802.11 authentication and association */
  1443. int timeout = 60;
  1444. if (assoc_failed) {
  1445. /* give IBSS a bit more time */
  1446. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1447. } else if (wpa_s->conf->ap_scan == 1) {
  1448. /* give IBSS a bit more time */
  1449. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1450. }
  1451. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1452. }
  1453. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1454. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1455. /* Set static WEP keys again */
  1456. wpa_set_wep_keys(wpa_s, ssid);
  1457. }
  1458. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1459. /*
  1460. * Do not allow EAP session resumption between different
  1461. * network configurations.
  1462. */
  1463. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1464. }
  1465. old_ssid = wpa_s->current_ssid;
  1466. wpa_s->current_ssid = ssid;
  1467. wpa_s->current_bss = bss;
  1468. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1469. wpa_supplicant_initiate_eapol(wpa_s);
  1470. if (old_ssid != wpa_s->current_ssid)
  1471. wpas_notify_network_changed(wpa_s);
  1472. }
  1473. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1474. const u8 *addr)
  1475. {
  1476. struct wpa_ssid *old_ssid;
  1477. wpa_clear_keys(wpa_s, addr);
  1478. old_ssid = wpa_s->current_ssid;
  1479. wpa_supplicant_mark_disassoc(wpa_s);
  1480. wpa_sm_set_config(wpa_s->wpa, NULL);
  1481. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1482. if (old_ssid != wpa_s->current_ssid)
  1483. wpas_notify_network_changed(wpa_s);
  1484. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1485. }
  1486. /**
  1487. * wpa_supplicant_disassociate - Disassociate the current connection
  1488. * @wpa_s: Pointer to wpa_supplicant data
  1489. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1490. *
  1491. * This function is used to request %wpa_supplicant to disassociate with the
  1492. * current AP.
  1493. */
  1494. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1495. int reason_code)
  1496. {
  1497. u8 *addr = NULL;
  1498. union wpa_event_data event;
  1499. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1500. wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
  1501. addr = wpa_s->bssid;
  1502. os_memset(&event, 0, sizeof(event));
  1503. event.disassoc_info.reason_code = (u16) reason_code;
  1504. event.disassoc_info.locally_generated = 1;
  1505. wpa_supplicant_event(wpa_s, EVENT_DISASSOC, &event);
  1506. }
  1507. wpa_supplicant_clear_connection(wpa_s, addr);
  1508. }
  1509. /**
  1510. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1511. * @wpa_s: Pointer to wpa_supplicant data
  1512. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1513. *
  1514. * This function is used to request %wpa_supplicant to deauthenticate from the
  1515. * current AP.
  1516. */
  1517. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1518. int reason_code)
  1519. {
  1520. u8 *addr = NULL;
  1521. union wpa_event_data event;
  1522. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1523. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
  1524. addr = wpa_s->bssid;
  1525. os_memset(&event, 0, sizeof(event));
  1526. event.deauth_info.reason_code = (u16) reason_code;
  1527. event.deauth_info.locally_generated = 1;
  1528. wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
  1529. }
  1530. wpa_supplicant_clear_connection(wpa_s, addr);
  1531. }
  1532. /**
  1533. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1534. * @wpa_s: wpa_supplicant structure for a network interface
  1535. * @ssid: wpa_ssid structure for a configured network or %NULL
  1536. *
  1537. * Enables the specified network or all networks if no network specified.
  1538. */
  1539. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1540. struct wpa_ssid *ssid)
  1541. {
  1542. struct wpa_ssid *other_ssid;
  1543. int was_disabled;
  1544. if (ssid == NULL) {
  1545. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1546. other_ssid = other_ssid->next) {
  1547. if (other_ssid->disabled == 2)
  1548. continue; /* do not change persistent P2P group
  1549. * data */
  1550. if (other_ssid == wpa_s->current_ssid &&
  1551. other_ssid->disabled)
  1552. wpa_s->reassociate = 1;
  1553. was_disabled = other_ssid->disabled;
  1554. other_ssid->disabled = 0;
  1555. if (was_disabled)
  1556. wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
  1557. if (was_disabled != other_ssid->disabled)
  1558. wpas_notify_network_enabled_changed(
  1559. wpa_s, other_ssid);
  1560. }
  1561. if (wpa_s->reassociate)
  1562. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1563. } else if (ssid->disabled && ssid->disabled != 2) {
  1564. if (wpa_s->current_ssid == NULL) {
  1565. /*
  1566. * Try to reassociate since there is no current
  1567. * configuration and a new network was made available.
  1568. */
  1569. wpa_s->reassociate = 1;
  1570. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1571. }
  1572. was_disabled = ssid->disabled;
  1573. ssid->disabled = 0;
  1574. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  1575. if (was_disabled != ssid->disabled)
  1576. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1577. }
  1578. }
  1579. /**
  1580. * wpa_supplicant_disable_network - Mark a configured network as disabled
  1581. * @wpa_s: wpa_supplicant structure for a network interface
  1582. * @ssid: wpa_ssid structure for a configured network or %NULL
  1583. *
  1584. * Disables the specified network or all networks if no network specified.
  1585. */
  1586. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  1587. struct wpa_ssid *ssid)
  1588. {
  1589. struct wpa_ssid *other_ssid;
  1590. int was_disabled;
  1591. if (ssid == NULL) {
  1592. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1593. other_ssid = other_ssid->next) {
  1594. was_disabled = other_ssid->disabled;
  1595. if (was_disabled == 2)
  1596. continue; /* do not change persistent P2P group
  1597. * data */
  1598. other_ssid->disabled = 1;
  1599. if (was_disabled != other_ssid->disabled)
  1600. wpas_notify_network_enabled_changed(
  1601. wpa_s, other_ssid);
  1602. }
  1603. if (wpa_s->current_ssid)
  1604. wpa_supplicant_disassociate(
  1605. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1606. } else if (ssid->disabled != 2) {
  1607. if (ssid == wpa_s->current_ssid)
  1608. wpa_supplicant_disassociate(
  1609. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1610. was_disabled = ssid->disabled;
  1611. ssid->disabled = 1;
  1612. if (was_disabled != ssid->disabled)
  1613. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1614. }
  1615. }
  1616. /**
  1617. * wpa_supplicant_select_network - Attempt association with a network
  1618. * @wpa_s: wpa_supplicant structure for a network interface
  1619. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  1620. */
  1621. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  1622. struct wpa_ssid *ssid)
  1623. {
  1624. struct wpa_ssid *other_ssid;
  1625. int disconnected = 0;
  1626. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
  1627. wpa_supplicant_disassociate(
  1628. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1629. disconnected = 1;
  1630. }
  1631. if (ssid)
  1632. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  1633. /*
  1634. * Mark all other networks disabled or mark all networks enabled if no
  1635. * network specified.
  1636. */
  1637. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1638. other_ssid = other_ssid->next) {
  1639. int was_disabled = other_ssid->disabled;
  1640. if (was_disabled == 2)
  1641. continue; /* do not change persistent P2P group data */
  1642. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  1643. if (was_disabled && !other_ssid->disabled)
  1644. wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
  1645. if (was_disabled != other_ssid->disabled)
  1646. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  1647. }
  1648. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
  1649. /* We are already associated with the selected network */
  1650. wpa_printf(MSG_DEBUG, "Already associated with the "
  1651. "selected network - do nothing");
  1652. return;
  1653. }
  1654. if (ssid)
  1655. wpa_s->current_ssid = ssid;
  1656. wpa_s->connect_without_scan = NULL;
  1657. wpa_s->disconnected = 0;
  1658. wpa_s->reassociate = 1;
  1659. wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
  1660. if (ssid)
  1661. wpas_notify_network_selected(wpa_s, ssid);
  1662. }
  1663. /**
  1664. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  1665. * @wpa_s: wpa_supplicant structure for a network interface
  1666. * @ap_scan: AP scan mode
  1667. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  1668. *
  1669. */
  1670. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  1671. {
  1672. int old_ap_scan;
  1673. if (ap_scan < 0 || ap_scan > 2)
  1674. return -1;
  1675. #ifdef ANDROID
  1676. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  1677. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  1678. wpa_s->wpa_state < WPA_COMPLETED) {
  1679. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  1680. "associating", wpa_s->conf->ap_scan, ap_scan);
  1681. return 0;
  1682. }
  1683. #endif /* ANDROID */
  1684. old_ap_scan = wpa_s->conf->ap_scan;
  1685. wpa_s->conf->ap_scan = ap_scan;
  1686. if (old_ap_scan != wpa_s->conf->ap_scan)
  1687. wpas_notify_ap_scan_changed(wpa_s);
  1688. return 0;
  1689. }
  1690. /**
  1691. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  1692. * @wpa_s: wpa_supplicant structure for a network interface
  1693. * @expire_age: Expiration age in seconds
  1694. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  1695. *
  1696. */
  1697. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  1698. unsigned int bss_expire_age)
  1699. {
  1700. if (bss_expire_age < 10) {
  1701. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  1702. bss_expire_age);
  1703. return -1;
  1704. }
  1705. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  1706. bss_expire_age);
  1707. wpa_s->conf->bss_expiration_age = bss_expire_age;
  1708. return 0;
  1709. }
  1710. /**
  1711. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  1712. * @wpa_s: wpa_supplicant structure for a network interface
  1713. * @expire_count: number of scans after which an unseen BSS is reclaimed
  1714. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  1715. *
  1716. */
  1717. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  1718. unsigned int bss_expire_count)
  1719. {
  1720. if (bss_expire_count < 1) {
  1721. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  1722. bss_expire_count);
  1723. return -1;
  1724. }
  1725. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  1726. bss_expire_count);
  1727. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  1728. return 0;
  1729. }
  1730. /**
  1731. * wpa_supplicant_set_scan_interval - Set scan interval
  1732. * @wpa_s: wpa_supplicant structure for a network interface
  1733. * @scan_interval: scan interval in seconds
  1734. * Returns: 0 if succeed or -1 if scan_interval has an invalid value
  1735. *
  1736. */
  1737. int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
  1738. int scan_interval)
  1739. {
  1740. if (scan_interval < 0) {
  1741. wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
  1742. scan_interval);
  1743. return -1;
  1744. }
  1745. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
  1746. scan_interval);
  1747. wpa_s->scan_interval = scan_interval;
  1748. return 0;
  1749. }
  1750. /**
  1751. * wpa_supplicant_set_debug_params - Set global debug params
  1752. * @global: wpa_global structure
  1753. * @debug_level: debug level
  1754. * @debug_timestamp: determines if show timestamp in debug data
  1755. * @debug_show_keys: determines if show keys in debug data
  1756. * Returns: 0 if succeed or -1 if debug_level has wrong value
  1757. */
  1758. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  1759. int debug_timestamp, int debug_show_keys)
  1760. {
  1761. int old_level, old_timestamp, old_show_keys;
  1762. /* check for allowed debuglevels */
  1763. if (debug_level != MSG_EXCESSIVE &&
  1764. debug_level != MSG_MSGDUMP &&
  1765. debug_level != MSG_DEBUG &&
  1766. debug_level != MSG_INFO &&
  1767. debug_level != MSG_WARNING &&
  1768. debug_level != MSG_ERROR)
  1769. return -1;
  1770. old_level = wpa_debug_level;
  1771. old_timestamp = wpa_debug_timestamp;
  1772. old_show_keys = wpa_debug_show_keys;
  1773. wpa_debug_level = debug_level;
  1774. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  1775. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  1776. if (wpa_debug_level != old_level)
  1777. wpas_notify_debug_level_changed(global);
  1778. if (wpa_debug_timestamp != old_timestamp)
  1779. wpas_notify_debug_timestamp_changed(global);
  1780. if (wpa_debug_show_keys != old_show_keys)
  1781. wpas_notify_debug_show_keys_changed(global);
  1782. return 0;
  1783. }
  1784. /**
  1785. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1786. * @wpa_s: Pointer to wpa_supplicant data
  1787. * Returns: A pointer to the current network structure or %NULL on failure
  1788. */
  1789. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1790. {
  1791. struct wpa_ssid *entry;
  1792. u8 ssid[MAX_SSID_LEN];
  1793. int res;
  1794. size_t ssid_len;
  1795. u8 bssid[ETH_ALEN];
  1796. int wired;
  1797. res = wpa_drv_get_ssid(wpa_s, ssid);
  1798. if (res < 0) {
  1799. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1800. "driver");
  1801. return NULL;
  1802. }
  1803. ssid_len = res;
  1804. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1805. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  1806. "driver");
  1807. return NULL;
  1808. }
  1809. wired = wpa_s->conf->ap_scan == 0 &&
  1810. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  1811. entry = wpa_s->conf->ssid;
  1812. while (entry) {
  1813. if (!wpas_network_disabled(wpa_s, entry) &&
  1814. ((ssid_len == entry->ssid_len &&
  1815. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1816. (!entry->bssid_set ||
  1817. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1818. return entry;
  1819. #ifdef CONFIG_WPS
  1820. if (!wpas_network_disabled(wpa_s, entry) &&
  1821. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  1822. (entry->ssid == NULL || entry->ssid_len == 0) &&
  1823. (!entry->bssid_set ||
  1824. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1825. return entry;
  1826. #endif /* CONFIG_WPS */
  1827. if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
  1828. entry->ssid_len == 0 &&
  1829. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  1830. return entry;
  1831. entry = entry->next;
  1832. }
  1833. return NULL;
  1834. }
  1835. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  1836. {
  1837. struct wpa_global *global = wpa_s->global;
  1838. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  1839. global->drv_priv[i] = wpa_drivers[i]->global_init();
  1840. if (global->drv_priv[i] == NULL) {
  1841. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  1842. "'%s'", wpa_drivers[i]->name);
  1843. return -1;
  1844. }
  1845. }
  1846. wpa_s->driver = wpa_drivers[i];
  1847. wpa_s->global_drv_priv = global->drv_priv[i];
  1848. return 0;
  1849. }
  1850. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1851. const char *name)
  1852. {
  1853. int i;
  1854. size_t len;
  1855. const char *pos, *driver = name;
  1856. if (wpa_s == NULL)
  1857. return -1;
  1858. if (wpa_drivers[0] == NULL) {
  1859. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  1860. "wpa_supplicant");
  1861. return -1;
  1862. }
  1863. if (name == NULL) {
  1864. /* default to first driver in the list */
  1865. return select_driver(wpa_s, 0);
  1866. }
  1867. do {
  1868. pos = os_strchr(driver, ',');
  1869. if (pos)
  1870. len = pos - driver;
  1871. else
  1872. len = os_strlen(driver);
  1873. for (i = 0; wpa_drivers[i]; i++) {
  1874. if (os_strlen(wpa_drivers[i]->name) == len &&
  1875. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  1876. 0) {
  1877. /* First driver that succeeds wins */
  1878. if (select_driver(wpa_s, i) == 0)
  1879. return 0;
  1880. }
  1881. }
  1882. driver = pos + 1;
  1883. } while (pos);
  1884. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  1885. return -1;
  1886. }
  1887. /**
  1888. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  1889. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  1890. * with struct wpa_driver_ops::init()
  1891. * @src_addr: Source address of the EAPOL frame
  1892. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  1893. * @len: Length of the EAPOL data
  1894. *
  1895. * This function is called for each received EAPOL frame. Most driver
  1896. * interfaces rely on more generic OS mechanism for receiving frames through
  1897. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  1898. * take care of received EAPOL frames and deliver them to the core supplicant
  1899. * code by calling this function.
  1900. */
  1901. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1902. const u8 *buf, size_t len)
  1903. {
  1904. struct wpa_supplicant *wpa_s = ctx;
  1905. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1906. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1907. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  1908. /*
  1909. * There is possible race condition between receiving the
  1910. * association event and the EAPOL frame since they are coming
  1911. * through different paths from the driver. In order to avoid
  1912. * issues in trying to process the EAPOL frame before receiving
  1913. * association information, lets queue it for processing until
  1914. * the association event is received.
  1915. */
  1916. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  1917. "of received EAPOL frame");
  1918. wpabuf_free(wpa_s->pending_eapol_rx);
  1919. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  1920. if (wpa_s->pending_eapol_rx) {
  1921. os_get_time(&wpa_s->pending_eapol_rx_time);
  1922. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  1923. ETH_ALEN);
  1924. }
  1925. return;
  1926. }
  1927. #ifdef CONFIG_AP
  1928. if (wpa_s->ap_iface) {
  1929. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  1930. return;
  1931. }
  1932. #endif /* CONFIG_AP */
  1933. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1934. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  1935. "no key management is configured");
  1936. return;
  1937. }
  1938. if (wpa_s->eapol_received == 0 &&
  1939. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  1940. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1941. wpa_s->wpa_state != WPA_COMPLETED) &&
  1942. (wpa_s->current_ssid == NULL ||
  1943. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  1944. /* Timeout for completing IEEE 802.1X and WPA authentication */
  1945. wpa_supplicant_req_auth_timeout(
  1946. wpa_s,
  1947. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1948. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  1949. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  1950. 70 : 10, 0);
  1951. }
  1952. wpa_s->eapol_received++;
  1953. if (wpa_s->countermeasures) {
  1954. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  1955. "EAPOL packet");
  1956. return;
  1957. }
  1958. #ifdef CONFIG_IBSS_RSN
  1959. if (wpa_s->current_ssid &&
  1960. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  1961. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  1962. return;
  1963. }
  1964. #endif /* CONFIG_IBSS_RSN */
  1965. /* Source address of the incoming EAPOL frame could be compared to the
  1966. * current BSSID. However, it is possible that a centralized
  1967. * Authenticator could be using another MAC address than the BSSID of
  1968. * an AP, so just allow any address to be used for now. The replies are
  1969. * still sent to the current BSSID (if available), though. */
  1970. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  1971. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  1972. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  1973. return;
  1974. wpa_drv_poll(wpa_s);
  1975. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  1976. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  1977. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1978. /*
  1979. * Set portValid = TRUE here since we are going to skip 4-way
  1980. * handshake processing which would normally set portValid. We
  1981. * need this to allow the EAPOL state machines to be completed
  1982. * without going through EAPOL-Key handshake.
  1983. */
  1984. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1985. }
  1986. }
  1987. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  1988. {
  1989. if (wpa_s->driver->send_eapol) {
  1990. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1991. if (addr)
  1992. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1993. } else if (!(wpa_s->drv_flags &
  1994. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  1995. l2_packet_deinit(wpa_s->l2);
  1996. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  1997. wpa_drv_get_mac_addr(wpa_s),
  1998. ETH_P_EAPOL,
  1999. wpa_supplicant_rx_eapol, wpa_s, 0);
  2000. if (wpa_s->l2 == NULL)
  2001. return -1;
  2002. } else {
  2003. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  2004. if (addr)
  2005. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  2006. }
  2007. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  2008. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  2009. return -1;
  2010. }
  2011. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  2012. MAC2STR(wpa_s->own_addr));
  2013. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  2014. return 0;
  2015. }
  2016. static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
  2017. const u8 *buf, size_t len)
  2018. {
  2019. struct wpa_supplicant *wpa_s = ctx;
  2020. const struct l2_ethhdr *eth;
  2021. if (len < sizeof(*eth))
  2022. return;
  2023. eth = (const struct l2_ethhdr *) buf;
  2024. if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
  2025. !(eth->h_dest[0] & 0x01)) {
  2026. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  2027. " (bridge - not for this interface - ignore)",
  2028. MAC2STR(src_addr), MAC2STR(eth->h_dest));
  2029. return;
  2030. }
  2031. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  2032. " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
  2033. wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
  2034. len - sizeof(*eth));
  2035. }
  2036. /**
  2037. * wpa_supplicant_driver_init - Initialize driver interface parameters
  2038. * @wpa_s: Pointer to wpa_supplicant data
  2039. * Returns: 0 on success, -1 on failure
  2040. *
  2041. * This function is called to initialize driver interface parameters.
  2042. * wpa_drv_init() must have been called before this function to initialize the
  2043. * driver interface.
  2044. */
  2045. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  2046. {
  2047. static int interface_count = 0;
  2048. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  2049. return -1;
  2050. if (wpa_s->bridge_ifname[0]) {
  2051. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  2052. "interface '%s'", wpa_s->bridge_ifname);
  2053. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  2054. wpa_s->own_addr,
  2055. ETH_P_EAPOL,
  2056. wpa_supplicant_rx_eapol_bridge,
  2057. wpa_s, 1);
  2058. if (wpa_s->l2_br == NULL) {
  2059. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  2060. "connection for the bridge interface '%s'",
  2061. wpa_s->bridge_ifname);
  2062. return -1;
  2063. }
  2064. }
  2065. wpa_clear_keys(wpa_s, NULL);
  2066. /* Make sure that TKIP countermeasures are not left enabled (could
  2067. * happen if wpa_supplicant is killed during countermeasures. */
  2068. wpa_drv_set_countermeasures(wpa_s, 0);
  2069. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  2070. wpa_drv_flush_pmkid(wpa_s);
  2071. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  2072. wpa_s->prev_scan_wildcard = 0;
  2073. if (wpa_supplicant_enabled_networks(wpa_s)) {
  2074. if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
  2075. 100000))
  2076. wpa_supplicant_req_scan(wpa_s, interface_count,
  2077. 100000);
  2078. interface_count++;
  2079. } else
  2080. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  2081. return 0;
  2082. }
  2083. static int wpa_supplicant_daemon(const char *pid_file)
  2084. {
  2085. wpa_printf(MSG_DEBUG, "Daemonize..");
  2086. return os_daemonize(pid_file);
  2087. }
  2088. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  2089. {
  2090. struct wpa_supplicant *wpa_s;
  2091. wpa_s = os_zalloc(sizeof(*wpa_s));
  2092. if (wpa_s == NULL)
  2093. return NULL;
  2094. wpa_s->scan_req = 1;
  2095. wpa_s->scan_interval = 5;
  2096. wpa_s->new_connection = 1;
  2097. wpa_s->parent = wpa_s;
  2098. wpa_s->sched_scanning = 0;
  2099. return wpa_s;
  2100. }
  2101. #ifdef CONFIG_HT_OVERRIDES
  2102. static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
  2103. struct ieee80211_ht_capabilities *htcaps,
  2104. struct ieee80211_ht_capabilities *htcaps_mask,
  2105. const char *ht_mcs)
  2106. {
  2107. /* parse ht_mcs into hex array */
  2108. int i;
  2109. const char *tmp = ht_mcs;
  2110. char *end = NULL;
  2111. /* If ht_mcs is null, do not set anything */
  2112. if (!ht_mcs)
  2113. return 0;
  2114. /* This is what we are setting in the kernel */
  2115. os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
  2116. wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
  2117. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  2118. errno = 0;
  2119. long v = strtol(tmp, &end, 16);
  2120. if (errno == 0) {
  2121. wpa_msg(wpa_s, MSG_DEBUG,
  2122. "htcap value[%i]: %ld end: %p tmp: %p",
  2123. i, v, end, tmp);
  2124. if (end == tmp)
  2125. break;
  2126. htcaps->supported_mcs_set[i] = v;
  2127. tmp = end;
  2128. } else {
  2129. wpa_msg(wpa_s, MSG_ERROR,
  2130. "Failed to parse ht-mcs: %s, error: %s\n",
  2131. ht_mcs, strerror(errno));
  2132. return -1;
  2133. }
  2134. }
  2135. /*
  2136. * If we were able to parse any values, then set mask for the MCS set.
  2137. */
  2138. if (i) {
  2139. os_memset(&htcaps_mask->supported_mcs_set, 0xff,
  2140. IEEE80211_HT_MCS_MASK_LEN - 1);
  2141. /* skip the 3 reserved bits */
  2142. htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
  2143. 0x1f;
  2144. }
  2145. return 0;
  2146. }
  2147. static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
  2148. struct ieee80211_ht_capabilities *htcaps,
  2149. struct ieee80211_ht_capabilities *htcaps_mask,
  2150. int disabled)
  2151. {
  2152. u16 msk;
  2153. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
  2154. if (disabled == -1)
  2155. return 0;
  2156. msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
  2157. htcaps_mask->ht_capabilities_info |= msk;
  2158. if (disabled)
  2159. htcaps->ht_capabilities_info &= msk;
  2160. else
  2161. htcaps->ht_capabilities_info |= msk;
  2162. return 0;
  2163. }
  2164. static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
  2165. struct ieee80211_ht_capabilities *htcaps,
  2166. struct ieee80211_ht_capabilities *htcaps_mask,
  2167. int factor)
  2168. {
  2169. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
  2170. if (factor == -1)
  2171. return 0;
  2172. if (factor < 0 || factor > 3) {
  2173. wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
  2174. "Must be 0-3 or -1", factor);
  2175. return -EINVAL;
  2176. }
  2177. htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
  2178. htcaps->a_mpdu_params &= ~0x3;
  2179. htcaps->a_mpdu_params |= factor & 0x3;
  2180. return 0;
  2181. }
  2182. static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
  2183. struct ieee80211_ht_capabilities *htcaps,
  2184. struct ieee80211_ht_capabilities *htcaps_mask,
  2185. int density)
  2186. {
  2187. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
  2188. if (density == -1)
  2189. return 0;
  2190. if (density < 0 || density > 7) {
  2191. wpa_msg(wpa_s, MSG_ERROR,
  2192. "ampdu_density: %d out of range. Must be 0-7 or -1.",
  2193. density);
  2194. return -EINVAL;
  2195. }
  2196. htcaps_mask->a_mpdu_params |= 0x1C;
  2197. htcaps->a_mpdu_params &= ~(0x1C);
  2198. htcaps->a_mpdu_params |= (density << 2) & 0x1C;
  2199. return 0;
  2200. }
  2201. static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
  2202. struct ieee80211_ht_capabilities *htcaps,
  2203. struct ieee80211_ht_capabilities *htcaps_mask,
  2204. int disabled)
  2205. {
  2206. /* Masking these out disables HT40 */
  2207. u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
  2208. HT_CAP_INFO_SHORT_GI40MHZ);
  2209. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
  2210. if (disabled)
  2211. htcaps->ht_capabilities_info &= ~msk;
  2212. else
  2213. htcaps->ht_capabilities_info |= msk;
  2214. htcaps_mask->ht_capabilities_info |= msk;
  2215. return 0;
  2216. }
  2217. void wpa_supplicant_apply_ht_overrides(
  2218. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2219. struct wpa_driver_associate_params *params)
  2220. {
  2221. struct ieee80211_ht_capabilities *htcaps;
  2222. struct ieee80211_ht_capabilities *htcaps_mask;
  2223. if (!ssid)
  2224. return;
  2225. params->disable_ht = ssid->disable_ht;
  2226. if (!params->htcaps || !params->htcaps_mask)
  2227. return;
  2228. htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
  2229. htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
  2230. wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
  2231. wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
  2232. ssid->disable_max_amsdu);
  2233. wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
  2234. wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
  2235. wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
  2236. }
  2237. #endif /* CONFIG_HT_OVERRIDES */
  2238. static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
  2239. {
  2240. #ifdef PCSC_FUNCS
  2241. size_t len;
  2242. if (!wpa_s->conf->pcsc_reader)
  2243. return 0;
  2244. wpa_s->scard = scard_init(SCARD_TRY_BOTH, wpa_s->conf->pcsc_reader);
  2245. if (!wpa_s->scard)
  2246. return 1;
  2247. if (wpa_s->conf->pcsc_pin &&
  2248. scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
  2249. scard_deinit(wpa_s->scard);
  2250. wpa_s->scard = NULL;
  2251. wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
  2252. return -1;
  2253. }
  2254. len = sizeof(wpa_s->imsi) - 1;
  2255. if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
  2256. scard_deinit(wpa_s->scard);
  2257. wpa_s->scard = NULL;
  2258. wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
  2259. return -1;
  2260. }
  2261. wpa_s->imsi[len] = '\0';
  2262. wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
  2263. wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
  2264. wpa_s->imsi, wpa_s->mnc_len);
  2265. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  2266. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  2267. #endif /* PCSC_FUNCS */
  2268. return 0;
  2269. }
  2270. int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
  2271. {
  2272. char *val, *pos;
  2273. ext_password_deinit(wpa_s->ext_pw);
  2274. wpa_s->ext_pw = NULL;
  2275. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
  2276. if (!wpa_s->conf->ext_password_backend)
  2277. return 0;
  2278. val = os_strdup(wpa_s->conf->ext_password_backend);
  2279. if (val == NULL)
  2280. return -1;
  2281. pos = os_strchr(val, ':');
  2282. if (pos)
  2283. *pos++ = '\0';
  2284. wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
  2285. wpa_s->ext_pw = ext_password_init(val, pos);
  2286. os_free(val);
  2287. if (wpa_s->ext_pw == NULL) {
  2288. wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
  2289. return -1;
  2290. }
  2291. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
  2292. return 0;
  2293. }
  2294. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  2295. struct wpa_interface *iface)
  2296. {
  2297. const char *ifname, *driver;
  2298. struct wpa_driver_capa capa;
  2299. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  2300. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  2301. iface->confname ? iface->confname : "N/A",
  2302. iface->driver ? iface->driver : "default",
  2303. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  2304. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  2305. if (iface->confname) {
  2306. #ifdef CONFIG_BACKEND_FILE
  2307. wpa_s->confname = os_rel2abs_path(iface->confname);
  2308. if (wpa_s->confname == NULL) {
  2309. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  2310. "for configuration file '%s'.",
  2311. iface->confname);
  2312. return -1;
  2313. }
  2314. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  2315. iface->confname, wpa_s->confname);
  2316. #else /* CONFIG_BACKEND_FILE */
  2317. wpa_s->confname = os_strdup(iface->confname);
  2318. #endif /* CONFIG_BACKEND_FILE */
  2319. wpa_s->conf = wpa_config_read(wpa_s->confname);
  2320. if (wpa_s->conf == NULL) {
  2321. wpa_printf(MSG_ERROR, "Failed to read or parse "
  2322. "configuration '%s'.", wpa_s->confname);
  2323. return -1;
  2324. }
  2325. /*
  2326. * Override ctrl_interface and driver_param if set on command
  2327. * line.
  2328. */
  2329. if (iface->ctrl_interface) {
  2330. os_free(wpa_s->conf->ctrl_interface);
  2331. wpa_s->conf->ctrl_interface =
  2332. os_strdup(iface->ctrl_interface);
  2333. }
  2334. if (iface->driver_param) {
  2335. os_free(wpa_s->conf->driver_param);
  2336. wpa_s->conf->driver_param =
  2337. os_strdup(iface->driver_param);
  2338. }
  2339. } else
  2340. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  2341. iface->driver_param);
  2342. if (wpa_s->conf == NULL) {
  2343. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  2344. return -1;
  2345. }
  2346. if (iface->ifname == NULL) {
  2347. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  2348. return -1;
  2349. }
  2350. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  2351. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  2352. iface->ifname);
  2353. return -1;
  2354. }
  2355. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  2356. if (iface->bridge_ifname) {
  2357. if (os_strlen(iface->bridge_ifname) >=
  2358. sizeof(wpa_s->bridge_ifname)) {
  2359. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  2360. "name '%s'.", iface->bridge_ifname);
  2361. return -1;
  2362. }
  2363. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  2364. sizeof(wpa_s->bridge_ifname));
  2365. }
  2366. /* RSNA Supplicant Key Management - INITIALIZE */
  2367. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  2368. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  2369. /* Initialize driver interface and register driver event handler before
  2370. * L2 receive handler so that association events are processed before
  2371. * EAPOL-Key packets if both become available for the same select()
  2372. * call. */
  2373. driver = iface->driver;
  2374. next_driver:
  2375. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  2376. return -1;
  2377. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  2378. if (wpa_s->drv_priv == NULL) {
  2379. const char *pos;
  2380. pos = driver ? os_strchr(driver, ',') : NULL;
  2381. if (pos) {
  2382. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  2383. "driver interface - try next driver wrapper");
  2384. driver = pos + 1;
  2385. goto next_driver;
  2386. }
  2387. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  2388. "interface");
  2389. return -1;
  2390. }
  2391. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  2392. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  2393. "driver_param '%s'", wpa_s->conf->driver_param);
  2394. return -1;
  2395. }
  2396. ifname = wpa_drv_get_ifname(wpa_s);
  2397. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  2398. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  2399. "interface name with '%s'", ifname);
  2400. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  2401. }
  2402. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  2403. return -1;
  2404. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  2405. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  2406. NULL);
  2407. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  2408. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  2409. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  2410. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  2411. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2412. "dot11RSNAConfigPMKLifetime");
  2413. return -1;
  2414. }
  2415. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  2416. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  2417. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  2418. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2419. "dot11RSNAConfigPMKReauthThreshold");
  2420. return -1;
  2421. }
  2422. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  2423. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  2424. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  2425. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2426. "dot11RSNAConfigSATimeout");
  2427. return -1;
  2428. }
  2429. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  2430. &wpa_s->hw.num_modes,
  2431. &wpa_s->hw.flags);
  2432. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  2433. wpa_s->drv_capa_known = 1;
  2434. wpa_s->drv_flags = capa.flags;
  2435. wpa_s->drv_enc = capa.enc;
  2436. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  2437. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  2438. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  2439. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  2440. wpa_s->max_match_sets = capa.max_match_sets;
  2441. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  2442. wpa_s->max_stations = capa.max_stations;
  2443. }
  2444. if (wpa_s->max_remain_on_chan == 0)
  2445. wpa_s->max_remain_on_chan = 1000;
  2446. if (wpa_supplicant_driver_init(wpa_s) < 0)
  2447. return -1;
  2448. #ifdef CONFIG_TDLS
  2449. if (wpa_tdls_init(wpa_s->wpa))
  2450. return -1;
  2451. #endif /* CONFIG_TDLS */
  2452. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  2453. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  2454. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  2455. return -1;
  2456. }
  2457. if (wpas_wps_init(wpa_s))
  2458. return -1;
  2459. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  2460. return -1;
  2461. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2462. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  2463. if (wpa_s->ctrl_iface == NULL) {
  2464. wpa_printf(MSG_ERROR,
  2465. "Failed to initialize control interface '%s'.\n"
  2466. "You may have another wpa_supplicant process "
  2467. "already running or the file was\n"
  2468. "left by an unclean termination of wpa_supplicant "
  2469. "in which case you will need\n"
  2470. "to manually remove this file before starting "
  2471. "wpa_supplicant again.\n",
  2472. wpa_s->conf->ctrl_interface);
  2473. return -1;
  2474. }
  2475. wpa_s->gas = gas_query_init(wpa_s);
  2476. if (wpa_s->gas == NULL) {
  2477. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  2478. return -1;
  2479. }
  2480. #ifdef CONFIG_P2P
  2481. if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  2482. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  2483. return -1;
  2484. }
  2485. #endif /* CONFIG_P2P */
  2486. if (wpa_bss_init(wpa_s) < 0)
  2487. return -1;
  2488. if (pcsc_reader_init(wpa_s) < 0)
  2489. return -1;
  2490. if (wpas_init_ext_pw(wpa_s) < 0)
  2491. return -1;
  2492. return 0;
  2493. }
  2494. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  2495. int notify, int terminate)
  2496. {
  2497. if (wpa_s->drv_priv) {
  2498. wpa_supplicant_deauthenticate(wpa_s,
  2499. WLAN_REASON_DEAUTH_LEAVING);
  2500. wpa_drv_set_countermeasures(wpa_s, 0);
  2501. wpa_clear_keys(wpa_s, NULL);
  2502. }
  2503. wpa_supplicant_cleanup(wpa_s);
  2504. #ifdef CONFIG_P2P
  2505. if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
  2506. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
  2507. "the management interface is being removed");
  2508. wpas_p2p_deinit_global(wpa_s->global);
  2509. }
  2510. #endif /* CONFIG_P2P */
  2511. if (wpa_s->drv_priv)
  2512. wpa_drv_deinit(wpa_s);
  2513. if (notify)
  2514. wpas_notify_iface_removed(wpa_s);
  2515. if (terminate)
  2516. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
  2517. if (wpa_s->ctrl_iface) {
  2518. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  2519. wpa_s->ctrl_iface = NULL;
  2520. }
  2521. if (wpa_s->conf != NULL) {
  2522. wpa_config_free(wpa_s->conf);
  2523. wpa_s->conf = NULL;
  2524. }
  2525. }
  2526. /**
  2527. * wpa_supplicant_add_iface - Add a new network interface
  2528. * @global: Pointer to global data from wpa_supplicant_init()
  2529. * @iface: Interface configuration options
  2530. * Returns: Pointer to the created interface or %NULL on failure
  2531. *
  2532. * This function is used to add new network interfaces for %wpa_supplicant.
  2533. * This can be called before wpa_supplicant_run() to add interfaces before the
  2534. * main event loop has been started. In addition, new interfaces can be added
  2535. * dynamically while %wpa_supplicant is already running. This could happen,
  2536. * e.g., when a hotplug network adapter is inserted.
  2537. */
  2538. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  2539. struct wpa_interface *iface)
  2540. {
  2541. struct wpa_supplicant *wpa_s;
  2542. struct wpa_interface t_iface;
  2543. struct wpa_ssid *ssid;
  2544. if (global == NULL || iface == NULL)
  2545. return NULL;
  2546. wpa_s = wpa_supplicant_alloc();
  2547. if (wpa_s == NULL)
  2548. return NULL;
  2549. wpa_s->global = global;
  2550. t_iface = *iface;
  2551. if (global->params.override_driver) {
  2552. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  2553. "('%s' -> '%s')",
  2554. iface->driver, global->params.override_driver);
  2555. t_iface.driver = global->params.override_driver;
  2556. }
  2557. if (global->params.override_ctrl_interface) {
  2558. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  2559. "ctrl_interface ('%s' -> '%s')",
  2560. iface->ctrl_interface,
  2561. global->params.override_ctrl_interface);
  2562. t_iface.ctrl_interface =
  2563. global->params.override_ctrl_interface;
  2564. }
  2565. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  2566. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  2567. iface->ifname);
  2568. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  2569. os_free(wpa_s);
  2570. return NULL;
  2571. }
  2572. /* Notify the control interfaces about new iface */
  2573. if (wpas_notify_iface_added(wpa_s)) {
  2574. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  2575. os_free(wpa_s);
  2576. return NULL;
  2577. }
  2578. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2579. wpas_notify_network_added(wpa_s, ssid);
  2580. wpa_s->next = global->ifaces;
  2581. global->ifaces = wpa_s;
  2582. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  2583. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2584. return wpa_s;
  2585. }
  2586. /**
  2587. * wpa_supplicant_remove_iface - Remove a network interface
  2588. * @global: Pointer to global data from wpa_supplicant_init()
  2589. * @wpa_s: Pointer to the network interface to be removed
  2590. * Returns: 0 if interface was removed, -1 if interface was not found
  2591. *
  2592. * This function can be used to dynamically remove network interfaces from
  2593. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  2594. * addition, this function is used to remove all remaining interfaces when
  2595. * %wpa_supplicant is terminated.
  2596. */
  2597. int wpa_supplicant_remove_iface(struct wpa_global *global,
  2598. struct wpa_supplicant *wpa_s,
  2599. int terminate)
  2600. {
  2601. struct wpa_supplicant *prev;
  2602. /* Remove interface from the global list of interfaces */
  2603. prev = global->ifaces;
  2604. if (prev == wpa_s) {
  2605. global->ifaces = wpa_s->next;
  2606. } else {
  2607. while (prev && prev->next != wpa_s)
  2608. prev = prev->next;
  2609. if (prev == NULL)
  2610. return -1;
  2611. prev->next = wpa_s->next;
  2612. }
  2613. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  2614. if (global->p2p_group_formation == wpa_s)
  2615. global->p2p_group_formation = NULL;
  2616. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  2617. os_free(wpa_s);
  2618. return 0;
  2619. }
  2620. /**
  2621. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  2622. * @wpa_s: Pointer to the network interface
  2623. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  2624. */
  2625. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  2626. {
  2627. const char *eapol_method;
  2628. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  2629. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2630. return "NO-EAP";
  2631. }
  2632. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  2633. if (eapol_method == NULL)
  2634. return "UNKNOWN-EAP";
  2635. return eapol_method;
  2636. }
  2637. /**
  2638. * wpa_supplicant_get_iface - Get a new network interface
  2639. * @global: Pointer to global data from wpa_supplicant_init()
  2640. * @ifname: Interface name
  2641. * Returns: Pointer to the interface or %NULL if not found
  2642. */
  2643. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  2644. const char *ifname)
  2645. {
  2646. struct wpa_supplicant *wpa_s;
  2647. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2648. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2649. return wpa_s;
  2650. }
  2651. return NULL;
  2652. }
  2653. #ifndef CONFIG_NO_WPA_MSG
  2654. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  2655. {
  2656. struct wpa_supplicant *wpa_s = ctx;
  2657. if (wpa_s == NULL)
  2658. return NULL;
  2659. return wpa_s->ifname;
  2660. }
  2661. #endif /* CONFIG_NO_WPA_MSG */
  2662. /**
  2663. * wpa_supplicant_init - Initialize %wpa_supplicant
  2664. * @params: Parameters for %wpa_supplicant
  2665. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  2666. *
  2667. * This function is used to initialize %wpa_supplicant. After successful
  2668. * initialization, the returned data pointer can be used to add and remove
  2669. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  2670. */
  2671. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  2672. {
  2673. struct wpa_global *global;
  2674. int ret, i;
  2675. if (params == NULL)
  2676. return NULL;
  2677. #ifdef CONFIG_DRIVER_NDIS
  2678. {
  2679. void driver_ndis_init_ops(void);
  2680. driver_ndis_init_ops();
  2681. }
  2682. #endif /* CONFIG_DRIVER_NDIS */
  2683. #ifndef CONFIG_NO_WPA_MSG
  2684. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  2685. #endif /* CONFIG_NO_WPA_MSG */
  2686. wpa_debug_open_file(params->wpa_debug_file_path);
  2687. if (params->wpa_debug_syslog)
  2688. wpa_debug_open_syslog();
  2689. if (params->wpa_debug_tracing) {
  2690. ret = wpa_debug_open_linux_tracing();
  2691. if (ret) {
  2692. wpa_printf(MSG_ERROR,
  2693. "Failed to enable trace logging");
  2694. return NULL;
  2695. }
  2696. }
  2697. ret = eap_register_methods();
  2698. if (ret) {
  2699. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  2700. if (ret == -2)
  2701. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  2702. "the same EAP type.");
  2703. return NULL;
  2704. }
  2705. global = os_zalloc(sizeof(*global));
  2706. if (global == NULL)
  2707. return NULL;
  2708. dl_list_init(&global->p2p_srv_bonjour);
  2709. dl_list_init(&global->p2p_srv_upnp);
  2710. global->params.daemonize = params->daemonize;
  2711. global->params.wait_for_monitor = params->wait_for_monitor;
  2712. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  2713. if (params->pid_file)
  2714. global->params.pid_file = os_strdup(params->pid_file);
  2715. if (params->ctrl_interface)
  2716. global->params.ctrl_interface =
  2717. os_strdup(params->ctrl_interface);
  2718. if (params->override_driver)
  2719. global->params.override_driver =
  2720. os_strdup(params->override_driver);
  2721. if (params->override_ctrl_interface)
  2722. global->params.override_ctrl_interface =
  2723. os_strdup(params->override_ctrl_interface);
  2724. wpa_debug_level = global->params.wpa_debug_level =
  2725. params->wpa_debug_level;
  2726. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  2727. params->wpa_debug_show_keys;
  2728. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  2729. params->wpa_debug_timestamp;
  2730. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  2731. if (eloop_init()) {
  2732. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  2733. wpa_supplicant_deinit(global);
  2734. return NULL;
  2735. }
  2736. random_init(params->entropy_file);
  2737. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  2738. if (global->ctrl_iface == NULL) {
  2739. wpa_supplicant_deinit(global);
  2740. return NULL;
  2741. }
  2742. if (wpas_notify_supplicant_initialized(global)) {
  2743. wpa_supplicant_deinit(global);
  2744. return NULL;
  2745. }
  2746. for (i = 0; wpa_drivers[i]; i++)
  2747. global->drv_count++;
  2748. if (global->drv_count == 0) {
  2749. wpa_printf(MSG_ERROR, "No drivers enabled");
  2750. wpa_supplicant_deinit(global);
  2751. return NULL;
  2752. }
  2753. global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
  2754. if (global->drv_priv == NULL) {
  2755. wpa_supplicant_deinit(global);
  2756. return NULL;
  2757. }
  2758. #ifdef CONFIG_WIFI_DISPLAY
  2759. if (wifi_display_init(global) < 0) {
  2760. wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
  2761. wpa_supplicant_deinit(global);
  2762. return NULL;
  2763. }
  2764. #endif /* CONFIG_WIFI_DISPLAY */
  2765. return global;
  2766. }
  2767. /**
  2768. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  2769. * @global: Pointer to global data from wpa_supplicant_init()
  2770. * Returns: 0 after successful event loop run, -1 on failure
  2771. *
  2772. * This function starts the main event loop and continues running as long as
  2773. * there are any remaining events. In most cases, this function is running as
  2774. * long as the %wpa_supplicant process in still in use.
  2775. */
  2776. int wpa_supplicant_run(struct wpa_global *global)
  2777. {
  2778. struct wpa_supplicant *wpa_s;
  2779. if (global->params.daemonize &&
  2780. wpa_supplicant_daemon(global->params.pid_file))
  2781. return -1;
  2782. if (global->params.wait_for_monitor) {
  2783. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  2784. if (wpa_s->ctrl_iface)
  2785. wpa_supplicant_ctrl_iface_wait(
  2786. wpa_s->ctrl_iface);
  2787. }
  2788. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  2789. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  2790. eloop_run();
  2791. return 0;
  2792. }
  2793. /**
  2794. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  2795. * @global: Pointer to global data from wpa_supplicant_init()
  2796. *
  2797. * This function is called to deinitialize %wpa_supplicant and to free all
  2798. * allocated resources. Remaining network interfaces will also be removed.
  2799. */
  2800. void wpa_supplicant_deinit(struct wpa_global *global)
  2801. {
  2802. int i;
  2803. if (global == NULL)
  2804. return;
  2805. #ifdef CONFIG_WIFI_DISPLAY
  2806. wifi_display_deinit(global);
  2807. #endif /* CONFIG_WIFI_DISPLAY */
  2808. #ifdef CONFIG_P2P
  2809. wpas_p2p_deinit_global(global);
  2810. #endif /* CONFIG_P2P */
  2811. while (global->ifaces)
  2812. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  2813. if (global->ctrl_iface)
  2814. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  2815. wpas_notify_supplicant_deinitialized(global);
  2816. eap_peer_unregister_methods();
  2817. #ifdef CONFIG_AP
  2818. eap_server_unregister_methods();
  2819. #endif /* CONFIG_AP */
  2820. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  2821. if (!global->drv_priv[i])
  2822. continue;
  2823. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  2824. }
  2825. os_free(global->drv_priv);
  2826. random_deinit();
  2827. eloop_destroy();
  2828. if (global->params.pid_file) {
  2829. os_daemonize_terminate(global->params.pid_file);
  2830. os_free(global->params.pid_file);
  2831. }
  2832. os_free(global->params.ctrl_interface);
  2833. os_free(global->params.override_driver);
  2834. os_free(global->params.override_ctrl_interface);
  2835. os_free(global->p2p_disallow_freq);
  2836. os_free(global);
  2837. wpa_debug_close_syslog();
  2838. wpa_debug_close_file();
  2839. wpa_debug_close_linux_tracing();
  2840. }
  2841. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  2842. {
  2843. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  2844. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2845. char country[3];
  2846. country[0] = wpa_s->conf->country[0];
  2847. country[1] = wpa_s->conf->country[1];
  2848. country[2] = '\0';
  2849. if (wpa_drv_set_country(wpa_s, country) < 0) {
  2850. wpa_printf(MSG_ERROR, "Failed to set country code "
  2851. "'%s'", country);
  2852. }
  2853. }
  2854. if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
  2855. wpas_init_ext_pw(wpa_s);
  2856. #ifdef CONFIG_WPS
  2857. wpas_wps_update_config(wpa_s);
  2858. #endif /* CONFIG_WPS */
  2859. #ifdef CONFIG_P2P
  2860. wpas_p2p_update_config(wpa_s);
  2861. #endif /* CONFIG_P2P */
  2862. wpa_s->conf->changed_parameters = 0;
  2863. }
  2864. static void add_freq(int *freqs, int *num_freqs, int freq)
  2865. {
  2866. int i;
  2867. for (i = 0; i < *num_freqs; i++) {
  2868. if (freqs[i] == freq)
  2869. return;
  2870. }
  2871. freqs[*num_freqs] = freq;
  2872. (*num_freqs)++;
  2873. }
  2874. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  2875. {
  2876. struct wpa_bss *bss, *cbss;
  2877. const int max_freqs = 10;
  2878. int *freqs;
  2879. int num_freqs = 0;
  2880. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  2881. if (freqs == NULL)
  2882. return NULL;
  2883. cbss = wpa_s->current_bss;
  2884. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2885. if (bss == cbss)
  2886. continue;
  2887. if (bss->ssid_len == cbss->ssid_len &&
  2888. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  2889. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  2890. add_freq(freqs, &num_freqs, bss->freq);
  2891. if (num_freqs == max_freqs)
  2892. break;
  2893. }
  2894. }
  2895. if (num_freqs == 0) {
  2896. os_free(freqs);
  2897. freqs = NULL;
  2898. }
  2899. return freqs;
  2900. }
  2901. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2902. {
  2903. int timeout;
  2904. int count;
  2905. int *freqs = NULL;
  2906. /*
  2907. * Remove possible authentication timeout since the connection failed.
  2908. */
  2909. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  2910. /*
  2911. * Add the failed BSSID into the blacklist and speed up next scan
  2912. * attempt if there could be other APs that could accept association.
  2913. * The current blacklist count indicates how many times we have tried
  2914. * connecting to this AP and multiple attempts mean that other APs are
  2915. * either not available or has already been tried, so that we can start
  2916. * increasing the delay here to avoid constant scanning.
  2917. */
  2918. count = wpa_blacklist_add(wpa_s, bssid);
  2919. if (count == 1 && wpa_s->current_bss) {
  2920. /*
  2921. * This BSS was not in the blacklist before. If there is
  2922. * another BSS available for the same ESS, we should try that
  2923. * next. Otherwise, we may as well try this one once more
  2924. * before allowing other, likely worse, ESSes to be considered.
  2925. */
  2926. freqs = get_bss_freqs_in_ess(wpa_s);
  2927. if (freqs) {
  2928. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  2929. "has been seen; try it next");
  2930. wpa_blacklist_add(wpa_s, bssid);
  2931. /*
  2932. * On the next scan, go through only the known channels
  2933. * used in this ESS based on previous scans to speed up
  2934. * common load balancing use case.
  2935. */
  2936. os_free(wpa_s->next_scan_freqs);
  2937. wpa_s->next_scan_freqs = freqs;
  2938. }
  2939. }
  2940. switch (count) {
  2941. case 1:
  2942. timeout = 100;
  2943. break;
  2944. case 2:
  2945. timeout = 500;
  2946. break;
  2947. case 3:
  2948. timeout = 1000;
  2949. break;
  2950. default:
  2951. timeout = 5000;
  2952. }
  2953. /*
  2954. * TODO: if more than one possible AP is available in scan results,
  2955. * could try the other ones before requesting a new scan.
  2956. */
  2957. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  2958. 1000 * (timeout % 1000));
  2959. #ifdef CONFIG_P2P
  2960. if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
  2961. wpa_s->global->p2p != NULL) {
  2962. wpa_s->global->p2p_cb_on_scan_complete = 0;
  2963. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  2964. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  2965. "continued after failed association");
  2966. }
  2967. }
  2968. #endif /* CONFIG_P2P */
  2969. }
  2970. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  2971. {
  2972. return wpa_s->conf->ap_scan == 2 ||
  2973. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  2974. }
  2975. #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
  2976. int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
  2977. struct wpa_ssid *ssid,
  2978. const char *field,
  2979. const char *value)
  2980. {
  2981. #ifdef IEEE8021X_EAPOL
  2982. struct eap_peer_config *eap = &ssid->eap;
  2983. wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
  2984. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
  2985. (const u8 *) value, os_strlen(value));
  2986. switch (wpa_supplicant_ctrl_req_from_string(field)) {
  2987. case WPA_CTRL_REQ_EAP_IDENTITY:
  2988. os_free(eap->identity);
  2989. eap->identity = (u8 *) os_strdup(value);
  2990. eap->identity_len = os_strlen(value);
  2991. eap->pending_req_identity = 0;
  2992. if (ssid == wpa_s->current_ssid)
  2993. wpa_s->reassociate = 1;
  2994. break;
  2995. case WPA_CTRL_REQ_EAP_PASSWORD:
  2996. os_free(eap->password);
  2997. eap->password = (u8 *) os_strdup(value);
  2998. eap->password_len = os_strlen(value);
  2999. eap->pending_req_password = 0;
  3000. if (ssid == wpa_s->current_ssid)
  3001. wpa_s->reassociate = 1;
  3002. break;
  3003. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  3004. os_free(eap->new_password);
  3005. eap->new_password = (u8 *) os_strdup(value);
  3006. eap->new_password_len = os_strlen(value);
  3007. eap->pending_req_new_password = 0;
  3008. if (ssid == wpa_s->current_ssid)
  3009. wpa_s->reassociate = 1;
  3010. break;
  3011. case WPA_CTRL_REQ_EAP_PIN:
  3012. os_free(eap->pin);
  3013. eap->pin = os_strdup(value);
  3014. eap->pending_req_pin = 0;
  3015. if (ssid == wpa_s->current_ssid)
  3016. wpa_s->reassociate = 1;
  3017. break;
  3018. case WPA_CTRL_REQ_EAP_OTP:
  3019. os_free(eap->otp);
  3020. eap->otp = (u8 *) os_strdup(value);
  3021. eap->otp_len = os_strlen(value);
  3022. os_free(eap->pending_req_otp);
  3023. eap->pending_req_otp = NULL;
  3024. eap->pending_req_otp_len = 0;
  3025. break;
  3026. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  3027. os_free(eap->private_key_passwd);
  3028. eap->private_key_passwd = (u8 *) os_strdup(value);
  3029. eap->pending_req_passphrase = 0;
  3030. if (ssid == wpa_s->current_ssid)
  3031. wpa_s->reassociate = 1;
  3032. break;
  3033. default:
  3034. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
  3035. return -1;
  3036. }
  3037. return 0;
  3038. #else /* IEEE8021X_EAPOL */
  3039. wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
  3040. return -1;
  3041. #endif /* IEEE8021X_EAPOL */
  3042. }
  3043. #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
  3044. int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  3045. {
  3046. int i;
  3047. unsigned int drv_enc;
  3048. if (ssid == NULL)
  3049. return 1;
  3050. if (ssid->disabled)
  3051. return 1;
  3052. if (wpa_s && wpa_s->drv_capa_known)
  3053. drv_enc = wpa_s->drv_enc;
  3054. else
  3055. drv_enc = (unsigned int) -1;
  3056. for (i = 0; i < NUM_WEP_KEYS; i++) {
  3057. size_t len = ssid->wep_key_len[i];
  3058. if (len == 0)
  3059. continue;
  3060. if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
  3061. continue;
  3062. if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
  3063. continue;
  3064. if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
  3065. continue;
  3066. return 1; /* invalid WEP key */
  3067. }
  3068. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  3069. !ssid->ext_psk)
  3070. return 1;
  3071. return 0;
  3072. }
  3073. int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
  3074. {
  3075. if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
  3076. return 1;
  3077. if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
  3078. return 0;
  3079. return -1;
  3080. }
  3081. void wpas_auth_failed(struct wpa_supplicant *wpa_s)
  3082. {
  3083. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3084. int dur;
  3085. struct os_time now;
  3086. if (ssid == NULL) {
  3087. wpa_printf(MSG_DEBUG, "Authentication failure but no known "
  3088. "SSID block");
  3089. return;
  3090. }
  3091. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  3092. return;
  3093. ssid->auth_failures++;
  3094. if (ssid->auth_failures > 50)
  3095. dur = 300;
  3096. else if (ssid->auth_failures > 20)
  3097. dur = 120;
  3098. else if (ssid->auth_failures > 10)
  3099. dur = 60;
  3100. else if (ssid->auth_failures > 5)
  3101. dur = 30;
  3102. else if (ssid->auth_failures > 1)
  3103. dur = 20;
  3104. else
  3105. dur = 10;
  3106. os_get_time(&now);
  3107. if (now.sec + dur <= ssid->disabled_until.sec)
  3108. return;
  3109. ssid->disabled_until.sec = now.sec + dur;
  3110. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
  3111. "id=%d ssid=\"%s\" auth_failures=%u duration=%d",
  3112. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  3113. ssid->auth_failures, dur);
  3114. }
  3115. void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
  3116. struct wpa_ssid *ssid, int clear_failures)
  3117. {
  3118. if (ssid == NULL)
  3119. return;
  3120. if (ssid->disabled_until.sec) {
  3121. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
  3122. "id=%d ssid=\"%s\"",
  3123. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  3124. }
  3125. ssid->disabled_until.sec = 0;
  3126. ssid->disabled_until.usec = 0;
  3127. if (clear_failures)
  3128. ssid->auth_failures = 0;
  3129. }
  3130. int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
  3131. {
  3132. size_t i;
  3133. if (wpa_s->disallow_aps_bssid == NULL)
  3134. return 0;
  3135. for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
  3136. if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
  3137. bssid, ETH_ALEN) == 0)
  3138. return 1;
  3139. }
  3140. return 0;
  3141. }
  3142. int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
  3143. size_t ssid_len)
  3144. {
  3145. size_t i;
  3146. if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
  3147. return 0;
  3148. for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
  3149. struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
  3150. if (ssid_len == s->ssid_len &&
  3151. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  3152. return 1;
  3153. }
  3154. return 0;
  3155. }