wps_supplicant.c 38 KB

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