wps_supplicant.c 46 KB

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