Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route configuration cleanup and fixes #94

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 56 additions & 21 deletions lib/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ declare -r imds_token_path="api/token"
declare -r syslog_facility="user"
declare -r syslog_tag="ec2net"
declare -i -r rule_base=10000

# Systemd installs routes with a metric of 1024 by default. We
# override to a lower metric to ensure that our fully configured
# interfaces are preferred over those in the process of being
# configured.
declare -i -r metric_base=512
declare imds_endpoint imds_token

Expand All @@ -35,12 +40,13 @@ get_token() {
# invocations we avoid retrying
local deadline
deadline=$(date -d "now+30 seconds" +%s)
local old_opts=$-
while [ "$(date +%s)" -lt $deadline ]; do
for ep in "${imds_endpoints[@]}"; do
set +e
imds_token=$(curl --max-time 5 --connect-timeout 0.15 -s --fail \
-X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 60" ${ep}/${imds_token_path})
set -e
[[ $old_opts = *e* ]] && set -e
if [ -n "$imds_token" ]; then
debug "Got IMDSv2 token from ${ep}"
imds_endpoint=$ep
Expand Down Expand Up @@ -174,6 +180,23 @@ subnet_supports_ipv6() {
ip -6 addr show dev "$iface" scope global | grep -q inet6
}

subnet_prefixroutes() {
local ether=$1
local family=${2:-ipv4}
if [ -z "$ether" ]; then
err "${FUNCNAME[0]} called without an MAC address"
return 1
fi
case "$family" in
ipv4)
get_iface_imds "$ether" "subnet-${family}-cidr-block"
;;
ipv6)
get_iface_imds "$ether" "subnet-${family}-cidr-blocks"
;;
esac
}

create_rules() {
local iface=$1
local ifid=$2
Expand Down Expand Up @@ -242,10 +265,7 @@ create_if_overrides() {
local cfgdir="${cfgfile}.d"
local dropin="${cfgdir}/eni.conf"
local -i metric=$((metric_base+10*ifid))
local -i tableid=0
if [ $ifid -gt 0 ]; then
tableid=$((rule_base+ifid))
fi
local -i tableid=$((rule_base+ifid))

mkdir -p "$cfgdir"
cat <<EOF > "${dropin}.tmp"
Expand All @@ -254,32 +274,51 @@ create_if_overrides() {
MACAddress=${ether}
[Network]
DHCP=yes

[DHCPv4]
RouteMetric=${metric}
[DHCPv6]
UseRoutes=true
UseGateway=true

[IPv6AcceptRA]
RouteMetric=${metric}
UseGateway=true

EOF

if [ "$tableid" -gt 0 ]; then
cat <<EOF >> "${dropin}.tmp"
cat <<EOF >> "${dropin}.tmp"
[Route]
Table=${tableid}
Gateway=_ipv6ra
[DHCPv4]
RouteTable=${tableid}
[IPv6AcceptRA]
RouteTable=${tableid}

EOF
if subnet_supports_ipv4 "$iface"; then
# if we're not in a v6-only network, add IPv4 routes to the private table
cat <<EOF >> "${dropin}.tmp"
for dest in $(subnet_prefixroutes "$ether" ipv6); do
cat <<EOF >> "${dropin}.tmp"
[Route]
Table=${tableid}
Destination=${dest}

EOF
done

if subnet_supports_ipv4 "$iface"; then
# if not in a v6-only network, add IPv4 routes to the private table
cat <<EOF >> "${dropin}.tmp"
[Route]
Gateway=_dhcp4
Table=${tableid}
EOF
fi
local dest
for dest in $(subnet_prefixroutes "$ether" ipv4); do
cat <<EOF >> "${dropin}.tmp"
[Route]
Table=${tableid}
Destination=${dest}
EOF
done
fi


mv "${dropin}.tmp" "$dropin"
echo 1
}
Expand Down Expand Up @@ -395,11 +434,7 @@ setup_interface() {

changes+=$(create_interface_config "$iface" "$device_number" "$ether")
for family in 4 6; do
if [ "$device_number" -eq 0 ]; then
debug "Skipping ipv$family rules for primary ENI $iface"
else
changes+=$(create_rules "$iface" "$device_number" $family)
fi
changes+=$(create_rules "$iface" "$device_number" $family)
done
changes+=$(create_ipv4_aliases $iface $ether)

Expand Down
7 changes: 0 additions & 7 deletions systemd/network/80-ec2.network
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,3 @@ UseHostname=no
UseDNS=yes
UseNTP=yes
WithoutRA=solicit

[Route]
Gateway=_ipv6ra

[IPv6AcceptRA]
UseDomains=yes