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