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 | * |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 by Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 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 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 15 | |
| 16 | //usage:#define brctl_trivial_usage |
| 17 | //usage: "COMMAND [BRIDGE [INTERFACE]]" |
| 18 | //usage:#define brctl_full_usage "\n\n" |
| 19 | //usage: "Manage ethernet bridges\n" |
| 20 | //usage: "\nCommands:" |
| 21 | //usage: IF_FEATURE_BRCTL_SHOW( |
| 22 | //usage: "\n show Show a list of bridges" |
| 23 | //usage: ) |
| 24 | //usage: "\n addbr BRIDGE Create BRIDGE" |
| 25 | //usage: "\n delbr BRIDGE Delete BRIDGE" |
| 26 | //usage: "\n addif BRIDGE IFACE Add IFACE to BRIDGE" |
| 27 | //usage: "\n delif BRIDGE IFACE Delete IFACE from BRIDGE" |
| 28 | //usage: IF_FEATURE_BRCTL_FANCY( |
| 29 | //usage: "\n setageing BRIDGE TIME Set ageing time" |
| 30 | //usage: "\n setfd BRIDGE TIME Set bridge forward delay" |
| 31 | //usage: "\n sethello BRIDGE TIME Set hello time" |
| 32 | //usage: "\n setmaxage BRIDGE TIME Set max message age" |
| 33 | //usage: "\n setpathcost BRIDGE COST Set path cost" |
| 34 | //usage: "\n setportprio BRIDGE PRIO Set port priority" |
| 35 | //usage: "\n setbridgeprio BRIDGE PRIO Set bridge priority" |
| 36 | //usage: "\n stp BRIDGE [1/yes/on|0/no/off] STP on/off" |
| 37 | //usage: ) |
| 38 | |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 39 | #include "libbb.h" |
| 40 | #include <linux/sockios.h> |
| 41 | #include <net/if.h> |
| 42 | |
Denis Vlasenko | 802cab1 | 2009-01-31 20:08:21 +0000 | [diff] [blame] | 43 | #ifndef SIOCBRADDBR |
| 44 | # define SIOCBRADDBR BRCTL_ADD_BRIDGE |
| 45 | #endif |
| 46 | #ifndef SIOCBRDELBR |
| 47 | # define SIOCBRDELBR BRCTL_DEL_BRIDGE |
| 48 | #endif |
| 49 | #ifndef SIOCBRADDIF |
| 50 | # define SIOCBRADDIF BRCTL_ADD_IF |
| 51 | #endif |
| 52 | #ifndef SIOCBRDELIF |
| 53 | # define SIOCBRDELIF BRCTL_DEL_IF |
| 54 | #endif |
| 55 | |
| 56 | |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 57 | /* Maximum number of ports supported per bridge interface. */ |
| 58 | #ifndef MAX_PORTS |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 59 | # define MAX_PORTS 32 |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | /* Use internal number parsing and not the "exact" conversion. */ |
| 63 | /* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */ |
| 64 | #define BRCTL_USE_INTERNAL 1 |
| 65 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 66 | #if ENABLE_FEATURE_BRCTL_FANCY |
Denys Vlasenko | 5fa6d1a | 2015-10-05 11:15:43 +0200 | [diff] [blame] | 67 | /* #include <linux/if_bridge.h> |
| 68 | * breaks on musl: we already included netinet/in.h in libbb.h, |
| 69 | * if we include <linux/if_bridge.h> here, we get this: |
| 70 | * In file included from /usr/include/linux/if_bridge.h:18, |
| 71 | * from networking/brctl.c:67: |
| 72 | * /usr/include/linux/in6.h:32: error: redefinition of 'struct in6_addr' |
| 73 | * /usr/include/linux/in6.h:49: error: redefinition of 'struct sockaddr_in6' |
| 74 | * /usr/include/linux/in6.h:59: error: redefinition of 'struct ipv6_mreq' |
| 75 | */ |
| 76 | /* From <linux/if_bridge.h> */ |
| 77 | #define BRCTL_GET_VERSION 0 |
| 78 | #define BRCTL_GET_BRIDGES 1 |
| 79 | #define BRCTL_ADD_BRIDGE 2 |
| 80 | #define BRCTL_DEL_BRIDGE 3 |
| 81 | #define BRCTL_ADD_IF 4 |
| 82 | #define BRCTL_DEL_IF 5 |
| 83 | #define BRCTL_GET_BRIDGE_INFO 6 |
| 84 | #define BRCTL_GET_PORT_LIST 7 |
| 85 | #define BRCTL_SET_BRIDGE_FORWARD_DELAY 8 |
| 86 | #define BRCTL_SET_BRIDGE_HELLO_TIME 9 |
| 87 | #define BRCTL_SET_BRIDGE_MAX_AGE 10 |
| 88 | #define BRCTL_SET_AGEING_TIME 11 |
| 89 | #define BRCTL_SET_GC_INTERVAL 12 |
| 90 | #define BRCTL_GET_PORT_INFO 13 |
| 91 | #define BRCTL_SET_BRIDGE_STP_STATE 14 |
| 92 | #define BRCTL_SET_BRIDGE_PRIORITY 15 |
| 93 | #define BRCTL_SET_PORT_PRIORITY 16 |
| 94 | #define BRCTL_SET_PATH_COST 17 |
| 95 | #define BRCTL_GET_FDB_ENTRIES 18 |
| 96 | struct __bridge_info { |
| 97 | uint64_t designated_root; |
| 98 | uint64_t bridge_id; |
| 99 | uint32_t root_path_cost; |
| 100 | uint32_t max_age; |
| 101 | uint32_t hello_time; |
| 102 | uint32_t forward_delay; |
| 103 | uint32_t bridge_max_age; |
| 104 | uint32_t bridge_hello_time; |
| 105 | uint32_t bridge_forward_delay; |
| 106 | uint8_t topology_change; |
| 107 | uint8_t topology_change_detected; |
| 108 | uint8_t root_port; |
| 109 | uint8_t stp_enabled; |
| 110 | uint32_t ageing_time; |
| 111 | uint32_t gc_interval; |
| 112 | uint32_t hello_timer_value; |
| 113 | uint32_t tcn_timer_value; |
| 114 | uint32_t topology_change_timer_value; |
| 115 | uint32_t gc_timer_value; |
| 116 | }; |
| 117 | /* end <linux/if_bridge.h> */ |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 118 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 119 | /* FIXME: These 4 funcs are not really clean and could be improved */ |
Denys Vlasenko | 49b8e72 | 2012-06-10 14:16:16 +0200 | [diff] [blame] | 120 | static ALWAYS_INLINE void bb_strtotimeval(struct timeval *tv, |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 121 | const char *time_str) |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 122 | { |
| 123 | double secs; |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 124 | # if BRCTL_USE_INTERNAL |
Maciek Borzecki | 46abfc0 | 2010-03-16 12:41:29 +0100 | [diff] [blame] | 125 | char *endptr; |
| 126 | secs = /*bb_*/strtod(time_str, &endptr); |
| 127 | if (endptr == time_str) |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 128 | # else |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 129 | if (sscanf(time_str, "%lf", &secs) != 1) |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 130 | # endif |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 131 | bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec"); |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 132 | tv->tv_sec = secs; |
| 133 | tv->tv_usec = 1000000 * (secs - tv->tv_sec); |
| 134 | } |
| 135 | |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 136 | 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] | 137 | { |
| 138 | unsigned long long jif; |
| 139 | |
| 140 | jif = 1000000ULL * tv->tv_sec + tv->tv_usec; |
| 141 | |
| 142 | return jif/10000; |
| 143 | } |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 144 | # if 0 |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 145 | static void jiffies_to_tv(struct timeval *tv, unsigned long jiffies) |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 146 | { |
| 147 | unsigned long long tvusec; |
| 148 | |
| 149 | tvusec = 10000ULL*jiffies; |
| 150 | tv->tv_sec = tvusec/1000000; |
| 151 | tv->tv_usec = tvusec - 1000000 * tv->tv_sec; |
| 152 | } |
| 153 | # endif |
| 154 | static unsigned long str_to_jiffies(const char *time_str) |
| 155 | { |
| 156 | struct timeval tv; |
Denys Vlasenko | 49b8e72 | 2012-06-10 14:16:16 +0200 | [diff] [blame] | 157 | bb_strtotimeval(&tv, time_str); |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 158 | return tv_to_jiffies(&tv); |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 159 | } |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 160 | |
| 161 | static void arm_ioctl(unsigned long *args, |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 162 | unsigned long arg0, unsigned long arg1, unsigned long arg2) |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 163 | { |
| 164 | args[0] = arg0; |
| 165 | args[1] = arg1; |
| 166 | args[2] = arg2; |
| 167 | args[3] = 0; |
| 168 | } |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 169 | #endif |
| 170 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 171 | |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 172 | int brctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 173 | int brctl_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 174 | { |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 175 | static const char keywords[] ALIGN1 = |
| 176 | "addbr\0" "delbr\0" "addif\0" "delif\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 177 | IF_FEATURE_BRCTL_FANCY( |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 178 | "stp\0" |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 179 | "setageing\0" "setfd\0" "sethello\0" "setmaxage\0" |
| 180 | "setpathcost\0" "setportprio\0" "setbridgeprio\0" |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 181 | ) |
Nicolas Thill | f47ce07 | 2012-09-25 14:06:01 +0200 | [diff] [blame] | 182 | IF_FEATURE_BRCTL_SHOW("show\0"); |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 183 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 184 | enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 185 | IF_FEATURE_BRCTL_FANCY(, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 186 | ARG_stp, |
| 187 | ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage, |
| 188 | ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 189 | ) |
Nicolas Thill | f47ce07 | 2012-09-25 14:06:01 +0200 | [diff] [blame] | 190 | IF_FEATURE_BRCTL_SHOW(, ARG_show) |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | int fd; |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 194 | smallint key; |
| 195 | struct ifreq ifr; |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 196 | char *br, *brif; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 197 | |
| 198 | argv++; |
| 199 | while (*argv) { |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 200 | #if ENABLE_FEATURE_BRCTL_FANCY |
| 201 | int ifidx[MAX_PORTS]; |
| 202 | unsigned long args[4]; |
| 203 | ifr.ifr_data = (char *) &args; |
| 204 | #endif |
| 205 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 206 | key = index_in_strings(keywords, *argv); |
| 207 | if (key == -1) /* no match found in keywords array, bail out. */ |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 208 | bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name); |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 209 | argv++; |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 210 | fd = xsocket(AF_INET, SOCK_STREAM, 0); |
| 211 | |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 212 | #if ENABLE_FEATURE_BRCTL_SHOW |
| 213 | if (key == ARG_show) { /* show */ |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 214 | char brname[IFNAMSIZ]; |
| 215 | int bridx[MAX_PORTS]; |
| 216 | int i, num; |
| 217 | arm_ioctl(args, BRCTL_GET_BRIDGES, |
| 218 | (unsigned long) bridx, MAX_PORTS); |
| 219 | num = xioctl(fd, SIOCGIFBR, args); |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 220 | puts("bridge name\tbridge id\t\tSTP enabled\tinterfaces"); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 221 | for (i = 0; i < num; i++) { |
| 222 | char ifname[IFNAMSIZ]; |
| 223 | int j, tabs; |
| 224 | struct __bridge_info bi; |
| 225 | unsigned char *x; |
| 226 | |
| 227 | if (!if_indextoname(bridx[i], brname)) |
| 228 | bb_perror_msg_and_die("can't get bridge name for index %d", i); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 229 | strncpy_IFNAMSIZ(ifr.ifr_name, brname); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 230 | |
| 231 | arm_ioctl(args, BRCTL_GET_BRIDGE_INFO, |
| 232 | (unsigned long) &bi, 0); |
| 233 | xioctl(fd, SIOCDEVPRIVATE, &ifr); |
| 234 | printf("%s\t\t", brname); |
| 235 | |
| 236 | /* print bridge id */ |
| 237 | x = (unsigned char *) &bi.bridge_id; |
| 238 | for (j = 0; j < 8; j++) { |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 239 | printf("%02x", x[j]); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 240 | if (j == 1) |
| 241 | bb_putchar('.'); |
| 242 | } |
| 243 | printf(bi.stp_enabled ? "\tyes" : "\tno"); |
| 244 | |
| 245 | /* print interface list */ |
| 246 | arm_ioctl(args, BRCTL_GET_PORT_LIST, |
| 247 | (unsigned long) ifidx, MAX_PORTS); |
| 248 | xioctl(fd, SIOCDEVPRIVATE, &ifr); |
| 249 | tabs = 0; |
| 250 | for (j = 0; j < MAX_PORTS; j++) { |
| 251 | if (!ifidx[j]) |
| 252 | continue; |
| 253 | if (!if_indextoname(ifidx[j], ifname)) |
| 254 | bb_perror_msg_and_die("can't get interface name for index %d", j); |
| 255 | if (tabs) |
| 256 | printf("\t\t\t\t\t"); |
| 257 | else |
| 258 | tabs = 1; |
| 259 | printf("\t\t%s\n", ifname); |
| 260 | } |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 261 | if (!tabs) /* bridge has no interfaces */ |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 262 | bb_putchar('\n'); |
| 263 | } |
| 264 | goto done; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 265 | } |
| 266 | #endif |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 267 | |
| 268 | if (!*argv) /* all but 'show' need at least one argument */ |
| 269 | bb_show_usage(); |
| 270 | |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 271 | br = *argv++; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 272 | |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 273 | if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */ |
| 274 | ioctl_or_perror_and_die(fd, |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 275 | key == ARG_addbr ? SIOCBRADDBR : SIOCBRDELBR, |
| 276 | br, "bridge %s", br); |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 277 | goto done; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 278 | } |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 279 | |
Bernhard Reutner-Fischer | 0c0f176 | 2010-03-17 11:23:04 +0100 | [diff] [blame] | 280 | if (!*argv) /* all but 'addbr/delbr' need at least two arguments */ |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 281 | bb_show_usage(); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 282 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 283 | strncpy_IFNAMSIZ(ifr.ifr_name, br); |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 284 | if (key == ARG_addif || key == ARG_delif) { /* addif or delif */ |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 285 | brif = *argv; |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 286 | ifr.ifr_ifindex = if_nametoindex(brif); |
| 287 | if (!ifr.ifr_ifindex) { |
| 288 | bb_perror_msg_and_die("iface %s", brif); |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 289 | } |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 290 | ioctl_or_perror_and_die(fd, |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 291 | key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF, |
| 292 | &ifr, "bridge %s", br); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 293 | goto done_next_argv; |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 294 | } |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 295 | #if ENABLE_FEATURE_BRCTL_FANCY |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 296 | if (key == ARG_stp) { /* stp */ |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 297 | static const char no_yes[] ALIGN1 = |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 298 | "0\0" "off\0" "n\0" "no\0" /* 0 .. 3 */ |
| 299 | "1\0" "on\0" "y\0" "yes\0"; /* 4 .. 7 */ |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 300 | int onoff = index_in_strings(no_yes, *argv); |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 301 | if (onoff < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 302 | bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name); |
Maciek Borzecki | 30ebd7b | 2010-03-23 05:18:38 +0100 | [diff] [blame] | 303 | onoff = (unsigned)onoff / 4; |
| 304 | arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE, onoff, 0); |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 305 | goto fire; |
| 306 | } |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 307 | if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */ |
| 308 | static const uint8_t ops[] ALIGN1 = { |
| 309 | BRCTL_SET_AGEING_TIME, /* ARG_setageing */ |
| 310 | BRCTL_SET_BRIDGE_FORWARD_DELAY, /* ARG_setfd */ |
| 311 | BRCTL_SET_BRIDGE_HELLO_TIME, /* ARG_sethello */ |
| 312 | BRCTL_SET_BRIDGE_MAX_AGE /* ARG_setmaxage */ |
| 313 | }; |
| 314 | 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] | 315 | goto fire; |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 316 | } |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 317 | if (key == ARG_setpathcost |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 318 | || key == ARG_setportprio |
| 319 | || key == ARG_setbridgeprio |
| 320 | ) { |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 321 | static const uint8_t ops[] ALIGN1 = { |
| 322 | BRCTL_SET_PATH_COST, /* ARG_setpathcost */ |
| 323 | BRCTL_SET_PORT_PRIORITY, /* ARG_setportprio */ |
| 324 | BRCTL_SET_BRIDGE_PRIORITY /* ARG_setbridgeprio */ |
| 325 | }; |
| 326 | int port = -1; |
| 327 | unsigned arg1, arg2; |
| 328 | |
| 329 | if (key != ARG_setbridgeprio) { |
| 330 | /* get portnum */ |
| 331 | unsigned i; |
| 332 | |
| 333 | port = if_nametoindex(*argv++); |
| 334 | if (!port) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 335 | bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, "port"); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 336 | memset(ifidx, 0, sizeof ifidx); |
| 337 | arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 338 | MAX_PORTS); |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 339 | xioctl(fd, SIOCDEVPRIVATE, &ifr); |
| 340 | for (i = 0; i < MAX_PORTS; i++) { |
| 341 | if (ifidx[i] == port) { |
| 342 | port = i; |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | arg1 = port; |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 348 | arg2 = xatoi_positive(*argv); |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 349 | if (key == ARG_setbridgeprio) { |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 350 | arg1 = arg2; |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 351 | arg2 = 0; |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 352 | } |
| 353 | arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2); |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 354 | } |
| 355 | fire: |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 356 | /* Execute the previously set command */ |
Bernhard Reutner-Fischer | 2b11fb4 | 2008-01-14 16:10:11 +0000 | [diff] [blame] | 357 | xioctl(fd, SIOCDEVPRIVATE, &ifr); |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 358 | #endif |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 359 | done_next_argv: |
| 360 | argv++; |
Bernhard Reutner-Fischer | 1aac3ab | 2008-01-13 18:43:50 +0000 | [diff] [blame] | 361 | done: |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 362 | close(fd); |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 363 | } |
Denis Vlasenko | 278a1c2 | 2008-04-06 07:17:02 +0000 | [diff] [blame] | 364 | |
Bernhard Reutner-Fischer | d27d925 | 2008-01-13 15:23:27 +0000 | [diff] [blame] | 365 | return EXIT_SUCCESS; |
| 366 | } |