wps_supplicant.c 64 KB

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