Specify nf_queue max length from userspace
Subsystems (2)
cover Specify nf_queue max length from userspace
Hi,
With libnetfilter_queue it was not anymore possible to specify the max queue length from userspace. All needed framework was already in nfnetlink_queue code and as queue instance has its own max length, it was thus possible to specify max length on a per queue basis.
To be able to do so, I've added a new function nfq_set_queue_maxlen to libnetfilter_queue and done needed modifications in nfnetlink_queue code.
BR,
Signature · 2 lines
Eric Leblond <[email protected]> INL
PATCH 1/2 Specify nf_queue max length from userspace
Attach patch is against current git kernel tree.
Signature · 2 lines
Eric Leblond <[email protected]> INL
---
include/linux/netfilter/nfnetlink_queue.h | 2 ++ net/netfilter/nfnetlink_queue.c | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletions(-)
1fb988dc51e2465500df50ff4e085f80acb7b6a3
include/linux/netfilter/nfnetlink_queue.h +2 -0
@@ -67,6 +67,7 @@ struct nfqnl_msg_config_cmd { } __attribute__ ((packed));enum nfqnl_config_mode { +NFQNL_COPY_UNSPEC ,NFQNL_COPY_NONE ,NFQNL_COPY_META ,NFQNL_COPY_PACKET ,
@@ -75,6 +76,7 @@ enum nfqnl_config_mode {struct nfqnl_msg_config_params {u_int32_t copy_range;u_int8_t copy_mode;/* enum nfqnl_config_mode */ +u_int32_t queue_maxlen; } __attribute__ ((packed));
net/netfilter/nfnetlink_queue.c +6 -1
@@ -935,8 +935,13 @@ nfqnl_recv_config(struct sock *ctnl, str goto out_put; } params =NFA_DATA (nfqa[NFQA_CFG_PARAMS -1 ]); -nfqnl_set_mode (queue, params->copy_mode, +if (params->copy_mode !=NFQNL_COPY_UNSPEC ){ +nfqnl_set_mode (queue, params->copy_mode,ntohl (params->copy_range)); + } +if (params->queue_maxlen) { + queue->queue_maxlen=ntohl (params->queue_maxlen); + } } out_put:
Signature · 1 line
1.3.3
Pablo Neira Ayuso
maintainerHi Eric,
Quoted 10 lines · Eric Leblond
include/linux/netfilter/nfnetlink_queue.h | 2 ++ net/netfilter/nfnetlink_queue.c | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletions(-)
1fb988dc51e2465500df50ff4e085f80acb7b6a3include/linux/netfilter/nfnetlink_queue.h +0
-0
include/linux/netfilter/nfnetlink_queue.h struct nfqnl_msg_config_cmd { · line 73
Quoted 7 lines
include/linux/netfilter/nfnetlink_queue.h +1
-0@@ -67,6 +67,7 @@ struct nfqnl_msg_config_cmd { } __attribute__ ((packed));enum nfqnl_config_mode { +NFQNL_COPY_UNSPEC ,NFQNL_COPY_NONE ,NFQNL_COPY_META ,NFQNL_COPY_PACKET ,
If you have to add new attributes, do it always at the end, in other words, just before NFQNL_COPY_MAX. Otherwise you're breaking backward compatibility. See that old binaries use COPY_NONE set to 0, but after applying your patch COPY_NONE is set to 1. Result: old binaries will no work with nfnetlink_queue anymore.
include/linux/netfilter/nfnetlink_queue.h enum nfqnl_config_mode { · line 80
Quoted 6 lines
include/linux/netfilter/nfnetlink_queue.h +1
-0@@ -75,6 +76,7 @@ enum nfqnl_config_mode {struct nfqnl_msg_config_params {u_int32_t copy_range;u_int8_t copy_mode;/* enum nfqnl_config_mode */ +u_int32_t queue_maxlen; } __attribute__ ((packed));
Same thing here. If you have to modify this structure, you need to create a new one called struct nfqnl_msg_config_params2. Since I don't like this option, I think that the best solution is to add a new attribute called NFQNL_CFG_QUEUE_MAXLEN.
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
Hi Pablo,
Quoted 12 lines · Pablo Neira Ayuso
Hi Eric,
include/linux/netfilter/nfnetlink_queue.h struct nfqnl_msg_config_cmd { · line 73
Quoted 5 lines
enum
nfqnl_config_mode{include/linux/netfilter/nfnetlink_queue.h +1
-0+NFQNL_COPY_UNSPEC ,NFQNL_COPY_NONE ,NFQNL_COPY_META , NFQNL_COPY_PACKET,If you have to add new attributes, do it always at the end, in other words, just before
NFQNL_COPY_MAX. Otherwise you're breaking backward compatibility. See that old binaries useCOPY_NONEset to 0, but after applying your patchCOPY_NONEis set to 1. Result: old binaries will no work withnfnetlink_queueanymore.
I agree with you on this point. But, as I know that second point was also breaking compatibility, I've made the choice to put less significant value first. It looks cleaner to me.
Quoted 11 lines
include/linux/netfilter/nfnetlink_queue.h enum nfqnl_config_mode { · line 80
Quoted 6 lines
include/linux/netfilter/nfnetlink_queue.h +1
-0@@ -75,6 +76,7 @@ enum nfqnl_config_mode {struct nfqnl_msg_config_params {u_int32_t copy_range;u_int8_t copy_mode;/* enum nfqnl_config_mode */ +u_int32_t queue_maxlen; } __attribute__ ((packed));Same thing here. If you have to modify this structure, you need to create a new one called struct
nfqnl_msg_config_params2. Since I don't like this option, I think that the best solution is to add a new attribute calledNFQNL_CFG_QUEUE_MAXLEN.
I also think this is the cleanest way not to loose binary compatibility. But, as you notice it, it's ugly as queue max length is a param. The user-basis of libnetfilter_queue is really small for the moment: Only about two projects use it. There may be also some unpublished project but it should hurt too much to break binary compatibility to have a cleaner code.
In fact, I think that having only one attribute and one structure could be used in the future to have a function that will be able to set copy mode and queue length in one call and one message.
BR,
Signature · 2 lines
Eric Leblond <[email protected]> INL
Pablo Neira Ayuso
maintainerQuoted 30 lines · Eric Leblond
Hi Pablo,
Quoted 9 lines · Pablo Neira Ayuso
Hi Eric,
Quoted 5 lines
enum
nfqnl_config_mode{include/linux/netfilter/nfnetlink_queue.h +1
-0+ NFQNL_COPY_UNSPEC,
NFQNL_COPY_NONE,
NFQNL_COPY_META,
NFQNL_COPY_PACKET,If you have to add new attributes, do it always at the end, in other words, just before
NFQNL_COPY_MAX. Otherwise you're breaking backward compatibility. See that old binaries useCOPY_NONEset to 0, but after applying your patchCOPY_NONEis set to 1. Result: old binaries will no work withnfnetlink_queueanymore.I agree with you on this point. But, as I know that second point was also breaking compatibility, I've made the choice to put less significant value first. It looks cleaner to me.
Quoted 8 lines
include/linux/netfilter/nfnetlink_queue.h enum nfqnl_config_mode { · line 80
Quoted 6 lines
include/linux/netfilter/nfnetlink_queue.h +1
-0@@ -75,6 +76,7 @@ enum nfqnl_config_mode {struct nfqnl_msg_config_params {u_int32_t copy_range;u_int8_t copy_mode;/* enum nfqnl_config_mode */ +u_int32_t queue_maxlen; } __attribute__ ((packed));Same thing here. If you have to modify this structure, you need to create a new one called struct
nfqnl_msg_config_params2. Since I don't like this option, I think that the best solution is to add a new attribute calledNFQNL_CFG_QUEUE_MAXLEN.
Quoted 6 lines
I also think this is the cleanest way not to loose binary compatibility. But, as you notice it, it's ugly as queue max length is a param. The user-basis of
libnetfilter_queueis really small for the moment: Only about two projects use it. There may be also some unpublished project but it should hurt too much to break binary compatibility to have a cleaner code.
Actually, this is what happen when you attach kernel and userspace to a certain structure. Instead, I think that it is better to split structures into fields.
Quoted 3 lines
In fact, I think that having only one attribute and one structure could be used in the future to have a function that will be able to set copy mode and queue length in one call and one message.
This is not true, you can still implement a function that sets the queue length and copy mode with the solution that I proposed.
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
Quoted 21 lines · Eric Leblond
Attach patch is against current git kernel tree.
------------------------------------------------------------------------
Signed-off-by Eric Leblond <[email protected]>---
include/linux/netfilter/nfnetlink_queue.h | 2 ++ net/netfilter/nfnetlink_queue.c | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletions(-)
1fb988dc51e2465500df50ff4e085f80acb7b6a3include/linux/netfilter/nfnetlink_queue.h +0
-0
include/linux/netfilter/nfnetlink_queue.h struct nfqnl_msg_config_cmd { · line 73
Quoted 7 lines
include/linux/netfilter/nfnetlink_queue.h +1
-0@@ -67,6 +67,7 @@ struct nfqnl_msg_config_cmd { } __attribute__ ((packed));enum nfqnl_config_mode { +NFQNL_COPY_UNSPEC ,NFQNL_COPY_NONE ,NFQNL_COPY_META ,NFQNL_COPY_PACKET ,
Looks like an ABI breaker.
include/linux/netfilter/nfnetlink_queue.h enum nfqnl_config_mode { · line 80
Quoted 6 lines
include/linux/netfilter/nfnetlink_queue.h +1
-0@@ -75,6 +76,7 @@ enum nfqnl_config_mode {struct nfqnl_msg_config_params {u_int32_t copy_range;u_int8_t copy_mode;/* enum nfqnl_config_mode */ +u_int32_t queue_maxlen; } __attribute__ ((packed));
This too. Why not just use a new attribute?
Hi,
Quoted 8 lines
Review enum nfqnl_config_mode { · line 80
Quoted 6 lines
(quoted diff) +1
-0@@ -75,6 +76,7 @@ enum nfqnl_config_mode { struct nfqnl_msg_config_params { u_int32_t copy_range; u_int8_t copy_mode; /* enum nfqnl_config_mode */ + u_int32_t queue_maxlen; } __attribute__ ((packed));This too. Why not just use a new attribute?
I did not like the idea of having params split between two structures. And as the user base is still small, I thought we may break ABI to have something cleaner.
Pablo and you do not like the idea, I wrote a patch that should not break the ABI (attached to the mail).
In fact, after introducing the new attribute, I just found it is in a way cleaner than my initial one because code does not depend from a structure.
BR,
Signature · 2 lines
Eric Leblond <[email protected]> INL
------------------------------------------------------------------------
---
include/linux/netfilter/nfnetlink_queue.h +1 -0
@@ -82,6 +82,7 @@ enum nfqnl_attr_config {NFQA_CFG_UNSPEC ,NFQA_CFG_CMD ,/* nfqnl_msg_config_cmd */ NFQA_CFG_PARAMS ,/* nfqnl_msg_config_params */ +NFQA_CFG_QUEUE_MAXLEN ,/* u_int32_t */ __NFQA_CFG_MAX };#define NFQA_CFG_MAX (__NFQA_CFG_MAX-1)
net/netfilter/nfnetlink_queue.c +8 -0
@@ -939,6 +939,14 @@ nfqnl_recv_config(struct sock *ctnl, str ntohl(params->copy_range)); } +if (nfqa[NFQA_CFG_QUEUE_MAXLEN -1 ]) { +u_int32_t *queue_maxlen; + queue_maxlen =NFA_DATA (nfqa[NFQA_CFG_QUEUE_MAXLEN -1 ]); +spin_lock_bh (&queue->lock); + queue->queue_maxlen =ntohl (*queue_maxlen); +spin_unlock_bh (&queue->lock); + } + out_put:instance_put (queue);return ret;
Quoted 8 lines · Eric Leblond
I did not like the idea of having params split between two structures. And as the user base is still small, I thought we may break ABI to have something cleaner.
Pablo and you do not like the idea, I wrote a patch that should not break the ABI (attached to the mail).
In fact, after introducing the new attribute, I just found it is in a way cleaner than my initial one because code does not depend from a structure.
Yes, that is better. I still would prefer to have all configuration attributes nested under some common attribute .. but I can't see a clean way to do that right now. Let me think about this some more.
PATCH 2/2 Specify nf_queue max length from userspace
In reply to Patrick McHardyHi,
Please found attached to this mail a libnetfilter_queue patch related to my previous mail and patch.
BR,
Quoted 40 lines · Patrick McHardy
Quoted 21 lines · Eric Leblond
Attach patch is against current git kernel tree.
------------------------------------------------------------------------
Signed-off-by Eric Leblond <[email protected]>---
include/linux/netfilter/nfnetlink_queue.h | 2 ++ net/netfilter/nfnetlink_queue.c | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletions(-)
1fb988dc51e2465500df50ff4e085f80acb7b6a3include/linux/netfilter/nfnetlink_queue.h +0
-0include/linux/netfilter/nfnetlink_queue.h struct nfqnl_msg_config_cmd { · line 73
Quoted 7 lines
include/linux/netfilter/nfnetlink_queue.h +1
-0@@ -67,6 +67,7 @@ struct nfqnl_msg_config_cmd { } __attribute__ ((packed));enum nfqnl_config_mode { +NFQNL_COPY_UNSPEC ,NFQNL_COPY_NONE ,NFQNL_COPY_META ,NFQNL_COPY_PACKET ,Looks like an ABI breaker.
include/linux/netfilter/nfnetlink_queue.h enum nfqnl_config_mode { · line 80
Quoted 6 lines
include/linux/netfilter/nfnetlink_queue.h +1
-0@@ -75,6 +76,7 @@ enum nfqnl_config_mode {struct nfqnl_msg_config_params {u_int32_t copy_range;u_int8_t copy_mode;/* enum nfqnl_config_mode */ +u_int32_t queue_maxlen; } __attribute__ ((packed));This too. Why not just use a new attribute?
Signature · 2 lines
Eric Leblond <[email protected]> INL
------------------------------------------------------------------------
---
libnetfilter_queue/linux_nfnetlink_queue.h +4 -3
@@ -1,13 +1,13 @@#ifndef _NFNETLINK_QUEUE_H#define _NFNETLINK_QUEUE_H -#include <linux/types.h> -#include <libnfnetlink/linux_nfnetlink.h> -#ifndef aligned_u64#define aligned_u64 unsigned long long __attribute__((aligned(8)))#endif +#include <linux/types.h> +#include <libnfnetlink/linux_nfnetlink.h> +enum nfqnl_msg_types {NFQNL_MSG_PACKET ,/* packet from kernel to userspace */ NFQNL_MSG_VERDICT, /* verdict from userspace to kernel */
@@ -86,6 +86,7 @@NFQA_CFG_UNSPEC ,NFQA_CFG_CMD ,/* nfqnl_msg_config_cmd */ NFQA_CFG_PARAMS ,/* nfqnl_msg_config_params */ +NFQA_CFG_QUEUE_MAXLEN ,/* u_int32_t */ __NFQA_CFG_MAX };#define NFQA_CFG_MAX (__NFQA_CFG_MAX-1)
libnetfilter_queue/libnetfilter_queue.h +3 -0
@@ -49,6 +49,9 @@extern int nfq_set_mode (struct nfq_q_handle *qh,u_int8_t mode,unsigned int len); +int nfq_set_queue_maxlen (struct nfq_q_handle *qh, +u_int32_t queuelen); + extern int nfq_set_verdict(struct nfq_q_handle *qh, u_int32_t id, u_int32_t verdict,
libnetfilter_queue.c +17 -0
@@ -288,6 +288,23 @@return nfnl_talk (qh->h->nfnlh, nmh,0 ,0 ,NULL ,NULL ,NULL ); } +int nfq_set_queue_maxlen (struct nfq_q_handle *qh, +u_int32_t queuelen) +{ +char buf[NFNL_HEADER_LEN + +NFA_LENGTH (sizeof (struct nfqnl_msg_config_params ))]; +u_int32_t queue_maxlen =htonl (queuelen); +struct nlmsghdr *nmh = (struct nlmsghdr *) buf; + +nfnl_fill_hdr (qh->h->nfnlssh, nmh,0 ,AF_UNSPEC , qh->id, +NFQNL_MSG_CONFIG ,NLM_F_REQUEST |NLM_F_ACK ); + +nfnl_addattr_l (nmh,sizeof (buf),NFQA_CFG_QUEUE_MAXLEN , &queue_maxlen, +sizeof (queue_maxlen)); + +return nfnl_talk (qh->h->nfnlh, nmh,0 ,0 ,NULL ,NULL ,NULL ); +} +static int __set_verdict (struct nfq_q_handle *qh,u_int32_t id,u_int32_t verdict,u_int32_t mark,int set_mark,u_int32_t data_len,unsigned char *data)
From where can I get libipq?
In reply to Patrick McHardyHi Eric,
I am trying to build an application which filters RTP packets. I am planing to use iptables QUEUE to implement this application. I am running RedHat enterprise linux WS version 4 on a 64bit hardware. The rpc shows me iptables-1.2.11-3.1.RHEL4. But I couldn't find libipq. Could you please kindly tell me where to find libipq? I would really appreciate your help. Thanks.
Mohammad
Re: From where can I get libipq?
Hi Mohammad
Quoted 7 lines · Mohammad Farooq
Hi Eric,
I am trying to build an application which filters RTP packets. I am planing to use iptables QUEUE to implement this application. I am running RedHat enterprise linux WS version 4 on a 64bit hardware. The rpc shows me iptables-1.2.11-3.1.RHEL4. But I couldn't find libipq. Could you please kindly tell me where to find libipq?
libipq is a bad choice on 64bits system. It has some design problem and you shoulod try to use libnetfilter_queue.
Nethertheless you should find libipq.a in the iptables devel packet.
BR,
Signature · 2 lines
Eric Leblond <[email protected]> INL
PATCH 2/2 Specify nf_queue max length from userspace
In reply to Eric LeblondAttached patch is against libnetfilter_queue svn.
BR,
Signature · 2 lines
Eric Leblond <[email protected]> INL
libnetfilter_queue/linux_nfnetlink_queue.h +2 -0
@@ -71,6 +71,7 @@ } __attribute__ ((packed));enum nfqnl_config_mode { +NFQNL_COPY_UNSPEC ,NFQNL_COPY_NONE ,NFQNL_COPY_META ,NFQNL_COPY_PACKET ,
@@ -79,6 +80,7 @@struct nfqnl_msg_config_params {u_int32_t copy_range;u_int8_t copy_mode;/* enum nfqnl_config_mode */ +u_int32_t queue_maxlen; } __attribute__ ((packed));
libnetfilter_queue/libnetfilter_queue.h +3 -0
@@ -49,6 +49,9 @@extern int nfq_set_mode (struct nfq_q_handle *qh,u_int8_t mode,unsigned int len); +int nfq_set_queue_maxlen (struct nfq_q_handle *qh, +u_int32_t queuelen); + extern int nfq_set_verdict(struct nfq_q_handle *qh, u_int32_t id, u_int32_t verdict,
libnetfilter_queue.c +20 -0
@@ -288,6 +288,26 @@return nfnl_talk (qh->h->nfnlh, nmh,0 ,0 ,NULL ,NULL ,NULL ); } +int nfq_set_queue_maxlen (struct nfq_q_handle *qh, +u_int32_t queuelen) +{ +char buf[NFNL_HEADER_LEN + +NFA_LENGTH (sizeof (struct nfqnl_msg_config_params ))]; +struct nfqnl_msg_config_params params; +struct nlmsghdr *nmh = (struct nlmsghdr *) buf; + +nfnl_fill_hdr (qh->h->nfnlssh, nmh,0 ,AF_UNSPEC , qh->id, +NFQNL_MSG_CONFIG ,NLM_F_REQUEST |NLM_F_ACK ); + + params.copy_range =0 ; + params.copy_mode =NFQNL_COPY_UNSPEC ; + params.queue_maxlen=htonl (queuelen); +nfnl_addattr_l (nmh,sizeof (buf),NFQA_CFG_PARAMS , ¶ms, +sizeof (params)); + +return nfnl_talk (qh->h->nfnlh, nmh,0 ,0 ,NULL ,NULL ,NULL ); +} +static int __set_verdict (struct nfq_q_handle *qh,u_int32_t id,u_int32_t verdict,u_int32_t mark,int set_mark,u_int32_t data_len,unsigned char *data)