bpf.c revision 119137
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.4 (Berkeley) 1/9/95 39 * 40 * $FreeBSD: head/sys/net/bpf.c 119137 2003-08-19 17:51:11Z sam $ 41 */ 42 43#include "opt_bpf.h" 44#include "opt_mac.h" 45#include "opt_netgraph.h" 46 47#include <sys/types.h> 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/conf.h> 51#include <sys/mac.h> 52#include <sys/malloc.h> 53#include <sys/mbuf.h> 54#include <sys/time.h> 55#include <sys/proc.h> 56#include <sys/signalvar.h> 57#include <sys/filio.h> 58#include <sys/sockio.h> 59#include <sys/ttycom.h> 60#include <sys/filedesc.h> 61 62#include <sys/event.h> 63#include <sys/file.h> 64#include <sys/poll.h> 65#include <sys/proc.h> 66 67#include <sys/socket.h> 68#include <sys/vnode.h> 69 70#include <net/if.h> 71#include <net/bpf.h> 72#include <net/bpfdesc.h> 73 74#include <netinet/in.h> 75#include <netinet/if_ether.h> 76#include <sys/kernel.h> 77#include <sys/sysctl.h> 78 79static MALLOC_DEFINE(M_BPF, "BPF", "BPF data"); 80 81#if defined(DEV_BPF) || defined(NETGRAPH_BPF) 82 83#define PRINET 26 /* interruptible */ 84 85/* 86 * The default read buffer size is patchable. 87 */ 88static int bpf_bufsize = 4096; 89SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW, 90 &bpf_bufsize, 0, ""); 91static int bpf_maxbufsize = BPF_MAXBUFSIZE; 92SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW, 93 &bpf_maxbufsize, 0, ""); 94 95/* 96 * bpf_iflist is the list of interfaces; each corresponds to an ifnet 97 */ 98static struct bpf_if *bpf_iflist; 99static struct mtx bpf_mtx; /* bpf global lock */ 100 101static int bpf_allocbufs(struct bpf_d *); 102static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp); 103static void bpf_detachd(struct bpf_d *d); 104static void bpf_freed(struct bpf_d *); 105static void bpf_mcopy(const void *, void *, size_t); 106static int bpf_movein(struct uio *, int, 107 struct mbuf **, struct sockaddr *, int *); 108static int bpf_setif(struct bpf_d *, struct ifreq *); 109static void bpf_timed_out(void *); 110static __inline void 111 bpf_wakeup(struct bpf_d *); 112static void catchpacket(struct bpf_d *, u_char *, u_int, 113 u_int, void (*)(const void *, void *, size_t)); 114static void reset_d(struct bpf_d *); 115static int bpf_setf(struct bpf_d *, struct bpf_program *); 116static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); 117static int bpf_setdlt(struct bpf_d *, u_int); 118static void filt_bpfdetach(struct knote *); 119static int filt_bpfread(struct knote *, long); 120 121static d_open_t bpfopen; 122static d_close_t bpfclose; 123static d_read_t bpfread; 124static d_write_t bpfwrite; 125static d_ioctl_t bpfioctl; 126static d_poll_t bpfpoll; 127static d_kqfilter_t bpfkqfilter; 128 129#define CDEV_MAJOR 23 130static struct cdevsw bpf_cdevsw = { 131 .d_open = bpfopen, 132 .d_close = bpfclose, 133 .d_read = bpfread, 134 .d_write = bpfwrite, 135 .d_ioctl = bpfioctl, 136 .d_poll = bpfpoll, 137 .d_name = "bpf", 138 .d_maj = CDEV_MAJOR, 139 .d_kqfilter = bpfkqfilter, 140}; 141 142static struct filterops bpfread_filtops = 143 { 1, NULL, filt_bpfdetach, filt_bpfread }; 144 145static int 146bpf_movein(uio, linktype, mp, sockp, datlen) 147 struct uio *uio; 148 int linktype, *datlen; 149 struct mbuf **mp; 150 struct sockaddr *sockp; 151{ 152 struct mbuf *m; 153 int error; 154 int len; 155 int hlen; 156 157 /* 158 * Build a sockaddr based on the data link layer type. 159 * We do this at this level because the ethernet header 160 * is copied directly into the data field of the sockaddr. 161 * In the case of SLIP, there is no header and the packet 162 * is forwarded as is. 163 * Also, we are careful to leave room at the front of the mbuf 164 * for the link level header. 165 */ 166 switch (linktype) { 167 168 case DLT_SLIP: 169 sockp->sa_family = AF_INET; 170 hlen = 0; 171 break; 172 173 case DLT_EN10MB: 174 sockp->sa_family = AF_UNSPEC; 175 /* XXX Would MAXLINKHDR be better? */ 176 hlen = ETHER_HDR_LEN; 177 break; 178 179 case DLT_FDDI: 180 sockp->sa_family = AF_IMPLINK; 181 hlen = 0; 182 break; 183 184 case DLT_RAW: 185 case DLT_NULL: 186 sockp->sa_family = AF_UNSPEC; 187 hlen = 0; 188 break; 189 190 case DLT_ATM_RFC1483: 191 /* 192 * en atm driver requires 4-byte atm pseudo header. 193 * though it isn't standard, vpi:vci needs to be 194 * specified anyway. 195 */ 196 sockp->sa_family = AF_UNSPEC; 197 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ 198 break; 199 200 case DLT_PPP: 201 sockp->sa_family = AF_UNSPEC; 202 hlen = 4; /* This should match PPP_HDRLEN */ 203 break; 204 205 default: 206 return (EIO); 207 } 208 209 len = uio->uio_resid; 210 *datlen = len - hlen; 211 if ((unsigned)len > MCLBYTES) 212 return (EIO); 213 214 if (len > MHLEN) { 215 m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR); 216 } else { 217 MGETHDR(m, M_TRYWAIT, MT_DATA); 218 } 219 if (m == NULL) 220 return (ENOBUFS); 221 m->m_pkthdr.len = m->m_len = len; 222 m->m_pkthdr.rcvif = NULL; 223 *mp = m; 224 225 /* 226 * Make room for link header. 227 */ 228 if (hlen != 0) { 229 m->m_pkthdr.len -= hlen; 230 m->m_len -= hlen; 231#if BSD >= 199103 232 m->m_data += hlen; /* XXX */ 233#else 234 m->m_off += hlen; 235#endif 236 error = uiomove(sockp->sa_data, hlen, uio); 237 if (error) 238 goto bad; 239 } 240 error = uiomove(mtod(m, void *), len - hlen, uio); 241 if (!error) 242 return (0); 243bad: 244 m_freem(m); 245 return (error); 246} 247 248/* 249 * Attach file to the bpf interface, i.e. make d listen on bp. 250 */ 251static void 252bpf_attachd(d, bp) 253 struct bpf_d *d; 254 struct bpf_if *bp; 255{ 256 /* 257 * Point d at bp, and add d to the interface's list of listeners. 258 * Finally, point the driver's bpf cookie at the interface so 259 * it will divert packets to bpf. 260 */ 261 BPFIF_LOCK(bp); 262 d->bd_bif = bp; 263 d->bd_next = bp->bif_dlist; 264 bp->bif_dlist = d; 265 266 *bp->bif_driverp = bp; 267 BPFIF_UNLOCK(bp); 268} 269 270/* 271 * Detach a file from its interface. 272 */ 273static void 274bpf_detachd(d) 275 struct bpf_d *d; 276{ 277 int error; 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 error = ifpromisc(bp->bif_ifp, 0); 289 if (error != 0 && error != ENXIO) { 290 /* 291 * ENXIO can happen if a pccard is unplugged 292 * Something is really wrong if we were able to put 293 * the driver into promiscuous mode, but can't 294 * take it out. 295 */ 296 if_printf(bp->bif_ifp, 297 "bpf_detach: ifpromisc failed (%d)\n", error); 298 } 299 } 300 /* Remove d from the interface's descriptor list. */ 301 BPFIF_LOCK(bp); 302 p = &bp->bif_dlist; 303 while (*p != d) { 304 p = &(*p)->bd_next; 305 if (*p == 0) 306 panic("bpf_detachd: descriptor not in list"); 307 } 308 *p = (*p)->bd_next; 309 if (bp->bif_dlist == 0) 310 /* 311 * Let the driver know that there are no more listeners. 312 */ 313 *d->bd_bif->bif_driverp = 0; 314 BPFIF_UNLOCK(bp); 315 d->bd_bif = 0; 316} 317 318/* 319 * Open ethernet device. Returns ENXIO for illegal minor device number, 320 * EBUSY if file is open by another process. 321 */ 322/* ARGSUSED */ 323static int 324bpfopen(dev, flags, fmt, td) 325 dev_t dev; 326 int flags; 327 int fmt; 328 struct thread *td; 329{ 330 struct bpf_d *d; 331 332 mtx_lock(&bpf_mtx); 333 d = dev->si_drv1; 334 /* 335 * Each minor can be opened by only one process. If the requested 336 * minor is in use, return EBUSY. 337 */ 338 if (d) { 339 mtx_unlock(&bpf_mtx); 340 return (EBUSY); 341 } 342 dev->si_drv1 = (struct bpf_d *)~0; /* mark device in use */ 343 mtx_unlock(&bpf_mtx); 344 345 if ((dev->si_flags & SI_NAMED) == 0) 346 make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600, 347 "bpf%d", dev2unit(dev)); 348 MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO); 349 dev->si_drv1 = d; 350 d->bd_bufsize = bpf_bufsize; 351 d->bd_sig = SIGIO; 352 d->bd_seesent = 1; 353#ifdef MAC 354 mac_init_bpfdesc(d); 355 mac_create_bpfdesc(td->td_ucred, d); 356#endif 357 mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF); 358 callout_init(&d->bd_callout, CALLOUT_MPSAFE); 359 360 return (0); 361} 362 363/* 364 * Close the descriptor by detaching it from its interface, 365 * deallocating its buffers, and marking it free. 366 */ 367/* ARGSUSED */ 368static int 369bpfclose(dev, flags, fmt, td) 370 dev_t dev; 371 int flags; 372 int fmt; 373 struct thread *td; 374{ 375 struct bpf_d *d = dev->si_drv1; 376 377 BPFD_LOCK(d); 378 if (d->bd_state == BPF_WAITING) 379 callout_stop(&d->bd_callout); 380 d->bd_state = BPF_IDLE; 381 BPFD_UNLOCK(d); 382 funsetown(&d->bd_sigio); 383 mtx_lock(&bpf_mtx); 384 if (d->bd_bif) 385 bpf_detachd(d); 386 mtx_unlock(&bpf_mtx); 387#ifdef MAC 388 mac_destroy_bpfdesc(d); 389#endif /* MAC */ 390 bpf_freed(d); 391 dev->si_drv1 = 0; 392 free(d, M_BPF); 393 394 return (0); 395} 396 397 398/* 399 * Rotate the packet buffers in descriptor d. Move the store buffer 400 * into the hold slot, and the free buffer into the store slot. 401 * Zero the length of the new store buffer. 402 */ 403#define ROTATE_BUFFERS(d) \ 404 (d)->bd_hbuf = (d)->bd_sbuf; \ 405 (d)->bd_hlen = (d)->bd_slen; \ 406 (d)->bd_sbuf = (d)->bd_fbuf; \ 407 (d)->bd_slen = 0; \ 408 (d)->bd_fbuf = 0; 409/* 410 * bpfread - read next chunk of packets from buffers 411 */ 412static int 413bpfread(dev, uio, ioflag) 414 dev_t dev; 415 struct uio *uio; 416 int ioflag; 417{ 418 struct bpf_d *d = dev->si_drv1; 419 int timed_out; 420 int error; 421 422 /* 423 * Restrict application to use a buffer the same size as 424 * as kernel buffers. 425 */ 426 if (uio->uio_resid != d->bd_bufsize) 427 return (EINVAL); 428 429 BPFD_LOCK(d); 430 if (d->bd_state == BPF_WAITING) 431 callout_stop(&d->bd_callout); 432 timed_out = (d->bd_state == BPF_TIMED_OUT); 433 d->bd_state = BPF_IDLE; 434 /* 435 * If the hold buffer is empty, then do a timed sleep, which 436 * ends when the timeout expires or when enough packets 437 * have arrived to fill the store buffer. 438 */ 439 while (d->bd_hbuf == 0) { 440 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) { 441 /* 442 * A packet(s) either arrived since the previous 443 * read or arrived while we were asleep. 444 * Rotate the buffers and return what's here. 445 */ 446 ROTATE_BUFFERS(d); 447 break; 448 } 449 450 /* 451 * No data is available, check to see if the bpf device 452 * is still pointed at a real interface. If not, return 453 * ENXIO so that the userland process knows to rebind 454 * it before using it again. 455 */ 456 if (d->bd_bif == NULL) { 457 BPFD_UNLOCK(d); 458 return (ENXIO); 459 } 460 461 if (ioflag & IO_NDELAY) { 462 BPFD_UNLOCK(d); 463 return (EWOULDBLOCK); 464 } 465 error = msleep(d, &d->bd_mtx, PRINET|PCATCH, 466 "bpf", d->bd_rtout); 467 if (error == EINTR || error == ERESTART) { 468 BPFD_UNLOCK(d); 469 return (error); 470 } 471 if (error == EWOULDBLOCK) { 472 /* 473 * On a timeout, return what's in the buffer, 474 * which may be nothing. If there is something 475 * in the store buffer, we can rotate the buffers. 476 */ 477 if (d->bd_hbuf) 478 /* 479 * We filled up the buffer in between 480 * getting the timeout and arriving 481 * here, so we don't need to rotate. 482 */ 483 break; 484 485 if (d->bd_slen == 0) { 486 BPFD_UNLOCK(d); 487 return (0); 488 } 489 ROTATE_BUFFERS(d); 490 break; 491 } 492 } 493 /* 494 * At this point, we know we have something in the hold slot. 495 */ 496 BPFD_UNLOCK(d); 497 498 /* 499 * Move data from hold buffer into user space. 500 * We know the entire buffer is transferred since 501 * we checked above that the read buffer is bpf_bufsize bytes. 502 */ 503 error = uiomove(d->bd_hbuf, d->bd_hlen, uio); 504 505 BPFD_LOCK(d); 506 d->bd_fbuf = d->bd_hbuf; 507 d->bd_hbuf = 0; 508 d->bd_hlen = 0; 509 BPFD_UNLOCK(d); 510 511 return (error); 512} 513 514 515/* 516 * If there are processes sleeping on this descriptor, wake them up. 517 */ 518static __inline void 519bpf_wakeup(d) 520 struct bpf_d *d; 521{ 522 if (d->bd_state == BPF_WAITING) { 523 callout_stop(&d->bd_callout); 524 d->bd_state = BPF_IDLE; 525 } 526 wakeup(d); 527 if (d->bd_async && d->bd_sig && d->bd_sigio) 528 pgsigio(&d->bd_sigio, d->bd_sig, 0); 529 530 selwakeup(&d->bd_sel); 531 KNOTE(&d->bd_sel.si_note, 0); 532} 533 534static void 535bpf_timed_out(arg) 536 void *arg; 537{ 538 struct bpf_d *d = (struct bpf_d *)arg; 539 540 BPFD_LOCK(d); 541 if (d->bd_state == BPF_WAITING) { 542 d->bd_state = BPF_TIMED_OUT; 543 if (d->bd_slen != 0) 544 bpf_wakeup(d); 545 } 546 BPFD_UNLOCK(d); 547} 548 549static int 550bpfwrite(dev, uio, ioflag) 551 dev_t dev; 552 struct uio *uio; 553 int ioflag; 554{ 555 struct bpf_d *d = dev->si_drv1; 556 struct ifnet *ifp; 557 struct mbuf *m; 558 int error; 559 static struct sockaddr dst; 560 int datlen; 561 562 if (d->bd_bif == 0) 563 return (ENXIO); 564 565 ifp = d->bd_bif->bif_ifp; 566 567 if (uio->uio_resid == 0) 568 return (0); 569 570 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen); 571 if (error) 572 return (error); 573 574 if (datlen > ifp->if_mtu) 575 return (EMSGSIZE); 576 577 if (d->bd_hdrcmplt) 578 dst.sa_family = pseudo_AF_HDRCMPLT; 579 580 mtx_lock(&Giant); 581#ifdef MAC 582 mac_create_mbuf_from_bpfdesc(d, m); 583#endif 584 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0); 585 mtx_unlock(&Giant); 586 /* 587 * The driver frees the mbuf. 588 */ 589 return (error); 590} 591 592/* 593 * Reset a descriptor by flushing its packet buffer and clearing the 594 * receive and drop counts. 595 */ 596static void 597reset_d(d) 598 struct bpf_d *d; 599{ 600 601 mtx_assert(&d->bd_mtx, MA_OWNED); 602 if (d->bd_hbuf) { 603 /* Free the hold buffer. */ 604 d->bd_fbuf = d->bd_hbuf; 605 d->bd_hbuf = 0; 606 } 607 d->bd_slen = 0; 608 d->bd_hlen = 0; 609 d->bd_rcount = 0; 610 d->bd_dcount = 0; 611} 612 613/* 614 * FIONREAD Check for read packet available. 615 * SIOCGIFADDR Get interface address - convenient hook to driver. 616 * BIOCGBLEN Get buffer len [for read()]. 617 * BIOCSETF Set ethernet read filter. 618 * BIOCFLUSH Flush read packet buffer. 619 * BIOCPROMISC Put interface into promiscuous mode. 620 * BIOCGDLT Get link layer type. 621 * BIOCGETIF Get interface name. 622 * BIOCSETIF Set interface. 623 * BIOCSRTIMEOUT Set read timeout. 624 * BIOCGRTIMEOUT Get read timeout. 625 * BIOCGSTATS Get packet stats. 626 * BIOCIMMEDIATE Set immediate mode. 627 * BIOCVERSION Get filter language version. 628 * BIOCGHDRCMPLT Get "header already complete" flag 629 * BIOCSHDRCMPLT Set "header already complete" flag 630 * BIOCGSEESENT Get "see packets sent" flag 631 * BIOCSSEESENT Set "see packets sent" flag 632 */ 633/* ARGSUSED */ 634static int 635bpfioctl(dev, cmd, addr, flags, td) 636 dev_t dev; 637 u_long cmd; 638 caddr_t addr; 639 int flags; 640 struct thread *td; 641{ 642 struct bpf_d *d = dev->si_drv1; 643 int error = 0; 644 645 BPFD_LOCK(d); 646 if (d->bd_state == BPF_WAITING) 647 callout_stop(&d->bd_callout); 648 d->bd_state = BPF_IDLE; 649 BPFD_UNLOCK(d); 650 651 switch (cmd) { 652 653 default: 654 error = EINVAL; 655 break; 656 657 /* 658 * Check for read packet available. 659 */ 660 case FIONREAD: 661 { 662 int n; 663 664 BPFD_LOCK(d); 665 n = d->bd_slen; 666 if (d->bd_hbuf) 667 n += d->bd_hlen; 668 BPFD_UNLOCK(d); 669 670 *(int *)addr = n; 671 break; 672 } 673 674 case SIOCGIFADDR: 675 { 676 struct ifnet *ifp; 677 678 if (d->bd_bif == 0) 679 error = EINVAL; 680 else { 681 ifp = d->bd_bif->bif_ifp; 682 error = (*ifp->if_ioctl)(ifp, cmd, addr); 683 } 684 break; 685 } 686 687 /* 688 * Get buffer len [for read()]. 689 */ 690 case BIOCGBLEN: 691 *(u_int *)addr = d->bd_bufsize; 692 break; 693 694 /* 695 * Set buffer length. 696 */ 697 case BIOCSBLEN: 698 if (d->bd_bif != 0) 699 error = EINVAL; 700 else { 701 u_int size = *(u_int *)addr; 702 703 if (size > bpf_maxbufsize) 704 *(u_int *)addr = size = bpf_maxbufsize; 705 else if (size < BPF_MINBUFSIZE) 706 *(u_int *)addr = size = BPF_MINBUFSIZE; 707 d->bd_bufsize = size; 708 } 709 break; 710 711 /* 712 * Set link layer read filter. 713 */ 714 case BIOCSETF: 715 error = bpf_setf(d, (struct bpf_program *)addr); 716 break; 717 718 /* 719 * Flush read packet buffer. 720 */ 721 case BIOCFLUSH: 722 BPFD_LOCK(d); 723 reset_d(d); 724 BPFD_UNLOCK(d); 725 break; 726 727 /* 728 * Put interface into promiscuous mode. 729 */ 730 case BIOCPROMISC: 731 if (d->bd_bif == 0) { 732 /* 733 * No interface attached yet. 734 */ 735 error = EINVAL; 736 break; 737 } 738 if (d->bd_promisc == 0) { 739 mtx_lock(&Giant); 740 error = ifpromisc(d->bd_bif->bif_ifp, 1); 741 mtx_unlock(&Giant); 742 if (error == 0) 743 d->bd_promisc = 1; 744 } 745 break; 746 747 /* 748 * Get current data link type. 749 */ 750 case BIOCGDLT: 751 if (d->bd_bif == 0) 752 error = EINVAL; 753 else 754 *(u_int *)addr = d->bd_bif->bif_dlt; 755 break; 756 757 /* 758 * Get a list of supported data link types. 759 */ 760 case BIOCGDLTLIST: 761 if (d->bd_bif == 0) 762 error = EINVAL; 763 else 764 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr); 765 break; 766 767 /* 768 * Set data link type. 769 */ 770 case BIOCSDLT: 771 if (d->bd_bif == 0) 772 error = EINVAL; 773 else 774 error = bpf_setdlt(d, *(u_int *)addr); 775 break; 776 777 /* 778 * Get interface name. 779 */ 780 case BIOCGETIF: 781 if (d->bd_bif == 0) 782 error = EINVAL; 783 else { 784 struct ifnet *const ifp = d->bd_bif->bif_ifp; 785 struct ifreq *const ifr = (struct ifreq *)addr; 786 787 snprintf(ifr->ifr_name, sizeof(ifr->ifr_name), 788 "%s%d", ifp->if_name, ifp->if_unit); 789 } 790 break; 791 792 /* 793 * Set interface. 794 */ 795 case BIOCSETIF: 796 error = bpf_setif(d, (struct ifreq *)addr); 797 break; 798 799 /* 800 * Set read timeout. 801 */ 802 case BIOCSRTIMEOUT: 803 { 804 struct timeval *tv = (struct timeval *)addr; 805 806 /* 807 * Subtract 1 tick from tvtohz() since this isn't 808 * a one-shot timer. 809 */ 810 if ((error = itimerfix(tv)) == 0) 811 d->bd_rtout = tvtohz(tv) - 1; 812 break; 813 } 814 815 /* 816 * Get read timeout. 817 */ 818 case BIOCGRTIMEOUT: 819 { 820 struct timeval *tv = (struct timeval *)addr; 821 822 tv->tv_sec = d->bd_rtout / hz; 823 tv->tv_usec = (d->bd_rtout % hz) * tick; 824 break; 825 } 826 827 /* 828 * Get packet stats. 829 */ 830 case BIOCGSTATS: 831 { 832 struct bpf_stat *bs = (struct bpf_stat *)addr; 833 834 bs->bs_recv = d->bd_rcount; 835 bs->bs_drop = d->bd_dcount; 836 break; 837 } 838 839 /* 840 * Set immediate mode. 841 */ 842 case BIOCIMMEDIATE: 843 d->bd_immediate = *(u_int *)addr; 844 break; 845 846 case BIOCVERSION: 847 { 848 struct bpf_version *bv = (struct bpf_version *)addr; 849 850 bv->bv_major = BPF_MAJOR_VERSION; 851 bv->bv_minor = BPF_MINOR_VERSION; 852 break; 853 } 854 855 /* 856 * Get "header already complete" flag 857 */ 858 case BIOCGHDRCMPLT: 859 *(u_int *)addr = d->bd_hdrcmplt; 860 break; 861 862 /* 863 * Set "header already complete" flag 864 */ 865 case BIOCSHDRCMPLT: 866 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; 867 break; 868 869 /* 870 * Get "see sent packets" flag 871 */ 872 case BIOCGSEESENT: 873 *(u_int *)addr = d->bd_seesent; 874 break; 875 876 /* 877 * Set "see sent packets" flag 878 */ 879 case BIOCSSEESENT: 880 d->bd_seesent = *(u_int *)addr; 881 break; 882 883 case FIONBIO: /* Non-blocking I/O */ 884 break; 885 886 case FIOASYNC: /* Send signal on receive packets */ 887 d->bd_async = *(int *)addr; 888 break; 889 890 case FIOSETOWN: 891 error = fsetown(*(int *)addr, &d->bd_sigio); 892 break; 893 894 case FIOGETOWN: 895 *(int *)addr = fgetown(&d->bd_sigio); 896 break; 897 898 /* This is deprecated, FIOSETOWN should be used instead. */ 899 case TIOCSPGRP: 900 error = fsetown(-(*(int *)addr), &d->bd_sigio); 901 break; 902 903 /* This is deprecated, FIOGETOWN should be used instead. */ 904 case TIOCGPGRP: 905 *(int *)addr = -fgetown(&d->bd_sigio); 906 break; 907 908 case BIOCSRSIG: /* Set receive signal */ 909 { 910 u_int sig; 911 912 sig = *(u_int *)addr; 913 914 if (sig >= NSIG) 915 error = EINVAL; 916 else 917 d->bd_sig = sig; 918 break; 919 } 920 case BIOCGRSIG: 921 *(u_int *)addr = d->bd_sig; 922 break; 923 } 924 return (error); 925} 926 927/* 928 * Set d's packet filter program to fp. If this file already has a filter, 929 * free it and replace it. Returns EINVAL for bogus requests. 930 */ 931static int 932bpf_setf(d, fp) 933 struct bpf_d *d; 934 struct bpf_program *fp; 935{ 936 struct bpf_insn *fcode, *old; 937 u_int flen, size; 938 939 old = d->bd_filter; 940 if (fp->bf_insns == 0) { 941 if (fp->bf_len != 0) 942 return (EINVAL); 943 BPFD_LOCK(d); 944 d->bd_filter = 0; 945 reset_d(d); 946 BPFD_UNLOCK(d); 947 if (old != 0) 948 free((caddr_t)old, M_BPF); 949 return (0); 950 } 951 flen = fp->bf_len; 952 if (flen > BPF_MAXINSNS) 953 return (EINVAL); 954 955 size = flen * sizeof(*fp->bf_insns); 956 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK); 957 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && 958 bpf_validate(fcode, (int)flen)) { 959 BPFD_LOCK(d); 960 d->bd_filter = fcode; 961 reset_d(d); 962 BPFD_UNLOCK(d); 963 if (old != 0) 964 free((caddr_t)old, M_BPF); 965 966 return (0); 967 } 968 free((caddr_t)fcode, M_BPF); 969 return (EINVAL); 970} 971 972/* 973 * Detach a file from its current interface (if attached at all) and attach 974 * to the interface indicated by the name stored in ifr. 975 * Return an errno or 0. 976 */ 977static int 978bpf_setif(d, ifr) 979 struct bpf_d *d; 980 struct ifreq *ifr; 981{ 982 struct bpf_if *bp; 983 int error; 984 struct ifnet *theywant; 985 986 theywant = ifunit(ifr->ifr_name); 987 if (theywant == 0) 988 return ENXIO; 989 990 /* 991 * Look through attached interfaces for the named one. 992 */ 993 mtx_lock(&bpf_mtx); 994 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) { 995 struct ifnet *ifp = bp->bif_ifp; 996 997 if (ifp == 0 || ifp != theywant) 998 continue; 999 /* skip additional entry */ 1000 if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf) 1001 continue; 1002 1003 mtx_unlock(&bpf_mtx); 1004 /* 1005 * We found the requested interface. 1006 * If it's not up, return an error. 1007 * Allocate the packet buffers if we need to. 1008 * If we're already attached to requested interface, 1009 * just flush the buffer. 1010 */ 1011 if ((ifp->if_flags & IFF_UP) == 0) 1012 return (ENETDOWN); 1013 1014 if (d->bd_sbuf == 0) { 1015 error = bpf_allocbufs(d); 1016 if (error != 0) 1017 return (error); 1018 } 1019 if (bp != d->bd_bif) { 1020 if (d->bd_bif) 1021 /* 1022 * Detach if attached to something else. 1023 */ 1024 bpf_detachd(d); 1025 1026 bpf_attachd(d, bp); 1027 } 1028 BPFD_LOCK(d); 1029 reset_d(d); 1030 BPFD_UNLOCK(d); 1031 return (0); 1032 } 1033 mtx_unlock(&bpf_mtx); 1034 /* Not found. */ 1035 return (ENXIO); 1036} 1037 1038/* 1039 * Support for select() and poll() system calls 1040 * 1041 * Return true iff the specific operation will not block indefinitely. 1042 * Otherwise, return false but make a note that a selwakeup() must be done. 1043 */ 1044static int 1045bpfpoll(dev, events, td) 1046 dev_t dev; 1047 int events; 1048 struct thread *td; 1049{ 1050 struct bpf_d *d; 1051 int revents; 1052 1053 d = dev->si_drv1; 1054 if (d->bd_bif == NULL) 1055 return (ENXIO); 1056 1057 revents = events & (POLLOUT | POLLWRNORM); 1058 BPFD_LOCK(d); 1059 if (events & (POLLIN | POLLRDNORM)) { 1060 if (bpf_ready(d)) 1061 revents |= events & (POLLIN | POLLRDNORM); 1062 else { 1063 selrecord(td, &d->bd_sel); 1064 /* Start the read timeout if necessary. */ 1065 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1066 callout_reset(&d->bd_callout, d->bd_rtout, 1067 bpf_timed_out, d); 1068 d->bd_state = BPF_WAITING; 1069 } 1070 } 1071 } 1072 BPFD_UNLOCK(d); 1073 return (revents); 1074} 1075 1076/* 1077 * Support for kevent() system call. Register EVFILT_READ filters and 1078 * reject all others. 1079 */ 1080int 1081bpfkqfilter(dev, kn) 1082 dev_t dev; 1083 struct knote *kn; 1084{ 1085 struct bpf_d *d = (struct bpf_d *)dev->si_drv1; 1086 1087 if (kn->kn_filter != EVFILT_READ) 1088 return (1); 1089 1090 kn->kn_fop = &bpfread_filtops; 1091 kn->kn_hook = d; 1092 BPFD_LOCK(d); 1093 SLIST_INSERT_HEAD(&d->bd_sel.si_note, kn, kn_selnext); 1094 BPFD_UNLOCK(d); 1095 1096 return (0); 1097} 1098 1099static void 1100filt_bpfdetach(kn) 1101 struct knote *kn; 1102{ 1103 struct bpf_d *d = (struct bpf_d *)kn->kn_hook; 1104 1105 BPFD_LOCK(d); 1106 SLIST_REMOVE(&d->bd_sel.si_note, kn, knote, kn_selnext); 1107 BPFD_UNLOCK(d); 1108} 1109 1110static int 1111filt_bpfread(kn, hint) 1112 struct knote *kn; 1113 long hint; 1114{ 1115 struct bpf_d *d = (struct bpf_d *)kn->kn_hook; 1116 int ready; 1117 1118 BPFD_LOCK(d); 1119 ready = bpf_ready(d); 1120 if (ready) { 1121 kn->kn_data = d->bd_slen; 1122 if (d->bd_hbuf) 1123 kn->kn_data += d->bd_hlen; 1124 } 1125 else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1126 callout_reset(&d->bd_callout, d->bd_rtout, 1127 bpf_timed_out, d); 1128 d->bd_state = BPF_WAITING; 1129 } 1130 BPFD_UNLOCK(d); 1131 1132 return (ready); 1133} 1134 1135/* 1136 * Incoming linkage from device drivers. Process the packet pkt, of length 1137 * pktlen, which is stored in a contiguous buffer. The packet is parsed 1138 * by each process' filter, and if accepted, stashed into the corresponding 1139 * buffer. 1140 */ 1141void 1142bpf_tap(bp, pkt, pktlen) 1143 struct bpf_if *bp; 1144 u_char *pkt; 1145 u_int pktlen; 1146{ 1147 struct bpf_d *d; 1148 u_int slen; 1149 1150 BPFIF_LOCK(bp); 1151 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 1152 BPFD_LOCK(d); 1153 ++d->bd_rcount; 1154 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen); 1155 if (slen != 0) { 1156#ifdef MAC 1157 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0) 1158#endif 1159 catchpacket(d, pkt, pktlen, slen, bcopy); 1160 } 1161 BPFD_UNLOCK(d); 1162 } 1163 BPFIF_UNLOCK(bp); 1164} 1165 1166/* 1167 * Copy data from an mbuf chain into a buffer. This code is derived 1168 * from m_copydata in sys/uipc_mbuf.c. 1169 */ 1170static void 1171bpf_mcopy(src_arg, dst_arg, len) 1172 const void *src_arg; 1173 void *dst_arg; 1174 size_t len; 1175{ 1176 const struct mbuf *m; 1177 u_int count; 1178 u_char *dst; 1179 1180 m = src_arg; 1181 dst = dst_arg; 1182 while (len > 0) { 1183 if (m == 0) 1184 panic("bpf_mcopy"); 1185 count = min(m->m_len, len); 1186 bcopy(mtod(m, void *), dst, count); 1187 m = m->m_next; 1188 dst += count; 1189 len -= count; 1190 } 1191} 1192 1193/* 1194 * Incoming linkage from device drivers, when packet is in an mbuf chain. 1195 */ 1196void 1197bpf_mtap(bp, m) 1198 struct bpf_if *bp; 1199 struct mbuf *m; 1200{ 1201 struct bpf_d *d; 1202 u_int pktlen, slen; 1203 1204 pktlen = m_length(m, NULL); 1205 if (pktlen == m->m_len) { 1206 bpf_tap(bp, mtod(m, u_char *), pktlen); 1207 return; 1208 } 1209 1210 BPFIF_LOCK(bp); 1211 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 1212 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL)) 1213 continue; 1214 BPFD_LOCK(d); 1215 ++d->bd_rcount; 1216 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0); 1217 if (slen != 0) 1218#ifdef MAC 1219 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0) 1220#endif 1221 catchpacket(d, (u_char *)m, pktlen, slen, 1222 bpf_mcopy); 1223 BPFD_UNLOCK(d); 1224 } 1225 BPFIF_UNLOCK(bp); 1226} 1227 1228/* 1229 * Move the packet data from interface memory (pkt) into the 1230 * store buffer. Return 1 if it's time to wakeup a listener (buffer full), 1231 * otherwise 0. "copy" is the routine called to do the actual data 1232 * transfer. bcopy is passed in to copy contiguous chunks, while 1233 * bpf_mcopy is passed in to copy mbuf chains. In the latter case, 1234 * pkt is really an mbuf. 1235 */ 1236static void 1237catchpacket(d, pkt, pktlen, snaplen, cpfn) 1238 struct bpf_d *d; 1239 u_char *pkt; 1240 u_int pktlen, snaplen; 1241 void (*cpfn)(const void *, void *, size_t); 1242{ 1243 struct bpf_hdr *hp; 1244 int totlen, curlen; 1245 int hdrlen = d->bd_bif->bif_hdrlen; 1246 /* 1247 * Figure out how many bytes to move. If the packet is 1248 * greater or equal to the snapshot length, transfer that 1249 * much. Otherwise, transfer the whole packet (unless 1250 * we hit the buffer size limit). 1251 */ 1252 totlen = hdrlen + min(snaplen, pktlen); 1253 if (totlen > d->bd_bufsize) 1254 totlen = d->bd_bufsize; 1255 1256 /* 1257 * Round up the end of the previous packet to the next longword. 1258 */ 1259 curlen = BPF_WORDALIGN(d->bd_slen); 1260 if (curlen + totlen > d->bd_bufsize) { 1261 /* 1262 * This packet will overflow the storage buffer. 1263 * Rotate the buffers if we can, then wakeup any 1264 * pending reads. 1265 */ 1266 if (d->bd_fbuf == 0) { 1267 /* 1268 * We haven't completed the previous read yet, 1269 * so drop the packet. 1270 */ 1271 ++d->bd_dcount; 1272 return; 1273 } 1274 ROTATE_BUFFERS(d); 1275 bpf_wakeup(d); 1276 curlen = 0; 1277 } 1278 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) 1279 /* 1280 * Immediate mode is set, or the read timeout has 1281 * already expired during a select call. A packet 1282 * arrived, so the reader should be woken up. 1283 */ 1284 bpf_wakeup(d); 1285 1286 /* 1287 * Append the bpf header. 1288 */ 1289 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); 1290 microtime(&hp->bh_tstamp); 1291 hp->bh_datalen = pktlen; 1292 hp->bh_hdrlen = hdrlen; 1293 /* 1294 * Copy the packet data into the store buffer and update its length. 1295 */ 1296 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); 1297 d->bd_slen = curlen + totlen; 1298} 1299 1300/* 1301 * Initialize all nonzero fields of a descriptor. 1302 */ 1303static int 1304bpf_allocbufs(d) 1305 struct bpf_d *d; 1306{ 1307 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK); 1308 if (d->bd_fbuf == 0) 1309 return (ENOBUFS); 1310 1311 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK); 1312 if (d->bd_sbuf == 0) { 1313 free(d->bd_fbuf, M_BPF); 1314 return (ENOBUFS); 1315 } 1316 d->bd_slen = 0; 1317 d->bd_hlen = 0; 1318 return (0); 1319} 1320 1321/* 1322 * Free buffers currently in use by a descriptor. 1323 * Called on close. 1324 */ 1325static void 1326bpf_freed(d) 1327 struct bpf_d *d; 1328{ 1329 /* 1330 * We don't need to lock out interrupts since this descriptor has 1331 * been detached from its interface and it yet hasn't been marked 1332 * free. 1333 */ 1334 if (d->bd_sbuf != 0) { 1335 free(d->bd_sbuf, M_BPF); 1336 if (d->bd_hbuf != 0) 1337 free(d->bd_hbuf, M_BPF); 1338 if (d->bd_fbuf != 0) 1339 free(d->bd_fbuf, M_BPF); 1340 } 1341 if (d->bd_filter) 1342 free((caddr_t)d->bd_filter, M_BPF); 1343 mtx_destroy(&d->bd_mtx); 1344} 1345 1346/* 1347 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the 1348 * fixed size of the link header (variable length headers not yet supported). 1349 */ 1350void 1351bpfattach(ifp, dlt, hdrlen) 1352 struct ifnet *ifp; 1353 u_int dlt, hdrlen; 1354{ 1355 1356 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); 1357} 1358 1359/* 1360 * Attach an interface to bpf. ifp is a pointer to the structure 1361 * defining the interface to be attached, dlt is the link layer type, 1362 * and hdrlen is the fixed size of the link header (variable length 1363 * headers are not yet supporrted). 1364 */ 1365void 1366bpfattach2(ifp, dlt, hdrlen, driverp) 1367 struct ifnet *ifp; 1368 u_int dlt, hdrlen; 1369 struct bpf_if **driverp; 1370{ 1371 struct bpf_if *bp; 1372 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO); 1373 if (bp == 0) 1374 panic("bpfattach"); 1375 1376 bp->bif_dlist = 0; 1377 bp->bif_driverp = driverp; 1378 bp->bif_ifp = ifp; 1379 bp->bif_dlt = dlt; 1380 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF); 1381 1382 mtx_lock(&bpf_mtx); 1383 bp->bif_next = bpf_iflist; 1384 bpf_iflist = bp; 1385 mtx_unlock(&bpf_mtx); 1386 1387 *bp->bif_driverp = 0; 1388 1389 /* 1390 * Compute the length of the bpf header. This is not necessarily 1391 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1392 * that the network layer header begins on a longword boundary (for 1393 * performance reasons and to alleviate alignment restrictions). 1394 */ 1395 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1396 1397 if (bootverbose) 1398 if_printf(ifp, "bpf attached\n"); 1399} 1400 1401/* 1402 * Detach bpf from an interface. This involves detaching each descriptor 1403 * associated with the interface, and leaving bd_bif NULL. Notify each 1404 * descriptor as it's detached so that any sleepers wake up and get 1405 * ENXIO. 1406 */ 1407void 1408bpfdetach(ifp) 1409 struct ifnet *ifp; 1410{ 1411 struct bpf_if *bp, *bp_prev; 1412 struct bpf_d *d; 1413 1414 /* Locate BPF interface information */ 1415 bp_prev = NULL; 1416 1417 mtx_lock(&bpf_mtx); 1418 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1419 if (ifp == bp->bif_ifp) 1420 break; 1421 bp_prev = bp; 1422 } 1423 1424 /* Interface wasn't attached */ 1425 if ((bp == NULL) || (bp->bif_ifp == NULL)) { 1426 mtx_unlock(&bpf_mtx); 1427 printf("bpfdetach: %s%d was not attached\n", ifp->if_name, 1428 ifp->if_unit); 1429 return; 1430 } 1431 1432 if (bp_prev) { 1433 bp_prev->bif_next = bp->bif_next; 1434 } else { 1435 bpf_iflist = bp->bif_next; 1436 } 1437 mtx_unlock(&bpf_mtx); 1438 1439 while ((d = bp->bif_dlist) != NULL) { 1440 bpf_detachd(d); 1441 BPFD_LOCK(d); 1442 bpf_wakeup(d); 1443 BPFD_UNLOCK(d); 1444 } 1445 1446 mtx_destroy(&bp->bif_mtx); 1447 free(bp, M_BPF); 1448} 1449 1450/* 1451 * Get a list of available data link type of the interface. 1452 */ 1453static int 1454bpf_getdltlist(d, bfl) 1455 struct bpf_d *d; 1456 struct bpf_dltlist *bfl; 1457{ 1458 int n, error; 1459 struct ifnet *ifp; 1460 struct bpf_if *bp; 1461 1462 ifp = d->bd_bif->bif_ifp; 1463 n = 0; 1464 error = 0; 1465 mtx_lock(&bpf_mtx); 1466 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1467 if (bp->bif_ifp != ifp) 1468 continue; 1469 if (bfl->bfl_list != NULL) { 1470 if (n >= bfl->bfl_len) { 1471 mtx_unlock(&bpf_mtx); 1472 return (ENOMEM); 1473 } 1474 error = copyout(&bp->bif_dlt, 1475 bfl->bfl_list + n, sizeof(u_int)); 1476 } 1477 n++; 1478 } 1479 mtx_unlock(&bpf_mtx); 1480 bfl->bfl_len = n; 1481 return (error); 1482} 1483 1484/* 1485 * Set the data link type of a BPF instance. 1486 */ 1487static int 1488bpf_setdlt(d, dlt) 1489 struct bpf_d *d; 1490 u_int dlt; 1491{ 1492 int error, opromisc; 1493 struct ifnet *ifp; 1494 struct bpf_if *bp; 1495 1496 if (d->bd_bif->bif_dlt == dlt) 1497 return (0); 1498 ifp = d->bd_bif->bif_ifp; 1499 mtx_lock(&bpf_mtx); 1500 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1501 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) 1502 break; 1503 } 1504 mtx_unlock(&bpf_mtx); 1505 if (bp != NULL) { 1506 BPFD_LOCK(d); 1507 opromisc = d->bd_promisc; 1508 bpf_detachd(d); 1509 bpf_attachd(d, bp); 1510 reset_d(d); 1511 BPFD_UNLOCK(d); 1512 if (opromisc) { 1513 error = ifpromisc(bp->bif_ifp, 1); 1514 if (error) 1515 if_printf(bp->bif_ifp, 1516 "bpf_setdlt: ifpromisc failed (%d)\n", 1517 error); 1518 else 1519 d->bd_promisc = 1; 1520 } 1521 } 1522 return (bp == NULL ? EINVAL : 0); 1523} 1524 1525static void bpf_drvinit(void *unused); 1526 1527static void bpf_clone(void *arg, char *name, int namelen, dev_t *dev); 1528 1529static void 1530bpf_clone(arg, name, namelen, dev) 1531 void *arg; 1532 char *name; 1533 int namelen; 1534 dev_t *dev; 1535{ 1536 int u; 1537 1538 if (*dev != NODEV) 1539 return; 1540 if (dev_stdclone(name, NULL, "bpf", &u) != 1) 1541 return; 1542 *dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600, 1543 "bpf%d", u); 1544 (*dev)->si_flags |= SI_CHEAPCLONE; 1545 return; 1546} 1547 1548static void 1549bpf_drvinit(unused) 1550 void *unused; 1551{ 1552 1553 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF); 1554 EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000); 1555} 1556 1557SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL) 1558 1559#else /* !DEV_BPF && !NETGRAPH_BPF */ 1560/* 1561 * NOP stubs to allow bpf-using drivers to load and function. 1562 * 1563 * A 'better' implementation would allow the core bpf functionality 1564 * to be loaded at runtime. 1565 */ 1566 1567void 1568bpf_tap(bp, pkt, pktlen) 1569 struct bpf_if *bp; 1570 u_char *pkt; 1571 u_int pktlen; 1572{ 1573} 1574 1575void 1576bpf_mtap(bp, m) 1577 struct bpf_if *bp; 1578 struct mbuf *m; 1579{ 1580} 1581 1582void 1583bpfattach(ifp, dlt, hdrlen) 1584 struct ifnet *ifp; 1585 u_int dlt, hdrlen; 1586{ 1587} 1588 1589void 1590bpfdetach(ifp) 1591 struct ifnet *ifp; 1592{ 1593} 1594 1595u_int 1596bpf_filter(pc, p, wirelen, buflen) 1597 const struct bpf_insn *pc; 1598 u_char *p; 1599 u_int wirelen; 1600 u_int buflen; 1601{ 1602 return -1; /* "no filter" behaviour */ 1603} 1604 1605int 1606bpf_validate(f, len) 1607 const struct bpf_insn *f; 1608 int len; 1609{ 1610 return 0; /* false */ 1611} 1612 1613#endif /* !DEV_BPF && !NETGRAPH_BPF */ 1614