3
0

brctl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. * breaks on musl: we already included netinet/in.h in libbb.h,
  62. * if we include <linux/if_bridge.h> here, we get this:
  63. * In file included from /usr/include/linux/if_bridge.h:18,
  64. * from networking/brctl.c:67:
  65. * /usr/include/linux/in6.h:32: error: redefinition of 'struct in6_addr'
  66. * /usr/include/linux/in6.h:49: error: redefinition of 'struct sockaddr_in6'
  67. * /usr/include/linux/in6.h:59: error: redefinition of 'struct ipv6_mreq'
  68. */
  69. /* From <linux/if_bridge.h> */
  70. #define BRCTL_GET_VERSION 0
  71. #define BRCTL_GET_BRIDGES 1
  72. #define BRCTL_ADD_BRIDGE 2
  73. #define BRCTL_DEL_BRIDGE 3
  74. #define BRCTL_ADD_IF 4
  75. #define BRCTL_DEL_IF 5
  76. #define BRCTL_GET_BRIDGE_INFO 6
  77. #define BRCTL_GET_PORT_LIST 7
  78. #define BRCTL_SET_BRIDGE_FORWARD_DELAY 8
  79. #define BRCTL_SET_BRIDGE_HELLO_TIME 9
  80. #define BRCTL_SET_BRIDGE_MAX_AGE 10
  81. #define BRCTL_SET_AGEING_TIME 11
  82. #define BRCTL_SET_GC_INTERVAL 12
  83. #define BRCTL_GET_PORT_INFO 13
  84. #define BRCTL_SET_BRIDGE_STP_STATE 14
  85. #define BRCTL_SET_BRIDGE_PRIORITY 15
  86. #define BRCTL_SET_PORT_PRIORITY 16
  87. #define BRCTL_SET_PATH_COST 17
  88. #define BRCTL_GET_FDB_ENTRIES 18
  89. struct __bridge_info {
  90. uint64_t designated_root;
  91. uint64_t bridge_id;
  92. uint32_t root_path_cost;
  93. uint32_t max_age;
  94. uint32_t hello_time;
  95. uint32_t forward_delay;
  96. uint32_t bridge_max_age;
  97. uint32_t bridge_hello_time;
  98. uint32_t bridge_forward_delay;
  99. uint8_t topology_change;
  100. uint8_t topology_change_detected;
  101. uint8_t root_port;
  102. uint8_t stp_enabled;
  103. uint32_t ageing_time;
  104. uint32_t gc_interval;
  105. uint32_t hello_timer_value;
  106. uint32_t tcn_timer_value;
  107. uint32_t topology_change_timer_value;
  108. uint32_t gc_timer_value;
  109. };
  110. /* end <linux/if_bridge.h> */
  111. /* FIXME: These 4 funcs are not really clean and could be improved */
  112. static ALWAYS_INLINE void bb_strtotimeval(struct timeval *tv,
  113. const char *time_str)
  114. {
  115. double secs;
  116. # if BRCTL_USE_INTERNAL
  117. char *endptr;
  118. secs = /*bb_*/strtod(time_str, &endptr);
  119. if (endptr == time_str)
  120. # else
  121. if (sscanf(time_str, "%lf", &secs) != 1)
  122. # endif
  123. bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec");
  124. tv->tv_sec = secs;
  125. tv->tv_usec = 1000000 * (secs - tv->tv_sec);
  126. }
  127. static ALWAYS_INLINE unsigned long tv_to_jiffies(const struct timeval *tv)
  128. {
  129. unsigned long long jif;
  130. jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
  131. return jif/10000;
  132. }
  133. # if 0
  134. static void jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
  135. {
  136. unsigned long long tvusec;
  137. tvusec = 10000ULL*jiffies;
  138. tv->tv_sec = tvusec/1000000;
  139. tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
  140. }
  141. # endif
  142. static unsigned long str_to_jiffies(const char *time_str)
  143. {
  144. struct timeval tv;
  145. bb_strtotimeval(&tv, time_str);
  146. return tv_to_jiffies(&tv);
  147. }
  148. static void arm_ioctl(unsigned long *args,
  149. unsigned long arg0, unsigned long arg1, unsigned long arg2)
  150. {
  151. args[0] = arg0;
  152. args[1] = arg1;
  153. args[2] = arg2;
  154. args[3] = 0;
  155. }
  156. #endif
  157. int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  158. int brctl_main(int argc UNUSED_PARAM, char **argv)
  159. {
  160. static const char keywords[] ALIGN1 =
  161. "addbr\0" "delbr\0" "addif\0" "delif\0"
  162. IF_FEATURE_BRCTL_FANCY(
  163. "stp\0"
  164. "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
  165. "setpathcost\0" "setportprio\0" "setbridgeprio\0"
  166. )
  167. IF_FEATURE_BRCTL_SHOW("show\0");
  168. enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
  169. IF_FEATURE_BRCTL_FANCY(,
  170. ARG_stp,
  171. ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
  172. ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio
  173. )
  174. IF_FEATURE_BRCTL_SHOW(, ARG_show)
  175. };
  176. int fd;
  177. smallint key;
  178. struct ifreq ifr;
  179. char *br, *brif;
  180. argv++;
  181. while (*argv) {
  182. #if ENABLE_FEATURE_BRCTL_FANCY
  183. int ifidx[MAX_PORTS];
  184. unsigned long args[4];
  185. ifr.ifr_data = (char *) &args;
  186. #endif
  187. key = index_in_strings(keywords, *argv);
  188. if (key == -1) /* no match found in keywords array, bail out. */
  189. bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
  190. argv++;
  191. fd = xsocket(AF_INET, SOCK_STREAM, 0);
  192. #if ENABLE_FEATURE_BRCTL_SHOW
  193. if (key == ARG_show) { /* show */
  194. char brname[IFNAMSIZ];
  195. int bridx[MAX_PORTS];
  196. int i, num;
  197. arm_ioctl(args, BRCTL_GET_BRIDGES,
  198. (unsigned long) bridx, MAX_PORTS);
  199. num = xioctl(fd, SIOCGIFBR, args);
  200. puts("bridge name\tbridge id\t\tSTP enabled\tinterfaces");
  201. for (i = 0; i < num; i++) {
  202. char ifname[IFNAMSIZ];
  203. int j, tabs;
  204. struct __bridge_info bi;
  205. unsigned char *x;
  206. if (!if_indextoname(bridx[i], brname))
  207. bb_perror_msg_and_die("can't get bridge name for index %d", i);
  208. strncpy_IFNAMSIZ(ifr.ifr_name, brname);
  209. arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
  210. (unsigned long) &bi, 0);
  211. xioctl(fd, SIOCDEVPRIVATE, &ifr);
  212. printf("%s\t\t", brname);
  213. /* print bridge id */
  214. x = (unsigned char *) &bi.bridge_id;
  215. for (j = 0; j < 8; j++) {
  216. printf("%02x", x[j]);
  217. if (j == 1)
  218. bb_putchar('.');
  219. }
  220. printf(bi.stp_enabled ? "\tyes" : "\tno");
  221. /* print interface list */
  222. arm_ioctl(args, BRCTL_GET_PORT_LIST,
  223. (unsigned long) ifidx, MAX_PORTS);
  224. xioctl(fd, SIOCDEVPRIVATE, &ifr);
  225. tabs = 0;
  226. for (j = 0; j < MAX_PORTS; j++) {
  227. if (!ifidx[j])
  228. continue;
  229. if (!if_indextoname(ifidx[j], ifname))
  230. bb_perror_msg_and_die("can't get interface name for index %d", j);
  231. if (tabs)
  232. printf("\t\t\t\t\t");
  233. else
  234. tabs = 1;
  235. printf("\t\t%s\n", ifname);
  236. }
  237. if (!tabs) /* bridge has no interfaces */
  238. bb_putchar('\n');
  239. }
  240. goto done;
  241. }
  242. #endif
  243. if (!*argv) /* all but 'show' need at least one argument */
  244. bb_show_usage();
  245. br = *argv++;
  246. if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */
  247. ioctl_or_perror_and_die(fd,
  248. key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
  249. br, "bridge %s", br);
  250. goto done;
  251. }
  252. if (!*argv) /* all but 'addbr/delbr' need at least two arguments */
  253. bb_show_usage();
  254. strncpy_IFNAMSIZ(ifr.ifr_name, br);
  255. if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
  256. brif = *argv;
  257. ifr.ifr_ifindex = if_nametoindex(brif);
  258. if (!ifr.ifr_ifindex) {
  259. bb_perror_msg_and_die("iface %s", brif);
  260. }
  261. ioctl_or_perror_and_die(fd,
  262. key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
  263. &ifr, "bridge %s", br);
  264. goto done_next_argv;
  265. }
  266. #if ENABLE_FEATURE_BRCTL_FANCY
  267. if (key == ARG_stp) { /* stp */
  268. static const char no_yes[] ALIGN1 =
  269. "0\0" "off\0" "n\0" "no\0" /* 0 .. 3 */
  270. "1\0" "on\0" "y\0" "yes\0"; /* 4 .. 7 */
  271. int onoff = index_in_strings(no_yes, *argv);
  272. if (onoff < 0)
  273. bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
  274. onoff = (unsigned)onoff / 4;
  275. arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE, onoff, 0);
  276. goto fire;
  277. }
  278. if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
  279. static const uint8_t ops[] ALIGN1 = {
  280. BRCTL_SET_AGEING_TIME, /* ARG_setageing */
  281. BRCTL_SET_BRIDGE_FORWARD_DELAY, /* ARG_setfd */
  282. BRCTL_SET_BRIDGE_HELLO_TIME, /* ARG_sethello */
  283. BRCTL_SET_BRIDGE_MAX_AGE /* ARG_setmaxage */
  284. };
  285. arm_ioctl(args, ops[key - ARG_setageing], str_to_jiffies(*argv), 0);
  286. goto fire;
  287. }
  288. if (key == ARG_setpathcost
  289. || key == ARG_setportprio
  290. || key == ARG_setbridgeprio
  291. ) {
  292. static const uint8_t ops[] ALIGN1 = {
  293. BRCTL_SET_PATH_COST, /* ARG_setpathcost */
  294. BRCTL_SET_PORT_PRIORITY, /* ARG_setportprio */
  295. BRCTL_SET_BRIDGE_PRIORITY /* ARG_setbridgeprio */
  296. };
  297. int port = -1;
  298. unsigned arg1, arg2;
  299. if (key != ARG_setbridgeprio) {
  300. /* get portnum */
  301. unsigned i;
  302. port = if_nametoindex(*argv++);
  303. if (!port)
  304. bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, "port");
  305. memset(ifidx, 0, sizeof ifidx);
  306. arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
  307. MAX_PORTS);
  308. xioctl(fd, SIOCDEVPRIVATE, &ifr);
  309. for (i = 0; i < MAX_PORTS; i++) {
  310. if (ifidx[i] == port) {
  311. port = i;
  312. break;
  313. }
  314. }
  315. }
  316. arg1 = port;
  317. arg2 = xatoi_positive(*argv);
  318. if (key == ARG_setbridgeprio) {
  319. arg1 = arg2;
  320. arg2 = 0;
  321. }
  322. arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2);
  323. }
  324. fire:
  325. /* Execute the previously set command */
  326. xioctl(fd, SIOCDEVPRIVATE, &ifr);
  327. #endif
  328. done_next_argv:
  329. argv++;
  330. done:
  331. close(fd);
  332. }
  333. return EXIT_SUCCESS;
  334. }