wps_supplicant.c 57 KB

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