wps_supplicant.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. }
  380. }
  381. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  382. {
  383. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  384. eap_is_wps_pin_enrollee(&ssid->eap))
  385. return WPS_REQ_ENROLLEE;
  386. else
  387. return WPS_REQ_REGISTRAR;
  388. }
  389. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  390. {
  391. int id;
  392. struct wpa_ssid *ssid, *remove_ssid = NULL;
  393. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  394. /* Remove any existing WPS network from configuration */
  395. ssid = wpa_s->conf->ssid;
  396. while (ssid) {
  397. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  398. if (ssid == wpa_s->current_ssid) {
  399. wpa_s->current_ssid = NULL;
  400. if (ssid != NULL)
  401. wpas_notify_network_changed(wpa_s);
  402. }
  403. id = ssid->id;
  404. remove_ssid = ssid;
  405. } else
  406. id = -1;
  407. ssid = ssid->next;
  408. if (id >= 0) {
  409. wpas_notify_network_removed(wpa_s, remove_ssid);
  410. wpa_config_remove_network(wpa_s->conf, id);
  411. }
  412. }
  413. }
  414. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  415. {
  416. struct wpa_supplicant *wpa_s = eloop_ctx;
  417. wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  418. "out");
  419. wpas_clear_wps(wpa_s);
  420. }
  421. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  422. int registrar, const u8 *bssid)
  423. {
  424. struct wpa_ssid *ssid;
  425. ssid = wpa_config_add_network(wpa_s->conf);
  426. if (ssid == NULL)
  427. return NULL;
  428. wpas_notify_network_added(wpa_s, ssid);
  429. wpa_config_set_network_defaults(ssid);
  430. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  431. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  432. wpa_config_set(ssid, "identity", registrar ?
  433. "\"" WSC_ID_REGISTRAR "\"" :
  434. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  435. wpas_notify_network_removed(wpa_s, ssid);
  436. wpa_config_remove_network(wpa_s->conf, ssid->id);
  437. return NULL;
  438. }
  439. if (bssid) {
  440. size_t i;
  441. struct wpa_scan_res *res;
  442. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  443. ssid->bssid_set = 1;
  444. /* Try to get SSID from scan results */
  445. if (wpa_s->scan_res == NULL &&
  446. wpa_supplicant_get_scan_results(wpa_s) < 0)
  447. return ssid; /* Could not find any scan results */
  448. for (i = 0; i < wpa_s->scan_res->num; i++) {
  449. const u8 *ie;
  450. res = wpa_s->scan_res->res[i];
  451. if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
  452. continue;
  453. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  454. if (ie == NULL)
  455. break;
  456. os_free(ssid->ssid);
  457. ssid->ssid = os_malloc(ie[1]);
  458. if (ssid->ssid == NULL)
  459. break;
  460. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  461. ssid->ssid_len = ie[1];
  462. break;
  463. }
  464. }
  465. return ssid;
  466. }
  467. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  468. struct wpa_ssid *selected)
  469. {
  470. struct wpa_ssid *ssid;
  471. /* Mark all other networks disabled and trigger reassociation */
  472. ssid = wpa_s->conf->ssid;
  473. while (ssid) {
  474. int was_disabled = ssid->disabled;
  475. ssid->disabled = ssid != selected;
  476. if (was_disabled != ssid->disabled)
  477. wpas_notify_network_enabled_changed(wpa_s, ssid);
  478. ssid = ssid->next;
  479. }
  480. wpa_s->disconnected = 0;
  481. wpa_s->reassociate = 1;
  482. wpa_s->scan_runs = 0;
  483. wpa_s->wps_success = 0;
  484. wpa_s->blacklist_cleared = 0;
  485. wpa_supplicant_req_scan(wpa_s, 0, 0);
  486. }
  487. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  488. {
  489. struct wpa_ssid *ssid;
  490. wpas_clear_wps(wpa_s);
  491. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  492. if (ssid == NULL)
  493. return -1;
  494. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  495. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  496. wpa_s, NULL);
  497. wpas_wps_reassoc(wpa_s, ssid);
  498. return 0;
  499. }
  500. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  501. const char *pin)
  502. {
  503. struct wpa_ssid *ssid;
  504. char val[128];
  505. unsigned int rpin = 0;
  506. wpas_clear_wps(wpa_s);
  507. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  508. if (ssid == NULL)
  509. return -1;
  510. if (pin)
  511. os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
  512. else {
  513. rpin = wps_generate_pin();
  514. os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
  515. }
  516. wpa_config_set(ssid, "phase1", val, 0);
  517. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  518. wpa_s, NULL);
  519. wpas_wps_reassoc(wpa_s, ssid);
  520. return rpin;
  521. }
  522. #ifdef CONFIG_WPS_OOB
  523. int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
  524. char *path, char *method, char *name)
  525. {
  526. struct wps_context *wps = wpa_s->wps;
  527. struct oob_device_data *oob_dev;
  528. oob_dev = wps_get_oob_device(device_type);
  529. if (oob_dev == NULL)
  530. return -1;
  531. oob_dev->device_path = path;
  532. oob_dev->device_name = name;
  533. wps->oob_conf.oob_method = wps_get_oob_method(method);
  534. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  535. /*
  536. * Use pre-configured DH keys in order to be able to write the
  537. * key hash into the OOB file.
  538. */
  539. wpabuf_free(wps->dh_pubkey);
  540. wpabuf_free(wps->dh_privkey);
  541. wps->dh_privkey = NULL;
  542. wps->dh_pubkey = NULL;
  543. dh5_free(wps->dh_ctx);
  544. wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
  545. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  546. if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
  547. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  548. "Diffie-Hellman handshake");
  549. return -1;
  550. }
  551. }
  552. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  553. wpas_clear_wps(wpa_s);
  554. if (wps_process_oob(wps, oob_dev, 0) < 0)
  555. return -1;
  556. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  557. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  558. wpas_wps_start_pin(wpa_s, NULL,
  559. wpabuf_head(wps->oob_conf.dev_password)) < 0)
  560. return -1;
  561. return 0;
  562. }
  563. #endif /* CONFIG_WPS_OOB */
  564. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  565. const char *pin, struct wps_new_ap_settings *settings)
  566. {
  567. struct wpa_ssid *ssid;
  568. char val[200];
  569. char *pos, *end;
  570. int res;
  571. if (!pin)
  572. return -1;
  573. wpas_clear_wps(wpa_s);
  574. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  575. if (ssid == NULL)
  576. return -1;
  577. pos = val;
  578. end = pos + sizeof(val);
  579. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  580. if (res < 0 || res >= end - pos)
  581. return -1;
  582. pos += res;
  583. if (settings) {
  584. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  585. "new_encr=%s new_key=%s",
  586. settings->ssid_hex, settings->auth,
  587. settings->encr, settings->key_hex);
  588. if (res < 0 || res >= end - pos)
  589. return -1;
  590. pos += res;
  591. }
  592. res = os_snprintf(pos, end - pos, "\"");
  593. if (res < 0 || res >= end - pos)
  594. return -1;
  595. wpa_config_set(ssid, "phase1", val, 0);
  596. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  597. wpa_s, NULL);
  598. wpas_wps_reassoc(wpa_s, ssid);
  599. return 0;
  600. }
  601. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  602. size_t psk_len)
  603. {
  604. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  605. "STA " MACSTR, MAC2STR(mac_addr));
  606. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  607. /* TODO */
  608. return 0;
  609. }
  610. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  611. const struct wps_device_data *dev)
  612. {
  613. char uuid[40], txt[400];
  614. int len;
  615. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  616. return;
  617. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  618. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  619. " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  620. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  621. dev->manufacturer, dev->model_name,
  622. dev->model_number, dev->serial_number,
  623. dev->categ, dev->oui, dev->sub_categ);
  624. if (len > 0 && len < (int) sizeof(txt))
  625. wpa_printf(MSG_INFO, "%s", txt);
  626. }
  627. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  628. {
  629. struct wps_context *wps;
  630. struct wps_registrar_config rcfg;
  631. wps = os_zalloc(sizeof(*wps));
  632. if (wps == NULL)
  633. return -1;
  634. wps->cred_cb = wpa_supplicant_wps_cred;
  635. wps->event_cb = wpa_supplicant_wps_event;
  636. wps->cb_ctx = wpa_s;
  637. wps->dev.device_name = wpa_s->conf->device_name;
  638. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  639. wps->dev.model_name = wpa_s->conf->model_name;
  640. wps->dev.model_number = wpa_s->conf->model_number;
  641. wps->dev.serial_number = wpa_s->conf->serial_number;
  642. if (wpa_s->conf->device_type) {
  643. char *pos;
  644. u8 oui[4];
  645. /* <categ>-<OUI>-<subcateg> */
  646. wps->dev.categ = atoi(wpa_s->conf->device_type);
  647. pos = os_strchr(wpa_s->conf->device_type, '-');
  648. if (pos == NULL) {
  649. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  650. os_free(wps);
  651. return -1;
  652. }
  653. pos++;
  654. if (hexstr2bin(pos, oui, 4)) {
  655. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  656. os_free(wps);
  657. return -1;
  658. }
  659. wps->dev.oui = WPA_GET_BE32(oui);
  660. pos = os_strchr(pos, '-');
  661. if (pos == NULL) {
  662. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  663. os_free(wps);
  664. return -1;
  665. }
  666. pos++;
  667. wps->dev.sub_categ = atoi(pos);
  668. }
  669. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  670. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  671. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  672. if (is_nil_uuid(wpa_s->conf->uuid)) {
  673. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  674. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  675. wps->uuid, WPS_UUID_LEN);
  676. } else
  677. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  678. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  679. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  680. os_memset(&rcfg, 0, sizeof(rcfg));
  681. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  682. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  683. rcfg.cb_ctx = wpa_s;
  684. wps->registrar = wps_registrar_init(wps, &rcfg);
  685. if (wps->registrar == NULL) {
  686. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  687. os_free(wps);
  688. return -1;
  689. }
  690. wpa_s->wps = wps;
  691. return 0;
  692. }
  693. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  694. {
  695. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  696. if (wpa_s->wps == NULL)
  697. return;
  698. wps_registrar_deinit(wpa_s->wps->registrar);
  699. wpabuf_free(wpa_s->wps->dh_pubkey);
  700. wpabuf_free(wpa_s->wps->dh_privkey);
  701. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  702. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  703. os_free(wpa_s->wps->network_key);
  704. os_free(wpa_s->wps);
  705. wpa_s->wps = NULL;
  706. }
  707. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  708. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  709. {
  710. struct wpabuf *wps_ie;
  711. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  712. return -1;
  713. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  714. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  715. if (!wps_ie) {
  716. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  717. return 0;
  718. }
  719. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  720. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  721. "without active PBC Registrar");
  722. wpabuf_free(wps_ie);
  723. return 0;
  724. }
  725. /* TODO: overlap detection */
  726. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  727. "(Active PBC)");
  728. wpabuf_free(wps_ie);
  729. return 1;
  730. }
  731. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  732. if (!wps_ie) {
  733. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  734. return 0;
  735. }
  736. /*
  737. * Start with WPS APs that advertise active PIN Registrar and
  738. * allow any WPS AP after third scan since some APs do not set
  739. * Selected Registrar attribute properly when using external
  740. * Registrar.
  741. */
  742. if (!wps_is_selected_pin_registrar(wps_ie)) {
  743. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  744. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  745. "without active PIN Registrar");
  746. wpabuf_free(wps_ie);
  747. return 0;
  748. }
  749. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  750. } else {
  751. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  752. "(Active PIN)");
  753. }
  754. wpabuf_free(wps_ie);
  755. return 1;
  756. }
  757. if (wps_ie) {
  758. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  759. wpabuf_free(wps_ie);
  760. return 1;
  761. }
  762. return -1;
  763. }
  764. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  765. struct wpa_ssid *ssid,
  766. struct wpa_scan_res *bss)
  767. {
  768. struct wpabuf *wps_ie = NULL;
  769. int ret = 0;
  770. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  771. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  772. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  773. /* allow wildcard SSID for WPS PBC */
  774. ret = 1;
  775. }
  776. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  777. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  778. if (wps_ie &&
  779. (wps_is_selected_pin_registrar(wps_ie) ||
  780. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  781. /* allow wildcard SSID for WPS PIN */
  782. ret = 1;
  783. }
  784. }
  785. if (!ret && ssid->bssid_set &&
  786. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  787. /* allow wildcard SSID due to hardcoded BSSID match */
  788. ret = 1;
  789. }
  790. wpabuf_free(wps_ie);
  791. return ret;
  792. }
  793. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  794. struct wpa_scan_res *selected,
  795. struct wpa_ssid *ssid)
  796. {
  797. const u8 *sel_uuid, *uuid;
  798. size_t i;
  799. struct wpabuf *wps_ie;
  800. int ret = 0;
  801. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  802. return 0;
  803. /* Make sure that only one AP is in active PBC mode */
  804. wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  805. if (wps_ie)
  806. sel_uuid = wps_get_uuid_e(wps_ie);
  807. else
  808. sel_uuid = NULL;
  809. for (i = 0; i < wpa_s->scan_res->num; i++) {
  810. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  811. struct wpabuf *ie;
  812. if (bss == selected)
  813. continue;
  814. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  815. if (!ie)
  816. continue;
  817. if (!wps_is_selected_pbc_registrar(ie)) {
  818. wpabuf_free(ie);
  819. continue;
  820. }
  821. uuid = wps_get_uuid_e(ie);
  822. if (sel_uuid == NULL || uuid == NULL ||
  823. os_memcmp(sel_uuid, uuid, 16) != 0) {
  824. ret = 1; /* PBC overlap */
  825. wpabuf_free(ie);
  826. break;
  827. }
  828. /* TODO: verify that this is reasonable dual-band situation */
  829. wpabuf_free(ie);
  830. }
  831. wpabuf_free(wps_ie);
  832. return ret;
  833. }
  834. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  835. {
  836. size_t i;
  837. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  838. return;
  839. for (i = 0; i < wpa_s->scan_res->num; i++) {
  840. struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
  841. struct wpabuf *ie;
  842. ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  843. if (!ie)
  844. continue;
  845. if (wps_is_selected_pbc_registrar(ie))
  846. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  847. else if (wps_is_selected_pin_registrar(ie))
  848. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  849. else
  850. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  851. wpabuf_free(ie);
  852. break;
  853. }
  854. }
  855. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  856. {
  857. struct wpa_ssid *ssid;
  858. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  859. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  860. return 1;
  861. }
  862. return 0;
  863. }
  864. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  865. char *end)
  866. {
  867. struct wpabuf *wps_ie;
  868. int ret;
  869. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  870. if (wps_ie == NULL)
  871. return 0;
  872. ret = wps_attr_text(wps_ie, buf, end);
  873. wpabuf_free(wps_ie);
  874. return ret;
  875. }