Subject: Re: bpf patch: bpf_mtap2
To: None <tech-net@NetBSD.org, matt@NetBSD.org, darrenr@NetBSD.org>
From: David Young <dyoung@pobox.com>
List: tech-net
Date: 05/04/2004 20:46:59
--E/DnYTRukya0zdZ1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Here is another bpf_mtap2 patch.  Also, I have patched if_tun.c to use
bpf_mtap2, following a suggestion by Darren.

Dave

-- 
David Young             OJC Technologies
dyoung@ojctech.com      Urbana, IL * (217) 278-3933

--E/DnYTRukya0zdZ1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=bpf_mtap2

Index: net/bpf.c
===================================================================
RCS file: /cvsroot/src/sys/net/bpf.c,v
retrieving revision 1.96
diff -u -r1.96 bpf.c
--- net/bpf.c	30 Apr 2004 22:07:21 -0000	1.96
+++ net/bpf.c	5 May 2004 01:37:57 -0000
@@ -106,6 +106,9 @@
 struct bpf_d	bpf_dtab[NBPFILTER];
 
 static int	bpf_allocbufs __P((struct bpf_d *));
+static void	bpf_deliver(struct bpf_if *,
+		            void *(*cpfn)(void *, const void *, size_t),
+			    void *, u_int, u_int, struct ifnet *);
 static void	bpf_freed __P((struct bpf_d *));
 static void	bpf_ifname __P((struct ifnet *, struct ifreq *));
 static void	*bpf_mcpy __P((void *, const void *, size_t));
@@ -1257,18 +1260,6 @@
 	}
 }
 
-static __inline u_int
-bpf_measure(struct mbuf *m)
-{
-	struct mbuf *m0;
-	u_int pktlen;
-
-	pktlen = 0;
-	for (m0 = m; m0 != 0; m0 = m0->m_next)
-		pktlen += m0->m_len;
-	return pktlen;
-}
-
 /*
  * Incoming linkage from device drivers, when the head of the packet is in
  * a buffer, and the tail is in an mbuf chain.
@@ -1284,13 +1275,14 @@
 	u_int pktlen;
 	struct mbuf mb;
 
-	pktlen = bpf_measure(m) + dlen;
+	pktlen = m_length(m) + dlen;
 
 	/*
 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
 	 * Note that we cut corners here; we only setup what's
 	 * absolutely needed--this mbuf should never go anywhere else.
 	 */
+	(void)memset(&mb, 0, sizeof(mb));
 	mb.m_next = m;
 	mb.m_data = data;
 	mb.m_len = dlen;
@@ -1311,7 +1303,7 @@
 	u_int pktlen, buflen;
 	void *marg;
 
-	pktlen = bpf_measure(m);
+	pktlen = m_length(m);
 
 	if (pktlen == m->m_len) {
 		cpfn = memcpy;
Index: sys/mbuf.h
===================================================================
RCS file: /cvsroot/src/sys/sys/mbuf.h,v
retrieving revision 1.91
diff -u -r1.91 mbuf.h
--- sys/mbuf.h	9 Apr 2004 15:43:19 -0000	1.91
+++ sys/mbuf.h	5 May 2004 01:37:57 -0000
@@ -856,6 +856,9 @@
 void	m_reclaim(void *, int);
 void	mbinit(void);
 
+/* Inline routines. */
+static	u_int m_length(struct mbuf *);
+
 /* Packet tag routines */
 struct	m_tag *m_tag_get(int, int, int);
 void	m_tag_free(struct m_tag *);
@@ -892,6 +895,23 @@
 #define	PACKET_TAG_IPSEC_SOCKET			22 /* IPSEC socket ref */
 #define	PACKET_TAG_IPSEC_HISTORY		23 /* IPSEC history */
 
+/*
+ * Return the number of bytes in the mbuf chain, m.
+ */
+static __inline u_int
+m_length(struct mbuf *m)
+{
+	struct mbuf *m0;
+	u_int pktlen;
+
+	if ((m->m_flags & M_PKTHDR) != 0) 
+		return m->m_pkthdr.len;
+
+	pktlen = 0;
+	for (m0 = m; m0 != 0; m0 = m0->m_next)
+		pktlen += m0->m_len;
+	return pktlen;
+}
 
 #endif /* _KERNEL */
 #endif /* !_SYS_MBUF_H_ */

--E/DnYTRukya0zdZ1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=tun-patch

Index: net/if_tun.c
===================================================================
RCS file: /cvsroot/src/sys/net/if_tun.c,v
retrieving revision 1.68
diff -u -r1.68 if_tun.c
--- net/if_tun.c	1 Mar 2004 13:54:02 -0000	1.68
+++ net/if_tun.c	5 May 2004 01:46:09 -0000
@@ -459,20 +459,12 @@
 	if (ifp->if_bpf) {
 		/*
 		 * We need to prepend the address family as
-		 * a four byte field.  Cons up a dummy header
-		 * to pacify bpf.  This is safe because bpf
-		 * will only read from the mbuf (i.e., it won't
-		 * try to free it or keep a pointer to it).
+		 * a four-byte field.  Note that dst->sa_family
+		 * is not always a four-byte field.
 		 */
-		struct mbuf m;
 		u_int32_t af = dst->sa_family;
 
-		m.m_flags = 0;
-		m.m_next = m0;
-		m.m_len = sizeof(af);
-		m.m_data = (char *)&af;
-
-		bpf_mtap(ifp->if_bpf, &m);
+		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
 	}
 #endif
 

--E/DnYTRukya0zdZ1--