git: 35af22ac986c - main - if_tuntap: trigger the bpf hook on transmitting for the tap interface

From: Michael Tuexen <tuexen_at_FreeBSD.org>
Date: Sun, 05 Nov 2023 19:36:43 UTC
The branch main has been updated by tuexen:

URL: https://cgit.FreeBSD.org/src/commit/?id=35af22ac986c4f3e8160c943a478492e748b6f23

commit 35af22ac986c4f3e8160c943a478492e748b6f23
Author:     Michael Tuexen <tuexen@FreeBSD.org>
AuthorDate: 2023-11-05 19:32:46 +0000
Commit:     Michael Tuexen <tuexen@FreeBSD.org>
CommitDate: 2023-11-05 19:32:46 +0000

    if_tuntap: trigger the bpf hook on transmitting for the tap interface
    
    The tun interface triggers the bpf hook when a packet is transmitted,
    the tap interface triggers it when the packet is read from the
    character device. This is inconsistent.
    So fix the tap device such that it behaves like the tun device.
    This is needed for adding support for the tap device to packetdrill.
    
    Reviewed by:            kevans, rscheff
    MFC after:              1 week
    Sponsored by:           Netflix, Inc.
    Differential Revision:  https://reviews.freebsd.org/D42467
---
 sys/net/if_tuntap.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/sys/net/if_tuntap.c b/sys/net/if_tuntap.c
index 3eeaadb83a20..a865c23cb372 100644
--- a/sys/net/if_tuntap.c
+++ b/sys/net/if_tuntap.c
@@ -933,6 +933,16 @@ tunstart_l2(struct ifnet *ifp)
 	TUN_UNLOCK(tp);
 } /* tunstart_l2 */
 
+static int
+tap_transmit(struct ifnet *ifp, struct mbuf *m)
+{
+	int error;
+
+	BPF_MTAP(ifp, m);
+	IFQ_HANDOFF(ifp, m, error);
+	return (error);
+}
+
 /* XXX: should return an error code so it can fail. */
 static void
 tuncreate(struct cdev *dev)
@@ -972,6 +982,8 @@ tuncreate(struct cdev *dev)
 	if ((tp->tun_flags & TUN_L2) != 0) {
 		ifp->if_init = tunifinit;
 		ifp->if_start = tunstart_l2;
+		ifp->if_transmit = tap_transmit;
+		ifp->if_qflush = if_qflush;
 
 		ether_gen_addr(ifp, &eaddr);
 		ether_ifattach(ifp, eaddr.octet);
@@ -1713,9 +1725,6 @@ tunread(struct cdev *dev, struct uio *uio, int flag)
 	}
 	TUN_UNLOCK(tp);
 
-	if ((tp->tun_flags & TUN_L2) != 0)
-		BPF_MTAP(ifp, m);
-
 	len = min(tp->tun_vhdrlen, uio->uio_resid);
 	if (len > 0) {
 		struct virtio_net_hdr_mrg_rxbuf vhdr;