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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20