wps_supplicant.c 37 KB

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