blob: 886ff49a8703a4f6f142b939f9502662d1691786 [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
Glenn L McGrath688cf012002-12-17 12:43:43 +000025
Glenn L McGrathf03c9332002-12-13 00:01:44 +000026#include <sys/syslog.h>
27#include <sys/socket.h>
28#include <sys/ioctl.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000029#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
Eric Andersenab4e19a2003-01-14 08:54:08 +000038/* take from linux/sockios.h */
39#define SIOCSIFNAME 0x8923 /* set interface name */
40
Glenn L McGrathf03c9332002-12-13 00:01:44 +000041/* Octets in one ethernet addr, from <linux/if_ether.h> */
42#define ETH_ALEN 6
43
44#ifndef ifr_newname
45#define ifr_newname ifr_ifru.ifru_slave
46#endif
47
48typedef struct mactable_s {
49 struct mactable_s *next;
Glenn L McGrath688cf012002-12-17 12:43:43 +000050 struct mactable_s *prev;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000051 char *ifname;
52 struct ether_addr *mac;
53} mactable_t;
54
Glenn L McGrath688cf012002-12-17 12:43:43 +000055static void serror(const char use_syslog, const char *s, ...)
Glenn L McGrathf03c9332002-12-13 00:01:44 +000056{
57 va_list ap;
58
59 va_start(ap, s);
60
61 if (use_syslog) {
62 openlog("nameif", 0, LOG_LOCAL0);
63 syslog(LOG_ERR, s, ap);
64 closelog();
65 } else {
66 vfprintf(stderr, s, ap);
67 putc('\n', stderr);
68 }
69
70 va_end(ap);
Glenn L McGrath688cf012002-12-17 12:43:43 +000071
Glenn L McGrathf03c9332002-12-13 00:01:44 +000072 exit(EXIT_FAILURE);
73}
74
Glenn L McGrath688cf012002-12-17 12:43:43 +000075/* Check ascii str_macaddr, convert and copy to *mac */
76struct ether_addr *cc_macaddr(char *str_macaddr, unsigned char use_syslog)
77{
78 struct ether_addr *lmac, *mac;
79
80 lmac = ether_aton(str_macaddr);
81 if (lmac == NULL)
82 serror(use_syslog, "cannot parse MAC %s", str_macaddr);
83 mac = xcalloc(1, ETH_ALEN);
84 memcpy(mac, lmac, ETH_ALEN);
85
86 return mac;
87}
88
Glenn L McGrathf03c9332002-12-13 00:01:44 +000089int nameif_main(int argc, char **argv)
90{
91 mactable_t *clist = NULL;
92 FILE *ifh;
93 char *fname = "/etc/mactab";
94 char *line;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000095 unsigned char use_syslog = 0;
96 int ctl_sk = -1;
97 int opt;
Glenn L McGrath688cf012002-12-17 12:43:43 +000098 int if_index = 1;
99 mactable_t *ch = NULL;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000100
101 static struct option opts[] = {
102 {"syslog", 0, NULL, 's'},
103 {"configfile", 1, NULL, 'c'},
104 {NULL},
105 };
106
107 while ((opt = getopt_long(argc, argv, "c:s", opts, NULL)) != -1) {
108 switch (opt) {
109 case 'c':
110 fname = optarg;
111 break;
112 case 's':
113 use_syslog = 1;
114 break;
115 default:
116 show_usage();
117 }
118 }
119
Glenn L McGrath688cf012002-12-17 12:43:43 +0000120 if ((argc - optind) & 1)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000121 show_usage();
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000122
123 if (optind < argc) {
124 while (optind < argc) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000125
Glenn L McGrath688cf012002-12-17 12:43:43 +0000126 if (strlen(argv[optind]) > IF_NAMESIZE)
127 serror(use_syslog, "interface name `%s' too long",
128 argv[optind]);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000129 optind++;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000130
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000131 ch = xcalloc(1, sizeof(mactable_t));
Glenn L McGrath688cf012002-12-17 12:43:43 +0000132 ch->next = NULL;
133 ch->prev = NULL;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000134 ch->ifname = strdup(argv[optind - 1]);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000135 ch->mac = cc_macaddr(argv[optind], use_syslog);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000136 optind++;
137 if (clist)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000138 clist->prev = ch->next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000139 ch->next = clist;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000140 ch->prev = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000141 clist = ch;
142 }
143 } else {
144 ifh = xfopen(fname, "r");
145
146 while ((line = get_line_from_file(ifh)) != NULL) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000147 char *line_ptr;
148 unsigned short name_length;
149
150 line_ptr = line + strspn(line, " \t");
151 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
152 continue;
153 name_length = strcspn(line_ptr, " \t");
Glenn L McGrath688cf012002-12-17 12:43:43 +0000154 if (name_length > IF_NAMESIZE)
155 serror(use_syslog, "interface name `%s' too long",
156 argv[optind]);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000157 ch = xcalloc(1, sizeof(mactable_t));
Glenn L McGrath688cf012002-12-17 12:43:43 +0000158 ch->next = NULL;
159 ch->prev = NULL;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000160 ch->ifname = strndup(line_ptr, name_length);
161 line_ptr += name_length;
162 line_ptr += strspn(line_ptr, " \t");
163 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
164 line_ptr[name_length] = '\0';
Glenn L McGrath688cf012002-12-17 12:43:43 +0000165 ch->mac = cc_macaddr(line_ptr, use_syslog);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000166 if (clist)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000167 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000168 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000169 clist = ch;
170 free(line);
171 }
172 fclose(ifh);
173 }
174
Glenn L McGrath688cf012002-12-17 12:43:43 +0000175 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
176 serror(use_syslog, "socket: %s", strerror(errno));
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000177
Glenn L McGrath688cf012002-12-17 12:43:43 +0000178 while (clist) {
179 struct ifreq ifr;
180
181 bzero(&ifr, sizeof(struct ifreq));
182 if_index++;
183 ifr.ifr_ifindex = if_index;
184
185 /* Get ifname by index or die */
186 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
187 break;
188
189 /* Has this device hwaddr? */
190 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000191 continue;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000192
193 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000194 for (ch = clist; ch; ch = ch->next)
195 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
196 break;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000197
198 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
199 if (ch == NULL)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000200 continue;
Glenn L McGrath8b08bda2002-12-13 09:02:16 +0000201
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000202 strcpy(ifr.ifr_newname, ch->ifname);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000203 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
204 serror(use_syslog, "cannot change ifname %s to %s: %s",
205 ifr.ifr_name, ch->ifname, strerror(errno));
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000206
Glenn L McGrath688cf012002-12-17 12:43:43 +0000207 /* Remove list entry of renamed interface */
208 if (ch->prev != NULL) {
209 (ch->prev)->next = ch->next;
210 } else {
211 clist = ch->next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000212 }
Glenn L McGrath688cf012002-12-17 12:43:43 +0000213 if (ch->next != NULL)
214 (ch->next)->prev = ch->prev;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000215 free(ch);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000216 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000217
218 while (clist) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000219 ch = clist;
220 clist = clist->next;
221 free(ch);
222 }
223
224 return 0;
225}