wpas_glue.c 34 KB

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