# 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
	    else
		set -- $new_rfc3442_classless_static_routes

		while [ $# -gt 0 ]; do
			net_length=$1
			via_arg=''

			case $net_length in
				32|31|30|29|28|27|26|25)
					if [ $# -lt 9 ]; then
						return 1
					fi
					net_address="${2}.${3}.${4}.${5}"
					gateway="${6}.${7}.${8}.${9}"
					shift 9
					;;
				24|23|22|21|20|19|18|17)
					if [ $# -lt 8 ]; then
						return 1
					fi
					net_address="${2}.${3}.${4}.0"
					gateway="${5}.${6}.${7}.${8}"
					shift 8
					;;
				16|15|14|13|12|11|10|9)
					if [ $# -lt 7 ]; then
						return 1
					fi
					net_address="${2}.${3}.0.0"
					gateway="${4}.${5}.${6}.${7}"
					shift 7
					;;
				8|7|6|5|4|3|2|1)
					if [ $# -lt 6 ]; then
						return 1
					fi
					net_address="${2}.0.0.0"
					gateway="${3}.${4}.${5}.${6}"
					shift 6
					;;
				0)	# default route
					if [ $# -lt 5 ]; then
						return 1
					fi
					net_address="0.0.0.0"
					gateway="${2}.${3}.${4}.${5}"
					shift 5
					;;
				*)	# error
					return 1
					;;
			esac

			# take care of link-local routes
			if [ "${gateway}" != '0.0.0.0' ]; then
				via_arg="via ${gateway}"
			fi

			# set route (ip detects host routes automatically)
			ip -4 route add table ${TABLE_ID} "${net_address}/${net_length}" \
				${via_arg} dev "${interface}" >/dev/null 2>&1
		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
