From f04d3a914d3dfe9f292f2bdac1f845e35138f3b0 Mon Sep 17 00:00:00 2001
From: Michael Tuexen <tuexen@fh-muenster.de>
Date: Thu, 1 Jun 2017 20:12:22 +0200
Subject: [PATCH] Load if_tun module on FreeBSD if needed.

---
 gtests/net/packetdrill/netdev.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/gtests/net/packetdrill/netdev.c b/gtests/net/packetdrill/netdev.c
index 18bdefb8..d895e5bb 100644
--- a/gtests/net/packetdrill/netdev.c
+++ b/gtests/net/packetdrill/netdev.c
@@ -112,10 +112,20 @@ static void check_remote_address(struct config *config,
 static void create_device(struct config *config, struct local_netdev *netdev)
 {
 	/* Open the tun device, which "clones" it for our purposes. */
-	int tun_fd = open(TUN_PATH, O_RDWR);
-	if (tun_fd < 0)
-		die_perror("open tun device");
+	int tun_fd;
 
+	tun_fd = open(TUN_PATH, O_RDWR);
+#if defined(__FreeBSD__)
+	if ((tun_fd < 0) && (errno == ENOENT)) {
+		if (system("kldload -q if_tun") < 0) {
+			die_perror("kldload -q if_tun");
+		}
+		tun_fd = open(TUN_PATH, O_RDWR);
+	}
+#endif
+	if (tun_fd < 0) {
+		die_perror("open tun device");
+	}
 	netdev->tun_fd = tun_fd;
 
 #ifdef linux
-- 
GitLab