]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
xshared: Share save_rule_details() with legacy
authorPhil Sutter <phil@nwl.cc>
Fri, 5 Nov 2021 17:27:53 +0000 (18:27 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 23 Nov 2021 14:01:23 +0000 (15:01 +0100)
The function combines printing of input and output interfaces and
protocol parameter, all being IP family independent. Extend the function
to print fragment option ('-f'), too if requested. While being at it,
drop unused iptables_command_state parameter and reorder the remaining
ones a bit.

Signed-off-by: Phil Sutter <phil@nwl.cc>
iptables/ip6tables.c
iptables/iptables.c
iptables/nft-ipv4.c
iptables/nft-ipv6.c
iptables/nft-shared.c
iptables/nft-shared.h
iptables/xshared.c
iptables/xshared.h

index 1c9b076196e8f0aaf9dc1a5dfec9177703c010e0..eacbf704f97698b9d2fe082a5f7d100a24877511 100644 (file)
@@ -707,19 +707,6 @@ list_entries(const xt_chainlabel chain, int rulenum, int verbose, int numeric,
        return found;
 }
 
-static void print_proto(uint16_t proto, int invert)
-{
-       if (proto) {
-               const char *pname = proto_to_name(proto, 0);
-               const char *invertstr = invert ? " !" : "";
-
-               if (pname)
-                       printf("%s -p %s", invertstr, pname);
-               else
-                       printf("%s -p %u", invertstr, proto);
-       }
-}
-
 static int print_match_save(const struct xt_entry_match *e,
                        const struct ip6t_ip6 *ip)
 {
@@ -795,13 +782,9 @@ void print_rule6(const struct ip6t_entry *e,
        print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
                        e->ipv6.invflags & IP6T_INV_DSTIP);
 
-       save_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
-                   e->ipv6.invflags & IP6T_INV_VIA_IN);
-
-       save_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
-                   e->ipv6.invflags & IP6T_INV_VIA_OUT);
-
-       print_proto(e->ipv6.proto, e->ipv6.invflags & XT_INV_PROTO);
+       save_rule_details(e->ipv6.iniface, e->ipv6.iniface_mask,
+                         e->ipv6.outiface, e->ipv6.outiface_mask,
+                         e->ipv6.proto, 0, e->ipv6.invflags);
 
 #if 0
        /* not definied in ipv6
index 7802bd6d95bd0a479b426102a9b80e7117cc3340..85fb7bdcd0ca1702848b07c6887d73853884b85e 100644 (file)
@@ -699,19 +699,6 @@ list_entries(const xt_chainlabel chain, int rulenum, int verbose, int numeric,
        return found;
 }
 
-static void print_proto(uint16_t proto, int invert)
-{
-       if (proto) {
-               const char *pname = proto_to_name(proto, 0);
-               const char *invertstr = invert ? " !" : "";
-
-               if (pname)
-                       printf("%s -p %s", invertstr, pname);
-               else
-                       printf("%s -p %u", invertstr, proto);
-       }
-}
-
 #define IP_PARTS_NATIVE(n)                     \
 (unsigned int)((n)>>24)&0xFF,                  \
 (unsigned int)((n)>>16)&0xFF,                  \
@@ -804,17 +791,10 @@ void print_rule4(const struct ipt_entry *e,
        print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
                        e->ip.invflags & IPT_INV_DSTIP);
 
-       save_iface('i', e->ip.iniface, e->ip.iniface_mask,
-                   e->ip.invflags & IPT_INV_VIA_IN);
-
-       save_iface('o', e->ip.outiface, e->ip.outiface_mask,
-                   e->ip.invflags & IPT_INV_VIA_OUT);
-
-       print_proto(e->ip.proto, e->ip.invflags & XT_INV_PROTO);
-
-       if (e->ip.flags & IPT_F_FRAG)
-               printf("%s -f",
-                      e->ip.invflags & IPT_INV_FRAG ? " !" : "");
+       save_rule_details(e->ip.iniface, e->ip.iniface_mask,
+                         e->ip.outiface, e->ip.outiface_mask,
+                         e->ip.proto, e->ip.flags & IPT_F_FRAG,
+                         e->ip.invflags);
 
        /* Print matchinfo part */
        if (e->target_offset)
index 287112d0e6b993f8d22190d0bf4b17a91690a29e..39d6e61232cdbbc484719845045d5ced13c6d31e 100644 (file)
@@ -317,15 +317,10 @@ static void nft_ipv4_save_rule(const void *data, unsigned int format)
        save_ipv4_addr('d', &cs->fw.ip.dst, cs->fw.ip.dmsk.s_addr,
                       cs->fw.ip.invflags & IPT_INV_DSTIP);
 
-       save_rule_details(cs, cs->fw.ip.invflags, cs->fw.ip.proto,
-                         cs->fw.ip.iniface, cs->fw.ip.iniface_mask,
-                         cs->fw.ip.outiface, cs->fw.ip.outiface_mask);
-
-       if (cs->fw.ip.flags & IPT_F_FRAG) {
-               if (cs->fw.ip.invflags & IPT_INV_FRAG)
-                       printf(" !");
-               printf(" -f");
-       }
+       save_rule_details(cs->fw.ip.iniface, cs->fw.ip.iniface_mask,
+                         cs->fw.ip.outiface, cs->fw.ip.outiface_mask,
+                         cs->fw.ip.proto, cs->fw.ip.flags & IPT_F_FRAG,
+                         cs->fw.ip.invflags);
 
        save_matches_and_target(cs, cs->fw.ip.flags & IPT_F_GOTO,
                                &cs->fw, format);
