| 1 |
# iproute2 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 |
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 |
[[ -x /sbin/ip ]] && return 0
|
| 29 |
${1:-false} && eerror "For iproute2 support, emerge sys-apps/iproute2"
|
| 30 |
return 1
|
| 31 |
}
|
| 32 |
|
| 33 |
# char* iproute2_provides(void)
|
| 34 |
#
|
| 35 |
# Returns a string to change module definition for starting up
|
| 36 |
iproute2_provides() {
|
| 37 |
echo "interface"
|
| 38 |
}
|
| 39 |
|
| 40 |
# char* iproute2_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 |
iproute2_module() {
|
| 46 |
echo "iproute2"
|
| 47 |
}
|
| 48 |
|
| 49 |
# bool iproute2_check_depends(void)
|
| 50 |
#
|
| 51 |
# Checks to see if we have the needed functions
|
| 52 |
iproute2_check_depends() {
|
| 53 |
local f
|
| 54 |
|
| 55 |
for f in interface_device interface_variable; do
|
| 56 |
[[ $( type -t ${f} ) == function ]] && continue
|
| 57 |
eerror "iproute2: missing required function ${f}\n"
|
| 58 |
return 1
|
| 59 |
done
|
| 60 |
|
| 61 |
return 0
|
| 62 |
}
|
| 63 |
|
| 64 |
# bool iproute2_exists(char *interface, bool report)
|
| 65 |
#
|
| 66 |
# Returns 1 if the interface exists, otherwise 0
|
| 67 |
iproute2_exists() {
|
| 68 |
local e=$( ip addr show label ${1} ) report=${2:-false}
|
| 69 |
[[ -n ${e} ]] && return 0
|
| 70 |
${report} && eerror "${1} does not exist"
|
| 71 |
return 1
|
| 72 |
}
|
| 73 |
|
| 74 |
# void iproute2_up(char *interface)
|
| 75 |
#
|
| 76 |
# provides a generic interface for bringing interfaces up
|
| 77 |
iproute2_up() {
|
| 78 |
ip link set up dev ${1} &>${devnull}
|
| 79 |
}
|
| 80 |
|
| 81 |
# void iproute2_down(char *interface)
|
| 82 |
#
|
| 83 |
# provides a generic interface for bringing interfaces up
|
| 84 |
iproute2_down() {
|
| 85 |
ip link set down dev ${1} &>${devnull}
|
| 86 |
}
|
| 87 |
|
| 88 |
# bool ifproute2_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 |
iproute2_is_up() {
|
| 94 |
local iface=${1} addr=${2:-false}
|
| 95 |
|
| 96 |
ip link show ${iface} 2>${devnull} | grep -Eq "\<UP\>" || return 1
|
| 97 |
! ${addr} && return 0
|
| 98 |
ip addr show ${iface} 2>${devnull} | grep -v link | grep -q 'inet ' || return 1
|
| 99 |
}
|
| 100 |
|
| 101 |
# void iproute2_set_flag(char *iface, char *flag, bool enabled)
|
| 102 |
#
|
| 103 |
# Sets or disables the interface flag
|
| 104 |
iproute2_set_flag() {
|
| 105 |
local iface=${1} flag=${2} enable=${3} opt="on"
|
| 106 |
${enable} || opt="off"
|
| 107 |
ip link set ${iface} ${flag} ${opt} &>${devnull}
|
| 108 |
}
|
| 109 |
|
| 110 |
# void loopback_create(void)
|
| 111 |
#
|
| 112 |
# Creates our loopback interface
|
| 113 |
iproute2_loopback_create() {
|
| 114 |
ip addr add 127.0.0.1/8 dev lo brd + scope host &>/dev/null
|
| 115 |
iproute2_up lo
|
| 116 |
ip route add 127.0.0.0/8 dev lo &>/dev/null
|
| 117 |
}
|
| 118 |
|
| 119 |
# void iproute2_get_address(char *interface)
|
| 120 |
#
|
| 121 |
# Fetch the address retrieved by DHCP. If successful, echoes the
|
| 122 |
# address on stdout, otherwise echoes nothing.
|
| 123 |
iproute2_get_address() {
|
| 124 |
ip -family inet addr show ${1} 2>${devnull} | awk '/inet/ {print $2}' | cut -d/ -f1
|
| 125 |
}
|
| 126 |
|
| 127 |
# void get_mac_address(char *interface)
|
| 128 |
#
|
| 129 |
# Fetch the mac address assingned to the network card
|
| 130 |
iproute2_get_mac_address() {
|
| 131 |
ip link show ${1} 2>${devnull} | awk '/link/ {print $2}' | cut -d/ -f1
|
| 132 |
}
|
| 133 |
|
| 134 |
# void iproute2_get_aliases_rev(char *interface)
|
| 135 |
#
|
| 136 |
# Fetch the list of aliases for an interface.
|
| 137 |
# Outputs a space-separated list on stdout, in reverse order, for
|
| 138 |
# example "eth0:2 eth0:1"
|
| 139 |
iproute2_get_aliases_rev() {
|
| 140 |
local iface=$( interface_device ${1} )
|
| 141 |
ip addr show dev ${iface} 2>${devnull} | awk -v re="^${1}:" \
|
| 142 |
'$NF~re {print $NF}' | tac | xargs
|
| 143 |
}
|
| 144 |
|
| 145 |
# bool iproute2_del_addresses(char *interface, bool report)
|
| 146 |
#
|
| 147 |
# Remove addresses from interface.
|
| 148 |
iproute2_del_addresses() {
|
| 149 |
local iface=${1}
|
| 150 |
|
| 151 |
ip addr flush label ${iface} scope global &>/dev/null
|
| 152 |
ip addr flush label ${iface} scope host &>/dev/null
|
| 153 |
|
| 154 |
return 0
|
| 155 |
}
|
| 156 |
|
| 157 |
# char* iproute2_get_vars(char *interface)
|
| 158 |
#
|
| 159 |
# Returns a string spaced with possible user set
|
| 160 |
# configuration variables
|
| 161 |
iproute2_get_vars() {
|
| 162 |
echo "config_${1} routes_${1} fallback_${1} ipaddr_${1} ipaddr_fallback_${1} iproute_${1} inet6_${1}"
|
| 163 |
}
|
| 164 |
|
| 165 |
# bool iproute2_get_old_config(char *iface)
|
| 166 |
#
|
| 167 |
# Returns config and config_fallback for the given interface
|
| 168 |
iproute2_get_old_config() {
|
| 169 |
local ifvar=$( interface_variable ${1} ) inet6
|
| 170 |
|
| 171 |
# iproute2-style config vars
|
| 172 |
eval config=( \"\$\{ipaddr_${ifvar}\[@\]\}\" )
|
| 173 |
eval config_fallback=( \"\$\{ipaddr_fallback_${ifvar}\[@\]\}\" )
|
| 174 |
eval inet6=( \"\$\{inet6_${ifvar}\[@\]\}\" )
|
| 175 |
|
| 176 |
# BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
|
| 177 |
[[ ${#inet6[@]} == 1 && ${inet6} == *' '* ]] && inet6=( ${inet6} )
|
| 178 |
|
| 179 |
# Add inet6 addresses to our config if required
|
| 180 |
[[ -n ${inet6} ]] && config=( "${config[@]}" "${inet6[@]}" )
|
| 181 |
|
| 182 |
return 0
|
| 183 |
}
|
| 184 |
|
| 185 |
# bool iproute2_iface_stop(char *interface)
|
| 186 |
#
|
| 187 |
# Do final shutdown for an interface or alias.
|
| 188 |
#
|
| 189 |
# Returns 0 (true) when successful, non-zero (false) on failure
|
| 190 |
iproute2_iface_stop() {
|
| 191 |
local label=${1} iface=$( interface_device ${1} )
|
| 192 |
|
| 193 |
# Shut down the link if this isn't an alias or vlan
|
| 194 |
if [[ ${label} == ${iface} ]]; then
|
| 195 |
iproute2_down ${iface}
|
| 196 |
return $?
|
| 197 |
fi
|
| 198 |
return 0
|
| 199 |
}
|
| 200 |
|
| 201 |
# bool iproute2_add_address(char *interface, char *options ...)
|
| 202 |
#
|
| 203 |
# Adds an the specified address to the interface
|
| 204 |
# returns 0 on success and non-zero on failure
|
| 205 |
iproute2_add_address() {
|
| 206 |
local iface=${1} x
|
| 207 |
|
| 208 |
iproute2_exists ${iface} true || return 1
|
| 209 |
|
| 210 |
# Extract the config
|
| 211 |
local -a config=( "$@" )
|
| 212 |
config=( ${config[@]:1} )
|
| 213 |
|
| 214 |
# Convert an ifconfig line to iproute2
|
| 215 |
local n=${#config[@]};
|
| 216 |
for (( x=0; x<n; x++ )); do
|
| 217 |
case ${config[x]} in
|
| 218 |
netmask)
|
| 219 |
config[0]="${config[0]}/$( netmask2cidr ${config[x+1]} )"
|
| 220 |
unset config[x]
|
| 221 |
unset config[x+1]
|
| 222 |
;;
|
| 223 |
esac
|
| 224 |
done
|
| 225 |
config=( "${config[@]//pointtopoint/peer}" )
|
| 226 |
|
| 227 |
# Make sure interface is marked UP
|
| 228 |
# This is required for IPv6 addresses
|
| 229 |
iproute2_up ${iface}
|
| 230 |
|
| 231 |
# If the address already exists then the following command
|
| 232 |
# will fail. Catch the failure and be graceful
|
| 233 |
x=$( ip addr add dev ${iface} ${config[@]} 2>&1 )
|
| 234 |
case "${x}" in
|
| 235 |
'RTNETLINK answers: File exists'|'')
|
| 236 |
eend 0
|
| 237 |
return 0
|
| 238 |
;;
|
| 239 |
*)
|
| 240 |
printf '%s\n' "${x}" >&2
|
| 241 |
eend 1
|
| 242 |
return 1
|
| 243 |
esac
|
| 244 |
}
|
| 245 |
|
| 246 |
# bool iproute2_post_start(char *interface)
|
| 247 |
#
|
| 248 |
# Runs any post_start stuff on our interface and adds routes
|
| 249 |
# Always returns 0
|
| 250 |
iproute2_post_start() {
|
| 251 |
local iface=${1} ifvar=$( interface_variable ${1} ) routes e r
|
| 252 |
|
| 253 |
# Make sure interface is marked UP
|
| 254 |
iproute2_up ${iface}
|
| 255 |
|
| 256 |
eval routes=( \"\$\{routes_${ifvar}\[@\]\}\" )
|
| 257 |
|
| 258 |
# Test for old style ipaddr variable
|
| 259 |
if [[ -z ${routes} ]]; then
|
| 260 |
eval routes=( \"\$\{iproute_${ifvar}\[@\]\}\" )
|
| 261 |
fi
|
| 262 |
|
| 263 |
[[ -z ${routes} ]] && return 0
|
| 264 |
|
| 265 |
# Set routes with ip route -- this might also include default route
|
| 266 |
einfo "Adding routes"
|
| 267 |
eindent
|
| 268 |
for x in "${routes[@]}"; do
|
| 269 |
# Support net-tools routing too
|
| 270 |
x=${x//gw/via}
|
| 271 |
x=${x//-A inet6}
|
| 272 |
|
| 273 |
einfo "${x}"
|
| 274 |
e=$( ip route add dev ${iface} ${x} 2>&1 )
|
| 275 |
case "${e}" in
|
| 276 |
'RTNETLINK answers: File exists'|'')
|
| 277 |
eend 0
|
| 278 |
;;
|
| 279 |
*) printf '%s\n' "${e}" >&2
|
| 280 |
eend 1
|
| 281 |
;;
|
| 282 |
esac
|
| 283 |
done
|
| 284 |
eoutdent
|
| 285 |
|
| 286 |
return 0
|
| 287 |
}
|