2008-01-13 20:53:27 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Small implementation of brctl for busybox.
|
|
|
|
*
|
2008-09-25 17:43:34 +05:30
|
|
|
* Copyright (C) 2008 by Bernhard Reutner-Fischer
|
2008-01-13 20:53:27 +05:30
|
|
|
*
|
2008-01-14 21:40:11 +05:30
|
|
|
* Some helper functions from bridge-utils are
|
|
|
|
* Copyright (C) 2000 Lennert Buytenhek
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2008-01-13 20:53:27 +05:30
|
|
|
*/
|
2016-11-23 13:35:14 +05:30
|
|
|
//config:config BRCTL
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "brctl (4.7 kb)"
|
2016-11-23 13:35:14 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Manage ethernet bridges.
|
|
|
|
//config: Supports addbr/delbr and addif/delif.
|
2016-11-23 13:35:14 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_BRCTL_FANCY
|
|
|
|
//config: bool "Fancy options"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on BRCTL
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Add support for extended option like:
|
|
|
|
//config: setageing, setfd, sethello, setmaxage,
|
|
|
|
//config: setpathcost, setportprio, setbridgeprio,
|
|
|
|
//config: stp
|
|
|
|
//config: This adds about 600 bytes.
|
2016-11-23 13:35:14 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_BRCTL_SHOW
|
|
|
|
//config: bool "Support show"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on BRCTL && FEATURE_BRCTL_FANCY
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Add support for option which prints the current config:
|
|
|
|
//config: show
|
2016-11-23 13:35:14 +05:30
|
|
|
|
2017-08-06 23:44:02 +05:30
|
|
|
//applet:IF_BRCTL(APPLET_NOEXEC(brctl, brctl, BB_DIR_USR_SBIN, BB_SUID_DROP, brctl))
|
2016-11-23 13:35:14 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_BRCTL) += brctl.o
|
2011-04-11 06:59:49 +05:30
|
|
|
|
|
|
|
//usage:#define brctl_trivial_usage
|
2019-04-13 17:28:06 +05:30
|
|
|
//usage: "COMMAND [BRIDGE [ARGS]]"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define brctl_full_usage "\n\n"
|
2019-04-13 19:18:31 +05:30
|
|
|
//usage: "Manage ethernet bridges"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\nCommands:"
|
|
|
|
//usage: IF_FEATURE_BRCTL_SHOW(
|
2019-04-13 17:28:06 +05:30
|
|
|
//usage: "\n show [BRIDGE]... Show bridges"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: )
|
|
|
|
//usage: "\n addbr BRIDGE Create BRIDGE"
|
|
|
|
//usage: "\n delbr BRIDGE Delete BRIDGE"
|
|
|
|
//usage: "\n addif BRIDGE IFACE Add IFACE to BRIDGE"
|
|
|
|
//usage: "\n delif BRIDGE IFACE Delete IFACE from BRIDGE"
|
|
|
|
//usage: IF_FEATURE_BRCTL_FANCY(
|
2019-10-12 23:21:46 +05:30
|
|
|
//usage: "\n showmacs BRIDGE List MAC addresses"
|
2019-10-12 22:54:38 +05:30
|
|
|
//usage: "\n showstp BRIDGE Show STP info"
|
2019-10-12 23:21:46 +05:30
|
|
|
//usage: "\n stp BRIDGE 1/yes/on|0/no/off Set STP on/off"
|
2019-04-13 17:28:06 +05:30
|
|
|
//usage: "\n setageing BRIDGE SECONDS Set ageing time"
|
|
|
|
//usage: "\n setfd BRIDGE SECONDS Set bridge forward delay"
|
|
|
|
//usage: "\n sethello BRIDGE SECONDS Set hello time"
|
|
|
|
//usage: "\n setmaxage BRIDGE SECONDS Set max message age"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n setbridgeprio BRIDGE PRIO Set bridge priority"
|
2019-04-13 17:28:06 +05:30
|
|
|
//usage: "\n setportprio BRIDGE IFACE PRIO Set port priority"
|
|
|
|
//usage: "\n setpathcost BRIDGE IFACE COST Set path cost"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: )
|
2019-04-13 17:28:06 +05:30
|
|
|
// Not yet implemented:
|
2019-10-12 23:21:46 +05:30
|
|
|
// hairpin BRIDGE IFACE on|off Set hairpin on/off
|
2019-10-11 02:30:19 +05:30
|
|
|
|
2008-01-13 20:53:27 +05:30
|
|
|
#include "libbb.h"
|
2019-04-12 22:22:31 +05:30
|
|
|
#include "common_bufsiz.h"
|
2008-01-13 20:53:27 +05:30
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
|
2009-02-01 01:38:21 +05:30
|
|
|
#ifndef SIOCBRADDBR
|
|
|
|
# define SIOCBRADDBR BRCTL_ADD_BRIDGE
|
|
|
|
#endif
|
|
|
|
#ifndef SIOCBRDELBR
|
|
|
|
# define SIOCBRDELBR BRCTL_DEL_BRIDGE
|
|
|
|
#endif
|
|
|
|
#ifndef SIOCBRADDIF
|
|
|
|
# define SIOCBRADDIF BRCTL_ADD_IF
|
|
|
|
#endif
|
|
|
|
#ifndef SIOCBRDELIF
|
|
|
|
# define SIOCBRDELIF BRCTL_DEL_IF
|
|
|
|
#endif
|
|
|
|
|
2008-01-14 00:13:50 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_FANCY
|
2019-04-13 17:47:55 +05:30
|
|
|
static unsigned str_to_jiffies(const char *time_str)
|
2008-01-14 00:13:50 +05:30
|
|
|
{
|
2019-04-13 17:47:55 +05:30
|
|
|
double dd;
|
2010-03-16 17:11:29 +05:30
|
|
|
char *endptr;
|
2021-03-23 18:20:02 +05:30
|
|
|
//TODO: needs setlocale(LC_NUMERIC, "C")?
|
2019-04-13 17:47:55 +05:30
|
|
|
dd = /*bb_*/strtod(time_str, &endptr);
|
|
|
|
if (endptr == time_str || dd < 0)
|
2015-10-14 16:51:01 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec");
|
2008-01-14 00:13:50 +05:30
|
|
|
|
2019-04-13 17:47:55 +05:30
|
|
|
dd *= 100;
|
|
|
|
/* For purposes of brctl,
|
|
|
|
* capping SECONDS by ~20 million seconds is quite enough:
|
|
|
|
*/
|
|
|
|
if (dd > INT_MAX)
|
|
|
|
dd = INT_MAX;
|
2008-01-14 00:13:50 +05:30
|
|
|
|
2019-04-13 17:47:55 +05:30
|
|
|
return dd;
|
2008-01-14 00:13:50 +05:30
|
|
|
}
|
2008-01-13 20:53:27 +05:30
|
|
|
#endif
|
|
|
|
|
2019-04-12 22:22:31 +05:30
|
|
|
#define filedata bb_common_bufsiz1
|
2019-04-13 17:28:06 +05:30
|
|
|
|
2020-06-23 06:43:55 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_SHOW || ENABLE_FEATURE_BRCTL_FANCY
|
2019-04-12 22:22:31 +05:30
|
|
|
static int read_file(const char *name)
|
|
|
|
{
|
|
|
|
int n = open_read_close(name, filedata, COMMON_BUFSIZE - 1);
|
|
|
|
if (n < 0) {
|
|
|
|
filedata[0] = '\0';
|
|
|
|
} else {
|
|
|
|
filedata[n] = '\0';
|
|
|
|
if (n != 0 && filedata[n - 1] == '\n')
|
|
|
|
filedata[--n] = '\0';
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2020-06-23 06:43:55 +05:30
|
|
|
#endif
|
2019-04-12 22:22:31 +05:30
|
|
|
|
2020-06-23 06:43:55 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_SHOW
|
2019-04-12 22:22:31 +05:30
|
|
|
/* NB: we are in /sys/class/net
|
|
|
|
*/
|
|
|
|
static int show_bridge(const char *name, int need_hdr)
|
|
|
|
{
|
2019-04-13 17:28:06 +05:30
|
|
|
/* Output:
|
|
|
|
*bridge name bridge id STP enabled interfaces
|
|
|
|
*br0 8000.000000000000 no eth0
|
|
|
|
*/
|
2019-10-12 22:54:38 +05:30
|
|
|
char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 8];
|
2019-04-12 22:22:31 +05:30
|
|
|
int tabs;
|
|
|
|
DIR *ifaces;
|
|
|
|
struct dirent *ent;
|
|
|
|
char *sfx;
|
|
|
|
|
2019-04-13 17:28:06 +05:30
|
|
|
#if IFNAMSIZ == 16
|
|
|
|
sfx = pathbuf + sprintf(pathbuf, "%.16s/bridge/", name);
|
|
|
|
#else
|
|
|
|
sfx = pathbuf + sprintf(pathbuf, "%.*s/bridge/", (int)IFNAMSIZ, name);
|
|
|
|
#endif
|
2019-04-12 22:22:31 +05:30
|
|
|
strcpy(sfx, "bridge_id");
|
|
|
|
if (read_file(pathbuf) < 0)
|
|
|
|
return -1; /* this iface is not a bridge */
|
|
|
|
|
|
|
|
if (need_hdr)
|
|
|
|
puts("bridge name\tbridge id\t\tSTP enabled\tinterfaces");
|
2019-10-11 02:30:19 +05:30
|
|
|
printf("%s\t\t%s\t", name, filedata);
|
2019-04-12 22:22:31 +05:30
|
|
|
|
|
|
|
strcpy(sfx, "stp_state");
|
|
|
|
read_file(pathbuf);
|
|
|
|
if (LONE_CHAR(filedata, '0'))
|
|
|
|
strcpy(filedata, "no");
|
|
|
|
else
|
|
|
|
if (LONE_CHAR(filedata, '1'))
|
|
|
|
strcpy(filedata, "yes");
|
2021-02-04 01:17:14 +05:30
|
|
|
fputs_stdout(filedata);
|
2019-04-12 22:22:31 +05:30
|
|
|
|
2019-10-15 16:01:54 +05:30
|
|
|
/* sfx points past "BR/bridge/", turn it into "BR/brif": */
|
|
|
|
sfx[-4] = 'f'; sfx[-3] = '\0';
|
2019-04-12 22:22:31 +05:30
|
|
|
tabs = 0;
|
|
|
|
ifaces = opendir(pathbuf);
|
|
|
|
if (ifaces) {
|
|
|
|
while ((ent = readdir(ifaces)) != NULL) {
|
2019-04-13 17:28:06 +05:30
|
|
|
if (DOT_OR_DOTDOT(ent->d_name))
|
|
|
|
continue; /* . or .. */
|
2019-04-12 22:22:31 +05:30
|
|
|
if (tabs)
|
|
|
|
printf("\t\t\t\t\t");
|
|
|
|
else
|
|
|
|
tabs = 1;
|
|
|
|
printf("\t\t%s\n", ent->d_name);
|
|
|
|
}
|
|
|
|
closedir(ifaces);
|
|
|
|
}
|
|
|
|
if (!tabs) /* bridge has no interfaces */
|
|
|
|
bb_putchar('\n');
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-08 16:05:06 +05:30
|
|
|
#endif
|
2008-01-14 00:13:50 +05:30
|
|
|
|
2019-06-08 16:05:06 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_FANCY
|
2019-04-13 17:47:55 +05:30
|
|
|
static void write_uint(const char *name, const char *leaf, unsigned val)
|
2019-04-13 17:28:06 +05:30
|
|
|
{
|
|
|
|
char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 32];
|
|
|
|
int fd, n;
|
|
|
|
|
|
|
|
#if IFNAMSIZ == 16
|
|
|
|
sprintf(pathbuf, "%.16s/%s", name, leaf);
|
|
|
|
#else
|
|
|
|
sprintf(pathbuf, "%.*s/%s", (int)IFNAMSIZ, name, leaf);
|
|
|
|
#endif
|
|
|
|
fd = xopen(pathbuf, O_WRONLY);
|
2019-04-13 17:47:55 +05:30
|
|
|
n = sprintf(filedata, "%u\n", val);
|
2019-04-13 17:28:06 +05:30
|
|
|
if (write(fd, filedata, n) < 0)
|
|
|
|
bb_simple_perror_msg_and_die(name);
|
2019-10-12 22:54:38 +05:30
|
|
|
/* So far all callers exit very soon after calling us.
|
|
|
|
* Do not bother closing fd (unless debugging):
|
|
|
|
*/
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
2019-04-13 17:28:06 +05:30
|
|
|
}
|
2019-09-15 21:34:49 +05:30
|
|
|
|
|
|
|
struct fdb_entry {
|
|
|
|
uint8_t mac_addr[6];
|
|
|
|
uint8_t port_no;
|
|
|
|
uint8_t is_local;
|
|
|
|
uint32_t ageing_timer_value;
|
|
|
|
uint8_t port_hi;
|
|
|
|
uint8_t pad0;
|
|
|
|
uint16_t unused;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int compare_fdbs(const void *_f0, const void *_f1)
|
|
|
|
{
|
|
|
|
const struct fdb_entry *f0 = _f0;
|
|
|
|
const struct fdb_entry *f1 = _f1;
|
|
|
|
|
|
|
|
return memcmp(f0->mac_addr, f1->mac_addr, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t read_bridge_forward_db(const char *name, struct fdb_entry **_fdb)
|
|
|
|
{
|
2019-10-12 22:54:38 +05:30
|
|
|
char pathbuf[IFNAMSIZ + sizeof("/brforward") + 8];
|
2019-09-15 21:34:49 +05:30
|
|
|
struct fdb_entry *fdb;
|
|
|
|
size_t nentries;
|
|
|
|
int fd;
|
|
|
|
ssize_t cc;
|
|
|
|
|
2019-10-12 22:54:38 +05:30
|
|
|
#if IFNAMSIZ == 16
|
|
|
|
sprintf(pathbuf, "%.16s/brforward", name);
|
|
|
|
#else
|
|
|
|
sprintf(pathbuf, "%.*s/brforward", (int)IFNAMSIZ, name);
|
|
|
|
#endif
|
|
|
|
fd = open(pathbuf, O_RDONLY);
|
2019-09-15 21:34:49 +05:30
|
|
|
if (fd < 0)
|
|
|
|
bb_error_msg_and_die("bridge %s does not exist", name);
|
|
|
|
|
|
|
|
fdb = NULL;
|
|
|
|
nentries = 0;
|
|
|
|
for (;;) {
|
|
|
|
fdb = xrealloc_vector(fdb, 4, nentries);
|
|
|
|
cc = full_read(fd, &fdb[nentries], sizeof(*fdb));
|
|
|
|
if (cc == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cc != sizeof(*fdb)) {
|
|
|
|
bb_perror_msg_and_die("can't read bridge %s forward db", name);
|
|
|
|
}
|
|
|
|
++nentries;
|
|
|
|
}
|
|
|
|
|
2019-10-12 22:54:38 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
2019-09-15 21:34:49 +05:30
|
|
|
|
|
|
|
qsort(fdb, nentries, sizeof(*fdb), compare_fdbs);
|
|
|
|
|
|
|
|
*_fdb = fdb;
|
|
|
|
return nentries;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_bridge_macs(const char *name)
|
|
|
|
{
|
|
|
|
struct fdb_entry *fdb;
|
|
|
|
size_t nentries;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
nentries = read_bridge_forward_db(name, &fdb);
|
|
|
|
|
|
|
|
printf("port no\tmac addr\t\tis local?\tageing timer\n");
|
|
|
|
for (i = 0; i < nentries; ++i) {
|
|
|
|
const struct fdb_entry *f = &fdb[i];
|
2019-10-12 22:54:38 +05:30
|
|
|
unsigned tv_sec = f->ageing_timer_value / 100;
|
|
|
|
unsigned tv_csec = f->ageing_timer_value % 100;
|
2019-09-15 21:34:49 +05:30
|
|
|
printf("%3u\t"
|
|
|
|
"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\t"
|
|
|
|
"%s\t\t"
|
|
|
|
"%4u.%.2u\n",
|
|
|
|
f->port_no,
|
|
|
|
f->mac_addr[0], f->mac_addr[1], f->mac_addr[2],
|
|
|
|
f->mac_addr[3], f->mac_addr[4], f->mac_addr[5],
|
|
|
|
(f->is_local ? "yes" : "no"),
|
2019-10-12 22:54:38 +05:30
|
|
|
tv_sec, tv_csec
|
2019-09-15 21:34:49 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-12 22:54:38 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
free(fdb);
|
2019-09-15 21:34:49 +05:30
|
|
|
}
|
2019-10-11 02:30:19 +05:30
|
|
|
|
|
|
|
static void show_bridge_timer(const char *msg)
|
|
|
|
{
|
2019-10-12 22:54:38 +05:30
|
|
|
unsigned long long centisec = xstrtoull(filedata, 0);
|
|
|
|
unsigned tv_sec = centisec / 100;
|
|
|
|
unsigned tv_csec = centisec % 100;
|
|
|
|
printf("%s%4u.%.2u", msg, tv_sec, tv_csec);
|
2019-10-11 02:30:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static const char *show_bridge_state(unsigned state)
|
|
|
|
{
|
|
|
|
/* See linux/if_bridge.h, BR_STATE_ constants */
|
2019-10-12 22:54:38 +05:30
|
|
|
static const char state_names[] ALIGN1 =
|
2019-10-11 02:30:19 +05:30
|
|
|
"disabled\0" //BR_STATE_DISABLED 0
|
|
|
|
"listening\0" //BR_STATE_LISTENING 1
|
|
|
|
"learning\0" //BR_STATE_LEARNING 2
|
|
|
|
"forwarding\0" //BR_STATE_FORWARDING 3
|
|
|
|
"blocking" //BR_STATE_BLOCKING 4
|
|
|
|
;
|
|
|
|
if (state < 5)
|
|
|
|
return nth_string(state_names, state);
|
|
|
|
return utoa(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printf_xstrtou(const char *fmt)
|
|
|
|
{
|
|
|
|
printf(fmt, xstrtou(filedata, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_bridge_port(const char *name)
|
|
|
|
{
|
2019-10-12 22:54:38 +05:30
|
|
|
char pathbuf[IFNAMSIZ + sizeof("/brport/forward_delay_timer") + 8];
|
2019-10-11 02:30:19 +05:30
|
|
|
char *sfx;
|
|
|
|
|
|
|
|
#if IFNAMSIZ == 16
|
|
|
|
sfx = pathbuf + sprintf(pathbuf, "%.16s/brport/", name);
|
|
|
|
#else
|
|
|
|
sfx = pathbuf + sprintf(pathbuf, "%.*s/brport/", (int)IFNAMSIZ, name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
strcpy(sfx, "port_no");
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf("%s (%u)\n", name, xstrtou(filedata, 0));
|
|
|
|
|
|
|
|
strcpy(sfx + 5, "id"); // "port_id"
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf_xstrtou(" port id\t\t%.4x");
|
|
|
|
|
|
|
|
strcpy(sfx, "state");
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf("\t\t\tstate\t\t%15s\n", show_bridge_state(xstrtou(filedata, 0)));
|
|
|
|
|
|
|
|
strcpy(sfx, "designated_root");
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf(" designated root\t%s", filedata);
|
|
|
|
|
|
|
|
strcpy(sfx, "path_cost");
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf_xstrtou("\tpath cost\t\t%4u\n");
|
|
|
|
|
|
|
|
strcpy(sfx, "designated_bridge");
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf(" designated bridge\t%s", filedata);
|
|
|
|
|
|
|
|
strcpy(sfx, "message_age_timer");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\tmessage age timer\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "designated_port");
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf_xstrtou("\n designated port\t%.4x");
|
|
|
|
|
|
|
|
strcpy(sfx, "forward_delay_timer");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\t\t\tforward delay timer\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "designated_cost");
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf_xstrtou("\n designated cost\t%4u");
|
|
|
|
|
|
|
|
strcpy(sfx, "hold_timer");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\t\t\thold timer\t\t");
|
|
|
|
|
|
|
|
printf("\n flags\t\t\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "config_pending");
|
|
|
|
read_file(pathbuf);
|
|
|
|
if (!LONE_CHAR(filedata, '0'))
|
|
|
|
printf("CONFIG_PENDING ");
|
|
|
|
|
|
|
|
strcpy(sfx, "change_ack");
|
|
|
|
read_file(pathbuf);
|
|
|
|
if (!LONE_CHAR(filedata, '0'))
|
|
|
|
printf("TOPOLOGY_CHANGE_ACK ");
|
|
|
|
|
|
|
|
strcpy(sfx, "hairpin_mode");
|
|
|
|
read_file(pathbuf);
|
|
|
|
if (!LONE_CHAR(filedata, '0'))
|
|
|
|
printf_xstrtou("\n hairpin mode\t\t%4u");
|
|
|
|
|
|
|
|
printf("\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_bridge_stp(const char *name)
|
|
|
|
{
|
2019-10-12 22:54:38 +05:30
|
|
|
char pathbuf[IFNAMSIZ + sizeof("/bridge/topology_change_timer") + 8];
|
2019-10-11 02:30:19 +05:30
|
|
|
char *sfx;
|
|
|
|
|
|
|
|
#if IFNAMSIZ == 16
|
|
|
|
sfx = pathbuf + sprintf(pathbuf, "%.16s/bridge/", name);
|
|
|
|
#else
|
|
|
|
sfx = pathbuf + sprintf(pathbuf, "%.*s/bridge/", (int)IFNAMSIZ, name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
strcpy(sfx, "bridge_id");
|
|
|
|
if (read_file(pathbuf) < 0)
|
|
|
|
bb_error_msg_and_die("bridge %s does not exist", name);
|
|
|
|
|
|
|
|
printf("%s\n"
|
|
|
|
" bridge id\t\t%s", name, filedata);
|
|
|
|
|
|
|
|
strcpy(sfx, "root_id");
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf("\n designated root\t%s", filedata);
|
|
|
|
|
|
|
|
strcpy(sfx + 5, "port"); // "root_port"
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf_xstrtou("\n root port\t\t%4u\t\t\t");
|
|
|
|
|
|
|
|
strcpy(sfx + 6, "ath_cost"); // "root_path_cost"
|
|
|
|
read_file(pathbuf);
|
|
|
|
printf_xstrtou("path cost\t\t%4u\n");
|
|
|
|
|
|
|
|
strcpy(sfx, "max_age");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer(" max age\t\t");
|
|
|
|
show_bridge_timer("\t\t\tbridge max age\t\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "hello_time");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\n hello time\t\t");
|
|
|
|
show_bridge_timer("\t\t\tbridge hello time\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "forward_delay");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\n forward delay\t\t");
|
|
|
|
show_bridge_timer("\t\t\tbridge forward delay\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "ageing_time");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\n ageing time\t\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "hello_timer");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\n hello timer\t\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "tcn_timer");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\t\t\ttcn timer\t\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "topology_change_timer");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\n topology change timer\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "gc_timer");
|
|
|
|
read_file(pathbuf);
|
|
|
|
show_bridge_timer("\t\t\tgc timer\t\t");
|
|
|
|
|
|
|
|
printf("\n flags\t\t\t");
|
|
|
|
|
|
|
|
strcpy(sfx, "topology_change");
|
|
|
|
read_file(pathbuf);
|
|
|
|
if (!LONE_CHAR(filedata, '0'))
|
|
|
|
printf("TOPOLOGY_CHANGE ");
|
|
|
|
|
|
|
|
strcpy(sfx, "topology_change_detected");
|
|
|
|
read_file(pathbuf);
|
|
|
|
if (!LONE_CHAR(filedata, '0'))
|
|
|
|
printf("TOPOLOGY_CHANGE_DETECTED ");
|
|
|
|
printf("\n\n\n");
|
|
|
|
|
2019-10-12 23:12:37 +05:30
|
|
|
/* Show bridge ports */
|
|
|
|
{
|
|
|
|
DIR *ifaces;
|
|
|
|
|
2019-10-15 16:01:54 +05:30
|
|
|
/* sfx points past "BR/bridge/", turn it into "BR/brif": */
|
|
|
|
sfx[-4] = 'f'; sfx[-3] = '\0';
|
2019-10-12 23:12:37 +05:30
|
|
|
ifaces = opendir(pathbuf);
|
|
|
|
if (ifaces) {
|
|
|
|
struct dirent *ent;
|
|
|
|
while ((ent = readdir(ifaces)) != NULL) {
|
|
|
|
if (DOT_OR_DOTDOT(ent->d_name))
|
|
|
|
continue; /* . or .. */
|
|
|
|
show_bridge_port(ent->d_name);
|
|
|
|
}
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
closedir(ifaces);
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 02:30:19 +05:30
|
|
|
}
|
2019-06-08 16:05:06 +05:30
|
|
|
#endif
|
2019-04-13 17:28:06 +05:30
|
|
|
|
2008-03-17 15:03:45 +05:30
|
|
|
int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int brctl_main(int argc UNUSED_PARAM, char **argv)
|
2008-01-13 20:53:27 +05:30
|
|
|
{
|
|
|
|
static const char keywords[] ALIGN1 =
|
|
|
|
"addbr\0" "delbr\0" "addif\0" "delif\0"
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_FEATURE_BRCTL_FANCY(
|
2008-01-14 21:40:11 +05:30
|
|
|
"stp\0"
|
2019-10-11 02:30:19 +05:30
|
|
|
"showstp\0"
|
2008-01-14 00:13:50 +05:30
|
|
|
"setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
|
2019-04-13 17:28:06 +05:30
|
|
|
"setpathcost\0" "setportprio\0"
|
|
|
|
"setbridgeprio\0"
|
2019-09-15 21:34:49 +05:30
|
|
|
"showmacs\0"
|
2008-01-14 00:13:50 +05:30
|
|
|
)
|
2012-09-25 17:36:01 +05:30
|
|
|
IF_FEATURE_BRCTL_SHOW("show\0");
|
2008-01-14 00:13:50 +05:30
|
|
|
enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_FEATURE_BRCTL_FANCY(,
|
2013-01-14 20:27:44 +05:30
|
|
|
ARG_stp,
|
2019-10-11 02:30:19 +05:30
|
|
|
ARG_showstp,
|
2013-01-14 20:27:44 +05:30
|
|
|
ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
|
2019-04-13 17:28:06 +05:30
|
|
|
ARG_setpathcost, ARG_setportprio,
|
2019-09-15 21:34:49 +05:30
|
|
|
ARG_setbridgeprio,
|
|
|
|
ARG_showmacs
|
2008-01-14 00:13:50 +05:30
|
|
|
)
|
2012-09-25 17:36:01 +05:30
|
|
|
IF_FEATURE_BRCTL_SHOW(, ARG_show)
|
2008-03-17 15:03:45 +05:30
|
|
|
};
|
2019-10-12 23:12:37 +05:30
|
|
|
int key;
|
|
|
|
char *br;
|
2008-03-17 15:03:45 +05:30
|
|
|
|
2008-01-13 20:53:27 +05:30
|
|
|
argv++;
|
2019-04-12 22:22:31 +05:30
|
|
|
if (!*argv) {
|
|
|
|
/* bare "brctl" shows --help */
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
xchdir("/sys/class/net");
|
|
|
|
|
2019-10-12 23:12:37 +05:30
|
|
|
key = index_in_strings(keywords, *argv);
|
|
|
|
if (key == -1) /* no match found in keywords array, bail out. */
|
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
|
|
|
|
argv++;
|
2008-04-06 12:47:02 +05:30
|
|
|
|
2008-01-13 20:53:27 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_SHOW
|
2019-10-12 23:12:37 +05:30
|
|
|
if (key == ARG_show) { /* show [BR]... */
|
|
|
|
DIR *net;
|
|
|
|
struct dirent *ent;
|
|
|
|
int need_hdr = 1;
|
|
|
|
int exitcode = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
if (*argv) {
|
|
|
|
/* "show BR1 BR2 BR3" */
|
|
|
|
do {
|
|
|
|
if (show_bridge(*argv, need_hdr) >= 0) {
|
|
|
|
need_hdr = 0;
|
|
|
|
} else {
|
|
|
|
bb_error_msg("bridge %s does not exist", *argv);
|
2019-04-13 17:28:06 +05:30
|
|
|
//TODO: if device exists, but is not a BR, brctl from bridge-utils 1.6
|
|
|
|
//says this instead: "device eth0 is not a bridge"
|
2019-10-12 23:12:37 +05:30
|
|
|
exitcode = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
} while (*++argv != NULL);
|
2019-04-12 22:22:31 +05:30
|
|
|
return exitcode;
|
2008-01-13 20:53:27 +05:30
|
|
|
}
|
2019-10-12 23:12:37 +05:30
|
|
|
|
|
|
|
/* "show" (if no ifaces, shows nothing, not even header) */
|
|
|
|
net = xopendir(".");
|
|
|
|
while ((ent = readdir(net)) != NULL) {
|
|
|
|
if (DOT_OR_DOTDOT(ent->d_name))
|
|
|
|
continue; /* . or .. */
|
|
|
|
if (show_bridge(ent->d_name, need_hdr) >= 0)
|
|
|
|
need_hdr = 0;
|
|
|
|
}
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
closedir(net);
|
|
|
|
return exitcode;
|
|
|
|
}
|
2008-01-13 20:53:27 +05:30
|
|
|
#endif
|
2008-04-06 12:47:02 +05:30
|
|
|
|
2019-10-12 23:21:46 +05:30
|
|
|
if (!*argv) /* All of the below need at least one argument */
|
2019-10-12 23:12:37 +05:30
|
|
|
bb_show_usage();
|
2008-04-06 12:47:02 +05:30
|
|
|
|
2019-10-12 23:12:37 +05:30
|
|
|
br = *argv++;
|
2008-04-06 12:47:02 +05:30
|
|
|
|
2019-10-12 23:12:37 +05:30
|
|
|
if (key == ARG_addbr || key == ARG_delbr) {
|
|
|
|
/* brctl from bridge-utils 1.6 still uses ioctl
|
|
|
|
* for SIOCBRADDBR / SIOCBRDELBR, not /sys accesses
|
|
|
|
*/
|
|
|
|
int fd = xsocket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
ioctl_or_perror_and_die(fd,
|
|
|
|
key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
|
|
|
|
br, "bridge %s", br
|
|
|
|
);
|
|
|
|
//close(fd);
|
|
|
|
//goto done;
|
|
|
|
/* bridge-utils 1.6 simply ignores trailing args:
|
|
|
|
* "brctl addbr BR1 ARGS" ignores ARGS
|
|
|
|
*/
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2019-09-15 21:34:49 +05:30
|
|
|
|
2020-06-23 06:16:53 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_FANCY
|
2019-10-12 23:12:37 +05:30
|
|
|
if (key == ARG_showmacs) {
|
|
|
|
show_bridge_macs(br);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
if (key == ARG_showstp) {
|
|
|
|
show_bridge_stp(br);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-06-23 06:16:53 +05:30
|
|
|
#endif
|
2019-10-12 23:12:37 +05:30
|
|
|
|
2019-10-12 23:21:46 +05:30
|
|
|
if (!*argv) /* All of the below need at least two arguments */
|
2019-10-12 23:12:37 +05:30
|
|
|
bb_show_usage();
|
2008-04-06 12:47:02 +05:30
|
|
|
|
2008-01-14 00:13:50 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_FANCY
|
2019-10-12 23:12:37 +05:30
|
|
|
if (key == ARG_stp) {
|
|
|
|
static const char no_yes[] ALIGN1 =
|
|
|
|
"0\0" "off\0" "n\0" "no\0" /* 0 .. 3 */
|
|
|
|
"1\0" "on\0" "y\0" "yes\0"; /* 4 .. 7 */
|
|
|
|
int onoff = index_in_strings(no_yes, *argv);
|
|
|
|
if (onoff < 0)
|
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
|
|
|
|
onoff = (unsigned)onoff / 4;
|
|
|
|
write_uint(br, "bridge/stp_state", onoff);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2019-04-13 17:28:06 +05:30
|
|
|
|
2019-10-12 23:12:37 +05:30
|
|
|
if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
|
|
|
|
/* setageing BR N: "N*100\n" to /sys/class/net/BR/bridge/ageing_time
|
|
|
|
* setfd BR N: "N*100\n" to /sys/class/net/BR/bridge/forward_delay
|
|
|
|
* sethello BR N: "N*100\n" to /sys/class/net/BR/bridge/hello_time
|
|
|
|
* setmaxage BR N: "N*100\n" to /sys/class/net/BR/bridge/max_age
|
|
|
|
*/
|
|
|
|
write_uint(br,
|
|
|
|
nth_string(
|
|
|
|
"bridge/ageing_time" "\0" /* ARG_setageing */
|
|
|
|
"bridge/forward_delay""\0" /* ARG_setfd */
|
|
|
|
"bridge/hello_time" "\0" /* ARG_sethello */
|
|
|
|
"bridge/max_age", /* ARG_setmaxage */
|
|
|
|
key - ARG_setageing
|
|
|
|
),
|
|
|
|
str_to_jiffies(*argv)
|
|
|
|
);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2019-04-13 17:28:06 +05:30
|
|
|
|
2019-10-12 23:12:37 +05:30
|
|
|
if (key == ARG_setbridgeprio) {
|
|
|
|
write_uint(br, "bridge/priority", xatoi_positive(*argv));
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2019-04-13 17:28:06 +05:30
|
|
|
|
2019-10-12 23:12:37 +05:30
|
|
|
if (key == ARG_setpathcost
|
|
|
|
|| key == ARG_setportprio
|
|
|
|
) {
|
|
|
|
if (!argv[1])
|
|
|
|
bb_show_usage();
|
|
|
|
/* BR is not used (and ignored!) for these commands:
|
|
|
|
* "setpathcost BR PORT N" writes "N\n" to
|
|
|
|
* /sys/class/net/PORT/brport/path_cost
|
|
|
|
* "setportprio BR PORT N" writes "N\n" to
|
|
|
|
* /sys/class/net/PORT/brport/priority
|
|
|
|
*/
|
|
|
|
write_uint(argv[0],
|
|
|
|
nth_string(
|
|
|
|
"brport/path_cost" "\0" /* ARG_setpathcost */
|
|
|
|
"brport/priority", /* ARG_setportprio */
|
|
|
|
key - ARG_setpathcost
|
|
|
|
),
|
|
|
|
xatoi_positive(argv[1])
|
|
|
|
);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2008-01-14 00:13:50 +05:30
|
|
|
#endif
|
2019-10-12 23:12:37 +05:30
|
|
|
/* always true: if (key == ARG_addif || key == ARG_delif) */ {
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd = xsocket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, br);
|
|
|
|
ifr.ifr_ifindex = if_nametoindex(*argv);
|
|
|
|
if (ifr.ifr_ifindex == 0) {
|
|
|
|
bb_perror_msg_and_die("iface %s", *argv);
|
2019-04-13 17:28:06 +05:30
|
|
|
}
|
2019-10-12 23:12:37 +05:30
|
|
|
ioctl_or_perror_and_die(fd,
|
|
|
|
key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
|
|
|
|
&ifr, "bridge %s", br
|
|
|
|
);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
2008-01-13 20:53:27 +05:30
|
|
|
}
|
2008-04-06 12:47:02 +05:30
|
|
|
|
2008-01-13 20:53:27 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|