wps_hostapd.c 26 KB

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