brctl.c 7.5 KB

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