wps_supplicant.c 24 KB

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