/
netif.tmpl
executable file
·261 lines (236 loc) · 6.46 KB
/
netif.tmpl
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/sbin/runscript
# Copyright (c) 2007-2010 Funtoo Technologies
# All rights reserved. Released under the 2-clause BSD license.
# TODO - generate cached config for /etc/init.d/foo, use them for starting and stopping
# rather than running "live" commands. This way "stop" will work correctly at all times.
# next line: INT = deprecated, support it for backwards compat:
INT=${RC_SVCNAME#netif.}
interface=${interface:-$INT}
[ "$interface" = "tmpl" ] && exit 0
if [ -z "$template" ]; then
eerror "Please specify a template (template=) for $RC_SVCNAME"
exit 1
fi
CYAN="[36;01m"
OFF="[0m"
DESC2="${template}"
[ -n "$description" ] && DESC2="${description}"
STARTD="Network $DESC2 $CYAN$interface$OFF up"
STOPD="Network $DESC2 $CYAN$interface$OFF down"
require() {
local missing=""
for envname in $*; do
[ -z "${!envname}" ] && missing="$missing $envname"
done
if [ -n "$missing" ]; then
echo
eerror "ERROR: Required configuration variable(s) are missing:"
eerror ""
for miss in $missing; do
eerror " ${CYAN}$miss${OFF}"
done
eerror ""
eerror "Please correct your configuration to address this issue."
echo
exit 1
fi
}
die() { eend 1 "$*"; exit 1; }
netif_depend() { return; }
netif_create() { return; }
netif_pre_up() { return; }
netif_post_up() { return; }
netif_pre_down() { return; }
netif_post_down() { return; }
netif_destroy() { return; }
try() {
$*
if [ $? -ne 0 ]; then
echo "Command failure: $*"
exit 1
fi
}
debug() {
$*
if [ $? -ne 0 ]; then
echo "Command failure: $*"
fi
}
depend() {
need localmount
[ "$vlanmode" != "off" ] && need "netif.${trunk}"
[ -n "$slaves" ] && need $slaves
netif_depend
}
start() {
ebegin "$STARTD"
netif_create
if [ "$macaddr" != "" ]; then
/sbin/nameif ${interface} ${macaddr} || die "Couldn't rename interface"
fi
if [ ! -d /proc/sys/net/ipv6 ]; then
# attempt to silently load ipv6 module
modprobe -q ipv6
fi
if [ "$vlanmode" != "off" ]; then
if [ ! -d /proc/net/vlan ]; then
modprobe -q 8021q
if [ ! -d /proc/net/vlan ]; then
eerror "VLAN for interface $interface could not be configured:"
die " \n802.1q support is not present in this kernel."
fi
fi
require trunk vlan
ip link add dev ${interface} link ${trunk} type vlan id ${vlan} || die "Couldn't configure VLAN ${vlan} on ${trunk}"
fi
ip addr flush dev $interface > /dev/null 2>&1
netif_pre_up
debug ip link set $interface up
if [ -n "$mtu" ]; then
ip link set $interface mtu $mtu
if [ -n "$slaves" ]; then
for slave in ${slaves//netif./}; do
ip link set $slave mtu $mtu || eend $?
done
fi
fi
ezroute 4 add; ezroute 6 add; ezresolv add
if [ "$multicast" = "yes" ]
then
ebegin "Adding multicast route for $interface"
route add -net 224.0.0.0 netmask 240.0.0.0 dev $interface
eend $?
fi
netif_post_up
eend $?
}
stop() {
ebegin "$STOPD"
netif_pre_down
ezresolv del; ezroute 6 del; ezroute 4 del
ip link set $interface down
ip addr flush dev $interface
netif_post_down
# VLANS:
[ "${vlanmode}" != "off" ] && ip link delete ${interface}
netif_destroy
eend 0
}
# DOM, IP, NM, SLAVES, MTU, NS1, NS2, GW are deprecated - this is backwards compat. code:
domain=${domain:-$DOM}
if [ -n "$IP" ]; then
ipaddr=$IP/$NM
fi
netmask=${netmask:-$NM}
slaves=${slaves:-$SLAVES}
mtu=${mtu:-$MTU}
nameservers="${nameservers:-$NS1 $NS2}"
gateway=${gateway:-$GW}
# COMPAT END
get_ipnm_part() {
echo "${1%%;*}"
}
# we encode the broadcast like this: 1.2.3.4/24;broadcast=1.2.3.255;foo=bar;x;y;z
# we call the following function like this:
# 'get_option "$ipnm" broadcast +'
# it will return "1.2.3.255" if defined, and a '+' if not defined.
# 'get_option "1.2.3.4;a;b;c" a false' will return 'true' if "a" is defined as a keyword, and "false" (default value)
# otherwise.
if_ipv4() {
echo "${1%%/*}" | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}'
}
get_option() {
local full="$1"
local ipnm="$(get_ipnm_part $full)"
local oldparts="${full:${#ipnm}}"
local trim
while true; do
[ "${oldparts:$((${#oldparts}-1))}" = ";" ] && oldparts="${oldparts:0:$((${#oldparts}-1))}" && continue
count=$(($count + 1))
thispart="${oldparts##*;}"
varname="${thispart%%=*}"
if [ "$varname" = "$thispart" ] && [ "$thispart" = "$2" ]; then
echo "true"
return
fi
if [ "$varname" != "$thispart" ] && [ "$varname" = "$2" ]; then
echo "${thispart#*=}"
return
fi
[ -z "$thispart" ] && break
trim=$(( ${#oldparts} - ${#thispart} ))
oldparts="${oldparts:0:${trim}}"
[ -z "${oldparts}" ] && break
done
# return default (if any)
echo "$3"
}
start_pre() {
if [ -n "$ipaddr $ipaddrs" ]; then
for i in $ipaddr $ipaddrs; do
if [ "${i##*/}" = "$i" ]; then
echo
ewarn "You probably want to add a netmask to the ipaddr $i defined in your configuration."
ewarn "Example: ipaddr=\"192.168.0.1/24\""
echo
fi
done
fi
}
ezdns() {
# This function generates a resolv.conf file, which ezresolv() passes to resolvconf
[ -n "$domain" ] && echo "domain $domain"
[ -n "$search" ] && echo "search $search"
for ns in $nameservers; do
echo "nameserver $ns"
done
}
ezresolv() {
# This function calls resolvconf (openresolv) with the correct resolv.conf passed as a here-file
if [ "$1" = "add" ]; then
[ -z "`ezdns`" ] && return
resolvconf -a $interface << EOF || die "Problem adding DNS info for $interface"
`ezdns`
EOF
else
resolvconf -d $interface > /dev/null 2>&1
fi
}
ezroute() {
# This function processes a semi-colon delimited list of routes set in $route, and also
# supports the $gateway variable for setting a default IP route.
local rest
if [ "$1" = "4" ]; then
rest="$route"
[ -n "$gateway" ] && rest="$rest; default via $gateway dev $interface"
elif [ "$1" = "6" ]; then
rest="$route6"
[ -n "$gateway6" ] && rest="$rest; default via $gateway6 dev $interface"
fi
if [ -n "$rest" ]; then
# must end in a semi-colon before we start processing:
rest="$rest;"
while [ "$rest" != "" ]
do
# first = current route command; rest = remainder
first="${rest%%;*}"
rest="${rest#*;}"
# trim any trailing or leading spaces:
first=${first%% }; first=${first## }
[ "$first" != "" ] && ip -$1 route $2 $first
[ "$2" = "add" ] && [ $? -ne 0 ] && die "Couldn't set route: $first"
done
fi
}
if [ -z "$trunk" ]; then
# auto-detect trunk/vlan ID from interface name
vlanmode="auto"
vlan="${interface##*.}"
trunk="${interface%.*}"
[ "$vlan" = "$interface" ] && vlanmode="off"
else
# trunk/vlan ID specified by user in config
vlanmode="custom"
require vlan
fi
. /etc/netif.d/$template