wps_supplicant.c 42 KB

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