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.
30 # This script manipulates dns64 configuration.
33 BIND_CONF_OPTIONS=/etc/bind/named.conf.options
34 NAT64_PREFIX=64:ff9b::/96
36 DNS64_NAMESERVER_ADDR=127.0.0.1
37 DNS64_CONF="dns64 $(echo $NAT64_PREFIX | tr \"/\" \"/\") { clients { thread; }; recursive-only yes; };"
39 # Currently solution was verified only on raspbian and ubuntu.
41 without NAT64 || test "$PLATFORM" = ubuntu || test "$PLATFORM" = beagleboneblack || test "$PLATFORM" = raspbian || die "dns64 is not tested under $PLATFORM."
43 if [ "$PLATFORM" = raspbian ]; then
44 RESOLV_CONF_HEAD=/etc/resolv.conf.head
45 elif [ "$PLATFORM" = beagleboneblack ]; then
46 RESOLV_CONF_HEAD=/etc/resolvconf/resolv.conf.d/head
47 elif [ "$PLATFORM" = ubuntu ]; then
48 RESOLV_CONF_HEAD=/etc/resolvconf/resolv.conf.d/head
51 dns64_update_resolvconf()
53 if [ "$PLATFORM" = ubuntu ]; then
54 sudo resolvconf -u || true
55 elif [ "$PLATFORM" = beagleboneblack ]; then
56 sudo resolvconf -u || true
57 elif [ "$PLATFORM" = raspbian ]; then
58 if systemctl is-enabled NetworkManager; then
59 sudo systemctl restart NetworkManager || true
62 if systemctl is-enabled dhcpcd; then
63 sudo systemctl restart dhcpcd || true
68 _detect_service_name()
70 dpkg -L bind9 | grep /etc/init.d/ | cut -d/ -f4
75 with NAT64 && with DNS64 || return 0
77 test -f $BIND_CONF_OPTIONS || die 'Cannot find bind9 configuration file!'
78 sudo sed -i '/^};/i\\tlisten-on-v6 { thread; };' $BIND_CONF_OPTIONS
79 sudo sed -i '/^\tlisten-on-v6 { a/d' $BIND_CONF_OPTIONS
80 sudo sed -i '/^};/i\\tallow-query { any; };' $BIND_CONF_OPTIONS
81 sudo sed -i '/^};/i\\tallow-recursion { thread; };' $BIND_CONF_OPTIONS
82 sudo sed -i '/^};/i\\tforwarders { 8.8.8.8; 8.8.8.4; };' $BIND_CONF_OPTIONS
83 sudo sed -i '/^};/i\\tforward only;' $BIND_CONF_OPTIONS
84 sudo sed -i '/^};/i\\t'"$DNS64_CONF" $BIND_CONF_OPTIONS
85 sudo sed -i '1s/^/acl thread {\n\tfe80::\/16;\n\tfc00::\/7;\n\t127.0.0.1;\n};\n\n/' $BIND_CONF_OPTIONS
87 service_name="$(_detect_service_name)"
89 if without DOCKER; then
90 sudo sh -c "echo \"nameserver $DNS64_NAMESERVER_ADDR\" >> $RESOLV_CONF_HEAD"
93 if have systemctl; then
94 sudo systemctl stop dnsmasq || true
95 sudo systemctl disable dnsmasq || true
96 sudo systemctl enable "${service_name}" || true
97 sudo systemctl is-enabled "${service_name}" || die 'Failed to enable bind9!'
98 sudo systemctl start "${service_name}" || die 'Failed to start bind9!'
101 if without DOCKER; then
102 dns64_update_resolvconf
108 with NAT64 && with DNS64 || return 0
110 service_name="$(_detect_service_name)"
113 sudo sed -i '/^\tlisten-on-v6/d' $BIND_CONF_OPTIONS
114 sudo sed -i '/^\tallow-query/d' $BIND_CONF_OPTIONS
115 sudo sed -i '/^\tallow-recursion/d' $BIND_CONF_OPTIONS
116 sudo sed -i '/^\tforward/d' $BIND_CONF_OPTIONS
117 sudo sed -i '/^};/i\\tlisten-on-v6 { any; };' $BIND_CONF_OPTIONS
118 sudo sed -i '/^\tdns64/d' $BIND_CONF_OPTIONS
119 sudo sed -i '/^acl/,/^options/{/^options/!d}' $BIND_CONF_OPTIONS
121 sudo sed -i '/^nameserver '$DNS64_NAMESERVER_ADDR'/d' $RESOLV_CONF_HEAD || true
123 if without DOCKER; then
124 dns64_update_resolvconf
127 if have systemctl; then
128 sudo systemctl stop "${service_name}" || true
129 sudo systemctl disable "${service_name}" || true
135 with NAT64 && with DNS64 || return 0
137 service_name="$(_detect_service_name)"
139 if have systemctl; then
140 sudo systemctl start "${service_name}" || die 'Failed to start bind9!'
141 elif command -v service; then
142 sudo service "${service_name}" start || die 'Failed to start bind9!'
148 with NAT64 && with DNS64 || return 0
150 service_name="$(_detect_service_name)"
152 if have systemctl; then
153 sudo systemctl stop "${service_name}" || true
154 elif command -v service; then
155 sudo service "${service_name}" stop || true