Dash:
[t2.git] / package / network / ifenslave / ifenslave.c
blob9631822925d00db8a0f732ff4be1de0574e6eb9b
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: package/.../ifenslave/ifenslave.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 *
8 * More information can be found in the files COPYING and README.
9 *
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 ---
16 /* Mode: C;
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
33 * Changes :
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
47 * at runtime.
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
53 * old versions
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
95 * processing.
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 =
130 "\n"
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"
141 "\n"
142 " To set the bond device down and automatically release all the slaves :\n"
143 " # ifconfig bond0 down\n"
144 "\n"
145 " To detach a dead interface without setting the bond device down :\n"
146 " # ifenslave {-d|--detach} bond0 eth0 [eth1 [eth2]...]\n"
147 "\n"
148 " To change active slave :\n"
149 " # ifenslave {-c|--change-active} bond0 eth0\n"
150 "\n"
151 " To show master interface info\n"
152 " # ifenslave bond0\n"
153 "\n"
154 " To show all interfaces info\n"
155 " # ifenslave {-a|--all-interfaces}\n"
156 "\n"
157 " To be more verbose\n"
158 " # ifenslave {-v|--verbose} ...\n"
159 "\n"
160 " # ifenslave {-u|--usage} Show usage\n"
161 " # ifenslave {-V|--version} Show version\n"
162 " # ifenslave {-h|--help} This message\n"
163 "\n";
165 #include <unistd.h>
166 #include <stdlib.h>
167 #include <stdio.h>
168 #include <ctype.h>
169 #include <string.h>
170 #include <errno.h>
171 #include <fcntl.h>
172 #include <getopt.h>
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. */
198 { 0, 0, 0, 0}
201 /* Command-line flags. */
202 unsigned int
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 */
215 int saved_errno;
217 struct ifreq master_mtu, master_flags, master_hwaddr;
218 struct ifreq slave_mtu, slave_flags, slave_hwaddr;
220 struct dev_ifr {
221 struct ifreq *req_ifr;
222 char *req_name;
223 int req_type;
226 struct dev_ifr master_ifra[] = {
227 {&master_mtu, "SIOCGIFMTU", SIOCGIFMTU},
228 {&master_flags, "SIOCGIFFLAGS", SIOCGIFFLAGS},
229 {&master_hwaddr, "SIOCGIFHWADDR", SIOCGIFHWADDR},
230 {NULL, "", 0}
233 struct dev_ifr slave_ifra[] = {
234 {&slave_mtu, "SIOCGIFMTU", SIOCGIFMTU},
235 {&slave_flags, "SIOCGIFFLAGS", SIOCGIFFLAGS},
236 {&slave_hwaddr, "SIOCGIFHWADDR", SIOCGIFHWADDR},
237 {NULL, "", 0}
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...) \
256 if (opt_v) \
257 fprintf(stderr, fmt, ## args )
259 int main(int argc, char *argv[])
261 char **spp, *master_ifname, *slave_ifname;
262 int c, i, rv;
263 int res = 0;
264 int exclusive = 0;
266 while ((c = getopt_long(argc, argv, "acdfhuvV", longopts, 0)) != EOF) {
267 switch (c) {
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;
277 case '?':
278 fprintf(stderr, usage_msg);
279 res = 2;
280 goto out;
284 /* options check */
285 if (exclusive > 1) {
286 fprintf(stderr, usage_msg);
287 res = 2;
288 goto out;
291 if (opt_v || opt_V) {
292 printf(version);
293 if (opt_V) {
294 res = 0;
295 goto out;
299 if (opt_u) {
300 printf(usage_msg);
301 res = 0;
302 goto out;
305 if (opt_h) {
306 printf(usage_msg);
307 printf(help_msg);
308 res = 0;
309 goto out;
312 /* Open a basic socket */
313 if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
314 perror("socket");
315 res = 1;
316 goto out;
319 if (opt_a) {
320 if (optind == argc) {
321 /* No remaining args */
322 /* show all interfaces */
323 if_print((char *)NULL);
324 goto out;
325 } else {
326 /* Just show usage */
327 fprintf(stderr, usage_msg);
328 res = 2;
329 goto out;
333 /* Copy the interface name */
334 spp = argv + optind;
335 master_ifname = *spp++;
337 if (master_ifname == NULL) {
338 fprintf(stderr, usage_msg);
339 res = 2;
340 goto out;
343 /* exchange abi version with bonding module */
344 res = get_drv_info(master_ifname);
345 if (res) {
346 fprintf(stderr,
347 "Master '%s': Error: handshake with driver failed. "
348 "Aborting\n",
349 master_ifname);
350 goto out;
353 slave_ifname = *spp++;
355 if (slave_ifname == NULL) {
356 if (opt_d || opt_c) {
357 fprintf(stderr, usage_msg);
358 res = 2;
359 goto out;
362 /* A single arg means show the
363 * configuration for this interface
365 if_print(master_ifname);
366 goto out;
369 res = get_if_settings(master_ifname, master_ifra);
370 if (res) {
371 /* Probably a good reason not to go on */
372 fprintf(stderr,
373 "Master '%s': Error: get settings failed: %s. "
374 "Aborting\n",
375 master_ifname, strerror(res));
376 goto out;
379 /* check if master is indeed a master;
380 * if not then fail any operation
382 if (!(master_flags.ifr_flags & IFF_MASTER)) {
383 fprintf(stderr,
384 "Illegal operation; the specified interface '%s' "
385 "is not a master. Aborting\n",
386 master_ifname);
387 res = 1;
388 goto out;
391 /* check if master is up; if not then fail any operation */
392 if (!(master_flags.ifr_flags & IFF_UP)) {
393 fprintf(stderr,
394 "Illegal operation; the specified master interface "
395 "'%s' is not up.\n",
396 master_ifname);
397 res = 1;
398 goto out;
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) {
409 fprintf(stderr,
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 "
415 "operation.\n",
416 master_ifname);
417 res = 1;
418 goto out;
421 /* Check master's hw addr */
422 for (i = 0; i < 6; i++) {
423 if (hwaddr[i] != 0) {
424 hwaddr_set = 1;
425 break;
429 if (hwaddr_set) {
430 v_print("current hardware address of master '%s' "
431 "is %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
432 "type %d\n",
433 master_ifname,
434 hwaddr[0], hwaddr[1],
435 hwaddr[2], hwaddr[3],
436 hwaddr[4], hwaddr[5],
437 master_family);
441 /* Accepts only one slave */
442 if (opt_c) {
443 /* change active slave */
444 res = get_slave_flags(slave_ifname);
445 if (res) {
446 fprintf(stderr,
447 "Slave '%s': Error: get flags failed. "
448 "Aborting\n",
449 slave_ifname);
450 goto out;
452 res = change_active(master_ifname, slave_ifname);
453 if (res) {
454 fprintf(stderr,
455 "Master '%s', Slave '%s': Error: "
456 "Change active failed\n",
457 master_ifname, slave_ifname);
459 } else {
460 /* Accept multiple slaves */
461 do {
462 if (opt_d) {
463 /* detach a slave interface from the master */
464 rv = get_slave_flags(slave_ifname);
465 if (rv) {
466 /* Can't work with this slave. */
467 /* remember the error and skip it*/
468 fprintf(stderr,
469 "Slave '%s': Error: get flags "
470 "failed. Skipping\n",
471 slave_ifname);
472 res = rv;
473 continue;
475 rv = release(master_ifname, slave_ifname);
476 if (rv) {
477 fprintf(stderr,
478 "Master '%s', Slave '%s': Error: "
479 "Release failed\n",
480 master_ifname, slave_ifname);
481 res = rv;
483 } else {
484 /* attach a slave interface to the master */
485 rv = get_if_settings(slave_ifname, slave_ifra);
486 if (rv) {
487 /* Can't work with this slave. */
488 /* remember the error and skip it*/
489 fprintf(stderr,
490 "Slave '%s': Error: get "
491 "settings failed: %s. "
492 "Skipping\n",
493 slave_ifname, strerror(rv));
494 res = rv;
495 continue;
497 rv = enslave(master_ifname, slave_ifname);
498 if (rv) {
499 fprintf(stderr,
500 "Master '%s', Slave '%s': Error: "
501 "Enslave failed\n",
502 master_ifname, slave_ifname);
503 res = rv;
506 } while ((slave_ifname = *spp++) != NULL);
509 out:
510 if (skfd >= 0) {
511 close(skfd);
514 return res;
517 static short mif_flags;
519 /* Get the inteface configuration from the kernel. */
520 static int if_getconfig(char *ifname)
522 struct ifreq ifr;
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)
529 return -1;
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)
536 return -1;
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)
543 return -1;
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) {
554 metric = 0;
555 } else
556 metric = ifr.ifr_metric;
558 strcpy(ifr.ifr_name, ifname);
559 if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
560 mtu = 0;
561 else
562 mtu = ifr.ifr_mtu;
564 strcpy(ifr.ifr_name, ifname);
565 if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) < 0) {
566 memset(&dstaddr, 0, sizeof(struct sockaddr));
567 } else
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));
573 } else
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));
579 } else
580 netmask = ifr.ifr_netmask;
582 return 0;
585 static void if_print(char *ifname)
587 char buff[1024];
588 struct ifconf ifc;
589 struct ifreq *ifr;
590 int i;
592 if (ifname == (char *)NULL) {
593 ifc.ifc_len = sizeof(buff);
594 ifc.ifc_buf = buff;
595 if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
596 perror("SIOCGIFCONF failed");
597 return;
600 ifr = ifc.ifc_req;
601 for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) {
602 if (if_getconfig(ifr->ifr_name) < 0) {
603 fprintf(stderr,
604 "%s: unknown interface.\n",
605 ifr->ifr_name);
606 continue;
609 if (((mif_flags & IFF_UP) == 0) && !opt_a) continue;
610 /*ife_print(&ife);*/
612 } else {
613 if (if_getconfig(ifname) < 0) {
614 fprintf(stderr,
615 "%s: unknown interface.\n", ifname);
620 static int get_drv_info(char *master_ifname)
622 struct ifreq ifr;
623 struct ethtool_drvinfo info;
624 char *endptr;
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) {
636 goto out;
639 saved_errno = errno;
640 v_print("Master '%s': Error: get bonding info failed %s\n",
641 master_ifname, strerror(saved_errno));
642 return 1;
645 abi_ver = strtoul(info.fw_version, &endptr, 0);
646 if (*endptr) {
647 v_print("Master '%s': Error: got invalid string as an ABI "
648 "version from the bonding module\n",
649 master_ifname);
650 return 1;
653 out:
654 v_print("ABI ver is %d\n", abi_ver);
656 return 0;
659 static int change_active(char *master_ifname, char *slave_ifname)
661 struct ifreq ifr;
662 int res = 0;
664 if (!(slave_flags.ifr_flags & IFF_SLAVE)) {
665 fprintf(stderr,
666 "Illegal operation: The specified slave interface "
667 "'%s' is not a slave\n",
668 slave_ifname);
669 return 1;
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)) {
676 saved_errno = errno;
677 v_print("Master '%s': Error: SIOCBONDCHANGEACTIVE failed: "
678 "%s\n",
679 master_ifname, strerror(saved_errno));
680 res = 1;
683 return res;
686 static int enslave(char *master_ifname, char *slave_ifname)
688 struct ifreq ifr;
689 int res = 0;
691 if (slave_flags.ifr_flags & IFF_SLAVE) {
692 fprintf(stderr,
693 "Illegal operation: The specified slave interface "
694 "'%s' is already a slave\n",
695 slave_ifname);
696 return 1;
699 res = set_if_down(slave_ifname, slave_flags.ifr_flags);
700 if (res) {
701 fprintf(stderr,
702 "Slave '%s': Error: bring interface down failed\n",
703 slave_ifname);
704 return res;
707 if (abi_ver < 2) {
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);
712 if (res) {
713 fprintf(stderr,
714 "Slave '%s': Error: set address failed\n",
715 slave_ifname);
716 return res;
718 } else {
719 res = clear_if_addr(slave_ifname);
720 if (res) {
721 fprintf(stderr,
722 "Slave '%s': Error: clear address failed\n",
723 slave_ifname);
724 return res;
728 if (master_mtu.ifr_mtu != slave_mtu.ifr_mtu) {
729 res = set_slave_mtu(slave_ifname, master_mtu.ifr_mtu);
730 if (res) {
731 fprintf(stderr,
732 "Slave '%s': Error: set MTU failed\n",
733 slave_ifname);
734 return res;
738 if (hwaddr_set) {
739 /* Master already has an hwaddr
740 * so set it's hwaddr to the slave
742 if (abi_ver < 1) {
743 /* The driver is using an old ABI, so
744 * the application sets the slave's
745 * hwaddr
747 res = set_slave_hwaddr(slave_ifname,
748 &(master_hwaddr.ifr_hwaddr));
749 if (res) {
750 fprintf(stderr,
751 "Slave '%s': Error: set hw address "
752 "failed\n",
753 slave_ifname);
754 goto undo_mtu;
757 /* For old ABI the application needs to bring the
758 * slave back up
760 res = set_if_up(slave_ifname, slave_flags.ifr_flags);
761 if (res) {
762 fprintf(stderr,
763 "Slave '%s': Error: bring interface "
764 "down failed\n",
765 slave_ifname);
766 goto undo_slave_mac;
769 /* The driver is using a new ABI,
770 * so the driver takes care of setting
771 * the slave's hwaddr and bringing
772 * it up again
774 } else {
775 /* No hwaddr for master yet, so
776 * set the slave's hwaddr to it
778 if (abi_ver < 1) {
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);
783 if (res) {
784 fprintf(stderr,
785 "Master '%s': Error: bring interface "
786 "down failed\n",
787 master_ifname);
788 goto undo_mtu;
792 res = set_master_hwaddr(master_ifname,
793 &(slave_hwaddr.ifr_hwaddr));
794 if (res) {
795 fprintf(stderr,
796 "Master '%s': Error: set hw address "
797 "failed\n",
798 master_ifname);
799 goto undo_mtu;
802 if (abi_ver < 1) {
803 /* For old ABI, bring the master
804 * back up
806 res = set_if_up(master_ifname, master_flags.ifr_flags);
807 if (res) {
808 fprintf(stderr,
809 "Master '%s': Error: bring interface "
810 "up failed\n",
811 master_ifname);
812 goto undo_master_mac;
816 hwaddr_set = 1;
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)) {
824 saved_errno = errno;
825 v_print("Master '%s': Error: SIOCBONDENSLAVE failed: %s\n",
826 master_ifname, strerror(saved_errno));
827 res = 1;
830 if (res) {
831 goto undo_master_mac;
834 return 0;
836 /* rollback (best effort) */
837 undo_master_mac:
838 set_master_hwaddr(master_ifname, &(master_hwaddr.ifr_hwaddr));
839 hwaddr_set = 0;
840 goto undo_mtu;
841 undo_slave_mac:
842 set_slave_hwaddr(slave_ifname, &(slave_hwaddr.ifr_hwaddr));
843 undo_mtu:
844 set_slave_mtu(slave_ifname, slave_mtu.ifr_mtu);
845 return res;
848 static int release(char *master_ifname, char *slave_ifname)
850 struct ifreq ifr;
851 int res = 0;
853 if (!(slave_flags.ifr_flags & IFF_SLAVE)) {
854 fprintf(stderr,
855 "Illegal operation: The specified slave interface "
856 "'%s' is not a slave\n",
857 slave_ifname);
858 return 1;
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)) {
865 saved_errno = errno;
866 v_print("Master '%s': Error: SIOCBONDRELEASE failed: %s\n",
867 master_ifname, strerror(saved_errno));
868 return 1;
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);
874 if (res) {
875 fprintf(stderr,
876 "Slave '%s': Error: bring interface "
877 "down failed\n",
878 slave_ifname);
882 /* set to default mtu */
883 set_slave_mtu(slave_ifname, 1500);
885 return res;
888 static int get_if_settings(char *ifname, struct dev_ifr ifra[])
890 int i;
891 int res = 0;
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);
896 if (res < 0) {
897 saved_errno = errno;
898 v_print("Interface '%s': Error: %s failed: %s\n",
899 ifname, ifra[i].req_name,
900 strerror(saved_errno));
902 return saved_errno;
906 return 0;
909 static int get_slave_flags(char *slave_ifname)
911 int res = 0;
913 strncpy(slave_flags.ifr_name, slave_ifname, IFNAMSIZ);
914 res = ioctl(skfd, SIOCGIFFLAGS, &slave_flags);
915 if (res < 0) {
916 saved_errno = errno;
917 v_print("Slave '%s': Error: SIOCGIFFLAGS failed: %s\n",
918 slave_ifname, strerror(saved_errno));
919 } else {
920 v_print("Slave %s: flags %04X.\n",
921 slave_ifname, slave_flags.ifr_flags);
924 return res;
927 static int set_master_hwaddr(char *master_ifname, struct sockaddr *hwaddr)
929 unsigned char *addr = (unsigned char *)hwaddr->sa_data;
930 struct ifreq ifr;
931 int res = 0;
933 strncpy(ifr.ifr_name, master_ifname, IFNAMSIZ);
934 memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(struct sockaddr));
935 res = ioctl(skfd, SIOCSIFHWADDR, &ifr);
936 if (res < 0) {
937 saved_errno = errno;
938 v_print("Master '%s': Error: SIOCSIFHWADDR failed: %s\n",
939 master_ifname, strerror(saved_errno));
940 return res;
941 } else {
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]);
948 return res;
951 static int set_slave_hwaddr(char *slave_ifname, struct sockaddr *hwaddr)
953 unsigned char *addr = (unsigned char *)hwaddr->sa_data;
954 struct ifreq ifr;
955 int res = 0;
957 strncpy(ifr.ifr_name, slave_ifname, IFNAMSIZ);
958 memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(struct sockaddr));
959 res = ioctl(skfd, SIOCSIFHWADDR, &ifr);
960 if (res < 0) {
961 saved_errno = errno;
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 "
971 "the MAC address.\n"
972 " Your kernel likely does not support slave "
973 "devices.\n");
974 } else if (saved_errno == EINVAL) {
975 v_print(" The device's address type does not match "
976 "the master's address type.\n");
978 return res;
979 } else {
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]);
986 return res;
989 static int set_slave_mtu(char *slave_ifname, int mtu)
991 struct ifreq ifr;
992 int res = 0;
994 ifr.ifr_mtu = mtu;
995 strncpy(ifr.ifr_name, slave_ifname, IFNAMSIZ);
997 res = ioctl(skfd, SIOCSIFMTU, &ifr);
998 if (res < 0) {
999 saved_errno = errno;
1000 v_print("Slave '%s': Error: SIOCSIFMTU failed: %s\n",
1001 slave_ifname, strerror(saved_errno));
1002 } else {
1003 v_print("Slave '%s': MTU set to %d.\n", slave_ifname, mtu);
1006 return res;
1009 static int set_if_flags(char *ifname, short flags)
1011 struct ifreq ifr;
1012 int res = 0;
1014 ifr.ifr_flags = flags;
1015 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
1017 res = ioctl(skfd, SIOCSIFFLAGS, &ifr);
1018 if (res < 0) {
1019 saved_errno = errno;
1020 v_print("Interface '%s': Error: SIOCSIFFLAGS failed: %s\n",
1021 ifname, strerror(saved_errno));
1022 } else {
1023 v_print("Interface '%s': flags set to %04X.\n", ifname, flags);
1026 return res;
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)
1041 struct ifreq ifr;
1042 int res = 0;
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);
1049 if (res < 0) {
1050 saved_errno = errno;
1051 v_print("Interface '%s': Error: SIOCSIFADDR failed: %s\n",
1052 ifname, strerror(saved_errno));
1053 } else {
1054 v_print("Interface '%s': address cleared\n", ifname);
1057 return res;
1060 static int set_if_addr(char *master_ifname, char *slave_ifname)
1062 struct ifreq ifr;
1063 int res;
1064 unsigned char *ipaddr;
1065 int i;
1066 struct {
1067 char *req_name;
1068 char *desc;
1069 int g_ioctl;
1070 int s_ioctl;
1071 } ifra[] = {
1072 {"IFADDR", "addr", SIOCGIFADDR, SIOCSIFADDR},
1073 {"DSTADDR", "destination addr", SIOCGIFDSTADDR, SIOCSIFDSTADDR},
1074 {"BRDADDR", "broadcast addr", SIOCGIFBRDADDR, SIOCSIFBRDADDR},
1075 {"NETMASK", "netmask", SIOCGIFNETMASK, SIOCSIFNETMASK},
1076 {NULL, NULL, 0, 0},
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);
1082 if (res < 0) {
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);
1096 if (res < 0) {
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));
1103 return res;
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]);
1112 return 0;
1116 * Local variables:
1117 * version-control: t
1118 * kept-new-versions: 5
1119 * c-indent-level: 4
1120 * c-basic-offset: 4
1121 * tab-width: 4
1122 * compile-command: "gcc -Wall -Wstrict-prototypes -O -I/usr/src/linux/include ifenslave.c -o ifenslave"
1123 * End: