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 | * |
| 4 | * Writen 2000 by Andi Kleen. |
| 5 | * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua> |
| 6 | * Glenn McGrath <bug1@optushome.com.au> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 21 | * 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 25 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 26 | #include <sys/syslog.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/ioctl.h> |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <getopt.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <net/if.h> |
| 34 | #include <netinet/ether.h> |
| 35 | |
| 36 | #include "busybox.h" |
| 37 | |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 38 | /* take from linux/sockios.h */ |
| 39 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
| 40 | |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 41 | /* Octets in one ethernet addr, from <linux/if_ether.h> */ |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 42 | #define ETH_ALEN 6 |
| 43 | |
| 44 | #ifndef ifr_newname |
| 45 | #define ifr_newname ifr_ifru.ifru_slave |
| 46 | #endif |
| 47 | |
| 48 | typedef struct mactable_s { |
| 49 | struct mactable_s *next; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 50 | struct mactable_s *prev; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 51 | char *ifname; |
| 52 | struct ether_addr *mac; |
| 53 | } mactable_t; |
| 54 | |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 55 | static unsigned char use_syslog; |
| 56 | |
| 57 | static void serror(const char *s, ...) __attribute__ ((noreturn)); |
| 58 | |
| 59 | static void serror(const char *s, ...) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 60 | { |
| 61 | va_list ap; |
| 62 | |
| 63 | va_start(ap, s); |
| 64 | |
| 65 | if (use_syslog) { |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 66 | openlog(applet_name, 0, LOG_LOCAL0); |
| 67 | vsyslog(LOG_ERR, s, ap); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 68 | closelog(); |
| 69 | } else { |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 70 | verror_msg(s, ap); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 71 | putc('\n', stderr); |
| 72 | } |
| 73 | |
| 74 | va_end(ap); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 75 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 76 | exit(EXIT_FAILURE); |
| 77 | } |
| 78 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 79 | /* Check ascii str_macaddr, convert and copy to *mac */ |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 80 | struct ether_addr *cc_macaddr(char *str_macaddr) |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 81 | { |
| 82 | struct ether_addr *lmac, *mac; |
| 83 | |
| 84 | lmac = ether_aton(str_macaddr); |
| 85 | if (lmac == NULL) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 86 | serror("cannot parse MAC %s", str_macaddr); |
| 87 | mac = xmalloc(ETH_ALEN); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 88 | memcpy(mac, lmac, ETH_ALEN); |
| 89 | |
| 90 | return mac; |
| 91 | } |
| 92 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 93 | int nameif_main(int argc, char **argv) |
| 94 | { |
| 95 | mactable_t *clist = NULL; |
| 96 | FILE *ifh; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 97 | const char *fname = "/etc/mactab"; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 98 | char *line; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 99 | int ctl_sk; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 100 | int opt; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 101 | int if_index = 1; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 102 | mactable_t *ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 103 | |
| 104 | static struct option opts[] = { |
| 105 | {"syslog", 0, NULL, 's'}, |
| 106 | {"configfile", 1, NULL, 'c'}, |
| 107 | {NULL}, |
| 108 | }; |
| 109 | |
| 110 | while ((opt = getopt_long(argc, argv, "c:s", opts, NULL)) != -1) { |
| 111 | switch (opt) { |
| 112 | case 'c': |
| 113 | fname = optarg; |
| 114 | break; |
| 115 | case 's': |
| 116 | use_syslog = 1; |
| 117 | break; |
| 118 | default: |
| 119 | show_usage(); |
| 120 | } |
| 121 | } |
| 122 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 123 | if ((argc - optind) & 1) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 124 | show_usage(); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 125 | |
| 126 | if (optind < argc) { |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 127 | char **a = argv + optind; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 128 | |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 129 | while (*a) { |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 130 | |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 131 | if (strlen(*a) > IF_NAMESIZE) |
| 132 | serror("interface name `%s' too long", *a); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 133 | ch = xcalloc(1, sizeof(mactable_t)); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 134 | ch->ifname = xstrdup(*a++); |
| 135 | ch->mac = cc_macaddr(*a++); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 136 | if (clist) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 137 | clist->prev = ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 138 | ch->next = clist; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 139 | clist = ch; |
| 140 | } |
| 141 | } else { |
| 142 | ifh = xfopen(fname, "r"); |
| 143 | |
| 144 | while ((line = get_line_from_file(ifh)) != NULL) { |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 145 | char *line_ptr; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 146 | size_t name_length; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 147 | |
| 148 | line_ptr = line + strspn(line, " \t"); |
| 149 | if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) |
| 150 | continue; |
| 151 | name_length = strcspn(line_ptr, " \t"); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 152 | ch = xcalloc(1, sizeof(mactable_t)); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 153 | ch->ifname = xstrndup(line_ptr, name_length); |
| 154 | if (name_length > IF_NAMESIZE) |
| 155 | serror("interface name `%s' too long", |
| 156 | ch->ifname); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 157 | line_ptr += name_length; |
| 158 | line_ptr += strspn(line_ptr, " \t"); |
| 159 | name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:"); |
| 160 | line_ptr[name_length] = '\0'; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 161 | ch->mac = cc_macaddr(line_ptr); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 162 | if (clist) |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 163 | clist->prev = ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 164 | ch->next = clist; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 165 | clist = ch; |
| 166 | free(line); |
| 167 | } |
| 168 | fclose(ifh); |
| 169 | } |
| 170 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 171 | if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 172 | serror("socket: %m"); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 173 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 174 | while (clist) { |
| 175 | struct ifreq ifr; |
| 176 | |
| 177 | bzero(&ifr, sizeof(struct ifreq)); |
| 178 | if_index++; |
| 179 | ifr.ifr_ifindex = if_index; |
| 180 | |
| 181 | /* Get ifname by index or die */ |
| 182 | if (ioctl(ctl_sk, SIOCGIFNAME, &ifr)) |
| 183 | break; |
| 184 | |
| 185 | /* Has this device hwaddr? */ |
| 186 | if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr)) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 187 | continue; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 188 | |
| 189 | /* Search for mac like in ifr.ifr_hwaddr.sa_data */ |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 190 | for (ch = clist; ch; ch = ch->next) |
| 191 | if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN)) |
| 192 | break; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 193 | |
| 194 | /* Nothing found for current ifr.ifr_hwaddr.sa_data */ |
| 195 | if (ch == NULL) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 196 | continue; |
Glenn L McGrath | 8b08bda | 2002-12-13 09:02:16 +0000 | [diff] [blame] | 197 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 198 | strcpy(ifr.ifr_newname, ch->ifname); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 199 | if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 200 | serror("cannot change ifname %s to %s: %m", |
| 201 | ifr.ifr_name, ch->ifname); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 202 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 203 | /* Remove list entry of renamed interface */ |
| 204 | if (ch->prev != NULL) { |
| 205 | (ch->prev)->next = ch->next; |
| 206 | } else { |
| 207 | clist = ch->next; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 208 | } |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 209 | if (ch->next != NULL) |
| 210 | (ch->next)->prev = ch->prev; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 211 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 212 | free(ch->ifname); |
| 213 | free(ch->mac); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 214 | free(ch); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 215 | #endif |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |