• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3#  Copyright (c) 2024, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29
30# TODO: set the upstream interface according to the environment variables of `script/setup`.
31UPSTREAM_INTERFACE="eth0"
32WPAN_INTERFACE="wpan0"
33
34RADVD_CONF="/etc/radvd.conf"
35LOG_TAG="dhcpcd.enter.hook:"
36
37config_ra()
38{
39    local old_prefix="$1"
40    local old_prefix_len="$2"
41    local new_prefix="$3"
42    local new_prefix_len="$4"
43    local new_pltime="$5"
44    local new_vltime="$6"
45
46    local deprecate_old_prefix=false
47    if [ -n "$old_prefix" ] && [ "$old_prefix/$old_prefix_len" != "$new_prefix/$new_prefix_len" ]; then
48        deprecate_old_prefix=true
49    fi
50
51    local publish_new_prefix=false
52    if [ -n "$new_prefix" ] && [ -n "$new_prefix_len" ] && [ -n "$new_pltime" ] && [ -n "$new_vltime" ]; then
53        publish_new_prefix=true
54    fi
55
56    logger "$LOG_TAG $reason start config radvd"
57
58sudo tee "${RADVD_CONF}" > /dev/null <<EOF
59interface ${WPAN_INTERFACE}
60{
61    IgnoreIfMissing on;
62    AdvSendAdvert on;
63EOF
64
65    if "$deprecate_old_prefix"; then
66        logger "$LOG_TAG Deprecating old prefix $old_prefix/$old_prefix_len"
67sudo tee -a "${RADVD_CONF}" > /dev/null <<EOF
68    prefix ${old_prefix}/${old_prefix_len}
69    {
70        AdvOnLink on;
71        AdvAutonomous on;
72        AdvRouterAddr off;
73        AdvPreferredLifetime 0;
74        AdvValidLifetime 0;
75    };
76EOF
77    fi
78
79    if $publish_new_prefix; then
80        logger "$LOG_TAG Publishing new prefix $new_prefix/$new_prefix_len  PLTime: $new_pltime  VLTime: $new_vltime"
81sudo tee -a "${RADVD_CONF}" > /dev/null <<EOF
82    prefix ${new_prefix}/${new_prefix_len}
83    {
84        AdvOnLink on;
85        AdvAutonomous on;
86        AdvRouterAddr off;
87        AdvPreferredLifetime ${new_pltime};
88        AdvValidLifetime ${new_vltime};
89    };
90EOF
91    fi
92
93sudo tee -a "${RADVD_CONF}" > /dev/null <<EOF
94};
95EOF
96
97}
98
99
100if [ ${interface} = ${UPSTREAM_INTERFACE} ]; then
101
102    for var in $(env); do
103        # Split the variable into name and value
104        name="${var%%=*}"
105        value="${var#*=}"
106        logger "$LOG_TAG $reason sysenv: $name=$value"
107    done
108
109    case $reason in
110        DELEGATED6 | REBIND6 | RENEW6 | BOUND6 )
111            # TODO: Handle multiple IA_PD prefixes (new_dhcp6_ia_pd{i}_prefix{j}, new_dhcp6_ia_pd{i}_prefix{j}_length, etc.)
112            #       and deprecate old prefixes properly for each.  Currently, only one prefix is handled.
113            if { [ -n "$new_dhcp6_ia_pd1_prefix1" ] && [ -n "$new_dhcp6_ia_pd1_prefix1_length" ]; } || \
114               { [ -n "$old_dhcp6_ia_pd1_prefix1" ] && [ -n "$old_dhcp6_ia_pd1_prefix1_length" ]; }; then
115                config_ra "$old_dhcp6_ia_pd1_prefix1" "$old_dhcp6_ia_pd1_prefix1_length" \
116                    "$new_dhcp6_ia_pd1_prefix1" "$new_dhcp6_ia_pd1_prefix1_length" "$new_dhcp6_ia_pd1_prefix1_pltime"  "$new_dhcp6_ia_pd1_prefix1_vltime"
117                if systemctl is-active network.target; then
118                    sudo systemctl reload-or-restart radvd || logger "$LOG_TAG Failed to reload radvd"
119                fi
120            fi
121            ;;
122    esac
123fi
124