xref: /aosp_15_r20/external/ot-br-posix/script/_dhcpv6_pd (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker#!/bin/bash
2*4a64e381SAndroid Build Coastguard Worker#
3*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2017, 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 manipulates DHCPv6-PD configuration.
31*4a64e381SAndroid Build Coastguard Worker#
32*4a64e381SAndroid Build Coastguard Worker#
33*4a64e381SAndroid Build Coastguard Worker# Currently solution was verified only on raspbian.
34*4a64e381SAndroid Build Coastguard Worker#
35*4a64e381SAndroid Build Coastguard Worker
36*4a64e381SAndroid Build Coastguard Workerif [ "$PLATFORM" = "ubuntu" ]; then
37*4a64e381SAndroid Build Coastguard Worker    WAN_INTERFACE="enp0s3"
38*4a64e381SAndroid Build Coastguard Workerelse
39*4a64e381SAndroid Build Coastguard Worker    WAN_INTERFACE="eth0"
40*4a64e381SAndroid Build Coastguard Workerfi
41*4a64e381SAndroid Build Coastguard Worker
42*4a64e381SAndroid Build Coastguard WorkerWLAN_INTERFACE="wlan0"
43*4a64e381SAndroid Build Coastguard WorkerWPAN_INTERFACE="wpan0"
44*4a64e381SAndroid Build Coastguard Worker
45*4a64e381SAndroid Build Coastguard WorkerDHCPCD_CONF="/etc/dhcpcd.conf"
46*4a64e381SAndroid Build Coastguard WorkerDHCPCD_CONF_BACKUP="$DHCPCD_CONF.orig"
47*4a64e381SAndroid Build Coastguard Worker
48*4a64e381SAndroid Build Coastguard WorkerNCP_STATE_NOTIFIER="/usr/sbin/ncp_state_notifier"
49*4a64e381SAndroid Build Coastguard WorkerNCP_STATE_DISPATCHER="/etc/ncp_state_notifier/dispatcher.d"
50*4a64e381SAndroid Build Coastguard Worker
51*4a64e381SAndroid Build Coastguard WorkerNCP_STATE_NOTIFIER_SERVICE_NAME="ncp_state_notifier.service"
52*4a64e381SAndroid Build Coastguard WorkerNCP_STATE_NOTIFIER_SERVICE="/etc/systemd/system/${NCP_STATE_NOTIFIER_SERVICE_NAME}"
53*4a64e381SAndroid Build Coastguard Worker
54*4a64e381SAndroid Build Coastguard WorkerDHCPCD_RELOADER="${NCP_STATE_DISPATCHER}/dhcpcd_reloader"
55*4a64e381SAndroid Build Coastguard Worker
56*4a64e381SAndroid Build Coastguard Workerwithout DHCPV6_PD || test "$PLATFORM" = beagleboneblack || test "$PLATFORM" = raspbian || test "$PLATFORM" = ubuntu || die "DHCPv6-PD is not tested under $PLATFORM."
57*4a64e381SAndroid Build Coastguard Worker
58*4a64e381SAndroid Build Coastguard Workercreate_dhcpcd_conf_with_dhcpv6_pd()
59*4a64e381SAndroid Build Coastguard Worker{
60*4a64e381SAndroid Build Coastguard Worker    sudo tee ${DHCPCD_CONF} <<EOF
61*4a64e381SAndroid Build Coastguard Worker# A sample configuration for dhcpcd.
62*4a64e381SAndroid Build Coastguard Worker# See dhcpcd.conf(5) for details.
63*4a64e381SAndroid Build Coastguard Worker
64*4a64e381SAndroid Build Coastguard Worker# Allow users of this group to interact with dhcpcd via the control socket.
65*4a64e381SAndroid Build Coastguard Worker#controlgroup wheel
66*4a64e381SAndroid Build Coastguard Worker
67*4a64e381SAndroid Build Coastguard Worker# Inform the DHCP server of our hostname for DDNS.
68*4a64e381SAndroid Build Coastguard Workerhostname
69*4a64e381SAndroid Build Coastguard Worker
70*4a64e381SAndroid Build Coastguard Worker# Use the hardware address of the interface for the Client ID.
71*4a64e381SAndroid Build Coastguard Worker#clientid
72*4a64e381SAndroid Build Coastguard Worker# or
73*4a64e381SAndroid Build Coastguard Worker# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
74*4a64e381SAndroid Build Coastguard Workerduid
75*4a64e381SAndroid Build Coastguard Worker
76*4a64e381SAndroid Build Coastguard Worker# Rapid commit support.
77*4a64e381SAndroid Build Coastguard Worker# Safe to enable by default because it requires the equivalent option set
78*4a64e381SAndroid Build Coastguard Worker# on the server to actually work.
79*4a64e381SAndroid Build Coastguard Workeroption rapid_commit
80*4a64e381SAndroid Build Coastguard Worker
81*4a64e381SAndroid Build Coastguard Worker# A list of options to request from the DHCP server.
82*4a64e381SAndroid Build Coastguard Workeroption domain_name_servers, domain_name, domain_search, host_name
83*4a64e381SAndroid Build Coastguard Workeroption classless_static_routes
84*4a64e381SAndroid Build Coastguard Worker# Most distributions have NTP support.
85*4a64e381SAndroid Build Coastguard Workeroption ntp_servers
86*4a64e381SAndroid Build Coastguard Worker# Respect the network MTU.
87*4a64e381SAndroid Build Coastguard Worker# Some interface drivers reset when changing the MTU so disabled by default.
88*4a64e381SAndroid Build Coastguard Worker#option interface_mtu
89*4a64e381SAndroid Build Coastguard Worker
90*4a64e381SAndroid Build Coastguard Worker# A ServerID is required by RFC2131.
91*4a64e381SAndroid Build Coastguard Workerrequire dhcp_server_identifier
92*4a64e381SAndroid Build Coastguard Worker
93*4a64e381SAndroid Build Coastguard Worker# Generate Stable Private IPv6 Addresses instead of hardware based ones
94*4a64e381SAndroid Build Coastguard Workerslaac hwaddr
95*4a64e381SAndroid Build Coastguard Worker
96*4a64e381SAndroid Build Coastguard Worker# A hook script is provided to lookup the hostname if not set by the DHCP
97*4a64e381SAndroid Build Coastguard Worker# server, but it should not be run by default.
98*4a64e381SAndroid Build Coastguard Workernohook lookup-hostname
99*4a64e381SAndroid Build Coastguard Workernohook wpa_supplicant
100*4a64e381SAndroid Build Coastguard Worker
101*4a64e381SAndroid Build Coastguard Workerdenyinterfaces nat*
102*4a64e381SAndroid Build Coastguard Worker
103*4a64e381SAndroid Build Coastguard Workernoipv6rs
104*4a64e381SAndroid Build Coastguard Worker
105*4a64e381SAndroid Build Coastguard Workerinterface $WPAN_INTERFACE
106*4a64e381SAndroid Build Coastguard Workernodhcp
107*4a64e381SAndroid Build Coastguard Workernodhcp6
108*4a64e381SAndroid Build Coastguard Worker
109*4a64e381SAndroid Build Coastguard Workerinterface $WAN_INTERFACE
110*4a64e381SAndroid Build Coastguard Workeriaid 1
111*4a64e381SAndroid Build Coastguard Workeripv6rs
112*4a64e381SAndroid Build Coastguard Workeria_na 2
113*4a64e381SAndroid Build Coastguard Workeria_pd 3/::/63 $WPAN_INTERFACE/1
114*4a64e381SAndroid Build Coastguard Worker
115*4a64e381SAndroid Build Coastguard WorkerEOF
116*4a64e381SAndroid Build Coastguard Worker
117*4a64e381SAndroid Build Coastguard Worker    if [ "$PLATFORM" = "raspbian" ] || with NETWORK_MANAGER_WIFI; then
118*4a64e381SAndroid Build Coastguard Worker        sudo tee -a ${DHCPCD_CONF} <<EOF
119*4a64e381SAndroid Build Coastguard Workerinterface $WLAN_INTERFACE
120*4a64e381SAndroid Build Coastguard Workeriaid 4
121*4a64e381SAndroid Build Coastguard Workeripv6rs
122*4a64e381SAndroid Build Coastguard Workeria_na 5
123*4a64e381SAndroid Build Coastguard Workeria_pd 6/::/63 $WPAN_INTERFACE/1
124*4a64e381SAndroid Build Coastguard WorkerEOF
125*4a64e381SAndroid Build Coastguard Worker    fi
126*4a64e381SAndroid Build Coastguard Worker}
127*4a64e381SAndroid Build Coastguard Worker
128*4a64e381SAndroid Build Coastguard Workercreate_ncp_state_notifier_script()
129*4a64e381SAndroid Build Coastguard Worker{
130*4a64e381SAndroid Build Coastguard Worker    sudo tee ${NCP_STATE_NOTIFIER} <<EOF
131*4a64e381SAndroid Build Coastguard Worker#!/bin/sh
132*4a64e381SAndroid Build Coastguard Worker#
133*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2017, The OpenThread Authors.
134*4a64e381SAndroid Build Coastguard Worker#  All rights reserved.
135*4a64e381SAndroid Build Coastguard Worker#
136*4a64e381SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
137*4a64e381SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
138*4a64e381SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
139*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
140*4a64e381SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
141*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
142*4a64e381SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
143*4a64e381SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
144*4a64e381SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
145*4a64e381SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
146*4a64e381SAndroid Build Coastguard Worker#
147*4a64e381SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
148*4a64e381SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
149*4a64e381SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
150*4a64e381SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
151*4a64e381SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
152*4a64e381SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
153*4a64e381SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
154*4a64e381SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
155*4a64e381SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
156*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
157*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
158*4a64e381SAndroid Build Coastguard Worker#
159*4a64e381SAndroid Build Coastguard Worker#   Description:
160*4a64e381SAndroid Build Coastguard Worker#       This script notifies about NCP state changes.
161*4a64e381SAndroid Build Coastguard Worker#
162*4a64e381SAndroid Build Coastguard Worker
163*4a64e381SAndroid Build Coastguard Workerset -euxo pipefail
164*4a64e381SAndroid Build Coastguard Worker
165*4a64e381SAndroid Build Coastguard WorkerPID=\$\$
166*4a64e381SAndroid Build Coastguard WorkerNAME="ncp_state_notifier"
167*4a64e381SAndroid Build Coastguard Worker
168*4a64e381SAndroid Build Coastguard WorkerDISPATCHER_PATH="${NCP_STATE_DISPATCHER}"
169*4a64e381SAndroid Build Coastguard Worker
170*4a64e381SAndroid Build Coastguard Workerecho \${PID} > "/tmp/\${NAME}.pid"
171*4a64e381SAndroid Build Coastguard Worker
172*4a64e381SAndroid Build Coastguard Workerif [ -z \${IFACE} ]; then
173*4a64e381SAndroid Build Coastguard Worker    IFACE=${WPAN_INTERFACE}
174*4a64e381SAndroid Build Coastguard Workerfi
175*4a64e381SAndroid Build Coastguard Worker
176*4a64e381SAndroid Build Coastguard WorkerOTBR_PATH="/io/openthread/BorderRouter/\${IFACE}"
177*4a64e381SAndroid Build Coastguard Worker
178*4a64e381SAndroid Build Coastguard WorkerWATCH1="type='signal', interface=org.freedesktop.DBus.Properties, path=\${OTBR_PATH}, member='PropertiesChanged'"
179*4a64e381SAndroid Build Coastguard Worker
180*4a64e381SAndroid Build Coastguard Workernotify_about_state()
181*4a64e381SAndroid Build Coastguard Worker{
182*4a64e381SAndroid Build Coastguard Worker    logger -t "\${NAME}[\${PID}]" "Notifying about change state to: \${1} on the interface: \${2}"
183*4a64e381SAndroid Build Coastguard Worker
184*4a64e381SAndroid Build Coastguard Worker    for SCRIPT in \`find \${DISPATCHER_PATH} -type f\`; do
185*4a64e381SAndroid Build Coastguard Worker        logger -t "\${NAME}[\${PID}]" "Running script: \${SCRIPT}"
186*4a64e381SAndroid Build Coastguard Worker        .\${SCRIPT} \${1} \${2}
187*4a64e381SAndroid Build Coastguard Worker    done
188*4a64e381SAndroid Build Coastguard Worker}
189*4a64e381SAndroid Build Coastguard Worker
190*4a64e381SAndroid Build Coastguard Workerprocess_output()
191*4a64e381SAndroid Build Coastguard Worker{
192*4a64e381SAndroid Build Coastguard Worker    local NEXT_LINE_IS_STATE=0
193*4a64e381SAndroid Build Coastguard Worker
194*4a64e381SAndroid Build Coastguard Worker    while read -r LINE; do
195*4a64e381SAndroid Build Coastguard Worker        if echo \${LINE} | grep -q "NCP:State"; then
196*4a64e381SAndroid Build Coastguard Worker            NEXT_LINE_IS_STATE=1
197*4a64e381SAndroid Build Coastguard Worker            continue
198*4a64e381SAndroid Build Coastguard Worker        fi
199*4a64e381SAndroid Build Coastguard Worker
200*4a64e381SAndroid Build Coastguard Worker        if [ \${NEXT_LINE_IS_STATE} -eq 1 ]; then
201*4a64e381SAndroid Build Coastguard Worker            NEXT_LINE_IS_STATE=1
202*4a64e381SAndroid Build Coastguard Worker
203*4a64e381SAndroid Build Coastguard Worker            STATE=\`echo \${LINE} | cut -d'"' -f2\`
204*4a64e381SAndroid Build Coastguard Worker            notify_about_state \${STATE} \${IFACE}
205*4a64e381SAndroid Build Coastguard Worker        fi
206*4a64e381SAndroid Build Coastguard Worker    done
207*4a64e381SAndroid Build Coastguard Worker}
208*4a64e381SAndroid Build Coastguard Worker
209*4a64e381SAndroid Build Coastguard Workerdbus-monitor --system "\${WATCH1}" | process_output
210*4a64e381SAndroid Build Coastguard WorkerEOF
211*4a64e381SAndroid Build Coastguard Worker}
212*4a64e381SAndroid Build Coastguard Worker
213*4a64e381SAndroid Build Coastguard Workercreate_dhcpcd_reloader_script()
214*4a64e381SAndroid Build Coastguard Worker{
215*4a64e381SAndroid Build Coastguard Worker    sudo tee ${DHCPCD_RELOADER} <<EOF
216*4a64e381SAndroid Build Coastguard Worker#!/bin/sh
217*4a64e381SAndroid Build Coastguard Worker#
218*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2017, The OpenThread Authors.
219*4a64e381SAndroid Build Coastguard Worker#  All rights reserved.
220*4a64e381SAndroid Build Coastguard Worker#
221*4a64e381SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
222*4a64e381SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
223*4a64e381SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
224*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
225*4a64e381SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
226*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
227*4a64e381SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
228*4a64e381SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
229*4a64e381SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
230*4a64e381SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
231*4a64e381SAndroid Build Coastguard Worker#
232*4a64e381SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
233*4a64e381SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234*4a64e381SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
235*4a64e381SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
236*4a64e381SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
237*4a64e381SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
238*4a64e381SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
239*4a64e381SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
240*4a64e381SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
241*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
242*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
243*4a64e381SAndroid Build Coastguard Worker#
244*4a64e381SAndroid Build Coastguard Worker#   Description:
245*4a64e381SAndroid Build Coastguard Worker#       This script reloads dhcpcd.
246*4a64e381SAndroid Build Coastguard Worker#
247*4a64e381SAndroid Build Coastguard Worker
248*4a64e381SAndroid Build Coastguard WorkerPID=\$\$
249*4a64e381SAndroid Build Coastguard WorkerNAME="dhcpcd_reloader"
250*4a64e381SAndroid Build Coastguard Worker
251*4a64e381SAndroid Build Coastguard WorkerDHCPCD_INTERFACES="/tmp/dhcpcd_interfaces"
252*4a64e381SAndroid Build Coastguard Worker
253*4a64e381SAndroid Build Coastguard WorkerSTATE=\$1
254*4a64e381SAndroid Build Coastguard Worker
255*4a64e381SAndroid Build Coastguard Workerif [ \${STATE} = "associated" ]; then
256*4a64e381SAndroid Build Coastguard Worker    if systemctl is-active NetworkManager; then
257*4a64e381SAndroid Build Coastguard Worker        logger -t "\${NAME}[\${PID}]" "NetworkManager: active"
258*4a64e381SAndroid Build Coastguard Worker
259*4a64e381SAndroid Build Coastguard Worker        if ! [ -f \${DHCPCD_INTERFACES} ]; then
260*4a64e381SAndroid Build Coastguard Worker            exit 1
261*4a64e381SAndroid Build Coastguard Worker        fi
262*4a64e381SAndroid Build Coastguard Worker
263*4a64e381SAndroid Build Coastguard Worker        for interface in \`cat \${DHCPCD_INTERFACES}\`; do
264*4a64e381SAndroid Build Coastguard Worker            logger -t "\${NAME}[\${PID}]" "dhcpcd: rebind on the interface: \${interface}"
265*4a64e381SAndroid Build Coastguard Worker            /sbin/dhcpcd -6 -n \${interface}
266*4a64e381SAndroid Build Coastguard Worker        done
267*4a64e381SAndroid Build Coastguard Worker    fi
268*4a64e381SAndroid Build Coastguard Worker
269*4a64e381SAndroid Build Coastguard Worker    if systemctl is-active dhcpcd; then
270*4a64e381SAndroid Build Coastguard Worker        logger -t "\${NAME}[\${PID}]" "dhcpcd: reload"
271*4a64e381SAndroid Build Coastguard Worker
272*4a64e381SAndroid Build Coastguard Worker        sudo systemctl reload-daemon
273*4a64e381SAndroid Build Coastguard Worker        sudo systemctl force-reload dhcpcd
274*4a64e381SAndroid Build Coastguard Worker    fi
275*4a64e381SAndroid Build Coastguard Workerfi
276*4a64e381SAndroid Build Coastguard WorkerEOF
277*4a64e381SAndroid Build Coastguard Worker}
278*4a64e381SAndroid Build Coastguard Worker
279*4a64e381SAndroid Build Coastguard Workercreate_ncp_state_notifier_service()
280*4a64e381SAndroid Build Coastguard Worker{
281*4a64e381SAndroid Build Coastguard Worker    sudo tee ${NCP_STATE_NOTIFIER_SERVICE} <<EOF
282*4a64e381SAndroid Build Coastguard Worker[Unit]
283*4a64e381SAndroid Build Coastguard WorkerDescription=Daemon call scripts on every NCP state change
284*4a64e381SAndroid Build Coastguard WorkerAfter=otbr-agent.service
285*4a64e381SAndroid Build Coastguard WorkerConditionPathExists=${NCP_STATE_NOTIFIER}
286*4a64e381SAndroid Build Coastguard Worker
287*4a64e381SAndroid Build Coastguard Worker[Service]
288*4a64e381SAndroid Build Coastguard WorkerExecStart=/bin/sh ${NCP_STATE_NOTIFIER}
289*4a64e381SAndroid Build Coastguard WorkerPIDFile=/tmp/ncp_state_notifier.pid
290*4a64e381SAndroid Build Coastguard WorkerKillMode=control-group
291*4a64e381SAndroid Build Coastguard Worker
292*4a64e381SAndroid Build Coastguard Worker[Install]
293*4a64e381SAndroid Build Coastguard WorkerWantedBy=multi-user.target
294*4a64e381SAndroid Build Coastguard WorkerEOF
295*4a64e381SAndroid Build Coastguard Worker}
296*4a64e381SAndroid Build Coastguard Worker
297*4a64e381SAndroid Build Coastguard Workerdhcpv6_pd_install()
298*4a64e381SAndroid Build Coastguard Worker{
299*4a64e381SAndroid Build Coastguard Worker    with DHCPV6_PD || return 0
300*4a64e381SAndroid Build Coastguard Worker
301*4a64e381SAndroid Build Coastguard Worker    # Create backup of the default configuration of dhcpcd
302*4a64e381SAndroid Build Coastguard Worker    sudo mv ${DHCPCD_CONF} ${DHCPCD_CONF_BACKUP}
303*4a64e381SAndroid Build Coastguard Worker
304*4a64e381SAndroid Build Coastguard Worker    create_dhcpcd_conf_with_dhcpv6_pd
305*4a64e381SAndroid Build Coastguard Worker
306*4a64e381SAndroid Build Coastguard Worker    create_ncp_state_notifier_script
307*4a64e381SAndroid Build Coastguard Worker    sudo chmod +x ${NCP_STATE_NOTIFIER}
308*4a64e381SAndroid Build Coastguard Worker
309*4a64e381SAndroid Build Coastguard Worker    sudo mkdir -p ${NCP_STATE_DISPATCHER}
310*4a64e381SAndroid Build Coastguard Worker
311*4a64e381SAndroid Build Coastguard Worker    create_dhcpcd_reloader_script
312*4a64e381SAndroid Build Coastguard Worker    sudo chmod +x ${DHCPCD_RELOADER}
313*4a64e381SAndroid Build Coastguard Worker
314*4a64e381SAndroid Build Coastguard Worker    if have systemctl; then
315*4a64e381SAndroid Build Coastguard Worker        create_ncp_state_notifier_service
316*4a64e381SAndroid Build Coastguard Worker
317*4a64e381SAndroid Build Coastguard Worker        sudo systemctl daemon-reload
318*4a64e381SAndroid Build Coastguard Worker
319*4a64e381SAndroid Build Coastguard Worker        if systemctl is-active NetworkManager; then
320*4a64e381SAndroid Build Coastguard Worker            sudo systemctl restart NetworkManager || die "Unable to restart NetworkManager!"
321*4a64e381SAndroid Build Coastguard Worker        fi
322*4a64e381SAndroid Build Coastguard Worker
323*4a64e381SAndroid Build Coastguard Worker        if systemctl is-active dhcpcd; then
324*4a64e381SAndroid Build Coastguard Worker            sudo systemctl restart dhcpcd || die 'Unable to restart dhcpcd!'
325*4a64e381SAndroid Build Coastguard Worker        fi
326*4a64e381SAndroid Build Coastguard Worker
327*4a64e381SAndroid Build Coastguard Worker        sudo systemctl start ${NCP_STATE_NOTIFIER_SERVICE_NAME} || die "Unable to start ${NCP_STATE_NOTIFIER_SERVICE_NAME}!"
328*4a64e381SAndroid Build Coastguard Worker        sudo systemctl enable ${NCP_STATE_NOTIFIER_SERVICE_NAME} || die "Unable to enable ${NCP_STATE_NOTIFIER_SERVICE_NAME}!"
329*4a64e381SAndroid Build Coastguard Worker    fi
330*4a64e381SAndroid Build Coastguard Worker}
331*4a64e381SAndroid Build Coastguard Worker
332*4a64e381SAndroid Build Coastguard Workerdhcpv6_pd_uninstall()
333*4a64e381SAndroid Build Coastguard Worker{
334*4a64e381SAndroid Build Coastguard Worker    with DHCPV6_PD || return 0
335*4a64e381SAndroid Build Coastguard Worker
336*4a64e381SAndroid Build Coastguard Worker    if have systemctl; then
337*4a64e381SAndroid Build Coastguard Worker        sudo systemctl disable ${NCP_STATE_NOTIFIER_SERVICE_NAME} || true
338*4a64e381SAndroid Build Coastguard Worker        sudo systemctl stop ${NCP_STATE_NOTIFIER_SERVICE_NAME} || true
339*4a64e381SAndroid Build Coastguard Worker
340*4a64e381SAndroid Build Coastguard Worker        sudo rm ${NCP_STATE_NOTIFIER_SERVICE} || true
341*4a64e381SAndroid Build Coastguard Worker    fi
342*4a64e381SAndroid Build Coastguard Worker
343*4a64e381SAndroid Build Coastguard Worker    sudo rm ${NCP_STATE_NOTIFIER} || true
344*4a64e381SAndroid Build Coastguard Worker    sudo rm ${DHCPCD_RELOADER} || true
345*4a64e381SAndroid Build Coastguard Worker
346*4a64e381SAndroid Build Coastguard Worker    sudo rm -r ${NCP_STATE_DISPATCHER} || true
347*4a64e381SAndroid Build Coastguard Worker
348*4a64e381SAndroid Build Coastguard Worker    # Restore backup of the default configuration of dhcpcd
349*4a64e381SAndroid Build Coastguard Worker    sudo mv ${DHCPCD_CONF_BACKUP} ${DHCPCD_CONF} || true
350*4a64e381SAndroid Build Coastguard Worker
351*4a64e381SAndroid Build Coastguard Worker    # Restart dhcpcd or NetworkManager
352*4a64e381SAndroid Build Coastguard Worker    if have systemctl; then
353*4a64e381SAndroid Build Coastguard Worker        sudo systemctl daemon-reload
354*4a64e381SAndroid Build Coastguard Worker
355*4a64e381SAndroid Build Coastguard Worker        if systemctl is-active NetworkManager; then
356*4a64e381SAndroid Build Coastguard Worker            sudo systemctl restart NetworkManager || true
357*4a64e381SAndroid Build Coastguard Worker        fi
358*4a64e381SAndroid Build Coastguard Worker
359*4a64e381SAndroid Build Coastguard Worker        if systemctl is-active dhcpcd; then
360*4a64e381SAndroid Build Coastguard Worker            sudo systemctl restart dhcpcd || true
361*4a64e381SAndroid Build Coastguard Worker        fi
362*4a64e381SAndroid Build Coastguard Worker    fi
363*4a64e381SAndroid Build Coastguard Worker}
364