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