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

Diff of /baselayout-vserver/trunk/net-scripts/net.modules.d/iproute2

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 235 Revision 236
26# bool iproute2_check_installed(void) 26# bool iproute2_check_installed(void)
27# 27#
28# Returns 1 if iproute2 is installed, otherwise 0 28# Returns 1 if iproute2 is installed, otherwise 0
29iproute2_check_installed() { 29iproute2_check_installed() {
30 local report="${1:-false}" installed="0" 30 local report="${1:-false}" installed="0"
31 if [[ ! -x /sbin/ip ]]; then 31 if [[ ! -x /sbin/ip ]] ; then
32 installed="1" 32 installed="1"
33 ${report} && eerror "For iproute2 support, emerge sys-apps/iproute2" 33 ${report} && eerror "For iproute2 support, emerge sys-apps/iproute2"
34 fi 34 fi
35 if [[ ! -e /proc/net/netlink ]]; then 35 if [[ ! -e /proc/net/netlink ]] ; then
36 installed="1" 36 installed="1"
37 ${report} && eerror "iproute2 requires NetLink enabled in the kernel" 37 ${report} && eerror "iproute2 requires NetLink enabled in the kernel"
38 fi 38 fi
39 return "${installed}" 39 return "${installed}"
40} 40}
41 41
42# bool iproute2_exists(char *interface, bool report) 42# bool iproute2_exists(char *interface, bool report)
43# 43#
44# Returns 1 if the interface exists, otherwise 0 44# Returns 1 if the interface exists, otherwise 0
45iproute2_exists() { 45iproute2_exists() {
46 local e=$( ip addr show label "$1" ) report="${2:-false}" 46 local e="$( ip addr show label "$1" )" report="${2:-false}"
47 [[ -n ${e} ]] && return 0 47 [[ -n ${e} ]] && return 0
48 48
49 if ${report} ; then 49 if ${report} ; then
50 eerror "network interface $1 does not exist" 50 eerror "network interface $1 does not exist"
51 eerror "Please verify hardware or kernel module (driver)" 51 eerror "Please verify hardware or kernel module (driver)"
91# void iproute2_get_address(char *interface) 91# void iproute2_get_address(char *interface)
92# 92#
93# Fetch the address retrieved by DHCP. If successful, echoes the 93# Fetch the address retrieved by DHCP. If successful, echoes the
94# address on stdout, otherwise echoes nothing. 94# address on stdout, otherwise echoes nothing.
95iproute2_get_address() { 95iproute2_get_address() {
96 ip -family inet addr show "$1" \ 96 ip -family inet addr show "$1" | sed -n -e 's/.*inet \([^ ]*\).*/\1/p'
97 | sed -n -e 's/.*inet \([^ ]*\).*/\1/p'
98} 97}
99 98
100# bool iproute2_is_ethernet(char *interface) 99# bool iproute2_is_ethernet(char *interface)
101# 100#
102# Return 0 if the link is ethernet, otherwise 1. 101# Return 0 if the link is ethernet, otherwise 1.
106 105
107# void iproute2_get_mac_address(char *interface) 106# void iproute2_get_mac_address(char *interface)
108# 107#
109# Fetch the mac address assingned to the network card 108# Fetch the mac address assingned to the network card
110iproute2_get_mac_address() { 109iproute2_get_mac_address() {
111 local mac=$( ip link show "$1" | sed -n -e \ 110 local mac="$( ip link show "$1" | sed -n -e \
112 '/link\// s/^.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' ) 111 '/link\// s/^.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' )"
113 [[ ${mac} != '00:00:00:00:00:00' \ 112 [[ ${mac} != '00:00:00:00:00:00' \
114 && ${mac} != '44:44:44:44:44:44' \ 113 && ${mac} != '44:44:44:44:44:44' \
115 && ${mac} != 'FF:FF:FF:FF:FF:FF' ]] \ 114 && ${mac} != 'FF:FF:FF:FF:FF:FF' ]] \
116 && echo "${mac}" 115 && echo "${mac}"
117} 116}
135# 134#
136# Fetch the list of aliases for an interface. 135# Fetch the list of aliases for an interface.
137# Outputs a space-separated list on stdout, in reverse order, for 136# Outputs a space-separated list on stdout, in reverse order, for
138# example "eth0:2 eth0:1" 137# example "eth0:2 eth0:1"
139iproute2_get_aliases_rev() { 138iproute2_get_aliases_rev() {
140 local iface=$( interface_device "$1" ) 139 local iface="$( interface_device "$1" )"
141 ip addr show dev "${iface}" | grep -o "${iface}:[0-9].*" | tac 140 ip addr show dev "${iface}" | grep -o "${iface}:[0-9].*" | tac
142} 141}
143 142
144# bool iproute2_del_addresses(char *interface, bool onlyinet) 143# bool iproute2_del_addresses(char *interface, bool onlyinet)
145# 144#
156 155
157# bool iproute2_get_old_config(char *iface) 156# bool iproute2_get_old_config(char *iface)
158# 157#
159# Returns config and config_fallback for the given interface 158# Returns config and config_fallback for the given interface
160iproute2_get_old_config() { 159iproute2_get_old_config() {
161 local ifvar=$( bash_variable "$1" ) inet6 t 160 local ifvar="$( bash_variable "$1" )" inet6="" t=""
162 161
163 # iproute2-style config vars 162 # iproute2-style config vars
164 t="ipaddr_${ifvar}[@]" 163 t="ipaddr_${ifvar}[@]"
165 config=( "${!t}" ) 164 config=( "${!t}" )
166 t="config_fallback_${ifvar}[@]" 165 t="config_fallback_${ifvar}[@]"
188# 187#
189# Do final shutdown for an interface or alias. 188# Do final shutdown for an interface or alias.
190# 189#
191# Returns 0 (true) when successful, non-zero (false) on failure 190# Returns 0 (true) when successful, non-zero (false) on failure
192iproute2_iface_stop() { 191iproute2_iface_stop() {
193 local label="$1" iface=$( interface_device "$1" ) 192 local label="$1" iface="$( interface_device "$1" )"
194 193
195 # Shut down the link if this isn't an alias or vlan 194 # Shut down the link if this isn't an alias or vlan
196 if [[ ${label} == "${iface}" ]]; then 195 if [[ ${label} == "${iface}" ]] ; then
197 iproute2_down "${iface}" 196 iproute2_down "${iface}"
198 return $? 197 return $?
199 fi 198 fi
200 return 0 199 return 0
201} 200}
203# bool iproute2_add_address(char *interface, char *options ...) 202# bool iproute2_add_address(char *interface, char *options ...)
204# 203#
205# Adds an the specified address to the interface 204# Adds an the specified address to the interface
206# returns 0 on success and non-zero on failure 205# returns 0 on success and non-zero on failure
207iproute2_add_address() { 206iproute2_add_address() {
208 local iface="$1" x 207 local iface="$1" x=""
209 208
210 iproute2_exists "${iface}" true || return 1 209 iproute2_exists "${iface}" true || return 1
211 210
212 # Extract the config 211 # Extract the config
213 local -a config=( "$@" ) 212 local -a config=( "$@" )
216 # Convert an ifconfig line to iproute2 215 # Convert an ifconfig line to iproute2
217 local n="${#config[@]}" 216 local n="${#config[@]}"
218 for (( x=0; x<n; x++ )); do 217 for (( x=0; x<n; x++ )); do
219 case "${config[x]}" in 218 case "${config[x]}" in
220 netmask) 219 netmask)
221 config[0]="${config[0]}/$( netmask2cidr ${config[x+1]} )" 220 config[0]="${config[0]}/$( netmask2cidr "${config[x+1]}" )"
222 unset config[x] config[x+1] 221 unset config[x] config[x+1]
223 ;; 222 ;;
224 mtu) 223 mtu)
225 ip link set mtu "${config[x+1]}" dev "${iface}" 224 ip link set mtu "${config[x+1]}" dev "${iface}"
226 unset config[x] config[x+1] 225 unset config[x] config[x+1]
229 done 228 done
230 config=( "${config[@]//pointopoint/peer}" ) 229 config=( "${config[@]//pointopoint/peer}" )
231 230
232 # Always scope lo addresses as host unless specified otherwise 231 # Always scope lo addresses as host unless specified otherwise
233 [[ ${iface} == "lo" && " ${config[@]} " != *" scope "* ]] \ 232 [[ ${iface} == "lo" && " ${config[@]} " != *" scope "* ]] \
234 && config=( "${config[@]}" "scope host" ) 233 && config=( "${config[@]}" "scope host" )
235 234
236 # IPv4 specifics 235 # IPv4 specifics
237 if [[ ${config[@]} == *.*.*.* ]]; then 236 if [[ ${config[@]} == *.*.*.* ]] ; then
238 # Work out a broadcast if none supplied 237 # Work out a broadcast if none supplied
239 [[ ${config[@]} != *" brd "* && ${config[@]} != *" broadcast "* ]] \ 238 [[ ${config[@]} != *" brd "* && ${config[@]} != *" broadcast "* ]] \
240 && config=( "${config[@]}" "brd +" ) 239 && config=( "${config[@]}" "brd +" )
241 fi 240 fi
242 241
243 # Ensure that the interface is up so we can add IPv6 addresses 242 # Ensure that the interface is up so we can add IPv6 addresses
244 interface_up "${iface}" 243 interface_up "${iface}"
245 244
246 # Some kernels like to apply lo with an address when they are brought up 245 # Some kernels like to apply lo with an address when they are brought up
247 if [[ ${iface} == "lo" \ 246 if [[ ${iface} == "lo" \
248 && ${config[@]} == "127.0.0.1/8 brd 127.255.255.255 scope host" ]]; then 247 && ${config[@]} == "127.0.0.1/8 brd 127.255.255.255 scope host" ]] ; then
249 ip addr del dev "${iface}" 127.0.0.1/8 2>/dev/null 248 ip addr del dev "${iface}" 127.0.0.1/8 2>/dev/null
250 fi 249 fi
251 250
252 ip addr add dev "${iface}" ${config[@]} 251 ip addr add dev "${iface}" ${config[@]}
253 local r="$?" 252 local r="$?"
254 [[ ${r} != "0" ]] && return "${r}" 253 [[ ${r} != "0" ]] && return "${r}"
255 254
256 local metric ifvar=$( bash_variable "${iface}" ) 255 local ifvar="$( bash_variable "${iface}" )"
257 # Remove the newly added route and replace with our metric 256 # Remove the newly added route and replace with our metric
258 metric="metric_${ifvar}" 257 local metric="metric_${ifvar}"
259 [[ ${!metric} == "0" || ${RC_AUTO_INTERFACE} != "yes" ]] && return "${r}" 258 [[ ${!metric:-0} == "0" ]] && return "${r}"
260 259
261 local network=$( ip_network "${config[0]}" ) 260 local network="$( ip_network "${config[0]}" )"
262 [[ -z ${network} ]] && return "${r}" 261 [[ -z ${network} ]] && return "${r}"
263 262
264 local cidr="${config[0]##*/}" 263 local cidr="${config[0]##*/}"
265 if ip route del "${network}/${cidr}" metric 0 dev "${iface}" \ 264 if ip route del "${network}/${cidr}" metric 0 dev "${iface}" \
266 2>/dev/null ; then 265 2>/dev/null ; then
267 ip route add "${network}/${cidr}" metric "${!metric}" dev "${iface}" 266 ip route add "${network}/${cidr}" metric "${!metric:-0}" dev "${iface}"
268 fi 267 fi
269 268
270 return "${r}" 269 return "${r}"
271} 270}
272 271
278iproute2_pre_start() { 277iproute2_pre_start() {
279 local iface="$1" 278 local iface="$1"
280 279
281 interface_exists "${iface}" || return 0 280 interface_exists "${iface}" || return 0
282 281
283 local ifvar=$( bash_variable "$1" ) mtu 282 local ifvar="$( bash_variable "$1" )"
284 283
285 # MTU support 284 # MTU support
286 mtu="mtu_${ifvar}" 285 local mtu="mtu_${ifvar}"
287 [[ -n ${!mtu} ]] && ip link set mtu "${!mtu}" dev "${iface}" 286 [[ -n ${!mtu} ]] && ip link set mtu "${!mtu}" dev "${iface}"
288 287
289 return 0 288 return 0
290} 289}
291 290
292# bool iproute2_post_start(char *interface) 291# bool iproute2_post_start(char *interface)
293# 292#
294# Runs any post_start stuff on our interface and adds routes 293# Runs any post_start stuff on our interface and adds routes
295# Always returns 0 294# Always returns 0
296iproute2_post_start() { 295iproute2_post_start() {
297 local iface="$1" ifvar=$( bash_variable "$1" ) routes metric mtu x netmask 296 local iface="$1" ifvar="$( bash_variable "$1" )" x=""
298 297
299 iproute2_exists "${iface}" || return 0 298 iproute2_exists "${iface}" || return 0
300 299
301 # Make sure interface is marked UP 300 # Make sure interface is marked UP
302 iproute2_up "${iface}" 301 iproute2_up "${iface}"
303 302
304 # MTU support 303 # MTU support
305 mtu="mtu_${ifvar}" 304 local mtu="mtu_${ifvar}"
306 [[ -n ${!mtu} ]] && ip link set mtu "${!mtu}" dev "${iface}" 305 [[ -n ${!mtu} ]] && ip link set mtu "${!mtu}" dev "${iface}"
307 306
308 x="routes_${ifvar}[@]" 307 local x="routes_${ifvar}[@]"
309 routes=( "${!x}" ) 308 local -a routes=( "${!x}" )
310 metric="metric_${ifvar}" 309 local metric="metric_${ifvar}"
311 310
312 # Test for old style ipaddr variable 311 # Test for old style ipaddr variable
313 if [[ -z ${routes} ]]; then 312 if [[ -z ${routes} ]] ; then
314 t="iproute_${ifvar}[@]" 313 t="iproute_${ifvar}[@]"
315 routes=( "${!t}" ) 314 routes=( "${!t}" )
316 fi 315 fi
317 316
318 [[ -z ${routes} ]] && return 0
319
320 # Set routes with ip route -- this might also include default route 317 # Set routes with ip route -- this might also include default route
318 if [[ -n ${routes} ]] ; then
321 einfo "Adding routes" 319 einfo "Adding routes"
322 eindent 320 eindent
323 for x in "${routes[@]}"; do 321 for x in "${routes[@]}"; do
324 ebegin "${x}" 322 ebegin "${x}"
325 323
326 # Support net-tools routing too 324 # Support net-tools routing too
327 x="${x//gw/via}" 325 x="${x//gw/via}"
328 x="${x//-A inet6/}" 326 x="${x//-A inet6/}"
329 x="${x//-net/}" 327 x="${x//-net/}"
330 [[ " ${x} " == *" -host "* ]] && x="${x//-host/} scope host" 328 [[ " ${x} " == *" -host "* ]] && x="${x//-host/} scope host"
331 329
332 # Attempt to support net-tools route netmask option 330 # Attempt to support net-tools route netmask option
333 netmask="${x##* netmask }" 331 netmask="${x##* netmask }"
334 if [[ -n ${netmask} && ${x} != "${netmask}" ]]; then 332 if [[ -n ${netmask} && ${x} != "${netmask}" ]] ; then
335 netmask="${netmask%% *}" 333 netmask="${netmask%% *}"
336 x="${x// netmask ${netmask} / }" 334 x="${x// netmask ${netmask} / }"
337 local -a a=( ${x} ) 335 local -a a=( ${x} )
338 a[0]="${a[0]}/$( netmask2cidr ${netmask} )" 336 a[0]="${a[0]}/$( netmask2cidr "${netmask}")"
339 x="${a[@]}" 337 x="${a[@]}"
338 fi
339
340 # Add a metric if we don't have one
341 [[ " ${x} " != *" metric "* ]] && x="${x} metric ${!metric}"
342
343 ip route append ${x} dev "${iface}"
344 eend $?
345 done
346 eoutdent
347 fi
348
349 # Now apply any rules we may want
350 x="rules_${ifvar}[@]"
351 local -a rules=( "${!x}" )
352 if [[ -n ${rules} ]] ; then
353 einfo "Adding IP policy routing rules"
354 eindent
355 # Ensure that the kernel supports policy routing
356 if ! ip rule list | grep -q "^" ; then
357 eerror "You need to enable IP Policy Routing (CONFIG_IP_MULTIPLE_TABLES)"
358 eerror "in your kernel to use ip rules"
359 else
360 for x in "${rules[@]}" ; do
361 ebegin "${x}"
362 ip rule add ${x} dev "${iface}"
363 eend $?
364 done
340 fi 365 fi
341
342 # Add a metric if we don't have one
343 [[ " ${x} " != *" metric "* ]] && x="${x} metric ${!metric}"
344
345 ip route append ${x} dev "${iface}"
346 eend $?
347 done
348 eoutdent 366 eoutdent
367 fi
368
369 # Flush the route cache
370 ip route flush cache dev "${iface}"
349 371
350 return 0 372 return 0
351} 373}
352 374
353# void iproute2_route_metric(char* interface, int metric) 375# void iproute2_route_metric(char* interface, int metric)
354# 376#
355# Change all routes for an interface to a given metric 377# Change all routes for an interface to a given metric
356iproute2_route_metric() { 378iproute2_route_metric() {
357 local route 379 local route=""
358 ip route | grep " dev $1" | { 380 ip route | grep " dev $1" | {
359 while read route ; do 381 while read route ; do
360 ip route del ${route} 382 ip route del ${route}
361 ip route add ${route} metric "$2" 383 ip route add ${route} metric "$2"
362 done 384 done
372 394
373 ip route change default via "$2" metric "${metric}" dev "$1" 2>/dev/null \ 395 ip route change default via "$2" metric "${metric}" dev "$1" 2>/dev/null \
374 || ip route append default via "$2" metric "${metric}" dev "$1" 2>/dev/null 396 || ip route append default via "$2" metric "${metric}" dev "$1" 2>/dev/null
375} 397}
376 398
399# void iproute2_post_stop(char* interface)
400iproute2_post_stop() {
401 local iface="$1" rule=""
402
403 iproute2_exists "${iface}" || return
404
405 # Remove any rules for this interface
406 ip rule list | grep " iif ${iface}[ ]*" | {
407 while read rule ; do
408 rule="${rule#*:}"
409 ip rule del ${rule}
410 done
411 }
412
413 # Flush the route cache
414 ip route flush cache dev "${iface}"
415}
416
377# vim:ts=4 417# vim:ts=4

Legend:
Removed from v.235  
changed lines
  Added in v.236

  ViewVC Help
Powered by ViewVC 1.1.20