wps_supplicant.c 75 KB

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