wpas_glue.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. #ifndef CONFIG_NO_CONFIG_BLOBS
  28. #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
  29. static void wpa_supplicant_set_config_blob(void *ctx,
  30. struct wpa_config_blob *blob)
  31. {
  32. struct wpa_supplicant *wpa_s = ctx;
  33. wpa_config_set_blob(wpa_s->conf, blob);
  34. if (wpa_s->conf->update_config) {
  35. int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  36. if (ret) {
  37. wpa_printf(MSG_DEBUG, "Failed to update config after "
  38. "blob set");
  39. }
  40. }
  41. }
  42. static const struct wpa_config_blob *
  43. wpa_supplicant_get_config_blob(void *ctx, const char *name)
  44. {
  45. struct wpa_supplicant *wpa_s = ctx;
  46. return wpa_config_get_blob(wpa_s->conf, name);
  47. }
  48. #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
  49. #endif /* CONFIG_NO_CONFIG_BLOBS */
  50. #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
  51. static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
  52. const void *data, u16 data_len,
  53. size_t *msg_len, void **data_pos)
  54. {
  55. struct ieee802_1x_hdr *hdr;
  56. *msg_len = sizeof(*hdr) + data_len;
  57. hdr = os_malloc(*msg_len);
  58. if (hdr == NULL)
  59. return NULL;
  60. hdr->version = wpa_s->conf->eapol_version;
  61. hdr->type = type;
  62. hdr->length = host_to_be16(data_len);
  63. if (data)
  64. os_memcpy(hdr + 1, data, data_len);
  65. else
  66. os_memset(hdr + 1, 0, data_len);
  67. if (data_pos)
  68. *data_pos = hdr + 1;
  69. return (u8 *) hdr;
  70. }
  71. /**
  72. * wpa_ether_send - Send Ethernet frame
  73. * @wpa_s: Pointer to wpa_supplicant data
  74. * @dest: Destination MAC address
  75. * @proto: Ethertype in host byte order
  76. * @buf: Frame payload starting from IEEE 802.1X header
  77. * @len: Frame payload length
  78. * Returns: >=0 on success, <0 on failure
  79. */
  80. static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
  81. u16 proto, const u8 *buf, size_t len)
  82. {
  83. if (wpa_s->l2) {
  84. return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
  85. }
  86. return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
  87. }
  88. #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
  89. #ifdef IEEE8021X_EAPOL
  90. /**
  91. * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
  92. * @ctx: Pointer to wpa_supplicant data (wpa_s)
  93. * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
  94. * @buf: EAPOL payload (after IEEE 802.1X header)
  95. * @len: EAPOL payload length
  96. * Returns: >=0 on success, <0 on failure
  97. *
  98. * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
  99. * to the current Authenticator.
  100. */
  101. static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
  102. size_t len)
  103. {
  104. struct wpa_supplicant *wpa_s = ctx;
  105. u8 *msg, *dst, bssid[ETH_ALEN];
  106. size_t msglen;
  107. int res;
  108. /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
  109. * extra copy here */
  110. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  111. wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  112. /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
  113. * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
  114. * machines. */
  115. wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
  116. "mode (type=%d len=%lu)", type,
  117. (unsigned long) len);
  118. return -1;
  119. }
  120. if (pmksa_cache_get_current(wpa_s->wpa) &&
  121. type == IEEE802_1X_TYPE_EAPOL_START) {
  122. /* Trying to use PMKSA caching - do not send EAPOL-Start frames
  123. * since they will trigger full EAPOL authentication. */
  124. wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
  125. "EAPOL-Start");
  126. return -1;
  127. }
  128. if (is_zero_ether_addr(wpa_s->bssid)) {
  129. wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
  130. "EAPOL frame");
  131. if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  132. !is_zero_ether_addr(bssid)) {
  133. dst = bssid;
  134. wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
  135. " from the driver as the EAPOL destination",
  136. MAC2STR(dst));
  137. } else {
  138. dst = wpa_s->last_eapol_src;
  139. wpa_printf(MSG_DEBUG, "Using the source address of the"
  140. " last received EAPOL frame " MACSTR " as "
  141. "the EAPOL destination",
  142. MAC2STR(dst));
  143. }
  144. } else {
  145. /* BSSID was already set (from (Re)Assoc event, so use it as
  146. * the EAPOL destination. */
  147. dst = wpa_s->bssid;
  148. }
  149. msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
  150. if (msg == NULL)
  151. return -1;
  152. wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
  153. wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
  154. res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
  155. os_free(msg);
  156. return res;
  157. }
  158. /**
  159. * wpa_eapol_set_wep_key - set WEP key for the driver
  160. * @ctx: Pointer to wpa_supplicant data (wpa_s)
  161. * @unicast: 1 = individual unicast key, 0 = broadcast key
  162. * @keyidx: WEP key index (0..3)
  163. * @key: Pointer to key data
  164. * @keylen: Key length in bytes
  165. * Returns: 0 on success or < 0 on error.
  166. */
  167. static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
  168. const u8 *key, size_t keylen)
  169. {
  170. struct wpa_supplicant *wpa_s = ctx;
  171. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  172. int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
  173. WPA_CIPHER_WEP104;
  174. if (unicast)
  175. wpa_s->pairwise_cipher = cipher;
  176. else
  177. wpa_s->group_cipher = cipher;
  178. }
  179. return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
  180. unicast ? wpa_s->bssid : NULL,
  181. keyidx, unicast, NULL, 0, key, keylen);
  182. }
  183. static void wpa_supplicant_aborted_cached(void *ctx)
  184. {
  185. struct wpa_supplicant *wpa_s = ctx;
  186. wpa_sm_aborted_cached(wpa_s->wpa);
  187. }
  188. static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
  189. void *ctx)
  190. {
  191. struct wpa_supplicant *wpa_s = ctx;
  192. int res, pmk_len;
  193. u8 pmk[PMK_LEN];
  194. wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
  195. success ? "" : "un");
  196. if (wpas_wps_eapol_cb(wpa_s) > 0)
  197. return;
  198. if (!success) {
  199. /*
  200. * Make sure we do not get stuck here waiting for long EAPOL
  201. * timeout if the AP does not disconnect in case of
  202. * authentication failure.
  203. */
  204. wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
  205. }
  206. if (!success || !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  207. return;
  208. if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
  209. return;
  210. wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
  211. "handshake");
  212. pmk_len = PMK_LEN;
  213. if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
  214. #ifdef CONFIG_IEEE80211R
  215. u8 buf[2 * PMK_LEN];
  216. wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
  217. "driver-based 4-way hs and FT");
  218. res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
  219. if (res == 0) {
  220. os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
  221. os_memset(buf, 0, sizeof(buf));
  222. }
  223. #else /* CONFIG_IEEE80211R */
  224. res = -1;
  225. #endif /* CONFIG_IEEE80211R */
  226. } else {
  227. res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
  228. if (res) {
  229. /*
  230. * EAP-LEAP is an exception from other EAP methods: it
  231. * uses only 16-byte PMK.
  232. */
  233. res = eapol_sm_get_key(eapol, pmk, 16);
  234. pmk_len = 16;
  235. }
  236. }
  237. if (res) {
  238. wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
  239. "machines");
  240. return;
  241. }
  242. wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
  243. "handshake", pmk, pmk_len);
  244. if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
  245. pmk_len)) {
  246. wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
  247. }
  248. wpa_supplicant_cancel_scan(wpa_s);
  249. wpa_supplicant_cancel_auth_timeout(wpa_s);
  250. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  251. }
  252. static void wpa_supplicant_notify_eapol_done(void *ctx)
  253. {
  254. struct wpa_supplicant *wpa_s = ctx;
  255. wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
  256. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  257. wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
  258. } else {
  259. wpa_supplicant_cancel_auth_timeout(wpa_s);
  260. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  261. }
  262. }
  263. #endif /* IEEE8021X_EAPOL */
  264. #ifndef CONFIG_NO_WPA
  265. static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
  266. {
  267. int ret = 0;
  268. struct wpa_bss *curr = NULL, *bss;
  269. struct wpa_ssid *ssid = wpa_s->current_ssid;
  270. const u8 *ie;
  271. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  272. if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  273. continue;
  274. if (ssid == NULL ||
  275. ((bss->ssid_len == ssid->ssid_len &&
  276. os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
  277. ssid->ssid_len == 0)) {
  278. curr = bss;
  279. break;
  280. }
  281. }
  282. if (curr) {
  283. ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
  284. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  285. ret = -1;
  286. ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
  287. if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  288. ret = -1;
  289. } else {
  290. ret = -1;
  291. }
  292. return ret;
  293. }
  294. static int wpa_supplicant_get_beacon_ie(void *ctx)
  295. {
  296. struct wpa_supplicant *wpa_s = ctx;
  297. if (wpa_get_beacon_ie(wpa_s) == 0) {
  298. return 0;
  299. }
  300. /* No WPA/RSN IE found in the cached scan results. Try to get updated
  301. * scan results from the driver. */
  302. if (wpa_supplicant_update_scan_results(wpa_s) < 0)
  303. return -1;
  304. return wpa_get_beacon_ie(wpa_s);
  305. }
  306. static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
  307. const void *data, u16 data_len,
  308. size_t *msg_len, void **data_pos)
  309. {
  310. return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
  311. }
  312. static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
  313. const u8 *buf, size_t len)
  314. {
  315. return wpa_ether_send(wpa_s, dest, proto, buf, len);
  316. }
  317. static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
  318. {
  319. wpa_supplicant_cancel_auth_timeout(wpa_s);
  320. }
  321. static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
  322. {
  323. wpa_supplicant_set_state(wpa_s, state);
  324. }
  325. /**
  326. * wpa_supplicant_get_state - Get the connection state
  327. * @wpa_s: Pointer to wpa_supplicant data
  328. * Returns: The current connection state (WPA_*)
  329. */
  330. static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
  331. {
  332. return wpa_s->wpa_state;
  333. }
  334. static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
  335. {
  336. return wpa_supplicant_get_state(wpa_s);
  337. }
  338. static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
  339. {
  340. wpa_supplicant_deauthenticate(wpa_s, reason_code);
  341. /* Schedule a scan to make sure we continue looking for networks */
  342. wpa_supplicant_req_scan(wpa_s, 5, 0);
  343. }
  344. static void * wpa_supplicant_get_network_ctx(void *wpa_s)
  345. {
  346. return wpa_supplicant_get_ssid(wpa_s);
  347. }
  348. static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
  349. {
  350. struct wpa_supplicant *wpa_s = ctx;
  351. return wpa_drv_get_bssid(wpa_s, bssid);
  352. }
  353. static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
  354. const u8 *addr, int key_idx, int set_tx,
  355. const u8 *seq, size_t seq_len,
  356. const u8 *key, size_t key_len)
  357. {
  358. struct wpa_supplicant *wpa_s = _wpa_s;
  359. if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
  360. /* Clear the MIC error counter when setting a new PTK. */
  361. wpa_s->mic_errors_seen = 0;
  362. }
  363. #ifdef CONFIG_TESTING_GET_GTK
  364. if (key_idx > 0 && addr && is_broadcast_ether_addr(addr) &&
  365. alg != WPA_ALG_NONE && key_len <= sizeof(wpa_s->last_gtk)) {
  366. os_memcpy(wpa_s->last_gtk, key, key_len);
  367. wpa_s->last_gtk_len = key_len;
  368. }
  369. #endif /* CONFIG_TESTING_GET_GTK */
  370. return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
  371. key, key_len);
  372. }
  373. static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
  374. int protection_type,
  375. int key_type)
  376. {
  377. return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
  378. key_type);
  379. }
  380. static int wpa_supplicant_add_pmkid(void *wpa_s,
  381. const u8 *bssid, const u8 *pmkid)
  382. {
  383. return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
  384. }
  385. static int wpa_supplicant_remove_pmkid(void *wpa_s,
  386. const u8 *bssid, const u8 *pmkid)
  387. {
  388. return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
  389. }
  390. #ifdef CONFIG_IEEE80211R
  391. static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
  392. const u8 *ies, size_t ies_len)
  393. {
  394. struct wpa_supplicant *wpa_s = ctx;
  395. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  396. return sme_update_ft_ies(wpa_s, md, ies, ies_len);
  397. return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
  398. }
  399. static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
  400. const u8 *target_ap,
  401. const u8 *ies, size_t ies_len)
  402. {
  403. struct wpa_supplicant *wpa_s = ctx;
  404. return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
  405. }
  406. static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
  407. {
  408. struct wpa_supplicant *wpa_s = ctx;
  409. struct wpa_driver_auth_params params;
  410. struct wpa_bss *bss;
  411. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  412. if (bss == NULL)
  413. return -1;
  414. os_memset(&params, 0, sizeof(params));
  415. params.bssid = target_ap;
  416. params.freq = bss->freq;
  417. params.ssid = bss->ssid;
  418. params.ssid_len = bss->ssid_len;
  419. params.auth_alg = WPA_AUTH_ALG_FT;
  420. params.local_state_change = 1;
  421. return wpa_drv_authenticate(wpa_s, &params);
  422. }
  423. #endif /* CONFIG_IEEE80211R */
  424. #ifdef CONFIG_TDLS
  425. static int wpa_supplicant_tdls_get_capa(void *ctx, int *tdls_supported,
  426. int *tdls_ext_setup)
  427. {
  428. struct wpa_supplicant *wpa_s = ctx;
  429. *tdls_supported = 0;
  430. *tdls_ext_setup = 0;
  431. if (!wpa_s->drv_capa_known)
  432. return -1;
  433. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
  434. *tdls_supported = 1;
  435. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
  436. *tdls_ext_setup = 1;
  437. return 0;
  438. }
  439. static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
  440. u8 action_code, u8 dialog_token,
  441. u16 status_code, const u8 *buf,
  442. size_t len)
  443. {
  444. struct wpa_supplicant *wpa_s = ctx;
  445. return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
  446. status_code, buf, len);
  447. }
  448. static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
  449. {
  450. struct wpa_supplicant *wpa_s = ctx;
  451. return wpa_drv_tdls_oper(wpa_s, oper, peer);
  452. }
  453. static int wpa_supplicant_tdls_peer_addset(
  454. void *ctx, const u8 *peer, int add, u16 aid, u16 capability,
  455. const u8 *supp_rates, size_t supp_rates_len,
  456. const struct ieee80211_ht_capabilities *ht_capab,
  457. const struct ieee80211_vht_capabilities *vht_capab,
  458. u8 qosinfo, const u8 *ext_capab, size_t ext_capab_len)
  459. {
  460. struct wpa_supplicant *wpa_s = ctx;
  461. struct hostapd_sta_add_params params;
  462. os_memset(&params, 0, sizeof(params));
  463. params.addr = peer;
  464. params.aid = aid;
  465. params.capability = capability;
  466. params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
  467. /*
  468. * TDLS Setup frames do not contain WMM IEs, hence need to depend on
  469. * qosinfo to check if the peer is WMM capable.
  470. */
  471. if (qosinfo)
  472. params.flags |= WPA_STA_WMM;
  473. params.ht_capabilities = ht_capab;
  474. params.vht_capabilities = vht_capab;
  475. params.qosinfo = qosinfo;
  476. params.listen_interval = 0;
  477. params.supp_rates = supp_rates;
  478. params.supp_rates_len = supp_rates_len;
  479. params.set = !add;
  480. params.ext_capab = ext_capab;
  481. params.ext_capab_len = ext_capab_len;
  482. return wpa_drv_sta_add(wpa_s, &params);
  483. }
  484. #endif /* CONFIG_TDLS */
  485. #endif /* CONFIG_NO_WPA */
  486. enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
  487. {
  488. if (os_strcmp(field, "IDENTITY") == 0)
  489. return WPA_CTRL_REQ_EAP_IDENTITY;
  490. else if (os_strcmp(field, "PASSWORD") == 0)
  491. return WPA_CTRL_REQ_EAP_PASSWORD;
  492. else if (os_strcmp(field, "NEW_PASSWORD") == 0)
  493. return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
  494. else if (os_strcmp(field, "PIN") == 0)
  495. return WPA_CTRL_REQ_EAP_PIN;
  496. else if (os_strcmp(field, "OTP") == 0)
  497. return WPA_CTRL_REQ_EAP_OTP;
  498. else if (os_strcmp(field, "PASSPHRASE") == 0)
  499. return WPA_CTRL_REQ_EAP_PASSPHRASE;
  500. return WPA_CTRL_REQ_UNKNOWN;
  501. }
  502. const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
  503. const char *default_txt,
  504. const char **txt)
  505. {
  506. const char *ret = NULL;
  507. *txt = default_txt;
  508. switch (field) {
  509. case WPA_CTRL_REQ_EAP_IDENTITY:
  510. *txt = "Identity";
  511. ret = "IDENTITY";
  512. break;
  513. case WPA_CTRL_REQ_EAP_PASSWORD:
  514. *txt = "Password";
  515. ret = "PASSWORD";
  516. break;
  517. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  518. *txt = "New Password";
  519. ret = "NEW_PASSWORD";
  520. break;
  521. case WPA_CTRL_REQ_EAP_PIN:
  522. *txt = "PIN";
  523. ret = "PIN";
  524. break;
  525. case WPA_CTRL_REQ_EAP_OTP:
  526. ret = "OTP";
  527. break;
  528. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  529. *txt = "Private key passphrase";
  530. ret = "PASSPHRASE";
  531. break;
  532. default:
  533. break;
  534. }
  535. /* txt needs to be something */
  536. if (*txt == NULL) {
  537. wpa_printf(MSG_WARNING, "No message for request %d", field);
  538. ret = NULL;
  539. }
  540. return ret;
  541. }
  542. #ifdef IEEE8021X_EAPOL
  543. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  544. static void wpa_supplicant_eap_param_needed(void *ctx,
  545. enum wpa_ctrl_req_type field,
  546. const char *default_txt)
  547. {
  548. struct wpa_supplicant *wpa_s = ctx;
  549. struct wpa_ssid *ssid = wpa_s->current_ssid;
  550. const char *field_name, *txt = NULL;
  551. char *buf;
  552. size_t buflen;
  553. int len;
  554. if (ssid == NULL)
  555. return;
  556. wpas_notify_network_request(wpa_s, ssid, field, default_txt);
  557. field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
  558. &txt);
  559. if (field_name == NULL) {
  560. wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
  561. field);
  562. return;
  563. }
  564. wpas_notify_eap_status(wpa_s, "eap parameter needed", field_name);
  565. buflen = 100 + os_strlen(txt) + ssid->ssid_len;
  566. buf = os_malloc(buflen);
  567. if (buf == NULL)
  568. return;
  569. len = os_snprintf(buf, buflen,
  570. WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
  571. field_name, ssid->id, txt);
  572. if (len < 0 || (size_t) len >= buflen) {
  573. os_free(buf);
  574. return;
  575. }
  576. if (ssid->ssid && buflen > len + ssid->ssid_len) {
  577. os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
  578. len += ssid->ssid_len;
  579. buf[len] = '\0';
  580. }
  581. buf[buflen - 1] = '\0';
  582. wpa_msg(wpa_s, MSG_INFO, "%s", buf);
  583. os_free(buf);
  584. }
  585. #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  586. #define wpa_supplicant_eap_param_needed NULL
  587. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  588. static void wpa_supplicant_port_cb(void *ctx, int authorized)
  589. {
  590. struct wpa_supplicant *wpa_s = ctx;
  591. #ifdef CONFIG_AP
  592. if (wpa_s->ap_iface) {
  593. wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
  594. "port status: %s",
  595. authorized ? "Authorized" : "Unauthorized");
  596. return;
  597. }
  598. #endif /* CONFIG_AP */
  599. wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
  600. authorized ? "Authorized" : "Unauthorized");
  601. wpa_drv_set_supp_port(wpa_s, authorized);
  602. }
  603. static void wpa_supplicant_cert_cb(void *ctx, int depth, const char *subject,
  604. const char *cert_hash,
  605. const struct wpabuf *cert)
  606. {
  607. struct wpa_supplicant *wpa_s = ctx;
  608. wpas_notify_certification(wpa_s, depth, subject, cert_hash, cert);
  609. }
  610. static void wpa_supplicant_status_cb(void *ctx, const char *status,
  611. const char *parameter)
  612. {
  613. struct wpa_supplicant *wpa_s = ctx;
  614. wpas_notify_eap_status(wpa_s, status, parameter);
  615. }
  616. static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
  617. {
  618. struct wpa_supplicant *wpa_s = ctx;
  619. char *str;
  620. int res;
  621. wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
  622. id, len);
  623. if (wpa_s->current_ssid == NULL)
  624. return;
  625. if (id == NULL) {
  626. if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
  627. "NULL", 0) < 0)
  628. return;
  629. } else {
  630. str = os_malloc(len * 2 + 1);
  631. if (str == NULL)
  632. return;
  633. wpa_snprintf_hex(str, len * 2 + 1, id, len);
  634. res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
  635. str, 0);
  636. os_free(str);
  637. if (res < 0)
  638. return;
  639. }
  640. if (wpa_s->conf->update_config) {
  641. res = wpa_config_write(wpa_s->confname, wpa_s->conf);
  642. if (res) {
  643. wpa_printf(MSG_DEBUG, "Failed to update config after "
  644. "anonymous_id update");
  645. }
  646. }
  647. }
  648. #endif /* IEEE8021X_EAPOL */
  649. int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
  650. {
  651. #ifdef IEEE8021X_EAPOL
  652. struct eapol_ctx *ctx;
  653. ctx = os_zalloc(sizeof(*ctx));
  654. if (ctx == NULL) {
  655. wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
  656. return -1;
  657. }
  658. ctx->ctx = wpa_s;
  659. ctx->msg_ctx = wpa_s;
  660. ctx->eapol_send_ctx = wpa_s;
  661. ctx->preauth = 0;
  662. ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
  663. ctx->eapol_send = wpa_supplicant_eapol_send;
  664. ctx->set_wep_key = wpa_eapol_set_wep_key;
  665. #ifndef CONFIG_NO_CONFIG_BLOBS
  666. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  667. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  668. #endif /* CONFIG_NO_CONFIG_BLOBS */
  669. ctx->aborted_cached = wpa_supplicant_aborted_cached;
  670. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  671. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  672. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  673. ctx->wps = wpa_s->wps;
  674. ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
  675. ctx->port_cb = wpa_supplicant_port_cb;
  676. ctx->cb = wpa_supplicant_eapol_cb;
  677. ctx->cert_cb = wpa_supplicant_cert_cb;
  678. ctx->status_cb = wpa_supplicant_status_cb;
  679. ctx->set_anon_id = wpa_supplicant_set_anon_id;
  680. ctx->cb_ctx = wpa_s;
  681. wpa_s->eapol = eapol_sm_init(ctx);
  682. if (wpa_s->eapol == NULL) {
  683. os_free(ctx);
  684. wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
  685. "machines.");
  686. return -1;
  687. }
  688. #endif /* IEEE8021X_EAPOL */
  689. return 0;
  690. }
  691. #ifndef CONFIG_NO_WPA
  692. static void wpa_supplicant_set_rekey_offload(void *ctx, const u8 *kek,
  693. const u8 *kck,
  694. const u8 *replay_ctr)
  695. {
  696. struct wpa_supplicant *wpa_s = ctx;
  697. wpa_drv_set_rekey_info(wpa_s, kek, kck, replay_ctr);
  698. }
  699. #endif /* CONFIG_NO_WPA */
  700. int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
  701. {
  702. #ifndef CONFIG_NO_WPA
  703. struct wpa_sm_ctx *ctx;
  704. ctx = os_zalloc(sizeof(*ctx));
  705. if (ctx == NULL) {
  706. wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
  707. return -1;
  708. }
  709. ctx->ctx = wpa_s;
  710. ctx->msg_ctx = wpa_s;
  711. ctx->set_state = _wpa_supplicant_set_state;
  712. ctx->get_state = _wpa_supplicant_get_state;
  713. ctx->deauthenticate = _wpa_supplicant_deauthenticate;
  714. ctx->set_key = wpa_supplicant_set_key;
  715. ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
  716. ctx->get_bssid = wpa_supplicant_get_bssid;
  717. ctx->ether_send = _wpa_ether_send;
  718. ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
  719. ctx->alloc_eapol = _wpa_alloc_eapol;
  720. ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
  721. ctx->add_pmkid = wpa_supplicant_add_pmkid;
  722. ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
  723. #ifndef CONFIG_NO_CONFIG_BLOBS
  724. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  725. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  726. #endif /* CONFIG_NO_CONFIG_BLOBS */
  727. ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
  728. #ifdef CONFIG_IEEE80211R
  729. ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
  730. ctx->send_ft_action = wpa_supplicant_send_ft_action;
  731. ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
  732. #endif /* CONFIG_IEEE80211R */
  733. #ifdef CONFIG_TDLS
  734. ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
  735. ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
  736. ctx->tdls_oper = wpa_supplicant_tdls_oper;
  737. ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
  738. #endif /* CONFIG_TDLS */
  739. ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
  740. wpa_s->wpa = wpa_sm_init(ctx);
  741. if (wpa_s->wpa == NULL) {
  742. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  743. "machine");
  744. return -1;
  745. }
  746. #endif /* CONFIG_NO_WPA */
  747. return 0;
  748. }
  749. void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
  750. struct wpa_ssid *ssid)
  751. {
  752. struct rsn_supp_config conf;
  753. if (ssid) {
  754. os_memset(&conf, 0, sizeof(conf));
  755. conf.network_ctx = ssid;
  756. conf.peerkey_enabled = ssid->peerkey;
  757. conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
  758. #ifdef IEEE8021X_EAPOL
  759. conf.proactive_key_caching = ssid->proactive_key_caching < 0 ?
  760. wpa_s->conf->okc : ssid->proactive_key_caching;
  761. conf.eap_workaround = ssid->eap_workaround;
  762. conf.eap_conf_ctx = &ssid->eap;
  763. #endif /* IEEE8021X_EAPOL */
  764. conf.ssid = ssid->ssid;
  765. conf.ssid_len = ssid->ssid_len;
  766. conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
  767. }
  768. wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
  769. }