ns.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. #
  3. # Copyright (C) 2018 Nikos Mavrogiannopoulos
  4. #
  5. # This file is part of ocserv.
  6. #
  7. # ocserv is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by the
  9. # Free Software Foundation; either version 2 of the License, or (at
  10. # your option) any later version.
  11. #
  12. # ocserv is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # Input:
  21. # ADDRESS=10.200.2.1
  22. # CLI_ADDRESS=10.200.1.1
  23. # VPNNET=192.168.1.0/24
  24. # VPNADDR=192.168.1.1
  25. #
  26. # Provides:
  27. # ${NSCMD1} - to run on NS1
  28. # ${NSCMD2} - to run on NS2
  29. #
  30. # Cleanup is automatic via a trap
  31. # Requires: finish() to be defined
  32. PATH=${PATH}:/usr/sbin
  33. IP=$(which ip)
  34. if test "$(id -u)" != "0";then
  35. echo "This test must be run as root"
  36. exit 77
  37. fi
  38. ip netns list >/dev/null 2>&1
  39. if test $? != 0;then
  40. echo "This test requires ip netns command"
  41. exit 77
  42. fi
  43. if test "$(uname -s)" != Linux;then
  44. echo "This test must be run on Linux"
  45. exit 77
  46. fi
  47. function nsfinish {
  48. set +e
  49. test -n "${ETHNAME1}" && ${IP} link delete ${ETHNAME1} >/dev/null 2>&1
  50. test -n "${ETHNAME2}" && ${IP} link delete ${ETHNAME2} >/dev/null 2>&1
  51. test -n "${NSNAME1}" && ${IP} netns delete ${NSNAME1} >/dev/null 2>&1
  52. test -n "${NSNAME2}" && ${IP} netns delete ${NSNAME2} >/dev/null 2>&1
  53. finish
  54. }
  55. trap nsfinish EXIT
  56. echo " * Setting up namespaces..."
  57. set -e
  58. NSNAME1="ocserv-c-tmp-$$"
  59. NSNAME2="ocserv-s-tmp-$$"
  60. ETHNAME1="oceth-c$$"
  61. ETHNAME2="oceth-s$$"
  62. ${IP} netns add ${NSNAME1}
  63. ${IP} netns add ${NSNAME2}
  64. ${IP} link add ${ETHNAME1} type veth peer name ${ETHNAME2}
  65. ${IP} link set ${ETHNAME1} netns ${NSNAME1}
  66. ${IP} link set ${ETHNAME2} netns ${NSNAME2}
  67. ${IP} netns exec ${NSNAME1} ip link set ${ETHNAME1} up
  68. ${IP} netns exec ${NSNAME2} ip link set ${ETHNAME2} up
  69. ${IP} netns exec ${NSNAME2} ip link set lo up
  70. ${IP} netns exec ${NSNAME1} ip addr add ${CLI_ADDRESS} dev ${ETHNAME1}
  71. ${IP} netns exec ${NSNAME2} ip addr add ${ADDRESS} dev ${ETHNAME2}
  72. ${IP} netns exec ${NSNAME1} ip route add default via ${CLI_ADDRESS} dev ${ETHNAME1}
  73. ${IP} netns exec ${NSNAME2} ip route add default via ${ADDRESS} dev ${ETHNAME2}
  74. ${IP} netns exec ${NSNAME2} ip addr
  75. ${IP} netns exec ${NSNAME2} ip route
  76. ${IP} netns exec ${NSNAME1} ip route
  77. ${IP} netns exec ${NSNAME1} ping -c 1 ${ADDRESS} >/dev/null
  78. ${IP} netns exec ${NSNAME2} ping -c 1 ${ADDRESS} >/dev/null
  79. ${IP} netns exec ${NSNAME2} ping -c 1 ${CLI_ADDRESS} >/dev/null
  80. set +e
  81. CMDNS1="${IP} netns exec ${NSNAME1}"
  82. CMDNS2="${IP} netns exec ${NSNAME2}"