hust test: complain if busybox binary can't be found
[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.h> - no. linux/if_bonding.h pulls in linux/if.h */
104 #include <net/if_arp.h>
105 #include <linux/if_bonding.h>
106 #include <linux/sockios.h>
107
108 #include "fix_u32.h" /* hack, so we may include kernel's ethtool.h */
109 #include <linux/ethtool.h>
110
111 #ifndef IFNAMSIZ
112 # define IFNAMSIZ 16
113 #endif
114
115
116 struct dev_data {
117         struct ifreq mtu, flags, hwaddr;
118 };
119
120
121 enum { skfd = 3 };      /* AF_INET socket for ioctl() calls. */
122 struct globals {
123         unsigned abi_ver;       /* userland - kernel ABI version */
124         smallint hwaddr_set;    /* Master's hwaddr is set */
125         struct dev_data master;
126         struct dev_data slave;
127 };
128 #define G (*ptr_to_globals)
129 #define abi_ver    (G.abi_ver   )
130 #define hwaddr_set (G.hwaddr_set)
131 #define master     (G.master    )
132 #define slave      (G.slave     )
133 #define INIT_G() do { \
134         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
135 } while (0)
136
137
138 /* NOINLINEs are placed where it results in smaller code (gcc 4.3.1) */
139
140 static int ioctl_on_skfd(unsigned request, struct ifreq *ifr)
141 {
142         return ioctl(skfd, request, ifr);
143 }
144
145 static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname)
146 {
147         strncpy_IFNAMSIZ(ifr->ifr_name, ifname);
148         return ioctl_on_skfd(request, ifr);
149 }
150
151 static int get_if_settings(char *ifname, struct dev_data *dd)
152 {
153         int res;
154
155         res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname);
156         res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname);
157         res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname);
158
159         return res;
160 }
161
162 static int get_slave_flags(char *slave_ifname)
163 {
164         return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname);
165 }
166
167 static int set_hwaddr(char *ifname, struct sockaddr *hwaddr)
168 {
169         struct ifreq ifr;
170
171         memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr));
172         return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname);
173 }
174
175 static int set_mtu(char *ifname, int mtu)
176 {
177         struct ifreq ifr;
178
179         ifr.ifr_mtu = mtu;
180         return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname);
181 }
182
183 static int set_if_flags(char *ifname, int flags)
184 {
185         struct ifreq ifr;
186
187         ifr.ifr_flags = flags;
188         return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname);
189 }
190
191 static int set_if_up(char *ifname, int flags)
192 {
193         int res = set_if_flags(ifname, flags | IFF_UP);
194         if (res)
195                 bb_perror_msg("%s: can't up", ifname);
196         return res;
197 }
198
199 static int set_if_down(char *ifname, int flags)
200 {
201         int res = set_if_flags(ifname, flags & ~IFF_UP);
202         if (res)
203                 bb_perror_msg("%s: can't down", ifname);
204         return res;
205 }
206
207 static int clear_if_addr(char *ifname)
208 {
209         struct ifreq ifr;
210
211         ifr.ifr_addr.sa_family = AF_INET;
212         memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data));
213         return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname);
214 }
215
216 static int set_if_addr(char *master_ifname, char *slave_ifname)
217 {
218 #if (SIOCGIFADDR | SIOCSIFADDR \
219   | SIOCGIFDSTADDR | SIOCSIFDSTADDR \
220   | SIOCGIFBRDADDR | SIOCSIFBRDADDR \
221   | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff
222 #define INT uint16_t
223 #else
224 #define INT int
225 #endif
226         static const struct {
227                 INT g_ioctl;
228                 INT s_ioctl;
229         } ifra[] = {
230                 { SIOCGIFADDR,    SIOCSIFADDR    },
231                 { SIOCGIFDSTADDR, SIOCSIFDSTADDR },
232                 { SIOCGIFBRDADDR, SIOCSIFBRDADDR },
233                 { SIOCGIFNETMASK, SIOCSIFNETMASK },
234         };
235
236         struct ifreq ifr;
237         int res;
238         unsigned i;
239
240         for (i = 0; i < ARRAY_SIZE(ifra); i++) {
241                 res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname);
242                 if (res < 0) {
243                         ifr.ifr_addr.sa_family = AF_INET;
244                         memset(ifr.ifr_addr.sa_data, 0,
245                                sizeof(ifr.ifr_addr.sa_data));
246                 }
247
248                 res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname);
249                 if (res < 0)
250                         return res;
251         }
252
253         return 0;
254 }
255
256 static void change_active(char *master_ifname, char *slave_ifname)
257 {
258         struct ifreq ifr;
259
260         if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
261                 bb_error_msg_and_die("%s is not a slave", slave_ifname);
262         }
263
264         strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
265         if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname)
266          && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr)
267         ) {
268                 bb_perror_msg_and_die(
269                         "master %s, slave %s: can't "
270                         "change active",
271                         master_ifname, slave_ifname);
272         }
273 }
274
275 static NOINLINE int enslave(char *master_ifname, char *slave_ifname)
276 {
277         struct ifreq ifr;
278         int res;
279
280         if (slave.flags.ifr_flags & IFF_SLAVE) {
281                 bb_error_msg(
282                         "%s is already a slave",
283                         slave_ifname);
284                 return 1;
285         }
286
287         res = set_if_down(slave_ifname, slave.flags.ifr_flags);
288         if (res)
289                 return res;
290
291         if (abi_ver < 2) {
292                 /* Older bonding versions would panic if the slave has no IP
293                  * address, so get the IP setting from the master.
294                  */
295                 res = set_if_addr(master_ifname, slave_ifname);
296                 if (res) {
297                         bb_perror_msg("%s: can't set address", slave_ifname);
298                         return res;
299                 }
300         } else {
301                 res = clear_if_addr(slave_ifname);
302                 if (res) {
303                         bb_perror_msg("%s: can't clear address", slave_ifname);
304                         return res;
305                 }
306         }
307
308         if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) {
309                 res = set_mtu(slave_ifname, master.mtu.ifr_mtu);
310                 if (res) {
311                         bb_perror_msg("%s: can't set MTU", slave_ifname);
312                         return res;
313                 }
314         }
315
316         if (hwaddr_set) {
317                 /* Master already has an hwaddr
318                  * so set it's hwaddr to the slave
319                  */
320                 if (abi_ver < 1) {
321                         /* The driver is using an old ABI, so
322                          * the application sets the slave's
323                          * hwaddr
324                          */
325                         if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) {
326                                 bb_perror_msg("%s: can't set hw address",
327                                                 slave_ifname);
328                                 goto undo_mtu;
329                         }
330
331                         /* For old ABI the application needs to bring the
332                          * slave back up
333                          */
334                         if (set_if_up(slave_ifname, slave.flags.ifr_flags))
335                                 goto undo_slave_mac;
336                 }
337                 /* The driver is using a new ABI,
338                  * so the driver takes care of setting
339                  * the slave's hwaddr and bringing
340                  * it up again
341                  */
342         } else {
343                 /* No hwaddr for master yet, so
344                  * set the slave's hwaddr to it
345                  */
346                 if (abi_ver < 1) {
347                         /* For old ABI, the master needs to be
348                          * down before setting it's hwaddr
349                          */
350                         if (set_if_down(master_ifname, master.flags.ifr_flags))
351                                 goto undo_mtu;
352                 }
353
354                 if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) {
355                         bb_error_msg("%s: can't set hw address",
356                                 master_ifname);
357                         goto undo_mtu;
358                 }
359
360                 if (abi_ver < 1) {
361                         /* For old ABI, bring the master
362                          * back up
363                          */
364                         if (set_if_up(master_ifname, master.flags.ifr_flags))
365                                 goto undo_master_mac;
366                 }
367
368                 hwaddr_set = 1;
369         }
370
371         /* Do the real thing */
372         strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
373         if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname)
374          && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr)
375         ) {
376                 goto undo_master_mac;
377         }
378
379         return 0;
380
381 /* rollback (best effort) */
382  undo_master_mac:
383         set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr));
384         hwaddr_set = 0;
385         goto undo_mtu;
386
387  undo_slave_mac:
388         set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr));
389  undo_mtu:
390         set_mtu(slave_ifname, slave.mtu.ifr_mtu);
391         return 1;
392 }
393
394 static int release(char *master_ifname, char *slave_ifname)
395 {
396         struct ifreq ifr;
397         int res = 0;
398
399         if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
400                 bb_error_msg("%s is not a slave", slave_ifname);
401                 return 1;
402         }
403
404         strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
405         if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0
406          && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0
407         ) {
408                 return 1;
409         }
410         if (abi_ver < 1) {
411                 /* The driver is using an old ABI, so we'll set the interface
412                  * down to avoid any conflicts due to same MAC/IP
413                  */
414                 res = set_if_down(slave_ifname, slave.flags.ifr_flags);
415         }
416
417         /* set to default mtu */
418         set_mtu(slave_ifname, 1500);
419
420         return res;
421 }
422
423 static NOINLINE void get_drv_info(char *master_ifname)
424 {
425         struct ifreq ifr;
426         struct ethtool_drvinfo info;
427
428         memset(&ifr, 0, sizeof(ifr));
429         ifr.ifr_data = (caddr_t)&info;
430         info.cmd = ETHTOOL_GDRVINFO;
431         /* both fields are 32 bytes long (long enough) */
432         strcpy(info.driver, "ifenslave");
433         strcpy(info.fw_version, utoa(BOND_ABI_VERSION));
434         if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) {
435                 if (errno == EOPNOTSUPP)
436                         return;
437                 bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
438         }
439
440         abi_ver = bb_strtou(info.fw_version, NULL, 0);
441         if (errno)
442                 bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
443 }
444
445 int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
446 int ifenslave_main(int argc UNUSED_PARAM, char **argv)
447 {
448         char *master_ifname, *slave_ifname;
449         int rv;
450         int res;
451         unsigned opt;
452         enum {
453                 OPT_c = (1 << 0),
454                 OPT_d = (1 << 1),
455                 OPT_f = (1 << 2),
456         };
457 #if ENABLE_LONG_OPTS
458         static const char ifenslave_longopts[] ALIGN1 =
459                 "change-active\0"  No_argument "c"
460                 "detach\0"         No_argument "d"
461                 "force\0"          No_argument "f"
462                 /* "all-interfaces\0" No_argument "a" */
463                 ;
464
465         applet_long_options = ifenslave_longopts;
466 #endif
467         INIT_G();
468
469         opt = getopt32(argv, "cdfa");
470         argv += optind;
471         if (opt & (opt-1)) /* Only one option can be given */
472                 bb_show_usage();
473
474         master_ifname = *argv++;
475
476         /* No interface names - show all interfaces. */
477         if (!master_ifname) {
478                 display_interfaces(NULL);
479                 return EXIT_SUCCESS;
480         }
481
482         /* Open a basic socket */
483         xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd);
484
485         /* Exchange abi version with bonding module */
486         get_drv_info(master_ifname);
487
488         slave_ifname = *argv++;
489         if (!slave_ifname) {
490                 if (opt & (OPT_d|OPT_c)) {
491                         /* --change or --detach, and no slaves given -
492                          * show all interfaces. */
493                         display_interfaces(slave_ifname /* == NULL */);
494                         return 2; /* why 2? */
495                 }
496                 /* A single arg means show the
497                  * configuration for this interface
498                  */
499                 display_interfaces(master_ifname);
500                 return EXIT_SUCCESS;
501         }
502
503         if (get_if_settings(master_ifname, &master)) {
504                 /* Probably a good reason not to go on */
505                 bb_perror_msg_and_die("%s: can't get settings", master_ifname);
506         }
507
508         /* Check if master is indeed a master;
509          * if not then fail any operation
510          */
511         if (!(master.flags.ifr_flags & IFF_MASTER))
512                 bb_error_msg_and_die("%s is not a master", master_ifname);
513
514         /* Check if master is up; if not then fail any operation */
515         if (!(master.flags.ifr_flags & IFF_UP))
516                 bb_error_msg_and_die("%s is not up", master_ifname);
517
518 #ifdef WHY_BOTHER
519         /* Neither -c[hange] nor -d[etach] -> it's "enslave" then;
520          * and -f[orce] is not there too. Check that it's ethernet. */
521         if (!(opt & (OPT_d|OPT_c|OPT_f)) {
522                 /* The family '1' is ARPHRD_ETHER for ethernet. */
523                 if (master.hwaddr.ifr_hwaddr.sa_family != 1) {
524                         bb_error_msg_and_die(
525                                 "%s is not ethernet-like (-f overrides)",
526                                 master_ifname);
527                 }
528         }
529 #endif
530
531         /* Accepts only one slave */
532         if (opt & OPT_c) {
533                 /* Change active slave */
534                 if (get_slave_flags(slave_ifname)) {
535                         bb_perror_msg_and_die(
536                                 "%s: can't get flags", slave_ifname);
537                 }
538                 change_active(master_ifname, slave_ifname);
539                 return EXIT_SUCCESS;
540         }
541
542         /* Accepts multiple slaves */
543         res = 0;
544         do {
545                 if (opt & OPT_d) {
546                         /* Detach a slave interface from the master */
547                         rv = get_slave_flags(slave_ifname);
548                         if (rv) {
549                                 /* Can't work with this slave, */
550                                 /* remember the error and skip it */
551                                 bb_perror_msg(
552                                         "skipping %s: can't get flags",
553                                         slave_ifname);
554                                 res = rv;
555                                 continue;
556                         }
557                         rv = release(master_ifname, slave_ifname);
558                         if (rv) {
559                                 bb_perror_msg("can't release %s from %s",
560                                         slave_ifname, master_ifname);
561                                 res = rv;
562                         }
563                 } else {
564                         /* Attach a slave interface to the master */
565                         rv = get_if_settings(slave_ifname, &slave);
566                         if (rv) {
567                                 /* Can't work with this slave, */
568                                 /* remember the error and skip it */
569                                 bb_perror_msg(
570                                         "skipping %s: can't get settings",
571                                         slave_ifname);
572                                 res = rv;
573                                 continue;
574                         }
575                         rv = enslave(master_ifname, slave_ifname);
576                         if (rv) {
577                                 bb_perror_msg("can't enslave %s to %s",
578                                         slave_ifname, master_ifname);
579                                 res = rv;
580                         }
581                 }
582         } while ((slave_ifname = *argv++) != NULL);
583
584         if (ENABLE_FEATURE_CLEAN_UP) {
585                 close(skfd);
586         }
587
588         return res;
589 }