wpa_supplicant.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924
  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-2012, 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->bssid_set &&
  1222. wpa_s->conf->ap_scan == 2) {
  1223. params.bssid = ssid->bssid;
  1224. params.fixed_bssid = 1;
  1225. }
  1226. if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
  1227. params.freq == 0)
  1228. params.freq = ssid->frequency; /* Initial channel for IBSS */
  1229. params.wpa_ie = wpa_ie;
  1230. params.wpa_ie_len = wpa_ie_len;
  1231. params.pairwise_suite = cipher_pairwise;
  1232. params.group_suite = cipher_group;
  1233. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  1234. params.wpa_proto = wpa_s->wpa_proto;
  1235. params.auth_alg = algs;
  1236. params.mode = ssid->mode;
  1237. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1238. if (ssid->wep_key_len[i])
  1239. params.wep_key[i] = ssid->wep_key[i];
  1240. params.wep_key_len[i] = ssid->wep_key_len[i];
  1241. }
  1242. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1243. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1244. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  1245. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  1246. params.passphrase = ssid->passphrase;
  1247. if (ssid->psk_set)
  1248. params.psk = ssid->psk;
  1249. }
  1250. params.drop_unencrypted = use_crypt;
  1251. #ifdef CONFIG_IEEE80211W
  1252. params.mgmt_frame_protection = ssid->ieee80211w;
  1253. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  1254. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1255. struct wpa_ie_data ie;
  1256. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1257. ie.capabilities &
  1258. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1259. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1260. "MFP: require MFP");
  1261. params.mgmt_frame_protection =
  1262. MGMT_FRAME_PROTECTION_REQUIRED;
  1263. }
  1264. }
  1265. #endif /* CONFIG_IEEE80211W */
  1266. params.p2p = ssid->p2p_group;
  1267. if (wpa_s->parent->set_sta_uapsd)
  1268. params.uapsd = wpa_s->parent->sta_uapsd;
  1269. else
  1270. params.uapsd = -1;
  1271. ret = wpa_drv_associate(wpa_s, &params);
  1272. if (ret < 0) {
  1273. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1274. "failed");
  1275. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1276. /*
  1277. * The driver is known to mean what is saying, so we
  1278. * can stop right here; the association will not
  1279. * succeed.
  1280. */
  1281. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1282. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1283. return;
  1284. }
  1285. /* try to continue anyway; new association will be tried again
  1286. * after timeout */
  1287. assoc_failed = 1;
  1288. }
  1289. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1290. /* Set the key after the association just in case association
  1291. * cleared the previously configured key. */
  1292. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1293. /* No need to timeout authentication since there is no key
  1294. * management. */
  1295. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1296. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1297. #ifdef CONFIG_IBSS_RSN
  1298. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1299. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1300. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1301. /*
  1302. * RSN IBSS authentication is per-STA and we can disable the
  1303. * per-BSSID authentication.
  1304. */
  1305. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1306. #endif /* CONFIG_IBSS_RSN */
  1307. } else {
  1308. /* Timeout for IEEE 802.11 authentication and association */
  1309. int timeout = 60;
  1310. if (assoc_failed) {
  1311. /* give IBSS a bit more time */
  1312. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1313. } else if (wpa_s->conf->ap_scan == 1) {
  1314. /* give IBSS a bit more time */
  1315. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1316. }
  1317. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1318. }
  1319. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1320. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1321. /* Set static WEP keys again */
  1322. wpa_set_wep_keys(wpa_s, ssid);
  1323. }
  1324. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1325. /*
  1326. * Do not allow EAP session resumption between different
  1327. * network configurations.
  1328. */
  1329. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1330. }
  1331. old_ssid = wpa_s->current_ssid;
  1332. wpa_s->current_ssid = ssid;
  1333. wpa_s->current_bss = bss;
  1334. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1335. wpa_supplicant_initiate_eapol(wpa_s);
  1336. if (old_ssid != wpa_s->current_ssid)
  1337. wpas_notify_network_changed(wpa_s);
  1338. }
  1339. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1340. const u8 *addr)
  1341. {
  1342. struct wpa_ssid *old_ssid;
  1343. wpa_clear_keys(wpa_s, addr);
  1344. wpa_supplicant_mark_disassoc(wpa_s);
  1345. old_ssid = wpa_s->current_ssid;
  1346. wpa_s->current_ssid = NULL;
  1347. wpa_s->current_bss = NULL;
  1348. wpa_sm_set_config(wpa_s->wpa, NULL);
  1349. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1350. if (old_ssid != wpa_s->current_ssid)
  1351. wpas_notify_network_changed(wpa_s);
  1352. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1353. }
  1354. /**
  1355. * wpa_supplicant_disassociate - Disassociate the current connection
  1356. * @wpa_s: Pointer to wpa_supplicant data
  1357. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1358. *
  1359. * This function is used to request %wpa_supplicant to disassociate with the
  1360. * current AP.
  1361. */
  1362. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1363. int reason_code)
  1364. {
  1365. u8 *addr = NULL;
  1366. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1367. wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
  1368. addr = wpa_s->bssid;
  1369. }
  1370. wpa_supplicant_clear_connection(wpa_s, addr);
  1371. }
  1372. /**
  1373. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1374. * @wpa_s: Pointer to wpa_supplicant data
  1375. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1376. *
  1377. * This function is used to request %wpa_supplicant to deauthenticate from the
  1378. * current AP.
  1379. */
  1380. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1381. int reason_code)
  1382. {
  1383. u8 *addr = NULL;
  1384. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1385. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
  1386. addr = wpa_s->bssid;
  1387. }
  1388. wpa_supplicant_clear_connection(wpa_s, addr);
  1389. }
  1390. /**
  1391. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1392. * @wpa_s: wpa_supplicant structure for a network interface
  1393. * @ssid: wpa_ssid structure for a configured network or %NULL
  1394. *
  1395. * Enables the specified network or all networks if no network specified.
  1396. */
  1397. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1398. struct wpa_ssid *ssid)
  1399. {
  1400. struct wpa_ssid *other_ssid;
  1401. int was_disabled;
  1402. if (ssid == NULL) {
  1403. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1404. other_ssid = other_ssid->next) {
  1405. if (other_ssid->disabled == 2)
  1406. continue; /* do not change persistent P2P group
  1407. * data */
  1408. if (other_ssid == wpa_s->current_ssid &&
  1409. other_ssid->disabled)
  1410. wpa_s->reassociate = 1;
  1411. was_disabled = other_ssid->disabled;
  1412. other_ssid->disabled = 0;
  1413. if (was_disabled != other_ssid->disabled)
  1414. wpas_notify_network_enabled_changed(
  1415. wpa_s, other_ssid);
  1416. }
  1417. if (wpa_s->reassociate)
  1418. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1419. } else if (ssid->disabled && ssid->disabled != 2) {
  1420. if (wpa_s->current_ssid == NULL) {
  1421. /*
  1422. * Try to reassociate since there is no current
  1423. * configuration and a new network was made available.
  1424. */
  1425. wpa_s->reassociate = 1;
  1426. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1427. }
  1428. was_disabled = ssid->disabled;
  1429. ssid->disabled = 0;
  1430. if (was_disabled != ssid->disabled)
  1431. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1432. }
  1433. }
  1434. /**
  1435. * wpa_supplicant_disable_network - Mark a configured network as disabled
  1436. * @wpa_s: wpa_supplicant structure for a network interface
  1437. * @ssid: wpa_ssid structure for a configured network or %NULL
  1438. *
  1439. * Disables the specified network or all networks if no network specified.
  1440. */
  1441. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  1442. struct wpa_ssid *ssid)
  1443. {
  1444. struct wpa_ssid *other_ssid;
  1445. int was_disabled;
  1446. if (ssid == NULL) {
  1447. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1448. other_ssid = other_ssid->next) {
  1449. was_disabled = other_ssid->disabled;
  1450. if (was_disabled == 2)
  1451. continue; /* do not change persistent P2P group
  1452. * data */
  1453. other_ssid->disabled = 1;
  1454. if (was_disabled != other_ssid->disabled)
  1455. wpas_notify_network_enabled_changed(
  1456. wpa_s, other_ssid);
  1457. }
  1458. if (wpa_s->current_ssid)
  1459. wpa_supplicant_disassociate(
  1460. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1461. } else if (ssid->disabled != 2) {
  1462. if (ssid == wpa_s->current_ssid)
  1463. wpa_supplicant_disassociate(
  1464. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1465. was_disabled = ssid->disabled;
  1466. ssid->disabled = 1;
  1467. if (was_disabled != ssid->disabled)
  1468. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1469. }
  1470. }
  1471. /**
  1472. * wpa_supplicant_select_network - Attempt association with a network
  1473. * @wpa_s: wpa_supplicant structure for a network interface
  1474. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  1475. */
  1476. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  1477. struct wpa_ssid *ssid)
  1478. {
  1479. struct wpa_ssid *other_ssid;
  1480. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
  1481. wpa_supplicant_disassociate(
  1482. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1483. /*
  1484. * Mark all other networks disabled or mark all networks enabled if no
  1485. * network specified.
  1486. */
  1487. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1488. other_ssid = other_ssid->next) {
  1489. int was_disabled = other_ssid->disabled;
  1490. if (was_disabled == 2)
  1491. continue; /* do not change persistent P2P group data */
  1492. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  1493. if (was_disabled != other_ssid->disabled)
  1494. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  1495. }
  1496. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
  1497. /* We are already associated with the selected network */
  1498. wpa_printf(MSG_DEBUG, "Already associated with the "
  1499. "selected network - do nothing");
  1500. return;
  1501. }
  1502. wpa_s->connect_without_scan = NULL;
  1503. wpa_s->disconnected = 0;
  1504. wpa_s->reassociate = 1;
  1505. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1506. if (ssid)
  1507. wpas_notify_network_selected(wpa_s, ssid);
  1508. }
  1509. /**
  1510. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  1511. * @wpa_s: wpa_supplicant structure for a network interface
  1512. * @ap_scan: AP scan mode
  1513. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  1514. *
  1515. */
  1516. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  1517. {
  1518. int old_ap_scan;
  1519. if (ap_scan < 0 || ap_scan > 2)
  1520. return -1;
  1521. #ifdef ANDROID
  1522. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  1523. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  1524. wpa_s->wpa_state < WPA_COMPLETED) {
  1525. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  1526. "associating", wpa_s->conf->ap_scan, ap_scan);
  1527. return 0;
  1528. }
  1529. #endif /* ANDROID */
  1530. old_ap_scan = wpa_s->conf->ap_scan;
  1531. wpa_s->conf->ap_scan = ap_scan;
  1532. if (old_ap_scan != wpa_s->conf->ap_scan)
  1533. wpas_notify_ap_scan_changed(wpa_s);
  1534. return 0;
  1535. }
  1536. /**
  1537. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  1538. * @wpa_s: wpa_supplicant structure for a network interface
  1539. * @expire_age: Expiration age in seconds
  1540. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  1541. *
  1542. */
  1543. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  1544. unsigned int bss_expire_age)
  1545. {
  1546. if (bss_expire_age < 10) {
  1547. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  1548. bss_expire_age);
  1549. return -1;
  1550. }
  1551. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  1552. bss_expire_age);
  1553. wpa_s->conf->bss_expiration_age = bss_expire_age;
  1554. return 0;
  1555. }
  1556. /**
  1557. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  1558. * @wpa_s: wpa_supplicant structure for a network interface
  1559. * @expire_count: number of scans after which an unseen BSS is reclaimed
  1560. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  1561. *
  1562. */
  1563. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  1564. unsigned int bss_expire_count)
  1565. {
  1566. if (bss_expire_count < 1) {
  1567. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  1568. bss_expire_count);
  1569. return -1;
  1570. }
  1571. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  1572. bss_expire_count);
  1573. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  1574. return 0;
  1575. }
  1576. /**
  1577. * wpa_supplicant_set_debug_params - Set global debug params
  1578. * @global: wpa_global structure
  1579. * @debug_level: debug level
  1580. * @debug_timestamp: determines if show timestamp in debug data
  1581. * @debug_show_keys: determines if show keys in debug data
  1582. * Returns: 0 if succeed or -1 if debug_level has wrong value
  1583. */
  1584. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  1585. int debug_timestamp, int debug_show_keys)
  1586. {
  1587. int old_level, old_timestamp, old_show_keys;
  1588. /* check for allowed debuglevels */
  1589. if (debug_level != MSG_EXCESSIVE &&
  1590. debug_level != MSG_MSGDUMP &&
  1591. debug_level != MSG_DEBUG &&
  1592. debug_level != MSG_INFO &&
  1593. debug_level != MSG_WARNING &&
  1594. debug_level != MSG_ERROR)
  1595. return -1;
  1596. old_level = wpa_debug_level;
  1597. old_timestamp = wpa_debug_timestamp;
  1598. old_show_keys = wpa_debug_show_keys;
  1599. wpa_debug_level = debug_level;
  1600. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  1601. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  1602. if (wpa_debug_level != old_level)
  1603. wpas_notify_debug_level_changed(global);
  1604. if (wpa_debug_timestamp != old_timestamp)
  1605. wpas_notify_debug_timestamp_changed(global);
  1606. if (wpa_debug_show_keys != old_show_keys)
  1607. wpas_notify_debug_show_keys_changed(global);
  1608. return 0;
  1609. }
  1610. /**
  1611. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1612. * @wpa_s: Pointer to wpa_supplicant data
  1613. * Returns: A pointer to the current network structure or %NULL on failure
  1614. */
  1615. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1616. {
  1617. struct wpa_ssid *entry;
  1618. u8 ssid[MAX_SSID_LEN];
  1619. int res;
  1620. size_t ssid_len;
  1621. u8 bssid[ETH_ALEN];
  1622. int wired;
  1623. res = wpa_drv_get_ssid(wpa_s, ssid);
  1624. if (res < 0) {
  1625. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1626. "driver");
  1627. return NULL;
  1628. }
  1629. ssid_len = res;
  1630. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1631. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  1632. "driver");
  1633. return NULL;
  1634. }
  1635. wired = wpa_s->conf->ap_scan == 0 &&
  1636. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  1637. entry = wpa_s->conf->ssid;
  1638. while (entry) {
  1639. if (!entry->disabled &&
  1640. ((ssid_len == entry->ssid_len &&
  1641. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1642. (!entry->bssid_set ||
  1643. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1644. return entry;
  1645. #ifdef CONFIG_WPS
  1646. if (!entry->disabled &&
  1647. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  1648. (entry->ssid == NULL || entry->ssid_len == 0) &&
  1649. (!entry->bssid_set ||
  1650. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1651. return entry;
  1652. #endif /* CONFIG_WPS */
  1653. if (!entry->disabled && entry->bssid_set &&
  1654. entry->ssid_len == 0 &&
  1655. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  1656. return entry;
  1657. entry = entry->next;
  1658. }
  1659. return NULL;
  1660. }
  1661. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  1662. {
  1663. struct wpa_global *global = wpa_s->global;
  1664. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  1665. global->drv_priv[i] = wpa_drivers[i]->global_init();
  1666. if (global->drv_priv[i] == NULL) {
  1667. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  1668. "'%s'", wpa_drivers[i]->name);
  1669. return -1;
  1670. }
  1671. }
  1672. wpa_s->driver = wpa_drivers[i];
  1673. wpa_s->global_drv_priv = global->drv_priv[i];
  1674. return 0;
  1675. }
  1676. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1677. const char *name)
  1678. {
  1679. int i;
  1680. size_t len;
  1681. const char *pos, *driver = name;
  1682. if (wpa_s == NULL)
  1683. return -1;
  1684. if (wpa_drivers[0] == NULL) {
  1685. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  1686. "wpa_supplicant");
  1687. return -1;
  1688. }
  1689. if (name == NULL) {
  1690. /* default to first driver in the list */
  1691. return select_driver(wpa_s, 0);
  1692. }
  1693. do {
  1694. pos = os_strchr(driver, ',');
  1695. if (pos)
  1696. len = pos - driver;
  1697. else
  1698. len = os_strlen(driver);
  1699. for (i = 0; wpa_drivers[i]; i++) {
  1700. if (os_strlen(wpa_drivers[i]->name) == len &&
  1701. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  1702. 0)
  1703. return select_driver(wpa_s, i);
  1704. }
  1705. driver = pos + 1;
  1706. } while (pos);
  1707. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  1708. return -1;
  1709. }
  1710. /**
  1711. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  1712. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  1713. * with struct wpa_driver_ops::init()
  1714. * @src_addr: Source address of the EAPOL frame
  1715. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  1716. * @len: Length of the EAPOL data
  1717. *
  1718. * This function is called for each received EAPOL frame. Most driver
  1719. * interfaces rely on more generic OS mechanism for receiving frames through
  1720. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  1721. * take care of received EAPOL frames and deliver them to the core supplicant
  1722. * code by calling this function.
  1723. */
  1724. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1725. const u8 *buf, size_t len)
  1726. {
  1727. struct wpa_supplicant *wpa_s = ctx;
  1728. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1729. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1730. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  1731. /*
  1732. * There is possible race condition between receiving the
  1733. * association event and the EAPOL frame since they are coming
  1734. * through different paths from the driver. In order to avoid
  1735. * issues in trying to process the EAPOL frame before receiving
  1736. * association information, lets queue it for processing until
  1737. * the association event is received.
  1738. */
  1739. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  1740. "of received EAPOL frame");
  1741. wpabuf_free(wpa_s->pending_eapol_rx);
  1742. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  1743. if (wpa_s->pending_eapol_rx) {
  1744. os_get_time(&wpa_s->pending_eapol_rx_time);
  1745. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  1746. ETH_ALEN);
  1747. }
  1748. return;
  1749. }
  1750. #ifdef CONFIG_AP
  1751. if (wpa_s->ap_iface) {
  1752. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  1753. return;
  1754. }
  1755. #endif /* CONFIG_AP */
  1756. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1757. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  1758. "no key management is configured");
  1759. return;
  1760. }
  1761. if (wpa_s->eapol_received == 0 &&
  1762. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  1763. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1764. wpa_s->wpa_state != WPA_COMPLETED) &&
  1765. (wpa_s->current_ssid == NULL ||
  1766. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  1767. /* Timeout for completing IEEE 802.1X and WPA authentication */
  1768. wpa_supplicant_req_auth_timeout(
  1769. wpa_s,
  1770. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1771. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  1772. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  1773. 70 : 10, 0);
  1774. }
  1775. wpa_s->eapol_received++;
  1776. if (wpa_s->countermeasures) {
  1777. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  1778. "EAPOL packet");
  1779. return;
  1780. }
  1781. #ifdef CONFIG_IBSS_RSN
  1782. if (wpa_s->current_ssid &&
  1783. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  1784. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  1785. return;
  1786. }
  1787. #endif /* CONFIG_IBSS_RSN */
  1788. /* Source address of the incoming EAPOL frame could be compared to the
  1789. * current BSSID. However, it is possible that a centralized
  1790. * Authenticator could be using another MAC address than the BSSID of
  1791. * an AP, so just allow any address to be used for now. The replies are
  1792. * still sent to the current BSSID (if available), though. */
  1793. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  1794. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  1795. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  1796. return;
  1797. wpa_drv_poll(wpa_s);
  1798. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  1799. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  1800. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1801. /*
  1802. * Set portValid = TRUE here since we are going to skip 4-way
  1803. * handshake processing which would normally set portValid. We
  1804. * need this to allow the EAPOL state machines to be completed
  1805. * without going through EAPOL-Key handshake.
  1806. */
  1807. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1808. }
  1809. }
  1810. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  1811. {
  1812. if (wpa_s->driver->send_eapol) {
  1813. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1814. if (addr)
  1815. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1816. } else if (!(wpa_s->drv_flags &
  1817. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  1818. l2_packet_deinit(wpa_s->l2);
  1819. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  1820. wpa_drv_get_mac_addr(wpa_s),
  1821. ETH_P_EAPOL,
  1822. wpa_supplicant_rx_eapol, wpa_s, 0);
  1823. if (wpa_s->l2 == NULL)
  1824. return -1;
  1825. } else {
  1826. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1827. if (addr)
  1828. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1829. }
  1830. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  1831. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  1832. return -1;
  1833. }
  1834. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  1835. MAC2STR(wpa_s->own_addr));
  1836. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  1837. return 0;
  1838. }
  1839. /**
  1840. * wpa_supplicant_driver_init - Initialize driver interface parameters
  1841. * @wpa_s: Pointer to wpa_supplicant data
  1842. * Returns: 0 on success, -1 on failure
  1843. *
  1844. * This function is called to initialize driver interface parameters.
  1845. * wpa_drv_init() must have been called before this function to initialize the
  1846. * driver interface.
  1847. */
  1848. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  1849. {
  1850. static int interface_count = 0;
  1851. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  1852. return -1;
  1853. if (wpa_s->bridge_ifname[0]) {
  1854. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  1855. "interface '%s'", wpa_s->bridge_ifname);
  1856. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  1857. wpa_s->own_addr,
  1858. ETH_P_EAPOL,
  1859. wpa_supplicant_rx_eapol, wpa_s,
  1860. 0);
  1861. if (wpa_s->l2_br == NULL) {
  1862. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  1863. "connection for the bridge interface '%s'",
  1864. wpa_s->bridge_ifname);
  1865. return -1;
  1866. }
  1867. }
  1868. wpa_clear_keys(wpa_s, NULL);
  1869. /* Make sure that TKIP countermeasures are not left enabled (could
  1870. * happen if wpa_supplicant is killed during countermeasures. */
  1871. wpa_drv_set_countermeasures(wpa_s, 0);
  1872. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  1873. wpa_drv_flush_pmkid(wpa_s);
  1874. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  1875. wpa_s->prev_scan_wildcard = 0;
  1876. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  1877. if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
  1878. 100000))
  1879. wpa_supplicant_req_scan(wpa_s, interface_count,
  1880. 100000);
  1881. interface_count++;
  1882. } else
  1883. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  1884. return 0;
  1885. }
  1886. static int wpa_supplicant_daemon(const char *pid_file)
  1887. {
  1888. wpa_printf(MSG_DEBUG, "Daemonize..");
  1889. return os_daemonize(pid_file);
  1890. }
  1891. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  1892. {
  1893. struct wpa_supplicant *wpa_s;
  1894. wpa_s = os_zalloc(sizeof(*wpa_s));
  1895. if (wpa_s == NULL)
  1896. return NULL;
  1897. wpa_s->scan_req = 1;
  1898. wpa_s->scan_interval = 5;
  1899. wpa_s->new_connection = 1;
  1900. wpa_s->parent = wpa_s;
  1901. wpa_s->sched_scanning = 0;
  1902. return wpa_s;
  1903. }
  1904. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  1905. struct wpa_interface *iface)
  1906. {
  1907. const char *ifname, *driver;
  1908. struct wpa_driver_capa capa;
  1909. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  1910. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  1911. iface->confname ? iface->confname : "N/A",
  1912. iface->driver ? iface->driver : "default",
  1913. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  1914. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  1915. if (iface->confname) {
  1916. #ifdef CONFIG_BACKEND_FILE
  1917. wpa_s->confname = os_rel2abs_path(iface->confname);
  1918. if (wpa_s->confname == NULL) {
  1919. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  1920. "for configuration file '%s'.",
  1921. iface->confname);
  1922. return -1;
  1923. }
  1924. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  1925. iface->confname, wpa_s->confname);
  1926. #else /* CONFIG_BACKEND_FILE */
  1927. wpa_s->confname = os_strdup(iface->confname);
  1928. #endif /* CONFIG_BACKEND_FILE */
  1929. wpa_s->conf = wpa_config_read(wpa_s->confname);
  1930. if (wpa_s->conf == NULL) {
  1931. wpa_printf(MSG_ERROR, "Failed to read or parse "
  1932. "configuration '%s'.", wpa_s->confname);
  1933. return -1;
  1934. }
  1935. /*
  1936. * Override ctrl_interface and driver_param if set on command
  1937. * line.
  1938. */
  1939. if (iface->ctrl_interface) {
  1940. os_free(wpa_s->conf->ctrl_interface);
  1941. wpa_s->conf->ctrl_interface =
  1942. os_strdup(iface->ctrl_interface);
  1943. }
  1944. if (iface->driver_param) {
  1945. os_free(wpa_s->conf->driver_param);
  1946. wpa_s->conf->driver_param =
  1947. os_strdup(iface->driver_param);
  1948. }
  1949. } else
  1950. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  1951. iface->driver_param);
  1952. if (wpa_s->conf == NULL) {
  1953. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  1954. return -1;
  1955. }
  1956. if (iface->ifname == NULL) {
  1957. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  1958. return -1;
  1959. }
  1960. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  1961. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  1962. iface->ifname);
  1963. return -1;
  1964. }
  1965. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  1966. if (iface->bridge_ifname) {
  1967. if (os_strlen(iface->bridge_ifname) >=
  1968. sizeof(wpa_s->bridge_ifname)) {
  1969. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  1970. "name '%s'.", iface->bridge_ifname);
  1971. return -1;
  1972. }
  1973. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  1974. sizeof(wpa_s->bridge_ifname));
  1975. }
  1976. /* RSNA Supplicant Key Management - INITIALIZE */
  1977. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1978. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1979. /* Initialize driver interface and register driver event handler before
  1980. * L2 receive handler so that association events are processed before
  1981. * EAPOL-Key packets if both become available for the same select()
  1982. * call. */
  1983. driver = iface->driver;
  1984. next_driver:
  1985. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  1986. return -1;
  1987. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  1988. if (wpa_s->drv_priv == NULL) {
  1989. const char *pos;
  1990. pos = driver ? os_strchr(driver, ',') : NULL;
  1991. if (pos) {
  1992. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  1993. "driver interface - try next driver wrapper");
  1994. driver = pos + 1;
  1995. goto next_driver;
  1996. }
  1997. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  1998. "interface");
  1999. return -1;
  2000. }
  2001. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  2002. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  2003. "driver_param '%s'", wpa_s->conf->driver_param);
  2004. return -1;
  2005. }
  2006. ifname = wpa_drv_get_ifname(wpa_s);
  2007. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  2008. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  2009. "interface name with '%s'", ifname);
  2010. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  2011. }
  2012. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  2013. return -1;
  2014. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  2015. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  2016. NULL);
  2017. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  2018. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  2019. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  2020. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  2021. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2022. "dot11RSNAConfigPMKLifetime");
  2023. return -1;
  2024. }
  2025. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  2026. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  2027. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  2028. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2029. "dot11RSNAConfigPMKReauthThreshold");
  2030. return -1;
  2031. }
  2032. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  2033. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  2034. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  2035. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2036. "dot11RSNAConfigSATimeout");
  2037. return -1;
  2038. }
  2039. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  2040. &wpa_s->hw.num_modes,
  2041. &wpa_s->hw.flags);
  2042. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  2043. wpa_s->drv_capa_known = 1;
  2044. wpa_s->drv_flags = capa.flags;
  2045. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  2046. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  2047. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  2048. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  2049. wpa_s->max_match_sets = capa.max_match_sets;
  2050. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  2051. wpa_s->max_stations = capa.max_stations;
  2052. }
  2053. if (wpa_s->max_remain_on_chan == 0)
  2054. wpa_s->max_remain_on_chan = 1000;
  2055. if (wpa_supplicant_driver_init(wpa_s) < 0)
  2056. return -1;
  2057. #ifdef CONFIG_TDLS
  2058. if (wpa_tdls_init(wpa_s->wpa))
  2059. return -1;
  2060. #endif /* CONFIG_TDLS */
  2061. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  2062. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  2063. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  2064. return -1;
  2065. }
  2066. if (wpas_wps_init(wpa_s))
  2067. return -1;
  2068. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  2069. return -1;
  2070. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2071. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  2072. if (wpa_s->ctrl_iface == NULL) {
  2073. wpa_printf(MSG_ERROR,
  2074. "Failed to initialize control interface '%s'.\n"
  2075. "You may have another wpa_supplicant process "
  2076. "already running or the file was\n"
  2077. "left by an unclean termination of wpa_supplicant "
  2078. "in which case you will need\n"
  2079. "to manually remove this file before starting "
  2080. "wpa_supplicant again.\n",
  2081. wpa_s->conf->ctrl_interface);
  2082. return -1;
  2083. }
  2084. wpa_s->gas = gas_query_init(wpa_s);
  2085. if (wpa_s->gas == NULL) {
  2086. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  2087. return -1;
  2088. }
  2089. #ifdef CONFIG_P2P
  2090. if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  2091. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  2092. return -1;
  2093. }
  2094. #endif /* CONFIG_P2P */
  2095. if (wpa_bss_init(wpa_s) < 0)
  2096. return -1;
  2097. return 0;
  2098. }
  2099. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  2100. int notify, int terminate)
  2101. {
  2102. if (wpa_s->drv_priv) {
  2103. wpa_supplicant_deauthenticate(wpa_s,
  2104. WLAN_REASON_DEAUTH_LEAVING);
  2105. wpa_drv_set_countermeasures(wpa_s, 0);
  2106. wpa_clear_keys(wpa_s, NULL);
  2107. }
  2108. wpa_supplicant_cleanup(wpa_s);
  2109. if (notify)
  2110. wpas_notify_iface_removed(wpa_s);
  2111. if (wpa_s->drv_priv)
  2112. wpa_drv_deinit(wpa_s);
  2113. }
  2114. /**
  2115. * wpa_supplicant_add_iface - Add a new network interface
  2116. * @global: Pointer to global data from wpa_supplicant_init()
  2117. * @iface: Interface configuration options
  2118. * Returns: Pointer to the created interface or %NULL on failure
  2119. *
  2120. * This function is used to add new network interfaces for %wpa_supplicant.
  2121. * This can be called before wpa_supplicant_run() to add interfaces before the
  2122. * main event loop has been started. In addition, new interfaces can be added
  2123. * dynamically while %wpa_supplicant is already running. This could happen,
  2124. * e.g., when a hotplug network adapter is inserted.
  2125. */
  2126. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  2127. struct wpa_interface *iface)
  2128. {
  2129. struct wpa_supplicant *wpa_s;
  2130. struct wpa_interface t_iface;
  2131. struct wpa_ssid *ssid;
  2132. if (global == NULL || iface == NULL)
  2133. return NULL;
  2134. wpa_s = wpa_supplicant_alloc();
  2135. if (wpa_s == NULL)
  2136. return NULL;
  2137. wpa_s->global = global;
  2138. t_iface = *iface;
  2139. if (global->params.override_driver) {
  2140. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  2141. "('%s' -> '%s')",
  2142. iface->driver, global->params.override_driver);
  2143. t_iface.driver = global->params.override_driver;
  2144. }
  2145. if (global->params.override_ctrl_interface) {
  2146. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  2147. "ctrl_interface ('%s' -> '%s')",
  2148. iface->ctrl_interface,
  2149. global->params.override_ctrl_interface);
  2150. t_iface.ctrl_interface =
  2151. global->params.override_ctrl_interface;
  2152. }
  2153. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  2154. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  2155. iface->ifname);
  2156. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  2157. os_free(wpa_s);
  2158. return NULL;
  2159. }
  2160. /* Notify the control interfaces about new iface */
  2161. if (wpas_notify_iface_added(wpa_s)) {
  2162. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  2163. os_free(wpa_s);
  2164. return NULL;
  2165. }
  2166. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2167. wpas_notify_network_added(wpa_s, ssid);
  2168. wpa_s->next = global->ifaces;
  2169. global->ifaces = wpa_s;
  2170. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  2171. return wpa_s;
  2172. }
  2173. /**
  2174. * wpa_supplicant_remove_iface - Remove a network interface
  2175. * @global: Pointer to global data from wpa_supplicant_init()
  2176. * @wpa_s: Pointer to the network interface to be removed
  2177. * Returns: 0 if interface was removed, -1 if interface was not found
  2178. *
  2179. * This function can be used to dynamically remove network interfaces from
  2180. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  2181. * addition, this function is used to remove all remaining interfaces when
  2182. * %wpa_supplicant is terminated.
  2183. */
  2184. int wpa_supplicant_remove_iface(struct wpa_global *global,
  2185. struct wpa_supplicant *wpa_s,
  2186. int terminate)
  2187. {
  2188. struct wpa_supplicant *prev;
  2189. /* Remove interface from the global list of interfaces */
  2190. prev = global->ifaces;
  2191. if (prev == wpa_s) {
  2192. global->ifaces = wpa_s->next;
  2193. } else {
  2194. while (prev && prev->next != wpa_s)
  2195. prev = prev->next;
  2196. if (prev == NULL)
  2197. return -1;
  2198. prev->next = wpa_s->next;
  2199. }
  2200. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  2201. if (global->p2p_group_formation == wpa_s)
  2202. global->p2p_group_formation = NULL;
  2203. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  2204. os_free(wpa_s);
  2205. return 0;
  2206. }
  2207. /**
  2208. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  2209. * @wpa_s: Pointer to the network interface
  2210. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  2211. */
  2212. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  2213. {
  2214. const char *eapol_method;
  2215. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  2216. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2217. return "NO-EAP";
  2218. }
  2219. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  2220. if (eapol_method == NULL)
  2221. return "UNKNOWN-EAP";
  2222. return eapol_method;
  2223. }
  2224. /**
  2225. * wpa_supplicant_get_iface - Get a new network interface
  2226. * @global: Pointer to global data from wpa_supplicant_init()
  2227. * @ifname: Interface name
  2228. * Returns: Pointer to the interface or %NULL if not found
  2229. */
  2230. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  2231. const char *ifname)
  2232. {
  2233. struct wpa_supplicant *wpa_s;
  2234. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2235. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2236. return wpa_s;
  2237. }
  2238. return NULL;
  2239. }
  2240. #ifndef CONFIG_NO_WPA_MSG
  2241. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  2242. {
  2243. struct wpa_supplicant *wpa_s = ctx;
  2244. if (wpa_s == NULL)
  2245. return NULL;
  2246. return wpa_s->ifname;
  2247. }
  2248. #endif /* CONFIG_NO_WPA_MSG */
  2249. /**
  2250. * wpa_supplicant_init - Initialize %wpa_supplicant
  2251. * @params: Parameters for %wpa_supplicant
  2252. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  2253. *
  2254. * This function is used to initialize %wpa_supplicant. After successful
  2255. * initialization, the returned data pointer can be used to add and remove
  2256. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  2257. */
  2258. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  2259. {
  2260. struct wpa_global *global;
  2261. int ret, i;
  2262. if (params == NULL)
  2263. return NULL;
  2264. #ifdef CONFIG_DRIVER_NDIS
  2265. {
  2266. void driver_ndis_init_ops(void);
  2267. driver_ndis_init_ops();
  2268. }
  2269. #endif /* CONFIG_DRIVER_NDIS */
  2270. #ifndef CONFIG_NO_WPA_MSG
  2271. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  2272. #endif /* CONFIG_NO_WPA_MSG */
  2273. wpa_debug_open_file(params->wpa_debug_file_path);
  2274. if (params->wpa_debug_syslog)
  2275. wpa_debug_open_syslog();
  2276. ret = eap_register_methods();
  2277. if (ret) {
  2278. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  2279. if (ret == -2)
  2280. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  2281. "the same EAP type.");
  2282. return NULL;
  2283. }
  2284. global = os_zalloc(sizeof(*global));
  2285. if (global == NULL)
  2286. return NULL;
  2287. dl_list_init(&global->p2p_srv_bonjour);
  2288. dl_list_init(&global->p2p_srv_upnp);
  2289. global->params.daemonize = params->daemonize;
  2290. global->params.wait_for_monitor = params->wait_for_monitor;
  2291. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  2292. if (params->pid_file)
  2293. global->params.pid_file = os_strdup(params->pid_file);
  2294. if (params->ctrl_interface)
  2295. global->params.ctrl_interface =
  2296. os_strdup(params->ctrl_interface);
  2297. if (params->override_driver)
  2298. global->params.override_driver =
  2299. os_strdup(params->override_driver);
  2300. if (params->override_ctrl_interface)
  2301. global->params.override_ctrl_interface =
  2302. os_strdup(params->override_ctrl_interface);
  2303. wpa_debug_level = global->params.wpa_debug_level =
  2304. params->wpa_debug_level;
  2305. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  2306. params->wpa_debug_show_keys;
  2307. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  2308. params->wpa_debug_timestamp;
  2309. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  2310. if (eloop_init()) {
  2311. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  2312. wpa_supplicant_deinit(global);
  2313. return NULL;
  2314. }
  2315. random_init(params->entropy_file);
  2316. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  2317. if (global->ctrl_iface == NULL) {
  2318. wpa_supplicant_deinit(global);
  2319. return NULL;
  2320. }
  2321. if (wpas_notify_supplicant_initialized(global)) {
  2322. wpa_supplicant_deinit(global);
  2323. return NULL;
  2324. }
  2325. for (i = 0; wpa_drivers[i]; i++)
  2326. global->drv_count++;
  2327. if (global->drv_count == 0) {
  2328. wpa_printf(MSG_ERROR, "No drivers enabled");
  2329. wpa_supplicant_deinit(global);
  2330. return NULL;
  2331. }
  2332. global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
  2333. if (global->drv_priv == NULL) {
  2334. wpa_supplicant_deinit(global);
  2335. return NULL;
  2336. }
  2337. return global;
  2338. }
  2339. /**
  2340. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  2341. * @global: Pointer to global data from wpa_supplicant_init()
  2342. * Returns: 0 after successful event loop run, -1 on failure
  2343. *
  2344. * This function starts the main event loop and continues running as long as
  2345. * there are any remaining events. In most cases, this function is running as
  2346. * long as the %wpa_supplicant process in still in use.
  2347. */
  2348. int wpa_supplicant_run(struct wpa_global *global)
  2349. {
  2350. struct wpa_supplicant *wpa_s;
  2351. if (global->params.daemonize &&
  2352. wpa_supplicant_daemon(global->params.pid_file))
  2353. return -1;
  2354. if (global->params.wait_for_monitor) {
  2355. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  2356. if (wpa_s->ctrl_iface)
  2357. wpa_supplicant_ctrl_iface_wait(
  2358. wpa_s->ctrl_iface);
  2359. }
  2360. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  2361. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  2362. eloop_run();
  2363. return 0;
  2364. }
  2365. /**
  2366. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  2367. * @global: Pointer to global data from wpa_supplicant_init()
  2368. *
  2369. * This function is called to deinitialize %wpa_supplicant and to free all
  2370. * allocated resources. Remaining network interfaces will also be removed.
  2371. */
  2372. void wpa_supplicant_deinit(struct wpa_global *global)
  2373. {
  2374. int i;
  2375. if (global == NULL)
  2376. return;
  2377. #ifdef CONFIG_P2P
  2378. wpas_p2p_deinit_global(global);
  2379. #endif /* CONFIG_P2P */
  2380. while (global->ifaces)
  2381. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  2382. if (global->ctrl_iface)
  2383. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  2384. wpas_notify_supplicant_deinitialized(global);
  2385. eap_peer_unregister_methods();
  2386. #ifdef CONFIG_AP
  2387. eap_server_unregister_methods();
  2388. #endif /* CONFIG_AP */
  2389. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  2390. if (!global->drv_priv[i])
  2391. continue;
  2392. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  2393. }
  2394. os_free(global->drv_priv);
  2395. random_deinit();
  2396. eloop_destroy();
  2397. if (global->params.pid_file) {
  2398. os_daemonize_terminate(global->params.pid_file);
  2399. os_free(global->params.pid_file);
  2400. }
  2401. os_free(global->params.ctrl_interface);
  2402. os_free(global->params.override_driver);
  2403. os_free(global->params.override_ctrl_interface);
  2404. os_free(global);
  2405. wpa_debug_close_syslog();
  2406. wpa_debug_close_file();
  2407. }
  2408. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  2409. {
  2410. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  2411. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2412. char country[3];
  2413. country[0] = wpa_s->conf->country[0];
  2414. country[1] = wpa_s->conf->country[1];
  2415. country[2] = '\0';
  2416. if (wpa_drv_set_country(wpa_s, country) < 0) {
  2417. wpa_printf(MSG_ERROR, "Failed to set country code "
  2418. "'%s'", country);
  2419. }
  2420. }
  2421. #ifdef CONFIG_WPS
  2422. wpas_wps_update_config(wpa_s);
  2423. #endif /* CONFIG_WPS */
  2424. #ifdef CONFIG_P2P
  2425. wpas_p2p_update_config(wpa_s);
  2426. #endif /* CONFIG_P2P */
  2427. wpa_s->conf->changed_parameters = 0;
  2428. }
  2429. static void add_freq(int *freqs, int *num_freqs, int freq)
  2430. {
  2431. int i;
  2432. for (i = 0; i < *num_freqs; i++) {
  2433. if (freqs[i] == freq)
  2434. return;
  2435. }
  2436. freqs[*num_freqs] = freq;
  2437. (*num_freqs)++;
  2438. }
  2439. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  2440. {
  2441. struct wpa_bss *bss, *cbss;
  2442. const int max_freqs = 10;
  2443. int *freqs;
  2444. int num_freqs = 0;
  2445. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  2446. if (freqs == NULL)
  2447. return NULL;
  2448. cbss = wpa_s->current_bss;
  2449. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2450. if (bss == cbss)
  2451. continue;
  2452. if (bss->ssid_len == cbss->ssid_len &&
  2453. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  2454. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  2455. add_freq(freqs, &num_freqs, bss->freq);
  2456. if (num_freqs == max_freqs)
  2457. break;
  2458. }
  2459. }
  2460. if (num_freqs == 0) {
  2461. os_free(freqs);
  2462. freqs = NULL;
  2463. }
  2464. return freqs;
  2465. }
  2466. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2467. {
  2468. int timeout;
  2469. int count;
  2470. int *freqs = NULL;
  2471. /*
  2472. * Remove possible authentication timeout since the connection failed.
  2473. */
  2474. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  2475. /*
  2476. * Add the failed BSSID into the blacklist and speed up next scan
  2477. * attempt if there could be other APs that could accept association.
  2478. * The current blacklist count indicates how many times we have tried
  2479. * connecting to this AP and multiple attempts mean that other APs are
  2480. * either not available or has already been tried, so that we can start
  2481. * increasing the delay here to avoid constant scanning.
  2482. */
  2483. count = wpa_blacklist_add(wpa_s, bssid);
  2484. if (count == 1 && wpa_s->current_bss) {
  2485. /*
  2486. * This BSS was not in the blacklist before. If there is
  2487. * another BSS available for the same ESS, we should try that
  2488. * next. Otherwise, we may as well try this one once more
  2489. * before allowing other, likely worse, ESSes to be considered.
  2490. */
  2491. freqs = get_bss_freqs_in_ess(wpa_s);
  2492. if (freqs) {
  2493. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  2494. "has been seen; try it next");
  2495. wpa_blacklist_add(wpa_s, bssid);
  2496. /*
  2497. * On the next scan, go through only the known channels
  2498. * used in this ESS based on previous scans to speed up
  2499. * common load balancing use case.
  2500. */
  2501. os_free(wpa_s->next_scan_freqs);
  2502. wpa_s->next_scan_freqs = freqs;
  2503. }
  2504. }
  2505. switch (count) {
  2506. case 1:
  2507. timeout = 100;
  2508. break;
  2509. case 2:
  2510. timeout = 500;
  2511. break;
  2512. case 3:
  2513. timeout = 1000;
  2514. break;
  2515. default:
  2516. timeout = 5000;
  2517. }
  2518. /*
  2519. * TODO: if more than one possible AP is available in scan results,
  2520. * could try the other ones before requesting a new scan.
  2521. */
  2522. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  2523. 1000 * (timeout % 1000));
  2524. }
  2525. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  2526. {
  2527. return wpa_s->conf->ap_scan == 2 ||
  2528. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  2529. }