wpas_glue.c 17 KB

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