brctl.c 17 KB

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