vif-route-qubes 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. #============================================================================
  3. # /etc/xen/vif-route-qubes
  4. #
  5. # Script for configuring a vif in routed mode.
  6. # The hotplugging system will call this script if it is specified either in
  7. # the device configuration given to Xend, or the default Xend configuration
  8. # in /etc/xen/xend-config.sxp. If the script is specified in neither of those
  9. # places, then vif-bridge is the default.
  10. #
  11. # Usage:
  12. # vif-route (add|remove|online|offline)
  13. #
  14. # Environment vars:
  15. # vif vif interface name (required).
  16. # XENBUS_PATH path to this device's details in the XenStore (required).
  17. #
  18. # Read from the store:
  19. # ip list of IP networks for the vif, space-separated (default given in
  20. # this script).
  21. #============================================================================
  22. dir=$(dirname "$0")
  23. # shellcheck disable=SC1091,SC1090
  24. . "$dir/vif-common.sh"
  25. set -o pipefail
  26. #main_ip=$(dom0_ip)
  27. # Network Hooks for triggering supplementary actions on AppVM connect
  28. network_hooks() {
  29. local command="$1"
  30. local vif="$2"
  31. local addr="$3"
  32. local vif_type
  33. vif_type="$(xenstore-read "${XENBUS_PATH}/type")"
  34. if [ -d /rw/config/network-hooks.d ]; then
  35. for hook in /rw/config/network-hooks.d/*
  36. do
  37. if [ -x "$hook" ]; then
  38. log debug "Executing network-hook $(basename "$hook")..."
  39. do_without_error "${hook}" "${command}" "${vif}" "${vif_type}" "${addr}"
  40. fi
  41. done
  42. fi
  43. }
  44. ipt_arg=
  45. if "iptables-restore" --help 2>&1 | grep -q wait=; then
  46. # 'wait' must be last on command line if secs not specified
  47. ipt_arg=--wait
  48. fi
  49. # shellcheck disable=SC2154
  50. if [ "${ip}" ]; then
  51. # get first IPv4 and first IPv6
  52. for addr in ${ip}; do
  53. if [ -z "$ip4" ] && [[ "$addr" = *.* ]]; then
  54. ip4="$addr"
  55. elif [ -z "$ip6" ] && [[ "$addr" = *:* ]]; then
  56. ip6="$addr"
  57. fi
  58. done
  59. # IPs as seen by this VM
  60. netvm_ip="$ip4"
  61. netvm_gw_ip=$(qubesdb-read /qubes-netvm-gateway)
  62. netvm_gw_ip6=$(qubesdb-read /qubes-netvm-gateway6 || :)
  63. netvm_dns1_ip=$(qubesdb-read /qubes-netvm-primary-dns)
  64. netvm_dns2_ip=$(qubesdb-read /qubes-netvm-secondary-dns)
  65. back_ip="$netvm_gw_ip"
  66. back_ip6="$netvm_gw_ip6"
  67. # IPs as seen by the VM - if other than $netvm_ip
  68. appvm_gw_ip="$(qubesdb-read "/mapped-ip/$ip4/visible-gateway" 2>/dev/null || :)"
  69. appvm_ip="$(qubesdb-read "/mapped-ip/$ip4/visible-ip" 2>/dev/null || :)"
  70. fi
  71. readonly max_domid=32752
  72. # if domid is 0 something is seriously wrong, so don’t check for that case
  73. if ! [[ $vif =~ ^vif([1-9][0-9]{,4})\.(0|[1-9][0-9]*)$ ]]; then
  74. printf 'Bad interface name %q\n' "$vif">&2
  75. exit 1
  76. fi
  77. domid=${BASH_REMATCH[1]} sub=${BASH_REMATCH[2]}
  78. # metric must be positive, but prefer later interface
  79. # 32752 is max XID aka domid
  80. if (( domid > max_domid )); then
  81. printf %s\\n "domid $domid too large"
  82. exit 1
  83. fi
  84. metric=$(( max_domid - domid ))
  85. # shellcheck disable=SC2154
  86. case "$command" in
  87. online)
  88. echo 1 >"/proc/sys/net/ipv4/conf/${vif}/proxy_arp"
  89. ipcmd='add'
  90. iptables_cmd='-I PREROUTING 1'
  91. cmdprefix=''
  92. ipv6_disabled=$(cat /proc/sys/net/ipv6/conf/"${vif}"/disable_ipv6 || echo 1)
  93. # without a MAC address we will fail later with a confusing error
  94. mac=$(xenstore-read "backend/vif/$domid/$sub/mac") || exit 1
  95. ;;
  96. offline)
  97. do_without_error ifdown "${vif}"
  98. ipcmd='del'
  99. iptables_cmd='-D PREROUTING'
  100. cmdprefix='do_without_error'
  101. # cleanup IPv6 config even if _now_ it is disabled
  102. ipv6_disabled=0
  103. ;;
  104. esac
  105. # Apply NAT if IP visible from the VM is different than the "real" one
  106. # See vif-qubes-nat.sh for details
  107. # XXX: supported only for the first IPv4 address, IPv6 is dropped if this
  108. # feature is enabled
  109. if [ -n "$appvm_ip" ] && [ -n "$appvm_gw_ip" ] && [ "$appvm_ip" != "$netvm_ip" ]; then
  110. # shellcheck source=network/vif-qubes-nat.sh
  111. . "$dir/vif-qubes-nat.sh"
  112. fi
  113. # add anti-spoofing rules before enabling the interface
  114. if [ "${ip}" ]; then
  115. # If we’ve been given a list of IP addresses, then add routes from us to
  116. # the VMs we serve using those addresses.
  117. for addr in ${ip};
  118. do
  119. if [[ "$addr" = *:* ]]; then
  120. ipt=ip6tables-restore
  121. else
  122. ipt=iptables-restore
  123. fi
  124. printf '%s\n' "*raw" \
  125. "$iptables_cmd -i ${vif} ! -s ${addr} -j DROP" \
  126. "$iptables_cmd ! -i vif+ -s ${addr} -j DROP" \
  127. "COMMIT" | \
  128. ${cmdprefix} $ipt --noflush $ipt_arg
  129. if [[ "$command" = 'online' ]]; then
  130. ip -- neighbour "${ipcmd}" to "${addr}" \
  131. dev "${vif}" lladdr "$mac" nud permanent
  132. fi
  133. done
  134. # if no IPv6 is assigned, block all IPv6 traffic on that interface
  135. if ! [[ "$ip" = *:* ]]; then
  136. echo -e "*raw\\n$iptables_cmd -i ${vif} -j DROP\\nCOMMIT" | \
  137. ${cmdprefix} ip6tables-restore --noflush $ipt_arg
  138. fi
  139. fi
  140. if [ "$command" = "online" ]; then
  141. ifconfig "${vif}" up
  142. fi
  143. if [ "${ip}" ]; then
  144. # If we've been given a list of IP addresses, then add routes from dom0 to
  145. # the guest using those addresses.
  146. for addr in ${ip};
  147. do
  148. if [[ "$addr" = *:* ]] && [[ "$ipv6_disabled" = 1 ]]; then
  149. log error "Cannot set IPv6 route to ${addr}, IPv6 disabled in the kernel"
  150. continue
  151. fi
  152. ${cmdprefix} ip route "${ipcmd}" "${addr}" dev "${vif}" metric "$metric"
  153. network_hooks "${command}" "${vif}" "${addr}"
  154. done
  155. ${cmdprefix} ip addr "${ipcmd}" "${back_ip}/32" dev "${vif}"
  156. if [ "${back_ip6}" ] && [[ "${back_ip6}" != "fe80:"* ]] && [[ "$ipv6_disabled" != 1 ]]; then
  157. ${cmdprefix} ip addr "${ipcmd}" "${back_ip6}/128" dev "${vif}"
  158. fi
  159. else
  160. network_hooks "${command}" "${vif}"
  161. fi
  162. log debug "Successful vif-route-qubes $command for $vif."
  163. if [ "$command" = "online" ]; then
  164. # disable tx checksumming offload, apparently it doesn't work with our ancient qemu in stubdom
  165. do_without_error ethtool -K "$vif" tx off
  166. success
  167. fi