wps_supplicant.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. /*
  2. * wpa_supplicant / WPS integration
  3. * Copyright (c) 2008-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "uuid.h"
  18. #include "crypto/dh_group5.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "common/ieee802_11_common.h"
  21. #include "common/wpa_common.h"
  22. #include "common/wpa_ctrl.h"
  23. #include "eap_common/eap_wsc_common.h"
  24. #include "eap_peer/eap.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "config.h"
  27. #include "wpa_supplicant_i.h"
  28. #include "driver_i.h"
  29. #include "notify.h"
  30. #include "blacklist.h"
  31. #include "bss.h"
  32. #include "scan.h"
  33. #include "p2p/p2p.h"
  34. #include "p2p_supplicant.h"
  35. #include "wps_supplicant.h"
  36. #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
  37. #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
  38. #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
  39. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
  40. static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
  41. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  42. {
  43. if (!wpa_s->wps_success &&
  44. wpa_s->current_ssid &&
  45. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  46. const u8 *bssid = wpa_s->bssid;
  47. if (is_zero_ether_addr(bssid))
  48. bssid = wpa_s->pending_bssid;
  49. wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
  50. " did not succeed - continue trying to find "
  51. "suitable AP", MAC2STR(bssid));
  52. wpa_blacklist_add(wpa_s, bssid);
  53. wpa_supplicant_deauthenticate(wpa_s,
  54. WLAN_REASON_DEAUTH_LEAVING);
  55. wpa_s->reassociate = 1;
  56. wpa_supplicant_req_scan(wpa_s,
  57. wpa_s->blacklist_cleared ? 5 : 0, 0);
  58. wpa_s->blacklist_cleared = 0;
  59. return 1;
  60. }
  61. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  62. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  63. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  64. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  65. "try to associate with the received credential");
  66. wpa_supplicant_deauthenticate(wpa_s,
  67. WLAN_REASON_DEAUTH_LEAVING);
  68. wpa_s->after_wps = 5;
  69. wpa_s->wps_freq = wpa_s->assoc_freq;
  70. wpa_s->reassociate = 1;
  71. wpa_supplicant_req_scan(wpa_s, 0, 0);
  72. return 1;
  73. }
  74. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
  75. wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
  76. "for external credential processing");
  77. wpas_clear_wps(wpa_s);
  78. wpa_supplicant_deauthenticate(wpa_s,
  79. WLAN_REASON_DEAUTH_LEAVING);
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
  85. struct wpa_ssid *ssid,
  86. const struct wps_credential *cred)
  87. {
  88. struct wpa_driver_capa capa;
  89. struct wpa_bss *bss;
  90. const u8 *ie;
  91. struct wpa_ie_data adv;
  92. int wpa2 = 0, ccmp = 0;
  93. /*
  94. * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
  95. * case they are configured for mixed mode operation (WPA+WPA2 and
  96. * TKIP+CCMP). Try to use scan results to figure out whether the AP
  97. * actually supports stronger security and select that if the client
  98. * has support for it, too.
  99. */
  100. if (wpa_drv_get_capa(wpa_s, &capa))
  101. return; /* Unknown what driver supports */
  102. bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
  103. if (bss == NULL) {
  104. wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
  105. "table - use credential as-is");
  106. return;
  107. }
  108. wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
  109. ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  110. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
  111. wpa2 = 1;
  112. if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
  113. ccmp = 1;
  114. } else {
  115. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  116. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
  117. adv.pairwise_cipher & WPA_CIPHER_CCMP)
  118. ccmp = 1;
  119. }
  120. if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
  121. (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
  122. /*
  123. * TODO: This could be the initial AP configuration and the
  124. * Beacon contents could change shortly. Should request a new
  125. * scan and delay addition of the network until the updated
  126. * scan results are available.
  127. */
  128. wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
  129. "support - use credential as-is");
  130. return;
  131. }
  132. if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  133. (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
  134. (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  135. wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
  136. "based on scan results");
  137. if (wpa_s->conf->ap_scan == 1)
  138. ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
  139. else
  140. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  141. }
  142. if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
  143. (ssid->proto & WPA_PROTO_WPA) &&
  144. (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
  145. wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
  146. "based on scan results");
  147. if (wpa_s->conf->ap_scan == 1)
  148. ssid->proto |= WPA_PROTO_RSN;
  149. else
  150. ssid->proto = WPA_PROTO_RSN;
  151. }
  152. }
  153. static int wpa_supplicant_wps_cred(void *ctx,
  154. const struct wps_credential *cred)
  155. {
  156. struct wpa_supplicant *wpa_s = ctx;
  157. struct wpa_ssid *ssid = wpa_s->current_ssid;
  158. u8 key_idx = 0;
  159. u16 auth_type;
  160. if ((wpa_s->conf->wps_cred_processing == 1 ||
  161. wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
  162. size_t blen = cred->cred_attr_len * 2 + 1;
  163. char *buf = os_malloc(blen);
  164. if (buf) {
  165. wpa_snprintf_hex(buf, blen,
  166. cred->cred_attr, cred->cred_attr_len);
  167. wpa_msg(wpa_s, MSG_INFO, "%s%s",
  168. WPS_EVENT_CRED_RECEIVED, buf);
  169. os_free(buf);
  170. }
  171. wpas_notify_wps_credential(wpa_s, cred);
  172. } else
  173. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
  174. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  175. cred->cred_attr, cred->cred_attr_len);
  176. if (wpa_s->conf->wps_cred_processing == 1)
  177. return 0;
  178. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  179. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  180. cred->auth_type);
  181. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  182. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  183. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  184. cred->key, cred->key_len);
  185. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  186. MAC2STR(cred->mac_addr));
  187. auth_type = cred->auth_type;
  188. if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  189. wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
  190. "auth_type into WPA2PSK");
  191. auth_type = WPS_AUTH_WPA2PSK;
  192. }
  193. if (auth_type != WPS_AUTH_OPEN &&
  194. auth_type != WPS_AUTH_SHARED &&
  195. auth_type != WPS_AUTH_WPAPSK &&
  196. auth_type != WPS_AUTH_WPA2PSK) {
  197. wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
  198. "unsupported authentication type 0x%x",
  199. auth_type);
  200. return 0;
  201. }
  202. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  203. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  204. "on the received credential");
  205. os_free(ssid->eap.identity);
  206. ssid->eap.identity = NULL;
  207. ssid->eap.identity_len = 0;
  208. os_free(ssid->eap.phase1);
  209. ssid->eap.phase1 = NULL;
  210. os_free(ssid->eap.eap_methods);
  211. ssid->eap.eap_methods = NULL;
  212. if (!ssid->p2p_group)
  213. ssid->temporary = 0;
  214. } else {
  215. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  216. "received credential");
  217. ssid = wpa_config_add_network(wpa_s->conf);
  218. if (ssid == NULL)
  219. return -1;
  220. wpas_notify_network_added(wpa_s, ssid);
  221. }
  222. wpa_config_set_network_defaults(ssid);
  223. os_free(ssid->ssid);
  224. ssid->ssid = os_malloc(cred->ssid_len);
  225. if (ssid->ssid) {
  226. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  227. ssid->ssid_len = cred->ssid_len;
  228. }
  229. switch (cred->encr_type) {
  230. case WPS_ENCR_NONE:
  231. break;
  232. case WPS_ENCR_WEP:
  233. if (cred->key_len <= 0)
  234. break;
  235. if (cred->key_len != 5 && cred->key_len != 13 &&
  236. cred->key_len != 10 && cred->key_len != 26) {
  237. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
  238. "%lu", (unsigned long) cred->key_len);
  239. return -1;
  240. }
  241. if (cred->key_idx > NUM_WEP_KEYS) {
  242. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
  243. cred->key_idx);
  244. return -1;
  245. }
  246. if (cred->key_idx)
  247. key_idx = cred->key_idx - 1;
  248. if (cred->key_len == 10 || cred->key_len == 26) {
  249. if (hexstr2bin((char *) cred->key,
  250. ssid->wep_key[key_idx],
  251. cred->key_len / 2) < 0) {
  252. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
  253. "%d", key_idx);
  254. return -1;
  255. }
  256. ssid->wep_key_len[key_idx] = cred->key_len / 2;
  257. } else {
  258. os_memcpy(ssid->wep_key[key_idx], cred->key,
  259. cred->key_len);
  260. ssid->wep_key_len[key_idx] = cred->key_len;
  261. }
  262. ssid->wep_tx_keyidx = key_idx;
  263. break;
  264. case WPS_ENCR_TKIP:
  265. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  266. break;
  267. case WPS_ENCR_AES:
  268. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  269. break;
  270. }
  271. switch (auth_type) {
  272. case WPS_AUTH_OPEN:
  273. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  274. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  275. ssid->proto = 0;
  276. break;
  277. case WPS_AUTH_SHARED:
  278. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  279. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  280. ssid->proto = 0;
  281. break;
  282. case WPS_AUTH_WPAPSK:
  283. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  284. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  285. ssid->proto = WPA_PROTO_WPA;
  286. break;
  287. case WPS_AUTH_WPA:
  288. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  289. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  290. ssid->proto = WPA_PROTO_WPA;
  291. break;
  292. case WPS_AUTH_WPA2:
  293. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  294. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  295. ssid->proto = WPA_PROTO_RSN;
  296. break;
  297. case WPS_AUTH_WPA2PSK:
  298. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  299. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  300. ssid->proto = WPA_PROTO_RSN;
  301. break;
  302. }
  303. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  304. if (cred->key_len == 2 * PMK_LEN) {
  305. if (hexstr2bin((const char *) cred->key, ssid->psk,
  306. PMK_LEN)) {
  307. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  308. "Key");
  309. return -1;
  310. }
  311. ssid->psk_set = 1;
  312. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  313. os_free(ssid->passphrase);
  314. ssid->passphrase = os_malloc(cred->key_len + 1);
  315. if (ssid->passphrase == NULL)
  316. return -1;
  317. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  318. ssid->passphrase[cred->key_len] = '\0';
  319. wpa_config_update_psk(ssid);
  320. } else {
  321. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  322. "length %lu",
  323. (unsigned long) cred->key_len);
  324. return -1;
  325. }
  326. }
  327. wpas_wps_security_workaround(wpa_s, ssid, cred);
  328. #ifndef CONFIG_NO_CONFIG_WRITE
  329. if (wpa_s->conf->update_config &&
  330. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  331. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  332. return -1;
  333. }
  334. #endif /* CONFIG_NO_CONFIG_WRITE */
  335. return 0;
  336. }
  337. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  338. struct wps_event_m2d *m2d)
  339. {
  340. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  341. "dev_password_id=%d config_error=%d",
  342. m2d->dev_password_id, m2d->config_error);
  343. wpas_notify_wps_event_m2d(wpa_s, m2d);
  344. }
  345. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  346. struct wps_event_fail *fail)
  347. {
  348. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
  349. wpas_clear_wps(wpa_s);
  350. wpas_notify_wps_event_fail(wpa_s, fail);
  351. }
  352. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  353. {
  354. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  355. wpa_s->wps_success = 1;
  356. wpas_notify_wps_event_success(wpa_s);
  357. #ifdef CONFIG_P2P
  358. wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
  359. #endif /* CONFIG_P2P */
  360. }
  361. static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
  362. struct wps_event_er_ap *ap)
  363. {
  364. char uuid_str[100];
  365. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  366. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  367. if (ap->pri_dev_type)
  368. wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
  369. sizeof(dev_type));
  370. else
  371. dev_type[0] = '\0';
  372. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
  373. " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
  374. uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
  375. ap->friendly_name ? ap->friendly_name : "",
  376. ap->manufacturer ? ap->manufacturer : "",
  377. ap->model_description ? ap->model_description : "",
  378. ap->model_name ? ap->model_name : "",
  379. ap->manufacturer_url ? ap->manufacturer_url : "",
  380. ap->model_url ? ap->model_url : "");
  381. }
  382. static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
  383. struct wps_event_er_ap *ap)
  384. {
  385. char uuid_str[100];
  386. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  387. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
  388. }
  389. static void wpa_supplicant_wps_event_er_enrollee_add(
  390. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  391. {
  392. char uuid_str[100];
  393. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  394. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  395. if (enrollee->pri_dev_type)
  396. wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
  397. sizeof(dev_type));
  398. else
  399. dev_type[0] = '\0';
  400. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
  401. " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
  402. "|%s|%s|%s|%s|%s|",
  403. uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
  404. enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
  405. enrollee->dev_name ? enrollee->dev_name : "",
  406. enrollee->manufacturer ? enrollee->manufacturer : "",
  407. enrollee->model_name ? enrollee->model_name : "",
  408. enrollee->model_number ? enrollee->model_number : "",
  409. enrollee->serial_number ? enrollee->serial_number : "");
  410. }
  411. static void wpa_supplicant_wps_event_er_enrollee_remove(
  412. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  413. {
  414. char uuid_str[100];
  415. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  416. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
  417. uuid_str, MAC2STR(enrollee->mac_addr));
  418. }
  419. static void wpa_supplicant_wps_event_er_ap_settings(
  420. struct wpa_supplicant *wpa_s,
  421. struct wps_event_er_ap_settings *ap_settings)
  422. {
  423. char uuid_str[100];
  424. char key_str[65];
  425. const struct wps_credential *cred = ap_settings->cred;
  426. key_str[0] = '\0';
  427. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  428. if (cred->key_len >= 8 && cred->key_len <= 64) {
  429. os_memcpy(key_str, cred->key, cred->key_len);
  430. key_str[cred->key_len] = '\0';
  431. }
  432. }
  433. uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
  434. /* Use wpa_msg_ctrl to avoid showing the key in debug log */
  435. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
  436. "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
  437. "key=%s",
  438. uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
  439. cred->auth_type, cred->encr_type, key_str);
  440. }
  441. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  442. union wps_event_data *data)
  443. {
  444. struct wpa_supplicant *wpa_s = ctx;
  445. switch (event) {
  446. case WPS_EV_M2D:
  447. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  448. break;
  449. case WPS_EV_FAIL:
  450. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  451. break;
  452. case WPS_EV_SUCCESS:
  453. wpa_supplicant_wps_event_success(wpa_s);
  454. break;
  455. case WPS_EV_PWD_AUTH_FAIL:
  456. break;
  457. case WPS_EV_PBC_OVERLAP:
  458. break;
  459. case WPS_EV_PBC_TIMEOUT:
  460. break;
  461. case WPS_EV_ER_AP_ADD:
  462. wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
  463. break;
  464. case WPS_EV_ER_AP_REMOVE:
  465. wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
  466. break;
  467. case WPS_EV_ER_ENROLLEE_ADD:
  468. wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
  469. &data->enrollee);
  470. break;
  471. case WPS_EV_ER_ENROLLEE_REMOVE:
  472. wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
  473. &data->enrollee);
  474. break;
  475. case WPS_EV_ER_AP_SETTINGS:
  476. wpa_supplicant_wps_event_er_ap_settings(wpa_s,
  477. &data->ap_settings);
  478. break;
  479. }
  480. }
  481. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  482. {
  483. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  484. eap_is_wps_pin_enrollee(&ssid->eap))
  485. return WPS_REQ_ENROLLEE;
  486. else
  487. return WPS_REQ_REGISTRAR;
  488. }
  489. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  490. {
  491. int id;
  492. struct wpa_ssid *ssid, *remove_ssid = NULL;
  493. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  494. /* Remove any existing WPS network from configuration */
  495. ssid = wpa_s->conf->ssid;
  496. while (ssid) {
  497. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  498. if (ssid == wpa_s->current_ssid) {
  499. wpa_s->current_ssid = NULL;
  500. if (ssid != NULL)
  501. wpas_notify_network_changed(wpa_s);
  502. }
  503. id = ssid->id;
  504. remove_ssid = ssid;
  505. } else
  506. id = -1;
  507. ssid = ssid->next;
  508. if (id >= 0) {
  509. wpas_notify_network_removed(wpa_s, remove_ssid);
  510. wpa_config_remove_network(wpa_s->conf, id);
  511. }
  512. }
  513. }
  514. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  515. {
  516. struct wpa_supplicant *wpa_s = eloop_ctx;
  517. wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  518. "out");
  519. wpas_clear_wps(wpa_s);
  520. }
  521. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  522. int registrar, const u8 *bssid)
  523. {
  524. struct wpa_ssid *ssid;
  525. ssid = wpa_config_add_network(wpa_s->conf);
  526. if (ssid == NULL)
  527. return NULL;
  528. wpas_notify_network_added(wpa_s, ssid);
  529. wpa_config_set_network_defaults(ssid);
  530. ssid->temporary = 1;
  531. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  532. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  533. wpa_config_set(ssid, "identity", registrar ?
  534. "\"" WSC_ID_REGISTRAR "\"" :
  535. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  536. wpas_notify_network_removed(wpa_s, ssid);
  537. wpa_config_remove_network(wpa_s->conf, ssid->id);
  538. return NULL;
  539. }
  540. if (bssid) {
  541. #ifndef CONFIG_P2P
  542. struct wpa_bss *bss;
  543. int count = 0;
  544. #endif /* CONFIG_P2P */
  545. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  546. ssid->bssid_set = 1;
  547. /*
  548. * Note: With P2P, the SSID may change at the time the WPS
  549. * provisioning is started, so better not filter the AP based
  550. * on the current SSID in the scan results.
  551. */
  552. #ifndef CONFIG_P2P
  553. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  554. if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
  555. continue;
  556. os_free(ssid->ssid);
  557. ssid->ssid = os_malloc(bss->ssid_len);
  558. if (ssid->ssid == NULL)
  559. break;
  560. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  561. ssid->ssid_len = bss->ssid_len;
  562. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
  563. "scan results",
  564. ssid->ssid, ssid->ssid_len);
  565. count++;
  566. }
  567. if (count > 1) {
  568. wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
  569. "for the AP; use wildcard");
  570. os_free(ssid->ssid);
  571. ssid->ssid = NULL;
  572. ssid->ssid_len = 0;
  573. }
  574. #endif /* CONFIG_P2P */
  575. }
  576. return ssid;
  577. }
  578. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  579. struct wpa_ssid *selected)
  580. {
  581. struct wpa_ssid *ssid;
  582. /* Mark all other networks disabled and trigger reassociation */
  583. ssid = wpa_s->conf->ssid;
  584. while (ssid) {
  585. int was_disabled = ssid->disabled;
  586. ssid->disabled = ssid != selected;
  587. if (was_disabled != ssid->disabled)
  588. wpas_notify_network_enabled_changed(wpa_s, ssid);
  589. ssid = ssid->next;
  590. }
  591. wpa_s->disconnected = 0;
  592. wpa_s->reassociate = 1;
  593. wpa_s->scan_runs = 0;
  594. wpa_s->wps_success = 0;
  595. wpa_s->blacklist_cleared = 0;
  596. wpa_supplicant_req_scan(wpa_s, 0, 0);
  597. }
  598. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  599. int p2p_group)
  600. {
  601. struct wpa_ssid *ssid;
  602. wpas_clear_wps(wpa_s);
  603. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  604. if (ssid == NULL)
  605. return -1;
  606. ssid->temporary = 1;
  607. ssid->p2p_group = p2p_group;
  608. #ifdef CONFIG_P2P
  609. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  610. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  611. if (ssid->ssid) {
  612. ssid->ssid_len = wpa_s->go_params->ssid_len;
  613. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  614. ssid->ssid_len);
  615. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  616. "SSID", ssid->ssid, ssid->ssid_len);
  617. }
  618. }
  619. #endif /* CONFIG_P2P */
  620. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  621. if (wpa_s->wps_fragment_size)
  622. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  623. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  624. wpa_s, NULL);
  625. wpas_wps_reassoc(wpa_s, ssid);
  626. return 0;
  627. }
  628. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  629. const char *pin, int p2p_group, u16 dev_pw_id)
  630. {
  631. struct wpa_ssid *ssid;
  632. char val[128];
  633. unsigned int rpin = 0;
  634. wpas_clear_wps(wpa_s);
  635. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  636. if (ssid == NULL)
  637. return -1;
  638. ssid->temporary = 1;
  639. ssid->p2p_group = p2p_group;
  640. #ifdef CONFIG_P2P
  641. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  642. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  643. if (ssid->ssid) {
  644. ssid->ssid_len = wpa_s->go_params->ssid_len;
  645. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  646. ssid->ssid_len);
  647. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  648. "SSID", ssid->ssid, ssid->ssid_len);
  649. }
  650. }
  651. #endif /* CONFIG_P2P */
  652. if (pin)
  653. os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
  654. pin, dev_pw_id);
  655. else {
  656. rpin = wps_generate_pin();
  657. os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
  658. rpin, dev_pw_id);
  659. }
  660. wpa_config_set(ssid, "phase1", val, 0);
  661. if (wpa_s->wps_fragment_size)
  662. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  663. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  664. wpa_s, NULL);
  665. wpas_wps_reassoc(wpa_s, ssid);
  666. return rpin;
  667. }
  668. #ifdef CONFIG_WPS_OOB
  669. int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
  670. char *path, char *method, char *name)
  671. {
  672. struct wps_context *wps = wpa_s->wps;
  673. struct oob_device_data *oob_dev;
  674. oob_dev = wps_get_oob_device(device_type);
  675. if (oob_dev == NULL)
  676. return -1;
  677. oob_dev->device_path = path;
  678. oob_dev->device_name = name;
  679. wps->oob_conf.oob_method = wps_get_oob_method(method);
  680. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  681. /*
  682. * Use pre-configured DH keys in order to be able to write the
  683. * key hash into the OOB file.
  684. */
  685. wpabuf_free(wps->dh_pubkey);
  686. wpabuf_free(wps->dh_privkey);
  687. wps->dh_privkey = NULL;
  688. wps->dh_pubkey = NULL;
  689. dh5_free(wps->dh_ctx);
  690. wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
  691. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  692. if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
  693. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  694. "Diffie-Hellman handshake");
  695. return -1;
  696. }
  697. }
  698. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  699. wpas_clear_wps(wpa_s);
  700. if (wps_process_oob(wps, oob_dev, 0) < 0)
  701. return -1;
  702. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  703. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  704. wpas_wps_start_pin(wpa_s, NULL,
  705. wpabuf_head(wps->oob_conf.dev_password), 0) < 0)
  706. return -1;
  707. return 0;
  708. }
  709. #endif /* CONFIG_WPS_OOB */
  710. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  711. const char *pin, struct wps_new_ap_settings *settings)
  712. {
  713. struct wpa_ssid *ssid;
  714. char val[200];
  715. char *pos, *end;
  716. int res;
  717. if (!pin)
  718. return -1;
  719. wpas_clear_wps(wpa_s);
  720. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  721. if (ssid == NULL)
  722. return -1;
  723. ssid->temporary = 1;
  724. pos = val;
  725. end = pos + sizeof(val);
  726. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  727. if (res < 0 || res >= end - pos)
  728. return -1;
  729. pos += res;
  730. if (settings) {
  731. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  732. "new_encr=%s new_key=%s",
  733. settings->ssid_hex, settings->auth,
  734. settings->encr, settings->key_hex);
  735. if (res < 0 || res >= end - pos)
  736. return -1;
  737. pos += res;
  738. }
  739. res = os_snprintf(pos, end - pos, "\"");
  740. if (res < 0 || res >= end - pos)
  741. return -1;
  742. wpa_config_set(ssid, "phase1", val, 0);
  743. if (wpa_s->wps_fragment_size)
  744. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  745. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  746. wpa_s, NULL);
  747. wpas_wps_reassoc(wpa_s, ssid);
  748. return 0;
  749. }
  750. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  751. size_t psk_len)
  752. {
  753. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  754. "STA " MACSTR, MAC2STR(mac_addr));
  755. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  756. /* TODO */
  757. return 0;
  758. }
  759. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  760. const struct wps_device_data *dev)
  761. {
  762. char uuid[40], txt[400];
  763. int len;
  764. char devtype[WPS_DEV_TYPE_BUFSIZE];
  765. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  766. return;
  767. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  768. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  769. " [%s|%s|%s|%s|%s|%s]",
  770. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  771. dev->manufacturer, dev->model_name,
  772. dev->model_number, dev->serial_number,
  773. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  774. sizeof(devtype)));
  775. if (len > 0 && len < (int) sizeof(txt))
  776. wpa_printf(MSG_INFO, "%s", txt);
  777. }
  778. static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
  779. u16 sel_reg_config_methods)
  780. {
  781. #ifdef CONFIG_WPS_ER
  782. struct wpa_supplicant *wpa_s = ctx;
  783. if (wpa_s->wps_er == NULL)
  784. return;
  785. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
  786. "dev_password_id=%u sel_reg_config_methods=0x%x",
  787. sel_reg, dev_passwd_id, sel_reg_config_methods);
  788. wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
  789. sel_reg_config_methods);
  790. #endif /* CONFIG_WPS_ER */
  791. }
  792. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  793. {
  794. struct wps_context *wps;
  795. struct wps_registrar_config rcfg;
  796. wps = os_zalloc(sizeof(*wps));
  797. if (wps == NULL)
  798. return -1;
  799. wps->cred_cb = wpa_supplicant_wps_cred;
  800. wps->event_cb = wpa_supplicant_wps_event;
  801. wps->cb_ctx = wpa_s;
  802. wps->dev.device_name = wpa_s->conf->device_name;
  803. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  804. wps->dev.model_name = wpa_s->conf->model_name;
  805. wps->dev.model_number = wpa_s->conf->model_number;
  806. wps->dev.serial_number = wpa_s->conf->serial_number;
  807. wps->config_methods =
  808. wps_config_methods_str2bin(wpa_s->conf->config_methods);
  809. if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  810. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  811. wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
  812. "methods are not allowed at the same time");
  813. os_free(wps);
  814. return -1;
  815. }
  816. if (wpa_s->conf->device_type &&
  817. wps_dev_type_str2bin(wpa_s->conf->device_type,
  818. wps->dev.pri_dev_type) < 0) {
  819. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  820. os_free(wps);
  821. return -1;
  822. }
  823. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  824. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  825. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  826. if (is_nil_uuid(wpa_s->conf->uuid)) {
  827. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  828. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  829. wps->uuid, WPS_UUID_LEN);
  830. } else
  831. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  832. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  833. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  834. os_memset(&rcfg, 0, sizeof(rcfg));
  835. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  836. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  837. rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
  838. rcfg.cb_ctx = wpa_s;
  839. wps->registrar = wps_registrar_init(wps, &rcfg);
  840. if (wps->registrar == NULL) {
  841. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  842. os_free(wps);
  843. return -1;
  844. }
  845. wpa_s->wps = wps;
  846. return 0;
  847. }
  848. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  849. {
  850. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  851. if (wpa_s->wps == NULL)
  852. return;
  853. #ifdef CONFIG_WPS_ER
  854. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  855. wpa_s->wps_er = NULL;
  856. #endif /* CONFIG_WPS_ER */
  857. wps_registrar_deinit(wpa_s->wps->registrar);
  858. wpabuf_free(wpa_s->wps->dh_pubkey);
  859. wpabuf_free(wpa_s->wps->dh_privkey);
  860. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  861. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  862. os_free(wpa_s->wps->network_key);
  863. os_free(wpa_s->wps);
  864. wpa_s->wps = NULL;
  865. }
  866. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  867. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  868. {
  869. struct wpabuf *wps_ie;
  870. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  871. return -1;
  872. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  873. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  874. if (!wps_ie) {
  875. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  876. return 0;
  877. }
  878. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  879. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  880. "without active PBC Registrar");
  881. wpabuf_free(wps_ie);
  882. return 0;
  883. }
  884. /* TODO: overlap detection */
  885. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  886. "(Active PBC)");
  887. wpabuf_free(wps_ie);
  888. return 1;
  889. }
  890. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  891. if (!wps_ie) {
  892. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  893. return 0;
  894. }
  895. /*
  896. * Start with WPS APs that advertise our address as an
  897. * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
  898. * allow any WPS AP after couple of scans since some APs do not
  899. * set Selected Registrar attribute properly when using
  900. * external Registrar.
  901. */
  902. if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
  903. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  904. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  905. "without active PIN Registrar");
  906. wpabuf_free(wps_ie);
  907. return 0;
  908. }
  909. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  910. } else {
  911. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  912. "(Authorized MAC or Active PIN)");
  913. }
  914. wpabuf_free(wps_ie);
  915. return 1;
  916. }
  917. if (wps_ie) {
  918. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  919. wpabuf_free(wps_ie);
  920. return 1;
  921. }
  922. return -1;
  923. }
  924. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  925. struct wpa_ssid *ssid,
  926. struct wpa_scan_res *bss)
  927. {
  928. struct wpabuf *wps_ie = NULL;
  929. int ret = 0;
  930. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  931. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  932. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  933. /* allow wildcard SSID for WPS PBC */
  934. ret = 1;
  935. }
  936. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  937. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  938. if (wps_ie &&
  939. (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
  940. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  941. /* allow wildcard SSID for WPS PIN */
  942. ret = 1;
  943. }
  944. }
  945. if (!ret && ssid->bssid_set &&
  946. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  947. /* allow wildcard SSID due to hardcoded BSSID match */
  948. ret = 1;
  949. }
  950. #ifdef CONFIG_WPS_STRICT
  951. if (wps_ie) {
  952. if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
  953. 0) < 0)
  954. ret = 0;
  955. if (bss->beacon_ie_len) {
  956. struct wpabuf *bcn_wps;
  957. bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
  958. bss, WPS_IE_VENDOR_TYPE);
  959. if (bcn_wps == NULL) {
  960. wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
  961. "missing from AP Beacon");
  962. ret = 0;
  963. } else {
  964. if (wps_validate_beacon(wps_ie) < 0)
  965. ret = 0;
  966. wpabuf_free(bcn_wps);
  967. }
  968. }
  969. }
  970. #endif /* CONFIG_WPS_STRICT */
  971. wpabuf_free(wps_ie);
  972. return ret;
  973. }
  974. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  975. struct wpa_bss *selected, struct wpa_ssid *ssid)
  976. {
  977. const u8 *sel_uuid, *uuid;
  978. struct wpabuf *wps_ie;
  979. int ret = 0;
  980. struct wpa_bss *bss;
  981. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  982. return 0;
  983. /* Make sure that only one AP is in active PBC mode */
  984. wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  985. if (wps_ie)
  986. sel_uuid = wps_get_uuid_e(wps_ie);
  987. else
  988. sel_uuid = NULL;
  989. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  990. struct wpabuf *ie;
  991. if (bss == selected)
  992. continue;
  993. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  994. if (!ie)
  995. continue;
  996. if (!wps_is_selected_pbc_registrar(ie)) {
  997. wpabuf_free(ie);
  998. continue;
  999. }
  1000. uuid = wps_get_uuid_e(ie);
  1001. if (sel_uuid == NULL || uuid == NULL ||
  1002. os_memcmp(sel_uuid, uuid, 16) != 0) {
  1003. ret = 1; /* PBC overlap */
  1004. wpabuf_free(ie);
  1005. break;
  1006. }
  1007. /* TODO: verify that this is reasonable dual-band situation */
  1008. wpabuf_free(ie);
  1009. }
  1010. wpabuf_free(wps_ie);
  1011. return ret;
  1012. }
  1013. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  1014. {
  1015. struct wpa_bss *bss;
  1016. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  1017. return;
  1018. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1019. struct wpabuf *ie;
  1020. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1021. if (!ie)
  1022. continue;
  1023. if (wps_is_selected_pbc_registrar(ie))
  1024. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1025. WPS_EVENT_AP_AVAILABLE_PBC);
  1026. else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
  1027. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1028. WPS_EVENT_AP_AVAILABLE_AUTH);
  1029. else if (wps_is_selected_pin_registrar(ie))
  1030. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1031. WPS_EVENT_AP_AVAILABLE_PIN);
  1032. else
  1033. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1034. WPS_EVENT_AP_AVAILABLE);
  1035. wpabuf_free(ie);
  1036. break;
  1037. }
  1038. }
  1039. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  1040. {
  1041. struct wpa_ssid *ssid;
  1042. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1043. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  1044. return 1;
  1045. }
  1046. return 0;
  1047. }
  1048. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  1049. char *end)
  1050. {
  1051. struct wpabuf *wps_ie;
  1052. int ret;
  1053. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  1054. if (wps_ie == NULL)
  1055. return 0;
  1056. ret = wps_attr_text(wps_ie, buf, end);
  1057. wpabuf_free(wps_ie);
  1058. return ret;
  1059. }
  1060. int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
  1061. {
  1062. #ifdef CONFIG_WPS_ER
  1063. if (wpa_s->wps_er) {
  1064. wps_er_refresh(wpa_s->wps_er);
  1065. return 0;
  1066. }
  1067. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
  1068. if (wpa_s->wps_er == NULL)
  1069. return -1;
  1070. return 0;
  1071. #else /* CONFIG_WPS_ER */
  1072. return 0;
  1073. #endif /* CONFIG_WPS_ER */
  1074. }
  1075. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  1076. {
  1077. #ifdef CONFIG_WPS_ER
  1078. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1079. wpa_s->wps_er = NULL;
  1080. #endif /* CONFIG_WPS_ER */
  1081. return 0;
  1082. }
  1083. #ifdef CONFIG_WPS_ER
  1084. int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
  1085. const char *uuid, const char *pin)
  1086. {
  1087. u8 u[UUID_LEN];
  1088. int any = 0;
  1089. if (os_strcmp(uuid, "any") == 0)
  1090. any = 1;
  1091. else if (uuid_str2bin(uuid, u))
  1092. return -1;
  1093. return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
  1094. any ? NULL : u,
  1095. (const u8 *) pin, os_strlen(pin), 300);
  1096. }
  1097. int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
  1098. {
  1099. u8 u[UUID_LEN];
  1100. if (uuid_str2bin(uuid, u))
  1101. return -1;
  1102. return wps_er_pbc(wpa_s->wps_er, u);
  1103. }
  1104. int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
  1105. const char *pin)
  1106. {
  1107. u8 u[UUID_LEN];
  1108. if (uuid_str2bin(uuid, u))
  1109. return -1;
  1110. return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
  1111. os_strlen(pin));
  1112. }
  1113. int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1114. const char *pin, struct wps_new_ap_settings *settings)
  1115. {
  1116. u8 u[UUID_LEN];
  1117. struct wps_credential cred;
  1118. size_t len;
  1119. if (uuid_str2bin(uuid, u))
  1120. return -1;
  1121. if (settings->ssid_hex == NULL || settings->auth == NULL ||
  1122. settings->encr == NULL || settings->key_hex == NULL)
  1123. return -1;
  1124. os_memset(&cred, 0, sizeof(cred));
  1125. len = os_strlen(settings->ssid_hex);
  1126. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1127. hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
  1128. return -1;
  1129. cred.ssid_len = len / 2;
  1130. len = os_strlen(settings->key_hex);
  1131. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1132. hexstr2bin(settings->key_hex, cred.key, len / 2))
  1133. return -1;
  1134. cred.key_len = len / 2;
  1135. if (os_strcmp(settings->auth, "OPEN") == 0)
  1136. cred.auth_type = WPS_AUTH_OPEN;
  1137. else if (os_strcmp(settings->auth, "WPAPSK") == 0)
  1138. cred.auth_type = WPS_AUTH_WPAPSK;
  1139. else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
  1140. cred.auth_type = WPS_AUTH_WPA2PSK;
  1141. else
  1142. return -1;
  1143. if (os_strcmp(settings->encr, "NONE") == 0)
  1144. cred.encr_type = WPS_ENCR_NONE;
  1145. else if (os_strcmp(settings->encr, "WEP") == 0)
  1146. cred.encr_type = WPS_ENCR_WEP;
  1147. else if (os_strcmp(settings->encr, "TKIP") == 0)
  1148. cred.encr_type = WPS_ENCR_TKIP;
  1149. else if (os_strcmp(settings->encr, "CCMP") == 0)
  1150. cred.encr_type = WPS_ENCR_AES;
  1151. else
  1152. return -1;
  1153. return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
  1154. os_strlen(pin), &cred);
  1155. }
  1156. static void wpas_wps_terminate_cb(void *ctx)
  1157. {
  1158. wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
  1159. eloop_terminate();
  1160. }
  1161. #endif /* CONFIG_WPS_ER */
  1162. int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
  1163. {
  1164. #ifdef CONFIG_WPS_ER
  1165. if (wpa_s->wps_er) {
  1166. wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
  1167. wpa_s->wps_er = NULL;
  1168. return 1;
  1169. }
  1170. #endif /* CONFIG_WPS_ER */
  1171. return 0;
  1172. }
  1173. int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
  1174. {
  1175. struct wpa_ssid *ssid;
  1176. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1177. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  1178. return 1;
  1179. }
  1180. return 0;
  1181. }
  1182. void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
  1183. {
  1184. struct wps_context *wps = wpa_s->wps;
  1185. if (wps == NULL)
  1186. return;
  1187. if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
  1188. wps->config_methods = wps_config_methods_str2bin(
  1189. wpa_s->conf->config_methods);
  1190. if ((wps->config_methods &
  1191. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1192. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1193. wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
  1194. "config methods are not allowed at the "
  1195. "same time");
  1196. wps->config_methods &= ~WPS_CONFIG_LABEL;
  1197. }
  1198. }
  1199. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
  1200. if (wpa_s->conf->device_type &&
  1201. wps_dev_type_str2bin(wpa_s->conf->device_type,
  1202. wps->dev.pri_dev_type) < 0)
  1203. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  1204. }
  1205. if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
  1206. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1207. if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
  1208. if (is_nil_uuid(wpa_s->conf->uuid)) {
  1209. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  1210. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  1211. "address", wps->uuid, WPS_UUID_LEN);
  1212. } else
  1213. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  1214. }
  1215. if (wpa_s->conf->changed_parameters &
  1216. (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
  1217. /* Update pointers to make sure they refer current values */
  1218. wps->dev.device_name = wpa_s->conf->device_name;
  1219. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1220. wps->dev.model_name = wpa_s->conf->model_name;
  1221. wps->dev.model_number = wpa_s->conf->model_number;
  1222. wps->dev.serial_number = wpa_s->conf->serial_number;
  1223. }
  1224. }