blob: 146eccea9ebaea0800f95c4cdf33b0b639950ed5 [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
Glenn L McGrath688cf012002-12-17 12:43:43 +000044/* Check ascii str_macaddr, convert and copy to *mac */
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000045static struct ether_addr *cc_macaddr(const char *str_macaddr)
Glenn L McGrath688cf012002-12-17 12:43:43 +000046{
47 struct ether_addr *lmac, *mac;
48
49 lmac = ether_aton(str_macaddr);
50 if (lmac == NULL)
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000051 bb_error_msg_and_die("cannot parse MAC %s", str_macaddr);
Glenn L McGrath77c60e52003-01-16 11:37:57 +000052 mac = xmalloc(ETH_ALEN);
Glenn L McGrath688cf012002-12-17 12:43:43 +000053 memcpy(mac, lmac, ETH_ALEN);
54
55 return mac;
56}
57
Glenn L McGrathf03c9332002-12-13 00:01:44 +000058int nameif_main(int argc, char **argv)
59{
60 mactable_t *clist = NULL;
61 FILE *ifh;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000062 const char *fname = "/etc/mactab";
Glenn L McGrathf03c9332002-12-13 00:01:44 +000063 char *line;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000064 int ctl_sk;
Glenn L McGrath688cf012002-12-17 12:43:43 +000065 int if_index = 1;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000066 mactable_t *ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000067
Denis Vlasenko67b23e62006-10-03 21:00:06 +000068 if (1 & getopt32(argc, argv, "sc:", &fname)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000069 openlog(bb_applet_name, 0, LOG_LOCAL0);
70 logmode = LOGMODE_SYSLOG;
71 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +000072
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000073 if ((argc - optind) & 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_show_usage();
Glenn L McGrathf03c9332002-12-13 00:01:44 +000075
76 if (optind < argc) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +000077 char **a = argv + optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000078
Glenn L McGrath77c60e52003-01-16 11:37:57 +000079 while (*a) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +000080 if (strlen(*a) > IF_NAMESIZE)
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000081 bb_error_msg_and_die("interface name `%s' "
82 "too long", *a);
Rob Landleya6e131d2006-05-29 06:43:55 +000083 ch = xzalloc(sizeof(mactable_t));
Rob Landleyd921b2e2006-08-03 15:41:12 +000084 ch->ifname = xstrdup(*a++);
Glenn L McGrath77c60e52003-01-16 11:37:57 +000085 ch->mac = cc_macaddr(*a++);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000086 if (clist)
Glenn L McGrath77c60e52003-01-16 11:37:57 +000087 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000088 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000089 clist = ch;
90 }
91 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +000092 ifh = xfopen(fname, "r");
Glenn L McGrathf03c9332002-12-13 00:01:44 +000093
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 while ((line = bb_get_line_from_file(ifh)) != NULL) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +000095 char *line_ptr;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000096 size_t name_length;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000097
98 line_ptr = line + strspn(line, " \t");
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000099 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
100 free(line);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000101 continue;
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000102 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000103 name_length = strcspn(line_ptr, " \t");
Rob Landleya6e131d2006-05-29 06:43:55 +0000104 ch = xzalloc(sizeof(mactable_t));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000105 ch->ifname = xstrndup(line_ptr, name_length);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000106 if (name_length > IF_NAMESIZE)
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000107 bb_error_msg_and_die("interface name `%s' "
108 "too long", ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000109 line_ptr += name_length;
110 line_ptr += strspn(line_ptr, " \t");
111 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
112 line_ptr[name_length] = '\0';
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000113 ch->mac = cc_macaddr(line_ptr);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000114 if (clist)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000115 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000116 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000117 clist = ch;
118 free(line);
119 }
120 fclose(ifh);
121 }
122
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000123 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000124
Glenn L McGrath688cf012002-12-17 12:43:43 +0000125 while (clist) {
126 struct ifreq ifr;
127
Rob Landley855f1e12006-01-15 02:20:06 +0000128 memset(&ifr, 0, sizeof(struct ifreq));
Glenn L McGrath688cf012002-12-17 12:43:43 +0000129 if_index++;
130 ifr.ifr_ifindex = if_index;
131
132 /* Get ifname by index or die */
133 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
134 break;
135
136 /* Has this device hwaddr? */
137 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000138 continue;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000139
140 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000141 for (ch = clist; ch; ch = ch->next)
142 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
143 break;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000144
145 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
146 if (ch == NULL)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000147 continue;
Glenn L McGrath8b08bda2002-12-13 09:02:16 +0000148
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000149 strcpy(ifr.ifr_newname, ch->ifname);
Glenn L McGrath688cf012002-12-17 12:43:43 +0000150 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000151 bb_perror_msg_and_die("cannot change ifname %s to %s",
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000152 ifr.ifr_name, ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000153
Glenn L McGrath688cf012002-12-17 12:43:43 +0000154 /* Remove list entry of renamed interface */
155 if (ch->prev != NULL) {
156 (ch->prev)->next = ch->next;
157 } else {
158 clist = ch->next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000159 }
Glenn L McGrath688cf012002-12-17 12:43:43 +0000160 if (ch->next != NULL)
161 (ch->next)->prev = ch->prev;
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000162 if (ENABLE_FEATURE_CLEAN_UP) {
163 free(ch->ifname);
164 free(ch->mac);
165 free(ch);
166 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000167 }
168
169 return 0;
170}