Skip to content
Snippets Groups Projects
config.c 18.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • Anjan Chanda's avatar
    Anjan Chanda committed
    /*
     * config.c - configurations handling
     *
     * Copyright (C) 2019 IOPSYS Software Solutions AB. All rights reserved.
     *
     * Author: anjan.chanda@iopsys.eu
     *
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #ifndef _GNU_SOURCE
    #define _GNU_SOURCE
    #endif
    
    #include <json-c/json.h>
    #include <libubox/blobmsg.h>
    #include <libubox/blobmsg_json.h>
    #include <libubox/uloop.h>
    #include <libubox/ustream.h>
    #include <libubox/utils.h>
    #include <libubus.h>
    #include <uci.h>
    
    #include "debug.h"
    #include "utils.h"
    #include "config.h"
    #include "steer_rules.h"
    #include "comm.h"
    
    #include "msgqueue.h"
    #include "worker.h"
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    #include "agent.h"
    
    
    static 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)
    {
    	struct uci_ptr ptr = {0};
    
    	ptr.p = pkg;
    	ptr.s = section;
    	ptr.option = key;
    	ptr.value = value;
    
    	if (type == UCI_TYPE_STRING)
    		return uci_set(ctx, &ptr);
    
    	if (type == UCI_TYPE_LIST)
    		return uci_add_list(ctx, &ptr);
    
    	return -1;
    }
    
    static struct uci_section *wifi_get_iface_section(struct uci_context *ctx,
    		struct uci_package *pkg, char *ifname)
    {
    
    	struct uci_element *e;
    	struct uci_section *section;
    
    	/* get the wet iface section */
    	uci_foreach_element(&pkg->sections, e) {
    		const char *c_ifname;
    
    		section = uci_to_section(e);
    		if (strcmp(section->type, "wifi-iface"))
    			continue;
    
    		c_ifname = uci_lookup_option_string(ctx, section, "ifname");
    		if (c_ifname && !strcmp(c_ifname, ifname))
    			return section;
    	}
    
    	return NULL;
    }
    
    int wifi_apply_iface_cfg(const char *ifname, const char *encryption,
    		const char *ssid, const char *key)
    {
    	struct uci_context *ctx;
    	struct uci_package *pkg;
    	struct uci_element *e;
    	struct uci_section *section;
    	int rv = -1;
    
    	ctx = uci_alloc_context();
    	if (!ctx)
    		goto out;
    
    	if (uci_load(ctx, "wireless", &pkg) != UCI_OK) {
    		dbg("config file 'wireless' not found!\n");
    		goto out_uci;
    	}
    
    	section = wifi_get_iface_section(ctx, pkg, ifname);
    	if (!section)
    		goto out_pkg;
    
    	set_value(ctx, pkg, section, "encryption", encryption, UCI_TYPE_STRING);
    	set_value(ctx, pkg, section, "ssid", ssid, UCI_TYPE_STRING);
    	set_value(ctx, pkg, section, "key", key, UCI_TYPE_STRING);
    
    	uci_commit(ctx, &pkg, false);
    
    out_pkg:
    	uci_unload(ctx, pkg);
    out_uci:
    	uci_free_context(ctx);
    out:
    	return rv;
    }
    
    
    static struct netif_bkcfg *get_netif_bkcfg_by_name(struct agent_config *c,
    							const char *name)
    {
    	struct netif_bkcfg *p;
    
    	list_for_each_entry(p, &c->bklist, list) {
    		if (!strcmp(name, p->name))
    			return p;
    	}
    
    	return NULL;
    }
    
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    static struct netif_fhcfg *get_netif_fhcfg_by_name(struct agent_config *c,
    							const char *name)
    {
    	struct netif_fhcfg *p;
    
    	list_for_each_entry(p, &c->fhlist, list) {
    		if (!strcmp(name, p->name))
    			return p;
    	}
    
    	return NULL;
    }
    
    static struct steer_policy *get_steer_policy_by_name(struct netif_fhcfg *c,
    							const char *name)
    {
    	struct steer_policy *p;
    
    	if (!c)
    		return NULL;
    
    	list_for_each_entry(p, &c->steer_policylist, list) {
    		if (!strcmp(name, p->name))
    			return p;
    	}
    
    	return NULL;
    }
    
    void stax_add_entry(struct list_head *h, char *sta_macstr)
    {
    	struct stax *n;
    
    	n = calloc(1, sizeof(struct stax));
    	if (n) {
    		snprintf(n->macstring, 18, "%s", sta_macstr);
    		list_add(&n->list, h);
    	}
    }
    
    void stax_del_entry(struct list_head *h, char *sta_macstr)
    {
    	struct stax *s, *tmp;
    
    	list_for_each_entry_safe(s, tmp, h, list) {
    		if (!strncmp(s->macstring, sta_macstr, sizeof(s->macstring))) {
    			list_del(&s->list);
    			free(s);
    			return;
    		}
    	}
    }
    
    void agent_config_dump(struct agent_config *cfg)
    {
    	struct netif_fhcfg *n;
    	struct steer_policy *pol;
    	struct stax *x;
    
    	if (!cfg)
    		return;
    
    	list_for_each_entry(n, &cfg->fhlist, list) {
    		dbg("name: %s\n", n->name);
    		dbg("  enabled  : %s\n", n->enabled ? "true" : "false");
    		dbg("  assocctrl: %s\n", n->assoc_control ? "true" : "false");
    
    		dbg("  Policies -------\n");
    		list_for_each_entry(pol, &n->steer_policylist, list) {
    			dbg("    name: %s\n", pol->name);
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    			dbg("    enabled  : %s\n",
    					pol->enabled ? "true" : "false");
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    			/* if (pol->dump_config)
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    			 *	pol->dump_config(pol, pol->policy);
    			 */
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    		}
    
    		dbg("  Steer Exclude Lists -------\n");
    		list_for_each_entry(x, &n->steer_excludelist, list) {
    			dbg("    mac: %s\n", x->macstring);
    		}
    
    		dbg("  Steer BTM Exclude Lists -------\n");
    		list_for_each_entry(x, &n->steer_btm_excludelist, list) {
    			dbg("    mac: %s\n", x->macstring);
    		}
    
    		dbg("  Assoc Ctrl Lists -------\n");
    		list_for_each_entry(x, &n->assoc_ctrllist, list) {
    			dbg("    mac: %s\n", x->macstring);
    		}
    	}
    }
    
    int agent_config_defaults(struct agent *a, struct agent_config *cfg)
    {
    	struct list_head *p, *tmp;
    	struct netif_fh *ifptr;
    	struct netif_fhcfg *new;
    
    	if (!cfg)
    		return -1;
    
    	cfg->enabled = false;
    	cfg->runfreq = AGENT_RUN_AUTO;
    
    	INIT_LIST_HEAD(&cfg->fhlist);
    	INIT_LIST_HEAD(&cfg->bklist);
    	list_for_each_entry(ifptr, &a->fhlist, list) {
    		/* struct list_head pollist; */
    		struct steer_rule *r;
    
    		new = calloc(1, sizeof(struct netif_fhcfg));
    		if (!new) {
    			warn("OOM! config\n");
    			goto err_alloc;
    		}
    
    		snprintf(new->name, 16, "%s", ifptr->name);
    		new->enabled = false;
    		new->steer_btm_retry = STEER_BTM_RETRY;
    		new->steer_btm_retry_secs = STEER_BTM_RETRY_INT;
    		new->fallback_legacy = STEER_LEGACY_FALLBACK_INT;
    		new->steer_legacy_reassoc_secs = STEER_LEGACY_REASSOC_INT;
    		new->steer_legacy_retry_secs = STEER_LEGACY_RETRY_INT;
    		new->assoc_control_time = ASSOC_CONTROL_INT;
    		INIT_LIST_HEAD(&new->steer_policylist);
    		/* nrules = get_registered_steer_rules(&pollist); */ /* TODO */
    		list_for_each_entry(r, &regd_steer_rules, list) {
    			struct steer_policy *pol;
    
    			pol = calloc(1, sizeof(struct steer_policy));
    			if (!pol)
    				goto err_alloc_policy;
    
    			snprintf(pol->name, 16, "%s", r->name);
    			pol->enabled = false;
    			if (r->init_config)
    				r->init_config(r, &pol->policy);
    			list_add(&pol->list, &new->steer_policylist);
    		}
    
    		INIT_LIST_HEAD(&new->steer_excludelist);
    		INIT_LIST_HEAD(&new->steer_btm_excludelist);
    		INIT_LIST_HEAD(&new->assoc_ctrllist);
    
    		ifptr->cfg = new;
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		dbg("%s: %s netif_fh->cfg = %p\n", __func__, ifptr->name,
    				ifptr->cfg);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    		list_add(&new->list, &cfg->fhlist);
    	}
    
    	return 0;
    
    err_alloc_policy:
    	/* TODO */
    err_alloc:
    	list_for_each_safe(p, tmp, &cfg->fhlist) {
    		list_del(p);
    		free(p);
    	}
    
    	return -1;
    }
    
    /* create fh-iface config and initialize with default values */
    struct netif_fhcfg *create_fronthaul_iface_config(struct agent_config *cfg,
    							const char *ifname)
    {
    	struct netif_fhcfg *new;
    	struct steer_rule *r;
    
    	if (!cfg)
    		return NULL;
    
    	new = calloc(1, sizeof(struct netif_fhcfg));
    	if (!new) {
    		warn("OOM! config\n");
    		return NULL;
    	}
    
    	snprintf(new->name, 16, "%s", ifname);
    	new->enabled = true;
    	new->fallback_legacy = STEER_LEGACY_FALLBACK_INT;
    	new->steer_btm_retry_secs = STEER_BTM_RETRY_INT;
    	new->steer_legacy_reassoc_secs = STEER_LEGACY_REASSOC_INT;
    	new->steer_legacy_retry_secs = STEER_LEGACY_RETRY_INT;
    	new->assoc_control_time = ASSOC_CONTROL_INT;
    	INIT_LIST_HEAD(&new->steer_policylist);
    	/* nrules = get_registered_steer_rules(&pollist); */ /* TODO */
    	list_for_each_entry(r, &regd_steer_rules, list) {
    		struct steer_policy *pol;
    
    		pol = calloc(1, sizeof(struct steer_policy));
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		if (!pol)
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    			goto err_oom;
    
    		snprintf(pol->name, 16, "%s", r->name);
    		pol->enabled = false;
    		if (r->init_config)
    			r->init_config(r, &pol->policy);
    		list_add(&pol->list, &new->steer_policylist);
    	}
    
    	INIT_LIST_HEAD(&new->steer_excludelist);
    	INIT_LIST_HEAD(&new->steer_btm_excludelist);
    	INIT_LIST_HEAD(&new->assoc_ctrllist);
    
    	/* f->cfg = new; */
    	dbg("%s: %s netif_fh->cfg = %p\n", __func__, new->name, new);
    
    	list_add(&new->list, &cfg->fhlist);
    
    	return new;
    
    err_oom:
    	list_flush(&new->steer_policylist, struct steer_policy, list);
    	free(new);
    	return NULL;
    }
    
    
    /* create fh-iface config and initialize with default values */
    struct netif_bkcfg *create_backhaul_iface_config(struct agent_config *cfg,
    							const char *ifname)
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    {
    
    	struct netif_bkcfg *new;
    	struct steer_rule *r;
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    	new = calloc(1, sizeof(struct netif_bkcfg));
    	if (!new) {
    		warn("OOM! config\n");
    		return NULL;
    	}
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    	snprintf(new->name, 16, "%s", ifname);
    	new->enabled = true;
    	new->onboarded = false;
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    	/* f->cfg = new; */
    	dbg("%s: %s netif_fh->cfg = %p\n", __func__, new->name, new);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    	list_add(&new->list, &cfg->fhlist);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    }
    
    static void config_update_entry(struct uci_context *ctx, struct uci_package *p,
    				struct uci_section *s, const char *optname,
    				int add, void *val, int len)
    {
    	struct uci_ptr ptr;
    
    	memset(&ptr, 0, sizeof(struct uci_ptr));
    	ptr.p = p;
    	ptr.s = s;
    	ptr.package = p->e.name;
    	ptr.section = s->e.name;
    	ptr.option = optname;
    	ptr.target = UCI_TYPE_OPTION;
    	ptr.flags |= UCI_LOOKUP_EXTENDED;
    	ptr.value = (char *)val;
    
    	if (add) {
    		dbg("config: add list option: %s\n", (char *)val);
    		uci_add_list(ctx, &ptr);
    	} else {
    		dbg("config: del list option: %s\n", (char *)val);
    		uci_del_list(ctx, &ptr);
    	}
    	uci_commit(ctx, &p, false);
    }
    
    int config_update(const char *confname, struct agent_config *cfg,
    			const char *section, const char *option,
    			int add,
    			void *value, int len)
    {
    	struct uci_context *ctx = NULL;
    	struct uci_package *pkg = NULL;
    	struct uci_element *e;
    
    	ctx = uci_alloc_context();
    	if (ctx && uci_load(ctx, confname, &pkg) != UCI_OK) {
    		dbg("config file '%s' not found!\n", confname);
    		free(ctx);
    		return -1;
    	}
    
    	uci_foreach_element(&pkg->sections, e) {
    		struct uci_section *s = uci_to_section(e);
    		struct uci_element *x, *tmp;
    		struct uci_option *op;
    
    		if (strcmp(s->type, section))
    			continue;
    
    		/* iter through matched 'section' for the 'option' */
    		uci_foreach_element_safe(&s->options, tmp, x) {
    			if (strcmp(x->name, option))
    				continue;
    
    			op = uci_to_option(x);
    			if (op->type == UCI_TYPE_LIST) {
    				uci_foreach_element(&op->v.list, x) {
    					if (!strncmp(x->name, value, len)) {
    						if (!add)
    							config_update_entry(ctx,
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    								pkg, s,
    								option, 0,
    								value, len);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    						goto out_exit;
    					}
    				}
    				/* add new exclude at end of list */
    				if (add)
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    					config_update_entry(ctx, pkg, s, option,
    							1, value, len);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    				goto out_exit;
    			}
    		}
    		/* 'option' name not present in 'section'
    		 * Create a new one at end of 'section'.
    		 */
    		if (add)
    			config_update_entry(ctx, pkg, s, option, 1, value, len);
    
    		goto out_exit;
    	}
    out_exit:
    	uci_free_context(ctx);
    	return 0;
    }
    
    int config_update2(const char *confname, struct agent_config *cfg,
    		const char *section_type,
    		const char *match_option,
    		const char *match_option_value,
    		const char *option,
    		int add,
    		void *value, int len)
    {
    	struct uci_context *ctx = NULL;
    	struct uci_package *pkg = NULL;
    	struct uci_element *e;
    
    	ctx = uci_alloc_context();
    	if (ctx && uci_load(ctx, confname, &pkg) != UCI_OK) {
    		dbg("config file '%s' not found!\n", confname);
    		free(ctx);
    		return -1;
    	}
    
    	uci_foreach_element(&pkg->sections, e) {
    		struct uci_section *s = uci_to_section(e);
    		struct uci_element *x, *tmp;
    		struct uci_option *op;
    		const char *optstring;
    
    		if (strcmp(s->type, section_type))
    			continue;
    
    		if (match_option && match_option_value) {
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    			optstring = uci_lookup_option_string(ctx, s,
    				match_option);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    			if (!optstring || strcmp(optstring, match_option_value))
    				continue;
    		}
    
    		/* iter through matched 'section' for the 'option' */
    		uci_foreach_element_safe(&s->options, tmp, x) {
    			if (strcmp(x->name, option))
    				continue;
    
    			op = uci_to_option(x);
    			if (op->type == UCI_TYPE_LIST) {
    				uci_foreach_element(&op->v.list, x) {
    					if (!strncmp(x->name, value, len)) {
    						if (!add) {
    							config_update_entry(ctx,
    								pkg, s, option,
    								0, value, len);
    						}
    
    						goto out_exit;
    					}
    				}
    				/* add new 'option' at end of list */
    				if (add) {
    					config_update_entry(ctx, pkg, s, option,
    								1, value, len);
    				}
    
    				goto out_exit;
    			}
    		}
    		/* 'option' name not present in 'section'
    		 * Create a new one at end of 'section'.
    		 */
    		if (add)
    			config_update_entry(ctx, pkg, s, option,
    							1, value, len);
    
    		goto out_exit;
    	}
    out_exit:
    	uci_free_context(ctx);
    	return 0;
    }
    
    
    #if 0
    struct config uci_config = {
    	.name = "uci",
    	.priv = uci_ctx;
    	.get = uci_get_config,
    	.set = uci_set_config,
    	.init = uci_setup,
    	.exit = uci_exit,
    };
    
    #define priv_get_config(priv)	container_of(priv, struct config, priv)
    
    void register_config(struct config *c)
    {
    	static struct uci_context *ctx;
    	static struct uci_package *pkg;
    	struct uci_element *e;
    	int ret = 0;
    
    	if (uci_ctx)
    		return priv_get_config(uci_ctx);
    
    	ctx = uci_alloc_context();
    	if (ctx) {
    		uci_ctx = ctx;
    		memcpy(c, &uci_config, sizeof(*cfg));
    	}
    	if (uci_load(ctx, "wifiagent", &pkg))
    		return -1;
    
    	uci_foreach_element(&pkg->sections, e) {
    		struct uci_section *s = uci_to_section(e);
    		const char *option_val;
    
    		if (strcmp(s->type, "wifiagent"))
    			continue;
    
    		option_val = uci_lookup_option_string(ctx, s, name);
    		if (option_val)
    			sprintf(val, "%s", option_val);
    		else
    			ret = -1;
    	}
    	uci_free_context(ctx);
    	return ret;
    
    }
    #endif
    
    
    static int cntlr_config_get_wifi_agent(struct agent_config *a,
    						struct uci_section *s)
    {
    	enum {
    		A_ENABLED,
    
    		A_PROFILE,
    
    		NUM_POLICIES
    	};
    	const struct uci_parse_option opts[] = {
    		{ .name = "enabled", .type = UCI_TYPE_STRING },
    
    		{ .name = "debug", .type = UCI_TYPE_STRING },
    
    		{ .name = "profile", .type = UCI_TYPE_STRING },
    
    	};
    	struct uci_option *tb[NUM_POLICIES];
    
    	uci_parse_section(s, opts, NUM_POLICIES, tb);
    
    	if (tb[A_ENABLED])
    		a->enabled = atoi(tb[A_ENABLED]->v.string) == 1 ? true : false;
    
    
    	if (tb[A_DEBUG]) {
    		a->debug_level = atoi(tb[A_DEBUG]->v.string);
    		if (verbose < a->debug_level)
    			verbose = a->debug_level;
    	}
    
    
    	if (tb[A_PROFILE])
    		a->profile = atoi(tb[A_PROFILE]->v.string);
    
    
    	return 0;
    }
    
    static int cntlr_config_get_bk_iface(struct agent_config *a,
    						struct uci_section *s)
    {
    	enum {
    		BK_IFNAME,
    		BK_ENABLED,
    		BK_ONBOARDED,
    		NUM_POLICIES
    	};
    	const struct uci_parse_option opts[] = {
    		{ .name = "ifname", .type = UCI_TYPE_STRING },
    		{ .name = "enabled", .type = UCI_TYPE_STRING },
    		{ .name = "onboarded", .type = UCI_TYPE_STRING }
    	};
    	struct uci_option *tb[NUM_POLICIES];
    	struct netif_bkcfg *bk;
    	const char *ifname;
    
    	uci_parse_section(s, opts, NUM_POLICIES, tb);
    
    	if (tb[BK_IFNAME]) {
    		ifname = tb[BK_IFNAME]->v.string;
    		bk = get_netif_bkcfg_by_name(a, ifname);
    		if (!bk) {
    			bk = create_backhaul_iface_config(a, ifname);
    			if (!bk) {
    				warn("%s: OOM!\n", __func__);
    				return -1;
    			}
    		} else {
    			warn("Duplicate 'bk-iface %s' config!! ignore\n",
    					ifname);
    		}
    	} else {
    		warn("No ifname in bk-iface section!\n");
    		return -1;
    	}
    
    	if (tb[BK_ENABLED])
    		bk->enabled = atoi(tb[BK_ENABLED]->v.string);
    
    	if (tb[BK_ONBOARDED])
    		bk->onboarded = atoi(tb[BK_ONBOARDED]->v.string);
    
    	return 0;
    }
    
    static int cntlr_config_get_fh_iface(struct agent_config *a,
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		struct uci_section *s)
    
    {
    	enum {
    		FH_IFNAME,
    		FH_STEER,
    		FH_EXCLUDE,
    		FH_EXCLUDE_BTM,
    		FH_ASSOC_CTRL,
    		FH_BTM_RETRY,
    		FH_BTM_RETRY_SECS,
    		FH_FLBK_LEGACY,
    		FH_STEER_LEGACY_RASSOC_SECS,
    		FH_STEER_LEGACY_RETRY_SECS,
    		FH_ASSOC_CTRL_SECS,
    		NUM_POLICIES,
    	};
    	const struct uci_parse_option opts[] = {
    		{ .name = "ifname", .type = UCI_TYPE_STRING },
    
    		{ .name = "steer", .type = UCI_TYPE_LIST },
    		{ .name = "exclude", .type = UCI_TYPE_LIST },
    		{ .name = "exclude_btm", .type = UCI_TYPE_LIST },
    		{ .name = "assoc_ctrl", .type = UCI_TYPE_LIST },
    
    		{ .name = "btm_retry", .type = UCI_TYPE_STRING },
    		{ .name = "btm_retry_secs", .type = UCI_TYPE_STRING },
    		{ .name = "fallback_legacy", .type = UCI_TYPE_STRING },
    		{ .name = "steer_legacy_reassoc_secs", .type = UCI_TYPE_STRING },
    		{ .name = "steer_legacy_retry_secs", .type = UCI_TYPE_STRING },
    		{ .name = "assoc_ctrl_secs", .type = UCI_TYPE_STRING }
    	};
    	struct uci_option *tb[NUM_POLICIES];
    	struct netif_fhcfg *fh;
    	const char *ifname;
    
    	uci_parse_section(s, opts, NUM_POLICIES, tb);
    
    	if (tb[FH_IFNAME]) {
    		ifname = tb[FH_IFNAME]->v.string;
    		fh = get_netif_fhcfg_by_name(a, ifname);
    		if (!fh) {
    			fh = create_fronthaul_iface_config(a, ifname);
    			if (!fh) {
    				warn("%s: OOM!\n", __func__);
    				return -1;
    			}
    		} else {
    			warn("Duplicate 'fh-iface %s' config!! ignore\n",
    					ifname);
    		}
    	} else {
    		warn("No ifname in fh-iface section!\n");
    		return -1;
    	}
    
    	if (tb[FH_STEER]) {
    		struct uci_element *xi;
    
    		dbg("Steer: param: ");
    		uci_foreach_element(&tb[FH_STEER]->v.list, xi) {
    			struct steer_policy *p = NULL;
    			struct steer_rule *r;
    
    			dbg("%s ", xi->name);
    			p = get_steer_policy_by_name(fh, xi->name);
    			if (!p) {
    				/* TODO? */
    				dbg("TODO!! steer before ifname\n");
    				continue;
    			}
    			p->enabled = true;
    			r = get_steer_rule_by_name(xi->name);
    			if (r)
    				r->enabled = true;
    		}
    		dbg("\n");
    	}
    
    	if (tb[FH_EXCLUDE]) {
    		struct uci_element *xi;
    
    		dbg("Steer: exclude: ");
    		uci_foreach_element(&tb[FH_EXCLUDE]->v.list, xi) {
    			dbg("%s ", xi->name);
    			stax_add_entry(&fh->steer_excludelist, xi->name);
    		}
    		dbg("\n");
    	}
    
    	if (tb[FH_EXCLUDE_BTM]) {
    		struct uci_element *xi;
    
    		dbg("Steer: exclude_btm: ");
    		uci_foreach_element(&tb[FH_EXCLUDE_BTM]->v.list, xi) {
    			dbg("%s ", xi->name);
    			stax_add_entry(&fh->steer_btm_excludelist, xi->name);
    		}
    		dbg("\n");
    	}
    
    	if (tb[FH_BTM_RETRY])
    		fh->steer_btm_retry = atoi(tb[FH_BTM_RETRY]->v.string);
    
    	if (tb[FH_BTM_RETRY_SECS])
    		fh->steer_btm_retry_secs = atoi(tb[FH_BTM_RETRY_SECS]->v.string);
    
    	if (tb[FH_FLBK_LEGACY])
    		fh->fallback_legacy = atoi(tb[FH_FLBK_LEGACY]->v.string);
    
    	if (tb[FH_STEER_LEGACY_RASSOC_SECS])
    		fh->steer_legacy_reassoc_secs =
    				atoi(tb[FH_STEER_LEGACY_RASSOC_SECS]->v.string);
    
    	if (tb[FH_STEER_LEGACY_RETRY_SECS])
    		fh->steer_legacy_retry_secs =
    				atoi(tb[FH_STEER_LEGACY_RETRY_SECS]->v.string);
    
    	if (tb[FH_ASSOC_CTRL_SECS])
    		fh->assoc_control_time =
    				atoi(tb[FH_ASSOC_CTRL_SECS]->v.string);
    
    	return 0;
    }
    
    static int cntlr_config_get_steer_param(struct agent_config *a,
    						struct uci_section *s)
    {
    	struct steer_rule *r;
    
    	dbg("Steer-param: %s\n", s->e.name);
    	r = get_steer_rule_by_name(s->e.name);
    	if (!r)
    		return -1;
    
    	dbg("Rule to handle steer-param '%s' available\n", s->e.name);
    	r->config(r, a, s);
    
    	return 0;
    }
    
    
    int agent_config_reload(struct agent_config *cfg)
    
    {
    	struct uci_context *ctx;
    	struct uci_package *pkg;
    	struct uci_element *e;
    
    
    	cfg->enabled = false;
    	cfg->runfreq = AGENT_RUN_AUTO;
    
    	INIT_LIST_HEAD(&cfg->fhlist);
    	INIT_LIST_HEAD(&cfg->bklist);
    
    
    	ctx = uci_alloc_context();
    	if (!ctx)
    		return -1;
    
    	if (uci_load(ctx, "agent", &pkg)) {
    		uci_free_context(ctx);
    		return -1;
    	}
    
    	uci_foreach_element(&pkg->sections, e) {
    		struct uci_section *s = uci_to_section(e);
    
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		if (!strcmp(s->type, "wifiagent"))
    
    			cntlr_config_get_wifi_agent(cfg, s);
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		else if (!strcmp(s->type, "fh-iface"))
    
    			cntlr_config_get_fh_iface(cfg, s);
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		else if (!strcmp(s->type, "bk-iface"))
    
    			cntlr_config_get_bk_iface(cfg, s);
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		else if (!strcmp(s->type, "steer"))
    
    			cntlr_config_get_steer_param(cfg, s);
    	}
    
    	uci_free_context(ctx);
    	return 0;
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    }