123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
- #include "libbb.h"
- #include <linux/if.h>
- #include <linux/if_bonding.h>
- #include <linux/sockios.h>
- #include "fix_u32.h"
- #include <linux/ethtool.h>
- #ifndef BOND_ABI_VERSION
- # define BOND_ABI_VERSION 2
- #endif
- #ifndef IFNAMSIZ
- # define IFNAMSIZ 16
- #endif
- struct dev_data {
- struct ifreq mtu, flags, hwaddr;
- };
- enum { skfd = 3 };
- struct globals {
- unsigned abi_ver;
- smallint hwaddr_set;
- struct dev_data master;
- struct dev_data slave;
- };
- #define G (*ptr_to_globals)
- #define abi_ver (G.abi_ver )
- #define hwaddr_set (G.hwaddr_set)
- #define master (G.master )
- #define slave (G.slave )
- #define INIT_G() do { \
- SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
- } while (0)
- static int ioctl_on_skfd(unsigned request, struct ifreq *ifr)
- {
- return ioctl(skfd, request, ifr);
- }
- static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname)
- {
- strncpy_IFNAMSIZ(ifr->ifr_name, ifname);
- return ioctl_on_skfd(request, ifr);
- }
- static int get_if_settings(char *ifname, struct dev_data *dd)
- {
- int res;
- res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname);
- res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname);
- res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname);
- return res;
- }
- static int get_slave_flags(char *slave_ifname)
- {
- return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname);
- }
- static int set_hwaddr(char *ifname, struct sockaddr *hwaddr)
- {
- struct ifreq ifr;
- memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr));
- return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname);
- }
- static int set_mtu(char *ifname, int mtu)
- {
- struct ifreq ifr;
- ifr.ifr_mtu = mtu;
- return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname);
- }
- static int set_if_flags(char *ifname, int flags)
- {
- struct ifreq ifr;
- ifr.ifr_flags = flags;
- return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname);
- }
- static int set_if_up(char *ifname, int flags)
- {
- int res = set_if_flags(ifname, flags | IFF_UP);
- if (res)
- bb_perror_msg("%s: can't up", ifname);
- return res;
- }
- static int set_if_down(char *ifname, int flags)
- {
- int res = set_if_flags(ifname, flags & ~IFF_UP);
- if (res)
- bb_perror_msg("%s: can't down", ifname);
- return res;
- }
- static int clear_if_addr(char *ifname)
- {
- struct ifreq ifr;
- ifr.ifr_addr.sa_family = AF_INET;
- memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data));
- return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname);
- }
- static int set_if_addr(char *master_ifname, char *slave_ifname)
- {
- #if (SIOCGIFADDR | SIOCSIFADDR \
- | SIOCGIFDSTADDR | SIOCSIFDSTADDR \
- | SIOCGIFBRDADDR | SIOCSIFBRDADDR \
- | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff
- #define INT uint16_t
- #else
- #define INT int
- #endif
- static const struct {
- INT g_ioctl;
- INT s_ioctl;
- } ifra[] = {
- { SIOCGIFADDR, SIOCSIFADDR },
- { SIOCGIFDSTADDR, SIOCSIFDSTADDR },
- { SIOCGIFBRDADDR, SIOCSIFBRDADDR },
- { SIOCGIFNETMASK, SIOCSIFNETMASK },
- };
- struct ifreq ifr;
- int res;
- unsigned i;
- for (i = 0; i < ARRAY_SIZE(ifra); i++) {
- res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname);
- if (res < 0) {
- ifr.ifr_addr.sa_family = AF_INET;
- memset(ifr.ifr_addr.sa_data, 0,
- sizeof(ifr.ifr_addr.sa_data));
- }
- res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname);
- if (res < 0)
- return res;
- }
- return 0;
- }
- static void change_active(char *master_ifname, char *slave_ifname)
- {
- struct ifreq ifr;
- if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
- bb_error_msg_and_die("%s is not a slave", slave_ifname);
- }
- strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
- if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname)
- && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr)
- ) {
- bb_perror_msg_and_die(
- "master %s, slave %s: can't "
- "change active",
- master_ifname, slave_ifname);
- }
- }
- static NOINLINE int enslave(char *master_ifname, char *slave_ifname)
- {
- struct ifreq ifr;
- int res;
- if (slave.flags.ifr_flags & IFF_SLAVE) {
- bb_error_msg(
- "%s is already a slave",
- slave_ifname);
- return 1;
- }
- res = set_if_down(slave_ifname, slave.flags.ifr_flags);
- if (res)
- return res;
- if (abi_ver < 2) {
-
- res = set_if_addr(master_ifname, slave_ifname);
- if (res) {
- bb_perror_msg("%s: can't set address", slave_ifname);
- return res;
- }
- } else {
- res = clear_if_addr(slave_ifname);
- if (res) {
- bb_perror_msg("%s: can't clear address", slave_ifname);
- return res;
- }
- }
- if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) {
- res = set_mtu(slave_ifname, master.mtu.ifr_mtu);
- if (res) {
- bb_perror_msg("%s: can't set MTU", slave_ifname);
- return res;
- }
- }
- if (hwaddr_set) {
-
- if (abi_ver < 1) {
-
- if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) {
- bb_perror_msg("%s: can't set hw address",
- slave_ifname);
- goto undo_mtu;
- }
-
- if (set_if_up(slave_ifname, slave.flags.ifr_flags))
- goto undo_slave_mac;
- }
-
- } else {
-
- if (abi_ver < 1) {
-
- if (set_if_down(master_ifname, master.flags.ifr_flags))
- goto undo_mtu;
- }
- if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) {
- bb_error_msg("%s: can't set hw address",
- master_ifname);
- goto undo_mtu;
- }
- if (abi_ver < 1) {
-
- if (set_if_up(master_ifname, master.flags.ifr_flags))
- goto undo_master_mac;
- }
- hwaddr_set = 1;
- }
-
- strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
- if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname)
- && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr)
- ) {
- goto undo_master_mac;
- }
- return 0;
- undo_master_mac:
- set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr));
- hwaddr_set = 0;
- goto undo_mtu;
- undo_slave_mac:
- set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr));
- undo_mtu:
- set_mtu(slave_ifname, slave.mtu.ifr_mtu);
- return 1;
- }
- static int release(char *master_ifname, char *slave_ifname)
- {
- struct ifreq ifr;
- int res = 0;
- if (!(slave.flags.ifr_flags & IFF_SLAVE)) {
- bb_error_msg("%s is not a slave", slave_ifname);
- return 1;
- }
- strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname);
- if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0
- && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0
- ) {
- return 1;
- }
- if (abi_ver < 1) {
-
- res = set_if_down(slave_ifname, slave.flags.ifr_flags);
- }
-
- set_mtu(slave_ifname, 1500);
- return res;
- }
- static NOINLINE void get_drv_info(char *master_ifname)
- {
- struct ifreq ifr;
- struct ethtool_drvinfo info;
- memset(&ifr, 0, sizeof(ifr));
- ifr.ifr_data = (caddr_t)&info;
- info.cmd = ETHTOOL_GDRVINFO;
-
- strcpy(info.driver, "ifenslave");
- strcpy(info.fw_version, utoa(BOND_ABI_VERSION));
- if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) {
- if (errno == EOPNOTSUPP)
- return;
- bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
- }
- abi_ver = bb_strtou(info.fw_version, NULL, 0);
- if (errno)
- bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname);
- }
- int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int ifenslave_main(int argc UNUSED_PARAM, char **argv)
- {
- char *master_ifname, *slave_ifname;
- int rv;
- int res;
- unsigned opt;
- enum {
- OPT_c = (1 << 0),
- OPT_d = (1 << 1),
- OPT_f = (1 << 2),
- };
- #if ENABLE_LONG_OPTS
- static const char ifenslave_longopts[] ALIGN1 =
- "change-active\0" No_argument "c"
- "detach\0" No_argument "d"
- "force\0" No_argument "f"
-
- ;
- applet_long_options = ifenslave_longopts;
- #endif
- INIT_G();
- opt = getopt32(argv, "cdfa");
- argv += optind;
- if (opt & (opt-1))
- bb_show_usage();
- master_ifname = *argv++;
-
- if (!master_ifname) {
- display_interfaces(NULL);
- return EXIT_SUCCESS;
- }
-
- xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd);
-
- get_drv_info(master_ifname);
- slave_ifname = *argv++;
- if (!slave_ifname) {
- if (opt & (OPT_d|OPT_c)) {
-
- display_interfaces(slave_ifname );
- return 2;
- }
-
- display_interfaces(master_ifname);
- return EXIT_SUCCESS;
- }
- if (get_if_settings(master_ifname, &master)) {
-
- bb_perror_msg_and_die("%s: can't get settings", master_ifname);
- }
-
- if (!(master.flags.ifr_flags & IFF_MASTER))
- bb_error_msg_and_die("%s is not a master", master_ifname);
-
- if (!(master.flags.ifr_flags & IFF_UP))
- bb_error_msg_and_die("%s is not up", master_ifname);
- #ifdef WHY_BOTHER
-
- if (!(opt & (OPT_d|OPT_c|OPT_f))) {
-
- if (master.hwaddr.ifr_hwaddr.sa_family != 1) {
- bb_error_msg_and_die(
- "%s is not ethernet-like (-f overrides)",
- master_ifname);
- }
- }
- #endif
-
- if (opt & OPT_c) {
-
- if (get_slave_flags(slave_ifname)) {
- bb_perror_msg_and_die(
- "%s: can't get flags", slave_ifname);
- }
- change_active(master_ifname, slave_ifname);
- return EXIT_SUCCESS;
- }
-
- res = 0;
- do {
- if (opt & OPT_d) {
-
- rv = get_slave_flags(slave_ifname);
- if (rv) {
-
-
- bb_perror_msg(
- "skipping %s: can't get %s",
- slave_ifname, "flags");
- res = rv;
- continue;
- }
- rv = release(master_ifname, slave_ifname);
- if (rv) {
- bb_perror_msg("can't release %s from %s",
- slave_ifname, master_ifname);
- res = rv;
- }
- } else {
-
- rv = get_if_settings(slave_ifname, &slave);
- if (rv) {
-
-
- bb_perror_msg(
- "skipping %s: can't get %s",
- slave_ifname, "settings");
- res = rv;
- continue;
- }
- rv = enslave(master_ifname, slave_ifname);
- if (rv) {
- bb_perror_msg("can't enslave %s to %s",
- slave_ifname, master_ifname);
- res = rv;
- }
- }
- } while ((slave_ifname = *argv++) != NULL);
- if (ENABLE_FEATURE_CLEAN_UP) {
- close(skfd);
- }
- return res;
- }
|