wps_supplicant.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * wpa_supplicant / WPS integration
  3. * Copyright (c) 2008, 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 "ieee802_11_defs.h"
  17. #include "ieee802_11_common.h"
  18. #include "wpa_common.h"
  19. #include "config.h"
  20. #include "eap_peer/eap.h"
  21. #include "wpa_supplicant_i.h"
  22. #include "driver_i.h"
  23. #include "eloop.h"
  24. #include "uuid.h"
  25. #include "wpa_ctrl.h"
  26. #include "notify.h"
  27. #include "eap_common/eap_wsc_common.h"
  28. #include "blacklist.h"
  29. #include "wpa.h"
  30. #include "wps_supplicant.h"
  31. #include "dh_group5.h"
  32. #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
  33. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
  34. static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
  35. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  36. {
  37. if (!wpa_s->wps_success &&
  38. wpa_s->current_ssid &&
  39. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  40. const u8 *bssid = wpa_s->bssid;
  41. if (is_zero_ether_addr(bssid))
  42. bssid = wpa_s->pending_bssid;
  43. wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
  44. " did not succeed - continue trying to find "
  45. "suitable AP", MAC2STR(bssid));
  46. wpa_blacklist_add(wpa_s, bssid);
  47. wpa_supplicant_deauthenticate(wpa_s,
  48. WLAN_REASON_DEAUTH_LEAVING);
  49. wpa_s->reassociate = 1;
  50. wpa_supplicant_req_scan(wpa_s,
  51. wpa_s->blacklist_cleared ? 5 : 0, 0);
  52. wpa_s->blacklist_cleared = 0;
  53. return 1;
  54. }
  55. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  56. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  57. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  58. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  59. "try to associate with the received credential");
  60. wpa_supplicant_deauthenticate(wpa_s,
  61. WLAN_REASON_DEAUTH_LEAVING);
  62. wpa_s->reassociate = 1;
  63. wpa_supplicant_req_scan(wpa_s, 0, 0);
  64. return 1;
  65. }
  66. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
  67. wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
  68. "for external credential processing");
  69. wpas_clear_wps(wpa_s);
  70. wpa_supplicant_deauthenticate(wpa_s,
  71. WLAN_REASON_DEAUTH_LEAVING);
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
  77. struct wpa_ssid *ssid,
  78. const struct wps_credential *cred)
  79. {
  80. struct wpa_driver_capa capa;
  81. size_t i;
  82. struct wpa_scan_res *bss;
  83. const u8 *ie;
  84. struct wpa_ie_data adv;
  85. int wpa2 = 0, ccmp = 0;
  86. /*
  87. * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
  88. * case they are configured for mixed mode operation (WPA+WPA2 and
  89. * TKIP+CCMP). Try to use scan results to figure out whether the AP
  90. * actually supports stronger security and select that if the client
  91. * has support for it, too.
  92. */
  93. if (wpa_drv_get_capa(wpa_s, &capa))
  94. return; /* Unknown what driver supports */
  95. if (wpa_supplicant_get_scan_results(wpa_s) || wpa_s->scan_res == NULL)
  96. return; /* Could not get scan results for checking advertised
  97. * parameters */
  98. for (i = 0; i < wpa_s->scan_res->num; i++) {
  99. bss = wpa_s->scan_res->res[i];
  100. if (os_memcmp(bss->bssid, cred->mac_addr, ETH_ALEN) != 0)
  101. continue;
  102. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  103. if (ie == NULL)
  104. continue;
  105. if (ie[1] != ssid->ssid_len || ssid->ssid == NULL ||
  106. os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) != 0)
  107. continue;
  108. wpa_printf(MSG_DEBUG, "WPS: AP found from scan results");
  109. break;
  110. }
  111. if (i == wpa_s->scan_res->num) {
  112. wpa_printf(MSG_DEBUG, "WPS: The AP was not found from scan "
  113. "results - use credential as-is");
  114. return;
  115. }
  116. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  117. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
  118. wpa2 = 1;
  119. if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
  120. ccmp = 1;
  121. } else {
  122. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  123. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
  124. adv.pairwise_cipher & WPA_CIPHER_CCMP)
  125. ccmp = 1;
  126. }
  127. if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
  128. (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
  129. /*
  130. * TODO: This could be the initial AP configuration and the
  131. * Beacon contents could change shortly. Should request a new
  132. * scan and delay addition of the network until the updated
  133. * scan results are available.
  134. */
  135. wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
  136. "support - use credential as-is");
  137. return;
  138. }
  139. if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  140. (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
  141. (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  142. wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
  143. "based on scan results");
  144. if (wpa_s->conf->ap_scan == 1)
  145. ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
  146. else
  147. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  148. }
  149. if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
  150. (ssid->proto & WPA_PROTO_WPA) &&
  151. (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
  152. wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
  153. "based on scan results");
  154. if (wpa_s->conf->ap_scan == 1)
  155. ssid->proto |= WPA_PROTO_RSN;
  156. else
  157. ssid->proto = WPA_PROTO_RSN;
  158. }
  159. }
  160. static int wpa_supplicant_wps_cred(void *ctx,
  161. const struct wps_credential *cred)
  162. {
  163. struct wpa_supplicant *wpa_s = ctx;
  164. struct wpa_ssid *ssid = wpa_s->current_ssid;
  165. u8 key_idx = 0;
  166. u16 auth_type;
  167. if ((wpa_s->conf->wps_cred_processing == 1 ||
  168. wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
  169. size_t blen = cred->cred_attr_len * 2 + 1;
  170. char *buf = os_malloc(blen);
  171. if (buf) {
  172. wpa_snprintf_hex(buf, blen,
  173. cred->cred_attr, cred->cred_attr_len);
  174. wpa_msg(wpa_s, MSG_INFO, "%s%s",
  175. WPS_EVENT_CRED_RECEIVED, buf);
  176. os_free(buf);
  177. }
  178. wpas_notify_wps_credential(wpa_s, cred);
  179. } else
  180. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
  181. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  182. cred->cred_attr, cred->cred_attr_len);
  183. if (wpa_s->conf->wps_cred_processing == 1)
  184. return 0;
  185. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  186. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  187. cred->auth_type);
  188. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  189. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  190. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  191. cred->key, cred->key_len);
  192. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  193. MAC2STR(cred->mac_addr));
  194. auth_type = cred->auth_type;
  195. if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  196. wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
  197. "auth_type into WPA2PSK");
  198. auth_type = WPS_AUTH_WPA2PSK;
  199. }
  200. if (auth_type != WPS_AUTH_OPEN &&
  201. auth_type != WPS_AUTH_SHARED &&
  202. auth_type != WPS_AUTH_WPAPSK &&
  203. auth_type != WPS_AUTH_WPA2PSK) {
  204. wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
  205. "unsupported authentication type 0x%x",
  206. auth_type);
  207. return 0;
  208. }
  209. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  210. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  211. "on the received credential");
  212. os_free(ssid->eap.identity);
  213. ssid->eap.identity = NULL;
  214. ssid->eap.identity_len = 0;
  215. os_free(ssid->eap.phase1);
  216. ssid->eap.phase1 = NULL;
  217. os_free(ssid->eap.eap_methods);
  218. ssid->eap.eap_methods = NULL;
  219. } else {
  220. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  221. "received credential");
  222. ssid = wpa_config_add_network(wpa_s->conf);
  223. if (ssid == NULL)
  224. return -1;
  225. wpas_notify_network_added(wpa_s, ssid);
  226. }
  227. wpa_config_set_network_defaults(ssid);
  228. os_free(ssid->ssid);
  229. ssid->ssid = os_malloc(cred->ssid_len);
  230. if (ssid->ssid) {
  231. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  232. ssid->ssid_len = cred->ssid_len;
  233. }
  234. switch (cred->encr_type) {
  235. case WPS_ENCR_NONE:
  236. break;
  237. case WPS_ENCR_WEP:
  238. if (cred->key_len <= 0)
  239. break;
  240. if (cred->key_len != 5 && cred->key_len != 13 &&
  241. cred->key_len != 10 && cred->key_len != 26) {
  242. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
  243. "%lu", (unsigned long) cred->key_len);
  244. return -1;
  245. }
  246. if (cred->key_idx > NUM_WEP_KEYS) {
  247. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
  248. cred->key_idx);
  249. return -1;
  250. }
  251. if (cred->key_idx)
  252. key_idx = cred->key_idx - 1;
  253. if (cred->key_len == 10 || cred->key_len == 26) {
  254. if (hexstr2bin((char *) cred->key,
  255. ssid->wep_key[key_idx],
  256. cred->key_len / 2) < 0) {
  257. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
  258. "%d", key_idx);
  259. return -1;
  260. }
  261. ssid->wep_key_len[key_idx] = cred->key_len / 2;
  262. } else {
  263. os_memcpy(ssid->wep_key[key_idx], cred->key,
  264. cred->key_len);
  265. ssid->wep_key_len[key_idx] = cred->key_len;
  266. }
  267. ssid->wep_tx_keyidx = key_idx;
  268. break;
  269. case WPS_ENCR_TKIP:
  270. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  271. break;
  272. case WPS_ENCR_AES:
  273. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  274. break;
  275. }
  276. switch (auth_type) {
  277. case WPS_AUTH_OPEN:
  278. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  279. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  280. ssid->proto = 0;
  281. break;
  282. case WPS_AUTH_SHARED:
  283. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  284. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  285. ssid->proto = 0;
  286. break;
  287. case WPS_AUTH_WPAPSK:
  288. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  289. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  290. ssid->proto = WPA_PROTO_WPA;
  291. break;
  292. case WPS_AUTH_WPA:
  293. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  294. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  295. ssid->proto = WPA_PROTO_WPA;
  296. break;
  297. case WPS_AUTH_WPA2:
  298. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  299. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  300. ssid->proto = WPA_PROTO_RSN;
  301. break;
  302. case WPS_AUTH_WPA2PSK:
  303. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  304. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  305. ssid->proto = WPA_PROTO_RSN;
  306. break;
  307. }
  308. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  309. if (cred->key_len == 2 * PMK_LEN) {
  310. if (hexstr2bin((const char *) cred->key, ssid->psk,
  311. PMK_LEN)) {
  312. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  313. "Key");
  314. return -1;
  315. }
  316. ssid->psk_set = 1;
  317. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  318. os_free(ssid->passphrase);
  319. ssid->passphrase = os_malloc(cred->key_len + 1);
  320. if (ssid->passphrase == NULL)
  321. return -1;
  322. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  323. ssid->passphrase[cred->key_len] = '\0';
  324. wpa_config_update_psk(ssid);
  325. } else {
  326. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  327. "length %lu",
  328. (unsigned long) cred->key_len);
  329. return -1;
  330. }
  331. }
  332. wpas_wps_security_workaround(wpa_s, ssid, cred);
  333. #ifndef CONFIG_NO_CONFIG_WRITE
  334. if (wpa_s->conf->update_config &&
  335. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  336. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  337. return -1;
  338. }
  339. #endif /* CONFIG_NO_CONFIG_WRITE */
  340. return 0;
  341. }
  342. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  343. struct wps_event_m2d *m2d)
  344. {
  345. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  346. "dev_password_id=%d config_error=%d",
  347. m2d->dev_password_id, m2d->config_error);
  348. wpas_notify_wps_event_m2d(wpa_s, m2d);
  349. }
  350. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  351. struct wps_event_fail *fail)
  352. {
  353. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
  354. wpas_clear_wps(wpa_s);
  355. wpas_notify_wps_event_fail(wpa_s, fail);
  356. }
  357. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  358. {
  359. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  360. wpa_s->wps_success = 1;
  361. wpas_notify_wps_event_success(wpa_s);
  362. }
  363. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  364. union wps_event_data *data)
  365. {
  366. struct wpa_supplicant *wpa_s = ctx;
  367. switch (event) {
  368. case WPS_EV_M2D:
  369. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  370. break;
  371. case WPS_EV_FAIL:
  372. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  373. break;
  374. case WPS_EV_SUCCESS:
  375. wpa_supplicant_wps_event_success(wpa_s);
  376. break;
  377. case WPS_EV_PWD_AUTH_FAIL:
  378. break;
  379. case WPS_EV_PBC_OVERLAP:
  380. break;
  381. case WPS_EV_PBC_TIMEOUT:
  382. break;
  383. }
  384. }
  385. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  386. {
  387. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  388. eap_is_wps_pin_enrollee(&ssid->eap))
  389. return WPS_REQ_ENROLLEE;
  390. else
  391. return WPS_REQ_REGISTRAR;
  392. }
  393. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  394. {
  395. int id;
  396. struct wpa_ssid *ssid, *remove_ssid = NULL;
  397. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  398. /* Remove any existing WPS network from configuration */
  399. ssid = wpa_s->conf->ssid;
  400. while (ssid) {
  401. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  402. if (ssid == wpa_s->current_ssid) {
  403. wpa_s->current_ssid = NULL;
  404. if (ssid != NULL)
  405. wpas_notify_network_changed(wpa_s);
  406. }
  407. id = ssid->id;
  408. remove_ssid = ssid;
  409. } else
  410. id = -1;
  411. ssid = ssid->next;
  412. if (id >= 0) {
  413. wpas_notify_network_removed(wpa_s, remove_ssid);
  414. wpa_config_remove_network(wpa_s->conf, id);
  415. }
  416. }
  417. }
  418. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  419. {
  420. struct wpa_supplicant *wpa_s = eloop_ctx;
  421. wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  422. "out");
  423. wpas_clear_wps(wpa_s);
  424. }
  425. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  426. int registrar, const u8 *bssid)
  427. {
  428. struct wpa_ssid *ssid;
  429. ssid = wpa_config_add_network(wpa_s->conf);
  430. if (ssid == NULL)
  431. return NULL;
  432. wpas_notify_network_added(wpa_s, ssid);
  433. wpa_config_set_network_defaults(ssid);
  434. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  435. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  436. wpa_config_set(ssid, "identity", registrar ?
  437. "\"" WSC_ID_REGISTRAR "\"" :
  438. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  439. wpas_notify_network_removed(wpa_s, ssid);
  440. wpa_config_remove_network(wpa_s->conf, ssid->id);
  441. return NULL;
  442. }
  443. if (bssid) {
  444. size_t i;
  445. struct wpa_scan_res *res;
  446. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  447. ssid->bssid_set = 1;
  448. /* Try to get SSID from scan results */
  449. if (wpa_s->scan_res == NULL &&
  450. wpa_supplicant_get_scan_results(wpa_s) < 0)
  451. return ssid; /* Could not find any scan results */
  452. for (i = 0; i < wpa_s->scan_res->num; i++) {
  453. const u8 *ie;
  454. res = wpa_s->scan_res->res[i];
  455. if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
  456. continue;
  457. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  458. if (ie == NULL)
  459. break;
  460. os_free(ssid->ssid);
  461. ssid->ssid = os_malloc(ie[1]);
  462. if (ssid->ssid == NULL)
  463. break;
  464. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  465. ssid->ssid_len = ie[1];
  466. break;
  467. }
  468. }
  469. return ssid;
  470. }
  471. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  472. struct wpa_ssid *selected)
  473. {
  474. struct wpa_ssid *ssid;
  475. /* Mark all other networks disabled and trigger reassociation */
  476. ssid = wpa_s->conf->ssid;
  477. while (ssid) {
  478. int was_disabled = ssid->disabled;
  479. ssid->disabled = ssid != selected;
  480. if (was_disabled != ssid->disabled)
  481. wpas_notify_network_enabled_changed(wpa_s, ssid);
  482. ssid = ssid->next;
  483. }
  484. wpa_s->disconnected = 0;
  485. wpa_s->reassociate = 1;
  486. wpa_s->scan_runs = 0;
  487. wpa_s->wps_success = 0;
  488. wpa_s->blacklist_cleared = 0;
  489. wpa_supplicant_req_scan(wpa_s, 0, 0);
  490. }
  491. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  492. {
  493. struct wpa_ssid *ssid;
  494. wpas_clear_wps(wpa_s);
  495. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  496. if (ssid == NULL)
  497. return -1;
  498. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  499. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  500. wpa_s, NULL);
  501. wpas_wps_reassoc(wpa_s, ssid);
  502. return 0;
  503. }
  504. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  505. const char *pin)
  506. {
  507. struct wpa_ssid *ssid;
  508. char val[128];
  509. unsigned int rpin = 0;
  510. wpas_clear_wps(wpa_s);
  511. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  512. if (ssid == NULL)
  513. return -1;
  514. if (pin)
  515. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  516. else {
  517. rpin = wps_generate_pin();
  518. os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
  519. }
  520. wpa_config_set(ssid, "phase1", val, 0);
  521. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  522. wpa_s, NULL);
  523. wpas_wps_reassoc(wpa_s, ssid);
  524. return rpin;
  525. }
  526. #ifdef CONFIG_WPS_OOB
  527. int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
  528. char *path, char *method, char *name)
  529. {
  530. struct wps_context *wps = wpa_s->wps;
  531. struct oob_device_data *oob_dev;
  532. oob_dev = wps_get_oob_device(device_type);
  533. if (oob_dev == NULL)
  534. return -1;
  535. oob_dev->device_path = path;
  536. oob_dev->device_name = name;
  537. wps->oob_conf.oob_method = wps_get_oob_method(method);
  538. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  539. /*
  540. * Use pre-configured DH keys in order to be able to write the
  541. * key hash into the OOB file.
  542. */
  543. wpabuf_free(wps->dh_pubkey);
  544. wpabuf_free(wps->dh_privkey);
  545. wps->dh_privkey = NULL;
  546. wps->dh_pubkey = NULL;
  547. dh5_free(wps->dh_ctx);
  548. wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
  549. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  550. if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
  551. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  552. "Diffie-Hellman handshake");
  553. return -1;
  554. }
  555. }
  556. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  557. wpas_clear_wps(wpa_s);
  558. if (wps_process_oob(wps, oob_dev, 0) < 0)
  559. return -1;
  560. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  561. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  562. wpas_wps_start_pin(wpa_s, NULL,
  563. wpabuf_head(wps->oob_conf.dev_password)) < 0)
  564. return -1;
  565. return 0;
  566. }
  567. #endif /* CONFIG_WPS_OOB */
  568. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  569. const char *pin, struct wps_new_ap_settings *settings)
  570. {
  571. struct wpa_ssid *ssid;
  572. char val[200];
  573. char *pos, *end;
  574. int res;
  575. if (!pin)
  576. return -1;
  577. wpas_clear_wps(wpa_s);
  578. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  579. if (ssid == NULL)
  580. return -1;
  581. pos = val;
  582. end = pos + sizeof(val);
  583. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  584. if (res < 0 || res >= end - pos)
  585. return -1;
  586. pos += res;
  587. if (settings) {
  588. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  589. "new_encr=%s new_key=%s",
  590. settings->ssid_hex, settings->auth,
  591. settings->encr, settings->key_hex);
  592. if (res < 0 || res >= end - pos)
  593. return -1;
  594. pos += res;
  595. }
  596. res = os_snprintf(pos, end - pos, "\"");
  597. if (res < 0 || res >= end - pos)
  598. return -1;
  599. wpa_config_set(ssid, "phase1", val, 0);
  600. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  601. wpa_s, NULL);
  602. wpas_wps_reassoc(wpa_s, ssid);
  603. return 0;
  604. }
  605. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  606. size_t psk_len)
  607. {
  608. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  609. "STA " MACSTR, MAC2STR(mac_addr));
  610. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  611. /* TODO */
  612. return 0;
  613. }
  614. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  615. const struct wps_device_data *dev)
  616. {
  617. char uuid[40], txt[400];
  618. int len;
  619. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  620. return;
  621. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  622. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  623. " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  624. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  625. dev->manufacturer, dev->model_name,
  626. dev->model_number, dev->serial_number,
  627. dev->categ, dev->oui, dev->sub_categ);
  628. if (len > 0 && len < (int) sizeof(txt))
  629. wpa_printf(MSG_INFO, "%s", txt);
  630. }
  631. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  632. {
  633. struct wps_context *wps;
  634. struct wps_registrar_config rcfg;
  635. wps = os_zalloc(sizeof(*wps));
  636. if (wps == NULL)
  637. return -1;
  638. wps->cred_cb = wpa_supplicant_wps_cred;
  639. wps->event_cb = wpa_supplicant_wps_event;
  640. wps->cb_ctx = wpa_s;
  641. wps->dev.device_name = wpa_s->conf->device_name;
  642. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  643. wps->dev.model_name = wpa_s->conf->model_name;
  644. wps->dev.model_number = wpa_s->conf->model_number;
  645. wps->dev.serial_number = wpa_s->conf->serial_number;
  646. if (wpa_s->conf->device_type) {
  647. char *pos;
  648. u8 oui[4];
  649. /* <categ>-<OUI>-<subcateg> */
  650. wps->dev.categ = atoi(wpa_s->conf->device_type);
  651. pos = os_strchr(wpa_s->conf->device_type, '-');
  652. if (pos == NULL) {
  653. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  654. os_free(wps);
  655. return -1;
  656. }
  657. pos++;
  658. if (hexstr2bin(pos, oui, 4)) {
  659. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  660. os_free(wps);
  661. return -1;
  662. }
  663. wps->dev.oui = WPA_GET_BE32(oui);
  664. pos = os_strchr(pos, '-');
  665. if (pos == NULL) {
  666. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  667. os_free(wps);
  668. return -1;
  669. }
  670. pos++;
  671. wps->dev.sub_categ = atoi(pos);
  672. }
  673. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  674. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  675. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  676. if (is_nil_uuid(wpa_s->conf->uuid)) {
  677. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  678. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  679. wps->uuid, WPS_UUID_LEN);
  680. } else
  681. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  682. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  683. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  684. os_memset(&rcfg, 0, sizeof(rcfg));
  685. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  686. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  687. rcfg.cb_ctx = wpa_s;
  688. wps->registrar = wps_registrar_init(wps, &rcfg);
  689. if (wps->registrar == NULL) {
  690. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  691. os_free(wps);
  692. return -1;
  693. }
  694. wpa_s->wps = wps;
  695. return 0;
  696. }
  697. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  698. {
  699. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  700. if (wpa_s->wps == NULL)
  701. return;
  702. wps_registrar_deinit(wpa_s->wps->registrar);
  703. wpabuf_free(wpa_s->wps->dh_pubkey);
  704. wpabuf_free(wpa_s->wps->dh_privkey);
  705. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  706. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  707. os_free(wpa_s->wps->network_key);
  708. os_free(wpa_s->wps);
  709. wpa_s->wps = NULL;
  710. #ifdef CONFIG_WPS_ER
  711. wps_er_deinit(wpa_s->wps_er);
  712. wpa_s->wps_er = NULL;
  713. #endif /* CONFIG_WPS_ER */
  714. }
  715. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  716. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  717. {
  718. struct wpabuf *wps_ie;
  719. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  720. return -1;
  721. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  722. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  723. if (!wps_ie) {
  724. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  725. return 0;
  726. }
  727. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  728. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  729. "without active PBC Registrar");
  730. wpabuf_free(wps_ie);
  731. return 0;
  732. }
  733. /* TODO: overlap detection */
  734. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  735. "(Active PBC)");
  736. wpabuf_free(wps_ie);
  737. return 1;
  738. }
  739. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  740. if (!wps_ie) {
  741. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  742. return 0;
  743. }
  744. /*
  745. * Start with WPS APs that advertise active PIN Registrar and
  746. * allow any WPS AP after third scan since some APs do not set
  747. * Selected Registrar attribute properly when using external
  748. * Registrar.
  749. */
  750. if (!wps_is_selected_pin_registrar(wps_ie)) {
  751. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  752. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  753. "without active PIN Registrar");
  754. wpabuf_free(wps_ie);
  755. return 0;
  756. }
  757. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  758. } else {
  759. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  760. "(Active PIN)");
  761. }
  762. wpabuf_free(wps_ie);
  763. return 1;
  764. }
  765. if (wps_ie) {
  766. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  767. wpabuf_free(wps_ie);
  768. return 1;
  769. }
  770. return -1;
  771. }
  772. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  773. struct wpa_ssid *ssid,
  774. struct wpa_scan_res *bss)
  775. {
  776. struct wpabuf *wps_ie = NULL;
  777. int ret = 0;
  778. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  779. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  780. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  781. /* allow wildcard SSID for WPS PBC */
  782. ret = 1;
  783. }
  784. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  785. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  786. if (wps_ie &&
  787. (wps_is_selected_pin_registrar(wps_ie) ||
  788. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  789. /* allow wildcard SSID for WPS PIN */
  790. ret = 1;
  791. }
  792. }
  793. if (!ret && ssid->bssid_set &&
  794. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  795. /* allow wildcard SSID due to hardcoded BSSID match */
  796. ret = 1;
  797. }
  798. wpabuf_free(wps_ie);
  799. return ret;
  800. }
  801. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  802. struct wpa_scan_res *selected,
  803. struct wpa_ssid *ssid)
  804. {
  805. const u8 *sel_uuid, *uuid;
  806. size_t i;
  807. struct wpabuf *wps_ie;
  808. int ret = 0;
  809. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  810. return 0;
  811. /* Make sure that only one AP is in active PBC mode */
  812. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  813. if (wps_ie)
  814. sel_uuid = wps_get_uuid_e(wps_ie);
  815. else
  816. sel_uuid = NULL;
  817. for (i = 0; i < wpa_s->scan_res->num; i++) {
  818. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  819. struct wpabuf *ie;
  820. if (bss == selected)
  821. continue;
  822. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  823. if (!ie)
  824. continue;
  825. if (!wps_is_selected_pbc_registrar(ie)) {
  826. wpabuf_free(ie);
  827. continue;
  828. }
  829. uuid = wps_get_uuid_e(ie);
  830. if (sel_uuid == NULL || uuid == NULL ||
  831. os_memcmp(sel_uuid, uuid, 16) != 0) {
  832. ret = 1; /* PBC overlap */
  833. wpabuf_free(ie);
  834. break;
  835. }
  836. /* TODO: verify that this is reasonable dual-band situation */
  837. wpabuf_free(ie);
  838. }
  839. wpabuf_free(wps_ie);
  840. return ret;
  841. }
  842. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  843. {
  844. size_t i;
  845. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  846. return;
  847. for (i = 0; i < wpa_s->scan_res->num; i++) {
  848. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  849. struct wpabuf *ie;
  850. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  851. if (!ie)
  852. continue;
  853. if (wps_is_selected_pbc_registrar(ie))
  854. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  855. else if (wps_is_selected_pin_registrar(ie))
  856. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  857. else
  858. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  859. wpabuf_free(ie);
  860. break;
  861. }
  862. }
  863. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  864. {
  865. struct wpa_ssid *ssid;
  866. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  867. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  868. return 1;
  869. }
  870. return 0;
  871. }
  872. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  873. char *end)
  874. {
  875. struct wpabuf *wps_ie;
  876. int ret;
  877. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  878. if (wps_ie == NULL)
  879. return 0;
  880. ret = wps_attr_text(wps_ie, buf, end);
  881. wpabuf_free(wps_ie);
  882. return ret;
  883. }
  884. int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
  885. {
  886. #ifdef CONFIG_WPS_ER
  887. if (wpa_s->wps_er) {
  888. /* TODO: re-send ctrl_iface events for current data? */
  889. return 0;
  890. }
  891. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
  892. if (wpa_s->wps_er == NULL)
  893. return -1;
  894. return 0;
  895. #else /* CONFIG_WPS_ER */
  896. return 0;
  897. #endif /* CONFIG_WPS_ER */
  898. }
  899. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  900. {
  901. #ifdef CONFIG_WPS_ER
  902. wps_er_deinit(wpa_s->wps_er);
  903. wpa_s->wps_er = NULL;
  904. #endif /* CONFIG_WPS_ER */
  905. return 0;
  906. }