From 2d98b653275447a382db1e79dfa08467f70d700e Mon Sep 17 00:00:00 2001
From: Kamil Zulewski <kamil.zulewski@iopsys.eu>
Date: Tue, 29 Aug 2023 13:50:11 +0200
Subject: [PATCH] Store and restore bBSS allow/disallow assoc. mode when iface
 is up

---
 src/agent.c     | 12 ++++++++++++
 src/agent_map.c | 18 ++++++++++--------
 src/config.h    |  3 ++-
 src/wifi.c      |  4 ++--
 src/wifi.h      |  2 +-
 src/wifi_ubus.c |  6 +++---
 6 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/agent.c b/src/agent.c
index 33ac8272b..314087dcc 100644
--- a/src/agent.c
+++ b/src/agent.c
@@ -2604,6 +2604,16 @@ out_str:
 	}
 }
 
+static void apply_iface_assocation_mode(struct agent *a, const char *ifname)
+{
+	struct netif_ap *iface = get_netif_by_name(a, ifname);
+
+	if (iface) {
+		wifi_set_backhaul_bss_association_mode(ifname,
+						       iface->cfg->disallow_assoc);
+	}
+}
+
 static void wifi_iface_event_handler(void *c, struct blob_attr *msg)
 {
 	struct agent *a = (struct agent *)c;
@@ -2649,6 +2659,8 @@ static void wifi_iface_event_handler(void *c, struct blob_attr *msg)
 		 * BTM, WNM, ANQP
 		 */
 		wifi_parse_all_frame(a, ifname, data);
+	} else if (!strcmp(event, "bss-up")) {
+		apply_iface_assocation_mode(a, ifname);
 	}
 
 out_json:
diff --git a/src/agent_map.c b/src/agent_map.c
index 145aaf8f4..644f9f9ce 100644
--- a/src/agent_map.c
+++ b/src/agent_map.c
@@ -3968,20 +3968,22 @@ int agent_process_assoc_cntl_tlv(void *agent, uint8_t *p,
 		ret = -1;
 	}
 
-	// If bssid is backhaul BSS and STA addr. is ff:ff:ff:ff:ff:ff
-	// block/unblock associations for all macs.
-	if (!ret && (fh->cfg->multi_ap & 0x01) && data->num_sta == 1 &&
+	/* If bssid is backhaul BSS and STA addr. is ff:ff:ff:ff:ff:ff
+	 * block/unblock associations for all macs.
+	 */
+	if (!ret && (ap->cfg->multi_ap & 0x01) && data->num_sta == 1 &&
 	    hwaddr_is_bcast(data->sta[0].macaddr)) {
-		bool allow = true;
+		bool disallow = false;
 
 		if (data->control == ASSOC_CTRL_BLOCK)
-			allow = false;
+			disallow = true;
 
 		trace("%s: %s assoc. for bBSS: " MACFMT " [%s]\n",
-			 __func__, allow ? "Allow" : "Disallow", MAC2STR(data->bssid), fh->name);
-
-		ret = wifi_set_backhaul_bss_association_mode(fh->name, allow);
+			 __func__, disallow ? "Disallow" : "Allow", MAC2STR(data->bssid), ap->name);
 
+		ret = wifi_set_backhaul_bss_association_mode(ap->name, disallow);
+		if (!ret)
+			ap->cfg->disallow_assoc = disallow;
 
 		return ret;
 	}
diff --git a/src/config.h b/src/config.h
index 4c15749ff..2cb7c6840 100644
--- a/src/config.h
+++ b/src/config.h
@@ -110,7 +110,8 @@ struct netif_apcfg {
 	struct list_head list;  /* link to next netif_config */
 	uint8_t multi_ap;	/* BSS type, 0x01: backhaul, 0x02: fronthaul, 0x03: combined */
 	unsigned int vid;	/* traffic separation vlan id */
-	uint8_t bsta_disallow;  /*this option 1 p1 2 p2 3 both disallow*/
+	uint8_t bsta_disallow;  /* this option 1 p1 2 p2 3 both disallow */
+	bool disallow_assoc;	/* if true all associations disallowed */
 };
 
 struct netif_bkcfg {
diff --git a/src/wifi.c b/src/wifi.c
index 9d1b4ce52..cdfd1819a 100644
--- a/src/wifi.c
+++ b/src/wifi.c
@@ -385,12 +385,12 @@ int wifi_ap_stats(const char *ifname, struct wifi_ap_stats *stats)
 	return ret;
 }
 
-int wifi_set_backhaul_bss_association_mode(const char *ifname, bool allow)
+int wifi_set_backhaul_bss_association_mode(const char *ifname, bool disallow)
 {
 	struct ubus_context *ctx = ubus_connect(NULL);
 	int ret;
 
-	ret = wifi_ubus_ap_mbo_disallow_assoc(ctx, ifname, allow);
+	ret = wifi_ubus_ap_mbo_disallow_assoc(ctx, ifname, disallow);
 
 	ubus_free(ctx);
 	return ret;
diff --git a/src/wifi.h b/src/wifi.h
index 491f857b8..0fb2cd2fe 100644
--- a/src/wifi.h
+++ b/src/wifi.h
@@ -164,7 +164,7 @@ int wifi_bsta_status(const char *ifname, struct wifi_bsta_status *bsta_status);
 int wifi_sta_info(const char *ifname, struct wifi_sta *sta);
 int wifi_sta_disconnect_ap(const char *ifname, uint32_t reason);
 
-int wifi_set_backhaul_bss_association_mode(const char *ifname, bool allow);
+int wifi_set_backhaul_bss_association_mode(const char *ifname, bool disallow);
 
 
 int c2f(int chan);
diff --git a/src/wifi_ubus.c b/src/wifi_ubus.c
index 57884507f..bd2876883 100644
--- a/src/wifi_ubus.c
+++ b/src/wifi_ubus.c
@@ -2359,7 +2359,7 @@ out:
 }
 
 int wifi_ubus_ap_mbo_disallow_assoc(struct ubus_context *ubus_ctx,
-				    const char *ifname, bool allow)
+				    const char *ifname, bool disallow)
 {
 	enum { ALLOW = 0, DISALLOW = 2 };
 	struct blob_buf bb = { 0 };
@@ -2367,7 +2367,7 @@ int wifi_ubus_ap_mbo_disallow_assoc(struct ubus_context *ubus_ctx,
 	uint32_t id;
 	int ret;
 
-	trace("[%s] %s %s\n", ifname, __func__, allow ? "ALLOW" : "DISALLOW");
+	trace("[%s] %s %s\n", ifname, __func__, disallow ? "DISALLOW" : "ALLOW");
 
 	snprintf(name, sizeof(name), "wifi.ap.%s", ifname);
 	ret = ubus_lookup_id(ubus_ctx, name, &id);
@@ -2375,7 +2375,7 @@ int wifi_ubus_ap_mbo_disallow_assoc(struct ubus_context *ubus_ctx,
 		goto out;
 
 	blob_buf_init(&bb, 0);
-	blobmsg_add_u32(&bb, "reason", allow ? ALLOW : DISALLOW);
+	blobmsg_add_u32(&bb, "reason", disallow ? DISALLOW : ALLOW);
 	ret = ubus_invoke(ubus_ctx, id, "mbo_disallow_assoc", bb.head, NULL, NULL,
 			  2 * 1000);
 	blob_buf_free(&bb);
-- 
GitLab