1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 Patrick McHardy <kaber@trash.net>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <net/netfilter/nf_tables.h>
13 #include <net/netfilter/nf_tables_core.h>
17 struct nft_set_ext_tmpl tmpl
;
18 enum nft_dynset_ops op
:8;
19 enum nft_registers sreg_key
:8;
20 enum nft_registers sreg_data
:8;
23 struct nft_expr
*expr
;
24 struct nft_set_binding binding
;
27 static void *nft_dynset_new(struct nft_set
*set
, const struct nft_expr
*expr
,
28 struct nft_regs
*regs
)
30 const struct nft_dynset
*priv
= nft_expr_priv(expr
);
31 struct nft_set_ext
*ext
;
35 if (!atomic_add_unless(&set
->nelems
, 1, set
->size
))
38 timeout
= priv
->timeout
? : set
->timeout
;
39 elem
= nft_set_elem_init(set
, &priv
->tmpl
,
40 ®s
->data
[priv
->sreg_key
], NULL
,
41 ®s
->data
[priv
->sreg_data
],
42 timeout
, 0, GFP_ATOMIC
);
46 ext
= nft_set_elem_ext(set
, elem
);
47 if (priv
->expr
!= NULL
&&
48 nft_expr_clone(nft_set_ext_expr(ext
), priv
->expr
) < 0)
54 nft_set_elem_destroy(set
, elem
, false);
57 atomic_dec(&set
->nelems
);
61 void nft_dynset_eval(const struct nft_expr
*expr
,
62 struct nft_regs
*regs
, const struct nft_pktinfo
*pkt
)
64 const struct nft_dynset
*priv
= nft_expr_priv(expr
);
65 struct nft_set
*set
= priv
->set
;
66 const struct nft_set_ext
*ext
;
69 if (priv
->op
== NFT_DYNSET_OP_DELETE
) {
70 set
->ops
->delete(set
, ®s
->data
[priv
->sreg_key
]);
74 if (set
->ops
->update(set
, ®s
->data
[priv
->sreg_key
], nft_dynset_new
,
76 if (priv
->op
== NFT_DYNSET_OP_UPDATE
&&
77 nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
78 timeout
= priv
->timeout
? : set
->timeout
;
79 *nft_set_ext_expiration(ext
) = get_jiffies_64() + timeout
;
82 nft_set_elem_update_expr(ext
, regs
, pkt
);
85 regs
->verdict
.code
= NFT_BREAK
;
90 regs
->verdict
.code
= NFT_BREAK
;
93 static const struct nla_policy nft_dynset_policy
[NFTA_DYNSET_MAX
+ 1] = {
94 [NFTA_DYNSET_SET_NAME
] = { .type
= NLA_STRING
,
95 .len
= NFT_SET_MAXNAMELEN
- 1 },
96 [NFTA_DYNSET_SET_ID
] = { .type
= NLA_U32
},
97 [NFTA_DYNSET_OP
] = { .type
= NLA_U32
},
98 [NFTA_DYNSET_SREG_KEY
] = { .type
= NLA_U32
},
99 [NFTA_DYNSET_SREG_DATA
] = { .type
= NLA_U32
},
100 [NFTA_DYNSET_TIMEOUT
] = { .type
= NLA_U64
},
101 [NFTA_DYNSET_EXPR
] = { .type
= NLA_NESTED
},
102 [NFTA_DYNSET_FLAGS
] = { .type
= NLA_U32
},
105 static int nft_dynset_init(const struct nft_ctx
*ctx
,
106 const struct nft_expr
*expr
,
107 const struct nlattr
* const tb
[])
109 struct nft_dynset
*priv
= nft_expr_priv(expr
);
110 u8 genmask
= nft_genmask_next(ctx
->net
);
115 lockdep_assert_held(&ctx
->net
->nft
.commit_mutex
);
117 if (tb
[NFTA_DYNSET_SET_NAME
] == NULL
||
118 tb
[NFTA_DYNSET_OP
] == NULL
||
119 tb
[NFTA_DYNSET_SREG_KEY
] == NULL
)
122 if (tb
[NFTA_DYNSET_FLAGS
]) {
123 u32 flags
= ntohl(nla_get_be32(tb
[NFTA_DYNSET_FLAGS
]));
125 if (flags
& ~NFT_DYNSET_F_INV
)
127 if (flags
& NFT_DYNSET_F_INV
)
131 set
= nft_set_lookup_global(ctx
->net
, ctx
->table
,
132 tb
[NFTA_DYNSET_SET_NAME
],
133 tb
[NFTA_DYNSET_SET_ID
], genmask
);
137 if (set
->ops
->update
== NULL
)
140 if (set
->flags
& NFT_SET_CONSTANT
)
143 priv
->op
= ntohl(nla_get_be32(tb
[NFTA_DYNSET_OP
]));
145 case NFT_DYNSET_OP_ADD
:
146 case NFT_DYNSET_OP_DELETE
:
148 case NFT_DYNSET_OP_UPDATE
:
149 if (!(set
->flags
& NFT_SET_TIMEOUT
))
157 if (tb
[NFTA_DYNSET_TIMEOUT
] != NULL
) {
158 if (!(set
->flags
& NFT_SET_TIMEOUT
))
160 timeout
= msecs_to_jiffies(be64_to_cpu(nla_get_be64(
161 tb
[NFTA_DYNSET_TIMEOUT
])));
164 priv
->sreg_key
= nft_parse_register(tb
[NFTA_DYNSET_SREG_KEY
]);
165 err
= nft_validate_register_load(priv
->sreg_key
, set
->klen
);
169 if (tb
[NFTA_DYNSET_SREG_DATA
] != NULL
) {
170 if (!(set
->flags
& NFT_SET_MAP
))
172 if (set
->dtype
== NFT_DATA_VERDICT
)
175 priv
->sreg_data
= nft_parse_register(tb
[NFTA_DYNSET_SREG_DATA
]);
176 err
= nft_validate_register_load(priv
->sreg_data
, set
->dlen
);
179 } else if (set
->flags
& NFT_SET_MAP
)
182 if (tb
[NFTA_DYNSET_EXPR
] != NULL
) {
183 if (!(set
->flags
& NFT_SET_EVAL
))
186 priv
->expr
= nft_set_elem_expr_alloc(ctx
, set
,
187 tb
[NFTA_DYNSET_EXPR
]);
188 if (IS_ERR(priv
->expr
))
189 return PTR_ERR(priv
->expr
);
191 if (set
->expr
&& set
->expr
->ops
!= priv
->expr
->ops
) {
197 nft_set_ext_prepare(&priv
->tmpl
);
198 nft_set_ext_add_length(&priv
->tmpl
, NFT_SET_EXT_KEY
, set
->klen
);
199 if (set
->flags
& NFT_SET_MAP
)
200 nft_set_ext_add_length(&priv
->tmpl
, NFT_SET_EXT_DATA
, set
->dlen
);
201 if (priv
->expr
!= NULL
)
202 nft_set_ext_add_length(&priv
->tmpl
, NFT_SET_EXT_EXPR
,
203 priv
->expr
->ops
->size
);
204 if (set
->flags
& NFT_SET_TIMEOUT
) {
205 if (timeout
|| set
->timeout
)
206 nft_set_ext_add(&priv
->tmpl
, NFT_SET_EXT_EXPIRATION
);
209 priv
->timeout
= timeout
;
211 err
= nf_tables_bind_set(ctx
, set
, &priv
->binding
);
222 if (priv
->expr
!= NULL
)
223 nft_expr_destroy(ctx
, priv
->expr
);
227 static void nft_dynset_deactivate(const struct nft_ctx
*ctx
,
228 const struct nft_expr
*expr
,
229 enum nft_trans_phase phase
)
231 struct nft_dynset
*priv
= nft_expr_priv(expr
);
233 nf_tables_deactivate_set(ctx
, priv
->set
, &priv
->binding
, phase
);
236 static void nft_dynset_activate(const struct nft_ctx
*ctx
,
237 const struct nft_expr
*expr
)
239 struct nft_dynset
*priv
= nft_expr_priv(expr
);
244 static void nft_dynset_destroy(const struct nft_ctx
*ctx
,
245 const struct nft_expr
*expr
)
247 struct nft_dynset
*priv
= nft_expr_priv(expr
);
249 if (priv
->expr
!= NULL
)
250 nft_expr_destroy(ctx
, priv
->expr
);
252 nf_tables_destroy_set(ctx
, priv
->set
);
255 static int nft_dynset_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
257 const struct nft_dynset
*priv
= nft_expr_priv(expr
);
258 u32 flags
= priv
->invert
? NFT_DYNSET_F_INV
: 0;
260 if (nft_dump_register(skb
, NFTA_DYNSET_SREG_KEY
, priv
->sreg_key
))
261 goto nla_put_failure
;
262 if (priv
->set
->flags
& NFT_SET_MAP
&&
263 nft_dump_register(skb
, NFTA_DYNSET_SREG_DATA
, priv
->sreg_data
))
264 goto nla_put_failure
;
265 if (nla_put_be32(skb
, NFTA_DYNSET_OP
, htonl(priv
->op
)))
266 goto nla_put_failure
;
267 if (nla_put_string(skb
, NFTA_DYNSET_SET_NAME
, priv
->set
->name
))
268 goto nla_put_failure
;
269 if (nla_put_be64(skb
, NFTA_DYNSET_TIMEOUT
,
270 cpu_to_be64(jiffies_to_msecs(priv
->timeout
)),
272 goto nla_put_failure
;
273 if (priv
->expr
&& nft_expr_dump(skb
, NFTA_DYNSET_EXPR
, priv
->expr
))
274 goto nla_put_failure
;
275 if (nla_put_be32(skb
, NFTA_DYNSET_FLAGS
, htonl(flags
)))
276 goto nla_put_failure
;
283 static const struct nft_expr_ops nft_dynset_ops
= {
284 .type
= &nft_dynset_type
,
285 .size
= NFT_EXPR_SIZE(sizeof(struct nft_dynset
)),
286 .eval
= nft_dynset_eval
,
287 .init
= nft_dynset_init
,
288 .destroy
= nft_dynset_destroy
,
289 .activate
= nft_dynset_activate
,
290 .deactivate
= nft_dynset_deactivate
,
291 .dump
= nft_dynset_dump
,
294 struct nft_expr_type nft_dynset_type __read_mostly
= {
296 .ops
= &nft_dynset_ops
,
297 .policy
= nft_dynset_policy
,
298 .maxattr
= NFTA_DYNSET_MAX
,
299 .owner
= THIS_MODULE
,