/[baselayout]/branches/baselayout-1_12/net-scripts/net/ifconfig.sh
Gentoo

Contents of /branches/baselayout-1_12/net-scripts/net/ifconfig.sh

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20