summaryrefslogtreecommitdiffstats
path: root/tests/shell/testcases/netns/0001nft-f_0
blob: e616363e358cfdc9c8203603a0f6955c818284bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash

# test a kernel netns loading a simple ruleset

IP=$(which ip)
if [ ! -x "$IP" ] ; then
	echo "E: no ip binary" >&2
	exit 1
fi

tmpfile=$(mktemp)
if [ ! -w $tmpfile ] ; then
	echo "Failed to create tmp file" >&2
	exit 0
fi

trap "rm -rf $tmpfile" EXIT # cleanup if aborted

RULESET="table ip t {
	set s {
		type ipv4_addr
		elements = { 1.1.0.0}
	}

	chain c {
		ct state new
		udp dport { 12345}
		ip saddr @s drop
		jump other
	}

	chain other {
	}
}
table ip6 t {
	set s {
		type ipv6_addr
		elements = { fe00::1}
	}

	chain c {
		ct state new
		udp dport { 12345}
		ip6 saddr @s drop
		jump other
	}

	chain other {
	}
}
table inet t {
	set s {
		type ipv6_addr
		elements = { fe00::1}
	}

	chain c {
		ct state new
		udp dport { 12345}
		ip6 saddr @s drop
		jump other
	}

	chain other {
	}
}
table bridge t {
	chain c {
		jump other
	}

	chain other {
		accept
	}
}
table arp t {
	chain c {
		jump other
	}

	chain other {
		accept
	}
}"

# netns
NETNS_NAME=$(basename "$0")
$IP netns add $NETNS_NAME
if [ $? -ne 0 ] ; then
	echo "E: unable to create netns" >&2
	exit 1
fi

echo "$RULESET" > $tmpfile
$IP netns exec $NETNS_NAME $NFT -f $tmpfile
if [ $? -ne 0 ] ; then
	echo "E: unable to load ruleset in netns" >&2
	$IP netns del $NETNS_NAME
	exit 1
fi

KERNEL_RULESET="$($IP netns exec $NETNS_NAME $NFT list ruleset)"
$IP netns del $NETNS_NAME
if [ "$RULESET" != "$KERNEL_RULESET" ] ; then
        DIFF="$(which diff)"
        [ -x $DIFF ] && $DIFF -u <(echo "$RULESET") <(echo "$KERNEL_RULESET")
        exit 1
fi
exit 0