summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gwynne <dlg@cvs.openbsd.org>2018-02-01 12:10:28 +0000
committerDavid Gwynne <dlg@cvs.openbsd.org>2018-02-01 12:10:28 +0000
commitdcb688cc50469c003c564a5ea8fc9262ce63f4b2 (patch)
treea570bed945ec091ac5f44c796df54a85d72c69d9
parent8d2ff4a6e59d43d0c1adf093a491372c849d9bcb (diff)
add bpf_tap_hdr(), for handling a buffer (not an mbuf) with a header.
internally it uses mbufs to handle the chain of buffers, but the caller doesnt have to deal with that or allocate a temporary buffer with the header attached. ok mpi@
-rw-r--r--sys/net/bpf.c41
-rw-r--r--sys/net/bpf.h3
2 files changed, 42 insertions, 2 deletions
diff --git a/sys/net/bpf.c b/sys/net/bpf.c
index 2d3795199f0..af7406e1d00 100644
--- a/sys/net/bpf.c
+++ b/sys/net/bpf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bpf.c,v 1.166 2018/01/24 00:25:17 dlg Exp $ */
+/* $OpenBSD: bpf.c,v 1.167 2018/02/01 12:10:27 dlg Exp $ */
/* $NetBSD: bpf.c,v 1.33 1997/02/21 23:59:35 thorpej Exp $ */
/*
@@ -1291,6 +1291,45 @@ _bpf_mtap(caddr_t arg, const struct mbuf *m, u_int direction,
}
/*
+ * Incoming linkage from device drivers, where a data buffer should be
+ * prepended by an arbitrary header. In this situation we already have a
+ * way of representing a chain of memory buffers, ie, mbufs, so reuse
+ * the existing functionality by attaching the buffers to mbufs.
+ *
+ * Con up a minimal mbuf chain to pacify bpf by allocating (only) a
+ * struct m_hdr each for the header and data on the stack.
+ */
+int
+bpf_tap_hdr(caddr_t arg, const void *hdr, unsigned int hdrlen,
+ const void *buf, unsigned int buflen, u_int direction)
+{
+ struct m_hdr mh, md;
+ struct mbuf *m0 = NULL;
+ struct mbuf **mp = &m0;
+
+ if (hdr != NULL) {
+ mh.mh_flags = 0;
+ mh.mh_next = NULL;
+ mh.mh_len = hdrlen;
+ mh.mh_data = (void *)hdr;
+
+ *mp = (struct mbuf *)&mh;
+ mp = &mh.mh_next;
+ }
+
+ if (buf != NULL) {
+ md.mh_flags = 0;
+ md.mh_next = NULL;
+ md.mh_len = buflen;
+ md.mh_data = (void *)buf;
+
+ *mp = (struct mbuf *)&md;
+ }
+
+ return _bpf_mtap(arg, m0, direction, bpf_mcopy);
+}
+
+/*
* Incoming linkage from device drivers, when packet is in an mbuf chain.
*/
int
diff --git a/sys/net/bpf.h b/sys/net/bpf.h
index 98afe4f4f60..cb713eef697 100644
--- a/sys/net/bpf.h
+++ b/sys/net/bpf.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: bpf.h,v 1.63 2018/01/24 00:25:17 dlg Exp $ */
+/* $OpenBSD: bpf.h,v 1.64 2018/02/01 12:10:27 dlg Exp $ */
/* $NetBSD: bpf.h,v 1.15 1996/12/13 07:57:33 mikel Exp $ */
/*
@@ -311,6 +311,7 @@ int bpf_mtap_hdr(caddr_t, caddr_t, u_int, const struct mbuf *, u_int,
void (*)(const void *, void *, size_t));
int bpf_mtap_af(caddr_t, u_int32_t, const struct mbuf *, u_int);
int bpf_mtap_ether(caddr_t, const struct mbuf *, u_int);
+int bpf_tap_hdr(caddr_t, const void *, u_int, const void *, u_int, u_int);
void bpfattach(caddr_t *, struct ifnet *, u_int, u_int);
void bpfdetach(struct ifnet *);
void *bpfsattach(caddr_t *, const char *, u_int, u_int);