wps_supplicant.c 18 KB

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