blob: f13ef1b8b2eddfc9b38104468245cdf32eb66c8d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath77c60e52003-01-16 11:37:57 +00002/*
Glenn L McGrathf03c9332002-12-13 00:01:44 +00003 * nameif.c - Naming Interfaces based on MAC address for busybox.
4 *
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * Written 2000 by Andi Kleen.
Glenn L McGrathf03c9332002-12-13 00:01:44 +00006 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00007 * Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrathf03c9332002-12-13 00:01:44 +00008 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00009 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrathf03c9332002-12-13 00:01:44 +000010 */
11
Rob Landley362dc2b2006-06-05 17:35:24 +000012#include "busybox.h"
13
Glenn L McGrathf03c9332002-12-13 00:01:44 +000014#include <sys/syslog.h>
15#include <sys/socket.h>
16#include <sys/ioctl.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000017#include <errno.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000018#include <string.h>
Rob Landley855f1e12006-01-15 02:20:06 +000019#include <unistd.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000020#include <net/if.h>
21#include <netinet/ether.h>
22
Glenn L McGrathf03c9332002-12-13 00:01:44 +000023
Eric Andersen40ea66c2003-07-05 08:00:17 +000024/* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
25#ifndef IF_NAMESIZE
26# ifdef IFNAMSIZ
27# define IF_NAMESIZE IFNAMSIZ
28# else
29# define IF_NAMESIZE 16
30# endif
31#endif
32
Eric Andersenab4e19a2003-01-14 08:54:08 +000033/* take from linux/sockios.h */
Glenn L McGratha9adef02003-01-19 13:34:21 +000034#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000035
Eric Andersenaff114c2004-04-14 17:51:38 +000036/* Octets in one Ethernet addr, from <linux/if_ether.h> */
Glenn L McGrathf03c9332002-12-13 00:01:44 +000037#define ETH_ALEN 6
38
39#ifndef ifr_newname
40#define ifr_newname ifr_ifru.ifru_slave
41#endif
42
43typedef struct mactable_s {
44 struct mactable_s *next;
Glenn L McGrath688cf012002-12-17 12:43:43 +000045 struct mactable_s *prev;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000046 char *ifname;
47 struct ether_addr *mac;
48} mactable_t;
49
Rob Landley855f1e12006-01-15 02:20:06 +000050static unsigned long flags;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000051
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000052static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000053
54static void serror(const char *s, ...)
Glenn L McGrathf03c9332002-12-13 00:01:44 +000055{
56 va_list ap;
57
58 va_start(ap, s);
59
Rob Landley855f1e12006-01-15 02:20:06 +000060 if (flags & 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 openlog(bb_applet_name, 0, LOG_LOCAL0);
Glenn L McGrath77c60e52003-01-16 11:37:57 +000062 vsyslog(LOG_ERR, s, ap);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000063 closelog();
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000064 } else {
65 bb_verror_msg(s, ap);
66 putc('\n', stderr);
67 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +000068 va_end(ap);
Glenn L McGrath688cf012002-12-17 12:43:43 +000069
Glenn L McGrathf03c9332002-12-13 00:01:44 +000070 exit(EXIT_FAILURE);
71}
72
Glenn L McGrath688cf012002-12-17 12:43:43 +000073/* Check ascii str_macaddr, convert and copy to *mac */
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000074static struct ether_addr *cc_macaddr(const char *str_macaddr)
Glenn L McGrath688cf012002-12-17 12:43:43 +000075{
76 struct ether_addr *lmac, *mac;
77
78 lmac = ether_aton(str_macaddr);
79 if (lmac == NULL)
Glenn L McGrath77c60e52003-01-16 11:37:57 +000080 serror("cannot parse MAC %s", str_macaddr);
81 mac = xmalloc(ETH_ALEN);
Glenn L McGrath688cf012002-12-17 12:43:43 +000082 memcpy(mac, lmac, ETH_ALEN);
83
84 return mac;
85}
86
Glenn L McGrathf03c9332002-12-13 00:01:44 +000087int nameif_main(int argc, char **argv)
88{
89 mactable_t *clist = NULL;
90 FILE *ifh;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000091 const char *fname = "/etc/mactab";
Glenn L McGrathf03c9332002-12-13 00:01:44 +000092 char *line;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000093 int ctl_sk;
Glenn L McGrath688cf012002-12-17 12:43:43 +000094 int if_index = 1;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000095 mactable_t *ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000096
Rob Landley855f1e12006-01-15 02:20:06 +000097 flags = bb_getopt_ulflags(argc, argv, "sc:", &fname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000098
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000099 if ((argc - optind) & 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_show_usage();
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000101
102 if (optind < argc) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000103 char **a = argv + optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000104
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000105 while (*a) {
Glenn L McGrath688cf012002-12-17 12:43:43 +0000106
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000107 if (strlen(*a) > IF_NAMESIZE)
108 serror("interface name `%s' too long", *a);
Rob Landleya6e131d2006-05-29 06:43:55 +0000109 ch = xzalloc(sizeof(mactable_t));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 ch->ifname = bb_xstrdup(*a++);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000111 ch->mac = cc_macaddr(*a++);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000112 if (clist)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000113 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000114 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000115 clist = ch;
116 }
117 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 ifh = bb_xfopen(fname, "r");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000119
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 while ((line = bb_get_line_from_file(ifh)) != NULL) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000121 char *line_ptr;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000122 size_t name_length;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000123
124 line_ptr = line + strspn(line, " \t");
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000125 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
126 free(line);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000127 continue;
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000128 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000129 name_length = strcspn(line_ptr, " \t");
Rob Landleya6e131d2006-05-29 06:43:55 +0000130 ch = xzalloc(sizeof(mactable_t));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 ch->ifname = bb_xstrndup(line_ptr, name_length);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000132 if (name_length > IF_NAMESIZE)
Glenn L McGratha9adef02003-01-19 13:34:21 +0000133 serror("interface name `%s' too long", ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000134 line_ptr += name_length;
135 line_ptr += strspn(line_ptr, " \t");
136 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
137 line_ptr[name_length] = '\0';
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000138 ch->mac = cc_macaddr(line_ptr);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000139 if (clist)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000140 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000141 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000142 clist = ch;
143 free(line);
144 }
145 fclose(ifh);
146 }
147
Glenn L McGrath688cf012002-12-17 12:43:43 +0000148 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000149 serror("socket: %m");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000150
Glenn L McGrath688cf012002-12-17 12:43:43 +0000151 while (clist) {
152 struct ifreq ifr;
153
Rob Landley855f1e12006-01-15 02:20:06 +0000154 memset(&ifr, 0, sizeof(struct ifreq));
Glenn L McGrath688cf012002-12-17 12:43:43 +0000155 if_index++;
156 ifr.ifr_ifindex = if_index;
157
158 /* Get ifname by index or die */
159 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
160 break;
161
162 /* Has this device hwaddr? */
163 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000164 continue;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000165
166 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000167 for (ch = clist; ch; ch = ch->next)
168 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
169 break;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000170
171 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
172 if (ch == NULL)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000173 continue;
Glenn L McGrath8b08bda2002-12-13 09:02:16 +0000174
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000175 strcpy(ifr.ifr_newname, ch->ifname);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000176 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000177 serror("cannot change ifname %s to %s: %m",
178 ifr.ifr_name, ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000179
Glenn L McGrath688cf012002-12-17 12:43:43 +0000180 /* Remove list entry of renamed interface */
181 if (ch->prev != NULL) {
182 (ch->prev)->next = ch->next;
183 } else {
184 clist = ch->next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000185 }
Glenn L McGrath688cf012002-12-17 12:43:43 +0000186 if (ch->next != NULL)
187 (ch->next)->prev = ch->prev;
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000188 if (ENABLE_FEATURE_CLEAN_UP) {
189 free(ch->ifname);
190 free(ch->mac);
191 free(ch);
192 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000193 }
194
195 return 0;
196}