bpf: Use bpf global token instead mplock to protect bpf stuffs
authorSepherosa Ziehau <sephe@dragonflybsd.org>
Thu, 29 Nov 2012 09:16:06 +0000 (17:16 +0800)
committerSepherosa Ziehau <sephe@dragonflybsd.org>
Thu, 29 Nov 2012 09:17:10 +0000 (17:17 +0800)
Submitted-by: tuxillo@ w/ modification
21 files changed:
sys/dev/netif/ic/if_ic.c
sys/dev/netif/plip/if_plip.c
sys/net/bpf.c
sys/net/bpf.h
sys/net/disc/if_disc.c
sys/net/ef/if_ef.c
sys/net/ethernet.h
sys/net/faith/if_faith.c
sys/net/gif/if_gif.c
sys/net/gre/if_gre.c
sys/net/if_loop.c
sys/net/pf/if_pflog.c
sys/net/pf/if_pfsync.c
sys/net/sl/if_sl.c
sys/net/stf/if_stf.c
sys/net/tun/if_tun.c
sys/netgraph/fec/ng_fec.c
sys/netgraph/iface/ng_iface.c
sys/netgraph7/iface/ng_iface.c
sys/netinet/ip_gre.c
sys/netproto/802_11/wlan/ieee80211_radiotap.c

index c46dabf..bd1011d 100644 (file)
@@ -378,8 +378,12 @@ icoutput(struct ifnet *ifp, struct mbuf *m,
 
        } while ((mm = mm->m_next));
 
-       if (ifp->if_bpf)
-               bpf_ptap(ifp->if_bpf, m, &hdr, ICHDRLEN);
+       if (ifp->if_bpf) {
+               bpf_gettoken();
+               if (ifp->if_bpf)
+                       bpf_ptap(ifp->if_bpf, m, &hdr, ICHDRLEN);
+               bpf_reltoken();
+       }
 
        sc->ic_sending = 1;
 
index c559f48..f50e47b 100644 (file)
@@ -432,8 +432,12 @@ lptap(struct ifnet *ifp, struct mbuf *m)
         */
        static const uint32_t af = AF_INET;
 
-       if (ifp->if_bpf)
-               bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+       if (ifp->if_bpf) {
+               bpf_gettoken();
+               if (ifp->if_bpf)
+                       bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+               bpf_reltoken();
+       }
 }
 
 static void
index 1fb84c2..4f7ce91 100644 (file)
@@ -62,7 +62,6 @@
 #include <sys/vnode.h>
 
 #include <sys/thread2.h>
-#include <sys/mplock2.h>
 
 #include <net/if.h>
 #include <net/bpf.h>
@@ -109,6 +108,8 @@ SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
  */
 static struct bpf_if   *bpf_iflist;
 
+static struct lwkt_token bpf_token = LWKT_TOKEN_INITIALIZER(bpf_token);
+
 static int     bpf_allocbufs(struct bpf_d *);
 static void    bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
 static void    bpf_detachd(struct bpf_d *d);
@@ -140,7 +141,7 @@ static d_kqfilter_t bpfkqfilter;
 
 #define CDEV_MAJOR 23
 static struct dev_ops bpf_ops = {
-       { "bpf", 0, 0 },
+       { "bpf", 0, D_MPSAFE },
        .d_open =       bpfopen,
        .d_close =      bpfclose,
        .d_read =       bpfread,
@@ -263,11 +264,13 @@ bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
         * Finally, point the driver's bpf cookie at the interface so
         * it will divert packets to bpf.
         */
+       lwkt_gettoken(&bpf_token);
        d->bd_bif = bp;
        SLIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
        *bp->bif_driverp = bp;
 
        EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1);
+       lwkt_reltoken(&bpf_token);
 }
 
 /*
@@ -280,6 +283,7 @@ bpf_detachd(struct bpf_d *d)
        struct bpf_if *bp;
        struct ifnet *ifp;
 
+       lwkt_gettoken(&bpf_token);
        bp = d->bd_bif;
        ifp = bp->bif_ifp;
 
@@ -314,6 +318,7 @@ bpf_detachd(struct bpf_d *d)
                                  error);
                }
        }
+       lwkt_reltoken(&bpf_token);
 }
 
 /*
@@ -327,16 +332,21 @@ bpfopen(struct dev_open_args *ap)
        cdev_t dev = ap->a_head.a_dev;
        struct bpf_d *d;
 
-       if (ap->a_cred->cr_prison)
+       lwkt_gettoken(&bpf_token);
+       if (ap->a_cred->cr_prison) {
+               lwkt_reltoken(&bpf_token);
                return(EPERM);
+       }
 
        d = dev->si_drv1;
        /*
         * Each minor can be opened by only one process.  If the requested
         * minor is in use, return EBUSY.
         */
