summaryrefslogtreecommitdiff
path: root/sys/net/bpf.c
diff options
context:
space:
mode:
authorHenning Brauer <henning@cvs.openbsd.org>2014-07-09 09:30:50 +0000
committerHenning Brauer <henning@cvs.openbsd.org>2014-07-09 09:30:50 +0000
commitf254be81afb848372994ebd35f0ea34a274acbc8 (patch)
treebf3c15ba2e1cb64cd763ce82c78c59fbb01d7377 /sys/net/bpf.c
parent07edae74825f47a40c1c4573651c84db9a7865ed (diff)
bpf code surgery / shuffling / simplification.
the various bpf_mtap_* are very similiar, they differ in what (and to some extent how) they prepend something, and what copy function they pass to bpf_catchpacket. use an internal _bpf_mtap as "backend" for bpf_mtap and friends. extend bpf_mtap_hdr so that it covers all common cases: if dlen is 0, nothing gets prepended. copy function can be given, if NULL the default bpf_mcopy is used. adjust the existing bpf_mtap_hdr users to pass a NULL ptr for the copy fn. re-implement bpf_mtap_af as simple wrapper for bpf_mtap_hdr. re-implement bpf_mtap_ether using bpf_map_hdr re-implement bpf_mtap_pflog as trivial bpf_mtap_hdr wrapper ok bluhm benno
Diffstat (limited to 'sys/net/bpf.c')
-rw-r--r--sys/net/bpf.c102
1 files changed, 39 insertions, 63 deletions
diff --git a/sys/net/bpf.c b/sys/net/bpf.c
index df40c12ff25..4db2310febb 100644
--- a/sys/net/bpf.c
+++ b/sys/net/bpf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bpf.c,v 1.93 2014/04/23 10:50:18 jca Exp $ */
+/* $OpenBSD: bpf.c,v 1.94 2014/07/09 09:30:49 henning Exp $ */
/* $NetBSD: bpf.c,v 1.33 1997/02/21 23:59:35 thorpej Exp $ */
/*
@@ -92,6 +92,8 @@ LIST_HEAD(, bpf_d) bpf_d_list;
void bpf_allocbufs(struct bpf_d *);
void bpf_freed(struct bpf_d *);
void bpf_ifname(struct ifnet *, struct ifreq *);
+void _bpf_mtap(caddr_t, struct mbuf *, u_int,
+ void (*)(const void *, void *, size_t));
void bpf_mcopy(const void *, void *, size_t);
int bpf_movein(struct uio *, u_int, struct mbuf **,
struct sockaddr *, struct bpf_insn *);
@@ -1159,10 +1161,11 @@ bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
}
/*
- * Incoming linkage from device drivers, when packet is in an mbuf chain.
+ * like bpf_mtap, but copy fn can be given. used by various bpf_mtap*
*/
void
-bpf_mtap(caddr_t arg, struct mbuf *m, u_int direction)
+_bpf_mtap(caddr_t arg, struct mbuf *m, u_int direction,
+ void (*cpfn)(const void *, void *, size_t))
{
struct bpf_if *bp = (struct bpf_if *)arg;
struct bpf_d *d;
@@ -1174,6 +1177,9 @@ bpf_mtap(caddr_t arg, struct mbuf *m, u_int direction)
if (m == NULL)
return;
+ if (cpfn == NULL)
+ cpfn = bpf_mcopy;
+
pktlen = 0;
for (m0 = m; m0 != 0; m0 = m0->m_next)
pktlen += m0->m_len;
@@ -1191,13 +1197,22 @@ bpf_mtap(caddr_t arg, struct mbuf *m, u_int direction)
if (!gottime++)
microtime(&tv);
- bpf_catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy, &tv);
+ bpf_catchpacket(d, (u_char *)m, pktlen, slen, cpfn, &tv);
if (d->bd_fildrop)
m->m_flags |= M_FILDROP;
}
}
/*
+ * Incoming linkage from device drivers, when packet is in an mbuf chain.
+ */
+void
+bpf_mtap(caddr_t arg, struct mbuf *m, u_int direction)
+{
+ _bpf_mtap(arg, m, direction, NULL);
+}
+
+/*
* Incoming linkage from device drivers, where we have a mbuf chain
* but need to prepend some arbitrary header from a linear buffer.
*
@@ -1208,17 +1223,23 @@ bpf_mtap(caddr_t arg, struct mbuf *m, u_int direction)
*/
void
bpf_mtap_hdr(caddr_t arg, caddr_t data, u_int dlen, struct mbuf *m,
- u_int direction)
+ u_int direction, void (*cpfn)(const void *, void *, size_t))
{
- struct m_hdr mh;
-
- mh.mh_flags = 0;
- mh.mh_next = m;
- mh.mh_len = dlen;
- mh.mh_data = data;
-
- bpf_mtap(arg, (struct mbuf *) &mh, direction);
- m->m_flags |= mh.mh_flags & M_FILDROP;
+ struct m_hdr mh;
+ struct mbuf *m0;
+
+ if (dlen > 0) {
+ mh.mh_flags = 0;
+ mh.mh_next = m;
+ mh.mh_len = dlen;
+ mh.mh_data = data;
+ m0 = (struct mbuf *)&mh;
+ } else
+ m0 = m;
+
+ _bpf_mtap(arg, m0, direction, cpfn);
+ if (m0 != m)
+ m->m_flags |= m0->m_flags & M_FILDROP;
}
/*
@@ -1233,17 +1254,10 @@ bpf_mtap_hdr(caddr_t arg, caddr_t data, u_int dlen, struct mbuf *m,
void
bpf_mtap_af(caddr_t arg, u_int32_t af, struct mbuf *m, u_int direction)
{
- struct m_hdr mh;
u_int32_t afh;
- mh.mh_flags = 0;
- mh.mh_next = m;
- mh.mh_len = 4;
afh = htonl(af);
- mh.mh_data = (caddr_t)&afh;
-
- bpf_mtap(arg, (struct mbuf *) &mh, direction);
- m->m_flags |= mh.mh_flags & M_FILDROP;
+ bpf_mtap_hdr(arg, (caddr_t)&afh, 4, m, direction, NULL);
}
/*
@@ -1259,7 +1273,6 @@ void
bpf_mtap_ether(caddr_t arg, struct mbuf *m, u_int direction)
{
#if NVLAN > 0
- struct m_hdr mh;
struct ether_vlan_header evh;
if ((m->m_flags & M_VLANTAG) == 0)
@@ -1277,13 +1290,7 @@ bpf_mtap_ether(caddr_t arg, struct mbuf *m, u_int direction)
m->m_len -= ETHER_HDR_LEN;
m->m_data += ETHER_HDR_LEN;
- mh.mh_flags = 0;
- mh.mh_next = m;
- mh.mh_len = sizeof(evh);
- mh.mh_data = (caddr_t)&evh;
-
- bpf_mtap(arg, (struct mbuf *) &mh, direction);
- m->m_flags |= mh.mh_flags & M_FILDROP;
+ bpf_mtap_hdr(arg, (caddr_t)&evh, sizeof(evh), m, direction, NULL);
m->m_len += ETHER_HDR_LEN;
m->m_data -= ETHER_HDR_LEN;
@@ -1294,42 +1301,11 @@ void
bpf_mtap_pflog(caddr_t arg, caddr_t data, struct mbuf *m)
{
#if NPFLOG > 0
- struct m_hdr mh;
- struct bpf_if *bp = (struct bpf_if *)arg;
- struct bpf_d *d;
- size_t pktlen, slen;
- struct mbuf *m0;
- struct timeval tv;
- int gottime = 0;
-
if (m == NULL)
return;
- mh.mh_flags = 0;
- mh.mh_next = m;
- mh.mh_len = PFLOG_HDRLEN;
- mh.mh_data = data;
-
- pktlen = mh.mh_len;
- for (m0 = m; m0 != 0; m0 = m0->m_next)
- pktlen += m0->m_len;
-
- for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
- ++d->bd_rcount;
- if ((BPF_DIRECTION_OUT & d->bd_dirfilt) != 0)
- slen = 0;
- else
- slen = bpf_filter(d->bd_rfilter, (u_char *)&mh,
- pktlen, 0);
-
- if (slen == 0)
- continue;
-
- if (!gottime++)
- microtime(&tv);
- bpf_catchpacket(d, (u_char *)&mh, pktlen, slen, pflog_bpfcopy,
- &tv);
- }
+ bpf_mtap_hdr(arg, data, PFLOG_HDRLEN, m, BPF_DIRECTION_OUT,
+ pflog_bpfcopy);
#endif
}