brctl.c 7.0 KB

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