wps_supplicant.c 76 KB

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