Skip to contentSkip to conversation

Restore libxtables ABI compatibility

PATCH New Phil Sutter 1 participant 6 messages May 6, 2022 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Restore libxtables ABI compatibility

First three patches eliminate the 'print_help' callback added to struct xtables_globals by making xtables_printhelp() do the right thing for arptables as well. Due to the shared parser there are only a few distinct options left anyway. While doing so, help output is aligned with legacy arptables which prints all extensions' help texts even if not requested for.

Patch four changes how the different optstrings are passed to the shared parser. Via central storage in xshared.h and a helper function which maps from afinfo->family value, struct xtables_global's 'optstring' field may be dropped again.

The last patch just undoes the "fix" to struct xtables_pprot. It is not a problem which may occur during run-time and the compiler complains if code changes would trigger the bug.

Phil Sutter (5):
extensions: MARK: Drop extra newline at end of help
xshared: Move arp_opcodes into shared space
xshared: Extend xtables_printhelp() for arptables
libxtables: Drop xtables_globals 'optstring' field
libxtables: Revert change to struct xtables_pprot

 extensions/libarpt_mangle.c |   1 -
 extensions/libxt_MARK.c     |   3 +-
 include/xtables.h           |   4 +-
 iptables/ip6tables.c        |   2 -
 iptables/iptables.c         |   2 -
 iptables/nft-arp.c          |  22 ++------
 iptables/nft-arp.h          |   7 ---
 iptables/xshared.c          |  84 ++++++++++++++++++++++++----
 iptables/xshared.h          |   7 ++-
 iptables/xtables-arp.c      | 106 ------------------------------------
 iptables/xtables-eb.c       |   3 +-
 iptables/xtables-monitor.c  |   1 -
 iptables/xtables.c          |   3 -
 13 files changed, 86 insertions(+), 159 deletions(-)
 delete mode 100644 iptables/nft-arp.h
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH 1/5 extensions: MARK: Drop extra newline at end of help

Fixes f4b737fb0c52a ("libxt_MARK r2")
---
 extensions/libxt_MARK.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
extensions/libxt_MARK.c +1 -2
index b765af6c35304..1536563d0f4c7 100644
@@ -77,8 +77,7 @@ static void mark_tg_help(void)
 "  --set-mark value[/mask]   Clear bits in mask and OR value into nfmark\n"
 "  --and-mark bits           Binary AND the nfmark with bits\n"
 "  --or-mark bits            Binary OR the nfmark with bits\n"
-"  --xor-mark bits           Binary XOR the nfmark with bits\n"
-"\n");
+"  --xor-mark bits           Binary XOR the nfmark with bits\n");
 }
 
 static void MARK_parse_v0(struct xt_option_call *cb)
Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH 2/5 xshared: Move arp_opcodes into shared space

In reply to Phil Sutter

It will be referenced by xtables_printhelp() if printing for arptables and therefore must be present in legacy as well even if unused.

---
 extensions/libarpt_mangle.c |  1 -
 iptables/nft-arp.c          | 22 ++++------------------
 iptables/nft-arp.h          |  7 -------
 iptables/xshared.c          | 14 ++++++++++++++
 iptables/xshared.h          |  3 +++
 iptables/xtables-arp.c      |  1 -
 iptables/xtables-monitor.c  |  1 -
 iptables/xtables.c          |  1 -
 8 files changed, 21 insertions(+), 29 deletions(-)
 delete mode 100644 iptables/nft-arp.h
extensions/libarpt_mangle.c +0 -1
index a2378a8ba6ccb..765edf34781f3 100644
@@ -13,7 +13,6 @@
 #include <xtables.h>
 #include <linux/netfilter_arp/arpt_mangle.h>
 #include "iptables/nft.h"
