blob: daac191bfcc1bb318e9fadf12d5f61c347e7b735 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.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
Denis Vlasenko06af2162007-02-03 17:28:39 +000058int nameif_main(int argc, char **argv);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000059int nameif_main(int argc, char **argv)
60{
61 mactable_t *clist = NULL;
62 FILE *ifh;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000063 const char *fname = "/etc/mactab";
Glenn L McGrathf03c9332002-12-13 00:01:44 +000064 char *line;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000065 int ctl_sk;
Glenn L McGrath688cf012002-12-17 12:43:43 +000066 int if_index = 1;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000067 mactable_t *ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000068
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000069 if (1 & getopt32(argv, "sc:", &fname)) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000070 openlog(applet_name, 0, LOG_LOCAL0);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000071 logmode = LOGMODE_SYSLOG;
72 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +000073
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +000074 if ((argc - optind) & 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 bb_show_usage();
Glenn L McGrathf03c9332002-12-13 00:01:44 +000076
77 if (optind < argc) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +000078 char **a = argv + optind;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000079
Glenn L McGrath77c60e52003-01-16 11:37:57 +000080 while (*a) {
Glenn L McGrath77c60e52003-01-16 11:37:57 +000081 if (strlen(*a) > IF_NAMESIZE)
Denis Vlasenko89f0b342006-11-18 22:04:09 +000082 bb_error_msg_and_die("interface name '%s' "
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000083 "too long", *a);
Rob Landleya6e131d2006-05-29 06:43:55 +000084 ch = xzalloc(sizeof(mactable_t));
Rob Landleyd921b2e2006-08-03 15:41:12 +000085 ch->ifname = xstrdup(*a++);
Glenn L McGrath77c60e52003-01-16 11:37:57 +000086 ch->mac = cc_macaddr(*a++);
Glenn L McGrathf03c9332002-12-13 00:01:44 +000087 if (clist)
Glenn L McGrath77c60e52003-01-16 11:37:57 +000088 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000089 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000090 clist = ch;
91 }
92 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +000093 ifh = xfopen(fname, "r");
Glenn L McGrathf03c9332002-12-13 00:01:44 +000094
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000095 while ((line = xmalloc_fgets(ifh)) != NULL) {
Glenn L McGrathf03c9332002-12-13 00:01:44 +000096 char *line_ptr;
Glenn L McGrath77c60e52003-01-16 11:37:57 +000097 size_t name_length;
Glenn L McGrathf03c9332002-12-13 00:01:44 +000098
99 line_ptr = line + strspn(line, " \t");
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000100 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
101 free(line);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000102 continue;
"Vladimir N. Oleynik"8c9daa12006-01-15 09:29:41 +0000103 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000104 name_length = strcspn(line_ptr, " \t");
Rob Landleya6e131d2006-05-29 06:43:55 +0000105 ch = xzalloc(sizeof(mactable_t));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000106 ch->ifname = xstrndup(line_ptr, name_length);
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000107 if (name_length > IF_NAMESIZE)
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000108 bb_error_msg_and_die("interface name '%s' "
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000109 "too long", ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000110 line_ptr += name_length;
111 line_ptr += strspn(line_ptr, " \t");
112 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
113 line_ptr[name_length] = '\0';
Glenn L McGrath77c60e52003-01-16 11:37:57 +0000114 ch->mac = cc_macaddr(line_ptr);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000115 if (clist)
Glenn L McGrath688cf012002-12-17 12:43:43 +0000116 clist->prev = ch;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000117 ch->next = clist;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000118 clist = ch;
119 free(line);
120 }
121 fclose(ifh);
122 }
123
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000124 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000125
Glenn L McGrath688cf012002-12-17 12:43:43 +0000126 while (clist) {
127 struct ifreq ifr;
128
Rob Landley855f1e12006-01-15 02:20:06 +0000129 memset(&ifr, 0, sizeof(struct ifreq));
Glenn L McGrath688cf012002-12-17 12:43:43 +0000130 if_index++;
131 ifr.ifr_ifindex = if_index;
132
133 /* Get ifname by index or die */
134 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
135 break;
136
137 /* Has this device hwaddr? */
138 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000139 continue;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000140
141 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000142 for (ch = clist; ch; ch = ch->next)
143 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
144 break;
Glenn L McGrath688cf012002-12-17 12:43:43 +0000145
146 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
147 if (ch == NULL)
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000148 continue;
Glenn L McGrath8b08bda2002-12-13 09:02:16 +0000149
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000150 strcpy(ifr.ifr_newname, ch->ifname);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000151 ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
152 "cannot change ifname %s to %s",
153 ifr.ifr_name, ch->ifname);
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000154
Glenn L McGrath688cf012002-12-17 12:43:43 +0000155 /* Remove list entry of renamed interface */
156 if (ch->prev != NULL) {
157 (ch->prev)->next = ch->next;
158 } else {
159 clist = ch->next;
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000160 }
Glenn L McGrath688cf012002-12-17 12:43:43 +0000161 if (ch->next != NULL)
162 (ch->next)->prev = ch->prev;
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000163 if (ENABLE_FEATURE_CLEAN_UP) {
164 free(ch->ifname);
165 free(ch->mac);
166 free(ch);
167 }
Glenn L McGrathf03c9332002-12-13 00:01:44 +0000168 }
169
170 return 0;
171}