wpas_glue.c 26 KB

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