vif-qubes-nat.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. readonly netvm_mac=fe:ff:ff:ff:ff:ff
  26. function run
  27. {
  28. #echo "$@" >> /var/log/qubes-nat.log
  29. "$@"
  30. }
  31. function netns
  32. {
  33. if [[ "$1" = 'ip' ]]; then
  34. shift
  35. run ip -n "$netns" "$@"
  36. else
  37. run ip netns exec "$netns" "$@"
  38. fi
  39. }
  40. run ip addr flush dev "$netns_appvm_if"
  41. run ip netns delete "$netns" || :
  42. if test "$command" == online; then
  43. run ip netns add "$netns"
  44. run ip link set "$netns_appvm_if" netns "$netns"
  45. # keep the same MAC as the real vif interface, so NetworkManager will still
  46. # ignore it.
  47. # for the peer interface, make sure that it has the same MAC address
  48. # as the actual VM, so that our neighbor entry works.
  49. run ip link add name "$netns_netvm_if" address "$mac" type veth \
  50. peer name "$netvm_if" address "$netvm_mac"
  51. run ip link set dev "$netns_netvm_if" netns "$netns"
  52. netns ip6tables -t raw -I PREROUTING -j DROP
  53. netns ip6tables -P INPUT DROP
  54. netns ip6tables -P FORWARD DROP
  55. netns ip6tables -P OUTPUT DROP
  56. netns sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
  57. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" ! -s "$appvm_ip" -j DROP
  58. if test -n "$undetectable_netvm_ips"; then
  59. # prevent an AppVM connecting to its own ProxyVM IP because that makes the internal IPs detectable even with no firewall rules
  60. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_ip" -j DROP
  61. # same for the gateway/DNS IPs
  62. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_gw_ip" -j DROP
  63. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_dns1_ip" -j DROP
  64. netns iptables -t raw -I PREROUTING -i "$netns_appvm_if" -d "$netvm_dns2_ip" -j DROP
  65. fi
  66. netns iptables -t nat -I PREROUTING -i "$netns_netvm_if" -j DNAT --to-destination "$appvm_ip"
  67. netns iptables -t nat -I POSTROUTING -o "$netns_netvm_if" -j SNAT --to-source "$netvm_ip"
  68. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_gw_ip" -j DNAT --to-destination "$netvm_gw_ip"
  69. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_gw_ip" -j SNAT --to-source "$appvm_gw_ip"
  70. if test -n "$appvm_dns1_ip"; then
  71. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_dns1_ip" -j DNAT --to-destination "$netvm_dns1_ip"
  72. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_dns1_ip" -j SNAT --to-source "$appvm_dns1_ip"
  73. fi
  74. if test -n "$appvm_dns2_ip"; then
  75. netns iptables -t nat -I PREROUTING -i "$netns_appvm_if" -d "$appvm_dns2_ip" -j DNAT --to-destination "$netvm_dns2_ip"
  76. netns iptables -t nat -I POSTROUTING -o "$netns_appvm_if" -s "$netvm_dns2_ip" -j SNAT --to-source "$appvm_dns2_ip"
  77. fi
  78. netns ip neighbour add to "$appvm_ip" dev "$netns_appvm_if" lladdr "$mac" nud permanent
  79. netns ip neighbour add to "$netvm_gw_ip" dev "$netns_netvm_if" lladdr "$netvm_mac" nud permanent
  80. netns ip addr add "$netvm_ip" dev "$netns_netvm_if"
  81. netns ip addr add "$appvm_gw_ip" dev "$netns_appvm_if"
  82. netns ip link set "$netns_netvm_if" up
  83. netns ip link set "$netns_appvm_if" up
  84. netns ip route add "$appvm_ip" dev "$netns_appvm_if" src "$appvm_gw_ip"
  85. netns ip route add "$netvm_gw_ip" dev "$netns_netvm_if" src "$netvm_ip"
  86. netns ip route add default via "$netvm_gw_ip" dev "$netns_netvm_if" src "$netvm_ip"
  87. fi