wps_hostapd.c 53 KB

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