blob: 26e5e8cf3a8304f3d91810c1026b819a38c056bb [file] [log] [blame]
Denis Vlasenko06aed432008-02-26 18:25:24 +00001/* 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 Vlasenkoc6938402008-03-24 02:18:03 +00008 * This program controls the Linux implementation of running multiple
9 * network interfaces in parallel.
Denis Vlasenko06aed432008-02-26 18:25:24 +000010 *
Denis Vlasenkoc6938402008-03-24 02:18:03 +000011 * Author: Donald Becker <becker@cesdis.gsfc.nasa.gov>
12 * Copyright 1994-1996 Donald Becker
Denis Vlasenko06aed432008-02-26 18:25:24 +000013 *
Denis Vlasenkoc6938402008-03-24 02:18:03 +000014 * 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 Vlasenko06aed432008-02-26 18:25:24 +000017 *
Denis Vlasenkoc6938402008-03-24 02:18:03 +000018 * 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 Vlasenko06aed432008-02-26 18:25:24 +000021 *
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 Vlasenko10ad6222017-04-17 16:13:32 +020058 * - Moved setting the slave's mac address and opening it, from
Denis Vlasenko06aed432008-02-26 18:25:24 +000059 * 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 Vlasenkoc6938402008-03-24 02:18:03 +000082 * - ifenslave -c was broken; it's now fixed
83 * - Fixed problem with routes vanishing from master during enslave
84 * processing.
Denis Vlasenko06aed432008-02-26 18:25:24 +000085 *
86 * - 2003/05/27 - Amir Noam <amir.noam at intel dot com>
Denis Vlasenkoc6938402008-03-24 02:18:03 +000087 * - 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 Vlasenko06aed432008-02-26 18:25:24 +000095 *
96 * - 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
Denis Vlasenkoc6938402008-03-24 02:18:03 +000097 * - Code cleanup and style changes
98 * set version to 1.1.0
Denis Vlasenko06aed432008-02-26 18:25:24 +000099 */
Denys Vlasenko47367e12016-11-23 09:05:14 +0100100//config:config IFENSLAVE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +0200101//config: bool "ifenslave (13 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +0100102//config: default y
103//config: select PLATFORM_LINUX
104//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200105//config: Userspace application to bind several interfaces
106//config: to a logical interface (use with kernel bonding driver).
Denys Vlasenko47367e12016-11-23 09:05:14 +0100107
108//applet:IF_IFENSLAVE(APPLET(ifenslave, BB_DIR_SBIN, BB_SUID_DROP))
109
110//kbuild:lib-$(CONFIG_IFENSLAVE) += ifenslave.o interface.o
Denis Vlasenko06aed432008-02-26 18:25:24 +0000111
Pere Orga5bc8c002011-04-11 03:29:49 +0200112//usage:#define ifenslave_trivial_usage
113//usage: "[-cdf] MASTER_IFACE SLAVE_IFACE..."
114//usage:#define ifenslave_full_usage "\n\n"
115//usage: "Configure network interfaces for parallel routing\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200116//usage: "\n -c,--change-active Change active slave"
117//usage: "\n -d,--detach Remove slave interface from bonding device"
118//usage: "\n -f,--force Force, even if interface is not Ethernet"
119/* //usage: "\n -r,--receive-slave Create a receive-only slave" */
120//usage:
121//usage:#define ifenslave_example_usage
122//usage: "To create a bond device, simply follow these three steps:\n"
123//usage: "- ensure that the required drivers are properly loaded:\n"
124//usage: " # modprobe bonding ; modprobe <3c59x|eepro100|pcnet32|tulip|...>\n"
125//usage: "- assign an IP address to the bond device:\n"
126//usage: " # ifconfig bond0 <addr> netmask <mask> broadcast <bcast>\n"
127//usage: "- attach all the interfaces you need to the bond device:\n"
128//usage: " # ifenslave bond0 eth0 eth1 eth2\n"
129//usage: " If bond0 didn't have a MAC address, it will take eth0's. Then, all\n"
130//usage: " interfaces attached AFTER this assignment will get the same MAC addr.\n\n"
131//usage: " To detach a dead interface without setting the bond device down:\n"
132//usage: " # ifenslave -d bond0 eth1\n\n"
133//usage: " To set the bond device down and automatically release all the slaves:\n"
134//usage: " # ifconfig bond0 down\n\n"
135//usage: " To change active slave:\n"
136//usage: " # ifenslave -c bond0 eth0\n"
137
Denis Vlasenko06aed432008-02-26 18:25:24 +0000138#include "libbb.h"
139
Denis Vlasenkoee9deb82008-09-06 14:52:28 +0000140/* #include <net/if.h> - no. linux/if_bonding.h pulls in linux/if.h */
Denys Vlasenkoecb179b2009-09-23 23:47:52 +0200141#include <linux/if.h>
Denys Vlasenko5342c3f2010-04-01 15:09:44 +0200142//#include <net/if_arp.h> - not needed?
Denis Vlasenko06aed432008-02-26 18:25:24 +0000143#include <linux/if_bonding.h>
144#include <linux/sockios.h>
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200145#include "fix_u32.h" /* hack, so we may include kernel's ethtool.h */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000146#include <linux/ethtool.h>
Denys Vlasenkoecb179b2009-09-23 23:47:52 +0200147#ifndef BOND_ABI_VERSION
148# define BOND_ABI_VERSION 2
149#endif
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200150#ifndef IFNAMSIZ
151# define IFNAMSIZ 16
152#endif
153
Denis Vlasenko06aed432008-02-26 18:25:24 +0000154
155struct dev_data {
156 struct ifreq mtu, flags, hwaddr;
157};
158
159
Denis Vlasenko08768222008-06-07 22:13:04 +0000160enum { skfd = 3 }; /* AF_INET socket for ioctl() calls. */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000161struct globals {
162 unsigned abi_ver; /* userland - kernel ABI version */
163 smallint hwaddr_set; /* Master's hwaddr is set */
164 struct dev_data master;
165 struct dev_data slave;
166};
167#define G (*ptr_to_globals)
168#define abi_ver (G.abi_ver )
169#define hwaddr_set (G.hwaddr_set)
170#define master (G.master )
171#define slave (G.slave )
172#define INIT_G() do { \
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000173 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko06aed432008-02-26 18:25:24 +0000174} while (0)
175
176
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000177/* NOINLINEs are placed where it results in smaller code (gcc 4.3.1) */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000178
Denis Vlasenko08768222008-06-07 22:13:04 +0000179static int ioctl_on_skfd(unsigned request, struct ifreq *ifr)
180{
181 return ioctl(skfd, request, ifr);
182}
183
184static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname)
185{
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000186 strncpy_IFNAMSIZ(ifr->ifr_name, ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000187 return ioctl_on_skfd(request, ifr);
188}
189
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000190static int get_if_settings(char *ifname, struct dev_data *dd)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000191{
Denis Vlasenko06aed432008-02-26 18:25:24 +0000192 int res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000193
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000194 res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname);
195 res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname);
196 res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000197
198 return res;
199}
200
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000201static int get_slave_flags(char *slave_ifname)
202{
203 return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname);
204}
205
206static int set_hwaddr(char *ifname, struct sockaddr *hwaddr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000207{
208 struct ifreq ifr;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000209
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000210 memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr));
211 return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname);
212}
213
214static int set_mtu(char *ifname, int mtu)
215{
216 struct ifreq ifr;
217
218 ifr.ifr_mtu = mtu;
219 return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname);
220}
221
222static int set_if_flags(char *ifname, int flags)
223{
224 struct ifreq ifr;
225
226 ifr.ifr_flags = flags;
227 return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname);
228}
229
230static int set_if_up(char *ifname, int flags)
231{
232 int res = set_if_flags(ifname, flags | IFF_UP);
233 if (res)
234 bb_perror_msg("%s: can't up", ifname);
235 return res;
236}
237
238static int set_if_down(char *ifname, int flags)
239{
240 int res = set_if_flags(ifname, flags & ~IFF_UP);
241 if (res)
242 bb_perror_msg("%s: can't down", ifname);
243 return res;
244}
245
246static int clear_if_addr(char *ifname)
247{
248 struct ifreq ifr;
249
250 ifr.ifr_addr.sa_family = AF_INET;
251 memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data));
252 return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname);
253}
254
255static int set_if_addr(char *master_ifname, char *slave_ifname)
256{
257#if (SIOCGIFADDR | SIOCSIFADDR \
258 | SIOCGIFDSTADDR | SIOCSIFDSTADDR \
259 | SIOCGIFBRDADDR | SIOCSIFBRDADDR \
260 | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff
261#define INT uint16_t
262#else
263#define INT int
264#endif
265 static const struct {
266 INT g_ioctl;
267 INT s_ioctl;
268 } ifra[] = {
269 { SIOCGIFADDR, SIOCSIFADDR },
270 { SIOCGIFDSTADDR, SIOCSIFDSTADDR },
271 { SIOCGIFBRDADDR, SIOCSIFBRDADDR },
272 { SIOCGIFNETMASK, SIOCSIFNETMASK },
273 };
274
275 struct ifreq ifr;
276 int res;
277 unsigned i;
278
279 for (i = 0; i < ARRAY_SIZE(ifra); i++) {
280 res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname);
281 if (res < 0) {
282 ifr.ifr_addr.sa_family = AF_INET;
283 memset(ifr.ifr_addr.sa_data, 0,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100284 sizeof(ifr.ifr_addr.sa_data));
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000285 }
286
287 res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname);
288 if (res < 0)
289 return res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000290 }
291
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000292 return 0;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000293}
294
295static void change_active(char *master_ifname, char *slave_ifname)
296{
297 struct ifreq ifr;
298
299 if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000300 bb_error_msg_and_die("%s is not a slave", slave_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000301 }
302
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000303 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000304 if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname)
305 && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000306 ) {
307 bb_perror_msg_and_die(
308 "master %s, slave %s: can't "
309 "change active",
310 master_ifname, slave_ifname);
311 }
312}
313
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000314static NOINLINE int enslave(char *master_ifname, char *slave_ifname)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000315{
316 struct ifreq ifr;
317 int res;
318
319 if (slave.flags.ifr_flags & IFF_SLAVE) {
320 bb_error_msg(
321 "%s is already a slave",
322 slave_ifname);
323 return 1;
324 }
325
326 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
327 if (res)
328 return res;
329
330 if (abi_ver < 2) {
331 /* Older bonding versions would panic if the slave has no IP
332 * address, so get the IP setting from the master.
333 */
334 res = set_if_addr(master_ifname, slave_ifname);
335 if (res) {
336 bb_perror_msg("%s: can't set address", slave_ifname);
337 return res;
338 }
339 } else {
340 res = clear_if_addr(slave_ifname);
341 if (res) {
342 bb_perror_msg("%s: can't clear address", slave_ifname);
343 return res;
344 }
345 }
346
347 if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) {
Denis Vlasenko08768222008-06-07 22:13:04 +0000348 res = set_mtu(slave_ifname, master.mtu.ifr_mtu);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000349 if (res) {
350 bb_perror_msg("%s: can't set MTU", slave_ifname);
351 return res;
352 }
353 }
354
355 if (hwaddr_set) {
356 /* Master already has an hwaddr
357 * so set it's hwaddr to the slave
358 */
359 if (abi_ver < 1) {
360 /* The driver is using an old ABI, so
361 * the application sets the slave's
362 * hwaddr
363 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000364 if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000365 bb_perror_msg("%s: can't set hw address",
366 slave_ifname);
367 goto undo_mtu;
368 }
369
370 /* For old ABI the application needs to bring the
371 * slave back up
372 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000373 if (set_if_up(slave_ifname, slave.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000374 goto undo_slave_mac;
375 }
376 /* The driver is using a new ABI,
377 * so the driver takes care of setting
378 * the slave's hwaddr and bringing
379 * it up again
380 */
381 } else {
382 /* No hwaddr for master yet, so
383 * set the slave's hwaddr to it
384 */
385 if (abi_ver < 1) {
386 /* For old ABI, the master needs to be
387 * down before setting it's hwaddr
388 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000389 if (set_if_down(master_ifname, master.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000390 goto undo_mtu;
391 }
392
Denis Vlasenko08768222008-06-07 22:13:04 +0000393 if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000394 bb_error_msg("%s: can't set hw address",
395 master_ifname);
396 goto undo_mtu;
397 }
398
399 if (abi_ver < 1) {
400 /* For old ABI, bring the master
401 * back up
402 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000403 if (set_if_up(master_ifname, master.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000404 goto undo_master_mac;
405 }
406
407 hwaddr_set = 1;
408 }
409
410 /* Do the real thing */
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000411 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000412 if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname)
413 && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000414 ) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000415 goto undo_master_mac;
Denis Vlasenko08768222008-06-07 22:13:04 +0000416 }
Denis Vlasenko06aed432008-02-26 18:25:24 +0000417
418 return 0;
419
420/* rollback (best effort) */
421 undo_master_mac:
Denis Vlasenko08768222008-06-07 22:13:04 +0000422 set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr));
Denis Vlasenko06aed432008-02-26 18:25:24 +0000423 hwaddr_set = 0;
424 goto undo_mtu;
Denis Vlasenko08768222008-06-07 22:13:04 +0000425
Denis Vlasenko06aed432008-02-26 18:25:24 +0000426 undo_slave_mac:
Denis Vlasenko08768222008-06-07 22:13:04 +0000427 set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr));
Denis Vlasenko06aed432008-02-26 18:25:24 +0000428 undo_mtu:
Denis Vlasenko08768222008-06-07 22:13:04 +0000429 set_mtu(slave_ifname, slave.mtu.ifr_mtu);
430 return 1;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000431}
432
433static int release(char *master_ifname, char *slave_ifname)
434{
435 struct ifreq ifr;
436 int res = 0;
437
438 if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000439 bb_error_msg("%s is not a slave", slave_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000440 return 1;
441 }
442
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000443 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000444 if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0
445 && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0
Denis Vlasenko06aed432008-02-26 18:25:24 +0000446 ) {
447 return 1;
448 }
449 if (abi_ver < 1) {
450 /* The driver is using an old ABI, so we'll set the interface
451 * down to avoid any conflicts due to same MAC/IP
452 */
453 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
454 }
455
456 /* set to default mtu */
Denis Vlasenko08768222008-06-07 22:13:04 +0000457 set_mtu(slave_ifname, 1500);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000458
459 return res;
460}
461
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000462static NOINLINE void get_drv_info(char *master_ifname)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000463{
464 struct ifreq ifr;
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000465 struct ethtool_drvinfo info;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000466
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000467 memset(&ifr, 0, sizeof(ifr));
468 ifr.ifr_data = (caddr_t)&info;
469 info.cmd = ETHTOOL_GDRVINFO;
Denis Vlasenkob5975642008-06-08 00:24:43 +0000470 /* both fields are 32 bytes long (long enough) */
471 strcpy(info.driver, "ifenslave");
472 strcpy(info.fw_version, utoa(BOND_ABI_VERSION));
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000473 if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) {
474 if (errno == EOPNOTSUPP)
475 return;
476 bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000477 }
478
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000479 abi_ver = bb_strtou(info.fw_version, NULL, 0);
480 if (errno)
481 bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
482}
483
484int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000485int ifenslave_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000486{
487 char *master_ifname, *slave_ifname;
488 int rv;
489 int res;
490 unsigned opt;
491 enum {
492 OPT_c = (1 << 0),
493 OPT_d = (1 << 1),
494 OPT_f = (1 << 2),
495 };
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200496#if ENABLE_LONG_OPTS
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000497 static const char ifenslave_longopts[] ALIGN1 =
Denis Vlasenko048897c2008-06-08 08:01:12 +0000498 "change-active\0" No_argument "c"
499 "detach\0" No_argument "d"
500 "force\0" No_argument "f"
501 /* "all-interfaces\0" No_argument "a" */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000502 ;
503
504 applet_long_options = ifenslave_longopts;
505#endif
506 INIT_G();
507
Denis Vlasenko048897c2008-06-08 08:01:12 +0000508 opt = getopt32(argv, "cdfa");
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000509 argv += optind;
Denis Vlasenko048897c2008-06-08 08:01:12 +0000510 if (opt & (opt-1)) /* Only one option can be given */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000511 bb_show_usage();
512
513 master_ifname = *argv++;
514
515 /* No interface names - show all interfaces. */
516 if (!master_ifname) {
517 display_interfaces(NULL);
518 return EXIT_SUCCESS;
519 }
520
521 /* Open a basic socket */
522 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd);
523
524 /* Exchange abi version with bonding module */
525 get_drv_info(master_ifname);
526
527 slave_ifname = *argv++;
528 if (!slave_ifname) {
529 if (opt & (OPT_d|OPT_c)) {
530 /* --change or --detach, and no slaves given -
531 * show all interfaces. */
532 display_interfaces(slave_ifname /* == NULL */);
533 return 2; /* why 2? */
534 }
535 /* A single arg means show the
536 * configuration for this interface
537 */
538 display_interfaces(master_ifname);
539 return EXIT_SUCCESS;
540 }
541
Denis Vlasenko048897c2008-06-08 08:01:12 +0000542 if (get_if_settings(master_ifname, &master)) {
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000543 /* Probably a good reason not to go on */
544 bb_perror_msg_and_die("%s: can't get settings", master_ifname);
545 }
546
Denis Vlasenko048897c2008-06-08 08:01:12 +0000547 /* Check if master is indeed a master;
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000548 * if not then fail any operation
549 */
550 if (!(master.flags.ifr_flags & IFF_MASTER))
551 bb_error_msg_and_die("%s is not a master", master_ifname);
552
Denis Vlasenko048897c2008-06-08 08:01:12 +0000553 /* Check if master is up; if not then fail any operation */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000554 if (!(master.flags.ifr_flags & IFF_UP))
555 bb_error_msg_and_die("%s is not up", master_ifname);
556
Denis Vlasenko048897c2008-06-08 08:01:12 +0000557#ifdef WHY_BOTHER
558 /* Neither -c[hange] nor -d[etach] -> it's "enslave" then;
559 * and -f[orce] is not there too. Check that it's ethernet. */
Mike Frysingera38f9fa2012-12-26 17:12:26 -0500560 if (!(opt & (OPT_d|OPT_c|OPT_f))) {
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000561 /* The family '1' is ARPHRD_ETHER for ethernet. */
562 if (master.hwaddr.ifr_hwaddr.sa_family != 1) {
563 bb_error_msg_and_die(
564 "%s is not ethernet-like (-f overrides)",
565 master_ifname);
566 }
567 }
Denis Vlasenko048897c2008-06-08 08:01:12 +0000568#endif
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000569
570 /* Accepts only one slave */
571 if (opt & OPT_c) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000572 /* Change active slave */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000573 if (get_slave_flags(slave_ifname)) {
574 bb_perror_msg_and_die(
575 "%s: can't get flags", slave_ifname);
576 }
577 change_active(master_ifname, slave_ifname);
578 return EXIT_SUCCESS;
579 }
580
Denis Vlasenko048897c2008-06-08 08:01:12 +0000581 /* Accepts multiple slaves */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000582 res = 0;
583 do {
584 if (opt & OPT_d) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000585 /* Detach a slave interface from the master */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000586 rv = get_slave_flags(slave_ifname);
587 if (rv) {
588 /* Can't work with this slave, */
589 /* remember the error and skip it */
590 bb_perror_msg(
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200591 "skipping %s: can't get %s",
592 slave_ifname, "flags");
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000593 res = rv;
594 continue;
595 }
596 rv = release(master_ifname, slave_ifname);
597 if (rv) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000598 bb_perror_msg("can't release %s from %s",
599 slave_ifname, master_ifname);
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000600 res = rv;
601 }
602 } else {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000603 /* Attach a slave interface to the master */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000604 rv = get_if_settings(slave_ifname, &slave);
605 if (rv) {
606 /* Can't work with this slave, */
607 /* remember the error and skip it */
608 bb_perror_msg(
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200609 "skipping %s: can't get %s",
610 slave_ifname, "settings");
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000611 res = rv;
612 continue;
613 }
614 rv = enslave(master_ifname, slave_ifname);
615 if (rv) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000616 bb_perror_msg("can't enslave %s to %s",
617 slave_ifname, master_ifname);
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000618 res = rv;
619 }
620 }
621 } while ((slave_ifname = *argv++) != NULL);
622
623 if (ENABLE_FEATURE_CLEAN_UP) {
624 close(skfd);
625 }
626
627 return res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000628}