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