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
21# Connectivity
22ip -netns $NS link add type dummy
23ip -netns $NS link set dev dummy0 up
24ip -netns $NS addr add $IP4 dev dummy0
25ip -netns $NS addr add $IP6 dev dummy0
26
27ip -netns $NS rule add fwmark $MARK lookup 300
28ip -6 -netns $NS rule add fwmark $MARK lookup 300
29ip -netns $NS route add prohibit any table 300
30ip -6 -netns $NS route add prohibit any table 300
31
32# Test
33BAD=0
34TOTAL=0
35
36check_result() {
37    ((TOTAL++))
38    if [ $1 -ne $2 ]; then
39	echo "  Case $3 returned $1, expected $2"
40	((BAD++))
41    fi
42}
43
44ip netns exec $NS ./cmsg_so_mark $TGT4 1234 $((MARK + 1))
45check_result $? 0 "IPv4 pass"
46ip netns exec $NS ./cmsg_so_mark $TGT6 1234 $((MARK + 1))
47check_result $? 0 "IPv6 pass"
48
49ip netns exec $NS ./cmsg_so_mark $TGT4 1234 $MARK
50check_result $? 1 "IPv4 rejection"
51ip netns exec $NS ./cmsg_so_mark $TGT6 1234 $MARK
52check_result $? 1 "IPv6 rejection"
53
54# Summary
55if [ $BAD -ne 0 ]; then
56    echo "FAIL - $BAD/$TOTAL cases failed"
57    exit 1
58else
59    echo "OK"
60    exit 0
61fi
62