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