1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69#include "libbb.h"
70#include <linux/sockios.h>
71#include <net/if.h>
72
73#ifndef SIOCBRADDBR
74# define SIOCBRADDBR BRCTL_ADD_BRIDGE
75#endif
76#ifndef SIOCBRDELBR
77# define SIOCBRDELBR BRCTL_DEL_BRIDGE
78#endif
79#ifndef SIOCBRADDIF
80# define SIOCBRADDIF BRCTL_ADD_IF
81#endif
82#ifndef SIOCBRDELIF
83# define SIOCBRDELIF BRCTL_DEL_IF
84#endif
85
86
87
88#ifndef MAX_PORTS
89# define MAX_PORTS 32
90#endif
91
92
93
94#define BRCTL_USE_INTERNAL 1
95
96#if ENABLE_FEATURE_BRCTL_FANCY
97
98
99
100
101
102
103
104
105
106
107#define BRCTL_GET_VERSION 0
108#define BRCTL_GET_BRIDGES 1
109#define BRCTL_ADD_BRIDGE 2
110#define BRCTL_DEL_BRIDGE 3
111#define BRCTL_ADD_IF 4
112#define BRCTL_DEL_IF 5
113#define BRCTL_GET_BRIDGE_INFO 6
114#define BRCTL_GET_PORT_LIST 7
115#define BRCTL_SET_BRIDGE_FORWARD_DELAY 8
116#define BRCTL_SET_BRIDGE_HELLO_TIME 9
117#define BRCTL_SET_BRIDGE_MAX_AGE 10
118#define BRCTL_SET_AGEING_TIME 11
119#define BRCTL_SET_GC_INTERVAL 12
120#define BRCTL_GET_PORT_INFO 13
121#define BRCTL_SET_BRIDGE_STP_STATE 14
122#define BRCTL_SET_BRIDGE_PRIORITY 15
123#define BRCTL_SET_PORT_PRIORITY 16
124#define BRCTL_SET_PATH_COST 17
125#define BRCTL_GET_FDB_ENTRIES 18
126struct __bridge_info {
127 uint64_t designated_root;
128 uint64_t bridge_id;
129 uint32_t root_path_cost;
130 uint32_t max_age;
131 uint32_t hello_time;
132 uint32_t forward_delay;
133 uint32_t bridge_max_age;
134 uint32_t bridge_hello_time;
135 uint32_t bridge_forward_delay;
136 uint8_t topology_change;
137 uint8_t topology_change_detected;
138 uint8_t root_port;
139 uint8_t stp_enabled;
140 uint32_t ageing_time;
141 uint32_t gc_interval;
142 uint32_t hello_timer_value;
143 uint32_t tcn_timer_value;
144 uint32_t topology_change_timer_value;
145 uint32_t gc_timer_value;
146};
147
148
149
150static ALWAYS_INLINE void bb_strtotimeval(struct timeval *tv,
151 const char *time_str)
152{
153 double secs;
154# if BRCTL_USE_INTERNAL
155 char *endptr;
156 secs = strtod(time_str, &endptr);
157 if (endptr == time_str)
158# else
159 if (sscanf(time_str, "%lf", &secs) != 1)
160# endif
161 bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec");
162 tv->tv_sec = secs;
163 tv->tv_usec = 1000000 * (secs - tv->tv_sec);
164}
165
166static ALWAYS_INLINE unsigned long tv_to_jiffies(const struct timeval *tv)
167{
168 unsigned long long jif;
169
170 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
171
172 return jif/10000;
173}
174# if 0
175static void jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
176{
177 unsigned long long tvusec;
178
179 tvusec = 10000ULL*jiffies;
180 tv->tv_sec = tvusec/1000000;
181 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
182}
183# endif
184static unsigned long str_to_jiffies(const char *time_str)
185{
186 struct timeval tv;
187 bb_strtotimeval(&tv, time_str);
188 return tv_to_jiffies(&tv);
189}
190
191static void arm_ioctl(unsigned long *args,
192 unsigned long arg0, unsigned long arg1, unsigned long arg2)
193{
194 args[0] = arg0;
195 args[1] = arg1;
196 args[2] = arg2;
197 args[3] = 0;
198}
199#endif
200
201
202int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
203int brctl_main(int argc UNUSED_PARAM, char **argv)
204{
205 static const char keywords[] ALIGN1 =
206 "addbr\0" "delbr\0" "addif\0" "delif\0"
207 IF_FEATURE_BRCTL_FANCY(
208 "stp\0"
209 "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
210 "setpathcost\0" "setportprio\0" "setbridgeprio\0"
211 )
212 IF_FEATURE_BRCTL_SHOW("show\0");
213
214 enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
215 IF_FEATURE_BRCTL_FANCY(,
216 ARG_stp,
217 ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
218 ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio
219 )
220 IF_FEATURE_BRCTL_SHOW(, ARG_show)
221 };
222
223 int fd;
224 smallint key;
225 struct ifreq ifr;
226 char *br, *brif;
227
228 argv++;
229 while (*argv) {
230#if ENABLE_FEATURE_BRCTL_FANCY
231 int ifidx[MAX_PORTS];
232 unsigned long args[4];
233 ifr.ifr_data = (char *) &args;
234#endif
235
236 key = index_in_strings(keywords, *argv);
237 if (key == -1)
238 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
239 argv++;
240 fd = xsocket(AF_INET, SOCK_STREAM, 0);
241
242#if ENABLE_FEATURE_BRCTL_SHOW
243 if (key == ARG_show) {
244 char brname[IFNAMSIZ];
245 int bridx[MAX_PORTS];
246 int i, num;
247 arm_ioctl(args, BRCTL_GET_BRIDGES,
248 (unsigned long) bridx, MAX_PORTS);
249 num = xioctl(fd, SIOCGIFBR, args);
250 puts("bridge name\tbridge id\t\tSTP enabled\tinterfaces");
251 for (i = 0; i < num; i++) {
252 char ifname[IFNAMSIZ];
253 int j, tabs;
254 struct __bridge_info bi;
255 unsigned char *x;
256
257 if (!if_indextoname(bridx[i], brname))
258 bb_perror_msg_and_die("can't get bridge name for index %d", i);
259 strncpy_IFNAMSIZ(ifr.ifr_name, brname);
260
261 arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
262 (unsigned long) &bi, 0);
263 xioctl(fd, SIOCDEVPRIVATE, &ifr);
264 printf("%s\t\t", brname);
265
266
267 x = (unsigned char *) &bi.bridge_id;
268 for (j = 0; j < 8; j++) {
269 printf("%02x", x[j]);
270 if (j == 1)
271 bb_putchar('.');
272 }
273 printf(bi.stp_enabled ? "\tyes" : "\tno");
274
275
276 arm_ioctl(args, BRCTL_GET_PORT_LIST,
277 (unsigned long) ifidx, MAX_PORTS);
278 xioctl(fd, SIOCDEVPRIVATE, &ifr);
279 tabs = 0;
280 for (j = 0; j < MAX_PORTS; j++) {
281 if (!ifidx[j])
282 continue;
283 if (!if_indextoname(ifidx[j], ifname))
284 bb_perror_msg_and_die("can't get interface name for index %d", j);
285 if (tabs)
286 printf("\t\t\t\t\t");
287 else
288 tabs = 1;
289 printf("\t\t%s\n", ifname);
290 }
291 if (!tabs)
292 bb_putchar('\n');
293 }
294 goto done;
295 }
296#endif
297
298 if (!*argv)
299 bb_show_usage();
300
301 br = *argv++;
302
303 if (key == ARG_addbr || key == ARG_delbr) {
304 ioctl_or_perror_and_die(fd,
305 key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
306 br, "bridge %s", br);
307 goto done;
308 }
309
310 if (!*argv)
311 bb_show_usage();
312
313 strncpy_IFNAMSIZ(ifr.ifr_name, br);
314 if (key == ARG_addif || key == ARG_delif) {
315 brif = *argv;
316 ifr.ifr_ifindex = if_nametoindex(brif);
317 if (!ifr.ifr_ifindex) {
318 bb_perror_msg_and_die("iface %s", brif);
319 }
320 ioctl_or_perror_and_die(fd,
321 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
322 &ifr, "bridge %s", br);
323 goto done_next_argv;
324 }
325#if ENABLE_FEATURE_BRCTL_FANCY
326 if (key == ARG_stp) {
327 static const char no_yes[] ALIGN1 =
328 "0\0" "off\0" "n\0" "no\0"
329 "1\0" "on\0" "y\0" "yes\0";
330 int onoff = index_in_strings(no_yes, *argv);
331 if (onoff < 0)
332 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
333 onoff = (unsigned)onoff / 4;
334 arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE, onoff, 0);
335 goto fire;
336 }
337 if ((unsigned)(key - ARG_setageing) < 4) {
338 static const uint8_t ops[] ALIGN1 = {
339 BRCTL_SET_AGEING_TIME,
340 BRCTL_SET_BRIDGE_FORWARD_DELAY,
341 BRCTL_SET_BRIDGE_HELLO_TIME,
342 BRCTL_SET_BRIDGE_MAX_AGE
343 };
344 arm_ioctl(args, ops[key - ARG_setageing], str_to_jiffies(*argv), 0);
345 goto fire;
346 }
347 if (key == ARG_setpathcost
348 || key == ARG_setportprio
349 || key == ARG_setbridgeprio
350 ) {
351 static const uint8_t ops[] ALIGN1 = {
352 BRCTL_SET_PATH_COST,
353 BRCTL_SET_PORT_PRIORITY,
354 BRCTL_SET_BRIDGE_PRIORITY
355 };
356 int port = -1;
357 unsigned arg1, arg2;
358
359 if (key != ARG_setbridgeprio) {
360
361 unsigned i;
362
363 port = if_nametoindex(*argv++);
364 if (!port)
365 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, "port");
366 memset(ifidx, 0, sizeof ifidx);
367 arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
368 MAX_PORTS);
369 xioctl(fd, SIOCDEVPRIVATE, &ifr);
370 for (i = 0; i < MAX_PORTS; i++) {
371 if (ifidx[i] == port) {
372 port = i;
373 break;
374 }
375 }
376 }
377 arg1 = port;
378 arg2 = xatoi_positive(*argv);
379 if (key == ARG_setbridgeprio) {
380 arg1 = arg2;
381 arg2 = 0;
382 }
383 arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2);
384 }
385 fire:
386
387 xioctl(fd, SIOCDEVPRIVATE, &ifr);
388#endif
389 done_next_argv:
390 argv++;
391 done:
392 close(fd);
393 }
394
395 return EXIT_SUCCESS;
396}
397