summaryrefslogtreecommitdiff
blob: eea33db12d61bac44ad555ac9bd5b299f0459f78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
# Copyright (c) 2004-2005 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)
tunctl() {
	LC_ALL=C /usr/bin/tunctl "$@"
}

# char* tuntap_provides(void)
#
# Returns a string to change module definition for starting up
tuntap_provides() {
	echo "tuntap"
}

# void tuntap_depend(void)
#
# Sets up the dependancies for the module
tuntap_depend() {
	after interface
	before dhcp
}

# bool tuntap_check_installed(void)
#
# Returns 1 if tuntap is installed, otherwise 0
tuntap_check_installed() {
	[[ -x /usr/bin/tunctl ]] && return 0
	${1:-false} && eerror "For TunTap support, emerge sys-apps/usermode-utilities"
	return 1
}

# bool tuntap_check_depends(void)
#
# Checks to see if we have the needed functions
tuntap_check_depends() {
	local f

	for f in interface_exists interface_type; do
		[[ $( type -t "${f}" ) == "function" ]] && continue
		eerror "tuntap: missing required function ${f}\n"
		return 1
	done

	return 0
}

# 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
}

# char* tuntap_get_vars(char *interface)
#
# Returns a string spaced with possible user set
# configuration variables
tuntap_get_vars() {
	echo "tunctl_$1"
}

# bool tuntap_exists(char *interface)
#
# Returns 0 if the tun/tap interface exists, otherwise 1
tuntap_exists() {
	tunctl -d "$1" &>/dev/null
}

# bool tuntap_pre_start(char *iface)
#
# Create the device, give it the right perms
tuntap_pre_start() {
	local iface="$1" opts ifvar=$( bash_variable "$1" )
	local itype=$( interface_type "${iface}" )

	# 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

	# Get our options
	eval opts=\"\$\{tunctl_${ifvar}\}\"

	ebegin "Creating Tun/Tap interface ${iface}"
	tunctl ${opts} -t "${iface}" >/dev/null
	eend "$?" || return 1

	return 0
}

# bool tuntap_stop(char *iface)
#
# Removes the device
tuntap_stop() {
	local iface="$1"

	tuntap_check_installed || return 0
	interface_exists "${iface}" || return 0

	# tunctl doesn't always error on on tun/tap
	# interfaces (mainly aliases, etc)
	if tuntap_exists "${iface}" ; then
		interface_exists "${iface}" \
		|| einfo "Destroyed Tun/Tap interface ${iface}"
	fi

	return 0 
}

# vim:ts=4