wpas_glue.c 17 KB

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