vif-route-qubes 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #main_ip=$(dom0_ip)
  26. # Network Hooks for triggering supplementary actions on AppVM connect
  27. network_hooks() {
  28. local command="$1"
  29. local vif="$2"
  30. local addr="$3"
  31. local vif_type
  32. vif_type="$(xenstore-read "${XENBUS_PATH}/type")"
  33. if [ -d /rw/config/network-hooks.d ]; then
  34. for hook in /rw/config/network-hooks.d/*
  35. do
  36. if [ -x "$hook" ]; then
  37. log debug "Executing network-hook $(basename "$hook")..."
  38. do_without_error "${hook}" "${command}" "${vif}" "${vif_type}" "${addr}"
  39. fi
  40. done
  41. fi
  42. }
  43. ipt_arg=
  44. if "iptables-restore" --help 2>&1 | grep -q wait=; then
  45. # 'wait' must be last on command line if secs not specified
  46. ipt_arg=--wait
  47. fi
  48. # shellcheck disable=SC2154
  49. if [ "${ip}" ]; then
  50. # get first IPv4 and first IPv6
  51. for addr in ${ip}; do
  52. if [ -z "$ip4" ] && [[ "$addr" = *.* ]]; then
  53. ip4="$addr"
  54. elif [ -z "$ip6" ] && [[ "$addr" = *:* ]]; then
  55. ip6="$addr"
  56. fi
  57. done
  58. # IPs as seen by this VM
  59. netvm_ip="$ip4"
  60. netvm_gw_ip=$(qubesdb-read /qubes-netvm-gateway)
  61. netvm_gw_ip6=$(qubesdb-read /qubes-netvm-gateway6 || :)
  62. netvm_dns1_ip=$(qubesdb-read /qubes-netvm-primary-dns)
  63. netvm_dns2_ip=$(qubesdb-read /qubes-netvm-secondary-dns)
  64. back_ip="$netvm_gw_ip"
  65. back_ip6="$netvm_gw_ip6"
  66. # IPs as seen by the VM - if other than $netvm_ip
  67. appvm_gw_ip="$(qubesdb-read "/mapped-ip/$ip4/visible-gateway" 2>/dev/null || :)"
  68. appvm_ip="$(qubesdb-read "/mapped-ip/$ip4/visible-ip" 2>/dev/null || :)"
  69. fi
  70. # Apply NAT if IP visible from the VM is different than the "real" one
  71. # See vif-qubes-nat.sh for details
  72. # XXX: supported only for the first IPv4 address, IPv6 is dropped if this
  73. # feature is enabled
  74. if [ -n "$appvm_ip" ] && [ -n "$appvm_gw_ip" ] && [ "$appvm_ip" != "$netvm_ip" ]; then
  75. # shellcheck disable=SC2154
  76. if test "$command" == online; then
  77. # shellcheck disable=SC2154
  78. echo 1 >"/proc/sys/net/ipv4/conf/${vif}/proxy_arp"
  79. fi
  80. # shellcheck source=network/vif-qubes-nat.sh
  81. . "$dir/vif-qubes-nat.sh"
  82. fi
  83. # shellcheck disable=SC2154
  84. case "$command" in
  85. online)
  86. ifconfig "${vif}" up
  87. echo 1 >"/proc/sys/net/ipv4/conf/${vif}/proxy_arp"
  88. ipcmd='add'
  89. iptables_cmd='-I PREROUTING 1'
  90. cmdprefix=''
  91. ;;
  92. offline)
  93. do_without_error ifdown "${vif}"
  94. ipcmd='del'
  95. iptables_cmd='-D PREROUTING'
  96. cmdprefix='do_without_error'
  97. ;;
  98. esac
  99. domid=${vif/vif/}
  100. domid=${domid/.*/}
  101. # metric must be possitive, but prefer later interface
  102. # 32752 is max XID aka domid
  103. metric=$(( 32752 - domid ))
  104. if [ "${ip}" ]; then
  105. # If we've been given a list of IP addresses, then add routes from dom0 to
  106. # the guest using those addresses.
  107. for addr in ${ip};
  108. do
  109. ${cmdprefix} ip route "${ipcmd}" "${addr}" dev "${vif}" metric "$metric"
  110. if [[ "$addr" = *:* ]]; then
  111. ipt=ip6tables-restore
  112. else
  113. ipt=iptables-restore
  114. fi
  115. echo -e "*raw\\n$iptables_cmd -i ${vif} ! -s ${addr} -j DROP\\nCOMMIT" | \
  116. ${cmdprefix} $ipt --noflush $ipt_arg
  117. network_hooks "${command}" "${vif}" "${addr}"
  118. done
  119. # if no IPv6 is assigned, block all IPv6 traffic on that interface
  120. if ! [[ "$ip" = *:* ]]; then
  121. echo -e "*raw\\n$iptables_cmd -i ${vif} -j DROP\\nCOMMIT" | \
  122. ${cmdprefix} ip6tables-restore --noflush $ipt_arg
  123. fi
  124. ${cmdprefix} ip addr "${ipcmd}" "${back_ip}/32" dev "${vif}"
  125. if [ "${back_ip6}" ] && [[ "${back_ip6}" != "fe80:"* ]]; then
  126. ${cmdprefix} ip addr "${ipcmd}" "${back_ip6}/128" dev "${vif}"
  127. fi
  128. else
  129. network_hooks "${command}" "${vif}"
  130. fi
  131. log debug "Successful vif-route-qubes $command for $vif."
  132. if [ "$command" = "online" ]; then
  133. # disable tx checksumming offload, apparently it doesn't work with our ancient qemu in stubdom
  134. do_without_error ethtool -K "$vif" tx off
  135. success
  136. fi