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