123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #include "libbb.h"
- #include <syslog.h>
- #include <net/if.h>
- #include <netinet/ether.h>
- #include <linux/sockios.h>
- #ifndef IFNAMSIZ
- #define IFNAMSIZ 16
- #endif
- #define SIOCSIFNAME 0x8923
- #define ETH_ALEN 6
- #ifndef ifr_newname
- #define ifr_newname ifr_ifru.ifru_slave
- #endif
- typedef struct ethtable_s {
- struct ethtable_s *next;
- struct ethtable_s *prev;
- char *ifname;
- struct ether_addr *mac;
- #if ENABLE_FEATURE_NAMEIF_EXTENDED
- char *bus_info;
- char *driver;
- int32_t phy_address;
- #endif
- } ethtable_t;
- #if ENABLE_FEATURE_NAMEIF_EXTENDED
- #define ETHTOOL_BUSINFO_LEN 32
- struct ethtool_drvinfo {
- uint32_t cmd;
- char driver[32];
- char version[32];
- char fw_version[32];
- char bus_info[ETHTOOL_BUSINFO_LEN];
-
- char reserved1[32];
- char reserved2[16];
- uint32_t n_stats;
- uint32_t testinfo_len;
- uint32_t eedump_len;
- uint32_t regdump_len;
- };
- struct ethtool_cmd {
- uint32_t cmd;
- uint32_t supported;
- uint32_t advertising;
- uint16_t speed;
- uint8_t duplex;
- uint8_t port;
- uint8_t phy_address;
- uint8_t transceiver;
- uint8_t autoneg;
- uint32_t maxtxpkt;
- uint32_t maxrxpkt;
- uint16_t speed_hi;
- uint16_t reserved2;
- uint32_t reserved[3];
- };
- #define ETHTOOL_GSET 0x00000001
- #define ETHTOOL_GDRVINFO 0x00000003
- #endif
- static void nameif_parse_selector(ethtable_t *ch, char *selector)
- {
- struct ether_addr *lmac;
- #if ENABLE_FEATURE_NAMEIF_EXTENDED
- int found_selector = 0;
- while (*selector) {
- char *next;
- #endif
- selector = skip_whitespace(selector);
- #if ENABLE_FEATURE_NAMEIF_EXTENDED
- ch->phy_address = -1;
- if (*selector == '\0')
- break;
-
- next = skip_non_whitespace(selector);
- if (*next)
- *next++ = '\0';
-
- if (is_prefixed_with(selector, "bus=")) {
- ch->bus_info = xstrdup(selector + 4);
- found_selector++;
- } else if (is_prefixed_with(selector, "driver=")) {
- ch->driver = xstrdup(selector + 7);
- found_selector++;
- } else if (is_prefixed_with(selector, "phyaddr=")) {
- ch->phy_address = xatoi_positive(selector + 8);
- found_selector++;
- } else {
- #endif
- lmac = xmalloc(ETH_ALEN);
- ch->mac = ether_aton_r(selector + (is_prefixed_with(selector, "mac=") ? 4 : 0), lmac);
- if (ch->mac == NULL)
- bb_error_msg_and_die("can't parse %s", selector);
- #if ENABLE_FEATURE_NAMEIF_EXTENDED
- found_selector++;
- };
- selector = next;
- }
- if (found_selector == 0)
- bb_error_msg_and_die("no selectors found for %s", ch->ifname);
- #endif
- }
- static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
- {
- 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;
- }
- #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)
- {
- ethtable_t *clist = NULL;
- const char *fname = "/etc/mactab";
- int ctl_sk;
- ethtable_t *ch;
- parser_t *parser;
- char *token[2];
- if (1 & getopt32(argv, "sc:", &fname)) {
- openlog(applet_name, 0, LOG_LOCAL0);
-
- logmode |= LOGMODE_SYSLOG;
- }
- 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 {
- 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);
- }
- ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
- parser = config_open2("/proc/net/dev", xfopen_for_read);
- 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;
-
- memset(&ifr, 0, sizeof(struct ifreq));
- strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
- #if ENABLE_FEATURE_NAMEIF_EXTENDED
-
- memset(ð_settings, 0, sizeof(eth_settings));
- eth_settings.cmd = ETHTOOL_GSET;
- ifr.ifr_data = (caddr_t) ð_settings;
- ioctl(ctl_sk, SIOCETHTOOL, &ifr);
-
- memset(&drvinfo, 0, sizeof(drvinfo));
- drvinfo.cmd = ETHTOOL_GDRVINFO;
- ifr.ifr_data = (caddr_t) &drvinfo;
-
- ioctl(ctl_sk, SIOCETHTOOL, &ifr);
- #endif
- ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
-
- 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;
-
- goto found;
- }
-
- 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);
- }
-
- if (ch->prev != NULL)
- ch->prev->next = ch->next;
- else
- clist = ch->next;
- if (ch->next != NULL)
- ch->next->prev = ch->prev;
- if (ENABLE_FEATURE_CLEAN_UP)
- delete_eth_table(ch);
- }
- 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;
- }
|