Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Small implementation of brctl for busybox. |
| 4 | * |
| 5 | * Copyright (C) 2008 by Bernhard Fischer |
| 6 | * |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 7 | * Some helper functions from bridge-utils are |
| 8 | * Copyright (C) 2000 Lennert Buytenhek |
| 9 | * |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 11 | */ |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 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 | */ |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
| 16 | #include <linux/sockios.h> |
| 17 | #include <net/if.h> |
| 18 | |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 19 | /* 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-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 28 | #if ENABLE_FEATURE_BRCTL_FANCY |
| 29 | #include <linux/if_bridge.h> |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 30 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 31 | /* FIXME: These 4 funcs are not really clean and could be improved */ |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 32 | static ALWAYS_INLINE void strtotimeval(struct timeval *tv, |
| 33 | const char *time_str) |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 34 | { |
| 35 | double secs; |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 36 | #if BRCTL_USE_INTERNAL |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 37 | secs = /*bb_*/strtod(time_str, NULL); |
| 38 | if (!secs) |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 39 | #else |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 40 | if (sscanf(time_str, "%lf", &secs) != 1) |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 41 | #endif |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 42 | 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 Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 47 | static ALWAYS_INLINE unsigned long __tv_to_jiffies(const struct timeval *tv) |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 48 | { |
| 49 | unsigned long long jif; |
| 50 | |
| 51 | jif = 1000000ULL * tv->tv_sec + tv->tv_usec; |
| 52 | |
| 53 | return jif/10000; |
| 54 | } |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 55 | # if 0 |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 56 | static 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 |
| 65 | static 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-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 71 | |
| 72 | static void arm_ioctl(unsigned long *args, |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 73 | unsigned long arg0, unsigned long arg1, unsigned long arg2) |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 74 | { |
| 75 | args[0] = arg0; |
| 76 | args[1] = arg1; |
| 77 | args[2] = arg2; |
| 78 | args[3] = 0; |
| 79 | } |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 83 | int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 84 | int brctl_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 85 | { |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 86 | static const char keywords[] ALIGN1 = |
| 87 | "addbr\0" "delbr\0" "addif\0" "delif\0" |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 88 | USE_FEATURE_BRCTL_FANCY( |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 89 | "stp\0" |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 90 | "setageing\0" "setfd\0" "sethello\0" "setmaxage\0" |
| 91 | "setpathcost\0" "setportprio\0" "setbridgeprio\0" |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 92 | ) |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 93 | USE_FEATURE_BRCTL_SHOW("showmacs\0" "show\0"); |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 94 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 95 | enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif |
| 96 | USE_FEATURE_BRCTL_FANCY(, |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 97 | ARG_stp, |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 98 | ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage, |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 99 | ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 100 | ) |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 101 | USE_FEATURE_BRCTL_SHOW(, ARG_showmacs, ARG_show) |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | int fd; |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 105 | smallint key; |
| 106 | struct ifreq ifr; |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 107 | char *br, *brif; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 108 | |
| 109 | argv++; |
| 110 | while (*argv) { |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 111 | #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-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 117 | key = index_in_strings(keywords, *argv); |
| 118 | if (key == -1) /* no match found in keywords array, bail out. */ |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 119 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
| 120 | argv++; |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 121 | fd = xsocket(AF_INET, SOCK_STREAM, 0); |
| 122 | |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 123 | #if ENABLE_FEATURE_BRCTL_SHOW |
| 124 | if (key == ARG_show) { /* show */ |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 125 | 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 Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 140 | strncpy(ifr.ifr_name, brname, IFNAMSIZ); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 141 | |
| 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-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 176 | } |
| 177 | #endif |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 178 | |
| 179 | if (!*argv) /* all but 'show' need at least one argument */ |
| 180 | bb_show_usage(); |
| 181 | |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 182 | br = *argv++; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 183 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 184 | if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */ |
| 185 | ioctl_or_perror_and_die(fd, |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 186 | key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR, |
| 187 | br, "bridge %s", br); |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 188 | goto done; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 189 | } |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 190 | |
| 191 | if (!*argv) /* all but 'addif/delif' need at least two arguments */ |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 192 | bb_show_usage(); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 193 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 194 | strncpy(ifr.ifr_name, br, IFNAMSIZ); |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 195 | if (key == ARG_addif || key == ARG_delif) { /* addif or delif */ |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 196 | brif = *argv; |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 197 | ifr.ifr_ifindex = if_nametoindex(brif); |
| 198 | if (!ifr.ifr_ifindex) { |
| 199 | bb_perror_msg_and_die("iface %s", brif); |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 200 | } |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 201 | ioctl_or_perror_and_die(fd, |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 202 | key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF, |
| 203 | &ifr, "bridge %s", br); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 204 | goto done_next_argv; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 205 | } |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 206 | #if ENABLE_FEATURE_BRCTL_FANCY |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 207 | 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 Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 213 | 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-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 221 | goto fire; |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 222 | } |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 223 | if (key == ARG_setpathcost |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 224 | || key == ARG_setportprio |
| 225 | || key == ARG_setbridgeprio |
| 226 | ) { |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 227 | 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-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 255 | if (key == ARG_setbridgeprio) { |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 256 | arg1 = arg2; |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 257 | arg2 = 0; |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 258 | } |
| 259 | arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2); |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 260 | } |
| 261 | fire: |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 262 | /* Execute the previously set command */ |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 263 | xioctl(fd, SIOCDEVPRIVATE, &ifr); |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 264 | #endif |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 265 | done_next_argv: |
| 266 | argv++; |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 267 | done: |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 268 | close(fd); |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 269 | } |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 270 | |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 271 | return EXIT_SUCCESS; |
| 272 | } |