wps_supplicant.c 39 KB

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