-       if (d != NULL)
+       if (d != NULL) {
+               lwkt_reltoken(&bpf_token);
                return(EBUSY);
+       }
 
        d = kmalloc(sizeof *d, M_BPF, M_WAITOK | M_ZERO);
        dev->si_drv1 = d;
@@ -344,6 +354,8 @@ bpfopen(struct dev_open_args *ap)
        d->bd_sig = SIGIO;
        d->bd_seesent = 1;
        callout_init(&d->bd_callout);
+       lwkt_reltoken(&bpf_token);
+
        return(0);
 }
 
@@ -369,6 +381,7 @@ bpfclose(struct dev_close_args *ap)
        cdev_t dev = ap->a_head.a_dev;
        struct bpf_d *d = dev->si_drv1;
 
+       lwkt_gettoken(&bpf_token);
        funsetown(&d->bd_sigio);
        crit_enter();
        if (d->bd_state == BPF_WAITING)
@@ -384,6 +397,8 @@ bpfclose(struct dev_close_args *ap)
                destroy_dev(dev);
        }
        kfree(d, M_BPF);
+       lwkt_reltoken(&bpf_token);
+
        return(0);
 }
 
@@ -409,12 +424,15 @@ bpfread(struct dev_read_args *ap)
        int timed_out;
        int error;
 
+       lwkt_gettoken(&bpf_token);
        /*
         * Restrict application to use a buffer the same size as
         * as kernel buffers.
         */
-       if (ap->a_uio->uio_resid != d->bd_bufsize)
+       if (ap->a_uio->uio_resid != d->bd_bufsize) {
+               lwkt_reltoken(&bpf_token);
                return(EINVAL);
+       }
 
        crit_enter();
        if (d->bd_state == BPF_WAITING)
@@ -449,16 +467,19 @@ bpfread(struct dev_read_args *ap)
                 */
                if (d->bd_bif == NULL) {
                        crit_exit();
+                       lwkt_reltoken(&bpf_token);
                        return(ENXIO);
                }
 
                if (ap->a_ioflag & IO_NDELAY) {
                        crit_exit();
+                       lwkt_reltoken(&bpf_token);
                        return(EWOULDBLOCK);
                }
                error = tsleep(d, PCATCH, "bpf", d->bd_rtout);
                if (error == EINTR || error == ERESTART) {
                        crit_exit();
+                       lwkt_reltoken(&bpf_token);
                        return(error);
                }
                if (error == EWOULDBLOCK) {
@@ -477,6 +498,7 @@ bpfread(struct dev_read_args *ap)
 
                        if (d->bd_slen == 0) {
                                crit_exit();
+                               lwkt_reltoken(&bpf_token);
                                return(0);
                        }
                        ROTATE_BUFFERS(d);
@@ -500,6 +522,7 @@ bpfread(struct dev_read_args *ap)
        d->bd_hbuf = NULL;
        d->bd_hlen = 0;
        crit_exit();
+       lwkt_reltoken(&bpf_token);
 
        return(error);
 }
@@ -519,9 +542,7 @@ bpf_wakeup(struct bpf_d *d)
        if (d->bd_async && d->bd_sig && d->bd_sigio)
                pgsigio(d->bd_sigio, d->bd_sig, 0);
 
-       get_mplock();
        KNOTE(&d->bd_kq.ki_note, 0);
-       rel_mplock();
 }
 
 static void
@@ -559,26 +580,34 @@ bpfwrite(struct dev_write_args *ap)
        struct bpf_d *d = dev->si_drv1;
        struct ifnet *ifp;
        struct mbuf *m;
-       int error;
+       int error, ret;
        struct sockaddr dst;
        int datlen;
        struct netmsg_bpf_output bmsg;
 
-       if (d->bd_bif == NULL)
+       lwkt_gettoken(&bpf_token);
+       if (d->bd_bif == NULL) {
+               lwkt_reltoken(&bpf_token);
                return(ENXIO);
+       }
 
        ifp = d->bd_bif->bif_ifp;
 
-       if (ap->a_uio->uio_resid == 0)
+       if (ap->a_uio->uio_resid == 0) {
+               lwkt_reltoken(&bpf_token);
                return(0);
+       }
 
        error = bpf_movein(ap->a_uio, (int)d->bd_bif->bif_dlt, &m,
                           &dst, &datlen, d->bd_wfilter);
