xref: /aosp_15_r20/external/ot-br-posix/script/_network_manager (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
30*4a64e381SAndroid Build Coastguard WorkerAP_CONN="BorderRouter-AP"
31*4a64e381SAndroid Build Coastguard WorkerETH_CONN="BorderRouter-Eth"
32*4a64e381SAndroid Build Coastguard Worker
33*4a64e381SAndroid Build Coastguard WorkerAP_HELPER_SCRIPT="/etc/NetworkManager/dispatcher.d/ap-helper"
34*4a64e381SAndroid Build Coastguard WorkerDHCPV6_HELPER_SCRIPT="/etc/NetworkManager/dispatcher.d/dhcpv6-helper"
35*4a64e381SAndroid Build Coastguard Worker
36*4a64e381SAndroid Build Coastguard Workercreate_ap_connection()
37*4a64e381SAndroid Build Coastguard Worker{
38*4a64e381SAndroid Build Coastguard Worker    IFNAME=$(nmcli d | grep wifi | cut -d" " -f1)
39*4a64e381SAndroid Build Coastguard Worker
40*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c add type wifi ifname "${IFNAME}" con-name ${AP_CONN} ssid ${AP_CONN}
41*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c modify ${AP_CONN} 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared ipv6.method auto
42*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c modify ${AP_CONN} wifi-sec.key-mgmt wpa-psk
43*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c modify ${AP_CONN} wifi-sec.proto rsn
44*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c modify ${AP_CONN} wifi-sec.psk "12345678"
45*4a64e381SAndroid Build Coastguard Worker}
46*4a64e381SAndroid Build Coastguard Worker
47*4a64e381SAndroid Build Coastguard Workercreate_eth_connection()
48*4a64e381SAndroid Build Coastguard Worker{
49*4a64e381SAndroid Build Coastguard Worker    IFNAME=$(nmcli d | grep ethernet | cut -d" " -f1 | grep -v usb)
50*4a64e381SAndroid Build Coastguard Worker
51*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c add type ethernet ifname "${IFNAME}" con-name ${ETH_CONN}
52*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c modify ${ETH_CONN} ipv6.method ignore
53*4a64e381SAndroid Build Coastguard Worker}
54*4a64e381SAndroid Build Coastguard Worker
55*4a64e381SAndroid Build Coastguard Workercreate_ap_helper_script()
56*4a64e381SAndroid Build Coastguard Worker{
57*4a64e381SAndroid Build Coastguard Worker    sudo tee ${AP_HELPER_SCRIPT} <<EOF
58*4a64e381SAndroid Build Coastguard Worker#!/bin/sh
59*4a64e381SAndroid Build Coastguard Worker#
60*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2017, The OpenThread Authors.
61*4a64e381SAndroid Build Coastguard Worker#  All rights reserved.
62*4a64e381SAndroid Build Coastguard Worker#
63*4a64e381SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
64*4a64e381SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
65*4a64e381SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
66*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
67*4a64e381SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
68*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
69*4a64e381SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
70*4a64e381SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
71*4a64e381SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
72*4a64e381SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
73*4a64e381SAndroid Build Coastguard Worker#
74*4a64e381SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
75*4a64e381SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76*4a64e381SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77*4a64e381SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
78*4a64e381SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
79*4a64e381SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
80*4a64e381SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
81*4a64e381SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
82*4a64e381SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
83*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
84*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
85*4a64e381SAndroid Build Coastguard Worker#
86*4a64e381SAndroid Build Coastguard Worker
87*4a64e381SAndroid Build Coastguard Workerset -euxo pipefail
88*4a64e381SAndroid Build Coastguard Worker
89*4a64e381SAndroid Build Coastguard WorkerNAME="ap-helper"
90*4a64e381SAndroid Build Coastguard Worker
91*4a64e381SAndroid Build Coastguard WorkerIFNAME=\$1
92*4a64e381SAndroid Build Coastguard WorkerACTION=\$2
93*4a64e381SAndroid Build Coastguard Worker
94*4a64e381SAndroid Build Coastguard WorkerAP_CONN="${AP_CONN}"
95*4a64e381SAndroid Build Coastguard Worker
96*4a64e381SAndroid Build Coastguard WorkerDHCP_START="10.42.0.2"
97*4a64e381SAndroid Build Coastguard WorkerDHCP_END="10.42.0.8"
98*4a64e381SAndroid Build Coastguard Worker
99*4a64e381SAndroid Build Coastguard WorkerROUTER_IP="10.42.0.1"
100*4a64e381SAndroid Build Coastguard Worker
101*4a64e381SAndroid Build Coastguard WorkerDNS1=\${ROUTER_IP}
102*4a64e381SAndroid Build Coastguard WorkerDNS2="8.8.8.8"
103*4a64e381SAndroid Build Coastguard Worker
104*4a64e381SAndroid Build Coastguard Worker
105*4a64e381SAndroid Build Coastguard Workerlog()
106*4a64e381SAndroid Build Coastguard Worker{
107*4a64e381SAndroid Build Coastguard Worker    logger -t "\${NAME}[\${\$}]" \$*
108*4a64e381SAndroid Build Coastguard Worker}
109*4a64e381SAndroid Build Coastguard Worker
110*4a64e381SAndroid Build Coastguard Workerdisable_accept_ra()
111*4a64e381SAndroid Build Coastguard Worker{
112*4a64e381SAndroid Build Coastguard Worker    log "Disable accepting Router Advertisements on the interface: '\${IFNAME}'"
113*4a64e381SAndroid Build Coastguard Worker    sysctl -w net.ipv6.conf.\${IFNAME}.accept_ra=1
114*4a64e381SAndroid Build Coastguard Worker}
115*4a64e381SAndroid Build Coastguard Worker
116*4a64e381SAndroid Build Coastguard Workerstart_dnsmasq()
117*4a64e381SAndroid Build Coastguard Worker{
118*4a64e381SAndroid Build Coastguard Worker    log "Starting 'dnsmasq' on the interface: '\${IFNAME}'"
119*4a64e381SAndroid Build Coastguard Worker    /usr/sbin/dnsmasq -i \${IFNAME} -a \${ROUTER_IP} -b -z -K -F\${DHCP_START},\${DHCP_END},24h -p0 -O3,\${ROUTER_IP} -O6,\${DNS1},\${DNS2}
120*4a64e381SAndroid Build Coastguard Worker}
121*4a64e381SAndroid Build Coastguard Worker
122*4a64e381SAndroid Build Coastguard Workerkill_dnsmasq()
123*4a64e381SAndroid Build Coastguard Worker{
124*4a64e381SAndroid Build Coastguard Worker    local DNSMASQ_PID=\`pidof dnsmasq\`
125*4a64e381SAndroid Build Coastguard Worker
126*4a64e381SAndroid Build Coastguard Worker    if [ -n \${DNSMASQ_PID} ]; then
127*4a64e381SAndroid Build Coastguard Worker        log "Killing 'dnsmasq' process with PID: '\${DNSMASQ_PID}'"
128*4a64e381SAndroid Build Coastguard Worker        kill -9 \${DNSMASQ_PID}
129*4a64e381SAndroid Build Coastguard Worker    else
130*4a64e381SAndroid Build Coastguard Worker        log "'dnsmasq' is not running"
131*4a64e381SAndroid Build Coastguard Worker    fi
132*4a64e381SAndroid Build Coastguard Worker}
133*4a64e381SAndroid Build Coastguard Worker
134*4a64e381SAndroid Build Coastguard Workerrelease_dhcpcd()
135*4a64e381SAndroid Build Coastguard Worker{
136*4a64e381SAndroid Build Coastguard Worker    log "Releasing 'dhcpcd' on the interface: '\${IFNAME}'"
137*4a64e381SAndroid Build Coastguard Worker    /sbin/dhcpcd -6 -k \${IFNAME}
138*4a64e381SAndroid Build Coastguard Worker}
139*4a64e381SAndroid Build Coastguard Worker
140*4a64e381SAndroid Build Coastguard Workerhandle_action_up()
141*4a64e381SAndroid Build Coastguard Worker{
142*4a64e381SAndroid Build Coastguard Worker    case \${IFNAME} in
143*4a64e381SAndroid Build Coastguard Worker    wlan*)
144*4a64e381SAndroid Build Coastguard Worker        if [ \${CONNECTION_ID} = \${AP_CONN} ]; then
145*4a64e381SAndroid Build Coastguard Worker            release_dhcpcd
146*4a64e381SAndroid Build Coastguard Worker            disable_accept_ra
147*4a64e381SAndroid Build Coastguard Worker            start_dnsmasq
148*4a64e381SAndroid Build Coastguard Worker        fi
149*4a64e381SAndroid Build Coastguard Worker        ;;
150*4a64e381SAndroid Build Coastguard Worker    *)
151*4a64e381SAndroid Build Coastguard Worker        ;;
152*4a64e381SAndroid Build Coastguard Worker    esac
153*4a64e381SAndroid Build Coastguard Worker}
154*4a64e381SAndroid Build Coastguard Worker
155*4a64e381SAndroid Build Coastguard Workerhandle_action_down()
156*4a64e381SAndroid Build Coastguard Worker{
157*4a64e381SAndroid Build Coastguard Worker    case \${IFNAME} in
158*4a64e381SAndroid Build Coastguard Worker    wlan*)
159*4a64e381SAndroid Build Coastguard Worker        if [ \${CONNECTION_ID} = \${AP_CONN} ]; then
160*4a64e381SAndroid Build Coastguard Worker            kill_dnsmasq
161*4a64e381SAndroid Build Coastguard Worker        fi
162*4a64e381SAndroid Build Coastguard Worker        ;;
163*4a64e381SAndroid Build Coastguard Worker    *)
164*4a64e381SAndroid Build Coastguard Worker        log "Skipping action: '\${ACTION}' on the interface: '\${IFNAME}'"
165*4a64e381SAndroid Build Coastguard Worker        ;;
166*4a64e381SAndroid Build Coastguard Worker    esac
167*4a64e381SAndroid Build Coastguard Worker}
168*4a64e381SAndroid Build Coastguard Worker
169*4a64e381SAndroid Build Coastguard Worker
170*4a64e381SAndroid Build Coastguard Workercase \${ACTION} in
171*4a64e381SAndroid Build Coastguard Workerup)
172*4a64e381SAndroid Build Coastguard Worker    handle_action_up
173*4a64e381SAndroid Build Coastguard Worker    ;;
174*4a64e381SAndroid Build Coastguard Workerdown)
175*4a64e381SAndroid Build Coastguard Worker    handle_action_down
176*4a64e381SAndroid Build Coastguard Worker    ;;
177*4a64e381SAndroid Build Coastguard Worker*)
178*4a64e381SAndroid Build Coastguard Worker    log "Unsupported action: '\${ACTION}'"
179*4a64e381SAndroid Build Coastguard Worker    ;;
180*4a64e381SAndroid Build Coastguard Workeresac
181*4a64e381SAndroid Build Coastguard WorkerEOF
182*4a64e381SAndroid Build Coastguard Worker}
183*4a64e381SAndroid Build Coastguard Worker
184*4a64e381SAndroid Build Coastguard Workercreate_dhcpv6_helper_script()
185*4a64e381SAndroid Build Coastguard Worker{
186*4a64e381SAndroid Build Coastguard Worker    sudo tee ${DHCPV6_HELPER_SCRIPT} <<EOF
187*4a64e381SAndroid Build Coastguard Worker#!/bin/sh
188*4a64e381SAndroid Build Coastguard Worker#
189*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2017, The OpenThread Authors.
190*4a64e381SAndroid Build Coastguard Worker#  All rights reserved.
191*4a64e381SAndroid Build Coastguard Worker#
192*4a64e381SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
193*4a64e381SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
194*4a64e381SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
195*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
196*4a64e381SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
197*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
198*4a64e381SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
199*4a64e381SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
200*4a64e381SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
201*4a64e381SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
202*4a64e381SAndroid Build Coastguard Worker#
203*4a64e381SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
204*4a64e381SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
205*4a64e381SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206*4a64e381SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
207*4a64e381SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
208*4a64e381SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
209*4a64e381SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
210*4a64e381SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
211*4a64e381SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
212*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
213*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
214*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
215*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
216*4a64e381SAndroid Build Coastguard Worker#
217*4a64e381SAndroid Build Coastguard Worker#   Description:
218*4a64e381SAndroid Build Coastguard Worker#       This script manipulates DHCPv6-PD configuration.
219*4a64e381SAndroid Build Coastguard Worker#
220*4a64e381SAndroid Build Coastguard Worker
221*4a64e381SAndroid Build Coastguard Workerset -euxo pipefail
222*4a64e381SAndroid Build Coastguard Worker
223*4a64e381SAndroid Build Coastguard WorkerNAME="dhcpv6-helper"
224*4a64e381SAndroid Build Coastguard Worker
225*4a64e381SAndroid Build Coastguard WorkerIFNAME=\$1
226*4a64e381SAndroid Build Coastguard WorkerACTION=\$2
227*4a64e381SAndroid Build Coastguard Worker
228*4a64e381SAndroid Build Coastguard WorkerAP_CONN="${AP_CONN}"
229*4a64e381SAndroid Build Coastguard Worker
230*4a64e381SAndroid Build Coastguard WorkerDHCPCD_INTERFACES="/tmp/dhcpcd_interfaces"
231*4a64e381SAndroid Build Coastguard Worker
232*4a64e381SAndroid Build Coastguard Worker
233*4a64e381SAndroid Build Coastguard Workerlog()
234*4a64e381SAndroid Build Coastguard Worker{
235*4a64e381SAndroid Build Coastguard Worker    logger -t "\${NAME}[\${\$}]" \$*
236*4a64e381SAndroid Build Coastguard Worker}
237*4a64e381SAndroid Build Coastguard Worker
238*4a64e381SAndroid Build Coastguard Workerenable_accept_ra()
239*4a64e381SAndroid Build Coastguard Worker{
240*4a64e381SAndroid Build Coastguard Worker    log "Enable accepting Router Advertisements on the interface: '\${IFNAME}'"
241*4a64e381SAndroid Build Coastguard Worker    sysctl -w net.ipv6.conf.\${IFNAME}.accept_ra=2
242*4a64e381SAndroid Build Coastguard Worker}
243*4a64e381SAndroid Build Coastguard Worker
244*4a64e381SAndroid Build Coastguard Workerkill_dnsmasq()
245*4a64e381SAndroid Build Coastguard Worker{
246*4a64e381SAndroid Build Coastguard Worker    local DNSMASQ_PID=\`pidof dnsmasq\`
247*4a64e381SAndroid Build Coastguard Worker
248*4a64e381SAndroid Build Coastguard Worker    log "Killing 'dnsmasq' process with PID: '\${DNSMASQ_PID}'"
249*4a64e381SAndroid Build Coastguard Worker    kill -9 \${DNSMASQ_PID}
250*4a64e381SAndroid Build Coastguard Worker}
251*4a64e381SAndroid Build Coastguard Worker
252*4a64e381SAndroid Build Coastguard Workerstart_dhcpcd()
253*4a64e381SAndroid Build Coastguard Worker{
254*4a64e381SAndroid Build Coastguard Worker    log "Starting 'dhcpcd' on the interface: '\${IFNAME}'"
255*4a64e381SAndroid Build Coastguard Worker    /sbin/dhcpcd -6 -b -K -E \${IFNAME}
256*4a64e381SAndroid Build Coastguard Worker
257*4a64e381SAndroid Build Coastguard Worker    # Add interface to active dhcpcd interfaces.
258*4a64e381SAndroid Build Coastguard Worker    sed -i "/\${IFNAME}/d" \${DHCPCD_INTERFACES}
259*4a64e381SAndroid Build Coastguard Worker    echo "\${IFNAME}" >> \${DHCPCD_INTERFACES}
260*4a64e381SAndroid Build Coastguard Worker}
261*4a64e381SAndroid Build Coastguard Worker
262*4a64e381SAndroid Build Coastguard Workerrelease_dhcpcd()
263*4a64e381SAndroid Build Coastguard Worker{
264*4a64e381SAndroid Build Coastguard Worker    log "Releasing 'dhcpcd' on the interface: '\${IFNAME}'"
265*4a64e381SAndroid Build Coastguard Worker    /sbin/dhcpcd -6 -k \${IFNAME}
266*4a64e381SAndroid Build Coastguard Worker
267*4a64e381SAndroid Build Coastguard Worker    # Remove interface from active dhcpcd interfaces.
268*4a64e381SAndroid Build Coastguard Worker    sed -i "/\${IFNAME}/d" \${DHCPCD_INTERFACES}
269*4a64e381SAndroid Build Coastguard Worker}
270*4a64e381SAndroid Build Coastguard Worker
271*4a64e381SAndroid Build Coastguard Workerhandle_action_up()
272*4a64e381SAndroid Build Coastguard Worker{
273*4a64e381SAndroid Build Coastguard Worker    case \${IFNAME} in
274*4a64e381SAndroid Build Coastguard Worker    enp*)
275*4a64e381SAndroid Build Coastguard Worker        enable_accept_ra
276*4a64e381SAndroid Build Coastguard Worker        start_dhcpcd
277*4a64e381SAndroid Build Coastguard Worker        ;;
278*4a64e381SAndroid Build Coastguard Worker    eth*)
279*4a64e381SAndroid Build Coastguard Worker        enable_accept_ra
280*4a64e381SAndroid Build Coastguard Worker        start_dhcpcd
281*4a64e381SAndroid Build Coastguard Worker        ;;
282*4a64e381SAndroid Build Coastguard Worker    wlan*)
283*4a64e381SAndroid Build Coastguard Worker        if ! [ \${CONNECTION_ID} = \${AP_CONN} ]; then
284*4a64e381SAndroid Build Coastguard Worker            enable_accept_ra
285*4a64e381SAndroid Build Coastguard Worker            start_dhcpcd
286*4a64e381SAndroid Build Coastguard Worker        fi
287*4a64e381SAndroid Build Coastguard Worker        ;;
288*4a64e381SAndroid Build Coastguard Worker    *)
289*4a64e381SAndroid Build Coastguard Worker        ;;
290*4a64e381SAndroid Build Coastguard Worker    esac
291*4a64e381SAndroid Build Coastguard Worker
292*4a64e381SAndroid Build Coastguard Worker}
293*4a64e381SAndroid Build Coastguard Worker
294*4a64e381SAndroid Build Coastguard Workerhandle_action_down()
295*4a64e381SAndroid Build Coastguard Worker{
296*4a64e381SAndroid Build Coastguard Worker    case \${IFNAME} in
297*4a64e381SAndroid Build Coastguard Worker    enp*)
298*4a64e381SAndroid Build Coastguard Worker        release_dhcpcd
299*4a64e381SAndroid Build Coastguard Worker        ;;
300*4a64e381SAndroid Build Coastguard Worker    eth*)
301*4a64e381SAndroid Build Coastguard Worker        release_dhcpcd
302*4a64e381SAndroid Build Coastguard Worker        ;;
303*4a64e381SAndroid Build Coastguard Worker    wlan*)
304*4a64e381SAndroid Build Coastguard Worker        if ! [ \${CONNECTION_ID} = \${AP_CONN} ]; then
305*4a64e381SAndroid Build Coastguard Worker            release_dhcpcd
306*4a64e381SAndroid Build Coastguard Worker        fi
307*4a64e381SAndroid Build Coastguard Worker        ;;
308*4a64e381SAndroid Build Coastguard Worker    *)
309*4a64e381SAndroid Build Coastguard Worker        log "Skipping action: '\${ACTION}' on the interface: '\${IFNAME}'"
310*4a64e381SAndroid Build Coastguard Worker        ;;
311*4a64e381SAndroid Build Coastguard Worker    esac
312*4a64e381SAndroid Build Coastguard Worker}
313*4a64e381SAndroid Build Coastguard Worker
314*4a64e381SAndroid Build Coastguard Workercase \${ACTION} in
315*4a64e381SAndroid Build Coastguard Workerup)
316*4a64e381SAndroid Build Coastguard Worker    handle_action_up
317*4a64e381SAndroid Build Coastguard Worker    ;;
318*4a64e381SAndroid Build Coastguard Workerdown)
319*4a64e381SAndroid Build Coastguard Worker    handle_action_down
320*4a64e381SAndroid Build Coastguard Worker    ;;
321*4a64e381SAndroid Build Coastguard Worker*)
322*4a64e381SAndroid Build Coastguard Worker    log "Unsupported action: '\${ACTION}'"
323*4a64e381SAndroid Build Coastguard Worker    ;;
324*4a64e381SAndroid Build Coastguard Workeresac
325*4a64e381SAndroid Build Coastguard WorkerEOF
326*4a64e381SAndroid Build Coastguard Worker}
327*4a64e381SAndroid Build Coastguard Worker
328*4a64e381SAndroid Build Coastguard Workernetwork_manager_install()
329*4a64e381SAndroid Build Coastguard Worker{
330*4a64e381SAndroid Build Coastguard Worker    with NETWORK_MANAGER || return 0
331*4a64e381SAndroid Build Coastguard Worker
332*4a64e381SAndroid Build Coastguard Worker    if ! have systemctl; then
333*4a64e381SAndroid Build Coastguard Worker        echo "This script requires systemctl!"
334*4a64e381SAndroid Build Coastguard Worker        return 0
335*4a64e381SAndroid Build Coastguard Worker    fi
336*4a64e381SAndroid Build Coastguard Worker
337*4a64e381SAndroid Build Coastguard Worker    if with DNS64; then
338*4a64e381SAndroid Build Coastguard Worker        # bind9 provides DNS service
339*4a64e381SAndroid Build Coastguard Worker        sudo sed -i 's/^#port=5353/port=0/g' /etc/dnsmasq.conf
340*4a64e381SAndroid Build Coastguard Worker        sudo systemctl restart dnsmasq
341*4a64e381SAndroid Build Coastguard Worker    fi
342*4a64e381SAndroid Build Coastguard Worker
343*4a64e381SAndroid Build Coastguard Worker    sudo systemctl daemon-reload
344*4a64e381SAndroid Build Coastguard Worker
345*4a64e381SAndroid Build Coastguard Worker    sudo systemctl stop wpa_supplicant || true
346*4a64e381SAndroid Build Coastguard Worker    sudo systemctl disable wpa_supplicant || true
347*4a64e381SAndroid Build Coastguard Worker
348*4a64e381SAndroid Build Coastguard Worker    sudo systemctl stop dhcpcd || true
349*4a64e381SAndroid Build Coastguard Worker    sudo systemctl disable dhcpcd || true
350*4a64e381SAndroid Build Coastguard Worker
351*4a64e381SAndroid Build Coastguard Worker    sudo systemctl daemon-reload
352*4a64e381SAndroid Build Coastguard Worker
353*4a64e381SAndroid Build Coastguard Worker    sudo systemctl start NetworkManager || die "Failed to start NetworkManager."
354*4a64e381SAndroid Build Coastguard Worker    sudo systemctl enable NetworkManager || die "Failed to enable NetworkManager."
355*4a64e381SAndroid Build Coastguard Worker
356*4a64e381SAndroid Build Coastguard Worker    # Create AP connection only on raspbian platform.
357*4a64e381SAndroid Build Coastguard Worker    if [ "$PLATFORM" = raspbian ] || with NETWORK_MANAGER_WIFI; then
358*4a64e381SAndroid Build Coastguard Worker        create_ap_helper_script
359*4a64e381SAndroid Build Coastguard Worker        sudo chmod a+x ${AP_HELPER_SCRIPT}
360*4a64e381SAndroid Build Coastguard Worker
361*4a64e381SAndroid Build Coastguard Worker        create_ap_connection
362*4a64e381SAndroid Build Coastguard Worker    fi
363*4a64e381SAndroid Build Coastguard Worker
364*4a64e381SAndroid Build Coastguard Worker    create_dhcpv6_helper_script
365*4a64e381SAndroid Build Coastguard Worker    sudo chmod a+x ${DHCPV6_HELPER_SCRIPT}
366*4a64e381SAndroid Build Coastguard Worker
367*4a64e381SAndroid Build Coastguard Worker    create_eth_connection
368*4a64e381SAndroid Build Coastguard Worker
369*4a64e381SAndroid Build Coastguard Worker    sudo systemctl daemon-reload
370*4a64e381SAndroid Build Coastguard Worker    sudo systemctl restart NetworkManager
371*4a64e381SAndroid Build Coastguard Worker
372*4a64e381SAndroid Build Coastguard Worker    sleep 15
373*4a64e381SAndroid Build Coastguard Worker
374*4a64e381SAndroid Build Coastguard Worker    if [ "$PLATFORM" = raspbian ] || with NETWORK_MANAGER_WIFI; then
375*4a64e381SAndroid Build Coastguard Worker        sudo nmcli c up ${AP_CONN}
376*4a64e381SAndroid Build Coastguard Worker    fi
377*4a64e381SAndroid Build Coastguard Worker
378*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c up ${ETH_CONN}
379*4a64e381SAndroid Build Coastguard Worker}
380*4a64e381SAndroid Build Coastguard Worker
381*4a64e381SAndroid Build Coastguard Workernetwork_manager_uninstall()
382*4a64e381SAndroid Build Coastguard Worker{
383*4a64e381SAndroid Build Coastguard Worker    with NETWORK_MANAGER || return 0
384*4a64e381SAndroid Build Coastguard Worker
385*4a64e381SAndroid Build Coastguard Worker    if with DNS64; then
386*4a64e381SAndroid Build Coastguard Worker        sudo systemctl stop dnsmasq
387*4a64e381SAndroid Build Coastguard Worker        # revert changes to dnsmasq
388*4a64e381SAndroid Build Coastguard Worker        sudo sed -i 's/^port=0/#port=5353/g' /etc/dnsmasq.conf
389*4a64e381SAndroid Build Coastguard Worker    fi
390*4a64e381SAndroid Build Coastguard Worker
391*4a64e381SAndroid Build Coastguard Worker    if ! have systemctl; then
392*4a64e381SAndroid Build Coastguard Worker        echo "This script requires systemctl!"
393*4a64e381SAndroid Build Coastguard Worker        return 0
394*4a64e381SAndroid Build Coastguard Worker    fi
395*4a64e381SAndroid Build Coastguard Worker
396*4a64e381SAndroid Build Coastguard Worker    if ! systemctl is-active NetworkManager; then
397*4a64e381SAndroid Build Coastguard Worker        sudo systemctl daemon-reload
398*4a64e381SAndroid Build Coastguard Worker        sudo systemctl start NetworkManager
399*4a64e381SAndroid Build Coastguard Worker    fi
400*4a64e381SAndroid Build Coastguard Worker
401*4a64e381SAndroid Build Coastguard Worker    if [ "$PLATFORM" = raspbian ] || with NETWORK_MANAGER_WIFI; then
402*4a64e381SAndroid Build Coastguard Worker        sudo nmcli c down ${AP_CONN} || true
403*4a64e381SAndroid Build Coastguard Worker        sudo nmcli c delete ${AP_CONN} || true
404*4a64e381SAndroid Build Coastguard Worker    fi
405*4a64e381SAndroid Build Coastguard Worker
406*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c down ${ETH_CONN} || true
407*4a64e381SAndroid Build Coastguard Worker    sudo nmcli c delete ${ETH_CONN} || true
408*4a64e381SAndroid Build Coastguard Worker
409*4a64e381SAndroid Build Coastguard Worker    sudo systemctl disable NetworkManager || die 'Failed to disable NetworkManager!'
410*4a64e381SAndroid Build Coastguard Worker    sudo systemctl stop NetworkManager || die 'Failed to stop NetworkManager!'
411*4a64e381SAndroid Build Coastguard Worker
412*4a64e381SAndroid Build Coastguard Worker    sudo rm ${AP_HELPER_SCRIPT} || true
413*4a64e381SAndroid Build Coastguard Worker    sudo rm ${DHCPV6_HELPER_SCRIPT} || true
414*4a64e381SAndroid Build Coastguard Worker
415*4a64e381SAndroid Build Coastguard Worker    sudo systemctl daemon-reload
416*4a64e381SAndroid Build Coastguard Worker
417*4a64e381SAndroid Build Coastguard Worker    sudo systemctl start dhcpcd || true
418*4a64e381SAndroid Build Coastguard Worker    sudo systemctl enable dhcpcd || true
419*4a64e381SAndroid Build Coastguard Worker
420*4a64e381SAndroid Build Coastguard Worker    sudo systemctl start wpa_supplicant || true
421*4a64e381SAndroid Build Coastguard Worker    sudo systemctl enable wpa_supplicant || true
422*4a64e381SAndroid Build Coastguard Worker}
423