# Routes go to VRF table if interface is enslaved
TABLE_ID=$(vrf table ${interface})
if [ -n "${TABLE_ID}" ]; then
	VRF=$(vrf ls | awk -v table=${TABLE_ID} '$2 == table {print $1}')
fi

if [ -n "${TABLE_ID}" ]; then

# Cleanup DNS state
if [ -n "$old_domain_name_servers" ]; then
    # Delete rules associated with old DNS server
    for dns in $old_domain_name_servers; do
        /usr/lib/vrf/vrf-dns-helper dns_del ${dns} ${TABLE_ID} 1>/dev/null 2>&1
    done
fi

fi

# Add rules to handle DNS server
if [ -n "$new_domain_name_servers" ]; then
    for dns in $new_domain_name_servers; do
	if [ -n "${TABLE_ID}" ]; then
		/usr/lib/vrf/vrf-dns-helper dns_add ${dns} ${TABLE_ID}
		sed -i.bak -e "s/nameserver ${dns}.*/nameserver ${dns} # vrf ${VRF}/" /etc/resolv.conf
	fi
    done
fi

if [ -n "${TABLE_ID}" ]; then

case "$reason" in
    BOUND|RENEW|REBIND|REBOOT)
        if [ -z "$old_ip_address" ] ||
           [ "$old_ip_address" != "$new_ip_address" ] ||
           [ "$reason" = "BOUND" ] || [ "$reason" = "REBOOT" ]; then
	    # if we have $new_rfc3442_classless_static_routes then we have to
	    # ignore $new_routers entirely
	    if [ ! "$new_rfc3442_classless_static_routes" ]; then
		    # set if_metric if IF_METRIC is set or there's more than one router
		    if_metric="$IF_METRIC"
		    if [ "${new_routers%% *}" != "${new_routers}" ]; then
			if_metric=${if_metric:-1}
		    fi

		    for router in $new_routers; do
			if [ "$new_subnet_mask" = "255.255.255.255" ]; then
			    # point-to-point connection => set explicit route
			    ip -4 route add table ${TABLE_ID} ${router} dev $interface >/dev/null 2>&1
			fi

			# remove old default route should it remain from dhclient-script
			ip -4 route del default via ${router} dev ${interface} \
			    ${if_metric:+metric $if_metric} >/dev/null 2>&1

			# set default route
			ip -4 route add table ${TABLE_ID} default via ${router} dev ${interface} \
			    ${if_metric:+metric $if_metric} >/dev/null 2>&1

			if [ -n "$if_metric" ]; then
			    if_metric=$((if_metric+1))
			fi
		    done
	    fi
        fi

        if [ -n "$alias_ip_address" ] &&
           [ "$new_ip_address" != "$alias_ip_address" ]; then
            ip -4 route add table ${TABLE_ID} ${alias_ip_address} dev ${interface} >/dev/null 2>&1
        fi
        ;;

    EXPIRE|FAIL|RELEASE|STOP)
        if [ -n "$alias_ip_address" ]; then
            ip -4 route add table ${TABLE_ID} ${alias_ip_address} dev ${interface} >/dev/null 2>&1
        fi

        ;;

    TIMEOUT)
        # if there is no router recorded in the lease or the 1st router answers pings
        if [ -z "$new_routers" ] || ping -q -c 1 "${new_routers%% *}"; then
	    # if we have $new_rfc3442_classless_static_routes then we have to
	    # ignore $new_routers entirely
	    if [ ! "$new_rfc3442_classless_static_routes" ]; then
		    if [ -n "$alias_ip_address" ] &&
		       [ "$new_ip_address" != "$alias_ip_address" ]; then
			ip -4 route add table ${TABLE_ID} ${alias_ip_address} dev ${interface} >/dev/null 2>&1
		    fi

		    # set default route
		    for router in $new_routers; do
			ip -4 route add table ${TABLE_ID} default via ${router} dev ${interface} \
			    ${if_metric:+metric $if_metric} >/dev/null 2>&1

			if [ -n "$if_metric" ]; then
			    if_metric=$((if_metric+1))
			fi
		    done
	    fi
        fi

        ;;
esac

fi
