xref: /linux/tools/testing/selftests/net/cmsg_so_mark.sh (revision 249ebf3f65f8530beb2cbfb91bff1d83ba88d23c)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4source lib.sh
5
6IP4=172.16.0.1/24
7TGT4=172.16.0.2
8IP6=2001:db8:1::1/64
9TGT6=2001:db8:1::2
10MARK=1000
11
12cleanup()
13{
14    cleanup_ns $NS
15}
16
17trap cleanup EXIT
18
19# Namespaces
20setup_ns NS
21
22ip netns exec $NS sysctl -w net.ipv4.ping_group_range='0 2147483647' > /dev/null
23
24# Connectivity
25ip -netns $NS link add type dummy
26ip -netns $NS link set dev dummy0 up
27ip -netns $NS addr add $IP4 dev dummy0
28ip -netns $NS addr add $IP6 dev dummy0
29
30ip -netns $NS rule add fwmark $MARK lookup 300
31ip -6 -netns $NS rule add fwmark $MARK lookup 300
32ip -netns $NS route add prohibit any table 300
33ip -6 -netns $NS route add prohibit any table 300
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
47for ovr in setsock cmsg both; do
48    for i in 4 6; do
49	[ $i == 4 ] && TGT=$TGT4 || TGT=$TGT6
50
51	for p in u i r; do
52	    [ $p == "u" ] && prot=UDP
53	    [ $p == "i" ] && prot=ICMP
54	    [ $p == "r" ] && prot=RAW
55
56	    [ $ovr == "setsock" ] && m="-M"
57	    [ $ovr == "cmsg" ]    && m="-m"
58	    [ $ovr == "both" ]    && m="-M $MARK -m"
59
60	    ip netns exec $NS ./cmsg_sender -$i -p $p $m $((MARK + 1)) $TGT 1234
61	    check_result $? 0 "$prot $ovr - pass"
62
63	    [ $ovr == "diff" ] && m="-M $((MARK + 1)) -m"
64
65	    ip netns exec $NS ./cmsg_sender -$i -p $p $m $MARK -s $TGT 1234
66	    check_result $? 1 "$prot $ovr - rejection"
67	done
68    done
69done
70
71# Summary
72if [ $BAD -ne 0 ]; then
73    echo "FAIL - $BAD/$TOTAL cases failed"
74    exit 1
75else
76    echo "OK"
77    exit 0
78fi
79