wps_supplicant.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731
  1. /*
  2. * wpa_supplicant / WPS integration
  3. * Copyright (c) 2008-2014, 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. #ifdef CONFIG_P2P
  49. if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
  50. return 1;
  51. #endif /* CONFIG_P2P */
  52. if (!wpa_s->wps_success &&
  53. wpa_s->current_ssid &&
  54. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  55. const u8 *bssid = wpa_s->bssid;
  56. if (is_zero_ether_addr(bssid))
  57. bssid = wpa_s->pending_bssid;
  58. wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
  59. " did not succeed - continue trying to find "
  60. "suitable AP", MAC2STR(bssid));
  61. wpa_blacklist_add(wpa_s, bssid);
  62. wpa_supplicant_deauthenticate(wpa_s,
  63. WLAN_REASON_DEAUTH_LEAVING);
  64. wpa_s->reassociate = 1;
  65. wpa_supplicant_req_scan(wpa_s,
  66. wpa_s->blacklist_cleared ? 5 : 0, 0);
  67. wpa_s->blacklist_cleared = 0;
  68. return 1;
  69. }
  70. wpas_wps_clear_ap_info(wpa_s);
  71. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  72. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
  73. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
  74. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  75. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  76. int disabled = wpa_s->current_ssid->disabled;
  77. unsigned int freq = wpa_s->assoc_freq;
  78. struct wpa_bss *bss;
  79. struct wpa_ssid *ssid = NULL;
  80. int use_fast_assoc = 0;
  81. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  82. "try to associate with the received credential "
  83. "(freq=%u)", freq);
  84. wpa_supplicant_deauthenticate(wpa_s,
  85. WLAN_REASON_DEAUTH_LEAVING);
  86. if (disabled) {
  87. wpa_printf(MSG_DEBUG, "WPS: Current network is "
  88. "disabled - wait for user to enable");
  89. return 1;
  90. }
  91. wpa_s->after_wps = 5;
  92. wpa_s->wps_freq = freq;
  93. wpa_s->normal_scans = 0;
  94. wpa_s->reassociate = 1;
  95. wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
  96. "without a new scan can be used");
  97. bss = wpa_supplicant_pick_network(wpa_s, &ssid);
  98. if (bss) {
  99. struct wpabuf *wps;
  100. struct wps_parse_attr attr;
  101. wps = wpa_bss_get_vendor_ie_multi(bss,
  102. WPS_IE_VENDOR_TYPE);
  103. if (wps && wps_parse_msg(wps, &attr) == 0 &&
  104. attr.wps_state &&
  105. *attr.wps_state == WPS_STATE_CONFIGURED)
  106. use_fast_assoc = 1;
  107. wpabuf_free(wps);
  108. }
  109. if (!use_fast_assoc ||
  110. wpa_supplicant_fast_associate(wpa_s) != 1)
  111. wpa_supplicant_req_scan(wpa_s, 0, 0);
  112. return 1;
  113. }
  114. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
  115. wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
  116. "for external credential processing");
  117. wpas_clear_wps(wpa_s);
  118. wpa_supplicant_deauthenticate(wpa_s,
  119. WLAN_REASON_DEAUTH_LEAVING);
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
  125. struct wpa_ssid *ssid,
  126. const struct wps_credential *cred)
  127. {
  128. struct wpa_driver_capa capa;
  129. struct wpa_bss *bss;
  130. const u8 *ie;
  131. struct wpa_ie_data adv;
  132. int wpa2 = 0, ccmp = 0;
  133. /*
  134. * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
  135. * case they are configured for mixed mode operation (WPA+WPA2 and
  136. * TKIP+CCMP). Try to use scan results to figure out whether the AP
  137. * actually supports stronger security and select that if the client
  138. * has support for it, too.
  139. */
  140. if (wpa_drv_get_capa(wpa_s, &capa))
  141. return; /* Unknown what driver supports */
  142. if (ssid->ssid == NULL)
  143. return;
  144. bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
  145. if (bss == NULL) {
  146. wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
  147. "table - use credential as-is");
  148. return;
  149. }
  150. wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
  151. ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  152. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
  153. wpa2 = 1;
  154. if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
  155. ccmp = 1;
  156. } else {
  157. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  158. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
  159. adv.pairwise_cipher & WPA_CIPHER_CCMP)
  160. ccmp = 1;
  161. }
  162. if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
  163. (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
  164. /*
  165. * TODO: This could be the initial AP configuration and the
  166. * Beacon contents could change shortly. Should request a new
  167. * scan and delay addition of the network until the updated
  168. * scan results are available.
  169. */
  170. wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
  171. "support - use credential as-is");
  172. return;
  173. }
  174. if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  175. (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
  176. (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  177. wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
  178. "based on scan results");
  179. if (wpa_s->conf->ap_scan == 1)
  180. ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
  181. else
  182. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  183. }
  184. if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
  185. (ssid->proto & WPA_PROTO_WPA) &&
  186. (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
  187. wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
  188. "based on scan results");
  189. if (wpa_s->conf->ap_scan == 1)
  190. ssid->proto |= WPA_PROTO_RSN;
  191. else
  192. ssid->proto = WPA_PROTO_RSN;
  193. }
  194. }
  195. static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
  196. struct wpa_ssid *new_ssid)
  197. {
  198. struct wpa_ssid *ssid, *next;
  199. for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
  200. ssid = next, next = ssid ? ssid->next : NULL) {
  201. /*
  202. * new_ssid has already been added to the list in
  203. * wpas_wps_add_network(), so skip it.
  204. */
  205. if (ssid == new_ssid)
  206. continue;
  207. if (ssid->bssid_set || new_ssid->bssid_set) {
  208. if (ssid->bssid_set != new_ssid->bssid_set)
  209. continue;
  210. if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
  211. 0)
  212. continue;
  213. }
  214. /* compare SSID */
  215. if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
  216. continue;
  217. if (ssid->ssid && new_ssid->ssid) {
  218. if (os_memcmp(ssid->ssid, new_ssid->ssid,
  219. ssid->ssid_len) != 0)
  220. continue;
  221. } else if (ssid->ssid || new_ssid->ssid)
  222. continue;
  223. /* compare security parameters */
  224. if (ssid->auth_alg != new_ssid->auth_alg ||
  225. ssid->key_mgmt != new_ssid->key_mgmt ||
  226. ssid->proto != new_ssid->proto ||
  227. ssid->pairwise_cipher != new_ssid->pairwise_cipher ||
  228. ssid->group_cipher != new_ssid->group_cipher)
  229. continue;
  230. /* Remove the duplicated older network entry. */
  231. wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
  232. wpas_notify_network_removed(wpa_s, ssid);
  233. wpa_config_remove_network(wpa_s->conf, ssid->id);
  234. }
  235. }
  236. static int wpa_supplicant_wps_cred(void *ctx,
  237. const struct wps_credential *cred)
  238. {
  239. struct wpa_supplicant *wpa_s = ctx;
  240. struct wpa_ssid *ssid = wpa_s->current_ssid;
  241. u16 auth_type;
  242. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  243. int registrar = 0;
  244. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  245. if ((wpa_s->conf->wps_cred_processing == 1 ||
  246. wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
  247. size_t blen = cred->cred_attr_len * 2 + 1;
  248. char *buf = os_malloc(blen);
  249. if (buf) {
  250. wpa_snprintf_hex(buf, blen,
  251. cred->cred_attr, cred->cred_attr_len);
  252. wpa_msg(wpa_s, MSG_INFO, "%s%s",
  253. WPS_EVENT_CRED_RECEIVED, buf);
  254. os_free(buf);
  255. }
  256. wpas_notify_wps_credential(wpa_s, cred);
  257. } else
  258. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
  259. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  260. cred->cred_attr, cred->cred_attr_len);
  261. if (wpa_s->conf->wps_cred_processing == 1)
  262. return 0;
  263. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  264. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  265. cred->auth_type);
  266. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  267. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  268. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  269. cred->key, cred->key_len);
  270. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  271. MAC2STR(cred->mac_addr));
  272. auth_type = cred->auth_type;
  273. if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  274. wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
  275. "auth_type into WPA2PSK");
  276. auth_type = WPS_AUTH_WPA2PSK;
  277. }
  278. if (auth_type != WPS_AUTH_OPEN &&
  279. auth_type != WPS_AUTH_WPAPSK &&
  280. auth_type != WPS_AUTH_WPA2PSK) {
  281. wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
  282. "unsupported authentication type 0x%x",
  283. auth_type);
  284. return 0;
  285. }
  286. if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
  287. if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
  288. wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
  289. "invalid Network Key length %lu",
  290. (unsigned long) cred->key_len);
  291. return -1;
  292. }
  293. }
  294. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  295. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  296. "on the received credential");
  297. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  298. if (ssid->eap.identity &&
  299. ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
  300. os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
  301. WSC_ID_REGISTRAR_LEN) == 0)
  302. registrar = 1;
  303. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  304. os_free(ssid->eap.identity);
  305. ssid->eap.identity = NULL;
  306. ssid->eap.identity_len = 0;
  307. os_free(ssid->eap.phase1);
  308. ssid->eap.phase1 = NULL;
  309. os_free(ssid->eap.eap_methods);
  310. ssid->eap.eap_methods = NULL;
  311. if (!ssid->p2p_group) {
  312. ssid->temporary = 0;
  313. ssid->bssid_set = 0;
  314. }
  315. ssid->disabled_until.sec = 0;
  316. ssid->disabled_until.usec = 0;
  317. ssid->auth_failures = 0;
  318. } else {
  319. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  320. "received credential");
  321. ssid = wpa_config_add_network(wpa_s->conf);
  322. if (ssid == NULL)
  323. return -1;
  324. if (wpa_s->current_ssid) {
  325. /*
  326. * Should the GO issue multiple credentials for some
  327. * reason, each credential should be marked as a
  328. * temporary P2P group similarly to the one that gets
  329. * marked as such based on the pre-configured values
  330. * used for the WPS network block.
  331. */
  332. ssid->p2p_group = wpa_s->current_ssid->p2p_group;
  333. ssid->temporary = wpa_s->current_ssid->temporary;
  334. }
  335. wpas_notify_network_added(wpa_s, ssid);
  336. }
  337. wpa_config_set_network_defaults(ssid);
  338. os_free(ssid->ssid);
  339. ssid->ssid = os_malloc(cred->ssid_len);
  340. if (ssid->ssid) {
  341. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  342. ssid->ssid_len = cred->ssid_len;
  343. }
  344. switch (cred->encr_type) {
  345. case WPS_ENCR_NONE:
  346. break;
  347. case WPS_ENCR_TKIP:
  348. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  349. break;
  350. case WPS_ENCR_AES:
  351. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  352. break;
  353. }
  354. switch (auth_type) {
  355. case WPS_AUTH_OPEN:
  356. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  357. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  358. ssid->proto = 0;
  359. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  360. if (registrar) {
  361. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
  362. "id=%d - Credentials for an open "
  363. "network disabled by default - use "
  364. "'select_network %d' to enable",
  365. ssid->id, ssid->id);
  366. ssid->disabled = 1;
  367. }
  368. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  369. break;
  370. case WPS_AUTH_WPAPSK:
  371. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  372. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  373. ssid->proto = WPA_PROTO_WPA;
  374. break;
  375. case WPS_AUTH_WPA2PSK:
  376. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  377. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  378. ssid->proto = WPA_PROTO_RSN;
  379. break;
  380. }
  381. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  382. if (cred->key_len == 2 * PMK_LEN) {
  383. if (hexstr2bin((const char *) cred->key, ssid->psk,
  384. PMK_LEN)) {
  385. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  386. "Key");
  387. return -1;
  388. }
  389. ssid->psk_set = 1;
  390. ssid->export_keys = 1;
  391. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  392. os_free(ssid->passphrase);
  393. ssid->passphrase = os_malloc(cred->key_len + 1);
  394. if (ssid->passphrase == NULL)
  395. return -1;
  396. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  397. ssid->passphrase[cred->key_len] = '\0';
  398. wpa_config_update_psk(ssid);
  399. ssid->export_keys = 1;
  400. } else {
  401. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  402. "length %lu",
  403. (unsigned long) cred->key_len);
  404. return -1;
  405. }
  406. }
  407. wpas_wps_security_workaround(wpa_s, ssid, cred);
  408. wpas_wps_remove_dup_network(wpa_s, ssid);
  409. #ifndef CONFIG_NO_CONFIG_WRITE
  410. if (wpa_s->conf->update_config &&
  411. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  412. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  413. return -1;
  414. }
  415. #endif /* CONFIG_NO_CONFIG_WRITE */
  416. /*
  417. * Optimize the post-WPS scan based on the channel used during
  418. * the provisioning in case EAP-Failure is not received.
  419. */
  420. wpa_s->after_wps = 5;
  421. wpa_s->wps_freq = wpa_s->assoc_freq;
  422. return 0;
  423. }
  424. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  425. struct wps_event_m2d *m2d)
  426. {
  427. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  428. "dev_password_id=%d config_error=%d",
  429. m2d->dev_password_id, m2d->config_error);
  430. wpas_notify_wps_event_m2d(wpa_s, m2d);
  431. #ifdef CONFIG_P2P
  432. if (wpa_s->parent && wpa_s->parent != wpa_s) {
  433. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
  434. "dev_password_id=%d config_error=%d",
  435. m2d->dev_password_id, m2d->config_error);
  436. }
  437. if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
  438. /*
  439. * Notify P2P from eloop timeout to avoid issues with the
  440. * interface getting removed while processing a message.
  441. */
  442. eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
  443. NULL);
  444. }
  445. #endif /* CONFIG_P2P */
  446. }
  447. static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
  448. {
  449. struct wpa_supplicant *wpa_s = eloop_ctx;
  450. wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
  451. wpas_clear_wps(wpa_s);
  452. }
  453. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  454. struct wps_event_fail *fail)
  455. {
  456. if (fail->error_indication > 0 &&
  457. fail->error_indication < NUM_WPS_EI_VALUES) {
  458. wpa_msg(wpa_s, MSG_INFO,
  459. WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
  460. fail->msg, fail->config_error, fail->error_indication,
  461. wps_ei_str(fail->error_indication));
  462. if (wpa_s->parent && wpa_s->parent != wpa_s)
  463. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  464. "msg=%d config_error=%d reason=%d (%s)",
  465. fail->msg, fail->config_error,
  466. fail->error_indication,
  467. wps_ei_str(fail->error_indication));
  468. } else {
  469. wpa_msg(wpa_s, MSG_INFO,
  470. WPS_EVENT_FAIL "msg=%d config_error=%d",
  471. fail->msg, fail->config_error);
  472. if (wpa_s->parent && wpa_s->parent != wpa_s)
  473. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  474. "msg=%d config_error=%d",
  475. fail->msg, fail->config_error);
  476. }
  477. /*
  478. * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
  479. */
  480. wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
  481. eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
  482. eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
  483. wpas_notify_wps_event_fail(wpa_s, fail);
  484. #ifdef CONFIG_P2P
  485. wpas_p2p_wps_failed(wpa_s, fail);
  486. #endif /* CONFIG_P2P */
  487. }
  488. static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
  489. static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
  490. {
  491. struct wpa_ssid *ssid;
  492. int changed = 0;
  493. eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
  494. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  495. if (ssid->disabled_for_connect && ssid->disabled) {
  496. ssid->disabled_for_connect = 0;
  497. ssid->disabled = 0;
  498. wpas_notify_network_enabled_changed(wpa_s, ssid);
  499. changed++;
  500. }
  501. }
  502. if (changed) {
  503. #ifndef CONFIG_NO_CONFIG_WRITE
  504. if (wpa_s->conf->update_config &&
  505. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  506. wpa_printf(MSG_DEBUG, "WPS: Failed to update "
  507. "configuration");
  508. }
  509. #endif /* CONFIG_NO_CONFIG_WRITE */
  510. }
  511. }
  512. static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
  513. {
  514. struct wpa_supplicant *wpa_s = eloop_ctx;
  515. /* Enable the networks disabled during wpas_wps_reassoc */
  516. wpas_wps_reenable_networks(wpa_s);
  517. }
  518. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  519. {
  520. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  521. wpa_s->wps_success = 1;
  522. wpas_notify_wps_event_success(wpa_s);
  523. if (wpa_s->current_ssid)
  524. wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
  525. wpa_s->extra_blacklist_count = 0;
  526. /*
  527. * Enable the networks disabled during wpas_wps_reassoc after 10
  528. * seconds. The 10 seconds timer is to allow the data connection to be
  529. * formed before allowing other networks to be selected.
  530. */
  531. eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
  532. NULL);
  533. #ifdef CONFIG_P2P
  534. wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
  535. #endif /* CONFIG_P2P */
  536. }
  537. static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
  538. struct wps_event_er_ap *ap)
  539. {
  540. char uuid_str[100];
  541. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  542. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  543. if (ap->pri_dev_type)
  544. wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
  545. sizeof(dev_type));
  546. else
  547. dev_type[0] = '\0';
  548. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
  549. " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
  550. uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
  551. ap->friendly_name ? ap->friendly_name : "",
  552. ap->manufacturer ? ap->manufacturer : "",
  553. ap->model_description ? ap->model_description : "",
  554. ap->model_name ? ap->model_name : "",
  555. ap->manufacturer_url ? ap->manufacturer_url : "",
  556. ap->model_url ? ap->model_url : "");
  557. }
  558. static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
  559. struct wps_event_er_ap *ap)
  560. {
  561. char uuid_str[100];
  562. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  563. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
  564. }
  565. static void wpa_supplicant_wps_event_er_enrollee_add(
  566. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  567. {
  568. char uuid_str[100];
  569. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  570. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  571. if (enrollee->pri_dev_type)
  572. wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
  573. sizeof(dev_type));
  574. else
  575. dev_type[0] = '\0';
  576. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
  577. " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
  578. "|%s|%s|%s|%s|%s|",
  579. uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
  580. enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
  581. enrollee->dev_name ? enrollee->dev_name : "",
  582. enrollee->manufacturer ? enrollee->manufacturer : "",
  583. enrollee->model_name ? enrollee->model_name : "",
  584. enrollee->model_number ? enrollee->model_number : "",
  585. enrollee->serial_number ? enrollee->serial_number : "");
  586. }
  587. static void wpa_supplicant_wps_event_er_enrollee_remove(
  588. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  589. {
  590. char uuid_str[100];
  591. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  592. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
  593. uuid_str, MAC2STR(enrollee->mac_addr));
  594. }
  595. static void wpa_supplicant_wps_event_er_ap_settings(
  596. struct wpa_supplicant *wpa_s,
  597. struct wps_event_er_ap_settings *ap_settings)
  598. {
  599. char uuid_str[100];
  600. char key_str[65];
  601. const struct wps_credential *cred = ap_settings->cred;
  602. key_str[0] = '\0';
  603. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  604. if (cred->key_len >= 8 && cred->key_len <= 64) {
  605. os_memcpy(key_str, cred->key, cred->key_len);
  606. key_str[cred->key_len] = '\0';
  607. }
  608. }
  609. uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
  610. /* Use wpa_msg_ctrl to avoid showing the key in debug log */
  611. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
  612. "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
  613. "key=%s",
  614. uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
  615. cred->auth_type, cred->encr_type, key_str);
  616. }
  617. static void wpa_supplicant_wps_event_er_set_sel_reg(
  618. struct wpa_supplicant *wpa_s,
  619. struct wps_event_er_set_selected_registrar *ev)
  620. {
  621. char uuid_str[100];
  622. uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
  623. switch (ev->state) {
  624. case WPS_ER_SET_SEL_REG_START:
  625. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  626. "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
  627. "sel_reg_config_methods=0x%x",
  628. uuid_str, ev->sel_reg, ev->dev_passwd_id,
  629. ev->sel_reg_config_methods);
  630. break;
  631. case WPS_ER_SET_SEL_REG_DONE:
  632. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  633. "uuid=%s state=DONE", uuid_str);
  634. break;
  635. case WPS_ER_SET_SEL_REG_FAILED:
  636. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
  637. "uuid=%s state=FAILED", uuid_str);
  638. break;
  639. }
  640. }
  641. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  642. union wps_event_data *data)
  643. {
  644. struct wpa_supplicant *wpa_s = ctx;
  645. switch (event) {
  646. case WPS_EV_M2D:
  647. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  648. break;
  649. case WPS_EV_FAIL:
  650. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  651. break;
  652. case WPS_EV_SUCCESS:
  653. wpa_supplicant_wps_event_success(wpa_s);
  654. break;
  655. case WPS_EV_PWD_AUTH_FAIL:
  656. #ifdef CONFIG_AP
  657. if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
  658. wpa_supplicant_ap_pwd_auth_fail(wpa_s);
  659. #endif /* CONFIG_AP */
  660. break;
  661. case WPS_EV_PBC_OVERLAP:
  662. break;
  663. case WPS_EV_PBC_TIMEOUT:
  664. break;
  665. case WPS_EV_PBC_ACTIVE:
  666. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
  667. break;
  668. case WPS_EV_PBC_DISABLE:
  669. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
  670. break;
  671. case WPS_EV_ER_AP_ADD:
  672. wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
  673. break;
  674. case WPS_EV_ER_AP_REMOVE:
  675. wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
  676. break;
  677. case WPS_EV_ER_ENROLLEE_ADD:
  678. wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
  679. &data->enrollee);
  680. break;
  681. case WPS_EV_ER_ENROLLEE_REMOVE:
  682. wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
  683. &data->enrollee);
  684. break;
  685. case WPS_EV_ER_AP_SETTINGS:
  686. wpa_supplicant_wps_event_er_ap_settings(wpa_s,
  687. &data->ap_settings);
  688. break;
  689. case WPS_EV_ER_SET_SELECTED_REGISTRAR:
  690. wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
  691. &data->set_sel_reg);
  692. break;
  693. case WPS_EV_AP_PIN_SUCCESS:
  694. break;
  695. }
  696. }
  697. static int wpa_supplicant_wps_rf_band(void *ctx)
  698. {
  699. struct wpa_supplicant *wpa_s = ctx;
  700. if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
  701. return 0;
  702. return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
  703. }
  704. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  705. {
  706. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  707. eap_is_wps_pin_enrollee(&ssid->eap))
  708. return WPS_REQ_ENROLLEE;
  709. else
  710. return WPS_REQ_REGISTRAR;
  711. }
  712. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  713. {
  714. int id;
  715. struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
  716. wpa_s->after_wps = 0;
  717. wpa_s->known_wps_freq = 0;
  718. prev_current = wpa_s->current_ssid;
  719. /* Enable the networks disabled during wpas_wps_reassoc */
  720. wpas_wps_reenable_networks(wpa_s);
  721. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  722. eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
  723. /* Remove any existing WPS network from configuration */
  724. ssid = wpa_s->conf->ssid;
  725. while (ssid) {
  726. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  727. if (ssid == wpa_s->current_ssid) {
  728. wpa_supplicant_deauthenticate(
  729. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  730. }
  731. id = ssid->id;
  732. remove_ssid = ssid;
  733. } else
  734. id = -1;
  735. ssid = ssid->next;
  736. if (id >= 0) {
  737. if (prev_current == remove_ssid) {
  738. wpa_sm_set_config(wpa_s->wpa, NULL);
  739. eapol_sm_notify_config(wpa_s->eapol, NULL,
  740. NULL);
  741. }
  742. wpas_notify_network_removed(wpa_s, remove_ssid);
  743. wpa_config_remove_network(wpa_s->conf, id);
  744. }
  745. }
  746. wpas_wps_clear_ap_info(wpa_s);
  747. }
  748. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  749. {
  750. struct wpa_supplicant *wpa_s = eloop_ctx;
  751. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  752. "out");
  753. wpas_clear_wps(wpa_s);
  754. }
  755. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  756. int registrar, const u8 *dev_addr,
  757. const u8 *bssid)
  758. {
  759. struct wpa_ssid *ssid;
  760. ssid = wpa_config_add_network(wpa_s->conf);
  761. if (ssid == NULL)
  762. return NULL;
  763. wpas_notify_network_added(wpa_s, ssid);
  764. wpa_config_set_network_defaults(ssid);
  765. ssid->temporary = 1;
  766. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  767. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  768. wpa_config_set(ssid, "identity", registrar ?
  769. "\"" WSC_ID_REGISTRAR "\"" :
  770. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  771. wpas_notify_network_removed(wpa_s, ssid);
  772. wpa_config_remove_network(wpa_s->conf, ssid->id);
  773. return NULL;
  774. }
  775. #ifdef CONFIG_P2P
  776. if (dev_addr)
  777. os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
  778. #endif /* CONFIG_P2P */
  779. if (bssid) {
  780. #ifndef CONFIG_P2P
  781. struct wpa_bss *bss;
  782. int count = 0;
  783. #endif /* CONFIG_P2P */
  784. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  785. ssid->bssid_set = 1;
  786. /*
  787. * Note: With P2P, the SSID may change at the time the WPS
  788. * provisioning is started, so better not filter the AP based
  789. * on the current SSID in the scan results.
  790. */
  791. #ifndef CONFIG_P2P
  792. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  793. if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
  794. continue;
  795. os_free(ssid->ssid);
  796. ssid->ssid = os_malloc(bss->ssid_len);
  797. if (ssid->ssid == NULL)
  798. break;
  799. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  800. ssid->ssid_len = bss->ssid_len;
  801. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
  802. "scan results",
  803. ssid->ssid, ssid->ssid_len);
  804. count++;
  805. }
  806. if (count > 1) {
  807. wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
  808. "for the AP; use wildcard");
  809. os_free(ssid->ssid);
  810. ssid->ssid = NULL;
  811. ssid->ssid_len = 0;
  812. }
  813. #endif /* CONFIG_P2P */
  814. }
  815. return ssid;
  816. }
  817. static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
  818. struct wpa_ssid *selected)
  819. {
  820. struct wpa_ssid *ssid;
  821. if (wpa_s->current_ssid)
  822. wpa_supplicant_deauthenticate(
  823. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  824. /* Mark all other networks disabled and trigger reassociation */
  825. ssid = wpa_s->conf->ssid;
  826. while (ssid) {
  827. int was_disabled = ssid->disabled;
  828. ssid->disabled_for_connect = 0;
  829. /*
  830. * In case the network object corresponds to a persistent group
  831. * then do not send out network disabled signal. In addition,
  832. * do not change disabled status of persistent network objects
  833. * from 2 to 1 should we connect to another network.
  834. */
  835. if (was_disabled != 2) {
  836. ssid->disabled = ssid != selected;
  837. if (was_disabled != ssid->disabled) {
  838. if (ssid->disabled)
  839. ssid->disabled_for_connect = 1;
  840. wpas_notify_network_enabled_changed(wpa_s,
  841. ssid);
  842. }
  843. }
  844. ssid = ssid->next;
  845. }
  846. }
  847. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  848. struct wpa_ssid *selected, const u8 *bssid,
  849. int freq)
  850. {
  851. struct wpa_bss *bss;
  852. wpa_s->after_wps = 0;
  853. wpa_s->known_wps_freq = 0;
  854. if (freq) {
  855. wpa_s->after_wps = 5;
  856. wpa_s->wps_freq = freq;
  857. } else if (bssid) {
  858. bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
  859. if (bss && bss->freq > 0) {
  860. wpa_s->known_wps_freq = 1;
  861. wpa_s->wps_freq = bss->freq;
  862. }
  863. }
  864. wpas_wps_temp_disable(wpa_s, selected);
  865. wpa_s->disconnected = 0;
  866. wpa_s->reassociate = 1;
  867. wpa_s->scan_runs = 0;
  868. wpa_s->normal_scans = 0;
  869. wpa_s->wps_success = 0;
  870. wpa_s->blacklist_cleared = 0;
  871. wpa_supplicant_cancel_sched_scan(wpa_s);
  872. wpa_supplicant_req_scan(wpa_s, 0, 0);
  873. }
  874. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  875. int p2p_group)
  876. {
  877. struct wpa_ssid *ssid;
  878. wpas_clear_wps(wpa_s);
  879. ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
  880. if (ssid == NULL)
  881. return -1;
  882. ssid->temporary = 1;
  883. ssid->p2p_group = p2p_group;
  884. #ifdef CONFIG_P2P
  885. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  886. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  887. if (ssid->ssid) {
  888. ssid->ssid_len = wpa_s->go_params->ssid_len;
  889. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  890. ssid->ssid_len);
  891. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  892. "SSID", ssid->ssid, ssid->ssid_len);
  893. }
  894. }
  895. #endif /* CONFIG_P2P */
  896. if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
  897. return -1;
  898. if (wpa_s->wps_fragment_size)
  899. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  900. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  901. wpa_s, NULL);
  902. wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
  903. return 0;
  904. }
  905. static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
  906. const u8 *dev_addr, const u8 *bssid,
  907. const char *pin, int p2p_group, u16 dev_pw_id,
  908. const u8 *peer_pubkey_hash,
  909. const u8 *ssid_val, size_t ssid_len, int freq)
  910. {
  911. struct wpa_ssid *ssid;
  912. char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
  913. unsigned int rpin = 0;
  914. char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
  915. wpas_clear_wps(wpa_s);
  916. if (bssid && is_zero_ether_addr(bssid))
  917. bssid = NULL;
  918. ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
  919. if (ssid == NULL) {
  920. wpa_printf(MSG_DEBUG, "WPS: Could not add network");
  921. return -1;
  922. }
  923. ssid->temporary = 1;
  924. ssid->p2p_group = p2p_group;
  925. if (ssid_val) {
  926. ssid->ssid = os_malloc(ssid_len);
  927. if (ssid->ssid) {
  928. os_memcpy(ssid->ssid, ssid_val, ssid_len);
  929. ssid->ssid_len = ssid_len;
  930. }
  931. }
  932. if (peer_pubkey_hash) {
  933. os_memcpy(hash, " pkhash=", 8);
  934. wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
  935. peer_pubkey_hash,
  936. WPS_OOB_PUBKEY_HASH_LEN);
  937. } else {
  938. hash[0] = '\0';
  939. }
  940. #ifdef CONFIG_P2P
  941. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  942. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  943. if (ssid->ssid) {
  944. ssid->ssid_len = wpa_s->go_params->ssid_len;
  945. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  946. ssid->ssid_len);
  947. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  948. "SSID", ssid->ssid, ssid->ssid_len);
  949. }
  950. }
  951. #endif /* CONFIG_P2P */
  952. if (pin)
  953. os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
  954. pin, dev_pw_id, hash);
  955. else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
  956. os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
  957. dev_pw_id, hash);
  958. } else {
  959. rpin = wps_generate_pin();
  960. os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
  961. rpin, dev_pw_id, hash);
  962. }
  963. if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
  964. wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
  965. return -1;
  966. }
  967. if (wpa_s->wps_fragment_size)
  968. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  969. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  970. wpa_s, NULL);
  971. wpa_s->wps_ap_iter = 1;
  972. wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
  973. return rpin;
  974. }
  975. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  976. const char *pin, int p2p_group, u16 dev_pw_id)
  977. {
  978. return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
  979. dev_pw_id, NULL, NULL, 0, 0);
  980. }
  981. /* Cancel the wps pbc/pin requests */
  982. int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
  983. {
  984. #ifdef CONFIG_AP
  985. if (wpa_s->ap_iface) {
  986. wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
  987. return wpa_supplicant_ap_wps_cancel(wpa_s);
  988. }
  989. #endif /* CONFIG_AP */
  990. if (wpa_s->wpa_state == WPA_SCANNING ||
  991. wpa_s->wpa_state == WPA_DISCONNECTED) {
  992. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
  993. wpa_supplicant_cancel_scan(wpa_s);
  994. wpas_clear_wps(wpa_s);
  995. } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  996. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
  997. "deauthenticate");
  998. wpa_supplicant_deauthenticate(wpa_s,
  999. WLAN_REASON_DEAUTH_LEAVING);
  1000. wpas_clear_wps(wpa_s);
  1001. } else {
  1002. wpas_wps_reenable_networks(wpa_s);
  1003. wpas_wps_clear_ap_info(wpa_s);
  1004. if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
  1005. 0)
  1006. wpas_clear_wps(wpa_s);
  1007. }
  1008. wpa_s->after_wps = 0;
  1009. return 0;
  1010. }
  1011. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  1012. const char *pin, struct wps_new_ap_settings *settings)
  1013. {
  1014. struct wpa_ssid *ssid;
  1015. char val[200];
  1016. char *pos, *end;
  1017. int res;
  1018. if (!pin)
  1019. return -1;
  1020. wpas_clear_wps(wpa_s);
  1021. ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
  1022. if (ssid == NULL)
  1023. return -1;
  1024. ssid->temporary = 1;
  1025. pos = val;
  1026. end = pos + sizeof(val);
  1027. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  1028. if (res < 0 || res >= end - pos)
  1029. return -1;
  1030. pos += res;
  1031. if (settings) {
  1032. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  1033. "new_encr=%s new_key=%s",
  1034. settings->ssid_hex, settings->auth,
  1035. settings->encr, settings->key_hex);
  1036. if (res < 0 || res >= end - pos)
  1037. return -1;
  1038. pos += res;
  1039. }
  1040. res = os_snprintf(pos, end - pos, "\"");
  1041. if (res < 0 || res >= end - pos)
  1042. return -1;
  1043. if (wpa_config_set(ssid, "phase1", val, 0) < 0)
  1044. return -1;
  1045. if (wpa_s->wps_fragment_size)
  1046. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  1047. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  1048. wpa_s, NULL);
  1049. wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
  1050. return 0;
  1051. }
  1052. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
  1053. const u8 *p2p_dev_addr, const u8 *psk,
  1054. size_t psk_len)
  1055. {
  1056. if (is_zero_ether_addr(p2p_dev_addr)) {
  1057. wpa_printf(MSG_DEBUG,
  1058. "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
  1059. MAC2STR(mac_addr));
  1060. } else {
  1061. wpa_printf(MSG_DEBUG,
  1062. "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
  1063. " P2P Device Addr " MACSTR,
  1064. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  1065. }
  1066. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  1067. /* TODO */
  1068. return 0;
  1069. }
  1070. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  1071. const struct wps_device_data *dev)
  1072. {
  1073. char uuid[40], txt[400];
  1074. int len;
  1075. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1076. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  1077. return;
  1078. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  1079. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  1080. " [%s|%s|%s|%s|%s|%s]",
  1081. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  1082. dev->manufacturer, dev->model_name,
  1083. dev->model_number, dev->serial_number,
  1084. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  1085. sizeof(devtype)));
  1086. if (len > 0 && len < (int) sizeof(txt))
  1087. wpa_printf(MSG_INFO, "%s", txt);
  1088. }
  1089. static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
  1090. u16 sel_reg_config_methods)
  1091. {
  1092. #ifdef CONFIG_WPS_ER
  1093. struct wpa_supplicant *wpa_s = ctx;
  1094. if (wpa_s->wps_er == NULL)
  1095. return;
  1096. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
  1097. "dev_password_id=%u sel_reg_config_methods=0x%x",
  1098. sel_reg, dev_passwd_id, sel_reg_config_methods);
  1099. wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
  1100. sel_reg_config_methods);
  1101. #endif /* CONFIG_WPS_ER */
  1102. }
  1103. static u16 wps_fix_config_methods(u16 config_methods)
  1104. {
  1105. if ((config_methods &
  1106. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  1107. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  1108. wpa_printf(MSG_INFO, "WPS: Converting display to "
  1109. "virtual_display for WPS 2.0 compliance");
  1110. config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  1111. }
  1112. if ((config_methods &
  1113. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  1114. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  1115. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  1116. "virtual_push_button for WPS 2.0 compliance");
  1117. config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  1118. }
  1119. return config_methods;
  1120. }
  1121. static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
  1122. struct wps_context *wps)
  1123. {
  1124. char buf[50];
  1125. const char *src;
  1126. if (is_nil_uuid(wpa_s->conf->uuid)) {
  1127. struct wpa_supplicant *first;
  1128. first = wpa_s->global->ifaces;
  1129. while (first && first->next)
  1130. first = first->next;
  1131. if (first && first != wpa_s) {
  1132. if (wps != wpa_s->global->ifaces->wps)
  1133. os_memcpy(wps->uuid,
  1134. wpa_s->global->ifaces->wps->uuid,
  1135. WPS_UUID_LEN);
  1136. src = "from the first interface";
  1137. } else {
  1138. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  1139. src = "based on MAC address";
  1140. }
  1141. } else {
  1142. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  1143. src = "based on configuration";
  1144. }
  1145. uuid_bin2str(wps->uuid, buf, sizeof(buf));
  1146. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
  1147. }
  1148. static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
  1149. struct wps_context *wps)
  1150. {
  1151. wpabuf_free(wps->dev.vendor_ext_m1);
  1152. wps->dev.vendor_ext_m1 = NULL;
  1153. if (wpa_s->conf->wps_vendor_ext_m1) {
  1154. wps->dev.vendor_ext_m1 =
  1155. wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
  1156. if (!wps->dev.vendor_ext_m1) {
  1157. wpa_printf(MSG_ERROR, "WPS: Cannot "
  1158. "allocate memory for vendor_ext_m1");
  1159. }
  1160. }
  1161. }
  1162. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  1163. {
  1164. struct wps_context *wps;
  1165. struct wps_registrar_config rcfg;
  1166. struct hostapd_hw_modes *modes;
  1167. u16 m;
  1168. wps = os_zalloc(sizeof(*wps));
  1169. if (wps == NULL)
  1170. return -1;
  1171. wps->cred_cb = wpa_supplicant_wps_cred;
  1172. wps->event_cb = wpa_supplicant_wps_event;
  1173. wps->rf_band_cb = wpa_supplicant_wps_rf_band;
  1174. wps->cb_ctx = wpa_s;
  1175. wps->dev.device_name = wpa_s->conf->device_name;
  1176. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1177. wps->dev.model_name = wpa_s->conf->model_name;
  1178. wps->dev.model_number = wpa_s->conf->model_number;
  1179. wps->dev.serial_number = wpa_s->conf->serial_number;
  1180. wps->config_methods =
  1181. wps_config_methods_str2bin(wpa_s->conf->config_methods);
  1182. if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1183. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1184. wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
  1185. "methods are not allowed at the same time");
  1186. os_free(wps);
  1187. return -1;
  1188. }
  1189. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1190. wps->dev.config_methods = wps->config_methods;
  1191. os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
  1192. WPS_DEV_TYPE_LEN);
  1193. wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1194. os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
  1195. WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
  1196. wpas_wps_set_vendor_ext_m1(wpa_s, wps);
  1197. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1198. modes = wpa_s->hw.modes;
  1199. if (modes) {
  1200. for (m = 0; m < wpa_s->hw.num_modes; m++) {
  1201. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
  1202. modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  1203. wps->dev.rf_bands |= WPS_RF_24GHZ;
  1204. else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
  1205. wps->dev.rf_bands |= WPS_RF_50GHZ;
  1206. }
  1207. }
  1208. if (wps->dev.rf_bands == 0) {
  1209. /*
  1210. * Default to claiming support for both bands if the driver
  1211. * does not provide support for fetching supported bands.
  1212. */
  1213. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
  1214. }
  1215. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  1216. wpas_wps_set_uuid(wpa_s, wps);
  1217. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  1218. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  1219. os_memset(&rcfg, 0, sizeof(rcfg));
  1220. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  1221. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  1222. rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
  1223. rcfg.cb_ctx = wpa_s;
  1224. wps->registrar = wps_registrar_init(wps, &rcfg);
  1225. if (wps->registrar == NULL) {
  1226. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  1227. os_free(wps);
  1228. return -1;
  1229. }
  1230. wpa_s->wps = wps;
  1231. return 0;
  1232. }
  1233. #ifdef CONFIG_WPS_ER
  1234. static void wpas_wps_nfc_clear(struct wps_context *wps)
  1235. {
  1236. wps->ap_nfc_dev_pw_id = 0;
  1237. wpabuf_free(wps->ap_nfc_dh_pubkey);
  1238. wps->ap_nfc_dh_pubkey = NULL;
  1239. wpabuf_free(wps->ap_nfc_dh_privkey);
  1240. wps->ap_nfc_dh_privkey = NULL;
  1241. wpabuf_free(wps->ap_nfc_dev_pw);
  1242. wps->ap_nfc_dev_pw = NULL;
  1243. }
  1244. #endif /* CONFIG_WPS_ER */
  1245. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  1246. {
  1247. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  1248. eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
  1249. eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
  1250. wpas_wps_clear_ap_info(wpa_s);
  1251. if (wpa_s->wps == NULL)
  1252. return;
  1253. #ifdef CONFIG_WPS_ER
  1254. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1255. wpa_s->wps_er = NULL;
  1256. wpas_wps_nfc_clear(wpa_s->wps);
  1257. #endif /* CONFIG_WPS_ER */
  1258. wps_registrar_deinit(wpa_s->wps->registrar);
  1259. wpabuf_free(wpa_s->wps->dh_pubkey);
  1260. wpabuf_free(wpa_s->wps->dh_privkey);
  1261. wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
  1262. os_free(wpa_s->wps->network_key);
  1263. os_free(wpa_s->wps);
  1264. wpa_s->wps = NULL;
  1265. }
  1266. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  1267. struct wpa_ssid *ssid, struct wpa_bss *bss)
  1268. {
  1269. struct wpabuf *wps_ie;
  1270. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  1271. return -1;
  1272. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1273. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1274. if (!wps_ie) {
  1275. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1276. return 0;
  1277. }
  1278. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  1279. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1280. "without active PBC Registrar");
  1281. wpabuf_free(wps_ie);
  1282. return 0;
  1283. }
  1284. /* TODO: overlap detection */
  1285. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1286. "(Active PBC)");
  1287. wpabuf_free(wps_ie);
  1288. return 1;
  1289. }
  1290. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1291. if (!wps_ie) {
  1292. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1293. return 0;
  1294. }
  1295. /*
  1296. * Start with WPS APs that advertise our address as an
  1297. * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
  1298. * allow any WPS AP after couple of scans since some APs do not
  1299. * set Selected Registrar attribute properly when using
  1300. * external Registrar.
  1301. */
  1302. if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
  1303. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  1304. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1305. "without active PIN Registrar");
  1306. wpabuf_free(wps_ie);
  1307. return 0;
  1308. }
  1309. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1310. } else {
  1311. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1312. "(Authorized MAC or Active PIN)");
  1313. }
  1314. wpabuf_free(wps_ie);
  1315. return 1;
  1316. }
  1317. if (wps_ie) {
  1318. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1319. wpabuf_free(wps_ie);
  1320. return 1;
  1321. }
  1322. return -1;
  1323. }
  1324. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  1325. struct wpa_ssid *ssid,
  1326. struct wpa_bss *bss)
  1327. {
  1328. struct wpabuf *wps_ie = NULL;
  1329. int ret = 0;
  1330. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1331. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1332. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  1333. /* allow wildcard SSID for WPS PBC */
  1334. ret = 1;
  1335. }
  1336. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1337. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1338. if (wps_ie &&
  1339. (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
  1340. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  1341. /* allow wildcard SSID for WPS PIN */
  1342. ret = 1;
  1343. }
  1344. }
  1345. if (!ret && ssid->bssid_set &&
  1346. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  1347. /* allow wildcard SSID due to hardcoded BSSID match */
  1348. ret = 1;
  1349. }
  1350. #ifdef CONFIG_WPS_STRICT
  1351. if (wps_ie) {
  1352. if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
  1353. 0, bss->bssid) < 0)
  1354. ret = 0;
  1355. if (bss->beacon_ie_len) {
  1356. struct wpabuf *bcn_wps;
  1357. bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
  1358. bss, WPS_IE_VENDOR_TYPE);
  1359. if (bcn_wps == NULL) {
  1360. wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
  1361. "missing from AP Beacon");
  1362. ret = 0;
  1363. } else {
  1364. if (wps_validate_beacon(wps_ie) < 0)
  1365. ret = 0;
  1366. wpabuf_free(bcn_wps);
  1367. }
  1368. }
  1369. }
  1370. #endif /* CONFIG_WPS_STRICT */
  1371. wpabuf_free(wps_ie);
  1372. return ret;
  1373. }
  1374. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  1375. struct wpa_bss *selected, struct wpa_ssid *ssid)
  1376. {
  1377. const u8 *sel_uuid, *uuid;
  1378. struct wpabuf *wps_ie;
  1379. int ret = 0;
  1380. struct wpa_bss *bss;
  1381. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  1382. return 0;
  1383. wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
  1384. "present in scan results; selected BSSID " MACSTR,
  1385. MAC2STR(selected->bssid));
  1386. /* Make sure that only one AP is in active PBC mode */
  1387. wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  1388. if (wps_ie) {
  1389. sel_uuid = wps_get_uuid_e(wps_ie);
  1390. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
  1391. sel_uuid, UUID_LEN);
  1392. } else {
  1393. wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
  1394. "WPS IE?!");
  1395. sel_uuid = NULL;
  1396. }
  1397. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1398. struct wpabuf *ie;
  1399. if (bss == selected)
  1400. continue;
  1401. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1402. if (!ie)
  1403. continue;
  1404. if (!wps_is_selected_pbc_registrar(ie)) {
  1405. wpabuf_free(ie);
  1406. continue;
  1407. }
  1408. wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
  1409. MACSTR, MAC2STR(bss->bssid));
  1410. uuid = wps_get_uuid_e(ie);
  1411. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
  1412. uuid, UUID_LEN);
  1413. if (sel_uuid == NULL || uuid == NULL ||
  1414. os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
  1415. ret = 1; /* PBC overlap */
  1416. wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
  1417. MACSTR " and " MACSTR,
  1418. MAC2STR(selected->bssid),
  1419. MAC2STR(bss->bssid));
  1420. wpabuf_free(ie);
  1421. break;
  1422. }
  1423. /* TODO: verify that this is reasonable dual-band situation */
  1424. wpabuf_free(ie);
  1425. }
  1426. wpabuf_free(wps_ie);
  1427. return ret;
  1428. }
  1429. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  1430. {
  1431. struct wpa_bss *bss;
  1432. unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
  1433. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  1434. return;
  1435. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1436. struct wpabuf *ie;
  1437. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1438. if (!ie)
  1439. continue;
  1440. if (wps_is_selected_pbc_registrar(ie))
  1441. pbc++;
  1442. else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
  1443. auth++;
  1444. else if (wps_is_selected_pin_registrar(ie))
  1445. pin++;
  1446. else
  1447. wps++;
  1448. wpabuf_free(ie);
  1449. }
  1450. if (pbc)
  1451. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  1452. else if (auth)
  1453. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
  1454. else if (pin)
  1455. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  1456. else if (wps)
  1457. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  1458. }
  1459. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  1460. {
  1461. struct wpa_ssid *ssid;
  1462. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1463. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  1464. return 1;
  1465. }
  1466. return 0;
  1467. }
  1468. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  1469. char *end)
  1470. {
  1471. struct wpabuf *wps_ie;
  1472. int ret;
  1473. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  1474. if (wps_ie == NULL)
  1475. return 0;
  1476. ret = wps_attr_text(wps_ie, buf, end);
  1477. wpabuf_free(wps_ie);
  1478. return ret;
  1479. }
  1480. int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
  1481. {
  1482. #ifdef CONFIG_WPS_ER
  1483. if (wpa_s->wps_er) {
  1484. wps_er_refresh(wpa_s->wps_er);
  1485. return 0;
  1486. }
  1487. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
  1488. if (wpa_s->wps_er == NULL)
  1489. return -1;
  1490. return 0;
  1491. #else /* CONFIG_WPS_ER */
  1492. return 0;
  1493. #endif /* CONFIG_WPS_ER */
  1494. }
  1495. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  1496. {
  1497. #ifdef CONFIG_WPS_ER
  1498. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1499. wpa_s->wps_er = NULL;
  1500. #endif /* CONFIG_WPS_ER */
  1501. return 0;
  1502. }
  1503. #ifdef CONFIG_WPS_ER
  1504. int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
  1505. const char *uuid, const char *pin)
  1506. {
  1507. u8 u[UUID_LEN];
  1508. const u8 *use_uuid = NULL;
  1509. u8 addr_buf[ETH_ALEN];
  1510. if (os_strcmp(uuid, "any") == 0) {
  1511. } else if (uuid_str2bin(uuid, u) == 0) {
  1512. use_uuid = u;
  1513. } else if (hwaddr_aton(uuid, addr_buf) == 0) {
  1514. use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
  1515. if (use_uuid == NULL)
  1516. return -1;
  1517. } else
  1518. return -1;
  1519. return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
  1520. use_uuid,
  1521. (const u8 *) pin, os_strlen(pin), 300);
  1522. }
  1523. int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
  1524. {
  1525. u8 u[UUID_LEN], *use_uuid = NULL;
  1526. u8 addr[ETH_ALEN], *use_addr = NULL;
  1527. if (uuid_str2bin(uuid, u) == 0)
  1528. use_uuid = u;
  1529. else if (hwaddr_aton(uuid, addr) == 0)
  1530. use_addr = addr;
  1531. else
  1532. return -1;
  1533. return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
  1534. }
  1535. int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
  1536. const char *pin)
  1537. {
  1538. u8 u[UUID_LEN], *use_uuid = NULL;
  1539. u8 addr[ETH_ALEN], *use_addr = NULL;
  1540. if (uuid_str2bin(uuid, u) == 0)
  1541. use_uuid = u;
  1542. else if (hwaddr_aton(uuid, addr) == 0)
  1543. use_addr = addr;
  1544. else
  1545. return -1;
  1546. return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
  1547. os_strlen(pin));
  1548. }
  1549. static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
  1550. struct wps_credential *cred)
  1551. {
  1552. os_memset(cred, 0, sizeof(*cred));
  1553. if (ssid->ssid_len > 32)
  1554. return -1;
  1555. os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
  1556. cred->ssid_len = ssid->ssid_len;
  1557. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  1558. cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
  1559. WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
  1560. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  1561. cred->encr_type = WPS_ENCR_AES;
  1562. else
  1563. cred->encr_type = WPS_ENCR_TKIP;
  1564. if (ssid->passphrase) {
  1565. cred->key_len = os_strlen(ssid->passphrase);
  1566. if (cred->key_len >= 64)
  1567. return -1;
  1568. os_memcpy(cred->key, ssid->passphrase, cred->key_len);
  1569. } else if (ssid->psk_set) {
  1570. cred->key_len = 32;
  1571. os_memcpy(cred->key, ssid->psk, 32);
  1572. } else
  1573. return -1;
  1574. } else {
  1575. cred->auth_type = WPS_AUTH_OPEN;
  1576. cred->encr_type = WPS_ENCR_NONE;
  1577. }
  1578. return 0;
  1579. }
  1580. int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1581. int id)
  1582. {
  1583. u8 u[UUID_LEN], *use_uuid = NULL;
  1584. u8 addr[ETH_ALEN], *use_addr = NULL;
  1585. struct wpa_ssid *ssid;
  1586. struct wps_credential cred;
  1587. if (uuid_str2bin(uuid, u) == 0)
  1588. use_uuid = u;
  1589. else if (hwaddr_aton(uuid, addr) == 0)
  1590. use_addr = addr;
  1591. else
  1592. return -1;
  1593. ssid = wpa_config_get_network(wpa_s->conf, id);
  1594. if (ssid == NULL || ssid->ssid == NULL)
  1595. return -1;
  1596. if (wpas_wps_network_to_cred(ssid, &cred) < 0)
  1597. return -1;
  1598. return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
  1599. }
  1600. int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1601. const char *pin, struct wps_new_ap_settings *settings)
  1602. {
  1603. u8 u[UUID_LEN], *use_uuid = NULL;
  1604. u8 addr[ETH_ALEN], *use_addr = NULL;
  1605. struct wps_credential cred;
  1606. size_t len;
  1607. if (uuid_str2bin(uuid, u) == 0)
  1608. use_uuid = u;
  1609. else if (hwaddr_aton(uuid, addr) == 0)
  1610. use_addr = addr;
  1611. else
  1612. return -1;
  1613. if (settings->ssid_hex == NULL || settings->auth == NULL ||
  1614. settings->encr == NULL || settings->key_hex == NULL)
  1615. return -1;
  1616. os_memset(&cred, 0, sizeof(cred));
  1617. len = os_strlen(settings->ssid_hex);
  1618. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1619. hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
  1620. return -1;
  1621. cred.ssid_len = len / 2;
  1622. len = os_strlen(settings->key_hex);
  1623. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1624. hexstr2bin(settings->key_hex, cred.key, len / 2))
  1625. return -1;
  1626. cred.key_len = len / 2;
  1627. if (os_strcmp(settings->auth, "OPEN") == 0)
  1628. cred.auth_type = WPS_AUTH_OPEN;
  1629. else if (os_strcmp(settings->auth, "WPAPSK") == 0)
  1630. cred.auth_type = WPS_AUTH_WPAPSK;
  1631. else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
  1632. cred.auth_type = WPS_AUTH_WPA2PSK;
  1633. else
  1634. return -1;
  1635. if (os_strcmp(settings->encr, "NONE") == 0)
  1636. cred.encr_type = WPS_ENCR_NONE;
  1637. #ifdef CONFIG_TESTING_OPTIONS
  1638. else if (os_strcmp(settings->encr, "WEP") == 0)
  1639. cred.encr_type = WPS_ENCR_WEP;
  1640. #endif /* CONFIG_TESTING_OPTIONS */
  1641. else if (os_strcmp(settings->encr, "TKIP") == 0)
  1642. cred.encr_type = WPS_ENCR_TKIP;
  1643. else if (os_strcmp(settings->encr, "CCMP") == 0)
  1644. cred.encr_type = WPS_ENCR_AES;
  1645. else
  1646. return -1;
  1647. return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
  1648. (const u8 *) pin, os_strlen(pin), &cred);
  1649. }
  1650. #ifdef CONFIG_WPS_NFC
  1651. struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
  1652. int ndef, const char *uuid)
  1653. {
  1654. struct wpabuf *ret;
  1655. u8 u[UUID_LEN], *use_uuid = NULL;
  1656. u8 addr[ETH_ALEN], *use_addr = NULL;
  1657. if (!wpa_s->wps_er)
  1658. return NULL;
  1659. if (uuid_str2bin(uuid, u) == 0)
  1660. use_uuid = u;
  1661. else if (hwaddr_aton(uuid, addr) == 0)
  1662. use_addr = addr;
  1663. else
  1664. return NULL;
  1665. ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
  1666. if (ndef && ret) {
  1667. struct wpabuf *tmp;
  1668. tmp = ndef_build_wifi(ret);
  1669. wpabuf_free(ret);
  1670. if (tmp == NULL)
  1671. return NULL;
  1672. ret = tmp;
  1673. }
  1674. return ret;
  1675. }
  1676. #endif /* CONFIG_WPS_NFC */
  1677. static int callbacks_pending = 0;
  1678. static void wpas_wps_terminate_cb(void *ctx)
  1679. {
  1680. wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
  1681. if (--callbacks_pending <= 0)
  1682. eloop_terminate();
  1683. }
  1684. #endif /* CONFIG_WPS_ER */
  1685. int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
  1686. {
  1687. #ifdef CONFIG_WPS_ER
  1688. if (wpa_s->wps_er) {
  1689. callbacks_pending++;
  1690. wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
  1691. wpa_s->wps_er = NULL;
  1692. return 1;
  1693. }
  1694. #endif /* CONFIG_WPS_ER */
  1695. return 0;
  1696. }
  1697. void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
  1698. {
  1699. struct wps_context *wps = wpa_s->wps;
  1700. if (wps == NULL)
  1701. return;
  1702. if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
  1703. wps->config_methods = wps_config_methods_str2bin(
  1704. wpa_s->conf->config_methods);
  1705. if ((wps->config_methods &
  1706. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1707. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1708. wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
  1709. "config methods are not allowed at the "
  1710. "same time");
  1711. wps->config_methods &= ~WPS_CONFIG_LABEL;
  1712. }
  1713. }
  1714. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1715. wps->dev.config_methods = wps->config_methods;
  1716. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  1717. os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
  1718. WPS_DEV_TYPE_LEN);
  1719. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
  1720. wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1721. os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
  1722. wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  1723. }
  1724. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
  1725. wpas_wps_set_vendor_ext_m1(wpa_s, wps);
  1726. if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
  1727. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1728. if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
  1729. wpas_wps_set_uuid(wpa_s, wps);
  1730. if (wpa_s->conf->changed_parameters &
  1731. (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
  1732. /* Update pointers to make sure they refer current values */
  1733. wps->dev.device_name = wpa_s->conf->device_name;
  1734. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1735. wps->dev.model_name = wpa_s->conf->model_name;
  1736. wps->dev.model_number = wpa_s->conf->model_number;
  1737. wps->dev.serial_number = wpa_s->conf->serial_number;
  1738. }
  1739. }
  1740. #ifdef CONFIG_WPS_NFC
  1741. #ifdef CONFIG_WPS_ER
  1742. static struct wpabuf *
  1743. wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
  1744. struct wpa_ssid *ssid)
  1745. {
  1746. struct wpabuf *ret;
  1747. struct wps_credential cred;
  1748. if (wpas_wps_network_to_cred(ssid, &cred) < 0)
  1749. return NULL;
  1750. ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
  1751. if (ndef && ret) {
  1752. struct wpabuf *tmp;
  1753. tmp = ndef_build_wifi(ret);
  1754. wpabuf_free(ret);
  1755. if (tmp == NULL)
  1756. return NULL;
  1757. ret = tmp;
  1758. }
  1759. return ret;
  1760. }
  1761. #endif /* CONFIG_WPS_ER */
  1762. struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
  1763. int ndef, const char *id_str)
  1764. {
  1765. #ifdef CONFIG_WPS_ER
  1766. if (id_str) {
  1767. int id;
  1768. char *end = NULL;
  1769. struct wpa_ssid *ssid;
  1770. id = strtol(id_str, &end, 10);
  1771. if (end && *end)
  1772. return NULL;
  1773. ssid = wpa_config_get_network(wpa_s->conf, id);
  1774. if (ssid == NULL)
  1775. return NULL;
  1776. return wpas_wps_network_config_token(wpa_s, ndef, ssid);
  1777. }
  1778. #endif /* CONFIG_WPS_ER */
  1779. #ifdef CONFIG_AP
  1780. if (wpa_s->ap_iface)
  1781. return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
  1782. #endif /* CONFIG_AP */
  1783. return NULL;
  1784. }
  1785. struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
  1786. {
  1787. if (wpa_s->conf->wps_nfc_pw_from_config) {
  1788. return wps_nfc_token_build(ndef,
  1789. wpa_s->conf->wps_nfc_dev_pw_id,
  1790. wpa_s->conf->wps_nfc_dh_pubkey,
  1791. wpa_s->conf->wps_nfc_dev_pw);
  1792. }
  1793. return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
  1794. &wpa_s->conf->wps_nfc_dh_pubkey,
  1795. &wpa_s->conf->wps_nfc_dh_privkey,
  1796. &wpa_s->conf->wps_nfc_dev_pw);
  1797. }
  1798. int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
  1799. const u8 *bssid,
  1800. const struct wpabuf *dev_pw, u16 dev_pw_id,
  1801. int p2p_group, const u8 *peer_pubkey_hash,
  1802. const u8 *ssid, size_t ssid_len, int freq)
  1803. {
  1804. struct wps_context *wps = wpa_s->wps;
  1805. char pw[32 * 2 + 1];
  1806. if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
  1807. dev_pw = wpa_s->conf->wps_nfc_dev_pw;
  1808. dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  1809. }
  1810. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
  1811. wpa_s->conf->wps_nfc_dh_privkey == NULL) {
  1812. wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
  1813. "cannot start NFC-triggered connection");
  1814. return -1;
  1815. }
  1816. if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
  1817. wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
  1818. "cannot start NFC-triggered connection", dev_pw_id);
  1819. return -1;
  1820. }
  1821. dh5_free(wps->dh_ctx);
  1822. wpabuf_free(wps->dh_pubkey);
  1823. wpabuf_free(wps->dh_privkey);
  1824. wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  1825. wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  1826. if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
  1827. wps->dh_ctx = NULL;
  1828. wpabuf_free(wps->dh_pubkey);
  1829. wps->dh_pubkey = NULL;
  1830. wpabuf_free(wps->dh_privkey);
  1831. wps->dh_privkey = NULL;
  1832. wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
  1833. return -1;
  1834. }
  1835. wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
  1836. if (wps->dh_ctx == NULL) {
  1837. wpabuf_free(wps->dh_pubkey);
  1838. wps->dh_pubkey = NULL;
  1839. wpabuf_free(wps->dh_privkey);
  1840. wps->dh_privkey = NULL;
  1841. wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
  1842. return -1;
  1843. }
  1844. if (dev_pw) {
  1845. wpa_snprintf_hex_uppercase(pw, sizeof(pw),
  1846. wpabuf_head(dev_pw),
  1847. wpabuf_len(dev_pw));
  1848. }
  1849. return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
  1850. dev_pw ? pw : NULL,
  1851. p2p_group, dev_pw_id, peer_pubkey_hash,
  1852. ssid, ssid_len, freq);
  1853. }
  1854. static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
  1855. struct wps_parse_attr *attr)
  1856. {
  1857. /*
  1858. * Disable existing networks temporarily to allow the newly learned
  1859. * credential to be preferred. Enable the temporarily disabled networks
  1860. * after 10 seconds.
  1861. */
  1862. wpas_wps_temp_disable(wpa_s, NULL);
  1863. eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
  1864. NULL);
  1865. if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
  1866. return -1;
  1867. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1868. return 0;
  1869. if (attr->ap_channel) {
  1870. u16 chan = WPA_GET_BE16(attr->ap_channel);
  1871. int freq = 0;
  1872. if (chan >= 1 && chan <= 13)
  1873. freq = 2407 + 5 * chan;
  1874. else if (chan == 14)
  1875. freq = 2484;
  1876. else if (chan >= 30)
  1877. freq = 5000 + 5 * chan;
  1878. if (freq) {
  1879. wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
  1880. chan, freq);
  1881. wpa_s->after_wps = 5;
  1882. wpa_s->wps_freq = freq;
  1883. }
  1884. }
  1885. wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
  1886. "based on the received credential added");
  1887. wpa_s->normal_scans = 0;
  1888. wpa_supplicant_reinit_autoscan(wpa_s);
  1889. wpa_s->disconnected = 0;
  1890. wpa_s->reassociate = 1;
  1891. wpa_supplicant_cancel_sched_scan(wpa_s);
  1892. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1893. return 0;
  1894. }
  1895. #ifdef CONFIG_WPS_ER
  1896. static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
  1897. struct wps_parse_attr *attr)
  1898. {
  1899. return wps_registrar_add_nfc_password_token(
  1900. wpa_s->wps->registrar, attr->oob_dev_password,
  1901. attr->oob_dev_password_len);
  1902. }
  1903. #endif /* CONFIG_WPS_ER */
  1904. static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
  1905. const struct wpabuf *wps)
  1906. {
  1907. struct wps_parse_attr attr;
  1908. wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
  1909. if (wps_parse_msg(wps, &attr)) {
  1910. wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
  1911. return -1;
  1912. }
  1913. if (attr.num_cred)
  1914. return wpas_wps_use_cred(wpa_s, &attr);
  1915. #ifdef CONFIG_WPS_ER
  1916. if (attr.oob_dev_password)
  1917. return wpas_wps_add_nfc_password_token(wpa_s, &attr);
  1918. #endif /* CONFIG_WPS_ER */
  1919. wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
  1920. return -1;
  1921. }
  1922. int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
  1923. const struct wpabuf *data, int forced_freq)
  1924. {
  1925. const struct wpabuf *wps = data;
  1926. struct wpabuf *tmp = NULL;
  1927. int ret;
  1928. if (wpabuf_len(data) < 4)
  1929. return -1;
  1930. if (*wpabuf_head_u8(data) != 0x10) {
  1931. /* Assume this contains full NDEF record */
  1932. tmp = ndef_parse_wifi(data);
  1933. if (tmp == NULL) {
  1934. #ifdef CONFIG_P2P
  1935. tmp = ndef_parse_p2p(data);
  1936. if (tmp) {
  1937. ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
  1938. forced_freq);
  1939. wpabuf_free(tmp);
  1940. return ret;
  1941. }
  1942. #endif /* CONFIG_P2P */
  1943. wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
  1944. return -1;
  1945. }
  1946. wps = tmp;
  1947. }
  1948. ret = wpas_wps_nfc_tag_process(wpa_s, wps);
  1949. wpabuf_free(tmp);
  1950. return ret;
  1951. }
  1952. struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
  1953. int ndef)
  1954. {
  1955. struct wpabuf *ret;
  1956. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  1957. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  1958. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  1959. return NULL;
  1960. ret = wps_build_nfc_handover_req(wpa_s->wps,
  1961. wpa_s->conf->wps_nfc_dh_pubkey);
  1962. if (ndef && ret) {
  1963. struct wpabuf *tmp;
  1964. tmp = ndef_build_wifi(ret);
  1965. wpabuf_free(ret);
  1966. if (tmp == NULL)
  1967. return NULL;
  1968. ret = tmp;
  1969. }
  1970. return ret;
  1971. }
  1972. #ifdef CONFIG_WPS_NFC
  1973. static struct wpabuf *
  1974. wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
  1975. const char *uuid)
  1976. {
  1977. #ifdef CONFIG_WPS_ER
  1978. struct wpabuf *ret;
  1979. u8 u[UUID_LEN], *use_uuid = NULL;
  1980. u8 addr[ETH_ALEN], *use_addr = NULL;
  1981. struct wps_context *wps = wpa_s->wps;
  1982. if (wps == NULL)
  1983. return NULL;
  1984. if (uuid == NULL)
  1985. return NULL;
  1986. if (uuid_str2bin(uuid, u) == 0)
  1987. use_uuid = u;
  1988. else if (hwaddr_aton(uuid, addr) == 0)
  1989. use_addr = addr;
  1990. else
  1991. return NULL;
  1992. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
  1993. if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  1994. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  1995. return NULL;
  1996. }
  1997. wpas_wps_nfc_clear(wps);
  1998. wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
  1999. wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  2000. wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  2001. if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
  2002. wpas_wps_nfc_clear(wps);
  2003. return NULL;
  2004. }
  2005. ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
  2006. use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
  2007. if (ndef && ret) {
  2008. struct wpabuf *tmp;
  2009. tmp = ndef_build_wifi(ret);
  2010. wpabuf_free(ret);
  2011. if (tmp == NULL)
  2012. return NULL;
  2013. ret = tmp;
  2014. }
  2015. return ret;
  2016. #else /* CONFIG_WPS_ER */
  2017. return NULL;
  2018. #endif /* CONFIG_WPS_ER */
  2019. }
  2020. #endif /* CONFIG_WPS_NFC */
  2021. struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  2022. int ndef, int cr, const char *uuid)
  2023. {
  2024. struct wpabuf *ret;
  2025. if (!cr)
  2026. return NULL;
  2027. ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
  2028. if (ret)
  2029. return ret;
  2030. return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
  2031. }
  2032. static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
  2033. const struct wpabuf *data)
  2034. {
  2035. struct wpabuf *wps;
  2036. int ret = -1;
  2037. u16 wsc_len;
  2038. const u8 *pos;
  2039. struct wpabuf msg;
  2040. struct wps_parse_attr attr;
  2041. u16 dev_pw_id;
  2042. const u8 *bssid = NULL;
  2043. int freq = 0;
  2044. wps = ndef_parse_wifi(data);
  2045. if (wps == NULL)
  2046. return -1;
  2047. wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
  2048. "payload from NFC connection handover");
  2049. wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
  2050. if (wpabuf_len(wps) < 2) {
  2051. wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
  2052. "Message");
  2053. goto out;
  2054. }
  2055. pos = wpabuf_head(wps);
  2056. wsc_len = WPA_GET_BE16(pos);
  2057. if (wsc_len > wpabuf_len(wps) - 2) {
  2058. wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
  2059. "in Wi-Fi Handover Select Message", wsc_len);
  2060. goto out;
  2061. }
  2062. pos += 2;
  2063. wpa_hexdump(MSG_DEBUG,
  2064. "WPS: WSC attributes in Wi-Fi Handover Select Message",
  2065. pos, wsc_len);
  2066. if (wsc_len < wpabuf_len(wps) - 2) {
  2067. wpa_hexdump(MSG_DEBUG,
  2068. "WPS: Ignore extra data after WSC attributes",
  2069. pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
  2070. }
  2071. wpabuf_set(&msg, pos, wsc_len);
  2072. ret = wps_parse_msg(&msg, &attr);
  2073. if (ret < 0) {
  2074. wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
  2075. "Wi-Fi Handover Select Message");
  2076. goto out;
  2077. }
  2078. if (attr.oob_dev_password == NULL ||
  2079. attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  2080. wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
  2081. "included in Wi-Fi Handover Select Message");
  2082. ret = -1;
  2083. goto out;
  2084. }
  2085. if (attr.ssid == NULL) {
  2086. wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
  2087. "Select Message");
  2088. ret = -1;
  2089. goto out;
  2090. }
  2091. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
  2092. if (attr.mac_addr) {
  2093. bssid = attr.mac_addr;
  2094. wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
  2095. MAC2STR(bssid));
  2096. }
  2097. if (attr.rf_bands)
  2098. wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
  2099. if (attr.ap_channel) {
  2100. u16 chan = WPA_GET_BE16(attr.ap_channel);
  2101. wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
  2102. if (chan >= 1 && chan <= 13 &&
  2103. (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
  2104. freq = 2407 + 5 * chan;
  2105. else if (chan == 14 &&
  2106. (attr.rf_bands == NULL ||
  2107. *attr.rf_bands & WPS_RF_24GHZ))
  2108. freq = 2484;
  2109. else if (chan >= 30 &&
  2110. (attr.rf_bands == NULL ||
  2111. *attr.rf_bands & WPS_RF_50GHZ))
  2112. freq = 5000 + 5 * chan;
  2113. if (freq) {
  2114. wpa_printf(MSG_DEBUG,
  2115. "WPS: AP indicated channel %u -> %u MHz",
  2116. chan, freq);
  2117. }
  2118. }
  2119. wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
  2120. attr.oob_dev_password, attr.oob_dev_password_len);
  2121. dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
  2122. WPS_OOB_PUBKEY_HASH_LEN);
  2123. if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
  2124. wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
  2125. "%u in Wi-Fi Handover Select Message", dev_pw_id);
  2126. ret = -1;
  2127. goto out;
  2128. }
  2129. wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
  2130. attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
  2131. ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
  2132. attr.oob_dev_password,
  2133. attr.ssid, attr.ssid_len, freq);
  2134. out:
  2135. wpabuf_free(wps);
  2136. return ret;
  2137. }
  2138. int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
  2139. const struct wpabuf *req,
  2140. const struct wpabuf *sel)
  2141. {
  2142. wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
  2143. wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
  2144. wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
  2145. return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
  2146. }
  2147. int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
  2148. const struct wpabuf *req,
  2149. const struct wpabuf *sel)
  2150. {
  2151. struct wpabuf *wps;
  2152. int ret = -1;
  2153. u16 wsc_len;
  2154. const u8 *pos;
  2155. struct wpabuf msg;
  2156. struct wps_parse_attr attr;
  2157. u16 dev_pw_id;
  2158. /*
  2159. * Enrollee/station is always initiator of the NFC connection handover,
  2160. * so use the request message here to find Enrollee public key hash.
  2161. */
  2162. wps = ndef_parse_wifi(req);
  2163. if (wps == NULL)
  2164. return -1;
  2165. wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
  2166. "payload from NFC connection handover");
  2167. wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
  2168. if (wpabuf_len(wps) < 2) {
  2169. wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
  2170. "Message");
  2171. goto out;
  2172. }
  2173. pos = wpabuf_head(wps);
  2174. wsc_len = WPA_GET_BE16(pos);
  2175. if (wsc_len > wpabuf_len(wps) - 2) {
  2176. wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
  2177. "in rt Wi-Fi Handover Request Message", wsc_len);
  2178. goto out;
  2179. }
  2180. pos += 2;
  2181. wpa_hexdump(MSG_DEBUG,
  2182. "WPS: WSC attributes in Wi-Fi Handover Request Message",
  2183. pos, wsc_len);
  2184. if (wsc_len < wpabuf_len(wps) - 2) {
  2185. wpa_hexdump(MSG_DEBUG,
  2186. "WPS: Ignore extra data after WSC attributes",
  2187. pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
  2188. }
  2189. wpabuf_set(&msg, pos, wsc_len);
  2190. ret = wps_parse_msg(&msg, &attr);
  2191. if (ret < 0) {
  2192. wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
  2193. "Wi-Fi Handover Request Message");
  2194. goto out;
  2195. }
  2196. if (attr.oob_dev_password == NULL ||
  2197. attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  2198. wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
  2199. "included in Wi-Fi Handover Request Message");
  2200. ret = -1;
  2201. goto out;
  2202. }
  2203. if (attr.uuid_e == NULL) {
  2204. wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
  2205. "Handover Request Message");
  2206. ret = -1;
  2207. goto out;
  2208. }
  2209. wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
  2210. wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
  2211. attr.oob_dev_password, attr.oob_dev_password_len);
  2212. dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
  2213. WPS_OOB_PUBKEY_HASH_LEN);
  2214. if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
  2215. wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
  2216. "%u in Wi-Fi Handover Request Message", dev_pw_id);
  2217. ret = -1;
  2218. goto out;
  2219. }
  2220. wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
  2221. attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
  2222. ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
  2223. attr.oob_dev_password,
  2224. DEV_PW_NFC_CONNECTION_HANDOVER,
  2225. NULL, 0, 1);
  2226. out:
  2227. wpabuf_free(wps);
  2228. return ret;
  2229. }
  2230. #endif /* CONFIG_WPS_NFC */
  2231. static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
  2232. {
  2233. size_t i;
  2234. struct os_reltime now;
  2235. if (wpa_debug_level > MSG_DEBUG)
  2236. return;
  2237. if (wpa_s->wps_ap == NULL)
  2238. return;
  2239. os_get_reltime(&now);
  2240. for (i = 0; i < wpa_s->num_wps_ap; i++) {
  2241. struct wps_ap_info *ap = &wpa_s->wps_ap[i];
  2242. struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
  2243. wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
  2244. "tries=%d last_attempt=%d sec ago blacklist=%d",
  2245. (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
  2246. ap->last_attempt.sec > 0 ?
  2247. (int) now.sec - (int) ap->last_attempt.sec : -1,
  2248. e ? e->count : 0);
  2249. }
  2250. }
  2251. static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
  2252. const u8 *bssid)
  2253. {
  2254. size_t i;
  2255. if (wpa_s->wps_ap == NULL)
  2256. return NULL;
  2257. for (i = 0; i < wpa_s->num_wps_ap; i++) {
  2258. struct wps_ap_info *ap = &wpa_s->wps_ap[i];
  2259. if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
  2260. return ap;
  2261. }
  2262. return NULL;
  2263. }
  2264. static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
  2265. struct wpa_scan_res *res)
  2266. {
  2267. struct wpabuf *wps;
  2268. enum wps_ap_info_type type;
  2269. struct wps_ap_info *ap;
  2270. int r;
  2271. if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
  2272. return;
  2273. wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
  2274. if (wps == NULL)
  2275. return;
  2276. r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
  2277. if (r == 2)
  2278. type = WPS_AP_SEL_REG_OUR;
  2279. else if (r == 1)
  2280. type = WPS_AP_SEL_REG;
  2281. else
  2282. type = WPS_AP_NOT_SEL_REG;
  2283. wpabuf_free(wps);
  2284. ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
  2285. if (ap) {
  2286. if (ap->type != type) {
  2287. wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
  2288. " changed type %d -> %d",
  2289. MAC2STR(res->bssid), ap->type, type);
  2290. ap->type = type;
  2291. if (type != WPS_AP_NOT_SEL_REG)
  2292. wpa_blacklist_del(wpa_s, ap->bssid);
  2293. }
  2294. return;
  2295. }
  2296. ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
  2297. sizeof(struct wps_ap_info));
  2298. if (ap == NULL)
  2299. return;
  2300. wpa_s->wps_ap = ap;
  2301. ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
  2302. wpa_s->num_wps_ap++;
  2303. os_memset(ap, 0, sizeof(*ap));
  2304. os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
  2305. ap->type = type;
  2306. wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
  2307. MAC2STR(ap->bssid), ap->type);
  2308. }
  2309. void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
  2310. struct wpa_scan_results *scan_res)
  2311. {
  2312. size_t i;
  2313. for (i = 0; i < scan_res->num; i++)
  2314. wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
  2315. wpas_wps_dump_ap_info(wpa_s);
  2316. }
  2317. void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2318. {
  2319. struct wps_ap_info *ap;
  2320. wpa_s->after_wps = 0;
  2321. if (!wpa_s->wps_ap_iter)
  2322. return;
  2323. ap = wpas_wps_get_ap_info(wpa_s, bssid);
  2324. if (ap == NULL)
  2325. return;
  2326. ap->tries++;
  2327. os_get_reltime(&ap->last_attempt);
  2328. }