wps_supplicant.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 "notify.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_group5.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. wpas_notify_wps_credential(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. wpas_notify_network_added(wpa_s, ssid);
  226. }
  227. wpa_config_set_network_defaults(ssid);
  228. os_free(ssid->ssid);
  229. ssid->ssid = os_malloc(cred->ssid_len);
  230. if (ssid->ssid) {
  231. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  232. ssid->ssid_len = cred->ssid_len;
  233. }
  234. switch (cred->encr_type) {
  235. case WPS_ENCR_NONE:
  236. break;
  237. case WPS_ENCR_WEP:
  238. if (cred->key_len <= 0)
  239. break;
  240. if (cred->key_len != 5 && cred->key_len != 13 &&
  241. cred->key_len != 10 && cred->key_len != 26) {
  242. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
  243. "%lu", (unsigned long) cred->key_len);
  244. return -1;
  245. }
  246. if (cred->key_idx > NUM_WEP_KEYS) {
  247. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
  248. cred->key_idx);
  249. return -1;
  250. }
  251. if (cred->key_idx)
  252. key_idx = cred->key_idx - 1;
  253. if (cred->key_len == 10 || cred->key_len == 26) {
  254. if (hexstr2bin((char *) cred->key,
  255. ssid->wep_key[key_idx],
  256. cred->key_len / 2) < 0) {
  257. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
  258. "%d", key_idx);
  259. return -1;
  260. }
  261. ssid->wep_key_len[key_idx] = cred->key_len / 2;
  262. } else {
  263. os_memcpy(ssid->wep_key[key_idx], cred->key,
  264. cred->key_len);
  265. ssid->wep_key_len[key_idx] = cred->key_len;
  266. }
  267. ssid->wep_tx_keyidx = key_idx;
  268. break;
  269. case WPS_ENCR_TKIP:
  270. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  271. break;
  272. case WPS_ENCR_AES:
  273. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  274. break;
  275. }
  276. switch (auth_type) {
  277. case WPS_AUTH_OPEN:
  278. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  279. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  280. ssid->proto = 0;
  281. break;
  282. case WPS_AUTH_SHARED:
  283. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  284. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  285. ssid->proto = 0;
  286. break;
  287. case WPS_AUTH_WPAPSK:
  288. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  289. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  290. ssid->proto = WPA_PROTO_WPA;
  291. break;
  292. case WPS_AUTH_WPA:
  293. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  294. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  295. ssid->proto = WPA_PROTO_WPA;
  296. break;
  297. case WPS_AUTH_WPA2:
  298. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  299. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  300. ssid->proto = WPA_PROTO_RSN;
  301. break;
  302. case WPS_AUTH_WPA2PSK:
  303. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  304. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  305. ssid->proto = WPA_PROTO_RSN;
  306. break;
  307. }
  308. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  309. if (cred->key_len == 2 * PMK_LEN) {
  310. if (hexstr2bin((const char *) cred->key, ssid->psk,
  311. PMK_LEN)) {
  312. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  313. "Key");
  314. return -1;
  315. }
  316. ssid->psk_set = 1;
  317. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  318. os_free(ssid->passphrase);
  319. ssid->passphrase = os_malloc(cred->key_len + 1);
  320. if (ssid->passphrase == NULL)
  321. return -1;
  322. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  323. ssid->passphrase[cred->key_len] = '\0';
  324. wpa_config_update_psk(ssid);
  325. } else {
  326. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  327. "length %lu",
  328. (unsigned long) cred->key_len);
  329. return -1;
  330. }
  331. }
  332. wpas_wps_security_workaround(wpa_s, ssid, cred);
  333. #ifndef CONFIG_NO_CONFIG_WRITE
  334. if (wpa_s->conf->update_config &&
  335. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  336. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  337. return -1;
  338. }
  339. #endif /* CONFIG_NO_CONFIG_WRITE */
  340. return 0;
  341. }
  342. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  343. struct wps_event_m2d *m2d)
  344. {
  345. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  346. "dev_password_id=%d config_error=%d",
  347. m2d->dev_password_id, m2d->config_error);
  348. wpas_notify_wps_event_m2d(wpa_s, m2d);
  349. }
  350. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  351. struct wps_event_fail *fail)
  352. {
  353. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
  354. wpas_clear_wps(wpa_s);
  355. wpas_notify_wps_event_fail(wpa_s, fail);
  356. }
  357. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  358. {
  359. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  360. wpa_s->wps_success = 1;
  361. wpas_notify_wps_event_success(wpa_s);
  362. }
  363. static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
  364. struct wps_event_er_ap *ap)
  365. {
  366. char uuid_str[100];
  367. char dev_type[20];
  368. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  369. if (ap->pri_dev_type)
  370. os_snprintf(dev_type, sizeof(dev_type), "%u-%08X-%u",
  371. WPA_GET_BE16(ap->pri_dev_type),
  372. WPA_GET_BE32(ap->pri_dev_type + 2),
  373. WPA_GET_BE16(ap->pri_dev_type + 6));
  374. else
  375. dev_type[0] = '\0';
  376. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
  377. " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
  378. uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
  379. ap->friendly_name ? ap->friendly_name : "",
  380. ap->manufacturer ? ap->manufacturer : "",
  381. ap->model_description ? ap->model_description : "",
  382. ap->model_name ? ap->model_name : "",
  383. ap->manufacturer_url ? ap->manufacturer_url : "",
  384. ap->model_url ? ap->model_url : "");
  385. }
  386. static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
  387. struct wps_event_er_ap *ap)
  388. {
  389. char uuid_str[100];
  390. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  391. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
  392. }
  393. static void wpa_supplicant_wps_event_er_enrollee_add(
  394. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  395. {
  396. char uuid_str[100];
  397. char dev_type[20];
  398. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  399. if (enrollee->pri_dev_type)
  400. os_snprintf(dev_type, sizeof(dev_type), "%u-%08X-%u",
  401. WPA_GET_BE16(enrollee->pri_dev_type),
  402. WPA_GET_BE32(enrollee->pri_dev_type + 2),
  403. WPA_GET_BE16(enrollee->pri_dev_type + 6));
  404. else
  405. dev_type[0] = '\0';
  406. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
  407. " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
  408. "|%s|%s|%s|%s|%s|",
  409. uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
  410. enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
  411. enrollee->dev_name ? enrollee->dev_name : "",
  412. enrollee->manufacturer ? enrollee->manufacturer : "",
  413. enrollee->model_name ? enrollee->model_name : "",
  414. enrollee->model_number ? enrollee->model_number : "",
  415. enrollee->serial_number ? enrollee->serial_number : "");
  416. }
  417. static void wpa_supplicant_wps_event_er_enrollee_remove(
  418. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  419. {
  420. char uuid_str[100];
  421. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  422. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
  423. uuid_str, MAC2STR(enrollee->mac_addr));
  424. }
  425. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  426. union wps_event_data *data)
  427. {
  428. struct wpa_supplicant *wpa_s = ctx;
  429. switch (event) {
  430. case WPS_EV_M2D:
  431. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  432. break;
  433. case WPS_EV_FAIL:
  434. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  435. break;
  436. case WPS_EV_SUCCESS:
  437. wpa_supplicant_wps_event_success(wpa_s);
  438. break;
  439. case WPS_EV_PWD_AUTH_FAIL:
  440. break;
  441. case WPS_EV_PBC_OVERLAP:
  442. break;
  443. case WPS_EV_PBC_TIMEOUT:
  444. break;
  445. case WPS_EV_ER_AP_ADD:
  446. wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
  447. break;
  448. case WPS_EV_ER_AP_REMOVE:
  449. wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
  450. break;
  451. case WPS_EV_ER_ENROLLEE_ADD:
  452. wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
  453. &data->enrollee);
  454. break;
  455. case WPS_EV_ER_ENROLLEE_REMOVE:
  456. wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
  457. &data->enrollee);
  458. break;
  459. }
  460. }
  461. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  462. {
  463. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  464. eap_is_wps_pin_enrollee(&ssid->eap))
  465. return WPS_REQ_ENROLLEE;
  466. else
  467. return WPS_REQ_REGISTRAR;
  468. }
  469. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  470. {
  471. int id;
  472. struct wpa_ssid *ssid, *remove_ssid = NULL;
  473. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  474. /* Remove any existing WPS network from configuration */
  475. ssid = wpa_s->conf->ssid;
  476. while (ssid) {
  477. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  478. if (ssid == wpa_s->current_ssid) {
  479. wpa_s->current_ssid = NULL;
  480. if (ssid != NULL)
  481. wpas_notify_network_changed(wpa_s);
  482. }
  483. id = ssid->id;
  484. remove_ssid = ssid;
  485. } else
  486. id = -1;
  487. ssid = ssid->next;
  488. if (id >= 0) {
  489. wpas_notify_network_removed(wpa_s, remove_ssid);
  490. wpa_config_remove_network(wpa_s->conf, id);
  491. }
  492. }
  493. }
  494. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  495. {
  496. struct wpa_supplicant *wpa_s = eloop_ctx;
  497. wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  498. "out");
  499. wpas_clear_wps(wpa_s);
  500. }
  501. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  502. int registrar, const u8 *bssid)
  503. {
  504. struct wpa_ssid *ssid;
  505. ssid = wpa_config_add_network(wpa_s->conf);
  506. if (ssid == NULL)
  507. return NULL;
  508. wpas_notify_network_added(wpa_s, ssid);
  509. wpa_config_set_network_defaults(ssid);
  510. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  511. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  512. wpa_config_set(ssid, "identity", registrar ?
  513. "\"" WSC_ID_REGISTRAR "\"" :
  514. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  515. wpas_notify_network_removed(wpa_s, ssid);
  516. wpa_config_remove_network(wpa_s->conf, ssid->id);
  517. return NULL;
  518. }
  519. if (bssid) {
  520. size_t i;
  521. struct wpa_scan_res *res;
  522. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  523. ssid->bssid_set = 1;
  524. /* Try to get SSID from scan results */
  525. if (wpa_s->scan_res == NULL &&
  526. wpa_supplicant_get_scan_results(wpa_s) < 0)
  527. return ssid; /* Could not find any scan results */
  528. for (i = 0; i < wpa_s->scan_res->num; i++) {
  529. const u8 *ie;
  530. res = wpa_s->scan_res->res[i];
  531. if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
  532. continue;
  533. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  534. if (ie == NULL)
  535. break;
  536. os_free(ssid->ssid);
  537. ssid->ssid = os_malloc(ie[1]);
  538. if (ssid->ssid == NULL)
  539. break;
  540. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  541. ssid->ssid_len = ie[1];
  542. break;
  543. }
  544. }
  545. return ssid;
  546. }
  547. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  548. struct wpa_ssid *selected)
  549. {
  550. struct wpa_ssid *ssid;
  551. /* Mark all other networks disabled and trigger reassociation */
  552. ssid = wpa_s->conf->ssid;
  553. while (ssid) {
  554. int was_disabled = ssid->disabled;
  555. ssid->disabled = ssid != selected;
  556. if (was_disabled != ssid->disabled)
  557. wpas_notify_network_enabled_changed(wpa_s, ssid);
  558. ssid = ssid->next;
  559. }
  560. wpa_s->disconnected = 0;
  561. wpa_s->reassociate = 1;
  562. wpa_s->scan_runs = 0;
  563. wpa_s->wps_success = 0;
  564. wpa_s->blacklist_cleared = 0;
  565. wpa_supplicant_req_scan(wpa_s, 0, 0);
  566. }
  567. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  568. {
  569. struct wpa_ssid *ssid;
  570. wpas_clear_wps(wpa_s);
  571. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  572. if (ssid == NULL)
  573. return -1;
  574. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  575. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  576. wpa_s, NULL);
  577. wpas_wps_reassoc(wpa_s, ssid);
  578. return 0;
  579. }
  580. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  581. const char *pin)
  582. {
  583. struct wpa_ssid *ssid;
  584. char val[128];
  585. unsigned int rpin = 0;
  586. wpas_clear_wps(wpa_s);
  587. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  588. if (ssid == NULL)
  589. return -1;
  590. if (pin)
  591. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  592. else {
  593. rpin = wps_generate_pin();
  594. os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
  595. }
  596. wpa_config_set(ssid, "phase1", val, 0);
  597. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  598. wpa_s, NULL);
  599. wpas_wps_reassoc(wpa_s, ssid);
  600. return rpin;
  601. }
  602. #ifdef CONFIG_WPS_OOB
  603. int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
  604. char *path, char *method, char *name)
  605. {
  606. struct wps_context *wps = wpa_s->wps;
  607. struct oob_device_data *oob_dev;
  608. oob_dev = wps_get_oob_device(device_type);
  609. if (oob_dev == NULL)
  610. return -1;
  611. oob_dev->device_path = path;
  612. oob_dev->device_name = name;
  613. wps->oob_conf.oob_method = wps_get_oob_method(method);
  614. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  615. /*
  616. * Use pre-configured DH keys in order to be able to write the
  617. * key hash into the OOB file.
  618. */
  619. wpabuf_free(wps->dh_pubkey);
  620. wpabuf_free(wps->dh_privkey);
  621. wps->dh_privkey = NULL;
  622. wps->dh_pubkey = NULL;
  623. dh5_free(wps->dh_ctx);
  624. wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
  625. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  626. if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
  627. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  628. "Diffie-Hellman handshake");
  629. return -1;
  630. }
  631. }
  632. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  633. wpas_clear_wps(wpa_s);
  634. if (wps_process_oob(wps, oob_dev, 0) < 0)
  635. return -1;
  636. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  637. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  638. wpas_wps_start_pin(wpa_s, NULL,
  639. wpabuf_head(wps->oob_conf.dev_password)) < 0)
  640. return -1;
  641. return 0;
  642. }
  643. #endif /* CONFIG_WPS_OOB */
  644. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  645. const char *pin, struct wps_new_ap_settings *settings)
  646. {
  647. struct wpa_ssid *ssid;
  648. char val[200];
  649. char *pos, *end;
  650. int res;
  651. if (!pin)
  652. return -1;
  653. wpas_clear_wps(wpa_s);
  654. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  655. if (ssid == NULL)
  656. return -1;
  657. pos = val;
  658. end = pos + sizeof(val);
  659. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  660. if (res < 0 || res >= end - pos)
  661. return -1;
  662. pos += res;
  663. if (settings) {
  664. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  665. "new_encr=%s new_key=%s",
  666. settings->ssid_hex, settings->auth,
  667. settings->encr, settings->key_hex);
  668. if (res < 0 || res >= end - pos)
  669. return -1;
  670. pos += res;
  671. }
  672. res = os_snprintf(pos, end - pos, "\"");
  673. if (res < 0 || res >= end - pos)
  674. return -1;
  675. wpa_config_set(ssid, "phase1", val, 0);
  676. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  677. wpa_s, NULL);
  678. wpas_wps_reassoc(wpa_s, ssid);
  679. return 0;
  680. }
  681. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  682. size_t psk_len)
  683. {
  684. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  685. "STA " MACSTR, MAC2STR(mac_addr));
  686. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  687. /* TODO */
  688. return 0;
  689. }
  690. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  691. const struct wps_device_data *dev)
  692. {
  693. char uuid[40], txt[400];
  694. int len;
  695. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  696. return;
  697. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  698. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  699. " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  700. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  701. dev->manufacturer, dev->model_name,
  702. dev->model_number, dev->serial_number,
  703. dev->categ, dev->oui, dev->sub_categ);
  704. if (len > 0 && len < (int) sizeof(txt))
  705. wpa_printf(MSG_INFO, "%s", txt);
  706. }
  707. static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
  708. u16 sel_reg_config_methods)
  709. {
  710. #ifdef CONFIG_WPS_ER
  711. struct wpa_supplicant *wpa_s = ctx;
  712. if (wpa_s->wps_er == NULL)
  713. return;
  714. wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
  715. sel_reg_config_methods);
  716. #endif /* CONFIG_WPS_ER */
  717. }
  718. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  719. {
  720. struct wps_context *wps;
  721. struct wps_registrar_config rcfg;
  722. wps = os_zalloc(sizeof(*wps));
  723. if (wps == NULL)
  724. return -1;
  725. wps->cred_cb = wpa_supplicant_wps_cred;
  726. wps->event_cb = wpa_supplicant_wps_event;
  727. wps->cb_ctx = wpa_s;
  728. wps->dev.device_name = wpa_s->conf->device_name;
  729. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  730. wps->dev.model_name = wpa_s->conf->model_name;
  731. wps->dev.model_number = wpa_s->conf->model_number;
  732. wps->dev.serial_number = wpa_s->conf->serial_number;
  733. if (wpa_s->conf->device_type) {
  734. char *pos;
  735. u8 oui[4];
  736. /* <categ>-<OUI>-<subcateg> */
  737. wps->dev.categ = atoi(wpa_s->conf->device_type);
  738. pos = os_strchr(wpa_s->conf->device_type, '-');
  739. if (pos == NULL) {
  740. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  741. os_free(wps);
  742. return -1;
  743. }
  744. pos++;
  745. if (hexstr2bin(pos, oui, 4)) {
  746. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  747. os_free(wps);
  748. return -1;
  749. }
  750. wps->dev.oui = WPA_GET_BE32(oui);
  751. pos = os_strchr(pos, '-');
  752. if (pos == NULL) {
  753. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  754. os_free(wps);
  755. return -1;
  756. }
  757. pos++;
  758. wps->dev.sub_categ = atoi(pos);
  759. }
  760. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  761. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  762. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  763. if (is_nil_uuid(wpa_s->conf->uuid)) {
  764. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  765. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  766. wps->uuid, WPS_UUID_LEN);
  767. } else
  768. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  769. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  770. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  771. os_memset(&rcfg, 0, sizeof(rcfg));
  772. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  773. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  774. rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
  775. rcfg.cb_ctx = wpa_s;
  776. wps->registrar = wps_registrar_init(wps, &rcfg);
  777. if (wps->registrar == NULL) {
  778. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  779. os_free(wps);
  780. return -1;
  781. }
  782. wpa_s->wps = wps;
  783. return 0;
  784. }
  785. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  786. {
  787. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  788. if (wpa_s->wps == NULL)
  789. return;
  790. #ifdef CONFIG_WPS_ER
  791. wps_er_deinit(wpa_s->wps_er);
  792. wpa_s->wps_er = NULL;
  793. #endif /* CONFIG_WPS_ER */
  794. wps_registrar_deinit(wpa_s->wps->registrar);
  795. wpabuf_free(wpa_s->wps->dh_pubkey);
  796. wpabuf_free(wpa_s->wps->dh_privkey);
  797. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  798. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  799. os_free(wpa_s->wps->network_key);
  800. os_free(wpa_s->wps);
  801. wpa_s->wps = NULL;
  802. }
  803. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  804. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  805. {
  806. struct wpabuf *wps_ie;
  807. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  808. return -1;
  809. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  810. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  811. if (!wps_ie) {
  812. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  813. return 0;
  814. }
  815. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  816. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  817. "without active PBC Registrar");
  818. wpabuf_free(wps_ie);
  819. return 0;
  820. }
  821. /* TODO: overlap detection */
  822. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  823. "(Active PBC)");
  824. wpabuf_free(wps_ie);
  825. return 1;
  826. }
  827. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  828. if (!wps_ie) {
  829. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  830. return 0;
  831. }
  832. /*
  833. * Start with WPS APs that advertise active PIN Registrar and
  834. * allow any WPS AP after third scan since some APs do not set
  835. * Selected Registrar attribute properly when using external
  836. * Registrar.
  837. */
  838. if (!wps_is_selected_pin_registrar(wps_ie)) {
  839. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  840. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  841. "without active PIN Registrar");
  842. wpabuf_free(wps_ie);
  843. return 0;
  844. }
  845. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  846. } else {
  847. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  848. "(Active PIN)");
  849. }
  850. wpabuf_free(wps_ie);
  851. return 1;
  852. }
  853. if (wps_ie) {
  854. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  855. wpabuf_free(wps_ie);
  856. return 1;
  857. }
  858. return -1;
  859. }
  860. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  861. struct wpa_ssid *ssid,
  862. struct wpa_scan_res *bss)
  863. {
  864. struct wpabuf *wps_ie = NULL;
  865. int ret = 0;
  866. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  867. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  868. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  869. /* allow wildcard SSID for WPS PBC */
  870. ret = 1;
  871. }
  872. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  873. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  874. if (wps_ie &&
  875. (wps_is_selected_pin_registrar(wps_ie) ||
  876. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  877. /* allow wildcard SSID for WPS PIN */
  878. ret = 1;
  879. }
  880. }
  881. if (!ret && ssid->bssid_set &&
  882. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  883. /* allow wildcard SSID due to hardcoded BSSID match */
  884. ret = 1;
  885. }
  886. wpabuf_free(wps_ie);
  887. return ret;
  888. }
  889. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  890. struct wpa_scan_res *selected,
  891. struct wpa_ssid *ssid)
  892. {
  893. const u8 *sel_uuid, *uuid;
  894. size_t i;
  895. struct wpabuf *wps_ie;
  896. int ret = 0;
  897. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  898. return 0;
  899. /* Make sure that only one AP is in active PBC mode */
  900. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  901. if (wps_ie)
  902. sel_uuid = wps_get_uuid_e(wps_ie);
  903. else
  904. sel_uuid = NULL;
  905. for (i = 0; i < wpa_s->scan_res->num; i++) {
  906. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  907. struct wpabuf *ie;
  908. if (bss == selected)
  909. continue;
  910. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  911. if (!ie)
  912. continue;
  913. if (!wps_is_selected_pbc_registrar(ie)) {
  914. wpabuf_free(ie);
  915. continue;
  916. }
  917. uuid = wps_get_uuid_e(ie);
  918. if (sel_uuid == NULL || uuid == NULL ||
  919. os_memcmp(sel_uuid, uuid, 16) != 0) {
  920. ret = 1; /* PBC overlap */
  921. wpabuf_free(ie);
  922. break;
  923. }
  924. /* TODO: verify that this is reasonable dual-band situation */
  925. wpabuf_free(ie);
  926. }
  927. wpabuf_free(wps_ie);
  928. return ret;
  929. }
  930. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  931. {
  932. size_t i;
  933. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  934. return;
  935. for (i = 0; i < wpa_s->scan_res->num; i++) {
  936. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  937. struct wpabuf *ie;
  938. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  939. if (!ie)
  940. continue;
  941. if (wps_is_selected_pbc_registrar(ie))
  942. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  943. else if (wps_is_selected_pin_registrar(ie))
  944. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  945. else
  946. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  947. wpabuf_free(ie);
  948. break;
  949. }
  950. }
  951. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  952. {
  953. struct wpa_ssid *ssid;
  954. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  955. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  956. return 1;
  957. }
  958. return 0;
  959. }
  960. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  961. char *end)
  962. {
  963. struct wpabuf *wps_ie;
  964. int ret;
  965. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  966. if (wps_ie == NULL)
  967. return 0;
  968. ret = wps_attr_text(wps_ie, buf, end);
  969. wpabuf_free(wps_ie);
  970. return ret;
  971. }
  972. int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
  973. {
  974. #ifdef CONFIG_WPS_ER
  975. if (wpa_s->wps_er) {
  976. wps_er_refresh(wpa_s->wps_er);
  977. return 0;
  978. }
  979. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
  980. if (wpa_s->wps_er == NULL)
  981. return -1;
  982. return 0;
  983. #else /* CONFIG_WPS_ER */
  984. return 0;
  985. #endif /* CONFIG_WPS_ER */
  986. }
  987. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  988. {
  989. #ifdef CONFIG_WPS_ER
  990. wps_er_deinit(wpa_s->wps_er);
  991. wpa_s->wps_er = NULL;
  992. #endif /* CONFIG_WPS_ER */
  993. return 0;
  994. }
  995. #ifdef CONFIG_WPS_ER
  996. int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const char *uuid,
  997. const char *pin)
  998. {
  999. u8 u[UUID_LEN];
  1000. int any = 0;
  1001. if (os_strcmp(uuid, "any") == 0)
  1002. any = 1;
  1003. else if (uuid_str2bin(uuid, u))
  1004. return -1;
  1005. return wps_registrar_add_pin(wpa_s->wps->registrar, any ? NULL : u,
  1006. (const u8 *) pin, os_strlen(pin), 300);
  1007. }
  1008. int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
  1009. {
  1010. u8 u[UUID_LEN];
  1011. if (uuid_str2bin(uuid, u))
  1012. return -1;
  1013. return wps_er_pbc(wpa_s->wps_er, u);
  1014. }
  1015. int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
  1016. const char *pin)
  1017. {
  1018. u8 u[UUID_LEN];
  1019. if (uuid_str2bin(uuid, u))
  1020. return -1;
  1021. return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
  1022. os_strlen(pin));
  1023. }
  1024. #endif /* CONFIG_WPS_ER */