wps_supplicant.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * wpa_supplicant / WPS integration
  3. * Copyright (c) 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 "ieee802_11_defs.h"
  17. #include "wpa_common.h"
  18. #include "config.h"
  19. #include "eap_peer/eap.h"
  20. #include "wpa_supplicant_i.h"
  21. #include "eloop.h"
  22. #include "wpa_ctrl.h"
  23. #include "eap_common/eap_wsc_common.h"
  24. #include "wps/wps.h"
  25. #include "wps/wps_defs.h"
  26. #include "wps_supplicant.h"
  27. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
  28. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  29. {
  30. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  31. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  32. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  33. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  34. "try to associate with the received credential");
  35. wpa_supplicant_deauthenticate(wpa_s,
  36. WLAN_REASON_DEAUTH_LEAVING);
  37. wpa_s->reassociate = 1;
  38. wpa_supplicant_req_scan(wpa_s, 0, 0);
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. static int wpa_supplicant_wps_cred(void *ctx,
  44. const struct wps_credential *cred)
  45. {
  46. struct wpa_supplicant *wpa_s = ctx;
  47. struct wpa_ssid *ssid = wpa_s->current_ssid;
  48. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
  49. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  50. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  51. "on the received credential");
  52. os_free(ssid->eap.identity);
  53. ssid->eap.identity = NULL;
  54. ssid->eap.identity_len = 0;
  55. os_free(ssid->eap.phase1);
  56. ssid->eap.phase1 = NULL;
  57. os_free(ssid->eap.eap_methods);
  58. ssid->eap.eap_methods = NULL;
  59. } else {
  60. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  61. "received credential");
  62. ssid = wpa_config_add_network(wpa_s->conf);
  63. if (ssid == NULL)
  64. return -1;
  65. }
  66. wpa_config_set_network_defaults(ssid);
  67. os_free(ssid->ssid);
  68. ssid->ssid = os_malloc(cred->ssid_len);
  69. if (ssid->ssid) {
  70. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  71. ssid->ssid_len = cred->ssid_len;
  72. }
  73. switch (cred->encr_type) {
  74. case WPS_ENCR_NONE:
  75. ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
  76. break;
  77. case WPS_ENCR_WEP:
  78. ssid->pairwise_cipher = ssid->group_cipher =
  79. WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
  80. if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
  81. cred->key_idx < NUM_WEP_KEYS) {
  82. os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
  83. cred->key_len);
  84. ssid->wep_key_len[cred->key_idx] = cred->key_len;
  85. ssid->wep_tx_keyidx = cred->key_idx;
  86. }
  87. break;
  88. case WPS_ENCR_TKIP:
  89. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  90. ssid->group_cipher = WPA_CIPHER_TKIP;
  91. break;
  92. case WPS_ENCR_AES:
  93. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  94. ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
  95. break;
  96. }
  97. switch (cred->auth_type) {
  98. case WPS_AUTH_OPEN:
  99. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  100. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  101. ssid->proto = 0;
  102. break;
  103. case WPS_AUTH_SHARED:
  104. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  105. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  106. ssid->proto = 0;
  107. break;
  108. case WPS_AUTH_WPAPSK:
  109. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  110. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  111. ssid->proto = WPA_PROTO_WPA;
  112. break;
  113. case WPS_AUTH_WPA:
  114. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  115. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  116. ssid->proto = WPA_PROTO_WPA;
  117. break;
  118. case WPS_AUTH_WPA2:
  119. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  120. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  121. ssid->proto = WPA_PROTO_RSN;
  122. break;
  123. case WPS_AUTH_WPA2PSK:
  124. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  125. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  126. ssid->proto = WPA_PROTO_RSN;
  127. break;
  128. }
  129. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  130. if (cred->key_len == 2 * PMK_LEN) {
  131. if (hexstr2bin((const char *) cred->key, ssid->psk,
  132. PMK_LEN)) {
  133. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  134. "Key");
  135. return -1;
  136. }
  137. ssid->psk_set = 1;
  138. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  139. os_free(ssid->passphrase);
  140. ssid->passphrase = os_malloc(cred->key_len + 1);
  141. if (ssid->passphrase == NULL)
  142. return -1;
  143. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  144. ssid->passphrase[cred->key_len] = '\0';
  145. wpa_config_update_psk(ssid);
  146. } else {
  147. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  148. "length %lu",
  149. (unsigned long) cred->key_len);
  150. return -1;
  151. }
  152. }
  153. #ifndef CONFIG_NO_CONFIG_WRITE
  154. if (wpa_s->conf->update_config &&
  155. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  156. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  157. return -1;
  158. }
  159. #endif /* CONFIG_NO_CONFIG_WRITE */
  160. return 0;
  161. }
  162. u8 wpas_wps_get_req_type(struct wpa_ssid *ssid)
  163. {
  164. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  165. eap_is_wps_pin_enrollee(&ssid->eap))
  166. return WPS_REQ_ENROLLEE;
  167. else
  168. return WPS_REQ_REGISTRAR;
  169. }
  170. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  171. {
  172. int id;
  173. struct wpa_ssid *ssid;
  174. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  175. /* Remove any existing WPS network from configuration */
  176. ssid = wpa_s->conf->ssid;
  177. while (ssid) {
  178. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  179. if (ssid == wpa_s->current_ssid)
  180. wpa_s->current_ssid = NULL;
  181. id = ssid->id;
  182. } else
  183. id = -1;
  184. ssid = ssid->next;
  185. if (id >= 0)
  186. wpa_config_remove_network(wpa_s->conf, id);
  187. }
  188. }
  189. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  190. {
  191. struct wpa_supplicant *wpa_s = eloop_ctx;
  192. wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
  193. wpas_clear_wps(wpa_s);
  194. }
  195. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  196. int registrar, const u8 *bssid)
  197. {
  198. struct wpa_ssid *ssid;
  199. ssid = wpa_config_add_network(wpa_s->conf);
  200. if (ssid == NULL)
  201. return NULL;
  202. wpa_config_set_network_defaults(ssid);
  203. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  204. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  205. wpa_config_set(ssid, "identity", registrar ?
  206. "\"" WSC_ID_REGISTRAR "\"" :
  207. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  208. wpa_config_remove_network(wpa_s->conf, ssid->id);
  209. return NULL;
  210. }
  211. if (bssid) {
  212. size_t i;
  213. struct wpa_scan_res *res;
  214. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  215. /* Try to get SSID from scan results */
  216. if (wpa_s->scan_res == NULL &&
  217. wpa_supplicant_get_scan_results(wpa_s) < 0)
  218. return ssid; /* Could not find any scan results */
  219. for (i = 0; i < wpa_s->scan_res->num; i++) {
  220. const u8 *ie;
  221. res = wpa_s->scan_res->res[i];
  222. if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
  223. continue;
  224. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  225. if (ie == NULL)
  226. break;
  227. os_free(ssid->ssid);
  228. ssid->ssid = os_malloc(ie[1]);
  229. if (ssid->ssid == NULL)
  230. break;
  231. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  232. ssid->ssid_len = ie[1];
  233. break;
  234. }
  235. }
  236. return ssid;
  237. }
  238. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  239. struct wpa_ssid *selected)
  240. {
  241. struct wpa_ssid *ssid;
  242. /* Mark all other networks disabled and trigger reassociation */
  243. ssid = wpa_s->conf->ssid;
  244. while (ssid) {
  245. ssid->disabled = ssid != selected;
  246. ssid = ssid->next;
  247. }
  248. wpa_s->disconnected = 0;
  249. wpa_s->reassociate = 1;
  250. wpa_supplicant_req_scan(wpa_s, 0, 0);
  251. }
  252. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  253. {
  254. struct wpa_ssid *ssid;
  255. wpas_clear_wps(wpa_s);
  256. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  257. if (ssid == NULL)
  258. return -1;
  259. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  260. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  261. wpa_s, NULL);
  262. wpas_wps_reassoc(wpa_s, ssid);
  263. return 0;
  264. }
  265. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  266. const char *pin)
  267. {
  268. struct wpa_ssid *ssid;
  269. char val[30];
  270. unsigned int rpin = 0;
  271. wpas_clear_wps(wpa_s);
  272. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  273. if (ssid == NULL)
  274. return -1;
  275. if (pin)
  276. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  277. else {
  278. rpin = wps_generate_pin();
  279. os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
  280. }
  281. wpa_config_set(ssid, "phase1", val, 0);
  282. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  283. wpa_s, NULL);
  284. wpas_wps_reassoc(wpa_s, ssid);
  285. return rpin;
  286. }
  287. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  288. const char *pin)
  289. {
  290. struct wpa_ssid *ssid;
  291. char val[30];
  292. if (!pin)
  293. return -1;
  294. wpas_clear_wps(wpa_s);
  295. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  296. if (ssid == NULL)
  297. return -1;
  298. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  299. wpa_config_set(ssid, "phase1", val, 0);
  300. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  301. wpa_s, NULL);
  302. wpas_wps_reassoc(wpa_s, ssid);
  303. return 0;
  304. }
  305. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  306. {
  307. struct wps_context *wps;
  308. wps = os_zalloc(sizeof(*wps));
  309. if (wps == NULL)
  310. return -1;
  311. wps->cred_cb = wpa_supplicant_wps_cred;
  312. wps->cb_ctx = wpa_s;
  313. wps->dev.device_name = wpa_s->conf->device_name;
  314. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  315. wps->dev.model_name = wpa_s->conf->model_name;
  316. wps->dev.model_number = wpa_s->conf->model_number;
  317. wps->dev.serial_number = wpa_s->conf->serial_number;
  318. if (wpa_s->conf->device_type) {
  319. char *pos;
  320. u8 oui[4];
  321. /* <categ>-<OUI>-<subcateg> */
  322. wps->dev.categ = atoi(wpa_s->conf->device_type);
  323. pos = os_strchr(wpa_s->conf->device_type, '-');
  324. if (pos == NULL) {
  325. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  326. os_free(wps);
  327. return -1;
  328. }
  329. pos++;
  330. if (hexstr2bin(pos, oui, 4)) {
  331. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  332. os_free(wps);
  333. return -1;
  334. }
  335. wps->dev.oui = WPA_GET_BE32(oui);
  336. pos = os_strchr(pos, '-');
  337. if (pos == NULL) {
  338. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  339. os_free(wps);
  340. return -1;
  341. }
  342. pos++;
  343. wps->dev.sub_categ = atoi(pos);
  344. }
  345. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  346. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  347. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  348. os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
  349. wpa_s->wps = wps;
  350. return 0;
  351. }
  352. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  353. {
  354. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  355. if (wpa_s->wps == NULL)
  356. return;
  357. os_free(wpa_s->wps->network_key);
  358. os_free(wpa_s->wps);
  359. wpa_s->wps = NULL;
  360. }
  361. int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  362. {
  363. struct wpabuf *wps_ie;
  364. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  365. return -1;
  366. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  367. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  368. if (!wps_ie) {
  369. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  370. return 0;
  371. }
  372. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  373. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  374. "without active PBC Registrar");
  375. wpabuf_free(wps_ie);
  376. return 0;
  377. }
  378. /* TODO: overlap detection */
  379. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  380. "(Active PBC)");
  381. wpabuf_free(wps_ie);
  382. return 1;
  383. }
  384. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  385. if (!wps_ie) {
  386. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  387. return 0;
  388. }
  389. if (!wps_is_selected_pin_registrar(wps_ie)) {
  390. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  391. "without active PIN Registrar");
  392. wpabuf_free(wps_ie);
  393. return 0;
  394. }
  395. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  396. "(Active PIN)");
  397. wpabuf_free(wps_ie);
  398. return 1;
  399. }
  400. if (wps_ie) {
  401. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  402. wpabuf_free(wps_ie);
  403. return 1;
  404. }
  405. return -1;
  406. }
  407. int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
  408. struct wpa_scan_res *bss)
  409. {
  410. struct wpabuf *wps_ie = NULL;
  411. int ret = 0;
  412. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  413. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  414. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  415. /* allow wildcard SSID for WPS PBC */
  416. ret = 1;
  417. }
  418. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  419. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  420. if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
  421. /* allow wildcard SSID for WPS PIN */
  422. ret = 1;
  423. }
  424. }
  425. wpabuf_free(wps_ie);
  426. return ret;
  427. }
  428. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  429. struct wpa_scan_res *selected,
  430. struct wpa_ssid *ssid)
  431. {
  432. const u8 *sel_uuid, *uuid;
  433. size_t i;
  434. struct wpabuf *wps_ie;
  435. int ret = 0;
  436. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  437. return 0;
  438. /* Make sure that only one AP is in active PBC mode */
  439. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  440. if (wps_ie)
  441. sel_uuid = wps_get_uuid_e(wps_ie);
  442. else
  443. sel_uuid = NULL;
  444. if (!sel_uuid) {
  445. wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for PBC "
  446. "overlap detection");
  447. wpabuf_free(wps_ie);
  448. return 1;
  449. }
  450. for (i = 0; i < wpa_s->scan_res->num; i++) {
  451. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  452. struct wpabuf *ie;
  453. if (bss == selected)
  454. continue;
  455. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  456. if (!ie)
  457. continue;
  458. if (!wps_is_selected_pbc_registrar(ie)) {
  459. wpabuf_free(ie);
  460. continue;
  461. }
  462. uuid = wps_get_uuid_e(ie);
  463. if (uuid == NULL) {
  464. wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for "
  465. "PBC overlap detection (other BSS)");
  466. ret = 1;
  467. wpabuf_free(ie);
  468. break;
  469. }
  470. if (os_memcmp(sel_uuid, uuid, 16) != 0) {
  471. ret = 1; /* PBC overlap */
  472. wpabuf_free(ie);
  473. break;
  474. }
  475. /* TODO: verify that this is reasonable dual-band situation */
  476. wpabuf_free(ie);
  477. }
  478. wpabuf_free(wps_ie);
  479. return ret;
  480. }
  481. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  482. {
  483. size_t i;
  484. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  485. return;
  486. for (i = 0; i < wpa_s->scan_res->num; i++) {
  487. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  488. struct wpabuf *ie;
  489. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  490. if (!ie)
  491. continue;
  492. if (wps_is_selected_pbc_registrar(ie))
  493. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  494. else if (wps_is_selected_pin_registrar(ie))
  495. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  496. else
  497. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  498. wpabuf_free(ie);
  499. break;
  500. }
  501. }