[PATCH 2/4] Add support for AP+STA on a single radio

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Felix Fietkau <nbd@xxxxxxxx>

Although it is currently possible to have an AP (i.e. hostapd) and a STA
(i.e. wpa_supplicant) interface running simultaneously on the same
radio, this has severe limitations:

- If the AP is enabled first, the STA can only associate on the same
  channel that was configured by the AP.
- Once the STA is connected and the AP is enabled, it is no longer
  possible to switch channels. Thus, if the BSS to which the STA is
  associated switches channels, the connection is lost.

To overcome this limitation, add a command-line option to wpa_supplicant
with the hostapd socket that it can interact with.

When the STA starts scanning, the AP is first stopped with the STOP_AP
command.

When the STA connection is completed, the AP is started again with the
UPDATE command. The new channel information is passed as well, so
hostapd can update its internal state. Since the radio is already
configured on this channel, no switch is required.

When the STA receives a CSA, it is forwarded to the AP with the
CHAN_SWITCH command. To make this possible, the count is retrieved from
the CSA and added to struct ch_switch. Since CHAN_SWITCH is also
supposed to specify HT, VHT and HE options, but we don't want to
override these, add the 'auto-ht' option to the command so it can be
read from the config instead of from the command.

Signed-off-by: Felix Fietkau <nbd@xxxxxxxx>
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@xxxxxxx>
[ Raphaël: Fix cs count, remove unused ht_capab,
  replace asprintf, use channel_width_to_int, adapt to mainline ]
Signed-off-by: Raphaël Mélotte <raphael.melotte@xxxxxxx>
---
 hostapd/ctrl_iface.c               |  6 +++
 src/ap/beacon.c                    |  5 ---
 src/drivers/driver.h               |  2 +
 src/drivers/driver_nl80211_event.c |  6 ++-
 wpa_supplicant/Makefile            |  2 +
 wpa_supplicant/bss.c               | 10 +++++
 wpa_supplicant/bss.h               |  2 +
 wpa_supplicant/events.c            | 44 ++++++++++++++++++-
 wpa_supplicant/main.c              |  8 +++-
 wpa_supplicant/wpa_supplicant.c    | 70 ++++++++++++++++++++++++++++++
 wpa_supplicant/wpa_supplicant_i.h  |  7 +++
 11 files changed, 153 insertions(+), 9 deletions(-)

diff --git a/hostapd/ctrl_iface.c b/hostapd/ctrl_iface.c
index f76028cd5..949d47925 100644
--- a/hostapd/ctrl_iface.c
+++ b/hostapd/ctrl_iface.c
@@ -2698,6 +2698,12 @@ static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
 		return 0;
 	}
 
+	if (os_strstr(pos, " auto-ht")) {
+		settings.freq_params.ht_enabled = iface->conf->ieee80211n;
+		settings.freq_params.vht_enabled = iface->conf->ieee80211ac;
+		settings.freq_params.he_enabled = iface->conf->ieee80211ax;
+	}
+
 	for (i = 0; i < iface->num_bss; i++) {
 
 		/* Save CHAN_SWITCH VHT, HE, and EHT config */
diff --git a/src/ap/beacon.c b/src/ap/beacon.c
index c25a5bbc3..e097bc675 100644
--- a/src/ap/beacon.c
+++ b/src/ap/beacon.c
@@ -2052,11 +2052,6 @@ static int __ieee802_11_set_beacon(struct hostapd_data *hapd)
 		return -1;
 	}
 
-	if (hapd->csa_in_progress) {
-		wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
-		return -1;
-	}
-
 	hapd->beacon_set_done = 1;
 
 	if (ieee802_11_build_ap_params(hapd, &params) < 0)
