blob: f28e5ebe33c02c27a164807a3588b32ac07b6600 [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 *
Eric Andersenaff114c2004-04-14 17:51:38 +00004 * Written 2000 by Andi Kleen.
Glenn L McGrathf03c9332002-12-13 00:01:44 +00005 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00006 * Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrathf03c9332002-12-13 00:01:44 +00007 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrathf03c9332002-12-13 00:01:44 +00009 */
10
11#include <sys/syslog.h>
12#include <sys/socket.h>
13#include <sys/ioctl.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000014#include <errno.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000015#include <stdlib.h>
16#include <string.h>
Rob Landley855f1e12006-01-15 02:20:06 +000017#include <unistd.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000018#include <net/if.h>
19#include <netinet/ether.h>
20
21#include "busybox.h"
22
Eric Andersen40ea66c2003-07-05 08:00:17 +000023/* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
24#ifndef IF_NAMESIZE
25# ifdef IFNAMSIZ
26# define IF_NAMESIZE IFNAMSIZ
27# else
28# define IF_NAMESIZE 16
29# endif
30#endif
31
Eric Andersenab4e19a2003-01-14 08:54:08 +000032/* take from linux/sockios.h */
Glenn L McGratha9adef02003-01-19 13:34:21 +000033#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000034
Eric Andersenaff114c2004-04-14 17:51:38 +000035/* Octets in one Ethernet addr, from <linux/if_ether.h> */
Glenn L McGrathf03c9332002-12-13 00:01:44 +000036#define ETH_ALEN 6
37
38#ifndef ifr_newname
39#define ifr_newname ifr_ifru.ifru_slave
40#endif
41
42typedef struct mactable_s {
43 struct mactable_s *next;
Glenn L McGrath688cf012002-12-17 12:43:43 +000044 struct mactable_s *prev;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000045 char *ifname;
46 struct ether_addr *mac;
47} mactable_t;
48
Rob Landley855f1e12006-01-15 02:20:06 +000049static unsigned long flags;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000050
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000051static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000052
53static void serror(const char *s, ...)
Glenn L McGrathf03c9332002-12-13 00:01:44 +000054{
55 va_list ap;
56
57 va_start(ap, s);
58
Rob Landley855f1e12006-01-15 02:20:06 +000059 if (flags & 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 openlog(bb_applet_name, 0, LOG_LOCAL0);
Glenn L McGrath77c60e52003-01-16 11:37:57 +000061 vsyslog(LOG_ERR, s, ap);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000062 closelog();
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000063 } else {
64 bb_verror_msg(s, ap);
65 putc('\n', stderr);
66 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +000067 va_end(ap);
Glenn L McGrath688cf012002-12-17 12:43:43 +000068
Glenn L McGrathf03c9332002-12-13 00:01:44 +000069 exit(EXIT_FAILURE);
70}
71
Glenn L McGrath688cf012002-12-17 12:43:43 +000072/* Check ascii str_macaddr, convert and copy to *mac */
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000073static struct ether_addr *cc_macaddr(const char *str_macaddr)
Glenn L McGrath688cf012002-12-17 12:43:43 +000074{
75 struct ether_addr *lmac, *mac;
76
77 lmac = ether_aton(str_macaddr);
78 if (lmac == NULL)
Glenn L McGrath77c60e52003-01-16 11:37:57 +000079 serror("cannot parse MAC %s", str_macaddr);
80 mac = xmalloc(ETH_ALEN);
Glenn L McGrath688cf012002-12-17 12:43:43 +000081 memcpy(mac, lmac, ETH_ALEN);
82
83 return mac;
84}
85
Glenn L McGrathf03c9332002-12-13 00:01:44 +000086int nameif_main(int argc, char **argv)
87{
88 mactable_t *clist = NULL;
89 FILE *ifh;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000090 const char *fname = "/etc/mactab";
Glenn L McGrathf03c9332002-12-13 00:01:44 +000091 char *line;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000092 int ctl_sk;
Glenn L McGrath688cf012002-12-17 12:43:43 +000093 int if_index = 1;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000094 mactable_t *ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000095
Rob Landley855f1e12006-01-15 02:20:06 +000096 flags = bb_getopt_ulflags(argc, argv, "sc:", &fname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000097
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000098 if ((argc - optind) & 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 bb_show_usage();
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000100
101 if (optind < argc) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000102 char **a = argv + optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000103
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000104 while (*a) {
Glenn L McGrath688cf012002-12-17 12:43:43 +0000105
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000106 if (strlen(*a) > IF_NAMESIZE)
107 serror("interface name `%s' too long", *a);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000108 ch = xcalloc(1, sizeof(mactable_t));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 ch->ifname = bb_xstrdup(*a++);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000110 ch->mac = cc_macaddr(*a++);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000111 if (clist)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000112 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000113 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000114 clist = ch;
115 }
116 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 ifh = bb_xfopen(fname, "r");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000118
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 while ((line = bb_get_line_from_file(ifh)) != NULL) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000120 char *line_ptr;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000121 size_t name_length;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000122
123 line_ptr = line + strspn(line, " \t");
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000124 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
125 free(line);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000126 continue;
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000127 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000128 name_length = strcspn(line_ptr, " \t");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000129 ch = xcalloc(1, sizeof(mactable_t));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 ch->ifname = bb_xstrndup(line_ptr, name_length);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000131 if (name_length > IF_NAMESIZE)
Glenn L McGratha9adef02003-01-19 13:34:21 +0000132 serror("interface name `%s' too long", ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000133 line_ptr += name_length;
134 line_ptr += strspn(line_ptr, " \t");
135 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
136 line_ptr[name_length] = '\0';
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000137 ch->mac = cc_macaddr(line_ptr);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000138 if (clist)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000139 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000140 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000141 clist = ch;
142 free(line);
143 }
144 fclose(ifh);
145 }
146
Glenn L McGrath688cf012002-12-17 12:43:43 +0000147 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000148 serror("socket: %m");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000149
Glenn L McGrath688cf012002-12-17 12:43:43 +0000150 while (clist) {
151 struct ifreq ifr;
152
Rob Landley855f1e12006-01-15 02:20:06 +0000153 memset(&ifr, 0, sizeof(struct ifreq));
Glenn L McGrath688cf012002-12-17 12:43:43 +0000154 if_index++;
155 ifr.ifr_ifindex = if_index;
156
157 /* Get ifname by index or die */
158 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
159 break;
160
161 /* Has this device hwaddr? */
162 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000163 continue;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000164
165 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000166 for (ch = clist; ch; ch = ch->next)
167 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
168 break;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000169
170 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
171 if (ch == NULL)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000172 continue;
Glenn L McGrath8b08bda2002-12-13 09:02:16 +0000173
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000174 strcpy(ifr.ifr_newname, ch->ifname);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000175 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000176 serror("cannot change ifname %s to %s: %m",
177 ifr.ifr_name, ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000178
Glenn L McGrath688cf012002-12-17 12:43:43 +0000179 /* Remove list entry of renamed interface */
180 if (ch->prev != NULL) {
181 (ch->prev)->next = ch->next;
182 } else {
183 clist = ch->next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000184 }
Glenn L McGrath688cf012002-12-17 12:43:43 +0000185 if (ch->next != NULL)
186 (ch->next)->prev = ch->prev;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000187#ifdef CONFIG_FEATURE_CLEAN_UP
188 free(ch->ifname);
189 free(ch->mac);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000190 free(ch);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000191#endif
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000192 }
193
194 return 0;
195}