-       if (error)
+       if (error) {
+               lwkt_reltoken(&bpf_token);
                return(error);
+       }
 
        if (datlen > ifp->if_mtu) {
                m_freem(m);
+               lwkt_reltoken(&bpf_token);
                return(EMSGSIZE);
        }
 
@@ -591,7 +620,10 @@ bpfwrite(struct dev_write_args *ap)
        bmsg.nm_ifp = ifp;
        bmsg.nm_dst = &dst;
 
-       return lwkt_domsg(netisr_portfn(0), &bmsg.base.lmsg, 0);
+       ret = lwkt_domsg(netisr_portfn(0), &bmsg.base.lmsg, 0);
+       lwkt_reltoken(&bpf_token);
+
+       return ret;
 }
 
 /*
@@ -642,6 +674,7 @@ bpfioctl(struct dev_ioctl_args *ap)
        struct bpf_d *d = dev->si_drv1;
        int error = 0;
 
+       lwkt_gettoken(&bpf_token);
        crit_enter();
        if (d->bd_state == BPF_WAITING)
                callout_stop(&d->bd_callout);
@@ -667,6 +700,7 @@ bpfioctl(struct dev_ioctl_args *ap)
                case TIOCGPGRP:
                        break;
                default:
+                       lwkt_reltoken(&bpf_token);
                        return (EPERM);
                }
        }
@@ -949,6 +983,8 @@ bpfioctl(struct dev_ioctl_args *ap)
                d->bd_locked = 1;
                break;
        }
+       lwkt_reltoken(&bpf_token);
+
        return(error);
 }
 
@@ -1076,9 +1112,11 @@ bpfkqfilter(struct dev_kqfilter_args *ap)
        struct klist *klist;
        struct bpf_d *d;
 
+       lwkt_gettoken(&bpf_token);
        d = dev->si_drv1;
        if (d->bd_bif == NULL) {
                ap->a_result = 1;
+               lwkt_reltoken(&bpf_token);
                return (0);
        }
 
@@ -1090,11 +1128,13 @@ bpfkqfilter(struct dev_kqfilter_args *ap)
                break;
        default:
                ap->a_result = EOPNOTSUPP;
+               lwkt_reltoken(&bpf_token);
                return (0);
        }
 
        klist = &d->bd_kq.ki_note;
        knote_insert(klist, kn);
+       lwkt_reltoken(&bpf_token);
 
        return (0);
 }
@@ -1149,11 +1189,10 @@ bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
        int gottime = 0;
        u_int slen;
 
-       get_mplock();
-
+       lwkt_gettoken(&bpf_token);
        /* Re-check */
        if (bp == NULL) {
-               rel_mplock();
+               lwkt_reltoken(&bpf_token);
                return;
        }
 
@@ -1173,8 +1212,7 @@ bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
                        catchpacket(d, pkt, pktlen, slen, ovbcopy, &tv);
                }
        }
-
-       rel_mplock();
+       lwkt_reltoken(&bpf_token);
 }
 
 /*
@@ -1214,17 +1252,16 @@ bpf_mtap(struct bpf_if *bp, struct mbuf *m)
        struct timeval tv;
        int gottime = 0;
 
-       get_mplock();
-
+       lwkt_gettoken(&bpf_token);
        /* Re-check */
        if (bp == NULL) {
-               rel_mplock();
+               lwkt_reltoken(&bpf_token);
                return;
        }
 
        /* Don't compute pktlen, if no descriptor is attached. */
        if (SLIST_EMPTY(&bp->bif_dlist)) {
-               rel_mplock();
+               lwkt_reltoken(&bpf_token);
                return;
        }
 
@@ -1244,8 +1281,7 @@ bpf_mtap(struct bpf_if *bp, struct mbuf *m)
                                    &tv);
                }
        }
-
-       rel_mplock();
+       lwkt_reltoken(&bpf_token);
 }
 
 /*
@@ -1435,6 +1471,8 @@ bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driver
 
        bp = kmalloc(sizeof *bp, M_BPF, M_WAITOK | M_ZERO);
 
+       lwkt_gettoken(&bpf_token);
+
        SLIST_INIT(&bp->bif_dlist);
        bp->bif_ifp = ifp;
        bp->bif_dlt = dlt;
@@ -1452,6 +1490,8 @@ bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driver
         */
        bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
 
+       lwkt_reltoken(&bpf_token);
+
        if (bootverbose)
                if_printf(ifp, "bpf attached\n");
 }
@@ -1468,6 +1508,7 @@ bpfdetach(struct ifnet *ifp)
        struct bpf_if *bp, *bp_prev;
        struct bpf_d *d;
 
+       lwkt_gettoken(&bpf_token);
        crit_enter();
 
        /* Locate BPF interface information */
