diff options
Diffstat (limited to 'net-scripts/net.modules.d/tuntap.sh')
-rw-r--r-- | net-scripts/net.modules.d/tuntap.sh | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/net-scripts/net.modules.d/tuntap.sh b/net-scripts/net.modules.d/tuntap.sh new file mode 100644 index 0000000..e0d1865 --- /dev/null +++ b/net-scripts/net.modules.d/tuntap.sh @@ -0,0 +1,103 @@ +# Copyright (c) 2004-2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# Contributed by Roy Marples (uberlord@gentoo.org) + +# Fix any potential localisation problems +# Note that LC_ALL trumps LC_anything_else according to locale(7) +openvpn() { + LC_ALL=C /usr/sbin/openvpn "$@" +} +tunctl() { + LC_ALL=C /usr/bin/tunctl "$@" +} + +# void tuntap_depend(void) +# +# Sets up the dependancies for the module +tuntap_depend() { + before interface macchanger + functions interface_exists interface_type +} + +# void tuntap_expose(void) +# +# Expose variables that can be configured +tuntap_expose() { + variables tunctl +} + +# bool tuntap_check_installed(void) +# +# Returns 1 if tuntap is installed, otherwise 0 +tuntap_check_installed() { + [[ -x /usr/sbin/openvpn ]] && return 0 + [[ -x /usr/bin/tunctl ]] && return 0 + ${1:-false} && eerror "For TunTap support, emerge net-misc/openvpn or sys-apps/usermode-utilities" + return 1 +} + +# bool tuntap_check_kernel(void) +# +# Checks to see if the tun is present - if not try and load it +# Returns 1 if there is a problem +tuntap_check_kernel() { + [[ -a /dev/net/tun ]] && return 0 + /sbin/modprobe tun && sleep 1 + [[ -a /dev/net/tun ]] && return 0 + eerror "TUN/TAP support is not present in this kernel" + return 1 +} + +# bool tuntap_exists(char *interface) +# +# Returns 0 if the tun/tap interface exists, otherwise 1 +tuntap_exists() { + local itype="$(interface_type "$1")" + [[ ${itype} != "tun" && ${itype} != "tap" ]] && return 1 + interface_exists "$1" +} + +# bool tuntap_pre_start(char *iface) +# +# Create the device, give it the right perms +tuntap_pre_start() { + local iface="$1" itype="$(interface_type "$1")" + + # Check that we are a valid tun/tap interface + # NOTE - the name can be anything as we define it + # but for simplicity in the config we require either + # tun or tap + [[ ${itype} != "tun" && ${itype} != "tap" ]] && return 0 + + tuntap_check_kernel || return 1 + + ebegin "Creating Tun/Tap interface ${iface}" + if [[ -x /usr/sbin/openvpn ]] ; then + openvpn --mktun --dev "${iface}" >/dev/null + else + local ifvar="$(bash_variable "${iface}")" + local opts="tunctl_${ifvar}" + tunctl ${!opts} -t "${iface}" >/dev/null + fi + eend $? +} + +# bool tuntap_stop(char *iface) +# +# Removes the device +tuntap_stop() { + local iface="$1" + + tuntap_check_installed || return 0 + tuntap_exists "${iface}" || return 0 + + ebegin "Destroying Tun/Tap interface ${iface}" + if [[ -x /usr/sbin/openvpn ]] ; then + openvpn --rmtun --dev "${iface}" >/dev/null + else + tunctl -d "${fiace}" >/dev/null + fi + eend $? +} + +# vim: set ft=sh ts=4 : |