wps_supplicant.c 52 KB

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