wps_supplicant.c 73 KB

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