wps_supplicant.c 26 KB

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