device.c revision 1.7 1 1.7 itojun /* $NetBSD: device.c,v 1.7 2003/07/14 08:37:53 itojun Exp $ */
2 1.2 thorpej
3 1.1 cjs /*
4 1.1 cjs * Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
5 1.1 cjs *
6 1.1 cjs * Redistribution and use in source and binary forms, with or without
7 1.1 cjs * modification, are permitted provided that the following conditions
8 1.1 cjs * are met:
9 1.1 cjs * 1. Redistributions of source code must retain the above copyright
10 1.1 cjs * notice, this list of conditions and the following disclaimer.
11 1.1 cjs * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cjs * notice, this list of conditions and the following disclaimer in the
13 1.1 cjs * documentation and/or other materials provided with the distribution.
14 1.1 cjs * 3. All advertising materials mentioning features or use of this software
15 1.1 cjs * must display the following acknowledgement:
16 1.1 cjs * This product includes software developed by Mats O Jansson.
17 1.1 cjs * 4. The name of the author may not be used to endorse or promote products
18 1.1 cjs * derived from this software without specific prior written permission.
19 1.1 cjs *
20 1.1 cjs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 cjs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 cjs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 cjs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 cjs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 cjs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 cjs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 cjs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 cjs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 cjs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 cjs */
31 1.1 cjs
32 1.3 lukem #include <sys/cdefs.h>
33 1.3 lukem #ifndef lint
34 1.7 itojun __RCSID("$NetBSD: device.c,v 1.7 2003/07/14 08:37:53 itojun Exp $");
35 1.1 cjs #endif
36 1.1 cjs
37 1.1 cjs #include "os.h"
38 1.3 lukem #include "common.h"
39 1.3 lukem #include "device.h"
40 1.3 lukem #include "mopdef.h"
41 1.3 lukem #include "pf.h"
42 1.6 christos #include "log.h"
43 1.1 cjs
44 1.1 cjs struct if_info *iflist; /* Interface List */
45 1.1 cjs
46 1.3 lukem void deviceOpen __P((char *, u_short, int));
47 1.1 cjs
48 1.1 cjs #ifdef DEV_NEW_CONF
49 1.1 cjs /*
50 1.1 cjs * Return ethernet adress for interface
51 1.1 cjs */
52 1.1 cjs
53 1.1 cjs void
54 1.1 cjs deviceEthAddr(ifname, eaddr)
55 1.1 cjs char *ifname;
56 1.1 cjs u_char *eaddr;
57 1.1 cjs {
58 1.1 cjs char inbuf[8192];
59 1.1 cjs struct ifconf ifc;
60 1.1 cjs struct ifreq *ifr;
61 1.1 cjs struct sockaddr_dl *sdl;
62 1.1 cjs int fd;
63 1.1 cjs int i, len;
64 1.1 cjs
65 1.1 cjs /* We cannot use SIOCGIFADDR on the BPF descriptor.
66 1.1 cjs We must instead get all the interfaces with SIOCGIFCONF
67 1.1 cjs and find the right one. */
68 1.1 cjs
69 1.1 cjs /* Use datagram socket to get Ethernet address. */
70 1.6 christos if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
71 1.6 christos mopLogErr("deviceEthAddr: socket");
72 1.1 cjs
73 1.1 cjs ifc.ifc_len = sizeof(inbuf);
74 1.1 cjs ifc.ifc_buf = inbuf;
75 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
76 1.6 christos ifc.ifc_len < sizeof(struct ifreq))
77 1.6 christos mopLogErr("deviceEthAddr: SIOGIFCONF");
78 1.1 cjs ifr = ifc.ifc_req;
79 1.1 cjs for (i = 0; i < ifc.ifc_len;
80 1.1 cjs i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
81 1.1 cjs len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
82 1.1 cjs sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
83 1.1 cjs if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
84 1.1 cjs sdl->sdl_alen != 6)
85 1.1 cjs continue;
86 1.1 cjs if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) {
87 1.3 lukem memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
88 1.1 cjs return;
89 1.1 cjs }
90 1.1 cjs }
91 1.1 cjs
92 1.6 christos mopLogErrX("deviceEthAddr: Never saw interface `%s'!", ifname);
93 1.1 cjs }
94 1.1 cjs #endif /* DEV_NEW_CONF */
95 1.1 cjs
96 1.1 cjs void
97 1.1 cjs deviceOpen(ifname, proto, trans)
98 1.1 cjs char *ifname;
99 1.1 cjs u_short proto;
100 1.1 cjs int trans;
101 1.1 cjs {
102 1.1 cjs struct if_info *p, tmp;
103 1.1 cjs
104 1.5 itojun strlcpy(tmp.if_name, ifname, sizeof(tmp.if_name));
105 1.1 cjs tmp.iopen = pfInit;
106 1.1 cjs
107 1.1 cjs switch (proto) {
108 1.1 cjs case MOP_K_PROTO_RC:
109 1.1 cjs tmp.read = mopReadRC;
110 1.1 cjs tmp.fd = mopOpenRC(&tmp, trans);
111 1.1 cjs break;
112 1.1 cjs case MOP_K_PROTO_DL:
113 1.1 cjs tmp.read = mopReadDL;
114 1.1 cjs tmp.fd = mopOpenDL(&tmp, trans);
115 1.1 cjs break;
116 1.1 cjs default:
117 1.1 cjs break;
118 1.1 cjs }
119 1.1 cjs
120 1.1 cjs if (tmp.fd != -1) {
121 1.1 cjs p = (struct if_info *)malloc(sizeof(*p));
122 1.6 christos if (p == 0)
123 1.6 christos mopLogErr("deviceOpen: malloc");
124 1.1 cjs
125 1.1 cjs p->next = iflist;
126 1.1 cjs iflist = p;
127 1.1 cjs
128 1.7 itojun strlcpy(p->if_name, tmp.if_name, sizeof(p->if_name));
129 1.1 cjs p->iopen = tmp.iopen;
130 1.1 cjs p->write = pfWrite;
131 1.1 cjs p->read = tmp.read;
132 1.3 lukem memset((char *)p->eaddr, 0, sizeof(p->eaddr));
133 1.1 cjs p->fd = tmp.fd;
134 1.1 cjs
135 1.1 cjs #ifdef DEV_NEW_CONF
136 1.1 cjs deviceEthAddr(p->if_name,&p->eaddr[0]);
137 1.1 cjs #else
138 1.1 cjs p->eaddr[0]= tmp.eaddr[0];
139 1.1 cjs p->eaddr[1]= tmp.eaddr[1];
140 1.1 cjs p->eaddr[2]= tmp.eaddr[2];
141 1.1 cjs p->eaddr[3]= tmp.eaddr[3];
142 1.1 cjs p->eaddr[4]= tmp.eaddr[4];
143 1.1 cjs p->eaddr[5]= tmp.eaddr[5];
144 1.1 cjs #endif /* DEV_NEW_CONF */
145 1.1 cjs
146 1.1 cjs }
147 1.1 cjs }
148 1.1 cjs
149 1.1 cjs void
150 1.1 cjs deviceInitOne(ifname)
151 1.1 cjs char *ifname;
152 1.1 cjs {
153 1.1 cjs char interface[IFNAME_SIZE];
154 1.1 cjs struct if_info *p;
155 1.1 cjs int trans;
156 1.1 cjs #ifdef _AIX
157 1.1 cjs char dev[IFNAME_SIZE];
158 1.1 cjs int unit,j;
159 1.1 cjs
160 1.1 cjs unit = 0;
161 1.1 cjs for (j = 0; j < strlen(ifname); j++) {
162 1.1 cjs if (isalpha(ifname[j])) {
163 1.1 cjs dev[j] = ifname[j];
164 1.1 cjs } else {
165 1.1 cjs if (isdigit(ifname[j])) {
166 1.1 cjs unit = unit*10 + ifname[j] - '0';
167 1.1 cjs dev[j] = '\0';
168 1.1 cjs }
169 1.1 cjs }
170 1.1 cjs }
171 1.1 cjs
172 1.1 cjs if ((strlen(dev) == 2) &&
173 1.1 cjs (dev[0] == 'e') &&
174 1.1 cjs ((dev[1] == 'n') || (dev[1] == 't'))) {
175 1.4 itojun snprintf(interface, sizeof(interface), "ent%d\0", unit);
176 1.1 cjs } else {
177 1.4 itojun snprintf(interface, sizeof(interface), "%s%d\0", dev, unit);
178 1.1 cjs }
179 1.1 cjs #else
180 1.4 itojun snprintf(interface, sizeof(interface), "%s", ifname);
181 1.1 cjs #endif /* _AIX */
182 1.1 cjs
183 1.1 cjs /* Ok, init it just once */
184 1.1 cjs
185 1.1 cjs p = iflist;
186 1.1 cjs for (p = iflist; p; p = p->next) {
187 1.1 cjs if (strcmp(p->if_name,interface) == 0) {
188 1.1 cjs return;
189 1.1 cjs }
190 1.1 cjs }
191 1.1 cjs
192 1.6 christos if (!mopInteractive)
193 1.6 christos syslog(LOG_INFO, "Initialized %s", interface);
194 1.1 cjs
195 1.1 cjs /* Ok, get transport information */
196 1.1 cjs
197 1.1 cjs trans = pfTrans(interface);
198 1.1 cjs
199 1.1 cjs #ifndef NORC
200 1.1 cjs /* Start with MOP Remote Console */
201 1.1 cjs
202 1.1 cjs switch (trans) {
203 1.1 cjs case TRANS_ETHER:
204 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER);
205 1.1 cjs break;
206 1.1 cjs case TRANS_8023:
207 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_8023);
208 1.1 cjs break;
209 1.1 cjs case TRANS_ETHER+TRANS_8023:
210 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER);
211 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_8023);
212 1.1 cjs break;
213 1.1 cjs case TRANS_ETHER+TRANS_8023+TRANS_AND:
214 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER+TRANS_8023);
215 1.1 cjs break;
216 1.1 cjs }
217 1.1 cjs #endif
218 1.1 cjs
219 1.1 cjs #ifndef NODL
220 1.1 cjs /* and next MOP Dump/Load */
221 1.1 cjs
222 1.1 cjs switch (trans) {
223 1.1 cjs case TRANS_ETHER:
224 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER);
225 1.1 cjs break;
226 1.1 cjs case TRANS_8023:
227 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_8023);
228 1.1 cjs break;
229 1.1 cjs case TRANS_ETHER+TRANS_8023:
230 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER);
231 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_8023);
232 1.1 cjs break;
233 1.1 cjs case TRANS_ETHER+TRANS_8023+TRANS_AND:
234 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER+TRANS_8023);
235 1.1 cjs break;
236 1.1 cjs }
237 1.1 cjs #endif
238 1.1 cjs
239 1.1 cjs }
240 1.1 cjs
241 1.1 cjs /*
242 1.1 cjs * Initialize all "candidate" interfaces that are in the system
243 1.1 cjs * configuration list. A "candidate" is up, not loopback and not
244 1.1 cjs * point to point.
245 1.1 cjs */
246 1.1 cjs void
247 1.1 cjs deviceInitAll()
248 1.1 cjs {
249 1.1 cjs #ifdef DEV_NEW_CONF
250 1.1 cjs char inbuf[8192];
251 1.1 cjs struct ifconf ifc;
252 1.1 cjs struct ifreq *ifr;
253 1.1 cjs struct sockaddr_dl *sdl;
254 1.1 cjs int fd;
255 1.1 cjs int i, len;
256 1.1 cjs
257 1.6 christos if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
258 1.6 christos mopLogErr("deviceInitAll: socket");
259 1.1 cjs
260 1.1 cjs ifc.ifc_len = sizeof(inbuf);
261 1.1 cjs ifc.ifc_buf = inbuf;
262 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
263 1.6 christos ifc.ifc_len < sizeof(struct ifreq))
264 1.6 christos mopLogErr("deviceInitAll: SIOCGIFCONF");
265 1.1 cjs ifr = ifc.ifc_req;
266 1.1 cjs for (i = 0; i < ifc.ifc_len;
267 1.1 cjs i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
268 1.1 cjs len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
269 1.1 cjs sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
270 1.1 cjs if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
271 1.1 cjs sdl->sdl_alen != 6)
272 1.1 cjs continue;
273 1.1 cjs if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
274 1.6 christos mopLogWarn("deviceInitAll: SIOCGIFFLAGS");
275 1.1 cjs continue;
276 1.1 cjs }
277 1.1 cjs if ((ifr->ifr_flags &
278 1.1 cjs (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
279 1.1 cjs continue;
280 1.1 cjs deviceInitOne(ifr->ifr_name);
281 1.1 cjs }
282 1.1 cjs (void) close(fd);
283 1.1 cjs #else
284 1.1 cjs int fd;
285 1.1 cjs int n;
286 1.1 cjs struct ifreq ibuf[8], *ifrp;
287 1.1 cjs struct ifconf ifc;
288 1.1 cjs
289 1.6 christos if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
290 1.6 christos mopLogErr("deviceInitAll: old socket");
291 1.1 cjs ifc.ifc_len = sizeof ibuf;
292 1.1 cjs ifc.ifc_buf = (caddr_t)ibuf;
293 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
294 1.6 christos ifc.ifc_len < sizeof(struct ifreq))
295 1.6 christos mopLogErr("deviceInitAll: old SIOCGIFCONF");
296 1.1 cjs ifrp = ibuf;
297 1.1 cjs n = ifc.ifc_len / sizeof(*ifrp);
298 1.1 cjs for (; --n >= 0; ++ifrp) {
299 1.1 cjs if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
300 1.1 cjs continue;
301 1.1 cjs }
302 1.1 cjs if (/*(ifrp->ifr_flags & IFF_UP) == 0 ||*/
303 1.1 cjs ifrp->ifr_flags & IFF_LOOPBACK ||
304 1.1 cjs ifrp->ifr_flags & IFF_POINTOPOINT)
305 1.1 cjs continue;
306 1.1 cjs deviceInitOne(ifrp->ifr_name);
307 1.1 cjs }
308 1.1 cjs
309 1.1 cjs (void) close(fd);
310 1.1 cjs #endif /* DEV_NEW_CONF */
311 1.1 cjs }
312