mirror of
https://github.com/sheumann/hush.git
synced 2024-12-21 23:29:34 +00:00
nameif: extended matching (Nico Erfurth <masta@perlgolf.de>)
*: whitespace fixes function old new delta prepend_new_eth_table - 304 +304 nameif_main 620 684 +64 cc_macaddr 51 - -51 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 1/0 up/down: 368/-51) Total: 317 bytes
This commit is contained in:
parent
fcfb5c04bb
commit
f7be20e70c
@ -549,6 +549,19 @@ config NAMEIF
|
|||||||
# Comment
|
# Comment
|
||||||
new_interface_name XX:XX:XX:XX:XX:XX
|
new_interface_name XX:XX:XX:XX:XX:XX
|
||||||
|
|
||||||
|
config FEATURE_NAMEIF_EXTENDED
|
||||||
|
bool "Extended nameif"
|
||||||
|
default n
|
||||||
|
depends on NAMEIF
|
||||||
|
help
|
||||||
|
This extends the nameif syntax to support the bus_info and driver
|
||||||
|
checks. The syntax is compatible to the normal nameif.
|
||||||
|
File format:
|
||||||
|
new_interface_name driver=asix bus=usb-0000:00:08.2-3
|
||||||
|
new_interface_name bus=usb-0000:00:08.2-3 00:80:C8:38:91:B5
|
||||||
|
new_interface_name mac=00:80:C8:38:91:B5
|
||||||
|
new_interface_name 00:80:C8:38:91:B5
|
||||||
|
|
||||||
config NC
|
config NC
|
||||||
bool "nc"
|
bool "nc"
|
||||||
default n
|
default n
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <netinet/ether.h>
|
#include <netinet/ether.h>
|
||||||
|
#include <linux/sockios.h>
|
||||||
|
|
||||||
/* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
|
/* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
|
||||||
#ifndef IF_NAMESIZE
|
#ifndef IF_NAMESIZE
|
||||||
@ -34,138 +34,214 @@
|
|||||||
#define ifr_newname ifr_ifru.ifru_slave
|
#define ifr_newname ifr_ifru.ifru_slave
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct mactable_s {
|
typedef struct ethtable_s {
|
||||||
struct mactable_s *next;
|
struct ethtable_s *next;
|
||||||
struct mactable_s *prev;
|
struct ethtable_s *prev;
|
||||||
char *ifname;
|
char *ifname;
|
||||||
struct ether_addr *mac;
|
struct ether_addr *mac;
|
||||||
} mactable_t;
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
||||||
|
char *bus_info;
|
||||||
|
char *driver;
|
||||||
|
#endif
|
||||||
|
} ethtable_t;
|
||||||
|
|
||||||
/* Check ascii str_macaddr, convert and copy to *mac */
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
||||||
static struct ether_addr *cc_macaddr(const char *str_macaddr)
|
/* Cut'n'paste from ethtool.h */
|
||||||
|
#define ETHTOOL_BUSINFO_LEN 32
|
||||||
|
/* these strings are set to whatever the driver author decides... */
|
||||||
|
struct ethtool_drvinfo {
|
||||||
|
__u32 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];
|
||||||
|
__u32 n_stats; /* number of u64's from ETHTOOL_GSTATS */
|
||||||
|
__u32 testinfo_len;
|
||||||
|
__u32 eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
|
||||||
|
__u32 regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */
|
||||||
|
};
|
||||||
|
#define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static void nameif_parse_selector(ethtable_t *ch, char *selector)
|
||||||
{
|
{
|
||||||
struct ether_addr *lmac, *mac;
|
struct ether_addr *lmac;
|
||||||
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
||||||
|
int found_selector = 0;
|
||||||
|
|
||||||
lmac = ether_aton(str_macaddr);
|
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 = ether_aton(selector + (strncmp(selector, "mac=", 4) == 0 ? 4 : 0));
|
||||||
|
/* Check ascii selector, convert and copy to *mac */
|
||||||
if (lmac == NULL)
|
if (lmac == NULL)
|
||||||
bb_error_msg_and_die("cannot parse MAC %s", str_macaddr);
|
bb_error_msg_and_die("cannot parse %s", selector);
|
||||||
mac = xmalloc(ETH_ALEN);
|
ch->mac = xmalloc(ETH_ALEN);
|
||||||
memcpy(mac, lmac, ETH_ALEN);
|
memcpy(ch->mac, lmac, ETH_ALEN);
|
||||||
|
#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
|
||||||
|
}
|
||||||
|
|
||||||
return mac;
|
static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
|
||||||
|
{
|
||||||
|
ethtable_t *ch;
|
||||||
|
if (strlen(ifname) >= IF_NAMESIZE)
|
||||||
|
bb_error_msg_and_die("interface name '%s' too long", ifname);
|
||||||
|
ch = xzalloc(sizeof(*ch));
|
||||||
|
ch->ifname = ifname;
|
||||||
|
nameif_parse_selector(ch, selector);
|
||||||
|
ch->next = *clist;
|
||||||
|
if (*clist)
|
||||||
|
(*clist)->prev = ch;
|
||||||
|
*clist = ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||||
int nameif_main(int argc, char **argv)
|
int nameif_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
mactable_t *clist = NULL;
|
ethtable_t *clist = NULL;
|
||||||
FILE *ifh;
|
FILE *ifh;
|
||||||
const char *fname = "/etc/mactab";
|
const char *fname = "/etc/mactab";
|
||||||
char *line;
|
char *line;
|
||||||
|
char *line_ptr;
|
||||||
|
int linenum;
|
||||||
int ctl_sk;
|
int ctl_sk;
|
||||||
int if_index = 1;
|
ethtable_t *ch;
|
||||||
mactable_t *ch;
|
|
||||||
|
|
||||||
if (1 & getopt32(argv, "sc:", &fname)) {
|
if (1 & getopt32(argv, "sc:", &fname)) {
|
||||||
openlog(applet_name, 0, LOG_LOCAL0);
|
openlog(applet_name, 0, LOG_LOCAL0);
|
||||||
logmode = LOGMODE_SYSLOG;
|
logmode = LOGMODE_SYSLOG;
|
||||||
}
|
}
|
||||||
|
argc -= optind;
|
||||||
|
argv += optind;
|
||||||
|
|
||||||
if ((argc - optind) & 1)
|
if (argc & 1)
|
||||||
bb_show_usage();
|
bb_show_usage();
|
||||||
|
|
||||||
if (optind < argc) {
|
if (argc) {
|
||||||
char **a = argv + optind;
|
while (*argv) {
|
||||||
|
char *ifname = xstrdup(*argv++);
|
||||||
while (*a) {
|
prepend_new_eth_table(&clist, ifname, *argv++);
|
||||||
if (strlen(*a) > IF_NAMESIZE)
|
|
||||||
bb_error_msg_and_die("interface name '%s' "
|
|
||||||
"too long", *a);
|
|
||||||
ch = xzalloc(sizeof(mactable_t));
|
|
||||||
ch->ifname = xstrdup(*a++);
|
|
||||||
ch->mac = cc_macaddr(*a++);
|
|
||||||
if (clist)
|
|
||||||
clist->prev = ch;
|
|
||||||
ch->next = clist;
|
|
||||||
clist = ch;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ifh = xfopen(fname, "r");
|
ifh = xfopen(fname, "r");
|
||||||
|
|
||||||
while ((line = xmalloc_fgets(ifh)) != NULL) {
|
while ((line = xmalloc_fgets(ifh)) != NULL) {
|
||||||
char *line_ptr;
|
char *next;
|
||||||
size_t name_length;
|
|
||||||
|
|
||||||
line_ptr = line + strspn(line, " \t");
|
line_ptr = skip_whitespace(line);
|
||||||
if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
|
if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
|
||||||
free(line);
|
free(line);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
name_length = strcspn(line_ptr, " \t");
|
next = skip_non_whitespace(line_ptr);
|
||||||
ch = xzalloc(sizeof(mactable_t));
|
if (*next)
|
||||||
ch->ifname = xstrndup(line_ptr, name_length);
|
*next++ = '\0';
|
||||||
if (name_length > IF_NAMESIZE)
|
prepend_new_eth_table(&clist, line_ptr, next);
|
||||||
bb_error_msg_and_die("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);
|
free(line);
|
||||||
}
|
}
|
||||||
fclose(ifh);
|
fclose(ifh);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
|
ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
|
||||||
|
ifh = xfopen("/proc/net/dev", "r");
|
||||||
|
|
||||||
|
linenum = 0;
|
||||||
while (clist) {
|
while (clist) {
|
||||||
struct ifreq ifr;
|
struct ifreq ifr;
|
||||||
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
||||||
|
struct ethtool_drvinfo drvinfo;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
line = xmalloc_fgets(ifh);
|
||||||
|
if (line == NULL)
|
||||||
|
break; /* Seems like we're done */
|
||||||
|
if (linenum++ < 2 )
|
||||||
|
goto next_line; /* Skip the first two lines */
|
||||||
|
|
||||||
|
/* Find the current interface name and copy it to ifr.ifr_name */
|
||||||
|
line_ptr = skip_whitespace(line);
|
||||||
|
*skip_non_whitespace(line_ptr) = '\0';
|
||||||
|
|
||||||
memset(&ifr, 0, sizeof(struct ifreq));
|
memset(&ifr, 0, sizeof(struct ifreq));
|
||||||
if_index++;
|
strncpy(ifr.ifr_name, line_ptr, sizeof(ifr.ifr_name));
|
||||||
ifr.ifr_ifindex = if_index;
|
|
||||||
|
|
||||||
/* Get ifname by index or die */
|
#if ENABLE_FEATURE_NAMEIF_EXTENDED
|
||||||
if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
|
/* Check for driver etc. */
|
||||||
break;
|
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);
|
||||||
|
|
||||||
/* Has this device hwaddr? */
|
/* Search the list for a matching device */
|
||||||
if (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;
|
continue;
|
||||||
|
if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
|
||||||
/* 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;
|
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 */
|
||||||
|
goto next_line;
|
||||||
|
found:
|
||||||
|
if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
|
||||||
strcpy(ifr.ifr_newname, ch->ifname);
|
strcpy(ifr.ifr_newname, ch->ifname);
|
||||||
ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
|
ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
|
||||||
"cannot change ifname %s to %s",
|
"cannot change ifname %s to %s",
|
||||||
ifr.ifr_name, ch->ifname);
|
ifr.ifr_name, ch->ifname);
|
||||||
|
|
||||||
/* Remove list entry of renamed interface */
|
|
||||||
if (ch->prev != NULL) {
|
|
||||||
(ch->prev)->next = ch->next;
|
|
||||||
} else {
|
|
||||||
clist = ch->next;
|
|
||||||
}
|
}
|
||||||
|
/* Remove list entry of renamed interface */
|
||||||
|
if (ch->prev != NULL)
|
||||||
|
ch->prev->next = ch->next;
|
||||||
|
else
|
||||||
|
clist = ch->next;
|
||||||
if (ch->next != NULL)
|
if (ch->next != NULL)
|
||||||
(ch->next)->prev = ch->prev;
|
ch->next->prev = ch->prev;
|
||||||
if (ENABLE_FEATURE_CLEAN_UP) {
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||||
free(ch->ifname);
|
free(ch->ifname);
|
||||||
free(ch->mac);
|
free(ch->mac);
|
||||||
free(ch);
|
free(ch);
|
||||||
}
|
}
|
||||||
|
next_line:
|
||||||
|
free(line);
|
||||||
}
|
}
|
||||||
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||||
|
fclose(ifh);
|
||||||
|
};
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user