wps_hostapd.c 29 KB

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