powerpc/perf/hv-24x7: Move cpumask file to top folder of hv-24x7 driver
[linux-2.6-microblaze.git] / tools / testing / selftests / net / icmp_redirect.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 #
4 # redirect test
5 #
6 #                     .253 +----+
7 #                     +----| r1 |
8 #                     |    +----+
9 # +----+              |       |.1
10 # | h1 |--------------+       |   10.1.1.0/30 2001:db8:1::0/126
11 # +----+ .1           |       |.2
12 #         172.16.1/24 |    +----+                   +----+
13 #    2001:db8:16:1/64 +----| r2 |-------------------| h2 |
14 #                     .254 +----+ .254           .2 +----+
15 #                                    172.16.2/24
16 #                                  2001:db8:16:2/64
17 #
18 # Route from h1 to h2 goes through r1, eth1 - connection between r1 and r2.
19 # Route on r1 changed to go to r2 via eth0. This causes a redirect to be sent
20 # from r1 to h1 telling h1 to use r2 when talking to h2.
21
22 VERBOSE=0
23 PAUSE_ON_FAIL=no
24
25 H1_N1_IP=172.16.1.1
26 R1_N1_IP=172.16.1.253
27 R2_N1_IP=172.16.1.254
28
29 H1_N1_IP6=2001:db8:16:1::1
30 R1_N1_IP6=2001:db8:16:1::253
31 R2_N1_IP6=2001:db8:16:1::254
32
33 R1_R2_N1_IP=10.1.1.1
34 R2_R1_N1_IP=10.1.1.2
35
36 R1_R2_N1_IP6=2001:db8:1::1
37 R2_R1_N1_IP6=2001:db8:1::2
38
39 H2_N2=172.16.2.0/24
40 H2_N2_6=2001:db8:16:2::/64
41 H2_N2_IP=172.16.2.2
42 R2_N2_IP=172.16.2.254
43 H2_N2_IP6=2001:db8:16:2::2
44 R2_N2_IP6=2001:db8:16:2::254
45
46 VRF=red
47 VRF_TABLE=1111
48
49 ################################################################################
50 # helpers
51
52 log_section()
53 {
54         echo
55         echo "###########################################################################"
56         echo "$*"
57         echo "###########################################################################"
58         echo
59 }
60
61 log_test()
62 {
63         local rc=$1
64         local expected=$2
65         local msg="$3"
66
67         if [ ${rc} -eq ${expected} ]; then
68                 printf "TEST: %-60s  [ OK ]\n" "${msg}"
69                 nsuccess=$((nsuccess+1))
70         else
71                 ret=1
72                 nfail=$((nfail+1))
73                 printf "TEST: %-60s  [FAIL]\n" "${msg}"
74                 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
75                         echo
76                         echo "hit enter to continue, 'q' to quit"
77                         read a
78                         [ "$a" = "q" ] && exit 1
79                 fi
80         fi
81 }
82
83 log_debug()
84 {
85         if [ "$VERBOSE" = "1" ]; then
86                 echo "$*"
87         fi
88 }
89
90 run_cmd()
91 {
92         local cmd="$*"
93         local out
94         local rc
95
96         if [ "$VERBOSE" = "1" ]; then
97                 echo "COMMAND: $cmd"
98         fi
99
100         out=$(eval $cmd 2>&1)
101         rc=$?
102         if [ "$VERBOSE" = "1" -a -n "$out" ]; then
103                 echo "$out"
104         fi
105
106         [ "$VERBOSE" = "1" ] && echo
107
108         return $rc
109 }
110
111 get_linklocal()
112 {
113         local ns=$1
114         local dev=$2
115         local addr
116
117         addr=$(ip -netns $ns -6 -br addr show dev ${dev} | \
118         awk '{
119                 for (i = 3; i <= NF; ++i) {
120                         if ($i ~ /^fe80/)
121                                 print $i
122                 }
123         }'
124         )
125         addr=${addr/\/*}
126
127         [ -z "$addr" ] && return 1
128
129         echo $addr
130
131         return 0
132 }
133
134 ################################################################################
135 # setup and teardown
136
137 cleanup()
138 {
139         local ns
140
141         for ns in h1 h2 r1 r2; do
142                 ip netns del $ns 2>/dev/null
143         done
144 }
145
146 create_vrf()
147 {
148         local ns=$1
149
150         ip -netns ${ns} link add ${VRF} type vrf table ${VRF_TABLE}
151         ip -netns ${ns} link set ${VRF} up
152         ip -netns ${ns} route add vrf ${VRF} unreachable default metric 8192
153         ip -netns ${ns} -6 route add vrf ${VRF} unreachable default metric 8192
154
155         ip -netns ${ns} addr add 127.0.0.1/8 dev ${VRF}
156         ip -netns ${ns} -6 addr add ::1 dev ${VRF} nodad
157
158         ip -netns ${ns} ru del pref 0
159         ip -netns ${ns} ru add pref 32765 from all lookup local
160         ip -netns ${ns} -6 ru del pref 0
161         ip -netns ${ns} -6 ru add pref 32765 from all lookup local
162 }
163
164 setup()
165 {
166         local ns
167
168         #
169         # create nodes as namespaces
170         #
171         for ns in h1 h2 r1 r2; do
172                 ip netns add $ns
173                 ip -netns $ns li set lo up
174
175                 case "${ns}" in
176                 h[12]) ip netns exec $ns sysctl -q -w net.ipv4.conf.all.accept_redirects=1
177                        ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=0
178                        ip netns exec $ns sysctl -q -w net.ipv6.conf.all.accept_redirects=1
179                        ip netns exec $ns sysctl -q -w net.ipv6.conf.all.keep_addr_on_down=1
180                         ;;
181                 r[12]) ip netns exec $ns sysctl -q -w net.ipv4.ip_forward=1
182                        ip netns exec $ns sysctl -q -w net.ipv4.conf.all.send_redirects=1
183
184                        ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=1
185                        ip netns exec $ns sysctl -q -w net.ipv6.route.mtu_expires=10
186                 esac
187         done
188
189         #
190         # create interconnects
191         #
192         ip -netns h1 li add eth0 type veth peer name r1h1
193         ip -netns h1 li set r1h1 netns r1 name eth0 up
194
195         ip -netns h1 li add eth1 type veth peer name r2h1
196         ip -netns h1 li set r2h1 netns r2 name eth0 up
197
198         ip -netns h2 li add eth0 type veth peer name r2h2
199         ip -netns h2 li set eth0 up
200         ip -netns h2 li set r2h2 netns r2 name eth2 up
201
202         ip -netns r1 li add eth1 type veth peer name r2r1
203         ip -netns r1 li set eth1 up
204         ip -netns r1 li set r2r1 netns r2 name eth1 up
205
206         #
207         # h1
208         #
209         if [ "${WITH_VRF}" = "yes" ]; then
210                 create_vrf "h1"
211                 H1_VRF_ARG="vrf ${VRF}"
212                 H1_PING_ARG="-I ${VRF}"
213         else
214                 H1_VRF_ARG=
215                 H1_PING_ARG=
216         fi
217         ip -netns h1 li add br0 type bridge
218         if [ "${WITH_VRF}" = "yes" ]; then
219                 ip -netns h1 li set br0 vrf ${VRF} up
220         else
221                 ip -netns h1 li set br0 up
222         fi
223         ip -netns h1 addr add dev br0 ${H1_N1_IP}/24
224         ip -netns h1 -6 addr add dev br0 ${H1_N1_IP6}/64 nodad
225         ip -netns h1 li set eth0 master br0 up
226         ip -netns h1 li set eth1 master br0 up
227
228         #
229         # h2
230         #
231         ip -netns h2 addr add dev eth0 ${H2_N2_IP}/24
232         ip -netns h2 ro add default via ${R2_N2_IP} dev eth0
233         ip -netns h2 -6 addr add dev eth0 ${H2_N2_IP6}/64 nodad
234         ip -netns h2 -6 ro add default via ${R2_N2_IP6} dev eth0
235
236         #
237         # r1
238         #
239         ip -netns r1 addr add dev eth0 ${R1_N1_IP}/24
240         ip -netns r1 -6 addr add dev eth0 ${R1_N1_IP6}/64 nodad
241         ip -netns r1 addr add dev eth1 ${R1_R2_N1_IP}/30
242         ip -netns r1 -6 addr add dev eth1 ${R1_R2_N1_IP6}/126 nodad
243
244         #
245         # r2
246         #
247         ip -netns r2 addr add dev eth0 ${R2_N1_IP}/24
248         ip -netns r2 -6 addr add dev eth0 ${R2_N1_IP6}/64 nodad
249         ip -netns r2 addr add dev eth1 ${R2_R1_N1_IP}/30
250         ip -netns r2 -6 addr add dev eth1 ${R2_R1_N1_IP6}/126 nodad
251         ip -netns r2 addr add dev eth2 ${R2_N2_IP}/24
252         ip -netns r2 -6 addr add dev eth2 ${R2_N2_IP6}/64 nodad
253
254         sleep 2
255
256         R1_LLADDR=$(get_linklocal r1 eth0)
257         if [ $? -ne 0 ]; then
258                 echo "Error: Failed to get link-local address of r1's eth0"
259                 exit 1
260         fi
261         log_debug "initial gateway is R1's lladdr = ${R1_LLADDR}"
262
263         R2_LLADDR=$(get_linklocal r2 eth0)
264         if [ $? -ne 0 ]; then
265                 echo "Error: Failed to get link-local address of r2's eth0"
266                 exit 1
267         fi
268         log_debug "initial gateway is R2's lladdr = ${R2_LLADDR}"
269 }
270
271 change_h2_mtu()
272 {
273         local mtu=$1
274
275         run_cmd ip -netns h2 li set eth0 mtu ${mtu}
276         run_cmd ip -netns r2 li set eth2 mtu ${mtu}
277 }
278
279 check_exception()
280 {
281         local mtu="$1"
282         local with_redirect="$2"
283         local desc="$3"
284
285         # From 172.16.1.101: icmp_seq=1 Redirect Host(New nexthop: 172.16.1.102)
286         if [ "$VERBOSE" = "1" ]; then
287                 echo "Commands to check for exception:"
288                 run_cmd ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP}
289                 run_cmd ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6}
290         fi
291
292         if [ -n "${mtu}" ]; then
293                 mtu=" mtu ${mtu}"
294         fi
295         if [ "$with_redirect" = "yes" ]; then
296                 ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
297                 grep -q "cache <redirected> expires [0-9]*sec${mtu}"
298         elif [ -n "${mtu}" ]; then
299                 ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
300                 grep -q "cache expires [0-9]*sec${mtu}"
301         else
302                 # want to verify that neither mtu nor redirected appears in
303                 # the route get output. The -v will wipe out the cache line
304                 # if either are set so the last grep -q will not find a match
305                 ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
306                 grep -E -v 'mtu|redirected' | grep -q "cache"
307         fi
308         log_test $? 0 "IPv4: ${desc}"
309
310         if [ "$with_redirect" = "yes" ]; then
311                 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
312                 grep -q "${H2_N2_IP6} from :: via ${R2_LLADDR} dev br0.*${mtu}"
313         elif [ -n "${mtu}" ]; then
314                 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
315                 grep -q "${mtu}"
316         else
317                 # IPv6 is a bit harder. First strip out the match if it
318                 # contains an mtu exception and then look for the first
319                 # gateway - R1's lladdr
320                 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
321                 grep -v "mtu" | grep -q "${R1_LLADDR}"
322         fi
323         log_test $? 0 "IPv6: ${desc}"
324 }
325
326 run_ping()
327 {
328         local sz=$1
329
330         run_cmd ip netns exec h1 ping -q -M want -i 0.5 -c 10 -w 2 -s ${sz} ${H1_PING_ARG} ${H2_N2_IP}
331         run_cmd ip netns exec h1 ${ping6} -q -M want -i 0.5 -c 10 -w 2 -s ${sz} ${H1_PING_ARG} ${H2_N2_IP6}
332 }
333
334 replace_route_new()
335 {
336         # r1 to h2 via r2 and eth0
337         run_cmd ip -netns r1 nexthop replace id 1 via ${R2_N1_IP} dev eth0
338         run_cmd ip -netns r1 nexthop replace id 2 via ${R2_LLADDR} dev eth0
339 }
340
341 reset_route_new()
342 {
343         run_cmd ip -netns r1 nexthop flush
344         run_cmd ip -netns h1 nexthop flush
345
346         initial_route_new
347 }
348
349 initial_route_new()
350 {
351         # r1 to h2 via r2 and eth1
352         run_cmd ip -netns r1 nexthop add id 1 via ${R2_R1_N1_IP} dev eth1
353         run_cmd ip -netns r1 ro add ${H2_N2} nhid 1
354
355         run_cmd ip -netns r1 nexthop add id 2 via ${R2_R1_N1_IP6} dev eth1
356         run_cmd ip -netns r1 -6 ro add ${H2_N2_6} nhid 2
357
358         # h1 to h2 via r1
359         run_cmd ip -netns h1 nexthop add id 1 via ${R1_N1_IP} dev br0
360         run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} nhid 1
361
362         run_cmd ip -netns h1 nexthop add id 2 via ${R1_LLADDR} dev br0
363         run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} nhid 2
364 }
365
366 replace_route_legacy()
367 {
368         # r1 to h2 via r2 and eth0
369         run_cmd ip -netns r1    ro replace ${H2_N2}   via ${R2_N1_IP}  dev eth0
370         run_cmd ip -netns r1 -6 ro replace ${H2_N2_6} via ${R2_LLADDR} dev eth0
371 }
372
373 reset_route_legacy()
374 {
375         run_cmd ip -netns r1    ro del ${H2_N2}
376         run_cmd ip -netns r1 -6 ro del ${H2_N2_6}
377
378         run_cmd ip -netns h1    ro del ${H1_VRF_ARG} ${H2_N2}
379         run_cmd ip -netns h1 -6 ro del ${H1_VRF_ARG} ${H2_N2_6}
380
381         initial_route_legacy
382 }
383
384 initial_route_legacy()
385 {
386         # r1 to h2 via r2 and eth1
387         run_cmd ip -netns r1    ro add ${H2_N2}   via ${R2_R1_N1_IP}  dev eth1
388         run_cmd ip -netns r1 -6 ro add ${H2_N2_6} via ${R2_R1_N1_IP6} dev eth1
389
390         # h1 to h2 via r1
391         # - IPv6 redirect only works if gateway is the LLA
392         run_cmd ip -netns h1    ro add ${H1_VRF_ARG} ${H2_N2} via ${R1_N1_IP} dev br0
393         run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} via ${R1_LLADDR} dev br0
394 }
395
396 check_connectivity()
397 {
398         local rc
399
400         run_cmd ip netns exec h1 ping -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP}
401         rc=$?
402         run_cmd ip netns exec h1 ${ping6} -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP6}
403         [ $? -ne 0 ] && rc=$?
404
405         return $rc
406 }
407
408 do_test()
409 {
410         local ttype="$1"
411
412         eval initial_route_${ttype}
413
414         # verify connectivity
415         check_connectivity
416         if [ $? -ne 0 ]; then
417                 echo "Error: Basic connectivity is broken"
418                 ret=1
419                 return
420         fi
421
422         # redirect exception followed by mtu
423         eval replace_route_${ttype}
424         run_ping 64
425         check_exception "" "yes" "redirect exception"
426
427         check_connectivity
428         if [ $? -ne 0 ]; then
429                 echo "Error: Basic connectivity is broken after redirect"
430                 ret=1
431                 return
432         fi
433
434         change_h2_mtu 1300
435         run_ping 1350
436         check_exception "1300" "yes" "redirect exception plus mtu"
437
438         # remove exceptions and restore routing
439         change_h2_mtu 1500
440         eval reset_route_${ttype}
441
442         check_connectivity
443         if [ $? -ne 0 ]; then
444                 echo "Error: Basic connectivity is broken after reset"
445                 ret=1
446                 return
447         fi
448         check_exception "" "no" "routing reset"
449
450         # MTU exception followed by redirect
451         change_h2_mtu 1300
452         run_ping 1350
453         check_exception "1300" "no" "mtu exception"
454
455         eval replace_route_${ttype}
456         run_ping 64
457         check_exception "1300" "yes" "mtu exception plus redirect"
458
459         check_connectivity
460         if [ $? -ne 0 ]; then
461                 echo "Error: Basic connectivity is broken after redirect"
462                 ret=1
463                 return
464         fi
465 }
466
467 ################################################################################
468 # usage
469
470 usage()
471 {
472         cat <<EOF
473 usage: ${0##*/} OPTS
474
475         -p          Pause on fail
476         -v          verbose mode (show commands and output)
477 EOF
478 }
479
480 ################################################################################
481 # main
482
483 # Some systems don't have a ping6 binary anymore
484 which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
485
486 ret=0
487 nsuccess=0
488 nfail=0
489
490 while getopts :pv o
491 do
492         case $o in
493                 p) PAUSE_ON_FAIL=yes;;
494                 v) VERBOSE=$(($VERBOSE + 1));;
495                 *) usage; exit 1;;
496         esac
497 done
498
499 trap cleanup EXIT
500
501 cleanup
502 WITH_VRF=no
503 setup
504
505 log_section "Legacy routing"
506 do_test "legacy"
507
508 cleanup
509 log_section "Legacy routing with VRF"
510 WITH_VRF=yes
511 setup
512 do_test "legacy"
513
514 cleanup
515 log_section "Routing with nexthop objects"
516 ip nexthop ls >/dev/null 2>&1
517 if [ $? -eq 0 ]; then
518         WITH_VRF=no
519         setup
520         do_test "new"
521
522         cleanup
523         log_section "Routing with nexthop objects and VRF"
524         WITH_VRF=yes
525         setup
526         do_test "new"
527 else
528         echo "Nexthop objects not supported; skipping tests"
529 fi
530
531 printf "\nTests passed: %3d\n" ${nsuccess}
532 printf "Tests failed: %3d\n"   ${nfail}
533
534 exit $ret