Skip to contentSkip to conversation

new API for libnetfilter_conntrack

RFC In review Pablo Neira Ayuso 3 participants 8 messages Oct 30, 2006 – Nov 10, 2006 1 message not in the archive netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

RFC 1/1 new API for libnetfilter_conntrack

Hi,

Attached a patch, yet incomplete, that introduces the new API for libnetfilter_conntrack. Basically the idea consists of providing "conntrack" objects constructor/destructor functions and an interface to get and set object attributes. The conntrack object structure is encapsulated therefore we can add new attributes without breaking binary backward compatibility. There is an example of the new API use in the patch (see test.c file)

See that conntrack object attributes are different from the netlink attributes therefore they provide a high level interface that abstract from netlink details. The idea is to let the programmer make the choice to work with high level (objects) or low level (netlink) and provide the gateways to switch from high to low level and vice-versa by means of functions to build/parse netlink messages from/to conntrack objects.

The reason why I posted this patch is because I'm thinking about generalizing this a bit more and provide a complete high level API that would be the same for all the netfilter subsystems. This will result in a single library to rules them all with a subdirectory that contains the details of every specific subsystem.

nfh = nf_open(); /* encapsulate nfnl_open() */
ssh = nf_setup_subsys(nfnlh, CONNTRACK); /* encapsulate nfnl_subsys_open() */
...
ct = nf_new_object(NF_CONNTRACK_OBJECT); nf_set_attr_u8(ct, ATTR_XXX, value); nf_set_attr_u16(ct, ATTR_XXX, value);
...
nf_query_object(ssh, NF_CT_CREATE, ct); nf_free_object(ct);
nf_close(nfh);

