Skip to content

Commit 94b27cc

Browse files
Florian Westphalkaber
authored andcommitted
netfilter: allow NFQUEUE bypass if no listener is available
If an skb is to be NF_QUEUE'd, but no program has opened the queue, the packet is dropped. This adds a v2 target revision of xt_NFQUEUE that allows packets to continue through the ruleset instead. Because the actual queueing happens outside of the target context, the 'bypass' flag has to be communicated back to the netfilter core. Unfortunately the only choice to do this without adding a new function argument is to use the target function return value (i.e. the verdict). In the NF_QUEUE case, the upper 16bit already contain the queue number to use. The previous patch reduced NF_VERDICT_MASK to 0xff, i.e. we now have extra room for a new flag. If a hook issued a NF_QUEUE verdict, then the netfilter core will continue packet processing if the queueing hook returns -ESRCH (== "this queue does not exist") and the new NF_VERDICT_FLAG_QUEUE_BYPASS flag is set in the verdict value. Note: If the queue exists, but userspace does not consume packets fast enough, the skb will still be dropped. Signed-off-by: Florian Westphal <fwestphal@astaro.com> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent f615df7 commit 94b27cc

5 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎include/linux/netfilter.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define NF_VERDICT_MASK 0x000000ff
3030

3131
/* extra verdict flags have mask 0x0000ff00 */
32+
#define NF_VERDICT_FLAG_QUEUE_BYPASS 0x00008000
3233

3334
/* queue number (NF_QUEUE) or errno (NF_DROP) */
3435
#define NF_VERDICT_QMASK 0xffff0000

‎include/linux/netfilter/xt_NFQUEUE.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ struct xt_NFQ_info_v1 {
2020
__u16 queues_total;
2121
};
2222

23+
struct xt_NFQ_info_v2 {
24+
__u16 queuenum;
25+
__u16 queues_total;
26+
__u16 bypass;
27+
};
28+
2329
#endif /* _XT_NFQ_TARGET_H */

‎net/netfilter/core.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
184184
if (ret < 0) {
185185
if (ret == -ECANCELED)
186186
goto next_hook;
187+
if (ret == -ESRCH &&
188+
(verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
189+
goto next_hook;
187190
kfree_skb(skb);
188191
}
189192
ret = 0;

‎net/netfilter/nf_queue.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ static int __nf_queue(struct sk_buff *skb,
138138
rcu_read_lock();
139139

140140
qh = rcu_dereference(queue_handler[pf]);
141-
if (!qh)
141+
if (!qh) {
142+
status = -ESRCH;
142143
goto err_unlock;
144+
}
143145

144146
afinfo = nf_get_afinfo(pf);
145147
if (!afinfo)
@@ -303,6 +305,9 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
303305
if (err < 0) {
304306
if (err == -ECANCELED)
305307
goto next_hook;
308+
if (err == -ESRCH &&
309+
(verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
310+
goto next_hook;
306311
kfree_skb(skb);
307312
}
308313
break;

‎net/netfilter/xt_NFQUEUE.c‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,20 @@ nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
8383
return NF_QUEUE_NR(queue);
8484
}
8585

86-
static int nfqueue_tg_v1_check(const struct xt_tgchk_param *par)
86+
static unsigned int
87+
nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
8788
{
88-
const struct xt_NFQ_info_v1 *info = par->targinfo;
89+
const struct xt_NFQ_info_v2 *info = par->targinfo;
90+
unsigned int ret = nfqueue_tg_v1(skb, par);
91+
92+
if (info->bypass)
93+
ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
94+
return ret;
95+
}
96+
97+
static int nfqueue_tg_check(const struct xt_tgchk_param *par)
98+
{
99+
const struct xt_NFQ_info_v2 *info = par->targinfo;
89100
u32 maxid;
90101

91102
if (unlikely(!rnd_inited)) {
@@ -102,6 +113,8 @@ static int nfqueue_tg_v1_check(const struct xt_tgchk_param *par)
102113
info->queues_total, maxid);
103114
return -ERANGE;
104115
}
116+
if (par->target->revision == 2 && info->bypass > 1)
117+
return -EINVAL;
105118
return 0;
106119
}
107120

@@ -117,11 +130,20 @@ static struct xt_target nfqueue_tg_reg[] __read_mostly = {
117130
.name = "NFQUEUE",
118131
.revision = 1,
119132
.family = NFPROTO_UNSPEC,
120-
.checkentry = nfqueue_tg_v1_check,
133+
.checkentry = nfqueue_tg_check,
121134
.target = nfqueue_tg_v1,
122135
.targetsize = sizeof(struct xt_NFQ_info_v1),
123136
.me = THIS_MODULE,
124137
},
138+
{
139+
.name = "NFQUEUE",
140+
.revision = 2,
141+
.family = NFPROTO_UNSPEC,
142+
.checkentry = nfqueue_tg_check,
143+
.target = nfqueue_tg_v2,
144+
.targetsize = sizeof(struct xt_NFQ_info_v2),
145+
.me = THIS_MODULE,
146+
},
125147
};
126148

127149
static int __init nfqueue_tg_init(void)

0 commit comments

Comments
 (0)