wpas_glue.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  2. * WPA Supplicant - Glue code to setup EAPOL and RSN modules
  3. * Copyright (c) 2003-2012, 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. {
  510. struct wpa_supplicant *wpa_s = ctx;
  511. *tdls_supported = 0;
  512. *tdls_ext_setup = 0;
  513. if (!wpa_s->drv_capa_known)
  514. return -1;
  515. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
  516. *tdls_supported = 1;
  517. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
  518. *tdls_ext_setup = 1;
  519. return 0;
  520. }
  521. static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
  522. u8 action_code, u8 dialog_token,
  523. u16 status_code, u32 peer_capab,
  524. int initiator, const u8 *buf,
  525. size_t len)
  526. {
  527. struct wpa_supplicant *wpa_s = ctx;
  528. return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
  529. status_code, peer_capab, initiator, buf,
  530. len);
  531. }
  532. static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
  533. {
  534. struct wpa_supplicant *wpa_s = ctx;
  535. return wpa_drv_tdls_oper(wpa_s, oper, peer);
  536. }
  537. static int wpa_supplicant_tdls_peer_addset(
  538. void *ctx, const u8 *peer, int add, u16 aid, u16 capability,
  539. const u8 *supp_rates, size_t supp_rates_len,
  540. const struct ieee80211_ht_capabilities *ht_capab,
  541. const struct ieee80211_vht_capabilities *vht_capab,
  542. u8 qosinfo, int wmm, const u8 *ext_capab, size_t ext_capab_len,
  543. const u8 *supp_channels, size_t supp_channels_len,
  544. const u8 *supp_oper_classes, size_t supp_oper_classes_len)
  545. {
  546. struct wpa_supplicant *wpa_s = ctx;
  547. struct hostapd_sta_add_params params;
  548. os_memset(&params, 0, sizeof(params));
  549. params.addr = peer;
  550. params.aid = aid;
  551. params.capability = capability;
  552. params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
  553. /*
  554. * Don't rely only on qosinfo for WMM capability. It may be 0 even when
  555. * present. Allow the WMM IE to also indicate QoS support.
  556. */
  557. if (wmm || qosinfo)
  558. params.flags |= WPA_STA_WMM;
  559. params.ht_capabilities = ht_capab;
  560. params.vht_capabilities = vht_capab;
  561. params.qosinfo = qosinfo;
  562. params.listen_interval = 0;
  563. params.supp_rates = supp_rates;
  564. params.supp_rates_len = supp_rates_len;
  565. params.set = !add;
  566. params.ext_capab = ext_capab;
  567. params.ext_capab_len = ext_capab_len;
  568. params.supp_channels = supp_channels;
  569. params.supp_channels_len = supp_channels_len;
  570. params.supp_oper_classes = supp_oper_classes;
  571. params.supp_oper_classes_len = supp_oper_classes_len;
  572. return wpa_drv_sta_add(wpa_s, &params);
  573. }
  574. #endif /* CONFIG_TDLS */
  575. #endif /* CONFIG_NO_WPA */
  576. enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
  577. {
  578. if (os_strcmp(field, "IDENTITY") == 0)
  579. return WPA_CTRL_REQ_EAP_IDENTITY;
  580. else if (os_strcmp(field, "PASSWORD") == 0)
  581. return WPA_CTRL_REQ_EAP_PASSWORD;
  582. else if (os_strcmp(field, "NEW_PASSWORD") == 0)
  583. return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
  584. else if (os_strcmp(field, "PIN") == 0)
  585. return WPA_CTRL_REQ_EAP_PIN;
  586. else if (os_strcmp(field, "OTP") == 0)
  587. return WPA_CTRL_REQ_EAP_OTP;
  588. else if (os_strcmp(field, "PASSPHRASE") == 0)
  589. return WPA_CTRL_REQ_EAP_PASSPHRASE;
  590. else if (os_strcmp(field, "SIM") == 0)
  591. return WPA_CTRL_REQ_SIM;
  592. return WPA_CTRL_REQ_UNKNOWN;
  593. }
  594. const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
  595. const char *default_txt,
  596. const char **txt)
  597. {
  598. const char *ret = NULL;
  599. *txt = default_txt;
  600. switch (field) {
  601. case WPA_CTRL_REQ_EAP_IDENTITY:
  602. *txt = "Identity";
  603. ret = "IDENTITY";
  604. break;
  605. case WPA_CTRL_REQ_EAP_PASSWORD:
  606. *txt = "Password";
  607. ret = "PASSWORD";
  608. break;
  609. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  610. *txt = "New Password";
  611. ret = "NEW_PASSWORD";
  612. break;
  613. case WPA_CTRL_REQ_EAP_PIN:
  614. *txt = "PIN";
  615. ret = "PIN";
  616. break;
  617. case WPA_CTRL_REQ_EAP_OTP:
  618. ret = "OTP";
  619. break;
  620. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  621. *txt = "Private key passphrase";
  622. ret = "PASSPHRASE";
  623. break;
  624. case WPA_CTRL_REQ_SIM:
  625. ret = "SIM";
  626. break;
  627. default:
  628. break;
  629. }
  630. /* txt needs to be something */
  631. if (*txt == NULL) {
  632. wpa_printf(MSG_WARNING, "No message for request %d", field);
  633. ret = NULL;
  634. }
  635. return ret;
  636. }
  637. #ifdef IEEE8021X_EAPOL
  638. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  639. static void wpa_supplicant_eap_param_needed(void *ctx,
  640. enum wpa_ctrl_req_type field,
  641. const char *default_txt)
  642. {
  643. struct wpa_supplicant *wpa_s = ctx;
  644. struct wpa_ssid *ssid = wpa_s->current_ssid;
  645. const char *field_name, *txt = NULL;
  646. char *buf;
  647. size_t buflen;
  648. int len;
  649. if (ssid == NULL)
  650. return;
  651. wpas_notify_network_request(wpa_s, ssid, field, default_txt);
  652. field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
  653. &txt);
  654. if (field_name == NULL) {
  655. wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
  656. field);
  657. return;
  658. }
  659. wpas_notify_eap_status(wpa_s, "eap parameter needed", field_name);
  660. buflen = 100 + os_strlen(txt) + ssid->ssid_len;
  661. buf = os_malloc(buflen);
  662. if (buf == NULL)
  663. return;
  664. len = os_snprintf(buf, buflen,
  665. WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
  666. field_name, ssid->id, txt);
  667. if (os_snprintf_error(buflen, len)) {
  668. os_free(buf);
  669. return;
  670. }
  671. if (ssid->ssid && buflen > len + ssid->ssid_len) {
  672. os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
  673. len += ssid->ssid_len;
  674. buf[len] = '\0';
  675. }
  676. buf[buflen - 1] = '\0';
  677. wpa_msg(wpa_s, MSG_INFO, "%s", buf);
  678. os_free(buf);
  679. }
  680. #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  681. #define wpa_supplicant_eap_param_needed NULL
  682. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  683. static void wpa_supplicant_port_cb(void *ctx, int authorized)
  684. {
  685. struct wpa_supplicant *wpa_s = ctx;
  686. #ifdef CONFIG_AP
  687. if (wpa_s->ap_iface) {
  688. wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
  689. "port status: %s",
  690. authorized ? "Authorized" : "Unauthorized");
  691. return;
  692. }
  693. #endif /* CONFIG_AP */
  694. wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
  695. authorized ? "Authorized" : "Unauthorized");
  696. wpa_drv_set_supp_port(wpa_s, authorized);
  697. }
  698. static void wpa_supplicant_cert_cb(void *ctx, int depth, const char *subject,
  699. const char *cert_hash,
  700. const struct wpabuf *cert)
  701. {
  702. struct wpa_supplicant *wpa_s = ctx;
  703. wpas_notify_certification(wpa_s, depth, subject, cert_hash, cert);
  704. }
  705. static void wpa_supplicant_status_cb(void *ctx, const char *status,
  706. const char *parameter)
  707. {
  708. struct wpa_supplicant *wpa_s = ctx;
  709. wpas_notify_eap_status(wpa_s, status, parameter);
  710. }
  711. static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
  712. {
  713. struct wpa_supplicant *wpa_s = ctx;
  714. char *str;
  715. int res;
  716. wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
  717. id, len);
  718. if (wpa_s->current_ssid == NULL)
  719. return;
  720. if (id == NULL) {
  721. if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
  722. "NULL", 0) < 0)
  723. return;
  724. } else {
  725. str = os_malloc(len * 2 + 1);
  726. if (str == NULL)
  727. return;
  728. wpa_snprintf_hex(str, len * 2 + 1, id, len);
  729. res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
  730. str, 0);
  731. os_free(str);
  732. if (res < 0)
  733. return;
  734. }
  735. if (wpa_s->conf->update_config) {
  736. res = wpa_config_write(wpa_s->confname, wpa_s->conf);
  737. if (res) {
  738. wpa_printf(MSG_DEBUG, "Failed to update config after "
  739. "anonymous_id update");
  740. }
  741. }
  742. }
  743. #endif /* IEEE8021X_EAPOL */
  744. int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
  745. {
  746. #ifdef IEEE8021X_EAPOL
  747. struct eapol_ctx *ctx;
  748. ctx = os_zalloc(sizeof(*ctx));
  749. if (ctx == NULL) {
  750. wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
  751. return -1;
  752. }
  753. ctx->ctx = wpa_s;
  754. ctx->msg_ctx = wpa_s;
  755. ctx->eapol_send_ctx = wpa_s;
  756. ctx->preauth = 0;
  757. ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
  758. ctx->eapol_send = wpa_supplicant_eapol_send;
  759. ctx->set_wep_key = wpa_eapol_set_wep_key;
  760. #ifndef CONFIG_NO_CONFIG_BLOBS
  761. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  762. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  763. #endif /* CONFIG_NO_CONFIG_BLOBS */
  764. ctx->aborted_cached = wpa_supplicant_aborted_cached;
  765. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  766. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  767. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  768. ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
  769. ctx->wps = wpa_s->wps;
  770. ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
  771. ctx->port_cb = wpa_supplicant_port_cb;
  772. ctx->cb = wpa_supplicant_eapol_cb;
  773. ctx->cert_cb = wpa_supplicant_cert_cb;
  774. ctx->status_cb = wpa_supplicant_status_cb;
  775. ctx->set_anon_id = wpa_supplicant_set_anon_id;
  776. ctx->cb_ctx = wpa_s;
  777. wpa_s->eapol = eapol_sm_init(ctx);
  778. if (wpa_s->eapol == NULL) {
  779. os_free(ctx);
  780. wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
  781. "machines.");
  782. return -1;
  783. }
  784. #endif /* IEEE8021X_EAPOL */
  785. return 0;
  786. }
  787. #ifndef CONFIG_NO_WPA
  788. static void wpa_supplicant_set_rekey_offload(void *ctx, const u8 *kek,
  789. const u8 *kck,
  790. const u8 *replay_ctr)
  791. {
  792. struct wpa_supplicant *wpa_s = ctx;
  793. wpa_drv_set_rekey_info(wpa_s, kek, kck, replay_ctr);
  794. }
  795. #endif /* CONFIG_NO_WPA */
  796. static int wpa_supplicant_key_mgmt_set_pmk(void *ctx, const u8 *pmk,
  797. size_t pmk_len)
  798. {
  799. struct wpa_supplicant *wpa_s = ctx;
  800. if (wpa_s->conf->key_mgmt_offload)
  801. return wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0,
  802. NULL, 0, pmk, pmk_len);
  803. else
  804. return 0;
  805. }
  806. int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
  807. {
  808. #ifndef CONFIG_NO_WPA
  809. struct wpa_sm_ctx *ctx;
  810. ctx = os_zalloc(sizeof(*ctx));
  811. if (ctx == NULL) {
  812. wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
  813. return -1;
  814. }
  815. ctx->ctx = wpa_s;
  816. ctx->msg_ctx = wpa_s;
  817. ctx->set_state = _wpa_supplicant_set_state;
  818. ctx->get_state = _wpa_supplicant_get_state;
  819. ctx->deauthenticate = _wpa_supplicant_deauthenticate;
  820. ctx->set_key = wpa_supplicant_set_key;
  821. ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
  822. ctx->get_bssid = wpa_supplicant_get_bssid;
  823. ctx->ether_send = _wpa_ether_send;
  824. ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
  825. ctx->alloc_eapol = _wpa_alloc_eapol;
  826. ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
  827. ctx->add_pmkid = wpa_supplicant_add_pmkid;
  828. ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
  829. #ifndef CONFIG_NO_CONFIG_BLOBS
  830. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  831. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  832. #endif /* CONFIG_NO_CONFIG_BLOBS */
  833. ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
  834. #ifdef CONFIG_IEEE80211R
  835. ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
  836. ctx->send_ft_action = wpa_supplicant_send_ft_action;
  837. ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
  838. #endif /* CONFIG_IEEE80211R */
  839. #ifdef CONFIG_TDLS
  840. ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
  841. ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
  842. ctx->tdls_oper = wpa_supplicant_tdls_oper;
  843. ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
  844. #endif /* CONFIG_TDLS */
  845. ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
  846. ctx->key_mgmt_set_pmk = wpa_supplicant_key_mgmt_set_pmk;
  847. wpa_s->wpa = wpa_sm_init(ctx);
  848. if (wpa_s->wpa == NULL) {
  849. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  850. "machine");
  851. return -1;
  852. }
  853. #endif /* CONFIG_NO_WPA */
  854. return 0;
  855. }
  856. void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
  857. struct wpa_ssid *ssid)
  858. {
  859. struct rsn_supp_config conf;
  860. if (ssid) {
  861. os_memset(&conf, 0, sizeof(conf));
  862. conf.network_ctx = ssid;
  863. conf.peerkey_enabled = ssid->peerkey;
  864. conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
  865. #ifdef IEEE8021X_EAPOL
  866. conf.proactive_key_caching = ssid->proactive_key_caching < 0 ?
  867. wpa_s->conf->okc : ssid->proactive_key_caching;
  868. conf.eap_workaround = ssid->eap_workaround;
  869. conf.eap_conf_ctx = &ssid->eap;
  870. #endif /* IEEE8021X_EAPOL */
  871. conf.ssid = ssid->ssid;
  872. conf.ssid_len = ssid->ssid_len;
  873. conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
  874. #ifdef CONFIG_P2P
  875. if (ssid->p2p_group && wpa_s->current_bss &&
  876. !wpa_s->p2p_disable_ip_addr_req) {
  877. struct wpabuf *p2p;
  878. p2p = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
  879. P2P_IE_VENDOR_TYPE);
  880. if (p2p) {
  881. u8 group_capab;
  882. group_capab = p2p_get_group_capab(p2p);
  883. if (group_capab &
  884. P2P_GROUP_CAPAB_IP_ADDR_ALLOCATION)
  885. conf.p2p = 1;
  886. wpabuf_free(p2p);
  887. }
  888. }
  889. #endif /* CONFIG_P2P */
  890. }
  891. wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
  892. }