wpa_supplicant.c 99 KB

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