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
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
rv = uci_load(ctx, UCI_AGENT, &pkg);
if (rv)
return -1;
s = config_add_section(ctx, pkg, UCI_AGENT, UCI_BK_AGENT, "device",
device);
if (!s)
return -1;
if (band == BAND_2)
set_value(ctx, pkg, s, "band", "2", UCI_TYPE_STRING);
else if (band == BAND_5)
set_value(ctx, pkg, s, "band", "5", UCI_TYPE_STRING);
set_value(ctx, pkg, s, "ifname", ifname, UCI_TYPE_STRING);
uci_commit(ctx, &pkg, false);
uci_unload(ctx, pkg);
return 0;
}
int config_find_radio(struct agent_config *cfg, struct uci_context *ctx,
char *radio)
{
struct uci_package *pkg;
struct uci_section *s;
struct uci_element *e;
bool found = false;
int rv;
rv = uci_load(ctx, UCI_AGENT, &pkg);
if (rv)
return found;
s = config_get_section(ctx, pkg, "wifi-radio", "device", radio);
if (s)
found = true;
uci_unload(ctx, pkg);
return found;
}
static void wifi_radio_status_cb(struct ubus_request *req, int type,
struct blob_attr *msg)
{
struct blob_attr *tb[1];
static const struct blobmsg_policy ap_attr[1] = {
[0] = { .name = "band", .type = BLOBMSG_TYPE_STRING },
};
char band_str[8] = {0};
uint8_t *band = req->priv;
blobmsg_parse(ap_attr, 1, tb, blob_data(msg), blob_len(msg));
if (!tb[0])
return;
strncpy(band_str, blobmsg_data(tb[0]), sizeof(band_str));
if (!strncmp(band_str, "5GHz", sizeof(band_str)))
*band = BAND_5;
else if (!strncmp(band_str, "2.4GHz", sizeof(band_str)))
*band = BAND_2;
else
*band = BAND_UNKNOWN;
}
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
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
2131
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
void config_disable_bstas(struct agent_config *cfg)
{
struct uci_context *ctx = NULL;
struct uci_package *pkg;
struct uci_section *section;
struct netif_bkcfg *bk;
bool reload = false;
list_for_each_entry(bk, &cfg->bklist, list) {
/* only disable onboarded bsta */
// if (!bk->cfg->onboarded)
// continue;
/* disable from wireless config */
pkg = uci_load_pkg(&ctx, UCI_WIRELESS);
if (!pkg)
continue;
section = config_get_section(ctx, pkg, UCI_WLAN_IFACE, "ifname", bk->name);
if (!section) {
uci_unload(ctx, pkg);
continue;
}
set_value(ctx, pkg, section, "disabled", "1", UCI_TYPE_STRING);
uci_save(ctx, pkg);
reload = true;
}
if (reload) {
uci_commit(ctx, &pkg, false);
uci_unload(ctx, pkg);
}
list_for_each_entry(bk, &cfg->bklist, list) {
pkg = uci_load_pkg(&ctx, UCI_AGENT);
if (!pkg)
continue;
section = config_get_section(ctx, pkg, UCI_BK_AGENT, "ifname", bk->name);
if (!section) {
uci_unload(ctx, pkg);
continue;
}
set_value(ctx, pkg, section, "enabled", "0", UCI_TYPE_STRING);
uci_save(ctx, pkg);
bk->enabled = 0;
}
if (reload) {
uci_commit(ctx, &pkg, false);
uci_reload_services("wireless");
}
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;
}
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
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
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)
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
{
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;
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
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);
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
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
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;
}
void clean_radio_cfg(struct agent_config_radio *p)
{
list_del(&p->list);
free(p);
}
int clean_all_radios(struct agent_config *cfg)
{
struct agent_config_radio *p, *tmp;
list_for_each_entry_safe(p, tmp, &cfg->radiolist, list)
clean_radio_cfg(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);
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
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)
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
{
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;
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
}
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;
}
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
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;
}