blob: f3d333baf0412e7647365c2b16590bdce2f3620f [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath77c60e52003-01-16 11:37:57 +00002/*
Glenn L McGrathf03c9332002-12-13 00:01:44 +00003 * nameif.c - Naming Interfaces based on MAC address for busybox.
4 *
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * Written 2000 by Andi Kleen.
Glenn L McGrathf03c9332002-12-13 00:01:44 +00006 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
Denis Vlasenko3f5fdc72007-10-14 04:55:59 +00007 * Glenn McGrath
Denis Vlasenkob3f39f02008-04-10 02:03:21 +00008 * Extended matching support 2008 by Nico Erfurth <masta@perlgolf.de>
Glenn L McGrathf03c9332002-12-13 00:01:44 +00009 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000010 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrathf03c9332002-12-13 00:01:44 +000011 */
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000014#include <syslog.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000015#include <net/if.h>
16#include <netinet/ether.h>
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000017#include <linux/sockios.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000018
Denis Vlasenko01eaee92008-04-21 02:21:45 +000019#ifndef IFNAMSIZ
20#define IFNAMSIZ 16
Eric Andersen40ea66c2003-07-05 08:00:17 +000021#endif
22
Denis Vlasenko01eaee92008-04-21 02:21:45 +000023/* Taken from linux/sockios.h */
Glenn L McGratha9adef02003-01-19 13:34:21 +000024#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000025
Eric Andersenaff114c2004-04-14 17:51:38 +000026/* Octets in one Ethernet addr, from <linux/if_ether.h> */
Glenn L McGrathf03c9332002-12-13 00:01:44 +000027#define ETH_ALEN 6
28
29#ifndef ifr_newname
30#define ifr_newname ifr_ifru.ifru_slave
31#endif
32
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000033typedef struct ethtable_s {
34 struct ethtable_s *next;
35 struct ethtable_s *prev;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000036 char *ifname;
37 struct ether_addr *mac;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000038#if ENABLE_FEATURE_NAMEIF_EXTENDED
39 char *bus_info;
40 char *driver;
41#endif
42} ethtable_t;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000043
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000044#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... */
48struct ethtool_drvinfo {
Denis Vlasenko3f9c8482007-12-28 17:04:42 +000049 uint32_t cmd;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000050 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 Vlasenko6b404432008-01-07 16:13:14 +000054 /* For PCI devices, use pci_dev->slot_name. */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000055 char reserved1[32];
56 char reserved2[16];
Denis Vlasenko3f9c8482007-12-28 17:04:42 +000057 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 Vlasenkof7be20e2007-12-24 14:09:19 +000061};
62#define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
63#endif
64
65
66static void nameif_parse_selector(ethtable_t *ch, char *selector)
Glenn L McGrath688cf012002-12-17 12:43:43 +000067{
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000068 struct ether_addr *lmac;
69#if ENABLE_FEATURE_NAMEIF_EXTENDED
70 int found_selector = 0;
Glenn L McGrath688cf012002-12-17 12:43:43 +000071
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000072 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 Vlasenkob3f39f02008-04-10 02:03:21 +000092 lmac = xmalloc(ETH_ALEN);
93 ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) ? 0 : 4), lmac);
94 if (ch->mac == NULL)
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000095 bb_error_msg_and_die("cannot parse %s", selector);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000096#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 McGrath688cf012002-12-17 12:43:43 +0000105
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000106static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
107{
108 ethtable_t *ch;
Denis Vlasenko01eaee92008-04-21 02:21:45 +0000109 if (strlen(ifname) >= IFNAMSIZ)
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000110 bb_error_msg_and_die("interface name '%s' too long", ifname);
111 ch = xzalloc(sizeof(*ch));
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000112 ch->ifname = xstrdup(ifname);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000113 nameif_parse_selector(ch, selector);
114 ch->next = *clist;
115 if (*clist)
116 (*clist)->prev = ch;
117 *clist = ch;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000118}
119
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000120#if ENABLE_FEATURE_CLEAN_UP
121static 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
132void delete_eth_table(ethtable_t *ch);
133#endif
134
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000135int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000136int nameif_main(int argc, char **argv)
137{
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000138 ethtable_t *clist = NULL;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000139 FILE *ifh;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000140 const char *fname = "/etc/mactab";
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000141 char *line;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000142 char *line_ptr;
143 int linenum;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000144 int ctl_sk;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000145 ethtable_t *ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000146
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000147 if (1 & getopt32(argv, "sc:", &fname)) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000148 openlog(applet_name, 0, LOG_LOCAL0);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000149 logmode = LOGMODE_SYSLOG;
150 }
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000151 argc -= optind;
152 argv += optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000153
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000154 if (argc & 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 bb_show_usage();
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000156
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000157 if (argc) {
158 while (*argv) {
159 char *ifname = xstrdup(*argv++);
160 prepend_new_eth_table(&clist, ifname, *argv++);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000161 }
162 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000163 ifh = xfopen(fname, "r");
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000164 while ((line = xmalloc_fgets(ifh)) != NULL) {
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000165 char *next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000166
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000167 line_ptr = skip_whitespace(line);
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000168 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
169 goto read_next_line;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000170 next = skip_non_whitespace(line_ptr);
171 if (*next)
172 *next++ = '\0';
173 prepend_new_eth_table(&clist, line_ptr, next);
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000174 read_next_line:
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000175 free(line);
176 }
177 fclose(ifh);
178 }
179
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000180 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000181 ifh = xfopen("/proc/net/dev", "r");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000182
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000183 linenum = 0;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000184 while (clist) {
185 struct ifreq ifr;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000186#if ENABLE_FEATURE_NAMEIF_EXTENDED
187 struct ethtool_drvinfo drvinfo;
188#endif
189
190 line = xmalloc_fgets(ifh);
191 if (line == NULL)
192 break; /* Seems like we're done */
193 if (linenum++ < 2 )
194 goto next_line; /* Skip the first two lines */
195
196 /* Find the current interface name and copy it to ifr.ifr_name */
197 line_ptr = skip_whitespace(line);
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000198 *strpbrk(line_ptr, " \t\n:") = '\0';
Glenn L McGrath688cf012002-12-17 12:43:43 +0000199
Rob Landley855f1e12006-01-15 02:20:06 +0000200 memset(&ifr, 0, sizeof(struct ifreq));
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000201 strncpy(ifr.ifr_name, line_ptr, sizeof(ifr.ifr_name));
Glenn L McGrath688cf012002-12-17 12:43:43 +0000202
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000203#if ENABLE_FEATURE_NAMEIF_EXTENDED
204 /* Check for driver etc. */
205 memset(&drvinfo, 0, sizeof(struct ethtool_drvinfo));
206 drvinfo.cmd = ETHTOOL_GDRVINFO;
207 ifr.ifr_data = (caddr_t) &drvinfo;
208 /* Get driver and businfo first, so we have it in drvinfo */
209 ioctl(ctl_sk, SIOCETHTOOL, &ifr);
210#endif
211 ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000212
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000213 /* Search the list for a matching device */
214 for (ch = clist; ch; ch = ch->next) {
215#if ENABLE_FEATURE_NAMEIF_EXTENDED
216 if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
217 continue;
218 if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
219 continue;
220#endif
221 if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
222 continue;
223 /* if we came here, all selectors have matched */
224 goto found;
225 }
226 /* Nothing found for current interface */
227 goto next_line;
228 found:
229 if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
230 strcpy(ifr.ifr_newname, ch->ifname);
231 ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000232 "cannot change ifname %s to %s",
233 ifr.ifr_name, ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000234 }
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000235 /* Remove list entry of renamed interface */
236 if (ch->prev != NULL)
237 ch->prev->next = ch->next;
238 else
239 clist = ch->next;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000240 if (ch->next != NULL)
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000241 ch->next->prev = ch->prev;
242 if (ENABLE_FEATURE_CLEAN_UP)
243 delete_eth_table(ch);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000244 next_line:
245 free(line);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000246 }
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000247 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkob3f39f02008-04-10 02:03:21 +0000248 for (ch = clist; ch; ch = ch->next)
249 delete_eth_table(ch);
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000250 fclose(ifh);
251 };
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000252
253 return 0;
254}