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
|
|
|
*/
|
2008-01-14 00:13:50 +05:30
|
|
|
/* This applet currently uses only the ioctl interface and no sysfs at all.
|
|
|
|
* At the time of this writing this was considered a feature.
|
|
|
|
*/
|
2011-04-11 06:59:49 +05:30
|
|
|
|
|
|
|
//usage:#define brctl_trivial_usage
|
|
|
|
//usage: "COMMAND [BRIDGE [INTERFACE]]"
|
|
|
|
//usage:#define brctl_full_usage "\n\n"
|
|
|
|
//usage: "Manage ethernet bridges\n"
|
|
|
|
//usage: "\nCommands:"
|
|
|
|
//usage: IF_FEATURE_BRCTL_SHOW(
|
|
|
|
//usage: "\n show Show a list of bridges"
|
|
|
|
//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(
|
|
|
|
//usage: "\n setageing BRIDGE TIME Set ageing time"
|
|
|
|
//usage: "\n setfd BRIDGE TIME Set bridge forward delay"
|
|
|
|
//usage: "\n sethello BRIDGE TIME Set hello time"
|
|
|
|
//usage: "\n setmaxage BRIDGE TIME Set max message age"
|
|
|
|
//usage: "\n setpathcost BRIDGE COST Set path cost"
|
|
|
|
//usage: "\n setportprio BRIDGE PRIO Set port priority"
|
|
|
|
//usage: "\n setbridgeprio BRIDGE PRIO Set bridge priority"
|
|
|
|
//usage: "\n stp BRIDGE [1/yes/on|0/no/off] STP on/off"
|
|
|
|
//usage: )
|
|
|
|
|
2008-01-13 20:53:27 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#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 21:40:11 +05:30
|
|
|
/* Maximum number of ports supported per bridge interface. */
|
|
|
|
#ifndef MAX_PORTS
|
2010-03-23 09:48:38 +05:30
|
|
|
# define MAX_PORTS 32
|
2008-01-14 21:40:11 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Use internal number parsing and not the "exact" conversion. */
|
|
|
|
/* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */
|
|
|
|
#define BRCTL_USE_INTERNAL 1
|
|
|
|
|
2008-01-14 00:13:50 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_FANCY
|
2010-03-23 09:48:38 +05:30
|
|
|
# include <linux/if_bridge.h>
|
2008-03-17 15:03:45 +05:30
|
|
|
|
2008-01-14 00:13:50 +05:30
|
|
|
/* FIXME: These 4 funcs are not really clean and could be improved */
|
2012-06-10 17:46:16 +05:30
|
|
|
static ALWAYS_INLINE void bb_strtotimeval(struct timeval *tv,
|
2008-03-17 15:03:45 +05:30
|
|
|
const char *time_str)
|
2008-01-14 00:13:50 +05:30
|
|
|
{
|
|
|
|
double secs;
|
2010-03-23 09:48:38 +05:30
|
|
|
# if BRCTL_USE_INTERNAL
|
2010-03-16 17:11:29 +05:30
|
|
|
char *endptr;
|
|
|
|
secs = /*bb_*/strtod(time_str, &endptr);
|
|
|
|
if (endptr == time_str)
|
2010-03-23 09:48:38 +05:30
|
|
|
# else
|
2008-01-14 00:13:50 +05:30
|
|
|
if (sscanf(time_str, "%lf", &secs) != 1)
|
2010-03-23 09:48:38 +05:30
|
|
|
# endif
|
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, time_str, "timespec");
|
2008-01-14 00:13:50 +05:30
|
|
|
tv->tv_sec = secs;
|
|
|
|
tv->tv_usec = 1000000 * (secs - tv->tv_sec);
|
|
|
|
}
|
|
|
|
|
2010-03-23 09:48:38 +05:30
|
|
|
static ALWAYS_INLINE unsigned long tv_to_jiffies(const struct timeval *tv)
|
2008-01-14 00:13:50 +05:30
|
|
|
{
|
|
|
|
unsigned long long jif;
|
|
|
|
|
|
|
|
jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
|
|
|
|
|
|
|
|
return jif/10000;
|
|
|
|
}
|
2008-03-17 15:03:45 +05:30
|
|
|
# if 0
|
2010-03-23 09:48:38 +05:30
|
|
|
static void jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
|
2008-01-14 00:13:50 +05:30
|
|
|
{
|
|
|
|
unsigned long long tvusec;
|
|
|
|
|
|
|
|
tvusec = 10000ULL*jiffies;
|
|
|
|
tv->tv_sec = tvusec/1000000;
|
|
|
|
tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
static unsigned long str_to_jiffies(const char *time_str)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2012-06-10 17:46:16 +05:30
|
|
|
bb_strtotimeval(&tv, time_str);
|
2010-03-23 09:48:38 +05:30
|
|
|
return tv_to_jiffies(&tv);
|
2008-01-14 00:13:50 +05:30
|
|
|
}
|
2008-01-14 21:40:11 +05:30
|
|
|
|
|
|
|
static void arm_ioctl(unsigned long *args,
|
2008-03-17 15:03:45 +05:30
|
|
|
unsigned long arg0, unsigned long arg1, unsigned long arg2)
|
2008-01-14 21:40:11 +05:30
|
|
|
{
|
|
|
|
args[0] = arg0;
|
|
|
|
args[1] = arg1;
|
|
|
|
args[2] = arg2;
|
|
|
|
args[3] = 0;
|
|
|
|
}
|
2008-01-13 20:53:27 +05:30
|
|
|
#endif
|
|
|
|
|
2008-01-14 00:13:50 +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"
|
2008-01-14 00:13:50 +05:30
|
|
|
"setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
|
|
|
|
"setpathcost\0" "setportprio\0" "setbridgeprio\0"
|
|
|
|
)
|
2012-09-25 17:36:01 +05:30
|
|
|
IF_FEATURE_BRCTL_SHOW("show\0");
|
2008-03-17 15:03:45 +05:30
|
|
|
|
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(,
|
2008-01-14 21:40:11 +05:30
|
|
|
ARG_stp,
|
2008-01-14 00:13:50 +05:30
|
|
|
ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
|
2008-01-14 21:40:11 +05:30
|
|
|
ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio
|
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
|
|
|
};
|
|
|
|
|
|
|
|
int fd;
|
2008-01-14 00:13:50 +05:30
|
|
|
smallint key;
|
|
|
|
struct ifreq ifr;
|
2008-01-14 21:40:11 +05:30
|
|
|
char *br, *brif;
|
2008-01-13 20:53:27 +05:30
|
|
|
|
|
|
|
argv++;
|
|
|
|
while (*argv) {
|
2008-04-06 12:47:02 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_FANCY
|
|
|
|
int ifidx[MAX_PORTS];
|
|
|
|
unsigned long args[4];
|
|
|
|
ifr.ifr_data = (char *) &args;
|
|
|
|
#endif
|
|
|
|
|
2008-01-14 00:13:50 +05:30
|
|
|
key = index_in_strings(keywords, *argv);
|
|
|
|
if (key == -1) /* no match found in keywords array, bail out. */
|
2008-01-13 20:53:27 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
|
|
|
|
argv++;
|
2008-04-06 12:47:02 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
2008-01-13 20:53:27 +05:30
|
|
|
#if ENABLE_FEATURE_BRCTL_SHOW
|
|
|
|
if (key == ARG_show) { /* show */
|
2008-04-06 12:47:02 +05:30
|
|
|
char brname[IFNAMSIZ];
|
|
|
|
int bridx[MAX_PORTS];
|
|
|
|
int i, num;
|
|
|
|
arm_ioctl(args, BRCTL_GET_BRIDGES,
|
|
|
|
(unsigned long) bridx, MAX_PORTS);
|
|
|
|
num = xioctl(fd, SIOCGIFBR, args);
|
|
|
|
printf("bridge name\tbridge id\t\tSTP enabled\tinterfaces\n");
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
char ifname[IFNAMSIZ];
|
|
|
|
int j, tabs;
|
|
|
|
struct __bridge_info bi;
|
|
|
|
unsigned char *x;
|
|
|
|
|
|
|
|
if (!if_indextoname(bridx[i], brname))
|
|
|
|
bb_perror_msg_and_die("can't get bridge name for index %d", i);
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, brname);
|
2008-04-06 12:47:02 +05:30
|
|
|
|
|
|
|
arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
|
|
|
|
(unsigned long) &bi, 0);
|
|
|
|
xioctl(fd, SIOCDEVPRIVATE, &ifr);
|
|
|
|
printf("%s\t\t", brname);
|
|
|
|
|
|
|
|
/* print bridge id */
|
|
|
|
x = (unsigned char *) &bi.bridge_id;
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
printf("%.2x", x[j]);
|
|
|
|
if (j == 1)
|
|
|
|
bb_putchar('.');
|
|
|
|
}
|
|
|
|
printf(bi.stp_enabled ? "\tyes" : "\tno");
|
|
|
|
|
|
|
|
/* print interface list */
|
|
|
|
arm_ioctl(args, BRCTL_GET_PORT_LIST,
|
|
|
|
(unsigned long) ifidx, MAX_PORTS);
|
|
|
|
xioctl(fd, SIOCDEVPRIVATE, &ifr);
|
|
|
|
tabs = 0;
|
|
|
|
for (j = 0; j < MAX_PORTS; j++) {
|
|
|
|
if (!ifidx[j])
|
|
|
|
continue;
|
|
|
|
if (!if_indextoname(ifidx[j], ifname))
|
|
|
|
bb_perror_msg_and_die("can't get interface name for index %d", j);
|
|
|
|
if (tabs)
|
|
|
|
printf("\t\t\t\t\t");
|
|
|
|
else
|
|
|
|
tabs = 1;
|
|
|
|
printf("\t\t%s\n", ifname);
|
|
|
|
}
|
2010-10-28 22:27:19 +05:30
|
|
|
if (!tabs) /* bridge has no interfaces */
|
2008-04-06 12:47:02 +05:30
|
|
|
bb_putchar('\n');
|
|
|
|
}
|
|
|
|
goto done;
|
2008-01-13 20:53:27 +05:30
|
|
|
}
|
|
|
|
#endif
|
2008-04-06 12:47:02 +05:30
|
|
|
|
|
|
|
if (!*argv) /* all but 'show' need at least one argument */
|
|
|
|
bb_show_usage();
|
|
|
|
|
2008-03-17 15:03:45 +05:30
|
|
|
br = *argv++;
|
2008-01-13 20:53:27 +05:30
|
|
|
|
2008-01-14 00:13:50 +05:30
|
|
|
if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */
|
|
|
|
ioctl_or_perror_and_die(fd,
|
2008-03-17 15:03:45 +05:30
|
|
|
key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
|
|
|
|
br, "bridge %s", br);
|
2008-01-14 00:13:50 +05:30
|
|
|
goto done;
|
2008-01-13 20:53:27 +05:30
|
|
|
}
|
2008-04-06 12:47:02 +05:30
|
|
|
|
2010-03-17 15:53:04 +05:30
|
|
|
if (!*argv) /* all but 'addbr/delbr' need at least two arguments */
|
2008-01-14 00:13:50 +05:30
|
|
|
bb_show_usage();
|
2008-04-06 12:47:02 +05:30
|
|
|
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, br);
|
2008-01-14 00:13:50 +05:30
|
|
|
if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
|
2008-04-06 12:47:02 +05:30
|
|
|
brif = *argv;
|
2008-03-17 15:03:45 +05:30
|
|
|
ifr.ifr_ifindex = if_nametoindex(brif);
|
|
|
|
if (!ifr.ifr_ifindex) {
|
|
|
|
bb_perror_msg_and_die("iface %s", brif);
|
2008-01-13 20:53:27 +05:30
|
|
|
}
|
2008-01-14 00:13:50 +05:30
|
|
|
ioctl_or_perror_and_die(fd,
|
2008-03-17 15:03:45 +05:30
|
|
|
key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
|
|
|
|
&ifr, "bridge %s", br);
|
2008-04-06 12:47:02 +05:30
|
|
|
goto done_next_argv;
|
2008-01-14 00:13:50 +05:30
|
|
|
}
|
|
|
|
#if ENABLE_FEATURE_BRCTL_FANCY
|
2008-01-14 21:40:11 +05:30
|
|
|
if (key == ARG_stp) { /* stp */
|
2010-04-03 04:22:16 +05:30
|
|
|
static const char no_yes[] ALIGN1 =
|
2010-03-23 09:48:38 +05:30
|
|
|
"0\0" "off\0" "n\0" "no\0" /* 0 .. 3 */
|
|
|
|
"1\0" "on\0" "y\0" "yes\0"; /* 4 .. 7 */
|
2010-04-03 04:22:16 +05:30
|
|
|
int onoff = index_in_strings(no_yes, *argv);
|
2010-03-23 09:48:38 +05:30
|
|
|
if (onoff < 0)
|
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
|
|
|
|
onoff = (unsigned)onoff / 4;
|
|
|
|
arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE, onoff, 0);
|
2008-01-14 21:40:11 +05:30
|
|
|
goto fire;
|
|
|
|
}
|
2008-04-06 12:47:02 +05:30
|
|
|
if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
|
|
|
|
static const uint8_t ops[] ALIGN1 = {
|
|
|
|
BRCTL_SET_AGEING_TIME, /* ARG_setageing */
|
|
|
|
BRCTL_SET_BRIDGE_FORWARD_DELAY, /* ARG_setfd */
|
|
|
|
BRCTL_SET_BRIDGE_HELLO_TIME, /* ARG_sethello */
|
|
|
|
BRCTL_SET_BRIDGE_MAX_AGE /* ARG_setmaxage */
|
|
|
|
};
|
|
|
|
arm_ioctl(args, ops[key - ARG_setageing], str_to_jiffies(*argv), 0);
|
2008-01-14 21:40:11 +05:30
|
|
|
goto fire;
|
|
|
|
}
|
|
|
|
if (key == ARG_setpathcost
|
2008-03-17 15:03:45 +05:30
|
|
|
|| key == ARG_setportprio
|
|
|
|
|| key == ARG_setbridgeprio
|
|
|
|
) {
|
2008-04-06 12:47:02 +05:30
|
|
|
static const uint8_t ops[] ALIGN1 = {
|
|
|
|
BRCTL_SET_PATH_COST, /* ARG_setpathcost */
|
|
|
|
BRCTL_SET_PORT_PRIORITY, /* ARG_setportprio */
|
|
|
|
BRCTL_SET_BRIDGE_PRIORITY /* ARG_setbridgeprio */
|
|
|
|
};
|
|
|
|
int port = -1;
|
|
|
|
unsigned arg1, arg2;
|
|
|
|
|
|
|
|
if (key != ARG_setbridgeprio) {
|
|
|
|
/* get portnum */
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
port = if_nametoindex(*argv++);
|
|
|
|
if (!port)
|
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, "port");
|
|
|
|
memset(ifidx, 0, sizeof ifidx);
|
|
|
|
arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
|
|
|
|
MAX_PORTS);
|
|
|
|
xioctl(fd, SIOCDEVPRIVATE, &ifr);
|
|
|
|
for (i = 0; i < MAX_PORTS; i++) {
|
|
|
|
if (ifidx[i] == port) {
|
|
|
|
port = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arg1 = port;
|
2010-08-12 17:44:45 +05:30
|
|
|
arg2 = xatoi_positive(*argv);
|
2008-01-14 21:40:11 +05:30
|
|
|
if (key == ARG_setbridgeprio) {
|
2008-04-06 12:47:02 +05:30
|
|
|
arg1 = arg2;
|
2008-01-14 21:40:11 +05:30
|
|
|
arg2 = 0;
|
2008-04-06 12:47:02 +05:30
|
|
|
}
|
|
|
|
arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2);
|
2008-01-14 21:40:11 +05:30
|
|
|
}
|
|
|
|
fire:
|
2008-04-06 12:47:02 +05:30
|
|
|
/* Execute the previously set command */
|
2008-01-14 21:40:11 +05:30
|
|
|
xioctl(fd, SIOCDEVPRIVATE, &ifr);
|
2008-01-14 00:13:50 +05:30
|
|
|
#endif
|
2008-04-06 12:47:02 +05:30
|
|
|
done_next_argv:
|
|
|
|
argv++;
|
2008-01-14 00:13:50 +05:30
|
|
|
done:
|
2008-04-06 12:47:02 +05:30
|
|
|
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;
|
|
|
|
}
|