f3b4d7466a21344ffd21db0bf134d721c982574c
[oweals/busybox.git] / networking / ifenslave.c
1 /* 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  *
8  *      This program controls the Linux implementation of running multiple
9  *      network interfaces in parallel.
10  *
11  * Author:      Donald Becker <becker@cesdis.gsfc.nasa.gov>
12  *              Copyright 1994-1996 Donald Becker
13  *
14  *              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.
17  *
18  *      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
21  *
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>
82  *       - ifenslave -c was broken; it's now fixed
83  *       - Fixed problem with routes vanishing from master during enslave
84  *         processing.
85  *
86  *    - 2003/05/27 - Amir Noam <amir.noam at intel dot com>
87  *       - 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.
95  *
96  *    - 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
97  *       - Code cleanup and style changes
98  *         set version to 1.1.0
99  */
100
101 #include "libbb.h"
102
103 #include <net/if_arp.h>
104 #include <linux/if_bonding.h>
105 #include <linux/sockios.h>
106
107 typedef unsigned long long u64; /* hack, so we may include kernel's ethtool.h */
108 typedef uint32_t u32;           /* ditto */
109 typedef uint16_t u16;           /* ditto */
110 typedef uint8_t u8;             /* ditto */
111 #include <linux/ethtool.h>
112
113
114 struct dev_data {
115         struct ifreq mtu, flags, hwaddr;
116 };
117
118
119 enum { skfd = 3 };      /* AF_INET socket for ioctl() calls.*/
120 struct globals {
121         unsigned abi_ver;       /* userland - kernel ABI version */
122         smallint hwaddr_set;    /* Master's hwaddr is set */
123         struct dev_data master;
124         struct dev_data slave;
125 };
126 #define G (*ptr_to_globals)
127 #define abi_ver    (G.abi_ver   )
128 #define hwaddr_set (G.hwaddr_set)
129 #define master     (G.master    )
130 #define slave      (G.slave     )
131 #define INIT_G() do { \
132         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
133 } while (0)
134
135
136 static void get_drv_info(char *master_ifname);
137 static int get_if_settings(char *ifname, struct dev_data *dd);
138 static int get_slave_flags(char *slave_ifname);
139 static int set_master_hwaddr(char *master_ifname, struct sockaddr *hwaddr);
140 static int set_slave_hwaddr(char *slave_ifname, struct sockaddr *hwaddr);
141 static int set_slave_mtu(char *slave_ifname, int mtu);
142 static int set_if_flags(char *ifname, short flags);
143 static int set_if_up(char *ifname, short flags);
144 static int set_if_down(char *ifname, short flags);
145 static int clear_if_addr(char *ifname);
146 static int set_if_addr(char *master_ifname, char *slave_ifname);
147 static void change_active(char *master_ifname, char *slave_ifname);
148 static int enslave(char *master_ifname, char *slave_ifname);
149 static int release(char *master_ifname, char *slave_ifname);
150
151 static void strncpy_IFNAMSIZ(char *dst, const char *src)
152 {
153         strncpy(dst, src, IFNAMSIZ);
154 }
155
156 int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
157 int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv)
158 {
159         char *master_ifname, *slave_ifname;
160         int rv;
161         int res;
162         unsigned opt;
163         enum {
164                 OPT_c = (1 << 0),
165                 OPT_d = (1 << 1),
166                 OPT_f = (1 << 2),
167         };
168 #if ENABLE_GETOPT_LONG
169         static const char ifenslave_longopts[] ALIGN1 =
170                 "change-active" No_argument "c"
171                 "detach"        No_argument "d"
172                 "force"         No_argument "f"
173         ;
174
175         applet_long_options = ifenslave_longopts;
176 #endif
177         INIT_G();
178
179         opt = getopt32(argv, "cdf");
180         argv += optind;
181         if (opt & (opt-1)) /* options check */
182                 bb_show_usage();
183
184         /* Copy the interface name */
185         master_ifname = *argv++;
186
187         /* No remaining args means show all interfaces. */
188         if (!master_ifname) {
189                 display_interfaces(NULL);
190                 return EXIT_SUCCESS;
191         }
192
193         /* Open a basic socket */
194         xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd);
195
196         /* exchange abi version with bonding module */
197         get_drv_info(master_ifname);
198
199         slave_ifname = *argv++;
200         if (!slave_ifname) {
201                 if (opt & (OPT_d|OPT_c)) {
202                         display_interfaces(slave_ifname);
203                         return 2; /* why? */
204                 }
205                 /* A single arg means show the
206                  * configuration for this interface
207                  */
208                 display_interfaces(master_ifname);
209                 return EXIT_SUCCESS;
210         }
211
212         res = get_if_settings(master_ifname, &master);
213         if (res) {
214                 /* Probably a good reason not to go on */
215                 bb_perror_msg_and_die("%s: can't get settings", master_ifname);
216         }
217
218         /* check if master is indeed a master;
219          * if not then fail any operation
220          */
221         if (!(master.flags.ifr_flags & IFF_MASTER))
222                 bb_error_msg_and_die("%s is not a master", master_ifname);
223
224         /* check if master is up; if not then fail any operation */
225         if (!(master.flags.ifr_flags & IFF_UP))
226                 bb_error_msg_and_die("%s is not up", master_ifname);
227
228         /* Only for enslaving */
229         if (!(opt & (OPT_c|OPT_d))) {
230                 sa_family_t master_family = master.hwaddr.ifr_hwaddr.sa_family;
231
232                 /* The family '1' is ARPHRD_ETHER for ethernet. */
233                 if (master_family != 1 && !(opt & OPT_f)) {
234                         bb_error_msg_and_die(
235                                 "%s is not ethernet-like (-f overrides)",
236                                 master_ifname);
237                 }
238         }
239
240         /* Accepts only one slave */
241         if (opt & OPT_c) {
242                 /* change active slave */
243                 res = get_slave_flags(slave_ifname);
244                 if (res) {
245                         bb_perror_msg_and_die(
246                                 "%s: can't get flags", slave_ifname);
247                 }
248                 change_active(master_ifname, slave_ifname);
249                 return EXIT_SUCCESS;
250         }
251
252         /* Accept multiple slaves */
253         res = 0;
254         do {
255                 if (opt & OPT_d) {
256                         /* detach a slave interface from the master */
257                         rv = get_slave_flags(slave_ifname);
258                         if (rv) {
259                                 /* Can't work with this slave. */
260                                 /* remember the error and skip it*/
261                                 bb_perror_msg(
262                                         "skipping %s: can't get flags",
263                                         slave_ifname);
264                                 res = rv;
265                                 continue;
266                         }
267                         rv = release(master_ifname, slave_ifname);
268                         if (rv) {
269                                 bb_perror_msg(
270                                         "master %s, slave %s: "
271                                         "can't release",
272                                         master_ifname, slave_ifname);
273                                 res = rv;
274                         }
275                 } else {
276                         /* attach a slave interface to the master */
277                         rv = get_if_settings(slave_ifname, &slave);
278                         if (rv) {
279                                 /* Can't work with this slave. */
280                                 /* remember the error and skip it*/
281                                 bb_perror_msg(
282                                         "skipping %s: can't get settings",
283                                         slave_ifname);
284                                 res = rv;
285                                 continue;
286                         }
287                         rv = enslave(master_ifname, slave_ifname);
288                         if (rv) {
289                                 bb_perror_msg(
290                                         "master %s, slave %s: "
291                                         "can't enslave",
292                                         master_ifname, slave_ifname);
293                                 res = rv;
294                         }
295                 }
296         } while ((slave_ifname = *argv++) != NULL);
297
298         if (ENABLE_FEATURE_CLEAN_UP) {
299                 close(skfd);
300         }
301
302         return res;
303 }
304
305 static void get_drv_info(char *master_ifname)
306 {
307         struct ifreq ifr;
308         struct ethtool_drvinfo info;
309
310         memset(&ifr, 0, sizeof(ifr));
311         strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname);
312         ifr.ifr_data = (caddr_t)&info;
313
314         info.cmd = ETHTOOL_GDRVINFO;
315         strncpy(info.driver, "ifenslave", 32);
316         snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION);
317
318         if (ioctl(skfd, SIOCETHTOOL, &ifr) < 0) {
319                 if (errno == EOPNOTSUPP)
320                         return;
321                 bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
322         }
323
324         abi_ver = bb_strtou(info.fw_version, NULL, 0);
325         if (errno)
326                 bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
327
328         return;
329 }
330
331 static void change_active(char *master_ifname, char *slave_ifname)
332 {
333         struct ifreq ifr;
334
335         if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
336                 bb_error_msg_and_die(
337                         "%s is not a slave",
338                         slave_ifname);
339         }
340
341         strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname);
342         strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
343         if (ioctl(skfd, SIOCBONDCHANGEACTIVE, &ifr) < 0
344          && ioctl(skfd, BOND_CHANGE_ACTIVE_OLD, &ifr) < 0
345         ) {
346                 bb_perror_msg_and_die(
347                         "master %s, slave %s: can't "
348                         "change active",
349                         master_ifname, slave_ifname);
350         }
351 }
352
353 static int enslave(char *master_ifname, char *slave_ifname)
354 {
355         struct ifreq ifr;
356         int res;
357
358         if (slave.flags.ifr_flags & IFF_SLAVE) {
359                 bb_error_msg(
360                         "%s is already a slave",
361                         slave_ifname);
362                 return 1;
363         }
364
365         res = set_if_down(slave_ifname, slave.flags.ifr_flags);
366         if (res)
367                 return res;
368
369         if (abi_ver < 2) {
370                 /* Older bonding versions would panic if the slave has no IP
371                  * address, so get the IP setting from the master.
372                  */
373                 res = set_if_addr(master_ifname, slave_ifname);
374                 if (res) {
375                         bb_perror_msg("%s: can't set address", slave_ifname);
376                         return res;
377                 }
378         } else {
379                 res = clear_if_addr(slave_ifname);
380                 if (res) {
381                         bb_perror_msg("%s: can't clear address", slave_ifname);
382                         return res;
383                 }
384         }
385
386         if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) {
387                 res = set_slave_mtu(slave_ifname, master.mtu.ifr_mtu);
388                 if (res) {
389                         bb_perror_msg("%s: can't set MTU", slave_ifname);
390                         return res;
391                 }
392         }
393
394         if (hwaddr_set) {
395                 /* Master already has an hwaddr
396                  * so set it's hwaddr to the slave
397                  */
398                 if (abi_ver < 1) {
399                         /* The driver is using an old ABI, so
400                          * the application sets the slave's
401                          * hwaddr
402                          */
403                         res = set_slave_hwaddr(slave_ifname,
404                                                &(master.hwaddr.ifr_hwaddr));
405                         if (res) {
406                                 bb_perror_msg("%s: can't set hw address",
407                                                 slave_ifname);
408                                 goto undo_mtu;
409                         }
410
411                         /* For old ABI the application needs to bring the
412                          * slave back up
413                          */
414                         res = set_if_up(slave_ifname, slave.flags.ifr_flags);
415                         if (res)
416                                 goto undo_slave_mac;
417                 }
418                 /* The driver is using a new ABI,
419                  * so the driver takes care of setting
420                  * the slave's hwaddr and bringing
421                  * it up again
422                  */
423         } else {
424                 /* No hwaddr for master yet, so
425                  * set the slave's hwaddr to it
426                  */
427                 if (abi_ver < 1) {
428                         /* For old ABI, the master needs to be
429                          * down before setting it's hwaddr
430                          */
431                         res = set_if_down(master_ifname, master.flags.ifr_flags);
432                         if (res)
433                                 goto undo_mtu;
434                 }
435
436                 res = set_master_hwaddr(master_ifname,
437                                         &(slave.hwaddr.ifr_hwaddr));
438                 if (res) {
439                         bb_error_msg("%s: can't set hw address",
440                                 master_ifname);
441                         goto undo_mtu;
442                 }
443
444                 if (abi_ver < 1) {
445                         /* For old ABI, bring the master
446                          * back up
447                          */
448                         res = set_if_up(master_ifname, master.flags.ifr_flags);
449                         if (res)
450                                 goto undo_master_mac;
451                 }
452
453                 hwaddr_set = 1;
454         }
455
456         /* Do the real thing */
457         strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname);
458         strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
459         if (ioctl(skfd, SIOCBONDENSLAVE, &ifr) < 0
460          && ioctl(skfd, BOND_ENSLAVE_OLD, &ifr) < 0
461         ) {
462                 res = 1;
463         }
464
465         if (res)
466                 goto undo_master_mac;
467
468         return 0;
469
470 /* rollback (best effort) */
471  undo_master_mac:
472         set_master_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr));
473         hwaddr_set = 0;
474         goto undo_mtu;
475  undo_slave_mac:
476         set_slave_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr));
477  undo_mtu:
478         set_slave_mtu(slave_ifname, slave.mtu.ifr_mtu);
479         return res;
480 }
481
482 static int release(char *master_ifname, char *slave_ifname)
483 {
484         struct ifreq ifr;
485         int res = 0;
486
487         if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
488                 bb_error_msg("%s is not a slave",
489                         slave_ifname);
490                 return 1;
491         }
492
493         strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname);
494         strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
495         if (ioctl(skfd, SIOCBONDRELEASE, &ifr) < 0
496          && ioctl(skfd, BOND_RELEASE_OLD, &ifr) < 0
497         ) {
498                 return 1;
499         }
500         if (abi_ver < 1) {
501                 /* The driver is using an old ABI, so we'll set the interface
502                  * down to avoid any conflicts due to same MAC/IP
503                  */
504                 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
505         }
506
507         /* set to default mtu */
508         set_slave_mtu(slave_ifname, 1500);
509
510         return res;
511 }
512
513 static int get_if_settings(char *ifname, struct dev_data *dd)
514 {
515         int res;
516
517         strncpy_IFNAMSIZ(dd->mtu.ifr_name, ifname);
518         res = ioctl(skfd, SIOCGIFMTU, &dd->mtu);
519         strncpy_IFNAMSIZ(dd->flags.ifr_name, ifname);
520         res |= ioctl(skfd, SIOCGIFFLAGS, &dd->flags);
521         strncpy_IFNAMSIZ(dd->hwaddr.ifr_name, ifname);
522         res |= ioctl(skfd, SIOCGIFHWADDR, &dd->hwaddr);
523
524         return res;
525 }
526
527 static int get_slave_flags(char *slave_ifname)
528 {
529         strncpy_IFNAMSIZ(slave.flags.ifr_name, slave_ifname);
530         return ioctl(skfd, SIOCGIFFLAGS, &slave.flags);
531 }
532
533 static int set_master_hwaddr(char *master_ifname, struct sockaddr *hwaddr)
534 {
535         struct ifreq ifr;
536
537         strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname);
538         memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(struct sockaddr));
539         return ioctl(skfd, SIOCSIFHWADDR, &ifr);
540 }
541
542 static int set_slave_hwaddr(char *slave_ifname, struct sockaddr *hwaddr)
543 {
544         struct ifreq ifr;
545
546         strncpy_IFNAMSIZ(ifr.ifr_name, slave_ifname);
547         memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(struct sockaddr));
548         return ioctl(skfd, SIOCSIFHWADDR, &ifr);
549 }
550
551 static int set_slave_mtu(char *slave_ifname, int mtu)
552 {
553         struct ifreq ifr;
554
555         ifr.ifr_mtu = mtu;
556         strncpy_IFNAMSIZ(ifr.ifr_name, slave_ifname);
557         return ioctl(skfd, SIOCSIFMTU, &ifr);
558 }
559
560 static int set_if_flags(char *ifname, short flags)
561 {
562         struct ifreq ifr;
563
564         ifr.ifr_flags = flags;
565         strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
566         return ioctl(skfd, SIOCSIFFLAGS, &ifr);
567 }
568
569 static int set_if_up(char *ifname, short flags)
570 {
571         int res = set_if_flags(ifname, flags | IFF_UP);
572         if (res)
573                 bb_perror_msg("%s: can't up", ifname);
574         return res;
575 }
576
577 static int set_if_down(char *ifname, short flags)
578 {
579         int res = set_if_flags(ifname, flags & ~IFF_UP);
580         if (res)
581                 bb_perror_msg("%s: can't down", ifname);
582         return res;
583 }
584
585 static int clear_if_addr(char *ifname)
586 {
587         struct ifreq ifr;
588
589         strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
590         ifr.ifr_addr.sa_family = AF_INET;
591         memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data));
592         return ioctl(skfd, SIOCSIFADDR, &ifr);
593 }
594
595 static int set_if_addr(char *master_ifname, char *slave_ifname)
596 {
597         static const struct {
598                 int g_ioctl;
599                 int s_ioctl;
600         } ifra[] = {
601                 { SIOCGIFADDR,    SIOCSIFADDR    },
602                 { SIOCGIFDSTADDR, SIOCSIFDSTADDR },
603                 { SIOCGIFBRDADDR, SIOCSIFBRDADDR },
604                 { SIOCGIFNETMASK, SIOCSIFNETMASK },
605         };
606
607         struct ifreq ifr;
608         int res;
609         unsigned i;
610
611         for (i = 0; i < ARRAY_SIZE(ifra); i++) {
612                 strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname);
613                 res = ioctl(skfd, ifra[i].g_ioctl, &ifr);
614                 if (res < 0) {
615                         ifr.ifr_addr.sa_family = AF_INET;
616                         memset(ifr.ifr_addr.sa_data, 0,
617                                sizeof(ifr.ifr_addr.sa_data));
618                 }
619
620                 strncpy_IFNAMSIZ(ifr.ifr_name, slave_ifname);
621                 res = ioctl(skfd, ifra[i].s_ioctl, &ifr);
622                 if (res < 0)
623                         return res;
624         }
625
626         return 0;
627 }