wps_hostapd.c 48 KB

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