1#!/bin/bash 2# 3# Copyright (c) 2021, 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# Description: 30# This script sets up border routing configurations. 31# 32 33readonly INFRA_IF_NAME="${INFRA_IF_NAME:-wlan0}" 34readonly SYSCTL_ACCEPT_RA_FILE="/etc/sysctl.d/60-otbr-accept-ra.conf" 35readonly DHCPCD_CONF_FILE="/etc/dhcpcd.conf" 36readonly DHCPCD_CONF_BACKUP_FILE="$DHCPCD_CONF_FILE.orig" 37 38accept_ra_install() 39{ 40 sudo tee $SYSCTL_ACCEPT_RA_FILE <<EOF 41net.ipv6.conf.${INFRA_IF_NAME}.accept_ra = 2 42net.ipv6.conf.${INFRA_IF_NAME}.accept_ra_rt_info_max_plen = 64 43EOF 44} 45 46accept_ra_uninstall() 47{ 48 test ! -f $SYSCTL_ACCEPT_RA_FILE || sudo rm -v $SYSCTL_ACCEPT_RA_FILE 49} 50 51accept_ra_enable() 52{ 53 with BORDER_ROUTING || return 0 54 55 if [ -f /proc/sys/net/ipv6/conf/"${INFRA_IF_NAME}"/accept_ra ]; then 56 echo 2 | sudo tee /proc/sys/net/ipv6/conf/"${INFRA_IF_NAME}"/accept_ra || die 'Failed to enable IPv6 RA!' 57 fi 58 59 if [ -f /proc/sys/net/ipv6/conf/"${INFRA_IF_NAME}"/accept_ra_rt_info_max_plen ]; then 60 echo 64 | sudo tee /proc/sys/net/ipv6/conf/"${INFRA_IF_NAME}"/accept_ra_rt_info_max_plen || die 'Failed to enable IPv6 RIO!' 61 fi 62} 63 64# This function disables IPv6 support in dhcpcd. 65# 66# dhcpcd on raspberry Pi enables IPv6 support by default. The problem with 67# dhcpcd is that it does't support Route Information Option (RIO), so we need 68# to rely on the kernel implementation. dhcpcd will force set accept_ra to 0 69# for all interfaces it is currently running on, if IPv6 is enabled. This 70# conflicts with our accept_ra* configurations. 71# 72dhcpcd_disable_ipv6() 73{ 74 if [ -f $DHCPCD_CONF_FILE ]; then 75 sudo cp $DHCPCD_CONF_FILE $DHCPCD_CONF_BACKUP_FILE 76 sudo tee -a $DHCPCD_CONF_FILE <<EOF 77noipv6 78noipv6rs 79EOF 80 fi 81} 82 83# This function enables IPv6 support in dhcpcd. 84dhcpcd_enable_ipv6() 85{ 86 if [ -f $DHCPCD_CONF_BACKUP_FILE ]; then 87 sudo cp $DHCPCD_CONF_BACKUP_FILE $DHCPCD_CONF_FILE 88 fi 89} 90 91border_routing_uninstall() 92{ 93 with BORDER_ROUTING || return 0 94 95 accept_ra_uninstall 96 dhcpcd_enable_ipv6 97} 98 99border_routing_install() 100{ 101 with BORDER_ROUTING || return 0 102 103 dhcpcd_disable_ipv6 104 accept_ra_install 105 106 # /proc/sys/net/ipv6/conf/* files are read-only in docker 107 # when building the image. 108 if without DOCKER; then 109 accept_ra_enable 110 fi 111} 112