wpa_supplicant.c 83 KB

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