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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 236 - (show annotations) (download)
Tue Feb 14 13:00:44 2006 UTC (8 years, 7 months ago) by phreak
File size: 10923 byte(s)
Merging r1881
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 provide interface
22 functions interface_device
23 variables config routes fallback metric ipaddr ipaddr_fallback iproute inet6
24 }
25
26 # bool iproute2_check_installed(void)
27 #
28 # Returns 1 if iproute2 is installed, otherwise 0
29 iproute2_check_installed() {
30 local report="${1:-false}" installed="0"
31 if [[ ! -x /sbin/ip ]] ; then
32 installed="1"
33 ${report} && eerror "For iproute2 support, emerge sys-apps/iproute2"
34 fi
35 if [[ ! -e /proc/net/netlink ]] ; then
36 installed="1"
37 ${report} && eerror "iproute2 requires NetLink enabled in the kernel"
38 fi
39 return "${installed}"
40 }
41
42 # bool iproute2_exists(char *interface, bool report)
43 #
44 # Returns 1 if the interface exists, otherwise 0
45 iproute2_exists() {
46 local e="$( ip addr show label "$1" )" report="${2:-false}"
47 [[ -n ${e} ]] && return 0
48
49 if ${report} ; then
50 eerror "network interface $1 does not exist"
51 eerror "Please verify hardware or kernel module (driver)"
52 fi
53 return 1
54 }
55
56 # void iproute2_up(char *interface)
57 #
58 # provides a generic interface for bringing interfaces up
59 iproute2_up() {
60 ip link set up dev "$1"
61 }
62
63 # void iproute2_down(char *interface)
64 #
65 # provides a generic interface for bringing interfaces up
66 iproute2_down() {
67 ip link set down dev "$1"
68 }
69
70 # bool ifproute2_is_up(char *iface, bool withaddress)
71 #
72 # Returns 0 if the interface is up, otherwise 1
73 # If withaddress is true then the interface has to have an IPv4 address
74 # assigned as well
75 iproute2_is_up() {
76 local check="\<UP\>" addr="${2:-false}"
77 ${addr} && check="${check}.*inet "
78 ip addr show "$1" | xargs | grep -Eq "${check}" && return 0
79 return 1
80 }
81
82 # void iproute2_set_flag(char *iface, char *flag, bool enabled)
83 #
84 # Sets or disables the interface flag
85 iproute2_set_flag() {
86 local enable="$3" opt="on"
87 ${enable} || opt="off"
88 ip link set "$1" "$2" "${opt}"
89 }
90
91 # void iproute2_get_address(char *interface)
92 #
93 # Fetch the address retrieved by DHCP. If successful, echoes the
94 # address on stdout, otherwise echoes nothing.
95 iproute2_get_address() {
96 ip -family inet addr show "$1" | sed -n -e 's/.*inet \([^ ]*\).*/\1/p'
97 }
98
99 # bool iproute2_is_ethernet(char *interface)
100 #
101 # Return 0 if the link is ethernet, otherwise 1.
102 iproute2_is_ethernet() {
103 ip link show "$1" | grep -q "^[[:space:]]*link/ether[[:space:]]*"
104 }
105
106 # void iproute2_get_mac_address(char *interface)
107 #
108 # Fetch the mac address assingned to the network card
109 iproute2_get_mac_address() {
110 local mac="$( ip link show "$1" | sed -n -e \
111 '/link\// s/^.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' )"
112 [[ ${mac} != '00:00:00:00:00:00' \
113 && ${mac} != '44:44:44:44:44:44' \
114 && ${mac} != 'FF:FF:FF:FF:FF:FF' ]] \
115 && echo "${mac}"
116 }
117
118 # void iproute2_set_mac_address(char *interface, char *mac)
119 #
120 # Assigned the mac address to the network card
121 iproute2_set_mac_address() {
122 ip link set address "$2" dev "$1"
123 }
124
125 # int iproute2_set_name(char *interface, char *new_name)
126 #
127 # Renames the interface
128 # This will not work if the interface is setup!
129 iproute2_set_name() {
130 ip link set name "$2" dev "$1"
131 }
132
133 # void iproute2_get_aliases_rev(char *interface)
134 #
135 # Fetch the list of aliases for an interface.
136 # Outputs a space-separated list on stdout, in reverse order, for
137 # example "eth0:2 eth0:1"
138 iproute2_get_aliases_rev() {
139 local iface="$( interface_device "$1" )"
140 ip addr show dev "${iface}" | grep -o "${iface}:[0-9].*" | tac
141 }
142
143 # bool iproute2_del_addresses(char *interface, bool onlyinet)
144 #
145 # Remove addresses from interface.
146 # If onlyinet is true, then we only remove IPv4 / inet addresses.
147 iproute2_del_addresses() {
148 local pre=""
149 ${2:-false} && pre="-f inet"
150 ip ${pre} addr flush label "$1" scope global &>/dev/null
151 ip ${pre} addr flush label "$1" scope site &>/dev/null
152 ip ${pre} addr flush label "$1" scope host &>/dev/null
153 return 0
154 }
155
156 # bool iproute2_get_old_config(char *iface)
157 #
158 # Returns config and config_fallback for the given interface
159 iproute2_get_old_config() {
160 local ifvar="$( bash_variable "$1" )" inet6="" t=""
161
162 # iproute2-style config vars
163 t="ipaddr_${ifvar}[@]"
164 config=( "${!t}" )
165 t="config_fallback_${ifvar}[@]"
166 config_fallback=( "${!t}" )
167 t="inet6_${ifvar}[@]"
168 inet6=( "${!t}" )
169
170 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
171 [[ ${#inet6[@]} == "1" && ${inet6} == *" "* ]] && inet6=( ${inet6} )
172
173 # Add inet6 addresses to our config if required
174 [[ -n ${inet6} ]] && config=( "${config[@]}" "${inet6[@]}" )
175
176 # Support old style iface_xxx syntax
177 if [[ -z ${config} ]] ; then
178 if is_function ifconfig_get_old_config ; then
179 ifconfig_get_old_config "${iface}"
180 fi
181 fi
182
183 return 0
184 }
185
186 # bool iproute2_iface_stop(char *interface)
187 #
188 # Do final shutdown for an interface or alias.
189 #
190 # Returns 0 (true) when successful, non-zero (false) on failure
191 iproute2_iface_stop() {
192 local label="$1" iface="$( interface_device "$1" )"
193
194 # Shut down the link if this isn't an alias or vlan
195 if [[ ${label} == "${iface}" ]] ; then
196 iproute2_down "${iface}"
197 return $?
198 fi
199 return 0
200 }
201
202 # bool iproute2_add_address(char *interface, char *options ...)
203 #
204 # Adds an the specified address to the interface
205 # returns 0 on success and non-zero on failure
206 iproute2_add_address() {
207 local iface="$1" x=""
208
209 iproute2_exists "${iface}" true || return 1
210
211 # Extract the config
212 local -a config=( "$@" )
213 config=( ${config[@]:1} )
214
215 # Convert an ifconfig line to iproute2
216 local n="${#config[@]}"
217 for (( x=0; x<n; x++ )); do
218 case "${config[x]}" in
219 netmask)
220 config[0]="${config[0]}/$( netmask2cidr "${config[x+1]}" )"
221 unset config[x] config[x+1]
222 ;;
223 mtu)
224 ip link set mtu "${config[x+1]}" dev "${iface}"
225 unset config[x] config[x+1]
226 ;;
227 esac
228 done
229 config=( "${config[@]//pointopoint/peer}" )
230
231 # Always scope lo addresses as host unless specified otherwise
232 [[ ${iface} == "lo" && " ${config[@]} " != *" scope "* ]] \
233 && config=( "${config[@]}" "scope host" )
234
235 # IPv4 specifics
236 if [[ ${config[@]} == *.*.*.* ]] ; then
237 # Work out a broadcast if none supplied
238 [[ ${config[@]} != *" brd "* && ${config[@]} != *" broadcast "* ]] \
239 && config=( "${config[@]}" "brd +" )
240 fi
241
242 # Ensure that the interface is up so we can add IPv6 addresses
243 interface_up "${iface}"
244
245 # Some kernels like to apply lo with an address when they are brought up
246 if [[ ${iface} == "lo" \
247 && ${config[@]} == "127.0.0.1/8 brd 127.255.255.255 scope host" ]] ; then
248 ip addr del dev "${iface}" 127.0.0.1/8 2>/dev/null
249 fi
250
251 ip addr add dev "${iface}" ${config[@]}
252 local r="$?"
253 [[ ${r} != "0" ]] && return "${r}"
254
255 local ifvar="$( bash_variable "${iface}" )"
256 # Remove the newly added route and replace with our metric
257 local metric="metric_${ifvar}"
258 [[ ${!metric:-0} == "0" ]] && return "${r}"
259
260 local network="$( ip_network "${config[0]}" )"
261 [[ -z ${network} ]] && return "${r}"
262
263 local cidr="${config[0]##*/}"
264 if ip route del "${network}/${cidr}" metric 0 dev "${iface}" \
265 2>/dev/null ; then
266 ip route add "${network}/${cidr}" metric "${!metric:-0}" dev "${iface}"
267 fi
268
269 return "${r}"
270 }
271
272 # bool iproute2_pre_start(char *interface)
273 #
274 # Runs any pre_start stuff on our interface - just the MTU atm
275 # We set MTU twice as it may be needed for DHCP - a dhcp client could
276 # change it in error, so we set MTU in post start too
277 iproute2_pre_start() {
278 local iface="$1"
279
280 interface_exists "${iface}" || return 0
281
282 local ifvar="$( bash_variable "$1" )"
283
284 # MTU support
285 local mtu="mtu_${ifvar}"
286 [[ -n ${!mtu} ]] && ip link set mtu "${!mtu}" dev "${iface}"
287
288 return 0
289 }
290
291 # bool iproute2_post_start(char *interface)
292 #
293 # Runs any post_start stuff on our interface and adds routes
294 # Always returns 0
295 iproute2_post_start() {
296 local iface="$1" ifvar="$( bash_variable "$1" )" x=""
297
298 iproute2_exists "${iface}" || return 0
299
300 # Make sure interface is marked UP
301 iproute2_up "${iface}"
302
303 # MTU support
304 local mtu="mtu_${ifvar}"
305 [[ -n ${!mtu} ]] && ip link set mtu "${!mtu}" dev "${iface}"
306
307 local x="routes_${ifvar}[@]"
308 local -a routes=( "${!x}" )
309 local metric="metric_${ifvar}"
310
311 # Test for old style ipaddr variable
312 if [[ -z ${routes} ]] ; then
313 t="iproute_${ifvar}[@]"
314 routes=( "${!t}" )
315 fi
316
317 # Set routes with ip route -- this might also include default route
318 if [[ -n ${routes} ]] ; then
319 einfo "Adding routes"
320 eindent
321 for x in "${routes[@]}"; do
322 ebegin "${x}"
323
324 # Support net-tools routing too
325 x="${x//gw/via}"
326 x="${x//-A inet6/}"
327 x="${x//-net/}"
328 [[ " ${x} " == *" -host "* ]] && x="${x//-host/} scope host"
329
330 # Attempt to support net-tools route netmask option
331 netmask="${x##* netmask }"
332 if [[ -n ${netmask} && ${x} != "${netmask}" ]] ; then
333 netmask="${netmask%% *}"
334 x="${x// netmask ${netmask} / }"
335 local -a a=( ${x} )
336 a[0]="${a[0]}/$( netmask2cidr "${netmask}")"
337 x="${a[@]}"
338 fi
339
340 # Add a metric if we don't have one
341 [[ " ${x} " != *" metric "* ]] && x="${x} metric ${!metric}"
342
343 ip route append ${x} dev "${iface}"
344 eend $?
345 done
346 eoutdent
347 fi
348
349 # Now apply any rules we may want
350 x="rules_${ifvar}[@]"
351 local -a rules=( "${!x}" )
352 if [[ -n ${rules} ]] ; then
353 einfo "Adding IP policy routing rules"
354 eindent
355 # Ensure that the kernel supports policy routing
356 if ! ip rule list | grep -q "^" ; then
357 eerror "You need to enable IP Policy Routing (CONFIG_IP_MULTIPLE_TABLES)"
358 eerror "in your kernel to use ip rules"
359 else
360 for x in "${rules[@]}" ; do
361 ebegin "${x}"
362 ip rule add ${x} dev "${iface}"
363 eend $?
364 done
365 fi
366 eoutdent
367 fi
368
369 # Flush the route cache
370 ip route flush cache dev "${iface}"
371
372 return 0
373 }
374
375 # void iproute2_route_metric(char* interface, int metric)
376 #
377 # Change all routes for an interface to a given metric
378 iproute2_route_metric() {
379 local route=""
380 ip route | grep " dev $1" | {
381 while read route ; do
382 ip route del ${route}
383 ip route add ${route} metric "$2"
384 done
385 }
386 }
387
388 # void iproute2_default_route(char* interface, char* gateway_ip, int metric)
389 #
390 # Force default route to the specified gateway, optionally on
391 # the given interface
392 iproute2_default_route() {
393 local metric="${3:-0}"
394
395 ip route change default via "$2" metric "${metric}" dev "$1" 2>/dev/null \
396 || ip route append default via "$2" metric "${metric}" dev "$1" 2>/dev/null
397 }
398
399 # void iproute2_post_stop(char* interface)
400 iproute2_post_stop() {
401 local iface="$1" rule=""
402
403 iproute2_exists "${iface}" || return
404
405 # Remove any rules for this interface
406 ip rule list | grep " iif ${iface}[ ]*" | {
407 while read rule ; do
408 rule="${rule#*:}"
409 ip rule del ${rule}
410 done
411 }
412
413 # Flush the route cache
414 ip route flush cache dev "${iface}"
415 }
416
417 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20