ns-vpn 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #! /bin/bash
  2. usage() {
  3. printf "Usage: %s [-i <interface>] [-n <network number>] [-t <namespace suffix>] (up|start|stop|down)\n" "$0"
  4. }
  5. # set defaults
  6. iface=eth0
  7. namespace=ns-vpn
  8. n=1
  9. # source local config
  10. conf=/etc/ns-vpn.sh
  11. [[ -f "$conf" ]] && . "$conf"
  12. while getopts i:n:t:h opt; do
  13. case $opt in
  14. i)
  15. iface=$OPTARG
  16. ;;
  17. n)
  18. n=$OPTARG
  19. ;;
  20. t)
  21. namespace=ns-vpn-$OPTARG
  22. ;;
  23. h)
  24. usage
  25. exit
  26. ;;
  27. ?)
  28. 2>&1 usage
  29. exit 1
  30. ;;
  31. esac
  32. done
  33. net=192.168.$n
  34. shift "$(( OPTIND - 1 ))"
  35. if [[ "$#" -lt 1 ]]; then
  36. 2>&1 usage
  37. exit 1
  38. fi
  39. create_namespace() {
  40. gw=$1.1
  41. ip=$1.2
  42. ns=$2
  43. veth0=${ns}-0
  44. veth1=${ns}-1
  45. mkdir -p /etc/netns/$ns
  46. cp /etc/resolv.conf /etc/netns/$ns/
  47. /usr/bin/ip -b - << EOF
  48. link add $veth0 type veth peer name $veth1
  49. addr add $gw peer $ip dev $veth0
  50. link set dev $veth0 up
  51. netns add $ns
  52. link set $veth1 netns $ns
  53. netns exec $ns ip link set dev lo up
  54. netns exec $ns ip addr add $ip/24 dev $veth1
  55. netns exec $ns ip link set up dev $veth1
  56. netns exec $ns ip route add default via $gw dev $veth1
  57. netns exec $ns ip addr add $ip peer $gw dev $veth1
  58. netns exec $ns ip link set $veth1 up
  59. netns exec $ns ip route add 192.168.0.0/24 via $gw dev $veth1
  60. EOF
  61. }
  62. destroy_namespace() {
  63. sn=$1.0/24
  64. gw=$1.1
  65. ip=$1.2
  66. ns=$2
  67. veth1=${ns}-1
  68. /usr/bin/ip -b - << EOF
  69. netns exec $ns ip route del 192.168.0.0/24 via $gw
  70. netns exec $ns ip route del $sn
  71. netns exec $ns ip route del default via $gw
  72. netns exec $ns ip route del $gw
  73. netns exec $ns ip addr del $ip/32 peer $gw dev $veth1
  74. netns exec $ns ip link del $veth1
  75. netns del $ns
  76. EOF
  77. }
  78. iptables_rules=(
  79. "FORWARD -i ${namespace}-0 -o $iface -j ACCEPT"
  80. "FORWARD -i $iface -o ${namespace}-0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"
  81. )
  82. iptables_nat_rules=(
  83. "POSTROUTING -s $net.2/32 -o $iface -j MASQUERADE"
  84. )
  85. case "$*" in
  86. up|start)
  87. create_namespace $net $namespace
  88. for rule in "${iptables_nat_rules[@]}"; do
  89. iptables -t nat -A $rule
  90. done
  91. for rule in "${iptables_rules[@]}"; do
  92. iptables -A $rule
  93. done
  94. ;;
  95. down|stop)
  96. for rule in "${iptables_rules[@]}"; do
  97. iptables -D $rule
  98. done
  99. for rule in "${iptables_nat_rules[@]}"; do
  100. iptables -t nat -D $rule
  101. done
  102. destroy_namespace $net $namespace
  103. ;;
  104. ?)
  105. 2>&1 usage
  106. exit 1
  107. esac