Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/networking/nameif.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 3877 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 /* vi: set sw=4 ts=4: */
2 /*
3 * nameif.c - Naming Interfaces based on MAC address for busybox.
4 *
5 * Written 2000 by Andi Kleen.
6 * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
7 * Glenn McGrath <bug1@iinet.net.au>
8 *
9 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
10 */
11
12 #include "busybox.h"
13 #include <syslog.h>
14 #include <net/if.h>
15 #include <netinet/ether.h>
16
17
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
27 /* take from linux/sockios.h */
28 #define SIOCSIFNAME 0x8923 /* set interface name */
29
30 /* Octets in one Ethernet addr, from <linux/if_ether.h> */
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;
39 struct mactable_s *prev;
40 char *ifname;
41 struct ether_addr *mac;
42 } mactable_t;
43
44 /* Check ascii str_macaddr, convert and copy to *mac */
45 static struct ether_addr *cc_macaddr(const char *str_macaddr)
46 {
47 struct ether_addr *lmac, *mac;
48
49 lmac = ether_aton(str_macaddr);
50 if (lmac == NULL)
51 bb_error_msg_and_die("cannot parse MAC %s", str_macaddr);
52 mac = xmalloc(ETH_ALEN);
53 memcpy(mac, lmac, ETH_ALEN);
54
55 return mac;
56 }
57
58 int nameif_main(int argc, char **argv)
59 {
60 mactable_t *clist = NULL;
61 FILE *ifh;
62 const char *fname = "/etc/mactab";
63 char *line;
64 int ctl_sk;
65 int if_index = 1;
66 mactable_t *ch;
67
68 if (1 & getopt32(argc, argv, "sc:", &fname)) {
69 openlog(applet_name, 0, LOG_LOCAL0);
70 logmode = LOGMODE_SYSLOG;
71 }
72
73 if ((argc - optind) & 1)
74 bb_show_usage();
75
76 if (optind < argc) {
77 char **a = argv + optind;
78
79 while (*a) {
80 if (strlen(*a) > IF_NAMESIZE)
81 bb_error_msg_and_die("interface name '%s' "
82 "too long", *a);
83 ch = xzalloc(sizeof(mactable_t));
84 ch->ifname = xstrdup(*a++);
85 ch->mac = cc_macaddr(*a++);
86 if (clist)
87 clist->prev = ch;
88 ch->next = clist;
89 clist = ch;
90 }
91 } else {
92 ifh = xfopen(fname, "r");
93
94 while ((line = xmalloc_fgets(ifh)) != NULL) {
95 char *line_ptr;
96 size_t name_length;
97
98 line_ptr = line + strspn(line, " \t");
99 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) {
100 free(line);
101 continue;
102 }
103 name_length = strcspn(line_ptr, " \t");
104 ch = xzalloc(sizeof(mactable_t));
105 ch->ifname = xstrndup(line_ptr, name_length);
106 if (name_length > IF_NAMESIZE)
107 bb_error_msg_and_die("interface name '%s' "
108 "too long", ch->ifname);
109 line_ptr += name_length;
110 line_ptr += strspn(line_ptr, " \t");
111 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
112 line_ptr[name_length] = '\0';
113 ch->mac = cc_macaddr(line_ptr);
114 if (clist)
115 clist->prev = ch;
116 ch->next = clist;
117 clist = ch;
118 free(line);
119 }
120 fclose(ifh);
121 }
122
123 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
124
125 while (clist) {
126 struct ifreq ifr;
127
128 memset(&ifr, 0, sizeof(struct ifreq));
129 if_index++;
130 ifr.ifr_ifindex = if_index;
131
132 /* Get ifname by index or die */
133 if (ioctl(ctl_sk, SIOCGIFNAME, &ifr))
134 break;
135
136 /* Has this device hwaddr? */
137 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr))
138 continue;
139
140 /* Search for mac like in ifr.ifr_hwaddr.sa_data */
141 for (ch = clist; ch; ch = ch->next)
142 if (!memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN))
143 break;
144
145 /* Nothing found for current ifr.ifr_hwaddr.sa_data */
146 if (ch == NULL)
147 continue;
148
149 strcpy(ifr.ifr_newname, ch->ifname);
150 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
151 bb_perror_msg_and_die("cannot change ifname %s to %s",
152 ifr.ifr_name, ch->ifname);
153
154 /* Remove list entry of renamed interface */
155 if (ch->prev != NULL) {
156 (ch->prev)->next = ch->next;
157 } else {
158 clist = ch->next;
159 }
160 if (ch->next != NULL)
161 (ch->next)->prev = ch->prev;
162 if (ENABLE_FEATURE_CLEAN_UP) {
163 free(ch->ifname);
164 free(ch->mac);
165 free(ch);
166 }
167 }
168
169 return 0;
170 }