@@ -1481,6 +1522,7 @@ bpfdetach(struct ifnet *ifp)
        /* Interface wasn't attached */
        if (bp->bif_ifp == NULL) {
                crit_exit();
+               lwkt_reltoken(&bpf_token);
                kprintf("bpfdetach: %s was not attached\n", ifp->if_xname);
                return;
        }
@@ -1498,6 +1540,7 @@ bpfdetach(struct ifnet *ifp)
        kfree(bp, M_BPF);
 
        crit_exit();
+       lwkt_reltoken(&bpf_token);
 }
 
 /*
@@ -1567,6 +1610,18 @@ bpf_setdlt(struct bpf_d *d, u_int dlt)
        return(bp == NULL ? EINVAL : 0);
 }
 
+void
+bpf_gettoken(void)
+{
+       lwkt_gettoken(&bpf_token);
+}
+
+void
+bpf_reltoken(void)
+{
+       lwkt_reltoken(&bpf_token);
+}
+
 static void
 bpf_drvinit(void *unused)
 {
@@ -1635,4 +1690,14 @@ bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
        return -1;      /* "no filter" behaviour */
 }
 
+void
+bpf_gettoken(void)
+{
+}
+
+void
+bpf_reltoken(void)
+{
+}
+
 #endif /* !BPF */
index d205afe..f497276 100644 (file)
@@ -39,7 +39,6 @@
  *     @(#)bpf.h       1.34 (LBL)     6/16/96
  *
  * $FreeBSD: src/sys/net/bpf.h,v 1.21.2.4 2002/07/05 14:40:00 fenner Exp $
- * $DragonFly: src/sys/net/bpf.h,v 1.12 2008/03/14 09:52:10 matthias Exp $
  */
 
 #ifndef _NET_BPF_H_
@@ -241,6 +240,7 @@ struct bpf_dltlist {
 };
 
 #ifdef _KERNEL
+
 struct bpf_if;
 struct ifnet;
 struct mbuf;
@@ -254,19 +254,30 @@ void       bpf_ptap(struct bpf_if *, struct mbuf *, const void *, u_int);
 void    bpfattach(struct ifnet *, u_int, u_int);
 void    bpfattach_dlt(struct ifnet *, u_int, u_int, struct bpf_if **);
 void    bpfdetach(struct ifnet *);
+void    bpf_gettoken(void);
+void    bpf_reltoken(void);
 
-void    bpfilterattach(int);
 u_int   bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);
 
-#define        BPF_TAP(_ifp,_pkt,_pktlen) do {                         \
-       if ((_ifp)->if_bpf)                                     \
-               bpf_tap((_ifp)->if_bpf, (_pkt), (_pktlen));     \
+#define        BPF_TAP(_ifp,_pkt,_pktlen) do {                                 \
+       if ((_ifp)->if_bpf) {                                           \
+               bpf_gettoken();                                         \
+               if ((_ifp)->if_bpf)                                     \
+                       bpf_tap((_ifp)->if_bpf, (_pkt), (_pktlen));     \
+               bpf_reltoken();                                         \
+       }                                                               \
 } while (0)
-#define        BPF_MTAP(_ifp,_m) do {                                  \
-       if ((_ifp)->if_bpf)                                     \
-               bpf_mtap((_ifp)->if_bpf, (_m));                 \
+
+#define        BPF_MTAP(_ifp,_m) do {                                          \
+       if ((_ifp)->if_bpf) {                                           \
+               bpf_gettoken();                                         \
+               if ((_ifp)->if_bpf)                                     \
+                       bpf_mtap((_ifp)->if_bpf, (_m));                 \
+               bpf_reltoken();                                         \
+       }                                                               \
 } while (0)
-#endif
+
+#endif /* _KERNEL */
 
 /*
  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
index c183464..4f5cbb4 100644 (file)
@@ -125,13 +125,17 @@ discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
        }
 
        if (discif.if_bpf) {
-               /*
-                * We need to prepend the address family as
-                * a four byte field.
-                */
-               uint32_t af = dst->sa_family;
-
-               bpf_ptap(discif.if_bpf, m, &af, sizeof(af));
+               bpf_gettoken();
+               if (discif.if_bpf) {
+                       /*
+                        * We need to prepend the address family as
+                        * a four byte field.
+                        */
+                       uint32_t af = dst->sa_family;
+
+                       bpf_ptap(discif.if_bpf, m, &af, sizeof(af));
+               }
+               bpf_reltoken();
        }
        m->m_pkthdr.rcvif = ifp;
 
