]> git.ipfire.org Git - thirdparty/strongswan.git/blob - testing/hosts/dave/etc/runlevels/default/net.eth0
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / testing / hosts / dave / etc / runlevels / default / net.eth0
1 #!/sbin/runscript
2 # Copyright 1999-2004 Gentoo Technologies, Inc.
3 # Distributed under the terms of the GNU General Public License v2
4
5 #NB: Config is in /etc/conf.d/net
6
7 if [[ -n $NET_DEBUG ]]; then
8 set -x
9 devnull=/dev/stderr
10 else
11 devnull=/dev/null
12 fi
13
14 # For pcmcia users. note that pcmcia must be added to the same
15 # runlevel as the net.* script that needs it.
16 depend() {
17 use hotplug pcmcia
18 }
19
20 checkconfig() {
21 if [[ -z "${ifconfig_IFACE}" ]]; then
22 eerror "Please make sure that /etc/conf.d/net has \$ifconfig_$IFACE set"
23 eerror "(or \$iface_$IFACE for old-style configuration)"
24 return 1
25 fi
26 if [[ -n "${vlans_IFACE}" && ! -x /sbin/vconfig ]]; then
27 eerror "For VLAN (802.1q) support, emerge net-misc/vconfig"
28 return 1
29 fi
30 }
31
32 # Fix bug 50039 (init.d/net.eth0 localization)
33 # Some other commands in this script might need to be wrapped, but
34 # we'll get them one-by-one. Note that LC_ALL trumps LC_anything_else
35 # according to locale(7)
36 ifconfig() {
37 LC_ALL=C /sbin/ifconfig "$@"
38 }
39
40 # setup_vars: setup variables based on $1 and content of /etc/conf.d/net
41 # The following variables are set, which should be declared local by
42 # the calling routine.
43 # status_IFACE (up or '')
44 # vlans_IFACE (space-separated list)
45 # ifconfig_IFACE (array of ifconfig lines, replaces iface_IFACE)
46 # dhcpcd_IFACE (command-line args for dhcpcd)
47 # routes_IFACE (array of route lines)
48 # inet6_IFACE (array of inet6 lines)
49 # ifconfig_fallback_IFACE (fallback ifconfig if dhcp fails)
50 setup_vars() {
51 local i iface="${1//\./_}"
52
53 status_IFACE="$(ifconfig ${1} 2>${devnull} | gawk '$1 == "UP" {print "up"}')"
54 eval vlans_IFACE=\"\$\{iface_${iface}_vlans\}\"
55 eval ifconfig_IFACE=( \"\$\{ifconfig_$iface\[@\]\}\" )
56 eval dhcpcd_IFACE=\"\$\{dhcpcd_$iface\}\"
57 eval routes_IFACE=( \"\$\{routes_$iface\[@\]\}\" )
58 eval inet6_IFACE=( \"\$\{inet6_$iface\[@\]\}\" )
59 eval ifconfig_fallback_IFACE=( \"\$\{ifconfig_fallback_$iface\[@\]\}\" )
60
61 # BACKWARD COMPATIBILITY: populate the ifconfig_IFACE array
62 # if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0)
63 eval local iface_IFACE=\"\$\{iface_$iface\}\"
64 if [[ -n ${iface_IFACE} && -z ${ifconfig_IFACE} ]]; then
65 # Make sure these get evaluated as arrays
66 local -a aliases broadcasts netmasks
67
68 # Start with the primary interface
69 ifconfig_IFACE=( "${iface_IFACE}" )
70
71 # ..then add aliases
72 eval aliases=( \$\{alias_$iface\} )
73 eval broadcasts=( \$\{broadcast_$iface\} )
74 eval netmasks=( \$\{netmask_$iface\} )
75 for ((i = 0; i < ${#aliases[@]}; i = i + 1)); do
76 ifconfig_IFACE[i+1]="${aliases[i]} ${broadcasts[i]:+broadcast ${broadcasts[i]}} ${netmasks[i]:+netmask ${netmasks[i]}}"
77 done
78 fi
79
80 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
81 if [[ ${#inet6_IFACE[@]} == 1 && ${inet6_IFACE} == *' '* ]]; then
82 inet6_IFACE=( ${inet6_IFACE} )
83 fi
84 }
85
86 iface_start() {
87 local IFACE=${1} i x retval
88 checkconfig || return 1
89
90 if [[ ${ifconfig_IFACE} != dhcp ]]; then
91 # Show the address, but catch if this interface will be inet6 only
92 i=${ifconfig_IFACE%% *}
93 if [[ ${i} == *.*.*.* ]]; then
94 ebegin "Bringing ${IFACE} up (${i})"
95 else
96 ebegin "Bringing ${IFACE} up"
97 fi
98 # ifconfig does not always return failure ..
99 ifconfig ${IFACE} ${ifconfig_IFACE} >${devnull} && \
100 ifconfig ${IFACE} up &>${devnull}
101 eend $? || return $?
102 else
103 # Check that eth0 was not brought up by the kernel ...
104 if [[ ${status_IFACE} == up ]]; then
105 einfo "Keeping kernel configuration for ${IFACE}"
106 else
107 ebegin "Bringing ${IFACE} up via DHCP"
108 /sbin/dhcpcd ${dhcpcd_IFACE} ${IFACE}
109 retval=$?
110 eend $retval
111 if [[ $retval == 0 ]]; then
112 # DHCP succeeded, show address retrieved
113 i=$(ifconfig ${IFACE} | grep -m1 -o 'inet addr:[^ ]*' |
114 cut -d: -f2)
115 [[ -n ${i} ]] && einfo " ${IFACE} received address ${i}"
116 elif [[ -n "${ifconfig_fallback_IFACE}" ]]; then
117 # DHCP failed, try fallback.
118 # Show the address, but catch if this interface will be inet6 only
119 i=${ifconfig_fallback_IFACE%% *}
120 if [[ ${i} == *.*.*.* ]]; then
121 ebegin "Using fallback configuration (${i}) for ${IFACE}"
122 else
123 ebegin "Using fallback configuration for ${IFACE}"
124 fi
125 ifconfig ${IFACE} ${ifconfig_fallback_IFACE} >${devnull} && \
126 ifconfig ${IFACE} up &>${devnull}
127 eend $? || return $?
128 else
129 return $retval
130 fi
131 fi
132 fi
133
134 if [[ ${#ifconfig_IFACE[@]} -gt 1 ]]; then
135 einfo " Adding aliases"
136 for ((i = 1; i < ${#ifconfig_IFACE[@]}; i = i + 1)); do
137 ebegin " ${IFACE}:${i} (${ifconfig_IFACE[i]%% *})"
138 ifconfig ${IFACE}:${i} ${ifconfig_IFACE[i]}
139 eend $?
140 done
141 fi
142
143 if [[ -n ${inet6_IFACE} ]]; then
144 einfo " Adding inet6 addresses"
145 for ((i = 0; i < ${#inet6_IFACE[@]}; i = i + 1)); do
146 ebegin " ${IFACE} inet6 add ${inet6_IFACE[i]}"
147 ifconfig ${IFACE} inet6 add ${inet6_IFACE[i]} >${devnull}
148 eend $?
149 done
150 fi
151
152 # Set static routes
153 if [[ -n ${routes_IFACE} ]]; then
154 einfo " Adding routes"
155 for ((i = 0; i < ${#routes_IFACE[@]}; i = i + 1)); do
156 ebegin " ${routes_IFACE[i]}"
157 /sbin/route add ${routes_IFACE[i]}
158 eend $?
159 done
160 fi
161
162 # Set default route if applicable to this interface
163 if [[ ${gateway} == ${IFACE}/* ]]; then
164 local ogw=$(/bin/netstat -rn | awk '$1 == "0.0.0.0" {print $2}')
165 local gw=${gateway#*/}
166 if [[ ${ogw} != ${gw} ]]; then
167 ebegin " Setting default gateway ($gw)"
168
169 # First delete any existing route if it was setup by kernel...
170 /sbin/route del default dev ${IFACE} &>${devnull}
171
172 # Second delete old gateway if it was set...
173 /sbin/route del default gw ${ogw} &>${devnull}
174
175 # Third add our new default gateway
176 /sbin/route add default gw ${gw} >${devnull}
177 eend $? || {
178 true # need to have some command in here
179 # Note: This originally called stop, which is obviously
180 # wrong since it's calling with a local version of IFACE.
181 # The below code works correctly to abort configuration of
182 # the interface, but is commented because we're assuming
183 # that default route failure should not cause the interface
184 # to be unconfigured.
185 #local error=$?
186 #ewarn "Aborting configuration of ${IFACE}"
187 #iface_stop ${IFACE}
188 #return ${error}
189 }
190 fi
191 fi
192
193 # Enabling rp_filter causes wacky packets to be auto-dropped by
194 # the kernel. Note that we only do this if it is not set via
195 # /etc/sysctl.conf ...
196 if [[ -e /proc/sys/net/ipv4/conf/${IFACE}/rp_filter && \
197 -z "$(grep -s '^[^#]*rp_filter' /etc/sysctl.conf)" ]]; then
198 echo -n 1 > /proc/sys/net/ipv4/conf/${IFACE}/rp_filter
199 fi
200 }
201
202 # iface_stop: bring down an interface. Don't trust information in
203 # /etc/conf.d/net since the configuration might have changed since
204 # iface_start ran. Instead query for current configuration and bring
205 # down the interface.
206 iface_stop() {
207 local IFACE=${1} i x aliases inet6 count
208
209 # Try to do a simple down (no aliases, no inet6, no dhcp)
210 aliases="$(ifconfig | grep -o "^$IFACE:[0-9]*" | tac)"
211 inet6="$(ifconfig ${IFACE} | awk '$1 == "inet6" {print $2}')"
212 if [[ -z ${aliases} && -z ${inet6} && ! -e /var/run/dhcpcd-${IFACE}.pid ]]; then
213 ebegin "Bringing ${IFACE} down"
214 ifconfig ${IFACE} down &>/dev/null
215 eend 0
216 return 0
217 fi
218
219 einfo "Bringing ${IFACE} down"
220
221 # Stop aliases before primary interface.
222 # Note this must be done in reverse order, since ifconfig eth0:1
223 # will remove eth0:2, etc. It might be sufficient to simply remove
224 # the base interface but we're being safe here.
225 for i in ${aliases} ${IFACE}; do
226
227 # Delete all the inet6 addresses for this interface
228 inet6="$(ifconfig ${i} | awk '$1 == "inet6" {print $3}')"
229 if [[ -n ${inet6} ]]; then
230 einfo " Removing inet6 addresses"
231 for x in ${inet6}; do
232 ebegin " ${IFACE} inet6 del ${x}"
233 ifconfig ${i} inet6 del ${x}
234 eend $?
235 done
236 fi
237
238 # Stop DHCP (should be N/A for aliases)
239 # Don't trust current configuration... investigate ourselves
240 if /sbin/dhcpcd -z ${i} &>${devnull}; then
241 ebegin " Releasing DHCP lease for ${IFACE}"
242 for ((count = 0; count < 9; count = count + 1)); do
243 /sbin/dhcpcd -z ${i} &>${devnull} || break
244 sleep 1
245 done
246 [[ ${count} -lt 9 ]]
247 eend $? "Timed out"
248 fi
249 ebegin " Stopping ${i}"
250 ifconfig ${i} down &>${devnull}
251 eend 0
252 done
253
254 return 0
255 }
256
257 start() {
258 # These variables are set by setup_vars
259 local status_IFACE vlans_IFACE dhcpcd_IFACE
260 local -a ifconfig_IFACE routes_IFACE inet6_IFACE
261
262 # Call user-defined preup function if it exists
263 if [[ $(type -t preup) == function ]]; then
264 einfo "Running preup function"
265 preup ${IFACE} || {
266 eerror "preup ${IFACE} failed"
267 return 1
268 }
269 fi
270
271 # Start the primary interface and aliases
272 setup_vars ${IFACE}
273 iface_start ${IFACE} || return 1
274
275 # Start vlans
276 local vlan
277 for vlan in ${vlans_IFACE}; do
278 /sbin/vconfig add ${IFACE} ${vlan} >${devnull}
279 setup_vars ${IFACE}.${vlan}
280 iface_start ${IFACE}.${vlan}
281 done
282
283 # Call user-defined postup function if it exists
284 if [[ $(type -t postup) == function ]]; then
285 einfo "Running postup function"
286 postup ${IFACE}
287 fi
288 }
289
290 stop() {
291 # Call user-defined predown function if it exists
292 if [[ $(type -t predown) == function ]]; then
293 einfo "Running predown function"
294 predown ${IFACE}
295 fi
296
297 # Don't depend on setup_vars since configuration might have changed.
298 # Investigate current configuration instead.
299 local vlan
300 for vlan in $(ifconfig | grep -o "^${IFACE}\.[^ ]*"); do
301 iface_stop ${vlan}
302 /sbin/vconfig rem ${vlan} >${devnull}
303 done
304
305 iface_stop ${IFACE} || return 1 # always succeeds, btw
306
307 # Call user-defined postdown function if it exists
308 if [[ $(type -t postdown) == function ]]; then
309 einfo "Running postdown function"
310 postdown ${IFACE}
311 fi
312 }
313
314 # vim:ts=4