wps_supplicant.c 17 KB

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