index 5abe9fe..463503f 100644 (file)
@@ -366,8 +366,12 @@ ef_input(struct ifnet *ifp, const struct ether_header *eh, struct mbuf *m)
        eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
        m->m_pkthdr.rcvif = eifp;
 
-       if (eifp->if_bpf)
-               bpf_ptap(eifp->if_bpf, m, eh, ETHER_HDR_LEN);
+       if (eifp->if_bpf) {
+               bpf_gettoken();
+               if (eifp->if_bpf)
+                       bpf_ptap(eifp->if_bpf, m, eh, ETHER_HDR_LEN);
+               bpf_reltoken();
+       }
 
        /*
         * Now we ready to adjust mbufs and pass them to protocol intr's
index 48c9a37..4cae254 100644 (file)
@@ -389,12 +389,16 @@ extern    void (*vlan_input_p)(struct mbuf *m);
 #define ETHER_BPF_MTAP(_ifp, _m) do {                                  \
        if ((_ifp)->if_bpf) {                                           \
                M_ASSERTPKTHDR((_m));                                   \
-               if ((_m)->m_flags & M_VLANTAG) {                        \
-                       vlan_ether_ptap((_ifp)->if_bpf, (_m),           \
-                                       (_m)->m_pkthdr.ether_vlantag);  \
-               } else {                                                \
-                       bpf_mtap((_ifp)->if_bpf, (_m));                 \
+               bpf_gettoken();                                         \
+               if ((_ifp->if_bpf)) {                                   \
+                       if ((_m)->m_flags & M_VLANTAG) {                \
+                               vlan_ether_ptap((_ifp)->if_bpf, (_m),   \
+                                   (_m)->m_pkthdr.ether_vlantag);      \
+                       } else {                                        \
+                               bpf_mtap((_ifp)->if_bpf, (_m));         \
+                       }                                               \
                }                                                       \
+               bpf_reltoken();                                         \
        }                                                               \
 } while (0)
 
index 518ee8b..b1a1dc7 100644 (file)
@@ -211,13 +211,17 @@ faithoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
        }
 
        if (ifp->if_bpf != NULL) {
-               /*
-                * We need to prepend the address family as
-                * a four byte field.
-                */
-               uint32_t af = dst->sa_family;
-
-               bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+               bpf_gettoken();
+               if (ifp->if_bpf != NULL) {
+                       /*
+                        * We need to prepend the address family as
+                        * a four byte field.
+                        */
+                       uint32_t af = dst->sa_family;
+
+                       bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+               }
+               bpf_reltoken();
        }
 
        if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
index 5cd1b06..d4849b8 100644 (file)
@@ -346,13 +346,17 @@ gif_output_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
        }
 
        if (ifp->if_bpf) {
-               /*
-                * We need to prepend the address family as
-                * a four byte field.
-                */
-               uint32_t af = dst->sa_family;
-
-               bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+               bpf_gettoken();
+               if (ifp->if_bpf) {
+                       /*
+                        * We need to prepend the address family as
+                        * a four byte field.
+                        */
+                       uint32_t af = dst->sa_family;
+
+                       bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+               }
+               bpf_reltoken();
        }
        ifp->if_opackets++;     
        ifp->if_obytes += m->m_pkthdr.len;
@@ -412,13 +416,17 @@ gif_input(struct mbuf *m, int af, struct ifnet *ifp)
        m->m_pkthdr.rcvif = ifp;
        
        if (ifp->if_bpf) {
-               /*
-                * We need to prepend the address family as
-                * a four byte field.
-                */
-               uint32_t af1 = af;
-
-               bpf_ptap(ifp->if_bpf, m, &af1, sizeof(af1));
+               bpf_gettoken();
+               if (ifp->if_bpf) {
+                       /*
+                        * We need to prepend the address family as
+                        * a four byte field.
+                        */
+                       uint32_t af1 = af;
+
+                       bpf_ptap(ifp->if_bpf, m, &af1, sizeof(af1));
+               }
+               bpf_reltoken();
        }
 
        /*
index d249853..a908db6 100644 (file)
@@ -261,9 +261,13 @@ gre_output_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
        ip = NULL;
 
        if (ifp->if_bpf) {
-               uint32_t af = dst->sa_family;
+               bpf_gettoken();
+               if (ifp->if_bpf) {
+                       uint32_t af = dst->sa_family;
 
-               bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+                       bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+               }
+               bpf_reltoken();
        }
 
        m->m_flags &= ~(M_BCAST|M_MCAST);
index e3f6724..7659f27 100644 (file)
@@ -199,7 +199,7 @@ if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
        }
 
        if (ifp->if_bpf) {
-               get_mplock();
+               bpf_gettoken();
 
                /* Re-check */
                if (ifp->if_bpf == NULL)
