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"
30 #include "common/ieee802_11_defs.h"
31 #include "config_ssid.h"
33 #include "wpa_supplicant_i.h"
35 #include "p2p_supplicant.h"
37 #include "ap/sta_info.h"
42 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
43 #endif /* CONFIG_WPS */
46 #ifdef CONFIG_IEEE80211N
47 static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
48 struct hostapd_config *conf,
49 struct hostapd_hw_modes *mode)
53 u8 channel = conf->channel;
55 if (!conf->secondary_channel)
58 center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
62 /* Use 80 MHz channel */
63 conf->vht_oper_chwidth = 1;
64 conf->vht_oper_centr_freq_seg0_idx = center_chan;
68 conf->vht_oper_centr_freq_seg0_idx =
69 channel + conf->secondary_channel * 2;
70 #else /* CONFIG_P2P */
71 conf->vht_oper_centr_freq_seg0_idx =
72 conf->channel + conf->secondary_channel * 2;
73 #endif /* CONFIG_P2P */
75 #endif /* CONFIG_IEEE80211N */
78 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
79 struct wpa_ssid *ssid,
80 struct hostapd_config *conf)
82 struct hostapd_bss_config *bss = conf->bss[0];
84 conf->driver = wpa_s->driver;
86 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
88 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
90 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
91 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
96 /* TODO: enable HT40 if driver supports it;
97 * drop to 11b if driver does not support 11g */
99 #ifdef CONFIG_IEEE80211N
101 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
102 * and a mask of allowed capabilities within conf->ht_capab.
103 * Using default config settings for: conf->ht_op_mode_fixed,
104 * conf->secondary_channel, conf->require_ht
106 if (wpa_s->hw.modes) {
107 struct hostapd_hw_modes *mode = NULL;
109 for (i = 0; i < wpa_s->hw.num_modes; i++) {
110 if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
111 mode = &wpa_s->hw.modes[i];
116 #ifdef CONFIG_HT_OVERRIDES
117 if (ssid->disable_ht) {
118 conf->ieee80211n = 0;
122 #endif /* CONFIG_HT_OVERRIDES */
124 if (!no_ht && mode && mode->ht_capab) {
125 conf->ieee80211n = 1;
127 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
129 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
131 conf->secondary_channel =
132 wpas_p2p_get_ht40_mode(wpa_s, mode,
134 if (conf->secondary_channel)
136 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
137 #endif /* CONFIG_P2P */
140 * white-list capabilities that won't cause issues
141 * to connecting stations, while leaving the current
142 * capabilities intact (currently disabled SMPS).
144 conf->ht_capab |= mode->ht_capab &
145 (HT_CAP_INFO_GREEN_FIELD |
146 HT_CAP_INFO_SHORT_GI20MHZ |
147 HT_CAP_INFO_SHORT_GI40MHZ |
148 HT_CAP_INFO_RX_STBC_MASK |
149 HT_CAP_INFO_MAX_AMSDU_SIZE);
151 if (mode->vht_capab && ssid->vht) {
152 conf->ieee80211ac = 1;
153 wpas_conf_ap_vht(wpa_s, conf, mode);
157 #endif /* CONFIG_IEEE80211N */
160 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
161 (ssid->mode == WPAS_MODE_P2P_GO ||
162 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
163 /* Remove 802.11b rates from supported and basic rate sets */
164 int *list = os_malloc(4 * sizeof(int));
171 conf->basic_rates = list;
173 list = os_malloc(9 * sizeof(int));
185 conf->supported_rates = list;
188 bss->isolate = !wpa_s->conf->p2p_intra_bss;
189 bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
191 if (ssid->p2p_group) {
192 os_memcpy(bss->ip_addr_go, wpa_s->parent->conf->ip_addr_go, 4);
193 os_memcpy(bss->ip_addr_mask, wpa_s->parent->conf->ip_addr_mask,
195 os_memcpy(bss->ip_addr_start,
196 wpa_s->parent->conf->ip_addr_start, 4);
197 os_memcpy(bss->ip_addr_end, wpa_s->parent->conf->ip_addr_end,
200 #endif /* CONFIG_P2P */
202 if (ssid->ssid_len == 0) {
203 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
206 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
207 bss->ssid.ssid_len = ssid->ssid_len;
208 bss->ssid.ssid_set = 1;
210 bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
213 bss->auth_algs = ssid->auth_alg;
215 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
216 bss->wpa = ssid->proto;
217 bss->wpa_key_mgmt = ssid->key_mgmt;
218 bss->wpa_pairwise = ssid->pairwise_cipher;
220 os_free(bss->ssid.wpa_psk);
221 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
222 if (bss->ssid.wpa_psk == NULL)
224 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
225 bss->ssid.wpa_psk->group = 1;
226 } else if (ssid->passphrase) {
227 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
228 } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
229 ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
230 struct hostapd_wep_keys *wep = &bss->ssid.wep;
232 for (i = 0; i < NUM_WEP_KEYS; i++) {
233 if (ssid->wep_key_len[i] == 0)
235 wep->key[i] = os_malloc(ssid->wep_key_len[i]);
236 if (wep->key[i] == NULL)
238 os_memcpy(wep->key[i], ssid->wep_key[i],
239 ssid->wep_key_len[i]);
240 wep->len[i] = ssid->wep_key_len[i];
242 wep->idx = ssid->wep_tx_keyidx;
246 if (ssid->ap_max_inactivity)
247 bss->ap_max_inactivity = ssid->ap_max_inactivity;
249 if (ssid->dtim_period)
250 bss->dtim_period = ssid->dtim_period;
251 else if (wpa_s->conf->dtim_period)
252 bss->dtim_period = wpa_s->conf->dtim_period;
254 if (ssid->beacon_int)
255 conf->beacon_int = ssid->beacon_int;
256 else if (wpa_s->conf->beacon_int)
257 conf->beacon_int = wpa_s->conf->beacon_int;
259 if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
260 bss->rsn_pairwise = bss->wpa_pairwise;
261 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
264 if (bss->wpa && bss->ieee802_1x)
265 bss->ssid.security_policy = SECURITY_WPA;
267 bss->ssid.security_policy = SECURITY_WPA_PSK;
268 else if (bss->ieee802_1x) {
269 int cipher = WPA_CIPHER_NONE;
270 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
271 bss->ssid.wep.default_len = bss->default_wep_key_len;
272 if (bss->default_wep_key_len)
273 cipher = bss->default_wep_key_len >= 13 ?
274 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
275 bss->wpa_group = cipher;
276 bss->wpa_pairwise = cipher;
277 bss->rsn_pairwise = cipher;
278 } else if (bss->ssid.wep.keys_set) {
279 int cipher = WPA_CIPHER_WEP40;
280 if (bss->ssid.wep.len[0] >= 13)
281 cipher = WPA_CIPHER_WEP104;
282 bss->ssid.security_policy = SECURITY_STATIC_WEP;
283 bss->wpa_group = cipher;
284 bss->wpa_pairwise = cipher;
285 bss->rsn_pairwise = cipher;
287 bss->ssid.security_policy = SECURITY_PLAINTEXT;
288 bss->wpa_group = WPA_CIPHER_NONE;
289 bss->wpa_pairwise = WPA_CIPHER_NONE;
290 bss->rsn_pairwise = WPA_CIPHER_NONE;
293 if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
294 (bss->wpa_group == WPA_CIPHER_CCMP ||
295 bss->wpa_group == WPA_CIPHER_GCMP ||
296 bss->wpa_group == WPA_CIPHER_CCMP_256 ||
297 bss->wpa_group == WPA_CIPHER_GCMP_256)) {
299 * Strong ciphers do not need frequent rekeying, so increase
300 * the default GTK rekeying period to 24 hours.
302 bss->wpa_group_rekey = 86400;
305 #ifdef CONFIG_IEEE80211W
306 if (ssid->ieee80211w != MGMT_FRAME_PROTECTION_DEFAULT)
307 bss->ieee80211w = ssid->ieee80211w;
308 #endif /* CONFIG_IEEE80211W */
312 * Enable WPS by default for open and WPA/WPA2-Personal network, but
313 * require user interaction to actually use it. Only the internal
314 * Registrar is supported.
316 if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
317 bss->ssid.security_policy != SECURITY_PLAINTEXT)
319 if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
320 (!(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ||
322 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
326 if (!ssid->ignore_broadcast_ssid)
329 bss->ap_setup_locked = 2;
330 if (wpa_s->conf->config_methods)
331 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
332 os_memcpy(bss->device_type, wpa_s->conf->device_type,
334 if (wpa_s->conf->device_name) {
335 bss->device_name = os_strdup(wpa_s->conf->device_name);
336 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
338 if (wpa_s->conf->manufacturer)
339 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
340 if (wpa_s->conf->model_name)
341 bss->model_name = os_strdup(wpa_s->conf->model_name);
342 if (wpa_s->conf->model_number)
343 bss->model_number = os_strdup(wpa_s->conf->model_number);
344 if (wpa_s->conf->serial_number)
345 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
346 if (is_nil_uuid(wpa_s->conf->uuid))
347 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
349 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
350 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
351 bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
353 #endif /* CONFIG_WPS */
355 if (wpa_s->max_stations &&
356 wpa_s->max_stations < wpa_s->conf->max_num_sta)
357 bss->max_num_sta = wpa_s->max_stations;
359 bss->max_num_sta = wpa_s->conf->max_num_sta;
361 bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
363 if (wpa_s->conf->ap_vendor_elements) {
364 bss->vendor_elements =
365 wpabuf_dup(wpa_s->conf->ap_vendor_elements);
372 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
375 struct wpa_supplicant *wpa_s = ctx;
376 const struct ieee80211_mgmt *mgmt;
378 mgmt = (const struct ieee80211_mgmt *) buf;
379 if (len < IEEE80211_HDRLEN + 1)
381 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
383 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
384 mgmt->u.action.category,
385 buf + IEEE80211_HDRLEN + 1,
386 len - IEEE80211_HDRLEN - 1, freq);
387 #endif /* CONFIG_P2P */
391 static void ap_wps_event_cb(void *ctx, enum wps_event event,
392 union wps_event_data *data)
395 struct wpa_supplicant *wpa_s = ctx;
397 if (event == WPS_EV_FAIL) {
398 struct wps_event_fail *fail = &data->fail;
400 if (wpa_s->parent && wpa_s->parent != wpa_s &&
401 wpa_s == wpa_s->global->p2p_group_formation) {
403 * src/ap/wps_hostapd.c has already sent this on the
404 * main interface, so only send on the parent interface
407 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
408 "msg=%d config_error=%d",
409 fail->msg, fail->config_error);
411 wpas_p2p_wps_failed(wpa_s, fail);
413 #endif /* CONFIG_P2P */
417 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
418 int authorized, const u8 *p2p_dev_addr)
420 wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
425 static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
426 const u8 *psk, size_t psk_len)
429 struct wpa_supplicant *wpa_s = ctx;
430 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
432 wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
434 #endif /* CONFIG_P2P */
437 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
440 struct wpa_supplicant *wpa_s = ctx;
441 const struct ieee80211_mgmt *mgmt;
443 mgmt = (const struct ieee80211_mgmt *) buf;
444 if (len < IEEE80211_HDRLEN + 1)
446 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
447 mgmt->u.action.category,
448 buf + IEEE80211_HDRLEN + 1,
449 len - IEEE80211_HDRLEN - 1, freq);
450 #endif /* CONFIG_P2P */
455 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
456 const u8 *bssid, const u8 *ie, size_t ie_len,
459 struct wpa_supplicant *wpa_s = ctx;
460 return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
465 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
468 struct wpa_supplicant *wpa_s = ctx;
469 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
473 static void wpas_ap_configured_cb(void *ctx)
475 struct wpa_supplicant *wpa_s = ctx;
477 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
479 if (wpa_s->ap_configured_cb)
480 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
481 wpa_s->ap_configured_cb_data);
485 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
486 struct wpa_ssid *ssid)
488 struct wpa_driver_associate_params params;
489 struct hostapd_iface *hapd_iface;
490 struct hostapd_config *conf;
493 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
494 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
498 wpa_supplicant_ap_deinit(wpa_s);
500 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
501 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
503 os_memset(¶ms, 0, sizeof(params));
504 params.ssid = ssid->ssid;
505 params.ssid_len = ssid->ssid_len;
506 switch (ssid->mode) {
508 case WPAS_MODE_P2P_GO:
509 case WPAS_MODE_P2P_GROUP_FORMATION:
510 params.mode = IEEE80211_MODE_AP;
515 if (ssid->frequency == 0)
516 ssid->frequency = 2462; /* default channel 11 */
517 params.freq.freq = ssid->frequency;
519 params.wpa_proto = ssid->proto;
520 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
521 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
523 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
524 params.key_mgmt_suite = wpa_s->key_mgmt;
526 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
528 if (wpa_s->pairwise_cipher < 0) {
529 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
533 params.pairwise_suite = wpa_s->pairwise_cipher;
534 params.group_suite = params.pairwise_suite;
537 if (ssid->mode == WPAS_MODE_P2P_GO ||
538 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
540 #endif /* CONFIG_P2P */
542 if (wpa_s->parent->set_ap_uapsd)
543 params.uapsd = wpa_s->parent->ap_uapsd;
544 else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
545 params.uapsd = 1; /* mandatory for P2P GO */
549 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
550 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
554 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
555 if (hapd_iface == NULL)
557 hapd_iface->owner = wpa_s;
558 hapd_iface->drv_flags = wpa_s->drv_flags;
559 hapd_iface->smps_modes = wpa_s->drv_smps_modes;
560 hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
561 hapd_iface->extended_capa = wpa_s->extended_capa;
562 hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
563 hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
565 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
567 wpa_supplicant_ap_deinit(wpa_s);
571 os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
572 wpa_s->conf->wmm_ac_params,
573 sizeof(wpa_s->conf->wmm_ac_params));
575 if (params.uapsd > 0) {
576 conf->bss[0]->wmm_enabled = 1;
577 conf->bss[0]->wmm_uapsd = 1;
580 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
581 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
582 wpa_supplicant_ap_deinit(wpa_s);
587 if (ssid->mode == WPAS_MODE_P2P_GO)
588 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
589 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
590 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
592 #endif /* CONFIG_P2P */
594 hapd_iface->num_bss = conf->num_bss;
595 hapd_iface->bss = os_calloc(conf->num_bss,
596 sizeof(struct hostapd_data *));
597 if (hapd_iface->bss == NULL) {
598 wpa_supplicant_ap_deinit(wpa_s);
602 for (i = 0; i < conf->num_bss; i++) {
604 hostapd_alloc_bss_data(hapd_iface, conf,
606 if (hapd_iface->bss[i] == NULL) {
607 wpa_supplicant_ap_deinit(wpa_s);
611 hapd_iface->bss[i]->msg_ctx = wpa_s;
612 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
613 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
614 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
615 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
616 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
617 hostapd_register_probereq_cb(hapd_iface->bss[i],
618 ap_probe_req_rx, wpa_s);
619 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
620 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
621 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
622 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
623 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
624 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
626 hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
627 hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
628 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
629 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
631 #endif /* CONFIG_P2P */
632 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
633 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
634 #ifdef CONFIG_TESTING_OPTIONS
635 hapd_iface->bss[i]->ext_eapol_frame_io =
636 wpa_s->ext_eapol_frame_io;
637 #endif /* CONFIG_TESTING_OPTIONS */
640 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
641 hapd_iface->bss[0]->driver = wpa_s->driver;
642 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
644 wpa_s->current_ssid = ssid;
645 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
646 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
647 wpa_s->assoc_freq = ssid->frequency;
649 if (hostapd_setup_interface(wpa_s->ap_iface)) {
650 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
651 wpa_supplicant_ap_deinit(wpa_s);
659 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
662 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
663 #endif /* CONFIG_WPS */
665 if (wpa_s->ap_iface == NULL)
668 wpa_s->current_ssid = NULL;
669 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
670 wpa_s->assoc_freq = 0;
671 wpas_p2p_ap_deinit(wpa_s);
672 wpa_s->ap_iface->driver_ap_teardown =
673 !!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
675 hostapd_interface_deinit(wpa_s->ap_iface);
676 hostapd_interface_free(wpa_s->ap_iface);
677 wpa_s->ap_iface = NULL;
678 wpa_drv_deinit_ap(wpa_s);
682 void ap_tx_status(void *ctx, const u8 *addr,
683 const u8 *buf, size_t len, int ack)
686 struct wpa_supplicant *wpa_s = ctx;
687 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
688 #endif /* NEED_AP_MLME */
692 void ap_eapol_tx_status(void *ctx, const u8 *dst,
693 const u8 *data, size_t len, int ack)
696 struct wpa_supplicant *wpa_s = ctx;
697 if (!wpa_s->ap_iface)
699 hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
700 #endif /* NEED_AP_MLME */
704 void ap_client_poll_ok(void *ctx, const u8 *addr)
707 struct wpa_supplicant *wpa_s = ctx;
709 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
710 #endif /* NEED_AP_MLME */
714 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
717 struct wpa_supplicant *wpa_s = ctx;
718 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
719 #endif /* NEED_AP_MLME */
723 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
726 struct wpa_supplicant *wpa_s = ctx;
727 struct hostapd_frame_info fi;
728 os_memset(&fi, 0, sizeof(fi));
729 fi.datarate = rx_mgmt->datarate;
730 fi.ssi_signal = rx_mgmt->ssi_signal;
731 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
732 rx_mgmt->frame_len, &fi);
733 #endif /* NEED_AP_MLME */
737 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
740 struct wpa_supplicant *wpa_s = ctx;
741 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
742 #endif /* NEED_AP_MLME */
746 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
747 const u8 *src_addr, const u8 *buf, size_t len)
749 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
755 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
756 const u8 *p2p_dev_addr)
758 if (!wpa_s->ap_iface)
760 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
765 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
767 struct wps_registrar *reg;
768 int reg_sel = 0, wps_sta = 0;
770 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
773 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
774 reg_sel = wps_registrar_wps_cancel(reg);
775 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
776 ap_sta_wps_cancel, NULL);
778 if (!reg_sel && !wps_sta) {
779 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
785 * There are 2 cases to return wps cancel as success:
786 * 1. When wps cancel was initiated but no connection has been
787 * established with client yet.
788 * 2. Client is in the middle of exchanging WPS messages.
795 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
796 const char *pin, char *buf, size_t buflen,
799 int ret, ret_len = 0;
801 if (!wpa_s->ap_iface)
805 unsigned int rpin = wps_generate_pin();
806 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
809 ret_len = os_snprintf(buf, buflen, "%s", pin);
811 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
819 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
821 struct wpa_supplicant *wpa_s = eloop_data;
822 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
823 wpas_wps_ap_pin_disable(wpa_s);
827 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
829 struct hostapd_data *hapd;
831 if (wpa_s->ap_iface == NULL)
833 hapd = wpa_s->ap_iface->bss[0];
834 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
835 hapd->ap_pin_failures = 0;
836 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
838 eloop_register_timeout(timeout, 0,
839 wpas_wps_ap_pin_timeout, wpa_s, NULL);
843 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
845 struct hostapd_data *hapd;
847 if (wpa_s->ap_iface == NULL)
849 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
850 hapd = wpa_s->ap_iface->bss[0];
851 os_free(hapd->conf->ap_pin);
852 hapd->conf->ap_pin = NULL;
853 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
857 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
859 struct hostapd_data *hapd;
863 if (wpa_s->ap_iface == NULL)
865 hapd = wpa_s->ap_iface->bss[0];
866 pin = wps_generate_pin();
867 os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
868 os_free(hapd->conf->ap_pin);
869 hapd->conf->ap_pin = os_strdup(pin_txt);
870 if (hapd->conf->ap_pin == NULL)
872 wpas_wps_ap_pin_enable(wpa_s, timeout);
874 return hapd->conf->ap_pin;
878 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
880 struct hostapd_data *hapd;
881 if (wpa_s->ap_iface == NULL)
883 hapd = wpa_s->ap_iface->bss[0];
884 return hapd->conf->ap_pin;
888 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
891 struct hostapd_data *hapd;
895 if (wpa_s->ap_iface == NULL)
897 hapd = wpa_s->ap_iface->bss[0];
898 ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
899 if (ret < 0 || ret >= (int) sizeof(pin_txt))
901 os_free(hapd->conf->ap_pin);
902 hapd->conf->ap_pin = os_strdup(pin_txt);
903 if (hapd->conf->ap_pin == NULL)
905 wpas_wps_ap_pin_enable(wpa_s, timeout);
911 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
913 struct hostapd_data *hapd;
915 if (wpa_s->ap_iface == NULL)
917 hapd = wpa_s->ap_iface->bss[0];
920 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
921 * PIN if this happens multiple times to slow down brute force attacks.
923 hapd->ap_pin_failures++;
924 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
925 hapd->ap_pin_failures);
926 if (hapd->ap_pin_failures < 3)
929 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
930 hapd->ap_pin_failures = 0;
931 os_free(hapd->conf->ap_pin);
932 hapd->conf->ap_pin = NULL;
936 #ifdef CONFIG_WPS_NFC
938 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
941 struct hostapd_data *hapd;
943 if (wpa_s->ap_iface == NULL)
945 hapd = wpa_s->ap_iface->bss[0];
946 return hostapd_wps_nfc_config_token(hapd, ndef);
950 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
953 struct hostapd_data *hapd;
955 if (wpa_s->ap_iface == NULL)
957 hapd = wpa_s->ap_iface->bss[0];
958 return hostapd_wps_nfc_hs_cr(hapd, ndef);
962 int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
963 const struct wpabuf *req,
964 const struct wpabuf *sel)
966 struct hostapd_data *hapd;
968 if (wpa_s->ap_iface == NULL)
970 hapd = wpa_s->ap_iface->bss[0];
971 return hostapd_wps_nfc_report_handover(hapd, req, sel);
974 #endif /* CONFIG_WPS_NFC */
976 #endif /* CONFIG_WPS */
979 #ifdef CONFIG_CTRL_IFACE
981 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
982 char *buf, size_t buflen)
984 if (wpa_s->ap_iface == NULL)
986 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
991 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
992 char *buf, size_t buflen)
994 if (wpa_s->ap_iface == NULL)
996 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
1001 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
1002 char *buf, size_t buflen)
1004 if (wpa_s->ap_iface == NULL)
1006 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
1011 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
1012 const char *txtaddr)
1014 if (wpa_s->ap_iface == NULL)
1016 return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
1021 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
1022 const char *txtaddr)
1024 if (wpa_s->ap_iface == NULL)
1026 return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
1031 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
1032 size_t buflen, int verbose)
1034 char *pos = buf, *end = buf + buflen;
1036 struct hostapd_bss_config *conf;
1038 if (wpa_s->ap_iface == NULL)
1041 conf = wpa_s->ap_iface->bss[0]->conf;
1045 ret = os_snprintf(pos, end - pos,
1046 "pairwise_cipher=%s\n"
1049 wpa_cipher_txt(conf->rsn_pairwise),
1050 wpa_cipher_txt(conf->wpa_group),
1051 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
1053 if (ret < 0 || ret >= end - pos)
1059 #endif /* CONFIG_CTRL_IFACE */
1062 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1064 struct hostapd_iface *iface = wpa_s->ap_iface;
1065 struct wpa_ssid *ssid = wpa_s->current_ssid;
1066 struct hostapd_data *hapd;
1068 if (ssid == NULL || wpa_s->ap_iface == NULL ||
1069 ssid->mode == WPAS_MODE_INFRA ||
1070 ssid->mode == WPAS_MODE_IBSS)
1074 if (ssid->mode == WPAS_MODE_P2P_GO)
1075 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
1076 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1077 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
1078 P2P_GROUP_FORMATION;
1079 #endif /* CONFIG_P2P */
1081 hapd = iface->bss[0];
1082 if (hapd->drv_priv == NULL)
1084 ieee802_11_set_beacons(iface);
1085 hostapd_set_ap_wps_ie(hapd);
1091 int ap_switch_channel(struct wpa_supplicant *wpa_s,
1092 struct csa_settings *settings)
1095 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1098 return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
1099 #else /* NEED_AP_MLME */
1101 #endif /* NEED_AP_MLME */
1105 int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
1107 struct csa_settings settings;
1108 int ret = hostapd_parse_csa_settings(pos, &settings);
1113 return ap_switch_channel(wpa_s, &settings);
1117 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1118 int offset, int width, int cf1, int cf2)
1120 if (!wpa_s->ap_iface)
1123 wpa_s->assoc_freq = freq;
1124 hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset, width, cf1, cf1);
1128 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1131 struct hostapd_data *hapd;
1132 struct hostapd_bss_config *conf;
1134 if (!wpa_s->ap_iface)
1138 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1141 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1143 hapd = wpa_s->ap_iface->bss[0];
1146 os_free(conf->accept_mac);
1147 conf->accept_mac = NULL;
1148 conf->num_accept_mac = 0;
1149 os_free(conf->deny_mac);
1150 conf->deny_mac = NULL;
1151 conf->num_deny_mac = 0;
1154 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1158 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1159 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1160 if (conf->accept_mac == NULL)
1162 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1163 conf->num_accept_mac = 1;
1169 #ifdef CONFIG_WPS_NFC
1170 int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
1171 const struct wpabuf *pw, const u8 *pubkey_hash)
1173 struct hostapd_data *hapd;
1174 struct wps_context *wps;
1176 if (!wpa_s->ap_iface)
1178 hapd = wpa_s->ap_iface->bss[0];
1181 if (wpa_s->parent->conf->wps_nfc_dh_pubkey == NULL ||
1182 wpa_s->parent->conf->wps_nfc_dh_privkey == NULL) {
1183 wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
1187 dh5_free(wps->dh_ctx);
1188 wpabuf_free(wps->dh_pubkey);
1189 wpabuf_free(wps->dh_privkey);
1190 wps->dh_privkey = wpabuf_dup(
1191 wpa_s->parent->conf->wps_nfc_dh_privkey);
1192 wps->dh_pubkey = wpabuf_dup(
1193 wpa_s->parent->conf->wps_nfc_dh_pubkey);
1194 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
1196 wpabuf_free(wps->dh_pubkey);
1197 wps->dh_pubkey = NULL;
1198 wpabuf_free(wps->dh_privkey);
1199 wps->dh_privkey = NULL;
1202 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
1203 if (wps->dh_ctx == NULL)
1206 return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
1208 pw ? wpabuf_head(pw) : NULL,
1209 pw ? wpabuf_len(pw) : 0, 1);
1211 #endif /* CONFIG_WPS_NFC */