1 /* 2 * Copyright (c) 1990, 1991 Regents of the University of California. 3 * 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 * %sccs.include.redist.c% 11 * 12 * @(#)bpf.c 7.8 (Berkeley) 02/15/92 13 * 14 * static char rcsid[] = 15 * "$Header: bpf.c,v 1.33 91/10/27 21:21:58 mccanne Exp $"; 16 */ 17 18 #include "bpfilter.h" 19 20 #if NBPFILTER > 0 21 22 #ifndef __GNUC__ 23 #define inline 24 #else 25 #define inline __inline__ 26 #endif 27 28 #include <sys/param.h> 29 #include <sys/systm.h> 30 #include <sys/mbuf.h> 31 #include <sys/buf.h> 32 #include <sys/dir.h> 33 #include <sys/proc.h> 34 #include <sys/user.h> 35 #include <sys/ioctl.h> 36 #include <sys/map.h> 37 38 #include <sys/file.h> 39 #if defined(sparc) && BSD < 199103 40 #include <sys/stream.h> 41 #endif 42 #include <sys/tty.h> 43 #include <sys/uio.h> 44 45 #include <sys/protosw.h> 46 #include <sys/socket.h> 47 #include <net/if.h> 48 49 #include <net/bpf.h> 50 #include <net/bpfdesc.h> 51 52 #include <sys/errno.h> 53 54 #include <netinet/in.h> 55 #include <netinet/if_ether.h> 56 #include <sys/kernel.h> 57 58 /* 59 * Older BSDs don't have kernel malloc. 60 */ 61 #if BSD < 199103 62 extern bcopy(); 63 static caddr_t bpf_alloc(); 64 #define malloc(size, type, canwait) bpf_alloc(size, canwait) 65 #define free(cp, type) m_free(*(struct mbuf **)(cp - 8)) 66 #define M_WAITOK M_WAIT 67 #define BPF_BUFSIZE (MCLBYTES-8) 68 #define ERESTART EINTR 69 #else 70 #define BPF_BUFSIZE 4096 71 #endif 72 73 #define PRINET 26 /* interruptible */ 74 75 /* 76 * The default read buffer size is patchable. 77 */ 78 int bpf_bufsize = BPF_BUFSIZE; 79 80 /* 81 * bpf_iflist is the list of interfaces; each corresponds to an ifnet 82 * bpf_dtab holds the descriptors, indexed by minor device # 83 * 84 * We really don't need NBPFILTER bpf_if entries, but this eliminates 85 * the need to account for all possible drivers here. 86 * This problem will go away when these structures are allocated dynamically. 87 */ 88 static struct bpf_if *bpf_iflist; 89 static struct bpf_d bpf_dtab[NBPFILTER]; 90 91 static void bpf_ifname(); 92 static void catchpacket(); 93 static int bpf_setif(); 94 static int bpf_initd(); 95 96 static int 97 bpf_movein(uio, linktype, mp, sockp) 98 register struct uio *uio; 99 int linktype; 100 register struct mbuf **mp; 101 register struct sockaddr *sockp; 102 { 103 struct mbuf *m; 104 int error; 105 int len; 106 int hlen; 107 108 /* 109 * Build a sockaddr based on the data link layer type. 110 * We do this at this level because the ethernet header 111 * is copied directly into the data field of the sockaddr. 112 * In the case of SLIP, there is no header and the packet 113 * is forwarded as is. 114 * Also, we are careful to leave room at the front of the mbuf 115 * for the link level header. 116 */ 117 switch (linktype) { 118 case DLT_SLIP: 119 sockp->sa_family = AF_INET; 120 hlen = 0; 121 break; 122 123 case DLT_EN10MB: 124 sockp->sa_family = AF_UNSPEC; 125 /* XXX Would MAXLINKHDR be better? */ 126 hlen = sizeof(struct ether_header); 127 break; 128 129 case DLT_FDDI: 130 sockp->sa_family = AF_UNSPEC; 131 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */ 132 hlen = 24; 133 break; 134 135 default: 136 return (EIO); 137 } 138 139 len = uio->uio_resid; 140 if ((unsigned)len > MCLBYTES) 141 return (EIO); 142 143 MGET(m, M_WAIT, MT_DATA); 144 if (m == 0) 145 return (ENOBUFS); 146 if (len > MLEN) { 147 #if BSD >= 199103 148 MCLGET(m, M_WAIT); 149 if ((m->m_flags & M_EXT) == 0) { 150 #else 151 MCLGET(m); 152 if (m->m_len == MCLBYTES) { 153 #endif 154 error = ENOBUFS; 155 goto bad; 156 } 157 } 158 m->m_len = len; 159 *mp = m; 160 /* 161 * Make room for link header. 162 */ 163 if (hlen) { 164 m->m_len -= hlen; 165 #if BSD >= 199103 166 m->m_data += hlen; /* XXX */ 167 #else 168 m->m_off += hlen; 169 #endif 170 error = uiomove((caddr_t)sockp->sa_data, hlen, uio); 171 if (error) 172 goto bad; 173 } 174 error = uiomove(mtod(m, caddr_t), len - hlen, uio); 175 if (!error) 176 return (0); 177 bad: 178 m_freem(m); 179 return (error); 180 } 181 182 /* 183 * Attach file to the bpf interface, i.e. make d listen on bp. 184 * Must be called at splimp. 185 */ 186 static void 187 bpf_attachd(d, bp) 188 struct bpf_d *d; 189 struct bpf_if *bp; 190 { 191 /* 192 * Point d at bp, and add d to the interface's list of listeners. 193 * Finally, point the driver's bpf cookie at the interface so 194 * it will divert packets to bpf. 195 */ 196 d->bd_bif = bp; 197 d->bd_next = bp->bif_dlist; 198 bp->bif_dlist = d; 199 200 *bp->bif_driverp = bp; 201 } 202 203 /* 204 * Detach a file from its interface. 205 */ 206 static void 207 bpf_detachd(d) 208 struct bpf_d *d; 209 { 210 struct bpf_d **p; 211 struct bpf_if *bp; 212 213 bp = d->bd_bif; 214 /* 215 * Check if this descriptor had requested promiscuous mode. 216 * If so, turn it off. 217 */ 218 if (d->bd_promisc) { 219 d->bd_promisc = 0; 220 if (ifpromisc(bp->bif_ifp, 0)) 221 /* 222 * Something is really wrong if we were able to put 223 * the driver into promiscuous mode, but can't 224 * take it out. 225 */ 226 panic("bpf: ifpromisc failed"); 227 } 228 /* Remove d from the interface's descriptor list. */ 229 p = &bp->bif_dlist; 230 while (*p != d) { 231 p = &(*p)->bd_next; 232 if (*p == 0) 233 panic("bpf_detachd: descriptor not in list"); 234 } 235 *p = (*p)->bd_next; 236 if (bp->bif_dlist == 0) 237 /* 238 * Let the driver know that there are no more listeners. 239 */ 240 *d->bd_bif->bif_driverp = 0; 241 d->bd_bif = 0; 242 } 243 244 245 /* 246 * Mark a descriptor free by making it point to itself. 247 * This is probably cheaper than marking with a constant since 248 * the address should be in a register anyway. 249 */ 250 #define D_ISFREE(d) ((d) == (d)->bd_next) 251 #define D_MARKFREE(d) ((d)->bd_next = (d)) 252 #define D_MARKUSED(d) ((d)->bd_next = 0) 253 254 /* 255 * bpfopen - open ethernet device 256 * 257 * Errors: ENXIO - illegal minor device number 258 * EBUSY - too many files open 259 */ 260 /* ARGSUSED */ 261 int 262 bpfopen(dev, flag) 263 dev_t dev; 264 int flag; 265 { 266 int error, s; 267 register struct bpf_d *d; 268 269 if (minor(dev) >= NBPFILTER) 270 return (ENXIO); 271 272 /* 273 * Each minor can be opened by only one process. If the requested 274 * minor is in use, return EBUSY. 275 */ 276 s = splimp(); 277 d = &bpf_dtab[minor(dev)]; 278 if (!D_ISFREE(d)) { 279 splx(s); 280 return (EBUSY); 281 } else 282 /* Mark "free" and do most initialization. */ 283 bzero((char *)d, sizeof(*d)); 284 splx(s); 285 286 error = bpf_initd(d); 287 if (error) { 288 D_MARKFREE(d); 289 return (error); 290 } 291 return (0); 292 } 293 294 /* 295 * Close the descriptor by detaching it from its interface, 296 * deallocating its buffers, and marking it free. 297 */ 298 /* ARGSUSED */ 299 bpfclose(dev, flag) 300 dev_t dev; 301 int flag; 302 { 303 register struct bpf_d *d = &bpf_dtab[minor(dev)]; 304 int s; 305 306 s = splimp(); 307 if (d->bd_bif) 308 bpf_detachd(d); 309 splx(s); 310 311 bpf_freed(d); 312 } 313 314 #if BSD < 199103 315 static 316 bpf_timeout(arg) 317 caddr_t arg; 318 { 319 struct bpf_d *d = (struct bpf_d *)arg; 320 d->bd_timedout = 1; 321 wakeup(arg); 322 } 323 324 static int 325 tsleep(cp, pri, s, t) 326 register caddr_t cp; 327 register int pri; 328 char *s; 329 register int t; 330 { 331 register struct bpf_d *d = (struct bpf_d *)cp; 332 register int error; 333 334 if (t != 0) { 335 d->bd_timedout = 0; 336 timeout(bpf_timeout, cp); 337 } 338 error = sleep(cp, pri); 339 if (t != 0) { 340 if (d->bd_timedout != 0) 341 return EWOULDBLOCK; 342 untimeout(bpf_timeout, cp); 343 } 344 return error; 345 } 346 #endif 347 348 /* 349 * Rotate the packet buffers in descriptor d. Move the store buffer 350 * into the hold slot, and the free buffer into the store slot. 351 * Zero the length of the new store buffer. 352 */ 353 #define ROTATE_BUFFERS(d) \ 354 (d)->bd_hbuf = (d)->bd_sbuf; \ 355 (d)->bd_hlen = (d)->bd_slen; \ 356 (d)->bd_sbuf = (d)->bd_fbuf; \ 357 (d)->bd_slen = 0; \ 358 (d)->bd_fbuf = 0; 359 /* 360 * bpfread - read next chunk of packets from buffers 361 */ 362 int 363 bpfread(dev, uio) 364 dev_t dev; 365 register struct uio *uio; 366 { 367 register struct bpf_d *d = &bpf_dtab[minor(dev)]; 368 int error; 369 int s; 370 371 /* 372 * Restrict application to use a buffer the same size as 373 * as kernel buffers. 374 */ 375 if (uio->uio_resid != d->bd_bufsize) 376 return (EINVAL); 377 378 s = splimp(); 379 /* 380 * If the hold buffer is empty, then set a timer and sleep 381 * until either the timeout has occurred or enough packets have 382 * arrived to fill the store buffer. 383 */ 384 while (d->bd_hbuf == 0) { 385 if (d->bd_immediate && d->bd_slen != 0) { 386 /* 387 * A packet(s) either arrived since the previous 388 * read or arrived while we were asleep. 389 * Rotate the buffers and return what's here. 390 */ 391 ROTATE_BUFFERS(d); 392 break; 393 } 394 error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf", d->bd_rtout); 395 if (error == EINTR || error == ERESTART) { 396 splx(s); 397 return (error); 398 } 399 if (error == EWOULDBLOCK) { 400 /* 401 * On a timeout, return what's in the buffer, 402 * which may be nothing. If there is something 403 * in the store buffer, we can rotate the buffers. 404 */ 405 if (d->bd_hbuf) 406 /* 407 * We filled up the buffer in between 408 * getting the timeout and arriving 409 * here, so we don't need to rotate. 410 */ 411 break; 412 413 if (d->bd_slen == 0) { 414 splx(s); 415 return (0); 416 } 417 ROTATE_BUFFERS(d); 418 break; 419 } 420 } 421 /* 422 * At this point, we know we have something in the hold slot. 423 */ 424 splx(s); 425 426 /* 427 * Move data from hold buffer into user space. 428 * We know the entire buffer is transferred since 429 * we checked above that the read buffer is bpf_bufsize bytes. 430 */ 431 #if BSD >= 199103 432 error = uiomove(d->bd_hbuf, d->bd_hlen, uio); 433 #else 434 error = uiomove(d->bd_hbuf, d->bd_hlen, UIO_READ, uio); 435 #endif 436 s = splimp(); 437 d->bd_fbuf = d->bd_hbuf; 438 d->bd_hbuf = 0; 439 splx(s); 440 441 return (error); 442 } 443 444 445 /* 446 * If there are processes sleeping on this descriptor, wake them up. 447 */ 448 static inline void 449 bpf_wakeup(d) 450 register struct bpf_d *d; 451 { 452 wakeup((caddr_t)d); 453 selwakeup(&d->bd_selproc); 454 } 455 456 int 457 bpfwrite(dev, uio) 458 dev_t dev; 459 struct uio *uio; 460 { 461 register struct bpf_d *d = &bpf_dtab[minor(dev)]; 462 struct ifnet *ifp; 463 struct mbuf *m; 464 int error, s; 465 static struct sockaddr dst; 466 467 if (d->bd_bif == 0) 468 return (ENXIO); 469 470 ifp = d->bd_bif->bif_ifp; 471 472 if (uio->uio_resid == 0) 473 return (0); 474 if (uio->uio_resid > ifp->if_mtu) 475 return (EMSGSIZE); 476 477 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst); 478 if (error) 479 return (error); 480 481 s = splnet(); 482 #if BSD >= 199103 483 error = (*ifp->if_output)(ifp, m, &dst, (struct rtenty *)0); 484 #else 485 error = (*ifp->if_output)(ifp, m, &dst); 486 #endif 487 splx(s); 488 /* 489 * The driver frees the mbuf. 490 */ 491 return (error); 492 } 493 494 /* 495 * Reset a descriptor by flushing its packet bufferand clearing the receive 496 * and drop counts. Should be called at splimp. 497 */ 498 static void 499 reset_d(d) 500 struct bpf_d *d; 501 { 502 if (d->bd_hbuf) { 503 /* Free the hold buffer. */ 504 d->bd_fbuf = d->bd_hbuf; 505 d->bd_hbuf = 0; 506 } 507 d->bd_slen = 0; 508 d->bd_rcount = 0; 509 d->bd_dcount = 0; 510 } 511 512 /* 513 * FIONREAD Check for read packet available. 514 * SIOCGIFADDR Get interface address - convenient hook to driver. 515 * BIOCGBLEN Get buffer len [for read()]. 516 * BIOCSETF Set ethernet read filter. 517 * BIOCFLUSH Flush read packet buffer. 518 * BIOCPROMISC Put interface into promiscuous mode. 519 * BIOCGDLT Get link layer type. 520 * BIOCGETIF Get interface name. 521 * BIOCSETIF Set interface. 522 * BIOCSRTIMEOUT Set read timeout. 523 * BIOCGRTIMEOUT Get read timeout. 524 * BIOCGSTATS Get packet stats. 525 * BIOCIMMEDIATE Set immediate mode. 526 */ 527 /* ARGSUSED */ 528 int 529 bpfioctl(dev, cmd, addr, flag) 530 dev_t dev; 531 int cmd; 532 caddr_t addr; 533 int flag; 534 { 535 register struct bpf_d *d = &bpf_dtab[minor(dev)]; 536 int s, error = 0; 537 538 switch (cmd) { 539 540 default: 541 error = EINVAL; 542 break; 543 544 /* 545 * Check for read packet available. 546 */ 547 case FIONREAD: 548 { 549 int n; 550 551 s = splimp(); 552 n = d->bd_slen; 553 if (d->bd_hbuf) 554 n += d->bd_hlen; 555 splx(s); 556 557 *(int *)addr = n; 558 break; 559 } 560 561 case SIOCGIFADDR: 562 { 563 struct ifnet *ifp; 564 565 if (d->bd_bif == 0) 566 error = EINVAL; 567 else { 568 ifp = d->bd_bif->bif_ifp; 569 error = (*ifp->if_ioctl)(ifp, cmd, addr); 570 } 571 break; 572 } 573 574 /* 575 * Get buffer len [for read()]. 576 */ 577 case BIOCGBLEN: 578 *(u_int *)addr = d->bd_bufsize; 579 break; 580 581 /* 582 * Set link layer read filter. 583 */ 584 case BIOCSETF: 585 error = bpf_setf(d, (struct bpf_program *)addr); 586 break; 587 588 /* 589 * Flush read packet buffer. 590 */ 591 case BIOCFLUSH: 592 s = splimp(); 593 reset_d(d); 594 splx(s); 595 break; 596 597 /* 598 * Put interface into promiscuous mode. 599 */ 600 case BIOCPROMISC: 601 if (d->bd_bif == 0) { 602 /* 603 * No interface attached yet. 604 */ 605 error = EINVAL; 606 break; 607 } 608 s = splimp(); 609 if (d->bd_promisc == 0) { 610 error = ifpromisc(d->bd_bif->bif_ifp, 1); 611 if (error == 0) 612 d->bd_promisc = 1; 613 } 614 splx(s); 615 break; 616 617 /* 618 * Get device parameters. 619 */ 620 case BIOCGDLT: 621 if (d->bd_bif == 0) 622 error = EINVAL; 623 else 624 *(u_int *)addr = d->bd_bif->bif_dlt; 625 break; 626 627 /* 628 * Set interface name. 629 */ 630 case BIOCGETIF: 631 if (d->bd_bif == 0) 632 error = EINVAL; 633 else 634 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr); 635 break; 636 637 /* 638 * Set interface. 639 */ 640 case BIOCSETIF: 641 error = bpf_setif(d, (struct ifreq *)addr); 642 break; 643 644 /* 645 * Set read timeout. 646 */ 647 case BIOCSRTIMEOUT: 648 { 649 struct timeval *tv = (struct timeval *)addr; 650 u_long msec; 651 652 /* Compute number of milliseconds. */ 653 msec = tv->tv_sec * 1000 + tv->tv_usec / 1000; 654 /* Scale milliseconds to ticks. Assume hard 655 clock has millisecond or greater resolution 656 (i.e. tick >= 1000). For 10ms hardclock, 657 tick/1000 = 10, so rtout<-msec/10. */ 658 d->bd_rtout = msec / (tick / 1000); 659 break; 660 } 661 662 /* 663 * Get read timeout. 664 */ 665 case BIOCGRTIMEOUT: 666 { 667 struct timeval *tv = (struct timeval *)addr; 668 u_long msec = d->bd_rtout; 669 670 msec *= tick / 1000; 671 tv->tv_sec = msec / 1000; 672 tv->tv_usec = msec % 1000; 673 break; 674 } 675 676 /* 677 * Get packet stats. 678 */ 679 case BIOCGSTATS: 680 { 681 struct bpf_stat *bs = (struct bpf_stat *)addr; 682 683 bs->bs_recv = d->bd_rcount; 684 bs->bs_drop = d->bd_dcount; 685 break; 686 } 687 688 /* 689 * Set immediate mode. 690 */ 691 case BIOCIMMEDIATE: 692 d->bd_immediate = *(u_int *)addr; 693 break; 694 } 695 return (error); 696 } 697 698 /* 699 * Set d's packet filter program to fp. If this file already has a filter, 700 * free it and replace it. Returns EINVAL for bogus requests. 701 */ 702 int 703 bpf_setf(d, fp) 704 struct bpf_d *d; 705 struct bpf_program *fp; 706 { 707 struct bpf_insn *fcode, *old; 708 u_int flen, size; 709 int s; 710 711 old = d->bd_filter; 712 if (fp->bf_insns == 0) { 713 if (fp->bf_len != 0) 714 return (EINVAL); 715 s = splimp(); 716 d->bd_filter = 0; 717 reset_d(d); 718 splx(s); 719 if (old != 0) 720 free((caddr_t)old, M_DEVBUF); 721 return (0); 722 } 723 flen = fp->bf_len; 724 if (flen > BPF_MAXINSNS) 725 return (EINVAL); 726 727 size = flen * sizeof(*fp->bf_insns); 728 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK); 729 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && 730 bpf_validate(fcode, (int)flen)) { 731 s = splimp(); 732 d->bd_filter = fcode; 733 reset_d(d); 734 splx(s); 735 if (old != 0) 736 free((caddr_t)old, M_DEVBUF); 737 738 return (0); 739 } 740 free((caddr_t)fcode, M_DEVBUF); 741 return (EINVAL); 742 } 743 744 /* 745 * Detach a file from its current interface (if attached at all) and attach 746 * to the interface indicated by the name stored in ifr. 747 * Return an errno or 0. 748 */ 749 static int 750 bpf_setif(d, ifr) 751 struct bpf_d *d; 752 struct ifreq *ifr; 753 { 754 struct bpf_if *bp; 755 char *cp; 756 int unit, s; 757 758 /* 759 * Separate string into name part and unit number. Put a null 760 * byte at the end of the name part, and compute the number. 761 * If the a unit number is unspecified, the default is 0, 762 * as initialized above. XXX This should be common code. 763 */ 764 unit = 0; 765 cp = ifr->ifr_name; 766 cp[sizeof(ifr->ifr_name) - 1] = '\0'; 767 while (*cp++) { 768 if (*cp >= '0' && *cp <= '9') { 769 unit = *cp - '0'; 770 *cp++ = '\0'; 771 while (*cp) 772 unit = 10 * unit + *cp++ - '0'; 773 break; 774 } 775 } 776 /* 777 * Look through attached interfaces for the named one. 778 */ 779 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) { 780 struct ifnet *ifp = bp->bif_ifp; 781 782 if (ifp == 0 || unit != ifp->if_unit 783 || strcmp(ifp->if_name, ifr->ifr_name) != 0) 784 continue; 785 /* 786 * We found the requested interface. If we're 787 * already attached to it, just flush the buffer. 788 * If it's not up, return an error. 789 */ 790 if ((ifp->if_flags & IFF_UP) == 0) 791 return (ENETDOWN); 792 s = splimp(); 793 if (bp != d->bd_bif) { 794 if (d->bd_bif) 795 /* 796 * Detach if attached to something else. 797 */ 798 bpf_detachd(d); 799 800 bpf_attachd(d, bp); 801 } 802 reset_d(d); 803 splx(s); 804 return (0); 805 } 806 /* Not found. */ 807 return (ENXIO); 808 } 809 810 /* 811 * Convert an interface name plus unit number of an ifp to a single 812 * name which is returned in the ifr. 813 */ 814 static void 815 bpf_ifname(ifp, ifr) 816 struct ifnet *ifp; 817 struct ifreq *ifr; 818 { 819 char *s = ifp->if_name; 820 char *d = ifr->ifr_name; 821 822 while (*d++ = *s++) 823 ; 824 /* XXX Assume that unit number is less than 10. */ 825 *d++ = ifp->if_unit + '0'; 826 *d = '\0'; 827 } 828 829 /* 830 * The new select interface passes down the proc pointer; the old select 831 * stubs had to grab it out of the user struct. This glue allows either case. 832 */ 833 #if BSD >= 199103 834 #define bpf_select bpfselect 835 #else 836 int 837 bpfselect(dev, rw) 838 register dev_t dev; 839 int rw; 840 { 841 bpf_select(dev, rw, u.u_procp); 842 } 843 #endif 844 845 /* 846 * Support for select() system call 847 * Inspired by the code in tty.c for the same purpose. 848 * 849 * bpfselect - returns true iff the specific operation 850 * will not block indefinitely. Otherwise, return 851 * false but make a note that a selwakeup() must be done. 852 */ 853 int 854 bpf_select(dev, rw, p) 855 register dev_t dev; 856 int rw; 857 struct proc *p; 858 { 859 register struct bpf_d *d; 860 register int s; 861 862 if (rw != FREAD) 863 return (0); 864 /* 865 * An imitation of the FIONREAD ioctl code. 866 */ 867 d = &bpf_dtab[minor(dev)]; 868 869 s = splimp(); 870 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0)) { 871 /* 872 * There is data waiting. 873 */ 874 splx(s); 875 return (1); 876 } 877 /* 878 * No data ready. If there's already a select() waiting on this 879 * minor device then this is a collision. This shouldn't happen 880 * because minors really should not be shared, but if a process 881 * forks while one of these is open, it is possible that both 882 * processes could select on the same descriptor. 883 */ 884 selrecord(p, &d->bd_selproc); 885 splx(s); 886 return (0); 887 } 888 889 /* 890 * bpf_tap - incoming linkage from device drivers 891 */ 892 void 893 bpf_tap(arg, pkt, pktlen) 894 caddr_t arg; 895 register u_char *pkt; 896 register u_int pktlen; 897 { 898 struct bpf_if *bp; 899 register struct bpf_d *d; 900 register u_int slen; 901 /* 902 * Note that the ipl does not have to be raised at this point. 903 * The only problem that could arise here is that if two different 904 * interfaces shared any data. This is not the case. 905 */ 906 bp = (struct bpf_if *)arg; 907 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 908 ++d->bd_rcount; 909 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen); 910 if (slen != 0) 911 catchpacket(d, pkt, pktlen, slen, bcopy); 912 } 913 } 914 915 /* 916 * Copy data from an mbuf chain into a buffer. This code is derived 917 * from m_copydata in sys/uipc_mbuf.c. 918 */ 919 static void 920 bpf_mcopy(src, dst, len) 921 u_char *src; 922 u_char *dst; 923 register int len; 924 { 925 register struct mbuf *m = (struct mbuf *)src; 926 register unsigned count; 927 928 while (len > 0) { 929 if (m == 0) 930 panic("bpf_mcopy"); 931 count = MIN(m->m_len, len); 932 bcopy(mtod(m, caddr_t), (caddr_t)dst, count); 933 m = m->m_next; 934 dst += count; 935 len -= count; 936 } 937 } 938 939 /* 940 * bpf_mtap - incoming linkage from device drivers, when packet 941 * is in an mbuf chain 942 */ 943 void 944 bpf_mtap(arg, m) 945 caddr_t arg; 946 struct mbuf *m; 947 { 948 struct bpf_if *bp = (struct bpf_if *)arg; 949 struct bpf_d *d; 950 u_int pktlen, slen; 951 struct mbuf *m0; 952 953 pktlen = 0; 954 for (m0 = m; m0 != 0; m0 = m0->m_next) 955 pktlen += m0->m_len; 956 957 for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 958 ++d->bd_rcount; 959 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0); 960 if (slen != 0) 961 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy); 962 } 963 } 964 965 /* 966 * Move the packet data from interface memory (pkt) into the 967 * store buffer. Return 1 if it's time to wakeup a listener (buffer full), 968 * otherwise 0. "copy" is the routine called to do the actual data 969 * transfer. bcopy is passed in to copy contiguous chunks, while 970 * bpf_mcopy is passed in to copy mbuf chains. In the latter case, 971 * pkt is really an mbuf. 972 */ 973 static void 974 catchpacket(d, pkt, pktlen, snaplen, cpfn) 975 register struct bpf_d *d; 976 register u_char *pkt; 977 register u_int pktlen, snaplen; 978 register void (*cpfn)(); 979 { 980 register struct bpf_hdr *hp; 981 register int totlen, curlen; 982 register int hdrlen = d->bd_bif->bif_hdrlen; 983 /* 984 * Figure out how many bytes to move. If the packet is 985 * greater or equal to the snapshot length, transfer that 986 * much. Otherwise, transfer the whole packet (unless 987 * we hit the buffer size limit). 988 */ 989 totlen = hdrlen + MIN(snaplen, pktlen); 990 if (totlen > d->bd_bufsize) 991 totlen = d->bd_bufsize; 992 993 /* 994 * Round up the end of the previous packet to the next longword. 995 */ 996 curlen = BPF_WORDALIGN(d->bd_slen); 997 if (curlen + totlen > d->bd_bufsize) { 998 /* 999 * This packet will overflow the storage buffer. 1000 * Rotate the buffers if we can, then wakeup any 1001 * pending reads. 1002 */ 1003 if (d->bd_fbuf == 0) { 1004 /* 1005 * We haven't completed the previous read yet, 1006 * so drop the packet. 1007 */ 1008 ++d->bd_dcount; 1009 return; 1010 } 1011 ROTATE_BUFFERS(d); 1012 bpf_wakeup(d); 1013 curlen = 0; 1014 } 1015 else if (d->bd_immediate) 1016 /* 1017 * Immediate mode is set. A packet arrived so any 1018 * reads should be woken up. 1019 */ 1020 bpf_wakeup(d); 1021 1022 /* 1023 * Append the bpf header. 1024 */ 1025 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); 1026 #ifdef sun 1027 uniqtime(&hp->bh_tstamp); 1028 #else 1029 #if BSD >= 199103 1030 microtime(&hp->bh_tstamp); 1031 #else 1032 hp->bh_tstamp = time; 1033 #endif 1034 #endif 1035 hp->bh_datalen = pktlen; 1036 hp->bh_hdrlen = hdrlen; 1037 /* 1038 * Copy the packet data into the store buffer and update its length. 1039 */ 1040 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); 1041 d->bd_slen = curlen + totlen; 1042 } 1043 1044 /* 1045 * Initialize all nonzero fields of a descriptor. 1046 */ 1047 static int 1048 bpf_initd(d) 1049 register struct bpf_d *d; 1050 { 1051 d->bd_bufsize = bpf_bufsize; 1052 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK); 1053 if (d->bd_fbuf == 0) 1054 return (ENOBUFS); 1055 1056 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK); 1057 if (d->bd_sbuf == 0) { 1058 free(d->bd_fbuf, M_DEVBUF); 1059 return (ENOBUFS); 1060 } 1061 d->bd_slen = 0; 1062 d->bd_hlen = 0; 1063 return (0); 1064 } 1065 1066 /* 1067 * Free buffers currently in use by a descriptor. 1068 * Called on close. 1069 */ 1070 bpf_freed(d) 1071 register struct bpf_d *d; 1072 { 1073 /* 1074 * We don't need to lock out interrupts since this descriptor has 1075 * been detached from its interface and it yet hasn't been marked 1076 * free. 1077 */ 1078 if (d->bd_hbuf) 1079 free(d->bd_hbuf, M_DEVBUF); 1080 if (d->bd_fbuf) 1081 free(d->bd_fbuf, M_DEVBUF); 1082 free(d->bd_sbuf, M_DEVBUF); 1083 if (d->bd_filter) 1084 free((caddr_t)d->bd_filter, M_DEVBUF); 1085 1086 D_MARKFREE(d); 1087 } 1088 1089 /* 1090 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *) 1091 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed 1092 * size of the link header (variable length headers not yet supported). 1093 */ 1094 void 1095 bpfattach(driverp, ifp, dlt, hdrlen) 1096 caddr_t *driverp; 1097 struct ifnet *ifp; 1098 u_int dlt, hdrlen; 1099 { 1100 struct bpf_if *bp; 1101 int i; 1102 #if BSD < 199103 1103 static struct bpf_if bpf_ifs[NBPFILTER]; 1104 static int bpfifno; 1105 1106 bp = (bpfifno < NBPFILTER) ? &bpf_ifs[bpfifno++] : 0; 1107 #else 1108 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT); 1109 #endif 1110 if (bp == 0) 1111 panic("bpfattach"); 1112 1113 bp->bif_dlist = 0; 1114 bp->bif_driverp = (struct bpf_if **)driverp; 1115 bp->bif_ifp = ifp; 1116 bp->bif_dlt = dlt; 1117 1118 bp->bif_next = bpf_iflist; 1119 bpf_iflist = bp; 1120 1121 *bp->bif_driverp = 0; 1122 1123 /* 1124 * Compute the length of the bpf header. This is not necessarily 1125 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1126 * that the network layer header begins on a longword boundary (for 1127 * performance reasons and to alleviate alignment restrictions). 1128 */ 1129 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1130 1131 /* 1132 * Mark all the descriptors free if this hasn't been done. 1133 */ 1134 if (!D_ISFREE(&bpf_dtab[0])) 1135 for (i = 0; i < NBPFILTER; ++i) 1136 D_MARKFREE(&bpf_dtab[i]); 1137 1138 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit); 1139 } 1140 1141 #if BSD >= 199103 1142 /* XXX This routine belongs in net/if.c. */ 1143 /* 1144 * Set/clear promiscuous mode on interface ifp based on the truth value` 1145 * of pswitch. The calls are reference counted so that only the first 1146 * on request actually has an effect, as does the final off request. 1147 * Results are undefined if the off and on requests are not matched. 1148 */ 1149 int 1150 ifpromisc(ifp, pswitch) 1151 struct ifnet *ifp; 1152 int pswitch; 1153 { 1154 struct ifreq ifr; 1155 /* 1156 * If the device is not configured up, we cannot put it in 1157 * promiscuous mode. 1158 */ 1159 if ((ifp->if_flags & IFF_UP) == 0) 1160 return (ENETDOWN); 1161 1162 if (pswitch) { 1163 if (ifp->if_pcount++ != 0) 1164 return (0); 1165 ifp->if_flags |= IFF_PROMISC; 1166 } else { 1167 if (--ifp->if_pcount > 0) 1168 return (0); 1169 ifp->if_flags &= ~IFF_PROMISC; 1170 } 1171 ifr.ifr_flags = ifp->if_flags; 1172 return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr)); 1173 } 1174 #endif 1175 1176 #if BSD < 199103 1177 /* 1178 * Allocate some memory for bpf. This is temporary SunOS support, and 1179 * is admittedly a gross hack. 1180 * If resources unavaiable, return 0. 1181 */ 1182 static caddr_t 1183 bpf_alloc(size, canwait) 1184 register int size; 1185 register int canwait; 1186 { 1187 register struct mbuf *m; 1188 1189 if ((unsigned)size > (MCLBYTES-8)) 1190 return 0; 1191 1192 MGET(m, canwait, MT_DATA); 1193 if (m == 0) 1194 return 0; 1195 if ((unsigned)size > (MLEN-8)) { 1196 MCLGET(m); 1197 if (m->m_len != MCLBYTES) { 1198 m_freem(m); 1199 return 0; 1200 } 1201 } 1202 *mtod(m, struct mbuf **) = m; 1203 return mtod(m, caddr_t) + 8; 1204 } 1205 #endif 1206 #endif 1207