wps_hostapd.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  1. /*
  2. * hostapd / WPS integration
  3. * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/uuid.h"
  12. #include "common/wpa_ctrl.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "eapol_auth/eapol_auth_sm.h"
  16. #include "eapol_auth/eapol_auth_sm_i.h"
  17. #include "wps/wps.h"
  18. #include "wps/wps_defs.h"
  19. #include "wps/wps_dev_attr.h"
  20. #include "wps/wps_attr_parse.h"
  21. #include "hostapd.h"
  22. #include "ap_config.h"
  23. #include "ap_drv_ops.h"
  24. #include "beacon.h"
  25. #include "sta_info.h"
  26. #include "wps_hostapd.h"
  27. #ifdef CONFIG_WPS_UPNP
  28. #include "wps/wps_upnp.h"
  29. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  30. struct wps_context *wps);
  31. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
  32. #endif /* CONFIG_WPS_UPNP */
  33. static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
  34. const u8 *bssid,
  35. const u8 *ie, size_t ie_len,
  36. int ssi_signal);
  37. static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
  38. static void hostapd_wps_nfc_clear(struct wps_context *wps);
  39. struct wps_for_each_data {
  40. int (*func)(struct hostapd_data *h, void *ctx);
  41. void *ctx;
  42. struct hostapd_data *calling_hapd;
  43. };
  44. static int wps_for_each(struct hostapd_iface *iface, void *ctx)
  45. {
  46. struct wps_for_each_data *data = ctx;
  47. size_t j;
  48. if (iface == NULL)
  49. return 0;
  50. for (j = 0; j < iface->num_bss; j++) {
  51. struct hostapd_data *hapd = iface->bss[j];
  52. int ret;
  53. if (hapd != data->calling_hapd &&
  54. (hapd->conf->wps_independent ||
  55. data->calling_hapd->conf->wps_independent))
  56. continue;
  57. ret = data->func(hapd, data->ctx);
  58. if (ret)
  59. return ret;
  60. }
  61. return 0;
  62. }
  63. static int hostapd_wps_for_each(struct hostapd_data *hapd,
  64. int (*func)(struct hostapd_data *h, void *ctx),
  65. void *ctx)
  66. {
  67. struct hostapd_iface *iface = hapd->iface;
  68. struct wps_for_each_data data;
  69. data.func = func;
  70. data.ctx = ctx;
  71. data.calling_hapd = hapd;
  72. if (iface->interfaces == NULL ||
  73. iface->interfaces->for_each_interface == NULL)
  74. return wps_for_each(iface, &data);
  75. return iface->interfaces->for_each_interface(iface->interfaces,
  76. wps_for_each, &data);
  77. }
  78. static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
  79. const u8 *p2p_dev_addr, const u8 *psk,
  80. size_t psk_len)
  81. {
  82. struct hostapd_data *hapd = ctx;
  83. struct hostapd_wpa_psk *p;
  84. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  85. if (is_zero_ether_addr(p2p_dev_addr)) {
  86. wpa_printf(MSG_DEBUG,
  87. "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
  88. MAC2STR(mac_addr));
  89. } else {
  90. wpa_printf(MSG_DEBUG,
  91. "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
  92. " P2P Device Addr " MACSTR,
  93. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  94. }
  95. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  96. if (psk_len != PMK_LEN) {
  97. wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
  98. (unsigned long) psk_len);
  99. return -1;
  100. }
  101. /* Add the new PSK to runtime PSK list */
  102. p = os_zalloc(sizeof(*p));
  103. if (p == NULL)
  104. return -1;
  105. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  106. os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
  107. os_memcpy(p->psk, psk, PMK_LEN);
  108. if (hapd->new_psk_cb) {
  109. hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
  110. psk, psk_len);
  111. }
  112. p->next = ssid->wpa_psk;
  113. ssid->wpa_psk = p;
  114. if (ssid->wpa_psk_file) {
  115. FILE *f;
  116. char hex[PMK_LEN * 2 + 1];
  117. /* Add the new PSK to PSK list file */
  118. f = fopen(ssid->wpa_psk_file, "a");
  119. if (f == NULL) {
  120. wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
  121. "'%s'", ssid->wpa_psk_file);
  122. return -1;
  123. }
  124. wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
  125. fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
  126. fclose(f);
  127. }
  128. return 0;
  129. }
  130. static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
  131. struct wpabuf *probe_resp_ie)
  132. {
  133. struct hostapd_data *hapd = ctx;
  134. wpabuf_free(hapd->wps_beacon_ie);
  135. hapd->wps_beacon_ie = beacon_ie;
  136. wpabuf_free(hapd->wps_probe_resp_ie);
  137. hapd->wps_probe_resp_ie = probe_resp_ie;
  138. if (hapd->beacon_set_done)
  139. ieee802_11_set_beacon(hapd);
  140. return hostapd_set_ap_wps_ie(hapd);
  141. }
  142. static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  143. const struct wps_device_data *dev)
  144. {
  145. struct hostapd_data *hapd = ctx;
  146. char uuid[40], txt[400];
  147. int len;
  148. char devtype[WPS_DEV_TYPE_BUFSIZE];
  149. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  150. return;
  151. wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
  152. len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
  153. "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
  154. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  155. dev->manufacturer, dev->model_name,
  156. dev->model_number, dev->serial_number,
  157. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  158. sizeof(devtype)));
  159. if (!os_snprintf_error(sizeof(txt), len))
  160. wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
  161. if (hapd->conf->wps_pin_requests) {
  162. FILE *f;
  163. struct os_time t;
  164. f = fopen(hapd->conf->wps_pin_requests, "a");
  165. if (f == NULL)
  166. return;
  167. os_get_time(&t);
  168. fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
  169. "\t%s\n",
  170. t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
  171. dev->manufacturer, dev->model_name, dev->model_number,
  172. dev->serial_number,
  173. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  174. sizeof(devtype)));
  175. fclose(f);
  176. }
  177. }
  178. struct wps_stop_reg_data {
  179. struct hostapd_data *current_hapd;
  180. const u8 *uuid_e;
  181. const u8 *dev_pw;
  182. size_t dev_pw_len;
  183. };
  184. static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
  185. {
  186. struct wps_stop_reg_data *data = ctx;
  187. if (hapd != data->current_hapd && hapd->wps != NULL)
  188. wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
  189. data->dev_pw, data->dev_pw_len);
  190. return 0;
  191. }
  192. static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  193. const u8 *uuid_e, const u8 *dev_pw,
  194. size_t dev_pw_len)
  195. {
  196. struct hostapd_data *hapd = ctx;
  197. char uuid[40];
  198. struct wps_stop_reg_data data;
  199. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  200. return;
  201. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
  202. MAC2STR(mac_addr), uuid);
  203. if (hapd->wps_reg_success_cb)
  204. hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
  205. mac_addr, uuid_e);
  206. data.current_hapd = hapd;
  207. data.uuid_e = uuid_e;
  208. data.dev_pw = dev_pw;
  209. data.dev_pw_len = dev_pw_len;
  210. hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
  211. }
  212. static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
  213. const u8 *uuid_e,
  214. const u8 *pri_dev_type,
  215. u16 config_methods,
  216. u16 dev_password_id, u8 request_type,
  217. const char *dev_name)
  218. {
  219. struct hostapd_data *hapd = ctx;
  220. char uuid[40];
  221. char devtype[WPS_DEV_TYPE_BUFSIZE];
  222. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  223. return;
  224. if (dev_name == NULL)
  225. dev_name = "";
  226. wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
  227. " %s %s 0x%x %u %u [%s]",
  228. MAC2STR(addr), uuid,
  229. wps_dev_type_bin2str(pri_dev_type, devtype,
  230. sizeof(devtype)),
  231. config_methods, dev_password_id, request_type, dev_name);
  232. }
  233. static int str_starts(const char *str, const char *start)
  234. {
  235. return os_strncmp(str, start, os_strlen(start)) == 0;
  236. }
  237. static void wps_reload_config(void *eloop_data, void *user_ctx)
  238. {
  239. struct hostapd_iface *iface = eloop_data;
  240. wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
  241. if (iface->interfaces == NULL ||
  242. iface->interfaces->reload_config(iface) < 0) {
  243. wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
  244. "configuration");
  245. }
  246. }
  247. void hostapd_wps_eap_completed(struct hostapd_data *hapd)
  248. {
  249. /*
  250. * Reduce race condition of the station trying to reconnect immediately
  251. * after AP reconfiguration through WPS by rescheduling the reload
  252. * timeout to happen after EAP completion rather than the originally
  253. * scheduled 100 ms after new configuration became known.
  254. */
  255. if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
  256. 1)
  257. wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
  258. }
  259. static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
  260. size_t attr_len)
  261. {
  262. size_t blen = attr_len * 2 + 1;
  263. char *buf = os_malloc(blen);
  264. if (buf) {
  265. wpa_snprintf_hex(buf, blen, attr, attr_len);
  266. wpa_msg(hapd->msg_ctx, MSG_INFO,
  267. WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
  268. os_free(buf);
  269. }
  270. }
  271. static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
  272. const struct wps_credential *cred)
  273. {
  274. struct hostapd_bss_config *bss = hapd->conf;
  275. wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
  276. bss->wps_state = 2;
  277. if (cred->ssid_len <= SSID_MAX_LEN) {
  278. os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
  279. bss->ssid.ssid_len = cred->ssid_len;
  280. bss->ssid.ssid_set = 1;
  281. }
  282. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  283. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  284. bss->wpa = 3;
  285. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  286. bss->wpa = 2;
  287. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  288. bss->wpa = 1;
  289. else
  290. bss->wpa = 0;
  291. if (bss->wpa) {
  292. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
  293. bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  294. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  295. bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  296. bss->wpa_pairwise = 0;
  297. if (cred->encr_type & WPS_ENCR_AES) {
  298. if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
  299. bss->wpa_pairwise |= WPA_CIPHER_GCMP;
  300. else
  301. bss->wpa_pairwise |= WPA_CIPHER_CCMP;
  302. }
  303. if (cred->encr_type & WPS_ENCR_TKIP)
  304. bss->wpa_pairwise |= WPA_CIPHER_TKIP;
  305. bss->rsn_pairwise = bss->wpa_pairwise;
  306. bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
  307. bss->wpa_pairwise,
  308. bss->rsn_pairwise);
  309. if (cred->key_len >= 8 && cred->key_len < 64) {
  310. os_free(bss->ssid.wpa_passphrase);
  311. bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
  312. if (bss->ssid.wpa_passphrase)
  313. os_memcpy(bss->ssid.wpa_passphrase, cred->key,
  314. cred->key_len);
  315. hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
  316. } else if (cred->key_len == 64) {
  317. hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
  318. bss->ssid.wpa_psk =
  319. os_zalloc(sizeof(struct hostapd_wpa_psk));
  320. if (bss->ssid.wpa_psk &&
  321. hexstr2bin((const char *) cred->key,
  322. bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
  323. bss->ssid.wpa_psk->group = 1;
  324. os_free(bss->ssid.wpa_passphrase);
  325. bss->ssid.wpa_passphrase = NULL;
  326. }
  327. }
  328. bss->auth_algs = 1;
  329. } else {
  330. /*
  331. * WPS 2.0 does not allow WEP to be configured, so no need to
  332. * process that option here either.
  333. */
  334. bss->auth_algs = 1;
  335. }
  336. /* Schedule configuration reload after short period of time to allow
  337. * EAP-WSC to be finished.
  338. */
  339. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  340. NULL);
  341. return 0;
  342. }
  343. static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
  344. {
  345. const struct wps_credential *cred = ctx;
  346. FILE *oconf, *nconf;
  347. size_t len, i;
  348. char *tmp_fname;
  349. char buf[1024];
  350. int multi_bss;
  351. int wpa;
  352. if (hapd->wps == NULL)
  353. return 0;
  354. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  355. cred->cred_attr, cred->cred_attr_len);
  356. wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
  357. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  358. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  359. cred->auth_type);
  360. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  361. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  362. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  363. cred->key, cred->key_len);
  364. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  365. MAC2STR(cred->mac_addr));
  366. if ((hapd->conf->wps_cred_processing == 1 ||
  367. hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
  368. hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
  369. } else if (hapd->conf->wps_cred_processing == 1 ||
  370. hapd->conf->wps_cred_processing == 2) {
  371. struct wpabuf *attr;
  372. attr = wpabuf_alloc(200);
  373. if (attr && wps_build_credential_wrap(attr, cred) == 0)
  374. hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
  375. wpabuf_len(attr));
  376. wpabuf_free(attr);
  377. } else
  378. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
  379. if (hapd->conf->wps_cred_processing == 1)
  380. return 0;
  381. os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
  382. hapd->wps->ssid_len = cred->ssid_len;
  383. hapd->wps->encr_types = cred->encr_type;
  384. hapd->wps->auth_types = cred->auth_type;
  385. hapd->wps->ap_encr_type = cred->encr_type;
  386. hapd->wps->ap_auth_type = cred->auth_type;
  387. if (cred->key_len == 0) {
  388. os_free(hapd->wps->network_key);
  389. hapd->wps->network_key = NULL;
  390. hapd->wps->network_key_len = 0;
  391. } else if ((cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) &&
  392. (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN)) {
  393. wpa_printf(MSG_INFO, "WPS: Invalid key length %lu for WPA/WPA2",
  394. (unsigned long) cred->key_len);
  395. return -1;
  396. } else {
  397. if (hapd->wps->network_key == NULL ||
  398. hapd->wps->network_key_len < cred->key_len) {
  399. hapd->wps->network_key_len = 0;
  400. os_free(hapd->wps->network_key);
  401. hapd->wps->network_key = os_malloc(cred->key_len);
  402. if (hapd->wps->network_key == NULL)
  403. return -1;
  404. }
  405. hapd->wps->network_key_len = cred->key_len;
  406. os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
  407. }
  408. hapd->wps->wps_state = WPS_STATE_CONFIGURED;
  409. if (hapd->iface->config_fname == NULL)
  410. return hapd_wps_reconfig_in_memory(hapd, cred);
  411. len = os_strlen(hapd->iface->config_fname) + 5;
  412. tmp_fname = os_malloc(len);
  413. if (tmp_fname == NULL)
  414. return -1;
  415. os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
  416. oconf = fopen(hapd->iface->config_fname, "r");
  417. if (oconf == NULL) {
  418. wpa_printf(MSG_WARNING, "WPS: Could not open current "
  419. "configuration file");
  420. os_free(tmp_fname);
  421. return -1;
  422. }
  423. nconf = fopen(tmp_fname, "w");
  424. if (nconf == NULL) {
  425. wpa_printf(MSG_WARNING, "WPS: Could not write updated "
  426. "configuration file");
  427. os_free(tmp_fname);
  428. fclose(oconf);
  429. return -1;
  430. }
  431. fprintf(nconf, "# WPS configuration - START\n");
  432. fprintf(nconf, "wps_state=2\n");
  433. if (is_hex(cred->ssid, cred->ssid_len)) {
  434. fprintf(nconf, "ssid2=");
  435. for (i = 0; i < cred->ssid_len; i++)
  436. fprintf(nconf, "%02x", cred->ssid[i]);
  437. fprintf(nconf, "\n");
  438. } else {
  439. fprintf(nconf, "ssid=");
  440. for (i = 0; i < cred->ssid_len; i++)
  441. fputc(cred->ssid[i], nconf);
  442. fprintf(nconf, "\n");
  443. }
  444. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  445. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  446. wpa = 3;
  447. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  448. wpa = 2;
  449. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  450. wpa = 1;
  451. else
  452. wpa = 0;
  453. if (wpa) {
  454. char *prefix;
  455. fprintf(nconf, "wpa=%d\n", wpa);
  456. fprintf(nconf, "wpa_key_mgmt=");
  457. prefix = "";
  458. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
  459. fprintf(nconf, "WPA-EAP");
  460. prefix = " ";
  461. }
  462. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  463. fprintf(nconf, "%sWPA-PSK", prefix);
  464. fprintf(nconf, "\n");
  465. fprintf(nconf, "wpa_pairwise=");
  466. prefix = "";
  467. if (cred->encr_type & WPS_ENCR_AES) {
  468. if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
  469. fprintf(nconf, "GCMP");
  470. else
  471. fprintf(nconf, "CCMP");
  472. prefix = " ";
  473. }
  474. if (cred->encr_type & WPS_ENCR_TKIP) {
  475. fprintf(nconf, "%sTKIP", prefix);
  476. }
  477. fprintf(nconf, "\n");
  478. if (cred->key_len >= 8 && cred->key_len < 64) {
  479. fprintf(nconf, "wpa_passphrase=");
  480. for (i = 0; i < cred->key_len; i++)
  481. fputc(cred->key[i], nconf);
  482. fprintf(nconf, "\n");
  483. } else if (cred->key_len == 64) {
  484. fprintf(nconf, "wpa_psk=");
  485. for (i = 0; i < cred->key_len; i++)
  486. fputc(cred->key[i], nconf);
  487. fprintf(nconf, "\n");
  488. } else {
  489. wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
  490. "for WPA/WPA2",
  491. (unsigned long) cred->key_len);
  492. }
  493. fprintf(nconf, "auth_algs=1\n");
  494. } else {
  495. /*
  496. * WPS 2.0 does not allow WEP to be configured, so no need to
  497. * process that option here either.
  498. */
  499. fprintf(nconf, "auth_algs=1\n");
  500. }
  501. fprintf(nconf, "# WPS configuration - END\n");
  502. multi_bss = 0;
  503. while (fgets(buf, sizeof(buf), oconf)) {
  504. if (os_strncmp(buf, "bss=", 4) == 0)
  505. multi_bss = 1;
  506. if (!multi_bss &&
  507. (str_starts(buf, "ssid=") ||
  508. str_starts(buf, "ssid2=") ||
  509. str_starts(buf, "auth_algs=") ||
  510. str_starts(buf, "wep_default_key=") ||
  511. str_starts(buf, "wep_key") ||
  512. str_starts(buf, "wps_state=") ||
  513. str_starts(buf, "wpa=") ||
  514. str_starts(buf, "wpa_psk=") ||
  515. str_starts(buf, "wpa_pairwise=") ||
  516. str_starts(buf, "rsn_pairwise=") ||
  517. str_starts(buf, "wpa_key_mgmt=") ||
  518. str_starts(buf, "wpa_passphrase="))) {
  519. fprintf(nconf, "#WPS# %s", buf);
  520. } else
  521. fprintf(nconf, "%s", buf);
  522. }
  523. fclose(nconf);
  524. fclose(oconf);
  525. if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
  526. wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
  527. "configuration file: %s", strerror(errno));
  528. os_free(tmp_fname);
  529. return -1;
  530. }
  531. os_free(tmp_fname);
  532. /* Schedule configuration reload after short period of time to allow
  533. * EAP-WSC to be finished.
  534. */
  535. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  536. NULL);
  537. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  538. return 0;
  539. }
  540. static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
  541. {
  542. struct hostapd_data *hapd = ctx;
  543. return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
  544. }
  545. static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
  546. {
  547. struct hostapd_data *hapd = eloop_data;
  548. if (hapd->conf->ap_setup_locked)
  549. return;
  550. if (hapd->ap_pin_failures_consecutive >= 10)
  551. return;
  552. wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
  553. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
  554. hapd->wps->ap_setup_locked = 0;
  555. wps_registrar_update_ie(hapd->wps->registrar);
  556. }
  557. static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
  558. {
  559. struct wps_event_pwd_auth_fail *data = ctx;
  560. if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
  561. return 0;
  562. /*
  563. * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
  564. * for some time if this happens multiple times to slow down brute
  565. * force attacks.
  566. */
  567. hapd->ap_pin_failures++;
  568. hapd->ap_pin_failures_consecutive++;
  569. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
  570. "(%u consecutive)",
  571. hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
  572. if (hapd->ap_pin_failures < 3)
  573. return 0;
  574. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
  575. hapd->wps->ap_setup_locked = 1;
  576. wps_registrar_update_ie(hapd->wps->registrar);
  577. if (!hapd->conf->ap_setup_locked &&
  578. hapd->ap_pin_failures_consecutive >= 10) {
  579. /*
  580. * In indefinite lockdown - disable automatic AP PIN
  581. * reenablement.
  582. */
  583. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  584. wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
  585. } else if (!hapd->conf->ap_setup_locked) {
  586. if (hapd->ap_pin_lockout_time == 0)
  587. hapd->ap_pin_lockout_time = 60;
  588. else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
  589. (hapd->ap_pin_failures % 3) == 0)
  590. hapd->ap_pin_lockout_time *= 2;
  591. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
  592. hapd->ap_pin_lockout_time);
  593. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  594. eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
  595. hostapd_wps_reenable_ap_pin, hapd,
  596. NULL);
  597. }
  598. return 0;
  599. }
  600. static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
  601. struct wps_event_pwd_auth_fail *data)
  602. {
  603. /* Update WPS Status - Authentication Failure */
  604. wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
  605. hapd->wps_stats.status = WPS_STATUS_FAILURE;
  606. hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
  607. os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
  608. hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
  609. }
  610. static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
  611. {
  612. if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
  613. return 0;
  614. if (hapd->ap_pin_failures_consecutive == 0)
  615. return 0;
  616. wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
  617. "- total validation failures %u (%u consecutive)",
  618. hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
  619. hapd->ap_pin_failures_consecutive = 0;
  620. return 0;
  621. }
  622. static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
  623. {
  624. hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
  625. }
  626. static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
  627. {
  628. /* Update WPS Status - PBC Overlap */
  629. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
  630. }
  631. static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
  632. {
  633. /* Update WPS PBC Status:PBC Timeout */
  634. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
  635. }
  636. static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
  637. {
  638. /* Update WPS PBC status - Active */
  639. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
  640. }
  641. static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
  642. {
  643. /* Update WPS PBC status - Active */
  644. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
  645. }
  646. static void hostapd_wps_event_success(struct hostapd_data *hapd,
  647. struct wps_event_success *success)
  648. {
  649. /* Update WPS status - Success */
  650. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
  651. hapd->wps_stats.status = WPS_STATUS_SUCCESS;
  652. os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
  653. }
  654. static void hostapd_wps_event_fail(struct hostapd_data *hapd,
  655. struct wps_event_fail *fail)
  656. {
  657. /* Update WPS status - Failure */
  658. hapd->wps_stats.status = WPS_STATUS_FAILURE;
  659. os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
  660. hapd->wps_stats.failure_reason = fail->error_indication;
  661. if (fail->error_indication > 0 &&
  662. fail->error_indication < NUM_WPS_EI_VALUES) {
  663. wpa_msg(hapd->msg_ctx, MSG_INFO,
  664. WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
  665. fail->msg, fail->config_error, fail->error_indication,
  666. wps_ei_str(fail->error_indication));
  667. } else {
  668. wpa_msg(hapd->msg_ctx, MSG_INFO,
  669. WPS_EVENT_FAIL "msg=%d config_error=%d",
  670. fail->msg, fail->config_error);
  671. }
  672. }
  673. static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
  674. union wps_event_data *data)
  675. {
  676. struct hostapd_data *hapd = ctx;
  677. switch (event) {
  678. case WPS_EV_M2D:
  679. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
  680. break;
  681. case WPS_EV_FAIL:
  682. hostapd_wps_event_fail(hapd, &data->fail);
  683. break;
  684. case WPS_EV_SUCCESS:
  685. hostapd_wps_event_success(hapd, &data->success);
  686. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
  687. break;
  688. case WPS_EV_PWD_AUTH_FAIL:
  689. hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
  690. break;
  691. case WPS_EV_PBC_OVERLAP:
  692. hostapd_wps_event_pbc_overlap(hapd);
  693. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
  694. break;
  695. case WPS_EV_PBC_TIMEOUT:
  696. hostapd_wps_event_pbc_timeout(hapd);
  697. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
  698. break;
  699. case WPS_EV_PBC_ACTIVE:
  700. hostapd_wps_event_pbc_active(hapd);
  701. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
  702. break;
  703. case WPS_EV_PBC_DISABLE:
  704. hostapd_wps_event_pbc_disable(hapd);
  705. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
  706. break;
  707. case WPS_EV_ER_AP_ADD:
  708. break;
  709. case WPS_EV_ER_AP_REMOVE:
  710. break;
  711. case WPS_EV_ER_ENROLLEE_ADD:
  712. break;
  713. case WPS_EV_ER_ENROLLEE_REMOVE:
  714. break;
  715. case WPS_EV_ER_AP_SETTINGS:
  716. break;
  717. case WPS_EV_ER_SET_SELECTED_REGISTRAR:
  718. break;
  719. case WPS_EV_AP_PIN_SUCCESS:
  720. hostapd_wps_ap_pin_success(hapd);
  721. break;
  722. }
  723. if (hapd->wps_event_cb)
  724. hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
  725. }
  726. static int hostapd_wps_rf_band_cb(void *ctx)
  727. {
  728. struct hostapd_data *hapd = ctx;
  729. return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  730. WPS_RF_50GHZ :
  731. hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
  732. WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  733. }
  734. static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only)
  735. {
  736. wpabuf_free(hapd->wps_beacon_ie);
  737. hapd->wps_beacon_ie = NULL;
  738. wpabuf_free(hapd->wps_probe_resp_ie);
  739. hapd->wps_probe_resp_ie = NULL;
  740. if (deinit_only) {
  741. hostapd_reset_ap_wps_ie(hapd);
  742. return;
  743. }
  744. hostapd_set_ap_wps_ie(hapd);
  745. }
  746. static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
  747. {
  748. const u8 **uuid = ctx;
  749. size_t j;
  750. if (iface == NULL)
  751. return 0;
  752. for (j = 0; j < iface->num_bss; j++) {
  753. struct hostapd_data *hapd = iface->bss[j];
  754. if (hapd->wps && !hapd->conf->wps_independent &&
  755. !is_nil_uuid(hapd->wps->uuid)) {
  756. *uuid = hapd->wps->uuid;
  757. return 1;
  758. }
  759. }
  760. return 0;
  761. }
  762. static const u8 * get_own_uuid(struct hostapd_iface *iface)
  763. {
  764. const u8 *uuid;
  765. if (iface->interfaces == NULL ||
  766. iface->interfaces->for_each_interface == NULL)
  767. return NULL;
  768. uuid = NULL;
  769. iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
  770. &uuid);
  771. return uuid;
  772. }
  773. static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
  774. {
  775. int *count= ctx;
  776. (*count)++;
  777. return 0;
  778. }
  779. static int interface_count(struct hostapd_iface *iface)
  780. {
  781. int count = 0;
  782. if (iface->interfaces == NULL ||
  783. iface->interfaces->for_each_interface == NULL)
  784. return 0;
  785. iface->interfaces->for_each_interface(iface->interfaces,
  786. count_interface_cb, &count);
  787. return count;
  788. }
  789. static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
  790. struct wps_context *wps)
  791. {
  792. int i;
  793. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  794. wpabuf_free(wps->dev.vendor_ext[i]);
  795. wps->dev.vendor_ext[i] = NULL;
  796. if (hapd->conf->wps_vendor_ext[i] == NULL)
  797. continue;
  798. wps->dev.vendor_ext[i] =
  799. wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
  800. if (wps->dev.vendor_ext[i] == NULL) {
  801. while (--i >= 0)
  802. wpabuf_free(wps->dev.vendor_ext[i]);
  803. return -1;
  804. }
  805. }
  806. return 0;
  807. }
  808. static void hostapd_free_wps(struct wps_context *wps)
  809. {
  810. int i;
  811. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
  812. wpabuf_free(wps->dev.vendor_ext[i]);
  813. wps_device_data_free(&wps->dev);
  814. os_free(wps->network_key);
  815. hostapd_wps_nfc_clear(wps);
  816. wpabuf_free(wps->dh_pubkey);
  817. wpabuf_free(wps->dh_privkey);
  818. os_free(wps);
  819. }
  820. int hostapd_init_wps(struct hostapd_data *hapd,
  821. struct hostapd_bss_config *conf)
  822. {
  823. struct wps_context *wps;
  824. struct wps_registrar_config cfg;
  825. if (conf->wps_state == 0) {
  826. hostapd_wps_clear_ies(hapd, 0);
  827. return 0;
  828. }
  829. wps = os_zalloc(sizeof(*wps));
  830. if (wps == NULL)
  831. return -1;
  832. wps->cred_cb = hostapd_wps_cred_cb;
  833. wps->event_cb = hostapd_wps_event_cb;
  834. wps->rf_band_cb = hostapd_wps_rf_band_cb;
  835. wps->cb_ctx = hapd;
  836. os_memset(&cfg, 0, sizeof(cfg));
  837. wps->wps_state = hapd->conf->wps_state;
  838. wps->ap_setup_locked = hapd->conf->ap_setup_locked;
  839. if (is_nil_uuid(hapd->conf->uuid)) {
  840. const u8 *uuid;
  841. uuid = get_own_uuid(hapd->iface);
  842. if (uuid && !conf->wps_independent) {
  843. os_memcpy(wps->uuid, uuid, UUID_LEN);
  844. wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
  845. "interface", wps->uuid, UUID_LEN);
  846. } else {
  847. uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
  848. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  849. "address", wps->uuid, UUID_LEN);
  850. }
  851. } else {
  852. os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
  853. wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
  854. wps->uuid, UUID_LEN);
  855. }
  856. wps->ssid_len = hapd->conf->ssid.ssid_len;
  857. os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
  858. wps->ap = 1;
  859. os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
  860. wps->dev.device_name = hapd->conf->device_name ?
  861. os_strdup(hapd->conf->device_name) : NULL;
  862. wps->dev.manufacturer = hapd->conf->manufacturer ?
  863. os_strdup(hapd->conf->manufacturer) : NULL;
  864. wps->dev.model_name = hapd->conf->model_name ?
  865. os_strdup(hapd->conf->model_name) : NULL;
  866. wps->dev.model_number = hapd->conf->model_number ?
  867. os_strdup(hapd->conf->model_number) : NULL;
  868. wps->dev.serial_number = hapd->conf->serial_number ?
  869. os_strdup(hapd->conf->serial_number) : NULL;
  870. wps->config_methods =
  871. wps_config_methods_str2bin(hapd->conf->config_methods);
  872. if ((wps->config_methods &
  873. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  874. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  875. wpa_printf(MSG_INFO, "WPS: Converting display to "
  876. "virtual_display for WPS 2.0 compliance");
  877. wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  878. }
  879. if ((wps->config_methods &
  880. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  881. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  882. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  883. "virtual_push_button for WPS 2.0 compliance");
  884. wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  885. }
  886. os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
  887. WPS_DEV_TYPE_LEN);
  888. if (hostapd_wps_set_vendor_ext(hapd, wps) < 0)
  889. goto fail;
  890. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  891. if (conf->wps_rf_bands) {
  892. wps->dev.rf_bands = conf->wps_rf_bands;
  893. } else {
  894. wps->dev.rf_bands =
  895. hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  896. WPS_RF_50GHZ :
  897. hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
  898. WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  899. }
  900. if (conf->wpa & WPA_PROTO_RSN) {
  901. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  902. wps->auth_types |= WPS_AUTH_WPA2PSK;
  903. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  904. wps->auth_types |= WPS_AUTH_WPA2;
  905. if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP))
  906. wps->encr_types |= WPS_ENCR_AES;
  907. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  908. wps->encr_types |= WPS_ENCR_TKIP;
  909. }
  910. if (conf->wpa & WPA_PROTO_WPA) {
  911. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  912. wps->auth_types |= WPS_AUTH_WPAPSK;
  913. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  914. wps->auth_types |= WPS_AUTH_WPA;
  915. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  916. wps->encr_types |= WPS_ENCR_AES;
  917. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  918. wps->encr_types |= WPS_ENCR_TKIP;
  919. }
  920. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  921. wps->encr_types |= WPS_ENCR_NONE;
  922. wps->auth_types |= WPS_AUTH_OPEN;
  923. }
  924. if (conf->ssid.wpa_psk_file) {
  925. /* Use per-device PSKs */
  926. } else if (conf->ssid.wpa_passphrase) {
  927. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  928. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  929. } else if (conf->ssid.wpa_psk) {
  930. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  931. if (wps->network_key == NULL)
  932. goto fail;
  933. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  934. conf->ssid.wpa_psk->psk, PMK_LEN);
  935. wps->network_key_len = 2 * PMK_LEN;
  936. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  937. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  938. if (wps->network_key == NULL)
  939. goto fail;
  940. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  941. conf->ssid.wep.len[0]);
  942. wps->network_key_len = conf->ssid.wep.len[0];
  943. }
  944. if (conf->ssid.wpa_psk) {
  945. os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
  946. wps->psk_set = 1;
  947. }
  948. wps->ap_auth_type = wps->auth_types;
  949. wps->ap_encr_type = wps->encr_types;
  950. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  951. /* Override parameters to enable security by default */
  952. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  953. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  954. }
  955. wps->ap_settings = conf->ap_settings;
  956. wps->ap_settings_len = conf->ap_settings_len;
  957. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  958. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  959. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  960. cfg.reg_success_cb = hostapd_wps_reg_success_cb;
  961. cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
  962. cfg.cb_ctx = hapd;
  963. cfg.skip_cred_build = conf->skip_cred_build;
  964. cfg.extra_cred = conf->extra_cred;
  965. cfg.extra_cred_len = conf->extra_cred_len;
  966. cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
  967. conf->skip_cred_build;
  968. if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
  969. cfg.static_wep_only = 1;
  970. cfg.dualband = interface_count(hapd->iface) > 1;
  971. if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
  972. (WPS_RF_50GHZ | WPS_RF_24GHZ))
  973. cfg.dualband = 1;
  974. if (cfg.dualband)
  975. wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
  976. cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
  977. wps->registrar = wps_registrar_init(wps, &cfg);
  978. if (wps->registrar == NULL) {
  979. wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
  980. goto fail;
  981. }
  982. #ifdef CONFIG_WPS_UPNP
  983. wps->friendly_name = hapd->conf->friendly_name;
  984. wps->manufacturer_url = hapd->conf->manufacturer_url;
  985. wps->model_description = hapd->conf->model_description;
  986. wps->model_url = hapd->conf->model_url;
  987. wps->upc = hapd->conf->upc;
  988. #endif /* CONFIG_WPS_UPNP */
  989. hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
  990. hapd->wps = wps;
  991. return 0;
  992. fail:
  993. hostapd_free_wps(wps);
  994. return -1;
  995. }
  996. int hostapd_init_wps_complete(struct hostapd_data *hapd)
  997. {
  998. struct wps_context *wps = hapd->wps;
  999. if (wps == NULL)
  1000. return 0;
  1001. #ifdef CONFIG_WPS_UPNP
  1002. if (hostapd_wps_upnp_init(hapd, wps) < 0) {
  1003. wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
  1004. wps_registrar_deinit(wps->registrar);
  1005. hostapd_free_wps(wps);
  1006. hapd->wps = NULL;
  1007. return -1;
  1008. }
  1009. #endif /* CONFIG_WPS_UPNP */
  1010. return 0;
  1011. }
  1012. static void hostapd_wps_nfc_clear(struct wps_context *wps)
  1013. {
  1014. #ifdef CONFIG_WPS_NFC
  1015. wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
  1016. wps->ap_nfc_dev_pw_id = 0;
  1017. wpabuf_free(wps->ap_nfc_dh_pubkey);
  1018. wps->ap_nfc_dh_pubkey = NULL;
  1019. wpabuf_free(wps->ap_nfc_dh_privkey);
  1020. wps->ap_nfc_dh_privkey = NULL;
  1021. wpabuf_free(wps->ap_nfc_dev_pw);
  1022. wps->ap_nfc_dev_pw = NULL;
  1023. #endif /* CONFIG_WPS_NFC */
  1024. }
  1025. void hostapd_deinit_wps(struct hostapd_data *hapd)
  1026. {
  1027. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  1028. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  1029. eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
  1030. if (hapd->wps == NULL) {
  1031. hostapd_wps_clear_ies(hapd, 1);
  1032. return;
  1033. }
  1034. #ifdef CONFIG_WPS_UPNP
  1035. hostapd_wps_upnp_deinit(hapd);
  1036. #endif /* CONFIG_WPS_UPNP */
  1037. wps_registrar_deinit(hapd->wps->registrar);
  1038. wps_free_pending_msgs(hapd->wps->upnp_msgs);
  1039. hostapd_free_wps(hapd->wps);
  1040. hapd->wps = NULL;
  1041. hostapd_wps_clear_ies(hapd, 1);
  1042. }
  1043. void hostapd_update_wps(struct hostapd_data *hapd)
  1044. {
  1045. if (hapd->wps == NULL)
  1046. return;
  1047. #ifdef CONFIG_WPS_UPNP
  1048. hapd->wps->friendly_name = hapd->conf->friendly_name;
  1049. hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
  1050. hapd->wps->model_description = hapd->conf->model_description;
  1051. hapd->wps->model_url = hapd->conf->model_url;
  1052. hapd->wps->upc = hapd->conf->upc;
  1053. #endif /* CONFIG_WPS_UPNP */
  1054. hostapd_wps_set_vendor_ext(hapd, hapd->wps);
  1055. if (hapd->conf->wps_state)
  1056. wps_registrar_update_ie(hapd->wps->registrar);
  1057. else
  1058. hostapd_deinit_wps(hapd);
  1059. }
  1060. struct wps_add_pin_data {
  1061. const u8 *addr;
  1062. const u8 *uuid;
  1063. const u8 *pin;
  1064. size_t pin_len;
  1065. int timeout;
  1066. int added;
  1067. };
  1068. static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
  1069. {
  1070. struct wps_add_pin_data *data = ctx;
  1071. int ret;
  1072. if (hapd->wps == NULL)
  1073. return 0;
  1074. ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
  1075. data->uuid, data->pin, data->pin_len,
  1076. data->timeout);
  1077. if (ret == 0)
  1078. data->added++;
  1079. return ret;
  1080. }
  1081. int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
  1082. const char *uuid, const char *pin, int timeout)
  1083. {
  1084. u8 u[UUID_LEN];
  1085. struct wps_add_pin_data data;
  1086. data.addr = addr;
  1087. data.uuid = u;
  1088. data.pin = (const u8 *) pin;
  1089. data.pin_len = os_strlen(pin);
  1090. data.timeout = timeout;
  1091. data.added = 0;
  1092. if (os_strcmp(uuid, "any") == 0)
  1093. data.uuid = NULL;
  1094. else {
  1095. if (uuid_str2bin(uuid, u))
  1096. return -1;
  1097. data.uuid = u;
  1098. }
  1099. if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
  1100. return -1;
  1101. return data.added ? 0 : -1;
  1102. }
  1103. struct wps_button_pushed_ctx {
  1104. const u8 *p2p_dev_addr;
  1105. unsigned int count;
  1106. };
  1107. static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
  1108. {
  1109. struct wps_button_pushed_ctx *data = ctx;
  1110. if (hapd->wps) {
  1111. data->count++;
  1112. return wps_registrar_button_pushed(hapd->wps->registrar,
  1113. data->p2p_dev_addr);
  1114. }
  1115. return 0;
  1116. }
  1117. int hostapd_wps_button_pushed(struct hostapd_data *hapd,
  1118. const u8 *p2p_dev_addr)
  1119. {
  1120. struct wps_button_pushed_ctx ctx;
  1121. int ret;
  1122. os_memset(&ctx, 0, sizeof(ctx));
  1123. ctx.p2p_dev_addr = p2p_dev_addr;
  1124. ret = hostapd_wps_for_each(hapd, wps_button_pushed, &ctx);
  1125. if (ret == 0 && !ctx.count)
  1126. ret = -1;
  1127. return ret;
  1128. }
  1129. struct wps_cancel_ctx {
  1130. unsigned int count;
  1131. };
  1132. static int wps_cancel(struct hostapd_data *hapd, void *ctx)
  1133. {
  1134. struct wps_cancel_ctx *data = ctx;
  1135. if (hapd->wps) {
  1136. data->count++;
  1137. wps_registrar_wps_cancel(hapd->wps->registrar);
  1138. ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
  1139. }
  1140. return 0;
  1141. }
  1142. int hostapd_wps_cancel(struct hostapd_data *hapd)
  1143. {
  1144. struct wps_cancel_ctx ctx;
  1145. int ret;
  1146. os_memset(&ctx, 0, sizeof(ctx));
  1147. ret = hostapd_wps_for_each(hapd, wps_cancel, &ctx);
  1148. if (ret == 0 && !ctx.count)
  1149. ret = -1;
  1150. return ret;
  1151. }
  1152. static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
  1153. const u8 *bssid,
  1154. const u8 *ie, size_t ie_len,
  1155. int ssi_signal)
  1156. {
  1157. struct hostapd_data *hapd = ctx;
  1158. struct wpabuf *wps_ie;
  1159. struct ieee802_11_elems elems;
  1160. if (hapd->wps == NULL)
  1161. return 0;
  1162. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  1163. wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
  1164. MACSTR, MAC2STR(addr));
  1165. return 0;
  1166. }
  1167. if (elems.ssid && elems.ssid_len > 0 &&
  1168. (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  1169. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
  1170. 0))
  1171. return 0; /* Not for us */
  1172. wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
  1173. if (wps_ie == NULL)
  1174. return 0;
  1175. if (wps_validate_probe_req(wps_ie, addr) < 0) {
  1176. wpabuf_free(wps_ie);
  1177. return 0;
  1178. }
  1179. if (wpabuf_len(wps_ie) > 0) {
  1180. int p2p_wildcard = 0;
  1181. #ifdef CONFIG_P2P
  1182. if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
  1183. os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
  1184. P2P_WILDCARD_SSID_LEN) == 0)
  1185. p2p_wildcard = 1;
  1186. #endif /* CONFIG_P2P */
  1187. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
  1188. p2p_wildcard);
  1189. #ifdef CONFIG_WPS_UPNP
  1190. /* FIX: what exactly should be included in the WLANEvent?
  1191. * WPS attributes? Full ProbeReq frame? */
  1192. if (!p2p_wildcard)
  1193. upnp_wps_device_send_wlan_event(
  1194. hapd->wps_upnp, addr,
  1195. UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
  1196. #endif /* CONFIG_WPS_UPNP */
  1197. }
  1198. wpabuf_free(wps_ie);
  1199. return 0;
  1200. }
  1201. #ifdef CONFIG_WPS_UPNP
  1202. static int hostapd_rx_req_put_wlan_response(
  1203. void *priv, enum upnp_wps_wlanevent_type ev_type,
  1204. const u8 *mac_addr, const struct wpabuf *msg,
  1205. enum wps_msg_type msg_type)
  1206. {
  1207. struct hostapd_data *hapd = priv;
  1208. struct sta_info *sta;
  1209. struct upnp_pending_message *p;
  1210. wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
  1211. MACSTR, ev_type, MAC2STR(mac_addr));
  1212. wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
  1213. wpabuf_head(msg), wpabuf_len(msg));
  1214. if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
  1215. wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
  1216. "PutWLANResponse WLANEventType %d", ev_type);
  1217. return -1;
  1218. }
  1219. /*
  1220. * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
  1221. * server implementation for delivery to the peer.
  1222. */
  1223. sta = ap_get_sta(hapd, mac_addr);
  1224. #ifndef CONFIG_WPS_STRICT
  1225. if (!sta) {
  1226. /*
  1227. * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
  1228. * Pick STA that is in an ongoing WPS registration without
  1229. * checking the MAC address.
  1230. */
  1231. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
  1232. "on NewWLANEventMAC; try wildcard match");
  1233. for (sta = hapd->sta_list; sta; sta = sta->next) {
  1234. if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
  1235. break;
  1236. }
  1237. }
  1238. #endif /* CONFIG_WPS_STRICT */
  1239. if (!sta || !(sta->flags & WLAN_STA_WPS)) {
  1240. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
  1241. return 0;
  1242. }
  1243. if (!sta->eapol_sm) {
  1244. /*
  1245. * This can happen, e.g., if an ER sends an extra message after
  1246. * the station has disassociated (but not fully
  1247. * deauthenticated).
  1248. */
  1249. wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
  1250. return 0;
  1251. }
  1252. p = os_zalloc(sizeof(*p));
  1253. if (p == NULL)
  1254. return -1;
  1255. os_memcpy(p->addr, sta->addr, ETH_ALEN);
  1256. p->msg = wpabuf_dup(msg);
  1257. p->type = msg_type;
  1258. p->next = hapd->wps->upnp_msgs;
  1259. hapd->wps->upnp_msgs = p;
  1260. return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
  1261. }
  1262. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  1263. struct wps_context *wps)
  1264. {
  1265. struct upnp_wps_device_ctx *ctx;
  1266. if (!hapd->conf->upnp_iface)
  1267. return 0;
  1268. ctx = os_zalloc(sizeof(*ctx));
  1269. if (ctx == NULL)
  1270. return -1;
  1271. ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
  1272. if (hapd->conf->ap_pin)
  1273. ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
  1274. hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
  1275. hapd->conf->upnp_iface);
  1276. if (hapd->wps_upnp == NULL)
  1277. return -1;
  1278. wps->wps_upnp = hapd->wps_upnp;
  1279. return 0;
  1280. }
  1281. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
  1282. {
  1283. upnp_wps_device_deinit(hapd->wps_upnp, hapd);
  1284. }
  1285. #endif /* CONFIG_WPS_UPNP */
  1286. int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
  1287. char *buf, size_t buflen)
  1288. {
  1289. if (hapd->wps == NULL)
  1290. return 0;
  1291. return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
  1292. }
  1293. static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  1294. {
  1295. struct hostapd_data *hapd = eloop_data;
  1296. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  1297. hostapd_wps_ap_pin_disable(hapd);
  1298. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
  1299. }
  1300. static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
  1301. {
  1302. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  1303. hapd->ap_pin_failures = 0;
  1304. hapd->ap_pin_failures_consecutive = 0;
  1305. hapd->conf->ap_setup_locked = 0;
  1306. if (hapd->wps->ap_setup_locked) {
  1307. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
  1308. hapd->wps->ap_setup_locked = 0;
  1309. wps_registrar_update_ie(hapd->wps->registrar);
  1310. }
  1311. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  1312. if (timeout > 0)
  1313. eloop_register_timeout(timeout, 0,
  1314. hostapd_wps_ap_pin_timeout, hapd, NULL);
  1315. }
  1316. static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
  1317. {
  1318. os_free(hapd->conf->ap_pin);
  1319. hapd->conf->ap_pin = NULL;
  1320. #ifdef CONFIG_WPS_UPNP
  1321. upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
  1322. #endif /* CONFIG_WPS_UPNP */
  1323. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  1324. return 0;
  1325. }
  1326. void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
  1327. {
  1328. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  1329. hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
  1330. }
  1331. struct wps_ap_pin_data {
  1332. char pin_txt[9];
  1333. int timeout;
  1334. };
  1335. static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
  1336. {
  1337. struct wps_ap_pin_data *data = ctx;
  1338. if (!hapd->wps)
  1339. return 0;
  1340. os_free(hapd->conf->ap_pin);
  1341. hapd->conf->ap_pin = os_strdup(data->pin_txt);
  1342. #ifdef CONFIG_WPS_UPNP
  1343. upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
  1344. #endif /* CONFIG_WPS_UPNP */
  1345. hostapd_wps_ap_pin_enable(hapd, data->timeout);
  1346. return 0;
  1347. }
  1348. const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
  1349. {
  1350. unsigned int pin;
  1351. struct wps_ap_pin_data data;
  1352. pin = wps_generate_pin();
  1353. os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
  1354. data.timeout = timeout;
  1355. hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
  1356. return hapd->conf->ap_pin;
  1357. }
  1358. const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
  1359. {
  1360. return hapd->conf->ap_pin;
  1361. }
  1362. int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
  1363. int timeout)
  1364. {
  1365. struct wps_ap_pin_data data;
  1366. int ret;
  1367. ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
  1368. if (os_snprintf_error(sizeof(data.pin_txt), ret))
  1369. return -1;
  1370. data.timeout = timeout;
  1371. return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
  1372. }
  1373. static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
  1374. {
  1375. if (hapd->wps)
  1376. wps_registrar_update_ie(hapd->wps->registrar);
  1377. return 0;
  1378. }
  1379. void hostapd_wps_update_ie(struct hostapd_data *hapd)
  1380. {
  1381. hostapd_wps_for_each(hapd, wps_update_ie, NULL);
  1382. }
  1383. int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
  1384. const char *auth, const char *encr, const char *key)
  1385. {
  1386. struct wps_credential cred;
  1387. size_t len;
  1388. os_memset(&cred, 0, sizeof(cred));
  1389. len = os_strlen(ssid);
  1390. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1391. hexstr2bin(ssid, cred.ssid, len / 2))
  1392. return -1;
  1393. cred.ssid_len = len / 2;
  1394. if (os_strncmp(auth, "OPEN", 4) == 0)
  1395. cred.auth_type = WPS_AUTH_OPEN;
  1396. else if (os_strncmp(auth, "WPAPSK", 6) == 0)
  1397. cred.auth_type = WPS_AUTH_WPAPSK;
  1398. else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
  1399. cred.auth_type = WPS_AUTH_WPA2PSK;
  1400. else
  1401. return -1;
  1402. if (encr) {
  1403. if (os_strncmp(encr, "NONE", 4) == 0)
  1404. cred.encr_type = WPS_ENCR_NONE;
  1405. else if (os_strncmp(encr, "TKIP", 4) == 0)
  1406. cred.encr_type = WPS_ENCR_TKIP;
  1407. else if (os_strncmp(encr, "CCMP", 4) == 0)
  1408. cred.encr_type = WPS_ENCR_AES;
  1409. else
  1410. return -1;
  1411. } else
  1412. cred.encr_type = WPS_ENCR_NONE;
  1413. if (key) {
  1414. len = os_strlen(key);
  1415. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1416. hexstr2bin(key, cred.key, len / 2))
  1417. return -1;
  1418. cred.key_len = len / 2;
  1419. }
  1420. return wps_registrar_config_ap(hapd->wps->registrar, &cred);
  1421. }
  1422. #ifdef CONFIG_WPS_NFC
  1423. struct wps_nfc_password_token_data {
  1424. const u8 *oob_dev_pw;
  1425. size_t oob_dev_pw_len;
  1426. int added;
  1427. };
  1428. static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
  1429. {
  1430. struct wps_nfc_password_token_data *data = ctx;
  1431. int ret;
  1432. if (hapd->wps == NULL)
  1433. return 0;
  1434. ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
  1435. data->oob_dev_pw,
  1436. data->oob_dev_pw_len);
  1437. if (ret == 0)
  1438. data->added++;
  1439. return ret;
  1440. }
  1441. static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
  1442. struct wps_parse_attr *attr)
  1443. {
  1444. struct wps_nfc_password_token_data data;
  1445. data.oob_dev_pw = attr->oob_dev_password;
  1446. data.oob_dev_pw_len = attr->oob_dev_password_len;
  1447. data.added = 0;
  1448. if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
  1449. return -1;
  1450. return data.added ? 0 : -1;
  1451. }
  1452. static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
  1453. const struct wpabuf *wps)
  1454. {
  1455. struct wps_parse_attr attr;
  1456. wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
  1457. if (wps_parse_msg(wps, &attr)) {
  1458. wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
  1459. return -1;
  1460. }
  1461. if (attr.oob_dev_password)
  1462. return hostapd_wps_add_nfc_password_token(hapd, &attr);
  1463. wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
  1464. return -1;
  1465. }
  1466. int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
  1467. const struct wpabuf *data)
  1468. {
  1469. const struct wpabuf *wps = data;
  1470. struct wpabuf *tmp = NULL;
  1471. int ret;
  1472. if (wpabuf_len(data) < 4)
  1473. return -1;
  1474. if (*wpabuf_head_u8(data) != 0x10) {
  1475. /* Assume this contains full NDEF record */
  1476. tmp = ndef_parse_wifi(data);
  1477. if (tmp == NULL) {
  1478. wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
  1479. return -1;
  1480. }
  1481. wps = tmp;
  1482. }
  1483. ret = hostapd_wps_nfc_tag_process(hapd, wps);
  1484. wpabuf_free(tmp);
  1485. return ret;
  1486. }
  1487. struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
  1488. int ndef)
  1489. {
  1490. struct wpabuf *ret;
  1491. if (hapd->wps == NULL)
  1492. return NULL;
  1493. ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
  1494. hapd->iconf->channel);
  1495. if (ndef && ret) {
  1496. struct wpabuf *tmp;
  1497. tmp = ndef_build_wifi(ret);
  1498. wpabuf_free(ret);
  1499. if (tmp == NULL)
  1500. return NULL;
  1501. ret = tmp;
  1502. }
  1503. return ret;
  1504. }
  1505. struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
  1506. {
  1507. struct wpabuf *ret;
  1508. if (hapd->wps == NULL)
  1509. return NULL;
  1510. if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
  1511. struct wps_context *wps = hapd->wps;
  1512. if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
  1513. &hapd->conf->wps_nfc_dh_privkey) < 0)
  1514. return NULL;
  1515. hostapd_wps_nfc_clear(wps);
  1516. wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
  1517. wps->ap_nfc_dh_pubkey =
  1518. wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
  1519. wps->ap_nfc_dh_privkey =
  1520. wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
  1521. if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
  1522. hostapd_wps_nfc_clear(wps);
  1523. return NULL;
  1524. }
  1525. }
  1526. ret = wps_build_nfc_handover_sel(hapd->wps,
  1527. hapd->conf->wps_nfc_dh_pubkey,
  1528. hapd->own_addr, hapd->iface->freq);
  1529. if (ndef && ret) {
  1530. struct wpabuf *tmp;
  1531. tmp = ndef_build_wifi(ret);
  1532. wpabuf_free(ret);
  1533. if (tmp == NULL)
  1534. return NULL;
  1535. ret = tmp;
  1536. }
  1537. return ret;
  1538. }
  1539. int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
  1540. const struct wpabuf *req,
  1541. const struct wpabuf *sel)
  1542. {
  1543. struct wpabuf *wps;
  1544. int ret = -1;
  1545. u16 wsc_len;
  1546. const u8 *pos;
  1547. struct wpabuf msg;
  1548. struct wps_parse_attr attr;
  1549. u16 dev_pw_id;
  1550. /*
  1551. * Enrollee/station is always initiator of the NFC connection handover,
  1552. * so use the request message here to find Enrollee public key hash.
  1553. */
  1554. wps = ndef_parse_wifi(req);
  1555. if (wps == NULL)
  1556. return -1;
  1557. wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
  1558. "payload from NFC connection handover");
  1559. wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
  1560. if (wpabuf_len(wps) < 2) {
  1561. wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
  1562. "Message");
  1563. goto out;
  1564. }
  1565. pos = wpabuf_head(wps);
  1566. wsc_len = WPA_GET_BE16(pos);
  1567. if (wsc_len > wpabuf_len(wps) - 2) {
  1568. wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
  1569. "in rt Wi-Fi Handover Request Message", wsc_len);
  1570. goto out;
  1571. }
  1572. pos += 2;
  1573. wpa_hexdump(MSG_DEBUG,
  1574. "WPS: WSC attributes in Wi-Fi Handover Request Message",
  1575. pos, wsc_len);
  1576. if (wsc_len < wpabuf_len(wps) - 2) {
  1577. wpa_hexdump(MSG_DEBUG,
  1578. "WPS: Ignore extra data after WSC attributes",
  1579. pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
  1580. }
  1581. wpabuf_set(&msg, pos, wsc_len);
  1582. ret = wps_parse_msg(&msg, &attr);
  1583. if (ret < 0) {
  1584. wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
  1585. "Wi-Fi Handover Request Message");
  1586. goto out;
  1587. }
  1588. if (attr.oob_dev_password == NULL ||
  1589. attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  1590. wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
  1591. "included in Wi-Fi Handover Request Message");
  1592. ret = -1;
  1593. goto out;
  1594. }
  1595. if (attr.uuid_e == NULL) {
  1596. wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
  1597. "Handover Request Message");
  1598. ret = -1;
  1599. goto out;
  1600. }
  1601. wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
  1602. wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
  1603. attr.oob_dev_password, attr.oob_dev_password_len);
  1604. dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
  1605. WPS_OOB_PUBKEY_HASH_LEN);
  1606. if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
  1607. wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
  1608. "%u in Wi-Fi Handover Request Message", dev_pw_id);
  1609. ret = -1;
  1610. goto out;
  1611. }
  1612. wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
  1613. attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
  1614. ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
  1615. attr.oob_dev_password,
  1616. DEV_PW_NFC_CONNECTION_HANDOVER,
  1617. NULL, 0, 1);
  1618. out:
  1619. wpabuf_free(wps);
  1620. return ret;
  1621. }
  1622. struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
  1623. {
  1624. if (hapd->conf->wps_nfc_pw_from_config) {
  1625. return wps_nfc_token_build(ndef,
  1626. hapd->conf->wps_nfc_dev_pw_id,
  1627. hapd->conf->wps_nfc_dh_pubkey,
  1628. hapd->conf->wps_nfc_dev_pw);
  1629. }
  1630. return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
  1631. &hapd->conf->wps_nfc_dh_pubkey,
  1632. &hapd->conf->wps_nfc_dh_privkey,
  1633. &hapd->conf->wps_nfc_dev_pw);
  1634. }
  1635. int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
  1636. {
  1637. struct wps_context *wps = hapd->wps;
  1638. struct wpabuf *pw;
  1639. if (wps == NULL)
  1640. return -1;
  1641. if (!hapd->conf->wps_nfc_dh_pubkey ||
  1642. !hapd->conf->wps_nfc_dh_privkey ||
  1643. !hapd->conf->wps_nfc_dev_pw ||
  1644. !hapd->conf->wps_nfc_dev_pw_id)
  1645. return -1;
  1646. hostapd_wps_nfc_clear(wps);
  1647. wpa_printf(MSG_DEBUG,
  1648. "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
  1649. hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
  1650. wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
  1651. wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
  1652. wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
  1653. pw = hapd->conf->wps_nfc_dev_pw;
  1654. wps->ap_nfc_dev_pw = wpabuf_alloc(
  1655. wpabuf_len(pw) * 2 + 1);
  1656. if (wps->ap_nfc_dev_pw) {
  1657. wpa_snprintf_hex_uppercase(
  1658. (char *) wpabuf_put(wps->ap_nfc_dev_pw,
  1659. wpabuf_len(pw) * 2),
  1660. wpabuf_len(pw) * 2 + 1,
  1661. wpabuf_head(pw), wpabuf_len(pw));
  1662. }
  1663. if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
  1664. !wps->ap_nfc_dev_pw) {
  1665. hostapd_wps_nfc_clear(wps);
  1666. return -1;
  1667. }
  1668. return 0;
  1669. }
  1670. void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
  1671. {
  1672. wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
  1673. hapd->conf->iface);
  1674. hostapd_wps_nfc_clear(hapd->wps);
  1675. }
  1676. #endif /* CONFIG_WPS_NFC */