vif-qubes-nat.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # for the peer interface, make sure that it has the same MAC address
  42. # as the actual VM, so that our neighbor entry works.
  43. run ip link add name "$netns_netvm_if" address "$mac" type veth \
  44. peer name "$netvm_if" address fe:ff:ff:ff:ff:ff
  45. run ip link set dev "$netns_netvm_if" netns "$netns"
  46. netns ip6tables -t raw -I PREROUTING -j DROP
  47. netns ip6tables -P INPUT DROP
  48. netns ip6tables -P FORWARD DROP
  49. netns ip6tables -P OUTPUT DROP
  50. netns sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
  51. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" ! -s "$appvm_ip" -j DROP
  52. if test -n "$undetectable_netvm_ips"; then
  53. # prevent an AppVM connecting to its own ProxyVM IP because that makes the internal IPs detectable even with no firewall rules
  54. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_ip" -j DROP
  55. # same for the gateway/DNS IPs
  56. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_gw_ip" -j DROP
  57. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_dns1_ip" -j DROP
  58. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_dns2_ip" -j DROP
  59. fi
  60. netns iptables -t nat -I PREROUTING -i "$netns_netvm_if" -j DNAT --to-destination "$appvm_ip"
  61. netns iptables -t nat -I POSTROUTING -o "$netns_netvm_if" -j SNAT --to-source "$netvm_ip"
  62. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_gw_ip" -j DNAT --to-destination "$netvm_gw_ip"
  63. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_gw_ip" -j SNAT --to-source "$appvm_gw_ip"
  64. if test -n "$appvm_dns1_ip"; then
  65. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_dns1_ip" -j DNAT --to-destination "$netvm_dns1_ip"
  66. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_dns1_ip" -j SNAT --to-source "$appvm_dns1_ip"
  67. fi
  68. if test -n "$appvm_dns2_ip"; then
  69. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_dns2_ip" -j DNAT --to-destination "$netvm_dns2_ip"
  70. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_dns2_ip" -j SNAT --to-source "$appvm_dns2_ip"
  71. fi
  72. netns ip neighbour add to "$appvm_ip" dev "$netns_appvm_if" lladdr "$mac" nud permanent
  73. netns ip addr add "$netvm_ip" dev "$netns_netvm_if"
  74. netns ip addr add "$appvm_gw_ip" dev "$netns_appvm_if"
  75. netns ip link set "$netns_netvm_if" up
  76. netns ip link set "$netns_appvm_if" up
  77. netns ip route add "$appvm_ip" dev "$netns_appvm_if" src "$appvm_gw_ip"
  78. netns ip route add "$netvm_gw_ip" dev "$netns_netvm_if" src "$netvm_ip"
  79. netns ip route add default via "$netvm_gw_ip" dev "$netns_netvm_if" src "$netvm_ip"
  80. #run ip addr add "$netvm_gw_ip" dev "$netvm_if"
  81. #run ip link set "$netvm_if" up
  82. #run ip route add "$netvm_ip" dev "$netvm_if" src "$netvm_gw_ip"
  83. fi