1 /* $OpenBSD: bpf.c,v 1.78 2011/07/02 22:20:08 nicm Exp $ */ 2 /* $NetBSD: bpf.c,v 1.33 1997/02/21 23:59:35 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1990, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from the Stanford/CMU enet packet filter, 9 * (net/enet.c) distributed as part of 4.3BSD, and code contributed 10 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 11 * Berkeley Laboratory. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)bpf.c 8.2 (Berkeley) 3/28/94 38 */ 39 40 #include "bpfilter.h" 41 42 #include <sys/param.h> 43 #include <sys/mbuf.h> 44 #include <sys/proc.h> 45 #include <sys/signalvar.h> 46 #include <sys/ioctl.h> 47 #include <sys/conf.h> 48 #include <sys/vnode.h> 49 #include <sys/file.h> 50 #include <sys/socket.h> 51 #include <sys/poll.h> 52 #include <sys/kernel.h> 53 #include <sys/sysctl.h> 54 55 #include <net/if.h> 56 #include <net/bpf.h> 57 #include <net/bpfdesc.h> 58 59 #include <netinet/in.h> 60 #include <netinet/if_ether.h> 61 62 #include "vlan.h" 63 #if NVLAN > 0 64 #include <net/if_vlan_var.h> 65 #endif 66 67 #include "pflog.h" 68 #if NPFLOG > 0 69 #include <net/if_pflog.h> 70 #endif 71 72 #define BPF_BUFSIZE 32768 73 74 #define PRINET 26 /* interruptible */ 75 76 /* from kern/kern_clock.c; incremented each clock tick. */ 77 extern int ticks; 78 79 /* 80 * The default read buffer size is patchable. 81 */ 82 int bpf_bufsize = BPF_BUFSIZE; 83 int bpf_maxbufsize = BPF_MAXBUFSIZE; 84 85 /* 86 * bpf_iflist is the list of interfaces; each corresponds to an ifnet 87 * bpf_d_list is the list of descriptors 88 */ 89 struct bpf_if *bpf_iflist; 90 LIST_HEAD(, bpf_d) bpf_d_list; 91 92 int bpf_allocbufs(struct bpf_d *); 93 void bpf_freed(struct bpf_d *); 94 void bpf_ifname(struct ifnet *, struct ifreq *); 95 void bpf_mcopy(const void *, void *, size_t); 96 int bpf_movein(struct uio *, u_int, struct mbuf **, 97 struct sockaddr *, struct bpf_insn *); 98 void bpf_attachd(struct bpf_d *, struct bpf_if *); 99 void bpf_detachd(struct bpf_d *); 100 int bpf_setif(struct bpf_d *, struct ifreq *); 101 int bpfpoll(dev_t, int, struct proc *); 102 int bpfkqfilter(dev_t, struct knote *); 103 void bpf_wakeup(struct bpf_d *); 104 void bpf_catchpacket(struct bpf_d *, u_char *, size_t, size_t, 105 void (*)(const void *, void *, size_t)); 106 void bpf_reset_d(struct bpf_d *); 107 int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); 108 int bpf_setdlt(struct bpf_d *, u_int); 109 110 void filt_bpfrdetach(struct knote *); 111 int filt_bpfread(struct knote *, long); 112 113 struct bpf_d *bpfilter_lookup(int); 114 struct bpf_d *bpfilter_create(int); 115 void bpfilter_destroy(struct bpf_d *); 116 117 int 118 bpf_movein(struct uio *uio, u_int linktype, struct mbuf **mp, 119 struct sockaddr *sockp, struct bpf_insn *filter) 120 { 121 struct mbuf *m; 122 struct m_tag *mtag; 123 int error; 124 u_int hlen; 125 u_int len; 126 u_int slen; 127 128 /* 129 * Build a sockaddr based on the data link layer type. 130 * We do this at this level because the ethernet header 131 * is copied directly into the data field of the sockaddr. 132 * In the case of SLIP, there is no header and the packet 133 * is forwarded as is. 134 * Also, we are careful to leave room at the front of the mbuf 135 * for the link level header. 136 */ 137 switch (linktype) { 138 139 case DLT_SLIP: 140 sockp->sa_family = AF_INET; 141 hlen = 0; 142 break; 143 144 case DLT_PPP: 145 sockp->sa_family = AF_UNSPEC; 146 hlen = 0; 147 break; 148 149 case DLT_EN10MB: 150 sockp->sa_family = AF_UNSPEC; 151 /* XXX Would MAXLINKHDR be better? */ 152 hlen = ETHER_HDR_LEN; 153 break; 154 155 case DLT_FDDI: 156 sockp->sa_family = AF_UNSPEC; 157 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */ 158 hlen = 24; 159 break; 160 161 case DLT_IEEE802_11: 162 case DLT_IEEE802_11_RADIO: 163 sockp->sa_family = AF_UNSPEC; 164 hlen = 0; 165 break; 166 167 case DLT_RAW: 168 case DLT_NULL: 169 sockp->sa_family = AF_UNSPEC; 170 hlen = 0; 171 break; 172 173 case DLT_ATM_RFC1483: 174 /* 175 * en atm driver requires 4-byte atm pseudo header. 176 * though it isn't standard, vpi:vci needs to be 177 * specified anyway. 178 */ 179 sockp->sa_family = AF_UNSPEC; 180 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ 181 break; 182 183 default: 184 return (EIO); 185 } 186 187 if (uio->uio_resid > MCLBYTES) 188 return (EIO); 189 len = uio->uio_resid; 190 191 MGETHDR(m, M_WAIT, MT_DATA); 192 m->m_pkthdr.rcvif = 0; 193 m->m_pkthdr.len = len - hlen; 194 195 if (len > MHLEN) { 196 MCLGET(m, M_WAIT); 197 if ((m->m_flags & M_EXT) == 0) { 198 error = ENOBUFS; 199 goto bad; 200 } 201 } 202 m->m_len = len; 203 *mp = m; 204 205 error = uiomove(mtod(m, caddr_t), len, uio); 206 if (error) 207 goto bad; 208 209 slen = bpf_filter(filter, mtod(m, u_char *), len, len); 210 if (slen < len) { 211 error = EPERM; 212 goto bad; 213 } 214 215 if (m->m_len < hlen) { 216 error = EPERM; 217 goto bad; 218 } 219 /* 220 * Make room for link header, and copy it to sockaddr 221 */ 222 if (hlen != 0) { 223 bcopy(m->m_data, sockp->sa_data, hlen); 224 m->m_len -= hlen; 225 m->m_data += hlen; /* XXX */ 226 } 227 228 /* 229 * Prepend the data link type as a mbuf tag 230 */ 231 mtag = m_tag_get(PACKET_TAG_DLT, sizeof(u_int), M_NOWAIT); 232 if (mtag == NULL) 233 return (ENOMEM); 234 *(u_int *)(mtag + 1) = linktype; 235 m_tag_prepend(m, mtag); 236 237 return (0); 238 bad: 239 m_freem(m); 240 return (error); 241 } 242 243 /* 244 * Attach file to the bpf interface, i.e. make d listen on bp. 245 * Must be called at splnet. 246 */ 247 void 248 bpf_attachd(struct bpf_d *d, struct bpf_if *bp) 249 { 250 /* 251 * Point d at bp, and add d to the interface's list of listeners. 252 * Finally, point the driver's bpf cookie at the interface so 253 * it will divert packets to bpf. 254 */ 255 d->bd_bif = bp; 256 d->bd_next = bp->bif_dlist; 257 bp->bif_dlist = d; 258 259 *bp->bif_driverp = bp; 260 } 261 262 /* 263 * Detach a file from its interface. 264 */ 265 void 266 bpf_detachd(struct bpf_d *d) 267 { 268 struct bpf_d **p; 269 struct bpf_if *bp; 270 271 bp = d->bd_bif; 272 /* 273 * Check if this descriptor had requested promiscuous mode. 274 * If so, turn it off. 275 */ 276 if (d->bd_promisc) { 277 int error; 278 279 d->bd_promisc = 0; 280 error = ifpromisc(bp->bif_ifp, 0); 281 if (error && !(error == EINVAL || error == ENODEV)) 282 /* 283 * Something is really wrong if we were able to put 284 * the driver into promiscuous mode, but can't 285 * take it out. 286 */ 287 panic("bpf: ifpromisc failed"); 288 } 289 /* Remove d from the interface's descriptor list. */ 290 p = &bp->bif_dlist; 291 while (*p != d) { 292 p = &(*p)->bd_next; 293 if (*p == 0) 294 panic("bpf_detachd: descriptor not in list"); 295 } 296 *p = (*p)->bd_next; 297 if (bp->bif_dlist == 0) 298 /* 299 * Let the driver know that there are no more listeners. 300 */ 301 *d->bd_bif->bif_driverp = 0; 302 d->bd_bif = 0; 303 } 304 305 /* 306 * Reference count access to descriptor buffers 307 */ 308 #define D_GET(d) ((d)->bd_ref++) 309 #define D_PUT(d) bpf_freed(d) 310 311 /* 312 * bpfilterattach() is called at boot time in new systems. We do 313 * nothing here since old systems will not call this. 314 */ 315 /* ARGSUSED */ 316 void 317 bpfilterattach(int n) 318 { 319 LIST_INIT(&bpf_d_list); 320 } 321 322 /* 323 * Open ethernet device. Returns ENXIO for illegal minor device number, 324 * EBUSY if file is open by another process. 325 */ 326 /* ARGSUSED */ 327 int 328 bpfopen(dev_t dev, int flag, int mode, struct proc *p) 329 { 330 struct bpf_d *d; 331 332 /* create on demand */ 333 if ((d = bpfilter_create(minor(dev))) == NULL) 334 return (EBUSY); 335 336 /* Mark "free" and do most initialization. */ 337 d->bd_bufsize = bpf_bufsize; 338 d->bd_sig = SIGIO; 339 340 D_GET(d); 341 342 return (0); 343 } 344 345 /* 346 * Close the descriptor by detaching it from its interface, 347 * deallocating its buffers, and marking it free. 348 */ 349 /* ARGSUSED */ 350 int 351 bpfclose(dev_t dev, int flag, int mode, struct proc *p) 352 { 353 struct bpf_d *d; 354 int s; 355 356 d = bpfilter_lookup(minor(dev)); 357 s = splnet(); 358 if (d->bd_bif) 359 bpf_detachd(d); 360 bpf_wakeup(d); 361 D_PUT(d); 362 splx(s); 363 364 return (0); 365 } 366 367 /* 368 * Rotate the packet buffers in descriptor d. Move the store buffer 369 * into the hold slot, and the free buffer into the store slot. 370 * Zero the length of the new store buffer. 371 */ 372 #define ROTATE_BUFFERS(d) \ 373 (d)->bd_hbuf = (d)->bd_sbuf; \ 374 (d)->bd_hlen = (d)->bd_slen; \ 375 (d)->bd_sbuf = (d)->bd_fbuf; \ 376 (d)->bd_slen = 0; \ 377 (d)->bd_fbuf = 0; 378 /* 379 * bpfread - read next chunk of packets from buffers 380 */ 381 int 382 bpfread(dev_t dev, struct uio *uio, int ioflag) 383 { 384 struct bpf_d *d; 385 int error; 386 int s; 387 388 d = bpfilter_lookup(minor(dev)); 389 if (d->bd_bif == 0) 390 return (ENXIO); 391 392 /* 393 * Restrict application to use a buffer the same size as 394 * as kernel buffers. 395 */ 396 if (uio->uio_resid != d->bd_bufsize) 397 return (EINVAL); 398 399 s = splnet(); 400 401 D_GET(d); 402 403 /* 404 * bd_rdStart is tagged when we start the read, iff there's a timeout. 405 * we can then figure out when we're done reading. 406 */ 407 if (d->bd_rtout != -1 && d->bd_rdStart == 0) 408 d->bd_rdStart = ticks; 409 else 410 d->bd_rdStart = 0; 411 412 /* 413 * If the hold buffer is empty, then do a timed sleep, which 414 * ends when the timeout expires or when enough packets 415 * have arrived to fill the store buffer. 416 */ 417 while (d->bd_hbuf == 0) { 418 if (d->bd_bif == NULL) { 419 /* interface is gone */ 420 if (d->bd_slen == 0) { 421 D_PUT(d); 422 splx(s); 423 return (EIO); 424 } 425 ROTATE_BUFFERS(d); 426 break; 427 } 428 if (d->bd_immediate && d->bd_slen != 0) { 429 /* 430 * A packet(s) either arrived since the previous 431 * read or arrived while we were asleep. 432 * Rotate the buffers and return what's here. 433 */ 434 ROTATE_BUFFERS(d); 435 break; 436 } 437 if ((d->bd_rtout != -1) || 438 (d->bd_rdStart + d->bd_rtout) < ticks) { 439 error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf", 440 d->bd_rtout); 441 } else { 442 if (d->bd_rtout == -1) { 443 /* User requested non-blocking I/O */ 444 error = EWOULDBLOCK; 445 } else 446 error = 0; 447 } 448 if (error == EINTR || error == ERESTART) { 449 D_PUT(d); 450 splx(s); 451 return (error); 452 } 453 if (error == EWOULDBLOCK) { 454 /* 455 * On a timeout, return what's in the buffer, 456 * which may be nothing. If there is something 457 * in the store buffer, we can rotate the buffers. 458 */ 459 if (d->bd_hbuf) 460 /* 461 * We filled up the buffer in between 462 * getting the timeout and arriving 463 * here, so we don't need to rotate. 464 */ 465 break; 466 467 if (d->bd_slen == 0) { 468 D_PUT(d); 469 splx(s); 470 return (0); 471 } 472 ROTATE_BUFFERS(d); 473 break; 474 } 475 } 476 /* 477 * At this point, we know we have something in the hold slot. 478 */ 479 splx(s); 480 481 /* 482 * Move data from hold buffer into user space. 483 * We know the entire buffer is transferred since 484 * we checked above that the read buffer is bpf_bufsize bytes. 485 */ 486 error = uiomove(d->bd_hbuf, d->bd_hlen, uio); 487 488 s = splnet(); 489 d->bd_fbuf = d->bd_hbuf; 490 d->bd_hbuf = 0; 491 d->bd_hlen = 0; 492 493 D_PUT(d); 494 splx(s); 495 496 return (error); 497 } 498 499 500 /* 501 * If there are processes sleeping on this descriptor, wake them up. 502 */ 503 void 504 bpf_wakeup(struct bpf_d *d) 505 { 506 wakeup((caddr_t)d); 507 if (d->bd_async && d->bd_sig) 508 csignal(d->bd_pgid, d->bd_sig, 509 d->bd_siguid, d->bd_sigeuid); 510 511 selwakeup(&d->bd_sel); 512 /* XXX */ 513 d->bd_sel.si_selpid = 0; 514 } 515 516 int 517 bpfwrite(dev_t dev, struct uio *uio, int ioflag) 518 { 519 struct bpf_d *d; 520 struct ifnet *ifp; 521 struct mbuf *m; 522 int error, s; 523 struct sockaddr_storage dst; 524 525 d = bpfilter_lookup(minor(dev)); 526 if (d->bd_bif == 0) 527 return (ENXIO); 528 529 ifp = d->bd_bif->bif_ifp; 530 531 if ((ifp->if_flags & IFF_UP) == 0) 532 return (ENETDOWN); 533 534 if (uio->uio_resid == 0) 535 return (0); 536 537 error = bpf_movein(uio, d->bd_bif->bif_dlt, &m, 538 (struct sockaddr *)&dst, d->bd_wfilter); 539 if (error) 540 return (error); 541 542 if (m->m_pkthdr.len > ifp->if_mtu) { 543 m_freem(m); 544 return (EMSGSIZE); 545 } 546 547 m->m_pkthdr.rdomain = ifp->if_rdomain; 548 549 if (d->bd_hdrcmplt) 550 dst.ss_family = pseudo_AF_HDRCMPLT; 551 552 s = splsoftnet(); 553 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)&dst, 554 (struct rtentry *)0); 555 splx(s); 556 /* 557 * The driver frees the mbuf. 558 */ 559 return (error); 560 } 561 562 /* 563 * Reset a descriptor by flushing its packet buffer and clearing the 564 * receive and drop counts. Should be called at splnet. 565 */ 566 void 567 bpf_reset_d(struct bpf_d *d) 568 { 569 if (d->bd_hbuf) { 570 /* Free the hold buffer. */ 571 d->bd_fbuf = d->bd_hbuf; 572 d->bd_hbuf = 0; 573 } 574 d->bd_slen = 0; 575 d->bd_hlen = 0; 576 d->bd_rcount = 0; 577 d->bd_dcount = 0; 578 } 579 580 /* 581 * FIONREAD Check for read packet available. 582 * BIOCGBLEN Get buffer len [for read()]. 583 * BIOCSETF Set ethernet read filter. 584 * BIOCFLUSH Flush read packet buffer. 585 * BIOCPROMISC Put interface into promiscuous mode. 586 * BIOCGDLTLIST Get supported link layer types. 587 * BIOCGDLT Get link layer type. 588 * BIOCSDLT Set link layer type. 589 * BIOCGETIF Get interface name. 590 * BIOCSETIF Set interface. 591 * BIOCSRTIMEOUT Set read timeout. 592 * BIOCGRTIMEOUT Get read timeout. 593 * BIOCGSTATS Get packet stats. 594 * BIOCIMMEDIATE Set immediate mode. 595 * BIOCVERSION Get filter language version. 596 * BIOCGHDRCMPLT Get "header already complete" flag 597 * BIOCSHDRCMPLT Set "header already complete" flag 598 */ 599 /* ARGSUSED */ 600 int 601 bpfioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 602 { 603 struct bpf_d *d; 604 int s, error = 0; 605 606 d = bpfilter_lookup(minor(dev)); 607 if (d->bd_locked && suser(p, 0) != 0) { 608 /* list of allowed ioctls when locked and not root */ 609 switch (cmd) { 610 case BIOCGBLEN: 611 case BIOCFLUSH: 612 case BIOCGDLT: 613 case BIOCGDLTLIST: 614 case BIOCGETIF: 615 case BIOCGRTIMEOUT: 616 case BIOCGSTATS: 617 case BIOCVERSION: 618 case BIOCGRSIG: 619 case BIOCGHDRCMPLT: 620 case FIONREAD: 621 case BIOCLOCK: 622 case BIOCSRTIMEOUT: 623 case BIOCIMMEDIATE: 624 case TIOCGPGRP: 625 case BIOCGDIRFILT: 626 break; 627 default: 628 return (EPERM); 629 } 630 } 631 632 switch (cmd) { 633 634 default: 635 error = EINVAL; 636 break; 637 638 /* 639 * Check for read packet available. 640 */ 641 case FIONREAD: 642 { 643 int n; 644 645 s = splnet(); 646 n = d->bd_slen; 647 if (d->bd_hbuf) 648 n += d->bd_hlen; 649 splx(s); 650 651 *(int *)addr = n; 652 break; 653 } 654 655 /* 656 * Get buffer len [for read()]. 657 */ 658 case BIOCGBLEN: 659 *(u_int *)addr = d->bd_bufsize; 660 break; 661 662 /* 663 * Set buffer length. 664 */ 665 case BIOCSBLEN: 666 if (d->bd_bif != 0) 667 error = EINVAL; 668 else { 669 u_int size = *(u_int *)addr; 670 671 if (size > bpf_maxbufsize) 672 *(u_int *)addr = size = bpf_maxbufsize; 673 else if (size < BPF_MINBUFSIZE) 674 *(u_int *)addr = size = BPF_MINBUFSIZE; 675 d->bd_bufsize = size; 676 } 677 break; 678 679 /* 680 * Set link layer read filter. 681 */ 682 case BIOCSETF: 683 error = bpf_setf(d, (struct bpf_program *)addr, 0); 684 break; 685 686 /* 687 * Set link layer write filter. 688 */ 689 case BIOCSETWF: 690 error = bpf_setf(d, (struct bpf_program *)addr, 1); 691 break; 692 693 /* 694 * Flush read packet buffer. 695 */ 696 case BIOCFLUSH: 697 s = splnet(); 698 bpf_reset_d(d); 699 splx(s); 700 break; 701 702 /* 703 * Put interface into promiscuous mode. 704 */ 705 case BIOCPROMISC: 706 if (d->bd_bif == 0) { 707 /* 708 * No interface attached yet. 709 */ 710 error = EINVAL; 711 break; 712 } 713 s = splnet(); 714 if (d->bd_promisc == 0) { 715 error = ifpromisc(d->bd_bif->bif_ifp, 1); 716 if (error == 0) 717 d->bd_promisc = 1; 718 } 719 splx(s); 720 break; 721 722 /* 723 * Get a list of supported device parameters. 724 */ 725 case BIOCGDLTLIST: 726 if (d->bd_bif == NULL) 727 error = EINVAL; 728 else 729 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr); 730 break; 731 732 /* 733 * Get device parameters. 734 */ 735 case BIOCGDLT: 736 if (d->bd_bif == 0) 737 error = EINVAL; 738 else 739 *(u_int *)addr = d->bd_bif->bif_dlt; 740 break; 741 742 /* 743 * Set device parameters. 744 */ 745 case BIOCSDLT: 746 if (d->bd_bif == NULL) 747 error = EINVAL; 748 else 749 error = bpf_setdlt(d, *(u_int *)addr); 750 break; 751 752 /* 753 * Set interface name. 754 */ 755 case BIOCGETIF: 756 if (d->bd_bif == 0) 757 error = EINVAL; 758 else 759 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr); 760 break; 761 762 /* 763 * Set interface. 764 */ 765 case BIOCSETIF: 766 error = bpf_setif(d, (struct ifreq *)addr); 767 break; 768 769 /* 770 * Set read timeout. 771 */ 772 case BIOCSRTIMEOUT: 773 { 774 struct timeval *tv = (struct timeval *)addr; 775 776 /* Compute number of ticks. */ 777 d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick; 778 if (d->bd_rtout == 0 && tv->tv_usec != 0) 779 d->bd_rtout = 1; 780 break; 781 } 782 783 /* 784 * Get read timeout. 785 */ 786 case BIOCGRTIMEOUT: 787 { 788 struct timeval *tv = (struct timeval *)addr; 789 790 tv->tv_sec = d->bd_rtout / hz; 791 tv->tv_usec = (d->bd_rtout % hz) * tick; 792 break; 793 } 794 795 /* 796 * Get packet stats. 797 */ 798 case BIOCGSTATS: 799 { 800 struct bpf_stat *bs = (struct bpf_stat *)addr; 801 802 bs->bs_recv = d->bd_rcount; 803 bs->bs_drop = d->bd_dcount; 804 break; 805 } 806 807 /* 808 * Set immediate mode. 809 */ 810 case BIOCIMMEDIATE: 811 d->bd_immediate = *(u_int *)addr; 812 break; 813 814 case BIOCVERSION: 815 { 816 struct bpf_version *bv = (struct bpf_version *)addr; 817 818 bv->bv_major = BPF_MAJOR_VERSION; 819 bv->bv_minor = BPF_MINOR_VERSION; 820 break; 821 } 822 823 case BIOCGHDRCMPLT: /* get "header already complete" flag */ 824 *(u_int *)addr = d->bd_hdrcmplt; 825 break; 826 827 case BIOCSHDRCMPLT: /* set "header already complete" flag */ 828 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; 829 break; 830 831 case BIOCLOCK: /* set "locked" flag (no reset) */ 832 d->bd_locked = 1; 833 break; 834 835 case BIOCGFILDROP: /* get "filter-drop" flag */ 836 *(u_int *)addr = d->bd_fildrop; 837 break; 838 839 case BIOCSFILDROP: /* set "filter-drop" flag */ 840 d->bd_fildrop = *(u_int *)addr ? 1 : 0; 841 break; 842 843 case BIOCGDIRFILT: /* get direction filter */ 844 *(u_int *)addr = d->bd_dirfilt; 845 break; 846 847 case BIOCSDIRFILT: /* set direction filter */ 848 d->bd_dirfilt = (*(u_int *)addr) & 849 (BPF_DIRECTION_IN|BPF_DIRECTION_OUT); 850 break; 851 852 case FIONBIO: /* Non-blocking I/O */ 853 if (*(int *)addr) 854 d->bd_rtout = -1; 855 else 856 d->bd_rtout = 0; 857 break; 858 859 case FIOASYNC: /* Send signal on receive packets */ 860 d->bd_async = *(int *)addr; 861 break; 862 863 /* 864 * N.B. ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing 865 * the equivalent of a TIOCSPGRP and hence end up here. *However* 866 * TIOCSPGRP's arg is a process group if it's positive and a process 867 * id if it's negative. This is exactly the opposite of what the 868 * other two functions want! Therefore there is code in ioctl and 869 * fcntl to negate the arg before calling here. 870 */ 871 case TIOCSPGRP: /* Process or group to send signals to */ 872 d->bd_pgid = *(int *)addr; 873 d->bd_siguid = p->p_cred->p_ruid; 874 d->bd_sigeuid = p->p_ucred->cr_uid; 875 break; 876 877 case TIOCGPGRP: 878 *(int *)addr = d->bd_pgid; 879 break; 880 881 case BIOCSRSIG: /* Set receive signal */ 882 { 883 u_int sig; 884 885 sig = *(u_int *)addr; 886 887 if (sig >= NSIG) 888 error = EINVAL; 889 else 890 d->bd_sig = sig; 891 break; 892 } 893 case BIOCGRSIG: 894 *(u_int *)addr = d->bd_sig; 895 break; 896 } 897 return (error); 898 } 899 900 /* 901 * Set d's packet filter program to fp. If this file already has a filter, 902 * free it and replace it. Returns EINVAL for bogus requests. 903 */ 904 int 905 bpf_setf(struct bpf_d *d, struct bpf_program *fp, int wf) 906 { 907 struct bpf_insn *fcode, *old; 908 u_int flen, size; 909 int s; 910 911 old = wf ? d->bd_wfilter : d->bd_rfilter; 912 if (fp->bf_insns == 0) { 913 if (fp->bf_len != 0) 914 return (EINVAL); 915 s = splnet(); 916 if (wf) 917 d->bd_wfilter = 0; 918 else 919 d->bd_rfilter = 0; 920 bpf_reset_d(d); 921 splx(s); 922 if (old != 0) 923 free((caddr_t)old, M_DEVBUF); 924 return (0); 925 } 926 flen = fp->bf_len; 927 if (flen > BPF_MAXINSNS) 928 return (EINVAL); 929 930 size = flen * sizeof(*fp->bf_insns); 931 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK); 932 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && 933 bpf_validate(fcode, (int)flen)) { 934 s = splnet(); 935 if (wf) 936 d->bd_wfilter = fcode; 937 else 938 d->bd_rfilter = fcode; 939 bpf_reset_d(d); 940 splx(s); 941 if (old != 0) 942 free((caddr_t)old, M_DEVBUF); 943 944 return (0); 945 } 946 free((caddr_t)fcode, M_DEVBUF); 947 return (EINVAL); 948 } 949 950 /* 951 * Detach a file from its current interface (if attached at all) and attach 952 * to the interface indicated by the name stored in ifr. 953 * Return an errno or 0. 954 */ 955 int 956 bpf_setif(struct bpf_d *d, struct ifreq *ifr) 957 { 958 struct bpf_if *bp, *candidate = NULL; 959 int s, error; 960 961 /* 962 * Look through attached interfaces for the named one. 963 */ 964 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) { 965 struct ifnet *ifp = bp->bif_ifp; 966 967 if (ifp == 0 || 968 strcmp(ifp->if_xname, ifr->ifr_name) != 0) 969 continue; 970 971 /* 972 * We found the requested interface. 973 */ 974 if (candidate == NULL || candidate->bif_dlt > bp->bif_dlt) 975 candidate = bp; 976 } 977 978 if (candidate != NULL) { 979 /* 980 * Allocate the packet buffers if we need to. 981 * If we're already attached to requested interface, 982 * just flush the buffer. 983 */ 984 if (d->bd_sbuf == 0) { 985 error = bpf_allocbufs(d); 986 if (error != 0) 987 return (error); 988 } 989 s = splnet(); 990 if (candidate != d->bd_bif) { 991 if (d->bd_bif) 992 /* 993 * Detach if attached to something else. 994 */ 995 bpf_detachd(d); 996 997 bpf_attachd(d, candidate); 998 } 999 bpf_reset_d(d); 1000 splx(s); 1001 return (0); 1002 } 1003 /* Not found. */ 1004 return (ENXIO); 1005 } 1006 1007 /* 1008 * Copy the interface name to the ifreq. 1009 */ 1010 void 1011 bpf_ifname(struct ifnet *ifp, struct ifreq *ifr) 1012 { 1013 bcopy(ifp->if_xname, ifr->ifr_name, IFNAMSIZ); 1014 } 1015 1016 /* 1017 * Support for poll() system call 1018 */ 1019 int 1020 bpfpoll(dev_t dev, int events, struct proc *p) 1021 { 1022 struct bpf_d *d; 1023 int s, revents; 1024 1025 revents = events & (POLLIN | POLLRDNORM); 1026 if (revents == 0) 1027 return (0); /* only support reading */ 1028 1029 /* 1030 * An imitation of the FIONREAD ioctl code. 1031 */ 1032 d = bpfilter_lookup(minor(dev)); 1033 /* 1034 * XXX The USB stack manages it to trigger some race condition 1035 * which causes bpfilter_lookup to return NULL when a USB device 1036 * gets detached while it is up and has an open bpf handler (e.g. 1037 * dhclient). We still should recheck if we can fix the root 1038 * cause of this issue. 1039 */ 1040 if (d == NULL) 1041 return (POLLERR); 1042 s = splnet(); 1043 if (d->bd_hlen == 0 && (!d->bd_immediate || d->bd_slen == 0)) { 1044 revents = 0; /* no data waiting */ 1045 /* 1046 * if there's a timeout, mark the time we started waiting. 1047 */ 1048 if (d->bd_rtout != -1 && d->bd_rdStart == 0) 1049 d->bd_rdStart = ticks; 1050 selrecord(p, &d->bd_sel); 1051 } 1052 splx(s); 1053 return (revents); 1054 } 1055 1056 struct filterops bpfread_filtops = 1057 { 1, NULL, filt_bpfrdetach, filt_bpfread }; 1058 1059 int 1060 bpfkqfilter(dev_t dev, struct knote *kn) 1061 { 1062 struct bpf_d *d; 1063 struct klist *klist; 1064 int s; 1065 1066 d = bpfilter_lookup(minor(dev)); 1067 switch (kn->kn_filter) { 1068 case EVFILT_READ: 1069 klist = &d->bd_sel.si_note; 1070 kn->kn_fop = &bpfread_filtops; 1071 break; 1072 default: 1073 return (EINVAL); 1074 } 1075 1076 kn->kn_hook = (caddr_t)((u_long)dev); 1077 1078 s = splnet(); 1079 D_GET(d); 1080 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 1081 splx(s); 1082 1083 return (0); 1084 } 1085 1086 void 1087 filt_bpfrdetach(struct knote *kn) 1088 { 1089 dev_t dev = (dev_t)((u_long)kn->kn_hook); 1090 struct bpf_d *d; 1091 int s; 1092 1093 d = bpfilter_lookup(minor(dev)); 1094 s = splnet(); 1095 SLIST_REMOVE(&d->bd_sel.si_note, kn, knote, kn_selnext); 1096 D_PUT(d); 1097 splx(s); 1098 } 1099 1100 int 1101 filt_bpfread(struct knote *kn, long hint) 1102 { 1103 dev_t dev = (dev_t)((u_long)kn->kn_hook); 1104 struct bpf_d *d; 1105 1106 d = bpfilter_lookup(minor(dev)); 1107 kn->kn_data = d->bd_hlen; 1108 if (d->bd_immediate) 1109 kn->kn_data += d->bd_slen; 1110 return (kn->kn_data > 0); 1111 } 1112 1113 /* 1114 * Incoming linkage from device drivers. Process the packet pkt, of length 1115 * pktlen, which is stored in a contiguous buffer. The packet is parsed 1116 * by each process' filter, and if accepted, stashed into the corresponding 1117 * buffer. 1118 */ 1119 int 1120 bpf_tap(caddr_t arg, u_char *pkt, u_int pktlen, u_int direction) 1121 { 1122 struct bpf_if *bp; 1123 struct bpf_d *d; 1124 size_t slen; 1125 int drop = 0; 1126 1127 /* 1128 * Note that the ipl does not have to be raised at this point. 1129 * The only problem that could arise here is that if two different 1130 * interfaces shared any data. This is not the case. 1131 */ 1132 bp = (struct bpf_if *)arg; 1133 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 1134 ++d->bd_rcount; 1135 if ((direction & d->bd_dirfilt) != 0) 1136 slen = 0; 1137 else 1138 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen); 1139 if (slen != 0) { 1140 bpf_catchpacket(d, pkt, pktlen, slen, bcopy); 1141 if (d->bd_fildrop) 1142 drop++; 1143 } 1144 } 1145 1146 return (drop); 1147 } 1148 1149 /* 1150 * Copy data from an mbuf chain into a buffer. This code is derived 1151 * from m_copydata in sys/uipc_mbuf.c. 1152 */ 1153 void 1154 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len) 1155 { 1156 const struct mbuf *m; 1157 u_int count; 1158 u_char *dst; 1159 1160 m = src_arg; 1161 dst = dst_arg; 1162 while (len > 0) { 1163 if (m == 0) 1164 panic("bpf_mcopy"); 1165 count = min(m->m_len, len); 1166 bcopy(mtod(m, caddr_t), (caddr_t)dst, count); 1167 m = m->m_next; 1168 dst += count; 1169 len -= count; 1170 } 1171 } 1172 1173 /* 1174 * Incoming linkage from device drivers, when packet is in an mbuf chain. 1175 */ 1176 void 1177 bpf_mtap(caddr_t arg, struct mbuf *m, u_int direction) 1178 { 1179 struct bpf_if *bp = (struct bpf_if *)arg; 1180 struct bpf_d *d; 1181 size_t pktlen, slen; 1182 struct mbuf *m0; 1183 1184 if (m == NULL) 1185 return; 1186 1187 pktlen = 0; 1188 for (m0 = m; m0 != 0; m0 = m0->m_next) 1189 pktlen += m0->m_len; 1190 1191 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 1192 ++d->bd_rcount; 1193 if ((direction & d->bd_dirfilt) != 0) 1194 slen = 0; 1195 else 1196 slen = bpf_filter(d->bd_rfilter, (u_char *)m, 1197 pktlen, 0); 1198 1199 if (slen == 0) 1200 continue; 1201 1202 bpf_catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy); 1203 if (d->bd_fildrop) 1204 m->m_flags |= M_FILDROP; 1205 } 1206 } 1207 1208 /* 1209 * Incoming linkage from device drivers, where we have a mbuf chain 1210 * but need to prepend some arbitrary header from a linear buffer. 1211 * 1212 * Con up a minimal dummy header to pacify bpf. Allocate (only) a 1213 * struct m_hdr on the stack. This is safe as bpf only reads from the 1214 * fields in this header that we initialize, and will not try to free 1215 * it or keep a pointer to it. 1216 */ 1217 void 1218 bpf_mtap_hdr(caddr_t arg, caddr_t data, u_int dlen, struct mbuf *m, 1219 u_int direction) 1220 { 1221 struct m_hdr mh; 1222 1223 mh.mh_flags = 0; 1224 mh.mh_next = m; 1225 mh.mh_len = dlen; 1226 mh.mh_data = data; 1227 1228 bpf_mtap(arg, (struct mbuf *) &mh, direction); 1229 m->m_flags |= mh.mh_flags & M_FILDROP; 1230 } 1231 1232 /* 1233 * Incoming linkage from device drivers, where we have a mbuf chain 1234 * but need to prepend the address family. 1235 * 1236 * Con up a minimal dummy header to pacify bpf. We allocate (only) a 1237 * struct m_hdr on the stack. This is safe as bpf only reads from the 1238 * fields in this header that we initialize, and will not try to free 1239 * it or keep a pointer to it. 1240 */ 1241 void 1242 bpf_mtap_af(caddr_t arg, u_int32_t af, struct mbuf *m, u_int direction) 1243 { 1244 struct m_hdr mh; 1245 1246 mh.mh_flags = 0; 1247 mh.mh_next = m; 1248 mh.mh_len = 4; 1249 mh.mh_data = (caddr_t)⁡ 1250 1251 bpf_mtap(arg, (struct mbuf *) &mh, direction); 1252 m->m_flags |= mh.mh_flags & M_FILDROP; 1253 } 1254 1255 /* 1256 * Incoming linkage from device drivers, where we have a mbuf chain 1257 * but need to prepend a VLAN encapsulation header. 1258 * 1259 * Con up a minimal dummy header to pacify bpf. Allocate (only) a 1260 * struct m_hdr on the stack. This is safe as bpf only reads from the 1261 * fields in this header that we initialize, and will not try to free 1262 * it or keep a pointer to it. 1263 */ 1264 void 1265 bpf_mtap_ether(caddr_t arg, struct mbuf *m, u_int direction) 1266 { 1267 #if NVLAN > 0 1268 struct m_hdr mh; 1269 struct ether_vlan_header evh; 1270 1271 if ((m->m_flags & M_VLANTAG) == 0) 1272 #endif 1273 { 1274 bpf_mtap(arg, m, direction); 1275 return; 1276 } 1277 1278 #if NVLAN > 0 1279 bcopy(mtod(m, char *), &evh, ETHER_HDR_LEN); 1280 evh.evl_proto = evh.evl_encap_proto; 1281 evh.evl_encap_proto = htons(ETHERTYPE_VLAN); 1282 evh.evl_tag = htons(m->m_pkthdr.ether_vtag); 1283 m->m_len -= ETHER_HDR_LEN; 1284 m->m_data += ETHER_HDR_LEN; 1285 1286 mh.mh_flags = 0; 1287 mh.mh_next = m; 1288 mh.mh_len = sizeof(evh); 1289 mh.mh_data = (caddr_t)&evh; 1290 1291 bpf_mtap(arg, (struct mbuf *) &mh, direction); 1292 m->m_flags |= mh.mh_flags & M_FILDROP; 1293 1294 m->m_len += ETHER_HDR_LEN; 1295 m->m_data -= ETHER_HDR_LEN; 1296 #endif 1297 } 1298 1299 void 1300 bpf_mtap_pflog(caddr_t arg, caddr_t data, struct mbuf *m) 1301 { 1302 #if NPFLOG > 0 1303 struct m_hdr mh; 1304 struct bpf_if *bp = (struct bpf_if *)arg; 1305 struct bpf_d *d; 1306 size_t pktlen, slen; 1307 struct mbuf *m0; 1308 1309 if (m == NULL) 1310 return; 1311 1312 mh.mh_flags = 0; 1313 mh.mh_next = m; 1314 mh.mh_len = PFLOG_HDRLEN; 1315 mh.mh_data = data; 1316 1317 pktlen = mh.mh_len; 1318 for (m0 = m; m0 != 0; m0 = m0->m_next) 1319 pktlen += m0->m_len; 1320 1321 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 1322 ++d->bd_rcount; 1323 if ((BPF_DIRECTION_OUT & d->bd_dirfilt) != 0) 1324 slen = 0; 1325 else 1326 slen = bpf_filter(d->bd_rfilter, (u_char *)&mh, 1327 pktlen, 0); 1328 1329 if (slen == 0) 1330 continue; 1331 1332 bpf_catchpacket(d, (u_char *)&mh, pktlen, slen, pflog_bpfcopy); 1333 } 1334 #endif 1335 } 1336 1337 1338 /* 1339 * Move the packet data from interface memory (pkt) into the 1340 * store buffer. Return 1 if it's time to wakeup a listener (buffer full), 1341 * otherwise 0. "copy" is the routine called to do the actual data 1342 * transfer. bcopy is passed in to copy contiguous chunks, while 1343 * bpf_mcopy is passed in to copy mbuf chains. In the latter case, 1344 * pkt is really an mbuf. 1345 */ 1346 void 1347 bpf_catchpacket(struct bpf_d *d, u_char *pkt, size_t pktlen, size_t snaplen, 1348 void (*cpfn)(const void *, void *, size_t)) 1349 { 1350 struct bpf_hdr *hp; 1351 int totlen, curlen; 1352 int hdrlen = d->bd_bif->bif_hdrlen; 1353 struct timeval tv; 1354 1355 /* 1356 * Figure out how many bytes to move. If the packet is 1357 * greater or equal to the snapshot length, transfer that 1358 * much. Otherwise, transfer the whole packet (unless 1359 * we hit the buffer size limit). 1360 */ 1361 totlen = hdrlen + min(snaplen, pktlen); 1362 if (totlen > d->bd_bufsize) 1363 totlen = d->bd_bufsize; 1364 1365 /* 1366 * Round up the end of the previous packet to the next longword. 1367 */ 1368 curlen = BPF_WORDALIGN(d->bd_slen); 1369 if (curlen + totlen > d->bd_bufsize) { 1370 /* 1371 * This packet will overflow the storage buffer. 1372 * Rotate the buffers if we can, then wakeup any 1373 * pending reads. 1374 */ 1375 if (d->bd_fbuf == 0) { 1376 /* 1377 * We haven't completed the previous read yet, 1378 * so drop the packet. 1379 */ 1380 ++d->bd_dcount; 1381 return; 1382 } 1383 ROTATE_BUFFERS(d); 1384 bpf_wakeup(d); 1385 curlen = 0; 1386 } 1387 1388 /* 1389 * Append the bpf header. 1390 */ 1391 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); 1392 microtime(&tv); 1393 hp->bh_tstamp.tv_sec = tv.tv_sec; 1394 hp->bh_tstamp.tv_usec = tv.tv_usec; 1395 hp->bh_datalen = pktlen; 1396 hp->bh_hdrlen = hdrlen; 1397 /* 1398 * Copy the packet data into the store buffer and update its length. 1399 */ 1400 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); 1401 d->bd_slen = curlen + totlen; 1402 1403 if (d->bd_immediate) { 1404 /* 1405 * Immediate mode is set. A packet arrived so any 1406 * reads should be woken up. 1407 */ 1408 bpf_wakeup(d); 1409 } 1410 1411 if (d->bd_rdStart && (d->bd_rtout + d->bd_rdStart < ticks)) { 1412 /* 1413 * we could be selecting on the bpf, and we 1414 * may have timeouts set. We got here by getting 1415 * a packet, so wake up the reader. 1416 */ 1417 if (d->bd_fbuf) { 1418 d->bd_rdStart = 0; 1419 ROTATE_BUFFERS(d); 1420 bpf_wakeup(d); 1421 } 1422 } 1423 } 1424 1425 /* 1426 * Initialize all nonzero fields of a descriptor. 1427 */ 1428 int 1429 bpf_allocbufs(struct bpf_d *d) 1430 { 1431 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT); 1432 if (d->bd_fbuf == NULL) 1433 return (ENOBUFS); 1434 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT); 1435 if (d->bd_sbuf == NULL) { 1436 free(d->bd_fbuf, M_DEVBUF); 1437 return (ENOBUFS); 1438 } 1439 d->bd_slen = 0; 1440 d->bd_hlen = 0; 1441 return (0); 1442 } 1443 1444 /* 1445 * Free buffers currently in use by a descriptor 1446 * when the reference count drops to zero. 1447 */ 1448 void 1449 bpf_freed(struct bpf_d *d) 1450 { 1451 if (--d->bd_ref > 0) 1452 return; 1453 1454 if (d->bd_sbuf != 0) { 1455 free(d->bd_sbuf, M_DEVBUF); 1456 if (d->bd_hbuf != 0) 1457 free(d->bd_hbuf, M_DEVBUF); 1458 if (d->bd_fbuf != 0) 1459 free(d->bd_fbuf, M_DEVBUF); 1460 } 1461 if (d->bd_rfilter) 1462 free((caddr_t)d->bd_rfilter, M_DEVBUF); 1463 if (d->bd_wfilter) 1464 free((caddr_t)d->bd_wfilter, M_DEVBUF); 1465 1466 bpfilter_destroy(d); 1467 } 1468 1469 /* 1470 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *) 1471 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed 1472 * size of the link header (variable length headers not yet supported). 1473 */ 1474 void 1475 bpfattach(caddr_t *driverp, struct ifnet *ifp, u_int dlt, u_int hdrlen) 1476 { 1477 struct bpf_if *bp; 1478 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT); 1479 1480 if (bp == 0) 1481 panic("bpfattach"); 1482 1483 bp->bif_dlist = 0; 1484 bp->bif_driverp = (struct bpf_if **)driverp; 1485 bp->bif_ifp = ifp; 1486 bp->bif_dlt = dlt; 1487 1488 bp->bif_next = bpf_iflist; 1489 bpf_iflist = bp; 1490 1491 *bp->bif_driverp = NULL; 1492 1493 /* 1494 * Compute the length of the bpf header. This is not necessarily 1495 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1496 * that the network layer header begins on a longword boundary (for 1497 * performance reasons and to alleviate alignment restrictions). 1498 */ 1499 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1500 } 1501 1502 /* Detach an interface from its attached bpf device. */ 1503 void 1504 bpfdetach(struct ifnet *ifp) 1505 { 1506 struct bpf_if *bp, *nbp, **pbp = &bpf_iflist; 1507 struct bpf_d *bd; 1508 int maj; 1509 1510 for (bp = bpf_iflist; bp; bp = nbp) { 1511 nbp= bp->bif_next; 1512 if (bp->bif_ifp == ifp) { 1513 *pbp = nbp; 1514 1515 /* Locate the major number. */ 1516 for (maj = 0; maj < nchrdev; maj++) 1517 if (cdevsw[maj].d_open == bpfopen) 1518 break; 1519 1520 for (bd = bp->bif_dlist; bd; bd = bp->bif_dlist) { 1521 struct bpf_d *d; 1522 1523 /* 1524 * Locate the minor number and nuke the vnode 1525 * for any open instance. 1526 */ 1527 LIST_FOREACH(d, &bpf_d_list, bd_list) 1528 if (d == bd) { 1529 vdevgone(maj, d->bd_unit, 1530 d->bd_unit, VCHR); 1531 break; 1532 } 1533 } 1534 1535 free(bp, M_DEVBUF); 1536 } else 1537 pbp = &bp->bif_next; 1538 } 1539 ifp->if_bpf = NULL; 1540 } 1541 1542 int 1543 bpf_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 1544 size_t newlen) 1545 { 1546 int newval; 1547 int error; 1548 1549 if (namelen != 1) 1550 return (ENOTDIR); 1551 1552 switch (name[0]) { 1553 case NET_BPF_BUFSIZE: 1554 newval = bpf_bufsize; 1555 error = sysctl_int(oldp, oldlenp, newp, newlen, &newval); 1556 if (error) 1557 return (error); 1558 if (newval < BPF_MINBUFSIZE || newval > bpf_maxbufsize) 1559 return (EINVAL); 1560 bpf_bufsize = newval; 1561 break; 1562 case NET_BPF_MAXBUFSIZE: 1563 newval = bpf_maxbufsize; 1564 error = sysctl_int(oldp, oldlenp, newp, newlen, &newval); 1565 if (error) 1566 return (error); 1567 if (newval < BPF_MINBUFSIZE) 1568 return (EINVAL); 1569 bpf_maxbufsize = newval; 1570 break; 1571 default: 1572 return (EOPNOTSUPP); 1573 } 1574 return (0); 1575 } 1576 1577 struct bpf_d * 1578 bpfilter_lookup(int unit) 1579 { 1580 struct bpf_d *bd; 1581 1582 LIST_FOREACH(bd, &bpf_d_list, bd_list) 1583 if (bd->bd_unit == unit) 1584 return (bd); 1585 return (NULL); 1586 } 1587 1588 struct bpf_d * 1589 bpfilter_create(int unit) 1590 { 1591 struct bpf_d *bd; 1592 1593 if ((bd = bpfilter_lookup(unit)) != NULL) 1594 return (NULL); 1595 if ((bd = malloc(sizeof(*bd), M_DEVBUF, M_NOWAIT|M_ZERO)) != NULL) { 1596 bd->bd_unit = unit; 1597 LIST_INSERT_HEAD(&bpf_d_list, bd, bd_list); 1598 } 1599 return (bd); 1600 } 1601 1602 void 1603 bpfilter_destroy(struct bpf_d *bd) 1604 { 1605 LIST_REMOVE(bd, bd_list); 1606 free(bd, M_DEVBUF); 1607 } 1608 1609 /* 1610 * Get a list of available data link type of the interface. 1611 */ 1612 int 1613 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl) 1614 { 1615 int n, error; 1616 struct ifnet *ifp; 1617 struct bpf_if *bp; 1618 1619 ifp = d->bd_bif->bif_ifp; 1620 n = 0; 1621 error = 0; 1622 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1623 if (bp->bif_ifp != ifp) 1624 continue; 1625 if (bfl->bfl_list != NULL) { 1626 if (n >= bfl->bfl_len) 1627 return (ENOMEM); 1628 error = copyout(&bp->bif_dlt, 1629 bfl->bfl_list + n, sizeof(u_int)); 1630 if (error) 1631 break; 1632 } 1633 n++; 1634 } 1635 1636 bfl->bfl_len = n; 1637 return (error); 1638 } 1639 1640 /* 1641 * Set the data link type of a BPF instance. 1642 */ 1643 int 1644 bpf_setdlt(struct bpf_d *d, u_int dlt) 1645 { 1646 int s; 1647 struct ifnet *ifp; 1648 struct bpf_if *bp; 1649 1650 if (d->bd_bif->bif_dlt == dlt) 1651 return (0); 1652 ifp = d->bd_bif->bif_ifp; 1653 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1654 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) 1655 break; 1656 } 1657 if (bp == NULL) 1658 return (EINVAL); 1659 s = splnet(); 1660 bpf_detachd(d); 1661 bpf_attachd(d, bp); 1662 bpf_reset_d(d); 1663 splx(s); 1664 return (0); 1665 } 1666