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