3
0

brctl.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /* This applet currently uses only the ioctl interface and no sysfs at all.
  13. * At the time of this writing this was considered a feature.
  14. */
  15. //usage:#define brctl_trivial_usage
  16. //usage: "COMMAND [BRIDGE [INTERFACE]]"
  17. //usage:#define brctl_full_usage "\n\n"
  18. //usage: "Manage ethernet bridges\n"
  19. //usage: "\nCommands:"
  20. //usage: IF_FEATURE_BRCTL_SHOW(
  21. //usage: "\n show Show a list of bridges"
  22. //usage: )
  23. //usage: "\n addbr BRIDGE Create BRIDGE"
  24. //usage: "\n delbr BRIDGE Delete BRIDGE"
  25. //usage: "\n addif BRIDGE IFACE Add IFACE to BRIDGE"
  26. //usage: "\n delif BRIDGE IFACE Delete IFACE from BRIDGE"
  27. //usage: IF_FEATURE_BRCTL_FANCY(
  28. //usage: "\n setageing BRIDGE TIME Set ageing time"
  29. //usage: "\n setfd BRIDGE TIME Set bridge forward delay"
  30. //usage: "\n sethello BRIDGE TIME Set hello time"
  31. //usage: "\n setmaxage BRIDGE TIME Set max message age"
  32. //usage: "\n setpathcost BRIDGE COST Set path cost"
  33. //usage: "\n setportprio BRIDGE PRIO Set port priority"
  34. //usage: "\n setbridgeprio BRIDGE PRIO Set bridge priority"
  35. //usage: "\n stp BRIDGE [1/yes/on|0/no/off] STP on/off"
  36. //usage: )
  37. #include "libbb.h"
  38. #include <linux/sockios.h>
  39. #include <net/if.h>
  40. #ifndef SIOCBRADDBR
  41. # define SIOCBRADDBR BRCTL_ADD_BRIDGE
  42. #endif
  43. #ifndef SIOCBRDELBR
  44. # define SIOCBRDELBR BRCTL_DEL_BRIDGE
  45. #endif
  46. #ifndef SIOCBRADDIF
  47. # define SIOCBRADDIF BRCTL_ADD_IF
  48. #endif
  49. #ifndef SIOCBRDELIF
  50. # define SIOCBRDELIF BRCTL_DEL_IF
  51. #endif
  52. /* Maximum number of ports supported per bridge interface. */
  53. #ifndef MAX_PORTS
  54. # define MAX_PORTS 32
  55. #endif
  56. /* Use internal number parsing and not the "exact" conversion. */
  57. /* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */
  58. #define BRCTL_USE_INTERNAL 1
  59. #if ENABLE_FEATURE_BRCTL_FANCY
  60. # include <linux/if_bridge.h>
  61. /* FIXME: These 4 funcs are not really clean and could be improved */
  62. static ALWAYS_INLINE void bb_strtotimeval(struct timeval *tv,
  63. const char *time_str)
  64. {
  65. double secs;
  66. # if BRCTL_USE_INTERNAL
  67. char *endptr;
  68. secs = /*bb_*/strtod(time_str, &endptr);
  69. if (endptr == time_str)
  70. # else
  71. if (sscanf(time_str, "%lf", &secs) != 1)
  72. # endif
  73. bb_error_msg_and_die(bb_msg_invalid_arg, time_str, "timespec");
  74. tv->tv_sec = secs;
  75. tv->tv_usec = 1000000 * (secs - tv->tv_sec);
  76. }
  77. static ALWAYS_INLINE unsigned long tv_to_jiffies(const struct timeval *tv)
  78. {
  79. unsigned long long jif;
  80. jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
  81. return jif/10000;
  82. }
  83. # if 0
  84. static void jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
  85. {
  86. unsigned long long tvusec;
  87. tvusec = 10000ULL*jiffies;
  88. tv->tv_sec = tvusec/1000000;
  89. tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
  90. }
  91. # endif
  92. static unsigned long str_to_jiffies(const char *time_str)
  93. {
  94. struct timeval tv;
  95. bb_strtotimeval(&tv, time_str);
  96. return tv_to_jiffies(&tv);
  97. }
  98. static void arm_ioctl(unsigned long *args,
  99. unsigned long arg0, unsigned long arg1, unsigned long arg2)
  100. {
  101. args[0] = arg0;
  102. args[1] = arg1;
  103. args[2] = arg2;
  104. args[3] = 0;
  105. }
  106. #endif
  107. int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  108. int brctl_main(int argc UNUSED_PARAM, char **argv)
  109. {
  110. static const char keywords[] ALIGN1 =
  111. "addbr\0" "delbr\0" "addif\0" "delif\0"
  112. IF_FEATURE_BRCTL_FANCY(
  113. "stp\0"
  114. "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
  115. "setpathcost\0" "setportprio\0" "setbridgeprio\0"
  116. )
  117. IF_FEATURE_BRCTL_SHOW("show\0");
  118. enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
  119. IF_FEATURE_BRCTL_FANCY(,
  120. ARG_stp,
  121. ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
  122. ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio
  123. )
  124. IF_FEATURE_BRCTL_SHOW(, ARG_show)
  125. };
  126. int fd;
  127. smallint key;
  128. struct ifreq ifr;
  129. char *br, *brif;
  130. argv++;
  131. while (*argv) {
  132. #if ENABLE_FEATURE_BRCTL_FANCY
  133. int ifidx[MAX_PORTS];
  134. unsigned long args[4];
  135. ifr.ifr_data = (char *) &args;
  136. #endif
  137. key = index_in_strings(keywords, *argv);
  138. if (key == -1) /* no match found in keywords array, bail out. */
  139. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  140. argv++;
  141. fd = xsocket(AF_INET, SOCK_STREAM, 0);
  142. #if ENABLE_FEATURE_BRCTL_SHOW
  143. if (key == ARG_show) { /* show */
  144. char brname[IFNAMSIZ];
  145. int bridx[MAX_PORTS];
  146. int i, num;
  147. arm_ioctl(args, BRCTL_GET_BRIDGES,
  148. (unsigned long) bridx, MAX_PORTS);
  149. num = xioctl(fd, SIOCGIFBR, args);
  150. printf("bridge name\tbridge id\t\tSTP enabled\tinterfaces\n");
  151. for (i = 0; i < num; i++) {
  152. char ifname[IFNAMSIZ];
  153. int j, tabs;
  154. struct __bridge_info bi;
  155. unsigned char *x;
  156. if (!if_indextoname(bridx[i], brname))
  157. bb_perror_msg_and_die("can't get bridge name for index %d", i);
  158. strncpy_IFNAMSIZ(ifr.ifr_name, brname);
  159. arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
  160. (unsigned long) &bi, 0);
  161. xioctl(fd, SIOCDEVPRIVATE, &ifr);
  162. printf("%s\t\t", brname);
  163. /* print bridge id */
  164. x = (unsigned char *) &bi.bridge_id;
  165. for (j = 0; j < 8; j++) {
  166. printf("%.2x", x[j]);
  167. if (j == 1)
  168. bb_putchar('.');
  169. }
  170. printf(bi.stp_enabled ? "\tyes" : "\tno");
  171. /* print interface list */
  172. arm_ioctl(args, BRCTL_GET_PORT_LIST,
  173. (unsigned long) ifidx, MAX_PORTS);
  174. xioctl(fd, SIOCDEVPRIVATE, &ifr);
  175. tabs = 0;
  176. for (j = 0; j < MAX_PORTS; j++) {
  177. if (!ifidx[j])
  178. continue;
  179. if (!if_indextoname(ifidx[j], ifname))
  180. bb_perror_msg_and_die("can't get interface name for index %d", j);
  181. if (tabs)
  182. printf("\t\t\t\t\t");
  183. else
  184. tabs = 1;
  185. printf("\t\t%s\n", ifname);
  186. }
  187. if (!tabs) /* bridge has no interfaces */
  188. bb_putchar('\n');
  189. }
  190. goto done;
  191. }
  192. #endif
  193. if (!*argv) /* all but 'show' need at least one argument */
  194. bb_show_usage();
  195. br = *argv++;
  196. if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */
  197. ioctl_or_perror_and_die(fd,
  198. key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
  199. br, "bridge %s", br);
  200. goto done;
  201. }
  202. if (!*argv) /* all but 'addbr/delbr' need at least two arguments */
  203. bb_show_usage();
  204. strncpy_IFNAMSIZ(ifr.ifr_name, br);
  205. if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
  206. brif = *argv;
  207. ifr.ifr_ifindex = if_nametoindex(brif);
  208. if (!ifr.ifr_ifindex) {
  209. bb_perror_msg_and_die("iface %s", brif);
  210. }
  211. ioctl_or_perror_and_die(fd,
  212. key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
  213. &ifr, "bridge %s", br);
  214. goto done_next_argv;
  215. }
  216. #if ENABLE_FEATURE_BRCTL_FANCY
  217. if (key == ARG_stp) { /* stp */
  218. static const char no_yes[] ALIGN1 =
  219. "0\0" "off\0" "n\0" "no\0" /* 0 .. 3 */
  220. "1\0" "on\0" "y\0" "yes\0"; /* 4 .. 7 */
  221. int onoff = index_in_strings(no_yes, *argv);
  222. if (onoff < 0)
  223. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  224. onoff = (unsigned)onoff / 4;
  225. arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE, onoff, 0);
  226. goto fire;
  227. }
  228. if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
  229. static const uint8_t ops[] ALIGN1 = {
  230. BRCTL_SET_AGEING_TIME, /* ARG_setageing */
  231. BRCTL_SET_BRIDGE_FORWARD_DELAY, /* ARG_setfd */
  232. BRCTL_SET_BRIDGE_HELLO_TIME, /* ARG_sethello */
  233. BRCTL_SET_BRIDGE_MAX_AGE /* ARG_setmaxage */
  234. };
  235. arm_ioctl(args, ops[key - ARG_setageing], str_to_jiffies(*argv), 0);
  236. goto fire;
  237. }
  238. if (key == ARG_setpathcost
  239. || key == ARG_setportprio
  240. || key == ARG_setbridgeprio
  241. ) {
  242. static const uint8_t ops[] ALIGN1 = {
  243. BRCTL_SET_PATH_COST, /* ARG_setpathcost */
  244. BRCTL_SET_PORT_PRIORITY, /* ARG_setportprio */
  245. BRCTL_SET_BRIDGE_PRIORITY /* ARG_setbridgeprio */
  246. };
  247. int port = -1;
  248. unsigned arg1, arg2;
  249. if (key != ARG_setbridgeprio) {
  250. /* get portnum */
  251. unsigned i;
  252. port = if_nametoindex(*argv++);
  253. if (!port)
  254. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, "port");
  255. memset(ifidx, 0, sizeof ifidx);
  256. arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
  257. MAX_PORTS);
  258. xioctl(fd, SIOCDEVPRIVATE, &ifr);
  259. for (i = 0; i < MAX_PORTS; i++) {
  260. if (ifidx[i] == port) {
  261. port = i;
  262. break;
  263. }
  264. }
  265. }
  266. arg1 = port;
  267. arg2 = xatoi_positive(*argv);
  268. if (key == ARG_setbridgeprio) {
  269. arg1 = arg2;
  270. arg2 = 0;
  271. }
  272. arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2);
  273. }
  274. fire:
  275. /* Execute the previously set command */
  276. xioctl(fd, SIOCDEVPRIVATE, &ifr);
  277. #endif
  278. done_next_argv:
  279. argv++;
  280. done:
  281. close(fd);
  282. }
  283. return EXIT_SUCCESS;
  284. }