wps_supplicant.c 31 KB

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