wps_supplicant.c 20 KB

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