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