wpa_supplicant.c 83 KB

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