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