File: ns.sh

package info (click to toggle)
vpnc-scripts 0.1~git20220510-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 272 kB
  • sloc: sh: 1,577; javascript: 193; ansic: 24; makefile: 12
file content (132 lines) | stat: -rw-r--r-- 4,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
#
# Copyright (C) 2018 Nikos Mavrogiannopoulos
#
# This file is part of ocserv.
#
# ocserv is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# ocserv is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Input:
#  ADDRESS=10.200.2.1
#  ADDRESS2=10.200.2.2
#  CLI_ADDRESS=10.200.1.1
#  CLI_ADDRESS2=10.200.1.2
#  VPNNET=192.168.1.0/24
#  VPNADDR=192.168.1.1
#
# Provides:
#  ${NSCMD1} - to run on NS1
#  ${NSCMD2} - to run on NS2
#  ${NSCMD3} - to run on NS3
#
# Cleanup is automatic via a trap
#  Requires: finish() to be defined
 

PATH=${PATH}:/usr/sbin
if test -z ${IP};then
	IP=$(type -P ip)
fi

if test "$(id -u)" != "0";then
	echo "This test must be run as root"
	exit 1
fi

${IP} netns list >/dev/null 2>&1
if test $? != 0;then
	echo "This test requires ip netns command"
	exit 1
fi

if test "$(uname -s)" != Linux;then
	echo "This test must be run on Linux"
	exit 1
fi

function nsfinish {
  set +e
  test -n "${ETHNAME1}" && ${IP} link delete ${ETHNAME1} >/dev/null 2>&1
  test -n "${ETHNAME2}" && ${IP} link delete ${ETHNAME2} >/dev/null 2>&1
  test -n "${ETHNAME3}" && ${IP} link delete ${ETHNAME3} >/dev/null 2>&1
  test -n "${ETHNAME4}" && ${IP} link delete ${ETHNAME4} >/dev/null 2>&1
  test -n "${NSNAME1}" && ${IP} netns delete ${NSNAME1} >/dev/null 2>&1
  test -n "${NSNAME2}" && ${IP} netns delete ${NSNAME2} >/dev/null 2>&1
  test -n "${NSNAME3}" && ${IP} netns delete ${NSNAME3} >/dev/null 2>&1

  finish
}
trap nsfinish EXIT

# ETHNAME1 and ETHNAME2 are a veth pair
# ETHNAME3 and ETHNAME4 are a veth pair
# NSNAME1 and NSNAME3 are client namespaces containing ETHNAME1 and ETHNAME3
# NSNAME2 is the server namespace containing ETHNAME2 and ETHNAME4

echo " * Setting up namespaces..."
set -e
NSNAME1="ocserv-c-tmp-$$"
NSNAME3="ocserv-c-2-tmp-$$"
NSNAME2="ocserv-s-tmp-$$"
ETHNAME1="oceth-c$$"
ETHNAME2="oceth-s$$"
ETHNAME3="oceth-c-2$$"
ETHNAME4="oceth-s-2$$"

${IP} netns add ${NSNAME1}
${IP} netns add ${NSNAME2}
${IP} netns add ${NSNAME3}

${IP} link add ${ETHNAME1} type veth peer name ${ETHNAME2}
${IP} link set ${ETHNAME1} netns ${NSNAME1}
${IP} link set ${ETHNAME2} netns ${NSNAME2}

${IP} link add ${ETHNAME3} type veth peer name ${ETHNAME4}
${IP} link set ${ETHNAME3} netns ${NSNAME3}
${IP} link set ${ETHNAME4} netns ${NSNAME2}

${IP} -n ${NSNAME1} link set ${ETHNAME1} up
${IP} -n ${NSNAME2} link set ${ETHNAME2} up
${IP} -n ${NSNAME3} link set ${ETHNAME3} up
${IP} -n ${NSNAME2} link set ${ETHNAME4} up
${IP} -n ${NSNAME2} link set lo up

${IP} -n ${NSNAME1} addr add ${CLI_ADDRESS} dev ${ETHNAME1}
${IP} -n ${NSNAME2} addr add ${ADDRESS} dev ${ETHNAME2}
test -n "${CLI_ADDRESS2}" && ${IP} -n ${NSNAME3} addr add ${CLI_ADDRESS2} dev ${ETHNAME3}
test -n "${ADDRESS2}" && ${IP} -n ${NSNAME2} addr add ${ADDRESS2} dev ${ETHNAME4}

${IP} -n ${NSNAME1} route add default via ${CLI_ADDRESS} dev ${ETHNAME1}
${IP} -n ${NSNAME2} route
${IP} -n ${NSNAME2} route add default via ${ADDRESS} dev ${ETHNAME2}

test -n "${CLI_ADDRESS2}" && ${IP} -n ${NSNAME3} route add default via ${CLI_ADDRESS2} dev ${ETHNAME3}
test -n "${ADDRESS2}" && ${IP} -n ${NSNAME2} route add ${CLI_ADDRESS2}/32 via ${ADDRESS2} dev ${ETHNAME4}

${IP} -n ${NSNAME2} addr
${IP} -n ${NSNAME2} route
${IP} -n ${NSNAME1} route
test -n "${CLI_ADDRESS2}" && ${IP} -n ${NSNAME3} route

${IP} netns exec ${NSNAME1} ping -c 1 ${ADDRESS} >/dev/null 
${IP} netns exec ${NSNAME2} ping -c 1 ${ADDRESS} >/dev/null 
${IP} netns exec ${NSNAME2} ping -c 1 ${CLI_ADDRESS} >/dev/null
test -n "${ADDRESS2}" && ${IP} netns exec ${NSNAME2} ping -c 1 ${ADDRESS2} >/dev/null 
test -n "${CLI_ADDRESS2}" && ${IP} netns exec ${NSNAME2} ping -c 1 ${CLI_ADDRESS2} >/dev/null 
set +e

CMDNS1="${IP} netns exec ${NSNAME1}"
CMDNS2="${IP} netns exec ${NSNAME2}"
CMDNS3="${IP} netns exec ${NSNAME3}"