2015-10-15 04:34:55 +02:00
|
|
|
|
#!/bin/bash
|
2010-09-06 17:07:42 +02:00
|
|
|
|
#============================================================================
|
2015-10-15 04:34:55 +02:00
|
|
|
|
# /etc/xen/vif-route-qubes
|
|
|
|
|
#
|
|
|
|
|
# Script for configuring a vif in routed mode.
|
|
|
|
|
# The hotplugging system will call this script if it is specified either in
|
|
|
|
|
# the device configuration given to Xend, or the default Xend configuration
|
2010-09-06 17:07:42 +02:00
|
|
|
|
# in /etc/xen/xend-config.sxp. If the script is specified in neither of those
|
2015-10-15 04:34:55 +02:00
|
|
|
|
# places, then vif-bridge is the default.
|
|
|
|
|
#
|
|
|
|
|
# Usage:
|
|
|
|
|
# vif-route (add|remove|online|offline)
|
|
|
|
|
#
|
|
|
|
|
# Environment vars:
|
|
|
|
|
# vif vif interface name (required).
|
|
|
|
|
# XENBUS_PATH path to this device's details in the XenStore (required).
|
|
|
|
|
#
|
|
|
|
|
# Read from the store:
|
|
|
|
|
# ip list of IP networks for the vif, space-separated (default given in
|
|
|
|
|
# this script).
|
|
|
|
|
#============================================================================
|
2010-09-06 17:07:42 +02:00
|
|
|
|
|
|
|
|
|
dir=$(dirname "$0")
|
2017-09-30 03:17:14 +02:00
|
|
|
|
# shellcheck disable=SC1091,SC1090
|
2010-09-06 17:07:42 +02:00
|
|
|
|
. "$dir/vif-common.sh"
|
|
|
|
|
|
2019-12-19 01:14:39 +01:00
|
|
|
|
set -o pipefail
|
2010-09-06 17:07:42 +02:00
|
|
|
|
#main_ip=$(dom0_ip)
|
2018-10-15 06:15:04 +02:00
|
|
|
|
|
2019-05-30 15:31:57 +02:00
|
|
|
|
# Network Hooks for triggering supplementary actions on AppVM connect
|
|
|
|
|
network_hooks() {
|
|
|
|
|
local command="$1"
|
|
|
|
|
local vif="$2"
|
|
|
|
|
local addr="$3"
|
|
|
|
|
|
|
|
|
|
local vif_type
|
|
|
|
|
vif_type="$(xenstore-read "${XENBUS_PATH}/type")"
|
|
|
|
|
|
|
|
|
|
if [ -d /rw/config/network-hooks.d ]; then
|
|
|
|
|
for hook in /rw/config/network-hooks.d/*
|
|
|
|
|
do
|
|
|
|
|
if [ -x "$hook" ]; then
|
|
|
|
|
log debug "Executing network-hook $(basename "$hook")..."
|
|
|
|
|
do_without_error "${hook}" "${command}" "${vif}" "${vif_type}" "${addr}"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-06 18:55:51 +01:00
|
|
|
|
conntrack_purge () {
|
|
|
|
|
local 'n=(0|[1-9][0-9]*)' output deleted msg
|
|
|
|
|
msg='flow entries have been deleted\.$'
|
|
|
|
|
deleted="^conntrack v$n\\.$n\\.$n \\(conntrack-tools\\): $n $msg"
|
|
|
|
|
output=$(LC_ALL=C exec conntrack -D "$@" 2>&1 >/dev/null) || :
|
|
|
|
|
[[ "$output" =~ $deleted ]]
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 06:15:04 +02:00
|
|
|
|
ipt_arg=
|
|
|
|
|
if "iptables-restore" --help 2>&1 | grep -q wait=; then
|
|
|
|
|
# 'wait' must be last on command line if secs not specified
|
|
|
|
|
ipt_arg=--wait
|
|
|
|
|
fi
|
2010-09-06 17:07:42 +02:00
|
|
|
|
|
2017-09-30 03:17:14 +02:00
|
|
|
|
# shellcheck disable=SC2154
|
2016-10-30 21:41:35 +01:00
|
|
|
|
if [ "${ip}" ]; then
|
2017-12-03 03:27:45 +01:00
|
|
|
|
# get first IPv4 and first IPv6
|
|
|
|
|
for addr in ${ip}; do
|
|
|
|
|
if [ -z "$ip4" ] && [[ "$addr" = *.* ]]; then
|
|
|
|
|
ip4="$addr"
|
|
|
|
|
elif [ -z "$ip6" ] && [[ "$addr" = *:* ]]; then
|
|
|
|
|
ip6="$addr"
|
|
|
|
|
fi
|
|
|
|
|
done
|
2016-10-30 21:41:35 +01:00
|
|
|
|
# IPs as seen by this VM
|
2017-12-03 03:27:45 +01:00
|
|
|
|
netvm_ip="$ip4"
|
2017-09-30 03:17:14 +02:00
|
|
|
|
netvm_gw_ip=$(qubesdb-read /qubes-netvm-gateway)
|
2018-04-02 23:19:31 +02:00
|
|
|
|
netvm_gw_ip6=$(qubesdb-read /qubes-netvm-gateway6 || :)
|
2017-09-30 03:17:14 +02:00
|
|
|
|
netvm_dns1_ip=$(qubesdb-read /qubes-netvm-primary-dns)
|
|
|
|
|
netvm_dns2_ip=$(qubesdb-read /qubes-netvm-secondary-dns)
|
2016-10-30 21:41:35 +01:00
|
|
|
|
|
|
|
|
|
back_ip="$netvm_gw_ip"
|
2018-04-02 23:19:31 +02:00
|
|
|
|
back_ip6="$netvm_gw_ip6"
|
2016-10-30 21:41:35 +01:00
|
|
|
|
|
|
|
|
|
# IPs as seen by the VM - if other than $netvm_ip
|
2017-12-03 03:27:45 +01:00
|
|
|
|
appvm_gw_ip="$(qubesdb-read "/mapped-ip/$ip4/visible-gateway" 2>/dev/null || :)"
|
|
|
|
|
appvm_ip="$(qubesdb-read "/mapped-ip/$ip4/visible-ip" 2>/dev/null || :)"
|
2016-10-30 21:41:35 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2020-11-19 18:04:02 +01:00
|
|
|
|
readonly max_domid=32752
|
2016-10-30 21:41:35 +01:00
|
|
|
|
|
2020-11-19 18:04:02 +01:00
|
|
|
|
# if domid is 0 something is seriously wrong, so don’t check for that case
|
|
|
|
|
if ! [[ $vif =~ ^vif([1-9][0-9]{,4})\.(0|[1-9][0-9]*)$ ]]; then
|
|
|
|
|
printf 'Bad interface name %q\n' "$vif">&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
domid=${BASH_REMATCH[1]} sub=${BASH_REMATCH[2]}
|
|
|
|
|
|
|
|
|
|
# metric must be positive, but prefer later interface
|
|
|
|
|
# 32752 is max XID aka domid
|
|
|
|
|
if (( domid > max_domid )); then
|
|
|
|
|
printf %s\\n "domid $domid too large"
|
|
|
|
|
exit 1
|
2016-10-30 21:41:35 +01:00
|
|
|
|
fi
|
2020-11-19 18:04:02 +01:00
|
|
|
|
metric=$(( max_domid - domid ))
|
2016-10-30 21:41:35 +01:00
|
|
|
|
|
2017-09-30 03:17:14 +02:00
|
|
|
|
# shellcheck disable=SC2154
|
2010-09-06 17:07:42 +02:00
|
|
|
|
case "$command" in
|
2019-05-28 18:56:44 +02:00
|
|
|
|
online)
|
|
|
|
|
echo 1 >"/proc/sys/net/ipv4/conf/${vif}/proxy_arp"
|
|
|
|
|
ipcmd='add'
|
|
|
|
|
iptables_cmd='-I PREROUTING 1'
|
|
|
|
|
cmdprefix=''
|
2019-10-06 05:32:41 +02:00
|
|
|
|
ipv6_disabled=$(cat /proc/sys/net/ipv6/conf/"${vif}"/disable_ipv6 || echo 1)
|
2020-11-19 18:04:02 +01:00
|
|
|
|
# without a MAC address we will fail later with a confusing error
|
|
|
|
|
mac=$(xenstore-read "backend/vif/$domid/$sub/mac") || exit 1
|
2019-05-28 18:56:44 +02:00
|
|
|
|
;;
|
|
|
|
|
offline)
|
|
|
|
|
do_without_error ifdown "${vif}"
|
|
|
|
|
ipcmd='del'
|
|
|
|
|
iptables_cmd='-D PREROUTING'
|
|
|
|
|
cmdprefix='do_without_error'
|
2019-10-06 05:32:41 +02:00
|
|
|
|
# cleanup IPv6 config even if _now_ it is disabled
|
|
|
|
|
ipv6_disabled=0
|
2019-05-28 18:56:44 +02:00
|
|
|
|
;;
|
2010-09-06 17:07:42 +02:00
|
|
|
|
esac
|
|
|
|
|
|
2020-11-19 18:04:02 +01:00
|
|
|
|
# Apply NAT if IP visible from the VM is different than the "real" one
|
|
|
|
|
# See vif-qubes-nat.sh for details
|
|
|
|
|
# XXX: supported only for the first IPv4 address, IPv6 is dropped if this
|
|
|
|
|
# feature is enabled
|
|
|
|
|
if [ -n "$appvm_ip" ] && [ -n "$appvm_gw_ip" ] && [ "$appvm_ip" != "$netvm_ip" ]; then
|
|
|
|
|
# shellcheck source=network/vif-qubes-nat.sh
|
|
|
|
|
. "$dir/vif-qubes-nat.sh"
|
2020-11-13 19:15:24 +01:00
|
|
|
|
fi
|
2012-03-08 14:57:10 +01:00
|
|
|
|
|
2020-12-07 20:08:32 +01:00
|
|
|
|
case $- in
|
|
|
|
|
(*e*) :;;
|
|
|
|
|
(*) echo '-e not set'>&2; exit 1;;
|
|
|
|
|
esac
|
|
|
|
|
|
2019-12-19 01:14:39 +01:00
|
|
|
|
# add anti-spoofing rules before enabling the interface
|
2019-05-28 18:56:44 +02:00
|
|
|
|
if [ "${ip}" ]; then
|
2020-11-13 19:15:24 +01:00
|
|
|
|
# If we’ve been given a list of IP addresses, then add routes from us to
|
|
|
|
|
# the VMs we serve using those addresses.
|
2019-05-28 18:56:44 +02:00
|
|
|
|
for addr in ${ip};
|
|
|
|
|
do
|
2017-12-03 03:27:45 +01:00
|
|
|
|
if [[ "$addr" = *:* ]]; then
|
|
|
|
|
ipt=ip6tables-restore
|
|
|
|
|
else
|
|
|
|
|
ipt=iptables-restore
|
|
|
|
|
fi
|
2019-12-18 05:57:45 +01:00
|
|
|
|
printf '%s\n' "*raw" \
|
|
|
|
|
"$iptables_cmd -i ${vif} ! -s ${addr} -j DROP" \
|
|
|
|
|
"$iptables_cmd ! -i vif+ -s ${addr} -j DROP" \
|
2020-12-07 20:11:12 +01:00
|
|
|
|
"COMMIT" |
|
|
|
|
|
${cmdprefix} "$ipt" --noflush $ipt_arg
|
2020-11-10 22:28:53 +01:00
|
|
|
|
if [[ "$command" = 'online' ]]; then
|
2020-11-11 04:31:18 +01:00
|
|
|
|
ip -- neighbour "${ipcmd}" to "${addr}" \
|
|
|
|
|
dev "${vif}" lladdr "$mac" nud permanent
|
2020-11-10 22:28:53 +01:00
|
|
|
|
fi
|
2020-12-06 18:55:51 +01:00
|
|
|
|
if ! conntrack_purge -s "$addr" || ! conntrack_purge -d "$addr"; then
|
|
|
|
|
printf 'Cannot purge stale conntrack entries for %q\n' "$addr">&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2019-05-28 18:56:44 +02:00
|
|
|
|
done
|
2017-12-03 03:27:45 +01:00
|
|
|
|
# if no IPv6 is assigned, block all IPv6 traffic on that interface
|
|
|
|
|
if ! [[ "$ip" = *:* ]]; then
|
2020-12-07 20:11:12 +01:00
|
|
|
|
echo -e "*raw\\n$iptables_cmd -i ${vif} -j DROP\\nCOMMIT" |
|
2018-10-15 06:15:04 +02:00
|
|
|
|
${cmdprefix} ip6tables-restore --noflush $ipt_arg
|
2017-12-03 03:27:45 +01:00
|
|
|
|
fi
|
2019-12-19 01:14:39 +01:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$command" = "online" ]; then
|
|
|
|
|
ifconfig "${vif}" up
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "${ip}" ]; then
|
|
|
|
|
# If we've been given a list of IP addresses, then add routes from dom0 to
|
|
|
|
|
# the guest using those addresses.
|
|
|
|
|
for addr in ${ip};
|
|
|
|
|
do
|
|
|
|
|
if [[ "$addr" = *:* ]] && [[ "$ipv6_disabled" = 1 ]]; then
|
|
|
|
|
log error "Cannot set IPv6 route to ${addr}, IPv6 disabled in the kernel"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
2020-11-13 18:51:15 +01:00
|
|
|
|
${cmdprefix} ip route "${ipcmd}" "${addr}" dev "${vif}" metric "$metric"
|
2019-12-19 01:14:39 +01:00
|
|
|
|
|
|
|
|
|
network_hooks "${command}" "${vif}" "${addr}"
|
|
|
|
|
done
|
2018-04-02 23:19:31 +02:00
|
|
|
|
${cmdprefix} ip addr "${ipcmd}" "${back_ip}/32" dev "${vif}"
|
2019-10-06 05:32:41 +02:00
|
|
|
|
if [ "${back_ip6}" ] && [[ "${back_ip6}" != "fe80:"* ]] && [[ "$ipv6_disabled" != 1 ]]; then
|
2018-04-02 23:19:31 +02:00
|
|
|
|
${cmdprefix} ip addr "${ipcmd}" "${back_ip6}/128" dev "${vif}"
|
|
|
|
|
fi
|
2019-05-30 15:31:57 +02:00
|
|
|
|
else
|
|
|
|
|
network_hooks "${command}" "${vif}"
|
2010-09-06 17:07:42 +02:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
log debug "Successful vif-route-qubes $command for $vif."
|
2019-05-28 18:56:44 +02:00
|
|
|
|
if [ "$command" = "online" ]; then
|
2010-09-06 17:07:42 +02:00
|
|
|
|
success
|
|
|
|
|
fi
|