Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / script / _nat64
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 nat64 configuration.
31 #
32
33 TAYGA_DEFAULT=/etc/default/tayga
34 TAYGA_CONF=/etc/tayga.conf
35 TAYGA_IPV4_ADDR=192.168.255.1
36 TAYGA_IPV6_ADDR=fdaa:bb:1::1
37 TAYGA_TUN_V6_ADDR=fdaa:bb:1::2
38 NAT44_SERVICE=/etc/init.d/otbr-nat44
39 WLAN_IFNAMES=eth0
40
41 # Currently solution was verified only on raspbian and ubuntu.
42 #
43 #without NAT64 || test $PLATFORM = ubuntu || test $PLATFORM = raspbian || die "nat64 is not tested under $PLATFORM."
44
45 nat64_install()
46 {
47     with NAT64 || return 0
48
49     test -f $TAYGA_DEFAULT -a -f $TAYGA_CONF || die 'Cannot find tayga configuration file!'
50     sudo sed -i 's/^RUN="no"/RUN="yes"/' $TAYGA_DEFAULT
51     sudo sed -i 's/^IPV4_TUN_ADDR=""/IPV4_TUN_ADDR="'$TAYGA_IPV4_ADDR'"/' $TAYGA_DEFAULT
52     sudo sed -i 's/^IPV6_TUN_ADDR=""/IPV6_TUN_ADDR="'$TAYGA_TUN_V6_ADDR'"/' $TAYGA_DEFAULT
53     sudo sed -i 's/^prefix /##prefix /' $TAYGA_CONF
54     sudo sed -i 's/^# prefix 64:ff9b::\/96/prefix 64:ff9b::\/96/' $TAYGA_CONF
55     sudo sed -i '/^#ipv6-addr/a ipv6-addr '$TAYGA_IPV6_ADDR $TAYGA_CONF
56     sudo tee $NAT44_SERVICE <<EOF
57 #! /bin/sh
58 #
59 #  Copyright (c) 2017, The OpenThread Authors.
60 #  All rights reserved.
61 #
62 #  Redistribution and use in source and binary forms, with or without
63 #  modification, are permitted provided that the following conditions are met:
64 #  1. Redistributions of source code must retain the above copyright
65 #     notice, this list of conditions and the following disclaimer.
66 #  2. Redistributions in binary form must reproduce the above copyright
67 #     notice, this list of conditions and the following disclaimer in the
68 #     documentation and/or other materials provided with the distribution.
69 #  3. Neither the name of the copyright holder nor the
70 #     names of its contributors may be used to endorse or promote products
71 #     derived from this software without specific prior written permission.
72 #
73 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74 #  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 #  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
77 #  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
78 #  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
79 #  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
80 #  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
81 #  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
82 #  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
83 #  POSSIBILITY OF SUCH DAMAGE.
84 #
85 ### BEGIN INIT INFO
86 # Provides:          otbr-nat44
87 # Required-Start:    tayga
88 # Required-Stop:
89 # Should-Start:
90 # Should-Stop:
91 # Default-Start:     2 3 4 5
92 # Default-Stop:
93 # Short-Description: iptables NAT44
94 # Description:       NAT44 is require for OpenThread border router
95 #                    to connect to arbitrary IPv4 endpoints.
96 ### END INIT INFO
97
98 . /lib/lsb/init-functions
99 . /lib/init/vars.sh
100
101 case "\$1" in
102     start)
103 EOF
104     for IFNAME in $WLAN_IFNAMES; do
105         echo "      iptables -t nat -A POSTROUTING -o $IFNAME -j MASQUERADE" | sudo tee -a $NAT44_SERVICE
106     done
107     sudo tee -a $NAT44_SERVICE <<EOF
108         ;;
109     restart|reload|force-reload)
110         echo "Error: argument '\$1' not supported" >&2
111         exit 3
112         ;;
113     stop|status)
114         # No-op
115         ;;
116     *)
117         echo "Usage: \$0 start|stop" >&2
118         exit 3
119         ;;
120 esac
121 EOF
122     sudo chmod a+x $NAT44_SERVICE
123     if have systemctl; then
124         sudo systemctl stop tayga || die 'Unable to stop tayga service!'
125         sudo systemctl enable otbr-nat44 || die 'Unable to enable nat44 service!'
126     fi
127 }
128
129 nat64_uninstall()
130 {
131     with NAT64 || return 0
132
133     nat64_stop
134     sudo sed -i 's/^RUN="yes"/RUN="no"/' $TAYGA_DEFAULT
135     sudo sed -i 's/^IPV4_TUN_ADDR="'$TAYGA_IPV4_ADDR'"/IPV4_TUN_ADDR=""/' $TAYGA_DEFAULT
136     sudo sed -i 's/^prefix 64:ff9b::\/96/# prefix 64:ff9b::\/96/' $TAYGA_CONF
137     sudo sed -i 's/^##prefix /prefix /' $TAYGA_CONF
138     sudo sed -i '/^ipv6-addr '$TAYGA_IPV6_ADDR'/d' $TAYGA_CONF
139
140     if have systemctl; then
141         sudo systemctl disable otbr-nat44 || true
142     fi
143
144     # systemctl disable doesn't remove sym-links
145     if have update-rc.d; then
146         sudo update-rc.d otbr-nat44 remove || true
147     fi
148     test ! -f $NAT44_SERVICE || sudo rm $NAT44_SERVICE
149 }
150
151 nat64_start()
152 {
153     with NAT64 || return 0
154
155     if with DOCKER; then
156         service tayga start || die 'Failed to start tayga'
157     elif have systemctl; then
158         sudo systemctl start tayga || die 'Failed to start tayga!'
159         sudo systemctl enable tayga || die 'Failed to enable tayga!'
160         sudo systemctl start otbr-nat44 || die 'Failed to start NAT44!'
161     fi
162 }
163
164 nat64_stop()
165 {
166     with NAT64 || return 0
167
168     if with DOCKER; then
169         service tayga stop || true
170     elif have systemctl; then
171         sudo systemctl stop tayga || true
172         sudo systemctl stop otbr-nat44 || true
173     fi
174 }