wps_hostapd.c 28 KB

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