Skip to contentSkip to conversation

libxtables: Introduce xtables_set_revision() and make iptables and ip6tables use it

PATCH In review jamal 2 participants 4 messages Feb 10, 2009 – Feb 11, 2009 netfilter

lore
Share & tools
lore

jamal

author
#0 lore raw

PATCH 1/1 libxtables: Introduce xtables_set_revision() and make iptables and ip6tables use it

Ok, with this change i am almost 100% happy. The only thing i fear that may break in the future from an API perspective now is that apps now have to do:

------

        xtables_init();
        xtables_set_nfproto(NFPROTO_IPV4);
        xtables_set_params(&tcxt_globals);

-------

As i mentioned to Jan I would like to unify that into a simple call:

-----------

        xtables_init_all(&tcxt_globals);

----------

Then I will be more confident that we we will have peace going forward. Please let me know if this is acceptable and i will prepare a patch.

cheers,
jamal

commit 56dbd3ef3207cfb0cff26db34c69e9f31d9acf06

Author: Jamal Hadi Salim <[email protected]>
Date:   Tue Feb 10 16:30:14 2009 -0500

Introduce xtables_set_revision() and make iptables and ip6tables use it.

include/xtables.h.in +3 -1
index 609e6a6..d49fea9 100644
@@ -207,7 +207,9 @@ extern void *xtables_malloc(size_t);
 extern int xtables_insmod(const char *, const char *, bool);
 extern int xtables_load_ko(const char *, bool);
 extern int xtables_set_params(struct xtables_globals *xtp);
-void xtables_free_opts(int reset_offset);
+extern void xtables_free_opts(int reset_offset);
+extern void xtables_set_revision(char *name, u_int8_t revision);
+
 
 extern struct option *xtables_merge_options(struct option *oldopts,
 		const struct option *newopts, unsigned int *option_offset);
ip6tables.c +3 -11
index b1de41b..3bc29bd 100644
@@ -1288,14 +1288,6 @@ static void clear_rule_matches(struct ip6tables_rule_match **matches)
 	*matches = NULL;
 }
 
