/[vps]/baselayout-vserver/branches/baselayout-1_12/net-scripts/net.modules.d/iproute2
Gentoo

Contents of /baselayout-vserver/branches/baselayout-1_12/net-scripts/net.modules.d/iproute2

Parent Directory Parent Directory | Revision Log Revision Log


Revision 206 - (show annotations) (download)
Sun Jan 15 10:27:03 2006 UTC (8 years, 3 months ago) by phreak
File size: 9747 byte(s)
Merging r1822 for branches/baselayout-1_12
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" \
97 | sed -n -e 's/.*inet \([^ ]*\).*/\1/p'
98 }
99
100 # void iproute2_get_mac_address(char *interface)
101 #
102 # Fetch the mac address assingned to the network card
103 iproute2_get_mac_address() {
104 local mac=$( ip link show "$1" | sed -n -e \
105 's/^.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' )
106 [[ ${mac} != '00:00:00:00:00:00' \
107 && ${mac} != '44:44:44:44:44:44' \
108 && ${mac} != 'FF:FF:FF:FF:FF:FF' ]] \
109 && echo "${mac}"
110 }
111
112 # void iproute2_set_mac_address(char *interface, char *mac)
113 #
114 # Assigned the mac address to the network card
115 iproute2_set_mac_address() {
116 ip link set address "$2" dev "$1"
117 }
118
119 # int iproute2_set_name(char *interface, char *new_name)
120 #
121 # Renames the interface
122 # This will not work if the interface is setup!
123 iproute2_set_name() {
124 ip link set name "$2" dev "$1"
125 }
126
127 # void iproute2_get_aliases_rev(char *interface)
128 #
129 # Fetch the list of aliases for an interface.
130 # Outputs a space-separated list on stdout, in reverse order, for
131 # example "eth0:2 eth0:1"
132 iproute2_get_aliases_rev() {
133 local iface=$( interface_device "$1" )
134 ip addr show dev "${iface}" | grep -o "${iface}:[0-9].*" | tac
135 }
136
137 # bool iproute2_del_addresses(char *interface, bool onlyinet)
138 #
139 # Remove addresses from interface.
140 # If onlyinet is true, then we only remove IPv4 / inet addresses.
141 iproute2_del_addresses() {
142 local pre=""
143 ${2:-false} && pre="-f inet"
144 ip ${pre} addr flush label "$1" scope global &>/dev/null
145 ip ${pre} addr flush label "$1" scope site &>/dev/null
146 ip ${pre} addr flush label "$1" scope host &>/dev/null
147 return 0
148 }
149
150 # bool iproute2_get_old_config(char *iface)
151 #
152 # Returns config and config_fallback for the given interface
153 iproute2_get_old_config() {
154 local ifvar=$( bash_variable "$1" ) inet6 t
155
156 # iproute2-style config vars
157 t="ipaddr_${ifvar}[@]"
158 config=( "${!t}" )
159 t="config_fallback_${ifvar}[@]"
160 config_fallback=( "${!t}" )
161 t="inet6_${ifvar}[@]"
162 inet6=( "${!t}" )
163
164 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
165 [[ ${#inet6[@]} == "1" && ${inet6} == *" "* ]] && inet6=( ${inet6} )
166
167 # Add inet6 addresses to our config if required
168 [[ -n ${inet6} ]] && config=( "${config[@]}" "${inet6[@]}" )
169
170 # Support old style iface_xxx syntax
171 if [[ -z ${config} ]] ; then
172 if is_function ifconfig_get_old_config ; then
173 ifconfig_get_old_config "${iface}"
174 fi
175 fi
176
177 return 0
178 }
179
180 # bool iproute2_iface_stop(char *interface)
181 #
182 # Do final shutdown for an interface or alias.
183 #
184 # Returns 0 (true) when successful, non-zero (false) on failure
185 iproute2_iface_stop() {
186 local label="$1" iface=$( interface_device "$1" )
187
188 # Shut down the link if this isn't an alias or vlan
189 if [[ ${label} == "${iface}" ]]; then
190 iproute2_down "${iface}"
191 return $?
192 fi
193 return 0
194 }
195
196 # bool iproute2_add_address(char *interface, char *options ...)
197 #
198 # Adds an the specified address to the interface
199 # returns 0 on success and non-zero on failure
200 iproute2_add_address() {
201 local iface="$1" x
202
203 iproute2_exists "${iface}" true || return 1
204
205 # Extract the config
206 local -a config=( "$@" )
207 config=( ${config[@]:1} )
208
209 # Convert an ifconfig line to iproute2
210 local n="${#config[@]}"
211 for (( x=0; x<n; x++ )); do
212 case "${config[x]}" in
213 netmask)
214 config[0]="${config[0]}/$( netmask2cidr ${config[x+1]} )"
215 unset config[x] config[x+1]
216 ;;
217 mtu)
218 ip link set mtu "${config[x+1]}" dev "${iface}"
219 unset config[x] config[x+1]
220 ;;
221 esac
222 done
223 config=( "${config[@]//pointopoint/peer}" )
224
225 # Always scope lo addresses as host unless specified otherwise
226 [[ ${iface} == "lo" && " ${config[@]} " != *" scope "* ]] \
227 && config=( "${config[@]}" "scope host" )
228
229 # IPv4 specifics
230 if [[ ${config[@]} == *.*.*.* ]]; then
231 # Work out a broadcast if none supplied
232 [[ ${config[@]} != *" brd "* && ${config[@]} != *" broadcast "* ]] \
233 && config=( "${config[@]}" "brd +" )
234 fi
235
236 # Ensure that the interface is up so we can add IPv6 addresses
237 interface_up "${iface}"
238
239 # Some kernels like to apply lo with an address when they are brought up
240 if [[ ${iface} == "lo" \
241 && ${config[@]} == "127.0.0.1/8 brd 127.255.255.255 scope host" ]]; then
242 ip addr del dev "${iface}" 127.0.0.1/8 2>/dev/null
243 fi
244
245 ip addr add dev "${iface}" ${config[@]}
246 local r="$?"
247 [[ ${r} != "0" ]] && return "${r}"
248
249 local metric ifvar=$( bash_variable "${iface}" )
250 # Remove the newly added route and replace with our metric
251 metric="metric_${ifvar}"
252 [[ ${!metric} == "0" || ${RC_AUTO_INTERFACE} != "yes" ]] && return "${r}"
253
254 local network=$( ip_network "${config[0]}" )
255 [[ -z ${network} ]] && return "${r}"
256
257 local cidr="${config[0]##*/}"
258 if ip route del "${network}/${cidr}" metric 0 dev "${iface}" \
259 2>/dev/null ; then
260 ip route add "${network}/${cidr}" metric "${!metric}" dev "${iface}"
261 fi
262
263 return "${r}"
264 }
265
266 # bool iproute2_pre_start(char *interface)
267 #
268 # Runs any pre_start stuff on our interface - just the MTU atm
269 # We set MTU twice as it may be needed for DHCP - a dhcp client could
270 # change it in error, so we set MTU in post start too
271 iproute2_pre_start() {
272 local iface="$1"
273
274 interface_exists "${iface}" || return 0
275
276 local ifvar=$( bash_variable "$1" ) mtu
277
278 # MTU support
279 mtu="mtu_${ifvar}"
280 [[ -n ${!mtu} ]] && ip link set mtu "${!mtu}" dev "${iface}"
281
282 return 0
283 }
284
285 # bool iproute2_post_start(char *interface)
286 #
287 # Runs any post_start stuff on our interface and adds routes
288 # Always returns 0
289 iproute2_post_start() {
290 local iface="$1" ifvar=$( bash_variable "$1" ) routes metric mtu x netmask
291
292 iproute2_exists "${iface}" || return 0
293
294 # Make sure interface is marked UP
295 iproute2_up "${iface}"
296
297 # MTU support
298 mtu="mtu_${ifvar}"
299 [[ -n ${!mtu} ]] && ip link set mtu "${!mtu}" dev "${iface}"
300
301 x="routes_${ifvar}[@]"
302 routes=( "${!x}" )
303 metric="metric_${ifvar}"
304
305 # Test for old style ipaddr variable
306 if [[ -z ${routes} ]]; then
307 t="iproute_${ifvar}[@]"
308 routes=( "${!t}" )
309 fi
310
311 [[ -z ${routes} ]] && return 0
312
313 # Set routes with ip route -- this might also include default route
314 einfo "Adding routes"
315 eindent
316 for x in "${routes[@]}"; do
317 ebegin "${x}"
318
319 # Support net-tools routing too
320 x="${x//gw/via}"
321 x="${x//-A inet6/}"
322 x="${x//-net/}"
323 [[ " ${x} " == *" -host "* ]] && x="${x//-host/} scope host"
324
325 # Attempt to support net-tools route netmask option
326 netmask="${x##* netmask }"
327 if [[ -n ${netmask} && ${x} != "${netmask}" ]]; then
328 netmask="${netmask%% *}"
329 x="${x// netmask ${netmask} / }"
330 local -a a=( ${x} )
331 a[0]="${a[0]}/$( netmask2cidr ${netmask} )"
332 x="${a[@]}"
333 fi
334
335 # Add a metric if we don't have one
336 [[ " ${x} " != *" metric "* ]] && x="${x} metric ${!metric}"
337
338 ip route append ${x} dev "${iface}"
339 eend $?
340 done
341 eoutdent
342
343 return 0
344 }
345
346 # void iproute2_route_metric(char* interface, int metric)
347 #
348 # Change all routes for an interface to a given metric
349 iproute2_route_metric() {
350 local route
351 ip route | grep " dev $1" | {
352 while read route ; do
353 ip route del ${route}
354 ip route add ${route} metric "$2"
355 done
356 }
357 }
358
359 # void iproute2_default_route(char* interface, char* gateway_ip, int metric)
360 #
361 # Force default route to the specified gateway, optionally on
362 # the given interface
363 iproute2_default_route() {
364 local metric="${3:-0}"
365
366 ip route change default via "$2" metric "${metric}" dev "$1" 2>/dev/null \
367 || ip route append default via "$2" metric "${metric}" dev "$1" 2>/dev/null
368 }
369
370 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20