wps_supplicant.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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)
  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. wps->oob_conf.oob_method = wps_get_oob_method(method);
  500. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  501. /*
  502. * Use pre-configured DH keys in order to be able to write the
  503. * key hash into the OOB file.
  504. */
  505. wpabuf_free(wps->dh_pubkey);
  506. wpabuf_free(wps->dh_privkey);
  507. wps->dh_privkey = NULL;
  508. wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
  509. &wps->dh_privkey);
  510. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  511. if (wps->dh_pubkey == NULL) {
  512. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  513. "Diffie-Hellman handshake");
  514. return -1;
  515. }
  516. }
  517. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  518. wpas_clear_wps(wpa_s);
  519. if (wps_process_oob(wps, oob_dev, 0) < 0)
  520. return -1;
  521. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  522. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  523. wpas_wps_start_pin(wpa_s, NULL,
  524. wpabuf_head(wps->oob_conf.dev_password)) < 0)
  525. return -1;
  526. return 0;
  527. }
  528. #endif /* CONFIG_WPS_OOB */
  529. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  530. const char *pin)
  531. {
  532. struct wpa_ssid *ssid;
  533. char val[30];
  534. if (!pin)
  535. return -1;
  536. wpas_clear_wps(wpa_s);
  537. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  538. if (ssid == NULL)
  539. return -1;
  540. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  541. wpa_config_set(ssid, "phase1", val, 0);
  542. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  543. wpa_s, NULL);
  544. wpas_wps_reassoc(wpa_s, ssid);
  545. return 0;
  546. }
  547. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  548. size_t psk_len)
  549. {
  550. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  551. "STA " MACSTR, MAC2STR(mac_addr));
  552. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  553. /* TODO */
  554. return 0;
  555. }
  556. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  557. const struct wps_device_data *dev)
  558. {
  559. char uuid[40], txt[400];
  560. int len;
  561. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  562. return;
  563. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  564. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  565. " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  566. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  567. dev->manufacturer, dev->model_name,
  568. dev->model_number, dev->serial_number,
  569. dev->categ, dev->oui, dev->sub_categ);
  570. if (len > 0 && len < (int) sizeof(txt))
  571. wpa_printf(MSG_INFO, "%s", txt);
  572. }
  573. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  574. {
  575. struct wps_context *wps;
  576. struct wps_registrar_config rcfg;
  577. wps = os_zalloc(sizeof(*wps));
  578. if (wps == NULL)
  579. return -1;
  580. wps->cred_cb = wpa_supplicant_wps_cred;
  581. wps->event_cb = wpa_supplicant_wps_event;
  582. wps->cb_ctx = wpa_s;
  583. wps->dev.device_name = wpa_s->conf->device_name;
  584. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  585. wps->dev.model_name = wpa_s->conf->model_name;
  586. wps->dev.model_number = wpa_s->conf->model_number;
  587. wps->dev.serial_number = wpa_s->conf->serial_number;
  588. if (wpa_s->conf->device_type) {
  589. char *pos;
  590. u8 oui[4];
  591. /* <categ>-<OUI>-<subcateg> */
  592. wps->dev.categ = atoi(wpa_s->conf->device_type);
  593. pos = os_strchr(wpa_s->conf->device_type, '-');
  594. if (pos == NULL) {
  595. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  596. os_free(wps);
  597. return -1;
  598. }
  599. pos++;
  600. if (hexstr2bin(pos, oui, 4)) {
  601. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  602. os_free(wps);
  603. return -1;
  604. }
  605. wps->dev.oui = WPA_GET_BE32(oui);
  606. pos = os_strchr(pos, '-');
  607. if (pos == NULL) {
  608. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  609. os_free(wps);
  610. return -1;
  611. }
  612. pos++;
  613. wps->dev.sub_categ = atoi(pos);
  614. }
  615. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  616. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  617. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  618. if (is_nil_uuid(wpa_s->conf->uuid)) {
  619. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  620. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  621. wps->uuid, WPS_UUID_LEN);
  622. } else
  623. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  624. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  625. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  626. os_memset(&rcfg, 0, sizeof(rcfg));
  627. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  628. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  629. rcfg.cb_ctx = wpa_s;
  630. wps->registrar = wps_registrar_init(wps, &rcfg);
  631. if (wps->registrar == NULL) {
  632. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  633. os_free(wps);
  634. return -1;
  635. }
  636. wpa_s->wps = wps;
  637. return 0;
  638. }
  639. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  640. {
  641. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  642. if (wpa_s->wps == NULL)
  643. return;
  644. wps_registrar_deinit(wpa_s->wps->registrar);
  645. wpabuf_free(wpa_s->wps->dh_pubkey);
  646. wpabuf_free(wpa_s->wps->dh_privkey);
  647. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  648. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  649. os_free(wpa_s->wps->network_key);
  650. os_free(wpa_s->wps);
  651. wpa_s->wps = NULL;
  652. }
  653. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  654. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  655. {
  656. struct wpabuf *wps_ie;
  657. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  658. return -1;
  659. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  660. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  661. if (!wps_ie) {
  662. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  663. return 0;
  664. }
  665. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  666. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  667. "without active PBC Registrar");
  668. wpabuf_free(wps_ie);
  669. return 0;
  670. }
  671. /* TODO: overlap detection */
  672. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  673. "(Active PBC)");
  674. wpabuf_free(wps_ie);
  675. return 1;
  676. }
  677. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  678. if (!wps_ie) {
  679. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  680. return 0;
  681. }
  682. /*
  683. * Start with WPS APs that advertise active PIN Registrar and
  684. * allow any WPS AP after third scan since some APs do not set
  685. * Selected Registrar attribute properly when using external
  686. * Registrar.
  687. */
  688. if (!wps_is_selected_pin_registrar(wps_ie)) {
  689. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  690. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  691. "without active PIN Registrar");
  692. wpabuf_free(wps_ie);
  693. return 0;
  694. }
  695. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  696. } else {
  697. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  698. "(Active PIN)");
  699. }
  700. wpabuf_free(wps_ie);
  701. return 1;
  702. }
  703. if (wps_ie) {
  704. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  705. wpabuf_free(wps_ie);
  706. return 1;
  707. }
  708. return -1;
  709. }
  710. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  711. struct wpa_ssid *ssid,
  712. struct wpa_scan_res *bss)
  713. {
  714. struct wpabuf *wps_ie = NULL;
  715. int ret = 0;
  716. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  717. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  718. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  719. /* allow wildcard SSID for WPS PBC */
  720. ret = 1;
  721. }
  722. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  723. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  724. if (wps_ie &&
  725. (wps_is_selected_pin_registrar(wps_ie) ||
  726. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  727. /* allow wildcard SSID for WPS PIN */
  728. ret = 1;
  729. }
  730. }
  731. if (!ret && ssid->bssid_set &&
  732. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  733. /* allow wildcard SSID due to hardcoded BSSID match */
  734. ret = 1;
  735. }
  736. wpabuf_free(wps_ie);
  737. return ret;
  738. }
  739. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  740. struct wpa_scan_res *selected,
  741. struct wpa_ssid *ssid)
  742. {
  743. const u8 *sel_uuid, *uuid;
  744. size_t i;
  745. struct wpabuf *wps_ie;
  746. int ret = 0;
  747. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  748. return 0;
  749. /* Make sure that only one AP is in active PBC mode */
  750. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  751. if (wps_ie)
  752. sel_uuid = wps_get_uuid_e(wps_ie);
  753. else
  754. sel_uuid = NULL;
  755. for (i = 0; i < wpa_s->scan_res->num; i++) {
  756. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  757. struct wpabuf *ie;
  758. if (bss == selected)
  759. continue;
  760. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  761. if (!ie)
  762. continue;
  763. if (!wps_is_selected_pbc_registrar(ie)) {
  764. wpabuf_free(ie);
  765. continue;
  766. }
  767. uuid = wps_get_uuid_e(ie);
  768. if (sel_uuid == NULL || uuid == NULL ||
  769. os_memcmp(sel_uuid, uuid, 16) != 0) {
  770. ret = 1; /* PBC overlap */
  771. wpabuf_free(ie);
  772. break;
  773. }
  774. /* TODO: verify that this is reasonable dual-band situation */
  775. wpabuf_free(ie);
  776. }
  777. wpabuf_free(wps_ie);
  778. return ret;
  779. }
  780. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  781. {
  782. size_t i;
  783. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  784. return;
  785. for (i = 0; i < wpa_s->scan_res->num; i++) {
  786. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  787. struct wpabuf *ie;
  788. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  789. if (!ie)
  790. continue;
  791. if (wps_is_selected_pbc_registrar(ie))
  792. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  793. else if (wps_is_selected_pin_registrar(ie))
  794. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  795. else
  796. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  797. wpabuf_free(ie);
  798. break;
  799. }
  800. }
  801. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  802. {
  803. struct wpa_ssid *ssid;
  804. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  805. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  806. return 1;
  807. }
  808. return 0;
  809. }