evenet/lib/route.bash

82 lines
2.0 KiB
Bash
Raw Normal View History

remove_route4() {
local interface="$1"
local subnet="$2"
local netmask="$3"
local gateway="$4"
local cidr="$(mask2cidr $netmask)"
if has ip; then
ip route del "$subnet/$cidr" via "$gateway" dev "$interface"
elif has route; then
route -n del -net "$subnet/$prefixlen" "$gateway"
else
die "no suitable program found to set routes, need iproute2 or ifconfig"
fi
}
2015-02-19 08:36:40 +00:00
add_route4() {
local interface="$1"
local subnet="$2"
local netmask="$3"
local gateway="$4"
2015-02-19 08:36:40 +00:00
local cidr="$(mask2cidr $netmask)"
remove_route4 "$interface" "$subnet" "$netmask" "$gateway" || true
2015-02-21 08:41:13 +00:00
if has ip; then
2015-02-19 08:36:40 +00:00
ip route add "$subnet/$cidr" via "$gateway" dev "$interface"
2015-02-21 08:41:13 +00:00
elif has route; then
route -n add -net "$subnet/$prefixlen" "$gateway"
2015-02-19 08:36:40 +00:00
else
die "no suitable program found to set routes, need iproute2 or ifconfig"
fi
}
remove_route6() {
local interface="$1"
local subnet="$2"
local prefixlen="$3"
local gateway="$4"
if has ip; then
ip route del "$subnet/$prefixlen" via "$gateway" dev "$interface"
elif has route; then
if [[ "$gateway" == fe80* ]]; then
gateway="$gateway%$interface"
fi
route -n add -inet6 "$network" "$gateway" -prefixlen "$prefixlen"
else
die "no suitable program found to set routes, need iproute2 or ifconfig"
fi
}
2015-02-19 08:36:40 +00:00
add_route6() {
local interface="$1"
local subnet="$2"
local prefixlen="$3"
local gateway="$4"
remove_route6 "$interface" "$subnet" "$prefixlen" "$gateway" || true
2015-02-19 08:36:40 +00:00
2015-02-21 08:41:13 +00:00
if has ip; then
2015-02-19 08:36:40 +00:00
ip route add "$subnet/$prefixlen" via "$gateway" dev "$interface"
elif has route; then
2015-02-21 08:41:13 +00:00
if [[ "$gateway" == fe80* ]]; then
gateway="$gateway%$interface"
fi
route -n add -inet6 "$network" "$gateway" -prefixlen "$prefixlen"
2015-02-19 08:36:40 +00:00
else
die "no suitable program found to set routes, need iproute2 or ifconfig"
fi
}
set_route_policy() {
2015-02-19 08:36:40 +00:00
# TODO: other operating systems
if [[ "$OSTYPE" != "linux-gnu" ]]; then
return
fi
if ! has ip; then
warn "Could not set source"
2015-02-19 08:36:40 +00:00
fi
}