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