blob: 0b3ebf72adab89fb0c66e9c52e6fd3b2184b906c [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 */
Denys Vlasenkoecb179b2009-09-23 23:47:52 +0200104#include <linux/if.h>
Denys Vlasenko5342c3f2010-04-01 15:09:44 +0200105//#include <net/if_arp.h> - not needed?
Denis Vlasenko06aed432008-02-26 18:25:24 +0000106#include <linux/if_bonding.h>
107#include <linux/sockios.h>
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200108#include "fix_u32.h" /* hack, so we may include kernel's ethtool.h */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000109#include <linux/ethtool.h>
Denys Vlasenkoecb179b2009-09-23 23:47:52 +0200110#ifndef BOND_ABI_VERSION
111# define BOND_ABI_VERSION 2
112#endif
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200113#ifndef IFNAMSIZ
114# define IFNAMSIZ 16
115#endif
116
Denis Vlasenko06aed432008-02-26 18:25:24 +0000117
118struct dev_data {
119 struct ifreq mtu, flags, hwaddr;
120};
121
122
Denis Vlasenko08768222008-06-07 22:13:04 +0000123enum { skfd = 3 }; /* AF_INET socket for ioctl() calls. */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000124struct globals {
125 unsigned abi_ver; /* userland - kernel ABI version */
126 smallint hwaddr_set; /* Master's hwaddr is set */
127 struct dev_data master;
128 struct dev_data slave;
129};
130#define G (*ptr_to_globals)
131#define abi_ver (G.abi_ver )
132#define hwaddr_set (G.hwaddr_set)
133#define master (G.master )
134#define slave (G.slave )
135#define INIT_G() do { \
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000136 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko06aed432008-02-26 18:25:24 +0000137} while (0)
138
139
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000140/* NOINLINEs are placed where it results in smaller code (gcc 4.3.1) */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000141
Denis Vlasenko08768222008-06-07 22:13:04 +0000142static int ioctl_on_skfd(unsigned request, struct ifreq *ifr)
143{
144 return ioctl(skfd, request, ifr);
145}
146
147static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname)
148{
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000149 strncpy_IFNAMSIZ(ifr->ifr_name, ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000150 return ioctl_on_skfd(request, ifr);
151}
152
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000153static int get_if_settings(char *ifname, struct dev_data *dd)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000154{
Denis Vlasenko06aed432008-02-26 18:25:24 +0000155 int res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000156
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000157 res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname);
158 res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname);
159 res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000160
161 return res;
162}
163
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000164static int get_slave_flags(char *slave_ifname)
165{
166 return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname);
167}
168
169static int set_hwaddr(char *ifname, struct sockaddr *hwaddr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000170{
171 struct ifreq ifr;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000172
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000173 memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr));
174 return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname);
175}
176
177static int set_mtu(char *ifname, int mtu)
178{
179 struct ifreq ifr;
180
181 ifr.ifr_mtu = mtu;
182 return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname);
183}
184
185static int set_if_flags(char *ifname, int flags)
186{
187 struct ifreq ifr;
188
189 ifr.ifr_flags = flags;
190 return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname);
191}
192
193static int set_if_up(char *ifname, int flags)
194{
195 int res = set_if_flags(ifname, flags | IFF_UP);
196 if (res)
197 bb_perror_msg("%s: can't up", ifname);
198 return res;
199}
200
201static int set_if_down(char *ifname, int flags)
202{
203 int res = set_if_flags(ifname, flags & ~IFF_UP);
204 if (res)
205 bb_perror_msg("%s: can't down", ifname);
206 return res;
207}
208
209static int clear_if_addr(char *ifname)
210{
211 struct ifreq ifr;
212
213 ifr.ifr_addr.sa_family = AF_INET;
214 memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data));
215 return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname);
216}
217
218static int set_if_addr(char *master_ifname, char *slave_ifname)
219{
220#if (SIOCGIFADDR | SIOCSIFADDR \
221 | SIOCGIFDSTADDR | SIOCSIFDSTADDR \
222 | SIOCGIFBRDADDR | SIOCSIFBRDADDR \
223 | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff
224#define INT uint16_t
225#else
226#define INT int
227#endif
228 static const struct {
229 INT g_ioctl;
230 INT s_ioctl;
231 } ifra[] = {
232 { SIOCGIFADDR, SIOCSIFADDR },
233 { SIOCGIFDSTADDR, SIOCSIFDSTADDR },
234 { SIOCGIFBRDADDR, SIOCSIFBRDADDR },
235 { SIOCGIFNETMASK, SIOCSIFNETMASK },
236 };
237
238 struct ifreq ifr;
239 int res;
240 unsigned i;
241
242 for (i = 0; i < ARRAY_SIZE(ifra); i++) {
243 res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname);
244 if (res < 0) {
245 ifr.ifr_addr.sa_family = AF_INET;
246 memset(ifr.ifr_addr.sa_data, 0,
247 sizeof(ifr.ifr_addr.sa_data));
248 }
249
250 res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname);
251 if (res < 0)
252 return res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000253 }
254
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000255 return 0;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000256}
257
258static void change_active(char *master_ifname, char *slave_ifname)
259{
260 struct ifreq ifr;
261
262 if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000263 bb_error_msg_and_die("%s is not a slave", slave_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000264 }
265
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000266 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000267 if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname)
268 && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000269 ) {
270 bb_perror_msg_and_die(
271 "master %s, slave %s: can't "
272 "change active",
273 master_ifname, slave_ifname);
274 }
275}
276
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000277static NOINLINE int enslave(char *master_ifname, char *slave_ifname)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000278{
279 struct ifreq ifr;
280 int res;
281
282 if (slave.flags.ifr_flags & IFF_SLAVE) {
283 bb_error_msg(
284 "%s is already a slave",
285 slave_ifname);
286 return 1;
287 }
288
289 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
290 if (res)
291 return res;
292
293 if (abi_ver < 2) {
294 /* Older bonding versions would panic if the slave has no IP
295 * address, so get the IP setting from the master.
296 */
297 res = set_if_addr(master_ifname, slave_ifname);
298 if (res) {
299 bb_perror_msg("%s: can't set address", slave_ifname);
300 return res;
301 }
302 } else {
303 res = clear_if_addr(slave_ifname);
304 if (res) {
305 bb_perror_msg("%s: can't clear address", slave_ifname);
306 return res;
307 }
308 }
309
310 if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) {
Denis Vlasenko08768222008-06-07 22:13:04 +0000311 res = set_mtu(slave_ifname, master.mtu.ifr_mtu);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000312 if (res) {
313 bb_perror_msg("%s: can't set MTU", slave_ifname);
314 return res;
315 }
316 }
317
318 if (hwaddr_set) {
319 /* Master already has an hwaddr
320 * so set it's hwaddr to the slave
321 */
322 if (abi_ver < 1) {
323 /* The driver is using an old ABI, so
324 * the application sets the slave's
325 * hwaddr
326 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000327 if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000328 bb_perror_msg("%s: can't set hw address",
329 slave_ifname);
330 goto undo_mtu;
331 }
332
333 /* For old ABI the application needs to bring the
334 * slave back up
335 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000336 if (set_if_up(slave_ifname, slave.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000337 goto undo_slave_mac;
338 }
339 /* The driver is using a new ABI,
340 * so the driver takes care of setting
341 * the slave's hwaddr and bringing
342 * it up again
343 */
344 } else {
345 /* No hwaddr for master yet, so
346 * set the slave's hwaddr to it
347 */
348 if (abi_ver < 1) {
349 /* For old ABI, the master needs to be
350 * down before setting it's hwaddr
351 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000352 if (set_if_down(master_ifname, master.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000353 goto undo_mtu;
354 }
355
Denis Vlasenko08768222008-06-07 22:13:04 +0000356 if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000357 bb_error_msg("%s: can't set hw address",
358 master_ifname);
359 goto undo_mtu;
360 }
361
362 if (abi_ver < 1) {
363 /* For old ABI, bring the master
364 * back up
365 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000366 if (set_if_up(master_ifname, master.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000367 goto undo_master_mac;
368 }
369
370 hwaddr_set = 1;
371 }
372
373 /* Do the real thing */
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000374 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000375 if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname)
376 && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000377 ) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000378 goto undo_master_mac;
Denis Vlasenko08768222008-06-07 22:13:04 +0000379 }
Denis Vlasenko06aed432008-02-26 18:25:24 +0000380
381 return 0;
382
383/* rollback (best effort) */
384 undo_master_mac:
Denis Vlasenko08768222008-06-07 22:13:04 +0000385 set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr));
Denis Vlasenko06aed432008-02-26 18:25:24 +0000386 hwaddr_set = 0;
387 goto undo_mtu;
Denis Vlasenko08768222008-06-07 22:13:04 +0000388
Denis Vlasenko06aed432008-02-26 18:25:24 +0000389 undo_slave_mac:
Denis Vlasenko08768222008-06-07 22:13:04 +0000390 set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr));
Denis Vlasenko06aed432008-02-26 18:25:24 +0000391 undo_mtu:
Denis Vlasenko08768222008-06-07 22:13:04 +0000392 set_mtu(slave_ifname, slave.mtu.ifr_mtu);
393 return 1;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000394}
395
396static int release(char *master_ifname, char *slave_ifname)
397{
398 struct ifreq ifr;
399 int res = 0;
400
401 if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000402 bb_error_msg("%s is not a slave", slave_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000403 return 1;
404 }
405
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000406 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000407 if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0
408 && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0
Denis Vlasenko06aed432008-02-26 18:25:24 +0000409 ) {
410 return 1;
411 }
412 if (abi_ver < 1) {
413 /* The driver is using an old ABI, so we'll set the interface
414 * down to avoid any conflicts due to same MAC/IP
415 */
416 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
417 }
418
419 /* set to default mtu */
Denis Vlasenko08768222008-06-07 22:13:04 +0000420 set_mtu(slave_ifname, 1500);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000421
422 return res;
423}
424
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000425static NOINLINE void get_drv_info(char *master_ifname)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000426{
427 struct ifreq ifr;
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000428 struct ethtool_drvinfo info;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000429
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000430 memset(&ifr, 0, sizeof(ifr));
431 ifr.ifr_data = (caddr_t)&info;
432 info.cmd = ETHTOOL_GDRVINFO;
Denis Vlasenkob5975642008-06-08 00:24:43 +0000433 /* both fields are 32 bytes long (long enough) */
434 strcpy(info.driver, "ifenslave");
435 strcpy(info.fw_version, utoa(BOND_ABI_VERSION));
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000436 if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) {
437 if (errno == EOPNOTSUPP)
438 return;
439 bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000440 }
441
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000442 abi_ver = bb_strtou(info.fw_version, NULL, 0);
443 if (errno)
444 bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
445}
446
447int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000448int ifenslave_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000449{
450 char *master_ifname, *slave_ifname;
451 int rv;
452 int res;
453 unsigned opt;
454 enum {
455 OPT_c = (1 << 0),
456 OPT_d = (1 << 1),
457 OPT_f = (1 << 2),
458 };
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200459#if ENABLE_LONG_OPTS
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000460 static const char ifenslave_longopts[] ALIGN1 =
Denis Vlasenko048897c2008-06-08 08:01:12 +0000461 "change-active\0" No_argument "c"
462 "detach\0" No_argument "d"
463 "force\0" No_argument "f"
464 /* "all-interfaces\0" No_argument "a" */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000465 ;
466
467 applet_long_options = ifenslave_longopts;
468#endif
469 INIT_G();
470
Denis Vlasenko048897c2008-06-08 08:01:12 +0000471 opt = getopt32(argv, "cdfa");
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000472 argv += optind;
Denis Vlasenko048897c2008-06-08 08:01:12 +0000473 if (opt & (opt-1)) /* Only one option can be given */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000474 bb_show_usage();
475
476 master_ifname = *argv++;
477
478 /* No interface names - show all interfaces. */
479 if (!master_ifname) {
480 display_interfaces(NULL);
481 return EXIT_SUCCESS;
482 }
483
484 /* Open a basic socket */
485 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd);
486
487 /* Exchange abi version with bonding module */
488 get_drv_info(master_ifname);
489
490 slave_ifname = *argv++;
491 if (!slave_ifname) {
492 if (opt & (OPT_d|OPT_c)) {
493 /* --change or --detach, and no slaves given -
494 * show all interfaces. */
495 display_interfaces(slave_ifname /* == NULL */);
496 return 2; /* why 2? */
497 }
498 /* A single arg means show the
499 * configuration for this interface
500 */
501 display_interfaces(master_ifname);
502 return EXIT_SUCCESS;
503 }
504
Denis Vlasenko048897c2008-06-08 08:01:12 +0000505 if (get_if_settings(master_ifname, &master)) {
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000506 /* Probably a good reason not to go on */
507 bb_perror_msg_and_die("%s: can't get settings", master_ifname);
508 }
509
Denis Vlasenko048897c2008-06-08 08:01:12 +0000510 /* Check if master is indeed a master;
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000511 * if not then fail any operation
512 */
513 if (!(master.flags.ifr_flags & IFF_MASTER))
514 bb_error_msg_and_die("%s is not a master", master_ifname);
515
Denis Vlasenko048897c2008-06-08 08:01:12 +0000516 /* Check if master is up; if not then fail any operation */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000517 if (!(master.flags.ifr_flags & IFF_UP))
518 bb_error_msg_and_die("%s is not up", master_ifname);
519
Denis Vlasenko048897c2008-06-08 08:01:12 +0000520#ifdef WHY_BOTHER
521 /* Neither -c[hange] nor -d[etach] -> it's "enslave" then;
522 * and -f[orce] is not there too. Check that it's ethernet. */
523 if (!(opt & (OPT_d|OPT_c|OPT_f)) {
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000524 /* The family '1' is ARPHRD_ETHER for ethernet. */
525 if (master.hwaddr.ifr_hwaddr.sa_family != 1) {
526 bb_error_msg_and_die(
527 "%s is not ethernet-like (-f overrides)",
528 master_ifname);
529 }
530 }
Denis Vlasenko048897c2008-06-08 08:01:12 +0000531#endif
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000532
533 /* Accepts only one slave */
534 if (opt & OPT_c) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000535 /* Change active slave */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000536 if (get_slave_flags(slave_ifname)) {
537 bb_perror_msg_and_die(
538 "%s: can't get flags", slave_ifname);
539 }
540 change_active(master_ifname, slave_ifname);
541 return EXIT_SUCCESS;
542 }
543
Denis Vlasenko048897c2008-06-08 08:01:12 +0000544 /* Accepts multiple slaves */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000545 res = 0;
546 do {
547 if (opt & OPT_d) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000548 /* Detach a slave interface from the master */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000549 rv = get_slave_flags(slave_ifname);
550 if (rv) {
551 /* Can't work with this slave, */
552 /* remember the error and skip it */
553 bb_perror_msg(
554 "skipping %s: can't get flags",
555 slave_ifname);
556 res = rv;
557 continue;
558 }
559 rv = release(master_ifname, slave_ifname);
560 if (rv) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000561 bb_perror_msg("can't release %s from %s",
562 slave_ifname, master_ifname);
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000563 res = rv;
564 }
565 } else {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000566 /* Attach a slave interface to the master */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000567 rv = get_if_settings(slave_ifname, &slave);
568 if (rv) {
569 /* Can't work with this slave, */
570 /* remember the error and skip it */
571 bb_perror_msg(
572 "skipping %s: can't get settings",
573 slave_ifname);
574 res = rv;
575 continue;
576 }
577 rv = enslave(master_ifname, slave_ifname);
578 if (rv) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000579 bb_perror_msg("can't enslave %s to %s",
580 slave_ifname, master_ifname);
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000581 res = rv;
582 }
583 }
584 } while ((slave_ifname = *argv++) != NULL);
585
586 if (ENABLE_FEATURE_CLEAN_UP) {
587 close(skfd);
588 }
589
590 return res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000591}