xref: /netbsd-src/usr.sbin/rbootd/bpf.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: bpf.c,v 1.10 2000/04/13 08:52:44 itojun Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1992 The University of Utah and the Center
5  *	for Software Science (CSS).
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Center for Software Science of the University of Utah Computer
11  * Science Department.  CSS requests users of this software to return
12  * to css-dist@cs.utah.edu any improvements that they make and grant
13  * CSS redistribution rights.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *	This product includes software developed by the University of
26  *	California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  *	from: @(#)bpf.c	8.1 (Berkeley) 6/4/93
44  *
45  * From: Utah Hdr: bpf.c 3.1 92/07/06
46  * Author: Jeff Forys, University of Utah CSS
47  */
48 
49 #include <sys/cdefs.h>
50 #ifndef lint
51 #if 0
52 static char sccsid[] = "@(#)bpf.c	8.1 (Berkeley) 6/4/93";
53 #else
54 __RCSID("$NetBSD: bpf.c,v 1.10 2000/04/13 08:52:44 itojun Exp $");
55 #endif
56 #endif /* not lint */
57 
58 #include <sys/param.h>
59 #include <sys/ioctl.h>
60 #include <sys/socket.h>
61 
62 #include <net/if.h>
63 #include <net/bpf.h>
64 
65 #include <ctype.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <syslog.h>
72 #include <unistd.h>
73 #ifdef HAVE_IFADDRS_H
74 #include <ifaddrs.h>
75 #endif
76 #include "defs.h"
77 #include "pathnames.h"
78 
79 static int BpfFd = -1;
80 static unsigned BpfLen = 0;
81 static u_int8_t *BpfPkt = NULL;
82 
83 /*
84 **  BpfOpen -- Open and initialize a BPF device.
85 **
86 **	Parameters:
87 **		None.
88 **
89 **	Returns:
90 **		File descriptor of opened BPF device (for select() etc).
91 **
92 **	Side Effects:
93 **		If an error is encountered, the program terminates here.
94 */
95 int
96 BpfOpen()
97 {
98 	struct ifreq ifr;
99 	char bpfdev[32];
100 	int n = 0;
101 
102 	/*
103 	 *  Open the first available BPF device.
104 	 */
105 	do {
106 		(void) sprintf(bpfdev, _PATH_BPF, n++);
107 		BpfFd = open(bpfdev, O_RDWR);
108 	} while (BpfFd < 0 && (errno == EBUSY || errno == EPERM));
109 
110 	if (BpfFd < 0) {
111 		syslog(LOG_ERR, "bpf: no available devices: %m");
112 		Exit(0);
113 	}
114 
115 	/*
116 	 *  Set interface name for bpf device, get data link layer
117 	 *  type and make sure it's type Ethernet.
118 	 */
119 	(void) strncpy(ifr.ifr_name, IntfName, sizeof(ifr.ifr_name));
120 	if (ioctl(BpfFd, BIOCSETIF, (caddr_t)&ifr) < 0) {
121 		syslog(LOG_ERR, "bpf: ioctl(BIOCSETIF,%s): %m", IntfName);
122 		Exit(0);
123 	}
124 
125 	/*
126 	 *  Make sure we are dealing with an Ethernet device.
127 	 */
128 	if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) {
129 		syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m");
130 		Exit(0);
131 	}
132 	if (n != DLT_EN10MB) {
133 		syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported",
134 		       IntfName, n);
135 		Exit(0);
136 	}
137 
138 	/*
139 	 *  On read(), return packets immediately (do not buffer them).
140 	 */
141 	n = 1;
142 	if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) {
143 		syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m");
144 		Exit(0);
145 	}
146 
147 	/*
148 	 *  Try to enable the chip/driver's multicast address filter to
149 	 *  grab our RMP address.  If this fails, try promiscuous mode.
150 	 *  If this fails, there's no way we are going to get any RMP
151 	 *  packets so just exit here.
152 	 */
153 #ifdef MSG_EOR
154 	ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
155 #endif
156 	ifr.ifr_addr.sa_family = AF_UNSPEC;
157 	memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0],
158 	    RMP_ADDRLEN);
159 	if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) {
160 		syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m");
161 		Exit(0);
162 	}
163 
164 	/*
165 	 *  Ask BPF how much buffer space it requires and allocate one.
166 	 */
167 	if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) {
168 		syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m");
169 		Exit(0);
170 	}
171 	if (BpfPkt == NULL)
172 		BpfPkt = (u_int8_t *)malloc(BpfLen);
173 
174 	if (BpfPkt == NULL) {
175 		syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)",
176 		       BpfLen);
177 		Exit(0);
178 	}
179 
180 	/*
181 	 *  Write a little program to snarf RMP Boot packets and stuff
182 	 *  it down BPF's throat (i.e. set up the packet filter).
183 	 */
184 	{
185 #define	RMP	((struct rmp_packet *)0)
186 		static struct bpf_insn bpf_insn[] = {
187 		    { BPF_LD|BPF_B|BPF_ABS,  0, 0, (long)&RMP->hp_llc.dsap },
188 		    { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP },
189 		    { BPF_LD|BPF_H|BPF_ABS,  0, 0, (long)&RMP->hp_llc.cntrl },
190 		    { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP },
191 		    { BPF_LD|BPF_H|BPF_ABS,  0, 0, (long)&RMP->hp_llc.dxsap },
192 		    { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP },
193 		    { BPF_RET|BPF_K,         0, 0, RMP_MAX_PACKET },
194 		    { BPF_RET|BPF_K,         0, 0, 0x0 }
195 		};
196 #undef	RMP
197 		static struct bpf_program bpf_pgm = {
198 		    sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn
199 		};
200 
201 		if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) {
202 			syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m");
203 			Exit(0);
204 		}
205 	}
206 
207 	return(BpfFd);
208 }
209 
210 /*
211 **  BPF GetIntfName -- Return the name of a network interface attached to
212 **		the system, or 0 if none can be found.  The interface
213 **		must be configured up; the lowest unit number is
214 **		preferred; loopback is ignored.
215 **
216 **	Parameters:
217 **		errmsg - if no network interface found, *errmsg explains why.
218 **
219 **	Returns:
220 **		A (static) pointer to interface name, or NULL on error.
221 **
222 **	Side Effects:
223 **		None.
224 */
225 char *
226 BpfGetIntfName(errmsg)
227 	char **errmsg;
228 {
229 #ifdef HAVE_IFADDRS_H
230 	struct ifaddrs *ifap, *ifa, *p;
231 	int minunit, n;
232 	char *cp;
233 	static char device[IFNAMSIZ + 1];
234 	static char errbuf[128] = "No Error!";
235 
236 	if (getifaddrs(&ifap) != 0) {
237 		(void) strcpy(errbuf, "bpf: getifaddrs: %m");
238 		return(NULL);
239 	}
240 
241 	p = NULL;
242 	minunit = 666;
243 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
244 		/*
245 		 *  If interface is down or this is the loopback interface,
246 		 *  ignore it.
247 		 */
248 		if ((ifa->ifa_flags & IFF_UP) == 0 ||
249 #ifdef IFF_LOOPBACK
250 		    (ifa->ifa_flags & IFF_LOOPBACK))
251 #else
252 		    (strcmp(ifa->ifa_name, "lo0") == 0))
253 #endif
254 			continue;
255 
256 		for (cp = ifa->ifa_name; !isdigit(*cp); ++cp)
257 			;
258 		n = atoi(cp);
259 		if (n < minunit) {
260 			minunit = n;
261 			p = ifa;
262 		}
263 	}
264 	if (p == NULL) {
265 		(void) strcpy(errbuf, "bpf: no interfaces found");
266 		freeifaddrs(ifap);
267 		return(NULL);
268 	}
269 
270 	(void) strncpy(device, p->ifa_name, sizeof(device));
271 	device[sizeof(device) - 1] = '\0';
272 	freeifaddrs(ifap);
273 	return(device);
274 #else
275 	struct ifreq ibuf[8], *ifrp, *ifend, *mp;
276 	struct ifconf ifc;
277 	int fd;
278 	int minunit, n;
279 	char *cp;
280 	static char device[sizeof(ifrp->ifr_name)];
281 	static char errbuf[128] = "No Error!";
282 
283 	if (errmsg != NULL)
284 		*errmsg = errbuf;
285 
286 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
287 		(void) strcpy(errbuf, "bpf: socket: %m");
288 		return(NULL);
289 	}
290 	ifc.ifc_len = sizeof ibuf;
291 	ifc.ifc_buf = (caddr_t)ibuf;
292 
293 #ifdef OSIOCGIFCONF
294 	if (ioctl(fd, OSIOCGIFCONF, (char *)&ifc) < 0 ||
295 	    ifc.ifc_len < sizeof(struct ifreq)) {
296 		(void) strcpy(errbuf, "bpf: ioctl(OSIOCGIFCONF): %m");
297 		return(NULL);
298 	}
299 #else
300 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
301 	    ifc.ifc_len < sizeof(struct ifreq)) {
302 		(void) strcpy(errbuf, "bpf: ioctl(SIOCGIFCONF): %m");
303 		return(NULL);
304 	}
305 #endif
306 	ifrp = ibuf;
307 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
308 
309 	mp = 0;
310 	minunit = 666;
311 	for (; ifrp < ifend; ++ifrp) {
312 		if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
313 			(void) strcpy(errbuf, "bpf: ioctl(SIOCGIFFLAGS): %m");
314 			return(NULL);
315 		}
316 
317 		/*
318 		 *  If interface is down or this is the loopback interface,
319 		 *  ignore it.
320 		 */
321 		if ((ifrp->ifr_flags & IFF_UP) == 0 ||
322 #ifdef IFF_LOOPBACK
323 		    (ifrp->ifr_flags & IFF_LOOPBACK))
324 #else
325 		    (strcmp(ifrp->ifr_name, "lo0") == 0))
326 #endif
327 			continue;
328 
329 		for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp)
330 			;
331 		n = atoi(cp);
332 		if (n < minunit) {
333 			minunit = n;
334 			mp = ifrp;
335 		}
336 	}
337 
338 	(void) close(fd);
339 	if (mp == 0) {
340 		(void) strcpy(errbuf, "bpf: no interfaces found");
341 		return(NULL);
342 	}
343 
344 	(void) strcpy(device, mp->ifr_name);
345 	return(device);
346 #endif
347 }
348 
349 /*
350 **  BpfRead -- Read packets from a BPF device and fill in `rconn'.
351 **
352 **	Parameters:
353 **		rconn - filled in with next packet.
354 **		doread - is True if we can issue a read() syscall.
355 **
356 **	Returns:
357 **		True if `rconn' contains a new packet, False otherwise.
358 **
359 **	Side Effects:
360 **		None.
361 */
362 int
363 BpfRead(rconn, doread)
364 	RMPCONN *rconn;
365 	int doread;
366 {
367 	int datlen, caplen, hdrlen;
368 	static u_int8_t *bp = NULL, *ep = NULL;
369 	int cc;
370 
371 	/*
372 	 *  The read() may block, or it may return one or more packets.
373 	 *  We let the caller decide whether or not we can issue a read().
374 	 */
375 	if (doread) {
376 		if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) {
377 			syslog(LOG_ERR, "bpf: read: %m");
378 			return(0);
379 		} else {
380 			bp = BpfPkt;
381 			ep = BpfPkt + cc;
382 		}
383 	}
384 
385 #define bhp ((struct bpf_hdr *)bp)
386 	/*
387 	 *  If there is a new packet in the buffer, stuff it into `rconn'
388 	 *  and return a success indication.
389 	 */
390 	if (bp < ep) {
391 		datlen = bhp->bh_datalen;
392 		caplen = bhp->bh_caplen;
393 		hdrlen = bhp->bh_hdrlen;
394 
395 		if (caplen != datlen)
396 			syslog(LOG_ERR,
397 			       "bpf: short packet dropped (%d of %d bytes)",
398 			       caplen, datlen);
399 		else if (caplen > sizeof(struct rmp_packet))
400 			syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)",
401 			       caplen);
402 		else {
403 			rconn->rmplen = caplen;
404 			memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp,
405 			  sizeof(struct timeval));
406 			memmove((char *)&rconn->rmp, (char *)bp + hdrlen,
407 			    caplen);
408 		}
409 		bp += BPF_WORDALIGN(caplen + hdrlen);
410 		return(1);
411 	}
412 #undef bhp
413 
414 	return(0);
415 }
416 
417 /*
418 **  BpfWrite -- Write packet to BPF device.
419 **
420 **	Parameters:
421 **		rconn - packet to send.
422 **
423 **	Returns:
424 **		True if write succeeded, False otherwise.
425 **
426 **	Side Effects:
427 **		None.
428 */
429 int
430 BpfWrite(rconn)
431 	RMPCONN *rconn;
432 {
433 	if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) {
434 		syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn));
435 		return(0);
436 	}
437 
438 	return(1);
439 }
440 
441 /*
442 **  BpfClose -- Close a BPF device.
443 **
444 **	Parameters:
445 **		None.
446 **
447 **	Returns:
448 **		Nothing.
449 **
450 **	Side Effects:
451 **		None.
452 */
453 void
454 BpfClose()
455 {
456 	struct ifreq ifr;
457 
458 	if (BpfPkt != NULL) {
459 		free((char *)BpfPkt);
460 		BpfPkt = NULL;
461 	}
462 
463 	if (BpfFd == -1)
464 		return;
465 
466 #ifdef MSG_EOR
467 	ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
468 #endif
469 	ifr.ifr_addr.sa_family = AF_UNSPEC;
470 	memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0],
471 	    RMP_ADDRLEN);
472 	if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0)
473 		(void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0);
474 
475 	(void) close(BpfFd);
476 	BpfFd = -1;
477 }
478