-#include "iptables/nft-arp.h"
 
 static void arpmangle_print_help(void)
 {
iptables/nft-arp.c +4 -18
index 65bd965eb69f6..e6e4d2d81e528 100644
@@ -25,22 +25,8 @@
 #include <linux/netfilter/nf_tables.h>
 
 #include "nft-shared.h"
-#include "nft-arp.h"
 #include "nft.h"
-
-/* a few names */
-char *arp_opcodes[] =
-{
-	"Request",
-	"Reply",
-	"Request_Reverse",
-	"Reply_Reverse",
-	"DRARP_Request",
-	"DRARP_Reply",
-	"DRARP_Error",
-	"InARP_Request",
-	"ARP_NAK",
-};
+#include "xshared.h"
 
 static bool need_devaddr(struct arpt_devaddr_info *info)
 {
@@ -429,7 +415,7 @@ after_devdst:
 
 		printf("%s%s", sep, fw->arp.invflags & IPT_INV_ARPOP
 			? "! " : "");
-		if (tmp <= NUMOPCODES && !(format & FMT_NUMERIC))
+		if (tmp <= ARP_NUMOPCODES && !(format & FMT_NUMERIC))
 			printf("--opcode %s", arp_opcodes[tmp-1]);
 		else
 			printf("--opcode %d", tmp);
@@ -660,11 +646,11 @@ static void nft_arp_post_parse(int command,
 				   &cs->arp.arp.arpop_mask, 10)) {
 			int i;
 
-			for (i = 0; i < NUMOPCODES; i++)
+			for (i = 0; i < ARP_NUMOPCODES; i++)
 				if (!strcasecmp(arp_opcodes[i],
 						args->arp_opcode))
 					break;
-			if (i == NUMOPCODES)
+			if (i == ARP_NUMOPCODES)
 				xtables_error(PARAMETER_PROBLEM,
 					      "Problem with specified opcode");
 			cs->arp.arp.arpop = htons(i+1);
iptables/nft-arp.h deleted +0 -7
deleted file mode 100644
index 3411fc3d7c7b3..0000000000000
@@ -1,7 +0,0 @@
-#ifndef _NFT_ARP_H_
-#define _NFT_ARP_H_
-
-extern char *arp_opcodes[];
-#define NUMOPCODES 9
-
-#endif
iptables/xshared.c +14 -0
index 00828c8ae87d9..674b49cb72798 100644
@@ -20,6 +20,20 @@
 #include <signal.h>
 #include "xshared.h"
 
+/* a few arp opcode names */
+char *arp_opcodes[] =
+{
+	"Request",
+	"Reply",
+	"Request_Reverse",
+	"Reply_Reverse",
+	"DRARP_Request",
+	"DRARP_Reply",
+	"DRARP_Error",
+	"InARP_Request",
+	"ARP_NAK",
+};
+
 /*
  * Print out any special helps. A user might like to be able to add a --help
  * to the commandline, and see expected results. So we call help for all
iptables/xshared.h +3 -0
index ca761ee7246ad..2fdebc326a6d6 100644
@@ -330,4 +330,7 @@ void ipv4_post_parse(int command, struct iptables_command_state *cs,
 void ipv6_post_parse(int command, struct iptables_command_state *cs,
 		     struct xtables_args *args);
 
+extern char *arp_opcodes[];
+#define ARP_NUMOPCODES 9
+
 #endif /* IPTABLES_XSHARED_H */
iptables/xtables-arp.c +0 -1
index 68514297f381f..f1a128fc55647 100644
@@ -37,7 +37,6 @@
 #include "xshared.h"
 
 #include "nft.h"
-#include "nft-arp.h"
 
 static struct option original_opts[] = {
 	{ "append", 1, 0, 'A' },
iptables/xtables-monitor.c +0 -1
index 8a04f4d1490c1..905bb7fed6309 100644
@@ -37,7 +37,6 @@
 #include "iptables.h" /* for xtables_globals */
 #include "xtables-multi.h"
 #include "nft.h"
-#include "nft-arp.h"
 
 struct cb_arg {
 	uint32_t nfproto;
iptables/xtables.c +0 -1
index c44b39acdcd97..c65c3fce5cfbe 100644
@@ -42,7 +42,6 @@
 #include <fcntl.h>
 #include "xshared.h"
 #include "nft-shared.h"
-#include "nft-arp.h"
 #include "nft.h"
 
 static struct option original_opts[] = {
Signature · 1 line
2.34.1
author reviewer
#3 lore raw

PATCH 3/5 xshared: Extend xtables_printhelp() for arptables

In reply to Phil Sutter

The function checks afinfo->family already to cover ip6tables specifics, doing the same for arptables does not make things much worse.

This changes arptables-nft help output slightly:

* List possible negations extrapositioned, which is preferred anyway (arptables-nft supports both)
* List --out-interface option at lexically sorted position
* Print --wait option, it's ignored just like with iptables
* Restore default target option printing as with legacy arptables (not sure if arptables-nft ever did this) by explicitly loading them.

While being at it, add --set-counters short option '-c' to help output for ip(6)tables.

This effectively removes the need for (and all users of) xtables_global's 'print_help' callback, so drop that field from the struct.

---
 include/xtables.h      |   1 -
 iptables/ip6tables.c   |   1 -
 iptables/iptables.c    |   1 -
 iptables/xshared.c     |  53 +++++++++++++++++----
 iptables/xshared.h     |   1 -
 iptables/xtables-arp.c | 104 -----------------------------------------
 iptables/xtables.c     |   1 -
 7 files changed, 43 insertions(+), 119 deletions(-)
include/xtables.h +0 -1
index 84369dacb7e37..a93e8f6e91585 100644
@@ -425,7 +425,6 @@ struct xtables_globals
 	struct option *opts;
 	void (*exit_err)(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
 	int (*compat_rev)(const char *name, uint8_t rev, int opt);
-	void (*print_help)(const struct xtables_rule_match *m);
 };
 
 #define XT_GETOPT_TABLEEND {.name = NULL, .has_arg = false}
iptables/ip6tables.c +0 -1
index f4796b897d74c..5806a13ce0710 100644
@@ -93,7 +93,6 @@ struct xtables_globals ip6tables_globals = {
 	.optstring = OPTSTRING_COMMON "R:S::W::" "46bg:h::m:nvw::x",
 	.orig_opts = original_opts,
 	.compat_rev = xtables_compatible_revision,
-	.print_help = xtables_printhelp,
 };
 
 /*
iptables/iptables.c +0 -1
index ccebb1a6c7eac..edde604cf2367 100644
@@ -90,7 +90,6 @@ struct xtables_globals iptables_globals = {
 	.optstring = OPTSTRING_COMMON "R:S::W::" "46bfg:h::m:nvw::x",
 	.orig_opts = original_opts,
 	.compat_rev = xtables_compatible_revision,
-	.print_help = xtables_printhelp,
 };
 
 /*
iptables/xshared.c +43 -10

89 lines not on this page · raw message

iptables/xshared.h +0 -1
index 2fdebc326a6d6..e69da7351efa4 100644
@@ -258,7 +258,6 @@ void save_rule_details(const char *iniface, unsigned const char *iniface_mask,
 
 int print_match_save(const struct xt_entry_match *e, const void *ip);
 
-void xtables_printhelp(const struct xtables_rule_match *matches);
 void exit_tryhelp(int status, int line) __attribute__((noreturn));
 
 struct addr_mask {
iptables/xtables-arp.c +0 -104

118 lines not on this page · raw message

iptables/xtables.c +0 -1
index c65c3fce5cfbe..41b6eb4838733 100644
@@ -91,7 +91,6 @@ struct xtables_globals xtables_globals = {
 	.optstring = OPTSTRING_COMMON "R:S::W::" "46bfg:h::m:nvw::x",
 	.orig_opts = original_opts,
 	.compat_rev = nft_compatible_revision,
-	.print_help = xtables_printhelp,
 };
 
 /*
Signature · 1 line
2.34.1
author reviewer
#4 lore raw

PATCH 4/5 libxtables: Drop xtables_globals 'optstring' field

In reply to Phil Sutter

Define the different optstrings in xshared.h instead, they are not relevant for other libxtables users.

---
 include/xtables.h      |  1 -
 iptables/ip6tables.c   |  1 -
 iptables/iptables.c    |  1 -
 iptables/xshared.c     | 17 ++++++++++++++++-
 iptables/xshared.h     |  3 +++
 iptables/xtables-arp.c |  1 -
 iptables/xtables-eb.c  |  3 +--
 iptables/xtables.c     |  1 -
 8 files changed, 20 insertions(+), 8 deletions(-)
include/xtables.h +0 -1
index a93e8f6e91585..8c1065bc7e010 100644
@@ -420,7 +420,6 @@ struct xtables_globals
 {
 	unsigned int option_offset;
 	const char *program_name, *program_version;
-	const char *optstring;
 	struct option *orig_opts;
 	struct option *opts;
 	void (*exit_err)(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
iptables/ip6tables.c +0 -1
index 5806a13ce0710..75984cc1bcdd8 100644
@@ -90,7 +90,6 @@ static struct option original_opts[] = {
 struct xtables_globals ip6tables_globals = {
 	.option_offset = 0,
 	.program_version = PACKAGE_VERSION " (legacy)",
-	.optstring = OPTSTRING_COMMON "R:S::W::" "46bg:h::m:nvw::x",
 	.orig_opts = original_opts,
 	.compat_rev = xtables_compatible_revision,
 };
iptables/iptables.c +0 -1
index edde604cf2367..e5207ba106057 100644
@@ -87,7 +87,6 @@ static struct option original_opts[] = {
 struct xtables_globals iptables_globals = {
 	.option_offset = 0,
 	.program_version = PACKAGE_VERSION " (legacy)",
-	.optstring = OPTSTRING_COMMON "R:S::W::" "46bfg:h::m:nvw::x",
 	.orig_opts = original_opts,
 	.compat_rev = xtables_compatible_revision,
 };
iptables/xshared.c +16 -1
index e959f203e5cc9..fae5ddd5df93e 100644
@@ -1340,6 +1340,20 @@ static void check_inverse(struct xtables_args *args, const char option[],
 	}
 }
 
+static const char *optstring_lookup(int family)
+{
+	switch (family) {
+	case AF_INET:
+	case AF_INET6:
+		return IPT_OPTSTRING;
+	case NFPROTO_ARP:
+		return ARPT_OPTSTRING;
+	case NFPROTO_BRIDGE:
+		return EBT_OPTSTRING;
+	}
+	return "";
+}
+
 void do_parse(int argc, char *argv[],
 	      struct xt_cmd_parse *p, struct iptables_command_state *cs,
 	      struct xtables_args *args)
@@ -1370,7 +1384,8 @@ void do_parse(int argc, char *argv[],
 	opterr = 0;
 
 	xt_params->opts = xt_params->orig_opts;
-	while ((cs->c = getopt_long(argc, argv, xt_params->optstring,
+	while ((cs->c = getopt_long(argc, argv,
+				    optstring_lookup(afinfo->family),
 				    xt_params->opts, NULL)) != -1) {
 		switch (cs->c) {
 			/*
iptables/xshared.h +3 -0
index e69da7351efa4..14568bb00fb65 100644
@@ -68,6 +68,9 @@ struct xtables_rule_match;
 struct xtables_target;
 
 #define OPTSTRING_COMMON "-:A:C:D:E:F::I:L::M:N:P:VX::Z::" "c:d:i:j:o:p:s:t:"
+#define IPT_OPTSTRING	OPTSTRING_COMMON "R:S::W::" "46bfg:h::m:nvw::x"
+#define ARPT_OPTSTRING	OPTSTRING_COMMON "R:S::" "h::l:nv" /* "m:" */
+#define EBT_OPTSTRING	OPTSTRING_COMMON "hv"
 
 /* define invflags which won't collide with IPT ones */
 #define IPT_INV_SRCDEVADDR	0x0080
iptables/xtables-arp.c +0 -1
index bf7d44e7b815b..71518a9cbdb6a 100644
@@ -86,7 +86,6 @@ static struct option original_opts[] = {
 struct xtables_globals arptables_globals = {
 	.option_offset		= 0,
 	.program_version	= PACKAGE_VERSION " (nf_tables)",
-	.optstring		= OPTSTRING_COMMON "C:R:S::" "h::l:nv" /* "m:" */,
 	.orig_opts		= original_opts,
 	.compat_rev		= nft_compatible_revision,
 };
iptables/xtables-eb.c +1 -2
index a7bfb9c5c60b8..3d15063e80e91 100644
@@ -220,7 +220,6 @@ struct option ebt_original_options[] =
 struct xtables_globals ebtables_globals = {
 	.option_offset 		= 0,
 	.program_version	= PACKAGE_VERSION " (nf_tables)",
-	.optstring		= OPTSTRING_COMMON "hv",
 	.orig_opts		= ebt_original_options,
 	.compat_rev		= nft_compatible_revision,
 };
@@ -734,7 +733,7 @@ int do_commandeb(struct nft_handle *h, int argc, char *argv[], char **table,
 	opterr = false;
 
 	/* Getopt saves the day */
-	while ((c = getopt_long(argc, argv, xt_params->optstring,
+	while ((c = getopt_long(argc, argv, EBT_OPTSTRING,
 					opts, NULL)) != -1) {
 		cs.c = c;
 		switch (c) {
iptables/xtables.c +0 -1
index 41b6eb4838733..70924176df8c1 100644
@@ -88,7 +88,6 @@ static struct option original_opts[] = {
 struct xtables_globals xtables_globals = {
 	.option_offset = 0,
 	.program_version = PACKAGE_VERSION " (nf_tables)",
-	.optstring = OPTSTRING_COMMON "R:S::W::" "46bfg:h::m:nvw::x",
 	.orig_opts = original_opts,
 	.compat_rev = nft_compatible_revision,
 };
Signature · 1 line
2.34.1
author reviewer
#5 lore raw

PATCH 5/5 libxtables: Revert change to struct xtables_pprot

In reply to Phil Sutter

While protocol values may exceed eight bits, the data structure is indeed used only to store the static list of name/value pairs for faster lookups. None of those has such a value and if one is added in future, the compiler will complain about it.

So restore the old field type to retain binary compatibility.

Fixes 556f704458cdb ("Use proto_to_name() from xshared in more places")
---
 include/xtables.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/xtables.h +1 -1
index 8c1065bc7e010..c2694b7b28886 100644
@@ -395,7 +395,7 @@ struct xtables_rule_match {
  */
 struct xtables_pprot {
 	const char *name;
-	uint16_t num;
+	uint8_t num;
 };
 
 enum xtables_tryload {
Signature · 1 line
2.34.1