wpa_supplicant.c 67 KB

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