blob: 57074cd738b8a1e81a303daced06ef2f6ff6e8b5 [file] [log] [blame]
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Small implementation of brctl for busybox.
4 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00005 * Copyright (C) 2008 by Bernhard Reutner-Fischer
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +00006 *
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +00007 * Some helper functions from bridge-utils are
8 * Copyright (C) 2000 Lennert Buytenhek
9 *
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 */
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000012/* 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.
14 */
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000015#include "libbb.h"
16#include <linux/sockios.h>
17#include <net/if.h>
18
Denis Vlasenko802cab12009-01-31 20:08:21 +000019#ifndef SIOCBRADDBR
20# define SIOCBRADDBR BRCTL_ADD_BRIDGE
21#endif
22#ifndef SIOCBRDELBR
23# define SIOCBRDELBR BRCTL_DEL_BRIDGE
24#endif
25#ifndef SIOCBRADDIF
26# define SIOCBRADDIF BRCTL_ADD_IF
27#endif
28#ifndef SIOCBRDELIF
29# define SIOCBRDELIF BRCTL_DEL_IF
30#endif
31
32
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000033/* Maximum number of ports supported per bridge interface. */
34#ifndef MAX_PORTS
35#define MAX_PORTS 32
36#endif
37
38/* Use internal number parsing and not the "exact" conversion. */
39/* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */
40#define BRCTL_USE_INTERNAL 1
41
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000042#if ENABLE_FEATURE_BRCTL_FANCY
43#include <linux/if_bridge.h>
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000044
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000045/* FIXME: These 4 funcs are not really clean and could be improved */
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000046static ALWAYS_INLINE void strtotimeval(struct timeval *tv,
47 const char *time_str)
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000048{
49 double secs;
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000050#if BRCTL_USE_INTERNAL
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000051 secs = /*bb_*/strtod(time_str, NULL);
52 if (!secs)
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000053#else
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000054 if (sscanf(time_str, "%lf", &secs) != 1)
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000055#endif
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000056 bb_error_msg_and_die (bb_msg_invalid_arg, time_str, "timespec");
57 tv->tv_sec = secs;
58 tv->tv_usec = 1000000 * (secs - tv->tv_sec);
59}
60
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000061static ALWAYS_INLINE unsigned long __tv_to_jiffies(const struct timeval *tv)
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000062{
63 unsigned long long jif;
64
65 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
66
67 return jif/10000;
68}
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000069# if 0
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000070static void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
71{
72 unsigned long long tvusec;
73
74 tvusec = 10000ULL*jiffies;
75 tv->tv_sec = tvusec/1000000;
76 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
77}
78# endif
79static unsigned long str_to_jiffies(const char *time_str)
80{
81 struct timeval tv;
82 strtotimeval(&tv, time_str);
83 return __tv_to_jiffies(&tv);
84}
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000085
86static void arm_ioctl(unsigned long *args,
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000087 unsigned long arg0, unsigned long arg1, unsigned long arg2)
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000088{
89 args[0] = arg0;
90 args[1] = arg1;
91 args[2] = arg2;
92 args[3] = 0;
93}
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000094#endif
95
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000096
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000097int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000098int brctl_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000099{
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000100 static const char keywords[] ALIGN1 =
101 "addbr\0" "delbr\0" "addif\0" "delif\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000102 IF_FEATURE_BRCTL_FANCY(
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000103 "stp\0"
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000104 "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
105 "setpathcost\0" "setportprio\0" "setbridgeprio\0"
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000106 )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000107 IF_FEATURE_BRCTL_SHOW("showmacs\0" "show\0");
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000108
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000109 enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000110 IF_FEATURE_BRCTL_FANCY(,
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000111 ARG_stp,
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000112 ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000113 ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000114 )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000115 IF_FEATURE_BRCTL_SHOW(, ARG_showmacs, ARG_show)
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000116 };
117
118 int fd;
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000119 smallint key;
120 struct ifreq ifr;
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000121 char *br, *brif;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000122
123 argv++;
124 while (*argv) {
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000125#if ENABLE_FEATURE_BRCTL_FANCY
126 int ifidx[MAX_PORTS];
127 unsigned long args[4];
128 ifr.ifr_data = (char *) &args;
129#endif
130
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000131 key = index_in_strings(keywords, *argv);
132 if (key == -1) /* no match found in keywords array, bail out. */
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000133 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
134 argv++;
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000135 fd = xsocket(AF_INET, SOCK_STREAM, 0);
136
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000137#if ENABLE_FEATURE_BRCTL_SHOW
138 if (key == ARG_show) { /* show */
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000139 char brname[IFNAMSIZ];
140 int bridx[MAX_PORTS];
141 int i, num;
142 arm_ioctl(args, BRCTL_GET_BRIDGES,
143 (unsigned long) bridx, MAX_PORTS);
144 num = xioctl(fd, SIOCGIFBR, args);
145 printf("bridge name\tbridge id\t\tSTP enabled\tinterfaces\n");
146 for (i = 0; i < num; i++) {
147 char ifname[IFNAMSIZ];
148 int j, tabs;
149 struct __bridge_info bi;
150 unsigned char *x;
151
152 if (!if_indextoname(bridx[i], brname))
153 bb_perror_msg_and_die("can't get bridge name for index %d", i);
Denis Vlasenko360d9662008-12-02 18:18:50 +0000154 strncpy_IFNAMSIZ(ifr.ifr_name, brname);
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000155
156 arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
157 (unsigned long) &bi, 0);
158 xioctl(fd, SIOCDEVPRIVATE, &ifr);
159 printf("%s\t\t", brname);
160
161 /* print bridge id */
162 x = (unsigned char *) &bi.bridge_id;
163 for (j = 0; j < 8; j++) {
164 printf("%.2x", x[j]);
165 if (j == 1)
166 bb_putchar('.');
167 }
168 printf(bi.stp_enabled ? "\tyes" : "\tno");
169
170 /* print interface list */
171 arm_ioctl(args, BRCTL_GET_PORT_LIST,
172 (unsigned long) ifidx, MAX_PORTS);
173 xioctl(fd, SIOCDEVPRIVATE, &ifr);
174 tabs = 0;
175 for (j = 0; j < MAX_PORTS; j++) {
176 if (!ifidx[j])
177 continue;
178 if (!if_indextoname(ifidx[j], ifname))
179 bb_perror_msg_and_die("can't get interface name for index %d", j);
180 if (tabs)
181 printf("\t\t\t\t\t");
182 else
183 tabs = 1;
184 printf("\t\t%s\n", ifname);
185 }
186 if (!tabs) /* bridge has no interfaces */
187 bb_putchar('\n');
188 }
189 goto done;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000190 }
191#endif
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000192
193 if (!*argv) /* all but 'show' need at least one argument */
194 bb_show_usage();
195
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000196 br = *argv++;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000197
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000198 if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */
199 ioctl_or_perror_and_die(fd,
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000200 key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
201 br, "bridge %s", br);
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000202 goto done;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000203 }
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000204
205 if (!*argv) /* all but 'addif/delif' need at least two arguments */
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000206 bb_show_usage();
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000207
Denis Vlasenko360d9662008-12-02 18:18:50 +0000208 strncpy_IFNAMSIZ(ifr.ifr_name, br);
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000209 if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000210 brif = *argv;
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000211 ifr.ifr_ifindex = if_nametoindex(brif);
212 if (!ifr.ifr_ifindex) {
213 bb_perror_msg_and_die("iface %s", brif);
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000214 }
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000215 ioctl_or_perror_and_die(fd,
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000216 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
217 &ifr, "bridge %s", br);
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000218 goto done_next_argv;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000219 }
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000220#if ENABLE_FEATURE_BRCTL_FANCY
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000221 if (key == ARG_stp) { /* stp */
222 /* FIXME: parsing yes/y/on/1 versus no/n/off/0 is too involved */
223 arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE,
224 (unsigned)(**argv - '0'), 0);
225 goto fire;
226 }
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000227 if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
228 static const uint8_t ops[] ALIGN1 = {
229 BRCTL_SET_AGEING_TIME, /* ARG_setageing */
230 BRCTL_SET_BRIDGE_FORWARD_DELAY, /* ARG_setfd */
231 BRCTL_SET_BRIDGE_HELLO_TIME, /* ARG_sethello */
232 BRCTL_SET_BRIDGE_MAX_AGE /* ARG_setmaxage */
233 };
234 arm_ioctl(args, ops[key - ARG_setageing], str_to_jiffies(*argv), 0);
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000235 goto fire;
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000236 }
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000237 if (key == ARG_setpathcost
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000238 || key == ARG_setportprio
239 || key == ARG_setbridgeprio
240 ) {
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000241 static const uint8_t ops[] ALIGN1 = {
242 BRCTL_SET_PATH_COST, /* ARG_setpathcost */
243 BRCTL_SET_PORT_PRIORITY, /* ARG_setportprio */
244 BRCTL_SET_BRIDGE_PRIORITY /* ARG_setbridgeprio */
245 };
246 int port = -1;
247 unsigned arg1, arg2;
248
249 if (key != ARG_setbridgeprio) {
250 /* get portnum */
251 unsigned i;
252
253 port = if_nametoindex(*argv++);
254 if (!port)
255 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, "port");
256 memset(ifidx, 0, sizeof ifidx);
257 arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
258 MAX_PORTS);
259 xioctl(fd, SIOCDEVPRIVATE, &ifr);
260 for (i = 0; i < MAX_PORTS; i++) {
261 if (ifidx[i] == port) {
262 port = i;
263 break;
264 }
265 }
266 }
267 arg1 = port;
268 arg2 = xatoi_u(*argv);
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000269 if (key == ARG_setbridgeprio) {
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000270 arg1 = arg2;
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000271 arg2 = 0;
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000272 }
273 arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2);
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000274 }
275 fire:
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000276 /* Execute the previously set command */
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000277 xioctl(fd, SIOCDEVPRIVATE, &ifr);
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000278#endif
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000279 done_next_argv:
280 argv++;
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000281 done:
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000282 close(fd);
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000283 }
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000284
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000285 return EXIT_SUCCESS;
286}