wps_supplicant.c 21 KB

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