Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 1 | /* Mode: C; |
| 2 | * |
| 3 | * Mini ifenslave implementation for busybox |
| 4 | * Copyright (C) 2005 by Marc Leeman <marc.leeman@barco.com> |
| 5 | * |
| 6 | * ifenslave.c: Configure network interfaces for parallel routing. |
| 7 | * |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 8 | * This program controls the Linux implementation of running multiple |
| 9 | * network interfaces in parallel. |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 10 | * |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 11 | * Author: Donald Becker <becker@cesdis.gsfc.nasa.gov> |
| 12 | * Copyright 1994-1996 Donald Becker |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 13 | * |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 14 | * This program is free software; you can redistribute it |
| 15 | * and/or modify it under the terms of the GNU General Public |
| 16 | * License as published by the Free Software Foundation. |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 17 | * |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 18 | * The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O |
| 19 | * Center of Excellence in Space Data and Information Sciences |
| 20 | * Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771 |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 21 | * |
| 22 | * Changes : |
| 23 | * - 2000/10/02 Willy Tarreau <willy at meta-x.org> : |
| 24 | * - few fixes. Master's MAC address is now correctly taken from |
| 25 | * the first device when not previously set ; |
| 26 | * - detach support : call BOND_RELEASE to detach an enslaved interface. |
| 27 | * - give a mini-howto from command-line help : # ifenslave -h |
| 28 | * |
| 29 | * - 2001/02/16 Chad N. Tindel <ctindel at ieee dot org> : |
| 30 | * - Master is now brought down before setting the MAC address. In |
| 31 | * the 2.4 kernel you can't change the MAC address while the device is |
| 32 | * up because you get EBUSY. |
| 33 | * |
| 34 | * - 2001/09/13 Takao Indoh <indou dot takao at jp dot fujitsu dot com> |
| 35 | * - Added the ability to change the active interface on a mode 1 bond |
| 36 | * at runtime. |
| 37 | * |
| 38 | * - 2001/10/23 Chad N. Tindel <ctindel at ieee dot org> : |
| 39 | * - No longer set the MAC address of the master. The bond device will |
| 40 | * take care of this itself |
| 41 | * - Try the SIOC*** versions of the bonding ioctls before using the |
| 42 | * old versions |
| 43 | * - 2002/02/18 Erik Habbinga <erik_habbinga @ hp dot com> : |
| 44 | * - ifr2.ifr_flags was not initialized in the hwaddr_notset case, |
| 45 | * SIOCGIFFLAGS now called before hwaddr_notset test |
| 46 | * |
| 47 | * - 2002/10/31 Tony Cureington <tony.cureington * hp_com> : |
| 48 | * - If the master does not have a hardware address when the first slave |
| 49 | * is enslaved, the master is assigned the hardware address of that |
| 50 | * slave - there is a comment in bonding.c stating "ifenslave takes |
| 51 | * care of this now." This corrects the problem of slaves having |
| 52 | * different hardware addresses in active-backup mode when |
| 53 | * multiple interfaces are specified on a single ifenslave command |
| 54 | * (ifenslave bond0 eth0 eth1). |
| 55 | * |
| 56 | * - 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and |
| 57 | * Shmulik Hen <shmulik.hen at intel dot com> |
| 58 | * - Moved setting the slave's mac address and openning it, from |
| 59 | * the application to the driver. This enables support of modes |
| 60 | * that need to use the unique mac address of each slave. |
| 61 | * The driver also takes care of closing the slave and restoring its |
| 62 | * original mac address upon release. |
| 63 | * In addition, block possibility of enslaving before the master is up. |
| 64 | * This prevents putting the system in an undefined state. |
| 65 | * |
| 66 | * - 2003/05/01 - Amir Noam <amir.noam at intel dot com> |
| 67 | * - Added ABI version control to restore compatibility between |
| 68 | * new/old ifenslave and new/old bonding. |
| 69 | * - Prevent adding an adapter that is already a slave. |
| 70 | * Fixes the problem of stalling the transmission and leaving |
| 71 | * the slave in a down state. |
| 72 | * |
| 73 | * - 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com> |
| 74 | * - Prevent enslaving if the bond device is down. |
| 75 | * Fixes the problem of leaving the system in unstable state and |
| 76 | * halting when trying to remove the module. |
| 77 | * - Close socket on all abnormal exists. |
| 78 | * - Add versioning scheme that follows that of the bonding driver. |
| 79 | * current version is 1.0.0 as a base line. |
| 80 | * |
| 81 | * - 2003/05/22 - Jay Vosburgh <fubar at us dot ibm dot com> |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 82 | * - ifenslave -c was broken; it's now fixed |
| 83 | * - Fixed problem with routes vanishing from master during enslave |
| 84 | * processing. |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 85 | * |
| 86 | * - 2003/05/27 - Amir Noam <amir.noam at intel dot com> |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 87 | * - Fix backward compatibility issues: |
| 88 | * For drivers not using ABI versions, slave was set down while |
| 89 | * it should be left up before enslaving. |
| 90 | * Also, master was not set down and the default set_mac_address() |
| 91 | * would fail and generate an error message in the system log. |
| 92 | * - For opt_c: slave should not be set to the master's setting |
| 93 | * while it is running. It was already set during enslave. To |
| 94 | * simplify things, it is now handeled separately. |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 95 | * |
| 96 | * - 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com> |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 97 | * - Code cleanup and style changes |
| 98 | * set version to 1.1.0 |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 99 | */ |
| 100 | |
| 101 | #include "libbb.h" |
| 102 | |
| 103 | #include <net/if_arp.h> |
| 104 | #include <linux/if_bonding.h> |
| 105 | #include <linux/sockios.h> |
| 106 | |
| 107 | typedef unsigned long long u64; /* hack, so we may include kernel's ethtool.h */ |
| 108 | typedef uint32_t u32; /* ditto */ |
| 109 | typedef uint16_t u16; /* ditto */ |
| 110 | typedef uint8_t u8; /* ditto */ |
| 111 | #include <linux/ethtool.h> |
| 112 | |
| 113 | |
| 114 | struct dev_data { |
| 115 | struct ifreq mtu, flags, hwaddr; |
| 116 | }; |
| 117 | |
| 118 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 119 | enum { skfd = 3 }; /* AF_INET socket for ioctl() calls. */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 120 | struct globals { |
| 121 | unsigned abi_ver; /* userland - kernel ABI version */ |
| 122 | smallint hwaddr_set; /* Master's hwaddr is set */ |
| 123 | struct dev_data master; |
| 124 | struct dev_data slave; |
| 125 | }; |
| 126 | #define G (*ptr_to_globals) |
| 127 | #define abi_ver (G.abi_ver ) |
| 128 | #define hwaddr_set (G.hwaddr_set) |
| 129 | #define master (G.master ) |
| 130 | #define slave (G.slave ) |
| 131 | #define INIT_G() do { \ |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 132 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 133 | } while (0) |
| 134 | |
| 135 | |
| 136 | static void get_drv_info(char *master_ifname); |
| 137 | static int get_if_settings(char *ifname, struct dev_data *dd); |
| 138 | static int get_slave_flags(char *slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 139 | static int set_hwaddr(char *ifname, struct sockaddr *hwaddr); |
| 140 | static int set_mtu(char *ifname, int mtu); |
| 141 | static int set_if_flags(char *ifname, int flags); |
| 142 | static int set_if_up(char *ifname, int flags); |
| 143 | static int set_if_down(char *ifname, int flags); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 144 | static int clear_if_addr(char *ifname); |
| 145 | static int set_if_addr(char *master_ifname, char *slave_ifname); |
| 146 | static void change_active(char *master_ifname, char *slave_ifname); |
| 147 | static int enslave(char *master_ifname, char *slave_ifname); |
| 148 | static int release(char *master_ifname, char *slave_ifname); |
| 149 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 150 | static void strncpy_IFNAMSIZ(char *dst, const char *src) |
| 151 | { |
| 152 | strncpy(dst, src, IFNAMSIZ); |
| 153 | } |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 154 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 155 | static int ioctl_on_skfd(unsigned request, struct ifreq *ifr) |
| 156 | { |
| 157 | return ioctl(skfd, request, ifr); |
| 158 | } |
| 159 | |
| 160 | static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname) |
| 161 | { |
| 162 | strncpy_IFNAMSIZ(ifr->ifr_name, ifname); |
| 163 | return ioctl_on_skfd(request, ifr); |
| 164 | } |
| 165 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 166 | int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 167 | int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 168 | { |
| 169 | char *master_ifname, *slave_ifname; |
| 170 | int rv; |
| 171 | int res; |
| 172 | unsigned opt; |
| 173 | enum { |
| 174 | OPT_c = (1 << 0), |
| 175 | OPT_d = (1 << 1), |
| 176 | OPT_f = (1 << 2), |
| 177 | }; |
| 178 | #if ENABLE_GETOPT_LONG |
| 179 | static const char ifenslave_longopts[] ALIGN1 = |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 180 | "change-active\0" No_argument "c" |
| 181 | "detach\0" No_argument "d" |
| 182 | "force\0" No_argument "f" |
| 183 | ; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 184 | |
| 185 | applet_long_options = ifenslave_longopts; |
| 186 | #endif |
Denis Vlasenko | 38100a4 | 2008-05-20 18:57:35 +0000 | [diff] [blame] | 187 | INIT_G(); |
| 188 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 189 | opt = getopt32(argv, "cdf"); |
| 190 | argv += optind; |
| 191 | if (opt & (opt-1)) /* options check */ |
| 192 | bb_show_usage(); |
| 193 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 194 | master_ifname = *argv++; |
| 195 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 196 | /* No interface names - show all interfaces. */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 197 | if (!master_ifname) { |
| 198 | display_interfaces(NULL); |
| 199 | return EXIT_SUCCESS; |
| 200 | } |
| 201 | |
| 202 | /* Open a basic socket */ |
| 203 | xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd); |
| 204 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 205 | /* Exchange abi version with bonding module */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 206 | get_drv_info(master_ifname); |
| 207 | |
| 208 | slave_ifname = *argv++; |
| 209 | if (!slave_ifname) { |
| 210 | if (opt & (OPT_d|OPT_c)) { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 211 | /* --change or --detach, and no slaves given - |
| 212 | * show all interfaces. */ |
| 213 | display_interfaces(slave_ifname /* == NULL */); |
| 214 | return 2; /* why 2? */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 215 | } |
| 216 | /* A single arg means show the |
| 217 | * configuration for this interface |
| 218 | */ |
| 219 | display_interfaces(master_ifname); |
| 220 | return EXIT_SUCCESS; |
| 221 | } |
| 222 | |
| 223 | res = get_if_settings(master_ifname, &master); |
| 224 | if (res) { |
| 225 | /* Probably a good reason not to go on */ |
| 226 | bb_perror_msg_and_die("%s: can't get settings", master_ifname); |
| 227 | } |
| 228 | |
| 229 | /* check if master is indeed a master; |
| 230 | * if not then fail any operation |
| 231 | */ |
| 232 | if (!(master.flags.ifr_flags & IFF_MASTER)) |
| 233 | bb_error_msg_and_die("%s is not a master", master_ifname); |
| 234 | |
| 235 | /* check if master is up; if not then fail any operation */ |
| 236 | if (!(master.flags.ifr_flags & IFF_UP)) |
| 237 | bb_error_msg_and_die("%s is not up", master_ifname); |
| 238 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 239 | /* No opts: neither -c[hange] nor -d[etach] -> it's "enslave" then; |
| 240 | * and -f[orce] is not there too */ |
| 241 | if (!opt) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 242 | /* The family '1' is ARPHRD_ETHER for ethernet. */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 243 | if (master.hwaddr.ifr_hwaddr.sa_family != 1) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 244 | bb_error_msg_and_die( |
| 245 | "%s is not ethernet-like (-f overrides)", |
| 246 | master_ifname); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* Accepts only one slave */ |
| 251 | if (opt & OPT_c) { |
| 252 | /* change active slave */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 253 | if (get_slave_flags(slave_ifname)) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 254 | bb_perror_msg_and_die( |
| 255 | "%s: can't get flags", slave_ifname); |
| 256 | } |
| 257 | change_active(master_ifname, slave_ifname); |
| 258 | return EXIT_SUCCESS; |
| 259 | } |
| 260 | |
| 261 | /* Accept multiple slaves */ |
| 262 | res = 0; |
| 263 | do { |
| 264 | if (opt & OPT_d) { |
| 265 | /* detach a slave interface from the master */ |
| 266 | rv = get_slave_flags(slave_ifname); |
| 267 | if (rv) { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 268 | /* Can't work with this slave, */ |
| 269 | /* remember the error and skip it */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 270 | bb_perror_msg( |
| 271 | "skipping %s: can't get flags", |
| 272 | slave_ifname); |
| 273 | res = rv; |
| 274 | continue; |
| 275 | } |
| 276 | rv = release(master_ifname, slave_ifname); |
| 277 | if (rv) { |
| 278 | bb_perror_msg( |
| 279 | "master %s, slave %s: " |
| 280 | "can't release", |
| 281 | master_ifname, slave_ifname); |
| 282 | res = rv; |
| 283 | } |
| 284 | } else { |
| 285 | /* attach a slave interface to the master */ |
| 286 | rv = get_if_settings(slave_ifname, &slave); |
| 287 | if (rv) { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 288 | /* Can't work with this slave, */ |
| 289 | /* remember the error and skip it */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 290 | bb_perror_msg( |
| 291 | "skipping %s: can't get settings", |
| 292 | slave_ifname); |
| 293 | res = rv; |
| 294 | continue; |
| 295 | } |
| 296 | rv = enslave(master_ifname, slave_ifname); |
| 297 | if (rv) { |
| 298 | bb_perror_msg( |
| 299 | "master %s, slave %s: " |
| 300 | "can't enslave", |
| 301 | master_ifname, slave_ifname); |
| 302 | res = rv; |
| 303 | } |
| 304 | } |
| 305 | } while ((slave_ifname = *argv++) != NULL); |
| 306 | |
| 307 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 308 | close(skfd); |
| 309 | } |
| 310 | |
| 311 | return res; |
| 312 | } |
| 313 | |
| 314 | static void get_drv_info(char *master_ifname) |
| 315 | { |
| 316 | struct ifreq ifr; |
| 317 | struct ethtool_drvinfo info; |
| 318 | |
| 319 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 320 | ifr.ifr_data = (caddr_t)&info; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 321 | info.cmd = ETHTOOL_GDRVINFO; |
| 322 | strncpy(info.driver, "ifenslave", 32); |
| 323 | snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 324 | if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 325 | if (errno == EOPNOTSUPP) |
| 326 | return; |
| 327 | bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname); |
| 328 | } |
| 329 | |
| 330 | abi_ver = bb_strtou(info.fw_version, NULL, 0); |
| 331 | if (errno) |
| 332 | bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static void change_active(char *master_ifname, char *slave_ifname) |
| 336 | { |
| 337 | struct ifreq ifr; |
| 338 | |
| 339 | if (!(slave.flags.ifr_flags & IFF_SLAVE)) { |
| 340 | bb_error_msg_and_die( |
| 341 | "%s is not a slave", |
| 342 | slave_ifname); |
| 343 | } |
| 344 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 345 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 346 | if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname) |
| 347 | && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 348 | ) { |
| 349 | bb_perror_msg_and_die( |
| 350 | "master %s, slave %s: can't " |
| 351 | "change active", |
| 352 | master_ifname, slave_ifname); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static int enslave(char *master_ifname, char *slave_ifname) |
| 357 | { |
| 358 | struct ifreq ifr; |
| 359 | int res; |
| 360 | |
| 361 | if (slave.flags.ifr_flags & IFF_SLAVE) { |
| 362 | bb_error_msg( |
| 363 | "%s is already a slave", |
| 364 | slave_ifname); |
| 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | res = set_if_down(slave_ifname, slave.flags.ifr_flags); |
| 369 | if (res) |
| 370 | return res; |
| 371 | |
| 372 | if (abi_ver < 2) { |
| 373 | /* Older bonding versions would panic if the slave has no IP |
| 374 | * address, so get the IP setting from the master. |
| 375 | */ |
| 376 | res = set_if_addr(master_ifname, slave_ifname); |
| 377 | if (res) { |
| 378 | bb_perror_msg("%s: can't set address", slave_ifname); |
| 379 | return res; |
| 380 | } |
| 381 | } else { |
| 382 | res = clear_if_addr(slave_ifname); |
| 383 | if (res) { |
| 384 | bb_perror_msg("%s: can't clear address", slave_ifname); |
| 385 | return res; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 390 | res = set_mtu(slave_ifname, master.mtu.ifr_mtu); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 391 | if (res) { |
| 392 | bb_perror_msg("%s: can't set MTU", slave_ifname); |
| 393 | return res; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (hwaddr_set) { |
| 398 | /* Master already has an hwaddr |
| 399 | * so set it's hwaddr to the slave |
| 400 | */ |
| 401 | if (abi_ver < 1) { |
| 402 | /* The driver is using an old ABI, so |
| 403 | * the application sets the slave's |
| 404 | * hwaddr |
| 405 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 406 | if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 407 | bb_perror_msg("%s: can't set hw address", |
| 408 | slave_ifname); |
| 409 | goto undo_mtu; |
| 410 | } |
| 411 | |
| 412 | /* For old ABI the application needs to bring the |
| 413 | * slave back up |
| 414 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 415 | if (set_if_up(slave_ifname, slave.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 416 | goto undo_slave_mac; |
| 417 | } |
| 418 | /* The driver is using a new ABI, |
| 419 | * so the driver takes care of setting |
| 420 | * the slave's hwaddr and bringing |
| 421 | * it up again |
| 422 | */ |
| 423 | } else { |
| 424 | /* No hwaddr for master yet, so |
| 425 | * set the slave's hwaddr to it |
| 426 | */ |
| 427 | if (abi_ver < 1) { |
| 428 | /* For old ABI, the master needs to be |
| 429 | * down before setting it's hwaddr |
| 430 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 431 | if (set_if_down(master_ifname, master.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 432 | goto undo_mtu; |
| 433 | } |
| 434 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 435 | if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 436 | bb_error_msg("%s: can't set hw address", |
| 437 | master_ifname); |
| 438 | goto undo_mtu; |
| 439 | } |
| 440 | |
| 441 | if (abi_ver < 1) { |
| 442 | /* For old ABI, bring the master |
| 443 | * back up |
| 444 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 445 | if (set_if_up(master_ifname, master.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 446 | goto undo_master_mac; |
| 447 | } |
| 448 | |
| 449 | hwaddr_set = 1; |
| 450 | } |
| 451 | |
| 452 | /* Do the real thing */ |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 453 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 454 | if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname) |
| 455 | && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 456 | ) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 457 | goto undo_master_mac; |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 458 | } |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 459 | |
| 460 | return 0; |
| 461 | |
| 462 | /* rollback (best effort) */ |
| 463 | undo_master_mac: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 464 | set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr)); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 465 | hwaddr_set = 0; |
| 466 | goto undo_mtu; |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 467 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 468 | undo_slave_mac: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 469 | set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr)); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 470 | undo_mtu: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 471 | set_mtu(slave_ifname, slave.mtu.ifr_mtu); |
| 472 | return 1; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static int release(char *master_ifname, char *slave_ifname) |
| 476 | { |
| 477 | struct ifreq ifr; |
| 478 | int res = 0; |
| 479 | |
| 480 | if (!(slave.flags.ifr_flags & IFF_SLAVE)) { |
| 481 | bb_error_msg("%s is not a slave", |
| 482 | slave_ifname); |
| 483 | return 1; |
| 484 | } |
| 485 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 486 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 487 | if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0 |
| 488 | && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0 |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 489 | ) { |
| 490 | return 1; |
| 491 | } |
| 492 | if (abi_ver < 1) { |
| 493 | /* The driver is using an old ABI, so we'll set the interface |
| 494 | * down to avoid any conflicts due to same MAC/IP |
| 495 | */ |
| 496 | res = set_if_down(slave_ifname, slave.flags.ifr_flags); |
| 497 | } |
| 498 | |
| 499 | /* set to default mtu */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 500 | set_mtu(slave_ifname, 1500); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 501 | |
| 502 | return res; |
| 503 | } |
| 504 | |
| 505 | static int get_if_settings(char *ifname, struct dev_data *dd) |
| 506 | { |
| 507 | int res; |
| 508 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 509 | res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname); |
| 510 | res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname); |
| 511 | res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 512 | |
| 513 | return res; |
| 514 | } |
| 515 | |
| 516 | static int get_slave_flags(char *slave_ifname) |
| 517 | { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 518 | return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 521 | static int set_hwaddr(char *ifname, struct sockaddr *hwaddr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 522 | { |
| 523 | struct ifreq ifr; |
| 524 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 525 | memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr)); |
| 526 | return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 529 | static int set_mtu(char *ifname, int mtu) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 530 | { |
| 531 | struct ifreq ifr; |
| 532 | |
| 533 | ifr.ifr_mtu = mtu; |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 534 | return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 537 | static int set_if_flags(char *ifname, int flags) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 538 | { |
| 539 | struct ifreq ifr; |
| 540 | |
| 541 | ifr.ifr_flags = flags; |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 542 | return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 545 | static int set_if_up(char *ifname, int flags) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 546 | { |
| 547 | int res = set_if_flags(ifname, flags | IFF_UP); |
| 548 | if (res) |
| 549 | bb_perror_msg("%s: can't up", ifname); |
| 550 | return res; |
| 551 | } |
| 552 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 553 | static int set_if_down(char *ifname, int flags) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 554 | { |
| 555 | int res = set_if_flags(ifname, flags & ~IFF_UP); |
| 556 | if (res) |
| 557 | bb_perror_msg("%s: can't down", ifname); |
| 558 | return res; |
| 559 | } |
| 560 | |
| 561 | static int clear_if_addr(char *ifname) |
| 562 | { |
| 563 | struct ifreq ifr; |
| 564 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 565 | ifr.ifr_addr.sa_family = AF_INET; |
| 566 | memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data)); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 567 | return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static int set_if_addr(char *master_ifname, char *slave_ifname) |
| 571 | { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 572 | #if (SIOCGIFADDR | SIOCSIFADDR \ |
| 573 | | SIOCGIFDSTADDR | SIOCSIFDSTADDR \ |
| 574 | | SIOCGIFBRDADDR | SIOCSIFBRDADDR \ |
| 575 | | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff |
| 576 | #define INT uint16_t |
| 577 | #else |
| 578 | #define INT int |
| 579 | #endif |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 580 | static const struct { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 581 | INT g_ioctl; |
| 582 | INT s_ioctl; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 583 | } ifra[] = { |
| 584 | { SIOCGIFADDR, SIOCSIFADDR }, |
| 585 | { SIOCGIFDSTADDR, SIOCSIFDSTADDR }, |
| 586 | { SIOCGIFBRDADDR, SIOCSIFBRDADDR }, |
| 587 | { SIOCGIFNETMASK, SIOCSIFNETMASK }, |
| 588 | }; |
| 589 | |
| 590 | struct ifreq ifr; |
| 591 | int res; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 592 | unsigned i; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 593 | |
| 594 | for (i = 0; i < ARRAY_SIZE(ifra); i++) { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 595 | res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 596 | if (res < 0) { |
| 597 | ifr.ifr_addr.sa_family = AF_INET; |
| 598 | memset(ifr.ifr_addr.sa_data, 0, |
| 599 | sizeof(ifr.ifr_addr.sa_data)); |
| 600 | } |
| 601 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame^] | 602 | res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 603 | if (res < 0) |
| 604 | return res; |
| 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | } |