1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4NS=ns
5IP4=172.16.0.1/24
6TGT4=172.16.0.2
7IP6=2001:db8:1::1/64
8TGT6=2001:db8:1::2
9MARK=1000
10
11cleanup()
12{
13    ip netns del $NS
14}
15
16trap cleanup EXIT
17
18# Namespaces
19ip netns add $NS
20
21ip netns exec $NS sysctl -w net.ipv4.ping_group_range='0 2147483647' > /dev/null
22
23# Connectivity
24ip -netns $NS link add type dummy
25ip -netns $NS link set dev dummy0 up
26ip -netns $NS addr add $IP4 dev dummy0
27ip -netns $NS addr add $IP6 dev dummy0
28
29ip -netns $NS rule add fwmark $MARK lookup 300
30ip -6 -netns $NS rule add fwmark $MARK lookup 300
31ip -netns $NS route add prohibit any table 300
32ip -6 -netns $NS route add prohibit any table 300
33
34# Test
35BAD=0
36TOTAL=0
37
38check_result() {
39    ((TOTAL++))
40    if [ $1 -ne $2 ]; then
41	echo "  Case $3 returned $1, expected $2"
42	((BAD++))
43    fi
44}
45
46for ovr in setsock cmsg both; do
47    for i in 4 6; do
48	[ $i == 4 ] && TGT=$TGT4 || TGT=$TGT6
49
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="-M"
56	    [ $ovr == "cmsg" ]    && m="-m"
57	    [ $ovr == "both" ]    && m="-M $MARK -m"
58
59	    ip netns exec $NS ./cmsg_sender -$i -p $p $m $((MARK + 1)) $TGT 1234
60	    check_result $? 0 "$prot $ovr - pass"
61
62	    [ $ovr == "diff" ] && m="-M $((MARK + 1)) -m"
63
64	    ip netns exec $NS ./cmsg_sender -$i -p $p $m $MARK -s $TGT 1234
65	    check_result $? 1 "$prot $ovr - rejection"
66	done
67    done
68done
69
70# Summary
71if [ $BAD -ne 0 ]; then
72    echo "FAIL - $BAD/$TOTAL cases failed"
73    exit 1
74else
75    echo "OK"
76    exit 0
77fi
78