wps_hostapd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 = hapd->conf->device_name ?
  362. os_strdup(hapd->conf->device_name) : NULL;
  363. wps->dev.manufacturer = hapd->conf->manufacturer ?
  364. os_strdup(hapd->conf->manufacturer) : NULL;
  365. wps->dev.model_name = hapd->conf->model_name ?
  366. os_strdup(hapd->conf->model_name) : NULL;
  367. wps->dev.model_number = hapd->conf->model_number ?
  368. os_strdup(hapd->conf->model_number) : NULL;
  369. wps->dev.serial_number = hapd->conf->serial_number ?
  370. os_strdup(hapd->conf->serial_number) : NULL;
  371. if (hapd->conf->config_methods) {
  372. char *m = hapd->conf->config_methods;
  373. if (os_strstr(m, "label"))
  374. wps->config_methods |= WPS_CONFIG_LABEL;
  375. if (os_strstr(m, "display"))
  376. wps->config_methods |= WPS_CONFIG_DISPLAY;
  377. if (os_strstr(m, "push_button"))
  378. wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
  379. if (os_strstr(m, "keypad"))
  380. wps->config_methods |= WPS_CONFIG_KEYPAD;
  381. }
  382. if (hapd->conf->device_type) {
  383. char *pos;
  384. u8 oui[4];
  385. /* <categ>-<OUI>-<subcateg> */
  386. wps->dev.categ = atoi(hapd->conf->device_type);
  387. pos = os_strchr(hapd->conf->device_type, '-');
  388. if (pos == NULL) {
  389. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  390. os_free(wps);
  391. return -1;
  392. }
  393. pos++;
  394. if (hexstr2bin(pos, oui, 4)) {
  395. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  396. os_free(wps);
  397. return -1;
  398. }
  399. wps->dev.oui = WPA_GET_BE32(oui);
  400. pos = os_strchr(pos, '-');
  401. if (pos == NULL) {
  402. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  403. os_free(wps);
  404. return -1;
  405. }
  406. pos++;
  407. wps->dev.sub_categ = atoi(pos);
  408. }
  409. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  410. wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  411. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  412. if (conf->wpa & WPA_PROTO_RSN) {
  413. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  414. wps->auth_types |= WPS_AUTH_WPA2PSK;
  415. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  416. wps->auth_types |= WPS_AUTH_WPA2;
  417. if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
  418. wps->encr_types |= WPS_ENCR_AES;
  419. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  420. wps->encr_types |= WPS_ENCR_TKIP;
  421. }
  422. if (conf->wpa & WPA_PROTO_WPA) {
  423. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  424. wps->auth_types |= WPS_AUTH_WPAPSK;
  425. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  426. wps->auth_types |= WPS_AUTH_WPA;
  427. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  428. wps->encr_types |= WPS_ENCR_AES;
  429. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  430. wps->encr_types |= WPS_ENCR_TKIP;
  431. }
  432. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  433. wps->encr_types |= WPS_ENCR_NONE;
  434. wps->auth_types |= WPS_AUTH_OPEN;
  435. } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
  436. wps->encr_types |= WPS_ENCR_WEP;
  437. if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
  438. wps->auth_types |= WPS_AUTH_OPEN;
  439. if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
  440. wps->auth_types |= WPS_AUTH_SHARED;
  441. } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
  442. wps->auth_types |= WPS_AUTH_OPEN;
  443. if (conf->default_wep_key_len)
  444. wps->encr_types |= WPS_ENCR_WEP;
  445. else
  446. wps->encr_types |= WPS_ENCR_NONE;
  447. }
  448. if (conf->ssid.wpa_psk_file) {
  449. /* Use per-device PSKs */
  450. } else if (conf->ssid.wpa_passphrase) {
  451. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  452. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  453. } else if (conf->ssid.wpa_psk) {
  454. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  455. if (wps->network_key == NULL) {
  456. os_free(wps);
  457. return -1;
  458. }
  459. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  460. conf->ssid.wpa_psk->psk, PMK_LEN);
  461. wps->network_key_len = 2 * PMK_LEN;
  462. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  463. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  464. if (wps->network_key == NULL) {
  465. os_free(wps);
  466. return -1;
  467. }
  468. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  469. conf->ssid.wep.len[0]);
  470. wps->network_key_len = conf->ssid.wep.len[0];
  471. }
  472. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  473. /* Override parameters to enable security by default */
  474. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  475. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  476. }
  477. wps->ap_settings = conf->ap_settings;
  478. wps->ap_settings_len = conf->ap_settings_len;
  479. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  480. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  481. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  482. cfg.reg_success_cb = hostapd_wps_reg_success_cb;
  483. cfg.cb_ctx = hapd;
  484. cfg.skip_cred_build = conf->skip_cred_build;
  485. cfg.extra_cred = conf->extra_cred;
  486. cfg.extra_cred_len = conf->extra_cred_len;
  487. cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
  488. conf->skip_cred_build;
  489. wps->registrar = wps_registrar_init(wps, &cfg);
  490. if (wps->registrar == NULL) {
  491. printf("Failed to initialize WPS Registrar\n");
  492. os_free(wps->network_key);
  493. os_free(wps);
  494. return -1;
  495. }
  496. hapd->wps = wps;
  497. return 0;
  498. }
  499. void hostapd_deinit_wps(struct hostapd_data *hapd)
  500. {
  501. if (hapd->wps == NULL)
  502. return;
  503. wps_registrar_deinit(hapd->wps->registrar);
  504. os_free(hapd->wps->network_key);
  505. wps_device_data_free(&hapd->wps->dev);
  506. os_free(hapd->wps);
  507. hapd->wps = NULL;
  508. hostapd_wps_clear_ies(hapd);
  509. }
  510. int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
  511. const char *pin)
  512. {
  513. u8 u[UUID_LEN];
  514. int any = 0;
  515. if (hapd->wps == NULL)
  516. return -1;
  517. if (os_strcmp(uuid, "any") == 0)
  518. any = 1;
  519. else if (uuid_str2bin(uuid, u))
  520. return -1;
  521. return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
  522. (const u8 *) pin, os_strlen(pin));
  523. }
  524. int hostapd_wps_button_pushed(struct hostapd_data *hapd)
  525. {
  526. if (hapd->wps == NULL)
  527. return -1;
  528. return wps_registrar_button_pushed(hapd->wps->registrar);
  529. }
  530. void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
  531. const u8 *ie, size_t ie_len)
  532. {
  533. struct wpabuf *wps_ie;
  534. const u8 *end, *pos, *wps;
  535. if (hapd->wps == NULL)
  536. return;
  537. pos = ie;
  538. end = ie + ie_len;
  539. wps = NULL;
  540. while (pos + 1 < end) {
  541. if (pos + 2 + pos[1] > end)
  542. return;
  543. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  544. WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
  545. wps = pos;
  546. break;
  547. }
  548. pos += 2 + pos[1];
  549. }
  550. if (wps == NULL)
  551. return; /* No WPS IE in Probe Request */
  552. wps_ie = wpabuf_alloc(ie_len);
  553. if (wps_ie == NULL)
  554. return;
  555. /* There may be multiple WPS IEs in the message, so need to concatenate
  556. * their WPS Data fields */
  557. while (pos + 1 < end) {
  558. if (pos + 2 + pos[1] > end)
  559. break;
  560. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  561. WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
  562. wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
  563. pos += 2 + pos[1];
  564. }
  565. if (wpabuf_len(wps_ie) > 0)
  566. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
  567. wpabuf_free(wps_ie);
  568. }