wps_supplicant.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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 config_error=%d",
  350. fail->msg, fail->config_error);
  351. if (wpa_s->parent && wpa_s->parent != wpa_s)
  352. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  353. "msg=%d config_error=%d",
  354. fail->msg, fail->config_error);
  355. wpas_clear_wps(wpa_s);
  356. wpas_notify_wps_event_fail(wpa_s, fail);
  357. }
  358. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  359. {
  360. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  361. wpa_s->wps_success = 1;
  362. wpas_notify_wps_event_success(wpa_s);
  363. #ifdef CONFIG_P2P
  364. wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
  365. #endif /* CONFIG_P2P */
  366. }
  367. static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
  368. struct wps_event_er_ap *ap)
  369. {
  370. char uuid_str[100];
  371. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  372. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  373. if (ap->pri_dev_type)
  374. wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
  375. sizeof(dev_type));
  376. else
  377. dev_type[0] = '\0';
  378. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
  379. " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
  380. uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
  381. ap->friendly_name ? ap->friendly_name : "",
  382. ap->manufacturer ? ap->manufacturer : "",
  383. ap->model_description ? ap->model_description : "",
  384. ap->model_name ? ap->model_name : "",
  385. ap->manufacturer_url ? ap->manufacturer_url : "",
  386. ap->model_url ? ap->model_url : "");
  387. }
  388. static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
  389. struct wps_event_er_ap *ap)
  390. {
  391. char uuid_str[100];
  392. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  393. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
  394. }
  395. static void wpa_supplicant_wps_event_er_enrollee_add(
  396. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  397. {
  398. char uuid_str[100];
  399. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  400. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  401. if (enrollee->pri_dev_type)
  402. wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
  403. sizeof(dev_type));
  404. else
  405. dev_type[0] = '\0';
  406. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
  407. " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
  408. "|%s|%s|%s|%s|%s|",
  409. uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
  410. enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
  411. enrollee->dev_name ? enrollee->dev_name : "",
  412. enrollee->manufacturer ? enrollee->manufacturer : "",
  413. enrollee->model_name ? enrollee->model_name : "",
  414. enrollee->model_number ? enrollee->model_number : "",
  415. enrollee->serial_number ? enrollee->serial_number : "");
  416. }
  417. static void wpa_supplicant_wps_event_er_enrollee_remove(
  418. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  419. {
  420. char uuid_str[100];
  421. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  422. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
  423. uuid_str, MAC2STR(enrollee->mac_addr));
  424. }
  425. static void wpa_supplicant_wps_event_er_ap_settings(
  426. struct wpa_supplicant *wpa_s,
  427. struct wps_event_er_ap_settings *ap_settings)
  428. {
  429. char uuid_str[100];
  430. char key_str[65];
  431. const struct wps_credential *cred = ap_settings->cred;
  432. key_str[0] = '\0';
  433. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  434. if (cred->key_len >= 8 && cred->key_len <= 64) {
  435. os_memcpy(key_str, cred->key, cred->key_len);
  436. key_str[cred->key_len] = '\0';
  437. }
  438. }
  439. uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
  440. /* Use wpa_msg_ctrl to avoid showing the key in debug log */
  441. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
  442. "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
  443. "key=%s",
  444. uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
  445. cred->auth_type, cred->encr_type, key_str);
  446. }
  447. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  448. union wps_event_data *data)
  449. {
  450. struct wpa_supplicant *wpa_s = ctx;
  451. switch (event) {
  452. case WPS_EV_M2D:
  453. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  454. break;
  455. case WPS_EV_FAIL:
  456. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  457. break;
  458. case WPS_EV_SUCCESS:
  459. wpa_supplicant_wps_event_success(wpa_s);
  460. break;
  461. case WPS_EV_PWD_AUTH_FAIL:
  462. break;
  463. case WPS_EV_PBC_OVERLAP:
  464. break;
  465. case WPS_EV_PBC_TIMEOUT:
  466. break;
  467. case WPS_EV_ER_AP_ADD:
  468. wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
  469. break;
  470. case WPS_EV_ER_AP_REMOVE:
  471. wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
  472. break;
  473. case WPS_EV_ER_ENROLLEE_ADD:
  474. wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
  475. &data->enrollee);
  476. break;
  477. case WPS_EV_ER_ENROLLEE_REMOVE:
  478. wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
  479. &data->enrollee);
  480. break;
  481. case WPS_EV_ER_AP_SETTINGS:
  482. wpa_supplicant_wps_event_er_ap_settings(wpa_s,
  483. &data->ap_settings);
  484. break;
  485. }
  486. }
  487. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  488. {
  489. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  490. eap_is_wps_pin_enrollee(&ssid->eap))
  491. return WPS_REQ_ENROLLEE;
  492. else
  493. return WPS_REQ_REGISTRAR;
  494. }
  495. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  496. {
  497. int id;
  498. struct wpa_ssid *ssid, *remove_ssid = NULL;
  499. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  500. /* Remove any existing WPS network from configuration */
  501. ssid = wpa_s->conf->ssid;
  502. while (ssid) {
  503. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  504. if (ssid == wpa_s->current_ssid) {
  505. wpa_s->current_ssid = NULL;
  506. if (ssid != NULL)
  507. wpas_notify_network_changed(wpa_s);
  508. }
  509. id = ssid->id;
  510. remove_ssid = ssid;
  511. } else
  512. id = -1;
  513. ssid = ssid->next;
  514. if (id >= 0) {
  515. wpas_notify_network_removed(wpa_s, remove_ssid);
  516. wpa_config_remove_network(wpa_s->conf, id);
  517. }
  518. }
  519. }
  520. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  521. {
  522. struct wpa_supplicant *wpa_s = eloop_ctx;
  523. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  524. "out");
  525. wpas_clear_wps(wpa_s);
  526. }
  527. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  528. int registrar, const u8 *bssid)
  529. {
  530. struct wpa_ssid *ssid;
  531. ssid = wpa_config_add_network(wpa_s->conf);
  532. if (ssid == NULL)
  533. return NULL;
  534. wpas_notify_network_added(wpa_s, ssid);
  535. wpa_config_set_network_defaults(ssid);
  536. ssid->temporary = 1;
  537. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  538. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  539. wpa_config_set(ssid, "identity", registrar ?
  540. "\"" WSC_ID_REGISTRAR "\"" :
  541. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  542. wpas_notify_network_removed(wpa_s, ssid);
  543. wpa_config_remove_network(wpa_s->conf, ssid->id);
  544. return NULL;
  545. }
  546. if (bssid) {
  547. #ifndef CONFIG_P2P
  548. struct wpa_bss *bss;
  549. int count = 0;
  550. #endif /* CONFIG_P2P */
  551. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  552. ssid->bssid_set = 1;
  553. /*
  554. * Note: With P2P, the SSID may change at the time the WPS
  555. * provisioning is started, so better not filter the AP based
  556. * on the current SSID in the scan results.
  557. */
  558. #ifndef CONFIG_P2P
  559. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  560. if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
  561. continue;
  562. os_free(ssid->ssid);
  563. ssid->ssid = os_malloc(bss->ssid_len);
  564. if (ssid->ssid == NULL)
  565. break;
  566. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  567. ssid->ssid_len = bss->ssid_len;
  568. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
  569. "scan results",
  570. ssid->ssid, ssid->ssid_len);
  571. count++;
  572. }
  573. if (count > 1) {
  574. wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
  575. "for the AP; use wildcard");
  576. os_free(ssid->ssid);
  577. ssid->ssid = NULL;
  578. ssid->ssid_len = 0;
  579. }
  580. #endif /* CONFIG_P2P */
  581. }
  582. return ssid;
  583. }
  584. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  585. struct wpa_ssid *selected)
  586. {
  587. struct wpa_ssid *ssid;
  588. /* Mark all other networks disabled and trigger reassociation */
  589. ssid = wpa_s->conf->ssid;
  590. while (ssid) {
  591. int was_disabled = ssid->disabled;
  592. ssid->disabled = ssid != selected;
  593. if (was_disabled != ssid->disabled)
  594. wpas_notify_network_enabled_changed(wpa_s, ssid);
  595. ssid = ssid->next;
  596. }
  597. wpa_s->disconnected = 0;
  598. wpa_s->reassociate = 1;
  599. wpa_s->scan_runs = 0;
  600. wpa_s->wps_success = 0;
  601. wpa_s->blacklist_cleared = 0;
  602. wpa_supplicant_req_scan(wpa_s, 0, 0);
  603. }
  604. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  605. int p2p_group)
  606. {
  607. struct wpa_ssid *ssid;
  608. wpas_clear_wps(wpa_s);
  609. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  610. if (ssid == NULL)
  611. return -1;
  612. ssid->temporary = 1;
  613. ssid->p2p_group = p2p_group;
  614. #ifdef CONFIG_P2P
  615. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  616. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  617. if (ssid->ssid) {
  618. ssid->ssid_len = wpa_s->go_params->ssid_len;
  619. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  620. ssid->ssid_len);
  621. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  622. "SSID", ssid->ssid, ssid->ssid_len);
  623. }
  624. }
  625. #endif /* CONFIG_P2P */
  626. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  627. if (wpa_s->wps_fragment_size)
  628. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  629. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  630. wpa_s, NULL);
  631. wpas_wps_reassoc(wpa_s, ssid);
  632. return 0;
  633. }
  634. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  635. const char *pin, int p2p_group, u16 dev_pw_id)
  636. {
  637. struct wpa_ssid *ssid;
  638. char val[128];
  639. unsigned int rpin = 0;
  640. wpas_clear_wps(wpa_s);
  641. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  642. if (ssid == NULL)
  643. return -1;
  644. ssid->temporary = 1;
  645. ssid->p2p_group = p2p_group;
  646. #ifdef CONFIG_P2P
  647. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  648. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  649. if (ssid->ssid) {
  650. ssid->ssid_len = wpa_s->go_params->ssid_len;
  651. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  652. ssid->ssid_len);
  653. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  654. "SSID", ssid->ssid, ssid->ssid_len);
  655. }
  656. }
  657. #endif /* CONFIG_P2P */
  658. if (pin)
  659. os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
  660. pin, dev_pw_id);
  661. else {
  662. rpin = wps_generate_pin();
  663. os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
  664. rpin, dev_pw_id);
  665. }
  666. wpa_config_set(ssid, "phase1", val, 0);
  667. if (wpa_s->wps_fragment_size)
  668. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  669. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  670. wpa_s, NULL);
  671. wpas_wps_reassoc(wpa_s, ssid);
  672. return rpin;
  673. }
  674. /* Cancel the wps pbc/pin requests */
  675. int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
  676. {
  677. #ifdef CONFIG_AP
  678. if (wpa_s->ap_iface) {
  679. wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
  680. return wpa_supplicant_ap_wps_cancel(wpa_s);
  681. }
  682. #endif /* CONFIG_AP */
  683. if (wpa_s->wpa_state == WPA_SCANNING) {
  684. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
  685. wpa_supplicant_cancel_scan(wpa_s);
  686. wpas_clear_wps(wpa_s);
  687. } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  688. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
  689. "deauthenticate");
  690. wpa_supplicant_deauthenticate(wpa_s,
  691. WLAN_REASON_DEAUTH_LEAVING);
  692. wpas_clear_wps(wpa_s);
  693. }
  694. return 0;
  695. }
  696. #ifdef CONFIG_WPS_OOB
  697. int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
  698. char *path, char *method, char *name)
  699. {
  700. struct wps_context *wps = wpa_s->wps;
  701. struct oob_device_data *oob_dev;
  702. oob_dev = wps_get_oob_device(device_type);
  703. if (oob_dev == NULL)
  704. return -1;
  705. oob_dev->device_path = path;
  706. oob_dev->device_name = name;
  707. wps->oob_conf.oob_method = wps_get_oob_method(method);
  708. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  709. /*
  710. * Use pre-configured DH keys in order to be able to write the
  711. * key hash into the OOB file.
  712. */
  713. wpabuf_free(wps->dh_pubkey);
  714. wpabuf_free(wps->dh_privkey);
  715. wps->dh_privkey = NULL;
  716. wps->dh_pubkey = NULL;
  717. dh5_free(wps->dh_ctx);
  718. wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
  719. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  720. if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
  721. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  722. "Diffie-Hellman handshake");
  723. return -1;
  724. }
  725. }
  726. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  727. wpas_clear_wps(wpa_s);
  728. if (wps_process_oob(wps, oob_dev, 0) < 0)
  729. return -1;
  730. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  731. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  732. wpas_wps_start_pin(wpa_s, NULL,
  733. wpabuf_head(wps->oob_conf.dev_password), 0,
  734. DEV_PW_DEFAULT) < 0)
  735. return -1;
  736. return 0;
  737. }
  738. #endif /* CONFIG_WPS_OOB */
  739. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  740. const char *pin, struct wps_new_ap_settings *settings)
  741. {
  742. struct wpa_ssid *ssid;
  743. char val[200];
  744. char *pos, *end;
  745. int res;
  746. if (!pin)
  747. return -1;
  748. wpas_clear_wps(wpa_s);
  749. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  750. if (ssid == NULL)
  751. return -1;
  752. ssid->temporary = 1;
  753. pos = val;
  754. end = pos + sizeof(val);
  755. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  756. if (res < 0 || res >= end - pos)
  757. return -1;
  758. pos += res;
  759. if (settings) {
  760. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  761. "new_encr=%s new_key=%s",
  762. settings->ssid_hex, settings->auth,
  763. settings->encr, settings->key_hex);
  764. if (res < 0 || res >= end - pos)
  765. return -1;
  766. pos += res;
  767. }
  768. res = os_snprintf(pos, end - pos, "\"");
  769. if (res < 0 || res >= end - pos)
  770. return -1;
  771. wpa_config_set(ssid, "phase1", val, 0);
  772. if (wpa_s->wps_fragment_size)
  773. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  774. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  775. wpa_s, NULL);
  776. wpas_wps_reassoc(wpa_s, ssid);
  777. return 0;
  778. }
  779. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  780. size_t psk_len)
  781. {
  782. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  783. "STA " MACSTR, MAC2STR(mac_addr));
  784. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  785. /* TODO */
  786. return 0;
  787. }
  788. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  789. const struct wps_device_data *dev)
  790. {
  791. char uuid[40], txt[400];
  792. int len;
  793. char devtype[WPS_DEV_TYPE_BUFSIZE];
  794. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  795. return;
  796. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  797. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  798. " [%s|%s|%s|%s|%s|%s]",
  799. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  800. dev->manufacturer, dev->model_name,
  801. dev->model_number, dev->serial_number,
  802. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  803. sizeof(devtype)));
  804. if (len > 0 && len < (int) sizeof(txt))
  805. wpa_printf(MSG_INFO, "%s", txt);
  806. }
  807. static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
  808. u16 sel_reg_config_methods)
  809. {
  810. #ifdef CONFIG_WPS_ER
  811. struct wpa_supplicant *wpa_s = ctx;
  812. if (wpa_s->wps_er == NULL)
  813. return;
  814. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
  815. "dev_password_id=%u sel_reg_config_methods=0x%x",
  816. sel_reg, dev_passwd_id, sel_reg_config_methods);
  817. wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
  818. sel_reg_config_methods);
  819. #endif /* CONFIG_WPS_ER */
  820. }
  821. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  822. {
  823. struct wps_context *wps;
  824. struct wps_registrar_config rcfg;
  825. wps = os_zalloc(sizeof(*wps));
  826. if (wps == NULL)
  827. return -1;
  828. wps->cred_cb = wpa_supplicant_wps_cred;
  829. wps->event_cb = wpa_supplicant_wps_event;
  830. wps->cb_ctx = wpa_s;
  831. wps->dev.device_name = wpa_s->conf->device_name;
  832. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  833. wps->dev.model_name = wpa_s->conf->model_name;
  834. wps->dev.model_number = wpa_s->conf->model_number;
  835. wps->dev.serial_number = wpa_s->conf->serial_number;
  836. wps->config_methods =
  837. wps_config_methods_str2bin(wpa_s->conf->config_methods);
  838. if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  839. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  840. wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
  841. "methods are not allowed at the same time");
  842. os_free(wps);
  843. return -1;
  844. }
  845. if (wpa_s->conf->device_type &&
  846. wps_dev_type_str2bin(wpa_s->conf->device_type,
  847. wps->dev.pri_dev_type) < 0) {
  848. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  849. os_free(wps);
  850. return -1;
  851. }
  852. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  853. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  854. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  855. if (is_nil_uuid(wpa_s->conf->uuid)) {
  856. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  857. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  858. wps->uuid, WPS_UUID_LEN);
  859. } else
  860. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  861. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  862. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  863. os_memset(&rcfg, 0, sizeof(rcfg));
  864. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  865. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  866. rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
  867. rcfg.cb_ctx = wpa_s;
  868. wps->registrar = wps_registrar_init(wps, &rcfg);
  869. if (wps->registrar == NULL) {
  870. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  871. os_free(wps);
  872. return -1;
  873. }
  874. wpa_s->wps = wps;
  875. return 0;
  876. }
  877. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  878. {
  879. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  880. if (wpa_s->wps == NULL)
  881. return;
  882. #ifdef CONFIG_WPS_ER
  883. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  884. wpa_s->wps_er = NULL;
  885. #endif /* CONFIG_WPS_ER */
  886. wps_registrar_deinit(wpa_s->wps->registrar);
  887. wpabuf_free(wpa_s->wps->dh_pubkey);
  888. wpabuf_free(wpa_s->wps->dh_privkey);
  889. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  890. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  891. os_free(wpa_s->wps->network_key);
  892. os_free(wpa_s->wps);
  893. wpa_s->wps = NULL;
  894. }
  895. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  896. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  897. {
  898. struct wpabuf *wps_ie;
  899. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  900. return -1;
  901. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  902. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  903. if (!wps_ie) {
  904. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  905. return 0;
  906. }
  907. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  908. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  909. "without active PBC Registrar");
  910. wpabuf_free(wps_ie);
  911. return 0;
  912. }
  913. /* TODO: overlap detection */
  914. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  915. "(Active PBC)");
  916. wpabuf_free(wps_ie);
  917. return 1;
  918. }
  919. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  920. if (!wps_ie) {
  921. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  922. return 0;
  923. }
  924. /*
  925. * Start with WPS APs that advertise our address as an
  926. * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
  927. * allow any WPS AP after couple of scans since some APs do not
  928. * set Selected Registrar attribute properly when using
  929. * external Registrar.
  930. */
  931. if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
  932. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  933. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  934. "without active PIN Registrar");
  935. wpabuf_free(wps_ie);
  936. return 0;
  937. }
  938. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  939. } else {
  940. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  941. "(Authorized MAC or Active PIN)");
  942. }
  943. wpabuf_free(wps_ie);
  944. return 1;
  945. }
  946. if (wps_ie) {
  947. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  948. wpabuf_free(wps_ie);
  949. return 1;
  950. }
  951. return -1;
  952. }
  953. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  954. struct wpa_ssid *ssid,
  955. struct wpa_scan_res *bss)
  956. {
  957. struct wpabuf *wps_ie = NULL;
  958. int ret = 0;
  959. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  960. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  961. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  962. /* allow wildcard SSID for WPS PBC */
  963. ret = 1;
  964. }
  965. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  966. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  967. if (wps_ie &&
  968. (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
  969. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  970. /* allow wildcard SSID for WPS PIN */
  971. ret = 1;
  972. }
  973. }
  974. if (!ret && ssid->bssid_set &&
  975. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  976. /* allow wildcard SSID due to hardcoded BSSID match */
  977. ret = 1;
  978. }
  979. #ifdef CONFIG_WPS_STRICT
  980. if (wps_ie) {
  981. if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
  982. 0, bss->bssid) < 0)
  983. ret = 0;
  984. if (bss->beacon_ie_len) {
  985. struct wpabuf *bcn_wps;
  986. bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
  987. bss, WPS_IE_VENDOR_TYPE);
  988. if (bcn_wps == NULL) {
  989. wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
  990. "missing from AP Beacon");
  991. ret = 0;
  992. } else {
  993. if (wps_validate_beacon(wps_ie) < 0)
  994. ret = 0;
  995. wpabuf_free(bcn_wps);
  996. }
  997. }
  998. }
  999. #endif /* CONFIG_WPS_STRICT */
  1000. wpabuf_free(wps_ie);
  1001. return ret;
  1002. }
  1003. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  1004. struct wpa_bss *selected, struct wpa_ssid *ssid)
  1005. {
  1006. const u8 *sel_uuid, *uuid;
  1007. struct wpabuf *wps_ie;
  1008. int ret = 0;
  1009. struct wpa_bss *bss;
  1010. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  1011. return 0;
  1012. wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
  1013. "present in scan results; selected BSSID " MACSTR,
  1014. MAC2STR(selected->bssid));
  1015. /* Make sure that only one AP is in active PBC mode */
  1016. wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  1017. if (wps_ie) {
  1018. sel_uuid = wps_get_uuid_e(wps_ie);
  1019. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
  1020. sel_uuid, UUID_LEN);
  1021. } else {
  1022. wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
  1023. "WPS IE?!");
  1024. sel_uuid = NULL;
  1025. }
  1026. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1027. struct wpabuf *ie;
  1028. if (bss == selected)
  1029. continue;
  1030. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1031. if (!ie)
  1032. continue;
  1033. if (!wps_is_selected_pbc_registrar(ie)) {
  1034. wpabuf_free(ie);
  1035. continue;
  1036. }
  1037. wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
  1038. MACSTR, MAC2STR(bss->bssid));
  1039. uuid = wps_get_uuid_e(ie);
  1040. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
  1041. uuid, UUID_LEN);
  1042. if (sel_uuid == NULL || uuid == NULL ||
  1043. os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
  1044. ret = 1; /* PBC overlap */
  1045. wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
  1046. MACSTR " and " MACSTR,
  1047. MAC2STR(selected->bssid),
  1048. MAC2STR(bss->bssid));
  1049. wpabuf_free(ie);
  1050. break;
  1051. }
  1052. /* TODO: verify that this is reasonable dual-band situation */
  1053. wpabuf_free(ie);
  1054. }
  1055. wpabuf_free(wps_ie);
  1056. return ret;
  1057. }
  1058. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  1059. {
  1060. struct wpa_bss *bss;
  1061. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  1062. return;
  1063. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1064. struct wpabuf *ie;
  1065. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1066. if (!ie)
  1067. continue;
  1068. if (wps_is_selected_pbc_registrar(ie))
  1069. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1070. WPS_EVENT_AP_AVAILABLE_PBC);
  1071. else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
  1072. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1073. WPS_EVENT_AP_AVAILABLE_AUTH);
  1074. else if (wps_is_selected_pin_registrar(ie))
  1075. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1076. WPS_EVENT_AP_AVAILABLE_PIN);
  1077. else
  1078. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1079. WPS_EVENT_AP_AVAILABLE);
  1080. wpabuf_free(ie);
  1081. break;
  1082. }
  1083. }
  1084. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  1085. {
  1086. struct wpa_ssid *ssid;
  1087. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1088. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  1089. return 1;
  1090. }
  1091. return 0;
  1092. }
  1093. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  1094. char *end)
  1095. {
  1096. struct wpabuf *wps_ie;
  1097. int ret;
  1098. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  1099. if (wps_ie == NULL)
  1100. return 0;
  1101. ret = wps_attr_text(wps_ie, buf, end);
  1102. wpabuf_free(wps_ie);
  1103. return ret;
  1104. }
  1105. int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
  1106. {
  1107. #ifdef CONFIG_WPS_ER
  1108. if (wpa_s->wps_er) {
  1109. wps_er_refresh(wpa_s->wps_er);
  1110. return 0;
  1111. }
  1112. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
  1113. if (wpa_s->wps_er == NULL)
  1114. return -1;
  1115. return 0;
  1116. #else /* CONFIG_WPS_ER */
  1117. return 0;
  1118. #endif /* CONFIG_WPS_ER */
  1119. }
  1120. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  1121. {
  1122. #ifdef CONFIG_WPS_ER
  1123. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1124. wpa_s->wps_er = NULL;
  1125. #endif /* CONFIG_WPS_ER */
  1126. return 0;
  1127. }
  1128. #ifdef CONFIG_WPS_ER
  1129. int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
  1130. const char *uuid, const char *pin)
  1131. {
  1132. u8 u[UUID_LEN];
  1133. int any = 0;
  1134. if (os_strcmp(uuid, "any") == 0)
  1135. any = 1;
  1136. else if (uuid_str2bin(uuid, u))
  1137. return -1;
  1138. return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
  1139. any ? NULL : u,
  1140. (const u8 *) pin, os_strlen(pin), 300);
  1141. }
  1142. int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
  1143. {
  1144. u8 u[UUID_LEN];
  1145. if (uuid_str2bin(uuid, u))
  1146. return -1;
  1147. return wps_er_pbc(wpa_s->wps_er, u);
  1148. }
  1149. int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
  1150. const char *pin)
  1151. {
  1152. u8 u[UUID_LEN];
  1153. if (uuid_str2bin(uuid, u))
  1154. return -1;
  1155. return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
  1156. os_strlen(pin));
  1157. }
  1158. int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1159. const char *pin, struct wps_new_ap_settings *settings)
  1160. {
  1161. u8 u[UUID_LEN];
  1162. struct wps_credential cred;
  1163. size_t len;
  1164. if (uuid_str2bin(uuid, u))
  1165. return -1;
  1166. if (settings->ssid_hex == NULL || settings->auth == NULL ||
  1167. settings->encr == NULL || settings->key_hex == NULL)
  1168. return -1;
  1169. os_memset(&cred, 0, sizeof(cred));
  1170. len = os_strlen(settings->ssid_hex);
  1171. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1172. hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
  1173. return -1;
  1174. cred.ssid_len = len / 2;
  1175. len = os_strlen(settings->key_hex);
  1176. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1177. hexstr2bin(settings->key_hex, cred.key, len / 2))
  1178. return -1;
  1179. cred.key_len = len / 2;
  1180. if (os_strcmp(settings->auth, "OPEN") == 0)
  1181. cred.auth_type = WPS_AUTH_OPEN;
  1182. else if (os_strcmp(settings->auth, "WPAPSK") == 0)
  1183. cred.auth_type = WPS_AUTH_WPAPSK;
  1184. else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
  1185. cred.auth_type = WPS_AUTH_WPA2PSK;
  1186. else
  1187. return -1;
  1188. if (os_strcmp(settings->encr, "NONE") == 0)
  1189. cred.encr_type = WPS_ENCR_NONE;
  1190. else if (os_strcmp(settings->encr, "WEP") == 0)
  1191. cred.encr_type = WPS_ENCR_WEP;
  1192. else if (os_strcmp(settings->encr, "TKIP") == 0)
  1193. cred.encr_type = WPS_ENCR_TKIP;
  1194. else if (os_strcmp(settings->encr, "CCMP") == 0)
  1195. cred.encr_type = WPS_ENCR_AES;
  1196. else
  1197. return -1;
  1198. return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
  1199. os_strlen(pin), &cred);
  1200. }
  1201. static void wpas_wps_terminate_cb(void *ctx)
  1202. {
  1203. wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
  1204. eloop_terminate();
  1205. }
  1206. #endif /* CONFIG_WPS_ER */
  1207. int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
  1208. {
  1209. #ifdef CONFIG_WPS_ER
  1210. if (wpa_s->wps_er) {
  1211. wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
  1212. wpa_s->wps_er = NULL;
  1213. return 1;
  1214. }
  1215. #endif /* CONFIG_WPS_ER */
  1216. return 0;
  1217. }
  1218. int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
  1219. {
  1220. struct wpa_ssid *ssid;
  1221. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1222. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  1223. return 1;
  1224. }
  1225. return 0;
  1226. }
  1227. void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
  1228. {
  1229. struct wps_context *wps = wpa_s->wps;
  1230. if (wps == NULL)
  1231. return;
  1232. if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
  1233. wps->config_methods = wps_config_methods_str2bin(
  1234. wpa_s->conf->config_methods);
  1235. if ((wps->config_methods &
  1236. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1237. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1238. wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
  1239. "config methods are not allowed at the "
  1240. "same time");
  1241. wps->config_methods &= ~WPS_CONFIG_LABEL;
  1242. }
  1243. }
  1244. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
  1245. if (wpa_s->conf->device_type &&
  1246. wps_dev_type_str2bin(wpa_s->conf->device_type,
  1247. wps->dev.pri_dev_type) < 0)
  1248. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  1249. }
  1250. if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
  1251. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1252. if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
  1253. if (is_nil_uuid(wpa_s->conf->uuid)) {
  1254. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  1255. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  1256. "address", wps->uuid, WPS_UUID_LEN);
  1257. } else
  1258. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  1259. }
  1260. if (wpa_s->conf->changed_parameters &
  1261. (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
  1262. /* Update pointers to make sure they refer current values */
  1263. wps->dev.device_name = wpa_s->conf->device_name;
  1264. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1265. wps->dev.model_name = wpa_s->conf->model_name;
  1266. wps->dev.model_number = wpa_s->conf->model_number;
  1267. wps->dev.serial_number = wpa_s->conf->serial_number;
  1268. }
  1269. }