-static void set_revision(char *name, u_int8_t revision)
-{
-	/* Old kernel sources don't have ".revision" field,
-	   but we stole a byte from name. */
-	name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
-	name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
-}
-
 int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
 {
 	struct ip6t_entry fw, *e = NULL;
@@ -1560,7 +1552,7 @@ int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **hand
 				target->t = xtables_calloc(1, size);
 				target->t->u.target_size = size;
 				strcpy(target->t->u.user.name, jumpto);
-				set_revision(target->t->u.user.name,
+				xtables_set_revision(target->t->u.user.name,
 					     target->revision);
 				if (target->init != NULL)
 					target->init(target->t);
@@ -1613,7 +1605,7 @@ int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **hand
 			m->m = xtables_calloc(1, size);
 			m->m->u.match_size = size;
 			strcpy(m->m->u.user.name, m->name);
-			set_revision(m->m->u.user.name, m->revision);
+			xtables_set_revision(m->m->u.user.name, m->revision);
 			if (m->init != NULL)
 				m->init(m->m);
 			if (m != m->next)
@@ -1760,7 +1752,7 @@ int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **hand
 					m->m = xtables_calloc(1, size);
 					m->m->u.match_size = size;
 					strcpy(m->m->u.user.name, m->name);
-					set_revision(m->m->u.user.name,
+					xtables_set_revision(m->m->u.user.name,
 						     m->revision);
 					if (m->init != NULL)
 						m->init(m->m);
iptables.c +4 -12
index dfc1937..c5b48c4 100644
@@ -1296,14 +1296,6 @@ static void clear_rule_matches(struct iptables_rule_match **matches)
 	*matches = NULL;
 }
 
-static void set_revision(char *name, u_int8_t revision)
-{
-	/* Old kernel sources don't have ".revision" field,
-	   but we stole a byte from name. */
-	name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
-	name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
-}
-
 void
 get_kernel_version(void) {
 	static struct utsname uts;
@@ -1574,7 +1566,7 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
 				target->t = xtables_calloc(1, size);
 				target->t->u.target_size = size;
 				strcpy(target->t->u.user.name, jumpto);
-				set_revision(target->t->u.user.name,
+				xtables_set_revision(target->t->u.user.name,
 					     target->revision);
 				if (target->init != NULL)
 					target->init(target->t);
@@ -1633,7 +1625,7 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
 			m->m = xtables_calloc(1, size);
 			m->m->u.match_size = size;
 			strcpy(m->m->u.user.name, m->name);
-			set_revision(m->m->u.user.name, m->revision);
+			xtables_set_revision(m->m->u.user.name, m->revision);
 			if (m->init != NULL)
 				m->init(m->m);
 			if (m != m->next) {
@@ -1787,7 +1779,7 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
 					m->m = xtables_calloc(1, size);
 					m->m->u.match_size = size;
 					strcpy(m->m->u.user.name, m->name);
-					set_revision(m->m->u.user.name,
+					xtables_set_revision(m->m->u.user.name,
 						     m->revision);
 					if (m->init != NULL)
 						m->init(m->m);
@@ -1948,7 +1940,7 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
 			target->t->u.target_size = size;
 			strcpy(target->t->u.user.name, jumpto);
 			if (!iptc_is_chain(jumpto, *handle))
-				set_revision(target->t->u.user.name,
+				xtables_set_revision(target->t->u.user.name,
 					     target->revision);
 			if (target->init != NULL)
 				target->init(target->t);
xtables.c +27 -0
index 805b940..48b4104 100644
@@ -99,6 +99,8 @@ void xtables_free_opts(int reset_offset)
 	}
 }
 
+void xtables_set_revision_ipv4(char *name, u_int8_t revision);
+void xtables_set_revision_ipv6(char *name, u_int8_t revision);
 
 struct option *
 xtables_merge_options(struct option *oldopts, const struct option *newopts,
@@ -130,6 +132,22 @@ xtables_merge_options(struct option *oldopts, const struct option *newopts,
 	return merge;
 }
 
+void xtables_set_revision_ipv4(char *name, u_int8_t revision)
+{
+	/* Old kernel sources don't have ".revision" field,
+	*            but we stole a byte from name. */
+	name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
+	name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
+}
+
+void xtables_set_revision_ipv6(char *name, u_int8_t revision)
+{
+	/* Old kernel sources don't have ".revision" field,
+	*            but we stole a byte from name. */
+	name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
+	name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
+}
+
 /**
  * xtables_afinfo - protocol family dependent information
  * @kmod:		kernel module basename (e.g. "ip_tables")
@@ -138,6 +156,7 @@ xtables_merge_options(struct option *oldopts, const struct option *newopts,
  * @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
+ * @set_revision:	callback to set revision
  */
 struct xtables_afinfo {
 	const char *kmod;
@@ -146,6 +165,7 @@ struct xtables_afinfo {
 	uint8_t ipproto;
 	int so_rev_match;
 	int so_rev_target;
+	void (*set_revision)(char *name, u_int8_t revision);
 };
 
 static const struct xtables_afinfo afinfo_ipv4 = {
@@ -155,6 +175,7 @@ static const struct xtables_afinfo afinfo_ipv4 = {
 	.ipproto       = IPPROTO_IP,
 	.so_rev_match  = IPT_SO_GET_REVISION_MATCH,
 	.so_rev_target = IPT_SO_GET_REVISION_TARGET,
+	.set_revision = xtables_set_revision_ipv4,
 };
 
 static const struct xtables_afinfo afinfo_ipv6 = {
@@ -164,6 +185,7 @@ static const struct xtables_afinfo afinfo_ipv6 = {
 	.ipproto       = IPPROTO_IPV6,
 	.so_rev_match  = IP6T_SO_GET_REVISION_MATCH,
 	.so_rev_target = IP6T_SO_GET_REVISION_TARGET,
+	.set_revision = xtables_set_revision_ipv6,
 };
 
 static const struct xtables_afinfo *afinfo;
@@ -212,6 +234,11 @@ void xtables_set_nfproto(uint8_t nfproto)
 	}
 }
 
+void xtables_set_revision(char *name, u_int8_t revision)
+{
+	afinfo->set_revision(name, revision);
+}
+
 /**
  * xtables_*alloc - wrappers that exit on failure
  */
#1 lore raw
Quoted 11 lines · jamal

As i mentioned to Jan I would like to unify that into a simple call:

-----------

       xtables_init_all(&tcxt_globals);

----------

Then I will be more confident that we we will have peace going forward. Please let me know if this is acceptable and i will prepare a patch.

Ok.

include/xtables.h.in extern void *xtables_malloc(size_t); · line 211

Quoted 2 lines
include/xtables.h.in +2 -0
+extern void xtables_free_opts(int reset_offset);
+extern void xtables_set_revision(char *name, u_int8_t revision);

You need const char * to avoid warnings.

Quoted 11 lines
xtables.c +2 -0
index 805b940..48b4104 100644
@@ -99,6 +99,8 @@ void xtables_free_opts(int reset_offset)
 	}
 }
 
+void xtables_set_revision_ipv4(char *name, u_int8_t revision);
+void xtables_set_revision_ipv6(char *name, u_int8_t revision);
 
Quoted 2 lines

struct option *
xtables_merge_options(struct option *oldopts, const struct option *newopts,

Why are these prototypes here? (You can make the functions static and reorder to leave them out.)

Quoted 10 lines
xtables.c +6 -0
@@ -130,6 +132,22 @@ xtables_merge_options(struct option *oldopts, const struct option *newopts,
 	return merge;
 }
 
+void xtables_set_revision_ipv4(char *name, u_int8_t revision)
+{
+	/* Old kernel sources don't have ".revision" field,
+	*            but we stole a byte from name. */
+	name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
+	name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;

xtables.c xtables_merge_options(struct option *oldopts, const struct option *newopts, · line 150

Quoted 10 lines
xtables.c +10 -0
+}
+
+void xtables_set_revision_ipv6(char *name, u_int8_t revision)
+{
+	/* Old kernel sources don't have ".revision" field,
+	*            but we stole a byte from name. */
+	name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
+	name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
+}
+

Since IPT_FUNCTION_MAXNAMELEN is the same as IP6T_FUNCTION_MAXNAMELEN, these functions can be consolidated.

xtables.c static const struct xtables_afinfo afinfo_ipv4 = { · line 178

Quoted 5 lines
xtables.c +1 -0
@@ -155,6 +175,7 @@ static const struct xtables_afinfo afinfo_ipv4 = {
 	.ipproto       = IPPROTO_IP,
 	.so_rev_match  = IPT_SO_GET_REVISION_MATCH,
 	.so_rev_target = IPT_SO_GET_REVISION_TARGET,
+	.set_revision = xtables_set_revision_ipv4,

One off a space ;^)

Quoted 8 lines
xtables.c +1 -0
@@ -164,6 +185,7 @@ static const struct xtables_afinfo afinfo_ipv6 = {
 	.ipproto       = IPPROTO_IPV6,
 	.so_rev_match  = IP6T_SO_GET_REVISION_MATCH,
 	.so_rev_target = IP6T_SO_GET_REVISION_TARGET,
+	.set_revision = xtables_set_revision_ipv6,
 };
 
 static const struct xtables_afinfo *afinfo;
author
#2 lore raw
Quoted 9 lines · Jan Engelhardt
Quoted 8 lines · jamal

As i mentioned to Jan I would like to unify that into a simple call:

-----------

       xtables_init_all(&tcxt_globals);

----------

Quoted 5 lines
Quoted 3 lines

Then I will be more confident that we we will have peace going forward. Please let me know if this is acceptable and i will prepare a patch.

Ok.

Ok, goody - I actually had almost finished it; have some cycles. I will post shortly.

Quoted 5 lines

include/xtables.h.in extern void *xtables_malloc(size_t); · line 211

Quoted 3 lines
include/xtables.h.in +2 -0
+extern void xtables_free_opts(int reset_offset);
+extern void xtables_set_revision(char *name, u_int8_t revision);

You need const char * to avoid warnings.

Ok.

Quoted 8 lines
Quoted 8 lines
xtables.c +0 -0
index 805b940..48b4104 100644
@@ -99,6 +99,8 @@ void xtables_free_opts(int reset_offset)
 	}
 }
 
Quoted 9 lines
Quoted 5 lines
xtables.c +2 -0
+void xtables_set_revision_ipv4(char *name, u_int8_t revision);
+void xtables_set_revision_ipv6(char *name, u_int8_t revision);
 

struct option *
xtables_merge_options(struct option *oldopts, const struct option *newopts,

Why are these prototypes here? (You can make the functions static and reorder to leave them out.)

True.

Quoted 3 lines

Since IPT_FUNCTION_MAXNAMELEN is the same as IP6T_FUNCTION_MAXNAMELEN, these functions can be consolidated.

Shouldve checked that.
Ok, since this patch hasnt been merged; i will resend it and i dont see the need for a callback in xtables_globals

Thanks for all the feedback Jan.

cheers,
jamal

author
#3 lore raw

RFC 1/1 libxtables: Introduce xtables_init_all()

Quoted 4 lines · jamal

Ok, goody - I actually had almost finished it; have some cycles. I will post shortly.

Ok, here it is inlined. I just got pulled off into some tangent, so will sync with Patrick's tree only later today and fixup all the issues you brought up.

With this patch (in addition to the other 3), I think tc/m_xt.c will be a happy camper. Any additional changes (such as killing program_name) are not exposed to user of xtables so should not affect it. I have moved around xtables_set_params() in this patch for readability purposes inside xtables.c

cheers,
jamal

include/ip6tables.h +1 -0
index 1956cf0..05a963e 100644
@@ -14,6 +14,7 @@
 #define ip6t_tryload		xt_tryload
 
 extern int line;
+extern struct xtables_globals ip6tables_globals;
 
 /* Your shared library should call one of these. */
 extern int do_command6(int argc, char *argv[], char **table,
include/iptables.h +1 -0
index f7ae0cc..69564fe 100644
@@ -15,6 +15,7 @@
 
 extern int line;
 
+extern struct xtables_globals iptables_globals;
 /* Your shared library should call one of these. */
 extern int do_command(int argc, char *argv[], char **table,
 		      struct iptc_handle **handle);
include/xtables.h.in +1 -0
index d49fea9..477d534 100644
@@ -209,6 +209,7 @@ extern int xtables_load_ko(const char *, bool);
 extern int xtables_set_params(struct xtables_globals *xtp);
 extern void xtables_free_opts(int reset_offset);
 extern void xtables_set_revision(char *name, u_int8_t revision);
+extern int xtables_init_all(struct xtables_globals *xtp, uint8_t

nfproto);

extern struct option *xtables_merge_options(struct option *oldopts,

ip6tables-restore.c +7 -2
index acaf97b..c7557d4 100644
@@ -131,8 +131,13 @@ int main(int argc, char *argv[])
 	line = 0;
 
 	xtables_program_name = program_name;
-	xtables_init();
-	xtables_set_nfproto(NFPROTO_IPV6);
+
+	ip6tables_globals.program_name = "ip6tables-restore",
+	c = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6);
+	if (c < 0) {
+		fprintf(stderr, "%s/%s Failed to initialize xtables
\n",ip6tables_globals.program_name,ip6tables_globals.program_version);
+		exit(1);
+	}
 #ifdef NO_SHARED_LIBS
 	init_extensions();
 #endif
ip6tables-save.c +7 -2
index 32b5992..dc066b8 100644
@@ -140,8 +140,13 @@ int main(int argc, char *argv[])
 	program_version = IPTABLES_VERSION;
 
 	xtables_program_name = program_name;
-	xtables_init();
-	xtables_set_nfproto(NFPROTO_IPV6);
+
+	ip6tables_globals.program_name = "ip6tables-save",
+	c = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6);
+	if (c < 0) {
+		fprintf(stderr, "%s/%s Failed to initialize xtables
\n",ip6tables_globals.program_name, ip6tables_globals.program_version);
+		exit(1);
+	}
 #ifdef NO_SHARED_LIBS
 	init_extensions();
 #endif
ip6tables-standalone.c +7 -2
index cea4818..27583be 100644
@@ -53,8 +53,13 @@ main(int argc, char *argv[])
 	program_version = IPTABLES_VERSION;
 
 	xtables_program_name = program_name;
-	xtables_init();
-	xtables_set_nfproto(NFPROTO_IPV6);
+
+	ip6tables_globals.program_name = "ip6tables",
+	ret = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6);
+	if (ret < 0) {
+		fprintf(stderr, "%s/%s Failed to initialize xtables
\n",ip6tables_globals.program_name, ip6tables_globals.program_version);
+		exit(1);
+	}
 #ifdef NO_SHARED_LIBS
 	init_extensions();
 #endif
ip6tables.c +0 -0
index 3bc29bd..474405b 100644
@@ -148,7 +148,6 @@ void ip6tables_exit_error(enum xtables_exittype

status, const char *msg, ...) __
struct xtables_globals ip6tables_globals = {
.option_offset = 0,
.program_version = IPTABLES_VERSION,
- .program_name = "ip6tables",
.opts = original_opts,
.orig_opts = original_opts,
.exit_err = ip6tables_exit_error,
@@ -1333,7 +1332,6 @@ int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **hand
demand-load a protocol. */
opterr = 0;

- xtables_set_params(&ip6tables_globals);
while ((c = getopt_long(argc, argv,
"-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:",
opts, NULL)) != -1) {

iptables-restore.c +7 -2
index 810806f..8a260df 100644
@@ -133,8 +133,13 @@ main(int argc, char *argv[])
 	line = 0;
 
 	xtables_program_name = program_name;
-	xtables_init();
-	xtables_set_nfproto(NFPROTO_IPV4);
+
+	iptables_globals.program_name = "iptables-restore";
+	c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
+	if (c < 0) {
+		fprintf(stderr, "%s/%s Failed to initialize xtables
\n",iptables_globals.program_name, iptables_globals.program_version);
+		exit(1);
+	}
 #ifdef NO_SHARED_LIBS
 	init_extensions();
 #endif
iptables-save.c +7 -2
index c4306fd..b36ed34 100644
@@ -140,8 +140,13 @@ main(int argc, char *argv[])
 	program_version = IPTABLES_VERSION;
 
 	xtables_program_name = program_name;
-	xtables_init();
-	xtables_set_nfproto(NFPROTO_IPV4);
+
+	iptables_globals.program_name = "iptables-save";
+	c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
+	if (c < 0) {
+		fprintf(stderr, "%s/%s Failed to initialize xtables
\n",iptables_globals.program_name, iptables_globals.program_version);
+		exit(1);
+	}
 #ifdef NO_SHARED_LIBS
 	init_extensions();
 #endif
iptables-standalone.c +7 -2
index ece7cf4..3aed1c1 100644
@@ -54,8 +54,13 @@ main(int argc, char *argv[])
 	program_version = IPTABLES_VERSION;
 
 	xtables_program_name = program_name;
-	xtables_init();
-	xtables_set_nfproto(NFPROTO_IPV4);
+	iptables_globals.program_name = "iptables";
+
+	ret = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
+	if (ret < 0) {
+		fprintf(stderr, "%s/%s Failed to initialize xtables
\n",iptables_globals.program_name, iptables_globals.program_version);
+		exit(1);
+	}
 #ifdef NO_SHARED_LIBS
 	init_extensions();
 #endif
iptables.c +0 -0
index c5b48c4..ce43075 100644
@@ -149,7 +149,6 @@ void iptables_exit_error(enum xtables_exittype

status, const char *msg, ...) __a
struct xtables_globals iptables_globals = {
.option_offset = 0,
.program_version = IPTABLES_VERSION,
- .program_name = "iptables",
.opts = original_opts,
.orig_opts = original_opts,
.exit_err = iptables_exit_error,
@@ -1355,7 +1354,6 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
demand-load a protocol. */
opterr = 0;

- xtables_set_params(&iptables_globals);
while ((c = getopt_long(argc, argv,
"-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:",
opts, NULL)) != -1) {

xtables.c +0 -0
index 48b4104..c0077e0 100644
@@ -64,31 +64,6 @@ void basic_exit_err(enum xtables_exittype status,

const char *msg, ...)
}

-/**
- * xtables_set_params - set the global parameters used by xtables
- * @xtp: input xtables_globals structure
- *
- * The app is expected to pass a valid xtables_globals data-filled
- * with proper values
- * @xtp cannot be NULL
- *
- * Returns -1 on failure to set and 0 on success
- */
-int xtables_set_params(struct xtables_globals *xtp)
-{
- if (!xtp) {
- fprintf(stderr, "%s: Illegal global params\n",__func__);
- return -1;
- }
-
- xt_params = xtp;
-
- if (!xt_params->exit_err)
- xt_params->exit_err = basic_exit_err;
-
- return 0;
-}
-
void xtables_free_opts(int reset_offset)
{
if (xt_params->opts != xt_params->orig_opts) {
@@ -240,6 +215,38 @@ void xtables_set_revision(char *name, u_int8_t revision)
}

/**
+ * xtables_set_params - set the global parameters used by xtables
+ * @xtp: input xtables_globals structure
+ *
+ * The app is expected to pass a valid xtables_globals data-filled
+ * with proper values
+ * @xtp cannot be NULL
+ *
+ * Returns -1 on failure to set and 0 on success
+ */
+int xtables_set_params(struct xtables_globals *xtp)
+{
+ if (!xtp) {
+ fprintf(stderr, "%s: Illegal global params\n",__func__);
+ return -1;
+ }
+
+ xt_params = xtp;
+
+ if (!xt_params->exit_err)
+ xt_params->exit_err = basic_exit_err;
+
+ return 0;
+}
+
+int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto) +{
+ xtables_init();
+ xtables_set_nfproto(nfproto);
+ return xtables_set_params(xtp);
+}
+
+/**
* xtables_*alloc - wrappers that exit on failure
*/
void *xtables_calloc(size_t count, size_t size)