vif-route-qubes 3.7 KB

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