bpf.c revision 233938
1/*- 2 * Copyright (c) 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from the Stanford/CMU enet packet filter, 6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed 7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 8 * Berkeley Laboratory. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)bpf.c 8.4 (Berkeley) 1/9/95 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: head/sys/net/bpf.c 233938 2012-04-06 06:55:21Z melifaro $"); 39 40#include "opt_bpf.h" 41#include "opt_compat.h" 42#include "opt_netgraph.h" 43 44#include <sys/types.h> 45#include <sys/param.h> 46#include <sys/lock.h> 47#include <sys/rwlock.h> 48#include <sys/systm.h> 49#include <sys/conf.h> 50#include <sys/fcntl.h> 51#include <sys/jail.h> 52#include <sys/malloc.h> 53#include <sys/mbuf.h> 54#include <sys/time.h> 55#include <sys/priv.h> 56#include <sys/proc.h> 57#include <sys/signalvar.h> 58#include <sys/filio.h> 59#include <sys/sockio.h> 60#include <sys/ttycom.h> 61#include <sys/uio.h> 62 63#include <sys/event.h> 64#include <sys/file.h> 65#include <sys/poll.h> 66#include <sys/proc.h> 67 68#include <sys/socket.h> 69 70#include <net/if.h> 71#define BPF_INTERNAL 72#include <net/bpf.h> 73#include <net/bpf_buffer.h> 74#ifdef BPF_JITTER 75#include <net/bpf_jitter.h> 76#endif 77#include <net/bpf_zerocopy.h> 78#include <net/bpfdesc.h> 79#include <net/vnet.h> 80 81#include <netinet/in.h> 82#include <netinet/if_ether.h> 83#include <sys/kernel.h> 84#include <sys/sysctl.h> 85 86#include <net80211/ieee80211_freebsd.h> 87 88#include <security/mac/mac_framework.h> 89 90MALLOC_DEFINE(M_BPF, "BPF", "BPF data"); 91 92#if defined(DEV_BPF) || defined(NETGRAPH_BPF) 93 94#define PRINET 26 /* interruptible */ 95 96#define SIZEOF_BPF_HDR(type) \ 97 (offsetof(type, bh_hdrlen) + sizeof(((type *)0)->bh_hdrlen)) 98 99#ifdef COMPAT_FREEBSD32 100#include <sys/mount.h> 101#include <compat/freebsd32/freebsd32.h> 102#define BPF_ALIGNMENT32 sizeof(int32_t) 103#define BPF_WORDALIGN32(x) (((x)+(BPF_ALIGNMENT32-1))&~(BPF_ALIGNMENT32-1)) 104 105#ifndef BURN_BRIDGES 106/* 107 * 32-bit version of structure prepended to each packet. We use this header 108 * instead of the standard one for 32-bit streams. We mark the a stream as 109 * 32-bit the first time we see a 32-bit compat ioctl request. 110 */ 111struct bpf_hdr32 { 112 struct timeval32 bh_tstamp; /* time stamp */ 113 uint32_t bh_caplen; /* length of captured portion */ 114 uint32_t bh_datalen; /* original length of packet */ 115 uint16_t bh_hdrlen; /* length of bpf header (this struct 116 plus alignment padding) */ 117}; 118#endif 119 120struct bpf_program32 { 121 u_int bf_len; 122 uint32_t bf_insns; 123}; 124 125struct bpf_dltlist32 { 126 u_int bfl_len; 127 u_int bfl_list; 128}; 129 130#define BIOCSETF32 _IOW('B', 103, struct bpf_program32) 131#define BIOCSRTIMEOUT32 _IOW('B', 109, struct timeval32) 132#define BIOCGRTIMEOUT32 _IOR('B', 110, struct timeval32) 133#define BIOCGDLTLIST32 _IOWR('B', 121, struct bpf_dltlist32) 134#define BIOCSETWF32 _IOW('B', 123, struct bpf_program32) 135#define BIOCSETFNR32 _IOW('B', 130, struct bpf_program32) 136#endif 137 138/* 139 * bpf_iflist is a list of BPF interface structures, each corresponding to a 140 * specific DLT. The same network interface might have several BPF interface 141 * structures registered by different layers in the stack (i.e., 802.11 142 * frames, ethernet frames, etc). 143 */ 144static LIST_HEAD(, bpf_if) bpf_iflist; 145static struct mtx bpf_mtx; /* bpf global lock */ 146static int bpf_bpfd_cnt; 147 148static void bpf_attachd(struct bpf_d *, struct bpf_if *); 149static void bpf_detachd(struct bpf_d *); 150static void bpf_freed(struct bpf_d *); 151static int bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **, 152 struct sockaddr *, int *, struct bpf_insn *); 153static int bpf_setif(struct bpf_d *, struct ifreq *); 154static void bpf_timed_out(void *); 155static __inline void 156 bpf_wakeup(struct bpf_d *); 157static void catchpacket(struct bpf_d *, u_char *, u_int, u_int, 158 void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int), 159 struct bintime *); 160static void reset_d(struct bpf_d *); 161static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd); 162static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); 163static int bpf_setdlt(struct bpf_d *, u_int); 164static void filt_bpfdetach(struct knote *); 165static int filt_bpfread(struct knote *, long); 166static void bpf_drvinit(void *); 167static int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS); 168 169SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl"); 170int bpf_maxinsns = BPF_MAXINSNS; 171SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW, 172 &bpf_maxinsns, 0, "Maximum bpf program instructions"); 173static int bpf_zerocopy_enable = 0; 174SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW, 175 &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions"); 176static SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW, 177 bpf_stats_sysctl, "bpf statistics portal"); 178 179static VNET_DEFINE(int, bpf_optimize_writers) = 0; 180#define V_bpf_optimize_writers VNET(bpf_optimize_writers) 181SYSCTL_VNET_INT(_net_bpf, OID_AUTO, optimize_writers, 182 CTLFLAG_RW, &VNET_NAME(bpf_optimize_writers), 0, 183 "Do not send packets until BPF program is set"); 184 185static d_open_t bpfopen; 186static d_read_t bpfread; 187static d_write_t bpfwrite; 188static d_ioctl_t bpfioctl; 189static d_poll_t bpfpoll; 190static d_kqfilter_t bpfkqfilter; 191 192static struct cdevsw bpf_cdevsw = { 193 .d_version = D_VERSION, 194 .d_open = bpfopen, 195 .d_read = bpfread, 196 .d_write = bpfwrite, 197 .d_ioctl = bpfioctl, 198 .d_poll = bpfpoll, 199 .d_name = "bpf", 200 .d_kqfilter = bpfkqfilter, 201}; 202 203static struct filterops bpfread_filtops = { 204 .f_isfd = 1, 205 .f_detach = filt_bpfdetach, 206 .f_event = filt_bpfread, 207}; 208 209/* 210 * Wrapper functions for various buffering methods. If the set of buffer 211 * modes expands, we will probably want to introduce a switch data structure 212 * similar to protosw, et. 213 */ 214static void 215bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src, 216 u_int len) 217{ 218 219 BPFD_WLOCK_ASSERT(d); 220 221 switch (d->bd_bufmode) { 222 case BPF_BUFMODE_BUFFER: 223 return (bpf_buffer_append_bytes(d, buf, offset, src, len)); 224 225 case BPF_BUFMODE_ZBUF: 226 d->bd_zcopy++; 227 return (bpf_zerocopy_append_bytes(d, buf, offset, src, len)); 228 229 default: 230 panic("bpf_buf_append_bytes"); 231 } 232} 233 234static void 235bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src, 236 u_int len) 237{ 238 239 BPFD_WLOCK_ASSERT(d); 240 241 switch (d->bd_bufmode) { 242 case BPF_BUFMODE_BUFFER: 243 return (bpf_buffer_append_mbuf(d, buf, offset, src, len)); 244 245 case BPF_BUFMODE_ZBUF: 246 d->bd_zcopy++; 247 return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len)); 248 249 default: 250 panic("bpf_buf_append_mbuf"); 251 } 252} 253 254/* 255 * This function gets called when the free buffer is re-assigned. 256 */ 257static void 258bpf_buf_reclaimed(struct bpf_d *d) 259{ 260 261 BPFD_WLOCK_ASSERT(d); 262 263 switch (d->bd_bufmode) { 264 case BPF_BUFMODE_BUFFER: 265 return; 266 267 case BPF_BUFMODE_ZBUF: 268 bpf_zerocopy_buf_reclaimed(d); 269 return; 270 271 default: 272 panic("bpf_buf_reclaimed"); 273 } 274} 275 276/* 277 * If the buffer mechanism has a way to decide that a held buffer can be made 278 * free, then it is exposed via the bpf_canfreebuf() interface. (1) is 279 * returned if the buffer can be discarded, (0) is returned if it cannot. 280 */ 281static int 282bpf_canfreebuf(struct bpf_d *d) 283{ 284 285 BPFD_LOCK_ASSERT(d); 286 287 switch (d->bd_bufmode) { 288 case BPF_BUFMODE_ZBUF: 289 return (bpf_zerocopy_canfreebuf(d)); 290 } 291 return (0); 292} 293 294/* 295 * Allow the buffer model to indicate that the current store buffer is 296 * immutable, regardless of the appearance of space. Return (1) if the 297 * buffer is writable, and (0) if not. 298 */ 299static int 300bpf_canwritebuf(struct bpf_d *d) 301{ 302 BPFD_LOCK_ASSERT(d); 303 304 switch (d->bd_bufmode) { 305 case BPF_BUFMODE_ZBUF: 306 return (bpf_zerocopy_canwritebuf(d)); 307 } 308 return (1); 309} 310 311/* 312 * Notify buffer model that an attempt to write to the store buffer has 313 * resulted in a dropped packet, in which case the buffer may be considered 314 * full. 315 */ 316static void 317bpf_buffull(struct bpf_d *d) 318{ 319 320 BPFD_WLOCK_ASSERT(d); 321 322 switch (d->bd_bufmode) { 323 case BPF_BUFMODE_ZBUF: 324 bpf_zerocopy_buffull(d); 325 break; 326 } 327} 328 329/* 330 * Notify the buffer model that a buffer has moved into the hold position. 331 */ 332void 333bpf_bufheld(struct bpf_d *d) 334{ 335 336 BPFD_WLOCK_ASSERT(d); 337 338 switch (d->bd_bufmode) { 339 case BPF_BUFMODE_ZBUF: 340 bpf_zerocopy_bufheld(d); 341 break; 342 } 343} 344 345static void 346bpf_free(struct bpf_d *d) 347{ 348 349 switch (d->bd_bufmode) { 350 case BPF_BUFMODE_BUFFER: 351 return (bpf_buffer_free(d)); 352 353 case BPF_BUFMODE_ZBUF: 354 return (bpf_zerocopy_free(d)); 355 356 default: 357 panic("bpf_buf_free"); 358 } 359} 360 361static int 362bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio) 363{ 364 365 if (d->bd_bufmode != BPF_BUFMODE_BUFFER) 366 return (EOPNOTSUPP); 367 return (bpf_buffer_uiomove(d, buf, len, uio)); 368} 369 370static int 371bpf_ioctl_sblen(struct bpf_d *d, u_int *i) 372{ 373 374 if (d->bd_bufmode != BPF_BUFMODE_BUFFER) 375 return (EOPNOTSUPP); 376 return (bpf_buffer_ioctl_sblen(d, i)); 377} 378 379static int 380bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i) 381{ 382 383 if (d->bd_bufmode != BPF_BUFMODE_ZBUF) 384 return (EOPNOTSUPP); 385 return (bpf_zerocopy_ioctl_getzmax(td, d, i)); 386} 387 388static int 389bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz) 390{ 391 392 if (d->bd_bufmode != BPF_BUFMODE_ZBUF) 393 return (EOPNOTSUPP); 394 return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz)); 395} 396 397static int 398bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz) 399{ 400 401 if (d->bd_bufmode != BPF_BUFMODE_ZBUF) 402 return (EOPNOTSUPP); 403 return (bpf_zerocopy_ioctl_setzbuf(td, d, bz)); 404} 405 406/* 407 * General BPF functions. 408 */ 409static int 410bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp, 411 struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter) 412{ 413 const struct ieee80211_bpf_params *p; 414 struct ether_header *eh; 415 struct mbuf *m; 416 int error; 417 int len; 418 int hlen; 419 int slen; 420 421 /* 422 * Build a sockaddr based on the data link layer type. 423 * We do this at this level because the ethernet header 424 * is copied directly into the data field of the sockaddr. 425 * In the case of SLIP, there is no header and the packet 426 * is forwarded as is. 427 * Also, we are careful to leave room at the front of the mbuf 428 * for the link level header. 429 */ 430 switch (linktype) { 431 432 case DLT_SLIP: 433 sockp->sa_family = AF_INET; 434 hlen = 0; 435 break; 436 437 case DLT_EN10MB: 438 sockp->sa_family = AF_UNSPEC; 439 /* XXX Would MAXLINKHDR be better? */ 440 hlen = ETHER_HDR_LEN; 441 break; 442 443 case DLT_FDDI: 444 sockp->sa_family = AF_IMPLINK; 445 hlen = 0; 446 break; 447 448 case DLT_RAW: 449 sockp->sa_family = AF_UNSPEC; 450 hlen = 0; 451 break; 452 453 case DLT_NULL: 454 /* 455 * null interface types require a 4 byte pseudo header which 456 * corresponds to the address family of the packet. 457 */ 458 sockp->sa_family = AF_UNSPEC; 459 hlen = 4; 460 break; 461 462 case DLT_ATM_RFC1483: 463 /* 464 * en atm driver requires 4-byte atm pseudo header. 465 * though it isn't standard, vpi:vci needs to be 466 * specified anyway. 467 */ 468 sockp->sa_family = AF_UNSPEC; 469 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ 470 break; 471 472 case DLT_PPP: 473 sockp->sa_family = AF_UNSPEC; 474 hlen = 4; /* This should match PPP_HDRLEN */ 475 break; 476 477 case DLT_IEEE802_11: /* IEEE 802.11 wireless */ 478 sockp->sa_family = AF_IEEE80211; 479 hlen = 0; 480 break; 481 482 case DLT_IEEE802_11_RADIO: /* IEEE 802.11 wireless w/ phy params */ 483 sockp->sa_family = AF_IEEE80211; 484 sockp->sa_len = 12; /* XXX != 0 */ 485 hlen = sizeof(struct ieee80211_bpf_params); 486 break; 487 488 default: 489 return (EIO); 490 } 491 492 len = uio->uio_resid; 493 494 if (len - hlen > ifp->if_mtu) 495 return (EMSGSIZE); 496 497 if ((unsigned)len > MJUM16BYTES) 498 return (EIO); 499 500 if (len <= MHLEN) 501 MGETHDR(m, M_WAIT, MT_DATA); 502 else if (len <= MCLBYTES) 503 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR); 504 else 505 m = m_getjcl(M_WAIT, MT_DATA, M_PKTHDR, 506#if (MJUMPAGESIZE > MCLBYTES) 507 len <= MJUMPAGESIZE ? MJUMPAGESIZE : 508#endif 509 (len <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES)); 510 m->m_pkthdr.len = m->m_len = len; 511 m->m_pkthdr.rcvif = NULL; 512 *mp = m; 513 514 if (m->m_len < hlen) { 515 error = EPERM; 516 goto bad; 517 } 518 519 error = uiomove(mtod(m, u_char *), len, uio); 520 if (error) 521 goto bad; 522 523 slen = bpf_filter(wfilter, mtod(m, u_char *), len, len); 524 if (slen == 0) { 525 error = EPERM; 526 goto bad; 527 } 528 529 /* Check for multicast destination */ 530 switch (linktype) { 531 case DLT_EN10MB: 532 eh = mtod(m, struct ether_header *); 533 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 534 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost, 535 ETHER_ADDR_LEN) == 0) 536 m->m_flags |= M_BCAST; 537 else 538 m->m_flags |= M_MCAST; 539 } 540 break; 541 } 542 543 /* 544 * Make room for link header, and copy it to sockaddr 545 */ 546 if (hlen != 0) { 547 if (sockp->sa_family == AF_IEEE80211) { 548 /* 549 * Collect true length from the parameter header 550 * NB: sockp is known to be zero'd so if we do a 551 * short copy unspecified parameters will be 552 * zero. 553 * NB: packet may not be aligned after stripping 554 * bpf params 555 * XXX check ibp_vers 556 */ 557 p = mtod(m, const struct ieee80211_bpf_params *); 558 hlen = p->ibp_len; 559 if (hlen > sizeof(sockp->sa_data)) { 560 error = EINVAL; 561 goto bad; 562 } 563 } 564 bcopy(m->m_data, sockp->sa_data, hlen); 565 } 566 *hdrlen = hlen; 567 568 return (0); 569bad: 570 m_freem(m); 571 return (error); 572} 573 574/* 575 * Attach file to the bpf interface, i.e. make d listen on bp. 576 */ 577static void 578bpf_attachd(struct bpf_d *d, struct bpf_if *bp) 579{ 580 /* 581 * Point d at bp, and add d to the interface's list. 582 * Since there are many applicaiotns using BPF for 583 * sending raw packets only (dhcpd, cdpd are good examples) 584 * we can delay adding d to the list of active listeners until 585 * some filter is configured. 586 */ 587 d->bd_bif = bp; 588 589 BPFIF_WLOCK(bp); 590 591 if (V_bpf_optimize_writers != 0) { 592 /* Add to writers-only list */ 593 LIST_INSERT_HEAD(&bp->bif_wlist, d, bd_next); 594 /* 595 * We decrement bd_writer on every filter set operation. 596 * First BIOCSETF is done by pcap_open_live() to set up 597 * snap length. After that appliation usually sets its own filter 598 */ 599 d->bd_writer = 2; 600 } else 601 LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); 602 603 BPFIF_WUNLOCK(bp); 604 605 BPF_LOCK(); 606 bpf_bpfd_cnt++; 607 BPF_UNLOCK(); 608 609 CTR3(KTR_NET, "%s: bpf_attach called by pid %d, adding to %s list", 610 __func__, d->bd_pid, d->bd_writer ? "writer" : "active"); 611 612 if (V_bpf_optimize_writers == 0) 613 EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1); 614} 615 616/* 617 * Add d to the list of active bp filters. 618 * Reuqires bpf_attachd() to be called before 619 */ 620static void 621bpf_upgraded(struct bpf_d *d) 622{ 623 struct bpf_if *bp; 624 625 bp = d->bd_bif; 626 627 BPFIF_WLOCK(bp); 628 BPFD_WLOCK(d); 629 630 /* Remove from writers-only list */ 631 LIST_REMOVE(d, bd_next); 632 LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); 633 /* Mark d as reader */ 634 d->bd_writer = 0; 635 636 BPFD_WUNLOCK(d); 637 BPFIF_WUNLOCK(bp); 638 639 CTR2(KTR_NET, "%s: upgrade required by pid %d", __func__, d->bd_pid); 640 641 EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1); 642} 643 644/* 645 * Detach a file from its interface. 646 */ 647static void 648bpf_detachd(struct bpf_d *d) 649{ 650 int error; 651 struct bpf_if *bp; 652 struct ifnet *ifp; 653 654 CTR2(KTR_NET, "%s: detach required by pid %d", __func__, d->bd_pid); 655 656 BPF_LOCK_ASSERT(); 657 658 bp = d->bd_bif; 659 BPFIF_WLOCK(bp); 660 BPFD_WLOCK(d); 661 662 /* Save bd_writer value */ 663 error = d->bd_writer; 664 665 /* 666 * Remove d from the interface's descriptor list. 667 */ 668 LIST_REMOVE(d, bd_next); 669 670 ifp = bp->bif_ifp; 671 d->bd_bif = NULL; 672 BPFD_WUNLOCK(d); 673 BPFIF_WUNLOCK(bp); 674 675 /* We're already protected by global lock. */ 676 bpf_bpfd_cnt--; 677 678 /* Call event handler iff d is attached */ 679 if (error == 0) 680 EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0); 681 682 /* 683 * Check if this descriptor had requested promiscuous mode. 684 * If so, turn it off. 685 */ 686 if (d->bd_promisc) { 687 d->bd_promisc = 0; 688 CURVNET_SET(ifp->if_vnet); 689 error = ifpromisc(ifp, 0); 690 CURVNET_RESTORE(); 691 if (error != 0 && error != ENXIO) { 692 /* 693 * ENXIO can happen if a pccard is unplugged 694 * Something is really wrong if we were able to put 695 * the driver into promiscuous mode, but can't 696 * take it out. 697 */ 698 if_printf(bp->bif_ifp, 699 "bpf_detach: ifpromisc failed (%d)\n", error); 700 } 701 } 702} 703 704/* 705 * Close the descriptor by detaching it from its interface, 706 * deallocating its buffers, and marking it free. 707 */ 708static void 709bpf_dtor(void *data) 710{ 711 struct bpf_d *d = data; 712 713 BPFD_WLOCK(d); 714 if (d->bd_state == BPF_WAITING) 715 callout_stop(&d->bd_callout); 716 d->bd_state = BPF_IDLE; 717 BPFD_WUNLOCK(d); 718 funsetown(&d->bd_sigio); 719 BPF_LOCK(); 720 if (d->bd_bif) 721 bpf_detachd(d); 722 BPF_UNLOCK(); 723#ifdef MAC 724 mac_bpfdesc_destroy(d); 725#endif /* MAC */ 726 seldrain(&d->bd_sel); 727 knlist_destroy(&d->bd_sel.si_note); 728 callout_drain(&d->bd_callout); 729 bpf_freed(d); 730 free(d, M_BPF); 731} 732 733/* 734 * Open ethernet device. Returns ENXIO for illegal minor device number, 735 * EBUSY if file is open by another process. 736 */ 737/* ARGSUSED */ 738static int 739bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td) 740{ 741 struct bpf_d *d; 742 int error; 743 744 d = malloc(sizeof(*d), M_BPF, M_WAITOK | M_ZERO); 745 error = devfs_set_cdevpriv(d, bpf_dtor); 746 if (error != 0) { 747 free(d, M_BPF); 748 return (error); 749 } 750 751 /* 752 * For historical reasons, perform a one-time initialization call to 753 * the buffer routines, even though we're not yet committed to a 754 * particular buffer method. 755 */ 756 bpf_buffer_init(d); 757 d->bd_bufmode = BPF_BUFMODE_BUFFER; 758 d->bd_sig = SIGIO; 759 d->bd_direction = BPF_D_INOUT; 760 BPF_PID_REFRESH(d, td); 761#ifdef MAC 762 mac_bpfdesc_init(d); 763 mac_bpfdesc_create(td->td_ucred, d); 764#endif 765 rw_init(&d->bd_lock, "bpf cdev lock"); 766 callout_init_rw(&d->bd_callout, &d->bd_lock, 0); 767 knlist_init_rw_reader(&d->bd_sel.si_note, &d->bd_lock); 768 769 return (0); 770} 771 772/* 773 * bpfread - read next chunk of packets from buffers 774 */ 775static int 776bpfread(struct cdev *dev, struct uio *uio, int ioflag) 777{ 778 struct bpf_d *d; 779 int error; 780 int non_block; 781 int timed_out; 782 783 error = devfs_get_cdevpriv((void **)&d); 784 if (error != 0) 785 return (error); 786 787 /* 788 * Restrict application to use a buffer the same size as 789 * as kernel buffers. 790 */ 791 if (uio->uio_resid != d->bd_bufsize) 792 return (EINVAL); 793 794 non_block = ((ioflag & O_NONBLOCK) != 0); 795 796 BPFD_WLOCK(d); 797 BPF_PID_REFRESH_CUR(d); 798 if (d->bd_bufmode != BPF_BUFMODE_BUFFER) { 799 BPFD_WUNLOCK(d); 800 return (EOPNOTSUPP); 801 } 802 if (d->bd_state == BPF_WAITING) 803 callout_stop(&d->bd_callout); 804 timed_out = (d->bd_state == BPF_TIMED_OUT); 805 d->bd_state = BPF_IDLE; 806 /* 807 * If the hold buffer is empty, then do a timed sleep, which 808 * ends when the timeout expires or when enough packets 809 * have arrived to fill the store buffer. 810 */ 811 while (d->bd_hbuf == NULL) { 812 if (d->bd_slen != 0) { 813 /* 814 * A packet(s) either arrived since the previous 815 * read or arrived while we were asleep. 816 */ 817 if (d->bd_immediate || non_block || timed_out) { 818 /* 819 * Rotate the buffers and return what's here 820 * if we are in immediate mode, non-blocking 821 * flag is set, or this descriptor timed out. 822 */ 823 ROTATE_BUFFERS(d); 824 break; 825 } 826 } 827 828 /* 829 * No data is available, check to see if the bpf device 830 * is still pointed at a real interface. If not, return 831 * ENXIO so that the userland process knows to rebind 832 * it before using it again. 833 */ 834 if (d->bd_bif == NULL) { 835 BPFD_WUNLOCK(d); 836 return (ENXIO); 837 } 838 839 if (non_block) { 840 BPFD_WUNLOCK(d); 841 return (EWOULDBLOCK); 842 } 843 error = rw_sleep(d, &d->bd_lock, PRINET|PCATCH, 844 "bpf", d->bd_rtout); 845 if (error == EINTR || error == ERESTART) { 846 BPFD_WUNLOCK(d); 847 return (error); 848 } 849 if (error == EWOULDBLOCK) { 850 /* 851 * On a timeout, return what's in the buffer, 852 * which may be nothing. If there is something 853 * in the store buffer, we can rotate the buffers. 854 */ 855 if (d->bd_hbuf) 856 /* 857 * We filled up the buffer in between 858 * getting the timeout and arriving 859 * here, so we don't need to rotate. 860 */ 861 break; 862 863 if (d->bd_slen == 0) { 864 BPFD_WUNLOCK(d); 865 return (0); 866 } 867 ROTATE_BUFFERS(d); 868 break; 869 } 870 } 871 /* 872 * At this point, we know we have something in the hold slot. 873 */ 874 BPFD_WUNLOCK(d); 875 876 /* 877 * Move data from hold buffer into user space. 878 * We know the entire buffer is transferred since 879 * we checked above that the read buffer is bpf_bufsize bytes. 880 * 881 * XXXRW: More synchronization needed here: what if a second thread 882 * issues a read on the same fd at the same time? Don't want this 883 * getting invalidated. 884 */ 885 error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio); 886 887 BPFD_WLOCK(d); 888 d->bd_fbuf = d->bd_hbuf; 889 d->bd_hbuf = NULL; 890 d->bd_hlen = 0; 891 bpf_buf_reclaimed(d); 892 BPFD_WUNLOCK(d); 893 894 return (error); 895} 896 897/* 898 * If there are processes sleeping on this descriptor, wake them up. 899 */ 900static __inline void 901bpf_wakeup(struct bpf_d *d) 902{ 903 904 BPFD_WLOCK_ASSERT(d); 905 if (d->bd_state == BPF_WAITING) { 906 callout_stop(&d->bd_callout); 907 d->bd_state = BPF_IDLE; 908 } 909 wakeup(d); 910 if (d->bd_async && d->bd_sig && d->bd_sigio) 911 pgsigio(&d->bd_sigio, d->bd_sig, 0); 912 913 selwakeuppri(&d->bd_sel, PRINET); 914 KNOTE_LOCKED(&d->bd_sel.si_note, 0); 915} 916 917static void 918bpf_timed_out(void *arg) 919{ 920 struct bpf_d *d = (struct bpf_d *)arg; 921 922 BPFD_WLOCK_ASSERT(d); 923 924 if (callout_pending(&d->bd_callout) || !callout_active(&d->bd_callout)) 925 return; 926 if (d->bd_state == BPF_WAITING) { 927 d->bd_state = BPF_TIMED_OUT; 928 if (d->bd_slen != 0) 929 bpf_wakeup(d); 930 } 931} 932 933static int 934bpf_ready(struct bpf_d *d) 935{ 936 937 BPFD_WLOCK_ASSERT(d); 938 939 if (!bpf_canfreebuf(d) && d->bd_hlen != 0) 940 return (1); 941 if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) && 942 d->bd_slen != 0) 943 return (1); 944 return (0); 945} 946 947static int 948bpfwrite(struct cdev *dev, struct uio *uio, int ioflag) 949{ 950 struct bpf_d *d; 951 struct ifnet *ifp; 952 struct mbuf *m, *mc; 953 struct sockaddr dst; 954 int error, hlen; 955 956 error = devfs_get_cdevpriv((void **)&d); 957 if (error != 0) 958 return (error); 959 960 BPF_PID_REFRESH_CUR(d); 961 d->bd_wcount++; 962 if (d->bd_bif == NULL) { 963 d->bd_wdcount++; 964 return (ENXIO); 965 } 966 967 ifp = d->bd_bif->bif_ifp; 968 969 if ((ifp->if_flags & IFF_UP) == 0) { 970 d->bd_wdcount++; 971 return (ENETDOWN); 972 } 973 974 if (uio->uio_resid == 0) { 975 d->bd_wdcount++; 976 return (0); 977 } 978 979 bzero(&dst, sizeof(dst)); 980 m = NULL; 981 hlen = 0; 982 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp, 983 &m, &dst, &hlen, d->bd_wfilter); 984 if (error) { 985 d->bd_wdcount++; 986 return (error); 987 } 988 d->bd_wfcount++; 989 if (d->bd_hdrcmplt) 990 dst.sa_family = pseudo_AF_HDRCMPLT; 991 992 if (d->bd_feedback) { 993 mc = m_dup(m, M_DONTWAIT); 994 if (mc != NULL) 995 mc->m_pkthdr.rcvif = ifp; 996 /* Set M_PROMISC for outgoing packets to be discarded. */ 997 if (d->bd_direction == BPF_D_INOUT) 998 m->m_flags |= M_PROMISC; 999 } else 1000 mc = NULL; 1001 1002 m->m_pkthdr.len -= hlen; 1003 m->m_len -= hlen; 1004 m->m_data += hlen; /* XXX */ 1005 1006 CURVNET_SET(ifp->if_vnet); 1007#ifdef MAC 1008 BPFD_WLOCK(d); 1009 mac_bpfdesc_create_mbuf(d, m); 1010 if (mc != NULL) 1011 mac_bpfdesc_create_mbuf(d, mc); 1012 BPFD_WUNLOCK(d); 1013#endif 1014 1015 error = (*ifp->if_output)(ifp, m, &dst, NULL); 1016 if (error) 1017 d->bd_wdcount++; 1018 1019 if (mc != NULL) { 1020 if (error == 0) 1021 (*ifp->if_input)(ifp, mc); 1022 else 1023 m_freem(mc); 1024 } 1025 CURVNET_RESTORE(); 1026 1027 return (error); 1028} 1029 1030/* 1031 * Reset a descriptor by flushing its packet buffer and clearing the receive 1032 * and drop counts. This is doable for kernel-only buffers, but with 1033 * zero-copy buffers, we can't write to (or rotate) buffers that are 1034 * currently owned by userspace. It would be nice if we could encapsulate 1035 * this logic in the buffer code rather than here. 1036 */ 1037static void 1038reset_d(struct bpf_d *d) 1039{ 1040 1041 BPFD_WLOCK_ASSERT(d); 1042 1043 if ((d->bd_hbuf != NULL) && 1044 (d->bd_bufmode != BPF_BUFMODE_ZBUF || bpf_canfreebuf(d))) { 1045 /* Free the hold buffer. */ 1046 d->bd_fbuf = d->bd_hbuf; 1047 d->bd_hbuf = NULL; 1048 d->bd_hlen = 0; 1049 bpf_buf_reclaimed(d); 1050 } 1051 if (bpf_canwritebuf(d)) 1052 d->bd_slen = 0; 1053 d->bd_rcount = 0; 1054 d->bd_dcount = 0; 1055 d->bd_fcount = 0; 1056 d->bd_wcount = 0; 1057 d->bd_wfcount = 0; 1058 d->bd_wdcount = 0; 1059 d->bd_zcopy = 0; 1060} 1061 1062/* 1063 * FIONREAD Check for read packet available. 1064 * SIOCGIFADDR Get interface address - convenient hook to driver. 1065 * BIOCGBLEN Get buffer len [for read()]. 1066 * BIOCSETF Set read filter. 1067 * BIOCSETFNR Set read filter without resetting descriptor. 1068 * BIOCSETWF Set write filter. 1069 * BIOCFLUSH Flush read packet buffer. 1070 * BIOCPROMISC Put interface into promiscuous mode. 1071 * BIOCGDLT Get link layer type. 1072 * BIOCGETIF Get interface name. 1073 * BIOCSETIF Set interface. 1074 * BIOCSRTIMEOUT Set read timeout. 1075 * BIOCGRTIMEOUT Get read timeout. 1076 * BIOCGSTATS Get packet stats. 1077 * BIOCIMMEDIATE Set immediate mode. 1078 * BIOCVERSION Get filter language version. 1079 * BIOCGHDRCMPLT Get "header already complete" flag 1080 * BIOCSHDRCMPLT Set "header already complete" flag 1081 * BIOCGDIRECTION Get packet direction flag 1082 * BIOCSDIRECTION Set packet direction flag 1083 * BIOCGTSTAMP Get time stamp format and resolution. 1084 * BIOCSTSTAMP Set time stamp format and resolution. 1085 * BIOCLOCK Set "locked" flag 1086 * BIOCFEEDBACK Set packet feedback mode. 1087 * BIOCSETZBUF Set current zero-copy buffer locations. 1088 * BIOCGETZMAX Get maximum zero-copy buffer size. 1089 * BIOCROTZBUF Force rotation of zero-copy buffer 1090 * BIOCSETBUFMODE Set buffer mode. 1091 * BIOCGETBUFMODE Get current buffer mode. 1092 */ 1093/* ARGSUSED */ 1094static int 1095bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, 1096 struct thread *td) 1097{ 1098 struct bpf_d *d; 1099 int error; 1100 1101 error = devfs_get_cdevpriv((void **)&d); 1102 if (error != 0) 1103 return (error); 1104 1105 /* 1106 * Refresh PID associated with this descriptor. 1107 */ 1108 BPFD_WLOCK(d); 1109 BPF_PID_REFRESH(d, td); 1110 if (d->bd_state == BPF_WAITING) 1111 callout_stop(&d->bd_callout); 1112 d->bd_state = BPF_IDLE; 1113 BPFD_WUNLOCK(d); 1114 1115 if (d->bd_locked == 1) { 1116 switch (cmd) { 1117 case BIOCGBLEN: 1118 case BIOCFLUSH: 1119 case BIOCGDLT: 1120 case BIOCGDLTLIST: 1121#ifdef COMPAT_FREEBSD32 1122 case BIOCGDLTLIST32: 1123#endif 1124 case BIOCGETIF: 1125 case BIOCGRTIMEOUT: 1126#if defined(COMPAT_FREEBSD32) && !defined(__mips__) 1127 case BIOCGRTIMEOUT32: 1128#endif 1129 case BIOCGSTATS: 1130 case BIOCVERSION: 1131 case BIOCGRSIG: 1132 case BIOCGHDRCMPLT: 1133 case BIOCSTSTAMP: 1134 case BIOCFEEDBACK: 1135 case FIONREAD: 1136 case BIOCLOCK: 1137 case BIOCSRTIMEOUT: 1138#if defined(COMPAT_FREEBSD32) && !defined(__mips__) 1139 case BIOCSRTIMEOUT32: 1140#endif 1141 case BIOCIMMEDIATE: 1142 case TIOCGPGRP: 1143 case BIOCROTZBUF: 1144 break; 1145 default: 1146 return (EPERM); 1147 } 1148 } 1149#ifdef COMPAT_FREEBSD32 1150 /* 1151 * If we see a 32-bit compat ioctl, mark the stream as 32-bit so 1152 * that it will get 32-bit packet headers. 1153 */ 1154 switch (cmd) { 1155 case BIOCSETF32: 1156 case BIOCSETFNR32: 1157 case BIOCSETWF32: 1158 case BIOCGDLTLIST32: 1159 case BIOCGRTIMEOUT32: 1160 case BIOCSRTIMEOUT32: 1161 d->bd_compat32 = 1; 1162 } 1163#endif 1164 1165 CURVNET_SET(TD_TO_VNET(td)); 1166 switch (cmd) { 1167 1168 default: 1169 error = EINVAL; 1170 break; 1171 1172 /* 1173 * Check for read packet available. 1174 */ 1175 case FIONREAD: 1176 { 1177 int n; 1178 1179 BPFD_WLOCK(d); 1180 n = d->bd_slen; 1181 if (d->bd_hbuf) 1182 n += d->bd_hlen; 1183 BPFD_WUNLOCK(d); 1184 1185 *(int *)addr = n; 1186 break; 1187 } 1188 1189 case SIOCGIFADDR: 1190 { 1191 struct ifnet *ifp; 1192 1193 if (d->bd_bif == NULL) 1194 error = EINVAL; 1195 else { 1196 ifp = d->bd_bif->bif_ifp; 1197 error = (*ifp->if_ioctl)(ifp, cmd, addr); 1198 } 1199 break; 1200 } 1201 1202 /* 1203 * Get buffer len [for read()]. 1204 */ 1205 case BIOCGBLEN: 1206 *(u_int *)addr = d->bd_bufsize; 1207 break; 1208 1209 /* 1210 * Set buffer length. 1211 */ 1212 case BIOCSBLEN: 1213 error = bpf_ioctl_sblen(d, (u_int *)addr); 1214 break; 1215 1216 /* 1217 * Set link layer read filter. 1218 */ 1219 case BIOCSETF: 1220 case BIOCSETFNR: 1221 case BIOCSETWF: 1222#ifdef COMPAT_FREEBSD32 1223 case BIOCSETF32: 1224 case BIOCSETFNR32: 1225 case BIOCSETWF32: 1226#endif 1227 error = bpf_setf(d, (struct bpf_program *)addr, cmd); 1228 break; 1229 1230 /* 1231 * Flush read packet buffer. 1232 */ 1233 case BIOCFLUSH: 1234 BPFD_WLOCK(d); 1235 reset_d(d); 1236 BPFD_WUNLOCK(d); 1237 break; 1238 1239 /* 1240 * Put interface into promiscuous mode. 1241 */ 1242 case BIOCPROMISC: 1243 if (d->bd_bif == NULL) { 1244 /* 1245 * No interface attached yet. 1246 */ 1247 error = EINVAL; 1248 break; 1249 } 1250 if (d->bd_promisc == 0) { 1251 error = ifpromisc(d->bd_bif->bif_ifp, 1); 1252 if (error == 0) 1253 d->bd_promisc = 1; 1254 } 1255 break; 1256 1257 /* 1258 * Get current data link type. 1259 */ 1260 case BIOCGDLT: 1261 if (d->bd_bif == NULL) 1262 error = EINVAL; 1263 else 1264 *(u_int *)addr = d->bd_bif->bif_dlt; 1265 break; 1266 1267 /* 1268 * Get a list of supported data link types. 1269 */ 1270#ifdef COMPAT_FREEBSD32 1271 case BIOCGDLTLIST32: 1272 { 1273 struct bpf_dltlist32 *list32; 1274 struct bpf_dltlist dltlist; 1275 1276 list32 = (struct bpf_dltlist32 *)addr; 1277 dltlist.bfl_len = list32->bfl_len; 1278 dltlist.bfl_list = PTRIN(list32->bfl_list); 1279 if (d->bd_bif == NULL) 1280 error = EINVAL; 1281 else { 1282 error = bpf_getdltlist(d, &dltlist); 1283 if (error == 0) 1284 list32->bfl_len = dltlist.bfl_len; 1285 } 1286 break; 1287 } 1288#endif 1289 1290 case BIOCGDLTLIST: 1291 if (d->bd_bif == NULL) 1292 error = EINVAL; 1293 else 1294 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr); 1295 break; 1296 1297 /* 1298 * Set data link type. 1299 */ 1300 case BIOCSDLT: 1301 if (d->bd_bif == NULL) 1302 error = EINVAL; 1303 else 1304 error = bpf_setdlt(d, *(u_int *)addr); 1305 break; 1306 1307 /* 1308 * Get interface name. 1309 */ 1310 case BIOCGETIF: 1311 if (d->bd_bif == NULL) 1312 error = EINVAL; 1313 else { 1314 struct ifnet *const ifp = d->bd_bif->bif_ifp; 1315 struct ifreq *const ifr = (struct ifreq *)addr; 1316 1317 strlcpy(ifr->ifr_name, ifp->if_xname, 1318 sizeof(ifr->ifr_name)); 1319 } 1320 break; 1321 1322 /* 1323 * Set interface. 1324 */ 1325 case BIOCSETIF: 1326 error = bpf_setif(d, (struct ifreq *)addr); 1327 break; 1328 1329 /* 1330 * Set read timeout. 1331 */ 1332 case BIOCSRTIMEOUT: 1333#if defined(COMPAT_FREEBSD32) && !defined(__mips__) 1334 case BIOCSRTIMEOUT32: 1335#endif 1336 { 1337 struct timeval *tv = (struct timeval *)addr; 1338#if defined(COMPAT_FREEBSD32) && !defined(__mips__) 1339 struct timeval32 *tv32; 1340 struct timeval tv64; 1341 1342 if (cmd == BIOCSRTIMEOUT32) { 1343 tv32 = (struct timeval32 *)addr; 1344 tv = &tv64; 1345 tv->tv_sec = tv32->tv_sec; 1346 tv->tv_usec = tv32->tv_usec; 1347 } else 1348#endif 1349 tv = (struct timeval *)addr; 1350 1351 /* 1352 * Subtract 1 tick from tvtohz() since this isn't 1353 * a one-shot timer. 1354 */ 1355 if ((error = itimerfix(tv)) == 0) 1356 d->bd_rtout = tvtohz(tv) - 1; 1357 break; 1358 } 1359 1360 /* 1361 * Get read timeout. 1362 */ 1363 case BIOCGRTIMEOUT: 1364#if defined(COMPAT_FREEBSD32) && !defined(__mips__) 1365 case BIOCGRTIMEOUT32: 1366#endif 1367 { 1368 struct timeval *tv; 1369#if defined(COMPAT_FREEBSD32) && !defined(__mips__) 1370 struct timeval32 *tv32; 1371 struct timeval tv64; 1372 1373 if (cmd == BIOCGRTIMEOUT32) 1374 tv = &tv64; 1375 else 1376#endif 1377 tv = (struct timeval *)addr; 1378 1379 tv->tv_sec = d->bd_rtout / hz; 1380 tv->tv_usec = (d->bd_rtout % hz) * tick; 1381#if defined(COMPAT_FREEBSD32) && !defined(__mips__) 1382 if (cmd == BIOCGRTIMEOUT32) { 1383 tv32 = (struct timeval32 *)addr; 1384 tv32->tv_sec = tv->tv_sec; 1385 tv32->tv_usec = tv->tv_usec; 1386 } 1387#endif 1388 1389 break; 1390 } 1391 1392 /* 1393 * Get packet stats. 1394 */ 1395 case BIOCGSTATS: 1396 { 1397 struct bpf_stat *bs = (struct bpf_stat *)addr; 1398 1399 /* XXXCSJP overflow */ 1400 bs->bs_recv = d->bd_rcount; 1401 bs->bs_drop = d->bd_dcount; 1402 break; 1403 } 1404 1405 /* 1406 * Set immediate mode. 1407 */ 1408 case BIOCIMMEDIATE: 1409 d->bd_immediate = *(u_int *)addr; 1410 break; 1411 1412 case BIOCVERSION: 1413 { 1414 struct bpf_version *bv = (struct bpf_version *)addr; 1415 1416 bv->bv_major = BPF_MAJOR_VERSION; 1417 bv->bv_minor = BPF_MINOR_VERSION; 1418 break; 1419 } 1420 1421 /* 1422 * Get "header already complete" flag 1423 */ 1424 case BIOCGHDRCMPLT: 1425 *(u_int *)addr = d->bd_hdrcmplt; 1426 break; 1427 1428 /* 1429 * Set "header already complete" flag 1430 */ 1431 case BIOCSHDRCMPLT: 1432 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; 1433 break; 1434 1435 /* 1436 * Get packet direction flag 1437 */ 1438 case BIOCGDIRECTION: 1439 *(u_int *)addr = d->bd_direction; 1440 break; 1441 1442 /* 1443 * Set packet direction flag 1444 */ 1445 case BIOCSDIRECTION: 1446 { 1447 u_int direction; 1448 1449 direction = *(u_int *)addr; 1450 switch (direction) { 1451 case BPF_D_IN: 1452 case BPF_D_INOUT: 1453 case BPF_D_OUT: 1454 d->bd_direction = direction; 1455 break; 1456 default: 1457 error = EINVAL; 1458 } 1459 } 1460 break; 1461 1462 /* 1463 * Get packet timestamp format and resolution. 1464 */ 1465 case BIOCGTSTAMP: 1466 *(u_int *)addr = d->bd_tstamp; 1467 break; 1468 1469 /* 1470 * Set packet timestamp format and resolution. 1471 */ 1472 case BIOCSTSTAMP: 1473 { 1474 u_int func; 1475 1476 func = *(u_int *)addr; 1477 if (BPF_T_VALID(func)) 1478 d->bd_tstamp = func; 1479 else 1480 error = EINVAL; 1481 } 1482 break; 1483 1484 case BIOCFEEDBACK: 1485 d->bd_feedback = *(u_int *)addr; 1486 break; 1487 1488 case BIOCLOCK: 1489 d->bd_locked = 1; 1490 break; 1491 1492 case FIONBIO: /* Non-blocking I/O */ 1493 break; 1494 1495 case FIOASYNC: /* Send signal on receive packets */ 1496 d->bd_async = *(int *)addr; 1497 break; 1498 1499 case FIOSETOWN: 1500 error = fsetown(*(int *)addr, &d->bd_sigio); 1501 break; 1502 1503 case FIOGETOWN: 1504 *(int *)addr = fgetown(&d->bd_sigio); 1505 break; 1506 1507 /* This is deprecated, FIOSETOWN should be used instead. */ 1508 case TIOCSPGRP: 1509 error = fsetown(-(*(int *)addr), &d->bd_sigio); 1510 break; 1511 1512 /* This is deprecated, FIOGETOWN should be used instead. */ 1513 case TIOCGPGRP: 1514 *(int *)addr = -fgetown(&d->bd_sigio); 1515 break; 1516 1517 case BIOCSRSIG: /* Set receive signal */ 1518 { 1519 u_int sig; 1520 1521 sig = *(u_int *)addr; 1522 1523 if (sig >= NSIG) 1524 error = EINVAL; 1525 else 1526 d->bd_sig = sig; 1527 break; 1528 } 1529 case BIOCGRSIG: 1530 *(u_int *)addr = d->bd_sig; 1531 break; 1532 1533 case BIOCGETBUFMODE: 1534 *(u_int *)addr = d->bd_bufmode; 1535 break; 1536 1537 case BIOCSETBUFMODE: 1538 /* 1539 * Allow the buffering mode to be changed as long as we 1540 * haven't yet committed to a particular mode. Our 1541 * definition of commitment, for now, is whether or not a 1542 * buffer has been allocated or an interface attached, since 1543 * that's the point where things get tricky. 1544 */ 1545 switch (*(u_int *)addr) { 1546 case BPF_BUFMODE_BUFFER: 1547 break; 1548 1549 case BPF_BUFMODE_ZBUF: 1550 if (bpf_zerocopy_enable) 1551 break; 1552 /* FALLSTHROUGH */ 1553 1554 default: 1555 CURVNET_RESTORE(); 1556 return (EINVAL); 1557 } 1558 1559 BPFD_WLOCK(d); 1560 if (d->bd_sbuf != NULL || d->bd_hbuf != NULL || 1561 d->bd_fbuf != NULL || d->bd_bif != NULL) { 1562 BPFD_WUNLOCK(d); 1563 CURVNET_RESTORE(); 1564 return (EBUSY); 1565 } 1566 d->bd_bufmode = *(u_int *)addr; 1567 BPFD_WUNLOCK(d); 1568 break; 1569 1570 case BIOCGETZMAX: 1571 error = bpf_ioctl_getzmax(td, d, (size_t *)addr); 1572 break; 1573 1574 case BIOCSETZBUF: 1575 error = bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr); 1576 break; 1577 1578 case BIOCROTZBUF: 1579 error = bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr); 1580 break; 1581 } 1582 CURVNET_RESTORE(); 1583 return (error); 1584} 1585 1586/* 1587 * Set d's packet filter program to fp. If this file already has a filter, 1588 * free it and replace it. Returns EINVAL for bogus requests. 1589 */ 1590static int 1591bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd) 1592{ 1593 struct bpf_insn *fcode, *old; 1594 u_int wfilter, flen, size; 1595#ifdef BPF_JITTER 1596 bpf_jit_filter *ofunc; 1597#endif 1598#ifdef COMPAT_FREEBSD32 1599 struct bpf_program32 *fp32; 1600 struct bpf_program fp_swab; 1601 int need_upgrade = 0; 1602 1603 if (cmd == BIOCSETWF32 || cmd == BIOCSETF32 || cmd == BIOCSETFNR32) { 1604 fp32 = (struct bpf_program32 *)fp; 1605 fp_swab.bf_len = fp32->bf_len; 1606 fp_swab.bf_insns = (struct bpf_insn *)(uintptr_t)fp32->bf_insns; 1607 fp = &fp_swab; 1608 if (cmd == BIOCSETWF32) 1609 cmd = BIOCSETWF; 1610 } 1611#endif 1612 if (cmd == BIOCSETWF) { 1613 old = d->bd_wfilter; 1614 wfilter = 1; 1615#ifdef BPF_JITTER 1616 ofunc = NULL; 1617#endif 1618 } else { 1619 wfilter = 0; 1620 old = d->bd_rfilter; 1621#ifdef BPF_JITTER 1622 ofunc = d->bd_bfilter; 1623#endif 1624 } 1625 if (fp->bf_insns == NULL) { 1626 if (fp->bf_len != 0) 1627 return (EINVAL); 1628 /* 1629 * Protect filter change by interface lock, too. 1630 * The same lock order is used by bpf_detachd(). 1631 */ 1632 BPFIF_WLOCK(d->bd_bif); 1633 BPFD_WLOCK(d); 1634 if (wfilter) 1635 d->bd_wfilter = NULL; 1636 else { 1637 d->bd_rfilter = NULL; 1638#ifdef BPF_JITTER 1639 d->bd_bfilter = NULL; 1640#endif 1641 if (cmd == BIOCSETF) 1642 reset_d(d); 1643 } 1644 BPFD_WUNLOCK(d); 1645 BPFIF_WUNLOCK(d->bd_bif); 1646 if (old != NULL) 1647 free((caddr_t)old, M_BPF); 1648#ifdef BPF_JITTER 1649 if (ofunc != NULL) 1650 bpf_destroy_jit_filter(ofunc); 1651#endif 1652 return (0); 1653 } 1654 flen = fp->bf_len; 1655 if (flen > bpf_maxinsns) 1656 return (EINVAL); 1657 1658 size = flen * sizeof(*fp->bf_insns); 1659 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK); 1660 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && 1661 bpf_validate(fcode, (int)flen)) { 1662 /* 1663 * Protect filter change by interface lock, too 1664 * The same lock order is used by bpf_detachd(). 1665 */ 1666 BPFIF_WLOCK(d->bd_bif); 1667 BPFD_WLOCK(d); 1668 if (wfilter) 1669 d->bd_wfilter = fcode; 1670 else { 1671 d->bd_rfilter = fcode; 1672#ifdef BPF_JITTER 1673 d->bd_bfilter = bpf_jitter(fcode, flen); 1674#endif 1675 if (cmd == BIOCSETF) 1676 reset_d(d); 1677 1678 /* 1679 * Do not require upgrade by first BIOCSETF 1680 * (used to set snaplen) by pcap_open_live() 1681 */ 1682 if ((d->bd_writer != 0) && (--d->bd_writer == 0)) 1683 need_upgrade = 1; 1684 CTR4(KTR_NET, "%s: filter function set by pid %d, " 1685 "bd_writer counter %d, need_upgrade %d", 1686 __func__, d->bd_pid, d->bd_writer, need_upgrade); 1687 } 1688 BPFD_WUNLOCK(d); 1689 BPFIF_WUNLOCK(d->bd_bif); 1690 if (old != NULL) 1691 free((caddr_t)old, M_BPF); 1692#ifdef BPF_JITTER 1693 if (ofunc != NULL) 1694 bpf_destroy_jit_filter(ofunc); 1695#endif 1696 1697 /* Move d to active readers list */ 1698 if (need_upgrade != 0) 1699 bpf_upgraded(d); 1700 1701 return (0); 1702 } 1703 free((caddr_t)fcode, M_BPF); 1704 return (EINVAL); 1705} 1706 1707/* 1708 * Detach a file from its current interface (if attached at all) and attach 1709 * to the interface indicated by the name stored in ifr. 1710 * Return an errno or 0. 1711 */ 1712static int 1713bpf_setif(struct bpf_d *d, struct ifreq *ifr) 1714{ 1715 struct bpf_if *bp; 1716 struct ifnet *theywant; 1717 1718 theywant = ifunit(ifr->ifr_name); 1719 if (theywant == NULL || theywant->if_bpf == NULL) 1720 return (ENXIO); 1721 1722 bp = theywant->if_bpf; 1723 1724 /* 1725 * Behavior here depends on the buffering model. If we're using 1726 * kernel memory buffers, then we can allocate them here. If we're 1727 * using zero-copy, then the user process must have registered 1728 * buffers by the time we get here. If not, return an error. 1729 * 1730 * XXXRW: There are locking issues here with multi-threaded use: what 1731 * if two threads try to set the interface at once? 1732 */ 1733 switch (d->bd_bufmode) { 1734 case BPF_BUFMODE_BUFFER: 1735 if (d->bd_sbuf == NULL) 1736 bpf_buffer_alloc(d); 1737 KASSERT(d->bd_sbuf != NULL, ("bpf_setif: bd_sbuf NULL")); 1738 break; 1739 1740 case BPF_BUFMODE_ZBUF: 1741 if (d->bd_sbuf == NULL) 1742 return (EINVAL); 1743 break; 1744 1745 default: 1746 panic("bpf_setif: bufmode %d", d->bd_bufmode); 1747 } 1748 if (bp != d->bd_bif) { 1749 if (d->bd_bif) 1750 /* 1751 * Detach if attached to something else. 1752 */ 1753 bpf_detachd(d); 1754 1755 bpf_attachd(d, bp); 1756 } 1757 BPFD_WLOCK(d); 1758 reset_d(d); 1759 BPFD_WUNLOCK(d); 1760 return (0); 1761} 1762 1763/* 1764 * Support for select() and poll() system calls 1765 * 1766 * Return true iff the specific operation will not block indefinitely. 1767 * Otherwise, return false but make a note that a selwakeup() must be done. 1768 */ 1769static int 1770bpfpoll(struct cdev *dev, int events, struct thread *td) 1771{ 1772 struct bpf_d *d; 1773 int revents; 1774 1775 if (devfs_get_cdevpriv((void **)&d) != 0 || d->bd_bif == NULL) 1776 return (events & 1777 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 1778 1779 /* 1780 * Refresh PID associated with this descriptor. 1781 */ 1782 revents = events & (POLLOUT | POLLWRNORM); 1783 BPFD_WLOCK(d); 1784 BPF_PID_REFRESH(d, td); 1785 if (events & (POLLIN | POLLRDNORM)) { 1786 if (bpf_ready(d)) 1787 revents |= events & (POLLIN | POLLRDNORM); 1788 else { 1789 selrecord(td, &d->bd_sel); 1790 /* Start the read timeout if necessary. */ 1791 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1792 callout_reset(&d->bd_callout, d->bd_rtout, 1793 bpf_timed_out, d); 1794 d->bd_state = BPF_WAITING; 1795 } 1796 } 1797 } 1798 BPFD_WUNLOCK(d); 1799 return (revents); 1800} 1801 1802/* 1803 * Support for kevent() system call. Register EVFILT_READ filters and 1804 * reject all others. 1805 */ 1806int 1807bpfkqfilter(struct cdev *dev, struct knote *kn) 1808{ 1809 struct bpf_d *d; 1810 1811 if (devfs_get_cdevpriv((void **)&d) != 0 || 1812 kn->kn_filter != EVFILT_READ) 1813 return (1); 1814 1815 /* 1816 * Refresh PID associated with this descriptor. 1817 */ 1818 BPFD_WLOCK(d); 1819 BPF_PID_REFRESH_CUR(d); 1820 kn->kn_fop = &bpfread_filtops; 1821 kn->kn_hook = d; 1822 knlist_add(&d->bd_sel.si_note, kn, 1); 1823 BPFD_WUNLOCK(d); 1824 1825 return (0); 1826} 1827 1828static void 1829filt_bpfdetach(struct knote *kn) 1830{ 1831 struct bpf_d *d = (struct bpf_d *)kn->kn_hook; 1832 1833 knlist_remove(&d->bd_sel.si_note, kn, 0); 1834} 1835 1836static int 1837filt_bpfread(struct knote *kn, long hint) 1838{ 1839 struct bpf_d *d = (struct bpf_d *)kn->kn_hook; 1840 int ready; 1841 1842 BPFD_WLOCK_ASSERT(d); 1843 ready = bpf_ready(d); 1844 if (ready) { 1845 kn->kn_data = d->bd_slen; 1846 if (d->bd_hbuf) 1847 kn->kn_data += d->bd_hlen; 1848 } else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1849 callout_reset(&d->bd_callout, d->bd_rtout, 1850 bpf_timed_out, d); 1851 d->bd_state = BPF_WAITING; 1852 } 1853 1854 return (ready); 1855} 1856 1857#define BPF_TSTAMP_NONE 0 1858#define BPF_TSTAMP_FAST 1 1859#define BPF_TSTAMP_NORMAL 2 1860#define BPF_TSTAMP_EXTERN 3 1861 1862static int 1863bpf_ts_quality(int tstype) 1864{ 1865 1866 if (tstype == BPF_T_NONE) 1867 return (BPF_TSTAMP_NONE); 1868 if ((tstype & BPF_T_FAST) != 0) 1869 return (BPF_TSTAMP_FAST); 1870 1871 return (BPF_TSTAMP_NORMAL); 1872} 1873 1874static int 1875bpf_gettime(struct bintime *bt, int tstype, struct mbuf *m) 1876{ 1877 struct m_tag *tag; 1878 int quality; 1879 1880 quality = bpf_ts_quality(tstype); 1881 if (quality == BPF_TSTAMP_NONE) 1882 return (quality); 1883 1884 if (m != NULL) { 1885 tag = m_tag_locate(m, MTAG_BPF, MTAG_BPF_TIMESTAMP, NULL); 1886 if (tag != NULL) { 1887 *bt = *(struct bintime *)(tag + 1); 1888 return (BPF_TSTAMP_EXTERN); 1889 } 1890 } 1891 if (quality == BPF_TSTAMP_NORMAL) 1892 binuptime(bt); 1893 else 1894 getbinuptime(bt); 1895 1896 return (quality); 1897} 1898 1899/* 1900 * Incoming linkage from device drivers. Process the packet pkt, of length 1901 * pktlen, which is stored in a contiguous buffer. The packet is parsed 1902 * by each process' filter, and if accepted, stashed into the corresponding 1903 * buffer. 1904 */ 1905void 1906bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 1907{ 1908 struct bintime bt; 1909 struct bpf_d *d; 1910#ifdef BPF_JITTER 1911 bpf_jit_filter *bf; 1912#endif 1913 u_int slen; 1914 int gottime; 1915 1916 gottime = BPF_TSTAMP_NONE; 1917 1918 BPFIF_RLOCK(bp); 1919 1920 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1921 /* 1922 * We are not using any locks for d here because: 1923 * 1) any filter change is protected by interface 1924 * write lock 1925 * 2) destroying/detaching d is protected by interface 1926 * write lock, too 1927 */ 1928 1929 /* XXX: Do not protect counter for the sake of performance. */ 1930 ++d->bd_rcount; 1931 /* 1932 * NB: We dont call BPF_CHECK_DIRECTION() here since there is no 1933 * way for the caller to indiciate to us whether this packet 1934 * is inbound or outbound. In the bpf_mtap() routines, we use 1935 * the interface pointers on the mbuf to figure it out. 1936 */ 1937#ifdef BPF_JITTER 1938 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL; 1939 if (bf != NULL) 1940 slen = (*(bf->func))(pkt, pktlen, pktlen); 1941 else 1942#endif 1943 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen); 1944 if (slen != 0) { 1945 /* 1946 * Filter matches. Let's to acquire write lock. 1947 */ 1948 BPFD_WLOCK(d); 1949 1950 d->bd_fcount++; 1951 if (gottime < bpf_ts_quality(d->bd_tstamp)) 1952 gottime = bpf_gettime(&bt, d->bd_tstamp, NULL); 1953#ifdef MAC 1954 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0) 1955#endif 1956 catchpacket(d, pkt, pktlen, slen, 1957 bpf_append_bytes, &bt); 1958 BPFD_WUNLOCK(d); 1959 } 1960 } 1961 BPFIF_RUNLOCK(bp); 1962} 1963 1964#define BPF_CHECK_DIRECTION(d, r, i) \ 1965 (((d)->bd_direction == BPF_D_IN && (r) != (i)) || \ 1966 ((d)->bd_direction == BPF_D_OUT && (r) == (i))) 1967 1968/* 1969 * Incoming linkage from device drivers, when packet is in an mbuf chain. 1970 * Locking model is explained in bpf_tap(). 1971 */ 1972void 1973bpf_mtap(struct bpf_if *bp, struct mbuf *m) 1974{ 1975 struct bintime bt; 1976 struct bpf_d *d; 1977#ifdef BPF_JITTER 1978 bpf_jit_filter *bf; 1979#endif 1980 u_int pktlen, slen; 1981 int gottime; 1982 1983 /* Skip outgoing duplicate packets. */ 1984 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) { 1985 m->m_flags &= ~M_PROMISC; 1986 return; 1987 } 1988 1989 pktlen = m_length(m, NULL); 1990 gottime = BPF_TSTAMP_NONE; 1991 1992 BPFIF_RLOCK(bp); 1993 1994 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1995 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp)) 1996 continue; 1997 ++d->bd_rcount; 1998#ifdef BPF_JITTER 1999 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL; 2000 /* XXX We cannot handle multiple mbufs. */ 2001 if (bf != NULL && m->m_next == NULL) 2002 slen = (*(bf->func))(mtod(m, u_char *), pktlen, pktlen); 2003 else 2004#endif 2005 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0); 2006 if (slen != 0) { 2007 BPFD_WLOCK(d); 2008 2009 d->bd_fcount++; 2010 if (gottime < bpf_ts_quality(d->bd_tstamp)) 2011 gottime = bpf_gettime(&bt, d->bd_tstamp, m); 2012#ifdef MAC 2013 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0) 2014#endif 2015 catchpacket(d, (u_char *)m, pktlen, slen, 2016 bpf_append_mbuf, &bt); 2017 BPFD_WUNLOCK(d); 2018 } 2019 } 2020 BPFIF_RUNLOCK(bp); 2021} 2022 2023/* 2024 * Incoming linkage from device drivers, when packet is in 2025 * an mbuf chain and to be prepended by a contiguous header. 2026 */ 2027void 2028bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m) 2029{ 2030 struct bintime bt; 2031 struct mbuf mb; 2032 struct bpf_d *d; 2033 u_int pktlen, slen; 2034 int gottime; 2035 2036 /* Skip outgoing duplicate packets. */ 2037 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) { 2038 m->m_flags &= ~M_PROMISC; 2039 return; 2040 } 2041 2042 pktlen = m_length(m, NULL); 2043 /* 2044 * Craft on-stack mbuf suitable for passing to bpf_filter. 2045 * Note that we cut corners here; we only setup what's 2046 * absolutely needed--this mbuf should never go anywhere else. 2047 */ 2048 mb.m_next = m; 2049 mb.m_data = data; 2050 mb.m_len = dlen; 2051 pktlen += dlen; 2052 2053 gottime = BPF_TSTAMP_NONE; 2054 2055 BPFIF_RLOCK(bp); 2056 2057 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 2058 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp)) 2059 continue; 2060 ++d->bd_rcount; 2061 slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0); 2062 if (slen != 0) { 2063 BPFD_WLOCK(d); 2064 2065 d->bd_fcount++; 2066 if (gottime < bpf_ts_quality(d->bd_tstamp)) 2067 gottime = bpf_gettime(&bt, d->bd_tstamp, m); 2068#ifdef MAC 2069 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0) 2070#endif 2071 catchpacket(d, (u_char *)&mb, pktlen, slen, 2072 bpf_append_mbuf, &bt); 2073 BPFD_WUNLOCK(d); 2074 } 2075 } 2076 BPFIF_RUNLOCK(bp); 2077} 2078 2079#undef BPF_CHECK_DIRECTION 2080 2081#undef BPF_TSTAMP_NONE 2082#undef BPF_TSTAMP_FAST 2083#undef BPF_TSTAMP_NORMAL 2084#undef BPF_TSTAMP_EXTERN 2085 2086static int 2087bpf_hdrlen(struct bpf_d *d) 2088{ 2089 int hdrlen; 2090 2091 hdrlen = d->bd_bif->bif_hdrlen; 2092#ifndef BURN_BRIDGES 2093 if (d->bd_tstamp == BPF_T_NONE || 2094 BPF_T_FORMAT(d->bd_tstamp) == BPF_T_MICROTIME) 2095#ifdef COMPAT_FREEBSD32 2096 if (d->bd_compat32) 2097 hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr32); 2098 else 2099#endif 2100 hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr); 2101 else 2102#endif 2103 hdrlen += SIZEOF_BPF_HDR(struct bpf_xhdr); 2104#ifdef COMPAT_FREEBSD32 2105 if (d->bd_compat32) 2106 hdrlen = BPF_WORDALIGN32(hdrlen); 2107 else 2108#endif 2109 hdrlen = BPF_WORDALIGN(hdrlen); 2110 2111 return (hdrlen - d->bd_bif->bif_hdrlen); 2112} 2113 2114static void 2115bpf_bintime2ts(struct bintime *bt, struct bpf_ts *ts, int tstype) 2116{ 2117 struct bintime bt2; 2118 struct timeval tsm; 2119 struct timespec tsn; 2120 2121 if ((tstype & BPF_T_MONOTONIC) == 0) { 2122 bt2 = *bt; 2123 bintime_add(&bt2, &boottimebin); 2124 bt = &bt2; 2125 } 2126 switch (BPF_T_FORMAT(tstype)) { 2127 case BPF_T_MICROTIME: 2128 bintime2timeval(bt, &tsm); 2129 ts->bt_sec = tsm.tv_sec; 2130 ts->bt_frac = tsm.tv_usec; 2131 break; 2132 case BPF_T_NANOTIME: 2133 bintime2timespec(bt, &tsn); 2134 ts->bt_sec = tsn.tv_sec; 2135 ts->bt_frac = tsn.tv_nsec; 2136 break; 2137 case BPF_T_BINTIME: 2138 ts->bt_sec = bt->sec; 2139 ts->bt_frac = bt->frac; 2140 break; 2141 } 2142} 2143 2144/* 2145 * Move the packet data from interface memory (pkt) into the 2146 * store buffer. "cpfn" is the routine called to do the actual data 2147 * transfer. bcopy is passed in to copy contiguous chunks, while 2148 * bpf_append_mbuf is passed in to copy mbuf chains. In the latter case, 2149 * pkt is really an mbuf. 2150 */ 2151static void 2152catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen, 2153 void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int), 2154 struct bintime *bt) 2155{ 2156 struct bpf_xhdr hdr; 2157#ifndef BURN_BRIDGES 2158 struct bpf_hdr hdr_old; 2159#ifdef COMPAT_FREEBSD32 2160 struct bpf_hdr32 hdr32_old; 2161#endif 2162#endif 2163 int caplen, curlen, hdrlen, totlen; 2164 int do_wakeup = 0; 2165 int do_timestamp; 2166 int tstype; 2167 2168 BPFD_WLOCK_ASSERT(d); 2169 2170 /* 2171 * Detect whether user space has released a buffer back to us, and if 2172 * so, move it from being a hold buffer to a free buffer. This may 2173 * not be the best place to do it (for example, we might only want to 2174 * run this check if we need the space), but for now it's a reliable 2175 * spot to do it. 2176 */ 2177 if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) { 2178 d->bd_fbuf = d->bd_hbuf; 2179 d->bd_hbuf = NULL; 2180 d->bd_hlen = 0; 2181 bpf_buf_reclaimed(d); 2182 } 2183 2184 /* 2185 * Figure out how many bytes to move. If the packet is 2186 * greater or equal to the snapshot length, transfer that 2187 * much. Otherwise, transfer the whole packet (unless 2188 * we hit the buffer size limit). 2189 */ 2190 hdrlen = bpf_hdrlen(d); 2191 totlen = hdrlen + min(snaplen, pktlen); 2192 if (totlen > d->bd_bufsize) 2193 totlen = d->bd_bufsize; 2194 2195 /* 2196 * Round up the end of the previous packet to the next longword. 2197 * 2198 * Drop the packet if there's no room and no hope of room 2199 * If the packet would overflow the storage buffer or the storage 2200 * buffer is considered immutable by the buffer model, try to rotate 2201 * the buffer and wakeup pending processes. 2202 */ 2203#ifdef COMPAT_FREEBSD32 2204 if (d->bd_compat32) 2205 curlen = BPF_WORDALIGN32(d->bd_slen); 2206 else 2207#endif 2208 curlen = BPF_WORDALIGN(d->bd_slen); 2209 if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) { 2210 if (d->bd_fbuf == NULL) { 2211 /* 2212 * There's no room in the store buffer, and no 2213 * prospect of room, so drop the packet. Notify the 2214 * buffer model. 2215 */ 2216 bpf_buffull(d); 2217 ++d->bd_dcount; 2218 return; 2219 } 2220 ROTATE_BUFFERS(d); 2221 do_wakeup = 1; 2222 curlen = 0; 2223 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) 2224 /* 2225 * Immediate mode is set, or the read timeout has already 2226 * expired during a select call. A packet arrived, so the 2227 * reader should be woken up. 2228 */ 2229 do_wakeup = 1; 2230 caplen = totlen - hdrlen; 2231 tstype = d->bd_tstamp; 2232 do_timestamp = tstype != BPF_T_NONE; 2233#ifndef BURN_BRIDGES 2234 if (tstype == BPF_T_NONE || BPF_T_FORMAT(tstype) == BPF_T_MICROTIME) { 2235 struct bpf_ts ts; 2236 if (do_timestamp) 2237 bpf_bintime2ts(bt, &ts, tstype); 2238#ifdef COMPAT_FREEBSD32 2239 if (d->bd_compat32) { 2240 bzero(&hdr32_old, sizeof(hdr32_old)); 2241 if (do_timestamp) { 2242 hdr32_old.bh_tstamp.tv_sec = ts.bt_sec; 2243 hdr32_old.bh_tstamp.tv_usec = ts.bt_frac; 2244 } 2245 hdr32_old.bh_datalen = pktlen; 2246 hdr32_old.bh_hdrlen = hdrlen; 2247 hdr32_old.bh_caplen = caplen; 2248 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr32_old, 2249 sizeof(hdr32_old)); 2250 goto copy; 2251 } 2252#endif 2253 bzero(&hdr_old, sizeof(hdr_old)); 2254 if (do_timestamp) { 2255 hdr_old.bh_tstamp.tv_sec = ts.bt_sec; 2256 hdr_old.bh_tstamp.tv_usec = ts.bt_frac; 2257 } 2258 hdr_old.bh_datalen = pktlen; 2259 hdr_old.bh_hdrlen = hdrlen; 2260 hdr_old.bh_caplen = caplen; 2261 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr_old, 2262 sizeof(hdr_old)); 2263 goto copy; 2264 } 2265#endif 2266 2267 /* 2268 * Append the bpf header. Note we append the actual header size, but 2269 * move forward the length of the header plus padding. 2270 */ 2271 bzero(&hdr, sizeof(hdr)); 2272 if (do_timestamp) 2273 bpf_bintime2ts(bt, &hdr.bh_tstamp, tstype); 2274 hdr.bh_datalen = pktlen; 2275 hdr.bh_hdrlen = hdrlen; 2276 hdr.bh_caplen = caplen; 2277 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr)); 2278 2279 /* 2280 * Copy the packet data into the store buffer and update its length. 2281 */ 2282#ifndef BURN_BRIDGES 2283copy: 2284#endif 2285 (*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, caplen); 2286 d->bd_slen = curlen + totlen; 2287 2288 if (do_wakeup) 2289 bpf_wakeup(d); 2290} 2291 2292/* 2293 * Free buffers currently in use by a descriptor. 2294 * Called on close. 2295 */ 2296static void 2297bpf_freed(struct bpf_d *d) 2298{ 2299 2300 /* 2301 * We don't need to lock out interrupts since this descriptor has 2302 * been detached from its interface and it yet hasn't been marked 2303 * free. 2304 */ 2305 bpf_free(d); 2306 if (d->bd_rfilter != NULL) { 2307 free((caddr_t)d->bd_rfilter, M_BPF); 2308#ifdef BPF_JITTER 2309 if (d->bd_bfilter != NULL) 2310 bpf_destroy_jit_filter(d->bd_bfilter); 2311#endif 2312 } 2313 if (d->bd_wfilter != NULL) 2314 free((caddr_t)d->bd_wfilter, M_BPF); 2315 rw_destroy(&d->bd_lock); 2316} 2317 2318/* 2319 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the 2320 * fixed size of the link header (variable length headers not yet supported). 2321 */ 2322void 2323bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) 2324{ 2325 2326 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); 2327} 2328 2329/* 2330 * Attach an interface to bpf. ifp is a pointer to the structure 2331 * defining the interface to be attached, dlt is the link layer type, 2332 * and hdrlen is the fixed size of the link header (variable length 2333 * headers are not yet supporrted). 2334 */ 2335void 2336bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 2337{ 2338 struct bpf_if *bp; 2339 2340 bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO); 2341 if (bp == NULL) 2342 panic("bpfattach"); 2343 2344 LIST_INIT(&bp->bif_dlist); 2345 LIST_INIT(&bp->bif_wlist); 2346 bp->bif_ifp = ifp; 2347 bp->bif_dlt = dlt; 2348 rw_init(&bp->bif_lock, "bpf interface lock"); 2349 KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized")); 2350 *driverp = bp; 2351 2352 BPF_LOCK(); 2353 LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next); 2354 BPF_UNLOCK(); 2355 2356 bp->bif_hdrlen = hdrlen; 2357 2358 if (bootverbose) 2359 if_printf(ifp, "bpf attached\n"); 2360} 2361 2362/* 2363 * Detach bpf from an interface. This involves detaching each descriptor 2364 * associated with the interface, and leaving bd_bif NULL. Notify each 2365 * descriptor as it's detached so that any sleepers wake up and get 2366 * ENXIO. 2367 */ 2368void 2369bpfdetach(struct ifnet *ifp) 2370{ 2371 struct bpf_if *bp; 2372 struct bpf_d *d; 2373#ifdef INVARIANTS 2374 int ndetached; 2375 2376 ndetached = 0; 2377#endif 2378 2379 /* Find all bpf_if struct's which reference ifp and detach them. */ 2380 do { 2381 BPF_LOCK(); 2382 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 2383 if (ifp == bp->bif_ifp) 2384 break; 2385 } 2386 if (bp != NULL) 2387 LIST_REMOVE(bp, bif_next); 2388 BPF_UNLOCK(); 2389 2390 if (bp != NULL) { 2391#ifdef INVARIANTS 2392 ndetached++; 2393#endif 2394 while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) { 2395 bpf_detachd(d); 2396 BPFD_WLOCK(d); 2397 bpf_wakeup(d); 2398 BPFD_WUNLOCK(d); 2399 } 2400 rw_destroy(&bp->bif_lock); 2401 free(bp, M_BPF); 2402 } 2403 } while (bp != NULL); 2404 2405#ifdef INVARIANTS 2406 if (ndetached == 0) 2407 printf("bpfdetach: %s was not attached\n", ifp->if_xname); 2408#endif 2409} 2410 2411/* 2412 * Get a list of available data link type of the interface. 2413 */ 2414static int 2415bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl) 2416{ 2417 int n, error; 2418 struct ifnet *ifp; 2419 struct bpf_if *bp; 2420 2421 ifp = d->bd_bif->bif_ifp; 2422 n = 0; 2423 error = 0; 2424 BPF_LOCK(); 2425 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 2426 if (bp->bif_ifp != ifp) 2427 continue; 2428 if (bfl->bfl_list != NULL) { 2429 if (n >= bfl->bfl_len) { 2430 BPF_UNLOCK(); 2431 return (ENOMEM); 2432 } 2433 error = copyout(&bp->bif_dlt, 2434 bfl->bfl_list + n, sizeof(u_int)); 2435 } 2436 n++; 2437 } 2438 BPF_UNLOCK(); 2439 bfl->bfl_len = n; 2440 return (error); 2441} 2442 2443/* 2444 * Set the data link type of a BPF instance. 2445 */ 2446static int 2447bpf_setdlt(struct bpf_d *d, u_int dlt) 2448{ 2449 int error, opromisc; 2450 struct ifnet *ifp; 2451 struct bpf_if *bp; 2452 2453 if (d->bd_bif->bif_dlt == dlt) 2454 return (0); 2455 ifp = d->bd_bif->bif_ifp; 2456 BPF_LOCK(); 2457 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 2458 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) 2459 break; 2460 } 2461 BPF_UNLOCK(); 2462 if (bp != NULL) { 2463 opromisc = d->bd_promisc; 2464 bpf_detachd(d); 2465 bpf_attachd(d, bp); 2466 BPFD_WLOCK(d); 2467 reset_d(d); 2468 BPFD_WUNLOCK(d); 2469 if (opromisc) { 2470 error = ifpromisc(bp->bif_ifp, 1); 2471 if (error) 2472 if_printf(bp->bif_ifp, 2473 "bpf_setdlt: ifpromisc failed (%d)\n", 2474 error); 2475 else 2476 d->bd_promisc = 1; 2477 } 2478 } 2479 return (bp == NULL ? EINVAL : 0); 2480} 2481 2482static void 2483bpf_drvinit(void *unused) 2484{ 2485 struct cdev *dev; 2486 2487 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF); 2488 LIST_INIT(&bpf_iflist); 2489 2490 dev = make_dev(&bpf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "bpf"); 2491 /* For compatibility */ 2492 make_dev_alias(dev, "bpf0"); 2493} 2494 2495/* 2496 * Zero out the various packet counters associated with all of the bpf 2497 * descriptors. At some point, we will probably want to get a bit more 2498 * granular and allow the user to specify descriptors to be zeroed. 2499 */ 2500static void 2501bpf_zero_counters(void) 2502{ 2503 struct bpf_if *bp; 2504 struct bpf_d *bd; 2505 2506 BPF_LOCK(); 2507 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 2508 BPFIF_RLOCK(bp); 2509 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) { 2510 BPFD_WLOCK(bd); 2511 bd->bd_rcount = 0; 2512 bd->bd_dcount = 0; 2513 bd->bd_fcount = 0; 2514 bd->bd_wcount = 0; 2515 bd->bd_wfcount = 0; 2516 bd->bd_zcopy = 0; 2517 BPFD_WUNLOCK(bd); 2518 } 2519 BPFIF_RUNLOCK(bp); 2520 } 2521 BPF_UNLOCK(); 2522} 2523 2524static void 2525bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd) 2526{ 2527 2528 bzero(d, sizeof(*d)); 2529 BPFD_LOCK_ASSERT(bd); 2530 d->bd_structsize = sizeof(*d); 2531 d->bd_immediate = bd->bd_immediate; 2532 d->bd_promisc = bd->bd_promisc; 2533 d->bd_hdrcmplt = bd->bd_hdrcmplt; 2534 d->bd_direction = bd->bd_direction; 2535 d->bd_feedback = bd->bd_feedback; 2536 d->bd_async = bd->bd_async; 2537 d->bd_rcount = bd->bd_rcount; 2538 d->bd_dcount = bd->bd_dcount; 2539 d->bd_fcount = bd->bd_fcount; 2540 d->bd_sig = bd->bd_sig; 2541 d->bd_slen = bd->bd_slen; 2542 d->bd_hlen = bd->bd_hlen; 2543 d->bd_bufsize = bd->bd_bufsize; 2544 d->bd_pid = bd->bd_pid; 2545 strlcpy(d->bd_ifname, 2546 bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ); 2547 d->bd_locked = bd->bd_locked; 2548 d->bd_wcount = bd->bd_wcount; 2549 d->bd_wdcount = bd->bd_wdcount; 2550 d->bd_wfcount = bd->bd_wfcount; 2551 d->bd_zcopy = bd->bd_zcopy; 2552 d->bd_bufmode = bd->bd_bufmode; 2553} 2554 2555static int 2556bpf_stats_sysctl(SYSCTL_HANDLER_ARGS) 2557{ 2558 struct xbpf_d *xbdbuf, *xbd, zerostats; 2559 int index, error; 2560 struct bpf_if *bp; 2561 struct bpf_d *bd; 2562 2563 /* 2564 * XXX This is not technically correct. It is possible for non 2565 * privileged users to open bpf devices. It would make sense 2566 * if the users who opened the devices were able to retrieve 2567 * the statistics for them, too. 2568 */ 2569 error = priv_check(req->td, PRIV_NET_BPF); 2570 if (error) 2571 return (error); 2572 /* 2573 * Check to see if the user is requesting that the counters be 2574 * zeroed out. Explicitly check that the supplied data is zeroed, 2575 * as we aren't allowing the user to set the counters currently. 2576 */ 2577 if (req->newptr != NULL) { 2578 if (req->newlen != sizeof(zerostats)) 2579 return (EINVAL); 2580 bzero(&zerostats, sizeof(zerostats)); 2581 xbd = req->newptr; 2582 if (bcmp(xbd, &zerostats, sizeof(*xbd)) != 0) 2583 return (EINVAL); 2584 bpf_zero_counters(); 2585 return (0); 2586 } 2587 if (req->oldptr == NULL) 2588 return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd))); 2589 if (bpf_bpfd_cnt == 0) 2590 return (SYSCTL_OUT(req, 0, 0)); 2591 xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK); 2592 BPF_LOCK(); 2593 if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) { 2594 BPF_UNLOCK(); 2595 free(xbdbuf, M_BPF); 2596 return (ENOMEM); 2597 } 2598 index = 0; 2599 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 2600 BPFIF_RLOCK(bp); 2601 /* Send writers-only first */ 2602 LIST_FOREACH(bd, &bp->bif_wlist, bd_next) { 2603 xbd = &xbdbuf[index++]; 2604 BPFD_RLOCK(bd); 2605 bpfstats_fill_xbpf(xbd, bd); 2606 BPFD_RUNLOCK(bd); 2607 } 2608 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) { 2609 xbd = &xbdbuf[index++]; 2610 BPFD_RLOCK(bd); 2611 bpfstats_fill_xbpf(xbd, bd); 2612 BPFD_RUNLOCK(bd); 2613 } 2614 BPFIF_RUNLOCK(bp); 2615 } 2616 BPF_UNLOCK(); 2617 error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd)); 2618 free(xbdbuf, M_BPF); 2619 return (error); 2620} 2621 2622SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL); 2623 2624#else /* !DEV_BPF && !NETGRAPH_BPF */ 2625/* 2626 * NOP stubs to allow bpf-using drivers to load and function. 2627 * 2628 * A 'better' implementation would allow the core bpf functionality 2629 * to be loaded at runtime. 2630 */ 2631static struct bpf_if bp_null; 2632 2633void 2634bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 2635{ 2636} 2637 2638void 2639bpf_mtap(struct bpf_if *bp, struct mbuf *m) 2640{ 2641} 2642 2643void 2644bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m) 2645{ 2646} 2647 2648void 2649bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) 2650{ 2651 2652 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); 2653} 2654 2655void 2656bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 2657{ 2658 2659 *driverp = &bp_null; 2660} 2661 2662void 2663bpfdetach(struct ifnet *ifp) 2664{ 2665} 2666 2667u_int 2668bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen) 2669{ 2670 return -1; /* "no filter" behaviour */ 2671} 2672 2673int 2674bpf_validate(const struct bpf_insn *f, int len) 2675{ 2676 return 0; /* false */ 2677} 2678 2679#endif /* !DEV_BPF && !NETGRAPH_BPF */ 2680