Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
20a69341 | 3 | * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
96518518 | 4 | * |
96518518 PM |
5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
6 | */ | |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/list.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/netlink.h> | |
1ff75a3e | 13 | #include <linux/vmalloc.h> |
0eb71a9d | 14 | #include <linux/rhashtable.h> |
8e6cf365 | 15 | #include <linux/audit.h> |
96518518 PM |
16 | #include <linux/netfilter.h> |
17 | #include <linux/netfilter/nfnetlink.h> | |
18 | #include <linux/netfilter/nf_tables.h> | |
3b49e2e9 | 19 | #include <net/netfilter/nf_flow_table.h> |
96518518 PM |
20 | #include <net/netfilter/nf_tables_core.h> |
21 | #include <net/netfilter/nf_tables.h> | |
c9626a2c | 22 | #include <net/netfilter/nf_tables_offload.h> |
99633ab2 | 23 | #include <net/net_namespace.h> |
96518518 PM |
24 | #include <net/sock.h> |
25 | ||
9332d27d FW |
26 | #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-")) |
27 | ||
0854db2a FW |
28 | unsigned int nf_tables_net_id __read_mostly; |
29 | ||
96518518 | 30 | static LIST_HEAD(nf_tables_expressions); |
e5009240 | 31 | static LIST_HEAD(nf_tables_objects); |
3b49e2e9 | 32 | static LIST_HEAD(nf_tables_flowtables); |
0935d558 FW |
33 | static LIST_HEAD(nf_tables_destroy_list); |
34 | static DEFINE_SPINLOCK(nf_tables_destroy_list_lock); | |
96518518 | 35 | |
a654de8f PNA |
36 | enum { |
37 | NFT_VALIDATE_SKIP = 0, | |
38 | NFT_VALIDATE_NEED, | |
39 | NFT_VALIDATE_DO, | |
40 | }; | |
41 | ||
4d44175a FW |
42 | static struct rhltable nft_objname_ht; |
43 | ||
1b2470e5 FW |
44 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed); |
45 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed); | |
46 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
47 | ||
4d44175a FW |
48 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed); |
49 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed); | |
50 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
51 | ||
1b2470e5 FW |
52 | static const struct rhashtable_params nft_chain_ht_params = { |
53 | .head_offset = offsetof(struct nft_chain, rhlhead), | |
54 | .key_offset = offsetof(struct nft_chain, name), | |
55 | .hashfn = nft_chain_hash, | |
56 | .obj_hashfn = nft_chain_hash_obj, | |
57 | .obj_cmpfn = nft_chain_hash_cmp, | |
1b2470e5 FW |
58 | .automatic_shrinking = true, |
59 | }; | |
60 | ||
4d44175a FW |
61 | static const struct rhashtable_params nft_objname_ht_params = { |
62 | .head_offset = offsetof(struct nft_object, rhlhead), | |
63 | .key_offset = offsetof(struct nft_object, key), | |
64 | .hashfn = nft_objname_hash, | |
65 | .obj_hashfn = nft_objname_hash_obj, | |
66 | .obj_cmpfn = nft_objname_hash_cmp, | |
67 | .automatic_shrinking = true, | |
68 | }; | |
69 | ||
c520292f RGB |
70 | struct nft_audit_data { |
71 | struct nft_table *table; | |
72 | int entries; | |
73 | int op; | |
74 | struct list_head list; | |
75 | }; | |
76 | ||
77 | static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types | |
78 | [NFT_MSG_NEWTABLE] = AUDIT_NFT_OP_TABLE_REGISTER, | |
79 | [NFT_MSG_GETTABLE] = AUDIT_NFT_OP_INVALID, | |
80 | [NFT_MSG_DELTABLE] = AUDIT_NFT_OP_TABLE_UNREGISTER, | |
81 | [NFT_MSG_NEWCHAIN] = AUDIT_NFT_OP_CHAIN_REGISTER, | |
82 | [NFT_MSG_GETCHAIN] = AUDIT_NFT_OP_INVALID, | |
83 | [NFT_MSG_DELCHAIN] = AUDIT_NFT_OP_CHAIN_UNREGISTER, | |
84 | [NFT_MSG_NEWRULE] = AUDIT_NFT_OP_RULE_REGISTER, | |
85 | [NFT_MSG_GETRULE] = AUDIT_NFT_OP_INVALID, | |
86 | [NFT_MSG_DELRULE] = AUDIT_NFT_OP_RULE_UNREGISTER, | |
87 | [NFT_MSG_NEWSET] = AUDIT_NFT_OP_SET_REGISTER, | |
88 | [NFT_MSG_GETSET] = AUDIT_NFT_OP_INVALID, | |
89 | [NFT_MSG_DELSET] = AUDIT_NFT_OP_SET_UNREGISTER, | |
90 | [NFT_MSG_NEWSETELEM] = AUDIT_NFT_OP_SETELEM_REGISTER, | |
91 | [NFT_MSG_GETSETELEM] = AUDIT_NFT_OP_INVALID, | |
92 | [NFT_MSG_DELSETELEM] = AUDIT_NFT_OP_SETELEM_UNREGISTER, | |
93 | [NFT_MSG_NEWGEN] = AUDIT_NFT_OP_GEN_REGISTER, | |
94 | [NFT_MSG_GETGEN] = AUDIT_NFT_OP_INVALID, | |
95 | [NFT_MSG_TRACE] = AUDIT_NFT_OP_INVALID, | |
96 | [NFT_MSG_NEWOBJ] = AUDIT_NFT_OP_OBJ_REGISTER, | |
97 | [NFT_MSG_GETOBJ] = AUDIT_NFT_OP_INVALID, | |
98 | [NFT_MSG_DELOBJ] = AUDIT_NFT_OP_OBJ_UNREGISTER, | |
99 | [NFT_MSG_GETOBJ_RESET] = AUDIT_NFT_OP_OBJ_RESET, | |
100 | [NFT_MSG_NEWFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_REGISTER, | |
101 | [NFT_MSG_GETFLOWTABLE] = AUDIT_NFT_OP_INVALID, | |
102 | [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, | |
103 | }; | |
104 | ||
00c320f9 | 105 | static void nft_validate_state_update(struct nft_table *table, u8 new_validate_state) |
a654de8f | 106 | { |
00c320f9 | 107 | switch (table->validate_state) { |
a654de8f PNA |
108 | case NFT_VALIDATE_SKIP: |
109 | WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO); | |
110 | break; | |
111 | case NFT_VALIDATE_NEED: | |
112 | break; | |
113 | case NFT_VALIDATE_DO: | |
114 | if (new_validate_state == NFT_VALIDATE_NEED) | |
115 | return; | |
116 | } | |
117 | ||
00c320f9 | 118 | table->validate_state = new_validate_state; |
a654de8f | 119 | } |
0935d558 FW |
120 | static void nf_tables_trans_destroy_work(struct work_struct *w); |
121 | static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work); | |
a654de8f | 122 | |
7c95f6d8 | 123 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 124 | struct net *net, |
7c95f6d8 PNA |
125 | const struct sk_buff *skb, |
126 | const struct nlmsghdr *nlh, | |
36596dad | 127 | u8 family, |
7c95f6d8 PNA |
128 | struct nft_table *table, |
129 | struct nft_chain *chain, | |
130 | const struct nlattr * const *nla) | |
131 | { | |
633c9a84 | 132 | ctx->net = net; |
36596dad | 133 | ctx->family = family; |
26b2f552 | 134 | ctx->level = 0; |
128ad332 PNA |
135 | ctx->table = table; |
136 | ctx->chain = chain; | |
137 | ctx->nla = nla; | |
138 | ctx->portid = NETLINK_CB(skb).portid; | |
139 | ctx->report = nlmsg_report(nlh); | |
c9626a2c | 140 | ctx->flags = nlh->nlmsg_flags; |
128ad332 | 141 | ctx->seq = nlh->nlmsg_seq; |
7c95f6d8 PNA |
142 | } |
143 | ||
8411b644 PNA |
144 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
145 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
146 | { |
147 | struct nft_trans *trans; | |
148 | ||
8411b644 | 149 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
150 | if (trans == NULL) |
151 | return NULL; | |
152 | ||
58007785 | 153 | INIT_LIST_HEAD(&trans->list); |
b380e5c7 | 154 | trans->msg_type = msg_type; |
1081d11b PNA |
155 | trans->ctx = *ctx; |
156 | ||
157 | return trans; | |
158 | } | |
159 | ||
8411b644 PNA |
160 | static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
161 | int msg_type, u32 size) | |
162 | { | |
163 | return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); | |
164 | } | |
165 | ||
1081d11b PNA |
166 | static void nft_trans_destroy(struct nft_trans *trans) |
167 | { | |
168 | list_del(&trans->list); | |
169 | kfree(trans); | |
170 | } | |
171 | ||
26b5a571 PNA |
172 | static void __nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set, |
173 | bool bind) | |
f6ac8585 | 174 | { |
0854db2a | 175 | struct nftables_pernet *nft_net; |
f6ac8585 PNA |
176 | struct net *net = ctx->net; |
177 | struct nft_trans *trans; | |
178 | ||
179 | if (!nft_set_is_anonymous(set)) | |
180 | return; | |
181 | ||
d59d2f82 | 182 | nft_net = nft_pernet(net); |
0854db2a | 183 | list_for_each_entry_reverse(trans, &nft_net->commit_list, list) { |
6a0a8d10 PNA |
184 | switch (trans->msg_type) { |
185 | case NFT_MSG_NEWSET: | |
186 | if (nft_trans_set(trans) == set) | |
26b5a571 | 187 | nft_trans_set_bound(trans) = bind; |
6a0a8d10 PNA |
188 | break; |
189 | case NFT_MSG_NEWSETELEM: | |
190 | if (nft_trans_elem_set(trans) == set) | |
26b5a571 | 191 | nft_trans_elem_set_bound(trans) = bind; |
f6ac8585 PNA |
192 | break; |
193 | } | |
194 | } | |
195 | } | |
196 | ||
26b5a571 PNA |
197 | static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set) |
198 | { | |
199 | return __nft_set_trans_bind(ctx, set, true); | |
200 | } | |
201 | ||
202 | static void nft_set_trans_unbind(const struct nft_ctx *ctx, struct nft_set *set) | |
203 | { | |
204 | return __nft_set_trans_bind(ctx, set, false); | |
205 | } | |
206 | ||
207 | static void __nft_chain_trans_bind(const struct nft_ctx *ctx, | |
208 | struct nft_chain *chain, bool bind) | |
4bedf9ee PNA |
209 | { |
210 | struct nftables_pernet *nft_net; | |
211 | struct net *net = ctx->net; | |
212 | struct nft_trans *trans; | |
213 | ||
214 | if (!nft_chain_binding(chain)) | |
215 | return; | |
216 | ||
217 | nft_net = nft_pernet(net); | |
218 | list_for_each_entry_reverse(trans, &nft_net->commit_list, list) { | |
219 | switch (trans->msg_type) { | |
220 | case NFT_MSG_NEWCHAIN: | |
221 | if (nft_trans_chain(trans) == chain) | |
26b5a571 | 222 | nft_trans_chain_bound(trans) = bind; |
4bedf9ee PNA |
223 | break; |
224 | case NFT_MSG_NEWRULE: | |
225 | if (trans->ctx.chain == chain) | |
26b5a571 | 226 | nft_trans_rule_bound(trans) = bind; |
4bedf9ee PNA |
227 | break; |
228 | } | |
229 | } | |
230 | } | |
231 | ||
26b5a571 PNA |
232 | static void nft_chain_trans_bind(const struct nft_ctx *ctx, |
233 | struct nft_chain *chain) | |
234 | { | |
235 | __nft_chain_trans_bind(ctx, chain, true); | |
236 | } | |
237 | ||
4bedf9ee PNA |
238 | int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain) |
239 | { | |
240 | if (!nft_chain_binding(chain)) | |
241 | return 0; | |
242 | ||
243 | if (nft_chain_binding(ctx->chain)) | |
244 | return -EOPNOTSUPP; | |
245 | ||
246 | if (chain->bound) | |
247 | return -EBUSY; | |
248 | ||
249 | chain->bound = true; | |
250 | chain->use++; | |
251 | nft_chain_trans_bind(ctx, chain); | |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
26b5a571 PNA |
256 | void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain) |
257 | { | |
258 | __nft_chain_trans_bind(ctx, chain, false); | |
259 | } | |
260 | ||
d54725cd PNA |
261 | static int nft_netdev_register_hooks(struct net *net, |
262 | struct list_head *hook_list) | |
263 | { | |
264 | struct nft_hook *hook; | |
265 | int err, j; | |
266 | ||
267 | j = 0; | |
268 | list_for_each_entry(hook, hook_list, list) { | |
269 | err = nf_register_net_hook(net, &hook->ops); | |
270 | if (err < 0) | |
271 | goto err_register; | |
272 | ||
273 | j++; | |
274 | } | |
275 | return 0; | |
276 | ||
277 | err_register: | |
278 | list_for_each_entry(hook, hook_list, list) { | |
279 | if (j-- <= 0) | |
280 | break; | |
281 | ||
282 | nf_unregister_net_hook(net, &hook->ops); | |
283 | } | |
284 | return err; | |
285 | } | |
286 | ||
287 | static void nft_netdev_unregister_hooks(struct net *net, | |
f9a43007 PNA |
288 | struct list_head *hook_list, |
289 | bool release_netdev) | |
d54725cd | 290 | { |
f9a43007 | 291 | struct nft_hook *hook, *next; |
d54725cd | 292 | |
f9a43007 | 293 | list_for_each_entry_safe(hook, next, hook_list, list) { |
d54725cd | 294 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
295 | if (release_netdev) { |
296 | list_del(&hook->list); | |
297 | kfree_rcu(hook, rcu); | |
298 | } | |
299 | } | |
d54725cd PNA |
300 | } |
301 | ||
c974a3a3 PNA |
302 | static int nf_tables_register_hook(struct net *net, |
303 | const struct nft_table *table, | |
304 | struct nft_chain *chain) | |
d8ee8f7c | 305 | { |
d54725cd | 306 | struct nft_base_chain *basechain; |
a37061a6 | 307 | const struct nf_hook_ops *ops; |
ae6153b5 | 308 | |
d8ee8f7c | 309 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 310 | !nft_is_base_chain(chain)) |
d8ee8f7c PNA |
311 | return 0; |
312 | ||
4e25ceb8 FW |
313 | basechain = nft_base_chain(chain); |
314 | ops = &basechain->ops; | |
ae6153b5 | 315 | |
4e25ceb8 FW |
316 | if (basechain->type->ops_register) |
317 | return basechain->type->ops_register(net, ops); | |
318 | ||
d3519cb8 | 319 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
1e9451cb FW |
320 | return nft_netdev_register_hooks(net, &basechain->hook_list); |
321 | ||
322 | return nf_register_net_hook(net, &basechain->ops); | |
d8ee8f7c PNA |
323 | } |
324 | ||
f9a43007 PNA |
325 | static void __nf_tables_unregister_hook(struct net *net, |
326 | const struct nft_table *table, | |
327 | struct nft_chain *chain, | |
328 | bool release_netdev) | |
c5598794 | 329 | { |
d54725cd | 330 | struct nft_base_chain *basechain; |
4e25ceb8 FW |
331 | const struct nf_hook_ops *ops; |
332 | ||
d8ee8f7c | 333 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 334 | !nft_is_base_chain(chain)) |
d8ee8f7c | 335 | return; |
4e25ceb8 FW |
336 | basechain = nft_base_chain(chain); |
337 | ops = &basechain->ops; | |
d8ee8f7c | 338 | |
4e25ceb8 FW |
339 | if (basechain->type->ops_unregister) |
340 | return basechain->type->ops_unregister(net, ops); | |
341 | ||
d3519cb8 | 342 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
f9a43007 PNA |
343 | nft_netdev_unregister_hooks(net, &basechain->hook_list, |
344 | release_netdev); | |
1e9451cb FW |
345 | else |
346 | nf_unregister_net_hook(net, &basechain->ops); | |
c5598794 AB |
347 | } |
348 | ||
f9a43007 PNA |
349 | static void nf_tables_unregister_hook(struct net *net, |
350 | const struct nft_table *table, | |
351 | struct nft_chain *chain) | |
352 | { | |
353 | return __nf_tables_unregister_hook(net, table, chain, false); | |
354 | } | |
355 | ||
0854db2a FW |
356 | static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans) |
357 | { | |
d59d2f82 | 358 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a | 359 | |
0854db2a FW |
360 | list_add_tail(&trans->list, &nft_net->commit_list); |
361 | } | |
362 | ||
ee01d542 AB |
363 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
364 | { | |
365 | struct nft_trans *trans; | |
366 | ||
367 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
368 | if (trans == NULL) | |
369 | return -ENOMEM; | |
370 | ||
371 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 372 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 | 373 | |
0854db2a | 374 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
375 | return 0; |
376 | } | |
377 | ||
378 | static int nft_deltable(struct nft_ctx *ctx) | |
379 | { | |
380 | int err; | |
381 | ||
382 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
383 | if (err < 0) | |
384 | return err; | |
385 | ||
f2a6d766 | 386 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
387 | return err; |
388 | } | |
389 | ||
66293c46 | 390 | static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
ee01d542 AB |
391 | { |
392 | struct nft_trans *trans; | |
393 | ||
394 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
395 | if (trans == NULL) | |
66293c46 | 396 | return ERR_PTR(-ENOMEM); |
ee01d542 | 397 | |
74cccc3d | 398 | if (msg_type == NFT_MSG_NEWCHAIN) { |
664b0f8c | 399 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 | 400 | |
74cccc3d PNA |
401 | if (ctx->nla[NFTA_CHAIN_ID]) { |
402 | nft_trans_chain_id(trans) = | |
403 | ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID])); | |
404 | } | |
405 | } | |
4bedf9ee | 406 | nft_trans_chain(trans) = ctx->chain; |
0854db2a | 407 | nft_trans_commit_list_add_tail(ctx->net, trans); |
4bedf9ee | 408 | |
66293c46 | 409 | return trans; |
ee01d542 AB |
410 | } |
411 | ||
412 | static int nft_delchain(struct nft_ctx *ctx) | |
413 | { | |
66293c46 | 414 | struct nft_trans *trans; |
ee01d542 | 415 | |
66293c46 FW |
416 | trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
417 | if (IS_ERR(trans)) | |
418 | return PTR_ERR(trans); | |
ee01d542 AB |
419 | |
420 | ctx->table->use--; | |
664b0f8c | 421 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 | 422 | |
66293c46 | 423 | return 0; |
ee01d542 AB |
424 | } |
425 | ||
4bedf9ee | 426 | void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae PNA |
427 | { |
428 | struct nft_expr *expr; | |
429 | ||
430 | expr = nft_expr_first(rule); | |
31cc578a | 431 | while (nft_expr_more(rule, expr)) { |
bb7b40ae PNA |
432 | if (expr->ops->activate) |
433 | expr->ops->activate(ctx, expr); | |
434 | ||
435 | expr = nft_expr_next(expr); | |
436 | } | |
437 | } | |
438 | ||
4bedf9ee PNA |
439 | void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule, |
440 | enum nft_trans_phase phase) | |
bb7b40ae PNA |
441 | { |
442 | struct nft_expr *expr; | |
443 | ||
444 | expr = nft_expr_first(rule); | |
31cc578a | 445 | while (nft_expr_more(rule, expr)) { |
bb7b40ae | 446 | if (expr->ops->deactivate) |
f6ac8585 | 447 | expr->ops->deactivate(ctx, expr, phase); |
bb7b40ae PNA |
448 | |
449 | expr = nft_expr_next(expr); | |
450 | } | |
451 | } | |
452 | ||
ee01d542 AB |
453 | static int |
454 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
455 | { | |
456 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
457 | if (nft_is_active_next(ctx->net, rule)) { |
458 | nft_deactivate_next(ctx->net, rule); | |
ee01d542 AB |
459 | ctx->chain->use--; |
460 | return 0; | |
461 | } | |
462 | return -ENOENT; | |
463 | } | |
464 | ||
465 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
466 | struct nft_rule *rule) | |
467 | { | |
468 | struct nft_trans *trans; | |
469 | ||
470 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
471 | if (trans == NULL) | |
472 | return NULL; | |
473 | ||
1a94e38d PNA |
474 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
475 | nft_trans_rule_id(trans) = | |
476 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
477 | } | |
ee01d542 | 478 | nft_trans_rule(trans) = rule; |
0854db2a | 479 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
480 | |
481 | return trans; | |
482 | } | |
483 | ||
484 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
485 | { | |
63b48c73 | 486 | struct nft_flow_rule *flow; |
ee01d542 AB |
487 | struct nft_trans *trans; |
488 | int err; | |
489 | ||
490 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
491 | if (trans == NULL) | |
492 | return -ENOMEM; | |
493 | ||
63b48c73 PNA |
494 | if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
495 | flow = nft_flow_rule_create(ctx->net, rule); | |
496 | if (IS_ERR(flow)) { | |
497 | nft_trans_destroy(trans); | |
498 | return PTR_ERR(flow); | |
499 | } | |
500 | ||
501 | nft_trans_flow_rule(trans) = flow; | |
502 | } | |
503 | ||
ee01d542 AB |
504 | err = nf_tables_delrule_deactivate(ctx, rule); |
505 | if (err < 0) { | |
506 | nft_trans_destroy(trans); | |
507 | return err; | |
508 | } | |
f6ac8585 | 509 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE); |
ee01d542 AB |
510 | |
511 | return 0; | |
512 | } | |
513 | ||
514 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
515 | { | |
516 | struct nft_rule *rule; | |
517 | int err; | |
518 | ||
519 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
23b7ca4f PNA |
520 | if (!nft_is_active_next(ctx->net, rule)) |
521 | continue; | |
522 | ||
ee01d542 AB |
523 | err = nft_delrule(ctx, rule); |
524 | if (err < 0) | |
525 | return err; | |
526 | } | |
527 | return 0; | |
528 | } | |
529 | ||
123b9961 PNA |
530 | static int __nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
531 | struct nft_set *set, | |
532 | const struct nft_set_desc *desc) | |
ee01d542 AB |
533 | { |
534 | struct nft_trans *trans; | |
535 | ||
536 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
537 | if (trans == NULL) | |
538 | return -ENOMEM; | |
539 | ||
123b9961 | 540 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] && !desc) { |
ee01d542 AB |
541 | nft_trans_set_id(trans) = |
542 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 543 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
544 | } |
545 | nft_trans_set(trans) = set; | |
123b9961 PNA |
546 | if (desc) { |
547 | nft_trans_set_update(trans) = true; | |
548 | nft_trans_set_gc_int(trans) = desc->gc_int; | |
549 | nft_trans_set_timeout(trans) = desc->timeout; | |
550 | } | |
0854db2a | 551 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
552 | |
553 | return 0; | |
554 | } | |
555 | ||
123b9961 PNA |
556 | static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
557 | struct nft_set *set) | |
558 | { | |
559 | return __nft_trans_set_add(ctx, msg_type, set, NULL); | |
560 | } | |
561 | ||
628bd3e4 PNA |
562 | static void nft_setelem_data_deactivate(const struct net *net, |
563 | const struct nft_set *set, | |
564 | struct nft_set_elem *elem); | |
565 | ||
566 | static int nft_mapelem_deactivate(const struct nft_ctx *ctx, | |
567 | struct nft_set *set, | |
568 | const struct nft_set_iter *iter, | |
569 | struct nft_set_elem *elem) | |
570 | { | |
571 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
572 | ||
573 | return 0; | |
574 | } | |
575 | ||
576 | struct nft_set_elem_catchall { | |
577 | struct list_head list; | |
578 | struct rcu_head rcu; | |
579 | void *elem; | |
580 | }; | |
581 | ||
582 | static void nft_map_catchall_deactivate(const struct nft_ctx *ctx, | |
583 | struct nft_set *set) | |
584 | { | |
585 | u8 genmask = nft_genmask_next(ctx->net); | |
586 | struct nft_set_elem_catchall *catchall; | |
587 | struct nft_set_elem elem; | |
588 | struct nft_set_ext *ext; | |
589 | ||
590 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
591 | ext = nft_set_elem_ext(set, catchall->elem); | |
592 | if (!nft_set_elem_active(ext, genmask)) | |
593 | continue; | |
594 | ||
595 | elem.priv = catchall->elem; | |
596 | nft_setelem_data_deactivate(ctx->net, set, &elem); | |
597 | break; | |
598 | } | |
599 | } | |
600 | ||
601 | static void nft_map_deactivate(const struct nft_ctx *ctx, struct nft_set *set) | |
602 | { | |
603 | struct nft_set_iter iter = { | |
604 | .genmask = nft_genmask_next(ctx->net), | |
605 | .fn = nft_mapelem_deactivate, | |
606 | }; | |
607 | ||
608 | set->ops->walk(ctx, set, &iter); | |
609 | WARN_ON_ONCE(iter.err); | |
610 | ||
611 | nft_map_catchall_deactivate(ctx, set); | |
612 | } | |
613 | ||
cd5125d8 | 614 | static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set) |
ee01d542 AB |
615 | { |
616 | int err; | |
617 | ||
618 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
619 | if (err < 0) | |
620 | return err; | |
621 | ||
628bd3e4 PNA |
622 | if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) |
623 | nft_map_deactivate(ctx, set); | |
624 | ||
37a9cc52 | 625 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
626 | ctx->table->use--; |
627 | ||
628 | return err; | |
629 | } | |
630 | ||
e5009240 PNA |
631 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
632 | struct nft_object *obj) | |
633 | { | |
634 | struct nft_trans *trans; | |
635 | ||
636 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
637 | if (trans == NULL) | |
638 | return -ENOMEM; | |
639 | ||
640 | if (msg_type == NFT_MSG_NEWOBJ) | |
641 | nft_activate_next(ctx->net, obj); | |
642 | ||
643 | nft_trans_obj(trans) = obj; | |
0854db2a | 644 | nft_trans_commit_list_add_tail(ctx->net, trans); |
e5009240 PNA |
645 | |
646 | return 0; | |
647 | } | |
648 | ||
649 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
650 | { | |
651 | int err; | |
652 | ||
653 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
654 | if (err < 0) | |
655 | return err; | |
656 | ||
657 | nft_deactivate_next(ctx->net, obj); | |
658 | ctx->table->use--; | |
659 | ||
660 | return err; | |
661 | } | |
662 | ||
3b49e2e9 PNA |
663 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
664 | struct nft_flowtable *flowtable) | |
665 | { | |
666 | struct nft_trans *trans; | |
667 | ||
668 | trans = nft_trans_alloc(ctx, msg_type, | |
669 | sizeof(struct nft_trans_flowtable)); | |
670 | if (trans == NULL) | |
671 | return -ENOMEM; | |
672 | ||
673 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
674 | nft_activate_next(ctx->net, flowtable); | |
675 | ||
2c9e4559 | 676 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); |
3b49e2e9 | 677 | nft_trans_flowtable(trans) = flowtable; |
0854db2a | 678 | nft_trans_commit_list_add_tail(ctx->net, trans); |
3b49e2e9 PNA |
679 | |
680 | return 0; | |
681 | } | |
682 | ||
683 | static int nft_delflowtable(struct nft_ctx *ctx, | |
684 | struct nft_flowtable *flowtable) | |
685 | { | |
686 | int err; | |
687 | ||
688 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
689 | if (err < 0) | |
690 | return err; | |
691 | ||
692 | nft_deactivate_next(ctx->net, flowtable); | |
693 | ctx->table->use--; | |
694 | ||
695 | return err; | |
696 | } | |
697 | ||
34cc9e52 PNA |
698 | static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg) |
699 | { | |
700 | int i; | |
701 | ||
702 | for (i = track->regs[dreg].num_reg; i > 0; i--) | |
703 | __nft_reg_track_cancel(track, dreg - i); | |
704 | } | |
705 | ||
706 | static void __nft_reg_track_update(struct nft_regs_track *track, | |
707 | const struct nft_expr *expr, | |
708 | u8 dreg, u8 num_reg) | |
709 | { | |
710 | track->regs[dreg].selector = expr; | |
711 | track->regs[dreg].bitwise = NULL; | |
712 | track->regs[dreg].num_reg = num_reg; | |
713 | } | |
714 | ||
715 | void nft_reg_track_update(struct nft_regs_track *track, | |
716 | const struct nft_expr *expr, u8 dreg, u8 len) | |
717 | { | |
718 | unsigned int regcount; | |
719 | int i; | |
720 | ||
721 | __nft_reg_track_clobber(track, dreg); | |
722 | ||
723 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
724 | for (i = 0; i < regcount; i++, dreg++) | |
725 | __nft_reg_track_update(track, expr, dreg, i); | |
726 | } | |
727 | EXPORT_SYMBOL_GPL(nft_reg_track_update); | |
728 | ||
729 | void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len) | |
730 | { | |
731 | unsigned int regcount; | |
732 | int i; | |
733 | ||
734 | __nft_reg_track_clobber(track, dreg); | |
735 | ||
736 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
737 | for (i = 0; i < regcount; i++, dreg++) | |
738 | __nft_reg_track_cancel(track, dreg); | |
739 | } | |
740 | EXPORT_SYMBOL_GPL(nft_reg_track_cancel); | |
741 | ||
742 | void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) | |
743 | { | |
744 | track->regs[dreg].selector = NULL; | |
745 | track->regs[dreg].bitwise = NULL; | |
746 | track->regs[dreg].num_reg = 0; | |
747 | } | |
748 | EXPORT_SYMBOL_GPL(__nft_reg_track_cancel); | |
749 | ||
96518518 PM |
750 | /* |
751 | * Tables | |
752 | */ | |
753 | ||
36596dad | 754 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 755 | const struct nlattr *nla, |
6001a930 | 756 | u8 family, u8 genmask, u32 nlpid) |
96518518 | 757 | { |
0854db2a | 758 | struct nftables_pernet *nft_net; |
96518518 PM |
759 | struct nft_table *table; |
760 | ||
cac20fcd PNA |
761 | if (nla == NULL) |
762 | return ERR_PTR(-EINVAL); | |
763 | ||
d59d2f82 | 764 | nft_net = nft_pernet(net); |
0854db2a FW |
765 | list_for_each_entry_rcu(table, &nft_net->tables, list, |
766 | lockdep_is_held(&nft_net->commit_mutex)) { | |
f2a6d766 | 767 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 768 | table->family == family && |
6001a930 PNA |
769 | nft_active_genmask(table, genmask)) { |
770 | if (nft_table_has_owner(table) && | |
53479909 | 771 | nlpid && table->nlpid != nlpid) |
6001a930 PNA |
772 | return ERR_PTR(-EPERM); |
773 | ||
96518518 | 774 | return table; |
6001a930 | 775 | } |
96518518 | 776 | } |
cac20fcd PNA |
777 | |
778 | return ERR_PTR(-ENOENT); | |
96518518 PM |
779 | } |
780 | ||
3ecbfd65 HS |
781 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
782 | const struct nlattr *nla, | |
e31f072f | 783 | u8 genmask, u32 nlpid) |
3ecbfd65 | 784 | { |
0854db2a | 785 | struct nftables_pernet *nft_net; |
3ecbfd65 HS |
786 | struct nft_table *table; |
787 | ||
d59d2f82 | 788 | nft_net = nft_pernet(net); |
0854db2a | 789 | list_for_each_entry(table, &nft_net->tables, list) { |
3ecbfd65 | 790 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && |
e31f072f PNA |
791 | nft_active_genmask(table, genmask)) { |
792 | if (nft_table_has_owner(table) && | |
793 | nlpid && table->nlpid != nlpid) | |
794 | return ERR_PTR(-EPERM); | |
795 | ||
3ecbfd65 | 796 | return table; |
e31f072f | 797 | } |
3ecbfd65 | 798 | } |
3ecbfd65 HS |
799 | |
800 | return ERR_PTR(-ENOENT); | |
801 | } | |
802 | ||
96518518 PM |
803 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
804 | { | |
805 | return ++table->hgenerator; | |
806 | } | |
807 | ||
32537e91 | 808 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 809 | |
82603549 PNA |
810 | static const struct nft_chain_type * |
811 | __nft_chain_type_get(u8 family, enum nft_chain_types type) | |
812 | { | |
813 | if (family >= NFPROTO_NUMPROTO || | |
814 | type >= NFT_CHAIN_T_MAX) | |
815 | return NULL; | |
816 | ||
817 | return chain_type[family][type]; | |
818 | } | |
819 | ||
32537e91 | 820 | static const struct nft_chain_type * |
1ea26cca | 821 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c | 822 | { |
82603549 | 823 | const struct nft_chain_type *type; |
9370761c PNA |
824 | int i; |
825 | ||
baae3e62 | 826 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
82603549 PNA |
827 | type = __nft_chain_type_get(family, i); |
828 | if (!type) | |
829 | continue; | |
830 | if (!nla_strcmp(nla, type->name)) | |
831 | return type; | |
9370761c | 832 | } |
baae3e62 | 833 | return NULL; |
9370761c PNA |
834 | } |
835 | ||
eb014de4 PNA |
836 | struct nft_module_request { |
837 | struct list_head list; | |
838 | char module[MODULE_NAME_LEN]; | |
839 | bool done; | |
840 | }; | |
841 | ||
452238e8 | 842 | #ifdef CONFIG_MODULES |
cefa31a9 FW |
843 | __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, |
844 | ...) | |
452238e8 FW |
845 | { |
846 | char module_name[MODULE_NAME_LEN]; | |
0854db2a | 847 | struct nftables_pernet *nft_net; |
eb014de4 | 848 | struct nft_module_request *req; |
452238e8 FW |
849 | va_list args; |
850 | int ret; | |
851 | ||
452238e8 FW |
852 | va_start(args, fmt); |
853 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | |
854 | va_end(args); | |
9332d27d | 855 | if (ret >= MODULE_NAME_LEN) |
eb014de4 | 856 | return 0; |
452238e8 | 857 | |
d59d2f82 | 858 | nft_net = nft_pernet(net); |
0854db2a | 859 | list_for_each_entry(req, &nft_net->module_list, list) { |
eb014de4 PNA |
860 | if (!strcmp(req->module, module_name)) { |
861 | if (req->done) | |
862 | return 0; | |
ec7470b8 | 863 | |
eb014de4 PNA |
864 | /* A request to load this module already exists. */ |
865 | return -EAGAIN; | |
866 | } | |
867 | } | |
868 | ||
869 | req = kmalloc(sizeof(*req), GFP_KERNEL); | |
870 | if (!req) | |
871 | return -ENOMEM; | |
872 | ||
873 | req->done = false; | |
8556bceb | 874 | strscpy(req->module, module_name, MODULE_NAME_LEN); |
0854db2a | 875 | list_add_tail(&req->list, &nft_net->module_list); |
eb014de4 PNA |
876 | |
877 | return -EAGAIN; | |
452238e8 | 878 | } |
cefa31a9 | 879 | EXPORT_SYMBOL_GPL(nft_request_module); |
452238e8 FW |
880 | #endif |
881 | ||
f102d66b FW |
882 | static void lockdep_nfnl_nft_mutex_not_held(void) |
883 | { | |
884 | #ifdef CONFIG_PROVE_LOCKING | |
c0700dfa FW |
885 | if (debug_locks) |
886 | WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
f102d66b FW |
887 | #endif |
888 | } | |
889 | ||
32537e91 | 890 | static const struct nft_chain_type * |
452238e8 FW |
891 | nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla, |
892 | u8 family, bool autoload) | |
9370761c | 893 | { |
32537e91 | 894 | const struct nft_chain_type *type; |
9370761c | 895 | |
1ea26cca | 896 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
897 | if (type != NULL) |
898 | return type; | |
f102d66b FW |
899 | |
900 | lockdep_nfnl_nft_mutex_not_held(); | |
9370761c | 901 | #ifdef CONFIG_MODULES |
93b0806f | 902 | if (autoload) { |
eb014de4 PNA |
903 | if (nft_request_module(net, "nft-chain-%u-%.*s", family, |
904 | nla_len(nla), | |
905 | (const char *)nla_data(nla)) == -EAGAIN) | |
93b0806f | 906 | return ERR_PTR(-EAGAIN); |
9370761c PNA |
907 | } |
908 | #endif | |
93b0806f | 909 | return ERR_PTR(-ENOENT); |
9370761c PNA |
910 | } |
911 | ||
802b8051 PNA |
912 | static __be16 nft_base_seq(const struct net *net) |
913 | { | |
d59d2f82 | 914 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
915 | |
916 | return htons(nft_net->base_seq & 0xffff); | |
802b8051 PNA |
917 | } |
918 | ||
96518518 | 919 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
920 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
921 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 922 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 923 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
7a81575b JGG |
924 | [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY, |
925 | .len = NFT_USERDATA_MAXLEN } | |
96518518 PM |
926 | }; |
927 | ||
84d7fce6 PNA |
928 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
929 | u32 portid, u32 seq, int event, u32 flags, | |
930 | int family, const struct nft_table *table) | |
96518518 PM |
931 | { |
932 | struct nlmsghdr *nlh; | |
96518518 | 933 | |
dedb67c4 | 934 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
935 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
936 | NFNETLINK_V0, nft_base_seq(net)); | |
937 | if (!nlh) | |
96518518 PM |
938 | goto nla_put_failure; |
939 | ||
9ddf6323 | 940 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
3ecbfd65 HS |
941 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
942 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
943 | NFTA_TABLE_PAD)) | |
96518518 | 944 | goto nla_put_failure; |
28339b21 PNA |
945 | |
946 | if (event == NFT_MSG_DELTABLE) { | |
947 | nlmsg_end(skb, nlh); | |
948 | return 0; | |
949 | } | |
950 | ||
951 | if (nla_put_be32(skb, NFTA_TABLE_FLAGS, | |
952 | htonl(table->flags & NFT_TABLE_F_MASK))) | |
953 | goto nla_put_failure; | |
954 | ||
6001a930 PNA |
955 | if (nft_table_has_owner(table) && |
956 | nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid))) | |
957 | goto nla_put_failure; | |
96518518 | 958 | |
7a81575b JGG |
959 | if (table->udata) { |
960 | if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata)) | |
961 | goto nla_put_failure; | |
962 | } | |
963 | ||
053c095a JB |
964 | nlmsg_end(skb, nlh); |
965 | return 0; | |
96518518 PM |
966 | |
967 | nla_put_failure: | |
968 | nlmsg_trim(skb, nlh); | |
969 | return -1; | |
970 | } | |
971 | ||
67cc570e PNA |
972 | struct nftnl_skb_parms { |
973 | bool report; | |
974 | }; | |
975 | #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb)) | |
976 | ||
977 | static void nft_notify_enqueue(struct sk_buff *skb, bool report, | |
978 | struct list_head *notify_list) | |
979 | { | |
980 | NFT_CB(skb).report = report; | |
981 | list_add_tail(&skb->list, notify_list); | |
982 | } | |
983 | ||
25e94a99 | 984 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 | 985 | { |
0854db2a | 986 | struct nftables_pernet *nft_net; |
96518518 | 987 | struct sk_buff *skb; |
6fb721cf | 988 | u16 flags = 0; |
96518518 PM |
989 | int err; |
990 | ||
128ad332 PNA |
991 | if (!ctx->report && |
992 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 993 | return; |
96518518 | 994 | |
96518518 PM |
995 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
996 | if (skb == NULL) | |
997 | goto err; | |
998 | ||
6fb721cf PNA |
999 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
1000 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
1001 | ||
84d7fce6 | 1002 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 1003 | event, flags, ctx->family, ctx->table); |
96518518 PM |
1004 | if (err < 0) { |
1005 | kfree_skb(skb); | |
1006 | goto err; | |
1007 | } | |
1008 | ||
d59d2f82 | 1009 | nft_net = nft_pernet(ctx->net); |
0854db2a | 1010 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 1011 | return; |
96518518 | 1012 | err: |
25e94a99 | 1013 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1014 | } |
1015 | ||
1016 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
1017 | struct netlink_callback *cb) | |
1018 | { | |
1019 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
0854db2a | 1020 | struct nftables_pernet *nft_net; |
96518518 PM |
1021 | const struct nft_table *table; |
1022 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 1023 | struct net *net = sock_net(skb->sk); |
96518518 PM |
1024 | int family = nfmsg->nfgen_family; |
1025 | ||
e688a7f8 | 1026 | rcu_read_lock(); |
d59d2f82 | 1027 | nft_net = nft_pernet(net); |
34002783 | 1028 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 1029 | |
0854db2a | 1030 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 1031 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1032 | continue; |
1033 | ||
36596dad PNA |
1034 | if (idx < s_idx) |
1035 | goto cont; | |
1036 | if (idx > s_idx) | |
1037 | memset(&cb->args[1], 0, | |
1038 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1039 | if (!nft_is_active(net, table)) | |
1040 | continue; | |
1041 | if (nf_tables_fill_table_info(skb, net, | |
1042 | NETLINK_CB(cb->skb).portid, | |
1043 | cb->nlh->nlmsg_seq, | |
1044 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 1045 | table->family, table) < 0) |
36596dad PNA |
1046 | goto done; |
1047 | ||
1048 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 1049 | cont: |
36596dad | 1050 | idx++; |
96518518 PM |
1051 | } |
1052 | done: | |
e688a7f8 | 1053 | rcu_read_unlock(); |
96518518 PM |
1054 | cb->args[0] = idx; |
1055 | return skb->len; | |
1056 | } | |
1057 | ||
d9adf22a FW |
1058 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
1059 | const struct nlmsghdr *nlh, | |
1060 | struct netlink_dump_control *c) | |
1061 | { | |
1062 | int err; | |
1063 | ||
1064 | if (!try_module_get(THIS_MODULE)) | |
1065 | return -EINVAL; | |
1066 | ||
1067 | rcu_read_unlock(); | |
1068 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
1069 | rcu_read_lock(); | |
1070 | module_put(THIS_MODULE); | |
1071 | ||
1072 | return err; | |
1073 | } | |
1074 | ||
1075 | /* called with rcu_read_lock held */ | |
797d4980 PNA |
1076 | static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info, |
1077 | const struct nlattr * const nla[]) | |
96518518 | 1078 | { |
797d4980 PNA |
1079 | struct netlink_ext_ack *extack = info->extack; |
1080 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 1081 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 1082 | const struct nft_table *table; |
797d4980 | 1083 | struct net *net = info->net; |
96518518 | 1084 | struct sk_buff *skb2; |
96518518 PM |
1085 | int err; |
1086 | ||
797d4980 | 1087 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
1088 | struct netlink_dump_control c = { |
1089 | .dump = nf_tables_dump_tables, | |
d9adf22a | 1090 | .module = THIS_MODULE, |
96518518 | 1091 | }; |
d9adf22a | 1092 | |
797d4980 | 1093 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
1094 | } |
1095 | ||
6001a930 | 1096 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0); |
36dd1bcc PNA |
1097 | if (IS_ERR(table)) { |
1098 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 1099 | return PTR_ERR(table); |
36dd1bcc | 1100 | } |
96518518 | 1101 | |
d9adf22a | 1102 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1103 | if (!skb2) |
1104 | return -ENOMEM; | |
1105 | ||
84d7fce6 | 1106 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
1107 | info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE, |
1108 | 0, family, table); | |
96518518 | 1109 | if (err < 0) |
ee921183 | 1110 | goto err_fill_table_info; |
96518518 | 1111 | |
ee921183 | 1112 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 1113 | |
ee921183 | 1114 | err_fill_table_info: |
96518518 PM |
1115 | kfree_skb(skb2); |
1116 | return err; | |
1117 | } | |
1118 | ||
c9c17211 | 1119 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
1120 | { |
1121 | struct nft_chain *chain; | |
1122 | u32 i = 0; | |
1123 | ||
1124 | list_for_each_entry(chain, &table->chains, list) { | |
1125 | if (!nft_is_active_next(net, chain)) | |
1126 | continue; | |
f323d954 | 1127 | if (!nft_is_base_chain(chain)) |
10435c11 F |
1128 | continue; |
1129 | ||
1130 | if (cnt && i++ == cnt) | |
1131 | break; | |
1132 | ||
1e9451cb | 1133 | nf_tables_unregister_hook(net, table, chain); |
10435c11 F |
1134 | } |
1135 | } | |
1136 | ||
c9c17211 | 1137 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
1138 | { |
1139 | struct nft_chain *chain; | |
1140 | int err, i = 0; | |
1141 | ||
1142 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1143 | if (!nft_is_active_next(net, chain)) |
1144 | continue; | |
f323d954 | 1145 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
1146 | continue; |
1147 | ||
1e9451cb | 1148 | err = nf_tables_register_hook(net, table, chain); |
9ddf6323 | 1149 | if (err < 0) |
d54725cd | 1150 | goto err_register_hooks; |
9ddf6323 PNA |
1151 | |
1152 | i++; | |
1153 | } | |
1154 | return 0; | |
d54725cd PNA |
1155 | |
1156 | err_register_hooks: | |
10435c11 | 1157 | if (i) |
c9c17211 | 1158 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
1159 | return err; |
1160 | } | |
1161 | ||
c9c17211 | 1162 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 1163 | { |
179d9ba5 | 1164 | table->flags &= ~NFT_TABLE_F_DORMANT; |
c9c17211 | 1165 | nft_table_disable(net, table, 0); |
179d9ba5 | 1166 | table->flags |= NFT_TABLE_F_DORMANT; |
9ddf6323 PNA |
1167 | } |
1168 | ||
179d9ba5 PNA |
1169 | #define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1) |
1170 | #define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0) | |
1171 | #define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1) | |
1172 | #define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \ | |
1173 | __NFT_TABLE_F_WAS_AWAKEN) | |
0ce7cf41 | 1174 | |
e1aaca93 | 1175 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 1176 | { |
55dd6f93 | 1177 | struct nft_trans *trans; |
e1aaca93 | 1178 | u32 flags; |
179d9ba5 | 1179 | int ret; |
9ddf6323 | 1180 | |
e1aaca93 PNA |
1181 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
1182 | return 0; | |
9ddf6323 | 1183 | |
e1aaca93 | 1184 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
6001a930 PNA |
1185 | if (flags & ~NFT_TABLE_F_MASK) |
1186 | return -EOPNOTSUPP; | |
e1aaca93 | 1187 | |
63283dd2 PNA |
1188 | if (flags == ctx->table->flags) |
1189 | return 0; | |
1190 | ||
9cc0001a PNA |
1191 | if ((nft_table_has_owner(ctx->table) && |
1192 | !(flags & NFT_TABLE_F_OWNER)) || | |
1193 | (!nft_table_has_owner(ctx->table) && | |
1194 | flags & NFT_TABLE_F_OWNER)) | |
1195 | return -EOPNOTSUPP; | |
1196 | ||
55dd6f93 PNA |
1197 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
1198 | sizeof(struct nft_trans_table)); | |
1199 | if (trans == NULL) | |
1200 | return -ENOMEM; | |
9ddf6323 | 1201 | |
e1aaca93 PNA |
1202 | if ((flags & NFT_TABLE_F_DORMANT) && |
1203 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
179d9ba5 PNA |
1204 | ctx->table->flags |= NFT_TABLE_F_DORMANT; |
1205 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) | |
1206 | ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN; | |
e1aaca93 PNA |
1207 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
1208 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
179d9ba5 PNA |
1209 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
1210 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) { | |
1211 | ret = nf_tables_table_enable(ctx->net, ctx->table); | |
1212 | if (ret < 0) | |
1213 | goto err_register_hooks; | |
1214 | ||
1215 | ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT; | |
1216 | } | |
9ddf6323 PNA |
1217 | } |
1218 | ||
55dd6f93 | 1219 | nft_trans_table_update(trans) = true; |
0854db2a | 1220 | nft_trans_commit_list_add_tail(ctx->net, trans); |
179d9ba5 | 1221 | |
55dd6f93 | 1222 | return 0; |
179d9ba5 PNA |
1223 | |
1224 | err_register_hooks: | |
55dd6f93 | 1225 | nft_trans_destroy(trans); |
9ddf6323 PNA |
1226 | return ret; |
1227 | } | |
1228 | ||
1b2470e5 FW |
1229 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
1230 | { | |
1231 | const char *name = data; | |
1232 | ||
1233 | return jhash(name, strlen(name), seed); | |
1234 | } | |
1235 | ||
1236 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
1237 | { | |
1238 | const struct nft_chain *chain = data; | |
1239 | ||
1240 | return nft_chain_hash(chain->name, 0, seed); | |
1241 | } | |
1242 | ||
1243 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
1244 | const void *ptr) | |
1245 | { | |
1246 | const struct nft_chain *chain = ptr; | |
1247 | const char *name = arg->key; | |
1248 | ||
1249 | return strcmp(chain->name, name); | |
1250 | } | |
1251 | ||
4d44175a FW |
1252 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed) |
1253 | { | |
1254 | const struct nft_object_hash_key *k = data; | |
1255 | ||
1256 | seed ^= hash_ptr(k->table, 32); | |
1257 | ||
1258 | return jhash(k->name, strlen(k->name), seed); | |
1259 | } | |
1260 | ||
1261 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed) | |
1262 | { | |
1263 | const struct nft_object *obj = data; | |
1264 | ||
1265 | return nft_objname_hash(&obj->key, 0, seed); | |
1266 | } | |
1267 | ||
1268 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg, | |
1269 | const void *ptr) | |
1270 | { | |
1271 | const struct nft_object_hash_key *k = arg->key; | |
1272 | const struct nft_object *obj = ptr; | |
1273 | ||
1274 | if (obj->key.table != k->table) | |
1275 | return -1; | |
1276 | ||
1277 | return strcmp(obj->key.name, k->name); | |
1278 | } | |
1279 | ||
f1082dd3 PS |
1280 | static bool nft_supported_family(u8 family) |
1281 | { | |
1282 | return false | |
1283 | #ifdef CONFIG_NF_TABLES_INET | |
1284 | || family == NFPROTO_INET | |
1285 | #endif | |
1286 | #ifdef CONFIG_NF_TABLES_IPV4 | |
1287 | || family == NFPROTO_IPV4 | |
1288 | #endif | |
1289 | #ifdef CONFIG_NF_TABLES_ARP | |
1290 | || family == NFPROTO_ARP | |
1291 | #endif | |
1292 | #ifdef CONFIG_NF_TABLES_NETDEV | |
1293 | || family == NFPROTO_NETDEV | |
1294 | #endif | |
1295 | #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE) | |
1296 | || family == NFPROTO_BRIDGE | |
1297 | #endif | |
1298 | #ifdef CONFIG_NF_TABLES_IPV6 | |
1299 | || family == NFPROTO_IPV6 | |
1300 | #endif | |
1301 | ; | |
1302 | } | |
1303 | ||
7dab8ee3 PNA |
1304 | static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, |
1305 | const struct nlattr * const nla[]) | |
96518518 | 1306 | { |
7dab8ee3 | 1307 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
1308 | struct netlink_ext_ack *extack = info->extack; |
1309 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1310 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1311 | struct net *net = info->net; |
36dd1bcc PNA |
1312 | const struct nlattr *attr; |
1313 | struct nft_table *table; | |
e1aaca93 | 1314 | struct nft_ctx ctx; |
7a81575b | 1315 | u32 flags = 0; |
55dd6f93 | 1316 | int err; |
96518518 | 1317 | |
f1082dd3 PS |
1318 | if (!nft_supported_family(family)) |
1319 | return -EOPNOTSUPP; | |
1320 | ||
0854db2a | 1321 | lockdep_assert_held(&nft_net->commit_mutex); |
36dd1bcc | 1322 | attr = nla[NFTA_TABLE_NAME]; |
6001a930 PNA |
1323 | table = nft_table_lookup(net, attr, family, genmask, |
1324 | NETLINK_CB(skb).portid); | |
96518518 PM |
1325 | if (IS_ERR(table)) { |
1326 | if (PTR_ERR(table) != -ENOENT) | |
1327 | return PTR_ERR(table); | |
1a28ad74 | 1328 | } else { |
7dab8ee3 | 1329 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 1330 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1331 | return -EEXIST; |
36dd1bcc | 1332 | } |
7dab8ee3 | 1333 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 | 1334 | return -EOPNOTSUPP; |
e1aaca93 | 1335 | |
7dab8ee3 PNA |
1336 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
1337 | ||
e1aaca93 | 1338 | return nf_tables_updtable(&ctx); |
96518518 PM |
1339 | } |
1340 | ||
c5c1f975 PM |
1341 | if (nla[NFTA_TABLE_FLAGS]) { |
1342 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
6001a930 PNA |
1343 | if (flags & ~NFT_TABLE_F_MASK) |
1344 | return -EOPNOTSUPP; | |
c5c1f975 PM |
1345 | } |
1346 | ||
ffdb210e | 1347 | err = -ENOMEM; |
33758c89 | 1348 | table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT); |
ffdb210e | 1349 | if (table == NULL) |
98319cb9 | 1350 | goto err_kzalloc; |
96518518 | 1351 | |
00c320f9 | 1352 | table->validate_state = NFT_VALIDATE_SKIP; |
33758c89 | 1353 | table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT); |
e46abbcc | 1354 | if (table->name == NULL) |
98319cb9 | 1355 | goto err_strdup; |
e46abbcc | 1356 | |
7a81575b | 1357 | if (nla[NFTA_TABLE_USERDATA]) { |
33758c89 | 1358 | table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT); |
7a81575b JGG |
1359 | if (table->udata == NULL) |
1360 | goto err_table_udata; | |
1361 | ||
85db827a | 1362 | table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]); |
7a81575b JGG |
1363 | } |
1364 | ||
1b2470e5 FW |
1365 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
1366 | if (err) | |
1367 | goto err_chain_ht; | |
1368 | ||
96518518 | 1369 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 1370 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 1371 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 1372 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 1373 | table->family = family; |
c5c1f975 | 1374 | table->flags = flags; |
ab482c6b | 1375 | table->handle = ++nft_net->table_handle; |
6001a930 PNA |
1376 | if (table->flags & NFT_TABLE_F_OWNER) |
1377 | table->nlpid = NETLINK_CB(skb).portid; | |
9ddf6323 | 1378 | |
7dab8ee3 | 1379 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
55dd6f93 | 1380 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 1381 | if (err < 0) |
98319cb9 | 1382 | goto err_trans; |
ffdb210e | 1383 | |
0854db2a | 1384 | list_add_tail_rcu(&table->list, &nft_net->tables); |
96518518 | 1385 | return 0; |
98319cb9 | 1386 | err_trans: |
1b2470e5 FW |
1387 | rhltable_destroy(&table->chains_ht); |
1388 | err_chain_ht: | |
7a81575b JGG |
1389 | kfree(table->udata); |
1390 | err_table_udata: | |
e46abbcc | 1391 | kfree(table->name); |
98319cb9 | 1392 | err_strdup: |
ffdb210e | 1393 | kfree(table); |
98319cb9 | 1394 | err_kzalloc: |
ffdb210e | 1395 | return err; |
96518518 PM |
1396 | } |
1397 | ||
b9ac12ef AB |
1398 | static int nft_flush_table(struct nft_ctx *ctx) |
1399 | { | |
3b49e2e9 | 1400 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 1401 | struct nft_chain *chain, *nc; |
e5009240 | 1402 | struct nft_object *obj, *ne; |
b9ac12ef | 1403 | struct nft_set *set, *ns; |
3b49e2e9 | 1404 | int err; |
b9ac12ef | 1405 | |
a2f18db0 | 1406 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
1407 | if (!nft_is_active_next(ctx->net, chain)) |
1408 | continue; | |
1409 | ||
d0e2c7de PNA |
1410 | if (nft_chain_is_bound(chain)) |
1411 | continue; | |
1412 | ||
b9ac12ef AB |
1413 | ctx->chain = chain; |
1414 | ||
1415 | err = nft_delrule_by_chain(ctx); | |
1416 | if (err < 0) | |
1417 | goto out; | |
b9ac12ef AB |
1418 | } |
1419 | ||
1420 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
1421 | if (!nft_is_active_next(ctx->net, set)) |
1422 | continue; | |
1423 | ||
408070d6 | 1424 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
1425 | !list_empty(&set->bindings)) |
1426 | continue; | |
1427 | ||
1428 | err = nft_delset(ctx, set); | |
1429 | if (err < 0) | |
1430 | goto out; | |
1431 | } | |
1432 | ||
3b49e2e9 | 1433 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
335178d5 FW |
1434 | if (!nft_is_active_next(ctx->net, flowtable)) |
1435 | continue; | |
1436 | ||
3b49e2e9 PNA |
1437 | err = nft_delflowtable(ctx, flowtable); |
1438 | if (err < 0) | |
1439 | goto out; | |
1440 | } | |
1441 | ||
e5009240 | 1442 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
335178d5 FW |
1443 | if (!nft_is_active_next(ctx->net, obj)) |
1444 | continue; | |
1445 | ||
e5009240 PNA |
1446 | err = nft_delobj(ctx, obj); |
1447 | if (err < 0) | |
1448 | goto out; | |
1449 | } | |
1450 | ||
a2f18db0 | 1451 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
1452 | if (!nft_is_active_next(ctx->net, chain)) |
1453 | continue; | |
1454 | ||
d0e2c7de PNA |
1455 | if (nft_chain_is_bound(chain)) |
1456 | continue; | |
1457 | ||
a2f18db0 PNA |
1458 | ctx->chain = chain; |
1459 | ||
1460 | err = nft_delchain(ctx); | |
1461 | if (err < 0) | |
1462 | goto out; | |
1463 | } | |
1464 | ||
b9ac12ef AB |
1465 | err = nft_deltable(ctx); |
1466 | out: | |
1467 | return err; | |
1468 | } | |
1469 | ||
1470 | static int nft_flush(struct nft_ctx *ctx, int family) | |
1471 | { | |
d59d2f82 | 1472 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
b9ac12ef | 1473 | const struct nlattr * const *nla = ctx->nla; |
d59d2f82 | 1474 | struct nft_table *table, *nt; |
b9ac12ef AB |
1475 | int err = 0; |
1476 | ||
0854db2a | 1477 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
98319cb9 | 1478 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
1479 | continue; |
1480 | ||
98319cb9 | 1481 | ctx->family = table->family; |
f2a6d766 | 1482 | |
36596dad PNA |
1483 | if (!nft_is_active_next(ctx->net, table)) |
1484 | continue; | |
b9ac12ef | 1485 | |
6001a930 PNA |
1486 | if (nft_table_has_owner(table) && table->nlpid != ctx->portid) |
1487 | continue; | |
1488 | ||
36596dad PNA |
1489 | if (nla[NFTA_TABLE_NAME] && |
1490 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
1491 | continue; | |
b9ac12ef | 1492 | |
36596dad PNA |
1493 | ctx->table = table; |
1494 | ||
1495 | err = nft_flush_table(ctx); | |
1496 | if (err < 0) | |
1497 | goto out; | |
b9ac12ef AB |
1498 | } |
1499 | out: | |
1500 | return err; | |
1501 | } | |
1502 | ||
7dab8ee3 PNA |
1503 | static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info, |
1504 | const struct nlattr * const nla[]) | |
96518518 | 1505 | { |
7dab8ee3 PNA |
1506 | struct netlink_ext_ack *extack = info->extack; |
1507 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1508 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1509 | struct net *net = info->net; |
36dd1bcc PNA |
1510 | const struct nlattr *attr; |
1511 | struct nft_table *table; | |
55dd6f93 | 1512 | struct nft_ctx ctx; |
96518518 | 1513 | |
7dab8ee3 | 1514 | nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
1515 | if (family == AF_UNSPEC || |
1516 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
1517 | return nft_flush(&ctx, family); |
1518 | ||
36dd1bcc PNA |
1519 | if (nla[NFTA_TABLE_HANDLE]) { |
1520 | attr = nla[NFTA_TABLE_HANDLE]; | |
e31f072f PNA |
1521 | table = nft_table_lookup_byhandle(net, attr, genmask, |
1522 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
1523 | } else { |
1524 | attr = nla[NFTA_TABLE_NAME]; | |
6001a930 PNA |
1525 | table = nft_table_lookup(net, attr, family, genmask, |
1526 | NETLINK_CB(skb).portid); | |
36dd1bcc | 1527 | } |
3ecbfd65 | 1528 | |
36dd1bcc | 1529 | if (IS_ERR(table)) { |
f80a612d FFM |
1530 | if (PTR_ERR(table) == -ENOENT && |
1531 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYTABLE) | |
1532 | return 0; | |
1533 | ||
36dd1bcc | 1534 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1535 | return PTR_ERR(table); |
36dd1bcc | 1536 | } |
96518518 | 1537 | |
7dab8ee3 | 1538 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
a8278400 PNA |
1539 | table->use > 0) |
1540 | return -EBUSY; | |
1541 | ||
98319cb9 | 1542 | ctx.family = family; |
b9ac12ef | 1543 | ctx.table = table; |
55dd6f93 | 1544 | |
b9ac12ef | 1545 | return nft_flush_table(&ctx); |
96518518 PM |
1546 | } |
1547 | ||
55dd6f93 PNA |
1548 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
1549 | { | |
fa5950e4 FW |
1550 | if (WARN_ON(ctx->table->use > 0)) |
1551 | return; | |
4fefee57 | 1552 | |
1b2470e5 | 1553 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 1554 | kfree(ctx->table->name); |
bc7a7082 | 1555 | kfree(ctx->table->udata); |
55dd6f93 | 1556 | kfree(ctx->table); |
55dd6f93 PNA |
1557 | } |
1558 | ||
cc07eeb0 | 1559 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1560 | { |
96518518 | 1561 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
82603549 | 1562 | if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) { |
cc07eeb0 PNA |
1563 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1564 | return; | |
96518518 | 1565 | } |
9370761c | 1566 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 1567 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 1568 | } |
9370761c | 1569 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 1570 | |
32537e91 | 1571 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1572 | { |
96518518 | 1573 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 1574 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
1575 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1576 | } | |
9370761c | 1577 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
1578 | |
1579 | /* | |
1580 | * Chains | |
1581 | */ | |
1582 | ||
1583 | static struct nft_chain * | |
cac20fcd | 1584 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1585 | { |
1586 | struct nft_chain *chain; | |
1587 | ||
1588 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1589 | if (chain->handle == handle && |
1590 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1591 | return chain; |
1592 | } | |
1593 | ||
1594 | return ERR_PTR(-ENOENT); | |
1595 | } | |
1596 | ||
4d44175a | 1597 | static bool lockdep_commit_lock_is_held(const struct net *net) |
f102d66b FW |
1598 | { |
1599 | #ifdef CONFIG_PROVE_LOCKING | |
d59d2f82 | 1600 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
1601 | |
1602 | return lockdep_is_held(&nft_net->commit_mutex); | |
f102d66b FW |
1603 | #else |
1604 | return true; | |
1605 | #endif | |
1606 | } | |
1607 | ||
1608 | static struct nft_chain *nft_chain_lookup(struct net *net, | |
1609 | struct nft_table *table, | |
cac20fcd | 1610 | const struct nlattr *nla, u8 genmask) |
96518518 | 1611 | { |
1b2470e5 FW |
1612 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1613 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1614 | struct nft_chain *chain; |
1615 | ||
1616 | if (nla == NULL) | |
1617 | return ERR_PTR(-EINVAL); | |
1618 | ||
872f6903 | 1619 | nla_strscpy(search, nla, sizeof(search)); |
96518518 | 1620 | |
1b2470e5 | 1621 | WARN_ON(!rcu_read_lock_held() && |
f102d66b | 1622 | !lockdep_commit_lock_is_held(net)); |
1b2470e5 FW |
1623 | |
1624 | chain = ERR_PTR(-ENOENT); | |
1625 | rcu_read_lock(); | |
1626 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1627 | if (!list) | |
1628 | goto out_unlock; | |
1629 | ||
1630 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1631 | if (nft_active_genmask(chain, genmask)) | |
1632 | goto out_unlock; | |
1633 | } | |
1634 | chain = ERR_PTR(-ENOENT); | |
1635 | out_unlock: | |
1636 | rcu_read_unlock(); | |
1637 | return chain; | |
96518518 PM |
1638 | } |
1639 | ||
1640 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1641 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1642 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1643 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1644 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1645 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1646 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1647 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
9332d27d FW |
1648 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, |
1649 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
0ca743a5 | 1650 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
c9626a2c | 1651 | [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, |
74cccc3d | 1652 | [NFTA_CHAIN_ID] = { .type = NLA_U32 }, |
002f2176 JGG |
1653 | [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY, |
1654 | .len = NFT_USERDATA_MAXLEN }, | |
96518518 PM |
1655 | }; |
1656 | ||
1657 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1658 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1659 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1660 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1661 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1662 | }; |
1663 | ||
0ca743a5 PNA |
1664 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1665 | { | |
1666 | struct nft_stats *cpu_stats, total; | |
1667 | struct nlattr *nest; | |
ce355e20 ED |
1668 | unsigned int seq; |
1669 | u64 pkts, bytes; | |
0ca743a5 PNA |
1670 | int cpu; |
1671 | ||
edbd82c5 FW |
1672 | if (!stats) |
1673 | return 0; | |
1674 | ||
0ca743a5 PNA |
1675 | memset(&total, 0, sizeof(total)); |
1676 | for_each_possible_cpu(cpu) { | |
1677 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 | 1678 | do { |
d120d1a6 | 1679 | seq = u64_stats_fetch_begin(&cpu_stats->syncp); |
ce355e20 ED |
1680 | pkts = cpu_stats->pkts; |
1681 | bytes = cpu_stats->bytes; | |
d120d1a6 | 1682 | } while (u64_stats_fetch_retry(&cpu_stats->syncp, seq)); |
ce355e20 ED |
1683 | total.pkts += pkts; |
1684 | total.bytes += bytes; | |
0ca743a5 | 1685 | } |
ae0be8de | 1686 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS); |
0ca743a5 PNA |
1687 | if (nest == NULL) |
1688 | goto nla_put_failure; | |
1689 | ||
b46f6ded ND |
1690 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
1691 | NFTA_COUNTER_PAD) || | |
1692 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
1693 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
1694 | goto nla_put_failure; |
1695 | ||
1696 | nla_nest_end(skb, nest); | |
1697 | return 0; | |
1698 | ||
1699 | nla_put_failure: | |
1700 | return -ENOSPC; | |
1701 | } | |
1702 | ||
d54725cd | 1703 | static int nft_dump_basechain_hook(struct sk_buff *skb, int family, |
b9703ed4 PNA |
1704 | const struct nft_base_chain *basechain, |
1705 | const struct list_head *hook_list) | |
d54725cd PNA |
1706 | { |
1707 | const struct nf_hook_ops *ops = &basechain->ops; | |
1708 | struct nft_hook *hook, *first = NULL; | |
1709 | struct nlattr *nest, *nest_devs; | |
1710 | int n = 0; | |
1711 | ||
1712 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK); | |
1713 | if (nest == NULL) | |
1714 | goto nla_put_failure; | |
1715 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1716 | goto nla_put_failure; | |
1717 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1718 | goto nla_put_failure; | |
1719 | ||
d3519cb8 | 1720 | if (nft_base_chain_netdev(family, ops->hooknum)) { |
d54725cd | 1721 | nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS); |
bd058763 GI |
1722 | if (!nest_devs) |
1723 | goto nla_put_failure; | |
b9703ed4 PNA |
1724 | |
1725 | if (!hook_list) | |
1726 | hook_list = &basechain->hook_list; | |
1727 | ||
1728 | list_for_each_entry(hook, hook_list, list) { | |
d54725cd PNA |
1729 | if (!first) |
1730 | first = hook; | |
1731 | ||
1732 | if (nla_put_string(skb, NFTA_DEVICE_NAME, | |
1733 | hook->ops.dev->name)) | |
1734 | goto nla_put_failure; | |
1735 | n++; | |
1736 | } | |
1737 | nla_nest_end(skb, nest_devs); | |
1738 | ||
1739 | if (n == 1 && | |
1740 | nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name)) | |
1741 | goto nla_put_failure; | |
1742 | } | |
1743 | nla_nest_end(skb, nest); | |
1744 | ||
1745 | return 0; | |
1746 | nla_put_failure: | |
1747 | return -1; | |
1748 | } | |
1749 | ||
84d7fce6 PNA |
1750 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
1751 | u32 portid, u32 seq, int event, u32 flags, | |
1752 | int family, const struct nft_table *table, | |
b9703ed4 PNA |
1753 | const struct nft_chain *chain, |
1754 | const struct list_head *hook_list) | |
96518518 PM |
1755 | { |
1756 | struct nlmsghdr *nlh; | |
96518518 | 1757 | |
dedb67c4 | 1758 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
1759 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
1760 | NFNETLINK_V0, nft_base_seq(net)); | |
1761 | if (!nlh) | |
96518518 PM |
1762 | goto nla_put_failure; |
1763 | ||
28339b21 PNA |
1764 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name) || |
1765 | nla_put_string(skb, NFTA_CHAIN_NAME, chain->name) || | |
1766 | nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), | |
b46f6ded | 1767 | NFTA_CHAIN_PAD)) |
96518518 | 1768 | goto nla_put_failure; |
28339b21 | 1769 | |
7d937b10 | 1770 | if (event == NFT_MSG_DELCHAIN && !hook_list) { |
28339b21 PNA |
1771 | nlmsg_end(skb, nlh); |
1772 | return 0; | |
1773 | } | |
96518518 | 1774 | |
f323d954 | 1775 | if (nft_is_base_chain(chain)) { |
0ca743a5 | 1776 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
edbd82c5 | 1777 | struct nft_stats __percpu *stats; |
0ca743a5 | 1778 | |
b9703ed4 | 1779 | if (nft_dump_basechain_hook(skb, family, basechain, hook_list)) |
2cbce139 | 1780 | goto nla_put_failure; |
9370761c | 1781 | |
0ca743a5 PNA |
1782 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1783 | htonl(basechain->policy))) | |
1784 | goto nla_put_failure; | |
1785 | ||
baae3e62 PM |
1786 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1787 | goto nla_put_failure; | |
0ca743a5 | 1788 | |
edbd82c5 FW |
1789 | stats = rcu_dereference_check(basechain->stats, |
1790 | lockdep_commit_lock_is_held(net)); | |
1791 | if (nft_dump_stats(skb, stats)) | |
0ca743a5 | 1792 | goto nla_put_failure; |
96518518 PM |
1793 | } |
1794 | ||
d0e2c7de PNA |
1795 | if (chain->flags && |
1796 | nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags))) | |
1797 | goto nla_put_failure; | |
1798 | ||
0ca743a5 PNA |
1799 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1800 | goto nla_put_failure; | |
1801 | ||
002f2176 JGG |
1802 | if (chain->udata && |
1803 | nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata)) | |
1804 | goto nla_put_failure; | |
1805 | ||
053c095a JB |
1806 | nlmsg_end(skb, nlh); |
1807 | return 0; | |
96518518 PM |
1808 | |
1809 | nla_put_failure: | |
1810 | nlmsg_trim(skb, nlh); | |
1811 | return -1; | |
1812 | } | |
1813 | ||
b9703ed4 PNA |
1814 | static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event, |
1815 | const struct list_head *hook_list) | |
96518518 | 1816 | { |
0854db2a | 1817 | struct nftables_pernet *nft_net; |
96518518 | 1818 | struct sk_buff *skb; |
6fb721cf | 1819 | u16 flags = 0; |
96518518 PM |
1820 | int err; |
1821 | ||
128ad332 PNA |
1822 | if (!ctx->report && |
1823 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1824 | return; |
96518518 | 1825 | |
96518518 PM |
1826 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1827 | if (skb == NULL) | |
1828 | goto err; | |
1829 | ||
6fb721cf PNA |
1830 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
1831 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
1832 | ||
84d7fce6 | 1833 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 1834 | event, flags, ctx->family, ctx->table, |
b9703ed4 | 1835 | ctx->chain, hook_list); |
96518518 PM |
1836 | if (err < 0) { |
1837 | kfree_skb(skb); | |
1838 | goto err; | |
1839 | } | |
1840 | ||
d59d2f82 | 1841 | nft_net = nft_pernet(ctx->net); |
0854db2a | 1842 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 1843 | return; |
96518518 | 1844 | err: |
25e94a99 | 1845 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1846 | } |
1847 | ||
1848 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1849 | struct netlink_callback *cb) | |
1850 | { | |
1851 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 | 1852 | unsigned int idx = 0, s_idx = cb->args[0]; |
99633ab2 | 1853 | struct net *net = sock_net(skb->sk); |
96518518 | 1854 | int family = nfmsg->nfgen_family; |
0854db2a | 1855 | struct nftables_pernet *nft_net; |
d59d2f82 PNA |
1856 | const struct nft_table *table; |
1857 | const struct nft_chain *chain; | |
96518518 | 1858 | |
e688a7f8 | 1859 | rcu_read_lock(); |
d59d2f82 | 1860 | nft_net = nft_pernet(net); |
34002783 | 1861 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 1862 | |
0854db2a | 1863 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 1864 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1865 | continue; |
1866 | ||
36596dad PNA |
1867 | list_for_each_entry_rcu(chain, &table->chains, list) { |
1868 | if (idx < s_idx) | |
1869 | goto cont; | |
1870 | if (idx > s_idx) | |
1871 | memset(&cb->args[1], 0, | |
1872 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1873 | if (!nft_is_active(net, chain)) | |
1874 | continue; | |
1875 | if (nf_tables_fill_chain_info(skb, net, | |
1876 | NETLINK_CB(cb->skb).portid, | |
1877 | cb->nlh->nlmsg_seq, | |
1878 | NFT_MSG_NEWCHAIN, | |
1879 | NLM_F_MULTI, | |
98319cb9 | 1880 | table->family, table, |
b9703ed4 | 1881 | chain, NULL) < 0) |
36596dad | 1882 | goto done; |
38e029f1 | 1883 | |
36596dad | 1884 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
96518518 | 1885 | cont: |
36596dad | 1886 | idx++; |
96518518 PM |
1887 | } |
1888 | } | |
1889 | done: | |
e688a7f8 | 1890 | rcu_read_unlock(); |
96518518 PM |
1891 | cb->args[0] = idx; |
1892 | return skb->len; | |
1893 | } | |
1894 | ||
d9adf22a | 1895 | /* called with rcu_read_lock held */ |
797d4980 PNA |
1896 | static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info, |
1897 | const struct nlattr * const nla[]) | |
96518518 | 1898 | { |
797d4980 PNA |
1899 | struct netlink_ext_ack *extack = info->extack; |
1900 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 1901 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 1902 | const struct nft_chain *chain; |
797d4980 | 1903 | struct net *net = info->net; |
1b2470e5 | 1904 | struct nft_table *table; |
96518518 | 1905 | struct sk_buff *skb2; |
96518518 PM |
1906 | int err; |
1907 | ||
797d4980 | 1908 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
1909 | struct netlink_dump_control c = { |
1910 | .dump = nf_tables_dump_chains, | |
d9adf22a | 1911 | .module = THIS_MODULE, |
96518518 | 1912 | }; |
d9adf22a | 1913 | |
797d4980 | 1914 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
1915 | } |
1916 | ||
6001a930 | 1917 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0); |
36dd1bcc PNA |
1918 | if (IS_ERR(table)) { |
1919 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1920 | return PTR_ERR(table); |
36dd1bcc | 1921 | } |
96518518 | 1922 | |
f102d66b | 1923 | chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask); |
36dd1bcc PNA |
1924 | if (IS_ERR(chain)) { |
1925 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
96518518 | 1926 | return PTR_ERR(chain); |
36dd1bcc | 1927 | } |
96518518 | 1928 | |
d9adf22a | 1929 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1930 | if (!skb2) |
1931 | return -ENOMEM; | |
1932 | ||
84d7fce6 | 1933 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 1934 | info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, |
b9703ed4 | 1935 | 0, family, table, chain, NULL); |
96518518 | 1936 | if (err < 0) |
ee921183 | 1937 | goto err_fill_chain_info; |
96518518 | 1938 | |
ee921183 | 1939 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 1940 | |
ee921183 | 1941 | err_fill_chain_info: |
96518518 PM |
1942 | kfree_skb(skb2); |
1943 | return err; | |
1944 | } | |
1945 | ||
0ca743a5 PNA |
1946 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1947 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1948 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1949 | }; | |
1950 | ||
ff3cd7b3 | 1951 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1952 | { |
1953 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1954 | struct nft_stats __percpu *newstats; | |
1955 | struct nft_stats *stats; | |
1956 | int err; | |
1957 | ||
8cb08174 JB |
1958 | err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr, |
1959 | nft_counter_policy, NULL); | |
0ca743a5 | 1960 | if (err < 0) |
ff3cd7b3 | 1961 | return ERR_PTR(err); |
0ca743a5 PNA |
1962 | |
1963 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1964 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1965 | |
ce355e20 | 1966 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1967 | if (newstats == NULL) |
ff3cd7b3 | 1968 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1969 | |
1970 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1971 | * are not exposed to userspace. | |
1972 | */ | |
e8781f70 | 1973 | preempt_disable(); |
0ca743a5 PNA |
1974 | stats = this_cpu_ptr(newstats); |
1975 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
1976 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 1977 | preempt_enable(); |
0ca743a5 | 1978 | |
ff3cd7b3 PNA |
1979 | return newstats; |
1980 | } | |
1981 | ||
53315ac6 | 1982 | static void nft_chain_stats_replace(struct nft_trans *trans) |
ff3cd7b3 | 1983 | { |
53315ac6 | 1984 | struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain); |
0befd061 | 1985 | |
53315ac6 | 1986 | if (!nft_trans_chain_stats(trans)) |
b88825de PNA |
1987 | return; |
1988 | ||
b685b534 PM |
1989 | nft_trans_chain_stats(trans) = |
1990 | rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans), | |
1991 | lockdep_commit_lock_is_held(trans->ctx.net)); | |
53315ac6 FW |
1992 | |
1993 | if (!nft_trans_chain_stats(trans)) | |
bbb8c61f | 1994 | static_branch_inc(&nft_counters_enabled); |
0ca743a5 PNA |
1995 | } |
1996 | ||
0cbc06b3 FW |
1997 | static void nf_tables_chain_free_chain_rules(struct nft_chain *chain) |
1998 | { | |
2c865a8a PNA |
1999 | struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0); |
2000 | struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1); | |
0cbc06b3 FW |
2001 | |
2002 | if (g0 != g1) | |
2003 | kvfree(g1); | |
2004 | kvfree(g0); | |
2005 | ||
2006 | /* should be NULL either via abort or via successful commit */ | |
2c865a8a PNA |
2007 | WARN_ON_ONCE(chain->blob_next); |
2008 | kvfree(chain->blob_next); | |
0cbc06b3 FW |
2009 | } |
2010 | ||
d0e2c7de | 2011 | void nf_tables_chain_destroy(struct nft_ctx *ctx) |
91c7b38d | 2012 | { |
43a605f2 | 2013 | struct nft_chain *chain = ctx->chain; |
d54725cd | 2014 | struct nft_hook *hook, *next; |
43a605f2 | 2015 | |
fa5950e4 FW |
2016 | if (WARN_ON(chain->use > 0)) |
2017 | return; | |
91c7b38d | 2018 | |
0cbc06b3 FW |
2019 | /* no concurrent access possible anymore */ |
2020 | nf_tables_chain_free_chain_rules(chain); | |
2021 | ||
f323d954 | 2022 | if (nft_is_base_chain(chain)) { |
2cbce139 PNA |
2023 | struct nft_base_chain *basechain = nft_base_chain(chain); |
2024 | ||
d3519cb8 | 2025 | if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) { |
d54725cd PNA |
2026 | list_for_each_entry_safe(hook, next, |
2027 | &basechain->hook_list, list) { | |
2028 | list_del_rcu(&hook->list); | |
2029 | kfree_rcu(hook, rcu); | |
2030 | } | |
2031 | } | |
2cbce139 | 2032 | module_put(basechain->type->owner); |
4c05ec47 | 2033 | if (rcu_access_pointer(basechain->stats)) { |
9f08ea84 | 2034 | static_branch_dec(&nft_counters_enabled); |
4c05ec47 TY |
2035 | free_percpu(rcu_dereference_raw(basechain->stats)); |
2036 | } | |
b7263e07 | 2037 | kfree(chain->name); |
002f2176 | 2038 | kfree(chain->udata); |
2cbce139 | 2039 | kfree(basechain); |
91c7b38d | 2040 | } else { |
b7263e07 | 2041 | kfree(chain->name); |
002f2176 | 2042 | kfree(chain->udata); |
91c7b38d PNA |
2043 | kfree(chain); |
2044 | } | |
2045 | } | |
2046 | ||
3f0465a9 PNA |
2047 | static struct nft_hook *nft_netdev_hook_alloc(struct net *net, |
2048 | const struct nlattr *attr) | |
2049 | { | |
2050 | struct net_device *dev; | |
2051 | char ifname[IFNAMSIZ]; | |
2052 | struct nft_hook *hook; | |
2053 | int err; | |
2054 | ||
33758c89 | 2055 | hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT); |
3f0465a9 PNA |
2056 | if (!hook) { |
2057 | err = -ENOMEM; | |
2058 | goto err_hook_alloc; | |
2059 | } | |
2060 | ||
872f6903 | 2061 | nla_strscpy(ifname, attr, IFNAMSIZ); |
42f1c271 PNA |
2062 | /* nf_tables_netdev_event() is called under rtnl_mutex, this is |
2063 | * indirectly serializing all the other holders of the commit_mutex with | |
2064 | * the rtnl_mutex. | |
2065 | */ | |
3f0465a9 PNA |
2066 | dev = __dev_get_by_name(net, ifname); |
2067 | if (!dev) { | |
2068 | err = -ENOENT; | |
2069 | goto err_hook_dev; | |
2070 | } | |
2071 | hook->ops.dev = dev; | |
2072 | ||
2073 | return hook; | |
2074 | ||
2075 | err_hook_dev: | |
2076 | kfree(hook); | |
2077 | err_hook_alloc: | |
2078 | return ERR_PTR(err); | |
2079 | } | |
2080 | ||
abadb2f8 PNA |
2081 | static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, |
2082 | const struct nft_hook *this) | |
b75a3e83 PNA |
2083 | { |
2084 | struct nft_hook *hook; | |
2085 | ||
2086 | list_for_each_entry(hook, hook_list, list) { | |
2087 | if (this->ops.dev == hook->ops.dev) | |
abadb2f8 | 2088 | return hook; |
b75a3e83 PNA |
2089 | } |
2090 | ||
abadb2f8 | 2091 | return NULL; |
b75a3e83 PNA |
2092 | } |
2093 | ||
3f0465a9 PNA |
2094 | static int nf_tables_parse_netdev_hooks(struct net *net, |
2095 | const struct nlattr *attr, | |
c3c060ad PNA |
2096 | struct list_head *hook_list, |
2097 | struct netlink_ext_ack *extack) | |
3f0465a9 PNA |
2098 | { |
2099 | struct nft_hook *hook, *next; | |
2100 | const struct nlattr *tmp; | |
2101 | int rem, n = 0, err; | |
2102 | ||
2103 | nla_for_each_nested(tmp, attr, rem) { | |
2104 | if (nla_type(tmp) != NFTA_DEVICE_NAME) { | |
2105 | err = -EINVAL; | |
2106 | goto err_hook; | |
2107 | } | |
2108 | ||
2109 | hook = nft_netdev_hook_alloc(net, tmp); | |
2110 | if (IS_ERR(hook)) { | |
c3c060ad | 2111 | NL_SET_BAD_ATTR(extack, tmp); |
3f0465a9 PNA |
2112 | err = PTR_ERR(hook); |
2113 | goto err_hook; | |
2114 | } | |
b75a3e83 | 2115 | if (nft_hook_list_find(hook_list, hook)) { |
c3c060ad | 2116 | NL_SET_BAD_ATTR(extack, tmp); |
cd77e75b | 2117 | kfree(hook); |
b75a3e83 PNA |
2118 | err = -EEXIST; |
2119 | goto err_hook; | |
2120 | } | |
3f0465a9 PNA |
2121 | list_add_tail(&hook->list, hook_list); |
2122 | n++; | |
2123 | ||
cb662ac6 | 2124 | if (n == NFT_NETDEVICE_MAX) { |
3f0465a9 PNA |
2125 | err = -EFBIG; |
2126 | goto err_hook; | |
2127 | } | |
2128 | } | |
3f0465a9 PNA |
2129 | |
2130 | return 0; | |
2131 | ||
2132 | err_hook: | |
2133 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
2134 | list_del(&hook->list); | |
2135 | kfree(hook); | |
2136 | } | |
2137 | return err; | |
2138 | } | |
2139 | ||
508f8ccd PNA |
2140 | struct nft_chain_hook { |
2141 | u32 num; | |
84ba7dd7 | 2142 | s32 priority; |
32537e91 | 2143 | const struct nft_chain_type *type; |
d54725cd | 2144 | struct list_head list; |
508f8ccd PNA |
2145 | }; |
2146 | ||
207296f1 | 2147 | static int nft_chain_parse_netdev(struct net *net, struct nlattr *tb[], |
c3c060ad | 2148 | struct list_head *hook_list, |
207296f1 | 2149 | struct netlink_ext_ack *extack, u32 flags) |
d54725cd PNA |
2150 | { |
2151 | struct nft_hook *hook; | |
2152 | int err; | |
2153 | ||
2154 | if (tb[NFTA_HOOK_DEV]) { | |
2155 | hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]); | |
c3c060ad PNA |
2156 | if (IS_ERR(hook)) { |
2157 | NL_SET_BAD_ATTR(extack, tb[NFTA_HOOK_DEV]); | |
d54725cd | 2158 | return PTR_ERR(hook); |
c3c060ad | 2159 | } |
d54725cd PNA |
2160 | |
2161 | list_add_tail(&hook->list, hook_list); | |
2162 | } else if (tb[NFTA_HOOK_DEVS]) { | |
2163 | err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS], | |
c3c060ad | 2164 | hook_list, extack); |
d54725cd PNA |
2165 | if (err < 0) |
2166 | return err; | |
05abe445 | 2167 | |
d54725cd PNA |
2168 | } |
2169 | ||
207296f1 PNA |
2170 | if (flags & NFT_CHAIN_HW_OFFLOAD && |
2171 | list_empty(hook_list)) | |
2172 | return -EINVAL; | |
2173 | ||
d54725cd PNA |
2174 | return 0; |
2175 | } | |
2176 | ||
508f8ccd | 2177 | static int nft_chain_parse_hook(struct net *net, |
b9703ed4 | 2178 | struct nft_base_chain *basechain, |
508f8ccd | 2179 | const struct nlattr * const nla[], |
36596dad | 2180 | struct nft_chain_hook *hook, u8 family, |
207296f1 | 2181 | u32 flags, struct netlink_ext_ack *extack) |
508f8ccd | 2182 | { |
d59d2f82 | 2183 | struct nftables_pernet *nft_net = nft_pernet(net); |
508f8ccd | 2184 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; |
32537e91 | 2185 | const struct nft_chain_type *type; |
508f8ccd PNA |
2186 | int err; |
2187 | ||
0854db2a | 2188 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b FW |
2189 | lockdep_nfnl_nft_mutex_not_held(); |
2190 | ||
8cb08174 JB |
2191 | err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX, |
2192 | nla[NFTA_CHAIN_HOOK], | |
2193 | nft_hook_policy, NULL); | |
508f8ccd PNA |
2194 | if (err < 0) |
2195 | return err; | |
2196 | ||
b9703ed4 PNA |
2197 | if (!basechain) { |
2198 | if (!ha[NFTA_HOOK_HOOKNUM] || | |
8509f62b PNA |
2199 | !ha[NFTA_HOOK_PRIORITY]) { |
2200 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
2201 | return -ENOENT; | |
2202 | } | |
508f8ccd | 2203 | |
b9703ed4 PNA |
2204 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); |
2205 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); | |
508f8ccd | 2206 | |
b9703ed4 PNA |
2207 | type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT); |
2208 | if (!type) | |
2209 | return -EOPNOTSUPP; | |
82603549 | 2210 | |
b9703ed4 PNA |
2211 | if (nla[NFTA_CHAIN_TYPE]) { |
2212 | type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE], | |
2213 | family, true); | |
2214 | if (IS_ERR(type)) { | |
2215 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
2216 | return PTR_ERR(type); | |
2217 | } | |
983c4fcb | 2218 | } |
b9703ed4 PNA |
2219 | if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num))) |
2220 | return -EOPNOTSUPP; | |
84ba7dd7 | 2221 | |
b9703ed4 PNA |
2222 | if (type->type == NFT_CHAIN_T_NAT && |
2223 | hook->priority <= NF_IP_PRI_CONNTRACK) | |
2224 | return -EOPNOTSUPP; | |
2225 | } else { | |
2226 | if (ha[NFTA_HOOK_HOOKNUM]) { | |
2227 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
2228 | if (hook->num != basechain->ops.hooknum) | |
2229 | return -EOPNOTSUPP; | |
2230 | } | |
2231 | if (ha[NFTA_HOOK_PRIORITY]) { | |
2232 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); | |
2233 | if (hook->priority != basechain->ops.priority) | |
2234 | return -EOPNOTSUPP; | |
2235 | } | |
2236 | ||
2237 | type = basechain->type; | |
2238 | } | |
84ba7dd7 | 2239 | |
983c4fcb PNA |
2240 | if (!try_module_get(type->owner)) { |
2241 | if (nla[NFTA_CHAIN_TYPE]) | |
2242 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
508f8ccd | 2243 | return -ENOENT; |
983c4fcb | 2244 | } |
508f8ccd PNA |
2245 | |
2246 | hook->type = type; | |
2247 | ||
d54725cd | 2248 | INIT_LIST_HEAD(&hook->list); |
d3519cb8 | 2249 | if (nft_base_chain_netdev(family, hook->num)) { |
207296f1 | 2250 | err = nft_chain_parse_netdev(net, ha, &hook->list, extack, flags); |
d54725cd | 2251 | if (err < 0) { |
508f8ccd | 2252 | module_put(type->owner); |
d54725cd | 2253 | return err; |
508f8ccd | 2254 | } |
d54725cd | 2255 | } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) { |
508f8ccd PNA |
2256 | module_put(type->owner); |
2257 | return -EOPNOTSUPP; | |
2258 | } | |
2259 | ||
2260 | return 0; | |
2261 | } | |
2262 | ||
2263 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
2264 | { | |
d54725cd PNA |
2265 | struct nft_hook *h, *next; |
2266 | ||
2267 | list_for_each_entry_safe(h, next, &hook->list, list) { | |
2268 | list_del(&h->list); | |
2269 | kfree(h); | |
2270 | } | |
508f8ccd | 2271 | module_put(hook->type->owner); |
508f8ccd PNA |
2272 | } |
2273 | ||
e38fbfa9 | 2274 | static void nft_last_rule(const struct nft_chain *chain, const void *ptr) |
0cbc06b3 | 2275 | { |
e38fbfa9 FW |
2276 | struct nft_rule_dp_last *lrule; |
2277 | ||
2278 | BUILD_BUG_ON(offsetof(struct nft_rule_dp_last, end) != 0); | |
2c865a8a | 2279 | |
e38fbfa9 FW |
2280 | lrule = (struct nft_rule_dp_last *)ptr; |
2281 | lrule->end.is_last = 1; | |
2282 | lrule->chain = chain; | |
2c865a8a PNA |
2283 | /* blob size does not include the trailer rule */ |
2284 | } | |
2285 | ||
e38fbfa9 FW |
2286 | static struct nft_rule_blob *nf_tables_chain_alloc_rules(const struct nft_chain *chain, |
2287 | unsigned int size) | |
2c865a8a PNA |
2288 | { |
2289 | struct nft_rule_blob *blob; | |
2290 | ||
2c865a8a | 2291 | if (size > INT_MAX) |
0cbc06b3 FW |
2292 | return NULL; |
2293 | ||
e38fbfa9 FW |
2294 | size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rule_dp_last); |
2295 | ||
33758c89 | 2296 | blob = kvmalloc(size, GFP_KERNEL_ACCOUNT); |
2c865a8a | 2297 | if (!blob) |
0cbc06b3 FW |
2298 | return NULL; |
2299 | ||
2c865a8a | 2300 | blob->size = 0; |
e38fbfa9 | 2301 | nft_last_rule(chain, blob->data); |
0cbc06b3 | 2302 | |
2c865a8a | 2303 | return blob; |
0cbc06b3 FW |
2304 | } |
2305 | ||
d54725cd PNA |
2306 | static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family, |
2307 | const struct nft_chain_hook *hook, | |
2308 | struct nft_chain *chain) | |
2309 | { | |
c5c6accd FW |
2310 | ops->pf = family; |
2311 | ops->hooknum = hook->num; | |
2312 | ops->priority = hook->priority; | |
2313 | ops->priv = chain; | |
2314 | ops->hook = hook->type->hooks[ops->hooknum]; | |
2315 | ops->hook_ops_type = NF_HOOK_OP_NF_TABLES; | |
d54725cd PNA |
2316 | } |
2317 | ||
2318 | static int nft_basechain_init(struct nft_base_chain *basechain, u8 family, | |
2319 | struct nft_chain_hook *hook, u32 flags) | |
2320 | { | |
2321 | struct nft_chain *chain; | |
2322 | struct nft_hook *h; | |
2323 | ||
2324 | basechain->type = hook->type; | |
2325 | INIT_LIST_HEAD(&basechain->hook_list); | |
2326 | chain = &basechain->chain; | |
2327 | ||
d3519cb8 | 2328 | if (nft_base_chain_netdev(family, hook->num)) { |
d54725cd PNA |
2329 | list_splice_init(&hook->list, &basechain->hook_list); |
2330 | list_for_each_entry(h, &basechain->hook_list, list) | |
2331 | nft_basechain_hook_init(&h->ops, family, hook, chain); | |
d54725cd | 2332 | } |
b9703ed4 | 2333 | nft_basechain_hook_init(&basechain->ops, family, hook, chain); |
d54725cd | 2334 | |
67c49de4 | 2335 | chain->flags |= NFT_CHAIN_BASE | flags; |
d54725cd PNA |
2336 | basechain->policy = NF_ACCEPT; |
2337 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD && | |
77972a36 PNA |
2338 | !nft_chain_offload_support(basechain)) { |
2339 | list_splice_init(&basechain->hook_list, &hook->list); | |
d54725cd | 2340 | return -EOPNOTSUPP; |
77972a36 | 2341 | } |
d54725cd PNA |
2342 | |
2343 | flow_block_init(&basechain->flow_block); | |
2344 | ||
2345 | return 0; | |
2346 | } | |
2347 | ||
4bedf9ee | 2348 | int nft_chain_add(struct nft_table *table, struct nft_chain *chain) |
04b7db41 PNA |
2349 | { |
2350 | int err; | |
2351 | ||
2352 | err = rhltable_insert_key(&table->chains_ht, chain->name, | |
2353 | &chain->rhlhead, nft_chain_ht_params); | |
2354 | if (err) | |
2355 | return err; | |
2356 | ||
2357 | list_add_tail_rcu(&chain->list, &table->chains); | |
2358 | ||
2359 | return 0; | |
2360 | } | |
2361 | ||
d0e2c7de PNA |
2362 | static u64 chain_id; |
2363 | ||
4035285f | 2364 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
983c4fcb PNA |
2365 | u8 policy, u32 flags, |
2366 | struct netlink_ext_ack *extack) | |
4035285f PNA |
2367 | { |
2368 | const struct nlattr * const *nla = ctx->nla; | |
2369 | struct nft_table *table = ctx->table; | |
4035285f | 2370 | struct nft_base_chain *basechain; |
4035285f | 2371 | struct net *net = ctx->net; |
d0e2c7de | 2372 | char name[NFT_NAME_MAXLEN]; |
2c865a8a | 2373 | struct nft_rule_blob *blob; |
66293c46 | 2374 | struct nft_trans *trans; |
4035285f | 2375 | struct nft_chain *chain; |
4035285f PNA |
2376 | int err; |
2377 | ||
2378 | if (table->use == UINT_MAX) | |
2379 | return -EOVERFLOW; | |
2380 | ||
2381 | if (nla[NFTA_CHAIN_HOOK]) { | |
921ebde3 | 2382 | struct nft_stats __percpu *stats = NULL; |
b9703ed4 | 2383 | struct nft_chain_hook hook = {}; |
4035285f | 2384 | |
d0e2c7de PNA |
2385 | if (flags & NFT_CHAIN_BINDING) |
2386 | return -EOPNOTSUPP; | |
2387 | ||
207296f1 | 2388 | err = nft_chain_parse_hook(net, NULL, nla, &hook, family, flags, |
b9703ed4 | 2389 | extack); |
4035285f PNA |
2390 | if (err < 0) |
2391 | return err; | |
2392 | ||
33758c89 | 2393 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2394 | if (basechain == NULL) { |
2395 | nft_chain_release_hook(&hook); | |
2396 | return -ENOMEM; | |
2397 | } | |
d54725cd | 2398 | chain = &basechain->chain; |
4035285f PNA |
2399 | |
2400 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2401 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2402 | if (IS_ERR(stats)) { | |
2403 | nft_chain_release_hook(&hook); | |
2404 | kfree(basechain); | |
2405 | return PTR_ERR(stats); | |
2406 | } | |
4c05ec47 | 2407 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
2408 | } |
2409 | ||
d54725cd PNA |
2410 | err = nft_basechain_init(basechain, family, &hook, flags); |
2411 | if (err < 0) { | |
2412 | nft_chain_release_hook(&hook); | |
2413 | kfree(basechain); | |
9a4d6dd5 | 2414 | free_percpu(stats); |
d54725cd PNA |
2415 | return err; |
2416 | } | |
921ebde3 TH |
2417 | if (stats) |
2418 | static_branch_inc(&nft_counters_enabled); | |
4035285f | 2419 | } else { |
d0e2c7de PNA |
2420 | if (flags & NFT_CHAIN_BASE) |
2421 | return -EINVAL; | |
2422 | if (flags & NFT_CHAIN_HW_OFFLOAD) | |
2423 | return -EOPNOTSUPP; | |
2424 | ||
33758c89 | 2425 | chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2426 | if (chain == NULL) |
2427 | return -ENOMEM; | |
d0e2c7de PNA |
2428 | |
2429 | chain->flags = flags; | |
4035285f | 2430 | } |
43a605f2 PNA |
2431 | ctx->chain = chain; |
2432 | ||
4035285f PNA |
2433 | INIT_LIST_HEAD(&chain->rules); |
2434 | chain->handle = nf_tables_alloc_handle(table); | |
2435 | chain->table = table; | |
d0e2c7de PNA |
2436 | |
2437 | if (nla[NFTA_CHAIN_NAME]) { | |
33758c89 | 2438 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
d0e2c7de | 2439 | } else { |
59136aa3 FW |
2440 | if (!(flags & NFT_CHAIN_BINDING)) { |
2441 | err = -EINVAL; | |
002f2176 | 2442 | goto err_destroy_chain; |
59136aa3 | 2443 | } |
d0e2c7de PNA |
2444 | |
2445 | snprintf(name, sizeof(name), "__chain%llu", ++chain_id); | |
33758c89 | 2446 | chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT); |
d0e2c7de PNA |
2447 | } |
2448 | ||
4035285f PNA |
2449 | if (!chain->name) { |
2450 | err = -ENOMEM; | |
002f2176 JGG |
2451 | goto err_destroy_chain; |
2452 | } | |
2453 | ||
2454 | if (nla[NFTA_CHAIN_USERDATA]) { | |
33758c89 | 2455 | chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT); |
002f2176 JGG |
2456 | if (chain->udata == NULL) { |
2457 | err = -ENOMEM; | |
2458 | goto err_destroy_chain; | |
2459 | } | |
2460 | chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]); | |
4035285f PNA |
2461 | } |
2462 | ||
e38fbfa9 | 2463 | blob = nf_tables_chain_alloc_rules(chain, 0); |
2c865a8a | 2464 | if (!blob) { |
0cbc06b3 | 2465 | err = -ENOMEM; |
002f2176 | 2466 | goto err_destroy_chain; |
0cbc06b3 FW |
2467 | } |
2468 | ||
2c865a8a PNA |
2469 | RCU_INIT_POINTER(chain->blob_gen_0, blob); |
2470 | RCU_INIT_POINTER(chain->blob_gen_1, blob); | |
0cbc06b3 | 2471 | |
c974a3a3 | 2472 | err = nf_tables_register_hook(net, table, chain); |
4035285f | 2473 | if (err < 0) |
002f2176 | 2474 | goto err_destroy_chain; |
4035285f | 2475 | |
66293c46 FW |
2476 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
2477 | if (IS_ERR(trans)) { | |
2478 | err = PTR_ERR(trans); | |
002f2176 | 2479 | goto err_unregister_hook; |
1b2470e5 | 2480 | } |
4035285f | 2481 | |
ad652f38 | 2482 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
2483 | if (nft_is_base_chain(chain)) |
2484 | nft_trans_chain_policy(trans) = policy; | |
2485 | ||
04b7db41 PNA |
2486 | err = nft_chain_add(table, chain); |
2487 | if (err < 0) { | |
2488 | nft_trans_destroy(trans); | |
002f2176 | 2489 | goto err_unregister_hook; |
04b7db41 PNA |
2490 | } |
2491 | ||
4035285f | 2492 | table->use++; |
4035285f PNA |
2493 | |
2494 | return 0; | |
002f2176 | 2495 | err_unregister_hook: |
c974a3a3 | 2496 | nf_tables_unregister_hook(net, table, chain); |
002f2176 | 2497 | err_destroy_chain: |
43a605f2 | 2498 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2499 | |
2500 | return err; | |
2501 | } | |
2502 | ||
c9626a2c | 2503 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
98a381a7 PNA |
2504 | u32 flags, const struct nlattr *attr, |
2505 | struct netlink_ext_ack *extack) | |
2c4a488a PNA |
2506 | { |
2507 | const struct nlattr * const *nla = ctx->nla; | |
b9703ed4 | 2508 | struct nft_base_chain *basechain = NULL; |
2c4a488a PNA |
2509 | struct nft_table *table = ctx->table; |
2510 | struct nft_chain *chain = ctx->chain; | |
b9703ed4 | 2511 | struct nft_chain_hook hook = {}; |
2c4a488a | 2512 | struct nft_stats *stats = NULL; |
b9703ed4 | 2513 | struct nft_hook *h, *next; |
2c4a488a PNA |
2514 | struct nf_hook_ops *ops; |
2515 | struct nft_trans *trans; | |
b9703ed4 | 2516 | bool unregister = false; |
c974a3a3 | 2517 | int err; |
2c4a488a | 2518 | |
c9626a2c PNA |
2519 | if (chain->flags ^ flags) |
2520 | return -EOPNOTSUPP; | |
2521 | ||
b9703ed4 PNA |
2522 | INIT_LIST_HEAD(&hook.list); |
2523 | ||
2c4a488a | 2524 | if (nla[NFTA_CHAIN_HOOK]) { |
98a381a7 PNA |
2525 | if (!nft_is_base_chain(chain)) { |
2526 | NL_SET_BAD_ATTR(extack, attr); | |
77a92189 | 2527 | return -EEXIST; |
98a381a7 | 2528 | } |
b9703ed4 PNA |
2529 | |
2530 | basechain = nft_base_chain(chain); | |
2531 | err = nft_chain_parse_hook(ctx->net, basechain, nla, &hook, | |
207296f1 | 2532 | ctx->family, flags, extack); |
2c4a488a PNA |
2533 | if (err < 0) |
2534 | return err; | |
2535 | ||
2c4a488a PNA |
2536 | if (basechain->type != hook.type) { |
2537 | nft_chain_release_hook(&hook); | |
98a381a7 | 2538 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2539 | return -EEXIST; |
2c4a488a PNA |
2540 | } |
2541 | ||
b9703ed4 PNA |
2542 | if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) { |
2543 | list_for_each_entry_safe(h, next, &hook.list, list) { | |
2544 | h->ops.pf = basechain->ops.pf; | |
2545 | h->ops.hooknum = basechain->ops.hooknum; | |
2546 | h->ops.priority = basechain->ops.priority; | |
2547 | h->ops.priv = basechain->ops.priv; | |
2548 | h->ops.hook = basechain->ops.hook; | |
2549 | ||
2550 | if (nft_hook_list_find(&basechain->hook_list, h)) { | |
2551 | list_del(&h->list); | |
2552 | kfree(h); | |
2553 | } | |
d54725cd PNA |
2554 | } |
2555 | } else { | |
2556 | ops = &basechain->ops; | |
2557 | if (ops->hooknum != hook.num || | |
2558 | ops->priority != hook.priority) { | |
2559 | nft_chain_release_hook(&hook); | |
98a381a7 | 2560 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2561 | return -EEXIST; |
d54725cd | 2562 | } |
2c4a488a | 2563 | } |
2c4a488a PNA |
2564 | } |
2565 | ||
2566 | if (nla[NFTA_CHAIN_HANDLE] && | |
2567 | nla[NFTA_CHAIN_NAME]) { | |
2568 | struct nft_chain *chain2; | |
2569 | ||
f102d66b FW |
2570 | chain2 = nft_chain_lookup(ctx->net, table, |
2571 | nla[NFTA_CHAIN_NAME], genmask); | |
98a381a7 PNA |
2572 | if (!IS_ERR(chain2)) { |
2573 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
b9703ed4 PNA |
2574 | err = -EEXIST; |
2575 | goto err_hooks; | |
98a381a7 | 2576 | } |
2c4a488a PNA |
2577 | } |
2578 | ||
2579 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
b9703ed4 PNA |
2580 | if (!nft_is_base_chain(chain)) { |
2581 | err = -EOPNOTSUPP; | |
2582 | goto err_hooks; | |
2583 | } | |
2c4a488a PNA |
2584 | |
2585 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
b9703ed4 PNA |
2586 | if (IS_ERR(stats)) { |
2587 | err = PTR_ERR(stats); | |
2588 | goto err_hooks; | |
2589 | } | |
2c4a488a PNA |
2590 | } |
2591 | ||
b9703ed4 PNA |
2592 | if (!(table->flags & NFT_TABLE_F_DORMANT) && |
2593 | nft_is_base_chain(chain) && | |
2594 | !list_empty(&hook.list)) { | |
2595 | basechain = nft_base_chain(chain); | |
2596 | ops = &basechain->ops; | |
2597 | ||
2598 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) { | |
2599 | err = nft_netdev_register_hooks(ctx->net, &hook.list); | |
2600 | if (err < 0) | |
2601 | goto err_hooks; | |
2602 | } | |
2603 | } | |
2604 | ||
2605 | unregister = true; | |
c6cc94df | 2606 | err = -ENOMEM; |
2c4a488a PNA |
2607 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2608 | sizeof(struct nft_trans_chain)); | |
c6cc94df | 2609 | if (trans == NULL) |
b9703ed4 | 2610 | goto err_trans; |
2c4a488a PNA |
2611 | |
2612 | nft_trans_chain_stats(trans) = stats; | |
2613 | nft_trans_chain_update(trans) = true; | |
2614 | ||
2615 | if (nla[NFTA_CHAIN_POLICY]) | |
2616 | nft_trans_chain_policy(trans) = policy; | |
2617 | else | |
2618 | nft_trans_chain_policy(trans) = -1; | |
2619 | ||
c6cc94df FW |
2620 | if (nla[NFTA_CHAIN_HANDLE] && |
2621 | nla[NFTA_CHAIN_NAME]) { | |
d59d2f82 | 2622 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c6cc94df FW |
2623 | struct nft_trans *tmp; |
2624 | char *name; | |
2625 | ||
2626 | err = -ENOMEM; | |
33758c89 | 2627 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
c6cc94df | 2628 | if (!name) |
b9703ed4 | 2629 | goto err_trans; |
c6cc94df FW |
2630 | |
2631 | err = -EEXIST; | |
0854db2a | 2632 | list_for_each_entry(tmp, &nft_net->commit_list, list) { |
c6cc94df FW |
2633 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && |
2634 | tmp->ctx.table == table && | |
2635 | nft_trans_chain_update(tmp) && | |
2636 | nft_trans_chain_name(tmp) && | |
2637 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
98a381a7 | 2638 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); |
c6cc94df | 2639 | kfree(name); |
b9703ed4 | 2640 | goto err_trans; |
c6cc94df | 2641 | } |
2c4a488a | 2642 | } |
c6cc94df FW |
2643 | |
2644 | nft_trans_chain_name(trans) = name; | |
2c4a488a | 2645 | } |
b9703ed4 PNA |
2646 | |
2647 | nft_trans_basechain(trans) = basechain; | |
2648 | INIT_LIST_HEAD(&nft_trans_chain_hooks(trans)); | |
2649 | list_splice(&hook.list, &nft_trans_chain_hooks(trans)); | |
2650 | ||
0854db2a | 2651 | nft_trans_commit_list_add_tail(ctx->net, trans); |
2c4a488a PNA |
2652 | |
2653 | return 0; | |
b9703ed4 PNA |
2654 | |
2655 | err_trans: | |
c6cc94df FW |
2656 | free_percpu(stats); |
2657 | kfree(trans); | |
b9703ed4 PNA |
2658 | err_hooks: |
2659 | if (nla[NFTA_CHAIN_HOOK]) { | |
2660 | list_for_each_entry_safe(h, next, &hook.list, list) { | |
2661 | if (unregister) | |
2662 | nf_unregister_net_hook(ctx->net, &h->ops); | |
2663 | list_del(&h->list); | |
2664 | kfree_rcu(h, rcu); | |
2665 | } | |
2666 | module_put(hook.type->owner); | |
2667 | } | |
2668 | ||
c6cc94df | 2669 | return err; |
2c4a488a PNA |
2670 | } |
2671 | ||
837830a4 | 2672 | static struct nft_chain *nft_chain_lookup_byid(const struct net *net, |
95f466d2 | 2673 | const struct nft_table *table, |
837830a4 PNA |
2674 | const struct nlattr *nla) |
2675 | { | |
d59d2f82 | 2676 | struct nftables_pernet *nft_net = nft_pernet(net); |
837830a4 PNA |
2677 | u32 id = ntohl(nla_get_be32(nla)); |
2678 | struct nft_trans *trans; | |
2679 | ||
0854db2a | 2680 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
837830a4 PNA |
2681 | struct nft_chain *chain = trans->ctx.chain; |
2682 | ||
2683 | if (trans->msg_type == NFT_MSG_NEWCHAIN && | |
95f466d2 | 2684 | chain->table == table && |
837830a4 PNA |
2685 | id == nft_trans_chain_id(trans)) |
2686 | return chain; | |
2687 | } | |
2688 | return ERR_PTR(-ENOENT); | |
2689 | } | |
2690 | ||
7dab8ee3 PNA |
2691 | static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info, |
2692 | const struct nlattr * const nla[]) | |
96518518 | 2693 | { |
7dab8ee3 | 2694 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
2695 | struct netlink_ext_ack *extack = info->extack; |
2696 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2697 | u8 family = info->nfmsg->nfgen_family; |
74cccc3d | 2698 | struct nft_chain *chain = NULL; |
7dab8ee3 | 2699 | struct net *net = info->net; |
36dd1bcc | 2700 | const struct nlattr *attr; |
96518518 | 2701 | struct nft_table *table; |
57de2a0c | 2702 | u8 policy = NF_ACCEPT; |
4035285f | 2703 | struct nft_ctx ctx; |
96518518 | 2704 | u64 handle = 0; |
c9626a2c | 2705 | u32 flags = 0; |
96518518 | 2706 | |
0854db2a | 2707 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 2708 | |
6001a930 PNA |
2709 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2710 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2711 | if (IS_ERR(table)) { |
2712 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2713 | return PTR_ERR(table); |
36dd1bcc | 2714 | } |
96518518 | 2715 | |
96518518 | 2716 | chain = NULL; |
36dd1bcc | 2717 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2718 | |
2719 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2720 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2721 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2722 | if (IS_ERR(chain)) { |
2723 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2724 | return PTR_ERR(chain); |
36dd1bcc PNA |
2725 | } |
2726 | attr = nla[NFTA_CHAIN_HANDLE]; | |
74cccc3d | 2727 | } else if (nla[NFTA_CHAIN_NAME]) { |
f102d66b | 2728 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2729 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2730 | if (PTR_ERR(chain) != -ENOENT) { |
2731 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2732 | return PTR_ERR(chain); |
36dd1bcc | 2733 | } |
96518518 PM |
2734 | chain = NULL; |
2735 | } | |
74cccc3d PNA |
2736 | } else if (!nla[NFTA_CHAIN_ID]) { |
2737 | return -EINVAL; | |
96518518 PM |
2738 | } |
2739 | ||
57de2a0c | 2740 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2741 | if (chain != NULL && |
36dd1bcc PNA |
2742 | !nft_is_base_chain(chain)) { |
2743 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2744 | return -EOPNOTSUPP; |
36dd1bcc | 2745 | } |
d6b6cb1d PNA |
2746 | |
2747 | if (chain == NULL && | |
36dd1bcc PNA |
2748 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2749 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2750 | return -EOPNOTSUPP; |
36dd1bcc | 2751 | } |
57de2a0c | 2752 | |
8f46df18 | 2753 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2754 | switch (policy) { |
2755 | case NF_DROP: | |
2756 | case NF_ACCEPT: | |
2757 | break; | |
2758 | default: | |
2759 | return -EINVAL; | |
2760 | } | |
2761 | } | |
2762 | ||
c9626a2c PNA |
2763 | if (nla[NFTA_CHAIN_FLAGS]) |
2764 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2765 | else if (chain) |
2766 | flags = chain->flags; | |
c9626a2c | 2767 | |
c1f79a2e PNA |
2768 | if (flags & ~NFT_CHAIN_FLAGS) |
2769 | return -EOPNOTSUPP; | |
2770 | ||
7dab8ee3 | 2771 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
4035285f | 2772 | |
96518518 | 2773 | if (chain != NULL) { |
5dc52d83 PNA |
2774 | if (chain->flags & NFT_CHAIN_BINDING) |
2775 | return -EINVAL; | |
2776 | ||
7dab8ee3 | 2777 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 2778 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2779 | return -EEXIST; |
36dd1bcc | 2780 | } |
7dab8ee3 | 2781 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
2782 | return -EOPNOTSUPP; |
2783 | ||
67c49de4 | 2784 | flags |= chain->flags & NFT_CHAIN_BASE; |
98a381a7 PNA |
2785 | return nf_tables_updchain(&ctx, genmask, policy, flags, attr, |
2786 | extack); | |
96518518 PM |
2787 | } |
2788 | ||
983c4fcb | 2789 | return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack); |
96518518 PM |
2790 | } |
2791 | ||
7d937b10 PNA |
2792 | static int nft_delchain_hook(struct nft_ctx *ctx, struct nft_chain *chain, |
2793 | struct netlink_ext_ack *extack) | |
2794 | { | |
2795 | const struct nlattr * const *nla = ctx->nla; | |
2796 | struct nft_chain_hook chain_hook = {}; | |
2797 | struct nft_base_chain *basechain; | |
2798 | struct nft_hook *this, *hook; | |
2799 | LIST_HEAD(chain_del_list); | |
2800 | struct nft_trans *trans; | |
2801 | int err; | |
2802 | ||
2803 | if (!nft_is_base_chain(chain)) | |
2804 | return -EOPNOTSUPP; | |
2805 | ||
2806 | basechain = nft_base_chain(chain); | |
2807 | err = nft_chain_parse_hook(ctx->net, basechain, nla, &chain_hook, | |
207296f1 | 2808 | ctx->family, chain->flags, extack); |
7d937b10 PNA |
2809 | if (err < 0) |
2810 | return err; | |
2811 | ||
2812 | list_for_each_entry(this, &chain_hook.list, list) { | |
2813 | hook = nft_hook_list_find(&basechain->hook_list, this); | |
2814 | if (!hook) { | |
2815 | err = -ENOENT; | |
2816 | goto err_chain_del_hook; | |
2817 | } | |
2818 | list_move(&hook->list, &chain_del_list); | |
2819 | } | |
2820 | ||
2821 | trans = nft_trans_alloc(ctx, NFT_MSG_DELCHAIN, | |
2822 | sizeof(struct nft_trans_chain)); | |
2823 | if (!trans) { | |
2824 | err = -ENOMEM; | |
2825 | goto err_chain_del_hook; | |
2826 | } | |
2827 | ||
2828 | nft_trans_basechain(trans) = basechain; | |
2829 | nft_trans_chain_update(trans) = true; | |
2830 | INIT_LIST_HEAD(&nft_trans_chain_hooks(trans)); | |
2831 | list_splice(&chain_del_list, &nft_trans_chain_hooks(trans)); | |
2832 | nft_chain_release_hook(&chain_hook); | |
2833 | ||
2834 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
2835 | ||
2836 | return 0; | |
2837 | ||
2838 | err_chain_del_hook: | |
2839 | list_splice(&chain_del_list, &basechain->hook_list); | |
2840 | nft_chain_release_hook(&chain_hook); | |
2841 | ||
2842 | return err; | |
2843 | } | |
2844 | ||
7dab8ee3 PNA |
2845 | static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info, |
2846 | const struct nlattr * const nla[]) | |
96518518 | 2847 | { |
7dab8ee3 PNA |
2848 | struct netlink_ext_ack *extack = info->extack; |
2849 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2850 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 2851 | struct net *net = info->net; |
36dd1bcc | 2852 | const struct nlattr *attr; |
96518518 PM |
2853 | struct nft_table *table; |
2854 | struct nft_chain *chain; | |
9dee1474 | 2855 | struct nft_rule *rule; |
91c7b38d | 2856 | struct nft_ctx ctx; |
3ecbfd65 | 2857 | u64 handle; |
9dee1474 PNA |
2858 | u32 use; |
2859 | int err; | |
96518518 | 2860 | |
6001a930 PNA |
2861 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2862 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2863 | if (IS_ERR(table)) { |
2864 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2865 | return PTR_ERR(table); |
36dd1bcc | 2866 | } |
96518518 | 2867 | |
3ecbfd65 | 2868 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2869 | attr = nla[NFTA_CHAIN_HANDLE]; |
2870 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2871 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2872 | } else { |
36dd1bcc | 2873 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2874 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2875 | } |
36dd1bcc | 2876 | if (IS_ERR(chain)) { |
f80a612d FFM |
2877 | if (PTR_ERR(chain) == -ENOENT && |
2878 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN) | |
2879 | return 0; | |
2880 | ||
36dd1bcc | 2881 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2882 | return PTR_ERR(chain); |
36dd1bcc | 2883 | } |
9dee1474 | 2884 | |
7d937b10 PNA |
2885 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
2886 | ||
2887 | if (nla[NFTA_CHAIN_HOOK]) { | |
2888 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) | |
2889 | return -EOPNOTSUPP; | |
2890 | ||
2891 | return nft_delchain_hook(&ctx, chain, extack); | |
2892 | } | |
2893 | ||
7dab8ee3 | 2894 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
9dee1474 | 2895 | chain->use > 0) |
96518518 PM |
2896 | return -EBUSY; |
2897 | ||
9dee1474 PNA |
2898 | use = chain->use; |
2899 | list_for_each_entry(rule, &chain->rules, list) { | |
2900 | if (!nft_is_active_next(net, rule)) | |
2901 | continue; | |
2902 | use--; | |
2903 | ||
2904 | err = nft_delrule(&ctx, rule); | |
2905 | if (err < 0) | |
2906 | return err; | |
2907 | } | |
2908 | ||
2909 | /* There are rules and elements that are still holding references to us, | |
2910 | * we cannot do a recursive removal in this case. | |
2911 | */ | |
36dd1bcc PNA |
2912 | if (use > 0) { |
2913 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2914 | return -EBUSY; |
36dd1bcc | 2915 | } |
9dee1474 | 2916 | |
ee01d542 | 2917 | return nft_delchain(&ctx); |
96518518 PM |
2918 | } |
2919 | ||
96518518 PM |
2920 | /* |
2921 | * Expressions | |
2922 | */ | |
2923 | ||
2924 | /** | |
ef1f7df9 | 2925 | * nft_register_expr - register nf_tables expr type |
3db86c39 | 2926 | * @type: expr type |
96518518 | 2927 | * |
ef1f7df9 | 2928 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2929 | * success or a negative errno code otherwise. |
2930 | */ | |
ef1f7df9 | 2931 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2932 | { |
2933 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2934 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2935 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2936 | else |
e688a7f8 | 2937 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2938 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2939 | return 0; | |
2940 | } | |
2941 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2942 | ||
2943 | /** | |
ef1f7df9 | 2944 | * nft_unregister_expr - unregister nf_tables expr type |
3db86c39 | 2945 | * @type: expr type |
96518518 | 2946 | * |
ef1f7df9 | 2947 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2948 | */ |
ef1f7df9 | 2949 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2950 | { |
2951 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2952 | list_del_rcu(&type->list); |
96518518 PM |
2953 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2954 | } | |
2955 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2956 | ||
64d46806 PM |
2957 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2958 | struct nlattr *nla) | |
96518518 | 2959 | { |
9cff126f | 2960 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2961 | |
ef1f7df9 | 2962 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2963 | if (!nla_strcmp(nla, type->name)) { |
2964 | if (!type->family && !candidate) | |
2965 | candidate = type; | |
2966 | else if (type->family == family) | |
2967 | candidate = type; | |
2968 | } | |
96518518 | 2969 | } |
9cff126f | 2970 | return candidate; |
96518518 PM |
2971 | } |
2972 | ||
b9c04ae7 PNA |
2973 | #ifdef CONFIG_MODULES |
2974 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
2975 | struct nlattr *nla) | |
2976 | { | |
eb014de4 PNA |
2977 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
2978 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
2979 | return -EAGAIN; |
2980 | ||
2981 | return 0; | |
2982 | } | |
2983 | #endif | |
2984 | ||
452238e8 FW |
2985 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
2986 | u8 family, | |
64d46806 | 2987 | struct nlattr *nla) |
96518518 | 2988 | { |
ef1f7df9 | 2989 | const struct nft_expr_type *type; |
96518518 PM |
2990 | |
2991 | if (nla == NULL) | |
2992 | return ERR_PTR(-EINVAL); | |
2993 | ||
64d46806 | 2994 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
2995 | if (type != NULL && try_module_get(type->owner)) |
2996 | return type; | |
96518518 | 2997 | |
f102d66b | 2998 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 2999 | #ifdef CONFIG_MODULES |
ef1f7df9 | 3000 | if (type == NULL) { |
b9c04ae7 | 3001 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
3002 | return ERR_PTR(-EAGAIN); |
3003 | ||
eb014de4 PNA |
3004 | if (nft_request_module(net, "nft-expr-%.*s", |
3005 | nla_len(nla), | |
3006 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
3007 | return ERR_PTR(-EAGAIN); |
3008 | } | |
3009 | #endif | |
3010 | return ERR_PTR(-ENOENT); | |
3011 | } | |
3012 | ||
3013 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
3014 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
3015 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
3016 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
3017 | }; | |
3018 | ||
3019 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
8daa8fde | 3020 | const struct nft_expr *expr, bool reset) |
96518518 | 3021 | { |
ef1f7df9 | 3022 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
3023 | goto nla_put_failure; |
3024 | ||
3025 | if (expr->ops->dump) { | |
ae0be8de MK |
3026 | struct nlattr *data = nla_nest_start_noflag(skb, |
3027 | NFTA_EXPR_DATA); | |
96518518 PM |
3028 | if (data == NULL) |
3029 | goto nla_put_failure; | |
8daa8fde | 3030 | if (expr->ops->dump(skb, expr, reset) < 0) |
96518518 PM |
3031 | goto nla_put_failure; |
3032 | nla_nest_end(skb, data); | |
3033 | } | |
3034 | ||
3035 | return skb->len; | |
3036 | ||
3037 | nla_put_failure: | |
3038 | return -1; | |
3039 | }; | |
3040 | ||
0b2d8a7b | 3041 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
8daa8fde | 3042 | const struct nft_expr *expr, bool reset) |
0b2d8a7b PM |
3043 | { |
3044 | struct nlattr *nest; | |
3045 | ||
ae0be8de | 3046 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
3047 | if (!nest) |
3048 | goto nla_put_failure; | |
8daa8fde | 3049 | if (nf_tables_fill_expr_info(skb, expr, reset) < 0) |
0b2d8a7b PM |
3050 | goto nla_put_failure; |
3051 | nla_nest_end(skb, nest); | |
3052 | return 0; | |
3053 | ||
3054 | nla_put_failure: | |
3055 | return -1; | |
3056 | } | |
3057 | ||
96518518 PM |
3058 | struct nft_expr_info { |
3059 | const struct nft_expr_ops *ops; | |
83d9dcba | 3060 | const struct nlattr *attr; |
ef1f7df9 | 3061 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
3062 | }; |
3063 | ||
0ca743a5 PNA |
3064 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
3065 | const struct nlattr *nla, | |
96518518 PM |
3066 | struct nft_expr_info *info) |
3067 | { | |
ef1f7df9 | 3068 | const struct nft_expr_type *type; |
96518518 | 3069 | const struct nft_expr_ops *ops; |
ef1f7df9 | 3070 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
3071 | int err; |
3072 | ||
8cb08174 JB |
3073 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
3074 | nft_expr_policy, NULL); | |
96518518 PM |
3075 | if (err < 0) |
3076 | return err; | |
3077 | ||
452238e8 | 3078 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
3079 | if (IS_ERR(type)) |
3080 | return PTR_ERR(type); | |
3081 | ||
3082 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
3083 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
3084 | tb[NFTA_EXPR_DATA], | |
3085 | type->policy, NULL); | |
ef1f7df9 PM |
3086 | if (err < 0) |
3087 | goto err1; | |
3088 | } else | |
3089 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
3090 | ||
3091 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
3092 | ops = type->select_ops(ctx, |
3093 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
3094 | if (IS_ERR(ops)) { |
3095 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
3096 | #ifdef CONFIG_MODULES |
3097 | if (err == -EAGAIN) | |
eb014de4 PNA |
3098 | if (nft_expr_type_request_module(ctx->net, |
3099 | ctx->family, | |
3100 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
3101 | err = -ENOENT; | |
0ef1efd1 | 3102 | #endif |
ef1f7df9 PM |
3103 | goto err1; |
3104 | } | |
3105 | } else | |
3106 | ops = type->ops; | |
3107 | ||
83d9dcba | 3108 | info->attr = nla; |
96518518 | 3109 | info->ops = ops; |
83d9dcba | 3110 | |
96518518 | 3111 | return 0; |
ef1f7df9 PM |
3112 | |
3113 | err1: | |
3114 | module_put(type->owner); | |
3115 | return err; | |
96518518 PM |
3116 | } |
3117 | ||
3a07327d PNA |
3118 | int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla, |
3119 | struct nft_expr_info *info) | |
3120 | { | |
3121 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; | |
3122 | const struct nft_expr_type *type; | |
3123 | int err; | |
3124 | ||
3125 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, | |
3126 | nft_expr_policy, NULL); | |
3127 | if (err < 0) | |
3128 | return err; | |
3129 | ||
3130 | if (!tb[NFTA_EXPR_DATA]) | |
3131 | return -EINVAL; | |
3132 | ||
3133 | type = __nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]); | |
98cbc40e DC |
3134 | if (!type) |
3135 | return -ENOENT; | |
3a07327d PNA |
3136 | |
3137 | if (!type->inner_ops) | |
3138 | return -EOPNOTSUPP; | |
3139 | ||
3140 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, | |
3141 | tb[NFTA_EXPR_DATA], | |
3142 | type->policy, NULL); | |
3143 | if (err < 0) | |
3144 | goto err_nla_parse; | |
3145 | ||
3146 | info->attr = nla; | |
3147 | info->ops = type->inner_ops; | |
3148 | ||
3149 | return 0; | |
3150 | ||
3151 | err_nla_parse: | |
3152 | return err; | |
3153 | } | |
3154 | ||
96518518 | 3155 | static int nf_tables_newexpr(const struct nft_ctx *ctx, |
7dab8ee3 | 3156 | const struct nft_expr_info *expr_info, |
96518518 PM |
3157 | struct nft_expr *expr) |
3158 | { | |
7dab8ee3 | 3159 | const struct nft_expr_ops *ops = expr_info->ops; |
96518518 PM |
3160 | int err; |
3161 | ||
3162 | expr->ops = ops; | |
3163 | if (ops->init) { | |
7dab8ee3 | 3164 | err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb); |
96518518 PM |
3165 | if (err < 0) |
3166 | goto err1; | |
3167 | } | |
3168 | ||
96518518 | 3169 | return 0; |
96518518 PM |
3170 | err1: |
3171 | expr->ops = NULL; | |
3172 | return err; | |
3173 | } | |
3174 | ||
62472bce PM |
3175 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
3176 | struct nft_expr *expr) | |
96518518 | 3177 | { |
3f3a390d PNA |
3178 | const struct nft_expr_type *type = expr->ops->type; |
3179 | ||
96518518 | 3180 | if (expr->ops->destroy) |
62472bce | 3181 | expr->ops->destroy(ctx, expr); |
3f3a390d | 3182 | module_put(type->owner); |
96518518 PM |
3183 | } |
3184 | ||
795a6d6b PNA |
3185 | static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
3186 | const struct nlattr *nla) | |
0b2d8a7b | 3187 | { |
7dab8ee3 | 3188 | struct nft_expr_info expr_info; |
0b2d8a7b | 3189 | struct nft_expr *expr; |
b8e20400 | 3190 | struct module *owner; |
0b2d8a7b PM |
3191 | int err; |
3192 | ||
7dab8ee3 | 3193 | err = nf_tables_expr_parse(ctx, nla, &expr_info); |
0b2d8a7b | 3194 | if (err < 0) |
52077804 PNA |
3195 | goto err_expr_parse; |
3196 | ||
3197 | err = -EOPNOTSUPP; | |
3198 | if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL)) | |
3199 | goto err_expr_stateful; | |
0b2d8a7b PM |
3200 | |
3201 | err = -ENOMEM; | |
33758c89 | 3202 | expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT); |
0b2d8a7b | 3203 | if (expr == NULL) |
52077804 | 3204 | goto err_expr_stateful; |
0b2d8a7b | 3205 | |
7dab8ee3 | 3206 | err = nf_tables_newexpr(ctx, &expr_info, expr); |
0b2d8a7b | 3207 | if (err < 0) |
52077804 | 3208 | goto err_expr_new; |
0b2d8a7b PM |
3209 | |
3210 | return expr; | |
52077804 | 3211 | err_expr_new: |
6cafaf47 | 3212 | kfree(expr); |
52077804 | 3213 | err_expr_stateful: |
7dab8ee3 PNA |
3214 | owner = expr_info.ops->type->owner; |
3215 | if (expr_info.ops->type->release_ops) | |
3216 | expr_info.ops->type->release_ops(expr_info.ops); | |
b8e20400 PNA |
3217 | |
3218 | module_put(owner); | |
52077804 | 3219 | err_expr_parse: |
0b2d8a7b PM |
3220 | return ERR_PTR(err); |
3221 | } | |
3222 | ||
c604cc69 PNA |
3223 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
3224 | { | |
3225 | int err; | |
3226 | ||
3227 | if (src->ops->clone) { | |
3228 | dst->ops = src->ops; | |
3229 | err = src->ops->clone(dst, src); | |
3230 | if (err < 0) | |
3231 | return err; | |
3232 | } else { | |
3233 | memcpy(dst, src, src->ops->size); | |
3234 | } | |
3235 | ||
3236 | __module_get(src->ops->type->owner); | |
3237 | ||
3238 | return 0; | |
3239 | } | |
3240 | ||
0b2d8a7b PM |
3241 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
3242 | { | |
3243 | nf_tables_expr_destroy(ctx, expr); | |
3244 | kfree(expr); | |
3245 | } | |
3246 | ||
96518518 PM |
3247 | /* |
3248 | * Rules | |
3249 | */ | |
3250 | ||
cac20fcd PNA |
3251 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
3252 | u64 handle) | |
96518518 PM |
3253 | { |
3254 | struct nft_rule *rule; | |
3255 | ||
3256 | // FIXME: this sucks | |
d9adf22a | 3257 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
3258 | if (handle == rule->handle) |
3259 | return rule; | |
3260 | } | |
3261 | ||
3262 | return ERR_PTR(-ENOENT); | |
3263 | } | |
3264 | ||
cac20fcd PNA |
3265 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
3266 | const struct nlattr *nla) | |
96518518 PM |
3267 | { |
3268 | if (nla == NULL) | |
3269 | return ERR_PTR(-EINVAL); | |
3270 | ||
cac20fcd | 3271 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
3272 | } |
3273 | ||
3274 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
3275 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
3276 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
3277 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
3278 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
3279 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
3280 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 3281 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 3282 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
3283 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
3284 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 3285 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 3286 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
837830a4 | 3287 | [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
3288 | }; |
3289 | ||
84d7fce6 PNA |
3290 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
3291 | u32 portid, u32 seq, int event, | |
3292 | u32 flags, int family, | |
96518518 PM |
3293 | const struct nft_table *table, |
3294 | const struct nft_chain *chain, | |
8daa8fde PS |
3295 | const struct nft_rule *rule, u64 handle, |
3296 | bool reset) | |
96518518 PM |
3297 | { |
3298 | struct nlmsghdr *nlh; | |
96518518 PM |
3299 | const struct nft_expr *expr, *next; |
3300 | struct nlattr *list; | |
dedb67c4 | 3301 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 3302 | |
19c28b13 PNA |
3303 | nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0, |
3304 | nft_base_seq(net)); | |
3305 | if (!nlh) | |
96518518 PM |
3306 | goto nla_put_failure; |
3307 | ||
96518518 PM |
3308 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
3309 | goto nla_put_failure; | |
3310 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
3311 | goto nla_put_failure; | |
b46f6ded ND |
3312 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
3313 | NFTA_RULE_PAD)) | |
96518518 PM |
3314 | goto nla_put_failure; |
3315 | ||
e189ae16 PNA |
3316 | if (event != NFT_MSG_DELRULE && handle) { |
3317 | if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle), | |
b46f6ded | 3318 | NFTA_RULE_PAD)) |
5e948466 EL |
3319 | goto nla_put_failure; |
3320 | } | |
3321 | ||
b72920f6 PNA |
3322 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) |
3323 | nft_flow_rule_stats(chain, rule); | |
3324 | ||
ae0be8de | 3325 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
3326 | if (list == NULL) |
3327 | goto nla_put_failure; | |
3328 | nft_rule_for_each_expr(expr, next, rule) { | |
8daa8fde | 3329 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0) |
96518518 | 3330 | goto nla_put_failure; |
96518518 PM |
3331 | } |
3332 | nla_nest_end(skb, list); | |
3333 | ||
86f1ec32 PM |
3334 | if (rule->udata) { |
3335 | struct nft_userdata *udata = nft_userdata(rule); | |
3336 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
3337 | udata->data) < 0) | |
3338 | goto nla_put_failure; | |
3339 | } | |
0768b3b3 | 3340 | |
053c095a JB |
3341 | nlmsg_end(skb, nlh); |
3342 | return 0; | |
96518518 PM |
3343 | |
3344 | nla_put_failure: | |
3345 | nlmsg_trim(skb, nlh); | |
3346 | return -1; | |
3347 | } | |
3348 | ||
25e94a99 PNA |
3349 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
3350 | const struct nft_rule *rule, int event) | |
96518518 | 3351 | { |
d59d2f82 | 3352 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
e189ae16 | 3353 | const struct nft_rule *prule; |
96518518 | 3354 | struct sk_buff *skb; |
e189ae16 PNA |
3355 | u64 handle = 0; |
3356 | u16 flags = 0; | |
96518518 PM |
3357 | int err; |
3358 | ||
128ad332 PNA |
3359 | if (!ctx->report && |
3360 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3361 | return; |
96518518 | 3362 | |
96518518 PM |
3363 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
3364 | if (skb == NULL) | |
3365 | goto err; | |
3366 | ||
e189ae16 PNA |
3367 | if (event == NFT_MSG_NEWRULE && |
3368 | !list_is_first(&rule->list, &ctx->chain->rules) && | |
3369 | !list_is_last(&rule->list, &ctx->chain->rules)) { | |
3370 | prule = list_prev_entry(rule, list); | |
3371 | handle = prule->handle; | |
3372 | } | |
3373 | if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE)) | |
3374 | flags |= NLM_F_APPEND; | |
6fb721cf PNA |
3375 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
3376 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
e189ae16 | 3377 | |
84d7fce6 | 3378 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
e189ae16 | 3379 | event, flags, ctx->family, ctx->table, |
8daa8fde | 3380 | ctx->chain, rule, handle, false); |
96518518 PM |
3381 | if (err < 0) { |
3382 | kfree_skb(skb); | |
3383 | goto err; | |
3384 | } | |
3385 | ||
0854db2a | 3386 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 3387 | return; |
96518518 | 3388 | err: |
25e94a99 | 3389 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
3390 | } |
3391 | ||
6e1f760e | 3392 | struct nft_rule_dump_ctx { |
e46abbcc | 3393 | char *table; |
b7263e07 | 3394 | char *chain; |
6e1f760e PNA |
3395 | }; |
3396 | ||
241faece PS |
3397 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
3398 | unsigned int *idx, | |
3399 | struct netlink_callback *cb, | |
3400 | const struct nft_table *table, | |
8daa8fde PS |
3401 | const struct nft_chain *chain, |
3402 | bool reset) | |
241faece PS |
3403 | { |
3404 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 3405 | const struct nft_rule *rule, *prule; |
241faece | 3406 | unsigned int s_idx = cb->args[0]; |
e189ae16 | 3407 | u64 handle; |
241faece | 3408 | |
2c82c7e7 | 3409 | prule = NULL; |
241faece PS |
3410 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
3411 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 3412 | goto cont_skip; |
241faece PS |
3413 | if (*idx < s_idx) |
3414 | goto cont; | |
3415 | if (*idx > s_idx) { | |
3416 | memset(&cb->args[1], 0, | |
3417 | sizeof(cb->args) - sizeof(cb->args[0])); | |
3418 | } | |
e189ae16 PNA |
3419 | if (prule) |
3420 | handle = prule->handle; | |
3421 | else | |
3422 | handle = 0; | |
3423 | ||
241faece PS |
3424 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
3425 | cb->nlh->nlmsg_seq, | |
3426 | NFT_MSG_NEWRULE, | |
3427 | NLM_F_MULTI | NLM_F_APPEND, | |
3428 | table->family, | |
8daa8fde | 3429 | table, chain, rule, handle, reset) < 0) |
310529e6 | 3430 | return 1; |
241faece PS |
3431 | |
3432 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
3433 | cont: | |
2c82c7e7 FW |
3434 | prule = rule; |
3435 | cont_skip: | |
241faece PS |
3436 | (*idx)++; |
3437 | } | |
310529e6 | 3438 | return 0; |
241faece PS |
3439 | } |
3440 | ||
96518518 PM |
3441 | static int nf_tables_dump_rules(struct sk_buff *skb, |
3442 | struct netlink_callback *cb) | |
3443 | { | |
3444 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 3445 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 3446 | struct nft_table *table; |
96518518 | 3447 | const struct nft_chain *chain; |
241faece | 3448 | unsigned int idx = 0; |
99633ab2 | 3449 | struct net *net = sock_net(skb->sk); |
96518518 | 3450 | int family = nfmsg->nfgen_family; |
0854db2a | 3451 | struct nftables_pernet *nft_net; |
8daa8fde PS |
3452 | bool reset = false; |
3453 | ||
3454 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET) | |
3455 | reset = true; | |
96518518 | 3456 | |
e688a7f8 | 3457 | rcu_read_lock(); |
d59d2f82 | 3458 | nft_net = nft_pernet(net); |
34002783 | 3459 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 3460 | |
0854db2a | 3461 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 3462 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
3463 | continue; |
3464 | ||
3465 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
3466 | continue; |
3467 | ||
715849ab | 3468 | if (ctx && ctx->table && ctx->chain) { |
241faece | 3469 | struct rhlist_head *list, *tmp; |
6e1f760e | 3470 | |
241faece PS |
3471 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
3472 | nft_chain_ht_params); | |
3473 | if (!list) | |
3474 | goto done; | |
3475 | ||
3476 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
3477 | if (!nft_is_active(net, chain)) | |
3478 | continue; | |
3479 | __nf_tables_dump_rules(skb, &idx, | |
8daa8fde | 3480 | cb, table, chain, reset); |
241faece | 3481 | break; |
96518518 | 3482 | } |
241faece | 3483 | goto done; |
96518518 | 3484 | } |
241faece PS |
3485 | |
3486 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
8daa8fde PS |
3487 | if (__nf_tables_dump_rules(skb, &idx, |
3488 | cb, table, chain, reset)) | |
241faece PS |
3489 | goto done; |
3490 | } | |
3491 | ||
3492 | if (ctx && ctx->table) | |
3493 | break; | |
96518518 PM |
3494 | } |
3495 | done: | |
e688a7f8 | 3496 | rcu_read_unlock(); |
310529e6 PS |
3497 | |
3498 | cb->args[0] = idx; | |
96518518 PM |
3499 | return skb->len; |
3500 | } | |
3501 | ||
90fd131a FW |
3502 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
3503 | { | |
3504 | const struct nlattr * const *nla = cb->data; | |
3505 | struct nft_rule_dump_ctx *ctx = NULL; | |
3506 | ||
3507 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
3508 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
3509 | if (!ctx) | |
3510 | return -ENOMEM; | |
3511 | ||
3512 | if (nla[NFTA_RULE_TABLE]) { | |
3513 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
3514 | GFP_ATOMIC); | |
3515 | if (!ctx->table) { | |
3516 | kfree(ctx); | |
3517 | return -ENOMEM; | |
3518 | } | |
3519 | } | |
3520 | if (nla[NFTA_RULE_CHAIN]) { | |
3521 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
3522 | GFP_ATOMIC); | |
3523 | if (!ctx->chain) { | |
3524 | kfree(ctx->table); | |
3525 | kfree(ctx); | |
3526 | return -ENOMEM; | |
3527 | } | |
3528 | } | |
3529 | } | |
3530 | ||
3531 | cb->data = ctx; | |
3532 | return 0; | |
3533 | } | |
3534 | ||
6e1f760e PNA |
3535 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
3536 | { | |
e46abbcc PS |
3537 | struct nft_rule_dump_ctx *ctx = cb->data; |
3538 | ||
3539 | if (ctx) { | |
3540 | kfree(ctx->table); | |
b7263e07 | 3541 | kfree(ctx->chain); |
e46abbcc PS |
3542 | kfree(ctx); |
3543 | } | |
6e1f760e PNA |
3544 | return 0; |
3545 | } | |
3546 | ||
d9adf22a | 3547 | /* called with rcu_read_lock held */ |
797d4980 PNA |
3548 | static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info, |
3549 | const struct nlattr * const nla[]) | |
96518518 | 3550 | { |
797d4980 PNA |
3551 | struct netlink_ext_ack *extack = info->extack; |
3552 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 3553 | u8 family = info->nfmsg->nfgen_family; |
96518518 PM |
3554 | const struct nft_chain *chain; |
3555 | const struct nft_rule *rule; | |
797d4980 | 3556 | struct net *net = info->net; |
1b2470e5 | 3557 | struct nft_table *table; |
96518518 | 3558 | struct sk_buff *skb2; |
8daa8fde | 3559 | bool reset = false; |
96518518 PM |
3560 | int err; |
3561 | ||
797d4980 | 3562 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 | 3563 | struct netlink_dump_control c = { |
90fd131a | 3564 | .start= nf_tables_dump_rules_start, |
96518518 | 3565 | .dump = nf_tables_dump_rules, |
6e1f760e | 3566 | .done = nf_tables_dump_rules_done, |
d9adf22a | 3567 | .module = THIS_MODULE, |
90fd131a | 3568 | .data = (void *)nla, |
96518518 | 3569 | }; |
6e1f760e | 3570 | |
797d4980 | 3571 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
3572 | } |
3573 | ||
6001a930 | 3574 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0); |
36dd1bcc PNA |
3575 | if (IS_ERR(table)) { |
3576 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3577 | return PTR_ERR(table); |
36dd1bcc | 3578 | } |
96518518 | 3579 | |
f102d66b | 3580 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3581 | if (IS_ERR(chain)) { |
3582 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3583 | return PTR_ERR(chain); |
36dd1bcc | 3584 | } |
96518518 | 3585 | |
cac20fcd | 3586 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3587 | if (IS_ERR(rule)) { |
3588 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3589 | return PTR_ERR(rule); |
36dd1bcc | 3590 | } |
96518518 | 3591 | |
d9adf22a | 3592 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
3593 | if (!skb2) |
3594 | return -ENOMEM; | |
3595 | ||
8daa8fde PS |
3596 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET) |
3597 | reset = true; | |
3598 | ||
84d7fce6 | 3599 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 3600 | info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
8daa8fde | 3601 | family, table, chain, rule, 0, reset); |
96518518 | 3602 | if (err < 0) |
ee921183 | 3603 | goto err_fill_rule_info; |
96518518 | 3604 | |
ee921183 | 3605 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 3606 | |
ee921183 | 3607 | err_fill_rule_info: |
96518518 PM |
3608 | kfree_skb(skb2); |
3609 | return err; | |
3610 | } | |
3611 | ||
4bedf9ee | 3612 | void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule) |
96518518 | 3613 | { |
29e38801 | 3614 | struct nft_expr *expr, *next; |
96518518 PM |
3615 | |
3616 | /* | |
3617 | * Careful: some expressions might not be initialized in case this | |
3618 | * is called on error from nf_tables_newrule(). | |
3619 | */ | |
3620 | expr = nft_expr_first(rule); | |
31cc578a | 3621 | while (nft_expr_more(rule, expr)) { |
29e38801 | 3622 | next = nft_expr_next(expr); |
62472bce | 3623 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 3624 | expr = next; |
96518518 PM |
3625 | } |
3626 | kfree(rule); | |
3627 | } | |
3628 | ||
4bedf9ee | 3629 | static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae | 3630 | { |
f6ac8585 | 3631 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
3632 | nf_tables_rule_destroy(ctx, rule); |
3633 | } | |
3634 | ||
a654de8f PNA |
3635 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
3636 | { | |
3637 | struct nft_expr *expr, *last; | |
3638 | const struct nft_data *data; | |
3639 | struct nft_rule *rule; | |
3640 | int err; | |
3641 | ||
26b2f552 TY |
3642 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
3643 | return -EMLINK; | |
3644 | ||
a654de8f PNA |
3645 | list_for_each_entry(rule, &chain->rules, list) { |
3646 | if (!nft_is_active_next(ctx->net, rule)) | |
3647 | continue; | |
3648 | ||
3649 | nft_rule_for_each_expr(expr, last, rule) { | |
3650 | if (!expr->ops->validate) | |
3651 | continue; | |
3652 | ||
3653 | err = expr->ops->validate(ctx, expr, &data); | |
3654 | if (err < 0) | |
3655 | return err; | |
3656 | } | |
81ea0106 FW |
3657 | |
3658 | cond_resched(); | |
a654de8f PNA |
3659 | } |
3660 | ||
3661 | return 0; | |
3662 | } | |
3663 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
3664 | ||
3665 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
3666 | { | |
3667 | struct nft_chain *chain; | |
3668 | struct nft_ctx ctx = { | |
3669 | .net = net, | |
3670 | .family = table->family, | |
3671 | }; | |
3672 | int err; | |
3673 | ||
3674 | list_for_each_entry(chain, &table->chains, list) { | |
3675 | if (!nft_is_base_chain(chain)) | |
3676 | continue; | |
3677 | ||
3678 | ctx.chain = chain; | |
3679 | err = nft_chain_validate(&ctx, chain); | |
3680 | if (err < 0) | |
3681 | return err; | |
3682 | } | |
3683 | ||
3684 | return 0; | |
3685 | } | |
3686 | ||
d46fc894 PNA |
3687 | int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set, |
3688 | const struct nft_set_iter *iter, | |
3689 | struct nft_set_elem *elem) | |
3690 | { | |
3691 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
3692 | struct nft_ctx *pctx = (struct nft_ctx *)ctx; | |
3693 | const struct nft_data *data; | |
3694 | int err; | |
3695 | ||
3696 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
3697 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
3698 | return 0; | |
3699 | ||
3700 | data = nft_set_ext_data(ext); | |
3701 | switch (data->verdict.code) { | |
3702 | case NFT_JUMP: | |
3703 | case NFT_GOTO: | |
3704 | pctx->level++; | |
3705 | err = nft_chain_validate(ctx, data->verdict.chain); | |
3706 | if (err < 0) | |
3707 | return err; | |
3708 | pctx->level--; | |
3709 | break; | |
3710 | default: | |
3711 | break; | |
3712 | } | |
3713 | ||
3714 | return 0; | |
3715 | } | |
3716 | ||
d46fc894 PNA |
3717 | int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set) |
3718 | { | |
3719 | u8 genmask = nft_genmask_next(ctx->net); | |
3720 | struct nft_set_elem_catchall *catchall; | |
3721 | struct nft_set_elem elem; | |
3722 | struct nft_set_ext *ext; | |
3723 | int ret = 0; | |
3724 | ||
3725 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
3726 | ext = nft_set_elem_ext(set, catchall->elem); | |
3727 | if (!nft_set_elem_active(ext, genmask)) | |
3728 | continue; | |
3729 | ||
3730 | elem.priv = catchall->elem; | |
3731 | ret = nft_setelem_validate(ctx, set, NULL, &elem); | |
3732 | if (ret < 0) | |
3733 | return ret; | |
3734 | } | |
3735 | ||
3736 | return ret; | |
3737 | } | |
3738 | ||
75dd48e2 | 3739 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3740 | const struct nft_chain *chain, |
75dd48e2 PS |
3741 | const struct nlattr *nla); |
3742 | ||
1081d11b PNA |
3743 | #define NFT_RULE_MAXEXPRS 128 |
3744 | ||
7dab8ee3 PNA |
3745 | static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, |
3746 | const struct nlattr * const nla[]) | |
96518518 | 3747 | { |
7dab8ee3 | 3748 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
3749 | struct netlink_ext_ack *extack = info->extack; |
3750 | unsigned int size, i, n, ulen = 0, usize = 0; | |
3751 | u8 genmask = nft_genmask_next(info->net); | |
3752 | struct nft_rule *rule, *old_rule = NULL; | |
3753 | struct nft_expr_info *expr_info = NULL; | |
ef4b65e5 | 3754 | u8 family = info->nfmsg->nfgen_family; |
3c5e4462 | 3755 | struct nft_flow_rule *flow = NULL; |
7dab8ee3 | 3756 | struct net *net = info->net; |
7dab8ee3 | 3757 | struct nft_userdata *udata; |
96518518 PM |
3758 | struct nft_table *table; |
3759 | struct nft_chain *chain; | |
7dab8ee3 PNA |
3760 | struct nft_trans *trans; |
3761 | u64 handle, pos_handle; | |
96518518 PM |
3762 | struct nft_expr *expr; |
3763 | struct nft_ctx ctx; | |
3764 | struct nlattr *tmp; | |
96518518 | 3765 | int err, rem; |
96518518 | 3766 | |
0854db2a | 3767 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3768 | |
6001a930 PNA |
3769 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3770 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3771 | if (IS_ERR(table)) { |
3772 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3773 | return PTR_ERR(table); |
36dd1bcc | 3774 | } |
96518518 | 3775 | |
837830a4 PNA |
3776 | if (nla[NFTA_RULE_CHAIN]) { |
3777 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], | |
3778 | genmask); | |
3779 | if (IS_ERR(chain)) { | |
3780 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
3781 | return PTR_ERR(chain); | |
3782 | } | |
d0e2c7de PNA |
3783 | if (nft_chain_is_bound(chain)) |
3784 | return -EOPNOTSUPP; | |
3785 | ||
837830a4 | 3786 | } else if (nla[NFTA_RULE_CHAIN_ID]) { |
95f466d2 | 3787 | chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID]); |
837830a4 PNA |
3788 | if (IS_ERR(chain)) { |
3789 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]); | |
3790 | return PTR_ERR(chain); | |
3791 | } | |
3792 | } else { | |
3793 | return -EINVAL; | |
36dd1bcc | 3794 | } |
96518518 PM |
3795 | |
3796 | if (nla[NFTA_RULE_HANDLE]) { | |
3797 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3798 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3799 | if (IS_ERR(rule)) { |
3800 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3801 | return PTR_ERR(rule); |
36dd1bcc | 3802 | } |
96518518 | 3803 | |
7dab8ee3 | 3804 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 3805 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
96518518 | 3806 | return -EEXIST; |
36dd1bcc | 3807 | } |
7dab8ee3 | 3808 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
3809 | old_rule = rule; |
3810 | else | |
3811 | return -EOPNOTSUPP; | |
3812 | } else { | |
7dab8ee3 PNA |
3813 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) || |
3814 | info->nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3815 | return -EINVAL; |
3816 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
3817 | |
3818 | if (chain->use == UINT_MAX) | |
3819 | return -EOVERFLOW; | |
5e948466 | 3820 | |
447750f2 FW |
3821 | if (nla[NFTA_RULE_POSITION]) { |
3822 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3823 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3824 | if (IS_ERR(old_rule)) { | |
3825 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3826 | return PTR_ERR(old_rule); | |
3827 | } | |
75dd48e2 | 3828 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
36d5b291 | 3829 | old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]); |
75dd48e2 PS |
3830 | if (IS_ERR(old_rule)) { |
3831 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3832 | return PTR_ERR(old_rule); | |
3833 | } | |
36dd1bcc | 3834 | } |
5e948466 EL |
3835 | } |
3836 | ||
7dab8ee3 | 3837 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0ca743a5 | 3838 | |
96518518 PM |
3839 | n = 0; |
3840 | size = 0; | |
3841 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
7dab8ee3 PNA |
3842 | expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3843 | sizeof(struct nft_expr_info), | |
3844 | GFP_KERNEL); | |
3845 | if (!expr_info) | |
2a43ecf9 FW |
3846 | return -ENOMEM; |
3847 | ||
96518518 PM |
3848 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3849 | err = -EINVAL; | |
3850 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3c5e4462 | 3851 | goto err_release_expr; |
96518518 | 3852 | if (n == NFT_RULE_MAXEXPRS) |
3c5e4462 | 3853 | goto err_release_expr; |
7dab8ee3 | 3854 | err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]); |
c781471d PNA |
3855 | if (err < 0) { |
3856 | NL_SET_BAD_ATTR(extack, tmp); | |
3c5e4462 | 3857 | goto err_release_expr; |
c781471d | 3858 | } |
7dab8ee3 | 3859 | size += expr_info[n].ops->size; |
96518518 PM |
3860 | n++; |
3861 | } | |
3862 | } | |
9889840f PM |
3863 | /* Check for overflow of dlen field */ |
3864 | err = -EFBIG; | |
3865 | if (size >= 1 << 12) | |
3c5e4462 | 3866 | goto err_release_expr; |
96518518 | 3867 | |
86f1ec32 | 3868 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3869 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3870 | if (ulen > 0) |
3871 | usize = sizeof(struct nft_userdata) + ulen; | |
3872 | } | |
0768b3b3 | 3873 | |
96518518 | 3874 | err = -ENOMEM; |
33758c89 | 3875 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT); |
96518518 | 3876 | if (rule == NULL) |
3c5e4462 | 3877 | goto err_release_expr; |
96518518 | 3878 | |
889f7ee7 | 3879 | nft_activate_next(net, rule); |
0628b123 | 3880 | |
96518518 PM |
3881 | rule->handle = handle; |
3882 | rule->dlen = size; | |
86f1ec32 | 3883 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3884 | |
86f1ec32 PM |
3885 | if (ulen) { |
3886 | udata = nft_userdata(rule); | |
3887 | udata->len = ulen - 1; | |
3888 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3889 | } | |
96518518 | 3890 | |
96518518 PM |
3891 | expr = nft_expr_first(rule); |
3892 | for (i = 0; i < n; i++) { | |
7dab8ee3 | 3893 | err = nf_tables_newexpr(&ctx, &expr_info[i], expr); |
83d9dcba | 3894 | if (err < 0) { |
7dab8ee3 | 3895 | NL_SET_BAD_ATTR(extack, expr_info[i].attr); |
3c5e4462 | 3896 | goto err_release_rule; |
83d9dcba | 3897 | } |
a654de8f | 3898 | |
7dab8ee3 | 3899 | if (expr_info[i].ops->validate) |
00c320f9 | 3900 | nft_validate_state_update(table, NFT_VALIDATE_NEED); |
a654de8f | 3901 | |
7dab8ee3 | 3902 | expr_info[i].ops = NULL; |
96518518 PM |
3903 | expr = nft_expr_next(expr); |
3904 | } | |
3905 | ||
3c5e4462 PNA |
3906 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
3907 | flow = nft_flow_rule_create(net, rule); | |
3908 | if (IS_ERR(flow)) { | |
3909 | err = PTR_ERR(flow); | |
3910 | goto err_release_rule; | |
3911 | } | |
3912 | } | |
3913 | ||
7dab8ee3 | 3914 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) { |
2c964c55 PNA |
3915 | err = nft_delrule(&ctx, old_rule); |
3916 | if (err < 0) | |
3917 | goto err_destroy_flow_rule; | |
3918 | ||
ca089878 | 3919 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3920 | if (trans == NULL) { |
3921 | err = -ENOMEM; | |
3c5e4462 | 3922 | goto err_destroy_flow_rule; |
569ccae6 | 3923 | } |
569ccae6 FW |
3924 | list_add_tail_rcu(&rule->list, &old_rule->list); |
3925 | } else { | |
c9626a2c PNA |
3926 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3927 | if (!trans) { | |
569ccae6 | 3928 | err = -ENOMEM; |
3c5e4462 | 3929 | goto err_destroy_flow_rule; |
569ccae6 FW |
3930 | } |
3931 | ||
7dab8ee3 | 3932 | if (info->nlh->nlmsg_flags & NLM_F_APPEND) { |
569ccae6 FW |
3933 | if (old_rule) |
3934 | list_add_rcu(&rule->list, &old_rule->list); | |
3935 | else | |
3936 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3937 | } else { | |
3938 | if (old_rule) | |
3939 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3940 | else | |
3941 | list_add_rcu(&rule->list, &chain->rules); | |
3942 | } | |
0628b123 | 3943 | } |
7dab8ee3 | 3944 | kvfree(expr_info); |
4fefee57 | 3945 | chain->use++; |
96518518 | 3946 | |
3c5e4462 PNA |
3947 | if (flow) |
3948 | nft_trans_flow_rule(trans) = flow; | |
3949 | ||
00c320f9 | 3950 | if (table->validate_state == NFT_VALIDATE_DO) |
a654de8f PNA |
3951 | return nft_table_validate(net, table); |
3952 | ||
3953 | return 0; | |
3c5e4462 PNA |
3954 | |
3955 | err_destroy_flow_rule: | |
4ca041f9 CIK |
3956 | if (flow) |
3957 | nft_flow_rule_destroy(flow); | |
3c5e4462 | 3958 | err_release_rule: |
26b5a571 | 3959 | nft_rule_expr_deactivate(&ctx, rule, NFT_TRANS_PREPARE_ERROR); |
1240eb93 | 3960 | nf_tables_rule_destroy(&ctx, rule); |
3c5e4462 | 3961 | err_release_expr: |
96518518 | 3962 | for (i = 0; i < n; i++) { |
7dab8ee3 PNA |
3963 | if (expr_info[i].ops) { |
3964 | module_put(expr_info[i].ops->type->owner); | |
3965 | if (expr_info[i].ops->type->release_ops) | |
3966 | expr_info[i].ops->type->release_ops(expr_info[i].ops); | |
b25a31bf | 3967 | } |
96518518 | 3968 | } |
7dab8ee3 PNA |
3969 | kvfree(expr_info); |
3970 | ||
96518518 PM |
3971 | return err; |
3972 | } | |
3973 | ||
1a94e38d | 3974 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3975 | const struct nft_chain *chain, |
1a94e38d PNA |
3976 | const struct nlattr *nla) |
3977 | { | |
d59d2f82 | 3978 | struct nftables_pernet *nft_net = nft_pernet(net); |
1a94e38d PNA |
3979 | u32 id = ntohl(nla_get_be32(nla)); |
3980 | struct nft_trans *trans; | |
3981 | ||
0854db2a | 3982 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
1a94e38d | 3983 | if (trans->msg_type == NFT_MSG_NEWRULE && |
36d5b291 | 3984 | trans->ctx.chain == chain && |
1a94e38d | 3985 | id == nft_trans_rule_id(trans)) |
e3c361b8 | 3986 | return nft_trans_rule(trans); |
1a94e38d PNA |
3987 | } |
3988 | return ERR_PTR(-ENOENT); | |
3989 | } | |
3990 | ||
7dab8ee3 PNA |
3991 | static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info, |
3992 | const struct nlattr * const nla[]) | |
96518518 | 3993 | { |
7dab8ee3 | 3994 | struct netlink_ext_ack *extack = info->extack; |
7dab8ee3 | 3995 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 3996 | u8 family = info->nfmsg->nfgen_family; |
cf9dc09d | 3997 | struct nft_chain *chain = NULL; |
7dab8ee3 PNA |
3998 | struct net *net = info->net; |
3999 | struct nft_table *table; | |
cf9dc09d | 4000 | struct nft_rule *rule; |
0628b123 | 4001 | struct nft_ctx ctx; |
ef4b65e5 | 4002 | int err = 0; |
96518518 | 4003 | |
6001a930 PNA |
4004 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
4005 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4006 | if (IS_ERR(table)) { |
4007 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 4008 | return PTR_ERR(table); |
36dd1bcc | 4009 | } |
96518518 | 4010 | |
cf9dc09d | 4011 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
4012 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
4013 | genmask); | |
36dd1bcc | 4014 | if (IS_ERR(chain)) { |
1fb7696a | 4015 | if (PTR_ERR(chain) == -ENOENT && |
f80a612d FFM |
4016 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE) |
4017 | return 0; | |
4018 | ||
36dd1bcc | 4019 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); |
cf9dc09d | 4020 | return PTR_ERR(chain); |
36dd1bcc | 4021 | } |
d0e2c7de PNA |
4022 | if (nft_chain_is_bound(chain)) |
4023 | return -EOPNOTSUPP; | |
cf9dc09d | 4024 | } |
96518518 | 4025 | |
7dab8ee3 | 4026 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0628b123 | 4027 | |
cf9dc09d PNA |
4028 | if (chain) { |
4029 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 4030 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc | 4031 | if (IS_ERR(rule)) { |
f80a612d FFM |
4032 | if (PTR_ERR(rule) == -ENOENT && |
4033 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE) | |
4034 | return 0; | |
4035 | ||
36dd1bcc | 4036 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
cf9dc09d | 4037 | return PTR_ERR(rule); |
36dd1bcc | 4038 | } |
96518518 | 4039 | |
1a94e38d PNA |
4040 | err = nft_delrule(&ctx, rule); |
4041 | } else if (nla[NFTA_RULE_ID]) { | |
36d5b291 | 4042 | rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]); |
36dd1bcc PNA |
4043 | if (IS_ERR(rule)) { |
4044 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 4045 | return PTR_ERR(rule); |
36dd1bcc | 4046 | } |
1a94e38d | 4047 | |
5e266fe7 | 4048 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 4049 | } else { |
ce24b721 | 4050 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
4051 | } |
4052 | } else { | |
4053 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
4054 | if (!nft_is_active_next(net, chain)) |
4055 | continue; | |
4056 | ||
cf9dc09d | 4057 | ctx.chain = chain; |
ce24b721 | 4058 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
4059 | if (err < 0) |
4060 | break; | |
4061 | } | |
4062 | } | |
4063 | ||
4064 | return err; | |
4065 | } | |
4066 | ||
20a69341 PM |
4067 | /* |
4068 | * Sets | |
4069 | */ | |
e32a4dc6 FW |
4070 | static const struct nft_set_type *nft_set_types[] = { |
4071 | &nft_set_hash_fast_type, | |
4072 | &nft_set_hash_type, | |
4073 | &nft_set_rhash_type, | |
4074 | &nft_set_bitmap_type, | |
4075 | &nft_set_rbtree_type, | |
e6abef61 | 4076 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
7400b063 SB |
4077 | &nft_set_pipapo_avx2_type, |
4078 | #endif | |
e32a4dc6 FW |
4079 | &nft_set_pipapo_type, |
4080 | }; | |
20a69341 | 4081 | |
2b664957 | 4082 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
4083 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
4084 | NFT_SET_EVAL) | |
2b664957 | 4085 | |
71cc0873 | 4086 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 4087 | { |
71cc0873 | 4088 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
4089 | } |
4090 | ||
c50b960c PM |
4091 | /* |
4092 | * Select a set implementation based on the data characteristics and the | |
4093 | * given policy. The total memory use might not be known if no size is | |
4094 | * given, in that case the amount of memory per element is used. | |
4095 | */ | |
4096 | static const struct nft_set_ops * | |
2b664957 PNA |
4097 | nft_select_set_ops(const struct nft_ctx *ctx, |
4098 | const struct nlattr * const nla[], | |
bed4a63e | 4099 | const struct nft_set_desc *desc) |
20a69341 | 4100 | { |
d59d2f82 | 4101 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c50b960c PM |
4102 | const struct nft_set_ops *ops, *bops; |
4103 | struct nft_set_estimate est, best; | |
2b664957 PNA |
4104 | const struct nft_set_type *type; |
4105 | u32 flags = 0; | |
e32a4dc6 | 4106 | int i; |
20a69341 | 4107 | |
0854db2a | 4108 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 4109 | lockdep_nfnl_nft_mutex_not_held(); |
e32a4dc6 | 4110 | |
2b664957 PNA |
4111 | if (nla[NFTA_SET_FLAGS] != NULL) |
4112 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 4113 | |
55af753c PNA |
4114 | bops = NULL; |
4115 | best.size = ~0; | |
4116 | best.lookup = ~0; | |
0b5a7874 | 4117 | best.space = ~0; |
c50b960c | 4118 | |
e32a4dc6 FW |
4119 | for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) { |
4120 | type = nft_set_types[i]; | |
71cc0873 | 4121 | ops = &type->ops; |
2b664957 | 4122 | |
71cc0873 | 4123 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 4124 | continue; |
2b664957 | 4125 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
4126 | continue; |
4127 | ||
bed4a63e | 4128 | switch (desc->policy) { |
c50b960c | 4129 | case NFT_SET_POL_PERFORMANCE: |
55af753c | 4130 | if (est.lookup < best.lookup) |
c50b960c | 4131 | break; |
644e334e PNA |
4132 | if (est.lookup == best.lookup && |
4133 | est.space < best.space) | |
4134 | break; | |
c50b960c PM |
4135 | continue; |
4136 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
4137 | if (!desc->size) { |
4138 | if (est.space < best.space) | |
4139 | break; | |
4140 | if (est.space == best.space && | |
4141 | est.lookup < best.lookup) | |
4142 | break; | |
4f2921ca | 4143 | } else if (est.size < best.size || !bops) { |
c50b960c | 4144 | break; |
0b5a7874 | 4145 | } |
c50b960c PM |
4146 | continue; |
4147 | default: | |
4148 | break; | |
4149 | } | |
4150 | ||
c50b960c PM |
4151 | bops = ops; |
4152 | best = est; | |
20a69341 PM |
4153 | } |
4154 | ||
c50b960c PM |
4155 | if (bops != NULL) |
4156 | return bops; | |
4157 | ||
20a69341 PM |
4158 | return ERR_PTR(-EOPNOTSUPP); |
4159 | } | |
4160 | ||
4161 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
4162 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
4163 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 4164 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 4165 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
4166 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
4167 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
4168 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
4169 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
4170 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
4171 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
4172 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 4173 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
4174 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
4175 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
4176 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
4177 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 4178 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 4179 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
65038428 | 4180 | [NFTA_SET_EXPR] = { .type = NLA_NESTED }, |
48b0ae04 | 4181 | [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED }, |
c50b960c PM |
4182 | }; |
4183 | ||
4184 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
4185 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 4186 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
4187 | }; |
4188 | ||
cac20fcd PNA |
4189 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
4190 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
4191 | { |
4192 | struct nft_set *set; | |
4193 | ||
4194 | if (nla == NULL) | |
4195 | return ERR_PTR(-EINVAL); | |
4196 | ||
d9adf22a | 4197 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
4198 | if (!nla_strcmp(nla, set->name) && |
4199 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
4200 | return set; |
4201 | } | |
4202 | return ERR_PTR(-ENOENT); | |
4203 | } | |
4204 | ||
cac20fcd PNA |
4205 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
4206 | const struct nlattr *nla, | |
4207 | u8 genmask) | |
3ecbfd65 HS |
4208 | { |
4209 | struct nft_set *set; | |
4210 | ||
3ecbfd65 HS |
4211 | list_for_each_entry(set, &table->sets, list) { |
4212 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
4213 | nft_active_genmask(set, genmask)) | |
4214 | return set; | |
4215 | } | |
4216 | return ERR_PTR(-ENOENT); | |
4217 | } | |
4218 | ||
cac20fcd | 4219 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
470ee20e | 4220 | const struct nft_table *table, |
cac20fcd | 4221 | const struct nlattr *nla, u8 genmask) |
958bee14 | 4222 | { |
d59d2f82 | 4223 | struct nftables_pernet *nft_net = nft_pernet(net); |
958bee14 | 4224 | u32 id = ntohl(nla_get_be32(nla)); |
d59d2f82 | 4225 | struct nft_trans *trans; |
958bee14 | 4226 | |
0854db2a | 4227 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
9c7f96fd AK |
4228 | if (trans->msg_type == NFT_MSG_NEWSET) { |
4229 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 4230 | |
9c7f96fd | 4231 | if (id == nft_trans_set_id(trans) && |
470ee20e | 4232 | set->table == table && |
9c7f96fd AK |
4233 | nft_active_genmask(set, genmask)) |
4234 | return set; | |
4235 | } | |
958bee14 PNA |
4236 | } |
4237 | return ERR_PTR(-ENOENT); | |
4238 | } | |
c7a72e3f | 4239 | |
10659cba PNA |
4240 | struct nft_set *nft_set_lookup_global(const struct net *net, |
4241 | const struct nft_table *table, | |
4242 | const struct nlattr *nla_set_name, | |
4243 | const struct nlattr *nla_set_id, | |
4244 | u8 genmask) | |
c7a72e3f PNA |
4245 | { |
4246 | struct nft_set *set; | |
4247 | ||
cac20fcd | 4248 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
4249 | if (IS_ERR(set)) { |
4250 | if (!nla_set_id) | |
4251 | return set; | |
4252 | ||
470ee20e | 4253 | set = nft_set_lookup_byid(net, table, nla_set_id, genmask); |
c7a72e3f PNA |
4254 | } |
4255 | return set; | |
4256 | } | |
10659cba | 4257 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 4258 | |
20a69341 PM |
4259 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
4260 | const char *name) | |
4261 | { | |
4262 | const struct nft_set *i; | |
4263 | const char *p; | |
4264 | unsigned long *inuse; | |
60eb1894 | 4265 | unsigned int n = 0, min = 0; |
20a69341 | 4266 | |
38745490 | 4267 | p = strchr(name, '%'); |
20a69341 PM |
4268 | if (p != NULL) { |
4269 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
4270 | return -EINVAL; | |
4271 | ||
4272 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
4273 | if (inuse == NULL) | |
4274 | return -ENOMEM; | |
60eb1894 | 4275 | cont: |
20a69341 | 4276 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
4277 | int tmp; |
4278 | ||
271c5ca8 | 4279 | if (!nft_is_active_next(ctx->net, i)) |
37a9cc52 | 4280 | continue; |
14662917 | 4281 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 4282 | continue; |
60eb1894 | 4283 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 4284 | continue; |
14662917 | 4285 | |
60eb1894 | 4286 | set_bit(tmp - min, inuse); |
20a69341 PM |
4287 | } |
4288 | ||
53b70287 | 4289 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
4290 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
4291 | min += BITS_PER_BYTE * PAGE_SIZE; | |
4292 | memset(inuse, 0, PAGE_SIZE); | |
4293 | goto cont; | |
4294 | } | |
20a69341 PM |
4295 | free_page((unsigned long)inuse); |
4296 | } | |
4297 | ||
33758c89 | 4298 | set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n); |
38745490 PS |
4299 | if (!set->name) |
4300 | return -ENOMEM; | |
4301 | ||
20a69341 | 4302 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
4303 | if (!nft_is_active_next(ctx->net, i)) |
4304 | continue; | |
e63aaaa6 AY |
4305 | if (!strcmp(set->name, i->name)) { |
4306 | kfree(set->name); | |
7fb6f78d | 4307 | set->name = NULL; |
20a69341 | 4308 | return -ENFILE; |
e63aaaa6 | 4309 | } |
20a69341 PM |
4310 | } |
4311 | return 0; | |
4312 | } | |
4313 | ||
917d80d3 | 4314 | int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
8e1102d5 FW |
4315 | { |
4316 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
4317 | u64 max = (u64)(~((u64)0)); | |
4318 | ||
4319 | max = div_u64(max, NSEC_PER_MSEC); | |
4320 | if (ms >= max) | |
4321 | return -ERANGE; | |
4322 | ||
4323 | ms *= NSEC_PER_MSEC; | |
4324 | *result = nsecs_to_jiffies64(ms); | |
4325 | return 0; | |
4326 | } | |
4327 | ||
917d80d3 | 4328 | __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 4329 | { |
3b15d09f | 4330 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
4331 | } |
4332 | ||
f3a2181e SB |
4333 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
4334 | const struct nft_set *set) | |
4335 | { | |
4336 | struct nlattr *concat, *field; | |
4337 | int i; | |
4338 | ||
4339 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
4340 | if (!concat) | |
4341 | return -ENOMEM; | |
4342 | ||
4343 | for (i = 0; i < set->field_count; i++) { | |
4344 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
4345 | if (!field) | |
4346 | return -ENOMEM; | |
4347 | ||
4348 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
4349 | htonl(set->field_len[i]))) | |
4350 | return -ENOMEM; | |
4351 | ||
4352 | nla_nest_end(skb, field); | |
4353 | } | |
4354 | ||
4355 | nla_nest_end(skb, concat); | |
4356 | ||
4357 | return 0; | |
4358 | } | |
4359 | ||
20a69341 PM |
4360 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
4361 | const struct nft_set *set, u16 event, u16 flags) | |
4362 | { | |
123b9961 PNA |
4363 | u64 timeout = READ_ONCE(set->timeout); |
4364 | u32 gc_int = READ_ONCE(set->gc_int); | |
128ad332 | 4365 | u32 portid = ctx->portid; |
123b9961 | 4366 | struct nlmsghdr *nlh; |
65038428 | 4367 | struct nlattr *nest; |
128ad332 | 4368 | u32 seq = ctx->seq; |
48b0ae04 | 4369 | int i; |
20a69341 | 4370 | |
dedb67c4 | 4371 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
4372 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
4373 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
4374 | if (!nlh) | |
20a69341 PM |
4375 | goto nla_put_failure; |
4376 | ||
20a69341 PM |
4377 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
4378 | goto nla_put_failure; | |
4379 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
4380 | goto nla_put_failure; | |
3ecbfd65 HS |
4381 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
4382 | NFTA_SET_PAD)) | |
4383 | goto nla_put_failure; | |
28339b21 PNA |
4384 | |
4385 | if (event == NFT_MSG_DELSET) { | |
4386 | nlmsg_end(skb, nlh); | |
4387 | return 0; | |
4388 | } | |
4389 | ||
20a69341 PM |
4390 | if (set->flags != 0) |
4391 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
4392 | goto nla_put_failure; | |
4393 | ||
4394 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
4395 | goto nla_put_failure; | |
4396 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
4397 | goto nla_put_failure; | |
4398 | if (set->flags & NFT_SET_MAP) { | |
4399 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
4400 | goto nla_put_failure; | |
4401 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
4402 | goto nla_put_failure; | |
4403 | } | |
8aeff920 PNA |
4404 | if (set->flags & NFT_SET_OBJECT && |
4405 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
4406 | goto nla_put_failure; | |
20a69341 | 4407 | |
123b9961 | 4408 | if (timeout && |
d3e2a111 | 4409 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
123b9961 | 4410 | nf_jiffies64_to_msecs(timeout), |
b46f6ded | 4411 | NFTA_SET_PAD)) |
761da293 | 4412 | goto nla_put_failure; |
123b9961 PNA |
4413 | if (gc_int && |
4414 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(gc_int))) | |
761da293 PM |
4415 | goto nla_put_failure; |
4416 | ||
9363dc4b AB |
4417 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
4418 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
4419 | goto nla_put_failure; | |
4420 | } | |
4421 | ||
6f03bf43 PNA |
4422 | if (set->udata && |
4423 | nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) | |
e6d8ecac CFG |
4424 | goto nla_put_failure; |
4425 | ||
65038428 PNA |
4426 | nest = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
4427 | if (!nest) | |
c50b960c PM |
4428 | goto nla_put_failure; |
4429 | if (set->size && | |
4430 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
4431 | goto nla_put_failure; | |
f3a2181e SB |
4432 | |
4433 | if (set->field_count > 1 && | |
4434 | nf_tables_fill_set_concat(skb, set)) | |
4435 | goto nla_put_failure; | |
4436 | ||
65038428 PNA |
4437 | nla_nest_end(skb, nest); |
4438 | ||
8cfd9b0f | 4439 | if (set->num_exprs == 1) { |
65038428 | 4440 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR); |
8daa8fde | 4441 | if (nf_tables_fill_expr_info(skb, set->exprs[0], false) < 0) |
65038428 PNA |
4442 | goto nla_put_failure; |
4443 | ||
4444 | nla_nest_end(skb, nest); | |
48b0ae04 PNA |
4445 | } else if (set->num_exprs > 1) { |
4446 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS); | |
4447 | if (nest == NULL) | |
65038428 PNA |
4448 | goto nla_put_failure; |
4449 | ||
48b0ae04 PNA |
4450 | for (i = 0; i < set->num_exprs; i++) { |
4451 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, | |
8daa8fde | 4452 | set->exprs[i], false) < 0) |
48b0ae04 PNA |
4453 | goto nla_put_failure; |
4454 | } | |
65038428 PNA |
4455 | nla_nest_end(skb, nest); |
4456 | } | |
c50b960c | 4457 | |
053c095a JB |
4458 | nlmsg_end(skb, nlh); |
4459 | return 0; | |
20a69341 PM |
4460 | |
4461 | nla_put_failure: | |
4462 | nlmsg_trim(skb, nlh); | |
4463 | return -1; | |
4464 | } | |
4465 | ||
25e94a99 PNA |
4466 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
4467 | const struct nft_set *set, int event, | |
4468 | gfp_t gfp_flags) | |
20a69341 | 4469 | { |
d59d2f82 | 4470 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
128ad332 | 4471 | u32 portid = ctx->portid; |
6fb721cf PNA |
4472 | struct sk_buff *skb; |
4473 | u16 flags = 0; | |
20a69341 PM |
4474 | int err; |
4475 | ||
128ad332 PNA |
4476 | if (!ctx->report && |
4477 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 4478 | return; |
20a69341 | 4479 | |
31f8441c | 4480 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
4481 | if (skb == NULL) |
4482 | goto err; | |
4483 | ||
6fb721cf PNA |
4484 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
4485 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
4486 | ||
4487 | err = nf_tables_fill_set(skb, ctx, set, event, flags); | |
20a69341 PM |
4488 | if (err < 0) { |
4489 | kfree_skb(skb); | |
4490 | goto err; | |
4491 | } | |
4492 | ||
0854db2a | 4493 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 4494 | return; |
20a69341 | 4495 | err: |
25e94a99 | 4496 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
4497 | } |
4498 | ||
5b96af77 | 4499 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
4500 | { |
4501 | const struct nft_set *set; | |
4502 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
4503 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
4504 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 4505 | struct nft_ctx *ctx = cb->data, ctx_set; |
0854db2a | 4506 | struct nftables_pernet *nft_net; |
c9c8e485 PNA |
4507 | |
4508 | if (cb->args[1]) | |
4509 | return skb->len; | |
4510 | ||
e688a7f8 | 4511 | rcu_read_lock(); |
d59d2f82 | 4512 | nft_net = nft_pernet(net); |
34002783 | 4513 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 4514 | |
0854db2a | 4515 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 4516 | if (ctx->family != NFPROTO_UNSPEC && |
98319cb9 | 4517 | ctx->family != table->family) |
36596dad PNA |
4518 | continue; |
4519 | ||
4520 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
4521 | continue; |
4522 | ||
36596dad PNA |
4523 | if (cur_table) { |
4524 | if (cur_table != table) | |
c9c8e485 PNA |
4525 | continue; |
4526 | ||
36596dad | 4527 | cur_table = NULL; |
c9c8e485 | 4528 | } |
36596dad PNA |
4529 | idx = 0; |
4530 | list_for_each_entry_rcu(set, &table->sets, list) { | |
4531 | if (idx < s_idx) | |
4532 | goto cont; | |
4533 | if (!nft_is_active(net, set)) | |
4534 | goto cont; | |
5b96af77 | 4535 | |
36596dad PNA |
4536 | ctx_set = *ctx; |
4537 | ctx_set.table = table; | |
98319cb9 | 4538 | ctx_set.family = table->family; |
c9c8e485 | 4539 | |
36596dad PNA |
4540 | if (nf_tables_fill_set(skb, &ctx_set, set, |
4541 | NFT_MSG_NEWSET, | |
4542 | NLM_F_MULTI) < 0) { | |
4543 | cb->args[0] = idx; | |
4544 | cb->args[2] = (unsigned long) table; | |
4545 | goto done; | |
c9c8e485 | 4546 | } |
36596dad | 4547 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 4548 | cont: |
36596dad | 4549 | idx++; |
c9c8e485 | 4550 | } |
36596dad PNA |
4551 | if (s_idx) |
4552 | s_idx = 0; | |
c9c8e485 PNA |
4553 | } |
4554 | cb->args[1] = 1; | |
4555 | done: | |
e688a7f8 | 4556 | rcu_read_unlock(); |
c9c8e485 PNA |
4557 | return skb->len; |
4558 | } | |
4559 | ||
90fd131a FW |
4560 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
4561 | { | |
4562 | struct nft_ctx *ctx_dump = NULL; | |
4563 | ||
4564 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
4565 | if (ctx_dump == NULL) | |
4566 | return -ENOMEM; | |
4567 | ||
4568 | cb->data = ctx_dump; | |
4569 | return 0; | |
4570 | } | |
4571 | ||
5b96af77 | 4572 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 4573 | { |
5b96af77 PNA |
4574 | kfree(cb->data); |
4575 | return 0; | |
20a69341 PM |
4576 | } |
4577 | ||
d9adf22a | 4578 | /* called with rcu_read_lock held */ |
797d4980 PNA |
4579 | static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info, |
4580 | const struct nlattr * const nla[]) | |
20a69341 | 4581 | { |
797d4980 PNA |
4582 | struct netlink_ext_ack *extack = info->extack; |
4583 | u8 genmask = nft_genmask_cur(info->net); | |
67086651 PNA |
4584 | u8 family = info->nfmsg->nfgen_family; |
4585 | struct nft_table *table = NULL; | |
797d4980 | 4586 | struct net *net = info->net; |
20a69341 | 4587 | const struct nft_set *set; |
20a69341 | 4588 | struct sk_buff *skb2; |
797d4980 | 4589 | struct nft_ctx ctx; |
20a69341 PM |
4590 | int err; |
4591 | ||
67086651 PNA |
4592 | if (nla[NFTA_SET_TABLE]) { |
4593 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, | |
4594 | genmask, 0); | |
4595 | if (IS_ERR(table)) { | |
4596 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4597 | return PTR_ERR(table); | |
4598 | } | |
4599 | } | |
4600 | ||
4601 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); | |
20a69341 | 4602 | |
797d4980 | 4603 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
20a69341 | 4604 | struct netlink_dump_control c = { |
90fd131a | 4605 | .start = nf_tables_dump_sets_start, |
20a69341 | 4606 | .dump = nf_tables_dump_sets, |
5b96af77 | 4607 | .done = nf_tables_dump_sets_done, |
90fd131a | 4608 | .data = &ctx, |
d9adf22a | 4609 | .module = THIS_MODULE, |
20a69341 | 4610 | }; |
5b96af77 | 4611 | |
797d4980 | 4612 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
20a69341 PM |
4613 | } |
4614 | ||
c9c8e485 | 4615 | /* Only accept unspec with dump */ |
ef4b65e5 | 4616 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
c9c8e485 | 4617 | return -EAFNOSUPPORT; |
eaa2bcd6 PT |
4618 | if (!nla[NFTA_SET_TABLE]) |
4619 | return -EINVAL; | |
c9c8e485 | 4620 | |
67086651 | 4621 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
4622 | if (IS_ERR(set)) |
4623 | return PTR_ERR(set); | |
4624 | ||
d9adf22a | 4625 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
4626 | if (skb2 == NULL) |
4627 | return -ENOMEM; | |
4628 | ||
4629 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
4630 | if (err < 0) | |
ee921183 | 4631 | goto err_fill_set_info; |
20a69341 | 4632 | |
ee921183 | 4633 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
20a69341 | 4634 | |
ee921183 | 4635 | err_fill_set_info: |
20a69341 PM |
4636 | kfree_skb(skb2); |
4637 | return err; | |
4638 | } | |
4639 | ||
f3a2181e SB |
4640 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
4641 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
4642 | }; | |
4643 | ||
4644 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
4645 | struct nft_set_desc *desc) | |
4646 | { | |
4647 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
4648 | u32 len; | |
4649 | int err; | |
4650 | ||
fecf31ee PNA |
4651 | if (desc->field_count >= ARRAY_SIZE(desc->field_len)) |
4652 | return -E2BIG; | |
4653 | ||
f3a2181e SB |
4654 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, |
4655 | nft_concat_policy, NULL); | |
4656 | if (err < 0) | |
4657 | return err; | |
4658 | ||
4659 | if (!tb[NFTA_SET_FIELD_LEN]) | |
4660 | return -EINVAL; | |
4661 | ||
4662 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
fecf31ee PNA |
4663 | if (!len || len > U8_MAX) |
4664 | return -EINVAL; | |
f3a2181e SB |
4665 | |
4666 | desc->field_len[desc->field_count++] = len; | |
4667 | ||
4668 | return 0; | |
4669 | } | |
4670 | ||
4671 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
4672 | const struct nlattr *nla) | |
4673 | { | |
4674 | struct nlattr *attr; | |
fecf31ee PNA |
4675 | u32 num_regs = 0; |
4676 | int rem, err, i; | |
f3a2181e SB |
4677 | |
4678 | nla_for_each_nested(attr, nla, rem) { | |
4679 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
4680 | return -EINVAL; | |
4681 | ||
4682 | err = nft_set_desc_concat_parse(attr, desc); | |
4683 | if (err < 0) | |
4684 | return err; | |
4685 | } | |
4686 | ||
fecf31ee PNA |
4687 | for (i = 0; i < desc->field_count; i++) |
4688 | num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32)); | |
4689 | ||
4690 | if (num_regs > NFT_REG32_COUNT) | |
4691 | return -E2BIG; | |
4692 | ||
f3a2181e SB |
4693 | return 0; |
4694 | } | |
4695 | ||
f7e840ee | 4696 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
4697 | const struct nlattr *nla) |
4698 | { | |
4699 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
4700 | int err; | |
4701 | ||
8cb08174 JB |
4702 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
4703 | nft_set_desc_policy, NULL); | |
c50b960c PM |
4704 | if (err < 0) |
4705 | return err; | |
4706 | ||
4707 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
4708 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
4709 | if (da[NFTA_SET_DESC_CONCAT]) |
4710 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 4711 | |
f3a2181e | 4712 | return err; |
c50b960c PM |
4713 | } |
4714 | ||
a8fe4154 PNA |
4715 | static int nft_set_expr_alloc(struct nft_ctx *ctx, struct nft_set *set, |
4716 | const struct nlattr * const *nla, | |
4717 | struct nft_expr **exprs, int *num_exprs, | |
4718 | u32 flags) | |
4719 | { | |
4720 | struct nft_expr *expr; | |
4721 | int err, i; | |
4722 | ||
4723 | if (nla[NFTA_SET_EXPR]) { | |
4724 | expr = nft_set_elem_expr_alloc(ctx, set, nla[NFTA_SET_EXPR]); | |
4725 | if (IS_ERR(expr)) { | |
4726 | err = PTR_ERR(expr); | |
4727 | goto err_set_expr_alloc; | |
4728 | } | |
4729 | exprs[0] = expr; | |
4730 | (*num_exprs)++; | |
4731 | } else if (nla[NFTA_SET_EXPRESSIONS]) { | |
4732 | struct nlattr *tmp; | |
4733 | int left; | |
4734 | ||
4735 | if (!(flags & NFT_SET_EXPR)) { | |
4736 | err = -EINVAL; | |
4737 | goto err_set_expr_alloc; | |
4738 | } | |
4739 | i = 0; | |
4740 | nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) { | |
4741 | if (i == NFT_SET_EXPR_MAX) { | |
4742 | err = -E2BIG; | |
4743 | goto err_set_expr_alloc; | |
4744 | } | |
4745 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
4746 | err = -EINVAL; | |
4747 | goto err_set_expr_alloc; | |
4748 | } | |
4749 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
4750 | if (IS_ERR(expr)) { | |
4751 | err = PTR_ERR(expr); | |
4752 | goto err_set_expr_alloc; | |
4753 | } | |
4754 | exprs[i++] = expr; | |
4755 | (*num_exprs)++; | |
4756 | } | |
4757 | } | |
4758 | ||
4759 | return 0; | |
4760 | ||
4761 | err_set_expr_alloc: | |
4762 | for (i = 0; i < *num_exprs; i++) | |
4763 | nft_expr_destroy(ctx, exprs[i]); | |
4764 | ||
4765 | return err; | |
4766 | } | |
4767 | ||
f6594c37 PNA |
4768 | static bool nft_set_is_same(const struct nft_set *set, |
4769 | const struct nft_set_desc *desc, | |
4770 | struct nft_expr *exprs[], u32 num_exprs, u32 flags) | |
4771 | { | |
4772 | int i; | |
4773 | ||
4774 | if (set->ktype != desc->ktype || | |
4775 | set->dtype != desc->dtype || | |
4776 | set->flags != flags || | |
4777 | set->klen != desc->klen || | |
4778 | set->dlen != desc->dlen || | |
4779 | set->field_count != desc->field_count || | |
4780 | set->num_exprs != num_exprs) | |
4781 | return false; | |
4782 | ||
4783 | for (i = 0; i < desc->field_count; i++) { | |
4784 | if (set->field_len[i] != desc->field_len[i]) | |
4785 | return false; | |
4786 | } | |
4787 | ||
4788 | for (i = 0; i < num_exprs; i++) { | |
4789 | if (set->exprs[i]->ops != exprs[i]->ops) | |
4790 | return false; | |
4791 | } | |
4792 | ||
4793 | return true; | |
4794 | } | |
4795 | ||
7dab8ee3 PNA |
4796 | static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, |
4797 | const struct nlattr * const nla[]) | |
20a69341 | 4798 | { |
7dab8ee3 PNA |
4799 | struct netlink_ext_ack *extack = info->extack; |
4800 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 4801 | u8 family = info->nfmsg->nfgen_family; |
20a69341 | 4802 | const struct nft_set_ops *ops; |
7dab8ee3 PNA |
4803 | struct net *net = info->net; |
4804 | struct nft_set_desc desc; | |
20a69341 | 4805 | struct nft_table *table; |
7dab8ee3 | 4806 | unsigned char *udata; |
20a69341 PM |
4807 | struct nft_set *set; |
4808 | struct nft_ctx ctx; | |
6c8774a9 | 4809 | size_t alloc_size; |
a8fe4154 | 4810 | int num_exprs = 0; |
7dab8ee3 PNA |
4811 | char *name; |
4812 | int err, i; | |
e6d8ecac | 4813 | u16 udlen; |
bed4a63e | 4814 | u32 flags; |
7dab8ee3 | 4815 | u64 size; |
20a69341 PM |
4816 | |
4817 | if (nla[NFTA_SET_TABLE] == NULL || | |
4818 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
4819 | nla[NFTA_SET_KEY_LEN] == NULL || |
4820 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
4821 | return -EINVAL; |
4822 | ||
c50b960c PM |
4823 | memset(&desc, 0, sizeof(desc)); |
4824 | ||
bed4a63e | 4825 | desc.ktype = NFT_DATA_VALUE; |
20a69341 | 4826 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { |
bed4a63e PNA |
4827 | desc.ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); |
4828 | if ((desc.ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
20a69341 PM |
4829 | return -EINVAL; |
4830 | } | |
4831 | ||
c50b960c | 4832 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 4833 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4834 | return -EINVAL; |
4835 | ||
4836 | flags = 0; | |
4837 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
4838 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
4839 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 4840 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 | 4841 | NFT_SET_MAP | NFT_SET_EVAL | |
b4e70d8d | 4842 | NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR)) |
d9583cdf | 4843 | return -EOPNOTSUPP; |
8aeff920 | 4844 | /* Only one of these operations is supported */ |
acab7131 FW |
4845 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
4846 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
4847 | return -EOPNOTSUPP; | |
4848 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
4849 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 4850 | return -EOPNOTSUPP; |
20a69341 PM |
4851 | } |
4852 | ||
bed4a63e | 4853 | desc.dtype = 0; |
20a69341 PM |
4854 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
4855 | if (!(flags & NFT_SET_MAP)) | |
4856 | return -EINVAL; | |
4857 | ||
bed4a63e PNA |
4858 | desc.dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); |
4859 | if ((desc.dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
4860 | desc.dtype != NFT_DATA_VERDICT) | |
20a69341 PM |
4861 | return -EINVAL; |
4862 | ||
bed4a63e | 4863 | if (desc.dtype != NFT_DATA_VERDICT) { |
20a69341 PM |
4864 | if (nla[NFTA_SET_DATA_LEN] == NULL) |
4865 | return -EINVAL; | |
c50b960c | 4866 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 4867 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4868 | return -EINVAL; |
4869 | } else | |
7d740264 | 4870 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
4871 | } else if (flags & NFT_SET_MAP) |
4872 | return -EINVAL; | |
4873 | ||
8aeff920 PNA |
4874 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
4875 | if (!(flags & NFT_SET_OBJECT)) | |
4876 | return -EINVAL; | |
4877 | ||
bed4a63e PNA |
4878 | desc.objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); |
4879 | if (desc.objtype == NFT_OBJECT_UNSPEC || | |
4880 | desc.objtype > NFT_OBJECT_MAX) | |
d9583cdf | 4881 | return -EOPNOTSUPP; |
8aeff920 PNA |
4882 | } else if (flags & NFT_SET_OBJECT) |
4883 | return -EINVAL; | |
4884 | else | |
bed4a63e | 4885 | desc.objtype = NFT_OBJECT_UNSPEC; |
8aeff920 | 4886 | |
bed4a63e | 4887 | desc.timeout = 0; |
761da293 PM |
4888 | if (nla[NFTA_SET_TIMEOUT] != NULL) { |
4889 | if (!(flags & NFT_SET_TIMEOUT)) | |
4890 | return -EINVAL; | |
8e1102d5 | 4891 | |
bed4a63e | 4892 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &desc.timeout); |
8e1102d5 FW |
4893 | if (err) |
4894 | return err; | |
761da293 | 4895 | } |
bed4a63e | 4896 | desc.gc_int = 0; |
761da293 PM |
4897 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { |
4898 | if (!(flags & NFT_SET_TIMEOUT)) | |
4899 | return -EINVAL; | |
bed4a63e | 4900 | desc.gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); |
761da293 PM |
4901 | } |
4902 | ||
bed4a63e | 4903 | desc.policy = NFT_SET_POL_PERFORMANCE; |
c50b960c | 4904 | if (nla[NFTA_SET_POLICY] != NULL) |
bed4a63e | 4905 | desc.policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); |
c50b960c PM |
4906 | |
4907 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4908 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4909 | if (err < 0) |
4910 | return err; | |
1b6345d4 PNA |
4911 | |
4912 | if (desc.field_count > 1 && !(flags & NFT_SET_CONCAT)) | |
4913 | return -EINVAL; | |
4914 | } else if (flags & NFT_SET_CONCAT) { | |
4915 | return -EINVAL; | |
c50b960c PM |
4916 | } |
4917 | ||
48b0ae04 | 4918 | if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS]) |
d56aab26 PNA |
4919 | desc.expr = true; |
4920 | ||
6001a930 PNA |
4921 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask, |
4922 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4923 | if (IS_ERR(table)) { |
4924 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4925 | return PTR_ERR(table); |
36dd1bcc | 4926 | } |
20a69341 | 4927 | |
7dab8ee3 | 4928 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
20a69341 | 4929 | |
cac20fcd | 4930 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4931 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4932 | if (PTR_ERR(set) != -ENOENT) { |
4933 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4934 | return PTR_ERR(set); |
36dd1bcc | 4935 | } |
1a28ad74 | 4936 | } else { |
a8fe4154 PNA |
4937 | struct nft_expr *exprs[NFT_SET_EXPR_MAX] = {}; |
4938 | ||
7dab8ee3 | 4939 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 4940 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); |
20a69341 | 4941 | return -EEXIST; |
36dd1bcc | 4942 | } |
7dab8ee3 | 4943 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
20a69341 | 4944 | return -EOPNOTSUPP; |
36dd1bcc | 4945 | |
a8fe4154 PNA |
4946 | err = nft_set_expr_alloc(&ctx, set, nla, exprs, &num_exprs, flags); |
4947 | if (err < 0) | |
4948 | return err; | |
4949 | ||
f6594c37 PNA |
4950 | err = 0; |
4951 | if (!nft_set_is_same(set, &desc, exprs, num_exprs, flags)) { | |
4952 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
4953 | err = -EEXIST; | |
4954 | } | |
4955 | ||
a8fe4154 PNA |
4956 | for (i = 0; i < num_exprs; i++) |
4957 | nft_expr_destroy(&ctx, exprs[i]); | |
4958 | ||
123b9961 PNA |
4959 | if (err < 0) |
4960 | return err; | |
4961 | ||
4962 | return __nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set, &desc); | |
20a69341 PM |
4963 | } |
4964 | ||
7dab8ee3 | 4965 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
20a69341 PM |
4966 | return -ENOENT; |
4967 | ||
bed4a63e | 4968 | ops = nft_select_set_ops(&ctx, nla, &desc); |
20a69341 PM |
4969 | if (IS_ERR(ops)) |
4970 | return PTR_ERR(ops); | |
4971 | ||
e6d8ecac CFG |
4972 | udlen = 0; |
4973 | if (nla[NFTA_SET_USERDATA]) | |
4974 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
4975 | ||
20a69341 PM |
4976 | size = 0; |
4977 | if (ops->privsize != NULL) | |
347b408d | 4978 | size = ops->privsize(nla, &desc); |
6c8774a9 | 4979 | alloc_size = sizeof(*set) + size + udlen; |
45928afe | 4980 | if (alloc_size < size || alloc_size > INT_MAX) |
6c8774a9 | 4981 | return -ENOMEM; |
33758c89 | 4982 | set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT); |
24d19826 FW |
4983 | if (!set) |
4984 | return -ENOMEM; | |
20a69341 | 4985 | |
33758c89 | 4986 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT); |
38745490 PS |
4987 | if (!name) { |
4988 | err = -ENOMEM; | |
65038428 | 4989 | goto err_set_name; |
38745490 PS |
4990 | } |
4991 | ||
20a69341 | 4992 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 4993 | kfree(name); |
20a69341 | 4994 | if (err < 0) |
ad9f151e PNA |
4995 | goto err_set_name; |
4996 | ||
4997 | udata = NULL; | |
4998 | if (udlen) { | |
4999 | udata = set->data + size; | |
5000 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
5001 | } | |
5002 | ||
5003 | INIT_LIST_HEAD(&set->bindings); | |
5004 | INIT_LIST_HEAD(&set->catchall_list); | |
5005 | set->table = table; | |
5006 | write_pnet(&set->net, net); | |
5007 | set->ops = ops; | |
bed4a63e | 5008 | set->ktype = desc.ktype; |
ad9f151e | 5009 | set->klen = desc.klen; |
bed4a63e PNA |
5010 | set->dtype = desc.dtype; |
5011 | set->objtype = desc.objtype; | |
ad9f151e PNA |
5012 | set->dlen = desc.dlen; |
5013 | set->flags = flags; | |
5014 | set->size = desc.size; | |
bed4a63e | 5015 | set->policy = desc.policy; |
ad9f151e PNA |
5016 | set->udlen = udlen; |
5017 | set->udata = udata; | |
bed4a63e PNA |
5018 | set->timeout = desc.timeout; |
5019 | set->gc_int = desc.gc_int; | |
ad9f151e PNA |
5020 | |
5021 | set->field_count = desc.field_count; | |
5022 | for (i = 0; i < desc.field_count; i++) | |
5023 | set->field_len[i] = desc.field_len[i]; | |
5024 | ||
5025 | err = ops->init(set, &desc, nla); | |
5026 | if (err < 0) | |
5027 | goto err_set_init; | |
65038428 | 5028 | |
a8fe4154 PNA |
5029 | err = nft_set_expr_alloc(&ctx, set, nla, set->exprs, &num_exprs, flags); |
5030 | if (err < 0) | |
5031 | goto err_set_destroy; | |
20a69341 | 5032 | |
a8fe4154 | 5033 | set->num_exprs = num_exprs; |
3ecbfd65 | 5034 | set->handle = nf_tables_alloc_handle(table); |
212ed75d | 5035 | INIT_LIST_HEAD(&set->pending_update); |
20a69341 | 5036 | |
958bee14 | 5037 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 5038 | if (err < 0) |
ad9f151e | 5039 | goto err_set_expr_alloc; |
20a69341 | 5040 | |
e688a7f8 | 5041 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 5042 | table->use++; |
20a69341 PM |
5043 | return 0; |
5044 | ||
ad9f151e | 5045 | err_set_expr_alloc: |
8cfd9b0f PNA |
5046 | for (i = 0; i < set->num_exprs; i++) |
5047 | nft_expr_destroy(&ctx, set->exprs[i]); | |
a8fe4154 | 5048 | err_set_destroy: |
628bd3e4 | 5049 | ops->destroy(&ctx, set); |
ad9f151e | 5050 | err_set_init: |
2f6adf48 | 5051 | kfree(set->name); |
65038428 | 5052 | err_set_name: |
1ff75a3e | 5053 | kvfree(set); |
20a69341 PM |
5054 | return err; |
5055 | } | |
5056 | ||
aaa31047 PNA |
5057 | static void nft_set_catchall_destroy(const struct nft_ctx *ctx, |
5058 | struct nft_set *set) | |
5059 | { | |
0f7d9b31 | 5060 | struct nft_set_elem_catchall *next, *catchall; |
aaa31047 | 5061 | |
0f7d9b31 | 5062 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
aaa31047 | 5063 | list_del_rcu(&catchall->list); |
628bd3e4 | 5064 | nf_tables_set_elem_destroy(ctx, set, catchall->elem); |
ae089831 | 5065 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
5066 | } |
5067 | } | |
5068 | ||
0c2a85ed | 5069 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
20a69341 | 5070 | { |
8cfd9b0f PNA |
5071 | int i; |
5072 | ||
273fe3f1 PNA |
5073 | if (WARN_ON(set->use > 0)) |
5074 | return; | |
5075 | ||
8cfd9b0f PNA |
5076 | for (i = 0; i < set->num_exprs; i++) |
5077 | nft_expr_destroy(ctx, set->exprs[i]); | |
65038428 | 5078 | |
628bd3e4 | 5079 | set->ops->destroy(ctx, set); |
aaa31047 | 5080 | nft_set_catchall_destroy(ctx, set); |
38745490 | 5081 | kfree(set->name); |
1ff75a3e | 5082 | kvfree(set); |
20a69341 PM |
5083 | } |
5084 | ||
7dab8ee3 PNA |
5085 | static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info, |
5086 | const struct nlattr * const nla[]) | |
20a69341 | 5087 | { |
7dab8ee3 PNA |
5088 | struct netlink_ext_ack *extack = info->extack; |
5089 | u8 genmask = nft_genmask_next(info->net); | |
67086651 | 5090 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 5091 | struct net *net = info->net; |
36dd1bcc | 5092 | const struct nlattr *attr; |
67086651 | 5093 | struct nft_table *table; |
20a69341 PM |
5094 | struct nft_set *set; |
5095 | struct nft_ctx ctx; | |
20a69341 | 5096 | |
ef4b65e5 | 5097 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
ec2c9935 | 5098 | return -EAFNOSUPPORT; |
20a69341 | 5099 | |
67086651 PNA |
5100 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
5101 | genmask, NETLINK_CB(skb).portid); | |
5102 | if (IS_ERR(table)) { | |
5103 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
5104 | return PTR_ERR(table); | |
5105 | } | |
20a69341 | 5106 | |
36dd1bcc PNA |
5107 | if (nla[NFTA_SET_HANDLE]) { |
5108 | attr = nla[NFTA_SET_HANDLE]; | |
67086651 | 5109 | set = nft_set_lookup_byhandle(table, attr, genmask); |
36dd1bcc PNA |
5110 | } else { |
5111 | attr = nla[NFTA_SET_NAME]; | |
67086651 | 5112 | set = nft_set_lookup(table, attr, genmask); |
36dd1bcc | 5113 | } |
a8278400 | 5114 | |
36dd1bcc | 5115 | if (IS_ERR(set)) { |
f80a612d FFM |
5116 | if (PTR_ERR(set) == -ENOENT && |
5117 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET) | |
5118 | return 0; | |
5119 | ||
36dd1bcc PNA |
5120 | NL_SET_BAD_ATTR(extack, attr); |
5121 | return PTR_ERR(set); | |
5122 | } | |
273fe3f1 | 5123 | if (set->use || |
7dab8ee3 PNA |
5124 | (info->nlh->nlmsg_flags & NLM_F_NONREC && |
5125 | atomic_read(&set->nelems) > 0)) { | |
36dd1bcc | 5126 | NL_SET_BAD_ATTR(extack, attr); |
20a69341 | 5127 | return -EBUSY; |
36dd1bcc | 5128 | } |
20a69341 | 5129 | |
67086651 PNA |
5130 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5131 | ||
ee01d542 | 5132 | return nft_delset(&ctx, set); |
20a69341 PM |
5133 | } |
5134 | ||
345023b0 PNA |
5135 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
5136 | enum nft_registers reg, | |
5137 | const struct nft_data *data, | |
5138 | enum nft_data_types type, | |
5139 | unsigned int len); | |
5140 | ||
97c976d6 PNA |
5141 | static int nft_setelem_data_validate(const struct nft_ctx *ctx, |
5142 | struct nft_set *set, | |
5143 | struct nft_set_elem *elem) | |
20a69341 | 5144 | { |
fe2811eb | 5145 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
5146 | enum nft_registers dreg; |
5147 | ||
5148 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
5149 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
5150 | set->dtype == NFT_DATA_VERDICT ? | |
5151 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
5152 | set->dlen); | |
20a69341 PM |
5153 | } |
5154 | ||
97c976d6 PNA |
5155 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, |
5156 | struct nft_set *set, | |
5157 | const struct nft_set_iter *iter, | |
5158 | struct nft_set_elem *elem) | |
5159 | { | |
5160 | return nft_setelem_data_validate(ctx, set, elem); | |
5161 | } | |
5162 | ||
aaa31047 PNA |
5163 | static int nft_set_catchall_bind_check(const struct nft_ctx *ctx, |
5164 | struct nft_set *set) | |
5165 | { | |
5166 | u8 genmask = nft_genmask_next(ctx->net); | |
5167 | struct nft_set_elem_catchall *catchall; | |
5168 | struct nft_set_elem elem; | |
5169 | struct nft_set_ext *ext; | |
5170 | int ret = 0; | |
5171 | ||
5172 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5173 | ext = nft_set_elem_ext(set, catchall->elem); | |
5174 | if (!nft_set_elem_active(ext, genmask)) | |
5175 | continue; | |
5176 | ||
5177 | elem.priv = catchall->elem; | |
5178 | ret = nft_setelem_data_validate(ctx, set, &elem); | |
5179 | if (ret < 0) | |
5180 | break; | |
5181 | } | |
5182 | ||
5183 | return ret; | |
5184 | } | |
5185 | ||
20a69341 PM |
5186 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
5187 | struct nft_set_binding *binding) | |
5188 | { | |
5189 | struct nft_set_binding *i; | |
5190 | struct nft_set_iter iter; | |
5191 | ||
273fe3f1 PNA |
5192 | if (set->use == UINT_MAX) |
5193 | return -EOVERFLOW; | |
5194 | ||
408070d6 | 5195 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
5196 | return -EBUSY; |
5197 | ||
11113e19 | 5198 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
5199 | /* If the set is already bound to the same chain all |
5200 | * jumps are already validated for that chain. | |
5201 | */ | |
5202 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 5203 | if (i->flags & NFT_SET_MAP && |
11113e19 | 5204 | i->chain == binding->chain) |
20a69341 PM |
5205 | goto bind; |
5206 | } | |
5207 | ||
8588ac09 | 5208 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
5209 | iter.skip = 0; |
5210 | iter.count = 0; | |
5211 | iter.err = 0; | |
5212 | iter.fn = nf_tables_bind_check_setelem; | |
5213 | ||
5214 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
5215 | if (!iter.err) |
5216 | iter.err = nft_set_catchall_bind_check(ctx, set); | |
5217 | ||
a02f4248 | 5218 | if (iter.err < 0) |
20a69341 | 5219 | return iter.err; |
20a69341 PM |
5220 | } |
5221 | bind: | |
5222 | binding->chain = ctx->chain; | |
e688a7f8 | 5223 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 | 5224 | nft_set_trans_bind(ctx, set); |
273fe3f1 | 5225 | set->use++; |
f6ac8585 | 5226 | |
20a69341 PM |
5227 | return 0; |
5228 | } | |
63aea290 | 5229 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 5230 | |
3b0a081d FW |
5231 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
5232 | struct nft_set_binding *binding, bool event) | |
20a69341 | 5233 | { |
e688a7f8 | 5234 | list_del_rcu(&binding->list); |
20a69341 | 5235 | |
f6ac8585 | 5236 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 5237 | list_del_rcu(&set->list); |
f6ac8585 PNA |
5238 | if (event) |
5239 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
5240 | GFP_KERNEL); | |
5241 | } | |
20a69341 PM |
5242 | } |
5243 | ||
628bd3e4 PNA |
5244 | static void nft_setelem_data_activate(const struct net *net, |
5245 | const struct nft_set *set, | |
5246 | struct nft_set_elem *elem); | |
5247 | ||
5248 | static int nft_mapelem_activate(const struct nft_ctx *ctx, | |
5249 | struct nft_set *set, | |
5250 | const struct nft_set_iter *iter, | |
5251 | struct nft_set_elem *elem) | |
5252 | { | |
5253 | nft_setelem_data_activate(ctx->net, set, elem); | |
5254 | ||
5255 | return 0; | |
5256 | } | |
5257 | ||
5258 | static void nft_map_catchall_activate(const struct nft_ctx *ctx, | |
5259 | struct nft_set *set) | |
5260 | { | |
5261 | u8 genmask = nft_genmask_next(ctx->net); | |
5262 | struct nft_set_elem_catchall *catchall; | |
5263 | struct nft_set_elem elem; | |
5264 | struct nft_set_ext *ext; | |
5265 | ||
5266 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5267 | ext = nft_set_elem_ext(set, catchall->elem); | |
5268 | if (!nft_set_elem_active(ext, genmask)) | |
5269 | continue; | |
5270 | ||
5271 | elem.priv = catchall->elem; | |
5272 | nft_setelem_data_activate(ctx->net, set, &elem); | |
5273 | break; | |
5274 | } | |
5275 | } | |
5276 | ||
5277 | static void nft_map_activate(const struct nft_ctx *ctx, struct nft_set *set) | |
5278 | { | |
5279 | struct nft_set_iter iter = { | |
5280 | .genmask = nft_genmask_next(ctx->net), | |
5281 | .fn = nft_mapelem_activate, | |
5282 | }; | |
5283 | ||
5284 | set->ops->walk(ctx, set, &iter); | |
5285 | WARN_ON_ONCE(iter.err); | |
5286 | ||
5287 | nft_map_catchall_activate(ctx, set); | |
5288 | } | |
5289 | ||
c1592a89 PNA |
5290 | void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set) |
5291 | { | |
628bd3e4 PNA |
5292 | if (nft_set_is_anonymous(set)) { |
5293 | if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) | |
5294 | nft_map_activate(ctx, set); | |
5295 | ||
c1592a89 | 5296 | nft_clear(ctx->net, set); |
628bd3e4 | 5297 | } |
c1592a89 PNA |
5298 | |
5299 | set->use++; | |
5300 | } | |
5301 | EXPORT_SYMBOL_GPL(nf_tables_activate_set); | |
5302 | ||
273fe3f1 PNA |
5303 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
5304 | struct nft_set_binding *binding, | |
5305 | enum nft_trans_phase phase) | |
5306 | { | |
5307 | switch (phase) { | |
26b5a571 PNA |
5308 | case NFT_TRANS_PREPARE_ERROR: |
5309 | nft_set_trans_unbind(ctx, set); | |
5310 | if (nft_set_is_anonymous(set)) | |
5311 | nft_deactivate_next(ctx->net, set); | |
5312 | ||
5313 | set->use--; | |
5314 | break; | |
273fe3f1 | 5315 | case NFT_TRANS_PREPARE: |
628bd3e4 PNA |
5316 | if (nft_set_is_anonymous(set)) { |
5317 | if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) | |
5318 | nft_map_deactivate(ctx, set); | |
c1592a89 | 5319 | |
628bd3e4 PNA |
5320 | nft_deactivate_next(ctx->net, set); |
5321 | } | |
273fe3f1 PNA |
5322 | set->use--; |
5323 | return; | |
5324 | case NFT_TRANS_ABORT: | |
5325 | case NFT_TRANS_RELEASE: | |
628bd3e4 PNA |
5326 | if (nft_set_is_anonymous(set) && |
5327 | set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) | |
5328 | nft_map_deactivate(ctx, set); | |
5329 | ||
273fe3f1 | 5330 | set->use--; |
954d8297 | 5331 | fallthrough; |
273fe3f1 PNA |
5332 | default: |
5333 | nf_tables_unbind_set(ctx, set, binding, | |
5334 | phase == NFT_TRANS_COMMIT); | |
5335 | } | |
5336 | } | |
5337 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
5338 | ||
cd5125d8 FW |
5339 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
5340 | { | |
f6ac8585 | 5341 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
0c2a85ed | 5342 | nft_set_destroy(ctx, set); |
cd5125d8 FW |
5343 | } |
5344 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
5345 | ||
3ac4c07a PM |
5346 | const struct nft_set_ext_type nft_set_ext_types[] = { |
5347 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 5348 | .align = __alignof__(u32), |
3ac4c07a PM |
5349 | }, |
5350 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 5351 | .align = __alignof__(u32), |
3ac4c07a | 5352 | }, |
563125a7 PNA |
5353 | [NFT_SET_EXT_EXPRESSIONS] = { |
5354 | .align = __alignof__(struct nft_set_elem_expr), | |
f25ad2e9 | 5355 | }, |
8aeff920 PNA |
5356 | [NFT_SET_EXT_OBJREF] = { |
5357 | .len = sizeof(struct nft_object *), | |
5358 | .align = __alignof__(struct nft_object *), | |
5359 | }, | |
3ac4c07a PM |
5360 | [NFT_SET_EXT_FLAGS] = { |
5361 | .len = sizeof(u8), | |
5362 | .align = __alignof__(u8), | |
5363 | }, | |
c3e1b005 PM |
5364 | [NFT_SET_EXT_TIMEOUT] = { |
5365 | .len = sizeof(u64), | |
5366 | .align = __alignof__(u64), | |
5367 | }, | |
5368 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
5369 | .len = sizeof(u64), |
5370 | .align = __alignof__(u64), | |
c3e1b005 | 5371 | }, |
68e942e8 PM |
5372 | [NFT_SET_EXT_USERDATA] = { |
5373 | .len = sizeof(struct nft_userdata), | |
5374 | .align = __alignof__(struct nft_userdata), | |
5375 | }, | |
7b225d0b PNA |
5376 | [NFT_SET_EXT_KEY_END] = { |
5377 | .align = __alignof__(u32), | |
5378 | }, | |
3ac4c07a | 5379 | }; |
3ac4c07a | 5380 | |
20a69341 PM |
5381 | /* |
5382 | * Set elements | |
5383 | */ | |
5384 | ||
5385 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
5386 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
5387 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
5388 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 5389 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 5390 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
5391 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
5392 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 5393 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
5394 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
5395 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 5396 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
48b0ae04 | 5397 | [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED }, |
20a69341 PM |
5398 | }; |
5399 | ||
5400 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
5401 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
5402 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
5403 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
5404 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 5405 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 5406 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
5407 | }; |
5408 | ||
563125a7 PNA |
5409 | static int nft_set_elem_expr_dump(struct sk_buff *skb, |
5410 | const struct nft_set *set, | |
5411 | const struct nft_set_ext *ext) | |
5412 | { | |
5413 | struct nft_set_elem_expr *elem_expr; | |
5414 | u32 size, num_exprs = 0; | |
5415 | struct nft_expr *expr; | |
48b0ae04 | 5416 | struct nlattr *nest; |
563125a7 PNA |
5417 | |
5418 | elem_expr = nft_set_ext_expr(ext); | |
5419 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
5420 | num_exprs++; | |
5421 | ||
5422 | if (num_exprs == 1) { | |
5423 | expr = nft_setelem_expr_at(elem_expr, 0); | |
8daa8fde | 5424 | if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr, false) < 0) |
563125a7 PNA |
5425 | return -1; |
5426 | ||
5427 | return 0; | |
48b0ae04 PNA |
5428 | } else if (num_exprs > 1) { |
5429 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS); | |
5430 | if (nest == NULL) | |
5431 | goto nla_put_failure; | |
563125a7 | 5432 | |
48b0ae04 PNA |
5433 | nft_setelem_expr_foreach(expr, elem_expr, size) { |
5434 | expr = nft_setelem_expr_at(elem_expr, size); | |
8daa8fde | 5435 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, false) < 0) |
48b0ae04 PNA |
5436 | goto nla_put_failure; |
5437 | } | |
5438 | nla_nest_end(skb, nest); | |
5439 | } | |
563125a7 | 5440 | return 0; |
48b0ae04 PNA |
5441 | |
5442 | nla_put_failure: | |
5443 | return -1; | |
563125a7 PNA |
5444 | } |
5445 | ||
20a69341 PM |
5446 | static int nf_tables_fill_setelem(struct sk_buff *skb, |
5447 | const struct nft_set *set, | |
5448 | const struct nft_set_elem *elem) | |
5449 | { | |
fe2811eb | 5450 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
5451 | unsigned char *b = skb_tail_pointer(skb); |
5452 | struct nlattr *nest; | |
5453 | ||
ae0be8de | 5454 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
5455 | if (nest == NULL) |
5456 | goto nla_put_failure; | |
5457 | ||
aaa31047 PNA |
5458 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
5459 | nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), | |
fe2811eb | 5460 | NFT_DATA_VALUE, set->klen) < 0) |
20a69341 PM |
5461 | goto nla_put_failure; |
5462 | ||
7b225d0b PNA |
5463 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
5464 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
5465 | NFT_DATA_VALUE, set->klen) < 0) | |
5466 | goto nla_put_failure; | |
5467 | ||
fe2811eb PM |
5468 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
5469 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
5470 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
5471 | set->dlen) < 0) | |
5472 | goto nla_put_failure; | |
5473 | ||
563125a7 PNA |
5474 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) && |
5475 | nft_set_elem_expr_dump(skb, set, ext)) | |
f25ad2e9 PM |
5476 | goto nla_put_failure; |
5477 | ||
8aeff920 PNA |
5478 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
5479 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 5480 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
5481 | goto nla_put_failure; |
5482 | ||
fe2811eb PM |
5483 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
5484 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
5485 | htonl(*nft_set_ext_flags(ext)))) | |
5486 | goto nla_put_failure; | |
20a69341 | 5487 | |
c3e1b005 PM |
5488 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
5489 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 5490 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 5491 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
5492 | goto nla_put_failure; |
5493 | ||
5494 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 5495 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
5496 | |
5497 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 5498 | if (time_before64(now, expires)) |
c3e1b005 PM |
5499 | expires -= now; |
5500 | else | |
5501 | expires = 0; | |
5502 | ||
5503 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 5504 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 5505 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
5506 | goto nla_put_failure; |
5507 | } | |
5508 | ||
68e942e8 PM |
5509 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
5510 | struct nft_userdata *udata; | |
5511 | ||
5512 | udata = nft_set_ext_userdata(ext); | |
5513 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
5514 | udata->len + 1, udata->data)) | |
5515 | goto nla_put_failure; | |
5516 | } | |
5517 | ||
20a69341 PM |
5518 | nla_nest_end(skb, nest); |
5519 | return 0; | |
5520 | ||
5521 | nla_put_failure: | |
5522 | nlmsg_trim(skb, b); | |
5523 | return -EMSGSIZE; | |
5524 | } | |
5525 | ||
5526 | struct nft_set_dump_args { | |
5527 | const struct netlink_callback *cb; | |
5528 | struct nft_set_iter iter; | |
5529 | struct sk_buff *skb; | |
5530 | }; | |
5531 | ||
5532 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 5533 | struct nft_set *set, |
20a69341 | 5534 | const struct nft_set_iter *iter, |
de70185d | 5535 | struct nft_set_elem *elem) |
20a69341 PM |
5536 | { |
5537 | struct nft_set_dump_args *args; | |
5538 | ||
5539 | args = container_of(iter, struct nft_set_dump_args, iter); | |
5540 | return nf_tables_fill_setelem(args->skb, set, elem); | |
5541 | } | |
5542 | ||
fa803605 LZ |
5543 | struct nft_set_dump_ctx { |
5544 | const struct nft_set *set; | |
5545 | struct nft_ctx ctx; | |
5546 | }; | |
5547 | ||
aaa31047 PNA |
5548 | static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb, |
5549 | const struct nft_set *set) | |
5550 | { | |
5551 | struct nft_set_elem_catchall *catchall; | |
5552 | u8 genmask = nft_genmask_cur(net); | |
5553 | struct nft_set_elem elem; | |
5554 | struct nft_set_ext *ext; | |
5555 | int ret = 0; | |
5556 | ||
5557 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5558 | ext = nft_set_elem_ext(set, catchall->elem); | |
5559 | if (!nft_set_elem_active(ext, genmask) || | |
5560 | nft_set_elem_expired(ext)) | |
5561 | continue; | |
5562 | ||
5563 | elem.priv = catchall->elem; | |
5564 | ret = nf_tables_fill_setelem(skb, set, &elem); | |
5565 | break; | |
5566 | } | |
5567 | ||
5568 | return ret; | |
5569 | } | |
5570 | ||
20a69341 PM |
5571 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
5572 | { | |
fa803605 | 5573 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 5574 | struct net *net = sock_net(skb->sk); |
0854db2a | 5575 | struct nftables_pernet *nft_net; |
fa803605 | 5576 | struct nft_table *table; |
de70185d | 5577 | struct nft_set *set; |
20a69341 | 5578 | struct nft_set_dump_args args; |
fa803605 | 5579 | bool set_found = false; |
20a69341 PM |
5580 | struct nlmsghdr *nlh; |
5581 | struct nlattr *nest; | |
5582 | u32 portid, seq; | |
fa803605 | 5583 | int event; |
20a69341 | 5584 | |
fa803605 | 5585 | rcu_read_lock(); |
d59d2f82 | 5586 | nft_net = nft_pernet(net); |
34002783 PNA |
5587 | cb->seq = READ_ONCE(nft_net->base_seq); |
5588 | ||
0854db2a | 5589 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 5590 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && |
98319cb9 | 5591 | dump_ctx->ctx.family != table->family) |
fa803605 | 5592 | continue; |
20a69341 | 5593 | |
36596dad PNA |
5594 | if (table != dump_ctx->ctx.table) |
5595 | continue; | |
20a69341 | 5596 | |
36596dad PNA |
5597 | list_for_each_entry_rcu(set, &table->sets, list) { |
5598 | if (set == dump_ctx->set) { | |
5599 | set_found = true; | |
5600 | break; | |
fa803605 | 5601 | } |
fa803605 LZ |
5602 | } |
5603 | break; | |
5604 | } | |
5605 | ||
5606 | if (!set_found) { | |
5607 | rcu_read_unlock(); | |
5608 | return -ENOENT; | |
5609 | } | |
20a69341 | 5610 | |
dedb67c4 | 5611 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
5612 | portid = NETLINK_CB(cb->skb).portid; |
5613 | seq = cb->nlh->nlmsg_seq; | |
5614 | ||
19c28b13 PNA |
5615 | nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI, |
5616 | table->family, NFNETLINK_V0, nft_base_seq(net)); | |
5617 | if (!nlh) | |
20a69341 PM |
5618 | goto nla_put_failure; |
5619 | ||
fa803605 | 5620 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
5621 | goto nla_put_failure; |
5622 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
5623 | goto nla_put_failure; | |
5624 | ||
ae0be8de | 5625 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
5626 | if (nest == NULL) |
5627 | goto nla_put_failure; | |
5628 | ||
8588ac09 PNA |
5629 | args.cb = cb; |
5630 | args.skb = skb; | |
fa803605 | 5631 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
5632 | args.iter.skip = cb->args[0]; |
5633 | args.iter.count = 0; | |
5634 | args.iter.err = 0; | |
5635 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 | 5636 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
aaa31047 PNA |
5637 | |
5638 | if (!args.iter.err && args.iter.count == cb->args[0]) | |
5639 | args.iter.err = nft_set_catchall_dump(net, skb, set); | |
fa803605 | 5640 | rcu_read_unlock(); |
20a69341 PM |
5641 | |
5642 | nla_nest_end(skb, nest); | |
5643 | nlmsg_end(skb, nlh); | |
5644 | ||
5645 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
5646 | return args.iter.err; | |
5647 | if (args.iter.count == cb->args[0]) | |
5648 | return 0; | |
5649 | ||
5650 | cb->args[0] = args.iter.count; | |
5651 | return skb->len; | |
5652 | ||
5653 | nla_put_failure: | |
fa803605 | 5654 | rcu_read_unlock(); |
20a69341 PM |
5655 | return -ENOSPC; |
5656 | } | |
5657 | ||
90fd131a FW |
5658 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
5659 | { | |
5660 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
5661 | ||
5662 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
5663 | ||
5664 | return cb->data ? 0 : -ENOMEM; | |
5665 | } | |
5666 | ||
fa803605 LZ |
5667 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
5668 | { | |
5669 | kfree(cb->data); | |
5670 | return 0; | |
5671 | } | |
5672 | ||
d60ce62f AB |
5673 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
5674 | const struct nft_ctx *ctx, u32 seq, | |
5675 | u32 portid, int event, u16 flags, | |
5676 | const struct nft_set *set, | |
5677 | const struct nft_set_elem *elem) | |
5678 | { | |
d60ce62f AB |
5679 | struct nlmsghdr *nlh; |
5680 | struct nlattr *nest; | |
5681 | int err; | |
5682 | ||
dedb67c4 | 5683 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
5684 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
5685 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
5686 | if (!nlh) | |
d60ce62f AB |
5687 | goto nla_put_failure; |
5688 | ||
d60ce62f AB |
5689 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
5690 | goto nla_put_failure; | |
5691 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
5692 | goto nla_put_failure; | |
5693 | ||
ae0be8de | 5694 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
5695 | if (nest == NULL) |
5696 | goto nla_put_failure; | |
5697 | ||
5698 | err = nf_tables_fill_setelem(skb, set, elem); | |
5699 | if (err < 0) | |
5700 | goto nla_put_failure; | |
5701 | ||
5702 | nla_nest_end(skb, nest); | |
5703 | ||
053c095a JB |
5704 | nlmsg_end(skb, nlh); |
5705 | return 0; | |
d60ce62f AB |
5706 | |
5707 | nla_put_failure: | |
5708 | nlmsg_trim(skb, nlh); | |
5709 | return -1; | |
5710 | } | |
5711 | ||
ba0e4d99 PNA |
5712 | static int nft_setelem_parse_flags(const struct nft_set *set, |
5713 | const struct nlattr *attr, u32 *flags) | |
5714 | { | |
5715 | if (attr == NULL) | |
5716 | return 0; | |
5717 | ||
5718 | *flags = ntohl(nla_get_be32(attr)); | |
aaa31047 PNA |
5719 | if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) |
5720 | return -EOPNOTSUPP; | |
ba0e4d99 PNA |
5721 | if (!(set->flags & NFT_SET_INTERVAL) && |
5722 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
5723 | return -EINVAL; | |
fc0ae524 PNA |
5724 | if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) == |
5725 | (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) | |
5726 | return -EINVAL; | |
ba0e4d99 PNA |
5727 | |
5728 | return 0; | |
5729 | } | |
5730 | ||
20a1452c PNA |
5731 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
5732 | struct nft_data *key, struct nlattr *attr) | |
5733 | { | |
341b6941 PNA |
5734 | struct nft_data_desc desc = { |
5735 | .type = NFT_DATA_VALUE, | |
5736 | .size = NFT_DATA_VALUE_MAXLEN, | |
5737 | .len = set->klen, | |
5738 | }; | |
20a1452c | 5739 | |
341b6941 | 5740 | return nft_data_init(ctx, key, &desc, attr); |
20a1452c PNA |
5741 | } |
5742 | ||
fdb9c405 PNA |
5743 | static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, |
5744 | struct nft_data_desc *desc, | |
5745 | struct nft_data *data, | |
5746 | struct nlattr *attr) | |
5747 | { | |
7e6bc1f6 | 5748 | u32 dtype; |
fdb9c405 | 5749 | |
7e6bc1f6 PNA |
5750 | if (set->dtype == NFT_DATA_VERDICT) |
5751 | dtype = NFT_DATA_VERDICT; | |
5752 | else | |
5753 | dtype = NFT_DATA_VALUE; | |
5754 | ||
341b6941 PNA |
5755 | desc->type = dtype; |
5756 | desc->size = NFT_DATA_VALUE_MAXLEN; | |
5757 | desc->len = set->dlen; | |
f323ef3a | 5758 | desc->flags = NFT_DATA_DESC_SETELEM; |
fdb9c405 | 5759 | |
341b6941 | 5760 | return nft_data_init(ctx, data, desc, attr); |
fdb9c405 PNA |
5761 | } |
5762 | ||
aaa31047 PNA |
5763 | static void *nft_setelem_catchall_get(const struct net *net, |
5764 | const struct nft_set *set) | |
5765 | { | |
5766 | struct nft_set_elem_catchall *catchall; | |
5767 | u8 genmask = nft_genmask_cur(net); | |
5768 | struct nft_set_ext *ext; | |
5769 | void *priv = NULL; | |
5770 | ||
5771 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5772 | ext = nft_set_elem_ext(set, catchall->elem); | |
5773 | if (!nft_set_elem_active(ext, genmask) || | |
5774 | nft_set_elem_expired(ext)) | |
5775 | continue; | |
5776 | ||
5777 | priv = catchall->elem; | |
5778 | break; | |
5779 | } | |
5780 | ||
5781 | return priv; | |
5782 | } | |
5783 | ||
5784 | static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set, | |
5785 | struct nft_set_elem *elem, u32 flags) | |
5786 | { | |
5787 | void *priv; | |
5788 | ||
5789 | if (!(flags & NFT_SET_ELEM_CATCHALL)) { | |
5790 | priv = set->ops->get(ctx->net, set, elem, flags); | |
5791 | if (IS_ERR(priv)) | |
5792 | return PTR_ERR(priv); | |
5793 | } else { | |
5794 | priv = nft_setelem_catchall_get(ctx->net, set); | |
5795 | if (!priv) | |
5796 | return -ENOENT; | |
5797 | } | |
5798 | elem->priv = priv; | |
5799 | ||
5800 | return 0; | |
5801 | } | |
5802 | ||
ba0e4d99 PNA |
5803 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
5804 | const struct nlattr *attr) | |
5805 | { | |
5806 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
5807 | struct nft_set_elem elem; |
5808 | struct sk_buff *skb; | |
5809 | uint32_t flags = 0; | |
ba0e4d99 PNA |
5810 | int err; |
5811 | ||
8cb08174 JB |
5812 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5813 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
5814 | if (err < 0) |
5815 | return err; | |
5816 | ||
ba0e4d99 PNA |
5817 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5818 | if (err < 0) | |
5819 | return err; | |
5820 | ||
aaa31047 PNA |
5821 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
5822 | return -EINVAL; | |
5823 | ||
5824 | if (nla[NFTA_SET_ELEM_KEY]) { | |
5825 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
5826 | nla[NFTA_SET_ELEM_KEY]); | |
5827 | if (err < 0) | |
5828 | return err; | |
5829 | } | |
ba0e4d99 | 5830 | |
7b225d0b PNA |
5831 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5832 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5833 | nla[NFTA_SET_ELEM_KEY_END]); | |
5834 | if (err < 0) | |
5835 | return err; | |
5836 | } | |
5837 | ||
aaa31047 PNA |
5838 | err = nft_setelem_get(ctx, set, &elem, flags); |
5839 | if (err < 0) | |
5840 | return err; | |
ba0e4d99 PNA |
5841 | |
5842 | err = -ENOMEM; | |
d9adf22a | 5843 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 | 5844 | if (skb == NULL) |
ee921183 | 5845 | return err; |
ba0e4d99 PNA |
5846 | |
5847 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
5848 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
5849 | if (err < 0) | |
ee921183 | 5850 | goto err_fill_setelem; |
ba0e4d99 | 5851 | |
ee921183 | 5852 | return nfnetlink_unicast(skb, ctx->net, ctx->portid); |
ba0e4d99 | 5853 | |
ee921183 | 5854 | err_fill_setelem: |
ba0e4d99 | 5855 | kfree_skb(skb); |
ee921183 | 5856 | return err; |
ba0e4d99 PNA |
5857 | } |
5858 | ||
d9adf22a | 5859 | /* called with rcu_read_lock held */ |
797d4980 PNA |
5860 | static int nf_tables_getsetelem(struct sk_buff *skb, |
5861 | const struct nfnl_info *info, | |
5862 | const struct nlattr * const nla[]) | |
ba0e4d99 | 5863 | { |
797d4980 PNA |
5864 | struct netlink_ext_ack *extack = info->extack; |
5865 | u8 genmask = nft_genmask_cur(info->net); | |
e2b750d7 | 5866 | u8 family = info->nfmsg->nfgen_family; |
797d4980 | 5867 | struct net *net = info->net; |
e2b750d7 | 5868 | struct nft_table *table; |
ba0e4d99 PNA |
5869 | struct nft_set *set; |
5870 | struct nlattr *attr; | |
5871 | struct nft_ctx ctx; | |
5872 | int rem, err = 0; | |
5873 | ||
e2b750d7 | 5874 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
92f3e96d | 5875 | genmask, 0); |
e2b750d7 PNA |
5876 | if (IS_ERR(table)) { |
5877 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
5878 | return PTR_ERR(table); | |
5879 | } | |
ba0e4d99 | 5880 | |
e2b750d7 | 5881 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
5882 | if (IS_ERR(set)) |
5883 | return PTR_ERR(set); | |
5884 | ||
e2b750d7 PNA |
5885 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5886 | ||
797d4980 | 5887 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
ba0e4d99 | 5888 | struct netlink_dump_control c = { |
90fd131a | 5889 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
5890 | .dump = nf_tables_dump_set, |
5891 | .done = nf_tables_dump_set_done, | |
d9adf22a | 5892 | .module = THIS_MODULE, |
ba0e4d99 | 5893 | }; |
90fd131a FW |
5894 | struct nft_set_dump_ctx dump_ctx = { |
5895 | .set = set, | |
5896 | .ctx = ctx, | |
5897 | }; | |
ba0e4d99 | 5898 | |
90fd131a | 5899 | c.data = &dump_ctx; |
797d4980 | 5900 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
ba0e4d99 PNA |
5901 | } |
5902 | ||
5903 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
5904 | return -EINVAL; | |
5905 | ||
5906 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
5907 | err = nft_get_set_elem(&ctx, set, attr); | |
b53c1166 PNA |
5908 | if (err < 0) { |
5909 | NL_SET_BAD_ATTR(extack, attr); | |
ba0e4d99 | 5910 | break; |
b53c1166 | 5911 | } |
ba0e4d99 PNA |
5912 | } |
5913 | ||
5914 | return err; | |
5915 | } | |
5916 | ||
25e94a99 PNA |
5917 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
5918 | const struct nft_set *set, | |
5919 | const struct nft_set_elem *elem, | |
6fb721cf | 5920 | int event) |
d60ce62f | 5921 | { |
0854db2a | 5922 | struct nftables_pernet *nft_net; |
128ad332 PNA |
5923 | struct net *net = ctx->net; |
5924 | u32 portid = ctx->portid; | |
d60ce62f | 5925 | struct sk_buff *skb; |
6fb721cf | 5926 | u16 flags = 0; |
d60ce62f AB |
5927 | int err; |
5928 | ||
128ad332 | 5929 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 5930 | return; |
d60ce62f | 5931 | |
d60ce62f AB |
5932 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
5933 | if (skb == NULL) | |
5934 | goto err; | |
5935 | ||
6fb721cf PNA |
5936 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
5937 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
5938 | ||
d60ce62f AB |
5939 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, |
5940 | set, elem); | |
5941 | if (err < 0) { | |
5942 | kfree_skb(skb); | |
5943 | goto err; | |
5944 | } | |
5945 | ||
d59d2f82 | 5946 | nft_net = nft_pernet(net); |
0854db2a | 5947 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 5948 | return; |
d60ce62f | 5949 | err: |
25e94a99 | 5950 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
5951 | } |
5952 | ||
60319eb1 PNA |
5953 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
5954 | int msg_type, | |
5955 | struct nft_set *set) | |
5956 | { | |
5957 | struct nft_trans *trans; | |
5958 | ||
5959 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
5960 | if (trans == NULL) | |
5961 | return NULL; | |
5962 | ||
5963 | nft_trans_elem_set(trans) = set; | |
5964 | return trans; | |
5965 | } | |
5966 | ||
a7fc9368 PNA |
5967 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
5968 | const struct nft_set *set, | |
5969 | const struct nlattr *attr) | |
5970 | { | |
5971 | struct nft_expr *expr; | |
5972 | int err; | |
5973 | ||
5974 | expr = nft_expr_init(ctx, attr); | |
5975 | if (IS_ERR(expr)) | |
5976 | return expr; | |
5977 | ||
5978 | err = -EOPNOTSUPP; | |
a7fc9368 PNA |
5979 | if (expr->ops->type->flags & NFT_EXPR_GC) { |
5980 | if (set->flags & NFT_SET_TIMEOUT) | |
5981 | goto err_set_elem_expr; | |
5982 | if (!set->ops->gc_init) | |
5983 | goto err_set_elem_expr; | |
5984 | set->ops->gc_init(set); | |
5985 | } | |
5986 | ||
5987 | return expr; | |
5988 | ||
5989 | err_set_elem_expr: | |
5990 | nft_expr_destroy(ctx, expr); | |
5991 | return ERR_PTR(err); | |
5992 | } | |
5993 | ||
34aae2c2 PNA |
5994 | static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len) |
5995 | { | |
5996 | len += nft_set_ext_types[id].len; | |
5997 | if (len > tmpl->ext_len[id] || | |
5998 | len > U8_MAX) | |
5999 | return -1; | |
6000 | ||
6001 | return 0; | |
6002 | } | |
6003 | ||
6004 | static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id, | |
6005 | void *to, const void *from, u32 len) | |
6006 | { | |
6007 | if (nft_set_ext_check(tmpl, id, len) < 0) | |
6008 | return -1; | |
6009 | ||
6010 | memcpy(to, from, len); | |
6011 | ||
6012 | return 0; | |
6013 | } | |
6014 | ||
22fe54d5 PM |
6015 | void *nft_set_elem_init(const struct nft_set *set, |
6016 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
6017 | const u32 *key, const u32 *key_end, |
6018 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
6019 | { |
6020 | struct nft_set_ext *ext; | |
6021 | void *elem; | |
6022 | ||
6023 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
6024 | if (elem == NULL) | |
34aae2c2 | 6025 | return ERR_PTR(-ENOMEM); |
fe2811eb PM |
6026 | |
6027 | ext = nft_set_elem_ext(set, elem); | |
6028 | nft_set_ext_init(ext, tmpl); | |
6029 | ||
34aae2c2 PNA |
6030 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
6031 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY, | |
6032 | nft_set_ext_key(ext), key, set->klen) < 0) | |
6033 | goto err_ext_check; | |
6034 | ||
6035 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && | |
6036 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END, | |
6037 | nft_set_ext_key_end(ext), key_end, set->klen) < 0) | |
6038 | goto err_ext_check; | |
6039 | ||
6040 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && | |
6041 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA, | |
6042 | nft_set_ext_data(ext), data, set->dlen) < 0) | |
6043 | goto err_ext_check; | |
6044 | ||
79ebb5bb LGL |
6045 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
6046 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
6047 | if (expiration == 0) | |
6048 | *nft_set_ext_expiration(ext) += timeout; | |
6049 | } | |
c3e1b005 PM |
6050 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
6051 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
6052 | |
6053 | return elem; | |
34aae2c2 PNA |
6054 | |
6055 | err_ext_check: | |
6056 | kfree(elem); | |
6057 | ||
6058 | return ERR_PTR(-EINVAL); | |
fe2811eb PM |
6059 | } |
6060 | ||
563125a7 PNA |
6061 | static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
6062 | struct nft_expr *expr) | |
475beb9c PNA |
6063 | { |
6064 | if (expr->ops->destroy_clone) { | |
6065 | expr->ops->destroy_clone(ctx, expr); | |
6066 | module_put(expr->ops->type->owner); | |
6067 | } else { | |
6068 | nf_tables_expr_destroy(ctx, expr); | |
6069 | } | |
6070 | } | |
6071 | ||
563125a7 PNA |
6072 | static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
6073 | struct nft_set_elem_expr *elem_expr) | |
6074 | { | |
6075 | struct nft_expr *expr; | |
6076 | u32 size; | |
6077 | ||
6078 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
6079 | __nft_set_elem_expr_destroy(ctx, expr); | |
6080 | } | |
6081 | ||
628bd3e4 | 6082 | /* Drop references and destroy. Called from gc, dynset and abort path. */ |
61f9e292 LZ |
6083 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
6084 | bool destroy_expr) | |
61edafbb PM |
6085 | { |
6086 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
6087 | struct nft_ctx ctx = { |
6088 | .net = read_pnet(&set->net), | |
6089 | .family = set->table->family, | |
6090 | }; | |
61edafbb | 6091 | |
59105446 | 6092 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 6093 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 6094 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
563125a7 | 6095 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c | 6096 | nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext)); |
371ebcbb | 6097 | |
8aeff920 PNA |
6098 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
6099 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
6100 | kfree(elem); |
6101 | } | |
6102 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
6103 | ||
628bd3e4 PNA |
6104 | /* Destroy element. References have been already dropped in the preparation |
6105 | * path via nft_setelem_data_deactivate(). | |
59105446 | 6106 | */ |
628bd3e4 PNA |
6107 | void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
6108 | const struct nft_set *set, void *elem) | |
59105446 PNA |
6109 | { |
6110 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
6111 | ||
563125a7 | 6112 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c PNA |
6113 | nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext)); |
6114 | ||
59105446 PNA |
6115 | kfree(elem); |
6116 | } | |
6117 | ||
fca05d4d PNA |
6118 | int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
6119 | struct nft_expr *expr_array[]) | |
8cfd9b0f PNA |
6120 | { |
6121 | struct nft_expr *expr; | |
6122 | int err, i, k; | |
6123 | ||
6124 | for (i = 0; i < set->num_exprs; i++) { | |
42193ffd | 6125 | expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT); |
8cfd9b0f PNA |
6126 | if (!expr) |
6127 | goto err_expr; | |
6128 | ||
6129 | err = nft_expr_clone(expr, set->exprs[i]); | |
6130 | if (err < 0) { | |
c485c35f | 6131 | kfree(expr); |
8cfd9b0f PNA |
6132 | goto err_expr; |
6133 | } | |
6134 | expr_array[i] = expr; | |
6135 | } | |
6136 | ||
6137 | return 0; | |
6138 | ||
6139 | err_expr: | |
161b838e CIK |
6140 | for (k = i - 1; k >= 0; k--) |
6141 | nft_expr_destroy(ctx, expr_array[k]); | |
8cfd9b0f PNA |
6142 | |
6143 | return -ENOMEM; | |
6144 | } | |
6145 | ||
4d8f9065 | 6146 | static int nft_set_elem_expr_setup(struct nft_ctx *ctx, |
34aae2c2 | 6147 | const struct nft_set_ext_tmpl *tmpl, |
4d8f9065 PNA |
6148 | const struct nft_set_ext *ext, |
6149 | struct nft_expr *expr_array[], | |
6150 | u32 num_exprs) | |
563125a7 PNA |
6151 | { |
6152 | struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext); | |
34aae2c2 | 6153 | u32 len = sizeof(struct nft_set_elem_expr); |
4d8f9065 PNA |
6154 | struct nft_expr *expr; |
6155 | int i, err; | |
6156 | ||
34aae2c2 PNA |
6157 | if (num_exprs == 0) |
6158 | return 0; | |
6159 | ||
6160 | for (i = 0; i < num_exprs; i++) | |
6161 | len += expr_array[i]->ops->size; | |
6162 | ||
6163 | if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0) | |
6164 | return -EINVAL; | |
6165 | ||
4d8f9065 PNA |
6166 | for (i = 0; i < num_exprs; i++) { |
6167 | expr = nft_setelem_expr_at(elem_expr, elem_expr->size); | |
6168 | err = nft_expr_clone(expr, expr_array[i]); | |
6169 | if (err < 0) | |
6170 | goto err_elem_expr_setup; | |
6171 | ||
6172 | elem_expr->size += expr_array[i]->ops->size; | |
6173 | nft_expr_destroy(ctx, expr_array[i]); | |
6174 | expr_array[i] = NULL; | |
6175 | } | |
6176 | ||
6177 | return 0; | |
6178 | ||
6179 | err_elem_expr_setup: | |
6180 | for (; i < num_exprs; i++) { | |
6181 | nft_expr_destroy(ctx, expr_array[i]); | |
6182 | expr_array[i] = NULL; | |
6183 | } | |
563125a7 | 6184 | |
4d8f9065 | 6185 | return -ENOMEM; |
563125a7 PNA |
6186 | } |
6187 | ||
aaa31047 PNA |
6188 | struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
6189 | const struct nft_set *set) | |
6190 | { | |
6191 | struct nft_set_elem_catchall *catchall; | |
6192 | u8 genmask = nft_genmask_cur(net); | |
6193 | struct nft_set_ext *ext; | |
6194 | ||
6195 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
6196 | ext = nft_set_elem_ext(set, catchall->elem); | |
6197 | if (nft_set_elem_active(ext, genmask) && | |
6198 | !nft_set_elem_expired(ext)) | |
6199 | return ext; | |
6200 | } | |
6201 | ||
6202 | return NULL; | |
6203 | } | |
6204 | EXPORT_SYMBOL_GPL(nft_set_catchall_lookup); | |
6205 | ||
6206 | void *nft_set_catchall_gc(const struct nft_set *set) | |
6207 | { | |
6208 | struct nft_set_elem_catchall *catchall, *next; | |
6209 | struct nft_set_ext *ext; | |
6210 | void *elem = NULL; | |
6211 | ||
6212 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
6213 | ext = nft_set_elem_ext(set, catchall->elem); | |
6214 | ||
6215 | if (!nft_set_elem_expired(ext) || | |
6216 | nft_set_elem_mark_busy(ext)) | |
6217 | continue; | |
6218 | ||
6219 | elem = catchall->elem; | |
6220 | list_del_rcu(&catchall->list); | |
6221 | kfree_rcu(catchall, rcu); | |
6222 | break; | |
6223 | } | |
6224 | ||
6225 | return elem; | |
6226 | } | |
6227 | EXPORT_SYMBOL_GPL(nft_set_catchall_gc); | |
6228 | ||
6229 | static int nft_setelem_catchall_insert(const struct net *net, | |
6230 | struct nft_set *set, | |
6231 | const struct nft_set_elem *elem, | |
6232 | struct nft_set_ext **pext) | |
6233 | { | |
6234 | struct nft_set_elem_catchall *catchall; | |
6235 | u8 genmask = nft_genmask_next(net); | |
6236 | struct nft_set_ext *ext; | |
6237 | ||
6238 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
6239 | ext = nft_set_elem_ext(set, catchall->elem); | |
6240 | if (nft_set_elem_active(ext, genmask)) { | |
6241 | *pext = ext; | |
6242 | return -EEXIST; | |
6243 | } | |
6244 | } | |
6245 | ||
6246 | catchall = kmalloc(sizeof(*catchall), GFP_KERNEL); | |
6247 | if (!catchall) | |
6248 | return -ENOMEM; | |
6249 | ||
6250 | catchall->elem = elem->priv; | |
6251 | list_add_tail_rcu(&catchall->list, &set->catchall_list); | |
6252 | ||
6253 | return 0; | |
6254 | } | |
6255 | ||
6256 | static int nft_setelem_insert(const struct net *net, | |
6257 | struct nft_set *set, | |
6258 | const struct nft_set_elem *elem, | |
6259 | struct nft_set_ext **ext, unsigned int flags) | |
6260 | { | |
6261 | int ret; | |
6262 | ||
6263 | if (flags & NFT_SET_ELEM_CATCHALL) | |
6264 | ret = nft_setelem_catchall_insert(net, set, elem, ext); | |
6265 | else | |
6266 | ret = set->ops->insert(net, set, elem, ext); | |
6267 | ||
6268 | return ret; | |
6269 | } | |
6270 | ||
6271 | static bool nft_setelem_is_catchall(const struct nft_set *set, | |
6272 | const struct nft_set_elem *elem) | |
6273 | { | |
6274 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6275 | ||
6276 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
6277 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL) | |
6278 | return true; | |
6279 | ||
6280 | return false; | |
6281 | } | |
6282 | ||
6283 | static void nft_setelem_activate(struct net *net, struct nft_set *set, | |
6284 | struct nft_set_elem *elem) | |
6285 | { | |
6286 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6287 | ||
6288 | if (nft_setelem_is_catchall(set, elem)) { | |
6289 | nft_set_elem_change_active(net, set, ext); | |
6290 | nft_set_elem_clear_busy(ext); | |
6291 | } else { | |
6292 | set->ops->activate(net, set, elem); | |
6293 | } | |
6294 | } | |
6295 | ||
6296 | static int nft_setelem_catchall_deactivate(const struct net *net, | |
6297 | struct nft_set *set, | |
6298 | struct nft_set_elem *elem) | |
6299 | { | |
6300 | struct nft_set_elem_catchall *catchall; | |
6301 | struct nft_set_ext *ext; | |
6302 | ||
6303 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
6304 | ext = nft_set_elem_ext(set, catchall->elem); | |
6305 | if (!nft_is_active(net, ext) || | |
6306 | nft_set_elem_mark_busy(ext)) | |
6307 | continue; | |
6308 | ||
6309 | kfree(elem->priv); | |
6310 | elem->priv = catchall->elem; | |
6311 | nft_set_elem_change_active(net, set, ext); | |
6312 | return 0; | |
6313 | } | |
6314 | ||
6315 | return -ENOENT; | |
6316 | } | |
6317 | ||
6318 | static int __nft_setelem_deactivate(const struct net *net, | |
6319 | struct nft_set *set, | |
6320 | struct nft_set_elem *elem) | |
6321 | { | |
6322 | void *priv; | |
6323 | ||
6324 | priv = set->ops->deactivate(net, set, elem); | |
6325 | if (!priv) | |
6326 | return -ENOENT; | |
6327 | ||
6328 | kfree(elem->priv); | |
6329 | elem->priv = priv; | |
6330 | set->ndeact++; | |
6331 | ||
6332 | return 0; | |
6333 | } | |
6334 | ||
6335 | static int nft_setelem_deactivate(const struct net *net, | |
6336 | struct nft_set *set, | |
6337 | struct nft_set_elem *elem, u32 flags) | |
6338 | { | |
6339 | int ret; | |
6340 | ||
6341 | if (flags & NFT_SET_ELEM_CATCHALL) | |
6342 | ret = nft_setelem_catchall_deactivate(net, set, elem); | |
6343 | else | |
6344 | ret = __nft_setelem_deactivate(net, set, elem); | |
6345 | ||
6346 | return ret; | |
6347 | } | |
6348 | ||
6349 | static void nft_setelem_catchall_remove(const struct net *net, | |
6350 | const struct nft_set *set, | |
6351 | const struct nft_set_elem *elem) | |
6352 | { | |
6353 | struct nft_set_elem_catchall *catchall, *next; | |
6354 | ||
6355 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
6356 | if (catchall->elem == elem->priv) { | |
6357 | list_del_rcu(&catchall->list); | |
ae089831 | 6358 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
6359 | break; |
6360 | } | |
6361 | } | |
6362 | } | |
6363 | ||
6364 | static void nft_setelem_remove(const struct net *net, | |
6365 | const struct nft_set *set, | |
6366 | const struct nft_set_elem *elem) | |
6367 | { | |
6368 | if (nft_setelem_is_catchall(set, elem)) | |
6369 | nft_setelem_catchall_remove(net, set, elem); | |
6370 | else | |
6371 | set->ops->remove(net, set, elem); | |
6372 | } | |
6373 | ||
88cccd90 PNA |
6374 | static bool nft_setelem_valid_key_end(const struct nft_set *set, |
6375 | struct nlattr **nla, u32 flags) | |
6376 | { | |
6377 | if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) == | |
6378 | (NFT_SET_CONCAT | NFT_SET_INTERVAL)) { | |
6379 | if (flags & NFT_SET_ELEM_INTERVAL_END) | |
6380 | return false; | |
96df8360 PNA |
6381 | |
6382 | if (nla[NFTA_SET_ELEM_KEY_END] && | |
6383 | flags & NFT_SET_ELEM_CATCHALL) | |
88cccd90 PNA |
6384 | return false; |
6385 | } else { | |
6386 | if (nla[NFTA_SET_ELEM_KEY_END]) | |
6387 | return false; | |
6388 | } | |
6389 | ||
6390 | return true; | |
6391 | } | |
6392 | ||
60319eb1 | 6393 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 6394 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 | 6395 | { |
8cfd9b0f | 6396 | struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {}; |
20a69341 | 6397 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
8aeff920 | 6398 | u8 genmask = nft_genmask_next(ctx->net); |
664899e8 | 6399 | u32 flags = 0, size = 0, num_exprs = 0; |
fe2811eb | 6400 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 6401 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
6402 | struct nft_set_elem elem; |
6403 | struct nft_set_binding *binding; | |
8aeff920 | 6404 | struct nft_object *obj = NULL; |
68e942e8 | 6405 | struct nft_userdata *udata; |
20a1452c | 6406 | struct nft_data_desc desc; |
20a69341 | 6407 | enum nft_registers dreg; |
60319eb1 | 6408 | struct nft_trans *trans; |
c3e1b005 | 6409 | u64 timeout; |
79ebb5bb | 6410 | u64 expiration; |
8cfd9b0f | 6411 | int err, i; |
68e942e8 | 6412 | u8 ulen; |
20a69341 | 6413 | |
8cb08174 JB |
6414 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6415 | nft_set_elem_policy, NULL); | |
20a69341 PM |
6416 | if (err < 0) |
6417 | return err; | |
6418 | ||
fe2811eb PM |
6419 | nft_set_ext_prepare(&tmpl); |
6420 | ||
0e9091d6 PNA |
6421 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6422 | if (err < 0) | |
6423 | return err; | |
aaa31047 | 6424 | |
d4eb7e39 PNA |
6425 | if (((flags & NFT_SET_ELEM_CATCHALL) && nla[NFTA_SET_ELEM_KEY]) || |
6426 | (!(flags & NFT_SET_ELEM_CATCHALL) && !nla[NFTA_SET_ELEM_KEY])) | |
aaa31047 PNA |
6427 | return -EINVAL; |
6428 | ||
c39ba4de PNA |
6429 | if (flags != 0) { |
6430 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6431 | if (err < 0) | |
6432 | return err; | |
6433 | } | |
20a69341 PM |
6434 | |
6435 | if (set->flags & NFT_SET_MAP) { | |
6436 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 6437 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
6438 | return -EINVAL; |
6439 | } else { | |
6440 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
6441 | return -EINVAL; | |
6442 | } | |
6443 | ||
5a2f3dc3 PNA |
6444 | if (set->flags & NFT_SET_OBJECT) { |
6445 | if (!nla[NFTA_SET_ELEM_OBJREF] && | |
6446 | !(flags & NFT_SET_ELEM_INTERVAL_END)) | |
6447 | return -EINVAL; | |
6448 | } else { | |
6449 | if (nla[NFTA_SET_ELEM_OBJREF]) | |
6450 | return -EINVAL; | |
6451 | } | |
6452 | ||
88cccd90 PNA |
6453 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6454 | return -EINVAL; | |
6455 | ||
bffc124b PNA |
6456 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
6457 | (nla[NFTA_SET_ELEM_DATA] || | |
6458 | nla[NFTA_SET_ELEM_OBJREF] || | |
6459 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
6460 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
6461 | nla[NFTA_SET_ELEM_USERDATA] || | |
48b0ae04 | 6462 | nla[NFTA_SET_ELEM_EXPR] || |
4963674c | 6463 | nla[NFTA_SET_ELEM_KEY_END] || |
48b0ae04 | 6464 | nla[NFTA_SET_ELEM_EXPRESSIONS])) |
bffc124b PNA |
6465 | return -EINVAL; |
6466 | ||
c3e1b005 PM |
6467 | timeout = 0; |
6468 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
6469 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
6470 | return -EINVAL; | |
8e1102d5 FW |
6471 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
6472 | &timeout); | |
6473 | if (err) | |
6474 | return err; | |
33c7aba0 PNA |
6475 | } else if (set->flags & NFT_SET_TIMEOUT && |
6476 | !(flags & NFT_SET_ELEM_INTERVAL_END)) { | |
123b9961 | 6477 | timeout = READ_ONCE(set->timeout); |
c3e1b005 PM |
6478 | } |
6479 | ||
79ebb5bb LGL |
6480 | expiration = 0; |
6481 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
6482 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
6483 | return -EINVAL; | |
6484 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
6485 | &expiration); | |
6486 | if (err) | |
6487 | return err; | |
6488 | } | |
6489 | ||
8cfd9b0f PNA |
6490 | if (nla[NFTA_SET_ELEM_EXPR]) { |
6491 | struct nft_expr *expr; | |
6492 | ||
664899e8 | 6493 | if (set->num_exprs && set->num_exprs != 1) |
8cfd9b0f PNA |
6494 | return -EOPNOTSUPP; |
6495 | ||
40944452 PNA |
6496 | expr = nft_set_elem_expr_alloc(ctx, set, |
6497 | nla[NFTA_SET_ELEM_EXPR]); | |
6498 | if (IS_ERR(expr)) | |
6499 | return PTR_ERR(expr); | |
65038428 | 6500 | |
8cfd9b0f | 6501 | expr_array[0] = expr; |
664899e8 | 6502 | num_exprs = 1; |
65038428 | 6503 | |
664899e8 | 6504 | if (set->num_exprs && set->exprs[0]->ops != expr->ops) { |
8cfd9b0f | 6505 | err = -EOPNOTSUPP; |
65038428 | 6506 | goto err_set_elem_expr; |
8cfd9b0f | 6507 | } |
48b0ae04 PNA |
6508 | } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) { |
6509 | struct nft_expr *expr; | |
6510 | struct nlattr *tmp; | |
6511 | int left; | |
65038428 | 6512 | |
48b0ae04 PNA |
6513 | i = 0; |
6514 | nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) { | |
664899e8 PNA |
6515 | if (i == NFT_SET_EXPR_MAX || |
6516 | (set->num_exprs && set->num_exprs == i)) { | |
48b0ae04 PNA |
6517 | err = -E2BIG; |
6518 | goto err_set_elem_expr; | |
6519 | } | |
6520 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
6521 | err = -EINVAL; | |
6522 | goto err_set_elem_expr; | |
6523 | } | |
6524 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
6525 | if (IS_ERR(expr)) { | |
6526 | err = PTR_ERR(expr); | |
6527 | goto err_set_elem_expr; | |
6528 | } | |
6529 | expr_array[i] = expr; | |
664899e8 | 6530 | num_exprs++; |
48b0ae04 | 6531 | |
664899e8 | 6532 | if (set->num_exprs && expr->ops != set->exprs[i]->ops) { |
48b0ae04 PNA |
6533 | err = -EOPNOTSUPP; |
6534 | goto err_set_elem_expr; | |
6535 | } | |
6536 | i++; | |
6537 | } | |
664899e8 | 6538 | if (set->num_exprs && set->num_exprs != i) { |
48b0ae04 | 6539 | err = -EOPNOTSUPP; |
65038428 | 6540 | goto err_set_elem_expr; |
48b0ae04 | 6541 | } |
33c7aba0 PNA |
6542 | } else if (set->num_exprs > 0 && |
6543 | !(flags & NFT_SET_ELEM_INTERVAL_END)) { | |
8cfd9b0f PNA |
6544 | err = nft_set_elem_expr_clone(ctx, set, expr_array); |
6545 | if (err < 0) | |
6546 | goto err_set_elem_expr_clone; | |
664899e8 PNA |
6547 | |
6548 | num_exprs = set->num_exprs; | |
40944452 PNA |
6549 | } |
6550 | ||
aaa31047 PNA |
6551 | if (nla[NFTA_SET_ELEM_KEY]) { |
6552 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6553 | nla[NFTA_SET_ELEM_KEY]); | |
6554 | if (err < 0) | |
6555 | goto err_set_elem_expr; | |
20a69341 | 6556 | |
c39ba4de PNA |
6557 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6558 | if (err < 0) | |
6559 | goto err_parse_key; | |
aaa31047 | 6560 | } |
7b225d0b PNA |
6561 | |
6562 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
6563 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6564 | nla[NFTA_SET_ELEM_KEY_END]); | |
6565 | if (err < 0) | |
6566 | goto err_parse_key; | |
6567 | ||
c39ba4de PNA |
6568 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6569 | if (err < 0) | |
6570 | goto err_parse_key_end; | |
7b225d0b PNA |
6571 | } |
6572 | ||
c3e1b005 | 6573 | if (timeout > 0) { |
c39ba4de PNA |
6574 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); |
6575 | if (err < 0) | |
6576 | goto err_parse_key_end; | |
6577 | ||
123b9961 | 6578 | if (timeout != READ_ONCE(set->timeout)) { |
c39ba4de PNA |
6579 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); |
6580 | if (err < 0) | |
6581 | goto err_parse_key_end; | |
6582 | } | |
c3e1b005 | 6583 | } |
fe2811eb | 6584 | |
664899e8 PNA |
6585 | if (num_exprs) { |
6586 | for (i = 0; i < num_exprs; i++) | |
563125a7 PNA |
6587 | size += expr_array[i]->ops->size; |
6588 | ||
c39ba4de PNA |
6589 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS, |
6590 | sizeof(struct nft_set_elem_expr) + size); | |
6591 | if (err < 0) | |
6592 | goto err_parse_key_end; | |
563125a7 | 6593 | } |
40944452 | 6594 | |
8aeff920 | 6595 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
4d44175a FW |
6596 | obj = nft_obj_lookup(ctx->net, ctx->table, |
6597 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 6598 | set->objtype, genmask); |
8aeff920 PNA |
6599 | if (IS_ERR(obj)) { |
6600 | err = PTR_ERR(obj); | |
7b225d0b | 6601 | goto err_parse_key_end; |
8aeff920 | 6602 | } |
c39ba4de PNA |
6603 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); |
6604 | if (err < 0) | |
6605 | goto err_parse_key_end; | |
8aeff920 PNA |
6606 | } |
6607 | ||
20a69341 | 6608 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
fdb9c405 PNA |
6609 | err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val, |
6610 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 | 6611 | if (err < 0) |
7b225d0b | 6612 | goto err_parse_key_end; |
20a69341 | 6613 | |
20a69341 PM |
6614 | dreg = nft_type_to_reg(set->dtype); |
6615 | list_for_each_entry(binding, &set->bindings, list) { | |
6616 | struct nft_ctx bind_ctx = { | |
58c78e10 | 6617 | .net = ctx->net, |
36596dad | 6618 | .family = ctx->family, |
20a69341 | 6619 | .table = ctx->table, |
7c95f6d8 | 6620 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
6621 | }; |
6622 | ||
11113e19 PM |
6623 | if (!(binding->flags & NFT_SET_MAP)) |
6624 | continue; | |
6625 | ||
1ec10212 | 6626 | err = nft_validate_register_store(&bind_ctx, dreg, |
fdb9c405 | 6627 | &elem.data.val, |
20a1452c | 6628 | desc.type, desc.len); |
20a69341 | 6629 | if (err < 0) |
7b225d0b | 6630 | goto err_parse_data; |
a654de8f | 6631 | |
20a1452c | 6632 | if (desc.type == NFT_DATA_VERDICT && |
fdb9c405 PNA |
6633 | (elem.data.val.verdict.code == NFT_GOTO || |
6634 | elem.data.val.verdict.code == NFT_JUMP)) | |
00c320f9 | 6635 | nft_validate_state_update(ctx->table, |
a654de8f | 6636 | NFT_VALIDATE_NEED); |
20a69341 | 6637 | } |
fe2811eb | 6638 | |
c39ba4de PNA |
6639 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
6640 | if (err < 0) | |
6641 | goto err_parse_data; | |
20a69341 PM |
6642 | } |
6643 | ||
68e942e8 PM |
6644 | /* The full maximum length of userdata can exceed the maximum |
6645 | * offset value (U8_MAX) for following extensions, therefor it | |
6646 | * must be the last extension added. | |
6647 | */ | |
6648 | ulen = 0; | |
6649 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
6650 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
c39ba4de PNA |
6651 | if (ulen > 0) { |
6652 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
6653 | ulen); | |
6654 | if (err < 0) | |
6655 | goto err_parse_data; | |
6656 | } | |
68e942e8 PM |
6657 | } |
6658 | ||
7b225d0b | 6659 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
fdb9c405 | 6660 | elem.key_end.val.data, elem.data.val.data, |
33758c89 | 6661 | timeout, expiration, GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6662 | if (IS_ERR(elem.priv)) { |
6663 | err = PTR_ERR(elem.priv); | |
7b225d0b | 6664 | goto err_parse_data; |
34aae2c2 | 6665 | } |
fe2811eb PM |
6666 | |
6667 | ext = nft_set_elem_ext(set, elem.priv); | |
6668 | if (flags) | |
6669 | *nft_set_ext_flags(ext) = flags; | |
34aae2c2 | 6670 | |
d6b47866 PNA |
6671 | if (obj) { |
6672 | *nft_set_ext_obj(ext) = obj; | |
6673 | obj->use++; | |
6674 | } | |
68e942e8 | 6675 | if (ulen > 0) { |
34aae2c2 PNA |
6676 | if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) { |
6677 | err = -EINVAL; | |
d6b47866 | 6678 | goto err_elem_free; |
34aae2c2 | 6679 | } |
68e942e8 PM |
6680 | udata = nft_set_ext_userdata(ext); |
6681 | udata->len = ulen - 1; | |
6682 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
6683 | } | |
34aae2c2 | 6684 | err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs); |
4d8f9065 | 6685 | if (err < 0) |
34aae2c2 | 6686 | goto err_elem_free; |
fe2811eb | 6687 | |
60319eb1 | 6688 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
4d8f9065 PNA |
6689 | if (trans == NULL) { |
6690 | err = -ENOMEM; | |
34aae2c2 | 6691 | goto err_elem_free; |
4d8f9065 | 6692 | } |
60319eb1 | 6693 | |
69086658 | 6694 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
aaa31047 PNA |
6695 | |
6696 | err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags); | |
c016c7e4 PNA |
6697 | if (err) { |
6698 | if (err == -EEXIST) { | |
9744a6fc PNA |
6699 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
6700 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
6701 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
77a92189 | 6702 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) |
7b225d0b | 6703 | goto err_element_clash; |
8aeff920 PNA |
6704 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
6705 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
6706 | memcmp(nft_set_ext_data(ext), | |
6707 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
6708 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
6709 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
6710 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
77a92189 | 6711 | goto err_element_clash; |
c016c7e4 PNA |
6712 | else if (!(nlmsg_flags & NLM_F_EXCL)) |
6713 | err = 0; | |
8c2d45b2 PNA |
6714 | } else if (err == -ENOTEMPTY) { |
6715 | /* ENOTEMPTY reports overlapping between this element | |
6716 | * and an existing one. | |
6717 | */ | |
6718 | err = -EEXIST; | |
c016c7e4 | 6719 | } |
7b225d0b | 6720 | goto err_element_clash; |
c016c7e4 | 6721 | } |
20a69341 | 6722 | |
aaa31047 | 6723 | if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size && |
35d0ac90 PNA |
6724 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { |
6725 | err = -ENFILE; | |
7b225d0b | 6726 | goto err_set_full; |
35d0ac90 PNA |
6727 | } |
6728 | ||
60319eb1 | 6729 | nft_trans_elem(trans) = elem; |
0854db2a | 6730 | nft_trans_commit_list_add_tail(ctx->net, trans); |
20a69341 PM |
6731 | return 0; |
6732 | ||
7b225d0b | 6733 | err_set_full: |
aaa31047 | 6734 | nft_setelem_remove(ctx->net, set, &elem); |
7b225d0b | 6735 | err_element_clash: |
60319eb1 | 6736 | kfree(trans); |
34aae2c2 | 6737 | err_elem_free: |
628bd3e4 | 6738 | nft_set_elem_destroy(set, elem.priv, true); |
7b225d0b | 6739 | err_parse_data: |
20a69341 | 6740 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
fdb9c405 | 6741 | nft_data_release(&elem.data.val, desc.type); |
7b225d0b PNA |
6742 | err_parse_key_end: |
6743 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
6744 | err_parse_key: | |
20a1452c | 6745 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
40944452 | 6746 | err_set_elem_expr: |
664899e8 | 6747 | for (i = 0; i < num_exprs && expr_array[i]; i++) |
8cfd9b0f PNA |
6748 | nft_expr_destroy(ctx, expr_array[i]); |
6749 | err_set_elem_expr_clone: | |
20a69341 PM |
6750 | return err; |
6751 | } | |
6752 | ||
7dab8ee3 PNA |
6753 | static int nf_tables_newsetelem(struct sk_buff *skb, |
6754 | const struct nfnl_info *info, | |
6755 | const struct nlattr * const nla[]) | |
20a69341 | 6756 | { |
7dab8ee3 PNA |
6757 | struct netlink_ext_ack *extack = info->extack; |
6758 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6759 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6760 | struct net *net = info->net; |
20a69341 | 6761 | const struct nlattr *attr; |
e2b750d7 | 6762 | struct nft_table *table; |
20a69341 PM |
6763 | struct nft_set *set; |
6764 | struct nft_ctx ctx; | |
a654de8f | 6765 | int rem, err; |
20a69341 | 6766 | |
7d5570ca PNA |
6767 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
6768 | return -EINVAL; | |
6769 | ||
e2b750d7 PNA |
6770 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6771 | genmask, NETLINK_CB(skb).portid); | |
6772 | if (IS_ERR(table)) { | |
6773 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6774 | return PTR_ERR(table); | |
6775 | } | |
20a69341 | 6776 | |
e2b750d7 | 6777 | set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET], |
a3073c17 PNA |
6778 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); |
6779 | if (IS_ERR(set)) | |
6780 | return PTR_ERR(set); | |
958bee14 | 6781 | |
c88c535b PNA |
6782 | if (!list_empty(&set->bindings) && |
6783 | (set->flags & (NFT_SET_CONSTANT | NFT_SET_ANONYMOUS))) | |
20a69341 PM |
6784 | return -EBUSY; |
6785 | ||
e2b750d7 PNA |
6786 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6787 | ||
20a69341 | 6788 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7dab8ee3 | 6789 | err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags); |
b53c1166 PNA |
6790 | if (err < 0) { |
6791 | NL_SET_BAD_ATTR(extack, attr); | |
a654de8f | 6792 | return err; |
b53c1166 | 6793 | } |
20a69341 | 6794 | } |
a654de8f | 6795 | |
00c320f9 | 6796 | if (table->validate_state == NFT_VALIDATE_DO) |
e2b750d7 | 6797 | return nft_table_validate(net, table); |
a654de8f PNA |
6798 | |
6799 | return 0; | |
20a69341 PM |
6800 | } |
6801 | ||
59105446 PNA |
6802 | /** |
6803 | * nft_data_hold - hold a nft_data item | |
6804 | * | |
6805 | * @data: struct nft_data to release | |
6806 | * @type: type of data | |
6807 | * | |
6808 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
6809 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
6810 | * NFT_GOTO verdicts. This function must be called on active data objects | |
6811 | * from the second phase of the commit protocol. | |
6812 | */ | |
bb7b40ae | 6813 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 | 6814 | { |
d0e2c7de | 6815 | struct nft_chain *chain; |
d0e2c7de | 6816 | |
59105446 PNA |
6817 | if (type == NFT_DATA_VERDICT) { |
6818 | switch (data->verdict.code) { | |
6819 | case NFT_JUMP: | |
6820 | case NFT_GOTO: | |
d0e2c7de PNA |
6821 | chain = data->verdict.chain; |
6822 | chain->use++; | |
59105446 PNA |
6823 | break; |
6824 | } | |
6825 | } | |
6826 | } | |
6827 | ||
f8bb7889 PNA |
6828 | static void nft_setelem_data_activate(const struct net *net, |
6829 | const struct nft_set *set, | |
6830 | struct nft_set_elem *elem) | |
59105446 PNA |
6831 | { |
6832 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6833 | ||
6834 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6835 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
6836 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6837 | (*nft_set_ext_obj(ext))->use++; | |
6838 | } | |
6839 | ||
f8bb7889 PNA |
6840 | static void nft_setelem_data_deactivate(const struct net *net, |
6841 | const struct nft_set *set, | |
6842 | struct nft_set_elem *elem) | |
59105446 PNA |
6843 | { |
6844 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6845 | ||
6846 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6847 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
6848 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6849 | (*nft_set_ext_obj(ext))->use--; | |
6850 | } | |
6851 | ||
60319eb1 | 6852 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
6853 | const struct nlattr *attr) |
6854 | { | |
6855 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 6856 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 6857 | struct nft_set_elem elem; |
3971ca14 | 6858 | struct nft_set_ext *ext; |
60319eb1 | 6859 | struct nft_trans *trans; |
3971ca14 | 6860 | u32 flags = 0; |
20a69341 PM |
6861 | int err; |
6862 | ||
8cb08174 JB |
6863 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6864 | nft_set_elem_policy, NULL); | |
20a69341 | 6865 | if (err < 0) |
20a1452c | 6866 | return err; |
20a69341 | 6867 | |
aaa31047 PNA |
6868 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6869 | if (err < 0) | |
6870 | return err; | |
6871 | ||
6872 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
20a1452c | 6873 | return -EINVAL; |
20a69341 | 6874 | |
88cccd90 PNA |
6875 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6876 | return -EINVAL; | |
6877 | ||
3971ca14 PNA |
6878 | nft_set_ext_prepare(&tmpl); |
6879 | ||
c39ba4de PNA |
6880 | if (flags != 0) { |
6881 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6882 | if (err < 0) | |
6883 | return err; | |
6884 | } | |
3971ca14 | 6885 | |
aaa31047 PNA |
6886 | if (nla[NFTA_SET_ELEM_KEY]) { |
6887 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6888 | nla[NFTA_SET_ELEM_KEY]); | |
6889 | if (err < 0) | |
6890 | return err; | |
20a69341 | 6891 | |
c39ba4de PNA |
6892 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6893 | if (err < 0) | |
6894 | goto fail_elem; | |
aaa31047 | 6895 | } |
3971ca14 | 6896 | |
7b225d0b PNA |
6897 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
6898 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6899 | nla[NFTA_SET_ELEM_KEY_END]); | |
6900 | if (err < 0) | |
c39ba4de | 6901 | goto fail_elem; |
7b225d0b | 6902 | |
c39ba4de PNA |
6903 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6904 | if (err < 0) | |
6905 | goto fail_elem_key_end; | |
7b225d0b PNA |
6906 | } |
6907 | ||
3971ca14 | 6908 | err = -ENOMEM; |
7b225d0b PNA |
6909 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
6910 | elem.key_end.val.data, NULL, 0, 0, | |
33758c89 | 6911 | GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6912 | if (IS_ERR(elem.priv)) { |
6913 | err = PTR_ERR(elem.priv); | |
c39ba4de | 6914 | goto fail_elem_key_end; |
34aae2c2 | 6915 | } |
3971ca14 PNA |
6916 | |
6917 | ext = nft_set_elem_ext(set, elem.priv); | |
6918 | if (flags) | |
6919 | *nft_set_ext_flags(ext) = flags; | |
6920 | ||
60319eb1 | 6921 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
6922 | if (trans == NULL) |
6923 | goto fail_trans; | |
20a69341 | 6924 | |
aaa31047 PNA |
6925 | err = nft_setelem_deactivate(ctx->net, set, &elem, flags); |
6926 | if (err < 0) | |
20a1452c | 6927 | goto fail_ops; |
cc02e457 | 6928 | |
f8bb7889 | 6929 | nft_setelem_data_deactivate(ctx->net, set, &elem); |
59105446 | 6930 | |
60319eb1 | 6931 | nft_trans_elem(trans) = elem; |
0854db2a | 6932 | nft_trans_commit_list_add_tail(ctx->net, trans); |
0dc13625 | 6933 | return 0; |
cc02e457 | 6934 | |
20a1452c | 6935 | fail_ops: |
cc02e457 | 6936 | kfree(trans); |
20a1452c | 6937 | fail_trans: |
3971ca14 | 6938 | kfree(elem.priv); |
c39ba4de PNA |
6939 | fail_elem_key_end: |
6940 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
20a1452c PNA |
6941 | fail_elem: |
6942 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
6943 | return err; |
6944 | } | |
6945 | ||
e6ba7cb6 PNA |
6946 | static int nft_setelem_flush(const struct nft_ctx *ctx, |
6947 | struct nft_set *set, | |
6948 | const struct nft_set_iter *iter, | |
6949 | struct nft_set_elem *elem) | |
8411b644 PNA |
6950 | { |
6951 | struct nft_trans *trans; | |
6952 | int err; | |
6953 | ||
6954 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6955 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
6956 | if (!trans) | |
6957 | return -ENOMEM; | |
6958 | ||
1ba1c414 | 6959 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
6960 | err = -ENOENT; |
6961 | goto err1; | |
6962 | } | |
b2c11e4b | 6963 | set->ndeact++; |
8411b644 | 6964 | |
f8bb7889 | 6965 | nft_setelem_data_deactivate(ctx->net, set, elem); |
de70185d PNA |
6966 | nft_trans_elem_set(trans) = set; |
6967 | nft_trans_elem(trans) = *elem; | |
0854db2a | 6968 | nft_trans_commit_list_add_tail(ctx->net, trans); |
8411b644 PNA |
6969 | |
6970 | return 0; | |
6971 | err1: | |
6972 | kfree(trans); | |
6973 | return err; | |
6974 | } | |
6975 | ||
aaa31047 PNA |
6976 | static int __nft_set_catchall_flush(const struct nft_ctx *ctx, |
6977 | struct nft_set *set, | |
6978 | struct nft_set_elem *elem) | |
6979 | { | |
6980 | struct nft_trans *trans; | |
6981 | ||
6982 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6983 | sizeof(struct nft_trans_elem), GFP_KERNEL); | |
6984 | if (!trans) | |
6985 | return -ENOMEM; | |
6986 | ||
6987 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
6988 | nft_trans_elem_set(trans) = set; | |
6989 | nft_trans_elem(trans) = *elem; | |
6990 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
6991 | ||
6992 | return 0; | |
6993 | } | |
6994 | ||
6995 | static int nft_set_catchall_flush(const struct nft_ctx *ctx, | |
6996 | struct nft_set *set) | |
6997 | { | |
6998 | u8 genmask = nft_genmask_next(ctx->net); | |
6999 | struct nft_set_elem_catchall *catchall; | |
7000 | struct nft_set_elem elem; | |
7001 | struct nft_set_ext *ext; | |
7002 | int ret = 0; | |
7003 | ||
7004 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
7005 | ext = nft_set_elem_ext(set, catchall->elem); | |
7006 | if (!nft_set_elem_active(ext, genmask) || | |
7007 | nft_set_elem_mark_busy(ext)) | |
7008 | continue; | |
7009 | ||
7010 | elem.priv = catchall->elem; | |
7011 | ret = __nft_set_catchall_flush(ctx, set, &elem); | |
7012 | if (ret < 0) | |
7013 | break; | |
7014 | } | |
7015 | ||
7016 | return ret; | |
7017 | } | |
7018 | ||
e6ba7cb6 PNA |
7019 | static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) |
7020 | { | |
7021 | struct nft_set_iter iter = { | |
7022 | .genmask = genmask, | |
7023 | .fn = nft_setelem_flush, | |
7024 | }; | |
7025 | ||
7026 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
7027 | if (!iter.err) |
7028 | iter.err = nft_set_catchall_flush(ctx, set); | |
e6ba7cb6 PNA |
7029 | |
7030 | return iter.err; | |
7031 | } | |
7032 | ||
7dab8ee3 PNA |
7033 | static int nf_tables_delsetelem(struct sk_buff *skb, |
7034 | const struct nfnl_info *info, | |
7035 | const struct nlattr * const nla[]) | |
20a69341 | 7036 | { |
7dab8ee3 PNA |
7037 | struct netlink_ext_ack *extack = info->extack; |
7038 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 7039 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 7040 | struct net *net = info->net; |
20a69341 | 7041 | const struct nlattr *attr; |
e2b750d7 | 7042 | struct nft_table *table; |
20a69341 PM |
7043 | struct nft_set *set; |
7044 | struct nft_ctx ctx; | |
60319eb1 | 7045 | int rem, err = 0; |
20a69341 | 7046 | |
e2b750d7 PNA |
7047 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
7048 | genmask, NETLINK_CB(skb).portid); | |
7049 | if (IS_ERR(table)) { | |
7050 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
7051 | return PTR_ERR(table); | |
7052 | } | |
20a69341 | 7053 | |
e2b750d7 | 7054 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
7055 | if (IS_ERR(set)) |
7056 | return PTR_ERR(set); | |
c88c535b PNA |
7057 | |
7058 | if (!list_empty(&set->bindings) && | |
7059 | (set->flags & (NFT_SET_CONSTANT | NFT_SET_ANONYMOUS))) | |
20a69341 PM |
7060 | return -EBUSY; |
7061 | ||
e2b750d7 PNA |
7062 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
7063 | ||
e6ba7cb6 PNA |
7064 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) |
7065 | return nft_set_flush(&ctx, set, genmask); | |
8411b644 | 7066 | |
20a69341 PM |
7067 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7068 | err = nft_del_setelem(&ctx, set, attr); | |
f80a612d FFM |
7069 | if (err == -ENOENT && |
7070 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSETELEM) | |
7071 | continue; | |
7072 | ||
b53c1166 PNA |
7073 | if (err < 0) { |
7074 | NL_SET_BAD_ATTR(extack, attr); | |
60319eb1 | 7075 | break; |
b53c1166 | 7076 | } |
20a69341 | 7077 | } |
60319eb1 | 7078 | return err; |
20a69341 PM |
7079 | } |
7080 | ||
cfed7e1b PM |
7081 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
7082 | { | |
7083 | struct nft_set_gc_batch *gcb; | |
7084 | unsigned int i; | |
7085 | ||
7086 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
7087 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 7088 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
7089 | kfree(gcb); |
7090 | } | |
cfed7e1b PM |
7091 | |
7092 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
7093 | gfp_t gfp) | |
7094 | { | |
7095 | struct nft_set_gc_batch *gcb; | |
7096 | ||
7097 | gcb = kzalloc(sizeof(*gcb), gfp); | |
7098 | if (gcb == NULL) | |
7099 | return gcb; | |
7100 | gcb->head.set = set; | |
7101 | return gcb; | |
7102 | } | |
cfed7e1b | 7103 | |
e5009240 PNA |
7104 | /* |
7105 | * Stateful objects | |
7106 | */ | |
7107 | ||
7108 | /** | |
7109 | * nft_register_obj- register nf_tables stateful object type | |
3db86c39 | 7110 | * @obj_type: object type |
e5009240 PNA |
7111 | * |
7112 | * Registers the object type for use with nf_tables. Returns zero on | |
7113 | * success or a negative errno code otherwise. | |
7114 | */ | |
7115 | int nft_register_obj(struct nft_object_type *obj_type) | |
7116 | { | |
7117 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
7118 | return -EINVAL; | |
7119 | ||
7120 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7121 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
7122 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7123 | return 0; | |
7124 | } | |
7125 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
7126 | ||
7127 | /** | |
7128 | * nft_unregister_obj - unregister nf_tables object type | |
3db86c39 | 7129 | * @obj_type: object type |
e5009240 PNA |
7130 | * |
7131 | * Unregisters the object type for use with nf_tables. | |
7132 | */ | |
7133 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
7134 | { | |
7135 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7136 | list_del_rcu(&obj_type->list); | |
7137 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7138 | } | |
7139 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
7140 | ||
4d44175a FW |
7141 | struct nft_object *nft_obj_lookup(const struct net *net, |
7142 | const struct nft_table *table, | |
cac20fcd PNA |
7143 | const struct nlattr *nla, u32 objtype, |
7144 | u8 genmask) | |
e5009240 | 7145 | { |
4d44175a FW |
7146 | struct nft_object_hash_key k = { .table = table }; |
7147 | char search[NFT_OBJ_MAXNAMELEN]; | |
7148 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
7149 | struct nft_object *obj; |
7150 | ||
872f6903 | 7151 | nla_strscpy(search, nla, sizeof(search)); |
4d44175a FW |
7152 | k.name = search; |
7153 | ||
7154 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
7155 | !lockdep_commit_lock_is_held(net)); | |
7156 | ||
7157 | rcu_read_lock(); | |
7158 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
7159 | if (!list) | |
7160 | goto out; | |
7161 | ||
7162 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
7163 | if (objtype == obj->ops->type->type && | |
7164 | nft_active_genmask(obj, genmask)) { | |
7165 | rcu_read_unlock(); | |
e5009240 | 7166 | return obj; |
4d44175a | 7167 | } |
e5009240 | 7168 | } |
4d44175a FW |
7169 | out: |
7170 | rcu_read_unlock(); | |
e5009240 PNA |
7171 | return ERR_PTR(-ENOENT); |
7172 | } | |
cac20fcd | 7173 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 7174 | |
cac20fcd PNA |
7175 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
7176 | const struct nlattr *nla, | |
7177 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
7178 | { |
7179 | struct nft_object *obj; | |
7180 | ||
7181 | list_for_each_entry(obj, &table->objects, list) { | |
7182 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
7183 | objtype == obj->ops->type->type && | |
7184 | nft_active_genmask(obj, genmask)) | |
7185 | return obj; | |
7186 | } | |
7187 | return ERR_PTR(-ENOENT); | |
7188 | } | |
7189 | ||
e5009240 | 7190 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
7191 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
7192 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
7193 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
7194 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
7195 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
7196 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7197 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
b131c964 JGG |
7198 | [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY, |
7199 | .len = NFT_USERDATA_MAXLEN }, | |
e5009240 PNA |
7200 | }; |
7201 | ||
84fba055 FW |
7202 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
7203 | const struct nft_object_type *type, | |
e5009240 PNA |
7204 | const struct nlattr *attr) |
7205 | { | |
5b4c6e38 | 7206 | struct nlattr **tb; |
dfc46034 | 7207 | const struct nft_object_ops *ops; |
e5009240 | 7208 | struct nft_object *obj; |
5b4c6e38 GS |
7209 | int err = -ENOMEM; |
7210 | ||
7211 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
7212 | if (!tb) | |
7213 | goto err1; | |
e5009240 PNA |
7214 | |
7215 | if (attr) { | |
8cb08174 JB |
7216 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
7217 | type->policy, NULL); | |
e5009240 | 7218 | if (err < 0) |
5b4c6e38 | 7219 | goto err2; |
e5009240 PNA |
7220 | } else { |
7221 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
7222 | } | |
7223 | ||
dfc46034 PBG |
7224 | if (type->select_ops) { |
7225 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
7226 | if (IS_ERR(ops)) { | |
7227 | err = PTR_ERR(ops); | |
5b4c6e38 | 7228 | goto err2; |
dfc46034 PBG |
7229 | } |
7230 | } else { | |
7231 | ops = type->ops; | |
7232 | } | |
7233 | ||
e5009240 | 7234 | err = -ENOMEM; |
33758c89 | 7235 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT); |
5b4c6e38 GS |
7236 | if (!obj) |
7237 | goto err2; | |
e5009240 | 7238 | |
dfc46034 | 7239 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 7240 | if (err < 0) |
5b4c6e38 | 7241 | goto err3; |
e5009240 | 7242 | |
dfc46034 PBG |
7243 | obj->ops = ops; |
7244 | ||
5b4c6e38 | 7245 | kfree(tb); |
e5009240 | 7246 | return obj; |
5b4c6e38 | 7247 | err3: |
e5009240 | 7248 | kfree(obj); |
5b4c6e38 GS |
7249 | err2: |
7250 | kfree(tb); | |
e5009240 PNA |
7251 | err1: |
7252 | return ERR_PTR(err); | |
7253 | } | |
7254 | ||
7255 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 7256 | struct nft_object *obj, bool reset) |
e5009240 PNA |
7257 | { |
7258 | struct nlattr *nest; | |
7259 | ||
ae0be8de | 7260 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
7261 | if (!nest) |
7262 | goto nla_put_failure; | |
dfc46034 | 7263 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
7264 | goto nla_put_failure; |
7265 | nla_nest_end(skb, nest); | |
7266 | return 0; | |
7267 | ||
7268 | nla_put_failure: | |
7269 | return -1; | |
7270 | } | |
7271 | ||
7272 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
7273 | { | |
7274 | const struct nft_object_type *type; | |
7275 | ||
7276 | list_for_each_entry(type, &nf_tables_objects, list) { | |
7277 | if (objtype == type->type) | |
7278 | return type; | |
7279 | } | |
7280 | return NULL; | |
7281 | } | |
7282 | ||
452238e8 FW |
7283 | static const struct nft_object_type * |
7284 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
7285 | { |
7286 | const struct nft_object_type *type; | |
7287 | ||
7288 | type = __nft_obj_type_get(objtype); | |
7289 | if (type != NULL && try_module_get(type->owner)) | |
7290 | return type; | |
7291 | ||
f102d66b | 7292 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
7293 | #ifdef CONFIG_MODULES |
7294 | if (type == NULL) { | |
eb014de4 | 7295 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
7296 | return ERR_PTR(-EAGAIN); |
7297 | } | |
7298 | #endif | |
7299 | return ERR_PTR(-ENOENT); | |
7300 | } | |
7301 | ||
d62d0ba9 FFM |
7302 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
7303 | const struct nft_object_type *type, | |
7304 | const struct nlattr *attr, | |
7305 | struct nft_object *obj) | |
7306 | { | |
7307 | struct nft_object *newobj; | |
7308 | struct nft_trans *trans; | |
dad3bdee FW |
7309 | int err = -ENOMEM; |
7310 | ||
7311 | if (!try_module_get(type->owner)) | |
7312 | return -ENOENT; | |
d62d0ba9 FFM |
7313 | |
7314 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
7315 | sizeof(struct nft_trans_obj)); | |
7316 | if (!trans) | |
dad3bdee | 7317 | goto err_trans; |
d62d0ba9 FFM |
7318 | |
7319 | newobj = nft_obj_init(ctx, type, attr); | |
7320 | if (IS_ERR(newobj)) { | |
7321 | err = PTR_ERR(newobj); | |
b74ae961 | 7322 | goto err_free_trans; |
d62d0ba9 FFM |
7323 | } |
7324 | ||
7325 | nft_trans_obj(trans) = obj; | |
7326 | nft_trans_obj_update(trans) = true; | |
7327 | nft_trans_obj_newobj(trans) = newobj; | |
0854db2a | 7328 | nft_trans_commit_list_add_tail(ctx->net, trans); |
d62d0ba9 FFM |
7329 | |
7330 | return 0; | |
b74ae961 DC |
7331 | |
7332 | err_free_trans: | |
d62d0ba9 | 7333 | kfree(trans); |
dad3bdee FW |
7334 | err_trans: |
7335 | module_put(type->owner); | |
d62d0ba9 FFM |
7336 | return err; |
7337 | } | |
7338 | ||
7dab8ee3 PNA |
7339 | static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info, |
7340 | const struct nlattr * const nla[]) | |
e5009240 | 7341 | { |
7dab8ee3 PNA |
7342 | struct netlink_ext_ack *extack = info->extack; |
7343 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7344 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7345 | const struct nft_object_type *type; |
7dab8ee3 | 7346 | struct net *net = info->net; |
e5009240 PNA |
7347 | struct nft_table *table; |
7348 | struct nft_object *obj; | |
7349 | struct nft_ctx ctx; | |
7350 | u32 objtype; | |
7351 | int err; | |
7352 | ||
7353 | if (!nla[NFTA_OBJ_TYPE] || | |
7354 | !nla[NFTA_OBJ_NAME] || | |
7355 | !nla[NFTA_OBJ_DATA]) | |
7356 | return -EINVAL; | |
7357 | ||
6001a930 PNA |
7358 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7359 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7360 | if (IS_ERR(table)) { |
7361 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7362 | return PTR_ERR(table); |
36dd1bcc | 7363 | } |
e5009240 PNA |
7364 | |
7365 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7366 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
7367 | if (IS_ERR(obj)) { |
7368 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
7369 | if (err != -ENOENT) { |
7370 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7371 | return err; |
36dd1bcc | 7372 | } |
1a28ad74 | 7373 | } else { |
7dab8ee3 | 7374 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 7375 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); |
e5009240 | 7376 | return -EEXIST; |
36dd1bcc | 7377 | } |
7dab8ee3 | 7378 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
d62d0ba9 FFM |
7379 | return -EOPNOTSUPP; |
7380 | ||
fd57d0cb | 7381 | type = __nft_obj_type_get(objtype); |
dac7f50a AT |
7382 | if (WARN_ON_ONCE(!type)) |
7383 | return -ENOENT; | |
7384 | ||
7dab8ee3 | 7385 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
d62d0ba9 FFM |
7386 | |
7387 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
7388 | } |
7389 | ||
7dab8ee3 | 7390 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 | 7391 | |
452238e8 | 7392 | type = nft_obj_type_get(net, objtype); |
e5009240 PNA |
7393 | if (IS_ERR(type)) |
7394 | return PTR_ERR(type); | |
7395 | ||
84fba055 | 7396 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
7397 | if (IS_ERR(obj)) { |
7398 | err = PTR_ERR(obj); | |
b131c964 | 7399 | goto err_init; |
e5009240 | 7400 | } |
d152159b | 7401 | obj->key.table = table; |
3ecbfd65 HS |
7402 | obj->handle = nf_tables_alloc_handle(table); |
7403 | ||
33758c89 | 7404 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT); |
d152159b | 7405 | if (!obj->key.name) { |
61509575 | 7406 | err = -ENOMEM; |
b131c964 JGG |
7407 | goto err_strdup; |
7408 | } | |
7409 | ||
7410 | if (nla[NFTA_OBJ_USERDATA]) { | |
af0acf22 | 7411 | obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL_ACCOUNT); |
b131c964 JGG |
7412 | if (obj->udata == NULL) |
7413 | goto err_userdata; | |
7414 | ||
85db827a | 7415 | obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]); |
61509575 | 7416 | } |
e5009240 PNA |
7417 | |
7418 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
7419 | if (err < 0) | |
b131c964 | 7420 | goto err_trans; |
e5009240 | 7421 | |
4d44175a FW |
7422 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
7423 | nft_objname_ht_params); | |
7424 | if (err < 0) | |
b131c964 | 7425 | goto err_obj_ht; |
4d44175a | 7426 | |
e5009240 PNA |
7427 | list_add_tail_rcu(&obj->list, &table->objects); |
7428 | table->use++; | |
7429 | return 0; | |
b131c964 | 7430 | err_obj_ht: |
4d44175a FW |
7431 | /* queued in transaction log */ |
7432 | INIT_LIST_HEAD(&obj->list); | |
7433 | return err; | |
b131c964 | 7434 | err_trans: |
b131c964 | 7435 | kfree(obj->udata); |
85dfd816 PNA |
7436 | err_userdata: |
7437 | kfree(obj->key.name); | |
b131c964 | 7438 | err_strdup: |
dfc46034 | 7439 | if (obj->ops->destroy) |
00bfb320 | 7440 | obj->ops->destroy(&ctx, obj); |
e5009240 | 7441 | kfree(obj); |
b131c964 | 7442 | err_init: |
e5009240 PNA |
7443 | module_put(type->owner); |
7444 | return err; | |
7445 | } | |
7446 | ||
7447 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
7448 | u32 portid, u32 seq, int event, u32 flags, | |
7449 | int family, const struct nft_table *table, | |
43da04a5 | 7450 | struct nft_object *obj, bool reset) |
e5009240 | 7451 | { |
e5009240 PNA |
7452 | struct nlmsghdr *nlh; |
7453 | ||
dedb67c4 | 7454 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
7455 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
7456 | NFNETLINK_V0, nft_base_seq(net)); | |
7457 | if (!nlh) | |
e5009240 PNA |
7458 | goto nla_put_failure; |
7459 | ||
e5009240 | 7460 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || |
d152159b | 7461 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
3ecbfd65 HS |
7462 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), |
7463 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
7464 | goto nla_put_failure; |
7465 | ||
28339b21 PNA |
7466 | if (event == NFT_MSG_DELOBJ) { |
7467 | nlmsg_end(skb, nlh); | |
7468 | return 0; | |
7469 | } | |
7470 | ||
7471 | if (nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || | |
7472 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || | |
7473 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset)) | |
7474 | goto nla_put_failure; | |
7475 | ||
b131c964 JGG |
7476 | if (obj->udata && |
7477 | nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata)) | |
7478 | goto nla_put_failure; | |
7479 | ||
e5009240 PNA |
7480 | nlmsg_end(skb, nlh); |
7481 | return 0; | |
7482 | ||
7483 | nla_put_failure: | |
7484 | nlmsg_trim(skb, nlh); | |
7485 | return -1; | |
7486 | } | |
7487 | ||
a9fea2a3 | 7488 | struct nft_obj_filter { |
e46abbcc | 7489 | char *table; |
a9fea2a3 PNA |
7490 | u32 type; |
7491 | }; | |
7492 | ||
e5009240 PNA |
7493 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
7494 | { | |
7495 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 7496 | const struct nft_table *table; |
e5009240 | 7497 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 7498 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
7499 | struct net *net = sock_net(skb->sk); |
7500 | int family = nfmsg->nfgen_family; | |
0854db2a | 7501 | struct nftables_pernet *nft_net; |
43da04a5 PNA |
7502 | struct nft_object *obj; |
7503 | bool reset = false; | |
7504 | ||
7505 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
7506 | reset = true; | |
e5009240 PNA |
7507 | |
7508 | rcu_read_lock(); | |
d59d2f82 | 7509 | nft_net = nft_pernet(net); |
34002783 | 7510 | cb->seq = READ_ONCE(nft_net->base_seq); |
e5009240 | 7511 | |
0854db2a | 7512 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 7513 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
7514 | continue; |
7515 | ||
36596dad PNA |
7516 | list_for_each_entry_rcu(obj, &table->objects, list) { |
7517 | if (!nft_is_active(net, obj)) | |
7518 | goto cont; | |
7519 | if (idx < s_idx) | |
7520 | goto cont; | |
7521 | if (idx > s_idx) | |
7522 | memset(&cb->args[1], 0, | |
7523 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7524 | if (filter && filter->table && |
36596dad PNA |
7525 | strcmp(filter->table, table->name)) |
7526 | goto cont; | |
7527 | if (filter && | |
7528 | filter->type != NFT_OBJECT_UNSPEC && | |
7529 | obj->ops->type->type != filter->type) | |
7530 | goto cont; | |
8e6cf365 | 7531 | if (reset) { |
68df2ed5 | 7532 | char *buf = kasprintf(GFP_ATOMIC, |
c520292f | 7533 | "%s:%u", |
8e6cf365 | 7534 | table->name, |
0854db2a | 7535 | nft_net->base_seq); |
8e6cf365 RGB |
7536 | |
7537 | audit_log_nfcfg(buf, | |
7538 | family, | |
7539 | obj->handle, | |
14224039 | 7540 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7541 | GFP_ATOMIC); |
8e6cf365 RGB |
7542 | kfree(buf); |
7543 | } | |
7544 | ||
36596dad PNA |
7545 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
7546 | cb->nlh->nlmsg_seq, | |
7547 | NFT_MSG_NEWOBJ, | |
7548 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 7549 | table->family, table, |
36596dad PNA |
7550 | obj, reset) < 0) |
7551 | goto done; | |
e5009240 | 7552 | |
36596dad | 7553 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 7554 | cont: |
36596dad | 7555 | idx++; |
e5009240 PNA |
7556 | } |
7557 | } | |
7558 | done: | |
7559 | rcu_read_unlock(); | |
7560 | ||
7561 | cb->args[0] = idx; | |
7562 | return skb->len; | |
7563 | } | |
7564 | ||
90fd131a | 7565 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 7566 | { |
90fd131a FW |
7567 | const struct nlattr * const *nla = cb->data; |
7568 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 7569 | |
90fd131a FW |
7570 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
7571 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7572 | if (!filter) | |
7573 | return -ENOMEM; | |
7574 | ||
7575 | if (nla[NFTA_OBJ_TABLE]) { | |
7576 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
7577 | if (!filter->table) { | |
7578 | kfree(filter); | |
7579 | return -ENOMEM; | |
7580 | } | |
7581 | } | |
7582 | ||
7583 | if (nla[NFTA_OBJ_TYPE]) | |
7584 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 7585 | } |
a9fea2a3 | 7586 | |
90fd131a | 7587 | cb->data = filter; |
a9fea2a3 PNA |
7588 | return 0; |
7589 | } | |
7590 | ||
90fd131a | 7591 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 7592 | { |
90fd131a | 7593 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 7594 | |
90fd131a FW |
7595 | if (filter) { |
7596 | kfree(filter->table); | |
7597 | kfree(filter); | |
e46abbcc | 7598 | } |
a9fea2a3 | 7599 | |
90fd131a | 7600 | return 0; |
a9fea2a3 PNA |
7601 | } |
7602 | ||
d9adf22a | 7603 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7604 | static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info, |
7605 | const struct nlattr * const nla[]) | |
e5009240 | 7606 | { |
797d4980 PNA |
7607 | struct netlink_ext_ack *extack = info->extack; |
7608 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 7609 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7610 | const struct nft_table *table; |
797d4980 | 7611 | struct net *net = info->net; |
e5009240 PNA |
7612 | struct nft_object *obj; |
7613 | struct sk_buff *skb2; | |
43da04a5 | 7614 | bool reset = false; |
e5009240 PNA |
7615 | u32 objtype; |
7616 | int err; | |
7617 | ||
797d4980 | 7618 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
e5009240 | 7619 | struct netlink_dump_control c = { |
90fd131a | 7620 | .start = nf_tables_dump_obj_start, |
e5009240 | 7621 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 7622 | .done = nf_tables_dump_obj_done, |
d9adf22a | 7623 | .module = THIS_MODULE, |
90fd131a | 7624 | .data = (void *)nla, |
e5009240 | 7625 | }; |
a9fea2a3 | 7626 | |
797d4980 | 7627 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
e5009240 PNA |
7628 | } |
7629 | ||
7630 | if (!nla[NFTA_OBJ_NAME] || | |
7631 | !nla[NFTA_OBJ_TYPE]) | |
7632 | return -EINVAL; | |
7633 | ||
6001a930 | 7634 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0); |
36dd1bcc PNA |
7635 | if (IS_ERR(table)) { |
7636 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7637 | return PTR_ERR(table); |
36dd1bcc | 7638 | } |
e5009240 PNA |
7639 | |
7640 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7641 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
7642 | if (IS_ERR(obj)) { |
7643 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7644 | return PTR_ERR(obj); |
36dd1bcc | 7645 | } |
e5009240 | 7646 | |
d9adf22a | 7647 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
7648 | if (!skb2) |
7649 | return -ENOMEM; | |
7650 | ||
797d4980 | 7651 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
43da04a5 PNA |
7652 | reset = true; |
7653 | ||
8e6cf365 | 7654 | if (reset) { |
0854db2a FW |
7655 | const struct nftables_pernet *nft_net; |
7656 | char *buf; | |
7657 | ||
d59d2f82 | 7658 | nft_net = nft_pernet(net); |
0854db2a | 7659 | buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq); |
8e6cf365 RGB |
7660 | |
7661 | audit_log_nfcfg(buf, | |
7662 | family, | |
7663 | obj->handle, | |
14224039 | 7664 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7665 | GFP_ATOMIC); |
8e6cf365 RGB |
7666 | kfree(buf); |
7667 | } | |
7668 | ||
e5009240 | 7669 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 7670 | info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, |
43da04a5 | 7671 | family, table, obj, reset); |
e5009240 | 7672 | if (err < 0) |
ee921183 | 7673 | goto err_fill_obj_info; |
e5009240 | 7674 | |
ee921183 PNA |
7675 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
7676 | ||
7677 | err_fill_obj_info: | |
e5009240 PNA |
7678 | kfree_skb(skb2); |
7679 | return err; | |
e5009240 PNA |
7680 | } |
7681 | ||
00bfb320 | 7682 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 7683 | { |
dfc46034 | 7684 | if (obj->ops->destroy) |
00bfb320 | 7685 | obj->ops->destroy(ctx, obj); |
e5009240 | 7686 | |
dfc46034 | 7687 | module_put(obj->ops->type->owner); |
d152159b | 7688 | kfree(obj->key.name); |
bc7a7082 | 7689 | kfree(obj->udata); |
e5009240 PNA |
7690 | kfree(obj); |
7691 | } | |
7692 | ||
7dab8ee3 PNA |
7693 | static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info, |
7694 | const struct nlattr * const nla[]) | |
e5009240 | 7695 | { |
7dab8ee3 PNA |
7696 | struct netlink_ext_ack *extack = info->extack; |
7697 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7698 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 7699 | struct net *net = info->net; |
36dd1bcc | 7700 | const struct nlattr *attr; |
e5009240 PNA |
7701 | struct nft_table *table; |
7702 | struct nft_object *obj; | |
7703 | struct nft_ctx ctx; | |
7704 | u32 objtype; | |
7705 | ||
7706 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 7707 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
7708 | return -EINVAL; |
7709 | ||
6001a930 PNA |
7710 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7711 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7712 | if (IS_ERR(table)) { |
7713 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7714 | return PTR_ERR(table); |
36dd1bcc | 7715 | } |
e5009240 PNA |
7716 | |
7717 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
7718 | if (nla[NFTA_OBJ_HANDLE]) { |
7719 | attr = nla[NFTA_OBJ_HANDLE]; | |
7720 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
7721 | } else { | |
7722 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 7723 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
7724 | } |
7725 | ||
7726 | if (IS_ERR(obj)) { | |
f80a612d FFM |
7727 | if (PTR_ERR(obj) == -ENOENT && |
7728 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYOBJ) | |
7729 | return 0; | |
7730 | ||
36dd1bcc | 7731 | NL_SET_BAD_ATTR(extack, attr); |
e5009240 | 7732 | return PTR_ERR(obj); |
36dd1bcc PNA |
7733 | } |
7734 | if (obj->use > 0) { | |
7735 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7736 | return -EBUSY; |
36dd1bcc | 7737 | } |
e5009240 | 7738 | |
7dab8ee3 | 7739 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 PNA |
7740 | |
7741 | return nft_delobj(&ctx, obj); | |
7742 | } | |
7743 | ||
d152159b | 7744 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 | 7745 | struct nft_object *obj, u32 portid, u32 seq, int event, |
6fb721cf | 7746 | u16 flags, int family, int report, gfp_t gfp) |
e5009240 | 7747 | { |
d59d2f82 | 7748 | struct nftables_pernet *nft_net = nft_pernet(net); |
e5009240 PNA |
7749 | struct sk_buff *skb; |
7750 | int err; | |
c520292f | 7751 | char *buf = kasprintf(gfp, "%s:%u", |
0854db2a | 7752 | table->name, nft_net->base_seq); |
8e6cf365 RGB |
7753 | |
7754 | audit_log_nfcfg(buf, | |
7755 | family, | |
7756 | obj->handle, | |
7757 | event == NFT_MSG_NEWOBJ ? | |
c520292f RGB |
7758 | AUDIT_NFT_OP_OBJ_REGISTER : |
7759 | AUDIT_NFT_OP_OBJ_UNREGISTER, | |
68df2ed5 | 7760 | gfp); |
8e6cf365 | 7761 | kfree(buf); |
e5009240 | 7762 | |
2599e989 PNA |
7763 | if (!report && |
7764 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 7765 | return; |
e5009240 | 7766 | |
2599e989 | 7767 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
7768 | if (skb == NULL) |
7769 | goto err; | |
7770 | ||
6fb721cf PNA |
7771 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, |
7772 | flags & (NLM_F_CREATE | NLM_F_EXCL), | |
7773 | family, table, obj, false); | |
e5009240 PNA |
7774 | if (err < 0) { |
7775 | kfree_skb(skb); | |
7776 | goto err; | |
7777 | } | |
7778 | ||
0854db2a | 7779 | nft_notify_enqueue(skb, report, &nft_net->notify_list); |
25e94a99 | 7780 | return; |
e5009240 | 7781 | err: |
25e94a99 | 7782 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 7783 | } |
2599e989 PNA |
7784 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
7785 | ||
25e94a99 PNA |
7786 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
7787 | struct nft_object *obj, int event) | |
2599e989 | 7788 | { |
25e94a99 | 7789 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
6fb721cf | 7790 | ctx->flags, ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 7791 | } |
e5009240 | 7792 | |
3b49e2e9 PNA |
7793 | /* |
7794 | * Flow tables | |
7795 | */ | |
7796 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
7797 | { | |
7798 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7799 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
7800 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7801 | } | |
7802 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
7803 | ||
7804 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
7805 | { | |
7806 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7807 | list_del_rcu(&type->list); | |
7808 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7809 | } | |
7810 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
7811 | ||
7812 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
7813 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
7814 | .len = NFT_NAME_MAXLEN - 1 }, | |
7815 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
7816 | .len = NFT_NAME_MAXLEN - 1 }, | |
7817 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7818 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 7819 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
7820 | }; |
7821 | ||
cac20fcd PNA |
7822 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
7823 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
7824 | { |
7825 | struct nft_flowtable *flowtable; | |
7826 | ||
d9adf22a | 7827 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
7828 | if (!nla_strcmp(nla, flowtable->name) && |
7829 | nft_active_genmask(flowtable, genmask)) | |
7830 | return flowtable; | |
7831 | } | |
7832 | return ERR_PTR(-ENOENT); | |
7833 | } | |
cac20fcd | 7834 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 7835 | |
9b05b6e1 LGL |
7836 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
7837 | struct nft_flowtable *flowtable, | |
7838 | enum nft_trans_phase phase) | |
7839 | { | |
7840 | switch (phase) { | |
26b5a571 | 7841 | case NFT_TRANS_PREPARE_ERROR: |
9b05b6e1 LGL |
7842 | case NFT_TRANS_PREPARE: |
7843 | case NFT_TRANS_ABORT: | |
7844 | case NFT_TRANS_RELEASE: | |
7845 | flowtable->use--; | |
954d8297 | 7846 | fallthrough; |
9b05b6e1 LGL |
7847 | default: |
7848 | return; | |
7849 | } | |
7850 | } | |
7851 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
7852 | ||
ae0662f8 | 7853 | static struct nft_flowtable * |
cac20fcd PNA |
7854 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
7855 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
7856 | { |
7857 | struct nft_flowtable *flowtable; | |
7858 | ||
7859 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
7860 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
7861 | nft_active_genmask(flowtable, genmask)) | |
7862 | return flowtable; | |
7863 | } | |
7864 | return ERR_PTR(-ENOENT); | |
7865 | } | |
7866 | ||
d9246a53 PNA |
7867 | struct nft_flowtable_hook { |
7868 | u32 num; | |
7869 | int priority; | |
7870 | struct list_head list; | |
7871 | }; | |
7872 | ||
3b49e2e9 PNA |
7873 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
7874 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
7875 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
7876 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
7877 | }; | |
7878 | ||
d9246a53 | 7879 | static int nft_flowtable_parse_hook(const struct nft_ctx *ctx, |
8509f62b | 7880 | const struct nlattr * const nla[], |
d9246a53 | 7881 | struct nft_flowtable_hook *flowtable_hook, |
c3c060ad PNA |
7882 | struct nft_flowtable *flowtable, |
7883 | struct netlink_ext_ack *extack, bool add) | |
3b49e2e9 | 7884 | { |
3b49e2e9 | 7885 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 7886 | struct nft_hook *hook; |
3b49e2e9 | 7887 | int hooknum, priority; |
3f0465a9 | 7888 | int err; |
3b49e2e9 | 7889 | |
d9246a53 PNA |
7890 | INIT_LIST_HEAD(&flowtable_hook->list); |
7891 | ||
8509f62b PNA |
7892 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, |
7893 | nla[NFTA_FLOWTABLE_HOOK], | |
8cb08174 | 7894 | nft_flowtable_hook_policy, NULL); |
3b49e2e9 PNA |
7895 | if (err < 0) |
7896 | return err; | |
7897 | ||
5b6743fb PNA |
7898 | if (add) { |
7899 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
8509f62b PNA |
7900 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { |
7901 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
7902 | return -ENOENT; | |
7903 | } | |
3b49e2e9 | 7904 | |
5b6743fb PNA |
7905 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); |
7906 | if (hooknum != NF_NETDEV_INGRESS) | |
7907 | return -EOPNOTSUPP; | |
7908 | ||
7909 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7910 | ||
7911 | flowtable_hook->priority = priority; | |
7912 | flowtable_hook->num = hooknum; | |
7913 | } else { | |
7914 | if (tb[NFTA_FLOWTABLE_HOOK_NUM]) { | |
7915 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
7916 | if (hooknum != flowtable->hooknum) | |
7917 | return -EOPNOTSUPP; | |
7918 | } | |
7919 | ||
7920 | if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { | |
7921 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7922 | if (priority != flowtable->data.priority) | |
7923 | return -EOPNOTSUPP; | |
7924 | } | |
3b49e2e9 | 7925 | |
5b6743fb PNA |
7926 | flowtable_hook->priority = flowtable->data.priority; |
7927 | flowtable_hook->num = flowtable->hooknum; | |
7928 | } | |
3b49e2e9 | 7929 | |
05abe445 PNA |
7930 | if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) { |
7931 | err = nf_tables_parse_netdev_hooks(ctx->net, | |
7932 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
c3c060ad PNA |
7933 | &flowtable_hook->list, |
7934 | extack); | |
05abe445 PNA |
7935 | if (err < 0) |
7936 | return err; | |
7937 | } | |
3b49e2e9 | 7938 | |
d9246a53 | 7939 | list_for_each_entry(hook, &flowtable_hook->list, list) { |
3f0465a9 | 7940 | hook->ops.pf = NFPROTO_NETDEV; |
5b6743fb PNA |
7941 | hook->ops.hooknum = flowtable_hook->num; |
7942 | hook->ops.priority = flowtable_hook->priority; | |
7943 | hook->ops.priv = &flowtable->data; | |
7944 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
7945 | } |
7946 | ||
3b49e2e9 PNA |
7947 | return err; |
7948 | } | |
7949 | ||
98319cb9 | 7950 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
7951 | { |
7952 | const struct nf_flowtable_type *type; | |
7953 | ||
7954 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 7955 | if (family == type->family) |
3b49e2e9 PNA |
7956 | return type; |
7957 | } | |
7958 | return NULL; | |
7959 | } | |
7960 | ||
452238e8 FW |
7961 | static const struct nf_flowtable_type * |
7962 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
7963 | { |
7964 | const struct nf_flowtable_type *type; | |
7965 | ||
98319cb9 | 7966 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
7967 | if (type != NULL && try_module_get(type->owner)) |
7968 | return type; | |
7969 | ||
f102d66b | 7970 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
7971 | #ifdef CONFIG_MODULES |
7972 | if (type == NULL) { | |
eb014de4 | 7973 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
7974 | return ERR_PTR(-EAGAIN); |
7975 | } | |
7976 | #endif | |
7977 | return ERR_PTR(-ENOENT); | |
7978 | } | |
7979 | ||
5acab914 | 7980 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
7981 | static void nft_unregister_flowtable_hook(struct net *net, |
7982 | struct nft_flowtable *flowtable, | |
7983 | struct nft_hook *hook) | |
7984 | { | |
7985 | nf_unregister_net_hook(net, &hook->ops); | |
7986 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
7987 | FLOW_BLOCK_UNBIND); | |
7988 | } | |
7989 | ||
f9a43007 PNA |
7990 | static void __nft_unregister_flowtable_net_hooks(struct net *net, |
7991 | struct list_head *hook_list, | |
7992 | bool release_netdev) | |
3b49e2e9 | 7993 | { |
f9a43007 | 7994 | struct nft_hook *hook, *next; |
3b49e2e9 | 7995 | |
f9a43007 | 7996 | list_for_each_entry_safe(hook, next, hook_list, list) { |
5acab914 | 7997 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
7998 | if (release_netdev) { |
7999 | list_del(&hook->list); | |
ab5e5c06 | 8000 | kfree_rcu(hook, rcu); |
f9a43007 PNA |
8001 | } |
8002 | } | |
8003 | } | |
8004 | ||
8005 | static void nft_unregister_flowtable_net_hooks(struct net *net, | |
8006 | struct list_head *hook_list) | |
8007 | { | |
8008 | __nft_unregister_flowtable_net_hooks(net, hook_list, false); | |
3f0465a9 PNA |
8009 | } |
8010 | ||
8011 | static int nft_register_flowtable_net_hooks(struct net *net, | |
8012 | struct nft_table *table, | |
f9382669 | 8013 | struct list_head *hook_list, |
3f0465a9 PNA |
8014 | struct nft_flowtable *flowtable) |
8015 | { | |
8016 | struct nft_hook *hook, *hook2, *next; | |
8017 | struct nft_flowtable *ft; | |
8018 | int err, i = 0; | |
8019 | ||
f9382669 | 8020 | list_for_each_entry(hook, hook_list, list) { |
3f0465a9 | 8021 | list_for_each_entry(ft, &table->flowtables, list) { |
86fe2c19 PNA |
8022 | if (!nft_is_active_next(net, ft)) |
8023 | continue; | |
8024 | ||
3f0465a9 PNA |
8025 | list_for_each_entry(hook2, &ft->hook_list, list) { |
8026 | if (hook->ops.dev == hook2->ops.dev && | |
8027 | hook->ops.pf == hook2->ops.pf) { | |
77a92189 | 8028 | err = -EEXIST; |
3f0465a9 PNA |
8029 | goto err_unregister_net_hooks; |
8030 | } | |
8031 | } | |
8032 | } | |
3b49e2e9 | 8033 | |
d7c03a9f | 8034 | err = flowtable->data.type->setup(&flowtable->data, |
8035 | hook->ops.dev, | |
8036 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
8037 | if (err < 0) |
8038 | goto err_unregister_net_hooks; | |
8039 | ||
d7c03a9f | 8040 | err = nf_register_net_hook(net, &hook->ops); |
8041 | if (err < 0) { | |
8042 | flowtable->data.type->setup(&flowtable->data, | |
8043 | hook->ops.dev, | |
8044 | FLOW_BLOCK_UNBIND); | |
8045 | goto err_unregister_net_hooks; | |
8046 | } | |
8047 | ||
3f0465a9 | 8048 | i++; |
3b49e2e9 | 8049 | } |
3f0465a9 PNA |
8050 | |
8051 | return 0; | |
8052 | ||
8053 | err_unregister_net_hooks: | |
f9382669 | 8054 | list_for_each_entry_safe(hook, next, hook_list, list) { |
3f0465a9 PNA |
8055 | if (i-- <= 0) |
8056 | break; | |
8057 | ||
ff4bf2f4 | 8058 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
8059 | list_del_rcu(&hook->list); |
8060 | kfree_rcu(hook, rcu); | |
8061 | } | |
8062 | ||
8063 | return err; | |
3b49e2e9 PNA |
8064 | } |
8065 | ||
cdc32546 | 8066 | static void nft_hooks_destroy(struct list_head *hook_list) |
389a2cbc PNA |
8067 | { |
8068 | struct nft_hook *hook, *next; | |
8069 | ||
8070 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
8071 | list_del_rcu(&hook->list); | |
8072 | kfree_rcu(hook, rcu); | |
8073 | } | |
8074 | } | |
8075 | ||
78d9f48f | 8076 | static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, |
c3c060ad PNA |
8077 | struct nft_flowtable *flowtable, |
8078 | struct netlink_ext_ack *extack) | |
78d9f48f PNA |
8079 | { |
8080 | const struct nlattr * const *nla = ctx->nla; | |
8081 | struct nft_flowtable_hook flowtable_hook; | |
8082 | struct nft_hook *hook, *next; | |
8083 | struct nft_trans *trans; | |
8084 | bool unregister = false; | |
7b35582c | 8085 | u32 flags; |
78d9f48f PNA |
8086 | int err; |
8087 | ||
8509f62b PNA |
8088 | err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable, |
8089 | extack, false); | |
78d9f48f PNA |
8090 | if (err < 0) |
8091 | return err; | |
8092 | ||
8093 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
8094 | if (nft_hook_list_find(&flowtable->hook_list, hook)) { | |
8095 | list_del(&hook->list); | |
8096 | kfree(hook); | |
8097 | } | |
8098 | } | |
8099 | ||
7b35582c PNA |
8100 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
8101 | flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
c271cc9f PNA |
8102 | if (flags & ~NFT_FLOWTABLE_MASK) { |
8103 | err = -EOPNOTSUPP; | |
8104 | goto err_flowtable_update_hook; | |
8105 | } | |
7b35582c | 8106 | if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^ |
c271cc9f PNA |
8107 | (flags & NFT_FLOWTABLE_HW_OFFLOAD)) { |
8108 | err = -EOPNOTSUPP; | |
8109 | goto err_flowtable_update_hook; | |
8110 | } | |
7b35582c PNA |
8111 | } else { |
8112 | flags = flowtable->data.flags; | |
8113 | } | |
8114 | ||
78d9f48f PNA |
8115 | err = nft_register_flowtable_net_hooks(ctx->net, ctx->table, |
8116 | &flowtable_hook.list, flowtable); | |
8117 | if (err < 0) | |
8118 | goto err_flowtable_update_hook; | |
8119 | ||
8120 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE, | |
8121 | sizeof(struct nft_trans_flowtable)); | |
8122 | if (!trans) { | |
8123 | unregister = true; | |
8124 | err = -ENOMEM; | |
8125 | goto err_flowtable_update_hook; | |
8126 | } | |
8127 | ||
7b35582c | 8128 | nft_trans_flowtable_flags(trans) = flags; |
78d9f48f PNA |
8129 | nft_trans_flowtable(trans) = flowtable; |
8130 | nft_trans_flowtable_update(trans) = true; | |
8131 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
8132 | list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans)); | |
8133 | ||
0854db2a | 8134 | nft_trans_commit_list_add_tail(ctx->net, trans); |
78d9f48f PNA |
8135 | |
8136 | return 0; | |
8137 | ||
8138 | err_flowtable_update_hook: | |
8139 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
8140 | if (unregister) | |
8141 | nft_unregister_flowtable_hook(ctx->net, flowtable, hook); | |
8142 | list_del_rcu(&hook->list); | |
8143 | kfree_rcu(hook, rcu); | |
8144 | } | |
8145 | ||
8146 | return err; | |
8147 | ||
8148 | } | |
8149 | ||
7dab8ee3 PNA |
8150 | static int nf_tables_newflowtable(struct sk_buff *skb, |
8151 | const struct nfnl_info *info, | |
8152 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8153 | { |
7dab8ee3 | 8154 | struct netlink_ext_ack *extack = info->extack; |
d9246a53 | 8155 | struct nft_flowtable_hook flowtable_hook; |
7dab8ee3 | 8156 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 8157 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8158 | const struct nf_flowtable_type *type; |
3f0465a9 PNA |
8159 | struct nft_flowtable *flowtable; |
8160 | struct nft_hook *hook, *next; | |
7dab8ee3 | 8161 | struct net *net = info->net; |
3b49e2e9 PNA |
8162 | struct nft_table *table; |
8163 | struct nft_ctx ctx; | |
3f0465a9 | 8164 | int err; |
3b49e2e9 PNA |
8165 | |
8166 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
8167 | !nla[NFTA_FLOWTABLE_NAME] || | |
8168 | !nla[NFTA_FLOWTABLE_HOOK]) | |
8169 | return -EINVAL; | |
8170 | ||
cac20fcd | 8171 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8172 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
8173 | if (IS_ERR(table)) { |
8174 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 8175 | return PTR_ERR(table); |
36dd1bcc | 8176 | } |
3b49e2e9 | 8177 | |
cac20fcd PNA |
8178 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
8179 | genmask); | |
3b49e2e9 PNA |
8180 | if (IS_ERR(flowtable)) { |
8181 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
8182 | if (err != -ENOENT) { |
8183 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 8184 | return err; |
36dd1bcc | 8185 | } |
3b49e2e9 | 8186 | } else { |
7dab8ee3 | 8187 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 8188 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); |
3b49e2e9 | 8189 | return -EEXIST; |
36dd1bcc | 8190 | } |
3b49e2e9 | 8191 | |
7dab8ee3 | 8192 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
78d9f48f | 8193 | |
c3c060ad | 8194 | return nft_flowtable_update(&ctx, info->nlh, flowtable, extack); |
3b49e2e9 PNA |
8195 | } |
8196 | ||
7dab8ee3 | 8197 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
3b49e2e9 | 8198 | |
33758c89 | 8199 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
8200 | if (!flowtable) |
8201 | return -ENOMEM; | |
8202 | ||
8203 | flowtable->table = table; | |
3ecbfd65 | 8204 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 8205 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 8206 | |
33758c89 | 8207 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
8208 | if (!flowtable->name) { |
8209 | err = -ENOMEM; | |
8210 | goto err1; | |
8211 | } | |
8212 | ||
452238e8 | 8213 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
8214 | if (IS_ERR(type)) { |
8215 | err = PTR_ERR(type); | |
8216 | goto err2; | |
8217 | } | |
8218 | ||
8bb69f3b PNA |
8219 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
8220 | flowtable->data.flags = | |
8221 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
7e6136f1 PNA |
8222 | if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) { |
8223 | err = -EOPNOTSUPP; | |
8bb69f3b | 8224 | goto err3; |
7e6136f1 | 8225 | } |
8bb69f3b PNA |
8226 | } |
8227 | ||
8228 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 8229 | flowtable->data.type = type; |
a268de77 | 8230 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
8231 | if (err < 0) |
8232 | goto err3; | |
8233 | ||
8509f62b PNA |
8234 | err = nft_flowtable_parse_hook(&ctx, nla, &flowtable_hook, flowtable, |
8235 | extack, true); | |
3b49e2e9 | 8236 | if (err < 0) |
a268de77 | 8237 | goto err4; |
3b49e2e9 | 8238 | |
d9246a53 PNA |
8239 | list_splice(&flowtable_hook.list, &flowtable->hook_list); |
8240 | flowtable->data.priority = flowtable_hook.priority; | |
8241 | flowtable->hooknum = flowtable_hook.num; | |
8242 | ||
f9382669 PNA |
8243 | err = nft_register_flowtable_net_hooks(ctx.net, table, |
8244 | &flowtable->hook_list, | |
8245 | flowtable); | |
2d285f26 | 8246 | if (err < 0) { |
cdc32546 | 8247 | nft_hooks_destroy(&flowtable->hook_list); |
3f0465a9 | 8248 | goto err4; |
2d285f26 | 8249 | } |
3b49e2e9 PNA |
8250 | |
8251 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
8252 | if (err < 0) | |
3f0465a9 | 8253 | goto err5; |
3b49e2e9 PNA |
8254 | |
8255 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
8256 | table->use++; | |
8257 | ||
8258 | return 0; | |
a268de77 | 8259 | err5: |
3f0465a9 | 8260 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 8261 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
8262 | list_del_rcu(&hook->list); |
8263 | kfree_rcu(hook, rcu); | |
8264 | } | |
a268de77 FF |
8265 | err4: |
8266 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
8267 | err3: |
8268 | module_put(type->owner); | |
8269 | err2: | |
8270 | kfree(flowtable->name); | |
8271 | err1: | |
8272 | kfree(flowtable); | |
8273 | return err; | |
8274 | } | |
8275 | ||
3003055f PNA |
8276 | static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook) |
8277 | { | |
8278 | struct nft_hook *this, *next; | |
8279 | ||
8280 | list_for_each_entry_safe(this, next, &flowtable_hook->list, list) { | |
8281 | list_del(&this->list); | |
8282 | kfree(this); | |
8283 | } | |
8284 | } | |
8285 | ||
abadb2f8 | 8286 | static int nft_delflowtable_hook(struct nft_ctx *ctx, |
c3c060ad PNA |
8287 | struct nft_flowtable *flowtable, |
8288 | struct netlink_ext_ack *extack) | |
abadb2f8 PNA |
8289 | { |
8290 | const struct nlattr * const *nla = ctx->nla; | |
8291 | struct nft_flowtable_hook flowtable_hook; | |
b6d9014a | 8292 | LIST_HEAD(flowtable_del_list); |
3003055f | 8293 | struct nft_hook *this, *hook; |
abadb2f8 PNA |
8294 | struct nft_trans *trans; |
8295 | int err; | |
8296 | ||
8509f62b PNA |
8297 | err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable, |
8298 | extack, false); | |
abadb2f8 PNA |
8299 | if (err < 0) |
8300 | return err; | |
8301 | ||
3003055f | 8302 | list_for_each_entry(this, &flowtable_hook.list, list) { |
abadb2f8 PNA |
8303 | hook = nft_hook_list_find(&flowtable->hook_list, this); |
8304 | if (!hook) { | |
8305 | err = -ENOENT; | |
8306 | goto err_flowtable_del_hook; | |
8307 | } | |
b6d9014a | 8308 | list_move(&hook->list, &flowtable_del_list); |
abadb2f8 PNA |
8309 | } |
8310 | ||
8311 | trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE, | |
8312 | sizeof(struct nft_trans_flowtable)); | |
3003055f PNA |
8313 | if (!trans) { |
8314 | err = -ENOMEM; | |
8315 | goto err_flowtable_del_hook; | |
8316 | } | |
abadb2f8 PNA |
8317 | |
8318 | nft_trans_flowtable(trans) = flowtable; | |
8319 | nft_trans_flowtable_update(trans) = true; | |
8320 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
b6d9014a | 8321 | list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans)); |
3003055f | 8322 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 | 8323 | |
0854db2a | 8324 | nft_trans_commit_list_add_tail(ctx->net, trans); |
abadb2f8 PNA |
8325 | |
8326 | return 0; | |
8327 | ||
8328 | err_flowtable_del_hook: | |
b6d9014a | 8329 | list_splice(&flowtable_del_list, &flowtable->hook_list); |
3003055f | 8330 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 PNA |
8331 | |
8332 | return err; | |
8333 | } | |
8334 | ||
7dab8ee3 PNA |
8335 | static int nf_tables_delflowtable(struct sk_buff *skb, |
8336 | const struct nfnl_info *info, | |
8337 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8338 | { |
7dab8ee3 PNA |
8339 | struct netlink_ext_ack *extack = info->extack; |
8340 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 8341 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8342 | struct nft_flowtable *flowtable; |
7dab8ee3 | 8343 | struct net *net = info->net; |
36dd1bcc | 8344 | const struct nlattr *attr; |
3b49e2e9 PNA |
8345 | struct nft_table *table; |
8346 | struct nft_ctx ctx; | |
8347 | ||
e603ea4b PNA |
8348 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
8349 | (!nla[NFTA_FLOWTABLE_NAME] && | |
8350 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
8351 | return -EINVAL; | |
8352 | ||
cac20fcd | 8353 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8354 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
8355 | if (IS_ERR(table)) { |
8356 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 8357 | return PTR_ERR(table); |
36dd1bcc | 8358 | } |
3b49e2e9 | 8359 | |
36dd1bcc PNA |
8360 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
8361 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
8362 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
8363 | } else { | |
8364 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
8365 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
8366 | } | |
8367 | ||
8368 | if (IS_ERR(flowtable)) { | |
f80a612d FFM |
8369 | if (PTR_ERR(flowtable) == -ENOENT && |
8370 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYFLOWTABLE) | |
8371 | return 0; | |
8372 | ||
36dd1bcc PNA |
8373 | NL_SET_BAD_ATTR(extack, attr); |
8374 | return PTR_ERR(flowtable); | |
8375 | } | |
abadb2f8 | 8376 | |
7dab8ee3 | 8377 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
abadb2f8 PNA |
8378 | |
8379 | if (nla[NFTA_FLOWTABLE_HOOK]) | |
c3c060ad | 8380 | return nft_delflowtable_hook(&ctx, flowtable, extack); |
abadb2f8 | 8381 | |
36dd1bcc PNA |
8382 | if (flowtable->use > 0) { |
8383 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 8384 | return -EBUSY; |
36dd1bcc | 8385 | } |
3b49e2e9 | 8386 | |
3b49e2e9 PNA |
8387 | return nft_delflowtable(&ctx, flowtable); |
8388 | } | |
8389 | ||
8390 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
8391 | u32 portid, u32 seq, int event, | |
8392 | u32 flags, int family, | |
c42d8bda PNA |
8393 | struct nft_flowtable *flowtable, |
8394 | struct list_head *hook_list) | |
3b49e2e9 PNA |
8395 | { |
8396 | struct nlattr *nest, *nest_devs; | |
3f0465a9 | 8397 | struct nft_hook *hook; |
3b49e2e9 | 8398 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
8399 | |
8400 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
19c28b13 PNA |
8401 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
8402 | NFNETLINK_V0, nft_base_seq(net)); | |
8403 | if (!nlh) | |
3b49e2e9 PNA |
8404 | goto nla_put_failure; |
8405 | ||
3b49e2e9 PNA |
8406 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || |
8407 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 | 8408 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), |
28339b21 PNA |
8409 | NFTA_FLOWTABLE_PAD)) |
8410 | goto nla_put_failure; | |
8411 | ||
8412 | if (event == NFT_MSG_DELFLOWTABLE && !hook_list) { | |
8413 | nlmsg_end(skb, nlh); | |
8414 | return 0; | |
8415 | } | |
8416 | ||
8417 | if (nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || | |
8bb69f3b | 8418 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) |
3b49e2e9 PNA |
8419 | goto nla_put_failure; |
8420 | ||
ae0be8de | 8421 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
8422 | if (!nest) |
8423 | goto nla_put_failure; | |
3b49e2e9 | 8424 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 8425 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
8426 | goto nla_put_failure; |
8427 | ||
ae0be8de | 8428 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
8429 | if (!nest_devs) |
8430 | goto nla_put_failure; | |
8431 | ||
28339b21 PNA |
8432 | if (!hook_list) |
8433 | hook_list = &flowtable->hook_list; | |
8434 | ||
c42d8bda | 8435 | list_for_each_entry_rcu(hook, hook_list, list) { |
3f0465a9 | 8436 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) |
3b49e2e9 PNA |
8437 | goto nla_put_failure; |
8438 | } | |
8439 | nla_nest_end(skb, nest_devs); | |
8440 | nla_nest_end(skb, nest); | |
8441 | ||
8442 | nlmsg_end(skb, nlh); | |
8443 | return 0; | |
8444 | ||
8445 | nla_put_failure: | |
8446 | nlmsg_trim(skb, nlh); | |
8447 | return -1; | |
8448 | } | |
8449 | ||
8450 | struct nft_flowtable_filter { | |
8451 | char *table; | |
8452 | }; | |
8453 | ||
8454 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
8455 | struct netlink_callback *cb) | |
8456 | { | |
8457 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
8458 | struct nft_flowtable_filter *filter = cb->data; | |
8459 | unsigned int idx = 0, s_idx = cb->args[0]; | |
8460 | struct net *net = sock_net(skb->sk); | |
8461 | int family = nfmsg->nfgen_family; | |
8462 | struct nft_flowtable *flowtable; | |
0854db2a | 8463 | struct nftables_pernet *nft_net; |
3b49e2e9 PNA |
8464 | const struct nft_table *table; |
8465 | ||
8466 | rcu_read_lock(); | |
d59d2f82 | 8467 | nft_net = nft_pernet(net); |
34002783 | 8468 | cb->seq = READ_ONCE(nft_net->base_seq); |
3b49e2e9 | 8469 | |
0854db2a | 8470 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 8471 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
8472 | continue; |
8473 | ||
36596dad PNA |
8474 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
8475 | if (!nft_is_active(net, flowtable)) | |
8476 | goto cont; | |
8477 | if (idx < s_idx) | |
8478 | goto cont; | |
8479 | if (idx > s_idx) | |
8480 | memset(&cb->args[1], 0, | |
8481 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 8482 | if (filter && filter->table && |
36596dad PNA |
8483 | strcmp(filter->table, table->name)) |
8484 | goto cont; | |
3b49e2e9 | 8485 | |
36596dad PNA |
8486 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
8487 | cb->nlh->nlmsg_seq, | |
8488 | NFT_MSG_NEWFLOWTABLE, | |
8489 | NLM_F_MULTI | NLM_F_APPEND, | |
c42d8bda | 8490 | table->family, |
28339b21 | 8491 | flowtable, NULL) < 0) |
36596dad | 8492 | goto done; |
3b49e2e9 | 8493 | |
36596dad | 8494 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 8495 | cont: |
36596dad | 8496 | idx++; |
3b49e2e9 PNA |
8497 | } |
8498 | } | |
8499 | done: | |
8500 | rcu_read_unlock(); | |
8501 | ||
8502 | cb->args[0] = idx; | |
8503 | return skb->len; | |
8504 | } | |
8505 | ||
90fd131a | 8506 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 8507 | { |
90fd131a FW |
8508 | const struct nlattr * const *nla = cb->data; |
8509 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 8510 | |
90fd131a FW |
8511 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
8512 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
8513 | if (!filter) | |
8514 | return -ENOMEM; | |
3b49e2e9 | 8515 | |
90fd131a FW |
8516 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
8517 | GFP_ATOMIC); | |
8518 | if (!filter->table) { | |
8519 | kfree(filter); | |
8520 | return -ENOMEM; | |
8521 | } | |
8522 | } | |
3b49e2e9 | 8523 | |
90fd131a | 8524 | cb->data = filter; |
3b49e2e9 PNA |
8525 | return 0; |
8526 | } | |
8527 | ||
90fd131a | 8528 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 8529 | { |
90fd131a | 8530 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 8531 | |
3b49e2e9 | 8532 | if (!filter) |
90fd131a | 8533 | return 0; |
3b49e2e9 | 8534 | |
90fd131a FW |
8535 | kfree(filter->table); |
8536 | kfree(filter); | |
8537 | ||
8538 | return 0; | |
3b49e2e9 PNA |
8539 | } |
8540 | ||
d9adf22a | 8541 | /* called with rcu_read_lock held */ |
797d4980 PNA |
8542 | static int nf_tables_getflowtable(struct sk_buff *skb, |
8543 | const struct nfnl_info *info, | |
8544 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8545 | { |
797d4980 | 8546 | u8 genmask = nft_genmask_cur(info->net); |
ef4b65e5 | 8547 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8548 | struct nft_flowtable *flowtable; |
3b49e2e9 | 8549 | const struct nft_table *table; |
797d4980 | 8550 | struct net *net = info->net; |
3b49e2e9 PNA |
8551 | struct sk_buff *skb2; |
8552 | int err; | |
8553 | ||
797d4980 | 8554 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
3b49e2e9 | 8555 | struct netlink_dump_control c = { |
90fd131a | 8556 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
8557 | .dump = nf_tables_dump_flowtable, |
8558 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 8559 | .module = THIS_MODULE, |
90fd131a | 8560 | .data = (void *)nla, |
3b49e2e9 PNA |
8561 | }; |
8562 | ||
797d4980 | 8563 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
3b49e2e9 PNA |
8564 | } |
8565 | ||
8566 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
8567 | return -EINVAL; | |
8568 | ||
cac20fcd | 8569 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8570 | genmask, 0); |
3b49e2e9 PNA |
8571 | if (IS_ERR(table)) |
8572 | return PTR_ERR(table); | |
8573 | ||
cac20fcd PNA |
8574 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
8575 | genmask); | |
03a0120f | 8576 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
8577 | return PTR_ERR(flowtable); |
8578 | ||
d9adf22a | 8579 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
8580 | if (!skb2) |
8581 | return -ENOMEM; | |
8582 | ||
8583 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
797d4980 | 8584 | info->nlh->nlmsg_seq, |
3b49e2e9 | 8585 | NFT_MSG_NEWFLOWTABLE, 0, family, |
28339b21 | 8586 | flowtable, NULL); |
3b49e2e9 | 8587 | if (err < 0) |
ee921183 | 8588 | goto err_fill_flowtable_info; |
3b49e2e9 | 8589 | |
ee921183 PNA |
8590 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
8591 | ||
8592 | err_fill_flowtable_info: | |
3b49e2e9 PNA |
8593 | kfree_skb(skb2); |
8594 | return err; | |
8595 | } | |
8596 | ||
8597 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
8598 | struct nft_flowtable *flowtable, | |
28339b21 | 8599 | struct list_head *hook_list, int event) |
3b49e2e9 | 8600 | { |
d59d2f82 | 8601 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
3b49e2e9 | 8602 | struct sk_buff *skb; |
6fb721cf | 8603 | u16 flags = 0; |
3b49e2e9 PNA |
8604 | int err; |
8605 | ||
dceababa | 8606 | if (!ctx->report && |
3b49e2e9 PNA |
8607 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
8608 | return; | |
8609 | ||
8610 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
8611 | if (skb == NULL) | |
8612 | goto err; | |
8613 | ||
6fb721cf PNA |
8614 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
8615 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
8616 | ||
3b49e2e9 | 8617 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, |
6fb721cf | 8618 | ctx->seq, event, flags, |
c42d8bda | 8619 | ctx->family, flowtable, hook_list); |
3b49e2e9 PNA |
8620 | if (err < 0) { |
8621 | kfree_skb(skb); | |
8622 | goto err; | |
8623 | } | |
8624 | ||
0854db2a | 8625 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
3b49e2e9 PNA |
8626 | return; |
8627 | err: | |
8628 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
8629 | } | |
8630 | ||
3b49e2e9 PNA |
8631 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
8632 | { | |
3f0465a9 PNA |
8633 | struct nft_hook *hook, *next; |
8634 | ||
5acab914 | 8635 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 8636 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
8637 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
8638 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
8639 | list_del_rcu(&hook->list); |
8640 | kfree(hook); | |
8641 | } | |
3b49e2e9 | 8642 | kfree(flowtable->name); |
3b49e2e9 | 8643 | module_put(flowtable->data.type->owner); |
a12486eb | 8644 | kfree(flowtable); |
3b49e2e9 PNA |
8645 | } |
8646 | ||
84d7fce6 PNA |
8647 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
8648 | u32 portid, u32 seq) | |
8649 | { | |
d59d2f82 | 8650 | struct nftables_pernet *nft_net = nft_pernet(net); |
84d7fce6 | 8651 | struct nlmsghdr *nlh; |
784b4e61 | 8652 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 8653 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 | 8654 | |
19c28b13 PNA |
8655 | nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC, |
8656 | NFNETLINK_V0, nft_base_seq(net)); | |
8657 | if (!nlh) | |
84d7fce6 PNA |
8658 | goto nla_put_failure; |
8659 | ||
0854db2a | 8660 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) || |
784b4e61 PS |
8661 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || |
8662 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
8663 | goto nla_put_failure; |
8664 | ||
053c095a JB |
8665 | nlmsg_end(skb, nlh); |
8666 | return 0; | |
84d7fce6 PNA |
8667 | |
8668 | nla_put_failure: | |
8669 | nlmsg_trim(skb, nlh); | |
8670 | return -EMSGSIZE; | |
8671 | } | |
8672 | ||
3b49e2e9 PNA |
8673 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
8674 | struct nft_flowtable *flowtable) | |
8675 | { | |
3f0465a9 | 8676 | struct nft_hook *hook; |
3b49e2e9 | 8677 | |
3f0465a9 PNA |
8678 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
8679 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
8680 | continue; |
8681 | ||
5acab914 | 8682 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 8683 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
8684 | list_del_rcu(&hook->list); |
8685 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
8686 | break; |
8687 | } | |
8688 | } | |
8689 | ||
8690 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
8691 | unsigned long event, void *ptr) | |
8692 | { | |
8693 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
8694 | struct nft_flowtable *flowtable; | |
0854db2a | 8695 | struct nftables_pernet *nft_net; |
3b49e2e9 | 8696 | struct nft_table *table; |
0a2cf5ee | 8697 | struct net *net; |
3b49e2e9 PNA |
8698 | |
8699 | if (event != NETDEV_UNREGISTER) | |
8700 | return 0; | |
8701 | ||
6a48de01 | 8702 | net = dev_net(dev); |
d59d2f82 | 8703 | nft_net = nft_pernet(net); |
0854db2a FW |
8704 | mutex_lock(&nft_net->commit_mutex); |
8705 | list_for_each_entry(table, &nft_net->tables, list) { | |
36596dad PNA |
8706 | list_for_each_entry(flowtable, &table->flowtables, list) { |
8707 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
8708 | } |
8709 | } | |
0854db2a | 8710 | mutex_unlock(&nft_net->commit_mutex); |
6a48de01 | 8711 | |
3b49e2e9 PNA |
8712 | return NOTIFY_DONE; |
8713 | } | |
8714 | ||
8715 | static struct notifier_block nf_tables_flowtable_notifier = { | |
8716 | .notifier_call = nf_tables_flowtable_event, | |
8717 | }; | |
8718 | ||
25e94a99 PNA |
8719 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
8720 | int event) | |
84d7fce6 PNA |
8721 | { |
8722 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
8723 | struct sk_buff *skb2; | |
8724 | int err; | |
8725 | ||
dceababa | 8726 | if (!nlmsg_report(nlh) && |
84d7fce6 | 8727 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 8728 | return; |
84d7fce6 | 8729 | |
84d7fce6 PNA |
8730 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
8731 | if (skb2 == NULL) | |
8732 | goto err; | |
8733 | ||
8734 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
8735 | nlh->nlmsg_seq); | |
8736 | if (err < 0) { | |
8737 | kfree_skb(skb2); | |
8738 | goto err; | |
8739 | } | |
8740 | ||
25e94a99 PNA |
8741 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8742 | nlmsg_report(nlh), GFP_KERNEL); | |
8743 | return; | |
84d7fce6 | 8744 | err: |
25e94a99 PNA |
8745 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8746 | -ENOBUFS); | |
84d7fce6 PNA |
8747 | } |
8748 | ||
797d4980 PNA |
8749 | static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info, |
8750 | const struct nlattr * const nla[]) | |
84d7fce6 | 8751 | { |
84d7fce6 PNA |
8752 | struct sk_buff *skb2; |
8753 | int err; | |
8754 | ||
d9adf22a | 8755 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
8756 | if (skb2 == NULL) |
8757 | return -ENOMEM; | |
8758 | ||
797d4980 PNA |
8759 | err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid, |
8760 | info->nlh->nlmsg_seq); | |
84d7fce6 | 8761 | if (err < 0) |
ee921183 | 8762 | goto err_fill_gen_info; |
84d7fce6 | 8763 | |
797d4980 | 8764 | return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); |
ee921183 PNA |
8765 | |
8766 | err_fill_gen_info: | |
84d7fce6 PNA |
8767 | kfree_skb(skb2); |
8768 | return err; | |
8769 | } | |
8770 | ||
96518518 PM |
8771 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
8772 | [NFT_MSG_NEWTABLE] = { | |
50f2db9e PNA |
8773 | .call = nf_tables_newtable, |
8774 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8775 | .attr_count = NFTA_TABLE_MAX, |
8776 | .policy = nft_table_policy, | |
8777 | }, | |
8778 | [NFT_MSG_GETTABLE] = { | |
50f2db9e PNA |
8779 | .call = nf_tables_gettable, |
8780 | .type = NFNL_CB_RCU, | |
96518518 PM |
8781 | .attr_count = NFTA_TABLE_MAX, |
8782 | .policy = nft_table_policy, | |
8783 | }, | |
8784 | [NFT_MSG_DELTABLE] = { | |
50f2db9e PNA |
8785 | .call = nf_tables_deltable, |
8786 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8787 | .attr_count = NFTA_TABLE_MAX, |
8788 | .policy = nft_table_policy, | |
8789 | }, | |
f80a612d FFM |
8790 | [NFT_MSG_DESTROYTABLE] = { |
8791 | .call = nf_tables_deltable, | |
8792 | .type = NFNL_CB_BATCH, | |
8793 | .attr_count = NFTA_TABLE_MAX, | |
8794 | .policy = nft_table_policy, | |
8795 | }, | |
96518518 | 8796 | [NFT_MSG_NEWCHAIN] = { |
50f2db9e PNA |
8797 | .call = nf_tables_newchain, |
8798 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8799 | .attr_count = NFTA_CHAIN_MAX, |
8800 | .policy = nft_chain_policy, | |
8801 | }, | |
8802 | [NFT_MSG_GETCHAIN] = { | |
50f2db9e PNA |
8803 | .call = nf_tables_getchain, |
8804 | .type = NFNL_CB_RCU, | |
96518518 PM |
8805 | .attr_count = NFTA_CHAIN_MAX, |
8806 | .policy = nft_chain_policy, | |
8807 | }, | |
8808 | [NFT_MSG_DELCHAIN] = { | |
50f2db9e PNA |
8809 | .call = nf_tables_delchain, |
8810 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8811 | .attr_count = NFTA_CHAIN_MAX, |
8812 | .policy = nft_chain_policy, | |
8813 | }, | |
f80a612d FFM |
8814 | [NFT_MSG_DESTROYCHAIN] = { |
8815 | .call = nf_tables_delchain, | |
8816 | .type = NFNL_CB_BATCH, | |
8817 | .attr_count = NFTA_CHAIN_MAX, | |
8818 | .policy = nft_chain_policy, | |
8819 | }, | |
96518518 | 8820 | [NFT_MSG_NEWRULE] = { |
50f2db9e PNA |
8821 | .call = nf_tables_newrule, |
8822 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8823 | .attr_count = NFTA_RULE_MAX, |
8824 | .policy = nft_rule_policy, | |
8825 | }, | |
8826 | [NFT_MSG_GETRULE] = { | |
50f2db9e PNA |
8827 | .call = nf_tables_getrule, |
8828 | .type = NFNL_CB_RCU, | |
96518518 PM |
8829 | .attr_count = NFTA_RULE_MAX, |
8830 | .policy = nft_rule_policy, | |
8831 | }, | |
8daa8fde PS |
8832 | [NFT_MSG_GETRULE_RESET] = { |
8833 | .call = nf_tables_getrule, | |
8834 | .type = NFNL_CB_RCU, | |
8835 | .attr_count = NFTA_RULE_MAX, | |
8836 | .policy = nft_rule_policy, | |
8837 | }, | |
96518518 | 8838 | [NFT_MSG_DELRULE] = { |
50f2db9e PNA |
8839 | .call = nf_tables_delrule, |
8840 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8841 | .attr_count = NFTA_RULE_MAX, |
8842 | .policy = nft_rule_policy, | |
8843 | }, | |
f80a612d FFM |
8844 | [NFT_MSG_DESTROYRULE] = { |
8845 | .call = nf_tables_delrule, | |
8846 | .type = NFNL_CB_BATCH, | |
8847 | .attr_count = NFTA_RULE_MAX, | |
8848 | .policy = nft_rule_policy, | |
8849 | }, | |
20a69341 | 8850 | [NFT_MSG_NEWSET] = { |
50f2db9e PNA |
8851 | .call = nf_tables_newset, |
8852 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8853 | .attr_count = NFTA_SET_MAX, |
8854 | .policy = nft_set_policy, | |
8855 | }, | |
8856 | [NFT_MSG_GETSET] = { | |
50f2db9e PNA |
8857 | .call = nf_tables_getset, |
8858 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8859 | .attr_count = NFTA_SET_MAX, |
8860 | .policy = nft_set_policy, | |
8861 | }, | |
8862 | [NFT_MSG_DELSET] = { | |
50f2db9e PNA |
8863 | .call = nf_tables_delset, |
8864 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8865 | .attr_count = NFTA_SET_MAX, |
8866 | .policy = nft_set_policy, | |
8867 | }, | |
f80a612d FFM |
8868 | [NFT_MSG_DESTROYSET] = { |
8869 | .call = nf_tables_delset, | |
8870 | .type = NFNL_CB_BATCH, | |
8871 | .attr_count = NFTA_SET_MAX, | |
8872 | .policy = nft_set_policy, | |
8873 | }, | |
20a69341 | 8874 | [NFT_MSG_NEWSETELEM] = { |
50f2db9e PNA |
8875 | .call = nf_tables_newsetelem, |
8876 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8877 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8878 | .policy = nft_set_elem_list_policy, | |
8879 | }, | |
8880 | [NFT_MSG_GETSETELEM] = { | |
50f2db9e PNA |
8881 | .call = nf_tables_getsetelem, |
8882 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8883 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8884 | .policy = nft_set_elem_list_policy, | |
8885 | }, | |
8886 | [NFT_MSG_DELSETELEM] = { | |
50f2db9e PNA |
8887 | .call = nf_tables_delsetelem, |
8888 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8889 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8890 | .policy = nft_set_elem_list_policy, | |
8891 | }, | |
f80a612d FFM |
8892 | [NFT_MSG_DESTROYSETELEM] = { |
8893 | .call = nf_tables_delsetelem, | |
8894 | .type = NFNL_CB_BATCH, | |
8895 | .attr_count = NFTA_SET_ELEM_LIST_MAX, | |
8896 | .policy = nft_set_elem_list_policy, | |
8897 | }, | |
84d7fce6 | 8898 | [NFT_MSG_GETGEN] = { |
50f2db9e PNA |
8899 | .call = nf_tables_getgen, |
8900 | .type = NFNL_CB_RCU, | |
84d7fce6 | 8901 | }, |
e5009240 | 8902 | [NFT_MSG_NEWOBJ] = { |
50f2db9e PNA |
8903 | .call = nf_tables_newobj, |
8904 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8905 | .attr_count = NFTA_OBJ_MAX, |
8906 | .policy = nft_obj_policy, | |
8907 | }, | |
8908 | [NFT_MSG_GETOBJ] = { | |
50f2db9e PNA |
8909 | .call = nf_tables_getobj, |
8910 | .type = NFNL_CB_RCU, | |
e5009240 PNA |
8911 | .attr_count = NFTA_OBJ_MAX, |
8912 | .policy = nft_obj_policy, | |
8913 | }, | |
8914 | [NFT_MSG_DELOBJ] = { | |
50f2db9e PNA |
8915 | .call = nf_tables_delobj, |
8916 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8917 | .attr_count = NFTA_OBJ_MAX, |
8918 | .policy = nft_obj_policy, | |
8919 | }, | |
f80a612d FFM |
8920 | [NFT_MSG_DESTROYOBJ] = { |
8921 | .call = nf_tables_delobj, | |
8922 | .type = NFNL_CB_BATCH, | |
8923 | .attr_count = NFTA_OBJ_MAX, | |
8924 | .policy = nft_obj_policy, | |
8925 | }, | |
43da04a5 | 8926 | [NFT_MSG_GETOBJ_RESET] = { |
50f2db9e PNA |
8927 | .call = nf_tables_getobj, |
8928 | .type = NFNL_CB_RCU, | |
43da04a5 PNA |
8929 | .attr_count = NFTA_OBJ_MAX, |
8930 | .policy = nft_obj_policy, | |
8931 | }, | |
3b49e2e9 | 8932 | [NFT_MSG_NEWFLOWTABLE] = { |
50f2db9e PNA |
8933 | .call = nf_tables_newflowtable, |
8934 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8935 | .attr_count = NFTA_FLOWTABLE_MAX, |
8936 | .policy = nft_flowtable_policy, | |
8937 | }, | |
8938 | [NFT_MSG_GETFLOWTABLE] = { | |
50f2db9e PNA |
8939 | .call = nf_tables_getflowtable, |
8940 | .type = NFNL_CB_RCU, | |
3b49e2e9 PNA |
8941 | .attr_count = NFTA_FLOWTABLE_MAX, |
8942 | .policy = nft_flowtable_policy, | |
8943 | }, | |
8944 | [NFT_MSG_DELFLOWTABLE] = { | |
50f2db9e PNA |
8945 | .call = nf_tables_delflowtable, |
8946 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8947 | .attr_count = NFTA_FLOWTABLE_MAX, |
8948 | .policy = nft_flowtable_policy, | |
8949 | }, | |
f80a612d FFM |
8950 | [NFT_MSG_DESTROYFLOWTABLE] = { |
8951 | .call = nf_tables_delflowtable, | |
8952 | .type = NFNL_CB_BATCH, | |
8953 | .attr_count = NFTA_FLOWTABLE_MAX, | |
8954 | .policy = nft_flowtable_policy, | |
8955 | }, | |
96518518 PM |
8956 | }; |
8957 | ||
a654de8f PNA |
8958 | static int nf_tables_validate(struct net *net) |
8959 | { | |
d59d2f82 | 8960 | struct nftables_pernet *nft_net = nft_pernet(net); |
a654de8f PNA |
8961 | struct nft_table *table; |
8962 | ||
00c320f9 FW |
8963 | list_for_each_entry(table, &nft_net->tables, list) { |
8964 | switch (table->validate_state) { | |
8965 | case NFT_VALIDATE_SKIP: | |
8966 | continue; | |
8967 | case NFT_VALIDATE_NEED: | |
8968 | nft_validate_state_update(table, NFT_VALIDATE_DO); | |
8969 | fallthrough; | |
8970 | case NFT_VALIDATE_DO: | |
a654de8f PNA |
8971 | if (nft_table_validate(net, table) < 0) |
8972 | return -EAGAIN; | |
00c320f9 FW |
8973 | |
8974 | nft_validate_state_update(table, NFT_VALIDATE_SKIP); | |
a654de8f | 8975 | } |
9a32e985 | 8976 | |
a654de8f PNA |
8977 | break; |
8978 | } | |
8979 | ||
8980 | return 0; | |
8981 | } | |
8982 | ||
66293c46 FW |
8983 | /* a drop policy has to be deferred until all rules have been activated, |
8984 | * otherwise a large ruleset that contains a drop-policy base chain will | |
8985 | * cause all packets to get dropped until the full transaction has been | |
8986 | * processed. | |
8987 | * | |
8988 | * We defer the drop policy until the transaction has been finalized. | |
8989 | */ | |
8990 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
8991 | { | |
8992 | struct nft_base_chain *basechain; | |
8993 | ||
8994 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
8995 | return; | |
8996 | ||
8997 | if (!nft_is_base_chain(trans->ctx.chain)) | |
8998 | return; | |
8999 | ||
9000 | basechain = nft_base_chain(trans->ctx.chain); | |
9001 | basechain->policy = NF_DROP; | |
9002 | } | |
9003 | ||
91c7b38d PNA |
9004 | static void nft_chain_commit_update(struct nft_trans *trans) |
9005 | { | |
9006 | struct nft_base_chain *basechain; | |
9007 | ||
1b2470e5 FW |
9008 | if (nft_trans_chain_name(trans)) { |
9009 | rhltable_remove(&trans->ctx.table->chains_ht, | |
9010 | &trans->ctx.chain->rhlhead, | |
9011 | nft_chain_ht_params); | |
d71efb59 | 9012 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
9013 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
9014 | trans->ctx.chain->name, | |
9015 | &trans->ctx.chain->rhlhead, | |
9016 | nft_chain_ht_params); | |
9017 | } | |
91c7b38d | 9018 | |
f323d954 | 9019 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
9020 | return; |
9021 | ||
53315ac6 FW |
9022 | nft_chain_stats_replace(trans); |
9023 | ||
91c7b38d | 9024 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
9025 | |
9026 | switch (nft_trans_chain_policy(trans)) { | |
9027 | case NF_DROP: | |
9028 | case NF_ACCEPT: | |
9029 | basechain->policy = nft_trans_chain_policy(trans); | |
9030 | break; | |
9031 | } | |
9032 | } | |
9033 | ||
d62d0ba9 FFM |
9034 | static void nft_obj_commit_update(struct nft_trans *trans) |
9035 | { | |
9036 | struct nft_object *newobj; | |
9037 | struct nft_object *obj; | |
9038 | ||
9039 | obj = nft_trans_obj(trans); | |
9040 | newobj = nft_trans_obj_newobj(trans); | |
9041 | ||
9fedd894 FFM |
9042 | if (obj->ops->update) |
9043 | obj->ops->update(obj, newobj); | |
d62d0ba9 | 9044 | |
dad3bdee | 9045 | nft_obj_destroy(&trans->ctx, newobj); |
d62d0ba9 FFM |
9046 | } |
9047 | ||
2f99aa31 | 9048 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 9049 | { |
c7c32e72 PNA |
9050 | switch (trans->msg_type) { |
9051 | case NFT_MSG_DELTABLE: | |
f80a612d | 9052 | case NFT_MSG_DESTROYTABLE: |
c7c32e72 PNA |
9053 | nf_tables_table_destroy(&trans->ctx); |
9054 | break; | |
9f8aac0b | 9055 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 9056 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
9057 | kfree(nft_trans_chain_name(trans)); |
9058 | break; | |
c7c32e72 | 9059 | case NFT_MSG_DELCHAIN: |
f80a612d | 9060 | case NFT_MSG_DESTROYCHAIN: |
7d937b10 PNA |
9061 | if (nft_trans_chain_update(trans)) |
9062 | nft_hooks_destroy(&nft_trans_chain_hooks(trans)); | |
9063 | else | |
9064 | nf_tables_chain_destroy(&trans->ctx); | |
c7c32e72 PNA |
9065 | break; |
9066 | case NFT_MSG_DELRULE: | |
f80a612d | 9067 | case NFT_MSG_DESTROYRULE: |
c7c32e72 PNA |
9068 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); |
9069 | break; | |
9070 | case NFT_MSG_DELSET: | |
f80a612d | 9071 | case NFT_MSG_DESTROYSET: |
0c2a85ed | 9072 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 9073 | break; |
61edafbb | 9074 | case NFT_MSG_DELSETELEM: |
f80a612d | 9075 | case NFT_MSG_DESTROYSETELEM: |
3453c927 PNA |
9076 | nf_tables_set_elem_destroy(&trans->ctx, |
9077 | nft_trans_elem_set(trans), | |
59105446 | 9078 | nft_trans_elem(trans).priv); |
61edafbb | 9079 | break; |
e5009240 | 9080 | case NFT_MSG_DELOBJ: |
f80a612d | 9081 | case NFT_MSG_DESTROYOBJ: |
00bfb320 | 9082 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 9083 | break; |
3b49e2e9 | 9084 | case NFT_MSG_DELFLOWTABLE: |
f80a612d | 9085 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 | 9086 | if (nft_trans_flowtable_update(trans)) |
cdc32546 | 9087 | nft_hooks_destroy(&nft_trans_flowtable_hooks(trans)); |
abadb2f8 PNA |
9088 | else |
9089 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 9090 | break; |
c7c32e72 | 9091 | } |
0935d558 FW |
9092 | |
9093 | if (trans->put_net) | |
9094 | put_net(trans->ctx.net); | |
9095 | ||
c7c32e72 PNA |
9096 | kfree(trans); |
9097 | } | |
9098 | ||
0935d558 | 9099 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
9100 | { |
9101 | struct nft_trans *trans, *next; | |
0935d558 FW |
9102 | LIST_HEAD(head); |
9103 | ||
9104 | spin_lock(&nf_tables_destroy_list_lock); | |
9105 | list_splice_init(&nf_tables_destroy_list, &head); | |
9106 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 9107 | |
0935d558 | 9108 | if (list_empty(&head)) |
2f99aa31 FW |
9109 | return; |
9110 | ||
9111 | synchronize_rcu(); | |
9112 | ||
0935d558 | 9113 | list_for_each_entry_safe(trans, next, &head, list) { |
2f99aa31 FW |
9114 | list_del(&trans->list); |
9115 | nft_commit_release(trans); | |
9116 | } | |
9117 | } | |
9118 | ||
ffe8923f FW |
9119 | void nf_tables_trans_destroy_flush_work(void) |
9120 | { | |
9121 | flush_work(&trans_destroy_work); | |
9122 | } | |
9123 | EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work); | |
9124 | ||
ed5f85d4 PNA |
9125 | static bool nft_expr_reduce(struct nft_regs_track *track, |
9126 | const struct nft_expr *expr) | |
9127 | { | |
9e539c5b | 9128 | return false; |
ed5f85d4 PNA |
9129 | } |
9130 | ||
0cbc06b3 FW |
9131 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
9132 | { | |
2c865a8a | 9133 | const struct nft_expr *expr, *last; |
12e4ecfa | 9134 | struct nft_regs_track track = {}; |
2c865a8a PNA |
9135 | unsigned int size, data_size; |
9136 | void *data, *data_boundary; | |
9137 | struct nft_rule_dp *prule; | |
0cbc06b3 | 9138 | struct nft_rule *rule; |
0cbc06b3 FW |
9139 | |
9140 | /* already handled or inactive chain? */ | |
2c865a8a | 9141 | if (chain->blob_next || !nft_is_active_next(net, chain)) |
0cbc06b3 FW |
9142 | return 0; |
9143 | ||
63045bfd | 9144 | data_size = 0; |
10377d42 | 9145 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
9146 | if (nft_is_active_next(net, rule)) { |
9147 | data_size += sizeof(*prule) + rule->dlen; | |
9148 | if (data_size > INT_MAX) | |
9149 | return -ENOMEM; | |
9150 | } | |
0cbc06b3 FW |
9151 | } |
9152 | ||
e38fbfa9 | 9153 | chain->blob_next = nf_tables_chain_alloc_rules(chain, data_size); |
2c865a8a | 9154 | if (!chain->blob_next) |
0cbc06b3 FW |
9155 | return -ENOMEM; |
9156 | ||
2c865a8a PNA |
9157 | data = (void *)chain->blob_next->data; |
9158 | data_boundary = data + data_size; | |
9159 | size = 0; | |
9160 | ||
10377d42 | 9161 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
9162 | if (!nft_is_active_next(net, rule)) |
9163 | continue; | |
9164 | ||
9165 | prule = (struct nft_rule_dp *)data; | |
9166 | data += offsetof(struct nft_rule_dp, data); | |
9167 | if (WARN_ON_ONCE(data > data_boundary)) | |
9168 | return -ENOMEM; | |
9169 | ||
12e4ecfa | 9170 | size = 0; |
fe75e84a | 9171 | track.last = nft_expr_last(rule); |
2c865a8a | 9172 | nft_rule_for_each_expr(expr, last, rule) { |
12e4ecfa PNA |
9173 | track.cur = expr; |
9174 | ||
ed5f85d4 | 9175 | if (nft_expr_reduce(&track, expr)) { |
12e4ecfa PNA |
9176 | expr = track.cur; |
9177 | continue; | |
9178 | } | |
9179 | ||
08e42a0d | 9180 | if (WARN_ON_ONCE(data + size + expr->ops->size > data_boundary)) |
2c865a8a PNA |
9181 | return -ENOMEM; |
9182 | ||
9183 | memcpy(data + size, expr, expr->ops->size); | |
9184 | size += expr->ops->size; | |
9185 | } | |
9186 | if (WARN_ON_ONCE(size >= 1 << 12)) | |
9187 | return -ENOMEM; | |
9188 | ||
9189 | prule->handle = rule->handle; | |
9190 | prule->dlen = size; | |
9191 | prule->is_last = 0; | |
9192 | ||
9193 | data += size; | |
9194 | size = 0; | |
9195 | chain->blob_next->size += (unsigned long)(data - (void *)prule); | |
0cbc06b3 FW |
9196 | } |
9197 | ||
2c865a8a PNA |
9198 | if (WARN_ON_ONCE(data > data_boundary)) |
9199 | return -ENOMEM; | |
9200 | ||
e38fbfa9 FW |
9201 | prule = (struct nft_rule_dp *)data; |
9202 | nft_last_rule(chain, prule); | |
2c865a8a | 9203 | |
0cbc06b3 FW |
9204 | return 0; |
9205 | } | |
9206 | ||
9207 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
9208 | { | |
d59d2f82 | 9209 | struct nftables_pernet *nft_net = nft_pernet(net); |
0cbc06b3 FW |
9210 | struct nft_trans *trans, *next; |
9211 | ||
0854db2a | 9212 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 FW |
9213 | struct nft_chain *chain = trans->ctx.chain; |
9214 | ||
9215 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
9216 | trans->msg_type == NFT_MSG_DELRULE) { | |
2c865a8a PNA |
9217 | kvfree(chain->blob_next); |
9218 | chain->blob_next = NULL; | |
0cbc06b3 FW |
9219 | } |
9220 | } | |
9221 | } | |
9222 | ||
e38fbfa9 | 9223 | static void __nf_tables_commit_chain_free_rules(struct rcu_head *h) |
0cbc06b3 | 9224 | { |
e38fbfa9 | 9225 | struct nft_rule_dp_last *l = container_of(h, struct nft_rule_dp_last, h); |
0cbc06b3 | 9226 | |
e38fbfa9 | 9227 | kvfree(l->blob); |
0cbc06b3 FW |
9228 | } |
9229 | ||
2c865a8a | 9230 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob) |
0cbc06b3 | 9231 | { |
e38fbfa9 | 9232 | struct nft_rule_dp_last *last; |
0cbc06b3 | 9233 | |
e38fbfa9 FW |
9234 | /* last rule trailer is after end marker */ |
9235 | last = (void *)blob + sizeof(*blob) + blob->size; | |
9236 | last->blob = blob; | |
0cbc06b3 | 9237 | |
e38fbfa9 | 9238 | call_rcu(&last->h, __nf_tables_commit_chain_free_rules); |
0cbc06b3 FW |
9239 | } |
9240 | ||
0fb39bbe | 9241 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 | 9242 | { |
2c865a8a | 9243 | struct nft_rule_blob *g0, *g1; |
0cbc06b3 FW |
9244 | bool next_genbit; |
9245 | ||
9246 | next_genbit = nft_gencursor_next(net); | |
9247 | ||
2c865a8a | 9248 | g0 = rcu_dereference_protected(chain->blob_gen_0, |
f102d66b | 9249 | lockdep_commit_lock_is_held(net)); |
2c865a8a | 9250 | g1 = rcu_dereference_protected(chain->blob_gen_1, |
f102d66b | 9251 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
9252 | |
9253 | /* No changes to this chain? */ | |
2c865a8a | 9254 | if (chain->blob_next == NULL) { |
0cbc06b3 FW |
9255 | /* chain had no change in last or next generation */ |
9256 | if (g0 == g1) | |
9257 | return; | |
9258 | /* | |
9259 | * chain had no change in this generation; make sure next | |
9260 | * one uses same rules as current generation. | |
9261 | */ | |
9262 | if (next_genbit) { | |
2c865a8a | 9263 | rcu_assign_pointer(chain->blob_gen_1, g0); |
0cbc06b3 FW |
9264 | nf_tables_commit_chain_free_rules_old(g1); |
9265 | } else { | |
2c865a8a | 9266 | rcu_assign_pointer(chain->blob_gen_0, g1); |
0cbc06b3 FW |
9267 | nf_tables_commit_chain_free_rules_old(g0); |
9268 | } | |
9269 | ||
9270 | return; | |
9271 | } | |
9272 | ||
9273 | if (next_genbit) | |
2c865a8a | 9274 | rcu_assign_pointer(chain->blob_gen_1, chain->blob_next); |
0cbc06b3 | 9275 | else |
2c865a8a | 9276 | rcu_assign_pointer(chain->blob_gen_0, chain->blob_next); |
0cbc06b3 | 9277 | |
2c865a8a | 9278 | chain->blob_next = NULL; |
0cbc06b3 FW |
9279 | |
9280 | if (g0 == g1) | |
9281 | return; | |
9282 | ||
9283 | if (next_genbit) | |
9284 | nf_tables_commit_chain_free_rules_old(g1); | |
9285 | else | |
9286 | nf_tables_commit_chain_free_rules_old(g0); | |
9287 | } | |
9288 | ||
d152159b FW |
9289 | static void nft_obj_del(struct nft_object *obj) |
9290 | { | |
4d44175a | 9291 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
9292 | list_del_rcu(&obj->list); |
9293 | } | |
9294 | ||
d0e2c7de | 9295 | void nft_chain_del(struct nft_chain *chain) |
1b2470e5 FW |
9296 | { |
9297 | struct nft_table *table = chain->table; | |
9298 | ||
9299 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
9300 | nft_chain_ht_params)); | |
9301 | list_del_rcu(&chain->list); | |
9302 | } | |
9303 | ||
eb014de4 PNA |
9304 | static void nf_tables_module_autoload_cleanup(struct net *net) |
9305 | { | |
d59d2f82 | 9306 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
9307 | struct nft_module_request *req, *next; |
9308 | ||
0854db2a FW |
9309 | WARN_ON_ONCE(!list_empty(&nft_net->commit_list)); |
9310 | list_for_each_entry_safe(req, next, &nft_net->module_list, list) { | |
eb014de4 PNA |
9311 | WARN_ON_ONCE(!req->done); |
9312 | list_del(&req->list); | |
9313 | kfree(req); | |
9314 | } | |
9315 | } | |
9316 | ||
0935d558 FW |
9317 | static void nf_tables_commit_release(struct net *net) |
9318 | { | |
d59d2f82 | 9319 | struct nftables_pernet *nft_net = nft_pernet(net); |
0935d558 FW |
9320 | struct nft_trans *trans; |
9321 | ||
9322 | /* all side effects have to be made visible. | |
9323 | * For example, if a chain named 'foo' has been deleted, a | |
9324 | * new transaction must not find it anymore. | |
9325 | * | |
9326 | * Memory reclaim happens asynchronously from work queue | |
9327 | * to prevent expensive synchronize_rcu() in commit phase. | |
9328 | */ | |
0854db2a | 9329 | if (list_empty(&nft_net->commit_list)) { |
eb014de4 | 9330 | nf_tables_module_autoload_cleanup(net); |
0854db2a | 9331 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
9332 | return; |
9333 | } | |
9334 | ||
0854db2a | 9335 | trans = list_last_entry(&nft_net->commit_list, |
0935d558 FW |
9336 | struct nft_trans, list); |
9337 | get_net(trans->ctx.net); | |
9338 | WARN_ON_ONCE(trans->put_net); | |
9339 | ||
9340 | trans->put_net = true; | |
9341 | spin_lock(&nf_tables_destroy_list_lock); | |
0854db2a | 9342 | list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list); |
0935d558 FW |
9343 | spin_unlock(&nf_tables_destroy_list_lock); |
9344 | ||
eb014de4 | 9345 | nf_tables_module_autoload_cleanup(net); |
0935d558 | 9346 | schedule_work(&trans_destroy_work); |
ffe8923f | 9347 | |
0854db2a | 9348 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
9349 | } |
9350 | ||
67cc570e PNA |
9351 | static void nft_commit_notify(struct net *net, u32 portid) |
9352 | { | |
d59d2f82 | 9353 | struct nftables_pernet *nft_net = nft_pernet(net); |
67cc570e PNA |
9354 | struct sk_buff *batch_skb = NULL, *nskb, *skb; |
9355 | unsigned char *data; | |
9356 | int len; | |
9357 | ||
0854db2a | 9358 | list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) { |
67cc570e PNA |
9359 | if (!batch_skb) { |
9360 | new_batch: | |
9361 | batch_skb = skb; | |
9362 | len = NLMSG_GOODSIZE - skb->len; | |
9363 | list_del(&skb->list); | |
9364 | continue; | |
9365 | } | |
9366 | len -= skb->len; | |
9367 | if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) { | |
9368 | data = skb_put(batch_skb, skb->len); | |
9369 | memcpy(data, skb->data, skb->len); | |
9370 | list_del(&skb->list); | |
9371 | kfree_skb(skb); | |
9372 | continue; | |
9373 | } | |
9374 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
9375 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
9376 | goto new_batch; | |
9377 | } | |
9378 | ||
9379 | if (batch_skb) { | |
9380 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
9381 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
9382 | } | |
9383 | ||
0854db2a | 9384 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); |
67cc570e PNA |
9385 | } |
9386 | ||
c520292f RGB |
9387 | static int nf_tables_commit_audit_alloc(struct list_head *adl, |
9388 | struct nft_table *table) | |
9389 | { | |
9390 | struct nft_audit_data *adp; | |
9391 | ||
9392 | list_for_each_entry(adp, adl, list) { | |
9393 | if (adp->table == table) | |
9394 | return 0; | |
9395 | } | |
9396 | adp = kzalloc(sizeof(*adp), GFP_KERNEL); | |
9397 | if (!adp) | |
9398 | return -ENOMEM; | |
9399 | adp->table = table; | |
9400 | list_add(&adp->list, adl); | |
9401 | return 0; | |
9402 | } | |
9403 | ||
cfbe3650 DM |
9404 | static void nf_tables_commit_audit_free(struct list_head *adl) |
9405 | { | |
9406 | struct nft_audit_data *adp, *adn; | |
9407 | ||
9408 | list_for_each_entry_safe(adp, adn, adl, list) { | |
9409 | list_del(&adp->list); | |
9410 | kfree(adp); | |
9411 | } | |
9412 | } | |
9413 | ||
c520292f RGB |
9414 | static void nf_tables_commit_audit_collect(struct list_head *adl, |
9415 | struct nft_table *table, u32 op) | |
9416 | { | |
9417 | struct nft_audit_data *adp; | |
9418 | ||
9419 | list_for_each_entry(adp, adl, list) { | |
9420 | if (adp->table == table) | |
9421 | goto found; | |
9422 | } | |
dadf33c9 | 9423 | WARN_ONCE(1, "table=%s not expected in commit list", table->name); |
c520292f RGB |
9424 | return; |
9425 | found: | |
9426 | adp->entries++; | |
9427 | if (!adp->op || adp->op > op) | |
9428 | adp->op = op; | |
9429 | } | |
9430 | ||
9431 | #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22) | |
9432 | ||
9433 | static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation) | |
9434 | { | |
9435 | struct nft_audit_data *adp, *adn; | |
9436 | char aubuf[AUNFTABLENAMELEN]; | |
9437 | ||
9438 | list_for_each_entry_safe(adp, adn, adl, list) { | |
9439 | snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name, | |
9440 | generation); | |
9441 | audit_log_nfcfg(aubuf, adp->table->family, adp->entries, | |
9442 | nft2audit_op[adp->op], GFP_KERNEL); | |
9443 | list_del(&adp->list); | |
9444 | kfree(adp); | |
9445 | } | |
9446 | } | |
9447 | ||
212ed75d PNA |
9448 | static void nft_set_commit_update(struct list_head *set_update_list) |
9449 | { | |
9450 | struct nft_set *set, *next; | |
9451 | ||
9452 | list_for_each_entry_safe(set, next, set_update_list, pending_update) { | |
9453 | list_del_init(&set->pending_update); | |
9454 | ||
9455 | if (!set->ops->commit) | |
9456 | continue; | |
9457 | ||
9458 | set->ops->commit(set); | |
9459 | } | |
9460 | } | |
9461 | ||
5913beaf | 9462 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 9463 | { |
d59d2f82 | 9464 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 9465 | struct nft_trans *trans, *next; |
212ed75d | 9466 | LIST_HEAD(set_update_list); |
a3716e70 | 9467 | struct nft_trans_elem *te; |
0cbc06b3 FW |
9468 | struct nft_chain *chain; |
9469 | struct nft_table *table; | |
34002783 | 9470 | unsigned int base_seq; |
c520292f | 9471 | LIST_HEAD(adl); |
c9626a2c | 9472 | int err; |
37082f93 | 9473 | |
0854db2a FW |
9474 | if (list_empty(&nft_net->commit_list)) { |
9475 | mutex_unlock(&nft_net->commit_mutex); | |
b8b27498 FW |
9476 | return 0; |
9477 | } | |
9478 | ||
a654de8f PNA |
9479 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
9480 | if (nf_tables_validate(net) < 0) | |
9481 | return -EAGAIN; | |
9482 | ||
c9626a2c PNA |
9483 | err = nft_flow_rule_offload_commit(net); |
9484 | if (err < 0) | |
9485 | return err; | |
9486 | ||
0cbc06b3 | 9487 | /* 1. Allocate space for next generation rules_gen_X[] */ |
0854db2a | 9488 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 | 9489 | int ret; |
37082f93 | 9490 | |
c520292f RGB |
9491 | ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table); |
9492 | if (ret) { | |
9493 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 9494 | nf_tables_commit_audit_free(&adl); |
c520292f RGB |
9495 | return ret; |
9496 | } | |
0cbc06b3 FW |
9497 | if (trans->msg_type == NFT_MSG_NEWRULE || |
9498 | trans->msg_type == NFT_MSG_DELRULE) { | |
9499 | chain = trans->ctx.chain; | |
9500 | ||
9501 | ret = nf_tables_commit_chain_prepare(net, chain); | |
9502 | if (ret < 0) { | |
9503 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 9504 | nf_tables_commit_audit_free(&adl); |
0cbc06b3 FW |
9505 | return ret; |
9506 | } | |
9507 | } | |
9508 | } | |
37082f93 | 9509 | |
0cbc06b3 | 9510 | /* step 2. Make rules_gen_X visible to packet path */ |
0854db2a | 9511 | list_for_each_entry(table, &nft_net->tables, list) { |
0fb39bbe FW |
9512 | list_for_each_entry(chain, &table->chains, list) |
9513 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
9514 | } |
9515 | ||
9516 | /* | |
9517 | * Bump generation counter, invalidate any dump in progress. | |
9518 | * Cannot fail after this point. | |
37082f93 | 9519 | */ |
34002783 PNA |
9520 | base_seq = READ_ONCE(nft_net->base_seq); |
9521 | while (++base_seq == 0) | |
0854db2a | 9522 | ; |
0cbc06b3 | 9523 | |
34002783 PNA |
9524 | WRITE_ONCE(nft_net->base_seq, base_seq); |
9525 | ||
0cbc06b3 FW |
9526 | /* step 3. Start new generation, rules_gen_X now in use. */ |
9527 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 | 9528 | |
0854db2a | 9529 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
c520292f RGB |
9530 | nf_tables_commit_audit_collect(&adl, trans->ctx.table, |
9531 | trans->msg_type); | |
b380e5c7 | 9532 | switch (trans->msg_type) { |
55dd6f93 PNA |
9533 | case NFT_MSG_NEWTABLE: |
9534 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
9535 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
9536 | nft_trans_destroy(trans); | |
9537 | break; | |
9538 | } | |
9539 | if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT) | |
0ce7cf41 PNA |
9540 | nf_tables_table_disable(net, trans->ctx.table); |
9541 | ||
179d9ba5 | 9542 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; |
55dd6f93 | 9543 | } else { |
f2a6d766 | 9544 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 9545 | } |
35151d84 | 9546 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
9547 | nft_trans_destroy(trans); |
9548 | break; | |
9549 | case NFT_MSG_DELTABLE: | |
f80a612d | 9550 | case NFT_MSG_DESTROYTABLE: |
f2a6d766 | 9551 | list_del_rcu(&trans->ctx.table->list); |
f80a612d | 9552 | nf_tables_table_notify(&trans->ctx, trans->msg_type); |
55dd6f93 | 9553 | break; |
91c7b38d | 9554 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 9555 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 9556 | nft_chain_commit_update(trans); |
b9703ed4 PNA |
9557 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN, |
9558 | &nft_trans_chain_hooks(trans)); | |
9559 | list_splice(&nft_trans_chain_hooks(trans), | |
9560 | &nft_trans_basechain(trans)->hook_list); | |
9f8aac0b FW |
9561 | /* trans destroyed after rcu grace period */ |
9562 | } else { | |
66293c46 | 9563 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 9564 | nft_clear(net, trans->ctx.chain); |
b9703ed4 | 9565 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN, NULL); |
9f8aac0b FW |
9566 | nft_trans_destroy(trans); |
9567 | } | |
91c7b38d PNA |
9568 | break; |
9569 | case NFT_MSG_DELCHAIN: | |
f80a612d | 9570 | case NFT_MSG_DESTROYCHAIN: |
7d937b10 PNA |
9571 | if (nft_trans_chain_update(trans)) { |
9572 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN, | |
9573 | &nft_trans_chain_hooks(trans)); | |
9574 | nft_netdev_unregister_hooks(net, | |
9575 | &nft_trans_chain_hooks(trans), | |
9576 | true); | |
9577 | } else { | |
9578 | nft_chain_del(trans->ctx.chain); | |
9579 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN, | |
9580 | NULL); | |
9581 | nf_tables_unregister_hook(trans->ctx.net, | |
9582 | trans->ctx.table, | |
9583 | trans->ctx.chain); | |
9584 | } | |
91c7b38d | 9585 | break; |
b380e5c7 | 9586 | case NFT_MSG_NEWRULE: |
889f7ee7 | 9587 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 9588 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 9589 | nft_trans_rule(trans), |
35151d84 | 9590 | NFT_MSG_NEWRULE); |
9dd732e0 PNA |
9591 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9592 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9593 | ||
37082f93 | 9594 | nft_trans_destroy(trans); |
b380e5c7 PNA |
9595 | break; |
9596 | case NFT_MSG_DELRULE: | |
f80a612d | 9597 | case NFT_MSG_DESTROYRULE: |
b380e5c7 | 9598 | list_del_rcu(&nft_trans_rule(trans)->list); |
35151d84 PNA |
9599 | nf_tables_rule_notify(&trans->ctx, |
9600 | nft_trans_rule(trans), | |
f80a612d | 9601 | trans->msg_type); |
f6ac8585 PNA |
9602 | nft_rule_expr_deactivate(&trans->ctx, |
9603 | nft_trans_rule(trans), | |
9604 | NFT_TRANS_COMMIT); | |
26b5934f PNA |
9605 | |
9606 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) | |
9607 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 | 9608 | break; |
958bee14 | 9609 | case NFT_MSG_NEWSET: |
123b9961 PNA |
9610 | if (nft_trans_set_update(trans)) { |
9611 | struct nft_set *set = nft_trans_set(trans); | |
4fefee57 | 9612 | |
123b9961 PNA |
9613 | WRITE_ONCE(set->timeout, nft_trans_set_timeout(trans)); |
9614 | WRITE_ONCE(set->gc_int, nft_trans_set_gc_int(trans)); | |
9615 | } else { | |
9616 | nft_clear(net, nft_trans_set(trans)); | |
9617 | /* This avoids hitting -EBUSY when deleting the table | |
9618 | * from the transaction. | |
9619 | */ | |
9620 | if (nft_set_is_anonymous(nft_trans_set(trans)) && | |
9621 | !list_empty(&nft_trans_set(trans)->bindings)) | |
9622 | trans->ctx.table->use--; | |
9623 | } | |
958bee14 | 9624 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 9625 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
9626 | nft_trans_destroy(trans); |
9627 | break; | |
9628 | case NFT_MSG_DELSET: | |
f80a612d | 9629 | case NFT_MSG_DESTROYSET: |
37a9cc52 | 9630 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 9631 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
f80a612d | 9632 | trans->msg_type, GFP_KERNEL); |
958bee14 | 9633 | break; |
60319eb1 | 9634 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
9635 | te = (struct nft_trans_elem *)trans->data; |
9636 | ||
aaa31047 | 9637 | nft_setelem_activate(net, te->set, &te->elem); |
cc02e457 PM |
9638 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9639 | &te->elem, | |
6fb721cf | 9640 | NFT_MSG_NEWSETELEM); |
212ed75d PNA |
9641 | if (te->set->ops->commit && |
9642 | list_empty(&te->set->pending_update)) { | |
9643 | list_add_tail(&te->set->pending_update, | |
9644 | &set_update_list); | |
9645 | } | |
60319eb1 PNA |
9646 | nft_trans_destroy(trans); |
9647 | break; | |
9648 | case NFT_MSG_DELSETELEM: | |
f80a612d | 9649 | case NFT_MSG_DESTROYSETELEM: |
a3716e70 | 9650 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 9651 | |
a3716e70 PNA |
9652 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9653 | &te->elem, | |
f80a612d | 9654 | trans->msg_type); |
aaa31047 PNA |
9655 | nft_setelem_remove(net, te->set, &te->elem); |
9656 | if (!nft_setelem_is_catchall(te->set, &te->elem)) { | |
9657 | atomic_dec(&te->set->nelems); | |
9658 | te->set->ndeact--; | |
9659 | } | |
212ed75d PNA |
9660 | if (te->set->ops->commit && |
9661 | list_empty(&te->set->pending_update)) { | |
9662 | list_add_tail(&te->set->pending_update, | |
9663 | &set_update_list); | |
9664 | } | |
60319eb1 | 9665 | break; |
e5009240 | 9666 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
9667 | if (nft_trans_obj_update(trans)) { |
9668 | nft_obj_commit_update(trans); | |
9669 | nf_tables_obj_notify(&trans->ctx, | |
9670 | nft_trans_obj(trans), | |
9671 | NFT_MSG_NEWOBJ); | |
9672 | } else { | |
9673 | nft_clear(net, nft_trans_obj(trans)); | |
9674 | nf_tables_obj_notify(&trans->ctx, | |
9675 | nft_trans_obj(trans), | |
9676 | NFT_MSG_NEWOBJ); | |
9677 | nft_trans_destroy(trans); | |
9678 | } | |
e5009240 PNA |
9679 | break; |
9680 | case NFT_MSG_DELOBJ: | |
f80a612d | 9681 | case NFT_MSG_DESTROYOBJ: |
d152159b | 9682 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 | 9683 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
f80a612d | 9684 | trans->msg_type); |
e5009240 | 9685 | break; |
3b49e2e9 | 9686 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 9687 | if (nft_trans_flowtable_update(trans)) { |
7b35582c PNA |
9688 | nft_trans_flowtable(trans)->data.flags = |
9689 | nft_trans_flowtable_flags(trans); | |
78d9f48f PNA |
9690 | nf_tables_flowtable_notify(&trans->ctx, |
9691 | nft_trans_flowtable(trans), | |
9692 | &nft_trans_flowtable_hooks(trans), | |
9693 | NFT_MSG_NEWFLOWTABLE); | |
9694 | list_splice(&nft_trans_flowtable_hooks(trans), | |
9695 | &nft_trans_flowtable(trans)->hook_list); | |
9696 | } else { | |
9697 | nft_clear(net, nft_trans_flowtable(trans)); | |
9698 | nf_tables_flowtable_notify(&trans->ctx, | |
9699 | nft_trans_flowtable(trans), | |
28339b21 | 9700 | NULL, |
78d9f48f PNA |
9701 | NFT_MSG_NEWFLOWTABLE); |
9702 | } | |
3b49e2e9 PNA |
9703 | nft_trans_destroy(trans); |
9704 | break; | |
9705 | case NFT_MSG_DELFLOWTABLE: | |
f80a612d | 9706 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 | 9707 | if (nft_trans_flowtable_update(trans)) { |
abadb2f8 PNA |
9708 | nf_tables_flowtable_notify(&trans->ctx, |
9709 | nft_trans_flowtable(trans), | |
9710 | &nft_trans_flowtable_hooks(trans), | |
f80a612d | 9711 | trans->msg_type); |
abadb2f8 PNA |
9712 | nft_unregister_flowtable_net_hooks(net, |
9713 | &nft_trans_flowtable_hooks(trans)); | |
9714 | } else { | |
9715 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9716 | nf_tables_flowtable_notify(&trans->ctx, | |
9717 | nft_trans_flowtable(trans), | |
28339b21 | 9718 | NULL, |
f80a612d | 9719 | trans->msg_type); |
abadb2f8 PNA |
9720 | nft_unregister_flowtable_net_hooks(net, |
9721 | &nft_trans_flowtable(trans)->hook_list); | |
9722 | } | |
3b49e2e9 | 9723 | break; |
37082f93 | 9724 | } |
37082f93 PNA |
9725 | } |
9726 | ||
212ed75d PNA |
9727 | nft_set_commit_update(&set_update_list); |
9728 | ||
67cc570e | 9729 | nft_commit_notify(net, NETLINK_CB(skb).portid); |
84d7fce6 | 9730 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0854db2a | 9731 | nf_tables_commit_audit_log(&adl, nft_net->base_seq); |
0935d558 | 9732 | nf_tables_commit_release(net); |
37082f93 PNA |
9733 | |
9734 | return 0; | |
9735 | } | |
9736 | ||
eb014de4 PNA |
9737 | static void nf_tables_module_autoload(struct net *net) |
9738 | { | |
d59d2f82 | 9739 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
9740 | struct nft_module_request *req, *next; |
9741 | LIST_HEAD(module_list); | |
9742 | ||
0854db2a FW |
9743 | list_splice_init(&nft_net->module_list, &module_list); |
9744 | mutex_unlock(&nft_net->commit_mutex); | |
eb014de4 | 9745 | list_for_each_entry_safe(req, next, &module_list, list) { |
1d305ba4 FW |
9746 | request_module("%s", req->module); |
9747 | req->done = true; | |
eb014de4 | 9748 | } |
0854db2a FW |
9749 | mutex_lock(&nft_net->commit_mutex); |
9750 | list_splice(&module_list, &nft_net->module_list); | |
eb014de4 PNA |
9751 | } |
9752 | ||
b326dd37 | 9753 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 9754 | { |
c7c32e72 PNA |
9755 | switch (trans->msg_type) { |
9756 | case NFT_MSG_NEWTABLE: | |
9757 | nf_tables_table_destroy(&trans->ctx); | |
9758 | break; | |
9759 | case NFT_MSG_NEWCHAIN: | |
b9703ed4 PNA |
9760 | if (nft_trans_chain_update(trans)) |
9761 | nft_hooks_destroy(&nft_trans_chain_hooks(trans)); | |
9762 | else | |
9763 | nf_tables_chain_destroy(&trans->ctx); | |
c7c32e72 PNA |
9764 | break; |
9765 | case NFT_MSG_NEWRULE: | |
9766 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
9767 | break; | |
9768 | case NFT_MSG_NEWSET: | |
0c2a85ed | 9769 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 9770 | break; |
61edafbb PM |
9771 | case NFT_MSG_NEWSETELEM: |
9772 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 9773 | nft_trans_elem(trans).priv, true); |
61edafbb | 9774 | break; |
e5009240 | 9775 | case NFT_MSG_NEWOBJ: |
00bfb320 | 9776 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 9777 | break; |
3b49e2e9 | 9778 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 9779 | if (nft_trans_flowtable_update(trans)) |
cdc32546 | 9780 | nft_hooks_destroy(&nft_trans_flowtable_hooks(trans)); |
78d9f48f PNA |
9781 | else |
9782 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 9783 | break; |
c7c32e72 PNA |
9784 | } |
9785 | kfree(trans); | |
9786 | } | |
9787 | ||
212ed75d PNA |
9788 | static void nft_set_abort_update(struct list_head *set_update_list) |
9789 | { | |
9790 | struct nft_set *set, *next; | |
9791 | ||
9792 | list_for_each_entry_safe(set, next, set_update_list, pending_update) { | |
9793 | list_del_init(&set->pending_update); | |
9794 | ||
9795 | if (!set->ops->abort) | |
9796 | continue; | |
9797 | ||
9798 | set->ops->abort(set); | |
9799 | } | |
9800 | } | |
9801 | ||
c0391b6a | 9802 | static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) |
37082f93 | 9803 | { |
d59d2f82 | 9804 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 9805 | struct nft_trans *trans, *next; |
212ed75d | 9806 | LIST_HEAD(set_update_list); |
02263db0 | 9807 | struct nft_trans_elem *te; |
37082f93 | 9808 | |
c0391b6a PNA |
9809 | if (action == NFNL_ABORT_VALIDATE && |
9810 | nf_tables_validate(net) < 0) | |
9811 | return -EAGAIN; | |
9812 | ||
0854db2a | 9813 | list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list, |
a907e36d | 9814 | list) { |
b380e5c7 | 9815 | switch (trans->msg_type) { |
55dd6f93 PNA |
9816 | case NFT_MSG_NEWTABLE: |
9817 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
9818 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
9819 | nft_trans_destroy(trans); | |
9820 | break; | |
9821 | } | |
9822 | if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) { | |
0ce7cf41 | 9823 | nf_tables_table_disable(net, trans->ctx.table); |
179d9ba5 PNA |
9824 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
9825 | } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) { | |
9826 | trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT; | |
9827 | } | |
9828 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; | |
55dd6f93 PNA |
9829 | nft_trans_destroy(trans); |
9830 | } else { | |
e688a7f8 | 9831 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
9832 | } |
9833 | break; | |
9834 | case NFT_MSG_DELTABLE: | |
f80a612d | 9835 | case NFT_MSG_DESTROYTABLE: |
f2a6d766 | 9836 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
9837 | nft_trans_destroy(trans); |
9838 | break; | |
91c7b38d PNA |
9839 | case NFT_MSG_NEWCHAIN: |
9840 | if (nft_trans_chain_update(trans)) { | |
b9703ed4 PNA |
9841 | nft_netdev_unregister_hooks(net, |
9842 | &nft_trans_chain_hooks(trans), | |
9843 | true); | |
982f4051 | 9844 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 9845 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
9846 | nft_trans_destroy(trans); |
9847 | } else { | |
4bedf9ee | 9848 | if (nft_trans_chain_bound(trans)) { |
d0e2c7de PNA |
9849 | nft_trans_destroy(trans); |
9850 | break; | |
9851 | } | |
4fefee57 | 9852 | trans->ctx.table->use--; |
1b2470e5 | 9853 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
9854 | nf_tables_unregister_hook(trans->ctx.net, |
9855 | trans->ctx.table, | |
9856 | trans->ctx.chain); | |
91c7b38d PNA |
9857 | } |
9858 | break; | |
9859 | case NFT_MSG_DELCHAIN: | |
f80a612d | 9860 | case NFT_MSG_DESTROYCHAIN: |
7d937b10 PNA |
9861 | if (nft_trans_chain_update(trans)) { |
9862 | list_splice(&nft_trans_chain_hooks(trans), | |
9863 | &nft_trans_basechain(trans)->hook_list); | |
9864 | } else { | |
9865 | trans->ctx.table->use++; | |
9866 | nft_clear(trans->ctx.net, trans->ctx.chain); | |
9867 | } | |
91c7b38d PNA |
9868 | nft_trans_destroy(trans); |
9869 | break; | |
b380e5c7 | 9870 | case NFT_MSG_NEWRULE: |
4bedf9ee PNA |
9871 | if (nft_trans_rule_bound(trans)) { |
9872 | nft_trans_destroy(trans); | |
9873 | break; | |
9874 | } | |
4fefee57 | 9875 | trans->ctx.chain->use--; |
b380e5c7 | 9876 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
9877 | nft_rule_expr_deactivate(&trans->ctx, |
9878 | nft_trans_rule(trans), | |
9879 | NFT_TRANS_ABORT); | |
3c5e4462 PNA |
9880 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9881 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 PNA |
9882 | break; |
9883 | case NFT_MSG_DELRULE: | |
f80a612d | 9884 | case NFT_MSG_DESTROYRULE: |
4fefee57 | 9885 | trans->ctx.chain->use++; |
889f7ee7 | 9886 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 9887 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
3c5e4462 PNA |
9888 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9889 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9890 | ||
37082f93 | 9891 | nft_trans_destroy(trans); |
b380e5c7 | 9892 | break; |
958bee14 | 9893 | case NFT_MSG_NEWSET: |
123b9961 PNA |
9894 | if (nft_trans_set_update(trans)) { |
9895 | nft_trans_destroy(trans); | |
9896 | break; | |
9897 | } | |
4fefee57 | 9898 | trans->ctx.table->use--; |
6a0a8d10 | 9899 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
9900 | nft_trans_destroy(trans); |
9901 | break; | |
9902 | } | |
9903 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
9904 | break; |
9905 | case NFT_MSG_DELSET: | |
f80a612d | 9906 | case NFT_MSG_DESTROYSET: |
4fefee57 | 9907 | trans->ctx.table->use++; |
37a9cc52 | 9908 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
628bd3e4 PNA |
9909 | if (nft_trans_set(trans)->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) |
9910 | nft_map_activate(&trans->ctx, nft_trans_set(trans)); | |
9911 | ||
958bee14 PNA |
9912 | nft_trans_destroy(trans); |
9913 | break; | |
60319eb1 | 9914 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 9915 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
9916 | nft_trans_destroy(trans); |
9917 | break; | |
9918 | } | |
02263db0 | 9919 | te = (struct nft_trans_elem *)trans->data; |
aaa31047 PNA |
9920 | nft_setelem_remove(net, te->set, &te->elem); |
9921 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9922 | atomic_dec(&te->set->nelems); | |
212ed75d PNA |
9923 | |
9924 | if (te->set->ops->abort && | |
9925 | list_empty(&te->set->pending_update)) { | |
9926 | list_add_tail(&te->set->pending_update, | |
9927 | &set_update_list); | |
9928 | } | |
60319eb1 PNA |
9929 | break; |
9930 | case NFT_MSG_DELSETELEM: | |
f80a612d | 9931 | case NFT_MSG_DESTROYSETELEM: |
cc02e457 PM |
9932 | te = (struct nft_trans_elem *)trans->data; |
9933 | ||
f8bb7889 | 9934 | nft_setelem_data_activate(net, te->set, &te->elem); |
aaa31047 PNA |
9935 | nft_setelem_activate(net, te->set, &te->elem); |
9936 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9937 | te->set->ndeact--; | |
cc02e457 | 9938 | |
212ed75d PNA |
9939 | if (te->set->ops->abort && |
9940 | list_empty(&te->set->pending_update)) { | |
9941 | list_add_tail(&te->set->pending_update, | |
9942 | &set_update_list); | |
9943 | } | |
e5009240 PNA |
9944 | nft_trans_destroy(trans); |
9945 | break; | |
9946 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 | 9947 | if (nft_trans_obj_update(trans)) { |
dad3bdee | 9948 | nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans)); |
d62d0ba9 FFM |
9949 | nft_trans_destroy(trans); |
9950 | } else { | |
9951 | trans->ctx.table->use--; | |
9952 | nft_obj_del(nft_trans_obj(trans)); | |
9953 | } | |
e5009240 PNA |
9954 | break; |
9955 | case NFT_MSG_DELOBJ: | |
f80a612d | 9956 | case NFT_MSG_DESTROYOBJ: |
e5009240 PNA |
9957 | trans->ctx.table->use++; |
9958 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
9959 | nft_trans_destroy(trans); |
9960 | break; | |
3b49e2e9 | 9961 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9962 | if (nft_trans_flowtable_update(trans)) { |
9963 | nft_unregister_flowtable_net_hooks(net, | |
9964 | &nft_trans_flowtable_hooks(trans)); | |
9965 | } else { | |
9966 | trans->ctx.table->use--; | |
9967 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9968 | nft_unregister_flowtable_net_hooks(net, | |
9969 | &nft_trans_flowtable(trans)->hook_list); | |
9970 | } | |
3b49e2e9 PNA |
9971 | break; |
9972 | case NFT_MSG_DELFLOWTABLE: | |
f80a612d | 9973 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 | 9974 | if (nft_trans_flowtable_update(trans)) { |
b6d9014a PNA |
9975 | list_splice(&nft_trans_flowtable_hooks(trans), |
9976 | &nft_trans_flowtable(trans)->hook_list); | |
abadb2f8 PNA |
9977 | } else { |
9978 | trans->ctx.table->use++; | |
9979 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
9980 | } | |
3b49e2e9 PNA |
9981 | nft_trans_destroy(trans); |
9982 | break; | |
37082f93 | 9983 | } |
37082f93 PNA |
9984 | } |
9985 | ||
212ed75d PNA |
9986 | nft_set_abort_update(&set_update_list); |
9987 | ||
b326dd37 PNA |
9988 | synchronize_rcu(); |
9989 | ||
a1cee076 | 9990 | list_for_each_entry_safe_reverse(trans, next, |
0854db2a | 9991 | &nft_net->commit_list, list) { |
c7c32e72 | 9992 | list_del(&trans->list); |
b326dd37 | 9993 | nf_tables_abort_release(trans); |
37082f93 PNA |
9994 | } |
9995 | ||
c0391b6a | 9996 | if (action == NFNL_ABORT_AUTOLOAD) |
eb014de4 PNA |
9997 | nf_tables_module_autoload(net); |
9998 | else | |
9999 | nf_tables_module_autoload_cleanup(net); | |
10000 | ||
37082f93 PNA |
10001 | return 0; |
10002 | } | |
10003 | ||
c0391b6a PNA |
10004 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, |
10005 | enum nfnl_abort_action action) | |
71ad00c5 | 10006 | { |
d59d2f82 | 10007 | struct nftables_pernet *nft_net = nft_pernet(net); |
c0391b6a | 10008 | int ret = __nf_tables_abort(net, action); |
f102d66b | 10009 | |
0854db2a | 10010 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
10011 | |
10012 | return ret; | |
71ad00c5 FW |
10013 | } |
10014 | ||
74e8bcd2 PNA |
10015 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
10016 | { | |
d59d2f82 | 10017 | struct nftables_pernet *nft_net = nft_pernet(net); |
f102d66b FW |
10018 | bool genid_ok; |
10019 | ||
0854db2a | 10020 | mutex_lock(&nft_net->commit_mutex); |
f102d66b | 10021 | |
0854db2a | 10022 | genid_ok = genid == 0 || nft_net->base_seq == genid; |
f102d66b | 10023 | if (!genid_ok) |
0854db2a | 10024 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
10025 | |
10026 | /* else, commit mutex has to be released by commit or abort function */ | |
10027 | return genid_ok; | |
74e8bcd2 PNA |
10028 | } |
10029 | ||
96518518 PM |
10030 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
10031 | .name = "nf_tables", | |
10032 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
10033 | .cb_count = NFT_MSG_MAX, | |
10034 | .cb = nf_tables_cb, | |
0628b123 PNA |
10035 | .commit = nf_tables_commit, |
10036 | .abort = nf_tables_abort, | |
74e8bcd2 | 10037 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 10038 | .owner = THIS_MODULE, |
96518518 PM |
10039 | }; |
10040 | ||
7210e4e3 | 10041 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 10042 | enum nft_chain_types type) |
7210e4e3 PNA |
10043 | { |
10044 | const struct nft_base_chain *basechain; | |
10045 | ||
f323d954 | 10046 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
10047 | basechain = nft_base_chain(chain); |
10048 | if (basechain->type->type != type) | |
10049 | return -EOPNOTSUPP; | |
10050 | } | |
10051 | return 0; | |
10052 | } | |
10053 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
10054 | ||
75e8d06d PNA |
10055 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
10056 | unsigned int hook_flags) | |
10057 | { | |
10058 | struct nft_base_chain *basechain; | |
10059 | ||
f323d954 | 10060 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
10061 | basechain = nft_base_chain(chain); |
10062 | ||
c974a3a3 | 10063 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
10064 | return 0; |
10065 | ||
10066 | return -EOPNOTSUPP; | |
10067 | } | |
10068 | ||
10069 | return 0; | |
10070 | } | |
10071 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
10072 | ||
20a69341 PM |
10073 | /* |
10074 | * Loop detection - walk through the ruleset beginning at the destination chain | |
10075 | * of a new jump until either the source chain is reached (loop) or all | |
10076 | * reachable chains have been traversed. | |
10077 | * | |
10078 | * The loop check is performed whenever a new jump verdict is added to an | |
10079 | * expression or verdict map or a verdict map is bound to a new chain. | |
10080 | */ | |
10081 | ||
10082 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
10083 | const struct nft_chain *chain); | |
10084 | ||
6387aa6e PNA |
10085 | static int nft_check_loops(const struct nft_ctx *ctx, |
10086 | const struct nft_set_ext *ext) | |
10087 | { | |
10088 | const struct nft_data *data; | |
10089 | int ret; | |
10090 | ||
10091 | data = nft_set_ext_data(ext); | |
10092 | switch (data->verdict.code) { | |
10093 | case NFT_JUMP: | |
10094 | case NFT_GOTO: | |
10095 | ret = nf_tables_check_loops(ctx, data->verdict.chain); | |
10096 | break; | |
10097 | default: | |
10098 | ret = 0; | |
10099 | break; | |
10100 | } | |
10101 | ||
10102 | return ret; | |
10103 | } | |
10104 | ||
20a69341 | 10105 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, |
de70185d | 10106 | struct nft_set *set, |
20a69341 | 10107 | const struct nft_set_iter *iter, |
de70185d | 10108 | struct nft_set_elem *elem) |
20a69341 | 10109 | { |
fe2811eb | 10110 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
fe2811eb PM |
10111 | |
10112 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
10113 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
10114 | return 0; |
10115 | ||
6387aa6e | 10116 | return nft_check_loops(ctx, ext); |
20a69341 PM |
10117 | } |
10118 | ||
aaa31047 PNA |
10119 | static int nft_set_catchall_loops(const struct nft_ctx *ctx, |
10120 | struct nft_set *set) | |
10121 | { | |
10122 | u8 genmask = nft_genmask_next(ctx->net); | |
10123 | struct nft_set_elem_catchall *catchall; | |
10124 | struct nft_set_ext *ext; | |
10125 | int ret = 0; | |
10126 | ||
10127 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
10128 | ext = nft_set_elem_ext(set, catchall->elem); | |
10129 | if (!nft_set_elem_active(ext, genmask)) | |
10130 | continue; | |
10131 | ||
10132 | ret = nft_check_loops(ctx, ext); | |
10133 | if (ret < 0) | |
10134 | return ret; | |
10135 | } | |
10136 | ||
10137 | return ret; | |
10138 | } | |
10139 | ||
20a69341 PM |
10140 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
10141 | const struct nft_chain *chain) | |
10142 | { | |
10143 | const struct nft_rule *rule; | |
10144 | const struct nft_expr *expr, *last; | |
de70185d | 10145 | struct nft_set *set; |
20a69341 PM |
10146 | struct nft_set_binding *binding; |
10147 | struct nft_set_iter iter; | |
20a69341 PM |
10148 | |
10149 | if (ctx->chain == chain) | |
10150 | return -ELOOP; | |
10151 | ||
10152 | list_for_each_entry(rule, &chain->rules, list) { | |
10153 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
10154 | struct nft_immediate_expr *priv; |
10155 | const struct nft_data *data; | |
0ca743a5 PNA |
10156 | int err; |
10157 | ||
a654de8f | 10158 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
10159 | continue; |
10160 | ||
a654de8f PNA |
10161 | priv = nft_expr_priv(expr); |
10162 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 10163 | continue; |
20a69341 | 10164 | |
a654de8f | 10165 | data = &priv->data; |
1ca2e170 | 10166 | switch (data->verdict.code) { |
20a69341 PM |
10167 | case NFT_JUMP: |
10168 | case NFT_GOTO: | |
1ca2e170 PM |
10169 | err = nf_tables_check_loops(ctx, |
10170 | data->verdict.chain); | |
20a69341 PM |
10171 | if (err < 0) |
10172 | return err; | |
c2168e6b | 10173 | break; |
20a69341 PM |
10174 | default: |
10175 | break; | |
10176 | } | |
10177 | } | |
10178 | } | |
10179 | ||
10180 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
10181 | if (!nft_is_active_next(ctx->net, set)) |
10182 | continue; | |
20a69341 PM |
10183 | if (!(set->flags & NFT_SET_MAP) || |
10184 | set->dtype != NFT_DATA_VERDICT) | |
10185 | continue; | |
10186 | ||
10187 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
10188 | if (!(binding->flags & NFT_SET_MAP) || |
10189 | binding->chain != chain) | |
20a69341 PM |
10190 | continue; |
10191 | ||
8588ac09 | 10192 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
10193 | iter.skip = 0; |
10194 | iter.count = 0; | |
10195 | iter.err = 0; | |
10196 | iter.fn = nf_tables_loop_check_setelem; | |
10197 | ||
10198 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
10199 | if (!iter.err) |
10200 | iter.err = nft_set_catchall_loops(ctx, set); | |
10201 | ||
20a69341 PM |
10202 | if (iter.err < 0) |
10203 | return iter.err; | |
10204 | } | |
10205 | } | |
10206 | ||
10207 | return 0; | |
10208 | } | |
10209 | ||
36b701fa LGL |
10210 | /** |
10211 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
10212 | * | |
10213 | * @attr: netlink attribute to fetch value from | |
10214 | * @max: maximum value to be stored in dest | |
10215 | * @dest: pointer to the variable | |
10216 | * | |
10217 | * Parse, check and store a given u32 netlink attribute into variable. | |
10218 | * This function returns -ERANGE if the value goes over maximum value. | |
10219 | * Otherwise a 0 is returned and the attribute value is stored in the | |
10220 | * destination variable. | |
10221 | */ | |
f1d505bb | 10222 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 10223 | { |
09525a09 | 10224 | u32 val; |
36b701fa LGL |
10225 | |
10226 | val = ntohl(nla_get_be32(attr)); | |
10227 | if (val > max) | |
10228 | return -ERANGE; | |
10229 | ||
10230 | *dest = val; | |
10231 | return 0; | |
10232 | } | |
10233 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
10234 | ||
6c6f9f31 | 10235 | static int nft_parse_register(const struct nlattr *attr, u32 *preg) |
b1c96ed3 | 10236 | { |
49499c3e PM |
10237 | unsigned int reg; |
10238 | ||
10239 | reg = ntohl(nla_get_be32(attr)); | |
10240 | switch (reg) { | |
10241 | case NFT_REG_VERDICT...NFT_REG_4: | |
6e1acfa3 PNA |
10242 | *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE; |
10243 | break; | |
10244 | case NFT_REG32_00...NFT_REG32_15: | |
10245 | *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
10246 | break; | |
49499c3e | 10247 | default: |
6e1acfa3 | 10248 | return -ERANGE; |
49499c3e | 10249 | } |
6e1acfa3 PNA |
10250 | |
10251 | return 0; | |
b1c96ed3 | 10252 | } |
b1c96ed3 | 10253 | |
49499c3e PM |
10254 | /** |
10255 | * nft_dump_register - dump a register value to a netlink attribute | |
10256 | * | |
10257 | * @skb: socket buffer | |
10258 | * @attr: attribute number | |
10259 | * @reg: register number | |
10260 | * | |
10261 | * Construct a netlink attribute containing the register number. For | |
10262 | * compatibility reasons, register numbers being a multiple of 4 are | |
10263 | * translated to the corresponding 128 bit register numbers. | |
10264 | */ | |
b1c96ed3 PM |
10265 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
10266 | { | |
49499c3e PM |
10267 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
10268 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
10269 | else | |
10270 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
10271 | ||
b1c96ed3 PM |
10272 | return nla_put_be32(skb, attr, htonl(reg)); |
10273 | } | |
10274 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
10275 | ||
4f16d25c | 10276 | static int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 10277 | { |
49499c3e | 10278 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 10279 | return -EINVAL; |
d07db988 PM |
10280 | if (len == 0) |
10281 | return -EINVAL; | |
c593642c | 10282 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 10283 | return -ERANGE; |
49499c3e | 10284 | |
96518518 PM |
10285 | return 0; |
10286 | } | |
4f16d25c PNA |
10287 | |
10288 | int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len) | |
10289 | { | |
10290 | u32 reg; | |
10291 | int err; | |
10292 | ||
6e1acfa3 PNA |
10293 | err = nft_parse_register(attr, ®); |
10294 | if (err < 0) | |
10295 | return err; | |
10296 | ||
4f16d25c PNA |
10297 | err = nft_validate_register_load(reg, len); |
10298 | if (err < 0) | |
10299 | return err; | |
10300 | ||
10301 | *sreg = reg; | |
10302 | return 0; | |
10303 | } | |
10304 | EXPORT_SYMBOL_GPL(nft_parse_register_load); | |
96518518 | 10305 | |
345023b0 PNA |
10306 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
10307 | enum nft_registers reg, | |
10308 | const struct nft_data *data, | |
10309 | enum nft_data_types type, | |
10310 | unsigned int len) | |
96518518 | 10311 | { |
20a69341 PM |
10312 | int err; |
10313 | ||
96518518 PM |
10314 | switch (reg) { |
10315 | case NFT_REG_VERDICT: | |
58f40ab6 | 10316 | if (type != NFT_DATA_VERDICT) |
96518518 | 10317 | return -EINVAL; |
20a69341 | 10318 | |
58f40ab6 | 10319 | if (data != NULL && |
1ca2e170 PM |
10320 | (data->verdict.code == NFT_GOTO || |
10321 | data->verdict.code == NFT_JUMP)) { | |
10322 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
10323 | if (err < 0) |
10324 | return err; | |
20a69341 PM |
10325 | } |
10326 | ||
96518518 PM |
10327 | return 0; |
10328 | default: | |
49499c3e | 10329 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 10330 | return -EINVAL; |
45d9bcda PM |
10331 | if (len == 0) |
10332 | return -EINVAL; | |
49499c3e | 10333 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 10334 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 10335 | return -ERANGE; |
27e6d201 | 10336 | |
96518518 PM |
10337 | if (data != NULL && type != NFT_DATA_VALUE) |
10338 | return -EINVAL; | |
10339 | return 0; | |
10340 | } | |
10341 | } | |
345023b0 PNA |
10342 | |
10343 | int nft_parse_register_store(const struct nft_ctx *ctx, | |
10344 | const struct nlattr *attr, u8 *dreg, | |
10345 | const struct nft_data *data, | |
10346 | enum nft_data_types type, unsigned int len) | |
10347 | { | |
10348 | int err; | |
10349 | u32 reg; | |
10350 | ||
6e1acfa3 PNA |
10351 | err = nft_parse_register(attr, ®); |
10352 | if (err < 0) | |
10353 | return err; | |
10354 | ||
345023b0 PNA |
10355 | err = nft_validate_register_store(ctx, reg, data, type, len); |
10356 | if (err < 0) | |
10357 | return err; | |
10358 | ||
10359 | *dreg = reg; | |
10360 | return 0; | |
10361 | } | |
10362 | EXPORT_SYMBOL_GPL(nft_parse_register_store); | |
96518518 PM |
10363 | |
10364 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
10365 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
10366 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
10367 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
51d70f18 | 10368 | [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
10369 | }; |
10370 | ||
10371 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
10372 | struct nft_data_desc *desc, const struct nlattr *nla) | |
10373 | { | |
664b0f8c | 10374 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
10375 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
10376 | struct nft_chain *chain; | |
10377 | int err; | |
10378 | ||
8cb08174 JB |
10379 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
10380 | nft_verdict_policy, NULL); | |
96518518 PM |
10381 | if (err < 0) |
10382 | return err; | |
10383 | ||
10384 | if (!tb[NFTA_VERDICT_CODE]) | |
10385 | return -EINVAL; | |
1ca2e170 | 10386 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 10387 | |
1ca2e170 | 10388 | switch (data->verdict.code) { |
e0abdadc | 10389 | default: |
1ca2e170 | 10390 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
10391 | case NF_ACCEPT: |
10392 | case NF_DROP: | |
10393 | case NF_QUEUE: | |
10394 | break; | |
10395 | default: | |
10396 | return -EINVAL; | |
10397 | } | |
954d8297 | 10398 | fallthrough; |
96518518 PM |
10399 | case NFT_CONTINUE: |
10400 | case NFT_BREAK: | |
10401 | case NFT_RETURN: | |
96518518 PM |
10402 | break; |
10403 | case NFT_JUMP: | |
10404 | case NFT_GOTO: | |
51d70f18 PNA |
10405 | if (tb[NFTA_VERDICT_CHAIN]) { |
10406 | chain = nft_chain_lookup(ctx->net, ctx->table, | |
10407 | tb[NFTA_VERDICT_CHAIN], | |
10408 | genmask); | |
10409 | } else if (tb[NFTA_VERDICT_CHAIN_ID]) { | |
95f466d2 | 10410 | chain = nft_chain_lookup_byid(ctx->net, ctx->table, |
51d70f18 PNA |
10411 | tb[NFTA_VERDICT_CHAIN_ID]); |
10412 | if (IS_ERR(chain)) | |
10413 | return PTR_ERR(chain); | |
10414 | } else { | |
96518518 | 10415 | return -EINVAL; |
51d70f18 PNA |
10416 | } |
10417 | ||
96518518 PM |
10418 | if (IS_ERR(chain)) |
10419 | return PTR_ERR(chain); | |
f323d954 | 10420 | if (nft_is_base_chain(chain)) |
96518518 | 10421 | return -EOPNOTSUPP; |
e02f0d39 PNA |
10422 | if (nft_chain_is_bound(chain)) |
10423 | return -EINVAL; | |
f323ef3a PNA |
10424 | if (desc->flags & NFT_DATA_DESC_SETELEM && |
10425 | chain->flags & NFT_CHAIN_BINDING) | |
10426 | return -EINVAL; | |
96518518 | 10427 | |
96518518 | 10428 | chain->use++; |
1ca2e170 | 10429 | data->verdict.chain = chain; |
96518518 | 10430 | break; |
96518518 PM |
10431 | } |
10432 | ||
4c4ed074 | 10433 | desc->len = sizeof(data->verdict); |
341b6941 | 10434 | |
96518518 PM |
10435 | return 0; |
10436 | } | |
10437 | ||
10438 | static void nft_verdict_uninit(const struct nft_data *data) | |
10439 | { | |
d0e2c7de | 10440 | struct nft_chain *chain; |
d0e2c7de | 10441 | |
1ca2e170 | 10442 | switch (data->verdict.code) { |
96518518 PM |
10443 | case NFT_JUMP: |
10444 | case NFT_GOTO: | |
d0e2c7de PNA |
10445 | chain = data->verdict.chain; |
10446 | chain->use--; | |
96518518 PM |
10447 | break; |
10448 | } | |
10449 | } | |
10450 | ||
33d5a7b1 | 10451 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
10452 | { |
10453 | struct nlattr *nest; | |
10454 | ||
ae0be8de | 10455 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
10456 | if (!nest) |
10457 | goto nla_put_failure; | |
10458 | ||
33d5a7b1 | 10459 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
10460 | goto nla_put_failure; |
10461 | ||
33d5a7b1 | 10462 | switch (v->code) { |
96518518 PM |
10463 | case NFT_JUMP: |
10464 | case NFT_GOTO: | |
1ca2e170 | 10465 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 10466 | v->chain->name)) |
96518518 PM |
10467 | goto nla_put_failure; |
10468 | } | |
10469 | nla_nest_end(skb, nest); | |
10470 | return 0; | |
10471 | ||
10472 | nla_put_failure: | |
10473 | return -1; | |
10474 | } | |
10475 | ||
d0a11fc3 | 10476 | static int nft_value_init(const struct nft_ctx *ctx, |
341b6941 PNA |
10477 | struct nft_data *data, struct nft_data_desc *desc, |
10478 | const struct nlattr *nla) | |
96518518 PM |
10479 | { |
10480 | unsigned int len; | |
10481 | ||
10482 | len = nla_len(nla); | |
10483 | if (len == 0) | |
10484 | return -EINVAL; | |
341b6941 | 10485 | if (len > desc->size) |
96518518 | 10486 | return -EOVERFLOW; |
341b6941 PNA |
10487 | if (desc->len) { |
10488 | if (len != desc->len) | |
10489 | return -EINVAL; | |
10490 | } else { | |
10491 | desc->len = len; | |
10492 | } | |
96518518 | 10493 | |
d0a11fc3 | 10494 | nla_memcpy(data->data, nla, len); |
341b6941 | 10495 | |
96518518 PM |
10496 | return 0; |
10497 | } | |
10498 | ||
10499 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
10500 | unsigned int len) | |
10501 | { | |
10502 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
10503 | } | |
10504 | ||
10505 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 10506 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
10507 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
10508 | }; | |
10509 | ||
10510 | /** | |
10511 | * nft_data_init - parse nf_tables data netlink attributes | |
10512 | * | |
10513 | * @ctx: context of the expression using the data | |
10514 | * @data: destination struct nft_data | |
10515 | * @desc: data description | |
10516 | * @nla: netlink attribute containing data | |
10517 | * | |
10518 | * Parse the netlink data attributes and initialize a struct nft_data. | |
10519 | * The type and length of data are returned in the data description. | |
10520 | * | |
10521 | * The caller can indicate that it only wants to accept data of type | |
10522 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
10523 | */ | |
341b6941 | 10524 | int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
96518518 PM |
10525 | struct nft_data_desc *desc, const struct nlattr *nla) |
10526 | { | |
10527 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
10528 | int err; | |
10529 | ||
341b6941 PNA |
10530 | if (WARN_ON_ONCE(!desc->size)) |
10531 | return -EINVAL; | |
10532 | ||
8cb08174 JB |
10533 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
10534 | nft_data_policy, NULL); | |
96518518 PM |
10535 | if (err < 0) |
10536 | return err; | |
10537 | ||
341b6941 PNA |
10538 | if (tb[NFTA_DATA_VALUE]) { |
10539 | if (desc->type != NFT_DATA_VALUE) | |
10540 | return -EINVAL; | |
10541 | ||
10542 | err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]); | |
10543 | } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) { | |
10544 | if (desc->type != NFT_DATA_VERDICT) | |
10545 | return -EINVAL; | |
10546 | ||
10547 | err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
10548 | } else { | |
10549 | err = -EINVAL; | |
10550 | } | |
10551 | ||
10552 | return err; | |
96518518 PM |
10553 | } |
10554 | EXPORT_SYMBOL_GPL(nft_data_init); | |
10555 | ||
10556 | /** | |
59105446 | 10557 | * nft_data_release - release a nft_data item |
96518518 PM |
10558 | * |
10559 | * @data: struct nft_data to release | |
10560 | * @type: type of data | |
10561 | * | |
10562 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
10563 | * all others need to be released by calling this function. | |
10564 | */ | |
59105446 | 10565 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 10566 | { |
960bd2c2 | 10567 | if (type < NFT_DATA_VERDICT) |
96518518 | 10568 | return; |
960bd2c2 | 10569 | switch (type) { |
96518518 PM |
10570 | case NFT_DATA_VERDICT: |
10571 | return nft_verdict_uninit(data); | |
10572 | default: | |
10573 | WARN_ON(1); | |
10574 | } | |
10575 | } | |
59105446 | 10576 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
10577 | |
10578 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
10579 | enum nft_data_types type, unsigned int len) | |
10580 | { | |
10581 | struct nlattr *nest; | |
10582 | int err; | |
10583 | ||
ae0be8de | 10584 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
10585 | if (nest == NULL) |
10586 | return -1; | |
10587 | ||
10588 | switch (type) { | |
10589 | case NFT_DATA_VALUE: | |
10590 | err = nft_value_dump(skb, data, len); | |
10591 | break; | |
10592 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 10593 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
10594 | break; |
10595 | default: | |
10596 | err = -EINVAL; | |
10597 | WARN_ON(1); | |
10598 | } | |
10599 | ||
10600 | nla_nest_end(skb, nest); | |
10601 | return err; | |
10602 | } | |
10603 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
10604 | ||
5ebe0b0e PNA |
10605 | int __nft_release_basechain(struct nft_ctx *ctx) |
10606 | { | |
10607 | struct nft_rule *rule, *nr; | |
10608 | ||
fa5950e4 FW |
10609 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
10610 | return 0; | |
5ebe0b0e | 10611 | |
c974a3a3 | 10612 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
10613 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
10614 | list_del(&rule->list); | |
10615 | ctx->chain->use--; | |
bb7b40ae | 10616 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 10617 | } |
1b2470e5 | 10618 | nft_chain_del(ctx->chain); |
5ebe0b0e | 10619 | ctx->table->use--; |
43a605f2 | 10620 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
10621 | |
10622 | return 0; | |
10623 | } | |
10624 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
10625 | ||
00dfe9be PNA |
10626 | static void __nft_release_hook(struct net *net, struct nft_table *table) |
10627 | { | |
6069da44 | 10628 | struct nft_flowtable *flowtable; |
00dfe9be PNA |
10629 | struct nft_chain *chain; |
10630 | ||
10631 | list_for_each_entry(chain, &table->chains, list) | |
f9a43007 | 10632 | __nf_tables_unregister_hook(net, table, chain, true); |
6069da44 | 10633 | list_for_each_entry(flowtable, &table->flowtables, list) |
f9a43007 PNA |
10634 | __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list, |
10635 | true); | |
00dfe9be PNA |
10636 | } |
10637 | ||
767d1216 PNA |
10638 | static void __nft_release_hooks(struct net *net) |
10639 | { | |
d59d2f82 | 10640 | struct nftables_pernet *nft_net = nft_pernet(net); |
767d1216 | 10641 | struct nft_table *table; |
767d1216 | 10642 | |
0854db2a | 10643 | list_for_each_entry(table, &nft_net->tables, list) { |
2888b080 PNA |
10644 | if (nft_table_has_owner(table)) |
10645 | continue; | |
10646 | ||
00dfe9be | 10647 | __nft_release_hook(net, table); |
2888b080 | 10648 | } |
767d1216 PNA |
10649 | } |
10650 | ||
fd020332 | 10651 | static void __nft_release_table(struct net *net, struct nft_table *table) |
df05ef87 | 10652 | { |
3b49e2e9 | 10653 | struct nft_flowtable *flowtable, *nf; |
df05ef87 | 10654 | struct nft_chain *chain, *nc; |
e5009240 | 10655 | struct nft_object *obj, *ne; |
df05ef87 PNA |
10656 | struct nft_rule *rule, *nr; |
10657 | struct nft_set *set, *ns; | |
10658 | struct nft_ctx ctx = { | |
10659 | .net = net, | |
43a605f2 | 10660 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
10661 | }; |
10662 | ||
fd020332 PNA |
10663 | ctx.family = table->family; |
10664 | ctx.table = table; | |
10665 | list_for_each_entry(chain, &table->chains, list) { | |
10666 | ctx.chain = chain; | |
10667 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
10668 | list_del(&rule->list); | |
10669 | chain->use--; | |
10670 | nf_tables_rule_release(&ctx, rule); | |
df05ef87 | 10671 | } |
df05ef87 | 10672 | } |
fd020332 PNA |
10673 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
10674 | list_del(&flowtable->list); | |
10675 | table->use--; | |
10676 | nf_tables_flowtable_destroy(flowtable); | |
10677 | } | |
10678 | list_for_each_entry_safe(set, ns, &table->sets, list) { | |
10679 | list_del(&set->list); | |
10680 | table->use--; | |
628bd3e4 PNA |
10681 | if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) |
10682 | nft_map_deactivate(&ctx, set); | |
10683 | ||
fd020332 PNA |
10684 | nft_set_destroy(&ctx, set); |
10685 | } | |
10686 | list_for_each_entry_safe(obj, ne, &table->objects, list) { | |
10687 | nft_obj_del(obj); | |
10688 | table->use--; | |
10689 | nft_obj_destroy(&ctx, obj); | |
10690 | } | |
10691 | list_for_each_entry_safe(chain, nc, &table->chains, list) { | |
10692 | ctx.chain = chain; | |
10693 | nft_chain_del(chain); | |
10694 | table->use--; | |
10695 | nf_tables_chain_destroy(&ctx); | |
10696 | } | |
fd020332 PNA |
10697 | nf_tables_table_destroy(&ctx); |
10698 | } | |
10699 | ||
bd1777b3 | 10700 | static void __nft_release_tables(struct net *net) |
fd020332 | 10701 | { |
d59d2f82 | 10702 | struct nftables_pernet *nft_net = nft_pernet(net); |
fd020332 PNA |
10703 | struct nft_table *table, *nt; |
10704 | ||
0854db2a | 10705 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
bd1777b3 | 10706 | if (nft_table_has_owner(table)) |
6001a930 PNA |
10707 | continue; |
10708 | ||
a499b03b FW |
10709 | list_del(&table->list); |
10710 | ||
fd020332 | 10711 | __nft_release_table(net, table); |
6001a930 PNA |
10712 | } |
10713 | } | |
10714 | ||
10715 | static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event, | |
10716 | void *ptr) | |
10717 | { | |
a499b03b | 10718 | struct nft_table *table, *to_delete[8]; |
0854db2a | 10719 | struct nftables_pernet *nft_net; |
6001a930 | 10720 | struct netlink_notify *n = ptr; |
6001a930 | 10721 | struct net *net = n->net; |
a499b03b FW |
10722 | unsigned int deleted; |
10723 | bool restart = false; | |
6001a930 PNA |
10724 | |
10725 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER) | |
10726 | return NOTIFY_DONE; | |
10727 | ||
d59d2f82 | 10728 | nft_net = nft_pernet(net); |
a499b03b | 10729 | deleted = 0; |
0854db2a | 10730 | mutex_lock(&nft_net->commit_mutex); |
d4bc8271 PNA |
10731 | if (!list_empty(&nf_tables_destroy_list)) |
10732 | rcu_barrier(); | |
a499b03b | 10733 | again: |
0854db2a | 10734 | list_for_each_entry(table, &nft_net->tables, list) { |
6001a930 PNA |
10735 | if (nft_table_has_owner(table) && |
10736 | n->portid == table->nlpid) { | |
10737 | __nft_release_hook(net, table); | |
a499b03b FW |
10738 | list_del_rcu(&table->list); |
10739 | to_delete[deleted++] = table; | |
10740 | if (deleted >= ARRAY_SIZE(to_delete)) | |
10741 | break; | |
6001a930 PNA |
10742 | } |
10743 | } | |
a499b03b FW |
10744 | if (deleted) { |
10745 | restart = deleted >= ARRAY_SIZE(to_delete); | |
6001a930 | 10746 | synchronize_rcu(); |
a499b03b FW |
10747 | while (deleted) |
10748 | __nft_release_table(net, to_delete[--deleted]); | |
10749 | ||
10750 | if (restart) | |
10751 | goto again; | |
6001a930 | 10752 | } |
0854db2a | 10753 | mutex_unlock(&nft_net->commit_mutex); |
6001a930 PNA |
10754 | |
10755 | return NOTIFY_DONE; | |
df05ef87 PNA |
10756 | } |
10757 | ||
6001a930 PNA |
10758 | static struct notifier_block nft_nl_notifier = { |
10759 | .notifier_call = nft_rcv_nl_event, | |
10760 | }; | |
10761 | ||
dd4cbef7 PNA |
10762 | static int __net_init nf_tables_init_net(struct net *net) |
10763 | { | |
d59d2f82 | 10764 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10765 | |
10766 | INIT_LIST_HEAD(&nft_net->tables); | |
10767 | INIT_LIST_HEAD(&nft_net->commit_list); | |
10768 | INIT_LIST_HEAD(&nft_net->module_list); | |
10769 | INIT_LIST_HEAD(&nft_net->notify_list); | |
10770 | mutex_init(&nft_net->commit_mutex); | |
10771 | nft_net->base_seq = 1; | |
a654de8f | 10772 | |
dd4cbef7 PNA |
10773 | return 0; |
10774 | } | |
10775 | ||
767d1216 PNA |
10776 | static void __net_exit nf_tables_pre_exit_net(struct net *net) |
10777 | { | |
3923b1e4 PNA |
10778 | struct nftables_pernet *nft_net = nft_pernet(net); |
10779 | ||
10780 | mutex_lock(&nft_net->commit_mutex); | |
767d1216 | 10781 | __nft_release_hooks(net); |
3923b1e4 | 10782 | mutex_unlock(&nft_net->commit_mutex); |
767d1216 PNA |
10783 | } |
10784 | ||
dd4cbef7 PNA |
10785 | static void __net_exit nf_tables_exit_net(struct net *net) |
10786 | { | |
d59d2f82 | 10787 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10788 | |
10789 | mutex_lock(&nft_net->commit_mutex); | |
03c1f1ef SY |
10790 | if (!list_empty(&nft_net->commit_list) || |
10791 | !list_empty(&nft_net->module_list)) | |
c0391b6a | 10792 | __nf_tables_abort(net, NFNL_ABORT_NONE); |
bd1777b3 | 10793 | __nft_release_tables(net); |
0854db2a FW |
10794 | mutex_unlock(&nft_net->commit_mutex); |
10795 | WARN_ON_ONCE(!list_empty(&nft_net->tables)); | |
10796 | WARN_ON_ONCE(!list_empty(&nft_net->module_list)); | |
10797 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); | |
dd4cbef7 PNA |
10798 | } |
10799 | ||
99633ab2 | 10800 | static struct pernet_operations nf_tables_net_ops = { |
767d1216 PNA |
10801 | .init = nf_tables_init_net, |
10802 | .pre_exit = nf_tables_pre_exit_net, | |
10803 | .exit = nf_tables_exit_net, | |
0854db2a FW |
10804 | .id = &nf_tables_net_id, |
10805 | .size = sizeof(struct nftables_pernet), | |
99633ab2 PNA |
10806 | }; |
10807 | ||
96518518 PM |
10808 | static int __init nf_tables_module_init(void) |
10809 | { | |
10810 | int err; | |
10811 | ||
d209df3e FW |
10812 | err = register_pernet_subsys(&nf_tables_net_ops); |
10813 | if (err < 0) | |
10814 | return err; | |
10815 | ||
10816 | err = nft_chain_filter_init(); | |
10817 | if (err < 0) | |
6001a930 | 10818 | goto err_chain_filter; |
02c7b25e | 10819 | |
96518518 PM |
10820 | err = nf_tables_core_module_init(); |
10821 | if (err < 0) | |
6001a930 | 10822 | goto err_core_module; |
96518518 | 10823 | |
d209df3e | 10824 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 10825 | if (err < 0) |
6001a930 | 10826 | goto err_netdev_notifier; |
96518518 | 10827 | |
4d44175a FW |
10828 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
10829 | if (err < 0) | |
6001a930 | 10830 | goto err_rht_objname; |
4d44175a | 10831 | |
06d392cb | 10832 | err = nft_offload_init(); |
10833 | if (err < 0) | |
6001a930 PNA |
10834 | goto err_offload; |
10835 | ||
10836 | err = netlink_register_notifier(&nft_nl_notifier); | |
10837 | if (err < 0) | |
10838 | goto err_netlink_notifier; | |
06d392cb | 10839 | |
d209df3e FW |
10840 | /* must be last */ |
10841 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
10842 | if (err < 0) | |
6001a930 | 10843 | goto err_nfnl_subsys; |
3b49e2e9 | 10844 | |
c1deb065 | 10845 | nft_chain_route_init(); |
3474a2c6 | 10846 | |
d209df3e | 10847 | return err; |
6001a930 PNA |
10848 | |
10849 | err_nfnl_subsys: | |
10850 | netlink_unregister_notifier(&nft_nl_notifier); | |
10851 | err_netlink_notifier: | |
06d392cb | 10852 | nft_offload_exit(); |
6001a930 | 10853 | err_offload: |
4d44175a | 10854 | rhltable_destroy(&nft_objname_ht); |
6001a930 | 10855 | err_rht_objname: |
d209df3e | 10856 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
6001a930 | 10857 | err_netdev_notifier: |
96518518 | 10858 | nf_tables_core_module_exit(); |
6001a930 | 10859 | err_core_module: |
d209df3e | 10860 | nft_chain_filter_fini(); |
6001a930 | 10861 | err_chain_filter: |
d209df3e | 10862 | unregister_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
10863 | return err; |
10864 | } | |
10865 | ||
10866 | static void __exit nf_tables_module_exit(void) | |
10867 | { | |
10868 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
6001a930 | 10869 | netlink_unregister_notifier(&nft_nl_notifier); |
06d392cb | 10870 | nft_offload_exit(); |
3b49e2e9 | 10871 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 10872 | nft_chain_filter_fini(); |
c1deb065 | 10873 | nft_chain_route_fini(); |
71ad00c5 | 10874 | unregister_pernet_subsys(&nf_tables_net_ops); |
0935d558 | 10875 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 10876 | rcu_barrier(); |
4d44175a | 10877 | rhltable_destroy(&nft_objname_ht); |
96518518 | 10878 | nf_tables_core_module_exit(); |
96518518 PM |
10879 | } |
10880 | ||
10881 | module_init(nf_tables_module_init); | |
10882 | module_exit(nf_tables_module_exit); | |
10883 | ||
10884 | MODULE_LICENSE("GPL"); | |
10885 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
10886 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |