wps_supplicant.c 31 KB

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