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