wps_supplicant.c 57 KB

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