wps_hostapd.c 26 KB

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