wps_supplicant.c 25 KB

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