vif-route-qubes-nat 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. #============================================================================
  3. # /etc/xen/vif-route-qubes-nat
  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. appvm_gw_ip="$1"
  23. netvm_ip="$2"
  24. shift 2
  25. dir=$(dirname "$0")
  26. . "$dir/vif-common.sh"
  27. if [ "${ip}" ]; then
  28. appvm_ip="$ip"
  29. netvm_gw_ip=`qubesdb-read /qubes-netvm-gateway`
  30. netvm_dns2_ip=`qubesdb-read /qubes-netvm-secondary-dns`
  31. ip="$netvm_ip"
  32. back_ip="$netvm_gw_ip"
  33. fi
  34. #echo "$appvm_ip $appvm_gw_ip $netvm_ip $netvm_gw_ip" >> /var/log/qubes-nat.log
  35. #main_ip=$(dom0_ip)
  36. lockfile=/var/run/xen-hotplug/vif-lock
  37. if [ "${ip}" ]; then
  38. if test "$command" == online; then
  39. echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
  40. fi
  41. . "$dir/vif-qubes-nat.sh"
  42. fi
  43. case "$command" in
  44. online)
  45. ifconfig ${vif} up
  46. echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
  47. ipcmd='add'
  48. iptables_cmd='-I PREROUTING 1'
  49. cmdprefix=''
  50. ;;
  51. offline)
  52. do_without_error ifdown ${vif}
  53. ipcmd='del'
  54. iptables_cmd='-D PREROUTING'
  55. cmdprefix='do_without_error'
  56. ;;
  57. esac
  58. domid=${vif/vif/}
  59. domid=${domid/.*/}
  60. # metric must be possitive, but prefer later interface
  61. # 32752 is max XID aka domid
  62. metric=$[ 32752 - $domid ]
  63. if [ "${ip}" ] ; then
  64. # If we've been given a list of IP addresses, then add routes from dom0 to
  65. # the guest using those addresses.
  66. for addr in ${ip} ; do
  67. ${cmdprefix} ip route ${ipcmd} ${addr} dev ${vif} metric $metric
  68. done
  69. echo -e "*raw\n$iptables_cmd -i ${vif} ! -s ${ip} -j DROP\nCOMMIT" | \
  70. ${cmdprefix} flock $lockfile iptables-restore --noflush
  71. ${cmdprefix} ip addr ${ipcmd} ${back_ip}/32 dev ${vif}
  72. fi
  73. log debug "Successful vif-route-qubes-nat $command for $vif."
  74. if [ "$command" = "online" ]
  75. then
  76. # disable tx checksumming offload, apparently it doesn't work with our ancient qemu in stubdom
  77. do_without_error ethtool -K $vif tx off
  78. success
  79. fi