"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 2 | /* |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 3 | * nameif.c - Naming Interfaces based on MAC address for busybox. |
| 4 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * Written 2000 by Andi Kleen. |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 6 | * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua> |
Denis Vlasenko | 3f5fdc7 | 2007-10-14 04:55:59 +0000 | [diff] [blame] | 7 | * Glenn McGrath |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 8 | * Extended matching support 2008 by Nico Erfurth <masta@perlgolf.de> |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 9 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 10 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 14 | #include <syslog.h> |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 15 | #include <net/if.h> |
| 16 | #include <netinet/ether.h> |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 17 | #include <linux/sockios.h> |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 19 | #ifndef IFNAMSIZ |
| 20 | #define IFNAMSIZ 16 |
Eric Andersen | 40ea66c | 2003-07-05 08:00:17 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 23 | /* Taken from linux/sockios.h */ |
Glenn L McGrath | a9adef0 | 2003-01-19 13:34:21 +0000 | [diff] [blame] | 24 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 25 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 26 | /* Octets in one Ethernet addr, from <linux/if_ether.h> */ |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 27 | #define ETH_ALEN 6 |
| 28 | |
| 29 | #ifndef ifr_newname |
| 30 | #define ifr_newname ifr_ifru.ifru_slave |
| 31 | #endif |
| 32 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 33 | typedef struct ethtable_s { |
| 34 | struct ethtable_s *next; |
| 35 | struct ethtable_s *prev; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 36 | char *ifname; |
| 37 | struct ether_addr *mac; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 38 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 39 | char *bus_info; |
| 40 | char *driver; |
| 41 | #endif |
| 42 | } ethtable_t; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 44 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 45 | /* Cut'n'paste from ethtool.h */ |
| 46 | #define ETHTOOL_BUSINFO_LEN 32 |
| 47 | /* these strings are set to whatever the driver author decides... */ |
| 48 | struct ethtool_drvinfo { |
Denis Vlasenko | 3f9c848 | 2007-12-28 17:04:42 +0000 | [diff] [blame] | 49 | uint32_t cmd; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 50 | char driver[32]; /* driver short name, "tulip", "eepro100" */ |
| 51 | char version[32]; /* driver version string */ |
| 52 | char fw_version[32]; /* firmware version string, if applicable */ |
| 53 | char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */ |
Denis Vlasenko | 6b40443 | 2008-01-07 16:13:14 +0000 | [diff] [blame] | 54 | /* For PCI devices, use pci_dev->slot_name. */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 55 | char reserved1[32]; |
| 56 | char reserved2[16]; |
Denis Vlasenko | 3f9c848 | 2007-12-28 17:04:42 +0000 | [diff] [blame] | 57 | uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */ |
| 58 | uint32_t testinfo_len; |
| 59 | uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */ |
| 60 | uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 61 | }; |
| 62 | #define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */ |
| 63 | #endif |
| 64 | |
| 65 | |
| 66 | static void nameif_parse_selector(ethtable_t *ch, char *selector) |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 67 | { |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 68 | struct ether_addr *lmac; |
| 69 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 70 | int found_selector = 0; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 71 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 72 | while (*selector) { |
| 73 | char *next; |
| 74 | #endif |
| 75 | selector = skip_whitespace(selector); |
| 76 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 77 | if (*selector == '\0') |
| 78 | break; |
| 79 | /* Search for the end .... */ |
| 80 | next = skip_non_whitespace(selector); |
| 81 | if (*next) |
| 82 | *next++ = '\0'; |
| 83 | /* Check for selectors, mac= is assumed */ |
| 84 | if (strncmp(selector, "bus=", 4) == 0) { |
| 85 | ch->bus_info = xstrdup(selector + 4); |
| 86 | found_selector++; |
| 87 | } else if (strncmp(selector, "driver=", 7) == 0) { |
| 88 | ch->driver = xstrdup(selector + 7); |
| 89 | found_selector++; |
| 90 | } else { |
| 91 | #endif |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 92 | lmac = xmalloc(ETH_ALEN); |
| 93 | ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) ? 0 : 4), lmac); |
| 94 | if (ch->mac == NULL) |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 95 | bb_error_msg_and_die("cannot parse %s", selector); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 96 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 97 | found_selector++; |
| 98 | }; |
| 99 | selector = next; |
| 100 | } |
| 101 | if (found_selector == 0) |
| 102 | bb_error_msg_and_die("no selectors found for %s", ch->ifname); |
| 103 | #endif |
| 104 | } |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 105 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 106 | static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector) |
| 107 | { |
| 108 | ethtable_t *ch; |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 109 | if (strlen(ifname) >= IFNAMSIZ) |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 110 | bb_error_msg_and_die("interface name '%s' too long", ifname); |
| 111 | ch = xzalloc(sizeof(*ch)); |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 112 | ch->ifname = xstrdup(ifname); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 113 | nameif_parse_selector(ch, selector); |
| 114 | ch->next = *clist; |
| 115 | if (*clist) |
| 116 | (*clist)->prev = ch; |
| 117 | *clist = ch; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 120 | #if ENABLE_FEATURE_CLEAN_UP |
| 121 | static void delete_eth_table(ethtable_t *ch) |
| 122 | { |
| 123 | free(ch->ifname); |
| 124 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 125 | free(ch->bus_info); |
| 126 | free(ch->driver); |
| 127 | #endif |
| 128 | free(ch->mac); |
| 129 | free(ch); |
| 130 | }; |
| 131 | #else |
| 132 | void delete_eth_table(ethtable_t *ch); |
| 133 | #endif |
| 134 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 135 | int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 136 | int nameif_main(int argc, char **argv) |
| 137 | { |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 138 | ethtable_t *clist = NULL; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 139 | FILE *ifh; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 140 | const char *fname = "/etc/mactab"; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 141 | char *line; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 142 | char *line_ptr; |
| 143 | int linenum; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 144 | int ctl_sk; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 145 | ethtable_t *ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 147 | if (1 & getopt32(argv, "sc:", &fname)) { |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 148 | openlog(applet_name, 0, LOG_LOCAL0); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 149 | logmode = LOGMODE_SYSLOG; |
| 150 | } |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 151 | argc -= optind; |
| 152 | argv += optind; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 154 | if (argc & 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 155 | bb_show_usage(); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 156 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 157 | if (argc) { |
| 158 | while (*argv) { |
| 159 | char *ifname = xstrdup(*argv++); |
| 160 | prepend_new_eth_table(&clist, ifname, *argv++); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 161 | } |
| 162 | } else { |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 163 | char *tokens[2]; |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 164 | struct parser_t *parser = config_open(fname); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 165 | while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) |
| 166 | prepend_new_eth_table(&clist, tokens[0], tokens[1]); |
| 167 | config_close(parser); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 170 | ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 171 | ifh = xfopen_for_read("/proc/net/dev"); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 172 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 173 | linenum = 0; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 174 | while (clist) { |
| 175 | struct ifreq ifr; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 176 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 177 | struct ethtool_drvinfo drvinfo; |
| 178 | #endif |
| 179 | |
| 180 | line = xmalloc_fgets(ifh); |
| 181 | if (line == NULL) |
| 182 | break; /* Seems like we're done */ |
| 183 | if (linenum++ < 2 ) |
| 184 | goto next_line; /* Skip the first two lines */ |
| 185 | |
| 186 | /* Find the current interface name and copy it to ifr.ifr_name */ |
| 187 | line_ptr = skip_whitespace(line); |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 188 | *strpbrk(line_ptr, " \t\n:") = '\0'; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 189 | |
Rob Landley | 855f1e1 | 2006-01-15 02:20:06 +0000 | [diff] [blame] | 190 | memset(&ifr, 0, sizeof(struct ifreq)); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 191 | strncpy(ifr.ifr_name, line_ptr, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 193 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 194 | /* Check for driver etc. */ |
| 195 | memset(&drvinfo, 0, sizeof(struct ethtool_drvinfo)); |
| 196 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
| 197 | ifr.ifr_data = (caddr_t) &drvinfo; |
| 198 | /* Get driver and businfo first, so we have it in drvinfo */ |
| 199 | ioctl(ctl_sk, SIOCETHTOOL, &ifr); |
| 200 | #endif |
| 201 | ioctl(ctl_sk, SIOCGIFHWADDR, &ifr); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 203 | /* Search the list for a matching device */ |
| 204 | for (ch = clist; ch; ch = ch->next) { |
| 205 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 206 | if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0) |
| 207 | continue; |
| 208 | if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0) |
| 209 | continue; |
| 210 | #endif |
| 211 | if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0) |
| 212 | continue; |
| 213 | /* if we came here, all selectors have matched */ |
| 214 | goto found; |
| 215 | } |
| 216 | /* Nothing found for current interface */ |
| 217 | goto next_line; |
| 218 | found: |
| 219 | if (strcmp(ifr.ifr_name, ch->ifname) != 0) { |
| 220 | strcpy(ifr.ifr_newname, ch->ifname); |
| 221 | ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr, |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 222 | "cannot change ifname %s to %s", |
| 223 | ifr.ifr_name, ch->ifname); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 224 | } |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 225 | /* Remove list entry of renamed interface */ |
| 226 | if (ch->prev != NULL) |
| 227 | ch->prev->next = ch->next; |
| 228 | else |
| 229 | clist = ch->next; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 230 | if (ch->next != NULL) |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 231 | ch->next->prev = ch->prev; |
| 232 | if (ENABLE_FEATURE_CLEAN_UP) |
| 233 | delete_eth_table(ch); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 234 | next_line: |
| 235 | free(line); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 236 | } |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 237 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 238 | for (ch = clist; ch; ch = ch->next) |
| 239 | delete_eth_table(ch); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 240 | fclose(ifh); |
| 241 | }; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |