wps_supplicant.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 "uuid.h"
  23. #include "wpa_ctrl.h"
  24. #include "ctrl_iface_dbus.h"
  25. #include "eap_common/eap_wsc_common.h"
  26. #include "blacklist.h"
  27. #include "wps_supplicant.h"
  28. #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
  29. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
  30. static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
  31. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  32. {
  33. if (!wpa_s->wps_success &&
  34. wpa_s->current_ssid &&
  35. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  36. const u8 *bssid = wpa_s->bssid;
  37. if (is_zero_ether_addr(bssid))
  38. bssid = wpa_s->pending_bssid;
  39. wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
  40. " did not succeed - continue trying to find "
  41. "suitable AP", MAC2STR(bssid));
  42. wpa_blacklist_add(wpa_s, bssid);
  43. wpa_supplicant_deauthenticate(wpa_s,
  44. WLAN_REASON_DEAUTH_LEAVING);
  45. wpa_s->reassociate = 1;
  46. wpa_supplicant_req_scan(wpa_s,
  47. wpa_s->blacklist_cleared ? 5 : 0, 0);
  48. wpa_s->blacklist_cleared = 0;
  49. return 1;
  50. }
  51. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  52. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  53. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  54. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  55. "try to associate with the received credential");
  56. wpa_supplicant_deauthenticate(wpa_s,
  57. WLAN_REASON_DEAUTH_LEAVING);
  58. wpa_s->reassociate = 1;
  59. wpa_supplicant_req_scan(wpa_s, 0, 0);
  60. return 1;
  61. }
  62. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
  63. wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
  64. "for external credential processing");
  65. wpas_clear_wps(wpa_s);
  66. wpa_supplicant_deauthenticate(wpa_s,
  67. WLAN_REASON_DEAUTH_LEAVING);
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. static int wpa_supplicant_wps_cred(void *ctx,
  73. const struct wps_credential *cred)
  74. {
  75. struct wpa_supplicant *wpa_s = ctx;
  76. struct wpa_ssid *ssid = wpa_s->current_ssid;
  77. if ((wpa_s->conf->wps_cred_processing == 1 ||
  78. wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
  79. size_t blen = cred->cred_attr_len * 2 + 1;
  80. char *buf = os_malloc(blen);
  81. if (buf) {
  82. wpa_snprintf_hex(buf, blen,
  83. cred->cred_attr, cred->cred_attr_len);
  84. wpa_msg(wpa_s, MSG_INFO, "%s%s",
  85. WPS_EVENT_CRED_RECEIVED, buf);
  86. os_free(buf);
  87. }
  88. wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
  89. } else
  90. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
  91. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  92. cred->cred_attr, cred->cred_attr_len);
  93. if (wpa_s->conf->wps_cred_processing == 1)
  94. return 0;
  95. if (cred->auth_type != WPS_AUTH_OPEN &&
  96. cred->auth_type != WPS_AUTH_SHARED &&
  97. cred->auth_type != WPS_AUTH_WPAPSK &&
  98. cred->auth_type != WPS_AUTH_WPA2PSK) {
  99. wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
  100. "unsupported authentication type %d",
  101. cred->auth_type);
  102. return 0;
  103. }
  104. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  105. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  106. "on the received credential");
  107. os_free(ssid->eap.identity);
  108. ssid->eap.identity = NULL;
  109. ssid->eap.identity_len = 0;
  110. os_free(ssid->eap.phase1);
  111. ssid->eap.phase1 = NULL;
  112. os_free(ssid->eap.eap_methods);
  113. ssid->eap.eap_methods = NULL;
  114. } else {
  115. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  116. "received credential");
  117. ssid = wpa_config_add_network(wpa_s->conf);
  118. if (ssid == NULL)
  119. return -1;
  120. }
  121. wpa_config_set_network_defaults(ssid);
  122. os_free(ssid->ssid);
  123. ssid->ssid = os_malloc(cred->ssid_len);
  124. if (ssid->ssid) {
  125. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  126. ssid->ssid_len = cred->ssid_len;
  127. }
  128. switch (cred->encr_type) {
  129. case WPS_ENCR_NONE:
  130. break;
  131. case WPS_ENCR_WEP:
  132. if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
  133. cred->key_idx < NUM_WEP_KEYS) {
  134. os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
  135. cred->key_len);
  136. ssid->wep_key_len[cred->key_idx] = cred->key_len;
  137. ssid->wep_tx_keyidx = cred->key_idx;
  138. }
  139. break;
  140. case WPS_ENCR_TKIP:
  141. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  142. break;
  143. case WPS_ENCR_AES:
  144. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  145. break;
  146. }
  147. switch (cred->auth_type) {
  148. case WPS_AUTH_OPEN:
  149. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  150. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  151. ssid->proto = 0;
  152. break;
  153. case WPS_AUTH_SHARED:
  154. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  155. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  156. ssid->proto = 0;
  157. break;
  158. case WPS_AUTH_WPAPSK:
  159. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  160. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  161. ssid->proto = WPA_PROTO_WPA;
  162. break;
  163. case WPS_AUTH_WPA:
  164. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  165. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  166. ssid->proto = WPA_PROTO_WPA;
  167. break;
  168. case WPS_AUTH_WPA2:
  169. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  170. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  171. ssid->proto = WPA_PROTO_RSN;
  172. break;
  173. case WPS_AUTH_WPA2PSK:
  174. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  175. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  176. ssid->proto = WPA_PROTO_RSN;
  177. break;
  178. }
  179. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  180. if (cred->key_len == 2 * PMK_LEN) {
  181. if (hexstr2bin((const char *) cred->key, ssid->psk,
  182. PMK_LEN)) {
  183. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  184. "Key");
  185. return -1;
  186. }
  187. ssid->psk_set = 1;
  188. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  189. os_free(ssid->passphrase);
  190. ssid->passphrase = os_malloc(cred->key_len + 1);
  191. if (ssid->passphrase == NULL)
  192. return -1;
  193. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  194. ssid->passphrase[cred->key_len] = '\0';
  195. wpa_config_update_psk(ssid);
  196. } else {
  197. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  198. "length %lu",
  199. (unsigned long) cred->key_len);
  200. return -1;
  201. }
  202. }
  203. #ifndef CONFIG_NO_CONFIG_WRITE
  204. if (wpa_s->conf->update_config &&
  205. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  206. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  207. return -1;
  208. }
  209. #endif /* CONFIG_NO_CONFIG_WRITE */
  210. return 0;
  211. }
  212. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  213. struct wps_event_m2d *m2d)
  214. {
  215. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  216. "dev_password_id=%d config_error=%d",
  217. m2d->dev_password_id, m2d->config_error);
  218. }
  219. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  220. struct wps_event_fail *fail)
  221. {
  222. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
  223. wpas_clear_wps(wpa_s);
  224. }
  225. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  226. {
  227. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  228. wpa_s->wps_success = 1;
  229. }
  230. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  231. union wps_event_data *data)
  232. {
  233. struct wpa_supplicant *wpa_s = ctx;
  234. switch (event) {
  235. case WPS_EV_M2D:
  236. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  237. break;
  238. case WPS_EV_FAIL:
  239. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  240. break;
  241. case WPS_EV_SUCCESS:
  242. wpa_supplicant_wps_event_success(wpa_s);
  243. break;
  244. case WPS_EV_PWD_AUTH_FAIL:
  245. break;
  246. }
  247. }
  248. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  249. {
  250. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  251. eap_is_wps_pin_enrollee(&ssid->eap))
  252. return WPS_REQ_ENROLLEE;
  253. else
  254. return WPS_REQ_REGISTRAR;
  255. }
  256. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  257. {
  258. int id;
  259. struct wpa_ssid *ssid;
  260. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  261. /* Remove any existing WPS network from configuration */
  262. ssid = wpa_s->conf->ssid;
  263. while (ssid) {
  264. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  265. if (ssid == wpa_s->current_ssid)
  266. wpa_s->current_ssid = NULL;
  267. id = ssid->id;
  268. } else
  269. id = -1;
  270. ssid = ssid->next;
  271. if (id >= 0)
  272. wpa_config_remove_network(wpa_s->conf, id);
  273. }
  274. }
  275. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  276. {
  277. struct wpa_supplicant *wpa_s = eloop_ctx;
  278. wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  279. "out");
  280. wpas_clear_wps(wpa_s);
  281. }
  282. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  283. int registrar, const u8 *bssid)
  284. {
  285. struct wpa_ssid *ssid;
  286. ssid = wpa_config_add_network(wpa_s->conf);
  287. if (ssid == NULL)
  288. return NULL;
  289. wpa_config_set_network_defaults(ssid);
  290. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  291. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  292. wpa_config_set(ssid, "identity", registrar ?
  293. "\"" WSC_ID_REGISTRAR "\"" :
  294. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  295. wpa_config_remove_network(wpa_s->conf, ssid->id);
  296. return NULL;
  297. }
  298. if (bssid) {
  299. size_t i;
  300. struct wpa_scan_res *res;
  301. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  302. ssid->bssid_set = 1;
  303. /* Try to get SSID from scan results */
  304. if (wpa_s->scan_res == NULL &&
  305. wpa_supplicant_get_scan_results(wpa_s) < 0)
  306. return ssid; /* Could not find any scan results */
  307. for (i = 0; i < wpa_s->scan_res->num; i++) {
  308. const u8 *ie;
  309. res = wpa_s->scan_res->res[i];
  310. if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
  311. continue;
  312. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  313. if (ie == NULL)
  314. break;
  315. os_free(ssid->ssid);
  316. ssid->ssid = os_malloc(ie[1]);
  317. if (ssid->ssid == NULL)
  318. break;
  319. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  320. ssid->ssid_len = ie[1];
  321. break;
  322. }
  323. }
  324. return ssid;
  325. }
  326. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  327. struct wpa_ssid *selected)
  328. {
  329. struct wpa_ssid *ssid;
  330. /* Mark all other networks disabled and trigger reassociation */
  331. ssid = wpa_s->conf->ssid;
  332. while (ssid) {
  333. ssid->disabled = ssid != selected;
  334. ssid = ssid->next;
  335. }
  336. wpa_s->disconnected = 0;
  337. wpa_s->reassociate = 1;
  338. wpa_s->scan_runs = 0;
  339. wpa_s->wps_success = 0;
  340. wpa_s->blacklist_cleared = 0;
  341. wpa_supplicant_req_scan(wpa_s, 0, 0);
  342. }
  343. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  344. {
  345. struct wpa_ssid *ssid;
  346. wpas_clear_wps(wpa_s);
  347. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  348. if (ssid == NULL)
  349. return -1;
  350. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  351. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  352. wpa_s, NULL);
  353. wpas_wps_reassoc(wpa_s, ssid);
  354. return 0;
  355. }
  356. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  357. const char *pin)
  358. {
  359. struct wpa_ssid *ssid;
  360. char val[30];
  361. unsigned int rpin = 0;
  362. wpas_clear_wps(wpa_s);
  363. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  364. if (ssid == NULL)
  365. return -1;
  366. if (pin)
  367. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  368. else {
  369. rpin = wps_generate_pin();
  370. os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
  371. }
  372. wpa_config_set(ssid, "phase1", val, 0);
  373. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  374. wpa_s, NULL);
  375. wpas_wps_reassoc(wpa_s, ssid);
  376. return rpin;
  377. }
  378. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  379. const char *pin)
  380. {
  381. struct wpa_ssid *ssid;
  382. char val[30];
  383. if (!pin)
  384. return -1;
  385. wpas_clear_wps(wpa_s);
  386. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  387. if (ssid == NULL)
  388. return -1;
  389. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  390. wpa_config_set(ssid, "phase1", val, 0);
  391. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  392. wpa_s, NULL);
  393. wpas_wps_reassoc(wpa_s, ssid);
  394. return 0;
  395. }
  396. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  397. size_t psk_len)
  398. {
  399. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  400. "STA " MACSTR, MAC2STR(mac_addr));
  401. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  402. /* TODO */
  403. return 0;
  404. }
  405. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  406. const struct wps_device_data *dev)
  407. {
  408. char uuid[40], txt[400];
  409. int len;
  410. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  411. return;
  412. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  413. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  414. " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  415. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  416. dev->manufacturer, dev->model_name,
  417. dev->model_number, dev->serial_number,
  418. dev->categ, dev->oui, dev->sub_categ);
  419. if (len > 0 && len < (int) sizeof(txt))
  420. wpa_printf(MSG_INFO, "%s", txt);
  421. }
  422. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  423. {
  424. struct wps_context *wps;
  425. struct wps_registrar_config rcfg;
  426. wps = os_zalloc(sizeof(*wps));
  427. if (wps == NULL)
  428. return -1;
  429. wps->cred_cb = wpa_supplicant_wps_cred;
  430. wps->event_cb = wpa_supplicant_wps_event;
  431. wps->cb_ctx = wpa_s;
  432. wps->dev.device_name = wpa_s->conf->device_name;
  433. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  434. wps->dev.model_name = wpa_s->conf->model_name;
  435. wps->dev.model_number = wpa_s->conf->model_number;
  436. wps->dev.serial_number = wpa_s->conf->serial_number;
  437. if (wpa_s->conf->device_type) {
  438. char *pos;
  439. u8 oui[4];
  440. /* <categ>-<OUI>-<subcateg> */
  441. wps->dev.categ = atoi(wpa_s->conf->device_type);
  442. pos = os_strchr(wpa_s->conf->device_type, '-');
  443. if (pos == NULL) {
  444. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  445. os_free(wps);
  446. return -1;
  447. }
  448. pos++;
  449. if (hexstr2bin(pos, oui, 4)) {
  450. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  451. os_free(wps);
  452. return -1;
  453. }
  454. wps->dev.oui = WPA_GET_BE32(oui);
  455. pos = os_strchr(pos, '-');
  456. if (pos == NULL) {
  457. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  458. os_free(wps);
  459. return -1;
  460. }
  461. pos++;
  462. wps->dev.sub_categ = atoi(pos);
  463. }
  464. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  465. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  466. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  467. if (is_nil_uuid(wpa_s->conf->uuid)) {
  468. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  469. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  470. wps->uuid, WPS_UUID_LEN);
  471. } else
  472. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  473. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  474. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  475. os_memset(&rcfg, 0, sizeof(rcfg));
  476. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  477. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  478. rcfg.cb_ctx = wpa_s;
  479. wps->registrar = wps_registrar_init(wps, &rcfg);
  480. if (wps->registrar == NULL) {
  481. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  482. os_free(wps);
  483. return -1;
  484. }
  485. wpa_s->wps = wps;
  486. return 0;
  487. }
  488. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  489. {
  490. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  491. if (wpa_s->wps == NULL)
  492. return;
  493. wps_registrar_deinit(wpa_s->wps->registrar);
  494. os_free(wpa_s->wps->network_key);
  495. os_free(wpa_s->wps);
  496. wpa_s->wps = NULL;
  497. }
  498. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  499. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  500. {
  501. struct wpabuf *wps_ie;
  502. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  503. return -1;
  504. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  505. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  506. if (!wps_ie) {
  507. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  508. return 0;
  509. }
  510. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  511. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  512. "without active PBC Registrar");
  513. wpabuf_free(wps_ie);
  514. return 0;
  515. }
  516. /* TODO: overlap detection */
  517. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  518. "(Active PBC)");
  519. wpabuf_free(wps_ie);
  520. return 1;
  521. }
  522. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  523. if (!wps_ie) {
  524. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  525. return 0;
  526. }
  527. /*
  528. * Start with WPS APs that advertise active PIN Registrar and
  529. * allow any WPS AP after third scan since some APs do not set
  530. * Selected Registrar attribute properly when using external
  531. * Registrar.
  532. */
  533. if (!wps_is_selected_pin_registrar(wps_ie)) {
  534. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  535. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  536. "without active PIN Registrar");
  537. wpabuf_free(wps_ie);
  538. return 0;
  539. }
  540. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  541. } else {
  542. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  543. "(Active PIN)");
  544. }
  545. wpabuf_free(wps_ie);
  546. return 1;
  547. }
  548. if (wps_ie) {
  549. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  550. wpabuf_free(wps_ie);
  551. return 1;
  552. }
  553. return -1;
  554. }
  555. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  556. struct wpa_ssid *ssid,
  557. struct wpa_scan_res *bss)
  558. {
  559. struct wpabuf *wps_ie = NULL;
  560. int ret = 0;
  561. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  562. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  563. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  564. /* allow wildcard SSID for WPS PBC */
  565. ret = 1;
  566. }
  567. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  568. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  569. if (wps_ie &&
  570. (wps_is_selected_pin_registrar(wps_ie) ||
  571. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  572. /* allow wildcard SSID for WPS PIN */
  573. ret = 1;
  574. }
  575. }
  576. if (!ret && ssid->bssid_set &&
  577. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  578. /* allow wildcard SSID due to hardcoded BSSID match */
  579. ret = 1;
  580. }
  581. wpabuf_free(wps_ie);
  582. return ret;
  583. }
  584. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  585. struct wpa_scan_res *selected,
  586. struct wpa_ssid *ssid)
  587. {
  588. const u8 *sel_uuid, *uuid;
  589. size_t i;
  590. struct wpabuf *wps_ie;
  591. int ret = 0;
  592. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  593. return 0;
  594. /* Make sure that only one AP is in active PBC mode */
  595. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  596. if (wps_ie)
  597. sel_uuid = wps_get_uuid_e(wps_ie);
  598. else
  599. sel_uuid = NULL;
  600. for (i = 0; i < wpa_s->scan_res->num; i++) {
  601. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  602. struct wpabuf *ie;
  603. if (bss == selected)
  604. continue;
  605. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  606. if (!ie)
  607. continue;
  608. if (!wps_is_selected_pbc_registrar(ie)) {
  609. wpabuf_free(ie);
  610. continue;
  611. }
  612. uuid = wps_get_uuid_e(ie);
  613. if (sel_uuid == NULL || uuid == NULL ||
  614. os_memcmp(sel_uuid, uuid, 16) != 0) {
  615. ret = 1; /* PBC overlap */
  616. wpabuf_free(ie);
  617. break;
  618. }
  619. /* TODO: verify that this is reasonable dual-band situation */
  620. wpabuf_free(ie);
  621. }
  622. wpabuf_free(wps_ie);
  623. return ret;
  624. }
  625. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  626. {
  627. size_t i;
  628. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  629. return;
  630. for (i = 0; i < wpa_s->scan_res->num; i++) {
  631. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  632. struct wpabuf *ie;
  633. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  634. if (!ie)
  635. continue;
  636. if (wps_is_selected_pbc_registrar(ie))
  637. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  638. else if (wps_is_selected_pin_registrar(ie))
  639. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  640. else
  641. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  642. wpabuf_free(ie);
  643. break;
  644. }
  645. }
  646. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  647. {
  648. struct wpa_ssid *ssid;
  649. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  650. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  651. return 1;
  652. }
  653. return 0;
  654. }