3
0

brctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Small implementation of brctl for busybox.
  4. *
  5. * Copyright (C) 2008 by Bernhard Reutner-Fischer
  6. *
  7. * Some helper functions from bridge-utils are
  8. * Copyright (C) 2000 Lennert Buytenhek
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. //config:config BRCTL
  13. //config: bool "brctl (4.7 kb)"
  14. //config: default y
  15. //config: help
  16. //config: Manage ethernet bridges.
  17. //config: Supports addbr/delbr and addif/delif.
  18. //config:
  19. //config:config FEATURE_BRCTL_FANCY
  20. //config: bool "Fancy options"
  21. //config: default y
  22. //config: depends on BRCTL
  23. //config: help
  24. //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.
  29. //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
  35. //config: Add support for option which prints the current config:
  36. //config: show
  37. //applet:IF_BRCTL(APPLET_NOEXEC(brctl, brctl, BB_DIR_USR_SBIN, BB_SUID_DROP, brctl))
  38. //kbuild:lib-$(CONFIG_BRCTL) += brctl.o
  39. //usage:#define brctl_trivial_usage
  40. //usage: "COMMAND [BRIDGE [ARGS]]"
  41. //usage:#define brctl_full_usage "\n\n"
  42. //usage: "Manage ethernet bridges"
  43. //usage: "\nCommands:"
  44. //usage: IF_FEATURE_BRCTL_SHOW(
  45. //usage: "\n show [BRIDGE]... Show bridges"
  46. //usage: )
  47. //usage: "\n addbr BRIDGE Create BRIDGE"
  48. //usage: "\n delbr BRIDGE Delete BRIDGE"
  49. //usage: "\n addif BRIDGE IFACE Add IFACE to BRIDGE"
  50. //usage: "\n delif BRIDGE IFACE Delete IFACE from BRIDGE"
  51. //usage: IF_FEATURE_BRCTL_FANCY(
  52. //usage: "\n showmacs BRIDGE List MAC addresses"
  53. //usage: "\n showstp BRIDGE Show STP info"
  54. //usage: "\n stp BRIDGE 1/yes/on|0/no/off Set STP on/off"
  55. //usage: "\n setageing BRIDGE SECONDS Set ageing time"
  56. //usage: "\n setfd BRIDGE SECONDS Set bridge forward delay"
  57. //usage: "\n sethello BRIDGE SECONDS Set hello time"
  58. //usage: "\n setmaxage BRIDGE SECONDS Set max message age"
  59. //usage: "\n setbridgeprio BRIDGE PRIO Set bridge priority"
  60. //usage: "\n setportprio BRIDGE IFACE PRIO Set port priority"
  61. //usage: "\n setpathcost BRIDGE IFACE COST Set path cost"
  62. //usage: )
  63. // Not yet implemented:
  64. // hairpin BRIDGE IFACE on|off Set hairpin on/off
  65. #include "libbb.h"
  66. #include "common_bufsiz.h"
  67. #include <linux/sockios.h>
  68. #include <net/if.h>
  69. #ifndef SIOCBRADDBR
  70. # define SIOCBRADDBR BRCTL_ADD_BRIDGE
  71. #endif
  72. #ifndef SIOCBRDELBR
  73. # define SIOCBRDELBR BRCTL_DEL_BRIDGE
  74. #endif
  75. #ifndef SIOCBRADDIF
  76. # define SIOCBRADDIF BRCTL_ADD_IF
  77. #endif
  78. #ifndef SIOCBRDELIF
  79. # define SIOCBRDELIF BRCTL_DEL_IF
  80. #endif
  81. #if ENABLE_FEATURE_BRCTL_FANCY
  82. static unsigned str_to_jiffies(const char *time_str)
  83. {
  84. double dd;
  85. char *endptr;
  86. dd = /*bb_*/strtod(time_str, &endptr);
  87. if (endptr == time_str || dd < 0)
  88. bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec");
  89. dd *= 100;
  90. /* For purposes of brctl,
  91. * capping SECONDS by ~20 million seconds is quite enough:
  92. */
  93. if (dd > INT_MAX)
  94. dd = INT_MAX;
  95. return dd;
  96. }
  97. #endif
  98. #define filedata bb_common_bufsiz1
  99. #if ENABLE_FEATURE_BRCTL_SHOW || ENABLE_FEATURE_BRCTL_FANCY
  100. static int read_file(const char *name)
  101. {
  102. int n = open_read_close(name, filedata, COMMON_BUFSIZE - 1);
  103. if (n < 0) {
  104. filedata[0] = '\0';
  105. } else {
  106. filedata[n] = '\0';
  107. if (n != 0 && filedata[n - 1] == '\n')
  108. filedata[--n] = '\0';
  109. }
  110. return n;
  111. }
  112. #endif
  113. #if ENABLE_FEATURE_BRCTL_SHOW
  114. /* NB: we are in /sys/class/net
  115. */
  116. static int show_bridge(const char *name, int need_hdr)
  117. {
  118. /* Output:
  119. *bridge name bridge id STP enabled interfaces
  120. *br0 8000.000000000000 no eth0
  121. */
  122. char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 8];
  123. int tabs;
  124. DIR *ifaces;
  125. struct dirent *ent;
  126. char *sfx;
  127. #if IFNAMSIZ == 16
  128. sfx = pathbuf + sprintf(pathbuf, "%.16s/bridge/", name);
  129. #else
  130. sfx = pathbuf + sprintf(pathbuf, "%.*s/bridge/", (int)IFNAMSIZ, name);
  131. #endif
  132. strcpy(sfx, "bridge_id");
  133. if (read_file(pathbuf) < 0)
  134. return -1; /* this iface is not a bridge */
  135. if (need_hdr)
  136. puts("bridge name\tbridge id\t\tSTP enabled\tinterfaces");
  137. printf("%s\t\t%s\t", name, filedata);
  138. strcpy(sfx, "stp_state");
  139. read_file(pathbuf);
  140. if (LONE_CHAR(filedata, '0'))
  141. strcpy(filedata, "no");
  142. else
  143. if (LONE_CHAR(filedata, '1'))
  144. strcpy(filedata, "yes");
  145. fputs(filedata, stdout);
  146. /* sfx points past "BR/bridge/", turn it into "BR/brif": */
  147. sfx[-4] = 'f'; sfx[-3] = '\0';
  148. tabs = 0;
  149. ifaces = opendir(pathbuf);
  150. if (ifaces) {
  151. while ((ent = readdir(ifaces)) != NULL) {
  152. if (DOT_OR_DOTDOT(ent->d_name))
  153. continue; /* . or .. */
  154. if (tabs)
  155. printf("\t\t\t\t\t");
  156. else
  157. tabs = 1;
  158. printf("\t\t%s\n", ent->d_name);
  159. }
  160. closedir(ifaces);
  161. }
  162. if (!tabs) /* bridge has no interfaces */
  163. bb_putchar('\n');
  164. return 0;
  165. }
  166. #endif
  167. #if ENABLE_FEATURE_BRCTL_FANCY
  168. static void write_uint(const char *name, const char *leaf, unsigned val)
  169. {
  170. char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 32];
  171. int fd, n;
  172. #if IFNAMSIZ == 16
  173. sprintf(pathbuf, "%.16s/%s", name, leaf);
  174. #else
  175. sprintf(pathbuf, "%.*s/%s", (int)IFNAMSIZ, name, leaf);
  176. #endif
  177. fd = xopen(pathbuf, O_WRONLY);
  178. n = sprintf(filedata, "%u\n", val);
  179. if (write(fd, filedata, n) < 0)
  180. bb_simple_perror_msg_and_die(name);
  181. /* So far all callers exit very soon after calling us.
  182. * Do not bother closing fd (unless debugging):
  183. */
  184. if (ENABLE_FEATURE_CLEAN_UP)
  185. close(fd);
  186. }
  187. struct fdb_entry {
  188. uint8_t mac_addr[6];
  189. uint8_t port_no;
  190. uint8_t is_local;
  191. uint32_t ageing_timer_value;
  192. uint8_t port_hi;
  193. uint8_t pad0;
  194. uint16_t unused;
  195. };
  196. static int compare_fdbs(const void *_f0, const void *_f1)
  197. {
  198. const struct fdb_entry *f0 = _f0;
  199. const struct fdb_entry *f1 = _f1;
  200. return memcmp(f0->mac_addr, f1->mac_addr, 6);
  201. }
  202. static size_t read_bridge_forward_db(const char *name, struct fdb_entry **_fdb)
  203. {
  204. char pathbuf[IFNAMSIZ + sizeof("/brforward") + 8];
  205. struct fdb_entry *fdb;
  206. size_t nentries;
  207. int fd;
  208. ssize_t cc;
  209. #if IFNAMSIZ == 16
  210. sprintf(pathbuf, "%.16s/brforward", name);
  211. #else
  212. sprintf(pathbuf, "%.*s/brforward", (int)IFNAMSIZ, name);
  213. #endif
  214. fd = open(pathbuf, O_RDONLY);
  215. if (fd < 0)
  216. bb_error_msg_and_die("bridge %s does not exist", name);
  217. fdb = NULL;
  218. nentries = 0;
  219. for (;;) {
  220. fdb = xrealloc_vector(fdb, 4, nentries);
  221. cc = full_read(fd, &fdb[nentries], sizeof(*fdb));
  222. if (cc == 0) {
  223. break;
  224. }
  225. if (cc != sizeof(*fdb)) {
  226. bb_perror_msg_and_die("can't read bridge %s forward db", name);
  227. }
  228. ++nentries;
  229. }
  230. if (ENABLE_FEATURE_CLEAN_UP)
  231. close(fd);
  232. qsort(fdb, nentries, sizeof(*fdb), compare_fdbs);
  233. *_fdb = fdb;
  234. return nentries;
  235. }
  236. static void show_bridge_macs(const char *name)
  237. {
  238. struct fdb_entry *fdb;
  239. size_t nentries;
  240. size_t i;
  241. nentries = read_bridge_forward_db(name, &fdb);
  242. printf("port no\tmac addr\t\tis local?\tageing timer\n");
  243. for (i = 0; i < nentries; ++i) {
  244. const struct fdb_entry *f = &fdb[i];
  245. unsigned tv_sec = f->ageing_timer_value / 100;
  246. unsigned tv_csec = f->ageing_timer_value % 100;
  247. printf("%3u\t"
  248. "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\t"
  249. "%s\t\t"
  250. "%4u.%.2u\n",
  251. f->port_no,
  252. f->mac_addr[0], f->mac_addr[1], f->mac_addr[2],
  253. f->mac_addr[3], f->mac_addr[4], f->mac_addr[5],
  254. (f->is_local ? "yes" : "no"),
  255. tv_sec, tv_csec
  256. );
  257. }
  258. if (ENABLE_FEATURE_CLEAN_UP)
  259. free(fdb);
  260. }
  261. static void show_bridge_timer(const char *msg)
  262. {
  263. unsigned long long centisec = xstrtoull(filedata, 0);
  264. unsigned tv_sec = centisec / 100;
  265. unsigned tv_csec = centisec % 100;
  266. printf("%s%4u.%.2u", msg, tv_sec, tv_csec);
  267. }
  268. static const char *show_bridge_state(unsigned state)
  269. {
  270. /* See linux/if_bridge.h, BR_STATE_ constants */
  271. static const char state_names[] ALIGN1 =
  272. "disabled\0" //BR_STATE_DISABLED 0
  273. "listening\0" //BR_STATE_LISTENING 1
  274. "learning\0" //BR_STATE_LEARNING 2
  275. "forwarding\0" //BR_STATE_FORWARDING 3
  276. "blocking" //BR_STATE_BLOCKING 4
  277. ;
  278. if (state < 5)
  279. return nth_string(state_names, state);
  280. return utoa(state);
  281. }
  282. static void printf_xstrtou(const char *fmt)
  283. {
  284. printf(fmt, xstrtou(filedata, 0));
  285. }
  286. static void show_bridge_port(const char *name)
  287. {
  288. char pathbuf[IFNAMSIZ + sizeof("/brport/forward_delay_timer") + 8];
  289. char *sfx;
  290. #if IFNAMSIZ == 16
  291. sfx = pathbuf + sprintf(pathbuf, "%.16s/brport/", name);
  292. #else
  293. sfx = pathbuf + sprintf(pathbuf, "%.*s/brport/", (int)IFNAMSIZ, name);
  294. #endif
  295. strcpy(sfx, "port_no");
  296. read_file(pathbuf);
  297. printf("%s (%u)\n", name, xstrtou(filedata, 0));
  298. strcpy(sfx + 5, "id"); // "port_id"
  299. read_file(pathbuf);
  300. printf_xstrtou(" port id\t\t%.4x");
  301. strcpy(sfx, "state");
  302. read_file(pathbuf);
  303. printf("\t\t\tstate\t\t%15s\n", show_bridge_state(xstrtou(filedata, 0)));
  304. strcpy(sfx, "designated_root");
  305. read_file(pathbuf);
  306. printf(" designated root\t%s", filedata);
  307. strcpy(sfx, "path_cost");
  308. read_file(pathbuf);
  309. printf_xstrtou("\tpath cost\t\t%4u\n");
  310. strcpy(sfx, "designated_bridge");
  311. read_file(pathbuf);
  312. printf(" designated bridge\t%s", filedata);
  313. strcpy(sfx, "message_age_timer");
  314. read_file(pathbuf);
  315. show_bridge_timer("\tmessage age timer\t");
  316. strcpy(sfx, "designated_port");
  317. read_file(pathbuf);
  318. printf_xstrtou("\n designated port\t%.4x");
  319. strcpy(sfx, "forward_delay_timer");
  320. read_file(pathbuf);
  321. show_bridge_timer("\t\t\tforward delay timer\t");
  322. strcpy(sfx, "designated_cost");
  323. read_file(pathbuf);
  324. printf_xstrtou("\n designated cost\t%4u");
  325. strcpy(sfx, "hold_timer");
  326. read_file(pathbuf);
  327. show_bridge_timer("\t\t\thold timer\t\t");
  328. printf("\n flags\t\t\t");
  329. strcpy(sfx, "config_pending");
  330. read_file(pathbuf);
  331. if (!LONE_CHAR(filedata, '0'))
  332. printf("CONFIG_PENDING ");
  333. strcpy(sfx, "change_ack");
  334. read_file(pathbuf);
  335. if (!LONE_CHAR(filedata, '0'))
  336. printf("TOPOLOGY_CHANGE_ACK ");
  337. strcpy(sfx, "hairpin_mode");
  338. read_file(pathbuf);
  339. if (!LONE_CHAR(filedata, '0'))
  340. printf_xstrtou("\n hairpin mode\t\t%4u");
  341. printf("\n\n");
  342. }
  343. static void show_bridge_stp(const char *name)
  344. {
  345. char pathbuf[IFNAMSIZ + sizeof("/bridge/topology_change_timer") + 8];
  346. char *sfx;
  347. #if IFNAMSIZ == 16
  348. sfx = pathbuf + sprintf(pathbuf, "%.16s/bridge/", name);
  349. #else
  350. sfx = pathbuf + sprintf(pathbuf, "%.*s/bridge/", (int)IFNAMSIZ, name);
  351. #endif
  352. strcpy(sfx, "bridge_id");
  353. if (read_file(pathbuf) < 0)
  354. bb_error_msg_and_die("bridge %s does not exist", name);
  355. printf("%s\n"
  356. " bridge id\t\t%s", name, filedata);
  357. strcpy(sfx, "root_id");
  358. read_file(pathbuf);
  359. printf("\n designated root\t%s", filedata);
  360. strcpy(sfx + 5, "port"); // "root_port"
  361. read_file(pathbuf);
  362. printf_xstrtou("\n root port\t\t%4u\t\t\t");
  363. strcpy(sfx + 6, "ath_cost"); // "root_path_cost"
  364. read_file(pathbuf);
  365. printf_xstrtou("path cost\t\t%4u\n");
  366. strcpy(sfx, "max_age");
  367. read_file(pathbuf);
  368. show_bridge_timer(" max age\t\t");
  369. show_bridge_timer("\t\t\tbridge max age\t\t");
  370. strcpy(sfx, "hello_time");
  371. read_file(pathbuf);
  372. show_bridge_timer("\n hello time\t\t");
  373. show_bridge_timer("\t\t\tbridge hello time\t");
  374. strcpy(sfx, "forward_delay");
  375. read_file(pathbuf);
  376. show_bridge_timer("\n forward delay\t\t");
  377. show_bridge_timer("\t\t\tbridge forward delay\t");
  378. strcpy(sfx, "ageing_time");
  379. read_file(pathbuf);
  380. show_bridge_timer("\n ageing time\t\t");
  381. strcpy(sfx, "hello_timer");
  382. read_file(pathbuf);
  383. show_bridge_timer("\n hello timer\t\t");
  384. strcpy(sfx, "tcn_timer");
  385. read_file(pathbuf);
  386. show_bridge_timer("\t\t\ttcn timer\t\t");
  387. strcpy(sfx, "topology_change_timer");
  388. read_file(pathbuf);
  389. show_bridge_timer("\n topology change timer\t");
  390. strcpy(sfx, "gc_timer");
  391. read_file(pathbuf);
  392. show_bridge_timer("\t\t\tgc timer\t\t");
  393. printf("\n flags\t\t\t");
  394. strcpy(sfx, "topology_change");
  395. read_file(pathbuf);
  396. if (!LONE_CHAR(filedata, '0'))
  397. printf("TOPOLOGY_CHANGE ");
  398. strcpy(sfx, "topology_change_detected");
  399. read_file(pathbuf);
  400. if (!LONE_CHAR(filedata, '0'))
  401. printf("TOPOLOGY_CHANGE_DETECTED ");
  402. printf("\n\n\n");
  403. /* Show bridge ports */
  404. {
  405. DIR *ifaces;
  406. /* sfx points past "BR/bridge/", turn it into "BR/brif": */
  407. sfx[-4] = 'f'; sfx[-3] = '\0';
  408. ifaces = opendir(pathbuf);
  409. if (ifaces) {
  410. struct dirent *ent;
  411. while ((ent = readdir(ifaces)) != NULL) {
  412. if (DOT_OR_DOTDOT(ent->d_name))
  413. continue; /* . or .. */
  414. show_bridge_port(ent->d_name);
  415. }
  416. if (ENABLE_FEATURE_CLEAN_UP)
  417. closedir(ifaces);
  418. }
  419. }
  420. }
  421. #endif
  422. int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  423. int brctl_main(int argc UNUSED_PARAM, char **argv)
  424. {
  425. static const char keywords[] ALIGN1 =
  426. "addbr\0" "delbr\0" "addif\0" "delif\0"
  427. IF_FEATURE_BRCTL_FANCY(
  428. "stp\0"
  429. "showstp\0"
  430. "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
  431. "setpathcost\0" "setportprio\0"
  432. "setbridgeprio\0"
  433. "showmacs\0"
  434. )
  435. IF_FEATURE_BRCTL_SHOW("show\0");
  436. enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
  437. IF_FEATURE_BRCTL_FANCY(,
  438. ARG_stp,
  439. ARG_showstp,
  440. ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
  441. ARG_setpathcost, ARG_setportprio,
  442. ARG_setbridgeprio,
  443. ARG_showmacs
  444. )
  445. IF_FEATURE_BRCTL_SHOW(, ARG_show)
  446. };
  447. int key;
  448. char *br;
  449. argv++;
  450. if (!*argv) {
  451. /* bare "brctl" shows --help */
  452. bb_show_usage();
  453. }
  454. xchdir("/sys/class/net");
  455. key = index_in_strings(keywords, *argv);
  456. if (key == -1) /* no match found in keywords array, bail out. */
  457. bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
  458. argv++;
  459. #if ENABLE_FEATURE_BRCTL_SHOW
  460. if (key == ARG_show) { /* show [BR]... */
  461. DIR *net;
  462. struct dirent *ent;
  463. int need_hdr = 1;
  464. int exitcode = EXIT_SUCCESS;
  465. if (*argv) {
  466. /* "show BR1 BR2 BR3" */
  467. do {
  468. if (show_bridge(*argv, need_hdr) >= 0) {
  469. need_hdr = 0;
  470. } else {
  471. bb_error_msg("bridge %s does not exist", *argv);
  472. //TODO: if device exists, but is not a BR, brctl from bridge-utils 1.6
  473. //says this instead: "device eth0 is not a bridge"
  474. exitcode = EXIT_FAILURE;
  475. }
  476. } while (*++argv != NULL);
  477. return exitcode;
  478. }
  479. /* "show" (if no ifaces, shows nothing, not even header) */
  480. net = xopendir(".");
  481. while ((ent = readdir(net)) != NULL) {
  482. if (DOT_OR_DOTDOT(ent->d_name))
  483. continue; /* . or .. */
  484. if (show_bridge(ent->d_name, need_hdr) >= 0)
  485. need_hdr = 0;
  486. }
  487. if (ENABLE_FEATURE_CLEAN_UP)
  488. closedir(net);
  489. return exitcode;
  490. }
  491. #endif
  492. if (!*argv) /* All of the below need at least one argument */
  493. bb_show_usage();
  494. br = *argv++;
  495. if (key == ARG_addbr || key == ARG_delbr) {
  496. /* brctl from bridge-utils 1.6 still uses ioctl
  497. * for SIOCBRADDBR / SIOCBRDELBR, not /sys accesses
  498. */
  499. int fd = xsocket(AF_INET, SOCK_STREAM, 0);
  500. ioctl_or_perror_and_die(fd,
  501. key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
  502. br, "bridge %s", br
  503. );
  504. //close(fd);
  505. //goto done;
  506. /* bridge-utils 1.6 simply ignores trailing args:
  507. * "brctl addbr BR1 ARGS" ignores ARGS
  508. */
  509. if (ENABLE_FEATURE_CLEAN_UP)
  510. close(fd);
  511. return EXIT_SUCCESS;
  512. }
  513. #if ENABLE_FEATURE_BRCTL_FANCY
  514. if (key == ARG_showmacs) {
  515. show_bridge_macs(br);
  516. return EXIT_SUCCESS;
  517. }
  518. if (key == ARG_showstp) {
  519. show_bridge_stp(br);
  520. return EXIT_SUCCESS;
  521. }
  522. #endif
  523. if (!*argv) /* All of the below need at least two arguments */
  524. bb_show_usage();
  525. #if ENABLE_FEATURE_BRCTL_FANCY
  526. if (key == ARG_stp) {
  527. static const char no_yes[] ALIGN1 =
  528. "0\0" "off\0" "n\0" "no\0" /* 0 .. 3 */
  529. "1\0" "on\0" "y\0" "yes\0"; /* 4 .. 7 */
  530. int onoff = index_in_strings(no_yes, *argv);
  531. if (onoff < 0)
  532. bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
  533. onoff = (unsigned)onoff / 4;
  534. write_uint(br, "bridge/stp_state", onoff);
  535. return EXIT_SUCCESS;
  536. }
  537. if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
  538. /* setageing BR N: "N*100\n" to /sys/class/net/BR/bridge/ageing_time
  539. * setfd BR N: "N*100\n" to /sys/class/net/BR/bridge/forward_delay
  540. * sethello BR N: "N*100\n" to /sys/class/net/BR/bridge/hello_time
  541. * setmaxage BR N: "N*100\n" to /sys/class/net/BR/bridge/max_age
  542. */
  543. write_uint(br,
  544. nth_string(
  545. "bridge/ageing_time" "\0" /* ARG_setageing */
  546. "bridge/forward_delay""\0" /* ARG_setfd */
  547. "bridge/hello_time" "\0" /* ARG_sethello */
  548. "bridge/max_age", /* ARG_setmaxage */
  549. key - ARG_setageing
  550. ),
  551. str_to_jiffies(*argv)
  552. );
  553. return EXIT_SUCCESS;
  554. }
  555. if (key == ARG_setbridgeprio) {
  556. write_uint(br, "bridge/priority", xatoi_positive(*argv));
  557. return EXIT_SUCCESS;
  558. }
  559. if (key == ARG_setpathcost
  560. || key == ARG_setportprio
  561. ) {
  562. if (!argv[1])
  563. bb_show_usage();
  564. /* BR is not used (and ignored!) for these commands:
  565. * "setpathcost BR PORT N" writes "N\n" to
  566. * /sys/class/net/PORT/brport/path_cost
  567. * "setportprio BR PORT N" writes "N\n" to
  568. * /sys/class/net/PORT/brport/priority
  569. */
  570. write_uint(argv[0],
  571. nth_string(
  572. "brport/path_cost" "\0" /* ARG_setpathcost */
  573. "brport/priority", /* ARG_setportprio */
  574. key - ARG_setpathcost
  575. ),
  576. xatoi_positive(argv[1])
  577. );
  578. return EXIT_SUCCESS;
  579. }
  580. #endif
  581. /* always true: if (key == ARG_addif || key == ARG_delif) */ {
  582. struct ifreq ifr;
  583. int fd = xsocket(AF_INET, SOCK_STREAM, 0);
  584. strncpy_IFNAMSIZ(ifr.ifr_name, br);
  585. ifr.ifr_ifindex = if_nametoindex(*argv);
  586. if (ifr.ifr_ifindex == 0) {
  587. bb_perror_msg_and_die("iface %s", *argv);
  588. }
  589. ioctl_or_perror_and_die(fd,
  590. key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
  591. &ifr, "bridge %s", br
  592. );
  593. if (ENABLE_FEATURE_CLEAN_UP)
  594. close(fd);
  595. }
  596. return EXIT_SUCCESS;
  597. }