summaryrefslogtreecommitdiff
path: root/src/system/on-dhcpv6-event.sh
blob: fcc88ae6f3e8bf2a83a80b735e358da0e49c6320 (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
#!/bin/bash
#
# Copyright (C) 2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

if [ $# -lt 1 ]; then
  echo Invalid args
  logger -s -t on-dhcpv6-event "Invalid args \"$@\""
  exit 1
fi

action=$1

case "$action" in
  lease6_renew|lease6_recover)
    exit 0
    ;;

  lease6_release|lease6_expire|lease6_decline)
    ifname=$QUERY6_IFACE_NAME
    client_ip=$LEASE6_ADDRESS
    client_prefix_len=$LEASE6_PREFIX_LEN

    if [[ "$LEASE6_TYPE" != "IA_PD" ]]; then
      exit 0
    fi

    sudo -n /sbin/ip -6 route del ${client_ip}/${client_prefix_len} \
      dev ${ifname} \
      proto static

    exit 0
    ;;

  leases6_committed)
    for ((i = 0; i < $LEASES6_SIZE; i++)); do
      ifname=$QUERY6_IFACE_NAME
      requester_link_local=$QUERY6_REMOTE_ADDR
      client_type_var="LEASES6_AT${i}_TYPE"
      client_ip_var="LEASES6_AT${i}_ADDRESS"
      client_prefix_len_var="LEASES6_AT${i}_PREFIX_LEN"

      client_type=${!client_type_var}

      if [[ "$client_type" != "IA_PD" ]]; then
        continue
      fi

      client_ip=${!client_ip_var}
      client_prefix_len=${!client_prefix_len_var}
      
      sudo -n /sbin/ip -6 route replace ${client_ip}/${client_prefix_len} \
        via ${requester_link_local} \
        dev ${ifname} \
        proto static
    done

    exit 0
    ;;

  *)
    logger -s -t on-dhcpv6-event "Invalid command \"$1\""
    exit 1
    ;;
esac