From cdba2e30338d44497a541affa037a57620cd784d Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakob.olsson@iopsys.eu>
Date: Fri, 21 May 2021 15:54:49 +0200
Subject: [PATCH] map-agent: create named wifi-iface sections
---
src/core/agent.c | 2 +-
src/core/config.c | 70 +++++++++++++++++++++++++++++++++--------------
src/core/config.h | 2 +-
3 files changed, 51 insertions(+), 23 deletions(-)
diff --git a/src/core/agent.c b/src/core/agent.c
index f1ac0c5c1..8091f0713 100644
--- a/src/core/agent.c
+++ b/src/core/agent.c
@@ -2354,7 +2354,7 @@ static void wifi_wps_creds_event_handler(void *c, struct blob_attr *msg)
ret = uci_check_wifi_iface("mapagent", ifname, "bk-iface");
if (!ret) {
ret = uci_add_wireless_iface_sec("mapagent", ifname,
- "bk-iface");
+ "bk-iface", NULL);
if (!ret)
return;
}
diff --git a/src/core/config.c b/src/core/config.c
index 60acf63d6..85350c234 100644
--- a/src/core/config.c
+++ b/src/core/config.c
@@ -52,6 +52,18 @@
int verbose;
+char *replace_char(char *str, char find, char replace)
+{
+ char *current_pos = strchr(str, find);
+
+ while (current_pos) {
+ *current_pos = replace;
+ current_pos = strchr(current_pos, find);
+ }
+
+ return str;
+}
+
int set_value(struct uci_context *ctx, struct uci_package *pkg,
struct uci_section *section, const char *key,
const char *value, enum uci_option_type type)
@@ -549,11 +561,13 @@ static bool get_encryption_value(uint16_t auth_type, uint16_t encryption_type,
}
bool uci_add_wireless_iface_sec(char *package_name, char *interface_name,
- char *section_name)
+ char *section_type, char *section_name)
{
struct uci_context *ctx;
struct uci_package *pkg;
struct uci_section *s;
+ struct uci_ptr ptr = {0};
+ bool ret = false;
if (!interface_name || !package_name)
return false;
@@ -562,33 +576,43 @@ bool uci_add_wireless_iface_sec(char *package_name, char *interface_name,
if (!ctx)
return false;
- if (uci_load(ctx, package_name, &pkg)) {
- uci_free_context(ctx);
- return false;
- }
+ if (uci_load(ctx, package_name, &pkg))
+ goto out_ctx;
- if (uci_add_section(ctx, pkg, section_name, &s) == UCI_OK) {
- struct uci_ptr ptr = {0};
+ ptr.p = pkg;
+
+ if (section_name) {
+ ptr.section = section_name;
+ ptr.value = section_type;
+ ptr.option = NULL;
+ uci_set(ctx, &ptr);
+ if (uci_save(ctx, ptr.p) != UCI_OK)
+ goto out_unload;
+ } else {
+ if (uci_add_section(ctx, pkg, section_type, &s) != UCI_OK)
+ goto out_unload;
if (uci_save(ctx, pkg) != UCI_OK)
- return false;
+ goto out_unload;
- ptr.value = interface_name;
- ptr.package = package_name;
ptr.section = s->e.name;
- ptr.option = "ifname";
- ptr.target = UCI_TYPE_OPTION;
- uci_lookup_ptr(ctx, &ptr, NULL, false);
-
- uci_set(ctx, &ptr);
- uci_save(ctx, ptr.p);
- uci_commit(ctx, &pkg, false);
}
+ ptr.value = interface_name;
+ ptr.option = "ifname";
+ ptr.target = UCI_TYPE_OPTION;
+ uci_lookup_ptr(ctx, &ptr, NULL, false);
+
+ uci_set(ctx, &ptr);
+ uci_save(ctx, ptr.p);
+ uci_commit(ctx, &pkg, false);
+
+ ret = true;
+out_unload:
uci_unload(ctx, pkg);
+out_ctx:
uci_free_context(ctx);
-
- return true;
+ return ret;
}
static int ubus_call(const char *object, const char *method,
@@ -751,7 +775,7 @@ int uci_apply_m2(struct agent_config *cfg, char *interface_name, char *device,
agent_section);
if (!ret) {
ret = uci_add_wireless_iface_sec(UCI_AGENT, interface_name,
- agent_section);
+ agent_section, NULL);
if (!ret)
return M2_PROCESS_ERROR;
}
@@ -788,8 +812,12 @@ int uci_apply_m2(struct agent_config *cfg, char *interface_name, char *device,
ret = uci_check_wifi_iface(UCI_WIRELESS, interface_name,
UCI_WLAN_IFACE);
if (!ret) {
+ char name[32] = {0};
+
+ snprintf(name, sizeof(name), "%s_ap", interface_name);
+ replace_char(name, '.', '_');
ret = uci_add_wireless_iface_sec(UCI_WIRELESS, interface_name,
- UCI_WLAN_IFACE);
+ UCI_WLAN_IFACE, name);
if (!ret)
return M2_PROCESS_ERROR;
}
diff --git a/src/core/config.h b/src/core/config.h
index 79356f498..43e6dedc8 100644
--- a/src/core/config.h
+++ b/src/core/config.h
@@ -249,7 +249,7 @@ bool uci_set_wireless_interface_option(char *package_name,
char *section_type, char *search_key, char *search_val,
char *option, char *value);
bool uci_add_wireless_iface_sec(char *package_name, char *interface_name,
- char *section_name);
+ char *section_type, char *section_name);
void clean_bk(struct netif_bkcfg *p);
int clean_all_bk(struct agent_config *cfg);
--
GitLab