wpa_supplicant.c 65 KB

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