]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
iptables-save: Avoid /etc/protocols lookups
authorPhil Sutter <phil@nwl.cc>
Wed, 10 Jan 2024 14:26:59 +0000 (15:26 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 6 Feb 2024 23:25:03 +0000 (00:25 +0100)
Instrument proto_to_name() to abort if given protocol number is not
among the well-known ones in xtables_chain_protos. Along with
xtables_parse_protocol() preferring said array for lookups as well, this
ensures reliable dump'n'restore regardless of /etc/protocols contents.

Another benefit is rule dump performance. A simple test-case dumping
100k rules matching on dccp protocol shows an 8s delta (2s vs. 10s for
legacy, 0.5s vs. 8s for nft) with this patch applied. For reference:

| for variant in nft legacy; do
|  (
|  echo "*filter"
|  for ((i = 0; i < 100000; i++)); do
|          echo "-A FORWARD -p dccp -j ACCEPT"
|  done
|  echo "COMMIT"
|  ) | iptables-${variant}-restore
|  time iptables-${variant}-save | wc -l
|  iptables-${variant} -F
| done

Signed-off-by: Phil Sutter <phil@nwl.cc>
iptables/tests/shell/testcases/ipt-save/0001load-dumps_0
iptables/xshared.c

index 4e0be51c071c80056379a9562945483371043482..48f5f7b42337909eabfec509a111b09cf3f223ee 100755 (executable)
@@ -39,6 +39,7 @@ do_simple()
 
        $XT_MULTI ${iptables}-restore < "$dumpfile"
        $XT_MULTI ${iptables}-save | grep -v "^#" > "$tmpfile"
+       sed -i -e 's/-p 47 /-p gre /' "$tmpfile"
        do_diff $dumpfile "$tmpfile"
        if [ $? -ne 0 ]; then
                # cp "$tmpfile" "$dumpfile.got"
index 75ab2a63a996fe0e4b1747b5e235ae15177e78ef..bff7d60ce13908f23372d52b947df857a757c65d 100644 (file)
@@ -1103,7 +1103,7 @@ void save_rule_details(const char *iniface, const char *outiface,
        }
 
        if (proto > 0) {
-               const char *pname = proto_to_name(proto, 0);
+               const char *pname = proto_to_name(proto, true);
 
                if (invflags & XT_INV_PROTO)
                        printf(" !");