wps_hostapd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  158. cred->cred_attr, cred->cred_attr_len);
  159. wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
  160. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  161. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  162. cred->auth_type);
  163. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  164. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  165. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  166. cred->key, cred->key_len);
  167. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  168. MAC2STR(cred->mac_addr));
  169. hostapd_ctrl_iface_send(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS,
  170. os_strlen(WPS_EVENT_NEW_AP_SETTINGS));
  171. len = os_strlen(hapd->iface->config_fname) + 5;
  172. tmp_fname = os_malloc(len);
  173. if (tmp_fname == NULL)
  174. return -1;
  175. os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
  176. oconf = fopen(hapd->iface->config_fname, "r");
  177. if (oconf == NULL) {
  178. wpa_printf(MSG_WARNING, "WPS: Could not open current "
  179. "configuration file");
  180. os_free(tmp_fname);
  181. return -1;
  182. }
  183. nconf = fopen(tmp_fname, "w");
  184. if (nconf == NULL) {
  185. wpa_printf(MSG_WARNING, "WPS: Could not write updated "
  186. "configuration file");
  187. os_free(tmp_fname);
  188. fclose(oconf);
  189. return -1;
  190. }
  191. fprintf(nconf, "# WPS configuration - START\n");
  192. fprintf(nconf, "wps_state=2\n");
  193. fprintf(nconf, "ssid=");
  194. for (i = 0; i < cred->ssid_len; i++)
  195. fputc(cred->ssid[i], nconf);
  196. fprintf(nconf, "\n");
  197. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  198. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  199. wpa = 3;
  200. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  201. wpa = 2;
  202. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  203. wpa = 1;
  204. else
  205. wpa = 0;
  206. if (wpa) {
  207. char *prefix;
  208. fprintf(nconf, "wpa=%d\n", wpa);
  209. fprintf(nconf, "wpa_key_mgmt=");
  210. prefix = "";
  211. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
  212. fprintf(nconf, "WPA-EAP");
  213. prefix = " ";
  214. }
  215. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  216. fprintf(nconf, "%sWPA-PSK", prefix);
  217. fprintf(nconf, "\n");
  218. fprintf(nconf, "wpa_pairwise=");
  219. prefix = "";
  220. if (cred->encr_type & WPS_ENCR_AES) {
  221. fprintf(nconf, "CCMP");
  222. prefix = " ";
  223. }
  224. if (cred->encr_type & WPS_ENCR_TKIP) {
  225. fprintf(nconf, "%sTKIP", prefix);
  226. }
  227. fprintf(nconf, "\n");
  228. if (cred->key_len >= 8 && cred->key_len < 64) {
  229. fprintf(nconf, "wpa_passphrase=");
  230. for (i = 0; i < cred->key_len; i++)
  231. fputc(cred->key[i], nconf);
  232. fprintf(nconf, "\n");
  233. } else if (cred->key_len == 64) {
  234. fprintf(nconf, "wpa_psk=");
  235. for (i = 0; i < cred->key_len; i++)
  236. fputc(cred->key[i], nconf);
  237. fprintf(nconf, "\n");
  238. } else {
  239. wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
  240. "for WPA/WPA2",
  241. (unsigned long) cred->key_len);
  242. }
  243. fprintf(nconf, "auth_algs=1\n");
  244. } else {
  245. if ((cred->auth_type & WPS_AUTH_OPEN) &&
  246. (cred->auth_type & WPS_AUTH_SHARED))
  247. fprintf(nconf, "auth_algs=3\n");
  248. else if (cred->auth_type & WPS_AUTH_SHARED)
  249. fprintf(nconf, "auth_algs=2\n");
  250. else
  251. fprintf(nconf, "auth_algs=1\n");
  252. if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
  253. fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
  254. fprintf(nconf, "wep_key%d=", cred->key_idx);
  255. if (cred->key_len != 10 && cred->key_len != 26)
  256. fputc('"', nconf);
  257. for (i = 0; i < cred->key_len; i++)
  258. fputc(cred->key[i], nconf);
  259. if (cred->key_len != 10 && cred->key_len != 26)
  260. fputc('"', nconf);
  261. fprintf(nconf, "\n");
  262. }
  263. }
  264. fprintf(nconf, "# WPS configuration - END\n");
  265. multi_bss = 0;
  266. while (fgets(buf, sizeof(buf), oconf)) {
  267. if (os_strncmp(buf, "bss=", 4) == 0)
  268. multi_bss = 1;
  269. if (!multi_bss &&
  270. (str_starts(buf, "ssid=") ||
  271. str_starts(buf, "auth_algs=") ||
  272. str_starts(buf, "wps_state=") ||
  273. str_starts(buf, "wpa=") ||
  274. str_starts(buf, "wpa_psk=") ||
  275. str_starts(buf, "wpa_pairwise=") ||
  276. str_starts(buf, "rsn_pairwise=") ||
  277. str_starts(buf, "wpa_key_mgmt=") ||
  278. str_starts(buf, "wpa_passphrase="))) {
  279. fprintf(nconf, "#WPS# %s", buf);
  280. } else
  281. fprintf(nconf, "%s", buf);
  282. }
  283. fclose(nconf);
  284. fclose(oconf);
  285. if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
  286. wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
  287. "configuration file: %s", strerror(errno));
  288. os_free(tmp_fname);
  289. return -1;
  290. }
  291. os_free(tmp_fname);
  292. /* Schedule configuration reload after short period of time to allow
  293. * EAP-WSC to be finished.
  294. */
  295. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  296. NULL);
  297. /* TODO: dualband AP may need to update multiple configuration files */
  298. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  299. return 0;
  300. }
  301. static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
  302. {
  303. os_free(hapd->wps_beacon_ie);
  304. hapd->wps_beacon_ie = NULL;
  305. hapd->wps_beacon_ie_len = 0;
  306. hostapd_set_wps_beacon_ie(hapd, NULL, 0);
  307. os_free(hapd->wps_probe_resp_ie);
  308. hapd->wps_probe_resp_ie = NULL;
  309. hapd->wps_probe_resp_ie_len = 0;
  310. hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
  311. }
  312. int hostapd_init_wps(struct hostapd_data *hapd,
  313. struct hostapd_bss_config *conf)
  314. {
  315. struct wps_context *wps;
  316. struct wps_registrar_config cfg;
  317. if (conf->wps_state == 0) {
  318. hostapd_wps_clear_ies(hapd);
  319. return 0;
  320. }
  321. wps = os_zalloc(sizeof(*wps));
  322. if (wps == NULL)
  323. return -1;
  324. wps->cred_cb = hostapd_wps_cred_cb;
  325. wps->cb_ctx = hapd;
  326. os_memset(&cfg, 0, sizeof(cfg));
  327. wps->wps_state = hapd->conf->wps_state;
  328. wps->ap_setup_locked = hapd->conf->ap_setup_locked;
  329. if (is_nil_uuid(hapd->conf->uuid)) {
  330. uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
  331. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  332. wps->uuid, UUID_LEN);
  333. } else
  334. os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
  335. wps->ssid_len = hapd->conf->ssid.ssid_len;
  336. os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
  337. wps->ap = 1;
  338. os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
  339. wps->dev.device_name = os_strdup(hapd->conf->device_name);
  340. wps->dev.manufacturer = os_strdup(hapd->conf->manufacturer);
  341. wps->dev.model_name = os_strdup(hapd->conf->model_name);
  342. wps->dev.model_number = os_strdup(hapd->conf->model_number);
  343. wps->dev.serial_number = os_strdup(hapd->conf->serial_number);
  344. if (hapd->conf->config_methods) {
  345. char *m = hapd->conf->config_methods;
  346. if (os_strstr(m, "label"))
  347. wps->config_methods |= WPS_CONFIG_LABEL;
  348. if (os_strstr(m, "display"))
  349. wps->config_methods |= WPS_CONFIG_DISPLAY;
  350. if (os_strstr(m, "push_button"))
  351. wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
  352. if (os_strstr(m, "keypad"))
  353. wps->config_methods |= WPS_CONFIG_KEYPAD;
  354. }
  355. if (hapd->conf->device_type) {
  356. char *pos;
  357. u8 oui[4];
  358. /* <categ>-<OUI>-<subcateg> */
  359. wps->dev.categ = atoi(hapd->conf->device_type);
  360. pos = os_strchr(hapd->conf->device_type, '-');
  361. if (pos == NULL) {
  362. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  363. os_free(wps);
  364. return -1;
  365. }
  366. pos++;
  367. if (hexstr2bin(pos, oui, 4)) {
  368. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  369. os_free(wps);
  370. return -1;
  371. }
  372. wps->dev.oui = WPA_GET_BE32(oui);
  373. pos = os_strchr(pos, '-');
  374. if (pos == NULL) {
  375. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  376. os_free(wps);
  377. return -1;
  378. }
  379. pos++;
  380. wps->dev.sub_categ = atoi(pos);
  381. }
  382. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  383. wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  384. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  385. if (conf->wpa & WPA_PROTO_RSN) {
  386. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  387. wps->auth_types |= WPS_AUTH_WPA2PSK;
  388. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  389. wps->auth_types |= WPS_AUTH_WPA2;
  390. if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
  391. wps->encr_types |= WPS_ENCR_AES;
  392. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  393. wps->encr_types |= WPS_ENCR_TKIP;
  394. }
  395. if (conf->wpa & WPA_PROTO_WPA) {
  396. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  397. wps->auth_types |= WPS_AUTH_WPAPSK;
  398. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  399. wps->auth_types |= WPS_AUTH_WPA;
  400. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  401. wps->encr_types |= WPS_ENCR_AES;
  402. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  403. wps->encr_types |= WPS_ENCR_TKIP;
  404. }
  405. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  406. wps->encr_types |= WPS_ENCR_NONE;
  407. wps->auth_types |= WPS_AUTH_OPEN;
  408. } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
  409. wps->encr_types |= WPS_ENCR_WEP;
  410. if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
  411. wps->auth_types |= WPS_AUTH_OPEN;
  412. if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
  413. wps->auth_types |= WPS_AUTH_SHARED;
  414. } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
  415. wps->auth_types |= WPS_AUTH_OPEN;
  416. if (conf->default_wep_key_len)
  417. wps->encr_types |= WPS_ENCR_WEP;
  418. else
  419. wps->encr_types |= WPS_ENCR_NONE;
  420. }
  421. if (conf->ssid.wpa_psk_file) {
  422. /* Use per-device PSKs */
  423. } else if (conf->ssid.wpa_passphrase) {
  424. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  425. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  426. } else if (conf->ssid.wpa_psk) {
  427. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  428. if (wps->network_key == NULL) {
  429. os_free(wps);
  430. return -1;
  431. }
  432. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  433. conf->ssid.wpa_psk->psk, PMK_LEN);
  434. wps->network_key_len = 2 * PMK_LEN;
  435. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  436. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  437. if (wps->network_key == NULL) {
  438. os_free(wps);
  439. return -1;
  440. }
  441. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  442. conf->ssid.wep.len[0]);
  443. wps->network_key_len = conf->ssid.wep.len[0];
  444. }
  445. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  446. /* Override parameters to enable security by default */
  447. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  448. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  449. }
  450. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  451. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  452. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  453. cfg.cb_ctx = hapd;
  454. cfg.skip_cred_build = conf->skip_cred_build;
  455. cfg.extra_cred = conf->extra_cred;
  456. cfg.extra_cred_len = conf->extra_cred_len;
  457. wps->registrar = wps_registrar_init(wps, &cfg);
  458. if (wps->registrar == NULL) {
  459. printf("Failed to initialize WPS Registrar\n");
  460. os_free(wps->network_key);
  461. os_free(wps);
  462. return -1;
  463. }
  464. hapd->wps = wps;
  465. return 0;
  466. }
  467. void hostapd_deinit_wps(struct hostapd_data *hapd)
  468. {
  469. if (hapd->wps == NULL)
  470. return;
  471. wps_registrar_deinit(hapd->wps->registrar);
  472. os_free(hapd->wps->network_key);
  473. wps_device_data_free(&hapd->wps->dev);
  474. os_free(hapd->wps);
  475. hapd->wps = NULL;
  476. hostapd_wps_clear_ies(hapd);
  477. }
  478. int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
  479. const char *pin)
  480. {
  481. u8 u[UUID_LEN];
  482. int any = 0;
  483. if (hapd->wps == NULL)
  484. return -1;
  485. if (os_strcmp(uuid, "any") == 0)
  486. any = 1;
  487. else if (uuid_str2bin(uuid, u))
  488. return -1;
  489. return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
  490. (const u8 *) pin, os_strlen(pin));
  491. }
  492. int hostapd_wps_button_pushed(struct hostapd_data *hapd)
  493. {
  494. if (hapd->wps == NULL)
  495. return -1;
  496. return wps_registrar_button_pushed(hapd->wps->registrar);
  497. }
  498. void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
  499. const u8 *ie, size_t ie_len)
  500. {
  501. struct wpabuf *wps_ie;
  502. const u8 *end, *pos, *wps;
  503. if (hapd->wps == NULL)
  504. return;
  505. pos = ie;
  506. end = ie + ie_len;
  507. wps = NULL;
  508. while (pos + 1 < end) {
  509. if (pos + 2 + pos[1] > end)
  510. return;
  511. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  512. WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
  513. wps = pos;
  514. break;
  515. }
  516. pos += 2 + pos[1];
  517. }
  518. if (wps == NULL)
  519. return; /* No WPS IE in Probe Request */
  520. wps_ie = wpabuf_alloc(ie_len);
  521. if (wps_ie == NULL)
  522. return;
  523. /* There may be multiple WPS IEs in the message, so need to concatenate
  524. * their WPS Data fields */
  525. while (pos + 1 < end) {
  526. if (pos + 2 + pos[1] > end)
  527. break;
  528. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  529. WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
  530. wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
  531. pos += 2 + pos[1];
  532. }
  533. if (wpabuf_len(wps_ie) > 0)
  534. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
  535. wpabuf_free(wps_ie);
  536. }