2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 * T2 SDE: package/.../ifenslave/ifenslave.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
8 * More information can be found in the files COPYING and README.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License. A copy of the
13 * GNU General Public License can be found in the file COPYING.
14 * --- T2-COPYRIGHT-NOTE-END ---
17 * ifenslave.c: Configure network interfaces for parallel routing.
19 * This program controls the Linux implementation of running multiple
20 * network interfaces in parallel.
22 * Author: Donald Becker <becker@cesdis.gsfc.nasa.gov>
23 * Copyright 1994-1996 Donald Becker
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General Public
27 * License as published by the Free Software Foundation.
29 * The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
30 * Center of Excellence in Space Data and Information Sciences
31 * Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
34 * - 2000/10/02 Willy Tarreau <willy at meta-x.org> :
35 * - few fixes. Master's MAC address is now correctly taken from
36 * the first device when not previously set ;
37 * - detach support : call BOND_RELEASE to detach an enslaved interface.
38 * - give a mini-howto from command-line help : # ifenslave -h
40 * - 2001/02/16 Chad N. Tindel <ctindel at ieee dot org> :
41 * - Master is now brought down before setting the MAC address. In
42 * the 2.4 kernel you can't change the MAC address while the device is
43 * up because you get EBUSY.
45 * - 2001/09/13 Takao Indoh <indou dot takao at jp dot fujitsu dot com>
46 * - Added the ability to change the active interface on a mode 1 bond
49 * - 2001/10/23 Chad N. Tindel <ctindel at ieee dot org> :
50 * - No longer set the MAC address of the master. The bond device will
51 * take care of this itself
52 * - Try the SIOC*** versions of the bonding ioctls before using the
54 * - 2002/02/18 Erik Habbinga <erik_habbinga @ hp dot com> :
55 * - ifr2.ifr_flags was not initialized in the hwaddr_notset case,
56 * SIOCGIFFLAGS now called before hwaddr_notset test
58 * - 2002/10/31 Tony Cureington <tony.cureington * hp_com> :
59 * - If the master does not have a hardware address when the first slave
60 * is enslaved, the master is assigned the hardware address of that
61 * slave - there is a comment in bonding.c stating "ifenslave takes
62 * care of this now." This corrects the problem of slaves having
63 * different hardware addresses in active-backup mode when
64 * multiple interfaces are specified on a single ifenslave command
65 * (ifenslave bond0 eth0 eth1).
67 * - 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
68 * Shmulik Hen <shmulik.hen at intel dot com>
69 * - Moved setting the slave's mac address and openning it, from
70 * the application to the driver. This enables support of modes
71 * that need to use the unique mac address of each slave.
72 * The driver also takes care of closing the slave and restoring its
73 * original mac address upon release.
74 * In addition, block possibility of enslaving before the master is up.
75 * This prevents putting the system in an undefined state.
77 * - 2003/05/01 - Amir Noam <amir.noam at intel dot com>
78 * - Added ABI version control to restore compatibility between
79 * new/old ifenslave and new/old bonding.
80 * - Prevent adding an adapter that is already a slave.
81 * Fixes the problem of stalling the transmission and leaving
82 * the slave in a down state.
84 * - 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
85 * - Prevent enslaving if the bond device is down.
86 * Fixes the problem of leaving the system in unstable state and
87 * halting when trying to remove the module.
88 * - Close socket on all abnormal exists.
89 * - Add versioning scheme that follows that of the bonding driver.
90 * current version is 1.0.0 as a base line.
92 * - 2003/05/22 - Jay Vosburgh <fubar at us dot ibm dot com>
93 * - ifenslave -c was broken; it's now fixed
94 * - Fixed problem with routes vanishing from master during enslave
97 * - 2003/05/27 - Amir Noam <amir.noam at intel dot com>
98 * - Fix backward compatibility issues:
99 * For drivers not using ABI versions, slave was set down while
100 * it should be left up before enslaving.
101 * Also, master was not set down and the default set_mac_address()
102 * would fail and generate an error message in the system log.
103 * - For opt_c: slave should not be set to the master's setting
104 * while it is running. It was already set during enslave. To
105 * simplify things, it is now handeled separately.
107 * - 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
108 * - Code cleanup and style changes
109 * set version to 1.1.0
112 #define APP_VERSION "1.1.0"
113 #define APP_RELDATE "December 1, 2003"
114 #define APP_NAME "ifenslave"
116 static char *version
=
117 APP_NAME
".c:v" APP_VERSION
" (" APP_RELDATE
")\n"
118 "o Donald Becker (becker@cesdis.gsfc.nasa.gov).\n"
119 "o Detach support added on 2000/10/02 by Willy Tarreau (willy at meta-x.org).\n"
120 "o 2.4 kernel support added on 2001/02/16 by Chad N. Tindel\n"
121 " (ctindel at ieee dot org).\n";
123 static const char *usage_msg
=
124 "Usage: ifenslave [-f] <master-if> <slave-if> [<slave-if>...]\n"
125 " ifenslave -d <master-if> <slave-if> [<slave-if>...]\n"
126 " ifenslave -c <master-if> <slave-if>\n"
127 " ifenslave --help\n";
129 static const char *help_msg
=
131 " To create a bond device, simply follow these three steps :\n"
132 " - ensure that the required drivers are properly loaded :\n"
133 " # modprobe bonding ; modprobe <3c59x|eepro100|pcnet32|tulip|...>\n"
134 " - assign an IP address to the bond device :\n"
135 " # ifconfig bond0 <addr> netmask <mask> broadcast <bcast>\n"
136 " - attach all the interfaces you need to the bond device :\n"
137 " # ifenslave [{-f|--force}] bond0 eth0 [eth1 [eth2]...]\n"
138 " If bond0 didn't have a MAC address, it will take eth0's. Then, all\n"
139 " interfaces attached AFTER this assignment will get the same MAC addr.\n"
140 " (except for ALB/TLB modes)\n"
142 " To set the bond device down and automatically release all the slaves :\n"
143 " # ifconfig bond0 down\n"
145 " To detach a dead interface without setting the bond device down :\n"
146 " # ifenslave {-d|--detach} bond0 eth0 [eth1 [eth2]...]\n"
148 " To change active slave :\n"
149 " # ifenslave {-c|--change-active} bond0 eth0\n"
151 " To show master interface info\n"
152 " # ifenslave bond0\n"
154 " To show all interfaces info\n"
155 " # ifenslave {-a|--all-interfaces}\n"
157 " To be more verbose\n"
158 " # ifenslave {-v|--verbose} ...\n"
160 " # ifenslave {-u|--usage} Show usage\n"
161 " # ifenslave {-V|--version} Show version\n"
162 " # ifenslave {-h|--help} This message\n"
173 #include <sys/types.h>
174 #include <sys/socket.h>
175 #include <sys/ioctl.h>
176 #include <linux/if.h>
177 #include <net/if_arp.h>
178 #include <linux/if_ether.h>
179 #include <linux/if_bonding.h>
180 #include <linux/sockios.h>
182 typedef unsigned long long u64
; /* hack, so we may include kernel's ethtool.h */
183 typedef __uint32_t u32
; /* ditto */
184 typedef __uint16_t u16
; /* ditto */
185 typedef __uint8_t u8
; /* ditto */
186 #include <linux/ethtool.h>
188 struct option longopts
[] = {
189 /* { name has_arg *flag val } */
190 {"all-interfaces", 0, 0, 'a'}, /* Show all interfaces. */
191 {"change-active", 0, 0, 'c'}, /* Change the active slave. */
192 {"detach", 0, 0, 'd'}, /* Detach a slave interface. */
193 {"force", 0, 0, 'f'}, /* Force the operation. */
194 {"help", 0, 0, 'h'}, /* Give help */
195 {"usage", 0, 0, 'u'}, /* Give usage */
196 {"verbose", 0, 0, 'v'}, /* Report each action taken. */
197 {"version", 0, 0, 'V'}, /* Emit version information. */
201 /* Command-line flags. */
203 opt_a
= 0, /* Show-all-interfaces flag. */
204 opt_c
= 0, /* Change-active-slave flag. */
205 opt_d
= 0, /* Detach a slave interface. */
206 opt_f
= 0, /* Force the operation. */
207 opt_h
= 0, /* Help */
208 opt_u
= 0, /* Usage */
209 opt_v
= 0, /* Verbose flag. */
210 opt_V
= 0; /* Version */
212 int skfd
= -1; /* AF_INET socket for ioctl() calls.*/
213 int abi_ver
= 0; /* userland - kernel ABI version */
214 int hwaddr_set
= 0; /* Master's hwaddr is set */
217 struct ifreq master_mtu
, master_flags
, master_hwaddr
;
218 struct ifreq slave_mtu
, slave_flags
, slave_hwaddr
;
221 struct ifreq
*req_ifr
;
226 struct dev_ifr master_ifra
[] = {
227 {&master_mtu
, "SIOCGIFMTU", SIOCGIFMTU
},
228 {&master_flags
, "SIOCGIFFLAGS", SIOCGIFFLAGS
},
229 {&master_hwaddr
, "SIOCGIFHWADDR", SIOCGIFHWADDR
},
233 struct dev_ifr slave_ifra
[] = {
234 {&slave_mtu
, "SIOCGIFMTU", SIOCGIFMTU
},
235 {&slave_flags
, "SIOCGIFFLAGS", SIOCGIFFLAGS
},
236 {&slave_hwaddr
, "SIOCGIFHWADDR", SIOCGIFHWADDR
},
240 static void if_print(char *ifname
);
241 static int get_drv_info(char *master_ifname
);
242 static int get_if_settings(char *ifname
, struct dev_ifr ifra
[]);
243 static int get_slave_flags(char *slave_ifname
);
244 static int set_master_hwaddr(char *master_ifname
, struct sockaddr
*hwaddr
);
245 static int set_slave_hwaddr(char *slave_ifname
, struct sockaddr
*hwaddr
);
246 static int set_slave_mtu(char *slave_ifname
, int mtu
);
247 static int set_if_flags(char *ifname
, short flags
);
248 static int set_if_up(char *ifname
, short flags
);
249 static int set_if_down(char *ifname
, short flags
);
250 static int clear_if_addr(char *ifname
);
251 static int set_if_addr(char *master_ifname
, char *slave_ifname
);
252 static int change_active(char *master_ifname
, char *slave_ifname
);
253 static int enslave(char *master_ifname
, char *slave_ifname
);
254 static int release(char *master_ifname
, char *slave_ifname
);
255 #define v_print(fmt, args...) \
257 fprintf(stderr, fmt, ## args )
259 int main(int argc
, char *argv
[])
261 char **spp
, *master_ifname
, *slave_ifname
;
266 while ((c
= getopt_long(argc
, argv
, "acdfhuvV", longopts
, 0)) != EOF
) {
268 case 'a': opt_a
++; exclusive
++; break;
269 case 'c': opt_c
++; exclusive
++; break;
270 case 'd': opt_d
++; exclusive
++; break;
271 case 'f': opt_f
++; exclusive
++; break;
272 case 'h': opt_h
++; exclusive
++; break;
273 case 'u': opt_u
++; exclusive
++; break;
274 case 'v': opt_v
++; break;
275 case 'V': opt_V
++; exclusive
++; break;
278 fprintf(stderr
, usage_msg
);
286 fprintf(stderr
, usage_msg
);
291 if (opt_v
|| opt_V
) {
312 /* Open a basic socket */
313 if ((skfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
320 if (optind
== argc
) {
321 /* No remaining args */
322 /* show all interfaces */
323 if_print((char *)NULL
);
326 /* Just show usage */
327 fprintf(stderr
, usage_msg
);
333 /* Copy the interface name */
335 master_ifname
= *spp
++;
337 if (master_ifname
== NULL
) {
338 fprintf(stderr
, usage_msg
);
343 /* exchange abi version with bonding module */
344 res
= get_drv_info(master_ifname
);
347 "Master '%s': Error: handshake with driver failed. "
353 slave_ifname
= *spp
++;
355 if (slave_ifname
== NULL
) {
356 if (opt_d
|| opt_c
) {
357 fprintf(stderr
, usage_msg
);
362 /* A single arg means show the
363 * configuration for this interface
365 if_print(master_ifname
);
369 res
= get_if_settings(master_ifname
, master_ifra
);
371 /* Probably a good reason not to go on */
373 "Master '%s': Error: get settings failed: %s. "
375 master_ifname
, strerror(res
));
379 /* check if master is indeed a master;
380 * if not then fail any operation
382 if (!(master_flags
.ifr_flags
& IFF_MASTER
)) {
384 "Illegal operation; the specified interface '%s' "
385 "is not a master. Aborting\n",
391 /* check if master is up; if not then fail any operation */
392 if (!(master_flags
.ifr_flags
& IFF_UP
)) {
394 "Illegal operation; the specified master interface "
401 /* Only for enslaving */
402 if (!opt_c
&& !opt_d
) {
403 sa_family_t master_family
= master_hwaddr
.ifr_hwaddr
.sa_family
;
404 unsigned char *hwaddr
=
405 (unsigned char *)master_hwaddr
.ifr_hwaddr
.sa_data
;
407 /* The family '1' is ARPHRD_ETHER for ethernet. */
408 if (master_family
!= 1 && !opt_f
) {
410 "Illegal operation: The specified master "
411 "interface '%s' is not ethernet-like.\n "
412 "This program is designed to work with "
413 "ethernet-like network interfaces.\n "
414 "Use the '-f' option to force the "
421 /* Check master's hw addr */
422 for (i
= 0; i
< 6; i
++) {
423 if (hwaddr
[i
] != 0) {
430 v_print("current hardware address of master '%s' "
431 "is %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
434 hwaddr
[0], hwaddr
[1],
435 hwaddr
[2], hwaddr
[3],
436 hwaddr
[4], hwaddr
[5],
441 /* Accepts only one slave */
443 /* change active slave */
444 res
= get_slave_flags(slave_ifname
);
447 "Slave '%s': Error: get flags failed. "
452 res
= change_active(master_ifname
, slave_ifname
);
455 "Master '%s', Slave '%s': Error: "
456 "Change active failed\n",
457 master_ifname
, slave_ifname
);
460 /* Accept multiple slaves */
463 /* detach a slave interface from the master */
464 rv
= get_slave_flags(slave_ifname
);
466 /* Can't work with this slave. */
467 /* remember the error and skip it*/
469 "Slave '%s': Error: get flags "
470 "failed. Skipping\n",
475 rv
= release(master_ifname
, slave_ifname
);
478 "Master '%s', Slave '%s': Error: "
480 master_ifname
, slave_ifname
);
484 /* attach a slave interface to the master */
485 rv
= get_if_settings(slave_ifname
, slave_ifra
);
487 /* Can't work with this slave. */
488 /* remember the error and skip it*/
490 "Slave '%s': Error: get "
491 "settings failed: %s. "
493 slave_ifname
, strerror(rv
));
497 rv
= enslave(master_ifname
, slave_ifname
);
500 "Master '%s', Slave '%s': Error: "
502 master_ifname
, slave_ifname
);
506 } while ((slave_ifname
= *spp
++) != NULL
);
517 static short mif_flags
;
519 /* Get the inteface configuration from the kernel. */
520 static int if_getconfig(char *ifname
)
523 int metric
, mtu
; /* Parameters of the master interface. */
524 struct sockaddr dstaddr
, broadaddr
, netmask
;
525 unsigned char *hwaddr
;
527 strcpy(ifr
.ifr_name
, ifname
);
528 if (ioctl(skfd
, SIOCGIFFLAGS
, &ifr
) < 0)
530 mif_flags
= ifr
.ifr_flags
;
531 printf("The result of SIOCGIFFLAGS on %s is %x.\n",
532 ifname
, ifr
.ifr_flags
);
534 strcpy(ifr
.ifr_name
, ifname
);
535 if (ioctl(skfd
, SIOCGIFADDR
, &ifr
) < 0)
537 printf("The result of SIOCGIFADDR is %2.2x.%2.2x.%2.2x.%2.2x.\n",
538 ifr
.ifr_addr
.sa_data
[0], ifr
.ifr_addr
.sa_data
[1],
539 ifr
.ifr_addr
.sa_data
[2], ifr
.ifr_addr
.sa_data
[3]);
541 strcpy(ifr
.ifr_name
, ifname
);
542 if (ioctl(skfd
, SIOCGIFHWADDR
, &ifr
) < 0)
545 /* Gotta convert from 'char' to unsigned for printf(). */
546 hwaddr
= (unsigned char *)ifr
.ifr_hwaddr
.sa_data
;
547 printf("The result of SIOCGIFHWADDR is type %d "
548 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n",
549 ifr
.ifr_hwaddr
.sa_family
, hwaddr
[0], hwaddr
[1],
550 hwaddr
[2], hwaddr
[3], hwaddr
[4], hwaddr
[5]);
552 strcpy(ifr
.ifr_name
, ifname
);
553 if (ioctl(skfd
, SIOCGIFMETRIC
, &ifr
) < 0) {
556 metric
= ifr
.ifr_metric
;
558 strcpy(ifr
.ifr_name
, ifname
);
559 if (ioctl(skfd
, SIOCGIFMTU
, &ifr
) < 0)
564 strcpy(ifr
.ifr_name
, ifname
);
565 if (ioctl(skfd
, SIOCGIFDSTADDR
, &ifr
) < 0) {
566 memset(&dstaddr
, 0, sizeof(struct sockaddr
));
568 dstaddr
= ifr
.ifr_dstaddr
;
570 strcpy(ifr
.ifr_name
, ifname
);
571 if (ioctl(skfd
, SIOCGIFBRDADDR
, &ifr
) < 0) {
572 memset(&broadaddr
, 0, sizeof(struct sockaddr
));
574 broadaddr
= ifr
.ifr_broadaddr
;
576 strcpy(ifr
.ifr_name
, ifname
);
577 if (ioctl(skfd
, SIOCGIFNETMASK
, &ifr
) < 0) {
578 memset(&netmask
, 0, sizeof(struct sockaddr
));
580 netmask
= ifr
.ifr_netmask
;
585 static void if_print(char *ifname
)
592 if (ifname
== (char *)NULL
) {
593 ifc
.ifc_len
= sizeof(buff
);
595 if (ioctl(skfd
, SIOCGIFCONF
, &ifc
) < 0) {
596 perror("SIOCGIFCONF failed");
601 for (i
= ifc
.ifc_len
/ sizeof(struct ifreq
); --i
>= 0; ifr
++) {
602 if (if_getconfig(ifr
->ifr_name
) < 0) {
604 "%s: unknown interface.\n",
609 if (((mif_flags
& IFF_UP
) == 0) && !opt_a
) continue;
613 if (if_getconfig(ifname
) < 0) {
615 "%s: unknown interface.\n", ifname
);
620 static int get_drv_info(char *master_ifname
)
623 struct ethtool_drvinfo info
;
626 memset(&ifr
, 0, sizeof(ifr
));
627 strncpy(ifr
.ifr_name
, master_ifname
, IFNAMSIZ
);
628 ifr
.ifr_data
= (caddr_t
)&info
;
630 info
.cmd
= ETHTOOL_GDRVINFO
;
631 strncpy(info
.driver
, "ifenslave", 32);
632 snprintf(info
.fw_version
, 32, "%d", BOND_ABI_VERSION
);
634 if (ioctl(skfd
, SIOCETHTOOL
, &ifr
) < 0) {
635 if (errno
== EOPNOTSUPP
) {
640 v_print("Master '%s': Error: get bonding info failed %s\n",
641 master_ifname
, strerror(saved_errno
));
645 abi_ver
= strtoul(info
.fw_version
, &endptr
, 0);
647 v_print("Master '%s': Error: got invalid string as an ABI "
648 "version from the bonding module\n",
654 v_print("ABI ver is %d\n", abi_ver
);
659 static int change_active(char *master_ifname
, char *slave_ifname
)
664 if (!(slave_flags
.ifr_flags
& IFF_SLAVE
)) {
666 "Illegal operation: The specified slave interface "
667 "'%s' is not a slave\n",
672 strncpy(ifr
.ifr_name
, master_ifname
, IFNAMSIZ
);
673 strncpy(ifr
.ifr_slave
, slave_ifname
, IFNAMSIZ
);
674 if ((ioctl(skfd
, SIOCBONDCHANGEACTIVE
, &ifr
) < 0) &&
675 (ioctl(skfd
, BOND_CHANGE_ACTIVE_OLD
, &ifr
) < 0)) {
677 v_print("Master '%s': Error: SIOCBONDCHANGEACTIVE failed: "
679 master_ifname
, strerror(saved_errno
));
686 static int enslave(char *master_ifname
, char *slave_ifname
)
691 if (slave_flags
.ifr_flags
& IFF_SLAVE
) {
693 "Illegal operation: The specified slave interface "
694 "'%s' is already a slave\n",
699 res
= set_if_down(slave_ifname
, slave_flags
.ifr_flags
);
702 "Slave '%s': Error: bring interface down failed\n",
708 /* Older bonding versions would panic if the slave has no IP
709 * address, so get the IP setting from the master.
711 res
= set_if_addr(master_ifname
, slave_ifname
);
714 "Slave '%s': Error: set address failed\n",
719 res
= clear_if_addr(slave_ifname
);
722 "Slave '%s': Error: clear address failed\n",
728 if (master_mtu
.ifr_mtu
!= slave_mtu
.ifr_mtu
) {
729 res
= set_slave_mtu(slave_ifname
, master_mtu
.ifr_mtu
);
732 "Slave '%s': Error: set MTU failed\n",
739 /* Master already has an hwaddr
740 * so set it's hwaddr to the slave
743 /* The driver is using an old ABI, so
744 * the application sets the slave's
747 res
= set_slave_hwaddr(slave_ifname
,
748 &(master_hwaddr
.ifr_hwaddr
));
751 "Slave '%s': Error: set hw address "
757 /* For old ABI the application needs to bring the
760 res
= set_if_up(slave_ifname
, slave_flags
.ifr_flags
);
763 "Slave '%s': Error: bring interface "
769 /* The driver is using a new ABI,
770 * so the driver takes care of setting
771 * the slave's hwaddr and bringing
775 /* No hwaddr for master yet, so
776 * set the slave's hwaddr to it
779 /* For old ABI, the master needs to be
780 * down before setting it's hwaddr
782 res
= set_if_down(master_ifname
, master_flags
.ifr_flags
);
785 "Master '%s': Error: bring interface "
792 res
= set_master_hwaddr(master_ifname
,
793 &(slave_hwaddr
.ifr_hwaddr
));
796 "Master '%s': Error: set hw address "
803 /* For old ABI, bring the master
806 res
= set_if_up(master_ifname
, master_flags
.ifr_flags
);
809 "Master '%s': Error: bring interface "
812 goto undo_master_mac
;
819 /* Do the real thing */
820 strncpy(ifr
.ifr_name
, master_ifname
, IFNAMSIZ
);
821 strncpy(ifr
.ifr_slave
, slave_ifname
, IFNAMSIZ
);
822 if ((ioctl(skfd
, SIOCBONDENSLAVE
, &ifr
) < 0) &&
823 (ioctl(skfd
, BOND_ENSLAVE_OLD
, &ifr
) < 0)) {
825 v_print("Master '%s': Error: SIOCBONDENSLAVE failed: %s\n",
826 master_ifname
, strerror(saved_errno
));
831 goto undo_master_mac
;
836 /* rollback (best effort) */
838 set_master_hwaddr(master_ifname
, &(master_hwaddr
.ifr_hwaddr
));
842 set_slave_hwaddr(slave_ifname
, &(slave_hwaddr
.ifr_hwaddr
));
844 set_slave_mtu(slave_ifname
, slave_mtu
.ifr_mtu
);
848 static int release(char *master_ifname
, char *slave_ifname
)
853 if (!(slave_flags
.ifr_flags
& IFF_SLAVE
)) {
855 "Illegal operation: The specified slave interface "
856 "'%s' is not a slave\n",
861 strncpy(ifr
.ifr_name
, master_ifname
, IFNAMSIZ
);
862 strncpy(ifr
.ifr_slave
, slave_ifname
, IFNAMSIZ
);
863 if ((ioctl(skfd
, SIOCBONDRELEASE
, &ifr
) < 0) &&
864 (ioctl(skfd
, BOND_RELEASE_OLD
, &ifr
) < 0)) {
866 v_print("Master '%s': Error: SIOCBONDRELEASE failed: %s\n",
867 master_ifname
, strerror(saved_errno
));
869 } else if (abi_ver
< 1) {
870 /* The driver is using an old ABI, so we'll set the interface
871 * down to avoid any conflicts due to same MAC/IP
873 res
= set_if_down(slave_ifname
, slave_flags
.ifr_flags
);
876 "Slave '%s': Error: bring interface "
882 /* set to default mtu */
883 set_slave_mtu(slave_ifname
, 1500);
888 static int get_if_settings(char *ifname
, struct dev_ifr ifra
[])
893 for (i
= 0; ifra
[i
].req_ifr
; i
++) {
894 strncpy(ifra
[i
].req_ifr
->ifr_name
, ifname
, IFNAMSIZ
);
895 res
= ioctl(skfd
, ifra
[i
].req_type
, ifra
[i
].req_ifr
);
898 v_print("Interface '%s': Error: %s failed: %s\n",
899 ifname
, ifra
[i
].req_name
,
900 strerror(saved_errno
));
909 static int get_slave_flags(char *slave_ifname
)
913 strncpy(slave_flags
.ifr_name
, slave_ifname
, IFNAMSIZ
);
914 res
= ioctl(skfd
, SIOCGIFFLAGS
, &slave_flags
);
917 v_print("Slave '%s': Error: SIOCGIFFLAGS failed: %s\n",
918 slave_ifname
, strerror(saved_errno
));
920 v_print("Slave %s: flags %04X.\n",
921 slave_ifname
, slave_flags
.ifr_flags
);
927 static int set_master_hwaddr(char *master_ifname
, struct sockaddr
*hwaddr
)
929 unsigned char *addr
= (unsigned char *)hwaddr
->sa_data
;
933 strncpy(ifr
.ifr_name
, master_ifname
, IFNAMSIZ
);
934 memcpy(&(ifr
.ifr_hwaddr
), hwaddr
, sizeof(struct sockaddr
));
935 res
= ioctl(skfd
, SIOCSIFHWADDR
, &ifr
);
938 v_print("Master '%s': Error: SIOCSIFHWADDR failed: %s\n",
939 master_ifname
, strerror(saved_errno
));
942 v_print("Master '%s': hardware address set to "
943 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n",
944 master_ifname
, addr
[0], addr
[1], addr
[2],
945 addr
[3], addr
[4], addr
[5]);
951 static int set_slave_hwaddr(char *slave_ifname
, struct sockaddr
*hwaddr
)
953 unsigned char *addr
= (unsigned char *)hwaddr
->sa_data
;
957 strncpy(ifr
.ifr_name
, slave_ifname
, IFNAMSIZ
);
958 memcpy(&(ifr
.ifr_hwaddr
), hwaddr
, sizeof(struct sockaddr
));
959 res
= ioctl(skfd
, SIOCSIFHWADDR
, &ifr
);
963 v_print("Slave '%s': Error: SIOCSIFHWADDR failed: %s\n",
964 slave_ifname
, strerror(saved_errno
));
966 if (saved_errno
== EBUSY
) {
967 v_print(" The device is busy: it must be idle "
968 "before running this command.\n");
969 } else if (saved_errno
== EOPNOTSUPP
) {
970 v_print(" The device does not support setting "
972 " Your kernel likely does not support slave "
974 } else if (saved_errno
== EINVAL
) {
975 v_print(" The device's address type does not match "
976 "the master's address type.\n");
980 v_print("Slave '%s': hardware address set to "
981 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n",
982 slave_ifname
, addr
[0], addr
[1], addr
[2],
983 addr
[3], addr
[4], addr
[5]);
989 static int set_slave_mtu(char *slave_ifname
, int mtu
)
995 strncpy(ifr
.ifr_name
, slave_ifname
, IFNAMSIZ
);
997 res
= ioctl(skfd
, SIOCSIFMTU
, &ifr
);
1000 v_print("Slave '%s': Error: SIOCSIFMTU failed: %s\n",
1001 slave_ifname
, strerror(saved_errno
));
1003 v_print("Slave '%s': MTU set to %d.\n", slave_ifname
, mtu
);
1009 static int set_if_flags(char *ifname
, short flags
)
1014 ifr
.ifr_flags
= flags
;
1015 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1017 res
= ioctl(skfd
, SIOCSIFFLAGS
, &ifr
);
1019 saved_errno
= errno
;
1020 v_print("Interface '%s': Error: SIOCSIFFLAGS failed: %s\n",
1021 ifname
, strerror(saved_errno
));
1023 v_print("Interface '%s': flags set to %04X.\n", ifname
, flags
);
1029 static int set_if_up(char *ifname
, short flags
)
1031 return set_if_flags(ifname
, flags
| IFF_UP
);
1034 static int set_if_down(char *ifname
, short flags
)
1036 return set_if_flags(ifname
, flags
& ~IFF_UP
);
1039 static int clear_if_addr(char *ifname
)
1044 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1045 ifr
.ifr_addr
.sa_family
= AF_INET
;
1046 memset(ifr
.ifr_addr
.sa_data
, 0, sizeof(ifr
.ifr_addr
.sa_data
));
1048 res
= ioctl(skfd
, SIOCSIFADDR
, &ifr
);
1050 saved_errno
= errno
;
1051 v_print("Interface '%s': Error: SIOCSIFADDR failed: %s\n",
1052 ifname
, strerror(saved_errno
));
1054 v_print("Interface '%s': address cleared\n", ifname
);
1060 static int set_if_addr(char *master_ifname
, char *slave_ifname
)
1064 unsigned char *ipaddr
;
1072 {"IFADDR", "addr", SIOCGIFADDR
, SIOCSIFADDR
},
1073 {"DSTADDR", "destination addr", SIOCGIFDSTADDR
, SIOCSIFDSTADDR
},
1074 {"BRDADDR", "broadcast addr", SIOCGIFBRDADDR
, SIOCSIFBRDADDR
},
1075 {"NETMASK", "netmask", SIOCGIFNETMASK
, SIOCSIFNETMASK
},
1079 for (i
= 0; ifra
[i
].req_name
; i
++) {
1080 strncpy(ifr
.ifr_name
, master_ifname
, IFNAMSIZ
);
1081 res
= ioctl(skfd
, ifra
[i
].g_ioctl
, &ifr
);
1083 int saved_errno
= errno
;
1085 v_print("Interface '%s': Error: SIOCG%s failed: %s\n",
1086 master_ifname
, ifra
[i
].req_name
,
1087 strerror(saved_errno
));
1089 ifr
.ifr_addr
.sa_family
= AF_INET
;
1090 memset(ifr
.ifr_addr
.sa_data
, 0,
1091 sizeof(ifr
.ifr_addr
.sa_data
));
1094 strncpy(ifr
.ifr_name
, slave_ifname
, IFNAMSIZ
);
1095 res
= ioctl(skfd
, ifra
[i
].s_ioctl
, &ifr
);
1097 int saved_errno
= errno
;
1099 v_print("Interface '%s': Error: SIOCS%s failed: %s\n",
1100 slave_ifname
, ifra
[i
].req_name
,
1101 strerror(saved_errno
));
1106 ipaddr
= ifr
.ifr_addr
.sa_data
;
1107 v_print("Interface '%s': set IP %s to %d.%d.%d.%d\n",
1108 slave_ifname
, ifra
[i
].desc
,
1109 ipaddr
[0], ipaddr
[1], ipaddr
[2], ipaddr
[3]);
1117 * version-control: t
1118 * kept-new-versions: 5
1122 * compile-command: "gcc -Wall -Wstrict-prototypes -O -I/usr/src/linux/include ifenslave.c -o ifenslave"