| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# test using many netns |
| 4 |
|
| 5 |
# arbitry value of 'many' |
| 6 |
HOWMANY=20 |
| 7 |
|
| 8 |
IP=$(which ip) |
| 9 |
if [ ! -x "$IP" ] ; then |
| 10 |
echo "E: no ip binary" >&2 |
| 11 |
exit 1 |
| 12 |
fi |
| 13 |
|
| 14 |
RULESET="table ip t { |
| 15 |
set s { |
| 16 |
type ipv4_addr |
| 17 |
elements = { 1.1.0.0 } |
| 18 |
} |
| 19 |
|
| 20 |
chain c { |
| 21 |
ct state new |
| 22 |
udp dport { 12345, 54321 } |
| 23 |
ip saddr @s drop |
| 24 |
jump other |
| 25 |
} |
| 26 |
|
| 27 |
chain other { |
| 28 |
} |
| 29 |
} |
| 30 |
table ip6 t { |
| 31 |
set s { |
| 32 |
type ipv6_addr |
| 33 |
elements = { fe00::1 } |
| 34 |
} |
| 35 |
|
| 36 |
chain c { |
| 37 |
ct state new |
| 38 |
udp dport { 12345, 54321 } |
| 39 |
ip6 saddr @s drop |
| 40 |
jump other |
| 41 |
} |
| 42 |
|
| 43 |
chain other { |
| 44 |
} |
| 45 |
} |
| 46 |
table inet t { |
| 47 |
set s { |
| 48 |
type ipv6_addr |
| 49 |
elements = { fe00::1 } |
| 50 |
} |
| 51 |
|
| 52 |
chain c { |
| 53 |
ct state new |
| 54 |
udp dport { 12345, 54321 } |
| 55 |
ip6 saddr @s drop |
| 56 |
jump other |
| 57 |
} |
| 58 |
|
| 59 |
chain other { |
| 60 |
} |
| 61 |
} |
| 62 |
table bridge t { |
| 63 |
chain c { |
| 64 |
jump other |
| 65 |
} |
| 66 |
|
| 67 |
chain other { |
| 68 |
accept |
| 69 |
} |
| 70 |
} |
| 71 |
table arp t { |
| 72 |
chain c { |
| 73 |
jump other |
| 74 |
} |
| 75 |
|
| 76 |
chain other { |
| 77 |
accept |
| 78 |
} |
| 79 |
}" |
| 80 |
|
| 81 |
function test_netns() |
| 82 |
{ |
| 83 |
local NETNS_NAME=$1 |
| 84 |
$IP netns add $NETNS_NAME |
| 85 |
if [ $? -ne 0 ] ; then |
| 86 |
echo "E: unable to create netns" >&2 |
| 87 |
exit 1 |
| 88 |
fi |
| 89 |
|
| 90 |
$IP netns exec $NETNS_NAME $NFT -f - <<< "$RULESET" |
| 91 |
if [ $? -ne 0 ] ; then |
| 92 |
echo "E: unable to load ruleset in netns" >&2 |
| 93 |
$IP netns del $NETNS_NAME |
| 94 |
exit 1 |
| 95 |
fi |
| 96 |
|
| 97 |
KERNEL_RULESET="$($IP netns exec $NETNS_NAME $NFT list ruleset)" |
| 98 |
if [ "$RULESET" != "$KERNEL_RULESET" ] ; then |
| 99 |
echo "E: ruleset in netns $NETNS_NAME differs from the loaded" >&2 |
| 100 |
$DIFF -u <(echo "$RULESET") <(echo "$KERNEL_RULESET") |
| 101 |
$IP netns del $NETNS_NAME |
| 102 |
exit 1 |
| 103 |
fi |
| 104 |
|
| 105 |
$IP netns del $NETNS_NAME |
| 106 |
} |
| 107 |
|
| 108 |
for i in $(seq 1 $HOWMANY) ; do |
| 109 |
NETNS_NAME="$netns${i}_$(basename "$0")" |
| 110 |
test_netns $NETNS_NAME |
| 111 |
done |
| 112 |
|
| 113 |
exit 0 |