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