1 /* $OpenBSD: bpf.c,v 1.12 2003/06/02 23:36:54 millert Exp $ */ 2 /* $NetBSD: bpf.c,v 1.5.2.1 1995/11/14 08:45:42 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1988, 1992 The University of Utah and the Center 6 * for Software Science (CSS). 7 * Copyright (c) 1992, 1993 8 * The Regents of the University of California. All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * the Center for Software Science of the University of Utah Computer 12 * Science Department. CSS requests users of this software to return 13 * to css-dist@cs.utah.edu any improvements that they make and grant 14 * CSS redistribution rights. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)bpf.c 8.1 (Berkeley) 6/4/93 41 * 42 * From: Utah Hdr: bpf.c 3.1 92/07/06 43 * Author: Jeff Forys, University of Utah CSS 44 */ 45 46 #ifndef lint 47 /*static char sccsid[] = "@(#)bpf.c 8.1 (Berkeley) 6/4/93";*/ 48 static char rcsid[] = "$OpenBSD: bpf.c,v 1.12 2003/06/02 23:36:54 millert Exp $"; 49 #endif /* not lint */ 50 51 #include <sys/param.h> 52 #include <sys/ioctl.h> 53 #include <sys/socket.h> 54 55 #include <net/if.h> 56 #include <net/bpf.h> 57 58 #include <ctype.h> 59 #include <errno.h> 60 #include <fcntl.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <syslog.h> 65 #include <unistd.h> 66 #include "defs.h" 67 #include "pathnames.h" 68 69 static int BpfFd = -1; 70 static unsigned int BpfLen = 0; 71 static u_int8_t *BpfPkt = NULL; 72 73 /* 74 ** BpfOpen -- Open and initialize a BPF device. 75 ** 76 ** Parameters: 77 ** None. 78 ** 79 ** Returns: 80 ** File descriptor of opened BPF device (for select() etc). 81 ** 82 ** Side Effects: 83 ** If an error is encountered, the program terminates here. 84 */ 85 int 86 BpfOpen(void) 87 { 88 struct ifreq ifr; 89 char bpfdev[32]; 90 int n = 0; 91 92 /* 93 * Open the first available BPF device. 94 */ 95 do { 96 (void) snprintf(bpfdev, sizeof bpfdev, _PATH_BPF, n++); 97 BpfFd = open(bpfdev, O_RDWR); 98 } while (BpfFd < 0 && (errno == EBUSY || errno == EPERM)); 99 100 if (BpfFd < 0) { 101 syslog(LOG_ERR, "bpf: no available devices: %m"); 102 DoExit(); 103 } 104 105 /* 106 * Set interface name for bpf device, get data link layer 107 * type and make sure it's type Ethernet. 108 */ 109 (void) strncpy(ifr.ifr_name, IntfName, sizeof(ifr.ifr_name)); 110 if (ioctl(BpfFd, BIOCSETIF, (caddr_t)&ifr) < 0) { 111 syslog(LOG_ERR, "bpf: ioctl(BIOCSETIF,%s): %m", IntfName); 112 DoExit(); 113 } 114 115 /* 116 * Make sure we are dealing with an Ethernet device. 117 */ 118 if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) { 119 syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m"); 120 DoExit(); 121 } 122 if (n != DLT_EN10MB) { 123 syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported", 124 IntfName, n); 125 DoExit(); 126 } 127 128 /* 129 * On read(), return packets immediately (do not buffer them). 130 */ 131 n = 1; 132 if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) { 133 syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m"); 134 DoExit(); 135 } 136 137 /* 138 * Try to enable the chip/driver's multicast address filter to 139 * grab our RMP address. If this fails, try promiscuous mode. 140 * If this fails, there's no way we are going to get any RMP 141 * packets so just exit here. 142 */ 143 #ifdef MSG_EOR 144 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 145 #endif 146 ifr.ifr_addr.sa_family = AF_UNSPEC; 147 bcopy(&RmpMcastAddr[0], (char *)&ifr.ifr_addr.sa_data[0], RMP_ADDRLEN); 148 if (ioctl(BpfFd, SIOCADDMULTI, (caddr_t)&ifr) < 0) { 149 syslog(LOG_WARNING, 150 "bpf: can't add mcast addr (%m), setting promiscuous mode"); 151 152 if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) { 153 syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m"); 154 DoExit(); 155 } 156 } 157 158 /* 159 * Ask BPF how much buffer space it requires and allocate one. 160 */ 161 if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) { 162 syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m"); 163 DoExit(); 164 } 165 if (BpfPkt == NULL) 166 BpfPkt = (u_int8_t *)malloc(BpfLen); 167 168 if (BpfPkt == NULL) { 169 syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)", 170 BpfLen); 171 DoExit(); 172 } 173 174 /* 175 * Write a little program to snarf RMP Boot packets and stuff 176 * it down BPF's throat (i.e. set up the packet filter). 177 */ 178 { 179 #define RMP ((struct rmp_packet *)0) 180 static struct bpf_insn bpf_insn[] = { 181 { BPF_LD|BPF_B|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dsap }, 182 { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP }, 183 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.cntrl }, 184 { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP }, 185 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dxsap }, 186 { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP }, 187 { BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET }, 188 { BPF_RET|BPF_K, 0, 0, 0x0 } 189 }; 190 #undef RMP 191 static struct bpf_program bpf_pgm = { 192 sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn 193 }; 194 195 if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) { 196 syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m"); 197 DoExit(); 198 } 199 } 200 201 return(BpfFd); 202 } 203 204 /* 205 ** BPF GetIntfName -- Return the name of a network interface attached to 206 ** the system, or 0 if none can be found. The interface 207 ** must be configured up; the lowest unit number is 208 ** preferred; loopback is ignored. 209 ** 210 ** Parameters: 211 ** errmsg - if no network interface found, *errmsg explains why. 212 ** 213 ** Returns: 214 ** A (static) pointer to interface name, or NULL on error. 215 ** 216 ** Side Effects: 217 ** None. 218 */ 219 char * 220 BpfGetIntfName(char **errmsg) 221 { 222 struct ifreq ibuf[8], *ifrp, *ifend, *mp; 223 struct ifconf ifc; 224 int fd; 225 int minunit, n; 226 char *cp; 227 static char device[sizeof(ifrp->ifr_name)]; 228 static char errbuf[128] = "No Error!"; 229 230 if (errmsg != NULL) 231 *errmsg = errbuf; 232 233 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 234 (void) strlcpy(errbuf, "bpf: socket: %m", sizeof(errbuf)); 235 return(NULL); 236 } 237 ifc.ifc_len = sizeof ibuf; 238 ifc.ifc_buf = (caddr_t)ibuf; 239 240 #ifdef OSIOCGIFCONF 241 if (ioctl(fd, OSIOCGIFCONF, (char *)&ifc) < 0 || 242 ifc.ifc_len < sizeof(struct ifreq)) { 243 (void) strlcpy(errbuf, "bpf: ioctl(OSIOCGIFCONF): %m", 244 sizeof (errbuf)); 245 return(NULL); 246 } 247 #else 248 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || 249 ifc.ifc_len < sizeof(struct ifreq)) { 250 (void) strlcpy(errbuf, "bpf: ioctl(SIOCGIFCONF): %m", 251 sizeof(errbuf)); 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) strlcpy(errbuf, "bpf: ioctl(SIOCGIFFLAGS): %m", 263 sizeof(errbuf)); 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) strlcpy(errbuf, "bpf: no interfaces found", 291 sizeof(errbuf)); 292 return(NULL); 293 } 294 295 (void) strlcpy(device, mp->ifr_name, sizeof device); 296 return(device); 297 } 298 299 /* 300 ** BpfRead -- Read packets from a BPF device and fill in `rconn'. 301 ** 302 ** Parameters: 303 ** rconn - filled in with next packet. 304 ** doread - is True if we can issue a read() syscall. 305 ** 306 ** Returns: 307 ** True if `rconn' contains a new packet, False otherwise. 308 ** 309 ** Side Effects: 310 ** None. 311 */ 312 int 313 BpfRead(RMPCONN *rconn, int doread) 314 { 315 int datlen, caplen, hdrlen; 316 static u_int8_t *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(RMPCONN *rconn) 378 { 379 if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) { 380 syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn)); 381 return(0); 382 } 383 384 return(1); 385 } 386