wps_hostapd.c 29 KB

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