wps_supplicant.c 47 KB

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