Skip to contentSkip to conversation

Review xtables.h vs. xshared.h

PATCH New Phil Sutter 1 participant 3 messages Jun 11, 2022 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Review xtables.h vs. xshared.h

I didn't like how libxtables has to include xshared.h from iptables code base, this series eliminates this:

* struct xtables_afinfo is used to hold family-specific configuration. There are static const instances in libxtables and xtables_set_nfproto() mangles the public 'afinfo' pointer. This is all libxtables code, so move the struct definition and pointer declaration into xtables.h (patch 1).

* XT_OPTION_OFFSET_SCALE is the base distance between different extensions' offset values. It is mostly used by libxtables when merging options and referenced by xshared.c for sanity checking only. Patch 2 moves it into xtables.h (and turns the single value enum into a define).

To avoid impact on external libxtables users, guard both changes above by XTABLES_INTERNAL. Without access to xshared.h, external extensions can't have made use of afinfo or XT_OPTION_OFFSET_SCALE yet.

Phil Sutter (2):
libxtables: Move struct xtables_afinfo into xtables.h
libxtables: Define XT_OPTION_OFFSET_SCALE in xtables.h

 extensions/libxt_set.c |  6 ++++++
 extensions/libxt_set.h |  1 -
 include/xtables.h      | 25 +++++++++++++++++++++++++
 iptables/xshared.h     | 26 --------------------------
 libxtables/xtables.c   |  1 -
 libxtables/xtoptions.c |  1 -
 6 files changed, 31 insertions(+), 29 deletions(-)
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH 1/2 libxtables: Move struct xtables_afinfo into xtables.h

The library "owns" this structure and maintains 'afinfo' pointer to instances of it. With libxt_set, there's even an extension making use of the data.

To avoid impact on library users, guard it by XTABLES_INTERNAL.

To eliminate the xshared.h include by libxt_set, DEBUGP has to be redefined. Other extensions have similar defines, fix this later.

---
 extensions/libxt_set.c |  6 ++++++
 extensions/libxt_set.h |  1 -
 include/xtables.h      | 22 ++++++++++++++++++++++
 iptables/xshared.h     | 22 ----------------------
 4 files changed, 28 insertions(+), 23 deletions(-)
extensions/libxt_set.c +6 -0
index 16921023a22f8..a2137ab1eb180 100644
@@ -22,6 +22,12 @@
 #include <linux/netfilter/xt_set.h>
 #include "libxt_set.h"
 
+#ifdef DEBUG
+#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
+#else
+#define DEBUGP(x, args...)
+#endif
+
 /* Revision 0 */
 
 static void
extensions/libxt_set.h +0 -1
index ad895a7504d9d..597bf7ebe575a 100644
@@ -6,7 +6,6 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <errno.h>
-#include "../iptables/xshared.h"
 
 static int
 get_version(unsigned *version)
include/xtables.h +22 -0
index f1937f3ea0530..b8d8372d0e498 100644
@@ -665,6 +665,28 @@ void xtables_announce_chain(const char *name);
 
 extern void _init(void);
 
+/**
+ * xtables_afinfo - protocol family dependent information
+ * @kmod:		kernel module basename (e.g. "ip_tables")
+ * @proc_exists:	file which exists in procfs when module already loaded
+ * @libprefix:		prefix of .so library name (e.g. "libipt_")
+ * @family:		nfproto family
+ * @ipproto:		used by setsockopt (e.g. IPPROTO_IP)
+ * @so_rev_match:	optname to check revision support of match
+ * @so_rev_target:	optname to check revision support of target
+ */
+struct xtables_afinfo {
+	const char *kmod;
+	const char *proc_exists;
+	const char *libprefix;
+	uint8_t family;
+	uint8_t ipproto;
+	int so_rev_match;
+	int so_rev_target;
+};
+
+extern const struct xtables_afinfo *afinfo;
+
 #endif /* XTABLES_INTERNAL */
 
 #ifdef __cplusplus
iptables/xshared.h +0 -22
index 1d6b9bf4ee9b7..1fdc760a32442 100644
@@ -75,26 +75,6 @@ struct xtables_target;
 #define IPT_INV_ARPOP		0x0400
 #define IPT_INV_ARPHRD		0x0800
 
-/**
- * xtables_afinfo - protocol family dependent information
- * @kmod:		kernel module basename (e.g. "ip_tables")
- * @proc_exists:	file which exists in procfs when module already loaded
- * @libprefix:		prefix of .so library name (e.g. "libipt_")
- * @family:		nfproto family
- * @ipproto:		used by setsockopt (e.g. IPPROTO_IP)
- * @so_rev_match:	optname to check revision support of match
- * @so_rev_target:	optname to check revision support of target
- */
-struct xtables_afinfo {
-	const char *kmod;
-	const char *proc_exists;
-	const char *libprefix;
-	uint8_t family;
-	uint8_t ipproto;
-	int so_rev_match;
-	int so_rev_target;
-};
-
 /* trick for ebtables-compat, since watchers are targets */
 struct ebt_match {
 	struct ebt_match			*next;
@@ -187,8 +167,6 @@ int parse_counters(const char *string, struct xt_counters *ctr);
 bool tokenize_rule_counters(char **bufferp, char **pcnt, char **bcnt, int line);
 bool xs_has_arg(int argc, char *argv[]);
 
-extern const struct xtables_afinfo *afinfo;
-
 #define MAX_ARGC	255
 struct argv_store {
 	int argc;
Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH 2/2 libxtables: Define XT_OPTION_OFFSET_SCALE in xtables.h

In reply to Phil Sutter

This is the last symbol in xshared.h used by libxtables, move it over. Again, treat this as "implementation detail" and hence put it behind XTABLES_INTERNAL-curtains.

---
 include/xtables.h      | 3 +++
 iptables/xshared.h     | 4 ----
 libxtables/xtables.c   | 1 -
 libxtables/xtoptions.c | 1 -
 4 files changed, 3 insertions(+), 6 deletions(-)
include/xtables.h +3 -0
index b8d8372d0e498..9eba4f619d351 100644
@@ -687,6 +687,9 @@ struct xtables_afinfo {
 
 extern const struct xtables_afinfo *afinfo;
 
+/* base offset of merged extensions' consecutive options */
+#define XT_OPTION_OFFSET_SCALE	256
+
 #endif /* XTABLES_INTERNAL */
 
 #ifdef __cplusplus
iptables/xshared.h +0 -4
index 1fdc760a32442..1a019a7c04882 100644
@@ -132,10 +132,6 @@ struct subcommand {
 	mainfunc_t main;
 };
 
-enum {
-	XT_OPTION_OFFSET_SCALE = 256,
-};
-
 extern int subcmd_main(int, char **, const struct subcommand *);
 extern void xs_init_target(struct xtables_target *);
 extern void xs_init_match(struct xtables_match *);
libxtables/xtables.c +0 -1
index 0638f9271c601..dc645162973f0 100644
@@ -64,7 +64,6 @@
 #endif
 #include <getopt.h>
 #include "iptables/internal.h"
-#include "xshared.h"
 
 #define NPROTO	255
 
libxtables/xtoptions.c +0 -1
index 9d3ac5c8066cb..8174a560ec4df 100644
@@ -21,7 +21,6 @@
 #include <arpa/inet.h>
 #include <netinet/ip.h>
 #include "xtables.h"
-#include "xshared.h"
 #ifndef IPTOS_NORMALSVC
 #	define IPTOS_NORMALSVC 0
 #endif
Signature · 1 line
2.34.1