wps_hostapd.c 27 KB

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