Home | History | Annotate | Line # | Download | only in rbootd
bpf.c revision 1.13
      1 /*	$NetBSD: bpf.c,v 1.13 2003/07/13 12:18:55 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 (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.13 2003/07/13 12:18:55 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) snprintf(bpfdev, sizeof(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) strlcpy(errbuf, "bpf: getifaddrs: %m", sizeof(errbuf));
    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) strlcpy(errbuf, "bpf: no interfaces found",
    266 		    sizeof(errbuf));
    267 		freeifaddrs(ifap);
    268 		return(NULL);
    269 	}
    270 
    271 	(void) strncpy(device, p->ifa_name, sizeof(device));
    272 	device[sizeof(device) - 1] = '\0';
    273 	freeifaddrs(ifap);
    274 	return(device);
    275 }
    276 
    277 /*
    278 **  BpfRead -- Read packets from a BPF device and fill in `rconn'.
    279 **
    280 **	Parameters:
    281 **		rconn - filled in with next packet.
    282 **		doread - is True if we can issue a read() syscall.
    283 **
    284 **	Returns:
    285 **		True if `rconn' contains a new packet, False otherwise.
    286 **
    287 **	Side Effects:
    288 **		None.
    289 */
    290 int
    291 BpfRead(rconn, doread)
    292 	RMPCONN *rconn;
    293 	int doread;
    294 {
    295 	int datlen, caplen, hdrlen;
    296 	static u_int8_t *bp = NULL, *ep = NULL;
    297 	int cc;
    298 
    299 	/*
    300 	 *  The read() may block, or it may return one or more packets.
    301 	 *  We let the caller decide whether or not we can issue a read().
    302 	 */
    303 	if (doread) {
    304 		if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) {
    305 			syslog(LOG_ERR, "bpf: read: %m");
    306 			return(0);
    307 		} else {
    308 			bp = BpfPkt;
    309 			ep = BpfPkt + cc;
    310 		}
    311 	}
    312 
    313 #define bhp ((struct bpf_hdr *)bp)
    314 	/*
    315 	 *  If there is a new packet in the buffer, stuff it into `rconn'
    316 	 *  and return a success indication.
    317 	 */
    318 	if (bp < ep) {
    319 		datlen = bhp->bh_datalen;
    320 		caplen = bhp->bh_caplen;
    321 		hdrlen = bhp->bh_hdrlen;
    322 
    323 		if (caplen != datlen)
    324 			syslog(LOG_ERR,
    325 			       "bpf: short packet dropped (%d of %d bytes)",
    326 			       caplen, datlen);
    327 		else if (caplen > sizeof(struct rmp_packet))
    328 			syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)",
    329 			       caplen);
    330 		else {
    331 			rconn->rmplen = caplen;
    332 			memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp,
    333 			  sizeof(struct timeval));
    334 			memmove((char *)&rconn->rmp, (char *)bp + hdrlen,
    335 			    caplen);
    336 		}
    337 		bp += BPF_WORDALIGN(caplen + hdrlen);
    338 		return(1);
    339 	}
    340 #undef bhp
    341 
    342 	return(0);
    343 }
    344 
    345 /*
    346 **  BpfWrite -- Write packet to BPF device.
    347 **
    348 **	Parameters:
    349 **		rconn - packet to send.
    350 **
    351 **	Returns:
    352 **		True if write succeeded, False otherwise.
    353 **
    354 **	Side Effects:
    355 **		None.
    356 */
    357 int
    358 BpfWrite(rconn)
    359 	RMPCONN *rconn;
    360 {
    361 	if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) {
    362 		syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn));
    363 		return(0);
    364 	}
    365 
    366 	return(1);
    367 }
    368 
    369 /*
    370 **  BpfClose -- Close a BPF device.
    371 **
    372 **	Parameters:
    373 **		None.
    374 **
    375 **	Returns:
    376 **		Nothing.
    377 **
    378 **	Side Effects:
    379 **		None.
    380 */
    381 void
    382 BpfClose()
    383 {
    384 	struct ifreq ifr;
    385 
    386 	if (BpfPkt != NULL) {
    387 		free((char *)BpfPkt);
    388 		BpfPkt = NULL;
    389 	}
    390 
    391 	if (BpfFd == -1)
    392 		return;
    393 
    394 #ifdef MSG_EOR
    395 	ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
    396 #endif
    397 	ifr.ifr_addr.sa_family = AF_UNSPEC;
    398 	memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0],
    399 	    RMP_ADDRLEN);
    400 	if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0)
    401 		(void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0);
    402 
    403 	(void) close(BpfFd);
    404 	BpfFd = -1;
    405 }
    406