wps_supplicant.c 33 KB

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