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 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 136 | /* NOINLINEs are placed where it results in smaller code (gcc 4.3.1) */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 137 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 138 | static void strncpy_IFNAMSIZ(char *dst, const char *src) |
| 139 | { |
| 140 | strncpy(dst, src, IFNAMSIZ); |
| 141 | } |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 143 | static int ioctl_on_skfd(unsigned request, struct ifreq *ifr) |
| 144 | { |
| 145 | return ioctl(skfd, request, ifr); |
| 146 | } |
| 147 | |
| 148 | static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname) |
| 149 | { |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 150 | strncpy_IFNAMSIZ(ifr->ifr_name, ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 151 | return ioctl_on_skfd(request, ifr); |
| 152 | } |
| 153 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 154 | static int get_if_settings(char *ifname, struct dev_data *dd) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 155 | { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 156 | int res; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 158 | res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname); |
| 159 | res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname); |
| 160 | res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 161 | |
| 162 | return res; |
| 163 | } |
| 164 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 165 | static int get_slave_flags(char *slave_ifname) |
| 166 | { |
| 167 | return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname); |
| 168 | } |
| 169 | |
| 170 | static int set_hwaddr(char *ifname, struct sockaddr *hwaddr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 171 | { |
| 172 | struct ifreq ifr; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 173 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 174 | memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr)); |
| 175 | return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname); |
| 176 | } |
| 177 | |
| 178 | static int set_mtu(char *ifname, int mtu) |
| 179 | { |
| 180 | struct ifreq ifr; |
| 181 | |
| 182 | ifr.ifr_mtu = mtu; |
| 183 | return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname); |
| 184 | } |
| 185 | |
| 186 | static int set_if_flags(char *ifname, int flags) |
| 187 | { |
| 188 | struct ifreq ifr; |
| 189 | |
| 190 | ifr.ifr_flags = flags; |
| 191 | return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname); |
| 192 | } |
| 193 | |
| 194 | static int set_if_up(char *ifname, int flags) |
| 195 | { |
| 196 | int res = set_if_flags(ifname, flags | IFF_UP); |
| 197 | if (res) |
| 198 | bb_perror_msg("%s: can't up", ifname); |
| 199 | return res; |
| 200 | } |
| 201 | |
| 202 | static int set_if_down(char *ifname, int flags) |
| 203 | { |
| 204 | int res = set_if_flags(ifname, flags & ~IFF_UP); |
| 205 | if (res) |
| 206 | bb_perror_msg("%s: can't down", ifname); |
| 207 | return res; |
| 208 | } |
| 209 | |
| 210 | static int clear_if_addr(char *ifname) |
| 211 | { |
| 212 | struct ifreq ifr; |
| 213 | |
| 214 | ifr.ifr_addr.sa_family = AF_INET; |
| 215 | memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data)); |
| 216 | return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname); |
| 217 | } |
| 218 | |
| 219 | static int set_if_addr(char *master_ifname, char *slave_ifname) |
| 220 | { |
| 221 | #if (SIOCGIFADDR | SIOCSIFADDR \ |
| 222 | | SIOCGIFDSTADDR | SIOCSIFDSTADDR \ |
| 223 | | SIOCGIFBRDADDR | SIOCSIFBRDADDR \ |
| 224 | | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff |
| 225 | #define INT uint16_t |
| 226 | #else |
| 227 | #define INT int |
| 228 | #endif |
| 229 | static const struct { |
| 230 | INT g_ioctl; |
| 231 | INT s_ioctl; |
| 232 | } ifra[] = { |
| 233 | { SIOCGIFADDR, SIOCSIFADDR }, |
| 234 | { SIOCGIFDSTADDR, SIOCSIFDSTADDR }, |
| 235 | { SIOCGIFBRDADDR, SIOCSIFBRDADDR }, |
| 236 | { SIOCGIFNETMASK, SIOCSIFNETMASK }, |
| 237 | }; |
| 238 | |
| 239 | struct ifreq ifr; |
| 240 | int res; |
| 241 | unsigned i; |
| 242 | |
| 243 | for (i = 0; i < ARRAY_SIZE(ifra); i++) { |
| 244 | res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname); |
| 245 | if (res < 0) { |
| 246 | ifr.ifr_addr.sa_family = AF_INET; |
| 247 | memset(ifr.ifr_addr.sa_data, 0, |
| 248 | sizeof(ifr.ifr_addr.sa_data)); |
| 249 | } |
| 250 | |
| 251 | res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname); |
| 252 | if (res < 0) |
| 253 | return res; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 256 | return 0; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static void change_active(char *master_ifname, char *slave_ifname) |
| 260 | { |
| 261 | struct ifreq ifr; |
| 262 | |
| 263 | if (!(slave.flags.ifr_flags & IFF_SLAVE)) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 264 | bb_error_msg_and_die("%s is not a slave", slave_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 267 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 268 | if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname) |
| 269 | && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 270 | ) { |
| 271 | bb_perror_msg_and_die( |
| 272 | "master %s, slave %s: can't " |
| 273 | "change active", |
| 274 | master_ifname, slave_ifname); |
| 275 | } |
| 276 | } |
| 277 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 278 | static NOINLINE int enslave(char *master_ifname, char *slave_ifname) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 279 | { |
| 280 | struct ifreq ifr; |
| 281 | int res; |
| 282 | |
| 283 | if (slave.flags.ifr_flags & IFF_SLAVE) { |
| 284 | bb_error_msg( |
| 285 | "%s is already a slave", |
| 286 | slave_ifname); |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | res = set_if_down(slave_ifname, slave.flags.ifr_flags); |
| 291 | if (res) |
| 292 | return res; |
| 293 | |
| 294 | if (abi_ver < 2) { |
| 295 | /* Older bonding versions would panic if the slave has no IP |
| 296 | * address, so get the IP setting from the master. |
| 297 | */ |
| 298 | res = set_if_addr(master_ifname, slave_ifname); |
| 299 | if (res) { |
| 300 | bb_perror_msg("%s: can't set address", slave_ifname); |
| 301 | return res; |
| 302 | } |
| 303 | } else { |
| 304 | res = clear_if_addr(slave_ifname); |
| 305 | if (res) { |
| 306 | bb_perror_msg("%s: can't clear address", slave_ifname); |
| 307 | return res; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 312 | res = set_mtu(slave_ifname, master.mtu.ifr_mtu); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 313 | if (res) { |
| 314 | bb_perror_msg("%s: can't set MTU", slave_ifname); |
| 315 | return res; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (hwaddr_set) { |
| 320 | /* Master already has an hwaddr |
| 321 | * so set it's hwaddr to the slave |
| 322 | */ |
| 323 | if (abi_ver < 1) { |
| 324 | /* The driver is using an old ABI, so |
| 325 | * the application sets the slave's |
| 326 | * hwaddr |
| 327 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 328 | if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 329 | bb_perror_msg("%s: can't set hw address", |
| 330 | slave_ifname); |
| 331 | goto undo_mtu; |
| 332 | } |
| 333 | |
| 334 | /* For old ABI the application needs to bring the |
| 335 | * slave back up |
| 336 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 337 | if (set_if_up(slave_ifname, slave.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 338 | goto undo_slave_mac; |
| 339 | } |
| 340 | /* The driver is using a new ABI, |
| 341 | * so the driver takes care of setting |
| 342 | * the slave's hwaddr and bringing |
| 343 | * it up again |
| 344 | */ |
| 345 | } else { |
| 346 | /* No hwaddr for master yet, so |
| 347 | * set the slave's hwaddr to it |
| 348 | */ |
| 349 | if (abi_ver < 1) { |
| 350 | /* For old ABI, the master needs to be |
| 351 | * down before setting it's hwaddr |
| 352 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 353 | if (set_if_down(master_ifname, master.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 354 | goto undo_mtu; |
| 355 | } |
| 356 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 357 | if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 358 | bb_error_msg("%s: can't set hw address", |
| 359 | master_ifname); |
| 360 | goto undo_mtu; |
| 361 | } |
| 362 | |
| 363 | if (abi_ver < 1) { |
| 364 | /* For old ABI, bring the master |
| 365 | * back up |
| 366 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 367 | if (set_if_up(master_ifname, master.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 368 | goto undo_master_mac; |
| 369 | } |
| 370 | |
| 371 | hwaddr_set = 1; |
| 372 | } |
| 373 | |
| 374 | /* Do the real thing */ |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 375 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 376 | if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname) |
| 377 | && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 378 | ) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 379 | goto undo_master_mac; |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 380 | } |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 381 | |
| 382 | return 0; |
| 383 | |
| 384 | /* rollback (best effort) */ |
| 385 | undo_master_mac: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 386 | set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr)); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 387 | hwaddr_set = 0; |
| 388 | goto undo_mtu; |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 389 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 390 | undo_slave_mac: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 391 | set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr)); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 392 | undo_mtu: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 393 | set_mtu(slave_ifname, slave.mtu.ifr_mtu); |
| 394 | return 1; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int release(char *master_ifname, char *slave_ifname) |
| 398 | { |
| 399 | struct ifreq ifr; |
| 400 | int res = 0; |
| 401 | |
| 402 | if (!(slave.flags.ifr_flags & IFF_SLAVE)) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 403 | bb_error_msg("%s is not a slave", slave_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 404 | return 1; |
| 405 | } |
| 406 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 407 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 408 | if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0 |
| 409 | && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0 |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 410 | ) { |
| 411 | return 1; |
| 412 | } |
| 413 | if (abi_ver < 1) { |
| 414 | /* The driver is using an old ABI, so we'll set the interface |
| 415 | * down to avoid any conflicts due to same MAC/IP |
| 416 | */ |
| 417 | res = set_if_down(slave_ifname, slave.flags.ifr_flags); |
| 418 | } |
| 419 | |
| 420 | /* set to default mtu */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 421 | set_mtu(slave_ifname, 1500); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 422 | |
| 423 | return res; |
| 424 | } |
| 425 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 426 | static NOINLINE void get_drv_info(char *master_ifname) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 427 | { |
| 428 | struct ifreq ifr; |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 429 | struct ethtool_drvinfo info; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 430 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 431 | memset(&ifr, 0, sizeof(ifr)); |
| 432 | ifr.ifr_data = (caddr_t)&info; |
| 433 | info.cmd = ETHTOOL_GDRVINFO; |
Denis Vlasenko | b597564 | 2008-06-08 00:24:43 +0000 | [diff] [blame] | 434 | /* both fields are 32 bytes long (long enough) */ |
| 435 | strcpy(info.driver, "ifenslave"); |
| 436 | strcpy(info.fw_version, utoa(BOND_ABI_VERSION)); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 437 | if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) { |
| 438 | if (errno == EOPNOTSUPP) |
| 439 | return; |
| 440 | bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 443 | abi_ver = bb_strtou(info.fw_version, NULL, 0); |
| 444 | if (errno) |
| 445 | bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname); |
| 446 | } |
| 447 | |
| 448 | int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 449 | int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) |
| 450 | { |
| 451 | char *master_ifname, *slave_ifname; |
| 452 | int rv; |
| 453 | int res; |
| 454 | unsigned opt; |
| 455 | enum { |
| 456 | OPT_c = (1 << 0), |
| 457 | OPT_d = (1 << 1), |
| 458 | OPT_f = (1 << 2), |
| 459 | }; |
| 460 | #if ENABLE_GETOPT_LONG |
| 461 | static const char ifenslave_longopts[] ALIGN1 = |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 462 | "change-active\0" No_argument "c" |
| 463 | "detach\0" No_argument "d" |
| 464 | "force\0" No_argument "f" |
| 465 | /* "all-interfaces\0" No_argument "a" */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 466 | ; |
| 467 | |
| 468 | applet_long_options = ifenslave_longopts; |
| 469 | #endif |
| 470 | INIT_G(); |
| 471 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 472 | opt = getopt32(argv, "cdfa"); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 473 | argv += optind; |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 474 | if (opt & (opt-1)) /* Only one option can be given */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 475 | bb_show_usage(); |
| 476 | |
| 477 | master_ifname = *argv++; |
| 478 | |
| 479 | /* No interface names - show all interfaces. */ |
| 480 | if (!master_ifname) { |
| 481 | display_interfaces(NULL); |
| 482 | return EXIT_SUCCESS; |
| 483 | } |
| 484 | |
| 485 | /* Open a basic socket */ |
| 486 | xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd); |
| 487 | |
| 488 | /* Exchange abi version with bonding module */ |
| 489 | get_drv_info(master_ifname); |
| 490 | |
| 491 | slave_ifname = *argv++; |
| 492 | if (!slave_ifname) { |
| 493 | if (opt & (OPT_d|OPT_c)) { |
| 494 | /* --change or --detach, and no slaves given - |
| 495 | * show all interfaces. */ |
| 496 | display_interfaces(slave_ifname /* == NULL */); |
| 497 | return 2; /* why 2? */ |
| 498 | } |
| 499 | /* A single arg means show the |
| 500 | * configuration for this interface |
| 501 | */ |
| 502 | display_interfaces(master_ifname); |
| 503 | return EXIT_SUCCESS; |
| 504 | } |
| 505 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 506 | if (get_if_settings(master_ifname, &master)) { |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 507 | /* Probably a good reason not to go on */ |
| 508 | bb_perror_msg_and_die("%s: can't get settings", master_ifname); |
| 509 | } |
| 510 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 511 | /* Check if master is indeed a master; |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 512 | * if not then fail any operation |
| 513 | */ |
| 514 | if (!(master.flags.ifr_flags & IFF_MASTER)) |
| 515 | bb_error_msg_and_die("%s is not a master", master_ifname); |
| 516 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 517 | /* Check if master is up; if not then fail any operation */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 518 | if (!(master.flags.ifr_flags & IFF_UP)) |
| 519 | bb_error_msg_and_die("%s is not up", master_ifname); |
| 520 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 521 | #ifdef WHY_BOTHER |
| 522 | /* Neither -c[hange] nor -d[etach] -> it's "enslave" then; |
| 523 | * and -f[orce] is not there too. Check that it's ethernet. */ |
| 524 | if (!(opt & (OPT_d|OPT_c|OPT_f)) { |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 525 | /* The family '1' is ARPHRD_ETHER for ethernet. */ |
| 526 | if (master.hwaddr.ifr_hwaddr.sa_family != 1) { |
| 527 | bb_error_msg_and_die( |
| 528 | "%s is not ethernet-like (-f overrides)", |
| 529 | master_ifname); |
| 530 | } |
| 531 | } |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 532 | #endif |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 533 | |
| 534 | /* Accepts only one slave */ |
| 535 | if (opt & OPT_c) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 536 | /* Change active slave */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 537 | if (get_slave_flags(slave_ifname)) { |
| 538 | bb_perror_msg_and_die( |
| 539 | "%s: can't get flags", slave_ifname); |
| 540 | } |
| 541 | change_active(master_ifname, slave_ifname); |
| 542 | return EXIT_SUCCESS; |
| 543 | } |
| 544 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 545 | /* Accepts multiple slaves */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 546 | res = 0; |
| 547 | do { |
| 548 | if (opt & OPT_d) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 549 | /* Detach a slave interface from the master */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 550 | rv = get_slave_flags(slave_ifname); |
| 551 | if (rv) { |
| 552 | /* Can't work with this slave, */ |
| 553 | /* remember the error and skip it */ |
| 554 | bb_perror_msg( |
| 555 | "skipping %s: can't get flags", |
| 556 | slave_ifname); |
| 557 | res = rv; |
| 558 | continue; |
| 559 | } |
| 560 | rv = release(master_ifname, slave_ifname); |
| 561 | if (rv) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 562 | bb_perror_msg("can't release %s from %s", |
| 563 | slave_ifname, master_ifname); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 564 | res = rv; |
| 565 | } |
| 566 | } else { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 567 | /* Attach a slave interface to the master */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 568 | rv = get_if_settings(slave_ifname, &slave); |
| 569 | if (rv) { |
| 570 | /* Can't work with this slave, */ |
| 571 | /* remember the error and skip it */ |
| 572 | bb_perror_msg( |
| 573 | "skipping %s: can't get settings", |
| 574 | slave_ifname); |
| 575 | res = rv; |
| 576 | continue; |
| 577 | } |
| 578 | rv = enslave(master_ifname, slave_ifname); |
| 579 | if (rv) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 580 | bb_perror_msg("can't enslave %s to %s", |
| 581 | slave_ifname, master_ifname); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 582 | res = rv; |
| 583 | } |
| 584 | } |
| 585 | } while ((slave_ifname = *argv++) != NULL); |
| 586 | |
| 587 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 588 | close(skfd); |
| 589 | } |
| 590 | |
| 591 | return res; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 592 | } |