1 /* $OpenBSD: bpf.c,v 1.16 2004/05/10 20:50:21 canacar 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.16 2004/05/10 20:50:21 canacar 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 <ifaddrs.h> 67 #include "defs.h" 68 #include "pathnames.h" 69 70 static int BpfFd = -1; 71 static unsigned int BpfLen = 0; 72 static u_int8_t *BpfPkt = NULL; 73 74 /* 75 ** BpfOpen -- Open and initialize a BPF device. 76 ** 77 ** Parameters: 78 ** None. 79 ** 80 ** Returns: 81 ** File descriptor of opened BPF device (for select() etc). 82 ** 83 ** Side Effects: 84 ** If an error is encountered, the program terminates here. 85 */ 86 int 87 BpfOpen(void) 88 { 89 struct ifreq ifr; 90 char bpfdev[32]; 91 int n = 0; 92 93 /* 94 * Open the first available BPF device. 95 */ 96 do { 97 (void) snprintf(bpfdev, sizeof bpfdev, _PATH_BPF, n++); 98 BpfFd = open(bpfdev, O_RDWR); 99 } while (BpfFd < 0 && (errno == EBUSY || errno == EPERM)); 100 101 if (BpfFd < 0) { 102 syslog(LOG_ERR, "bpf: no available devices: %m"); 103 DoExit(); 104 } 105 106 /* 107 * Set interface name for bpf device, get data link layer 108 * type and make sure it's type Ethernet. 109 */ 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); 113 DoExit(); 114 } 115 116 /* 117 * Make sure we are dealing with an Ethernet device. 118 */ 119 if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) { 120 syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m"); 121 DoExit(); 122 } 123 if (n != DLT_EN10MB) { 124 syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported", 125 IntfName, n); 126 DoExit(); 127 } 128 129 /* 130 * On read(), return packets immediately (do not buffer them). 131 */ 132 n = 1; 133 if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) { 134 syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m"); 135 DoExit(); 136 } 137 138 /* 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. 143 */ 144 #ifdef MSG_EOR 145 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 146 #endif 147 ifr.ifr_addr.sa_family = AF_UNSPEC; 148 bcopy(&RmpMcastAddr[0], (char *)&ifr.ifr_addr.sa_data[0], RMP_ADDRLEN); 149 if (ioctl(BpfFd, SIOCADDMULTI, (caddr_t)&ifr) < 0) { 150 syslog(LOG_WARNING, 151 "bpf: can't add mcast addr (%m), setting promiscuous mode"); 152 153 if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) { 154 syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m"); 155 DoExit(); 156 } 157 } 158 159 /* 160 * Ask BPF how much buffer space it requires and allocate one. 161 */ 162 if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) { 163 syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m"); 164 DoExit(); 165 } 166 if (BpfPkt == NULL) 167 BpfPkt = (u_int8_t *)malloc(BpfLen); 168 169 if (BpfPkt == NULL) { 170 syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)", 171 BpfLen); 172 DoExit(); 173 } 174 175 /* 176 * Write a little program to snarf RMP Boot packets and stuff 177 * it down BPF's throat (i.e. set up the packet filter). 178 */ 179 { 180 #define RMP ((struct rmp_packet *)0) 181 static struct bpf_insn bpf_insn[] = { 182 /* make sure it is a 802.3 packet */ 183 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_hdr.len }, 184 { BPF_JMP|BPF_JGE|BPF_K, 7, 0, 0x600 }, 185 186 { BPF_LD|BPF_B|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_llc.dsap }, 187 { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP }, 188 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_llc.cntrl }, 189 { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP }, 190 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_llc.dxsap }, 191 { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP }, 192 { BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET }, 193 { BPF_RET|BPF_K, 0, 0, 0x0 } 194 }; 195 196 static struct bpf_insn bpf_wf_insn[] = { 197 /* make sure it is a 802.3 packet */ 198 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_hdr.len }, 199 { BPF_JMP|BPF_JGE|BPF_K, 12, 0, 0x600 }, 200 201 /* check the SNAP header */ 202 { BPF_LD|BPF_B|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_llc.dsap }, 203 { BPF_JMP|BPF_JEQ|BPF_K, 0, 10, IEEE_DSAP_HP }, 204 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_llc.cntrl }, 205 { BPF_JMP|BPF_JEQ|BPF_K, 0, 8, IEEE_CNTL_HP }, 206 207 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_llc.sxsap }, 208 { BPF_JMP|BPF_JEQ|BPF_K, 0, 6, HPEXT_DXSAP }, 209 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (u_int32_t)&RMP->hp_llc.dxsap }, 210 { BPF_JMP|BPF_JEQ|BPF_K, 0, 4, HPEXT_SXSAP }, 211 212 /* check return type code */ 213 { BPF_LD|BPF_B|BPF_ABS, 0, 0, 214 (u_int32_t)&RMP->rmp_proto.rmp_raw.rmp_type }, 215 { BPF_JMP|BPF_JEQ|BPF_K, 1, 0, RMP_BOOT_REPL }, 216 { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, RMP_READ_REPL }, 217 218 { BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET }, 219 { BPF_RET|BPF_K, 0, 0, 0x0 } 220 }; 221 #undef RMP 222 static struct bpf_program bpf_pgm = { 223 sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn 224 }; 225 226 static struct bpf_program bpf_w_pgm = { 227 sizeof(bpf_wf_insn)/sizeof(bpf_wf_insn[0]), bpf_wf_insn 228 }; 229 230 if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) { 231 syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m"); 232 DoExit(); 233 } 234 235 if (ioctl(BpfFd, BIOCSETWF, (caddr_t)&bpf_w_pgm) < 0) { 236 syslog(LOG_ERR, "bpf: ioctl(BIOCSETWF): %m"); 237 DoExit(); 238 } 239 240 if (ioctl(BpfFd, BIOCLOCK) < 0) { 241 syslog(LOG_ERR, "bpf: ioctl(BIOCLOCK): %m"); 242 DoExit(); 243 } 244 } 245 246 return(BpfFd); 247 } 248 249 /* 250 ** BPF GetIntfName -- Return the name of a network interface attached to 251 ** the system, or 0 if none can be found. The interface 252 ** must be configured up; the lowest unit number is 253 ** preferred; loopback is ignored. 254 ** 255 ** Parameters: 256 ** errmsg - if no network interface found, *errmsg explains why. 257 ** 258 ** Returns: 259 ** A (static) pointer to interface name, or NULL on error. 260 ** 261 ** Side Effects: 262 ** None. 263 */ 264 char * 265 BpfGetIntfName(char **errmsg) 266 { 267 int minunit = 999, n; 268 char *cp; 269 static char device[IFNAMSIZ]; 270 static char errbuf[128] = "No Error!"; 271 struct ifaddrs *ifap, *ifa, *mp = NULL; 272 273 if (errmsg != NULL) 274 *errmsg = errbuf; 275 276 if (getifaddrs(&ifap) != 0) { 277 (void) strlcpy(errbuf, "bpf: getifaddrs: %m", sizeof(errbuf)); 278 return(NULL); 279 } 280 281 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 282 /* 283 * If interface is down or this is the loopback interface, 284 * ignore it. 285 */ 286 if ((ifa->ifa_flags & IFF_UP) == 0 || 287 #ifdef IFF_LOOPBACK 288 (ifa->ifa_flags & IFF_LOOPBACK)) 289 #else 290 (strcmp(ifa->ifa_name, "lo0") == 0)) 291 #endif 292 continue; 293 294 for (cp = ifa->ifa_name; !isdigit(*cp); ++cp) 295 ; 296 n = atoi(cp); 297 if (n < minunit) { 298 minunit = n; 299 mp = ifa; 300 } 301 } 302 303 if (mp == 0) { 304 (void) strlcpy(errbuf, "bpf: no interfaces found", 305 sizeof(errbuf)); 306 freeifaddrs(ifap); 307 return(NULL); 308 } 309 310 (void) strlcpy(device, mp->ifa_name, sizeof device); 311 freeifaddrs(ifap); 312 return(device); 313 } 314 315 /* 316 ** BpfRead -- Read packets from a BPF device and fill in `rconn'. 317 ** 318 ** Parameters: 319 ** rconn - filled in with next packet. 320 ** doread - is True if we can issue a read() syscall. 321 ** 322 ** Returns: 323 ** True if `rconn' contains a new packet, False otherwise. 324 ** 325 ** Side Effects: 326 ** None. 327 */ 328 int 329 BpfRead(RMPCONN *rconn, int doread) 330 { 331 int datlen, caplen, hdrlen; 332 static u_int8_t *bp = NULL, *ep = NULL; 333 int cc; 334 335 /* 336 * The read() may block, or it may return one or more packets. 337 * We let the caller decide whether or not we can issue a read(). 338 */ 339 if (doread) { 340 if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) { 341 syslog(LOG_ERR, "bpf: read: %m"); 342 return(0); 343 } else { 344 bp = BpfPkt; 345 ep = BpfPkt + cc; 346 } 347 } 348 349 #define bhp ((struct bpf_hdr *)bp) 350 /* 351 * If there is a new packet in the buffer, stuff it into `rconn' 352 * and return a success indication. 353 */ 354 if (bp < ep) { 355 datlen = bhp->bh_datalen; 356 caplen = bhp->bh_caplen; 357 hdrlen = bhp->bh_hdrlen; 358 359 if (caplen != datlen) 360 syslog(LOG_ERR, 361 "bpf: short packet dropped (%d of %d bytes)", 362 caplen, datlen); 363 else if (caplen > sizeof(struct rmp_packet)) 364 syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)", 365 caplen); 366 else { 367 rconn->rmplen = caplen; 368 bcopy((char *)&bhp->bh_tstamp, (char *)&rconn->tstamp, 369 sizeof(struct timeval)); 370 bcopy((char *)bp + hdrlen, (char *)&rconn->rmp, caplen); 371 } 372 bp += BPF_WORDALIGN(caplen + hdrlen); 373 return(1); 374 } 375 #undef bhp 376 377 return(0); 378 } 379 380 /* 381 ** BpfWrite -- Write packet to BPF device. 382 ** 383 ** Parameters: 384 ** rconn - packet to send. 385 ** 386 ** Returns: 387 ** True if write succeeded, False otherwise. 388 ** 389 ** Side Effects: 390 ** None. 391 */ 392 int 393 BpfWrite(RMPCONN *rconn) 394 { 395 if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) { 396 syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn)); 397 return(0); 398 } 399 400 return(1); 401 } 402