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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20