xref: /netbsd/usr.sbin/rbootd/bpf.c (revision d871ec05)
1 /*	$NetBSD: bpf.c,v 1.4 1995/09/12 07:13:06 thorpej 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 #ifndef lint
50 /*static char sccsid[] = "@(#)bpf.c	8.1 (Berkeley) 6/4/93";*/
51 static char rcsid[] = "$NetBSD: bpf.c,v 1.4 1995/09/12 07:13:06 thorpej Exp $";
52 #endif /* not lint */
53 
54 #include <sys/param.h>
55 #include <sys/ioctl.h>
56 #include <sys/socket.h>
57 
58 #include <net/if.h>
59 #include <net/bpf.h>
60 
61 #include <ctype.h>
62 #include <errno.h>
63 #include <fcntl.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <syslog.h>
68 #include <unistd.h>
69 #include "defs.h"
70 #include "pathnames.h"
71 
72 static int BpfFd = -1;
73 static unsigned BpfLen = 0;
74 static u_char *BpfPkt = NULL;
75 
76 /*
77 **  BpfOpen -- Open and initialize a BPF device.
78 **
79 **	Parameters:
80 **		None.
81 **
82 **	Returns:
83 **		File descriptor of opened BPF device (for select() etc).
84 **
85 **	Side Effects:
86 **		If an error is encountered, the program terminates here.
87 */
88 int
89 BpfOpen()
90 {
91 	struct ifreq ifr;
92 	char bpfdev[32];
93 	int n = 0;
94 
95 	/*
96 	 *  Open the first available BPF device.
97 	 */
98 	do {
99 		(void) sprintf(bpfdev, _PATH_BPF, n++);
100 		BpfFd = open(bpfdev, O_RDWR);
101 	} while (BpfFd < 0 && (errno == EBUSY || errno == EPERM));
102 
103 	if (BpfFd < 0) {
104 		syslog(LOG_ERR, "bpf: no available devices: %m");
105 		Exit(0);
106 	}
107 
108 	/*
109 	 *  Set interface name for bpf device, get data link layer
110 	 *  type and make sure it's type Ethernet.
111 	 */
112 	(void) strncpy(ifr.ifr_name, IntfName, sizeof(ifr.ifr_name));
113 	if (ioctl(BpfFd, BIOCSETIF, (caddr_t)&ifr) < 0) {
114 		syslog(LOG_ERR, "bpf: ioctl(BIOCSETIF,%s): %m", IntfName);
115 		Exit(0);
116 	}
117 
118 	/*
119 	 *  Make sure we are dealing with an Ethernet device.
120 	 */
121 	if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) {
122 		syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m");
123 		Exit(0);
124 	}
125 	if (n != DLT_EN10MB) {
126 		syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported",
127 		       IntfName, n);
128 		Exit(0);
129 	}
130 
131 	/*
132 	 *  On read(), return packets immediately (do not buffer them).
133 	 */
134 	n = 1;
135 	if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) {
136 		syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m");
137 		Exit(0);
138 	}
139 
140 	/*
141 	 *  Try to enable the chip/driver's multicast address filter to
142 	 *  grab our RMP address.  If this fails, try promiscuous mode.
143 	 *  If this fails, there's no way we are going to get any RMP
144 	 *  packets so just exit here.
145 	 */
146 #ifdef MSG_EOR
147 	ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
148 #endif
149 	ifr.ifr_addr.sa_family = AF_UNSPEC;
150 	bcopy(&RmpMcastAddr[0], (char *)&ifr.ifr_addr.sa_data[0], RMP_ADDRLEN);
151 	if (ioctl(BpfFd, SIOCADDMULTI, (caddr_t)&ifr) < 0) {
152 		syslog(LOG_WARNING,
153 		    "bpf: can't add mcast addr (%m), setting promiscuous mode");
154 
155 		if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) {
156 			syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m");
157 			Exit(0);
158 		}
159 	}
160 
161 	/*
162 	 *  Ask BPF how much buffer space it requires and allocate one.
163 	 */
164 	if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) {
165 		syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m");
166 		Exit(0);
167 	}
168 	if (BpfPkt == NULL)
169 		BpfPkt = (u_char *)malloc(BpfLen);
170 
171 	if (BpfPkt == NULL) {
172 		syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)",
173 		       BpfLen);
174 		Exit(0);
175 	}
176 
177 	/*
178 	 *  Write a little program to snarf RMP Boot packets and stuff
179 	 *  it down BPF's throat (i.e. set up the packet filter).
180 	 */
181 	{
182 #define	RMP	((struct rmp_packet *)0)
183 		static struct bpf_insn bpf_insn[] = {
184 		    { BPF_LD|BPF_B|BPF_ABS,  0, 0, (long)&RMP->hp_llc.dsap },
185 		    { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP },
186 		    { BPF_LD|BPF_H|BPF_ABS,  0, 0, (long)&RMP->hp_llc.cntrl },
187 		    { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP },
188 		    { BPF_LD|BPF_H|BPF_ABS,  0, 0, (long)&RMP->hp_llc.dxsap },
189 		    { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP },
190 		    { BPF_RET|BPF_K,         0, 0, RMP_MAX_PACKET },
191 		    { BPF_RET|BPF_K,         0, 0, 0x0 }
192 		};
193 #undef	RMP
194 		static struct bpf_program bpf_pgm = {
195 		    sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn
196 		};
197 
198 		if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) {
199 			syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m");
200 			Exit(0);
201 		}
202 	}
203 
204 	return(BpfFd);
205 }
206 
207 /*
208 **  BPF GetIntfName -- Return the name of a network interface attached to
209 **		the system, or 0 if none can be found.  The interface
210 **		must be configured up; the lowest unit number is
211 **		preferred; loopback is ignored.
212 **
213 **	Parameters:
214 **		errmsg - if no network interface found, *errmsg explains why.
215 **
216 **	Returns:
217 **		A (static) pointer to interface name, or NULL on error.
218 **
219 **	Side Effects:
220 **		None.
221 */
222 char *
223 BpfGetIntfName(errmsg)
224 	char **errmsg;
225 {
226 	struct ifreq ibuf[8], *ifrp, *ifend, *mp;
227 	struct ifconf ifc;
228 	int fd;
229 	int minunit, n;
230 	char *cp;
231 	static char device[sizeof(ifrp->ifr_name)];
232 	static char errbuf[128] = "No Error!";
233 
234 	if (errmsg != NULL)
235 		*errmsg = errbuf;
236 
237 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
238 		(void) strcpy(errbuf, "bpf: socket: %m");
239 		return(NULL);
240 	}
241 	ifc.ifc_len = sizeof ibuf;
242 	ifc.ifc_buf = (caddr_t)ibuf;
243 
244 #ifdef OSIOCGIFCONF
245 	if (ioctl(fd, OSIOCGIFCONF, (char *)&ifc) < 0 ||
246 	    ifc.ifc_len < sizeof(struct ifreq)) {
247 		(void) strcpy(errbuf, "bpf: ioctl(OSIOCGIFCONF): %m");
248 		return(NULL);
249 	}
250 #else
251 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
252 	    ifc.ifc_len < sizeof(struct ifreq)) {
253 		(void) strcpy(errbuf, "bpf: ioctl(SIOCGIFCONF): %m");
254 		return(NULL);
255 	}
256 #endif
257 	ifrp = ibuf;
258 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
259 
260 	mp = 0;
261 	minunit = 666;
262 	for (; ifrp < ifend; ++ifrp) {
263 		if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
264 			(void) strcpy(errbuf, "bpf: ioctl(SIOCGIFFLAGS): %m");
265 			return(NULL);
266 		}
267 
268 		/*
269 		 *  If interface is down or this is the loopback interface,
270 		 *  ignore it.
271 		 */
272 		if ((ifrp->ifr_flags & IFF_UP) == 0 ||
273 #ifdef IFF_LOOPBACK
274 		    (ifrp->ifr_flags & IFF_LOOPBACK))
275 #else
276 		    (strcmp(ifrp->ifr_name, "lo0") == 0))
277 #endif
278 			continue;
279 
280 		for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp)
281 			;
282 		n = atoi(cp);
283 		if (n < minunit) {
284 			minunit = n;
285 			mp = ifrp;
286 		}
287 	}
288 
289 	(void) close(fd);
290 	if (mp == 0) {
291 		(void) strcpy(errbuf, "bpf: no interfaces found");
292 		return(NULL);
293 	}
294 
295 	(void) strcpy(device, mp->ifr_name);
296 	return(device);
297 }
298 
299 /*
300 **  BpfRead -- Read packets from a BPF device and fill in `rconn'.
301 **
302 **	Parameters:
303 **		rconn - filled in with next packet.
304 **		doread - is True if we can issue a read() syscall.
305 **
306 **	Returns:
307 **		True if `rconn' contains a new packet, False otherwise.
308 **
309 **	Side Effects:
310 **		None.
311 */
312 int
313 BpfRead(rconn, doread)
314 	RMPCONN *rconn;
315 	int doread;
316 {
317 	register int datlen, caplen, hdrlen;
318 	static u_char *bp = NULL, *ep = NULL;
319 	int cc;
320 
321 	/*
322 	 *  The read() may block, or it may return one or more packets.
323 	 *  We let the caller decide whether or not we can issue a read().
324 	 */
325 	if (doread) {
326 		if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) {
327 			syslog(LOG_ERR, "bpf: read: %m");
328 			return(0);
329 		} else {
330 			bp = BpfPkt;
331 			ep = BpfPkt + cc;
332 		}
333 	}
334 
335 #define bhp ((struct bpf_hdr *)bp)
336 	/*
337 	 *  If there is a new packet in the buffer, stuff it into `rconn'
338 	 *  and return a success indication.
339 	 */
340 	if (bp < ep) {
341 		datlen = bhp->bh_datalen;
342 		caplen = bhp->bh_caplen;
343 		hdrlen = bhp->bh_hdrlen;
344 
345 		if (caplen != datlen)
346 			syslog(LOG_ERR,
347 			       "bpf: short packet dropped (%d of %d bytes)",
348 			       caplen, datlen);
349 		else if (caplen > sizeof(struct rmp_packet))
350 			syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)",
351 			       caplen);
352 		else {
353 			rconn->rmplen = htons(caplen);
354 			bcopy((char *)&bhp->bh_tstamp, (char *)&rconn->tstamp,
355 			      sizeof(struct timeval));
356 			bcopy((char *)bp + hdrlen, (char *)&rconn->rmp, caplen);
357 		}
358 		bp += BPF_WORDALIGN(caplen + hdrlen);
359 		return(1);
360 	}
361 #undef bhp
362 
363 	return(0);
364 }
365 
366 /*
367 **  BpfWrite -- Write packet to BPF device.
368 **
369 **	Parameters:
370 **		rconn - packet to send.
371 **
372 **	Returns:
373 **		True if write succeeded, False otherwise.
374 **
375 **	Side Effects:
376 **		None.
377 */
378 int
379 BpfWrite(rconn)
380 	RMPCONN *rconn;
381 {
382 	if (write(BpfFd, (char *)&rconn->rmp, ntohs(rconn->rmplen)) < 0) {
383 		syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn));
384 		return(0);
385 	}
386 
387 	return(1);
388 }
389 
390 /*
391 **  BpfClose -- Close a BPF device.
392 **
393 **	Parameters:
394 **		None.
395 **
396 **	Returns:
397 **		Nothing.
398 **
399 **	Side Effects:
400 **		None.
401 */
402 void
403 BpfClose()
404 {
405 	struct ifreq ifr;
406 
407 	if (BpfPkt != NULL) {
408 		free((char *)BpfPkt);
409 		BpfPkt = NULL;
410 	}
411 
412 	if (BpfFd == -1)
413 		return;
414 
415 #ifdef MSG_EOR
416 	ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
417 #endif
418 	ifr.ifr_addr.sa_family = AF_UNSPEC;
419 	bcopy(&RmpMcastAddr[0], (char *)&ifr.ifr_addr.sa_data[0], RMP_ADDRLEN);
420 	if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0)
421 		(void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0);
422 
423 	(void) close(BpfFd);
424 	BpfFd = -1;
425 }
426