wps_supplicant.c 77 KB

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