wpa_supplicant.c 100 KB

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