ping[6]: accept and ignore -n. Code shrink
[oweals/busybox.git] / networking / nameif.c
index dca7c8c08f95e31be9c2b538151502ccb37fb170..9a8846dc0a534e583fbf50c43286201c343707d0 100644 (file)
+/* vi: set sw=4 ts=4: */
 /*
  * nameif.c - Naming Interfaces based on MAC address for busybox.
  *
- * Writen 2000 by Andi Kleen.
+ * Written 2000 by Andi Kleen.
  * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
- *                     Glenn McGrath <bug1@optushome.com.au>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- * 02111-1307 USA
+ *                     Glenn McGrath
+ * Extended matching support 2008 by Nico Erfurth <masta@perlgolf.de>
  *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
-
-#include <sys/syslog.h>
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <errno.h>
-#include <getopt.h>
-#include <stdlib.h>
-#include <string.h>
+//config:config NAMEIF
+//config:      bool "nameif"
+//config:      default y
+//config:      select PLATFORM_LINUX
+//config:      select FEATURE_SYSLOG
+//config:      help
+//config:        nameif is used to rename network interface by its MAC address.
+//config:        Renamed interfaces MUST be in the down state.
+//config:        It is possible to use a file (default: /etc/mactab)
+//config:        with list of new interface names and MACs.
+//config:        Maximum interface name length: IFNAMSIZ = 16
+//config:        File fields are separated by space or tab.
+//config:        File format:
+//config:        # Comment
+//config:        new_interface_name    XX:XX:XX:XX:XX:XX
+//config:
+//config:config FEATURE_NAMEIF_EXTENDED
+//config:      bool "Extended nameif"
+//config:      default y
+//config:      depends on NAMEIF
+//config:      help
+//config:        This extends the nameif syntax to support the bus_info, driver,
+//config:        phyaddr selectors. The syntax is compatible to the normal nameif.
+//config:        File format:
+//config:          new_interface_name  driver=asix bus=usb-0000:00:08.2-3
+//config:          new_interface_name  bus=usb-0000:00:08.2-3 00:80:C8:38:91:B5
+//config:          new_interface_name  phy_address=2 00:80:C8:38:91:B5
+//config:          new_interface_name  mac=00:80:C8:38:91:B5
+//config:          new_interface_name  00:80:C8:38:91:B5
+
+//usage:#define nameif_trivial_usage
+//usage:       IF_NOT_FEATURE_NAMEIF_EXTENDED(
+//usage:               "[-s] [-c FILE] [IFNAME HWADDR]..."
+//usage:       )
+//usage:       IF_FEATURE_NAMEIF_EXTENDED(
+//usage:               "[-s] [-c FILE] [IFNAME SELECTOR]..."
+//usage:       )
+//usage:#define nameif_full_usage "\n\n"
+//usage:       "Rename network interface while it in the down state."
+//usage:       IF_NOT_FEATURE_NAMEIF_EXTENDED(
+//usage:     "\nThe device with address HWADDR is renamed to IFACE."
+//usage:       )
+//usage:       IF_FEATURE_NAMEIF_EXTENDED(
+//usage:     "\nThe device matched by SELECTOR is renamed to IFACE."
+//usage:     "\nSELECTOR can be a combination of:"
+//usage:     "\n       driver=STRING"
+//usage:     "\n       bus=STRING"
+//usage:     "\n       phy_address=NUM"
+//usage:     "\n       [mac=]XX:XX:XX:XX:XX:XX"
+//usage:       )
+//usage:     "\n"
+//usage:     "\n       -c FILE Configuration file (default: /etc/mactab)"
+//usage:     "\n       -s      Log to syslog"
+//usage:
+//usage:#define nameif_example_usage
+//usage:       "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n"
+//usage:       " or\n"
+//usage:       "$ nameif -c /etc/my_mactab_file\n"
+
+#include "libbb.h"
+#include <syslog.h>
 #include <net/if.h>
 #include <netinet/ether.h>
+#include <linux/sockios.h>
 
-#include "busybox.h"
-
-/* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
-#ifndef IF_NAMESIZE
-#  ifdef IFNAMSIZ
-#    define IF_NAMESIZE IFNAMSIZ
-#  else
-#    define IF_NAMESIZE 16
-#  endif
+#ifndef IFNAMSIZ
+#define IFNAMSIZ 16
 #endif
 
-/* take from linux/sockios.h */
-#define SIOCSIFNAME    0x8923  /* set interface name */
+/* Taken from linux/sockios.h */
+#define SIOCSIFNAME  0x8923  /* set interface name */
 
