您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

wpas_glue.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 : NULL,
  187. keyidx, unicast, NULL, 0, key, keylen);
  188. }
  189. static void wpa_supplicant_aborted_cached(void *ctx)
  190. {
  191. struct wpa_supplicant *wpa_s = ctx;
  192. wpa_sm_aborted_cached(wpa_s->wpa);
  193. }
  194. static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
  195. void *ctx)
  196. {
  197. struct wpa_supplicant *wpa_s = ctx;
  198. int res, pmk_len;
  199. u8 pmk[PMK_LEN];
  200. wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
  201. success ? "" : "un");
  202. if (wpas_wps_eapol_cb(wpa_s) > 0)
  203. return;
  204. if (!success) {
  205. /*
  206. * Make sure we do not get stuck here waiting for long EAPOL
  207. * timeout if the AP does not disconnect in case of
  208. * authentication failure.
  209. */
  210. wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
  211. }
  212. if (!success || !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  213. return;
  214. if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
  215. return;
  216. wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
  217. "handshake");
  218. pmk_len = PMK_LEN;
  219. if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
  220. #ifdef CONFIG_IEEE80211R
  221. u8 buf[2 * PMK_LEN];
  222. wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
  223. "driver-based 4-way hs and FT");
  224. res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
  225. if (res == 0) {
  226. os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
  227. os_memset(buf, 0, sizeof(buf));
  228. }
  229. #else /* CONFIG_IEEE80211R */
  230. res = -1;
  231. #endif /* CONFIG_IEEE80211R */
  232. } else {
  233. res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
  234. if (res) {
  235. /*
  236. * EAP-LEAP is an exception from other EAP methods: it
  237. * uses only 16-byte PMK.
  238. */
  239. res = eapol_sm_get_key(eapol, pmk, 16);
  240. pmk_len = 16;
  241. }
  242. }
  243. if (res) {
  244. wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
  245. "machines");
  246. return;
  247. }
  248. wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
  249. "handshake", pmk, pmk_len);
  250. if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
  251. pmk_len)) {
  252. wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
  253. }
  254. wpa_supplicant_cancel_scan(wpa_s);
  255. wpa_supplicant_cancel_auth_timeout(wpa_s);
  256. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  257. }
  258. static void wpa_supplicant_notify_eapol_done(void *ctx)
  259. {
  260. struct wpa_supplicant *wpa_s = ctx;
  261. wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
  262. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  263. wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
  264. } else {
  265. wpa_supplicant_cancel_auth_timeout(wpa_s);
  266. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  267. }
  268. }
  269. #endif /* IEEE8021X_EAPOL */
  270. #ifndef CONFIG_NO_WPA
  271. static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
  272. {
  273. int ret = 0;
  274. struct wpa_bss *curr = NULL, *bss;
  275. struct wpa_ssid *ssid = wpa_s->current_ssid;
  276. const u8 *ie;
  277. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  278. if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  279. continue;
  280. if (ssid == NULL ||
  281. ((bss->ssid_len == ssid->ssid_len &&
  282. os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
  283. ssid->ssid_len == 0)) {
  284. curr = bss;
  285. break;
  286. }
  287. }
  288. if (curr) {
  289. ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
  290. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  291. ret = -1;
  292. ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
  293. if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  294. ret = -1;
  295. } else {
  296. ret = -1;
  297. }
  298. return ret;
  299. }
  300. static int wpa_supplicant_get_beacon_ie(void *ctx)
  301. {
  302. struct wpa_supplicant *wpa_s = ctx;
  303. if (wpa_get_beacon_ie(wpa_s) == 0) {
  304. return 0;
  305. }
  306. /* No WPA/RSN IE found in the cached scan results. Try to get updated
  307. * scan results from the driver. */
  308. if (wpa_supplicant_update_scan_results(wpa_s) < 0)
  309. return -1;
  310. return wpa_get_beacon_ie(wpa_s);
  311. }
  312. static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
  313. const void *data, u16 data_len,
  314. size_t *msg_len, void **data_pos)
  315. {
  316. return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
  317. }
  318. static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
  319. const u8 *buf, size_t len)
  320. {
  321. return wpa_ether_send(wpa_s, dest, proto, buf, len);
  322. }
  323. static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
  324. {
  325. wpa_supplicant_cancel_auth_timeout(wpa_s);
  326. }
  327. static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
  328. {
  329. wpa_supplicant_set_state(wpa_s, state);
  330. }
  331. /**
  332. * wpa_supplicant_get_state - Get the connection state
  333. * @wpa_s: Pointer to wpa_supplicant data
  334. * Returns: The current connection state (WPA_*)
  335. */
  336. static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
  337. {
  338. return wpa_s->wpa_state;
  339. }
  340. static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
  341. {
  342. return wpa_supplicant_get_state(wpa_s);
  343. }
  344. static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
  345. {
  346. wpa_supplicant_disassociate(wpa_s, reason_code);
  347. /* Schedule a scan to make sure we continue looking for networks */
  348. wpa_supplicant_req_scan(wpa_s, 5, 0);
  349. }
  350. static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
  351. {
  352. wpa_supplicant_deauthenticate(wpa_s, reason_code);
  353. /* Schedule a scan to make sure we continue looking for networks */
  354. wpa_supplicant_req_scan(wpa_s, 5, 0);
  355. }
  356. static void * wpa_supplicant_get_network_ctx(void *wpa_s)
  357. {
  358. return wpa_supplicant_get_ssid(wpa_s);
  359. }
  360. static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
  361. {
  362. struct wpa_supplicant *wpa_s = ctx;
  363. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  364. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  365. return 0;
  366. }
  367. return wpa_drv_get_bssid(wpa_s, bssid);
  368. }
  369. static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
  370. const u8 *addr, int key_idx, int set_tx,
  371. const u8 *seq, size_t seq_len,
  372. const u8 *key, size_t key_len)
  373. {
  374. struct wpa_supplicant *wpa_s = _wpa_s;
  375. if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
  376. /* Clear the MIC error counter when setting a new PTK. */
  377. wpa_s->mic_errors_seen = 0;
  378. }
  379. return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
  380. key, key_len);
  381. }
  382. static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
  383. int protection_type,
  384. int key_type)
  385. {
  386. return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
  387. key_type);
  388. }
  389. static int wpa_supplicant_add_pmkid(void *wpa_s,
  390. const u8 *bssid, const u8 *pmkid)
  391. {
  392. return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
  393. }
  394. static int wpa_supplicant_remove_pmkid(void *wpa_s,
  395. const u8 *bssid, const u8 *pmkid)
  396. {
  397. return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
  398. }
  399. #ifdef CONFIG_IEEE80211R
  400. static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
  401. const u8 *ies, size_t ies_len)
  402. {
  403. struct wpa_supplicant *wpa_s = ctx;
  404. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  405. return ieee80211_sta_update_ft_ies(wpa_s, md, ies, ies_len);
  406. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  407. return sme_update_ft_ies(wpa_s, md, ies, ies_len);
  408. return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
  409. }
  410. static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
  411. const u8 *target_ap,
  412. const u8 *ies, size_t ies_len)
  413. {
  414. struct wpa_supplicant *wpa_s = ctx;
  415. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  416. return ieee80211_sta_send_ft_action(wpa_s, action, target_ap,
  417. ies, ies_len);
  418. return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
  419. }
  420. static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
  421. {
  422. struct wpa_supplicant *wpa_s = ctx;
  423. struct wpa_driver_auth_params params;
  424. struct wpa_bss *bss;
  425. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  426. return -1;
  427. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  428. if (bss == NULL)
  429. return -1;
  430. os_memset(&params, 0, sizeof(params));
  431. params.bssid = target_ap;
  432. params.freq = bss->freq;
  433. params.ssid = bss->ssid;
  434. params.ssid_len = bss->ssid_len;
  435. params.auth_alg = WPA_AUTH_ALG_FT;
  436. params.local_state_change = 1;
  437. return wpa_drv_authenticate(wpa_s, &params);
  438. }
  439. #endif /* CONFIG_IEEE80211R */
  440. #endif /* CONFIG_NO_WPA */
  441. #ifdef CONFIG_TDLS
  442. static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
  443. u8 action_code, u8 dialog_token,
  444. u16 status_code, const u8 *buf,
  445. size_t len)
  446. {
  447. struct wpa_supplicant *wpa_s = ctx;
  448. return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
  449. status_code, buf, len);
  450. }
  451. static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
  452. {
  453. struct wpa_supplicant *wpa_s = ctx;
  454. return wpa_drv_tdls_oper(wpa_s, oper, peer);
  455. }
  456. #endif /* CONFIG_TDLS */
  457. #ifdef IEEE8021X_EAPOL
  458. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  459. static void wpa_supplicant_eap_param_needed(void *ctx, const char *field,
  460. const char *txt)
  461. {
  462. struct wpa_supplicant *wpa_s = ctx;
  463. struct wpa_ssid *ssid = wpa_s->current_ssid;
  464. char *buf;
  465. size_t buflen;
  466. int len;
  467. if (ssid == NULL)
  468. return;
  469. buflen = 100 + os_strlen(txt) + ssid->ssid_len;
  470. buf = os_malloc(buflen);
  471. if (buf == NULL)
  472. return;
  473. len = os_snprintf(buf, buflen,
  474. WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
  475. field, ssid->id, txt);
  476. if (len < 0 || (size_t) len >= buflen) {
  477. os_free(buf);
  478. return;
  479. }
  480. if (ssid->ssid && buflen > len + ssid->ssid_len) {
  481. os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
  482. len += ssid->ssid_len;
  483. buf[len] = '\0';
  484. }
  485. buf[buflen - 1] = '\0';
  486. wpa_msg(wpa_s, MSG_INFO, "%s", buf);
  487. os_free(buf);
  488. }
  489. #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  490. #define wpa_supplicant_eap_param_needed NULL
  491. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  492. static void wpa_supplicant_port_cb(void *ctx, int authorized)
  493. {
  494. struct wpa_supplicant *wpa_s = ctx;
  495. #ifdef CONFIG_AP
  496. if (wpa_s->ap_iface) {
  497. wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
  498. "port status: %s",
  499. authorized ? "Authorized" : "Unauthorized");
  500. return;
  501. }
  502. #endif /* CONFIG_AP */
  503. wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
  504. authorized ? "Authorized" : "Unauthorized");
  505. wpa_drv_set_supp_port(wpa_s, authorized);
  506. }
  507. #endif /* IEEE8021X_EAPOL */
  508. int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
  509. {
  510. #ifdef IEEE8021X_EAPOL
  511. struct eapol_ctx *ctx;
  512. ctx = os_zalloc(sizeof(*ctx));
  513. if (ctx == NULL) {
  514. wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
  515. return -1;
  516. }
  517. ctx->ctx = wpa_s;
  518. ctx->msg_ctx = wpa_s;
  519. ctx->eapol_send_ctx = wpa_s;
  520. ctx->preauth = 0;
  521. ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
  522. ctx->eapol_send = wpa_supplicant_eapol_send;
  523. ctx->set_wep_key = wpa_eapol_set_wep_key;
  524. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  525. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  526. ctx->aborted_cached = wpa_supplicant_aborted_cached;
  527. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  528. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  529. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  530. ctx->wps = wpa_s->wps;
  531. ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
  532. ctx->port_cb = wpa_supplicant_port_cb;
  533. ctx->cb = wpa_supplicant_eapol_cb;
  534. ctx->cb_ctx = wpa_s;
  535. wpa_s->eapol = eapol_sm_init(ctx);
  536. if (wpa_s->eapol == NULL) {
  537. os_free(ctx);
  538. wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
  539. "machines.");
  540. return -1;
  541. }
  542. #endif /* IEEE8021X_EAPOL */
  543. return 0;
  544. }
  545. int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
  546. {
  547. #ifndef CONFIG_NO_WPA
  548. struct wpa_sm_ctx *ctx;
  549. ctx = os_zalloc(sizeof(*ctx));
  550. if (ctx == NULL) {
  551. wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
  552. return -1;
  553. }
  554. ctx->ctx = wpa_s;
  555. ctx->msg_ctx = wpa_s;
  556. ctx->set_state = _wpa_supplicant_set_state;
  557. ctx->get_state = _wpa_supplicant_get_state;
  558. ctx->deauthenticate = _wpa_supplicant_deauthenticate;
  559. ctx->disassociate = _wpa_supplicant_disassociate;
  560. ctx->set_key = wpa_supplicant_set_key;
  561. ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
  562. ctx->get_bssid = wpa_supplicant_get_bssid;
  563. ctx->ether_send = _wpa_ether_send;
  564. ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
  565. ctx->alloc_eapol = _wpa_alloc_eapol;
  566. ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
  567. ctx->add_pmkid = wpa_supplicant_add_pmkid;
  568. ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
  569. #ifndef CONFIG_NO_CONFIG_BLOBS
  570. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  571. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  572. #endif /* CONFIG_NO_CONFIG_BLOBS */
  573. ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
  574. #ifdef CONFIG_IEEE80211R
  575. ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
  576. ctx->send_ft_action = wpa_supplicant_send_ft_action;
  577. ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
  578. #endif /* CONFIG_IEEE80211R */
  579. #ifdef CONFIG_TDLS
  580. ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
  581. ctx->tdls_oper = wpa_supplicant_tdls_oper;
  582. #endif /* CONFIG_TDLS */
  583. wpa_s->wpa = wpa_sm_init(ctx);
  584. if (wpa_s->wpa == NULL) {
  585. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  586. "machine");
  587. return -1;
  588. }
  589. #endif /* CONFIG_NO_WPA */
  590. return 0;
  591. }
  592. void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
  593. struct wpa_ssid *ssid)
  594. {
  595. struct rsn_supp_config conf;
  596. if (ssid) {
  597. os_memset(&conf, 0, sizeof(conf));
  598. conf.network_ctx = ssid;
  599. conf.peerkey_enabled = ssid->peerkey;
  600. conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
  601. #ifdef IEEE8021X_EAPOL
  602. conf.eap_workaround = ssid->eap_workaround;
  603. conf.eap_conf_ctx = &ssid->eap;
  604. #endif /* IEEE8021X_EAPOL */
  605. conf.ssid = ssid->ssid;
  606. conf.ssid_len = ssid->ssid_len;
  607. conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
  608. }
  609. wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
  610. }