Skip to content

Commit 2ef740d

Browse files
Florian Westphalummakynes
authored andcommitted
selftests: netfilter: add meta iif/oif match test
simple test case, but would have caught this: FAIL: iifgroupcount, want "packets 2", got table inet filter { counter iifgroupcount { packets 0 bytes 0 } } Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 78470d9 commit 2ef740d

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

tools/testing/selftests/netfilter/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
TEST_PROGS := nft_trans_stress.sh nft_nat.sh bridge_brouter.sh \
55
conntrack_icmp_related.sh nft_flowtable.sh ipvs.sh \
66
nft_concat_range.sh nft_conntrack_helper.sh \
7-
nft_queue.sh
7+
nft_queue.sh nft_meta.sh
88

99
LDLIBS = -lmnl
1010
TEST_GEN_FILES = nf-queue
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/bin/bash
2+
3+
# check iif/iifname/oifgroup/iiftype match.
4+
5+
# Kselftest framework requirement - SKIP code is 4.
6+
ksft_skip=4
7+
sfx=$(mktemp -u "XXXXXXXX")
8+
ns0="ns0-$sfx"
9+
10+
nft --version > /dev/null 2>&1
11+
if [ $? -ne 0 ];then
12+
echo "SKIP: Could not run test without nft tool"
13+
exit $ksft_skip
14+
fi
15+
16+
cleanup()
17+
{
18+
ip netns del "$ns0"
19+
}
20+
21+
ip netns add "$ns0"
22+
ip -net "$ns0" link set lo up
23+
ip -net "$ns0" addr add 127.0.0.1 dev lo
24+
25+
trap cleanup EXIT
26+
27+
ip netns exec "$ns0" nft -f /dev/stdin <<EOF
28+
table inet filter {
29+
counter iifcount {}
30+
counter iifnamecount {}
31+
counter iifgroupcount {}
32+
counter iiftypecount {}
33+
counter infproto4count {}
34+
counter il4protocounter {}
35+
counter imarkcounter {}
36+
37+
counter oifcount {}
38+
counter oifnamecount {}
39+
counter oifgroupcount {}
40+
counter oiftypecount {}
41+
counter onfproto4count {}
42+
counter ol4protocounter {}
43+
counter oskuidcounter {}
44+
counter oskgidcounter {}
45+
counter omarkcounter {}
46+
47+
chain input {
48+
type filter hook input priority 0; policy accept;
49+
50+
meta iif lo counter name "iifcount"
51+
meta iifname "lo" counter name "iifnamecount"
52+
meta iifgroup "default" counter name "iifgroupcount"
53+
meta iiftype "loopback" counter name "iiftypecount"
54+
meta nfproto ipv4 counter name "infproto4count"
55+
meta l4proto icmp counter name "il4protocounter"
56+
meta mark 42 counter name "imarkcounter"
57+
}
58+
59+
chain output {
60+
type filter hook output priority 0; policy accept;
61+
meta oif lo counter name "oifcount" counter
62+
meta oifname "lo" counter name "oifnamecount"
63+
meta oifgroup "default" counter name "oifgroupcount"
64+
meta oiftype "loopback" counter name "oiftypecount"
65+
meta nfproto ipv4 counter name "onfproto4count"
66+
meta l4proto icmp counter name "ol4protocounter"
67+
meta skuid 0 counter name "oskuidcounter"
68+
meta skgid 0 counter name "oskgidcounter"
69+
meta mark 42 counter name "omarkcounter"
70+
}
71+
}
72+
EOF
73+
74+
if [ $? -ne 0 ]; then
75+
echo "SKIP: Could not add test ruleset"
76+
exit $ksft_skip
77+
fi
78+
79+
ret=0
80+
81+
check_one_counter()
82+
{
83+
local cname="$1"
84+
local want="packets $2"
85+
local verbose="$3"
86+
87+
cnt=$(ip netns exec "$ns0" nft list counter inet filter $cname | grep -q "$want")
88+
if [ $? -ne 0 ];then
89+
echo "FAIL: $cname, want \"$want\", got"
90+
ret=1
91+
ip netns exec "$ns0" nft list counter inet filter $counter
92+
fi
93+
}
94+
95+
check_lo_counters()
96+
{
97+
local want="$1"
98+
local verbose="$2"
99+
local counter
100+
101+
for counter in iifcount iifnamecount iifgroupcount iiftypecount infproto4count \
102+
oifcount oifnamecount oifgroupcount oiftypecount onfproto4count \
103+
il4protocounter \
104+
ol4protocounter \
105+
; do
106+
check_one_counter "$counter" "$want" "$verbose"
107+
done
108+
}
109+
110+
check_lo_counters "0" false
111+
ip netns exec "$ns0" ping -q -c 1 127.0.0.1 -m 42 > /dev/null
112+
113+
check_lo_counters "2" true
114+
115+
check_one_counter oskuidcounter "1" true
116+
check_one_counter oskgidcounter "1" true
117+
check_one_counter imarkcounter "1" true
118+
check_one_counter omarkcounter "1" true
119+
120+
if [ $ret -eq 0 ];then
121+
echo "OK: nftables meta iif/oif counters at expected values"
122+
fi
123+
124+
exit $ret

0 commit comments

Comments
 (0)