Skip to content

Commit f615df7

Browse files
Florian Westphalkaber
authored andcommitted
netfilter: reduce NF_VERDICT_MASK to 0xff
NF_VERDICT_MASK is currently 0xffff. This is because the upper 16 bits are used to store errno (for NF_DROP) or the queue number (NF_QUEUE verdict). As there are up to 0xffff different queues available, there is no more room to store additional flags. At the moment there are only 6 different verdicts, i.e. we can reduce NF_VERDICT_MASK to 0xff to allow storing additional flags in the 0xff00 space. NF_VERDICT_BITS would then be reduced to 8, but because the value is exported to userspace, this might cause breakage; e.g.: e.g. 'queuenr = (1 << NF_VERDICT_BITS) | NF_QUEUE' would now break. Thus, remove NF_VERDICT_BITS usage in the kernel and move the old value to the 'userspace compat' section. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent 06cdb63 commit f615df7

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

‎include/linux/netfilter.h‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,29 @@
2424
#define NF_MAX_VERDICT NF_STOP
2525

2626
/* we overload the higher bits for encoding auxiliary data such as the queue
27-
* number. Not nice, but better than additional function arguments. */
28-
#define NF_VERDICT_MASK 0x0000ffff
29-
#define NF_VERDICT_BITS 16
27+
* number or errno values. Not nice, but better than additional function
28+
* arguments. */
29+
#define NF_VERDICT_MASK 0x000000ff
30+
31+
/* extra verdict flags have mask 0x0000ff00 */
3032

33+
/* queue number (NF_QUEUE) or errno (NF_DROP) */
3134
#define NF_VERDICT_QMASK 0xffff0000
3235
#define NF_VERDICT_QBITS 16
3336

34-
#define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
37+
#define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE)
3538

36-
#define NF_DROP_ERR(x) (((-x) << NF_VERDICT_BITS) | NF_DROP)
39+
#define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP)
3740

3841
/* only for userspace compatibility */
3942
#ifndef __KERNEL__
4043
/* Generic cache responses from hook functions.
4144
<= 0x2000 is used for protocol-flags. */
4245
#define NFC_UNKNOWN 0x4000
4346
#define NFC_ALTERED 0x8000
47+
48+
/* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */
49+
#define NF_VERDICT_BITS 16
4450
#endif
4551

4652
enum nf_inet_hooks {
@@ -72,6 +78,10 @@ union nf_inet_addr {
7278

7379
#ifdef __KERNEL__
7480
#ifdef CONFIG_NETFILTER
81+
static inline int NF_DROP_GETERR(int verdict)
82+
{
83+
return -(verdict >> NF_VERDICT_QBITS);
84+
}
7585

7686
static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
7787
const union nf_inet_addr *a2)

‎net/netfilter/core.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
175175
ret = 1;
176176
} else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
177177
kfree_skb(skb);
178-
ret = -(verdict >> NF_VERDICT_BITS);
178+
ret = NF_DROP_GETERR(verdict);
179179
if (ret == 0)
180180
ret = -EPERM;
181181
} else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
182182
ret = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
183-
verdict >> NF_VERDICT_BITS);
183+
verdict >> NF_VERDICT_QBITS);
184184
if (ret < 0) {
185185
if (ret == -ECANCELED)
186186
goto next_hook;

‎net/netfilter/nf_queue.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
299299
case NF_QUEUE:
300300
err = __nf_queue(skb, elem, entry->pf, entry->hook,
301301
entry->indev, entry->outdev, entry->okfn,
302-
verdict >> NF_VERDICT_BITS);
302+
verdict >> NF_VERDICT_QBITS);
303303
if (err < 0) {
304304
if (err == -ECANCELED)
305305
goto next_hook;

0 commit comments

Comments
 (0)