wps_supplicant.c 76 KB

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