wps_supplicant.c 17 KB

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