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