wps_supplicant.c 79 KB

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