I'd like to hear from you before going ahead, always appreciate your feedback guys. Comments welcome.

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
include/libnetfilter_conntrack/libnetfilter_conntrack.h +110 -0
Index: libnetfilter_conntrack/include/libnetfilter_conntrack/libnetfilter_conntrack.h
===================================================================
@@ -238,6 +238,9 @@ extern int nfct_close(struct nfct_handle
 
 extern int nfct_fd(struct nfct_handle *cth);
 
+extern struct nfnl_handle *nfct_get_nfnl(struct nfct_handle *h);
+extern struct nfnl_subsys_handle *nfct_get_nfnlssh_ct(struct nfct_handle *h);
+
 /*
  * [Register|unregister] callbacks
  */
@@ -332,6 +335,113 @@ extern int nfct_sprintf_expect_id(char *
 extern void nfct_build_tuple(struct nfnlhdr *req, int size, 
 			     struct nfct_tuple *t, int type);
 
+/* 
+ * New libnetfilter_conntrack API 
+ */
+
+/* high level API */
+
+#include <sys/types.h>
+
+/* conntrack object */
+struct nf_conntrack;
+
+/* conntrack attributes */
+enum nf_conntrack_attr {
+	ATTR_ORIG_IPV4_SRC = 0,
+	ATTR_ORIG_IPV4_DST,
+	ATTR_REPL_IPV4_SRC,
+	ATTR_REPL_IPV4_DST,
+	ATTR_ORIG_IPV6_SRC,
+	ATTR_ORIG_IPV6_DST,
+	ATTR_REPL_IPV6_SRC,
+	ATTR_REPL_IPV6_DST,
+	ATTR_ORIG_PORT_SRC,
+	ATTR_ORIG_PORT_DST,
+	ATTR_REPL_PORT_SRC,
+	ATTR_REPL_PORT_DST,
+	ATTR_ICMP_TYPE,
+	ATTR_ICMP_CODE,
+	ATTR_ICMP_ID,
+	ATTR_ORIG_L3PROTO,
+	ATTR_REPL_L3PROTO,
+	ATTR_ORIG_L4PROTO,
+	ATTR_REPL_L4PROTO,
+	ATTR_TCP_STATE,
+	ATTR_SNAT_IPV4,
+	ATTR_DNAT_IPV4,
+	ATTR_SNAT_PORT,
+	ATTR_DNAT_PORT,
+	ATTR_TIMEOUT,
+	ATTR_MARK,
+	ATTR_ORIG_COUNTER_PACKETS,
+	ATTR_REPL_COUNTER_PACKETS,
+	ATTR_ORIG_COUNTER_BYTES,
+	ATTR_REPL_COUNTER_BYTES,
+	ATTR_USE,
+	ATTR_ID,
+	ATTR_ASSURED,
+	ATTR_SEEN_REPLY,
+	ATTR_MAX
+};
+
+/* constructor / destructor */
+extern struct nf_conntrack *nfct_new(void);
+extern void nfct_destroy(struct nf_conntrack *ct);
+
+/* setter */
+extern void nfct_set_attr(struct nf_conntrack *ct,
+			  const enum nf_conntrack_attr type,
+			  void *value);
+
+extern void nfct_set_attr_u8(struct nf_conntrack *ct,
+			     const enum nf_conntrack_attr type,
+			     u_int8_t value);
+
+extern void nfct_set_attr_u16(struct nf_conntrack *ct,
+			      const enum nf_conntrack_attr type,
+			      u_int16_t value);
+
+extern void nfct_set_attr_u32(struct nf_conntrack *ct,
+			      const enum nf_conntrack_attr type,
+			      u_int32_t value);
+
+/* getter */
+extern void * nfct_get_attr(const struct nf_conntrack *ct,
+			    const enum nf_conntrack_attr type);
+
+extern u_int8_t nfct_get_attr_u8(const struct nf_conntrack *ct,
+				 const enum nf_conntrack_attr type);
+
+extern u_int16_t nfct_get_attr_u16(const struct nf_conntrack *ct,
+				   const enum nf_conntrack_attr type);
+
+extern u_int32_t nfct_get_attr_u32(const struct nf_conntrack *ct,
+				   const enum nf_conntrack_attr type);
+
+/* query */
+enum nf_conntrack_query {
+	NFCT_Q_CREATE,
+	NFCT_Q_UPDATE,
+	NFCT_Q_DESTROY
+};
+
+extern int nfct_query(const struct nfct_handle *h,
+		      const enum nf_conntrack_query query,
+		      const struct nf_conntrack *ct);
+
+extern int nfct_sprintf(char *buf, const struct nf_conntrack *ct);
+
+/* low level API: netlink functions */
+
+extern int nfct_nl_build_conntrack(struct nfnl_subsys_handle *ssh,
+				   struct nfnlhdr *req,
+				   size_t size,
+				   u_int16_t type,
+				   u_int16_t flags,
+				   const struct nf_conntrack *ct);
 
+extern int nfct_nl_parse_conntrack(const struct nlmsghdr *nlh, 
+				   struct nf_conntrack *ct);
 
 #endif	/* _LIBNETFILTER_CONNTRACK_H_ */
include/libnetfilter_conntrack/linux_nfnetlink_conntrack.h +3 -1
Index: libnetfilter_conntrack/include/libnetfilter_conntrack/linux_nfnetlink_conntrack.h
===================================================================
@@ -27,13 +27,15 @@ enum ctattr_type {
 	CTA_STATUS,
 	CTA_PROTOINFO,
 	CTA_HELP,
-	CTA_NAT,
+	CTA_NAT_SRC,
+#define CTA_NAT	CTA_NAT_SRC	/* backwards compatibility */
 	CTA_TIMEOUT,
 	CTA_MARK,
 	CTA_COUNTERS_ORIG,
 	CTA_COUNTERS_REPLY,
 	CTA_USE,
 	CTA_ID,
+	CTA_NAT_DST,
 	__CTA_MAX
 };
 #define CTA_MAX (__CTA_MAX - 1)
src/Makefile.am +3 -1
Index: libnetfilter_conntrack/src/Makefile.am
===================================================================
@@ -11,4 +11,6 @@ lib_LTLIBRARIES = libnetfilter_conntrack
 
 libnetfilter_conntrack_la_LDFLAGS = -Wc,-nostartfiles -lnfnetlink -ldl 	\
 				    -version-info $(LIBVERSION)
-libnetfilter_conntrack_la_SOURCES = libnetfilter_conntrack.c 
+libnetfilter_conntrack_la_SOURCES = libnetfilter_conntrack.c \
+				    api.c build.c parse.c sprint.c \
+				    setter.c getter.c
src/libnetfilter_conntrack.c +10 -0
Index: libnetfilter_conntrack/src/libnetfilter_conntrack.c
===================================================================
@@ -37,6 +37,16 @@ struct nfct_handle {
 	nfct_handler handler;		/* netlink handler */
 };
 
+struct nfnl_subsys_handle *nfct_get_nfnlssh_ct(struct nfct_handle *h)
+{
+	return h->nfnlssh_ct;
+}
+
+extern struct nfnl_handle *nfct_get_nfnl(struct nfct_handle *h)
+{
+	return h->nfnlh;
+}
+
 static char *lib_dir = LIBNETFILTER_CONNTRACK_DIR;
 static LIST_HEAD(proto_list);
 static LIST_HEAD(l3proto_list);
src/api.c new file +344 -0

344 lines not on this page · raw message

src/build.c new file +240 -0

240 lines not on this page · raw message

src/getter.c new file +179 -0

179 lines not on this page · raw message

src/setter.c new file +179 -0

179 lines not on this page · raw message

src/internal.h new file +133 -0

133 lines not on this page · raw message

src/parse.c new file +231 -0

231 lines not on this page · raw message

src/sprint.c new file +137 -0

137 lines not on this page · raw message

utils/test.c new file +58 -0

58 lines not on this page · raw message

author maintainer
#1 lore raw
Quoted 9 lines · Pablo Neira Ayuso

Hi,

Attached a patch, yet incomplete, that introduces the new API for libnetfilter_conntrack. Basically the idea consists of providing "conntrack" objects constructor/destructor functions and an interface to get and set object attributes. The conntrack object structure is encapsulated therefore we can add new attributes without breaking binary backward compatibility. There is an example of the new API use in the patch (see test.c file)

JFYI: I'm going to finish the patch and then get back to the list for discussion, perhaps I was too fast to request feedback. If someone is interested in the evolution of the proposed new API, I'll be updating the patch on my people netfilter place.

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
#2 lore raw
In reply to Pablo Neira Ayuso
Quoted 35 lines · Pablo Neira Ayuso

Hi,

Attached a patch, yet incomplete, that introduces the new API for libnetfilter_conntrack. Basically the idea consists of providing "conntrack" objects constructor/destructor functions and an interface to get and set object attributes. The conntrack object structure is encapsulated therefore we can add new attributes without breaking binary backward compatibility. There is an example of the new API use in the patch (see test.c file)

See that conntrack object attributes are different from the netlink attributes therefore they provide a high level interface that abstract from netlink details. The idea is to let the programmer make the choice to work with high level (objects) or low level (netlink) and provide the gateways to switch from high to low level and vice-versa by means of functions to build/parse netlink messages from/to conntrack objects.

The reason why I posted this patch is because I'm thinking about generalizing this a bit more and provide a complete high level API that would be the same for all the netfilter subsystems. This will result in a single library to rules them all with a subdirectory that contains the details of every specific subsystem.

nfh = nf_open(); /* encapsulate nfnl_open() */
ssh = nf_setup_subsys(nfnlh, CONNTRACK); /* encapsulate nfnl_subsys_open() */
...
ct = nf_new_object(NF_CONNTRACK_OBJECT); nf_set_attr_u8(ct, ATTR_XXX, value); nf_set_attr_u16(ct, ATTR_XXX, value);
...
nf_query_object(ssh, NF_CT_CREATE, ct); nf_free_object(ct);
nf_close(nfh);

Quoted 2 lines

I'd like to hear from you before going ahead, always appreciate your feedback guys. Comments welcome.

I like that approach. Its getting pretty close to the libnl API, have you considered integrating this in libnl?

author maintainer
#3 lore raw
Quoted 18 lines · Patrick McHardy
Quoted 17 lines · Pablo Neira Ayuso

Hi,

Attached a patch, yet incomplete, that introduces the new API for libnetfilter_conntrack. Basically the idea consists of providing "conntrack" objects constructor/destructor functions and an interface to get and set object attributes. The conntrack object structure is encapsulated therefore we can add new attributes without breaking binary backward compatibility. There is an example of the new API use in the patch (see test.c file)

See that conntrack object attributes are different from the netlink attributes therefore they provide a high level interface that abstract from netlink details. The idea is to let the programmer make the choice to work with high level (objects) or low level (netlink) and provide the gateways to switch from high to low level and vice-versa by means of functions to build/parse netlink messages from/to conntrack objects.

Quoted 4 lines
Quoted 1 line

[...]

I like that approach. Its getting pretty close to the libnl API, have you considered integrating this in libnl?

I'm going to, I'm not too familiar with libnl (just spend some hours yesterday and today) but I agree that the new API is getting close. At first sight, my impression is that most of the code of my current patch could be adapted to introduce ctnetlink support in libnl, and I don't mind about doing so in the close future.

Perhaps we can introduce some kind of dependency between libnl and libnetfilter_* (of course, providing an homogeneous API) but I'm unsure that Thomas would like that.

I agree that people could choose between different libraries depending on their preferences but at the moment I'm unsure that this would imply dropping support for our libraries.

Back to libnl, I think that the cache thing provided by the library is nice to implement quick applications without too much effort but I'm unsure that this is the place to put this, I think that the data structure used to store the entries of, say the neighbour cache, must be up to the programmer (array, hash table, tree...), although nothing prevents the programmer of not using the cache thing.

Anyway, I'm going to post the patches for libnfnetlink and libnetfilter_conntrack soon, please let me make peace and apply them since I do need them in mainline as well as many other people, we can go back to this discussion later once I get more familiarized with libnl.

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
#4 lore raw
Quoted 12 lines · Pablo Neira Ayuso
Quoted 2 lines · Patrick McHardy

I like that approach. Its getting pretty close to the libnl API, have you considered integrating this in libnl?

I'm going to, I'm not too familiar with libnl (just spend some hours yesterday and today) but I agree that the new API is getting close. At first sight, my impression is that most of the code of my current patch could be adapted to introduce ctnetlink support in libnl, and I don't mind about doing so in the close future.

Quoted 3 lines

Perhaps we can introduce some kind of dependency between libnl and libnetfilter_* (of course, providing an homogeneous API) but I'm unsure that Thomas would like that.

I don't think thats necessary, libnl includes all the infrastrucure needed to handle nfnetlink.

Quoted 10 lines

I agree that people could choose between different libraries depending on their preferences but at the moment I'm unsure that this would imply dropping support for our libraries.

Back to libnl, I think that the cache thing provided by the library is nice to implement quick applications without too much effort but I'm unsure that this is the place to put this, I think that the data structure used to store the entries of, say the neighbour cache, must be up to the programmer (array, hash table, tree...), although nothing prevents the programmer of not using the cache thing.

Agreed. I have a patch that replaces the linked list caches by hashes, but it has some drawbacks since not all objects are suitable for hashing without making expectations about their use. It shouldn't be hard to use your own scheme, I think the only thing that prevents it right now is that the structure is hidden from API users.

Quoted 4 lines

Anyway, I'm going to post the patches for libnfnetlink and libnetfilter_conntrack soon, please let me make peace and apply them since I do need them in mainline as well as many other people, we can go back to this discussion later once I get more familiarized with libnl.

Sure. Harald is renewing your keys, so you can simply apply it yourself.

#5 lore raw
In reply to Pablo Neira Ayuso
Quoted 9 lines · Pablo Neira Ayuso

Hi,

Attached a patch, yet incomplete, that introduces the new API for libnetfilter_conntrack. Basically the idea consists of providing "conntrack" objects constructor/destructor functions and an interface to get and set object attributes. The conntrack object structure is encapsulated therefore we can add new attributes without breaking binary backward compatibility. There is an example of the new API use in the patch (see test.c file)

I like that API and I think it's the right way to go.

Quoted 3 lines

The reason why I posted this patch is because I'm thinking about generalizing this a bit more and provide a complete high level API that would be the same for all the netfilter subsystems.

Makes sense.

Quoted 2 lines

This will result in a single library to rules them all with a subdirectory that contains the details of every specific subsystem.

I disagree with that proposition. I don't really understand why completely independent netfilter subsystems should share a high-level userspace library. Every time some obscure bug in libnetfilter_log happens, libnetfilter_conntrack will be affected, too.

This is especially difficult if meanwhile you have commited not-yet-completely-stable code for one subsystem, and then need to make a bugfix releease of the other (svn branching is an option, but it all sounds a bit too complex for my taste).

One option would be implement the subsystem specific parts as plugins and to dlopen() them at runtime, providing a way of updating only individual subystem-plugins

Signature · 5 lines
- Harald Welte <[email protected]>                 http://netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie

A message that is not in the archive (sent off-list or not yet received): 20061107192458.GJ4423@(none)

author maintainer
#7 lore raw
Quoted 3 lines · Harald Welte

One option would be implement the subsystem specific parts as plugins and to dlopen() them at runtime, providing a way of updating only individual subystem-plugins

I like this option

Signature · 1 line
The dawn of the fourth age of Linux firewalling is coming; a time of great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
author maintainer
#8 lore raw
In reply to a message not in the archive
Quoted 8 lines · Harald Welte
Quoted 2 lines

This will result in a single library to rules them all with a subdirectory that contains the details of every specific subsystem.

I disagree with that proposition. I don't really understand why completely independent netfilter subsystems should share a high-level userspace library. Every time some obscure bug in libnetfilter_log happens, libnetfilter_conntrack will be affected, too.

Quoted 4 lines

This is especially difficult if meanwhile you have commited not-yet-completely-stable code for one subsystem, and then need to make a bugfix releease of the other (svn branching is an option, but it all sounds a bit too complex for my taste).

Yes, this is a tricky issue, although the libraries are relatively small in terms of code and the impact of such problem could be reduced if we do frequent releases.

Quoted 3 lines

One option would be implement the subsystem specific parts as plugins and to dlopen() them at runtime, providing a way of updating only individual subystem-plugins

As I posted days ago, this is an alternative that I like, such dlopen() would require a versioning mechanism, the only concerned here is about getting people confused with this modular view.

Signature · 1 line
The dawn of the fourth age of Linux firewalling is coming; a time of great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris