brctl.c 7.3 KB

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