diff --git a/src/drivers/driver.h b/src/drivers/driver.h
index 7c0e949b9..35b5ca1bc 100644
--- a/src/drivers/driver.h
+++ b/src/drivers/driver.h
@@ -6324,6 +6324,7 @@ union wpa_event_data {
 
 	/**
 	 * struct ch_switch
+	 * @count: Count until channel switch activates
 	 * @freq: Frequency of new channel in MHz
 	 * @ht_enabled: Whether this is an HT channel
 	 * @ch_offset: Secondary channel offset
@@ -6334,6 +6335,7 @@ union wpa_event_data {
 	 * @punct_bitmap: Puncturing bitmap
 	 */
 	struct ch_switch {
+		int count;
 		int freq;
 		int ht_enabled;
 		int ch_offset;
diff --git a/src/drivers/driver_nl80211_event.c b/src/drivers/driver_nl80211_event.c
index a6f8c97ca..9b83533ab 100644
--- a/src/drivers/driver_nl80211_event.c
+++ b/src/drivers/driver_nl80211_event.c
@@ -995,7 +995,7 @@ static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
 				 struct nlattr *ifindex, struct nlattr *link,
 				 struct nlattr *freq, struct nlattr *type,
 				 struct nlattr *bw, struct nlattr *cf1,
-				 struct nlattr *cf2,
+				 struct nlattr *cf2, struct nlattr *count,
 				 struct nlattr *punct_bitmap,
 				 int finished)
 {
@@ -1060,6 +1060,8 @@ static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
 		data.ch_switch.cf1 = nla_get_u32(cf1);
 	if (cf2)
 		data.ch_switch.cf2 = nla_get_u32(cf2);
+	if (count)
+		data.ch_switch.count = nla_get_u32(count);
 
 	if (finished)
 		bss->flink->freq = data.ch_switch.freq;
@@ -3603,6 +3605,7 @@ static void do_process_drv_event(struct i802_bss *bss, int cmd,
 				     tb[NL80211_ATTR_CHANNEL_WIDTH],
 				     tb[NL80211_ATTR_CENTER_FREQ1],
 				     tb[NL80211_ATTR_CENTER_FREQ2],
+				     tb[NL80211_ATTR_CH_SWITCH_COUNT],
 				     tb[NL80211_ATTR_PUNCT_BITMAP],
 				     0);
 		break;
@@ -3615,6 +3618,7 @@ static void do_process_drv_event(struct i802_bss *bss, int cmd,
 				     tb[NL80211_ATTR_CHANNEL_WIDTH],
 				     tb[NL80211_ATTR_CENTER_FREQ1],
 				     tb[NL80211_ATTR_CENTER_FREQ2],
+				     NULL,
 				     tb[NL80211_ATTR_PUNCT_BITMAP],
 				     1);
 		break;
diff --git a/wpa_supplicant/Makefile b/wpa_supplicant/Makefile
index 57620fe79..09796ceea 100644
--- a/wpa_supplicant/Makefile
+++ b/wpa_supplicant/Makefile
@@ -125,6 +125,8 @@ OBJS_c += ../src/utils/common.o
 OBJS_c += ../src/common/cli.o
 OBJS += wmm_ac.o
 
+OBJS += ../src/common/wpa_ctrl.o
+
 ifndef CONFIG_OS
 ifdef CONFIG_NATIVE_WINDOWS
 CONFIG_OS=win32
diff --git a/wpa_supplicant/bss.c b/wpa_supplicant/bss.c
index 320441426..b3501705c 100644
--- a/wpa_supplicant/bss.c
+++ b/wpa_supplicant/bss.c
@@ -11,6 +11,7 @@
 #include "utils/common.h"
 #include "utils/eloop.h"
 #include "common/ieee802_11_defs.h"
+#include "common/ieee802_11_common.h"
 #include "drivers/driver.h"
 #include "eap_peer/eap.h"
 #include "wpa_supplicant_i.h"
@@ -283,6 +284,9 @@ void calculate_update_time(const struct os_reltime *fetch_time,
 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
 			     struct os_reltime *fetch_time)
 {
+	struct ieee80211_ht_operation *oper;
+	struct ieee802_11_elems elems;
+
 	dst->flags = src->flags;
 	os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
 	dst->freq = src->freq;
@@ -296,6 +300,12 @@ static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
 	dst->est_throughput = src->est_throughput;
 	dst->snr = src->snr;
 
+	memset(&elems, 0, sizeof(elems));
+	ieee802_11_parse_elems((u8 *) (src + 1), src->ie_len, &elems, 0);
+	oper = (struct ieee80211_ht_operation *) elems.ht_operation;
+	if (oper)
+		dst->ht_param = oper->ht_param;
+
 	calculate_update_time(fetch_time, src->age, &dst->last_update);
 }
 
diff --git a/wpa_supplicant/bss.h b/wpa_supplicant/bss.h
index 611da8844..372fee97e 100644
--- a/wpa_supplicant/bss.h
+++ b/wpa_supplicant/bss.h
@@ -94,6 +94,8 @@ struct wpa_bss {
 	u8 ssid[SSID_MAX_LEN];
 	/** Length of SSID */
 	size_t ssid_len;
+	/* Five octets of HT Operation Information */
+	u8 ht_param;
 	/** Frequency of the channel in MHz (e.g., 2412 = channel 1) */
 	int freq;
 	/** Beacon interval in TUs (host byte order) */
diff --git a/wpa_supplicant/events.c b/wpa_supplicant/events.c
index ebac8a152..28c1e7885 100644
--- a/wpa_supplicant/events.c
+++ b/wpa_supplicant/events.c
@@ -5235,6 +5235,46 @@ static void wpas_event_unprot_beacon(struct wpa_supplicant *wpa_s,
 }
 
 
+static void
+supplicant_ch_switch_started(struct wpa_supplicant *wpa_s,
+			    union wpa_event_data *data)
+{
+	char buf[256];
+	size_t len = sizeof(buf);
+	char cmd[256];
+	int width = 20;
+	int ret;
+
+	if (!wpa_s->hostapd)
+		return;
+
+	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CHANNEL_SWITCH
+		"count=%d freq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
+		data->ch_switch.count,
+		data->ch_switch.freq,
+		data->ch_switch.ht_enabled,
+		data->ch_switch.ch_offset,
+		channel_width_to_string(data->ch_switch.ch_width),
+		data->ch_switch.cf1,
+		data->ch_switch.cf2);
+
+	data->ch_switch.ch_width = channel_width_to_int(data->ch_switch.ch_width);
+
+	os_snprintf(cmd, sizeof(cmd),
+		    "CHAN_SWITCH %d %d sec_channel_offset=%d center_freq1=%d center_freq2=%d, bandwidth=%d auto-ht\n",
+		    data->ch_switch.count,
+		    data->ch_switch.freq,
+		    data->ch_switch.ch_offset,
+		    data->ch_switch.cf1,
+		    data->ch_switch.cf2,
+		    width);
+	ret = wpa_ctrl_request(wpa_s->hostapd, cmd, os_strlen(cmd), buf, &len, NULL);
+
+	if (ret < 0)
+		wpa_printf(MSG_ERROR, "\nFailed to reload hostapd AP interfaces\n");
+}
+
+
 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
 			  union wpa_event_data *data)
 {
@@ -5584,8 +5624,10 @@ void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
 			channel_width_to_string(data->ch_switch.ch_width),
 			data->ch_switch.cf1,
 			data->ch_switch.cf2);
-		if (event == EVENT_CH_SWITCH_STARTED)
+		if (event == EVENT_CH_SWITCH_STARTED) {
+			supplicant_ch_switch_started(wpa_s, data);
 			break;
+		}
 
 		wpa_s->assoc_freq = data->ch_switch.freq;
 		wpa_s->current_ssid->frequency = data->ch_switch.freq;
diff --git a/wpa_supplicant/main.c b/wpa_supplicant/main.c
index 9229eb51f..535923ecd 100644
--- a/wpa_supplicant/main.c
+++ b/wpa_supplicant/main.c
@@ -35,7 +35,7 @@ static void usage(void)
 	       "vW] [-P<pid file>] "
 	       "[-g<global ctrl>] \\\n"
 	       "        [-G<group>] \\\n"
-	       "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
+	       "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] [-H<hostapd path>] "
 	       "[-p<driver_param>] \\\n"
 	       "        [-b<br_ifname>] [-e<entropy file>]"
 #ifdef CONFIG_DEBUG_FILE
@@ -75,6 +75,7 @@ static void usage(void)
 	       "  -g = global ctrl_interface\n"
 	       "  -G = global ctrl_interface group\n"
 	       "  -h = show this help text\n"
+	       "  -H = connect to a hostapd instance to manage state changes\n"
 	       "  -i = interface name\n"
 	       "  -I = additional configuration file\n"
 	       "  -K = include keys (passwords, etc.) in debug output\n"
@@ -202,7 +203,7 @@ int main(int argc, char *argv[])
 
 	for (;;) {
 		c = getopt(argc, argv,
-			   "b:Bc:C:D:de:f:g:G:hi:I:KLMm:No:O:p:P:qsTtuvW");
+			   "b:Bc:C:D:de:f:g:G:hH:i:I:KLMm:No:O:p:P:qsTtuvW");
 		if (c < 0)
 			break;
 		switch (c) {
@@ -249,6 +250,9 @@ int main(int argc, char *argv[])
 			usage();
 			exitcode = 0;
 			goto out;
+		case 'H':
+			iface->hostapd_ctrl = optarg;
+			break;
 		case 'i':
 			iface->ifname = optarg;
 			break;
diff --git a/wpa_supplicant/wpa_supplicant.c b/wpa_supplicant/wpa_supplicant.c
index 1de6fa036..bcaede701 100644
--- a/wpa_supplicant/wpa_supplicant.c
+++ b/wpa_supplicant/wpa_supplicant.c
@@ -131,6 +131,55 @@ static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s);
 static void wpas_update_owe_connect_params(struct wpa_supplicant *wpa_s);
 #endif /* CONFIG_OWE */
 
+static int hostapd_stop(struct wpa_supplicant *wpa_s)
+{
+	const char *cmd = "STOP_AP";
+	char buf[256];
+	size_t len = sizeof(buf);
+
+	if (wpa_ctrl_request(wpa_s->hostapd, cmd, os_strlen(cmd), buf, &len, NULL) < 0) {
+		wpa_printf(MSG_ERROR, "\nFailed to stop hostapd AP interfaces\n");
+		return -1;
+	}
+	return 0;
+}
+
+static int hostapd_reload(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
+{
+	char cmd[256];
+	char buf[256];
+	size_t len = sizeof(buf);
+	enum hostapd_hw_mode hw_mode;
+	u8 channel;
+	int sec_chan = 0;
+	int ret;
+
+	if (!bss)
+		return -1;
+
+	if (bss->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) {
+		int sec = bss->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
+		if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
+			sec_chan = 1;
+		else if (sec ==  HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
+			sec_chan = -1;
+	}
+
+	hw_mode = ieee80211_freq_to_chan(bss->freq, &channel);
+
+	ret = os_snprintf(cmd, sizeof(cmd), "UPDATE channel=%d sec_chan=%d hw_mode=%d",
+			  channel, sec_chan, hw_mode);
+	if (os_snprintf_error(sizeof(cmd), ret))
+		return -1;
+
+	ret = wpa_ctrl_request(wpa_s->hostapd, cmd, os_strlen(cmd), buf, &len, NULL);
+
+	if (ret < 0) {
+		wpa_printf(MSG_ERROR, "\nFailed to reload hostapd AP interfaces\n");
+		return -1;
+	}
+	return 0;
+}
 
 #ifdef CONFIG_WEP
 /* Configure default/group WEP keys for static WEP */
@@ -1026,6 +1075,8 @@ void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
 
 		sme_sched_obss_scan(wpa_s, 1);
 
+		if (wpa_s->hostapd)
+			hostapd_reload(wpa_s, wpa_s->current_bss);
 #if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
 		if (!fils_hlp_sent && ssid && ssid->eap.erp)
 			update_fils_connect_params = true;
@@ -1036,6 +1087,8 @@ void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
 #endif /* CONFIG_OWE */
 	} else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
 		   state == WPA_ASSOCIATED) {
+		if (wpa_s->hostapd)
+			hostapd_stop(wpa_s);
 		wpa_s->new_connection = 1;
 		wpa_drv_set_operstate(wpa_s, 0);
 #ifndef IEEE8021X_EAPOL
@@ -2519,6 +2572,8 @@ void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
 			return;
 		}
 		wpa_s->current_bss = bss;
+		if (wpa_s->hostapd)
+			hostapd_reload(wpa_s, wpa_s->current_bss);
 #else /* CONFIG_MESH */
 		wpa_msg(wpa_s, MSG_ERROR,
 			"mesh mode support not included in the build");
@@ -6996,6 +7051,16 @@ static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
 			   sizeof(wpa_s->bridge_ifname));
 	}
 
