t_ipsec_natt.sh revision 1.2.4.3 1 1.2.4.3 martin # $NetBSD: t_ipsec_natt.sh,v 1.2.4.3 2020/04/13 08:05:31 martin Exp $
2 1.2.4.2 christos #
3 1.2.4.2 christos # Copyright (c) 2018 Internet Initiative Japan Inc.
4 1.2.4.2 christos # All rights reserved.
5 1.2.4.2 christos #
6 1.2.4.2 christos # Redistribution and use in source and binary forms, with or without
7 1.2.4.2 christos # modification, are permitted provided that the following conditions
8 1.2.4.2 christos # are met:
9 1.2.4.2 christos # 1. Redistributions of source code must retain the above copyright
10 1.2.4.2 christos # notice, this list of conditions and the following disclaimer.
11 1.2.4.2 christos # 2. Redistributions in binary form must reproduce the above copyright
12 1.2.4.2 christos # notice, this list of conditions and the following disclaimer in the
13 1.2.4.2 christos # documentation and/or other materials provided with the distribution.
14 1.2.4.2 christos #
15 1.2.4.2 christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.2.4.2 christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.2.4.2 christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.2.4.2 christos # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.2.4.2 christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.2.4.2 christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.2.4.2 christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.2.4.2 christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.2.4.2 christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.2.4.2 christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.2.4.2 christos # POSSIBILITY OF SUCH DAMAGE.
26 1.2.4.2 christos #
27 1.2.4.2 christos
28 1.2.4.2 christos SOCK_LOCAL_A=unix://ipsec_natt_local_a
29 1.2.4.2 christos SOCK_LOCAL_B=unix://ipsec_natt_local_b
30 1.2.4.2 christos SOCK_NAT=unix://ipsec_natt_nat
31 1.2.4.2 christos SOCK_REMOTE=unix://ipsec_natt_remote
32 1.2.4.2 christos BUS_LOCAL=./bus_ipsec_natt_local
33 1.2.4.2 christos BUS_NAT=./bus_ipsec_natt_nat
34 1.2.4.2 christos
35 1.2.4.2 christos DEBUG=${DEBUG:-false}
36 1.2.4.2 christos HIJACKING_NPF="${HIJACKING},blanket=/dev/npf"
37 1.2.4.2 christos
38 1.2.4.2 christos setup_servers()
39 1.2.4.2 christos {
40 1.2.4.2 christos
41 1.2.4.2 christos rump_server_crypto_start $SOCK_LOCAL_A netipsec ipsec
42 1.2.4.2 christos rump_server_crypto_start $SOCK_LOCAL_B netipsec ipsec
43 1.2.4.2 christos rump_server_npf_start $SOCK_NAT
44 1.2.4.2 christos rump_server_crypto_start $SOCK_REMOTE netipsec ipsec
45 1.2.4.2 christos rump_server_add_iface $SOCK_LOCAL_A shmif0 $BUS_LOCAL
46 1.2.4.2 christos rump_server_add_iface $SOCK_LOCAL_B shmif0 $BUS_LOCAL
47 1.2.4.2 christos rump_server_add_iface $SOCK_NAT shmif0 $BUS_LOCAL
48 1.2.4.2 christos rump_server_add_iface $SOCK_NAT shmif1 $BUS_NAT
49 1.2.4.2 christos rump_server_add_iface $SOCK_REMOTE shmif0 $BUS_NAT
50 1.2.4.2 christos }
51 1.2.4.2 christos
52 1.2.4.2 christos setup_ipsecif()
53 1.2.4.2 christos {
54 1.2.4.2 christos local sock=$1
55 1.2.4.2 christos local ifid=$2
56 1.2.4.2 christos local src_ip=$3
57 1.2.4.2 christos local src_port=$4
58 1.2.4.2 christos local dst_ip=$5
59 1.2.4.2 christos local dst_port=$6
60 1.2.4.2 christos local ipsecif_ip=$7
61 1.2.4.2 christos local peer_ip=$8
62 1.2.4.2 christos
63 1.2.4.2 christos export RUMP_SERVER=$sock
64 1.2.4.3 martin rump_server_add_iface $sock ipsec$ifid
65 1.2.4.2 christos atf_check -s exit:0 rump.ifconfig ipsec$ifid link0 # enable NAT-T
66 1.2.4.2 christos atf_check -s exit:0 rump.ifconfig ipsec$ifid tunnel ${src_ip},${src_port} ${dst_ip},${dst_port}
67 1.2.4.2 christos atf_check -s exit:0 rump.ifconfig ipsec$ifid ${ipsecif_ip}/32
68 1.2.4.2 christos atf_check -s exit:0 -o ignore \
69 1.2.4.2 christos rump.route -n add ${peer_ip}/32 $ipsecif_ip
70 1.2.4.2 christos }
71 1.2.4.2 christos
72 1.2.4.2 christos add_sa()
73 1.2.4.2 christos {
74 1.2.4.2 christos local sock=$1
75 1.2.4.2 christos local proto=$2
76 1.2.4.2 christos local algo_args="$3"
77 1.2.4.2 christos local src_ip=$4
78 1.2.4.2 christos local src_port=$5
79 1.2.4.2 christos local dst_ip=$6
80 1.2.4.2 christos local dst_port=$7
81 1.2.4.2 christos local out_spi=$8
82 1.2.4.2 christos local in_spi=$9
83 1.2.4.2 christos local tmpfile=./tmp
84 1.2.4.2 christos
85 1.2.4.2 christos export RUMP_SERVER=$sock
86 1.2.4.2 christos cat > $tmpfile <<-EOF
87 1.2.4.2 christos add $src_ip [$src_port] $dst_ip [$dst_port] $proto $out_spi -m transport $algo_args;
88 1.2.4.2 christos add $dst_ip [$dst_port] $src_ip [$src_port] $proto $in_spi -m transport $algo_args;
89 1.2.4.2 christos EOF
90 1.2.4.2 christos $DEBUG && cat $tmpfile
91 1.2.4.2 christos atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
92 1.2.4.2 christos # XXX it can be expired if $lifetime is very short
93 1.2.4.2 christos #check_sa_entries $SOCK_LOCAL $ip_local $ip_remote
94 1.2.4.2 christos }
95 1.2.4.2 christos
96 1.2.4.2 christos prepare_file()
97 1.2.4.2 christos {
98 1.2.4.2 christos local file=$1
99 1.2.4.2 christos local data="0123456789"
100 1.2.4.2 christos
101 1.2.4.2 christos touch $file
102 1.2.4.2 christos for i in `seq 1 512`
103 1.2.4.2 christos do
104 1.2.4.2 christos echo $data >> $file
105 1.2.4.2 christos done
106 1.2.4.2 christos }
107 1.2.4.2 christos
108 1.2.4.2 christos build_npf_conf()
109 1.2.4.2 christos {
110 1.2.4.2 christos local outfile=$1
111 1.2.4.2 christos local localnet=$2
112 1.2.4.2 christos
113 1.2.4.2 christos cat > $outfile <<-EOF
114 1.2.4.2 christos set bpf.jit off
115 1.2.4.2 christos \$int_if = inet4(shmif0)
116 1.2.4.2 christos \$ext_if = inet4(shmif1)
117 1.2.4.2 christos \$localnet = { $localnet }
118 1.2.4.2 christos map \$ext_if dynamic \$localnet -> \$ext_if
119 1.2.4.2 christos group "external" on \$ext_if {
120 1.2.4.2 christos pass stateful out final all
121 1.2.4.2 christos }
122 1.2.4.2 christos group "internal" on \$int_if {
123 1.2.4.2 christos block in all
124 1.2.4.2 christos pass in final from \$localnet
125 1.2.4.2 christos pass out final all
126 1.2.4.2 christos }
127 1.2.4.2 christos group default {
128 1.2.4.2 christos pass final on lo0 all
129 1.2.4.2 christos block all
130 1.2.4.2 christos }
131 1.2.4.2 christos EOF
132 1.2.4.2 christos }
133 1.2.4.2 christos
134 1.2.4.2 christos PIDSFILE=./terminator.pids
135 1.2.4.2 christos start_natt_terminator()
136 1.2.4.2 christos {
137 1.2.4.2 christos local sock=$1
138 1.2.4.2 christos local ip=$2
139 1.2.4.2 christos local port=$3
140 1.2.4.2 christos local pidsfile=$4
141 1.2.4.2 christos local backup=$RUMP_SERVER
142 1.2.4.2 christos local pid=
143 1.2.4.2 christos local terminator="$(atf_get_srcdir)/../ipsec/natt_terminator"
144 1.2.4.2 christos
145 1.2.4.2 christos export RUMP_SERVER=$sock
146 1.2.4.2 christos
147 1.2.4.2 christos env LD_PRELOAD=/usr/lib/librumphijack.so \
148 1.2.4.2 christos $terminator $ip $port &
149 1.2.4.2 christos pid=$!
150 1.2.4.2 christos if [ ! -f $PIDSFILE ]; then
151 1.2.4.2 christos touch $PIDSFILE
152 1.2.4.2 christos fi
153 1.2.4.2 christos echo $pid >> $PIDSFILE
154 1.2.4.2 christos
155 1.2.4.2 christos $DEBUG && rump.netstat -a -f inet
156 1.2.4.2 christos
157 1.2.4.2 christos export RUMP_SERVER=$backup
158 1.2.4.2 christos
159 1.2.4.2 christos sleep 1
160 1.2.4.2 christos }
161 1.2.4.2 christos
162 1.2.4.2 christos stop_natt_terminators()
163 1.2.4.2 christos {
164 1.2.4.2 christos local pid=
165 1.2.4.2 christos
166 1.2.4.2 christos if [ ! -f $PIDSFILE ]; then
167 1.2.4.2 christos return
168 1.2.4.2 christos fi
169 1.2.4.2 christos
170 1.2.4.2 christos for pid in $(cat $PIDSFILE); do
171 1.2.4.2 christos kill -9 $pid
172 1.2.4.2 christos done
173 1.2.4.2 christos rm -f $PIDSFILE
174 1.2.4.2 christos }
175 1.2.4.2 christos
176 1.2.4.2 christos check_ping_packets()
177 1.2.4.2 christos {
178 1.2.4.2 christos local sock=$1
179 1.2.4.2 christos local bus=$2
180 1.2.4.2 christos local from_ip=$3
181 1.2.4.2 christos local to_ip=$4
182 1.2.4.2 christos
183 1.2.4.2 christos local outfile=./out.ping
184 1.2.4.2 christos
185 1.2.4.2 christos extract_new_packets $bus > $outfile
186 1.2.4.2 christos
187 1.2.4.2 christos export RUMP_SERVER=$sock
188 1.2.4.2 christos atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 3 $to_ip
189 1.2.4.2 christos
190 1.2.4.2 christos extract_new_packets $bus > $outfile
191 1.2.4.2 christos $DEBUG && cat $outfile
192 1.2.4.2 christos atf_check -s exit:0 \
193 1.2.4.2 christos -o match:"$from_ip > $to_ip: ICMP echo request" \
194 1.2.4.2 christos cat $outfile
195 1.2.4.2 christos atf_check -s exit:0 \
196 1.2.4.2 christos -o match:"$to_ip > $from_ip: ICMP echo reply" \
197 1.2.4.2 christos cat $outfile
198 1.2.4.2 christos }
199 1.2.4.2 christos
200 1.2.4.2 christos check_ping_packets_over_ipsecif()
201 1.2.4.2 christos {
202 1.2.4.2 christos local sock=$1
203 1.2.4.2 christos local bus=$2
204 1.2.4.2 christos local to_ip=$3
205 1.2.4.2 christos local nat_from_ip=$4
206 1.2.4.2 christos local nat_from_port=$5
207 1.2.4.2 christos local nat_to_ip=$6
208 1.2.4.2 christos local nat_to_port=$7
209 1.2.4.2 christos
210 1.2.4.2 christos local outfile=./out.ping_over_ipsecif
211 1.2.4.2 christos
212 1.2.4.2 christos extract_new_packets $bus > $outfile
213 1.2.4.2 christos
214 1.2.4.2 christos export RUMP_SERVER=$sock
215 1.2.4.2 christos atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 7 $to_ip
216 1.2.4.2 christos
217 1.2.4.2 christos # Check both ports and UDP encapsulation
218 1.2.4.2 christos extract_new_packets $bus > $outfile
219 1.2.4.2 christos $DEBUG && cat $outfile
220 1.2.4.2 christos atf_check -s exit:0 \
221 1.2.4.2 christos -o match:"${nat_from_ip}\.$nat_from_port > ${nat_to_ip}\.${nat_to_port}: UDP-encap" \
222 1.2.4.2 christos cat $outfile
223 1.2.4.2 christos atf_check -s exit:0 \
224 1.2.4.2 christos -o match:"${nat_to_ip}\.${nat_to_port} > ${nat_from_ip}\.${nat_from_port}: UDP-encap" \
225 1.2.4.2 christos cat $outfile
226 1.2.4.2 christos }
227 1.2.4.2 christos
228 1.2.4.2 christos check_tcp_com_prepare()
229 1.2.4.2 christos {
230 1.2.4.2 christos local server_sock=$1
231 1.2.4.2 christos local client_sock=$2
232 1.2.4.2 christos local bus=$3
233 1.2.4.2 christos local to_ip=$4
234 1.2.4.2 christos local nat_from_ip=$5
235 1.2.4.2 christos local nat_to_ip=$6
236 1.2.4.2 christos
237 1.2.4.2 christos local outfile=./out.prepare
238 1.2.4.2 christos local file_send=./file.send.prepare
239 1.2.4.2 christos local file_recv=./file.recv.prepare
240 1.2.4.2 christos
241 1.2.4.2 christos extract_new_packets $bus > $outfile
242 1.2.4.2 christos
243 1.2.4.2 christos start_nc_server $server_sock 4501 $file_recv ipv4
244 1.2.4.2 christos
245 1.2.4.2 christos prepare_file $file_send
246 1.2.4.2 christos export RUMP_SERVER=$client_sock
247 1.2.4.2 christos atf_check -s exit:0 $HIJACKING nc -w 3 $to_ip 4501 < $file_send
248 1.2.4.2 christos atf_check -s exit:0 diff -q $file_send $file_recv
249 1.2.4.2 christos extract_new_packets $bus > $outfile
250 1.2.4.2 christos $DEBUG && cat $outfile
251 1.2.4.2 christos atf_check -s exit:0 \
252 1.2.4.2 christos -o match:"${nat_from_ip}\.[0-9]+ > ${nat_to_ip}\.4501" \
253 1.2.4.2 christos cat $outfile
254 1.2.4.2 christos atf_check -s exit:0 \
255 1.2.4.2 christos -o match:"${nat_to_ip}\.4501 > ${nat_from_ip}\.[0-9]+" \
256 1.2.4.2 christos cat $outfile
257 1.2.4.2 christos
258 1.2.4.2 christos stop_nc_server
259 1.2.4.2 christos }
260 1.2.4.2 christos
261 1.2.4.2 christos check_tcp_com_over_ipsecif()
262 1.2.4.2 christos {
263 1.2.4.2 christos local server_sock=$1
264 1.2.4.2 christos local client_sock=$2
265 1.2.4.2 christos local bus=$3
266 1.2.4.2 christos local to_ip=$4
267 1.2.4.2 christos local nat_from_ip=$5
268 1.2.4.2 christos local nat_from_port=$6
269 1.2.4.2 christos local nat_to_ip=$7
270 1.2.4.2 christos local nat_to_port=$8
271 1.2.4.2 christos
272 1.2.4.2 christos local outfile=./out.ipsecif
273 1.2.4.2 christos local file_send=./file.send.ipsecif
274 1.2.4.2 christos local file_recv=./file.recv.ipsecif
275 1.2.4.2 christos
276 1.2.4.2 christos extract_new_packets $bus > $outfile
277 1.2.4.2 christos
278 1.2.4.2 christos start_nc_server $server_sock 4501 $file_recv ipv4
279 1.2.4.2 christos prepare_file $file_send
280 1.2.4.2 christos export RUMP_SERVER=$client_sock
281 1.2.4.2 christos atf_check -s exit:0 -o ignore $HIJACKING nc -w 7 $to_ip 4501 < $file_send
282 1.2.4.2 christos atf_check -s exit:0 diff -q $file_send $file_recv
283 1.2.4.2 christos stop_nc_server
284 1.2.4.2 christos
285 1.2.4.2 christos # Check both ports and UDP encapsulation
286 1.2.4.2 christos extract_new_packets $bus > $outfile
287 1.2.4.2 christos $DEBUG && cat $outfile
288 1.2.4.2 christos atf_check -s exit:0 \
289 1.2.4.2 christos -o match:"${nat_from_ip}\.$nat_from_port > ${nat_to_ip}\.${nat_to_port}: UDP-encap" \
290 1.2.4.2 christos cat $outfile
291 1.2.4.2 christos atf_check -s exit:0 \
292 1.2.4.2 christos -o match:"${nat_to_ip}\.${nat_to_port} > ${nat_from_ip}\.${nat_from_port}: UDP-encap" \
293 1.2.4.2 christos cat $outfile
294 1.2.4.2 christos }
295 1.2.4.2 christos
296 1.2.4.2 christos test_ipsecif_natt_transport()
297 1.2.4.2 christos {
298 1.2.4.2 christos local algo=$1
299 1.2.4.2 christos local ip_local_a=192.168.0.2
300 1.2.4.2 christos local ip_local_b=192.168.0.3
301 1.2.4.2 christos local ip_nat_local=192.168.0.1
302 1.2.4.2 christos local ip_nat_remote=10.0.0.1
303 1.2.4.2 christos local ip_remote=10.0.0.2
304 1.2.4.2 christos local subnet_local=192.168.0.0
305 1.2.4.2 christos local ip_local_ipsecif_a=172.16.100.1
306 1.2.4.2 christos local ip_local_ipsecif_b=172.16.110.1
307 1.2.4.2 christos local ip_remote_ipsecif_a=172.16.10.1
308 1.2.4.2 christos local ip_remote_ipsecif_b=172.16.11.1
309 1.2.4.2 christos
310 1.2.4.2 christos local npffile=./npf.conf
311 1.2.4.2 christos local file_send=./file.send
312 1.2.4.2 christos local algo_args="$(generate_algo_args esp-udp $algo)"
313 1.2.4.2 christos local pid= port_a= port_b=
314 1.2.4.2 christos
315 1.2.4.2 christos setup_servers
316 1.2.4.2 christos
317 1.2.4.2 christos export RUMP_SERVER=$SOCK_LOCAL_A
318 1.2.4.2 christos atf_check -s exit:0 rump.sysctl -q -w net.inet.ip.dad_count=0
319 1.2.4.2 christos atf_check -s exit:0 rump.ifconfig shmif0 $ip_local_a/24
320 1.2.4.2 christos atf_check -s exit:0 -o ignore \
321 1.2.4.2 christos rump.route -n add default $ip_nat_local
322 1.2.4.2 christos
323 1.2.4.2 christos export RUMP_SERVER=$SOCK_LOCAL_B
324 1.2.4.2 christos atf_check -s exit:0 rump.sysctl -q -w net.inet.ip.dad_count=0
325 1.2.4.2 christos atf_check -s exit:0 rump.ifconfig shmif0 $ip_local_b/24
326 1.2.4.2 christos atf_check -s exit:0 -o ignore \
327 1.2.4.2 christos rump.route -n add default $ip_nat_local
328 1.2.4.2 christos
329 1.2.4.2 christos export RUMP_SERVER=$SOCK_NAT
330 1.2.4.2 christos atf_check -s exit:0 rump.sysctl -q -w net.inet.ip.dad_count=0
331 1.2.4.2 christos atf_check -s exit:0 rump.ifconfig shmif0 $ip_nat_local/24
332 1.2.4.2 christos atf_check -s exit:0 rump.ifconfig shmif1 $ip_nat_remote/24
333 1.2.4.2 christos atf_check -s exit:0 rump.sysctl -q -w net.inet.ip.forwarding=1
334 1.2.4.2 christos
335 1.2.4.2 christos export RUMP_SERVER=$SOCK_REMOTE
336 1.2.4.2 christos atf_check -s exit:0 rump.sysctl -q -w net.inet.ip.dad_count=0
337 1.2.4.2 christos atf_check -s exit:0 rump.ifconfig shmif0 $ip_remote/24
338 1.2.4.2 christos atf_check -s exit:0 -o ignore \
339 1.2.4.2 christos rump.route -n add -net $subnet_local $ip_nat_remote
340 1.2.4.2 christos
341 1.2.4.2 christos # There is no NAT/NAPT. ping should just work.
342 1.2.4.2 christos check_ping_packets $SOCK_LOCAL_A $BUS_NAT $ip_local_a $ip_remote
343 1.2.4.2 christos check_ping_packets $SOCK_LOCAL_B $BUS_NAT $ip_local_b $ip_remote
344 1.2.4.2 christos
345 1.2.4.2 christos # Setup an NAPT with npf
346 1.2.4.2 christos build_npf_conf $npffile "$subnet_local/24"
347 1.2.4.2 christos
348 1.2.4.2 christos export RUMP_SERVER=$SOCK_NAT
349 1.2.4.2 christos atf_check -s exit:0 $HIJACKING_NPF npfctl reload $npffile
350 1.2.4.2 christos atf_check -s exit:0 $HIJACKING_NPF npfctl start
351 1.2.4.2 christos $DEBUG && ${HIJACKING},"blanket=/dev/npf" npfctl show
352 1.2.4.2 christos
353 1.2.4.2 christos # There is an NAPT. ping works but source IP/port are translated
354 1.2.4.2 christos check_ping_packets $SOCK_LOCAL_A $BUS_NAT $ip_nat_remote $ip_remote
355 1.2.4.2 christos check_ping_packets $SOCK_LOCAL_B $BUS_NAT $ip_nat_remote $ip_remote
356 1.2.4.2 christos
357 1.2.4.2 christos # Try TCP communications just in case
358 1.2.4.2 christos check_tcp_com_prepare $SOCK_REMOTE $SOCK_LOCAL_A $BUS_NAT \
359 1.2.4.2 christos $ip_remote $ip_nat_remote $ip_remote
360 1.2.4.2 christos check_tcp_com_prepare $SOCK_REMOTE $SOCK_LOCAL_B $BUS_NAT \
361 1.2.4.2 christos $ip_remote $ip_nat_remote $ip_remote
362 1.2.4.2 christos
363 1.2.4.2 christos # Launch a nc server as a terminator of NAT-T on outside the NAPT
364 1.2.4.2 christos start_natt_terminator $SOCK_REMOTE $ip_remote 4500
365 1.2.4.2 christos echo zzz > $file_send
366 1.2.4.2 christos
367 1.2.4.2 christos #################### Test for primary ipsecif(4) NAT-T.
368 1.2.4.2 christos
369 1.2.4.2 christos export RUMP_SERVER=$SOCK_LOCAL_A
370 1.2.4.2 christos # Send a UDP packet to the remote server at port 4500 from the local
371 1.2.4.2 christos # host of port 4500. This makes a mapping on the NAPT between them
372 1.2.4.2 christos atf_check -s exit:0 $HIJACKING \
373 1.2.4.2 christos nc -u -w 3 -p 4500 $ip_remote 4500 < $file_send
374 1.2.4.2 christos # Launch a nc server as a terminator of NAT-T on inside the NAPT,
375 1.2.4.2 christos # taking over port 4500 of the local host.
376 1.2.4.2 christos start_natt_terminator $SOCK_LOCAL_A $ip_local_a 4500
377 1.2.4.2 christos
378 1.2.4.2 christos # We need to keep the servers for NAT-T
379 1.2.4.2 christos
380 1.2.4.2 christos export RUMP_SERVER=$SOCK_LOCAL_A
381 1.2.4.2 christos $DEBUG && rump.netstat -na -f inet
382 1.2.4.2 christos export RUMP_SERVER=$SOCK_REMOTE
383 1.2.4.2 christos $DEBUG && rump.netstat -na -f inet
384 1.2.4.2 christos
385 1.2.4.2 christos # Get a translated port number from 4500 on the NAPT
386 1.2.4.2 christos export RUMP_SERVER=$SOCK_NAT
387 1.2.4.2 christos $DEBUG && $HIJACKING_NPF npfctl list
388 1.2.4.2 christos # 192.168.0.2:4500 10.0.0.2:4500 via shmif1:65248
389 1.2.4.2 christos port_a=$($HIJACKING_NPF npfctl list | grep $ip_local_a | awk -F 'shmif1:' '/4500/ {print $2;}')
390 1.2.4.2 christos $DEBUG && echo port_a=$port_a
391 1.2.4.2 christos if [ -z "$port_a" ]; then
392 1.2.4.2 christos atf_fail "Failed to get a traslated port on NAPT"
393 1.2.4.2 christos fi
394 1.2.4.2 christos
395 1.2.4.2 christos # Setup ESP-UDP ipsecif(4) for first client under NAPT
396 1.2.4.2 christos setup_ipsecif $SOCK_LOCAL_A 0 $ip_local_a 4500 $ip_remote 4500 \
397 1.2.4.2 christos $ip_local_ipsecif_a $ip_remote_ipsecif_a
398 1.2.4.2 christos setup_ipsecif $SOCK_REMOTE 0 $ip_remote 4500 $ip_nat_remote $port_a \
399 1.2.4.2 christos $ip_remote_ipsecif_a $ip_local_ipsecif_a
400 1.2.4.2 christos
401 1.2.4.2 christos add_sa $SOCK_LOCAL_A "esp-udp" "$algo_args" \
402 1.2.4.2 christos $ip_local_a 4500 $ip_remote 4500 10000 10001
403 1.2.4.2 christos add_sa $SOCK_REMOTE "esp-udp" "$algo_args" \
404 1.2.4.2 christos $ip_remote 4500 $ip_nat_remote $port_a 10001 10000
405 1.2.4.2 christos
406 1.2.4.2 christos export RUMP_SERVER=$SOCK_LOCAL_A
407 1.2.4.2 christos # ping should still work
408 1.2.4.2 christos atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 3 $ip_remote
409 1.2.4.2 christos
410 1.2.4.2 christos # Try ping over the ESP-UDP ipsecif(4)
411 1.2.4.2 christos check_ping_packets_over_ipsecif $SOCK_LOCAL_A $BUS_NAT \
412 1.2.4.2 christos $ip_remote_ipsecif_a $ip_nat_remote $port_a $ip_remote 4500
413 1.2.4.2 christos
414 1.2.4.2 christos # Try TCP communications over the ESP-UDP ipsecif(4)
415 1.2.4.2 christos check_tcp_com_over_ipsecif $SOCK_REMOTE $SOCK_LOCAL_A $BUS_NAT \
416 1.2.4.2 christos $ip_remote_ipsecif_a $ip_nat_remote $port_a $ip_remote 4500
417 1.2.4.2 christos
418 1.2.4.2 christos #################### Test for secondary ipsecif(4) NAT-T.
419 1.2.4.2 christos
420 1.2.4.2 christos export RUMP_SERVER=$SOCK_REMOTE
421 1.2.4.2 christos $HIJACKING setkey -D
422 1.2.4.2 christos $HIJACKING setkey -DP
423 1.2.4.2 christos
424 1.2.4.2 christos export RUMP_SERVER=$SOCK_LOCAL_B
425 1.2.4.2 christos # Send a UDP packet to the remote server at port 4500 from the local
426 1.2.4.2 christos # host of port 4500. This makes a mapping on the NAPT between them
427 1.2.4.2 christos atf_check -s exit:0 $HIJACKING \
428 1.2.4.2 christos nc -u -w 3 -p 4500 $ip_remote 4500 < $file_send
429 1.2.4.2 christos # Launch a nc server as a terminator of NAT-T on inside the NAPT,
430 1.2.4.2 christos # taking over port 4500 of the local host.
431 1.2.4.2 christos start_natt_terminator $SOCK_LOCAL_B $ip_local_b 4500
432 1.2.4.2 christos
433 1.2.4.2 christos # We need to keep the servers for NAT-T
434 1.2.4.2 christos
435 1.2.4.2 christos export RUMP_SERVER=$SOCK_LOCAL_B
436 1.2.4.2 christos $DEBUG && rump.netstat -na -f inet
437 1.2.4.2 christos export RUMP_SERVER=$SOCK_REMOTE
438 1.2.4.2 christos $DEBUG && rump.netstat -na -f inet
439 1.2.4.2 christos
440 1.2.4.2 christos # Get a translated port number from 4500 on the NAPT
441 1.2.4.2 christos export RUMP_SERVER=$SOCK_NAT
442 1.2.4.2 christos $DEBUG && $HIJACKING_NPF npfctl list
443 1.2.4.2 christos # 192.168.0.2:4500 10.0.0.2:4500 via shmif1:65248
444 1.2.4.2 christos port_b=$($HIJACKING_NPF npfctl list | grep $ip_local_b | awk -F 'shmif1:' '/4500/ {print $2;}')
445 1.2.4.2 christos $DEBUG && echo port_b=$port_b
446 1.2.4.2 christos if [ -z "$port_b" ]; then
447 1.2.4.2 christos atf_fail "Failed to get a traslated port on NAPT"
448 1.2.4.2 christos fi
449 1.2.4.2 christos
450 1.2.4.2 christos # Setup ESP-UDP ipsecif(4) for first client under NAPT
451 1.2.4.2 christos setup_ipsecif $SOCK_LOCAL_B 0 $ip_local_b 4500 $ip_remote 4500 \
452 1.2.4.2 christos $ip_local_ipsecif_b $ip_remote_ipsecif_b
453 1.2.4.2 christos setup_ipsecif $SOCK_REMOTE 1 $ip_remote 4500 $ip_nat_remote $port_b \
454 1.2.4.2 christos $ip_remote_ipsecif_b $ip_local_ipsecif_b
455 1.2.4.2 christos
456 1.2.4.2 christos check_ping_packets_over_ipsecif $SOCK_LOCAL_A $BUS_NAT \
457 1.2.4.2 christos $ip_remote_ipsecif_a $ip_nat_remote $port_a $ip_remote 4500
458 1.2.4.2 christos
459 1.2.4.2 christos add_sa $SOCK_LOCAL_B "esp-udp" "$algo_args" \
460 1.2.4.2 christos $ip_local_b 4500 $ip_remote 4500 11000 11001
461 1.2.4.2 christos add_sa $SOCK_REMOTE "esp-udp" "$algo_args" \
462 1.2.4.2 christos $ip_remote 4500 $ip_nat_remote $port_b 11001 11000
463 1.2.4.2 christos
464 1.2.4.2 christos export RUMP_SERVER=$SOCK_LOCAL_B
465 1.2.4.2 christos # ping should still work
466 1.2.4.2 christos atf_check -s exit:0 -o ignore rump.ping -c 1 -n -w 3 $ip_remote
467 1.2.4.2 christos
468 1.2.4.2 christos # Try ping over the ESP-UDP ipsecif(4)
469 1.2.4.2 christos check_ping_packets_over_ipsecif $SOCK_LOCAL_B $BUS_NAT \
470 1.2.4.2 christos $ip_remote_ipsecif_b $ip_nat_remote $port_b $ip_remote 4500
471 1.2.4.2 christos
472 1.2.4.2 christos
473 1.2.4.2 christos # Try TCP communications over the ESP-UDP ipsecif(4)
474 1.2.4.2 christos check_tcp_com_over_ipsecif $SOCK_REMOTE $SOCK_LOCAL_B $BUS_NAT \
475 1.2.4.2 christos $ip_remote_ipsecif_b $ip_nat_remote $port_b $ip_remote 4500
476 1.2.4.2 christos
477 1.2.4.2 christos # Try ping over the ESP-UDP ipsecif(4) for primary again
478 1.2.4.2 christos check_ping_packets_over_ipsecif $SOCK_LOCAL_A $BUS_NAT \
479 1.2.4.2 christos $ip_remote_ipsecif_a $ip_nat_remote $port_a $ip_remote 4500
480 1.2.4.2 christos
481 1.2.4.2 christos # Try TCP communications over the ESP-UDP ipsecif(4) for primary again
482 1.2.4.2 christos check_tcp_com_over_ipsecif $SOCK_REMOTE $SOCK_LOCAL_A $BUS_NAT \
483 1.2.4.2 christos $ip_remote_ipsecif_a $ip_nat_remote $port_a $ip_remote 4500
484 1.2.4.2 christos
485 1.2.4.2 christos # Kill the NAT-T terminator
486 1.2.4.2 christos stop_natt_terminators
487 1.2.4.2 christos }
488 1.2.4.2 christos
489 1.2.4.2 christos add_test_ipsecif_natt_transport()
490 1.2.4.2 christos {
491 1.2.4.2 christos local algo=$1
492 1.2.4.2 christos local _algo=$(echo $algo | sed 's/-//g')
493 1.2.4.2 christos local name= desc=
494 1.2.4.2 christos
495 1.2.4.2 christos desc="Test ipsecif(4) NAT-T ($algo)"
496 1.2.4.2 christos name="ipsecif_natt_transport_${_algo}"
497 1.2.4.2 christos
498 1.2.4.2 christos atf_test_case ${name} cleanup
499 1.2.4.2 christos eval "
500 1.2.4.2 christos ${name}_head() {
501 1.2.4.2 christos atf_set descr \"$desc\"
502 1.2.4.2 christos atf_set require.progs rump_server setkey nc
503 1.2.4.2 christos }
504 1.2.4.2 christos ${name}_body() {
505 1.2.4.2 christos test_ipsecif_natt_transport $algo
506 1.2.4.2 christos rump_server_destroy_ifaces
507 1.2.4.2 christos }
508 1.2.4.2 christos ${name}_cleanup() {
509 1.2.4.2 christos stop_nc_server
510 1.2.4.2 christos stop_natt_terminators
511 1.2.4.2 christos \$DEBUG && dump
512 1.2.4.2 christos cleanup
513 1.2.4.2 christos }
514 1.2.4.2 christos "
515 1.2.4.2 christos atf_add_test_case ${name}
516 1.2.4.2 christos }
517 1.2.4.2 christos
518 1.2.4.2 christos atf_init_test_cases()
519 1.2.4.2 christos {
520 1.2.4.2 christos local algo=
521 1.2.4.2 christos
522 1.2.4.2 christos for algo in $ESP_ENCRYPTION_ALGORITHMS_MINIMUM; do
523 1.2.4.2 christos add_test_ipsecif_natt_transport $algo
524 1.2.4.2 christos done
525 1.2.4.2 christos }
526