wps_supplicant.c 73 KB

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