-/* Octets in one ethernet addr, from <linux/if_ether.h> */
-#define ETH_ALEN       6
+/* Octets in one Ethernet addr, from <linux/if_ether.h> */
+#define ETH_ALEN     6
 
 #ifndef ifr_newname
 #define ifr_newname ifr_ifru.ifru_slave
 #endif
 
-typedef struct mactable_s {
-       struct mactable_s *next;
-       struct mactable_s *prev;
+typedef struct ethtable_s {
+       struct ethtable_s *next;
+       struct ethtable_s *prev;
        char *ifname;
        struct ether_addr *mac;
-} mactable_t;
-
-static unsigned char use_syslog;
+#if ENABLE_FEATURE_NAMEIF_EXTENDED
+       char *bus_info;
+       char *driver;
+       int32_t phy_address;
+#endif
+} ethtable_t;
+
+#if ENABLE_FEATURE_NAMEIF_EXTENDED
+/* Cut'n'paste from ethtool.h */
+#define ETHTOOL_BUSINFO_LEN 32
+/* these strings are set to whatever the driver author decides... */
+struct ethtool_drvinfo {
+       uint32_t cmd;
+       char  driver[32]; /* driver short name, "tulip", "eepro100" */
+       char  version[32];  /* driver version string */
+       char  fw_version[32]; /* firmware version string, if applicable */
+       char  bus_info[ETHTOOL_BUSINFO_LEN];  /* Bus info for this IF. */
+       /* For PCI devices, use pci_dev->slot_name. */
+       char  reserved1[32];
+       char  reserved2[16];
+       uint32_t n_stats;  /* number of u64's from ETHTOOL_GSTATS */
+       uint32_t testinfo_len;
+       uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
+       uint32_t regdump_len;  /* Size of data from ETHTOOL_GREGS (bytes) */
+};
+
+struct ethtool_cmd {
+       uint32_t   cmd;
+       uint32_t   supported;      /* Features this interface supports */
+       uint32_t   advertising;    /* Features this interface advertises */
+       uint16_t   speed;          /* The forced speed, 10Mb, 100Mb, gigabit */
+       uint8_t    duplex;         /* Duplex, half or full */
+       uint8_t    port;           /* Which connector port */
+       uint8_t    phy_address;
+       uint8_t    transceiver;    /* Which transceiver to use */
+       uint8_t    autoneg;        /* Enable or disable autonegotiation */
+       uint32_t   maxtxpkt;       /* Tx pkts before generating tx int */
+       uint32_t   maxrxpkt;       /* Rx pkts before generating rx int */
+       uint16_t   speed_hi;
+       uint16_t   reserved2;
+       uint32_t   reserved[3];
+};
+
+#define ETHTOOL_GSET      0x00000001 /* Get settings. */
+#define ETHTOOL_GDRVINFO  0x00000003 /* Get driver info. */
+#endif
 
-static void serror(const char *s, ...) __attribute__ ((noreturn));
 
-static void serror(const char *s, ...)
+static void nameif_parse_selector(ethtable_t *ch, char *selector)
 {
-       va_list ap;
+       struct ether_addr *lmac;
+#if ENABLE_FEATURE_NAMEIF_EXTENDED
+       int found_selector = 0;
 
-       va_start(ap, s);
-
-       if (use_syslog) {
-               openlog(bb_applet_name, 0, LOG_LOCAL0);
-               vsyslog(LOG_ERR, s, ap);
-               closelog();
-       } else {
-               bb_verror_msg(s, ap);
-               putc('\n', stderr);
+       while (*selector) {
+               char *next;
+#endif
+               selector = skip_whitespace(selector);
+#if ENABLE_FEATURE_NAMEIF_EXTENDED
+               ch->phy_address = -1;
+               if (*selector == '\0')
+                       break;
+               /* Search for the end .... */
+               next = skip_non_whitespace(selector);
+               if (*next)
+                       *next++ = '\0';
+               /* Check for selectors, mac= is assumed */
+               if (strncmp(selector, "bus=", 4) == 0) {
+                       ch->bus_info = xstrdup(selector + 4);
+                       found_selector++;
+               } else if (strncmp(selector, "driver=", 7) == 0) {
+                       ch->driver = xstrdup(selector + 7);
+                       found_selector++;
+               } else if (strncmp(selector, "phyaddr=", 8) == 0) {
+                       ch->phy_address = xatoi_positive(selector + 8);
+                       found_selector++;
+               } else {
+#endif
+                       lmac = xmalloc(ETH_ALEN);
+                       ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) != 0 ? 0 : 4), lmac);
+                       if (ch->mac == NULL)
+                               bb_error_msg_and_die("can't parse %s", selector);
+#if  ENABLE_FEATURE_NAMEIF_EXTENDED
+                       found_selector++;
+               };
+               selector = next;
        }
