wps_hostapd.c 51 KB

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