wps_supplicant.c 45 KB

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