3 # Copyright (c) 2017, The OpenThread Authors.
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.
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.
29 #----------------------------------------
31 # To understand the purpose of this script see: print_big_ugly_warning() below.
32 #----------------------------------------
35 # remember the name of this script
41 # shellcheck source=script/_initrc
42 . "$(dirname "$0")"/_initrc
44 ETH0_IPV6_BASE_PREFIX=fd11:33
48 if [ x"${_DEBUG_IPV6}" == x"true" ]; then
58 # this gives us a sorted list of network interface names
60 cd /sys/class/net || exit
63 # We want the physical device
64 # Not things like "usb0" or "wpan0"
65 # And we assume the first one is what we want
66 debug_echo "Consider: ${devname}"
69 usb* | can* | wpan* | br* | wlan* | lo)
70 # by name we can ignore USB-gadget, CANbus, Thread, wireless and loopback
79 debug_echo "Ignore ${devname} by name"
83 debug_echo "Consider: ${devname}"
84 if [ ! -L /sys/class/net/"${devname}"/device ]; then
85 debug_echo "Not a DEVICE ${devname}"
89 type=$(cat /sys/class/net/"${devname}"/type)
90 # Type1 = ARPHRD_ETHER
91 if [ "$type" -ne 1 ]; then
92 debug_echo "Not ARPHRD_ETHER"
95 # We assume the first thing we find is our device
100 if [ -z "${ETH0_NAME}" ]; then
101 echo "Cannot determine ETH0 name...."
104 echo "Assuming: Primary ETHERNET name is $ETH0_NAME"
109 echo "Fetching RADVD..."
110 echo "apt-get install --no-install-recommends radvd"
111 sudo apt-get install --no-install-recommends radvd
114 choose_random_eth0_address()
117 # Using "od" see http://man7.org/linux/man-pages/man1/od.1.html
118 # read from /dev/urandom
120 # We use /dev/urandom not /dev/random for these reasons:
121 # 1) This is for private (not public) test purposes
122 # 2) urandom might stall ... and not give us bytes
124 # We want data in 16bit hex, hence: --format=x2
125 # We want only 4 bytes, hence --read-bytes=4
127 # The output looks like:
131 # head gives us the first line
132 # cut gives us the items 2 and 3 on the line
133 # tr converts the space into a ':'
135 RANDOM_32BIT_VALUE=$(od --read-bytes=4 --format=x2 /dev/urandom \
140 # thus, "RANDOM_32BIT_VALUE=1234:5678"
142 # We'll use this for the radvd config
143 ETH0_IPV6_PREFIX=${ETH0_IPV6_BASE_PREFIX}:${RANDOM_32BIT_VALUE}
145 # and this for the static network address
146 ETH0_IPV6_STATIC_ADDRESS=${ETH0_IPV6_PREFIX}::1
151 # this creates a configuration file for radvd
152 CFG_FILE=/etc/radvd.conf
153 if [ -f $CFG_FILE ]; then
154 echo "radvd config file exists: $CFG_FILE"
155 echo "SKIPPING radvd configuration"
157 sudo tee -a /etc/radvd.conf <<__EOF__
159 # This RADVD configuration file was created
160 # by the OpenThread configuration script $SCRIPT_NAME
161 # Executed in the directory ${CWD} on ${DATE}
163 # The purpose is to configure IPV6 in an issolated and
164 # standalone network configuration for the purpose of test only
166 # This is by no means a complete IPv6 configuration
167 # it is sufficent to allow coap transactions
168 # with thread devices on the thread mesh network
169 # attched to this boarder router
171 interface ${ETH0_NAME} {
172 # We want to send router adverts
175 # This is not a proper IPv6 router
176 # it is only for openthread
177 AdvDefaultPreference low;
179 # We should advertize this prefix
180 prefix ${ETH0_IPV6_PREFIX}::/64 {
181 # we want this "on link"
183 # devices should self-assign addresses with this prefix
192 assign_eth0_static_ipv6()
195 # this creates a static IPv6 address for Eth0
196 sudo tee -a /etc/network/interfaces <<__EOF__
198 # This configuration was created by
199 # the openthread ${SCRIPT_NAME}
200 # executing in the directory ${CWD}
201 # and executed on ${DATE}
203 # for the purposes of testing ipv6 addresses
204 # in an issolated network configuration
206 # ensure ETH0 is configured at boot
209 # Configure the IPv6 address static
210 # Note: IPv4 is not effected by this
211 iface ${ETH0_NAME} inet6 static
212 address ${ETH0_IPV6_STATIC_ADDRESS}
219 # other platforms might do something simular
224 choose_random_eth0_address
226 assign_eth0_static_ipv6
228 echo "You should now reboot your Device"
231 print_big_ugly_warning()
237 Please understand the purpose of this script.
239 This script is not intended to be an complete and proper IPv6
240 configuration script.
242 This is only hack that turns on just enough IPv6 to perform simple
243 CoAP requests on or across an issolated test network to the Thread
246 The example issolated test network consists of these parts:
248 1) A thread RF radio network.
250 2) The OpenThread Border router attached to the Thread network
252 3) The Openthread Border router would typically be connected
253 to a residential home network in some way (wifi, or wired)
255 In this case, it is connected to a router that is not
256 connected to an upstream provider - it is issolated.
258 4) In order to test & develop applications other things on the
259 "home/test network" need to talk to the various Thread end nodes
264 * Laptop, or desktop machine
265 * Android/Apple Phone or Tablet
266 * other network devices
268 To test/develop your applications a means for these other devices on
269 your test network to talk to devices on the Thread Network must exist.
274 Most home network routers provide only IPv4 services. They most
275 typically assume the upstream ISP network provider will provide
276 IPv6 addresses and configuration. A test network is often issolated
277 and not connected to the large world wide web, it is completely an
278 island. The upstream ISP provider does not exist.
280 In the end something needs to provide a minimal IPv6 configuration.
282 =========================================================
283 The above is the purpose of this ipv6 standalone hack script
285 Never consider this script a proper IPv6 configuration.
286 =========================================================
288 It is only a quick hack that enables enough IPv6 to work such that:
290 1) Your local test laptop/cellphone has an IPv6 address and
291 2) can perform CoAP transfers between a node on thread network
293 Now that you have read and understood the above, execute this script
296 ${SCRIPT_NAME} enable_ipv6_hack
302 # ensure user/victim has read the ugly warning.
303 if [ "${1}" != "enable_ipv6_hack" ]; then
304 print_big_ugly_warning
314 die "Unsupported/unknown platform ${PLATFORM}"