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 | * |
| 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 | 40ea66c | 2003-07-05 08:00:17 +0000 | [diff] [blame] | 38 | /* Older versions of net/if.h do not appear to define IF_NAMESIZE. */ |
| 39 | #ifndef IF_NAMESIZE |
| 40 | # ifdef IFNAMSIZ |
| 41 | # define IF_NAMESIZE IFNAMSIZ |
| 42 | # else |
| 43 | # define IF_NAMESIZE 16 |
| 44 | # endif |
| 45 | #endif |
| 46 | |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 47 | /* take from linux/sockios.h */ |
Glenn L McGrath | a9adef0 | 2003-01-19 13:34:21 +0000 | [diff] [blame] | 48 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 49 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 50 | /* Octets in one Ethernet addr, from <linux/if_ether.h> */ |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 51 | #define ETH_ALEN 6 |
| 52 | |
| 53 | #ifndef ifr_newname |
| 54 | #define ifr_newname ifr_ifru.ifru_slave |
| 55 | #endif |
| 56 | |
| 57 | typedef struct mactable_s { |
| 58 | struct mactable_s *next; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 59 | struct mactable_s *prev; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 60 | char *ifname; |
| 61 | struct ether_addr *mac; |
| 62 | } mactable_t; |
| 63 | |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 64 | static unsigned char use_syslog; |
| 65 | |
| 66 | static void serror(const char *s, ...) __attribute__ ((noreturn)); |
| 67 | |
| 68 | static void serror(const char *s, ...) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 69 | { |
| 70 | va_list ap; |
| 71 | |
| 72 | va_start(ap, s); |
| 73 | |
| 74 | if (use_syslog) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | openlog(bb_applet_name, 0, LOG_LOCAL0); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 76 | vsyslog(LOG_ERR, s, ap); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 77 | closelog(); |
| 78 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | bb_verror_msg(s, ap); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 80 | putc('\n', stderr); |
| 81 | } |
| 82 | |
| 83 | va_end(ap); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 84 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 85 | exit(EXIT_FAILURE); |
| 86 | } |
| 87 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 88 | /* Check ascii str_macaddr, convert and copy to *mac */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 89 | static struct ether_addr *cc_macaddr(char *str_macaddr) |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 90 | { |
| 91 | struct ether_addr *lmac, *mac; |
| 92 | |
| 93 | lmac = ether_aton(str_macaddr); |
| 94 | if (lmac == NULL) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 95 | serror("cannot parse MAC %s", str_macaddr); |
| 96 | mac = xmalloc(ETH_ALEN); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 97 | memcpy(mac, lmac, ETH_ALEN); |
| 98 | |
| 99 | return mac; |
| 100 | } |
| 101 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 102 | int nameif_main(int argc, char **argv) |
| 103 | { |
| 104 | mactable_t *clist = NULL; |
| 105 | FILE *ifh; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 106 | const char *fname = "/etc/mactab"; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 107 | char *line; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 108 | int ctl_sk; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 109 | int opt; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 110 | int if_index = 1; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 111 | mactable_t *ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 112 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 113 | |
Glenn L McGrath | a9adef0 | 2003-01-19 13:34:21 +0000 | [diff] [blame] | 114 | while ((opt = getopt(argc, argv, "c:s")) != -1) { |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 115 | switch (opt) { |
| 116 | case 'c': |
| 117 | fname = optarg; |
| 118 | break; |
| 119 | case 's': |
| 120 | use_syslog = 1; |
| 121 | break; |
| 122 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | bb_show_usage(); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 127 | if ((argc - optind) & 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 128 | bb_show_usage(); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 129 | |
| 130 | if (optind < argc) { |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 131 | char **a = argv + optind; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 132 | |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 133 | while (*a) { |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 134 | |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 135 | if (strlen(*a) > IF_NAMESIZE) |
| 136 | serror("interface name `%s' too long", *a); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 137 | ch = xcalloc(1, sizeof(mactable_t)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 138 | ch->ifname = bb_xstrdup(*a++); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 139 | ch->mac = cc_macaddr(*a++); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 140 | if (clist) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 141 | clist->prev = ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 142 | ch->next = clist; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 143 | clist = ch; |
| 144 | } |
| 145 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 146 | ifh = bb_xfopen(fname, "r"); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 147 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 148 | while ((line = bb_get_line_from_file(ifh)) != NULL) { |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 149 | char *line_ptr; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 150 | size_t name_length; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 151 | |
| 152 | line_ptr = line + strspn(line, " \t"); |
| 153 | if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) |
| 154 | continue; |
| 155 | name_length = strcspn(line_ptr, " \t"); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 156 | ch = xcalloc(1, sizeof(mactable_t)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 157 | ch->ifname = bb_xstrndup(line_ptr, name_length); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 158 | if (name_length > IF_NAMESIZE) |
Glenn L McGrath | a9adef0 | 2003-01-19 13:34:21 +0000 | [diff] [blame] | 159 | serror("interface name `%s' too long", ch->ifname); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 160 | line_ptr += name_length; |
| 161 | line_ptr += strspn(line_ptr, " \t"); |
| 162 | name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:"); |
| 163 | line_ptr[name_length] = '\0'; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 164 | ch->mac = cc_macaddr(line_ptr); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 165 | if (clist) |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 166 | clist->prev = ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 167 | ch->next = clist; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 168 | clist = ch; |
| 169 | free(line); |
| 170 | } |
| 171 | fclose(ifh); |
| 172 | } |
| 173 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 174 | if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 175 | serror("socket: %m"); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 176 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 177 | while (clist) { |
| 178 | struct ifreq ifr; |
| 179 | |
| 180 | bzero(&ifr, sizeof(struct ifreq)); |
| 181 | if_index++; |
| 182 | ifr.ifr_ifindex = if_index; |
| 183 | |
| 184 | /* Get ifname by index or die */ |
| 185 | if (ioctl(ctl_sk, SIOCGIFNAME, &ifr)) |
| 186 | break; |
| 187 | |
| 188 | /* Has this device hwaddr? */ |
| 189 | if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr)) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 190 | continue; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 191 | |
| 192 | /* Search for mac like in ifr.ifr_hwaddr.sa_data */ |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 193 | for (ch = clist; ch; ch = ch->next) |
| 194 | if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN)) |
| 195 | break; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 196 | |
| 197 | /* Nothing found for current ifr.ifr_hwaddr.sa_data */ |
| 198 | if (ch == NULL) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 199 | continue; |
Glenn L McGrath | 8b08bda | 2002-12-13 09:02:16 +0000 | [diff] [blame] | 200 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 201 | strcpy(ifr.ifr_newname, ch->ifname); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 202 | if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 203 | serror("cannot change ifname %s to %s: %m", |
| 204 | ifr.ifr_name, ch->ifname); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 205 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 206 | /* Remove list entry of renamed interface */ |
| 207 | if (ch->prev != NULL) { |
| 208 | (ch->prev)->next = ch->next; |
| 209 | } else { |
| 210 | clist = ch->next; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 211 | } |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 212 | if (ch->next != NULL) |
| 213 | (ch->next)->prev = ch->prev; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 214 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 215 | free(ch->ifname); |
| 216 | free(ch->mac); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 217 | free(ch); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 218 | #endif |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |