ap.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * WPA Supplicant - Basic AP mode support routines
  3. * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2009, Atheros Communications
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "utils/includes.h"
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "utils/uuid.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "eapol_supp/eapol_supp_sm.h"
  16. #include "crypto/dh_group5.h"
  17. #include "ap/hostapd.h"
  18. #include "ap/ap_config.h"
  19. #include "ap/ap_drv_ops.h"
  20. #ifdef NEED_AP_MLME
  21. #include "ap/ieee802_11.h"
  22. #endif /* NEED_AP_MLME */
  23. #include "ap/beacon.h"
  24. #include "ap/ieee802_1x.h"
  25. #include "ap/wps_hostapd.h"
  26. #include "ap/ctrl_iface_ap.h"
  27. #include "wps/wps.h"
  28. #include "common/ieee802_11_defs.h"
  29. #include "config_ssid.h"
  30. #include "config.h"
  31. #include "wpa_supplicant_i.h"
  32. #include "driver_i.h"
  33. #include "p2p_supplicant.h"
  34. #include "ap.h"
  35. #include "ap/sta_info.h"
  36. #include "notify.h"
  37. #ifdef CONFIG_WPS
  38. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
  39. #endif /* CONFIG_WPS */
  40. #ifdef CONFIG_IEEE80211N
  41. static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
  42. struct hostapd_config *conf,
  43. struct hostapd_hw_modes *mode)
  44. {
  45. u8 center_chan = 0;
  46. u8 channel = conf->channel;
  47. if (!conf->secondary_channel)
  48. goto no_vht;
  49. center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
  50. if (!center_chan)
  51. goto no_vht;
  52. /* Use 80 MHz channel */
  53. conf->vht_oper_chwidth = 1;
  54. conf->vht_oper_centr_freq_seg0_idx = center_chan;
  55. return;
  56. no_vht:
  57. conf->vht_oper_centr_freq_seg0_idx =
  58. channel + conf->secondary_channel * 2;
  59. }
  60. #endif /* CONFIG_IEEE80211N */
  61. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  62. struct wpa_ssid *ssid,
  63. struct hostapd_config *conf)
  64. {
  65. struct hostapd_bss_config *bss = conf->bss[0];
  66. conf->driver = wpa_s->driver;
  67. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  68. conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
  69. &conf->channel);
  70. if (conf->hw_mode == NUM_HOSTAPD_MODES) {
  71. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  72. ssid->frequency);
  73. return -1;
  74. }
  75. /* TODO: enable HT40 if driver supports it;
  76. * drop to 11b if driver does not support 11g */
  77. #ifdef CONFIG_IEEE80211N
  78. /*
  79. * Enable HT20 if the driver supports it, by setting conf->ieee80211n
  80. * and a mask of allowed capabilities within conf->ht_capab.
  81. * Using default config settings for: conf->ht_op_mode_fixed,
  82. * conf->secondary_channel, conf->require_ht
  83. */
  84. if (wpa_s->hw.modes) {
  85. struct hostapd_hw_modes *mode = NULL;
  86. int i, no_ht = 0;
  87. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  88. if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
  89. mode = &wpa_s->hw.modes[i];
  90. break;
  91. }
  92. }
  93. #ifdef CONFIG_HT_OVERRIDES
  94. if (ssid->disable_ht) {
  95. conf->ieee80211n = 0;
  96. conf->ht_capab = 0;
  97. no_ht = 1;
  98. }
  99. #endif /* CONFIG_HT_OVERRIDES */
  100. if (!no_ht && mode && mode->ht_capab) {
  101. conf->ieee80211n = 1;
  102. #ifdef CONFIG_P2P
  103. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
  104. (mode->ht_capab &
  105. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
  106. ssid->ht40)
  107. conf->secondary_channel =
  108. wpas_p2p_get_ht40_mode(wpa_s, mode,
  109. conf->channel);
  110. if (conf->secondary_channel)
  111. conf->ht_capab |=
  112. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
  113. #endif /* CONFIG_P2P */
  114. /*
  115. * white-list capabilities that won't cause issues
  116. * to connecting stations, while leaving the current
  117. * capabilities intact (currently disabled SMPS).
  118. */
  119. conf->ht_capab |= mode->ht_capab &
  120. (HT_CAP_INFO_GREEN_FIELD |
  121. HT_CAP_INFO_SHORT_GI20MHZ |
  122. HT_CAP_INFO_SHORT_GI40MHZ |
  123. HT_CAP_INFO_RX_STBC_MASK |
  124. HT_CAP_INFO_MAX_AMSDU_SIZE);
  125. if (mode->vht_capab && ssid->vht) {
  126. conf->ieee80211ac = 1;
  127. wpas_conf_ap_vht(wpa_s, conf, mode);
  128. }
  129. }
  130. }
  131. #endif /* CONFIG_IEEE80211N */
  132. #ifdef CONFIG_P2P
  133. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
  134. (ssid->mode == WPAS_MODE_P2P_GO ||
  135. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
  136. /* Remove 802.11b rates from supported and basic rate sets */
  137. int *list = os_malloc(4 * sizeof(int));
  138. if (list) {
  139. list[0] = 60;
  140. list[1] = 120;
  141. list[2] = 240;
  142. list[3] = -1;
  143. }
  144. conf->basic_rates = list;
  145. list = os_malloc(9 * sizeof(int));
  146. if (list) {
  147. list[0] = 60;
  148. list[1] = 90;
  149. list[2] = 120;
  150. list[3] = 180;
  151. list[4] = 240;
  152. list[5] = 360;
  153. list[6] = 480;
  154. list[7] = 540;
  155. list[8] = -1;
  156. }
  157. conf->supported_rates = list;
  158. }
  159. bss->isolate = !wpa_s->conf->p2p_intra_bss;
  160. bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
  161. #endif /* CONFIG_P2P */
  162. if (ssid->ssid_len == 0) {
  163. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  164. return -1;
  165. }
  166. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  167. bss->ssid.ssid_len = ssid->ssid_len;
  168. bss->ssid.ssid_set = 1;
  169. bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
  170. if (ssid->auth_alg)
  171. bss->auth_algs = ssid->auth_alg;
  172. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  173. bss->wpa = ssid->proto;
  174. bss->wpa_key_mgmt = ssid->key_mgmt;
  175. bss->wpa_pairwise = ssid->pairwise_cipher;
  176. if (ssid->psk_set) {
  177. os_free(bss->ssid.wpa_psk);
  178. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  179. if (bss->ssid.wpa_psk == NULL)
  180. return -1;
  181. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  182. bss->ssid.wpa_psk->group = 1;
  183. } else if (ssid->passphrase) {
  184. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  185. } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
  186. ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
  187. struct hostapd_wep_keys *wep = &bss->ssid.wep;
  188. int i;
  189. for (i = 0; i < NUM_WEP_KEYS; i++) {
  190. if (ssid->wep_key_len[i] == 0)
  191. continue;
  192. wep->key[i] = os_malloc(ssid->wep_key_len[i]);
  193. if (wep->key[i] == NULL)
  194. return -1;
  195. os_memcpy(wep->key[i], ssid->wep_key[i],
  196. ssid->wep_key_len[i]);
  197. wep->len[i] = ssid->wep_key_len[i];
  198. }
  199. wep->idx = ssid->wep_tx_keyidx;
  200. wep->keys_set = 1;
  201. }
  202. if (ssid->ap_max_inactivity)
  203. bss->ap_max_inactivity = ssid->ap_max_inactivity;
  204. if (ssid->dtim_period)
  205. bss->dtim_period = ssid->dtim_period;
  206. else if (wpa_s->conf->dtim_period)
  207. bss->dtim_period = wpa_s->conf->dtim_period;
  208. if (ssid->beacon_int)
  209. conf->beacon_int = ssid->beacon_int;
  210. else if (wpa_s->conf->beacon_int)
  211. conf->beacon_int = wpa_s->conf->beacon_int;
  212. if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
  213. bss->rsn_pairwise = bss->wpa_pairwise;
  214. bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
  215. bss->rsn_pairwise);
  216. if (bss->wpa && bss->ieee802_1x)
  217. bss->ssid.security_policy = SECURITY_WPA;
  218. else if (bss->wpa)
  219. bss->ssid.security_policy = SECURITY_WPA_PSK;
  220. else if (bss->ieee802_1x) {
  221. int cipher = WPA_CIPHER_NONE;
  222. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  223. bss->ssid.wep.default_len = bss->default_wep_key_len;
  224. if (bss->default_wep_key_len)
  225. cipher = bss->default_wep_key_len >= 13 ?
  226. WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
  227. bss->wpa_group = cipher;
  228. bss->wpa_pairwise = cipher;
  229. bss->rsn_pairwise = cipher;
  230. } else if (bss->ssid.wep.keys_set) {
  231. int cipher = WPA_CIPHER_WEP40;
  232. if (bss->ssid.wep.len[0] >= 13)
  233. cipher = WPA_CIPHER_WEP104;
  234. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  235. bss->wpa_group = cipher;
  236. bss->wpa_pairwise = cipher;
  237. bss->rsn_pairwise = cipher;
  238. } else {
  239. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  240. bss->wpa_group = WPA_CIPHER_NONE;
  241. bss->wpa_pairwise = WPA_CIPHER_NONE;
  242. bss->rsn_pairwise = WPA_CIPHER_NONE;
  243. }
  244. if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
  245. (bss->wpa_group == WPA_CIPHER_CCMP ||
  246. bss->wpa_group == WPA_CIPHER_GCMP ||
  247. bss->wpa_group == WPA_CIPHER_CCMP_256 ||
  248. bss->wpa_group == WPA_CIPHER_GCMP_256)) {
  249. /*
  250. * Strong ciphers do not need frequent rekeying, so increase
  251. * the default GTK rekeying period to 24 hours.
  252. */
  253. bss->wpa_group_rekey = 86400;
  254. }
  255. #ifdef CONFIG_WPS
  256. /*
  257. * Enable WPS by default for open and WPA/WPA2-Personal network, but
  258. * require user interaction to actually use it. Only the internal
  259. * Registrar is supported.
  260. */
  261. if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
  262. bss->ssid.security_policy != SECURITY_PLAINTEXT)
  263. goto no_wps;
  264. #ifdef CONFIG_WPS2
  265. if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
  266. (!(bss->rsn_pairwise & WPA_CIPHER_CCMP) || !(bss->wpa & 2)))
  267. goto no_wps; /* WPS2 does not allow WPA/TKIP-only
  268. * configuration */
  269. #endif /* CONFIG_WPS2 */
  270. bss->eap_server = 1;
  271. if (!ssid->ignore_broadcast_ssid)
  272. bss->wps_state = 2;
  273. bss->ap_setup_locked = 2;
  274. if (wpa_s->conf->config_methods)
  275. bss->config_methods = os_strdup(wpa_s->conf->config_methods);
  276. os_memcpy(bss->device_type, wpa_s->conf->device_type,
  277. WPS_DEV_TYPE_LEN);
  278. if (wpa_s->conf->device_name) {
  279. bss->device_name = os_strdup(wpa_s->conf->device_name);
  280. bss->friendly_name = os_strdup(wpa_s->conf->device_name);
  281. }
  282. if (wpa_s->conf->manufacturer)
  283. bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
  284. if (wpa_s->conf->model_name)
  285. bss->model_name = os_strdup(wpa_s->conf->model_name);
  286. if (wpa_s->conf->model_number)
  287. bss->model_number = os_strdup(wpa_s->conf->model_number);
  288. if (wpa_s->conf->serial_number)
  289. bss->serial_number = os_strdup(wpa_s->conf->serial_number);
  290. if (is_nil_uuid(wpa_s->conf->uuid))
  291. os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
  292. else
  293. os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  294. os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
  295. bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
  296. no_wps:
  297. #endif /* CONFIG_WPS */
  298. if (wpa_s->max_stations &&
  299. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  300. bss->max_num_sta = wpa_s->max_stations;
  301. else
  302. bss->max_num_sta = wpa_s->conf->max_num_sta;
  303. bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
  304. if (wpa_s->conf->ap_vendor_elements) {
  305. bss->vendor_elements =
  306. wpabuf_dup(wpa_s->conf->ap_vendor_elements);
  307. }
  308. return 0;
  309. }
  310. static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  311. {
  312. #ifdef CONFIG_P2P
  313. struct wpa_supplicant *wpa_s = ctx;
  314. const struct ieee80211_mgmt *mgmt;
  315. size_t hdr_len;
  316. mgmt = (const struct ieee80211_mgmt *) buf;
  317. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  318. if (hdr_len > len)
  319. return;
  320. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
  321. return;
  322. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  323. mgmt->u.action.category,
  324. &mgmt->u.action.u.vs_public_action.action,
  325. len - hdr_len, freq);
  326. #endif /* CONFIG_P2P */
  327. }
  328. static void ap_wps_event_cb(void *ctx, enum wps_event event,
  329. union wps_event_data *data)
  330. {
  331. #ifdef CONFIG_P2P
  332. struct wpa_supplicant *wpa_s = ctx;
  333. if (event == WPS_EV_FAIL) {
  334. struct wps_event_fail *fail = &data->fail;
  335. if (wpa_s->parent && wpa_s->parent != wpa_s &&
  336. wpa_s == wpa_s->global->p2p_group_formation) {
  337. /*
  338. * src/ap/wps_hostapd.c has already sent this on the
  339. * main interface, so only send on the parent interface
  340. * here if needed.
  341. */
  342. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  343. "msg=%d config_error=%d",
  344. fail->msg, fail->config_error);
  345. }
  346. wpas_p2p_wps_failed(wpa_s, fail);
  347. }
  348. #endif /* CONFIG_P2P */
  349. }
  350. static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
  351. int authorized, const u8 *p2p_dev_addr)
  352. {
  353. wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
  354. }
  355. #ifdef CONFIG_P2P
  356. static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
  357. const u8 *psk, size_t psk_len)
  358. {
  359. struct wpa_supplicant *wpa_s = ctx;
  360. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
  361. return;
  362. wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
  363. }
  364. #endif /* CONFIG_P2P */
  365. static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  366. {
  367. #ifdef CONFIG_P2P
  368. struct wpa_supplicant *wpa_s = ctx;
  369. const struct ieee80211_mgmt *mgmt;
  370. size_t hdr_len;
  371. mgmt = (const struct ieee80211_mgmt *) buf;
  372. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  373. if (hdr_len > len)
  374. return -1;
  375. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  376. mgmt->u.action.category,
  377. &mgmt->u.action.u.vs_public_action.action,
  378. len - hdr_len, freq);
  379. #endif /* CONFIG_P2P */
  380. return 0;
  381. }
  382. static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
  383. const u8 *bssid, const u8 *ie, size_t ie_len,
  384. int ssi_signal)
  385. {
  386. #ifdef CONFIG_P2P
  387. struct wpa_supplicant *wpa_s = ctx;
  388. return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
  389. ssi_signal);
  390. #else /* CONFIG_P2P */
  391. return 0;
  392. #endif /* CONFIG_P2P */
  393. }
  394. static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  395. const u8 *uuid_e)
  396. {
  397. #ifdef CONFIG_P2P
  398. struct wpa_supplicant *wpa_s = ctx;
  399. wpas_p2p_wps_success(wpa_s, mac_addr, 1);
  400. #endif /* CONFIG_P2P */
  401. }
  402. static void wpas_ap_configured_cb(void *ctx)
  403. {
  404. struct wpa_supplicant *wpa_s = ctx;
  405. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  406. if (wpa_s->ap_configured_cb)
  407. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  408. wpa_s->ap_configured_cb_data);
  409. }
  410. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  411. struct wpa_ssid *ssid)
  412. {
  413. struct wpa_driver_associate_params params;
  414. struct hostapd_iface *hapd_iface;
  415. struct hostapd_config *conf;
  416. size_t i;
  417. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  418. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  419. return -1;
  420. }
  421. wpa_supplicant_ap_deinit(wpa_s);
  422. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  423. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  424. os_memset(&params, 0, sizeof(params));
  425. params.ssid = ssid->ssid;
  426. params.ssid_len = ssid->ssid_len;
  427. switch (ssid->mode) {
  428. case WPAS_MODE_INFRA:
  429. params.mode = IEEE80211_MODE_INFRA;
  430. break;
  431. case WPAS_MODE_IBSS:
  432. params.mode = IEEE80211_MODE_IBSS;
  433. break;
  434. case WPAS_MODE_AP:
  435. case WPAS_MODE_P2P_GO:
  436. case WPAS_MODE_P2P_GROUP_FORMATION:
  437. params.mode = IEEE80211_MODE_AP;
  438. break;
  439. }
  440. if (ssid->frequency == 0)
  441. ssid->frequency = 2462; /* default channel 11 */
  442. params.freq = ssid->frequency;
  443. params.wpa_proto = ssid->proto;
  444. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
  445. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  446. else
  447. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  448. params.key_mgmt_suite = wpa_s->key_mgmt;
  449. wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
  450. 1);
  451. if (wpa_s->pairwise_cipher < 0) {
  452. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  453. "cipher.");
  454. return -1;
  455. }
  456. params.pairwise_suite = wpa_s->pairwise_cipher;
  457. params.group_suite = params.pairwise_suite;
  458. #ifdef CONFIG_P2P
  459. if (ssid->mode == WPAS_MODE_P2P_GO ||
  460. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  461. params.p2p = 1;
  462. #endif /* CONFIG_P2P */
  463. if (wpa_s->parent->set_ap_uapsd)
  464. params.uapsd = wpa_s->parent->ap_uapsd;
  465. else
  466. params.uapsd = -1;
  467. if (wpa_drv_associate(wpa_s, &params) < 0) {
  468. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  469. return -1;
  470. }
  471. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  472. if (hapd_iface == NULL)
  473. return -1;
  474. hapd_iface->owner = wpa_s;
  475. hapd_iface->drv_flags = wpa_s->drv_flags;
  476. hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
  477. hapd_iface->extended_capa = wpa_s->extended_capa;
  478. hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
  479. hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
  480. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  481. if (conf == NULL) {
  482. wpa_supplicant_ap_deinit(wpa_s);
  483. return -1;
  484. }
  485. os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
  486. wpa_s->conf->wmm_ac_params,
  487. sizeof(wpa_s->conf->wmm_ac_params));
  488. if (params.uapsd > 0) {
  489. conf->bss[0]->wmm_enabled = 1;
  490. conf->bss[0]->wmm_uapsd = 1;
  491. }
  492. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  493. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  494. wpa_supplicant_ap_deinit(wpa_s);
  495. return -1;
  496. }
  497. #ifdef CONFIG_P2P
  498. if (ssid->mode == WPAS_MODE_P2P_GO)
  499. conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  500. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  501. conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  502. P2P_GROUP_FORMATION;
  503. #endif /* CONFIG_P2P */
  504. hapd_iface->num_bss = conf->num_bss;
  505. hapd_iface->bss = os_calloc(conf->num_bss,
  506. sizeof(struct hostapd_data *));
  507. if (hapd_iface->bss == NULL) {
  508. wpa_supplicant_ap_deinit(wpa_s);
  509. return -1;
  510. }
  511. for (i = 0; i < conf->num_bss; i++) {
  512. hapd_iface->bss[i] =
  513. hostapd_alloc_bss_data(hapd_iface, conf,
  514. conf->bss[i]);
  515. if (hapd_iface->bss[i] == NULL) {
  516. wpa_supplicant_ap_deinit(wpa_s);
  517. return -1;
  518. }
  519. hapd_iface->bss[i]->msg_ctx = wpa_s;
  520. hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
  521. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  522. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  523. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  524. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  525. hostapd_register_probereq_cb(hapd_iface->bss[i],
  526. ap_probe_req_rx, wpa_s);
  527. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  528. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  529. hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
  530. hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
  531. hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
  532. hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
  533. #ifdef CONFIG_P2P
  534. hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
  535. hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
  536. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  537. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
  538. ssid);
  539. #endif /* CONFIG_P2P */
  540. hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
  541. hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
  542. }
  543. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  544. hapd_iface->bss[0]->driver = wpa_s->driver;
  545. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  546. wpa_s->current_ssid = ssid;
  547. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  548. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  549. wpa_s->assoc_freq = ssid->frequency;
  550. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  551. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  552. wpa_supplicant_ap_deinit(wpa_s);
  553. return -1;
  554. }
  555. return 0;
  556. }
  557. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  558. {
  559. #ifdef CONFIG_WPS
  560. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  561. #endif /* CONFIG_WPS */
  562. if (wpa_s->ap_iface == NULL)
  563. return;
  564. wpa_s->current_ssid = NULL;
  565. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  566. wpa_s->assoc_freq = 0;
  567. #ifdef CONFIG_P2P
  568. if (wpa_s->ap_iface->bss)
  569. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  570. wpas_p2p_group_deinit(wpa_s);
  571. #endif /* CONFIG_P2P */
  572. hostapd_interface_deinit(wpa_s->ap_iface);
  573. hostapd_interface_free(wpa_s->ap_iface);
  574. wpa_s->ap_iface = NULL;
  575. wpa_drv_deinit_ap(wpa_s);
  576. }
  577. void ap_tx_status(void *ctx, const u8 *addr,
  578. const u8 *buf, size_t len, int ack)
  579. {
  580. #ifdef NEED_AP_MLME
  581. struct wpa_supplicant *wpa_s = ctx;
  582. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  583. #endif /* NEED_AP_MLME */
  584. }
  585. void ap_eapol_tx_status(void *ctx, const u8 *dst,
  586. const u8 *data, size_t len, int ack)
  587. {
  588. #ifdef NEED_AP_MLME
  589. struct wpa_supplicant *wpa_s = ctx;
  590. if (!wpa_s->ap_iface)
  591. return;
  592. hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
  593. #endif /* NEED_AP_MLME */
  594. }
  595. void ap_client_poll_ok(void *ctx, const u8 *addr)
  596. {
  597. #ifdef NEED_AP_MLME
  598. struct wpa_supplicant *wpa_s = ctx;
  599. if (wpa_s->ap_iface)
  600. hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
  601. #endif /* NEED_AP_MLME */
  602. }
  603. void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
  604. {
  605. #ifdef NEED_AP_MLME
  606. struct wpa_supplicant *wpa_s = ctx;
  607. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
  608. #endif /* NEED_AP_MLME */
  609. }
  610. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  611. {
  612. #ifdef NEED_AP_MLME
  613. struct wpa_supplicant *wpa_s = ctx;
  614. struct hostapd_frame_info fi;
  615. os_memset(&fi, 0, sizeof(fi));
  616. fi.datarate = rx_mgmt->datarate;
  617. fi.ssi_signal = rx_mgmt->ssi_signal;
  618. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  619. rx_mgmt->frame_len, &fi);
  620. #endif /* NEED_AP_MLME */
  621. }
  622. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  623. {
  624. #ifdef NEED_AP_MLME
  625. struct wpa_supplicant *wpa_s = ctx;
  626. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  627. #endif /* NEED_AP_MLME */
  628. }
  629. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  630. const u8 *src_addr, const u8 *buf, size_t len)
  631. {
  632. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  633. }
  634. #ifdef CONFIG_WPS
  635. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  636. const u8 *p2p_dev_addr)
  637. {
  638. if (!wpa_s->ap_iface)
  639. return -1;
  640. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
  641. p2p_dev_addr);
  642. }
  643. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  644. {
  645. struct wps_registrar *reg;
  646. int reg_sel = 0, wps_sta = 0;
  647. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  648. return -1;
  649. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  650. reg_sel = wps_registrar_wps_cancel(reg);
  651. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  652. ap_sta_wps_cancel, NULL);
  653. if (!reg_sel && !wps_sta) {
  654. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  655. "time");
  656. return -1;
  657. }
  658. /*
  659. * There are 2 cases to return wps cancel as success:
  660. * 1. When wps cancel was initiated but no connection has been
  661. * established with client yet.
  662. * 2. Client is in the middle of exchanging WPS messages.
  663. */
  664. return 0;
  665. }
  666. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  667. const char *pin, char *buf, size_t buflen,
  668. int timeout)
  669. {
  670. int ret, ret_len = 0;
  671. if (!wpa_s->ap_iface)
  672. return -1;
  673. if (pin == NULL) {
  674. unsigned int rpin = wps_generate_pin();
  675. ret_len = os_snprintf(buf, buflen, "%08d", rpin);
  676. pin = buf;
  677. } else
  678. ret_len = os_snprintf(buf, buflen, "%s", pin);
  679. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  680. timeout);
  681. if (ret)
  682. return -1;
  683. return ret_len;
  684. }
  685. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  686. {
  687. struct wpa_supplicant *wpa_s = eloop_data;
  688. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  689. wpas_wps_ap_pin_disable(wpa_s);
  690. }
  691. static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
  692. {
  693. struct hostapd_data *hapd;
  694. if (wpa_s->ap_iface == NULL)
  695. return;
  696. hapd = wpa_s->ap_iface->bss[0];
  697. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  698. hapd->ap_pin_failures = 0;
  699. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  700. if (timeout > 0)
  701. eloop_register_timeout(timeout, 0,
  702. wpas_wps_ap_pin_timeout, wpa_s, NULL);
  703. }
  704. void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
  705. {
  706. struct hostapd_data *hapd;
  707. if (wpa_s->ap_iface == NULL)
  708. return;
  709. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  710. hapd = wpa_s->ap_iface->bss[0];
  711. os_free(hapd->conf->ap_pin);
  712. hapd->conf->ap_pin = NULL;
  713. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  714. }
  715. const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
  716. {
  717. struct hostapd_data *hapd;
  718. unsigned int pin;
  719. char pin_txt[9];
  720. if (wpa_s->ap_iface == NULL)
  721. return NULL;
  722. hapd = wpa_s->ap_iface->bss[0];
  723. pin = wps_generate_pin();
  724. os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
  725. os_free(hapd->conf->ap_pin);
  726. hapd->conf->ap_pin = os_strdup(pin_txt);
  727. if (hapd->conf->ap_pin == NULL)
  728. return NULL;
  729. wpas_wps_ap_pin_enable(wpa_s, timeout);
  730. return hapd->conf->ap_pin;
  731. }
  732. const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
  733. {
  734. struct hostapd_data *hapd;
  735. if (wpa_s->ap_iface == NULL)
  736. return NULL;
  737. hapd = wpa_s->ap_iface->bss[0];
  738. return hapd->conf->ap_pin;
  739. }
  740. int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
  741. int timeout)
  742. {
  743. struct hostapd_data *hapd;
  744. char pin_txt[9];
  745. int ret;
  746. if (wpa_s->ap_iface == NULL)
  747. return -1;
  748. hapd = wpa_s->ap_iface->bss[0];
  749. ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
  750. if (ret < 0 || ret >= (int) sizeof(pin_txt))
  751. return -1;
  752. os_free(hapd->conf->ap_pin);
  753. hapd->conf->ap_pin = os_strdup(pin_txt);
  754. if (hapd->conf->ap_pin == NULL)
  755. return -1;
  756. wpas_wps_ap_pin_enable(wpa_s, timeout);
  757. return 0;
  758. }
  759. void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
  760. {
  761. struct hostapd_data *hapd;
  762. if (wpa_s->ap_iface == NULL)
  763. return;
  764. hapd = wpa_s->ap_iface->bss[0];
  765. /*
  766. * Registrar failed to prove its knowledge of the AP PIN. Disable AP
  767. * PIN if this happens multiple times to slow down brute force attacks.
  768. */
  769. hapd->ap_pin_failures++;
  770. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  771. hapd->ap_pin_failures);
  772. if (hapd->ap_pin_failures < 3)
  773. return;
  774. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
  775. hapd->ap_pin_failures = 0;
  776. os_free(hapd->conf->ap_pin);
  777. hapd->conf->ap_pin = NULL;
  778. }
  779. #ifdef CONFIG_WPS_NFC
  780. struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
  781. int ndef)
  782. {
  783. struct hostapd_data *hapd;
  784. if (wpa_s->ap_iface == NULL)
  785. return NULL;
  786. hapd = wpa_s->ap_iface->bss[0];
  787. return hostapd_wps_nfc_config_token(hapd, ndef);
  788. }
  789. struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  790. int ndef)
  791. {
  792. struct hostapd_data *hapd;
  793. if (wpa_s->ap_iface == NULL)
  794. return NULL;
  795. hapd = wpa_s->ap_iface->bss[0];
  796. return hostapd_wps_nfc_hs_cr(hapd, ndef);
  797. }
  798. int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
  799. const struct wpabuf *req,
  800. const struct wpabuf *sel)
  801. {
  802. struct hostapd_data *hapd;
  803. if (wpa_s->ap_iface == NULL)
  804. return -1;
  805. hapd = wpa_s->ap_iface->bss[0];
  806. return hostapd_wps_nfc_report_handover(hapd, req, sel);
  807. }
  808. #endif /* CONFIG_WPS_NFC */
  809. #endif /* CONFIG_WPS */
  810. #ifdef CONFIG_CTRL_IFACE
  811. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  812. char *buf, size_t buflen)
  813. {
  814. if (wpa_s->ap_iface == NULL)
  815. return -1;
  816. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  817. buf, buflen);
  818. }
  819. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  820. char *buf, size_t buflen)
  821. {
  822. if (wpa_s->ap_iface == NULL)
  823. return -1;
  824. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  825. buf, buflen);
  826. }
  827. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  828. char *buf, size_t buflen)
  829. {
  830. if (wpa_s->ap_iface == NULL)
  831. return -1;
  832. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  833. buf, buflen);
  834. }
  835. int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
  836. const char *txtaddr)
  837. {
  838. if (wpa_s->ap_iface == NULL)
  839. return -1;
  840. return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
  841. txtaddr);
  842. }
  843. int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
  844. const char *txtaddr)
  845. {
  846. if (wpa_s->ap_iface == NULL)
  847. return -1;
  848. return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
  849. txtaddr);
  850. }
  851. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  852. size_t buflen, int verbose)
  853. {
  854. char *pos = buf, *end = buf + buflen;
  855. int ret;
  856. struct hostapd_bss_config *conf;
  857. if (wpa_s->ap_iface == NULL)
  858. return -1;
  859. conf = wpa_s->ap_iface->bss[0]->conf;
  860. if (conf->wpa == 0)
  861. return 0;
  862. ret = os_snprintf(pos, end - pos,
  863. "pairwise_cipher=%s\n"
  864. "group_cipher=%s\n"
  865. "key_mgmt=%s\n",
  866. wpa_cipher_txt(conf->rsn_pairwise),
  867. wpa_cipher_txt(conf->wpa_group),
  868. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  869. conf->wpa));
  870. if (ret < 0 || ret >= end - pos)
  871. return pos - buf;
  872. pos += ret;
  873. return pos - buf;
  874. }
  875. #endif /* CONFIG_CTRL_IFACE */
  876. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  877. {
  878. struct hostapd_iface *iface = wpa_s->ap_iface;
  879. struct wpa_ssid *ssid = wpa_s->current_ssid;
  880. struct hostapd_data *hapd;
  881. if (ssid == NULL || wpa_s->ap_iface == NULL ||
  882. ssid->mode == WPAS_MODE_INFRA ||
  883. ssid->mode == WPAS_MODE_IBSS)
  884. return -1;
  885. #ifdef CONFIG_P2P
  886. if (ssid->mode == WPAS_MODE_P2P_GO)
  887. iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  888. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  889. iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  890. P2P_GROUP_FORMATION;
  891. #endif /* CONFIG_P2P */
  892. hapd = iface->bss[0];
  893. if (hapd->drv_priv == NULL)
  894. return -1;
  895. ieee802_11_set_beacons(iface);
  896. hostapd_set_ap_wps_ie(hapd);
  897. return 0;
  898. }
  899. int ap_switch_channel(struct wpa_supplicant *wpa_s,
  900. struct csa_settings *settings)
  901. {
  902. #ifdef NEED_AP_MLME
  903. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
  904. return -1;
  905. return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
  906. #else /* NEED_AP_MLME */
  907. return -1;
  908. #endif /* NEED_AP_MLME */
  909. }
  910. int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
  911. {
  912. struct csa_settings settings;
  913. int ret = hostapd_parse_csa_settings(pos, &settings);
  914. if (ret)
  915. return ret;
  916. return ap_switch_channel(wpa_s, &settings);
  917. }
  918. void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
  919. int offset, int width, int cf1, int cf2)
  920. {
  921. if (!wpa_s->ap_iface)
  922. return;
  923. wpa_s->assoc_freq = freq;
  924. hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset, width, cf1, cf1);
  925. }
  926. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  927. const u8 *addr)
  928. {
  929. struct hostapd_data *hapd;
  930. struct hostapd_bss_config *conf;
  931. if (!wpa_s->ap_iface)
  932. return -1;
  933. if (addr)
  934. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  935. MAC2STR(addr));
  936. else
  937. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  938. hapd = wpa_s->ap_iface->bss[0];
  939. conf = hapd->conf;
  940. os_free(conf->accept_mac);
  941. conf->accept_mac = NULL;
  942. conf->num_accept_mac = 0;
  943. os_free(conf->deny_mac);
  944. conf->deny_mac = NULL;
  945. conf->num_deny_mac = 0;
  946. if (addr == NULL) {
  947. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  948. return 0;
  949. }
  950. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  951. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  952. if (conf->accept_mac == NULL)
  953. return -1;
  954. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  955. conf->num_accept_mac = 1;
  956. return 0;
  957. }
  958. #ifdef CONFIG_WPS_NFC
  959. int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
  960. const struct wpabuf *pw, const u8 *pubkey_hash)
  961. {
  962. struct hostapd_data *hapd;
  963. struct wps_context *wps;
  964. if (!wpa_s->ap_iface)
  965. return -1;
  966. hapd = wpa_s->ap_iface->bss[0];
  967. wps = hapd->wps;
  968. if (wpa_s->parent->conf->wps_nfc_dh_pubkey == NULL ||
  969. wpa_s->parent->conf->wps_nfc_dh_privkey == NULL) {
  970. wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
  971. return -1;
  972. }
  973. dh5_free(wps->dh_ctx);
  974. wpabuf_free(wps->dh_pubkey);
  975. wpabuf_free(wps->dh_privkey);
  976. wps->dh_privkey = wpabuf_dup(
  977. wpa_s->parent->conf->wps_nfc_dh_privkey);
  978. wps->dh_pubkey = wpabuf_dup(
  979. wpa_s->parent->conf->wps_nfc_dh_pubkey);
  980. if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
  981. wps->dh_ctx = NULL;
  982. wpabuf_free(wps->dh_pubkey);
  983. wps->dh_pubkey = NULL;
  984. wpabuf_free(wps->dh_privkey);
  985. wps->dh_privkey = NULL;
  986. return -1;
  987. }
  988. wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
  989. if (wps->dh_ctx == NULL)
  990. return -1;
  991. return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
  992. pw_id,
  993. pw ? wpabuf_head(pw) : NULL,
  994. pw ? wpabuf_len(pw) : 0, 1);
  995. }
  996. #endif /* CONFIG_WPS_NFC */