wpa_supplicant.c 77 KB

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