wps_supplicant.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. }
  245. }
  246. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  247. {
  248. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  249. eap_is_wps_pin_enrollee(&ssid->eap))
  250. return WPS_REQ_ENROLLEE;
  251. else
  252. return WPS_REQ_REGISTRAR;
  253. }
  254. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  255. {
  256. int id;
  257. struct wpa_ssid *ssid;
  258. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  259. /* Remove any existing WPS network from configuration */
  260. ssid = wpa_s->conf->ssid;
  261. while (ssid) {
  262. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  263. if (ssid == wpa_s->current_ssid)
  264. wpa_s->current_ssid = NULL;
  265. id = ssid->id;
  266. } else
  267. id = -1;
  268. ssid = ssid->next;
  269. if (id >= 0)
  270. wpa_config_remove_network(wpa_s->conf, id);
  271. }
  272. }
  273. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  274. {
  275. struct wpa_supplicant *wpa_s = eloop_ctx;
  276. wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  277. "out");
  278. wpas_clear_wps(wpa_s);
  279. }
  280. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  281. int registrar, const u8 *bssid)
  282. {
  283. struct wpa_ssid *ssid;
  284. ssid = wpa_config_add_network(wpa_s->conf);
  285. if (ssid == NULL)
  286. return NULL;
  287. wpa_config_set_network_defaults(ssid);
  288. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  289. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  290. wpa_config_set(ssid, "identity", registrar ?
  291. "\"" WSC_ID_REGISTRAR "\"" :
  292. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  293. wpa_config_remove_network(wpa_s->conf, ssid->id);
  294. return NULL;
  295. }
  296. if (bssid) {
  297. size_t i;
  298. struct wpa_scan_res *res;
  299. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  300. ssid->bssid_set = 1;
  301. /* Try to get SSID from scan results */
  302. if (wpa_s->scan_res == NULL &&
  303. wpa_supplicant_get_scan_results(wpa_s) < 0)
  304. return ssid; /* Could not find any scan results */
  305. for (i = 0; i < wpa_s->scan_res->num; i++) {
  306. const u8 *ie;
  307. res = wpa_s->scan_res->res[i];
  308. if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
  309. continue;
  310. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  311. if (ie == NULL)
  312. break;
  313. os_free(ssid->ssid);
  314. ssid->ssid = os_malloc(ie[1]);
  315. if (ssid->ssid == NULL)
  316. break;
  317. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  318. ssid->ssid_len = ie[1];
  319. break;
  320. }
  321. }
  322. return ssid;
  323. }
  324. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  325. struct wpa_ssid *selected)
  326. {
  327. struct wpa_ssid *ssid;
  328. /* Mark all other networks disabled and trigger reassociation */
  329. ssid = wpa_s->conf->ssid;
  330. while (ssid) {
  331. ssid->disabled = ssid != selected;
  332. ssid = ssid->next;
  333. }
  334. wpa_s->disconnected = 0;
  335. wpa_s->reassociate = 1;
  336. wpa_s->scan_runs = 0;
  337. wpa_s->wps_success = 0;
  338. wpa_s->blacklist_cleared = 0;
  339. wpa_supplicant_req_scan(wpa_s, 0, 0);
  340. }
  341. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  342. {
  343. struct wpa_ssid *ssid;
  344. wpas_clear_wps(wpa_s);
  345. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  346. if (ssid == NULL)
  347. return -1;
  348. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  349. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  350. wpa_s, NULL);
  351. wpas_wps_reassoc(wpa_s, ssid);
  352. return 0;
  353. }
  354. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  355. const char *pin)
  356. {
  357. struct wpa_ssid *ssid;
  358. char val[30];
  359. unsigned int rpin = 0;
  360. wpas_clear_wps(wpa_s);
  361. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  362. if (ssid == NULL)
  363. return -1;
  364. if (pin)
  365. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  366. else {
  367. rpin = wps_generate_pin();
  368. os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
  369. }
  370. wpa_config_set(ssid, "phase1", val, 0);
  371. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  372. wpa_s, NULL);
  373. wpas_wps_reassoc(wpa_s, ssid);
  374. return rpin;
  375. }
  376. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  377. const char *pin)
  378. {
  379. struct wpa_ssid *ssid;
  380. char val[30];
  381. if (!pin)
  382. return -1;
  383. wpas_clear_wps(wpa_s);
  384. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  385. if (ssid == NULL)
  386. return -1;
  387. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  388. wpa_config_set(ssid, "phase1", val, 0);
  389. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  390. wpa_s, NULL);
  391. wpas_wps_reassoc(wpa_s, ssid);
  392. return 0;
  393. }
  394. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  395. size_t psk_len)
  396. {
  397. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  398. "STA " MACSTR, MAC2STR(mac_addr));
  399. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  400. /* TODO */
  401. return 0;
  402. }
  403. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  404. const struct wps_device_data *dev)
  405. {
  406. char uuid[40], txt[400];
  407. int len;
  408. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  409. return;
  410. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  411. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  412. " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  413. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  414. dev->manufacturer, dev->model_name,
  415. dev->model_number, dev->serial_number,
  416. dev->categ, dev->oui, dev->sub_categ);
  417. if (len > 0 && len < (int) sizeof(txt))
  418. wpa_printf(MSG_INFO, "%s", txt);
  419. }
  420. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  421. {
  422. struct wps_context *wps;
  423. struct wps_registrar_config rcfg;
  424. wps = os_zalloc(sizeof(*wps));
  425. if (wps == NULL)
  426. return -1;
  427. wps->cred_cb = wpa_supplicant_wps_cred;
  428. wps->event_cb = wpa_supplicant_wps_event;
  429. wps->cb_ctx = wpa_s;
  430. wps->dev.device_name = wpa_s->conf->device_name;
  431. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  432. wps->dev.model_name = wpa_s->conf->model_name;
  433. wps->dev.model_number = wpa_s->conf->model_number;
  434. wps->dev.serial_number = wpa_s->conf->serial_number;
  435. if (wpa_s->conf->device_type) {
  436. char *pos;
  437. u8 oui[4];
  438. /* <categ>-<OUI>-<subcateg> */
  439. wps->dev.categ = atoi(wpa_s->conf->device_type);
  440. pos = os_strchr(wpa_s->conf->device_type, '-');
  441. if (pos == NULL) {
  442. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  443. os_free(wps);
  444. return -1;
  445. }
  446. pos++;
  447. if (hexstr2bin(pos, oui, 4)) {
  448. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  449. os_free(wps);
  450. return -1;
  451. }
  452. wps->dev.oui = WPA_GET_BE32(oui);
  453. pos = os_strchr(pos, '-');
  454. if (pos == NULL) {
  455. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  456. os_free(wps);
  457. return -1;
  458. }
  459. pos++;
  460. wps->dev.sub_categ = atoi(pos);
  461. }
  462. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  463. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  464. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  465. if (is_nil_uuid(wpa_s->conf->uuid)) {
  466. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  467. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  468. wps->uuid, WPS_UUID_LEN);
  469. } else
  470. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  471. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  472. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  473. os_memset(&rcfg, 0, sizeof(rcfg));
  474. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  475. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  476. rcfg.cb_ctx = wpa_s;
  477. wps->registrar = wps_registrar_init(wps, &rcfg);
  478. if (wps->registrar == NULL) {
  479. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  480. os_free(wps);
  481. return -1;
  482. }
  483. wpa_s->wps = wps;
  484. return 0;
  485. }
  486. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  487. {
  488. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  489. if (wpa_s->wps == NULL)
  490. return;
  491. wps_registrar_deinit(wpa_s->wps->registrar);
  492. os_free(wpa_s->wps->network_key);
  493. os_free(wpa_s->wps);
  494. wpa_s->wps = NULL;
  495. }
  496. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  497. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  498. {
  499. struct wpabuf *wps_ie;
  500. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  501. return -1;
  502. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  503. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  504. if (!wps_ie) {
  505. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  506. return 0;
  507. }
  508. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  509. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  510. "without active PBC Registrar");
  511. wpabuf_free(wps_ie);
  512. return 0;
  513. }
  514. /* TODO: overlap detection */
  515. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  516. "(Active PBC)");
  517. wpabuf_free(wps_ie);
  518. return 1;
  519. }
  520. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  521. if (!wps_ie) {
  522. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  523. return 0;
  524. }
  525. /*
  526. * Start with WPS APs that advertise active PIN Registrar and
  527. * allow any WPS AP after third scan since some APs do not set
  528. * Selected Registrar attribute properly when using external
  529. * Registrar.
  530. */
  531. if (!wps_is_selected_pin_registrar(wps_ie)) {
  532. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  533. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  534. "without active PIN Registrar");
  535. wpabuf_free(wps_ie);
  536. return 0;
  537. }
  538. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  539. } else {
  540. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  541. "(Active PIN)");
  542. }
  543. wpabuf_free(wps_ie);
  544. return 1;
  545. }
  546. if (wps_ie) {
  547. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  548. wpabuf_free(wps_ie);
  549. return 1;
  550. }
  551. return -1;
  552. }
  553. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  554. struct wpa_ssid *ssid,
  555. struct wpa_scan_res *bss)
  556. {
  557. struct wpabuf *wps_ie = NULL;
  558. int ret = 0;
  559. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  560. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  561. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  562. /* allow wildcard SSID for WPS PBC */
  563. ret = 1;
  564. }
  565. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  566. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  567. if (wps_ie &&
  568. (wps_is_selected_pin_registrar(wps_ie) ||
  569. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  570. /* allow wildcard SSID for WPS PIN */
  571. ret = 1;
  572. }
  573. }
  574. if (!ret && ssid->bssid_set &&
  575. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  576. /* allow wildcard SSID due to hardcoded BSSID match */
  577. ret = 1;
  578. }
  579. wpabuf_free(wps_ie);
  580. return ret;
  581. }
  582. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  583. struct wpa_scan_res *selected,
  584. struct wpa_ssid *ssid)
  585. {
  586. const u8 *sel_uuid, *uuid;
  587. size_t i;
  588. struct wpabuf *wps_ie;
  589. int ret = 0;
  590. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  591. return 0;
  592. /* Make sure that only one AP is in active PBC mode */
  593. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  594. if (wps_ie)
  595. sel_uuid = wps_get_uuid_e(wps_ie);
  596. else
  597. sel_uuid = NULL;
  598. for (i = 0; i < wpa_s->scan_res->num; i++) {
  599. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  600. struct wpabuf *ie;
  601. if (bss == selected)
  602. continue;
  603. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  604. if (!ie)
  605. continue;
  606. if (!wps_is_selected_pbc_registrar(ie)) {
  607. wpabuf_free(ie);
  608. continue;
  609. }
  610. uuid = wps_get_uuid_e(ie);
  611. if (sel_uuid == NULL || uuid == NULL ||
  612. os_memcmp(sel_uuid, uuid, 16) != 0) {
  613. ret = 1; /* PBC overlap */
  614. wpabuf_free(ie);
  615. break;
  616. }
  617. /* TODO: verify that this is reasonable dual-band situation */
  618. wpabuf_free(ie);
  619. }
  620. wpabuf_free(wps_ie);
  621. return ret;
  622. }
  623. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  624. {
  625. size_t i;
  626. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  627. return;
  628. for (i = 0; i < wpa_s->scan_res->num; i++) {
  629. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  630. struct wpabuf *ie;
  631. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  632. if (!ie)
  633. continue;
  634. if (wps_is_selected_pbc_registrar(ie))
  635. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  636. else if (wps_is_selected_pin_registrar(ie))
  637. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  638. else
  639. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  640. wpabuf_free(ie);
  641. break;
  642. }
  643. }
  644. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  645. {
  646. struct wpa_ssid *ssid;
  647. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  648. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  649. return 1;
  650. }
  651. return 0;
  652. }