wpas_glue.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. * WPA Supplicant - Glue code to setup EAPOL and RSN modules
  3. * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eapol_supp/eapol_supp_sm.h"
  11. #include "rsn_supp/wpa.h"
  12. #include "eloop.h"
  13. #include "config.h"
  14. #include "l2_packet/l2_packet.h"
  15. #include "common/wpa_common.h"
  16. #include "wpa_supplicant_i.h"
  17. #include "driver_i.h"
  18. #include "rsn_supp/pmksa_cache.h"
  19. #include "sme.h"
  20. #include "common/ieee802_11_defs.h"
  21. #include "common/wpa_ctrl.h"
  22. #include "wpas_glue.h"
  23. #include "wps_supplicant.h"
  24. #include "bss.h"
  25. #include "scan.h"
  26. #include "notify.h"
  27. #include "wpas_kay.h"
  28. #ifndef CONFIG_NO_CONFIG_BLOBS
  29. #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
  30. static void wpa_supplicant_set_config_blob(void *ctx,
  31. struct wpa_config_blob *blob)
  32. {
  33. struct wpa_supplicant *wpa_s = ctx;
  34. wpa_config_set_blob(wpa_s->conf, blob);
  35. if (wpa_s->conf->update_config) {
  36. int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  37. if (ret) {
  38. wpa_printf(MSG_DEBUG, "Failed to update config after "
  39. "blob set");
  40. }
  41. }
  42. }
  43. static const struct wpa_config_blob *
  44. wpa_supplicant_get_config_blob(void *ctx, const char *name)
  45. {
  46. struct wpa_supplicant *wpa_s = ctx;
  47. return wpa_config_get_blob(wpa_s->conf, name);
  48. }
  49. #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
  50. #endif /* CONFIG_NO_CONFIG_BLOBS */
  51. #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
  52. static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
  53. const void *data, u16 data_len,
  54. size_t *msg_len, void **data_pos)
  55. {
  56. struct ieee802_1x_hdr *hdr;
  57. *msg_len = sizeof(*hdr) + data_len;
  58. hdr = os_malloc(*msg_len);
  59. if (hdr == NULL)
  60. return NULL;
  61. hdr->version = wpa_s->conf->eapol_version;
  62. hdr->type = type;
  63. hdr->length = host_to_be16(data_len);
  64. if (data)
  65. os_memcpy(hdr + 1, data, data_len);
  66. else
  67. os_memset(hdr + 1, 0, data_len);
  68. if (data_pos)
  69. *data_pos = hdr + 1;
  70. return (u8 *) hdr;
  71. }
  72. /**
  73. * wpa_ether_send - Send Ethernet frame
  74. * @wpa_s: Pointer to wpa_supplicant data
  75. * @dest: Destination MAC address
  76. * @proto: Ethertype in host byte order
  77. * @buf: Frame payload starting from IEEE 802.1X header
  78. * @len: Frame payload length
  79. * Returns: >=0 on success, <0 on failure
  80. */
  81. static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
  82. u16 proto, const u8 *buf, size_t len)
  83. {
  84. #ifdef CONFIG_TESTING_OPTIONS
  85. if (wpa_s->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
  86. size_t hex_len = 2 * len + 1;
  87. char *hex = os_malloc(hex_len);
  88. if (hex == NULL)
  89. return -1;
  90. wpa_snprintf_hex(hex, hex_len, buf, len);
  91. wpa_msg(wpa_s, MSG_INFO, "EAPOL-TX " MACSTR " %s",
  92. MAC2STR(dest), hex);
  93. os_free(hex);
  94. return 0;
  95. }
  96. #endif /* CONFIG_TESTING_OPTIONS */
  97. if (wpa_s->l2) {
  98. return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
  99. }
  100. return -1;
  101. }
  102. #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
  103. #ifdef IEEE8021X_EAPOL
  104. /**
  105. * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
  106. * @ctx: Pointer to wpa_supplicant data (wpa_s)
  107. * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
  108. * @buf: EAPOL payload (after IEEE 802.1X header)
  109. * @len: EAPOL payload length
  110. * Returns: >=0 on success, <0 on failure
  111. *
  112. * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
  113. * to the current Authenticator.
  114. */
  115. static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
  116. size_t len)
  117. {
  118. struct wpa_supplicant *wpa_s = ctx;
  119. u8 *msg, *dst, bssid[ETH_ALEN];
  120. size_t msglen;
  121. int res;
  122. /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
  123. * extra copy here */
  124. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  125. wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  126. /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
  127. * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
  128. * machines. */
  129. wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
  130. "mode (type=%d len=%lu)", type,
  131. (unsigned long) len);
  132. return -1;
  133. }
  134. if (pmksa_cache_get_current(wpa_s->wpa) &&
  135. type == IEEE802_1X_TYPE_EAPOL_START) {
  136. /*
  137. * We were trying to use PMKSA caching and sending EAPOL-Start
  138. * would abort that and trigger full EAPOL authentication.
  139. * However, we've already waited for the AP/Authenticator to
  140. * start 4-way handshake or EAP authentication, and apparently
  141. * it has not done so since the startWhen timer has reached zero
  142. * to get the state machine sending EAPOL-Start. This is not
  143. * really supposed to happen, but an interoperability issue with
  144. * a deployed AP has been identified where the connection fails
  145. * due to that AP failing to operate correctly if PMKID is
  146. * included in the Association Request frame. To work around
  147. * this, assume PMKSA caching failed and try to initiate full
  148. * EAP authentication.
  149. */
  150. if (!wpa_s->current_ssid ||
  151. wpa_s->current_ssid->eap_workaround) {
  152. wpa_printf(MSG_DEBUG,
  153. "RSN: Timeout on waiting for the AP to initiate 4-way handshake for PMKSA caching or EAP authentication - try to force it to start EAP authentication");
  154. } else {
  155. wpa_printf(MSG_DEBUG,
  156. "RSN: PMKSA caching - do not send EAPOL-Start");
  157. return -1;
  158. }
  159. }
  160. if (is_zero_ether_addr(wpa_s->bssid)) {
  161. wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
  162. "EAPOL frame");
  163. if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  164. !is_zero_ether_addr(bssid)) {
  165. dst = bssid;
  166. wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
  167. " from the driver as the EAPOL destination",
  168. MAC2STR(dst));
  169. } else {
  170. dst = wpa_s->last_eapol_src;
  171. wpa_printf(MSG_DEBUG, "Using the source address of the"
  172. " last received EAPOL frame " MACSTR " as "
  173. "the EAPOL destination",
  174. MAC2STR(dst));
  175. }
  176. } else {
  177. /* BSSID was already set (from (Re)Assoc event, so use it as
  178. * the EAPOL destination. */
  179. dst = wpa_s->bssid;
  180. }
  181. msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
  182. if (msg == NULL)
  183. return -1;
  184. wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
  185. wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
  186. res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
  187. os_free(msg);
  188. return res;
  189. }
  190. /**
  191. * wpa_eapol_set_wep_key - set WEP key for the driver
  192. * @ctx: Pointer to wpa_supplicant data (wpa_s)
  193. * @unicast: 1 = individual unicast key, 0 = broadcast key
  194. * @keyidx: WEP key index (0..3)
  195. * @key: Pointer to key data
  196. * @keylen: Key length in bytes
  197. * Returns: 0 on success or < 0 on error.
  198. */
  199. static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
  200. const u8 *key, size_t keylen)
  201. {
  202. struct wpa_supplicant *wpa_s = ctx;
  203. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  204. int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
  205. WPA_CIPHER_WEP104;
  206. if (unicast)
  207. wpa_s->pairwise_cipher = cipher;
  208. else
  209. wpa_s->group_cipher = cipher;
  210. }
  211. return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
  212. unicast ? wpa_s->bssid : NULL,
  213. keyidx, unicast, NULL, 0, key, keylen);
  214. }
  215. static void wpa_supplicant_aborted_cached(void *ctx)
  216. {
  217. struct wpa_supplicant *wpa_s = ctx;
  218. wpa_sm_aborted_cached(wpa_s->wpa);
  219. }
  220. static const char * result_str(enum eapol_supp_result result)
  221. {
  222. switch (result) {
  223. case EAPOL_SUPP_RESULT_FAILURE:
  224. return "FAILURE";
  225. case EAPOL_SUPP_RESULT_SUCCESS:
  226. return "SUCCESS";
  227. case EAPOL_SUPP_RESULT_EXPECTED_FAILURE:
  228. return "EXPECTED_FAILURE";
  229. }
  230. return "?";
  231. }
  232. static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol,
  233. enum eapol_supp_result result,
  234. void *ctx)
  235. {
  236. struct wpa_supplicant *wpa_s = ctx;
  237. int res, pmk_len;
  238. u8 pmk[PMK_LEN];
  239. wpa_printf(MSG_DEBUG, "EAPOL authentication completed - result=%s",
  240. result_str(result));
  241. if (wpas_wps_eapol_cb(wpa_s) > 0)
  242. return;
  243. wpa_s->eap_expected_failure = result ==
  244. EAPOL_SUPP_RESULT_EXPECTED_FAILURE;
  245. if (result != EAPOL_SUPP_RESULT_SUCCESS) {
  246. /*
  247. * Make sure we do not get stuck here waiting for long EAPOL
  248. * timeout if the AP does not disconnect in case of
  249. * authentication failure.
  250. */
  251. wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
  252. } else {
  253. ieee802_1x_notify_create_actor(wpa_s, wpa_s->last_eapol_src);
  254. }
  255. if (result != EAPOL_SUPP_RESULT_SUCCESS ||
  256. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  257. return;
  258. if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
  259. return;
  260. wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
  261. "handshake");
  262. pmk_len = PMK_LEN;
  263. if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
  264. #ifdef CONFIG_IEEE80211R
  265. u8 buf[2 * PMK_LEN];
  266. wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
  267. "driver-based 4-way hs and FT");
  268. res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
  269. if (res == 0) {
  270. os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
  271. os_memset(buf, 0, sizeof(buf));
  272. }
  273. #else /* CONFIG_IEEE80211R */
  274. res = -1;
  275. #endif /* CONFIG_IEEE80211R */
  276. } else {
  277. res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
  278. if (res) {
  279. /*
  280. * EAP-LEAP is an exception from other EAP methods: it
  281. * uses only 16-byte PMK.
  282. */
  283. res = eapol_sm_get_key(eapol, pmk, 16);
  284. pmk_len = 16;
  285. }
  286. }
  287. if (res) {
  288. wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
  289. "machines");
  290. return;
  291. }
  292. wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
  293. "handshake", pmk, pmk_len);
  294. if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
  295. pmk_len)) {
  296. wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
  297. }
  298. wpa_supplicant_cancel_scan(wpa_s);
  299. wpa_supplicant_cancel_auth_timeout(wpa_s);
  300. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  301. }
  302. static void wpa_supplicant_notify_eapol_done(void *ctx)
  303. {
  304. struct wpa_supplicant *wpa_s = ctx;
  305. wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
  306. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  307. wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
  308. } else {
  309. wpa_supplicant_cancel_auth_timeout(wpa_s);
  310. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  311. }
  312. }
  313. #endif /* IEEE8021X_EAPOL */
  314. #ifndef CONFIG_NO_WPA
  315. static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
  316. {
  317. int ret = 0;
  318. struct wpa_bss *curr = NULL, *bss;
  319. struct wpa_ssid *ssid = wpa_s->current_ssid;
  320. const u8 *ie;
  321. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  322. if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  323. continue;
  324. if (ssid == NULL ||
  325. ((bss->ssid_len == ssid->ssid_len &&
  326. os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
  327. ssid->ssid_len == 0)) {
  328. curr = bss;
  329. break;
  330. }
  331. }
  332. if (curr) {
  333. ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
  334. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  335. ret = -1;
  336. ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
  337. if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  338. ret = -1;
  339. } else {
  340. ret = -1;
  341. }
  342. return ret;
  343. }
  344. static int wpa_supplicant_get_beacon_ie(void *ctx)
  345. {
  346. struct wpa_supplicant *wpa_s = ctx;
  347. if (wpa_get_beacon_ie(wpa_s) == 0) {
  348. return 0;
  349. }
  350. /* No WPA/RSN IE found in the cached scan results. Try to get updated
  351. * scan results from the driver. */
  352. if (wpa_supplicant_update_scan_results(wpa_s) < 0)
  353. return -1;
  354. return wpa_get_beacon_ie(wpa_s);
  355. }
  356. static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
  357. const void *data, u16 data_len,
  358. size_t *msg_len, void **data_pos)
  359. {
  360. return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
  361. }
  362. static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
  363. const u8 *buf, size_t len)
  364. {
  365. return wpa_ether_send(wpa_s, dest, proto, buf, len);
  366. }
  367. static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
  368. {
  369. wpa_supplicant_cancel_auth_timeout(wpa_s);
  370. }
  371. static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
  372. {
  373. wpa_supplicant_set_state(wpa_s, state);
  374. }
  375. /**
  376. * wpa_supplicant_get_state - Get the connection state
  377. * @wpa_s: Pointer to wpa_supplicant data
  378. * Returns: The current connection state (WPA_*)
  379. */
  380. static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
  381. {
  382. return wpa_s->wpa_state;
  383. }
  384. static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
  385. {
  386. return wpa_supplicant_get_state(wpa_s);
  387. }
  388. static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
  389. {
  390. wpa_supplicant_deauthenticate(wpa_s, reason_code);
  391. /* Schedule a scan to make sure we continue looking for networks */
  392. wpa_supplicant_req_scan(wpa_s, 5, 0);
  393. }
  394. static void * wpa_supplicant_get_network_ctx(void *wpa_s)
  395. {
  396. return wpa_supplicant_get_ssid(wpa_s);
  397. }
  398. static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
  399. {
  400. struct wpa_supplicant *wpa_s = ctx;
  401. return wpa_drv_get_bssid(wpa_s, bssid);
  402. }
  403. static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
  404. const u8 *addr, int key_idx, int set_tx,
  405. const u8 *seq, size_t seq_len,
  406. const u8 *key, size_t key_len)
  407. {
  408. struct wpa_supplicant *wpa_s = _wpa_s;
  409. if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
  410. /* Clear the MIC error counter when setting a new PTK. */
  411. wpa_s->mic_errors_seen = 0;
  412. }
  413. #ifdef CONFIG_TESTING_GET_GTK
  414. if (key_idx > 0 && addr && is_broadcast_ether_addr(addr) &&
  415. alg != WPA_ALG_NONE && key_len <= sizeof(wpa_s->last_gtk)) {
  416. os_memcpy(wpa_s->last_gtk, key, key_len);
  417. wpa_s->last_gtk_len = key_len;
  418. }
  419. #endif /* CONFIG_TESTING_GET_GTK */
  420. return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
  421. key, key_len);
  422. }
  423. static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
  424. int protection_type,
  425. int key_type)
  426. {
  427. return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
  428. key_type);
  429. }
  430. static int wpa_supplicant_add_pmkid(void *wpa_s,
  431. const u8 *bssid, const u8 *pmkid)
  432. {
  433. return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
  434. }
  435. static int wpa_supplicant_remove_pmkid(void *wpa_s,
  436. const u8 *bssid, const u8 *pmkid)
  437. {
  438. return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
  439. }
  440. #ifdef CONFIG_IEEE80211R
  441. static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
  442. const u8 *ies, size_t ies_len)
  443. {
  444. struct wpa_supplicant *wpa_s = ctx;
  445. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  446. return sme_update_ft_ies(wpa_s, md, ies, ies_len);
  447. return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
  448. }
  449. static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
  450. const u8 *target_ap,
  451. const u8 *ies, size_t ies_len)
  452. {
  453. struct wpa_supplicant *wpa_s = ctx;
  454. int ret;
  455. u8 *data, *pos;
  456. size_t data_len;
  457. if (action != 1) {
  458. wpa_printf(MSG_ERROR, "Unsupported send_ft_action action %d",
  459. action);
  460. return -1;
  461. }
  462. /*
  463. * Action frame payload:
  464. * Category[1] = 6 (Fast BSS Transition)
  465. * Action[1] = 1 (Fast BSS Transition Request)
  466. * STA Address
  467. * Target AP Address
  468. * FT IEs
  469. */
  470. data_len = 2 + 2 * ETH_ALEN + ies_len;
  471. data = os_malloc(data_len);
  472. if (data == NULL)
  473. return -1;
  474. pos = data;
  475. *pos++ = 0x06; /* FT Action category */
  476. *pos++ = action;
  477. os_memcpy(pos, wpa_s->own_addr, ETH_ALEN);
  478. pos += ETH_ALEN;
  479. os_memcpy(pos, target_ap, ETH_ALEN);
  480. pos += ETH_ALEN;
  481. os_memcpy(pos, ies, ies_len);
  482. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
  483. wpa_s->bssid, wpa_s->own_addr, wpa_s->bssid,
  484. data, data_len, 0);
  485. os_free(data);
  486. return ret;
  487. }
  488. static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
  489. {
  490. struct wpa_supplicant *wpa_s = ctx;
  491. struct wpa_driver_auth_params params;
  492. struct wpa_bss *bss;
  493. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  494. if (bss == NULL)
  495. return -1;
  496. os_memset(&params, 0, sizeof(params));
  497. params.bssid = target_ap;
  498. params.freq = bss->freq;
  499. params.ssid = bss->ssid;
  500. params.ssid_len = bss->ssid_len;
  501. params.auth_alg = WPA_AUTH_ALG_FT;
  502. params.local_state_change = 1;
  503. return wpa_drv_authenticate(wpa_s, &params);
  504. }
  505. #endif /* CONFIG_IEEE80211R */
  506. #ifdef CONFIG_TDLS
  507. static int wpa_supplicant_tdls_get_capa(void *ctx, int *tdls_supported,
  508. int *tdls_ext_setup,
  509. int *tdls_chan_switch)
  510. {
  511. struct wpa_supplicant *wpa_s = ctx;
  512. *tdls_supported = 0;
  513. *tdls_ext_setup = 0;
  514. *tdls_chan_switch = 0;
  515. if (!wpa_s->drv_capa_known)
  516. return -1;
  517. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
  518. *tdls_supported = 1;
  519. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
  520. *tdls_ext_setup = 1;
  521. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH)
  522. *tdls_chan_switch = 1;
  523. return 0;
  524. }
  525. static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
  526. u8 action_code, u8 dialog_token,
  527. u16 status_code, u32 peer_capab,
  528. int initiator, const u8 *buf,
  529. size_t len)
  530. {
  531. struct wpa_supplicant *wpa_s = ctx;
  532. return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
  533. status_code, peer_capab, initiator, buf,
  534. len);
  535. }
  536. static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
  537. {
  538. struct wpa_supplicant *wpa_s = ctx;
  539. return wpa_drv_tdls_oper(wpa_s, oper, peer);
  540. }
  541. static int wpa_supplicant_tdls_peer_addset(
  542. void *ctx, const u8 *peer, int add, u16 aid, u16 capability,
  543. const u8 *supp_rates, size_t supp_rates_len,
  544. const struct ieee80211_ht_capabilities *ht_capab,
  545. const struct ieee80211_vht_capabilities *vht_capab,
  546. u8 qosinfo, int wmm, const u8 *ext_capab, size_t ext_capab_len,
  547. const u8 *supp_channels, size_t supp_channels_len,
  548. const u8 *supp_oper_classes, size_t supp_oper_classes_len)
  549. {
  550. struct wpa_supplicant *wpa_s = ctx;
  551. struct hostapd_sta_add_params params;
  552. os_memset(&params, 0, sizeof(params));
  553. params.addr = peer;
  554. params.aid = aid;
  555. params.capability = capability;
  556. params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
  557. /*
  558. * Don't rely only on qosinfo for WMM capability. It may be 0 even when
  559. * present. Allow the WMM IE to also indicate QoS support.
  560. */
  561. if (wmm || qosinfo)
  562. params.flags |= WPA_STA_WMM;
  563. params.ht_capabilities = ht_capab;
  564. params.vht_capabilities = vht_capab;
  565. params.qosinfo = qosinfo;
  566. params.listen_interval = 0;
  567. params.supp_rates = supp_rates;
  568. params.supp_rates_len = supp_rates_len;
  569. params.set = !add;
  570. params.ext_capab = ext_capab;
  571. params.ext_capab_len = ext_capab_len;
  572. params.supp_channels = supp_channels;
  573. params.supp_channels_len = supp_channels_len;
  574. params.supp_oper_classes = supp_oper_classes;
  575. params.supp_oper_classes_len = supp_oper_classes_len;
  576. return wpa_drv_sta_add(wpa_s, &params);
  577. }
  578. static int wpa_supplicant_tdls_enable_channel_switch(
  579. void *ctx, const u8 *addr, u8 oper_class,
  580. const struct hostapd_freq_params *params)
  581. {
  582. struct wpa_supplicant *wpa_s = ctx;
  583. return wpa_drv_tdls_enable_channel_switch(wpa_s, addr, oper_class,
  584. params);
  585. }
  586. static int wpa_supplicant_tdls_disable_channel_switch(void *ctx, const u8 *addr)
  587. {
  588. struct wpa_supplicant *wpa_s = ctx;
  589. return wpa_drv_tdls_disable_channel_switch(wpa_s, addr);
  590. }
  591. #endif /* CONFIG_TDLS */
  592. #endif /* CONFIG_NO_WPA */
  593. enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
  594. {
  595. if (os_strcmp(field, "IDENTITY") == 0)
  596. return WPA_CTRL_REQ_EAP_IDENTITY;
  597. else if (os_strcmp(field, "PASSWORD") == 0)
  598. return WPA_CTRL_REQ_EAP_PASSWORD;
  599. else if (os_strcmp(field, "NEW_PASSWORD") == 0)
  600. return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
  601. else if (os_strcmp(field, "PIN") == 0)
  602. return WPA_CTRL_REQ_EAP_PIN;
  603. else if (os_strcmp(field, "OTP") == 0)
  604. return WPA_CTRL_REQ_EAP_OTP;
  605. else if (os_strcmp(field, "PASSPHRASE") == 0)
  606. return WPA_CTRL_REQ_EAP_PASSPHRASE;
  607. else if (os_strcmp(field, "SIM") == 0)
  608. return WPA_CTRL_REQ_SIM;
  609. else if (os_strcmp(field, "PSK_PASSPHRASE") == 0)
  610. return WPA_CTRL_REQ_PSK_PASSPHRASE;
  611. return WPA_CTRL_REQ_UNKNOWN;
  612. }
  613. const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
  614. const char *default_txt,
  615. const char **txt)
  616. {
  617. const char *ret = NULL;
  618. *txt = default_txt;
  619. switch (field) {
  620. case WPA_CTRL_REQ_EAP_IDENTITY:
  621. *txt = "Identity";
  622. ret = "IDENTITY";
  623. break;
  624. case WPA_CTRL_REQ_EAP_PASSWORD:
  625. *txt = "Password";
  626. ret = "PASSWORD";
  627. break;
  628. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  629. *txt = "New Password";
  630. ret = "NEW_PASSWORD";
  631. break;
  632. case WPA_CTRL_REQ_EAP_PIN:
  633. *txt = "PIN";
  634. ret = "PIN";
  635. break;
  636. case WPA_CTRL_REQ_EAP_OTP:
  637. ret = "OTP";
  638. break;
  639. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  640. *txt = "Private key passphrase";
  641. ret = "PASSPHRASE";
  642. break;
  643. case WPA_CTRL_REQ_SIM:
  644. ret = "SIM";
  645. break;
  646. case WPA_CTRL_REQ_PSK_PASSPHRASE:
  647. *txt = "PSK or passphrase";
  648. ret = "PSK_PASSPHRASE";
  649. break;
  650. default:
  651. break;
  652. }
  653. /* txt needs to be something */
  654. if (*txt == NULL) {
  655. wpa_printf(MSG_WARNING, "No message for request %d", field);
  656. ret = NULL;
  657. }
  658. return ret;
  659. }
  660. void wpas_send_ctrl_req(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  661. const char *field_name, const char *txt)
  662. {
  663. char *buf;
  664. size_t buflen;
  665. int len;
  666. buflen = 100 + os_strlen(txt) + ssid->ssid_len;
  667. buf = os_malloc(buflen);
  668. if (buf == NULL)
  669. return;
  670. len = os_snprintf(buf, buflen, "%s-%d:%s needed for SSID ",
  671. field_name, ssid->id, txt);
  672. if (os_snprintf_error(buflen, len)) {
  673. os_free(buf);
  674. return;
  675. }
  676. if (ssid->ssid && buflen > len + ssid->ssid_len) {
  677. os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
  678. len += ssid->ssid_len;
  679. buf[len] = '\0';
  680. }
  681. buf[buflen - 1] = '\0';
  682. wpa_msg(wpa_s, MSG_INFO, WPA_CTRL_REQ "%s", buf);
  683. os_free(buf);
  684. }
  685. #ifdef IEEE8021X_EAPOL
  686. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  687. static void wpa_supplicant_eap_param_needed(void *ctx,
  688. enum wpa_ctrl_req_type field,
  689. const char *default_txt)
  690. {
  691. struct wpa_supplicant *wpa_s = ctx;
  692. struct wpa_ssid *ssid = wpa_s->current_ssid;
  693. const char *field_name, *txt = NULL;
  694. if (ssid == NULL)
  695. return;
  696. wpas_notify_network_request(wpa_s, ssid, field, default_txt);
  697. field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
  698. &txt);
  699. if (field_name == NULL) {
  700. wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
  701. field);
  702. return;
  703. }
  704. wpas_notify_eap_status(wpa_s, "eap parameter needed", field_name);
  705. wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
  706. }
  707. #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  708. #define wpa_supplicant_eap_param_needed NULL
  709. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  710. #ifdef CONFIG_EAP_PROXY
  711. static void wpa_supplicant_eap_proxy_cb(void *ctx)
  712. {
  713. struct wpa_supplicant *wpa_s = ctx;
  714. size_t len;
  715. wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
  716. wpa_s->imsi, &len);
  717. if (wpa_s->mnc_len > 0) {
  718. wpa_s->imsi[len] = '\0';
  719. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
  720. wpa_s->imsi, wpa_s->mnc_len);
  721. } else {
  722. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
  723. }
  724. }
  725. #endif /* CONFIG_EAP_PROXY */
  726. static void wpa_supplicant_port_cb(void *ctx, int authorized)
  727. {
  728. struct wpa_supplicant *wpa_s = ctx;
  729. #ifdef CONFIG_AP
  730. if (wpa_s->ap_iface) {
  731. wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
  732. "port status: %s",
  733. authorized ? "Authorized" : "Unauthorized");
  734. return;
  735. }
  736. #endif /* CONFIG_AP */
  737. wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
  738. authorized ? "Authorized" : "Unauthorized");
  739. wpa_drv_set_supp_port(wpa_s, authorized);
  740. }
  741. static void wpa_supplicant_cert_cb(void *ctx, int depth, const char *subject,
  742. const char *altsubject[], int num_altsubject,
  743. const char *cert_hash,
  744. const struct wpabuf *cert)
  745. {
  746. struct wpa_supplicant *wpa_s = ctx;
  747. wpas_notify_certification(wpa_s, depth, subject, altsubject,
  748. num_altsubject, cert_hash, cert);
  749. }
  750. static void wpa_supplicant_status_cb(void *ctx, const char *status,
  751. const char *parameter)
  752. {
  753. struct wpa_supplicant *wpa_s = ctx;
  754. wpas_notify_eap_status(wpa_s, status, parameter);
  755. }
  756. static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
  757. {
  758. struct wpa_supplicant *wpa_s = ctx;
  759. char *str;
  760. int res;
  761. wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
  762. id, len);
  763. if (wpa_s->current_ssid == NULL)
  764. return;
  765. if (id == NULL) {
  766. if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
  767. "NULL", 0) < 0)
  768. return;
  769. } else {
  770. str = os_malloc(len * 2 + 1);
  771. if (str == NULL)
  772. return;
  773. wpa_snprintf_hex(str, len * 2 + 1, id, len);
  774. res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
  775. str, 0);
  776. os_free(str);
  777. if (res < 0)
  778. return;
  779. }
  780. if (wpa_s->conf->update_config) {
  781. res = wpa_config_write(wpa_s->confname, wpa_s->conf);
  782. if (res) {
  783. wpa_printf(MSG_DEBUG, "Failed to update config after "
  784. "anonymous_id update");
  785. }
  786. }
  787. }
  788. #endif /* IEEE8021X_EAPOL */
  789. int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
  790. {
  791. #ifdef IEEE8021X_EAPOL
  792. struct eapol_ctx *ctx;
  793. ctx = os_zalloc(sizeof(*ctx));
  794. if (ctx == NULL) {
  795. wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
  796. return -1;
  797. }
  798. ctx->ctx = wpa_s;
  799. ctx->msg_ctx = wpa_s;
  800. ctx->eapol_send_ctx = wpa_s;
  801. ctx->preauth = 0;
  802. ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
  803. ctx->eapol_send = wpa_supplicant_eapol_send;
  804. ctx->set_wep_key = wpa_eapol_set_wep_key;
  805. #ifndef CONFIG_NO_CONFIG_BLOBS
  806. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  807. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  808. #endif /* CONFIG_NO_CONFIG_BLOBS */
  809. ctx->aborted_cached = wpa_supplicant_aborted_cached;
  810. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  811. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  812. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  813. ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
  814. ctx->wps = wpa_s->wps;
  815. ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
  816. #ifdef CONFIG_EAP_PROXY
  817. ctx->eap_proxy_cb = wpa_supplicant_eap_proxy_cb;
  818. #endif /* CONFIG_EAP_PROXY */
  819. ctx->port_cb = wpa_supplicant_port_cb;
  820. ctx->cb = wpa_supplicant_eapol_cb;
  821. ctx->cert_cb = wpa_supplicant_cert_cb;
  822. ctx->cert_in_cb = wpa_s->conf->cert_in_cb;
  823. ctx->status_cb = wpa_supplicant_status_cb;
  824. ctx->set_anon_id = wpa_supplicant_set_anon_id;
  825. ctx->cb_ctx = wpa_s;
  826. wpa_s->eapol = eapol_sm_init(ctx);
  827. if (wpa_s->eapol == NULL) {
  828. os_free(ctx);
  829. wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
  830. "machines.");
  831. return -1;
  832. }
  833. #endif /* IEEE8021X_EAPOL */
  834. return 0;
  835. }
  836. #ifndef CONFIG_NO_WPA
  837. static void wpa_supplicant_set_rekey_offload(void *ctx,
  838. const u8 *kek, size_t kek_len,
  839. const u8 *kck, size_t kck_len,
  840. const u8 *replay_ctr)
  841. {
  842. struct wpa_supplicant *wpa_s = ctx;
  843. wpa_drv_set_rekey_info(wpa_s, kek, kek_len, kck, kck_len, replay_ctr);
  844. }
  845. #endif /* CONFIG_NO_WPA */
  846. static int wpa_supplicant_key_mgmt_set_pmk(void *ctx, const u8 *pmk,
  847. size_t pmk_len)
  848. {
  849. struct wpa_supplicant *wpa_s = ctx;
  850. if (wpa_s->conf->key_mgmt_offload &&
  851. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
  852. return wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0,
  853. NULL, 0, pmk, pmk_len);
  854. else
  855. return 0;
  856. }
  857. int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
  858. {
  859. #ifndef CONFIG_NO_WPA
  860. struct wpa_sm_ctx *ctx;
  861. ctx = os_zalloc(sizeof(*ctx));
  862. if (ctx == NULL) {
  863. wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
  864. return -1;
  865. }
  866. ctx->ctx = wpa_s;
  867. ctx->msg_ctx = wpa_s;
  868. ctx->set_state = _wpa_supplicant_set_state;
  869. ctx->get_state = _wpa_supplicant_get_state;
  870. ctx->deauthenticate = _wpa_supplicant_deauthenticate;
  871. ctx->set_key = wpa_supplicant_set_key;
  872. ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
  873. ctx->get_bssid = wpa_supplicant_get_bssid;
  874. ctx->ether_send = _wpa_ether_send;
  875. ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
  876. ctx->alloc_eapol = _wpa_alloc_eapol;
  877. ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
  878. ctx->add_pmkid = wpa_supplicant_add_pmkid;
  879. ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
  880. #ifndef CONFIG_NO_CONFIG_BLOBS
  881. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  882. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  883. #endif /* CONFIG_NO_CONFIG_BLOBS */
  884. ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
  885. #ifdef CONFIG_IEEE80211R
  886. ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
  887. ctx->send_ft_action = wpa_supplicant_send_ft_action;
  888. ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
  889. #endif /* CONFIG_IEEE80211R */
  890. #ifdef CONFIG_TDLS
  891. ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
  892. ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
  893. ctx->tdls_oper = wpa_supplicant_tdls_oper;
  894. ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
  895. ctx->tdls_enable_channel_switch =
  896. wpa_supplicant_tdls_enable_channel_switch;
  897. ctx->tdls_disable_channel_switch =
  898. wpa_supplicant_tdls_disable_channel_switch;
  899. #endif /* CONFIG_TDLS */
  900. ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
  901. ctx->key_mgmt_set_pmk = wpa_supplicant_key_mgmt_set_pmk;
  902. wpa_s->wpa = wpa_sm_init(ctx);
  903. if (wpa_s->wpa == NULL) {
  904. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  905. "machine");
  906. os_free(ctx);
  907. return -1;
  908. }
  909. #endif /* CONFIG_NO_WPA */
  910. return 0;
  911. }
  912. void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
  913. struct wpa_ssid *ssid)
  914. {
  915. struct rsn_supp_config conf;
  916. if (ssid) {
  917. os_memset(&conf, 0, sizeof(conf));
  918. conf.network_ctx = ssid;
  919. conf.peerkey_enabled = ssid->peerkey;
  920. conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
  921. #ifdef IEEE8021X_EAPOL
  922. conf.proactive_key_caching = ssid->proactive_key_caching < 0 ?
  923. wpa_s->conf->okc : ssid->proactive_key_caching;
  924. conf.eap_workaround = ssid->eap_workaround;
  925. conf.eap_conf_ctx = &ssid->eap;
  926. #endif /* IEEE8021X_EAPOL */
  927. conf.ssid = ssid->ssid;
  928. conf.ssid_len = ssid->ssid_len;
  929. conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
  930. #ifdef CONFIG_P2P
  931. if (ssid->p2p_group && wpa_s->current_bss &&
  932. !wpa_s->p2p_disable_ip_addr_req) {
  933. struct wpabuf *p2p;
  934. p2p = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
  935. P2P_IE_VENDOR_TYPE);
  936. if (p2p) {
  937. u8 group_capab;
  938. group_capab = p2p_get_group_capab(p2p);
  939. if (group_capab &
  940. P2P_GROUP_CAPAB_IP_ADDR_ALLOCATION)
  941. conf.p2p = 1;
  942. wpabuf_free(p2p);
  943. }
  944. }
  945. #endif /* CONFIG_P2P */
  946. }
  947. wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
  948. }