vif-route-qubes 3.0 KB

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