"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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 11 | */ |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 12 | //config:config NAMEIF |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 13 | //config: bool "nameif (6.6 kb)" |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 14 | //config: default y |
| 15 | //config: select PLATFORM_LINUX |
| 16 | //config: select FEATURE_SYSLOG |
| 17 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 18 | //config: nameif is used to rename network interface by its MAC address. |
| 19 | //config: Renamed interfaces MUST be in the down state. |
| 20 | //config: It is possible to use a file (default: /etc/mactab) |
| 21 | //config: with list of new interface names and MACs. |
| 22 | //config: Maximum interface name length: IFNAMSIZ = 16 |
| 23 | //config: File fields are separated by space or tab. |
| 24 | //config: File format: |
| 25 | //config: # Comment |
| 26 | //config: new_interface_name XX:XX:XX:XX:XX:XX |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 27 | //config: |
| 28 | //config:config FEATURE_NAMEIF_EXTENDED |
| 29 | //config: bool "Extended nameif" |
| 30 | //config: default y |
| 31 | //config: depends on NAMEIF |
| 32 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 33 | //config: This extends the nameif syntax to support the bus_info, driver, |
| 34 | //config: phyaddr selectors. The syntax is compatible to the normal nameif. |
| 35 | //config: File format: |
| 36 | //config: new_interface_name driver=asix bus=usb-0000:00:08.2-3 |
| 37 | //config: new_interface_name bus=usb-0000:00:08.2-3 00:80:C8:38:91:B5 |
| 38 | //config: new_interface_name phy_address=2 00:80:C8:38:91:B5 |
| 39 | //config: new_interface_name mac=00:80:C8:38:91:B5 |
| 40 | //config: new_interface_name 00:80:C8:38:91:B5 |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 41 | |
Denys Vlasenko | a759b22 | 2017-08-06 14:15:24 +0200 | [diff] [blame] | 42 | //applet:IF_NAMEIF(APPLET_NOEXEC(nameif, nameif, BB_DIR_SBIN, BB_SUID_DROP, nameif)) |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 43 | |
| 44 | //kbuild:lib-$(CONFIG_NAMEIF) += nameif.o |
| 45 | |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 46 | //usage:#define nameif_trivial_usage |
| 47 | //usage: IF_NOT_FEATURE_NAMEIF_EXTENDED( |
| 48 | //usage: "[-s] [-c FILE] [IFNAME HWADDR]..." |
| 49 | //usage: ) |
| 50 | //usage: IF_FEATURE_NAMEIF_EXTENDED( |
| 51 | //usage: "[-s] [-c FILE] [IFNAME SELECTOR]..." |
| 52 | //usage: ) |
| 53 | //usage:#define nameif_full_usage "\n\n" |
| 54 | //usage: "Rename network interface while it in the down state." |
| 55 | //usage: IF_NOT_FEATURE_NAMEIF_EXTENDED( |
| 56 | //usage: "\nThe device with address HWADDR is renamed to IFACE." |
| 57 | //usage: ) |
| 58 | //usage: IF_FEATURE_NAMEIF_EXTENDED( |
| 59 | //usage: "\nThe device matched by SELECTOR is renamed to IFACE." |
| 60 | //usage: "\nSELECTOR can be a combination of:" |
| 61 | //usage: "\n driver=STRING" |
| 62 | //usage: "\n bus=STRING" |
| 63 | //usage: "\n phy_address=NUM" |
| 64 | //usage: "\n [mac=]XX:XX:XX:XX:XX:XX" |
| 65 | //usage: ) |
| 66 | //usage: "\n" |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 67 | //usage: "\n -c FILE Configuration file (default: /etc/mactab)" |
| 68 | //usage: "\n -s Log to syslog" |
| 69 | //usage: |
| 70 | //usage:#define nameif_example_usage |
| 71 | //usage: "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n" |
| 72 | //usage: " or\n" |
| 73 | //usage: "$ nameif -c /etc/my_mactab_file\n" |
| 74 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 75 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 76 | #include <syslog.h> |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 77 | #include <net/if.h> |
| 78 | #include <netinet/ether.h> |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 79 | #include <linux/sockios.h> |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 81 | #ifndef IFNAMSIZ |
| 82 | #define IFNAMSIZ 16 |
Eric Andersen | 40ea66c | 2003-07-05 08:00:17 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 85 | /* Taken from linux/sockios.h */ |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 86 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 87 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 88 | /* Octets in one Ethernet addr, from <linux/if_ether.h> */ |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 89 | #define ETH_ALEN 6 |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 90 | |
| 91 | #ifndef ifr_newname |
| 92 | #define ifr_newname ifr_ifru.ifru_slave |
| 93 | #endif |
| 94 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 95 | typedef struct ethtable_s { |
| 96 | struct ethtable_s *next; |
| 97 | struct ethtable_s *prev; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 98 | char *ifname; |
| 99 | struct ether_addr *mac; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 100 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 101 | char *bus_info; |
| 102 | char *driver; |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 103 | int32_t phy_address; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 104 | #endif |
| 105 | } ethtable_t; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 107 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 108 | /* Cut'n'paste from ethtool.h */ |
| 109 | #define ETHTOOL_BUSINFO_LEN 32 |
| 110 | /* these strings are set to whatever the driver author decides... */ |
| 111 | struct ethtool_drvinfo { |
Denis Vlasenko | 3f9c848 | 2007-12-28 17:04:42 +0000 | [diff] [blame] | 112 | uint32_t cmd; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 113 | char driver[32]; /* driver short name, "tulip", "eepro100" */ |
| 114 | char version[32]; /* driver version string */ |
| 115 | char fw_version[32]; /* firmware version string, if applicable */ |
| 116 | char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */ |
Denis Vlasenko | 6b40443 | 2008-01-07 16:13:14 +0000 | [diff] [blame] | 117 | /* For PCI devices, use pci_dev->slot_name. */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 118 | char reserved1[32]; |
| 119 | char reserved2[16]; |
Denis Vlasenko | 3f9c848 | 2007-12-28 17:04:42 +0000 | [diff] [blame] | 120 | uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */ |
| 121 | uint32_t testinfo_len; |
| 122 | uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */ |
| 123 | uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 124 | }; |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 125 | |
| 126 | struct ethtool_cmd { |
Daniel Fandrich | 6295d27 | 2011-06-09 15:44:44 -0700 | [diff] [blame] | 127 | uint32_t cmd; |
| 128 | uint32_t supported; /* Features this interface supports */ |
| 129 | uint32_t advertising; /* Features this interface advertises */ |
| 130 | uint16_t speed; /* The forced speed, 10Mb, 100Mb, gigabit */ |
| 131 | uint8_t duplex; /* Duplex, half or full */ |
| 132 | uint8_t port; /* Which connector port */ |
| 133 | uint8_t phy_address; |
| 134 | uint8_t transceiver; /* Which transceiver to use */ |
| 135 | uint8_t autoneg; /* Enable or disable autonegotiation */ |
| 136 | uint32_t maxtxpkt; /* Tx pkts before generating tx int */ |
| 137 | uint32_t maxrxpkt; /* Rx pkts before generating rx int */ |
| 138 | uint16_t speed_hi; |
| 139 | uint16_t reserved2; |
| 140 | uint32_t reserved[3]; |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | #define ETHTOOL_GSET 0x00000001 /* Get settings. */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 144 | #define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */ |
| 145 | #endif |
| 146 | |
| 147 | |
| 148 | static void nameif_parse_selector(ethtable_t *ch, char *selector) |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 149 | { |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 150 | struct ether_addr *lmac; |
| 151 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 152 | int found_selector = 0; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 154 | while (*selector) { |
| 155 | char *next; |
| 156 | #endif |
| 157 | selector = skip_whitespace(selector); |
| 158 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 159 | ch->phy_address = -1; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 160 | if (*selector == '\0') |
| 161 | break; |
| 162 | /* Search for the end .... */ |
| 163 | next = skip_non_whitespace(selector); |
| 164 | if (*next) |
| 165 | *next++ = '\0'; |
| 166 | /* Check for selectors, mac= is assumed */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 167 | if (is_prefixed_with(selector, "bus=")) { |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 168 | ch->bus_info = xstrdup(selector + 4); |
| 169 | found_selector++; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 170 | } else if (is_prefixed_with(selector, "driver=")) { |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 171 | ch->driver = xstrdup(selector + 7); |
| 172 | found_selector++; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 173 | } else if (is_prefixed_with(selector, "phyaddr=")) { |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 174 | ch->phy_address = xatoi_positive(selector + 8); |
| 175 | found_selector++; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 176 | } else { |
| 177 | #endif |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 178 | lmac = xmalloc(ETH_ALEN); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 179 | ch->mac = ether_aton_r(selector + (is_prefixed_with(selector, "mac=") ? 4 : 0), lmac); |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 180 | if (ch->mac == NULL) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 181 | bb_error_msg_and_die("can't parse %s", selector); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 182 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 183 | found_selector++; |
| 184 | }; |
| 185 | selector = next; |
| 186 | } |
| 187 | if (found_selector == 0) |
| 188 | bb_error_msg_and_die("no selectors found for %s", ch->ifname); |
| 189 | #endif |
| 190 | } |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 191 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 192 | static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector) |
| 193 | { |
| 194 | ethtable_t *ch; |
Denis Vlasenko | 01eaee9 | 2008-04-21 02:21:45 +0000 | [diff] [blame] | 195 | if (strlen(ifname) >= IFNAMSIZ) |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 196 | bb_error_msg_and_die("interface name '%s' too long", ifname); |
| 197 | ch = xzalloc(sizeof(*ch)); |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 198 | ch->ifname = xstrdup(ifname); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 199 | nameif_parse_selector(ch, selector); |
| 200 | ch->next = *clist; |
| 201 | if (*clist) |
| 202 | (*clist)->prev = ch; |
| 203 | *clist = ch; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 206 | #if ENABLE_FEATURE_CLEAN_UP |
| 207 | static void delete_eth_table(ethtable_t *ch) |
| 208 | { |
| 209 | free(ch->ifname); |
| 210 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 211 | free(ch->bus_info); |
| 212 | free(ch->driver); |
| 213 | #endif |
| 214 | free(ch->mac); |
| 215 | free(ch); |
| 216 | }; |
| 217 | #else |
| 218 | void delete_eth_table(ethtable_t *ch); |
| 219 | #endif |
| 220 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 221 | int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Phil Sutter | 2adc0e6 | 2011-03-06 19:15:30 +0100 | [diff] [blame] | 222 | int nameif_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 223 | { |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 224 | ethtable_t *clist = NULL; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 225 | const char *fname = "/etc/mactab"; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 226 | int ctl_sk; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 227 | ethtable_t *ch; |
Denis Vlasenko | cfe2936 | 2008-08-01 02:32:23 +0000 | [diff] [blame] | 228 | parser_t *parser; |
| 229 | char *token[2]; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 230 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 231 | if (1 & getopt32(argv, "sc:", &fname)) { |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 232 | openlog(applet_name, 0, LOG_LOCAL0); |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 233 | /* Why not just "="? I assume logging to stderr |
| 234 | * can't hurt. 2>/dev/null if you don't like it: */ |
| 235 | logmode |= LOGMODE_SYSLOG; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 236 | } |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 237 | argv += optind; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 238 | |
Phil Sutter | 2adc0e6 | 2011-03-06 19:15:30 +0100 | [diff] [blame] | 239 | if (argv[0]) { |
| 240 | do { |
| 241 | if (!argv[1]) |
| 242 | bb_show_usage(); |
| 243 | prepend_new_eth_table(&clist, argv[0], argv[1]); |
| 244 | argv += 2; |
| 245 | } while (*argv); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 246 | } else { |
Denis Vlasenko | cfe2936 | 2008-08-01 02:32:23 +0000 | [diff] [blame] | 247 | parser = config_open(fname); |
| 248 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) |
| 249 | prepend_new_eth_table(&clist, token[0], token[1]); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 250 | config_close(parser); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 253 | ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | cfe2936 | 2008-08-01 02:32:23 +0000 | [diff] [blame] | 254 | parser = config_open2("/proc/net/dev", xfopen_for_read); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 255 | |
Denis Vlasenko | cfe2936 | 2008-08-01 02:32:23 +0000 | [diff] [blame] | 256 | while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) { |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 257 | struct ifreq ifr; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 258 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 259 | struct ethtool_drvinfo drvinfo; |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 260 | struct ethtool_cmd eth_settings; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 261 | #endif |
Phil Sutter | 2adc0e6 | 2011-03-06 19:15:30 +0100 | [diff] [blame] | 262 | if (parser->lineno <= 2) |
Denis Vlasenko | cfe2936 | 2008-08-01 02:32:23 +0000 | [diff] [blame] | 263 | continue; /* Skip the first two lines */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 264 | |
| 265 | /* Find the current interface name and copy it to ifr.ifr_name */ |
Rob Landley | 855f1e1 | 2006-01-15 02:20:06 +0000 | [diff] [blame] | 266 | memset(&ifr, 0, sizeof(struct ifreq)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 267 | strncpy_IFNAMSIZ(ifr.ifr_name, token[0]); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 268 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 269 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 270 | /* Check for phy address */ |
| 271 | memset(ð_settings, 0, sizeof(eth_settings)); |
| 272 | eth_settings.cmd = ETHTOOL_GSET; |
| 273 | ifr.ifr_data = (caddr_t) ð_settings; |
| 274 | ioctl(ctl_sk, SIOCETHTOOL, &ifr); |
| 275 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 276 | /* Check for driver etc. */ |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 277 | memset(&drvinfo, 0, sizeof(drvinfo)); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 278 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
| 279 | ifr.ifr_data = (caddr_t) &drvinfo; |
| 280 | /* Get driver and businfo first, so we have it in drvinfo */ |
| 281 | ioctl(ctl_sk, SIOCETHTOOL, &ifr); |
| 282 | #endif |
| 283 | ioctl(ctl_sk, SIOCGIFHWADDR, &ifr); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 284 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 285 | /* Search the list for a matching device */ |
| 286 | for (ch = clist; ch; ch = ch->next) { |
| 287 | #if ENABLE_FEATURE_NAMEIF_EXTENDED |
| 288 | if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0) |
| 289 | continue; |
| 290 | if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0) |
| 291 | continue; |
Phil Sutter | 293a8f2 | 2011-03-06 19:42:51 +0100 | [diff] [blame] | 292 | if (ch->phy_address != -1 && ch->phy_address != eth_settings.phy_address) |
| 293 | continue; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 294 | #endif |
| 295 | if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0) |
| 296 | continue; |
| 297 | /* if we came here, all selectors have matched */ |
Thomas De Schampheleire | bc0ffc0 | 2013-02-28 10:31:54 +0100 | [diff] [blame] | 298 | goto found; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 299 | } |
| 300 | /* Nothing found for current interface */ |
Thomas De Schampheleire | bc0ffc0 | 2013-02-28 10:31:54 +0100 | [diff] [blame] | 301 | continue; |
| 302 | found: |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 303 | if (strcmp(ifr.ifr_name, ch->ifname) != 0) { |
| 304 | strcpy(ifr.ifr_newname, ch->ifname); |
| 305 | ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr, |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 306 | "can't change ifname %s to %s", |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 307 | ifr.ifr_name, ch->ifname); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 308 | } |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 309 | /* Remove list entry of renamed interface */ |
| 310 | if (ch->prev != NULL) |
| 311 | ch->prev->next = ch->next; |
| 312 | else |
| 313 | clist = ch->next; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 314 | if (ch->next != NULL) |
Denis Vlasenko | cfe2936 | 2008-08-01 02:32:23 +0000 | [diff] [blame] | 315 | ch->next->prev = ch->prev; |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 316 | if (ENABLE_FEATURE_CLEAN_UP) |
| 317 | delete_eth_table(ch); |
Thomas De Schampheleire | bc0ffc0 | 2013-02-28 10:31:54 +0100 | [diff] [blame] | 318 | } /* while */ |
| 319 | |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 320 | if (ENABLE_FEATURE_CLEAN_UP) { |
Thomas De Schampheleire | bc0ffc0 | 2013-02-28 10:31:54 +0100 | [diff] [blame] | 321 | ethtable_t *next; |
| 322 | for (ch = clist; ch; ch = next) { |
| 323 | next = ch->next; |
Denis Vlasenko | b3f39f0 | 2008-04-10 02:03:21 +0000 | [diff] [blame] | 324 | delete_eth_table(ch); |
Thomas De Schampheleire | bc0ffc0 | 2013-02-28 10:31:54 +0100 | [diff] [blame] | 325 | } |
Denis Vlasenko | cfe2936 | 2008-08-01 02:32:23 +0000 | [diff] [blame] | 326 | config_close(parser); |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 327 | }; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 328 | |
| 329 | return 0; |
| 330 | } |