wps_supplicant.c 24 KB

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