wps_supplicant.c 31 KB

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