@@ -212,7 +212,7 @@ if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
                        bpf_mtap(ifp->if_bpf, m);
                }
 rel:
-               rel_mplock();
+               bpf_reltoken();
        }
 
        /* Strip away media header */
index c83b91d..fd49342 100644 (file)
@@ -304,10 +304,14 @@ pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
 
        ifn->if_opackets++;
        ifn->if_obytes += m->m_pkthdr.len;
-       lwkt_reltoken(&pf_token);
-       bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
-           BPF_DIRECTION_OUT);
-       lwkt_gettoken(&pf_token);
+       if (ifn->if_bpf) {
+               bpf_gettoken();
+               if (ifn->if_bpf) {
+                       bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
+                           BPF_DIRECTION_OUT);
+               }
+               bpf_reltoken();
+       }
 
 #ifdef INET
        if (af == AF_INET) {
index 4c986bd..bf73c4b 100644 (file)
@@ -1638,9 +1638,10 @@ pfsync_sendout(struct pfsync_softc *sc)
 
 #if NBPFILTER > 0
        if (ifp->if_bpf) {
-               lwkt_reltoken(&pf_token);
-               bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
-               lwkt_gettoken(&pf_token);
+               bpf_gettoken();
+               if (ifp->if_bpf)
+                       bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
+               bpf_reltoken();
        }
 #endif
 
index 1b87c3c..d78e8d1 100644 (file)
@@ -623,14 +623,22 @@ slstart(struct tty *tp)
                                    &sc->sc_comp, 1);
                }
                if (sc->sc_if.if_bpf) {
-                       /*
-                        * Put the SLIP pseudo-"link header" in place.  The
-                        * compressed header is now at the beginning of the
-                        * mbuf.
-                        */
-                       bpfbuf[SLX_DIR] = SLIPDIR_OUT;
-                       bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
-                       bpf_tap(sc->sc_if.if_bpf, bpfbuf, len + SLIP_HDRLEN);
+                       bpf_gettoken();
+
+                       if (sc->sc_if.if_bpf) {
+                               /*
+                                * Put the SLIP pseudo-"link header" in place.
+                                * The compressed header is now at the
+                                * beginning of the mbuf.
+                                */
+                               bpfbuf[SLX_DIR] = SLIPDIR_OUT;
+                               bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR],
+                                   CHDR_LEN);
+                               bpf_tap(sc->sc_if.if_bpf, bpfbuf,
+                                   len + SLIP_HDRLEN);
+                       }
+
+                       bpf_reltoken();
                }
 
                /*
@@ -890,17 +898,24 @@ slinput(int c, struct tty *tp)
                                goto error;
                }
                if (sc->sc_if.if_bpf) {
-                       /*
-                        * Put the SLIP pseudo-"link header" in place.
-                        * We couldn't do this any earlier since
-                        * decompression probably moved the buffer
-                        * pointer.  Then, invoke BPF.
-                        */
-                       u_char *hp = sc->sc_buf - SLIP_HDRLEN;
+                       bpf_gettoken();
+
+                       if (sc->sc_if.if_bpf) {
+                               /*
+                                * Put the SLIP pseudo-"link header" in place.
+                                * We couldn't do this any earlier since
+                                * decompression probably moved the buffer
+                                * pointer.  Then, invoke BPF.
+                                */
+                               u_char *hp = sc->sc_buf - SLIP_HDRLEN;
+
+                               hp[SLX_DIR] = SLIPDIR_IN;
+                               bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
+                               bpf_tap(sc->sc_if.if_bpf, hp,
+                                   len + SLIP_HDRLEN);
+                       }
 
-                       hp[SLX_DIR] = SLIPDIR_IN;
-                       bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
-                       bpf_tap(sc->sc_if.if_bpf, hp, len + SLIP_HDRLEN);
+                       bpf_reltoken();
                }
                m = sl_btom(sc, len);
                if (m == NULL)
index 1386c58..b9316cc 100644 (file)
@@ -365,8 +365,12 @@ stf_output_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
                return ENETUNREACH;
        }
 
-       if (ifp->if_bpf)
-               bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+       if (ifp->if_bpf) {
+               bpf_gettoken();
+               if (ifp->if_bpf)
+                       bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+               bpf_reltoken();
+       }
 
        M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
        if (m && m->m_len < sizeof(struct ip))
@@ -581,8 +585,12 @@ in_stf_input(struct mbuf **mp, int *offp, int proto)
 
        m->m_pkthdr.rcvif = ifp;
 
