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