1 /* $NetBSD: bpf.c,v 1.187 2014/08/07 03:40:21 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from the Stanford/CMU enet packet filter, 8 * (net/enet.c) distributed as part of 4.3BSD, and code contributed 9 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 10 * Berkeley Laboratory. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)bpf.c 8.4 (Berkeley) 1/9/95 37 * static char rcsid[] = 38 * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp "; 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.187 2014/08/07 03:40:21 ozaki-r Exp $"); 43 44 #if defined(_KERNEL_OPT) 45 #include "opt_bpf.h" 46 #include "sl.h" 47 #include "strip.h" 48 #endif 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/mbuf.h> 53 #include <sys/buf.h> 54 #include <sys/time.h> 55 #include <sys/proc.h> 56 #include <sys/ioctl.h> 57 #include <sys/conf.h> 58 #include <sys/vnode.h> 59 #include <sys/queue.h> 60 #include <sys/stat.h> 61 #include <sys/module.h> 62 #include <sys/once.h> 63 #include <sys/atomic.h> 64 65 #include <sys/file.h> 66 #include <sys/filedesc.h> 67 #include <sys/tty.h> 68 #include <sys/uio.h> 69 70 #include <sys/protosw.h> 71 #include <sys/socket.h> 72 #include <sys/errno.h> 73 #include <sys/kernel.h> 74 #include <sys/poll.h> 75 #include <sys/sysctl.h> 76 #include <sys/kauth.h> 77 78 #include <net/if.h> 79 #include <net/slip.h> 80 81 #include <net/bpf.h> 82 #include <net/bpfdesc.h> 83 #include <net/bpfjit.h> 84 85 #include <net/if_arc.h> 86 #include <net/if_ether.h> 87 88 #include <netinet/in.h> 89 #include <netinet/if_inarp.h> 90 91 92 #include <compat/sys/sockio.h> 93 94 #ifndef BPF_BUFSIZE 95 /* 96 * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet 97 * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k). 98 */ 99 # define BPF_BUFSIZE 32768 100 #endif 101 102 #define PRINET 26 /* interruptible */ 103 104 /* 105 * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able. 106 * XXX the default values should be computed dynamically based 107 * on available memory size and available mbuf clusters. 108 */ 109 int bpf_bufsize = BPF_BUFSIZE; 110 int bpf_maxbufsize = BPF_DFLTBUFSIZE; /* XXX set dynamically, see above */ 111 bool bpf_jit = false; 112 113 struct bpfjit_ops bpfjit_module_ops = { 114 .bj_generate_code = NULL, 115 .bj_free_code = NULL 116 }; 117 118 /* 119 * Global BPF statistics returned by net.bpf.stats sysctl. 120 */ 121 struct bpf_stat bpf_gstats; 122 123 /* 124 * Use a mutex to avoid a race condition between gathering the stats/peers 125 * and opening/closing the device. 126 */ 127 static kmutex_t bpf_mtx; 128 129 /* 130 * bpf_iflist is the list of interfaces; each corresponds to an ifnet 131 * bpf_dtab holds the descriptors, indexed by minor device # 132 */ 133 struct bpf_if *bpf_iflist; 134 LIST_HEAD(, bpf_d) bpf_list; 135 136 static int bpf_allocbufs(struct bpf_d *); 137 static void bpf_deliver(struct bpf_if *, 138 void *(*cpfn)(void *, const void *, size_t), 139 void *, u_int, u_int, const bool); 140 static void bpf_freed(struct bpf_d *); 141 static void bpf_ifname(struct ifnet *, struct ifreq *); 142 static void *bpf_mcpy(void *, const void *, size_t); 143 static int bpf_movein(struct uio *, int, uint64_t, 144 struct mbuf **, struct sockaddr *); 145 static void bpf_attachd(struct bpf_d *, struct bpf_if *); 146 static void bpf_detachd(struct bpf_d *); 147 static int bpf_setif(struct bpf_d *, struct ifreq *); 148 static void bpf_timed_out(void *); 149 static inline void 150 bpf_wakeup(struct bpf_d *); 151 static int bpf_hdrlen(struct bpf_d *); 152 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int, 153 void *(*)(void *, const void *, size_t), struct timespec *); 154 static void reset_d(struct bpf_d *); 155 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); 156 static int bpf_setdlt(struct bpf_d *, u_int); 157 158 static int bpf_read(struct file *, off_t *, struct uio *, kauth_cred_t, 159 int); 160 static int bpf_write(struct file *, off_t *, struct uio *, kauth_cred_t, 161 int); 162 static int bpf_ioctl(struct file *, u_long, void *); 163 static int bpf_poll(struct file *, int); 164 static int bpf_stat(struct file *, struct stat *); 165 static int bpf_close(struct file *); 166 static int bpf_kqfilter(struct file *, struct knote *); 167 static void bpf_softintr(void *); 168 169 static const struct fileops bpf_fileops = { 170 .fo_read = bpf_read, 171 .fo_write = bpf_write, 172 .fo_ioctl = bpf_ioctl, 173 .fo_fcntl = fnullop_fcntl, 174 .fo_poll = bpf_poll, 175 .fo_stat = bpf_stat, 176 .fo_close = bpf_close, 177 .fo_kqfilter = bpf_kqfilter, 178 .fo_restart = fnullop_restart, 179 }; 180 181 dev_type_open(bpfopen); 182 183 const struct cdevsw bpf_cdevsw = { 184 .d_open = bpfopen, 185 .d_close = noclose, 186 .d_read = noread, 187 .d_write = nowrite, 188 .d_ioctl = noioctl, 189 .d_stop = nostop, 190 .d_tty = notty, 191 .d_poll = nopoll, 192 .d_mmap = nommap, 193 .d_kqfilter = nokqfilter, 194 .d_discard = nodiscard, 195 .d_flag = D_OTHER 196 }; 197 198 bpfjit_func_t 199 bpf_jit_generate(bpf_ctx_t *bc, void *code, size_t size) 200 { 201 202 membar_consumer(); 203 if (bpfjit_module_ops.bj_generate_code != NULL) { 204 return bpfjit_module_ops.bj_generate_code(bc, code, size); 205 } 206 return NULL; 207 } 208 209 void 210 bpf_jit_freecode(bpfjit_func_t jcode) 211 { 212 KASSERT(bpfjit_module_ops.bj_free_code != NULL); 213 bpfjit_module_ops.bj_free_code(jcode); 214 } 215 216 static int 217 bpf_movein(struct uio *uio, int linktype, uint64_t mtu, struct mbuf **mp, 218 struct sockaddr *sockp) 219 { 220 struct mbuf *m; 221 int error; 222 size_t len; 223 size_t hlen; 224 size_t align; 225 226 /* 227 * Build a sockaddr based on the data link layer type. 228 * We do this at this level because the ethernet header 229 * is copied directly into the data field of the sockaddr. 230 * In the case of SLIP, there is no header and the packet 231 * is forwarded as is. 232 * Also, we are careful to leave room at the front of the mbuf 233 * for the link level header. 234 */ 235 switch (linktype) { 236 237 case DLT_SLIP: 238 sockp->sa_family = AF_INET; 239 hlen = 0; 240 align = 0; 241 break; 242 243 case DLT_PPP: 244 sockp->sa_family = AF_UNSPEC; 245 hlen = 0; 246 align = 0; 247 break; 248 249 case DLT_EN10MB: 250 sockp->sa_family = AF_UNSPEC; 251 /* XXX Would MAXLINKHDR be better? */ 252 /* 6(dst)+6(src)+2(type) */ 253 hlen = sizeof(struct ether_header); 254 align = 2; 255 break; 256 257 case DLT_ARCNET: 258 sockp->sa_family = AF_UNSPEC; 259 hlen = ARC_HDRLEN; 260 align = 5; 261 break; 262 263 case DLT_FDDI: 264 sockp->sa_family = AF_LINK; 265 /* XXX 4(FORMAC)+6(dst)+6(src) */ 266 hlen = 16; 267 align = 0; 268 break; 269 270 case DLT_ECONET: 271 sockp->sa_family = AF_UNSPEC; 272 hlen = 6; 273 align = 2; 274 break; 275 276 case DLT_NULL: 277 sockp->sa_family = AF_UNSPEC; 278 hlen = 0; 279 align = 0; 280 break; 281 282 default: 283 return (EIO); 284 } 285 286 len = uio->uio_resid; 287 /* 288 * If there aren't enough bytes for a link level header or the 289 * packet length exceeds the interface mtu, return an error. 290 */ 291 if (len - hlen > mtu) 292 return (EMSGSIZE); 293 294 /* 295 * XXX Avoid complicated buffer chaining --- 296 * bail if it won't fit in a single mbuf. 297 * (Take into account possible alignment bytes) 298 */ 299 if (len + align > MCLBYTES) 300 return (EIO); 301 302 m = m_gethdr(M_WAIT, MT_DATA); 303 m->m_pkthdr.rcvif = NULL; 304 m->m_pkthdr.len = (int)(len - hlen); 305 if (len + align > MHLEN) { 306 m_clget(m, M_WAIT); 307 if ((m->m_flags & M_EXT) == 0) { 308 error = ENOBUFS; 309 goto bad; 310 } 311 } 312 313 /* Insure the data is properly aligned */ 314 if (align > 0) { 315 m->m_data += align; 316 m->m_len -= (int)align; 317 } 318 319 error = uiomove(mtod(m, void *), len, uio); 320 if (error) 321 goto bad; 322 if (hlen != 0) { 323 memcpy(sockp->sa_data, mtod(m, void *), hlen); 324 m->m_data += hlen; /* XXX */ 325 len -= hlen; 326 } 327 m->m_len = (int)len; 328 *mp = m; 329 return (0); 330 331 bad: 332 m_freem(m); 333 return (error); 334 } 335 336 /* 337 * Attach file to the bpf interface, i.e. make d listen on bp. 338 * Must be called at splnet. 339 */ 340 static void 341 bpf_attachd(struct bpf_d *d, struct bpf_if *bp) 342 { 343 /* 344 * Point d at bp, and add d to the interface's list of listeners. 345 * Finally, point the driver's bpf cookie at the interface so 346 * it will divert packets to bpf. 347 */ 348 d->bd_bif = bp; 349 d->bd_next = bp->bif_dlist; 350 bp->bif_dlist = d; 351 352 *bp->bif_driverp = bp; 353 } 354 355 /* 356 * Detach a file from its interface. 357 */ 358 static void 359 bpf_detachd(struct bpf_d *d) 360 { 361 struct bpf_d **p; 362 struct bpf_if *bp; 363 364 bp = d->bd_bif; 365 /* 366 * Check if this descriptor had requested promiscuous mode. 367 * If so, turn it off. 368 */ 369 if (d->bd_promisc) { 370 int error __diagused; 371 372 d->bd_promisc = 0; 373 /* 374 * Take device out of promiscuous mode. Since we were 375 * able to enter promiscuous mode, we should be able 376 * to turn it off. But we can get an error if 377 * the interface was configured down, so only panic 378 * if we don't get an unexpected error. 379 */ 380 error = ifpromisc(bp->bif_ifp, 0); 381 #ifdef DIAGNOSTIC 382 if (error) 383 printf("%s: ifpromisc failed: %d", __func__, error); 384 #endif 385 } 386 /* Remove d from the interface's descriptor list. */ 387 p = &bp->bif_dlist; 388 while (*p != d) { 389 p = &(*p)->bd_next; 390 if (*p == NULL) 391 panic("%s: descriptor not in list", __func__); 392 } 393 *p = (*p)->bd_next; 394 if (bp->bif_dlist == NULL) 395 /* 396 * Let the driver know that there are no more listeners. 397 */ 398 *d->bd_bif->bif_driverp = NULL; 399 d->bd_bif = NULL; 400 } 401 402 static int 403 doinit(void) 404 { 405 406 mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE); 407 408 LIST_INIT(&bpf_list); 409 410 bpf_gstats.bs_recv = 0; 411 bpf_gstats.bs_drop = 0; 412 bpf_gstats.bs_capt = 0; 413 414 return 0; 415 } 416 417 /* 418 * bpfilterattach() is called at boot time. 419 */ 420 /* ARGSUSED */ 421 void 422 bpfilterattach(int n) 423 { 424 static ONCE_DECL(control); 425 426 RUN_ONCE(&control, doinit); 427 } 428 429 /* 430 * Open ethernet device. Clones. 431 */ 432 /* ARGSUSED */ 433 int 434 bpfopen(dev_t dev, int flag, int mode, struct lwp *l) 435 { 436 struct bpf_d *d; 437 struct file *fp; 438 int error, fd; 439 440 /* falloc() will use the descriptor for us. */ 441 if ((error = fd_allocfile(&fp, &fd)) != 0) 442 return error; 443 444 d = malloc(sizeof(*d), M_DEVBUF, M_WAITOK|M_ZERO); 445 d->bd_bufsize = bpf_bufsize; 446 d->bd_seesent = 1; 447 d->bd_feedback = 0; 448 d->bd_pid = l->l_proc->p_pid; 449 #ifdef _LP64 450 if (curproc->p_flag & PK_32) 451 d->bd_compat32 = 1; 452 #endif 453 getnanotime(&d->bd_btime); 454 d->bd_atime = d->bd_mtime = d->bd_btime; 455 callout_init(&d->bd_callout, 0); 456 selinit(&d->bd_sel); 457 d->bd_sih = softint_establish(SOFTINT_CLOCK, bpf_softintr, d); 458 d->bd_jitcode = NULL; 459 460 mutex_enter(&bpf_mtx); 461 LIST_INSERT_HEAD(&bpf_list, d, bd_list); 462 mutex_exit(&bpf_mtx); 463 464 return fd_clone(fp, fd, flag, &bpf_fileops, d); 465 } 466 467 /* 468 * Close the descriptor by detaching it from its interface, 469 * deallocating its buffers, and marking it free. 470 */ 471 /* ARGSUSED */ 472 static int 473 bpf_close(struct file *fp) 474 { 475 struct bpf_d *d = fp->f_data; 476 int s; 477 478 KERNEL_LOCK(1, NULL); 479 480 /* 481 * Refresh the PID associated with this bpf file. 482 */ 483 d->bd_pid = curproc->p_pid; 484 485 s = splnet(); 486 if (d->bd_state == BPF_WAITING) 487 callout_stop(&d->bd_callout); 488 d->bd_state = BPF_IDLE; 489 if (d->bd_bif) 490 bpf_detachd(d); 491 splx(s); 492 bpf_freed(d); 493 mutex_enter(&bpf_mtx); 494 LIST_REMOVE(d, bd_list); 495 mutex_exit(&bpf_mtx); 496 callout_destroy(&d->bd_callout); 497 seldestroy(&d->bd_sel); 498 softint_disestablish(d->bd_sih); 499 free(d, M_DEVBUF); 500 fp->f_data = NULL; 501 502 KERNEL_UNLOCK_ONE(NULL); 503 504 return (0); 505 } 506 507 /* 508 * Rotate the packet buffers in descriptor d. Move the store buffer 509 * into the hold slot, and the free buffer into the store slot. 510 * Zero the length of the new store buffer. 511 */ 512 #define ROTATE_BUFFERS(d) \ 513 (d)->bd_hbuf = (d)->bd_sbuf; \ 514 (d)->bd_hlen = (d)->bd_slen; \ 515 (d)->bd_sbuf = (d)->bd_fbuf; \ 516 (d)->bd_slen = 0; \ 517 (d)->bd_fbuf = NULL; 518 /* 519 * bpfread - read next chunk of packets from buffers 520 */ 521 static int 522 bpf_read(struct file *fp, off_t *offp, struct uio *uio, 523 kauth_cred_t cred, int flags) 524 { 525 struct bpf_d *d = fp->f_data; 526 int timed_out; 527 int error; 528 int s; 529 530 getnanotime(&d->bd_atime); 531 /* 532 * Restrict application to use a buffer the same size as 533 * the kernel buffers. 534 */ 535 if (uio->uio_resid != d->bd_bufsize) 536 return (EINVAL); 537 538 KERNEL_LOCK(1, NULL); 539 s = splnet(); 540 if (d->bd_state == BPF_WAITING) 541 callout_stop(&d->bd_callout); 542 timed_out = (d->bd_state == BPF_TIMED_OUT); 543 d->bd_state = BPF_IDLE; 544 /* 545 * If the hold buffer is empty, then do a timed sleep, which 546 * ends when the timeout expires or when enough packets 547 * have arrived to fill the store buffer. 548 */ 549 while (d->bd_hbuf == NULL) { 550 if (fp->f_flag & FNONBLOCK) { 551 if (d->bd_slen == 0) { 552 splx(s); 553 KERNEL_UNLOCK_ONE(NULL); 554 return (EWOULDBLOCK); 555 } 556 ROTATE_BUFFERS(d); 557 break; 558 } 559 560 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) { 561 /* 562 * A packet(s) either arrived since the previous 563 * read or arrived while we were asleep. 564 * Rotate the buffers and return what's here. 565 */ 566 ROTATE_BUFFERS(d); 567 break; 568 } 569 error = tsleep(d, PRINET|PCATCH, "bpf", 570 d->bd_rtout); 571 if (error == EINTR || error == ERESTART) { 572 splx(s); 573 KERNEL_UNLOCK_ONE(NULL); 574 return (error); 575 } 576 if (error == EWOULDBLOCK) { 577 /* 578 * On a timeout, return what's in the buffer, 579 * which may be nothing. If there is something 580 * in the store buffer, we can rotate the buffers. 581 */ 582 if (d->bd_hbuf) 583 /* 584 * We filled up the buffer in between 585 * getting the timeout and arriving 586 * here, so we don't need to rotate. 587 */ 588 break; 589 590 if (d->bd_slen == 0) { 591 splx(s); 592 KERNEL_UNLOCK_ONE(NULL); 593 return (0); 594 } 595 ROTATE_BUFFERS(d); 596 break; 597 } 598 if (error != 0) 599 goto done; 600 } 601 /* 602 * At this point, we know we have something in the hold slot. 603 */ 604 splx(s); 605 606 /* 607 * Move data from hold buffer into user space. 608 * We know the entire buffer is transferred since 609 * we checked above that the read buffer is bpf_bufsize bytes. 610 */ 611 error = uiomove(d->bd_hbuf, d->bd_hlen, uio); 612 613 s = splnet(); 614 d->bd_fbuf = d->bd_hbuf; 615 d->bd_hbuf = NULL; 616 d->bd_hlen = 0; 617 done: 618 splx(s); 619 KERNEL_UNLOCK_ONE(NULL); 620 return (error); 621 } 622 623 624 /* 625 * If there are processes sleeping on this descriptor, wake them up. 626 */ 627 static inline void 628 bpf_wakeup(struct bpf_d *d) 629 { 630 wakeup(d); 631 if (d->bd_async) 632 softint_schedule(d->bd_sih); 633 selnotify(&d->bd_sel, 0, 0); 634 } 635 636 static void 637 bpf_softintr(void *cookie) 638 { 639 struct bpf_d *d; 640 641 d = cookie; 642 if (d->bd_async) 643 fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL); 644 } 645 646 static void 647 bpf_timed_out(void *arg) 648 { 649 struct bpf_d *d = arg; 650 int s; 651 652 s = splnet(); 653 if (d->bd_state == BPF_WAITING) { 654 d->bd_state = BPF_TIMED_OUT; 655 if (d->bd_slen != 0) 656 bpf_wakeup(d); 657 } 658 splx(s); 659 } 660 661 662 static int 663 bpf_write(struct file *fp, off_t *offp, struct uio *uio, 664 kauth_cred_t cred, int flags) 665 { 666 struct bpf_d *d = fp->f_data; 667 struct ifnet *ifp; 668 struct mbuf *m, *mc; 669 int error, s; 670 static struct sockaddr_storage dst; 671 672 m = NULL; /* XXX gcc */ 673 674 KERNEL_LOCK(1, NULL); 675 676 if (d->bd_bif == NULL) { 677 KERNEL_UNLOCK_ONE(NULL); 678 return (ENXIO); 679 } 680 getnanotime(&d->bd_mtime); 681 682 ifp = d->bd_bif->bif_ifp; 683 684 if (uio->uio_resid == 0) { 685 KERNEL_UNLOCK_ONE(NULL); 686 return (0); 687 } 688 689 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m, 690 (struct sockaddr *) &dst); 691 if (error) { 692 KERNEL_UNLOCK_ONE(NULL); 693 return (error); 694 } 695 696 if (m->m_pkthdr.len > ifp->if_mtu) { 697 KERNEL_UNLOCK_ONE(NULL); 698 m_freem(m); 699 return (EMSGSIZE); 700 } 701 702 if (d->bd_hdrcmplt) 703 dst.ss_family = pseudo_AF_HDRCMPLT; 704 705 if (d->bd_feedback) { 706 mc = m_dup(m, 0, M_COPYALL, M_NOWAIT); 707 if (mc != NULL) 708 mc->m_pkthdr.rcvif = ifp; 709 /* Set M_PROMISC for outgoing packets to be discarded. */ 710 if (1 /*d->bd_direction == BPF_D_INOUT*/) 711 m->m_flags |= M_PROMISC; 712 } else 713 mc = NULL; 714 715 s = splsoftnet(); 716 error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL); 717 718 if (mc != NULL) { 719 if (error == 0) 720 (*ifp->if_input)(ifp, mc); 721 m_freem(mc); 722 } 723 splx(s); 724 KERNEL_UNLOCK_ONE(NULL); 725 /* 726 * The driver frees the mbuf. 727 */ 728 return (error); 729 } 730 731 /* 732 * Reset a descriptor by flushing its packet buffer and clearing the 733 * receive and drop counts. Should be called at splnet. 734 */ 735 static void 736 reset_d(struct bpf_d *d) 737 { 738 if (d->bd_hbuf) { 739 /* Free the hold buffer. */ 740 d->bd_fbuf = d->bd_hbuf; 741 d->bd_hbuf = NULL; 742 } 743 d->bd_slen = 0; 744 d->bd_hlen = 0; 745 d->bd_rcount = 0; 746 d->bd_dcount = 0; 747 d->bd_ccount = 0; 748 } 749 750 /* 751 * FIONREAD Check for read packet available. 752 * BIOCGBLEN Get buffer len [for read()]. 753 * BIOCSETF Set ethernet read filter. 754 * BIOCFLUSH Flush read packet buffer. 755 * BIOCPROMISC Put interface into promiscuous mode. 756 * BIOCGDLT Get link layer type. 757 * BIOCGETIF Get interface name. 758 * BIOCSETIF Set interface. 759 * BIOCSRTIMEOUT Set read timeout. 760 * BIOCGRTIMEOUT Get read timeout. 761 * BIOCGSTATS Get packet stats. 762 * BIOCIMMEDIATE Set immediate mode. 763 * BIOCVERSION Get filter language version. 764 * BIOCGHDRCMPLT Get "header already complete" flag. 765 * BIOCSHDRCMPLT Set "header already complete" flag. 766 * BIOCSFEEDBACK Set packet feedback mode. 767 * BIOCGFEEDBACK Get packet feedback mode. 768 * BIOCGSEESENT Get "see sent packets" mode. 769 * BIOCSSEESENT Set "see sent packets" mode. 770 */ 771 /* ARGSUSED */ 772 static int 773 bpf_ioctl(struct file *fp, u_long cmd, void *addr) 774 { 775 struct bpf_d *d = fp->f_data; 776 int s, error = 0; 777 778 /* 779 * Refresh the PID associated with this bpf file. 780 */ 781 KERNEL_LOCK(1, NULL); 782 d->bd_pid = curproc->p_pid; 783 #ifdef _LP64 784 if (curproc->p_flag & PK_32) 785 d->bd_compat32 = 1; 786 else 787 d->bd_compat32 = 0; 788 #endif 789 790 s = splnet(); 791 if (d->bd_state == BPF_WAITING) 792 callout_stop(&d->bd_callout); 793 d->bd_state = BPF_IDLE; 794 splx(s); 795 796 switch (cmd) { 797 798 default: 799 error = EINVAL; 800 break; 801 802 /* 803 * Check for read packet available. 804 */ 805 case FIONREAD: 806 { 807 int n; 808 809 s = splnet(); 810 n = d->bd_slen; 811 if (d->bd_hbuf) 812 n += d->bd_hlen; 813 splx(s); 814 815 *(int *)addr = n; 816 break; 817 } 818 819 /* 820 * Get buffer len [for read()]. 821 */ 822 case BIOCGBLEN: 823 *(u_int *)addr = d->bd_bufsize; 824 break; 825 826 /* 827 * Set buffer length. 828 */ 829 case BIOCSBLEN: 830 if (d->bd_bif != NULL) 831 error = EINVAL; 832 else { 833 u_int size = *(u_int *)addr; 834 835 if (size > bpf_maxbufsize) 836 *(u_int *)addr = size = bpf_maxbufsize; 837 else if (size < BPF_MINBUFSIZE) 838 *(u_int *)addr = size = BPF_MINBUFSIZE; 839 d->bd_bufsize = size; 840 } 841 break; 842 843 /* 844 * Set link layer read filter. 845 */ 846 case BIOCSETF: 847 error = bpf_setf(d, addr); 848 break; 849 850 /* 851 * Flush read packet buffer. 852 */ 853 case BIOCFLUSH: 854 s = splnet(); 855 reset_d(d); 856 splx(s); 857 break; 858 859 /* 860 * Put interface into promiscuous mode. 861 */ 862 case BIOCPROMISC: 863 if (d->bd_bif == NULL) { 864 /* 865 * No interface attached yet. 866 */ 867 error = EINVAL; 868 break; 869 } 870 s = splnet(); 871 if (d->bd_promisc == 0) { 872 error = ifpromisc(d->bd_bif->bif_ifp, 1); 873 if (error == 0) 874 d->bd_promisc = 1; 875 } 876 splx(s); 877 break; 878 879 /* 880 * Get device parameters. 881 */ 882 case BIOCGDLT: 883 if (d->bd_bif == NULL) 884 error = EINVAL; 885 else 886 *(u_int *)addr = d->bd_bif->bif_dlt; 887 break; 888 889 /* 890 * Get a list of supported device parameters. 891 */ 892 case BIOCGDLTLIST: 893 if (d->bd_bif == NULL) 894 error = EINVAL; 895 else 896 error = bpf_getdltlist(d, addr); 897 break; 898 899 /* 900 * Set device parameters. 901 */ 902 case BIOCSDLT: 903 if (d->bd_bif == NULL) 904 error = EINVAL; 905 else 906 error = bpf_setdlt(d, *(u_int *)addr); 907 break; 908 909 /* 910 * Set interface name. 911 */ 912 #ifdef OBIOCGETIF 913 case OBIOCGETIF: 914 #endif 915 case BIOCGETIF: 916 if (d->bd_bif == NULL) 917 error = EINVAL; 918 else 919 bpf_ifname(d->bd_bif->bif_ifp, addr); 920 break; 921 922 /* 923 * Set interface. 924 */ 925 #ifdef OBIOCSETIF 926 case OBIOCSETIF: 927 #endif 928 case BIOCSETIF: 929 error = bpf_setif(d, addr); 930 break; 931 932 /* 933 * Set read timeout. 934 */ 935 case BIOCSRTIMEOUT: 936 { 937 struct timeval *tv = addr; 938 939 /* Compute number of ticks. */ 940 d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick; 941 if ((d->bd_rtout == 0) && (tv->tv_usec != 0)) 942 d->bd_rtout = 1; 943 break; 944 } 945 946 #ifdef BIOCGORTIMEOUT 947 /* 948 * Get read timeout. 949 */ 950 case BIOCGORTIMEOUT: 951 { 952 struct timeval50 *tv = addr; 953 954 tv->tv_sec = d->bd_rtout / hz; 955 tv->tv_usec = (d->bd_rtout % hz) * tick; 956 break; 957 } 958 #endif 959 960 #ifdef BIOCSORTIMEOUT 961 /* 962 * Set read timeout. 963 */ 964 case BIOCSORTIMEOUT: 965 { 966 struct timeval50 *tv = addr; 967 968 /* Compute number of ticks. */ 969 d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick; 970 if ((d->bd_rtout == 0) && (tv->tv_usec != 0)) 971 d->bd_rtout = 1; 972 break; 973 } 974 #endif 975 976 /* 977 * Get read timeout. 978 */ 979 case BIOCGRTIMEOUT: 980 { 981 struct timeval *tv = addr; 982 983 tv->tv_sec = d->bd_rtout / hz; 984 tv->tv_usec = (d->bd_rtout % hz) * tick; 985 break; 986 } 987 /* 988 * Get packet stats. 989 */ 990 case BIOCGSTATS: 991 { 992 struct bpf_stat *bs = addr; 993 994 bs->bs_recv = d->bd_rcount; 995 bs->bs_drop = d->bd_dcount; 996 bs->bs_capt = d->bd_ccount; 997 break; 998 } 999 1000 case BIOCGSTATSOLD: 1001 { 1002 struct bpf_stat_old *bs = addr; 1003 1004 bs->bs_recv = d->bd_rcount; 1005 bs->bs_drop = d->bd_dcount; 1006 break; 1007 } 1008 1009 /* 1010 * Set immediate mode. 1011 */ 1012 case BIOCIMMEDIATE: 1013 d->bd_immediate = *(u_int *)addr; 1014 break; 1015 1016 case BIOCVERSION: 1017 { 1018 struct bpf_version *bv = addr; 1019 1020 bv->bv_major = BPF_MAJOR_VERSION; 1021 bv->bv_minor = BPF_MINOR_VERSION; 1022 break; 1023 } 1024 1025 case BIOCGHDRCMPLT: /* get "header already complete" flag */ 1026 *(u_int *)addr = d->bd_hdrcmplt; 1027 break; 1028 1029 case BIOCSHDRCMPLT: /* set "header already complete" flag */ 1030 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; 1031 break; 1032 1033 /* 1034 * Get "see sent packets" flag 1035 */ 1036 case BIOCGSEESENT: 1037 *(u_int *)addr = d->bd_seesent; 1038 break; 1039 1040 /* 1041 * Set "see sent" packets flag 1042 */ 1043 case BIOCSSEESENT: 1044 d->bd_seesent = *(u_int *)addr; 1045 break; 1046 1047 /* 1048 * Set "feed packets from bpf back to input" mode 1049 */ 1050 case BIOCSFEEDBACK: 1051 d->bd_feedback = *(u_int *)addr; 1052 break; 1053 1054 /* 1055 * Get "feed packets from bpf back to input" mode 1056 */ 1057 case BIOCGFEEDBACK: 1058 *(u_int *)addr = d->bd_feedback; 1059 break; 1060 1061 case FIONBIO: /* Non-blocking I/O */ 1062 /* 1063 * No need to do anything special as we use IO_NDELAY in 1064 * bpfread() as an indication of whether or not to block 1065 * the read. 1066 */ 1067 break; 1068 1069 case FIOASYNC: /* Send signal on receive packets */ 1070 d->bd_async = *(int *)addr; 1071 break; 1072 1073 case TIOCSPGRP: /* Process or group to send signals to */ 1074 case FIOSETOWN: 1075 error = fsetown(&d->bd_pgid, cmd, addr); 1076 break; 1077 1078 case TIOCGPGRP: 1079 case FIOGETOWN: 1080 error = fgetown(d->bd_pgid, cmd, addr); 1081 break; 1082 } 1083 KERNEL_UNLOCK_ONE(NULL); 1084 return (error); 1085 } 1086 1087 /* 1088 * Set d's packet filter program to fp. If this file already has a filter, 1089 * free it and replace it. Returns EINVAL for bogus requests. 1090 */ 1091 int 1092 bpf_setf(struct bpf_d *d, struct bpf_program *fp) 1093 { 1094 struct bpf_insn *fcode, *old; 1095 bpfjit_func_t jcode, oldj; 1096 size_t flen, size; 1097 int s; 1098 1099 jcode = NULL; 1100 flen = fp->bf_len; 1101 1102 if ((fp->bf_insns == NULL && flen) || flen > BPF_MAXINSNS) { 1103 return EINVAL; 1104 } 1105 1106 if (flen) { 1107 /* 1108 * Allocate the buffer, copy the byte-code from 1109 * userspace and validate it. 1110 */ 1111 size = flen * sizeof(*fp->bf_insns); 1112 fcode = malloc(size, M_DEVBUF, M_WAITOK); 1113 if (copyin(fp->bf_insns, fcode, size) != 0 || 1114 !bpf_validate(fcode, (int)flen)) { 1115 free(fcode, M_DEVBUF); 1116 return EINVAL; 1117 } 1118 membar_consumer(); 1119 if (bpf_jit) 1120 jcode = bpf_jit_generate(NULL, fcode, flen); 1121 } else { 1122 fcode = NULL; 1123 } 1124 1125 s = splnet(); 1126 old = d->bd_filter; 1127 d->bd_filter = fcode; 1128 oldj = d->bd_jitcode; 1129 d->bd_jitcode = jcode; 1130 reset_d(d); 1131 splx(s); 1132 1133 if (old) { 1134 free(old, M_DEVBUF); 1135 } 1136 if (oldj) { 1137 bpf_jit_freecode(oldj); 1138 } 1139 1140 return 0; 1141 } 1142 1143 /* 1144 * Detach a file from its current interface (if attached at all) and attach 1145 * to the interface indicated by the name stored in ifr. 1146 * Return an errno or 0. 1147 */ 1148 static int 1149 bpf_setif(struct bpf_d *d, struct ifreq *ifr) 1150 { 1151 struct bpf_if *bp; 1152 char *cp; 1153 int unit_seen, i, s, error; 1154 1155 /* 1156 * Make sure the provided name has a unit number, and default 1157 * it to '0' if not specified. 1158 * XXX This is ugly ... do this differently? 1159 */ 1160 unit_seen = 0; 1161 cp = ifr->ifr_name; 1162 cp[sizeof(ifr->ifr_name) - 1] = '\0'; /* sanity */ 1163 while (*cp++) 1164 if (*cp >= '0' && *cp <= '9') 1165 unit_seen = 1; 1166 if (!unit_seen) { 1167 /* Make sure to leave room for the '\0'. */ 1168 for (i = 0; i < (IFNAMSIZ - 1); ++i) { 1169 if ((ifr->ifr_name[i] >= 'a' && 1170 ifr->ifr_name[i] <= 'z') || 1171 (ifr->ifr_name[i] >= 'A' && 1172 ifr->ifr_name[i] <= 'Z')) 1173 continue; 1174 ifr->ifr_name[i] = '0'; 1175 } 1176 } 1177 1178 /* 1179 * Look through attached interfaces for the named one. 1180 */ 1181 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1182 struct ifnet *ifp = bp->bif_ifp; 1183 1184 if (ifp == NULL || 1185 strcmp(ifp->if_xname, ifr->ifr_name) != 0) 1186 continue; 1187 /* skip additional entry */ 1188 if (bp->bif_driverp != &ifp->if_bpf) 1189 continue; 1190 /* 1191 * We found the requested interface. 1192 * Allocate the packet buffers if we need to. 1193 * If we're already attached to requested interface, 1194 * just flush the buffer. 1195 */ 1196 if (d->bd_sbuf == NULL) { 1197 error = bpf_allocbufs(d); 1198 if (error != 0) 1199 return (error); 1200 } 1201 s = splnet(); 1202 if (bp != d->bd_bif) { 1203 if (d->bd_bif) 1204 /* 1205 * Detach if attached to something else. 1206 */ 1207 bpf_detachd(d); 1208 1209 bpf_attachd(d, bp); 1210 } 1211 reset_d(d); 1212 splx(s); 1213 return (0); 1214 } 1215 /* Not found. */ 1216 return (ENXIO); 1217 } 1218 1219 /* 1220 * Copy the interface name to the ifreq. 1221 */ 1222 static void 1223 bpf_ifname(struct ifnet *ifp, struct ifreq *ifr) 1224 { 1225 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ); 1226 } 1227 1228 static int 1229 bpf_stat(struct file *fp, struct stat *st) 1230 { 1231 struct bpf_d *d = fp->f_data; 1232 1233 (void)memset(st, 0, sizeof(*st)); 1234 KERNEL_LOCK(1, NULL); 1235 st->st_dev = makedev(cdevsw_lookup_major(&bpf_cdevsw), d->bd_pid); 1236 st->st_atimespec = d->bd_atime; 1237 st->st_mtimespec = d->bd_mtime; 1238 st->st_ctimespec = st->st_birthtimespec = d->bd_btime; 1239 st->st_uid = kauth_cred_geteuid(fp->f_cred); 1240 st->st_gid = kauth_cred_getegid(fp->f_cred); 1241 st->st_mode = S_IFCHR; 1242 KERNEL_UNLOCK_ONE(NULL); 1243 return 0; 1244 } 1245 1246 /* 1247 * Support for poll() system call 1248 * 1249 * Return true iff the specific operation will not block indefinitely - with 1250 * the assumption that it is safe to positively acknowledge a request for the 1251 * ability to write to the BPF device. 1252 * Otherwise, return false but make a note that a selnotify() must be done. 1253 */ 1254 static int 1255 bpf_poll(struct file *fp, int events) 1256 { 1257 struct bpf_d *d = fp->f_data; 1258 int s = splnet(); 1259 int revents; 1260 1261 /* 1262 * Refresh the PID associated with this bpf file. 1263 */ 1264 KERNEL_LOCK(1, NULL); 1265 d->bd_pid = curproc->p_pid; 1266 1267 revents = events & (POLLOUT | POLLWRNORM); 1268 if (events & (POLLIN | POLLRDNORM)) { 1269 /* 1270 * An imitation of the FIONREAD ioctl code. 1271 */ 1272 if (d->bd_hlen != 0 || 1273 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) && 1274 d->bd_slen != 0)) { 1275 revents |= events & (POLLIN | POLLRDNORM); 1276 } else { 1277 selrecord(curlwp, &d->bd_sel); 1278 /* Start the read timeout if necessary */ 1279 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1280 callout_reset(&d->bd_callout, d->bd_rtout, 1281 bpf_timed_out, d); 1282 d->bd_state = BPF_WAITING; 1283 } 1284 } 1285 } 1286 1287 KERNEL_UNLOCK_ONE(NULL); 1288 splx(s); 1289 return (revents); 1290 } 1291 1292 static void 1293 filt_bpfrdetach(struct knote *kn) 1294 { 1295 struct bpf_d *d = kn->kn_hook; 1296 int s; 1297 1298 KERNEL_LOCK(1, NULL); 1299 s = splnet(); 1300 SLIST_REMOVE(&d->bd_sel.sel_klist, kn, knote, kn_selnext); 1301 splx(s); 1302 KERNEL_UNLOCK_ONE(NULL); 1303 } 1304 1305 static int 1306 filt_bpfread(struct knote *kn, long hint) 1307 { 1308 struct bpf_d *d = kn->kn_hook; 1309 int rv; 1310 1311 KERNEL_LOCK(1, NULL); 1312 kn->kn_data = d->bd_hlen; 1313 if (d->bd_immediate) 1314 kn->kn_data += d->bd_slen; 1315 rv = (kn->kn_data > 0); 1316 KERNEL_UNLOCK_ONE(NULL); 1317 return rv; 1318 } 1319 1320 static const struct filterops bpfread_filtops = 1321 { 1, NULL, filt_bpfrdetach, filt_bpfread }; 1322 1323 static int 1324 bpf_kqfilter(struct file *fp, struct knote *kn) 1325 { 1326 struct bpf_d *d = fp->f_data; 1327 struct klist *klist; 1328 int s; 1329 1330 KERNEL_LOCK(1, NULL); 1331 1332 switch (kn->kn_filter) { 1333 case EVFILT_READ: 1334 klist = &d->bd_sel.sel_klist; 1335 kn->kn_fop = &bpfread_filtops; 1336 break; 1337 1338 default: 1339 KERNEL_UNLOCK_ONE(NULL); 1340 return (EINVAL); 1341 } 1342 1343 kn->kn_hook = d; 1344 1345 s = splnet(); 1346 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 1347 splx(s); 1348 KERNEL_UNLOCK_ONE(NULL); 1349 1350 return (0); 1351 } 1352 1353 /* 1354 * Copy data from an mbuf chain into a buffer. This code is derived 1355 * from m_copydata in sys/uipc_mbuf.c. 1356 */ 1357 static void * 1358 bpf_mcpy(void *dst_arg, const void *src_arg, size_t len) 1359 { 1360 const struct mbuf *m; 1361 u_int count; 1362 u_char *dst; 1363 1364 m = src_arg; 1365 dst = dst_arg; 1366 while (len > 0) { 1367 if (m == NULL) 1368 panic("bpf_mcpy"); 1369 count = min(m->m_len, len); 1370 memcpy(dst, mtod(m, const void *), count); 1371 m = m->m_next; 1372 dst += count; 1373 len -= count; 1374 } 1375 return dst_arg; 1376 } 1377 1378 /* 1379 * Dispatch a packet to all the listeners on interface bp. 1380 * 1381 * pkt pointer to the packet, either a data buffer or an mbuf chain 1382 * buflen buffer length, if pkt is a data buffer 1383 * cpfn a function that can copy pkt into the listener's buffer 1384 * pktlen length of the packet 1385 * rcv true if packet came in 1386 */ 1387 static inline void 1388 bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t), 1389 void *pkt, u_int pktlen, u_int buflen, const bool rcv) 1390 { 1391 struct timespec ts; 1392 bpf_args_t args = { 1393 .pkt = (const uint8_t *)pkt, 1394 .wirelen = pktlen, 1395 .buflen = buflen, 1396 .mem = NULL, 1397 .arg = NULL 1398 }; 1399 struct bpf_d *d; 1400 1401 const bpf_ctx_t *bc = NULL; 1402 bool gottime = false; 1403 1404 /* 1405 * Note that the IPL does not have to be raised at this point. 1406 * The only problem that could arise here is that if two different 1407 * interfaces shared any data. This is not the case. 1408 */ 1409 for (d = bp->bif_dlist; d != NULL; d = d->bd_next) { 1410 u_int slen; 1411 1412 if (!d->bd_seesent && !rcv) { 1413 continue; 1414 } 1415 d->bd_rcount++; 1416 bpf_gstats.bs_recv++; 1417 1418 if (d->bd_jitcode) 1419 slen = d->bd_jitcode(bc, &args); 1420 else 1421 slen = bpf_filter_ext(bc, d->bd_filter, &args); 1422 1423 if (!slen) { 1424 continue; 1425 } 1426 if (!gottime) { 1427 gottime = true; 1428 nanotime(&ts); 1429 } 1430 catchpacket(d, pkt, pktlen, slen, cpfn, &ts); 1431 } 1432 } 1433 1434 /* 1435 * Incoming linkage from device drivers. Process the packet pkt, of length 1436 * pktlen, which is stored in a contiguous buffer. The packet is parsed 1437 * by each process' filter, and if accepted, stashed into the corresponding 1438 * buffer. 1439 */ 1440 static void 1441 _bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 1442 { 1443 1444 bpf_deliver(bp, memcpy, pkt, pktlen, pktlen, true); 1445 } 1446 1447 /* 1448 * Incoming linkage from device drivers, when the head of the packet is in 1449 * a buffer, and the tail is in an mbuf chain. 1450 */ 1451 static void 1452 _bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m) 1453 { 1454 u_int pktlen; 1455 struct mbuf mb; 1456 1457 /* Skip outgoing duplicate packets. */ 1458 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) { 1459 m->m_flags &= ~M_PROMISC; 1460 return; 1461 } 1462 1463 pktlen = m_length(m) + dlen; 1464 1465 /* 1466 * Craft on-stack mbuf suitable for passing to bpf_filter. 1467 * Note that we cut corners here; we only setup what's 1468 * absolutely needed--this mbuf should never go anywhere else. 1469 */ 1470 (void)memset(&mb, 0, sizeof(mb)); 1471 mb.m_next = m; 1472 mb.m_data = data; 1473 mb.m_len = dlen; 1474 1475 bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, m->m_pkthdr.rcvif != NULL); 1476 } 1477 1478 /* 1479 * Incoming linkage from device drivers, when packet is in an mbuf chain. 1480 */ 1481 static void 1482 _bpf_mtap(struct bpf_if *bp, struct mbuf *m) 1483 { 1484 void *(*cpfn)(void *, const void *, size_t); 1485 u_int pktlen, buflen; 1486 void *marg; 1487 1488 /* Skip outgoing duplicate packets. */ 1489 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) { 1490 m->m_flags &= ~M_PROMISC; 1491 return; 1492 } 1493 1494 pktlen = m_length(m); 1495 1496 if (pktlen == m->m_len) { 1497 cpfn = (void *)memcpy; 1498 marg = mtod(m, void *); 1499 buflen = pktlen; 1500 } else { 1501 cpfn = bpf_mcpy; 1502 marg = m; 1503 buflen = 0; 1504 } 1505 1506 bpf_deliver(bp, cpfn, marg, pktlen, buflen, m->m_pkthdr.rcvif != NULL); 1507 } 1508 1509 /* 1510 * We need to prepend the address family as 1511 * a four byte field. Cons up a dummy header 1512 * to pacify bpf. This is safe because bpf 1513 * will only read from the mbuf (i.e., it won't 1514 * try to free it or keep a pointer a to it). 1515 */ 1516 static void 1517 _bpf_mtap_af(struct bpf_if *bp, uint32_t af, struct mbuf *m) 1518 { 1519 struct mbuf m0; 1520 1521 m0.m_flags = 0; 1522 m0.m_next = m; 1523 m0.m_len = 4; 1524 m0.m_data = (char *)⁡ 1525 1526 _bpf_mtap(bp, &m0); 1527 } 1528 1529 /* 1530 * Put the SLIP pseudo-"link header" in place. 1531 * Note this M_PREPEND() should never fail, 1532 * swince we know we always have enough space 1533 * in the input buffer. 1534 */ 1535 static void 1536 _bpf_mtap_sl_in(struct bpf_if *bp, u_char *chdr, struct mbuf **m) 1537 { 1538 int s; 1539 u_char *hp; 1540 1541 M_PREPEND(*m, SLIP_HDRLEN, M_DONTWAIT); 1542 if (*m == NULL) 1543 return; 1544 1545 hp = mtod(*m, u_char *); 1546 hp[SLX_DIR] = SLIPDIR_IN; 1547 (void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN); 1548 1549 s = splnet(); 1550 _bpf_mtap(bp, *m); 1551 splx(s); 1552 1553 m_adj(*m, SLIP_HDRLEN); 1554 } 1555 1556 /* 1557 * Put the SLIP pseudo-"link header" in 1558 * place. The compressed header is now 1559 * at the beginning of the mbuf. 1560 */ 1561 static void 1562 _bpf_mtap_sl_out(struct bpf_if *bp, u_char *chdr, struct mbuf *m) 1563 { 1564 struct mbuf m0; 1565 u_char *hp; 1566 int s; 1567 1568 m0.m_flags = 0; 1569 m0.m_next = m; 1570 m0.m_data = m0.m_dat; 1571 m0.m_len = SLIP_HDRLEN; 1572 1573 hp = mtod(&m0, u_char *); 1574 1575 hp[SLX_DIR] = SLIPDIR_OUT; 1576 (void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN); 1577 1578 s = splnet(); 1579 _bpf_mtap(bp, &m0); 1580 splx(s); 1581 m_freem(m); 1582 } 1583 1584 static int 1585 bpf_hdrlen(struct bpf_d *d) 1586 { 1587 int hdrlen = d->bd_bif->bif_hdrlen; 1588 /* 1589 * Compute the length of the bpf header. This is not necessarily 1590 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1591 * that the network layer header begins on a longword boundary (for 1592 * performance reasons and to alleviate alignment restrictions). 1593 */ 1594 #ifdef _LP64 1595 if (d->bd_compat32) 1596 return (BPF_WORDALIGN32(hdrlen + SIZEOF_BPF_HDR32) - hdrlen); 1597 else 1598 #endif 1599 return (BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen); 1600 } 1601 1602 /* 1603 * Move the packet data from interface memory (pkt) into the 1604 * store buffer. Call the wakeup functions if it's time to wakeup 1605 * a listener (buffer full), "cpfn" is the routine called to do the 1606 * actual data transfer. memcpy is passed in to copy contiguous chunks, 1607 * while bpf_mcpy is passed in to copy mbuf chains. In the latter case, 1608 * pkt is really an mbuf. 1609 */ 1610 static void 1611 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen, 1612 void *(*cpfn)(void *, const void *, size_t), struct timespec *ts) 1613 { 1614 char *h; 1615 int totlen, curlen, caplen; 1616 int hdrlen = bpf_hdrlen(d); 1617 int do_wakeup = 0; 1618 1619 ++d->bd_ccount; 1620 ++bpf_gstats.bs_capt; 1621 /* 1622 * Figure out how many bytes to move. If the packet is 1623 * greater or equal to the snapshot length, transfer that 1624 * much. Otherwise, transfer the whole packet (unless 1625 * we hit the buffer size limit). 1626 */ 1627 totlen = hdrlen + min(snaplen, pktlen); 1628 if (totlen > d->bd_bufsize) 1629 totlen = d->bd_bufsize; 1630 /* 1631 * If we adjusted totlen to fit the bufsize, it could be that 1632 * totlen is smaller than hdrlen because of the link layer header. 1633 */ 1634 caplen = totlen - hdrlen; 1635 if (caplen < 0) 1636 caplen = 0; 1637 1638 /* 1639 * Round up the end of the previous packet to the next longword. 1640 */ 1641 #ifdef _LP64 1642 if (d->bd_compat32) 1643 curlen = BPF_WORDALIGN32(d->bd_slen); 1644 else 1645 #endif 1646 curlen = BPF_WORDALIGN(d->bd_slen); 1647 if (curlen + totlen > d->bd_bufsize) { 1648 /* 1649 * This packet will overflow the storage buffer. 1650 * Rotate the buffers if we can, then wakeup any 1651 * pending reads. 1652 */ 1653 if (d->bd_fbuf == NULL) { 1654 /* 1655 * We haven't completed the previous read yet, 1656 * so drop the packet. 1657 */ 1658 ++d->bd_dcount; 1659 ++bpf_gstats.bs_drop; 1660 return; 1661 } 1662 ROTATE_BUFFERS(d); 1663 do_wakeup = 1; 1664 curlen = 0; 1665 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) { 1666 /* 1667 * Immediate mode is set, or the read timeout has 1668 * already expired during a select call. A packet 1669 * arrived, so the reader should be woken up. 1670 */ 1671 do_wakeup = 1; 1672 } 1673 1674 /* 1675 * Append the bpf header. 1676 */ 1677 h = (char *)d->bd_sbuf + curlen; 1678 #ifdef _LP64 1679 if (d->bd_compat32) { 1680 struct bpf_hdr32 *hp32; 1681 1682 hp32 = (struct bpf_hdr32 *)h; 1683 hp32->bh_tstamp.tv_sec = ts->tv_sec; 1684 hp32->bh_tstamp.tv_usec = ts->tv_nsec / 1000; 1685 hp32->bh_datalen = pktlen; 1686 hp32->bh_hdrlen = hdrlen; 1687 hp32->bh_caplen = caplen; 1688 } else 1689 #endif 1690 { 1691 struct bpf_hdr *hp; 1692 1693 hp = (struct bpf_hdr *)h; 1694 hp->bh_tstamp.tv_sec = ts->tv_sec; 1695 hp->bh_tstamp.tv_usec = ts->tv_nsec / 1000; 1696 hp->bh_datalen = pktlen; 1697 hp->bh_hdrlen = hdrlen; 1698 hp->bh_caplen = caplen; 1699 } 1700 1701 /* 1702 * Copy the packet data into the store buffer and update its length. 1703 */ 1704 (*cpfn)(h + hdrlen, pkt, caplen); 1705 d->bd_slen = curlen + totlen; 1706 1707 /* 1708 * Call bpf_wakeup after bd_slen has been updated so that kevent(2) 1709 * will cause filt_bpfread() to be called with it adjusted. 1710 */ 1711 if (do_wakeup) 1712 bpf_wakeup(d); 1713 } 1714 1715 /* 1716 * Initialize all nonzero fields of a descriptor. 1717 */ 1718 static int 1719 bpf_allocbufs(struct bpf_d *d) 1720 { 1721 1722 d->bd_fbuf = malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK | M_CANFAIL); 1723 if (!d->bd_fbuf) 1724 return (ENOBUFS); 1725 d->bd_sbuf = malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK | M_CANFAIL); 1726 if (!d->bd_sbuf) { 1727 free(d->bd_fbuf, M_DEVBUF); 1728 return (ENOBUFS); 1729 } 1730 d->bd_slen = 0; 1731 d->bd_hlen = 0; 1732 return (0); 1733 } 1734 1735 /* 1736 * Free buffers currently in use by a descriptor. 1737 * Called on close. 1738 */ 1739 static void 1740 bpf_freed(struct bpf_d *d) 1741 { 1742 /* 1743 * We don't need to lock out interrupts since this descriptor has 1744 * been detached from its interface and it yet hasn't been marked 1745 * free. 1746 */ 1747 if (d->bd_sbuf != NULL) { 1748 free(d->bd_sbuf, M_DEVBUF); 1749 if (d->bd_hbuf != NULL) 1750 free(d->bd_hbuf, M_DEVBUF); 1751 if (d->bd_fbuf != NULL) 1752 free(d->bd_fbuf, M_DEVBUF); 1753 } 1754 if (d->bd_filter) 1755 free(d->bd_filter, M_DEVBUF); 1756 1757 if (d->bd_jitcode != NULL) { 1758 bpf_jit_freecode(d->bd_jitcode); 1759 } 1760 } 1761 1762 /* 1763 * Attach an interface to bpf. dlt is the link layer type; 1764 * hdrlen is the fixed size of the link header for the specified dlt 1765 * (variable length headers not yet supported). 1766 */ 1767 static void 1768 _bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 1769 { 1770 struct bpf_if *bp; 1771 bp = malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT); 1772 if (bp == NULL) 1773 panic("bpfattach"); 1774 1775 bp->bif_dlist = NULL; 1776 bp->bif_driverp = driverp; 1777 bp->bif_ifp = ifp; 1778 bp->bif_dlt = dlt; 1779 1780 bp->bif_next = bpf_iflist; 1781 bpf_iflist = bp; 1782 1783 *bp->bif_driverp = NULL; 1784 1785 bp->bif_hdrlen = hdrlen; 1786 #if 0 1787 printf("bpf: %s attached\n", ifp->if_xname); 1788 #endif 1789 } 1790 1791 /* 1792 * Remove an interface from bpf. 1793 */ 1794 static void 1795 _bpfdetach(struct ifnet *ifp) 1796 { 1797 struct bpf_if *bp, **pbp; 1798 struct bpf_d *d; 1799 int s; 1800 1801 /* Nuke the vnodes for any open instances */ 1802 LIST_FOREACH(d, &bpf_list, bd_list) { 1803 if (d->bd_bif != NULL && d->bd_bif->bif_ifp == ifp) { 1804 /* 1805 * Detach the descriptor from an interface now. 1806 * It will be free'ed later by close routine. 1807 */ 1808 s = splnet(); 1809 d->bd_promisc = 0; /* we can't touch device. */ 1810 bpf_detachd(d); 1811 splx(s); 1812 } 1813 } 1814 1815 again: 1816 for (bp = bpf_iflist, pbp = &bpf_iflist; 1817 bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) { 1818 if (bp->bif_ifp == ifp) { 1819 *pbp = bp->bif_next; 1820 free(bp, M_DEVBUF); 1821 goto again; 1822 } 1823 } 1824 } 1825 1826 /* 1827 * Change the data link type of a interface. 1828 */ 1829 static void 1830 _bpf_change_type(struct ifnet *ifp, u_int dlt, u_int hdrlen) 1831 { 1832 struct bpf_if *bp; 1833 1834 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1835 if (bp->bif_driverp == &ifp->if_bpf) 1836 break; 1837 } 1838 if (bp == NULL) 1839 panic("bpf_change_type"); 1840 1841 bp->bif_dlt = dlt; 1842 1843 bp->bif_hdrlen = hdrlen; 1844 } 1845 1846 /* 1847 * Get a list of available data link type of the interface. 1848 */ 1849 static int 1850 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl) 1851 { 1852 int n, error; 1853 struct ifnet *ifp; 1854 struct bpf_if *bp; 1855 1856 ifp = d->bd_bif->bif_ifp; 1857 n = 0; 1858 error = 0; 1859 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1860 if (bp->bif_ifp != ifp) 1861 continue; 1862 if (bfl->bfl_list != NULL) { 1863 if (n >= bfl->bfl_len) 1864 return ENOMEM; 1865 error = copyout(&bp->bif_dlt, 1866 bfl->bfl_list + n, sizeof(u_int)); 1867 } 1868 n++; 1869 } 1870 bfl->bfl_len = n; 1871 return error; 1872 } 1873 1874 /* 1875 * Set the data link type of a BPF instance. 1876 */ 1877 static int 1878 bpf_setdlt(struct bpf_d *d, u_int dlt) 1879 { 1880 int s, error, opromisc; 1881 struct ifnet *ifp; 1882 struct bpf_if *bp; 1883 1884 if (d->bd_bif->bif_dlt == dlt) 1885 return 0; 1886 ifp = d->bd_bif->bif_ifp; 1887 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1888 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) 1889 break; 1890 } 1891 if (bp == NULL) 1892 return EINVAL; 1893 s = splnet(); 1894 opromisc = d->bd_promisc; 1895 bpf_detachd(d); 1896 bpf_attachd(d, bp); 1897 reset_d(d); 1898 if (opromisc) { 1899 error = ifpromisc(bp->bif_ifp, 1); 1900 if (error) 1901 printf("%s: bpf_setdlt: ifpromisc failed (%d)\n", 1902 bp->bif_ifp->if_xname, error); 1903 else 1904 d->bd_promisc = 1; 1905 } 1906 splx(s); 1907 return 0; 1908 } 1909 1910 static int 1911 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS) 1912 { 1913 int newsize, error; 1914 struct sysctlnode node; 1915 1916 node = *rnode; 1917 node.sysctl_data = &newsize; 1918 newsize = bpf_maxbufsize; 1919 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1920 if (error || newp == NULL) 1921 return (error); 1922 1923 if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE) 1924 return (EINVAL); 1925 1926 bpf_maxbufsize = newsize; 1927 1928 return (0); 1929 } 1930 1931 #if defined(MODULAR) || defined(BPFJIT) 1932 static int 1933 sysctl_net_bpf_jit(SYSCTLFN_ARGS) 1934 { 1935 bool newval; 1936 int error; 1937 struct sysctlnode node; 1938 1939 node = *rnode; 1940 node.sysctl_data = &newval; 1941 newval = bpf_jit; 1942 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1943 if (error != 0 || newp == NULL) 1944 return error; 1945 1946 bpf_jit = newval; 1947 1948 /* 1949 * Do a full sync to publish new bpf_jit value and 1950 * update bpfjit_module_ops.bj_generate_code variable. 1951 */ 1952 membar_sync(); 1953 1954 if (newval && bpfjit_module_ops.bj_generate_code == NULL) { 1955 printf("JIT compilation is postponed " 1956 "until after bpfjit module is loaded\n"); 1957 } 1958 1959 return 0; 1960 } 1961 #endif 1962 1963 static int 1964 sysctl_net_bpf_peers(SYSCTLFN_ARGS) 1965 { 1966 int error, elem_count; 1967 struct bpf_d *dp; 1968 struct bpf_d_ext dpe; 1969 size_t len, needed, elem_size, out_size; 1970 char *sp; 1971 1972 if (namelen == 1 && name[0] == CTL_QUERY) 1973 return (sysctl_query(SYSCTLFN_CALL(rnode))); 1974 1975 if (namelen != 2) 1976 return (EINVAL); 1977 1978 /* BPF peers is privileged information. */ 1979 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 1980 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, NULL, NULL, NULL); 1981 if (error) 1982 return (EPERM); 1983 1984 len = (oldp != NULL) ? *oldlenp : 0; 1985 sp = oldp; 1986 elem_size = name[0]; 1987 elem_count = name[1]; 1988 out_size = MIN(sizeof(dpe), elem_size); 1989 needed = 0; 1990 1991 if (elem_size < 1 || elem_count < 0) 1992 return (EINVAL); 1993 1994 mutex_enter(&bpf_mtx); 1995 LIST_FOREACH(dp, &bpf_list, bd_list) { 1996 if (len >= elem_size && elem_count > 0) { 1997 #define BPF_EXT(field) dpe.bde_ ## field = dp->bd_ ## field 1998 BPF_EXT(bufsize); 1999 BPF_EXT(promisc); 2000 BPF_EXT(state); 2001 BPF_EXT(immediate); 2002 BPF_EXT(hdrcmplt); 2003 BPF_EXT(seesent); 2004 BPF_EXT(pid); 2005 BPF_EXT(rcount); 2006 BPF_EXT(dcount); 2007 BPF_EXT(ccount); 2008 #undef BPF_EXT 2009 if (dp->bd_bif) 2010 (void)strlcpy(dpe.bde_ifname, 2011 dp->bd_bif->bif_ifp->if_xname, 2012 IFNAMSIZ - 1); 2013 else 2014 dpe.bde_ifname[0] = '\0'; 2015 2016 error = copyout(&dpe, sp, out_size); 2017 if (error) 2018 break; 2019 sp += elem_size; 2020 len -= elem_size; 2021 } 2022 needed += elem_size; 2023 if (elem_count > 0 && elem_count != INT_MAX) 2024 elem_count--; 2025 } 2026 mutex_exit(&bpf_mtx); 2027 2028 *oldlenp = needed; 2029 2030 return (error); 2031 } 2032 2033 static struct sysctllog *bpf_sysctllog; 2034 static void 2035 sysctl_net_bpf_setup(void) 2036 { 2037 const struct sysctlnode *node; 2038 2039 node = NULL; 2040 sysctl_createv(&bpf_sysctllog, 0, NULL, &node, 2041 CTLFLAG_PERMANENT, 2042 CTLTYPE_NODE, "bpf", 2043 SYSCTL_DESCR("BPF options"), 2044 NULL, 0, NULL, 0, 2045 CTL_NET, CTL_CREATE, CTL_EOL); 2046 if (node != NULL) { 2047 #if defined(MODULAR) || defined(BPFJIT) 2048 sysctl_createv(&bpf_sysctllog, 0, NULL, NULL, 2049 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2050 CTLTYPE_BOOL, "jit", 2051 SYSCTL_DESCR("Toggle Just-In-Time compilation"), 2052 sysctl_net_bpf_jit, 0, &bpf_jit, 0, 2053 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2054 #endif 2055 sysctl_createv(&bpf_sysctllog, 0, NULL, NULL, 2056 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2057 CTLTYPE_INT, "maxbufsize", 2058 SYSCTL_DESCR("Maximum size for data capture buffer"), 2059 sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0, 2060 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2061 sysctl_createv(&bpf_sysctllog, 0, NULL, NULL, 2062 CTLFLAG_PERMANENT, 2063 CTLTYPE_STRUCT, "stats", 2064 SYSCTL_DESCR("BPF stats"), 2065 NULL, 0, &bpf_gstats, sizeof(bpf_gstats), 2066 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2067 sysctl_createv(&bpf_sysctllog, 0, NULL, NULL, 2068 CTLFLAG_PERMANENT, 2069 CTLTYPE_STRUCT, "peers", 2070 SYSCTL_DESCR("BPF peers"), 2071 sysctl_net_bpf_peers, 0, NULL, 0, 2072 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2073 } 2074 2075 } 2076 2077 struct bpf_ops bpf_ops_kernel = { 2078 .bpf_attach = _bpfattach, 2079 .bpf_detach = _bpfdetach, 2080 .bpf_change_type = _bpf_change_type, 2081 2082 .bpf_tap = _bpf_tap, 2083 .bpf_mtap = _bpf_mtap, 2084 .bpf_mtap2 = _bpf_mtap2, 2085 .bpf_mtap_af = _bpf_mtap_af, 2086 .bpf_mtap_sl_in = _bpf_mtap_sl_in, 2087 .bpf_mtap_sl_out = _bpf_mtap_sl_out, 2088 }; 2089 2090 MODULE(MODULE_CLASS_DRIVER, bpf, NULL); 2091 2092 static int 2093 bpf_modcmd(modcmd_t cmd, void *arg) 2094 { 2095 devmajor_t bmajor, cmajor; 2096 int error; 2097 2098 bmajor = cmajor = NODEVMAJOR; 2099 2100 switch (cmd) { 2101 case MODULE_CMD_INIT: 2102 bpfilterattach(0); 2103 error = devsw_attach("bpf", NULL, &bmajor, 2104 &bpf_cdevsw, &cmajor); 2105 if (error == EEXIST) 2106 error = 0; /* maybe built-in ... improve eventually */ 2107 if (error) 2108 break; 2109 2110 bpf_ops_handover_enter(&bpf_ops_kernel); 2111 atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel); 2112 bpf_ops_handover_exit(); 2113 sysctl_net_bpf_setup(); 2114 break; 2115 2116 case MODULE_CMD_FINI: 2117 /* 2118 * While there is no reference counting for bpf callers, 2119 * unload could at least in theory be done similarly to 2120 * system call disestablishment. This should even be 2121 * a little simpler: 2122 * 2123 * 1) replace op vector with stubs 2124 * 2) post update to all cpus with xc 2125 * 3) check that nobody is in bpf anymore 2126 * (it's doubtful we'd want something like l_sysent, 2127 * but we could do something like *signed* percpu 2128 * counters. if the sum is 0, we're good). 2129 * 4) if fail, unroll changes 2130 * 2131 * NOTE: change won't be atomic to the outside. some 2132 * packets may be not captured even if unload is 2133 * not succesful. I think packet capture not working 2134 * is a perfectly logical consequence of trying to 2135 * disable packet capture. 2136 */ 2137 error = EOPNOTSUPP; 2138 /* insert sysctl teardown */ 2139 break; 2140 2141 default: 2142 error = ENOTTY; 2143 break; 2144 } 2145 2146 return error; 2147 } 2148