index 845937b180b068c8921f118a11f55e28f09f7ef5..0c73cedd71c96ca3d6f0c1606b104deec246a5c5 100644 (file)
@@ -253,9 +253,9 @@ static void nft_ipv6_save_rule(const void *data, unsigned int format)
        save_ipv6_addr('d', &cs->fw6.ipv6.dst, &cs->fw6.ipv6.dmsk,
                       cs->fw6.ipv6.invflags & IP6T_INV_DSTIP);
 
-       save_rule_details(cs, cs->fw6.ipv6.invflags, cs->fw6.ipv6.proto,
-                         cs->fw6.ipv6.iniface, cs->fw6.ipv6.iniface_mask,
-                         cs->fw6.ipv6.outiface, cs->fw6.ipv6.outiface_mask);
+       save_rule_details(cs->fw6.ipv6.iniface, cs->fw6.ipv6.iniface_mask,
+                         cs->fw6.ipv6.outiface, cs->fw6.ipv6.outiface_mask,
+                         cs->fw6.ipv6.proto, 0, cs->fw6.ipv6.invflags);
 
        save_matches_and_target(cs, cs->fw6.ipv6.flags & IP6T_F_GOTO,
                                &cs->fw6, format);
index b86cc086bed1c75aae26bda78ed7db07ee69ad3a..168c224627fd0c6a78a681bffe728247c4c19b49 100644 (file)
@@ -785,35 +785,6 @@ void print_rule_details(const struct iptables_command_state *cs,
        }
 }
 
-void save_rule_details(const struct iptables_command_state *cs,
-                      uint8_t invflags, uint16_t proto,
-                      const char *iniface,
-                      unsigned const char *iniface_mask,
-                      const char *outiface,
-                      unsigned const char *outiface_mask)
-{
-       if (iniface != NULL) {
-               save_iface('i', iniface, iniface_mask,
-                           invflags & IPT_INV_VIA_IN);
-       }
-       if (outiface != NULL) {
-               save_iface('o', outiface, outiface_mask,
-                           invflags & IPT_INV_VIA_OUT);
-       }
-
-       if (proto > 0) {
-               const char *pname = proto_to_name(proto, 0);
-
-               if (invflags & XT_INV_PROTO)
-                       printf(" !");
-
-               if (pname)
-                       printf(" -p %s", pname);
-               else
-                       printf(" -p %u", proto);
-       }
-}
-
 void nft_ipv46_save_chain(const struct nftnl_chain *c, const char *policy)
 {
        const char *chain = nftnl_chain_get_str(c, NFTNL_CHAIN_NAME);
index 339c46e7f5b06ab2b68ffe4e628c4d2bb7ce6544..cac5757ff0708e8fd775102c791029b211db24a3 100644 (file)
@@ -173,12 +173,6 @@ void print_rule_details(const struct iptables_command_state *cs,
                        unsigned int num, unsigned int format);
 void print_matches_and_target(struct iptables_command_state *cs,
                              unsigned int format);
-void save_rule_details(const struct iptables_command_state *cs,
-                      uint8_t invflags, uint16_t proto,
-                      const char *iniface,
-                      unsigned const char *iniface_mask,
-                      const char *outiface,
-                      unsigned const char *outiface_mask);
 void nft_ipv46_save_chain(const struct nftnl_chain *c, const char *policy);
 void save_matches_and_target(const struct iptables_command_state *cs,
                             bool goto_flag, const void *fw,
index db03aaaa324b0dabd2ef16296dd2c836ae7f9544..db701ead4811fdba7b36da00d53ca9e488f86250 100644 (file)
@@ -941,3 +941,35 @@ void parse_chain(const char *chainname)
                        xtables_error(PARAMETER_PROBLEM,
                                      "Invalid chain name `%s'", chainname);
 }
+
+void save_rule_details(const char *iniface, unsigned const char *iniface_mask,
+                      const char *outiface, unsigned const char *outiface_mask,
+                      uint16_t proto, int frag, uint8_t invflags)
+{
+       if (iniface != NULL) {
+               save_iface('i', iniface, iniface_mask,
+                           invflags & IPT_INV_VIA_IN);
+       }
+       if (outiface != NULL) {
+               save_iface('o', outiface, outiface_mask,
+                           invflags & IPT_INV_VIA_OUT);
+       }
+
+       if (proto > 0) {
+               const char *pname = proto_to_name(proto, 0);
+
+               if (invflags & XT_INV_PROTO)
+                       printf(" !");
+
+               if (pname)
+                       printf(" -p %s", pname);
+               else
+                       printf(" -p %u", proto);
+       }
+
+       if (frag) {
+               if (invflags & IPT_INV_FRAG)
+                       printf(" !");
+               printf(" -f");
+       }
+}
index 3281ce584476c41f572508b8f33527b164ab93f2..484ade126404c59997cd5a5ef6ba3135c71870d5 100644 (file)
@@ -242,4 +242,8 @@ void parse_chain(const char *chainname);
 void generic_opt_check(int command, int options);
 char opt2char(int option);
 
+void save_rule_details(const char *iniface, unsigned const char *iniface_mask,
+                      const char *outiface, unsigned const char *outiface_mask,
+                      uint16_t proto, int frag, uint8_t invflags);
+
 #endif /* IPTABLES_XSHARED_H */