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