wps_supplicant.c 13 KB

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