xref: /netbsd-src/usr.sbin/rbootd/bpf.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: bpf.c,v 1.12 2003/05/15 14:50:50 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.12 2003/05/15 14:50:50 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 #include <ifaddrs.h>
74 #include "defs.h"
75 #include "pathnames.h"
76 
77 static int BpfFd = -1;
78 static unsigned BpfLen = 0;
79 static u_int8_t *BpfPkt = NULL;
80 
81 /*
82 **  BpfOpen -- Open and initialize a BPF device.
83 **
84 **	Parameters:
85 **		None.
86 **
87 **	Returns:
88 **		File descriptor of opened BPF device (for select() etc).
89 **
90 **	Side Effects:
91 **		If an error is encountered, the program terminates here.
92 */
93 int
94 BpfOpen()
95 {
96 	struct ifreq ifr;
97 	char bpfdev[32];
98 	int n = 0;
99 
100 	/*
101 	 *  Open the first available BPF device.
102 	 */
103 	do {
104 		(void) sprintf(bpfdev, _PATH_BPF, n++);
105 		BpfFd = open(bpfdev, O_RDWR);
106 	} while (BpfFd < 0 && (errno == EBUSY || errno == EPERM));
107 
108 	if (BpfFd < 0) {
109 		syslog(LOG_ERR, "bpf: no available devices: %m");
110 		Exit(0);
111 	}
112 
113 	/*
114 	 *  Set interface name for bpf device, get data link layer
115 	 *  type and make sure it's type Ethernet.
116 	 */
117 	(void) strncpy(ifr.ifr_name, IntfName, sizeof(ifr.ifr_name));
118 	if (ioctl(BpfFd, BIOCSETIF, (caddr_t)&ifr) < 0) {
119 		syslog(LOG_ERR, "bpf: ioctl(BIOCSETIF,%s): %m", IntfName);
120 		Exit(0);
121 	}
122 
123 	/*
124 	 *  Make sure we are dealing with an Ethernet device.
125 	 */
126 	if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) {
127 		syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m");
128 		Exit(0);
129 	}
130 	if (n != DLT_EN10MB) {
131 		syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported",
132 		       IntfName, n);
133 		Exit(0);
134 	}
135 
136 	/*
137 	 *  On read(), return packets immediately (do not buffer them).
138 	 */
139 	n = 1;
140 	if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) {
141 		syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m");
142 		Exit(0);
143 	}
144 
145 	/*
146 	 *  Try to enable the chip/driver's multicast address filter to
147 	 *  grab our RMP address.  If this fails, try promiscuous mode.
148 	 *  If this fails, there's no way we are going to get any RMP
149 	 *  packets so just exit here.
150 	 */
151 #ifdef MSG_EOR
152 	ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
153 #endif
154 	ifr.ifr_addr.sa_family = AF_UNSPEC;
155 	memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0],
156 	    RMP_ADDRLEN);
157 	if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) {
158 		syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m");
159 		Exit(0);
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 ifaddrs *ifap, *ifa, *p;
228 	int minunit, n;
229 	char *cp;
230 	static char device[IFNAMSIZ + 1];
231 	static char errbuf[128] = "No Error!";
232 
233 	if (errmsg != NULL)
234 		*errmsg = errbuf;
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 }
275 
276 /*
277 **  BpfRead -- Read packets from a BPF device and fill in `rconn'.
278 **
279 **	Parameters:
280 **		rconn - filled in with next packet.
281 **		doread - is True if we can issue a read() syscall.
282 **
283 **	Returns:
284 **		True if `rconn' contains a new packet, False otherwise.
285 **
286 **	Side Effects:
287 **		None.
288 */
289 int
290 BpfRead(rconn, doread)
291 	RMPCONN *rconn;
292 	int doread;
293 {
294 	int datlen, caplen, hdrlen;
295 	static u_int8_t *bp = NULL, *ep = NULL;
296 	int cc;
297 
298 	/*
299 	 *  The read() may block, or it may return one or more packets.
300 	 *  We let the caller decide whether or not we can issue a read().
301 	 */
302 	if (doread) {
303 		if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) {
304 			syslog(LOG_ERR, "bpf: read: %m");
305 			return(0);
306 		} else {
307 			bp = BpfPkt;
308 			ep = BpfPkt + cc;
309 		}
310 	}
311 
312 #define bhp ((struct bpf_hdr *)bp)
313 	/*
314 	 *  If there is a new packet in the buffer, stuff it into `rconn'
315 	 *  and return a success indication.
316 	 */
317 	if (bp < ep) {
318 		datlen = bhp->bh_datalen;
319 		caplen = bhp->bh_caplen;
320 		hdrlen = bhp->bh_hdrlen;
321 
322 		if (caplen != datlen)
323 			syslog(LOG_ERR,
324 			       "bpf: short packet dropped (%d of %d bytes)",
325 			       caplen, datlen);
326 		else if (caplen > sizeof(struct rmp_packet))
327 			syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)",
328 			       caplen);
329 		else {
330 			rconn->rmplen = caplen;
331 			memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp,
332 			  sizeof(struct timeval));
333 			memmove((char *)&rconn->rmp, (char *)bp + hdrlen,
334 			    caplen);
335 		}
336 		bp += BPF_WORDALIGN(caplen + hdrlen);
337 		return(1);
338 	}
339 #undef bhp
340 
341 	return(0);
342 }
343 
344 /*
345 **  BpfWrite -- Write packet to BPF device.
346 **
347 **	Parameters:
348 **		rconn - packet to send.
349 **
350 **	Returns:
351 **		True if write succeeded, False otherwise.
352 **
353 **	Side Effects:
354 **		None.
355 */
356 int
357 BpfWrite(rconn)
358 	RMPCONN *rconn;
359 {
360 	if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) {
361 		syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn));
362 		return(0);
363 	}
364 
365 	return(1);
366 }
367 
368 /*
369 **  BpfClose -- Close a BPF device.
370 **
371 **	Parameters:
372 **		None.
373 **
374 **	Returns:
375 **		Nothing.
376 **
377 **	Side Effects:
378 **		None.
379 */
380 void
381 BpfClose()
382 {
383 	struct ifreq ifr;
384 
385 	if (BpfPkt != NULL) {
386 		free((char *)BpfPkt);
387 		BpfPkt = NULL;
388 	}
389 
390 	if (BpfFd == -1)
391 		return;
392 
393 #ifdef MSG_EOR
394 	ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
395 #endif
396 	ifr.ifr_addr.sa_family = AF_UNSPEC;
397 	memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0],
398 	    RMP_ADDRLEN);
399 	if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0)
400 		(void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0);
401 
402 	(void) close(BpfFd);
403 	BpfFd = -1;
404 }
405