wpas_glue.c 20 KB

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