1 /* 2 * Copyright (c) 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from the Stanford/CMU enet packet filter, 6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed 7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 8 * Berkeley Laboratory. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)bpf.c 8.2 (Berkeley) 3/28/94 39 * 40 * $Id: bpf.c,v 1.45 1998/11/11 10:04:09 truckman Exp $ 41 */ 42 43 #include "bpfilter.h" 44 45 #if NBPFILTER > 0 46 47 #ifndef __GNUC__ 48 #define inline 49 #else 50 #define inline __inline 51 #endif 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/conf.h> 56 #include <sys/malloc.h> 57 #include <sys/mbuf.h> 58 #include <sys/time.h> 59 #include <sys/proc.h> 60 #include <sys/signalvar.h> 61 #include <sys/filio.h> 62 #include <sys/sockio.h> 63 #include <sys/ttycom.h> 64 #include <sys/filedesc.h> 65 66 #if defined(sparc) && BSD < 199103 67 #include <sys/stream.h> 68 #endif 69 #include <sys/poll.h> 70 71 #include <sys/socket.h> 72 #include <sys/vnode.h> 73 74 #include <net/if.h> 75 #include <net/bpf.h> 76 #include <net/bpfdesc.h> 77 78 #include <netinet/in.h> 79 #include <netinet/if_ether.h> 80 #include <sys/kernel.h> 81 #include <sys/sysctl.h> 82 83 #include "opt_devfs.h" 84 85 #ifdef DEVFS 86 #include <sys/devfsext.h> 87 #endif /*DEVFS*/ 88 89 90 /* 91 * Older BSDs don't have kernel malloc. 92 */ 93 #if BSD < 199103 94 extern bcopy(); 95 static caddr_t bpf_alloc(); 96 #include <net/bpf_compat.h> 97 #define BPF_BUFSIZE (MCLBYTES-8) 98 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio) 99 #else 100 #define BPF_BUFSIZE 4096 101 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio) 102 #endif 103 104 #define PRINET 26 /* interruptible */ 105 106 /* 107 * The default read buffer size is patchable. 108 */ 109 static int bpf_bufsize = BPF_BUFSIZE; 110 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW, 111 &bpf_bufsize, 0, ""); 112 113 /* 114 * bpf_iflist is the list of interfaces; each corresponds to an ifnet 115 * bpf_dtab holds the descriptors, indexed by minor device # 116 */ 117 static struct bpf_if *bpf_iflist; 118 static struct bpf_d bpf_dtab[NBPFILTER]; 119 static int bpf_dtab_init; 120 121 static int bpf_allocbufs __P((struct bpf_d *)); 122 static void bpf_attachd __P((struct bpf_d *d, struct bpf_if *bp)); 123 static void bpf_detachd __P((struct bpf_d *d)); 124 static void bpf_freed __P((struct bpf_d *)); 125 static void bpf_ifname __P((struct ifnet *, struct ifreq *)); 126 static void bpf_mcopy __P((const void *, void *, size_t)); 127 static int bpf_movein __P((struct uio *, int, 128 struct mbuf **, struct sockaddr *, int *)); 129 static int bpf_setif __P((struct bpf_d *, struct ifreq *)); 130 static inline void 131 bpf_wakeup __P((struct bpf_d *)); 132 static void catchpacket __P((struct bpf_d *, u_char *, u_int, 133 u_int, void (*)(const void *, void *, size_t))); 134 static void reset_d __P((struct bpf_d *)); 135 static int bpf_setf __P((struct bpf_d *, struct bpf_program *)); 136 137 static d_open_t bpfopen; 138 static d_close_t bpfclose; 139 static d_read_t bpfread; 140 static d_write_t bpfwrite; 141 static d_ioctl_t bpfioctl; 142 static d_poll_t bpfpoll; 143 144 #define CDEV_MAJOR 23 145 static struct cdevsw bpf_cdevsw = 146 { bpfopen, bpfclose, bpfread, bpfwrite, /*23*/ 147 bpfioctl, nostop, nullreset, nodevtotty,/* bpf */ 148 bpfpoll, nommap, NULL, "bpf", NULL, -1 }; 149 150 151 static int 152 bpf_movein(uio, linktype, mp, sockp, datlen) 153 register struct uio *uio; 154 int linktype, *datlen; 155 register struct mbuf **mp; 156 register struct sockaddr *sockp; 157 { 158 struct mbuf *m; 159 int error; 160 int len; 161 int hlen; 162 163 /* 164 * Build a sockaddr based on the data link layer type. 165 * We do this at this level because the ethernet header 166 * is copied directly into the data field of the sockaddr. 167 * In the case of SLIP, there is no header and the packet 168 * is forwarded as is. 169 * Also, we are careful to leave room at the front of the mbuf 170 * for the link level header. 171 */ 172 switch (linktype) { 173 174 case DLT_SLIP: 175 sockp->sa_family = AF_INET; 176 hlen = 0; 177 break; 178 179 case DLT_EN10MB: 180 sockp->sa_family = AF_UNSPEC; 181 /* XXX Would MAXLINKHDR be better? */ 182 hlen = sizeof(struct ether_header); 183 break; 184 185 case DLT_FDDI: 186 #if defined(__FreeBSD__) || defined(__bsdi__) 187 sockp->sa_family = AF_IMPLINK; 188 hlen = 0; 189 #else 190 sockp->sa_family = AF_UNSPEC; 191 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */ 192 hlen = 24; 193 #endif 194 break; 195 196 case DLT_RAW: 197 case DLT_NULL: 198 sockp->sa_family = AF_UNSPEC; 199 hlen = 0; 200 break; 201 202 #ifdef __FreeBSD__ 203 case DLT_ATM_RFC1483: 204 /* 205 * en atm driver requires 4-byte atm pseudo header. 206 * though it isn't standard, vpi:vci needs to be 207 * specified anyway. 208 */ 209 sockp->sa_family = AF_UNSPEC; 210 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ 211 break; 212 #endif 213 214 default: 215 return (EIO); 216 } 217 218 len = uio->uio_resid; 219 *datlen = len - hlen; 220 if ((unsigned)len > MCLBYTES) 221 return (EIO); 222 223 MGETHDR(m, M_WAIT, MT_DATA); 224 if (m == 0) 225 return (ENOBUFS); 226 if (len > MHLEN) { 227 #if BSD >= 199103 228 MCLGET(m, M_WAIT); 229 if ((m->m_flags & M_EXT) == 0) { 230 #else 231 MCLGET(m); 232 if (m->m_len != MCLBYTES) { 233 #endif 234 error = ENOBUFS; 235 goto bad; 236 } 237 } 238 m->m_pkthdr.len = m->m_len = len; 239 m->m_pkthdr.rcvif = NULL; 240 *mp = m; 241 /* 242 * Make room for link header. 243 */ 244 if (hlen != 0) { 245 m->m_pkthdr.len -= hlen; 246 m->m_len -= hlen; 247 #if BSD >= 199103 248 m->m_data += hlen; /* XXX */ 249 #else 250 m->m_off += hlen; 251 #endif 252 error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio); 253 if (error) 254 goto bad; 255 } 256 error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio); 257 if (!error) 258 return (0); 259 bad: 260 m_freem(m); 261 return (error); 262 } 263 264 /* 265 * Attach file to the bpf interface, i.e. make d listen on bp. 266 * Must be called at splimp. 267 */ 268 static void 269 bpf_attachd(d, bp) 270 struct bpf_d *d; 271 struct bpf_if *bp; 272 { 273 /* 274 * Point d at bp, and add d to the interface's list of listeners. 275 * Finally, point the driver's bpf cookie at the interface so 276 * it will divert packets to bpf. 277 */ 278 d->bd_bif = bp; 279 d->bd_next = bp->bif_dlist; 280 bp->bif_dlist = d; 281 282 bp->bif_ifp->if_bpf = bp; 283 } 284 285 /* 286 * Detach a file from its interface. 287 */ 288 static void 289 bpf_detachd(d) 290 struct bpf_d *d; 291 { 292 struct bpf_d **p; 293 struct bpf_if *bp; 294 295 bp = d->bd_bif; 296 /* 297 * Check if this descriptor had requested promiscuous mode. 298 * If so, turn it off. 299 */ 300 if (d->bd_promisc) { 301 d->bd_promisc = 0; 302 if (ifpromisc(bp->bif_ifp, 0)) 303 /* 304 * Something is really wrong if we were able to put 305 * the driver into promiscuous mode, but can't 306 * take it out. 307 */ 308 panic("bpf: ifpromisc failed"); 309 } 310 /* Remove d from the interface's descriptor list. */ 311 p = &bp->bif_dlist; 312 while (*p != d) { 313 p = &(*p)->bd_next; 314 if (*p == 0) 315 panic("bpf_detachd: descriptor not in list"); 316 } 317 *p = (*p)->bd_next; 318 if (bp->bif_dlist == 0) 319 /* 320 * Let the driver know that there are no more listeners. 321 */ 322 d->bd_bif->bif_ifp->if_bpf = 0; 323 d->bd_bif = 0; 324 } 325 326 327 /* 328 * Mark a descriptor free by making it point to itself. 329 * This is probably cheaper than marking with a constant since 330 * the address should be in a register anyway. 331 */ 332 #define D_ISFREE(d) ((d) == (d)->bd_next) 333 #define D_MARKFREE(d) ((d)->bd_next = (d)) 334 #define D_MARKUSED(d) ((d)->bd_next = 0) 335 336 /* 337 * Open ethernet device. Returns ENXIO for illegal minor device number, 338 * EBUSY if file is open by another process. 339 */ 340 /* ARGSUSED */ 341 static int 342 bpfopen(dev, flags, fmt, p) 343 dev_t dev; 344 int flags; 345 int fmt; 346 struct proc *p; 347 { 348 register struct bpf_d *d; 349 350 if (minor(dev) >= NBPFILTER) 351 return (ENXIO); 352 /* 353 * Each minor can be opened by only one process. If the requested 354 * minor is in use, return EBUSY. 355 */ 356 d = &bpf_dtab[minor(dev)]; 357 if (!D_ISFREE(d)) 358 return (EBUSY); 359 360 /* Mark "free" and do most initialization. */ 361 bzero((char *)d, sizeof(*d)); 362 d->bd_bufsize = bpf_bufsize; 363 d->bd_sig = SIGIO; 364 365 return (0); 366 } 367 368 /* 369 * Close the descriptor by detaching it from its interface, 370 * deallocating its buffers, and marking it free. 371 */ 372 /* ARGSUSED */ 373 static int 374 bpfclose(dev, flags, fmt, p) 375 dev_t dev; 376 int flags; 377 int fmt; 378 struct proc *p; 379 { 380 register struct bpf_d *d = &bpf_dtab[minor(dev)]; 381 register int s; 382 383 funsetown(d->bd_sigio); 384 s = splimp(); 385 if (d->bd_bif) 386 bpf_detachd(d); 387 splx(s); 388 bpf_freed(d); 389 390 return (0); 391 } 392 393 /* 394 * Support for SunOS, which does not have tsleep. 395 */ 396 #if BSD < 199103 397 static 398 bpf_timeout(arg) 399 caddr_t arg; 400 { 401 struct bpf_d *d = (struct bpf_d *)arg; 402 d->bd_timedout = 1; 403 wakeup(arg); 404 } 405 406 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan) 407 408 int 409 bpf_sleep(d) 410 register struct bpf_d *d; 411 { 412 register int rto = d->bd_rtout; 413 register int st; 414 415 if (rto != 0) { 416 d->bd_timedout = 0; 417 timeout(bpf_timeout, (caddr_t)d, rto); 418 } 419 st = sleep((caddr_t)d, PRINET|PCATCH); 420 if (rto != 0) { 421 if (d->bd_timedout == 0) 422 untimeout(bpf_timeout, (caddr_t)d); 423 else if (st == 0) 424 return EWOULDBLOCK; 425 } 426 return (st != 0) ? EINTR : 0; 427 } 428 #else 429 #define BPF_SLEEP tsleep 430 #endif 431 432 /* 433 * Rotate the packet buffers in descriptor d. Move the store buffer 434 * into the hold slot, and the free buffer into the store slot. 435 * Zero the length of the new store buffer. 436 */ 437 #define ROTATE_BUFFERS(d) \ 438 (d)->bd_hbuf = (d)->bd_sbuf; \ 439 (d)->bd_hlen = (d)->bd_slen; \ 440 (d)->bd_sbuf = (d)->bd_fbuf; \ 441 (d)->bd_slen = 0; \ 442 (d)->bd_fbuf = 0; 443 /* 444 * bpfread - read next chunk of packets from buffers 445 */ 446 static int 447 bpfread(dev, uio, ioflag) 448 dev_t dev; 449 register struct uio *uio; 450 int ioflag; 451 { 452 register struct bpf_d *d = &bpf_dtab[minor(dev)]; 453 int error; 454 int s; 455 456 /* 457 * Restrict application to use a buffer the same size as 458 * as kernel buffers. 459 */ 460 if (uio->uio_resid != d->bd_bufsize) 461 return (EINVAL); 462 463 s = splimp(); 464 /* 465 * If the hold buffer is empty, then do a timed sleep, which 466 * ends when the timeout expires or when enough packets 467 * have arrived to fill the store buffer. 468 */ 469 while (d->bd_hbuf == 0) { 470 if (d->bd_immediate && d->bd_slen != 0) { 471 /* 472 * A packet(s) either arrived since the previous 473 * read or arrived while we were asleep. 474 * Rotate the buffers and return what's here. 475 */ 476 ROTATE_BUFFERS(d); 477 break; 478 } 479 if (ioflag & IO_NDELAY) 480 error = EWOULDBLOCK; 481 else 482 error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf", 483 d->bd_rtout); 484 if (error == EINTR || error == ERESTART) { 485 splx(s); 486 return (error); 487 } 488 if (error == EWOULDBLOCK) { 489 /* 490 * On a timeout, return what's in the buffer, 491 * which may be nothing. If there is something 492 * in the store buffer, we can rotate the buffers. 493 */ 494 if (d->bd_hbuf) 495 /* 496 * We filled up the buffer in between 497 * getting the timeout and arriving 498 * here, so we don't need to rotate. 499 */ 500 break; 501 502 if (d->bd_slen == 0) { 503 splx(s); 504 return (0); 505 } 506 ROTATE_BUFFERS(d); 507 break; 508 } 509 } 510 /* 511 * At this point, we know we have something in the hold slot. 512 */ 513 splx(s); 514 515 /* 516 * Move data from hold buffer into user space. 517 * We know the entire buffer is transferred since 518 * we checked above that the read buffer is bpf_bufsize bytes. 519 */ 520 error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio); 521 522 s = splimp(); 523 d->bd_fbuf = d->bd_hbuf; 524 d->bd_hbuf = 0; 525 d->bd_hlen = 0; 526 splx(s); 527 528 return (error); 529 } 530 531 532 /* 533 * If there are processes sleeping on this descriptor, wake them up. 534 */ 535 static inline void 536 bpf_wakeup(d) 537 register struct bpf_d *d; 538 { 539 wakeup((caddr_t)d); 540 if (d->bd_async && d->bd_sig && d->bd_sigio) 541 pgsigio(d->bd_sigio, d->bd_sig, 0); 542 543 #if BSD >= 199103 544 selwakeup(&d->bd_sel); 545 /* XXX */ 546 d->bd_sel.si_pid = 0; 547 #else 548 if (d->bd_selproc) { 549 selwakeup(d->bd_selproc, (int)d->bd_selcoll); 550 d->bd_selcoll = 0; 551 d->bd_selproc = 0; 552 } 553 #endif 554 } 555 556 static int 557 bpfwrite(dev, uio, ioflag) 558 dev_t dev; 559 struct uio *uio; 560 int ioflag; 561 { 562 register struct bpf_d *d = &bpf_dtab[minor(dev)]; 563 struct ifnet *ifp; 564 struct mbuf *m; 565 int error, s; 566 static struct sockaddr dst; 567 int datlen; 568 569 if (d->bd_bif == 0) 570 return (ENXIO); 571 572 ifp = d->bd_bif->bif_ifp; 573 574 if (uio->uio_resid == 0) 575 return (0); 576 577 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen); 578 if (error) 579 return (error); 580 581 if (datlen > ifp->if_mtu) 582 return (EMSGSIZE); 583 584 s = splnet(); 585 #if BSD >= 199103 586 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0); 587 #else 588 error = (*ifp->if_output)(ifp, m, &dst); 589 #endif 590 splx(s); 591 /* 592 * The driver frees the mbuf. 593 */ 594 return (error); 595 } 596 597 /* 598 * Reset a descriptor by flushing its packet buffer and clearing the 599 * receive and drop counts. Should be called at splimp. 600 */ 601 static void 602 reset_d(d) 603 struct bpf_d *d; 604 { 605 if (d->bd_hbuf) { 606 /* Free the hold buffer. */ 607 d->bd_fbuf = d->bd_hbuf; 608 d->bd_hbuf = 0; 609 } 610 d->bd_slen = 0; 611 d->bd_hlen = 0; 612 d->bd_rcount = 0; 613 d->bd_dcount = 0; 614 } 615 616 /* 617 * FIONREAD Check for read packet available. 618 * SIOCGIFADDR Get interface address - convenient hook to driver. 619 * BIOCGBLEN Get buffer len [for read()]. 620 * BIOCSETF Set ethernet read filter. 621 * BIOCFLUSH Flush read packet buffer. 622 * BIOCPROMISC Put interface into promiscuous mode. 623 * BIOCGDLT Get link layer type. 624 * BIOCGETIF Get interface name. 625 * BIOCSETIF Set interface. 626 * BIOCSRTIMEOUT Set read timeout. 627 * BIOCGRTIMEOUT Get read timeout. 628 * BIOCGSTATS Get packet stats. 629 * BIOCIMMEDIATE Set immediate mode. 630 * BIOCVERSION Get filter language version. 631 */ 632 /* ARGSUSED */ 633 static int 634 bpfioctl(dev, cmd, addr, flags, p) 635 dev_t dev; 636 u_long cmd; 637 caddr_t addr; 638 int flags; 639 struct proc *p; 640 { 641 register struct bpf_d *d = &bpf_dtab[minor(dev)]; 642 int s, error = 0; 643 644 switch (cmd) { 645 646 default: 647 error = EINVAL; 648 break; 649 650 /* 651 * Check for read packet available. 652 */ 653 case FIONREAD: 654 { 655 int n; 656 657 s = splimp(); 658 n = d->bd_slen; 659 if (d->bd_hbuf) 660 n += d->bd_hlen; 661 splx(s); 662 663 *(int *)addr = n; 664 break; 665 } 666 667 case SIOCGIFADDR: 668 { 669 struct ifnet *ifp; 670 671 if (d->bd_bif == 0) 672 error = EINVAL; 673 else { 674 ifp = d->bd_bif->bif_ifp; 675 error = (*ifp->if_ioctl)(ifp, cmd, addr); 676 } 677 break; 678 } 679 680 /* 681 * Get buffer len [for read()]. 682 */ 683 case BIOCGBLEN: 684 *(u_int *)addr = d->bd_bufsize; 685 break; 686 687 /* 688 * Set buffer length. 689 */ 690 case BIOCSBLEN: 691 #if BSD < 199103 692 error = EINVAL; 693 #else 694 if (d->bd_bif != 0) 695 error = EINVAL; 696 else { 697 register u_int size = *(u_int *)addr; 698 699 if (size > BPF_MAXBUFSIZE) 700 *(u_int *)addr = size = BPF_MAXBUFSIZE; 701 else if (size < BPF_MINBUFSIZE) 702 *(u_int *)addr = size = BPF_MINBUFSIZE; 703 d->bd_bufsize = size; 704 } 705 #endif 706 break; 707 708 /* 709 * Set link layer read filter. 710 */ 711 case BIOCSETF: 712 error = bpf_setf(d, (struct bpf_program *)addr); 713 break; 714 715 /* 716 * Flush read packet buffer. 717 */ 718 case BIOCFLUSH: 719 s = splimp(); 720 reset_d(d); 721 splx(s); 722 break; 723 724 /* 725 * Put interface into promiscuous mode. 726 */ 727 case BIOCPROMISC: 728 if (d->bd_bif == 0) { 729 /* 730 * No interface attached yet. 731 */ 732 error = EINVAL; 733 break; 734 } 735 s = splimp(); 736 if (d->bd_promisc == 0) { 737 error = ifpromisc(d->bd_bif->bif_ifp, 1); 738 if (error == 0) 739 d->bd_promisc = 1; 740 } 741 splx(s); 742 break; 743 744 /* 745 * Get device parameters. 746 */ 747 case BIOCGDLT: 748 if (d->bd_bif == 0) 749 error = EINVAL; 750 else 751 *(u_int *)addr = d->bd_bif->bif_dlt; 752 break; 753 754 /* 755 * Set interface name. 756 */ 757 case BIOCGETIF: 758 if (d->bd_bif == 0) 759 error = EINVAL; 760 else 761 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr); 762 break; 763 764 /* 765 * Set interface. 766 */ 767 case BIOCSETIF: 768 error = bpf_setif(d, (struct ifreq *)addr); 769 break; 770 771 /* 772 * Set read timeout. 773 */ 774 case BIOCSRTIMEOUT: 775 { 776 struct timeval *tv = (struct timeval *)addr; 777 778 /* 779 * Subtract 1 tick from tvtohz() since this isn't 780 * a one-shot timer. 781 */ 782 if ((error = itimerfix(tv)) == 0) 783 d->bd_rtout = tvtohz(tv) - 1; 784 break; 785 } 786 787 /* 788 * Get read timeout. 789 */ 790 case BIOCGRTIMEOUT: 791 { 792 struct timeval *tv = (struct timeval *)addr; 793 794 tv->tv_sec = d->bd_rtout / hz; 795 tv->tv_usec = (d->bd_rtout % hz) * tick; 796 break; 797 } 798 799 /* 800 * Get packet stats. 801 */ 802 case BIOCGSTATS: 803 { 804 struct bpf_stat *bs = (struct bpf_stat *)addr; 805 806 bs->bs_recv = d->bd_rcount; 807 bs->bs_drop = d->bd_dcount; 808 break; 809 } 810 811 /* 812 * Set immediate mode. 813 */ 814 case BIOCIMMEDIATE: 815 d->bd_immediate = *(u_int *)addr; 816 break; 817 818 case BIOCVERSION: 819 { 820 struct bpf_version *bv = (struct bpf_version *)addr; 821 822 bv->bv_major = BPF_MAJOR_VERSION; 823 bv->bv_minor = BPF_MINOR_VERSION; 824 break; 825 } 826 827 case FIONBIO: /* Non-blocking I/O */ 828 break; 829 830 case FIOASYNC: /* Send signal on receive packets */ 831 d->bd_async = *(int *)addr; 832 break; 833 834 case FIOSETOWN: 835 error = fsetown(*(int *)addr, &d->bd_sigio); 836 break; 837 838 case FIOGETOWN: 839 *(int *)addr = fgetown(d->bd_sigio); 840 break; 841 842 /* This is deprecated, FIOSETOWN should be used instead. */ 843 case TIOCSPGRP: 844 error = fsetown(-(*(int *)addr), &d->bd_sigio); 845 break; 846 847 /* This is deprecated, FIOGETOWN should be used instead. */ 848 case TIOCGPGRP: 849 *(int *)addr = -fgetown(d->bd_sigio); 850 break; 851 852 case BIOCSRSIG: /* Set receive signal */ 853 { 854 u_int sig; 855 856 sig = *(u_int *)addr; 857 858 if (sig >= NSIG) 859 error = EINVAL; 860 else 861 d->bd_sig = sig; 862 break; 863 } 864 case BIOCGRSIG: 865 *(u_int *)addr = d->bd_sig; 866 break; 867 } 868 return (error); 869 } 870 871 /* 872 * Set d's packet filter program to fp. If this file already has a filter, 873 * free it and replace it. Returns EINVAL for bogus requests. 874 */ 875 static int 876 bpf_setf(d, fp) 877 struct bpf_d *d; 878 struct bpf_program *fp; 879 { 880 struct bpf_insn *fcode, *old; 881 u_int flen, size; 882 int s; 883 884 old = d->bd_filter; 885 if (fp->bf_insns == 0) { 886 if (fp->bf_len != 0) 887 return (EINVAL); 888 s = splimp(); 889 d->bd_filter = 0; 890 reset_d(d); 891 splx(s); 892 if (old != 0) 893 free((caddr_t)old, M_DEVBUF); 894 return (0); 895 } 896 flen = fp->bf_len; 897 if (flen > BPF_MAXINSNS) 898 return (EINVAL); 899 900 size = flen * sizeof(*fp->bf_insns); 901 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK); 902 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && 903 bpf_validate(fcode, (int)flen)) { 904 s = splimp(); 905 d->bd_filter = fcode; 906 reset_d(d); 907 splx(s); 908 if (old != 0) 909 free((caddr_t)old, M_DEVBUF); 910 911 return (0); 912 } 913 free((caddr_t)fcode, M_DEVBUF); 914 return (EINVAL); 915 } 916 917 /* 918 * Detach a file from its current interface (if attached at all) and attach 919 * to the interface indicated by the name stored in ifr. 920 * Return an errno or 0. 921 */ 922 static int 923 bpf_setif(d, ifr) 924 struct bpf_d *d; 925 struct ifreq *ifr; 926 { 927 struct bpf_if *bp; 928 int s, error; 929 struct ifnet *theywant; 930 931 theywant = ifunit(ifr->ifr_name); 932 if (theywant == 0) 933 return ENXIO; 934 935 /* 936 * Look through attached interfaces for the named one. 937 */ 938 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) { 939 struct ifnet *ifp = bp->bif_ifp; 940 941 if (ifp == 0 || ifp != theywant) 942 continue; 943 /* 944 * We found the requested interface. 945 * If it's not up, return an error. 946 * Allocate the packet buffers if we need to. 947 * If we're already attached to requested interface, 948 * just flush the buffer. 949 */ 950 if ((ifp->if_flags & IFF_UP) == 0) 951 return (ENETDOWN); 952 953 if (d->bd_sbuf == 0) { 954 error = bpf_allocbufs(d); 955 if (error != 0) 956 return (error); 957 } 958 s = splimp(); 959 if (bp != d->bd_bif) { 960 if (d->bd_bif) 961 /* 962 * Detach if attached to something else. 963 */ 964 bpf_detachd(d); 965 966 bpf_attachd(d, bp); 967 } 968 reset_d(d); 969 splx(s); 970 return (0); 971 } 972 /* Not found. */ 973 return (ENXIO); 974 } 975 976 /* 977 * Convert an interface name plus unit number of an ifp to a single 978 * name which is returned in the ifr. 979 */ 980 static void 981 bpf_ifname(ifp, ifr) 982 struct ifnet *ifp; 983 struct ifreq *ifr; 984 { 985 char *s = ifp->if_name; 986 char *d = ifr->ifr_name; 987 988 while (*d++ = *s++) 989 continue; 990 d--; /* back to the null */ 991 /* XXX Assume that unit number is less than 10. */ 992 *d++ = ifp->if_unit + '0'; 993 *d = '\0'; 994 } 995 996 /* 997 * Support for select() and poll() system calls 998 * 999 * Return true iff the specific operation will not block indefinitely. 1000 * Otherwise, return false but make a note that a selwakeup() must be done. 1001 */ 1002 int 1003 bpfpoll(dev, events, p) 1004 register dev_t dev; 1005 int events; 1006 struct proc *p; 1007 { 1008 register struct bpf_d *d; 1009 register int s; 1010 int revents = 0; 1011 1012 /* 1013 * An imitation of the FIONREAD ioctl code. 1014 */ 1015 d = &bpf_dtab[minor(dev)]; 1016 1017 s = splimp(); 1018 if (events & (POLLIN | POLLRDNORM)) 1019 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0)) 1020 revents |= events & (POLLIN | POLLRDNORM); 1021 else 1022 selrecord(p, &d->bd_sel); 1023 1024 splx(s); 1025 return (revents); 1026 } 1027 1028 /* 1029 * Incoming linkage from device drivers. Process the packet pkt, of length 1030 * pktlen, which is stored in a contiguous buffer. The packet is parsed 1031 * by each process' filter, and if accepted, stashed into the corresponding 1032 * buffer. 1033 */ 1034 void 1035 bpf_tap(ifp, pkt, pktlen) 1036 struct ifnet *ifp; 1037 register u_char *pkt; 1038 register u_int pktlen; 1039 { 1040 struct bpf_if *bp; 1041 register struct bpf_d *d; 1042 register u_int slen; 1043 /* 1044 * Note that the ipl does not have to be raised at this point. 1045 * The only problem that could arise here is that if two different 1046 * interfaces shared any data. This is not the case. 1047 */ 1048 bp = ifp->if_bpf; 1049 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 1050 ++d->bd_rcount; 1051 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen); 1052 if (slen != 0) 1053 catchpacket(d, pkt, pktlen, slen, bcopy); 1054 } 1055 } 1056 1057 /* 1058 * Copy data from an mbuf chain into a buffer. This code is derived 1059 * from m_copydata in sys/uipc_mbuf.c. 1060 */ 1061 static void 1062 bpf_mcopy(src_arg, dst_arg, len) 1063 const void *src_arg; 1064 void *dst_arg; 1065 register size_t len; 1066 { 1067 register const struct mbuf *m; 1068 register u_int count; 1069 u_char *dst; 1070 1071 m = src_arg; 1072 dst = dst_arg; 1073 while (len > 0) { 1074 if (m == 0) 1075 panic("bpf_mcopy"); 1076 count = min(m->m_len, len); 1077 bcopy(mtod(m, void *), dst, count); 1078 m = m->m_next; 1079 dst += count; 1080 len -= count; 1081 } 1082 } 1083 1084 /* 1085 * Incoming linkage from device drivers, when packet is in an mbuf chain. 1086 */ 1087 void 1088 bpf_mtap(ifp, m) 1089 struct ifnet *ifp; 1090 struct mbuf *m; 1091 { 1092 struct bpf_if *bp = ifp->if_bpf; 1093 struct bpf_d *d; 1094 u_int pktlen, slen; 1095 struct mbuf *m0; 1096 1097 pktlen = 0; 1098 for (m0 = m; m0 != 0; m0 = m0->m_next) 1099 pktlen += m0->m_len; 1100 1101 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 1102 ++d->bd_rcount; 1103 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0); 1104 if (slen != 0) 1105 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy); 1106 } 1107 } 1108 1109 /* 1110 * Move the packet data from interface memory (pkt) into the 1111 * store buffer. Return 1 if it's time to wakeup a listener (buffer full), 1112 * otherwise 0. "copy" is the routine called to do the actual data 1113 * transfer. bcopy is passed in to copy contiguous chunks, while 1114 * bpf_mcopy is passed in to copy mbuf chains. In the latter case, 1115 * pkt is really an mbuf. 1116 */ 1117 static void 1118 catchpacket(d, pkt, pktlen, snaplen, cpfn) 1119 register struct bpf_d *d; 1120 register u_char *pkt; 1121 register u_int pktlen, snaplen; 1122 register void (*cpfn) __P((const void *, void *, size_t)); 1123 { 1124 register struct bpf_hdr *hp; 1125 register int totlen, curlen; 1126 register int hdrlen = d->bd_bif->bif_hdrlen; 1127 /* 1128 * Figure out how many bytes to move. If the packet is 1129 * greater or equal to the snapshot length, transfer that 1130 * much. Otherwise, transfer the whole packet (unless 1131 * we hit the buffer size limit). 1132 */ 1133 totlen = hdrlen + min(snaplen, pktlen); 1134 if (totlen > d->bd_bufsize) 1135 totlen = d->bd_bufsize; 1136 1137 /* 1138 * Round up the end of the previous packet to the next longword. 1139 */ 1140 curlen = BPF_WORDALIGN(d->bd_slen); 1141 if (curlen + totlen > d->bd_bufsize) { 1142 /* 1143 * This packet will overflow the storage buffer. 1144 * Rotate the buffers if we can, then wakeup any 1145 * pending reads. 1146 */ 1147 if (d->bd_fbuf == 0) { 1148 /* 1149 * We haven't completed the previous read yet, 1150 * so drop the packet. 1151 */ 1152 ++d->bd_dcount; 1153 return; 1154 } 1155 ROTATE_BUFFERS(d); 1156 bpf_wakeup(d); 1157 curlen = 0; 1158 } 1159 else if (d->bd_immediate) 1160 /* 1161 * Immediate mode is set. A packet arrived so any 1162 * reads should be woken up. 1163 */ 1164 bpf_wakeup(d); 1165 1166 /* 1167 * Append the bpf header. 1168 */ 1169 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); 1170 #if BSD >= 199103 1171 microtime(&hp->bh_tstamp); 1172 #elif defined(sun) 1173 uniqtime(&hp->bh_tstamp); 1174 #else 1175 hp->bh_tstamp = time; 1176 #endif 1177 hp->bh_datalen = pktlen; 1178 hp->bh_hdrlen = hdrlen; 1179 /* 1180 * Copy the packet data into the store buffer and update its length. 1181 */ 1182 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); 1183 d->bd_slen = curlen + totlen; 1184 } 1185 1186 /* 1187 * Initialize all nonzero fields of a descriptor. 1188 */ 1189 static int 1190 bpf_allocbufs(d) 1191 register struct bpf_d *d; 1192 { 1193 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK); 1194 if (d->bd_fbuf == 0) 1195 return (ENOBUFS); 1196 1197 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK); 1198 if (d->bd_sbuf == 0) { 1199 free(d->bd_fbuf, M_DEVBUF); 1200 return (ENOBUFS); 1201 } 1202 d->bd_slen = 0; 1203 d->bd_hlen = 0; 1204 return (0); 1205 } 1206 1207 /* 1208 * Free buffers currently in use by a descriptor. 1209 * Called on close. 1210 */ 1211 static void 1212 bpf_freed(d) 1213 register struct bpf_d *d; 1214 { 1215 /* 1216 * We don't need to lock out interrupts since this descriptor has 1217 * been detached from its interface and it yet hasn't been marked 1218 * free. 1219 */ 1220 if (d->bd_sbuf != 0) { 1221 free(d->bd_sbuf, M_DEVBUF); 1222 if (d->bd_hbuf != 0) 1223 free(d->bd_hbuf, M_DEVBUF); 1224 if (d->bd_fbuf != 0) 1225 free(d->bd_fbuf, M_DEVBUF); 1226 } 1227 if (d->bd_filter) 1228 free((caddr_t)d->bd_filter, M_DEVBUF); 1229 1230 D_MARKFREE(d); 1231 } 1232 1233 /* 1234 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *) 1235 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed 1236 * size of the link header (variable length headers not yet supported). 1237 */ 1238 void 1239 bpfattach(ifp, dlt, hdrlen) 1240 struct ifnet *ifp; 1241 u_int dlt, hdrlen; 1242 { 1243 struct bpf_if *bp; 1244 int i; 1245 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT); 1246 if (bp == 0) 1247 panic("bpfattach"); 1248 1249 bp->bif_dlist = 0; 1250 bp->bif_ifp = ifp; 1251 bp->bif_dlt = dlt; 1252 1253 bp->bif_next = bpf_iflist; 1254 bpf_iflist = bp; 1255 1256 bp->bif_ifp->if_bpf = 0; 1257 1258 /* 1259 * Compute the length of the bpf header. This is not necessarily 1260 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1261 * that the network layer header begins on a longword boundary (for 1262 * performance reasons and to alleviate alignment restrictions). 1263 */ 1264 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1265 1266 /* 1267 * Mark all the descriptors free if this hasn't been done. 1268 */ 1269 if (!bpf_dtab_init) { 1270 for (i = 0; i < NBPFILTER; ++i) 1271 D_MARKFREE(&bpf_dtab[i]); 1272 bpf_dtab_init = 1; 1273 } 1274 1275 if (bootverbose) 1276 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit); 1277 } 1278 1279 #ifdef DEVFS 1280 static void *bpf_devfs_token[NBPFILTER]; 1281 #endif 1282 1283 static int bpf_devsw_installed; 1284 1285 static void bpf_drvinit __P((void *unused)); 1286 static void 1287 bpf_drvinit(unused) 1288 void *unused; 1289 { 1290 dev_t dev; 1291 #ifdef DEVFS 1292 int i; 1293 #endif 1294 1295 if( ! bpf_devsw_installed ) { 1296 dev = makedev(CDEV_MAJOR, 0); 1297 cdevsw_add(&dev,&bpf_cdevsw, NULL); 1298 bpf_devsw_installed = 1; 1299 #ifdef DEVFS 1300 1301 for ( i = 0 ; i < NBPFILTER ; i++ ) { 1302 bpf_devfs_token[i] = 1303 devfs_add_devswf(&bpf_cdevsw, i, DV_CHR, 0, 0, 1304 0600, "bpf%d", i); 1305 } 1306 #endif 1307 } 1308 } 1309 1310 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL) 1311 1312 #endif 1313