tun/tap: merge and rename to tuntap

tun(4) and tap(4) share the same general management interface and have a lot
in common. Bugs exist in tap(4) that have been fixed in tun(4), and
vice-versa. Let's reduce the maintenance requirements by merging them
together and using flags to differentiate between the three interface types
(tun, tap, vmnet).

This fixes a couple of tap(4)/vmnet(4) issues right out of the gate:
- tap devices may no longer be destroyed while they're open [0]
- VIMAGE issues already addressed in tun by kp

[0] emaste had removed an easy-panic-button in r240938 due to devdrn
blocking. A naive glance over this leads me to believe that this isn't quite
complete -- destroy_devl will only block while executing d_* functions, but
doesn't block the device from being destroyed while a process has it open.
The latter is the intent of the condvar in tun, so this is "fixed" (for
certain definitions of the word -- it wasn't really broken in tap, it just
wasn't quite ideal).

ifconfig(8) also grew the ability to map an interface name to a kld, so
that `ifconfig {tun,tap}0` can continue to autoload the correct module, and
`ifconfig vmnet0 create` will now autoload the correct module. This is a
low overhead addition.

(MFC commentary)

This may get MFC'd if many bugs in tun(4)/tap(4) are discovered after this,
and how critical they are. Changes after this are likely easily MFC'd
without taking this merge, but the merge will be easier.

I have no plans to do this MFC as of now.

Reviewed by:	bcr (manpages), tuexen (testing, syzkaller/packetdrill)
Input also from:	melifaro
Relnotes:	yes
Differential Revision:	https://reviews.freebsd.org/D20044
This commit is contained in:
Kyle Evans 2019-05-08 02:32:11 +00:00
parent d044b69950
commit 251a32b5b2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=347241
35 changed files with 1847 additions and 2422 deletions

View file

@ -31,6 +31,14 @@ NOTE TO PEOPLE WHO THINK THAT FreeBSD 13.x IS SLOW:
disable the most expensive debugging functionality run
"ln -s 'abort:false,junk:false' /etc/malloc.conf".)
20190507:
The tap(4) driver has been folded into tun(4), and the module has been
renamed to tuntap. You should update any kld_load="if_tap" or
kld_load="if_tun" entries in /etc/rc.conf, if_tap_load="YES" or
if_tun_load="YES" entries in /boot/loader.conf to load the if_tuntap
module instead, and "device tap" or "device tun" entries in kernel
config files to select the tuntap device instead.
20190418:
The following knobs have been added related to tradeoffs between
safe use of the random device and availability in the absence of

View file

