--- a/etc/inc/openvpn.inc
+++ b/etc/inc/openvpn.inc
@@ -209,6 +209,134 @@
 	return($list);
 }
 
+/*
+ * DCO patch (local, not from Netgate): helpers for OpenVPN Data Channel
+ * Offload with a self-built if_ovpn.ko on pfSense CE.
+ */
+
+/* Is the if_ovpn kernel module loaded? */
+function openvpn_dco_module_loaded() : bool {
+	$out = [];
+	$rc = 1;
+	exec('/sbin/kldstat -q -n if_ovpn', $out, $rc);
+	return ($rc === 0);
+}
+
+/*
+ * Can this instance use DCO? Mirrors the restrictions of OpenVPN and the
+ * FreeBSD if_ovpn driver. If anything is unclear, answer "no" so the
+ * instance keeps working in classic tun mode.
+ */
+function openvpn_dco_usable(string $mode, array $settings) : bool {
+	if (!openvpn_dco_module_loaded()) {
+		 return false;
+	}
+	/* DCO only supports layer 3 (tun) */
+	if (array_get_path($settings, 'dev_mode', 'tun') !== 'tun') {
+		 return false;
+	}
+	/* DCO only supports UDP */
+	if (strncasecmp(array_get_path($settings, 'protocol', ''), 'UDP', 3) !== 0) {
+		 return false;
+	}
+	/* DCO requires a TLS-based mode, no static shared key */
+	if (array_get_path($settings, 'mode', '') === 'p2p_shared_key') {
+		 return false;
+	}
+	/* All data ciphers and the fallback cipher must be supported by DCO */
+	$dco_ciphers = ['AES-128-GCM', 'AES-256-GCM', 'CHACHA20-POLY1305'];
+	$ciphers = preg_split('/[,:]/', array_get_path($settings, 'data_ciphers', ''), -1, PREG_SPLIT_NO_EMPTY);
+	$ciphers[] = array_get_path($settings, 'data_ciphers_fallback', '');
+	foreach ($ciphers as $cipher) {
+		 if (!in_array(strtoupper(trim($cipher)), $dco_ciphers, true)) {
+			  return false;
+		 }
+	}
+	/* No compression */
+	if (array_get_path($settings, 'allow_compression', 'no') !== 'no') {
+		 return false;
+	}
+	/* /30 or smaller peer-to-peer tunnel networks are not DCO compatible */
+	$tunnel_network = array_get_path($settings, 'tunnel_network', '');
+	if (!empty($tunnel_network)) {
+		 list($ip, $cidr) = openvpn_gen_tunnel_network($tunnel_network);
+		 if (is_numeric($cidr) && ((int)$cidr >= 30)) {
+			  return false;
+		 }
+	}
+	/* Per-instance opt-out and options that disable DCO anyway */
+	if (preg_match('/(^|[;\s])(disable-dco|fragment|comp-lzo|compress)\b/',
+		 array_get_path($settings, 'custom_options', ''))) {
+		 return false;
+	}
+	return true;
+}
+
+/*
+ * Does the interface exist right now? Asks the kernel directly instead of
+ * does_interface_exist(), which may answer from a cached interface list
+ * and then still reports an interface that was just destroyed.
+ */
+function openvpn_dco_if_exists(string $ifname) : bool {
+	$out = [];
+	$rc = 1;
+	exec('/sbin/ifconfig ' . escapeshellarg($ifname) . ' >/dev/null 2>&1', $out, $rc);
+	return ($rc === 0);
+}
+
+/*
+ * Stop the OpenVPN instance and remove its interface, waiting until both
+ * are really gone. Without waiting, the exiting process can still hold or
+ * remove the interface while the new one is being created.
+ */
+function openvpn_dco_stop_and_destroy(string $ifname, string $pidfile) : void {
+	$pf = escapeshellarg($pidfile);
+	if (file_exists($pidfile)) {
+		 mwexec("/bin/pkill -TERM -F {$pf}");
+		 /* wait up to 10 s for the process to exit */
+		 for ($i = 0; $i < 100; $i++) {
+			  $out = [];
+			  $rc = 1;
+			  exec("/bin/pgrep -F {$pf}", $out, $rc);
+			  if ($rc !== 0) {
+					break;
+			  }
+			  usleep(100000);
+		 }
+		 if ($rc === 0) {
+			  mwexec("/bin/pkill -KILL -F {$pf}");
+			  usleep(500000);
+		 }
+	}
+	/* remove the interface and wait up to 5 s until it is gone */
+	for ($i = 0; $i < 50 && openvpn_dco_if_exists($ifname); $i++) {
+		 if (($i % 10) == 0) {
+			  exec("/sbin/ifconfig " . escapeshellarg($ifname) . " destroy >/dev/null 2>&1");
+		 }
+		 usleep(100000);
+	}
+	if (openvpn_dco_if_exists($ifname)) {
+		 syslog(LOG_WARNING, "OpenVPN {$ifname}: could not remove interface to switch DCO mode");
+	}
+	/* refresh pfSense's cached interface list */
+	does_interface_exist($ifname, true);
+}
+
+/* Kind of an existing interface: 'tun', 'tap', 'ovpn' or null if missing */
+function openvpn_if_kind(string $ifname) : ?string {
+	if (!openvpn_dco_if_exists($ifname)) {
+		 return null;
+	}
+	foreach (['tun', 'tap'] as $group) {
+		 $out = [];
+		 exec('/sbin/ifconfig -g ' . escapeshellarg($group) . ' 2>/dev/null', $out);
+		 if (in_array($ifname, array_map('trim', $out), true)) {
+			  return $group;
+		 }
+	}
+	return 'ovpn';
+}
+
 function openvpn_name(string $mode, array $settings, string $type = 'if') : string {
 	switch ($type) {
 		case 'if':
@@ -836,16 +964,47 @@
 	$tunname = openvpn_name($mode, $settings, 'dev');
 	$devname = openvpn_name($mode, $settings);
 
-	/* is our device already configured */
-	if (!does_interface_exist($devname)) {
+	/* DCO patch: decide between if_ovpn (DCO) and classic tun */
+	$use_dco = openvpn_dco_usable($mode, $settings);
+
+	/*
+	 * DCO patch: if the existing interface has the wrong kind (tun while
+	 * DCO is wanted, or if_ovpn while it is not), stop the instance and
+	 * remove the interface so it is recreated below. tap is never touched.
+	 */
+	$cur_kind = openvpn_if_kind($devname);
+	if (($use_dco && ($cur_kind === 'tun')) ||
+		 (!$use_dco && ($cur_kind === 'ovpn'))) {
+		 openvpn_dco_stop_and_destroy($devname,
+			  g_get('varrun_path') . "/openvpn_{$mode_id}.pid");
+	}
+
+	/* is our device already configured (DCO patch: uncached check) */
+	if (!openvpn_dco_if_exists($devname)) {
+
+		 $srcif = $tunname;
+		 if ($use_dco) {
+			  /* DCO patch: create an if_ovpn interface instead of tun */
+			  $out = [];
+			  $rc = 1;
+			  exec("/sbin/ifconfig ovpn create", $out, $rc);
+			  $srcif = trim($out[0] ?? '');
+			  if (($rc !== 0) || empty($srcif)) {
+					syslog(LOG_WARNING, "OpenVPN {$devname}: could not create if_ovpn interface, falling back to tun (no DCO)");
+					$use_dco = false;
+					$srcif = $tunname;
+			  }
+		 }
 
 		/* create the tap device if required */
-		if (!file_exists("/dev/{$tunname}")) {
+		if (!$use_dco && !file_exists("/dev/{$tunname}")) {
 			exec("/sbin/ifconfig " . escapeshellarg($tunname) . " create");
 		}
 
 		/* rename the device */
-		mwexec("/sbin/ifconfig " . escapeshellarg($tunname) . " name " . escapeshellarg($devname));
+		 mwexec("/sbin/ifconfig " . escapeshellarg($srcif) . " name " . escapeshellarg($devname));
+		 /* DCO patch: refresh pfSense's cached interface list */
+		 does_interface_exist($devname, true);
 
 		/* add the device to the openvpn group */
 		mwexec("/sbin/ifconfig " . escapeshellarg($devname) . " group openvpn");
@@ -898,7 +1057,16 @@
 	}
 
 	$conf .= "dev-type {$settings['dev_mode']}\n";
-	$conf .= "dev-node /dev/{$tunname}\n";
+	if ($use_dco) {
+		 /* DCO patch: OpenVPN takes over the existing if_ovpn interface */
+		 syslog(LOG_NOTICE, "OpenVPN {$devname}: using data channel offload (if_ovpn)");
+	} else {
+		 $conf .= "dev-node /dev/{$tunname}\n";
+		 /* DCO patch: never switch to DCO on its own on a tun interface */
+		 if (!preg_match('/(^|[;\s])disable-dco\b/', array_get_path($settings, 'custom_options', ''))) {
+			  $conf .= "disable-dco\n";
+		 }
+	}
 	$conf .= "writepid {$pfile}\n";
 	$conf .= "#user nobody\n";
 	$conf .= "#group nobody\n";
