wps_supplicant.c 25 KB

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