wps_supplicant.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 "eap_common/eap_wsc_common.h"
  25. #include "wps_supplicant.h"
  26. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
  27. static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
  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. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  163. struct wps_event_m2d *m2d)
  164. {
  165. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  166. "dev_password_id=%d config_error=%d",
  167. m2d->dev_password_id, m2d->config_error);
  168. }
  169. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  170. struct wps_event_fail *fail)
  171. {
  172. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
  173. wpas_clear_wps(wpa_s);
  174. }
  175. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  176. {
  177. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  178. }
  179. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  180. union wps_event_data *data)
  181. {
  182. struct wpa_supplicant *wpa_s = ctx;
  183. switch (event) {
  184. case WPS_EV_M2D:
  185. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  186. break;
  187. case WPS_EV_FAIL:
  188. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  189. break;
  190. case WPS_EV_SUCCESS:
  191. wpa_supplicant_wps_event_success(wpa_s);
  192. break;
  193. }
  194. }
  195. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  196. {
  197. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  198. eap_is_wps_pin_enrollee(&ssid->eap))
  199. return WPS_REQ_ENROLLEE;
  200. else
  201. return WPS_REQ_REGISTRAR;
  202. }
  203. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  204. {
  205. int id;
  206. struct wpa_ssid *ssid;
  207. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  208. /* Remove any existing WPS network from configuration */
  209. ssid = wpa_s->conf->ssid;
  210. while (ssid) {
  211. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  212. if (ssid == wpa_s->current_ssid)
  213. wpa_s->current_ssid = NULL;
  214. id = ssid->id;
  215. } else
  216. id = -1;
  217. ssid = ssid->next;
  218. if (id >= 0)
  219. wpa_config_remove_network(wpa_s->conf, id);
  220. }
  221. }
  222. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  223. {
  224. struct wpa_supplicant *wpa_s = eloop_ctx;
  225. wpa_printf(MSG_DEBUG, "WPS: Requested operation timed out");
  226. wpas_clear_wps(wpa_s);
  227. }
  228. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  229. int registrar, const u8 *bssid)
  230. {
  231. struct wpa_ssid *ssid;
  232. ssid = wpa_config_add_network(wpa_s->conf);
  233. if (ssid == NULL)
  234. return NULL;
  235. wpa_config_set_network_defaults(ssid);
  236. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  237. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  238. wpa_config_set(ssid, "identity", registrar ?
  239. "\"" WSC_ID_REGISTRAR "\"" :
  240. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  241. wpa_config_remove_network(wpa_s->conf, ssid->id);
  242. return NULL;
  243. }
  244. if (bssid) {
  245. size_t i;
  246. struct wpa_scan_res *res;
  247. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  248. ssid->bssid_set = 1;
  249. /* Try to get SSID from scan results */
  250. if (wpa_s->scan_res == NULL &&
  251. wpa_supplicant_get_scan_results(wpa_s) < 0)
  252. return ssid; /* Could not find any scan results */
  253. for (i = 0; i < wpa_s->scan_res->num; i++) {
  254. const u8 *ie;
  255. res = wpa_s->scan_res->res[i];
  256. if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
  257. continue;
  258. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  259. if (ie == NULL)
  260. break;
  261. os_free(ssid->ssid);
  262. ssid->ssid = os_malloc(ie[1]);
  263. if (ssid->ssid == NULL)
  264. break;
  265. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  266. ssid->ssid_len = ie[1];
  267. break;
  268. }
  269. }
  270. return ssid;
  271. }
  272. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  273. struct wpa_ssid *selected)
  274. {
  275. struct wpa_ssid *ssid;
  276. /* Mark all other networks disabled and trigger reassociation */
  277. ssid = wpa_s->conf->ssid;
  278. while (ssid) {
  279. ssid->disabled = ssid != selected;
  280. ssid = ssid->next;
  281. }
  282. wpa_s->disconnected = 0;
  283. wpa_s->reassociate = 1;
  284. wpa_supplicant_req_scan(wpa_s, 0, 0);
  285. }
  286. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  287. {
  288. struct wpa_ssid *ssid;
  289. wpas_clear_wps(wpa_s);
  290. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  291. if (ssid == NULL)
  292. return -1;
  293. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  294. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  295. wpa_s, NULL);
  296. wpas_wps_reassoc(wpa_s, ssid);
  297. return 0;
  298. }
  299. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  300. const char *pin)
  301. {
  302. struct wpa_ssid *ssid;
  303. char val[30];
  304. unsigned int rpin = 0;
  305. wpas_clear_wps(wpa_s);
  306. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  307. if (ssid == NULL)
  308. return -1;
  309. if (pin)
  310. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  311. else {
  312. rpin = wps_generate_pin();
  313. os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
  314. }
  315. wpa_config_set(ssid, "phase1", val, 0);
  316. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  317. wpa_s, NULL);
  318. wpas_wps_reassoc(wpa_s, ssid);
  319. return rpin;
  320. }
  321. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  322. const char *pin)
  323. {
  324. struct wpa_ssid *ssid;
  325. char val[30];
  326. if (!pin)
  327. return -1;
  328. wpas_clear_wps(wpa_s);
  329. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  330. if (ssid == NULL)
  331. return -1;
  332. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  333. wpa_config_set(ssid, "phase1", val, 0);
  334. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  335. wpa_s, NULL);
  336. wpas_wps_reassoc(wpa_s, ssid);
  337. return 0;
  338. }
  339. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  340. size_t psk_len)
  341. {
  342. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  343. "STA " MACSTR, MAC2STR(mac_addr));
  344. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  345. /* TODO */
  346. return 0;
  347. }
  348. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  349. const struct wps_device_data *dev)
  350. {
  351. char uuid[40], txt[400];
  352. int len;
  353. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  354. return;
  355. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  356. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  357. " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  358. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  359. dev->manufacturer, dev->model_name,
  360. dev->model_number, dev->serial_number,
  361. dev->categ, dev->oui, dev->sub_categ);
  362. if (len > 0 && len < (int) sizeof(txt))
  363. wpa_printf(MSG_INFO, "%s", txt);
  364. }
  365. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  366. {
  367. struct wps_context *wps;
  368. struct wps_registrar_config rcfg;
  369. wps = os_zalloc(sizeof(*wps));
  370. if (wps == NULL)
  371. return -1;
  372. wps->cred_cb = wpa_supplicant_wps_cred;
  373. wps->event_cb = wpa_supplicant_wps_event;
  374. wps->cb_ctx = wpa_s;
  375. wps->dev.device_name = wpa_s->conf->device_name;
  376. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  377. wps->dev.model_name = wpa_s->conf->model_name;
  378. wps->dev.model_number = wpa_s->conf->model_number;
  379. wps->dev.serial_number = wpa_s->conf->serial_number;
  380. if (wpa_s->conf->device_type) {
  381. char *pos;
  382. u8 oui[4];
  383. /* <categ>-<OUI>-<subcateg> */
  384. wps->dev.categ = atoi(wpa_s->conf->device_type);
  385. pos = os_strchr(wpa_s->conf->device_type, '-');
  386. if (pos == NULL) {
  387. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  388. os_free(wps);
  389. return -1;
  390. }
  391. pos++;
  392. if (hexstr2bin(pos, oui, 4)) {
  393. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  394. os_free(wps);
  395. return -1;
  396. }
  397. wps->dev.oui = WPA_GET_BE32(oui);
  398. pos = os_strchr(pos, '-');
  399. if (pos == NULL) {
  400. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  401. os_free(wps);
  402. return -1;
  403. }
  404. pos++;
  405. wps->dev.sub_categ = atoi(pos);
  406. }
  407. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  408. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  409. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  410. if (is_nil_uuid(wpa_s->conf->uuid)) {
  411. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  412. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  413. wps->uuid, WPS_UUID_LEN);
  414. } else
  415. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  416. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  417. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  418. os_memset(&rcfg, 0, sizeof(rcfg));
  419. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  420. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  421. rcfg.cb_ctx = wpa_s;
  422. wps->registrar = wps_registrar_init(wps, &rcfg);
  423. if (wps->registrar == NULL) {
  424. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  425. os_free(wps);
  426. return -1;
  427. }
  428. wpa_s->wps = wps;
  429. return 0;
  430. }
  431. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  432. {
  433. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  434. if (wpa_s->wps == NULL)
  435. return;
  436. wps_registrar_deinit(wpa_s->wps->registrar);
  437. os_free(wpa_s->wps->network_key);
  438. os_free(wpa_s->wps);
  439. wpa_s->wps = NULL;
  440. }
  441. int wpas_wps_ssid_bss_match(struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  442. {
  443. struct wpabuf *wps_ie;
  444. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  445. return -1;
  446. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  447. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  448. if (!wps_ie) {
  449. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  450. return 0;
  451. }
  452. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  453. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  454. "without active PBC Registrar");
  455. wpabuf_free(wps_ie);
  456. return 0;
  457. }
  458. /* TODO: overlap detection */
  459. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  460. "(Active PBC)");
  461. wpabuf_free(wps_ie);
  462. return 1;
  463. }
  464. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  465. if (!wps_ie) {
  466. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  467. return 0;
  468. }
  469. if (!wps_is_selected_pin_registrar(wps_ie)) {
  470. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  471. "without active PIN Registrar");
  472. wpabuf_free(wps_ie);
  473. return 0;
  474. }
  475. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  476. "(Active PIN)");
  477. wpabuf_free(wps_ie);
  478. return 1;
  479. }
  480. if (wps_ie) {
  481. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  482. wpabuf_free(wps_ie);
  483. return 1;
  484. }
  485. return -1;
  486. }
  487. int wpas_wps_ssid_wildcard_ok(struct wpa_ssid *ssid,
  488. struct wpa_scan_res *bss)
  489. {
  490. struct wpabuf *wps_ie = NULL;
  491. int ret = 0;
  492. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  493. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  494. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  495. /* allow wildcard SSID for WPS PBC */
  496. ret = 1;
  497. }
  498. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  499. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  500. if (wps_ie && wps_is_selected_pin_registrar(wps_ie)) {
  501. /* allow wildcard SSID for WPS PIN */
  502. ret = 1;
  503. }
  504. }
  505. if (!ret && ssid->bssid_set &&
  506. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  507. /* allow wildcard SSID due to hardcoded BSSID match */
  508. ret = 1;
  509. }
  510. wpabuf_free(wps_ie);
  511. return ret;
  512. }
  513. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  514. struct wpa_scan_res *selected,
  515. struct wpa_ssid *ssid)
  516. {
  517. const u8 *sel_uuid, *uuid;
  518. size_t i;
  519. struct wpabuf *wps_ie;
  520. int ret = 0;
  521. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  522. return 0;
  523. /* Make sure that only one AP is in active PBC mode */
  524. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  525. if (wps_ie)
  526. sel_uuid = wps_get_uuid_e(wps_ie);
  527. else
  528. sel_uuid = NULL;
  529. for (i = 0; i < wpa_s->scan_res->num; i++) {
  530. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  531. struct wpabuf *ie;
  532. if (bss == selected)
  533. continue;
  534. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  535. if (!ie)
  536. continue;
  537. if (!wps_is_selected_pbc_registrar(ie)) {
  538. wpabuf_free(ie);
  539. continue;
  540. }
  541. uuid = wps_get_uuid_e(ie);
  542. if (sel_uuid == NULL || uuid == NULL ||
  543. os_memcmp(sel_uuid, uuid, 16) != 0) {
  544. ret = 1; /* PBC overlap */
  545. wpabuf_free(ie);
  546. break;
  547. }
  548. /* TODO: verify that this is reasonable dual-band situation */
  549. wpabuf_free(ie);
  550. }
  551. wpabuf_free(wps_ie);
  552. return ret;
  553. }
  554. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  555. {
  556. size_t i;
  557. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  558. return;
  559. for (i = 0; i < wpa_s->scan_res->num; i++) {
  560. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  561. struct wpabuf *ie;
  562. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  563. if (!ie)
  564. continue;
  565. if (wps_is_selected_pbc_registrar(ie))
  566. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  567. else if (wps_is_selected_pin_registrar(ie))
  568. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  569. else
  570. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  571. wpabuf_free(ie);
  572. break;
  573. }
  574. }
  575. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  576. {
  577. struct wpa_ssid *ssid;
  578. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  579. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  580. return 1;
  581. }
  582. return 0;
  583. }