wps_hostapd.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * hostapd / 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "utils/uuid.h"
  18. #include "crypto/dh_groups.h"
  19. #include "common/wpa_ctrl.h"
  20. #include "common/ieee802_11_defs.h"
  21. #include "common/ieee802_11_common.h"
  22. #include "eapol_auth/eapol_auth_sm.h"
  23. #include "eapol_auth/eapol_auth_sm_i.h"
  24. #include "wps/wps.h"
  25. #include "wps/wps_defs.h"
  26. #include "wps/wps_dev_attr.h"
  27. #include "hostapd.h"
  28. #include "ap_config.h"
  29. #include "beacon.h"
  30. #include "sta_info.h"
  31. #include "wps_hostapd.h"
  32. #ifdef CONFIG_WPS_UPNP
  33. #include "wps/wps_upnp.h"
  34. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  35. struct wps_context *wps);
  36. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
  37. #endif /* CONFIG_WPS_UPNP */
  38. static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
  39. const u8 *ie, size_t ie_len);
  40. static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
  41. struct wps_for_each_data {
  42. int (*func)(struct hostapd_data *h, void *ctx);
  43. void *ctx;
  44. };
  45. static int wps_for_each(struct hostapd_iface *iface, void *ctx)
  46. {
  47. struct wps_for_each_data *data = ctx;
  48. size_t j;
  49. if (iface == NULL)
  50. return 0;
  51. for (j = 0; j < iface->num_bss; j++) {
  52. struct hostapd_data *hapd = iface->bss[j];
  53. int ret = data->func(hapd, data->ctx);
  54. if (ret)
  55. return ret;
  56. }
  57. return 0;
  58. }
  59. static int hostapd_wps_for_each(struct hostapd_data *hapd,
  60. int (*func)(struct hostapd_data *h, void *ctx),
  61. void *ctx)
  62. {
  63. struct hostapd_iface *iface = hapd->iface;
  64. struct wps_for_each_data data;
  65. if (iface->for_each_interface == NULL)
  66. return -1;
  67. data.func = func;
  68. data.ctx = ctx;
  69. return iface->for_each_interface(iface->interfaces, wps_for_each,
  70. &data);
  71. }
  72. static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  73. size_t psk_len)
  74. {
  75. struct hostapd_data *hapd = ctx;
  76. struct hostapd_wpa_psk *p;
  77. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  78. wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
  79. MACSTR, MAC2STR(mac_addr));
  80. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  81. if (psk_len != PMK_LEN) {
  82. wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
  83. (unsigned long) psk_len);
  84. return -1;
  85. }
  86. /* Add the new PSK to runtime PSK list */
  87. p = os_zalloc(sizeof(*p));
  88. if (p == NULL)
  89. return -1;
  90. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  91. os_memcpy(p->psk, psk, PMK_LEN);
  92. p->next = ssid->wpa_psk;
  93. ssid->wpa_psk = p;
  94. if (ssid->wpa_psk_file) {
  95. FILE *f;
  96. char hex[PMK_LEN * 2 + 1];
  97. /* Add the new PSK to PSK list file */
  98. f = fopen(ssid->wpa_psk_file, "a");
  99. if (f == NULL) {
  100. wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
  101. "'%s'", ssid->wpa_psk_file);
  102. return -1;
  103. }
  104. wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
  105. fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
  106. fclose(f);
  107. }
  108. return 0;
  109. }
  110. static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
  111. struct wpabuf *probe_resp_ie)
  112. {
  113. struct hostapd_data *hapd = ctx;
  114. wpabuf_free(hapd->wps_beacon_ie);
  115. hapd->wps_beacon_ie = beacon_ie;
  116. wpabuf_free(hapd->wps_probe_resp_ie);
  117. hapd->wps_probe_resp_ie = probe_resp_ie;
  118. ieee802_11_set_beacon(hapd);
  119. return hapd->drv.set_ap_wps_ie(hapd);
  120. }
  121. static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  122. const struct wps_device_data *dev)
  123. {
  124. struct hostapd_data *hapd = ctx;
  125. char uuid[40], txt[400];
  126. int len;
  127. char devtype[WPS_DEV_TYPE_BUFSIZE];
  128. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  129. return;
  130. wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
  131. len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
  132. "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
  133. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  134. dev->manufacturer, dev->model_name,
  135. dev->model_number, dev->serial_number,
  136. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  137. sizeof(devtype)));
  138. if (len > 0 && len < (int) sizeof(txt))
  139. wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
  140. if (hapd->conf->wps_pin_requests) {
  141. FILE *f;
  142. struct os_time t;
  143. f = fopen(hapd->conf->wps_pin_requests, "a");
  144. if (f == NULL)
  145. return;
  146. os_get_time(&t);
  147. fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
  148. "\t%s\n",
  149. t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
  150. dev->manufacturer, dev->model_name, dev->model_number,
  151. dev->serial_number,
  152. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  153. sizeof(devtype)));
  154. fclose(f);
  155. }
  156. }
  157. static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  158. const u8 *uuid_e)
  159. {
  160. struct hostapd_data *hapd = ctx;
  161. char uuid[40];
  162. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  163. return;
  164. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
  165. MAC2STR(mac_addr), uuid);
  166. if (hapd->wps_reg_success_cb)
  167. hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
  168. mac_addr, uuid_e);
  169. }
  170. static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
  171. const u8 *uuid_e,
  172. const u8 *pri_dev_type,
  173. u16 config_methods,
  174. u16 dev_password_id, u8 request_type,
  175. const char *dev_name)
  176. {
  177. struct hostapd_data *hapd = ctx;
  178. char uuid[40];
  179. char devtype[WPS_DEV_TYPE_BUFSIZE];
  180. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  181. return;
  182. if (dev_name == NULL)
  183. dev_name = "";
  184. wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
  185. " %s %s 0x%x %u %u [%s]",
  186. MAC2STR(addr), uuid,
  187. wps_dev_type_bin2str(pri_dev_type, devtype,
  188. sizeof(devtype)),
  189. config_methods, dev_password_id, request_type, dev_name);
  190. }
  191. static int str_starts(const char *str, const char *start)
  192. {
  193. return os_strncmp(str, start, os_strlen(start)) == 0;
  194. }
  195. static void wps_reload_config(void *eloop_data, void *user_ctx)
  196. {
  197. struct hostapd_iface *iface = eloop_data;
  198. wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
  199. if (iface->reload_config(iface) < 0) {
  200. wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
  201. "configuration");
  202. }
  203. }
  204. static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
  205. {
  206. const struct wps_credential *cred = ctx;
  207. FILE *oconf, *nconf;
  208. size_t len, i;
  209. char *tmp_fname;
  210. char buf[1024];
  211. int multi_bss;
  212. int wpa;
  213. if (hapd->wps == NULL)
  214. return 0;
  215. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  216. cred->cred_attr, cred->cred_attr_len);
  217. wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
  218. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  219. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  220. cred->auth_type);
  221. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  222. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  223. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  224. cred->key, cred->key_len);
  225. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  226. MAC2STR(cred->mac_addr));
  227. if ((hapd->conf->wps_cred_processing == 1 ||
  228. hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
  229. size_t blen = cred->cred_attr_len * 2 + 1;
  230. char *_buf = os_malloc(blen);
  231. if (_buf) {
  232. wpa_snprintf_hex(_buf, blen,
  233. cred->cred_attr, cred->cred_attr_len);
  234. wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
  235. WPS_EVENT_NEW_AP_SETTINGS, _buf);
  236. os_free(_buf);
  237. }
  238. } else
  239. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
  240. if (hapd->conf->wps_cred_processing == 1)
  241. return 0;
  242. os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
  243. hapd->wps->ssid_len = cred->ssid_len;
  244. hapd->wps->encr_types = cred->encr_type;
  245. hapd->wps->auth_types = cred->auth_type;
  246. if (cred->key_len == 0) {
  247. os_free(hapd->wps->network_key);
  248. hapd->wps->network_key = NULL;
  249. hapd->wps->network_key_len = 0;
  250. } else {
  251. if (hapd->wps->network_key == NULL ||
  252. hapd->wps->network_key_len < cred->key_len) {
  253. hapd->wps->network_key_len = 0;
  254. os_free(hapd->wps->network_key);
  255. hapd->wps->network_key = os_malloc(cred->key_len);
  256. if (hapd->wps->network_key == NULL)
  257. return -1;
  258. }
  259. hapd->wps->network_key_len = cred->key_len;
  260. os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
  261. }
  262. hapd->wps->wps_state = WPS_STATE_CONFIGURED;
  263. len = os_strlen(hapd->iface->config_fname) + 5;
  264. tmp_fname = os_malloc(len);
  265. if (tmp_fname == NULL)
  266. return -1;
  267. os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
  268. oconf = fopen(hapd->iface->config_fname, "r");
  269. if (oconf == NULL) {
  270. wpa_printf(MSG_WARNING, "WPS: Could not open current "
  271. "configuration file");
  272. os_free(tmp_fname);
  273. return -1;
  274. }
  275. nconf = fopen(tmp_fname, "w");
  276. if (nconf == NULL) {
  277. wpa_printf(MSG_WARNING, "WPS: Could not write updated "
  278. "configuration file");
  279. os_free(tmp_fname);
  280. fclose(oconf);
  281. return -1;
  282. }
  283. fprintf(nconf, "# WPS configuration - START\n");
  284. fprintf(nconf, "wps_state=2\n");
  285. fprintf(nconf, "ssid=");
  286. for (i = 0; i < cred->ssid_len; i++)
  287. fputc(cred->ssid[i], nconf);
  288. fprintf(nconf, "\n");
  289. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  290. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  291. wpa = 3;
  292. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  293. wpa = 2;
  294. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  295. wpa = 1;
  296. else
  297. wpa = 0;
  298. if (wpa) {
  299. char *prefix;
  300. fprintf(nconf, "wpa=%d\n", wpa);
  301. fprintf(nconf, "wpa_key_mgmt=");
  302. prefix = "";
  303. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
  304. fprintf(nconf, "WPA-EAP");
  305. prefix = " ";
  306. }
  307. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  308. fprintf(nconf, "%sWPA-PSK", prefix);
  309. fprintf(nconf, "\n");
  310. fprintf(nconf, "wpa_pairwise=");
  311. prefix = "";
  312. if (cred->encr_type & WPS_ENCR_AES) {
  313. fprintf(nconf, "CCMP");
  314. prefix = " ";
  315. }
  316. if (cred->encr_type & WPS_ENCR_TKIP) {
  317. fprintf(nconf, "%sTKIP", prefix);
  318. }
  319. fprintf(nconf, "\n");
  320. if (cred->key_len >= 8 && cred->key_len < 64) {
  321. fprintf(nconf, "wpa_passphrase=");
  322. for (i = 0; i < cred->key_len; i++)
  323. fputc(cred->key[i], nconf);
  324. fprintf(nconf, "\n");
  325. } else if (cred->key_len == 64) {
  326. fprintf(nconf, "wpa_psk=");
  327. for (i = 0; i < cred->key_len; i++)
  328. fputc(cred->key[i], nconf);
  329. fprintf(nconf, "\n");
  330. } else {
  331. wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
  332. "for WPA/WPA2",
  333. (unsigned long) cred->key_len);
  334. }
  335. fprintf(nconf, "auth_algs=1\n");
  336. } else {
  337. if ((cred->auth_type & WPS_AUTH_OPEN) &&
  338. (cred->auth_type & WPS_AUTH_SHARED))
  339. fprintf(nconf, "auth_algs=3\n");
  340. else if (cred->auth_type & WPS_AUTH_SHARED)
  341. fprintf(nconf, "auth_algs=2\n");
  342. else
  343. fprintf(nconf, "auth_algs=1\n");
  344. if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
  345. int key_idx = cred->key_idx;
  346. if (key_idx)
  347. key_idx--;
  348. fprintf(nconf, "wep_default_key=%d\n", key_idx);
  349. fprintf(nconf, "wep_key%d=", key_idx);
  350. if (cred->key_len == 10 || cred->key_len == 26) {
  351. /* WEP key as a hex string */
  352. for (i = 0; i < cred->key_len; i++)
  353. fputc(cred->key[i], nconf);
  354. } else {
  355. /* Raw WEP key; convert to hex */
  356. for (i = 0; i < cred->key_len; i++)
  357. fprintf(nconf, "%02x", cred->key[i]);
  358. }
  359. fprintf(nconf, "\n");
  360. }
  361. }
  362. fprintf(nconf, "# WPS configuration - END\n");
  363. multi_bss = 0;
  364. while (fgets(buf, sizeof(buf), oconf)) {
  365. if (os_strncmp(buf, "bss=", 4) == 0)
  366. multi_bss = 1;
  367. if (!multi_bss &&
  368. (str_starts(buf, "ssid=") ||
  369. str_starts(buf, "auth_algs=") ||
  370. str_starts(buf, "wps_state=") ||
  371. str_starts(buf, "wpa=") ||
  372. str_starts(buf, "wpa_psk=") ||
  373. str_starts(buf, "wpa_pairwise=") ||
  374. str_starts(buf, "rsn_pairwise=") ||
  375. str_starts(buf, "wpa_key_mgmt=") ||
  376. str_starts(buf, "wpa_passphrase="))) {
  377. fprintf(nconf, "#WPS# %s", buf);
  378. } else
  379. fprintf(nconf, "%s", buf);
  380. }
  381. fclose(nconf);
  382. fclose(oconf);
  383. if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
  384. wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
  385. "configuration file: %s", strerror(errno));
  386. os_free(tmp_fname);
  387. return -1;
  388. }
  389. os_free(tmp_fname);
  390. /* Schedule configuration reload after short period of time to allow
  391. * EAP-WSC to be finished.
  392. */
  393. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  394. NULL);
  395. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  396. return 0;
  397. }
  398. static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
  399. {
  400. struct hostapd_data *hapd = ctx;
  401. return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
  402. }
  403. static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
  404. {
  405. struct hostapd_data *hapd = eloop_data;
  406. if (hapd->conf->ap_setup_locked)
  407. return;
  408. wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
  409. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
  410. hapd->wps->ap_setup_locked = 0;
  411. wps_registrar_update_ie(hapd->wps->registrar);
  412. }
  413. static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
  414. {
  415. struct wps_event_pwd_auth_fail *data = ctx;
  416. if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
  417. return 0;
  418. /*
  419. * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
  420. * for some time if this happens multiple times to slow down brute
  421. * force attacks.
  422. */
  423. hapd->ap_pin_failures++;
  424. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  425. hapd->ap_pin_failures);
  426. if (hapd->ap_pin_failures < 3)
  427. return 0;
  428. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
  429. hapd->wps->ap_setup_locked = 1;
  430. wps_registrar_update_ie(hapd->wps->registrar);
  431. if (!hapd->conf->ap_setup_locked) {
  432. if (hapd->ap_pin_lockout_time == 0)
  433. hapd->ap_pin_lockout_time = 60;
  434. else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
  435. (hapd->ap_pin_failures % 3) == 0)
  436. hapd->ap_pin_lockout_time *= 2;
  437. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
  438. hapd->ap_pin_lockout_time);
  439. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  440. eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
  441. hostapd_wps_reenable_ap_pin, hapd,
  442. NULL);
  443. }
  444. return 0;
  445. }
  446. static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
  447. struct wps_event_pwd_auth_fail *data)
  448. {
  449. hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
  450. }
  451. static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
  452. union wps_event_data *data)
  453. {
  454. struct hostapd_data *hapd = ctx;
  455. if (event == WPS_EV_PWD_AUTH_FAIL)
  456. hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
  457. }
  458. static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
  459. {
  460. wpabuf_free(hapd->wps_beacon_ie);
  461. hapd->wps_beacon_ie = NULL;
  462. wpabuf_free(hapd->wps_probe_resp_ie);
  463. hapd->wps_probe_resp_ie = NULL;
  464. hapd->drv.set_ap_wps_ie(hapd);
  465. }
  466. static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
  467. {
  468. const u8 **uuid = ctx;
  469. size_t j;
  470. if (iface == NULL)
  471. return 0;
  472. for (j = 0; j < iface->num_bss; j++) {
  473. struct hostapd_data *hapd = iface->bss[j];
  474. if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
  475. *uuid = hapd->wps->uuid;
  476. return 1;
  477. }
  478. }
  479. return 0;
  480. }
  481. static const u8 * get_own_uuid(struct hostapd_iface *iface)
  482. {
  483. const u8 *uuid;
  484. if (iface->for_each_interface == NULL)
  485. return NULL;
  486. uuid = NULL;
  487. iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid);
  488. return uuid;
  489. }
  490. static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
  491. {
  492. int *count= ctx;
  493. (*count)++;
  494. return 0;
  495. }
  496. static int interface_count(struct hostapd_iface *iface)
  497. {
  498. int count = 0;
  499. if (iface->for_each_interface == NULL)
  500. return 0;
  501. iface->for_each_interface(iface->interfaces, count_interface_cb,
  502. &count);
  503. return count;
  504. }
  505. int hostapd_init_wps(struct hostapd_data *hapd,
  506. struct hostapd_bss_config *conf)
  507. {
  508. struct wps_context *wps;
  509. struct wps_registrar_config cfg;
  510. if (conf->wps_state == 0) {
  511. hostapd_wps_clear_ies(hapd);
  512. return 0;
  513. }
  514. wps = os_zalloc(sizeof(*wps));
  515. if (wps == NULL)
  516. return -1;
  517. wps->cred_cb = hostapd_wps_cred_cb;
  518. wps->event_cb = hostapd_wps_event_cb;
  519. wps->cb_ctx = hapd;
  520. os_memset(&cfg, 0, sizeof(cfg));
  521. wps->wps_state = hapd->conf->wps_state;
  522. wps->ap_setup_locked = hapd->conf->ap_setup_locked;
  523. if (is_nil_uuid(hapd->conf->uuid)) {
  524. const u8 *uuid;
  525. uuid = get_own_uuid(hapd->iface);
  526. if (uuid) {
  527. os_memcpy(wps->uuid, uuid, UUID_LEN);
  528. wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
  529. "interface", wps->uuid, UUID_LEN);
  530. } else {
  531. uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
  532. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  533. "address", wps->uuid, UUID_LEN);
  534. }
  535. } else {
  536. os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
  537. wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
  538. wps->uuid, UUID_LEN);
  539. }
  540. wps->ssid_len = hapd->conf->ssid.ssid_len;
  541. os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
  542. wps->ap = 1;
  543. os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
  544. wps->dev.device_name = hapd->conf->device_name ?
  545. os_strdup(hapd->conf->device_name) : NULL;
  546. wps->dev.manufacturer = hapd->conf->manufacturer ?
  547. os_strdup(hapd->conf->manufacturer) : NULL;
  548. wps->dev.model_name = hapd->conf->model_name ?
  549. os_strdup(hapd->conf->model_name) : NULL;
  550. wps->dev.model_number = hapd->conf->model_number ?
  551. os_strdup(hapd->conf->model_number) : NULL;
  552. wps->dev.serial_number = hapd->conf->serial_number ?
  553. os_strdup(hapd->conf->serial_number) : NULL;
  554. wps->config_methods =
  555. wps_config_methods_str2bin(hapd->conf->config_methods);
  556. #ifdef CONFIG_WPS2
  557. if ((wps->config_methods &
  558. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  559. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  560. wpa_printf(MSG_INFO, "WPS: Converting display to "
  561. "virtual_display for WPS 2.0 compliance");
  562. wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  563. }
  564. if ((wps->config_methods &
  565. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  566. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  567. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  568. "virtual_push_button for WPS 2.0 compliance");
  569. wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  570. }
  571. #endif /* CONFIG_WPS2 */
  572. if (hapd->conf->device_type &&
  573. wps_dev_type_str2bin(hapd->conf->device_type,
  574. wps->dev.pri_dev_type) < 0) {
  575. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  576. os_free(wps);
  577. return -1;
  578. }
  579. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  580. wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  581. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  582. if (conf->wpa & WPA_PROTO_RSN) {
  583. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  584. wps->auth_types |= WPS_AUTH_WPA2PSK;
  585. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  586. wps->auth_types |= WPS_AUTH_WPA2;
  587. if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
  588. wps->encr_types |= WPS_ENCR_AES;
  589. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  590. wps->encr_types |= WPS_ENCR_TKIP;
  591. }
  592. if (conf->wpa & WPA_PROTO_WPA) {
  593. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  594. wps->auth_types |= WPS_AUTH_WPAPSK;
  595. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  596. wps->auth_types |= WPS_AUTH_WPA;
  597. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  598. wps->encr_types |= WPS_ENCR_AES;
  599. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  600. wps->encr_types |= WPS_ENCR_TKIP;
  601. }
  602. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  603. wps->encr_types |= WPS_ENCR_NONE;
  604. wps->auth_types |= WPS_AUTH_OPEN;
  605. } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
  606. wps->encr_types |= WPS_ENCR_WEP;
  607. if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
  608. wps->auth_types |= WPS_AUTH_OPEN;
  609. if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
  610. wps->auth_types |= WPS_AUTH_SHARED;
  611. } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
  612. wps->auth_types |= WPS_AUTH_OPEN;
  613. if (conf->default_wep_key_len)
  614. wps->encr_types |= WPS_ENCR_WEP;
  615. else
  616. wps->encr_types |= WPS_ENCR_NONE;
  617. }
  618. if (conf->ssid.wpa_psk_file) {
  619. /* Use per-device PSKs */
  620. } else if (conf->ssid.wpa_passphrase) {
  621. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  622. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  623. } else if (conf->ssid.wpa_psk) {
  624. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  625. if (wps->network_key == NULL) {
  626. os_free(wps);
  627. return -1;
  628. }
  629. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  630. conf->ssid.wpa_psk->psk, PMK_LEN);
  631. wps->network_key_len = 2 * PMK_LEN;
  632. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  633. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  634. if (wps->network_key == NULL) {
  635. os_free(wps);
  636. return -1;
  637. }
  638. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  639. conf->ssid.wep.len[0]);
  640. wps->network_key_len = conf->ssid.wep.len[0];
  641. }
  642. if (conf->ssid.wpa_psk) {
  643. os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
  644. wps->psk_set = 1;
  645. }
  646. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  647. /* Override parameters to enable security by default */
  648. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  649. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  650. }
  651. wps->ap_settings = conf->ap_settings;
  652. wps->ap_settings_len = conf->ap_settings_len;
  653. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  654. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  655. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  656. cfg.reg_success_cb = hostapd_wps_reg_success_cb;
  657. cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
  658. cfg.cb_ctx = hapd;
  659. cfg.skip_cred_build = conf->skip_cred_build;
  660. cfg.extra_cred = conf->extra_cred;
  661. cfg.extra_cred_len = conf->extra_cred_len;
  662. cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
  663. conf->skip_cred_build;
  664. if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
  665. cfg.static_wep_only = 1;
  666. cfg.dualband = interface_count(hapd->iface) > 1;
  667. if (cfg.dualband)
  668. wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
  669. wps->registrar = wps_registrar_init(wps, &cfg);
  670. if (wps->registrar == NULL) {
  671. wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
  672. os_free(wps->network_key);
  673. os_free(wps);
  674. return -1;
  675. }
  676. #ifdef CONFIG_WPS_UPNP
  677. wps->friendly_name = hapd->conf->friendly_name;
  678. wps->manufacturer_url = hapd->conf->manufacturer_url;
  679. wps->model_description = hapd->conf->model_description;
  680. wps->model_url = hapd->conf->model_url;
  681. wps->upc = hapd->conf->upc;
  682. if (hostapd_wps_upnp_init(hapd, wps) < 0) {
  683. wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
  684. wps_registrar_deinit(wps->registrar);
  685. os_free(wps->network_key);
  686. os_free(wps);
  687. return -1;
  688. }
  689. #endif /* CONFIG_WPS_UPNP */
  690. hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
  691. hapd->wps = wps;
  692. return 0;
  693. }
  694. void hostapd_deinit_wps(struct hostapd_data *hapd)
  695. {
  696. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  697. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  698. if (hapd->wps == NULL)
  699. return;
  700. #ifdef CONFIG_WPS_UPNP
  701. hostapd_wps_upnp_deinit(hapd);
  702. #endif /* CONFIG_WPS_UPNP */
  703. wps_registrar_deinit(hapd->wps->registrar);
  704. os_free(hapd->wps->network_key);
  705. wps_device_data_free(&hapd->wps->dev);
  706. wpabuf_free(hapd->wps->dh_pubkey);
  707. wpabuf_free(hapd->wps->dh_privkey);
  708. wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
  709. wpabuf_free(hapd->wps->oob_conf.dev_password);
  710. wps_free_pending_msgs(hapd->wps->upnp_msgs);
  711. os_free(hapd->wps);
  712. hapd->wps = NULL;
  713. hostapd_wps_clear_ies(hapd);
  714. }
  715. void hostapd_update_wps(struct hostapd_data *hapd)
  716. {
  717. if (hapd->wps == NULL)
  718. return;
  719. #ifdef CONFIG_WPS_UPNP
  720. hapd->wps->friendly_name = hapd->conf->friendly_name;
  721. hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
  722. hapd->wps->model_description = hapd->conf->model_description;
  723. hapd->wps->model_url = hapd->conf->model_url;
  724. hapd->wps->upc = hapd->conf->upc;
  725. #endif /* CONFIG_WPS_UPNP */
  726. if (hapd->conf->wps_state)
  727. wps_registrar_update_ie(hapd->wps->registrar);
  728. else
  729. hostapd_deinit_wps(hapd);
  730. }
  731. struct wps_add_pin_data {
  732. const u8 *addr;
  733. const u8 *uuid;
  734. const u8 *pin;
  735. size_t pin_len;
  736. int timeout;
  737. int added;
  738. };
  739. static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
  740. {
  741. struct wps_add_pin_data *data = ctx;
  742. int ret;
  743. if (hapd->wps == NULL)
  744. return 0;
  745. ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
  746. data->uuid, data->pin, data->pin_len,
  747. data->timeout);
  748. if (ret == 0)
  749. data->added++;
  750. return ret;
  751. }
  752. int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
  753. const char *uuid, const char *pin, int timeout)
  754. {
  755. u8 u[UUID_LEN];
  756. struct wps_add_pin_data data;
  757. data.addr = addr;
  758. data.uuid = u;
  759. data.pin = (const u8 *) pin;
  760. data.pin_len = os_strlen(pin);
  761. data.timeout = timeout;
  762. data.added = 0;
  763. if (os_strcmp(uuid, "any") == 0)
  764. data.uuid = NULL;
  765. else {
  766. if (uuid_str2bin(uuid, u))
  767. return -1;
  768. data.uuid = u;
  769. }
  770. if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
  771. return -1;
  772. return data.added ? 0 : -1;
  773. }
  774. static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
  775. {
  776. if (hapd->wps == NULL)
  777. return 0;
  778. return wps_registrar_button_pushed(hapd->wps->registrar);
  779. }
  780. int hostapd_wps_button_pushed(struct hostapd_data *hapd)
  781. {
  782. return hostapd_wps_for_each(hapd, wps_button_pushed, NULL);
  783. }
  784. #ifdef CONFIG_WPS_OOB
  785. int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
  786. char *path, char *method, char *name)
  787. {
  788. struct wps_context *wps = hapd->wps;
  789. struct oob_device_data *oob_dev;
  790. oob_dev = wps_get_oob_device(device_type);
  791. if (oob_dev == NULL)
  792. return -1;
  793. oob_dev->device_path = path;
  794. oob_dev->device_name = name;
  795. wps->oob_conf.oob_method = wps_get_oob_method(method);
  796. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
  797. /*
  798. * Use pre-configured DH keys in order to be able to write the
  799. * key hash into the OOB file.
  800. */
  801. wpabuf_free(wps->dh_pubkey);
  802. wpabuf_free(wps->dh_privkey);
  803. wps->dh_privkey = NULL;
  804. wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
  805. &wps->dh_privkey);
  806. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  807. if (wps->dh_pubkey == NULL) {
  808. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  809. "Diffie-Hellman handshake");
  810. return -1;
  811. }
  812. }
  813. if (wps_process_oob(wps, oob_dev, 1) < 0)
  814. goto error;
  815. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  816. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  817. hostapd_wps_add_pin(hapd, NULL, "any",
  818. wpabuf_head(wps->oob_conf.dev_password), 0) <
  819. 0)
  820. goto error;
  821. return 0;
  822. error:
  823. wpabuf_free(wps->dh_pubkey);
  824. wps->dh_pubkey = NULL;
  825. wpabuf_free(wps->dh_privkey);
  826. wps->dh_privkey = NULL;
  827. return -1;
  828. }
  829. #endif /* CONFIG_WPS_OOB */
  830. static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
  831. const u8 *ie, size_t ie_len)
  832. {
  833. struct hostapd_data *hapd = ctx;
  834. struct wpabuf *wps_ie;
  835. struct ieee802_11_elems elems;
  836. if (hapd->wps == NULL)
  837. return 0;
  838. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  839. wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
  840. MACSTR, MAC2STR(addr));
  841. return 0;
  842. }
  843. if (elems.ssid && elems.ssid_len > 0 &&
  844. (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  845. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
  846. 0))
  847. return 0; /* Not for us */
  848. wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
  849. if (wps_ie == NULL)
  850. return 0;
  851. if (wps_validate_probe_req(wps_ie, addr) < 0) {
  852. wpabuf_free(wps_ie);
  853. return 0;
  854. }
  855. if (wpabuf_len(wps_ie) > 0) {
  856. int p2p_wildcard = 0;
  857. #ifdef CONFIG_P2P
  858. if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
  859. os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
  860. P2P_WILDCARD_SSID_LEN) == 0)
  861. p2p_wildcard = 1;
  862. #endif /* CONFIG_P2P */
  863. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
  864. p2p_wildcard);
  865. #ifdef CONFIG_WPS_UPNP
  866. /* FIX: what exactly should be included in the WLANEvent?
  867. * WPS attributes? Full ProbeReq frame? */
  868. if (!p2p_wildcard)
  869. upnp_wps_device_send_wlan_event(
  870. hapd->wps_upnp, addr,
  871. UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
  872. #endif /* CONFIG_WPS_UPNP */
  873. }
  874. wpabuf_free(wps_ie);
  875. return 0;
  876. }
  877. #ifdef CONFIG_WPS_UPNP
  878. static int hostapd_rx_req_put_wlan_response(
  879. void *priv, enum upnp_wps_wlanevent_type ev_type,
  880. const u8 *mac_addr, const struct wpabuf *msg,
  881. enum wps_msg_type msg_type)
  882. {
  883. struct hostapd_data *hapd = priv;
  884. struct sta_info *sta;
  885. struct upnp_pending_message *p;
  886. wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
  887. MACSTR, ev_type, MAC2STR(mac_addr));
  888. wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
  889. wpabuf_head(msg), wpabuf_len(msg));
  890. if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
  891. wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
  892. "PutWLANResponse WLANEventType %d", ev_type);
  893. return -1;
  894. }
  895. /*
  896. * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
  897. * server implementation for delivery to the peer.
  898. */
  899. sta = ap_get_sta(hapd, mac_addr);
  900. #ifndef CONFIG_WPS_STRICT
  901. if (!sta) {
  902. /*
  903. * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
  904. * Pick STA that is in an ongoing WPS registration without
  905. * checking the MAC address.
  906. */
  907. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
  908. "on NewWLANEventMAC; try wildcard match");
  909. for (sta = hapd->sta_list; sta; sta = sta->next) {
  910. if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
  911. break;
  912. }
  913. }
  914. #endif /* CONFIG_WPS_STRICT */
  915. if (!sta) {
  916. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
  917. return 0;
  918. }
  919. p = os_zalloc(sizeof(*p));
  920. if (p == NULL)
  921. return -1;
  922. os_memcpy(p->addr, sta->addr, ETH_ALEN);
  923. p->msg = wpabuf_dup(msg);
  924. p->type = msg_type;
  925. p->next = hapd->wps->upnp_msgs;
  926. hapd->wps->upnp_msgs = p;
  927. return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
  928. }
  929. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  930. struct wps_context *wps)
  931. {
  932. struct upnp_wps_device_ctx *ctx;
  933. if (!hapd->conf->upnp_iface)
  934. return 0;
  935. ctx = os_zalloc(sizeof(*ctx));
  936. if (ctx == NULL)
  937. return -1;
  938. ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
  939. if (hapd->conf->ap_pin)
  940. ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
  941. hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
  942. if (hapd->wps_upnp == NULL) {
  943. os_free(ctx);
  944. return -1;
  945. }
  946. wps->wps_upnp = hapd->wps_upnp;
  947. if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
  948. upnp_wps_device_deinit(hapd->wps_upnp);
  949. hapd->wps_upnp = NULL;
  950. return -1;
  951. }
  952. return 0;
  953. }
  954. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
  955. {
  956. upnp_wps_device_deinit(hapd->wps_upnp);
  957. }
  958. #endif /* CONFIG_WPS_UPNP */
  959. int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
  960. char *buf, size_t buflen)
  961. {
  962. if (hapd->wps == NULL)
  963. return 0;
  964. return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
  965. }
  966. static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  967. {
  968. struct hostapd_data *hapd = eloop_data;
  969. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  970. hostapd_wps_ap_pin_disable(hapd);
  971. }
  972. static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
  973. {
  974. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  975. hapd->ap_pin_failures = 0;
  976. hapd->conf->ap_setup_locked = 0;
  977. if (hapd->wps->ap_setup_locked) {
  978. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
  979. hapd->wps->ap_setup_locked = 0;
  980. wps_registrar_update_ie(hapd->wps->registrar);
  981. }
  982. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  983. if (timeout > 0)
  984. eloop_register_timeout(timeout, 0,
  985. hostapd_wps_ap_pin_timeout, hapd, NULL);
  986. }
  987. static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
  988. {
  989. os_free(hapd->conf->ap_pin);
  990. hapd->conf->ap_pin = NULL;
  991. #ifdef CONFIG_WPS_UPNP
  992. upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
  993. #endif /* CONFIG_WPS_UPNP */
  994. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  995. return 0;
  996. }
  997. void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
  998. {
  999. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  1000. hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
  1001. }
  1002. struct wps_ap_pin_data {
  1003. char pin_txt[9];
  1004. int timeout;
  1005. };
  1006. static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
  1007. {
  1008. struct wps_ap_pin_data *data = ctx;
  1009. os_free(hapd->conf->ap_pin);
  1010. hapd->conf->ap_pin = os_strdup(data->pin_txt);
  1011. #ifdef CONFIG_WPS_UPNP
  1012. upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
  1013. #endif /* CONFIG_WPS_UPNP */
  1014. hostapd_wps_ap_pin_enable(hapd, data->timeout);
  1015. return 0;
  1016. }
  1017. const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
  1018. {
  1019. unsigned int pin;
  1020. struct wps_ap_pin_data data;
  1021. pin = wps_generate_pin();
  1022. os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
  1023. data.timeout = timeout;
  1024. hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
  1025. return hapd->conf->ap_pin;
  1026. }
  1027. const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
  1028. {
  1029. return hapd->conf->ap_pin;
  1030. }
  1031. int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
  1032. int timeout)
  1033. {
  1034. struct wps_ap_pin_data data;
  1035. int ret;
  1036. ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
  1037. if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
  1038. return -1;
  1039. data.timeout = timeout;
  1040. return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
  1041. }