X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fnameif.c;h=046e308c5fe423fc45a7f29dc2da7af65e994b40;hb=765b0eed3ef29a80115708c3249d3a541509cd24;hp=1edd64111b0149a2a0ad2c949f6c1b263dbb0fad;hpb=f03c933e473760cb3776aee8283ecb3e4bddf097;p=oweals%2Fbusybox.git diff --git a/networking/nameif.c b/networking/nameif.c index 1edd64111..046e308c5 100644 --- a/networking/nameif.c +++ b/networking/nameif.c @@ -1,217 +1,234 @@ -/* +/* 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 - * Glenn McGrath - * - * 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 * + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */ -#include -#include -#include - -#include -#include -#include -#include +#include "libbb.h" +#include #include #include +#include -#include "busybox.h" +#ifndef IFNAMSIZ +#define IFNAMSIZ 16 +#endif + +/* Taken from linux/sockios.h */ +#define SIOCSIFNAME 0x8923 /* set interface name */ -/* set interface name, from */ -#define SIOCSIFNAME 0x8923 -/* Octets in one ethernet addr, from */ +/* Octets in one Ethernet addr, from */ #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 **pprev; +typedef struct ethtable_s { + struct ethtable_s *next; + struct ethtable_s *prev; char *ifname; struct ether_addr *mac; -} mactable_t; +#if ENABLE_FEATURE_NAMEIF_EXTENDED + char *bus_info; + char *driver; +#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) */ +}; +#define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */ +#endif -static void serror_msg_and_die(const char use_syslog, const char *s, ...) -{ - va_list ap; - va_start(ap, s); +static void nameif_parse_selector(ethtable_t *ch, char *selector) +{ + struct ether_addr *lmac; +#if ENABLE_FEATURE_NAMEIF_EXTENDED + int found_selector = 0; - if (use_syslog) { - openlog("nameif", 0, LOG_LOCAL0); - syslog(LOG_ERR, s, ap); - closelog(); - } else { - vfprintf(stderr, s, ap); - putc('\n', stderr); + while (*selector) { + char *next; +#endif + selector = skip_whitespace(selector); +#if ENABLE_FEATURE_NAMEIF_EXTENDED + 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 { +#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; } + if (found_selector == 0) + bb_error_msg_and_die("no selectors found for %s", ch->ifname); +#endif +} - va_end(ap); - - exit(EXIT_FAILURE); +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; } -int nameif_main(int argc, char **argv) +#if ENABLE_FEATURE_CLEAN_UP +static void delete_eth_table(ethtable_t *ch) { - mactable_t *clist = NULL; - FILE *ifh; - char *fname = "/etc/mactab"; - char *line; - unsigned short linenum = 0; - unsigned char use_syslog = 0; - int ctl_sk = -1; - int opt; - - static struct option opts[] = { - {"syslog", 0, NULL, 's'}, - {"configfile", 1, NULL, 'c'}, - {NULL}, - }; + 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 - while ((opt = getopt_long(argc, argv, "c:s", opts, NULL)) != -1) { - switch (opt) { - case 'c': - fname = optarg; - break; - case 's': - use_syslog = 1; - break; - default: - show_usage(); - } +int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int nameif_main(int argc, 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); + /* Why not just "="? I assume logging to stderr + * can't hurt. 2>/dev/null if you don't like it: */ + logmode |= LOGMODE_SYSLOG; } + argc -= optind; + argv += optind; - if ((argc - optind) & 1) { - show_usage(); - } + if (argc & 1) + bb_show_usage(); - if (optind < argc) { - while (optind < argc) { - struct ether_addr *mac; - mactable_t *ch; - - if (strlen(argv[optind]) > IF_NAMESIZE) { - serror_msg_and_die(use_syslog, "interface name `%s' too long", argv[optind]); - } - optind++; - mac = ether_aton(argv[optind]); - if (mac == NULL) { - serror_msg_and_die(use_syslog, "cannot parse MAC %s", argv[optind]); - } - ch = xcalloc(1, sizeof(mactable_t)); - ch->ifname = strdup(argv[optind - 1]); - ch->mac = xcalloc(1, ETH_ALEN); - memcpy(ch->mac, &mac, ETH_ALEN); - optind++; - if (clist) - clist->pprev = &ch->next; - ch->next = clist; - ch->pprev = &clist; - clist = ch; + if (argc) { + while (*argv) { + char *ifname = xstrdup(*argv++); + prepend_new_eth_table(&clist, ifname, *argv++); } } else { - ifh = xfopen(fname, "r"); - - while ((line = get_line_from_file(ifh)) != NULL) { - struct ether_addr *mac; - mactable_t *ch; - char *line_ptr; - unsigned short name_length; - - line_ptr = line + strspn(line, " \t"); - if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) - continue; - name_length = strcspn(line_ptr, " \t"); - if (name_length > IF_NAMESIZE) { - serror_msg_and_die(use_syslog, "interface name `%s' too long", argv[optind]); - } - ch = xcalloc(1, sizeof(mactable_t)); - ch->ifname = strndup(line_ptr, name_length); - line_ptr += name_length; - line_ptr += strspn(line_ptr, " \t"); - name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:"); - line_ptr[name_length] = '\0'; - mac = ether_aton(line_ptr); - if (mac == NULL) { - serror_msg_and_die(use_syslog, "cannot parse MAC %s", argv[optind]); - } - ch->mac = xcalloc(1, ETH_ALEN); - memcpy(ch->mac, mac, ETH_ALEN); - if (clist) - clist->pprev = &ch->next; - ch->next = clist; - ch->pprev = &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); } - ifh = xfopen("/proc/net/dev", "r"); - while ((line = get_line_from_file(ifh)) != NULL) { - char *line_ptr; - unsigned short iface_name_length; + 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; - mactable_t *ch = NULL; +#if ENABLE_FEATURE_NAMEIF_EXTENDED + struct ethtool_drvinfo drvinfo; +#endif + if (parser->lineno < 2) + continue; /* Skip the first two lines */ - linenum++; - if (linenum < 3) - continue; - line_ptr = line + strspn(line, " \t"); - if (line_ptr[0] == '\n') - continue; - iface_name_length = strcspn(line_ptr, ":"); - if (ctl_sk < 0) - ctl_sk = socket(PF_INET, SOCK_DGRAM, 0); + /* Find the current interface name and copy it to ifr.ifr_name */ memset(&ifr, 0, sizeof(struct ifreq)); - strncpy(ifr.ifr_name, line_ptr, iface_name_length); - if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr) < 0) { - serror_msg_and_die(use_syslog, "cannot change name of %s to %s: %s", ifr.ifr_name, ch->ifname, strerror(errno)); + strncpy_IFNAMSIZ(ifr.ifr_name, token[0]); + +#if ENABLE_FEATURE_NAMEIF_EXTENDED + /* Check for driver etc. */ + memset(&drvinfo, 0, sizeof(struct ethtool_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); + + /* 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; +#endif + if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0) + continue; + /* if we came here, all selectors have matched */ + break; } - for (ch = clist; ch; ch = ch->next) - if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN)) - break; - if (ch == NULL) { + /* Nothing found for current interface */ + if (!ch) continue; - } - strcpy(ifr.ifr_newname, ch->ifname); - if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0) {; - serror_msg_and_die(use_syslog, "cannot change name of %s to %s: %s", ifr.ifr_name, ch->ifname, strerror(errno)); + 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); } - *ch->pprev = ch->next; - free(ch); - free(line); - } - fclose(ifh); - - while (clist) { - mactable_t *ch; - - ch = clist; - clist = clist->next; - free(ch); + /* Remove list entry of renamed interface */ + 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) { + for (ch = clist; ch; ch = ch->next) + delete_eth_table(ch); + config_close(parser); + }; return 0; }