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