2 * WPA Supplicant - Basic AP mode support routines
3 * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2009, Atheros Communications
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
10 #include "utils/includes.h"
12 #include "utils/common.h"
13 #include "utils/eloop.h"
14 #include "utils/uuid.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "eapol_supp/eapol_supp_sm.h"
18 #include "crypto/dh_group5.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/ap_drv_ops.h"
23 #include "ap/ieee802_11.h"
24 #endif /* NEED_AP_MLME */
25 #include "ap/beacon.h"
26 #include "ap/ieee802_1x.h"
27 #include "ap/wps_hostapd.h"
28 #include "ap/ctrl_iface_ap.h"
31 #include "common/ieee802_11_defs.h"
32 #include "config_ssid.h"
34 #include "wpa_supplicant_i.h"
36 #include "p2p_supplicant.h"
38 #include "ap/sta_info.h"
43 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
44 #endif /* CONFIG_WPS */
47 #ifdef CONFIG_IEEE80211N
48 static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
49 struct hostapd_config *conf,
50 struct hostapd_hw_modes *mode)
54 u8 channel = conf->channel;
56 if (!conf->secondary_channel)
59 center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
63 /* Use 80 MHz channel */
64 conf->vht_oper_chwidth = 1;
65 conf->vht_oper_centr_freq_seg0_idx = center_chan;
69 conf->vht_oper_centr_freq_seg0_idx =
70 channel + conf->secondary_channel * 2;
71 #else /* CONFIG_P2P */
72 conf->vht_oper_centr_freq_seg0_idx =
73 conf->channel + conf->secondary_channel * 2;
74 #endif /* CONFIG_P2P */
76 #endif /* CONFIG_IEEE80211N */
79 void wpa_supplicant_conf_ap_ht(struct wpa_supplicant *wpa_s,
80 struct wpa_ssid *ssid,
81 struct hostapd_config *conf)
83 /* TODO: enable HT40 if driver supports it;
84 * drop to 11b if driver does not support 11g */
86 #ifdef CONFIG_IEEE80211N
88 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
89 * and a mask of allowed capabilities within conf->ht_capab.
90 * Using default config settings for: conf->ht_op_mode_fixed,
91 * conf->secondary_channel, conf->require_ht
93 if (wpa_s->hw.modes) {
94 struct hostapd_hw_modes *mode = NULL;
96 for (i = 0; i < wpa_s->hw.num_modes; i++) {
97 if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
98 mode = &wpa_s->hw.modes[i];
103 #ifdef CONFIG_HT_OVERRIDES
104 if (ssid->disable_ht) {
105 conf->ieee80211n = 0;
109 #endif /* CONFIG_HT_OVERRIDES */
111 if (!no_ht && mode && mode->ht_capab) {
112 conf->ieee80211n = 1;
114 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
116 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
118 conf->secondary_channel =
119 wpas_p2p_get_ht40_mode(wpa_s, mode,
121 if (conf->secondary_channel)
123 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
124 #endif /* CONFIG_P2P */
127 * white-list capabilities that won't cause issues
128 * to connecting stations, while leaving the current
129 * capabilities intact (currently disabled SMPS).
131 conf->ht_capab |= mode->ht_capab &
132 (HT_CAP_INFO_GREEN_FIELD |
133 HT_CAP_INFO_SHORT_GI20MHZ |
134 HT_CAP_INFO_SHORT_GI40MHZ |
135 HT_CAP_INFO_RX_STBC_MASK |
136 HT_CAP_INFO_TX_STBC |
137 HT_CAP_INFO_MAX_AMSDU_SIZE);
139 if (mode->vht_capab && ssid->vht) {
140 conf->ieee80211ac = 1;
141 wpas_conf_ap_vht(wpa_s, conf, mode);
146 if (conf->secondary_channel) {
147 struct wpa_supplicant *iface;
149 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
151 if (iface == wpa_s ||
152 iface->wpa_state < WPA_AUTHENTICATING ||
153 (int) iface->assoc_freq != ssid->frequency)
157 * Do not allow 40 MHz co-ex PRI/SEC switch to force us
158 * to change our PRI channel since we have an existing,
159 * concurrent connection on that channel and doing
160 * multi-channel concurrency is likely to cause more
161 * harm than using different PRI/SEC selection in
162 * environment with multiple BSSes on these two channels
163 * with mixed 20 MHz or PRI channel selection.
165 conf->no_pri_sec_switch = 1;
168 #endif /* CONFIG_IEEE80211N */
172 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
173 struct wpa_ssid *ssid,
174 struct hostapd_config *conf)
176 struct hostapd_bss_config *bss = conf->bss[0];
178 conf->driver = wpa_s->driver;
180 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
182 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
184 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
185 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
190 wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
192 if (ieee80211_is_dfs(ssid->frequency) && wpa_s->conf->country[0]) {
193 conf->ieee80211h = 1;
194 conf->ieee80211d = 1;
195 conf->country[0] = wpa_s->conf->country[0];
196 conf->country[1] = wpa_s->conf->country[1];
200 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
201 (ssid->mode == WPAS_MODE_P2P_GO ||
202 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
203 /* Remove 802.11b rates from supported and basic rate sets */
204 int *list = os_malloc(4 * sizeof(int));
211 conf->basic_rates = list;
213 list = os_malloc(9 * sizeof(int));
225 conf->supported_rates = list;
228 bss->isolate = !wpa_s->conf->p2p_intra_bss;
229 bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
231 if (ssid->p2p_group) {
232 os_memcpy(bss->ip_addr_go, wpa_s->parent->conf->ip_addr_go, 4);
233 os_memcpy(bss->ip_addr_mask, wpa_s->parent->conf->ip_addr_mask,
235 os_memcpy(bss->ip_addr_start,
236 wpa_s->parent->conf->ip_addr_start, 4);
237 os_memcpy(bss->ip_addr_end, wpa_s->parent->conf->ip_addr_end,
240 #endif /* CONFIG_P2P */
242 if (ssid->ssid_len == 0) {
243 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
246 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
247 bss->ssid.ssid_len = ssid->ssid_len;
248 bss->ssid.ssid_set = 1;
250 bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
253 bss->auth_algs = ssid->auth_alg;
255 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
256 bss->wpa = ssid->proto;
257 bss->wpa_key_mgmt = ssid->key_mgmt;
258 bss->wpa_pairwise = ssid->pairwise_cipher;
260 bin_clear_free(bss->ssid.wpa_psk, sizeof(*bss->ssid.wpa_psk));
261 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
262 if (bss->ssid.wpa_psk == NULL)
264 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
265 bss->ssid.wpa_psk->group = 1;
266 } else if (ssid->passphrase) {
267 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
268 } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
269 ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
270 struct hostapd_wep_keys *wep = &bss->ssid.wep;
272 for (i = 0; i < NUM_WEP_KEYS; i++) {
273 if (ssid->wep_key_len[i] == 0)
275 wep->key[i] = os_malloc(ssid->wep_key_len[i]);
276 if (wep->key[i] == NULL)
278 os_memcpy(wep->key[i], ssid->wep_key[i],
279 ssid->wep_key_len[i]);
280 wep->len[i] = ssid->wep_key_len[i];
282 wep->idx = ssid->wep_tx_keyidx;
286 if (ssid->ap_max_inactivity)
287 bss->ap_max_inactivity = ssid->ap_max_inactivity;
289 if (ssid->dtim_period)
290 bss->dtim_period = ssid->dtim_period;
291 else if (wpa_s->conf->dtim_period)
292 bss->dtim_period = wpa_s->conf->dtim_period;
294 if (ssid->beacon_int)
295 conf->beacon_int = ssid->beacon_int;
296 else if (wpa_s->conf->beacon_int)
297 conf->beacon_int = wpa_s->conf->beacon_int;
300 if (wpa_s->conf->p2p_go_ctwindow > conf->beacon_int) {
302 "CTWindow (%d) is bigger than beacon interval (%d) - avoid configuring it",
303 wpa_s->conf->p2p_go_ctwindow, conf->beacon_int);
304 conf->p2p_go_ctwindow = 0;
306 conf->p2p_go_ctwindow = wpa_s->conf->p2p_go_ctwindow;
308 #endif /* CONFIG_P2P */
310 if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
311 bss->rsn_pairwise = bss->wpa_pairwise;
312 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
315 if (bss->wpa && bss->ieee802_1x)
316 bss->ssid.security_policy = SECURITY_WPA;
318 bss->ssid.security_policy = SECURITY_WPA_PSK;
319 else if (bss->ieee802_1x) {
320 int cipher = WPA_CIPHER_NONE;
321 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
322 bss->ssid.wep.default_len = bss->default_wep_key_len;
323 if (bss->default_wep_key_len)
324 cipher = bss->default_wep_key_len >= 13 ?
325 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
326 bss->wpa_group = cipher;
327 bss->wpa_pairwise = cipher;
328 bss->rsn_pairwise = cipher;
329 } else if (bss->ssid.wep.keys_set) {
330 int cipher = WPA_CIPHER_WEP40;
331 if (bss->ssid.wep.len[0] >= 13)
332 cipher = WPA_CIPHER_WEP104;
333 bss->ssid.security_policy = SECURITY_STATIC_WEP;
334 bss->wpa_group = cipher;
335 bss->wpa_pairwise = cipher;
336 bss->rsn_pairwise = cipher;
338 bss->ssid.security_policy = SECURITY_PLAINTEXT;
339 bss->wpa_group = WPA_CIPHER_NONE;
340 bss->wpa_pairwise = WPA_CIPHER_NONE;
341 bss->rsn_pairwise = WPA_CIPHER_NONE;
344 if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
345 (bss->wpa_group == WPA_CIPHER_CCMP ||
346 bss->wpa_group == WPA_CIPHER_GCMP ||
347 bss->wpa_group == WPA_CIPHER_CCMP_256 ||
348 bss->wpa_group == WPA_CIPHER_GCMP_256)) {
350 * Strong ciphers do not need frequent rekeying, so increase
351 * the default GTK rekeying period to 24 hours.
353 bss->wpa_group_rekey = 86400;
356 #ifdef CONFIG_IEEE80211W
357 if (ssid->ieee80211w != MGMT_FRAME_PROTECTION_DEFAULT)
358 bss->ieee80211w = ssid->ieee80211w;
359 #endif /* CONFIG_IEEE80211W */
363 * Enable WPS by default for open and WPA/WPA2-Personal network, but
364 * require user interaction to actually use it. Only the internal
365 * Registrar is supported.
367 if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
368 bss->ssid.security_policy != SECURITY_PLAINTEXT)
370 if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
371 (!(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ||
373 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
377 if (!ssid->ignore_broadcast_ssid)
380 bss->ap_setup_locked = 2;
381 if (wpa_s->conf->config_methods)
382 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
383 os_memcpy(bss->device_type, wpa_s->conf->device_type,
385 if (wpa_s->conf->device_name) {
386 bss->device_name = os_strdup(wpa_s->conf->device_name);
387 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
389 if (wpa_s->conf->manufacturer)
390 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
391 if (wpa_s->conf->model_name)
392 bss->model_name = os_strdup(wpa_s->conf->model_name);
393 if (wpa_s->conf->model_number)
394 bss->model_number = os_strdup(wpa_s->conf->model_number);
395 if (wpa_s->conf->serial_number)
396 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
397 if (is_nil_uuid(wpa_s->conf->uuid))
398 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
400 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
401 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
402 bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
404 #endif /* CONFIG_WPS */
406 if (wpa_s->max_stations &&
407 wpa_s->max_stations < wpa_s->conf->max_num_sta)
408 bss->max_num_sta = wpa_s->max_stations;
410 bss->max_num_sta = wpa_s->conf->max_num_sta;
412 bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
414 if (wpa_s->conf->ap_vendor_elements) {
415 bss->vendor_elements =
416 wpabuf_dup(wpa_s->conf->ap_vendor_elements);
423 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
426 struct wpa_supplicant *wpa_s = ctx;
427 const struct ieee80211_mgmt *mgmt;
429 mgmt = (const struct ieee80211_mgmt *) buf;
430 if (len < IEEE80211_HDRLEN + 1)
432 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
434 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
435 mgmt->u.action.category,
436 buf + IEEE80211_HDRLEN + 1,
437 len - IEEE80211_HDRLEN - 1, freq);
438 #endif /* CONFIG_P2P */
442 static void ap_wps_event_cb(void *ctx, enum wps_event event,
443 union wps_event_data *data)
446 struct wpa_supplicant *wpa_s = ctx;
448 if (event == WPS_EV_FAIL) {
449 struct wps_event_fail *fail = &data->fail;
451 if (wpa_s->parent && wpa_s->parent != wpa_s &&
452 wpa_s == wpa_s->global->p2p_group_formation) {
454 * src/ap/wps_hostapd.c has already sent this on the
455 * main interface, so only send on the parent interface
458 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
459 "msg=%d config_error=%d",
460 fail->msg, fail->config_error);
462 wpas_p2p_wps_failed(wpa_s, fail);
464 #endif /* CONFIG_P2P */
468 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
469 int authorized, const u8 *p2p_dev_addr)
471 wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
476 static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
477 const u8 *psk, size_t psk_len)
480 struct wpa_supplicant *wpa_s = ctx;
481 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
483 wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
485 #endif /* CONFIG_P2P */
488 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
491 struct wpa_supplicant *wpa_s = ctx;
492 const struct ieee80211_mgmt *mgmt;
494 mgmt = (const struct ieee80211_mgmt *) buf;
495 if (len < IEEE80211_HDRLEN + 1)
497 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
498 mgmt->u.action.category,
499 buf + IEEE80211_HDRLEN + 1,
500 len - IEEE80211_HDRLEN - 1, freq);
501 #endif /* CONFIG_P2P */
506 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
507 const u8 *bssid, const u8 *ie, size_t ie_len,
510 struct wpa_supplicant *wpa_s = ctx;
511 unsigned int freq = 0;
514 freq = wpa_s->ap_iface->freq;
516 return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
521 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
524 struct wpa_supplicant *wpa_s = ctx;
525 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
529 static void wpas_ap_configured_cb(void *ctx)
531 struct wpa_supplicant *wpa_s = ctx;
533 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
535 if (wpa_s->ap_configured_cb)
536 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
537 wpa_s->ap_configured_cb_data);
541 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
542 struct wpa_ssid *ssid)
544 struct wpa_driver_associate_params params;
545 struct hostapd_iface *hapd_iface;
546 struct hostapd_config *conf;
549 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
550 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
554 wpa_supplicant_ap_deinit(wpa_s);
556 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
557 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
559 os_memset(¶ms, 0, sizeof(params));
560 params.ssid = ssid->ssid;
561 params.ssid_len = ssid->ssid_len;
562 switch (ssid->mode) {
564 case WPAS_MODE_P2P_GO:
565 case WPAS_MODE_P2P_GROUP_FORMATION:
566 params.mode = IEEE80211_MODE_AP;
571 if (ssid->frequency == 0)
572 ssid->frequency = 2462; /* default channel 11 */
573 params.freq.freq = ssid->frequency;
575 params.wpa_proto = ssid->proto;
576 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
577 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
579 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
580 params.key_mgmt_suite = wpa_s->key_mgmt;
582 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
584 if (wpa_s->pairwise_cipher < 0) {
585 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
589 params.pairwise_suite = wpa_s->pairwise_cipher;
590 params.group_suite = params.pairwise_suite;
593 if (ssid->mode == WPAS_MODE_P2P_GO ||
594 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
596 #endif /* CONFIG_P2P */
598 if (wpa_s->parent->set_ap_uapsd)
599 params.uapsd = wpa_s->parent->ap_uapsd;
600 else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
601 params.uapsd = 1; /* mandatory for P2P GO */
605 if (ieee80211_is_dfs(params.freq.freq))
606 params.freq.freq = 0; /* set channel after CAC */
608 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
609 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
613 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
614 if (hapd_iface == NULL)
616 hapd_iface->owner = wpa_s;
617 hapd_iface->drv_flags = wpa_s->drv_flags;
618 hapd_iface->smps_modes = wpa_s->drv_smps_modes;
619 hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
620 hapd_iface->extended_capa = wpa_s->extended_capa;
621 hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
622 hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
624 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
626 wpa_supplicant_ap_deinit(wpa_s);
630 os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
631 wpa_s->conf->wmm_ac_params,
632 sizeof(wpa_s->conf->wmm_ac_params));
634 if (params.uapsd > 0) {
635 conf->bss[0]->wmm_enabled = 1;
636 conf->bss[0]->wmm_uapsd = 1;
639 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
640 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
641 wpa_supplicant_ap_deinit(wpa_s);
646 if (ssid->mode == WPAS_MODE_P2P_GO)
647 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
648 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
649 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
651 #endif /* CONFIG_P2P */
653 hapd_iface->num_bss = conf->num_bss;
654 hapd_iface->bss = os_calloc(conf->num_bss,
655 sizeof(struct hostapd_data *));
656 if (hapd_iface->bss == NULL) {
657 wpa_supplicant_ap_deinit(wpa_s);
661 for (i = 0; i < conf->num_bss; i++) {
663 hostapd_alloc_bss_data(hapd_iface, conf,
665 if (hapd_iface->bss[i] == NULL) {
666 wpa_supplicant_ap_deinit(wpa_s);
670 hapd_iface->bss[i]->msg_ctx = wpa_s;
671 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
672 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
673 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
674 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
675 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
676 hostapd_register_probereq_cb(hapd_iface->bss[i],
677 ap_probe_req_rx, wpa_s);
678 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
679 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
680 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
681 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
682 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
683 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
685 hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
686 hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
687 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
688 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
690 #endif /* CONFIG_P2P */
691 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
692 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
693 #ifdef CONFIG_TESTING_OPTIONS
694 hapd_iface->bss[i]->ext_eapol_frame_io =
695 wpa_s->ext_eapol_frame_io;
696 #endif /* CONFIG_TESTING_OPTIONS */
699 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
700 hapd_iface->bss[0]->driver = wpa_s->driver;
701 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
703 wpa_s->current_ssid = ssid;
704 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
705 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
706 wpa_s->assoc_freq = ssid->frequency;
708 if (hostapd_setup_interface(wpa_s->ap_iface)) {
709 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
710 wpa_supplicant_ap_deinit(wpa_s);
718 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
721 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
722 #endif /* CONFIG_WPS */
724 if (wpa_s->ap_iface == NULL)
727 wpa_s->current_ssid = NULL;
728 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
729 wpa_s->assoc_freq = 0;
730 wpas_p2p_ap_deinit(wpa_s);
731 wpa_s->ap_iface->driver_ap_teardown =
732 !!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
734 hostapd_interface_deinit(wpa_s->ap_iface);
735 hostapd_interface_free(wpa_s->ap_iface);
736 wpa_s->ap_iface = NULL;
737 wpa_drv_deinit_ap(wpa_s);
738 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
739 " reason=%d locally_generated=1",
740 MAC2STR(wpa_s->own_addr), WLAN_REASON_DEAUTH_LEAVING);
744 void ap_tx_status(void *ctx, const u8 *addr,
745 const u8 *buf, size_t len, int ack)
748 struct wpa_supplicant *wpa_s = ctx;
749 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
750 #endif /* NEED_AP_MLME */
754 void ap_eapol_tx_status(void *ctx, const u8 *dst,
755 const u8 *data, size_t len, int ack)
758 struct wpa_supplicant *wpa_s = ctx;
759 if (!wpa_s->ap_iface)
761 hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
762 #endif /* NEED_AP_MLME */
766 void ap_client_poll_ok(void *ctx, const u8 *addr)
769 struct wpa_supplicant *wpa_s = ctx;
771 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
772 #endif /* NEED_AP_MLME */
776 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
779 struct wpa_supplicant *wpa_s = ctx;
780 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
781 #endif /* NEED_AP_MLME */
785 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
788 struct wpa_supplicant *wpa_s = ctx;
789 struct hostapd_frame_info fi;
790 os_memset(&fi, 0, sizeof(fi));
791 fi.datarate = rx_mgmt->datarate;
792 fi.ssi_signal = rx_mgmt->ssi_signal;
793 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
794 rx_mgmt->frame_len, &fi);
795 #endif /* NEED_AP_MLME */
799 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
802 struct wpa_supplicant *wpa_s = ctx;
803 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
804 #endif /* NEED_AP_MLME */
808 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
809 const u8 *src_addr, const u8 *buf, size_t len)
811 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
817 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
818 const u8 *p2p_dev_addr)
820 if (!wpa_s->ap_iface)
822 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
827 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
829 struct wps_registrar *reg;
830 int reg_sel = 0, wps_sta = 0;
832 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
835 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
836 reg_sel = wps_registrar_wps_cancel(reg);
837 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
838 ap_sta_wps_cancel, NULL);
840 if (!reg_sel && !wps_sta) {
841 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
847 * There are 2 cases to return wps cancel as success:
848 * 1. When wps cancel was initiated but no connection has been
849 * established with client yet.
850 * 2. Client is in the middle of exchanging WPS messages.
857 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
858 const char *pin, char *buf, size_t buflen,
861 int ret, ret_len = 0;
863 if (!wpa_s->ap_iface)
867 unsigned int rpin = wps_generate_pin();
868 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
869 if (os_snprintf_error(buflen, ret_len))
873 ret_len = os_snprintf(buf, buflen, "%s", pin);
874 if (os_snprintf_error(buflen, ret_len))
878 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
886 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
888 struct wpa_supplicant *wpa_s = eloop_data;
889 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
890 wpas_wps_ap_pin_disable(wpa_s);
894 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
896 struct hostapd_data *hapd;
898 if (wpa_s->ap_iface == NULL)
900 hapd = wpa_s->ap_iface->bss[0];
901 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
902 hapd->ap_pin_failures = 0;
903 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
905 eloop_register_timeout(timeout, 0,
906 wpas_wps_ap_pin_timeout, wpa_s, NULL);
910 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
912 struct hostapd_data *hapd;
914 if (wpa_s->ap_iface == NULL)
916 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
917 hapd = wpa_s->ap_iface->bss[0];
918 os_free(hapd->conf->ap_pin);
919 hapd->conf->ap_pin = NULL;
920 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
924 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
926 struct hostapd_data *hapd;
930 if (wpa_s->ap_iface == NULL)
932 hapd = wpa_s->ap_iface->bss[0];
933 pin = wps_generate_pin();
934 os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
935 os_free(hapd->conf->ap_pin);
936 hapd->conf->ap_pin = os_strdup(pin_txt);
937 if (hapd->conf->ap_pin == NULL)
939 wpas_wps_ap_pin_enable(wpa_s, timeout);
941 return hapd->conf->ap_pin;
945 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
947 struct hostapd_data *hapd;
948 if (wpa_s->ap_iface == NULL)
950 hapd = wpa_s->ap_iface->bss[0];
951 return hapd->conf->ap_pin;
955 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
958 struct hostapd_data *hapd;
962 if (wpa_s->ap_iface == NULL)
964 hapd = wpa_s->ap_iface->bss[0];
965 ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
966 if (os_snprintf_error(sizeof(pin_txt), ret))
968 os_free(hapd->conf->ap_pin);
969 hapd->conf->ap_pin = os_strdup(pin_txt);
970 if (hapd->conf->ap_pin == NULL)
972 wpas_wps_ap_pin_enable(wpa_s, timeout);
978 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
980 struct hostapd_data *hapd;
982 if (wpa_s->ap_iface == NULL)
984 hapd = wpa_s->ap_iface->bss[0];
987 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
988 * PIN if this happens multiple times to slow down brute force attacks.
990 hapd->ap_pin_failures++;
991 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
992 hapd->ap_pin_failures);
993 if (hapd->ap_pin_failures < 3)
996 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
997 hapd->ap_pin_failures = 0;
998 os_free(hapd->conf->ap_pin);
999 hapd->conf->ap_pin = NULL;
1003 #ifdef CONFIG_WPS_NFC
1005 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
1008 struct hostapd_data *hapd;
1010 if (wpa_s->ap_iface == NULL)
1012 hapd = wpa_s->ap_iface->bss[0];
1013 return hostapd_wps_nfc_config_token(hapd, ndef);
1017 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
1020 struct hostapd_data *hapd;
1022 if (wpa_s->ap_iface == NULL)
1024 hapd = wpa_s->ap_iface->bss[0];
1025 return hostapd_wps_nfc_hs_cr(hapd, ndef);
1029 int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
1030 const struct wpabuf *req,
1031 const struct wpabuf *sel)
1033 struct hostapd_data *hapd;
1035 if (wpa_s->ap_iface == NULL)
1037 hapd = wpa_s->ap_iface->bss[0];
1038 return hostapd_wps_nfc_report_handover(hapd, req, sel);
1041 #endif /* CONFIG_WPS_NFC */
1043 #endif /* CONFIG_WPS */
1046 #ifdef CONFIG_CTRL_IFACE
1048 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
1049 char *buf, size_t buflen)
1051 struct hostapd_data *hapd;
1053 if (wpa_s->ap_iface)
1054 hapd = wpa_s->ap_iface->bss[0];
1055 else if (wpa_s->ifmsh)
1056 hapd = wpa_s->ifmsh->bss[0];
1059 return hostapd_ctrl_iface_sta_first(hapd, buf, buflen);
1063 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
1064 char *buf, size_t buflen)
1066 struct hostapd_data *hapd;
1068 if (wpa_s->ap_iface)
1069 hapd = wpa_s->ap_iface->bss[0];
1070 else if (wpa_s->ifmsh)
1071 hapd = wpa_s->ifmsh->bss[0];
1074 return hostapd_ctrl_iface_sta(hapd, txtaddr, buf, buflen);
1078 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
1079 char *buf, size_t buflen)
1081 struct hostapd_data *hapd;
1083 if (wpa_s->ap_iface)
1084 hapd = wpa_s->ap_iface->bss[0];
1085 else if (wpa_s->ifmsh)
1086 hapd = wpa_s->ifmsh->bss[0];
1089 return hostapd_ctrl_iface_sta_next(hapd, txtaddr, buf, buflen);
1093 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
1094 const char *txtaddr)
1096 if (wpa_s->ap_iface == NULL)
1098 return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
1103 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
1104 const char *txtaddr)
1106 if (wpa_s->ap_iface == NULL)
1108 return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
1113 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
1114 size_t buflen, int verbose)
1116 char *pos = buf, *end = buf + buflen;
1118 struct hostapd_bss_config *conf;
1120 if (wpa_s->ap_iface == NULL)
1123 conf = wpa_s->ap_iface->bss[0]->conf;
1127 ret = os_snprintf(pos, end - pos,
1128 "pairwise_cipher=%s\n"
1131 wpa_cipher_txt(conf->rsn_pairwise),
1132 wpa_cipher_txt(conf->wpa_group),
1133 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
1135 if (os_snprintf_error(end - pos, ret))
1141 #endif /* CONFIG_CTRL_IFACE */
1144 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1146 struct hostapd_iface *iface = wpa_s->ap_iface;
1147 struct wpa_ssid *ssid = wpa_s->current_ssid;
1148 struct hostapd_data *hapd;
1150 if (ssid == NULL || wpa_s->ap_iface == NULL ||
1151 ssid->mode == WPAS_MODE_INFRA ||
1152 ssid->mode == WPAS_MODE_IBSS)
1156 if (ssid->mode == WPAS_MODE_P2P_GO)
1157 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
1158 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1159 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
1160 P2P_GROUP_FORMATION;
1161 #endif /* CONFIG_P2P */
1163 hapd = iface->bss[0];
1164 if (hapd->drv_priv == NULL)
1166 ieee802_11_set_beacons(iface);
1167 hostapd_set_ap_wps_ie(hapd);
1173 int ap_switch_channel(struct wpa_supplicant *wpa_s,
1174 struct csa_settings *settings)
1177 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1180 return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
1181 #else /* NEED_AP_MLME */
1183 #endif /* NEED_AP_MLME */
1187 #ifdef CONFIG_CTRL_IFACE
1188 int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
1190 struct csa_settings settings;
1191 int ret = hostapd_parse_csa_settings(pos, &settings);
1196 return ap_switch_channel(wpa_s, &settings);
1198 #endif /* CONFIG_CTRL_IFACE */
1201 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1202 int offset, int width, int cf1, int cf2)
1204 if (!wpa_s->ap_iface)
1207 wpa_s->assoc_freq = freq;
1208 if (wpa_s->current_ssid)
1209 wpa_s->current_ssid->frequency = freq;
1210 hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht,
1211 offset, width, cf1, cf2);
1215 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1218 struct hostapd_data *hapd;
1219 struct hostapd_bss_config *conf;
1221 if (!wpa_s->ap_iface)
1225 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1228 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1230 hapd = wpa_s->ap_iface->bss[0];
1233 os_free(conf->accept_mac);
1234 conf->accept_mac = NULL;
1235 conf->num_accept_mac = 0;
1236 os_free(conf->deny_mac);
1237 conf->deny_mac = NULL;
1238 conf->num_deny_mac = 0;
1241 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1245 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1246 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1247 if (conf->accept_mac == NULL)
1249 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1250 conf->num_accept_mac = 1;
1256 #ifdef CONFIG_WPS_NFC
1257 int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
1258 const struct wpabuf *pw, const u8 *pubkey_hash)
1260 struct hostapd_data *hapd;
1261 struct wps_context *wps;
1263 if (!wpa_s->ap_iface)
1265 hapd = wpa_s->ap_iface->bss[0];
1268 if (wpa_s->parent->conf->wps_nfc_dh_pubkey == NULL ||
1269 wpa_s->parent->conf->wps_nfc_dh_privkey == NULL) {
1270 wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
1274 dh5_free(wps->dh_ctx);
1275 wpabuf_free(wps->dh_pubkey);
1276 wpabuf_free(wps->dh_privkey);
1277 wps->dh_privkey = wpabuf_dup(
1278 wpa_s->parent->conf->wps_nfc_dh_privkey);
1279 wps->dh_pubkey = wpabuf_dup(
1280 wpa_s->parent->conf->wps_nfc_dh_pubkey);
1281 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
1283 wpabuf_free(wps->dh_pubkey);
1284 wps->dh_pubkey = NULL;
1285 wpabuf_free(wps->dh_privkey);
1286 wps->dh_privkey = NULL;
1289 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
1290 if (wps->dh_ctx == NULL)
1293 return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
1295 pw ? wpabuf_head(pw) : NULL,
1296 pw ? wpabuf_len(pw) : 0, 1);
1298 #endif /* CONFIG_WPS_NFC */
1301 #ifdef CONFIG_CTRL_IFACE
1302 int wpas_ap_stop_ap(struct wpa_supplicant *wpa_s)
1304 struct hostapd_data *hapd;
1306 if (!wpa_s->ap_iface)
1308 hapd = wpa_s->ap_iface->bss[0];
1309 return hostapd_ctrl_iface_stop_ap(hapd);
1311 #endif /* CONFIG_CTRL_IFACE */
1315 void wpas_event_dfs_radar_detected(struct wpa_supplicant *wpa_s,
1316 struct dfs_event *radar)
1318 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1320 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
1321 hostapd_dfs_radar_detected(wpa_s->ap_iface, radar->freq,
1322 radar->ht_enabled, radar->chan_offset,
1324 radar->cf1, radar->cf2);
1328 void wpas_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
1329 struct dfs_event *radar)
1331 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1333 wpa_printf(MSG_DEBUG, "DFS CAC started on %d MHz", radar->freq);
1334 hostapd_dfs_start_cac(wpa_s->ap_iface, radar->freq,
1335 radar->ht_enabled, radar->chan_offset,
1336 radar->chan_width, radar->cf1, radar->cf2);
1340 void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
1341 struct dfs_event *radar)
1343 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1345 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
1346 hostapd_dfs_complete_cac(wpa_s->ap_iface, 1, radar->freq,
1347 radar->ht_enabled, radar->chan_offset,
1348 radar->chan_width, radar->cf1, radar->cf2);
1352 void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
1353 struct dfs_event *radar)
1355 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1357 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
1358 hostapd_dfs_complete_cac(wpa_s->ap_iface, 0, radar->freq,
1359 radar->ht_enabled, radar->chan_offset,
1360 radar->chan_width, radar->cf1, radar->cf2);
1364 void wpas_event_dfs_cac_nop_finished(struct wpa_supplicant *wpa_s,
1365 struct dfs_event *radar)
1367 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1369 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
1370 hostapd_dfs_nop_finished(wpa_s->ap_iface, radar->freq,
1371 radar->ht_enabled, radar->chan_offset,
1372 radar->chan_width, radar->cf1, radar->cf2);
1374 #endif /* NEED_AP_MLME */
1377 void ap_periodic(struct wpa_supplicant *wpa_s)
1379 if (wpa_s->ap_iface)
1380 hostapd_periodic_iface(wpa_s->ap_iface);