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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20