wpas_glue.c 25 KB

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