vif-qubes-nat.sh 3.9 KB

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