blob: 501e244b13a5fe181eaa385371a6437be1a7150f [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"
Rob Landleyd921b2e2006-08-03 15:41:12 +000013#include <syslog.h>
Glenn L McGrathf03c9332002-12-13 00:01:44 +000014#include <net/if.h>
15#include <netinet/ether.h>
16
Glenn L McGrathf03c9332002-12-13 00:01:44 +000017
Eric Andersen40ea66c2003-07-05 08:00:17 +000018/* Older versions of net/if.h do not appear to define IF_NAMESIZE. */
19#ifndef IF_NAMESIZE
20# ifdef IFNAMSIZ
21# define IF_NAMESIZE IFNAMSIZ
22# else
23# define IF_NAMESIZE 16
24# endif
25#endif
26
Eric Andersenab4e19a2003-01-14 08:54:08 +000027/* take from linux/sockios.h */
Glenn L McGratha9adef02003-01-19 13:34:21 +000028#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000029
Eric Andersenaff114c2004-04-14 17:51:38 +000030/* Octets in one Ethernet addr, from <linux/if_ether.h> */
Glenn L McGrathf03c9332002-12-13 00:01:44 +000031#define ETH_ALEN 6
32
33#ifndef ifr_newname
34#define ifr_newname ifr_ifru.ifru_slave
35#endif
36
37typedef struct mactable_s {
38 struct mactable_s *next;
Glenn L McGrath688cf012002-12-17 12:43:43 +000039 struct mactable_s *prev;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000040 char *ifname;
41 struct ether_addr *mac;
42} mactable_t;
43
Rob Landley855f1e12006-01-15 02:20:06 +000044static unsigned long flags;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000045
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000046static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000047
48static void serror(const char *s, ...)
Glenn L McGrathf03c9332002-12-13 00:01:44 +000049{
50 va_list ap;
51
52 va_start(ap, s);
53
Rob Landley855f1e12006-01-15 02:20:06 +000054 if (flags & 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 openlog(bb_applet_name, 0, LOG_LOCAL0);
Glenn L McGrath77c60e52003-01-16 11:37:57 +000056 vsyslog(LOG_ERR, s, ap);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000057 closelog();
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000058 } else {
59 bb_verror_msg(s, ap);
60 putc('\n', stderr);
61 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +000062 va_end(ap);
Glenn L McGrath688cf012002-12-17 12:43:43 +000063
Glenn L McGrathf03c9332002-12-13 00:01:44 +000064 exit(EXIT_FAILURE);
65}
66
Glenn L McGrath688cf012002-12-17 12:43:43 +000067/* Check ascii str_macaddr, convert and copy to *mac */
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000068static struct ether_addr *cc_macaddr(const char *str_macaddr)
Glenn L McGrath688cf012002-12-17 12:43:43 +000069{
70 struct ether_addr *lmac, *mac;
71
72 lmac = ether_aton(str_macaddr);
73 if (lmac == NULL)
Glenn L McGrath77c60e52003-01-16 11:37:57 +000074 serror("cannot parse MAC %s", str_macaddr);
75 mac = xmalloc(ETH_ALEN);
Glenn L McGrath688cf012002-12-17 12:43:43 +000076 memcpy(mac, lmac, ETH_ALEN);
77
78 return mac;
79}
80
Glenn L McGrathf03c9332002-12-13 00:01:44 +000081int nameif_main(int argc, char **argv)
82{
83 mactable_t *clist = NULL;
84 FILE *ifh;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000085 const char *fname = "/etc/mactab";
Glenn L McGrathf03c9332002-12-13 00:01:44 +000086 char *line;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000087 int ctl_sk;
Glenn L McGrath688cf012002-12-17 12:43:43 +000088 int if_index = 1;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000089 mactable_t *ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000090
Rob Landley855f1e12006-01-15 02:20:06 +000091 flags = bb_getopt_ulflags(argc, argv, "sc:", &fname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000092
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000093 if ((argc - optind) & 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 bb_show_usage();
Glenn L McGrathf03c9332002-12-13 00:01:44 +000095
96 if (optind < argc) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +000097 char **a = argv + optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000098
Glenn L McGrath77c60e52003-01-16 11:37:57 +000099 while (*a) {
Glenn L McGrath688cf012002-12-17 12:43:43 +0000100
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000101 if (strlen(*a) > IF_NAMESIZE)
102 serror("interface name `%s' too long", *a);
Rob Landleya6e131d2006-05-29 06:43:55 +0000103 ch = xzalloc(sizeof(mactable_t));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000104 ch->ifname = xstrdup(*a++);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000105 ch->mac = cc_macaddr(*a++);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000106 if (clist)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000107 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000108 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000109 clist = ch;
110 }
111 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000112 ifh = xfopen(fname, "r");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000113
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 while ((line = bb_get_line_from_file(ifh)) != NULL) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000115 char *line_ptr;
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000116 size_t name_length;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000117
118 line_ptr = line + strspn(line, " \t");
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000119 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
120 free(line);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000121 continue;
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000122 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000123 name_length = strcspn(line_ptr, " \t");
Rob Landleya6e131d2006-05-29 06:43:55 +0000124 ch = xzalloc(sizeof(mactable_t));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000125 ch->ifname = xstrndup(line_ptr, name_length);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000126 if (name_length > IF_NAMESIZE)
Glenn L McGratha9adef02003-01-19 13:34:21 +0000127 serror("interface name `%s' too long", ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000128 line_ptr += name_length;
129 line_ptr += strspn(line_ptr, " \t");
130 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
131 line_ptr[name_length] = '\0';
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000132 ch->mac = cc_macaddr(line_ptr);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000133 if (clist)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000134 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000135 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000136 clist = ch;
137 free(line);
138 }
139 fclose(ifh);
140 }
141
Glenn L McGrath688cf012002-12-17 12:43:43 +0000142 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000143 serror("socket: %m");
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000144
Glenn L McGrath688cf012002-12-17 12:43:43 +0000145 while (clist) {
146 struct ifreq ifr;
147
Rob Landley855f1e12006-01-15 02:20:06 +0000148 memset(&ifr, 0, sizeof(struct ifreq));
Glenn L McGrath688cf012002-12-17 12:43:43 +0000149 if_index++;
150 ifr.ifr_ifindex = if_index;
151
152 /* Get ifname by index or die */
153 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
154 break;
155
156 /* Has this device hwaddr? */
157 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000158 continue;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000159
160 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000161 for (ch = clist; ch; ch = ch->next)
162 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
163 break;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000164
165 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
166 if (ch == NULL)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000167 continue;
Glenn L McGrath8b08bda2002-12-13 09:02:16 +0000168
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000169 strcpy(ifr.ifr_newname, ch->ifname);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000170 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000171 serror("cannot change ifname %s to %s: %m",
172 ifr.ifr_name, ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000173
Glenn L McGrath688cf012002-12-17 12:43:43 +0000174 /* Remove list entry of renamed interface */
175 if (ch->prev != NULL) {
176 (ch->prev)->next = ch->next;
177 } else {
178 clist = ch->next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000179 }
Glenn L McGrath688cf012002-12-17 12:43:43 +0000180 if (ch->next != NULL)
181 (ch->next)->prev = ch->prev;
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000182 if (ENABLE_FEATURE_CLEAN_UP) {
183 free(ch->ifname);
184 free(ch->mac);
185 free(ch);
186 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000187 }
188
189 return 0;
190}