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