blob: 208623e7deb5add73f6bb210b50e0dcfe201c6e3 [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
Pere Orga5bc8c002011-04-11 03:29:49 +0200101//usage:#define ifenslave_trivial_usage
102//usage: "[-cdf] MASTER_IFACE SLAVE_IFACE..."
103//usage:#define ifenslave_full_usage "\n\n"
104//usage: "Configure network interfaces for parallel routing\n"
105//usage: "\nOptions:"
106//usage: "\n -c,--change-active Change active slave"
107//usage: "\n -d,--detach Remove slave interface from bonding device"
108//usage: "\n -f,--force Force, even if interface is not Ethernet"
109/* //usage: "\n -r,--receive-slave Create a receive-only slave" */
110//usage:
111//usage:#define ifenslave_example_usage
112//usage: "To create a bond device, simply follow these three steps:\n"
113//usage: "- ensure that the required drivers are properly loaded:\n"
114//usage: " # modprobe bonding ; modprobe <3c59x|eepro100|pcnet32|tulip|...>\n"
115//usage: "- assign an IP address to the bond device:\n"
116//usage: " # ifconfig bond0 <addr> netmask <mask> broadcast <bcast>\n"
117//usage: "- attach all the interfaces you need to the bond device:\n"
118//usage: " # ifenslave bond0 eth0 eth1 eth2\n"
119//usage: " If bond0 didn't have a MAC address, it will take eth0's. Then, all\n"
120//usage: " interfaces attached AFTER this assignment will get the same MAC addr.\n\n"
121//usage: " To detach a dead interface without setting the bond device down:\n"
122//usage: " # ifenslave -d bond0 eth1\n\n"
123//usage: " To set the bond device down and automatically release all the slaves:\n"
124//usage: " # ifconfig bond0 down\n\n"
125//usage: " To change active slave:\n"
126//usage: " # ifenslave -c bond0 eth0\n"
127
Denis Vlasenko06aed432008-02-26 18:25:24 +0000128#include "libbb.h"
129
Denis Vlasenkoee9deb82008-09-06 14:52:28 +0000130/* #include <net/if.h> - no. linux/if_bonding.h pulls in linux/if.h */
Denys Vlasenkoecb179b2009-09-23 23:47:52 +0200131#include <linux/if.h>
Denys Vlasenko5342c3f2010-04-01 15:09:44 +0200132//#include <net/if_arp.h> - not needed?
Denis Vlasenko06aed432008-02-26 18:25:24 +0000133#include <linux/if_bonding.h>
134#include <linux/sockios.h>
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200135#include "fix_u32.h" /* hack, so we may include kernel's ethtool.h */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000136#include <linux/ethtool.h>
Denys Vlasenkoecb179b2009-09-23 23:47:52 +0200137#ifndef BOND_ABI_VERSION
138# define BOND_ABI_VERSION 2
139#endif
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200140#ifndef IFNAMSIZ
141# define IFNAMSIZ 16
142#endif
143
Denis Vlasenko06aed432008-02-26 18:25:24 +0000144
145struct dev_data {
146 struct ifreq mtu, flags, hwaddr;
147};
148
149
Denis Vlasenko08768222008-06-07 22:13:04 +0000150enum { skfd = 3 }; /* AF_INET socket for ioctl() calls. */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000151struct globals {
152 unsigned abi_ver; /* userland - kernel ABI version */
153 smallint hwaddr_set; /* Master's hwaddr is set */
154 struct dev_data master;
155 struct dev_data slave;
156};
157#define G (*ptr_to_globals)
158#define abi_ver (G.abi_ver )
159#define hwaddr_set (G.hwaddr_set)
160#define master (G.master )
161#define slave (G.slave )
162#define INIT_G() do { \
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000163 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko06aed432008-02-26 18:25:24 +0000164} while (0)
165
166
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000167/* NOINLINEs are placed where it results in smaller code (gcc 4.3.1) */
Denis Vlasenko06aed432008-02-26 18:25:24 +0000168
Denis Vlasenko08768222008-06-07 22:13:04 +0000169static int ioctl_on_skfd(unsigned request, struct ifreq *ifr)
170{
171 return ioctl(skfd, request, ifr);
172}
173
174static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname)
175{
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000176 strncpy_IFNAMSIZ(ifr->ifr_name, ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000177 return ioctl_on_skfd(request, ifr);
178}
179
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000180static int get_if_settings(char *ifname, struct dev_data *dd)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000181{
Denis Vlasenko06aed432008-02-26 18:25:24 +0000182 int res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000183
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000184 res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname);
185 res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname);
186 res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000187
188 return res;
189}
190
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000191static int get_slave_flags(char *slave_ifname)
192{
193 return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname);
194}
195
196static int set_hwaddr(char *ifname, struct sockaddr *hwaddr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000197{
198 struct ifreq ifr;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000199
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000200 memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr));
201 return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname);
202}
203
204static int set_mtu(char *ifname, int mtu)
205{
206 struct ifreq ifr;
207
208 ifr.ifr_mtu = mtu;
209 return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname);
210}
211
212static int set_if_flags(char *ifname, int flags)
213{
214 struct ifreq ifr;
215
216 ifr.ifr_flags = flags;
217 return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname);
218}
219
220static int set_if_up(char *ifname, int flags)
221{
222 int res = set_if_flags(ifname, flags | IFF_UP);
223 if (res)
224 bb_perror_msg("%s: can't up", ifname);
225 return res;
226}
227
228static int set_if_down(char *ifname, int flags)
229{
230 int res = set_if_flags(ifname, flags & ~IFF_UP);
231 if (res)
232 bb_perror_msg("%s: can't down", ifname);
233 return res;
234}
235
236static int clear_if_addr(char *ifname)
237{
238 struct ifreq ifr;
239
240 ifr.ifr_addr.sa_family = AF_INET;
241 memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data));
242 return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname);
243}
244
245static int set_if_addr(char *master_ifname, char *slave_ifname)
246{
247#if (SIOCGIFADDR | SIOCSIFADDR \
248 | SIOCGIFDSTADDR | SIOCSIFDSTADDR \
249 | SIOCGIFBRDADDR | SIOCSIFBRDADDR \
250 | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff
251#define INT uint16_t
252#else
253#define INT int
254#endif
255 static const struct {
256 INT g_ioctl;
257 INT s_ioctl;
258 } ifra[] = {
259 { SIOCGIFADDR, SIOCSIFADDR },
260 { SIOCGIFDSTADDR, SIOCSIFDSTADDR },
261 { SIOCGIFBRDADDR, SIOCSIFBRDADDR },
262 { SIOCGIFNETMASK, SIOCSIFNETMASK },
263 };
264
265 struct ifreq ifr;
266 int res;
267 unsigned i;
268
269 for (i = 0; i < ARRAY_SIZE(ifra); i++) {
270 res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname);
271 if (res < 0) {
272 ifr.ifr_addr.sa_family = AF_INET;
273 memset(ifr.ifr_addr.sa_data, 0,
274 sizeof(ifr.ifr_addr.sa_data));
275 }
276
277 res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname);
278 if (res < 0)
279 return res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000280 }
281
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000282 return 0;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000283}
284
285static void change_active(char *master_ifname, char *slave_ifname)
286{
287 struct ifreq ifr;
288
289 if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000290 bb_error_msg_and_die("%s is not a slave", slave_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000291 }
292
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000293 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000294 if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname)
295 && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000296 ) {
297 bb_perror_msg_and_die(
298 "master %s, slave %s: can't "
299 "change active",
300 master_ifname, slave_ifname);
301 }
302}
303
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000304static NOINLINE int enslave(char *master_ifname, char *slave_ifname)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000305{
306 struct ifreq ifr;
307 int res;
308
309 if (slave.flags.ifr_flags & IFF_SLAVE) {
310 bb_error_msg(
311 "%s is already a slave",
312 slave_ifname);
313 return 1;
314 }
315
316 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
317 if (res)
318 return res;
319
320 if (abi_ver < 2) {
321 /* Older bonding versions would panic if the slave has no IP
322 * address, so get the IP setting from the master.
323 */
324 res = set_if_addr(master_ifname, slave_ifname);
325 if (res) {
326 bb_perror_msg("%s: can't set address", slave_ifname);
327 return res;
328 }
329 } else {
330 res = clear_if_addr(slave_ifname);
331 if (res) {
332 bb_perror_msg("%s: can't clear address", slave_ifname);
333 return res;
334 }
335 }
336
337 if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) {
Denis Vlasenko08768222008-06-07 22:13:04 +0000338 res = set_mtu(slave_ifname, master.mtu.ifr_mtu);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000339 if (res) {
340 bb_perror_msg("%s: can't set MTU", slave_ifname);
341 return res;
342 }
343 }
344
345 if (hwaddr_set) {
346 /* Master already has an hwaddr
347 * so set it's hwaddr to the slave
348 */
349 if (abi_ver < 1) {
350 /* The driver is using an old ABI, so
351 * the application sets the slave's
352 * hwaddr
353 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000354 if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000355 bb_perror_msg("%s: can't set hw address",
356 slave_ifname);
357 goto undo_mtu;
358 }
359
360 /* For old ABI the application needs to bring the
361 * slave back up
362 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000363 if (set_if_up(slave_ifname, slave.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000364 goto undo_slave_mac;
365 }
366 /* The driver is using a new ABI,
367 * so the driver takes care of setting
368 * the slave's hwaddr and bringing
369 * it up again
370 */
371 } else {
372 /* No hwaddr for master yet, so
373 * set the slave's hwaddr to it
374 */
375 if (abi_ver < 1) {
376 /* For old ABI, the master needs to be
377 * down before setting it's hwaddr
378 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000379 if (set_if_down(master_ifname, master.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000380 goto undo_mtu;
381 }
382
Denis Vlasenko08768222008-06-07 22:13:04 +0000383 if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000384 bb_error_msg("%s: can't set hw address",
385 master_ifname);
386 goto undo_mtu;
387 }
388
389 if (abi_ver < 1) {
390 /* For old ABI, bring the master
391 * back up
392 */
Denis Vlasenko08768222008-06-07 22:13:04 +0000393 if (set_if_up(master_ifname, master.flags.ifr_flags))
Denis Vlasenko06aed432008-02-26 18:25:24 +0000394 goto undo_master_mac;
395 }
396
397 hwaddr_set = 1;
398 }
399
400 /* Do the real thing */
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000401 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000402 if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname)
403 && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000404 ) {
Denis Vlasenko06aed432008-02-26 18:25:24 +0000405 goto undo_master_mac;
Denis Vlasenko08768222008-06-07 22:13:04 +0000406 }
Denis Vlasenko06aed432008-02-26 18:25:24 +0000407
408 return 0;
409
410/* rollback (best effort) */
411 undo_master_mac:
Denis Vlasenko08768222008-06-07 22:13:04 +0000412 set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr));
Denis Vlasenko06aed432008-02-26 18:25:24 +0000413 hwaddr_set = 0;
414 goto undo_mtu;
Denis Vlasenko08768222008-06-07 22:13:04 +0000415
Denis Vlasenko06aed432008-02-26 18:25:24 +0000416 undo_slave_mac:
Denis Vlasenko08768222008-06-07 22:13:04 +0000417 set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr));
Denis Vlasenko06aed432008-02-26 18:25:24 +0000418 undo_mtu:
Denis Vlasenko08768222008-06-07 22:13:04 +0000419 set_mtu(slave_ifname, slave.mtu.ifr_mtu);
420 return 1;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000421}
422
423static int release(char *master_ifname, char *slave_ifname)
424{
425 struct ifreq ifr;
426 int res = 0;
427
428 if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000429 bb_error_msg("%s is not a slave", slave_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000430 return 1;
431 }
432
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000433 strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
Denis Vlasenko08768222008-06-07 22:13:04 +0000434 if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0
435 && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0
Denis Vlasenko06aed432008-02-26 18:25:24 +0000436 ) {
437 return 1;
438 }
439 if (abi_ver < 1) {
440 /* The driver is using an old ABI, so we'll set the interface
441 * down to avoid any conflicts due to same MAC/IP
442 */
443 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
444 }
445
446 /* set to default mtu */
Denis Vlasenko08768222008-06-07 22:13:04 +0000447 set_mtu(slave_ifname, 1500);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000448
449 return res;
450}
451
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000452static NOINLINE void get_drv_info(char *master_ifname)
Denis Vlasenko06aed432008-02-26 18:25:24 +0000453{
454 struct ifreq ifr;
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000455 struct ethtool_drvinfo info;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000456
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000457 memset(&ifr, 0, sizeof(ifr));
458 ifr.ifr_data = (caddr_t)&info;
459 info.cmd = ETHTOOL_GDRVINFO;
Denis Vlasenkob5975642008-06-08 00:24:43 +0000460 /* both fields are 32 bytes long (long enough) */
461 strcpy(info.driver, "ifenslave");
462 strcpy(info.fw_version, utoa(BOND_ABI_VERSION));
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000463 if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) {
464 if (errno == EOPNOTSUPP)
465 return;
466 bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
Denis Vlasenko06aed432008-02-26 18:25:24 +0000467 }
468
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000469 abi_ver = bb_strtou(info.fw_version, NULL, 0);
470 if (errno)
471 bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
472}
473
474int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000475int ifenslave_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000476{
477 char *master_ifname, *slave_ifname;
478 int rv;
479 int res;
480 unsigned opt;
481 enum {
482 OPT_c = (1 << 0),
483 OPT_d = (1 << 1),
484 OPT_f = (1 << 2),
485 };
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200486#if ENABLE_LONG_OPTS
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000487 static const char ifenslave_longopts[] ALIGN1 =
Denis Vlasenko048897c2008-06-08 08:01:12 +0000488 "change-active\0" No_argument "c"
489 "detach\0" No_argument "d"
490 "force\0" No_argument "f"
491 /* "all-interfaces\0" No_argument "a" */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000492 ;
493
494 applet_long_options = ifenslave_longopts;
495#endif
496 INIT_G();
497
Denis Vlasenko048897c2008-06-08 08:01:12 +0000498 opt = getopt32(argv, "cdfa");
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000499 argv += optind;
Denis Vlasenko048897c2008-06-08 08:01:12 +0000500 if (opt & (opt-1)) /* Only one option can be given */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000501 bb_show_usage();
502
503 master_ifname = *argv++;
504
505 /* No interface names - show all interfaces. */
506 if (!master_ifname) {
507 display_interfaces(NULL);
508 return EXIT_SUCCESS;
509 }
510
511 /* Open a basic socket */
512 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd);
513
514 /* Exchange abi version with bonding module */
515 get_drv_info(master_ifname);
516
517 slave_ifname = *argv++;
518 if (!slave_ifname) {
519 if (opt & (OPT_d|OPT_c)) {
520 /* --change or --detach, and no slaves given -
521 * show all interfaces. */
522 display_interfaces(slave_ifname /* == NULL */);
523 return 2; /* why 2? */
524 }
525 /* A single arg means show the
526 * configuration for this interface
527 */
528 display_interfaces(master_ifname);
529 return EXIT_SUCCESS;
530 }
531
Denis Vlasenko048897c2008-06-08 08:01:12 +0000532 if (get_if_settings(master_ifname, &master)) {
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000533 /* Probably a good reason not to go on */
534 bb_perror_msg_and_die("%s: can't get settings", master_ifname);
535 }
536
Denis Vlasenko048897c2008-06-08 08:01:12 +0000537 /* Check if master is indeed a master;
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000538 * if not then fail any operation
539 */
540 if (!(master.flags.ifr_flags & IFF_MASTER))
541 bb_error_msg_and_die("%s is not a master", master_ifname);
542
Denis Vlasenko048897c2008-06-08 08:01:12 +0000543 /* Check if master is up; if not then fail any operation */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000544 if (!(master.flags.ifr_flags & IFF_UP))
545 bb_error_msg_and_die("%s is not up", master_ifname);
546
Denis Vlasenko048897c2008-06-08 08:01:12 +0000547#ifdef WHY_BOTHER
548 /* Neither -c[hange] nor -d[etach] -> it's "enslave" then;
549 * and -f[orce] is not there too. Check that it's ethernet. */
550 if (!(opt & (OPT_d|OPT_c|OPT_f)) {
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000551 /* The family '1' is ARPHRD_ETHER for ethernet. */
552 if (master.hwaddr.ifr_hwaddr.sa_family != 1) {
553 bb_error_msg_and_die(
554 "%s is not ethernet-like (-f overrides)",
555 master_ifname);
556 }
557 }
Denis Vlasenko048897c2008-06-08 08:01:12 +0000558#endif
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000559
560 /* Accepts only one slave */
561 if (opt & OPT_c) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000562 /* Change active slave */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000563 if (get_slave_flags(slave_ifname)) {
564 bb_perror_msg_and_die(
565 "%s: can't get flags", slave_ifname);
566 }
567 change_active(master_ifname, slave_ifname);
568 return EXIT_SUCCESS;
569 }
570
Denis Vlasenko048897c2008-06-08 08:01:12 +0000571 /* Accepts multiple slaves */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000572 res = 0;
573 do {
574 if (opt & OPT_d) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000575 /* Detach a slave interface from the master */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000576 rv = get_slave_flags(slave_ifname);
577 if (rv) {
578 /* Can't work with this slave, */
579 /* remember the error and skip it */
580 bb_perror_msg(
581 "skipping %s: can't get flags",
582 slave_ifname);
583 res = rv;
584 continue;
585 }
586 rv = release(master_ifname, slave_ifname);
587 if (rv) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000588 bb_perror_msg("can't release %s from %s",
589 slave_ifname, master_ifname);
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000590 res = rv;
591 }
592 } else {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000593 /* Attach a slave interface to the master */
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000594 rv = get_if_settings(slave_ifname, &slave);
595 if (rv) {
596 /* Can't work with this slave, */
597 /* remember the error and skip it */
598 bb_perror_msg(
599 "skipping %s: can't get settings",
600 slave_ifname);
601 res = rv;
602 continue;
603 }
604 rv = enslave(master_ifname, slave_ifname);
605 if (rv) {
Denis Vlasenko048897c2008-06-08 08:01:12 +0000606 bb_perror_msg("can't enslave %s to %s",
607 slave_ifname, master_ifname);
Denis Vlasenkoc8fa04f2008-06-07 22:14:25 +0000608 res = rv;
609 }
610 }
611 } while ((slave_ifname = *argv++) != NULL);
612
613 if (ENABLE_FEATURE_CLEAN_UP) {
614 close(skfd);
615 }
616
617 return res;
Denis Vlasenko06aed432008-02-26 18:25:24 +0000618}