wps_supplicant.c 75 KB

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