Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Static routing functionality #277 #281
Browse files Browse the repository at this point in the history
  • Loading branch information
dann1 authored Apr 25, 2023
2 parents fdeea1b + c3a44da commit c8fefd8
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 5 deletions.
2 changes: 1 addition & 1 deletion generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fi

###

VERSION=${VERSION:-6.6.0}
VERSION=${VERSION:-6.6.1}
RELEASE=${RELEASE:-1}
MAINTAINER=${MAINTAINER:-OpenNebula Systems <contact@opennebula.io>}
LICENSE=${LICENSE:-Apache 2.0}
Expand Down
22 changes: 22 additions & 0 deletions src/etc/one-context.d/loc-10-network.d/functions
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ setup_iface_vars()
export ip6_gateway=$(get_iface_var "$1" "IP6_GATEWAY")
export ip6_metric=$(get_iface_var "$1" "IP6_METRIC")
export ip6_method=$(get_iface_var "$1" "IP6_METHOD")
export static_routes=$(get_iface_var "$1" "ROUTES")

# backward compatibility
[ -z "$ip6_gateway" ] && ip6_gateway=$(get_iface_var "$1" "GATEWAY6")
Expand Down Expand Up @@ -572,3 +573,24 @@ gen_resolvconf()
sed -i "/^NETCONFIG_DNS_STATIC_SEARCHLIST=/ s/=.*$/=\"${all_search_domains}\"/" /etc/sysconfig/network/config
fi
}


# 0 if https://en.wikipedia.org/wiki/Link-local_address
is_link_local() {
[[ $1 == "169.254."* ]]
}

get_onegate_ip() {
if [[ -n $ONEGATE_ENDPOINT ]]; then
# Regular expression to match an IPv4 address
ipv4_regex="([0-9]{1,3}\.){3}[0-9]{1,3}"

export onegate_host=$(echo "$ONEGATE_ENDPOINT" | grep -oE "$ipv4_regex")

echo "$onegate_host"
fi
}

add_onegate_proxy_route?() {
is_link_local "$(get_onegate_ip)" && [[ $onegate_proxy_route_missing == "yes" ]]
}
57 changes: 56 additions & 1 deletion src/etc/one-context.d/loc-10-network.d/netcfg-bsd
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,63 @@ gen_iface_conf()

###

routes_conf_path="/etc/rc.conf.d/routing"

if [ -n "${gateway}" ]; then
echo "defaultrouter=\"${gateway}\"" >> /etc/rc.conf.d/routing
echo "defaultrouter=\"${gateway}\"" >> $routes_conf_path
fi

# Add static routes
# static_routes="lan mumoffice foo"
# route_lan="-net 192.168.1.0/24 192.168.1.254"
# route_mumoffice="-net 10.0.0.0/8 10.30.110.5"
# route_foo="-host 169.254.1.1 -iface lo0"
route_names=""

static_routes=$(get_iface_var "$dev" "ROUTES")

if [ -n "${static_routes}" ]; then
IFS=',' read -r -a routes <<< "$static_routes"

routes_conf=()

declare -i index=0

for route in "${routes[@]}"; do
rsplit=(${route})
dst="${rsplit[0]}"
gw="${rsplit[2]}"

route_name="r_${index}"
route_names="${route_names}${route_name} "

route_conf="route_${route_name}=\"-net ${dst} ${gw}\""
routes_conf+=("$route_conf")

index+=1
done

# remove last whitespace
echo -e "static_routes=\"$(echo "$route_names" | xargs)\"" >>$routes_conf_path

for route_conf in "${routes_conf[@]}"; do
echo -e "${route_conf}" >>$routes_conf_path
done

fi

# Add ONEGATE Proxy static route
if [[ $(add_onegate_proxy_route?) ]]; then
route_name="r_onegateproxy"

sed -i "s/${route_names}/${route_names} ${route_name}/g" "$routes_conf_path"
# ip route replace 169.254.16.9/32 via eth0
route_conf="route_${route_name}=\"-host ${onegate_host} -iface ${dev}\""

echo -e "$route_conf\n" >> $routes_conf_path

# Will make proxy static route only applicable to 1st interface
unset onegate_proxy_route_missing
fi
}

Expand Down
26 changes: 26 additions & 0 deletions src/etc/one-context.d/loc-10-network.d/netcfg-interfaces
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,30 @@ EOT
fi
fi

# Add static routes
if [ -n "${static_routes}" ]; then

IFS=',' read -r -a routes <<< "$static_routes"

for route in "${routes[@]}"
do
rsplit=( ${route} )
dst="${rsplit[0]}"
gw="${rsplit[2]}"

echo " up ip route add ${dst} via ${gw}"
done

fi

