[821] | 1 | /*
|
---|
| 2 | * nameif.c - Naming Interfaces based on MAC address for busybox.
|
---|
| 3 | *
|
---|
| 4 | * Written 2000 by Andi Kleen.
|
---|
| 5 | * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
|
---|
| 6 | * Glenn McGrath <bug1@iinet.net.au>
|
---|
| 7 | *
|
---|
| 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | #include "busybox.h"
|
---|
| 12 |
|
---|
| 13 | #include <sys/syslog.h>
|
---|
| 14 | #include <sys/socket.h>
|
---|
| 15 | #include <sys/ioctl.h>
|
---|
| 16 | #include <errno.h>
|
---|
| 17 | #include <string.h>
|
---|
| 18 | #include <unistd.h>
|
---|
| 19 | #include <net/if.h>
|
---|
| 20 | #include <netinet/ether.h>
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | /* 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 |
|
---|
| 32 | /* take from linux/sockios.h */
|
---|
| 33 | #define SIOCSIFNAME 0x8923 /* set interface name */
|
---|
| 34 |
|
---|
| 35 | /* Octets in one Ethernet addr, from <linux/if_ether.h> */
|
---|
| 36 | #define ETH_ALEN 6
|
---|
| 37 |
|
---|
| 38 | #ifndef ifr_newname
|
---|
| 39 | #define ifr_newname ifr_ifru.ifru_slave
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
| 42 | typedef struct mactable_s {
|
---|
| 43 | struct mactable_s *next;
|
---|
| 44 | struct mactable_s *prev;
|
---|
| 45 | char *ifname;
|
---|
| 46 | struct ether_addr *mac;
|
---|
| 47 | } mactable_t;
|
---|
| 48 |
|
---|
| 49 | static unsigned long flags;
|
---|
| 50 |
|
---|
| 51 | static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
|
---|
| 52 |
|
---|
| 53 | static void serror(const char *s, ...)
|
---|
| 54 | {
|
---|
| 55 | va_list ap;
|
---|
| 56 |
|
---|
| 57 | va_start(ap, s);
|
---|
| 58 |
|
---|
| 59 | if (flags & 1) {
|
---|
| 60 | openlog(bb_applet_name, 0, LOG_LOCAL0);
|
---|
| 61 | vsyslog(LOG_ERR, s, ap);
|
---|
| 62 | closelog();
|
---|
| 63 | } else {
|
---|
| 64 | bb_verror_msg(s, ap);
|
---|
| 65 | putc('\n', stderr);
|
---|
| 66 | }
|
---|
| 67 | va_end(ap);
|
---|
| 68 |
|
---|
| 69 | exit(EXIT_FAILURE);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /* Check ascii str_macaddr, convert and copy to *mac */
|
---|
| 73 | static struct ether_addr *cc_macaddr(const char *str_macaddr)
|
---|
| 74 | {
|
---|
| 75 | struct ether_addr *lmac, *mac;
|
---|
| 76 |
|
---|
| 77 | lmac = ether_aton(str_macaddr);
|
---|
| 78 | if (lmac == NULL)
|
---|
| 79 | serror("cannot parse MAC %s", str_macaddr);
|
---|
| 80 | mac = xmalloc(ETH_ALEN);
|
---|
| 81 | memcpy(mac, lmac, ETH_ALEN);
|
---|
| 82 |
|
---|
| 83 | return mac;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | int nameif_main(int argc, char **argv)
|
---|
| 87 | {
|
---|
| 88 | mactable_t *clist = NULL;
|
---|
| 89 | FILE *ifh;
|
---|
| 90 | const char *fname = "/etc/mactab";
|
---|
| 91 | char *line;
|
---|
| 92 | int ctl_sk;
|
---|
| 93 | int if_index = 1;
|
---|
| 94 | mactable_t *ch;
|
---|
| 95 |
|
---|
| 96 | flags = bb_getopt_ulflags(argc, argv, "sc:", &fname);
|
---|
| 97 |
|
---|
| 98 | if ((argc - optind) & 1)
|
---|
| 99 | bb_show_usage();
|
---|
| 100 |
|
---|
| 101 | if (optind < argc) {
|
---|
| 102 | char **a = argv + optind;
|
---|
| 103 |
|
---|
| 104 | while (*a) {
|
---|
| 105 |
|
---|
| 106 | if (strlen(*a) > IF_NAMESIZE)
|
---|
| 107 | serror("interface name `%s' too long", *a);
|
---|
| 108 | ch = xzalloc(sizeof(mactable_t));
|
---|
| 109 | ch->ifname = bb_xstrdup(*a++);
|
---|
| 110 | ch->mac = cc_macaddr(*a++);
|
---|
| 111 | if (clist)
|
---|
| 112 | clist->prev = ch;
|
---|
| 113 | ch->next = clist;
|
---|
| 114 | clist = ch;
|
---|
| 115 | }
|
---|
| 116 | } else {
|
---|
| 117 | ifh = bb_xfopen(fname, "r");
|
---|
| 118 |
|
---|
| 119 | while ((line = bb_get_line_from_file(ifh)) != NULL) {
|
---|
| 120 | char *line_ptr;
|
---|
| 121 | size_t name_length;
|
---|
| 122 |
|
---|
| 123 | line_ptr = line + strspn(line, " \t");
|
---|
| 124 | if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
|
---|
| 125 | free(line);
|
---|
| 126 | continue;
|
---|
| 127 | }
|
---|
| 128 | name_length = strcspn(line_ptr, " \t");
|
---|
| 129 | ch = xzalloc(sizeof(mactable_t));
|
---|
| 130 | ch->ifname = bb_xstrndup(line_ptr, name_length);
|
---|
| 131 | if (name_length > IF_NAMESIZE)
|
---|
| 132 | serror("interface name `%s' too long", ch->ifname);
|
---|
| 133 | line_ptr += name_length;
|
---|
| 134 | line_ptr += strspn(line_ptr, " \t");
|
---|
| 135 | name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
|
---|
| 136 | line_ptr[name_length] = '\0';
|
---|
| 137 | ch->mac = cc_macaddr(line_ptr);
|
---|
| 138 | if (clist)
|
---|
| 139 | clist->prev = ch;
|
---|
| 140 | ch->next = clist;
|
---|
| 141 | clist = ch;
|
---|
| 142 | free(line);
|
---|
| 143 | }
|
---|
| 144 | fclose(ifh);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
|
---|
| 148 | serror("socket: %m");
|
---|
| 149 |
|
---|
| 150 | while (clist) {
|
---|
| 151 | struct ifreq ifr;
|
---|
| 152 |
|
---|
| 153 | memset(&ifr, 0, sizeof(struct ifreq));
|
---|
| 154 | 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))
|
---|
| 163 | continue;
|
---|
| 164 |
|
---|
| 165 | /* Search for mac like in ifr.ifr_hwaddr.sa_data */
|
---|
| 166 | for (ch = clist; ch; ch = ch->next)
|
---|
| 167 | if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
|
---|
| 168 | break;
|
---|
| 169 |
|
---|
| 170 | /* Nothing found for current ifr.ifr_hwaddr.sa_data */
|
---|
| 171 | if (ch == NULL)
|
---|
| 172 | continue;
|
---|
| 173 |
|
---|
| 174 | strcpy(ifr.ifr_newname, ch->ifname);
|
---|
| 175 | if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
|
---|
| 176 | serror("cannot change ifname %s to %s: %m",
|
---|
| 177 | ifr.ifr_name, ch->ifname);
|
---|
| 178 |
|
---|
| 179 | /* Remove list entry of renamed interface */
|
---|
| 180 | if (ch->prev != NULL) {
|
---|
| 181 | (ch->prev)->next = ch->next;
|
---|
| 182 | } else {
|
---|
| 183 | clist = ch->next;
|
---|
| 184 | }
|
---|
| 185 | if (ch->next != NULL)
|
---|
| 186 | (ch->next)->prev = ch->prev;
|
---|
| 187 | if (ENABLE_FEATURE_CLEAN_UP) {
|
---|
| 188 | free(ch->ifname);
|
---|
| 189 | free(ch->mac);
|
---|
| 190 | free(ch);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | return 0;
|
---|
| 195 | }
|
---|