/[baselayout]/branches/rc-scripts-1_6/net-scripts/net.modules.d/ifconfig
Gentoo

Contents of /branches/rc-scripts-1_6/net-scripts/net.modules.d/ifconfig

Parent Directory Parent Directory | Revision Log Revision Log


Revision 860 - (show annotations) (download)
Mon Jan 17 15:40:32 2005 UTC (9 years, 7 months ago) by uberlord
File size: 9121 byte(s)
all modules should now check interface existance correctly - fixes #76385

1 # ifconfig module for net-scripts
2 # Version 1.0.5
3 # Copyright (c) 2004 Gentoo Foundation
4 # Distributed under the terms of the GNU General Public License V2
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 # void ifconfig_depend(void)
18 #
19 # Sets up the dependancies for the module
20 ifconfig_depend() {
21 after macchanger wireless
22 }
23
24 # bool ifconfig_check_installed(void)
25 #
26 # Returns 1 if ifconfig is installed, otherwise 0
27 ifconfig_check_installed() {
28 [[ -x /sbin/ifconfig ]] && return 0
29 ${1:-false} && eerror "For ifconfig support, emerge sys-apps/net-tools"
30 return 1
31 }
32
33 # char* ifconfig_provides(void)
34 #
35 # Returns a string to change module definition for starting up
36 ifconfig_provides() {
37 echo "interface"
38 }
39
40 # char* ifconfig_module(void)
41 #
42 # Returns the module name
43 # This is needed by dhclient as we run different scripts
44 # based on the interface
45 ifconfig_module() {
46 echo "ifconfig"
47 }
48
49 # bool ifconfig_check_depends(void)
50 #
51 # Checks to see if we have the needed functions
52 ifconfig_check_depends() {
53 local f
54
55 for f in interface_variable; do
56 [[ $( type -t ${f} ) == function ]] && continue
57 eerror "ifconfig: missing required function ${f}\n"
58 return 1
59 done
60
61 return 0
62 }
63
64 # bool ifconfig_exists(char *interface, bool report)
65 #
66 # Returns 1 if the interface exists, otherwise 0
67 ifconfig_exists() {
68 local e=$( ifconfig -a | grep -o "^${1}" ) report=${2:-false}
69 [[ -n ${e} ]] && return 0
70 ${report} && eerror "${1} does not exist"
71 return 1
72 }
73
74 # void ifconfig_up(char *iface)
75 #
76 # provides a generic interface for bringing interfaces up
77 ifconfig_up() {
78 ifconfig ${1} up &>${devnull}
79 }
80
81 # void ifconfig_down(char *iface)
82 #
83 # provides a generic interface for bringing interfaces down
84 ifconfig_down() {
85 ifconfig ${1} down &>${devnull}
86 }
87
88 # bool ifconfig_is_up(char *iface, bool withaddress)
89 #
90 # Returns 0 if the interface is up, otherwise 1
91 # If withaddress is true then the interface has to have an IPv4 address
92 # assigned as well
93 ifconfig_is_up() {
94 local check="\<UP\>" addr=${2:-false}
95 ${addr} && check="\<addr:.*${check}"
96 ifconfig ${1} 2>${devnull} | grep -v Scope | xargs | grep -Eq "${check}" && return 0
97 return 1
98 }
99
100 # void ifconfig_set_flag(char *iface, char *flag, bool enabled)
101 #
102 # Sets or disables the interface flag
103 ifconfig_set_flag() {
104 local iface=${1} flag=${2} enable=${3}
105 ${enable} || flag="-${flag}"
106 ifconfig ${iface} ${flag} &>${devnull}
107 }
108
109 # void ifconfig_loopback_create(void)
110 #
111 # Creates our loopback interface
112 ifconfig_loopback_create() {
113 ifconfig lo 127.0.0.1 up 2>/dev/null
114 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 \
115 gw 127.0.0.1 dev lo 2> /dev/null
116 }
117
118 # void ifconfig_get_address(char *interface)
119 #
120 # Fetch the address retrieved by DHCP. If successful, echoes the
121 # address on stdout, otherwise echoes nothing.
122 ifconfig_get_address() {
123 ifconfig ${1} | grep -m1 -o 'inet addr:[^ ]*' | cut -d: -f2
124 }
125
126 # void ifconfig_get_mac_address(char *interface)
127 #
128 # Fetch the mac address assingned to the network card
129 ifconfig_get_mac_address() {
130 ifconfig ${1} | grep -m1 -o 'HWaddr [^ ]*' | cut -d" " -f2
131 }
132
133 # void ifconfig_get_aliases_rev(char *interface)
134 #
135 # Fetch the list of aliases for an interface.
136 # Outputs a space-separated list on stdout, in reverse order, for
137 # example "eth0:2 eth0:1"
138 ifconfig_get_aliases_rev() {
139 ifconfig | grep -o "^${1}:[0-9]*" | tac | xargs
140 }
141
142 # bool ifconfig_interface_del_addresses(char *interface)
143 #
144 # Remove addresses from interface. Returns 0 (true) if there
145 # were addresses to remove (whether successful or not). Returns 1
146 # (false) if there were no addresses to remove.
147 ifconfig_del_addresses() {
148 local iface=${1} i
149
150 # We don't remove addresses from aliases
151 [[ ${iface} == *:* ]] && return 0
152
153 # iproute2 can add many addresses to an iface unlike ifconfig ...
154 # iproute2 added addresses cause problems for ifconfig
155 # as we delete an address, a new one appears, so we have to
156 # keep polling
157 while ifconfig ${iface} | grep -q -m1 -o 'inet addr:[^ ]*' ; do
158 ifconfig ${iface} 0.0.0.0 || break
159 done
160
161 # Remove IPv6 addresses
162 for i in "$(ifconfig ${iface} | awk '$1=="inet6" && $4!="Scope:Link" {print $3}')"; do
163 /sbin/ifconfig ${interface} inet6 del ${i} &>/dev/null
164 done
165
166 return 0
167 }
168
169 # char* ifconfig_get_vars(char *interface)
170 #
171 # Returns a string spaced with possible user set
172 # configuration variables
173 ifconfig_get_vars() {
174 echo "config_${1} routes_${1} fallback_${1} ifconfig_${1} ifconfig_fallback_${1} routes_${1} inet6_${1} iface_${1} alias_${1} broadcast_${1} netmask_${1}"
175 # The depreciated gateway var has to be handled by
176 # each module if needed
177 }
178
179 # bool ifconfig_get_old_config(char *iface)
180 #
181 # Returns config and config_fallback for the given interface
182 ifconfig_get_old_config() {
183 local iface=${1} ifvar=$( interface_variable ${1} ) i inet6
184
185 eval config=( \"\$\{ifconfig_${ifvar}\[@\]\}\" )
186 eval config_fallback=( \"\$\{ifconfig_fallback_${ifvar}\[@\]\}\" )
187 eval inet6=( \"\$\{inet6_${ifvar}\[@\]\}\" )
188
189 # BACKWARD COMPATIBILITY: populate the config_IFACE array
190 # if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0)
191 eval local i=\"\$\{iface_${ifvar}\}\"
192 if [[ -n ${i} && -z ${config} ]]; then
193 # Make sure these get evaluated as arrays
194 local -a aliases broadcasts netmasks
195
196 # Start with the primary interface
197 config=( "${i}" )
198
199 # ..then add aliases
200 eval aliases=( \$\{alias_${ifvar}\} )
201 eval broadcasts=( \$\{broadcast_${ifvar}\} )
202 eval netmasks=( \$\{netmask_${ifvar}\} )
203 for (( i=0; i<${#aliases[@]}; i++ )); do
204 config[i+1]="${aliases[i]} ${broadcasts[i]:+broadcast ${broadcasts[i]}} ${netmasks[i]:+netmask ${netmasks[i]}}"
205 done
206 fi
207
208 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
209 [[ ${#inet6[@]} == 1 && ${inet6} == *' '* ]] && inet6=( ${inet6} )
210
211 # Add inet6 addresses to our config if required
212 [[ -n ${inet6} ]] && config=( "${config[@]}" "${inet6[@]}" )
213
214 return 0
215 }
216
217 # bool ifconfig_iface_stop(char *interface)
218 #
219 # Do final shutdown for an interface or alias.
220 #
221 # Returns 0 (true) when successful, non-zero (false) on failure
222 ifconfig_iface_stop() {
223 # If an alias is already down, then "ifconfig eth0:1 down"
224 # will try to bring it up with an address of "down" which
225 # fails. Do some double-checking before returning error
226 # status
227 ifconfig_is_up $1 || return 0
228 ifconfig_down ${1} && return 0
229
230 # It is sometimes impossible to transition an alias from the
231 # UP state... particularly if the alias has no address. So
232 # ignore the failure, which should be okay since the entire
233 # interface will be shut down eventually.
234 [[ ${1} == *:* ]] && return 0
235 return 1
236 }
237
238 # bool ifconfig_post_start(char *iface)
239 #
240 # Bring up iface using ifconfig utilities, called from iface_start
241 #
242 # Returns 0 (true) when successful on the primary interface, non-zero
243 # (false) when the primary interface fails. Aliases are allowed to
244 # fail, the routine should still return success to indicate that
245 # net.eth0 was successful
246 ifconfig_post_start() {
247 local iface=${1} ifvar=$( interface_variable ${1} ) routes x
248
249 # Make sure interface is marked UP
250 ifconfig_up ${iface}
251
252 eval routes=( \"\$\{routes_${ifvar}\[@\]\}\" )
253
254 # BACKWARD COMPATIBILITY: set the default gateway
255 if [[ ${gateway} == ${iface}/* ]]; then
256 # We don't add the old gateway if one has been set in routes_IFACE
257 local gw=true
258 for x in "${routes[@]}"; do
259 [[ ${x} != "*default gw*" ]] && continue
260 gw=false
261 break
262 done
263 ${gw} && routes=( "${routes[@]}" "default gw ${gateway#*/}" )
264 fi
265
266 [[ -z ${routes} ]] && return 0
267
268 # Add routes for this interface, might even include default gw
269 einfo "Adding routes"
270 eindent
271 for x in "${routes[@]}"; do
272 # Support iproute2 style routes
273 x=${x//via/gw}
274
275 ebegin "${x}"
276 # Support adding IPv6 addresses easily
277 if [[ ${x} == *:* && ${x} != *'-A inet6'* ]]; then
278 /sbin/route add -A inet6 ${x} &>${devnull}
279 else
280 /sbin/route add ${x} &>${devnull}
281 fi
282 eend $?
283 done
284 eoutdent
285
286 return 0
287 }
288
289 # bool ifconfig_add_address(char *iface, char *options ...)
290 #
291 # Adds the given address to the interface
292 ifconfig_add_address() {
293 local iface=${1} i=0
294
295 ifconfig_exists ${iface} true || return 1
296
297 # Extract the config
298 local -a config=( "$@" )
299 config=( ${config[@]:1} )
300
301 if [[ ${config[0]} == *:* ]]; then
302 # Support IPv6 - nice and simple
303 config[0]="inet6 add ${config[0]}"
304 else
305 # IPv4 is tricky - ifconfig requires an aliased device
306 # for multiple addresses
307 if ifconfig ${iface} | grep -v Scope | xargs | grep -Eq "\<addr:.*" ; then
308 # Get the last alias made for the interface and add 1 to it
309 i=$( ifconfig | tac | grep -m 1 -o "^${iface}:[0-9]*" | awk -F: '{ print $2 }' )
310 i=${i:-0}
311 (( i++ ))
312 iface=${iface}:${i}
313 fi
314
315 # Support iproute2 style config where possible
316 config=( "${config[@]//brd/broadcast}" )
317 config=( "${config[@]//peer/pointtopoint}" )
318 fi
319
320 ewarn "ifconfig ${iface} ${config[@]}"
321 ifconfig ${iface} ${config[@]} &>${devnull}
322 eend $?
323 return $?
324 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20