wps_hostapd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * hostapd / 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 "hostapd.h"
  16. #include "driver_i.h"
  17. #include "eloop.h"
  18. #include "uuid.h"
  19. #include "wpa_ctrl.h"
  20. #include "ieee802_11_defs.h"
  21. #include "wps/wps.h"
  22. #include "wps/wps_defs.h"
  23. #include "wps/wps_dev_attr.h"
  24. #include "wps_hostapd.h"
  25. static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  26. size_t psk_len)
  27. {
  28. struct hostapd_data *hapd = ctx;
  29. struct hostapd_wpa_psk *p;
  30. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  31. wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
  32. MACSTR, MAC2STR(mac_addr));
  33. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  34. if (psk_len != PMK_LEN) {
  35. wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
  36. (unsigned long) psk_len);
  37. return -1;
  38. }
  39. /* Add the new PSK to runtime PSK list */
  40. p = os_zalloc(sizeof(*p));
  41. if (p == NULL)
  42. return -1;
  43. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  44. os_memcpy(p->psk, psk, PMK_LEN);
  45. p->next = ssid->wpa_psk;
  46. ssid->wpa_psk = p;
  47. if (ssid->wpa_psk_file) {
  48. FILE *f;
  49. char hex[PMK_LEN * 2 + 1];
  50. /* Add the new PSK to PSK list file */
  51. f = fopen(ssid->wpa_psk_file, "a");
  52. if (f == NULL) {
  53. wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
  54. "'%s'", ssid->wpa_psk_file);
  55. return -1;
  56. }
  57. wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
  58. fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
  59. fclose(f);
  60. }
  61. return 0;
  62. }
  63. static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
  64. size_t beacon_ie_len, const u8 *probe_resp_ie,
  65. size_t probe_resp_ie_len)
  66. {
  67. struct hostapd_data *hapd = ctx;
  68. os_free(hapd->wps_beacon_ie);
  69. if (beacon_ie_len == 0) {
  70. hapd->wps_beacon_ie = NULL;
  71. hapd->wps_beacon_ie_len = 0;
  72. } else {
  73. hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
  74. if (hapd->wps_beacon_ie == NULL) {
  75. hapd->wps_beacon_ie_len = 0;
  76. return -1;
  77. }
  78. os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
  79. hapd->wps_beacon_ie_len = beacon_ie_len;
  80. }
  81. hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
  82. hapd->wps_beacon_ie_len);
  83. os_free(hapd->wps_probe_resp_ie);
  84. if (probe_resp_ie_len == 0) {
  85. hapd->wps_probe_resp_ie = NULL;
  86. hapd->wps_probe_resp_ie_len = 0;
  87. } else {
  88. hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
  89. if (hapd->wps_probe_resp_ie == NULL) {
  90. hapd->wps_probe_resp_ie_len = 0;
  91. return -1;
  92. }
  93. os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
  94. probe_resp_ie_len);
  95. hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
  96. }
  97. hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
  98. hapd->wps_probe_resp_ie_len);
  99. return 0;
  100. }
  101. static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  102. const struct wps_device_data *dev)
  103. {
  104. struct hostapd_data *hapd = ctx;
  105. char uuid[40], txt[400];
  106. int len;
  107. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  108. return;
  109. wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
  110. len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
  111. "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  112. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  113. dev->manufacturer, dev->model_name,
  114. dev->model_number, dev->serial_number,
  115. dev->categ, dev->oui, dev->sub_categ);
  116. if (len > 0 && len < (int) sizeof(txt))
  117. wpa_msg(hapd, MSG_INFO, "%s", txt);
  118. if (hapd->conf->wps_pin_requests) {
  119. FILE *f;
  120. struct os_time t;
  121. f = fopen(hapd->conf->wps_pin_requests, "a");
  122. if (f == NULL)
  123. return;
  124. os_get_time(&t);
  125. fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
  126. "\t%d-%08X-%d\n",
  127. t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
  128. dev->manufacturer, dev->model_name, dev->model_number,
  129. dev->serial_number,
  130. dev->categ, dev->oui, dev->sub_categ);
  131. fclose(f);
  132. }
  133. }
  134. static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  135. const u8 *uuid_e)
  136. {
  137. struct hostapd_data *hapd = ctx;
  138. char uuid[40];
  139. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  140. return;
  141. wpa_msg(hapd, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
  142. MAC2STR(mac_addr), uuid);
  143. }
  144. static int str_starts(const char *str, const char *start)
  145. {
  146. return os_strncmp(str, start, os_strlen(start)) == 0;
  147. }
  148. static void wps_reload_config(void *eloop_data, void *user_ctx)
  149. {
  150. struct hostapd_iface *iface = eloop_data;
  151. wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
  152. if (hostapd_reload_config(iface) < 0) {
  153. wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
  154. "configuration");
  155. }
  156. }
  157. static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
  158. {
  159. struct hostapd_data *hapd = ctx;
  160. FILE *oconf, *nconf;
  161. size_t len, i;
  162. char *tmp_fname;
  163. char buf[1024];
  164. int multi_bss;
  165. int wpa;
  166. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  167. cred->cred_attr, cred->cred_attr_len);
  168. wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
  169. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  170. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  171. cred->auth_type);
  172. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  173. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  174. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  175. cred->key, cred->key_len);
  176. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  177. MAC2STR(cred->mac_addr));
  178. if ((hapd->conf->wps_cred_processing == 1 ||
  179. hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
  180. size_t blen = cred->cred_attr_len * 2 + 1;
  181. char *buf = os_malloc(blen);
  182. if (buf) {
  183. wpa_snprintf_hex(buf, blen,
  184. cred->cred_attr, cred->cred_attr_len);
  185. wpa_msg(hapd, MSG_INFO, "%s%s",
  186. WPS_EVENT_NEW_AP_SETTINGS, buf);
  187. os_free(buf);
  188. }
  189. } else
  190. wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
  191. if (hapd->conf->wps_cred_processing == 1)
  192. return 0;
  193. len = os_strlen(hapd->iface->config_fname) + 5;
  194. tmp_fname = os_malloc(len);
  195. if (tmp_fname == NULL)
  196. return -1;
  197. os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
  198. oconf = fopen(hapd->iface->config_fname, "r");
  199. if (oconf == NULL) {
  200. wpa_printf(MSG_WARNING, "WPS: Could not open current "
  201. "configuration file");
  202. os_free(tmp_fname);
  203. return -1;
  204. }
  205. nconf = fopen(tmp_fname, "w");
  206. if (nconf == NULL) {
  207. wpa_printf(MSG_WARNING, "WPS: Could not write updated "
  208. "configuration file");
  209. os_free(tmp_fname);
  210. fclose(oconf);
  211. return -1;
  212. }
  213. fprintf(nconf, "# WPS configuration - START\n");
  214. fprintf(nconf, "wps_state=2\n");
  215. fprintf(nconf, "ssid=");
  216. for (i = 0; i < cred->ssid_len; i++)
  217. fputc(cred->ssid[i], nconf);
  218. fprintf(nconf, "\n");
  219. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  220. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  221. wpa = 3;
  222. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  223. wpa = 2;
  224. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  225. wpa = 1;
  226. else
  227. wpa = 0;
  228. if (wpa) {
  229. char *prefix;
  230. fprintf(nconf, "wpa=%d\n", wpa);
  231. fprintf(nconf, "wpa_key_mgmt=");
  232. prefix = "";
  233. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
  234. fprintf(nconf, "WPA-EAP");
  235. prefix = " ";
  236. }
  237. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  238. fprintf(nconf, "%sWPA-PSK", prefix);
  239. fprintf(nconf, "\n");
  240. fprintf(nconf, "wpa_pairwise=");
  241. prefix = "";
  242. if (cred->encr_type & WPS_ENCR_AES) {
  243. fprintf(nconf, "CCMP");
  244. prefix = " ";
  245. }
  246. if (cred->encr_type & WPS_ENCR_TKIP) {
  247. fprintf(nconf, "%sTKIP", prefix);
  248. }
  249. fprintf(nconf, "\n");
  250. if (cred->key_len >= 8 && cred->key_len < 64) {
  251. fprintf(nconf, "wpa_passphrase=");
  252. for (i = 0; i < cred->key_len; i++)
  253. fputc(cred->key[i], nconf);
  254. fprintf(nconf, "\n");
  255. } else if (cred->key_len == 64) {
  256. fprintf(nconf, "wpa_psk=");
  257. for (i = 0; i < cred->key_len; i++)
  258. fputc(cred->key[i], nconf);
  259. fprintf(nconf, "\n");
  260. } else {
  261. wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
  262. "for WPA/WPA2",
  263. (unsigned long) cred->key_len);
  264. }
  265. fprintf(nconf, "auth_algs=1\n");
  266. } else {
  267. if ((cred->auth_type & WPS_AUTH_OPEN) &&
  268. (cred->auth_type & WPS_AUTH_SHARED))
  269. fprintf(nconf, "auth_algs=3\n");
  270. else if (cred->auth_type & WPS_AUTH_SHARED)
  271. fprintf(nconf, "auth_algs=2\n");
  272. else
  273. fprintf(nconf, "auth_algs=1\n");
  274. if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
  275. fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
  276. fprintf(nconf, "wep_key%d=", cred->key_idx);
  277. if (cred->key_len != 10 && cred->key_len != 26)
  278. fputc('"', nconf);
  279. for (i = 0; i < cred->key_len; i++)
  280. fputc(cred->key[i], nconf);
  281. if (cred->key_len != 10 && cred->key_len != 26)
  282. fputc('"', nconf);
  283. fprintf(nconf, "\n");
  284. }
  285. }
  286. fprintf(nconf, "# WPS configuration - END\n");
  287. multi_bss = 0;
  288. while (fgets(buf, sizeof(buf), oconf)) {
  289. if (os_strncmp(buf, "bss=", 4) == 0)
  290. multi_bss = 1;
  291. if (!multi_bss &&
  292. (str_starts(buf, "ssid=") ||
  293. str_starts(buf, "auth_algs=") ||
  294. str_starts(buf, "wps_state=") ||
  295. str_starts(buf, "wpa=") ||
  296. str_starts(buf, "wpa_psk=") ||
  297. str_starts(buf, "wpa_pairwise=") ||
  298. str_starts(buf, "rsn_pairwise=") ||
  299. str_starts(buf, "wpa_key_mgmt=") ||
  300. str_starts(buf, "wpa_passphrase="))) {
  301. fprintf(nconf, "#WPS# %s", buf);
  302. } else
  303. fprintf(nconf, "%s", buf);
  304. }
  305. fclose(nconf);
  306. fclose(oconf);
  307. if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
  308. wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
  309. "configuration file: %s", strerror(errno));
  310. os_free(tmp_fname);
  311. return -1;
  312. }
  313. os_free(tmp_fname);
  314. /* Schedule configuration reload after short period of time to allow
  315. * EAP-WSC to be finished.
  316. */
  317. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  318. NULL);
  319. /* TODO: dualband AP may need to update multiple configuration files */
  320. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  321. return 0;
  322. }
  323. static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
  324. {
  325. os_free(hapd->wps_beacon_ie);
  326. hapd->wps_beacon_ie = NULL;
  327. hapd->wps_beacon_ie_len = 0;
  328. hostapd_set_wps_beacon_ie(hapd, NULL, 0);
  329. os_free(hapd->wps_probe_resp_ie);
  330. hapd->wps_probe_resp_ie = NULL;
  331. hapd->wps_probe_resp_ie_len = 0;
  332. hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
  333. }
  334. int hostapd_init_wps(struct hostapd_data *hapd,
  335. struct hostapd_bss_config *conf)
  336. {
  337. struct wps_context *wps;
  338. struct wps_registrar_config cfg;
  339. if (conf->wps_state == 0) {
  340. hostapd_wps_clear_ies(hapd);
  341. return 0;
  342. }
  343. wps = os_zalloc(sizeof(*wps));
  344. if (wps == NULL)
  345. return -1;
  346. wps->cred_cb = hostapd_wps_cred_cb;
  347. wps->cb_ctx = hapd;
  348. os_memset(&cfg, 0, sizeof(cfg));
  349. wps->wps_state = hapd->conf->wps_state;
  350. wps->ap_setup_locked = hapd->conf->ap_setup_locked;
  351. if (is_nil_uuid(hapd->conf->uuid)) {
  352. uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
  353. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  354. wps->uuid, UUID_LEN);
  355. } else
  356. os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
  357. wps->ssid_len = hapd->conf->ssid.ssid_len;
  358. os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
  359. wps->ap = 1;
  360. os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
  361. wps->dev.device_name = os_strdup(hapd->conf->device_name);
  362. wps->dev.manufacturer = os_strdup(hapd->conf->manufacturer);
  363. wps->dev.model_name = os_strdup(hapd->conf->model_name);
  364. wps->dev.model_number = os_strdup(hapd->conf->model_number);
  365. wps->dev.serial_number = os_strdup(hapd->conf->serial_number);
  366. if (hapd->conf->config_methods) {
  367. char *m = hapd->conf->config_methods;
  368. if (os_strstr(m, "label"))
  369. wps->config_methods |= WPS_CONFIG_LABEL;
  370. if (os_strstr(m, "display"))
  371. wps->config_methods |= WPS_CONFIG_DISPLAY;
  372. if (os_strstr(m, "push_button"))
  373. wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
  374. if (os_strstr(m, "keypad"))
  375. wps->config_methods |= WPS_CONFIG_KEYPAD;
  376. }
  377. if (hapd->conf->device_type) {
  378. char *pos;
  379. u8 oui[4];
  380. /* <categ>-<OUI>-<subcateg> */
  381. wps->dev.categ = atoi(hapd->conf->device_type);
  382. pos = os_strchr(hapd->conf->device_type, '-');
  383. if (pos == NULL) {
  384. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  385. os_free(wps);
  386. return -1;
  387. }
  388. pos++;
  389. if (hexstr2bin(pos, oui, 4)) {
  390. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  391. os_free(wps);
  392. return -1;
  393. }
  394. wps->dev.oui = WPA_GET_BE32(oui);
  395. pos = os_strchr(pos, '-');
  396. if (pos == NULL) {
  397. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  398. os_free(wps);
  399. return -1;
  400. }
  401. pos++;
  402. wps->dev.sub_categ = atoi(pos);
  403. }
  404. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  405. wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  406. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  407. if (conf->wpa & WPA_PROTO_RSN) {
  408. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  409. wps->auth_types |= WPS_AUTH_WPA2PSK;
  410. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  411. wps->auth_types |= WPS_AUTH_WPA2;
  412. if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
  413. wps->encr_types |= WPS_ENCR_AES;
  414. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  415. wps->encr_types |= WPS_ENCR_TKIP;
  416. }
  417. if (conf->wpa & WPA_PROTO_WPA) {
  418. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  419. wps->auth_types |= WPS_AUTH_WPAPSK;
  420. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  421. wps->auth_types |= WPS_AUTH_WPA;
  422. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  423. wps->encr_types |= WPS_ENCR_AES;
  424. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  425. wps->encr_types |= WPS_ENCR_TKIP;
  426. }
  427. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  428. wps->encr_types |= WPS_ENCR_NONE;
  429. wps->auth_types |= WPS_AUTH_OPEN;
  430. } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
  431. wps->encr_types |= WPS_ENCR_WEP;
  432. if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
  433. wps->auth_types |= WPS_AUTH_OPEN;
  434. if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
  435. wps->auth_types |= WPS_AUTH_SHARED;
  436. } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
  437. wps->auth_types |= WPS_AUTH_OPEN;
  438. if (conf->default_wep_key_len)
  439. wps->encr_types |= WPS_ENCR_WEP;
  440. else
  441. wps->encr_types |= WPS_ENCR_NONE;
  442. }
  443. if (conf->ssid.wpa_psk_file) {
  444. /* Use per-device PSKs */
  445. } else if (conf->ssid.wpa_passphrase) {
  446. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  447. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  448. } else if (conf->ssid.wpa_psk) {
  449. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  450. if (wps->network_key == NULL) {
  451. os_free(wps);
  452. return -1;
  453. }
  454. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  455. conf->ssid.wpa_psk->psk, PMK_LEN);
  456. wps->network_key_len = 2 * PMK_LEN;
  457. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  458. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  459. if (wps->network_key == NULL) {
  460. os_free(wps);
  461. return -1;
  462. }
  463. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  464. conf->ssid.wep.len[0]);
  465. wps->network_key_len = conf->ssid.wep.len[0];
  466. }
  467. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  468. /* Override parameters to enable security by default */
  469. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  470. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  471. }
  472. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  473. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  474. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  475. cfg.reg_success_cb = hostapd_wps_reg_success_cb;
  476. cfg.cb_ctx = hapd;
  477. cfg.skip_cred_build = conf->skip_cred_build;
  478. cfg.extra_cred = conf->extra_cred;
  479. cfg.extra_cred_len = conf->extra_cred_len;
  480. cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
  481. conf->skip_cred_build;
  482. wps->registrar = wps_registrar_init(wps, &cfg);
  483. if (wps->registrar == NULL) {
  484. printf("Failed to initialize WPS Registrar\n");
  485. os_free(wps->network_key);
  486. os_free(wps);
  487. return -1;
  488. }
  489. hapd->wps = wps;
  490. return 0;
  491. }
  492. void hostapd_deinit_wps(struct hostapd_data *hapd)
  493. {
  494. if (hapd->wps == NULL)
  495. return;
  496. wps_registrar_deinit(hapd->wps->registrar);
  497. os_free(hapd->wps->network_key);
  498. wps_device_data_free(&hapd->wps->dev);
  499. os_free(hapd->wps);
  500. hapd->wps = NULL;
  501. hostapd_wps_clear_ies(hapd);
  502. }
  503. int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
  504. const char *pin)
  505. {
  506. u8 u[UUID_LEN];
  507. int any = 0;
  508. if (hapd->wps == NULL)
  509. return -1;
  510. if (os_strcmp(uuid, "any") == 0)
  511. any = 1;
  512. else if (uuid_str2bin(uuid, u))
  513. return -1;
  514. return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
  515. (const u8 *) pin, os_strlen(pin));
  516. }
  517. int hostapd_wps_button_pushed(struct hostapd_data *hapd)
  518. {
  519. if (hapd->wps == NULL)
  520. return -1;
  521. return wps_registrar_button_pushed(hapd->wps->registrar);
  522. }
  523. void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
  524. const u8 *ie, size_t ie_len)
  525. {
  526. struct wpabuf *wps_ie;
  527. const u8 *end, *pos, *wps;
  528. if (hapd->wps == NULL)
  529. return;
  530. pos = ie;
  531. end = ie + ie_len;
  532. wps = NULL;
  533. while (pos + 1 < end) {
  534. if (pos + 2 + pos[1] > end)
  535. return;
  536. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  537. WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
  538. wps = pos;
  539. break;
  540. }
  541. pos += 2 + pos[1];
  542. }
  543. if (wps == NULL)
  544. return; /* No WPS IE in Probe Request */
  545. wps_ie = wpabuf_alloc(ie_len);
  546. if (wps_ie == NULL)
  547. return;
  548. /* There may be multiple WPS IEs in the message, so need to concatenate
  549. * their WPS Data fields */
  550. while (pos + 1 < end) {
  551. if (pos + 2 + pos[1] > end)
  552. break;
  553. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  554. WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
  555. wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
  556. pos += 2 + pos[1];
  557. }
  558. if (wpabuf_len(wps_ie) > 0)
  559. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
  560. wpabuf_free(wps_ie);
  561. }