wpas_glue.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * WPA Supplicant - Glue code to setup EAPOL and RSN modules
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "wpa.h"
  18. #include "eloop.h"
  19. #include "config.h"
  20. #include "l2_packet/l2_packet.h"
  21. #include "wpa_common.h"
  22. #include "wpa_supplicant_i.h"
  23. #include "pmksa_cache.h"
  24. #include "mlme.h"
  25. #include "ieee802_11_defs.h"
  26. #include "wps/wps.h"
  27. #include "wps/wps_defs.h"
  28. #include "wpa_ctrl.h"
  29. #include "wpas_glue.h"
  30. #ifndef CONFIG_NO_CONFIG_BLOBS
  31. #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
  32. static void wpa_supplicant_set_config_blob(void *ctx,
  33. struct wpa_config_blob *blob)
  34. {
  35. struct wpa_supplicant *wpa_s = ctx;
  36. wpa_config_set_blob(wpa_s->conf, blob);
  37. if (wpa_s->conf->update_config) {
  38. int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  39. if (ret) {
  40. wpa_printf(MSG_DEBUG, "Failed to update config after "
  41. "blob set");
  42. }
  43. }
  44. }
  45. static const struct wpa_config_blob *
  46. wpa_supplicant_get_config_blob(void *ctx, const char *name)
  47. {
  48. struct wpa_supplicant *wpa_s = ctx;
  49. return wpa_config_get_blob(wpa_s->conf, name);
  50. }
  51. #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
  52. #endif /* CONFIG_NO_CONFIG_BLOBS */
  53. #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
  54. static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
  55. const void *data, u16 data_len,
  56. size_t *msg_len, void **data_pos)
  57. {
  58. struct ieee802_1x_hdr *hdr;
  59. *msg_len = sizeof(*hdr) + data_len;
  60. hdr = os_malloc(*msg_len);
  61. if (hdr == NULL)
  62. return NULL;
  63. hdr->version = wpa_s->conf->eapol_version;
  64. hdr->type = type;
  65. hdr->length = host_to_be16(data_len);
  66. if (data)
  67. os_memcpy(hdr + 1, data, data_len);
  68. else
  69. os_memset(hdr + 1, 0, data_len);
  70. if (data_pos)
  71. *data_pos = hdr + 1;
  72. return (u8 *) hdr;
  73. }
  74. /**
  75. * wpa_ether_send - Send Ethernet frame
  76. * @wpa_s: Pointer to wpa_supplicant data
  77. * @dest: Destination MAC address
  78. * @proto: Ethertype in host byte order
  79. * @buf: Frame payload starting from IEEE 802.1X header
  80. * @len: Frame payload length
  81. * Returns: >=0 on success, <0 on failure
  82. */
  83. static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
  84. u16 proto, const u8 *buf, size_t len)
  85. {
  86. if (wpa_s->l2) {
  87. return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
  88. }
  89. return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
  90. }
  91. #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
  92. #ifdef IEEE8021X_EAPOL
  93. /**
  94. * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
  95. * @ctx: Pointer to wpa_supplicant data (wpa_s)
  96. * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
  97. * @buf: EAPOL payload (after IEEE 802.1X header)
  98. * @len: EAPOL payload length
  99. * Returns: >=0 on success, <0 on failure
  100. *
  101. * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
  102. * to the current Authenticator.
  103. */
  104. static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
  105. size_t len)
  106. {
  107. struct wpa_supplicant *wpa_s = ctx;
  108. u8 *msg, *dst, bssid[ETH_ALEN];
  109. size_t msglen;
  110. int res;
  111. /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
  112. * extra copy here */
  113. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  114. wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  115. /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
  116. * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
  117. * machines. */
  118. wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
  119. "mode (type=%d len=%lu)", type,
  120. (unsigned long) len);
  121. return -1;
  122. }
  123. if (pmksa_cache_get_current(wpa_s->wpa) &&
  124. type == IEEE802_1X_TYPE_EAPOL_START) {
  125. /* Trying to use PMKSA caching - do not send EAPOL-Start frames
  126. * since they will trigger full EAPOL authentication. */
  127. wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
  128. "EAPOL-Start");
  129. return -1;
  130. }
  131. if (is_zero_ether_addr(wpa_s->bssid)) {
  132. wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
  133. "EAPOL frame");
  134. if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  135. !is_zero_ether_addr(bssid)) {
  136. dst = bssid;
  137. wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
  138. " from the driver as the EAPOL destination",
  139. MAC2STR(dst));
  140. } else {
  141. dst = wpa_s->last_eapol_src;
  142. wpa_printf(MSG_DEBUG, "Using the source address of the"
  143. " last received EAPOL frame " MACSTR " as "
  144. "the EAPOL destination",
  145. MAC2STR(dst));
  146. }
  147. } else {
  148. /* BSSID was already set (from (Re)Assoc event, so use it as
  149. * the EAPOL destination. */
  150. dst = wpa_s->bssid;
  151. }
  152. msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
  153. if (msg == NULL)
  154. return -1;
  155. wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
  156. wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
  157. res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
  158. os_free(msg);
  159. return res;
  160. }
  161. /**
  162. * wpa_eapol_set_wep_key - set WEP key for the driver
  163. * @ctx: Pointer to wpa_supplicant data (wpa_s)
  164. * @unicast: 1 = individual unicast key, 0 = broadcast key
  165. * @keyidx: WEP key index (0..3)
  166. * @key: Pointer to key data
  167. * @keylen: Key length in bytes
  168. * Returns: 0 on success or < 0 on error.
  169. */
  170. static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
  171. const u8 *key, size_t keylen)
  172. {
  173. struct wpa_supplicant *wpa_s = ctx;
  174. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  175. int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
  176. WPA_CIPHER_WEP104;
  177. if (unicast)
  178. wpa_s->pairwise_cipher = cipher;
  179. else
  180. wpa_s->group_cipher = cipher;
  181. }
  182. return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
  183. unicast ? wpa_s->bssid :
  184. (u8 *) "\xff\xff\xff\xff\xff\xff",
  185. keyidx, unicast, (u8 *) "", 0, key, keylen);
  186. }
  187. static void wpa_supplicant_aborted_cached(void *ctx)
  188. {
  189. struct wpa_supplicant *wpa_s = ctx;
  190. wpa_sm_aborted_cached(wpa_s->wpa);
  191. }
  192. static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
  193. void *ctx)
  194. {
  195. struct wpa_supplicant *wpa_s = ctx;
  196. int res, pmk_len;
  197. u8 pmk[PMK_LEN];
  198. wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
  199. success ? "" : "un");
  200. #ifdef CONFIG_WPS
  201. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  202. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  203. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  204. "try to associate with the received credential");
  205. wpa_supplicant_deauthenticate(wpa_s,
  206. WLAN_REASON_DEAUTH_LEAVING);
  207. wpa_s->reassociate = 1;
  208. wpa_supplicant_req_scan(wpa_s, 0, 0);
  209. return;
  210. }
  211. #endif /* CONFIG_WPS */
  212. if (!success) {
  213. /*
  214. * Make sure we do not get stuck here waiting for long EAPOL
  215. * timeout if the AP does not disconnect in case of
  216. * authentication failure.
  217. */
  218. wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
  219. }
  220. if (!success || !wpa_s->driver_4way_handshake)
  221. return;
  222. if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
  223. return;
  224. wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
  225. "handshake");
  226. pmk_len = PMK_LEN;
  227. res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
  228. if (res) {
  229. /*
  230. * EAP-LEAP is an exception from other EAP methods: it
  231. * uses only 16-byte PMK.
  232. */
  233. res = eapol_sm_get_key(eapol, pmk, 16);
  234. pmk_len = 16;
  235. }
  236. if (res) {
  237. wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
  238. "machines");
  239. return;
  240. }
  241. if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
  242. pmk_len)) {
  243. wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
  244. }
  245. wpa_supplicant_cancel_scan(wpa_s);
  246. wpa_supplicant_cancel_auth_timeout(wpa_s);
  247. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  248. }
  249. static void wpa_supplicant_notify_eapol_done(void *ctx)
  250. {
  251. struct wpa_supplicant *wpa_s = ctx;
  252. wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
  253. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  254. wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
  255. } else {
  256. wpa_supplicant_cancel_auth_timeout(wpa_s);
  257. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  258. }
  259. }
  260. #endif /* IEEE8021X_EAPOL */
  261. #ifndef CONFIG_NO_WPA
  262. static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
  263. {
  264. size_t i;
  265. int ret = 0;
  266. struct wpa_scan_res *curr = NULL;
  267. struct wpa_ssid *ssid = wpa_s->current_ssid;
  268. const u8 *ie;
  269. if (wpa_s->scan_res == NULL)
  270. return -1;
  271. for (i = 0; i < wpa_s->scan_res->num; i++) {
  272. struct wpa_scan_res *r = wpa_s->scan_res->res[i];
  273. if (os_memcmp(r->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  274. continue;
  275. ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
  276. if (ssid == NULL ||
  277. ((ie && ie[1] == ssid->ssid_len &&
  278. os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) == 0) ||
  279. ssid->ssid_len == 0)) {
  280. curr = r;
  281. break;
  282. }
  283. }
  284. if (curr) {
  285. ie = wpa_scan_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
  286. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  287. ret = -1;
  288. ie = wpa_scan_get_ie(curr, WLAN_EID_RSN);
  289. if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
  290. ret = -1;
  291. } else {
  292. ret = -1;
  293. }
  294. return ret;
  295. }
  296. static int wpa_supplicant_get_beacon_ie(void *ctx)
  297. {
  298. struct wpa_supplicant *wpa_s = ctx;
  299. if (wpa_get_beacon_ie(wpa_s) == 0) {
  300. return 0;
  301. }
  302. /* No WPA/RSN IE found in the cached scan results. Try to get updated
  303. * scan results from the driver. */
  304. if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
  305. return -1;
  306. }
  307. return wpa_get_beacon_ie(wpa_s);
  308. }
  309. static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
  310. const void *data, u16 data_len,
  311. size_t *msg_len, void **data_pos)
  312. {
  313. return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
  314. }
  315. static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
  316. const u8 *buf, size_t len)
  317. {
  318. return wpa_ether_send(wpa_s, dest, proto, buf, len);
  319. }
  320. static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
  321. {
  322. wpa_supplicant_cancel_auth_timeout(wpa_s);
  323. }
  324. static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state)
  325. {
  326. wpa_supplicant_set_state(wpa_s, state);
  327. }
  328. /**
  329. * wpa_supplicant_get_state - Get the connection state
  330. * @wpa_s: Pointer to wpa_supplicant data
  331. * Returns: The current connection state (WPA_*)
  332. */
  333. static wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
  334. {
  335. return wpa_s->wpa_state;
  336. }
  337. static wpa_states _wpa_supplicant_get_state(void *wpa_s)
  338. {
  339. return wpa_supplicant_get_state(wpa_s);
  340. }
  341. static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
  342. {
  343. wpa_supplicant_disassociate(wpa_s, reason_code);
  344. /* Schedule a scan to make sure we continue looking for networks */
  345. wpa_supplicant_req_scan(wpa_s, 0, 0);
  346. }
  347. static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
  348. {
  349. wpa_supplicant_deauthenticate(wpa_s, reason_code);
  350. /* Schedule a scan to make sure we continue looking for networks */
  351. wpa_supplicant_req_scan(wpa_s, 0, 0);
  352. }
  353. static void * wpa_supplicant_get_network_ctx(void *wpa_s)
  354. {
  355. return wpa_supplicant_get_ssid(wpa_s);
  356. }
  357. static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
  358. {
  359. struct wpa_supplicant *wpa_s = ctx;
  360. if (wpa_s->use_client_mlme) {
  361. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  362. return 0;
  363. }
  364. return wpa_drv_get_bssid(wpa_s, bssid);
  365. }
  366. static int wpa_supplicant_set_key(void *_wpa_s, wpa_alg alg,
  367. const u8 *addr, int key_idx, int set_tx,
  368. const u8 *seq, size_t seq_len,
  369. const u8 *key, size_t key_len)
  370. {
  371. struct wpa_supplicant *wpa_s = _wpa_s;
  372. if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
  373. /* Clear the MIC error counter when setting a new PTK. */
  374. wpa_s->mic_errors_seen = 0;
  375. }
  376. return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
  377. key, key_len);
  378. }
  379. static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
  380. int protection_type,
  381. int key_type)
  382. {
  383. return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
  384. key_type);
  385. }
  386. static int wpa_supplicant_add_pmkid(void *wpa_s,
  387. const u8 *bssid, const u8 *pmkid)
  388. {
  389. return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
  390. }
  391. static int wpa_supplicant_remove_pmkid(void *wpa_s,
  392. const u8 *bssid, const u8 *pmkid)
  393. {
  394. return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
  395. }
  396. #ifdef CONFIG_IEEE80211R
  397. static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
  398. const u8 *ies, size_t ies_len)
  399. {
  400. struct wpa_supplicant *wpa_s = ctx;
  401. if (wpa_s->use_client_mlme)
  402. return ieee80211_sta_update_ft_ies(wpa_s, md, ies, ies_len);
  403. return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
  404. }
  405. static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
  406. const u8 *target_ap,
  407. const u8 *ies, size_t ies_len)
  408. {
  409. struct wpa_supplicant *wpa_s = ctx;
  410. if (wpa_s->use_client_mlme)
  411. return ieee80211_sta_send_ft_action(wpa_s, action, target_ap,
  412. ies, ies_len);
  413. return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
  414. }
  415. #endif /* CONFIG_IEEE80211R */
  416. #endif /* CONFIG_NO_WPA */
  417. #ifdef CONFIG_WPS
  418. static int wpa_supplicant_wps_cred(void *ctx, struct wps_credential *cred)
  419. {
  420. struct wpa_supplicant *wpa_s = ctx;
  421. struct wpa_ssid *ssid = wpa_s->current_ssid;
  422. wpa_msg(wpa_s, MSG_INFO, "WPS: New credential received");
  423. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  424. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  425. "on the received credential");
  426. os_free(ssid->eap.identity);
  427. ssid->eap.identity = NULL;
  428. ssid->eap.identity_len = 0;
  429. os_free(ssid->eap.phase1);
  430. ssid->eap.phase1 = NULL;
  431. os_free(ssid->eap.eap_methods);
  432. ssid->eap.eap_methods = NULL;
  433. } else {
  434. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  435. "received credential");
  436. ssid = wpa_config_add_network(wpa_s->conf);
  437. if (ssid == NULL)
  438. return -1;
  439. }
  440. wpa_config_set_network_defaults(ssid);
  441. os_free(ssid->ssid);
  442. ssid->ssid = os_malloc(cred->ssid_len);
  443. if (ssid->ssid) {
  444. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  445. ssid->ssid_len = cred->ssid_len;
  446. }
  447. switch (cred->encr_type) {
  448. case WPS_ENCR_NONE:
  449. ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
  450. break;
  451. case WPS_ENCR_WEP:
  452. ssid->pairwise_cipher = ssid->group_cipher =
  453. WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
  454. if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
  455. cred->key_idx < NUM_WEP_KEYS) {
  456. os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
  457. cred->key_len);
  458. ssid->wep_key_len[cred->key_idx] = cred->key_len;
  459. ssid->wep_tx_keyidx = cred->key_idx;
  460. }
  461. break;
  462. case WPS_ENCR_TKIP:
  463. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  464. ssid->group_cipher = WPA_CIPHER_TKIP;
  465. break;
  466. case WPS_ENCR_AES:
  467. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  468. ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
  469. break;
  470. }
  471. switch (cred->auth_type) {
  472. case WPS_AUTH_OPEN:
  473. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  474. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  475. ssid->proto = 0;
  476. break;
  477. case WPS_AUTH_SHARED:
  478. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  479. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  480. ssid->proto = 0;
  481. break;
  482. case WPS_AUTH_WPAPSK:
  483. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  484. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  485. ssid->proto = WPA_PROTO_WPA;
  486. break;
  487. case WPS_AUTH_WPA:
  488. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  489. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  490. ssid->proto = WPA_PROTO_WPA;
  491. break;
  492. case WPS_AUTH_WPA2:
  493. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  494. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  495. ssid->proto = WPA_PROTO_RSN;
  496. break;
  497. case WPS_AUTH_WPA2PSK:
  498. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  499. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  500. ssid->proto = WPA_PROTO_RSN;
  501. break;
  502. }
  503. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  504. if (cred->key_len == 2 * PMK_LEN) {
  505. if (hexstr2bin((const char *) cred->key, ssid->psk,
  506. PMK_LEN)) {
  507. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  508. "Key");
  509. return -1;
  510. }
  511. ssid->psk_set = 1;
  512. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  513. os_free(ssid->passphrase);
  514. ssid->passphrase = os_malloc(cred->key_len + 1);
  515. if (ssid->passphrase == NULL)
  516. return -1;
  517. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  518. ssid->passphrase[cred->key_len] = '\0';
  519. wpa_config_update_psk(ssid);
  520. } else {
  521. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  522. "length %lu",
  523. (unsigned long) cred->key_len);
  524. return -1;
  525. }
  526. }
  527. #ifndef CONFIG_NO_CONFIG_WRITE
  528. if (wpa_s->conf->update_config &&
  529. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  530. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  531. return -1;
  532. }
  533. #endif /* CONFIG_NO_CONFIG_WRITE */
  534. return 0;
  535. }
  536. #else /* CONFIG_WPS */
  537. #define wpa_supplicant_wps_cred NULL
  538. #endif /* CONFIG_WPS */
  539. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  540. static void wpa_supplicant_eap_param_needed(void *ctx, const char *field,
  541. const char *txt)
  542. {
  543. struct wpa_supplicant *wpa_s = ctx;
  544. struct wpa_ssid *ssid = wpa_s->current_ssid;
  545. char *buf;
  546. size_t buflen;
  547. int len;
  548. if (ssid == NULL)
  549. return;
  550. buflen = 100 + os_strlen(txt) + ssid->ssid_len;
  551. buf = os_malloc(buflen);
  552. if (buf == NULL)
  553. return;
  554. len = os_snprintf(buf, buflen,
  555. WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
  556. field, ssid->id, txt);
  557. if (len < 0 || (size_t) len >= buflen) {
  558. os_free(buf);
  559. return;
  560. }
  561. if (ssid->ssid && buflen > len + ssid->ssid_len) {
  562. os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
  563. len += ssid->ssid_len;
  564. buf[len] = '\0';
  565. }
  566. buf[buflen - 1] = '\0';
  567. wpa_msg(wpa_s, MSG_INFO, "%s", buf);
  568. os_free(buf);
  569. }
  570. #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  571. #define wpa_supplicant_eap_param_needed NULL
  572. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  573. int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
  574. {
  575. #ifdef IEEE8021X_EAPOL
  576. struct eapol_ctx *ctx;
  577. ctx = os_zalloc(sizeof(*ctx));
  578. if (ctx == NULL) {
  579. wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
  580. return -1;
  581. }
  582. ctx->ctx = wpa_s;
  583. ctx->msg_ctx = wpa_s;
  584. ctx->eapol_send_ctx = wpa_s;
  585. ctx->preauth = 0;
  586. ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
  587. ctx->eapol_send = wpa_supplicant_eapol_send;
  588. ctx->set_wep_key = wpa_eapol_set_wep_key;
  589. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  590. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  591. ctx->aborted_cached = wpa_supplicant_aborted_cached;
  592. #ifdef EAP_TLS_OPENSSL
  593. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  594. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  595. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  596. #endif /* EAP_TLS_OPENSSL */
  597. ctx->mac_addr = wpa_s->own_addr;
  598. ctx->uuid = wpa_s->conf->uuid;
  599. ctx->wps_cred = wpa_supplicant_wps_cred;
  600. ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
  601. ctx->cb = wpa_supplicant_eapol_cb;
  602. ctx->cb_ctx = wpa_s;
  603. wpa_s->eapol = eapol_sm_init(ctx);
  604. if (wpa_s->eapol == NULL) {
  605. os_free(ctx);
  606. wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
  607. "machines.");
  608. return -1;
  609. }
  610. #endif /* IEEE8021X_EAPOL */
  611. return 0;
  612. }
  613. int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
  614. {
  615. #ifndef CONFIG_NO_WPA
  616. struct wpa_sm_ctx *ctx;
  617. ctx = os_zalloc(sizeof(*ctx));
  618. if (ctx == NULL) {
  619. wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
  620. return -1;
  621. }
  622. ctx->ctx = wpa_s;
  623. ctx->set_state = _wpa_supplicant_set_state;
  624. ctx->get_state = _wpa_supplicant_get_state;
  625. ctx->deauthenticate = _wpa_supplicant_deauthenticate;
  626. ctx->disassociate = _wpa_supplicant_disassociate;
  627. ctx->set_key = wpa_supplicant_set_key;
  628. ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
  629. ctx->get_bssid = wpa_supplicant_get_bssid;
  630. ctx->ether_send = _wpa_ether_send;
  631. ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
  632. ctx->alloc_eapol = _wpa_alloc_eapol;
  633. ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
  634. ctx->add_pmkid = wpa_supplicant_add_pmkid;
  635. ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
  636. #ifndef CONFIG_NO_CONFIG_BLOBS
  637. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  638. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  639. #endif /* CONFIG_NO_CONFIG_BLOBS */
  640. ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
  641. #ifdef CONFIG_IEEE80211R
  642. ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
  643. ctx->send_ft_action = wpa_supplicant_send_ft_action;
  644. #endif /* CONFIG_IEEE80211R */
  645. wpa_s->wpa = wpa_sm_init(ctx);
  646. if (wpa_s->wpa == NULL) {
  647. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  648. "machine");
  649. return -1;
  650. }
  651. #endif /* CONFIG_NO_WPA */
  652. return 0;
  653. }
  654. void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
  655. struct wpa_ssid *ssid)
  656. {
  657. struct rsn_supp_config conf;
  658. if (ssid) {
  659. os_memset(&conf, 0, sizeof(conf));
  660. conf.network_ctx = ssid;
  661. conf.peerkey_enabled = ssid->peerkey;
  662. conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
  663. #ifdef IEEE8021X_EAPOL
  664. conf.eap_workaround = ssid->eap_workaround;
  665. conf.eap_conf_ctx = &ssid->eap;
  666. #endif /* IEEE8021X_EAPOL */
  667. conf.ssid = ssid->ssid;
  668. conf.ssid_len = ssid->ssid_len;
  669. conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
  670. }
  671. wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
  672. }