vif-qubes-nat.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. #set -x
  3. undetectable_netvm_ips=
  4. netns="${vif}-nat"
  5. netvm_if="${vif}"
  6. netns_netvm_if="${vif}-p"
  7. netns_appvm_if="${vif}"
  8. #
  9. # .----------------------------------.
  10. # | NetVM/ProxyVM |
  11. # .------------.|.------------------. |
  12. # | AppVM ||| $netns namespace | |
  13. # | ||| | |
  14. # | eth0<--------->$netns_appvm_if | |
  15. # |$appvm_ip ||| $appvm_gw_ip | |
  16. # |$appvm_gw_ip||| ^ | |
  17. # '------------'|| |NAT | |
  18. # || v | |
  19. # || $netns_netvm_if<--->$netvm_if |
  20. # || $netvm_ip | $netvm_gw_ip|
  21. # |'------------------' |
  22. # '----------------------------------'
  23. #
  24. function run
  25. {
  26. #echo "$@" >> /var/log/qubes-nat.log
  27. "$@"
  28. }
  29. function netns
  30. {
  31. run ip netns exec "$netns" "$@"
  32. }
  33. run ip addr flush dev "$netns_appvm_if"
  34. run ip netns delete "$netns" || :
  35. if test "$command" == online; then
  36. run ip netns add "$netns"
  37. run ip link set "$netns_appvm_if" netns "$netns"
  38. # keep the same MAC as the real vif interface, so NetworkManager will still
  39. # ignore it
  40. run ip link add "$netns_netvm_if" type veth peer name "$netvm_if" address fe:ff:ff:ff:ff:ff
  41. run ip link set "$netns_netvm_if" netns "$netns"
  42. netns ip6tables -t raw -I PREROUTING -j DROP
  43. netns ip6tables -P INPUT DROP
  44. netns ip6tables -P FORWARD DROP
  45. netns ip6tables -P OUTPUT DROP
  46. netns sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
  47. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" ! -s "$appvm_ip" -j DROP
  48. if test -n "$undetectable_netvm_ips"; then
  49. # prevent an AppVM connecting to its own ProxyVM IP because that makes the internal IPs detectable even with no firewall rules
  50. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_ip" -j DROP
  51. # same for the gateway/DNS IPs
  52. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_gw_ip" -j DROP
  53. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_dns1_ip" -j DROP
  54. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_dns2_ip" -j DROP
  55. fi
  56. netns iptables -t nat -I PREROUTING -i "$netns_netvm_if" -j DNAT --to-destination "$appvm_ip"
  57. netns iptables -t nat -I POSTROUTING -o "$netns_netvm_if" -j SNAT --to-source "$netvm_ip"
  58. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_gw_ip" -j DNAT --to-destination "$netvm_gw_ip"
  59. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_gw_ip" -j SNAT --to-source "$appvm_gw_ip"
  60. if test -n "$appvm_dns1_ip"; then
  61. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_dns1_ip" -j DNAT --to-destination "$netvm_dns1_ip"
  62. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_dns1_ip" -j SNAT --to-source "$appvm_dns1_ip"
  63. fi
  64. if test -n "$appvm_dns2_ip"; then
  65. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_dns2_ip" -j DNAT --to-destination "$netvm_dns2_ip"
  66. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_dns2_ip" -j SNAT --to-source "$appvm_dns2_ip"
  67. fi
  68. netns ip addr add "$netvm_ip" dev "$netns_netvm_if"
  69. netns ip addr add "$appvm_gw_ip" dev "$netns_appvm_if"
  70. netns ip link set "$netns_netvm_if" up
  71. netns ip link set "$netns_appvm_if" up
  72. netns ip route add "$appvm_ip" dev "$netns_appvm_if" src "$appvm_gw_ip"
  73. netns ip route add "$netvm_gw_ip" dev "$netns_netvm_if" src "$netvm_ip"
  74. netns ip route add default via "$netvm_gw_ip" dev "$netns_netvm_if" src "$netvm_ip"
  75. #run ip addr add "$netvm_gw_ip" dev "$netvm_if"
  76. #run ip link set "$netvm_if" up
  77. #run ip route add "$netvm_ip" dev "$netvm_if" src "$netvm_gw_ip"
  78. fi