@ -130,6 +130,25 @@ struct ifa_order_elt {
TAILQ_HEAD(ifa_queue, ifa_order_elt);
static struct module_map_entry {
const char *ifname;
const char *kldname;
} module_map[] = {
{
.ifname = "tun",
.kldname = "if_tuntap",
},
{
.ifname = "tap",
.kldname = "if_tuntap",
},
{
.ifname = "vmnet",
.kldname = "if_tuntap",
},
};
void
opt_register(struct option *p)
{
@ -1413,9 +1432,10 @@ ifmaybeload(const char *name)
{
#define MOD_PREFIX_LEN 3 /* "if_" */
struct module_stat mstat;
int fileid, modid;
char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
int i, fileid, modid;
char ifname[IFNAMSIZ], *ifkind, *dp;
const char *cp;
struct module_map_entry *mme;
/* loading suppressed by the user */
if (noload)
@ -1429,9 +1449,26 @@ ifmaybeload(const char *name)
break;
}
/* turn interface and unit into module name */
strlcpy(ifkind, "if_", sizeof(ifkind));
strlcat(ifkind, ifname, sizeof(ifkind));
/* Either derive it from the map or guess otherwise */
ifkind = NULL;
for (i = 0; i < nitems(module_map); ++i) {
mme = &module_map[i];
if (strcmp(mme->ifname, ifname) == 0) {
ifkind = strdup(mme->kldname);
if (ifkind == NULL)
err(EXIT_FAILURE, "ifmaybeload");
break;
}
}
/* We didn't have an alias for it... we'll guess. */
if (ifkind == NULL) {
ifkind = malloc(IFNAMSIZ + MOD_PREFIX_LEN);
/* turn interface and unit into module name */
strlcpy(ifkind, "if_", sizeof(ifkind));
strlcat(ifkind, ifname, sizeof(ifkind));
}
/* scan files in kernel */
mstat.version = sizeof(struct module_stat);
@ -1450,7 +1487,7 @@ ifmaybeload(const char *name)
/* already loaded? */
if (strcmp(ifname, cp) == 0 ||
strcmp(ifkind, cp) == 0)
return;
goto out;
}
}
@ -1459,6 +1496,8 @@ ifmaybeload(const char *name)
* infer the names of all drivers (eg mlx4en(4)).
*/
(void) kldload(ifkind);
out:
free(ifkind);
}
static struct cmd basic_cmds[] = {

View file

@ -1,14 +1,14 @@
.\" $FreeBSD$
.\" Based on PR#2411
.\"
.Dd November 29, 2017
.Dd April 29, 2019
.Dt TAP 4
.Os
.Sh NAME
.Nm tap
.Nd Ethernet tunnel software network interface
.Sh SYNOPSIS
.Cd device tap
.Cd device tuntap
.Sh DESCRIPTION
The
.Nm
@ -51,7 +51,7 @@ The network interfaces are named
.Dq Li tap1 ,
etc., one for each control device that has been opened.
These Ethernet network interfaces persist until
.Pa if_tap.ko
.Pa if_tuntap.ko
module is unloaded, or until removed with "ifconfig destroy" (see below).
.Pp
.Nm
@ -96,7 +96,7 @@ It therefore defaults to being enabled until further notice.
.Ef
.Pp
Control devices (once successfully opened) persist until
.Pa if_tap.ko
.Pa if_tuntap.ko
is unloaded or the interface is destroyed.
.Pp
Each interface supports the usual Ethernet network interface
@ -296,27 +296,6 @@ device can also be used with the VMware port as a replacement
for the old
.Em VMnet
device driver.
The driver uses the minor number
to select between
.Nm
and
.Nm vmnet
devices.
.Em VMnet
minor numbers begin at
.Va 0x800000
+
.Va N ;
where
.Va N
is a
.Em VMnet
unit number.
In this case the control device is expected to be
.Pa /dev/vmnet Ns Sy N ,
and the network interface will be
.Sy vmnet Ns Ar N .
Additionally,
.Em VMnet
devices do not
.Xr ifconfig 8

View file

@ -2,14 +2,14 @@
.\" $FreeBSD$
.\" Based on PR#2411
.\"
.Dd November 29, 2017
.Dd April 29, 2019
.Dt TUN 4
.Os
.Sh NAME
.Nm tun
.Nd tunnel software network interface
.Sh SYNOPSIS
.Cd device tun
.Cd device tuntap
.Sh DESCRIPTION
The
.Nm
@ -52,7 +52,7 @@ The network interfaces are named
.Dq Li tun1 ,
etc., one for each control device that has been opened.
These network interfaces persist until the
.Pa if_tun.ko
.Pa if_tuntap.ko
module is unloaded, or until removed with the
.Xr ifconfig 8
command.
@ -99,7 +99,7 @@ It therefore defaults to being enabled until further notice.
.Ef
.Pp
Control devices (once successfully opened) persist until
.Pa if_tun.ko
.Pa if_tuntap.ko
is unloaded in the same way that network interfaces persist (see above).
.Pp
Each interface supports the usual network-interface

View file

@ -323,7 +323,7 @@ device padlock_rng # VIA Padlock RNG
device rdrand_rng # Intel Bull Mountain RNG
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device gif # IPv6 and IPv4 tunneling
device firmware # firmware assist module

View file

@ -136,7 +136,7 @@ device padlock_rng # VIA Padlock RNG
device rdrand_rng # Intel Bull Mountain RNG
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device gif # IPv6 and IPv4 tunneling
# The `bpf' device enables the Berkeley Packet Filter.

View file

@ -69,7 +69,7 @@ device loop # Network loopback
device md # Memory/malloc disk
device pty # BSD-style compatibility pseudo ttys
device random # Entropy device
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device ether # Required for all ethernet devices
device vlan # 802.1Q VLAN support
device wlan # 802.11 WLAN support

View file

@ -72,7 +72,7 @@ device loop # Network loopback
device md # Memory/malloc disk
device pty # BSD-style compatibility pseudo ttys
device random # Entropy device
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device ether # Required for all ethernet devices
device vlan # 802.1Q VLAN support
device wlan # 802.11 WLAN support

View file

@ -60,7 +60,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
#device vlan # 802.1Q VLAN support
#device tun # Packet tunnel.
#device tuntap # Packet tunnel.
#device md # Memory "disks"
#device gif # IPv6 and IPv4 tunneling
#device firmware # firmware assist module

View file

@ -47,7 +47,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
#device vlan # 802.1Q VLAN support
#device tun # Packet tunnel.
#device tuntap # Packet tunnel.
device md # Memory "disks"
#device gif # IPv6 and IPv4 tunneling
#device firmware # firmware assist module

View file

@ -51,7 +51,7 @@ device mpcore_timer
device loop # Network loopback
device random # Entropy device
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
#device gif # IPv6 and IPv4 tunneling
#device firmware # firmware assist module

View file

@ -45,7 +45,7 @@ device regulator
device loop # Network loopback
device random # Entropy device
device vlan # 802.1Q VLAN support
#device tun # Packet tunnel.
#device tuntap # Packet tunnel.
device md # Memory "disks"
#device gif # IPv6 and IPv4 tunneling
device firmware # firmware assist module

View file

@ -296,7 +296,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device gif # IPv6 and IPv4 tunneling
device firmware # firmware assist module

View file

@ -898,11 +898,9 @@ device epair
# which discards all packets sent and receives none.
device edsc
# The `tap' device is a pty-like virtual Ethernet interface
device tap
# The `tun' device implements (user-)ppp and nos-tun(8)
device tun
# The `tuntap' device implements (user-)ppp, nos-tun(8) and a pty-like virtual
# Ethernet interface
device tuntap
# The `gif' device implements IPv6 over IP4 tunneling,
# IPv4 over IPv6 tunneling, IPv4 over IPv4 tunneling and

View file

@ -4079,8 +4079,7 @@ net/if_mib.c standard
net/if_spppfr.c optional sppp | netgraph_sppp
net/if_spppsubr.c optional sppp | netgraph_sppp
net/if_stf.c optional stf inet inet6
net/if_tun.c optional tun
net/if_tap.c optional tap
net/if_tuntap.c optional tuntap
net/if_vlan.c optional vlan
net/if_vxlan.c optional vxlan inet | vxlan inet6
net/ifdi_if.m optional ether pci iflib

View file

@ -316,7 +316,7 @@ device padlock_rng # VIA Padlock RNG
device rdrand_rng # Intel Bull Mountain RNG
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device gif # IPv6 and IPv4 tunneling
device firmware # firmware assist module

View file

@ -137,7 +137,7 @@ device padlock_rng # VIA Padlock RNG
device rdrand_rng # Intel Bull Mountain RNG
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device gif # IPv6 and IPv4 tunneling
# The `bpf' device enables the Berkeley Packet Filter.

View file

@ -152,7 +152,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device gif # IPv6 and IPv4 tunneling
device firmware # firmware assist module

View file

@ -187,7 +187,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device gif # IPv6 and IPv4 tunneling
device firmware # firmware assist module

View file

@ -163,8 +163,7 @@ SUBDIR= \
if_lagg \
${_if_ndis} \
${_if_stf} \
if_tap \
if_tun \
if_tuntap \
if_vlan \
if_vxlan \
iflib \

View file

@ -1,10 +0,0 @@
# Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
#
# $FreeBSD$
.PATH: ${SRCTOP}/sys/net
KMOD= if_tap
SRCS= if_tap.c opt_inet.h vnode_if.h
.include <bsd.kmod.mk>

View file

@ -1,8 +0,0 @@
# $FreeBSD$
.PATH: ${SRCTOP}/sys/net
KMOD= if_tun
SRCS= if_tun.c opt_inet.h opt_inet6.h
.include <bsd.kmod.mk>

View file

@ -0,0 +1,35 @@
# $FreeBSD$
.PATH: ${SRCTOP}/sys/net
KMOD= if_tuntap
SRCS= if_tuntap.c opt_inet.h opt_inet6.h
# Symlink for backwards compatibility with systems installed at 12.0 or older
.if ${MACHINE_CPUARCH} != "powerpc"
SYMLINKS= ${KMOD}.ko ${KMODDIR}/if_tun.ko \
${KMOD}.ko ${KMODDIR}/if_tap.ko
.else
# Some PPC systems use msdosfs for /boot, which can't handle links or symlinks
afterinstall: alias alias_debug
alias: .PHONY
${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/if_tun.ko
${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/if_tap.ko
.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && \
"${MK_KERNEL_SYMBOLS}" != "no"
alias_debug: .PHONY
${INSTALL} -T debug -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
${_INSTALLFLAGS} ${PROG}.debug \
${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/if_tun.ko
${INSTALL} -T debug -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
${_INSTALLFLAGS} ${PROG}.debug \
${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/if_tap.ko
.else
alias_debug: .PHONY
.endif
.endif
.include <bsd.kmod.mk>

File diff suppressed because it is too large Load diff

View file

@ -40,23 +40,21 @@
#ifndef _NET_IF_TAP_H_
#define _NET_IF_TAP_H_
/* refer to if_tapvar.h for the softc stuff */
#include <net/if_tun.h>
/* maximum receive packet size (hard limit) */
#define TAPMRU 16384
struct tapinfo {
int baudrate; /* linespeed */
short mtu; /* maximum transmission unit */
u_char type; /* ethernet, tokenring, etc. */
u_char dummy; /* place holder */
};
#define tapinfo tuninfo
/* ioctl's for get/set debug */
#define TAPSDEBUG _IOW('t', 90, int)
#define TAPGDEBUG _IOR('t', 89, int)
#define TAPSIFINFO _IOW('t', 91, struct tapinfo)
#define TAPGIFINFO _IOR('t', 92, struct tapinfo)
/*
* ioctl's for get/set debug; these are aliases of TUN* ioctls, see net/if_tun.h
* for details.
*/
#define TAPSDEBUG TUNSDEBUG
#define TAPGDEBUG TUNGDEBUG
#define TAPSIFINFO TUNSIFINFO
#define TAPGIFINFO TUNGIFINFO
#define TAPGIFNAME _IOR('t', 93, struct ifreq)
/* VMware ioctl's */

View file

@ -1,71 +0,0 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* BASED ON:
* -------------------------------------------------------------------------
*
* Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
* All rights reserved.
*
* Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
* Nottingham University 1987.
*/
/*
* $FreeBSD$
* $Id: if_tapvar.h,v 0.6 2000/07/11 02:16:08 max Exp $
*/
#ifndef _NET_IF_TAPVAR_H_
#define _NET_IF_TAPVAR_H_
/*
* tap_mtx locks tap_flags, tap_pid. tap_next locked with global tapmtx.
* Other fields locked by owning subsystems.
*/
struct tap_softc {
struct ifnet *tap_ifp;
u_short tap_flags; /* misc flags */
#define TAP_OPEN (1 << 0)
#define TAP_INITED (1 << 1)
#define TAP_RWAIT (1 << 2)
#define TAP_ASYNC (1 << 3)
#define TAP_READY (TAP_OPEN|TAP_INITED)
#define TAP_VMNET (1 << 4)
u_int8_t ether_addr[ETHER_ADDR_LEN]; /* ether addr of the remote side */
pid_t tap_pid; /* PID of process to open */
struct sigio *tap_sigio; /* information for async I/O */
struct selinfo tap_rsel; /* read select */
SLIST_ENTRY(tap_softc) tap_next; /* next device in chain */
struct cdev *tap_dev;
struct mtx tap_mtx; /* per-softc mutex */
};
#endif /* !_NET_IF_TAPVAR_H_ */

File diff suppressed because it is too large Load diff

1716
sys/net/if_tuntap.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -167,7 +167,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device ofwd # Open Firmware disks
device gif # IPv6 and IPv4 tunneling

View file

@ -186,7 +186,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device ofwd # Open Firmware disks
device gif # IPv6 and IPv4 tunneling

View file

@ -103,7 +103,7 @@ device spibus
device spigen
device tsec
device dpaa
device tun
device tuntap
device uart
options USB_DEBUG # enable debug msgs
#device uhci

View file

@ -104,7 +104,7 @@ device spibus
device spigen
device tsec
device dpaa
device tun
device tuntap
device uart
options USB_DEBUG # enable debug msgs
#device uhci

View file

@ -107,7 +107,7 @@ device sdhci
device sec
device spibus
device spigen
device tun
device tuntap
device uart
options USB_DEBUG # enable debug msgs
#device uhci

View file

@ -135,7 +135,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device gif # IPv6 and IPv4 tunneling
device firmware # firmware assist module

View file

@ -236,7 +236,7 @@ device loop # Network loopback
device random # Entropy device
device ether # Ethernet support
device vlan # 802.1Q VLAN support
device tun # Packet tunnel.
device tuntap # Packet tunnel.
device md # Memory "disks"
device gif # IPv6 and IPv4 tunneling
device firmware # firmware assist module