vif-route-qubes 4.0 KB

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