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