+	if (iface->hostapd_ctrl) {
+		wpa_s->hostapd = wpa_ctrl_open(iface->hostapd_ctrl);
+		if (!wpa_s->hostapd) {
+			wpa_printf(MSG_ERROR, "\nFailed to connect to hostapd\n");
+			return -1;
+		}
+		if (hostapd_stop(wpa_s) < 0)
+			return -1;
+	}
+
 	/* RSNA Supplicant Key Management - INITIALIZE */
 	eapol_sm_notify_portEnabled(wpa_s->eapol, false);
 	eapol_sm_notify_portValid(wpa_s->eapol, false);
@@ -7338,6 +7403,11 @@ static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
 	if (terminate)
 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
 
+	if (wpa_s->hostapd) {
+		wpa_ctrl_close(wpa_s->hostapd);
+		wpa_s->hostapd = NULL;
+	}
+
 	wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
 	wpa_s->ctrl_iface = NULL;
 
diff --git a/wpa_supplicant/wpa_supplicant_i.h b/wpa_supplicant/wpa_supplicant_i.h
index d5b3dab67..8af948c36 100644
--- a/wpa_supplicant/wpa_supplicant_i.h
+++ b/wpa_supplicant/wpa_supplicant_i.h
@@ -105,6 +105,11 @@ struct wpa_interface {
 	 */
 	const char *ifname;
 
+	/**
+	 * hostapd_ctrl - path to hostapd control socket for notification
+	 */
+	const char *hostapd_ctrl;
+
 	/**
 	 * bridge_ifname - Optional bridge interface name
 	 *
@@ -665,6 +670,8 @@ struct wpa_supplicant {
 #endif /* CONFIG_CTRL_IFACE_BINDER */
 	char bridge_ifname[16];
 
+	struct wpa_ctrl *hostapd;
+
 	char *confname;
 	char *confanother;
 
-- 
2.37.3


_______________________________________________
Hostap mailing list
Hostap@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/hostap




[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux