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.exit.hook:" 36 37config_ra() 38{ 39 logger "$LOG_TAG $reason start config radvd" 40 41sudo tee "${RADVD_CONF}" > /dev/null <<EOF 42interface ${WPAN_INTERFACE} 43{ 44 AdvSendAdvert on; 45 prefix ${1}/${2} 46 { 47 AdvOnLink on; 48 AdvAutonomous on; 49 AdvRouterAddr off; 50 AdvPreferredLifetime ${3}; 51 AdvValidLifetime ${4}; 52 }; 53}; 54EOF 55} 56 57 58if [ ${interface} = ${UPSTREAM_INTERFACE} ]; then 59 60 for var in $(env); do 61 # Split the variable into name and value 62 name="${var%%=*}" 63 value="${var#*=}" 64 logger -t "$LOG_TAG $reason sysenv: " "$name=$value" 65 done 66 67 case $reason in 68 EXPIRE6 | STOP6 | RELEASE6 ) 69 # TODO: Handle multiple IA_PD prefixes (new_dhcp6_ia_pd{i}_prefix{j}, new_dhcp6_ia_pd{i}_prefix{j}_length, etc.) 70 # and deprecate old prefixes properly for each. Currently, only one prefix is handled.) 71 if [ -z "$old_dhcp6_ia_pd1_prefix1" ] || [ -z "$old_dhcp6_ia_pd1_prefix1_length" ]; then 72 logger "$LOG_TAG WARNING: Missing DHCPv6 prefix information. Skipping radvd configuration." 73 else 74 config_ra $old_dhcp6_ia_pd1_prefix1 $old_dhcp6_ia_pd1_prefix1_length 0 0 75 if systemctl is-active network.target; then 76 sudo systemctl reload-or-restart radvd 77 fi 78 fi 79 ;; 80 esac 81fi 82