/[baselayout]/branches/bsd-porting/net-scripts/net.modules.d/ifconfig
Gentoo

Contents of /branches/bsd-porting/net-scripts/net.modules.d/ifconfig

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1457 - (show annotations) (download)
Thu Sep 1 06:45:25 2005 UTC (9 years ago) by uberlord
File size: 11563 byte(s)
Fixed ifconfig bringing lo up and then working out aliases, bug #104415
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_post_start(char *iface)
260 #
261 # Bring up iface using ifconfig utilities, called from iface_start
262 #
263 # Returns 0 (true) when successful on the primary interface, non-zero
264 # (false) when the primary interface fails. Aliases are allowed to
265 # fail, the routine should still return success to indicate that
266 # net.eth0 was successful
267 ifconfig_post_start() {
268 local iface="$1" ifvar=$( bash_variable "$1" ) routes x metric mtu cidr
269 eval metric=\"\$\{metric_${ifvar}\}\"
270
271 # Make sure interface is marked UP
272 ifconfig_up "${iface}"
273
274 # MTU support
275 eval mtu=\"\$\{mtu_${ifvar}\}\"
276 [[ -n ${mtu} ]] && ifconfig "${iface}" mtu "${mtu}"
277
278 eval routes=( \"\$\{routes_${ifvar}\[@\]\}\" )
279
280 # BACKWARD COMPATIBILITY: set the default gateway
281 if [[ ${gateway} == "${iface}/"* ]]; then
282 # We don't add the old gateway if one has been set in routes_IFACE
283 local gw=true
284 for x in "${routes[@]}"; do
285 [[ ${x} != *"default gw"* ]] && continue
286 gw=false
287 break
288 done
289 ${gw} && routes=( "${routes[@]}" "default gw ${gateway#*/}" )
290 fi
291
292 [[ -z ${routes} ]] && return 0
293
294 # Add routes for this interface, might even include default gw
295 einfo "Adding routes"
296 eindent
297 for x in "${routes[@]}"; do
298 ebegin "${x}"
299
300 # Support iproute2 style routes
301 x="${x//via/gw} "
302 x="${x//scope * / }"
303
304 # Assume we're a net device unless told otherwise
305 [[ " ${x} " != *" -net "* && " ${x} " != *" -host "* ]] && x="-net ${x}"
306
307 # Support adding IPv6 addresses easily
308 [[ ${x} == *:* && ${x} != *"-A inet6"* ]] && x="-A inet6 ${x}"
309
310 # Add a metric if we don't have one
311 [[ ${x} != *" metric "* ]] && x="${x} metric ${metric}"
312
313 route add ${x} dev "${iface}"
314 eend $?
315 done
316 eoutdent
317
318 return 0
319 }
320
321 # bool ifconfig_add_address(char *iface, char *options ...)
322 #
323 # Adds the given address to the interface
324 ifconfig_add_address() {
325 local iface="$1" i=0 r e
326
327 ifconfig_exists "${iface}" true || return 1
328
329 # Extract the config
330 local -a config=( "$@" )
331 config=( ${config[@]:1} )
332
333 if [[ ${config[0]} == *:* ]]; then
334 # Support IPv6 - nice and simple
335 config[0]="inet6 add ${config[0]}"
336 else
337 # IPv4 is tricky - ifconfig requires an aliased device
338 # for multiple addresses
339 if ifconfig "${iface}" | grep -Eq "\<inet addr:.*" ; then
340 # Get the last alias made for the interface and add 1 to it
341 i=$( ifconfig | tac | grep -m 1 -o "^${iface}:[0-9]*" \
342 | sed -n -e 's/'"${iface}"'://p' )
343 i="${i:-0}"
344 (( i++ ))
345 iface="${iface}:${i}"
346 fi
347
348 # ifconfig doesn't like CIDR addresses
349 local ip="${config[0]%%/*}" cidr="${config[0]##*/}" netmask
350 if [[ -n ${cidr} && ${cidr} != "${ip}" ]]; then
351 netmask=$( cidr2netmask "${cidr}" )
352 config[0]="${ip} netmask ${netmask}"
353 fi
354
355 # Support iproute2 style config where possible
356 r="${config[@]}"
357 config=( ${r//brd +/} )
358 config=( "${config[@]//brd/broadcast}" )
359 config=( "${config[@]//peer/pointtopoint}" )
360 fi
361
362 # Ensure that the interface is up so we can add IPv6 addresses
363 interface_up "${iface}"
364
365 # Some kernels like to apply lo with an address when they are brought up
366 if [[ ${iface} == "lo" && ${config[@]} == "127.0.0.1 netmask 255.0.0.0 broadcast 127.255.255.255" ]]; then
367 ifconfig "${iface}" 0.0.0.0
368 fi
369
370 ifconfig "${iface}" ${config[@]}
371 r="$?"
372 [[ ${r} != "0" ]] && return ${r}
373
374 local metric ifvar=$( bash_variable "${iface}" )
375 # Remove the newly added route and replace with our metric
376 eval metric=\"\$\{metric_${ifvar}\}\"
377 [[ ${metric} == "0" || ${RC_AUTO_INTERFACE} != "yes" ]] && return ${r}
378
379 if [[ -z ${netmask} ]]; then
380 for (( i=1; i<${#config[@]}-1; i++ )); do
381 if [[ ${config[i]} == "netmask" ]]; then
382 netmask="${config[i+1]}"
383 cidr=$( netmask2cidr "${netmask}" )
384 break
385 fi
386 done
387 [[ -z ${netmask} ]] && return ${r}
388 fi
389
390 local network=$( ip_network "${ip}" "${netmask}" )
391
392 if route del -net "${network}/${cidr}" metric 0 dev "${iface}" \
393 2>/dev/null ; then
394 route add -net "${network}/${cidr}" metric "${metric}" dev "${iface}"
395 fi
396
397 return ${r}
398 }
399
400 # void ifconfig_default_route(char* interface, char* gateway_ip, int metric)
401 #
402 # Force default route to the specified gateway
403 ifconfig_default_route() {
404 local metric="${3:-0}"
405
406 # Delete any existing default routes
407 while true ; do
408 route del default metric "${metric}" dev "$1" 2>/dev/null || break
409 done
410
411 # Then we add our route
412 route add default gw "$2" metric "${metric}" dev "$1"
413 }
414
415 # vim:ts=4

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20