wps_hostapd.c 17 KB

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