diff options
author | Laura Garcia Liebana <nevola@gmail.com> | 2016-09-13 07:49:53 -0400 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2016-09-22 10:33:05 -0400 |
commit | 2b03bf732488a3c2e920afe22c03b82cb8477e28 (patch) | |
tree | 116f31f5533c7a0a97bf811417b18c81591e7540 | |
parent | 14e2dee0996f51e0ff0d868497c7e1b90f012665 (diff) |
netfilter: nft_numgen: add number generation offset
Add support of an offset value for incremental counter and random. With
this option the sysadmin is able to start the counter to a certain value
and then apply the generated number.
Example:
meta mark set numgen inc mod 2 offset 100
This will generate marks with the serie 100, 101, 100, 101, ...
Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
-rw-r--r-- | include/uapi/linux/netfilter/nf_tables.h | 2 | ||||
-rw-r--r-- | net/netfilter/nft_numgen.c | 32 |
2 files changed, 28 insertions, 6 deletions
diff --git a/include/uapi/linux/netfilter/nf_tables.h b/include/uapi/linux/netfilter/nf_tables.h index bc0eb6a1066d..bcfb892ff148 100644 --- a/include/uapi/linux/netfilter/nf_tables.h +++ b/include/uapi/linux/netfilter/nf_tables.h | |||
@@ -1136,12 +1136,14 @@ enum nft_trace_types { | |||
1136 | * @NFTA_NG_DREG: destination register (NLA_U32) | 1136 | * @NFTA_NG_DREG: destination register (NLA_U32) |
1137 | * @NFTA_NG_MODULUS: maximum counter value (NLA_U32) | 1137 | * @NFTA_NG_MODULUS: maximum counter value (NLA_U32) |
1138 | * @NFTA_NG_TYPE: operation type (NLA_U32) | 1138 | * @NFTA_NG_TYPE: operation type (NLA_U32) |
1139 | * @NFTA_NG_OFFSET: offset to be added to the counter (NLA_U32) | ||
1139 | */ | 1140 | */ |
1140 | enum nft_ng_attributes { | 1141 | enum nft_ng_attributes { |
1141 | NFTA_NG_UNSPEC, | 1142 | NFTA_NG_UNSPEC, |
1142 | NFTA_NG_DREG, | 1143 | NFTA_NG_DREG, |
1143 | NFTA_NG_MODULUS, | 1144 | NFTA_NG_MODULUS, |
1144 | NFTA_NG_TYPE, | 1145 | NFTA_NG_TYPE, |
1146 | NFTA_NG_OFFSET, | ||
1145 | __NFTA_NG_MAX | 1147 | __NFTA_NG_MAX |
1146 | }; | 1148 | }; |
1147 | #define NFTA_NG_MAX (__NFTA_NG_MAX - 1) | 1149 | #define NFTA_NG_MAX (__NFTA_NG_MAX - 1) |
diff --git a/net/netfilter/nft_numgen.c b/net/netfilter/nft_numgen.c index f173ebec30a7..55bc5ab78d4a 100644 --- a/net/netfilter/nft_numgen.c +++ b/net/netfilter/nft_numgen.c | |||
@@ -23,6 +23,7 @@ struct nft_ng_inc { | |||
23 | enum nft_registers dreg:8; | 23 | enum nft_registers dreg:8; |
24 | u32 modulus; | 24 | u32 modulus; |
25 | atomic_t counter; | 25 | atomic_t counter; |
26 | u32 offset; | ||
26 | }; | 27 | }; |
27 | 28 | ||
28 | static void nft_ng_inc_eval(const struct nft_expr *expr, | 29 | static void nft_ng_inc_eval(const struct nft_expr *expr, |
@@ -37,13 +38,14 @@ static void nft_ng_inc_eval(const struct nft_expr *expr, | |||
37 | nval = (oval + 1 < priv->modulus) ? oval + 1 : 0; | 38 | nval = (oval + 1 < priv->modulus) ? oval + 1 : 0; |
38 | } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval); | 39 | } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval); |
39 | 40 | ||
40 | regs->data[priv->dreg] = nval; | 41 | regs->data[priv->dreg] = nval + priv->offset; |
41 | } | 42 | } |
42 | 43 | ||
43 | static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = { | 44 | static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = { |
44 | [NFTA_NG_DREG] = { .type = NLA_U32 }, | 45 | [NFTA_NG_DREG] = { .type = NLA_U32 }, |
45 | [NFTA_NG_MODULUS] = { .type = NLA_U32 }, | 46 | [NFTA_NG_MODULUS] = { .type = NLA_U32 }, |
46 | [NFTA_NG_TYPE] = { .type = NLA_U32 }, | 47 | [NFTA_NG_TYPE] = { .type = NLA_U32 }, |
48 | [NFTA_NG_OFFSET] = { .type = NLA_U32 }, | ||
47 | }; | 49 | }; |
48 | 50 | ||
49 | static int nft_ng_inc_init(const struct nft_ctx *ctx, | 51 | static int nft_ng_inc_init(const struct nft_ctx *ctx, |
@@ -52,10 +54,16 @@ static int nft_ng_inc_init(const struct nft_ctx *ctx, | |||
52 | { | 54 | { |
53 | struct nft_ng_inc *priv = nft_expr_priv(expr); | 55 | struct nft_ng_inc *priv = nft_expr_priv(expr); |
54 | 56 | ||
57 | if (tb[NFTA_NG_OFFSET]) | ||
58 | priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET])); | ||
59 | |||
55 | priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS])); | 60 | priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS])); |
56 | if (priv->modulus == 0) | 61 | if (priv->modulus == 0) |
57 | return -ERANGE; | 62 | return -ERANGE; |
58 | 63 | ||
64 | if (priv->offset + priv->modulus - 1 < priv->offset) | ||
65 | return -EOVERFLOW; | ||
66 | |||
59 | priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]); | 67 | priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]); |
60 | atomic_set(&priv->counter, 0); | 68 | atomic_set(&priv->counter, 0); |
61 | 69 | ||
@@ -64,7 +72,7 @@ static int nft_ng_inc_init(const struct nft_ctx *ctx, | |||
64 | } | 72 | } |
65 | 73 | ||
66 | static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg, | 74 | static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg, |
67 | u32 modulus, enum nft_ng_types type) | 75 | u32 modulus, enum nft_ng_types type, u32 offset) |
68 | { | 76 | { |
69 | if (nft_dump_register(skb, NFTA_NG_DREG, dreg)) | 77 | if (nft_dump_register(skb, NFTA_NG_DREG, dreg)) |
70 | goto nla_put_failure; | 78 | goto nla_put_failure; |
@@ -72,6 +80,8 @@ static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg, | |||
72 | goto nla_put_failure; | 80 | goto nla_put_failure; |
73 | if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type))) | 81 | if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type))) |
74 | goto nla_put_failure; | 82 | goto nla_put_failure; |
83 | if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset))) | ||
84 | goto nla_put_failure; | ||
75 | 85 | ||
76 | return 0; | 86 | return 0; |
77 | 87 | ||
@@ -83,12 +93,14 @@ static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr) | |||
83 | { | 93 | { |
84 | const struct nft_ng_inc *priv = nft_expr_priv(expr); | 94 | const struct nft_ng_inc *priv = nft_expr_priv(expr); |
85 | 95 | ||
86 | return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL); | 96 | return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL, |
97 | priv->offset); | ||
87 | } | 98 | } |
88 | 99 | ||
89 | struct nft_ng_random { | 100 | struct nft_ng_random { |
90 | enum nft_registers dreg:8; | 101 | enum nft_registers dreg:8; |
91 | u32 modulus; | 102 | u32 modulus; |
103 | u32 offset; | ||
92 | }; | 104 | }; |
93 | 105 | ||
94 | static void nft_ng_random_eval(const struct nft_expr *expr, | 106 | static void nft_ng_random_eval(const struct nft_expr *expr, |
@@ -97,9 +109,10 @@ static void nft_ng_random_eval(const struct nft_expr *expr, | |||
97 | { | 109 | { |
98 | struct nft_ng_random *priv = nft_expr_priv(expr); | 110 | struct nft_ng_random *priv = nft_expr_priv(expr); |
99 | struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state); | 111 | struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state); |
112 | u32 val; | ||
100 | 113 | ||
101 | regs->data[priv->dreg] = reciprocal_scale(prandom_u32_state(state), | 114 | val = reciprocal_scale(prandom_u32_state(state), priv->modulus); |
102 | priv->modulus); | 115 | regs->data[priv->dreg] = val + priv->offset; |
103 | } | 116 | } |
104 | 117 | ||
105 | static int nft_ng_random_init(const struct nft_ctx *ctx, | 118 | static int nft_ng_random_init(const struct nft_ctx *ctx, |
@@ -108,10 +121,16 @@ static int nft_ng_random_init(const struct nft_ctx *ctx, | |||
108 | { | 121 | { |
109 | struct nft_ng_random *priv = nft_expr_priv(expr); | 122 | struct nft_ng_random *priv = nft_expr_priv(expr); |
110 | 123 | ||
124 | if (tb[NFTA_NG_OFFSET]) | ||
125 | priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET])); | ||
126 | |||
111 | priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS])); | 127 | priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS])); |
112 | if (priv->modulus == 0) | 128 | if (priv->modulus == 0) |
113 | return -ERANGE; | 129 | return -ERANGE; |
114 | 130 | ||
131 | if (priv->offset + priv->modulus - 1 < priv->offset) | ||
132 | return -EOVERFLOW; | ||
133 | |||
115 | prandom_init_once(&nft_numgen_prandom_state); | 134 | prandom_init_once(&nft_numgen_prandom_state); |
116 | 135 | ||
117 | priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]); | 136 | priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]); |
@@ -124,7 +143,8 @@ static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr) | |||
124 | { | 143 | { |
125 | const struct nft_ng_random *priv = nft_expr_priv(expr); | 144 | const struct nft_ng_random *priv = nft_expr_priv(expr); |
126 | 145 | ||
127 | return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM); | 146 | return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM, |
147 | priv->offset); | ||
128 | } | 148 | } |
129 | 149 | ||
130 | static struct nft_expr_type nft_ng_type; | 150 | static struct nft_expr_type nft_ng_type; |