-       if (ifp->if_bpf)
-               bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+       if (ifp->if_bpf) {
+               bpf_gettoken();
+               if (ifp->if_bpf)
+                       bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
+               bpf_reltoken();
+       }
 
        /*
         * Put the packet to the network layer input queue according to the
index 84e7ea2..3387a88 100644 (file)
@@ -340,13 +340,17 @@ tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
        }
 
        if (ifp->if_bpf) {
-               /*
-                * We need to prepend the address family as
-                * a four byte field.
-                */
-               uint32_t af = dst->sa_family;
+               bpf_gettoken();
+               if (ifp->if_bpf) {
+                       /*
+                        * We need to prepend the address family as
+                        * a four byte field.
+                        */
+                       uint32_t af = dst->sa_family;
 
-               bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
+                       bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
+               }
+               bpf_reltoken();
        }
 
        /* prepend sockaddr? this may abort if the mbuf allocation fails */
@@ -635,28 +639,38 @@ tunwrite(struct dev_write_args *ap)
        top->m_pkthdr.rcvif = ifp;
 
        if (ifp->if_bpf) {
-               if (tp->tun_flags & TUN_IFHEAD) {
-                       /*
-                        * Conveniently, we already have a 4-byte address
-                        * family prepended to our packet !
-                        * Inconveniently, it's in the wrong byte order !
-                        */
-                       if ((top = m_pullup(top, sizeof(family))) == NULL)
-                               return ENOBUFS;
-                       *mtod(top, u_int32_t *) =
-                           ntohl(*mtod(top, u_int32_t *));
-                       bpf_mtap(ifp->if_bpf, top);
-                       *mtod(top, u_int32_t *) =
-                           htonl(*mtod(top, u_int32_t *));
-               } else {
-                       /*
-                        * We need to prepend the address family as
-                        * a four byte field.
-                        */
-                       static const uint32_t af = AF_INET;
-
-                       bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
+               bpf_gettoken();
+
+               if (ifp->if_bpf) {
+                       if (tp->tun_flags & TUN_IFHEAD) {
+                               /*
+                                * Conveniently, we already have a 4-byte
+                                * address family prepended to our packet !
+                                * Inconveniently, it's in the wrong byte
+                                * order !
+                                */
+                               if ((top = m_pullup(top, sizeof(family)))
+                                   == NULL) {
+                                       bpf_reltoken();
+                                       return ENOBUFS;
+                               }
+                               *mtod(top, u_int32_t *) =
+                                   ntohl(*mtod(top, u_int32_t *));
+                               bpf_mtap(ifp->if_bpf, top);
+                               *mtod(top, u_int32_t *) =
+                                   htonl(*mtod(top, u_int32_t *));
+                       } else {
+                               /*
+                                * We need to prepend the address family as
+                                * a four byte field.
+                                */
+                               static const uint32_t af = AF_INET;
+
+                               bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
+                       }
                }
+
+               bpf_reltoken();
        }
 
        if (tp->tun_flags & TUN_IFHEAD) {
index 16dbcb9..97ccd71 100644 (file)
@@ -758,8 +758,12 @@ ng_fec_input(struct ifnet *ifp, struct mbuf **m0)
        bifp->if_ipackets++;
        bifp->if_ibytes += m->m_pkthdr.len;
 
-       if (bifp->if_bpf)
-               bpf_mtap(bifp->if_bpf, m);
+       if (bifp->if_bpf) {
+               bpf_gettoken();
+               if (bifp->if_bpf)
+                       bpf_mtap(bifp->if_bpf, m);
+               bpf_reltoken();
+       }
 }
 
 /*
index 1eafd75..61435d0 100644 (file)
@@ -488,8 +488,12 @@ ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
 
        KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
 
-       if (ifp->if_bpf)
-               bpf_ptap(ifp->if_bpf, m, &family, sizeof(family4));
+       if (ifp->if_bpf) {
+               bpf_gettoken();
+               if (ifp->if_bpf)
+                       bpf_ptap(ifp->if_bpf, m, &family, sizeof(family4));
+               bpf_reltoken();
+       }
 }
 
 #ifdef DEBUG
index 5c3b3d3..6cc5383 100644 (file)
@@ -483,8 +483,12 @@ ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
 
        KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
 
-       if (ifp->if_bpf)
-               bpf_ptap(ifp->if_bpf, m, &family4, sizeof(family4));
+       if (ifp->if_bpf) {
+               bpf_gettoken();
+               if (ifp->if_bpf)
+                       bpf_ptap(ifp->if_bpf, m, &family4, sizeof(family4));
+               bpf_reltoken();
+       }
 }
 
 /*
index b976f52..09d67e6 100644 (file)
@@ -181,8 +181,12 @@ gre_input2(struct mbuf *m ,int hlen, u_char proto)
        m->m_len -= hlen;
        m->m_pkthdr.len -= hlen;
 
-       if (sc->sc_if.if_bpf)
-               bpf_ptap(sc->sc_if.if_bpf, m, &af, sizeof(af));
+       if (sc->sc_if.if_bpf) {
+               bpf_gettoken();
+               if (sc->sc_if.if_bpf)
+                       bpf_ptap(sc->sc_if.if_bpf, m, &af, sizeof(af));
+               bpf_reltoken();
+       }
 
        m->m_pkthdr.rcvif = &sc->sc_if;
        netisr_queue(isr, m);
@@ -248,8 +252,12 @@ gre_mobile_input(struct mbuf **mp, int *offp, int proto)
        ip->ip_sum = 0;
        ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
 
-       if (sc->sc_if.if_bpf)
-               bpf_ptap(sc->sc_if.if_bpf, m, &af, sizeof(af));
+       if (sc->sc_if.if_bpf) {
+               bpf_gettoken();
+               if (sc->sc_if.if_bpf)
+                       bpf_ptap(sc->sc_if.if_bpf, m, &af, sizeof(af));
+               bpf_reltoken();
+       }
 
        m->m_pkthdr.rcvif = &sc->sc_if;
 
index 6710a88..3a468b8 100644 (file)
@@ -183,8 +183,14 @@ spam_vaps(struct ieee80211vap *vap0, struct mbuf *m,
                if (vap != vap0 &&
                    vap->iv_opmode == IEEE80211_M_MONITOR &&
                    (vap->iv_flags_ext & IEEE80211_FEXT_BPF) &&
-                   vap->iv_state != IEEE80211_S_INIT)
-                       bpf_ptap(vap->iv_rawbpf, m, rh, len);
+                   vap->iv_state != IEEE80211_S_INIT) {
+                       if (vap->iv_rawbpf) {
+                               bpf_gettoken();
+                               if (vap->iv_rawbpf)
+                                       bpf_ptap(vap->iv_rawbpf, m, rh, len);
+                               bpf_reltoken();
+                       }
+               }
        }
 }
 
@@ -201,8 +207,14 @@ ieee80211_radiotap_tx(struct ieee80211vap *vap0, struct mbuf *m)
        KASSERT(th != NULL, ("no tx radiotap header"));
        len = le16toh(th->it_len);
 
-       if (vap0->iv_flags_ext & IEEE80211_FEXT_BPF)
-               bpf_ptap(vap0->iv_rawbpf, m, th, len);
+       if (vap0->iv_flags_ext & IEEE80211_FEXT_BPF) {
+               if (vap0->iv_rawbpf) {
+                       bpf_gettoken();
+                       if (vap0->iv_rawbpf)
+                               bpf_ptap(vap0->iv_rawbpf, m, th, len);
+                       bpf_reltoken();
+               }
+       }
        /*
         * Spam monitor mode vaps.
         */
@@ -223,8 +235,14 @@ ieee80211_radiotap_rx(struct ieee80211vap *vap0, struct mbuf *m)
        KASSERT(rh != NULL, ("no rx radiotap header"));
        len = le16toh(rh->it_len);
 
-       if (vap0->iv_flags_ext & IEEE80211_FEXT_BPF)
-               bpf_ptap(vap0->iv_rawbpf, m, rh, len);
+       if (vap0->iv_flags_ext & IEEE80211_FEXT_BPF) {
+               if (vap0->iv_rawbpf) {
+                       bpf_gettoken();
+                       if (vap0->iv_rawbpf)
+                               bpf_ptap(vap0->iv_rawbpf, m, rh, len);
+                       bpf_reltoken();
+               }
+       }
        /*
         * Spam monitor mode vaps with unicast frames.  Multicast
         * frames are handled by passing through ieee80211_input_all
@@ -249,8 +267,14 @@ ieee80211_radiotap_rx_all(struct ieee80211com *ic, struct mbuf *m)
        /* XXX locking? */
        TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
                if (ieee80211_radiotap_active_vap(vap) &&
-                   vap->iv_state != IEEE80211_S_INIT)
-                       bpf_ptap(vap->iv_rawbpf, m, rh, len);
+                   vap->iv_state != IEEE80211_S_INIT) {
+                       if (vap->iv_rawbpf) {
+                               bpf_gettoken();
+                               if (vap->iv_rawbpf)
+                                       bpf_ptap(vap->iv_rawbpf, m, rh, len);
+                               bpf_reltoken();
+                       }
+               }
        }
 }