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.
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.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)bpf.c 8.1 (Berkeley) 6/4/93
39 * From: Utah Hdr: bpf.c 3.1 92/07/06
40 * Author: Jeff Forys, University of Utah CSS
45 static const char sccsid[] = "@(#)bpf.c 8.1 (Berkeley) 6/4/93";
47 static const char rcsid[] =
51 #include <sys/param.h>
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
68 #include "pathnames.h"
70 static int BpfFd = -1;
71 static unsigned BpfLen = 0;
72 static u_int8_t *BpfPkt = NULL;
75 ** BpfOpen -- Open and initialize a BPF device.
81 ** File descriptor of opened BPF device (for select() etc).
84 ** If an error is encountered, the program terminates here.
94 * Open the first available BPF device.
97 (void) sprintf(bpfdev, _PATH_BPF, n++);
98 BpfFd = open(bpfdev, O_RDWR);
99 } while (BpfFd < 0 && (errno == EBUSY || errno == EPERM));
102 syslog(LOG_ERR, "bpf: no available devices: %m");
107 * Set interface name for bpf device, get data link layer
108 * type and make sure it's type Ethernet.
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);
117 * Make sure we are dealing with an Ethernet device.
119 if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) {
120 syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m");
123 if (n != DLT_EN10MB) {
124 syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported",
130 * On read(), return packets immediately (do not buffer them).
133 if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) {
134 syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m");
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.
145 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
147 ifr.ifr_addr.sa_family = AF_UNSPEC;
148 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], RMP_ADDRLEN);
149 if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) {
150 syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m");
155 * Ask BPF how much buffer space it requires and allocate one.
157 if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) {
158 syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m");
162 BpfPkt = (u_int8_t *)malloc(BpfLen);
164 if (BpfPkt == NULL) {
165 syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)",
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).
175 #define RMP ((struct rmp_packet *)0)
176 static struct bpf_insn bpf_insn[] = {
177 { BPF_LD|BPF_B|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dsap },
178 { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP },
179 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.cntrl },
180 { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP },
181 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dxsap },
182 { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP },
183 { BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET },
184 { BPF_RET|BPF_K, 0, 0, 0x0 }
187 static struct bpf_program bpf_pgm = {
188 sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn
191 if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) {
192 syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m");
201 ** BPF GetIntfName -- Return the name of a network interface attached to
202 ** the system, or 0 if none can be found. The interface
203 ** must be configured up; the lowest unit number is
204 ** preferred; loopback is ignored.
207 ** errmsg - if no network interface found, *errmsg explains why.
210 ** A (static) pointer to interface name, or NULL on error.
216 BpfGetIntfName(char **errmsg)
218 struct ifreq ibuf[8], *ifrp, *ifend, *mp;
223 static char device[sizeof(ifrp->ifr_name)];
224 static char errbuf[128] = "No Error!";
229 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
230 (void) strcpy(errbuf, "bpf: socket: %m");
233 ifc.ifc_len = sizeof ibuf;
234 ifc.ifc_buf = (caddr_t)ibuf;
237 if (ioctl(fd, OSIOCGIFCONF, (char *)&ifc) < 0 ||
238 ifc.ifc_len < sizeof(struct ifreq)) {
239 (void) strcpy(errbuf, "bpf: ioctl(OSIOCGIFCONF): %m");
243 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
244 ifc.ifc_len < sizeof(struct ifreq)) {
245 (void) strcpy(errbuf, "bpf: ioctl(SIOCGIFCONF): %m");
250 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
254 for (; ifrp < ifend; ++ifrp) {
255 if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
256 (void) strcpy(errbuf, "bpf: ioctl(SIOCGIFFLAGS): %m");
261 * If interface is down or this is the loopback interface,
264 if ((ifrp->ifr_flags & IFF_UP) == 0 ||
266 (ifrp->ifr_flags & IFF_LOOPBACK))
268 (strcmp(ifrp->ifr_name, "lo0") == 0))
272 for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp)
283 (void) strcpy(errbuf, "bpf: no interfaces found");
287 (void) strcpy(device, mp->ifr_name);
292 ** BpfRead -- Read packets from a BPF device and fill in `rconn'.
295 ** rconn - filled in with next packet.
296 ** doread - is True if we can issue a read() syscall.
299 ** True if `rconn' contains a new packet, False otherwise.
305 BpfRead(RMPCONN *rconn, int doread)
307 int datlen, caplen, hdrlen;
308 static u_int8_t *bp = NULL, *ep = NULL;
312 * The read() may block, or it may return one or more packets.
313 * We let the caller decide whether or not we can issue a read().
316 if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) {
317 syslog(LOG_ERR, "bpf: read: %m");
325 #define bhp ((struct bpf_hdr *)bp)
327 * If there is a new packet in the buffer, stuff it into `rconn'
328 * and return a success indication.
331 datlen = bhp->bh_datalen;
332 caplen = bhp->bh_caplen;
333 hdrlen = bhp->bh_hdrlen;
335 if (caplen != datlen)
337 "bpf: short packet dropped (%d of %d bytes)",
339 else if (caplen > sizeof(struct rmp_packet))
340 syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)",
343 rconn->rmplen = caplen;
344 memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp,
345 sizeof(struct timeval));
346 memmove((char *)&rconn->rmp, (char *)bp + hdrlen, caplen);
348 bp += BPF_WORDALIGN(caplen + hdrlen);
357 ** BpfWrite -- Write packet to BPF device.
360 ** rconn - packet to send.
363 ** True if write succeeded, False otherwise.
369 BpfWrite(RMPCONN *rconn)
371 if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) {
372 syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn));
380 ** BpfClose -- Close a BPF device.
396 if (BpfPkt != NULL) {
397 free((char *)BpfPkt);
405 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
407 ifr.ifr_addr.sa_family = AF_UNSPEC;
408 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], RMP_ADDRLEN);
409 if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0)
410 (void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0);