|
| 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