/[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 1103 - (show annotations) (download)
Fri Apr 22 14:42:57 2005 UTC (14 years, 1 month ago) by uberlord
File size: 8086 byte(s)
Address network routes now get metrics applied as well
(ie a route automatically added when an address is added)

net.lo no longer errors when exlcuding modules who are installed

1 # Copyright (c) 2004-2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header$
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 macchanger 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]
224 unset config[x+1]
225 ;;
226 esac
227 done
228 config=( "${config[@]//pointtopoint/peer}" )
229
230 # Always scope lo addresses as host unless specified otherwise
231 [[ ${iface} == "lo" && " ${config[@]} " != *' scope '* ]] \
232 && config=( "${config[@]}" "scope host" )
233
234 # Make sure interface is marked UP
235 # This is required for IPv6 addresses
236 iproute2_up ${iface}
237
238 # Some kernels like to apply lo with an address when they are brought up
239 local r
240 if [[ ${iface} == "lo" && ${config[@]} == "127.0.0.1/8 brd 127.255.255.255 scope host" ]]; then
241 ip addr add dev ${iface} ${config[@]} 2>/dev/null
242 r=0
243 else
244 ip addr add dev ${iface} ${config[@]}
245 r=$?
246 fi
247
248 local metric ifvar=$( bash_variable ${iface} )
249 # Remove the newly added route and replace with our metric
250 eval metric=\"\$\{metric_${ifvar}\}\"
251 [[ ${metric} == 0 || ${auto_interface} != "yes" ]] && return ${r}
252
253 local network=$( ip_network ${config[0]} )
254 [[ -z ${network} ]] && return ${r}
255
256 local cidr=${config[0]}
257 cidr=${cidr##*/}
258
259 ip route del ${network}/${cidr} dev ${iface}
260 ip route add ${network}/${cidr} metric ${metric} dev ${iface}
261
262 return ${r}
263 }
264
265 # bool iproute2_post_start(char *interface)
266 #
267 # Runs any post_start stuff on our interface and adds routes
268 # Always returns 0
269 iproute2_post_start() {
270 local iface=$1 ifvar=$( bash_variable $1 ) routes e r metric m
271
272 # Make sure interface is marked UP
273 iproute2_up ${iface}
274
275 eval routes=( \"\$\{routes_${ifvar}\[@\]\}\" )
276 eval metric=\"\$\{metric_${ifvar}\:-0}\"
277
278 # Test for old style ipaddr variable
279 if [[ -z ${routes} ]]; then
280 eval routes=( \"\$\{iproute_${ifvar}\[@\]\}\" )
281 fi
282
283 [[ -z ${routes} ]] && return 0
284
285 # Set routes with ip route -- this might also include default route
286 einfo "Adding routes"
287 eindent
288 for x in "${routes[@]}"; do
289 ebegin "${x}"
290
291 # Support net-tools routing too
292 x=${x//gw/via}
293 x=${x//-A inet6}
294 x=${x//-net}
295 [[ ${x} == *'-host'* ]] && x="${x//-host} scope host"
296
297 # Add a metric if we don't have one
298 [[ ${x} != *' metric '* ]] && x="${x} metric ${metric}"
299
300 ip route append ${x} dev ${iface}
301 eend $?
302 done
303 eoutdent
304
305 return 0
306 }
307
308 # void iproute2_default_route(char* interface, char* gateway_ip, int metric)
309 #
310 # Force default route to the specified gateway, optionally on
311 # the given interface
312 iproute2_default_route() {
313 local metric=${3:-0}
314
315 ip route change default via $2 metric ${metric} dev $1 2>/dev/null \
316 || ip route append default via $2 metric ${metric} dev $1 2>/dev/null
317 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20