wpa_supplicant.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2008, 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 "wpa.h"
  23. #include "eloop.h"
  24. #include "drivers/driver.h"
  25. #include "config.h"
  26. #include "l2_packet/l2_packet.h"
  27. #include "wpa_supplicant_i.h"
  28. #include "ctrl_iface.h"
  29. #include "ctrl_iface_dbus.h"
  30. #include "pcsc_funcs.h"
  31. #include "version.h"
  32. #include "preauth.h"
  33. #include "pmksa_cache.h"
  34. #include "wpa_ctrl.h"
  35. #include "mlme.h"
  36. #include "ieee802_11_defs.h"
  37. #include "blacklist.h"
  38. #include "wpas_glue.h"
  39. #include "wps/wps.h"
  40. #include "wps_supplicant.h"
  41. const char *wpa_supplicant_version =
  42. "wpa_supplicant v" VERSION_STR "\n"
  43. "Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi> and contributors";
  44. const char *wpa_supplicant_license =
  45. "This program is free software. You can distribute it and/or modify it\n"
  46. "under the terms of the GNU General Public License version 2.\n"
  47. "\n"
  48. "Alternatively, this software may be distributed under the terms of the\n"
  49. "BSD license. See README and COPYING for more details.\n"
  50. #ifdef EAP_TLS_OPENSSL
  51. "\nThis product includes software developed by the OpenSSL Project\n"
  52. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  53. #endif /* EAP_TLS_OPENSSL */
  54. ;
  55. #ifndef CONFIG_NO_STDOUT_DEBUG
  56. /* Long text divided into parts in order to fit in C89 strings size limits. */
  57. const char *wpa_supplicant_full_license1 =
  58. "This program is free software; you can redistribute it and/or modify\n"
  59. "it under the terms of the GNU General Public License version 2 as\n"
  60. "published by the Free Software Foundation.\n"
  61. "\n"
  62. "This program is distributed in the hope that it will be useful,\n"
  63. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  64. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  65. "GNU General Public License for more details.\n"
  66. "\n";
  67. const char *wpa_supplicant_full_license2 =
  68. "You should have received a copy of the GNU General Public License\n"
  69. "along with this program; if not, write to the Free Software\n"
  70. "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
  71. "\n"
  72. "Alternatively, this software may be distributed under the terms of the\n"
  73. "BSD license.\n"
  74. "\n"
  75. "Redistribution and use in source and binary forms, with or without\n"
  76. "modification, are permitted provided that the following conditions are\n"
  77. "met:\n"
  78. "\n";
  79. const char *wpa_supplicant_full_license3 =
  80. "1. Redistributions of source code must retain the above copyright\n"
  81. " notice, this list of conditions and the following disclaimer.\n"
  82. "\n"
  83. "2. Redistributions in binary form must reproduce the above copyright\n"
  84. " notice, this list of conditions and the following disclaimer in the\n"
  85. " documentation and/or other materials provided with the distribution.\n"
  86. "\n";
  87. const char *wpa_supplicant_full_license4 =
  88. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  89. " names of its contributors may be used to endorse or promote products\n"
  90. " derived from this software without specific prior written permission.\n"
  91. "\n"
  92. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  93. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  94. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  95. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  96. const char *wpa_supplicant_full_license5 =
  97. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  98. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  99. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  100. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  101. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  102. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  103. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  104. "\n";
  105. #endif /* CONFIG_NO_STDOUT_DEBUG */
  106. extern struct wpa_driver_ops *wpa_supplicant_drivers[];
  107. extern int wpa_debug_level;
  108. extern int wpa_debug_show_keys;
  109. extern int wpa_debug_timestamp;
  110. /* Configure default/group WEP keys for static WEP */
  111. static int wpa_set_wep_keys(struct wpa_supplicant *wpa_s,
  112. struct wpa_ssid *ssid)
  113. {
  114. int i, set = 0;
  115. for (i = 0; i < NUM_WEP_KEYS; i++) {
  116. if (ssid->wep_key_len[i] == 0)
  117. continue;
  118. set = 1;
  119. wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
  120. (u8 *) "\xff\xff\xff\xff\xff\xff",
  121. i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
  122. ssid->wep_key[i], ssid->wep_key_len[i]);
  123. }
  124. return set;
  125. }
  126. static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  127. struct wpa_ssid *ssid)
  128. {
  129. u8 key[32];
  130. size_t keylen;
  131. wpa_alg alg;
  132. u8 seq[6] = { 0 };
  133. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  134. * sending unicast and multicast packets. */
  135. if (ssid->mode != IEEE80211_MODE_IBSS) {
  136. wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
  137. "for WPA-None", ssid->mode);
  138. return -1;
  139. }
  140. if (!ssid->psk_set) {
  141. wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
  142. return -1;
  143. }
  144. switch (wpa_s->group_cipher) {
  145. case WPA_CIPHER_CCMP:
  146. os_memcpy(key, ssid->psk, 16);
  147. keylen = 16;
  148. alg = WPA_ALG_CCMP;
  149. break;
  150. case WPA_CIPHER_TKIP:
  151. /* WPA-None uses the same Michael MIC key for both TX and RX */
  152. os_memcpy(key, ssid->psk, 16 + 8);
  153. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  154. keylen = 32;
  155. alg = WPA_ALG_TKIP;
  156. break;
  157. default:
  158. wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
  159. "WPA-None", wpa_s->group_cipher);
  160. return -1;
  161. }
  162. /* TODO: should actually remember the previously used seq#, both for TX
  163. * and RX from each STA.. */
  164. return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
  165. 0, 1, seq, 6, key, keylen);
  166. }
  167. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  168. {
  169. struct wpa_supplicant *wpa_s = eloop_ctx;
  170. const u8 *bssid = wpa_s->bssid;
  171. if (is_zero_ether_addr(bssid))
  172. bssid = wpa_s->pending_bssid;
  173. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  174. MAC2STR(bssid));
  175. wpa_blacklist_add(wpa_s, bssid);
  176. wpa_sm_notify_disassoc(wpa_s->wpa);
  177. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  178. wpa_s->reassociate = 1;
  179. wpa_supplicant_req_scan(wpa_s, 0, 0);
  180. }
  181. /**
  182. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  183. * @wpa_s: Pointer to wpa_supplicant data
  184. * @sec: Number of seconds after which to time out authentication
  185. * @usec: Number of microseconds after which to time out authentication
  186. *
  187. * This function is used to schedule a timeout for the current authentication
  188. * attempt.
  189. */
  190. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  191. int sec, int usec)
  192. {
  193. if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
  194. wpa_s->driver && IS_WIRED(wpa_s->driver))
  195. return;
  196. wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  197. "%d usec", sec, usec);
  198. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  199. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  200. }
  201. /**
  202. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  203. * @wpa_s: Pointer to wpa_supplicant data
  204. *
  205. * This function is used to cancel authentication timeout scheduled with
  206. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  207. * been completed.
  208. */
  209. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  210. {
  211. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  212. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  213. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  214. }
  215. /**
  216. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  217. * @wpa_s: Pointer to wpa_supplicant data
  218. *
  219. * This function is used to configure EAPOL state machine based on the selected
  220. * authentication mode.
  221. */
  222. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  223. {
  224. #ifdef IEEE8021X_EAPOL
  225. struct eapol_config eapol_conf;
  226. struct wpa_ssid *ssid = wpa_s->current_ssid;
  227. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  228. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  229. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  230. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  231. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  232. else
  233. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  234. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  235. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  236. eapol_conf.accept_802_1x_keys = 1;
  237. eapol_conf.required_keys = 0;
  238. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  239. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  240. }
  241. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  242. eapol_conf.required_keys |=
  243. EAPOL_REQUIRE_KEY_BROADCAST;
  244. }
  245. if (wpa_s->conf && wpa_s->driver && IS_WIRED(wpa_s->driver)) {
  246. eapol_conf.required_keys = 0;
  247. }
  248. }
  249. if (wpa_s->conf)
  250. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  251. eapol_conf.workaround = ssid->eap_workaround;
  252. eapol_conf.eap_disabled =
  253. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  254. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  255. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  256. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  257. #endif /* IEEE8021X_EAPOL */
  258. }
  259. /**
  260. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  261. * @wpa_s: Pointer to wpa_supplicant data
  262. * @ssid: Configuration data for the network
  263. *
  264. * This function is used to configure WPA state machine and related parameters
  265. * to a mode where WPA is not enabled. This is called as part of the
  266. * authentication configuration when the selected network does not use WPA.
  267. */
  268. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  269. struct wpa_ssid *ssid)
  270. {
  271. int i;
  272. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  273. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  274. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  275. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  276. else
  277. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  278. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  279. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  280. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  281. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  282. wpa_s->group_cipher = WPA_CIPHER_NONE;
  283. wpa_s->mgmt_group_cipher = 0;
  284. for (i = 0; i < NUM_WEP_KEYS; i++) {
  285. if (ssid->wep_key_len[i] > 5) {
  286. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  287. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  288. break;
  289. } else if (ssid->wep_key_len[i] > 0) {
  290. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  291. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  292. break;
  293. }
  294. }
  295. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  296. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  297. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  298. wpa_s->pairwise_cipher);
  299. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  300. #ifdef CONFIG_IEEE80211W
  301. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  302. wpa_s->mgmt_group_cipher);
  303. #endif /* CONFIG_IEEE80211W */
  304. pmksa_cache_clear_current(wpa_s->wpa);
  305. }
  306. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  307. {
  308. scard_deinit(wpa_s->scard);
  309. wpa_s->scard = NULL;
  310. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  311. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  312. l2_packet_deinit(wpa_s->l2);
  313. wpa_s->l2 = NULL;
  314. if (wpa_s->l2_br) {
  315. l2_packet_deinit(wpa_s->l2_br);
  316. wpa_s->l2_br = NULL;
  317. }
  318. if (wpa_s->ctrl_iface) {
  319. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  320. wpa_s->ctrl_iface = NULL;
  321. }
  322. if (wpa_s->conf != NULL) {
  323. wpa_config_free(wpa_s->conf);
  324. wpa_s->conf = NULL;
  325. }
  326. os_free(wpa_s->confname);
  327. wpa_s->confname = NULL;
  328. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  329. eapol_sm_deinit(wpa_s->eapol);
  330. wpa_s->eapol = NULL;
  331. rsn_preauth_deinit(wpa_s->wpa);
  332. pmksa_candidate_free(wpa_s->wpa);
  333. wpa_sm_deinit(wpa_s->wpa);
  334. wpa_s->wpa = NULL;
  335. wpa_blacklist_clear(wpa_s);
  336. wpa_scan_results_free(wpa_s->scan_res);
  337. wpa_s->scan_res = NULL;
  338. wpa_supplicant_cancel_scan(wpa_s);
  339. wpa_supplicant_cancel_auth_timeout(wpa_s);
  340. ieee80211_sta_deinit(wpa_s);
  341. wpas_wps_deinit(wpa_s);
  342. }
  343. /**
  344. * wpa_clear_keys - Clear keys configured for the driver
  345. * @wpa_s: Pointer to wpa_supplicant data
  346. * @addr: Previously used BSSID or %NULL if not available
  347. *
  348. * This function clears the encryption keys that has been previously configured
  349. * for the driver.
  350. */
  351. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  352. {
  353. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  354. if (wpa_s->keys_cleared) {
  355. /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
  356. * timing issues with keys being cleared just before new keys
  357. * are set or just after association or something similar. This
  358. * shows up in group key handshake failing often because of the
  359. * client not receiving the first encrypted packets correctly.
  360. * Skipping some of the extra key clearing steps seems to help
  361. * in completing group key handshake more reliably. */
  362. wpa_printf(MSG_DEBUG, "No keys have been configured - "
  363. "skip key clearing");
  364. return;
  365. }
  366. /* MLME-DELETEKEYS.request */
  367. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  368. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  369. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  370. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  371. if (addr) {
  372. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  373. 0);
  374. /* MLME-SETPROTECTION.request(None) */
  375. wpa_drv_mlme_setprotection(
  376. wpa_s, addr,
  377. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  378. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  379. }
  380. wpa_s->keys_cleared = 1;
  381. }
  382. /**
  383. * wpa_supplicant_state_txt - Get the connection state name as a text string
  384. * @state: State (wpa_state; WPA_*)
  385. * Returns: The state name as a printable text string
  386. */
  387. const char * wpa_supplicant_state_txt(int state)
  388. {
  389. switch (state) {
  390. case WPA_DISCONNECTED:
  391. return "DISCONNECTED";
  392. case WPA_INACTIVE:
  393. return "INACTIVE";
  394. case WPA_SCANNING:
  395. return "SCANNING";
  396. case WPA_ASSOCIATING:
  397. return "ASSOCIATING";
  398. case WPA_ASSOCIATED:
  399. return "ASSOCIATED";
  400. case WPA_4WAY_HANDSHAKE:
  401. return "4WAY_HANDSHAKE";
  402. case WPA_GROUP_HANDSHAKE:
  403. return "GROUP_HANDSHAKE";
  404. case WPA_COMPLETED:
  405. return "COMPLETED";
  406. default:
  407. return "UNKNOWN";
  408. }
  409. }
  410. /**
  411. * wpa_supplicant_set_state - Set current connection state
  412. * @wpa_s: Pointer to wpa_supplicant data
  413. * @state: The new connection state
  414. *
  415. * This function is called whenever the connection state changes, e.g.,
  416. * association is completed for WPA/WPA2 4-Way Handshake is started.
  417. */
  418. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
  419. {
  420. wpa_printf(MSG_DEBUG, "State: %s -> %s",
  421. wpa_supplicant_state_txt(wpa_s->wpa_state),
  422. wpa_supplicant_state_txt(state));
  423. wpa_supplicant_dbus_notify_state_change(wpa_s, state,
  424. wpa_s->wpa_state);
  425. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  426. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  427. struct wpa_ssid *ssid = wpa_s->current_ssid;
  428. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  429. MACSTR " completed %s [id=%d id_str=%s]",
  430. MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
  431. "(reauth)" : "(auth)",
  432. ssid ? ssid->id : -1,
  433. ssid && ssid->id_str ? ssid->id_str : "");
  434. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  435. wpa_s->new_connection = 0;
  436. wpa_s->reassociated_connection = 1;
  437. wpa_drv_set_operstate(wpa_s, 1);
  438. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  439. state == WPA_ASSOCIATED) {
  440. wpa_s->new_connection = 1;
  441. wpa_drv_set_operstate(wpa_s, 0);
  442. }
  443. wpa_s->wpa_state = state;
  444. }
  445. static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
  446. void *signal_ctx)
  447. {
  448. struct wpa_global *global = eloop_ctx;
  449. struct wpa_supplicant *wpa_s;
  450. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  451. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
  452. "received", sig);
  453. }
  454. eloop_terminate();
  455. }
  456. static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  457. {
  458. wpa_s->pairwise_cipher = 0;
  459. wpa_s->group_cipher = 0;
  460. wpa_s->mgmt_group_cipher = 0;
  461. wpa_s->key_mgmt = 0;
  462. wpa_s->wpa_state = WPA_DISCONNECTED;
  463. }
  464. /**
  465. * wpa_supplicant_reload_configuration - Reload configuration data
  466. * @wpa_s: Pointer to wpa_supplicant data
  467. * Returns: 0 on success or -1 if configuration parsing failed
  468. *
  469. * This function can be used to request that the configuration data is reloaded
  470. * (e.g., after configuration file change). This function is reloading
  471. * configuration only for one interface, so this may need to be called multiple
  472. * times if %wpa_supplicant is controlling multiple interfaces and all
  473. * interfaces need reconfiguration.
  474. */
  475. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  476. {
  477. struct wpa_config *conf;
  478. int reconf_ctrl;
  479. if (wpa_s->confname == NULL)
  480. return -1;
  481. conf = wpa_config_read(wpa_s->confname);
  482. if (conf == NULL) {
  483. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  484. "file '%s' - exiting", wpa_s->confname);
  485. return -1;
  486. }
  487. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  488. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  489. os_strcmp(conf->ctrl_interface,
  490. wpa_s->conf->ctrl_interface) != 0);
  491. if (reconf_ctrl && wpa_s->ctrl_iface) {
  492. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  493. wpa_s->ctrl_iface = NULL;
  494. }
  495. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  496. wpa_s->current_ssid = NULL;
  497. /*
  498. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  499. * pkcs11_engine_path, pkcs11_module_path.
  500. */
  501. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  502. /*
  503. * Clear forced success to clear EAP state for next
  504. * authentication.
  505. */
  506. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  507. }
  508. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  509. wpa_sm_set_config(wpa_s->wpa, NULL);
  510. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  511. rsn_preauth_deinit(wpa_s->wpa);
  512. wpa_config_free(wpa_s->conf);
  513. wpa_s->conf = conf;
  514. if (reconf_ctrl)
  515. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  516. wpa_supplicant_clear_status(wpa_s);
  517. wpa_s->reassociate = 1;
  518. wpa_supplicant_req_scan(wpa_s, 0, 0);
  519. wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  520. return 0;
  521. }
  522. static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
  523. void *signal_ctx)
  524. {
  525. struct wpa_global *global = eloop_ctx;
  526. struct wpa_supplicant *wpa_s;
  527. wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
  528. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  529. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  530. eloop_terminate();
  531. }
  532. }
  533. }
  534. static wpa_cipher cipher_suite2driver(int cipher)
  535. {
  536. switch (cipher) {
  537. case WPA_CIPHER_NONE:
  538. return CIPHER_NONE;
  539. case WPA_CIPHER_WEP40:
  540. return CIPHER_WEP40;
  541. case WPA_CIPHER_WEP104:
  542. return CIPHER_WEP104;
  543. case WPA_CIPHER_CCMP:
  544. return CIPHER_CCMP;
  545. case WPA_CIPHER_TKIP:
  546. default:
  547. return CIPHER_TKIP;
  548. }
  549. }
  550. static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
  551. {
  552. switch (key_mgmt) {
  553. case WPA_KEY_MGMT_NONE:
  554. return KEY_MGMT_NONE;
  555. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  556. return KEY_MGMT_802_1X_NO_WPA;
  557. case WPA_KEY_MGMT_IEEE8021X:
  558. return KEY_MGMT_802_1X;
  559. case WPA_KEY_MGMT_WPA_NONE:
  560. return KEY_MGMT_WPA_NONE;
  561. case WPA_KEY_MGMT_FT_IEEE8021X:
  562. return KEY_MGMT_FT_802_1X;
  563. case WPA_KEY_MGMT_FT_PSK:
  564. return KEY_MGMT_FT_PSK;
  565. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  566. return KEY_MGMT_802_1X_SHA256;
  567. case WPA_KEY_MGMT_PSK_SHA256:
  568. return KEY_MGMT_PSK_SHA256;
  569. case WPA_KEY_MGMT_WPS:
  570. return KEY_MGMT_WPS;
  571. case WPA_KEY_MGMT_PSK:
  572. default:
  573. return KEY_MGMT_PSK;
  574. }
  575. }
  576. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  577. struct wpa_ssid *ssid,
  578. struct wpa_ie_data *ie)
  579. {
  580. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  581. if (ret) {
  582. if (ret == -2) {
  583. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  584. "from association info");
  585. }
  586. return -1;
  587. }
  588. wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
  589. "suites");
  590. if (!(ie->group_cipher & ssid->group_cipher)) {
  591. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  592. "cipher 0x%x (mask 0x%x) - reject",
  593. ie->group_cipher, ssid->group_cipher);
  594. return -1;
  595. }
  596. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  597. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  598. "cipher 0x%x (mask 0x%x) - reject",
  599. ie->pairwise_cipher, ssid->pairwise_cipher);
  600. return -1;
  601. }
  602. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  603. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  604. "management 0x%x (mask 0x%x) - reject",
  605. ie->key_mgmt, ssid->key_mgmt);
  606. return -1;
  607. }
  608. #ifdef CONFIG_IEEE80211W
  609. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  610. ssid->ieee80211w == IEEE80211W_REQUIRED) {
  611. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  612. "that does not support management frame protection - "
  613. "reject");
  614. return -1;
  615. }
  616. #endif /* CONFIG_IEEE80211W */
  617. return 0;
  618. }
  619. /**
  620. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  621. * @wpa_s: Pointer to wpa_supplicant data
  622. * @bss: Scan results for the selected BSS, or %NULL if not available
  623. * @ssid: Configuration data for the selected network
  624. * @wpa_ie: Buffer for the WPA/RSN IE
  625. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  626. * used buffer length in case the functions returns success.
  627. * Returns: 0 on success or -1 on failure
  628. *
  629. * This function is used to configure authentication and encryption parameters
  630. * based on the network configuration and scan result for the selected BSS (if
  631. * available).
  632. */
  633. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  634. struct wpa_scan_res *bss,
  635. struct wpa_ssid *ssid,
  636. u8 *wpa_ie, size_t *wpa_ie_len)
  637. {
  638. struct wpa_ie_data ie;
  639. int sel, proto;
  640. const u8 *bss_wpa, *bss_rsn;
  641. if (bss) {
  642. bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  643. bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  644. } else
  645. bss_wpa = bss_rsn = NULL;
  646. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  647. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  648. (ie.group_cipher & ssid->group_cipher) &&
  649. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  650. (ie.key_mgmt & ssid->key_mgmt)) {
  651. wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  652. proto = WPA_PROTO_RSN;
  653. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  654. wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
  655. (ie.group_cipher & ssid->group_cipher) &&
  656. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  657. (ie.key_mgmt & ssid->key_mgmt)) {
  658. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  659. proto = WPA_PROTO_WPA;
  660. } else if (bss) {
  661. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  662. return -1;
  663. } else {
  664. if (ssid->proto & WPA_PROTO_RSN)
  665. proto = WPA_PROTO_RSN;
  666. else
  667. proto = WPA_PROTO_WPA;
  668. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  669. os_memset(&ie, 0, sizeof(ie));
  670. ie.group_cipher = ssid->group_cipher;
  671. ie.pairwise_cipher = ssid->pairwise_cipher;
  672. ie.key_mgmt = ssid->key_mgmt;
  673. #ifdef CONFIG_IEEE80211W
  674. ie.mgmt_group_cipher =
  675. ssid->ieee80211w != NO_IEEE80211W ?
  676. WPA_CIPHER_AES_128_CMAC : 0;
  677. #endif /* CONFIG_IEEE80211W */
  678. wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
  679. "on configuration");
  680. } else
  681. proto = ie.proto;
  682. }
  683. wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  684. "pairwise %d key_mgmt %d proto %d",
  685. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  686. #ifdef CONFIG_IEEE80211W
  687. if (ssid->ieee80211w) {
  688. wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  689. ie.mgmt_group_cipher);
  690. }
  691. #endif /* CONFIG_IEEE80211W */
  692. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  693. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  694. !!(ssid->proto & WPA_PROTO_RSN));
  695. if (bss || !wpa_s->ap_ies_from_associnfo) {
  696. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  697. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  698. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  699. bss_rsn ? 2 + bss_rsn[1] : 0))
  700. return -1;
  701. }
  702. sel = ie.group_cipher & ssid->group_cipher;
  703. if (sel & WPA_CIPHER_CCMP) {
  704. wpa_s->group_cipher = WPA_CIPHER_CCMP;
  705. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
  706. } else if (sel & WPA_CIPHER_TKIP) {
  707. wpa_s->group_cipher = WPA_CIPHER_TKIP;
  708. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
  709. } else if (sel & WPA_CIPHER_WEP104) {
  710. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  711. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
  712. } else if (sel & WPA_CIPHER_WEP40) {
  713. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  714. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
  715. } else {
  716. wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
  717. return -1;
  718. }
  719. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  720. if (sel & WPA_CIPHER_CCMP) {
  721. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  722. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
  723. } else if (sel & WPA_CIPHER_TKIP) {
  724. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  725. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
  726. } else if (sel & WPA_CIPHER_NONE) {
  727. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  728. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
  729. } else {
  730. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  731. "cipher.");
  732. return -1;
  733. }
  734. sel = ie.key_mgmt & ssid->key_mgmt;
  735. if (0) {
  736. #ifdef CONFIG_IEEE80211R
  737. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  738. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  739. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  740. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  741. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  742. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  743. #endif /* CONFIG_IEEE80211R */
  744. #ifdef CONFIG_IEEE80211W
  745. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  746. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  747. wpa_msg(wpa_s, MSG_DEBUG,
  748. "WPA: using KEY_MGMT 802.1X with SHA256");
  749. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  750. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  751. wpa_msg(wpa_s, MSG_DEBUG,
  752. "WPA: using KEY_MGMT PSK with SHA256");
  753. #endif /* CONFIG_IEEE80211W */
  754. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  755. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  756. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  757. } else if (sel & WPA_KEY_MGMT_PSK) {
  758. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  759. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  760. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  761. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  762. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  763. } else {
  764. wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
  765. "key management type.");
  766. return -1;
  767. }
  768. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  769. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  770. wpa_s->pairwise_cipher);
  771. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  772. #ifdef CONFIG_IEEE80211W
  773. sel = ie.mgmt_group_cipher;
  774. if (ssid->ieee80211w == NO_IEEE80211W ||
  775. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  776. sel = 0;
  777. if (sel & WPA_CIPHER_AES_128_CMAC) {
  778. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  779. wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  780. "AES-128-CMAC");
  781. } else {
  782. wpa_s->mgmt_group_cipher = 0;
  783. wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  784. }
  785. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  786. wpa_s->mgmt_group_cipher);
  787. #endif /* CONFIG_IEEE80211W */
  788. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  789. wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
  790. return -1;
  791. }
  792. if (ssid->key_mgmt &
  793. (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
  794. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
  795. else
  796. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  797. return 0;
  798. }
  799. /**
  800. * wpa_supplicant_associate - Request association
  801. * @wpa_s: Pointer to wpa_supplicant data
  802. * @bss: Scan results for the selected BSS, or %NULL if not available
  803. * @ssid: Configuration data for the selected network
  804. *
  805. * This function is used to request %wpa_supplicant to associate with a BSS.
  806. */
  807. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  808. struct wpa_scan_res *bss, struct wpa_ssid *ssid)
  809. {
  810. u8 wpa_ie[80];
  811. size_t wpa_ie_len;
  812. int use_crypt, ret, i;
  813. int algs = AUTH_ALG_OPEN_SYSTEM;
  814. wpa_cipher cipher_pairwise, cipher_group;
  815. struct wpa_driver_associate_params params;
  816. int wep_keys_set = 0;
  817. struct wpa_driver_capa capa;
  818. int assoc_failed = 0;
  819. wpa_s->reassociate = 0;
  820. if (bss) {
  821. #ifdef CONFIG_IEEE80211R
  822. const u8 *md = NULL;
  823. #endif /* CONFIG_IEEE80211R */
  824. const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  825. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  826. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  827. ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
  828. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  829. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  830. #ifdef CONFIG_IEEE80211R
  831. ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  832. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  833. md = ie + 2;
  834. wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
  835. if (md) {
  836. /* Prepare for the next transition */
  837. wpa_ft_prepare_auth_request(wpa_s->wpa);
  838. }
  839. #endif /* CONFIG_IEEE80211R */
  840. } else {
  841. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  842. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  843. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  844. }
  845. wpa_supplicant_cancel_scan(wpa_s);
  846. /* Starting new association, so clear the possibly used WPA IE from the
  847. * previous association. */
  848. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  849. if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
  850. wpa_printf(MSG_WARNING, "Failed to set operating mode");
  851. assoc_failed = 1;
  852. }
  853. #ifdef IEEE8021X_EAPOL
  854. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  855. if (ssid->leap) {
  856. if (ssid->non_leap == 0)
  857. algs = AUTH_ALG_LEAP;
  858. else
  859. algs |= AUTH_ALG_LEAP;
  860. }
  861. }
  862. #endif /* IEEE8021X_EAPOL */
  863. wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  864. if (ssid->auth_alg) {
  865. algs = 0;
  866. if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
  867. algs |= AUTH_ALG_OPEN_SYSTEM;
  868. if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
  869. algs |= AUTH_ALG_SHARED_KEY;
  870. if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
  871. algs |= AUTH_ALG_LEAP;
  872. wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
  873. algs);
  874. }
  875. wpa_drv_set_auth_alg(wpa_s, algs);
  876. if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  877. wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
  878. (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  879. WPA_KEY_MGMT_FT_IEEE8021X |
  880. WPA_KEY_MGMT_FT_PSK |
  881. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  882. WPA_KEY_MGMT_PSK_SHA256))) {
  883. int try_opportunistic;
  884. try_opportunistic = ssid->proactive_key_caching &&
  885. (ssid->proto & WPA_PROTO_RSN);
  886. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  887. wpa_s->current_ssid,
  888. try_opportunistic) == 0)
  889. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  890. wpa_ie_len = sizeof(wpa_ie);
  891. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  892. wpa_ie, &wpa_ie_len)) {
  893. wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
  894. "management and encryption suites");
  895. return;
  896. }
  897. } else if (ssid->key_mgmt &
  898. (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  899. WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
  900. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
  901. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  902. wpa_ie_len = sizeof(wpa_ie);
  903. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  904. wpa_ie, &wpa_ie_len)) {
  905. wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
  906. "management and encryption suites (no scan "
  907. "results)");
  908. return;
  909. }
  910. #ifdef CONFIG_WPS
  911. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  912. struct wpabuf *wps_ie;
  913. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  914. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  915. wpa_ie_len = wpabuf_len(wps_ie);
  916. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  917. }
  918. wpabuf_free(wps_ie);
  919. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  920. #endif /* CONFIG_WPS */
  921. } else {
  922. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  923. wpa_ie_len = 0;
  924. }
  925. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  926. use_crypt = 1;
  927. cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
  928. cipher_group = cipher_suite2driver(wpa_s->group_cipher);
  929. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  930. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  931. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  932. use_crypt = 0;
  933. if (wpa_set_wep_keys(wpa_s, ssid)) {
  934. use_crypt = 1;
  935. wep_keys_set = 1;
  936. }
  937. }
  938. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  939. use_crypt = 0;
  940. #ifdef IEEE8021X_EAPOL
  941. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  942. if ((ssid->eapol_flags &
  943. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  944. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  945. !wep_keys_set) {
  946. use_crypt = 0;
  947. } else {
  948. /* Assume that dynamic WEP-104 keys will be used and
  949. * set cipher suites in order for drivers to expect
  950. * encryption. */
  951. cipher_pairwise = cipher_group = CIPHER_WEP104;
  952. }
  953. }
  954. #endif /* IEEE8021X_EAPOL */
  955. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  956. /* Set the key before (and later after) association */
  957. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  958. }
  959. wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
  960. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  961. os_memset(&params, 0, sizeof(params));
  962. if (bss) {
  963. const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  964. params.bssid = bss->bssid;
  965. params.ssid = ie ? ie + 2 : (u8 *) "";
  966. params.ssid_len = ie ? ie[1] : 0;
  967. params.freq = bss->freq;
  968. } else {
  969. params.ssid = ssid->ssid;
  970. params.ssid_len = ssid->ssid_len;
  971. }
  972. if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
  973. params.freq = ssid->frequency; /* Initial channel for IBSS */
  974. params.wpa_ie = wpa_ie;
  975. params.wpa_ie_len = wpa_ie_len;
  976. params.pairwise_suite = cipher_pairwise;
  977. params.group_suite = cipher_group;
  978. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  979. params.auth_alg = algs;
  980. params.mode = ssid->mode;
  981. for (i = 0; i < NUM_WEP_KEYS; i++) {
  982. if (ssid->wep_key_len[i])
  983. params.wep_key[i] = ssid->wep_key[i];
  984. params.wep_key_len[i] = ssid->wep_key_len[i];
  985. }
  986. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  987. if (wpa_s->driver_4way_handshake &&
  988. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  989. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  990. params.passphrase = ssid->passphrase;
  991. if (ssid->psk_set)
  992. params.psk = ssid->psk;
  993. }
  994. #ifdef CONFIG_IEEE80211W
  995. switch (ssid->ieee80211w) {
  996. case NO_IEEE80211W:
  997. params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
  998. break;
  999. case IEEE80211W_OPTIONAL:
  1000. params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
  1001. break;
  1002. case IEEE80211W_REQUIRED:
  1003. params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
  1004. break;
  1005. }
  1006. #endif /* CONFIG_IEEE80211W */
  1007. if (wpa_s->use_client_mlme)
  1008. ret = ieee80211_sta_associate(wpa_s, &params);
  1009. else
  1010. ret = wpa_drv_associate(wpa_s, &params);
  1011. if (ret < 0) {
  1012. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1013. "failed");
  1014. /* try to continue anyway; new association will be tried again
  1015. * after timeout */
  1016. assoc_failed = 1;
  1017. }
  1018. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1019. /* Set the key after the association just in case association
  1020. * cleared the previously configured key. */
  1021. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1022. /* No need to timeout authentication since there is no key
  1023. * management. */
  1024. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1025. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1026. } else {
  1027. /* Timeout for IEEE 802.11 authentication and association */
  1028. int timeout = 60;
  1029. if (assoc_failed) {
  1030. /* give IBSS a bit more time */
  1031. timeout = ssid->mode ? 10 : 5;
  1032. } else if (wpa_s->conf->ap_scan == 1) {
  1033. /* give IBSS a bit more time */
  1034. timeout = ssid->mode ? 20 : 10;
  1035. }
  1036. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1037. }
  1038. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1039. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1040. /* Set static WEP keys again */
  1041. wpa_set_wep_keys(wpa_s, ssid);
  1042. }
  1043. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1044. /*
  1045. * Do not allow EAP session resumption between different
  1046. * network configurations.
  1047. */
  1048. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1049. }
  1050. wpa_s->current_ssid = ssid;
  1051. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1052. wpa_supplicant_initiate_eapol(wpa_s);
  1053. }
  1054. /**
  1055. * wpa_supplicant_disassociate - Disassociate the current connection
  1056. * @wpa_s: Pointer to wpa_supplicant data
  1057. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1058. *
  1059. * This function is used to request %wpa_supplicant to disassociate with the
  1060. * current AP.
  1061. */
  1062. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1063. int reason_code)
  1064. {
  1065. u8 *addr = NULL;
  1066. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1067. if (wpa_s->use_client_mlme)
  1068. ieee80211_sta_disassociate(wpa_s, reason_code);
  1069. else
  1070. wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
  1071. addr = wpa_s->bssid;
  1072. }
  1073. wpa_clear_keys(wpa_s, addr);
  1074. wpa_supplicant_mark_disassoc(wpa_s);
  1075. wpa_s->current_ssid = NULL;
  1076. wpa_sm_set_config(wpa_s->wpa, NULL);
  1077. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1078. }
  1079. /**
  1080. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1081. * @wpa_s: Pointer to wpa_supplicant data
  1082. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1083. *
  1084. * This function is used to request %wpa_supplicant to disassociate with the
  1085. * current AP.
  1086. */
  1087. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1088. int reason_code)
  1089. {
  1090. u8 *addr = NULL;
  1091. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1092. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1093. if (wpa_s->use_client_mlme)
  1094. ieee80211_sta_deauthenticate(wpa_s, reason_code);
  1095. else
  1096. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
  1097. reason_code);
  1098. addr = wpa_s->bssid;
  1099. }
  1100. wpa_clear_keys(wpa_s, addr);
  1101. wpa_s->current_ssid = NULL;
  1102. wpa_sm_set_config(wpa_s->wpa, NULL);
  1103. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1104. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1105. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1106. }
  1107. static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
  1108. {
  1109. #define SCAN_AP_LIMIT 128
  1110. struct wpa_scan_result *results;
  1111. int num, i;
  1112. struct wpa_scan_results *res;
  1113. results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
  1114. if (results == NULL) {
  1115. wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
  1116. "results");
  1117. return -1;
  1118. }
  1119. num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
  1120. wpa_printf(MSG_DEBUG, "Scan results: %d", num);
  1121. if (num < 0) {
  1122. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  1123. os_free(results);
  1124. return -1;
  1125. }
  1126. if (num > SCAN_AP_LIMIT) {
  1127. wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
  1128. num, SCAN_AP_LIMIT);
  1129. num = SCAN_AP_LIMIT;
  1130. }
  1131. wpa_scan_results_free(wpa_s->scan_res);
  1132. wpa_s->scan_res = NULL;
  1133. /* Convert old scan result data structure to the new one */
  1134. res = os_zalloc(sizeof(*res));
  1135. if (res == NULL) {
  1136. os_free(results);
  1137. return -1;
  1138. }
  1139. res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
  1140. if (res->res == NULL) {
  1141. os_free(results);
  1142. os_free(res);
  1143. return -1;
  1144. }
  1145. for (i = 0; i < num; i++) {
  1146. struct wpa_scan_result *bss = &results[i];
  1147. struct wpa_scan_res *r;
  1148. size_t ie_len;
  1149. u8 *pos;
  1150. ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
  1151. if (bss->maxrate)
  1152. ie_len += 3;
  1153. if (bss->mdie_present)
  1154. ie_len += 5;
  1155. r = os_zalloc(sizeof(*r) + ie_len);
  1156. if (r == NULL)
  1157. break;
  1158. os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
  1159. r->freq = bss->freq;
  1160. r->caps = bss->caps;
  1161. r->qual = bss->qual;
  1162. r->noise = bss->noise;
  1163. r->level = bss->level;
  1164. r->tsf = bss->tsf;
  1165. r->ie_len = ie_len;
  1166. pos = (u8 *) (r + 1);
  1167. /* SSID IE */
  1168. *pos++ = WLAN_EID_SSID;
  1169. *pos++ = bss->ssid_len;
  1170. os_memcpy(pos, bss->ssid, bss->ssid_len);
  1171. pos += bss->ssid_len;
  1172. if (bss->maxrate) {
  1173. /* Fake Supported Rate IE to include max rate */
  1174. *pos++ = WLAN_EID_SUPP_RATES;
  1175. *pos++ = 1;
  1176. *pos++ = bss->maxrate;
  1177. }
  1178. if (bss->rsn_ie_len) {
  1179. os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
  1180. pos += bss->rsn_ie_len;
  1181. }
  1182. if (bss->mdie_present) {
  1183. os_memcpy(pos, bss->mdie, 5);
  1184. pos += 5;
  1185. }
  1186. if (bss->wpa_ie_len) {
  1187. os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
  1188. pos += bss->wpa_ie_len;
  1189. }
  1190. res->res[res->num++] = r;
  1191. }
  1192. os_free(results);
  1193. wpa_s->scan_res = res;
  1194. return 0;
  1195. }
  1196. /**
  1197. * wpa_supplicant_get_scan_results - Get scan results
  1198. * @wpa_s: Pointer to wpa_supplicant data
  1199. * Returns: 0 on success, -1 on failure
  1200. *
  1201. * This function is request the current scan results from the driver and stores
  1202. * a local copy of the results in wpa_s->scan_res.
  1203. */
  1204. int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
  1205. {
  1206. int ret;
  1207. if (wpa_s->use_client_mlme) {
  1208. wpa_scan_results_free(wpa_s->scan_res);
  1209. wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
  1210. if (wpa_s->scan_res == NULL) {
  1211. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  1212. ret = -1;
  1213. } else
  1214. ret = 0;
  1215. } else if (wpa_s->driver->get_scan_results2 == NULL)
  1216. ret = wpa_supplicant_get_scan_results_old(wpa_s);
  1217. else {
  1218. wpa_scan_results_free(wpa_s->scan_res);
  1219. wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
  1220. if (wpa_s->scan_res == NULL) {
  1221. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  1222. ret = -1;
  1223. } else
  1224. ret = 0;
  1225. }
  1226. if (wpa_s->scan_res)
  1227. wpa_scan_sort_results(wpa_s->scan_res);
  1228. return ret;
  1229. }
  1230. /**
  1231. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1232. * @wpa_s: Pointer to wpa_supplicant data
  1233. * Returns: A pointer to the current network structure or %NULL on failure
  1234. */
  1235. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1236. {
  1237. struct wpa_ssid *entry;
  1238. u8 ssid[MAX_SSID_LEN];
  1239. int res;
  1240. size_t ssid_len;
  1241. u8 bssid[ETH_ALEN];
  1242. int wired;
  1243. if (wpa_s->use_client_mlme) {
  1244. if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
  1245. wpa_printf(MSG_WARNING, "Could not read SSID from "
  1246. "MLME.");
  1247. return NULL;
  1248. }
  1249. } else {
  1250. res = wpa_drv_get_ssid(wpa_s, ssid);
  1251. if (res < 0) {
  1252. wpa_printf(MSG_WARNING, "Could not read SSID from "
  1253. "driver.");
  1254. return NULL;
  1255. }
  1256. ssid_len = res;
  1257. }
  1258. if (wpa_s->use_client_mlme)
  1259. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  1260. else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1261. wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
  1262. return NULL;
  1263. }
  1264. wired = wpa_s->conf->ap_scan == 0 && wpa_s->driver &&
  1265. IS_WIRED(wpa_s->driver);
  1266. entry = wpa_s->conf->ssid;
  1267. while (entry) {
  1268. if (!entry->disabled &&
  1269. ((ssid_len == entry->ssid_len &&
  1270. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1271. (!entry->bssid_set ||
  1272. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1273. return entry;
  1274. entry = entry->next;
  1275. }
  1276. return NULL;
  1277. }
  1278. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1279. const char *name)
  1280. {
  1281. int i;
  1282. if (wpa_s == NULL)
  1283. return -1;
  1284. if (wpa_supplicant_drivers[0] == NULL) {
  1285. wpa_printf(MSG_ERROR, "No driver interfaces build into "
  1286. "wpa_supplicant.");
  1287. return -1;
  1288. }
  1289. if (name == NULL) {
  1290. /* default to first driver in the list */
  1291. wpa_s->driver = wpa_supplicant_drivers[0];
  1292. return 0;
  1293. }
  1294. for (i = 0; wpa_supplicant_drivers[i]; i++) {
  1295. if (os_strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
  1296. wpa_s->driver = wpa_supplicant_drivers[i];
  1297. return 0;
  1298. }
  1299. }
  1300. wpa_printf(MSG_ERROR, "Unsupported driver '%s'.\n", name);
  1301. return -1;
  1302. }
  1303. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1304. const u8 *buf, size_t len)
  1305. {
  1306. struct wpa_supplicant *wpa_s = ctx;
  1307. wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1308. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1309. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1310. wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
  1311. "no key management is configured");
  1312. return;
  1313. }
  1314. if (wpa_s->eapol_received == 0 &&
  1315. (!wpa_s->driver_4way_handshake ||
  1316. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1317. wpa_s->wpa_state != WPA_COMPLETED)) {
  1318. /* Timeout for completing IEEE 802.1X and WPA authentication */
  1319. wpa_supplicant_req_auth_timeout(
  1320. wpa_s,
  1321. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1322. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) ?
  1323. 70 : 10, 0);
  1324. }
  1325. wpa_s->eapol_received++;
  1326. if (wpa_s->countermeasures) {
  1327. wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
  1328. "packet");
  1329. return;
  1330. }
  1331. /* Source address of the incoming EAPOL frame could be compared to the
  1332. * current BSSID. However, it is possible that a centralized
  1333. * Authenticator could be using another MAC address than the BSSID of
  1334. * an AP, so just allow any address to be used for now. The replies are
  1335. * still sent to the current BSSID (if available), though. */
  1336. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  1337. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  1338. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  1339. return;
  1340. wpa_drv_poll(wpa_s);
  1341. if (!wpa_s->driver_4way_handshake)
  1342. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  1343. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1344. /*
  1345. * Set portValid = TRUE here since we are going to skip 4-way
  1346. * handshake processing which would normally set portValid. We
  1347. * need this to allow the EAPOL state machines to be completed
  1348. * without going through EAPOL-Key handshake.
  1349. */
  1350. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1351. }
  1352. }
  1353. void wpa_supplicant_sta_free_hw_features(struct wpa_hw_modes *hw_features,
  1354. size_t num_hw_features)
  1355. {
  1356. ieee80211_sta_free_hw_features(hw_features, num_hw_features);
  1357. }
  1358. void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
  1359. struct ieee80211_rx_status *rx_status)
  1360. {
  1361. struct wpa_supplicant *wpa_s = ctx;
  1362. ieee80211_sta_rx(wpa_s, buf, len, rx_status);
  1363. }
  1364. /**
  1365. * wpa_supplicant_driver_init - Initialize driver interface parameters
  1366. * @wpa_s: Pointer to wpa_supplicant data
  1367. * Returns: 0 on success, -1 on failure
  1368. *
  1369. * This function is called to initialize driver interface parameters.
  1370. * wpa_drv_init() must have been called before this function to initialize the
  1371. * driver interface.
  1372. */
  1373. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  1374. {
  1375. static int interface_count = 0;
  1376. if (wpa_s->driver->send_eapol) {
  1377. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1378. if (addr)
  1379. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1380. } else {
  1381. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  1382. wpa_drv_get_mac_addr(wpa_s),
  1383. ETH_P_EAPOL,
  1384. wpa_supplicant_rx_eapol, wpa_s, 0);
  1385. if (wpa_s->l2 == NULL)
  1386. return -1;
  1387. }
  1388. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  1389. wpa_printf(MSG_ERROR, "Failed to get own L2 address");
  1390. return -1;
  1391. }
  1392. wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
  1393. MAC2STR(wpa_s->own_addr));
  1394. if (wpa_s->bridge_ifname[0]) {
  1395. wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
  1396. " '%s'", wpa_s->bridge_ifname);
  1397. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  1398. wpa_s->own_addr,
  1399. ETH_P_EAPOL,
  1400. wpa_supplicant_rx_eapol, wpa_s,
  1401. 0);
  1402. if (wpa_s->l2_br == NULL) {
  1403. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  1404. "connection for the bridge interface '%s'",
  1405. wpa_s->bridge_ifname);
  1406. return -1;
  1407. }
  1408. }
  1409. /* Backwards compatibility call to set_wpa() handler. This is called
  1410. * only just after init and just before deinit, so these handler can be
  1411. * used to implement same functionality. */
  1412. if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
  1413. struct wpa_driver_capa capa;
  1414. if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
  1415. !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1416. WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
  1417. wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
  1418. /* Continue to allow non-WPA modes to be used. */
  1419. } else {
  1420. wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
  1421. "driver.");
  1422. return -1;
  1423. }
  1424. }
  1425. wpa_clear_keys(wpa_s, NULL);
  1426. /* Make sure that TKIP countermeasures are not left enabled (could
  1427. * happen if wpa_supplicant is killed during countermeasures. */
  1428. wpa_drv_set_countermeasures(wpa_s, 0);
  1429. wpa_drv_set_drop_unencrypted(wpa_s, 1);
  1430. wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  1431. wpa_drv_flush_pmkid(wpa_s);
  1432. wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
  1433. wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
  1434. interface_count++;
  1435. return 0;
  1436. }
  1437. static int wpa_supplicant_daemon(const char *pid_file)
  1438. {
  1439. wpa_printf(MSG_DEBUG, "Daemonize..");
  1440. return os_daemonize(pid_file);
  1441. }
  1442. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  1443. {
  1444. struct wpa_supplicant *wpa_s;
  1445. wpa_s = os_zalloc(sizeof(*wpa_s));
  1446. if (wpa_s == NULL)
  1447. return NULL;
  1448. wpa_s->scan_req = 1;
  1449. return wpa_s;
  1450. }
  1451. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  1452. struct wpa_interface *iface)
  1453. {
  1454. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  1455. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  1456. iface->confname ? iface->confname : "N/A",
  1457. iface->driver ? iface->driver : "default",
  1458. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  1459. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  1460. if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
  1461. return -1;
  1462. }
  1463. if (iface->confname) {
  1464. #ifdef CONFIG_BACKEND_FILE
  1465. wpa_s->confname = os_rel2abs_path(iface->confname);
  1466. if (wpa_s->confname == NULL) {
  1467. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  1468. "for configuration file '%s'.",
  1469. iface->confname);
  1470. return -1;
  1471. }
  1472. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  1473. iface->confname, wpa_s->confname);
  1474. #else /* CONFIG_BACKEND_FILE */
  1475. wpa_s->confname = os_strdup(iface->confname);
  1476. #endif /* CONFIG_BACKEND_FILE */
  1477. wpa_s->conf = wpa_config_read(wpa_s->confname);
  1478. if (wpa_s->conf == NULL) {
  1479. wpa_printf(MSG_ERROR, "Failed to read or parse "
  1480. "configuration '%s'.", wpa_s->confname);
  1481. return -1;
  1482. }
  1483. /*
  1484. * Override ctrl_interface and driver_param if set on command
  1485. * line.
  1486. */
  1487. if (iface->ctrl_interface) {
  1488. os_free(wpa_s->conf->ctrl_interface);
  1489. wpa_s->conf->ctrl_interface =
  1490. os_strdup(iface->ctrl_interface);
  1491. }
  1492. if (iface->driver_param) {
  1493. os_free(wpa_s->conf->driver_param);
  1494. wpa_s->conf->driver_param =
  1495. os_strdup(iface->driver_param);
  1496. }
  1497. } else
  1498. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  1499. iface->driver_param);
  1500. if (wpa_s->conf == NULL) {
  1501. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  1502. return -1;
  1503. }
  1504. if (iface->ifname == NULL) {
  1505. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  1506. return -1;
  1507. }
  1508. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  1509. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  1510. iface->ifname);
  1511. return -1;
  1512. }
  1513. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  1514. if (iface->bridge_ifname) {
  1515. if (os_strlen(iface->bridge_ifname) >=
  1516. sizeof(wpa_s->bridge_ifname)) {
  1517. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  1518. "name '%s'.", iface->bridge_ifname);
  1519. return -1;
  1520. }
  1521. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  1522. sizeof(wpa_s->bridge_ifname));
  1523. }
  1524. return 0;
  1525. }
  1526. static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s)
  1527. {
  1528. const char *ifname;
  1529. struct wpa_driver_capa capa;
  1530. wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
  1531. wpa_s->ifname);
  1532. /* RSNA Supplicant Key Management - INITIALIZE */
  1533. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1534. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1535. /* Initialize driver interface and register driver event handler before
  1536. * L2 receive handler so that association events are processed before
  1537. * EAPOL-Key packets if both become available for the same select()
  1538. * call. */
  1539. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  1540. if (wpa_s->drv_priv == NULL) {
  1541. wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
  1542. return -1;
  1543. }
  1544. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  1545. wpa_printf(MSG_ERROR, "Driver interface rejected "
  1546. "driver_param '%s'", wpa_s->conf->driver_param);
  1547. return -1;
  1548. }
  1549. ifname = wpa_drv_get_ifname(wpa_s);
  1550. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  1551. wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
  1552. "name with '%s'", ifname);
  1553. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  1554. }
  1555. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  1556. return -1;
  1557. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  1558. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  1559. NULL);
  1560. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  1561. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  1562. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  1563. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  1564. wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
  1565. "dot11RSNAConfigPMKLifetime");
  1566. return -1;
  1567. }
  1568. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  1569. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  1570. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  1571. wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
  1572. "dot11RSNAConfigPMKReauthThreshold");
  1573. return -1;
  1574. }
  1575. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  1576. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  1577. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  1578. wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
  1579. "dot11RSNAConfigSATimeout");
  1580. return -1;
  1581. }
  1582. if (wpa_supplicant_driver_init(wpa_s) < 0)
  1583. return -1;
  1584. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  1585. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  1586. wpa_printf(MSG_DEBUG, "Failed to set country");
  1587. return -1;
  1588. }
  1589. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  1590. if (wpas_wps_init(wpa_s))
  1591. return -1;
  1592. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  1593. return -1;
  1594. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  1595. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  1596. if (wpa_s->ctrl_iface == NULL) {
  1597. wpa_printf(MSG_ERROR,
  1598. "Failed to initialize control interface '%s'.\n"
  1599. "You may have another wpa_supplicant process "
  1600. "already running or the file was\n"
  1601. "left by an unclean termination of wpa_supplicant "
  1602. "in which case you will need\n"
  1603. "to manually remove this file before starting "
  1604. "wpa_supplicant again.\n",
  1605. wpa_s->conf->ctrl_interface);
  1606. return -1;
  1607. }
  1608. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  1609. if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  1610. wpa_s->use_client_mlme = 1;
  1611. if (ieee80211_sta_init(wpa_s))
  1612. return -1;
  1613. }
  1614. if (capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)
  1615. wpa_s->driver_4way_handshake = 1;
  1616. }
  1617. return 0;
  1618. }
  1619. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
  1620. {
  1621. if (wpa_s->drv_priv) {
  1622. wpa_supplicant_deauthenticate(wpa_s,
  1623. WLAN_REASON_DEAUTH_LEAVING);
  1624. /* Backwards compatibility call to set_wpa() handler. This is
  1625. * called only just after init and just before deinit, so these
  1626. * handler can be used to implement same functionality. */
  1627. if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
  1628. wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
  1629. "driver.");
  1630. }
  1631. wpa_drv_set_drop_unencrypted(wpa_s, 0);
  1632. wpa_drv_set_countermeasures(wpa_s, 0);
  1633. wpa_clear_keys(wpa_s, NULL);
  1634. }
  1635. wpas_dbus_unregister_iface(wpa_s);
  1636. wpa_supplicant_cleanup(wpa_s);
  1637. if (wpa_s->drv_priv)
  1638. wpa_drv_deinit(wpa_s);
  1639. }
  1640. /**
  1641. * wpa_supplicant_add_iface - Add a new network interface
  1642. * @global: Pointer to global data from wpa_supplicant_init()
  1643. * @iface: Interface configuration options
  1644. * Returns: Pointer to the created interface or %NULL on failure
  1645. *
  1646. * This function is used to add new network interfaces for %wpa_supplicant.
  1647. * This can be called before wpa_supplicant_run() to add interfaces before the
  1648. * main event loop has been started. In addition, new interfaces can be added
  1649. * dynamically while %wpa_supplicant is already running. This could happen,
  1650. * e.g., when a hotplug network adapter is inserted.
  1651. */
  1652. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  1653. struct wpa_interface *iface)
  1654. {
  1655. struct wpa_supplicant *wpa_s;
  1656. if (global == NULL || iface == NULL)
  1657. return NULL;
  1658. wpa_s = wpa_supplicant_alloc();
  1659. if (wpa_s == NULL)
  1660. return NULL;
  1661. if (wpa_supplicant_init_iface(wpa_s, iface) ||
  1662. wpa_supplicant_init_iface2(wpa_s)) {
  1663. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  1664. iface->ifname);
  1665. wpa_supplicant_deinit_iface(wpa_s);
  1666. os_free(wpa_s);
  1667. return NULL;
  1668. }
  1669. wpa_s->global = global;
  1670. /* Register the interface with the dbus control interface */
  1671. if (wpas_dbus_register_iface(wpa_s)) {
  1672. wpa_supplicant_deinit_iface(wpa_s);
  1673. os_free(wpa_s);
  1674. return NULL;
  1675. }
  1676. wpa_s->next = global->ifaces;
  1677. global->ifaces = wpa_s;
  1678. wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  1679. return wpa_s;
  1680. }
  1681. /**
  1682. * wpa_supplicant_remove_iface - Remove a network interface
  1683. * @global: Pointer to global data from wpa_supplicant_init()
  1684. * @wpa_s: Pointer to the network interface to be removed
  1685. * Returns: 0 if interface was removed, -1 if interface was not found
  1686. *
  1687. * This function can be used to dynamically remove network interfaces from
  1688. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  1689. * addition, this function is used to remove all remaining interfaces when
  1690. * %wpa_supplicant is terminated.
  1691. */
  1692. int wpa_supplicant_remove_iface(struct wpa_global *global,
  1693. struct wpa_supplicant *wpa_s)
  1694. {
  1695. struct wpa_supplicant *prev;
  1696. /* Remove interface from the global list of interfaces */
  1697. prev = global->ifaces;
  1698. if (prev == wpa_s) {
  1699. global->ifaces = wpa_s->next;
  1700. } else {
  1701. while (prev && prev->next != wpa_s)
  1702. prev = prev->next;
  1703. if (prev == NULL)
  1704. return -1;
  1705. prev->next = wpa_s->next;
  1706. }
  1707. wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  1708. wpa_supplicant_deinit_iface(wpa_s);
  1709. os_free(wpa_s);
  1710. return 0;
  1711. }
  1712. /**
  1713. * wpa_supplicant_get_iface - Get a new network interface
  1714. * @global: Pointer to global data from wpa_supplicant_init()
  1715. * @ifname: Interface name
  1716. * Returns: Pointer to the interface or %NULL if not found
  1717. */
  1718. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  1719. const char *ifname)
  1720. {
  1721. struct wpa_supplicant *wpa_s;
  1722. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  1723. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  1724. return wpa_s;
  1725. }
  1726. return NULL;
  1727. }
  1728. /**
  1729. * wpa_supplicant_init - Initialize %wpa_supplicant
  1730. * @params: Parameters for %wpa_supplicant
  1731. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  1732. *
  1733. * This function is used to initialize %wpa_supplicant. After successful
  1734. * initialization, the returned data pointer can be used to add and remove
  1735. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  1736. */
  1737. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  1738. {
  1739. struct wpa_global *global;
  1740. int ret;
  1741. if (params == NULL)
  1742. return NULL;
  1743. wpa_debug_open_file(params->wpa_debug_file_path);
  1744. ret = eap_peer_register_methods();
  1745. if (ret) {
  1746. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  1747. if (ret == -2)
  1748. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  1749. "the same EAP type.");
  1750. return NULL;
  1751. }
  1752. global = os_zalloc(sizeof(*global));
  1753. if (global == NULL)
  1754. return NULL;
  1755. global->params.daemonize = params->daemonize;
  1756. global->params.wait_for_monitor = params->wait_for_monitor;
  1757. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  1758. if (params->pid_file)
  1759. global->params.pid_file = os_strdup(params->pid_file);
  1760. if (params->ctrl_interface)
  1761. global->params.ctrl_interface =
  1762. os_strdup(params->ctrl_interface);
  1763. wpa_debug_level = global->params.wpa_debug_level =
  1764. params->wpa_debug_level;
  1765. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  1766. params->wpa_debug_show_keys;
  1767. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  1768. params->wpa_debug_timestamp;
  1769. if (eloop_init(global)) {
  1770. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  1771. wpa_supplicant_deinit(global);
  1772. return NULL;
  1773. }
  1774. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  1775. if (global->ctrl_iface == NULL) {
  1776. wpa_supplicant_deinit(global);
  1777. return NULL;
  1778. }
  1779. if (global->params.dbus_ctrl_interface) {
  1780. global->dbus_ctrl_iface =
  1781. wpa_supplicant_dbus_ctrl_iface_init(global);
  1782. if (global->dbus_ctrl_iface == NULL) {
  1783. wpa_supplicant_deinit(global);
  1784. return NULL;
  1785. }
  1786. }
  1787. return global;
  1788. }
  1789. /**
  1790. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  1791. * @global: Pointer to global data from wpa_supplicant_init()
  1792. * Returns: 0 after successful event loop run, -1 on failure
  1793. *
  1794. * This function starts the main event loop and continues running as long as
  1795. * there are any remaining events. In most cases, this function is running as
  1796. * long as the %wpa_supplicant process in still in use.
  1797. */
  1798. int wpa_supplicant_run(struct wpa_global *global)
  1799. {
  1800. struct wpa_supplicant *wpa_s;
  1801. if (global->params.daemonize &&
  1802. wpa_supplicant_daemon(global->params.pid_file))
  1803. return -1;
  1804. if (global->params.wait_for_monitor) {
  1805. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  1806. if (wpa_s->ctrl_iface)
  1807. wpa_supplicant_ctrl_iface_wait(
  1808. wpa_s->ctrl_iface);
  1809. }
  1810. eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
  1811. eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
  1812. eloop_run();
  1813. return 0;
  1814. }
  1815. /**
  1816. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  1817. * @global: Pointer to global data from wpa_supplicant_init()
  1818. *
  1819. * This function is called to deinitialize %wpa_supplicant and to free all
  1820. * allocated resources. Remaining network interfaces will also be removed.
  1821. */
  1822. void wpa_supplicant_deinit(struct wpa_global *global)
  1823. {
  1824. if (global == NULL)
  1825. return;
  1826. while (global->ifaces)
  1827. wpa_supplicant_remove_iface(global, global->ifaces);
  1828. if (global->ctrl_iface)
  1829. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  1830. if (global->dbus_ctrl_iface)
  1831. wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
  1832. eap_peer_unregister_methods();
  1833. eloop_destroy();
  1834. if (global->params.pid_file) {
  1835. os_daemonize_terminate(global->params.pid_file);
  1836. os_free(global->params.pid_file);
  1837. }
  1838. os_free(global->params.ctrl_interface);
  1839. os_free(global);
  1840. wpa_debug_close_file();
  1841. }