wpas_glue.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * WPA Supplicant - Glue code to setup EAPOL and RSN modules
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "rsn_supp/wpa.h"
  18. #include "eloop.h"
  19. #include "config.h"
  20. #include "l2_packet/l2_packet.h"
  21. #include "common/wpa_common.h"
  22. #include "wpa_supplicant_i.h"
  23. #include "driver_i.h"
  24. #include "rsn_supp/pmksa_cache.h"
  25. #include "mlme.h"
  26. #include "sme.h"
  27. #include "common/ieee802_11_defs.h"
  28. #include "common/wpa_ctrl.h"
  29. #include "wpas_glue.h"
  30. #include "wps_supplicant.h"
  31. #include "bss.h"
  32. #include "scan.h"
  33. #ifndef CONFIG_NO_CONFIG_BLOBS
  34. #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
  35. static void wpa_supplicant_set_config_blob(void *ctx,
  36. struct wpa_config_blob *blob)
  37. {
  38. struct wpa_supplicant *wpa_s = ctx;
  39. wpa_config_set_blob(wpa_s->conf, blob);
  40. if (wpa_s->conf->update_config) {
  41. int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  42. if (ret) {
  43. wpa_printf(MSG_DEBUG, "Failed to update config after "
  44. "blob set");
  45. }
  46. }
  47. }
  48. static const struct wpa_config_blob *
  49. wpa_supplicant_get_config_blob(void *ctx, const char *name)
  50. {
  51. struct wpa_supplicant *wpa_s = ctx;
  52. return wpa_config_get_blob(wpa_s->conf, name);
  53. }
  54. #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
  55. #endif /* CONFIG_NO_CONFIG_BLOBS */
  56. #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
  57. static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
  58. const void *data, u16 data_len,
  59. size_t *msg_len, void **data_pos)
  60. {
  61. struct ieee802_1x_hdr *hdr;
  62. *msg_len = sizeof(*hdr) + data_len;
  63. hdr = os_malloc(*msg_len);
  64. if (hdr == NULL)
  65. return NULL;
  66. hdr->version = wpa_s->conf->eapol_version;
  67. hdr->type = type;
  68. hdr->length = host_to_be16(data_len);
  69. if (data)
  70. os_memcpy(hdr + 1, data, data_len);
  71. else
  72. os_memset(hdr + 1, 0, data_len);
  73. if (data_pos)
  74. *data_pos = hdr + 1;
  75. return (u8 *) hdr;
  76. }
  77. /**
  78. * wpa_ether_send - Send Ethernet frame
  79. * @wpa_s: Pointer to wpa_supplicant data
  80. * @dest: Destination MAC address
  81. * @proto: Ethertype in host byte order
  82. * @buf: Frame payload starting from IEEE 802.1X header
  83. * @len: Frame payload length
  84. * Returns: >=0 on success, <0 on failure
  85. */
  86. static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
  87. u16 proto, const u8 *buf, size_t len)
  88. {
  89. if (wpa_s->l2) {
  90. return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
  91. }
  92. return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
  93. }
  94. #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
  95. #ifdef IEEE8021X_EAPOL
  96. /**
  97. * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
  98. * @ctx: Pointer to wpa_supplicant data (wpa_s)
  99. * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
  100. * @buf: EAPOL payload (after IEEE 802.1X header)
  101. * @len: EAPOL payload length
  102. * Returns: >=0 on success, <0 on failure
  103. *
  104. * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
  105. * to the current Authenticator.
  106. */
  107. static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
  108. size_t len)
  109. {
  110. struct wpa_supplicant *wpa_s = ctx;
  111. u8 *msg, *dst, bssid[ETH_ALEN];
  112. size_t msglen;
  113. int res;
  114. /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
  115. * extra copy here */
  116. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  117. wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  118. /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
  119. * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
  120. * machines. */
  121. wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
  122. "mode (type=%d len=%lu)", type,
  123. (unsigned long) len);
  124. return -1;
  125. }
  126. if (pmksa_cache_get_current(wpa_s->wpa) &&
  127. type == IEEE802_1X_TYPE_EAPOL_START) {
  128. /* Trying to use PMKSA caching - do not send EAPOL-Start frames
  129. * since they will trigger full EAPOL authentication. */
  130. wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
  131. "EAPOL-Start");
  132. return -1;
  133. }
  134. if (is_zero_ether_addr(wpa_s->bssid)) {
  135. wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
  136. "EAPOL frame");
  137. if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  138. !is_zero_ether_addr(bssid)) {
  139. dst = bssid;
  140. wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
  141. " from the driver as the EAPOL destination",
  142. MAC2STR(dst));
  143. } else {
  144. dst = wpa_s->last_eapol_src;
  145. wpa_printf(MSG_DEBUG, "Using the source address of the"
  146. " last received EAPOL frame " MACSTR " as "
  147. "the EAPOL destination",
  148. MAC2STR(dst));
  149. }
  150. } else {
  151. /* BSSID was already set (from (Re)Assoc event, so use it as
  152. * the EAPOL destination. */
  153. dst = wpa_s->bssid;
  154. }
  155. msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
  156. if (msg == NULL)
  157. return -1;
  158. wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
  159. wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
  160. res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
  161. os_free(msg);
  162. return res;
  163. }
  164. /**
  165. * wpa_eapol_set_wep_key - set WEP key for the driver
  166. * @ctx: Pointer to wpa_supplicant data (wpa_s)
  167. * @unicast: 1 = individual unicast key, 0 = broadcast key
  168. * @keyidx: WEP key index (0..3)
  169. * @key: Pointer to key data
  170. * @keylen: Key length in bytes
  171. * Returns: 0 on success or < 0 on error.
  172. */
  173. static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
  174. const u8 *key, size_t keylen)
  175. {
  176. struct wpa_supplicant *wpa_s = ctx;
  177. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  178. int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
  179. WPA_CIPHER_WEP104;
  180. if (unicast)
  181. wpa_s->pairwise_cipher = cipher;
  182. else
  183. wpa_s->group_cipher = cipher;
  184. }
  185. return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
  186. unicast ? wpa_s->bssid :
  187. (u8 *) "\xff\xff\xff\xff\xff\xff",
  188. keyidx, unicast, (u8 *) "", 0, key, keylen);
  189. }
  190. static void wpa_supplicant_aborted_cached(void *ctx)
  191. {
  192. struct wpa_supplicant *wpa_s = ctx;
  193. wpa_sm_aborted_cached(wpa_s->wpa);
  194. }
  195. static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
  196. void *ctx)
  197. {
  198. struct wpa_supplicant *wpa_s = ctx;
  199. int res, pmk_len;
  200. u8 pmk[PMK_LEN];
  201. wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
  202. success ? "" : "un");
  203. if (wpas_wps_eapol_cb(wpa_s) > 0)
  204. return;
  205. if (!success) {
  206. /*
  207. * Make sure we do not get stuck here waiting for long EAPOL
  208. * timeout if the AP does not disconnect in case of
  209. * authentication failure.
  210. */
  211. wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
  212. }
  213. if (!success || !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  214. return;
  215. if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
  216. return;
  217. wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
  218. "handshake");
  219. pmk_len = PMK_LEN;
  220. res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
  221. if (res) {
  222. /*
  223. * EAP-LEAP is an exception from other EAP methods: it
  224. * uses only 16-byte PMK.
  225. */
  226. res = eapol_sm_get_key(eapol, pmk, 16);
  227. pmk_len = 16;
  228. }
  229. if (res) {
  230. wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
  231. "machines");
  232. return;
  233. }
  234. if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
  235. pmk_len)) {
  236. wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
  237. }
  238. wpa_supplicant_cancel_scan(wpa_s);
  239. wpa_supplicant_cancel_auth_timeout(wpa_s);
  240. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  241. }
  242. static void wpa_supplicant_notify_eapol_done(void *ctx)
  243. {
  244. struct wpa_supplicant *wpa_s = ctx;
  245. wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
  246. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  247. wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
  248. } else {
  249. wpa_supplicant_cancel_auth_timeout(wpa_s);
  250. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  251. }
  252. }
  253. #endif /* IEEE8021X_EAPOL */
  254. #ifndef CONFIG_NO_WPA
  255. static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
  256. {
  257. int ret = 0;
  258. struct wpa_bss *curr = NULL, *bss;
  259. struct wpa_ssid *ssid = wpa_s->current_ssid;
  260. const u8 *ie;
  261. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  262. if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  263. continue;
  264. if (ssid == NULL ||
  265. ((bss->ssid_len == ssid->ssid_len &&
  266. os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
  267. ssid->ssid_len == 0)) {
  268. curr = bss;
  269. break;
  270. }
  271. }
  272. if (curr) {
  273. ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
  274. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  275. ret = -1;
  276. ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
  277. if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  278. ret = -1;
  279. } else {
  280. ret = -1;
  281. }
  282. return ret;
  283. }
  284. static int wpa_supplicant_get_beacon_ie(void *ctx)
  285. {
  286. struct wpa_supplicant *wpa_s = ctx;
  287. if (wpa_get_beacon_ie(wpa_s) == 0) {
  288. return 0;
  289. }
  290. /* No WPA/RSN IE found in the cached scan results. Try to get updated
  291. * scan results from the driver. */
  292. if (wpa_supplicant_update_scan_results(wpa_s) < 0)
  293. return -1;
  294. return wpa_get_beacon_ie(wpa_s);
  295. }
  296. static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
  297. const void *data, u16 data_len,
  298. size_t *msg_len, void **data_pos)
  299. {
  300. return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
  301. }
  302. static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
  303. const u8 *buf, size_t len)
  304. {
  305. return wpa_ether_send(wpa_s, dest, proto, buf, len);
  306. }
  307. static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
  308. {
  309. wpa_supplicant_cancel_auth_timeout(wpa_s);
  310. }
  311. static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
  312. {
  313. wpa_supplicant_set_state(wpa_s, state);
  314. }
  315. /**
  316. * wpa_supplicant_get_state - Get the connection state
  317. * @wpa_s: Pointer to wpa_supplicant data
  318. * Returns: The current connection state (WPA_*)
  319. */
  320. static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
  321. {
  322. return wpa_s->wpa_state;
  323. }
  324. static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
  325. {
  326. return wpa_supplicant_get_state(wpa_s);
  327. }
  328. static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
  329. {
  330. wpa_supplicant_disassociate(wpa_s, reason_code);
  331. /* Schedule a scan to make sure we continue looking for networks */
  332. wpa_supplicant_req_scan(wpa_s, 5, 0);
  333. }
  334. static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
  335. {
  336. wpa_supplicant_deauthenticate(wpa_s, reason_code);
  337. /* Schedule a scan to make sure we continue looking for networks */
  338. wpa_supplicant_req_scan(wpa_s, 5, 0);
  339. }
  340. static void * wpa_supplicant_get_network_ctx(void *wpa_s)
  341. {
  342. return wpa_supplicant_get_ssid(wpa_s);
  343. }
  344. static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
  345. {
  346. struct wpa_supplicant *wpa_s = ctx;
  347. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  348. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  349. return 0;
  350. }
  351. return wpa_drv_get_bssid(wpa_s, bssid);
  352. }
  353. static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
  354. const u8 *addr, int key_idx, int set_tx,
  355. const u8 *seq, size_t seq_len,
  356. const u8 *key, size_t key_len)
  357. {
  358. struct wpa_supplicant *wpa_s = _wpa_s;
  359. if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
  360. /* Clear the MIC error counter when setting a new PTK. */
  361. wpa_s->mic_errors_seen = 0;
  362. }
  363. return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
  364. key, key_len);
  365. }
  366. static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
  367. int protection_type,
  368. int key_type)
  369. {
  370. return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
  371. key_type);
  372. }
  373. static int wpa_supplicant_add_pmkid(void *wpa_s,
  374. const u8 *bssid, const u8 *pmkid)
  375. {
  376. return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
  377. }
  378. static int wpa_supplicant_remove_pmkid(void *wpa_s,
  379. const u8 *bssid, const u8 *pmkid)
  380. {
  381. return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
  382. }
  383. #ifdef CONFIG_IEEE80211R
  384. static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
  385. const u8 *ies, size_t ies_len)
  386. {
  387. struct wpa_supplicant *wpa_s = ctx;
  388. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  389. return ieee80211_sta_update_ft_ies(wpa_s, md, ies, ies_len);
  390. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  391. return sme_update_ft_ies(wpa_s, md, ies, ies_len);
  392. return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
  393. }
  394. static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
  395. const u8 *target_ap,
  396. const u8 *ies, size_t ies_len)
  397. {
  398. struct wpa_supplicant *wpa_s = ctx;
  399. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  400. return ieee80211_sta_send_ft_action(wpa_s, action, target_ap,
  401. ies, ies_len);
  402. return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
  403. }
  404. static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
  405. {
  406. struct wpa_supplicant *wpa_s = ctx;
  407. struct wpa_driver_auth_params params;
  408. struct wpa_bss *bss;
  409. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  410. return -1;
  411. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  412. if (bss == NULL)
  413. return -1;
  414. os_memset(&params, 0, sizeof(params));
  415. params.bssid = target_ap;
  416. params.freq = bss->freq;
  417. params.ssid = bss->ssid;
  418. params.ssid_len = bss->ssid_len;
  419. params.auth_alg = WPA_AUTH_ALG_FT;
  420. params.local_state_change = 1;
  421. return wpa_drv_authenticate(wpa_s, &params);
  422. }
  423. #endif /* CONFIG_IEEE80211R */
  424. #endif /* CONFIG_NO_WPA */
  425. #ifdef IEEE8021X_EAPOL
  426. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  427. static void wpa_supplicant_eap_param_needed(void *ctx, const char *field,
  428. const char *txt)
  429. {
  430. struct wpa_supplicant *wpa_s = ctx;
  431. struct wpa_ssid *ssid = wpa_s->current_ssid;
  432. char *buf;
  433. size_t buflen;
  434. int len;
  435. if (ssid == NULL)
  436. return;
  437. buflen = 100 + os_strlen(txt) + ssid->ssid_len;
  438. buf = os_malloc(buflen);
  439. if (buf == NULL)
  440. return;
  441. len = os_snprintf(buf, buflen,
  442. WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
  443. field, ssid->id, txt);
  444. if (len < 0 || (size_t) len >= buflen) {
  445. os_free(buf);
  446. return;
  447. }
  448. if (ssid->ssid && buflen > len + ssid->ssid_len) {
  449. os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
  450. len += ssid->ssid_len;
  451. buf[len] = '\0';
  452. }
  453. buf[buflen - 1] = '\0';
  454. wpa_msg(wpa_s, MSG_INFO, "%s", buf);
  455. os_free(buf);
  456. }
  457. #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  458. #define wpa_supplicant_eap_param_needed NULL
  459. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  460. static void wpa_supplicant_port_cb(void *ctx, int authorized)
  461. {
  462. struct wpa_supplicant *wpa_s = ctx;
  463. #ifdef CONFIG_AP
  464. if (wpa_s->ap_iface) {
  465. wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
  466. "port status: %s",
  467. authorized ? "Authorized" : "Unauthorized");
  468. return;
  469. }
  470. #endif /* CONFIG_AP */
  471. wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
  472. authorized ? "Authorized" : "Unauthorized");
  473. wpa_drv_set_supp_port(wpa_s, authorized);
  474. }
  475. #endif /* IEEE8021X_EAPOL */
  476. int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
  477. {
  478. #ifdef IEEE8021X_EAPOL
  479. struct eapol_ctx *ctx;
  480. ctx = os_zalloc(sizeof(*ctx));
  481. if (ctx == NULL) {
  482. wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
  483. return -1;
  484. }
  485. ctx->ctx = wpa_s;
  486. ctx->msg_ctx = wpa_s;
  487. ctx->eapol_send_ctx = wpa_s;
  488. ctx->preauth = 0;
  489. ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
  490. ctx->eapol_send = wpa_supplicant_eapol_send;
  491. ctx->set_wep_key = wpa_eapol_set_wep_key;
  492. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  493. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  494. ctx->aborted_cached = wpa_supplicant_aborted_cached;
  495. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  496. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  497. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  498. ctx->wps = wpa_s->wps;
  499. ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
  500. ctx->port_cb = wpa_supplicant_port_cb;
  501. ctx->cb = wpa_supplicant_eapol_cb;
  502. ctx->cb_ctx = wpa_s;
  503. wpa_s->eapol = eapol_sm_init(ctx);
  504. if (wpa_s->eapol == NULL) {
  505. os_free(ctx);
  506. wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
  507. "machines.");
  508. return -1;
  509. }
  510. #endif /* IEEE8021X_EAPOL */
  511. return 0;
  512. }
  513. int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
  514. {
  515. #ifndef CONFIG_NO_WPA
  516. struct wpa_sm_ctx *ctx;
  517. ctx = os_zalloc(sizeof(*ctx));
  518. if (ctx == NULL) {
  519. wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
  520. return -1;
  521. }
  522. ctx->ctx = wpa_s;
  523. ctx->msg_ctx = wpa_s;
  524. ctx->set_state = _wpa_supplicant_set_state;
  525. ctx->get_state = _wpa_supplicant_get_state;
  526. ctx->deauthenticate = _wpa_supplicant_deauthenticate;
  527. ctx->disassociate = _wpa_supplicant_disassociate;
  528. ctx->set_key = wpa_supplicant_set_key;
  529. ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
  530. ctx->get_bssid = wpa_supplicant_get_bssid;
  531. ctx->ether_send = _wpa_ether_send;
  532. ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
  533. ctx->alloc_eapol = _wpa_alloc_eapol;
  534. ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
  535. ctx->add_pmkid = wpa_supplicant_add_pmkid;
  536. ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
  537. #ifndef CONFIG_NO_CONFIG_BLOBS
  538. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  539. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  540. #endif /* CONFIG_NO_CONFIG_BLOBS */
  541. ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
  542. #ifdef CONFIG_IEEE80211R
  543. ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
  544. ctx->send_ft_action = wpa_supplicant_send_ft_action;
  545. ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
  546. #endif /* CONFIG_IEEE80211R */
  547. wpa_s->wpa = wpa_sm_init(ctx);
  548. if (wpa_s->wpa == NULL) {
  549. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  550. "machine");
  551. return -1;
  552. }
  553. #endif /* CONFIG_NO_WPA */
  554. return 0;
  555. }
  556. void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
  557. struct wpa_ssid *ssid)
  558. {
  559. struct rsn_supp_config conf;
  560. if (ssid) {
  561. os_memset(&conf, 0, sizeof(conf));
  562. conf.network_ctx = ssid;
  563. conf.peerkey_enabled = ssid->peerkey;
  564. conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
  565. #ifdef IEEE8021X_EAPOL
  566. conf.eap_workaround = ssid->eap_workaround;
  567. conf.eap_conf_ctx = &ssid->eap;
  568. #endif /* IEEE8021X_EAPOL */
  569. conf.ssid = ssid->ssid;
  570. conf.ssid_len = ssid->ssid_len;
  571. conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
  572. }
  573. wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
  574. }