blob: ae97457264d81dd6446386d2b37fc46512439953 [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>
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 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 */
100
101#include "libbb.h"
102
Denis Vlasenkoee9deb82008-09-06 14:52:28 +0000103/* #include <net/if.h> - no. linux/if_bonding.h pulls in linux/if.h */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000104#include <net/if_arp.h>
105#include <linux/if_bonding.h>
106#include <linux/sockios.h>
107
Denis Vlasenkoee9deb82008-09-06 14:52:28 +0000108#ifndef IFNAMSIZ
109#define IFNAMSIZ 16
110#endif
111
Denis Vlasenkoee34ebe2008-09-06 14:42:27 +0000112typedef uint64_t u64; /* hack, so we may include kernel's ethtool.h */
113typedef uint32_t u32; /* ditto */
114typedef uint16_t u16; /* ditto */
115typedef uint8_t u8; /* ditto */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000116#include <linux/ethtool.h>
117
118
119struct dev_data {
120 struct ifreq mtu, flags, hwaddr;
121};
122
123
Denis Vlasenko08768222008-06-07 22:13:04 +0000124enum { skfd = 3 }; /* AF_INET socket for ioctl() calls. */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000125struct globals {
126 unsigned abi_ver; /* userland - kernel ABI version */
127 smallint hwaddr_set; /* Master's hwaddr is set */
128 struct dev_data master;
129 struct dev_data slave;
130};
131#define G (*ptr_to_globals)
132#define abi_ver (G.abi_ver )
133#define hwaddr_set (G.hwaddr_set)
134#define master (G.master )
135#define slave (G.slave )
136#define INIT_G() do { \
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000137 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko06aed432008-02-26 18:25:24 +0000138} while (0)
139
140
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000141/* NOINLINEs are placed where it results in smaller code (gcc 4.3.1) */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000142
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000143static void strncpy_IFNAMSIZ(char *dst, const char *src)
144{
145 strncpy(dst, src, IFNAMSIZ);
146}
Denis Vlasenko06aed432008-02-26 18:25:24 +0000147
Denis Vlasenko08768222008-06-07 22:13:04 +0000148static int ioctl_on_skfd(unsigned request, struct ifreq *ifr)
149{
150 return ioctl(skfd, request, ifr);
151}
152
153static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname)
154{
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000155 strncpy_IFNAMSIZ(ifr->ifr_name, ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000156 return ioctl_on_skfd(request, ifr);
157}
158
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000159static int get_if_settings(char *ifname, struct dev_data *dd)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000160{
Denis Vlasenko06aed432008-02-26 18:25:24 +0000161 int res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000162
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000163 res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname);
164 res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname);
165 res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000166
167 return res;
168}
169
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000170static int get_slave_flags(char *slave_ifname)
171{
172 return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname);
173}
174
175static int set_hwaddr(char *ifname, struct sockaddr *hwaddr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000176{
177 struct ifreq ifr;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000178
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000179 memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr));
180 return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname);
181}
182
183static int set_mtu(char *ifname, int mtu)
184{
185 struct ifreq ifr;
186
187 ifr.ifr_mtu = mtu;
188 return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname);
189}
190
191static int set_if_flags(char *ifname, int flags)
192{
193 struct ifreq ifr;
194
195 ifr.ifr_flags = flags;
196 return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname);
197}
198
199static int set_if_up(char *ifname, int flags)
200{
201 int res = set_if_flags(ifname, flags | IFF_UP);
202 if (res)
203 bb_perror_msg("%s: can't up", ifname);
204 return res;
205}
206
207static int set_if_down(char *ifname, int flags)
208{
209 int res = set_if_flags(ifname, flags & ~IFF_UP);
210 if (res)
211 bb_perror_msg("%s: can't down", ifname);
212 return res;
213}
214
215static int clear_if_addr(char *ifname)
216{
217 struct ifreq ifr;
218
219 ifr.ifr_addr.sa_family = AF_INET;
220 memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data));
221 return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname);
222}
223
224static int set_if_addr(char *master_ifname, char *slave_ifname)
225{
226#if (SIOCGIFADDR | SIOCSIFADDR \
227 | SIOCGIFDSTADDR | SIOCSIFDSTADDR \
228 | SIOCGIFBRDADDR | SIOCSIFBRDADDR \
229 | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff
230#define INT uint16_t
231#else
232#define INT int
233#endif
234 static const struct {
235 INT g_ioctl;
236 INT s_ioctl;
237 } ifra[] = {
238 { SIOCGIFADDR, SIOCSIFADDR },
239 { SIOCGIFDSTADDR, SIOCSIFDSTADDR },
240 { SIOCGIFBRDADDR, SIOCSIFBRDADDR },
241 { SIOCGIFNETMASK, SIOCSIFNETMASK },
242 };
243
244 struct ifreq ifr;
245 int res;
246 unsigned i;
247
248 for (i = 0; i < ARRAY_SIZE(ifra); i++) {
249 res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname);
250 if (res < 0) {
251 ifr.ifr_addr.sa_family = AF_INET;
252 memset(ifr.ifr_addr.sa_data, 0,
253 sizeof(ifr.ifr_addr.sa_data));
254 }
255
256 res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname);
257 if (res < 0)
258 return res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000259 }
260
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000261 return 0;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000262}
263
264static void change_active(char *master_ifname, char *slave_ifname)
265{
266 struct ifreq ifr;
267
268 if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000269 bb_error_msg_and_die("%s is not a slave", slave_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000270 }
271
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000272 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000273 if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname)
274 && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000275 ) {
276 bb_perror_msg_and_die(
277 "master %s, slave %s: can't "
278 "change active",
279 master_ifname, slave_ifname);
280 }
281}
282
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000283static NOINLINE int enslave(char *master_ifname, char *slave_ifname)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000284{
285 struct ifreq ifr;
286 int res;
287
288 if (slave.flags.ifr_flags & IFF_SLAVE) {
289 bb_error_msg(
290 "%s is already a slave",
291 slave_ifname);
292 return 1;
293 }
294
295 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
296 if (res)
297 return res;
298
299 if (abi_ver < 2) {
300 /* Older bonding versions would panic if the slave has no IP
301 * address, so get the IP setting from the master.
302 */
303 res = set_if_addr(master_ifname, slave_ifname);
304 if (res) {
305 bb_perror_msg("%s: can't set address", slave_ifname);
306 return res;
307 }
308 } else {
309 res = clear_if_addr(slave_ifname);
310 if (res) {
311 bb_perror_msg("%s: can't clear address", slave_ifname);
312 return res;
313 }
314 }
315
316 if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) {
Denis Vlasenko08768222008-06-07 22:13:04 +0000317 res = set_mtu(slave_ifname, master.mtu.ifr_mtu);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000318 if (res) {
319 bb_perror_msg("%s: can't set MTU", slave_ifname);
320 return res;
321 }
322 }
323
324 if (hwaddr_set) {
325 /* Master already has an hwaddr
326 * so set it's hwaddr to the slave
327 */
328 if (abi_ver < 1) {
329 /* The driver is using an old ABI, so
330 * the application sets the slave's
331 * hwaddr
332 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000333 if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000334 bb_perror_msg("%s: can't set hw address",
335 slave_ifname);
336 goto undo_mtu;
337 }
338
339 /* For old ABI the application needs to bring the
340 * slave back up
341 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000342 if (set_if_up(slave_ifname, slave.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000343 goto undo_slave_mac;
344 }
345 /* The driver is using a new ABI,
346 * so the driver takes care of setting
347 * the slave's hwaddr and bringing
348 * it up again
349 */
350 } else {
351 /* No hwaddr for master yet, so
352 * set the slave's hwaddr to it
353 */
354 if (abi_ver < 1) {
355 /* For old ABI, the master needs to be
356 * down before setting it's hwaddr
357 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000358 if (set_if_down(master_ifname, master.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000359 goto undo_mtu;
360 }
361
Denis Vlasenko08768222008-06-07 22:13:04 +0000362 if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000363 bb_error_msg("%s: can't set hw address",
364 master_ifname);
365 goto undo_mtu;
366 }
367
368 if (abi_ver < 1) {
369 /* For old ABI, bring the master
370 * back up
371 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000372 if (set_if_up(master_ifname, master.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000373 goto undo_master_mac;
374 }
375
376 hwaddr_set = 1;
377 }
378
379 /* Do the real thing */
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000380 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000381 if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname)
382 && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000383 ) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000384 goto undo_master_mac;
Denis Vlasenko08768222008-06-07 22:13:04 +0000385 }
Denis Vlasenko06aed432008-02-26 18:25:24 +0000386
387 return 0;
388
389/* rollback (best effort) */
390 undo_master_mac:
Denis Vlasenko08768222008-06-07 22:13:04 +0000391 set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr));
Denis Vlasenko06aed432008-02-26 18:25:24 +0000392 hwaddr_set = 0;
393 goto undo_mtu;
Denis Vlasenko08768222008-06-07 22:13:04 +0000394
Denis Vlasenko06aed432008-02-26 18:25:24 +0000395 undo_slave_mac:
Denis Vlasenko08768222008-06-07 22:13:04 +0000396 set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr));
Denis Vlasenko06aed432008-02-26 18:25:24 +0000397 undo_mtu:
Denis Vlasenko08768222008-06-07 22:13:04 +0000398 set_mtu(slave_ifname, slave.mtu.ifr_mtu);
399 return 1;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000400}
401
402static int release(char *master_ifname, char *slave_ifname)
403{
404 struct ifreq ifr;
405 int res = 0;
406
407 if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000408 bb_error_msg("%s is not a slave", slave_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000409 return 1;
410 }
411
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000412 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000413 if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0
414 && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0
Denis Vlasenko06aed432008-02-26 18:25:24 +0000415 ) {
416 return 1;
417 }
418 if (abi_ver < 1) {
419 /* The driver is using an old ABI, so we'll set the interface
420 * down to avoid any conflicts due to same MAC/IP
421 */
422 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
423 }
424
425 /* set to default mtu */
Denis Vlasenko08768222008-06-07 22:13:04 +0000426 set_mtu(slave_ifname, 1500);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000427
428 return res;
429}
430
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000431static NOINLINE void get_drv_info(char *master_ifname)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000432{
433 struct ifreq ifr;
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000434 struct ethtool_drvinfo info;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000435
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000436 memset(&ifr, 0, sizeof(ifr));
437 ifr.ifr_data = (caddr_t)&info;
438 info.cmd = ETHTOOL_GDRVINFO;
Denis Vlasenkob5975642008-06-08 00:24:43 +0000439 /* both fields are 32 bytes long (long enough) */
440 strcpy(info.driver, "ifenslave");
441 strcpy(info.fw_version, utoa(BOND_ABI_VERSION));
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000442 if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) {
443 if (errno == EOPNOTSUPP)
444 return;
445 bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000446 }
447
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000448 abi_ver = bb_strtou(info.fw_version, NULL, 0);
449 if (errno)
450 bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
451}
452
453int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000454int ifenslave_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000455{
456 char *master_ifname, *slave_ifname;
457 int rv;
458 int res;
459 unsigned opt;
460 enum {
461 OPT_c = (1 << 0),
462 OPT_d = (1 << 1),
463 OPT_f = (1 << 2),
464 };
465#if ENABLE_GETOPT_LONG
466 static const char ifenslave_longopts[] ALIGN1 =
Denis Vlasenko048897c2008-06-08 08:01:12 +0000467 "change-active\0" No_argument "c"
468 "detach\0" No_argument "d"
469 "force\0" No_argument "f"
470 /* "all-interfaces\0" No_argument "a" */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000471 ;
472
473 applet_long_options = ifenslave_longopts;
474#endif
475 INIT_G();
476
Denis Vlasenko048897c2008-06-08 08:01:12 +0000477 opt = getopt32(argv, "cdfa");
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000478 argv += optind;
Denis Vlasenko048897c2008-06-08 08:01:12 +0000479 if (opt & (opt-1)) /* Only one option can be given */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000480 bb_show_usage();
481
482 master_ifname = *argv++;
483
484 /* No interface names - show all interfaces. */
485 if (!master_ifname) {
486 display_interfaces(NULL);
487 return EXIT_SUCCESS;
488 }
489
490 /* Open a basic socket */
491 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd);
492
493 /* Exchange abi version with bonding module */
494 get_drv_info(master_ifname);
495
496 slave_ifname = *argv++;
497 if (!slave_ifname) {
498 if (opt & (OPT_d|OPT_c)) {
499 /* --change or --detach, and no slaves given -
500 * show all interfaces. */
501 display_interfaces(slave_ifname /* == NULL */);
502 return 2; /* why 2? */
503 }
504 /* A single arg means show the
505 * configuration for this interface
506 */
507 display_interfaces(master_ifname);
508 return EXIT_SUCCESS;
509 }
510
Denis Vlasenko048897c2008-06-08 08:01:12 +0000511 if (get_if_settings(master_ifname, &master)) {
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000512 /* Probably a good reason not to go on */
513 bb_perror_msg_and_die("%s: can't get settings", master_ifname);
514 }
515
Denis Vlasenko048897c2008-06-08 08:01:12 +0000516 /* Check if master is indeed a master;
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000517 * if not then fail any operation
518 */
519 if (!(master.flags.ifr_flags & IFF_MASTER))
520 bb_error_msg_and_die("%s is not a master", master_ifname);
521
Denis Vlasenko048897c2008-06-08 08:01:12 +0000522 /* Check if master is up; if not then fail any operation */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000523 if (!(master.flags.ifr_flags & IFF_UP))
524 bb_error_msg_and_die("%s is not up", master_ifname);
525
Denis Vlasenko048897c2008-06-08 08:01:12 +0000526#ifdef WHY_BOTHER
527 /* Neither -c[hange] nor -d[etach] -> it's "enslave" then;
528 * and -f[orce] is not there too. Check that it's ethernet. */
529 if (!(opt & (OPT_d|OPT_c|OPT_f)) {
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000530 /* The family '1' is ARPHRD_ETHER for ethernet. */
531 if (master.hwaddr.ifr_hwaddr.sa_family != 1) {
532 bb_error_msg_and_die(
533 "%s is not ethernet-like (-f overrides)",
534 master_ifname);
535 }
536 }
Denis Vlasenko048897c2008-06-08 08:01:12 +0000537#endif
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000538
539 /* Accepts only one slave */
540 if (opt & OPT_c) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000541 /* Change active slave */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000542 if (get_slave_flags(slave_ifname)) {
543 bb_perror_msg_and_die(
544 "%s: can't get flags", slave_ifname);
545 }
546 change_active(master_ifname, slave_ifname);
547 return EXIT_SUCCESS;
548 }
549
Denis Vlasenko048897c2008-06-08 08:01:12 +0000550 /* Accepts multiple slaves */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000551 res = 0;
552 do {
553 if (opt & OPT_d) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000554 /* Detach a slave interface from the master */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000555 rv = get_slave_flags(slave_ifname);
556 if (rv) {
557 /* Can't work with this slave, */
558 /* remember the error and skip it */
559 bb_perror_msg(
560 "skipping %s: can't get flags",
561 slave_ifname);
562 res = rv;
563 continue;
564 }
565 rv = release(master_ifname, slave_ifname);
566 if (rv) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000567 bb_perror_msg("can't release %s from %s",
568 slave_ifname, master_ifname);
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000569 res = rv;
570 }
571 } else {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000572 /* Attach a slave interface to the master */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000573 rv = get_if_settings(slave_ifname, &slave);
574 if (rv) {
575 /* Can't work with this slave, */
576 /* remember the error and skip it */
577 bb_perror_msg(
578 "skipping %s: can't get settings",
579 slave_ifname);
580 res = rv;
581 continue;
582 }
583 rv = enslave(master_ifname, slave_ifname);
584 if (rv) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000585 bb_perror_msg("can't enslave %s to %s",
586 slave_ifname, master_ifname);
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000587 res = rv;
588 }
589 }
590 } while ((slave_ifname = *argv++) != NULL);
591
592 if (ENABLE_FEATURE_CLEAN_UP) {
593 close(skfd);
594 }
595
596 return res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000597}