Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
uci_free_context(ctx);
}
int config_disable_bsta(struct netif_bkcfg *bk)
{
struct uci_context *ctx = NULL;
struct uci_package *pkg;
struct uci_section *section;
int ret = -1;
/* disable mapagent bk-iface section */
pkg = uci_load_pkg(&ctx, UCI_AGENT);
if (!pkg)
return -1;
section = config_get_section(ctx, pkg, UCI_BK_AGENT, "ifname", bk->name);
if (!section)
goto out_pkg;
set_value(ctx, pkg, section, "enabled", "0", UCI_TYPE_STRING);
uci_save(ctx, pkg);
bk->enabled = 0;
uci_commit(ctx, &pkg, false);
uci_unload(ctx, pkg);
/* disable wireless config bsta wifi-iface section */
pkg = uci_load_pkg(&ctx, UCI_WIRELESS);
if (!pkg)
goto out;
section = config_get_section(ctx, pkg, UCI_WLAN_IFACE, "ifname", bk->name);
if (!section)
goto out_pkg;
set_value(ctx, pkg, section, "disabled", "1", UCI_TYPE_STRING);
uci_save(ctx, pkg);
uci_commit(ctx, &pkg, false);
ret = 0;
out_pkg:
uci_unload(ctx, pkg);
out:
uci_free_context(ctx);
return ret;
}
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
bool config_find_bsta_agent(struct agent_config *cfg, struct uci_context *ctx,
char *device)
{
struct uci_package *pkg;
struct uci_element *e;
struct uci_section *section = NULL;
int rv;
bool found = false;
rv = uci_load(ctx, UCI_AGENT, &pkg);
if (rv)
return found;
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
char *c_device;
if (strncmp(s->type, UCI_BK_AGENT, strlen(UCI_BK_AGENT)))
continue;
c_device = uci_lookup_option_string(ctx, s, "device");
if (!c_device)
continue;
if (strncmp(device, c_device, 16))
continue;
found = true;
break;
}
uci_unload(ctx, pkg);
return found;
}
struct uci_section *config_find_bsta_wireless(struct agent_config *cfg,
struct uci_context *ctx, struct uci_package *pkg, const char *device)
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
{
struct uci_element *e;
int rv;
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
char *c_device, *mode;
if (strncmp(s->type, UCI_WLAN_IFACE, strlen(UCI_WLAN_IFACE)))
continue;
c_device = uci_lookup_option_string(ctx, s, "device");
if (!c_device || strncmp(device, c_device, 16))
continue;
mode = uci_lookup_option_string(ctx, s, "mode");
if (!mode || strcmp(mode, "sta"))
continue;
return s;
}
return NULL;
}
int agent_config_prepare(struct agent_config *cfg)
{
// TODO: iterate through 'wifi-device' sections in wireless config.
// If corresponding 'wifi-radio <device-name>' section is not available
// in 'mapagent' config, create one. Check supported bands of the new
// wifi-device and add option 'band' to the wifi-radio section.
struct uci_context *ctx = NULL;
struct uci_package *pkg;
struct uci_element *e;
struct blob_buf bb = {0};
pkg = uci_load_pkg(&ctx, UCI_WIRELESS);
if (!pkg)
return -1;
blob_buf_init(&bb, 0);
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
struct uci_section *wl_s;
const char *device, *ifname;
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
uint8_t band = 0;
char obj_name[64] = {0};
int rv;
if (strncmp(s->type, UCI_WL_DEVICE, strlen(UCI_WL_DEVICE)))
continue;
device = s->e.name;
snprintf(obj_name, sizeof(obj_name), "wifi.radio.%s",
device);
rv = ubus_call(obj_name, "status", &bb,
wifi_radio_status_cb, &band);
if (rv)
continue;
if (!config_find_radio(cfg, ctx, device))
config_generate_radio(cfg, ctx, device, band);
if (config_find_bsta_agent(cfg, ctx, device))
continue;
wl_s = config_find_bsta_wireless(cfg, ctx, pkg, device);
if (!wl_s)
continue;
ifname = uci_lookup_option_string(ctx, wl_s, "ifname");
if (!ifname)
continue;
config_generate_bsta_agent(cfg, ctx, device, ifname, band);
}
blob_buf_free(&bb);
uci_unload(ctx, pkg);
uci_free_context(ctx);
return 0;
}
int agent_config_init(struct agent_config *cfg)
{
INIT_LIST_HEAD(&cfg->fhlist);
INIT_LIST_HEAD(&cfg->bklist);
INIT_LIST_HEAD(&cfg->radiolist);
INIT_LIST_HEAD(&cfg->steer_excludelist);
INIT_LIST_HEAD(&cfg->steer_btm_excludelist);
agent_config_prepare(cfg);
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
agent_config_reload(cfg);
return 0;
}
void clean_bk(struct netif_bkcfg *p)
{
list_del(&p->list);
free(p);
}
int clean_all_bk(struct agent_config *cfg)
{
struct netif_bkcfg *p, *tmp;
list_for_each_entry_safe(p, tmp, &cfg->bklist, list)
clean_bk(p);
return 0;
}
void clean_fh(struct netif_fhcfg *p)
{
list_del(&p->list);
free(p);
}
int clean_all_fh(struct agent_config *cfg)
{
struct netif_fhcfg *p, *tmp;
list_for_each_entry_safe(p, tmp, &cfg->fhlist, list)
clean_fh(p);
return 0;
}
int agent_config_clean(struct agent_config *cfg)
{
clean_all_fh(cfg);
clean_all_bk(cfg);
clean_steer_btm_excl(cfg);
clean_steer_excl(cfg);
if (cfg->pcfg)
free(cfg->pcfg);
int wifi_reorder_interfaces_by_device(struct agent_config *ac,
struct uci_context *ctx, struct uci_package *pkg, char *device)
{
int i, j = 0;
int devnum = 0;
char ifname[IFNAMSIZ] = {0};
enum {
W_IFNAME,
NUM_POLICIES
};
const struct uci_parse_option opts[] = {
{ .name = "ifname", .type = UCI_TYPE_STRING }
};
struct uci_option *tb[NUM_POLICIES];
struct uci_element *e;
trace("reordering interfaces for device %s\n", device);
devnum = get_device_num_from_name(device);
snprintf(ifname, IFNAMSIZ, "%s%d",
ac->netdev,
devnum);
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
for (i = 1; i < 16; i++) {
trace("iterating in search of %s\n", ifname);
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
if (strncmp(s->type, UCI_WLAN_IFACE,
strlen(UCI_WLAN_IFACE)))
continue;
uci_parse_section(s, opts, NUM_POLICIES, tb);
if (!tb[W_IFNAME])
continue;
if (strncmp(tb[W_IFNAME]->v.string, ifname, sizeof(ifname)))
continue;
trace("found interface %s, reordering to %d\n", ifname, j);
uci_reorder_section(ctx, s, j);
j++;
break;
}
snprintf(ifname, IFNAMSIZ, "%s%d%s%d",
ac->netdev,
devnum,
(ac->brcm_setup ? "." : "_"),
i);
}
return 0;
}
int wifi_reorder_interfaces(struct agent_config *ac)
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
{
struct uci_context *ctx;
struct uci_package *pkg;
struct uci_element *e;
ctx = uci_alloc_context();
if (!ctx)
return -1;
if (uci_load(ctx, UCI_WIRELESS, &pkg)) {
uci_free_context(ctx);
return -1;
}
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
if (strncmp(s->type, UCI_WL_DEVICE, strlen(UCI_WL_DEVICE)))
continue;
wifi_reorder_interfaces_by_device(ac, ctx, pkg, s->e.name);
}
uci_commit(ctx, &pkg, false);
uci_free_context(ctx);
return 0;
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
}
int uci_set_bridge(char *config, char *bridge, char *proto, char *ipaddress)
{
struct uci_context *ctx = NULL;
struct uci_package *pkg;
struct uci_element *e;
struct uci_section *section = NULL;
/** if bridge starts with br prefix, step past */
if (!strncmp(bridge, "br-", 3))
bridge += 3;
pkg = uci_load_pkg(&ctx, "network");
if (!pkg)
return -1;
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
if (strncmp(s->e.name, bridge, 16))
continue;
section = s;
break;
}
if (!section) {
struct uci_ptr ptr = {0};
ptr.p = pkg;
ptr.section = bridge;
ptr.value = "interface";
ptr.option = NULL;
uci_set(ctx, &ptr);
section = ptr.s;
}
set_value(ctx, pkg, section, "type", "bridge", UCI_TYPE_STRING);
set_value(ctx, pkg, section, "is_lan", "1", UCI_TYPE_STRING);
if (strlen(proto))
set_value(ctx, pkg, section, "proto", proto, UCI_TYPE_STRING);
if (!strcmp(proto, "static")) {
set_value(ctx, pkg, section, "ipaddr", ipaddress,
UCI_TYPE_STRING);
set_value(ctx, pkg, section, "netmask", "255.255.255.0",
UCI_TYPE_STRING);
}
uci_commit(ctx, &pkg, false);
uci_unload(ctx, pkg);
uci_free_context(ctx);
return false;
}
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
int uci_add_dhcp(char *iface)
{
struct uci_context *ctx = NULL;
struct uci_package *pkg;
struct uci_element *e;
struct uci_section *section = NULL;
struct uci_ptr ptr = {0};
/** if bridge starts with br prefix, step past */
if (!strncmp(iface, "br-", 3))
iface += 3;
pkg = uci_load_pkg(&ctx, "dhcp");
if (!pkg)
return -1;
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
if (strncmp(s->e.name, iface, 16))
continue;
trace("Existing section found for ifname %s\n", iface);
goto out;
}
trace("Adding DHCP section for ifname %s\n", iface);
ptr.p = pkg;
ptr.section = iface;
ptr.value = "dhcp";
ptr.option = NULL;
uci_set(ctx, &ptr);
section = ptr.s;
set_value(ctx, pkg, section, "interface", iface, UCI_TYPE_STRING);
set_value(ctx, pkg, section, "start", "100", UCI_TYPE_STRING);
set_value(ctx, pkg, section, "limit", "150", UCI_TYPE_STRING);
set_value(ctx, pkg, section, "leasetime", "1h", UCI_TYPE_STRING);
set_value(ctx, pkg, section, "dhcpv6", "server", UCI_TYPE_STRING);
set_value(ctx, pkg, section, "ra", "server", UCI_TYPE_STRING);
uci_commit(ctx, &pkg, false);
out:
uci_unload(ctx, pkg);
uci_free_context(ctx);
return false;
}
int uci_add_fw(struct agent_config *cfg, char *iface)
{
struct uci_context *ctx = NULL;
struct uci_package *pkg;
struct uci_element *e;
struct uci_section *section = NULL;
int rv;
/** if bridge starts with br prefix, step past */
if (!strncmp(iface, "br-", 3))
iface += 3;
pkg = uci_load_pkg(&ctx, "firewall");
if (!pkg)
return -1;
section = config_get_section(ctx, pkg, "zone", "name", iface);
if (!section) {
trace("No fw section found for %s\n", iface);
rv = uci_add_section(ctx, pkg, "zone", §ion);
if (rv)
goto out_pkg;
set_value(ctx, pkg, section, "name", iface, UCI_TYPE_STRING);
set_value(ctx, pkg, section, "network", iface, UCI_TYPE_LIST);
set_value(ctx, pkg, section, "input", "ACCEPT", UCI_TYPE_STRING);
set_value(ctx, pkg, section, "output", "ACCEPT", UCI_TYPE_STRING);
set_value(ctx, pkg, section, "forward", "ACCEPT", UCI_TYPE_STRING);
rv = uci_save(ctx, pkg);
if (rv)
goto out_pkg;
uci_commit(ctx, &pkg, false);
}
section = config_get_section(ctx, pkg, "forwarding", "src", iface);
if (!section) {
//section = config_add_section(ctx, pkg, "firewall", "forwarding", "src", iface);
//if (!section)
// goto out;
rv = uci_add_section(ctx, pkg, "forwarding", §ion);
if (rv)
goto out_pkg;
set_value(ctx, pkg, section, "src", iface, UCI_TYPE_STRING);
set_value(ctx, pkg, section, "dest", cfg->al_bridge, UCI_TYPE_STRING);
uci_commit(ctx, &pkg, false);
}
out_pkg:
uci_unload(ctx, pkg);
uci_free_context(ctx);
return false;
}