1 /* vi: set sw=4 ts=4: */
3 * Small implementation of brctl for busybox.
5 * Copyright (C) 2008 by Bernhard Fischer
7 * Some helper functions from bridge-utils are
8 * Copyright (C) 2000 Lennert Buytenhek
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 /* This applet currently uses only the ioctl interface and no sysfs at all.
13 * At the time of this writing this was considered a feature.
16 #include <linux/sockios.h>
19 /* Maximum number of ports supported per bridge interface. */
24 /* Use internal number parsing and not the "exact" conversion. */
25 /* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */
26 #define BRCTL_USE_INTERNAL 1
28 #ifdef ENABLE_FEATURE_BRCTL_SHOW
31 #define ENABLE_FEATURE_BRCTL_SHOW 0
32 #define USE_FEATURE_BRCTL_SHOW(...)
34 #if ENABLE_FEATURE_BRCTL_FANCY
35 #include <linux/if_bridge.h>
36 /* FIXME: These 4 funcs are not really clean and could be improved */
37 static inline ALWAYS_INLINE void strtotimeval(struct timeval *tv,
41 #if BRCTL_USE_INTERNAL
42 if (!(secs = /*bb_*/strtod(time_str, NULL)))
44 if (sscanf(time_str, "%lf", &secs) != 1)
46 bb_error_msg_and_die (bb_msg_invalid_arg, time_str, "timespec");
48 tv->tv_usec = 1000000 * (secs - tv->tv_sec);
51 static inline ALWAYS_INLINE unsigned long __tv_to_jiffies(const struct timeval *tv)
53 unsigned long long jif;
55 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
60 static void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
62 unsigned long long tvusec;
64 tvusec = 10000ULL*jiffies;
65 tv->tv_sec = tvusec/1000000;
66 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
69 static unsigned long str_to_jiffies(const char *time_str)
72 strtotimeval(&tv, time_str);
73 return __tv_to_jiffies(&tv);
76 static void arm_ioctl(unsigned long *args,
77 unsigned long arg0, unsigned long arg1, unsigned long arg2)
87 int brctl_main(int argc ATTRIBUTE_UNUSED, char **argv) MAIN_EXTERNALLY_VISIBLE;
88 int brctl_main(int argc ATTRIBUTE_UNUSED, char **argv)
91 static const char keywords[] ALIGN1 =
92 "addbr\0" "delbr\0" "addif\0" "delif\0"
93 USE_FEATURE_BRCTL_FANCY(
95 "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
96 "setpathcost\0" "setportprio\0" "setbridgeprio\0"
98 USE_FEATURE_BRCTL_SHOW("showmacs\0" "show\0");
99 enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
100 USE_FEATURE_BRCTL_FANCY(,
102 ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
103 ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio
105 USE_FEATURE_BRCTL_SHOW(, ARG_showmacs, ARG_show) };
108 static char info[] = "bridge %s\0 iface %s";
110 #if ENABLE_FEATURE_BRCTL_FANCY
111 unsigned long args[4] = {0, 0, 0, 0};
118 key = index_in_strings(keywords, *argv);
119 if (key == -1) /* no match found in keywords array, bail out. */
120 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
122 #if ENABLE_FEATURE_BRCTL_SHOW
123 if (key == ARG_show) { /* show */
124 goto out; /* FIXME: implement me! :) */
127 fd = xsocket(AF_INET, SOCK_STREAM, 0);
130 if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */
131 ioctl_or_perror_and_die(fd,
132 key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
136 if (!*argv) /* all but 'show' need at least one argument */
138 safe_strncpy(ifr.ifr_name, br, IFNAMSIZ);
139 if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
141 if (!(ifr.ifr_ifindex = if_nametoindex(brif))) {
142 bb_perror_msg_and_die(info+11, brif);
144 ioctl_or_perror_and_die(fd,
145 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
149 #if ENABLE_FEATURE_BRCTL_FANCY
150 ifr.ifr_data = (char *) &args;
151 if (key == ARG_stp) { /* stp */
152 /* FIXME: parsing yes/y/on/1 versus no/n/off/0 is too involved */
153 arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE,
154 (unsigned)(**argv - '0'), 0);
157 if ((unsigned)(key - ARG_stp) < 5) { /* time related ops */
158 unsigned long op = (key == ARG_setageing) ? BRCTL_SET_AGEING_TIME :
159 (key == ARG_setfd) ? BRCTL_SET_BRIDGE_FORWARD_DELAY:
160 (key == ARG_sethello) ? BRCTL_SET_BRIDGE_HELLO_TIME:
161 (key == ARG_setmaxage) ? BRCTL_SET_BRIDGE_MAX_AGE :
162 -1/* will never be used */;
163 arm_ioctl(args, op, str_to_jiffies (*argv), 0);
167 if (key == ARG_setpathcost || key == ARG_setportprio) {/* get portnum */
168 int ifidx[MAX_PORTS];
171 if (!(port = if_nametoindex(*argv)))
172 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, "port");
174 memset(ifidx, 0, sizeof ifidx);
175 arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
177 xioctl(fd, SIOCDEVPRIVATE, &ifr);
178 for (i = 0; i < MAX_PORTS; i++)
179 if (ifidx[i] == port) {
184 if (key == ARG_setpathcost
185 || key == ARG_setportprio || key == ARG_setbridgeprio) {
186 unsigned long op = (key == ARG_setpathcost) ? BRCTL_SET_PATH_COST :
187 (key == ARG_setportprio) ? BRCTL_SET_PORT_PRIORITY :
188 (key == ARG_setbridgeprio) ? BRCTL_SET_BRIDGE_PRIORITY :
189 -1/* will never be used */;
190 unsigned long arg1 = port;
192 # if BRCTL_USE_INTERNAL
195 if (sscanf(*argv, "%i", &tmp) != 1)
196 bb_error_msg_and_die(bb_msg_invalid_arg, *argv,
197 key == ARG_setpathcost ? "cost" : "prio");
199 if (key == ARG_setbridgeprio) {
204 arm_ioctl(args, op, arg1, arg2);
207 /* Execute the previously set command. */
208 xioctl(fd, SIOCDEVPRIVATE, &ifr);
212 if (ENABLE_FEATURE_CLEAN_UP)
215 USE_FEATURE_BRCTL_SHOW(out:)