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 int wpa_supplicant_conf_ap_ht(struct wpa_supplicant *wpa_s,
80 struct wpa_ssid *ssid,
81 struct hostapd_config *conf)
83 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
86 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
87 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
92 /* TODO: enable HT40 if driver supports it;
93 * drop to 11b if driver does not support 11g */
95 #ifdef CONFIG_IEEE80211N
97 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
98 * and a mask of allowed capabilities within conf->ht_capab.
99 * Using default config settings for: conf->ht_op_mode_fixed,
100 * conf->secondary_channel, conf->require_ht
102 if (wpa_s->hw.modes) {
103 struct hostapd_hw_modes *mode = NULL;
105 for (i = 0; i < wpa_s->hw.num_modes; i++) {
106 if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
107 mode = &wpa_s->hw.modes[i];
112 #ifdef CONFIG_HT_OVERRIDES
113 if (ssid->disable_ht) {
114 conf->ieee80211n = 0;
118 #endif /* CONFIG_HT_OVERRIDES */
120 if (!no_ht && mode && mode->ht_capab) {
121 conf->ieee80211n = 1;
123 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
125 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
127 conf->secondary_channel =
128 wpas_p2p_get_ht40_mode(wpa_s, mode,
130 if (conf->secondary_channel)
132 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
133 #endif /* CONFIG_P2P */
136 * white-list capabilities that won't cause issues
137 * to connecting stations, while leaving the current
138 * capabilities intact (currently disabled SMPS).
140 conf->ht_capab |= mode->ht_capab &
141 (HT_CAP_INFO_GREEN_FIELD |
142 HT_CAP_INFO_SHORT_GI20MHZ |
143 HT_CAP_INFO_SHORT_GI40MHZ |
144 HT_CAP_INFO_RX_STBC_MASK |
145 HT_CAP_INFO_TX_STBC |
146 HT_CAP_INFO_MAX_AMSDU_SIZE);
148 if (mode->vht_capab && ssid->vht) {
149 conf->ieee80211ac = 1;
150 wpas_conf_ap_vht(wpa_s, conf, mode);
155 if (conf->secondary_channel) {
156 struct wpa_supplicant *iface;
158 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
160 if (iface == wpa_s ||
161 iface->wpa_state < WPA_AUTHENTICATING ||
162 (int) iface->assoc_freq != ssid->frequency)
166 * Do not allow 40 MHz co-ex PRI/SEC switch to force us
167 * to change our PRI channel since we have an existing,
168 * concurrent connection on that channel and doing
169 * multi-channel concurrency is likely to cause more
170 * harm than using different PRI/SEC selection in
171 * environment with multiple BSSes on these two channels
172 * with mixed 20 MHz or PRI channel selection.
174 conf->no_pri_sec_switch = 1;
177 #endif /* CONFIG_IEEE80211N */
183 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
184 struct wpa_ssid *ssid,
185 struct hostapd_config *conf)
187 struct hostapd_bss_config *bss = conf->bss[0];
189 conf->driver = wpa_s->driver;
191 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
193 if (wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf))
196 if (ieee80211_is_dfs(ssid->frequency) && wpa_s->conf->country[0]) {
197 conf->ieee80211h = 1;
198 conf->ieee80211d = 1;
199 conf->country[0] = wpa_s->conf->country[0];
200 conf->country[1] = wpa_s->conf->country[1];
204 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
205 (ssid->mode == WPAS_MODE_P2P_GO ||
206 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
207 /* Remove 802.11b rates from supported and basic rate sets */
208 int *list = os_malloc(4 * sizeof(int));
215 conf->basic_rates = list;
217 list = os_malloc(9 * sizeof(int));
229 conf->supported_rates = list;
232 bss->isolate = !wpa_s->conf->p2p_intra_bss;
233 bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
235 if (ssid->p2p_group) {
236 os_memcpy(bss->ip_addr_go, wpa_s->parent->conf->ip_addr_go, 4);
237 os_memcpy(bss->ip_addr_mask, wpa_s->parent->conf->ip_addr_mask,
239 os_memcpy(bss->ip_addr_start,
240 wpa_s->parent->conf->ip_addr_start, 4);
241 os_memcpy(bss->ip_addr_end, wpa_s->parent->conf->ip_addr_end,
244 #endif /* CONFIG_P2P */
246 if (ssid->ssid_len == 0) {
247 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
250 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
251 bss->ssid.ssid_len = ssid->ssid_len;
252 bss->ssid.ssid_set = 1;
254 bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
257 bss->auth_algs = ssid->auth_alg;
259 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
260 bss->wpa = ssid->proto;
261 bss->wpa_key_mgmt = ssid->key_mgmt;
262 bss->wpa_pairwise = ssid->pairwise_cipher;
264 bin_clear_free(bss->ssid.wpa_psk, sizeof(*bss->ssid.wpa_psk));
265 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
266 if (bss->ssid.wpa_psk == NULL)
268 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
269 bss->ssid.wpa_psk->group = 1;
270 } else if (ssid->passphrase) {
271 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
272 } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
273 ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
274 struct hostapd_wep_keys *wep = &bss->ssid.wep;
276 for (i = 0; i < NUM_WEP_KEYS; i++) {
277 if (ssid->wep_key_len[i] == 0)
279 wep->key[i] = os_malloc(ssid->wep_key_len[i]);
280 if (wep->key[i] == NULL)
282 os_memcpy(wep->key[i], ssid->wep_key[i],
283 ssid->wep_key_len[i]);
284 wep->len[i] = ssid->wep_key_len[i];
286 wep->idx = ssid->wep_tx_keyidx;
290 if (ssid->ap_max_inactivity)
291 bss->ap_max_inactivity = ssid->ap_max_inactivity;
293 if (ssid->dtim_period)
294 bss->dtim_period = ssid->dtim_period;
295 else if (wpa_s->conf->dtim_period)
296 bss->dtim_period = wpa_s->conf->dtim_period;
298 if (ssid->beacon_int)
299 conf->beacon_int = ssid->beacon_int;
300 else if (wpa_s->conf->beacon_int)
301 conf->beacon_int = wpa_s->conf->beacon_int;
304 if (wpa_s->conf->p2p_go_ctwindow > conf->beacon_int) {
306 "CTWindow (%d) is bigger than beacon interval (%d) - avoid configuring it",
307 wpa_s->conf->p2p_go_ctwindow, conf->beacon_int);
308 conf->p2p_go_ctwindow = 0;
310 conf->p2p_go_ctwindow = wpa_s->conf->p2p_go_ctwindow;
312 #endif /* CONFIG_P2P */
314 if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
315 bss->rsn_pairwise = bss->wpa_pairwise;
316 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
319 if (bss->wpa && bss->ieee802_1x)
320 bss->ssid.security_policy = SECURITY_WPA;
322 bss->ssid.security_policy = SECURITY_WPA_PSK;
323 else if (bss->ieee802_1x) {
324 int cipher = WPA_CIPHER_NONE;
325 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
326 bss->ssid.wep.default_len = bss->default_wep_key_len;
327 if (bss->default_wep_key_len)
328 cipher = bss->default_wep_key_len >= 13 ?
329 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
330 bss->wpa_group = cipher;
331 bss->wpa_pairwise = cipher;
332 bss->rsn_pairwise = cipher;
333 } else if (bss->ssid.wep.keys_set) {
334 int cipher = WPA_CIPHER_WEP40;
335 if (bss->ssid.wep.len[0] >= 13)
336 cipher = WPA_CIPHER_WEP104;
337 bss->ssid.security_policy = SECURITY_STATIC_WEP;
338 bss->wpa_group = cipher;
339 bss->wpa_pairwise = cipher;
340 bss->rsn_pairwise = cipher;
342 bss->ssid.security_policy = SECURITY_PLAINTEXT;
343 bss->wpa_group = WPA_CIPHER_NONE;
344 bss->wpa_pairwise = WPA_CIPHER_NONE;
345 bss->rsn_pairwise = WPA_CIPHER_NONE;
348 if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
349 (bss->wpa_group == WPA_CIPHER_CCMP ||
350 bss->wpa_group == WPA_CIPHER_GCMP ||
351 bss->wpa_group == WPA_CIPHER_CCMP_256 ||
352 bss->wpa_group == WPA_CIPHER_GCMP_256)) {
354 * Strong ciphers do not need frequent rekeying, so increase
355 * the default GTK rekeying period to 24 hours.
357 bss->wpa_group_rekey = 86400;
360 #ifdef CONFIG_IEEE80211W
361 if (ssid->ieee80211w != MGMT_FRAME_PROTECTION_DEFAULT)
362 bss->ieee80211w = ssid->ieee80211w;
363 #endif /* CONFIG_IEEE80211W */
367 * Enable WPS by default for open and WPA/WPA2-Personal network, but
368 * require user interaction to actually use it. Only the internal
369 * Registrar is supported.
371 if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
372 bss->ssid.security_policy != SECURITY_PLAINTEXT)
374 if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
375 (!(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ||
377 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
381 if (!ssid->ignore_broadcast_ssid)
384 bss->ap_setup_locked = 2;
385 if (wpa_s->conf->config_methods)
386 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
387 os_memcpy(bss->device_type, wpa_s->conf->device_type,
389 if (wpa_s->conf->device_name) {
390 bss->device_name = os_strdup(wpa_s->conf->device_name);
391 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
393 if (wpa_s->conf->manufacturer)
394 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
395 if (wpa_s->conf->model_name)
396 bss->model_name = os_strdup(wpa_s->conf->model_name);
397 if (wpa_s->conf->model_number)
398 bss->model_number = os_strdup(wpa_s->conf->model_number);
399 if (wpa_s->conf->serial_number)
400 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
401 if (is_nil_uuid(wpa_s->conf->uuid))
402 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
404 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
405 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
406 bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
408 #endif /* CONFIG_WPS */
410 if (wpa_s->max_stations &&
411 wpa_s->max_stations < wpa_s->conf->max_num_sta)
412 bss->max_num_sta = wpa_s->max_stations;
414 bss->max_num_sta = wpa_s->conf->max_num_sta;
416 bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
418 if (wpa_s->conf->ap_vendor_elements) {
419 bss->vendor_elements =
420 wpabuf_dup(wpa_s->conf->ap_vendor_elements);
427 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
430 struct wpa_supplicant *wpa_s = ctx;
431 const struct ieee80211_mgmt *mgmt;
433 mgmt = (const struct ieee80211_mgmt *) buf;
434 if (len < IEEE80211_HDRLEN + 1)
436 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
438 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
439 mgmt->u.action.category,
440 buf + IEEE80211_HDRLEN + 1,
441 len - IEEE80211_HDRLEN - 1, freq);
442 #endif /* CONFIG_P2P */
446 static void ap_wps_event_cb(void *ctx, enum wps_event event,
447 union wps_event_data *data)
450 struct wpa_supplicant *wpa_s = ctx;
452 if (event == WPS_EV_FAIL) {
453 struct wps_event_fail *fail = &data->fail;
455 if (wpa_s->parent && wpa_s->parent != wpa_s &&
456 wpa_s == wpa_s->global->p2p_group_formation) {
458 * src/ap/wps_hostapd.c has already sent this on the
459 * main interface, so only send on the parent interface
462 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
463 "msg=%d config_error=%d",
464 fail->msg, fail->config_error);
466 wpas_p2p_wps_failed(wpa_s, fail);
468 #endif /* CONFIG_P2P */
472 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
473 int authorized, const u8 *p2p_dev_addr)
475 wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
480 static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
481 const u8 *psk, size_t psk_len)
484 struct wpa_supplicant *wpa_s = ctx;
485 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
487 wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
489 #endif /* CONFIG_P2P */
492 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
495 struct wpa_supplicant *wpa_s = ctx;
496 const struct ieee80211_mgmt *mgmt;
498 mgmt = (const struct ieee80211_mgmt *) buf;
499 if (len < IEEE80211_HDRLEN + 1)
501 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
502 mgmt->u.action.category,
503 buf + IEEE80211_HDRLEN + 1,
504 len - IEEE80211_HDRLEN - 1, freq);
505 #endif /* CONFIG_P2P */
510 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
511 const u8 *bssid, const u8 *ie, size_t ie_len,
514 struct wpa_supplicant *wpa_s = ctx;
515 unsigned int freq = 0;
518 freq = wpa_s->ap_iface->freq;
520 return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
525 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
528 struct wpa_supplicant *wpa_s = ctx;
529 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
533 static void wpas_ap_configured_cb(void *ctx)
535 struct wpa_supplicant *wpa_s = ctx;
537 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
539 if (wpa_s->ap_configured_cb)
540 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
541 wpa_s->ap_configured_cb_data);
545 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
546 struct wpa_ssid *ssid)
548 struct wpa_driver_associate_params params;
549 struct hostapd_iface *hapd_iface;
550 struct hostapd_config *conf;
553 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
554 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
558 wpa_supplicant_ap_deinit(wpa_s);
560 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
561 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
563 os_memset(¶ms, 0, sizeof(params));
564 params.ssid = ssid->ssid;
565 params.ssid_len = ssid->ssid_len;
566 switch (ssid->mode) {
568 case WPAS_MODE_P2P_GO:
569 case WPAS_MODE_P2P_GROUP_FORMATION:
570 params.mode = IEEE80211_MODE_AP;
575 if (ssid->frequency == 0)
576 ssid->frequency = 2462; /* default channel 11 */
577 params.freq.freq = ssid->frequency;
579 params.wpa_proto = ssid->proto;
580 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
581 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
583 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
584 params.key_mgmt_suite = wpa_s->key_mgmt;
586 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
588 if (wpa_s->pairwise_cipher < 0) {
589 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
593 params.pairwise_suite = wpa_s->pairwise_cipher;
594 params.group_suite = params.pairwise_suite;
597 if (ssid->mode == WPAS_MODE_P2P_GO ||
598 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
600 #endif /* CONFIG_P2P */
602 if (wpa_s->parent->set_ap_uapsd)
603 params.uapsd = wpa_s->parent->ap_uapsd;
604 else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
605 params.uapsd = 1; /* mandatory for P2P GO */
609 if (ieee80211_is_dfs(params.freq.freq))
610 params.freq.freq = 0; /* set channel after CAC */
612 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
613 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
617 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
618 if (hapd_iface == NULL)
620 hapd_iface->owner = wpa_s;
621 hapd_iface->drv_flags = wpa_s->drv_flags;
622 hapd_iface->smps_modes = wpa_s->drv_smps_modes;
623 hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
624 hapd_iface->extended_capa = wpa_s->extended_capa;
625 hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
626 hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
628 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
630 wpa_supplicant_ap_deinit(wpa_s);
634 os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
635 wpa_s->conf->wmm_ac_params,
636 sizeof(wpa_s->conf->wmm_ac_params));
638 if (params.uapsd > 0) {
639 conf->bss[0]->wmm_enabled = 1;
640 conf->bss[0]->wmm_uapsd = 1;
643 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
644 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
645 wpa_supplicant_ap_deinit(wpa_s);
650 if (ssid->mode == WPAS_MODE_P2P_GO)
651 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
652 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
653 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
655 #endif /* CONFIG_P2P */
657 hapd_iface->num_bss = conf->num_bss;
658 hapd_iface->bss = os_calloc(conf->num_bss,
659 sizeof(struct hostapd_data *));
660 if (hapd_iface->bss == NULL) {
661 wpa_supplicant_ap_deinit(wpa_s);
665 for (i = 0; i < conf->num_bss; i++) {
667 hostapd_alloc_bss_data(hapd_iface, conf,
669 if (hapd_iface->bss[i] == NULL) {
670 wpa_supplicant_ap_deinit(wpa_s);
674 hapd_iface->bss[i]->msg_ctx = wpa_s;
675 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
676 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
677 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
678 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
679 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
680 hostapd_register_probereq_cb(hapd_iface->bss[i],
681 ap_probe_req_rx, wpa_s);
682 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
683 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
684 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
685 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
686 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
687 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
689 hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
690 hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
691 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
692 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
694 #endif /* CONFIG_P2P */
695 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
696 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
697 #ifdef CONFIG_TESTING_OPTIONS
698 hapd_iface->bss[i]->ext_eapol_frame_io =
699 wpa_s->ext_eapol_frame_io;
700 #endif /* CONFIG_TESTING_OPTIONS */
703 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
704 hapd_iface->bss[0]->driver = wpa_s->driver;
705 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
707 wpa_s->current_ssid = ssid;
708 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
709 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
710 wpa_s->assoc_freq = ssid->frequency;
712 if (hostapd_setup_interface(wpa_s->ap_iface)) {
713 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
714 wpa_supplicant_ap_deinit(wpa_s);
722 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
725 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
726 #endif /* CONFIG_WPS */
728 if (wpa_s->ap_iface == NULL)
731 wpa_s->current_ssid = NULL;
732 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
733 wpa_s->assoc_freq = 0;
734 wpas_p2p_ap_deinit(wpa_s);
735 wpa_s->ap_iface->driver_ap_teardown =
736 !!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
738 hostapd_interface_deinit(wpa_s->ap_iface);
739 hostapd_interface_free(wpa_s->ap_iface);
740 wpa_s->ap_iface = NULL;
741 wpa_drv_deinit_ap(wpa_s);
742 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
743 " reason=%d locally_generated=1",
744 MAC2STR(wpa_s->own_addr), WLAN_REASON_DEAUTH_LEAVING);
748 void ap_tx_status(void *ctx, const u8 *addr,
749 const u8 *buf, size_t len, int ack)
752 struct wpa_supplicant *wpa_s = ctx;
753 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
754 #endif /* NEED_AP_MLME */
758 void ap_eapol_tx_status(void *ctx, const u8 *dst,
759 const u8 *data, size_t len, int ack)
762 struct wpa_supplicant *wpa_s = ctx;
763 if (!wpa_s->ap_iface)
765 hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
766 #endif /* NEED_AP_MLME */
770 void ap_client_poll_ok(void *ctx, const u8 *addr)
773 struct wpa_supplicant *wpa_s = ctx;
775 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
776 #endif /* NEED_AP_MLME */
780 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
783 struct wpa_supplicant *wpa_s = ctx;
784 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
785 #endif /* NEED_AP_MLME */
789 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
792 struct wpa_supplicant *wpa_s = ctx;
793 struct hostapd_frame_info fi;
794 os_memset(&fi, 0, sizeof(fi));
795 fi.datarate = rx_mgmt->datarate;
796 fi.ssi_signal = rx_mgmt->ssi_signal;
797 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
798 rx_mgmt->frame_len, &fi);
799 #endif /* NEED_AP_MLME */
803 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
806 struct wpa_supplicant *wpa_s = ctx;
807 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
808 #endif /* NEED_AP_MLME */
812 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
813 const u8 *src_addr, const u8 *buf, size_t len)
815 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
821 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
822 const u8 *p2p_dev_addr)
824 if (!wpa_s->ap_iface)
826 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
831 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
833 struct wps_registrar *reg;
834 int reg_sel = 0, wps_sta = 0;
836 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
839 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
840 reg_sel = wps_registrar_wps_cancel(reg);
841 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
842 ap_sta_wps_cancel, NULL);
844 if (!reg_sel && !wps_sta) {
845 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
851 * There are 2 cases to return wps cancel as success:
852 * 1. When wps cancel was initiated but no connection has been
853 * established with client yet.
854 * 2. Client is in the middle of exchanging WPS messages.
861 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
862 const char *pin, char *buf, size_t buflen,
865 int ret, ret_len = 0;
867 if (!wpa_s->ap_iface)
871 unsigned int rpin = wps_generate_pin();
872 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
873 if (os_snprintf_error(buflen, ret_len))
877 ret_len = os_snprintf(buf, buflen, "%s", pin);
878 if (os_snprintf_error(buflen, ret_len))
882 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
890 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
892 struct wpa_supplicant *wpa_s = eloop_data;
893 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
894 wpas_wps_ap_pin_disable(wpa_s);
898 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
900 struct hostapd_data *hapd;
902 if (wpa_s->ap_iface == NULL)
904 hapd = wpa_s->ap_iface->bss[0];
905 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
906 hapd->ap_pin_failures = 0;
907 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
909 eloop_register_timeout(timeout, 0,
910 wpas_wps_ap_pin_timeout, wpa_s, NULL);
914 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
916 struct hostapd_data *hapd;
918 if (wpa_s->ap_iface == NULL)
920 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
921 hapd = wpa_s->ap_iface->bss[0];
922 os_free(hapd->conf->ap_pin);
923 hapd->conf->ap_pin = NULL;
924 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
928 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
930 struct hostapd_data *hapd;
934 if (wpa_s->ap_iface == NULL)
936 hapd = wpa_s->ap_iface->bss[0];
937 pin = wps_generate_pin();
938 os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
939 os_free(hapd->conf->ap_pin);
940 hapd->conf->ap_pin = os_strdup(pin_txt);
941 if (hapd->conf->ap_pin == NULL)
943 wpas_wps_ap_pin_enable(wpa_s, timeout);
945 return hapd->conf->ap_pin;
949 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
951 struct hostapd_data *hapd;
952 if (wpa_s->ap_iface == NULL)
954 hapd = wpa_s->ap_iface->bss[0];
955 return hapd->conf->ap_pin;
959 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
962 struct hostapd_data *hapd;
966 if (wpa_s->ap_iface == NULL)
968 hapd = wpa_s->ap_iface->bss[0];
969 ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
970 if (os_snprintf_error(sizeof(pin_txt), ret))
972 os_free(hapd->conf->ap_pin);
973 hapd->conf->ap_pin = os_strdup(pin_txt);
974 if (hapd->conf->ap_pin == NULL)
976 wpas_wps_ap_pin_enable(wpa_s, timeout);
982 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
984 struct hostapd_data *hapd;
986 if (wpa_s->ap_iface == NULL)
988 hapd = wpa_s->ap_iface->bss[0];
991 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
992 * PIN if this happens multiple times to slow down brute force attacks.
994 hapd->ap_pin_failures++;
995 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
996 hapd->ap_pin_failures);
997 if (hapd->ap_pin_failures < 3)
1000 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
1001 hapd->ap_pin_failures = 0;
1002 os_free(hapd->conf->ap_pin);
1003 hapd->conf->ap_pin = NULL;
1007 #ifdef CONFIG_WPS_NFC
1009 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
1012 struct hostapd_data *hapd;
1014 if (wpa_s->ap_iface == NULL)
1016 hapd = wpa_s->ap_iface->bss[0];
1017 return hostapd_wps_nfc_config_token(hapd, ndef);
1021 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
1024 struct hostapd_data *hapd;
1026 if (wpa_s->ap_iface == NULL)
1028 hapd = wpa_s->ap_iface->bss[0];
1029 return hostapd_wps_nfc_hs_cr(hapd, ndef);
1033 int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
1034 const struct wpabuf *req,
1035 const struct wpabuf *sel)
1037 struct hostapd_data *hapd;
1039 if (wpa_s->ap_iface == NULL)
1041 hapd = wpa_s->ap_iface->bss[0];
1042 return hostapd_wps_nfc_report_handover(hapd, req, sel);
1045 #endif /* CONFIG_WPS_NFC */
1047 #endif /* CONFIG_WPS */
1050 #ifdef CONFIG_CTRL_IFACE
1052 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
1053 char *buf, size_t buflen)
1055 struct hostapd_data *hapd;
1057 if (wpa_s->ap_iface)
1058 hapd = wpa_s->ap_iface->bss[0];
1059 else if (wpa_s->ifmsh)
1060 hapd = wpa_s->ifmsh->bss[0];
1063 return hostapd_ctrl_iface_sta_first(hapd, buf, buflen);
1067 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
1068 char *buf, size_t buflen)
1070 struct hostapd_data *hapd;
1072 if (wpa_s->ap_iface)
1073 hapd = wpa_s->ap_iface->bss[0];
1074 else if (wpa_s->ifmsh)
1075 hapd = wpa_s->ifmsh->bss[0];
1078 return hostapd_ctrl_iface_sta(hapd, txtaddr, buf, buflen);
1082 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
1083 char *buf, size_t buflen)
1085 struct hostapd_data *hapd;
1087 if (wpa_s->ap_iface)
1088 hapd = wpa_s->ap_iface->bss[0];
1089 else if (wpa_s->ifmsh)
1090 hapd = wpa_s->ifmsh->bss[0];
1093 return hostapd_ctrl_iface_sta_next(hapd, txtaddr, buf, buflen);
1097 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
1098 const char *txtaddr)
1100 if (wpa_s->ap_iface == NULL)
1102 return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
1107 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
1108 const char *txtaddr)
1110 if (wpa_s->ap_iface == NULL)
1112 return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
1117 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
1118 size_t buflen, int verbose)
1120 char *pos = buf, *end = buf + buflen;
1122 struct hostapd_bss_config *conf;
1124 if (wpa_s->ap_iface == NULL)
1127 conf = wpa_s->ap_iface->bss[0]->conf;
1131 ret = os_snprintf(pos, end - pos,
1132 "pairwise_cipher=%s\n"
1135 wpa_cipher_txt(conf->rsn_pairwise),
1136 wpa_cipher_txt(conf->wpa_group),
1137 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
1139 if (os_snprintf_error(end - pos, ret))
1145 #endif /* CONFIG_CTRL_IFACE */
1148 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1150 struct hostapd_iface *iface = wpa_s->ap_iface;
1151 struct wpa_ssid *ssid = wpa_s->current_ssid;
1152 struct hostapd_data *hapd;
1154 if (ssid == NULL || wpa_s->ap_iface == NULL ||
1155 ssid->mode == WPAS_MODE_INFRA ||
1156 ssid->mode == WPAS_MODE_IBSS)
1160 if (ssid->mode == WPAS_MODE_P2P_GO)
1161 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
1162 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1163 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
1164 P2P_GROUP_FORMATION;
1165 #endif /* CONFIG_P2P */
1167 hapd = iface->bss[0];
1168 if (hapd->drv_priv == NULL)
1170 ieee802_11_set_beacons(iface);
1171 hostapd_set_ap_wps_ie(hapd);
1177 int ap_switch_channel(struct wpa_supplicant *wpa_s,
1178 struct csa_settings *settings)
1181 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1184 return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
1185 #else /* NEED_AP_MLME */
1187 #endif /* NEED_AP_MLME */
1191 #ifdef CONFIG_CTRL_IFACE
1192 int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
1194 struct csa_settings settings;
1195 int ret = hostapd_parse_csa_settings(pos, &settings);
1200 return ap_switch_channel(wpa_s, &settings);
1202 #endif /* CONFIG_CTRL_IFACE */
1205 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1206 int offset, int width, int cf1, int cf2)
1208 if (!wpa_s->ap_iface)
1211 wpa_s->assoc_freq = freq;
1212 if (wpa_s->current_ssid)
1213 wpa_s->current_ssid->frequency = freq;
1214 hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht,
1215 offset, width, cf1, cf2);
1219 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1222 struct hostapd_data *hapd;
1223 struct hostapd_bss_config *conf;
1225 if (!wpa_s->ap_iface)
1229 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1232 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1234 hapd = wpa_s->ap_iface->bss[0];
1237 os_free(conf->accept_mac);
1238 conf->accept_mac = NULL;
1239 conf->num_accept_mac = 0;
1240 os_free(conf->deny_mac);
1241 conf->deny_mac = NULL;
1242 conf->num_deny_mac = 0;
1245 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1249 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1250 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1251 if (conf->accept_mac == NULL)
1253 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1254 conf->num_accept_mac = 1;
1260 #ifdef CONFIG_WPS_NFC
1261 int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
1262 const struct wpabuf *pw, const u8 *pubkey_hash)
1264 struct hostapd_data *hapd;
1265 struct wps_context *wps;
1267 if (!wpa_s->ap_iface)
1269 hapd = wpa_s->ap_iface->bss[0];
1272 if (wpa_s->parent->conf->wps_nfc_dh_pubkey == NULL ||
1273 wpa_s->parent->conf->wps_nfc_dh_privkey == NULL) {
1274 wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
1278 dh5_free(wps->dh_ctx);
1279 wpabuf_free(wps->dh_pubkey);
1280 wpabuf_free(wps->dh_privkey);
1281 wps->dh_privkey = wpabuf_dup(
1282 wpa_s->parent->conf->wps_nfc_dh_privkey);
1283 wps->dh_pubkey = wpabuf_dup(
1284 wpa_s->parent->conf->wps_nfc_dh_pubkey);
1285 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
1287 wpabuf_free(wps->dh_pubkey);
1288 wps->dh_pubkey = NULL;
1289 wpabuf_free(wps->dh_privkey);
1290 wps->dh_privkey = NULL;
1293 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
1294 if (wps->dh_ctx == NULL)
1297 return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
1299 pw ? wpabuf_head(pw) : NULL,
1300 pw ? wpabuf_len(pw) : 0, 1);
1302 #endif /* CONFIG_WPS_NFC */
1305 #ifdef CONFIG_CTRL_IFACE
1306 int wpas_ap_stop_ap(struct wpa_supplicant *wpa_s)
1308 struct hostapd_data *hapd;
1310 if (!wpa_s->ap_iface)
1312 hapd = wpa_s->ap_iface->bss[0];
1313 return hostapd_ctrl_iface_stop_ap(hapd);
1315 #endif /* CONFIG_CTRL_IFACE */
1319 void wpas_event_dfs_radar_detected(struct wpa_supplicant *wpa_s,
1320 struct dfs_event *radar)
1322 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1324 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
1325 hostapd_dfs_radar_detected(wpa_s->ap_iface, radar->freq,
1326 radar->ht_enabled, radar->chan_offset,
1328 radar->cf1, radar->cf2);
1332 void wpas_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
1333 struct dfs_event *radar)
1335 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1337 wpa_printf(MSG_DEBUG, "DFS CAC started on %d MHz", radar->freq);
1338 hostapd_dfs_start_cac(wpa_s->ap_iface, radar->freq,
1339 radar->ht_enabled, radar->chan_offset,
1340 radar->chan_width, radar->cf1, radar->cf2);
1344 void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
1345 struct dfs_event *radar)
1347 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1349 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
1350 hostapd_dfs_complete_cac(wpa_s->ap_iface, 1, radar->freq,
1351 radar->ht_enabled, radar->chan_offset,
1352 radar->chan_width, radar->cf1, radar->cf2);
1356 void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
1357 struct dfs_event *radar)
1359 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1361 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
1362 hostapd_dfs_complete_cac(wpa_s->ap_iface, 0, radar->freq,
1363 radar->ht_enabled, radar->chan_offset,
1364 radar->chan_width, radar->cf1, radar->cf2);
1368 void wpas_event_dfs_cac_nop_finished(struct wpa_supplicant *wpa_s,
1369 struct dfs_event *radar)
1371 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1373 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
1374 hostapd_dfs_nop_finished(wpa_s->ap_iface, radar->freq,
1375 radar->ht_enabled, radar->chan_offset,
1376 radar->chan_width, radar->cf1, radar->cf2);
1378 #endif /* NEED_AP_MLME */
1381 void ap_periodic(struct wpa_supplicant *wpa_s)
1383 if (wpa_s->ap_iface)
1384 hostapd_periodic_iface(wpa_s->ap_iface);