wps_hostapd.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /*
  2. * hostapd / WPS integration
  3. * Copyright (c) 2008-2010, 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "utils/uuid.h"
  18. #include "crypto/dh_groups.h"
  19. #include "common/wpa_ctrl.h"
  20. #include "common/ieee802_11_defs.h"
  21. #include "common/ieee802_11_common.h"
  22. #include "eapol_auth/eapol_auth_sm.h"
  23. #include "eapol_auth/eapol_auth_sm_i.h"
  24. #include "wps/wps.h"
  25. #include "wps/wps_defs.h"
  26. #include "wps/wps_dev_attr.h"
  27. #include "hostapd.h"
  28. #include "ap_config.h"
  29. #include "beacon.h"
  30. #include "sta_info.h"
  31. #include "wps_hostapd.h"
  32. #ifdef CONFIG_WPS_UPNP
  33. #include "wps/wps_upnp.h"
  34. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  35. struct wps_context *wps);
  36. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
  37. #endif /* CONFIG_WPS_UPNP */
  38. static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
  39. const u8 *ie, size_t ie_len);
  40. static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
  41. struct wps_for_each_data {
  42. int (*func)(struct hostapd_data *h, void *ctx);
  43. void *ctx;
  44. };
  45. static int wps_for_each(struct hostapd_iface *iface, void *ctx)
  46. {
  47. struct wps_for_each_data *data = ctx;
  48. size_t j;
  49. if (iface == NULL)
  50. return 0;
  51. for (j = 0; j < iface->num_bss; j++) {
  52. struct hostapd_data *hapd = iface->bss[j];
  53. int ret = data->func(hapd, data->ctx);
  54. if (ret)
  55. return ret;
  56. }
  57. return 0;
  58. }
  59. static int hostapd_wps_for_each(struct hostapd_data *hapd,
  60. int (*func)(struct hostapd_data *h, void *ctx),
  61. void *ctx)
  62. {
  63. struct hostapd_iface *iface = hapd->iface;
  64. struct wps_for_each_data data;
  65. data.func = func;
  66. data.ctx = ctx;
  67. if (iface->for_each_interface == NULL)
  68. return wps_for_each(iface, &data);
  69. return iface->for_each_interface(iface->interfaces, wps_for_each,
  70. &data);
  71. }
  72. static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  73. size_t psk_len)
  74. {
  75. struct hostapd_data *hapd = ctx;
  76. struct hostapd_wpa_psk *p;
  77. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  78. wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
  79. MACSTR, MAC2STR(mac_addr));
  80. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  81. if (psk_len != PMK_LEN) {
  82. wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
  83. (unsigned long) psk_len);
  84. return -1;
  85. }
  86. /* Add the new PSK to runtime PSK list */
  87. p = os_zalloc(sizeof(*p));
  88. if (p == NULL)
  89. return -1;
  90. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  91. os_memcpy(p->psk, psk, PMK_LEN);
  92. p->next = ssid->wpa_psk;
  93. ssid->wpa_psk = p;
  94. if (ssid->wpa_psk_file) {
  95. FILE *f;
  96. char hex[PMK_LEN * 2 + 1];
  97. /* Add the new PSK to PSK list file */
  98. f = fopen(ssid->wpa_psk_file, "a");
  99. if (f == NULL) {
  100. wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
  101. "'%s'", ssid->wpa_psk_file);
  102. return -1;
  103. }
  104. wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
  105. fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
  106. fclose(f);
  107. }
  108. return 0;
  109. }
  110. static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
  111. struct wpabuf *probe_resp_ie)
  112. {
  113. struct hostapd_data *hapd = ctx;
  114. wpabuf_free(hapd->wps_beacon_ie);
  115. hapd->wps_beacon_ie = beacon_ie;
  116. wpabuf_free(hapd->wps_probe_resp_ie);
  117. hapd->wps_probe_resp_ie = probe_resp_ie;
  118. ieee802_11_set_beacon(hapd);
  119. return hapd->drv.set_ap_wps_ie(hapd);
  120. }
  121. static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  122. const struct wps_device_data *dev)
  123. {
  124. struct hostapd_data *hapd = ctx;
  125. char uuid[40], txt[400];
  126. int len;
  127. char devtype[WPS_DEV_TYPE_BUFSIZE];
  128. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  129. return;
  130. wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
  131. len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
  132. "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
  133. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  134. dev->manufacturer, dev->model_name,
  135. dev->model_number, dev->serial_number,
  136. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  137. sizeof(devtype)));
  138. if (len > 0 && len < (int) sizeof(txt))
  139. wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
  140. if (hapd->conf->wps_pin_requests) {
  141. FILE *f;
  142. struct os_time t;
  143. f = fopen(hapd->conf->wps_pin_requests, "a");
  144. if (f == NULL)
  145. return;
  146. os_get_time(&t);
  147. fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
  148. "\t%s\n",
  149. t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
  150. dev->manufacturer, dev->model_name, dev->model_number,
  151. dev->serial_number,
  152. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  153. sizeof(devtype)));
  154. fclose(f);
  155. }
  156. }
  157. static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  158. const u8 *uuid_e)
  159. {
  160. struct hostapd_data *hapd = ctx;
  161. char uuid[40];
  162. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  163. return;
  164. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
  165. MAC2STR(mac_addr), uuid);
  166. if (hapd->wps_reg_success_cb)
  167. hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
  168. mac_addr, uuid_e);
  169. }
  170. static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
  171. const u8 *uuid_e,
  172. const u8 *pri_dev_type,
  173. u16 config_methods,
  174. u16 dev_password_id, u8 request_type,
  175. const char *dev_name)
  176. {
  177. struct hostapd_data *hapd = ctx;
  178. char uuid[40];
  179. char devtype[WPS_DEV_TYPE_BUFSIZE];
  180. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  181. return;
  182. if (dev_name == NULL)
  183. dev_name = "";
  184. wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
  185. " %s %s 0x%x %u %u [%s]",
  186. MAC2STR(addr), uuid,
  187. wps_dev_type_bin2str(pri_dev_type, devtype,
  188. sizeof(devtype)),
  189. config_methods, dev_password_id, request_type, dev_name);
  190. }
  191. static int str_starts(const char *str, const char *start)
  192. {
  193. return os_strncmp(str, start, os_strlen(start)) == 0;
  194. }
  195. static void wps_reload_config(void *eloop_data, void *user_ctx)
  196. {
  197. struct hostapd_iface *iface = eloop_data;
  198. wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
  199. if (iface->reload_config(iface) < 0) {
  200. wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
  201. "configuration");
  202. }
  203. }
  204. static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
  205. {
  206. const struct wps_credential *cred = ctx;
  207. FILE *oconf, *nconf;
  208. size_t len, i;
  209. char *tmp_fname;
  210. char buf[1024];
  211. int multi_bss;
  212. int wpa;
  213. if (hapd->wps == NULL)
  214. return 0;
  215. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  216. cred->cred_attr, cred->cred_attr_len);
  217. wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
  218. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  219. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  220. cred->auth_type);
  221. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  222. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  223. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  224. cred->key, cred->key_len);
  225. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  226. MAC2STR(cred->mac_addr));
  227. if ((hapd->conf->wps_cred_processing == 1 ||
  228. hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
  229. size_t blen = cred->cred_attr_len * 2 + 1;
  230. char *_buf = os_malloc(blen);
  231. if (_buf) {
  232. wpa_snprintf_hex(_buf, blen,
  233. cred->cred_attr, cred->cred_attr_len);
  234. wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
  235. WPS_EVENT_NEW_AP_SETTINGS, _buf);
  236. os_free(_buf);
  237. }
  238. } else
  239. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
  240. if (hapd->conf->wps_cred_processing == 1)
  241. return 0;
  242. os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
  243. hapd->wps->ssid_len = cred->ssid_len;
  244. hapd->wps->encr_types = cred->encr_type;
  245. hapd->wps->auth_types = cred->auth_type;
  246. if (cred->key_len == 0) {
  247. os_free(hapd->wps->network_key);
  248. hapd->wps->network_key = NULL;
  249. hapd->wps->network_key_len = 0;
  250. } else {
  251. if (hapd->wps->network_key == NULL ||
  252. hapd->wps->network_key_len < cred->key_len) {
  253. hapd->wps->network_key_len = 0;
  254. os_free(hapd->wps->network_key);
  255. hapd->wps->network_key = os_malloc(cred->key_len);
  256. if (hapd->wps->network_key == NULL)
  257. return -1;
  258. }
  259. hapd->wps->network_key_len = cred->key_len;
  260. os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
  261. }
  262. hapd->wps->wps_state = WPS_STATE_CONFIGURED;
  263. len = os_strlen(hapd->iface->config_fname) + 5;
  264. tmp_fname = os_malloc(len);
  265. if (tmp_fname == NULL)
  266. return -1;
  267. os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
  268. oconf = fopen(hapd->iface->config_fname, "r");
  269. if (oconf == NULL) {
  270. wpa_printf(MSG_WARNING, "WPS: Could not open current "
  271. "configuration file");
  272. os_free(tmp_fname);
  273. return -1;
  274. }
  275. nconf = fopen(tmp_fname, "w");
  276. if (nconf == NULL) {
  277. wpa_printf(MSG_WARNING, "WPS: Could not write updated "
  278. "configuration file");
  279. os_free(tmp_fname);
  280. fclose(oconf);
  281. return -1;
  282. }
  283. fprintf(nconf, "# WPS configuration - START\n");
  284. fprintf(nconf, "wps_state=2\n");
  285. fprintf(nconf, "ssid=");
  286. for (i = 0; i < cred->ssid_len; i++)
  287. fputc(cred->ssid[i], nconf);
  288. fprintf(nconf, "\n");
  289. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  290. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  291. wpa = 3;
  292. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  293. wpa = 2;
  294. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  295. wpa = 1;
  296. else
  297. wpa = 0;
  298. if (wpa) {
  299. char *prefix;
  300. fprintf(nconf, "wpa=%d\n", wpa);
  301. fprintf(nconf, "wpa_key_mgmt=");
  302. prefix = "";
  303. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
  304. fprintf(nconf, "WPA-EAP");
  305. prefix = " ";
  306. }
  307. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  308. fprintf(nconf, "%sWPA-PSK", prefix);
  309. fprintf(nconf, "\n");
  310. fprintf(nconf, "wpa_pairwise=");
  311. prefix = "";
  312. if (cred->encr_type & WPS_ENCR_AES) {
  313. fprintf(nconf, "CCMP");
  314. prefix = " ";
  315. }
  316. if (cred->encr_type & WPS_ENCR_TKIP) {
  317. fprintf(nconf, "%sTKIP", prefix);
  318. }
  319. fprintf(nconf, "\n");
  320. if (cred->key_len >= 8 && cred->key_len < 64) {
  321. fprintf(nconf, "wpa_passphrase=");
  322. for (i = 0; i < cred->key_len; i++)
  323. fputc(cred->key[i], nconf);
  324. fprintf(nconf, "\n");
  325. } else if (cred->key_len == 64) {
  326. fprintf(nconf, "wpa_psk=");
  327. for (i = 0; i < cred->key_len; i++)
  328. fputc(cred->key[i], nconf);
  329. fprintf(nconf, "\n");
  330. } else {
  331. wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
  332. "for WPA/WPA2",
  333. (unsigned long) cred->key_len);
  334. }
  335. fprintf(nconf, "auth_algs=1\n");
  336. } else {
  337. if ((cred->auth_type & WPS_AUTH_OPEN) &&
  338. (cred->auth_type & WPS_AUTH_SHARED))
  339. fprintf(nconf, "auth_algs=3\n");
  340. else if (cred->auth_type & WPS_AUTH_SHARED)
  341. fprintf(nconf, "auth_algs=2\n");
  342. else
  343. fprintf(nconf, "auth_algs=1\n");
  344. if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
  345. int key_idx = cred->key_idx;
  346. if (key_idx)
  347. key_idx--;
  348. fprintf(nconf, "wep_default_key=%d\n", key_idx);
  349. fprintf(nconf, "wep_key%d=", key_idx);
  350. if (cred->key_len == 10 || cred->key_len == 26) {
  351. /* WEP key as a hex string */
  352. for (i = 0; i < cred->key_len; i++)
  353. fputc(cred->key[i], nconf);
  354. } else {
  355. /* Raw WEP key; convert to hex */
  356. for (i = 0; i < cred->key_len; i++)
  357. fprintf(nconf, "%02x", cred->key[i]);
  358. }
  359. fprintf(nconf, "\n");
  360. }
  361. }
  362. fprintf(nconf, "# WPS configuration - END\n");
  363. multi_bss = 0;
  364. while (fgets(buf, sizeof(buf), oconf)) {
  365. if (os_strncmp(buf, "bss=", 4) == 0)
  366. multi_bss = 1;
  367. if (!multi_bss &&
  368. (str_starts(buf, "ssid=") ||
  369. str_starts(buf, "auth_algs=") ||
  370. str_starts(buf, "wps_state=") ||
  371. str_starts(buf, "wpa=") ||
  372. str_starts(buf, "wpa_psk=") ||
  373. str_starts(buf, "wpa_pairwise=") ||
  374. str_starts(buf, "rsn_pairwise=") ||
  375. str_starts(buf, "wpa_key_mgmt=") ||
  376. str_starts(buf, "wpa_passphrase="))) {
  377. fprintf(nconf, "#WPS# %s", buf);
  378. } else
  379. fprintf(nconf, "%s", buf);
  380. }
  381. fclose(nconf);
  382. fclose(oconf);
  383. if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
  384. wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
  385. "configuration file: %s", strerror(errno));
  386. os_free(tmp_fname);
  387. return -1;
  388. }
  389. os_free(tmp_fname);
  390. /* Schedule configuration reload after short period of time to allow
  391. * EAP-WSC to be finished.
  392. */
  393. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  394. NULL);
  395. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  396. return 0;
  397. }
  398. static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
  399. {
  400. struct hostapd_data *hapd = ctx;
  401. return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
  402. }
  403. static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
  404. {
  405. struct hostapd_data *hapd = eloop_data;
  406. if (hapd->conf->ap_setup_locked)
  407. return;
  408. wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
  409. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
  410. hapd->wps->ap_setup_locked = 0;
  411. wps_registrar_update_ie(hapd->wps->registrar);
  412. }
  413. static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
  414. {
  415. struct wps_event_pwd_auth_fail *data = ctx;
  416. if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
  417. return 0;
  418. /*
  419. * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
  420. * for some time if this happens multiple times to slow down brute
  421. * force attacks.
  422. */
  423. hapd->ap_pin_failures++;
  424. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  425. hapd->ap_pin_failures);
  426. if (hapd->ap_pin_failures < 3)
  427. return 0;
  428. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
  429. hapd->wps->ap_setup_locked = 1;
  430. wps_registrar_update_ie(hapd->wps->registrar);
  431. if (!hapd->conf->ap_setup_locked) {
  432. if (hapd->ap_pin_lockout_time == 0)
  433. hapd->ap_pin_lockout_time = 60;
  434. else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
  435. (hapd->ap_pin_failures % 3) == 0)
  436. hapd->ap_pin_lockout_time *= 2;
  437. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
  438. hapd->ap_pin_lockout_time);
  439. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  440. eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
  441. hostapd_wps_reenable_ap_pin, hapd,
  442. NULL);
  443. }
  444. return 0;
  445. }
  446. static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
  447. struct wps_event_pwd_auth_fail *data)
  448. {
  449. hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
  450. }
  451. static void hostapd_wps_event_fail(struct hostapd_data *hapd,
  452. struct wps_event_fail *fail)
  453. {
  454. wpa_msg(hapd->msg_ctx, MSG_INFO,
  455. WPS_EVENT_FAIL "msg=%d config_error=%d",
  456. fail->msg, fail->config_error);
  457. }
  458. static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
  459. union wps_event_data *data)
  460. {
  461. struct hostapd_data *hapd = ctx;
  462. switch (event) {
  463. case WPS_EV_M2D:
  464. break;
  465. case WPS_EV_FAIL:
  466. hostapd_wps_event_fail(hapd, &data->fail);
  467. break;
  468. case WPS_EV_SUCCESS:
  469. break;
  470. case WPS_EV_PWD_AUTH_FAIL:
  471. hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
  472. break;
  473. case WPS_EV_PBC_OVERLAP:
  474. break;
  475. case WPS_EV_PBC_TIMEOUT:
  476. break;
  477. case WPS_EV_ER_AP_ADD:
  478. break;
  479. case WPS_EV_ER_AP_REMOVE:
  480. break;
  481. case WPS_EV_ER_ENROLLEE_ADD:
  482. break;
  483. case WPS_EV_ER_ENROLLEE_REMOVE:
  484. break;
  485. case WPS_EV_ER_AP_SETTINGS:
  486. break;
  487. }
  488. if (hapd->wps_event_cb)
  489. hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
  490. }
  491. static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
  492. {
  493. wpabuf_free(hapd->wps_beacon_ie);
  494. hapd->wps_beacon_ie = NULL;
  495. wpabuf_free(hapd->wps_probe_resp_ie);
  496. hapd->wps_probe_resp_ie = NULL;
  497. hapd->drv.set_ap_wps_ie(hapd);
  498. }
  499. static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
  500. {
  501. const u8 **uuid = ctx;
  502. size_t j;
  503. if (iface == NULL)
  504. return 0;
  505. for (j = 0; j < iface->num_bss; j++) {
  506. struct hostapd_data *hapd = iface->bss[j];
  507. if (hapd->wps && !is_nil_uuid(hapd->wps->uuid)) {
  508. *uuid = hapd->wps->uuid;
  509. return 1;
  510. }
  511. }
  512. return 0;
  513. }
  514. static const u8 * get_own_uuid(struct hostapd_iface *iface)
  515. {
  516. const u8 *uuid;
  517. if (iface->for_each_interface == NULL)
  518. return NULL;
  519. uuid = NULL;
  520. iface->for_each_interface(iface->interfaces, get_uuid_cb, &uuid);
  521. return uuid;
  522. }
  523. static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
  524. {
  525. int *count= ctx;
  526. (*count)++;
  527. return 0;
  528. }
  529. static int interface_count(struct hostapd_iface *iface)
  530. {
  531. int count = 0;
  532. if (iface->for_each_interface == NULL)
  533. return 0;
  534. iface->for_each_interface(iface->interfaces, count_interface_cb,
  535. &count);
  536. return count;
  537. }
  538. int hostapd_init_wps(struct hostapd_data *hapd,
  539. struct hostapd_bss_config *conf)
  540. {
  541. struct wps_context *wps;
  542. struct wps_registrar_config cfg;
  543. if (conf->wps_state == 0) {
  544. hostapd_wps_clear_ies(hapd);
  545. return 0;
  546. }
  547. wps = os_zalloc(sizeof(*wps));
  548. if (wps == NULL)
  549. return -1;
  550. wps->cred_cb = hostapd_wps_cred_cb;
  551. wps->event_cb = hostapd_wps_event_cb;
  552. wps->cb_ctx = hapd;
  553. os_memset(&cfg, 0, sizeof(cfg));
  554. wps->wps_state = hapd->conf->wps_state;
  555. wps->ap_setup_locked = hapd->conf->ap_setup_locked;
  556. if (is_nil_uuid(hapd->conf->uuid)) {
  557. const u8 *uuid;
  558. uuid = get_own_uuid(hapd->iface);
  559. if (uuid) {
  560. os_memcpy(wps->uuid, uuid, UUID_LEN);
  561. wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
  562. "interface", wps->uuid, UUID_LEN);
  563. } else {
  564. uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
  565. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  566. "address", wps->uuid, UUID_LEN);
  567. }
  568. } else {
  569. os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
  570. wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
  571. wps->uuid, UUID_LEN);
  572. }
  573. wps->ssid_len = hapd->conf->ssid.ssid_len;
  574. os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
  575. wps->ap = 1;
  576. os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
  577. wps->dev.device_name = hapd->conf->device_name ?
  578. os_strdup(hapd->conf->device_name) : NULL;
  579. wps->dev.manufacturer = hapd->conf->manufacturer ?
  580. os_strdup(hapd->conf->manufacturer) : NULL;
  581. wps->dev.model_name = hapd->conf->model_name ?
  582. os_strdup(hapd->conf->model_name) : NULL;
  583. wps->dev.model_number = hapd->conf->model_number ?
  584. os_strdup(hapd->conf->model_number) : NULL;
  585. wps->dev.serial_number = hapd->conf->serial_number ?
  586. os_strdup(hapd->conf->serial_number) : NULL;
  587. wps->config_methods =
  588. wps_config_methods_str2bin(hapd->conf->config_methods);
  589. #ifdef CONFIG_WPS2
  590. if ((wps->config_methods &
  591. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  592. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  593. wpa_printf(MSG_INFO, "WPS: Converting display to "
  594. "virtual_display for WPS 2.0 compliance");
  595. wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  596. }
  597. if ((wps->config_methods &
  598. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  599. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  600. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  601. "virtual_push_button for WPS 2.0 compliance");
  602. wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  603. }
  604. #endif /* CONFIG_WPS2 */
  605. if (hapd->conf->device_type &&
  606. wps_dev_type_str2bin(hapd->conf->device_type,
  607. wps->dev.pri_dev_type) < 0) {
  608. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  609. os_free(wps);
  610. return -1;
  611. }
  612. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  613. wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  614. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  615. if (conf->wpa & WPA_PROTO_RSN) {
  616. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  617. wps->auth_types |= WPS_AUTH_WPA2PSK;
  618. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  619. wps->auth_types |= WPS_AUTH_WPA2;
  620. if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
  621. wps->encr_types |= WPS_ENCR_AES;
  622. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  623. wps->encr_types |= WPS_ENCR_TKIP;
  624. }
  625. if (conf->wpa & WPA_PROTO_WPA) {
  626. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  627. wps->auth_types |= WPS_AUTH_WPAPSK;
  628. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  629. wps->auth_types |= WPS_AUTH_WPA;
  630. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  631. wps->encr_types |= WPS_ENCR_AES;
  632. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  633. wps->encr_types |= WPS_ENCR_TKIP;
  634. }
  635. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  636. wps->encr_types |= WPS_ENCR_NONE;
  637. wps->auth_types |= WPS_AUTH_OPEN;
  638. } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
  639. wps->encr_types |= WPS_ENCR_WEP;
  640. if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
  641. wps->auth_types |= WPS_AUTH_OPEN;
  642. if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
  643. wps->auth_types |= WPS_AUTH_SHARED;
  644. } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
  645. wps->auth_types |= WPS_AUTH_OPEN;
  646. if (conf->default_wep_key_len)
  647. wps->encr_types |= WPS_ENCR_WEP;
  648. else
  649. wps->encr_types |= WPS_ENCR_NONE;
  650. }
  651. if (conf->ssid.wpa_psk_file) {
  652. /* Use per-device PSKs */
  653. } else if (conf->ssid.wpa_passphrase) {
  654. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  655. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  656. } else if (conf->ssid.wpa_psk) {
  657. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  658. if (wps->network_key == NULL) {
  659. os_free(wps);
  660. return -1;
  661. }
  662. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  663. conf->ssid.wpa_psk->psk, PMK_LEN);
  664. wps->network_key_len = 2 * PMK_LEN;
  665. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  666. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  667. if (wps->network_key == NULL) {
  668. os_free(wps);
  669. return -1;
  670. }
  671. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  672. conf->ssid.wep.len[0]);
  673. wps->network_key_len = conf->ssid.wep.len[0];
  674. }
  675. if (conf->ssid.wpa_psk) {
  676. os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
  677. wps->psk_set = 1;
  678. }
  679. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  680. /* Override parameters to enable security by default */
  681. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  682. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  683. }
  684. wps->ap_settings = conf->ap_settings;
  685. wps->ap_settings_len = conf->ap_settings_len;
  686. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  687. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  688. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  689. cfg.reg_success_cb = hostapd_wps_reg_success_cb;
  690. cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
  691. cfg.cb_ctx = hapd;
  692. cfg.skip_cred_build = conf->skip_cred_build;
  693. cfg.extra_cred = conf->extra_cred;
  694. cfg.extra_cred_len = conf->extra_cred_len;
  695. cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
  696. conf->skip_cred_build;
  697. if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
  698. cfg.static_wep_only = 1;
  699. cfg.dualband = interface_count(hapd->iface) > 1;
  700. if (cfg.dualband)
  701. wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
  702. wps->registrar = wps_registrar_init(wps, &cfg);
  703. if (wps->registrar == NULL) {
  704. wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
  705. os_free(wps->network_key);
  706. os_free(wps);
  707. return -1;
  708. }
  709. #ifdef CONFIG_WPS_UPNP
  710. wps->friendly_name = hapd->conf->friendly_name;
  711. wps->manufacturer_url = hapd->conf->manufacturer_url;
  712. wps->model_description = hapd->conf->model_description;
  713. wps->model_url = hapd->conf->model_url;
  714. wps->upc = hapd->conf->upc;
  715. if (hostapd_wps_upnp_init(hapd, wps) < 0) {
  716. wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
  717. wps_registrar_deinit(wps->registrar);
  718. os_free(wps->network_key);
  719. os_free(wps);
  720. return -1;
  721. }
  722. #endif /* CONFIG_WPS_UPNP */
  723. hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
  724. hapd->wps = wps;
  725. return 0;
  726. }
  727. void hostapd_deinit_wps(struct hostapd_data *hapd)
  728. {
  729. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  730. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  731. if (hapd->wps == NULL)
  732. return;
  733. #ifdef CONFIG_WPS_UPNP
  734. hostapd_wps_upnp_deinit(hapd);
  735. #endif /* CONFIG_WPS_UPNP */
  736. wps_registrar_deinit(hapd->wps->registrar);
  737. os_free(hapd->wps->network_key);
  738. wps_device_data_free(&hapd->wps->dev);
  739. wpabuf_free(hapd->wps->dh_pubkey);
  740. wpabuf_free(hapd->wps->dh_privkey);
  741. wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
  742. wpabuf_free(hapd->wps->oob_conf.dev_password);
  743. wps_free_pending_msgs(hapd->wps->upnp_msgs);
  744. os_free(hapd->wps);
  745. hapd->wps = NULL;
  746. hostapd_wps_clear_ies(hapd);
  747. }
  748. void hostapd_update_wps(struct hostapd_data *hapd)
  749. {
  750. if (hapd->wps == NULL)
  751. return;
  752. #ifdef CONFIG_WPS_UPNP
  753. hapd->wps->friendly_name = hapd->conf->friendly_name;
  754. hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
  755. hapd->wps->model_description = hapd->conf->model_description;
  756. hapd->wps->model_url = hapd->conf->model_url;
  757. hapd->wps->upc = hapd->conf->upc;
  758. #endif /* CONFIG_WPS_UPNP */
  759. if (hapd->conf->wps_state)
  760. wps_registrar_update_ie(hapd->wps->registrar);
  761. else
  762. hostapd_deinit_wps(hapd);
  763. }
  764. struct wps_add_pin_data {
  765. const u8 *addr;
  766. const u8 *uuid;
  767. const u8 *pin;
  768. size_t pin_len;
  769. int timeout;
  770. int added;
  771. };
  772. static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
  773. {
  774. struct wps_add_pin_data *data = ctx;
  775. int ret;
  776. if (hapd->wps == NULL)
  777. return 0;
  778. ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
  779. data->uuid, data->pin, data->pin_len,
  780. data->timeout);
  781. if (ret == 0)
  782. data->added++;
  783. return ret;
  784. }
  785. int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
  786. const char *uuid, const char *pin, int timeout)
  787. {
  788. u8 u[UUID_LEN];
  789. struct wps_add_pin_data data;
  790. data.addr = addr;
  791. data.uuid = u;
  792. data.pin = (const u8 *) pin;
  793. data.pin_len = os_strlen(pin);
  794. data.timeout = timeout;
  795. data.added = 0;
  796. if (os_strcmp(uuid, "any") == 0)
  797. data.uuid = NULL;
  798. else {
  799. if (uuid_str2bin(uuid, u))
  800. return -1;
  801. data.uuid = u;
  802. }
  803. if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
  804. return -1;
  805. return data.added ? 0 : -1;
  806. }
  807. static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
  808. {
  809. if (hapd->wps == NULL)
  810. return 0;
  811. return wps_registrar_button_pushed(hapd->wps->registrar);
  812. }
  813. int hostapd_wps_button_pushed(struct hostapd_data *hapd)
  814. {
  815. return hostapd_wps_for_each(hapd, wps_button_pushed, NULL);
  816. }
  817. #ifdef CONFIG_WPS_OOB
  818. int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
  819. char *path, char *method, char *name)
  820. {
  821. struct wps_context *wps = hapd->wps;
  822. struct oob_device_data *oob_dev;
  823. oob_dev = wps_get_oob_device(device_type);
  824. if (oob_dev == NULL)
  825. return -1;
  826. oob_dev->device_path = path;
  827. oob_dev->device_name = name;
  828. wps->oob_conf.oob_method = wps_get_oob_method(method);
  829. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
  830. /*
  831. * Use pre-configured DH keys in order to be able to write the
  832. * key hash into the OOB file.
  833. */
  834. wpabuf_free(wps->dh_pubkey);
  835. wpabuf_free(wps->dh_privkey);
  836. wps->dh_privkey = NULL;
  837. wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
  838. &wps->dh_privkey);
  839. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  840. if (wps->dh_pubkey == NULL) {
  841. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  842. "Diffie-Hellman handshake");
  843. return -1;
  844. }
  845. }
  846. if (wps_process_oob(wps, oob_dev, 1) < 0)
  847. goto error;
  848. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  849. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  850. hostapd_wps_add_pin(hapd, NULL, "any",
  851. wpabuf_head(wps->oob_conf.dev_password), 0) <
  852. 0)
  853. goto error;
  854. return 0;
  855. error:
  856. wpabuf_free(wps->dh_pubkey);
  857. wps->dh_pubkey = NULL;
  858. wpabuf_free(wps->dh_privkey);
  859. wps->dh_privkey = NULL;
  860. return -1;
  861. }
  862. #endif /* CONFIG_WPS_OOB */
  863. static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
  864. const u8 *ie, size_t ie_len)
  865. {
  866. struct hostapd_data *hapd = ctx;
  867. struct wpabuf *wps_ie;
  868. struct ieee802_11_elems elems;
  869. if (hapd->wps == NULL)
  870. return 0;
  871. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  872. wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
  873. MACSTR, MAC2STR(addr));
  874. return 0;
  875. }
  876. if (elems.ssid && elems.ssid_len > 0 &&
  877. (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  878. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
  879. 0))
  880. return 0; /* Not for us */
  881. wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
  882. if (wps_ie == NULL)
  883. return 0;
  884. if (wps_validate_probe_req(wps_ie, addr) < 0) {
  885. wpabuf_free(wps_ie);
  886. return 0;
  887. }
  888. if (wpabuf_len(wps_ie) > 0) {
  889. int p2p_wildcard = 0;
  890. #ifdef CONFIG_P2P
  891. if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
  892. os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
  893. P2P_WILDCARD_SSID_LEN) == 0)
  894. p2p_wildcard = 1;
  895. #endif /* CONFIG_P2P */
  896. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
  897. p2p_wildcard);
  898. #ifdef CONFIG_WPS_UPNP
  899. /* FIX: what exactly should be included in the WLANEvent?
  900. * WPS attributes? Full ProbeReq frame? */
  901. if (!p2p_wildcard)
  902. upnp_wps_device_send_wlan_event(
  903. hapd->wps_upnp, addr,
  904. UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
  905. #endif /* CONFIG_WPS_UPNP */
  906. }
  907. wpabuf_free(wps_ie);
  908. return 0;
  909. }
  910. #ifdef CONFIG_WPS_UPNP
  911. static int hostapd_rx_req_put_wlan_response(
  912. void *priv, enum upnp_wps_wlanevent_type ev_type,
  913. const u8 *mac_addr, const struct wpabuf *msg,
  914. enum wps_msg_type msg_type)
  915. {
  916. struct hostapd_data *hapd = priv;
  917. struct sta_info *sta;
  918. struct upnp_pending_message *p;
  919. wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
  920. MACSTR, ev_type, MAC2STR(mac_addr));
  921. wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
  922. wpabuf_head(msg), wpabuf_len(msg));
  923. if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
  924. wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
  925. "PutWLANResponse WLANEventType %d", ev_type);
  926. return -1;
  927. }
  928. /*
  929. * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
  930. * server implementation for delivery to the peer.
  931. */
  932. sta = ap_get_sta(hapd, mac_addr);
  933. #ifndef CONFIG_WPS_STRICT
  934. if (!sta) {
  935. /*
  936. * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
  937. * Pick STA that is in an ongoing WPS registration without
  938. * checking the MAC address.
  939. */
  940. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
  941. "on NewWLANEventMAC; try wildcard match");
  942. for (sta = hapd->sta_list; sta; sta = sta->next) {
  943. if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
  944. break;
  945. }
  946. }
  947. #endif /* CONFIG_WPS_STRICT */
  948. if (!sta) {
  949. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
  950. return 0;
  951. }
  952. p = os_zalloc(sizeof(*p));
  953. if (p == NULL)
  954. return -1;
  955. os_memcpy(p->addr, sta->addr, ETH_ALEN);
  956. p->msg = wpabuf_dup(msg);
  957. p->type = msg_type;
  958. p->next = hapd->wps->upnp_msgs;
  959. hapd->wps->upnp_msgs = p;
  960. return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
  961. }
  962. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  963. struct wps_context *wps)
  964. {
  965. struct upnp_wps_device_ctx *ctx;
  966. if (!hapd->conf->upnp_iface)
  967. return 0;
  968. ctx = os_zalloc(sizeof(*ctx));
  969. if (ctx == NULL)
  970. return -1;
  971. ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
  972. if (hapd->conf->ap_pin)
  973. ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
  974. hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
  975. if (hapd->wps_upnp == NULL) {
  976. os_free(ctx);
  977. return -1;
  978. }
  979. wps->wps_upnp = hapd->wps_upnp;
  980. if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
  981. upnp_wps_device_deinit(hapd->wps_upnp);
  982. hapd->wps_upnp = NULL;
  983. return -1;
  984. }
  985. return 0;
  986. }
  987. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
  988. {
  989. upnp_wps_device_deinit(hapd->wps_upnp);
  990. }
  991. #endif /* CONFIG_WPS_UPNP */
  992. int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
  993. char *buf, size_t buflen)
  994. {
  995. if (hapd->wps == NULL)
  996. return 0;
  997. return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
  998. }
  999. static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  1000. {
  1001. struct hostapd_data *hapd = eloop_data;
  1002. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  1003. hostapd_wps_ap_pin_disable(hapd);
  1004. }
  1005. static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
  1006. {
  1007. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  1008. hapd->ap_pin_failures = 0;
  1009. hapd->conf->ap_setup_locked = 0;
  1010. if (hapd->wps->ap_setup_locked) {
  1011. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
  1012. hapd->wps->ap_setup_locked = 0;
  1013. wps_registrar_update_ie(hapd->wps->registrar);
  1014. }
  1015. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  1016. if (timeout > 0)
  1017. eloop_register_timeout(timeout, 0,
  1018. hostapd_wps_ap_pin_timeout, hapd, NULL);
  1019. }
  1020. static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
  1021. {
  1022. os_free(hapd->conf->ap_pin);
  1023. hapd->conf->ap_pin = NULL;
  1024. #ifdef CONFIG_WPS_UPNP
  1025. upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
  1026. #endif /* CONFIG_WPS_UPNP */
  1027. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  1028. return 0;
  1029. }
  1030. void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
  1031. {
  1032. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  1033. hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
  1034. }
  1035. struct wps_ap_pin_data {
  1036. char pin_txt[9];
  1037. int timeout;
  1038. };
  1039. static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
  1040. {
  1041. struct wps_ap_pin_data *data = ctx;
  1042. os_free(hapd->conf->ap_pin);
  1043. hapd->conf->ap_pin = os_strdup(data->pin_txt);
  1044. #ifdef CONFIG_WPS_UPNP
  1045. upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
  1046. #endif /* CONFIG_WPS_UPNP */
  1047. hostapd_wps_ap_pin_enable(hapd, data->timeout);
  1048. return 0;
  1049. }
  1050. const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
  1051. {
  1052. unsigned int pin;
  1053. struct wps_ap_pin_data data;
  1054. pin = wps_generate_pin();
  1055. os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%u", pin);
  1056. data.timeout = timeout;
  1057. hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
  1058. return hapd->conf->ap_pin;
  1059. }
  1060. const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
  1061. {
  1062. return hapd->conf->ap_pin;
  1063. }
  1064. int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
  1065. int timeout)
  1066. {
  1067. struct wps_ap_pin_data data;
  1068. int ret;
  1069. ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
  1070. if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
  1071. return -1;
  1072. data.timeout = timeout;
  1073. return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
  1074. }
  1075. static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
  1076. {
  1077. if (hapd->wps)
  1078. wps_registrar_update_ie(hapd->wps->registrar);
  1079. return 0;
  1080. }
  1081. void hostapd_wps_update_ie(struct hostapd_data *hapd)
  1082. {
  1083. hostapd_wps_for_each(hapd, wps_update_ie, NULL);
  1084. }
  1085. int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
  1086. const char *auth, const char *encr, const char *key)
  1087. {
  1088. struct wps_credential cred;
  1089. size_t len;
  1090. os_memset(&cred, 0, sizeof(cred));
  1091. len = os_strlen(ssid);
  1092. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1093. hexstr2bin(ssid, cred.ssid, len / 2))
  1094. return -1;
  1095. cred.ssid_len = len / 2;
  1096. if (os_strncmp(auth, "OPEN", 4) == 0)
  1097. cred.auth_type = WPS_AUTH_OPEN;
  1098. else if (os_strncmp(auth, "WPAPSK", 6) == 0)
  1099. cred.auth_type = WPS_AUTH_WPAPSK;
  1100. else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
  1101. cred.auth_type = WPS_AUTH_WPA2PSK;
  1102. else
  1103. return -1;
  1104. if (encr) {
  1105. if (os_strncmp(encr, "NONE", 4) == 0)
  1106. cred.encr_type = WPS_ENCR_NONE;
  1107. else if (os_strncmp(encr, "WEP", 3) == 0)
  1108. cred.encr_type = WPS_ENCR_WEP;
  1109. else if (os_strncmp(encr, "TKIP", 4) == 0)
  1110. cred.encr_type = WPS_ENCR_TKIP;
  1111. else if (os_strncmp(encr, "CCMP", 4) == 0)
  1112. cred.encr_type = WPS_ENCR_AES;
  1113. else
  1114. return -1;
  1115. } else
  1116. cred.encr_type = WPS_ENCR_NONE;
  1117. if (key) {
  1118. len = os_strlen(key);
  1119. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1120. hexstr2bin(key, cred.key, len / 2))
  1121. return -1;
  1122. cred.key_len = len / 2;
  1123. }
  1124. return wps_registrar_config_ap(hapd->wps->registrar, &cred);
  1125. }