bpf.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 1,218 行 · 第 1/2 页
C
1,218 行
/* * Flush read packet buffer. */ case BIOCFLUSH: s = splimp(); reset_d(d); splx(s); break; /* * Put interface into promiscuous mode. */ case BIOCPROMISC: if (d->bd_bif == 0) { /* * No interface attached yet. */ error = EINVAL; break; } s = splimp(); if (d->bd_promisc == 0) { d->bd_promisc = 1; error = ifpromisc(d->bd_bif->bif_ifp, 1); } splx(s); break; /* * Get device parameters. */ case BIOCDEVP: if (d->bd_bif == 0) error = EINVAL; else *(struct bpf_devp *)addr = d->bd_bif->bif_devp; break; /* * Set interface name. */ case BIOCGETIF: if (d->bd_bif == 0) error = EINVAL; else bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr); break; /* * Set interface. */ case BIOCSETIF: error = bpf_setif(d, (struct ifreq *)addr); break; /* * Set read timeout. */ case BIOCSRTIMEOUT: { struct timeval *tv = (struct timeval *)addr; u_long msec; /* Compute number of milliseconds. */ msec = tv->tv_sec * 1000 + tv->tv_usec / 1000; /* Scale milliseconds to ticks. Assume hard clock has millisecond or greater resolution (i.e. tick >= 1000). For 10ms hardclock, tick/1000 = 10, so rtout<-msec/10. */ d->bd_rtout = msec / (tick / 1000); break; } /* * Get read timeout. */ case BIOCGRTIMEOUT: { struct timeval *tv = (struct timeval *)addr; u_long msec = d->bd_rtout; msec *= tick / 1000; tv->tv_sec = msec / 1000; tv->tv_usec = msec % 1000; break; } /* * Get packet stats. */ case BIOCGSTATS: { struct bpf_stat *bs = (struct bpf_stat *)addr; bs->bs_recv = d->bd_rcount; bs->bs_drop = d->bd_dcount; break; } /* * Set immediate mode. */ case BIOCIMMEDIATE: d->bd_immediate = *(u_int *)addr; break; } return error;}/* * Set d's packet filter program to 'fp'. If 'd' already has a filter, * free it and replace it. Returns an appropriate ioctl error code. */intbpf_setf(d, fp) struct bpf_d *d; struct bpf_program *fp;{ struct bpf_insn *fcode; struct mbuf *m; u_int flen, size; int s; if (fp->bf_insns == 0) { s = splimp(); if (fp->bf_len != 0) return EINVAL; if (d->bd_filterm) m_freem(d->bd_filterm); d->bd_filterm = 0; d->bd_filter = bpf_default_filter; reset_d(d); splx(s); return 0; } flen = fp->bf_len; size = flen * sizeof(*fp->bf_insns); if (size > MCLBYTES) return EINVAL; MGET(m, M_DONTWAIT, MT_DATA); if (m == 0) return ENOBUFS; if (size > MLEN) { MCLGET(m); if (m->m_len != MCLBYTES) { m_freem(m); return ENOBUFS; } } fcode = mtod(m, struct bpf_insn *); if (copyin((caddr_t)(fp->bf_insns), (caddr_t)fcode, size)) return EINVAL; if (bpf_validate(fcode, flen)) { s = splimp(); if (d->bd_filterm) m_freem(d->bd_filterm); d->bd_filterm = m; d->bd_filter = fcode; reset_d(d); splx(s); return 0; } m_freem(m); return EINVAL;}/* * Detach 'd' from its current interface (if attached at all) and attach to * the interface named 'name'. Return ioctl error code or 0. */static intbpf_setif(d, ifr) struct bpf_d *d; struct ifreq *ifr;{ struct bpf_if *bp; char *cp; int unit, i, s; /* * Separate string into name part and unit number. Put a null * byte at the end of the name part, and compute the number. * If the a unit number is unspecified, the default is 0, * as initialized above. */ unit = 0; cp = ifr->ifr_name; cp[sizeof(ifr->ifr_name) - 1] = '\0'; while (*cp++) { if (*cp >= '0' && *cp <= '9') { unit = *cp - '0'; *cp++ = '\0'; while (*cp) unit = 10 * unit + *cp++ - '0'; break; } } /* * Look through attached interfaces for the named one. */ bp = bpf_iftab; for (i = 0; i < NBPFILTER; ++bp, ++i) { struct ifnet *ifp = bp->bif_ifp; if (ifp == 0 || unit != ifp->if_unit || strcmp(ifp->if_name, ifr->ifr_name) != 0) continue; /* * We found the requested interface. If we're * already attached to it, just flush the buffer. * If it's not up, return an error. */ if ((ifp->if_flags & IFF_UP) == 0) return ENETDOWN; s = splimp(); if (bp != d->bd_bif) { if (d->bd_bif) /* * Detach if attached to something else. */ bpf_detachd(d); bpf_attachd(d, bp); } reset_d(d); splx(s); return 0; } /* Not found. */ return ENXIO;}/* * Lookup the name of the 'ifp' interface and return it in 'ifr->ifr_name'. * We augment the ifp's base name with its unit number. */static voidbpf_ifname(ifp, ifr) struct ifnet *ifp; struct ifreq *ifr;{ char *s = ifp->if_name; char *d = ifr->ifr_name; while (*d++ = *s++) ; /* Assume that unit number is less than 10. */ *d++ = ifp->if_unit + '0'; *d = '\0';}/* * Support for select() system call * Inspired by the code in tty.c for the same purpose. * * bpfselect - returns true iff the specific operation * will not block indefinitely. Otherwise, return * false but make a note that a selwakeup() must be done. */intbpfselect(dev, rw) register dev_t dev; int rw;{ register struct bpf_d *d; register int s; if (rw != FREAD) return 0; /* * An imitation of the FIONREAD ioctl code. */ d = &bpf_dtab[minor(dev)]; s = splimp(); if (d->bd_sbuf->m_len || d->bd_hbuf && d->bd_hbuf->m_len) { /* * There is data waiting. */ splx(s); return 1; } /* * No data ready. If there's already a select() waiting on this * minor device then this is a collision. This shouldn't happen * because minors really should not be shared, but if a process * forks while one of these is open, it is possible that both * processes could select on the same descriptor. */ if (d->bd_SelProc && d->bd_SelProc->p_wchan == (caddr_t)&selwait) d->bd_SelColl = 1; else d->bd_SelProc = u.u_procp; splx(s); return 0;}/* * bpf_tap - incoming linkage from device drivers */voidbpf_tap(p, pbuf, plen) caddr_t p; register u_char *pbuf; register u_int plen;{ struct bpf_if *bp; register struct bpf_d *d; register int slen; extern bcopy(); /* * Note that the ipl does not have to be raised at this point. * The only problem that could arise here is that if two different * interfaces shared any data. This is not the case. */ bp = (struct bpf_if *)p; for (d = bp->bif_dlist; d != 0; d = d->bd_next) { ++d->bd_rcount; slen = bpf_filter(d->bd_filter, pbuf, plen, plen); if (slen != 0) catchpacket(d, pbuf, plen, slen, (void (*)())bcopy); }}/* * Copy data from an mbuf chain into a buffer. This code is derived * from m_copydata in sys/uipc_mbuf.c. */static voidbpf_m_copydata(src, dst, len) u_char *src; u_char *dst; register int len;{ register struct mbuf *m = (struct mbuf *)src; register unsigned count; while (len > 0) { if (m == 0) panic("bpf_m_copydata"); count = MIN(m->m_len, len); bcopy(mtod(m, caddr_t), (caddr_t)dst, count); len -= count; dst += count; m = m->m_next; }}/* * Length of ethernet and TCP/IP header header with no IP options. */#define BPF_MIN_SNAPLEN 50/* * bpf_mtap - incoming linkage from device drivers, when packet * is in an mbuf chain */voidbpf_mtap(bp, m0) struct bpf_if *bp; struct mbuf *m0;{ static u_char buf[BPF_MIN_SNAPLEN]; struct bpf_d *d; u_char *cp; u_int slen, plen; int nbytes; struct mbuf *m; if (m0->m_len >= BPF_MIN_SNAPLEN) { slen = m0->m_len; cp = mtod(m0, u_char *); } else { nbytes = BPF_MIN_SNAPLEN; cp = buf; m = m0; while (m && nbytes > 0) { slen = MIN(m->m_len, nbytes); bcopy(mtod(m, char *), (char *)cp, slen); cp += slen; nbytes -= slen; m = m->m_next; } if (nbytes > 0) /* Packet too small? */ return; slen = BPF_MIN_SNAPLEN; cp = buf; } plen = 0; m = m0; while (m) { plen += m->m_len; m = m->m_next; } for (d = bp->bif_dlist; d != 0; d = d->bd_next) { ++d->bd_rcount; slen = bpf_filter(d->bd_filter, cp, plen, slen); if (slen != 0) catchpacket(d, (u_char *)m0, plen, slen, bpf_m_copydata); }}/* * Move the packet data from interface memory ('pbuf') into the * store buffer. Return 1 if it's time to wakeup a listener (buffer full), * otherwise 0. 'copy' is the routine called to do the actual data * transfer. 'bcopy' is passed in to copy contiguous chunks, while * 'bpf_m_copydata' is passed in to copy mbuf chains. In the latter * case, 'pbuf' is really an mbuf. */static voidcatchpacket(d, pbuf, plen, snaplen, copy) struct bpf_d *d; u_char *pbuf; u_int plen, snaplen; void (*copy)();{ struct mbuf *m; struct bpf_hdr *hp; int totlen, curlen; int hdrlen = d->bd_bif->bif_hdrlen; /* * Figure out how many bytes to move. If the packet is * greater or equal to the snapshot length, transfer that * much. Otherwise, transfer the whole packet (unless * we hit the cluster limit). */ if (snaplen <= plen) totlen = snaplen + hdrlen; else { totlen = plen + hdrlen; if (totlen > MCLBYTES) totlen = MCLBYTES; } m = d->bd_sbuf; /* * Round up the end of the previous packet to the next longword. */ curlen = BPF_WORDALIGN(m->m_len); if (curlen + totlen > MCLBYTES) { /* * This packet will overflow the storage buffer. * Move the current cluster buffer to the hold slot, * and grab the free one. */ if (d->bd_fbuf == 0) { /* * We haven't completed the previous read yet? * Drop the packet. */ ++d->bd_dcount; return; } /* * Rotate the buffers. Move the 'store' buffer * into the 'hold' slot, and the 'free' buffer * into the 'store' slot. Zero out the length of * the new 'store' buffer. */ d->bd_hbuf = d->bd_sbuf; m = d->bd_sbuf = d->bd_fbuf; d->bd_fbuf = 0; curlen = m->m_len = 0; /* * Wake up anyone sleeping on this descriptor. */ wakeup((caddr_t)d); bpf_wakeup(d); } else if (d->bd_immediate) { /* * Immediate mode is set. A packet arrived so any * reads should be woken up. */ wakeup((caddr_t)d); bpf_wakeup(d); } /* * Append the bpf header. */ hp = (struct bpf_hdr *)(mtod(m, u_char *) + curlen);#ifdef sun uniqtime(&hp->bh_tstamp);#else#ifdef hp300 microtime(&hp->bh_tstamp);#else hp->bh_tstamp = time;#endif#endif hp->bh_datalen = plen; hp->bh_hdrlen = hdrlen; /* * Copy the packet data into the 'store' buffer and * update the cluster length. */ (*copy)(pbuf, (u_char *)hp + hdrlen, hp->bh_caplen = totlen - hdrlen); m->m_len = curlen + totlen;}/* * Allocate an mbuf cluster and clear its length field. * If resources unavaiable, return 0. * We can wait in MGET since we assume that we are called * at a low priority. */static struct mbuf *bpf_mcluster(){ struct mbuf *m; MGET(m, M_WAIT, MT_DATA); if (m == 0) return 0; MCLGET(m); if (m->m_len == MCLBYTES) { m->m_len = 0; return m; } m_freem(m); return 0;}/* * Initialize all nonzero fields of a descriptor. */static intbpf_initd(d) register struct bpf_d *d;{ struct mbuf *m; /* Get the buffer space. */ m = bpf_mcluster(); if (m == 0) return ENOBUFS; d->bd_fbuf = m; m = bpf_mcluster(); if (m == 0) { m_freem(d->bd_fbuf); return ENOBUFS; } d->bd_sbuf = m; return 0;}/* * Register 'ifp' with bpf. 'devp' is the link-level device descriptor * and 'driverp' is a pointer to the 'struct bpf_if *' in the driver's softc. */voidbpfattach(driverp, ifp, devp) caddr_t *driverp; struct ifnet *ifp; struct bpf_devp *devp;{ struct bpf_if *bp; int i; if (bpf_units >= NBPFILTER) { printf("bpf: too many interfaces: %s%d not attached\n", ifp->if_name, ifp->if_unit); return; } bp = &bpf_iftab[bpf_units++]; bp->bif_dlist = 0; bp->bif_driverp = (struct bpf_if **)driverp; bp->bif_ifp = ifp; bp->bif_devp = *devp; /* * Compute the length of the bpf header. This is not necessarily * equal to SIZEOF_BPF_HDR because we want to insert spacing such * that the network layer header begins on a longword boundary (for * performance reasons and to alleviate alignment restrictions). */ i = devp->bdev_hdrlen; bp->bif_hdrlen = BPF_WORDALIGN(i + SIZEOF_BPF_HDR) - i; /* * Mark all the descriptors free if this hasn't been done. */ if (!D_ISFREE(&bpf_dtab[0])) for (i = 0; i < NBPFILTER; ++i) D_MARKFREE(&bpf_dtab[i]); printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);}#endif (NBPFILTER > 0)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?