aboutsummaryrefslogtreecommitdiff
blob: e18906ca93f1d35160e4dc8b957501d05983c178 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright (c) 2018 
# Released under the 2-clause BSD license.
# shellcheck shell=sh disable=SC1008

veth_depend()
{
	program ip
}

_config_vars="$_config_vars veth"


# We need it because _exists() function seeks in /sys/class/net
_netns_exists()
{
	[ -e "/var/run/netns/$1" ]
}


#Creates the network namespace if it doesn't exist. If called with no arguments, does nothing
#Arguments:
# $1 - name of the namespace
_create_ns() {

	vethrc=0
	for _ns in "$@"; do
		if [ -z "$_ns" ]; then
			continue
		fi
		if ! _netns_exists "$_ns"; then
			ip netns add "$_ns" > /dev/null 2>&1
			vethrc=$(($?+ vethrc)) 
		fi	
	done
	return $vethrc
}

#Brings a virtual interface up and takes network namespaces into account
#Arguments: 
# $1 - name of the interface, required!
# $2 - namespace
_bring_peer_up()
{
	if [ ! -z "$2" ]; then
		ip link set "$1" netns "$2" > /dev/null 2>&1
		vethrc=$?
		ip netns exec "$2" ip link set dev "$1" up > /dev/null 2>&1
		vethrc=$(($?+ vethrc)) 
		return $vethrc
	fi

	ip link set dev "$1" up > /dev/null 2>&1
	return $?
} 

#Brings a virtual interface down and takes network namespaces into account
#Arguments: 
# $1 - name of the interface, required!
# $2 - namespace
_bring_peer_down()
{

	if [ ! -z "$2" ]; then
		ip netns exec "$2" ip link del dev "$1" > /dev/null 2>&1
		return $? 
	fi

	ip link del dev "$1" > /dev/null 2>&1
	return $?
}


#Create and bring the veth pair up
_create_peers()
{
	local peer1
	peer1="$(_get_array "veth_${IFVAR}_peer1")"

	local peer2
	peer2="$(_get_array "veth_${IFVAR}_peer2")"

	for x in $peer1 $peer2; do
		if _exists "$x" ; then
			eerror "Interface $x already exists. Can't continue"
			return 1
		fi		
	done

	local netns1
	netns1="$(_get_array "veth_${IFVAR}_ns1")"
	local netns2
	netns2="$(_get_array "veth_${IFVAR}_ns2")"

	local vethrc

	if ! _create_ns "$netns1" "$netns2"
	then
		eerror "Can't create namespaces: $netns1 $netns2"
		return 1
	fi

	ip link add "$peer1" type veth peer name "$peer2" > /dev/null 2>&1 || {
		eerror "Can't create veth peer $peer1 or $peer2"
		return 1
	}


	if ! _bring_peer_up "$peer1" "$netns1"
	then
		eerror "Can't bring the veth peer $peer1 up"
		return 1

	fi
	if ! _bring_peer_up "$peer2" "$netns2"
	then
		eerror "Can't bring the veth peer $peer2 up"
		return 1

	fi

	return 0
}

# Create peers and namespaces
veth_pre_start()
{
	local itype
	eval itype=\$type_${IFVAR}
	if [ "$itype" != "veth" ]; then
		return 0
	fi

	local createveth
	eval createveth=\$veth_${IFVAR}_create
	if [ "$createveth" = "no" ]; then
		return 0
	fi

	type ip >/dev/null 2>&1 || {
		eerror "iproute2 nor found, please install iproute2"
		return 1
	}

	if ! _create_peers
	then
		return 1
	fi


	return 0
}

#Delete the veth pair
#We don't delete namespaces because someone may use them for some purposes
veth_post_stop()
{
	local itype
	eval itype=\$type_${IFVAR}
	if [ "$itype" != "veth" ]; then
		return 0
	fi

	local createveth
	eval createveth=\$veth_${IFVAR}_create
	if [ "$createveth" == "no" ]; then
		return 0
	fi

	local peer1
	peer1="$(_get_array "veth_${IFVAR}_peer1")"

	local netns1
	netns1="$(_get_array "veth_${IFVAR}_ns1")"

	if ! _bring_peer_down "$peer1" "$netns1"
	then
		eerror "Can't delete the veth pair ${IFVAR}"
		eend 1
	fi
	return 0
}