-
Notifications
You must be signed in to change notification settings - Fork 96
/
nameif.c
301 lines (265 loc) · 6.03 KB
/
nameif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Name Interfaces based on MAC address.
* Writen 2000 by Andi Kleen.
* Subject to the Gnu Public License, version 2.
* TODO: make it support token ring etc.
* $Id: nameif.c,v 1.4 2003/09/11 03:46:49 ak Exp $
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <getopt.h>
#include <sys/syslog.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <errno.h>
#include "intl.h"
#include "net-support.h"
#include "util.h"
/* Current limitation of Linux network device ioctl(2) interface */
#define MAC_ADDRESS_MAX_LENGTH (sizeof(((struct ifreq *)0)->ifr_hwaddr.sa_data))
static const char default_conf[] = "/etc/mactab";
static const char *fname = default_conf;
static int use_syslog;
static int ctl_sk = -1;
attribute_printf(1, 2)
static void complain(const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
if (use_syslog) {
vsyslog(LOG_ERR,fmt,ap);
} else {
vfprintf(stderr,fmt,ap);
fputc('\n',stderr);
}
va_end(ap);
exit(1);
}
attribute_printf(1, 2)
static void warning(const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
if (use_syslog) {
vsyslog(LOG_ERR,fmt,ap);
} else {
vfprintf(stderr,fmt,ap);
fputc('\n',stderr);
}
va_end(ap);
}
static int parsemac(char *str, unsigned int *len, unsigned char *mac, const char *pos)
{
char *s;
while ((s = strsep(&str, ":")) != NULL) {
unsigned byte;
if (sscanf(s,"%x", &byte)!=1 || byte > 0xff)
return -1;
if (++(*len) > MAC_ADDRESS_MAX_LENGTH) {
complain("MAC address at %s is larger than maximum allowed %zu bytes",
pos, MAC_ADDRESS_MAX_LENGTH);
}
*mac++ = byte;
}
return 0;
}
static void opensock(void)
{
if (ctl_sk < 0)
ctl_sk = socket(PF_INET,SOCK_DGRAM,0);
}
#ifndef ifr_newname
#define ifr_newname ifr_ifru.ifru_slave
#endif
static int setname(const char *oldname, const char *newname)
{
struct ifreq ifr;
opensock();
memset(&ifr,0,sizeof(struct ifreq));
safe_strncpy(ifr.ifr_name, oldname, IFNAMSIZ);
safe_strncpy(ifr.ifr_newname, newname, IFNAMSIZ);
return ioctl(ctl_sk, SIOCSIFNAME, &ifr);
}
static int getmac(const char *name, unsigned char *mac)
{
int r;
struct ifreq ifr;
opensock();
memset(&ifr,0,sizeof(struct ifreq));
safe_strncpy(ifr.ifr_name, name, IFNAMSIZ);
r = ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
memcpy(mac, ifr.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LENGTH);
return r;
}
struct change {
struct change *next;
int found;
char ifname[IFNAMSIZ+1];
unsigned int macaddrlen;
unsigned char mac[MAC_ADDRESS_MAX_LENGTH];
};
static struct change *clist;
static struct change *lookupmac(unsigned char *mac)
{
struct change *ch;
for (ch = clist;ch;ch = ch->next)
if (memcmp(ch->mac, mac, ch->macaddrlen) == 0)
return ch;
return NULL;
}
static int addchange(char *p, struct change *ch, const char *pos)
{
if (strchr(ch->ifname, ':'))
warning(_("alias device %s at %s probably has no mac"),
ch->ifname, pos);
ch->macaddrlen = 0;
if (parsemac(p, &ch->macaddrlen, ch->mac, pos) < 0)
complain(_("cannot parse MAC `%s' at %s"), p, pos);
ch->next = clist;
clist = ch;
return 0;
}
static void readconf(void)
{
char *line;
size_t linel;
int linenum;
FILE *ifh;
char *p;
int n;
struct change *ch = NULL;
ifh = fopen(fname, "r");
if (!ifh)
complain(_("opening configuration file %s: %s"),fname,strerror(errno));
line = NULL;
linel = 0;
linenum = 1;
while (getdelim(&line, &linel, '\n', ifh) > 0) {
char pos[20];
sprintf(pos, _("line %d"), linenum);
if ((p = strchr(line,'#')) != NULL)
*p = '\0';
p = line;
while (isspace(*p))
++p;
if (*p == '\0')
continue;
n = strcspn(p, " \t");
if (n > IFNAMSIZ-1)
complain(_("interface name too long at %s"), line);
ch = xmalloc(sizeof(struct change));
memcpy(ch->ifname, p, n);
ch->ifname[n] = 0;
p += n;
p += strspn(p, " \t");
n = strspn(p, "0123456789ABCDEFabcdef:");
p[n] = 0;
addchange(p, ch, pos);
linenum++;
}
fclose(ifh);
}
static const struct option lopt[] = {
{"syslog", 0, NULL, 's' },
{"config-file", 1, NULL, 'c' },
{"help", 0, NULL, 'h' },
{NULL},
};
static void usage(int rc)
{
FILE *fp = rc ? stderr : stdout;
fprintf(fp, _("usage: nameif [-c configurationfile] [-s] {ifname macaddress}\n"));
exit(E_USAGE);
}
int main(int ac, char **av)
{
FILE *ifh;
char *p;
int n;
int linenum;
char *line = NULL;
size_t linel = 0;
int ret = 0;
for (;;) {
int c = getopt_long(ac,av,"c:sh",lopt,NULL);
if (c == -1) break;
switch (c) {
default:
usage(E_OPTERR);
case 'h':
usage(E_USAGE);
case 'c':
fname = optarg;
break;
case 's':
use_syslog = 1;
break;
}
}
if (use_syslog)
openlog("nameif",0,LOG_LOCAL0);
while (optind < ac) {
struct change *ch = xmalloc(sizeof(struct change));
char pos[30];
if ((ac-optind) & 1)
usage(E_OPTERR);
if (strlen(av[optind])+1>IFNAMSIZ)
complain(_("interface name `%s' too long"), av[optind]);
safe_strncpy(ch->ifname, av[optind], sizeof(ch->ifname));
optind++;
sprintf(pos,_("argument %d"),optind);
addchange(av[optind], ch, pos);
optind++;
}
if (!clist || fname != default_conf)
readconf();
ifh = fopen("/proc/net/dev", "r");
if (!ifh) complain(_("open of /proc/net/dev: %s"), strerror(errno));
linenum = 0;
while (getdelim(&line, &linel, '\n', ifh) > 0) {
struct change *ch;
unsigned char mac[MAC_ADDRESS_MAX_LENGTH];
if (linenum++ < 2)
continue;
p = line;
while (isspace(*p))
++p;
n = strcspn(p, ": \t");
p[n] = 0;
if (n > IFNAMSIZ-1)
complain(_("interface name `%s' too long"), p);
if (getmac(p, mac) < 0)
continue;
ch = lookupmac(mac);
if (!ch)
continue;
ch->found = 1;
if (strcmp(p, ch->ifname)) {
if (setname(p, ch->ifname) < 0)
complain(_("cannot change name of %s to %s: %s"),
p, ch->ifname, strerror(errno));
}
}
fclose(ifh);
while (clist) {
struct change *ch = clist;
clist = clist->next;
if (!ch->found){
warning(_("interface '%s' not found"), ch->ifname);
ret = 1;
}
free(ch);
}
if (use_syslog)
closelog();
return ret;
}