wps_supplicant.c 65 KB

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