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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 414 - (show annotations) (download) (as text)
Wed Aug 16 08:23:44 2006 UTC (8 years, 4 months ago) by hollow
File MIME type: text/x-sh
File size: 11599 byte(s)
merge r2190
1 # Copyright (c) 2004-2006 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # Contributed by Roy Marples (uberlord@gentoo.org)
4
5 # Fix any potential localisation problems
6 # Note that LC_ALL trumps LC_anything_else according to locale(7)
7 ifconfig() {
8 LC_ALL=C /sbin/ifconfig "$@"
9 }
10
11 ifconfig_tunnel() {
12 LC_ALL=C /sbin/iptunnel "$@"
13 }
14
15 route() {
16 LC_ALL=C /sbin/route "$@"
17 }
18
19 # void ifconfig_depend(void)
20 #
21 # Sets up the dependancies for the module
22 ifconfig_depend() {
23 provide interface
24 }
25
26 # void ifconfig_expose(void)
27 #
28 # Expose variables that can be configured
29 ifconfig_expose() {
30 variables config routes fallback metric ifconfig \
31 ifconfig_fallback routes inet6 iface alias broadcast netmask
32 }
33
34 # bool ifconfig_check_installed(void)
35 #
36 # Returns 1 if ifconfig is installed, otherwise 0
37 ifconfig_check_installed() {
38 [[ -x /sbin/ifconfig ]] && return 0
39 ${1:-false} && eerror "For ifconfig support, emerge sys-apps/net-tools"
40 return 1
41 }
42
43 # bool ifconfig_exists(char *interface, bool report)
44 #
45 # Returns 1 if the interface exists, otherwise 0
46 ifconfig_exists() {
47 local e=$(ifconfig -a | grep -o "^$1") report="${2:-false}"
48 [[ -n ${e} ]] && return 0
49
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 return 1
56 }
57
58 # 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 local octets= frac=
64
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 # void ifconfig_up(char *iface)
91 #
92 # provides a generic interface for bringing interfaces up
93 ifconfig_up() {
94 ifconfig "$1" up
95 }
96
97 # void ifconfig_down(char *iface)
98 #
99 # provides a generic interface for bringing interfaces down
100 ifconfig_down() {
101 ifconfig "$1" down
102 }
103
104 # bool ifconfig_is_up(char *iface, bool withaddress)
105 #
106 # Returns 0 if the interface is up, otherwise 1
107 # If withaddress is true then the interface has to have an IPv4 address
108 # assigned as well
109 ifconfig_is_up() {
110 local check="\<UP\>" addr="${2:-false}"
111 ${addr} && check="\<inet addr:.*${check}"
112 ifconfig "$1" | tr '\n' ' ' | grep -Eq "${check}" && return 0
113 return 1
114 }
115
116 # void ifconfig_set_flag(char *iface, char *flag, bool enabled)
117 #
118 # Sets or disables the interface flag
119 ifconfig_set_flag() {
120 local iface="$1" flag="$2" enable="$3"
121 ${enable} || flag="-${flag}"
122 ifconfig "${iface}" "${flag}"
123 }
124
125 # 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 local -a x=( $( ifconfig "$1" \
131 | sed -n -e 's/.*inet addr:\([^ ]*\).*Mask:\([^ ]*\).*/\1 \2/p' ) )
132 x[1]=$(netmask2cidr "${x[1]}")
133 [[ -n ${x[0]} ]] && echo "${x[0]}/${x[1]}"
134 }
135
136 # 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 # void ifconfig_get_mac_address(char *interface)
144 #
145 # Fetch the mac address assingned to the network card
146 ifconfig_get_mac_address() {
147 local mac=$(ifconfig "$1" | sed -n -e \
148 's/.*HWaddr[ \t]*\<\(..:..:..:..:..:..\)\>.*/\U\1/p')
149 [[ ${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 }
154
155 # 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 ifconfig "$1" hw ether "$2"
160 }
161
162 # 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 [[ -z $2 ]] && return 1
168 local current="$1" new="$2"
169
170 local mac=$(ifconfig_get_mac_address "${current}")
171 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 }
178
179 # 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 ifconfig | grep -o "^$1:[0-9]* " | tac
186 }
187
188 # bool ifconfig_del_addresses(char *interface, bool onlyinet)
189 #
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 # If onlyinet is true then we only delete IPv4 / inet addresses
194 ifconfig_del_addresses() {
195 local iface="$1" i= onlyinet="${2:-false}"
196 # We don't remove addresses from aliases
197 [[ ${iface} == *:* ]] && return 0
198
199 # If the interface doesn't exist, don't try and delete
200 ifconfig_exists "${iface}" || return 0
201
202 # 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
210 # 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 }
219
220 # bool ifconfig_get_old_config(char *iface)
221 #
222 # Returns config and config_fallback for the given interface
223 ifconfig_get_old_config() {
224 local iface="$1" ifvar=$(bash_variable "$1") i= inet6=
225
226 config="ifconfig_${ifvar}[@]"
227 config=( "${!config}" )
228 config_fallback="ifconfig_fallback_${ifvar}[@]"
229 config_fallback=( "${!config_fallback}" )
230 inet6="inet6_${ifvar}[@]"
231 inet6=( "${!inet6}" )
232
233 # BACKWARD COMPATIBILITY: populate the config_IFACE array
234 # if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0)
235 i="iface_${ifvar}"
236 if [[ -n ${!i} && -z ${config} ]]; then
237 # Make sure these get evaluated as arrays
238 local -a aliases=() broadcasts=() netmasks=()
239
240 # Start with the primary interface
241 config=( "${!i}" )
242
243 # ..then add aliases
244 aliases="alias_${ifvars}"
245 aliases=( ${!aliases} )
246 broadcasts="broadcast_${ifvar}"
247 broadcasts=( ${!broadcasts} )
248 netmasks="netmask_${ifvar}"
249 netmasks=( ${!netmasks} )
250 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
255 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
256 [[ ${#inet6[@]} == 1 && ${inet6} == *' '* ]] && inet6=( ${inet6} )
257
258 # Add inet6 addresses to our config if required
259 [[ -n ${inet6} ]] && config=( "${config[@]}" "${inet6[@]}" )
260
261 # 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 return 0
280 }
281
282 # bool ifconfig_iface_stop(char *interface)
283 #
284 # Do final shutdown for an interface or alias.
285 #
286 # Returns 0 (true) when successful, non-zero (false) on failure
287 ifconfig_iface_stop() {
288 # 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
295 # 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 }
302
303 # 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 local ifvar=$(bash_variable "$1") mtu=
314
315 # MTU support
316 mtu="mtu_${ifvar}"
317 [[ -n ${!mtu} ]] && ifconfig "${iface}" mtu "${!mtu}"
318
319 return 0
320 }
321
322
323 # bool ifconfig_post_start(char *iface)
324 #
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 local iface="$1" ifvar=$(bash_variable "$1") x= metric= mtu= cidr=
333 local -a routes=()
334 metric="metric_${ifvar}"
335
336 ifconfig_exists "${iface}" || return 0
337
338 # Make sure interface is marked UP
339 ifconfig_up "${iface}"
340
341 # MTU support
342 mtu="mtu_${ifvar}"
343 [[ -n ${!mtu} ]] && ifconfig "${iface}" mtu "${!mtu}"
344
345 x="routes_${ifvar}[@]"
346 routes=( "${!x}" )
347
348 [[ -z ${routes} ]] && return 0
349
350 # 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
356 # Support iproute2 style routes
357 x="${x//via/gw} "
358 x="${x//scope * / }"
359
360 # Assume we're a net device unless told otherwise
361 [[ " ${x} " != *" -net "* && " ${x} " != *" -host "* ]] && x="-net ${x}"
362
363 # Support adding IPv6 addresses easily
364 if [[ ${x} == *:* ]]; then
365 [[ ${x} != *"-A inet6"* ]] && x="-A inet6 ${x}"
366 x="${x// -net / }"
367 fi
368
369 # Add a metric if we don't have one
370 [[ ${x} != *" metric "* ]] && x="${x} metric ${!metric}"
371
372 route add ${x} dev "${iface}"
373 eend $?
374 done
375 eoutdent
376
377 return 0
378 }
379
380 # bool ifconfig_add_address(char *iface, char *options ...)
381 #
382 # Adds the given address to the interface
383 ifconfig_add_address() {
384 local iface="$1" i=0 r= e= real_iface=$(interface_device "$1")
385
386 ifconfig_exists "${real_iface}" true || return 1
387
388 # Extract the config
389 local -a config=( "$@" )
390 config=( ${config[@]:1} )
391
392 if [[ ${config[0]} == *:* ]]; then
393 # Support IPv6 - nice and simple
394 config[0]="inet6 add ${config[0]}"
395 else
396 # IPv4 is tricky - ifconfig requires an aliased device
397 # for multiple addresses
398 if ifconfig "${iface}" | grep -Eq "\<inet addr:.*" ; then
399 # Get the last alias made for the interface and add 1 to it
400 i=$(ifconfig | tac | grep -m 1 -o "^${iface}:[0-9]*" \
401 | sed -n -e 's/'"${iface}"'://p')
402 i="${i:-0}"
403 (( i++ ))
404 iface="${iface}:${i}"
405 fi
406
407 # ifconfig doesn't like CIDR addresses
408 local ip="${config[0]%%/*}" cidr="${config[0]##*/}" netmask=
409 if [[ -n ${cidr} && ${cidr} != "${ip}" ]]; then
410 netmask=$(cidr2netmask "${cidr}")
411 config[0]="${ip} netmask ${netmask}"
412 fi
413
414 # Support iproute2 style config where possible
415 r="${config[@]}"
416 config=( ${r//brd +/} )
417 config=( "${config[@]//brd/broadcast}" )
418 config=( "${config[@]//peer/pointopoint}" )
419 fi
420
421 # Ensure that the interface is up so we can add IPv6 addresses
422 interface_up "${real_iface}"
423
424 # Some kernels like to apply lo with an address when they are brought up
425 if [[ ${config[@]} == "127.0.0.1 netmask 255.0.0.0 broadcast 127.255.255.255" ]]; then
426 is_loopback "${iface}" && ifconfig "${iface}" 0.0.0.0
427 fi
428
429 ifconfig "${iface}" ${config[@]}
430 }
431
432 # vim: set ts=4 :

  ViewVC Help
Powered by ViewVC 1.1.20