1 /* 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. 6 * 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. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 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. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)bpf.c 8.1 (Berkeley) 6/4/93 42 * $Id: bpf.c,v 1.7 1997/06/29 19:00:01 steve Exp $ 43 * 44 * From: Utah Hdr: bpf.c 3.1 92/07/06 45 * Author: Jeff Forys, University of Utah CSS 46 */ 47 48 #ifndef lint 49 static const char sccsid[] = "@(#)bpf.c 8.1 (Berkeley) 6/4/93"; 50 #endif /* not lint */ 51 52 #include <sys/param.h> 53 #include <sys/ioctl.h> 54 #include <sys/socket.h> 55 #include <sys/time.h> 56 57 #include <net/if.h> 58 #include <net/bpf.h> 59 60 #include <ctype.h> 61 #include <errno.h> 62 #include <fcntl.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <syslog.h> 67 #include <unistd.h> 68 #include "defs.h" 69 #include "pathnames.h" 70 71 static int BpfFd = -1; 72 static unsigned BpfLen = 0; 73 static u_int8_t *BpfPkt = NULL; 74 75 /* 76 ** BpfOpen -- Open and initialize a BPF device. 77 ** 78 ** Parameters: 79 ** None. 80 ** 81 ** Returns: 82 ** File descriptor of opened BPF device (for select() etc). 83 ** 84 ** Side Effects: 85 ** If an error is encountered, the program terminates here. 86 */ 87 int 88 BpfOpen() 89 { 90 struct ifreq ifr; 91 char bpfdev[32]; 92 int n = 0; 93 94 /* 95 * Open the first available BPF device. 96 */ 97 do { 98 (void) sprintf(bpfdev, _PATH_BPF, n++); 99 BpfFd = open(bpfdev, O_RDWR); 100 } while (BpfFd < 0 && (errno == EBUSY || errno == EPERM)); 101 102 if (BpfFd < 0) { 103 syslog(LOG_ERR, "bpf: no available devices: %m"); 104 Exit(0); 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], RMP_ADDRLEN); 150 if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) { 151 syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m"); 152 Exit(0); 153 } 154 155 /* 156 * Ask BPF how much buffer space it requires and allocate one. 157 */ 158 if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) { 159 syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m"); 160 Exit(0); 161 } 162 if (BpfPkt == NULL) 163 BpfPkt = (u_int8_t *)malloc(BpfLen); 164 165 if (BpfPkt == NULL) { 166 syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)", 167 BpfLen); 168 Exit(0); 169 } 170 171 /* 172 * Write a little program to snarf RMP Boot packets and stuff 173 * it down BPF's throat (i.e. set up the packet filter). 174 */ 175 { 176 #define RMP ((struct rmp_packet *)0) 177 static struct bpf_insn bpf_insn[] = { 178 { BPF_LD|BPF_B|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dsap }, 179 { BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP }, 180 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.cntrl }, 181 { BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP }, 182 { BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dxsap }, 183 { BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP }, 184 { BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET }, 185 { BPF_RET|BPF_K, 0, 0, 0x0 } 186 }; 187 #undef RMP 188 static struct bpf_program bpf_pgm = { 189 sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn 190 }; 191 192 if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) { 193 syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m"); 194 Exit(0); 195 } 196 } 197 198 return(BpfFd); 199 } 200 201 /* 202 ** BPF GetIntfName -- Return the name of a network interface attached to 203 ** the system, or 0 if none can be found. The interface 204 ** must be configured up; the lowest unit number is 205 ** preferred; loopback is ignored. 206 ** 207 ** Parameters: 208 ** errmsg - if no network interface found, *errmsg explains why. 209 ** 210 ** Returns: 211 ** A (static) pointer to interface name, or NULL on error. 212 ** 213 ** Side Effects: 214 ** None. 215 */ 216 char * 217 BpfGetIntfName(errmsg) 218 char **errmsg; 219 { 220 struct ifreq ibuf[8], *ifrp, *ifend, *mp; 221 struct ifconf ifc; 222 int fd; 223 int minunit, n; 224 char *cp; 225 static char device[sizeof(ifrp->ifr_name)]; 226 static char errbuf[128] = "No Error!"; 227 228 if (errmsg != NULL) 229 *errmsg = errbuf; 230 231 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 232 (void) strcpy(errbuf, "bpf: socket: %m"); 233 return(NULL); 234 } 235 ifc.ifc_len = sizeof ibuf; 236 ifc.ifc_buf = (caddr_t)ibuf; 237 238 #ifdef OSIOCGIFCONF 239 if (ioctl(fd, OSIOCGIFCONF, (char *)&ifc) < 0 || 240 ifc.ifc_len < sizeof(struct ifreq)) { 241 (void) strcpy(errbuf, "bpf: ioctl(OSIOCGIFCONF): %m"); 242 return(NULL); 243 } 244 #else 245 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || 246 ifc.ifc_len < sizeof(struct ifreq)) { 247 (void) strcpy(errbuf, "bpf: ioctl(SIOCGIFCONF): %m"); 248 return(NULL); 249 } 250 #endif 251 ifrp = ibuf; 252 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); 253 254 mp = 0; 255 minunit = 666; 256 for (; ifrp < ifend; ++ifrp) { 257 if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) { 258 (void) strcpy(errbuf, "bpf: ioctl(SIOCGIFFLAGS): %m"); 259 return(NULL); 260 } 261 262 /* 263 * If interface is down or this is the loopback interface, 264 * ignore it. 265 */ 266 if ((ifrp->ifr_flags & IFF_UP) == 0 || 267 #ifdef IFF_LOOPBACK 268 (ifrp->ifr_flags & IFF_LOOPBACK)) 269 #else 270 (strcmp(ifrp->ifr_name, "lo0") == 0)) 271 #endif 272 continue; 273 274 for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp) 275 ; 276 n = atoi(cp); 277 if (n < minunit) { 278 minunit = n; 279 mp = ifrp; 280 } 281 } 282 283 (void) close(fd); 284 if (mp == 0) { 285 (void) strcpy(errbuf, "bpf: no interfaces found"); 286 return(NULL); 287 } 288 289 (void) strcpy(device, mp->ifr_name); 290 return(device); 291 } 292 293 /* 294 ** BpfRead -- Read packets from a BPF device and fill in `rconn'. 295 ** 296 ** Parameters: 297 ** rconn - filled in with next packet. 298 ** doread - is True if we can issue a read() syscall. 299 ** 300 ** Returns: 301 ** True if `rconn' contains a new packet, False otherwise. 302 ** 303 ** Side Effects: 304 ** None. 305 */ 306 int 307 BpfRead(rconn, doread) 308 RMPCONN *rconn; 309 int doread; 310 { 311 int datlen, caplen, hdrlen; 312 static u_int8_t *bp = NULL, *ep = NULL; 313 int cc; 314 315 /* 316 * The read() may block, or it may return one or more packets. 317 * We let the caller decide whether or not we can issue a read(). 318 */ 319 if (doread) { 320 if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) { 321 syslog(LOG_ERR, "bpf: read: %m"); 322 return(0); 323 } else { 324 bp = BpfPkt; 325 ep = BpfPkt + cc; 326 } 327 } 328 329 #define bhp ((struct bpf_hdr *)bp) 330 /* 331 * If there is a new packet in the buffer, stuff it into `rconn' 332 * and return a success indication. 333 */ 334 if (bp < ep) { 335 datlen = bhp->bh_datalen; 336 caplen = bhp->bh_caplen; 337 hdrlen = bhp->bh_hdrlen; 338 339 if (caplen != datlen) 340 syslog(LOG_ERR, 341 "bpf: short packet dropped (%d of %d bytes)", 342 caplen, datlen); 343 else if (caplen > sizeof(struct rmp_packet)) 344 syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)", 345 caplen); 346 else { 347 rconn->rmplen = caplen; 348 memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp, 349 sizeof(struct timeval)); 350 memmove((char *)&rconn->rmp, (char *)bp + hdrlen, caplen); 351 } 352 bp += BPF_WORDALIGN(caplen + hdrlen); 353 return(1); 354 } 355 #undef bhp 356 357 return(0); 358 } 359 360 /* 361 ** BpfWrite -- Write packet to BPF device. 362 ** 363 ** Parameters: 364 ** rconn - packet to send. 365 ** 366 ** Returns: 367 ** True if write succeeded, False otherwise. 368 ** 369 ** Side Effects: 370 ** None. 371 */ 372 int 373 BpfWrite(rconn) 374 RMPCONN *rconn; 375 { 376 if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) { 377 syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn)); 378 return(0); 379 } 380 381 return(1); 382 } 383 384 /* 385 ** BpfClose -- Close a BPF device. 386 ** 387 ** Parameters: 388 ** None. 389 ** 390 ** Returns: 391 ** Nothing. 392 ** 393 ** Side Effects: 394 ** None. 395 */ 396 void 397 BpfClose() 398 { 399 struct ifreq ifr; 400 401 if (BpfPkt != NULL) { 402 free((char *)BpfPkt); 403 BpfPkt = NULL; 404 } 405 406 if (BpfFd == -1) 407 return; 408 409 #ifdef MSG_EOR 410 ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2; 411 #endif 412 ifr.ifr_addr.sa_family = AF_UNSPEC; 413 memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0], RMP_ADDRLEN); 414 if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0) 415 (void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0); 416 417 (void) close(BpfFd); 418 BpfFd = -1; 419 } 420