wps_supplicant.c 25 KB

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