wps_supplicant.c 36 KB

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