1546990ae114c3cbfaf0071a52856fc01e7946a7
[iec.git] / src / type3_AndroidCloud / anbox-master / scripts / anbox-bridge.sh
1 #!/bin/sh
2 # Copyright (C) 2016 The Linux Containers Project
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Taken from https://github.com/lxc/lxd-pkg-ubuntu/blob/dpm-xenial/lxd-bridge/lxd-bridge
17 # but modified for the use within anbox.
18
19 varrun="/run/anbox"
20
21 if [ -n "$SNAP_DATA" ]; then
22     varrun="$SNAP_DATA"/network
23 fi
24
25 BRIDGE="anbox0"
26
27 # IPv4
28 IPV4_ADDR="192.168.250.1"
29 IPV4_NETMASK="255.255.255.0"
30 IPV4_NETWORK="192.168.250.1/24"
31 IPV4_BROADCAST="0.0.0.0"
32 IPV4_NAT="true"
33
34 if [ -n "$SNAP" ]; then
35     snap_ipv4_address=$(snapctl get bridge.address)
36     snap_ipv4_netmask=$(snapctl get bridge.netmask)
37     snap_ipv4_network=$(snapctl get bridge.network)
38     snap_ipv4_broadcast=$(snapctl get bridge.broadcast)
39     snap_enable_nat=$(snapctl get bridge.nat.enable)
40     if [ -n "$snap_ipv4_address" ]; then
41         IPV4_ADDR="$snap_ipv4_address"
42     fi
43     if [ -n "$snap_ipv4_netmask" ]; then
44         IPV4_NETMASK="$snap_ipv4_netmask"
45     fi
46     if [ -n "$snap_ipv4_network" ]; then
47         IPV4_NETWORK="$snap_ipv4_network"
48     fi
49     if [ -n "$snap_ipv4_broadcast" ]; then
50         IPV4_BROADCAST="$snap_ipv4_broadcast"
51     fi
52     if [ "$snap_enable_nat" = false ]; then
53         IPV4_NAT="false"
54     fi
55 fi
56
57 use_iptables_lock="-w"
58 iptables -w -L -n > /dev/null 2>&1 || use_iptables_lock=""
59
60 _netmask2cidr () {
61     # Assumes there's no "255." after a non-255 byte in the mask
62     local x=${1##*255.}
63     set -- "0^^^128^192^224^240^248^252^254^" "$(( (${#1} - ${#x})*2 ))" "${x%%.*}"
64     x=${1%%${3}*}
65     echo $(( ${2} + (${#x}/4) ))
66 }
67
68 ifdown() {
69     ip addr flush dev "${1}"
70     ip link set dev "${1}" down
71 }
72
73 ifup() {
74     [ "${HAS_IPV6}" = "true" ] && [ "${IPV6_PROXY}" = "true" ] && ip addr add fe80::1/64 dev "${1}"
75     if [ -n "${IPV4_NETMASK}" ] && [ -n "${IPV4_ADDR}" ]; then
76         MASK=$(_netmask2cidr ${IPV4_NETMASK})
77         CIDR_ADDR="${IPV4_ADDR}/${MASK}"
78         ip addr add "${CIDR_ADDR}" broadcast "${IPV4_BROADCAST}" dev "${1}"
79     fi
80     ip link set dev "${1}" up
81 }
82
83 start() {
84     [ ! -f "${varrun}/network_up" ] || { echo "anbox-bridge is already running"; exit 1; }
85
86     if [ -d /sys/class/net/${BRIDGE} ]; then
87         stop force 2>/dev/null || true
88     fi
89
90     FAILED=1
91
92     cleanup() {
93         set +e
94         if [ "${FAILED}" = "1" ]; then
95             echo "Failed to setup anbox-bridge." >&2
96             stop force
97         fi
98     }
99
100     trap cleanup EXIT HUP INT TERM
101     set -e
102
103     # set up the anbox network
104     [ ! -d "/sys/class/net/${BRIDGE}" ] && ip link add dev "${BRIDGE}" type bridge
105
106     # if we are run from systemd on a system with selinux enabled,
107     # the mkdir will create /run/anbox as init_var_run_t which dnsmasq
108     # can't write its pid into, so we restorecon it (to var_run_t)
109     if [ ! -d "${varrun}" ]; then
110         mkdir -p "${varrun}"
111         if which restorecon >/dev/null 2>&1; then
112             restorecon "${varrun}"
113         fi
114     fi
115
116     ifup "${BRIDGE}" "${IPV4_ADDR}" "${IPV4_NETMASK}"
117
118     IPV4_ARG=""
119     if [ -n "${IPV4_ADDR}" ] && [ -n "${IPV4_NETMASK}" ] && [ -n "${IPV4_NETWORK}" ]; then
120         echo 1 > /proc/sys/net/ipv4/ip_forward
121         if [ "${IPV4_NAT}" = "true" ]; then
122             iptables "${use_iptables_lock}" -t nat -A POSTROUTING -s "${IPV4_NETWORK}" ! -d "${IPV4_NETWORK}" -j MASQUERADE -m comment --comment "managed by anbox-bridge"
123         fi
124     fi
125
126     iptables "${use_iptables_lock}" -I INPUT -i "${BRIDGE}" -p udp --dport 67 -j ACCEPT -m comment --comment "managed by anbox-bridge"
127     iptables "${use_iptables_lock}" -I INPUT -i "${BRIDGE}" -p tcp --dport 67 -j ACCEPT -m comment --comment "managed by anbox-bridge"
128     iptables "${use_iptables_lock}" -I INPUT -i "${BRIDGE}" -p udp --dport 53 -j ACCEPT -m comment --comment "managed by anbox-bridge"
129     iptables "${use_iptables_lock}" -I INPUT -i "${BRIDGE}" -p tcp --dport 53 -j ACCEPT -m comment --comment "managed by anbox-bridge"
130     iptables "${use_iptables_lock}" -I FORWARD -i "${BRIDGE}" -j ACCEPT -m comment --comment "managed by anbox-bridge"
131     iptables "${use_iptables_lock}" -I FORWARD -o "${BRIDGE}" -j ACCEPT -m comment --comment "managed by anbox-bridge"
132     iptables "${use_iptables_lock}" -t mangle -A POSTROUTING -o "${BRIDGE}" -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill -m comment --comment "managed by anbox-bridge"
133
134     touch "${varrun}/network_up"
135     FAILED=0
136 }
137
138 stop() {
139     [ -f "${varrun}/network_up" ] || [ "${1}" = "force" ] || { echo "anbox-bridge isn't running"; exit 1; }
140
141     if [ -d /sys/class/net/${BRIDGE} ]; then
142         ifdown ${BRIDGE}
143         iptables ${use_iptables_lock} -D INPUT -i ${BRIDGE} -p udp --dport 67 -j ACCEPT -m comment --comment "managed by anbox-bridge"
144         iptables ${use_iptables_lock} -D INPUT -i ${BRIDGE} -p tcp --dport 67 -j ACCEPT -m comment --comment "managed by anbox-bridge"
145         iptables ${use_iptables_lock} -D INPUT -i ${BRIDGE} -p udp --dport 53 -j ACCEPT -m comment --comment "managed by anbox-bridge"
146         iptables ${use_iptables_lock} -D INPUT -i ${BRIDGE} -p tcp --dport 53 -j ACCEPT -m comment --comment "managed by anbox-bridge"
147         iptables ${use_iptables_lock} -D FORWARD -i ${BRIDGE} -j ACCEPT -m comment --comment "managed by anbox-bridge"
148         iptables ${use_iptables_lock} -D FORWARD -o ${BRIDGE} -j ACCEPT -m comment --comment "managed by anbox-bridge"
149         iptables ${use_iptables_lock} -t mangle -D POSTROUTING -o ${BRIDGE} -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill -m comment --comment "managed by anbox-bridge"
150
151         if [ -n "${IPV4_NETWORK}" ] && [ "${IPV4_NAT}" = "true" ]; then
152             iptables ${use_iptables_lock} -t nat -D POSTROUTING -s ${IPV4_NETWORK} ! -d ${IPV4_NETWORK} -j MASQUERADE -m comment --comment "managed by anbox-bridge"
153         fi
154
155         # if ${BRIDGE} has attached interfaces, don't destroy the bridge
156         ls /sys/class/net/${BRIDGE}/brif/* > /dev/null 2>&1 || ip link delete "${BRIDGE}"
157     fi
158
159     rm -f "${varrun}/network_up"
160 }
161
162 # See how we were called.
163 case "${1}" in
164     start)
165         start
166     ;;
167
168     stop)
169         stop
170     ;;
171
172     restart|reload|force-reload)
173         ${0} stop
174         ${0} start
175     ;;
176
177     *)
178         echo "Usage: ${0} {start|stop|restart|reload|force-reload}"
179         exit 2
180 esac
181
182 exit $?