/[baselayout]/trunk/net-scripts/net.modules.d/iproute2
Gentoo

Contents of /trunk/net-scripts/net.modules.d/iproute2

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1247 - (show annotations) (download)
Fri Jun 17 17:16:14 2005 UTC (13 years, 11 months ago) by uberlord
File size: 8736 byte(s)
style updates to all modules
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 ip() {
10 LC_ALL=C /sbin/ip "$@"
11 }
12
13 iproute2_tunnel() {
14 LC_ALL=C /sbin/ip tunnel "$@"
15 }
16
17 # void iproute2_depend(void)
18 #
19 # Sets up the dependancies for the module
20 iproute2_depend() {
21 after macnet wireless
22 }
23
24 # bool iproute2_check_installed(void)
25 #
26 # Returns 1 if iproute2 is installed, otherwise 0
27 iproute2_check_installed() {
28 local report="${1:-false}" installed=0
29 if [[ ! -x /sbin/ip ]]; then
30 installed=1
31 ${report} && eerror "For iproute2 support, emerge sys-apps/iproute2"
32 fi
33 if [[ ! -e /proc/net/netlink ]]; then
34 installed=1
35 ${report} && eerror "iproute2 requires NetLink enabled in the kernel"
36 fi
37 return ${installed}
38 }
39
40 # char* iproute2_provides(void)
41 #
42 # Returns a string to change module definition for starting up
43 iproute2_provides() {
44 echo "interface"
45 }
46
47 # char* iproute2_module(void)
48 #
49 # Returns the module name
50 # This is needed by dhclient as we run different scripts
51 # based on the interface
52 iproute2_module() {
53 echo "iproute2"
54 }
55
56 # bool iproute2_check_depends(void)
57 #
58 # Checks to see if we have the needed functions
59 iproute2_check_depends() {
60 local f
61
62 for f in interface_device; do
63 [[ $( type -t ${f} ) == "function" ]] && continue
64 eerror "iproute2: missing required function ${f}\n"
65 return 1
66 done
67
68 return 0
69 }
70
71 # bool iproute2_exists(char *interface, bool report)
72 #
73 # Returns 1 if the interface exists, otherwise 0
74 iproute2_exists() {
75 local e=$( ip addr show label "$1" ) report="${2:-false}"
76 [[ -n ${e} ]] && return 0
77
78 ${report} && eerror "$1 does not exist"
79 return 1
80 }
81
82 # void iproute2_up(char *interface)
83 #
84 # provides a generic interface for bringing interfaces up
85 iproute2_up() {
86 ip link set up dev "$1"
87 }
88
89 # void iproute2_down(char *interface)
90 #
91 # provides a generic interface for bringing interfaces up
92 iproute2_down() {
93 ip link set down dev "$1"
94 }
95
96 # bool ifproute2_is_up(char *iface, bool withaddress)
97 #
98 # Returns 0 if the interface is up, otherwise 1
99 # If withaddress is true then the interface has to have an IPv4 address
100 # assigned as well
101 iproute2_is_up() {
102 local check="\<UP\>" addr="${2:-false}"
103 ${addr} && check="${check}.*inet "
104 ip addr show "$1" | grep -Eq "${check}" && return 0
105 return 1
106 }
107
108 # void iproute2_set_flag(char *iface, char *flag, bool enabled)
109 #
110 # Sets or disables the interface flag
111 iproute2_set_flag() {
112 local enable="$3" opt="on"
113 ${enable} || opt="off"
114 ip link set "$1" "$2" "${opt}"
115 }
116
117 # void iproute2_get_address(char *interface)
118 #
119 # Fetch the address retrieved by DHCP. If successful, echoes the
120 # address on stdout, otherwise echoes nothing.
121 iproute2_get_address() {
122 ip -family inet addr show "$1" \
123 | sed -n -e 's/.*inet \([^ ]*\).*/\1/p'
124 }
125
126 # void iproute2_get_mac_address(char *interface)
127 #
128 # Fetch the mac address assingned to the network card
129 iproute2_get_mac_address() {
130 ip link show "$1" \
131 | sed -n -e 's/^.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p'
132 }
133
134 # void iproute2_set_mac_address(char *interface, char *mac)
135 #
136 # Assigned the mac address to the network card
137 iproute2_set_mac_address() {
138 ip link set address "$2" dev "$1"
139 }
140
141 # void iproute2_get_aliases_rev(char *interface)
142 #
143 # Fetch the list of aliases for an interface.
144 # Outputs a space-separated list on stdout, in reverse order, for
145 # example "eth0:2 eth0:1"
146 iproute2_get_aliases_rev() {
147 local iface=$( interface_device "$1" )
148 ip addr show dev "${iface}" | grep -o "${iface}:[0-9].*" | tac
149 }
150
151 # bool iproute2_del_addresses(char *interface, bool report)
152 #
153 # Remove addresses from interface.
154 iproute2_del_addresses() {
155 ip addr flush label "$1" scope global &>/dev/null
156 ip addr flush label "$1" scope host &>/dev/null
157 return 0
158 }
159
160 # char* iproute2_get_vars(char *interface)
161 #
162 # Returns a string spaced with possible user set
163 # configuration variables
164 iproute2_get_vars() {
165 echo "config_$1 routes_$1 fallback_$1 metric_$1 ipaddr_$1 ipaddr_fallback_$1 iproute_$1 inet6_$1"
166 }
167
168 # bool iproute2_get_old_config(char *iface)
169 #
170 # Returns config and config_fallback for the given interface
171 iproute2_get_old_config() {
172 local ifvar=$( bash_variable "$1" ) inet6
173
174 # iproute2-style config vars
175 eval config=( \"\$\{ipaddr_${ifvar}\[@\]\}\" )
176 eval config_fallback=( \"\$\{ipaddr_fallback_${ifvar}\[@\]\}\" )
177 eval inet6=( \"\$\{inet6_${ifvar}\[@\]\}\" )
178
179 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
180 [[ ${#inet6[@]} == 1 && ${inet6} == *' '* ]] && inet6=( ${inet6} )
181
182 # Add inet6 addresses to our config if required
183 [[ -n ${inet6} ]] && config=( "${config[@]}" "${inet6[@]}" )
184
185 return 0
186 }
187
188 # bool iproute2_iface_stop(char *interface)
189 #
190 # Do final shutdown for an interface or alias.
191 #
192 # Returns 0 (true) when successful, non-zero (false) on failure
193 iproute2_iface_stop() {
194 local label="$1" iface=$( interface_device "$1" )
195
196 # Shut down the link if this isn't an alias or vlan
197 if [[ ${label} == ${iface} ]]; then
198 iproute2_down "${iface}"
199 return $?
200 fi
201 return 0
202 }
203
204 # bool iproute2_add_address(char *interface, char *options ...)
205 #
206 # Adds an the specified address to the interface
207 # returns 0 on success and non-zero on failure
208 iproute2_add_address() {
209 local iface="$1" x
210
211 iproute2_exists "${iface}" true || return 1
212
213 # Extract the config
214 local -a config=( "$@" )
215 config=( ${config[@]:1} )
216
217 # Convert an ifconfig line to iproute2
218 local n=${#config[@]};
219 for (( x=0; x<n; x++ )); do
220 case ${config[x]} in
221 netmask)
222 config[0]="${config[0]}/$( netmask2cidr ${config[x+1]} )"
223 unset config[x] config[x+1]
224 ;;
225 mtu)
226 ip link set mtu "${config[x+1]}" dev "${iface}"
227 unset config[x] config[x+1]
228 ;;
229 esac
230 done
231 config=( "${config[@]//pointtopoint/peer}" )
232
233 # Always scope lo addresses as host unless specified otherwise
234 [[ ${iface} == "lo" && " ${config[@]} " != *' scope '* ]] \
235 && config=( "${config[@]}" "scope host" )
236
237 # Work out a broadcast if none supplied
238 [[ ${config[@]} != *' brd '* && ${config[@]} != *' broadcast '* ]] \
239 && config=( "${config[@]}" "brd +" )
240
241 # Make sure interface is marked UP
242 # This is required for IPv6 addresses
243 iproute2_up "${iface}"
244
245 # Some kernels like to apply lo with an address when they are brought up
246 local r
247 if [[ ${iface} == "lo" \
248 && ${config[@]} == "127.0.0.1/8 brd 127.255.255.255 scope host" ]]; then
249 ip addr add dev "${iface}" ${config[@]} 2>/dev/null
250 r=0
251 else
252 ip addr add dev "${iface}" ${config[@]}
253 r=$?
254 fi
255
256 local metric ifvar=$( bash_variable "${iface}" )
257 # Remove the newly added route and replace with our metric
258 eval metric=\"\$\{metric_${ifvar}\}\"
259 [[ ${metric} == 0 || ${RC_AUTO_INTERFACE} != "yes" ]] && return ${r}
260
261 local network=$( ip_network "${config[0]}" )
262 [[ -z ${network} ]] && return ${r}
263
264 local cidr="${config[0]##*/}"
265 #cidr="${cidr##*/}"
266
267 ip route del "${network}/${cidr}" dev "${iface}"
268 ip route add "${network}/${cidr}" metric "${metric}" dev "${iface}"
269
270 return ${r}
271 }
272
273 # bool iproute2_post_start(char *interface)
274 #
275 # Runs any post_start stuff on our interface and adds routes
276 # Always returns 0
277 iproute2_post_start() {
278 local iface="$1" ifvar=$( bash_variable "$1" ) routes e r metric m
279
280 # Make sure interface is marked UP
281 iproute2_up "${iface}"
282
283 eval routes=( \"\$\{routes_${ifvar}\[@\]\}\" )
284 eval metric=\"\$\{metric_${ifvar}\:-0}\"
285
286 # Test for old style ipaddr variable
287 if [[ -z ${routes} ]]; then
288 eval routes=( \"\$\{iproute_${ifvar}\[@\]\}\" )
289 fi
290
291 [[ -z ${routes} ]] && return 0
292
293 # Set routes with ip route -- this might also include default route
294 einfo "Adding routes"
295 eindent
296 for x in "${routes[@]}"; do
297 ebegin "${x}"
298
299 # Support net-tools routing too
300 x="${x//gw/via}"
301 x="${x//-A inet6}"
302 x="${x//-net}"
303 [[ ${x} == *'-host'* ]] && x="${x//-host} scope host"
304
305 # Add a metric if we don't have one
306 [[ ${x} != *' metric '* ]] && x="${x} metric ${metric}"
307
308 ip route append ${x} dev "${iface}"
309 eend $?
310 done
311 eoutdent
312
313 return 0
314 }
315
316 # void iproute2_default_route(char* interface, char* gateway_ip, int metric)
317 #
318 # Force default route to the specified gateway, optionally on
319 # the given interface
320 iproute2_default_route() {
321 local metric="${3:-0}"
322
323 ip route change default via "$2" metric "${metric}" dev "$1" 2>/dev/null \
324 || ip route append default via "$2" metric "${metric}" dev "$1" 2>/dev/null
325 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20