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