xref: /aosp_15_r20/external/ot-br-posix/script/_border_routing (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker#!/bin/bash
2*4a64e381SAndroid Build Coastguard Worker#
3*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2021, The OpenThread Authors.
4*4a64e381SAndroid Build Coastguard Worker#  All rights reserved.
5*4a64e381SAndroid Build Coastguard Worker#
6*4a64e381SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*4a64e381SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*4a64e381SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*4a64e381SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*4a64e381SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*4a64e381SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*4a64e381SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*4a64e381SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*4a64e381SAndroid Build Coastguard Worker#
17*4a64e381SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*4a64e381SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*4a64e381SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*4a64e381SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*4a64e381SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*4a64e381SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*4a64e381SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*4a64e381SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*4a64e381SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*4a64e381SAndroid Build Coastguard Worker#
29*4a64e381SAndroid Build Coastguard Worker#   Description:
30*4a64e381SAndroid Build Coastguard Worker#       This script sets up border routing configurations.
31*4a64e381SAndroid Build Coastguard Worker#
32*4a64e381SAndroid Build Coastguard Worker
33*4a64e381SAndroid Build Coastguard WorkerINFRA_IF_NAME="${INFRA_IF_NAME:-wlan0}"
34*4a64e381SAndroid Build Coastguard Workerreadonly INFRA_IF_NAME
35*4a64e381SAndroid Build Coastguard Worker
36*4a64e381SAndroid Build Coastguard WorkerSYSCTL_ACCEPT_RA_FILE="/etc/sysctl.d/60-otbr-accept-ra.conf"
37*4a64e381SAndroid Build Coastguard Workerreadonly SYSCTL_ACCEPT_RA_FILE
38*4a64e381SAndroid Build Coastguard Worker
39*4a64e381SAndroid Build Coastguard WorkerDHCPCD_CONF_FILE="/etc/dhcpcd.conf"
40*4a64e381SAndroid Build Coastguard Workerreadonly DHCPCD_CONF_FILE
41*4a64e381SAndroid Build Coastguard Worker
42*4a64e381SAndroid Build Coastguard WorkerDHCPCD_CONF_BACKUP_FILE="$DHCPCD_CONF_FILE.orig"
43*4a64e381SAndroid Build Coastguard Workerreadonly DHCPCD_CONF_BACKUP_FILE
44*4a64e381SAndroid Build Coastguard Worker
45*4a64e381SAndroid Build Coastguard Workeraccept_ra_install()
46*4a64e381SAndroid Build Coastguard Worker{
47*4a64e381SAndroid Build Coastguard Worker    sudo tee $SYSCTL_ACCEPT_RA_FILE <<EOF
48*4a64e381SAndroid Build Coastguard Workernet.ipv6.conf.${INFRA_IF_NAME}.accept_ra = 2
49*4a64e381SAndroid Build Coastguard Workernet.ipv6.conf.${INFRA_IF_NAME}.accept_ra_rt_info_max_plen = 64
50*4a64e381SAndroid Build Coastguard WorkerEOF
51*4a64e381SAndroid Build Coastguard Worker}
52*4a64e381SAndroid Build Coastguard Worker
53*4a64e381SAndroid Build Coastguard Workeraccept_ra_uninstall()
54*4a64e381SAndroid Build Coastguard Worker{
55*4a64e381SAndroid Build Coastguard Worker    test ! -f $SYSCTL_ACCEPT_RA_FILE || sudo rm -v $SYSCTL_ACCEPT_RA_FILE
56*4a64e381SAndroid Build Coastguard Worker}
57*4a64e381SAndroid Build Coastguard Worker
58*4a64e381SAndroid Build Coastguard Workeraccept_ra_enable()
59*4a64e381SAndroid Build Coastguard Worker{
60*4a64e381SAndroid Build Coastguard Worker    with BORDER_ROUTING || return 0
61*4a64e381SAndroid Build Coastguard Worker
62*4a64e381SAndroid Build Coastguard Worker    if [ -f /proc/sys/net/ipv6/conf/"${INFRA_IF_NAME}"/accept_ra ]; then
63*4a64e381SAndroid Build Coastguard Worker        echo 2 | sudo tee /proc/sys/net/ipv6/conf/"${INFRA_IF_NAME}"/accept_ra || die 'Failed to enable IPv6 RA!'
64*4a64e381SAndroid Build Coastguard Worker    fi
65*4a64e381SAndroid Build Coastguard Worker
66*4a64e381SAndroid Build Coastguard Worker    if [ -f /proc/sys/net/ipv6/conf/"${INFRA_IF_NAME}"/accept_ra_rt_info_max_plen ]; then
67*4a64e381SAndroid Build Coastguard Worker        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*4a64e381SAndroid Build Coastguard Worker    fi
69*4a64e381SAndroid Build Coastguard Worker}
70*4a64e381SAndroid Build Coastguard Worker
71*4a64e381SAndroid Build Coastguard Worker# This function disables IPv6 support in dhcpcd.
72*4a64e381SAndroid Build Coastguard Worker#
73*4a64e381SAndroid Build Coastguard Worker# dhcpcd on raspberry Pi enables IPv6 support by default. The problem with
74*4a64e381SAndroid Build Coastguard Worker# dhcpcd is that it does't support Route Information Option (RIO), so we need
75*4a64e381SAndroid Build Coastguard Worker# to rely on the kernel implementation. dhcpcd will force set accept_ra to 0
76*4a64e381SAndroid Build Coastguard Worker# for all interfaces it is currently running on, if IPv6 is enabled. This
77*4a64e381SAndroid Build Coastguard Worker# conflicts with our accept_ra* configurations.
78*4a64e381SAndroid Build Coastguard Worker#
79*4a64e381SAndroid Build Coastguard Workerdhcpcd_disable_ipv6()
80*4a64e381SAndroid Build Coastguard Worker{
81*4a64e381SAndroid Build Coastguard Worker    if [ -f $DHCPCD_CONF_FILE ]; then
82*4a64e381SAndroid Build Coastguard Worker        sudo cp $DHCPCD_CONF_FILE $DHCPCD_CONF_BACKUP_FILE
83*4a64e381SAndroid Build Coastguard Worker        sudo tee -a $DHCPCD_CONF_FILE <<EOF
84*4a64e381SAndroid Build Coastguard Workernoipv6
85*4a64e381SAndroid Build Coastguard Workernoipv6rs
86*4a64e381SAndroid Build Coastguard WorkerEOF
87*4a64e381SAndroid Build Coastguard Worker    fi
88*4a64e381SAndroid Build Coastguard Worker}
89*4a64e381SAndroid Build Coastguard Worker
90*4a64e381SAndroid Build Coastguard Worker# This function enables IPv6 support in dhcpcd.
91*4a64e381SAndroid Build Coastguard Workerdhcpcd_enable_ipv6()
92*4a64e381SAndroid Build Coastguard Worker{
93*4a64e381SAndroid Build Coastguard Worker    if [ -f $DHCPCD_CONF_BACKUP_FILE ]; then
94*4a64e381SAndroid Build Coastguard Worker        sudo cp $DHCPCD_CONF_BACKUP_FILE $DHCPCD_CONF_FILE
95*4a64e381SAndroid Build Coastguard Worker    fi
96*4a64e381SAndroid Build Coastguard Worker}
97*4a64e381SAndroid Build Coastguard Worker
98*4a64e381SAndroid Build Coastguard Workerborder_routing_uninstall()
99*4a64e381SAndroid Build Coastguard Worker{
100*4a64e381SAndroid Build Coastguard Worker    with BORDER_ROUTING || return 0
101*4a64e381SAndroid Build Coastguard Worker
102*4a64e381SAndroid Build Coastguard Worker    accept_ra_uninstall
103*4a64e381SAndroid Build Coastguard Worker    dhcpcd_enable_ipv6
104*4a64e381SAndroid Build Coastguard Worker}
105*4a64e381SAndroid Build Coastguard Worker
106*4a64e381SAndroid Build Coastguard Workerborder_routing_install()
107*4a64e381SAndroid Build Coastguard Worker{
108*4a64e381SAndroid Build Coastguard Worker    with BORDER_ROUTING || return 0
109*4a64e381SAndroid Build Coastguard Worker
110*4a64e381SAndroid Build Coastguard Worker    dhcpcd_disable_ipv6
111*4a64e381SAndroid Build Coastguard Worker    accept_ra_install
112*4a64e381SAndroid Build Coastguard Worker
113*4a64e381SAndroid Build Coastguard Worker    # /proc/sys/net/ipv6/conf/* files are read-only in docker
114*4a64e381SAndroid Build Coastguard Worker    # when building the image.
115*4a64e381SAndroid Build Coastguard Worker    if without DOCKER; then
116*4a64e381SAndroid Build Coastguard Worker        accept_ra_enable
117*4a64e381SAndroid Build Coastguard Worker    fi
118*4a64e381SAndroid Build Coastguard Worker}
119