wpas_glue.c 26 KB

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