blob: 1edd64111b0149a2a0ad2c949f6c1b263dbb0fad [file] [log] [blame]
Glenn L McGrathf03c9332002-12-13 00:01:44 +00001/*
2 * nameif.c - Naming Interfaces based on MAC address for busybox.
3 *
4 * Writen 2000 by Andi Kleen.
5 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
6 * Glenn McGrath <bug1@optushome.com.au>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 *
23 */
24
25#include <sys/syslog.h>
26#include <sys/socket.h>
27#include <sys/ioctl.h>
28
29#include <errno.h>
30#include <getopt.h>
31#include <stdlib.h>
32#include <string.h>
33#include <net/if.h>
34#include <netinet/ether.h>
35
36#include "busybox.h"
37
38/* set interface name, from <linux/sockios.h> */
39#define SIOCSIFNAME 0x8923
40/* Octets in one ethernet addr, from <linux/if_ether.h> */
41#define ETH_ALEN 6
42
43#ifndef ifr_newname
44#define ifr_newname ifr_ifru.ifru_slave
45#endif
46
47typedef struct mactable_s {
48 struct mactable_s *next;
49 struct mactable_s **pprev;
50 char *ifname;
51 struct ether_addr *mac;
52} mactable_t;
53
54static void serror_msg_and_die(const char use_syslog, const char *s, ...)
55{
56 va_list ap;
57
58 va_start(ap, s);
59
60 if (use_syslog) {
61 openlog("nameif", 0, LOG_LOCAL0);
62 syslog(LOG_ERR, s, ap);
63 closelog();
64 } else {
65 vfprintf(stderr, s, ap);
66 putc('\n', stderr);
67 }
68
69 va_end(ap);
70
71 exit(EXIT_FAILURE);
72}
73
74int nameif_main(int argc, char **argv)
75{
76 mactable_t *clist = NULL;
77 FILE *ifh;
78 char *fname = "/etc/mactab";
79 char *line;
80 unsigned short linenum = 0;
81 unsigned char use_syslog = 0;
82 int ctl_sk = -1;
83 int opt;
84
85 static struct option opts[] = {
86 {"syslog", 0, NULL, 's'},
87 {"configfile", 1, NULL, 'c'},
88 {NULL},
89 };
90
91 while ((opt = getopt_long(argc, argv, "c:s", opts, NULL)) != -1) {
92 switch (opt) {
93 case 'c':
94 fname = optarg;
95 break;
96 case 's':
97 use_syslog = 1;
98 break;
99 default:
100 show_usage();
101 }
102 }
103
104 if ((argc - optind) & 1) {
105 show_usage();
106 }
107
108 if (optind < argc) {
109 while (optind < argc) {
110 struct ether_addr *mac;
111 mactable_t *ch;
112
113 if (strlen(argv[optind]) > IF_NAMESIZE) {
114 serror_msg_and_die(use_syslog, "interface name `%s' too long", argv[optind]);
115 }
116 optind++;
117 mac = ether_aton(argv[optind]);
118 if (mac == NULL) {
119 serror_msg_and_die(use_syslog, "cannot parse MAC %s", argv[optind]);
120 }
121 ch = xcalloc(1, sizeof(mactable_t));
122 ch->ifname = strdup(argv[optind - 1]);
123 ch->mac = xcalloc(1, ETH_ALEN);
124 memcpy(ch->mac, &mac, ETH_ALEN);
125 optind++;
126 if (clist)
127 clist->pprev = &ch->next;
128 ch->next = clist;
129 ch->pprev = &clist;
130 clist = ch;
131 }
132 } else {
133 ifh = xfopen(fname, "r");
134
135 while ((line = get_line_from_file(ifh)) != NULL) {
136 struct ether_addr *mac;
137 mactable_t *ch;
138 char *line_ptr;
139 unsigned short name_length;
140
141 line_ptr = line + strspn(line, " \t");
142 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
143 continue;
144 name_length = strcspn(line_ptr, " \t");
145 if (name_length > IF_NAMESIZE) {
146 serror_msg_and_die(use_syslog, "interface name `%s' too long", argv[optind]);
147 }
148 ch = xcalloc(1, sizeof(mactable_t));
149 ch->ifname = strndup(line_ptr, name_length);
150 line_ptr += name_length;
151 line_ptr += strspn(line_ptr, " \t");
152 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
153 line_ptr[name_length] = '\0';
154 mac = ether_aton(line_ptr);
155 if (mac == NULL) {
156 serror_msg_and_die(use_syslog, "cannot parse MAC %s", argv[optind]);
157 }
158 ch->mac = xcalloc(1, ETH_ALEN);
159 memcpy(ch->mac, mac, ETH_ALEN);
160 if (clist)
161 clist->pprev = &ch->next;
162 ch->next = clist;
163 ch->pprev = &clist;
164 clist = ch;
165 free(line);
166 }
167 fclose(ifh);
168 }
169
170 ifh = xfopen("/proc/net/dev", "r");
171 while ((line = get_line_from_file(ifh)) != NULL) {
172 char *line_ptr;
173 unsigned short iface_name_length;
174 struct ifreq ifr;
175 mactable_t *ch = NULL;
176
177 linenum++;
178 if (linenum < 3)
179 continue;
180 line_ptr = line + strspn(line, " \t");
181 if (line_ptr[0] == '\n')
182 continue;
183 iface_name_length = strcspn(line_ptr, ":");
184 if (ctl_sk < 0)
185 ctl_sk = socket(PF_INET, SOCK_DGRAM, 0);
186 memset(&ifr, 0, sizeof(struct ifreq));
187 strncpy(ifr.ifr_name, line_ptr, iface_name_length);
188 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr) < 0) {
189 serror_msg_and_die(use_syslog, "cannot change name of %s to %s: %s", ifr.ifr_name, ch->ifname, strerror(errno));
190 }
191 for (ch = clist; ch; ch = ch->next)
192 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
193 break;
194 if (ch == NULL) {
195 continue;
196 }
197 strcpy(ifr.ifr_newname, ch->ifname);
198
199 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0) {;
200 serror_msg_and_die(use_syslog, "cannot change name of %s to %s: %s", ifr.ifr_name, ch->ifname, strerror(errno));
201 }
202 *ch->pprev = ch->next;
203 free(ch);
204 free(line);
205 }
206 fclose(ifh);
207
208 while (clist) {
209 mactable_t *ch;
210
211 ch = clist;
212 clist = clist->next;
213 free(ch);
214 }
215
216 return 0;
217}