xref: /linux/tools/testing/selftests/net/cmsg_ipv6.sh (revision 7255fcc80d4b525cc10cfaaf7f485830d4ed2000)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4source lib.sh
5
6IP6=2001:db8:1::1/64
7TGT6=2001:db8:1::2
8TMPF=$(mktemp --suffix ".pcap")
9
10cleanup()
11{
12    rm -f $TMPF
13    cleanup_ns $NS
14}
15
16trap cleanup EXIT
17
18tcpdump -h | grep immediate-mode >> /dev/null
19if [ $? -ne 0 ]; then
20    echo "SKIP - tcpdump with --immediate-mode option required"
21    exit $ksft_skip
22fi
23
24# Namespaces
25setup_ns NS
26NSEXE="ip netns exec $NS"
27
28$NSEXE sysctl -w net.ipv4.ping_group_range='0 2147483647' > /dev/null
29
30# Connectivity
31ip -netns $NS link add type dummy
32ip -netns $NS link set dev dummy0 up
33ip -netns $NS addr add $IP6 dev dummy0
34
35# Test
36BAD=0
37TOTAL=0
38
39check_result() {
40    ((TOTAL++))
41    if [ $1 -ne $2 ]; then
42	echo "  Case $3 returned $1, expected $2"
43	((BAD++))
44    fi
45}
46
47# IPV6_DONTFRAG
48for ovr in setsock cmsg both diff; do
49    for df in 0 1; do
50	for p in u i r; do
51	    [ $p == "u" ] && prot=UDP
52	    [ $p == "i" ] && prot=ICMP
53	    [ $p == "r" ] && prot=RAW
54
55	    [ $ovr == "setsock" ] && m="-F $df"
56	    [ $ovr == "cmsg" ]    && m="-f $df"
57	    [ $ovr == "both" ]    && m="-F $df -f $df"
58	    [ $ovr == "diff" ]    && m="-F $((1 - df)) -f $df"
59
60	    $NSEXE ./cmsg_sender -s -S 2000 -6 -p $p $m $TGT6 1234
61	    check_result $? $df "DONTFRAG $prot $ovr"
62	done
63    done
64done
65
66# IPV6_TCLASS
67TOS=0x10
68TOS2=0x20
69
70ip -6 -netns $NS rule add tos $TOS lookup 300
71ip -6 -netns $NS route add table 300 prohibit any
72
73for ovr in setsock cmsg both diff; do
74    for p in u i r; do
75	[ $p == "u" ] && prot=UDP
76	[ $p == "i" ] && prot=ICMP
77	[ $p == "r" ] && prot=RAW
78
79	[ $ovr == "setsock" ] && m="-C"
80	[ $ovr == "cmsg" ]    && m="-c"
81	[ $ovr == "both" ]    && m="-C $((TOS2)) -c"
82	[ $ovr == "diff" ]    && m="-C $((TOS )) -c"
83
84	$NSEXE nohup tcpdump --immediate-mode -p -ni dummy0 -w $TMPF -c 4 2> /dev/null &
85	BG=$!
86	sleep 0.05
87
88	$NSEXE ./cmsg_sender -6 -p $p $m $((TOS2)) $TGT6 1234
89	check_result $? 0 "TCLASS $prot $ovr - pass"
90
91	while [ -d /proc/$BG ]; do
92	    $NSEXE ./cmsg_sender -6 -p $p $m $((TOS2)) $TGT6 1234
93	done
94
95	tcpdump -r $TMPF -v 2>&1 | grep "class $TOS2" >> /dev/null
96	check_result $? 0 "TCLASS $prot $ovr - packet data"
97	rm $TMPF
98
99	[ $ovr == "both" ]    && m="-C $((TOS )) -c"
100	[ $ovr == "diff" ]    && m="-C $((TOS2)) -c"
101
102	$NSEXE ./cmsg_sender -6 -p $p $m $((TOS)) -s $TGT6 1234
103	check_result $? 1 "TCLASS $prot $ovr - rejection"
104    done
105done
106
107# IPV6_HOPLIMIT
108LIM=4
109
110for ovr in setsock cmsg both diff; do
111    for p in u i r; do
112	[ $p == "u" ] && prot=UDP
113	[ $p == "i" ] && prot=ICMP
114	[ $p == "r" ] && prot=RAW
115
116	[ $ovr == "setsock" ] && m="-L"
117	[ $ovr == "cmsg" ]    && m="-l"
118	[ $ovr == "both" ]    && m="-L $LIM -l"
119	[ $ovr == "diff" ]    && m="-L $((LIM + 1)) -l"
120
121	$NSEXE nohup tcpdump --immediate-mode -p -ni dummy0 -w $TMPF -c 4 2> /dev/null &
122	BG=$!
123	sleep 0.05
124
125	$NSEXE ./cmsg_sender -6 -p $p $m $LIM $TGT6 1234
126	check_result $? 0 "HOPLIMIT $prot $ovr - pass"
127
128	while [ -d /proc/$BG ]; do
129	    $NSEXE ./cmsg_sender -6 -p $p $m $LIM $TGT6 1234
130	done
131
132	tcpdump -r $TMPF -v 2>&1 | grep "hlim $LIM[^0-9]" >> /dev/null
133	check_result $? 0 "HOPLIMIT $prot $ovr - packet data"
134	rm $TMPF
135    done
136done
137
138# IPV6 exthdr
139for p in u i r; do
140    # Very basic "does it crash" test
141    for h in h d r; do
142	$NSEXE ./cmsg_sender -p $p -6 -H $h $TGT6 1234
143	check_result $? 0 "ExtHdr $prot $ovr - pass"
144    done
145done
146
147# Summary
148if [ $BAD -ne 0 ]; then
149    echo "FAIL - $BAD/$TOTAL cases failed"
150    exit 1
151else
152    echo "OK"
153    exit 0
154fi
155