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

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

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: 12407 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 ifconfig() {
10 LC_ALL=C /sbin/ifconfig "$@"
11 }
12
13 ifconfig_tunnel() {
14 LC_ALL=C /sbin/iptunnel "$@"
15 }
16
17 route() {
18 LC_ALL=C /sbin/route "$@"
19 }
20
21 # void ifconfig_depend(void)
22 #
23 # Sets up the dependancies for the module
24 ifconfig_depend() {
25 provide interface
26 variables config routes fallback metric ifconfig \
27 ifconfig_fallback routes inet6 iface alias broadcast netmask
28 }
29
30 # bool ifconfig_check_installed(void)
31 #
32 # Returns 1 if ifconfig is installed, otherwise 0
33 ifconfig_check_installed() {
34 [[ -x /sbin/ifconfig ]] && return 0
35 ${1:-false} && eerror "For ifconfig support, emerge sys-apps/net-tools"
36 return 1
37 }
38
39 # bool ifconfig_exists(char *interface, bool report)
40 #
41 # Returns 1 if the interface exists, otherwise 0
42 ifconfig_exists() {
43 local e=$( ifconfig -a | grep -o "^$1" ) report="${2:-false}"
44 [[ -n ${e} ]] && return 0
45
46 if ${report} ; then
47 eerror "network interface $1 does not exist"
48 eerror "Please verify hardware or kernel module (driver)"
49 fi
50
51 return 1
52 }
53
54 # void ifconfig_up(char *iface)
55 #
56 # provides a generic interface for bringing interfaces up
57 ifconfig_up() {
58 ifconfig "$1" up
59 }
60
61 # void ifconfig_down(char *iface)
62 #
63 # provides a generic interface for bringing interfaces down
64 ifconfig_down() {
65 ifconfig "$1" down
66 }
67
68 # bool ifconfig_is_up(char *iface, bool withaddress)
69 #
70 # Returns 0 if the interface is up, otherwise 1
71 # If withaddress is true then the interface has to have an IPv4 address
72 # assigned as well
73 ifconfig_is_up() {
74 local check="\<UP\>" addr="${2:-false}"
75 ${addr} && check="\<inet addr:.*${check}"
76 ifconfig "$1" | grep -Eq "${check}" && return 0
77 return 1
78 }
79
80 # void ifconfig_set_flag(char *iface, char *flag, bool enabled)
81 #
82 # Sets or disables the interface flag
83 ifconfig_set_flag() {
84 local iface="$1" flag="$2" enable="$3"
85 ${enable} || flag="-${flag}"
86 ifconfig "${iface}" "${flag}"
87 }
88
89 # void ifconfig_get_address(char *interface)
90 #
91 # Fetch the address retrieved by DHCP. If successful, echoes the
92 # address on stdout, otherwise echoes nothing.
93 ifconfig_get_address() {
94 local -a x=( $( ifconfig "$1" \
95 | sed -n -e 's/.*inet addr:\([^ ]*\).*Mask:\([^ ]*\).*/\1 \2/p' ) )
96 x[1]=$( netmask2cidr "${x[1]}" )
97 [[ -n ${x[0]} ]] && echo "${x[0]}/${x[1]}"
98 }
99
100 # bool ifconfig_is_ethernet(char *interface)
101 #
102 # Return 0 if the link is ethernet, otherwise 1.
103 ifconfig_is_ethernet() {
104 ifconfig "$1" | grep -q "^$1[[:space:]]*Link encap:Ethernet[[:space:]]"
105 }
106
107 # void ifconfig_get_mac_address(char *interface)
108 #
109 # Fetch the mac address assingned to the network card
110 ifconfig_get_mac_address() {
111 local mac=$( ifconfig "$1" | sed -n -e \
112 's/.*HWaddr[ \t]*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' )
113 [[ ${mac} != '00:00:00:00:00:00' \
114 && ${mac} != '44:44:44:44:44:44' \
115 && ${mac} != 'FF:FF:FF:FF:FF:FF' ]] \
116 && echo "${mac}"
117 }
118
119 # void ifconfig_set_mac_address(char *interface, char *mac)
120 #
121 # Assigned the mac address to the network card
122 ifconfig_set_mac_address() {
123 ifconfig "$1" hw ether "$2"
124 }
125
126 # int ifconfig_set_name(char *interface, char *new_name)
127 #
128 # Renames the interface
129 # This will not work if the interface is setup!
130 ifconfig_set_name() {
131 [[ -z $2 ]] && return 1
132 local current="$1" new="$2"
133
134 local mac=$( ifconfig_get_mac_address "${current}" )
135 if [[ -z ${mac} ]]; then
136 eerror "${iface} does not have a MAC address"
137 return 1
138 fi
139
140 /sbin/nameif "${new}" "${mac}"
141 }
142
143 # void ifconfig_get_aliases_rev(char *interface)
144 #
145 # Fetch the list of aliases for an interface.
146 # Outputs a space-separated list on stdout, in reverse order, for
147 # example "eth0:2 eth0:1"
148 ifconfig_get_aliases_rev() {
149 ifconfig | grep -o "^$1:[0-9]* " | tac
150 }
151
152 # bool ifconfig_del_addresses(char *interface, bool onlyinet)
153 #
154 # Remove addresses from interface. Returns 0 (true) if there
155 # were addresses to remove (whether successful or not). Returns 1
156 # (false) if there were no addresses to remove.
157 # If onlyinet is true then we only delete IPv4 / inet addresses
158 ifconfig_del_addresses() {
159 local iface="$1" i onlyinet="${2:-false}"
160 # We don't remove addresses from aliases
161 [[ ${iface} == *:* ]] && return 0
162
163 # If the interface doesn't exist, don't try and delete
164 ifconfig_exists "${iface}" || return 0
165
166 # iproute2 can add many addresses to an iface unlike ifconfig ...
167 # iproute2 added addresses cause problems for ifconfig
168 # as we delete an address, a new one appears, so we have to
169 # keep polling
170 while ifconfig "${iface}" | grep -q -m1 -o 'inet addr:[^ ]*' ; do
171 ifconfig "${iface}" 0.0.0.0 || break
172 done
173
174 # Remove IPv6 addresses
175 if ! ${onlyinet} ; then
176 for i in $( ifconfig "${iface}" \
177 | sed -n -e 's/^.*inet6 addr: \([^ ]*\) Scope:[^L].*/\1/p' ) ; do
178 /sbin/ifconfig "${iface}" inet6 del "${i}"
179 done
180 fi
181 return 0
182 }
183
184 # bool ifconfig_get_old_config(char *iface)
185 #
186 # Returns config and config_fallback for the given interface
187 ifconfig_get_old_config() {
188 local iface="$1" ifvar=$( bash_variable "$1" ) i inet6
189
190 config="ifconfig_${ifvar}[@]"
191 config=( "${!config}" )
192 config_fallback="ifconfig_fallback_${ifvar}[@]"
193 config_fallback=( "${!config_fallback}" )
194 inet6="inet6_${ifvar}[@]"
195 inet6=( "${!inet6}" )
196
197 # BACKWARD COMPATIBILITY: populate the config_IFACE array
198 # if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0)
199 i="iface_${ifvar}"
200 if [[ -n ${!i} && -z ${config} ]]; then
201 # Make sure these get evaluated as arrays
202 local -a aliases broadcasts netmasks
203
204 # Start with the primary interface
205 config=( ${!i} )
206
207 # ..then add aliases
208 aliases="alias_${ifvars}"
209 aliases=( ${!aliases} )
210 broadcasts="broadcast_${ifvar}"
211 broadcasts=( ${!broadcasts} )
212 netmasks="netmask_${ifvar}"
213 netmasks=( ${!netmasks} )
214 for (( i=0; i<${#aliases[@]}; i++ )); do
215 config[i+1]="${aliases[i]} ${broadcasts[i]:+broadcast ${broadcasts[i]}} ${netmasks[i]:+netmask ${netmasks[i]}}"
216 done
217 fi
218
219 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
220 [[ ${#inet6[@]} == 1 && ${inet6} == *' '* ]] && inet6=( ${inet6} )
221
222 # Add inet6 addresses to our config if required
223 [[ -n ${inet6} ]] && config=( "${config[@]}" "${inet6[@]}" )
224
225 return 0
226 }
227
228 # bool ifconfig_iface_stop(char *interface)
229 #
230 # Do final shutdown for an interface or alias.
231 #
232 # Returns 0 (true) when successful, non-zero (false) on failure
233 ifconfig_iface_stop() {
234 # If an alias is already down, then "ifconfig eth0:1 down"
235 # will try to bring it up with an address of "down" which
236 # fails. Do some double-checking before returning error
237 # status
238 ifconfig_is_up "$1" || return 0
239 ifconfig_down "$1" && return 0
240
241 # It is sometimes impossible to transition an alias from the
242 # UP state... particularly if the alias has no address. So
243 # ignore the failure, which should be okay since the entire
244 # interface will be shut down eventually.
245 [[ $1 == *:* ]] && return 0
246 return 1
247 }
248
249 # bool ifconfig_pre_start(char *interface)
250 #
251 # Runs any pre_start stuff on our interface - just the MTU atm
252 # We set MTU twice as it may be needed for DHCP - a dhcp client could
253 # change it in error, so we set MTU in post start too
254 ifconfig_pre_start() {
255 local iface="$1"
256
257 interface_exists "${iface}" || return 0
258
259 local ifvar=$( bash_variable "$1" ) mtu
260
261 # MTU support
262 mtu="mtu_${ifvar}"
263 [[ -n ${!mtu} ]] && ifconfig "${iface}" mtu "${!mtu}"
264
265 return 0
266 }
267
268
269 # bool ifconfig_post_start(char *iface)
270 #
271 # Bring up iface using ifconfig utilities, called from iface_start
272 #
273 # Returns 0 (true) when successful on the primary interface, non-zero
274 # (false) when the primary interface fails. Aliases are allowed to
275 # fail, the routine should still return success to indicate that
276 # net.eth0 was successful
277 ifconfig_post_start() {
278 local iface="$1" ifvar=$( bash_variable "$1" ) routes x metric mtu cidr
279 metric="metric_${ifvar}"
280
281 ifconfig_exists "${iface}" || return 0
282
283 # Make sure interface is marked UP
284 ifconfig_up "${iface}"
285
286 # MTU support
287 mtu="mtu_${ifvar}"
288 [[ -n ${!mtu} ]] && ifconfig "${iface}" mtu "${!mtu}"
289
290 routes="routes_${ifvar}[@]"
291 routes=( "${!routes}" )
292
293 # BACKWARD COMPATIBILITY: set the default gateway
294 if [[ ${gateway} == "${iface}/"* ]]; then
295 # We don't add the old gateway if one has been set in routes_IFACE
296 local gw=true
297 for x in "${routes[@]}"; do
298 [[ ${x} != *"default gw"* ]] && continue
299 gw=false
300 break
301 done
302 ${gw} && routes=( "${routes[@]}" "default gw ${gateway#*/}" )
303 fi
304
305 [[ -z ${routes} ]] && return 0
306
307 # Add routes for this interface, might even include default gw
308 einfo "Adding routes"
309 eindent
310 for x in "${routes[@]}"; do
311 ebegin "${x}"
312
313 # Support iproute2 style routes
314 x="${x//via/gw} "
315 x="${x//scope * / }"
316
317 # Assume we're a net device unless told otherwise
318 [[ " ${x} " != *" -net "* && " ${x} " != *" -host "* ]] && x="-net ${x}"
319
320 # Support adding IPv6 addresses easily
321 if [[ ${x} == *:* ]]; then
322 [[ ${x} != *"-A inet6"* ]] && x="-A inet6 ${x}"
323 x="${x// -net / }"
324 fi
325
326 # Add a metric if we don't have one
327 [[ ${x} != *" metric "* ]] && x="${x} metric ${!metric}"
328
329 route add ${x} dev "${iface}"
330 eend $?
331 done
332 eoutdent
333
334 return 0
335 }
336
337 # bool ifconfig_add_address(char *iface, char *options ...)
338 #
339 # Adds the given address to the interface
340 ifconfig_add_address() {
341 local iface="$1" i=0 r e real_iface=$(interface_device "$1")
342
343 ifconfig_exists "${real_iface}" true || return 1
344
345 # Extract the config
346 local -a config=( "$@" )
347 config=( ${config[@]:1} )
348
349 if [[ ${config[0]} == *:* ]]; then
350 # Support IPv6 - nice and simple
351 config[0]="inet6 add ${config[0]}"
352 else
353 # IPv4 is tricky - ifconfig requires an aliased device
354 # for multiple addresses
355 if ifconfig "${iface}" | grep -Eq "\<inet addr:.*" ; then
356 # Get the last alias made for the interface and add 1 to it
357 i=$( ifconfig | tac | grep -m 1 -o "^${iface}:[0-9]*" \
358 | sed -n -e 's/'"${iface}"'://p' )
359 i="${i:-0}"
360 (( i++ ))
361 iface="${iface}:${i}"
362 fi
363
364 # ifconfig doesn't like CIDR addresses
365 local ip="${config[0]%%/*}" cidr="${config[0]##*/}" netmask
366 if [[ -n ${cidr} && ${cidr} != "${ip}" ]]; then
367 netmask=$( cidr2netmask "${cidr}" )
368 config[0]="${ip} netmask ${netmask}"
369 fi
370
371 # Support iproute2 style config where possible
372 r="${config[@]}"
373 config=( ${r//brd +/} )
374 config=( "${config[@]//brd/broadcast}" )
375 config=( "${config[@]//peer/pointopoint}" )
376 fi
377
378 # Ensure that the interface is up so we can add IPv6 addresses
379 interface_up "${real_iface}"
380
381 # Some kernels like to apply lo with an address when they are brought up
382 if [[ ${iface} == "lo" && ${config[@]} == "127.0.0.1 netmask 255.0.0.0 broadcast 127.255.255.255" ]]; then
383 ifconfig "${iface}" 0.0.0.0
384 fi
385
386 ifconfig "${iface}" ${config[@]}
387 r="$?"
388 [[ ${r} != "0" ]] && return ${r}
389
390 local metric ifvar=$(bash_variable "${real_iface}")
391 # Remove the newly added route and replace with our metric
392 metric="metric_${ifvar}"
393 [[ ${!metric:-0} == "0" ]] && return ${r}
394
395 if [[ -z ${netmask} ]]; then
396 for (( i=1; i<${#config[@]}-1; i++ )); do
397 if [[ ${config[i]} == "netmask" ]]; then
398 netmask="${config[i+1]}"
399 cidr=$( netmask2cidr "${netmask}" )
400 break
401 fi
402 done
403 [[ -z ${netmask} ]] && return ${r}
404 fi
405
406 local network=$( ip_network "${ip}" "${netmask}" )
407
408 if route del -net "${network}/${cidr}" metric 0 dev "${iface}" \
409 2>/dev/null ; then
410 route add -net "${network}/${cidr}" metric "${!metric:-0}" dev "${iface}"
411 fi
412
413 return ${r}
414 }
415
416 # void ifconfig_route_metric(char* interface, int metric)
417 #
418 # Change all routes for an interface to a given metric
419 ifconfig_route_metric() {
420 local dest gateway mask flags metric ref use
421 route -n | grep " $1$" | {
422 while read dest gateway mask flags metric ref use ; do
423 if [[ ${gateway} != "0.0.0.0" ]]; then
424 gateway="gw ${gateway}"
425 else
426 unset gateway
427 fi
428 route del ${dest}
429 route add -net ${dest} netmask ${mask} ${gateway} metric "$2" dev "$1"
430 done
431 }
432 }
433
434 # void ifconfig_default_route(char* interface, char* gateway_ip, int metric)
435 #
436 # Force default route to the specified gateway
437 ifconfig_default_route() {
438 local metric="${3:-0}"
439
440 # Delete any existing default routes
441 while true ; do
442 route del default metric "${metric}" dev "$1" 2>/dev/null || break
443 done
444
445 # Then we add our route
446 route add default gw "$2" metric "${metric}" dev "$1"
447 }
448
449 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20