wps_hostapd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 "ieee802_11_common.h"
  22. #include "sta_info.h"
  23. #include "eapol_sm.h"
  24. #include "wps/wps.h"
  25. #include "wps/wps_defs.h"
  26. #include "wps/wps_dev_attr.h"
  27. #include "wps_hostapd.h"
  28. #include "dh_groups.h"
  29. #ifdef CONFIG_WPS_UPNP
  30. #include "wps/wps_upnp.h"
  31. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  32. struct wps_context *wps);
  33. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
  34. #endif /* CONFIG_WPS_UPNP */
  35. static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
  36. const u8 *ie, size_t ie_len);
  37. static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  38. size_t psk_len)
  39. {
  40. struct hostapd_data *hapd = ctx;
  41. struct hostapd_wpa_psk *p;
  42. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  43. wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
  44. MACSTR, MAC2STR(mac_addr));
  45. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  46. if (psk_len != PMK_LEN) {
  47. wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
  48. (unsigned long) psk_len);
  49. return -1;
  50. }
  51. /* Add the new PSK to runtime PSK list */
  52. p = os_zalloc(sizeof(*p));
  53. if (p == NULL)
  54. return -1;
  55. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  56. os_memcpy(p->psk, psk, PMK_LEN);
  57. p->next = ssid->wpa_psk;
  58. ssid->wpa_psk = p;
  59. if (ssid->wpa_psk_file) {
  60. FILE *f;
  61. char hex[PMK_LEN * 2 + 1];
  62. /* Add the new PSK to PSK list file */
  63. f = fopen(ssid->wpa_psk_file, "a");
  64. if (f == NULL) {
  65. wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
  66. "'%s'", ssid->wpa_psk_file);
  67. return -1;
  68. }
  69. wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
  70. fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
  71. fclose(f);
  72. }
  73. return 0;
  74. }
  75. static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
  76. size_t beacon_ie_len, const u8 *probe_resp_ie,
  77. size_t probe_resp_ie_len)
  78. {
  79. struct hostapd_data *hapd = ctx;
  80. os_free(hapd->wps_beacon_ie);
  81. if (beacon_ie_len == 0) {
  82. hapd->wps_beacon_ie = NULL;
  83. hapd->wps_beacon_ie_len = 0;
  84. } else {
  85. hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
  86. if (hapd->wps_beacon_ie == NULL) {
  87. hapd->wps_beacon_ie_len = 0;
  88. return -1;
  89. }
  90. os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
  91. hapd->wps_beacon_ie_len = beacon_ie_len;
  92. }
  93. hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
  94. hapd->wps_beacon_ie_len);
  95. os_free(hapd->wps_probe_resp_ie);
  96. if (probe_resp_ie_len == 0) {
  97. hapd->wps_probe_resp_ie = NULL;
  98. hapd->wps_probe_resp_ie_len = 0;
  99. } else {
  100. hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
  101. if (hapd->wps_probe_resp_ie == NULL) {
  102. hapd->wps_probe_resp_ie_len = 0;
  103. return -1;
  104. }
  105. os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
  106. probe_resp_ie_len);
  107. hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
  108. }
  109. hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
  110. hapd->wps_probe_resp_ie_len);
  111. return 0;
  112. }
  113. static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  114. const struct wps_device_data *dev)
  115. {
  116. struct hostapd_data *hapd = ctx;
  117. char uuid[40], txt[400];
  118. int len;
  119. char devtype[WPS_DEV_TYPE_BUFSIZE];
  120. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  121. return;
  122. wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
  123. len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
  124. "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
  125. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  126. dev->manufacturer, dev->model_name,
  127. dev->model_number, dev->serial_number,
  128. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  129. sizeof(devtype)));
  130. if (len > 0 && len < (int) sizeof(txt))
  131. wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
  132. if (hapd->conf->wps_pin_requests) {
  133. FILE *f;
  134. struct os_time t;
  135. f = fopen(hapd->conf->wps_pin_requests, "a");
  136. if (f == NULL)
  137. return;
  138. os_get_time(&t);
  139. fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
  140. "\t%s\n",
  141. t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
  142. dev->manufacturer, dev->model_name, dev->model_number,
  143. dev->serial_number,
  144. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  145. sizeof(devtype)));
  146. fclose(f);
  147. }
  148. }
  149. static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  150. const u8 *uuid_e)
  151. {
  152. struct hostapd_data *hapd = ctx;
  153. char uuid[40];
  154. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  155. return;
  156. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
  157. MAC2STR(mac_addr), uuid);
  158. }
  159. static int str_starts(const char *str, const char *start)
  160. {
  161. return os_strncmp(str, start, os_strlen(start)) == 0;
  162. }
  163. static void wps_reload_config(void *eloop_data, void *user_ctx)
  164. {
  165. struct hostapd_iface *iface = eloop_data;
  166. wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
  167. if (hostapd_reload_config(iface) < 0) {
  168. wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
  169. "configuration");
  170. }
  171. }
  172. static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
  173. {
  174. struct hostapd_data *hapd = ctx;
  175. FILE *oconf, *nconf;
  176. size_t len, i;
  177. char *tmp_fname;
  178. char buf[1024];
  179. int multi_bss;
  180. int wpa;
  181. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  182. cred->cred_attr, cred->cred_attr_len);
  183. wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
  184. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  185. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  186. cred->auth_type);
  187. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  188. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  189. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  190. cred->key, cred->key_len);
  191. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  192. MAC2STR(cred->mac_addr));
  193. if ((hapd->conf->wps_cred_processing == 1 ||
  194. hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
  195. size_t blen = cred->cred_attr_len * 2 + 1;
  196. char *_buf = os_malloc(blen);
  197. if (_buf) {
  198. wpa_snprintf_hex(_buf, blen,
  199. cred->cred_attr, cred->cred_attr_len);
  200. wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
  201. WPS_EVENT_NEW_AP_SETTINGS, _buf);
  202. os_free(_buf);
  203. }
  204. } else
  205. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
  206. if (hapd->conf->wps_cred_processing == 1)
  207. return 0;
  208. os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
  209. hapd->wps->ssid_len = cred->ssid_len;
  210. hapd->wps->encr_types = cred->encr_type;
  211. hapd->wps->auth_types = cred->auth_type;
  212. if (cred->key_len == 0) {
  213. os_free(hapd->wps->network_key);
  214. hapd->wps->network_key = NULL;
  215. hapd->wps->network_key_len = 0;
  216. } else {
  217. if (hapd->wps->network_key == NULL ||
  218. hapd->wps->network_key_len < cred->key_len) {
  219. hapd->wps->network_key_len = 0;
  220. os_free(hapd->wps->network_key);
  221. hapd->wps->network_key = os_malloc(cred->key_len);
  222. if (hapd->wps->network_key == NULL)
  223. return -1;
  224. }
  225. hapd->wps->network_key_len = cred->key_len;
  226. os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
  227. }
  228. hapd->wps->wps_state = WPS_STATE_CONFIGURED;
  229. len = os_strlen(hapd->iface->config_fname) + 5;
  230. tmp_fname = os_malloc(len);
  231. if (tmp_fname == NULL)
  232. return -1;
  233. os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
  234. oconf = fopen(hapd->iface->config_fname, "r");
  235. if (oconf == NULL) {
  236. wpa_printf(MSG_WARNING, "WPS: Could not open current "
  237. "configuration file");
  238. os_free(tmp_fname);
  239. return -1;
  240. }
  241. nconf = fopen(tmp_fname, "w");
  242. if (nconf == NULL) {
  243. wpa_printf(MSG_WARNING, "WPS: Could not write updated "
  244. "configuration file");
  245. os_free(tmp_fname);
  246. fclose(oconf);
  247. return -1;
  248. }
  249. fprintf(nconf, "# WPS configuration - START\n");
  250. fprintf(nconf, "wps_state=2\n");
  251. fprintf(nconf, "ssid=");
  252. for (i = 0; i < cred->ssid_len; i++)
  253. fputc(cred->ssid[i], nconf);
  254. fprintf(nconf, "\n");
  255. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  256. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  257. wpa = 3;
  258. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  259. wpa = 2;
  260. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  261. wpa = 1;
  262. else
  263. wpa = 0;
  264. if (wpa) {
  265. char *prefix;
  266. fprintf(nconf, "wpa=%d\n", wpa);
  267. fprintf(nconf, "wpa_key_mgmt=");
  268. prefix = "";
  269. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
  270. fprintf(nconf, "WPA-EAP");
  271. prefix = " ";
  272. }
  273. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  274. fprintf(nconf, "%sWPA-PSK", prefix);
  275. fprintf(nconf, "\n");
  276. fprintf(nconf, "wpa_pairwise=");
  277. prefix = "";
  278. if (cred->encr_type & WPS_ENCR_AES) {
  279. fprintf(nconf, "CCMP");
  280. prefix = " ";
  281. }
  282. if (cred->encr_type & WPS_ENCR_TKIP) {
  283. fprintf(nconf, "%sTKIP", prefix);
  284. }
  285. fprintf(nconf, "\n");
  286. if (cred->key_len >= 8 && cred->key_len < 64) {
  287. fprintf(nconf, "wpa_passphrase=");
  288. for (i = 0; i < cred->key_len; i++)
  289. fputc(cred->key[i], nconf);
  290. fprintf(nconf, "\n");
  291. } else if (cred->key_len == 64) {
  292. fprintf(nconf, "wpa_psk=");
  293. for (i = 0; i < cred->key_len; i++)
  294. fputc(cred->key[i], nconf);
  295. fprintf(nconf, "\n");
  296. } else {
  297. wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
  298. "for WPA/WPA2",
  299. (unsigned long) cred->key_len);
  300. }
  301. fprintf(nconf, "auth_algs=1\n");
  302. } else {
  303. if ((cred->auth_type & WPS_AUTH_OPEN) &&
  304. (cred->auth_type & WPS_AUTH_SHARED))
  305. fprintf(nconf, "auth_algs=3\n");
  306. else if (cred->auth_type & WPS_AUTH_SHARED)
  307. fprintf(nconf, "auth_algs=2\n");
  308. else
  309. fprintf(nconf, "auth_algs=1\n");
  310. if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
  311. int key_idx = cred->key_idx;
  312. if (key_idx)
  313. key_idx--;
  314. fprintf(nconf, "wep_default_key=%d\n", key_idx);
  315. fprintf(nconf, "wep_key%d=", key_idx);
  316. if (cred->key_len == 10 || cred->key_len == 26) {
  317. /* WEP key as a hex string */
  318. for (i = 0; i < cred->key_len; i++)
  319. fputc(cred->key[i], nconf);
  320. } else {
  321. /* Raw WEP key; convert to hex */
  322. for (i = 0; i < cred->key_len; i++)
  323. fprintf(nconf, "%02x", cred->key[i]);
  324. }
  325. fprintf(nconf, "\n");
  326. }
  327. }
  328. fprintf(nconf, "# WPS configuration - END\n");
  329. multi_bss = 0;
  330. while (fgets(buf, sizeof(buf), oconf)) {
  331. if (os_strncmp(buf, "bss=", 4) == 0)
  332. multi_bss = 1;
  333. if (!multi_bss &&
  334. (str_starts(buf, "ssid=") ||
  335. str_starts(buf, "auth_algs=") ||
  336. str_starts(buf, "wps_state=") ||
  337. str_starts(buf, "wpa=") ||
  338. str_starts(buf, "wpa_psk=") ||
  339. str_starts(buf, "wpa_pairwise=") ||
  340. str_starts(buf, "rsn_pairwise=") ||
  341. str_starts(buf, "wpa_key_mgmt=") ||
  342. str_starts(buf, "wpa_passphrase="))) {
  343. fprintf(nconf, "#WPS# %s", buf);
  344. } else
  345. fprintf(nconf, "%s", buf);
  346. }
  347. fclose(nconf);
  348. fclose(oconf);
  349. if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
  350. wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
  351. "configuration file: %s", strerror(errno));
  352. os_free(tmp_fname);
  353. return -1;
  354. }
  355. os_free(tmp_fname);
  356. /* Schedule configuration reload after short period of time to allow
  357. * EAP-WSC to be finished.
  358. */
  359. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  360. NULL);
  361. /* TODO: dualband AP may need to update multiple configuration files */
  362. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  363. return 0;
  364. }
  365. static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
  366. struct wps_event_pwd_auth_fail *data)
  367. {
  368. FILE *f;
  369. if (!data->enrollee)
  370. return;
  371. /*
  372. * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
  373. * if this happens multiple times.
  374. */
  375. hapd->ap_pin_failures++;
  376. if (hapd->ap_pin_failures < 4)
  377. return;
  378. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
  379. hapd->wps->ap_setup_locked = 1;
  380. wps_registrar_update_ie(hapd->wps->registrar);
  381. if (hapd->conf->wps_cred_processing == 1)
  382. return;
  383. f = fopen(hapd->iface->config_fname, "a");
  384. if (f == NULL) {
  385. wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
  386. "configuration file");
  387. return;
  388. }
  389. fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
  390. fprintf(f, "ap_setup_locked=1\n");
  391. fclose(f);
  392. /* TODO: dualband AP may need to update multiple configuration files */
  393. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  394. }
  395. static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
  396. union wps_event_data *data)
  397. {
  398. struct hostapd_data *hapd = ctx;
  399. if (event == WPS_EV_PWD_AUTH_FAIL)
  400. hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
  401. }
  402. static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
  403. {
  404. os_free(hapd->wps_beacon_ie);
  405. hapd->wps_beacon_ie = NULL;
  406. hapd->wps_beacon_ie_len = 0;
  407. hostapd_set_wps_beacon_ie(hapd, NULL, 0);
  408. os_free(hapd->wps_probe_resp_ie);
  409. hapd->wps_probe_resp_ie = NULL;
  410. hapd->wps_probe_resp_ie_len = 0;
  411. hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
  412. }
  413. int hostapd_init_wps(struct hostapd_data *hapd,
  414. struct hostapd_bss_config *conf)
  415. {
  416. struct wps_context *wps;
  417. struct wps_registrar_config cfg;
  418. if (conf->wps_state == 0) {
  419. hostapd_wps_clear_ies(hapd);
  420. return 0;
  421. }
  422. wps = os_zalloc(sizeof(*wps));
  423. if (wps == NULL)
  424. return -1;
  425. wps->cred_cb = hostapd_wps_cred_cb;
  426. wps->event_cb = hostapd_wps_event_cb;
  427. wps->cb_ctx = hapd;
  428. os_memset(&cfg, 0, sizeof(cfg));
  429. wps->wps_state = hapd->conf->wps_state;
  430. wps->ap_setup_locked = hapd->conf->ap_setup_locked;
  431. if (is_nil_uuid(hapd->conf->uuid)) {
  432. uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
  433. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  434. wps->uuid, UUID_LEN);
  435. } else
  436. os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
  437. wps->ssid_len = hapd->conf->ssid.ssid_len;
  438. os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
  439. wps->ap = 1;
  440. os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
  441. wps->dev.device_name = hapd->conf->device_name ?
  442. os_strdup(hapd->conf->device_name) : NULL;
  443. wps->dev.manufacturer = hapd->conf->manufacturer ?
  444. os_strdup(hapd->conf->manufacturer) : NULL;
  445. wps->dev.model_name = hapd->conf->model_name ?
  446. os_strdup(hapd->conf->model_name) : NULL;
  447. wps->dev.model_number = hapd->conf->model_number ?
  448. os_strdup(hapd->conf->model_number) : NULL;
  449. wps->dev.serial_number = hapd->conf->serial_number ?
  450. os_strdup(hapd->conf->serial_number) : NULL;
  451. if (hapd->conf->config_methods) {
  452. char *m = hapd->conf->config_methods;
  453. if (os_strstr(m, "label"))
  454. wps->config_methods |= WPS_CONFIG_LABEL;
  455. if (os_strstr(m, "display"))
  456. wps->config_methods |= WPS_CONFIG_DISPLAY;
  457. if (os_strstr(m, "push_button"))
  458. wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
  459. if (os_strstr(m, "keypad"))
  460. wps->config_methods |= WPS_CONFIG_KEYPAD;
  461. }
  462. if (hapd->conf->device_type &&
  463. wps_dev_type_str2bin(hapd->conf->device_type,
  464. wps->dev.pri_dev_type) < 0) {
  465. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  466. os_free(wps);
  467. return -1;
  468. }
  469. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  470. wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  471. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  472. if (conf->wpa & WPA_PROTO_RSN) {
  473. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  474. wps->auth_types |= WPS_AUTH_WPA2PSK;
  475. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  476. wps->auth_types |= WPS_AUTH_WPA2;
  477. if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
  478. wps->encr_types |= WPS_ENCR_AES;
  479. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  480. wps->encr_types |= WPS_ENCR_TKIP;
  481. }
  482. if (conf->wpa & WPA_PROTO_WPA) {
  483. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  484. wps->auth_types |= WPS_AUTH_WPAPSK;
  485. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  486. wps->auth_types |= WPS_AUTH_WPA;
  487. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  488. wps->encr_types |= WPS_ENCR_AES;
  489. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  490. wps->encr_types |= WPS_ENCR_TKIP;
  491. }
  492. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  493. wps->encr_types |= WPS_ENCR_NONE;
  494. wps->auth_types |= WPS_AUTH_OPEN;
  495. } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
  496. wps->encr_types |= WPS_ENCR_WEP;
  497. if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
  498. wps->auth_types |= WPS_AUTH_OPEN;
  499. if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
  500. wps->auth_types |= WPS_AUTH_SHARED;
  501. } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
  502. wps->auth_types |= WPS_AUTH_OPEN;
  503. if (conf->default_wep_key_len)
  504. wps->encr_types |= WPS_ENCR_WEP;
  505. else
  506. wps->encr_types |= WPS_ENCR_NONE;
  507. }
  508. if (conf->ssid.wpa_psk_file) {
  509. /* Use per-device PSKs */
  510. } else if (conf->ssid.wpa_passphrase) {
  511. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  512. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  513. } else if (conf->ssid.wpa_psk) {
  514. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  515. if (wps->network_key == NULL) {
  516. os_free(wps);
  517. return -1;
  518. }
  519. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  520. conf->ssid.wpa_psk->psk, PMK_LEN);
  521. wps->network_key_len = 2 * PMK_LEN;
  522. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  523. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  524. if (wps->network_key == NULL) {
  525. os_free(wps);
  526. return -1;
  527. }
  528. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  529. conf->ssid.wep.len[0]);
  530. wps->network_key_len = conf->ssid.wep.len[0];
  531. }
  532. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  533. /* Override parameters to enable security by default */
  534. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  535. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  536. }
  537. wps->ap_settings = conf->ap_settings;
  538. wps->ap_settings_len = conf->ap_settings_len;
  539. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  540. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  541. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  542. cfg.reg_success_cb = hostapd_wps_reg_success_cb;
  543. cfg.cb_ctx = hapd;
  544. cfg.skip_cred_build = conf->skip_cred_build;
  545. cfg.extra_cred = conf->extra_cred;
  546. cfg.extra_cred_len = conf->extra_cred_len;
  547. cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
  548. conf->skip_cred_build;
  549. if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
  550. cfg.static_wep_only = 1;
  551. wps->registrar = wps_registrar_init(wps, &cfg);
  552. if (wps->registrar == NULL) {
  553. printf("Failed to initialize WPS Registrar\n");
  554. os_free(wps->network_key);
  555. os_free(wps);
  556. return -1;
  557. }
  558. #ifdef CONFIG_WPS_UPNP
  559. wps->friendly_name = hapd->conf->friendly_name;
  560. wps->manufacturer_url = hapd->conf->manufacturer_url;
  561. wps->model_description = hapd->conf->model_description;
  562. wps->model_url = hapd->conf->model_url;
  563. wps->upc = hapd->conf->upc;
  564. if (hostapd_wps_upnp_init(hapd, wps) < 0) {
  565. wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
  566. wps_registrar_deinit(wps->registrar);
  567. os_free(wps->network_key);
  568. os_free(wps);
  569. return -1;
  570. }
  571. #endif /* CONFIG_WPS_UPNP */
  572. hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
  573. hapd->wps = wps;
  574. return 0;
  575. }
  576. void hostapd_deinit_wps(struct hostapd_data *hapd)
  577. {
  578. if (hapd->wps == NULL)
  579. return;
  580. #ifdef CONFIG_WPS_UPNP
  581. hostapd_wps_upnp_deinit(hapd);
  582. #endif /* CONFIG_WPS_UPNP */
  583. wps_registrar_deinit(hapd->wps->registrar);
  584. os_free(hapd->wps->network_key);
  585. wps_device_data_free(&hapd->wps->dev);
  586. wpabuf_free(hapd->wps->dh_pubkey);
  587. wpabuf_free(hapd->wps->dh_privkey);
  588. wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
  589. wpabuf_free(hapd->wps->oob_conf.dev_password);
  590. wps_free_pending_msgs(hapd->wps->upnp_msgs);
  591. os_free(hapd->wps);
  592. hapd->wps = NULL;
  593. hostapd_wps_clear_ies(hapd);
  594. }
  595. int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
  596. const char *pin, int timeout)
  597. {
  598. u8 u[UUID_LEN];
  599. int any = 0;
  600. if (hapd->wps == NULL)
  601. return -1;
  602. if (os_strcmp(uuid, "any") == 0)
  603. any = 1;
  604. else if (uuid_str2bin(uuid, u))
  605. return -1;
  606. return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
  607. (const u8 *) pin, os_strlen(pin),
  608. timeout);
  609. }
  610. int hostapd_wps_button_pushed(struct hostapd_data *hapd)
  611. {
  612. if (hapd->wps == NULL)
  613. return -1;
  614. return wps_registrar_button_pushed(hapd->wps->registrar);
  615. }
  616. #ifdef CONFIG_WPS_OOB
  617. int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
  618. char *path, char *method, char *name)
  619. {
  620. struct wps_context *wps = hapd->wps;
  621. struct oob_device_data *oob_dev;
  622. oob_dev = wps_get_oob_device(device_type);
  623. if (oob_dev == NULL)
  624. return -1;
  625. oob_dev->device_path = path;
  626. oob_dev->device_name = name;
  627. wps->oob_conf.oob_method = wps_get_oob_method(method);
  628. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
  629. /*
  630. * Use pre-configured DH keys in order to be able to write the
  631. * key hash into the OOB file.
  632. */
  633. wpabuf_free(wps->dh_pubkey);
  634. wpabuf_free(wps->dh_privkey);
  635. wps->dh_privkey = NULL;
  636. wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
  637. &wps->dh_privkey);
  638. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  639. if (wps->dh_pubkey == NULL) {
  640. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  641. "Diffie-Hellman handshake");
  642. return -1;
  643. }
  644. }
  645. if (wps_process_oob(wps, oob_dev, 1) < 0)
  646. goto error;
  647. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  648. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  649. hostapd_wps_add_pin(hapd, "any",
  650. wpabuf_head(wps->oob_conf.dev_password), 0) <
  651. 0)
  652. goto error;
  653. return 0;
  654. error:
  655. wpabuf_free(wps->dh_pubkey);
  656. wps->dh_pubkey = NULL;
  657. wpabuf_free(wps->dh_privkey);
  658. wps->dh_privkey = NULL;
  659. return -1;
  660. }
  661. #endif /* CONFIG_WPS_OOB */
  662. static void hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
  663. const u8 *ie, size_t ie_len)
  664. {
  665. struct hostapd_data *hapd = ctx;
  666. struct wpabuf *wps_ie;
  667. if (hapd->wps == NULL)
  668. return;
  669. wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
  670. if (wps_ie == NULL)
  671. return;
  672. if (wpabuf_len(wps_ie) > 0) {
  673. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
  674. #ifdef CONFIG_WPS_UPNP
  675. /* FIX: what exactly should be included in the WLANEvent?
  676. * WPS attributes? Full ProbeReq frame? */
  677. upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
  678. UPNP_WPS_WLANEVENT_TYPE_PROBE,
  679. wps_ie);
  680. #endif /* CONFIG_WPS_UPNP */
  681. }
  682. wpabuf_free(wps_ie);
  683. }
  684. #ifdef CONFIG_WPS_UPNP
  685. static struct wpabuf *
  686. hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
  687. {
  688. struct hostapd_data *hapd = priv;
  689. struct wps_config cfg;
  690. struct wps_data *wps;
  691. enum wsc_op_code op_code;
  692. struct wpabuf *m1;
  693. /*
  694. * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
  695. * registration over UPnP with the AP acting as an Enrollee. It should
  696. * be noted that this is frequently used just to get the device data,
  697. * i.e., there may not be any intent to actually complete the
  698. * registration.
  699. */
  700. if (peer->wps)
  701. wps_deinit(peer->wps);
  702. os_memset(&cfg, 0, sizeof(cfg));
  703. cfg.wps = hapd->wps;
  704. cfg.pin = (u8 *) hapd->conf->ap_pin;
  705. cfg.pin_len = os_strlen(hapd->conf->ap_pin);
  706. wps = wps_init(&cfg);
  707. if (wps == NULL)
  708. return NULL;
  709. m1 = wps_get_msg(wps, &op_code);
  710. if (m1 == NULL) {
  711. wps_deinit(wps);
  712. return NULL;
  713. }
  714. peer->wps = wps;
  715. return m1;
  716. }
  717. static struct wpabuf *
  718. hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
  719. const struct wpabuf *msg)
  720. {
  721. enum wps_process_res res;
  722. enum wsc_op_code op_code;
  723. /* PutMessage: msg = InMessage, return OutMessage */
  724. res = wps_process_msg(peer->wps, WSC_UPnP, msg);
  725. if (res == WPS_FAILURE)
  726. return NULL;
  727. return wps_get_msg(peer->wps, &op_code);
  728. }
  729. static int hostapd_rx_req_put_wlan_response(
  730. void *priv, enum upnp_wps_wlanevent_type ev_type,
  731. const u8 *mac_addr, const struct wpabuf *msg,
  732. enum wps_msg_type msg_type)
  733. {
  734. struct hostapd_data *hapd = priv;
  735. struct sta_info *sta;
  736. struct upnp_pending_message *p;
  737. wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
  738. MACSTR, ev_type, MAC2STR(mac_addr));
  739. wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
  740. wpabuf_head(msg), wpabuf_len(msg));
  741. if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
  742. wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
  743. "PutWLANResponse WLANEventType %d", ev_type);
  744. return -1;
  745. }
  746. /*
  747. * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
  748. * server implementation for delivery to the peer.
  749. */
  750. sta = ap_get_sta(hapd, mac_addr);
  751. if (!sta) {
  752. /*
  753. * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
  754. * Pick STA that is in an ongoing WPS registration without
  755. * checking the MAC address.
  756. */
  757. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
  758. "on NewWLANEventMAC; try wildcard match");
  759. for (sta = hapd->sta_list; sta; sta = sta->next) {
  760. if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
  761. break;
  762. }
  763. }
  764. if (!sta) {
  765. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
  766. return 0;
  767. }
  768. p = os_zalloc(sizeof(*p));
  769. if (p == NULL)
  770. return -1;
  771. os_memcpy(p->addr, sta->addr, ETH_ALEN);
  772. p->msg = wpabuf_dup(msg);
  773. p->type = msg_type;
  774. p->next = hapd->wps->upnp_msgs;
  775. hapd->wps->upnp_msgs = p;
  776. return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
  777. }
  778. static int hostapd_rx_req_set_selected_registrar(void *priv,
  779. const struct wpabuf *msg)
  780. {
  781. struct hostapd_data *hapd = priv;
  782. return wps_registrar_set_selected_registrar(hapd->wps->registrar, msg);
  783. }
  784. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  785. struct wps_context *wps)
  786. {
  787. struct upnp_wps_device_ctx *ctx;
  788. if (!hapd->conf->upnp_iface)
  789. return 0;
  790. ctx = os_zalloc(sizeof(*ctx));
  791. if (ctx == NULL)
  792. return -1;
  793. ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
  794. ctx->rx_req_put_message = hostapd_rx_req_put_message;
  795. ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
  796. ctx->rx_req_set_selected_registrar =
  797. hostapd_rx_req_set_selected_registrar;
  798. hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
  799. if (hapd->wps_upnp == NULL) {
  800. os_free(ctx);
  801. return -1;
  802. }
  803. wps->wps_upnp = hapd->wps_upnp;
  804. if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
  805. upnp_wps_device_deinit(hapd->wps_upnp);
  806. hapd->wps_upnp = NULL;
  807. return -1;
  808. }
  809. return 0;
  810. }
  811. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
  812. {
  813. upnp_wps_device_deinit(hapd->wps_upnp);
  814. }
  815. #endif /* CONFIG_WPS_UPNP */
  816. int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
  817. char *buf, size_t buflen)
  818. {
  819. return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
  820. }