blob: 956bd91f3fb7c2e7672584a7287188a3d63ca934 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000011 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010012//config:config BRCTL
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "brctl (4.7 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010014//config: default y
Denys Vlasenko47367e12016-11-23 09:05:14 +010015//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: Manage ethernet bridges.
17//config: Supports addbr/delbr and addif/delif.
Denys Vlasenko47367e12016-11-23 09:05:14 +010018//config:
19//config:config FEATURE_BRCTL_FANCY
20//config: bool "Fancy options"
21//config: default y
22//config: depends on BRCTL
23//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020024//config: Add support for extended option like:
25//config: setageing, setfd, sethello, setmaxage,
26//config: setpathcost, setportprio, setbridgeprio,
27//config: stp
28//config: This adds about 600 bytes.
Denys Vlasenko47367e12016-11-23 09:05:14 +010029//config:
30//config:config FEATURE_BRCTL_SHOW
31//config: bool "Support show"
32//config: default y
33//config: depends on BRCTL && FEATURE_BRCTL_FANCY
34//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020035//config: Add support for option which prints the current config:
36//config: show
Denys Vlasenko47367e12016-11-23 09:05:14 +010037
Denys Vlasenko86e07f62017-08-06 20:14:02 +020038//applet:IF_BRCTL(APPLET_NOEXEC(brctl, brctl, BB_DIR_USR_SBIN, BB_SUID_DROP, brctl))
Denys Vlasenko47367e12016-11-23 09:05:14 +010039
40//kbuild:lib-$(CONFIG_BRCTL) += brctl.o
Pere Orga5bc8c002011-04-11 03:29:49 +020041
42//usage:#define brctl_trivial_usage
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +020043//usage: "COMMAND [BRIDGE [ARGS]]"
Pere Orga5bc8c002011-04-11 03:29:49 +020044//usage:#define brctl_full_usage "\n\n"
Denys Vlasenko29458222019-04-13 15:48:31 +020045//usage: "Manage ethernet bridges"
Pere Orga5bc8c002011-04-11 03:29:49 +020046//usage: "\nCommands:"
47//usage: IF_FEATURE_BRCTL_SHOW(
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +020048//usage: "\n show [BRIDGE]... Show bridges"
Pere Orga5bc8c002011-04-11 03:29:49 +020049//usage: )
50//usage: "\n addbr BRIDGE Create BRIDGE"
51//usage: "\n delbr BRIDGE Delete BRIDGE"
52//usage: "\n addif BRIDGE IFACE Add IFACE to BRIDGE"
53//usage: "\n delif BRIDGE IFACE Delete IFACE from BRIDGE"
54//usage: IF_FEATURE_BRCTL_FANCY(
Denys Vlasenkoeb139512019-10-12 19:51:46 +020055//usage: "\n showmacs BRIDGE List MAC addresses"
Denys Vlasenko33987532019-10-12 19:24:38 +020056//usage: "\n showstp BRIDGE Show STP info"
Denys Vlasenkoeb139512019-10-12 19:51:46 +020057//usage: "\n stp BRIDGE 1/yes/on|0/no/off Set STP on/off"
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +020058//usage: "\n setageing BRIDGE SECONDS Set ageing time"
59//usage: "\n setfd BRIDGE SECONDS Set bridge forward delay"
60//usage: "\n sethello BRIDGE SECONDS Set hello time"
61//usage: "\n setmaxage BRIDGE SECONDS Set max message age"
Pere Orga5bc8c002011-04-11 03:29:49 +020062//usage: "\n setbridgeprio BRIDGE PRIO Set bridge priority"
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +020063//usage: "\n setportprio BRIDGE IFACE PRIO Set port priority"
64//usage: "\n setpathcost BRIDGE IFACE COST Set path cost"
Pere Orga5bc8c002011-04-11 03:29:49 +020065//usage: )
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +020066// Not yet implemented:
Denys Vlasenkoeb139512019-10-12 19:51:46 +020067// hairpin BRIDGE IFACE on|off Set hairpin on/off
Martin Lewis6dcf5632019-10-10 16:00:19 -050068
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000069#include "libbb.h"
Denys Vlasenkoc5150e92019-04-12 18:52:31 +020070#include "common_bufsiz.h"
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000071#include <linux/sockios.h>
72#include <net/if.h>
73
Denis Vlasenko802cab12009-01-31 20:08:21 +000074#ifndef SIOCBRADDBR
75# define SIOCBRADDBR BRCTL_ADD_BRIDGE
76#endif
77#ifndef SIOCBRDELBR
78# define SIOCBRDELBR BRCTL_DEL_BRIDGE
79#endif
80#ifndef SIOCBRADDIF
81# define SIOCBRADDIF BRCTL_ADD_IF
82#endif
83#ifndef SIOCBRDELIF
84# define SIOCBRDELIF BRCTL_DEL_IF
85#endif
86
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000087#if ENABLE_FEATURE_BRCTL_FANCY
Denys Vlasenko94356082019-04-13 14:17:55 +020088static unsigned str_to_jiffies(const char *time_str)
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000089{
Denys Vlasenko94356082019-04-13 14:17:55 +020090 double dd;
Maciek Borzecki46abfc02010-03-16 12:41:29 +010091 char *endptr;
Denys Vlasenkoc2bd0b62021-03-23 13:50:02 +010092//TODO: needs setlocale(LC_NUMERIC, "C")?
Denys Vlasenko94356082019-04-13 14:17:55 +020093 dd = /*bb_*/strtod(time_str, &endptr);
94 if (endptr == time_str || dd < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +020095 bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec");
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000096
Denys Vlasenko94356082019-04-13 14:17:55 +020097 dd *= 100;
98 /* For purposes of brctl,
99 * capping SECONDS by ~20 million seconds is quite enough:
100 */
101 if (dd > INT_MAX)
102 dd = INT_MAX;
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000103
Denys Vlasenko94356082019-04-13 14:17:55 +0200104 return dd;
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000105}
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000106#endif
107
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200108#define filedata bb_common_bufsiz1
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200109
Denys Vlasenkodf1f4792020-06-23 03:13:55 +0200110#if ENABLE_FEATURE_BRCTL_SHOW || ENABLE_FEATURE_BRCTL_FANCY
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200111static int read_file(const char *name)
112{
113 int n = open_read_close(name, filedata, COMMON_BUFSIZE - 1);
114 if (n < 0) {
115 filedata[0] = '\0';
116 } else {
117 filedata[n] = '\0';
118 if (n != 0 && filedata[n - 1] == '\n')
119 filedata[--n] = '\0';
120 }
121 return n;
122}
Denys Vlasenkodf1f4792020-06-23 03:13:55 +0200123#endif
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200124
Denys Vlasenkodf1f4792020-06-23 03:13:55 +0200125#if ENABLE_FEATURE_BRCTL_SHOW
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200126/* NB: we are in /sys/class/net
127 */
128static int show_bridge(const char *name, int need_hdr)
129{
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200130/* Output:
131 *bridge name bridge id STP enabled interfaces
132 *br0 8000.000000000000 no eth0
133 */
Denys Vlasenko33987532019-10-12 19:24:38 +0200134 char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 8];
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200135 int tabs;
136 DIR *ifaces;
137 struct dirent *ent;
138 char *sfx;
139
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200140#if IFNAMSIZ == 16
141 sfx = pathbuf + sprintf(pathbuf, "%.16s/bridge/", name);
142#else
143 sfx = pathbuf + sprintf(pathbuf, "%.*s/bridge/", (int)IFNAMSIZ, name);
144#endif
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200145 strcpy(sfx, "bridge_id");
146 if (read_file(pathbuf) < 0)
147 return -1; /* this iface is not a bridge */
148
149 if (need_hdr)
150 puts("bridge name\tbridge id\t\tSTP enabled\tinterfaces");
Martin Lewis6dcf5632019-10-10 16:00:19 -0500151 printf("%s\t\t%s\t", name, filedata);
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200152
153 strcpy(sfx, "stp_state");
154 read_file(pathbuf);
155 if (LONE_CHAR(filedata, '0'))
156 strcpy(filedata, "no");
157 else
158 if (LONE_CHAR(filedata, '1'))
159 strcpy(filedata, "yes");
Ron Yorstoncad3fc72021-02-03 20:47:14 +0100160 fputs_stdout(filedata);
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200161
Denys Vlasenko37a90082019-10-15 12:31:54 +0200162 /* sfx points past "BR/bridge/", turn it into "BR/brif": */
163 sfx[-4] = 'f'; sfx[-3] = '\0';
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200164 tabs = 0;
165 ifaces = opendir(pathbuf);
166 if (ifaces) {
167 while ((ent = readdir(ifaces)) != NULL) {
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200168 if (DOT_OR_DOTDOT(ent->d_name))
169 continue; /* . or .. */
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200170 if (tabs)
171 printf("\t\t\t\t\t");
172 else
173 tabs = 1;
174 printf("\t\t%s\n", ent->d_name);
175 }
176 closedir(ifaces);
177 }
178 if (!tabs) /* bridge has no interfaces */
179 bb_putchar('\n');
180 return 0;
181}
Denys Vlasenko3e463e12019-06-08 12:35:06 +0200182#endif
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000183
Denys Vlasenko3e463e12019-06-08 12:35:06 +0200184#if ENABLE_FEATURE_BRCTL_FANCY
Denys Vlasenko94356082019-04-13 14:17:55 +0200185static void write_uint(const char *name, const char *leaf, unsigned val)
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200186{
187 char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 32];
188 int fd, n;
189
190#if IFNAMSIZ == 16
191 sprintf(pathbuf, "%.16s/%s", name, leaf);
192#else
193 sprintf(pathbuf, "%.*s/%s", (int)IFNAMSIZ, name, leaf);
194#endif
195 fd = xopen(pathbuf, O_WRONLY);
Denys Vlasenko94356082019-04-13 14:17:55 +0200196 n = sprintf(filedata, "%u\n", val);
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200197 if (write(fd, filedata, n) < 0)
198 bb_simple_perror_msg_and_die(name);
Denys Vlasenko33987532019-10-12 19:24:38 +0200199 /* So far all callers exit very soon after calling us.
200 * Do not bother closing fd (unless debugging):
201 */
202 if (ENABLE_FEATURE_CLEAN_UP)
203 close(fd);
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200204}
Martin Lewis6c1af282019-09-15 18:04:49 +0200205
206struct fdb_entry {
207 uint8_t mac_addr[6];
208 uint8_t port_no;
209 uint8_t is_local;
210 uint32_t ageing_timer_value;
211 uint8_t port_hi;
212 uint8_t pad0;
213 uint16_t unused;
214};
215
216static int compare_fdbs(const void *_f0, const void *_f1)
217{
218 const struct fdb_entry *f0 = _f0;
219 const struct fdb_entry *f1 = _f1;
220
221 return memcmp(f0->mac_addr, f1->mac_addr, 6);
222}
223
224static size_t read_bridge_forward_db(const char *name, struct fdb_entry **_fdb)
225{
Denys Vlasenko33987532019-10-12 19:24:38 +0200226 char pathbuf[IFNAMSIZ + sizeof("/brforward") + 8];
Martin Lewis6c1af282019-09-15 18:04:49 +0200227 struct fdb_entry *fdb;
228 size_t nentries;
Martin Lewis6c1af282019-09-15 18:04:49 +0200229 int fd;
230 ssize_t cc;
231
Denys Vlasenko33987532019-10-12 19:24:38 +0200232#if IFNAMSIZ == 16
233 sprintf(pathbuf, "%.16s/brforward", name);
234#else
235 sprintf(pathbuf, "%.*s/brforward", (int)IFNAMSIZ, name);
236#endif
237 fd = open(pathbuf, O_RDONLY);
Martin Lewis6c1af282019-09-15 18:04:49 +0200238 if (fd < 0)
239 bb_error_msg_and_die("bridge %s does not exist", name);
240
241 fdb = NULL;
242 nentries = 0;
243 for (;;) {
244 fdb = xrealloc_vector(fdb, 4, nentries);
245 cc = full_read(fd, &fdb[nentries], sizeof(*fdb));
246 if (cc == 0) {
247 break;
248 }
249 if (cc != sizeof(*fdb)) {
250 bb_perror_msg_and_die("can't read bridge %s forward db", name);
251 }
252 ++nentries;
253 }
254
Denys Vlasenko33987532019-10-12 19:24:38 +0200255 if (ENABLE_FEATURE_CLEAN_UP)
256 close(fd);
Martin Lewis6c1af282019-09-15 18:04:49 +0200257
258 qsort(fdb, nentries, sizeof(*fdb), compare_fdbs);
259
260 *_fdb = fdb;
261 return nentries;
262}
263
264static void show_bridge_macs(const char *name)
265{
266 struct fdb_entry *fdb;
267 size_t nentries;
268 size_t i;
269
270 nentries = read_bridge_forward_db(name, &fdb);
271
272 printf("port no\tmac addr\t\tis local?\tageing timer\n");
273 for (i = 0; i < nentries; ++i) {
274 const struct fdb_entry *f = &fdb[i];
Denys Vlasenko33987532019-10-12 19:24:38 +0200275 unsigned tv_sec = f->ageing_timer_value / 100;
276 unsigned tv_csec = f->ageing_timer_value % 100;
Martin Lewis6c1af282019-09-15 18:04:49 +0200277 printf("%3u\t"
278 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\t"
279 "%s\t\t"
280 "%4u.%.2u\n",
281 f->port_no,
282 f->mac_addr[0], f->mac_addr[1], f->mac_addr[2],
283 f->mac_addr[3], f->mac_addr[4], f->mac_addr[5],
284 (f->is_local ? "yes" : "no"),
Denys Vlasenko33987532019-10-12 19:24:38 +0200285 tv_sec, tv_csec
Martin Lewis6c1af282019-09-15 18:04:49 +0200286 );
287 }
288
Denys Vlasenko33987532019-10-12 19:24:38 +0200289 if (ENABLE_FEATURE_CLEAN_UP)
290 free(fdb);
Martin Lewis6c1af282019-09-15 18:04:49 +0200291}
Martin Lewis6dcf5632019-10-10 16:00:19 -0500292
293static void show_bridge_timer(const char *msg)
294{
Denys Vlasenko33987532019-10-12 19:24:38 +0200295 unsigned long long centisec = xstrtoull(filedata, 0);
296 unsigned tv_sec = centisec / 100;
297 unsigned tv_csec = centisec % 100;
298 printf("%s%4u.%.2u", msg, tv_sec, tv_csec);
Martin Lewis6dcf5632019-10-10 16:00:19 -0500299}
300
301static const char *show_bridge_state(unsigned state)
302{
303 /* See linux/if_bridge.h, BR_STATE_ constants */
Denys Vlasenko33987532019-10-12 19:24:38 +0200304 static const char state_names[] ALIGN1 =
Martin Lewis6dcf5632019-10-10 16:00:19 -0500305 "disabled\0" //BR_STATE_DISABLED 0
306 "listening\0" //BR_STATE_LISTENING 1
307 "learning\0" //BR_STATE_LEARNING 2
308 "forwarding\0" //BR_STATE_FORWARDING 3
309 "blocking" //BR_STATE_BLOCKING 4
310 ;
311 if (state < 5)
312 return nth_string(state_names, state);
313 return utoa(state);
314}
315
316static void printf_xstrtou(const char *fmt)
317{
318 printf(fmt, xstrtou(filedata, 0));
319}
320
Denys Vlasenko53b2fdc2021-10-10 13:50:53 +0200321static NOINLINE void show_bridge_port(const char *name)
Martin Lewis6dcf5632019-10-10 16:00:19 -0500322{
Denys Vlasenko33987532019-10-12 19:24:38 +0200323 char pathbuf[IFNAMSIZ + sizeof("/brport/forward_delay_timer") + 8];
Martin Lewis6dcf5632019-10-10 16:00:19 -0500324 char *sfx;
325
326#if IFNAMSIZ == 16
327 sfx = pathbuf + sprintf(pathbuf, "%.16s/brport/", name);
328#else
329 sfx = pathbuf + sprintf(pathbuf, "%.*s/brport/", (int)IFNAMSIZ, name);
330#endif
331
332 strcpy(sfx, "port_no");
333 read_file(pathbuf);
334 printf("%s (%u)\n", name, xstrtou(filedata, 0));
335
336 strcpy(sfx + 5, "id"); // "port_id"
337 read_file(pathbuf);
338 printf_xstrtou(" port id\t\t%.4x");
339
340 strcpy(sfx, "state");
341 read_file(pathbuf);
342 printf("\t\t\tstate\t\t%15s\n", show_bridge_state(xstrtou(filedata, 0)));
343
344 strcpy(sfx, "designated_root");
345 read_file(pathbuf);
346 printf(" designated root\t%s", filedata);
347
348 strcpy(sfx, "path_cost");
349 read_file(pathbuf);
350 printf_xstrtou("\tpath cost\t\t%4u\n");
351
352 strcpy(sfx, "designated_bridge");
353 read_file(pathbuf);
354 printf(" designated bridge\t%s", filedata);
355
356 strcpy(sfx, "message_age_timer");
357 read_file(pathbuf);
358 show_bridge_timer("\tmessage age timer\t");
359
360 strcpy(sfx, "designated_port");
361 read_file(pathbuf);
362 printf_xstrtou("\n designated port\t%.4x");
363
364 strcpy(sfx, "forward_delay_timer");
365 read_file(pathbuf);
366 show_bridge_timer("\t\t\tforward delay timer\t");
367
368 strcpy(sfx, "designated_cost");
369 read_file(pathbuf);
370 printf_xstrtou("\n designated cost\t%4u");
371
372 strcpy(sfx, "hold_timer");
373 read_file(pathbuf);
374 show_bridge_timer("\t\t\thold timer\t\t");
375
376 printf("\n flags\t\t\t");
377
378 strcpy(sfx, "config_pending");
379 read_file(pathbuf);
380 if (!LONE_CHAR(filedata, '0'))
381 printf("CONFIG_PENDING ");
382
383 strcpy(sfx, "change_ack");
384 read_file(pathbuf);
385 if (!LONE_CHAR(filedata, '0'))
386 printf("TOPOLOGY_CHANGE_ACK ");
387
388 strcpy(sfx, "hairpin_mode");
389 read_file(pathbuf);
390 if (!LONE_CHAR(filedata, '0'))
391 printf_xstrtou("\n hairpin mode\t\t%4u");
392
393 printf("\n\n");
394}
395
Martin Lewis6dcf5632019-10-10 16:00:19 -0500396static void show_bridge_stp(const char *name)
397{
Denys Vlasenko33987532019-10-12 19:24:38 +0200398 char pathbuf[IFNAMSIZ + sizeof("/bridge/topology_change_timer") + 8];
Martin Lewis6dcf5632019-10-10 16:00:19 -0500399 char *sfx;
400
401#if IFNAMSIZ == 16
402 sfx = pathbuf + sprintf(pathbuf, "%.16s/bridge/", name);
403#else
404 sfx = pathbuf + sprintf(pathbuf, "%.*s/bridge/", (int)IFNAMSIZ, name);
405#endif
406
407 strcpy(sfx, "bridge_id");
408 if (read_file(pathbuf) < 0)
409 bb_error_msg_and_die("bridge %s does not exist", name);
410
411 printf("%s\n"
412 " bridge id\t\t%s", name, filedata);
413
414 strcpy(sfx, "root_id");
415 read_file(pathbuf);
416 printf("\n designated root\t%s", filedata);
417
418 strcpy(sfx + 5, "port"); // "root_port"
419 read_file(pathbuf);
420 printf_xstrtou("\n root port\t\t%4u\t\t\t");
421
422 strcpy(sfx + 6, "ath_cost"); // "root_path_cost"
423 read_file(pathbuf);
424 printf_xstrtou("path cost\t\t%4u\n");
425
426 strcpy(sfx, "max_age");
427 read_file(pathbuf);
428 show_bridge_timer(" max age\t\t");
429 show_bridge_timer("\t\t\tbridge max age\t\t");
430
431 strcpy(sfx, "hello_time");
432 read_file(pathbuf);
433 show_bridge_timer("\n hello time\t\t");
434 show_bridge_timer("\t\t\tbridge hello time\t");
435
436 strcpy(sfx, "forward_delay");
437 read_file(pathbuf);
438 show_bridge_timer("\n forward delay\t\t");
439 show_bridge_timer("\t\t\tbridge forward delay\t");
440
441 strcpy(sfx, "ageing_time");
442 read_file(pathbuf);
443 show_bridge_timer("\n ageing time\t\t");
444
445 strcpy(sfx, "hello_timer");
446 read_file(pathbuf);
447 show_bridge_timer("\n hello timer\t\t");
448
449 strcpy(sfx, "tcn_timer");
450 read_file(pathbuf);
451 show_bridge_timer("\t\t\ttcn timer\t\t");
452
453 strcpy(sfx, "topology_change_timer");
454 read_file(pathbuf);
455 show_bridge_timer("\n topology change timer\t");
456
457 strcpy(sfx, "gc_timer");
458 read_file(pathbuf);
459 show_bridge_timer("\t\t\tgc timer\t\t");
460
461 printf("\n flags\t\t\t");
462
463 strcpy(sfx, "topology_change");
464 read_file(pathbuf);
465 if (!LONE_CHAR(filedata, '0'))
466 printf("TOPOLOGY_CHANGE ");
467
468 strcpy(sfx, "topology_change_detected");
469 read_file(pathbuf);
470 if (!LONE_CHAR(filedata, '0'))
471 printf("TOPOLOGY_CHANGE_DETECTED ");
472 printf("\n\n\n");
473
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200474 /* Show bridge ports */
475 {
476 DIR *ifaces;
477
Denys Vlasenko37a90082019-10-15 12:31:54 +0200478 /* sfx points past "BR/bridge/", turn it into "BR/brif": */
479 sfx[-4] = 'f'; sfx[-3] = '\0';
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200480 ifaces = opendir(pathbuf);
481 if (ifaces) {
482 struct dirent *ent;
483 while ((ent = readdir(ifaces)) != NULL) {
484 if (DOT_OR_DOTDOT(ent->d_name))
485 continue; /* . or .. */
486 show_bridge_port(ent->d_name);
487 }
488 if (ENABLE_FEATURE_CLEAN_UP)
489 closedir(ifaces);
490 }
491 }
Martin Lewis6dcf5632019-10-10 16:00:19 -0500492}
Denys Vlasenko3e463e12019-06-08 12:35:06 +0200493#endif
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200494
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000495int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000496int brctl_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000497{
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000498 static const char keywords[] ALIGN1 =
499 "addbr\0" "delbr\0" "addif\0" "delif\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000500 IF_FEATURE_BRCTL_FANCY(
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000501 "stp\0"
Martin Lewis6dcf5632019-10-10 16:00:19 -0500502 "showstp\0"
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000503 "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200504 "setpathcost\0" "setportprio\0"
505 "setbridgeprio\0"
Martin Lewis6c1af282019-09-15 18:04:49 +0200506 "showmacs\0"
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000507 )
Nicolas Thillf47ce072012-09-25 14:06:01 +0200508 IF_FEATURE_BRCTL_SHOW("show\0");
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000509 enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000510 IF_FEATURE_BRCTL_FANCY(,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100511 ARG_stp,
Martin Lewis6dcf5632019-10-10 16:00:19 -0500512 ARG_showstp,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100513 ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200514 ARG_setpathcost, ARG_setportprio,
Martin Lewis6c1af282019-09-15 18:04:49 +0200515 ARG_setbridgeprio,
516 ARG_showmacs
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000517 )
Nicolas Thillf47ce072012-09-25 14:06:01 +0200518 IF_FEATURE_BRCTL_SHOW(, ARG_show)
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000519 };
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200520 int key;
521 char *br;
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000522
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000523 argv++;
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200524 if (!*argv) {
525 /* bare "brctl" shows --help */
526 bb_show_usage();
527 }
528
529 xchdir("/sys/class/net");
530
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200531 key = index_in_strings(keywords, *argv);
532 if (key == -1) /* no match found in keywords array, bail out. */
533 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
534 argv++;
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000535
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000536#if ENABLE_FEATURE_BRCTL_SHOW
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200537 if (key == ARG_show) { /* show [BR]... */
538 DIR *net;
539 struct dirent *ent;
540 int need_hdr = 1;
541 int exitcode = EXIT_SUCCESS;
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000542
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200543 if (*argv) {
544 /* "show BR1 BR2 BR3" */
545 do {
546 if (show_bridge(*argv, need_hdr) >= 0) {
547 need_hdr = 0;
548 } else {
549 bb_error_msg("bridge %s does not exist", *argv);
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200550//TODO: if device exists, but is not a BR, brctl from bridge-utils 1.6
551//says this instead: "device eth0 is not a bridge"
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200552 exitcode = EXIT_FAILURE;
553 }
554 } while (*++argv != NULL);
Denys Vlasenkoc5150e92019-04-12 18:52:31 +0200555 return exitcode;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000556 }
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200557
558 /* "show" (if no ifaces, shows nothing, not even header) */
559 net = xopendir(".");
560 while ((ent = readdir(net)) != NULL) {
561 if (DOT_OR_DOTDOT(ent->d_name))
562 continue; /* . or .. */
563 if (show_bridge(ent->d_name, need_hdr) >= 0)
564 need_hdr = 0;
565 }
566 if (ENABLE_FEATURE_CLEAN_UP)
567 closedir(net);
568 return exitcode;
569 }
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000570#endif
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000571
Denys Vlasenkoeb139512019-10-12 19:51:46 +0200572 if (!*argv) /* All of the below need at least one argument */
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200573 bb_show_usage();
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000574
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200575 br = *argv++;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000576
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200577 if (key == ARG_addbr || key == ARG_delbr) {
578 /* brctl from bridge-utils 1.6 still uses ioctl
579 * for SIOCBRADDBR / SIOCBRDELBR, not /sys accesses
580 */
581 int fd = xsocket(AF_INET, SOCK_STREAM, 0);
582 ioctl_or_perror_and_die(fd,
583 key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
584 br, "bridge %s", br
585 );
586 //close(fd);
587 //goto done;
588 /* bridge-utils 1.6 simply ignores trailing args:
589 * "brctl addbr BR1 ARGS" ignores ARGS
590 */
591 if (ENABLE_FEATURE_CLEAN_UP)
592 close(fd);
593 return EXIT_SUCCESS;
594 }
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000595
Denys Vlasenko6eb38fd2020-06-23 02:46:53 +0200596#if ENABLE_FEATURE_BRCTL_FANCY
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200597 if (key == ARG_showmacs) {
598 show_bridge_macs(br);
599 return EXIT_SUCCESS;
600 }
601 if (key == ARG_showstp) {
602 show_bridge_stp(br);
603 return EXIT_SUCCESS;
604 }
Denys Vlasenko6eb38fd2020-06-23 02:46:53 +0200605#endif
Martin Lewis6c1af282019-09-15 18:04:49 +0200606
Denys Vlasenkoeb139512019-10-12 19:51:46 +0200607 if (!*argv) /* All of the below need at least two arguments */
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200608 bb_show_usage();
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000609
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000610#if ENABLE_FEATURE_BRCTL_FANCY
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200611 if (key == ARG_stp) {
612 static const char no_yes[] ALIGN1 =
613 "0\0" "off\0" "n\0" "no\0" /* 0 .. 3 */
614 "1\0" "on\0" "y\0" "yes\0"; /* 4 .. 7 */
615 int onoff = index_in_strings(no_yes, *argv);
616 if (onoff < 0)
617 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
618 onoff = (unsigned)onoff / 4;
619 write_uint(br, "bridge/stp_state", onoff);
620 return EXIT_SUCCESS;
621 }
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200622
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200623 if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
624 /* setageing BR N: "N*100\n" to /sys/class/net/BR/bridge/ageing_time
625 * setfd BR N: "N*100\n" to /sys/class/net/BR/bridge/forward_delay
626 * sethello BR N: "N*100\n" to /sys/class/net/BR/bridge/hello_time
627 * setmaxage BR N: "N*100\n" to /sys/class/net/BR/bridge/max_age
628 */
629 write_uint(br,
630 nth_string(
631 "bridge/ageing_time" "\0" /* ARG_setageing */
632 "bridge/forward_delay""\0" /* ARG_setfd */
633 "bridge/hello_time" "\0" /* ARG_sethello */
634 "bridge/max_age", /* ARG_setmaxage */
635 key - ARG_setageing
636 ),
637 str_to_jiffies(*argv)
638 );
639 return EXIT_SUCCESS;
640 }
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200641
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200642 if (key == ARG_setbridgeprio) {
643 write_uint(br, "bridge/priority", xatoi_positive(*argv));
644 return EXIT_SUCCESS;
645 }
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200646
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200647 if (key == ARG_setpathcost
648 || key == ARG_setportprio
649 ) {
650 if (!argv[1])
651 bb_show_usage();
652 /* BR is not used (and ignored!) for these commands:
653 * "setpathcost BR PORT N" writes "N\n" to
654 * /sys/class/net/PORT/brport/path_cost
655 * "setportprio BR PORT N" writes "N\n" to
656 * /sys/class/net/PORT/brport/priority
657 */
658 write_uint(argv[0],
659 nth_string(
660 "brport/path_cost" "\0" /* ARG_setpathcost */
661 "brport/priority", /* ARG_setportprio */
662 key - ARG_setpathcost
663 ),
664 xatoi_positive(argv[1])
665 );
666 return EXIT_SUCCESS;
667 }
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000668#endif
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200669 /* always true: if (key == ARG_addif || key == ARG_delif) */ {
670 struct ifreq ifr;
671 int fd = xsocket(AF_INET, SOCK_STREAM, 0);
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200672
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200673 strncpy_IFNAMSIZ(ifr.ifr_name, br);
674 ifr.ifr_ifindex = if_nametoindex(*argv);
675 if (ifr.ifr_ifindex == 0) {
676 bb_perror_msg_and_die("iface %s", *argv);
Denys Vlasenkodc1b2d42019-04-13 13:58:06 +0200677 }
Denys Vlasenkob4fa16d2019-10-12 19:42:37 +0200678 ioctl_or_perror_and_die(fd,
679 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
680 &ifr, "bridge %s", br
681 );
682 if (ENABLE_FEATURE_CLEAN_UP)
683 close(fd);
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000684 }
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000685
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000686 return EXIT_SUCCESS;
687}