wps_hostapd.c 29 KB

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