2 * Copyright (c) 2015 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/netlink.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables.h>
17 #include <net/netfilter/nf_tables_core.h>
21 struct nft_set_ext_tmpl tmpl
;
22 enum nft_dynset_ops op
:8;
23 enum nft_registers sreg_key
:8;
24 enum nft_registers sreg_data
:8;
27 struct nft_expr
*expr
;
28 struct nft_set_binding binding
;
31 static void *nft_dynset_new(struct nft_set
*set
, const struct nft_expr
*expr
,
32 struct nft_regs
*regs
)
34 const struct nft_dynset
*priv
= nft_expr_priv(expr
);
35 struct nft_set_ext
*ext
;
39 if (!atomic_add_unless(&set
->nelems
, 1, set
->size
))
42 timeout
= priv
->timeout
? : set
->timeout
;
43 elem
= nft_set_elem_init(set
, &priv
->tmpl
,
44 ®s
->data
[priv
->sreg_key
],
45 ®s
->data
[priv
->sreg_data
],
50 ext
= nft_set_elem_ext(set
, elem
);
51 if (priv
->expr
!= NULL
&&
52 nft_expr_clone(nft_set_ext_expr(ext
), priv
->expr
) < 0)
58 nft_set_elem_destroy(set
, elem
, false);
61 atomic_dec(&set
->nelems
);
65 void nft_dynset_eval(const struct nft_expr
*expr
,
66 struct nft_regs
*regs
, const struct nft_pktinfo
*pkt
)
68 const struct nft_dynset
*priv
= nft_expr_priv(expr
);
69 struct nft_set
*set
= priv
->set
;
70 const struct nft_set_ext
*ext
;
71 const struct nft_expr
*sexpr
;
74 if (set
->ops
->update(set
, ®s
->data
[priv
->sreg_key
], nft_dynset_new
,
77 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
))
78 sexpr
= nft_set_ext_expr(ext
);
80 if (priv
->op
== NFT_DYNSET_OP_UPDATE
&&
81 nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
82 timeout
= priv
->timeout
? : set
->timeout
;
83 *nft_set_ext_expiration(ext
) = get_jiffies_64() + timeout
;
87 sexpr
->ops
->eval(sexpr
, regs
, pkt
);
90 regs
->verdict
.code
= NFT_BREAK
;
95 regs
->verdict
.code
= NFT_BREAK
;
98 static const struct nla_policy nft_dynset_policy
[NFTA_DYNSET_MAX
+ 1] = {
99 [NFTA_DYNSET_SET_NAME
] = { .type
= NLA_STRING
,
100 .len
= NFT_SET_MAXNAMELEN
- 1 },
101 [NFTA_DYNSET_SET_ID
] = { .type
= NLA_U32
},
102 [NFTA_DYNSET_OP
] = { .type
= NLA_U32
},
103 [NFTA_DYNSET_SREG_KEY
] = { .type
= NLA_U32
},
104 [NFTA_DYNSET_SREG_DATA
] = { .type
= NLA_U32
},
105 [NFTA_DYNSET_TIMEOUT
] = { .type
= NLA_U64
},
106 [NFTA_DYNSET_EXPR
] = { .type
= NLA_NESTED
},
107 [NFTA_DYNSET_FLAGS
] = { .type
= NLA_U32
},
110 static int nft_dynset_init(const struct nft_ctx
*ctx
,
111 const struct nft_expr
*expr
,
112 const struct nlattr
* const tb
[])
114 struct nft_dynset
*priv
= nft_expr_priv(expr
);
115 u8 genmask
= nft_genmask_next(ctx
->net
);
120 lockdep_assert_held(&ctx
->net
->nft
.commit_mutex
);
122 if (tb
[NFTA_DYNSET_SET_NAME
] == NULL
||
123 tb
[NFTA_DYNSET_OP
] == NULL
||
124 tb
[NFTA_DYNSET_SREG_KEY
] == NULL
)
127 if (tb
[NFTA_DYNSET_FLAGS
]) {
128 u32 flags
= ntohl(nla_get_be32(tb
[NFTA_DYNSET_FLAGS
]));
130 if (flags
& ~NFT_DYNSET_F_INV
)
132 if (flags
& NFT_DYNSET_F_INV
)
136 set
= nft_set_lookup_global(ctx
->net
, ctx
->table
,
137 tb
[NFTA_DYNSET_SET_NAME
],
138 tb
[NFTA_DYNSET_SET_ID
], genmask
);
142 if (set
->ops
->update
== NULL
)
145 if (set
->flags
& NFT_SET_CONSTANT
)
148 priv
->op
= ntohl(nla_get_be32(tb
[NFTA_DYNSET_OP
]));
150 case NFT_DYNSET_OP_ADD
:
152 case NFT_DYNSET_OP_UPDATE
:
153 if (!(set
->flags
& NFT_SET_TIMEOUT
))
161 if (tb
[NFTA_DYNSET_TIMEOUT
] != NULL
) {
162 if (!(set
->flags
& NFT_SET_TIMEOUT
))
164 timeout
= msecs_to_jiffies(be64_to_cpu(nla_get_be64(
165 tb
[NFTA_DYNSET_TIMEOUT
])));
168 priv
->sreg_key
= nft_parse_register(tb
[NFTA_DYNSET_SREG_KEY
]);
169 err
= nft_validate_register_load(priv
->sreg_key
, set
->klen
);
173 if (tb
[NFTA_DYNSET_SREG_DATA
] != NULL
) {
174 if (!(set
->flags
& NFT_SET_MAP
))
176 if (set
->dtype
== NFT_DATA_VERDICT
)
179 priv
->sreg_data
= nft_parse_register(tb
[NFTA_DYNSET_SREG_DATA
]);
180 err
= nft_validate_register_load(priv
->sreg_data
, set
->dlen
);
183 } else if (set
->flags
& NFT_SET_MAP
)
186 if (tb
[NFTA_DYNSET_EXPR
] != NULL
) {
187 if (!(set
->flags
& NFT_SET_EVAL
))
190 priv
->expr
= nft_expr_init(ctx
, tb
[NFTA_DYNSET_EXPR
]);
191 if (IS_ERR(priv
->expr
))
192 return PTR_ERR(priv
->expr
);
195 if (!(priv
->expr
->ops
->type
->flags
& NFT_EXPR_STATEFUL
))
198 if (priv
->expr
->ops
->type
->flags
& NFT_EXPR_GC
) {
199 if (set
->flags
& NFT_SET_TIMEOUT
)
201 if (!set
->ops
->gc_init
)
203 set
->ops
->gc_init(set
);
207 nft_set_ext_prepare(&priv
->tmpl
);
208 nft_set_ext_add_length(&priv
->tmpl
, NFT_SET_EXT_KEY
, set
->klen
);
209 if (set
->flags
& NFT_SET_MAP
)
210 nft_set_ext_add_length(&priv
->tmpl
, NFT_SET_EXT_DATA
, set
->dlen
);
211 if (priv
->expr
!= NULL
)
212 nft_set_ext_add_length(&priv
->tmpl
, NFT_SET_EXT_EXPR
,
213 priv
->expr
->ops
->size
);
214 if (set
->flags
& NFT_SET_TIMEOUT
) {
215 if (timeout
|| set
->timeout
)
216 nft_set_ext_add(&priv
->tmpl
, NFT_SET_EXT_EXPIRATION
);
219 priv
->timeout
= timeout
;
221 err
= nf_tables_bind_set(ctx
, set
, &priv
->binding
);
232 if (priv
->expr
!= NULL
)
233 nft_expr_destroy(ctx
, priv
->expr
);
237 static void nft_dynset_deactivate(const struct nft_ctx
*ctx
,
238 const struct nft_expr
*expr
,
239 enum nft_trans_phase phase
)
241 struct nft_dynset
*priv
= nft_expr_priv(expr
);
243 nf_tables_deactivate_set(ctx
, priv
->set
, &priv
->binding
, phase
);
246 static void nft_dynset_activate(const struct nft_ctx
*ctx
,
247 const struct nft_expr
*expr
)
249 struct nft_dynset
*priv
= nft_expr_priv(expr
);
254 static void nft_dynset_destroy(const struct nft_ctx
*ctx
,
255 const struct nft_expr
*expr
)
257 struct nft_dynset
*priv
= nft_expr_priv(expr
);
259 if (priv
->expr
!= NULL
)
260 nft_expr_destroy(ctx
, priv
->expr
);
262 nf_tables_destroy_set(ctx
, priv
->set
);
265 static int nft_dynset_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
267 const struct nft_dynset
*priv
= nft_expr_priv(expr
);
268 u32 flags
= priv
->invert
? NFT_DYNSET_F_INV
: 0;
270 if (nft_dump_register(skb
, NFTA_DYNSET_SREG_KEY
, priv
->sreg_key
))
271 goto nla_put_failure
;
272 if (priv
->set
->flags
& NFT_SET_MAP
&&
273 nft_dump_register(skb
, NFTA_DYNSET_SREG_DATA
, priv
->sreg_data
))
274 goto nla_put_failure
;
275 if (nla_put_be32(skb
, NFTA_DYNSET_OP
, htonl(priv
->op
)))
276 goto nla_put_failure
;
277 if (nla_put_string(skb
, NFTA_DYNSET_SET_NAME
, priv
->set
->name
))
278 goto nla_put_failure
;
279 if (nla_put_be64(skb
, NFTA_DYNSET_TIMEOUT
,
280 cpu_to_be64(jiffies_to_msecs(priv
->timeout
)),
282 goto nla_put_failure
;
283 if (priv
->expr
&& nft_expr_dump(skb
, NFTA_DYNSET_EXPR
, priv
->expr
))
284 goto nla_put_failure
;
285 if (nla_put_be32(skb
, NFTA_DYNSET_FLAGS
, htonl(flags
)))
286 goto nla_put_failure
;
293 static const struct nft_expr_ops nft_dynset_ops
= {
294 .type
= &nft_dynset_type
,
295 .size
= NFT_EXPR_SIZE(sizeof(struct nft_dynset
)),
296 .eval
= nft_dynset_eval
,
297 .init
= nft_dynset_init
,
298 .destroy
= nft_dynset_destroy
,
299 .activate
= nft_dynset_activate
,
300 .deactivate
= nft_dynset_deactivate
,
301 .dump
= nft_dynset_dump
,
304 struct nft_expr_type nft_dynset_type __read_mostly
= {
306 .ops
= &nft_dynset_ops
,
307 .policy
= nft_dynset_policy
,
308 .maxattr
= NFTA_DYNSET_MAX
,
309 .owner
= THIS_MODULE
,