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> |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 58 | * - Moved setting the slave's mac address and opening it, from |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 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 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 100 | //config:config IFENSLAVE |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 101 | //config: bool "ifenslave (13 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 102 | //config: default y |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 103 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 104 | //config: Userspace application to bind several interfaces |
| 105 | //config: to a logical interface (use with kernel bonding driver). |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 106 | |
Denys Vlasenko | a907b82 | 2017-08-07 22:31:51 +0200 | [diff] [blame] | 107 | //applet:IF_IFENSLAVE(APPLET_NOEXEC(ifenslave, ifenslave, BB_DIR_SBIN, BB_SUID_DROP, ifenslave)) |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 108 | |
| 109 | //kbuild:lib-$(CONFIG_IFENSLAVE) += ifenslave.o interface.o |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 110 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 111 | //usage:#define ifenslave_trivial_usage |
| 112 | //usage: "[-cdf] MASTER_IFACE SLAVE_IFACE..." |
| 113 | //usage:#define ifenslave_full_usage "\n\n" |
| 114 | //usage: "Configure network interfaces for parallel routing\n" |
Denys Vlasenko | 93c1a25 | 2017-08-07 22:30:59 +0200 | [diff] [blame] | 115 | //usage: "\n -c Change active slave" |
| 116 | //usage: "\n -d Remove slave interface from bonding device" |
| 117 | //usage: "\n -f Force, even if interface is not Ethernet" |
| 118 | /* //usage: "\n -r Create a receive-only slave" */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 119 | //usage: |
| 120 | //usage:#define ifenslave_example_usage |
| 121 | //usage: "To create a bond device, simply follow these three steps:\n" |
| 122 | //usage: "- ensure that the required drivers are properly loaded:\n" |
| 123 | //usage: " # modprobe bonding ; modprobe <3c59x|eepro100|pcnet32|tulip|...>\n" |
| 124 | //usage: "- assign an IP address to the bond device:\n" |
| 125 | //usage: " # ifconfig bond0 <addr> netmask <mask> broadcast <bcast>\n" |
| 126 | //usage: "- attach all the interfaces you need to the bond device:\n" |
| 127 | //usage: " # ifenslave bond0 eth0 eth1 eth2\n" |
| 128 | //usage: " If bond0 didn't have a MAC address, it will take eth0's. Then, all\n" |
| 129 | //usage: " interfaces attached AFTER this assignment will get the same MAC addr.\n\n" |
| 130 | //usage: " To detach a dead interface without setting the bond device down:\n" |
| 131 | //usage: " # ifenslave -d bond0 eth1\n\n" |
| 132 | //usage: " To set the bond device down and automatically release all the slaves:\n" |
| 133 | //usage: " # ifconfig bond0 down\n\n" |
| 134 | //usage: " To change active slave:\n" |
| 135 | //usage: " # ifenslave -c bond0 eth0\n" |
| 136 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 137 | #include "libbb.h" |
| 138 | |
Denis Vlasenko | ee9deb8 | 2008-09-06 14:52:28 +0000 | [diff] [blame] | 139 | /* #include <net/if.h> - no. linux/if_bonding.h pulls in linux/if.h */ |
Denys Vlasenko | ecb179b | 2009-09-23 23:47:52 +0200 | [diff] [blame] | 140 | #include <linux/if.h> |
Denys Vlasenko | 5342c3f | 2010-04-01 15:09:44 +0200 | [diff] [blame] | 141 | //#include <net/if_arp.h> - not needed? |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 142 | #include <linux/if_bonding.h> |
| 143 | #include <linux/sockios.h> |
Denys Vlasenko | 0568b6e | 2009-08-08 03:20:12 +0200 | [diff] [blame] | 144 | #include "fix_u32.h" /* hack, so we may include kernel's ethtool.h */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 145 | #include <linux/ethtool.h> |
Denys Vlasenko | ecb179b | 2009-09-23 23:47:52 +0200 | [diff] [blame] | 146 | #ifndef BOND_ABI_VERSION |
| 147 | # define BOND_ABI_VERSION 2 |
| 148 | #endif |
Denys Vlasenko | 0568b6e | 2009-08-08 03:20:12 +0200 | [diff] [blame] | 149 | #ifndef IFNAMSIZ |
| 150 | # define IFNAMSIZ 16 |
| 151 | #endif |
| 152 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 153 | |
| 154 | struct dev_data { |
| 155 | struct ifreq mtu, flags, hwaddr; |
| 156 | }; |
| 157 | |
| 158 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 159 | enum { skfd = 3 }; /* AF_INET socket for ioctl() calls. */ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 160 | struct globals { |
| 161 | unsigned abi_ver; /* userland - kernel ABI version */ |
| 162 | smallint hwaddr_set; /* Master's hwaddr is set */ |
| 163 | struct dev_data master; |
| 164 | struct dev_data slave; |
| 165 | }; |
| 166 | #define G (*ptr_to_globals) |
| 167 | #define abi_ver (G.abi_ver ) |
| 168 | #define hwaddr_set (G.hwaddr_set) |
| 169 | #define master (G.master ) |
| 170 | #define slave (G.slave ) |
| 171 | #define INIT_G() do { \ |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 172 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 173 | } while (0) |
| 174 | |
| 175 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 176 | /* 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] | 177 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 178 | static int ioctl_on_skfd(unsigned request, struct ifreq *ifr) |
| 179 | { |
| 180 | return ioctl(skfd, request, ifr); |
| 181 | } |
| 182 | |
| 183 | static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname) |
| 184 | { |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 185 | strncpy_IFNAMSIZ(ifr->ifr_name, ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 186 | return ioctl_on_skfd(request, ifr); |
| 187 | } |
| 188 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 189 | static int get_if_settings(char *ifname, struct dev_data *dd) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 190 | { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 191 | int res; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 193 | res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname); |
| 194 | res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname); |
| 195 | res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 196 | |
| 197 | return res; |
| 198 | } |
| 199 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 200 | static int get_slave_flags(char *slave_ifname) |
| 201 | { |
| 202 | return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname); |
| 203 | } |
| 204 | |
| 205 | static int set_hwaddr(char *ifname, struct sockaddr *hwaddr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 206 | { |
| 207 | struct ifreq ifr; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 208 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 209 | memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr)); |
| 210 | return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname); |
| 211 | } |
| 212 | |
| 213 | static int set_mtu(char *ifname, int mtu) |
| 214 | { |
| 215 | struct ifreq ifr; |
| 216 | |
| 217 | ifr.ifr_mtu = mtu; |
| 218 | return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname); |
| 219 | } |
| 220 | |
| 221 | static int set_if_flags(char *ifname, int flags) |
| 222 | { |
| 223 | struct ifreq ifr; |
| 224 | |
| 225 | ifr.ifr_flags = flags; |
| 226 | return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname); |
| 227 | } |
| 228 | |
| 229 | static int set_if_up(char *ifname, int flags) |
| 230 | { |
| 231 | int res = set_if_flags(ifname, flags | IFF_UP); |
| 232 | if (res) |
| 233 | bb_perror_msg("%s: can't up", ifname); |
| 234 | return res; |
| 235 | } |
| 236 | |
| 237 | static int set_if_down(char *ifname, int flags) |
| 238 | { |
| 239 | int res = set_if_flags(ifname, flags & ~IFF_UP); |
| 240 | if (res) |
| 241 | bb_perror_msg("%s: can't down", ifname); |
| 242 | return res; |
| 243 | } |
| 244 | |
| 245 | static int clear_if_addr(char *ifname) |
| 246 | { |
| 247 | struct ifreq ifr; |
| 248 | |
| 249 | ifr.ifr_addr.sa_family = AF_INET; |
| 250 | memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data)); |
| 251 | return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname); |
| 252 | } |
| 253 | |
| 254 | static int set_if_addr(char *master_ifname, char *slave_ifname) |
| 255 | { |
| 256 | #if (SIOCGIFADDR | SIOCSIFADDR \ |
| 257 | | SIOCGIFDSTADDR | SIOCSIFDSTADDR \ |
| 258 | | SIOCGIFBRDADDR | SIOCSIFBRDADDR \ |
| 259 | | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff |
| 260 | #define INT uint16_t |
| 261 | #else |
| 262 | #define INT int |
| 263 | #endif |
| 264 | static const struct { |
| 265 | INT g_ioctl; |
| 266 | INT s_ioctl; |
| 267 | } ifra[] = { |
| 268 | { SIOCGIFADDR, SIOCSIFADDR }, |
| 269 | { SIOCGIFDSTADDR, SIOCSIFDSTADDR }, |
| 270 | { SIOCGIFBRDADDR, SIOCSIFBRDADDR }, |
| 271 | { SIOCGIFNETMASK, SIOCSIFNETMASK }, |
| 272 | }; |
| 273 | |
| 274 | struct ifreq ifr; |
| 275 | int res; |
| 276 | unsigned i; |
| 277 | |
| 278 | for (i = 0; i < ARRAY_SIZE(ifra); i++) { |
| 279 | res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname); |
| 280 | if (res < 0) { |
| 281 | ifr.ifr_addr.sa_family = AF_INET; |
| 282 | memset(ifr.ifr_addr.sa_data, 0, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 283 | sizeof(ifr.ifr_addr.sa_data)); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname); |
| 287 | if (res < 0) |
| 288 | return res; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 291 | return 0; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void change_active(char *master_ifname, char *slave_ifname) |
| 295 | { |
| 296 | struct ifreq ifr; |
| 297 | |
| 298 | if (!(slave.flags.ifr_flags & IFF_SLAVE)) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 299 | bb_error_msg_and_die("%s is not a slave", slave_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 302 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 303 | if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname) |
| 304 | && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 305 | ) { |
| 306 | bb_perror_msg_and_die( |
| 307 | "master %s, slave %s: can't " |
| 308 | "change active", |
| 309 | master_ifname, slave_ifname); |
| 310 | } |
| 311 | } |
| 312 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 313 | static NOINLINE int enslave(char *master_ifname, char *slave_ifname) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 314 | { |
| 315 | struct ifreq ifr; |
| 316 | int res; |
| 317 | |
| 318 | if (slave.flags.ifr_flags & IFF_SLAVE) { |
| 319 | bb_error_msg( |
| 320 | "%s is already a slave", |
| 321 | slave_ifname); |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | res = set_if_down(slave_ifname, slave.flags.ifr_flags); |
| 326 | if (res) |
| 327 | return res; |
| 328 | |
| 329 | if (abi_ver < 2) { |
| 330 | /* Older bonding versions would panic if the slave has no IP |
| 331 | * address, so get the IP setting from the master. |
| 332 | */ |
| 333 | res = set_if_addr(master_ifname, slave_ifname); |
| 334 | if (res) { |
| 335 | bb_perror_msg("%s: can't set address", slave_ifname); |
| 336 | return res; |
| 337 | } |
| 338 | } else { |
| 339 | res = clear_if_addr(slave_ifname); |
| 340 | if (res) { |
| 341 | bb_perror_msg("%s: can't clear address", slave_ifname); |
| 342 | return res; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) { |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 347 | res = set_mtu(slave_ifname, master.mtu.ifr_mtu); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 348 | if (res) { |
| 349 | bb_perror_msg("%s: can't set MTU", slave_ifname); |
| 350 | return res; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (hwaddr_set) { |
| 355 | /* Master already has an hwaddr |
| 356 | * so set it's hwaddr to the slave |
| 357 | */ |
| 358 | if (abi_ver < 1) { |
| 359 | /* The driver is using an old ABI, so |
| 360 | * the application sets the slave's |
| 361 | * hwaddr |
| 362 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 363 | if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 364 | bb_perror_msg("%s: can't set hw address", |
| 365 | slave_ifname); |
| 366 | goto undo_mtu; |
| 367 | } |
| 368 | |
| 369 | /* For old ABI the application needs to bring the |
| 370 | * slave back up |
| 371 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 372 | if (set_if_up(slave_ifname, slave.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 373 | goto undo_slave_mac; |
| 374 | } |
| 375 | /* The driver is using a new ABI, |
| 376 | * so the driver takes care of setting |
| 377 | * the slave's hwaddr and bringing |
| 378 | * it up again |
| 379 | */ |
| 380 | } else { |
| 381 | /* No hwaddr for master yet, so |
| 382 | * set the slave's hwaddr to it |
| 383 | */ |
| 384 | if (abi_ver < 1) { |
| 385 | /* For old ABI, the master needs to be |
| 386 | * down before setting it's hwaddr |
| 387 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 388 | if (set_if_down(master_ifname, master.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 389 | goto undo_mtu; |
| 390 | } |
| 391 | |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 392 | if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 393 | bb_error_msg("%s: can't set hw address", |
| 394 | master_ifname); |
| 395 | goto undo_mtu; |
| 396 | } |
| 397 | |
| 398 | if (abi_ver < 1) { |
| 399 | /* For old ABI, bring the master |
| 400 | * back up |
| 401 | */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 402 | if (set_if_up(master_ifname, master.flags.ifr_flags)) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 403 | goto undo_master_mac; |
| 404 | } |
| 405 | |
| 406 | hwaddr_set = 1; |
| 407 | } |
| 408 | |
| 409 | /* Do the real thing */ |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 410 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 411 | if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname) |
| 412 | && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 413 | ) { |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 414 | goto undo_master_mac; |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 415 | } |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 416 | |
| 417 | return 0; |
| 418 | |
| 419 | /* rollback (best effort) */ |
| 420 | undo_master_mac: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 421 | set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr)); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 422 | hwaddr_set = 0; |
| 423 | goto undo_mtu; |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 424 | |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 425 | undo_slave_mac: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 426 | set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr)); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 427 | undo_mtu: |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 428 | set_mtu(slave_ifname, slave.mtu.ifr_mtu); |
| 429 | return 1; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static int release(char *master_ifname, char *slave_ifname) |
| 433 | { |
| 434 | struct ifreq ifr; |
| 435 | int res = 0; |
| 436 | |
| 437 | if (!(slave.flags.ifr_flags & IFF_SLAVE)) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 438 | bb_error_msg("%s is not a slave", slave_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 439 | return 1; |
| 440 | } |
| 441 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 442 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 443 | if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0 |
| 444 | && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0 |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 445 | ) { |
| 446 | return 1; |
| 447 | } |
| 448 | if (abi_ver < 1) { |
| 449 | /* The driver is using an old ABI, so we'll set the interface |
| 450 | * down to avoid any conflicts due to same MAC/IP |
| 451 | */ |
| 452 | res = set_if_down(slave_ifname, slave.flags.ifr_flags); |
| 453 | } |
| 454 | |
| 455 | /* set to default mtu */ |
Denis Vlasenko | 0876822 | 2008-06-07 22:13:04 +0000 | [diff] [blame] | 456 | set_mtu(slave_ifname, 1500); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 457 | |
| 458 | return res; |
| 459 | } |
| 460 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 461 | static NOINLINE void get_drv_info(char *master_ifname) |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 462 | { |
| 463 | struct ifreq ifr; |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 464 | struct ethtool_drvinfo info; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 465 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 466 | memset(&ifr, 0, sizeof(ifr)); |
| 467 | ifr.ifr_data = (caddr_t)&info; |
| 468 | info.cmd = ETHTOOL_GDRVINFO; |
Denis Vlasenko | b597564 | 2008-06-08 00:24:43 +0000 | [diff] [blame] | 469 | /* both fields are 32 bytes long (long enough) */ |
| 470 | strcpy(info.driver, "ifenslave"); |
| 471 | strcpy(info.fw_version, utoa(BOND_ABI_VERSION)); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 472 | if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) { |
| 473 | if (errno == EOPNOTSUPP) |
| 474 | return; |
| 475 | bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname); |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 478 | abi_ver = bb_strtou(info.fw_version, NULL, 0); |
| 479 | if (errno) |
| 480 | bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname); |
| 481 | } |
| 482 | |
| 483 | int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 484 | int ifenslave_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 485 | { |
| 486 | char *master_ifname, *slave_ifname; |
| 487 | int rv; |
| 488 | int res; |
| 489 | unsigned opt; |
| 490 | enum { |
| 491 | OPT_c = (1 << 0), |
| 492 | OPT_d = (1 << 1), |
| 493 | OPT_f = (1 << 2), |
| 494 | }; |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 495 | |
| 496 | INIT_G(); |
| 497 | |
| 498 | opt = getopt32long(argv, "cdfa", |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 499 | "change-active\0" No_argument "c" |
| 500 | "detach\0" No_argument "d" |
| 501 | "force\0" No_argument "f" |
| 502 | /* "all-interfaces\0" No_argument "a" */ |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 503 | ); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 504 | argv += optind; |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 505 | if (opt & (opt-1)) /* Only one option can be given */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 506 | bb_show_usage(); |
| 507 | |
| 508 | master_ifname = *argv++; |
| 509 | |
| 510 | /* No interface names - show all interfaces. */ |
| 511 | if (!master_ifname) { |
| 512 | display_interfaces(NULL); |
| 513 | return EXIT_SUCCESS; |
| 514 | } |
| 515 | |
| 516 | /* Open a basic socket */ |
| 517 | xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd); |
| 518 | |
| 519 | /* Exchange abi version with bonding module */ |
| 520 | get_drv_info(master_ifname); |
| 521 | |
| 522 | slave_ifname = *argv++; |
| 523 | if (!slave_ifname) { |
| 524 | if (opt & (OPT_d|OPT_c)) { |
| 525 | /* --change or --detach, and no slaves given - |
| 526 | * show all interfaces. */ |
| 527 | display_interfaces(slave_ifname /* == NULL */); |
| 528 | return 2; /* why 2? */ |
| 529 | } |
| 530 | /* A single arg means show the |
| 531 | * configuration for this interface |
| 532 | */ |
| 533 | display_interfaces(master_ifname); |
| 534 | return EXIT_SUCCESS; |
| 535 | } |
| 536 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 537 | if (get_if_settings(master_ifname, &master)) { |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 538 | /* Probably a good reason not to go on */ |
| 539 | bb_perror_msg_and_die("%s: can't get settings", master_ifname); |
| 540 | } |
| 541 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 542 | /* Check if master is indeed a master; |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 543 | * if not then fail any operation |
| 544 | */ |
| 545 | if (!(master.flags.ifr_flags & IFF_MASTER)) |
| 546 | bb_error_msg_and_die("%s is not a master", master_ifname); |
| 547 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 548 | /* Check if master is up; if not then fail any operation */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 549 | if (!(master.flags.ifr_flags & IFF_UP)) |
| 550 | bb_error_msg_and_die("%s is not up", master_ifname); |
| 551 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 552 | #ifdef WHY_BOTHER |
| 553 | /* Neither -c[hange] nor -d[etach] -> it's "enslave" then; |
| 554 | * and -f[orce] is not there too. Check that it's ethernet. */ |
Mike Frysinger | a38f9fa | 2012-12-26 17:12:26 -0500 | [diff] [blame] | 555 | if (!(opt & (OPT_d|OPT_c|OPT_f))) { |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 556 | /* The family '1' is ARPHRD_ETHER for ethernet. */ |
| 557 | if (master.hwaddr.ifr_hwaddr.sa_family != 1) { |
| 558 | bb_error_msg_and_die( |
| 559 | "%s is not ethernet-like (-f overrides)", |
| 560 | master_ifname); |
| 561 | } |
| 562 | } |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 563 | #endif |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 564 | |
| 565 | /* Accepts only one slave */ |
| 566 | if (opt & OPT_c) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 567 | /* Change active slave */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 568 | if (get_slave_flags(slave_ifname)) { |
| 569 | bb_perror_msg_and_die( |
| 570 | "%s: can't get flags", slave_ifname); |
| 571 | } |
| 572 | change_active(master_ifname, slave_ifname); |
| 573 | return EXIT_SUCCESS; |
| 574 | } |
| 575 | |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 576 | /* Accepts multiple slaves */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 577 | res = 0; |
| 578 | do { |
| 579 | if (opt & OPT_d) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 580 | /* Detach a slave interface from the master */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 581 | rv = get_slave_flags(slave_ifname); |
| 582 | if (rv) { |
| 583 | /* Can't work with this slave, */ |
| 584 | /* remember the error and skip it */ |
| 585 | bb_perror_msg( |
Denys Vlasenko | 7f3a2a2 | 2015-10-08 11:24:44 +0200 | [diff] [blame] | 586 | "skipping %s: can't get %s", |
| 587 | slave_ifname, "flags"); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 588 | res = rv; |
| 589 | continue; |
| 590 | } |
| 591 | rv = release(master_ifname, slave_ifname); |
| 592 | if (rv) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 593 | bb_perror_msg("can't release %s from %s", |
| 594 | slave_ifname, master_ifname); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 595 | res = rv; |
| 596 | } |
| 597 | } else { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 598 | /* Attach a slave interface to the master */ |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 599 | rv = get_if_settings(slave_ifname, &slave); |
| 600 | if (rv) { |
| 601 | /* Can't work with this slave, */ |
| 602 | /* remember the error and skip it */ |
| 603 | bb_perror_msg( |
Denys Vlasenko | 7f3a2a2 | 2015-10-08 11:24:44 +0200 | [diff] [blame] | 604 | "skipping %s: can't get %s", |
| 605 | slave_ifname, "settings"); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 606 | res = rv; |
| 607 | continue; |
| 608 | } |
| 609 | rv = enslave(master_ifname, slave_ifname); |
| 610 | if (rv) { |
Denis Vlasenko | 048897c | 2008-06-08 08:01:12 +0000 | [diff] [blame] | 611 | bb_perror_msg("can't enslave %s to %s", |
| 612 | slave_ifname, master_ifname); |
Denis Vlasenko | c8fa04f | 2008-06-07 22:14:25 +0000 | [diff] [blame] | 613 | res = rv; |
| 614 | } |
| 615 | } |
| 616 | } while ((slave_ifname = *argv++) != NULL); |
| 617 | |
| 618 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 619 | close(skfd); |
| 620 | } |
| 621 | |
| 622 | return res; |
Denis Vlasenko | 06aed43 | 2008-02-26 18:25:24 +0000 | [diff] [blame] | 623 | } |