blob: 847517961b930c731dcfefbe169eb9aa34d2086a [file] [log] [blame]
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Small implementation of brctl for busybox.
4 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00005 * Copyright (C) 2008 by Bernhard Reutner-Fischer
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +00006 *
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +00007 * Some helper functions from bridge-utils are
8 * Copyright (C) 2000 Lennert Buytenhek
9 *
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 */
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000012/* 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 */
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000015#include "libbb.h"
16#include <linux/sockios.h>
17#include <net/if.h>
18
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000019/* Maximum number of ports supported per bridge interface. */
20#ifndef MAX_PORTS
21#define MAX_PORTS 32
22#endif
23
24/* Use internal number parsing and not the "exact" conversion. */
25/* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */
26#define BRCTL_USE_INTERNAL 1
27
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000028#if ENABLE_FEATURE_BRCTL_FANCY
29#include <linux/if_bridge.h>
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000030
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000031/* FIXME: These 4 funcs are not really clean and could be improved */
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000032static ALWAYS_INLINE void strtotimeval(struct timeval *tv,
33 const char *time_str)
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000034{
35 double secs;
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000036#if BRCTL_USE_INTERNAL
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000037 secs = /*bb_*/strtod(time_str, NULL);
38 if (!secs)
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000039#else
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000040 if (sscanf(time_str, "%lf", &secs) != 1)
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000041#endif
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000042 bb_error_msg_and_die (bb_msg_invalid_arg, time_str, "timespec");
43 tv->tv_sec = secs;
44 tv->tv_usec = 1000000 * (secs - tv->tv_sec);
45}
46
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000047static ALWAYS_INLINE unsigned long __tv_to_jiffies(const struct timeval *tv)
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000048{
49 unsigned long long jif;
50
51 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
52
53 return jif/10000;
54}
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000055# if 0
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000056static void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
57{
58 unsigned long long tvusec;
59
60 tvusec = 10000ULL*jiffies;
61 tv->tv_sec = tvusec/1000000;
62 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
63}
64# endif
65static unsigned long str_to_jiffies(const char *time_str)
66{
67 struct timeval tv;
68 strtotimeval(&tv, time_str);
69 return __tv_to_jiffies(&tv);
70}
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000071
72static void arm_ioctl(unsigned long *args,
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000073 unsigned long arg0, unsigned long arg1, unsigned long arg2)
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000074{
75 args[0] = arg0;
76 args[1] = arg1;
77 args[2] = arg2;
78 args[3] = 0;
79}
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000080#endif
81
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000082
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000083int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000084int brctl_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000085{
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +000086 static const char keywords[] ALIGN1 =
87 "addbr\0" "delbr\0" "addif\0" "delif\0"
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000088 USE_FEATURE_BRCTL_FANCY(
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000089 "stp\0"
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000090 "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
91 "setpathcost\0" "setportprio\0" "setbridgeprio\0"
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000092 )
Denis Vlasenko278a1c22008-04-06 07:17:02 +000093 USE_FEATURE_BRCTL_SHOW("showmacs\0" "show\0");
Denis Vlasenkod0a071a2008-03-17 09:33:45 +000094
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000095 enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
96 USE_FEATURE_BRCTL_FANCY(,
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000097 ARG_stp,
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +000098 ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +000099 ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000100 )
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000101 USE_FEATURE_BRCTL_SHOW(, ARG_showmacs, ARG_show)
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000102 };
103
104 int fd;
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000105 smallint key;
106 struct ifreq ifr;
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000107 char *br, *brif;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000108
109 argv++;
110 while (*argv) {
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000111#if ENABLE_FEATURE_BRCTL_FANCY
112 int ifidx[MAX_PORTS];
113 unsigned long args[4];
114 ifr.ifr_data = (char *) &args;
115#endif
116
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000117 key = index_in_strings(keywords, *argv);
118 if (key == -1) /* no match found in keywords array, bail out. */
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000119 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
120 argv++;
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000121 fd = xsocket(AF_INET, SOCK_STREAM, 0);
122
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000123#if ENABLE_FEATURE_BRCTL_SHOW
124 if (key == ARG_show) { /* show */
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000125 char brname[IFNAMSIZ];
126 int bridx[MAX_PORTS];
127 int i, num;
128 arm_ioctl(args, BRCTL_GET_BRIDGES,
129 (unsigned long) bridx, MAX_PORTS);
130 num = xioctl(fd, SIOCGIFBR, args);
131 printf("bridge name\tbridge id\t\tSTP enabled\tinterfaces\n");
132 for (i = 0; i < num; i++) {
133 char ifname[IFNAMSIZ];
134 int j, tabs;
135 struct __bridge_info bi;
136 unsigned char *x;
137
138 if (!if_indextoname(bridx[i], brname))
139 bb_perror_msg_and_die("can't get bridge name for index %d", i);
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000140 strncpy(ifr.ifr_name, brname, IFNAMSIZ);
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000141
142 arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
143 (unsigned long) &bi, 0);
144 xioctl(fd, SIOCDEVPRIVATE, &ifr);
145 printf("%s\t\t", brname);
146
147 /* print bridge id */
148 x = (unsigned char *) &bi.bridge_id;
149 for (j = 0; j < 8; j++) {
150 printf("%.2x", x[j]);
151 if (j == 1)
152 bb_putchar('.');
153 }
154 printf(bi.stp_enabled ? "\tyes" : "\tno");
155
156 /* print interface list */
157 arm_ioctl(args, BRCTL_GET_PORT_LIST,
158 (unsigned long) ifidx, MAX_PORTS);
159 xioctl(fd, SIOCDEVPRIVATE, &ifr);
160 tabs = 0;
161 for (j = 0; j < MAX_PORTS; j++) {
162 if (!ifidx[j])
163 continue;
164 if (!if_indextoname(ifidx[j], ifname))
165 bb_perror_msg_and_die("can't get interface name for index %d", j);
166 if (tabs)
167 printf("\t\t\t\t\t");
168 else
169 tabs = 1;
170 printf("\t\t%s\n", ifname);
171 }
172 if (!tabs) /* bridge has no interfaces */
173 bb_putchar('\n');
174 }
175 goto done;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000176 }
177#endif
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000178
179 if (!*argv) /* all but 'show' need at least one argument */
180 bb_show_usage();
181
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000182 br = *argv++;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000183
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000184 if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */
185 ioctl_or_perror_and_die(fd,
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000186 key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR,
187 br, "bridge %s", br);
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000188 goto done;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000189 }
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000190
191 if (!*argv) /* all but 'addif/delif' need at least two arguments */
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000192 bb_show_usage();
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000193
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000194 strncpy(ifr.ifr_name, br, IFNAMSIZ);
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000195 if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000196 brif = *argv;
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000197 ifr.ifr_ifindex = if_nametoindex(brif);
198 if (!ifr.ifr_ifindex) {
199 bb_perror_msg_and_die("iface %s", brif);
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000200 }
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000201 ioctl_or_perror_and_die(fd,
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000202 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
203 &ifr, "bridge %s", br);
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000204 goto done_next_argv;
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000205 }
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000206#if ENABLE_FEATURE_BRCTL_FANCY
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000207 if (key == ARG_stp) { /* stp */
208 /* FIXME: parsing yes/y/on/1 versus no/n/off/0 is too involved */
209 arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE,
210 (unsigned)(**argv - '0'), 0);
211 goto fire;
212 }
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000213 if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
214 static const uint8_t ops[] ALIGN1 = {
215 BRCTL_SET_AGEING_TIME, /* ARG_setageing */
216 BRCTL_SET_BRIDGE_FORWARD_DELAY, /* ARG_setfd */
217 BRCTL_SET_BRIDGE_HELLO_TIME, /* ARG_sethello */
218 BRCTL_SET_BRIDGE_MAX_AGE /* ARG_setmaxage */
219 };
220 arm_ioctl(args, ops[key - ARG_setageing], str_to_jiffies(*argv), 0);
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000221 goto fire;
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000222 }
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000223 if (key == ARG_setpathcost
Denis Vlasenkod0a071a2008-03-17 09:33:45 +0000224 || key == ARG_setportprio
225 || key == ARG_setbridgeprio
226 ) {
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000227 static const uint8_t ops[] ALIGN1 = {
228 BRCTL_SET_PATH_COST, /* ARG_setpathcost */
229 BRCTL_SET_PORT_PRIORITY, /* ARG_setportprio */
230 BRCTL_SET_BRIDGE_PRIORITY /* ARG_setbridgeprio */
231 };
232 int port = -1;
233 unsigned arg1, arg2;
234
235 if (key != ARG_setbridgeprio) {
236 /* get portnum */
237 unsigned i;
238
239 port = if_nametoindex(*argv++);
240 if (!port)
241 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, "port");
242 memset(ifidx, 0, sizeof ifidx);
243 arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
244 MAX_PORTS);
245 xioctl(fd, SIOCDEVPRIVATE, &ifr);
246 for (i = 0; i < MAX_PORTS; i++) {
247 if (ifidx[i] == port) {
248 port = i;
249 break;
250 }
251 }
252 }
253 arg1 = port;
254 arg2 = xatoi_u(*argv);
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000255 if (key == ARG_setbridgeprio) {
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000256 arg1 = arg2;
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000257 arg2 = 0;
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000258 }
259 arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2);
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000260 }
261 fire:
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000262 /* Execute the previously set command */
Bernhard Reutner-Fischer2b11fb42008-01-14 16:10:11 +0000263 xioctl(fd, SIOCDEVPRIVATE, &ifr);
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000264#endif
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000265 done_next_argv:
266 argv++;
Bernhard Reutner-Fischer1aac3ab2008-01-13 18:43:50 +0000267 done:
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000268 close(fd);
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000269 }
Denis Vlasenko278a1c22008-04-06 07:17:02 +0000270
Bernhard Reutner-Fischerd27d9252008-01-13 15:23:27 +0000271 return EXIT_SUCCESS;
272}