"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 2 | /* |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 3 | * nameif.c - Naming Interfaces based on MAC address for busybox. |
| 4 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * Written 2000 by Andi Kleen. |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 6 | * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua> |
Denis Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 7 | * Glenn McGrath |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 8 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 9 | * 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] | 10 | */ |
| 11 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 13 | #include <syslog.h> |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 14 | #include <net/if.h> |
| 15 | #include <netinet/ether.h> |
| 16 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 17 | |
Eric Andersen | 40ea66c | 2003-07-05 08:00:17 +0000 | [diff] [blame] | 18 | /* 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 Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 27 | /* take from linux/sockios.h */ |
Glenn L McGrath | a9adef0 | 2003-01-19 13:34:21 +0000 | [diff] [blame] | 28 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 29 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 30 | /* Octets in one Ethernet addr, from <linux/if_ether.h> */ |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 31 | #define ETH_ALEN 6 |
| 32 | |
| 33 | #ifndef ifr_newname |
| 34 | #define ifr_newname ifr_ifru.ifru_slave |
| 35 | #endif |
| 36 | |
| 37 | typedef struct mactable_s { |
| 38 | struct mactable_s *next; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 39 | struct mactable_s *prev; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 40 | char *ifname; |
| 41 | struct ether_addr *mac; |
| 42 | } mactable_t; |
| 43 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 44 | /* Check ascii str_macaddr, convert and copy to *mac */ |
"Vladimir N. Oleynik" | 8c9daa1 | 2006-01-15 09:29:41 +0000 | [diff] [blame] | 45 | static struct ether_addr *cc_macaddr(const char *str_macaddr) |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 46 | { |
| 47 | struct ether_addr *lmac, *mac; |
| 48 | |
| 49 | lmac = ether_aton(str_macaddr); |
| 50 | if (lmac == NULL) |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 51 | bb_error_msg_and_die("cannot parse MAC %s", str_macaddr); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 52 | mac = xmalloc(ETH_ALEN); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 53 | memcpy(mac, lmac, ETH_ALEN); |
| 54 | |
| 55 | return mac; |
| 56 | } |
| 57 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 58 | int nameif_main(int argc, char **argv); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 59 | int nameif_main(int argc, char **argv) |
| 60 | { |
| 61 | mactable_t *clist = NULL; |
| 62 | FILE *ifh; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 63 | const char *fname = "/etc/mactab"; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 64 | char *line; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 65 | int ctl_sk; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 66 | int if_index = 1; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 67 | mactable_t *ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 69 | if (1 & getopt32(argv, "sc:", &fname)) { |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 70 | openlog(applet_name, 0, LOG_LOCAL0); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 71 | logmode = LOGMODE_SYSLOG; |
| 72 | } |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 73 | |
"Vladimir N. Oleynik" | 8c9daa1 | 2006-01-15 09:29:41 +0000 | [diff] [blame] | 74 | if ((argc - optind) & 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_show_usage(); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 76 | |
| 77 | if (optind < argc) { |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 78 | char **a = argv + optind; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 79 | |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 80 | while (*a) { |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 81 | if (strlen(*a) > IF_NAMESIZE) |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 82 | bb_error_msg_and_die("interface name '%s' " |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 83 | "too long", *a); |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 84 | ch = xzalloc(sizeof(mactable_t)); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 85 | ch->ifname = xstrdup(*a++); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 86 | ch->mac = cc_macaddr(*a++); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 87 | if (clist) |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 88 | clist->prev = ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 89 | ch->next = clist; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 90 | clist = ch; |
| 91 | } |
| 92 | } else { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 93 | ifh = xfopen(fname, "r"); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 95 | while ((line = xmalloc_fgets(ifh)) != NULL) { |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 96 | char *line_ptr; |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 97 | size_t name_length; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 98 | |
| 99 | line_ptr = line + strspn(line, " \t"); |
"Vladimir N. Oleynik" | 8c9daa1 | 2006-01-15 09:29:41 +0000 | [diff] [blame] | 100 | if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) { |
| 101 | free(line); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 102 | continue; |
"Vladimir N. Oleynik" | 8c9daa1 | 2006-01-15 09:29:41 +0000 | [diff] [blame] | 103 | } |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 104 | name_length = strcspn(line_ptr, " \t"); |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 105 | ch = xzalloc(sizeof(mactable_t)); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 106 | ch->ifname = xstrndup(line_ptr, name_length); |
Glenn L McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 107 | if (name_length > IF_NAMESIZE) |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 108 | bb_error_msg_and_die("interface name '%s' " |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 109 | "too long", ch->ifname); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 110 | 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 McGrath | 77c60e5 | 2003-01-16 11:37:57 +0000 | [diff] [blame] | 114 | ch->mac = cc_macaddr(line_ptr); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 115 | if (clist) |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 116 | clist->prev = ch; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 117 | ch->next = clist; |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 118 | clist = ch; |
| 119 | free(line); |
| 120 | } |
| 121 | fclose(ifh); |
| 122 | } |
| 123 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 124 | ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 125 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 126 | while (clist) { |
| 127 | struct ifreq ifr; |
| 128 | |
Rob Landley | 855f1e1 | 2006-01-15 02:20:06 +0000 | [diff] [blame] | 129 | memset(&ifr, 0, sizeof(struct ifreq)); |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 130 | 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 McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 139 | continue; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 140 | |
| 141 | /* Search for mac like in ifr.ifr_hwaddr.sa_data */ |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 142 | for (ch = clist; ch; ch = ch->next) |
| 143 | if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN)) |
| 144 | break; |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 145 | |
| 146 | /* Nothing found for current ifr.ifr_hwaddr.sa_data */ |
| 147 | if (ch == NULL) |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 148 | continue; |
Glenn L McGrath | 8b08bda | 2002-12-13 09:02:16 +0000 | [diff] [blame] | 149 | |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 150 | strcpy(ifr.ifr_newname, ch->ifname); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 151 | ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr, |
| 152 | "cannot change ifname %s to %s", |
| 153 | ifr.ifr_name, ch->ifname); |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 154 | |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 155 | /* 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 McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 160 | } |
Glenn L McGrath | 688cf01 | 2002-12-17 12:43:43 +0000 | [diff] [blame] | 161 | if (ch->next != NULL) |
| 162 | (ch->next)->prev = ch->prev; |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 163 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 164 | free(ch->ifname); |
| 165 | free(ch->mac); |
| 166 | free(ch); |
| 167 | } |
Glenn L McGrath | f03c933 | 2002-12-13 00:01:44 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |