wpa_supplicant.c 74 KB

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