wps_supplicant.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. /* TODO: make the device data configurable */
  311. wps->dev.device_name = "dev name";
  312. wps->dev.manufacturer = "manuf";
  313. wps->dev.model_name = "model name";
  314. wps->dev.model_number = "model number";
  315. wps->dev.serial_number = "12345";
  316. wps->dev.categ = WPS_DEV_COMPUTER;
  317. wps->dev.oui = WPS_DEV_OUI_WFA;
  318. wps->dev.sub_categ = WPS_DEV_COMPUTER_PC;
  319. wps->dev.os_version = 0;
  320. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
  321. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  322. os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
  323. wpa_s->wps = wps;
  324. return 0;
  325. }
  326. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  327. {
  328. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  329. if (wpa_s->wps == NULL)
  330. return;
  331. os_free(wpa_s->wps->network_key);
  332. os_free(wpa_s->wps);
  333. wpa_s->wps = NULL;
  334. }
  335. int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  336. {
  337. struct wpabuf *wps_ie;
  338. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  339. return -1;
  340. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  341. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  342. if (!wps_ie) {
  343. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  344. return 0;
  345. }
  346. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  347. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  348. "without active PBC Registrar");
  349. wpabuf_free(wps_ie);
  350. return 0;
  351. }
  352. /* TODO: overlap detection */
  353. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  354. "(Active PBC)");
  355. wpabuf_free(wps_ie);
  356. return 1;
  357. }
  358. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  359. if (!wps_ie) {
  360. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  361. return 0;
  362. }
  363. if (!wps_is_selected_pin_registrar(wps_ie)) {
  364. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  365. "without active PIN Registrar");
  366. wpabuf_free(wps_ie);
  367. return 0;
  368. }
  369. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  370. "(Active PIN)");
  371. wpabuf_free(wps_ie);
  372. return 1;
  373. }
  374. if (wps_ie) {
  375. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  376. wpabuf_free(wps_ie);
  377. return 1;
  378. }
  379. return -1;
  380. }
  381. int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
  382. struct wpa_scan_res *bss)
  383. {
  384. struct wpabuf *wps_ie = NULL;
  385. int ret = 0;
  386. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  387. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  388. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  389. /* allow wildcard SSID for WPS PBC */
  390. ret = 1;
  391. }
  392. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  393. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  394. if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
  395. /* allow wildcard SSID for WPS PIN */
  396. ret = 1;
  397. }
  398. }
  399. wpabuf_free(wps_ie);
  400. return ret;
  401. }
  402. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  403. struct wpa_scan_res *selected,
  404. struct wpa_ssid *ssid)
  405. {
  406. const u8 *sel_uuid, *uuid;
  407. size_t i;
  408. struct wpabuf *wps_ie;
  409. int ret = 0;
  410. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  411. return 0;
  412. /* Make sure that only one AP is in active PBC mode */
  413. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  414. if (wps_ie)
  415. sel_uuid = wps_get_uuid_e(wps_ie);
  416. else
  417. sel_uuid = NULL;
  418. if (!sel_uuid) {
  419. wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for PBC "
  420. "overlap detection");
  421. wpabuf_free(wps_ie);
  422. return 1;
  423. }
  424. for (i = 0; i < wpa_s->scan_res->num; i++) {
  425. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  426. struct wpabuf *ie;
  427. if (bss == selected)
  428. continue;
  429. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  430. if (!ie)
  431. continue;
  432. if (!wps_is_selected_pbc_registrar(ie)) {
  433. wpabuf_free(ie);
  434. continue;
  435. }
  436. uuid = wps_get_uuid_e(ie);
  437. if (uuid == NULL) {
  438. wpa_printf(MSG_DEBUG, "WPS: UUID-E not available for "
  439. "PBC overlap detection (other BSS)");
  440. ret = 1;
  441. wpabuf_free(ie);
  442. break;
  443. }
  444. if (os_memcmp(sel_uuid, uuid, 16) != 0) {
  445. ret = 1; /* PBC overlap */
  446. wpabuf_free(ie);
  447. break;
  448. }
  449. /* TODO: verify that this is reasonable dual-band situation */
  450. }
  451. wpabuf_free(wps_ie);
  452. return ret;
  453. }