|
| 1 | +# |
| 2 | +# DHCLIENT exit hook for vrf support. |
| 3 | +# |
| 4 | +# Code ported from https://github.com/CumulusNetworks/vrf under GPLv2 license |
| 5 | +# (see https://github.com/CumulusNetworks/vrf/blob/master/debian/copyright). |
| 6 | +# |
| 7 | + |
| 8 | +# Get table_id for device enslaved to a vrf. |
| 9 | +vrf_get_table_dev() |
| 10 | +{ |
| 11 | + local table_id |
| 12 | + |
| 13 | + # If vrf_slave is not in the output, device is not enslaved. |
| 14 | + table_id=$(ip -o -d link show dev ${1} 2>/dev/null |\ |
| 15 | + egrep ' vrf_slave table [0-9]*' |\ |
| 16 | + sed -e 's/.*vrf_slave table \([0-9]*\) .*/\1/') |
| 17 | + |
| 18 | + [ -z "${table_id}" ] && return 1 |
| 19 | + |
| 20 | + echo ${table_id} |
| 21 | + |
| 22 | + return 0 |
| 23 | +} |
| 24 | + |
| 25 | +# Get table_id for vrf device. |
| 26 | +vrf_get_table() |
| 27 | +{ |
| 28 | + local table_id |
| 29 | + |
| 30 | + table_id=$(ip -o -d link show dev ${1} 2>/dev/null |\ |
| 31 | + egrep ' vrf table [0-9]*' |\ |
| 32 | + sed -e 's/.*vrf table \([0-9]*\) .*/\1/') |
| 33 | + |
| 34 | + [ -z "${table_id}" ] && return 1 |
| 35 | + |
| 36 | + echo ${table_id} |
| 37 | + |
| 38 | + return 0 |
| 39 | +} |
| 40 | + |
| 41 | +vrf_exists() |
| 42 | +{ |
| 43 | + local vrf=${1} |
| 44 | + local n |
| 45 | + |
| 46 | + [ "$vrf" = "default" ] && return 0 |
| 47 | + |
| 48 | + # ip link show dev <name> type vrf happily returns 0 even though |
| 49 | + # <name> is not of type vrf. Hence the wc -l. |
| 50 | + n=$(ip -br link show dev ${vrf} type vrf 2>/dev/null | wc -l) |
| 51 | + [ ${n} -eq 1 ] && return 0 |
| 52 | + |
| 53 | + return $? |
| 54 | +} |
| 55 | + |
| 56 | +# Check vrf device contains only alphanumeric characters. |
| 57 | +get_vrf_arg() |
| 58 | +{ |
| 59 | + local vrf |
| 60 | + |
| 61 | + vrf=$(echo $1 | tr -cd [:alnum:]) |
| 62 | + if [ "$vrf" != "$1" ]; then |
| 63 | + echo "Invalid VRF" >&2 |
| 64 | + return 1 |
| 65 | + fi |
| 66 | + |
| 67 | + echo $vrf |
| 68 | +} |
| 69 | + |
| 70 | +vrf_table() |
| 71 | +{ |
| 72 | + local table_id |
| 73 | + local vrf |
| 74 | + |
| 75 | + vrf=$(get_vrf_arg ${1}) |
| 76 | + [ $? -ne 0 ] && return 1 |
| 77 | + |
| 78 | + vrf_exists $vrf |
| 79 | + if [ $? -eq 0 ]; then |
| 80 | + vrf_get_table $vrf |
| 81 | + return 0 |
| 82 | + fi |
| 83 | + |
| 84 | + # Maybe this is a device, not a vrf. |
| 85 | + table_id=$(vrf_get_table_dev $vrf) |
| 86 | + if [ $? -eq 0 ]; then |
| 87 | + echo ${table_id} |
| 88 | + return 0 |
| 89 | + fi |
| 90 | + |
| 91 | + return 1 |
| 92 | +} |
| 93 | + |
| 94 | +table_id=$(vrf_table ${interface}) |
| 95 | + |
| 96 | +if [ -n "${table_id}" ]; then |
| 97 | + |
| 98 | +case "$reason" in |
| 99 | + BOUND|RENEW|REBIND|REBOOT) |
| 100 | + if [ -z "$old_ip_address" ] || |
| 101 | + [ "$old_ip_address" != "$new_ip_address" ] || |
| 102 | + [ "$reason" = "BOUND" ] || [ "$reason" = "REBOOT" ]; then |
| 103 | + # If we have $new_rfc3442_classless_static_routes then we have to |
| 104 | + # ignore $new_routers entirely. |
| 105 | + if [ ! "$new_rfc3442_classless_static_routes" ]; then |
| 106 | + # Set if_metric if IF_METRIC is set or there's more than one router. |
| 107 | + if_metric="$IF_METRIC" |
| 108 | + if [ "${new_routers%% *}" != "${new_routers}" ]; then |
| 109 | + if_metric=${if_metric:-1} |
| 110 | + fi |
| 111 | + |
| 112 | + for router in $new_routers; do |
| 113 | + if [ "$new_subnet_mask" = "255.255.255.255" ]; then |
| 114 | + # Set explicit route for p2p connection. |
| 115 | + ip -4 route add table ${table_id} ${router} dev $interface >/dev/null 2>&1 |
| 116 | + fi |
| 117 | + |
| 118 | + # Remove old default route should it remain from dhclient-script. |
| 119 | + ip -4 route del default via ${router} dev ${interface} \ |
| 120 | + ${if_metric:+metric $if_metric} >/dev/null 2>&1 |
| 121 | + |
| 122 | + # Set default route. |
| 123 | + ip -4 route add table ${table_id} default via ${router} dev ${interface} \ |
| 124 | + ${if_metric:+metric $if_metric} >/dev/null 2>&1 |
| 125 | + |
| 126 | + if [ -n "$if_metric" ]; then |
| 127 | + if_metric=$((if_metric+1)) |
| 128 | + fi |
| 129 | + done |
| 130 | + else |
| 131 | + set -- $new_rfc3442_classless_static_routes |
| 132 | + |
| 133 | + while [ $# -gt 0 ]; do |
| 134 | + net_length=$1 |
| 135 | + via_arg='' |
| 136 | + |
| 137 | + case $net_length in |
| 138 | + 32|31|30|29|28|27|26|25) |
| 139 | + if [ $# -lt 9 ]; then |
| 140 | + return 1 |
| 141 | + fi |
| 142 | + net_address="${2}.${3}.${4}.${5}" |
| 143 | + gateway="${6}.${7}.${8}.${9}" |
| 144 | + shift 9 |
| 145 | + ;; |
| 146 | + 24|23|22|21|20|19|18|17) |
| 147 | + if [ $# -lt 8 ]; then |
| 148 | + return 1 |
| 149 | + fi |
| 150 | + net_address="${2}.${3}.${4}.0" |
| 151 | + gateway="${5}.${6}.${7}.${8}" |
| 152 | + shift 8 |
| 153 | + ;; |
| 154 | + 16|15|14|13|12|11|10|9) |
| 155 | + if [ $# -lt 7 ]; then |
| 156 | + return 1 |
| 157 | + fi |
| 158 | + net_address="${2}.${3}.0.0" |
| 159 | + gateway="${4}.${5}.${6}.${7}" |
| 160 | + shift 7 |
| 161 | + ;; |
| 162 | + 8|7|6|5|4|3|2|1) |
| 163 | + if [ $# -lt 6 ]; then |
| 164 | + return 1 |
| 165 | + fi |
| 166 | + net_address="${2}.0.0.0" |
| 167 | + gateway="${3}.${4}.${5}.${6}" |
| 168 | + shift 6 |
| 169 | + ;; |
| 170 | + 0) # default route |
| 171 | + if [ $# -lt 5 ]; then |
| 172 | + return 1 |
| 173 | + fi |
| 174 | + net_address="0.0.0.0" |
| 175 | + gateway="${2}.${3}.${4}.${5}" |
| 176 | + shift 5 |
| 177 | + ;; |
| 178 | + *) # error |
| 179 | + return 1 |
| 180 | + ;; |
| 181 | + esac |
| 182 | + |
| 183 | + # Take care of link-local routes. |
| 184 | + if [ "${gateway}" != '0.0.0.0' ]; then |
| 185 | + via_arg="via ${gateway}" |
| 186 | + fi |
| 187 | + |
| 188 | + # Set route (ip detects host routes automatically). |
| 189 | + ip -4 route add table ${table_id} "${net_address}/${net_length}" \ |
| 190 | + ${via_arg} dev "${interface}" >/dev/null 2>&1 |
| 191 | + done |
| 192 | + fi |
| 193 | + fi |
| 194 | + |
| 195 | + if [ -n "$alias_ip_address" ] && |
| 196 | + [ "$new_ip_address" != "$alias_ip_address" ]; then |
| 197 | + ip -4 route add table ${table_id} ${alias_ip_address} dev ${interface} >/dev/null 2>&1 |
| 198 | + fi |
| 199 | + ;; |
| 200 | + |
| 201 | + EXPIRE|FAIL|RELEASE|STOP) |
| 202 | + if [ -n "$alias_ip_address" ]; then |
| 203 | + ip -4 route add table ${table_id} ${alias_ip_address} dev ${interface} >/dev/null 2>&1 |
| 204 | + fi |
| 205 | + |
| 206 | + ;; |
| 207 | + |
| 208 | + TIMEOUT) |
| 209 | + # If there is no router recorded in the lease or the 1st router answers pings. |
| 210 | + if [ -z "$new_routers" ] || ping -q -c 1 "${new_routers%% *}"; then |
| 211 | + # If we have $new_rfc3442_classless_static_routes then we have to |
| 212 | + # ignore $new_routers entirely. |
| 213 | + if [ ! "$new_rfc3442_classless_static_routes" ]; then |
| 214 | + if [ -n "$alias_ip_address" ] && |
| 215 | + [ "$new_ip_address" != "$alias_ip_address" ]; then |
| 216 | + ip -4 route add table ${table_id} ${alias_ip_address} dev ${interface} >/dev/null 2>&1 |
| 217 | + fi |
| 218 | + |
| 219 | + # Set default route. |
| 220 | + for router in $new_routers; do |
| 221 | + ip -4 route add table ${table_id} default via ${router} dev ${interface} \ |
| 222 | + ${if_metric:+metric $if_metric} >/dev/null 2>&1 |
| 223 | + |
| 224 | + if [ -n "$if_metric" ]; then |
| 225 | + if_metric=$((if_metric+1)) |
| 226 | + fi |
| 227 | + done |
| 228 | + fi |
| 229 | + fi |
| 230 | + |
| 231 | + ;; |
| 232 | +esac |
| 233 | + |
| 234 | +fi |
0 commit comments