brctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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: select PLATFORM_LINUX
  16. //config: help
  17. //config: Manage ethernet bridges.
  18. //config: Supports addbr/delbr and addif/delif.
  19. //config:
  20. //config:config FEATURE_BRCTL_FANCY
  21. //config: bool "Fancy options"
  22. //config: default y
  23. //config: depends on BRCTL
  24. //config: help
  25. //config: Add support for extended option like:
  26. //config: setageing, setfd, sethello, setmaxage,
  27. //config: setpathcost, setportprio, setbridgeprio,
  28. //config: stp
  29. //config: This adds about 600 bytes.
  30. //config:
  31. //config:config FEATURE_BRCTL_SHOW
  32. //config: bool "Support show"
  33. //config: default y
  34. //config: depends on BRCTL && FEATURE_BRCTL_FANCY
  35. //config: help
  36. //config: Add support for option which prints the current config:
  37. //config: show
  38. //applet:IF_BRCTL(APPLET_NOEXEC(brctl, brctl, BB_DIR_USR_SBIN, BB_SUID_DROP, brctl))
  39. //kbuild:lib-$(CONFIG_BRCTL) += brctl.o
  40. //usage:#define brctl_trivial_usage
  41. //usage: "COMMAND [BRIDGE [ARGS]]"
  42. //usage:#define brctl_full_usage "\n\n"
  43. //usage: "Manage ethernet bridges"
  44. //usage: "\nCommands:"
  45. //usage: IF_FEATURE_BRCTL_SHOW(
  46. //usage: "\n show [BRIDGE]... Show bridges"
  47. //usage: )
  48. //usage: "\n addbr BRIDGE Create BRIDGE"
  49. //usage: "\n delbr BRIDGE Delete BRIDGE"
  50. //usage: "\n addif BRIDGE IFACE Add IFACE to BRIDGE"
  51. //usage: "\n delif BRIDGE IFACE Delete IFACE from BRIDGE"
  52. //usage: IF_FEATURE_BRCTL_FANCY(
  53. //usage: "\n stp BRIDGE 1/yes/on|0/no/off STP on/off"
  54. //usage: "\n setageing BRIDGE SECONDS Set ageing time"
  55. //usage: "\n setfd BRIDGE SECONDS Set bridge forward delay"
  56. //usage: "\n sethello BRIDGE SECONDS Set hello time"
  57. //usage: "\n setmaxage BRIDGE SECONDS Set max message age"
  58. //usage: "\n setbridgeprio BRIDGE PRIO Set bridge priority"
  59. //usage: "\n setportprio BRIDGE IFACE PRIO Set port priority"
  60. //usage: "\n setpathcost BRIDGE IFACE COST Set path cost"
  61. //usage: )
  62. // Not yet implemented:
  63. // hairpin BRIDGE IFACE on|off Hairpin on/off
  64. // showmacs BRIDGE List mac addrs
  65. // showstp BRIDGE Show stp info
  66. #include "libbb.h"
  67. #include "common_bufsiz.h"
  68. #include <linux/sockios.h>
  69. #include <net/if.h>
  70. #ifndef SIOCBRADDBR
  71. # define SIOCBRADDBR BRCTL_ADD_BRIDGE
  72. #endif
  73. #ifndef SIOCBRDELBR
  74. # define SIOCBRDELBR BRCTL_DEL_BRIDGE
  75. #endif
  76. #ifndef SIOCBRADDIF
  77. # define SIOCBRADDIF BRCTL_ADD_IF
  78. #endif
  79. #ifndef SIOCBRDELIF
  80. # define SIOCBRDELIF BRCTL_DEL_IF
  81. #endif
  82. #if ENABLE_FEATURE_BRCTL_FANCY
  83. static unsigned str_to_jiffies(const char *time_str)
  84. {
  85. double dd;
  86. char *endptr;
  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
  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. /* NB: we are in /sys/class/net
  114. */
  115. static int show_bridge(const char *name, int need_hdr)
  116. {
  117. /* Output:
  118. *bridge name bridge id STP enabled interfaces
  119. *br0 8000.000000000000 no eth0
  120. */
  121. char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 32];
  122. int tabs;
  123. DIR *ifaces;
  124. struct dirent *ent;
  125. char *sfx;
  126. #if IFNAMSIZ == 16
  127. sfx = pathbuf + sprintf(pathbuf, "%.16s/bridge/", name);
  128. #else
  129. sfx = pathbuf + sprintf(pathbuf, "%.*s/bridge/", (int)IFNAMSIZ, name);
  130. #endif
  131. strcpy(sfx, "bridge_id");
  132. if (read_file(pathbuf) < 0)
  133. return -1; /* this iface is not a bridge */
  134. if (need_hdr)
  135. puts("bridge name\tbridge id\t\tSTP enabled\tinterfaces");
  136. printf("%s\t\t", name);
  137. printf("%s\t", 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. strcpy(sfx - (sizeof("bridge/")-1), "brif");
  147. tabs = 0;
  148. ifaces = opendir(pathbuf);
  149. if (ifaces) {
  150. while ((ent = readdir(ifaces)) != NULL) {
  151. if (DOT_OR_DOTDOT(ent->d_name))
  152. continue; /* . or .. */
  153. if (tabs)
  154. printf("\t\t\t\t\t");
  155. else
  156. tabs = 1;
  157. printf("\t\t%s\n", ent->d_name);
  158. }
  159. closedir(ifaces);
  160. }
  161. if (!tabs) /* bridge has no interfaces */
  162. bb_putchar('\n');
  163. return 0;
  164. }
  165. #endif
  166. #if ENABLE_FEATURE_BRCTL_FANCY
  167. static void write_uint(const char *name, const char *leaf, unsigned val)
  168. {
  169. char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 32];
  170. int fd, n;
  171. #if IFNAMSIZ == 16
  172. sprintf(pathbuf, "%.16s/%s", name, leaf);
  173. #else
  174. sprintf(pathbuf, "%.*s/%s", (int)IFNAMSIZ, name, leaf);
  175. #endif
  176. fd = xopen(pathbuf, O_WRONLY);
  177. n = sprintf(filedata, "%u\n", val);
  178. if (write(fd, filedata, n) < 0)
  179. bb_simple_perror_msg_and_die(name);
  180. close(fd);
  181. }
  182. #endif
  183. int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  184. int brctl_main(int argc UNUSED_PARAM, char **argv)
  185. {
  186. static const char keywords[] ALIGN1 =
  187. "addbr\0" "delbr\0" "addif\0" "delif\0"
  188. IF_FEATURE_BRCTL_FANCY(
  189. "stp\0"
  190. "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
  191. "setpathcost\0" "setportprio\0"
  192. "setbridgeprio\0"
  193. )
  194. IF_FEATURE_BRCTL_SHOW("show\0");
  195. enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
  196. IF_FEATURE_BRCTL_FANCY(,
  197. ARG_stp,
  198. ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
  199. ARG_setpathcost, ARG_setportprio,
  200. ARG_setbridgeprio
  201. )
  202. IF_FEATURE_BRCTL_SHOW(, ARG_show)
  203. };
  204. argv++;
  205. if (!*argv) {
  206. /* bare "brctl" shows --help */
  207. bb_show_usage();
  208. }
  209. xchdir("/sys/class/net");
  210. // while (*argv)
  211. {
  212. smallint key;
  213. char *br;
  214. key = index_in_strings(keywords, *argv);
  215. if (key == -1) /* no match found in keywords array, bail out. */
  216. bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
  217. argv++;
  218. #if ENABLE_FEATURE_BRCTL_SHOW
  219. if (key == ARG_show) { /* show [BR]... */
  220. DIR *net;
  221. struct dirent *ent;
  222. int need_hdr = 1;
  223. int exitcode = EXIT_SUCCESS;
  224. if (*argv) {
  225. /* "show BR1 BR2 BR3" */
  226. do {
  227. if (show_bridge(*argv, need_hdr) >= 0) {
  228. need_hdr = 0;
  229. } else {
  230. bb_error_msg("bridge %s does not exist", *argv);
  231. //TODO: if device exists, but is not a BR, brctl from bridge-utils 1.6
  232. //says this instead: "device eth0 is not a bridge"
  233. exitcode = EXIT_FAILURE;
  234. }
  235. } while (*++argv != NULL);
  236. return exitcode;
  237. }
  238. /* "show" (if no ifaces, shows nothing, not even header) */
  239. net = xopendir(".");
  240. while ((ent = readdir(net)) != NULL) {
  241. if (DOT_OR_DOTDOT(ent->d_name))
  242. continue; /* . or .. */
  243. if (show_bridge(ent->d_name, need_hdr) >= 0)
  244. need_hdr = 0;
  245. }
  246. if (ENABLE_FEATURE_CLEAN_UP)
  247. closedir(net);
  248. return exitcode;
  249. }
  250. #endif
  251. if (!*argv) /* all but 'show' need at least one argument */
  252. bb_show_usage();
  253. br = *argv++;
  254. if (key == ARG_addbr || key == ARG_delbr) {
  255. /* addbr or delbr */
  256. /* brctl from bridge-utils 1.6 still uses ioctl
  257. * for SIOCBRADDBR / SIOCBRDELBR, not /sys accesses
  258. */
  259. int fd = xsocket(AF_INET, SOCK_STREAM, 0);
  260. ioctl_or_perror_and_die(fd,
  261. key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
  262. br, "bridge %s", br
  263. );
  264. //close(fd);
  265. //goto done;
  266. /* bridge-utils 1.6 simply ignores trailing args:
  267. * "brctl addbr BR1 ARGS" ignores ARGS
  268. */
  269. if (ENABLE_FEATURE_CLEAN_UP)
  270. close(fd);
  271. return EXIT_SUCCESS;
  272. }
  273. if (!*argv) /* all but 'addbr/delbr' need at least two arguments */
  274. bb_show_usage();
  275. #if ENABLE_FEATURE_BRCTL_FANCY
  276. if (key == ARG_stp) { /* stp */
  277. static const char no_yes[] ALIGN1 =
  278. "0\0" "off\0" "n\0" "no\0" /* 0 .. 3 */
  279. "1\0" "on\0" "y\0" "yes\0"; /* 4 .. 7 */
  280. int onoff = index_in_strings(no_yes, *argv);
  281. if (onoff < 0)
  282. bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
  283. onoff = (unsigned)onoff / 4;
  284. write_uint(br, "bridge/stp_state", onoff);
  285. //goto done_next_argv;
  286. return EXIT_SUCCESS;
  287. }
  288. if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
  289. /* setageing BR N: "N*100\n" to /sys/class/net/BR/bridge/ageing_time
  290. * setfd BR N: "N*100\n" to /sys/class/net/BR/bridge/forward_delay
  291. * sethello BR N: "N*100\n" to /sys/class/net/BR/bridge/hello_time
  292. * setmaxage BR N: "N*100\n" to /sys/class/net/BR/bridge/max_age
  293. */
  294. write_uint(br,
  295. nth_string(
  296. "bridge/ageing_time" "\0" /* ARG_setageing */
  297. "bridge/forward_delay""\0" /* ARG_setfd */
  298. "bridge/hello_time" "\0" /* ARG_sethello */
  299. "bridge/max_age", /* ARG_setmaxage */
  300. key - ARG_setageing
  301. ),
  302. str_to_jiffies(*argv)
  303. );
  304. //goto done_next_argv;
  305. return EXIT_SUCCESS;
  306. }
  307. if (key == ARG_setbridgeprio) {
  308. write_uint(br, "bridge/priority", xatoi_positive(*argv));
  309. //goto done_next_argv;
  310. return EXIT_SUCCESS;
  311. }
  312. if (key == ARG_setpathcost
  313. || key == ARG_setportprio
  314. ) {
  315. if (!argv[1])
  316. bb_show_usage();
  317. /* BR is not used (and ignored!) for these commands:
  318. * "setpathcost BR PORT N" writes "N\n" to
  319. * /sys/class/net/PORT/brport/path_cost
  320. * "setportprio BR PORT N" writes "N\n" to
  321. * /sys/class/net/PORT/brport/priority
  322. */
  323. write_uint(argv[0],
  324. nth_string(
  325. "brport/path_cost" "\0" /* ARG_setpathcost */
  326. "brport/priority", /* ARG_setportprio */
  327. key - ARG_setpathcost
  328. ),
  329. xatoi_positive(argv[1])
  330. );
  331. //argv++;
  332. //goto done_next_argv;
  333. return EXIT_SUCCESS;
  334. }
  335. /* TODO: "showmacs BR"
  336. * port no\tmac addr\t\tis local?\tageing timer
  337. * <sp><sp>1\txx:xx:xx:xx:xx:xx\tno\t\t<sp><sp><sp>1.31
  338. * port no mac addr is local? ageing timer
  339. * 1 xx:xx:xx:xx:xx:xx no 1.31
  340. * Read fixed-sized records from /sys/class/net/BR/brforward:
  341. * struct __fdb_entry {
  342. * uint8_t mac_addr[ETH_ALEN];
  343. * uint8_t port_no; //lsb
  344. * uint8_t is_local;
  345. * uint32_t ageing_timer_value;
  346. * uint8_t port_hi;
  347. * uint8_t pad0;
  348. * uint16_t unused;
  349. * };
  350. */
  351. #endif
  352. /* always true: if (key == ARG_addif || key == ARG_delif) */ {
  353. /* addif or delif */
  354. struct ifreq ifr;
  355. int fd = xsocket(AF_INET, SOCK_STREAM, 0);
  356. strncpy_IFNAMSIZ(ifr.ifr_name, br);
  357. ifr.ifr_ifindex = if_nametoindex(*argv);
  358. if (ifr.ifr_ifindex == 0) {
  359. bb_perror_msg_and_die("iface %s", *argv);
  360. }
  361. ioctl_or_perror_and_die(fd,
  362. key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
  363. &ifr, "bridge %s", br
  364. );
  365. //close(fd);
  366. //goto done_next_argv;
  367. if (ENABLE_FEATURE_CLEAN_UP)
  368. close(fd);
  369. return EXIT_SUCCESS;
  370. }
  371. // done_next_argv:
  372. // argv++;
  373. // done:
  374. }
  375. return EXIT_SUCCESS;
  376. }