wps_hostapd.c 26 KB

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