2 # Copyright (C) 2016 The Linux Containers Project
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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.
21 if [ -n "$SNAP_DATA" ]; then
22 varrun="$SNAP_DATA"/network
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"
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"
43 if [ -n "$snap_ipv4_netmask" ]; then
44 IPV4_NETMASK="$snap_ipv4_netmask"
46 if [ -n "$snap_ipv4_network" ]; then
47 IPV4_NETWORK="$snap_ipv4_network"
49 if [ -n "$snap_ipv4_broadcast" ]; then
50 IPV4_BROADCAST="$snap_ipv4_broadcast"
52 if [ "$snap_enable_nat" = false ]; then
57 use_iptables_lock="-w"
58 iptables -w -L -n > /dev/null 2>&1 || use_iptables_lock=""
61 # Assumes there's no "255." after a non-255 byte in the mask
63 set -- "0^^^128^192^224^240^248^252^254^" "$(( (${#1} - ${#x})*2 ))" "${x%%.*}"
65 echo $(( ${2} + (${#x}/4) ))
69 ip addr flush dev "${1}"
70 ip link set dev "${1}" down
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}"
80 ip link set dev "${1}" up
84 [ ! -f "${varrun}/network_up" ] || { echo "anbox-bridge is already running"; exit 1; }
86 if [ -d /sys/class/net/${BRIDGE} ]; then
87 stop force 2>/dev/null || true
94 if [ "${FAILED}" = "1" ]; then
95 echo "Failed to setup anbox-bridge." >&2
100 trap cleanup EXIT HUP INT TERM
103 # set up the anbox network
104 [ ! -d "/sys/class/net/${BRIDGE}" ] && ip link add dev "${BRIDGE}" type bridge
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
111 if which restorecon >/dev/null 2>&1; then
112 restorecon "${varrun}"
116 ifup "${BRIDGE}" "${IPV4_ADDR}" "${IPV4_NETMASK}"
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"
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"
134 touch "${varrun}/network_up"
139 [ -f "${varrun}/network_up" ] || [ "${1}" = "force" ] || { echo "anbox-bridge isn't running"; exit 1; }
141 if [ -d /sys/class/net/${BRIDGE} ]; then
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"
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"
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}"
159 rm -f "${varrun}/network_up"
162 # See how we were called.
172 restart|reload|force-reload)
178 echo "Usage: ${0} {start|stop|restart|reload|force-reload}"