1 /* 2 * Copyright (c) 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from the Stanford/CMU enet packet filter, 6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed 7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 8 * Berkeley Laboratory. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)bpf.c 8.2 (Berkeley) 3/28/94 39 * 40 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.12 2002/04/14 21:41:48 luigi Exp $ 41 */ 42 43 #include "use_bpf.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/conf.h> 48 #include <sys/device.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/filedesc.h> 58 59 #include <sys/event.h> 60 61 #include <sys/socket.h> 62 #include <sys/vnode.h> 63 64 #include <sys/thread2.h> 65 66 #include <net/if.h> 67 #include <net/bpf.h> 68 #include <net/bpfdesc.h> 69 #include <net/netmsg2.h> 70 #include <net/netisr2.h> 71 72 #include <netinet/in.h> 73 #include <netinet/if_ether.h> 74 #include <sys/kernel.h> 75 #include <sys/sysctl.h> 76 77 #include <sys/devfs.h> 78 79 struct netmsg_bpf_output { 80 struct netmsg_base base; 81 struct mbuf *nm_mbuf; 82 struct ifnet *nm_ifp; 83 struct sockaddr *nm_dst; 84 }; 85 86 MALLOC_DEFINE(M_BPF, "BPF", "BPF data"); 87 DEVFS_DECLARE_CLONE_BITMAP(bpf); 88 89 #if NBPF <= 1 90 #define BPF_PREALLOCATED_UNITS 4 91 #else 92 #define BPF_PREALLOCATED_UNITS NBPF 93 #endif 94 95 #if NBPF > 0 96 97 /* 98 * The default read buffer size is patchable. 99 */ 100 static int bpf_bufsize = BPF_DEFAULTBUFSIZE; 101 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW, 102 &bpf_bufsize, 0, "Current size of bpf buffer"); 103 int bpf_maxbufsize = BPF_MAXBUFSIZE; 104 SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW, 105 &bpf_maxbufsize, 0, "Maximum size of bpf buffer"); 106 107 /* 108 * bpf_iflist is the list of interfaces; each corresponds to an ifnet 109 */ 110 static struct bpf_if *bpf_iflist; 111 112 static struct lwkt_token bpf_token = LWKT_TOKEN_INITIALIZER(bpf_token); 113 114 static int bpf_allocbufs(struct bpf_d *); 115 static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp); 116 static void bpf_detachd(struct bpf_d *d); 117 static void bpf_resetd(struct bpf_d *); 118 static void bpf_freed(struct bpf_d *); 119 static void bpf_mcopy(const void *, void *, size_t); 120 static int bpf_movein(struct uio *, int, struct mbuf **, 121 struct sockaddr *, int *, struct bpf_insn *); 122 static int bpf_setif(struct bpf_d *, struct ifreq *); 123 static void bpf_timed_out(void *); 124 static void bpf_wakeup(struct bpf_d *); 125 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int, 126 void (*)(const void *, void *, size_t), 127 const struct timeval *); 128 static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd); 129 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); 130 static int bpf_setdlt(struct bpf_d *, u_int); 131 static void bpf_drvinit(void *unused); 132 static void bpf_filter_detach(struct knote *kn); 133 static int bpf_filter_read(struct knote *kn, long hint); 134 135 static d_open_t bpfopen; 136 static d_clone_t bpfclone; 137 static d_close_t bpfclose; 138 static d_read_t bpfread; 139 static d_write_t bpfwrite; 140 static d_ioctl_t bpfioctl; 141 static d_kqfilter_t bpfkqfilter; 142 143 #define CDEV_MAJOR 23 144 static struct dev_ops bpf_ops = { 145 { "bpf", 0, D_MPSAFE }, 146 .d_open = bpfopen, 147 .d_close = bpfclose, 148 .d_read = bpfread, 149 .d_write = bpfwrite, 150 .d_ioctl = bpfioctl, 151 .d_kqfilter = bpfkqfilter 152 }; 153 154 155 static int 156 bpf_movein(struct uio *uio, int linktype, struct mbuf **mp, 157 struct sockaddr *sockp, int *datlen, struct bpf_insn *wfilter) 158 { 159 struct mbuf *m; 160 int error; 161 int len; 162 int hlen; 163 int slen; 164 165 *datlen = 0; 166 *mp = NULL; 167 168 /* 169 * Build a sockaddr based on the data link layer type. 170 * We do this at this level because the ethernet header 171 * is copied directly into the data field of the sockaddr. 172 * In the case of SLIP, there is no header and the packet 173 * is forwarded as is. 174 * Also, we are careful to leave room at the front of the mbuf 175 * for the link level header. 176 */ 177 switch (linktype) { 178 case DLT_SLIP: 179 sockp->sa_family = AF_INET; 180 hlen = 0; 181 break; 182 183 case DLT_EN10MB: 184 sockp->sa_family = AF_UNSPEC; 185 /* XXX Would MAXLINKHDR be better? */ 186 hlen = sizeof(struct ether_header); 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 m = m_getl(len, MB_WAIT, MT_DATA, M_PKTHDR, NULL); 220 if (m == NULL) 221 return(ENOBUFS); 222 m->m_pkthdr.len = m->m_len = len; 223 m->m_pkthdr.rcvif = NULL; 224 *mp = m; 225 226 if (m->m_len < hlen) { 227 error = EPERM; 228 goto bad; 229 } 230 231 error = uiomove(mtod(m, u_char *), len, uio); 232 if (error) 233 goto bad; 234 235 slen = bpf_filter(wfilter, mtod(m, u_char *), len, len); 236 if (slen == 0) { 237 error = EPERM; 238 goto bad; 239 } 240 241 /* 242 * Make room for link header, and copy it to sockaddr. 243 */ 244 if (hlen != 0) { 245 bcopy(m->m_data, sockp->sa_data, hlen); 246 m->m_pkthdr.len -= hlen; 247 m->m_len -= hlen; 248 m->m_data += hlen; /* XXX */ 249 } 250 return (0); 251 bad: 252 m_freem(m); 253 return(error); 254 } 255 256 /* 257 * Attach file to the bpf interface, i.e. make d listen on bp. 258 * Must be called at splimp. 259 */ 260 static void 261 bpf_attachd(struct bpf_d *d, struct bpf_if *bp) 262 { 263 /* 264 * Point d at bp, and add d to the interface's list of listeners. 265 * Finally, point the driver's bpf cookie at the interface so 266 * it will divert packets to bpf. 267 */ 268 lwkt_gettoken(&bpf_token); 269 d->bd_bif = bp; 270 SLIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); 271 *bp->bif_driverp = bp; 272 273 EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1); 274 lwkt_reltoken(&bpf_token); 275 } 276 277 /* 278 * Detach a file from its interface. 279 */ 280 static void 281 bpf_detachd(struct bpf_d *d) 282 { 283 int error; 284 struct bpf_if *bp; 285 struct ifnet *ifp; 286 287 lwkt_gettoken(&bpf_token); 288 bp = d->bd_bif; 289 ifp = bp->bif_ifp; 290 291 /* Remove d from the interface's descriptor list. */ 292 SLIST_REMOVE(&bp->bif_dlist, d, bpf_d, bd_next); 293 294 if (SLIST_EMPTY(&bp->bif_dlist)) { 295 /* 296 * Let the driver know that there are no more listeners. 297 */ 298 *bp->bif_driverp = NULL; 299 } 300 d->bd_bif = NULL; 301 302 EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0); 303 304 /* 305 * Check if this descriptor had requested promiscuous mode. 306 * If so, turn it off. 307 */ 308 if (d->bd_promisc) { 309 d->bd_promisc = 0; 310 error = ifpromisc(ifp, 0); 311 if (error != 0 && error != ENXIO) { 312 /* 313 * ENXIO can happen if a pccard is unplugged, 314 * Something is really wrong if we were able to put 315 * the driver into promiscuous mode, but can't 316 * take it out. 317 */ 318 if_printf(ifp, "bpf_detach: ifpromisc failed(%d)\n", 319 error); 320 } 321 } 322 lwkt_reltoken(&bpf_token); 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(struct dev_open_args *ap) 332 { 333 cdev_t dev = ap->a_head.a_dev; 334 struct bpf_d *d; 335 336 lwkt_gettoken(&bpf_token); 337 if (ap->a_cred->cr_prison) { 338 lwkt_reltoken(&bpf_token); 339 return(EPERM); 340 } 341 342 d = dev->si_drv1; 343 /* 344 * Each minor can be opened by only one process. If the requested 345 * minor is in use, return EBUSY. 346 */ 347 if (d != NULL) { 348 lwkt_reltoken(&bpf_token); 349 return(EBUSY); 350 } 351 352 d = kmalloc(sizeof *d, M_BPF, M_WAITOK | M_ZERO); 353 dev->si_drv1 = d; 354 d->bd_bufsize = bpf_bufsize; 355 d->bd_sig = SIGIO; 356 d->bd_seesent = 1; 357 callout_init(&d->bd_callout); 358 lwkt_reltoken(&bpf_token); 359 360 return(0); 361 } 362 363 static int 364 bpfclone(struct dev_clone_args *ap) 365 { 366 int unit; 367 368 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(bpf), 0); 369 ap->a_dev = make_only_dev(&bpf_ops, unit, 0, 0, 0600, "bpf%d", unit); 370 371 return 0; 372 } 373 374 /* 375 * Close the descriptor by detaching it from its interface, 376 * deallocating its buffers, and marking it free. 377 */ 378 /* ARGSUSED */ 379 static int 380 bpfclose(struct dev_close_args *ap) 381 { 382 cdev_t dev = ap->a_head.a_dev; 383 struct bpf_d *d = dev->si_drv1; 384 385 lwkt_gettoken(&bpf_token); 386 funsetown(&d->bd_sigio); 387 if (d->bd_state == BPF_WAITING) 388 callout_stop(&d->bd_callout); 389 d->bd_state = BPF_IDLE; 390 if (d->bd_bif != NULL) 391 bpf_detachd(d); 392 bpf_freed(d); 393 dev->si_drv1 = NULL; 394 if (dev->si_uminor >= BPF_PREALLOCATED_UNITS) { 395 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(bpf), dev->si_uminor); 396 destroy_dev(dev); 397 } 398 kfree(d, M_BPF); 399 lwkt_reltoken(&bpf_token); 400 401 return(0); 402 } 403 404 /* 405 * Rotate the packet buffers in descriptor d. Move the store buffer 406 * into the hold slot, and the free buffer into the store slot. 407 * Zero the length of the new store buffer. 408 */ 409 #define ROTATE_BUFFERS(d) \ 410 (d)->bd_hbuf = (d)->bd_sbuf; \ 411 (d)->bd_hlen = (d)->bd_slen; \ 412 (d)->bd_sbuf = (d)->bd_fbuf; \ 413 (d)->bd_slen = 0; \ 414 (d)->bd_fbuf = NULL; 415 /* 416 * bpfread - read next chunk of packets from buffers 417 */ 418 static int 419 bpfread(struct dev_read_args *ap) 420 { 421 cdev_t dev = ap->a_head.a_dev; 422 struct bpf_d *d = dev->si_drv1; 423 int timed_out; 424 int error; 425 426 lwkt_gettoken(&bpf_token); 427 /* 428 * Restrict application to use a buffer the same size as 429 * as kernel buffers. 430 */ 431 if (ap->a_uio->uio_resid != d->bd_bufsize) { 432 lwkt_reltoken(&bpf_token); 433 return(EINVAL); 434 } 435 436 if (d->bd_state == BPF_WAITING) 437 callout_stop(&d->bd_callout); 438 timed_out = (d->bd_state == BPF_TIMED_OUT); 439 d->bd_state = BPF_IDLE; 440 /* 441 * If the hold buffer is empty, then do a timed sleep, which 442 * ends when the timeout expires or when enough packets 443 * have arrived to fill the store buffer. 444 */ 445 while (d->bd_hbuf == NULL) { 446 if ((d->bd_immediate || (ap->a_ioflag & IO_NDELAY) || timed_out) 447 && d->bd_slen != 0) { 448 /* 449 * A packet(s) either arrived since the previous, 450 * We're in immediate mode, or are reading 451 * in non-blocking mode, and a packet(s) 452 * 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 lwkt_reltoken(&bpf_token); 468 return(ENXIO); 469 } 470 471 if (ap->a_ioflag & IO_NDELAY) { 472 lwkt_reltoken(&bpf_token); 473 return(EWOULDBLOCK); 474 } 475 error = tsleep(d, PCATCH, "bpf", d->bd_rtout); 476 if (error == EINTR || error == ERESTART) { 477 lwkt_reltoken(&bpf_token); 478 return(error); 479 } 480 if (error == EWOULDBLOCK) { 481 /* 482 * On a timeout, return what's in the buffer, 483 * which may be nothing. If there is something 484 * in the store buffer, we can rotate the buffers. 485 */ 486 if (d->bd_hbuf) 487 /* 488 * We filled up the buffer in between 489 * getting the timeout and arriving 490 * here, so we don't need to rotate. 491 */ 492 break; 493 494 if (d->bd_slen == 0) { 495 lwkt_reltoken(&bpf_token); 496 return(0); 497 } 498 ROTATE_BUFFERS(d); 499 break; 500 } 501 } 502 /* 503 * At this point, we know we have something in the hold slot. 504 */ 505 506 /* 507 * Move data from hold buffer into user space. 508 * We know the entire buffer is transferred since 509 * we checked above that the read buffer is bpf_bufsize bytes. 510 */ 511 error = uiomove(d->bd_hbuf, d->bd_hlen, ap->a_uio); 512 513 d->bd_fbuf = d->bd_hbuf; 514 d->bd_hbuf = NULL; 515 d->bd_hlen = 0; 516 lwkt_reltoken(&bpf_token); 517 518 return(error); 519 } 520 521 522 /* 523 * If there are processes sleeping on this descriptor, wake them up. 524 */ 525 static void 526 bpf_wakeup(struct bpf_d *d) 527 { 528 if (d->bd_state == BPF_WAITING) { 529 callout_stop(&d->bd_callout); 530 d->bd_state = BPF_IDLE; 531 } 532 wakeup(d); 533 if (d->bd_async && d->bd_sig && d->bd_sigio) 534 pgsigio(d->bd_sigio, d->bd_sig, 0); 535 536 KNOTE(&d->bd_kq.ki_note, 0); 537 } 538 539 static void 540 bpf_timed_out(void *arg) 541 { 542 struct bpf_d *d = (struct bpf_d *)arg; 543 544 if (d->bd_state == BPF_WAITING) { 545 d->bd_state = BPF_TIMED_OUT; 546 if (d->bd_slen != 0) 547 bpf_wakeup(d); 548 } 549 } 550 551 static void 552 bpf_output_dispatch(netmsg_t msg) 553 { 554 struct netmsg_bpf_output *bmsg = (struct netmsg_bpf_output *)msg; 555 struct ifnet *ifp = bmsg->nm_ifp; 556 int error; 557 558 /* 559 * The driver frees the mbuf. 560 */ 561 error = ifp->if_output(ifp, bmsg->nm_mbuf, bmsg->nm_dst, NULL); 562 lwkt_replymsg(&msg->lmsg, error); 563 } 564 565 static int 566 bpfwrite(struct dev_write_args *ap) 567 { 568 cdev_t dev = ap->a_head.a_dev; 569 struct bpf_d *d = dev->si_drv1; 570 struct ifnet *ifp; 571 struct mbuf *m; 572 int error, ret; 573 struct sockaddr dst; 574 int datlen; 575 struct netmsg_bpf_output bmsg; 576 577 lwkt_gettoken(&bpf_token); 578 if (d->bd_bif == NULL) { 579 lwkt_reltoken(&bpf_token); 580 return(ENXIO); 581 } 582 583 ifp = d->bd_bif->bif_ifp; 584 585 if (ap->a_uio->uio_resid == 0) { 586 lwkt_reltoken(&bpf_token); 587 return(0); 588 } 589 590 error = bpf_movein(ap->a_uio, (int)d->bd_bif->bif_dlt, &m, 591 &dst, &datlen, d->bd_wfilter); 592 if (error) { 593 lwkt_reltoken(&bpf_token); 594 return(error); 595 } 596 597 if (datlen > ifp->if_mtu) { 598 m_freem(m); 599 lwkt_reltoken(&bpf_token); 600 return(EMSGSIZE); 601 } 602 603 if (d->bd_hdrcmplt) 604 dst.sa_family = pseudo_AF_HDRCMPLT; 605 606 netmsg_init(&bmsg.base, NULL, &curthread->td_msgport, 607 0, bpf_output_dispatch); 608 bmsg.nm_mbuf = m; 609 bmsg.nm_ifp = ifp; 610 bmsg.nm_dst = &dst; 611 612 ret = lwkt_domsg(netisr_cpuport(0), &bmsg.base.lmsg, 0); 613 lwkt_reltoken(&bpf_token); 614 615 return ret; 616 } 617 618 /* 619 * Reset a descriptor by flushing its packet buffer and clearing the 620 * receive and drop counts. Should be called at splimp. 621 */ 622 static void 623 bpf_resetd(struct bpf_d *d) 624 { 625 if (d->bd_hbuf) { 626 /* Free the hold buffer. */ 627 d->bd_fbuf = d->bd_hbuf; 628 d->bd_hbuf = NULL; 629 } 630 d->bd_slen = 0; 631 d->bd_hlen = 0; 632 d->bd_rcount = 0; 633 d->bd_dcount = 0; 634 } 635 636 /* 637 * FIONREAD Check for read packet available. 638 * SIOCGIFADDR Get interface address - convenient hook to driver. 639 * BIOCGBLEN Get buffer len [for read()]. 640 * BIOCSETF Set ethernet read filter. 641 * BIOCSETWF Set ethernet write filter. 642 * BIOCFLUSH Flush read packet buffer. 643 * BIOCPROMISC Put interface into promiscuous mode. 644 * BIOCGDLT Get link layer type. 645 * BIOCGETIF Get interface name. 646 * BIOCSETIF Set interface. 647 * BIOCSRTIMEOUT Set read timeout. 648 * BIOCGRTIMEOUT Get read timeout. 649 * BIOCGSTATS Get packet stats. 650 * BIOCIMMEDIATE Set immediate mode. 651 * BIOCVERSION Get filter language version. 652 * BIOCGHDRCMPLT Get "header already complete" flag 653 * BIOCSHDRCMPLT Set "header already complete" flag 654 * BIOCGSEESENT Get "see packets sent" flag 655 * BIOCSSEESENT Set "see packets sent" flag 656 * BIOCLOCK Set "locked" flag 657 */ 658 /* ARGSUSED */ 659 static int 660 bpfioctl(struct dev_ioctl_args *ap) 661 { 662 cdev_t dev = ap->a_head.a_dev; 663 struct bpf_d *d = dev->si_drv1; 664 int error = 0; 665 666 lwkt_gettoken(&bpf_token); 667 if (d->bd_state == BPF_WAITING) 668 callout_stop(&d->bd_callout); 669 d->bd_state = BPF_IDLE; 670 671 if (d->bd_locked == 1) { 672 switch (ap->a_cmd) { 673 case BIOCGBLEN: 674 case BIOCFLUSH: 675 case BIOCGDLT: 676 case BIOCGDLTLIST: 677 case BIOCGETIF: 678 case BIOCGRTIMEOUT: 679 case BIOCGSTATS: 680 case BIOCVERSION: 681 case BIOCGRSIG: 682 case BIOCGHDRCMPLT: 683 case FIONREAD: 684 case BIOCLOCK: 685 case BIOCSRTIMEOUT: 686 case BIOCIMMEDIATE: 687 case TIOCGPGRP: 688 break; 689 default: 690 lwkt_reltoken(&bpf_token); 691 return (EPERM); 692 } 693 } 694 switch (ap->a_cmd) { 695 default: 696 error = EINVAL; 697 break; 698 699 /* 700 * Check for read packet available. 701 */ 702 case FIONREAD: 703 { 704 int n; 705 706 n = d->bd_slen; 707 if (d->bd_hbuf) 708 n += d->bd_hlen; 709 710 *(int *)ap->a_data = n; 711 break; 712 } 713 714 case SIOCGIFADDR: 715 { 716 struct ifnet *ifp; 717 718 if (d->bd_bif == NULL) { 719 error = EINVAL; 720 } else { 721 ifp = d->bd_bif->bif_ifp; 722 ifnet_serialize_all(ifp); 723 error = ifp->if_ioctl(ifp, ap->a_cmd, 724 ap->a_data, ap->a_cred); 725 ifnet_deserialize_all(ifp); 726 } 727 break; 728 } 729 730 /* 731 * Get buffer len [for read()]. 732 */ 733 case BIOCGBLEN: 734 *(u_int *)ap->a_data = d->bd_bufsize; 735 break; 736 737 /* 738 * Set buffer length. 739 */ 740 case BIOCSBLEN: 741 if (d->bd_bif != NULL) { 742 error = EINVAL; 743 } else { 744 u_int size = *(u_int *)ap->a_data; 745 746 if (size > bpf_maxbufsize) 747 *(u_int *)ap->a_data = size = bpf_maxbufsize; 748 else if (size < BPF_MINBUFSIZE) 749 *(u_int *)ap->a_data = size = BPF_MINBUFSIZE; 750 d->bd_bufsize = size; 751 } 752 break; 753 754 /* 755 * Set link layer read filter. 756 */ 757 case BIOCSETF: 758 case BIOCSETWF: 759 error = bpf_setf(d, (struct bpf_program *)ap->a_data, 760 ap->a_cmd); 761 break; 762 763 /* 764 * Flush read packet buffer. 765 */ 766 case BIOCFLUSH: 767 bpf_resetd(d); 768 break; 769 770 /* 771 * Put interface into promiscuous mode. 772 */ 773 case BIOCPROMISC: 774 if (d->bd_bif == NULL) { 775 /* 776 * No interface attached yet. 777 */ 778 error = EINVAL; 779 break; 780 } 781 if (d->bd_promisc == 0) { 782 error = ifpromisc(d->bd_bif->bif_ifp, 1); 783 if (error == 0) 784 d->bd_promisc = 1; 785 } 786 break; 787 788 /* 789 * Get device parameters. 790 */ 791 case BIOCGDLT: 792 if (d->bd_bif == NULL) 793 error = EINVAL; 794 else 795 *(u_int *)ap->a_data = d->bd_bif->bif_dlt; 796 break; 797 798 /* 799 * Get a list of supported data link types. 800 */ 801 case BIOCGDLTLIST: 802 if (d->bd_bif == NULL) { 803 error = EINVAL; 804 } else { 805 error = bpf_getdltlist(d, 806 (struct bpf_dltlist *)ap->a_data); 807 } 808 break; 809 810 /* 811 * Set data link type. 812 */ 813 case BIOCSDLT: 814 if (d->bd_bif == NULL) 815 error = EINVAL; 816 else 817 error = bpf_setdlt(d, *(u_int *)ap->a_data); 818 break; 819 820 /* 821 * Get interface name. 822 */ 823 case BIOCGETIF: 824 if (d->bd_bif == NULL) { 825 error = EINVAL; 826 } else { 827 struct ifnet *const ifp = d->bd_bif->bif_ifp; 828 struct ifreq *const ifr = (struct ifreq *)ap->a_data; 829 830 strlcpy(ifr->ifr_name, ifp->if_xname, 831 sizeof ifr->ifr_name); 832 } 833 break; 834 835 /* 836 * Set interface. 837 */ 838 case BIOCSETIF: 839 error = bpf_setif(d, (struct ifreq *)ap->a_data); 840 break; 841 842 /* 843 * Set read timeout. 844 */ 845 case BIOCSRTIMEOUT: 846 { 847 struct timeval *tv = (struct timeval *)ap->a_data; 848 849 /* 850 * Subtract 1 tick from tvtohz() since this isn't 851 * a one-shot timer. 852 */ 853 if ((error = itimerfix(tv)) == 0) 854 d->bd_rtout = tvtohz_low(tv); 855 break; 856 } 857 858 /* 859 * Get read timeout. 860 */ 861 case BIOCGRTIMEOUT: 862 { 863 struct timeval *tv = (struct timeval *)ap->a_data; 864 865 tv->tv_sec = d->bd_rtout / hz; 866 tv->tv_usec = (d->bd_rtout % hz) * ustick; 867 break; 868 } 869 870 /* 871 * Get packet stats. 872 */ 873 case BIOCGSTATS: 874 { 875 struct bpf_stat *bs = (struct bpf_stat *)ap->a_data; 876 877 bs->bs_recv = d->bd_rcount; 878 bs->bs_drop = d->bd_dcount; 879 break; 880 } 881 882 /* 883 * Set immediate mode. 884 */ 885 case BIOCIMMEDIATE: 886 d->bd_immediate = *(u_int *)ap->a_data; 887 break; 888 889 case BIOCVERSION: 890 { 891 struct bpf_version *bv = (struct bpf_version *)ap->a_data; 892 893 bv->bv_major = BPF_MAJOR_VERSION; 894 bv->bv_minor = BPF_MINOR_VERSION; 895 break; 896 } 897 898 /* 899 * Get "header already complete" flag 900 */ 901 case BIOCGHDRCMPLT: 902 *(u_int *)ap->a_data = d->bd_hdrcmplt; 903 break; 904 905 /* 906 * Set "header already complete" flag 907 */ 908 case BIOCSHDRCMPLT: 909 d->bd_hdrcmplt = *(u_int *)ap->a_data ? 1 : 0; 910 break; 911 912 /* 913 * Get "see sent packets" flag 914 */ 915 case BIOCGSEESENT: 916 *(u_int *)ap->a_data = d->bd_seesent; 917 break; 918 919 /* 920 * Set "see sent packets" flag 921 */ 922 case BIOCSSEESENT: 923 d->bd_seesent = *(u_int *)ap->a_data; 924 break; 925 926 case FIOASYNC: /* Send signal on receive packets */ 927 d->bd_async = *(int *)ap->a_data; 928 break; 929 930 case FIOSETOWN: 931 error = fsetown(*(int *)ap->a_data, &d->bd_sigio); 932 break; 933 934 case FIOGETOWN: 935 *(int *)ap->a_data = fgetown(&d->bd_sigio); 936 break; 937 938 /* This is deprecated, FIOSETOWN should be used instead. */ 939 case TIOCSPGRP: 940 error = fsetown(-(*(int *)ap->a_data), &d->bd_sigio); 941 break; 942 943 /* This is deprecated, FIOGETOWN should be used instead. */ 944 case TIOCGPGRP: 945 *(int *)ap->a_data = -fgetown(&d->bd_sigio); 946 break; 947 948 case BIOCSRSIG: /* Set receive signal */ 949 { 950 u_int sig; 951 952 sig = *(u_int *)ap->a_data; 953 954 if (sig >= NSIG) 955 error = EINVAL; 956 else 957 d->bd_sig = sig; 958 break; 959 } 960 case BIOCGRSIG: 961 *(u_int *)ap->a_data = d->bd_sig; 962 break; 963 case BIOCLOCK: 964 d->bd_locked = 1; 965 break; 966 } 967 lwkt_reltoken(&bpf_token); 968 969 return(error); 970 } 971 972 /* 973 * Set d's packet filter program to fp. If this file already has a filter, 974 * free it and replace it. Returns EINVAL for bogus requests. 975 */ 976 static int 977 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd) 978 { 979 struct bpf_insn *fcode, *old; 980 u_int wfilter, flen, size; 981 982 if (cmd == BIOCSETWF) { 983 old = d->bd_wfilter; 984 wfilter = 1; 985 } else { 986 wfilter = 0; 987 old = d->bd_rfilter; 988 } 989 if (fp->bf_insns == NULL) { 990 if (fp->bf_len != 0) 991 return(EINVAL); 992 if (wfilter) 993 d->bd_wfilter = NULL; 994 else 995 d->bd_rfilter = NULL; 996 bpf_resetd(d); 997 if (old != NULL) 998 kfree(old, M_BPF); 999 return(0); 1000 } 1001 flen = fp->bf_len; 1002 if (flen > BPF_MAXINSNS) 1003 return(EINVAL); 1004 1005 size = flen * sizeof *fp->bf_insns; 1006 fcode = (struct bpf_insn *)kmalloc(size, M_BPF, M_WAITOK); 1007 if (copyin(fp->bf_insns, fcode, size) == 0 && 1008 bpf_validate(fcode, (int)flen)) { 1009 if (wfilter) 1010 d->bd_wfilter = fcode; 1011 else 1012 d->bd_rfilter = fcode; 1013 bpf_resetd(d); 1014 if (old != NULL) 1015 kfree(old, M_BPF); 1016 1017 return(0); 1018 } 1019 kfree(fcode, M_BPF); 1020 return(EINVAL); 1021 } 1022 1023 /* 1024 * Detach a file from its current interface (if attached at all) and attach 1025 * to the interface indicated by the name stored in ifr. 1026 * Return an errno or 0. 1027 */ 1028 static int 1029 bpf_setif(struct bpf_d *d, struct ifreq *ifr) 1030 { 1031 struct bpf_if *bp; 1032 int error; 1033 struct ifnet *theywant; 1034 1035 theywant = ifunit(ifr->ifr_name); 1036 if (theywant == NULL) 1037 return(ENXIO); 1038 1039 /* 1040 * Look through attached interfaces for the named one. 1041 */ 1042 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1043 struct ifnet *ifp = bp->bif_ifp; 1044 1045 if (ifp == NULL || ifp != theywant) 1046 continue; 1047 /* skip additional entry */ 1048 if (bp->bif_driverp != &ifp->if_bpf) 1049 continue; 1050 /* 1051 * We found the requested interface. 1052 * Allocate the packet buffers if we need to. 1053 * If we're already attached to requested interface, 1054 * just flush the buffer. 1055 */ 1056 if (d->bd_sbuf == NULL) { 1057 error = bpf_allocbufs(d); 1058 if (error != 0) 1059 return(error); 1060 } 1061 if (bp != d->bd_bif) { 1062 if (d->bd_bif != NULL) { 1063 /* 1064 * Detach if attached to something else. 1065 */ 1066 bpf_detachd(d); 1067 } 1068 1069 bpf_attachd(d, bp); 1070 } 1071 bpf_resetd(d); 1072 return(0); 1073 } 1074 1075 /* Not found. */ 1076 return(ENXIO); 1077 } 1078 1079 static struct filterops bpf_read_filtops = 1080 { FILTEROP_ISFD, NULL, bpf_filter_detach, bpf_filter_read }; 1081 1082 static int 1083 bpfkqfilter(struct dev_kqfilter_args *ap) 1084 { 1085 cdev_t dev = ap->a_head.a_dev; 1086 struct knote *kn = ap->a_kn; 1087 struct klist *klist; 1088 struct bpf_d *d; 1089 1090 lwkt_gettoken(&bpf_token); 1091 d = dev->si_drv1; 1092 if (d->bd_bif == NULL) { 1093 ap->a_result = 1; 1094 lwkt_reltoken(&bpf_token); 1095 return (0); 1096 } 1097 1098 ap->a_result = 0; 1099 switch (kn->kn_filter) { 1100 case EVFILT_READ: 1101 kn->kn_fop = &bpf_read_filtops; 1102 kn->kn_hook = (caddr_t)d; 1103 break; 1104 default: 1105 ap->a_result = EOPNOTSUPP; 1106 lwkt_reltoken(&bpf_token); 1107 return (0); 1108 } 1109 1110 klist = &d->bd_kq.ki_note; 1111 knote_insert(klist, kn); 1112 lwkt_reltoken(&bpf_token); 1113 1114 return (0); 1115 } 1116 1117 static void 1118 bpf_filter_detach(struct knote *kn) 1119 { 1120 struct klist *klist; 1121 struct bpf_d *d; 1122 1123 d = (struct bpf_d *)kn->kn_hook; 1124 klist = &d->bd_kq.ki_note; 1125 knote_remove(klist, kn); 1126 } 1127 1128 static int 1129 bpf_filter_read(struct knote *kn, long hint) 1130 { 1131 struct bpf_d *d; 1132 int ready = 0; 1133 1134 d = (struct bpf_d *)kn->kn_hook; 1135 if (d->bd_hlen != 0 || 1136 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) && 1137 d->bd_slen != 0)) { 1138 ready = 1; 1139 } else { 1140 /* Start the read timeout if necessary. */ 1141 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1142 callout_reset(&d->bd_callout, d->bd_rtout, 1143 bpf_timed_out, d); 1144 d->bd_state = BPF_WAITING; 1145 } 1146 } 1147 1148 return (ready); 1149 } 1150 1151 1152 /* 1153 * Process the packet pkt of length pktlen. The packet is parsed 1154 * by each listener's filter, and if accepted, stashed into the 1155 * corresponding buffer. 1156 */ 1157 void 1158 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 1159 { 1160 struct bpf_d *d; 1161 struct timeval tv; 1162 int gottime = 0; 1163 u_int slen; 1164 1165 lwkt_gettoken(&bpf_token); 1166 /* Re-check */ 1167 if (bp == NULL) { 1168 lwkt_reltoken(&bpf_token); 1169 return; 1170 } 1171 1172 /* 1173 * Note that the ipl does not have to be raised at this point. 1174 * The only problem that could arise here is that if two different 1175 * interfaces shared any data. This is not the case. 1176 */ 1177 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1178 ++d->bd_rcount; 1179 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen); 1180 if (slen != 0) { 1181 if (!gottime) { 1182 microtime(&tv); 1183 gottime = 1; 1184 } 1185 catchpacket(d, pkt, pktlen, slen, ovbcopy, &tv); 1186 } 1187 } 1188 lwkt_reltoken(&bpf_token); 1189 } 1190 1191 /* 1192 * Copy data from an mbuf chain into a buffer. This code is derived 1193 * from m_copydata in sys/uipc_mbuf.c. 1194 */ 1195 static void 1196 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len) 1197 { 1198 const struct mbuf *m; 1199 u_int count; 1200 u_char *dst; 1201 1202 m = src_arg; 1203 dst = dst_arg; 1204 while (len > 0) { 1205 if (m == NULL) 1206 panic("bpf_mcopy"); 1207 count = min(m->m_len, len); 1208 bcopy(mtod(m, void *), dst, count); 1209 m = m->m_next; 1210 dst += count; 1211 len -= count; 1212 } 1213 } 1214 1215 /* 1216 * Process the packet in the mbuf chain m. The packet is parsed by each 1217 * listener's filter, and if accepted, stashed into the corresponding 1218 * buffer. 1219 */ 1220 void 1221 bpf_mtap(struct bpf_if *bp, struct mbuf *m) 1222 { 1223 struct bpf_d *d; 1224 u_int pktlen, slen; 1225 struct timeval tv; 1226 int gottime = 0; 1227 1228 lwkt_gettoken(&bpf_token); 1229 /* Re-check */ 1230 if (bp == NULL) { 1231 lwkt_reltoken(&bpf_token); 1232 return; 1233 } 1234 1235 /* Don't compute pktlen, if no descriptor is attached. */ 1236 if (SLIST_EMPTY(&bp->bif_dlist)) { 1237 lwkt_reltoken(&bpf_token); 1238 return; 1239 } 1240 1241 pktlen = m_lengthm(m, NULL); 1242 1243 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1244 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL)) 1245 continue; 1246 ++d->bd_rcount; 1247 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0); 1248 if (slen != 0) { 1249 if (!gottime) { 1250 microtime(&tv); 1251 gottime = 1; 1252 } 1253 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy, 1254 &tv); 1255 } 1256 } 1257 lwkt_reltoken(&bpf_token); 1258 } 1259 1260 /* 1261 * Incoming linkage from device drivers, where we have a mbuf chain 1262 * but need to prepend some arbitrary header from a linear buffer. 1263 * 1264 * Con up a minimal dummy header to pacify bpf. Allocate (only) a 1265 * struct m_hdr on the stack. This is safe as bpf only reads from the 1266 * fields in this header that we initialize, and will not try to free 1267 * it or keep a pointer to it. 1268 */ 1269 void 1270 bpf_mtap_hdr(struct bpf_if *arg, caddr_t data, u_int dlen, struct mbuf *m, u_int direction) 1271 { 1272 struct m_hdr mh; 1273 1274 mh.mh_flags = 0; 1275 mh.mh_next = m; 1276 mh.mh_len = dlen; 1277 mh.mh_data = data; 1278 1279 return bpf_mtap(arg, (struct mbuf *) &mh); 1280 } 1281 1282 void 1283 bpf_mtap_family(struct bpf_if *bp, struct mbuf *m, sa_family_t family) 1284 { 1285 u_int family4; 1286 1287 KKASSERT(family != AF_UNSPEC); 1288 1289 family4 = (u_int)family; 1290 bpf_ptap(bp, m, &family4, sizeof(family4)); 1291 } 1292 1293 /* 1294 * Process the packet in the mbuf chain m with the header in m prepended. 1295 * The packet is parsed by each listener's filter, and if accepted, 1296 * stashed into the corresponding buffer. 1297 */ 1298 void 1299 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen) 1300 { 1301 struct mbuf mb; 1302 1303 /* 1304 * Craft on-stack mbuf suitable for passing to bpf_mtap. 1305 * Note that we cut corners here; we only setup what's 1306 * absolutely needed--this mbuf should never go anywhere else. 1307 */ 1308 mb.m_next = m; 1309 mb.m_data = __DECONST(void *, data); /* LINTED */ 1310 mb.m_len = dlen; 1311 mb.m_pkthdr.rcvif = m->m_pkthdr.rcvif; 1312 1313 bpf_mtap(bp, &mb); 1314 } 1315 1316 /* 1317 * Move the packet data from interface memory (pkt) into the 1318 * store buffer. Return 1 if it's time to wakeup a listener (buffer full), 1319 * otherwise 0. "copy" is the routine called to do the actual data 1320 * transfer. bcopy is passed in to copy contiguous chunks, while 1321 * bpf_mcopy is passed in to copy mbuf chains. In the latter case, 1322 * pkt is really an mbuf. 1323 */ 1324 static void 1325 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen, 1326 void (*cpfn)(const void *, void *, size_t), 1327 const struct timeval *tv) 1328 { 1329 struct bpf_hdr *hp; 1330 int totlen, curlen; 1331 int hdrlen = d->bd_bif->bif_hdrlen; 1332 int wakeup = 0; 1333 /* 1334 * Figure out how many bytes to move. If the packet is 1335 * greater or equal to the snapshot length, transfer that 1336 * much. Otherwise, transfer the whole packet (unless 1337 * we hit the buffer size limit). 1338 */ 1339 totlen = hdrlen + min(snaplen, pktlen); 1340 if (totlen > d->bd_bufsize) 1341 totlen = d->bd_bufsize; 1342 1343 /* 1344 * Round up the end of the previous packet to the next longword. 1345 */ 1346 curlen = BPF_WORDALIGN(d->bd_slen); 1347 if (curlen + totlen > d->bd_bufsize) { 1348 /* 1349 * This packet will overflow the storage buffer. 1350 * Rotate the buffers if we can, then wakeup any 1351 * pending reads. 1352 */ 1353 if (d->bd_fbuf == NULL) { 1354 /* 1355 * We haven't completed the previous read yet, 1356 * so drop the packet. 1357 */ 1358 ++d->bd_dcount; 1359 return; 1360 } 1361 ROTATE_BUFFERS(d); 1362 wakeup = 1; 1363 curlen = 0; 1364 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) { 1365 /* 1366 * Immediate mode is set, or the read timeout has 1367 * already expired during a select call. A packet 1368 * arrived, so the reader should be woken up. 1369 */ 1370 wakeup = 1; 1371 } 1372 1373 /* 1374 * Append the bpf header. 1375 */ 1376 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); 1377 hp->bh_tstamp = *tv; 1378 hp->bh_datalen = pktlen; 1379 hp->bh_hdrlen = hdrlen; 1380 /* 1381 * Copy the packet data into the store buffer and update its length. 1382 */ 1383 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); 1384 d->bd_slen = curlen + totlen; 1385 1386 if (wakeup) 1387 bpf_wakeup(d); 1388 } 1389 1390 /* 1391 * Initialize all nonzero fields of a descriptor. 1392 */ 1393 static int 1394 bpf_allocbufs(struct bpf_d *d) 1395 { 1396 d->bd_fbuf = kmalloc(d->bd_bufsize, M_BPF, M_WAITOK); 1397 d->bd_sbuf = kmalloc(d->bd_bufsize, M_BPF, M_WAITOK); 1398 d->bd_slen = 0; 1399 d->bd_hlen = 0; 1400 return(0); 1401 } 1402 1403 /* 1404 * Free buffers and packet filter program currently in use by a descriptor. 1405 * Called on close. 1406 */ 1407 static void 1408 bpf_freed(struct bpf_d *d) 1409 { 1410 /* 1411 * We don't need to lock out interrupts since this descriptor has 1412 * been detached from its interface and it yet hasn't been marked 1413 * free. 1414 */ 1415 if (d->bd_sbuf != NULL) { 1416 kfree(d->bd_sbuf, M_BPF); 1417 if (d->bd_hbuf != NULL) 1418 kfree(d->bd_hbuf, M_BPF); 1419 if (d->bd_fbuf != NULL) 1420 kfree(d->bd_fbuf, M_BPF); 1421 } 1422 if (d->bd_rfilter) 1423 kfree(d->bd_rfilter, M_BPF); 1424 if (d->bd_wfilter) 1425 kfree(d->bd_wfilter, M_BPF); 1426 } 1427 1428 /* 1429 * Attach an interface to bpf. ifp is a pointer to the structure 1430 * defining the interface to be attached, dlt is the link layer type, 1431 * and hdrlen is the fixed size of the link header (variable length 1432 * headers are not yet supported). 1433 */ 1434 void 1435 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) 1436 { 1437 bpfattach_dlt(ifp, dlt, hdrlen, &ifp->if_bpf); 1438 } 1439 1440 void 1441 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 1442 { 1443 struct bpf_if *bp; 1444 1445 bp = kmalloc(sizeof *bp, M_BPF, M_WAITOK | M_ZERO); 1446 1447 lwkt_gettoken(&bpf_token); 1448 1449 SLIST_INIT(&bp->bif_dlist); 1450 bp->bif_ifp = ifp; 1451 bp->bif_dlt = dlt; 1452 bp->bif_driverp = driverp; 1453 *bp->bif_driverp = NULL; 1454 1455 bp->bif_next = bpf_iflist; 1456 bpf_iflist = bp; 1457 1458 /* 1459 * Compute the length of the bpf header. This is not necessarily 1460 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1461 * that the network layer header begins on a longword boundary (for 1462 * performance reasons and to alleviate alignment restrictions). 1463 */ 1464 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1465 1466 lwkt_reltoken(&bpf_token); 1467 1468 if (bootverbose) 1469 if_printf(ifp, "bpf attached\n"); 1470 } 1471 1472 /* 1473 * Detach bpf from an interface. This involves detaching each descriptor 1474 * associated with the interface, and leaving bd_bif NULL. Notify each 1475 * descriptor as it's detached so that any sleepers wake up and get 1476 * ENXIO. 1477 */ 1478 void 1479 bpfdetach(struct ifnet *ifp) 1480 { 1481 struct bpf_if *bp, *bp_prev; 1482 struct bpf_d *d; 1483 1484 lwkt_gettoken(&bpf_token); 1485 1486 /* Locate BPF interface information */ 1487 bp_prev = NULL; 1488 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1489 if (ifp == bp->bif_ifp) 1490 break; 1491 bp_prev = bp; 1492 } 1493 1494 /* Interface wasn't attached */ 1495 if (bp->bif_ifp == NULL) { 1496 lwkt_reltoken(&bpf_token); 1497 kprintf("bpfdetach: %s was not attached\n", ifp->if_xname); 1498 return; 1499 } 1500 1501 while ((d = SLIST_FIRST(&bp->bif_dlist)) != NULL) { 1502 bpf_detachd(d); 1503 bpf_wakeup(d); 1504 } 1505 1506 if (bp_prev != NULL) 1507 bp_prev->bif_next = bp->bif_next; 1508 else 1509 bpf_iflist = bp->bif_next; 1510 1511 kfree(bp, M_BPF); 1512 1513 lwkt_reltoken(&bpf_token); 1514 } 1515 1516 /* 1517 * Get a list of available data link type of the interface. 1518 */ 1519 static int 1520 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl) 1521 { 1522 int n, error; 1523 struct ifnet *ifp; 1524 struct bpf_if *bp; 1525 1526 ifp = d->bd_bif->bif_ifp; 1527 n = 0; 1528 error = 0; 1529 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1530 if (bp->bif_ifp != ifp) 1531 continue; 1532 if (bfl->bfl_list != NULL) { 1533 if (n >= bfl->bfl_len) { 1534 return (ENOMEM); 1535 } 1536 error = copyout(&bp->bif_dlt, 1537 bfl->bfl_list + n, sizeof(u_int)); 1538 } 1539 n++; 1540 } 1541 bfl->bfl_len = n; 1542 return(error); 1543 } 1544 1545 /* 1546 * Set the data link type of a BPF instance. 1547 */ 1548 static int 1549 bpf_setdlt(struct bpf_d *d, u_int dlt) 1550 { 1551 int error, opromisc; 1552 struct ifnet *ifp; 1553 struct bpf_if *bp; 1554 1555 if (d->bd_bif->bif_dlt == dlt) 1556 return (0); 1557 ifp = d->bd_bif->bif_ifp; 1558 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1559 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) 1560 break; 1561 } 1562 if (bp != NULL) { 1563 opromisc = d->bd_promisc; 1564 bpf_detachd(d); 1565 bpf_attachd(d, bp); 1566 bpf_resetd(d); 1567 if (opromisc) { 1568 error = ifpromisc(bp->bif_ifp, 1); 1569 if (error) { 1570 if_printf(bp->bif_ifp, 1571 "bpf_setdlt: ifpromisc failed (%d)\n", 1572 error); 1573 } else { 1574 d->bd_promisc = 1; 1575 } 1576 } 1577 } 1578 return(bp == NULL ? EINVAL : 0); 1579 } 1580 1581 void 1582 bpf_gettoken(void) 1583 { 1584 lwkt_gettoken(&bpf_token); 1585 } 1586 1587 void 1588 bpf_reltoken(void) 1589 { 1590 lwkt_reltoken(&bpf_token); 1591 } 1592 1593 static void 1594 bpf_drvinit(void *unused) 1595 { 1596 int i; 1597 1598 make_autoclone_dev(&bpf_ops, &DEVFS_CLONE_BITMAP(bpf), 1599 bpfclone, 0, 0, 0600, "bpf"); 1600 for (i = 0; i < BPF_PREALLOCATED_UNITS; i++) { 1601 make_dev(&bpf_ops, i, 0, 0, 0600, "bpf%d", i); 1602 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(bpf), i); 1603 } 1604 } 1605 1606 static void 1607 bpf_drvuninit(void *unused) 1608 { 1609 devfs_clone_handler_del("bpf"); 1610 dev_ops_remove_all(&bpf_ops); 1611 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(bpf)); 1612 } 1613 1614 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL) 1615 SYSUNINIT(bpfdev, SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvuninit, NULL); 1616 1617 #else /* !BPF */ 1618 /* 1619 * NOP stubs to allow bpf-using drivers to load and function. 1620 * 1621 * A 'better' implementation would allow the core bpf functionality 1622 * to be loaded at runtime. 1623 */ 1624 1625 void 1626 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 1627 { 1628 } 1629 1630 void 1631 bpf_mtap(struct bpf_if *bp, struct mbuf *m) 1632 { 1633 } 1634 1635 void 1636 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen) 1637 { 1638 } 1639 1640 void 1641 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) 1642 { 1643 } 1644 1645 void 1646 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 1647 { 1648 } 1649 1650 void 1651 bpfdetach(struct ifnet *ifp) 1652 { 1653 } 1654 1655 u_int 1656 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen) 1657 { 1658 return -1; /* "no filter" behaviour */ 1659 } 1660 1661 void 1662 bpf_gettoken(void) 1663 { 1664 } 1665 1666 void 1667 bpf_reltoken(void) 1668 { 1669 } 1670 1671 #endif /* !BPF */ 1672