-
-       va_end(ap);
-
-       exit(EXIT_FAILURE);
+       if (found_selector == 0)
+               bb_error_msg_and_die("no selectors found for %s", ch->ifname);
+#endif
 }
 
-/* Check ascii str_macaddr, convert and copy to *mac */
-struct ether_addr *cc_macaddr(char *str_macaddr)
+static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
 {
-       struct ether_addr *lmac, *mac;
-
-       lmac = ether_aton(str_macaddr);
-       if (lmac == NULL)
-               serror("cannot parse MAC %s", str_macaddr);
-       mac = xmalloc(ETH_ALEN);
-       memcpy(mac, lmac, ETH_ALEN);
-
-       return mac;
+       ethtable_t *ch;
+       if (strlen(ifname) >= IFNAMSIZ)
+               bb_error_msg_and_die("interface name '%s' too long", ifname);
+       ch = xzalloc(sizeof(*ch));
+       ch->ifname = xstrdup(ifname);
+       nameif_parse_selector(ch, selector);
+       ch->next = *clist;
+       if (*clist)
+               (*clist)->prev = ch;
+       *clist = ch;
 }
 
-int nameif_main(int argc, char **argv)
+#if ENABLE_FEATURE_CLEAN_UP
+static void delete_eth_table(ethtable_t *ch)
+{
+       free(ch->ifname);
+#if ENABLE_FEATURE_NAMEIF_EXTENDED
+       free(ch->bus_info);
+       free(ch->driver);
+#endif
+       free(ch->mac);
+       free(ch);
+};
+#else
+void delete_eth_table(ethtable_t *ch);
+#endif
+
+int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int nameif_main(int argc UNUSED_PARAM, char **argv)
 {
-       mactable_t *clist = NULL;
-       FILE *ifh;
+       ethtable_t *clist = NULL;
        const char *fname = "/etc/mactab";
-       char *line;
        int ctl_sk;
-       int opt;
-       int if_index = 1;
-       mactable_t *ch;
-
-
-       while ((opt = getopt(argc, argv, "c:s")) != -1) {
-               switch (opt) {
-               case 'c':
-                       fname = optarg;
-                       break;
-               case 's':
-                       use_syslog = 1;
-                       break;
-               default:
-                       bb_show_usage();
-               }
+       ethtable_t *ch;
+       parser_t *parser;
+       char *token[2];
+
+       if (1 & getopt32(argv, "sc:", &fname)) {
+               openlog(applet_name, 0, LOG_LOCAL0);
+               /* Why not just "="? I assume logging to stderr
+                * can't hurt. 2>/dev/null if you don't like it: */
+               logmode |= LOGMODE_SYSLOG;
        }
-
-       if ((argc - optind) & 1)
-               bb_show_usage();
-
-       if (optind < argc) {
-               char **a = argv + optind;
-
-               while (*a) {
-
-                       if (strlen(*a) > IF_NAMESIZE)
-                               serror("interface name `%s' too long", *a);
-                       ch = xcalloc(1, sizeof(mactable_t));
-                       ch->ifname = bb_xstrdup(*a++);
-                       ch->mac = cc_macaddr(*a++);
-                       if (clist)
-                               clist->prev = ch;
-                       ch->next = clist;
-                       clist = ch;
-               }
+       argv += optind;
+
+       if (argv[0]) {
+               do {
+                       if (!argv[1])
+                               bb_show_usage();
+                       prepend_new_eth_table(&clist, argv[0], argv[1]);
+                       argv += 2;
+               } while (*argv);
        } else {
-               ifh = bb_xfopen(fname, "r");
-
-               while ((line = bb_get_line_from_file(ifh)) != NULL) {
-                       char *line_ptr;
-                       size_t name_length;
-
-                       line_ptr = line + strspn(line, " \t");
-                       if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
-                               continue;
-                       name_length = strcspn(line_ptr, " \t");
-                       ch = xcalloc(1, sizeof(mactable_t));
-                       ch->ifname = bb_xstrndup(line_ptr, name_length);
-                       if (name_length > IF_NAMESIZE)
-                               serror("interface name `%s' too long", ch->ifname);
-                       line_ptr += name_length;
-                       line_ptr += strspn(line_ptr, " \t");
-                       name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
-                       line_ptr[name_length] = '\0';
-                       ch->mac = cc_macaddr(line_ptr);
-                       if (clist)
-                               clist->prev = ch;
-                       ch->next = clist;
-                       clist = ch;
-                       free(line);
-               }
-               fclose(ifh);
+               parser = config_open(fname);
+               while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL))
+                       prepend_new_eth_table(&clist, token[0], token[1]);
+               config_close(parser);
        }
 
-       if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
-               serror("socket: %m");
+       ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
+       parser = config_open2("/proc/net/dev", xfopen_for_read);
 
-       while (clist) {
+       while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) {
                struct ifreq ifr;
+#if  ENABLE_FEATURE_NAMEIF_EXTENDED
+               struct ethtool_drvinfo drvinfo;
+               struct ethtool_cmd eth_settings;
+#endif
+               if (parser->lineno <= 2)
+                       continue; /* Skip the first two lines */
+
+               /* Find the current interface name and copy it to ifr.ifr_name */
+               memset(&ifr, 0, sizeof(struct ifreq));
+               strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
+
+#if ENABLE_FEATURE_NAMEIF_EXTENDED
+               /* Check for phy address */
+               memset(&eth_settings, 0, sizeof(eth_settings));
+               eth_settings.cmd = ETHTOOL_GSET;
+               ifr.ifr_data = (caddr_t) &eth_settings;
+               ioctl(ctl_sk, SIOCETHTOOL, &ifr);
+
+               /* Check for driver etc. */
+               memset(&drvinfo, 0, sizeof(drvinfo));
+               drvinfo.cmd = ETHTOOL_GDRVINFO;
+               ifr.ifr_data = (caddr_t) &drvinfo;
+               /* Get driver and businfo first, so we have it in drvinfo */
+               ioctl(ctl_sk, SIOCETHTOOL, &ifr);
+#endif
+               ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
 
-               bzero(&ifr, sizeof(struct ifreq));
-               if_index++;
-               ifr.ifr_ifindex = if_index;
-
-               /* Get ifname by index or die */
-               if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
-                       break;
-
-               /* Has this device hwaddr? */
-               if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
-                       continue;
-
-               /* Search for mac like in ifr.ifr_hwaddr.sa_data */
-               for (ch = clist; ch; ch = ch->next)
-                       if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
-                               break;
-
-               /* Nothing found for current ifr.ifr_hwaddr.sa_data */
-               if (ch == NULL)
-                       continue;
-
-               strcpy(ifr.ifr_newname, ch->ifname);
-               if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
-                       serror("cannot change ifname %s to %s: %m",
-                                  ifr.ifr_name, ch->ifname);
-
+               /* Search the list for a matching device */
+               for (ch = clist; ch; ch = ch->next) {
+#if ENABLE_FEATURE_NAMEIF_EXTENDED
+                       if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
+                               continue;
+                       if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
+                               continue;
+                       if (ch->phy_address != -1 && ch->phy_address != eth_settings.phy_address)
+                               continue;
+#endif
+                       if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
+                               continue;
+                       /* if we came here, all selectors have matched */
+                       goto found;
+               }
+               /* Nothing found for current interface */
+               continue;
+ found:
+               if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
+                       strcpy(ifr.ifr_newname, ch->ifname);
+                       ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
+                                       "can't change ifname %s to %s",
+                                       ifr.ifr_name, ch->ifname);
+               }
                /* Remove list entry of renamed interface */
-               if (ch->prev != NULL) {
-                       (ch->prev)->next = ch->next;
-               } else {
+               if (ch->prev != NULL)
+                       ch->prev->next = ch->next;
+               else
                        clist = ch->next;
-               }
                if (ch->next != NULL)
-                       (ch->next)->prev = ch->prev;
-#ifdef CONFIG_FEATURE_CLEAN_UP
-               free(ch->ifname);
-               free(ch->mac);
-               free(ch);
-#endif
-       }
+                       ch->next->prev = ch->prev;
+               if (ENABLE_FEATURE_CLEAN_UP)
+                       delete_eth_table(ch);
+       } /* while */
+
+       if (ENABLE_FEATURE_CLEAN_UP) {
+               ethtable_t *next;
+               for (ch = clist; ch; ch = next) {
+                       next = ch->next;
+                       delete_eth_table(ch);
+               }
+               config_close(parser);
+       };
 
        return 0;
 }