/[vps]/baselayout-vserver/trunk/lib/rcscripts/net.modules.d/iproute2
Gentoo

Contents of /baselayout-vserver/trunk/lib/rcscripts/net.modules.d/iproute2

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sat Sep 3 16:10:27 2005 UTC (8 years, 10 months ago) by hollow
File size: 9921 byte(s)
import initial baselayout sources (1.12.0_pre8)
1 #!/bin/bash
2 # Copyright (c) 2004-2005 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6
7 # Fix any potential localisation problems
8 # Note that LC_ALL trumps LC_anything_else according to locale(7)
9 ip() {
10 LC_ALL=C /sbin/ip "$@"
11 }
12
13 iproute2_tunnel() {
14 LC_ALL=C /sbin/ip tunnel "$@"
15 }
16
17 # void iproute2_depend(void)
18 #
19 # Sets up the dependancies for the module
20 iproute2_depend() {
21 after macnet wireless
22 }
23
24 # bool iproute2_check_installed(void)
25 #
26 # Returns 1 if iproute2 is installed, otherwise 0
27 iproute2_check_installed() {
28 local report="${1:-false}" installed="0"
29 if [[ ! -x /sbin/ip ]]; then
30 installed="1"
31 ${report} && eerror "For iproute2 support, emerge sys-apps/iproute2"
32 fi
33 if [[ ! -e /proc/net/netlink ]]; then
34 installed="1"
35 ${report} && eerror "iproute2 requires NetLink enabled in the kernel"
36 fi
37 return "${installed}"
38 }
39
40 # char* iproute2_provides(void)
41 #
42 # Returns a string to change module definition for starting up
43 iproute2_provides() {
44 echo "interface"
45 }
46
47 # char* iproute2_module(void)
48 #
49 # Returns the module name
50 # This is needed by dhclient as we run different scripts
51 # based on the interface
52 iproute2_module() {
53 echo "iproute2"
54 }
55
56 # bool iproute2_check_depends(void)
57 #
58 # Checks to see if we have the needed functions
59 iproute2_check_depends() {
60 local f
61
62 for f in interface_device; do
63 [[ $( type -t "${f}" ) == "function" ]] && continue
64 eerror "iproute2: missing required function ${f}\n"
65 return 1
66 done
67
68 return 0
69 }
70
71 # bool iproute2_exists(char *interface, bool report)
72 #
73 # Returns 1 if the interface exists, otherwise 0
74 iproute2_exists() {
75 local e=$( ip addr show label "$1" ) report="${2:-false}"
76 [[ -n ${e} ]] && return 0
77
78 ${report} && eerror "$1 does not exist"
79 return 1
80 }
81
82 # void iproute2_up(char *interface)
83 #
84 # provides a generic interface for bringing interfaces up
85 iproute2_up() {
86 ip link set up dev "$1"
87 }
88
89 # void iproute2_down(char *interface)
90 #
91 # provides a generic interface for bringing interfaces up
92 iproute2_down() {
93 ip link set down dev "$1"
94 }
95
96 # bool ifproute2_is_up(char *iface, bool withaddress)
97 #
98 # Returns 0 if the interface is up, otherwise 1
99 # If withaddress is true then the interface has to have an IPv4 address
100 # assigned as well
101 iproute2_is_up() {
102 local check="\<UP\>" addr="${2:-false}"
103 ${addr} && check="${check}.*inet "
104 ip addr show "$1" | grep -Eq "${check}" && return 0
105 return 1
106 }
107
108 # void iproute2_set_flag(char *iface, char *flag, bool enabled)
109 #
110 # Sets or disables the interface flag
111 iproute2_set_flag() {
112 local enable="$3" opt="on"
113 ${enable} || opt="off"
114 ip link set "$1" "$2" "${opt}"
115 }
116
117 # void iproute2_get_address(char *interface)
118 #
119 # Fetch the address retrieved by DHCP. If successful, echoes the
120 # address on stdout, otherwise echoes nothing.
121 iproute2_get_address() {
122 ip -family inet addr show "$1" \
123 | sed -n -e 's/.*inet \([^ ]*\).*/\1/p'
124 }
125
126 # void iproute2_get_mac_address(char *interface)
127 #
128 # Fetch the mac address assingned to the network card
129 iproute2_get_mac_address() {
130 ip link show "$1" \
131 | sed -n -e 's/^.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p'
132 }
133
134 # void iproute2_set_mac_address(char *interface, char *mac)
135 #
136 # Assigned the mac address to the network card
137 iproute2_set_mac_address() {
138 ip link set address "$2" dev "$1"
139 }
140
141 # int iproute2_set_name(char *interface, char *new_name)
142 #
143 # Renames the interface
144 # This will not work if the interface is setup!
145 iproute2_set_name() {
146 ip link set name "$2" dev "$1"
147 }
148
149 # void iproute2_get_aliases_rev(char *interface)
150 #
151 # Fetch the list of aliases for an interface.
152 # Outputs a space-separated list on stdout, in reverse order, for
153 # example "eth0:2 eth0:1"
154 iproute2_get_aliases_rev() {
155 local iface=$( interface_device "$1" )
156 ip addr show dev "${iface}" | grep -o "${iface}:[0-9].*" | tac
157 }
158
159 # bool iproute2_del_addresses(char *interface, bool onlyinet)
160 #
161 # Remove addresses from interface.
162 # If onlyinet is true, then we only remove IPv4 / inet addresses.
163 iproute2_del_addresses() {
164 local pre=""
165 ${2:-false} && pre="-f inet"
166 ip ${pre} addr flush label "$1" scope global &>/dev/null
167 ip ${pre} addr flush label "$1" scope host &>/dev/null
168 return 0
169 }
170
171 # char* iproute2_get_vars(char *interface)
172 #
173 # Returns a string spaced with possible user set
174 # configuration variables
175 iproute2_get_vars() {
176 echo "config_$1 routes_$1 fallback_$1 metric_$1 ipaddr_$1 ipaddr_fallback_$1 iproute_$1 inet6_$1"
177 }
178
179 # bool iproute2_get_old_config(char *iface)
180 #
181 # Returns config and config_fallback for the given interface
182 iproute2_get_old_config() {
183 local ifvar=$( bash_variable "$1" ) inet6
184
185 # iproute2-style config vars
186 eval config=( \"\$\{ipaddr_${ifvar}\[@\]\}\" )
187 eval config_fallback=( \"\$\{ipaddr_fallback_${ifvar}\[@\]\}\" )
188 eval inet6=( \"\$\{inet6_${ifvar}\[@\]\}\" )
189
190 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
191 [[ ${#inet6[@]} == "1" && ${inet6} == *" "* ]] && inet6=( ${inet6} )
192
193 # Add inet6 addresses to our config if required
194 [[ -n ${inet6} ]] && config=( "${config[@]}" "${inet6[@]}" )
195
196 # Support old style iface_xxx syntax
197 [[ -z ${config} && $(type -t ifconfig_get_old_config) == "function" ]] \
198 && ifconfig_get_old_config "${iface}"
199
200 return 0
201 }
202
203 # bool iproute2_iface_stop(char *interface)
204 #
205 # Do final shutdown for an interface or alias.
206 #
207 # Returns 0 (true) when successful, non-zero (false) on failure
208 iproute2_iface_stop() {
209 local label="$1" iface=$( interface_device "$1" )
210
211 # Shut down the link if this isn't an alias or vlan
212 if [[ ${label} == "${iface}" ]]; then
213 iproute2_down "${iface}"
214 return $?
215 fi
216 return 0
217 }
218
219 # bool iproute2_add_address(char *interface, char *options ...)
220 #
221 # Adds an the specified address to the interface
222 # returns 0 on success and non-zero on failure
223 iproute2_add_address() {
224 local iface="$1" x
225
226 iproute2_exists "${iface}" true || return 1
227
228 # Extract the config
229 local -a config=( "$@" )
230 config=( ${config[@]:1} )
231
232 # Convert an ifconfig line to iproute2
233 local n="${#config[@]}"
234 for (( x=0; x<n; x++ )); do
235 case "${config[x]}" in
236 netmask)
237 config[0]="${config[0]}/$( netmask2cidr ${config[x+1]} )"
238 unset config[x] config[x+1]
239 ;;
240 mtu)
241 ip link set mtu "${config[x+1]}" dev "${iface}"
242 unset config[x] config[x+1]
243 ;;
244 esac
245 done
246 config=( "${config[@]//pointtopoint/peer}" )
247
248 # Always scope lo addresses as host unless specified otherwise
249 [[ ${iface} == "lo" && " ${config[@]} " != *" scope "* ]] \
250 && config=( "${config[@]}" "scope host" )
251
252 # IPv4 specifics
253 if [[ ${config[@]} == *.*.*.* ]]; then
254 # Work out a broadcast if none supplied
255 [[ ${config[@]} != *" brd "* && ${config[@]} != *" broadcast "* ]] \
256 && config=( "${config[@]}" "brd +" )
257 fi
258
259 # Ensure that the interface is up so we can add IPv6 addresses
260 interface_up "${iface}"
261
262 # Some kernels like to apply lo with an address when they are brought up
263 if [[ ${iface} == "lo" \
264 && ${config[@]} == "127.0.0.1/8 brd 127.255.255.255 scope host" ]]; then
265 ip addr del dev "${iface}" 127.0.0.1/8 2>/dev/null
266 fi
267
268 ip addr add dev "${iface}" ${config[@]}
269 local r="$?"
270 [[ ${r} != "0" ]] && return "${r}"
271
272 local metric ifvar=$( bash_variable "${iface}" )
273 # Remove the newly added route and replace with our metric
274 eval metric=\"\$\{metric_${ifvar}\}\"
275 [[ ${metric} == "0" || ${RC_AUTO_INTERFACE} != "yes" ]] && return "${r}"
276
277 local network=$( ip_network "${config[0]}" )
278 [[ -z ${network} ]] && return "${r}"
279
280 local cidr="${config[0]##*/}"
281 if ip route del "${network}/${cidr}" metric 0 dev "${iface}" \
282 2>/dev/null ; then
283 ip route add "${network}/${cidr}" metric "${metric}" dev "${iface}"
284 fi
285
286 return "${r}"
287 }
288
289 # bool iproute2_pre_start(char *interface)
290 #
291 # Runs any pre_start stuff on our interface - just the MTU atm
292 # We set MTU twice as it may be needed for DHCP - a dhcp client could
293 # change it in error, so we set MTU in post start too
294 iproute2_pre_start() {
295 local iface="$1"
296
297 interface_exists "${iface}" || return 0
298
299 local ifvar=$( bash_variable "$1" ) mtu
300
301 # MTU support
302 eval mtu=\"\$\{mtu_${ifvar}\}\"
303 [[ -n ${mtu} ]] && ip link set mtu "${mtu}" dev "${iface}"
304
305 return 0
306 }
307
308 # bool iproute2_post_start(char *interface)
309 #
310 # Runs any post_start stuff on our interface and adds routes
311 # Always returns 0
312 iproute2_post_start() {
313 local iface="$1" ifvar=$( bash_variable "$1" ) routes metric mtu x netmask
314
315 # Make sure interface is marked UP
316 iproute2_up "${iface}"
317
318 # MTU support
319 eval mtu=\"\$\{mtu_${ifvar}\}\"
320 [[ -n ${mtu} ]] && ip link set mtu "${mtu}" dev "${iface}"
321
322 eval routes=( \"\$\{routes_${ifvar}\[@\]\}\" )
323 eval metric=\"\$\{metric_${ifvar}\:-0}\"
324
325 # Test for old style ipaddr variable
326 if [[ -z ${routes} ]]; then
327 eval routes=( \"\$\{iproute_${ifvar}\[@\]\}\" )
328 fi
329
330 [[ -z ${routes} ]] && return 0
331
332 # Set routes with ip route -- this might also include default route
333 einfo "Adding routes"
334 eindent
335 for x in "${routes[@]}"; do
336 ebegin "${x}"
337
338 # Support net-tools routing too
339 x="${x//gw/via}"
340 x="${x//-A inet6}"
341 x="${x//-net}"
342 [[ " ${x} " == *" -host "* ]] && x="${x//-host} scope host"
343
344 # Attempt to support net-tools route netmask option
345 netmask="${x##* netmask }"
346 if [[ -n ${netmask} && ${x} != "${netmask}" ]]; then
347 netmask="${netmask%% *}"
348 x="${x// netmask ${netmask} / }"
349 local -a a=( ${x} )
350 a[0]="${a[0]}/$( netmask2cidr ${netmask} )"
351 x="${a[@]}"
352 fi
353
354 # Add a metric if we don't have one
355 [[ " ${x} " != *" metric "* ]] && x="${x} metric ${metric}"
356
357 ip route append ${x} dev "${iface}"
358 eend $?
359 done
360 eoutdent
361
362 return 0
363 }
364
365 # void iproute2_default_route(char* interface, char* gateway_ip, int metric)
366 #
367 # Force default route to the specified gateway, optionally on
368 # the given interface
369 iproute2_default_route() {
370 local metric="${3:-0}"
371
372 ip route change default via "$2" metric "${metric}" dev "$1" 2>/dev/null \
373 || ip route append default via "$2" metric "${metric}" dev "$1" 2>/dev/null
374 }
375
376 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20