1 /* $NetBSD: bpf.c,v 1.20 2011/02/08 20:20:28 rmind 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@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. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: @(#)bpf.c 8.1 (Berkeley) 6/4/93 40 * 41 * From: Utah Hdr: bpf.c 3.1 92/07/06 42 * Author: Jeff Forys, University of Utah CSS 43 */ 44 45 #include <sys/cdefs.h> 46 #ifndef lint 47 #if 0 48 static char sccsid[] = "@(#)bpf.c 8.1 (Berkeley) 6/4/93"; 49 #else 50 __RCSID("$NetBSD: bpf.c,v 1.20 2011/02/08 20:20:28 rmind Exp $"); 51 #endif 52 #endif /* not lint */ 53 54 #include <sys/param.h> 55 #include <sys/ioctl.h> 56 #include <sys/socket.h> 57 58 #include <net/if.h> 59 #include <net/bpf.h> 60 61 #include <ctype.h> 62 #include <errno.h> 63 #include <fcntl.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <syslog.h> 68 #include <paths.h> 69 #include <unistd.h> 70 #include <ifaddrs.h> 71 #include "defs.h" 72 #include "pathnames.h" 73 74 static int BpfFd = -1; 75 static unsigned BpfLen = 0; 76 static u_int8_t *BpfPkt = NULL; 77 78 /* 79 ** BpfOpen -- Open and initialize a BPF device. 80 ** 81 ** Parameters: 82 ** None. 83 ** 84 ** Returns: 85 ** File descriptor of opened BPF device (for select() etc). 86 ** 87 ** Side Effects: 88 ** If an error is encountered, the program terminates here. 89 */ 90 int 91 BpfOpen() 92 { 93 struct ifreq ifr; 94 u_int bufsize = 32768; 95 int n; 96 97 BpfFd = open(_PATH_BPF, O_RDWR); 98 if (BpfFd < 0) { 99 syslog(LOG_ERR, "bpf: no available devices: %m"); 100 Exit(0); 101 } 102 103 if (ioctl(BpfFd, BIOCSBLEN, &bufsize) < 0) { 104 syslog(LOG_ERR, "bpf: ioctl(BIOCSBLEN,%d): %m", bufsize); 105 } 106 107 /* 108 * Set interface name for bpf device, get data link layer 109 * type and make sure it's type Ethernet. 110 */ 111 (void) strncpy(ifr.ifr_name, IntfName, sizeof(ifr.ifr_name)); 112 if (ioctl(BpfFd, BIOCSETIF, (caddr_t)&ifr) < 0) { 113 syslog(LOG_ERR, "bpf: ioctl(BIOCSETIF,%s): %m", IntfName); 114 Exit(0); 115 } 116 117 /* 118 * Make sure we are dealing with an Ethernet device. 119 */ 120 if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) { 121 syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m"); 122 Exit(0); 123 } 124 if (n != DLT_EN10MB) { 125 syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported", 126 IntfName, n); 127 Exit(0); 128 } 129 130 /* 131 * On read(), return packets immediately (do not buffer them). 132 */ 133 n = 1; 134 if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) { 135 syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m"); 136 Exit(0); 137 } 138 139 /* 140 * Try to enable the chip/driver's multicast address filter to 141 * grab our RMP address. If this fails, try promiscuous mode. 142 * If this fails, there's no way we are going to get any RMP 143 * packets so just exit here. 144 */ 145 #ifdef MSG_EOR 146 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 147 #endif 148 ifr.ifr_addr.sa_family = AF_UNSPEC; 149 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], 150 RMP_ADDRLEN); 151 if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) { 152 syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m"); 153 Exit(0); 154 } 155 156 /* 157 * Ask BPF how much buffer space it requires and allocate one. 158 */ 159 if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) { 160 syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m"); 161 Exit(0); 162 } 163 if (BpfPkt == NULL) 164 BpfPkt = (u_int8_t *)malloc(BpfLen); 165 166 if (BpfPkt == NULL) { 167 syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)", 168 BpfLen); 169 Exit(0); 170 } 171 172 /* 173 * Write a little program to snarf RMP Boot packets and stuff 174 * it down BPF's throat (i.e. set up the packet filter). 175 */ 176 { 177 #define RMP ((struct rmp_packet *)0) 178 static struct bpf_insn bpf_insn[] = { 179 { BPF_LD|BPF_B|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dsap }, 180 { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP }, 181 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.cntrl }, 182 { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP }, 183 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dxsap }, 184 { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP }, 185 { BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET }, 186 { BPF_RET|BPF_K, 0, 0, 0x0 } 187 }; 188 #undef RMP 189 static struct bpf_program bpf_pgm = { 190 sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn 191 }; 192 193 if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) { 194 syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m"); 195 Exit(0); 196 } 197 } 198 199 return(BpfFd); 200 } 201 202 /* 203 ** BPF GetIntfName -- Return the name of a network interface attached to 204 ** the system, or 0 if none can be found. The interface 205 ** must be configured up; the lowest unit number is 206 ** preferred; loopback is ignored. 207 ** 208 ** Parameters: 209 ** errmsg - if no network interface found, *errmsg explains why. 210 ** 211 ** Returns: 212 ** A (static) pointer to interface name, or NULL on error. 213 ** 214 ** Side Effects: 215 ** None. 216 */ 217 char * 218 BpfGetIntfName(errmsg) 219 char **errmsg; 220 { 221 struct ifaddrs *ifap, *ifa, *p; 222 int minunit, n; 223 char *cp; 224 static char device[IFNAMSIZ + 1]; 225 static char errbuf[128] = "No Error!"; 226 227 if (errmsg != NULL) 228 *errmsg = errbuf; 229 230 if (getifaddrs(&ifap) != 0) { 231 (void) strlcpy(errbuf, "bpf: getifaddrs: %m", sizeof(errbuf)); 232 return(NULL); 233 } 234 235 p = NULL; 236 minunit = 666; 237 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 238 /* 239 * If interface is down or this is the loopback interface, 240 * ignore it. 241 */ 242 if ((ifa->ifa_flags & IFF_UP) == 0 || 243 #ifdef IFF_LOOPBACK 244 (ifa->ifa_flags & IFF_LOOPBACK)) 245 #else 246 (strcmp(ifa->ifa_name, "lo0") == 0)) 247 #endif 248 continue; 249 250 for (cp = ifa->ifa_name; !isdigit((unsigned char)*cp); ++cp) 251 ; 252 n = atoi(cp); 253 if (n < minunit) { 254 minunit = n; 255 p = ifa; 256 } 257 } 258 if (p == NULL) { 259 (void) strlcpy(errbuf, "bpf: no interfaces found", 260 sizeof(errbuf)); 261 freeifaddrs(ifap); 262 return(NULL); 263 } 264 265 (void) strlcpy(device, p->ifa_name, sizeof(device)); 266 freeifaddrs(ifap); 267 return(device); 268 } 269 270 /* 271 ** BpfRead -- Read packets from a BPF device and fill in `rconn'. 272 ** 273 ** Parameters: 274 ** rconn - filled in with next packet. 275 ** doread - is True if we can issue a read() syscall. 276 ** 277 ** Returns: 278 ** True if `rconn' contains a new packet, False otherwise. 279 ** 280 ** Side Effects: 281 ** None. 282 */ 283 int 284 BpfRead(rconn, doread) 285 RMPCONN *rconn; 286 int doread; 287 { 288 int datlen, caplen, hdrlen; 289 static u_int8_t *bp = NULL, *ep = NULL; 290 int cc; 291 292 /* 293 * The read() may block, or it may return one or more packets. 294 * We let the caller decide whether or not we can issue a read(). 295 */ 296 if (doread) { 297 if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) { 298 syslog(LOG_ERR, "bpf: read: %m"); 299 return(0); 300 } else { 301 bp = BpfPkt; 302 ep = BpfPkt + cc; 303 } 304 } 305 306 #define bhp ((struct bpf_hdr *)bp) 307 /* 308 * If there is a new packet in the buffer, stuff it into `rconn' 309 * and return a success indication. 310 */ 311 if (bp < ep) { 312 datlen = bhp->bh_datalen; 313 caplen = bhp->bh_caplen; 314 hdrlen = bhp->bh_hdrlen; 315 316 if (caplen != datlen) 317 syslog(LOG_ERR, 318 "bpf: short packet dropped (%d of %d bytes)", 319 caplen, datlen); 320 else if (caplen > (int)sizeof(struct rmp_packet)) 321 syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)", 322 caplen); 323 else { 324 rconn->rmplen = caplen; 325 memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp, 326 sizeof(struct timeval)); 327 memmove((char *)&rconn->rmp, (char *)bp + hdrlen, 328 caplen); 329 } 330 bp += BPF_WORDALIGN(caplen + hdrlen); 331 return(1); 332 } 333 #undef bhp 334 335 return(0); 336 } 337 338 /* 339 ** BpfWrite -- Write packet to BPF device. 340 ** 341 ** Parameters: 342 ** rconn - packet to send. 343 ** 344 ** Returns: 345 ** True if write succeeded, False otherwise. 346 ** 347 ** Side Effects: 348 ** None. 349 */ 350 int 351 BpfWrite(rconn) 352 RMPCONN *rconn; 353 { 354 if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) { 355 syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn)); 356 return(0); 357 } 358 359 return(1); 360 } 361 362 /* 363 ** BpfClose -- Close a BPF device. 364 ** 365 ** Parameters: 366 ** None. 367 ** 368 ** Returns: 369 ** Nothing. 370 ** 371 ** Side Effects: 372 ** None. 373 */ 374 void 375 BpfClose() 376 { 377 struct ifreq ifr; 378 379 if (BpfPkt != NULL) { 380 free((char *)BpfPkt); 381 BpfPkt = NULL; 382 } 383 384 if (BpfFd == -1) 385 return; 386 387 #ifdef MSG_EOR 388 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 389 #endif 390 ifr.ifr_addr.sa_family = AF_UNSPEC; 391 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], 392 RMP_ADDRLEN); 393 if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0) 394 (void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0); 395 396 (void) close(BpfFd); 397 BpfFd = -1; 398 } 399