wps_supplicant.c 39 KB

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