# Add ONEGATE Proxy static route
if [[ $(add_onegate_proxy_route?) ]]; then
echo " up ip route replace ${onegate_host} via ${dev}"

# Will make proxy static route only applicable to 1st interface
unset onegate_proxy_route_missing
fi

if [ -n "$mtu" ]; then
echo " mtu ${mtu}"
fi
Expand Down Expand Up @@ -351,6 +375,8 @@ EOT

_context_interfaces=$(get_context_interfaces)

export onegate_proxy_route_missing="yes" # flag route not setup

for _iface in $_context_interfaces; do
setup_iface_vars "$_iface"

Expand Down
32 changes: 32 additions & 0 deletions src/etc/one-context.d/loc-10-network.d/netcfg-netplan
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ gen_routes()
via: ${gateway}
EOT



# Force default Linux IPv4 metric (man 8 route) to override
# automatic metrics calculation done by NetworkManager and unify
# behavior among different renderers.
Expand All @@ -145,6 +147,36 @@ EOT
fi
fi

# Add static routes
if [ -n "${static_routes}" ]; then

IFS=',' read -r -a routes <<< "$static_routes"

for route in "${routes[@]}"
do
rsplit=( ${route} )
dst="${rsplit[0]}"
gw="${rsplit[2]}"

cat <<EOT
- to: "${dst}"
via: "${gw}"
EOT
done

fi

# Add ONEGATE Proxy static route
if [[ $(add_onegate_proxy_route?) ]]; then
# ip route replace 169.254.16.9/32 via eth0
cat <<EOT
- to: "${onegate_host}"
scope: link
EOT

unset onegate_proxy_route_missing
fi

if [ -n "${ip6_gateway}" ] && { [ -z "${ip6_method}" ] || [ "${ip6_method}" = 'static' ]; }; then
cat <<EOT
- to: "::/0"
Expand Down
33 changes: 30 additions & 3 deletions src/etc/one-context.d/loc-10-network.d/netcfg-networkd
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ EOT
done
fi

cat <<EOT
[Route]
EOT
echo "[Route]"

if [ -n "$gateway" ]; then
echo "Gateway=${gateway}"
Expand All @@ -119,6 +117,35 @@ EOT
fi
fi

# Add static routes
if [ -n "${static_routes}" ]; then

IFS=',' read -r -a routes <<< "$static_routes"

for route in "${routes[@]}"
do
rsplit=( ${route} )
dst="${rsplit[0]}"
gw="${rsplit[2]}"

echo "[Route]"
echo "Destination=$dst"
echo "Gateway=$gw"

done

fi

# Add ONEGATE Proxy static route
if [[ $(add_onegate_proxy_route?) ]]; then
# ip route replace 169.254.16.9/32 via eth0
echo "[Route]"
echo "Destination=${onegate_host}"
echo "Scope=link"

unset onegate_proxy_route_missing
fi

echo ""
}

Expand Down
24 changes: 24 additions & 0 deletions src/etc/one-context.d/loc-10-network.d/netcfg-nm
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ gen_iface_conf()
nmcli con mod "${dev}" ipv4.gateway ""
fi

# Add static routes
if [ -n "${static_routes}" ]; then

IFS=',' read -r -a routes <<< "$static_routes"

for route in "${routes[@]}"
do
rsplit=( ${route} )
dst="${rsplit[0]}"
gw="${rsplit[2]}"

nmcli con mod "$dev" +ipv4.routes "${dst} ${gw}"
done

fi

# Add ONEGATE Proxy static route
if [[ $(add_onegate_proxy_route?) ]]; then
# ip route replace 169.254.16.9/32 via eth0
nmcli con mod "$dev" +ipv4.routes "$onegate_host"

unset onegate_proxy_route_missing
fi

if [ -n "$metric" ]; then
nmcli con mod "${dev}" ipv4.route-metric "${metric}"
else
Expand Down
25 changes: 25 additions & 0 deletions src/etc/one-context.d/loc-10-network.d/netcfg-scripts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ EOT
fi
fi

# Add static routes
if [ -n "${static_routes}" ]; then

IFS=',' read -r -a routes <<< "$static_routes"

for route in "${routes[@]}"
do
rsplit=( ${route} )
dst="${rsplit[0]}"
gw="${rsplit[2]}"

echo "$route" >> "${config_path}/route-${dev}"
done

fi

# Add ONEGATE Proxy static route
if [[ $(add_onegate_proxy_route?) ]]; then
route="${onegate_host} via ${dev}"
echo "$route" >> "${config_path}/route-${dev}"

# Will make proxy static route only applicable to 1st interface
unset onegate_proxy_route_missing
fi

if [ -n "${mtu}" ]; then
echo "MTU=${mtu}"
fi
Expand Down

0 comments on commit c8fefd8

Please sign in to comment.