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