blob: f3b9271584373b2ed1bdfa2e363d36d1528a171f [file] [log] [blame]
Glenn L McGrath77c60e52003-01-16 11:37:57 +00001/*
Glenn L McGrathf03c9332002-12-13 00:01:44 +00002 * 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 */
Glenn L McGratha9adef02003-01-19 13:34:21 +000039#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000040
Glenn L McGrath77c60e52003-01-16 11:37:57 +000041/* Octets in one ethernet addr, from <linux/if_ether.h> */
Glenn L McGrathf03c9332002-12-13 00:01:44 +000042#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 McGrath77c60e52003-01-16 11:37:57 +000055static unsigned char use_syslog;
56
57static void serror(const char *s, ...) __attribute__ ((noreturn));
58
59static void serror(const char *s, ...)
Glenn L McGrathf03c9332002-12-13 00:01:44 +000060{
61 va_list ap;
62
63 va_start(ap, s);
64
65 if (use_syslog) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +000066 openlog(applet_name, 0, LOG_LOCAL0);
67 vsyslog(LOG_ERR, s, ap);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000068 closelog();
69 } else {
Glenn L McGrath77c60e52003-01-16 11:37:57 +000070 verror_msg(s, ap);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000071 putc('\n', stderr);
72 }
73
74 va_end(ap);
Glenn L McGrath688cf012002-12-17 12:43:43 +000075
Glenn L McGrathf03c9332002-12-13 00:01:44 +000076 exit(EXIT_FAILURE);
77}
78
Glenn L McGrath688cf012002-12-17 12:43:43 +000079/* Check ascii str_macaddr, convert and copy to *mac */
Glenn L McGrath77c60e52003-01-16 11:37:57 +000080struct ether_addr *cc_macaddr(char *str_macaddr)
Glenn L McGrath688cf012002-12-17 12:43:43 +000081{
82 struct ether_addr *lmac, *mac;
83
84 lmac = ether_aton(str_macaddr);
85 if (lmac == NULL)
Glenn L McGrath77c60e52003-01-16 11:37:57 +000086 serror("cannot parse MAC %s", str_macaddr);
87 mac = xmalloc(ETH_ALEN);
Glenn L McGrath688cf012002-12-17 12:43:43 +000088 memcpy(mac, lmac, ETH_ALEN);
89
90 return mac;
91}
92
Glenn L McGrathf03c9332002-12-13 00:01:44 +000093int nameif_main(int argc, char **argv)
94{
95 mactable_t *clist = NULL;
96 FILE *ifh;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000097 const char *fname = "/etc/mactab";
Glenn L McGrathf03c9332002-12-13 00:01:44 +000098 char *line;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000099 int ctl_sk;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000100 int opt;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000101 int if_index = 1;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000102 mactable_t *ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000103
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000104
Glenn L McGratha9adef02003-01-19 13:34:21 +0000105 while ((opt = getopt(argc, argv, "c:s")) != -1) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000106 switch (opt) {
107 case 'c':
108 fname = optarg;
109 break;
110 case 's':
111 use_syslog = 1;
112 break;
113 default:
114 show_usage();
115 }
116 }
117
Glenn L McGrath688cf012002-12-17 12:43:43 +0000118 if ((argc - optind) & 1)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000119 show_usage();
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000120
121 if (optind < argc) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000122 char **a = argv + optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000123
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000124 while (*a) {
Glenn L McGrath688cf012002-12-17 12:43:43 +0000125
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000126 if (strlen(*a) > IF_NAMESIZE)
127 serror("interface name `%s' too long", *a);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000128 ch = xcalloc(1, sizeof(mactable_t));
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000129 ch->ifname = xstrdup(*a++);
130 ch->mac = cc_macaddr(*a++);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000131 if (clist)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000132 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000133 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000134 clist = ch;
135 }
136 } else {
137 ifh = xfopen(fname, "r");
138
139 while ((line = get_line_from_file(ifh)) != NULL) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000140 char *line_ptr;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000141 size_t name_length;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000142
143 line_ptr = line + strspn(line, " \t");
144 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
145 continue;
146 name_length = strcspn(line_ptr, " \t");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000147 ch = xcalloc(1, sizeof(mactable_t));
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000148 ch->ifname = xstrndup(line_ptr, name_length);
149 if (name_length > IF_NAMESIZE)
Glenn L McGratha9adef02003-01-19 13:34:21 +0000150 serror("interface name `%s' too long", ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000151 line_ptr += name_length;
152 line_ptr += strspn(line_ptr, " \t");
153 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
154 line_ptr[name_length] = '\0';
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000155 ch->mac = cc_macaddr(line_ptr);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000156 if (clist)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000157 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000158 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000159 clist = ch;
160 free(line);
161 }
162 fclose(ifh);
163 }
164
Glenn L McGrath688cf012002-12-17 12:43:43 +0000165 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000166 serror("socket: %m");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000167
Glenn L McGrath688cf012002-12-17 12:43:43 +0000168 while (clist) {
169 struct ifreq ifr;
170
171 bzero(&ifr, sizeof(struct ifreq));
172 if_index++;
173 ifr.ifr_ifindex = if_index;
174
175 /* Get ifname by index or die */
176 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
177 break;
178
179 /* Has this device hwaddr? */
180 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000181 continue;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000182
183 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000184 for (ch = clist; ch; ch = ch->next)
185 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
186 break;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000187
188 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
189 if (ch == NULL)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000190 continue;
Glenn L McGrath8b08bda2002-12-13 09:02:16 +0000191
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000192 strcpy(ifr.ifr_newname, ch->ifname);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000193 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000194 serror("cannot change ifname %s to %s: %m",
195 ifr.ifr_name, ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000196
Glenn L McGrath688cf012002-12-17 12:43:43 +0000197 /* Remove list entry of renamed interface */
198 if (ch->prev != NULL) {
199 (ch->prev)->next = ch->next;
200 } else {
201 clist = ch->next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000202 }
Glenn L McGrath688cf012002-12-17 12:43:43 +0000203 if (ch->next != NULL)
204 (ch->next)->prev = ch->prev;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000205#ifdef CONFIG_FEATURE_CLEAN_UP
206 free(ch->ifname);
207 free(ch->mac);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000208 free(ch);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000209#endif
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000210 }
211
212 return 0;
213}