wps_supplicant.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. id = ssid->id;
  180. else
  181. id = -1;
  182. ssid = ssid->next;
  183. if (id >= 0)
  184. wpa_config_remove_network(wpa_s->conf, id);
  185. }
  186. }
  187. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  188. {
  189. struct wpa_supplicant *wpa_s = eloop_ctx;
  190. wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
  191. wpas_clear_wps(wpa_s);
  192. }
  193. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  194. int registrar, const u8 *bssid)
  195. {
  196. struct wpa_ssid *ssid;
  197. ssid = wpa_config_add_network(wpa_s->conf);
  198. if (ssid == NULL)
  199. return NULL;
  200. wpa_config_set_network_defaults(ssid);
  201. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  202. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  203. wpa_config_set(ssid, "identity", registrar ?
  204. "\"" WSC_ID_REGISTRAR "\"" :
  205. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  206. wpa_config_remove_network(wpa_s->conf, ssid->id);
  207. return NULL;
  208. }
  209. if (bssid) {
  210. size_t i;
  211. struct wpa_scan_res *res;
  212. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  213. /* Try to get SSID from scan results */
  214. if (wpa_s->scan_res == NULL &&
  215. wpa_supplicant_get_scan_results(wpa_s) < 0)
  216. return ssid; /* Could not find any scan results */
  217. for (i = 0; i < wpa_s->scan_res->num; i++) {
  218. const u8 *ie;
  219. res = wpa_s->scan_res->res[i];
  220. if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
  221. continue;
  222. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  223. if (ie == NULL)
  224. break;
  225. os_free(ssid->ssid);
  226. ssid->ssid = os_malloc(ie[1]);
  227. if (ssid->ssid == NULL)
  228. break;
  229. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  230. ssid->ssid_len = ie[1];
  231. break;
  232. }
  233. }
  234. return ssid;
  235. }
  236. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  237. struct wpa_ssid *selected)
  238. {
  239. struct wpa_ssid *ssid;
  240. /* Mark all other networks disabled and trigger reassociation */
  241. ssid = wpa_s->conf->ssid;
  242. while (ssid) {
  243. ssid->disabled = ssid != selected;
  244. ssid = ssid->next;
  245. }
  246. wpa_s->disconnected = 0;
  247. wpa_s->reassociate = 1;
  248. wpa_supplicant_req_scan(wpa_s, 0, 0);
  249. }
  250. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  251. {
  252. struct wpa_ssid *ssid;
  253. wpas_clear_wps(wpa_s);
  254. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  255. if (ssid == NULL)
  256. return -1;
  257. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  258. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  259. wpa_s, NULL);
  260. wpas_wps_reassoc(wpa_s, ssid);
  261. return 0;
  262. }
  263. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  264. const char *pin)
  265. {
  266. struct wpa_ssid *ssid;
  267. char val[30];
  268. unsigned int rpin = 0;
  269. wpas_clear_wps(wpa_s);
  270. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  271. if (ssid == NULL)
  272. return -1;
  273. if (pin)
  274. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  275. else {
  276. rpin = wps_generate_pin();
  277. os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
  278. }
  279. wpa_config_set(ssid, "phase1", val, 0);
  280. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  281. wpa_s, NULL);
  282. wpas_wps_reassoc(wpa_s, ssid);
  283. return rpin;
  284. }
  285. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  286. const char *pin)
  287. {
  288. struct wpa_ssid *ssid;
  289. char val[30];
  290. if (!pin)
  291. return -1;
  292. wpas_clear_wps(wpa_s);
  293. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  294. if (ssid == NULL)
  295. return -1;
  296. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  297. wpa_config_set(ssid, "phase1", val, 0);
  298. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  299. wpa_s, NULL);
  300. wpas_wps_reassoc(wpa_s, ssid);
  301. return 0;
  302. }
  303. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  304. {
  305. struct wps_context *wps;
  306. wps = os_zalloc(sizeof(*wps));
  307. if (wps == NULL)
  308. return -1;
  309. wps->cred_cb = wpa_supplicant_wps_cred;
  310. wps->cb_ctx = wpa_s;
  311. wps->dev.device_name = wpa_s->conf->device_name;
  312. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  313. wps->dev.model_name = wpa_s->conf->model_name;
  314. wps->dev.model_number = wpa_s->conf->model_number;
  315. wps->dev.serial_number = wpa_s->conf->serial_number;
  316. if (wpa_s->conf->device_type) {
  317. char *pos;
  318. u8 oui[4];
  319. /* <categ>-<OUI>-<subcateg> */
  320. wps->dev.categ = atoi(wpa_s->conf->device_type);
  321. pos = os_strchr(wpa_s->conf->device_type, '-');
  322. if (pos == NULL) {
  323. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  324. os_free(wps);
  325. return -1;
  326. }
  327. pos++;
  328. if (hexstr2bin(pos, oui, 4)) {
  329. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  330. os_free(wps);
  331. return -1;
  332. }
  333. wps->dev.oui = WPA_GET_BE32(oui);
  334. pos = os_strchr(pos, '-');
  335. if (pos == NULL) {
  336. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  337. os_free(wps);
  338. return -1;
  339. }
  340. pos++;
  341. wps->dev.sub_categ = atoi(pos);
  342. }
  343. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  344. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  345. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  346. os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
  347. wpa_s->wps = wps;
  348. return 0;
  349. }
  350. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  351. {
  352. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  353. if (wpa_s->wps == NULL)
  354. return;
  355. os_free(wpa_s->wps->network_key);
  356. os_free(wpa_s->wps);
  357. wpa_s->wps = NULL;
  358. }
  359. int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  360. {
  361. struct wpabuf *wps_ie;
  362. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  363. return -1;
  364. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  365. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  366. if (!wps_ie) {
  367. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  368. return 0;
  369. }
  370. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  371. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  372. "without active PBC Registrar");
  373. wpabuf_free(wps_ie);
  374. return 0;
  375. }
  376. /* TODO: overlap detection */
  377. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  378. "(Active PBC)");
  379. wpabuf_free(wps_ie);
  380. return 1;
  381. }
  382. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  383. if (!wps_ie) {
  384. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  385. return 0;
  386. }
  387. if (!wps_is_selected_pin_registrar(wps_ie)) {
  388. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  389. "without active PIN Registrar");
  390. wpabuf_free(wps_ie);
  391. return 0;
  392. }
  393. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  394. "(Active PIN)");
  395. wpabuf_free(wps_ie);
  396. return 1;
  397. }
  398. if (wps_ie) {
  399. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  400. wpabuf_free(wps_ie);
  401. return 1;
  402. }
  403. return -1;
  404. }
  405. int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
  406. struct wpa_scan_res *bss)
  407. {
  408. struct wpabuf *wps_ie = NULL;
  409. int ret = 0;
  410. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  411. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  412. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  413. /* allow wildcard SSID for WPS PBC */
  414. ret = 1;
  415. }
  416. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  417. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  418. if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
  419. /* allow wildcard SSID for WPS PIN */
  420. ret = 1;
  421. }
  422. }
  423. wpabuf_free(wps_ie);
  424. return ret;
  425. }
  426. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  427. struct wpa_scan_res *selected,
  428. struct wpa_ssid *ssid)
  429. {
  430. const u8 *sel_uuid, *uuid;
  431. size_t i;
  432. struct wpabuf *wps_ie;
  433. int ret = 0;
  434. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  435. return 0;
  436. /* Make sure that only one AP is in active PBC mode */
  437. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  438. if (wps_ie)
  439. sel_uuid = wps_get_uuid_e(wps_ie);
  440. else
  441. sel_uuid = NULL;
  442. if (!sel_uuid) {
  443. wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for PBC "
  444. "overlap detection");
  445. wpabuf_free(wps_ie);
  446. return 1;
  447. }
  448. for (i = 0; i < wpa_s->scan_res->num; i++) {
  449. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  450. struct wpabuf *ie;
  451. if (bss == selected)
  452. continue;
  453. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  454. if (!ie)
  455. continue;
  456. if (!wps_is_selected_pbc_registrar(ie)) {
  457. wpabuf_free(ie);
  458. continue;
  459. }
  460. uuid = wps_get_uuid_e(ie);
  461. if (uuid == NULL) {
  462. wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for "
  463. "PBC overlap detection (other BSS)");
  464. ret = 1;
  465. wpabuf_free(ie);
  466. break;
  467. }
  468. if (os_memcmp(sel_uuid, uuid, 16) != 0) {
  469. ret = 1; /* PBC overlap */
  470. wpabuf_free(ie);
  471. break;
  472. }
  473. /* TODO: verify that this is reasonable dual-band situation */
  474. wpabuf_free(ie);
  475. }
  476. wpabuf_free(wps_ie);
  477. return ret;
  478. }
  479. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  480. {
  481. size_t i;
  482. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  483. return;
  484. for (i = 0; i < wpa_s->scan_res->num; i++) {
  485. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  486. struct wpabuf *ie;
  487. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  488. if (!ie)
  489. continue;
  490. if (wps_is_selected_pbc_registrar(ie))
  491. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  492. else if (wps_is_selected_pin_registrar(ie))
  493. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  494. else
  495. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  496. wpabuf_free(ie);
  497. break;
  498. }
  499. }