message parsing functions for nfqueue
PATCH 1/1 message parsing functions for nfqueue
Hi,
This is a patch against libnfnetlink_queue.
It adds "high" level functions to access to the data contained in a nfqueue message. These are mostly encapsulation of NFA macros.
The exemple provided in the utils directory now uses these functions. I've also modified NuFW to use the new API to test its usability. The obtained code is clean and the port was easy, thus I consider it improves the situation. Although, it may miss some higher level functions to access to multiple fields at the same time. I've thought about some but they may be too NuFW oriented.
Feedbacks welcome.
BR,
Signature · 2 lines
Eric Leblond <[email protected]> NuFW, Now User Filtering Works : http://www.nufw.org
nfqnl_test.c +20 -18
@@ -11,35 +11,37 @@static u_int32_t print_pkt (struct nfattr *tb[]) {int id =0 ; - -if (tb[NFQA_PACKET_HDR -1 ]) { -struct nfqnl_msg_packet_hdr *ph = -NFA_DATA (tb[NFQA_PACKET_HDR -1 ]); +struct nfqnl_msg_packet_hdr *ph; +u_int32_t mark,ifi; +int ret; +unsigned int datalength; +char * data; + + ph =nfqnl_get_msg_packet_hdr (tb); +if (ph){ id =ntohl (ph->packet_id);printf ("hw_protocol=0x%04x hook=%u id=%u " ,ntohs (ph->hw_protocol), ph->hook, id); } - -if (tb[NFQA_MARK -1 ]) { -u_int32_t mark = -ntohl (*(u_int32_t *)NFA_DATA (tb[NFQA_MARK -1 ])); + + mark=nfqnl_get_nfmark (tb); +if (mark) {printf ("mark=%u " , mark); } -if (tb[NFQA_IFINDEX_INDEV -1 ]) { -u_int32_t ifi = -ntohl (*(u_int32_t *)NFA_DATA (tb[NFQA_IFINDEX_INDEV -1 ])); -printf ("indev=%u " , ifi); + ret=nfqnl_get_indev (tb,&ifi,NULL ); +if (ret) { +printf ("indev=%u " ,ntohl (ifi)); } -if (tb[NFQA_IFINDEX_OUTDEV -1 ]) { -u_int32_t ifi = -ntohl (*(u_int32_t *)NFA_DATA (tb[NFQA_IFINDEX_OUTDEV -1 ])); -printf ("outdev=%u " , ifi); + ret=nfqnl_get_outdev (tb,&ifi,NULL ); +if (ret) { +printf ("outdev=%u " ,ntohl (ifi)); } -if (tb[NFQA_PAYLOAD -1 ]) { -printf ("payload_len=%d " ,NFA_PAYLOAD (tb[NFQA_PAYLOAD -1 ])); + ret=nfqnl_get_payload (tb,&data,&datalength); +if (ret) { +printf ("payload_len=%d " , datalength); }fputc ('\n' , stdout);
libnfnetlink_queue/libnfnetlink_queue.h +28 -1
@@ -2,6 +2,10 @@* * (C) 2005 by Harald Welte <[email protected]> * +* +* Changelog : +* (2005/08/11) added parsing function (Eric Leblond <[email protected]>) +* * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. */
@@ -21,7 +25,7 @@extern struct nfnl_handle *nfqnl_nfnlh (struct nfqnl_handle *h);extern int nfqnl_fd (struct nfqnl_handle *h); -typedef nfqnl_callback(struct nfqnl_q_handle *gh, struct nfgenmsg *nfmsg, +typedef int nfqnl_callback (struct nfqnl_q_handle *gh,struct nfgenmsg *nfmsg,struct nfattr *nfa[],void *data);
@@ -47,10 +51,33 @@u_int32_t verdict, u_int32_t data_len, unsigned char *buf); +extern int nfqnl_set_verdict_mark (struct nfqnl_q_handle *qh,u_int32_t id,u_int32_t verdict,u_int32_t mark,u_int32_t datalen,unsigned char *buf); + +/* message parsing function */ + +extern struct nfqnl_msg_packet_hdr *nfqnl_get_msg_packet_hdr (struct nfattr *nfa[]); + +extern u_int32_t nfqnl_get_nfmark (struct nfattr *nfa[]); + +extern struct nfqnl_msg_packet_timestamp *nfqnl_get_timestamp (struct nfattr *nfa[]); + +/* return 0 if not set */ +extern int nfqnl_get_indev (struct nfattr *nfa[],u_int32_t * indev,u_int32_t * physindev); + +/* return 0 if not set */ +extern int nfqnl_get_outdev (struct nfattr *nfa[],u_int32_t * outdev,u_int32_t * physoutdev); + +extern struct nfqnl_msg_packet_hw *nfqnl_get_packet_hw (struct nfattr *nfa[]); + +/* return 0 if problem */ +extern int nfqnl_get_payload (struct nfattr *nfa[],char ** data,unsigned int * datalen); + + +#endif /* __LIBNFQNETLINK_H */
libnfnetlink_queue.c +102 -19
190 lines not on this page · raw message
Makefile.am +1 -1
@@ -2,7 +2,7 @@ #EXTRA_DIST = $(man_MANS) acinclude.m4 -INCLUDES = $(all_includes) -I$(top_srcdir)/include -I${KERNELDIR} +INCLUDES = $(all_includes) -I$(top_srcdir)/include -I${KERNELDIR} AM_CFLAGS=-fPIC -Wall LIBS=
Quoted 3 lines · Eric Leblond
Hi,
This is a patch against
libnfnetlink_queue.
At the first sight, the patch looks fine to me. I'll look at it in detail over the weekend.
Oh. One thing that comes to my mind is "get_{in,out}dev" don't return a device [name], but ifindex. that should be reflected in the names.
Also, I think we should implement some common ifindex<->devicename mapping/resolval code. This could be part of libnfnetlink (but be put into a separate source code file).
IMHO, all required information can be accessed via nfnetlink. I've once written some code for caching the ifindex<->devicename in userspace (it receives rtnetlink events of ifup/ifdown messages and keeps the cache in sync). I'll try to locate that code.
If you (or anyone else) is interested in cleanly integrating it into libnfnetlink, please contact me and I'll give you that code.
It has to be common, since other nfnetlink users also need the same information. masq_index in the conntrack structure, e.g.
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
[added Thomas Graf to Cc list, please see bottom of this mail]
Quoted 11 lines · Harald Welte
Quoted 3 lines · Eric Leblond
Hi,
This is a patch against
libnfnetlink_queue.At the first sight, the patch looks fine to me. I'll look at it in detail over the weekend.
Oh. One thing that comes to my mind is "get_{in,out}dev" don't return a device [name], but ifindex. that should be reflected in the names.
Quoted 8 lines
Also, I think we should implement some common ifindex<->devicename mapping/resolval code. This could be part of libnfnetlink (but be put into a separate source code file).
IMHO, all required information can be accessed via netlink. I've once written some code for caching the ifindex<->devicename in userspace (it receives rtnetlink events of ifup/ifdown messages and keeps the cache in sync). I'll try to locate that code.
Ok, located it (and got permission to put it under GPL). It's now in
https://svn.netfilter.org/netfilter/trunk/libnfnetlink/src/{rtnl,iftable}.{c,h}
I'm not going to have the time to work on this before I'll get back from my holidays on Aug 26. So whoever wants to dig into this, feel free to go ahead.
One problem, though, is how we will integrate that functionality with the rest of the application. In order to receive the ifup/ifdown/... events by rtnetlink, the main application needs to select() on yet another filedescriptor - and call some rtnl message handling function.
One idea was ot skip the event messages and only poll the kernel every time the application requests it. Somthing like
iftable_cache_fill(void)
- read out a list of ifindex<->devicename mappingsiftable_cache_lookup(int ifindex)
- lookup devname for given ifindexiftable_lookup(int ifindex)
- lookup directly, bypassing the cache.
This time the app would have to call iftable_cache_fill() every so often. It's not very elegant.
In the end, this really belongs into yet-another netlink-related library. Something like librtnetlink. Maybe Thomas can help us if there is something that we could use.
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
Pablo Neira
maintainerQuoted 11 lines · Eric Leblond
This is a patch against
libnfnetlink_queue.It adds "high" level functions to access to the data contained in a nfqueue message. These are mostly encapsulation of NFA macros.
nfqnl_test.c +2
-4- -if (tb[NFQA_PACKET_HDR -1 ]) { -struct nfqnl_msg_packet_hdr *ph = -NFA_DATA (tb[NFQA_PACKET_HDR -1 ]); +struct nfqnl_msg_packet_hdr *ph; +u_int32_t mark,ifi;
nfqnl_test.c line 25
Quoted 10 lines
nfqnl_test.c +6
-0+int ret; +unsigned int datalength; +char * data; + + ph =nfqnl_get_msg_packet_hdr (tb); +if (ph){ id =ntohl (ph->packet_id);printf ("hw_protocol=0x%04x hook=%u id=%u " ,ntohs (ph->hw_protocol), ph->hook, id); }
Instead of adding a function to access every attribute like above, why don't we add something more generic in libnfnetlink? Like the following macros:
#define nfnl_get_data(tb, attr, type) \
({ type __ret = 0; \
if (tb[attr-1]) \
__ret = *(type *)NFA_DATA(tb[attr-1]); \ __ret; \ })
#define nfnl_get_pointer_to_data(tb, attr, type) \
({ type *__ret = NULL; \
if (tb[attr-1]) \
__ret = NFA_DATA(tb[attr-1]); \
__ret; \
})
Maybe too generic ?
OTOH, if we add such high level functions we'll have to do the same in other libnfnetlink_* libraries to keep homogeneity of the API.
--
Pablo
Quoted 11 lines
Quoted 4 lines · Eric Leblond
This is a patch against
libnfnetlink_queue.It adds "high" level functions to access to the data contained in a nfqueue message. These are mostly encapsulation of NFA macros.
Instead of adding a function to access every attribute like above, why don't we add something more generic in libnfnetlink? Like the following macros:
Maybe too generic ?
In a way, yes, you will only access to things one at a time, and when you want the payload you also want to know its size.
But I think such functions are needed to be able to have an evolving API. I will try to work on it.
Quoted 2 lines
OTOH, if we add such high level functions we'll have to do the same in other libnfnetlink_* libraries to keep homogeneity of the API.
yes ...
Quoted 10 lines · Eric Leblond
Quoted 10 lines
Quoted 4 lines · Eric Leblond
This is a patch against
libnfnetlink_queue.It adds "high" level functions to access to the data contained in a nfqueue message. These are mostly encapsulation of NFA macros.
Instead of adding a function to access every attribute like above, why don't we add something more generic in libnfnetlink? Like the following macros:
Quoted 6 lines
Quoted 1 line
Maybe too generic ?
In a way, yes, you will only access to things one at a time, and when you want the payload you also want to know its size.
But I think such functions are needed to be able to have an evolving API. I will try to work on it.
I like the generic macros proposed by Pablo, but making nfnetlink_queue users directly use them is somewhat of a layering violation.
The application shouldn't have to understand anything about netlink, nfattrs, the nfnetlink wire format or anything else. It's the job of the API to hide all those details. Ideally we don't even export any of that information to the application, by putting it in private header files.
Only this way we can make sure that if we'd ever have to change the underlying mechanism [again], the applications wouldn't be affected - they'd just link against a different libnfnetlink_queue. This is not too unlikely. There were discussions about netlink2 as well as having mmap() capability of netlink sockets. Obviously nfnetlink_queue would want to take advantage of that...
Quoted 4 lines
Quoted 2 lines
OTOH, if we add such high level functions we'll have to do the same in other libnfnetlink_* libraries to keep homogeneity of the API.
yes ...
I'm not actually too worried about homogenous API between all the different libnfnetlink_* variants. It's just _queue and _log which are similar enough in functionality so that the API's would look very similar. But think about management of conntrack tables or even pkt_tables at some point... they serve a totally different purpose, and the API will look different.
One thing that somebody could look into (if there are any volunteers) is thread-safety of libnfnetlink{,_queue,_log}. There will be users who want to use multithreaded programs, once this takes off.
I've personally never believed in multithreading anyway (I'm more a select/poll kind of guy), so I'm certainly not the best person to get those libs threadsafe...
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
Hi,
Quoted 9 lines · Harald Welte
Quoted 5 lines · Eric Leblond
Quoted 5 lines
Quoted 4 lines · Eric Leblond
This is a patch against
libnfnetlink_queue.It adds "high" level functions to access to the data contained in a nfqueue message. These are mostly encapsulation of NFA macros.
I like the generic macros proposed by Pablo, but making
nfnetlink_queueusers directly use them is somewhat of a layering violation.
I've finally found some time to rewrite the provided functions with Pablo's macros.
I've tested it against provided exemple and NuFW and it seems to work fine.
BR,
Signature · 2 lines
Eric Leblond <[email protected]> INL
nfqnl_test.c +20 -18
@@ -11,35 +11,37 @@static u_int32_t print_pkt (struct nfattr *tb[]) {int id =0 ; - -if (tb[NFQA_PACKET_HDR -1 ]) { -struct nfqnl_msg_packet_hdr *ph = -NFA_DATA (tb[NFQA_PACKET_HDR -1 ]); +struct nfqnl_msg_packet_hdr *ph; +u_int32_t mark,ifi; +int ret; +unsigned int datalength; +char * data; + + ph =nfqnl_get_msg_packet_hdr (tb); +if (ph){ id =ntohl (ph->packet_id);printf ("hw_protocol=0x%04x hook=%u id=%u " ,ntohs (ph->hw_protocol), ph->hook, id); } - -if (tb[NFQA_MARK -1 ]) { -u_int32_t mark = -ntohl (*(u_int32_t *)NFA_DATA (tb[NFQA_MARK -1 ])); + + mark=nfqnl_get_nfmark (tb); +if (mark) {printf ("mark=%u " , mark); } -if (tb[NFQA_IFINDEX_INDEV -1 ]) { -u_int32_t ifi = -ntohl (*(u_int32_t *)NFA_DATA (tb[NFQA_IFINDEX_INDEV -1 ])); -printf ("indev=%u " , ifi); + ret=nfqnl_get_indev (tb,&ifi,NULL ); +if (ret) { +printf ("indev=%u " ,ntohl (ifi)); } -if (tb[NFQA_IFINDEX_OUTDEV -1 ]) { -u_int32_t ifi = -ntohl (*(u_int32_t *)NFA_DATA (tb[NFQA_IFINDEX_OUTDEV -1 ])); -printf ("outdev=%u " , ifi); + ret=nfqnl_get_outdev (tb,&ifi,NULL ); +if (ret) { +printf ("outdev=%u " ,ntohl (ifi)); } -if (tb[NFQA_PAYLOAD -1 ]) { -printf ("payload_len=%d " ,NFA_PAYLOAD (tb[NFQA_PAYLOAD -1 ])); + ret=nfqnl_get_payload (tb,&data,&datalength); +if (ret) { +printf ("payload_len=%d " , datalength); }fputc ('\n' , stdout);
libnfnetlink_queue/libnfnetlink_queue.h +28 -1
@@ -2,6 +2,10 @@* * (C) 2005 by Harald Welte <[email protected]> * +* +* Changelog : +* (2005/08/11) added parsing function (Eric Leblond <[email protected]>) +* * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. */
@@ -21,7 +25,7 @@extern struct nfnl_handle *nfqnl_nfnlh (struct nfqnl_handle *h);extern int nfqnl_fd (struct nfqnl_handle *h); -typedef nfqnl_callback(struct nfqnl_q_handle *gh, struct nfgenmsg *nfmsg, +typedef int nfqnl_callback (struct nfqnl_q_handle *gh,struct nfgenmsg *nfmsg,struct nfattr *nfa[],void *data);
@@ -47,10 +51,33 @@u_int32_t verdict, u_int32_t data_len, unsigned char *buf); +extern int nfqnl_set_verdict_mark (struct nfqnl_q_handle *qh,u_int32_t id,u_int32_t verdict,u_int32_t mark,u_int32_t datalen,unsigned char *buf); + +/* message parsing function */ + +extern struct nfqnl_msg_packet_hdr *nfqnl_get_msg_packet_hdr (struct nfattr *nfa[]); + +extern u_int32_t nfqnl_get_nfmark (struct nfattr *nfa[]); + +extern struct nfqnl_msg_packet_timestamp *nfqnl_get_timestamp (struct nfattr *nfa[]); + +/* return 0 if not set */ +extern int nfqnl_get_indev (struct nfattr *nfa[],u_int32_t * indev,u_int32_t * physindev); + +/* return 0 if not set */ +extern int nfqnl_get_outdev (struct nfattr *nfa[],u_int32_t * outdev,u_int32_t * physoutdev); + +extern struct nfqnl_msg_packet_hw *nfqnl_get_packet_hw (struct nfattr *nfa[]); + +/* return 0 if problem */ +extern int nfqnl_get_payload (struct nfattr *nfa[],char ** data,unsigned int * datalen); + + +#endif /* __LIBNFQNETLINK_H */
libnfnetlink_queue.c +91 -19
179 lines not on this page · raw message
Makefile.am +1 -1
@@ -2,7 +2,7 @@ #EXTRA_DIST = $(man_MANS) acinclude.m4 -INCLUDES = $(all_includes) -I$(top_srcdir)/include -I${KERNELDIR} +INCLUDES = $(all_includes) -I$(top_srcdir)/include -I${KERNELDIR} AM_CFLAGS=-fPIC -Wall LIBS=