summaryrefslogtreecommitdiff
path: root/src/etc/dhcp/dhclient-exit-hooks.d/01-vyos-cleanup
blob: a6989441bd99487d415b782966abde4dd4ca09d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
##
## VyOS cleanup
##
# NOTE: here we use 'ip' wrapper, therefore a route will be actually deleted via ip or vtysh, according to the system state
hostsd_client="/usr/bin/vyos-hostsd-client"
hostsd_changes=
# check vyos-hostsd status
/usr/bin/systemctl -q is-active vyos-hostsd
hostsd_status=$?

if [[ $reason =~ (EXPIRE|FAIL|RELEASE|STOP) ]]; then
    if [[ $hostsd_status -eq 0 ]]; then
        # delete search domains and nameservers via vyos-hostsd
        logmsg info "Deleting search domains with tag \"dhcp-$interface\" via vyos-hostsd-client"
        $hostsd_client --delete-search-domains --tag "dhcp-$interface"
        logmsg info "Deleting nameservers with tag \"dhcp-${interface}\" via vyos-hostsd-client"
        $hostsd_client --delete-name-servers --tag "dhcp-${interface}"
        hostsd_changes=y
    fi

    if_metric="$IF_METRIC"

    # try to delete default ip route
    for router in $old_routers; do
        # check if we are bound to a VRF
        local vrf_name=$(basename /sys/class/net/${interface}/upper_* | sed -e 's/upper_//')
        if [ "$vrf_name" != "*" ]; then
            vrf="vrf $vrf_name"
        fi

        logmsg info "Deleting default route: via $router dev ${interface} ${if_metric:+metric $if_metric} ${vrf}"
        ip -4 route del default via $router dev ${interface} ${if_metric:+metric $if_metric} ${vrf}

        if_metric=$((if_metric+1))
    done

    # delete rfc3442 routes
    if [ -n "$old_rfc3442_classless_static_routes" ]; then
        set -- $old_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
            # delete route (ip detects host routes automatically)
            ip -4 route del "${net_address}/${net_length}" \
                ${via_arg} dev "${interface}" >/dev/null 2>&1
        done
    fi
fi

if [[ $reason =~ (EXPIRE6|RELEASE6|STOP6) ]]; then
    if [[ $hostsd_status -eq 0 ]]; then
        # delete search domains and nameservers via vyos-hostsd
        logmsg info "Deleting search domains with tag \"dhcpv6-$interface\" via vyos-hostsd-client"
        $hostsd_client --delete-search-domains --tag "dhcpv6-$interface"
        logmsg info "Deleting nameservers with tag \"dhcpv6-${interface}\" via vyos-hostsd-client"
        $hostsd_client --delete-name-servers --tag "dhcpv6-${interface}"
        hostsd_changes=y
    fi
fi

if [ $hostsd_changes ]; then
    logmsg info "Applying changes via vyos-hostsd-client"
    $hostsd_client --apply
else
    logmsg info "No changes to apply via vyos-hostsd-client"
fi