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 | ||
a654de8f PNA |
105 | static void nft_validate_state_update(struct net *net, u8 new_validate_state) |
106 | { | |
d59d2f82 | 107 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
108 | |
109 | switch (nft_net->validate_state) { | |
a654de8f PNA |
110 | case NFT_VALIDATE_SKIP: |
111 | WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO); | |
112 | break; | |
113 | case NFT_VALIDATE_NEED: | |
114 | break; | |
115 | case NFT_VALIDATE_DO: | |
116 | if (new_validate_state == NFT_VALIDATE_NEED) | |
117 | return; | |
118 | } | |
119 | ||
0854db2a | 120 | nft_net->validate_state = new_validate_state; |
a654de8f | 121 | } |
0935d558 FW |
122 | static void nf_tables_trans_destroy_work(struct work_struct *w); |
123 | static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work); | |
a654de8f | 124 | |
7c95f6d8 | 125 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 126 | struct net *net, |
7c95f6d8 PNA |
127 | const struct sk_buff *skb, |
128 | const struct nlmsghdr *nlh, | |
36596dad | 129 | u8 family, |
7c95f6d8 PNA |
130 | struct nft_table *table, |
131 | struct nft_chain *chain, | |
132 | const struct nlattr * const *nla) | |
133 | { | |
633c9a84 | 134 | ctx->net = net; |
36596dad | 135 | ctx->family = family; |
26b2f552 | 136 | ctx->level = 0; |
128ad332 PNA |
137 | ctx->table = table; |
138 | ctx->chain = chain; | |
139 | ctx->nla = nla; | |
140 | ctx->portid = NETLINK_CB(skb).portid; | |
141 | ctx->report = nlmsg_report(nlh); | |
c9626a2c | 142 | ctx->flags = nlh->nlmsg_flags; |
128ad332 | 143 | ctx->seq = nlh->nlmsg_seq; |
7c95f6d8 PNA |
144 | } |
145 | ||
8411b644 PNA |
146 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
147 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
148 | { |
149 | struct nft_trans *trans; | |
150 | ||
8411b644 | 151 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
152 | if (trans == NULL) |
153 | return NULL; | |
154 | ||
58007785 | 155 | INIT_LIST_HEAD(&trans->list); |
b380e5c7 | 156 | trans->msg_type = msg_type; |
1081d11b PNA |
157 | trans->ctx = *ctx; |
158 | ||
159 | return trans; | |
160 | } | |
161 | ||
8411b644 PNA |
162 | static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
163 | int msg_type, u32 size) | |
164 | { | |
165 | return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); | |
166 | } | |
167 | ||
1081d11b PNA |
168 | static void nft_trans_destroy(struct nft_trans *trans) |
169 | { | |
170 | list_del(&trans->list); | |
171 | kfree(trans); | |
172 | } | |
173 | ||
f6ac8585 PNA |
174 | static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set) |
175 | { | |
0854db2a | 176 | struct nftables_pernet *nft_net; |
f6ac8585 PNA |
177 | struct net *net = ctx->net; |
178 | struct nft_trans *trans; | |
179 | ||
180 | if (!nft_set_is_anonymous(set)) | |
181 | return; | |
182 | ||
d59d2f82 | 183 | nft_net = nft_pernet(net); |
0854db2a | 184 | list_for_each_entry_reverse(trans, &nft_net->commit_list, list) { |
6a0a8d10 PNA |
185 | switch (trans->msg_type) { |
186 | case NFT_MSG_NEWSET: | |
187 | if (nft_trans_set(trans) == set) | |
188 | nft_trans_set_bound(trans) = true; | |
189 | break; | |
190 | case NFT_MSG_NEWSETELEM: | |
191 | if (nft_trans_elem_set(trans) == set) | |
192 | nft_trans_elem_set_bound(trans) = true; | |
f6ac8585 PNA |
193 | break; |
194 | } | |
195 | } | |
196 | } | |
197 | ||
d54725cd PNA |
198 | static int nft_netdev_register_hooks(struct net *net, |
199 | struct list_head *hook_list) | |
200 | { | |
201 | struct nft_hook *hook; | |
202 | int err, j; | |
203 | ||
204 | j = 0; | |
205 | list_for_each_entry(hook, hook_list, list) { | |
206 | err = nf_register_net_hook(net, &hook->ops); | |
207 | if (err < 0) | |
208 | goto err_register; | |
209 | ||
210 | j++; | |
211 | } | |
212 | return 0; | |
213 | ||
214 | err_register: | |
215 | list_for_each_entry(hook, hook_list, list) { | |
216 | if (j-- <= 0) | |
217 | break; | |
218 | ||
219 | nf_unregister_net_hook(net, &hook->ops); | |
220 | } | |
221 | return err; | |
222 | } | |
223 | ||
224 | static void nft_netdev_unregister_hooks(struct net *net, | |
f9a43007 PNA |
225 | struct list_head *hook_list, |
226 | bool release_netdev) | |
d54725cd | 227 | { |
f9a43007 | 228 | struct nft_hook *hook, *next; |
d54725cd | 229 | |
f9a43007 | 230 | list_for_each_entry_safe(hook, next, hook_list, list) { |
d54725cd | 231 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
232 | if (release_netdev) { |
233 | list_del(&hook->list); | |
234 | kfree_rcu(hook, rcu); | |
235 | } | |
236 | } | |
d54725cd PNA |
237 | } |
238 | ||
c974a3a3 PNA |
239 | static int nf_tables_register_hook(struct net *net, |
240 | const struct nft_table *table, | |
241 | struct nft_chain *chain) | |
d8ee8f7c | 242 | { |
d54725cd | 243 | struct nft_base_chain *basechain; |
a37061a6 | 244 | const struct nf_hook_ops *ops; |
ae6153b5 | 245 | |
d8ee8f7c | 246 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 247 | !nft_is_base_chain(chain)) |
d8ee8f7c PNA |
248 | return 0; |
249 | ||
4e25ceb8 FW |
250 | basechain = nft_base_chain(chain); |
251 | ops = &basechain->ops; | |
ae6153b5 | 252 | |
4e25ceb8 FW |
253 | if (basechain->type->ops_register) |
254 | return basechain->type->ops_register(net, ops); | |
255 | ||
d3519cb8 | 256 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
1e9451cb FW |
257 | return nft_netdev_register_hooks(net, &basechain->hook_list); |
258 | ||
259 | return nf_register_net_hook(net, &basechain->ops); | |
d8ee8f7c PNA |
260 | } |
261 | ||
f9a43007 PNA |
262 | static void __nf_tables_unregister_hook(struct net *net, |
263 | const struct nft_table *table, | |
264 | struct nft_chain *chain, | |
265 | bool release_netdev) | |
c5598794 | 266 | { |
d54725cd | 267 | struct nft_base_chain *basechain; |
4e25ceb8 FW |
268 | const struct nf_hook_ops *ops; |
269 | ||
d8ee8f7c | 270 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 271 | !nft_is_base_chain(chain)) |
d8ee8f7c | 272 | return; |
4e25ceb8 FW |
273 | basechain = nft_base_chain(chain); |
274 | ops = &basechain->ops; | |
d8ee8f7c | 275 | |
4e25ceb8 FW |
276 | if (basechain->type->ops_unregister) |
277 | return basechain->type->ops_unregister(net, ops); | |
278 | ||
d3519cb8 | 279 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
f9a43007 PNA |
280 | nft_netdev_unregister_hooks(net, &basechain->hook_list, |
281 | release_netdev); | |
1e9451cb FW |
282 | else |
283 | nf_unregister_net_hook(net, &basechain->ops); | |
c5598794 AB |
284 | } |
285 | ||
f9a43007 PNA |
286 | static void nf_tables_unregister_hook(struct net *net, |
287 | const struct nft_table *table, | |
288 | struct nft_chain *chain) | |
289 | { | |
290 | return __nf_tables_unregister_hook(net, table, chain, false); | |
291 | } | |
292 | ||
0854db2a FW |
293 | static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans) |
294 | { | |
d59d2f82 | 295 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a | 296 | |
0854db2a FW |
297 | list_add_tail(&trans->list, &nft_net->commit_list); |
298 | } | |
299 | ||
ee01d542 AB |
300 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
301 | { | |
302 | struct nft_trans *trans; | |
303 | ||
304 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
305 | if (trans == NULL) | |
306 | return -ENOMEM; | |
307 | ||
308 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 309 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 | 310 | |
0854db2a | 311 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
312 | return 0; |
313 | } | |
314 | ||
315 | static int nft_deltable(struct nft_ctx *ctx) | |
316 | { | |
317 | int err; | |
318 | ||
319 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
320 | if (err < 0) | |
321 | return err; | |
322 | ||
f2a6d766 | 323 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
324 | return err; |
325 | } | |
326 | ||
66293c46 | 327 | static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
ee01d542 AB |
328 | { |
329 | struct nft_trans *trans; | |
330 | ||
331 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
332 | if (trans == NULL) | |
66293c46 | 333 | return ERR_PTR(-ENOMEM); |
ee01d542 | 334 | |
74cccc3d | 335 | if (msg_type == NFT_MSG_NEWCHAIN) { |
664b0f8c | 336 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 | 337 | |
74cccc3d PNA |
338 | if (ctx->nla[NFTA_CHAIN_ID]) { |
339 | nft_trans_chain_id(trans) = | |
340 | ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID])); | |
341 | } | |
342 | } | |
343 | ||
0854db2a | 344 | nft_trans_commit_list_add_tail(ctx->net, trans); |
66293c46 | 345 | return trans; |
ee01d542 AB |
346 | } |
347 | ||
348 | static int nft_delchain(struct nft_ctx *ctx) | |
349 | { | |
66293c46 | 350 | struct nft_trans *trans; |
ee01d542 | 351 | |
66293c46 FW |
352 | trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
353 | if (IS_ERR(trans)) | |
354 | return PTR_ERR(trans); | |
ee01d542 AB |
355 | |
356 | ctx->table->use--; | |
664b0f8c | 357 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 | 358 | |
66293c46 | 359 | return 0; |
ee01d542 AB |
360 | } |
361 | ||
bb7b40ae PNA |
362 | static void nft_rule_expr_activate(const struct nft_ctx *ctx, |
363 | struct nft_rule *rule) | |
364 | { | |
365 | struct nft_expr *expr; | |
366 | ||
367 | expr = nft_expr_first(rule); | |
31cc578a | 368 | while (nft_expr_more(rule, expr)) { |
bb7b40ae PNA |
369 | if (expr->ops->activate) |
370 | expr->ops->activate(ctx, expr); | |
371 | ||
372 | expr = nft_expr_next(expr); | |
373 | } | |
374 | } | |
375 | ||
376 | static void nft_rule_expr_deactivate(const struct nft_ctx *ctx, | |
f6ac8585 PNA |
377 | struct nft_rule *rule, |
378 | enum nft_trans_phase phase) | |
bb7b40ae PNA |
379 | { |
380 | struct nft_expr *expr; | |
381 | ||
382 | expr = nft_expr_first(rule); | |
31cc578a | 383 | while (nft_expr_more(rule, expr)) { |
bb7b40ae | 384 | if (expr->ops->deactivate) |
f6ac8585 | 385 | expr->ops->deactivate(ctx, expr, phase); |
bb7b40ae PNA |
386 | |
387 | expr = nft_expr_next(expr); | |
388 | } | |
389 | } | |
390 | ||
ee01d542 AB |
391 | static int |
392 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
393 | { | |
394 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
395 | if (nft_is_active_next(ctx->net, rule)) { |
396 | nft_deactivate_next(ctx->net, rule); | |
ee01d542 AB |
397 | ctx->chain->use--; |
398 | return 0; | |
399 | } | |
400 | return -ENOENT; | |
401 | } | |
402 | ||
403 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
404 | struct nft_rule *rule) | |
405 | { | |
406 | struct nft_trans *trans; | |
407 | ||
408 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
409 | if (trans == NULL) | |
410 | return NULL; | |
411 | ||
1a94e38d PNA |
412 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
413 | nft_trans_rule_id(trans) = | |
414 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
415 | } | |
ee01d542 | 416 | nft_trans_rule(trans) = rule; |
0854db2a | 417 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
418 | |
419 | return trans; | |
420 | } | |
421 | ||
422 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
423 | { | |
63b48c73 | 424 | struct nft_flow_rule *flow; |
ee01d542 AB |
425 | struct nft_trans *trans; |
426 | int err; | |
427 | ||
428 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
429 | if (trans == NULL) | |
430 | return -ENOMEM; | |
431 | ||
63b48c73 PNA |
432 | if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
433 | flow = nft_flow_rule_create(ctx->net, rule); | |
434 | if (IS_ERR(flow)) { | |
435 | nft_trans_destroy(trans); | |
436 | return PTR_ERR(flow); | |
437 | } | |
438 | ||
439 | nft_trans_flow_rule(trans) = flow; | |
440 | } | |
441 | ||
ee01d542 AB |
442 | err = nf_tables_delrule_deactivate(ctx, rule); |
443 | if (err < 0) { | |
444 | nft_trans_destroy(trans); | |
445 | return err; | |
446 | } | |
f6ac8585 | 447 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE); |
ee01d542 AB |
448 | |
449 | return 0; | |
450 | } | |
451 | ||
452 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
453 | { | |
454 | struct nft_rule *rule; | |
455 | int err; | |
456 | ||
457 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
23b7ca4f PNA |
458 | if (!nft_is_active_next(ctx->net, rule)) |
459 | continue; | |
460 | ||
ee01d542 AB |
461 | err = nft_delrule(ctx, rule); |
462 | if (err < 0) | |
463 | return err; | |
464 | } | |
465 | return 0; | |
466 | } | |
467 | ||
123b9961 PNA |
468 | static int __nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
469 | struct nft_set *set, | |
470 | const struct nft_set_desc *desc) | |
ee01d542 AB |
471 | { |
472 | struct nft_trans *trans; | |
473 | ||
474 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
475 | if (trans == NULL) | |
476 | return -ENOMEM; | |
477 | ||
123b9961 | 478 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] && !desc) { |
ee01d542 AB |
479 | nft_trans_set_id(trans) = |
480 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 481 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
482 | } |
483 | nft_trans_set(trans) = set; | |
123b9961 PNA |
484 | if (desc) { |
485 | nft_trans_set_update(trans) = true; | |
486 | nft_trans_set_gc_int(trans) = desc->gc_int; | |
487 | nft_trans_set_timeout(trans) = desc->timeout; | |
488 | } | |
0854db2a | 489 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
490 | |
491 | return 0; | |
492 | } | |
493 | ||
123b9961 PNA |
494 | static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
495 | struct nft_set *set) | |
496 | { | |
497 | return __nft_trans_set_add(ctx, msg_type, set, NULL); | |
498 | } | |
499 | ||
cd5125d8 | 500 | static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set) |
ee01d542 AB |
501 | { |
502 | int err; | |
503 | ||
504 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
505 | if (err < 0) | |
506 | return err; | |
507 | ||
37a9cc52 | 508 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
509 | ctx->table->use--; |
510 | ||
511 | return err; | |
512 | } | |
513 | ||
e5009240 PNA |
514 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
515 | struct nft_object *obj) | |
516 | { | |
517 | struct nft_trans *trans; | |
518 | ||
519 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
520 | if (trans == NULL) | |
521 | return -ENOMEM; | |
522 | ||
523 | if (msg_type == NFT_MSG_NEWOBJ) | |
524 | nft_activate_next(ctx->net, obj); | |
525 | ||
526 | nft_trans_obj(trans) = obj; | |
0854db2a | 527 | nft_trans_commit_list_add_tail(ctx->net, trans); |
e5009240 PNA |
528 | |
529 | return 0; | |
530 | } | |
531 | ||
532 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
533 | { | |
534 | int err; | |
535 | ||
536 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
537 | if (err < 0) | |
538 | return err; | |
539 | ||
540 | nft_deactivate_next(ctx->net, obj); | |
541 | ctx->table->use--; | |
542 | ||
543 | return err; | |
544 | } | |
545 | ||
3b49e2e9 PNA |
546 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
547 | struct nft_flowtable *flowtable) | |
548 | { | |
549 | struct nft_trans *trans; | |
550 | ||
551 | trans = nft_trans_alloc(ctx, msg_type, | |
552 | sizeof(struct nft_trans_flowtable)); | |
553 | if (trans == NULL) | |
554 | return -ENOMEM; | |
555 | ||
556 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
557 | nft_activate_next(ctx->net, flowtable); | |
558 | ||
2c9e4559 | 559 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); |
3b49e2e9 | 560 | nft_trans_flowtable(trans) = flowtable; |
0854db2a | 561 | nft_trans_commit_list_add_tail(ctx->net, trans); |
3b49e2e9 PNA |
562 | |
563 | return 0; | |
564 | } | |
565 | ||
566 | static int nft_delflowtable(struct nft_ctx *ctx, | |
567 | struct nft_flowtable *flowtable) | |
568 | { | |
569 | int err; | |
570 | ||
571 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
572 | if (err < 0) | |
573 | return err; | |
574 | ||
575 | nft_deactivate_next(ctx->net, flowtable); | |
576 | ctx->table->use--; | |
577 | ||
578 | return err; | |
579 | } | |
580 | ||
34cc9e52 PNA |
581 | static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg) |
582 | { | |
583 | int i; | |
584 | ||
585 | for (i = track->regs[dreg].num_reg; i > 0; i--) | |
586 | __nft_reg_track_cancel(track, dreg - i); | |
587 | } | |
588 | ||
589 | static void __nft_reg_track_update(struct nft_regs_track *track, | |
590 | const struct nft_expr *expr, | |
591 | u8 dreg, u8 num_reg) | |
592 | { | |
593 | track->regs[dreg].selector = expr; | |
594 | track->regs[dreg].bitwise = NULL; | |
595 | track->regs[dreg].num_reg = num_reg; | |
596 | } | |
597 | ||
598 | void nft_reg_track_update(struct nft_regs_track *track, | |
599 | const struct nft_expr *expr, u8 dreg, u8 len) | |
600 | { | |
601 | unsigned int regcount; | |
602 | int i; | |
603 | ||
604 | __nft_reg_track_clobber(track, dreg); | |
605 | ||
606 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
607 | for (i = 0; i < regcount; i++, dreg++) | |
608 | __nft_reg_track_update(track, expr, dreg, i); | |
609 | } | |
610 | EXPORT_SYMBOL_GPL(nft_reg_track_update); | |
611 | ||
612 | void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len) | |
613 | { | |
614 | unsigned int regcount; | |
615 | int i; | |
616 | ||
617 | __nft_reg_track_clobber(track, dreg); | |
618 | ||
619 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
620 | for (i = 0; i < regcount; i++, dreg++) | |
621 | __nft_reg_track_cancel(track, dreg); | |
622 | } | |
623 | EXPORT_SYMBOL_GPL(nft_reg_track_cancel); | |
624 | ||
625 | void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) | |
626 | { | |
627 | track->regs[dreg].selector = NULL; | |
628 | track->regs[dreg].bitwise = NULL; | |
629 | track->regs[dreg].num_reg = 0; | |
630 | } | |
631 | EXPORT_SYMBOL_GPL(__nft_reg_track_cancel); | |
632 | ||
96518518 PM |
633 | /* |
634 | * Tables | |
635 | */ | |
636 | ||
36596dad | 637 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 638 | const struct nlattr *nla, |
6001a930 | 639 | u8 family, u8 genmask, u32 nlpid) |
96518518 | 640 | { |
0854db2a | 641 | struct nftables_pernet *nft_net; |
96518518 PM |
642 | struct nft_table *table; |
643 | ||
cac20fcd PNA |
644 | if (nla == NULL) |
645 | return ERR_PTR(-EINVAL); | |
646 | ||
d59d2f82 | 647 | nft_net = nft_pernet(net); |
0854db2a FW |
648 | list_for_each_entry_rcu(table, &nft_net->tables, list, |
649 | lockdep_is_held(&nft_net->commit_mutex)) { | |
f2a6d766 | 650 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 651 | table->family == family && |
6001a930 PNA |
652 | nft_active_genmask(table, genmask)) { |
653 | if (nft_table_has_owner(table) && | |
53479909 | 654 | nlpid && table->nlpid != nlpid) |
6001a930 PNA |
655 | return ERR_PTR(-EPERM); |
656 | ||
96518518 | 657 | return table; |
6001a930 | 658 | } |
96518518 | 659 | } |
cac20fcd PNA |
660 | |
661 | return ERR_PTR(-ENOENT); | |
96518518 PM |
662 | } |
663 | ||
3ecbfd65 HS |
664 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
665 | const struct nlattr *nla, | |
e31f072f | 666 | u8 genmask, u32 nlpid) |
3ecbfd65 | 667 | { |
0854db2a | 668 | struct nftables_pernet *nft_net; |
3ecbfd65 HS |
669 | struct nft_table *table; |
670 | ||
d59d2f82 | 671 | nft_net = nft_pernet(net); |
0854db2a | 672 | list_for_each_entry(table, &nft_net->tables, list) { |
3ecbfd65 | 673 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && |
e31f072f PNA |
674 | nft_active_genmask(table, genmask)) { |
675 | if (nft_table_has_owner(table) && | |
676 | nlpid && table->nlpid != nlpid) | |
677 | return ERR_PTR(-EPERM); | |
678 | ||
3ecbfd65 | 679 | return table; |
e31f072f | 680 | } |
3ecbfd65 | 681 | } |
3ecbfd65 HS |
682 | |
683 | return ERR_PTR(-ENOENT); | |
684 | } | |
685 | ||
96518518 PM |
686 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
687 | { | |
688 | return ++table->hgenerator; | |
689 | } | |
690 | ||
32537e91 | 691 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 692 | |
82603549 PNA |
693 | static const struct nft_chain_type * |
694 | __nft_chain_type_get(u8 family, enum nft_chain_types type) | |
695 | { | |
696 | if (family >= NFPROTO_NUMPROTO || | |
697 | type >= NFT_CHAIN_T_MAX) | |
698 | return NULL; | |
699 | ||
700 | return chain_type[family][type]; | |
701 | } | |
702 | ||
32537e91 | 703 | static const struct nft_chain_type * |
1ea26cca | 704 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c | 705 | { |
82603549 | 706 | const struct nft_chain_type *type; |
9370761c PNA |
707 | int i; |
708 | ||
baae3e62 | 709 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
82603549 PNA |
710 | type = __nft_chain_type_get(family, i); |
711 | if (!type) | |
712 | continue; | |
713 | if (!nla_strcmp(nla, type->name)) | |
714 | return type; | |
9370761c | 715 | } |
baae3e62 | 716 | return NULL; |
9370761c PNA |
717 | } |
718 | ||
eb014de4 PNA |
719 | struct nft_module_request { |
720 | struct list_head list; | |
721 | char module[MODULE_NAME_LEN]; | |
722 | bool done; | |
723 | }; | |
724 | ||
452238e8 | 725 | #ifdef CONFIG_MODULES |
cefa31a9 FW |
726 | __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, |
727 | ...) | |
452238e8 FW |
728 | { |
729 | char module_name[MODULE_NAME_LEN]; | |
0854db2a | 730 | struct nftables_pernet *nft_net; |
eb014de4 | 731 | struct nft_module_request *req; |
452238e8 FW |
732 | va_list args; |
733 | int ret; | |
734 | ||
452238e8 FW |
735 | va_start(args, fmt); |
736 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | |
737 | va_end(args); | |
9332d27d | 738 | if (ret >= MODULE_NAME_LEN) |
eb014de4 | 739 | return 0; |
452238e8 | 740 | |
d59d2f82 | 741 | nft_net = nft_pernet(net); |
0854db2a | 742 | list_for_each_entry(req, &nft_net->module_list, list) { |
eb014de4 PNA |
743 | if (!strcmp(req->module, module_name)) { |
744 | if (req->done) | |
745 | return 0; | |
ec7470b8 | 746 | |
eb014de4 PNA |
747 | /* A request to load this module already exists. */ |
748 | return -EAGAIN; | |
749 | } | |
750 | } | |
751 | ||
752 | req = kmalloc(sizeof(*req), GFP_KERNEL); | |
753 | if (!req) | |
754 | return -ENOMEM; | |
755 | ||
756 | req->done = false; | |
8556bceb | 757 | strscpy(req->module, module_name, MODULE_NAME_LEN); |
0854db2a | 758 | list_add_tail(&req->list, &nft_net->module_list); |
eb014de4 PNA |
759 | |
760 | return -EAGAIN; | |
452238e8 | 761 | } |
cefa31a9 | 762 | EXPORT_SYMBOL_GPL(nft_request_module); |
452238e8 FW |
763 | #endif |
764 | ||
f102d66b FW |
765 | static void lockdep_nfnl_nft_mutex_not_held(void) |
766 | { | |
767 | #ifdef CONFIG_PROVE_LOCKING | |
c0700dfa FW |
768 | if (debug_locks) |
769 | WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
f102d66b FW |
770 | #endif |
771 | } | |
772 | ||
32537e91 | 773 | static const struct nft_chain_type * |
452238e8 FW |
774 | nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla, |
775 | u8 family, bool autoload) | |
9370761c | 776 | { |
32537e91 | 777 | const struct nft_chain_type *type; |
9370761c | 778 | |
1ea26cca | 779 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
780 | if (type != NULL) |
781 | return type; | |
f102d66b FW |
782 | |
783 | lockdep_nfnl_nft_mutex_not_held(); | |
9370761c | 784 | #ifdef CONFIG_MODULES |
93b0806f | 785 | if (autoload) { |
eb014de4 PNA |
786 | if (nft_request_module(net, "nft-chain-%u-%.*s", family, |
787 | nla_len(nla), | |
788 | (const char *)nla_data(nla)) == -EAGAIN) | |
93b0806f | 789 | return ERR_PTR(-EAGAIN); |
9370761c PNA |
790 | } |
791 | #endif | |
93b0806f | 792 | return ERR_PTR(-ENOENT); |
9370761c PNA |
793 | } |
794 | ||
802b8051 PNA |
795 | static __be16 nft_base_seq(const struct net *net) |
796 | { | |
d59d2f82 | 797 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
798 | |
799 | return htons(nft_net->base_seq & 0xffff); | |
802b8051 PNA |
800 | } |
801 | ||
96518518 | 802 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
803 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
804 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 805 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 806 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
7a81575b JGG |
807 | [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY, |
808 | .len = NFT_USERDATA_MAXLEN } | |
96518518 PM |
809 | }; |
810 | ||
84d7fce6 PNA |
811 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
812 | u32 portid, u32 seq, int event, u32 flags, | |
813 | int family, const struct nft_table *table) | |
96518518 PM |
814 | { |
815 | struct nlmsghdr *nlh; | |
96518518 | 816 | |
dedb67c4 | 817 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
818 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
819 | NFNETLINK_V0, nft_base_seq(net)); | |
820 | if (!nlh) | |
96518518 PM |
821 | goto nla_put_failure; |
822 | ||
9ddf6323 | 823 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
179d9ba5 PNA |
824 | nla_put_be32(skb, NFTA_TABLE_FLAGS, |
825 | htonl(table->flags & NFT_TABLE_F_MASK)) || | |
3ecbfd65 HS |
826 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
827 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
828 | NFTA_TABLE_PAD)) | |
96518518 | 829 | goto nla_put_failure; |
6001a930 PNA |
830 | if (nft_table_has_owner(table) && |
831 | nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid))) | |
832 | goto nla_put_failure; | |
96518518 | 833 | |
7a81575b JGG |
834 | if (table->udata) { |
835 | if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata)) | |
836 | goto nla_put_failure; | |
837 | } | |
838 | ||
053c095a JB |
839 | nlmsg_end(skb, nlh); |
840 | return 0; | |
96518518 PM |
841 | |
842 | nla_put_failure: | |
843 | nlmsg_trim(skb, nlh); | |
844 | return -1; | |
845 | } | |
846 | ||
67cc570e PNA |
847 | struct nftnl_skb_parms { |
848 | bool report; | |
849 | }; | |
850 | #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb)) | |
851 | ||
852 | static void nft_notify_enqueue(struct sk_buff *skb, bool report, | |
853 | struct list_head *notify_list) | |
854 | { | |
855 | NFT_CB(skb).report = report; | |
856 | list_add_tail(&skb->list, notify_list); | |
857 | } | |
858 | ||
25e94a99 | 859 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 | 860 | { |
0854db2a | 861 | struct nftables_pernet *nft_net; |
96518518 | 862 | struct sk_buff *skb; |
6fb721cf | 863 | u16 flags = 0; |
96518518 PM |
864 | int err; |
865 | ||
128ad332 PNA |
866 | if (!ctx->report && |
867 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 868 | return; |
96518518 | 869 | |
96518518 PM |
870 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
871 | if (skb == NULL) | |
872 | goto err; | |
873 | ||
6fb721cf PNA |
874 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
875 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
876 | ||
84d7fce6 | 877 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 878 | event, flags, ctx->family, ctx->table); |
96518518 PM |
879 | if (err < 0) { |
880 | kfree_skb(skb); | |
881 | goto err; | |
882 | } | |
883 | ||
d59d2f82 | 884 | nft_net = nft_pernet(ctx->net); |
0854db2a | 885 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 886 | return; |
96518518 | 887 | err: |
25e94a99 | 888 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
889 | } |
890 | ||
891 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
892 | struct netlink_callback *cb) | |
893 | { | |
894 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
0854db2a | 895 | struct nftables_pernet *nft_net; |
96518518 PM |
896 | const struct nft_table *table; |
897 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 898 | struct net *net = sock_net(skb->sk); |
96518518 PM |
899 | int family = nfmsg->nfgen_family; |
900 | ||
e688a7f8 | 901 | rcu_read_lock(); |
d59d2f82 | 902 | nft_net = nft_pernet(net); |
34002783 | 903 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 904 | |
0854db2a | 905 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 906 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
907 | continue; |
908 | ||
36596dad PNA |
909 | if (idx < s_idx) |
910 | goto cont; | |
911 | if (idx > s_idx) | |
912 | memset(&cb->args[1], 0, | |
913 | sizeof(cb->args) - sizeof(cb->args[0])); | |
914 | if (!nft_is_active(net, table)) | |
915 | continue; | |
916 | if (nf_tables_fill_table_info(skb, net, | |
917 | NETLINK_CB(cb->skb).portid, | |
918 | cb->nlh->nlmsg_seq, | |
919 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 920 | table->family, table) < 0) |
36596dad PNA |
921 | goto done; |
922 | ||
923 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 924 | cont: |
36596dad | 925 | idx++; |
96518518 PM |
926 | } |
927 | done: | |
e688a7f8 | 928 | rcu_read_unlock(); |
96518518 PM |
929 | cb->args[0] = idx; |
930 | return skb->len; | |
931 | } | |
932 | ||
d9adf22a FW |
933 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
934 | const struct nlmsghdr *nlh, | |
935 | struct netlink_dump_control *c) | |
936 | { | |
937 | int err; | |
938 | ||
939 | if (!try_module_get(THIS_MODULE)) | |
940 | return -EINVAL; | |
941 | ||
942 | rcu_read_unlock(); | |
943 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
944 | rcu_read_lock(); | |
945 | module_put(THIS_MODULE); | |
946 | ||
947 | return err; | |
948 | } | |
949 | ||
950 | /* called with rcu_read_lock held */ | |
797d4980 PNA |
951 | static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info, |
952 | const struct nlattr * const nla[]) | |
96518518 | 953 | { |
797d4980 PNA |
954 | struct netlink_ext_ack *extack = info->extack; |
955 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 956 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 957 | const struct nft_table *table; |
797d4980 | 958 | struct net *net = info->net; |
96518518 | 959 | struct sk_buff *skb2; |
96518518 PM |
960 | int err; |
961 | ||
797d4980 | 962 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
963 | struct netlink_dump_control c = { |
964 | .dump = nf_tables_dump_tables, | |
d9adf22a | 965 | .module = THIS_MODULE, |
96518518 | 966 | }; |
d9adf22a | 967 | |
797d4980 | 968 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
969 | } |
970 | ||
6001a930 | 971 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0); |
36dd1bcc PNA |
972 | if (IS_ERR(table)) { |
973 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 974 | return PTR_ERR(table); |
36dd1bcc | 975 | } |
96518518 | 976 | |
d9adf22a | 977 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
978 | if (!skb2) |
979 | return -ENOMEM; | |
980 | ||
84d7fce6 | 981 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
982 | info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE, |
983 | 0, family, table); | |
96518518 | 984 | if (err < 0) |
ee921183 | 985 | goto err_fill_table_info; |
96518518 | 986 | |
ee921183 | 987 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 988 | |
ee921183 | 989 | err_fill_table_info: |
96518518 PM |
990 | kfree_skb(skb2); |
991 | return err; | |
992 | } | |
993 | ||
c9c17211 | 994 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
995 | { |
996 | struct nft_chain *chain; | |
997 | u32 i = 0; | |
998 | ||
999 | list_for_each_entry(chain, &table->chains, list) { | |
1000 | if (!nft_is_active_next(net, chain)) | |
1001 | continue; | |
f323d954 | 1002 | if (!nft_is_base_chain(chain)) |
10435c11 F |
1003 | continue; |
1004 | ||
1005 | if (cnt && i++ == cnt) | |
1006 | break; | |
1007 | ||
1e9451cb | 1008 | nf_tables_unregister_hook(net, table, chain); |
10435c11 F |
1009 | } |
1010 | } | |
1011 | ||
c9c17211 | 1012 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
1013 | { |
1014 | struct nft_chain *chain; | |
1015 | int err, i = 0; | |
1016 | ||
1017 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1018 | if (!nft_is_active_next(net, chain)) |
1019 | continue; | |
f323d954 | 1020 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
1021 | continue; |
1022 | ||
1e9451cb | 1023 | err = nf_tables_register_hook(net, table, chain); |
9ddf6323 | 1024 | if (err < 0) |
d54725cd | 1025 | goto err_register_hooks; |
9ddf6323 PNA |
1026 | |
1027 | i++; | |
1028 | } | |
1029 | return 0; | |
d54725cd PNA |
1030 | |
1031 | err_register_hooks: | |
10435c11 | 1032 | if (i) |
c9c17211 | 1033 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
1034 | return err; |
1035 | } | |
1036 | ||
c9c17211 | 1037 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 1038 | { |
179d9ba5 | 1039 | table->flags &= ~NFT_TABLE_F_DORMANT; |
c9c17211 | 1040 | nft_table_disable(net, table, 0); |
179d9ba5 | 1041 | table->flags |= NFT_TABLE_F_DORMANT; |
9ddf6323 PNA |
1042 | } |
1043 | ||
179d9ba5 PNA |
1044 | #define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1) |
1045 | #define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0) | |
1046 | #define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1) | |
1047 | #define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \ | |
1048 | __NFT_TABLE_F_WAS_AWAKEN) | |
0ce7cf41 | 1049 | |
e1aaca93 | 1050 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 1051 | { |
55dd6f93 | 1052 | struct nft_trans *trans; |
e1aaca93 | 1053 | u32 flags; |
179d9ba5 | 1054 | int ret; |
9ddf6323 | 1055 | |
e1aaca93 PNA |
1056 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
1057 | return 0; | |
9ddf6323 | 1058 | |
e1aaca93 | 1059 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
6001a930 PNA |
1060 | if (flags & ~NFT_TABLE_F_MASK) |
1061 | return -EOPNOTSUPP; | |
e1aaca93 | 1062 | |
63283dd2 PNA |
1063 | if (flags == ctx->table->flags) |
1064 | return 0; | |
1065 | ||
9cc0001a PNA |
1066 | if ((nft_table_has_owner(ctx->table) && |
1067 | !(flags & NFT_TABLE_F_OWNER)) || | |
1068 | (!nft_table_has_owner(ctx->table) && | |
1069 | flags & NFT_TABLE_F_OWNER)) | |
1070 | return -EOPNOTSUPP; | |
1071 | ||
55dd6f93 PNA |
1072 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
1073 | sizeof(struct nft_trans_table)); | |
1074 | if (trans == NULL) | |
1075 | return -ENOMEM; | |
9ddf6323 | 1076 | |
e1aaca93 PNA |
1077 | if ((flags & NFT_TABLE_F_DORMANT) && |
1078 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
179d9ba5 PNA |
1079 | ctx->table->flags |= NFT_TABLE_F_DORMANT; |
1080 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) | |
1081 | ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN; | |
e1aaca93 PNA |
1082 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
1083 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
179d9ba5 PNA |
1084 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
1085 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) { | |
1086 | ret = nf_tables_table_enable(ctx->net, ctx->table); | |
1087 | if (ret < 0) | |
1088 | goto err_register_hooks; | |
1089 | ||
1090 | ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT; | |
1091 | } | |
9ddf6323 PNA |
1092 | } |
1093 | ||
55dd6f93 | 1094 | nft_trans_table_update(trans) = true; |
0854db2a | 1095 | nft_trans_commit_list_add_tail(ctx->net, trans); |
179d9ba5 | 1096 | |
55dd6f93 | 1097 | return 0; |
179d9ba5 PNA |
1098 | |
1099 | err_register_hooks: | |
55dd6f93 | 1100 | nft_trans_destroy(trans); |
9ddf6323 PNA |
1101 | return ret; |
1102 | } | |
1103 | ||
1b2470e5 FW |
1104 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
1105 | { | |
1106 | const char *name = data; | |
1107 | ||
1108 | return jhash(name, strlen(name), seed); | |
1109 | } | |
1110 | ||
1111 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
1112 | { | |
1113 | const struct nft_chain *chain = data; | |
1114 | ||
1115 | return nft_chain_hash(chain->name, 0, seed); | |
1116 | } | |
1117 | ||
1118 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
1119 | const void *ptr) | |
1120 | { | |
1121 | const struct nft_chain *chain = ptr; | |
1122 | const char *name = arg->key; | |
1123 | ||
1124 | return strcmp(chain->name, name); | |
1125 | } | |
1126 | ||
4d44175a FW |
1127 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed) |
1128 | { | |
1129 | const struct nft_object_hash_key *k = data; | |
1130 | ||
1131 | seed ^= hash_ptr(k->table, 32); | |
1132 | ||
1133 | return jhash(k->name, strlen(k->name), seed); | |
1134 | } | |
1135 | ||
1136 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed) | |
1137 | { | |
1138 | const struct nft_object *obj = data; | |
1139 | ||
1140 | return nft_objname_hash(&obj->key, 0, seed); | |
1141 | } | |
1142 | ||
1143 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg, | |
1144 | const void *ptr) | |
1145 | { | |
1146 | const struct nft_object_hash_key *k = arg->key; | |
1147 | const struct nft_object *obj = ptr; | |
1148 | ||
1149 | if (obj->key.table != k->table) | |
1150 | return -1; | |
1151 | ||
1152 | return strcmp(obj->key.name, k->name); | |
1153 | } | |
1154 | ||
f1082dd3 PS |
1155 | static bool nft_supported_family(u8 family) |
1156 | { | |
1157 | return false | |
1158 | #ifdef CONFIG_NF_TABLES_INET | |
1159 | || family == NFPROTO_INET | |
1160 | #endif | |
1161 | #ifdef CONFIG_NF_TABLES_IPV4 | |
1162 | || family == NFPROTO_IPV4 | |
1163 | #endif | |
1164 | #ifdef CONFIG_NF_TABLES_ARP | |
1165 | || family == NFPROTO_ARP | |
1166 | #endif | |
1167 | #ifdef CONFIG_NF_TABLES_NETDEV | |
1168 | || family == NFPROTO_NETDEV | |
1169 | #endif | |
1170 | #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE) | |
1171 | || family == NFPROTO_BRIDGE | |
1172 | #endif | |
1173 | #ifdef CONFIG_NF_TABLES_IPV6 | |
1174 | || family == NFPROTO_IPV6 | |
1175 | #endif | |
1176 | ; | |
1177 | } | |
1178 | ||
7dab8ee3 PNA |
1179 | static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, |
1180 | const struct nlattr * const nla[]) | |
96518518 | 1181 | { |
7dab8ee3 | 1182 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
1183 | struct netlink_ext_ack *extack = info->extack; |
1184 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1185 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1186 | struct net *net = info->net; |
36dd1bcc PNA |
1187 | const struct nlattr *attr; |
1188 | struct nft_table *table; | |
e1aaca93 | 1189 | struct nft_ctx ctx; |
7a81575b | 1190 | u32 flags = 0; |
55dd6f93 | 1191 | int err; |
96518518 | 1192 | |
f1082dd3 PS |
1193 | if (!nft_supported_family(family)) |
1194 | return -EOPNOTSUPP; | |
1195 | ||
0854db2a | 1196 | lockdep_assert_held(&nft_net->commit_mutex); |
36dd1bcc | 1197 | attr = nla[NFTA_TABLE_NAME]; |
6001a930 PNA |
1198 | table = nft_table_lookup(net, attr, family, genmask, |
1199 | NETLINK_CB(skb).portid); | |
96518518 PM |
1200 | if (IS_ERR(table)) { |
1201 | if (PTR_ERR(table) != -ENOENT) | |
1202 | return PTR_ERR(table); | |
1a28ad74 | 1203 | } else { |
7dab8ee3 | 1204 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 1205 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1206 | return -EEXIST; |
36dd1bcc | 1207 | } |
7dab8ee3 | 1208 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 | 1209 | return -EOPNOTSUPP; |
e1aaca93 | 1210 | |
7dab8ee3 PNA |
1211 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
1212 | ||
e1aaca93 | 1213 | return nf_tables_updtable(&ctx); |
96518518 PM |
1214 | } |
1215 | ||
c5c1f975 PM |
1216 | if (nla[NFTA_TABLE_FLAGS]) { |
1217 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
6001a930 PNA |
1218 | if (flags & ~NFT_TABLE_F_MASK) |
1219 | return -EOPNOTSUPP; | |
c5c1f975 PM |
1220 | } |
1221 | ||
ffdb210e | 1222 | err = -ENOMEM; |
33758c89 | 1223 | table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT); |
ffdb210e | 1224 | if (table == NULL) |
98319cb9 | 1225 | goto err_kzalloc; |
96518518 | 1226 | |
33758c89 | 1227 | table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT); |
e46abbcc | 1228 | if (table->name == NULL) |
98319cb9 | 1229 | goto err_strdup; |
e46abbcc | 1230 | |
7a81575b | 1231 | if (nla[NFTA_TABLE_USERDATA]) { |
33758c89 | 1232 | table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT); |
7a81575b JGG |
1233 | if (table->udata == NULL) |
1234 | goto err_table_udata; | |
1235 | ||
85db827a | 1236 | table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]); |
7a81575b JGG |
1237 | } |
1238 | ||
1b2470e5 FW |
1239 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
1240 | if (err) | |
1241 | goto err_chain_ht; | |
1242 | ||
96518518 | 1243 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 1244 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 1245 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 1246 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 1247 | table->family = family; |
c5c1f975 | 1248 | table->flags = flags; |
ab482c6b | 1249 | table->handle = ++nft_net->table_handle; |
6001a930 PNA |
1250 | if (table->flags & NFT_TABLE_F_OWNER) |
1251 | table->nlpid = NETLINK_CB(skb).portid; | |
9ddf6323 | 1252 | |
7dab8ee3 | 1253 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
55dd6f93 | 1254 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 1255 | if (err < 0) |
98319cb9 | 1256 | goto err_trans; |
ffdb210e | 1257 | |
0854db2a | 1258 | list_add_tail_rcu(&table->list, &nft_net->tables); |
96518518 | 1259 | return 0; |
98319cb9 | 1260 | err_trans: |
1b2470e5 FW |
1261 | rhltable_destroy(&table->chains_ht); |
1262 | err_chain_ht: | |
7a81575b JGG |
1263 | kfree(table->udata); |
1264 | err_table_udata: | |
e46abbcc | 1265 | kfree(table->name); |
98319cb9 | 1266 | err_strdup: |
ffdb210e | 1267 | kfree(table); |
98319cb9 | 1268 | err_kzalloc: |
ffdb210e | 1269 | return err; |
96518518 PM |
1270 | } |
1271 | ||
b9ac12ef AB |
1272 | static int nft_flush_table(struct nft_ctx *ctx) |
1273 | { | |
3b49e2e9 | 1274 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 1275 | struct nft_chain *chain, *nc; |
e5009240 | 1276 | struct nft_object *obj, *ne; |
b9ac12ef | 1277 | struct nft_set *set, *ns; |
3b49e2e9 | 1278 | int err; |
b9ac12ef | 1279 | |
a2f18db0 | 1280 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
1281 | if (!nft_is_active_next(ctx->net, chain)) |
1282 | continue; | |
1283 | ||
d0e2c7de PNA |
1284 | if (nft_chain_is_bound(chain)) |
1285 | continue; | |
1286 | ||
b9ac12ef AB |
1287 | ctx->chain = chain; |
1288 | ||
1289 | err = nft_delrule_by_chain(ctx); | |
1290 | if (err < 0) | |
1291 | goto out; | |
b9ac12ef AB |
1292 | } |
1293 | ||
1294 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
1295 | if (!nft_is_active_next(ctx->net, set)) |
1296 | continue; | |
1297 | ||
408070d6 | 1298 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
1299 | !list_empty(&set->bindings)) |
1300 | continue; | |
1301 | ||
1302 | err = nft_delset(ctx, set); | |
1303 | if (err < 0) | |
1304 | goto out; | |
1305 | } | |
1306 | ||
3b49e2e9 | 1307 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
335178d5 FW |
1308 | if (!nft_is_active_next(ctx->net, flowtable)) |
1309 | continue; | |
1310 | ||
3b49e2e9 PNA |
1311 | err = nft_delflowtable(ctx, flowtable); |
1312 | if (err < 0) | |
1313 | goto out; | |
1314 | } | |
1315 | ||
e5009240 | 1316 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
335178d5 FW |
1317 | if (!nft_is_active_next(ctx->net, obj)) |
1318 | continue; | |
1319 | ||
e5009240 PNA |
1320 | err = nft_delobj(ctx, obj); |
1321 | if (err < 0) | |
1322 | goto out; | |
1323 | } | |
1324 | ||
a2f18db0 | 1325 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
1326 | if (!nft_is_active_next(ctx->net, chain)) |
1327 | continue; | |
1328 | ||
d0e2c7de PNA |
1329 | if (nft_chain_is_bound(chain)) |
1330 | continue; | |
1331 | ||
a2f18db0 PNA |
1332 | ctx->chain = chain; |
1333 | ||
1334 | err = nft_delchain(ctx); | |
1335 | if (err < 0) | |
1336 | goto out; | |
1337 | } | |
1338 | ||
b9ac12ef AB |
1339 | err = nft_deltable(ctx); |
1340 | out: | |
1341 | return err; | |
1342 | } | |
1343 | ||
1344 | static int nft_flush(struct nft_ctx *ctx, int family) | |
1345 | { | |
d59d2f82 | 1346 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
b9ac12ef | 1347 | const struct nlattr * const *nla = ctx->nla; |
d59d2f82 | 1348 | struct nft_table *table, *nt; |
b9ac12ef AB |
1349 | int err = 0; |
1350 | ||
0854db2a | 1351 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
98319cb9 | 1352 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
1353 | continue; |
1354 | ||
98319cb9 | 1355 | ctx->family = table->family; |
f2a6d766 | 1356 | |
36596dad PNA |
1357 | if (!nft_is_active_next(ctx->net, table)) |
1358 | continue; | |
b9ac12ef | 1359 | |
6001a930 PNA |
1360 | if (nft_table_has_owner(table) && table->nlpid != ctx->portid) |
1361 | continue; | |
1362 | ||
36596dad PNA |
1363 | if (nla[NFTA_TABLE_NAME] && |
1364 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
1365 | continue; | |
b9ac12ef | 1366 | |
36596dad PNA |
1367 | ctx->table = table; |
1368 | ||
1369 | err = nft_flush_table(ctx); | |
1370 | if (err < 0) | |
1371 | goto out; | |
b9ac12ef AB |
1372 | } |
1373 | out: | |
1374 | return err; | |
1375 | } | |
1376 | ||
7dab8ee3 PNA |
1377 | static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info, |
1378 | const struct nlattr * const nla[]) | |
96518518 | 1379 | { |
7dab8ee3 PNA |
1380 | struct netlink_ext_ack *extack = info->extack; |
1381 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1382 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1383 | struct net *net = info->net; |
36dd1bcc PNA |
1384 | const struct nlattr *attr; |
1385 | struct nft_table *table; | |
55dd6f93 | 1386 | struct nft_ctx ctx; |
96518518 | 1387 | |
7dab8ee3 | 1388 | nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
1389 | if (family == AF_UNSPEC || |
1390 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
1391 | return nft_flush(&ctx, family); |
1392 | ||
36dd1bcc PNA |
1393 | if (nla[NFTA_TABLE_HANDLE]) { |
1394 | attr = nla[NFTA_TABLE_HANDLE]; | |
e31f072f PNA |
1395 | table = nft_table_lookup_byhandle(net, attr, genmask, |
1396 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
1397 | } else { |
1398 | attr = nla[NFTA_TABLE_NAME]; | |
6001a930 PNA |
1399 | table = nft_table_lookup(net, attr, family, genmask, |
1400 | NETLINK_CB(skb).portid); | |
36dd1bcc | 1401 | } |
3ecbfd65 | 1402 | |
36dd1bcc | 1403 | if (IS_ERR(table)) { |
f80a612d FFM |
1404 | if (PTR_ERR(table) == -ENOENT && |
1405 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYTABLE) | |
1406 | return 0; | |
1407 | ||
36dd1bcc | 1408 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1409 | return PTR_ERR(table); |
36dd1bcc | 1410 | } |
96518518 | 1411 | |
7dab8ee3 | 1412 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
a8278400 PNA |
1413 | table->use > 0) |
1414 | return -EBUSY; | |
1415 | ||
98319cb9 | 1416 | ctx.family = family; |
b9ac12ef | 1417 | ctx.table = table; |
55dd6f93 | 1418 | |
b9ac12ef | 1419 | return nft_flush_table(&ctx); |
96518518 PM |
1420 | } |
1421 | ||
55dd6f93 PNA |
1422 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
1423 | { | |
fa5950e4 FW |
1424 | if (WARN_ON(ctx->table->use > 0)) |
1425 | return; | |
4fefee57 | 1426 | |
1b2470e5 | 1427 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 1428 | kfree(ctx->table->name); |
bc7a7082 | 1429 | kfree(ctx->table->udata); |
55dd6f93 | 1430 | kfree(ctx->table); |
55dd6f93 PNA |
1431 | } |
1432 | ||
cc07eeb0 | 1433 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1434 | { |
96518518 | 1435 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
82603549 | 1436 | if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) { |
cc07eeb0 PNA |
1437 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1438 | return; | |
96518518 | 1439 | } |
9370761c | 1440 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 1441 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 1442 | } |
9370761c | 1443 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 1444 | |
32537e91 | 1445 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1446 | { |
96518518 | 1447 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 1448 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
1449 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1450 | } | |
9370761c | 1451 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
1452 | |
1453 | /* | |
1454 | * Chains | |
1455 | */ | |
1456 | ||
1457 | static struct nft_chain * | |
cac20fcd | 1458 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1459 | { |
1460 | struct nft_chain *chain; | |
1461 | ||
1462 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1463 | if (chain->handle == handle && |
1464 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1465 | return chain; |
1466 | } | |
1467 | ||
1468 | return ERR_PTR(-ENOENT); | |
1469 | } | |
1470 | ||
4d44175a | 1471 | static bool lockdep_commit_lock_is_held(const struct net *net) |
f102d66b FW |
1472 | { |
1473 | #ifdef CONFIG_PROVE_LOCKING | |
d59d2f82 | 1474 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
1475 | |
1476 | return lockdep_is_held(&nft_net->commit_mutex); | |
f102d66b FW |
1477 | #else |
1478 | return true; | |
1479 | #endif | |
1480 | } | |
1481 | ||
1482 | static struct nft_chain *nft_chain_lookup(struct net *net, | |
1483 | struct nft_table *table, | |
cac20fcd | 1484 | const struct nlattr *nla, u8 genmask) |
96518518 | 1485 | { |
1b2470e5 FW |
1486 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1487 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1488 | struct nft_chain *chain; |
1489 | ||
1490 | if (nla == NULL) | |
1491 | return ERR_PTR(-EINVAL); | |
1492 | ||
872f6903 | 1493 | nla_strscpy(search, nla, sizeof(search)); |
96518518 | 1494 | |
1b2470e5 | 1495 | WARN_ON(!rcu_read_lock_held() && |
f102d66b | 1496 | !lockdep_commit_lock_is_held(net)); |
1b2470e5 FW |
1497 | |
1498 | chain = ERR_PTR(-ENOENT); | |
1499 | rcu_read_lock(); | |
1500 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1501 | if (!list) | |
1502 | goto out_unlock; | |
1503 | ||
1504 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1505 | if (nft_active_genmask(chain, genmask)) | |
1506 | goto out_unlock; | |
1507 | } | |
1508 | chain = ERR_PTR(-ENOENT); | |
1509 | out_unlock: | |
1510 | rcu_read_unlock(); | |
1511 | return chain; | |
96518518 PM |
1512 | } |
1513 | ||
1514 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1515 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1516 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1517 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1518 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1519 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1520 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1521 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
9332d27d FW |
1522 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, |
1523 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
0ca743a5 | 1524 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
c9626a2c | 1525 | [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, |
74cccc3d | 1526 | [NFTA_CHAIN_ID] = { .type = NLA_U32 }, |
002f2176 JGG |
1527 | [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY, |
1528 | .len = NFT_USERDATA_MAXLEN }, | |
96518518 PM |
1529 | }; |
1530 | ||
1531 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1532 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1533 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1534 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1535 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1536 | }; |
1537 | ||
0ca743a5 PNA |
1538 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1539 | { | |
1540 | struct nft_stats *cpu_stats, total; | |
1541 | struct nlattr *nest; | |
ce355e20 ED |
1542 | unsigned int seq; |
1543 | u64 pkts, bytes; | |
0ca743a5 PNA |
1544 | int cpu; |
1545 | ||
edbd82c5 FW |
1546 | if (!stats) |
1547 | return 0; | |
1548 | ||
0ca743a5 PNA |
1549 | memset(&total, 0, sizeof(total)); |
1550 | for_each_possible_cpu(cpu) { | |
1551 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 | 1552 | do { |
d120d1a6 | 1553 | seq = u64_stats_fetch_begin(&cpu_stats->syncp); |
ce355e20 ED |
1554 | pkts = cpu_stats->pkts; |
1555 | bytes = cpu_stats->bytes; | |
d120d1a6 | 1556 | } while (u64_stats_fetch_retry(&cpu_stats->syncp, seq)); |
ce355e20 ED |
1557 | total.pkts += pkts; |
1558 | total.bytes += bytes; | |
0ca743a5 | 1559 | } |
ae0be8de | 1560 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS); |
0ca743a5 PNA |
1561 | if (nest == NULL) |
1562 | goto nla_put_failure; | |
1563 | ||
b46f6ded ND |
1564 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
1565 | NFTA_COUNTER_PAD) || | |
1566 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
1567 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
1568 | goto nla_put_failure; |
1569 | ||
1570 | nla_nest_end(skb, nest); | |
1571 | return 0; | |
1572 | ||
1573 | nla_put_failure: | |
1574 | return -ENOSPC; | |
1575 | } | |
1576 | ||
d54725cd PNA |
1577 | static int nft_dump_basechain_hook(struct sk_buff *skb, int family, |
1578 | const struct nft_base_chain *basechain) | |
1579 | { | |
1580 | const struct nf_hook_ops *ops = &basechain->ops; | |
1581 | struct nft_hook *hook, *first = NULL; | |
1582 | struct nlattr *nest, *nest_devs; | |
1583 | int n = 0; | |
1584 | ||
1585 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK); | |
1586 | if (nest == NULL) | |
1587 | goto nla_put_failure; | |
1588 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1589 | goto nla_put_failure; | |
1590 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1591 | goto nla_put_failure; | |
1592 | ||
d3519cb8 | 1593 | if (nft_base_chain_netdev(family, ops->hooknum)) { |
d54725cd PNA |
1594 | nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS); |
1595 | list_for_each_entry(hook, &basechain->hook_list, list) { | |
1596 | if (!first) | |
1597 | first = hook; | |
1598 | ||
1599 | if (nla_put_string(skb, NFTA_DEVICE_NAME, | |
1600 | hook->ops.dev->name)) | |
1601 | goto nla_put_failure; | |
1602 | n++; | |
1603 | } | |
1604 | nla_nest_end(skb, nest_devs); | |
1605 | ||
1606 | if (n == 1 && | |
1607 | nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name)) | |
1608 | goto nla_put_failure; | |
1609 | } | |
1610 | nla_nest_end(skb, nest); | |
1611 | ||
1612 | return 0; | |
1613 | nla_put_failure: | |
1614 | return -1; | |
1615 | } | |
1616 | ||
84d7fce6 PNA |
1617 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
1618 | u32 portid, u32 seq, int event, u32 flags, | |
1619 | int family, const struct nft_table *table, | |
96518518 PM |
1620 | const struct nft_chain *chain) |
1621 | { | |
1622 | struct nlmsghdr *nlh; | |
96518518 | 1623 | |
dedb67c4 | 1624 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
1625 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
1626 | NFNETLINK_V0, nft_base_seq(net)); | |
1627 | if (!nlh) | |
96518518 PM |
1628 | goto nla_put_failure; |
1629 | ||
96518518 PM |
1630 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) |
1631 | goto nla_put_failure; | |
b46f6ded ND |
1632 | if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), |
1633 | NFTA_CHAIN_PAD)) | |
96518518 PM |
1634 | goto nla_put_failure; |
1635 | if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) | |
1636 | goto nla_put_failure; | |
1637 | ||
f323d954 | 1638 | if (nft_is_base_chain(chain)) { |
0ca743a5 | 1639 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
edbd82c5 | 1640 | struct nft_stats __percpu *stats; |
0ca743a5 | 1641 | |
d54725cd | 1642 | if (nft_dump_basechain_hook(skb, family, basechain)) |
2cbce139 | 1643 | goto nla_put_failure; |
9370761c | 1644 | |
0ca743a5 PNA |
1645 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1646 | htonl(basechain->policy))) | |
1647 | goto nla_put_failure; | |
1648 | ||
baae3e62 PM |
1649 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1650 | goto nla_put_failure; | |
0ca743a5 | 1651 | |
edbd82c5 FW |
1652 | stats = rcu_dereference_check(basechain->stats, |
1653 | lockdep_commit_lock_is_held(net)); | |
1654 | if (nft_dump_stats(skb, stats)) | |
0ca743a5 | 1655 | goto nla_put_failure; |
96518518 PM |
1656 | } |
1657 | ||
d0e2c7de PNA |
1658 | if (chain->flags && |
1659 | nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags))) | |
1660 | goto nla_put_failure; | |
1661 | ||
0ca743a5 PNA |
1662 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1663 | goto nla_put_failure; | |
1664 | ||
002f2176 JGG |
1665 | if (chain->udata && |
1666 | nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata)) | |
1667 | goto nla_put_failure; | |
1668 | ||
053c095a JB |
1669 | nlmsg_end(skb, nlh); |
1670 | return 0; | |
96518518 PM |
1671 | |
1672 | nla_put_failure: | |
1673 | nlmsg_trim(skb, nlh); | |
1674 | return -1; | |
1675 | } | |
1676 | ||
25e94a99 | 1677 | static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
96518518 | 1678 | { |
0854db2a | 1679 | struct nftables_pernet *nft_net; |
96518518 | 1680 | struct sk_buff *skb; |
6fb721cf | 1681 | u16 flags = 0; |
96518518 PM |
1682 | int err; |
1683 | ||
128ad332 PNA |
1684 | if (!ctx->report && |
1685 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1686 | return; |
96518518 | 1687 | |
96518518 PM |
1688 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1689 | if (skb == NULL) | |
1690 | goto err; | |
1691 | ||
6fb721cf PNA |
1692 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
1693 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
1694 | ||
84d7fce6 | 1695 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 1696 | event, flags, ctx->family, ctx->table, |
35151d84 | 1697 | ctx->chain); |
96518518 PM |
1698 | if (err < 0) { |
1699 | kfree_skb(skb); | |
1700 | goto err; | |
1701 | } | |
1702 | ||
d59d2f82 | 1703 | nft_net = nft_pernet(ctx->net); |
0854db2a | 1704 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 1705 | return; |
96518518 | 1706 | err: |
25e94a99 | 1707 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1708 | } |
1709 | ||
1710 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1711 | struct netlink_callback *cb) | |
1712 | { | |
1713 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 | 1714 | unsigned int idx = 0, s_idx = cb->args[0]; |
99633ab2 | 1715 | struct net *net = sock_net(skb->sk); |
96518518 | 1716 | int family = nfmsg->nfgen_family; |
0854db2a | 1717 | struct nftables_pernet *nft_net; |
d59d2f82 PNA |
1718 | const struct nft_table *table; |
1719 | const struct nft_chain *chain; | |
96518518 | 1720 | |
e688a7f8 | 1721 | rcu_read_lock(); |
d59d2f82 | 1722 | nft_net = nft_pernet(net); |
34002783 | 1723 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 1724 | |
0854db2a | 1725 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 1726 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1727 | continue; |
1728 | ||
36596dad PNA |
1729 | list_for_each_entry_rcu(chain, &table->chains, list) { |
1730 | if (idx < s_idx) | |
1731 | goto cont; | |
1732 | if (idx > s_idx) | |
1733 | memset(&cb->args[1], 0, | |
1734 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1735 | if (!nft_is_active(net, chain)) | |
1736 | continue; | |
1737 | if (nf_tables_fill_chain_info(skb, net, | |
1738 | NETLINK_CB(cb->skb).portid, | |
1739 | cb->nlh->nlmsg_seq, | |
1740 | NFT_MSG_NEWCHAIN, | |
1741 | NLM_F_MULTI, | |
98319cb9 | 1742 | table->family, table, |
36596dad PNA |
1743 | chain) < 0) |
1744 | goto done; | |
38e029f1 | 1745 | |
36596dad | 1746 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
96518518 | 1747 | cont: |
36596dad | 1748 | idx++; |
96518518 PM |
1749 | } |
1750 | } | |
1751 | done: | |
e688a7f8 | 1752 | rcu_read_unlock(); |
96518518 PM |
1753 | cb->args[0] = idx; |
1754 | return skb->len; | |
1755 | } | |
1756 | ||
d9adf22a | 1757 | /* called with rcu_read_lock held */ |
797d4980 PNA |
1758 | static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info, |
1759 | const struct nlattr * const nla[]) | |
96518518 | 1760 | { |
797d4980 PNA |
1761 | struct netlink_ext_ack *extack = info->extack; |
1762 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 1763 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 1764 | const struct nft_chain *chain; |
797d4980 | 1765 | struct net *net = info->net; |
1b2470e5 | 1766 | struct nft_table *table; |
96518518 | 1767 | struct sk_buff *skb2; |
96518518 PM |
1768 | int err; |
1769 | ||
797d4980 | 1770 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
1771 | struct netlink_dump_control c = { |
1772 | .dump = nf_tables_dump_chains, | |
d9adf22a | 1773 | .module = THIS_MODULE, |
96518518 | 1774 | }; |
d9adf22a | 1775 | |
797d4980 | 1776 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
1777 | } |
1778 | ||
6001a930 | 1779 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0); |
36dd1bcc PNA |
1780 | if (IS_ERR(table)) { |
1781 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1782 | return PTR_ERR(table); |
36dd1bcc | 1783 | } |
96518518 | 1784 | |
f102d66b | 1785 | chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask); |
36dd1bcc PNA |
1786 | if (IS_ERR(chain)) { |
1787 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
96518518 | 1788 | return PTR_ERR(chain); |
36dd1bcc | 1789 | } |
96518518 | 1790 | |
d9adf22a | 1791 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1792 | if (!skb2) |
1793 | return -ENOMEM; | |
1794 | ||
84d7fce6 | 1795 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
1796 | info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, |
1797 | 0, family, table, chain); | |
96518518 | 1798 | if (err < 0) |
ee921183 | 1799 | goto err_fill_chain_info; |
96518518 | 1800 | |
ee921183 | 1801 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 1802 | |
ee921183 | 1803 | err_fill_chain_info: |
96518518 PM |
1804 | kfree_skb(skb2); |
1805 | return err; | |
1806 | } | |
1807 | ||
0ca743a5 PNA |
1808 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1809 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1810 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1811 | }; | |
1812 | ||
ff3cd7b3 | 1813 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1814 | { |
1815 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1816 | struct nft_stats __percpu *newstats; | |
1817 | struct nft_stats *stats; | |
1818 | int err; | |
1819 | ||
8cb08174 JB |
1820 | err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr, |
1821 | nft_counter_policy, NULL); | |
0ca743a5 | 1822 | if (err < 0) |
ff3cd7b3 | 1823 | return ERR_PTR(err); |
0ca743a5 PNA |
1824 | |
1825 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1826 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1827 | |
ce355e20 | 1828 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1829 | if (newstats == NULL) |
ff3cd7b3 | 1830 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1831 | |
1832 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1833 | * are not exposed to userspace. | |
1834 | */ | |
e8781f70 | 1835 | preempt_disable(); |
0ca743a5 PNA |
1836 | stats = this_cpu_ptr(newstats); |
1837 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
1838 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 1839 | preempt_enable(); |
0ca743a5 | 1840 | |
ff3cd7b3 PNA |
1841 | return newstats; |
1842 | } | |
1843 | ||
53315ac6 | 1844 | static void nft_chain_stats_replace(struct nft_trans *trans) |
ff3cd7b3 | 1845 | { |
53315ac6 | 1846 | struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain); |
0befd061 | 1847 | |
53315ac6 | 1848 | if (!nft_trans_chain_stats(trans)) |
b88825de PNA |
1849 | return; |
1850 | ||
b685b534 PM |
1851 | nft_trans_chain_stats(trans) = |
1852 | rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans), | |
1853 | lockdep_commit_lock_is_held(trans->ctx.net)); | |
53315ac6 FW |
1854 | |
1855 | if (!nft_trans_chain_stats(trans)) | |
bbb8c61f | 1856 | static_branch_inc(&nft_counters_enabled); |
0ca743a5 PNA |
1857 | } |
1858 | ||
0cbc06b3 FW |
1859 | static void nf_tables_chain_free_chain_rules(struct nft_chain *chain) |
1860 | { | |
2c865a8a PNA |
1861 | struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0); |
1862 | struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1); | |
0cbc06b3 FW |
1863 | |
1864 | if (g0 != g1) | |
1865 | kvfree(g1); | |
1866 | kvfree(g0); | |
1867 | ||
1868 | /* should be NULL either via abort or via successful commit */ | |
2c865a8a PNA |
1869 | WARN_ON_ONCE(chain->blob_next); |
1870 | kvfree(chain->blob_next); | |
0cbc06b3 FW |
1871 | } |
1872 | ||
d0e2c7de | 1873 | void nf_tables_chain_destroy(struct nft_ctx *ctx) |
91c7b38d | 1874 | { |
43a605f2 | 1875 | struct nft_chain *chain = ctx->chain; |
d54725cd | 1876 | struct nft_hook *hook, *next; |
43a605f2 | 1877 | |
fa5950e4 FW |
1878 | if (WARN_ON(chain->use > 0)) |
1879 | return; | |
91c7b38d | 1880 | |
0cbc06b3 FW |
1881 | /* no concurrent access possible anymore */ |
1882 | nf_tables_chain_free_chain_rules(chain); | |
1883 | ||
f323d954 | 1884 | if (nft_is_base_chain(chain)) { |
2cbce139 PNA |
1885 | struct nft_base_chain *basechain = nft_base_chain(chain); |
1886 | ||
d3519cb8 | 1887 | if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) { |
d54725cd PNA |
1888 | list_for_each_entry_safe(hook, next, |
1889 | &basechain->hook_list, list) { | |
1890 | list_del_rcu(&hook->list); | |
1891 | kfree_rcu(hook, rcu); | |
1892 | } | |
1893 | } | |
2cbce139 | 1894 | module_put(basechain->type->owner); |
4c05ec47 | 1895 | if (rcu_access_pointer(basechain->stats)) { |
9f08ea84 | 1896 | static_branch_dec(&nft_counters_enabled); |
4c05ec47 TY |
1897 | free_percpu(rcu_dereference_raw(basechain->stats)); |
1898 | } | |
b7263e07 | 1899 | kfree(chain->name); |
002f2176 | 1900 | kfree(chain->udata); |
2cbce139 | 1901 | kfree(basechain); |
91c7b38d | 1902 | } else { |
b7263e07 | 1903 | kfree(chain->name); |
002f2176 | 1904 | kfree(chain->udata); |
91c7b38d PNA |
1905 | kfree(chain); |
1906 | } | |
1907 | } | |
1908 | ||
3f0465a9 PNA |
1909 | static struct nft_hook *nft_netdev_hook_alloc(struct net *net, |
1910 | const struct nlattr *attr) | |
1911 | { | |
1912 | struct net_device *dev; | |
1913 | char ifname[IFNAMSIZ]; | |
1914 | struct nft_hook *hook; | |
1915 | int err; | |
1916 | ||
33758c89 | 1917 | hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT); |
3f0465a9 PNA |
1918 | if (!hook) { |
1919 | err = -ENOMEM; | |
1920 | goto err_hook_alloc; | |
1921 | } | |
1922 | ||
872f6903 | 1923 | nla_strscpy(ifname, attr, IFNAMSIZ); |
42f1c271 PNA |
1924 | /* nf_tables_netdev_event() is called under rtnl_mutex, this is |
1925 | * indirectly serializing all the other holders of the commit_mutex with | |
1926 | * the rtnl_mutex. | |
1927 | */ | |
3f0465a9 PNA |
1928 | dev = __dev_get_by_name(net, ifname); |
1929 | if (!dev) { | |
1930 | err = -ENOENT; | |
1931 | goto err_hook_dev; | |
1932 | } | |
1933 | hook->ops.dev = dev; | |
1934 | ||
1935 | return hook; | |
1936 | ||
1937 | err_hook_dev: | |
1938 | kfree(hook); | |
1939 | err_hook_alloc: | |
1940 | return ERR_PTR(err); | |
1941 | } | |
1942 | ||
abadb2f8 PNA |
1943 | static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, |
1944 | const struct nft_hook *this) | |
b75a3e83 PNA |
1945 | { |
1946 | struct nft_hook *hook; | |
1947 | ||
1948 | list_for_each_entry(hook, hook_list, list) { | |
1949 | if (this->ops.dev == hook->ops.dev) | |
abadb2f8 | 1950 | return hook; |
b75a3e83 PNA |
1951 | } |
1952 | ||
abadb2f8 | 1953 | return NULL; |
b75a3e83 PNA |
1954 | } |
1955 | ||
3f0465a9 PNA |
1956 | static int nf_tables_parse_netdev_hooks(struct net *net, |
1957 | const struct nlattr *attr, | |
1958 | struct list_head *hook_list) | |
1959 | { | |
1960 | struct nft_hook *hook, *next; | |
1961 | const struct nlattr *tmp; | |
1962 | int rem, n = 0, err; | |
1963 | ||
1964 | nla_for_each_nested(tmp, attr, rem) { | |
1965 | if (nla_type(tmp) != NFTA_DEVICE_NAME) { | |
1966 | err = -EINVAL; | |
1967 | goto err_hook; | |
1968 | } | |
1969 | ||
1970 | hook = nft_netdev_hook_alloc(net, tmp); | |
1971 | if (IS_ERR(hook)) { | |
1972 | err = PTR_ERR(hook); | |
1973 | goto err_hook; | |
1974 | } | |
b75a3e83 | 1975 | if (nft_hook_list_find(hook_list, hook)) { |
cd77e75b | 1976 | kfree(hook); |
b75a3e83 PNA |
1977 | err = -EEXIST; |
1978 | goto err_hook; | |
1979 | } | |
3f0465a9 PNA |
1980 | list_add_tail(&hook->list, hook_list); |
1981 | n++; | |
1982 | ||
cb662ac6 | 1983 | if (n == NFT_NETDEVICE_MAX) { |
3f0465a9 PNA |
1984 | err = -EFBIG; |
1985 | goto err_hook; | |
1986 | } | |
1987 | } | |
3f0465a9 PNA |
1988 | |
1989 | return 0; | |
1990 | ||
1991 | err_hook: | |
1992 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
1993 | list_del(&hook->list); | |
1994 | kfree(hook); | |
1995 | } | |
1996 | return err; | |
1997 | } | |
1998 | ||
508f8ccd PNA |
1999 | struct nft_chain_hook { |
2000 | u32 num; | |
84ba7dd7 | 2001 | s32 priority; |
32537e91 | 2002 | const struct nft_chain_type *type; |
d54725cd | 2003 | struct list_head list; |
508f8ccd PNA |
2004 | }; |
2005 | ||
d54725cd PNA |
2006 | static int nft_chain_parse_netdev(struct net *net, |
2007 | struct nlattr *tb[], | |
2008 | struct list_head *hook_list) | |
2009 | { | |
2010 | struct nft_hook *hook; | |
2011 | int err; | |
2012 | ||
2013 | if (tb[NFTA_HOOK_DEV]) { | |
2014 | hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]); | |
2015 | if (IS_ERR(hook)) | |
2016 | return PTR_ERR(hook); | |
2017 | ||
2018 | list_add_tail(&hook->list, hook_list); | |
2019 | } else if (tb[NFTA_HOOK_DEVS]) { | |
2020 | err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS], | |
2021 | hook_list); | |
2022 | if (err < 0) | |
2023 | return err; | |
05abe445 PNA |
2024 | |
2025 | if (list_empty(hook_list)) | |
2026 | return -EINVAL; | |
d54725cd PNA |
2027 | } else { |
2028 | return -EINVAL; | |
2029 | } | |
2030 | ||
2031 | return 0; | |
2032 | } | |
2033 | ||
508f8ccd PNA |
2034 | static int nft_chain_parse_hook(struct net *net, |
2035 | const struct nlattr * const nla[], | |
36596dad | 2036 | struct nft_chain_hook *hook, u8 family, |
983c4fcb | 2037 | struct netlink_ext_ack *extack, bool autoload) |
508f8ccd | 2038 | { |
d59d2f82 | 2039 | struct nftables_pernet *nft_net = nft_pernet(net); |
508f8ccd | 2040 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; |
32537e91 | 2041 | const struct nft_chain_type *type; |
508f8ccd PNA |
2042 | int err; |
2043 | ||
0854db2a | 2044 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b FW |
2045 | lockdep_nfnl_nft_mutex_not_held(); |
2046 | ||
8cb08174 JB |
2047 | err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX, |
2048 | nla[NFTA_CHAIN_HOOK], | |
2049 | nft_hook_policy, NULL); | |
508f8ccd PNA |
2050 | if (err < 0) |
2051 | return err; | |
2052 | ||
2053 | if (ha[NFTA_HOOK_HOOKNUM] == NULL || | |
2054 | ha[NFTA_HOOK_PRIORITY] == NULL) | |
2055 | return -EINVAL; | |
2056 | ||
2057 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
508f8ccd PNA |
2058 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); |
2059 | ||
82603549 PNA |
2060 | type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT); |
2061 | if (!type) | |
2062 | return -EOPNOTSUPP; | |
2063 | ||
508f8ccd | 2064 | if (nla[NFTA_CHAIN_TYPE]) { |
452238e8 | 2065 | type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE], |
445509eb | 2066 | family, autoload); |
983c4fcb PNA |
2067 | if (IS_ERR(type)) { |
2068 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
508f8ccd | 2069 | return PTR_ERR(type); |
983c4fcb | 2070 | } |
508f8ccd | 2071 | } |
d25e2e93 | 2072 | if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num))) |
508f8ccd | 2073 | return -EOPNOTSUPP; |
84ba7dd7 FW |
2074 | |
2075 | if (type->type == NFT_CHAIN_T_NAT && | |
2076 | hook->priority <= NF_IP_PRI_CONNTRACK) | |
2077 | return -EOPNOTSUPP; | |
2078 | ||
983c4fcb PNA |
2079 | if (!try_module_get(type->owner)) { |
2080 | if (nla[NFTA_CHAIN_TYPE]) | |
2081 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
508f8ccd | 2082 | return -ENOENT; |
983c4fcb | 2083 | } |
508f8ccd PNA |
2084 | |
2085 | hook->type = type; | |
2086 | ||
d54725cd | 2087 | INIT_LIST_HEAD(&hook->list); |
d3519cb8 | 2088 | if (nft_base_chain_netdev(family, hook->num)) { |
d54725cd PNA |
2089 | err = nft_chain_parse_netdev(net, ha, &hook->list); |
2090 | if (err < 0) { | |
508f8ccd | 2091 | module_put(type->owner); |
d54725cd | 2092 | return err; |
508f8ccd | 2093 | } |
d54725cd | 2094 | } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) { |
508f8ccd PNA |
2095 | module_put(type->owner); |
2096 | return -EOPNOTSUPP; | |
2097 | } | |
2098 | ||
2099 | return 0; | |
2100 | } | |
2101 | ||
2102 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
2103 | { | |
d54725cd PNA |
2104 | struct nft_hook *h, *next; |
2105 | ||
2106 | list_for_each_entry_safe(h, next, &hook->list, list) { | |
2107 | list_del(&h->list); | |
2108 | kfree(h); | |
2109 | } | |
508f8ccd | 2110 | module_put(hook->type->owner); |
508f8ccd PNA |
2111 | } |
2112 | ||
0cbc06b3 FW |
2113 | struct nft_rules_old { |
2114 | struct rcu_head h; | |
2c865a8a | 2115 | struct nft_rule_blob *blob; |
0cbc06b3 FW |
2116 | }; |
2117 | ||
2c865a8a | 2118 | static void nft_last_rule(struct nft_rule_blob *blob, const void *ptr) |
0cbc06b3 | 2119 | { |
2c865a8a PNA |
2120 | struct nft_rule_dp *prule; |
2121 | ||
2122 | prule = (struct nft_rule_dp *)ptr; | |
2123 | prule->is_last = 1; | |
2c865a8a PNA |
2124 | /* blob size does not include the trailer rule */ |
2125 | } | |
2126 | ||
2127 | static struct nft_rule_blob *nf_tables_chain_alloc_rules(unsigned int size) | |
2128 | { | |
2129 | struct nft_rule_blob *blob; | |
2130 | ||
2131 | /* size must include room for the last rule */ | |
2132 | if (size < offsetof(struct nft_rule_dp, data)) | |
2133 | return NULL; | |
2134 | ||
2135 | size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rules_old); | |
2136 | if (size > INT_MAX) | |
0cbc06b3 FW |
2137 | return NULL; |
2138 | ||
33758c89 | 2139 | blob = kvmalloc(size, GFP_KERNEL_ACCOUNT); |
2c865a8a | 2140 | if (!blob) |
0cbc06b3 FW |
2141 | return NULL; |
2142 | ||
2c865a8a PNA |
2143 | blob->size = 0; |
2144 | nft_last_rule(blob, blob->data); | |
0cbc06b3 | 2145 | |
2c865a8a | 2146 | return blob; |
0cbc06b3 FW |
2147 | } |
2148 | ||
d54725cd PNA |
2149 | static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family, |
2150 | const struct nft_chain_hook *hook, | |
2151 | struct nft_chain *chain) | |
2152 | { | |
c5c6accd FW |
2153 | ops->pf = family; |
2154 | ops->hooknum = hook->num; | |
2155 | ops->priority = hook->priority; | |
2156 | ops->priv = chain; | |
2157 | ops->hook = hook->type->hooks[ops->hooknum]; | |
2158 | ops->hook_ops_type = NF_HOOK_OP_NF_TABLES; | |
d54725cd PNA |
2159 | } |
2160 | ||
2161 | static int nft_basechain_init(struct nft_base_chain *basechain, u8 family, | |
2162 | struct nft_chain_hook *hook, u32 flags) | |
2163 | { | |
2164 | struct nft_chain *chain; | |
2165 | struct nft_hook *h; | |
2166 | ||
2167 | basechain->type = hook->type; | |
2168 | INIT_LIST_HEAD(&basechain->hook_list); | |
2169 | chain = &basechain->chain; | |
2170 | ||
d3519cb8 | 2171 | if (nft_base_chain_netdev(family, hook->num)) { |
d54725cd PNA |
2172 | list_splice_init(&hook->list, &basechain->hook_list); |
2173 | list_for_each_entry(h, &basechain->hook_list, list) | |
2174 | nft_basechain_hook_init(&h->ops, family, hook, chain); | |
2175 | ||
2176 | basechain->ops.hooknum = hook->num; | |
2177 | basechain->ops.priority = hook->priority; | |
2178 | } else { | |
2179 | nft_basechain_hook_init(&basechain->ops, family, hook, chain); | |
2180 | } | |
2181 | ||
67c49de4 | 2182 | chain->flags |= NFT_CHAIN_BASE | flags; |
d54725cd PNA |
2183 | basechain->policy = NF_ACCEPT; |
2184 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD && | |
77972a36 PNA |
2185 | !nft_chain_offload_support(basechain)) { |
2186 | list_splice_init(&basechain->hook_list, &hook->list); | |
d54725cd | 2187 | return -EOPNOTSUPP; |
77972a36 | 2188 | } |
d54725cd PNA |
2189 | |
2190 | flow_block_init(&basechain->flow_block); | |
2191 | ||
2192 | return 0; | |
2193 | } | |
2194 | ||
04b7db41 PNA |
2195 | static int nft_chain_add(struct nft_table *table, struct nft_chain *chain) |
2196 | { | |
2197 | int err; | |
2198 | ||
2199 | err = rhltable_insert_key(&table->chains_ht, chain->name, | |
2200 | &chain->rhlhead, nft_chain_ht_params); | |
2201 | if (err) | |
2202 | return err; | |
2203 | ||
2204 | list_add_tail_rcu(&chain->list, &table->chains); | |
2205 | ||
2206 | return 0; | |
2207 | } | |
2208 | ||
d0e2c7de PNA |
2209 | static u64 chain_id; |
2210 | ||
4035285f | 2211 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
983c4fcb PNA |
2212 | u8 policy, u32 flags, |
2213 | struct netlink_ext_ack *extack) | |
4035285f PNA |
2214 | { |
2215 | const struct nlattr * const *nla = ctx->nla; | |
2216 | struct nft_table *table = ctx->table; | |
4035285f | 2217 | struct nft_base_chain *basechain; |
4035285f | 2218 | struct net *net = ctx->net; |
d0e2c7de | 2219 | char name[NFT_NAME_MAXLEN]; |
2c865a8a | 2220 | struct nft_rule_blob *blob; |
66293c46 | 2221 | struct nft_trans *trans; |
4035285f | 2222 | struct nft_chain *chain; |
2c865a8a | 2223 | unsigned int data_size; |
4035285f PNA |
2224 | int err; |
2225 | ||
2226 | if (table->use == UINT_MAX) | |
2227 | return -EOVERFLOW; | |
2228 | ||
2229 | if (nla[NFTA_CHAIN_HOOK]) { | |
921ebde3 | 2230 | struct nft_stats __percpu *stats = NULL; |
4035285f | 2231 | struct nft_chain_hook hook; |
4035285f | 2232 | |
d0e2c7de PNA |
2233 | if (flags & NFT_CHAIN_BINDING) |
2234 | return -EOPNOTSUPP; | |
2235 | ||
983c4fcb PNA |
2236 | err = nft_chain_parse_hook(net, nla, &hook, family, extack, |
2237 | true); | |
4035285f PNA |
2238 | if (err < 0) |
2239 | return err; | |
2240 | ||
33758c89 | 2241 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2242 | if (basechain == NULL) { |
2243 | nft_chain_release_hook(&hook); | |
2244 | return -ENOMEM; | |
2245 | } | |
d54725cd | 2246 | chain = &basechain->chain; |
4035285f PNA |
2247 | |
2248 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2249 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2250 | if (IS_ERR(stats)) { | |
2251 | nft_chain_release_hook(&hook); | |
2252 | kfree(basechain); | |
2253 | return PTR_ERR(stats); | |
2254 | } | |
4c05ec47 | 2255 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
2256 | } |
2257 | ||
d54725cd PNA |
2258 | err = nft_basechain_init(basechain, family, &hook, flags); |
2259 | if (err < 0) { | |
2260 | nft_chain_release_hook(&hook); | |
2261 | kfree(basechain); | |
9a4d6dd5 | 2262 | free_percpu(stats); |
d54725cd PNA |
2263 | return err; |
2264 | } | |
921ebde3 TH |
2265 | if (stats) |
2266 | static_branch_inc(&nft_counters_enabled); | |
4035285f | 2267 | } else { |
d0e2c7de PNA |
2268 | if (flags & NFT_CHAIN_BASE) |
2269 | return -EINVAL; | |
2270 | if (flags & NFT_CHAIN_HW_OFFLOAD) | |
2271 | return -EOPNOTSUPP; | |
2272 | ||
33758c89 | 2273 | chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2274 | if (chain == NULL) |
2275 | return -ENOMEM; | |
d0e2c7de PNA |
2276 | |
2277 | chain->flags = flags; | |
4035285f | 2278 | } |
43a605f2 PNA |
2279 | ctx->chain = chain; |
2280 | ||
4035285f PNA |
2281 | INIT_LIST_HEAD(&chain->rules); |
2282 | chain->handle = nf_tables_alloc_handle(table); | |
2283 | chain->table = table; | |
d0e2c7de PNA |
2284 | |
2285 | if (nla[NFTA_CHAIN_NAME]) { | |
33758c89 | 2286 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
d0e2c7de | 2287 | } else { |
59136aa3 FW |
2288 | if (!(flags & NFT_CHAIN_BINDING)) { |
2289 | err = -EINVAL; | |
002f2176 | 2290 | goto err_destroy_chain; |
59136aa3 | 2291 | } |
d0e2c7de PNA |
2292 | |
2293 | snprintf(name, sizeof(name), "__chain%llu", ++chain_id); | |
33758c89 | 2294 | chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT); |
d0e2c7de PNA |
2295 | } |
2296 | ||
4035285f PNA |
2297 | if (!chain->name) { |
2298 | err = -ENOMEM; | |
002f2176 JGG |
2299 | goto err_destroy_chain; |
2300 | } | |
2301 | ||
2302 | if (nla[NFTA_CHAIN_USERDATA]) { | |
33758c89 | 2303 | chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT); |
002f2176 JGG |
2304 | if (chain->udata == NULL) { |
2305 | err = -ENOMEM; | |
2306 | goto err_destroy_chain; | |
2307 | } | |
2308 | chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]); | |
4035285f PNA |
2309 | } |
2310 | ||
2c865a8a PNA |
2311 | data_size = offsetof(struct nft_rule_dp, data); /* last rule */ |
2312 | blob = nf_tables_chain_alloc_rules(data_size); | |
2313 | if (!blob) { | |
0cbc06b3 | 2314 | err = -ENOMEM; |
002f2176 | 2315 | goto err_destroy_chain; |
0cbc06b3 FW |
2316 | } |
2317 | ||
2c865a8a PNA |
2318 | RCU_INIT_POINTER(chain->blob_gen_0, blob); |
2319 | RCU_INIT_POINTER(chain->blob_gen_1, blob); | |
0cbc06b3 | 2320 | |
c974a3a3 | 2321 | err = nf_tables_register_hook(net, table, chain); |
4035285f | 2322 | if (err < 0) |
002f2176 | 2323 | goto err_destroy_chain; |
4035285f | 2324 | |
66293c46 FW |
2325 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
2326 | if (IS_ERR(trans)) { | |
2327 | err = PTR_ERR(trans); | |
002f2176 | 2328 | goto err_unregister_hook; |
1b2470e5 | 2329 | } |
4035285f | 2330 | |
ad652f38 | 2331 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
2332 | if (nft_is_base_chain(chain)) |
2333 | nft_trans_chain_policy(trans) = policy; | |
2334 | ||
04b7db41 PNA |
2335 | err = nft_chain_add(table, chain); |
2336 | if (err < 0) { | |
2337 | nft_trans_destroy(trans); | |
002f2176 | 2338 | goto err_unregister_hook; |
04b7db41 PNA |
2339 | } |
2340 | ||
4035285f | 2341 | table->use++; |
4035285f PNA |
2342 | |
2343 | return 0; | |
002f2176 | 2344 | err_unregister_hook: |
c974a3a3 | 2345 | nf_tables_unregister_hook(net, table, chain); |
002f2176 | 2346 | err_destroy_chain: |
43a605f2 | 2347 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2348 | |
2349 | return err; | |
2350 | } | |
2351 | ||
d54725cd PNA |
2352 | static bool nft_hook_list_equal(struct list_head *hook_list1, |
2353 | struct list_head *hook_list2) | |
2354 | { | |
2355 | struct nft_hook *hook; | |
2356 | int n = 0, m = 0; | |
2357 | ||
2358 | n = 0; | |
2359 | list_for_each_entry(hook, hook_list2, list) { | |
2360 | if (!nft_hook_list_find(hook_list1, hook)) | |
2361 | return false; | |
2362 | ||
2363 | n++; | |
2364 | } | |
2365 | list_for_each_entry(hook, hook_list1, list) | |
2366 | m++; | |
2367 | ||
2368 | return n == m; | |
2369 | } | |
2370 | ||
c9626a2c | 2371 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
98a381a7 PNA |
2372 | u32 flags, const struct nlattr *attr, |
2373 | struct netlink_ext_ack *extack) | |
2c4a488a PNA |
2374 | { |
2375 | const struct nlattr * const *nla = ctx->nla; | |
2376 | struct nft_table *table = ctx->table; | |
2377 | struct nft_chain *chain = ctx->chain; | |
2c4a488a PNA |
2378 | struct nft_base_chain *basechain; |
2379 | struct nft_stats *stats = NULL; | |
2380 | struct nft_chain_hook hook; | |
2c4a488a PNA |
2381 | struct nf_hook_ops *ops; |
2382 | struct nft_trans *trans; | |
c974a3a3 | 2383 | int err; |
2c4a488a | 2384 | |
c9626a2c PNA |
2385 | if (chain->flags ^ flags) |
2386 | return -EOPNOTSUPP; | |
2387 | ||
2c4a488a | 2388 | if (nla[NFTA_CHAIN_HOOK]) { |
98a381a7 PNA |
2389 | if (!nft_is_base_chain(chain)) { |
2390 | NL_SET_BAD_ATTR(extack, attr); | |
77a92189 | 2391 | return -EEXIST; |
98a381a7 | 2392 | } |
36596dad | 2393 | err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family, |
983c4fcb | 2394 | extack, false); |
2c4a488a PNA |
2395 | if (err < 0) |
2396 | return err; | |
2397 | ||
2398 | basechain = nft_base_chain(chain); | |
2399 | if (basechain->type != hook.type) { | |
2400 | nft_chain_release_hook(&hook); | |
98a381a7 | 2401 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2402 | return -EEXIST; |
2c4a488a PNA |
2403 | } |
2404 | ||
d3519cb8 | 2405 | if (nft_base_chain_netdev(ctx->family, hook.num)) { |
d54725cd PNA |
2406 | if (!nft_hook_list_equal(&basechain->hook_list, |
2407 | &hook.list)) { | |
2408 | nft_chain_release_hook(&hook); | |
98a381a7 | 2409 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2410 | return -EEXIST; |
d54725cd PNA |
2411 | } |
2412 | } else { | |
2413 | ops = &basechain->ops; | |
2414 | if (ops->hooknum != hook.num || | |
2415 | ops->priority != hook.priority) { | |
2416 | nft_chain_release_hook(&hook); | |
98a381a7 | 2417 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2418 | return -EEXIST; |
d54725cd | 2419 | } |
2c4a488a PNA |
2420 | } |
2421 | nft_chain_release_hook(&hook); | |
2422 | } | |
2423 | ||
2424 | if (nla[NFTA_CHAIN_HANDLE] && | |
2425 | nla[NFTA_CHAIN_NAME]) { | |
2426 | struct nft_chain *chain2; | |
2427 | ||
f102d66b FW |
2428 | chain2 = nft_chain_lookup(ctx->net, table, |
2429 | nla[NFTA_CHAIN_NAME], genmask); | |
98a381a7 PNA |
2430 | if (!IS_ERR(chain2)) { |
2431 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
0d18779b | 2432 | return -EEXIST; |
98a381a7 | 2433 | } |
2c4a488a PNA |
2434 | } |
2435 | ||
2436 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2437 | if (!nft_is_base_chain(chain)) | |
2438 | return -EOPNOTSUPP; | |
2439 | ||
2440 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2441 | if (IS_ERR(stats)) | |
2442 | return PTR_ERR(stats); | |
2443 | } | |
2444 | ||
c6cc94df | 2445 | err = -ENOMEM; |
2c4a488a PNA |
2446 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2447 | sizeof(struct nft_trans_chain)); | |
c6cc94df FW |
2448 | if (trans == NULL) |
2449 | goto err; | |
2c4a488a PNA |
2450 | |
2451 | nft_trans_chain_stats(trans) = stats; | |
2452 | nft_trans_chain_update(trans) = true; | |
2453 | ||
2454 | if (nla[NFTA_CHAIN_POLICY]) | |
2455 | nft_trans_chain_policy(trans) = policy; | |
2456 | else | |
2457 | nft_trans_chain_policy(trans) = -1; | |
2458 | ||
c6cc94df FW |
2459 | if (nla[NFTA_CHAIN_HANDLE] && |
2460 | nla[NFTA_CHAIN_NAME]) { | |
d59d2f82 | 2461 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c6cc94df FW |
2462 | struct nft_trans *tmp; |
2463 | char *name; | |
2464 | ||
2465 | err = -ENOMEM; | |
33758c89 | 2466 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
c6cc94df FW |
2467 | if (!name) |
2468 | goto err; | |
2469 | ||
2470 | err = -EEXIST; | |
0854db2a | 2471 | list_for_each_entry(tmp, &nft_net->commit_list, list) { |
c6cc94df FW |
2472 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && |
2473 | tmp->ctx.table == table && | |
2474 | nft_trans_chain_update(tmp) && | |
2475 | nft_trans_chain_name(tmp) && | |
2476 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
98a381a7 | 2477 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); |
c6cc94df FW |
2478 | kfree(name); |
2479 | goto err; | |
2480 | } | |
2c4a488a | 2481 | } |
c6cc94df FW |
2482 | |
2483 | nft_trans_chain_name(trans) = name; | |
2c4a488a | 2484 | } |
0854db2a | 2485 | nft_trans_commit_list_add_tail(ctx->net, trans); |
2c4a488a PNA |
2486 | |
2487 | return 0; | |
c6cc94df FW |
2488 | err: |
2489 | free_percpu(stats); | |
2490 | kfree(trans); | |
2491 | return err; | |
2c4a488a PNA |
2492 | } |
2493 | ||
837830a4 | 2494 | static struct nft_chain *nft_chain_lookup_byid(const struct net *net, |
95f466d2 | 2495 | const struct nft_table *table, |
837830a4 PNA |
2496 | const struct nlattr *nla) |
2497 | { | |
d59d2f82 | 2498 | struct nftables_pernet *nft_net = nft_pernet(net); |
837830a4 PNA |
2499 | u32 id = ntohl(nla_get_be32(nla)); |
2500 | struct nft_trans *trans; | |
2501 | ||
0854db2a | 2502 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
837830a4 PNA |
2503 | struct nft_chain *chain = trans->ctx.chain; |
2504 | ||
2505 | if (trans->msg_type == NFT_MSG_NEWCHAIN && | |
95f466d2 | 2506 | chain->table == table && |
837830a4 PNA |
2507 | id == nft_trans_chain_id(trans)) |
2508 | return chain; | |
2509 | } | |
2510 | return ERR_PTR(-ENOENT); | |
2511 | } | |
2512 | ||
7dab8ee3 PNA |
2513 | static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info, |
2514 | const struct nlattr * const nla[]) | |
96518518 | 2515 | { |
7dab8ee3 | 2516 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
2517 | struct netlink_ext_ack *extack = info->extack; |
2518 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2519 | u8 family = info->nfmsg->nfgen_family; |
74cccc3d | 2520 | struct nft_chain *chain = NULL; |
7dab8ee3 | 2521 | struct net *net = info->net; |
36dd1bcc | 2522 | const struct nlattr *attr; |
96518518 | 2523 | struct nft_table *table; |
57de2a0c | 2524 | u8 policy = NF_ACCEPT; |
4035285f | 2525 | struct nft_ctx ctx; |
96518518 | 2526 | u64 handle = 0; |
c9626a2c | 2527 | u32 flags = 0; |
96518518 | 2528 | |
0854db2a | 2529 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 2530 | |
6001a930 PNA |
2531 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2532 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2533 | if (IS_ERR(table)) { |
2534 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2535 | return PTR_ERR(table); |
36dd1bcc | 2536 | } |
96518518 | 2537 | |
96518518 | 2538 | chain = NULL; |
36dd1bcc | 2539 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2540 | |
2541 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2542 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2543 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2544 | if (IS_ERR(chain)) { |
2545 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2546 | return PTR_ERR(chain); |
36dd1bcc PNA |
2547 | } |
2548 | attr = nla[NFTA_CHAIN_HANDLE]; | |
74cccc3d | 2549 | } else if (nla[NFTA_CHAIN_NAME]) { |
f102d66b | 2550 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2551 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2552 | if (PTR_ERR(chain) != -ENOENT) { |
2553 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2554 | return PTR_ERR(chain); |
36dd1bcc | 2555 | } |
96518518 PM |
2556 | chain = NULL; |
2557 | } | |
74cccc3d PNA |
2558 | } else if (!nla[NFTA_CHAIN_ID]) { |
2559 | return -EINVAL; | |
96518518 PM |
2560 | } |
2561 | ||
57de2a0c | 2562 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2563 | if (chain != NULL && |
36dd1bcc PNA |
2564 | !nft_is_base_chain(chain)) { |
2565 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2566 | return -EOPNOTSUPP; |
36dd1bcc | 2567 | } |
d6b6cb1d PNA |
2568 | |
2569 | if (chain == NULL && | |
36dd1bcc PNA |
2570 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2571 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2572 | return -EOPNOTSUPP; |
36dd1bcc | 2573 | } |
57de2a0c | 2574 | |
8f46df18 | 2575 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2576 | switch (policy) { |
2577 | case NF_DROP: | |
2578 | case NF_ACCEPT: | |
2579 | break; | |
2580 | default: | |
2581 | return -EINVAL; | |
2582 | } | |
2583 | } | |
2584 | ||
c9626a2c PNA |
2585 | if (nla[NFTA_CHAIN_FLAGS]) |
2586 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2587 | else if (chain) |
2588 | flags = chain->flags; | |
c9626a2c | 2589 | |
c1f79a2e PNA |
2590 | if (flags & ~NFT_CHAIN_FLAGS) |
2591 | return -EOPNOTSUPP; | |
2592 | ||
7dab8ee3 | 2593 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
4035285f | 2594 | |
96518518 | 2595 | if (chain != NULL) { |
5dc52d83 PNA |
2596 | if (chain->flags & NFT_CHAIN_BINDING) |
2597 | return -EINVAL; | |
2598 | ||
7dab8ee3 | 2599 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 2600 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2601 | return -EEXIST; |
36dd1bcc | 2602 | } |
7dab8ee3 | 2603 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
2604 | return -EOPNOTSUPP; |
2605 | ||
67c49de4 | 2606 | flags |= chain->flags & NFT_CHAIN_BASE; |
98a381a7 PNA |
2607 | return nf_tables_updchain(&ctx, genmask, policy, flags, attr, |
2608 | extack); | |
96518518 PM |
2609 | } |
2610 | ||
983c4fcb | 2611 | return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack); |
96518518 PM |
2612 | } |
2613 | ||
7dab8ee3 PNA |
2614 | static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info, |
2615 | const struct nlattr * const nla[]) | |
96518518 | 2616 | { |
7dab8ee3 PNA |
2617 | struct netlink_ext_ack *extack = info->extack; |
2618 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2619 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 2620 | struct net *net = info->net; |
36dd1bcc | 2621 | const struct nlattr *attr; |
96518518 PM |
2622 | struct nft_table *table; |
2623 | struct nft_chain *chain; | |
9dee1474 | 2624 | struct nft_rule *rule; |
91c7b38d | 2625 | struct nft_ctx ctx; |
3ecbfd65 | 2626 | u64 handle; |
9dee1474 PNA |
2627 | u32 use; |
2628 | int err; | |
96518518 | 2629 | |
6001a930 PNA |
2630 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2631 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2632 | if (IS_ERR(table)) { |
2633 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2634 | return PTR_ERR(table); |
36dd1bcc | 2635 | } |
96518518 | 2636 | |
3ecbfd65 | 2637 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2638 | attr = nla[NFTA_CHAIN_HANDLE]; |
2639 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2640 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2641 | } else { |
36dd1bcc | 2642 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2643 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2644 | } |
36dd1bcc | 2645 | if (IS_ERR(chain)) { |
f80a612d FFM |
2646 | if (PTR_ERR(chain) == -ENOENT && |
2647 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN) | |
2648 | return 0; | |
2649 | ||
36dd1bcc | 2650 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2651 | return PTR_ERR(chain); |
36dd1bcc | 2652 | } |
9dee1474 | 2653 | |
7dab8ee3 | 2654 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
9dee1474 | 2655 | chain->use > 0) |
96518518 PM |
2656 | return -EBUSY; |
2657 | ||
7dab8ee3 | 2658 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0165d932 | 2659 | |
9dee1474 PNA |
2660 | use = chain->use; |
2661 | list_for_each_entry(rule, &chain->rules, list) { | |
2662 | if (!nft_is_active_next(net, rule)) | |
2663 | continue; | |
2664 | use--; | |
2665 | ||
2666 | err = nft_delrule(&ctx, rule); | |
2667 | if (err < 0) | |
2668 | return err; | |
2669 | } | |
2670 | ||
2671 | /* There are rules and elements that are still holding references to us, | |
2672 | * we cannot do a recursive removal in this case. | |
2673 | */ | |
36dd1bcc PNA |
2674 | if (use > 0) { |
2675 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2676 | return -EBUSY; |
36dd1bcc | 2677 | } |
9dee1474 | 2678 | |
ee01d542 | 2679 | return nft_delchain(&ctx); |
96518518 PM |
2680 | } |
2681 | ||
96518518 PM |
2682 | /* |
2683 | * Expressions | |
2684 | */ | |
2685 | ||
2686 | /** | |
ef1f7df9 | 2687 | * nft_register_expr - register nf_tables expr type |
3db86c39 | 2688 | * @type: expr type |
96518518 | 2689 | * |
ef1f7df9 | 2690 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2691 | * success or a negative errno code otherwise. |
2692 | */ | |
ef1f7df9 | 2693 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2694 | { |
2695 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2696 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2697 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2698 | else |
e688a7f8 | 2699 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2700 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2701 | return 0; | |
2702 | } | |
2703 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2704 | ||
2705 | /** | |
ef1f7df9 | 2706 | * nft_unregister_expr - unregister nf_tables expr type |
3db86c39 | 2707 | * @type: expr type |
96518518 | 2708 | * |
ef1f7df9 | 2709 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2710 | */ |
ef1f7df9 | 2711 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2712 | { |
2713 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2714 | list_del_rcu(&type->list); |
96518518 PM |
2715 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2716 | } | |
2717 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2718 | ||
64d46806 PM |
2719 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2720 | struct nlattr *nla) | |
96518518 | 2721 | { |
9cff126f | 2722 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2723 | |
ef1f7df9 | 2724 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2725 | if (!nla_strcmp(nla, type->name)) { |
2726 | if (!type->family && !candidate) | |
2727 | candidate = type; | |
2728 | else if (type->family == family) | |
2729 | candidate = type; | |
2730 | } | |
96518518 | 2731 | } |
9cff126f | 2732 | return candidate; |
96518518 PM |
2733 | } |
2734 | ||
b9c04ae7 PNA |
2735 | #ifdef CONFIG_MODULES |
2736 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
2737 | struct nlattr *nla) | |
2738 | { | |
eb014de4 PNA |
2739 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
2740 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
2741 | return -EAGAIN; |
2742 | ||
2743 | return 0; | |
2744 | } | |
2745 | #endif | |
2746 | ||
452238e8 FW |
2747 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
2748 | u8 family, | |
64d46806 | 2749 | struct nlattr *nla) |
96518518 | 2750 | { |
ef1f7df9 | 2751 | const struct nft_expr_type *type; |
96518518 PM |
2752 | |
2753 | if (nla == NULL) | |
2754 | return ERR_PTR(-EINVAL); | |
2755 | ||
64d46806 | 2756 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
2757 | if (type != NULL && try_module_get(type->owner)) |
2758 | return type; | |
96518518 | 2759 | |
f102d66b | 2760 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 2761 | #ifdef CONFIG_MODULES |
ef1f7df9 | 2762 | if (type == NULL) { |
b9c04ae7 | 2763 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
2764 | return ERR_PTR(-EAGAIN); |
2765 | ||
eb014de4 PNA |
2766 | if (nft_request_module(net, "nft-expr-%.*s", |
2767 | nla_len(nla), | |
2768 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
2769 | return ERR_PTR(-EAGAIN); |
2770 | } | |
2771 | #endif | |
2772 | return ERR_PTR(-ENOENT); | |
2773 | } | |
2774 | ||
2775 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
2776 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
2777 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
2778 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
2779 | }; | |
2780 | ||
2781 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
8daa8fde | 2782 | const struct nft_expr *expr, bool reset) |
96518518 | 2783 | { |
ef1f7df9 | 2784 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
2785 | goto nla_put_failure; |
2786 | ||
2787 | if (expr->ops->dump) { | |
ae0be8de MK |
2788 | struct nlattr *data = nla_nest_start_noflag(skb, |
2789 | NFTA_EXPR_DATA); | |
96518518 PM |
2790 | if (data == NULL) |
2791 | goto nla_put_failure; | |
8daa8fde | 2792 | if (expr->ops->dump(skb, expr, reset) < 0) |
96518518 PM |
2793 | goto nla_put_failure; |
2794 | nla_nest_end(skb, data); | |
2795 | } | |
2796 | ||
2797 | return skb->len; | |
2798 | ||
2799 | nla_put_failure: | |
2800 | return -1; | |
2801 | }; | |
2802 | ||
0b2d8a7b | 2803 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
8daa8fde | 2804 | const struct nft_expr *expr, bool reset) |
0b2d8a7b PM |
2805 | { |
2806 | struct nlattr *nest; | |
2807 | ||
ae0be8de | 2808 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
2809 | if (!nest) |
2810 | goto nla_put_failure; | |
8daa8fde | 2811 | if (nf_tables_fill_expr_info(skb, expr, reset) < 0) |
0b2d8a7b PM |
2812 | goto nla_put_failure; |
2813 | nla_nest_end(skb, nest); | |
2814 | return 0; | |
2815 | ||
2816 | nla_put_failure: | |
2817 | return -1; | |
2818 | } | |
2819 | ||
96518518 PM |
2820 | struct nft_expr_info { |
2821 | const struct nft_expr_ops *ops; | |
83d9dcba | 2822 | const struct nlattr *attr; |
ef1f7df9 | 2823 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
2824 | }; |
2825 | ||
0ca743a5 PNA |
2826 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
2827 | const struct nlattr *nla, | |
96518518 PM |
2828 | struct nft_expr_info *info) |
2829 | { | |
ef1f7df9 | 2830 | const struct nft_expr_type *type; |
96518518 | 2831 | const struct nft_expr_ops *ops; |
ef1f7df9 | 2832 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
2833 | int err; |
2834 | ||
8cb08174 JB |
2835 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
2836 | nft_expr_policy, NULL); | |
96518518 PM |
2837 | if (err < 0) |
2838 | return err; | |
2839 | ||
452238e8 | 2840 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
2841 | if (IS_ERR(type)) |
2842 | return PTR_ERR(type); | |
2843 | ||
2844 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
2845 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
2846 | tb[NFTA_EXPR_DATA], | |
2847 | type->policy, NULL); | |
ef1f7df9 PM |
2848 | if (err < 0) |
2849 | goto err1; | |
2850 | } else | |
2851 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
2852 | ||
2853 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
2854 | ops = type->select_ops(ctx, |
2855 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
2856 | if (IS_ERR(ops)) { |
2857 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
2858 | #ifdef CONFIG_MODULES |
2859 | if (err == -EAGAIN) | |
eb014de4 PNA |
2860 | if (nft_expr_type_request_module(ctx->net, |
2861 | ctx->family, | |
2862 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
2863 | err = -ENOENT; | |
0ef1efd1 | 2864 | #endif |
ef1f7df9 PM |
2865 | goto err1; |
2866 | } | |
2867 | } else | |
2868 | ops = type->ops; | |
2869 | ||
83d9dcba | 2870 | info->attr = nla; |
96518518 | 2871 | info->ops = ops; |
83d9dcba | 2872 | |
96518518 | 2873 | return 0; |
ef1f7df9 PM |
2874 | |
2875 | err1: | |
2876 | module_put(type->owner); | |
2877 | return err; | |
96518518 PM |
2878 | } |
2879 | ||
3a07327d PNA |
2880 | int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla, |
2881 | struct nft_expr_info *info) | |
2882 | { | |
2883 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; | |
2884 | const struct nft_expr_type *type; | |
2885 | int err; | |
2886 | ||
2887 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, | |
2888 | nft_expr_policy, NULL); | |
2889 | if (err < 0) | |
2890 | return err; | |
2891 | ||
2892 | if (!tb[NFTA_EXPR_DATA]) | |
2893 | return -EINVAL; | |
2894 | ||
2895 | type = __nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]); | |
98cbc40e DC |
2896 | if (!type) |
2897 | return -ENOENT; | |
3a07327d PNA |
2898 | |
2899 | if (!type->inner_ops) | |
2900 | return -EOPNOTSUPP; | |
2901 | ||
2902 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, | |
2903 | tb[NFTA_EXPR_DATA], | |
2904 | type->policy, NULL); | |
2905 | if (err < 0) | |
2906 | goto err_nla_parse; | |
2907 | ||
2908 | info->attr = nla; | |
2909 | info->ops = type->inner_ops; | |
2910 | ||
2911 | return 0; | |
2912 | ||
2913 | err_nla_parse: | |
2914 | return err; | |
2915 | } | |
2916 | ||
96518518 | 2917 | static int nf_tables_newexpr(const struct nft_ctx *ctx, |
7dab8ee3 | 2918 | const struct nft_expr_info *expr_info, |
96518518 PM |
2919 | struct nft_expr *expr) |
2920 | { | |
7dab8ee3 | 2921 | const struct nft_expr_ops *ops = expr_info->ops; |
96518518 PM |
2922 | int err; |
2923 | ||
2924 | expr->ops = ops; | |
2925 | if (ops->init) { | |
7dab8ee3 | 2926 | err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb); |
96518518 PM |
2927 | if (err < 0) |
2928 | goto err1; | |
2929 | } | |
2930 | ||
96518518 | 2931 | return 0; |
96518518 PM |
2932 | err1: |
2933 | expr->ops = NULL; | |
2934 | return err; | |
2935 | } | |
2936 | ||
62472bce PM |
2937 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
2938 | struct nft_expr *expr) | |
96518518 | 2939 | { |
3f3a390d PNA |
2940 | const struct nft_expr_type *type = expr->ops->type; |
2941 | ||
96518518 | 2942 | if (expr->ops->destroy) |
62472bce | 2943 | expr->ops->destroy(ctx, expr); |
3f3a390d | 2944 | module_put(type->owner); |
96518518 PM |
2945 | } |
2946 | ||
795a6d6b PNA |
2947 | static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
2948 | const struct nlattr *nla) | |
0b2d8a7b | 2949 | { |
7dab8ee3 | 2950 | struct nft_expr_info expr_info; |
0b2d8a7b | 2951 | struct nft_expr *expr; |
b8e20400 | 2952 | struct module *owner; |
0b2d8a7b PM |
2953 | int err; |
2954 | ||
7dab8ee3 | 2955 | err = nf_tables_expr_parse(ctx, nla, &expr_info); |
0b2d8a7b | 2956 | if (err < 0) |
52077804 PNA |
2957 | goto err_expr_parse; |
2958 | ||
2959 | err = -EOPNOTSUPP; | |
2960 | if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL)) | |
2961 | goto err_expr_stateful; | |
0b2d8a7b PM |
2962 | |
2963 | err = -ENOMEM; | |
33758c89 | 2964 | expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT); |
0b2d8a7b | 2965 | if (expr == NULL) |
52077804 | 2966 | goto err_expr_stateful; |
0b2d8a7b | 2967 | |
7dab8ee3 | 2968 | err = nf_tables_newexpr(ctx, &expr_info, expr); |
0b2d8a7b | 2969 | if (err < 0) |
52077804 | 2970 | goto err_expr_new; |
0b2d8a7b PM |
2971 | |
2972 | return expr; | |
52077804 | 2973 | err_expr_new: |
6cafaf47 | 2974 | kfree(expr); |
52077804 | 2975 | err_expr_stateful: |
7dab8ee3 PNA |
2976 | owner = expr_info.ops->type->owner; |
2977 | if (expr_info.ops->type->release_ops) | |
2978 | expr_info.ops->type->release_ops(expr_info.ops); | |
b8e20400 PNA |
2979 | |
2980 | module_put(owner); | |
52077804 | 2981 | err_expr_parse: |
0b2d8a7b PM |
2982 | return ERR_PTR(err); |
2983 | } | |
2984 | ||
c604cc69 PNA |
2985 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
2986 | { | |
2987 | int err; | |
2988 | ||
2989 | if (src->ops->clone) { | |
2990 | dst->ops = src->ops; | |
2991 | err = src->ops->clone(dst, src); | |
2992 | if (err < 0) | |
2993 | return err; | |
2994 | } else { | |
2995 | memcpy(dst, src, src->ops->size); | |
2996 | } | |
2997 | ||
2998 | __module_get(src->ops->type->owner); | |
2999 | ||
3000 | return 0; | |
3001 | } | |
3002 | ||
0b2d8a7b PM |
3003 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
3004 | { | |
3005 | nf_tables_expr_destroy(ctx, expr); | |
3006 | kfree(expr); | |
3007 | } | |
3008 | ||
96518518 PM |
3009 | /* |
3010 | * Rules | |
3011 | */ | |
3012 | ||
cac20fcd PNA |
3013 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
3014 | u64 handle) | |
96518518 PM |
3015 | { |
3016 | struct nft_rule *rule; | |
3017 | ||
3018 | // FIXME: this sucks | |
d9adf22a | 3019 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
3020 | if (handle == rule->handle) |
3021 | return rule; | |
3022 | } | |
3023 | ||
3024 | return ERR_PTR(-ENOENT); | |
3025 | } | |
3026 | ||
cac20fcd PNA |
3027 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
3028 | const struct nlattr *nla) | |
96518518 PM |
3029 | { |
3030 | if (nla == NULL) | |
3031 | return ERR_PTR(-EINVAL); | |
3032 | ||
cac20fcd | 3033 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
3034 | } |
3035 | ||
3036 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
3037 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
3038 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
3039 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
3040 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
3041 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
3042 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 3043 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 3044 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
3045 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
3046 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 3047 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 3048 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
837830a4 | 3049 | [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
3050 | }; |
3051 | ||
84d7fce6 PNA |
3052 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
3053 | u32 portid, u32 seq, int event, | |
3054 | u32 flags, int family, | |
96518518 PM |
3055 | const struct nft_table *table, |
3056 | const struct nft_chain *chain, | |
8daa8fde PS |
3057 | const struct nft_rule *rule, u64 handle, |
3058 | bool reset) | |
96518518 PM |
3059 | { |
3060 | struct nlmsghdr *nlh; | |
96518518 PM |
3061 | const struct nft_expr *expr, *next; |
3062 | struct nlattr *list; | |
dedb67c4 | 3063 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 3064 | |
19c28b13 PNA |
3065 | nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0, |
3066 | nft_base_seq(net)); | |
3067 | if (!nlh) | |
96518518 PM |
3068 | goto nla_put_failure; |
3069 | ||
96518518 PM |
3070 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
3071 | goto nla_put_failure; | |
3072 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
3073 | goto nla_put_failure; | |
b46f6ded ND |
3074 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
3075 | NFTA_RULE_PAD)) | |
96518518 PM |
3076 | goto nla_put_failure; |
3077 | ||
e189ae16 PNA |
3078 | if (event != NFT_MSG_DELRULE && handle) { |
3079 | if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle), | |
b46f6ded | 3080 | NFTA_RULE_PAD)) |
5e948466 EL |
3081 | goto nla_put_failure; |
3082 | } | |
3083 | ||
b72920f6 PNA |
3084 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) |
3085 | nft_flow_rule_stats(chain, rule); | |
3086 | ||
ae0be8de | 3087 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
3088 | if (list == NULL) |
3089 | goto nla_put_failure; | |
3090 | nft_rule_for_each_expr(expr, next, rule) { | |
8daa8fde | 3091 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0) |
96518518 | 3092 | goto nla_put_failure; |
96518518 PM |
3093 | } |
3094 | nla_nest_end(skb, list); | |
3095 | ||
86f1ec32 PM |
3096 | if (rule->udata) { |
3097 | struct nft_userdata *udata = nft_userdata(rule); | |
3098 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
3099 | udata->data) < 0) | |
3100 | goto nla_put_failure; | |
3101 | } | |
0768b3b3 | 3102 | |
053c095a JB |
3103 | nlmsg_end(skb, nlh); |
3104 | return 0; | |
96518518 PM |
3105 | |
3106 | nla_put_failure: | |
3107 | nlmsg_trim(skb, nlh); | |
3108 | return -1; | |
3109 | } | |
3110 | ||
25e94a99 PNA |
3111 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
3112 | const struct nft_rule *rule, int event) | |
96518518 | 3113 | { |
d59d2f82 | 3114 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
e189ae16 | 3115 | const struct nft_rule *prule; |
96518518 | 3116 | struct sk_buff *skb; |
e189ae16 PNA |
3117 | u64 handle = 0; |
3118 | u16 flags = 0; | |
96518518 PM |
3119 | int err; |
3120 | ||
128ad332 PNA |
3121 | if (!ctx->report && |
3122 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3123 | return; |
96518518 | 3124 | |
96518518 PM |
3125 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
3126 | if (skb == NULL) | |
3127 | goto err; | |
3128 | ||
e189ae16 PNA |
3129 | if (event == NFT_MSG_NEWRULE && |
3130 | !list_is_first(&rule->list, &ctx->chain->rules) && | |
3131 | !list_is_last(&rule->list, &ctx->chain->rules)) { | |
3132 | prule = list_prev_entry(rule, list); | |
3133 | handle = prule->handle; | |
3134 | } | |
3135 | if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE)) | |
3136 | flags |= NLM_F_APPEND; | |
6fb721cf PNA |
3137 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
3138 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
e189ae16 | 3139 | |
84d7fce6 | 3140 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
e189ae16 | 3141 | event, flags, ctx->family, ctx->table, |
8daa8fde | 3142 | ctx->chain, rule, handle, false); |
96518518 PM |
3143 | if (err < 0) { |
3144 | kfree_skb(skb); | |
3145 | goto err; | |
3146 | } | |
3147 | ||
0854db2a | 3148 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 3149 | return; |
96518518 | 3150 | err: |
25e94a99 | 3151 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
3152 | } |
3153 | ||
6e1f760e | 3154 | struct nft_rule_dump_ctx { |
e46abbcc | 3155 | char *table; |
b7263e07 | 3156 | char *chain; |
6e1f760e PNA |
3157 | }; |
3158 | ||
241faece PS |
3159 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
3160 | unsigned int *idx, | |
3161 | struct netlink_callback *cb, | |
3162 | const struct nft_table *table, | |
8daa8fde PS |
3163 | const struct nft_chain *chain, |
3164 | bool reset) | |
241faece PS |
3165 | { |
3166 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 3167 | const struct nft_rule *rule, *prule; |
241faece | 3168 | unsigned int s_idx = cb->args[0]; |
e189ae16 | 3169 | u64 handle; |
241faece | 3170 | |
2c82c7e7 | 3171 | prule = NULL; |
241faece PS |
3172 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
3173 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 3174 | goto cont_skip; |
241faece PS |
3175 | if (*idx < s_idx) |
3176 | goto cont; | |
3177 | if (*idx > s_idx) { | |
3178 | memset(&cb->args[1], 0, | |
3179 | sizeof(cb->args) - sizeof(cb->args[0])); | |
3180 | } | |
e189ae16 PNA |
3181 | if (prule) |
3182 | handle = prule->handle; | |
3183 | else | |
3184 | handle = 0; | |
3185 | ||
241faece PS |
3186 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
3187 | cb->nlh->nlmsg_seq, | |
3188 | NFT_MSG_NEWRULE, | |
3189 | NLM_F_MULTI | NLM_F_APPEND, | |
3190 | table->family, | |
8daa8fde | 3191 | table, chain, rule, handle, reset) < 0) |
310529e6 | 3192 | return 1; |
241faece PS |
3193 | |
3194 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
3195 | cont: | |
2c82c7e7 FW |
3196 | prule = rule; |
3197 | cont_skip: | |
241faece PS |
3198 | (*idx)++; |
3199 | } | |
310529e6 | 3200 | return 0; |
241faece PS |
3201 | } |
3202 | ||
96518518 PM |
3203 | static int nf_tables_dump_rules(struct sk_buff *skb, |
3204 | struct netlink_callback *cb) | |
3205 | { | |
3206 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 3207 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 3208 | struct nft_table *table; |
96518518 | 3209 | const struct nft_chain *chain; |
241faece | 3210 | unsigned int idx = 0; |
99633ab2 | 3211 | struct net *net = sock_net(skb->sk); |
96518518 | 3212 | int family = nfmsg->nfgen_family; |
0854db2a | 3213 | struct nftables_pernet *nft_net; |
8daa8fde PS |
3214 | bool reset = false; |
3215 | ||
3216 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET) | |
3217 | reset = true; | |
96518518 | 3218 | |
e688a7f8 | 3219 | rcu_read_lock(); |
d59d2f82 | 3220 | nft_net = nft_pernet(net); |
34002783 | 3221 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 3222 | |
0854db2a | 3223 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 3224 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
3225 | continue; |
3226 | ||
3227 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
3228 | continue; |
3229 | ||
715849ab | 3230 | if (ctx && ctx->table && ctx->chain) { |
241faece | 3231 | struct rhlist_head *list, *tmp; |
6e1f760e | 3232 | |
241faece PS |
3233 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
3234 | nft_chain_ht_params); | |
3235 | if (!list) | |
3236 | goto done; | |
3237 | ||
3238 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
3239 | if (!nft_is_active(net, chain)) | |
3240 | continue; | |
3241 | __nf_tables_dump_rules(skb, &idx, | |
8daa8fde | 3242 | cb, table, chain, reset); |
241faece | 3243 | break; |
96518518 | 3244 | } |
241faece | 3245 | goto done; |
96518518 | 3246 | } |
241faece PS |
3247 | |
3248 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
8daa8fde PS |
3249 | if (__nf_tables_dump_rules(skb, &idx, |
3250 | cb, table, chain, reset)) | |
241faece PS |
3251 | goto done; |
3252 | } | |
3253 | ||
3254 | if (ctx && ctx->table) | |
3255 | break; | |
96518518 PM |
3256 | } |
3257 | done: | |
e688a7f8 | 3258 | rcu_read_unlock(); |
310529e6 PS |
3259 | |
3260 | cb->args[0] = idx; | |
96518518 PM |
3261 | return skb->len; |
3262 | } | |
3263 | ||
90fd131a FW |
3264 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
3265 | { | |
3266 | const struct nlattr * const *nla = cb->data; | |
3267 | struct nft_rule_dump_ctx *ctx = NULL; | |
3268 | ||
3269 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
3270 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
3271 | if (!ctx) | |
3272 | return -ENOMEM; | |
3273 | ||
3274 | if (nla[NFTA_RULE_TABLE]) { | |
3275 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
3276 | GFP_ATOMIC); | |
3277 | if (!ctx->table) { | |
3278 | kfree(ctx); | |
3279 | return -ENOMEM; | |
3280 | } | |
3281 | } | |
3282 | if (nla[NFTA_RULE_CHAIN]) { | |
3283 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
3284 | GFP_ATOMIC); | |
3285 | if (!ctx->chain) { | |
3286 | kfree(ctx->table); | |
3287 | kfree(ctx); | |
3288 | return -ENOMEM; | |
3289 | } | |
3290 | } | |
3291 | } | |
3292 | ||
3293 | cb->data = ctx; | |
3294 | return 0; | |
3295 | } | |
3296 | ||
6e1f760e PNA |
3297 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
3298 | { | |
e46abbcc PS |
3299 | struct nft_rule_dump_ctx *ctx = cb->data; |
3300 | ||
3301 | if (ctx) { | |
3302 | kfree(ctx->table); | |
b7263e07 | 3303 | kfree(ctx->chain); |
e46abbcc PS |
3304 | kfree(ctx); |
3305 | } | |
6e1f760e PNA |
3306 | return 0; |
3307 | } | |
3308 | ||
d9adf22a | 3309 | /* called with rcu_read_lock held */ |
797d4980 PNA |
3310 | static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info, |
3311 | const struct nlattr * const nla[]) | |
96518518 | 3312 | { |
797d4980 PNA |
3313 | struct netlink_ext_ack *extack = info->extack; |
3314 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 3315 | u8 family = info->nfmsg->nfgen_family; |
96518518 PM |
3316 | const struct nft_chain *chain; |
3317 | const struct nft_rule *rule; | |
797d4980 | 3318 | struct net *net = info->net; |
1b2470e5 | 3319 | struct nft_table *table; |
96518518 | 3320 | struct sk_buff *skb2; |
8daa8fde | 3321 | bool reset = false; |
96518518 PM |
3322 | int err; |
3323 | ||
797d4980 | 3324 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 | 3325 | struct netlink_dump_control c = { |
90fd131a | 3326 | .start= nf_tables_dump_rules_start, |
96518518 | 3327 | .dump = nf_tables_dump_rules, |
6e1f760e | 3328 | .done = nf_tables_dump_rules_done, |
d9adf22a | 3329 | .module = THIS_MODULE, |
90fd131a | 3330 | .data = (void *)nla, |
96518518 | 3331 | }; |
6e1f760e | 3332 | |
797d4980 | 3333 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
3334 | } |
3335 | ||
6001a930 | 3336 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0); |
36dd1bcc PNA |
3337 | if (IS_ERR(table)) { |
3338 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3339 | return PTR_ERR(table); |
36dd1bcc | 3340 | } |
96518518 | 3341 | |
f102d66b | 3342 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3343 | if (IS_ERR(chain)) { |
3344 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3345 | return PTR_ERR(chain); |
36dd1bcc | 3346 | } |
96518518 | 3347 | |
cac20fcd | 3348 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3349 | if (IS_ERR(rule)) { |
3350 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3351 | return PTR_ERR(rule); |
36dd1bcc | 3352 | } |
96518518 | 3353 | |
d9adf22a | 3354 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
3355 | if (!skb2) |
3356 | return -ENOMEM; | |
3357 | ||
8daa8fde PS |
3358 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET) |
3359 | reset = true; | |
3360 | ||
84d7fce6 | 3361 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 3362 | info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
8daa8fde | 3363 | family, table, chain, rule, 0, reset); |
96518518 | 3364 | if (err < 0) |
ee921183 | 3365 | goto err_fill_rule_info; |
96518518 | 3366 | |
ee921183 | 3367 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 3368 | |
ee921183 | 3369 | err_fill_rule_info: |
96518518 PM |
3370 | kfree_skb(skb2); |
3371 | return err; | |
3372 | } | |
3373 | ||
62472bce PM |
3374 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
3375 | struct nft_rule *rule) | |
96518518 | 3376 | { |
29e38801 | 3377 | struct nft_expr *expr, *next; |
96518518 PM |
3378 | |
3379 | /* | |
3380 | * Careful: some expressions might not be initialized in case this | |
3381 | * is called on error from nf_tables_newrule(). | |
3382 | */ | |
3383 | expr = nft_expr_first(rule); | |
31cc578a | 3384 | while (nft_expr_more(rule, expr)) { |
29e38801 | 3385 | next = nft_expr_next(expr); |
62472bce | 3386 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 3387 | expr = next; |
96518518 PM |
3388 | } |
3389 | kfree(rule); | |
3390 | } | |
3391 | ||
d0e2c7de | 3392 | void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae | 3393 | { |
f6ac8585 | 3394 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
3395 | nf_tables_rule_destroy(ctx, rule); |
3396 | } | |
3397 | ||
a654de8f PNA |
3398 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
3399 | { | |
3400 | struct nft_expr *expr, *last; | |
3401 | const struct nft_data *data; | |
3402 | struct nft_rule *rule; | |
3403 | int err; | |
3404 | ||
26b2f552 TY |
3405 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
3406 | return -EMLINK; | |
3407 | ||
a654de8f PNA |
3408 | list_for_each_entry(rule, &chain->rules, list) { |
3409 | if (!nft_is_active_next(ctx->net, rule)) | |
3410 | continue; | |
3411 | ||
3412 | nft_rule_for_each_expr(expr, last, rule) { | |
3413 | if (!expr->ops->validate) | |
3414 | continue; | |
3415 | ||
3416 | err = expr->ops->validate(ctx, expr, &data); | |
3417 | if (err < 0) | |
3418 | return err; | |
3419 | } | |
81ea0106 FW |
3420 | |
3421 | cond_resched(); | |
a654de8f PNA |
3422 | } |
3423 | ||
3424 | return 0; | |
3425 | } | |
3426 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
3427 | ||
3428 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
3429 | { | |
3430 | struct nft_chain *chain; | |
3431 | struct nft_ctx ctx = { | |
3432 | .net = net, | |
3433 | .family = table->family, | |
3434 | }; | |
3435 | int err; | |
3436 | ||
3437 | list_for_each_entry(chain, &table->chains, list) { | |
3438 | if (!nft_is_base_chain(chain)) | |
3439 | continue; | |
3440 | ||
3441 | ctx.chain = chain; | |
3442 | err = nft_chain_validate(&ctx, chain); | |
3443 | if (err < 0) | |
3444 | return err; | |
3445 | } | |
3446 | ||
3447 | return 0; | |
3448 | } | |
3449 | ||
d46fc894 PNA |
3450 | int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set, |
3451 | const struct nft_set_iter *iter, | |
3452 | struct nft_set_elem *elem) | |
3453 | { | |
3454 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
3455 | struct nft_ctx *pctx = (struct nft_ctx *)ctx; | |
3456 | const struct nft_data *data; | |
3457 | int err; | |
3458 | ||
3459 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
3460 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
3461 | return 0; | |
3462 | ||
3463 | data = nft_set_ext_data(ext); | |
3464 | switch (data->verdict.code) { | |
3465 | case NFT_JUMP: | |
3466 | case NFT_GOTO: | |
3467 | pctx->level++; | |
3468 | err = nft_chain_validate(ctx, data->verdict.chain); | |
3469 | if (err < 0) | |
3470 | return err; | |
3471 | pctx->level--; | |
3472 | break; | |
3473 | default: | |
3474 | break; | |
3475 | } | |
3476 | ||
3477 | return 0; | |
3478 | } | |
3479 | ||
3480 | struct nft_set_elem_catchall { | |
3481 | struct list_head list; | |
3482 | struct rcu_head rcu; | |
3483 | void *elem; | |
3484 | }; | |
3485 | ||
3486 | int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set) | |
3487 | { | |
3488 | u8 genmask = nft_genmask_next(ctx->net); | |
3489 | struct nft_set_elem_catchall *catchall; | |
3490 | struct nft_set_elem elem; | |
3491 | struct nft_set_ext *ext; | |
3492 | int ret = 0; | |
3493 | ||
3494 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
3495 | ext = nft_set_elem_ext(set, catchall->elem); | |
3496 | if (!nft_set_elem_active(ext, genmask)) | |
3497 | continue; | |
3498 | ||
3499 | elem.priv = catchall->elem; | |
3500 | ret = nft_setelem_validate(ctx, set, NULL, &elem); | |
3501 | if (ret < 0) | |
3502 | return ret; | |
3503 | } | |
3504 | ||
3505 | return ret; | |
3506 | } | |
3507 | ||
75dd48e2 | 3508 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3509 | const struct nft_chain *chain, |
75dd48e2 PS |
3510 | const struct nlattr *nla); |
3511 | ||
1081d11b PNA |
3512 | #define NFT_RULE_MAXEXPRS 128 |
3513 | ||
7dab8ee3 PNA |
3514 | static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, |
3515 | const struct nlattr * const nla[]) | |
96518518 | 3516 | { |
7dab8ee3 | 3517 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
3518 | struct netlink_ext_ack *extack = info->extack; |
3519 | unsigned int size, i, n, ulen = 0, usize = 0; | |
3520 | u8 genmask = nft_genmask_next(info->net); | |
3521 | struct nft_rule *rule, *old_rule = NULL; | |
3522 | struct nft_expr_info *expr_info = NULL; | |
ef4b65e5 | 3523 | u8 family = info->nfmsg->nfgen_family; |
3c5e4462 | 3524 | struct nft_flow_rule *flow = NULL; |
7dab8ee3 | 3525 | struct net *net = info->net; |
7dab8ee3 | 3526 | struct nft_userdata *udata; |
96518518 PM |
3527 | struct nft_table *table; |
3528 | struct nft_chain *chain; | |
7dab8ee3 PNA |
3529 | struct nft_trans *trans; |
3530 | u64 handle, pos_handle; | |
96518518 PM |
3531 | struct nft_expr *expr; |
3532 | struct nft_ctx ctx; | |
3533 | struct nlattr *tmp; | |
96518518 | 3534 | int err, rem; |
96518518 | 3535 | |
0854db2a | 3536 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3537 | |
6001a930 PNA |
3538 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3539 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3540 | if (IS_ERR(table)) { |
3541 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3542 | return PTR_ERR(table); |
36dd1bcc | 3543 | } |
96518518 | 3544 | |
837830a4 PNA |
3545 | if (nla[NFTA_RULE_CHAIN]) { |
3546 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], | |
3547 | genmask); | |
3548 | if (IS_ERR(chain)) { | |
3549 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
3550 | return PTR_ERR(chain); | |
3551 | } | |
d0e2c7de PNA |
3552 | if (nft_chain_is_bound(chain)) |
3553 | return -EOPNOTSUPP; | |
3554 | ||
837830a4 | 3555 | } else if (nla[NFTA_RULE_CHAIN_ID]) { |
95f466d2 | 3556 | chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID]); |
837830a4 PNA |
3557 | if (IS_ERR(chain)) { |
3558 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]); | |
3559 | return PTR_ERR(chain); | |
3560 | } | |
3561 | } else { | |
3562 | return -EINVAL; | |
36dd1bcc | 3563 | } |
96518518 PM |
3564 | |
3565 | if (nla[NFTA_RULE_HANDLE]) { | |
3566 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3567 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3568 | if (IS_ERR(rule)) { |
3569 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3570 | return PTR_ERR(rule); |
36dd1bcc | 3571 | } |
96518518 | 3572 | |
7dab8ee3 | 3573 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 3574 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
96518518 | 3575 | return -EEXIST; |
36dd1bcc | 3576 | } |
7dab8ee3 | 3577 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
3578 | old_rule = rule; |
3579 | else | |
3580 | return -EOPNOTSUPP; | |
3581 | } else { | |
7dab8ee3 PNA |
3582 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) || |
3583 | info->nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3584 | return -EINVAL; |
3585 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
3586 | |
3587 | if (chain->use == UINT_MAX) | |
3588 | return -EOVERFLOW; | |
5e948466 | 3589 | |
447750f2 FW |
3590 | if (nla[NFTA_RULE_POSITION]) { |
3591 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3592 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3593 | if (IS_ERR(old_rule)) { | |
3594 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3595 | return PTR_ERR(old_rule); | |
3596 | } | |
75dd48e2 | 3597 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
36d5b291 | 3598 | old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]); |
75dd48e2 PS |
3599 | if (IS_ERR(old_rule)) { |
3600 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3601 | return PTR_ERR(old_rule); | |
3602 | } | |
36dd1bcc | 3603 | } |
5e948466 EL |
3604 | } |
3605 | ||
7dab8ee3 | 3606 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0ca743a5 | 3607 | |
96518518 PM |
3608 | n = 0; |
3609 | size = 0; | |
3610 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
7dab8ee3 PNA |
3611 | expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3612 | sizeof(struct nft_expr_info), | |
3613 | GFP_KERNEL); | |
3614 | if (!expr_info) | |
2a43ecf9 FW |
3615 | return -ENOMEM; |
3616 | ||
96518518 PM |
3617 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3618 | err = -EINVAL; | |
3619 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3c5e4462 | 3620 | goto err_release_expr; |
96518518 | 3621 | if (n == NFT_RULE_MAXEXPRS) |
3c5e4462 | 3622 | goto err_release_expr; |
7dab8ee3 | 3623 | err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]); |
c781471d PNA |
3624 | if (err < 0) { |
3625 | NL_SET_BAD_ATTR(extack, tmp); | |
3c5e4462 | 3626 | goto err_release_expr; |
c781471d | 3627 | } |
7dab8ee3 | 3628 | size += expr_info[n].ops->size; |
96518518 PM |
3629 | n++; |
3630 | } | |
3631 | } | |
9889840f PM |
3632 | /* Check for overflow of dlen field */ |
3633 | err = -EFBIG; | |
3634 | if (size >= 1 << 12) | |
3c5e4462 | 3635 | goto err_release_expr; |
96518518 | 3636 | |
86f1ec32 | 3637 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3638 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3639 | if (ulen > 0) |
3640 | usize = sizeof(struct nft_userdata) + ulen; | |
3641 | } | |
0768b3b3 | 3642 | |
96518518 | 3643 | err = -ENOMEM; |
33758c89 | 3644 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT); |
96518518 | 3645 | if (rule == NULL) |
3c5e4462 | 3646 | goto err_release_expr; |
96518518 | 3647 | |
889f7ee7 | 3648 | nft_activate_next(net, rule); |
0628b123 | 3649 | |
96518518 PM |
3650 | rule->handle = handle; |
3651 | rule->dlen = size; | |
86f1ec32 | 3652 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3653 | |
86f1ec32 PM |
3654 | if (ulen) { |
3655 | udata = nft_userdata(rule); | |
3656 | udata->len = ulen - 1; | |
3657 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3658 | } | |
96518518 | 3659 | |
96518518 PM |
3660 | expr = nft_expr_first(rule); |
3661 | for (i = 0; i < n; i++) { | |
7dab8ee3 | 3662 | err = nf_tables_newexpr(&ctx, &expr_info[i], expr); |
83d9dcba | 3663 | if (err < 0) { |
7dab8ee3 | 3664 | NL_SET_BAD_ATTR(extack, expr_info[i].attr); |
3c5e4462 | 3665 | goto err_release_rule; |
83d9dcba | 3666 | } |
a654de8f | 3667 | |
7dab8ee3 | 3668 | if (expr_info[i].ops->validate) |
a654de8f PNA |
3669 | nft_validate_state_update(net, NFT_VALIDATE_NEED); |
3670 | ||
7dab8ee3 | 3671 | expr_info[i].ops = NULL; |
96518518 PM |
3672 | expr = nft_expr_next(expr); |
3673 | } | |
3674 | ||
3c5e4462 PNA |
3675 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
3676 | flow = nft_flow_rule_create(net, rule); | |
3677 | if (IS_ERR(flow)) { | |
3678 | err = PTR_ERR(flow); | |
3679 | goto err_release_rule; | |
3680 | } | |
3681 | } | |
3682 | ||
7dab8ee3 | 3683 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) { |
2c964c55 PNA |
3684 | err = nft_delrule(&ctx, old_rule); |
3685 | if (err < 0) | |
3686 | goto err_destroy_flow_rule; | |
3687 | ||
ca089878 | 3688 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3689 | if (trans == NULL) { |
3690 | err = -ENOMEM; | |
3c5e4462 | 3691 | goto err_destroy_flow_rule; |
569ccae6 | 3692 | } |
569ccae6 FW |
3693 | list_add_tail_rcu(&rule->list, &old_rule->list); |
3694 | } else { | |
c9626a2c PNA |
3695 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3696 | if (!trans) { | |
569ccae6 | 3697 | err = -ENOMEM; |
3c5e4462 | 3698 | goto err_destroy_flow_rule; |
569ccae6 FW |
3699 | } |
3700 | ||
7dab8ee3 | 3701 | if (info->nlh->nlmsg_flags & NLM_F_APPEND) { |
569ccae6 FW |
3702 | if (old_rule) |
3703 | list_add_rcu(&rule->list, &old_rule->list); | |
3704 | else | |
3705 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3706 | } else { | |
3707 | if (old_rule) | |
3708 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3709 | else | |
3710 | list_add_rcu(&rule->list, &chain->rules); | |
3711 | } | |
0628b123 | 3712 | } |
7dab8ee3 | 3713 | kvfree(expr_info); |
4fefee57 | 3714 | chain->use++; |
96518518 | 3715 | |
3c5e4462 PNA |
3716 | if (flow) |
3717 | nft_trans_flow_rule(trans) = flow; | |
3718 | ||
0854db2a | 3719 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
a654de8f PNA |
3720 | return nft_table_validate(net, table); |
3721 | ||
3722 | return 0; | |
3c5e4462 PNA |
3723 | |
3724 | err_destroy_flow_rule: | |
4ca041f9 CIK |
3725 | if (flow) |
3726 | nft_flow_rule_destroy(flow); | |
3c5e4462 | 3727 | err_release_rule: |
bb7b40ae | 3728 | nf_tables_rule_release(&ctx, rule); |
3c5e4462 | 3729 | err_release_expr: |
96518518 | 3730 | for (i = 0; i < n; i++) { |
7dab8ee3 PNA |
3731 | if (expr_info[i].ops) { |
3732 | module_put(expr_info[i].ops->type->owner); | |
3733 | if (expr_info[i].ops->type->release_ops) | |
3734 | expr_info[i].ops->type->release_ops(expr_info[i].ops); | |
b25a31bf | 3735 | } |
96518518 | 3736 | } |
7dab8ee3 PNA |
3737 | kvfree(expr_info); |
3738 | ||
96518518 PM |
3739 | return err; |
3740 | } | |
3741 | ||
1a94e38d | 3742 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3743 | const struct nft_chain *chain, |
1a94e38d PNA |
3744 | const struct nlattr *nla) |
3745 | { | |
d59d2f82 | 3746 | struct nftables_pernet *nft_net = nft_pernet(net); |
1a94e38d PNA |
3747 | u32 id = ntohl(nla_get_be32(nla)); |
3748 | struct nft_trans *trans; | |
3749 | ||
0854db2a | 3750 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
1a94e38d PNA |
3751 | struct nft_rule *rule = nft_trans_rule(trans); |
3752 | ||
3753 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
36d5b291 | 3754 | trans->ctx.chain == chain && |
1a94e38d PNA |
3755 | id == nft_trans_rule_id(trans)) |
3756 | return rule; | |
3757 | } | |
3758 | return ERR_PTR(-ENOENT); | |
3759 | } | |
3760 | ||
7dab8ee3 PNA |
3761 | static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info, |
3762 | const struct nlattr * const nla[]) | |
96518518 | 3763 | { |
7dab8ee3 | 3764 | struct netlink_ext_ack *extack = info->extack; |
7dab8ee3 | 3765 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 3766 | u8 family = info->nfmsg->nfgen_family; |
cf9dc09d | 3767 | struct nft_chain *chain = NULL; |
7dab8ee3 PNA |
3768 | struct net *net = info->net; |
3769 | struct nft_table *table; | |
cf9dc09d | 3770 | struct nft_rule *rule; |
0628b123 | 3771 | struct nft_ctx ctx; |
ef4b65e5 | 3772 | int err = 0; |
96518518 | 3773 | |
6001a930 PNA |
3774 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3775 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3776 | if (IS_ERR(table)) { |
3777 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3778 | return PTR_ERR(table); |
36dd1bcc | 3779 | } |
96518518 | 3780 | |
cf9dc09d | 3781 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
3782 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
3783 | genmask); | |
36dd1bcc | 3784 | if (IS_ERR(chain)) { |
1fb7696a | 3785 | if (PTR_ERR(chain) == -ENOENT && |
f80a612d FFM |
3786 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE) |
3787 | return 0; | |
3788 | ||
36dd1bcc | 3789 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); |
cf9dc09d | 3790 | return PTR_ERR(chain); |
36dd1bcc | 3791 | } |
d0e2c7de PNA |
3792 | if (nft_chain_is_bound(chain)) |
3793 | return -EOPNOTSUPP; | |
cf9dc09d | 3794 | } |
96518518 | 3795 | |
7dab8ee3 | 3796 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0628b123 | 3797 | |
cf9dc09d PNA |
3798 | if (chain) { |
3799 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 3800 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc | 3801 | if (IS_ERR(rule)) { |
f80a612d FFM |
3802 | if (PTR_ERR(rule) == -ENOENT && |
3803 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE) | |
3804 | return 0; | |
3805 | ||
36dd1bcc | 3806 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
cf9dc09d | 3807 | return PTR_ERR(rule); |
36dd1bcc | 3808 | } |
96518518 | 3809 | |
1a94e38d PNA |
3810 | err = nft_delrule(&ctx, rule); |
3811 | } else if (nla[NFTA_RULE_ID]) { | |
36d5b291 | 3812 | rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]); |
36dd1bcc PNA |
3813 | if (IS_ERR(rule)) { |
3814 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 3815 | return PTR_ERR(rule); |
36dd1bcc | 3816 | } |
1a94e38d | 3817 | |
5e266fe7 | 3818 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 3819 | } else { |
ce24b721 | 3820 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
3821 | } |
3822 | } else { | |
3823 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
3824 | if (!nft_is_active_next(net, chain)) |
3825 | continue; | |
3826 | ||
cf9dc09d | 3827 | ctx.chain = chain; |
ce24b721 | 3828 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
3829 | if (err < 0) |
3830 | break; | |
3831 | } | |
3832 | } | |
3833 | ||
3834 | return err; | |
3835 | } | |
3836 | ||
20a69341 PM |
3837 | /* |
3838 | * Sets | |
3839 | */ | |
e32a4dc6 FW |
3840 | static const struct nft_set_type *nft_set_types[] = { |
3841 | &nft_set_hash_fast_type, | |
3842 | &nft_set_hash_type, | |
3843 | &nft_set_rhash_type, | |
3844 | &nft_set_bitmap_type, | |
3845 | &nft_set_rbtree_type, | |
e6abef61 | 3846 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
7400b063 SB |
3847 | &nft_set_pipapo_avx2_type, |
3848 | #endif | |
e32a4dc6 FW |
3849 | &nft_set_pipapo_type, |
3850 | }; | |
20a69341 | 3851 | |
2b664957 | 3852 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
3853 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
3854 | NFT_SET_EVAL) | |
2b664957 | 3855 | |
71cc0873 | 3856 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 3857 | { |
71cc0873 | 3858 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
3859 | } |
3860 | ||
c50b960c PM |
3861 | /* |
3862 | * Select a set implementation based on the data characteristics and the | |
3863 | * given policy. The total memory use might not be known if no size is | |
3864 | * given, in that case the amount of memory per element is used. | |
3865 | */ | |
3866 | static const struct nft_set_ops * | |
2b664957 PNA |
3867 | nft_select_set_ops(const struct nft_ctx *ctx, |
3868 | const struct nlattr * const nla[], | |
bed4a63e | 3869 | const struct nft_set_desc *desc) |
20a69341 | 3870 | { |
d59d2f82 | 3871 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c50b960c PM |
3872 | const struct nft_set_ops *ops, *bops; |
3873 | struct nft_set_estimate est, best; | |
2b664957 PNA |
3874 | const struct nft_set_type *type; |
3875 | u32 flags = 0; | |
e32a4dc6 | 3876 | int i; |
20a69341 | 3877 | |
0854db2a | 3878 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3879 | lockdep_nfnl_nft_mutex_not_held(); |
e32a4dc6 | 3880 | |
2b664957 PNA |
3881 | if (nla[NFTA_SET_FLAGS] != NULL) |
3882 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 3883 | |
55af753c PNA |
3884 | bops = NULL; |
3885 | best.size = ~0; | |
3886 | best.lookup = ~0; | |
0b5a7874 | 3887 | best.space = ~0; |
c50b960c | 3888 | |
e32a4dc6 FW |
3889 | for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) { |
3890 | type = nft_set_types[i]; | |
71cc0873 | 3891 | ops = &type->ops; |
2b664957 | 3892 | |
71cc0873 | 3893 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 3894 | continue; |
2b664957 | 3895 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
3896 | continue; |
3897 | ||
bed4a63e | 3898 | switch (desc->policy) { |
c50b960c | 3899 | case NFT_SET_POL_PERFORMANCE: |
55af753c | 3900 | if (est.lookup < best.lookup) |
c50b960c | 3901 | break; |
644e334e PNA |
3902 | if (est.lookup == best.lookup && |
3903 | est.space < best.space) | |
3904 | break; | |
c50b960c PM |
3905 | continue; |
3906 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
3907 | if (!desc->size) { |
3908 | if (est.space < best.space) | |
3909 | break; | |
3910 | if (est.space == best.space && | |
3911 | est.lookup < best.lookup) | |
3912 | break; | |
4f2921ca | 3913 | } else if (est.size < best.size || !bops) { |
c50b960c | 3914 | break; |
0b5a7874 | 3915 | } |
c50b960c PM |
3916 | continue; |
3917 | default: | |
3918 | break; | |
3919 | } | |
3920 | ||
c50b960c PM |
3921 | bops = ops; |
3922 | best = est; | |
20a69341 PM |
3923 | } |
3924 | ||
c50b960c PM |
3925 | if (bops != NULL) |
3926 | return bops; | |
3927 | ||
20a69341 PM |
3928 | return ERR_PTR(-EOPNOTSUPP); |
3929 | } | |
3930 | ||
3931 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
3932 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
3933 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 3934 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 3935 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
3936 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
3937 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
3938 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
3939 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
3940 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
3941 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
3942 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 3943 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
3944 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
3945 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
3946 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
3947 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 3948 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 3949 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
65038428 | 3950 | [NFTA_SET_EXPR] = { .type = NLA_NESTED }, |
48b0ae04 | 3951 | [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED }, |
c50b960c PM |
3952 | }; |
3953 | ||
3954 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
3955 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 3956 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
3957 | }; |
3958 | ||
cac20fcd PNA |
3959 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
3960 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
3961 | { |
3962 | struct nft_set *set; | |
3963 | ||
3964 | if (nla == NULL) | |
3965 | return ERR_PTR(-EINVAL); | |
3966 | ||
d9adf22a | 3967 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
3968 | if (!nla_strcmp(nla, set->name) && |
3969 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
3970 | return set; |
3971 | } | |
3972 | return ERR_PTR(-ENOENT); | |
3973 | } | |
3974 | ||
cac20fcd PNA |
3975 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
3976 | const struct nlattr *nla, | |
3977 | u8 genmask) | |
3ecbfd65 HS |
3978 | { |
3979 | struct nft_set *set; | |
3980 | ||
3ecbfd65 HS |
3981 | list_for_each_entry(set, &table->sets, list) { |
3982 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
3983 | nft_active_genmask(set, genmask)) | |
3984 | return set; | |
3985 | } | |
3986 | return ERR_PTR(-ENOENT); | |
3987 | } | |
3988 | ||
cac20fcd | 3989 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
470ee20e | 3990 | const struct nft_table *table, |
cac20fcd | 3991 | const struct nlattr *nla, u8 genmask) |
958bee14 | 3992 | { |
d59d2f82 | 3993 | struct nftables_pernet *nft_net = nft_pernet(net); |
958bee14 | 3994 | u32 id = ntohl(nla_get_be32(nla)); |
d59d2f82 | 3995 | struct nft_trans *trans; |
958bee14 | 3996 | |
0854db2a | 3997 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
9c7f96fd AK |
3998 | if (trans->msg_type == NFT_MSG_NEWSET) { |
3999 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 4000 | |
9c7f96fd | 4001 | if (id == nft_trans_set_id(trans) && |
470ee20e | 4002 | set->table == table && |
9c7f96fd AK |
4003 | nft_active_genmask(set, genmask)) |
4004 | return set; | |
4005 | } | |
958bee14 PNA |
4006 | } |
4007 | return ERR_PTR(-ENOENT); | |
4008 | } | |
c7a72e3f | 4009 | |
10659cba PNA |
4010 | struct nft_set *nft_set_lookup_global(const struct net *net, |
4011 | const struct nft_table *table, | |
4012 | const struct nlattr *nla_set_name, | |
4013 | const struct nlattr *nla_set_id, | |
4014 | u8 genmask) | |
c7a72e3f PNA |
4015 | { |
4016 | struct nft_set *set; | |
4017 | ||
cac20fcd | 4018 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
4019 | if (IS_ERR(set)) { |
4020 | if (!nla_set_id) | |
4021 | return set; | |
4022 | ||
470ee20e | 4023 | set = nft_set_lookup_byid(net, table, nla_set_id, genmask); |
c7a72e3f PNA |
4024 | } |
4025 | return set; | |
4026 | } | |
10659cba | 4027 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 4028 | |
20a69341 PM |
4029 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
4030 | const char *name) | |
4031 | { | |
4032 | const struct nft_set *i; | |
4033 | const char *p; | |
4034 | unsigned long *inuse; | |
60eb1894 | 4035 | unsigned int n = 0, min = 0; |
20a69341 | 4036 | |
38745490 | 4037 | p = strchr(name, '%'); |
20a69341 PM |
4038 | if (p != NULL) { |
4039 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
4040 | return -EINVAL; | |
4041 | ||
4042 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
4043 | if (inuse == NULL) | |
4044 | return -ENOMEM; | |
60eb1894 | 4045 | cont: |
20a69341 | 4046 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
4047 | int tmp; |
4048 | ||
271c5ca8 | 4049 | if (!nft_is_active_next(ctx->net, i)) |
37a9cc52 | 4050 | continue; |
14662917 | 4051 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 4052 | continue; |
60eb1894 | 4053 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 4054 | continue; |
14662917 | 4055 | |
60eb1894 | 4056 | set_bit(tmp - min, inuse); |
20a69341 PM |
4057 | } |
4058 | ||
53b70287 | 4059 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
4060 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
4061 | min += BITS_PER_BYTE * PAGE_SIZE; | |
4062 | memset(inuse, 0, PAGE_SIZE); | |
4063 | goto cont; | |
4064 | } | |
20a69341 PM |
4065 | free_page((unsigned long)inuse); |
4066 | } | |
4067 | ||
33758c89 | 4068 | set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n); |
38745490 PS |
4069 | if (!set->name) |
4070 | return -ENOMEM; | |
4071 | ||
20a69341 | 4072 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
4073 | if (!nft_is_active_next(ctx->net, i)) |
4074 | continue; | |
e63aaaa6 AY |
4075 | if (!strcmp(set->name, i->name)) { |
4076 | kfree(set->name); | |
7fb6f78d | 4077 | set->name = NULL; |
20a69341 | 4078 | return -ENFILE; |
e63aaaa6 | 4079 | } |
20a69341 PM |
4080 | } |
4081 | return 0; | |
4082 | } | |
4083 | ||
917d80d3 | 4084 | int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
8e1102d5 FW |
4085 | { |
4086 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
4087 | u64 max = (u64)(~((u64)0)); | |
4088 | ||
4089 | max = div_u64(max, NSEC_PER_MSEC); | |
4090 | if (ms >= max) | |
4091 | return -ERANGE; | |
4092 | ||
4093 | ms *= NSEC_PER_MSEC; | |
4094 | *result = nsecs_to_jiffies64(ms); | |
4095 | return 0; | |
4096 | } | |
4097 | ||
917d80d3 | 4098 | __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 4099 | { |
3b15d09f | 4100 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
4101 | } |
4102 | ||
f3a2181e SB |
4103 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
4104 | const struct nft_set *set) | |
4105 | { | |
4106 | struct nlattr *concat, *field; | |
4107 | int i; | |
4108 | ||
4109 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
4110 | if (!concat) | |
4111 | return -ENOMEM; | |
4112 | ||
4113 | for (i = 0; i < set->field_count; i++) { | |
4114 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
4115 | if (!field) | |
4116 | return -ENOMEM; | |
4117 | ||
4118 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
4119 | htonl(set->field_len[i]))) | |
4120 | return -ENOMEM; | |
4121 | ||
4122 | nla_nest_end(skb, field); | |
4123 | } | |
4124 | ||
4125 | nla_nest_end(skb, concat); | |
4126 | ||
4127 | return 0; | |
4128 | } | |
4129 | ||
20a69341 PM |
4130 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
4131 | const struct nft_set *set, u16 event, u16 flags) | |
4132 | { | |
123b9961 PNA |
4133 | u64 timeout = READ_ONCE(set->timeout); |
4134 | u32 gc_int = READ_ONCE(set->gc_int); | |
128ad332 | 4135 | u32 portid = ctx->portid; |
123b9961 | 4136 | struct nlmsghdr *nlh; |
65038428 | 4137 | struct nlattr *nest; |
128ad332 | 4138 | u32 seq = ctx->seq; |
48b0ae04 | 4139 | int i; |
20a69341 | 4140 | |
dedb67c4 | 4141 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
4142 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
4143 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
4144 | if (!nlh) | |
20a69341 PM |
4145 | goto nla_put_failure; |
4146 | ||
20a69341 PM |
4147 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
4148 | goto nla_put_failure; | |
4149 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
4150 | goto nla_put_failure; | |
3ecbfd65 HS |
4151 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
4152 | NFTA_SET_PAD)) | |
4153 | goto nla_put_failure; | |
20a69341 PM |
4154 | if (set->flags != 0) |
4155 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
4156 | goto nla_put_failure; | |
4157 | ||
4158 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
4159 | goto nla_put_failure; | |
4160 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
4161 | goto nla_put_failure; | |
4162 | if (set->flags & NFT_SET_MAP) { | |
4163 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
4164 | goto nla_put_failure; | |
4165 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
4166 | goto nla_put_failure; | |
4167 | } | |
8aeff920 PNA |
4168 | if (set->flags & NFT_SET_OBJECT && |
4169 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
4170 | goto nla_put_failure; | |
20a69341 | 4171 | |
123b9961 | 4172 | if (timeout && |
d3e2a111 | 4173 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
123b9961 | 4174 | nf_jiffies64_to_msecs(timeout), |
b46f6ded | 4175 | NFTA_SET_PAD)) |
761da293 | 4176 | goto nla_put_failure; |
123b9961 PNA |
4177 | if (gc_int && |
4178 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(gc_int))) | |
761da293 PM |
4179 | goto nla_put_failure; |
4180 | ||
9363dc4b AB |
4181 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
4182 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
4183 | goto nla_put_failure; | |
4184 | } | |
4185 | ||
6f03bf43 PNA |
4186 | if (set->udata && |
4187 | nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) | |
e6d8ecac CFG |
4188 | goto nla_put_failure; |
4189 | ||
65038428 PNA |
4190 | nest = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
4191 | if (!nest) | |
c50b960c PM |
4192 | goto nla_put_failure; |
4193 | if (set->size && | |
4194 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
4195 | goto nla_put_failure; | |
f3a2181e SB |
4196 | |
4197 | if (set->field_count > 1 && | |
4198 | nf_tables_fill_set_concat(skb, set)) | |
4199 | goto nla_put_failure; | |
4200 | ||
65038428 PNA |
4201 | nla_nest_end(skb, nest); |
4202 | ||
8cfd9b0f | 4203 | if (set->num_exprs == 1) { |
65038428 | 4204 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR); |
8daa8fde | 4205 | if (nf_tables_fill_expr_info(skb, set->exprs[0], false) < 0) |
65038428 PNA |
4206 | goto nla_put_failure; |
4207 | ||
4208 | nla_nest_end(skb, nest); | |
48b0ae04 PNA |
4209 | } else if (set->num_exprs > 1) { |
4210 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS); | |
4211 | if (nest == NULL) | |
65038428 PNA |
4212 | goto nla_put_failure; |
4213 | ||
48b0ae04 PNA |
4214 | for (i = 0; i < set->num_exprs; i++) { |
4215 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, | |
8daa8fde | 4216 | set->exprs[i], false) < 0) |
48b0ae04 PNA |
4217 | goto nla_put_failure; |
4218 | } | |
65038428 PNA |
4219 | nla_nest_end(skb, nest); |
4220 | } | |
c50b960c | 4221 | |
053c095a JB |
4222 | nlmsg_end(skb, nlh); |
4223 | return 0; | |
20a69341 PM |
4224 | |
4225 | nla_put_failure: | |
4226 | nlmsg_trim(skb, nlh); | |
4227 | return -1; | |
4228 | } | |
4229 | ||
25e94a99 PNA |
4230 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
4231 | const struct nft_set *set, int event, | |
4232 | gfp_t gfp_flags) | |
20a69341 | 4233 | { |
d59d2f82 | 4234 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
128ad332 | 4235 | u32 portid = ctx->portid; |
6fb721cf PNA |
4236 | struct sk_buff *skb; |
4237 | u16 flags = 0; | |
20a69341 PM |
4238 | int err; |
4239 | ||
128ad332 PNA |
4240 | if (!ctx->report && |
4241 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 4242 | return; |
20a69341 | 4243 | |
31f8441c | 4244 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
4245 | if (skb == NULL) |
4246 | goto err; | |
4247 | ||
6fb721cf PNA |
4248 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
4249 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
4250 | ||
4251 | err = nf_tables_fill_set(skb, ctx, set, event, flags); | |
20a69341 PM |
4252 | if (err < 0) { |
4253 | kfree_skb(skb); | |
4254 | goto err; | |
4255 | } | |
4256 | ||
0854db2a | 4257 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 4258 | return; |
20a69341 | 4259 | err: |
25e94a99 | 4260 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
4261 | } |
4262 | ||
5b96af77 | 4263 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
4264 | { |
4265 | const struct nft_set *set; | |
4266 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
4267 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
4268 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 4269 | struct nft_ctx *ctx = cb->data, ctx_set; |
0854db2a | 4270 | struct nftables_pernet *nft_net; |
c9c8e485 PNA |
4271 | |
4272 | if (cb->args[1]) | |
4273 | return skb->len; | |
4274 | ||
e688a7f8 | 4275 | rcu_read_lock(); |
d59d2f82 | 4276 | nft_net = nft_pernet(net); |
34002783 | 4277 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 4278 | |
0854db2a | 4279 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 4280 | if (ctx->family != NFPROTO_UNSPEC && |
98319cb9 | 4281 | ctx->family != table->family) |
36596dad PNA |
4282 | continue; |
4283 | ||
4284 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
4285 | continue; |
4286 | ||
36596dad PNA |
4287 | if (cur_table) { |
4288 | if (cur_table != table) | |
c9c8e485 PNA |
4289 | continue; |
4290 | ||
36596dad | 4291 | cur_table = NULL; |
c9c8e485 | 4292 | } |
36596dad PNA |
4293 | idx = 0; |
4294 | list_for_each_entry_rcu(set, &table->sets, list) { | |
4295 | if (idx < s_idx) | |
4296 | goto cont; | |
4297 | if (!nft_is_active(net, set)) | |
4298 | goto cont; | |
5b96af77 | 4299 | |
36596dad PNA |
4300 | ctx_set = *ctx; |
4301 | ctx_set.table = table; | |
98319cb9 | 4302 | ctx_set.family = table->family; |
c9c8e485 | 4303 | |
36596dad PNA |
4304 | if (nf_tables_fill_set(skb, &ctx_set, set, |
4305 | NFT_MSG_NEWSET, | |
4306 | NLM_F_MULTI) < 0) { | |
4307 | cb->args[0] = idx; | |
4308 | cb->args[2] = (unsigned long) table; | |
4309 | goto done; | |
c9c8e485 | 4310 | } |
36596dad | 4311 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 4312 | cont: |
36596dad | 4313 | idx++; |
c9c8e485 | 4314 | } |
36596dad PNA |
4315 | if (s_idx) |
4316 | s_idx = 0; | |
c9c8e485 PNA |
4317 | } |
4318 | cb->args[1] = 1; | |
4319 | done: | |
e688a7f8 | 4320 | rcu_read_unlock(); |
c9c8e485 PNA |
4321 | return skb->len; |
4322 | } | |
4323 | ||
90fd131a FW |
4324 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
4325 | { | |
4326 | struct nft_ctx *ctx_dump = NULL; | |
4327 | ||
4328 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
4329 | if (ctx_dump == NULL) | |
4330 | return -ENOMEM; | |
4331 | ||
4332 | cb->data = ctx_dump; | |
4333 | return 0; | |
4334 | } | |
4335 | ||
5b96af77 | 4336 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 4337 | { |
5b96af77 PNA |
4338 | kfree(cb->data); |
4339 | return 0; | |
20a69341 PM |
4340 | } |
4341 | ||
d9adf22a | 4342 | /* called with rcu_read_lock held */ |
797d4980 PNA |
4343 | static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info, |
4344 | const struct nlattr * const nla[]) | |
20a69341 | 4345 | { |
797d4980 PNA |
4346 | struct netlink_ext_ack *extack = info->extack; |
4347 | u8 genmask = nft_genmask_cur(info->net); | |
67086651 PNA |
4348 | u8 family = info->nfmsg->nfgen_family; |
4349 | struct nft_table *table = NULL; | |
797d4980 | 4350 | struct net *net = info->net; |
20a69341 | 4351 | const struct nft_set *set; |
20a69341 | 4352 | struct sk_buff *skb2; |
797d4980 | 4353 | struct nft_ctx ctx; |
20a69341 PM |
4354 | int err; |
4355 | ||
67086651 PNA |
4356 | if (nla[NFTA_SET_TABLE]) { |
4357 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, | |
4358 | genmask, 0); | |
4359 | if (IS_ERR(table)) { | |
4360 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4361 | return PTR_ERR(table); | |
4362 | } | |
4363 | } | |
4364 | ||
4365 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); | |
20a69341 | 4366 | |
797d4980 | 4367 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
20a69341 | 4368 | struct netlink_dump_control c = { |
90fd131a | 4369 | .start = nf_tables_dump_sets_start, |
20a69341 | 4370 | .dump = nf_tables_dump_sets, |
5b96af77 | 4371 | .done = nf_tables_dump_sets_done, |
90fd131a | 4372 | .data = &ctx, |
d9adf22a | 4373 | .module = THIS_MODULE, |
20a69341 | 4374 | }; |
5b96af77 | 4375 | |
797d4980 | 4376 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
20a69341 PM |
4377 | } |
4378 | ||
c9c8e485 | 4379 | /* Only accept unspec with dump */ |
ef4b65e5 | 4380 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
c9c8e485 | 4381 | return -EAFNOSUPPORT; |
eaa2bcd6 PT |
4382 | if (!nla[NFTA_SET_TABLE]) |
4383 | return -EINVAL; | |
c9c8e485 | 4384 | |
67086651 | 4385 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
4386 | if (IS_ERR(set)) |
4387 | return PTR_ERR(set); | |
4388 | ||
d9adf22a | 4389 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
4390 | if (skb2 == NULL) |
4391 | return -ENOMEM; | |
4392 | ||
4393 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
4394 | if (err < 0) | |
ee921183 | 4395 | goto err_fill_set_info; |
20a69341 | 4396 | |
ee921183 | 4397 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
20a69341 | 4398 | |
ee921183 | 4399 | err_fill_set_info: |
20a69341 PM |
4400 | kfree_skb(skb2); |
4401 | return err; | |
4402 | } | |
4403 | ||
f3a2181e SB |
4404 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
4405 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
4406 | }; | |
4407 | ||
4408 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
4409 | struct nft_set_desc *desc) | |
4410 | { | |
4411 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
4412 | u32 len; | |
4413 | int err; | |
4414 | ||
fecf31ee PNA |
4415 | if (desc->field_count >= ARRAY_SIZE(desc->field_len)) |
4416 | return -E2BIG; | |
4417 | ||
f3a2181e SB |
4418 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, |
4419 | nft_concat_policy, NULL); | |
4420 | if (err < 0) | |
4421 | return err; | |
4422 | ||
4423 | if (!tb[NFTA_SET_FIELD_LEN]) | |
4424 | return -EINVAL; | |
4425 | ||
4426 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
fecf31ee PNA |
4427 | if (!len || len > U8_MAX) |
4428 | return -EINVAL; | |
f3a2181e SB |
4429 | |
4430 | desc->field_len[desc->field_count++] = len; | |
4431 | ||
4432 | return 0; | |
4433 | } | |
4434 | ||
4435 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
4436 | const struct nlattr *nla) | |
4437 | { | |
4438 | struct nlattr *attr; | |
fecf31ee PNA |
4439 | u32 num_regs = 0; |
4440 | int rem, err, i; | |
f3a2181e SB |
4441 | |
4442 | nla_for_each_nested(attr, nla, rem) { | |
4443 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
4444 | return -EINVAL; | |
4445 | ||
4446 | err = nft_set_desc_concat_parse(attr, desc); | |
4447 | if (err < 0) | |
4448 | return err; | |
4449 | } | |
4450 | ||
fecf31ee PNA |
4451 | for (i = 0; i < desc->field_count; i++) |
4452 | num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32)); | |
4453 | ||
4454 | if (num_regs > NFT_REG32_COUNT) | |
4455 | return -E2BIG; | |
4456 | ||
f3a2181e SB |
4457 | return 0; |
4458 | } | |
4459 | ||
f7e840ee | 4460 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
4461 | const struct nlattr *nla) |
4462 | { | |
4463 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
4464 | int err; | |
4465 | ||
8cb08174 JB |
4466 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
4467 | nft_set_desc_policy, NULL); | |
c50b960c PM |
4468 | if (err < 0) |
4469 | return err; | |
4470 | ||
4471 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
4472 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
4473 | if (da[NFTA_SET_DESC_CONCAT]) |
4474 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 4475 | |
f3a2181e | 4476 | return err; |
c50b960c PM |
4477 | } |
4478 | ||
a8fe4154 PNA |
4479 | static int nft_set_expr_alloc(struct nft_ctx *ctx, struct nft_set *set, |
4480 | const struct nlattr * const *nla, | |
4481 | struct nft_expr **exprs, int *num_exprs, | |
4482 | u32 flags) | |
4483 | { | |
4484 | struct nft_expr *expr; | |
4485 | int err, i; | |
4486 | ||
4487 | if (nla[NFTA_SET_EXPR]) { | |
4488 | expr = nft_set_elem_expr_alloc(ctx, set, nla[NFTA_SET_EXPR]); | |
4489 | if (IS_ERR(expr)) { | |
4490 | err = PTR_ERR(expr); | |
4491 | goto err_set_expr_alloc; | |
4492 | } | |
4493 | exprs[0] = expr; | |
4494 | (*num_exprs)++; | |
4495 | } else if (nla[NFTA_SET_EXPRESSIONS]) { | |
4496 | struct nlattr *tmp; | |
4497 | int left; | |
4498 | ||
4499 | if (!(flags & NFT_SET_EXPR)) { | |
4500 | err = -EINVAL; | |
4501 | goto err_set_expr_alloc; | |
4502 | } | |
4503 | i = 0; | |
4504 | nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) { | |
4505 | if (i == NFT_SET_EXPR_MAX) { | |
4506 | err = -E2BIG; | |
4507 | goto err_set_expr_alloc; | |
4508 | } | |
4509 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
4510 | err = -EINVAL; | |
4511 | goto err_set_expr_alloc; | |
4512 | } | |
4513 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
4514 | if (IS_ERR(expr)) { | |
4515 | err = PTR_ERR(expr); | |
4516 | goto err_set_expr_alloc; | |
4517 | } | |
4518 | exprs[i++] = expr; | |
4519 | (*num_exprs)++; | |
4520 | } | |
4521 | } | |
4522 | ||
4523 | return 0; | |
4524 | ||
4525 | err_set_expr_alloc: | |
4526 | for (i = 0; i < *num_exprs; i++) | |
4527 | nft_expr_destroy(ctx, exprs[i]); | |
4528 | ||
4529 | return err; | |
4530 | } | |
4531 | ||
f6594c37 PNA |
4532 | static bool nft_set_is_same(const struct nft_set *set, |
4533 | const struct nft_set_desc *desc, | |
4534 | struct nft_expr *exprs[], u32 num_exprs, u32 flags) | |
4535 | { | |
4536 | int i; | |
4537 | ||
4538 | if (set->ktype != desc->ktype || | |
4539 | set->dtype != desc->dtype || | |
4540 | set->flags != flags || | |
4541 | set->klen != desc->klen || | |
4542 | set->dlen != desc->dlen || | |
4543 | set->field_count != desc->field_count || | |
4544 | set->num_exprs != num_exprs) | |
4545 | return false; | |
4546 | ||
4547 | for (i = 0; i < desc->field_count; i++) { | |
4548 | if (set->field_len[i] != desc->field_len[i]) | |
4549 | return false; | |
4550 | } | |
4551 | ||
4552 | for (i = 0; i < num_exprs; i++) { | |
4553 | if (set->exprs[i]->ops != exprs[i]->ops) | |
4554 | return false; | |
4555 | } | |
4556 | ||
4557 | return true; | |
4558 | } | |
4559 | ||
7dab8ee3 PNA |
4560 | static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, |
4561 | const struct nlattr * const nla[]) | |
20a69341 | 4562 | { |
7dab8ee3 PNA |
4563 | struct netlink_ext_ack *extack = info->extack; |
4564 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 4565 | u8 family = info->nfmsg->nfgen_family; |
20a69341 | 4566 | const struct nft_set_ops *ops; |
7dab8ee3 PNA |
4567 | struct net *net = info->net; |
4568 | struct nft_set_desc desc; | |
20a69341 | 4569 | struct nft_table *table; |
7dab8ee3 | 4570 | unsigned char *udata; |
20a69341 PM |
4571 | struct nft_set *set; |
4572 | struct nft_ctx ctx; | |
6c8774a9 | 4573 | size_t alloc_size; |
a8fe4154 | 4574 | int num_exprs = 0; |
7dab8ee3 PNA |
4575 | char *name; |
4576 | int err, i; | |
e6d8ecac | 4577 | u16 udlen; |
bed4a63e | 4578 | u32 flags; |
7dab8ee3 | 4579 | u64 size; |
20a69341 PM |
4580 | |
4581 | if (nla[NFTA_SET_TABLE] == NULL || | |
4582 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
4583 | nla[NFTA_SET_KEY_LEN] == NULL || |
4584 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
4585 | return -EINVAL; |
4586 | ||
c50b960c PM |
4587 | memset(&desc, 0, sizeof(desc)); |
4588 | ||
bed4a63e | 4589 | desc.ktype = NFT_DATA_VALUE; |
20a69341 | 4590 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { |
bed4a63e PNA |
4591 | desc.ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); |
4592 | if ((desc.ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
20a69341 PM |
4593 | return -EINVAL; |
4594 | } | |
4595 | ||
c50b960c | 4596 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 4597 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4598 | return -EINVAL; |
4599 | ||
4600 | flags = 0; | |
4601 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
4602 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
4603 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 4604 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 | 4605 | NFT_SET_MAP | NFT_SET_EVAL | |
b4e70d8d | 4606 | NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR)) |
d9583cdf | 4607 | return -EOPNOTSUPP; |
8aeff920 | 4608 | /* Only one of these operations is supported */ |
acab7131 FW |
4609 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
4610 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
4611 | return -EOPNOTSUPP; | |
4612 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
4613 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 4614 | return -EOPNOTSUPP; |
20a69341 PM |
4615 | } |
4616 | ||
bed4a63e | 4617 | desc.dtype = 0; |
20a69341 PM |
4618 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
4619 | if (!(flags & NFT_SET_MAP)) | |
4620 | return -EINVAL; | |
4621 | ||
bed4a63e PNA |
4622 | desc.dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); |
4623 | if ((desc.dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
4624 | desc.dtype != NFT_DATA_VERDICT) | |
20a69341 PM |
4625 | return -EINVAL; |
4626 | ||
bed4a63e | 4627 | if (desc.dtype != NFT_DATA_VERDICT) { |
20a69341 PM |
4628 | if (nla[NFTA_SET_DATA_LEN] == NULL) |
4629 | return -EINVAL; | |
c50b960c | 4630 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 4631 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4632 | return -EINVAL; |
4633 | } else | |
7d740264 | 4634 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
4635 | } else if (flags & NFT_SET_MAP) |
4636 | return -EINVAL; | |
4637 | ||
8aeff920 PNA |
4638 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
4639 | if (!(flags & NFT_SET_OBJECT)) | |
4640 | return -EINVAL; | |
4641 | ||
bed4a63e PNA |
4642 | desc.objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); |
4643 | if (desc.objtype == NFT_OBJECT_UNSPEC || | |
4644 | desc.objtype > NFT_OBJECT_MAX) | |
d9583cdf | 4645 | return -EOPNOTSUPP; |
8aeff920 PNA |
4646 | } else if (flags & NFT_SET_OBJECT) |
4647 | return -EINVAL; | |
4648 | else | |
bed4a63e | 4649 | desc.objtype = NFT_OBJECT_UNSPEC; |
8aeff920 | 4650 | |
bed4a63e | 4651 | desc.timeout = 0; |
761da293 PM |
4652 | if (nla[NFTA_SET_TIMEOUT] != NULL) { |
4653 | if (!(flags & NFT_SET_TIMEOUT)) | |
4654 | return -EINVAL; | |
8e1102d5 | 4655 | |
bed4a63e | 4656 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &desc.timeout); |
8e1102d5 FW |
4657 | if (err) |
4658 | return err; | |
761da293 | 4659 | } |
bed4a63e | 4660 | desc.gc_int = 0; |
761da293 PM |
4661 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { |
4662 | if (!(flags & NFT_SET_TIMEOUT)) | |
4663 | return -EINVAL; | |
bed4a63e | 4664 | desc.gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); |
761da293 PM |
4665 | } |
4666 | ||
bed4a63e | 4667 | desc.policy = NFT_SET_POL_PERFORMANCE; |
c50b960c | 4668 | if (nla[NFTA_SET_POLICY] != NULL) |
bed4a63e | 4669 | desc.policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); |
c50b960c PM |
4670 | |
4671 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4672 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4673 | if (err < 0) |
4674 | return err; | |
1b6345d4 PNA |
4675 | |
4676 | if (desc.field_count > 1 && !(flags & NFT_SET_CONCAT)) | |
4677 | return -EINVAL; | |
4678 | } else if (flags & NFT_SET_CONCAT) { | |
4679 | return -EINVAL; | |
c50b960c PM |
4680 | } |
4681 | ||
48b0ae04 | 4682 | if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS]) |
d56aab26 PNA |
4683 | desc.expr = true; |
4684 | ||
6001a930 PNA |
4685 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask, |
4686 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4687 | if (IS_ERR(table)) { |
4688 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4689 | return PTR_ERR(table); |
36dd1bcc | 4690 | } |
20a69341 | 4691 | |
7dab8ee3 | 4692 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
20a69341 | 4693 | |
cac20fcd | 4694 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4695 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4696 | if (PTR_ERR(set) != -ENOENT) { |
4697 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4698 | return PTR_ERR(set); |
36dd1bcc | 4699 | } |
1a28ad74 | 4700 | } else { |
a8fe4154 PNA |
4701 | struct nft_expr *exprs[NFT_SET_EXPR_MAX] = {}; |
4702 | ||
7dab8ee3 | 4703 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 4704 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); |
20a69341 | 4705 | return -EEXIST; |
36dd1bcc | 4706 | } |
7dab8ee3 | 4707 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
20a69341 | 4708 | return -EOPNOTSUPP; |
36dd1bcc | 4709 | |
a8fe4154 PNA |
4710 | err = nft_set_expr_alloc(&ctx, set, nla, exprs, &num_exprs, flags); |
4711 | if (err < 0) | |
4712 | return err; | |
4713 | ||
f6594c37 PNA |
4714 | err = 0; |
4715 | if (!nft_set_is_same(set, &desc, exprs, num_exprs, flags)) { | |
4716 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
4717 | err = -EEXIST; | |
4718 | } | |
4719 | ||
a8fe4154 PNA |
4720 | for (i = 0; i < num_exprs; i++) |
4721 | nft_expr_destroy(&ctx, exprs[i]); | |
4722 | ||
123b9961 PNA |
4723 | if (err < 0) |
4724 | return err; | |
4725 | ||
4726 | return __nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set, &desc); | |
20a69341 PM |
4727 | } |
4728 | ||
7dab8ee3 | 4729 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
20a69341 PM |
4730 | return -ENOENT; |
4731 | ||
bed4a63e | 4732 | ops = nft_select_set_ops(&ctx, nla, &desc); |
20a69341 PM |
4733 | if (IS_ERR(ops)) |
4734 | return PTR_ERR(ops); | |
4735 | ||
e6d8ecac CFG |
4736 | udlen = 0; |
4737 | if (nla[NFTA_SET_USERDATA]) | |
4738 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
4739 | ||
20a69341 PM |
4740 | size = 0; |
4741 | if (ops->privsize != NULL) | |
347b408d | 4742 | size = ops->privsize(nla, &desc); |
6c8774a9 | 4743 | alloc_size = sizeof(*set) + size + udlen; |
45928afe | 4744 | if (alloc_size < size || alloc_size > INT_MAX) |
6c8774a9 | 4745 | return -ENOMEM; |
33758c89 | 4746 | set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT); |
24d19826 FW |
4747 | if (!set) |
4748 | return -ENOMEM; | |
20a69341 | 4749 | |
33758c89 | 4750 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT); |
38745490 PS |
4751 | if (!name) { |
4752 | err = -ENOMEM; | |
65038428 | 4753 | goto err_set_name; |
38745490 PS |
4754 | } |
4755 | ||
20a69341 | 4756 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 4757 | kfree(name); |
20a69341 | 4758 | if (err < 0) |
ad9f151e PNA |
4759 | goto err_set_name; |
4760 | ||
4761 | udata = NULL; | |
4762 | if (udlen) { | |
4763 | udata = set->data + size; | |
4764 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
4765 | } | |
4766 | ||
4767 | INIT_LIST_HEAD(&set->bindings); | |
4768 | INIT_LIST_HEAD(&set->catchall_list); | |
4769 | set->table = table; | |
4770 | write_pnet(&set->net, net); | |
4771 | set->ops = ops; | |
bed4a63e | 4772 | set->ktype = desc.ktype; |
ad9f151e | 4773 | set->klen = desc.klen; |
bed4a63e PNA |
4774 | set->dtype = desc.dtype; |
4775 | set->objtype = desc.objtype; | |
ad9f151e PNA |
4776 | set->dlen = desc.dlen; |
4777 | set->flags = flags; | |
4778 | set->size = desc.size; | |
bed4a63e | 4779 | set->policy = desc.policy; |
ad9f151e PNA |
4780 | set->udlen = udlen; |
4781 | set->udata = udata; | |
bed4a63e PNA |
4782 | set->timeout = desc.timeout; |
4783 | set->gc_int = desc.gc_int; | |
ad9f151e PNA |
4784 | |
4785 | set->field_count = desc.field_count; | |
4786 | for (i = 0; i < desc.field_count; i++) | |
4787 | set->field_len[i] = desc.field_len[i]; | |
4788 | ||
4789 | err = ops->init(set, &desc, nla); | |
4790 | if (err < 0) | |
4791 | goto err_set_init; | |
65038428 | 4792 | |
a8fe4154 PNA |
4793 | err = nft_set_expr_alloc(&ctx, set, nla, set->exprs, &num_exprs, flags); |
4794 | if (err < 0) | |
4795 | goto err_set_destroy; | |
20a69341 | 4796 | |
a8fe4154 | 4797 | set->num_exprs = num_exprs; |
3ecbfd65 | 4798 | set->handle = nf_tables_alloc_handle(table); |
20a69341 | 4799 | |
958bee14 | 4800 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 4801 | if (err < 0) |
ad9f151e | 4802 | goto err_set_expr_alloc; |
20a69341 | 4803 | |
e688a7f8 | 4804 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 4805 | table->use++; |
20a69341 PM |
4806 | return 0; |
4807 | ||
ad9f151e | 4808 | err_set_expr_alloc: |
8cfd9b0f PNA |
4809 | for (i = 0; i < set->num_exprs; i++) |
4810 | nft_expr_destroy(&ctx, set->exprs[i]); | |
a8fe4154 | 4811 | err_set_destroy: |
ad9f151e PNA |
4812 | ops->destroy(set); |
4813 | err_set_init: | |
2f6adf48 | 4814 | kfree(set->name); |
65038428 | 4815 | err_set_name: |
1ff75a3e | 4816 | kvfree(set); |
20a69341 PM |
4817 | return err; |
4818 | } | |
4819 | ||
aaa31047 PNA |
4820 | static void nft_set_catchall_destroy(const struct nft_ctx *ctx, |
4821 | struct nft_set *set) | |
4822 | { | |
0f7d9b31 | 4823 | struct nft_set_elem_catchall *next, *catchall; |
aaa31047 | 4824 | |
0f7d9b31 | 4825 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
aaa31047 PNA |
4826 | list_del_rcu(&catchall->list); |
4827 | nft_set_elem_destroy(set, catchall->elem, true); | |
ae089831 | 4828 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
4829 | } |
4830 | } | |
4831 | ||
0c2a85ed | 4832 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
20a69341 | 4833 | { |
8cfd9b0f PNA |
4834 | int i; |
4835 | ||
273fe3f1 PNA |
4836 | if (WARN_ON(set->use > 0)) |
4837 | return; | |
4838 | ||
8cfd9b0f PNA |
4839 | for (i = 0; i < set->num_exprs; i++) |
4840 | nft_expr_destroy(ctx, set->exprs[i]); | |
65038428 | 4841 | |
20a69341 | 4842 | set->ops->destroy(set); |
aaa31047 | 4843 | nft_set_catchall_destroy(ctx, set); |
38745490 | 4844 | kfree(set->name); |
1ff75a3e | 4845 | kvfree(set); |
20a69341 PM |
4846 | } |
4847 | ||
7dab8ee3 PNA |
4848 | static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info, |
4849 | const struct nlattr * const nla[]) | |
20a69341 | 4850 | { |
7dab8ee3 PNA |
4851 | struct netlink_ext_ack *extack = info->extack; |
4852 | u8 genmask = nft_genmask_next(info->net); | |
67086651 | 4853 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 4854 | struct net *net = info->net; |
36dd1bcc | 4855 | const struct nlattr *attr; |
67086651 | 4856 | struct nft_table *table; |
20a69341 PM |
4857 | struct nft_set *set; |
4858 | struct nft_ctx ctx; | |
20a69341 | 4859 | |
ef4b65e5 | 4860 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
ec2c9935 | 4861 | return -EAFNOSUPPORT; |
20a69341 | 4862 | |
67086651 PNA |
4863 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
4864 | genmask, NETLINK_CB(skb).portid); | |
4865 | if (IS_ERR(table)) { | |
4866 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4867 | return PTR_ERR(table); | |
4868 | } | |
20a69341 | 4869 | |
36dd1bcc PNA |
4870 | if (nla[NFTA_SET_HANDLE]) { |
4871 | attr = nla[NFTA_SET_HANDLE]; | |
67086651 | 4872 | set = nft_set_lookup_byhandle(table, attr, genmask); |
36dd1bcc PNA |
4873 | } else { |
4874 | attr = nla[NFTA_SET_NAME]; | |
67086651 | 4875 | set = nft_set_lookup(table, attr, genmask); |
36dd1bcc | 4876 | } |
a8278400 | 4877 | |
36dd1bcc | 4878 | if (IS_ERR(set)) { |
f80a612d FFM |
4879 | if (PTR_ERR(set) == -ENOENT && |
4880 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET) | |
4881 | return 0; | |
4882 | ||
36dd1bcc PNA |
4883 | NL_SET_BAD_ATTR(extack, attr); |
4884 | return PTR_ERR(set); | |
4885 | } | |
273fe3f1 | 4886 | if (set->use || |
7dab8ee3 PNA |
4887 | (info->nlh->nlmsg_flags & NLM_F_NONREC && |
4888 | atomic_read(&set->nelems) > 0)) { | |
36dd1bcc | 4889 | NL_SET_BAD_ATTR(extack, attr); |
20a69341 | 4890 | return -EBUSY; |
36dd1bcc | 4891 | } |
20a69341 | 4892 | |
67086651 PNA |
4893 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
4894 | ||
ee01d542 | 4895 | return nft_delset(&ctx, set); |
20a69341 PM |
4896 | } |
4897 | ||
345023b0 PNA |
4898 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
4899 | enum nft_registers reg, | |
4900 | const struct nft_data *data, | |
4901 | enum nft_data_types type, | |
4902 | unsigned int len); | |
4903 | ||
97c976d6 PNA |
4904 | static int nft_setelem_data_validate(const struct nft_ctx *ctx, |
4905 | struct nft_set *set, | |
4906 | struct nft_set_elem *elem) | |
20a69341 | 4907 | { |
fe2811eb | 4908 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4909 | enum nft_registers dreg; |
4910 | ||
4911 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
4912 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
4913 | set->dtype == NFT_DATA_VERDICT ? | |
4914 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
4915 | set->dlen); | |
20a69341 PM |
4916 | } |
4917 | ||
97c976d6 PNA |
4918 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, |
4919 | struct nft_set *set, | |
4920 | const struct nft_set_iter *iter, | |
4921 | struct nft_set_elem *elem) | |
4922 | { | |
4923 | return nft_setelem_data_validate(ctx, set, elem); | |
4924 | } | |
4925 | ||
aaa31047 PNA |
4926 | static int nft_set_catchall_bind_check(const struct nft_ctx *ctx, |
4927 | struct nft_set *set) | |
4928 | { | |
4929 | u8 genmask = nft_genmask_next(ctx->net); | |
4930 | struct nft_set_elem_catchall *catchall; | |
4931 | struct nft_set_elem elem; | |
4932 | struct nft_set_ext *ext; | |
4933 | int ret = 0; | |
4934 | ||
4935 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
4936 | ext = nft_set_elem_ext(set, catchall->elem); | |
4937 | if (!nft_set_elem_active(ext, genmask)) | |
4938 | continue; | |
4939 | ||
4940 | elem.priv = catchall->elem; | |
4941 | ret = nft_setelem_data_validate(ctx, set, &elem); | |
4942 | if (ret < 0) | |
4943 | break; | |
4944 | } | |
4945 | ||
4946 | return ret; | |
4947 | } | |
4948 | ||
20a69341 PM |
4949 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4950 | struct nft_set_binding *binding) | |
4951 | { | |
4952 | struct nft_set_binding *i; | |
4953 | struct nft_set_iter iter; | |
4954 | ||
273fe3f1 PNA |
4955 | if (set->use == UINT_MAX) |
4956 | return -EOVERFLOW; | |
4957 | ||
408070d6 | 4958 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
4959 | return -EBUSY; |
4960 | ||
11113e19 | 4961 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
4962 | /* If the set is already bound to the same chain all |
4963 | * jumps are already validated for that chain. | |
4964 | */ | |
4965 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 4966 | if (i->flags & NFT_SET_MAP && |
11113e19 | 4967 | i->chain == binding->chain) |
20a69341 PM |
4968 | goto bind; |
4969 | } | |
4970 | ||
8588ac09 | 4971 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
4972 | iter.skip = 0; |
4973 | iter.count = 0; | |
4974 | iter.err = 0; | |
4975 | iter.fn = nf_tables_bind_check_setelem; | |
4976 | ||
4977 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
4978 | if (!iter.err) |
4979 | iter.err = nft_set_catchall_bind_check(ctx, set); | |
4980 | ||
a02f4248 | 4981 | if (iter.err < 0) |
20a69341 | 4982 | return iter.err; |
20a69341 PM |
4983 | } |
4984 | bind: | |
4985 | binding->chain = ctx->chain; | |
e688a7f8 | 4986 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 | 4987 | nft_set_trans_bind(ctx, set); |
273fe3f1 | 4988 | set->use++; |
f6ac8585 | 4989 | |
20a69341 PM |
4990 | return 0; |
4991 | } | |
63aea290 | 4992 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 4993 | |
3b0a081d FW |
4994 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4995 | struct nft_set_binding *binding, bool event) | |
20a69341 | 4996 | { |
e688a7f8 | 4997 | list_del_rcu(&binding->list); |
20a69341 | 4998 | |
f6ac8585 | 4999 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 5000 | list_del_rcu(&set->list); |
f6ac8585 PNA |
5001 | if (event) |
5002 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
5003 | GFP_KERNEL); | |
5004 | } | |
20a69341 PM |
5005 | } |
5006 | ||
273fe3f1 PNA |
5007 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
5008 | struct nft_set_binding *binding, | |
5009 | enum nft_trans_phase phase) | |
5010 | { | |
5011 | switch (phase) { | |
5012 | case NFT_TRANS_PREPARE: | |
5013 | set->use--; | |
5014 | return; | |
5015 | case NFT_TRANS_ABORT: | |
5016 | case NFT_TRANS_RELEASE: | |
5017 | set->use--; | |
954d8297 | 5018 | fallthrough; |
273fe3f1 PNA |
5019 | default: |
5020 | nf_tables_unbind_set(ctx, set, binding, | |
5021 | phase == NFT_TRANS_COMMIT); | |
5022 | } | |
5023 | } | |
5024 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
5025 | ||
cd5125d8 FW |
5026 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
5027 | { | |
f6ac8585 | 5028 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
0c2a85ed | 5029 | nft_set_destroy(ctx, set); |
cd5125d8 FW |
5030 | } |
5031 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
5032 | ||
3ac4c07a PM |
5033 | const struct nft_set_ext_type nft_set_ext_types[] = { |
5034 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 5035 | .align = __alignof__(u32), |
3ac4c07a PM |
5036 | }, |
5037 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 5038 | .align = __alignof__(u32), |
3ac4c07a | 5039 | }, |
563125a7 PNA |
5040 | [NFT_SET_EXT_EXPRESSIONS] = { |
5041 | .align = __alignof__(struct nft_set_elem_expr), | |
f25ad2e9 | 5042 | }, |
8aeff920 PNA |
5043 | [NFT_SET_EXT_OBJREF] = { |
5044 | .len = sizeof(struct nft_object *), | |
5045 | .align = __alignof__(struct nft_object *), | |
5046 | }, | |
3ac4c07a PM |
5047 | [NFT_SET_EXT_FLAGS] = { |
5048 | .len = sizeof(u8), | |
5049 | .align = __alignof__(u8), | |
5050 | }, | |
c3e1b005 PM |
5051 | [NFT_SET_EXT_TIMEOUT] = { |
5052 | .len = sizeof(u64), | |
5053 | .align = __alignof__(u64), | |
5054 | }, | |
5055 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
5056 | .len = sizeof(u64), |
5057 | .align = __alignof__(u64), | |
c3e1b005 | 5058 | }, |
68e942e8 PM |
5059 | [NFT_SET_EXT_USERDATA] = { |
5060 | .len = sizeof(struct nft_userdata), | |
5061 | .align = __alignof__(struct nft_userdata), | |
5062 | }, | |
7b225d0b PNA |
5063 | [NFT_SET_EXT_KEY_END] = { |
5064 | .align = __alignof__(u32), | |
5065 | }, | |
3ac4c07a | 5066 | }; |
3ac4c07a | 5067 | |
20a69341 PM |
5068 | /* |
5069 | * Set elements | |
5070 | */ | |
5071 | ||
5072 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
5073 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
5074 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
5075 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 5076 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 5077 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
5078 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
5079 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 5080 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
5081 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
5082 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 5083 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
48b0ae04 | 5084 | [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED }, |
20a69341 PM |
5085 | }; |
5086 | ||
5087 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
5088 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
5089 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
5090 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
5091 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 5092 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 5093 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
5094 | }; |
5095 | ||
563125a7 PNA |
5096 | static int nft_set_elem_expr_dump(struct sk_buff *skb, |
5097 | const struct nft_set *set, | |
5098 | const struct nft_set_ext *ext) | |
5099 | { | |
5100 | struct nft_set_elem_expr *elem_expr; | |
5101 | u32 size, num_exprs = 0; | |
5102 | struct nft_expr *expr; | |
48b0ae04 | 5103 | struct nlattr *nest; |
563125a7 PNA |
5104 | |
5105 | elem_expr = nft_set_ext_expr(ext); | |
5106 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
5107 | num_exprs++; | |
5108 | ||
5109 | if (num_exprs == 1) { | |
5110 | expr = nft_setelem_expr_at(elem_expr, 0); | |
8daa8fde | 5111 | if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr, false) < 0) |
563125a7 PNA |
5112 | return -1; |
5113 | ||
5114 | return 0; | |
48b0ae04 PNA |
5115 | } else if (num_exprs > 1) { |
5116 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS); | |
5117 | if (nest == NULL) | |
5118 | goto nla_put_failure; | |
563125a7 | 5119 | |
48b0ae04 PNA |
5120 | nft_setelem_expr_foreach(expr, elem_expr, size) { |
5121 | expr = nft_setelem_expr_at(elem_expr, size); | |
8daa8fde | 5122 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, false) < 0) |
48b0ae04 PNA |
5123 | goto nla_put_failure; |
5124 | } | |
5125 | nla_nest_end(skb, nest); | |
5126 | } | |
563125a7 | 5127 | return 0; |
48b0ae04 PNA |
5128 | |
5129 | nla_put_failure: | |
5130 | return -1; | |
563125a7 PNA |
5131 | } |
5132 | ||
20a69341 PM |
5133 | static int nf_tables_fill_setelem(struct sk_buff *skb, |
5134 | const struct nft_set *set, | |
5135 | const struct nft_set_elem *elem) | |
5136 | { | |
fe2811eb | 5137 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
5138 | unsigned char *b = skb_tail_pointer(skb); |
5139 | struct nlattr *nest; | |
5140 | ||
ae0be8de | 5141 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
5142 | if (nest == NULL) |
5143 | goto nla_put_failure; | |
5144 | ||
aaa31047 PNA |
5145 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
5146 | nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), | |
fe2811eb | 5147 | NFT_DATA_VALUE, set->klen) < 0) |
20a69341 PM |
5148 | goto nla_put_failure; |
5149 | ||
7b225d0b PNA |
5150 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
5151 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
5152 | NFT_DATA_VALUE, set->klen) < 0) | |
5153 | goto nla_put_failure; | |
5154 | ||
fe2811eb PM |
5155 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
5156 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
5157 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
5158 | set->dlen) < 0) | |
5159 | goto nla_put_failure; | |
5160 | ||
563125a7 PNA |
5161 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) && |
5162 | nft_set_elem_expr_dump(skb, set, ext)) | |
f25ad2e9 PM |
5163 | goto nla_put_failure; |
5164 | ||
8aeff920 PNA |
5165 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
5166 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 5167 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
5168 | goto nla_put_failure; |
5169 | ||
fe2811eb PM |
5170 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
5171 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
5172 | htonl(*nft_set_ext_flags(ext)))) | |
5173 | goto nla_put_failure; | |
20a69341 | 5174 | |
c3e1b005 PM |
5175 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
5176 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 5177 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 5178 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
5179 | goto nla_put_failure; |
5180 | ||
5181 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 5182 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
5183 | |
5184 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 5185 | if (time_before64(now, expires)) |
c3e1b005 PM |
5186 | expires -= now; |
5187 | else | |
5188 | expires = 0; | |
5189 | ||
5190 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 5191 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 5192 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
5193 | goto nla_put_failure; |
5194 | } | |
5195 | ||
68e942e8 PM |
5196 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
5197 | struct nft_userdata *udata; | |
5198 | ||
5199 | udata = nft_set_ext_userdata(ext); | |
5200 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
5201 | udata->len + 1, udata->data)) | |
5202 | goto nla_put_failure; | |
5203 | } | |
5204 | ||
20a69341 PM |
5205 | nla_nest_end(skb, nest); |
5206 | return 0; | |
5207 | ||
5208 | nla_put_failure: | |
5209 | nlmsg_trim(skb, b); | |
5210 | return -EMSGSIZE; | |
5211 | } | |
5212 | ||
5213 | struct nft_set_dump_args { | |
5214 | const struct netlink_callback *cb; | |
5215 | struct nft_set_iter iter; | |
5216 | struct sk_buff *skb; | |
5217 | }; | |
5218 | ||
5219 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 5220 | struct nft_set *set, |
20a69341 | 5221 | const struct nft_set_iter *iter, |
de70185d | 5222 | struct nft_set_elem *elem) |
20a69341 PM |
5223 | { |
5224 | struct nft_set_dump_args *args; | |
5225 | ||
5226 | args = container_of(iter, struct nft_set_dump_args, iter); | |
5227 | return nf_tables_fill_setelem(args->skb, set, elem); | |
5228 | } | |
5229 | ||
fa803605 LZ |
5230 | struct nft_set_dump_ctx { |
5231 | const struct nft_set *set; | |
5232 | struct nft_ctx ctx; | |
5233 | }; | |
5234 | ||
aaa31047 PNA |
5235 | static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb, |
5236 | const struct nft_set *set) | |
5237 | { | |
5238 | struct nft_set_elem_catchall *catchall; | |
5239 | u8 genmask = nft_genmask_cur(net); | |
5240 | struct nft_set_elem elem; | |
5241 | struct nft_set_ext *ext; | |
5242 | int ret = 0; | |
5243 | ||
5244 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5245 | ext = nft_set_elem_ext(set, catchall->elem); | |
5246 | if (!nft_set_elem_active(ext, genmask) || | |
5247 | nft_set_elem_expired(ext)) | |
5248 | continue; | |
5249 | ||
5250 | elem.priv = catchall->elem; | |
5251 | ret = nf_tables_fill_setelem(skb, set, &elem); | |
5252 | break; | |
5253 | } | |
5254 | ||
5255 | return ret; | |
5256 | } | |
5257 | ||
20a69341 PM |
5258 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
5259 | { | |
fa803605 | 5260 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 5261 | struct net *net = sock_net(skb->sk); |
0854db2a | 5262 | struct nftables_pernet *nft_net; |
fa803605 | 5263 | struct nft_table *table; |
de70185d | 5264 | struct nft_set *set; |
20a69341 | 5265 | struct nft_set_dump_args args; |
fa803605 | 5266 | bool set_found = false; |
20a69341 PM |
5267 | struct nlmsghdr *nlh; |
5268 | struct nlattr *nest; | |
5269 | u32 portid, seq; | |
fa803605 | 5270 | int event; |
20a69341 | 5271 | |
fa803605 | 5272 | rcu_read_lock(); |
d59d2f82 | 5273 | nft_net = nft_pernet(net); |
34002783 PNA |
5274 | cb->seq = READ_ONCE(nft_net->base_seq); |
5275 | ||
0854db2a | 5276 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 5277 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && |
98319cb9 | 5278 | dump_ctx->ctx.family != table->family) |
fa803605 | 5279 | continue; |
20a69341 | 5280 | |
36596dad PNA |
5281 | if (table != dump_ctx->ctx.table) |
5282 | continue; | |
20a69341 | 5283 | |
36596dad PNA |
5284 | list_for_each_entry_rcu(set, &table->sets, list) { |
5285 | if (set == dump_ctx->set) { | |
5286 | set_found = true; | |
5287 | break; | |
fa803605 | 5288 | } |
fa803605 LZ |
5289 | } |
5290 | break; | |
5291 | } | |
5292 | ||
5293 | if (!set_found) { | |
5294 | rcu_read_unlock(); | |
5295 | return -ENOENT; | |
5296 | } | |
20a69341 | 5297 | |
dedb67c4 | 5298 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
5299 | portid = NETLINK_CB(cb->skb).portid; |
5300 | seq = cb->nlh->nlmsg_seq; | |
5301 | ||
19c28b13 PNA |
5302 | nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI, |
5303 | table->family, NFNETLINK_V0, nft_base_seq(net)); | |
5304 | if (!nlh) | |
20a69341 PM |
5305 | goto nla_put_failure; |
5306 | ||
fa803605 | 5307 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
5308 | goto nla_put_failure; |
5309 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
5310 | goto nla_put_failure; | |
5311 | ||
ae0be8de | 5312 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
5313 | if (nest == NULL) |
5314 | goto nla_put_failure; | |
5315 | ||
8588ac09 PNA |
5316 | args.cb = cb; |
5317 | args.skb = skb; | |
fa803605 | 5318 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
5319 | args.iter.skip = cb->args[0]; |
5320 | args.iter.count = 0; | |
5321 | args.iter.err = 0; | |
5322 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 | 5323 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
aaa31047 PNA |
5324 | |
5325 | if (!args.iter.err && args.iter.count == cb->args[0]) | |
5326 | args.iter.err = nft_set_catchall_dump(net, skb, set); | |
fa803605 | 5327 | rcu_read_unlock(); |
20a69341 PM |
5328 | |
5329 | nla_nest_end(skb, nest); | |
5330 | nlmsg_end(skb, nlh); | |
5331 | ||
5332 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
5333 | return args.iter.err; | |
5334 | if (args.iter.count == cb->args[0]) | |
5335 | return 0; | |
5336 | ||
5337 | cb->args[0] = args.iter.count; | |
5338 | return skb->len; | |
5339 | ||
5340 | nla_put_failure: | |
fa803605 | 5341 | rcu_read_unlock(); |
20a69341 PM |
5342 | return -ENOSPC; |
5343 | } | |
5344 | ||
90fd131a FW |
5345 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
5346 | { | |
5347 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
5348 | ||
5349 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
5350 | ||
5351 | return cb->data ? 0 : -ENOMEM; | |
5352 | } | |
5353 | ||
fa803605 LZ |
5354 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
5355 | { | |
5356 | kfree(cb->data); | |
5357 | return 0; | |
5358 | } | |
5359 | ||
d60ce62f AB |
5360 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
5361 | const struct nft_ctx *ctx, u32 seq, | |
5362 | u32 portid, int event, u16 flags, | |
5363 | const struct nft_set *set, | |
5364 | const struct nft_set_elem *elem) | |
5365 | { | |
d60ce62f AB |
5366 | struct nlmsghdr *nlh; |
5367 | struct nlattr *nest; | |
5368 | int err; | |
5369 | ||
dedb67c4 | 5370 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
5371 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
5372 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
5373 | if (!nlh) | |
d60ce62f AB |
5374 | goto nla_put_failure; |
5375 | ||
d60ce62f AB |
5376 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
5377 | goto nla_put_failure; | |
5378 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
5379 | goto nla_put_failure; | |
5380 | ||
ae0be8de | 5381 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
5382 | if (nest == NULL) |
5383 | goto nla_put_failure; | |
5384 | ||
5385 | err = nf_tables_fill_setelem(skb, set, elem); | |
5386 | if (err < 0) | |
5387 | goto nla_put_failure; | |
5388 | ||
5389 | nla_nest_end(skb, nest); | |
5390 | ||
053c095a JB |
5391 | nlmsg_end(skb, nlh); |
5392 | return 0; | |
d60ce62f AB |
5393 | |
5394 | nla_put_failure: | |
5395 | nlmsg_trim(skb, nlh); | |
5396 | return -1; | |
5397 | } | |
5398 | ||
ba0e4d99 PNA |
5399 | static int nft_setelem_parse_flags(const struct nft_set *set, |
5400 | const struct nlattr *attr, u32 *flags) | |
5401 | { | |
5402 | if (attr == NULL) | |
5403 | return 0; | |
5404 | ||
5405 | *flags = ntohl(nla_get_be32(attr)); | |
aaa31047 PNA |
5406 | if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) |
5407 | return -EOPNOTSUPP; | |
ba0e4d99 PNA |
5408 | if (!(set->flags & NFT_SET_INTERVAL) && |
5409 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
5410 | return -EINVAL; | |
fc0ae524 PNA |
5411 | if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) == |
5412 | (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) | |
5413 | return -EINVAL; | |
ba0e4d99 PNA |
5414 | |
5415 | return 0; | |
5416 | } | |
5417 | ||
20a1452c PNA |
5418 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
5419 | struct nft_data *key, struct nlattr *attr) | |
5420 | { | |
341b6941 PNA |
5421 | struct nft_data_desc desc = { |
5422 | .type = NFT_DATA_VALUE, | |
5423 | .size = NFT_DATA_VALUE_MAXLEN, | |
5424 | .len = set->klen, | |
5425 | }; | |
20a1452c | 5426 | |
341b6941 | 5427 | return nft_data_init(ctx, key, &desc, attr); |
20a1452c PNA |
5428 | } |
5429 | ||
fdb9c405 PNA |
5430 | static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, |
5431 | struct nft_data_desc *desc, | |
5432 | struct nft_data *data, | |
5433 | struct nlattr *attr) | |
5434 | { | |
7e6bc1f6 | 5435 | u32 dtype; |
fdb9c405 | 5436 | |
7e6bc1f6 PNA |
5437 | if (set->dtype == NFT_DATA_VERDICT) |
5438 | dtype = NFT_DATA_VERDICT; | |
5439 | else | |
5440 | dtype = NFT_DATA_VALUE; | |
5441 | ||
341b6941 PNA |
5442 | desc->type = dtype; |
5443 | desc->size = NFT_DATA_VALUE_MAXLEN; | |
5444 | desc->len = set->dlen; | |
f323ef3a | 5445 | desc->flags = NFT_DATA_DESC_SETELEM; |
fdb9c405 | 5446 | |
341b6941 | 5447 | return nft_data_init(ctx, data, desc, attr); |
fdb9c405 PNA |
5448 | } |
5449 | ||
aaa31047 PNA |
5450 | static void *nft_setelem_catchall_get(const struct net *net, |
5451 | const struct nft_set *set) | |
5452 | { | |
5453 | struct nft_set_elem_catchall *catchall; | |
5454 | u8 genmask = nft_genmask_cur(net); | |
5455 | struct nft_set_ext *ext; | |
5456 | void *priv = NULL; | |
5457 | ||
5458 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5459 | ext = nft_set_elem_ext(set, catchall->elem); | |
5460 | if (!nft_set_elem_active(ext, genmask) || | |
5461 | nft_set_elem_expired(ext)) | |
5462 | continue; | |
5463 | ||
5464 | priv = catchall->elem; | |
5465 | break; | |
5466 | } | |
5467 | ||
5468 | return priv; | |
5469 | } | |
5470 | ||
5471 | static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set, | |
5472 | struct nft_set_elem *elem, u32 flags) | |
5473 | { | |
5474 | void *priv; | |
5475 | ||
5476 | if (!(flags & NFT_SET_ELEM_CATCHALL)) { | |
5477 | priv = set->ops->get(ctx->net, set, elem, flags); | |
5478 | if (IS_ERR(priv)) | |
5479 | return PTR_ERR(priv); | |
5480 | } else { | |
5481 | priv = nft_setelem_catchall_get(ctx->net, set); | |
5482 | if (!priv) | |
5483 | return -ENOENT; | |
5484 | } | |
5485 | elem->priv = priv; | |
5486 | ||
5487 | return 0; | |
5488 | } | |
5489 | ||
ba0e4d99 PNA |
5490 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
5491 | const struct nlattr *attr) | |
5492 | { | |
5493 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
5494 | struct nft_set_elem elem; |
5495 | struct sk_buff *skb; | |
5496 | uint32_t flags = 0; | |
ba0e4d99 PNA |
5497 | int err; |
5498 | ||
8cb08174 JB |
5499 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5500 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
5501 | if (err < 0) |
5502 | return err; | |
5503 | ||
ba0e4d99 PNA |
5504 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5505 | if (err < 0) | |
5506 | return err; | |
5507 | ||
aaa31047 PNA |
5508 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
5509 | return -EINVAL; | |
5510 | ||
5511 | if (nla[NFTA_SET_ELEM_KEY]) { | |
5512 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
5513 | nla[NFTA_SET_ELEM_KEY]); | |
5514 | if (err < 0) | |
5515 | return err; | |
5516 | } | |
ba0e4d99 | 5517 | |
7b225d0b PNA |
5518 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5519 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5520 | nla[NFTA_SET_ELEM_KEY_END]); | |
5521 | if (err < 0) | |
5522 | return err; | |
5523 | } | |
5524 | ||
aaa31047 PNA |
5525 | err = nft_setelem_get(ctx, set, &elem, flags); |
5526 | if (err < 0) | |
5527 | return err; | |
ba0e4d99 PNA |
5528 | |
5529 | err = -ENOMEM; | |
d9adf22a | 5530 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 | 5531 | if (skb == NULL) |
ee921183 | 5532 | return err; |
ba0e4d99 PNA |
5533 | |
5534 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
5535 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
5536 | if (err < 0) | |
ee921183 | 5537 | goto err_fill_setelem; |
ba0e4d99 | 5538 | |
ee921183 | 5539 | return nfnetlink_unicast(skb, ctx->net, ctx->portid); |
ba0e4d99 | 5540 | |
ee921183 | 5541 | err_fill_setelem: |
ba0e4d99 | 5542 | kfree_skb(skb); |
ee921183 | 5543 | return err; |
ba0e4d99 PNA |
5544 | } |
5545 | ||
d9adf22a | 5546 | /* called with rcu_read_lock held */ |
797d4980 PNA |
5547 | static int nf_tables_getsetelem(struct sk_buff *skb, |
5548 | const struct nfnl_info *info, | |
5549 | const struct nlattr * const nla[]) | |
ba0e4d99 | 5550 | { |
797d4980 PNA |
5551 | struct netlink_ext_ack *extack = info->extack; |
5552 | u8 genmask = nft_genmask_cur(info->net); | |
e2b750d7 | 5553 | u8 family = info->nfmsg->nfgen_family; |
797d4980 | 5554 | struct net *net = info->net; |
e2b750d7 | 5555 | struct nft_table *table; |
ba0e4d99 PNA |
5556 | struct nft_set *set; |
5557 | struct nlattr *attr; | |
5558 | struct nft_ctx ctx; | |
5559 | int rem, err = 0; | |
5560 | ||
e2b750d7 | 5561 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
92f3e96d | 5562 | genmask, 0); |
e2b750d7 PNA |
5563 | if (IS_ERR(table)) { |
5564 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
5565 | return PTR_ERR(table); | |
5566 | } | |
ba0e4d99 | 5567 | |
e2b750d7 | 5568 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
5569 | if (IS_ERR(set)) |
5570 | return PTR_ERR(set); | |
5571 | ||
e2b750d7 PNA |
5572 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5573 | ||
797d4980 | 5574 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
ba0e4d99 | 5575 | struct netlink_dump_control c = { |
90fd131a | 5576 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
5577 | .dump = nf_tables_dump_set, |
5578 | .done = nf_tables_dump_set_done, | |
d9adf22a | 5579 | .module = THIS_MODULE, |
ba0e4d99 | 5580 | }; |
90fd131a FW |
5581 | struct nft_set_dump_ctx dump_ctx = { |
5582 | .set = set, | |
5583 | .ctx = ctx, | |
5584 | }; | |
ba0e4d99 | 5585 | |
90fd131a | 5586 | c.data = &dump_ctx; |
797d4980 | 5587 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
ba0e4d99 PNA |
5588 | } |
5589 | ||
5590 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
5591 | return -EINVAL; | |
5592 | ||
5593 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
5594 | err = nft_get_set_elem(&ctx, set, attr); | |
b53c1166 PNA |
5595 | if (err < 0) { |
5596 | NL_SET_BAD_ATTR(extack, attr); | |
ba0e4d99 | 5597 | break; |
b53c1166 | 5598 | } |
ba0e4d99 PNA |
5599 | } |
5600 | ||
5601 | return err; | |
5602 | } | |
5603 | ||
25e94a99 PNA |
5604 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
5605 | const struct nft_set *set, | |
5606 | const struct nft_set_elem *elem, | |
6fb721cf | 5607 | int event) |
d60ce62f | 5608 | { |
0854db2a | 5609 | struct nftables_pernet *nft_net; |
128ad332 PNA |
5610 | struct net *net = ctx->net; |
5611 | u32 portid = ctx->portid; | |
d60ce62f | 5612 | struct sk_buff *skb; |
6fb721cf | 5613 | u16 flags = 0; |
d60ce62f AB |
5614 | int err; |
5615 | ||
128ad332 | 5616 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 5617 | return; |
d60ce62f | 5618 | |
d60ce62f AB |
5619 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
5620 | if (skb == NULL) | |
5621 | goto err; | |
5622 | ||
6fb721cf PNA |
5623 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
5624 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
5625 | ||
d60ce62f AB |
5626 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, |
5627 | set, elem); | |
5628 | if (err < 0) { | |
5629 | kfree_skb(skb); | |
5630 | goto err; | |
5631 | } | |
5632 | ||
d59d2f82 | 5633 | nft_net = nft_pernet(net); |
0854db2a | 5634 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 5635 | return; |
d60ce62f | 5636 | err: |
25e94a99 | 5637 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
5638 | } |
5639 | ||
60319eb1 PNA |
5640 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
5641 | int msg_type, | |
5642 | struct nft_set *set) | |
5643 | { | |
5644 | struct nft_trans *trans; | |
5645 | ||
5646 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
5647 | if (trans == NULL) | |
5648 | return NULL; | |
5649 | ||
5650 | nft_trans_elem_set(trans) = set; | |
5651 | return trans; | |
5652 | } | |
5653 | ||
a7fc9368 PNA |
5654 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
5655 | const struct nft_set *set, | |
5656 | const struct nlattr *attr) | |
5657 | { | |
5658 | struct nft_expr *expr; | |
5659 | int err; | |
5660 | ||
5661 | expr = nft_expr_init(ctx, attr); | |
5662 | if (IS_ERR(expr)) | |
5663 | return expr; | |
5664 | ||
5665 | err = -EOPNOTSUPP; | |
a7fc9368 PNA |
5666 | if (expr->ops->type->flags & NFT_EXPR_GC) { |
5667 | if (set->flags & NFT_SET_TIMEOUT) | |
5668 | goto err_set_elem_expr; | |
5669 | if (!set->ops->gc_init) | |
5670 | goto err_set_elem_expr; | |
5671 | set->ops->gc_init(set); | |
5672 | } | |
5673 | ||
5674 | return expr; | |
5675 | ||
5676 | err_set_elem_expr: | |
5677 | nft_expr_destroy(ctx, expr); | |
5678 | return ERR_PTR(err); | |
5679 | } | |
5680 | ||
34aae2c2 PNA |
5681 | static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len) |
5682 | { | |
5683 | len += nft_set_ext_types[id].len; | |
5684 | if (len > tmpl->ext_len[id] || | |
5685 | len > U8_MAX) | |
5686 | return -1; | |
5687 | ||
5688 | return 0; | |
5689 | } | |
5690 | ||
5691 | static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id, | |
5692 | void *to, const void *from, u32 len) | |
5693 | { | |
5694 | if (nft_set_ext_check(tmpl, id, len) < 0) | |
5695 | return -1; | |
5696 | ||
5697 | memcpy(to, from, len); | |
5698 | ||
5699 | return 0; | |
5700 | } | |
5701 | ||
22fe54d5 PM |
5702 | void *nft_set_elem_init(const struct nft_set *set, |
5703 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
5704 | const u32 *key, const u32 *key_end, |
5705 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
5706 | { |
5707 | struct nft_set_ext *ext; | |
5708 | void *elem; | |
5709 | ||
5710 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
5711 | if (elem == NULL) | |
34aae2c2 | 5712 | return ERR_PTR(-ENOMEM); |
fe2811eb PM |
5713 | |
5714 | ext = nft_set_elem_ext(set, elem); | |
5715 | nft_set_ext_init(ext, tmpl); | |
5716 | ||
34aae2c2 PNA |
5717 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
5718 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY, | |
5719 | nft_set_ext_key(ext), key, set->klen) < 0) | |
5720 | goto err_ext_check; | |
5721 | ||
5722 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && | |
5723 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END, | |
5724 | nft_set_ext_key_end(ext), key_end, set->klen) < 0) | |
5725 | goto err_ext_check; | |
5726 | ||
5727 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && | |
5728 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA, | |
5729 | nft_set_ext_data(ext), data, set->dlen) < 0) | |
5730 | goto err_ext_check; | |
5731 | ||
79ebb5bb LGL |
5732 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
5733 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
5734 | if (expiration == 0) | |
5735 | *nft_set_ext_expiration(ext) += timeout; | |
5736 | } | |
c3e1b005 PM |
5737 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
5738 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
5739 | |
5740 | return elem; | |
34aae2c2 PNA |
5741 | |
5742 | err_ext_check: | |
5743 | kfree(elem); | |
5744 | ||
5745 | return ERR_PTR(-EINVAL); | |
fe2811eb PM |
5746 | } |
5747 | ||
563125a7 PNA |
5748 | static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5749 | struct nft_expr *expr) | |
475beb9c PNA |
5750 | { |
5751 | if (expr->ops->destroy_clone) { | |
5752 | expr->ops->destroy_clone(ctx, expr); | |
5753 | module_put(expr->ops->type->owner); | |
5754 | } else { | |
5755 | nf_tables_expr_destroy(ctx, expr); | |
5756 | } | |
5757 | } | |
5758 | ||
563125a7 PNA |
5759 | static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5760 | struct nft_set_elem_expr *elem_expr) | |
5761 | { | |
5762 | struct nft_expr *expr; | |
5763 | u32 size; | |
5764 | ||
5765 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
5766 | __nft_set_elem_expr_destroy(ctx, expr); | |
5767 | } | |
5768 | ||
61f9e292 LZ |
5769 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
5770 | bool destroy_expr) | |
61edafbb PM |
5771 | { |
5772 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
5773 | struct nft_ctx ctx = { |
5774 | .net = read_pnet(&set->net), | |
5775 | .family = set->table->family, | |
5776 | }; | |
61edafbb | 5777 | |
59105446 | 5778 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 5779 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 5780 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
563125a7 | 5781 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c | 5782 | nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext)); |
371ebcbb | 5783 | |
8aeff920 PNA |
5784 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
5785 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
5786 | kfree(elem); |
5787 | } | |
5788 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
5789 | ||
f8bb7889 PNA |
5790 | /* Only called from commit path, nft_setelem_data_deactivate() already deals |
5791 | * with the refcounting from the preparation phase. | |
59105446 | 5792 | */ |
3453c927 PNA |
5793 | static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
5794 | const struct nft_set *set, void *elem) | |
59105446 PNA |
5795 | { |
5796 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
5797 | ||
563125a7 | 5798 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c PNA |
5799 | nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext)); |
5800 | ||
59105446 PNA |
5801 | kfree(elem); |
5802 | } | |
5803 | ||
fca05d4d PNA |
5804 | int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
5805 | struct nft_expr *expr_array[]) | |
8cfd9b0f PNA |
5806 | { |
5807 | struct nft_expr *expr; | |
5808 | int err, i, k; | |
5809 | ||
5810 | for (i = 0; i < set->num_exprs; i++) { | |
42193ffd | 5811 | expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT); |
8cfd9b0f PNA |
5812 | if (!expr) |
5813 | goto err_expr; | |
5814 | ||
5815 | err = nft_expr_clone(expr, set->exprs[i]); | |
5816 | if (err < 0) { | |
c485c35f | 5817 | kfree(expr); |
8cfd9b0f PNA |
5818 | goto err_expr; |
5819 | } | |
5820 | expr_array[i] = expr; | |
5821 | } | |
5822 | ||
5823 | return 0; | |
5824 | ||
5825 | err_expr: | |
161b838e CIK |
5826 | for (k = i - 1; k >= 0; k--) |
5827 | nft_expr_destroy(ctx, expr_array[k]); | |
8cfd9b0f PNA |
5828 | |
5829 | return -ENOMEM; | |
5830 | } | |
5831 | ||
4d8f9065 | 5832 | static int nft_set_elem_expr_setup(struct nft_ctx *ctx, |
34aae2c2 | 5833 | const struct nft_set_ext_tmpl *tmpl, |
4d8f9065 PNA |
5834 | const struct nft_set_ext *ext, |
5835 | struct nft_expr *expr_array[], | |
5836 | u32 num_exprs) | |
563125a7 PNA |
5837 | { |
5838 | struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext); | |
34aae2c2 | 5839 | u32 len = sizeof(struct nft_set_elem_expr); |
4d8f9065 PNA |
5840 | struct nft_expr *expr; |
5841 | int i, err; | |
5842 | ||
34aae2c2 PNA |
5843 | if (num_exprs == 0) |
5844 | return 0; | |
5845 | ||
5846 | for (i = 0; i < num_exprs; i++) | |
5847 | len += expr_array[i]->ops->size; | |
5848 | ||
5849 | if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0) | |
5850 | return -EINVAL; | |
5851 | ||
4d8f9065 PNA |
5852 | for (i = 0; i < num_exprs; i++) { |
5853 | expr = nft_setelem_expr_at(elem_expr, elem_expr->size); | |
5854 | err = nft_expr_clone(expr, expr_array[i]); | |
5855 | if (err < 0) | |
5856 | goto err_elem_expr_setup; | |
5857 | ||
5858 | elem_expr->size += expr_array[i]->ops->size; | |
5859 | nft_expr_destroy(ctx, expr_array[i]); | |
5860 | expr_array[i] = NULL; | |
5861 | } | |
5862 | ||
5863 | return 0; | |
5864 | ||
5865 | err_elem_expr_setup: | |
5866 | for (; i < num_exprs; i++) { | |
5867 | nft_expr_destroy(ctx, expr_array[i]); | |
5868 | expr_array[i] = NULL; | |
5869 | } | |
563125a7 | 5870 | |
4d8f9065 | 5871 | return -ENOMEM; |
563125a7 PNA |
5872 | } |
5873 | ||
aaa31047 PNA |
5874 | struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
5875 | const struct nft_set *set) | |
5876 | { | |
5877 | struct nft_set_elem_catchall *catchall; | |
5878 | u8 genmask = nft_genmask_cur(net); | |
5879 | struct nft_set_ext *ext; | |
5880 | ||
5881 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5882 | ext = nft_set_elem_ext(set, catchall->elem); | |
5883 | if (nft_set_elem_active(ext, genmask) && | |
5884 | !nft_set_elem_expired(ext)) | |
5885 | return ext; | |
5886 | } | |
5887 | ||
5888 | return NULL; | |
5889 | } | |
5890 | EXPORT_SYMBOL_GPL(nft_set_catchall_lookup); | |
5891 | ||
5892 | void *nft_set_catchall_gc(const struct nft_set *set) | |
5893 | { | |
5894 | struct nft_set_elem_catchall *catchall, *next; | |
5895 | struct nft_set_ext *ext; | |
5896 | void *elem = NULL; | |
5897 | ||
5898 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5899 | ext = nft_set_elem_ext(set, catchall->elem); | |
5900 | ||
5901 | if (!nft_set_elem_expired(ext) || | |
5902 | nft_set_elem_mark_busy(ext)) | |
5903 | continue; | |
5904 | ||
5905 | elem = catchall->elem; | |
5906 | list_del_rcu(&catchall->list); | |
5907 | kfree_rcu(catchall, rcu); | |
5908 | break; | |
5909 | } | |
5910 | ||
5911 | return elem; | |
5912 | } | |
5913 | EXPORT_SYMBOL_GPL(nft_set_catchall_gc); | |
5914 | ||
5915 | static int nft_setelem_catchall_insert(const struct net *net, | |
5916 | struct nft_set *set, | |
5917 | const struct nft_set_elem *elem, | |
5918 | struct nft_set_ext **pext) | |
5919 | { | |
5920 | struct nft_set_elem_catchall *catchall; | |
5921 | u8 genmask = nft_genmask_next(net); | |
5922 | struct nft_set_ext *ext; | |
5923 | ||
5924 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5925 | ext = nft_set_elem_ext(set, catchall->elem); | |
5926 | if (nft_set_elem_active(ext, genmask)) { | |
5927 | *pext = ext; | |
5928 | return -EEXIST; | |
5929 | } | |
5930 | } | |
5931 | ||
5932 | catchall = kmalloc(sizeof(*catchall), GFP_KERNEL); | |
5933 | if (!catchall) | |
5934 | return -ENOMEM; | |
5935 | ||
5936 | catchall->elem = elem->priv; | |
5937 | list_add_tail_rcu(&catchall->list, &set->catchall_list); | |
5938 | ||
5939 | return 0; | |
5940 | } | |
5941 | ||
5942 | static int nft_setelem_insert(const struct net *net, | |
5943 | struct nft_set *set, | |
5944 | const struct nft_set_elem *elem, | |
5945 | struct nft_set_ext **ext, unsigned int flags) | |
5946 | { | |
5947 | int ret; | |
5948 | ||
5949 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5950 | ret = nft_setelem_catchall_insert(net, set, elem, ext); | |
5951 | else | |
5952 | ret = set->ops->insert(net, set, elem, ext); | |
5953 | ||
5954 | return ret; | |
5955 | } | |
5956 | ||
5957 | static bool nft_setelem_is_catchall(const struct nft_set *set, | |
5958 | const struct nft_set_elem *elem) | |
5959 | { | |
5960 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5961 | ||
5962 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
5963 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL) | |
5964 | return true; | |
5965 | ||
5966 | return false; | |
5967 | } | |
5968 | ||
5969 | static void nft_setelem_activate(struct net *net, struct nft_set *set, | |
5970 | struct nft_set_elem *elem) | |
5971 | { | |
5972 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5973 | ||
5974 | if (nft_setelem_is_catchall(set, elem)) { | |
5975 | nft_set_elem_change_active(net, set, ext); | |
5976 | nft_set_elem_clear_busy(ext); | |
5977 | } else { | |
5978 | set->ops->activate(net, set, elem); | |
5979 | } | |
5980 | } | |
5981 | ||
5982 | static int nft_setelem_catchall_deactivate(const struct net *net, | |
5983 | struct nft_set *set, | |
5984 | struct nft_set_elem *elem) | |
5985 | { | |
5986 | struct nft_set_elem_catchall *catchall; | |
5987 | struct nft_set_ext *ext; | |
5988 | ||
5989 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5990 | ext = nft_set_elem_ext(set, catchall->elem); | |
5991 | if (!nft_is_active(net, ext) || | |
5992 | nft_set_elem_mark_busy(ext)) | |
5993 | continue; | |
5994 | ||
5995 | kfree(elem->priv); | |
5996 | elem->priv = catchall->elem; | |
5997 | nft_set_elem_change_active(net, set, ext); | |
5998 | return 0; | |
5999 | } | |
6000 | ||
6001 | return -ENOENT; | |
6002 | } | |
6003 | ||
6004 | static int __nft_setelem_deactivate(const struct net *net, | |
6005 | struct nft_set *set, | |
6006 | struct nft_set_elem *elem) | |
6007 | { | |
6008 | void *priv; | |
6009 | ||
6010 | priv = set->ops->deactivate(net, set, elem); | |
6011 | if (!priv) | |
6012 | return -ENOENT; | |
6013 | ||
6014 | kfree(elem->priv); | |
6015 | elem->priv = priv; | |
6016 | set->ndeact++; | |
6017 | ||
6018 | return 0; | |
6019 | } | |
6020 | ||
6021 | static int nft_setelem_deactivate(const struct net *net, | |
6022 | struct nft_set *set, | |
6023 | struct nft_set_elem *elem, u32 flags) | |
6024 | { | |
6025 | int ret; | |
6026 | ||
6027 | if (flags & NFT_SET_ELEM_CATCHALL) | |
6028 | ret = nft_setelem_catchall_deactivate(net, set, elem); | |
6029 | else | |
6030 | ret = __nft_setelem_deactivate(net, set, elem); | |
6031 | ||
6032 | return ret; | |
6033 | } | |
6034 | ||
6035 | static void nft_setelem_catchall_remove(const struct net *net, | |
6036 | const struct nft_set *set, | |
6037 | const struct nft_set_elem *elem) | |
6038 | { | |
6039 | struct nft_set_elem_catchall *catchall, *next; | |
6040 | ||
6041 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
6042 | if (catchall->elem == elem->priv) { | |
6043 | list_del_rcu(&catchall->list); | |
ae089831 | 6044 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
6045 | break; |
6046 | } | |
6047 | } | |
6048 | } | |
6049 | ||
6050 | static void nft_setelem_remove(const struct net *net, | |
6051 | const struct nft_set *set, | |
6052 | const struct nft_set_elem *elem) | |
6053 | { | |
6054 | if (nft_setelem_is_catchall(set, elem)) | |
6055 | nft_setelem_catchall_remove(net, set, elem); | |
6056 | else | |
6057 | set->ops->remove(net, set, elem); | |
6058 | } | |
6059 | ||
88cccd90 PNA |
6060 | static bool nft_setelem_valid_key_end(const struct nft_set *set, |
6061 | struct nlattr **nla, u32 flags) | |
6062 | { | |
6063 | if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) == | |
6064 | (NFT_SET_CONCAT | NFT_SET_INTERVAL)) { | |
6065 | if (flags & NFT_SET_ELEM_INTERVAL_END) | |
6066 | return false; | |
96df8360 PNA |
6067 | |
6068 | if (nla[NFTA_SET_ELEM_KEY_END] && | |
6069 | flags & NFT_SET_ELEM_CATCHALL) | |
88cccd90 PNA |
6070 | return false; |
6071 | } else { | |
6072 | if (nla[NFTA_SET_ELEM_KEY_END]) | |
6073 | return false; | |
6074 | } | |
6075 | ||
6076 | return true; | |
6077 | } | |
6078 | ||
60319eb1 | 6079 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 6080 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 | 6081 | { |
8cfd9b0f | 6082 | struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {}; |
20a69341 | 6083 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
8aeff920 | 6084 | u8 genmask = nft_genmask_next(ctx->net); |
664899e8 | 6085 | u32 flags = 0, size = 0, num_exprs = 0; |
fe2811eb | 6086 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 6087 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
6088 | struct nft_set_elem elem; |
6089 | struct nft_set_binding *binding; | |
8aeff920 | 6090 | struct nft_object *obj = NULL; |
68e942e8 | 6091 | struct nft_userdata *udata; |
20a1452c | 6092 | struct nft_data_desc desc; |
20a69341 | 6093 | enum nft_registers dreg; |
60319eb1 | 6094 | struct nft_trans *trans; |
c3e1b005 | 6095 | u64 timeout; |
79ebb5bb | 6096 | u64 expiration; |
8cfd9b0f | 6097 | int err, i; |
68e942e8 | 6098 | u8 ulen; |
20a69341 | 6099 | |
8cb08174 JB |
6100 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6101 | nft_set_elem_policy, NULL); | |
20a69341 PM |
6102 | if (err < 0) |
6103 | return err; | |
6104 | ||
fe2811eb PM |
6105 | nft_set_ext_prepare(&tmpl); |
6106 | ||
0e9091d6 PNA |
6107 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6108 | if (err < 0) | |
6109 | return err; | |
aaa31047 PNA |
6110 | |
6111 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
6112 | return -EINVAL; | |
6113 | ||
c39ba4de PNA |
6114 | if (flags != 0) { |
6115 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6116 | if (err < 0) | |
6117 | return err; | |
6118 | } | |
20a69341 PM |
6119 | |
6120 | if (set->flags & NFT_SET_MAP) { | |
6121 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 6122 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
6123 | return -EINVAL; |
6124 | } else { | |
6125 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
6126 | return -EINVAL; | |
6127 | } | |
6128 | ||
5a2f3dc3 PNA |
6129 | if (set->flags & NFT_SET_OBJECT) { |
6130 | if (!nla[NFTA_SET_ELEM_OBJREF] && | |
6131 | !(flags & NFT_SET_ELEM_INTERVAL_END)) | |
6132 | return -EINVAL; | |
6133 | } else { | |
6134 | if (nla[NFTA_SET_ELEM_OBJREF]) | |
6135 | return -EINVAL; | |
6136 | } | |
6137 | ||
88cccd90 PNA |
6138 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6139 | return -EINVAL; | |
6140 | ||
bffc124b PNA |
6141 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
6142 | (nla[NFTA_SET_ELEM_DATA] || | |
6143 | nla[NFTA_SET_ELEM_OBJREF] || | |
6144 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
6145 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
6146 | nla[NFTA_SET_ELEM_USERDATA] || | |
48b0ae04 | 6147 | nla[NFTA_SET_ELEM_EXPR] || |
4963674c | 6148 | nla[NFTA_SET_ELEM_KEY_END] || |
48b0ae04 | 6149 | nla[NFTA_SET_ELEM_EXPRESSIONS])) |
bffc124b PNA |
6150 | return -EINVAL; |
6151 | ||
c3e1b005 PM |
6152 | timeout = 0; |
6153 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
6154 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
6155 | return -EINVAL; | |
8e1102d5 FW |
6156 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
6157 | &timeout); | |
6158 | if (err) | |
6159 | return err; | |
33c7aba0 PNA |
6160 | } else if (set->flags & NFT_SET_TIMEOUT && |
6161 | !(flags & NFT_SET_ELEM_INTERVAL_END)) { | |
123b9961 | 6162 | timeout = READ_ONCE(set->timeout); |
c3e1b005 PM |
6163 | } |
6164 | ||
79ebb5bb LGL |
6165 | expiration = 0; |
6166 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
6167 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
6168 | return -EINVAL; | |
6169 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
6170 | &expiration); | |
6171 | if (err) | |
6172 | return err; | |
6173 | } | |
6174 | ||
8cfd9b0f PNA |
6175 | if (nla[NFTA_SET_ELEM_EXPR]) { |
6176 | struct nft_expr *expr; | |
6177 | ||
664899e8 | 6178 | if (set->num_exprs && set->num_exprs != 1) |
8cfd9b0f PNA |
6179 | return -EOPNOTSUPP; |
6180 | ||
40944452 PNA |
6181 | expr = nft_set_elem_expr_alloc(ctx, set, |
6182 | nla[NFTA_SET_ELEM_EXPR]); | |
6183 | if (IS_ERR(expr)) | |
6184 | return PTR_ERR(expr); | |
65038428 | 6185 | |
8cfd9b0f | 6186 | expr_array[0] = expr; |
664899e8 | 6187 | num_exprs = 1; |
65038428 | 6188 | |
664899e8 | 6189 | if (set->num_exprs && set->exprs[0]->ops != expr->ops) { |
8cfd9b0f | 6190 | err = -EOPNOTSUPP; |
65038428 | 6191 | goto err_set_elem_expr; |
8cfd9b0f | 6192 | } |
48b0ae04 PNA |
6193 | } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) { |
6194 | struct nft_expr *expr; | |
6195 | struct nlattr *tmp; | |
6196 | int left; | |
65038428 | 6197 | |
48b0ae04 PNA |
6198 | i = 0; |
6199 | nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) { | |
664899e8 PNA |
6200 | if (i == NFT_SET_EXPR_MAX || |
6201 | (set->num_exprs && set->num_exprs == i)) { | |
48b0ae04 PNA |
6202 | err = -E2BIG; |
6203 | goto err_set_elem_expr; | |
6204 | } | |
6205 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
6206 | err = -EINVAL; | |
6207 | goto err_set_elem_expr; | |
6208 | } | |
6209 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
6210 | if (IS_ERR(expr)) { | |
6211 | err = PTR_ERR(expr); | |
6212 | goto err_set_elem_expr; | |
6213 | } | |
6214 | expr_array[i] = expr; | |
664899e8 | 6215 | num_exprs++; |
48b0ae04 | 6216 | |
664899e8 | 6217 | if (set->num_exprs && expr->ops != set->exprs[i]->ops) { |
48b0ae04 PNA |
6218 | err = -EOPNOTSUPP; |
6219 | goto err_set_elem_expr; | |
6220 | } | |
6221 | i++; | |
6222 | } | |
664899e8 | 6223 | if (set->num_exprs && set->num_exprs != i) { |
48b0ae04 | 6224 | err = -EOPNOTSUPP; |
65038428 | 6225 | goto err_set_elem_expr; |
48b0ae04 | 6226 | } |
33c7aba0 PNA |
6227 | } else if (set->num_exprs > 0 && |
6228 | !(flags & NFT_SET_ELEM_INTERVAL_END)) { | |
8cfd9b0f PNA |
6229 | err = nft_set_elem_expr_clone(ctx, set, expr_array); |
6230 | if (err < 0) | |
6231 | goto err_set_elem_expr_clone; | |
664899e8 PNA |
6232 | |
6233 | num_exprs = set->num_exprs; | |
40944452 PNA |
6234 | } |
6235 | ||
aaa31047 PNA |
6236 | if (nla[NFTA_SET_ELEM_KEY]) { |
6237 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6238 | nla[NFTA_SET_ELEM_KEY]); | |
6239 | if (err < 0) | |
6240 | goto err_set_elem_expr; | |
20a69341 | 6241 | |
c39ba4de PNA |
6242 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6243 | if (err < 0) | |
6244 | goto err_parse_key; | |
aaa31047 | 6245 | } |
7b225d0b PNA |
6246 | |
6247 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
6248 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6249 | nla[NFTA_SET_ELEM_KEY_END]); | |
6250 | if (err < 0) | |
6251 | goto err_parse_key; | |
6252 | ||
c39ba4de PNA |
6253 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6254 | if (err < 0) | |
6255 | goto err_parse_key_end; | |
7b225d0b PNA |
6256 | } |
6257 | ||
c3e1b005 | 6258 | if (timeout > 0) { |
c39ba4de PNA |
6259 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); |
6260 | if (err < 0) | |
6261 | goto err_parse_key_end; | |
6262 | ||
123b9961 | 6263 | if (timeout != READ_ONCE(set->timeout)) { |
c39ba4de PNA |
6264 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); |
6265 | if (err < 0) | |
6266 | goto err_parse_key_end; | |
6267 | } | |
c3e1b005 | 6268 | } |
fe2811eb | 6269 | |
664899e8 PNA |
6270 | if (num_exprs) { |
6271 | for (i = 0; i < num_exprs; i++) | |
563125a7 PNA |
6272 | size += expr_array[i]->ops->size; |
6273 | ||
c39ba4de PNA |
6274 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS, |
6275 | sizeof(struct nft_set_elem_expr) + size); | |
6276 | if (err < 0) | |
6277 | goto err_parse_key_end; | |
563125a7 | 6278 | } |
40944452 | 6279 | |
8aeff920 | 6280 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
4d44175a FW |
6281 | obj = nft_obj_lookup(ctx->net, ctx->table, |
6282 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 6283 | set->objtype, genmask); |
8aeff920 PNA |
6284 | if (IS_ERR(obj)) { |
6285 | err = PTR_ERR(obj); | |
7b225d0b | 6286 | goto err_parse_key_end; |
8aeff920 | 6287 | } |
c39ba4de PNA |
6288 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); |
6289 | if (err < 0) | |
6290 | goto err_parse_key_end; | |
8aeff920 PNA |
6291 | } |
6292 | ||
20a69341 | 6293 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
fdb9c405 PNA |
6294 | err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val, |
6295 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 | 6296 | if (err < 0) |
7b225d0b | 6297 | goto err_parse_key_end; |
20a69341 | 6298 | |
20a69341 PM |
6299 | dreg = nft_type_to_reg(set->dtype); |
6300 | list_for_each_entry(binding, &set->bindings, list) { | |
6301 | struct nft_ctx bind_ctx = { | |
58c78e10 | 6302 | .net = ctx->net, |
36596dad | 6303 | .family = ctx->family, |
20a69341 | 6304 | .table = ctx->table, |
7c95f6d8 | 6305 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
6306 | }; |
6307 | ||
11113e19 PM |
6308 | if (!(binding->flags & NFT_SET_MAP)) |
6309 | continue; | |
6310 | ||
1ec10212 | 6311 | err = nft_validate_register_store(&bind_ctx, dreg, |
fdb9c405 | 6312 | &elem.data.val, |
20a1452c | 6313 | desc.type, desc.len); |
20a69341 | 6314 | if (err < 0) |
7b225d0b | 6315 | goto err_parse_data; |
a654de8f | 6316 | |
20a1452c | 6317 | if (desc.type == NFT_DATA_VERDICT && |
fdb9c405 PNA |
6318 | (elem.data.val.verdict.code == NFT_GOTO || |
6319 | elem.data.val.verdict.code == NFT_JUMP)) | |
a654de8f PNA |
6320 | nft_validate_state_update(ctx->net, |
6321 | NFT_VALIDATE_NEED); | |
20a69341 | 6322 | } |
fe2811eb | 6323 | |
c39ba4de PNA |
6324 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
6325 | if (err < 0) | |
6326 | goto err_parse_data; | |
20a69341 PM |
6327 | } |
6328 | ||
68e942e8 PM |
6329 | /* The full maximum length of userdata can exceed the maximum |
6330 | * offset value (U8_MAX) for following extensions, therefor it | |
6331 | * must be the last extension added. | |
6332 | */ | |
6333 | ulen = 0; | |
6334 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
6335 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
c39ba4de PNA |
6336 | if (ulen > 0) { |
6337 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
6338 | ulen); | |
6339 | if (err < 0) | |
6340 | goto err_parse_data; | |
6341 | } | |
68e942e8 PM |
6342 | } |
6343 | ||
7b225d0b | 6344 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
fdb9c405 | 6345 | elem.key_end.val.data, elem.data.val.data, |
33758c89 | 6346 | timeout, expiration, GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6347 | if (IS_ERR(elem.priv)) { |
6348 | err = PTR_ERR(elem.priv); | |
7b225d0b | 6349 | goto err_parse_data; |
34aae2c2 | 6350 | } |
fe2811eb PM |
6351 | |
6352 | ext = nft_set_elem_ext(set, elem.priv); | |
6353 | if (flags) | |
6354 | *nft_set_ext_flags(ext) = flags; | |
34aae2c2 | 6355 | |
68e942e8 | 6356 | if (ulen > 0) { |
34aae2c2 PNA |
6357 | if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) { |
6358 | err = -EINVAL; | |
6359 | goto err_elem_userdata; | |
6360 | } | |
68e942e8 PM |
6361 | udata = nft_set_ext_userdata(ext); |
6362 | udata->len = ulen - 1; | |
6363 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
6364 | } | |
8aeff920 PNA |
6365 | if (obj) { |
6366 | *nft_set_ext_obj(ext) = obj; | |
6367 | obj->use++; | |
6368 | } | |
34aae2c2 | 6369 | err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs); |
4d8f9065 | 6370 | if (err < 0) |
34aae2c2 | 6371 | goto err_elem_free; |
fe2811eb | 6372 | |
60319eb1 | 6373 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
4d8f9065 PNA |
6374 | if (trans == NULL) { |
6375 | err = -ENOMEM; | |
34aae2c2 | 6376 | goto err_elem_free; |
4d8f9065 | 6377 | } |
60319eb1 | 6378 | |
69086658 | 6379 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
aaa31047 PNA |
6380 | |
6381 | err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags); | |
c016c7e4 PNA |
6382 | if (err) { |
6383 | if (err == -EEXIST) { | |
9744a6fc PNA |
6384 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
6385 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
6386 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
77a92189 | 6387 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) |
7b225d0b | 6388 | goto err_element_clash; |
8aeff920 PNA |
6389 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
6390 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
6391 | memcmp(nft_set_ext_data(ext), | |
6392 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
6393 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
6394 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
6395 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
77a92189 | 6396 | goto err_element_clash; |
c016c7e4 PNA |
6397 | else if (!(nlmsg_flags & NLM_F_EXCL)) |
6398 | err = 0; | |
8c2d45b2 PNA |
6399 | } else if (err == -ENOTEMPTY) { |
6400 | /* ENOTEMPTY reports overlapping between this element | |
6401 | * and an existing one. | |
6402 | */ | |
6403 | err = -EEXIST; | |
c016c7e4 | 6404 | } |
7b225d0b | 6405 | goto err_element_clash; |
c016c7e4 | 6406 | } |
20a69341 | 6407 | |
aaa31047 | 6408 | if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size && |
35d0ac90 PNA |
6409 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { |
6410 | err = -ENFILE; | |
7b225d0b | 6411 | goto err_set_full; |
35d0ac90 PNA |
6412 | } |
6413 | ||
60319eb1 | 6414 | nft_trans_elem(trans) = elem; |
0854db2a | 6415 | nft_trans_commit_list_add_tail(ctx->net, trans); |
20a69341 PM |
6416 | return 0; |
6417 | ||
7b225d0b | 6418 | err_set_full: |
aaa31047 | 6419 | nft_setelem_remove(ctx->net, set, &elem); |
7b225d0b | 6420 | err_element_clash: |
60319eb1 | 6421 | kfree(trans); |
34aae2c2 | 6422 | err_elem_free: |
b91d9036 TY |
6423 | if (obj) |
6424 | obj->use--; | |
34aae2c2 | 6425 | err_elem_userdata: |
475beb9c | 6426 | nf_tables_set_elem_destroy(ctx, set, elem.priv); |
7b225d0b | 6427 | err_parse_data: |
20a69341 | 6428 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
fdb9c405 | 6429 | nft_data_release(&elem.data.val, desc.type); |
7b225d0b PNA |
6430 | err_parse_key_end: |
6431 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
6432 | err_parse_key: | |
20a1452c | 6433 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
40944452 | 6434 | err_set_elem_expr: |
664899e8 | 6435 | for (i = 0; i < num_exprs && expr_array[i]; i++) |
8cfd9b0f PNA |
6436 | nft_expr_destroy(ctx, expr_array[i]); |
6437 | err_set_elem_expr_clone: | |
20a69341 PM |
6438 | return err; |
6439 | } | |
6440 | ||
7dab8ee3 PNA |
6441 | static int nf_tables_newsetelem(struct sk_buff *skb, |
6442 | const struct nfnl_info *info, | |
6443 | const struct nlattr * const nla[]) | |
20a69341 | 6444 | { |
7dab8ee3 PNA |
6445 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
6446 | struct netlink_ext_ack *extack = info->extack; | |
6447 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6448 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6449 | struct net *net = info->net; |
20a69341 | 6450 | const struct nlattr *attr; |
e2b750d7 | 6451 | struct nft_table *table; |
20a69341 PM |
6452 | struct nft_set *set; |
6453 | struct nft_ctx ctx; | |
a654de8f | 6454 | int rem, err; |
20a69341 | 6455 | |
7d5570ca PNA |
6456 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
6457 | return -EINVAL; | |
6458 | ||
e2b750d7 PNA |
6459 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6460 | genmask, NETLINK_CB(skb).portid); | |
6461 | if (IS_ERR(table)) { | |
6462 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6463 | return PTR_ERR(table); | |
6464 | } | |
20a69341 | 6465 | |
e2b750d7 | 6466 | set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET], |
a3073c17 PNA |
6467 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); |
6468 | if (IS_ERR(set)) | |
6469 | return PTR_ERR(set); | |
958bee14 | 6470 | |
20a69341 PM |
6471 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
6472 | return -EBUSY; | |
6473 | ||
e2b750d7 PNA |
6474 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6475 | ||
20a69341 | 6476 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7dab8ee3 | 6477 | err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags); |
b53c1166 PNA |
6478 | if (err < 0) { |
6479 | NL_SET_BAD_ATTR(extack, attr); | |
a654de8f | 6480 | return err; |
b53c1166 | 6481 | } |
20a69341 | 6482 | } |
a654de8f | 6483 | |
0854db2a | 6484 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
e2b750d7 | 6485 | return nft_table_validate(net, table); |
a654de8f PNA |
6486 | |
6487 | return 0; | |
20a69341 PM |
6488 | } |
6489 | ||
59105446 PNA |
6490 | /** |
6491 | * nft_data_hold - hold a nft_data item | |
6492 | * | |
6493 | * @data: struct nft_data to release | |
6494 | * @type: type of data | |
6495 | * | |
6496 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
6497 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
6498 | * NFT_GOTO verdicts. This function must be called on active data objects | |
6499 | * from the second phase of the commit protocol. | |
6500 | */ | |
bb7b40ae | 6501 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 | 6502 | { |
d0e2c7de PNA |
6503 | struct nft_chain *chain; |
6504 | struct nft_rule *rule; | |
6505 | ||
59105446 PNA |
6506 | if (type == NFT_DATA_VERDICT) { |
6507 | switch (data->verdict.code) { | |
6508 | case NFT_JUMP: | |
6509 | case NFT_GOTO: | |
d0e2c7de PNA |
6510 | chain = data->verdict.chain; |
6511 | chain->use++; | |
6512 | ||
6513 | if (!nft_chain_is_bound(chain)) | |
6514 | break; | |
6515 | ||
6516 | chain->table->use++; | |
6517 | list_for_each_entry(rule, &chain->rules, list) | |
6518 | chain->use++; | |
6519 | ||
6520 | nft_chain_add(chain->table, chain); | |
59105446 PNA |
6521 | break; |
6522 | } | |
6523 | } | |
6524 | } | |
6525 | ||
f8bb7889 PNA |
6526 | static void nft_setelem_data_activate(const struct net *net, |
6527 | const struct nft_set *set, | |
6528 | struct nft_set_elem *elem) | |
59105446 PNA |
6529 | { |
6530 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6531 | ||
6532 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6533 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
6534 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6535 | (*nft_set_ext_obj(ext))->use++; | |
6536 | } | |
6537 | ||
f8bb7889 PNA |
6538 | static void nft_setelem_data_deactivate(const struct net *net, |
6539 | const struct nft_set *set, | |
6540 | struct nft_set_elem *elem) | |
59105446 PNA |
6541 | { |
6542 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6543 | ||
6544 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6545 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
6546 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6547 | (*nft_set_ext_obj(ext))->use--; | |
6548 | } | |
6549 | ||
60319eb1 | 6550 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
6551 | const struct nlattr *attr) |
6552 | { | |
6553 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 6554 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 6555 | struct nft_set_elem elem; |
3971ca14 | 6556 | struct nft_set_ext *ext; |
60319eb1 | 6557 | struct nft_trans *trans; |
3971ca14 | 6558 | u32 flags = 0; |
20a69341 PM |
6559 | int err; |
6560 | ||
8cb08174 JB |
6561 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6562 | nft_set_elem_policy, NULL); | |
20a69341 | 6563 | if (err < 0) |
20a1452c | 6564 | return err; |
20a69341 | 6565 | |
aaa31047 PNA |
6566 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6567 | if (err < 0) | |
6568 | return err; | |
6569 | ||
6570 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
20a1452c | 6571 | return -EINVAL; |
20a69341 | 6572 | |
88cccd90 PNA |
6573 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6574 | return -EINVAL; | |
6575 | ||
3971ca14 PNA |
6576 | nft_set_ext_prepare(&tmpl); |
6577 | ||
c39ba4de PNA |
6578 | if (flags != 0) { |
6579 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6580 | if (err < 0) | |
6581 | return err; | |
6582 | } | |
3971ca14 | 6583 | |
aaa31047 PNA |
6584 | if (nla[NFTA_SET_ELEM_KEY]) { |
6585 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6586 | nla[NFTA_SET_ELEM_KEY]); | |
6587 | if (err < 0) | |
6588 | return err; | |
20a69341 | 6589 | |
c39ba4de PNA |
6590 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6591 | if (err < 0) | |
6592 | goto fail_elem; | |
aaa31047 | 6593 | } |
3971ca14 | 6594 | |
7b225d0b PNA |
6595 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
6596 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6597 | nla[NFTA_SET_ELEM_KEY_END]); | |
6598 | if (err < 0) | |
c39ba4de | 6599 | goto fail_elem; |
7b225d0b | 6600 | |
c39ba4de PNA |
6601 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6602 | if (err < 0) | |
6603 | goto fail_elem_key_end; | |
7b225d0b PNA |
6604 | } |
6605 | ||
3971ca14 | 6606 | err = -ENOMEM; |
7b225d0b PNA |
6607 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
6608 | elem.key_end.val.data, NULL, 0, 0, | |
33758c89 | 6609 | GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6610 | if (IS_ERR(elem.priv)) { |
6611 | err = PTR_ERR(elem.priv); | |
c39ba4de | 6612 | goto fail_elem_key_end; |
34aae2c2 | 6613 | } |
3971ca14 PNA |
6614 | |
6615 | ext = nft_set_elem_ext(set, elem.priv); | |
6616 | if (flags) | |
6617 | *nft_set_ext_flags(ext) = flags; | |
6618 | ||
60319eb1 | 6619 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
6620 | if (trans == NULL) |
6621 | goto fail_trans; | |
20a69341 | 6622 | |
aaa31047 PNA |
6623 | err = nft_setelem_deactivate(ctx->net, set, &elem, flags); |
6624 | if (err < 0) | |
20a1452c | 6625 | goto fail_ops; |
cc02e457 | 6626 | |
f8bb7889 | 6627 | nft_setelem_data_deactivate(ctx->net, set, &elem); |
59105446 | 6628 | |
60319eb1 | 6629 | nft_trans_elem(trans) = elem; |
0854db2a | 6630 | nft_trans_commit_list_add_tail(ctx->net, trans); |
0dc13625 | 6631 | return 0; |
cc02e457 | 6632 | |
20a1452c | 6633 | fail_ops: |
cc02e457 | 6634 | kfree(trans); |
20a1452c | 6635 | fail_trans: |
3971ca14 | 6636 | kfree(elem.priv); |
c39ba4de PNA |
6637 | fail_elem_key_end: |
6638 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
20a1452c PNA |
6639 | fail_elem: |
6640 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
6641 | return err; |
6642 | } | |
6643 | ||
e6ba7cb6 PNA |
6644 | static int nft_setelem_flush(const struct nft_ctx *ctx, |
6645 | struct nft_set *set, | |
6646 | const struct nft_set_iter *iter, | |
6647 | struct nft_set_elem *elem) | |
8411b644 PNA |
6648 | { |
6649 | struct nft_trans *trans; | |
6650 | int err; | |
6651 | ||
6652 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6653 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
6654 | if (!trans) | |
6655 | return -ENOMEM; | |
6656 | ||
1ba1c414 | 6657 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
6658 | err = -ENOENT; |
6659 | goto err1; | |
6660 | } | |
b2c11e4b | 6661 | set->ndeact++; |
8411b644 | 6662 | |
f8bb7889 | 6663 | nft_setelem_data_deactivate(ctx->net, set, elem); |
de70185d PNA |
6664 | nft_trans_elem_set(trans) = set; |
6665 | nft_trans_elem(trans) = *elem; | |
0854db2a | 6666 | nft_trans_commit_list_add_tail(ctx->net, trans); |
8411b644 PNA |
6667 | |
6668 | return 0; | |
6669 | err1: | |
6670 | kfree(trans); | |
6671 | return err; | |
6672 | } | |
6673 | ||
aaa31047 PNA |
6674 | static int __nft_set_catchall_flush(const struct nft_ctx *ctx, |
6675 | struct nft_set *set, | |
6676 | struct nft_set_elem *elem) | |
6677 | { | |
6678 | struct nft_trans *trans; | |
6679 | ||
6680 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6681 | sizeof(struct nft_trans_elem), GFP_KERNEL); | |
6682 | if (!trans) | |
6683 | return -ENOMEM; | |
6684 | ||
6685 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
6686 | nft_trans_elem_set(trans) = set; | |
6687 | nft_trans_elem(trans) = *elem; | |
6688 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
6689 | ||
6690 | return 0; | |
6691 | } | |
6692 | ||
6693 | static int nft_set_catchall_flush(const struct nft_ctx *ctx, | |
6694 | struct nft_set *set) | |
6695 | { | |
6696 | u8 genmask = nft_genmask_next(ctx->net); | |
6697 | struct nft_set_elem_catchall *catchall; | |
6698 | struct nft_set_elem elem; | |
6699 | struct nft_set_ext *ext; | |
6700 | int ret = 0; | |
6701 | ||
6702 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
6703 | ext = nft_set_elem_ext(set, catchall->elem); | |
6704 | if (!nft_set_elem_active(ext, genmask) || | |
6705 | nft_set_elem_mark_busy(ext)) | |
6706 | continue; | |
6707 | ||
6708 | elem.priv = catchall->elem; | |
6709 | ret = __nft_set_catchall_flush(ctx, set, &elem); | |
6710 | if (ret < 0) | |
6711 | break; | |
6712 | } | |
6713 | ||
6714 | return ret; | |
6715 | } | |
6716 | ||
e6ba7cb6 PNA |
6717 | static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) |
6718 | { | |
6719 | struct nft_set_iter iter = { | |
6720 | .genmask = genmask, | |
6721 | .fn = nft_setelem_flush, | |
6722 | }; | |
6723 | ||
6724 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
6725 | if (!iter.err) |
6726 | iter.err = nft_set_catchall_flush(ctx, set); | |
e6ba7cb6 PNA |
6727 | |
6728 | return iter.err; | |
6729 | } | |
6730 | ||
7dab8ee3 PNA |
6731 | static int nf_tables_delsetelem(struct sk_buff *skb, |
6732 | const struct nfnl_info *info, | |
6733 | const struct nlattr * const nla[]) | |
20a69341 | 6734 | { |
7dab8ee3 PNA |
6735 | struct netlink_ext_ack *extack = info->extack; |
6736 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6737 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6738 | struct net *net = info->net; |
20a69341 | 6739 | const struct nlattr *attr; |
e2b750d7 | 6740 | struct nft_table *table; |
20a69341 PM |
6741 | struct nft_set *set; |
6742 | struct nft_ctx ctx; | |
60319eb1 | 6743 | int rem, err = 0; |
20a69341 | 6744 | |
e2b750d7 PNA |
6745 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6746 | genmask, NETLINK_CB(skb).portid); | |
6747 | if (IS_ERR(table)) { | |
6748 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6749 | return PTR_ERR(table); | |
6750 | } | |
20a69341 | 6751 | |
e2b750d7 | 6752 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
6753 | if (IS_ERR(set)) |
6754 | return PTR_ERR(set); | |
6755 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
6756 | return -EBUSY; | |
6757 | ||
e2b750d7 PNA |
6758 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6759 | ||
e6ba7cb6 PNA |
6760 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) |
6761 | return nft_set_flush(&ctx, set, genmask); | |
8411b644 | 6762 | |
20a69341 PM |
6763 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
6764 | err = nft_del_setelem(&ctx, set, attr); | |
f80a612d FFM |
6765 | if (err == -ENOENT && |
6766 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSETELEM) | |
6767 | continue; | |
6768 | ||
b53c1166 PNA |
6769 | if (err < 0) { |
6770 | NL_SET_BAD_ATTR(extack, attr); | |
60319eb1 | 6771 | break; |
b53c1166 | 6772 | } |
20a69341 | 6773 | } |
60319eb1 | 6774 | return err; |
20a69341 PM |
6775 | } |
6776 | ||
cfed7e1b PM |
6777 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
6778 | { | |
6779 | struct nft_set_gc_batch *gcb; | |
6780 | unsigned int i; | |
6781 | ||
6782 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
6783 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 6784 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
6785 | kfree(gcb); |
6786 | } | |
cfed7e1b PM |
6787 | |
6788 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
6789 | gfp_t gfp) | |
6790 | { | |
6791 | struct nft_set_gc_batch *gcb; | |
6792 | ||
6793 | gcb = kzalloc(sizeof(*gcb), gfp); | |
6794 | if (gcb == NULL) | |
6795 | return gcb; | |
6796 | gcb->head.set = set; | |
6797 | return gcb; | |
6798 | } | |
cfed7e1b | 6799 | |
e5009240 PNA |
6800 | /* |
6801 | * Stateful objects | |
6802 | */ | |
6803 | ||
6804 | /** | |
6805 | * nft_register_obj- register nf_tables stateful object type | |
3db86c39 | 6806 | * @obj_type: object type |
e5009240 PNA |
6807 | * |
6808 | * Registers the object type for use with nf_tables. Returns zero on | |
6809 | * success or a negative errno code otherwise. | |
6810 | */ | |
6811 | int nft_register_obj(struct nft_object_type *obj_type) | |
6812 | { | |
6813 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
6814 | return -EINVAL; | |
6815 | ||
6816 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6817 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
6818 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6819 | return 0; | |
6820 | } | |
6821 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
6822 | ||
6823 | /** | |
6824 | * nft_unregister_obj - unregister nf_tables object type | |
3db86c39 | 6825 | * @obj_type: object type |
e5009240 PNA |
6826 | * |
6827 | * Unregisters the object type for use with nf_tables. | |
6828 | */ | |
6829 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
6830 | { | |
6831 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6832 | list_del_rcu(&obj_type->list); | |
6833 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6834 | } | |
6835 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
6836 | ||
4d44175a FW |
6837 | struct nft_object *nft_obj_lookup(const struct net *net, |
6838 | const struct nft_table *table, | |
cac20fcd PNA |
6839 | const struct nlattr *nla, u32 objtype, |
6840 | u8 genmask) | |
e5009240 | 6841 | { |
4d44175a FW |
6842 | struct nft_object_hash_key k = { .table = table }; |
6843 | char search[NFT_OBJ_MAXNAMELEN]; | |
6844 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
6845 | struct nft_object *obj; |
6846 | ||
872f6903 | 6847 | nla_strscpy(search, nla, sizeof(search)); |
4d44175a FW |
6848 | k.name = search; |
6849 | ||
6850 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
6851 | !lockdep_commit_lock_is_held(net)); | |
6852 | ||
6853 | rcu_read_lock(); | |
6854 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
6855 | if (!list) | |
6856 | goto out; | |
6857 | ||
6858 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
6859 | if (objtype == obj->ops->type->type && | |
6860 | nft_active_genmask(obj, genmask)) { | |
6861 | rcu_read_unlock(); | |
e5009240 | 6862 | return obj; |
4d44175a | 6863 | } |
e5009240 | 6864 | } |
4d44175a FW |
6865 | out: |
6866 | rcu_read_unlock(); | |
e5009240 PNA |
6867 | return ERR_PTR(-ENOENT); |
6868 | } | |
cac20fcd | 6869 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 6870 | |
cac20fcd PNA |
6871 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
6872 | const struct nlattr *nla, | |
6873 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
6874 | { |
6875 | struct nft_object *obj; | |
6876 | ||
6877 | list_for_each_entry(obj, &table->objects, list) { | |
6878 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
6879 | objtype == obj->ops->type->type && | |
6880 | nft_active_genmask(obj, genmask)) | |
6881 | return obj; | |
6882 | } | |
6883 | return ERR_PTR(-ENOENT); | |
6884 | } | |
6885 | ||
e5009240 | 6886 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
6887 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
6888 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
6889 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
6890 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
6891 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
6892 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 6893 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
b131c964 JGG |
6894 | [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY, |
6895 | .len = NFT_USERDATA_MAXLEN }, | |
e5009240 PNA |
6896 | }; |
6897 | ||
84fba055 FW |
6898 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
6899 | const struct nft_object_type *type, | |
e5009240 PNA |
6900 | const struct nlattr *attr) |
6901 | { | |
5b4c6e38 | 6902 | struct nlattr **tb; |
dfc46034 | 6903 | const struct nft_object_ops *ops; |
e5009240 | 6904 | struct nft_object *obj; |
5b4c6e38 GS |
6905 | int err = -ENOMEM; |
6906 | ||
6907 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
6908 | if (!tb) | |
6909 | goto err1; | |
e5009240 PNA |
6910 | |
6911 | if (attr) { | |
8cb08174 JB |
6912 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
6913 | type->policy, NULL); | |
e5009240 | 6914 | if (err < 0) |
5b4c6e38 | 6915 | goto err2; |
e5009240 PNA |
6916 | } else { |
6917 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
6918 | } | |
6919 | ||
dfc46034 PBG |
6920 | if (type->select_ops) { |
6921 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
6922 | if (IS_ERR(ops)) { | |
6923 | err = PTR_ERR(ops); | |
5b4c6e38 | 6924 | goto err2; |
dfc46034 PBG |
6925 | } |
6926 | } else { | |
6927 | ops = type->ops; | |
6928 | } | |
6929 | ||
e5009240 | 6930 | err = -ENOMEM; |
33758c89 | 6931 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT); |
5b4c6e38 GS |
6932 | if (!obj) |
6933 | goto err2; | |
e5009240 | 6934 | |
dfc46034 | 6935 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 6936 | if (err < 0) |
5b4c6e38 | 6937 | goto err3; |
e5009240 | 6938 | |
dfc46034 PBG |
6939 | obj->ops = ops; |
6940 | ||
5b4c6e38 | 6941 | kfree(tb); |
e5009240 | 6942 | return obj; |
5b4c6e38 | 6943 | err3: |
e5009240 | 6944 | kfree(obj); |
5b4c6e38 GS |
6945 | err2: |
6946 | kfree(tb); | |
e5009240 PNA |
6947 | err1: |
6948 | return ERR_PTR(err); | |
6949 | } | |
6950 | ||
6951 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 6952 | struct nft_object *obj, bool reset) |
e5009240 PNA |
6953 | { |
6954 | struct nlattr *nest; | |
6955 | ||
ae0be8de | 6956 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
6957 | if (!nest) |
6958 | goto nla_put_failure; | |
dfc46034 | 6959 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
6960 | goto nla_put_failure; |
6961 | nla_nest_end(skb, nest); | |
6962 | return 0; | |
6963 | ||
6964 | nla_put_failure: | |
6965 | return -1; | |
6966 | } | |
6967 | ||
6968 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
6969 | { | |
6970 | const struct nft_object_type *type; | |
6971 | ||
6972 | list_for_each_entry(type, &nf_tables_objects, list) { | |
6973 | if (objtype == type->type) | |
6974 | return type; | |
6975 | } | |
6976 | return NULL; | |
6977 | } | |
6978 | ||
452238e8 FW |
6979 | static const struct nft_object_type * |
6980 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
6981 | { |
6982 | const struct nft_object_type *type; | |
6983 | ||
6984 | type = __nft_obj_type_get(objtype); | |
6985 | if (type != NULL && try_module_get(type->owner)) | |
6986 | return type; | |
6987 | ||
f102d66b | 6988 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
6989 | #ifdef CONFIG_MODULES |
6990 | if (type == NULL) { | |
eb014de4 | 6991 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
6992 | return ERR_PTR(-EAGAIN); |
6993 | } | |
6994 | #endif | |
6995 | return ERR_PTR(-ENOENT); | |
6996 | } | |
6997 | ||
d62d0ba9 FFM |
6998 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
6999 | const struct nft_object_type *type, | |
7000 | const struct nlattr *attr, | |
7001 | struct nft_object *obj) | |
7002 | { | |
7003 | struct nft_object *newobj; | |
7004 | struct nft_trans *trans; | |
dad3bdee FW |
7005 | int err = -ENOMEM; |
7006 | ||
7007 | if (!try_module_get(type->owner)) | |
7008 | return -ENOENT; | |
d62d0ba9 FFM |
7009 | |
7010 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
7011 | sizeof(struct nft_trans_obj)); | |
7012 | if (!trans) | |
dad3bdee | 7013 | goto err_trans; |
d62d0ba9 FFM |
7014 | |
7015 | newobj = nft_obj_init(ctx, type, attr); | |
7016 | if (IS_ERR(newobj)) { | |
7017 | err = PTR_ERR(newobj); | |
b74ae961 | 7018 | goto err_free_trans; |
d62d0ba9 FFM |
7019 | } |
7020 | ||
7021 | nft_trans_obj(trans) = obj; | |
7022 | nft_trans_obj_update(trans) = true; | |
7023 | nft_trans_obj_newobj(trans) = newobj; | |
0854db2a | 7024 | nft_trans_commit_list_add_tail(ctx->net, trans); |
d62d0ba9 FFM |
7025 | |
7026 | return 0; | |
b74ae961 DC |
7027 | |
7028 | err_free_trans: | |
d62d0ba9 | 7029 | kfree(trans); |
dad3bdee FW |
7030 | err_trans: |
7031 | module_put(type->owner); | |
d62d0ba9 FFM |
7032 | return err; |
7033 | } | |
7034 | ||
7dab8ee3 PNA |
7035 | static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info, |
7036 | const struct nlattr * const nla[]) | |
e5009240 | 7037 | { |
7dab8ee3 PNA |
7038 | struct netlink_ext_ack *extack = info->extack; |
7039 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7040 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7041 | const struct nft_object_type *type; |
7dab8ee3 | 7042 | struct net *net = info->net; |
e5009240 PNA |
7043 | struct nft_table *table; |
7044 | struct nft_object *obj; | |
7045 | struct nft_ctx ctx; | |
7046 | u32 objtype; | |
7047 | int err; | |
7048 | ||
7049 | if (!nla[NFTA_OBJ_TYPE] || | |
7050 | !nla[NFTA_OBJ_NAME] || | |
7051 | !nla[NFTA_OBJ_DATA]) | |
7052 | return -EINVAL; | |
7053 | ||
6001a930 PNA |
7054 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7055 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7056 | if (IS_ERR(table)) { |
7057 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7058 | return PTR_ERR(table); |
36dd1bcc | 7059 | } |
e5009240 PNA |
7060 | |
7061 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7062 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
7063 | if (IS_ERR(obj)) { |
7064 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
7065 | if (err != -ENOENT) { |
7066 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7067 | return err; |
36dd1bcc | 7068 | } |
1a28ad74 | 7069 | } else { |
7dab8ee3 | 7070 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 7071 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); |
e5009240 | 7072 | return -EEXIST; |
36dd1bcc | 7073 | } |
7dab8ee3 | 7074 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
d62d0ba9 FFM |
7075 | return -EOPNOTSUPP; |
7076 | ||
fd57d0cb | 7077 | type = __nft_obj_type_get(objtype); |
dac7f50a AT |
7078 | if (WARN_ON_ONCE(!type)) |
7079 | return -ENOENT; | |
7080 | ||
7dab8ee3 | 7081 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
d62d0ba9 FFM |
7082 | |
7083 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
7084 | } |
7085 | ||
7dab8ee3 | 7086 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 | 7087 | |
452238e8 | 7088 | type = nft_obj_type_get(net, objtype); |
e5009240 PNA |
7089 | if (IS_ERR(type)) |
7090 | return PTR_ERR(type); | |
7091 | ||
84fba055 | 7092 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
7093 | if (IS_ERR(obj)) { |
7094 | err = PTR_ERR(obj); | |
b131c964 | 7095 | goto err_init; |
e5009240 | 7096 | } |
d152159b | 7097 | obj->key.table = table; |
3ecbfd65 HS |
7098 | obj->handle = nf_tables_alloc_handle(table); |
7099 | ||
33758c89 | 7100 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT); |
d152159b | 7101 | if (!obj->key.name) { |
61509575 | 7102 | err = -ENOMEM; |
b131c964 JGG |
7103 | goto err_strdup; |
7104 | } | |
7105 | ||
7106 | if (nla[NFTA_OBJ_USERDATA]) { | |
af0acf22 | 7107 | obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL_ACCOUNT); |
b131c964 JGG |
7108 | if (obj->udata == NULL) |
7109 | goto err_userdata; | |
7110 | ||
85db827a | 7111 | obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]); |
61509575 | 7112 | } |
e5009240 PNA |
7113 | |
7114 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
7115 | if (err < 0) | |
b131c964 | 7116 | goto err_trans; |
e5009240 | 7117 | |
4d44175a FW |
7118 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
7119 | nft_objname_ht_params); | |
7120 | if (err < 0) | |
b131c964 | 7121 | goto err_obj_ht; |
4d44175a | 7122 | |
e5009240 PNA |
7123 | list_add_tail_rcu(&obj->list, &table->objects); |
7124 | table->use++; | |
7125 | return 0; | |
b131c964 | 7126 | err_obj_ht: |
4d44175a FW |
7127 | /* queued in transaction log */ |
7128 | INIT_LIST_HEAD(&obj->list); | |
7129 | return err; | |
b131c964 | 7130 | err_trans: |
b131c964 | 7131 | kfree(obj->udata); |
85dfd816 PNA |
7132 | err_userdata: |
7133 | kfree(obj->key.name); | |
b131c964 | 7134 | err_strdup: |
dfc46034 | 7135 | if (obj->ops->destroy) |
00bfb320 | 7136 | obj->ops->destroy(&ctx, obj); |
e5009240 | 7137 | kfree(obj); |
b131c964 | 7138 | err_init: |
e5009240 PNA |
7139 | module_put(type->owner); |
7140 | return err; | |
7141 | } | |
7142 | ||
7143 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
7144 | u32 portid, u32 seq, int event, u32 flags, | |
7145 | int family, const struct nft_table *table, | |
43da04a5 | 7146 | struct nft_object *obj, bool reset) |
e5009240 | 7147 | { |
e5009240 PNA |
7148 | struct nlmsghdr *nlh; |
7149 | ||
dedb67c4 | 7150 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
7151 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
7152 | NFNETLINK_V0, nft_base_seq(net)); | |
7153 | if (!nlh) | |
e5009240 PNA |
7154 | goto nla_put_failure; |
7155 | ||
e5009240 | 7156 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || |
d152159b | 7157 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
dfc46034 | 7158 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || |
e5009240 | 7159 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || |
3ecbfd65 HS |
7160 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) || |
7161 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), | |
7162 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
7163 | goto nla_put_failure; |
7164 | ||
b131c964 JGG |
7165 | if (obj->udata && |
7166 | nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata)) | |
7167 | goto nla_put_failure; | |
7168 | ||
e5009240 PNA |
7169 | nlmsg_end(skb, nlh); |
7170 | return 0; | |
7171 | ||
7172 | nla_put_failure: | |
7173 | nlmsg_trim(skb, nlh); | |
7174 | return -1; | |
7175 | } | |
7176 | ||
a9fea2a3 | 7177 | struct nft_obj_filter { |
e46abbcc | 7178 | char *table; |
a9fea2a3 PNA |
7179 | u32 type; |
7180 | }; | |
7181 | ||
e5009240 PNA |
7182 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
7183 | { | |
7184 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 7185 | const struct nft_table *table; |
e5009240 | 7186 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 7187 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
7188 | struct net *net = sock_net(skb->sk); |
7189 | int family = nfmsg->nfgen_family; | |
0854db2a | 7190 | struct nftables_pernet *nft_net; |
43da04a5 PNA |
7191 | struct nft_object *obj; |
7192 | bool reset = false; | |
7193 | ||
7194 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
7195 | reset = true; | |
e5009240 PNA |
7196 | |
7197 | rcu_read_lock(); | |
d59d2f82 | 7198 | nft_net = nft_pernet(net); |
34002783 | 7199 | cb->seq = READ_ONCE(nft_net->base_seq); |
e5009240 | 7200 | |
0854db2a | 7201 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 7202 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
7203 | continue; |
7204 | ||
36596dad PNA |
7205 | list_for_each_entry_rcu(obj, &table->objects, list) { |
7206 | if (!nft_is_active(net, obj)) | |
7207 | goto cont; | |
7208 | if (idx < s_idx) | |
7209 | goto cont; | |
7210 | if (idx > s_idx) | |
7211 | memset(&cb->args[1], 0, | |
7212 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7213 | if (filter && filter->table && |
36596dad PNA |
7214 | strcmp(filter->table, table->name)) |
7215 | goto cont; | |
7216 | if (filter && | |
7217 | filter->type != NFT_OBJECT_UNSPEC && | |
7218 | obj->ops->type->type != filter->type) | |
7219 | goto cont; | |
8e6cf365 | 7220 | if (reset) { |
68df2ed5 | 7221 | char *buf = kasprintf(GFP_ATOMIC, |
c520292f | 7222 | "%s:%u", |
8e6cf365 | 7223 | table->name, |
0854db2a | 7224 | nft_net->base_seq); |
8e6cf365 RGB |
7225 | |
7226 | audit_log_nfcfg(buf, | |
7227 | family, | |
7228 | obj->handle, | |
14224039 | 7229 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7230 | GFP_ATOMIC); |
8e6cf365 RGB |
7231 | kfree(buf); |
7232 | } | |
7233 | ||
36596dad PNA |
7234 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
7235 | cb->nlh->nlmsg_seq, | |
7236 | NFT_MSG_NEWOBJ, | |
7237 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 7238 | table->family, table, |
36596dad PNA |
7239 | obj, reset) < 0) |
7240 | goto done; | |
e5009240 | 7241 | |
36596dad | 7242 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 7243 | cont: |
36596dad | 7244 | idx++; |
e5009240 PNA |
7245 | } |
7246 | } | |
7247 | done: | |
7248 | rcu_read_unlock(); | |
7249 | ||
7250 | cb->args[0] = idx; | |
7251 | return skb->len; | |
7252 | } | |
7253 | ||
90fd131a | 7254 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 7255 | { |
90fd131a FW |
7256 | const struct nlattr * const *nla = cb->data; |
7257 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 7258 | |
90fd131a FW |
7259 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
7260 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7261 | if (!filter) | |
7262 | return -ENOMEM; | |
7263 | ||
7264 | if (nla[NFTA_OBJ_TABLE]) { | |
7265 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
7266 | if (!filter->table) { | |
7267 | kfree(filter); | |
7268 | return -ENOMEM; | |
7269 | } | |
7270 | } | |
7271 | ||
7272 | if (nla[NFTA_OBJ_TYPE]) | |
7273 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 7274 | } |
a9fea2a3 | 7275 | |
90fd131a | 7276 | cb->data = filter; |
a9fea2a3 PNA |
7277 | return 0; |
7278 | } | |
7279 | ||
90fd131a | 7280 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 7281 | { |
90fd131a | 7282 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 7283 | |
90fd131a FW |
7284 | if (filter) { |
7285 | kfree(filter->table); | |
7286 | kfree(filter); | |
e46abbcc | 7287 | } |
a9fea2a3 | 7288 | |
90fd131a | 7289 | return 0; |
a9fea2a3 PNA |
7290 | } |
7291 | ||
d9adf22a | 7292 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7293 | static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info, |
7294 | const struct nlattr * const nla[]) | |
e5009240 | 7295 | { |
797d4980 PNA |
7296 | struct netlink_ext_ack *extack = info->extack; |
7297 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 7298 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7299 | const struct nft_table *table; |
797d4980 | 7300 | struct net *net = info->net; |
e5009240 PNA |
7301 | struct nft_object *obj; |
7302 | struct sk_buff *skb2; | |
43da04a5 | 7303 | bool reset = false; |
e5009240 PNA |
7304 | u32 objtype; |
7305 | int err; | |
7306 | ||
797d4980 | 7307 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
e5009240 | 7308 | struct netlink_dump_control c = { |
90fd131a | 7309 | .start = nf_tables_dump_obj_start, |
e5009240 | 7310 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 7311 | .done = nf_tables_dump_obj_done, |
d9adf22a | 7312 | .module = THIS_MODULE, |
90fd131a | 7313 | .data = (void *)nla, |
e5009240 | 7314 | }; |
a9fea2a3 | 7315 | |
797d4980 | 7316 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
e5009240 PNA |
7317 | } |
7318 | ||
7319 | if (!nla[NFTA_OBJ_NAME] || | |
7320 | !nla[NFTA_OBJ_TYPE]) | |
7321 | return -EINVAL; | |
7322 | ||
6001a930 | 7323 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0); |
36dd1bcc PNA |
7324 | if (IS_ERR(table)) { |
7325 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7326 | return PTR_ERR(table); |
36dd1bcc | 7327 | } |
e5009240 PNA |
7328 | |
7329 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7330 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
7331 | if (IS_ERR(obj)) { |
7332 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7333 | return PTR_ERR(obj); |
36dd1bcc | 7334 | } |
e5009240 | 7335 | |
d9adf22a | 7336 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
7337 | if (!skb2) |
7338 | return -ENOMEM; | |
7339 | ||
797d4980 | 7340 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
43da04a5 PNA |
7341 | reset = true; |
7342 | ||
8e6cf365 | 7343 | if (reset) { |
0854db2a FW |
7344 | const struct nftables_pernet *nft_net; |
7345 | char *buf; | |
7346 | ||
d59d2f82 | 7347 | nft_net = nft_pernet(net); |
0854db2a | 7348 | buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq); |
8e6cf365 RGB |
7349 | |
7350 | audit_log_nfcfg(buf, | |
7351 | family, | |
7352 | obj->handle, | |
14224039 | 7353 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7354 | GFP_ATOMIC); |
8e6cf365 RGB |
7355 | kfree(buf); |
7356 | } | |
7357 | ||
e5009240 | 7358 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 7359 | info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, |
43da04a5 | 7360 | family, table, obj, reset); |
e5009240 | 7361 | if (err < 0) |
ee921183 | 7362 | goto err_fill_obj_info; |
e5009240 | 7363 | |
ee921183 PNA |
7364 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
7365 | ||
7366 | err_fill_obj_info: | |
e5009240 PNA |
7367 | kfree_skb(skb2); |
7368 | return err; | |
e5009240 PNA |
7369 | } |
7370 | ||
00bfb320 | 7371 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 7372 | { |
dfc46034 | 7373 | if (obj->ops->destroy) |
00bfb320 | 7374 | obj->ops->destroy(ctx, obj); |
e5009240 | 7375 | |
dfc46034 | 7376 | module_put(obj->ops->type->owner); |
d152159b | 7377 | kfree(obj->key.name); |
bc7a7082 | 7378 | kfree(obj->udata); |
e5009240 PNA |
7379 | kfree(obj); |
7380 | } | |
7381 | ||
7dab8ee3 PNA |
7382 | static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info, |
7383 | const struct nlattr * const nla[]) | |
e5009240 | 7384 | { |
7dab8ee3 PNA |
7385 | struct netlink_ext_ack *extack = info->extack; |
7386 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7387 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 7388 | struct net *net = info->net; |
36dd1bcc | 7389 | const struct nlattr *attr; |
e5009240 PNA |
7390 | struct nft_table *table; |
7391 | struct nft_object *obj; | |
7392 | struct nft_ctx ctx; | |
7393 | u32 objtype; | |
7394 | ||
7395 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 7396 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
7397 | return -EINVAL; |
7398 | ||
6001a930 PNA |
7399 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7400 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7401 | if (IS_ERR(table)) { |
7402 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7403 | return PTR_ERR(table); |
36dd1bcc | 7404 | } |
e5009240 PNA |
7405 | |
7406 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
7407 | if (nla[NFTA_OBJ_HANDLE]) { |
7408 | attr = nla[NFTA_OBJ_HANDLE]; | |
7409 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
7410 | } else { | |
7411 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 7412 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
7413 | } |
7414 | ||
7415 | if (IS_ERR(obj)) { | |
f80a612d FFM |
7416 | if (PTR_ERR(obj) == -ENOENT && |
7417 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYOBJ) | |
7418 | return 0; | |
7419 | ||
36dd1bcc | 7420 | NL_SET_BAD_ATTR(extack, attr); |
e5009240 | 7421 | return PTR_ERR(obj); |
36dd1bcc PNA |
7422 | } |
7423 | if (obj->use > 0) { | |
7424 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7425 | return -EBUSY; |
36dd1bcc | 7426 | } |
e5009240 | 7427 | |
7dab8ee3 | 7428 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 PNA |
7429 | |
7430 | return nft_delobj(&ctx, obj); | |
7431 | } | |
7432 | ||
d152159b | 7433 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 | 7434 | struct nft_object *obj, u32 portid, u32 seq, int event, |
6fb721cf | 7435 | u16 flags, int family, int report, gfp_t gfp) |
e5009240 | 7436 | { |
d59d2f82 | 7437 | struct nftables_pernet *nft_net = nft_pernet(net); |
e5009240 PNA |
7438 | struct sk_buff *skb; |
7439 | int err; | |
c520292f | 7440 | char *buf = kasprintf(gfp, "%s:%u", |
0854db2a | 7441 | table->name, nft_net->base_seq); |
8e6cf365 RGB |
7442 | |
7443 | audit_log_nfcfg(buf, | |
7444 | family, | |
7445 | obj->handle, | |
7446 | event == NFT_MSG_NEWOBJ ? | |
c520292f RGB |
7447 | AUDIT_NFT_OP_OBJ_REGISTER : |
7448 | AUDIT_NFT_OP_OBJ_UNREGISTER, | |
68df2ed5 | 7449 | gfp); |
8e6cf365 | 7450 | kfree(buf); |
e5009240 | 7451 | |
2599e989 PNA |
7452 | if (!report && |
7453 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 7454 | return; |
e5009240 | 7455 | |
2599e989 | 7456 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
7457 | if (skb == NULL) |
7458 | goto err; | |
7459 | ||
6fb721cf PNA |
7460 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, |
7461 | flags & (NLM_F_CREATE | NLM_F_EXCL), | |
7462 | family, table, obj, false); | |
e5009240 PNA |
7463 | if (err < 0) { |
7464 | kfree_skb(skb); | |
7465 | goto err; | |
7466 | } | |
7467 | ||
0854db2a | 7468 | nft_notify_enqueue(skb, report, &nft_net->notify_list); |
25e94a99 | 7469 | return; |
e5009240 | 7470 | err: |
25e94a99 | 7471 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 7472 | } |
2599e989 PNA |
7473 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
7474 | ||
25e94a99 PNA |
7475 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
7476 | struct nft_object *obj, int event) | |
2599e989 | 7477 | { |
25e94a99 | 7478 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
6fb721cf | 7479 | ctx->flags, ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 7480 | } |
e5009240 | 7481 | |
3b49e2e9 PNA |
7482 | /* |
7483 | * Flow tables | |
7484 | */ | |
7485 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
7486 | { | |
7487 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7488 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
7489 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7490 | } | |
7491 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
7492 | ||
7493 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
7494 | { | |
7495 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7496 | list_del_rcu(&type->list); | |
7497 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7498 | } | |
7499 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
7500 | ||
7501 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
7502 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
7503 | .len = NFT_NAME_MAXLEN - 1 }, | |
7504 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
7505 | .len = NFT_NAME_MAXLEN - 1 }, | |
7506 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7507 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 7508 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
7509 | }; |
7510 | ||
cac20fcd PNA |
7511 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
7512 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
7513 | { |
7514 | struct nft_flowtable *flowtable; | |
7515 | ||
d9adf22a | 7516 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
7517 | if (!nla_strcmp(nla, flowtable->name) && |
7518 | nft_active_genmask(flowtable, genmask)) | |
7519 | return flowtable; | |
7520 | } | |
7521 | return ERR_PTR(-ENOENT); | |
7522 | } | |
cac20fcd | 7523 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 7524 | |
9b05b6e1 LGL |
7525 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
7526 | struct nft_flowtable *flowtable, | |
7527 | enum nft_trans_phase phase) | |
7528 | { | |
7529 | switch (phase) { | |
7530 | case NFT_TRANS_PREPARE: | |
7531 | case NFT_TRANS_ABORT: | |
7532 | case NFT_TRANS_RELEASE: | |
7533 | flowtable->use--; | |
954d8297 | 7534 | fallthrough; |
9b05b6e1 LGL |
7535 | default: |
7536 | return; | |
7537 | } | |
7538 | } | |
7539 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
7540 | ||
ae0662f8 | 7541 | static struct nft_flowtable * |
cac20fcd PNA |
7542 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
7543 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
7544 | { |
7545 | struct nft_flowtable *flowtable; | |
7546 | ||
7547 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
7548 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
7549 | nft_active_genmask(flowtable, genmask)) | |
7550 | return flowtable; | |
7551 | } | |
7552 | return ERR_PTR(-ENOENT); | |
7553 | } | |
7554 | ||
d9246a53 PNA |
7555 | struct nft_flowtable_hook { |
7556 | u32 num; | |
7557 | int priority; | |
7558 | struct list_head list; | |
7559 | }; | |
7560 | ||
3b49e2e9 PNA |
7561 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
7562 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
7563 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
7564 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
7565 | }; | |
7566 | ||
d9246a53 PNA |
7567 | static int nft_flowtable_parse_hook(const struct nft_ctx *ctx, |
7568 | const struct nlattr *attr, | |
7569 | struct nft_flowtable_hook *flowtable_hook, | |
5b6743fb | 7570 | struct nft_flowtable *flowtable, bool add) |
3b49e2e9 | 7571 | { |
3b49e2e9 | 7572 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 7573 | struct nft_hook *hook; |
3b49e2e9 | 7574 | int hooknum, priority; |
3f0465a9 | 7575 | int err; |
3b49e2e9 | 7576 | |
d9246a53 PNA |
7577 | INIT_LIST_HEAD(&flowtable_hook->list); |
7578 | ||
8cb08174 JB |
7579 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr, |
7580 | nft_flowtable_hook_policy, NULL); | |
3b49e2e9 PNA |
7581 | if (err < 0) |
7582 | return err; | |
7583 | ||
5b6743fb PNA |
7584 | if (add) { |
7585 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
7586 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) | |
7587 | return -EINVAL; | |
3b49e2e9 | 7588 | |
5b6743fb PNA |
7589 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); |
7590 | if (hooknum != NF_NETDEV_INGRESS) | |
7591 | return -EOPNOTSUPP; | |
7592 | ||
7593 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7594 | ||
7595 | flowtable_hook->priority = priority; | |
7596 | flowtable_hook->num = hooknum; | |
7597 | } else { | |
7598 | if (tb[NFTA_FLOWTABLE_HOOK_NUM]) { | |
7599 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
7600 | if (hooknum != flowtable->hooknum) | |
7601 | return -EOPNOTSUPP; | |
7602 | } | |
7603 | ||
7604 | if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { | |
7605 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7606 | if (priority != flowtable->data.priority) | |
7607 | return -EOPNOTSUPP; | |
7608 | } | |
3b49e2e9 | 7609 | |
5b6743fb PNA |
7610 | flowtable_hook->priority = flowtable->data.priority; |
7611 | flowtable_hook->num = flowtable->hooknum; | |
7612 | } | |
3b49e2e9 | 7613 | |
05abe445 PNA |
7614 | if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) { |
7615 | err = nf_tables_parse_netdev_hooks(ctx->net, | |
7616 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
7617 | &flowtable_hook->list); | |
7618 | if (err < 0) | |
7619 | return err; | |
7620 | } | |
3b49e2e9 | 7621 | |
d9246a53 | 7622 | list_for_each_entry(hook, &flowtable_hook->list, list) { |
3f0465a9 | 7623 | hook->ops.pf = NFPROTO_NETDEV; |
5b6743fb PNA |
7624 | hook->ops.hooknum = flowtable_hook->num; |
7625 | hook->ops.priority = flowtable_hook->priority; | |
7626 | hook->ops.priv = &flowtable->data; | |
7627 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
7628 | } |
7629 | ||
3b49e2e9 PNA |
7630 | return err; |
7631 | } | |
7632 | ||
98319cb9 | 7633 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
7634 | { |
7635 | const struct nf_flowtable_type *type; | |
7636 | ||
7637 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 7638 | if (family == type->family) |
3b49e2e9 PNA |
7639 | return type; |
7640 | } | |
7641 | return NULL; | |
7642 | } | |
7643 | ||
452238e8 FW |
7644 | static const struct nf_flowtable_type * |
7645 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
7646 | { |
7647 | const struct nf_flowtable_type *type; | |
7648 | ||
98319cb9 | 7649 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
7650 | if (type != NULL && try_module_get(type->owner)) |
7651 | return type; | |
7652 | ||
f102d66b | 7653 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
7654 | #ifdef CONFIG_MODULES |
7655 | if (type == NULL) { | |
eb014de4 | 7656 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
7657 | return ERR_PTR(-EAGAIN); |
7658 | } | |
7659 | #endif | |
7660 | return ERR_PTR(-ENOENT); | |
7661 | } | |
7662 | ||
5acab914 | 7663 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
7664 | static void nft_unregister_flowtable_hook(struct net *net, |
7665 | struct nft_flowtable *flowtable, | |
7666 | struct nft_hook *hook) | |
7667 | { | |
7668 | nf_unregister_net_hook(net, &hook->ops); | |
7669 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
7670 | FLOW_BLOCK_UNBIND); | |
7671 | } | |
7672 | ||
f9a43007 PNA |
7673 | static void __nft_unregister_flowtable_net_hooks(struct net *net, |
7674 | struct list_head *hook_list, | |
7675 | bool release_netdev) | |
3b49e2e9 | 7676 | { |
f9a43007 | 7677 | struct nft_hook *hook, *next; |
3b49e2e9 | 7678 | |
f9a43007 | 7679 | list_for_each_entry_safe(hook, next, hook_list, list) { |
5acab914 | 7680 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
7681 | if (release_netdev) { |
7682 | list_del(&hook->list); | |
ab5e5c06 | 7683 | kfree_rcu(hook, rcu); |
f9a43007 PNA |
7684 | } |
7685 | } | |
7686 | } | |
7687 | ||
7688 | static void nft_unregister_flowtable_net_hooks(struct net *net, | |
7689 | struct list_head *hook_list) | |
7690 | { | |
7691 | __nft_unregister_flowtable_net_hooks(net, hook_list, false); | |
3f0465a9 PNA |
7692 | } |
7693 | ||
7694 | static int nft_register_flowtable_net_hooks(struct net *net, | |
7695 | struct nft_table *table, | |
f9382669 | 7696 | struct list_head *hook_list, |
3f0465a9 PNA |
7697 | struct nft_flowtable *flowtable) |
7698 | { | |
7699 | struct nft_hook *hook, *hook2, *next; | |
7700 | struct nft_flowtable *ft; | |
7701 | int err, i = 0; | |
7702 | ||
f9382669 | 7703 | list_for_each_entry(hook, hook_list, list) { |
3f0465a9 | 7704 | list_for_each_entry(ft, &table->flowtables, list) { |
86fe2c19 PNA |
7705 | if (!nft_is_active_next(net, ft)) |
7706 | continue; | |
7707 | ||
3f0465a9 PNA |
7708 | list_for_each_entry(hook2, &ft->hook_list, list) { |
7709 | if (hook->ops.dev == hook2->ops.dev && | |
7710 | hook->ops.pf == hook2->ops.pf) { | |
77a92189 | 7711 | err = -EEXIST; |
3f0465a9 PNA |
7712 | goto err_unregister_net_hooks; |
7713 | } | |
7714 | } | |
7715 | } | |
3b49e2e9 | 7716 | |
d7c03a9f | 7717 | err = flowtable->data.type->setup(&flowtable->data, |
7718 | hook->ops.dev, | |
7719 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
7720 | if (err < 0) |
7721 | goto err_unregister_net_hooks; | |
7722 | ||
d7c03a9f | 7723 | err = nf_register_net_hook(net, &hook->ops); |
7724 | if (err < 0) { | |
7725 | flowtable->data.type->setup(&flowtable->data, | |
7726 | hook->ops.dev, | |
7727 | FLOW_BLOCK_UNBIND); | |
7728 | goto err_unregister_net_hooks; | |
7729 | } | |
7730 | ||
3f0465a9 | 7731 | i++; |
3b49e2e9 | 7732 | } |
3f0465a9 PNA |
7733 | |
7734 | return 0; | |
7735 | ||
7736 | err_unregister_net_hooks: | |
f9382669 | 7737 | list_for_each_entry_safe(hook, next, hook_list, list) { |
3f0465a9 PNA |
7738 | if (i-- <= 0) |
7739 | break; | |
7740 | ||
ff4bf2f4 | 7741 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7742 | list_del_rcu(&hook->list); |
7743 | kfree_rcu(hook, rcu); | |
7744 | } | |
7745 | ||
7746 | return err; | |
3b49e2e9 PNA |
7747 | } |
7748 | ||
389a2cbc PNA |
7749 | static void nft_flowtable_hooks_destroy(struct list_head *hook_list) |
7750 | { | |
7751 | struct nft_hook *hook, *next; | |
7752 | ||
7753 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
7754 | list_del_rcu(&hook->list); | |
7755 | kfree_rcu(hook, rcu); | |
7756 | } | |
7757 | } | |
7758 | ||
78d9f48f PNA |
7759 | static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, |
7760 | struct nft_flowtable *flowtable) | |
7761 | { | |
7762 | const struct nlattr * const *nla = ctx->nla; | |
7763 | struct nft_flowtable_hook flowtable_hook; | |
7764 | struct nft_hook *hook, *next; | |
7765 | struct nft_trans *trans; | |
7766 | bool unregister = false; | |
7b35582c | 7767 | u32 flags; |
78d9f48f PNA |
7768 | int err; |
7769 | ||
7770 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7771 | &flowtable_hook, flowtable, false); |
78d9f48f PNA |
7772 | if (err < 0) |
7773 | return err; | |
7774 | ||
7775 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7776 | if (nft_hook_list_find(&flowtable->hook_list, hook)) { | |
7777 | list_del(&hook->list); | |
7778 | kfree(hook); | |
7779 | } | |
7780 | } | |
7781 | ||
7b35582c PNA |
7782 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7783 | flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
c271cc9f PNA |
7784 | if (flags & ~NFT_FLOWTABLE_MASK) { |
7785 | err = -EOPNOTSUPP; | |
7786 | goto err_flowtable_update_hook; | |
7787 | } | |
7b35582c | 7788 | if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^ |
c271cc9f PNA |
7789 | (flags & NFT_FLOWTABLE_HW_OFFLOAD)) { |
7790 | err = -EOPNOTSUPP; | |
7791 | goto err_flowtable_update_hook; | |
7792 | } | |
7b35582c PNA |
7793 | } else { |
7794 | flags = flowtable->data.flags; | |
7795 | } | |
7796 | ||
78d9f48f PNA |
7797 | err = nft_register_flowtable_net_hooks(ctx->net, ctx->table, |
7798 | &flowtable_hook.list, flowtable); | |
7799 | if (err < 0) | |
7800 | goto err_flowtable_update_hook; | |
7801 | ||
7802 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE, | |
7803 | sizeof(struct nft_trans_flowtable)); | |
7804 | if (!trans) { | |
7805 | unregister = true; | |
7806 | err = -ENOMEM; | |
7807 | goto err_flowtable_update_hook; | |
7808 | } | |
7809 | ||
7b35582c | 7810 | nft_trans_flowtable_flags(trans) = flags; |
78d9f48f PNA |
7811 | nft_trans_flowtable(trans) = flowtable; |
7812 | nft_trans_flowtable_update(trans) = true; | |
7813 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
7814 | list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans)); | |
7815 | ||
0854db2a | 7816 | nft_trans_commit_list_add_tail(ctx->net, trans); |
78d9f48f PNA |
7817 | |
7818 | return 0; | |
7819 | ||
7820 | err_flowtable_update_hook: | |
7821 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7822 | if (unregister) | |
7823 | nft_unregister_flowtable_hook(ctx->net, flowtable, hook); | |
7824 | list_del_rcu(&hook->list); | |
7825 | kfree_rcu(hook, rcu); | |
7826 | } | |
7827 | ||
7828 | return err; | |
7829 | ||
7830 | } | |
7831 | ||
7dab8ee3 PNA |
7832 | static int nf_tables_newflowtable(struct sk_buff *skb, |
7833 | const struct nfnl_info *info, | |
7834 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7835 | { |
7dab8ee3 | 7836 | struct netlink_ext_ack *extack = info->extack; |
d9246a53 | 7837 | struct nft_flowtable_hook flowtable_hook; |
7dab8ee3 | 7838 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 7839 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7840 | const struct nf_flowtable_type *type; |
3f0465a9 PNA |
7841 | struct nft_flowtable *flowtable; |
7842 | struct nft_hook *hook, *next; | |
7dab8ee3 | 7843 | struct net *net = info->net; |
3b49e2e9 PNA |
7844 | struct nft_table *table; |
7845 | struct nft_ctx ctx; | |
3f0465a9 | 7846 | int err; |
3b49e2e9 PNA |
7847 | |
7848 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
7849 | !nla[NFTA_FLOWTABLE_NAME] || | |
7850 | !nla[NFTA_FLOWTABLE_HOOK]) | |
7851 | return -EINVAL; | |
7852 | ||
cac20fcd | 7853 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7854 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7855 | if (IS_ERR(table)) { |
7856 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7857 | return PTR_ERR(table); |
36dd1bcc | 7858 | } |
3b49e2e9 | 7859 | |
cac20fcd PNA |
7860 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
7861 | genmask); | |
3b49e2e9 PNA |
7862 | if (IS_ERR(flowtable)) { |
7863 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
7864 | if (err != -ENOENT) { |
7865 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 7866 | return err; |
36dd1bcc | 7867 | } |
3b49e2e9 | 7868 | } else { |
7dab8ee3 | 7869 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 7870 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); |
3b49e2e9 | 7871 | return -EEXIST; |
36dd1bcc | 7872 | } |
3b49e2e9 | 7873 | |
7dab8ee3 | 7874 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
78d9f48f | 7875 | |
7dab8ee3 | 7876 | return nft_flowtable_update(&ctx, info->nlh, flowtable); |
3b49e2e9 PNA |
7877 | } |
7878 | ||
7dab8ee3 | 7879 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
3b49e2e9 | 7880 | |
33758c89 | 7881 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
7882 | if (!flowtable) |
7883 | return -ENOMEM; | |
7884 | ||
7885 | flowtable->table = table; | |
3ecbfd65 | 7886 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 7887 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 7888 | |
33758c89 | 7889 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
7890 | if (!flowtable->name) { |
7891 | err = -ENOMEM; | |
7892 | goto err1; | |
7893 | } | |
7894 | ||
452238e8 | 7895 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
7896 | if (IS_ERR(type)) { |
7897 | err = PTR_ERR(type); | |
7898 | goto err2; | |
7899 | } | |
7900 | ||
8bb69f3b PNA |
7901 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7902 | flowtable->data.flags = | |
7903 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
7e6136f1 PNA |
7904 | if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) { |
7905 | err = -EOPNOTSUPP; | |
8bb69f3b | 7906 | goto err3; |
7e6136f1 | 7907 | } |
8bb69f3b PNA |
7908 | } |
7909 | ||
7910 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 7911 | flowtable->data.type = type; |
a268de77 | 7912 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
7913 | if (err < 0) |
7914 | goto err3; | |
7915 | ||
d9246a53 | 7916 | err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK], |
5b6743fb | 7917 | &flowtable_hook, flowtable, true); |
3b49e2e9 | 7918 | if (err < 0) |
a268de77 | 7919 | goto err4; |
3b49e2e9 | 7920 | |
d9246a53 PNA |
7921 | list_splice(&flowtable_hook.list, &flowtable->hook_list); |
7922 | flowtable->data.priority = flowtable_hook.priority; | |
7923 | flowtable->hooknum = flowtable_hook.num; | |
7924 | ||
f9382669 PNA |
7925 | err = nft_register_flowtable_net_hooks(ctx.net, table, |
7926 | &flowtable->hook_list, | |
7927 | flowtable); | |
2d285f26 | 7928 | if (err < 0) { |
389a2cbc | 7929 | nft_flowtable_hooks_destroy(&flowtable->hook_list); |
3f0465a9 | 7930 | goto err4; |
2d285f26 | 7931 | } |
3b49e2e9 PNA |
7932 | |
7933 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
7934 | if (err < 0) | |
3f0465a9 | 7935 | goto err5; |
3b49e2e9 PNA |
7936 | |
7937 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
7938 | table->use++; | |
7939 | ||
7940 | return 0; | |
a268de77 | 7941 | err5: |
3f0465a9 | 7942 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 7943 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7944 | list_del_rcu(&hook->list); |
7945 | kfree_rcu(hook, rcu); | |
7946 | } | |
a268de77 FF |
7947 | err4: |
7948 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
7949 | err3: |
7950 | module_put(type->owner); | |
7951 | err2: | |
7952 | kfree(flowtable->name); | |
7953 | err1: | |
7954 | kfree(flowtable); | |
7955 | return err; | |
7956 | } | |
7957 | ||
3003055f PNA |
7958 | static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook) |
7959 | { | |
7960 | struct nft_hook *this, *next; | |
7961 | ||
7962 | list_for_each_entry_safe(this, next, &flowtable_hook->list, list) { | |
7963 | list_del(&this->list); | |
7964 | kfree(this); | |
7965 | } | |
7966 | } | |
7967 | ||
abadb2f8 PNA |
7968 | static int nft_delflowtable_hook(struct nft_ctx *ctx, |
7969 | struct nft_flowtable *flowtable) | |
7970 | { | |
7971 | const struct nlattr * const *nla = ctx->nla; | |
7972 | struct nft_flowtable_hook flowtable_hook; | |
b6d9014a | 7973 | LIST_HEAD(flowtable_del_list); |
3003055f | 7974 | struct nft_hook *this, *hook; |
abadb2f8 PNA |
7975 | struct nft_trans *trans; |
7976 | int err; | |
7977 | ||
7978 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7979 | &flowtable_hook, flowtable, false); |
abadb2f8 PNA |
7980 | if (err < 0) |
7981 | return err; | |
7982 | ||
3003055f | 7983 | list_for_each_entry(this, &flowtable_hook.list, list) { |
abadb2f8 PNA |
7984 | hook = nft_hook_list_find(&flowtable->hook_list, this); |
7985 | if (!hook) { | |
7986 | err = -ENOENT; | |
7987 | goto err_flowtable_del_hook; | |
7988 | } | |
b6d9014a | 7989 | list_move(&hook->list, &flowtable_del_list); |
abadb2f8 PNA |
7990 | } |
7991 | ||
7992 | trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE, | |
7993 | sizeof(struct nft_trans_flowtable)); | |
3003055f PNA |
7994 | if (!trans) { |
7995 | err = -ENOMEM; | |
7996 | goto err_flowtable_del_hook; | |
7997 | } | |
abadb2f8 PNA |
7998 | |
7999 | nft_trans_flowtable(trans) = flowtable; | |
8000 | nft_trans_flowtable_update(trans) = true; | |
8001 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
b6d9014a | 8002 | list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans)); |
3003055f | 8003 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 | 8004 | |
0854db2a | 8005 | nft_trans_commit_list_add_tail(ctx->net, trans); |
abadb2f8 PNA |
8006 | |
8007 | return 0; | |
8008 | ||
8009 | err_flowtable_del_hook: | |
b6d9014a | 8010 | list_splice(&flowtable_del_list, &flowtable->hook_list); |
3003055f | 8011 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 PNA |
8012 | |
8013 | return err; | |
8014 | } | |
8015 | ||
7dab8ee3 PNA |
8016 | static int nf_tables_delflowtable(struct sk_buff *skb, |
8017 | const struct nfnl_info *info, | |
8018 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8019 | { |
7dab8ee3 PNA |
8020 | struct netlink_ext_ack *extack = info->extack; |
8021 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 8022 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8023 | struct nft_flowtable *flowtable; |
7dab8ee3 | 8024 | struct net *net = info->net; |
36dd1bcc | 8025 | const struct nlattr *attr; |
3b49e2e9 PNA |
8026 | struct nft_table *table; |
8027 | struct nft_ctx ctx; | |
8028 | ||
e603ea4b PNA |
8029 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
8030 | (!nla[NFTA_FLOWTABLE_NAME] && | |
8031 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
8032 | return -EINVAL; | |
8033 | ||
cac20fcd | 8034 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8035 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
8036 | if (IS_ERR(table)) { |
8037 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 8038 | return PTR_ERR(table); |
36dd1bcc | 8039 | } |
3b49e2e9 | 8040 | |
36dd1bcc PNA |
8041 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
8042 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
8043 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
8044 | } else { | |
8045 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
8046 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
8047 | } | |
8048 | ||
8049 | if (IS_ERR(flowtable)) { | |
f80a612d FFM |
8050 | if (PTR_ERR(flowtable) == -ENOENT && |
8051 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYFLOWTABLE) | |
8052 | return 0; | |
8053 | ||
36dd1bcc PNA |
8054 | NL_SET_BAD_ATTR(extack, attr); |
8055 | return PTR_ERR(flowtable); | |
8056 | } | |
abadb2f8 | 8057 | |
7dab8ee3 | 8058 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
abadb2f8 PNA |
8059 | |
8060 | if (nla[NFTA_FLOWTABLE_HOOK]) | |
8061 | return nft_delflowtable_hook(&ctx, flowtable); | |
8062 | ||
36dd1bcc PNA |
8063 | if (flowtable->use > 0) { |
8064 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 8065 | return -EBUSY; |
36dd1bcc | 8066 | } |
3b49e2e9 | 8067 | |
3b49e2e9 PNA |
8068 | return nft_delflowtable(&ctx, flowtable); |
8069 | } | |
8070 | ||
8071 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
8072 | u32 portid, u32 seq, int event, | |
8073 | u32 flags, int family, | |
c42d8bda PNA |
8074 | struct nft_flowtable *flowtable, |
8075 | struct list_head *hook_list) | |
3b49e2e9 PNA |
8076 | { |
8077 | struct nlattr *nest, *nest_devs; | |
3f0465a9 | 8078 | struct nft_hook *hook; |
3b49e2e9 | 8079 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
8080 | |
8081 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
19c28b13 PNA |
8082 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
8083 | NFNETLINK_V0, nft_base_seq(net)); | |
8084 | if (!nlh) | |
3b49e2e9 PNA |
8085 | goto nla_put_failure; |
8086 | ||
3b49e2e9 PNA |
8087 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || |
8088 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 HS |
8089 | nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || |
8090 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), | |
8bb69f3b PNA |
8091 | NFTA_FLOWTABLE_PAD) || |
8092 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) | |
3b49e2e9 PNA |
8093 | goto nla_put_failure; |
8094 | ||
ae0be8de | 8095 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
8096 | if (!nest) |
8097 | goto nla_put_failure; | |
3b49e2e9 | 8098 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 8099 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
8100 | goto nla_put_failure; |
8101 | ||
ae0be8de | 8102 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
8103 | if (!nest_devs) |
8104 | goto nla_put_failure; | |
8105 | ||
c42d8bda | 8106 | list_for_each_entry_rcu(hook, hook_list, list) { |
3f0465a9 | 8107 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) |
3b49e2e9 PNA |
8108 | goto nla_put_failure; |
8109 | } | |
8110 | nla_nest_end(skb, nest_devs); | |
8111 | nla_nest_end(skb, nest); | |
8112 | ||
8113 | nlmsg_end(skb, nlh); | |
8114 | return 0; | |
8115 | ||
8116 | nla_put_failure: | |
8117 | nlmsg_trim(skb, nlh); | |
8118 | return -1; | |
8119 | } | |
8120 | ||
8121 | struct nft_flowtable_filter { | |
8122 | char *table; | |
8123 | }; | |
8124 | ||
8125 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
8126 | struct netlink_callback *cb) | |
8127 | { | |
8128 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
8129 | struct nft_flowtable_filter *filter = cb->data; | |
8130 | unsigned int idx = 0, s_idx = cb->args[0]; | |
8131 | struct net *net = sock_net(skb->sk); | |
8132 | int family = nfmsg->nfgen_family; | |
8133 | struct nft_flowtable *flowtable; | |
0854db2a | 8134 | struct nftables_pernet *nft_net; |
3b49e2e9 PNA |
8135 | const struct nft_table *table; |
8136 | ||
8137 | rcu_read_lock(); | |
d59d2f82 | 8138 | nft_net = nft_pernet(net); |
34002783 | 8139 | cb->seq = READ_ONCE(nft_net->base_seq); |
3b49e2e9 | 8140 | |
0854db2a | 8141 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 8142 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
8143 | continue; |
8144 | ||
36596dad PNA |
8145 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
8146 | if (!nft_is_active(net, flowtable)) | |
8147 | goto cont; | |
8148 | if (idx < s_idx) | |
8149 | goto cont; | |
8150 | if (idx > s_idx) | |
8151 | memset(&cb->args[1], 0, | |
8152 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 8153 | if (filter && filter->table && |
36596dad PNA |
8154 | strcmp(filter->table, table->name)) |
8155 | goto cont; | |
3b49e2e9 | 8156 | |
36596dad PNA |
8157 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
8158 | cb->nlh->nlmsg_seq, | |
8159 | NFT_MSG_NEWFLOWTABLE, | |
8160 | NLM_F_MULTI | NLM_F_APPEND, | |
c42d8bda PNA |
8161 | table->family, |
8162 | flowtable, | |
8163 | &flowtable->hook_list) < 0) | |
36596dad | 8164 | goto done; |
3b49e2e9 | 8165 | |
36596dad | 8166 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 8167 | cont: |
36596dad | 8168 | idx++; |
3b49e2e9 PNA |
8169 | } |
8170 | } | |
8171 | done: | |
8172 | rcu_read_unlock(); | |
8173 | ||
8174 | cb->args[0] = idx; | |
8175 | return skb->len; | |
8176 | } | |
8177 | ||
90fd131a | 8178 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 8179 | { |
90fd131a FW |
8180 | const struct nlattr * const *nla = cb->data; |
8181 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 8182 | |
90fd131a FW |
8183 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
8184 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
8185 | if (!filter) | |
8186 | return -ENOMEM; | |
3b49e2e9 | 8187 | |
90fd131a FW |
8188 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
8189 | GFP_ATOMIC); | |
8190 | if (!filter->table) { | |
8191 | kfree(filter); | |
8192 | return -ENOMEM; | |
8193 | } | |
8194 | } | |
3b49e2e9 | 8195 | |
90fd131a | 8196 | cb->data = filter; |
3b49e2e9 PNA |
8197 | return 0; |
8198 | } | |
8199 | ||
90fd131a | 8200 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 8201 | { |
90fd131a | 8202 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 8203 | |
3b49e2e9 | 8204 | if (!filter) |
90fd131a | 8205 | return 0; |
3b49e2e9 | 8206 | |
90fd131a FW |
8207 | kfree(filter->table); |
8208 | kfree(filter); | |
8209 | ||
8210 | return 0; | |
3b49e2e9 PNA |
8211 | } |
8212 | ||
d9adf22a | 8213 | /* called with rcu_read_lock held */ |
797d4980 PNA |
8214 | static int nf_tables_getflowtable(struct sk_buff *skb, |
8215 | const struct nfnl_info *info, | |
8216 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8217 | { |
797d4980 | 8218 | u8 genmask = nft_genmask_cur(info->net); |
ef4b65e5 | 8219 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8220 | struct nft_flowtable *flowtable; |
3b49e2e9 | 8221 | const struct nft_table *table; |
797d4980 | 8222 | struct net *net = info->net; |
3b49e2e9 PNA |
8223 | struct sk_buff *skb2; |
8224 | int err; | |
8225 | ||
797d4980 | 8226 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
3b49e2e9 | 8227 | struct netlink_dump_control c = { |
90fd131a | 8228 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
8229 | .dump = nf_tables_dump_flowtable, |
8230 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 8231 | .module = THIS_MODULE, |
90fd131a | 8232 | .data = (void *)nla, |
3b49e2e9 PNA |
8233 | }; |
8234 | ||
797d4980 | 8235 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
3b49e2e9 PNA |
8236 | } |
8237 | ||
8238 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
8239 | return -EINVAL; | |
8240 | ||
cac20fcd | 8241 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8242 | genmask, 0); |
3b49e2e9 PNA |
8243 | if (IS_ERR(table)) |
8244 | return PTR_ERR(table); | |
8245 | ||
cac20fcd PNA |
8246 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
8247 | genmask); | |
03a0120f | 8248 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
8249 | return PTR_ERR(flowtable); |
8250 | ||
d9adf22a | 8251 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
8252 | if (!skb2) |
8253 | return -ENOMEM; | |
8254 | ||
8255 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
797d4980 | 8256 | info->nlh->nlmsg_seq, |
3b49e2e9 | 8257 | NFT_MSG_NEWFLOWTABLE, 0, family, |
c42d8bda | 8258 | flowtable, &flowtable->hook_list); |
3b49e2e9 | 8259 | if (err < 0) |
ee921183 | 8260 | goto err_fill_flowtable_info; |
3b49e2e9 | 8261 | |
ee921183 PNA |
8262 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
8263 | ||
8264 | err_fill_flowtable_info: | |
3b49e2e9 PNA |
8265 | kfree_skb(skb2); |
8266 | return err; | |
8267 | } | |
8268 | ||
8269 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
8270 | struct nft_flowtable *flowtable, | |
c42d8bda | 8271 | struct list_head *hook_list, |
3b49e2e9 PNA |
8272 | int event) |
8273 | { | |
d59d2f82 | 8274 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
3b49e2e9 | 8275 | struct sk_buff *skb; |
6fb721cf | 8276 | u16 flags = 0; |
3b49e2e9 PNA |
8277 | int err; |
8278 | ||
dceababa | 8279 | if (!ctx->report && |
3b49e2e9 PNA |
8280 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
8281 | return; | |
8282 | ||
8283 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
8284 | if (skb == NULL) | |
8285 | goto err; | |
8286 | ||
6fb721cf PNA |
8287 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
8288 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
8289 | ||
3b49e2e9 | 8290 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, |
6fb721cf | 8291 | ctx->seq, event, flags, |
c42d8bda | 8292 | ctx->family, flowtable, hook_list); |
3b49e2e9 PNA |
8293 | if (err < 0) { |
8294 | kfree_skb(skb); | |
8295 | goto err; | |
8296 | } | |
8297 | ||
0854db2a | 8298 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
3b49e2e9 PNA |
8299 | return; |
8300 | err: | |
8301 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
8302 | } | |
8303 | ||
3b49e2e9 PNA |
8304 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
8305 | { | |
3f0465a9 PNA |
8306 | struct nft_hook *hook, *next; |
8307 | ||
5acab914 | 8308 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 8309 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
8310 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
8311 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
8312 | list_del_rcu(&hook->list); |
8313 | kfree(hook); | |
8314 | } | |
3b49e2e9 | 8315 | kfree(flowtable->name); |
3b49e2e9 | 8316 | module_put(flowtable->data.type->owner); |
a12486eb | 8317 | kfree(flowtable); |
3b49e2e9 PNA |
8318 | } |
8319 | ||
84d7fce6 PNA |
8320 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
8321 | u32 portid, u32 seq) | |
8322 | { | |
d59d2f82 | 8323 | struct nftables_pernet *nft_net = nft_pernet(net); |
84d7fce6 | 8324 | struct nlmsghdr *nlh; |
784b4e61 | 8325 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 8326 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 | 8327 | |
19c28b13 PNA |
8328 | nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC, |
8329 | NFNETLINK_V0, nft_base_seq(net)); | |
8330 | if (!nlh) | |
84d7fce6 PNA |
8331 | goto nla_put_failure; |
8332 | ||
0854db2a | 8333 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) || |
784b4e61 PS |
8334 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || |
8335 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
8336 | goto nla_put_failure; |
8337 | ||
053c095a JB |
8338 | nlmsg_end(skb, nlh); |
8339 | return 0; | |
84d7fce6 PNA |
8340 | |
8341 | nla_put_failure: | |
8342 | nlmsg_trim(skb, nlh); | |
8343 | return -EMSGSIZE; | |
8344 | } | |
8345 | ||
3b49e2e9 PNA |
8346 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
8347 | struct nft_flowtable *flowtable) | |
8348 | { | |
3f0465a9 | 8349 | struct nft_hook *hook; |
3b49e2e9 | 8350 | |
3f0465a9 PNA |
8351 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
8352 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
8353 | continue; |
8354 | ||
5acab914 | 8355 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 8356 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
8357 | list_del_rcu(&hook->list); |
8358 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
8359 | break; |
8360 | } | |
8361 | } | |
8362 | ||
8363 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
8364 | unsigned long event, void *ptr) | |
8365 | { | |
8366 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
8367 | struct nft_flowtable *flowtable; | |
0854db2a | 8368 | struct nftables_pernet *nft_net; |
3b49e2e9 | 8369 | struct nft_table *table; |
0a2cf5ee | 8370 | struct net *net; |
3b49e2e9 PNA |
8371 | |
8372 | if (event != NETDEV_UNREGISTER) | |
8373 | return 0; | |
8374 | ||
6a48de01 | 8375 | net = dev_net(dev); |
d59d2f82 | 8376 | nft_net = nft_pernet(net); |
0854db2a FW |
8377 | mutex_lock(&nft_net->commit_mutex); |
8378 | list_for_each_entry(table, &nft_net->tables, list) { | |
36596dad PNA |
8379 | list_for_each_entry(flowtable, &table->flowtables, list) { |
8380 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
8381 | } |
8382 | } | |
0854db2a | 8383 | mutex_unlock(&nft_net->commit_mutex); |
6a48de01 | 8384 | |
3b49e2e9 PNA |
8385 | return NOTIFY_DONE; |
8386 | } | |
8387 | ||
8388 | static struct notifier_block nf_tables_flowtable_notifier = { | |
8389 | .notifier_call = nf_tables_flowtable_event, | |
8390 | }; | |
8391 | ||
25e94a99 PNA |
8392 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
8393 | int event) | |
84d7fce6 PNA |
8394 | { |
8395 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
8396 | struct sk_buff *skb2; | |
8397 | int err; | |
8398 | ||
dceababa | 8399 | if (!nlmsg_report(nlh) && |
84d7fce6 | 8400 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 8401 | return; |
84d7fce6 | 8402 | |
84d7fce6 PNA |
8403 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
8404 | if (skb2 == NULL) | |
8405 | goto err; | |
8406 | ||
8407 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
8408 | nlh->nlmsg_seq); | |
8409 | if (err < 0) { | |
8410 | kfree_skb(skb2); | |
8411 | goto err; | |
8412 | } | |
8413 | ||
25e94a99 PNA |
8414 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8415 | nlmsg_report(nlh), GFP_KERNEL); | |
8416 | return; | |
84d7fce6 | 8417 | err: |
25e94a99 PNA |
8418 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8419 | -ENOBUFS); | |
84d7fce6 PNA |
8420 | } |
8421 | ||
797d4980 PNA |
8422 | static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info, |
8423 | const struct nlattr * const nla[]) | |
84d7fce6 | 8424 | { |
84d7fce6 PNA |
8425 | struct sk_buff *skb2; |
8426 | int err; | |
8427 | ||
d9adf22a | 8428 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
8429 | if (skb2 == NULL) |
8430 | return -ENOMEM; | |
8431 | ||
797d4980 PNA |
8432 | err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid, |
8433 | info->nlh->nlmsg_seq); | |
84d7fce6 | 8434 | if (err < 0) |
ee921183 | 8435 | goto err_fill_gen_info; |
84d7fce6 | 8436 | |
797d4980 | 8437 | return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); |
ee921183 PNA |
8438 | |
8439 | err_fill_gen_info: | |
84d7fce6 PNA |
8440 | kfree_skb(skb2); |
8441 | return err; | |
8442 | } | |
8443 | ||
96518518 PM |
8444 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
8445 | [NFT_MSG_NEWTABLE] = { | |
50f2db9e PNA |
8446 | .call = nf_tables_newtable, |
8447 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8448 | .attr_count = NFTA_TABLE_MAX, |
8449 | .policy = nft_table_policy, | |
8450 | }, | |
8451 | [NFT_MSG_GETTABLE] = { | |
50f2db9e PNA |
8452 | .call = nf_tables_gettable, |
8453 | .type = NFNL_CB_RCU, | |
96518518 PM |
8454 | .attr_count = NFTA_TABLE_MAX, |
8455 | .policy = nft_table_policy, | |
8456 | }, | |
8457 | [NFT_MSG_DELTABLE] = { | |
50f2db9e PNA |
8458 | .call = nf_tables_deltable, |
8459 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8460 | .attr_count = NFTA_TABLE_MAX, |
8461 | .policy = nft_table_policy, | |
8462 | }, | |
f80a612d FFM |
8463 | [NFT_MSG_DESTROYTABLE] = { |
8464 | .call = nf_tables_deltable, | |
8465 | .type = NFNL_CB_BATCH, | |
8466 | .attr_count = NFTA_TABLE_MAX, | |
8467 | .policy = nft_table_policy, | |
8468 | }, | |
96518518 | 8469 | [NFT_MSG_NEWCHAIN] = { |
50f2db9e PNA |
8470 | .call = nf_tables_newchain, |
8471 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8472 | .attr_count = NFTA_CHAIN_MAX, |
8473 | .policy = nft_chain_policy, | |
8474 | }, | |
8475 | [NFT_MSG_GETCHAIN] = { | |
50f2db9e PNA |
8476 | .call = nf_tables_getchain, |
8477 | .type = NFNL_CB_RCU, | |
96518518 PM |
8478 | .attr_count = NFTA_CHAIN_MAX, |
8479 | .policy = nft_chain_policy, | |
8480 | }, | |
8481 | [NFT_MSG_DELCHAIN] = { | |
50f2db9e PNA |
8482 | .call = nf_tables_delchain, |
8483 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8484 | .attr_count = NFTA_CHAIN_MAX, |
8485 | .policy = nft_chain_policy, | |
8486 | }, | |
f80a612d FFM |
8487 | [NFT_MSG_DESTROYCHAIN] = { |
8488 | .call = nf_tables_delchain, | |
8489 | .type = NFNL_CB_BATCH, | |
8490 | .attr_count = NFTA_CHAIN_MAX, | |
8491 | .policy = nft_chain_policy, | |
8492 | }, | |
96518518 | 8493 | [NFT_MSG_NEWRULE] = { |
50f2db9e PNA |
8494 | .call = nf_tables_newrule, |
8495 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8496 | .attr_count = NFTA_RULE_MAX, |
8497 | .policy = nft_rule_policy, | |
8498 | }, | |
8499 | [NFT_MSG_GETRULE] = { | |
50f2db9e PNA |
8500 | .call = nf_tables_getrule, |
8501 | .type = NFNL_CB_RCU, | |
96518518 PM |
8502 | .attr_count = NFTA_RULE_MAX, |
8503 | .policy = nft_rule_policy, | |
8504 | }, | |
8daa8fde PS |
8505 | [NFT_MSG_GETRULE_RESET] = { |
8506 | .call = nf_tables_getrule, | |
8507 | .type = NFNL_CB_RCU, | |
8508 | .attr_count = NFTA_RULE_MAX, | |
8509 | .policy = nft_rule_policy, | |
8510 | }, | |
96518518 | 8511 | [NFT_MSG_DELRULE] = { |
50f2db9e PNA |
8512 | .call = nf_tables_delrule, |
8513 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8514 | .attr_count = NFTA_RULE_MAX, |
8515 | .policy = nft_rule_policy, | |
8516 | }, | |
f80a612d FFM |
8517 | [NFT_MSG_DESTROYRULE] = { |
8518 | .call = nf_tables_delrule, | |
8519 | .type = NFNL_CB_BATCH, | |
8520 | .attr_count = NFTA_RULE_MAX, | |
8521 | .policy = nft_rule_policy, | |
8522 | }, | |
20a69341 | 8523 | [NFT_MSG_NEWSET] = { |
50f2db9e PNA |
8524 | .call = nf_tables_newset, |
8525 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8526 | .attr_count = NFTA_SET_MAX, |
8527 | .policy = nft_set_policy, | |
8528 | }, | |
8529 | [NFT_MSG_GETSET] = { | |
50f2db9e PNA |
8530 | .call = nf_tables_getset, |
8531 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8532 | .attr_count = NFTA_SET_MAX, |
8533 | .policy = nft_set_policy, | |
8534 | }, | |
8535 | [NFT_MSG_DELSET] = { | |
50f2db9e PNA |
8536 | .call = nf_tables_delset, |
8537 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8538 | .attr_count = NFTA_SET_MAX, |
8539 | .policy = nft_set_policy, | |
8540 | }, | |
f80a612d FFM |
8541 | [NFT_MSG_DESTROYSET] = { |
8542 | .call = nf_tables_delset, | |
8543 | .type = NFNL_CB_BATCH, | |
8544 | .attr_count = NFTA_SET_MAX, | |
8545 | .policy = nft_set_policy, | |
8546 | }, | |
20a69341 | 8547 | [NFT_MSG_NEWSETELEM] = { |
50f2db9e PNA |
8548 | .call = nf_tables_newsetelem, |
8549 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8550 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8551 | .policy = nft_set_elem_list_policy, | |
8552 | }, | |
8553 | [NFT_MSG_GETSETELEM] = { | |
50f2db9e PNA |
8554 | .call = nf_tables_getsetelem, |
8555 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8556 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8557 | .policy = nft_set_elem_list_policy, | |
8558 | }, | |
8559 | [NFT_MSG_DELSETELEM] = { | |
50f2db9e PNA |
8560 | .call = nf_tables_delsetelem, |
8561 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8562 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8563 | .policy = nft_set_elem_list_policy, | |
8564 | }, | |
f80a612d FFM |
8565 | [NFT_MSG_DESTROYSETELEM] = { |
8566 | .call = nf_tables_delsetelem, | |
8567 | .type = NFNL_CB_BATCH, | |
8568 | .attr_count = NFTA_SET_ELEM_LIST_MAX, | |
8569 | .policy = nft_set_elem_list_policy, | |
8570 | }, | |
84d7fce6 | 8571 | [NFT_MSG_GETGEN] = { |
50f2db9e PNA |
8572 | .call = nf_tables_getgen, |
8573 | .type = NFNL_CB_RCU, | |
84d7fce6 | 8574 | }, |
e5009240 | 8575 | [NFT_MSG_NEWOBJ] = { |
50f2db9e PNA |
8576 | .call = nf_tables_newobj, |
8577 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8578 | .attr_count = NFTA_OBJ_MAX, |
8579 | .policy = nft_obj_policy, | |
8580 | }, | |
8581 | [NFT_MSG_GETOBJ] = { | |
50f2db9e PNA |
8582 | .call = nf_tables_getobj, |
8583 | .type = NFNL_CB_RCU, | |
e5009240 PNA |
8584 | .attr_count = NFTA_OBJ_MAX, |
8585 | .policy = nft_obj_policy, | |
8586 | }, | |
8587 | [NFT_MSG_DELOBJ] = { | |
50f2db9e PNA |
8588 | .call = nf_tables_delobj, |
8589 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8590 | .attr_count = NFTA_OBJ_MAX, |
8591 | .policy = nft_obj_policy, | |
8592 | }, | |
f80a612d FFM |
8593 | [NFT_MSG_DESTROYOBJ] = { |
8594 | .call = nf_tables_delobj, | |
8595 | .type = NFNL_CB_BATCH, | |
8596 | .attr_count = NFTA_OBJ_MAX, | |
8597 | .policy = nft_obj_policy, | |
8598 | }, | |
43da04a5 | 8599 | [NFT_MSG_GETOBJ_RESET] = { |
50f2db9e PNA |
8600 | .call = nf_tables_getobj, |
8601 | .type = NFNL_CB_RCU, | |
43da04a5 PNA |
8602 | .attr_count = NFTA_OBJ_MAX, |
8603 | .policy = nft_obj_policy, | |
8604 | }, | |
3b49e2e9 | 8605 | [NFT_MSG_NEWFLOWTABLE] = { |
50f2db9e PNA |
8606 | .call = nf_tables_newflowtable, |
8607 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8608 | .attr_count = NFTA_FLOWTABLE_MAX, |
8609 | .policy = nft_flowtable_policy, | |
8610 | }, | |
8611 | [NFT_MSG_GETFLOWTABLE] = { | |
50f2db9e PNA |
8612 | .call = nf_tables_getflowtable, |
8613 | .type = NFNL_CB_RCU, | |
3b49e2e9 PNA |
8614 | .attr_count = NFTA_FLOWTABLE_MAX, |
8615 | .policy = nft_flowtable_policy, | |
8616 | }, | |
8617 | [NFT_MSG_DELFLOWTABLE] = { | |
50f2db9e PNA |
8618 | .call = nf_tables_delflowtable, |
8619 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8620 | .attr_count = NFTA_FLOWTABLE_MAX, |
8621 | .policy = nft_flowtable_policy, | |
8622 | }, | |
f80a612d FFM |
8623 | [NFT_MSG_DESTROYFLOWTABLE] = { |
8624 | .call = nf_tables_delflowtable, | |
8625 | .type = NFNL_CB_BATCH, | |
8626 | .attr_count = NFTA_FLOWTABLE_MAX, | |
8627 | .policy = nft_flowtable_policy, | |
8628 | }, | |
96518518 PM |
8629 | }; |
8630 | ||
a654de8f PNA |
8631 | static int nf_tables_validate(struct net *net) |
8632 | { | |
d59d2f82 | 8633 | struct nftables_pernet *nft_net = nft_pernet(net); |
a654de8f PNA |
8634 | struct nft_table *table; |
8635 | ||
0854db2a | 8636 | switch (nft_net->validate_state) { |
a654de8f PNA |
8637 | case NFT_VALIDATE_SKIP: |
8638 | break; | |
8639 | case NFT_VALIDATE_NEED: | |
8640 | nft_validate_state_update(net, NFT_VALIDATE_DO); | |
954d8297 | 8641 | fallthrough; |
a654de8f | 8642 | case NFT_VALIDATE_DO: |
0854db2a | 8643 | list_for_each_entry(table, &nft_net->tables, list) { |
a654de8f PNA |
8644 | if (nft_table_validate(net, table) < 0) |
8645 | return -EAGAIN; | |
8646 | } | |
8647 | break; | |
8648 | } | |
8649 | ||
8650 | return 0; | |
8651 | } | |
8652 | ||
66293c46 FW |
8653 | /* a drop policy has to be deferred until all rules have been activated, |
8654 | * otherwise a large ruleset that contains a drop-policy base chain will | |
8655 | * cause all packets to get dropped until the full transaction has been | |
8656 | * processed. | |
8657 | * | |
8658 | * We defer the drop policy until the transaction has been finalized. | |
8659 | */ | |
8660 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
8661 | { | |
8662 | struct nft_base_chain *basechain; | |
8663 | ||
8664 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
8665 | return; | |
8666 | ||
8667 | if (!nft_is_base_chain(trans->ctx.chain)) | |
8668 | return; | |
8669 | ||
8670 | basechain = nft_base_chain(trans->ctx.chain); | |
8671 | basechain->policy = NF_DROP; | |
8672 | } | |
8673 | ||
91c7b38d PNA |
8674 | static void nft_chain_commit_update(struct nft_trans *trans) |
8675 | { | |
8676 | struct nft_base_chain *basechain; | |
8677 | ||
1b2470e5 FW |
8678 | if (nft_trans_chain_name(trans)) { |
8679 | rhltable_remove(&trans->ctx.table->chains_ht, | |
8680 | &trans->ctx.chain->rhlhead, | |
8681 | nft_chain_ht_params); | |
d71efb59 | 8682 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
8683 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
8684 | trans->ctx.chain->name, | |
8685 | &trans->ctx.chain->rhlhead, | |
8686 | nft_chain_ht_params); | |
8687 | } | |
91c7b38d | 8688 | |
f323d954 | 8689 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
8690 | return; |
8691 | ||
53315ac6 FW |
8692 | nft_chain_stats_replace(trans); |
8693 | ||
91c7b38d | 8694 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
8695 | |
8696 | switch (nft_trans_chain_policy(trans)) { | |
8697 | case NF_DROP: | |
8698 | case NF_ACCEPT: | |
8699 | basechain->policy = nft_trans_chain_policy(trans); | |
8700 | break; | |
8701 | } | |
8702 | } | |
8703 | ||
d62d0ba9 FFM |
8704 | static void nft_obj_commit_update(struct nft_trans *trans) |
8705 | { | |
8706 | struct nft_object *newobj; | |
8707 | struct nft_object *obj; | |
8708 | ||
8709 | obj = nft_trans_obj(trans); | |
8710 | newobj = nft_trans_obj_newobj(trans); | |
8711 | ||
9fedd894 FFM |
8712 | if (obj->ops->update) |
8713 | obj->ops->update(obj, newobj); | |
d62d0ba9 | 8714 | |
dad3bdee | 8715 | nft_obj_destroy(&trans->ctx, newobj); |
d62d0ba9 FFM |
8716 | } |
8717 | ||
2f99aa31 | 8718 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 8719 | { |
c7c32e72 PNA |
8720 | switch (trans->msg_type) { |
8721 | case NFT_MSG_DELTABLE: | |
f80a612d | 8722 | case NFT_MSG_DESTROYTABLE: |
c7c32e72 PNA |
8723 | nf_tables_table_destroy(&trans->ctx); |
8724 | break; | |
9f8aac0b | 8725 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 8726 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
8727 | kfree(nft_trans_chain_name(trans)); |
8728 | break; | |
c7c32e72 | 8729 | case NFT_MSG_DELCHAIN: |
f80a612d | 8730 | case NFT_MSG_DESTROYCHAIN: |
43a605f2 | 8731 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
8732 | break; |
8733 | case NFT_MSG_DELRULE: | |
f80a612d | 8734 | case NFT_MSG_DESTROYRULE: |
c7c32e72 PNA |
8735 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); |
8736 | break; | |
8737 | case NFT_MSG_DELSET: | |
f80a612d | 8738 | case NFT_MSG_DESTROYSET: |
0c2a85ed | 8739 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 8740 | break; |
61edafbb | 8741 | case NFT_MSG_DELSETELEM: |
f80a612d | 8742 | case NFT_MSG_DESTROYSETELEM: |
3453c927 PNA |
8743 | nf_tables_set_elem_destroy(&trans->ctx, |
8744 | nft_trans_elem_set(trans), | |
59105446 | 8745 | nft_trans_elem(trans).priv); |
61edafbb | 8746 | break; |
e5009240 | 8747 | case NFT_MSG_DELOBJ: |
f80a612d | 8748 | case NFT_MSG_DESTROYOBJ: |
00bfb320 | 8749 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 8750 | break; |
3b49e2e9 | 8751 | case NFT_MSG_DELFLOWTABLE: |
f80a612d | 8752 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 PNA |
8753 | if (nft_trans_flowtable_update(trans)) |
8754 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
8755 | else | |
8756 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 8757 | break; |
c7c32e72 | 8758 | } |
0935d558 FW |
8759 | |
8760 | if (trans->put_net) | |
8761 | put_net(trans->ctx.net); | |
8762 | ||
c7c32e72 PNA |
8763 | kfree(trans); |
8764 | } | |
8765 | ||
0935d558 | 8766 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
8767 | { |
8768 | struct nft_trans *trans, *next; | |
0935d558 FW |
8769 | LIST_HEAD(head); |
8770 | ||
8771 | spin_lock(&nf_tables_destroy_list_lock); | |
8772 | list_splice_init(&nf_tables_destroy_list, &head); | |
8773 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 8774 | |
0935d558 | 8775 | if (list_empty(&head)) |
2f99aa31 FW |
8776 | return; |
8777 | ||
8778 | synchronize_rcu(); | |
8779 | ||
0935d558 | 8780 | list_for_each_entry_safe(trans, next, &head, list) { |
2f99aa31 FW |
8781 | list_del(&trans->list); |
8782 | nft_commit_release(trans); | |
8783 | } | |
8784 | } | |
8785 | ||
ffe8923f FW |
8786 | void nf_tables_trans_destroy_flush_work(void) |
8787 | { | |
8788 | flush_work(&trans_destroy_work); | |
8789 | } | |
8790 | EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work); | |
8791 | ||
ed5f85d4 PNA |
8792 | static bool nft_expr_reduce(struct nft_regs_track *track, |
8793 | const struct nft_expr *expr) | |
8794 | { | |
9e539c5b | 8795 | return false; |
ed5f85d4 PNA |
8796 | } |
8797 | ||
0cbc06b3 FW |
8798 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
8799 | { | |
2c865a8a | 8800 | const struct nft_expr *expr, *last; |
12e4ecfa | 8801 | struct nft_regs_track track = {}; |
2c865a8a PNA |
8802 | unsigned int size, data_size; |
8803 | void *data, *data_boundary; | |
8804 | struct nft_rule_dp *prule; | |
0cbc06b3 | 8805 | struct nft_rule *rule; |
0cbc06b3 FW |
8806 | |
8807 | /* already handled or inactive chain? */ | |
2c865a8a | 8808 | if (chain->blob_next || !nft_is_active_next(net, chain)) |
0cbc06b3 FW |
8809 | return 0; |
8810 | ||
63045bfd | 8811 | data_size = 0; |
10377d42 | 8812 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
8813 | if (nft_is_active_next(net, rule)) { |
8814 | data_size += sizeof(*prule) + rule->dlen; | |
8815 | if (data_size > INT_MAX) | |
8816 | return -ENOMEM; | |
8817 | } | |
0cbc06b3 | 8818 | } |
2c865a8a | 8819 | data_size += offsetof(struct nft_rule_dp, data); /* last rule */ |
0cbc06b3 | 8820 | |
2c865a8a PNA |
8821 | chain->blob_next = nf_tables_chain_alloc_rules(data_size); |
8822 | if (!chain->blob_next) | |
0cbc06b3 FW |
8823 | return -ENOMEM; |
8824 | ||
2c865a8a PNA |
8825 | data = (void *)chain->blob_next->data; |
8826 | data_boundary = data + data_size; | |
8827 | size = 0; | |
8828 | ||
10377d42 | 8829 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
8830 | if (!nft_is_active_next(net, rule)) |
8831 | continue; | |
8832 | ||
8833 | prule = (struct nft_rule_dp *)data; | |
8834 | data += offsetof(struct nft_rule_dp, data); | |
8835 | if (WARN_ON_ONCE(data > data_boundary)) | |
8836 | return -ENOMEM; | |
8837 | ||
12e4ecfa | 8838 | size = 0; |
fe75e84a | 8839 | track.last = nft_expr_last(rule); |
2c865a8a | 8840 | nft_rule_for_each_expr(expr, last, rule) { |
12e4ecfa PNA |
8841 | track.cur = expr; |
8842 | ||
ed5f85d4 | 8843 | if (nft_expr_reduce(&track, expr)) { |
12e4ecfa PNA |
8844 | expr = track.cur; |
8845 | continue; | |
8846 | } | |
8847 | ||
2c865a8a PNA |
8848 | if (WARN_ON_ONCE(data + expr->ops->size > data_boundary)) |
8849 | return -ENOMEM; | |
8850 | ||
8851 | memcpy(data + size, expr, expr->ops->size); | |
8852 | size += expr->ops->size; | |
8853 | } | |
8854 | if (WARN_ON_ONCE(size >= 1 << 12)) | |
8855 | return -ENOMEM; | |
8856 | ||
8857 | prule->handle = rule->handle; | |
8858 | prule->dlen = size; | |
8859 | prule->is_last = 0; | |
8860 | ||
8861 | data += size; | |
8862 | size = 0; | |
8863 | chain->blob_next->size += (unsigned long)(data - (void *)prule); | |
0cbc06b3 FW |
8864 | } |
8865 | ||
2c865a8a PNA |
8866 | prule = (struct nft_rule_dp *)data; |
8867 | data += offsetof(struct nft_rule_dp, data); | |
8868 | if (WARN_ON_ONCE(data > data_boundary)) | |
8869 | return -ENOMEM; | |
8870 | ||
8871 | nft_last_rule(chain->blob_next, prule); | |
8872 | ||
0cbc06b3 FW |
8873 | return 0; |
8874 | } | |
8875 | ||
8876 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
8877 | { | |
d59d2f82 | 8878 | struct nftables_pernet *nft_net = nft_pernet(net); |
0cbc06b3 FW |
8879 | struct nft_trans *trans, *next; |
8880 | ||
0854db2a | 8881 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 FW |
8882 | struct nft_chain *chain = trans->ctx.chain; |
8883 | ||
8884 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
8885 | trans->msg_type == NFT_MSG_DELRULE) { | |
2c865a8a PNA |
8886 | kvfree(chain->blob_next); |
8887 | chain->blob_next = NULL; | |
0cbc06b3 FW |
8888 | } |
8889 | } | |
8890 | } | |
8891 | ||
8892 | static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h) | |
8893 | { | |
8894 | struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); | |
8895 | ||
2c865a8a | 8896 | kvfree(o->blob); |
0cbc06b3 FW |
8897 | } |
8898 | ||
2c865a8a | 8899 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob) |
0cbc06b3 | 8900 | { |
0cbc06b3 FW |
8901 | struct nft_rules_old *old; |
8902 | ||
2c865a8a PNA |
8903 | /* rcu_head is after end marker */ |
8904 | old = (void *)blob + sizeof(*blob) + blob->size; | |
8905 | old->blob = blob; | |
0cbc06b3 FW |
8906 | |
8907 | call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old); | |
8908 | } | |
8909 | ||
0fb39bbe | 8910 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 | 8911 | { |
2c865a8a | 8912 | struct nft_rule_blob *g0, *g1; |
0cbc06b3 FW |
8913 | bool next_genbit; |
8914 | ||
8915 | next_genbit = nft_gencursor_next(net); | |
8916 | ||
2c865a8a | 8917 | g0 = rcu_dereference_protected(chain->blob_gen_0, |
f102d66b | 8918 | lockdep_commit_lock_is_held(net)); |
2c865a8a | 8919 | g1 = rcu_dereference_protected(chain->blob_gen_1, |
f102d66b | 8920 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
8921 | |
8922 | /* No changes to this chain? */ | |
2c865a8a | 8923 | if (chain->blob_next == NULL) { |
0cbc06b3 FW |
8924 | /* chain had no change in last or next generation */ |
8925 | if (g0 == g1) | |
8926 | return; | |
8927 | /* | |
8928 | * chain had no change in this generation; make sure next | |
8929 | * one uses same rules as current generation. | |
8930 | */ | |
8931 | if (next_genbit) { | |
2c865a8a | 8932 | rcu_assign_pointer(chain->blob_gen_1, g0); |
0cbc06b3 FW |
8933 | nf_tables_commit_chain_free_rules_old(g1); |
8934 | } else { | |
2c865a8a | 8935 | rcu_assign_pointer(chain->blob_gen_0, g1); |
0cbc06b3 FW |
8936 | nf_tables_commit_chain_free_rules_old(g0); |
8937 | } | |
8938 | ||
8939 | return; | |
8940 | } | |
8941 | ||
8942 | if (next_genbit) | |
2c865a8a | 8943 | rcu_assign_pointer(chain->blob_gen_1, chain->blob_next); |
0cbc06b3 | 8944 | else |
2c865a8a | 8945 | rcu_assign_pointer(chain->blob_gen_0, chain->blob_next); |
0cbc06b3 | 8946 | |
2c865a8a | 8947 | chain->blob_next = NULL; |
0cbc06b3 FW |
8948 | |
8949 | if (g0 == g1) | |
8950 | return; | |
8951 | ||
8952 | if (next_genbit) | |
8953 | nf_tables_commit_chain_free_rules_old(g1); | |
8954 | else | |
8955 | nf_tables_commit_chain_free_rules_old(g0); | |
8956 | } | |
8957 | ||
d152159b FW |
8958 | static void nft_obj_del(struct nft_object *obj) |
8959 | { | |
4d44175a | 8960 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
8961 | list_del_rcu(&obj->list); |
8962 | } | |
8963 | ||
d0e2c7de | 8964 | void nft_chain_del(struct nft_chain *chain) |
1b2470e5 FW |
8965 | { |
8966 | struct nft_table *table = chain->table; | |
8967 | ||
8968 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
8969 | nft_chain_ht_params)); | |
8970 | list_del_rcu(&chain->list); | |
8971 | } | |
8972 | ||
eb014de4 PNA |
8973 | static void nf_tables_module_autoload_cleanup(struct net *net) |
8974 | { | |
d59d2f82 | 8975 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
8976 | struct nft_module_request *req, *next; |
8977 | ||
0854db2a FW |
8978 | WARN_ON_ONCE(!list_empty(&nft_net->commit_list)); |
8979 | list_for_each_entry_safe(req, next, &nft_net->module_list, list) { | |
eb014de4 PNA |
8980 | WARN_ON_ONCE(!req->done); |
8981 | list_del(&req->list); | |
8982 | kfree(req); | |
8983 | } | |
8984 | } | |
8985 | ||
0935d558 FW |
8986 | static void nf_tables_commit_release(struct net *net) |
8987 | { | |
d59d2f82 | 8988 | struct nftables_pernet *nft_net = nft_pernet(net); |
0935d558 FW |
8989 | struct nft_trans *trans; |
8990 | ||
8991 | /* all side effects have to be made visible. | |
8992 | * For example, if a chain named 'foo' has been deleted, a | |
8993 | * new transaction must not find it anymore. | |
8994 | * | |
8995 | * Memory reclaim happens asynchronously from work queue | |
8996 | * to prevent expensive synchronize_rcu() in commit phase. | |
8997 | */ | |
0854db2a | 8998 | if (list_empty(&nft_net->commit_list)) { |
eb014de4 | 8999 | nf_tables_module_autoload_cleanup(net); |
0854db2a | 9000 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
9001 | return; |
9002 | } | |
9003 | ||
0854db2a | 9004 | trans = list_last_entry(&nft_net->commit_list, |
0935d558 FW |
9005 | struct nft_trans, list); |
9006 | get_net(trans->ctx.net); | |
9007 | WARN_ON_ONCE(trans->put_net); | |
9008 | ||
9009 | trans->put_net = true; | |
9010 | spin_lock(&nf_tables_destroy_list_lock); | |
0854db2a | 9011 | list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list); |
0935d558 FW |
9012 | spin_unlock(&nf_tables_destroy_list_lock); |
9013 | ||
eb014de4 | 9014 | nf_tables_module_autoload_cleanup(net); |
0935d558 | 9015 | schedule_work(&trans_destroy_work); |
ffe8923f | 9016 | |
0854db2a | 9017 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
9018 | } |
9019 | ||
67cc570e PNA |
9020 | static void nft_commit_notify(struct net *net, u32 portid) |
9021 | { | |
d59d2f82 | 9022 | struct nftables_pernet *nft_net = nft_pernet(net); |
67cc570e PNA |
9023 | struct sk_buff *batch_skb = NULL, *nskb, *skb; |
9024 | unsigned char *data; | |
9025 | int len; | |
9026 | ||
0854db2a | 9027 | list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) { |
67cc570e PNA |
9028 | if (!batch_skb) { |
9029 | new_batch: | |
9030 | batch_skb = skb; | |
9031 | len = NLMSG_GOODSIZE - skb->len; | |
9032 | list_del(&skb->list); | |
9033 | continue; | |
9034 | } | |
9035 | len -= skb->len; | |
9036 | if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) { | |
9037 | data = skb_put(batch_skb, skb->len); | |
9038 | memcpy(data, skb->data, skb->len); | |
9039 | list_del(&skb->list); | |
9040 | kfree_skb(skb); | |
9041 | continue; | |
9042 | } | |
9043 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
9044 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
9045 | goto new_batch; | |
9046 | } | |
9047 | ||
9048 | if (batch_skb) { | |
9049 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
9050 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
9051 | } | |
9052 | ||
0854db2a | 9053 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); |
67cc570e PNA |
9054 | } |
9055 | ||
c520292f RGB |
9056 | static int nf_tables_commit_audit_alloc(struct list_head *adl, |
9057 | struct nft_table *table) | |
9058 | { | |
9059 | struct nft_audit_data *adp; | |
9060 | ||
9061 | list_for_each_entry(adp, adl, list) { | |
9062 | if (adp->table == table) | |
9063 | return 0; | |
9064 | } | |
9065 | adp = kzalloc(sizeof(*adp), GFP_KERNEL); | |
9066 | if (!adp) | |
9067 | return -ENOMEM; | |
9068 | adp->table = table; | |
9069 | list_add(&adp->list, adl); | |
9070 | return 0; | |
9071 | } | |
9072 | ||
cfbe3650 DM |
9073 | static void nf_tables_commit_audit_free(struct list_head *adl) |
9074 | { | |
9075 | struct nft_audit_data *adp, *adn; | |
9076 | ||
9077 | list_for_each_entry_safe(adp, adn, adl, list) { | |
9078 | list_del(&adp->list); | |
9079 | kfree(adp); | |
9080 | } | |
9081 | } | |
9082 | ||
c520292f RGB |
9083 | static void nf_tables_commit_audit_collect(struct list_head *adl, |
9084 | struct nft_table *table, u32 op) | |
9085 | { | |
9086 | struct nft_audit_data *adp; | |
9087 | ||
9088 | list_for_each_entry(adp, adl, list) { | |
9089 | if (adp->table == table) | |
9090 | goto found; | |
9091 | } | |
dadf33c9 | 9092 | WARN_ONCE(1, "table=%s not expected in commit list", table->name); |
c520292f RGB |
9093 | return; |
9094 | found: | |
9095 | adp->entries++; | |
9096 | if (!adp->op || adp->op > op) | |
9097 | adp->op = op; | |
9098 | } | |
9099 | ||
9100 | #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22) | |
9101 | ||
9102 | static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation) | |
9103 | { | |
9104 | struct nft_audit_data *adp, *adn; | |
9105 | char aubuf[AUNFTABLENAMELEN]; | |
9106 | ||
9107 | list_for_each_entry_safe(adp, adn, adl, list) { | |
9108 | snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name, | |
9109 | generation); | |
9110 | audit_log_nfcfg(aubuf, adp->table->family, adp->entries, | |
9111 | nft2audit_op[adp->op], GFP_KERNEL); | |
9112 | list_del(&adp->list); | |
9113 | kfree(adp); | |
9114 | } | |
9115 | } | |
9116 | ||
5913beaf | 9117 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 9118 | { |
d59d2f82 | 9119 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 9120 | struct nft_trans *trans, *next; |
a3716e70 | 9121 | struct nft_trans_elem *te; |
0cbc06b3 FW |
9122 | struct nft_chain *chain; |
9123 | struct nft_table *table; | |
34002783 | 9124 | unsigned int base_seq; |
c520292f | 9125 | LIST_HEAD(adl); |
c9626a2c | 9126 | int err; |
37082f93 | 9127 | |
0854db2a FW |
9128 | if (list_empty(&nft_net->commit_list)) { |
9129 | mutex_unlock(&nft_net->commit_mutex); | |
b8b27498 FW |
9130 | return 0; |
9131 | } | |
9132 | ||
a654de8f PNA |
9133 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
9134 | if (nf_tables_validate(net) < 0) | |
9135 | return -EAGAIN; | |
9136 | ||
c9626a2c PNA |
9137 | err = nft_flow_rule_offload_commit(net); |
9138 | if (err < 0) | |
9139 | return err; | |
9140 | ||
0cbc06b3 | 9141 | /* 1. Allocate space for next generation rules_gen_X[] */ |
0854db2a | 9142 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 | 9143 | int ret; |
37082f93 | 9144 | |
c520292f RGB |
9145 | ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table); |
9146 | if (ret) { | |
9147 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 9148 | nf_tables_commit_audit_free(&adl); |
c520292f RGB |
9149 | return ret; |
9150 | } | |
0cbc06b3 FW |
9151 | if (trans->msg_type == NFT_MSG_NEWRULE || |
9152 | trans->msg_type == NFT_MSG_DELRULE) { | |
9153 | chain = trans->ctx.chain; | |
9154 | ||
9155 | ret = nf_tables_commit_chain_prepare(net, chain); | |
9156 | if (ret < 0) { | |
9157 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 9158 | nf_tables_commit_audit_free(&adl); |
0cbc06b3 FW |
9159 | return ret; |
9160 | } | |
9161 | } | |
9162 | } | |
37082f93 | 9163 | |
0cbc06b3 | 9164 | /* step 2. Make rules_gen_X visible to packet path */ |
0854db2a | 9165 | list_for_each_entry(table, &nft_net->tables, list) { |
0fb39bbe FW |
9166 | list_for_each_entry(chain, &table->chains, list) |
9167 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
9168 | } |
9169 | ||
9170 | /* | |
9171 | * Bump generation counter, invalidate any dump in progress. | |
9172 | * Cannot fail after this point. | |
37082f93 | 9173 | */ |
34002783 PNA |
9174 | base_seq = READ_ONCE(nft_net->base_seq); |
9175 | while (++base_seq == 0) | |
0854db2a | 9176 | ; |
0cbc06b3 | 9177 | |
34002783 PNA |
9178 | WRITE_ONCE(nft_net->base_seq, base_seq); |
9179 | ||
0cbc06b3 FW |
9180 | /* step 3. Start new generation, rules_gen_X now in use. */ |
9181 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 | 9182 | |
0854db2a | 9183 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
c520292f RGB |
9184 | nf_tables_commit_audit_collect(&adl, trans->ctx.table, |
9185 | trans->msg_type); | |
b380e5c7 | 9186 | switch (trans->msg_type) { |
55dd6f93 PNA |
9187 | case NFT_MSG_NEWTABLE: |
9188 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
9189 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
9190 | nft_trans_destroy(trans); | |
9191 | break; | |
9192 | } | |
9193 | if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT) | |
0ce7cf41 PNA |
9194 | nf_tables_table_disable(net, trans->ctx.table); |
9195 | ||
179d9ba5 | 9196 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; |
55dd6f93 | 9197 | } else { |
f2a6d766 | 9198 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 9199 | } |
35151d84 | 9200 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
9201 | nft_trans_destroy(trans); |
9202 | break; | |
9203 | case NFT_MSG_DELTABLE: | |
f80a612d | 9204 | case NFT_MSG_DESTROYTABLE: |
f2a6d766 | 9205 | list_del_rcu(&trans->ctx.table->list); |
f80a612d | 9206 | nf_tables_table_notify(&trans->ctx, trans->msg_type); |
55dd6f93 | 9207 | break; |
91c7b38d | 9208 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 9209 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 9210 | nft_chain_commit_update(trans); |
9f8aac0b FW |
9211 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
9212 | /* trans destroyed after rcu grace period */ | |
9213 | } else { | |
66293c46 | 9214 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 9215 | nft_clear(net, trans->ctx.chain); |
9f8aac0b FW |
9216 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
9217 | nft_trans_destroy(trans); | |
9218 | } | |
91c7b38d PNA |
9219 | break; |
9220 | case NFT_MSG_DELCHAIN: | |
f80a612d | 9221 | case NFT_MSG_DESTROYCHAIN: |
1b2470e5 | 9222 | nft_chain_del(trans->ctx.chain); |
f80a612d | 9223 | nf_tables_chain_notify(&trans->ctx, trans->msg_type); |
c974a3a3 PNA |
9224 | nf_tables_unregister_hook(trans->ctx.net, |
9225 | trans->ctx.table, | |
9226 | trans->ctx.chain); | |
91c7b38d | 9227 | break; |
b380e5c7 | 9228 | case NFT_MSG_NEWRULE: |
889f7ee7 | 9229 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 9230 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 9231 | nft_trans_rule(trans), |
35151d84 | 9232 | NFT_MSG_NEWRULE); |
9dd732e0 PNA |
9233 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9234 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9235 | ||
37082f93 | 9236 | nft_trans_destroy(trans); |
b380e5c7 PNA |
9237 | break; |
9238 | case NFT_MSG_DELRULE: | |
f80a612d | 9239 | case NFT_MSG_DESTROYRULE: |
b380e5c7 | 9240 | list_del_rcu(&nft_trans_rule(trans)->list); |
35151d84 PNA |
9241 | nf_tables_rule_notify(&trans->ctx, |
9242 | nft_trans_rule(trans), | |
f80a612d | 9243 | trans->msg_type); |
f6ac8585 PNA |
9244 | nft_rule_expr_deactivate(&trans->ctx, |
9245 | nft_trans_rule(trans), | |
9246 | NFT_TRANS_COMMIT); | |
26b5934f PNA |
9247 | |
9248 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) | |
9249 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 | 9250 | break; |
958bee14 | 9251 | case NFT_MSG_NEWSET: |
123b9961 PNA |
9252 | if (nft_trans_set_update(trans)) { |
9253 | struct nft_set *set = nft_trans_set(trans); | |
4fefee57 | 9254 | |
123b9961 PNA |
9255 | WRITE_ONCE(set->timeout, nft_trans_set_timeout(trans)); |
9256 | WRITE_ONCE(set->gc_int, nft_trans_set_gc_int(trans)); | |
9257 | } else { | |
9258 | nft_clear(net, nft_trans_set(trans)); | |
9259 | /* This avoids hitting -EBUSY when deleting the table | |
9260 | * from the transaction. | |
9261 | */ | |
9262 | if (nft_set_is_anonymous(nft_trans_set(trans)) && | |
9263 | !list_empty(&nft_trans_set(trans)->bindings)) | |
9264 | trans->ctx.table->use--; | |
9265 | } | |
958bee14 | 9266 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 9267 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
9268 | nft_trans_destroy(trans); |
9269 | break; | |
9270 | case NFT_MSG_DELSET: | |
f80a612d | 9271 | case NFT_MSG_DESTROYSET: |
37a9cc52 | 9272 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 9273 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
f80a612d | 9274 | trans->msg_type, GFP_KERNEL); |
958bee14 | 9275 | break; |
60319eb1 | 9276 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
9277 | te = (struct nft_trans_elem *)trans->data; |
9278 | ||
aaa31047 | 9279 | nft_setelem_activate(net, te->set, &te->elem); |
cc02e457 PM |
9280 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9281 | &te->elem, | |
6fb721cf | 9282 | NFT_MSG_NEWSETELEM); |
60319eb1 PNA |
9283 | nft_trans_destroy(trans); |
9284 | break; | |
9285 | case NFT_MSG_DELSETELEM: | |
f80a612d | 9286 | case NFT_MSG_DESTROYSETELEM: |
a3716e70 | 9287 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 9288 | |
a3716e70 PNA |
9289 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9290 | &te->elem, | |
f80a612d | 9291 | trans->msg_type); |
aaa31047 PNA |
9292 | nft_setelem_remove(net, te->set, &te->elem); |
9293 | if (!nft_setelem_is_catchall(te->set, &te->elem)) { | |
9294 | atomic_dec(&te->set->nelems); | |
9295 | te->set->ndeact--; | |
9296 | } | |
60319eb1 | 9297 | break; |
e5009240 | 9298 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
9299 | if (nft_trans_obj_update(trans)) { |
9300 | nft_obj_commit_update(trans); | |
9301 | nf_tables_obj_notify(&trans->ctx, | |
9302 | nft_trans_obj(trans), | |
9303 | NFT_MSG_NEWOBJ); | |
9304 | } else { | |
9305 | nft_clear(net, nft_trans_obj(trans)); | |
9306 | nf_tables_obj_notify(&trans->ctx, | |
9307 | nft_trans_obj(trans), | |
9308 | NFT_MSG_NEWOBJ); | |
9309 | nft_trans_destroy(trans); | |
9310 | } | |
e5009240 PNA |
9311 | break; |
9312 | case NFT_MSG_DELOBJ: | |
f80a612d | 9313 | case NFT_MSG_DESTROYOBJ: |
d152159b | 9314 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 | 9315 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
f80a612d | 9316 | trans->msg_type); |
e5009240 | 9317 | break; |
3b49e2e9 | 9318 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 9319 | if (nft_trans_flowtable_update(trans)) { |
7b35582c PNA |
9320 | nft_trans_flowtable(trans)->data.flags = |
9321 | nft_trans_flowtable_flags(trans); | |
78d9f48f PNA |
9322 | nf_tables_flowtable_notify(&trans->ctx, |
9323 | nft_trans_flowtable(trans), | |
9324 | &nft_trans_flowtable_hooks(trans), | |
9325 | NFT_MSG_NEWFLOWTABLE); | |
9326 | list_splice(&nft_trans_flowtable_hooks(trans), | |
9327 | &nft_trans_flowtable(trans)->hook_list); | |
9328 | } else { | |
9329 | nft_clear(net, nft_trans_flowtable(trans)); | |
9330 | nf_tables_flowtable_notify(&trans->ctx, | |
9331 | nft_trans_flowtable(trans), | |
9332 | &nft_trans_flowtable(trans)->hook_list, | |
9333 | NFT_MSG_NEWFLOWTABLE); | |
9334 | } | |
3b49e2e9 PNA |
9335 | nft_trans_destroy(trans); |
9336 | break; | |
9337 | case NFT_MSG_DELFLOWTABLE: | |
f80a612d | 9338 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 | 9339 | if (nft_trans_flowtable_update(trans)) { |
abadb2f8 PNA |
9340 | nf_tables_flowtable_notify(&trans->ctx, |
9341 | nft_trans_flowtable(trans), | |
9342 | &nft_trans_flowtable_hooks(trans), | |
f80a612d | 9343 | trans->msg_type); |
abadb2f8 PNA |
9344 | nft_unregister_flowtable_net_hooks(net, |
9345 | &nft_trans_flowtable_hooks(trans)); | |
9346 | } else { | |
9347 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9348 | nf_tables_flowtable_notify(&trans->ctx, | |
9349 | nft_trans_flowtable(trans), | |
9350 | &nft_trans_flowtable(trans)->hook_list, | |
f80a612d | 9351 | trans->msg_type); |
abadb2f8 PNA |
9352 | nft_unregister_flowtable_net_hooks(net, |
9353 | &nft_trans_flowtable(trans)->hook_list); | |
9354 | } | |
3b49e2e9 | 9355 | break; |
37082f93 | 9356 | } |
37082f93 PNA |
9357 | } |
9358 | ||
67cc570e | 9359 | nft_commit_notify(net, NETLINK_CB(skb).portid); |
84d7fce6 | 9360 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0854db2a | 9361 | nf_tables_commit_audit_log(&adl, nft_net->base_seq); |
0935d558 | 9362 | nf_tables_commit_release(net); |
37082f93 PNA |
9363 | |
9364 | return 0; | |
9365 | } | |
9366 | ||
eb014de4 PNA |
9367 | static void nf_tables_module_autoload(struct net *net) |
9368 | { | |
d59d2f82 | 9369 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
9370 | struct nft_module_request *req, *next; |
9371 | LIST_HEAD(module_list); | |
9372 | ||
0854db2a FW |
9373 | list_splice_init(&nft_net->module_list, &module_list); |
9374 | mutex_unlock(&nft_net->commit_mutex); | |
eb014de4 | 9375 | list_for_each_entry_safe(req, next, &module_list, list) { |
1d305ba4 FW |
9376 | request_module("%s", req->module); |
9377 | req->done = true; | |
eb014de4 | 9378 | } |
0854db2a FW |
9379 | mutex_lock(&nft_net->commit_mutex); |
9380 | list_splice(&module_list, &nft_net->module_list); | |
eb014de4 PNA |
9381 | } |
9382 | ||
b326dd37 | 9383 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 9384 | { |
c7c32e72 PNA |
9385 | switch (trans->msg_type) { |
9386 | case NFT_MSG_NEWTABLE: | |
9387 | nf_tables_table_destroy(&trans->ctx); | |
9388 | break; | |
9389 | case NFT_MSG_NEWCHAIN: | |
43a605f2 | 9390 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
9391 | break; |
9392 | case NFT_MSG_NEWRULE: | |
9393 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
9394 | break; | |
9395 | case NFT_MSG_NEWSET: | |
0c2a85ed | 9396 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 9397 | break; |
61edafbb PM |
9398 | case NFT_MSG_NEWSETELEM: |
9399 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 9400 | nft_trans_elem(trans).priv, true); |
61edafbb | 9401 | break; |
e5009240 | 9402 | case NFT_MSG_NEWOBJ: |
00bfb320 | 9403 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 9404 | break; |
3b49e2e9 | 9405 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9406 | if (nft_trans_flowtable_update(trans)) |
9407 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
9408 | else | |
9409 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 9410 | break; |
c7c32e72 PNA |
9411 | } |
9412 | kfree(trans); | |
9413 | } | |
9414 | ||
c0391b6a | 9415 | static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) |
37082f93 | 9416 | { |
d59d2f82 | 9417 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 9418 | struct nft_trans *trans, *next; |
02263db0 | 9419 | struct nft_trans_elem *te; |
37082f93 | 9420 | |
c0391b6a PNA |
9421 | if (action == NFNL_ABORT_VALIDATE && |
9422 | nf_tables_validate(net) < 0) | |
9423 | return -EAGAIN; | |
9424 | ||
0854db2a | 9425 | list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list, |
a907e36d | 9426 | list) { |
b380e5c7 | 9427 | switch (trans->msg_type) { |
55dd6f93 PNA |
9428 | case NFT_MSG_NEWTABLE: |
9429 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
9430 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
9431 | nft_trans_destroy(trans); | |
9432 | break; | |
9433 | } | |
9434 | if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) { | |
0ce7cf41 | 9435 | nf_tables_table_disable(net, trans->ctx.table); |
179d9ba5 PNA |
9436 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
9437 | } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) { | |
9438 | trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT; | |
9439 | } | |
9440 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; | |
55dd6f93 PNA |
9441 | nft_trans_destroy(trans); |
9442 | } else { | |
e688a7f8 | 9443 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
9444 | } |
9445 | break; | |
9446 | case NFT_MSG_DELTABLE: | |
f80a612d | 9447 | case NFT_MSG_DESTROYTABLE: |
f2a6d766 | 9448 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
9449 | nft_trans_destroy(trans); |
9450 | break; | |
91c7b38d PNA |
9451 | case NFT_MSG_NEWCHAIN: |
9452 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 9453 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 9454 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
9455 | nft_trans_destroy(trans); |
9456 | } else { | |
d0e2c7de PNA |
9457 | if (nft_chain_is_bound(trans->ctx.chain)) { |
9458 | nft_trans_destroy(trans); | |
9459 | break; | |
9460 | } | |
4fefee57 | 9461 | trans->ctx.table->use--; |
1b2470e5 | 9462 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
9463 | nf_tables_unregister_hook(trans->ctx.net, |
9464 | trans->ctx.table, | |
9465 | trans->ctx.chain); | |
91c7b38d PNA |
9466 | } |
9467 | break; | |
9468 | case NFT_MSG_DELCHAIN: | |
f80a612d | 9469 | case NFT_MSG_DESTROYCHAIN: |
4fefee57 | 9470 | trans->ctx.table->use++; |
664b0f8c | 9471 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
9472 | nft_trans_destroy(trans); |
9473 | break; | |
b380e5c7 | 9474 | case NFT_MSG_NEWRULE: |
4fefee57 | 9475 | trans->ctx.chain->use--; |
b380e5c7 | 9476 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
9477 | nft_rule_expr_deactivate(&trans->ctx, |
9478 | nft_trans_rule(trans), | |
9479 | NFT_TRANS_ABORT); | |
3c5e4462 PNA |
9480 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9481 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 PNA |
9482 | break; |
9483 | case NFT_MSG_DELRULE: | |
f80a612d | 9484 | case NFT_MSG_DESTROYRULE: |
4fefee57 | 9485 | trans->ctx.chain->use++; |
889f7ee7 | 9486 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 9487 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
3c5e4462 PNA |
9488 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9489 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9490 | ||
37082f93 | 9491 | nft_trans_destroy(trans); |
b380e5c7 | 9492 | break; |
958bee14 | 9493 | case NFT_MSG_NEWSET: |
123b9961 PNA |
9494 | if (nft_trans_set_update(trans)) { |
9495 | nft_trans_destroy(trans); | |
9496 | break; | |
9497 | } | |
4fefee57 | 9498 | trans->ctx.table->use--; |
6a0a8d10 | 9499 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
9500 | nft_trans_destroy(trans); |
9501 | break; | |
9502 | } | |
9503 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
9504 | break; |
9505 | case NFT_MSG_DELSET: | |
f80a612d | 9506 | case NFT_MSG_DESTROYSET: |
4fefee57 | 9507 | trans->ctx.table->use++; |
37a9cc52 | 9508 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
9509 | nft_trans_destroy(trans); |
9510 | break; | |
60319eb1 | 9511 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 9512 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
9513 | nft_trans_destroy(trans); |
9514 | break; | |
9515 | } | |
02263db0 | 9516 | te = (struct nft_trans_elem *)trans->data; |
aaa31047 PNA |
9517 | nft_setelem_remove(net, te->set, &te->elem); |
9518 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9519 | atomic_dec(&te->set->nelems); | |
60319eb1 PNA |
9520 | break; |
9521 | case NFT_MSG_DELSETELEM: | |
f80a612d | 9522 | case NFT_MSG_DESTROYSETELEM: |
cc02e457 PM |
9523 | te = (struct nft_trans_elem *)trans->data; |
9524 | ||
f8bb7889 | 9525 | nft_setelem_data_activate(net, te->set, &te->elem); |
aaa31047 PNA |
9526 | nft_setelem_activate(net, te->set, &te->elem); |
9527 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9528 | te->set->ndeact--; | |
cc02e457 | 9529 | |
e5009240 PNA |
9530 | nft_trans_destroy(trans); |
9531 | break; | |
9532 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 | 9533 | if (nft_trans_obj_update(trans)) { |
dad3bdee | 9534 | nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans)); |
d62d0ba9 FFM |
9535 | nft_trans_destroy(trans); |
9536 | } else { | |
9537 | trans->ctx.table->use--; | |
9538 | nft_obj_del(nft_trans_obj(trans)); | |
9539 | } | |
e5009240 PNA |
9540 | break; |
9541 | case NFT_MSG_DELOBJ: | |
f80a612d | 9542 | case NFT_MSG_DESTROYOBJ: |
e5009240 PNA |
9543 | trans->ctx.table->use++; |
9544 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
9545 | nft_trans_destroy(trans); |
9546 | break; | |
3b49e2e9 | 9547 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9548 | if (nft_trans_flowtable_update(trans)) { |
9549 | nft_unregister_flowtable_net_hooks(net, | |
9550 | &nft_trans_flowtable_hooks(trans)); | |
9551 | } else { | |
9552 | trans->ctx.table->use--; | |
9553 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9554 | nft_unregister_flowtable_net_hooks(net, | |
9555 | &nft_trans_flowtable(trans)->hook_list); | |
9556 | } | |
3b49e2e9 PNA |
9557 | break; |
9558 | case NFT_MSG_DELFLOWTABLE: | |
f80a612d | 9559 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 | 9560 | if (nft_trans_flowtable_update(trans)) { |
b6d9014a PNA |
9561 | list_splice(&nft_trans_flowtable_hooks(trans), |
9562 | &nft_trans_flowtable(trans)->hook_list); | |
abadb2f8 PNA |
9563 | } else { |
9564 | trans->ctx.table->use++; | |
9565 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
9566 | } | |
3b49e2e9 PNA |
9567 | nft_trans_destroy(trans); |
9568 | break; | |
37082f93 | 9569 | } |
37082f93 PNA |
9570 | } |
9571 | ||
b326dd37 PNA |
9572 | synchronize_rcu(); |
9573 | ||
a1cee076 | 9574 | list_for_each_entry_safe_reverse(trans, next, |
0854db2a | 9575 | &nft_net->commit_list, list) { |
c7c32e72 | 9576 | list_del(&trans->list); |
b326dd37 | 9577 | nf_tables_abort_release(trans); |
37082f93 PNA |
9578 | } |
9579 | ||
c0391b6a | 9580 | if (action == NFNL_ABORT_AUTOLOAD) |
eb014de4 PNA |
9581 | nf_tables_module_autoload(net); |
9582 | else | |
9583 | nf_tables_module_autoload_cleanup(net); | |
9584 | ||
37082f93 PNA |
9585 | return 0; |
9586 | } | |
9587 | ||
a654de8f PNA |
9588 | static void nf_tables_cleanup(struct net *net) |
9589 | { | |
9590 | nft_validate_state_update(net, NFT_VALIDATE_SKIP); | |
9591 | } | |
9592 | ||
c0391b6a PNA |
9593 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, |
9594 | enum nfnl_abort_action action) | |
71ad00c5 | 9595 | { |
d59d2f82 | 9596 | struct nftables_pernet *nft_net = nft_pernet(net); |
c0391b6a | 9597 | int ret = __nf_tables_abort(net, action); |
f102d66b | 9598 | |
0854db2a | 9599 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9600 | |
9601 | return ret; | |
71ad00c5 FW |
9602 | } |
9603 | ||
74e8bcd2 PNA |
9604 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
9605 | { | |
d59d2f82 | 9606 | struct nftables_pernet *nft_net = nft_pernet(net); |
f102d66b FW |
9607 | bool genid_ok; |
9608 | ||
0854db2a | 9609 | mutex_lock(&nft_net->commit_mutex); |
f102d66b | 9610 | |
0854db2a | 9611 | genid_ok = genid == 0 || nft_net->base_seq == genid; |
f102d66b | 9612 | if (!genid_ok) |
0854db2a | 9613 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9614 | |
9615 | /* else, commit mutex has to be released by commit or abort function */ | |
9616 | return genid_ok; | |
74e8bcd2 PNA |
9617 | } |
9618 | ||
96518518 PM |
9619 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
9620 | .name = "nf_tables", | |
9621 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
9622 | .cb_count = NFT_MSG_MAX, | |
9623 | .cb = nf_tables_cb, | |
0628b123 PNA |
9624 | .commit = nf_tables_commit, |
9625 | .abort = nf_tables_abort, | |
a654de8f | 9626 | .cleanup = nf_tables_cleanup, |
74e8bcd2 | 9627 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 9628 | .owner = THIS_MODULE, |
96518518 PM |
9629 | }; |
9630 | ||
7210e4e3 | 9631 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 9632 | enum nft_chain_types type) |
7210e4e3 PNA |
9633 | { |
9634 | const struct nft_base_chain *basechain; | |
9635 | ||
f323d954 | 9636 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
9637 | basechain = nft_base_chain(chain); |
9638 | if (basechain->type->type != type) | |
9639 | return -EOPNOTSUPP; | |
9640 | } | |
9641 | return 0; | |
9642 | } | |
9643 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
9644 | ||
75e8d06d PNA |
9645 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
9646 | unsigned int hook_flags) | |
9647 | { | |
9648 | struct nft_base_chain *basechain; | |
9649 | ||
f323d954 | 9650 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
9651 | basechain = nft_base_chain(chain); |
9652 | ||
c974a3a3 | 9653 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
9654 | return 0; |
9655 | ||
9656 | return -EOPNOTSUPP; | |
9657 | } | |
9658 | ||
9659 | return 0; | |
9660 | } | |
9661 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
9662 | ||
20a69341 PM |
9663 | /* |
9664 | * Loop detection - walk through the ruleset beginning at the destination chain | |
9665 | * of a new jump until either the source chain is reached (loop) or all | |
9666 | * reachable chains have been traversed. | |
9667 | * | |
9668 | * The loop check is performed whenever a new jump verdict is added to an | |
9669 | * expression or verdict map or a verdict map is bound to a new chain. | |
9670 | */ | |
9671 | ||
9672 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
9673 | const struct nft_chain *chain); | |
9674 | ||
6387aa6e PNA |
9675 | static int nft_check_loops(const struct nft_ctx *ctx, |
9676 | const struct nft_set_ext *ext) | |
9677 | { | |
9678 | const struct nft_data *data; | |
9679 | int ret; | |
9680 | ||
9681 | data = nft_set_ext_data(ext); | |
9682 | switch (data->verdict.code) { | |
9683 | case NFT_JUMP: | |
9684 | case NFT_GOTO: | |
9685 | ret = nf_tables_check_loops(ctx, data->verdict.chain); | |
9686 | break; | |
9687 | default: | |
9688 | ret = 0; | |
9689 | break; | |
9690 | } | |
9691 | ||
9692 | return ret; | |
9693 | } | |
9694 | ||
20a69341 | 9695 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, |
de70185d | 9696 | struct nft_set *set, |
20a69341 | 9697 | const struct nft_set_iter *iter, |
de70185d | 9698 | struct nft_set_elem *elem) |
20a69341 | 9699 | { |
fe2811eb | 9700 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
fe2811eb PM |
9701 | |
9702 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
9703 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
9704 | return 0; |
9705 | ||
6387aa6e | 9706 | return nft_check_loops(ctx, ext); |
20a69341 PM |
9707 | } |
9708 | ||
aaa31047 PNA |
9709 | static int nft_set_catchall_loops(const struct nft_ctx *ctx, |
9710 | struct nft_set *set) | |
9711 | { | |
9712 | u8 genmask = nft_genmask_next(ctx->net); | |
9713 | struct nft_set_elem_catchall *catchall; | |
9714 | struct nft_set_ext *ext; | |
9715 | int ret = 0; | |
9716 | ||
9717 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
9718 | ext = nft_set_elem_ext(set, catchall->elem); | |
9719 | if (!nft_set_elem_active(ext, genmask)) | |
9720 | continue; | |
9721 | ||
9722 | ret = nft_check_loops(ctx, ext); | |
9723 | if (ret < 0) | |
9724 | return ret; | |
9725 | } | |
9726 | ||
9727 | return ret; | |
9728 | } | |
9729 | ||
20a69341 PM |
9730 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
9731 | const struct nft_chain *chain) | |
9732 | { | |
9733 | const struct nft_rule *rule; | |
9734 | const struct nft_expr *expr, *last; | |
de70185d | 9735 | struct nft_set *set; |
20a69341 PM |
9736 | struct nft_set_binding *binding; |
9737 | struct nft_set_iter iter; | |
20a69341 PM |
9738 | |
9739 | if (ctx->chain == chain) | |
9740 | return -ELOOP; | |
9741 | ||
9742 | list_for_each_entry(rule, &chain->rules, list) { | |
9743 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
9744 | struct nft_immediate_expr *priv; |
9745 | const struct nft_data *data; | |
0ca743a5 PNA |
9746 | int err; |
9747 | ||
a654de8f | 9748 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
9749 | continue; |
9750 | ||
a654de8f PNA |
9751 | priv = nft_expr_priv(expr); |
9752 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 9753 | continue; |
20a69341 | 9754 | |
a654de8f | 9755 | data = &priv->data; |
1ca2e170 | 9756 | switch (data->verdict.code) { |
20a69341 PM |
9757 | case NFT_JUMP: |
9758 | case NFT_GOTO: | |
1ca2e170 PM |
9759 | err = nf_tables_check_loops(ctx, |
9760 | data->verdict.chain); | |
20a69341 PM |
9761 | if (err < 0) |
9762 | return err; | |
c2168e6b | 9763 | break; |
20a69341 PM |
9764 | default: |
9765 | break; | |
9766 | } | |
9767 | } | |
9768 | } | |
9769 | ||
9770 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
9771 | if (!nft_is_active_next(ctx->net, set)) |
9772 | continue; | |
20a69341 PM |
9773 | if (!(set->flags & NFT_SET_MAP) || |
9774 | set->dtype != NFT_DATA_VERDICT) | |
9775 | continue; | |
9776 | ||
9777 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
9778 | if (!(binding->flags & NFT_SET_MAP) || |
9779 | binding->chain != chain) | |
20a69341 PM |
9780 | continue; |
9781 | ||
8588ac09 | 9782 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
9783 | iter.skip = 0; |
9784 | iter.count = 0; | |
9785 | iter.err = 0; | |
9786 | iter.fn = nf_tables_loop_check_setelem; | |
9787 | ||
9788 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
9789 | if (!iter.err) |
9790 | iter.err = nft_set_catchall_loops(ctx, set); | |
9791 | ||
20a69341 PM |
9792 | if (iter.err < 0) |
9793 | return iter.err; | |
9794 | } | |
9795 | } | |
9796 | ||
9797 | return 0; | |
9798 | } | |
9799 | ||
36b701fa LGL |
9800 | /** |
9801 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
9802 | * | |
9803 | * @attr: netlink attribute to fetch value from | |
9804 | * @max: maximum value to be stored in dest | |
9805 | * @dest: pointer to the variable | |
9806 | * | |
9807 | * Parse, check and store a given u32 netlink attribute into variable. | |
9808 | * This function returns -ERANGE if the value goes over maximum value. | |
9809 | * Otherwise a 0 is returned and the attribute value is stored in the | |
9810 | * destination variable. | |
9811 | */ | |
f1d505bb | 9812 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 9813 | { |
09525a09 | 9814 | u32 val; |
36b701fa LGL |
9815 | |
9816 | val = ntohl(nla_get_be32(attr)); | |
9817 | if (val > max) | |
9818 | return -ERANGE; | |
9819 | ||
9820 | *dest = val; | |
9821 | return 0; | |
9822 | } | |
9823 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
9824 | ||
6c6f9f31 | 9825 | static int nft_parse_register(const struct nlattr *attr, u32 *preg) |
b1c96ed3 | 9826 | { |
49499c3e PM |
9827 | unsigned int reg; |
9828 | ||
9829 | reg = ntohl(nla_get_be32(attr)); | |
9830 | switch (reg) { | |
9831 | case NFT_REG_VERDICT...NFT_REG_4: | |
6e1acfa3 PNA |
9832 | *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE; |
9833 | break; | |
9834 | case NFT_REG32_00...NFT_REG32_15: | |
9835 | *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
9836 | break; | |
49499c3e | 9837 | default: |
6e1acfa3 | 9838 | return -ERANGE; |
49499c3e | 9839 | } |
6e1acfa3 PNA |
9840 | |
9841 | return 0; | |
b1c96ed3 | 9842 | } |
b1c96ed3 | 9843 | |
49499c3e PM |
9844 | /** |
9845 | * nft_dump_register - dump a register value to a netlink attribute | |
9846 | * | |
9847 | * @skb: socket buffer | |
9848 | * @attr: attribute number | |
9849 | * @reg: register number | |
9850 | * | |
9851 | * Construct a netlink attribute containing the register number. For | |
9852 | * compatibility reasons, register numbers being a multiple of 4 are | |
9853 | * translated to the corresponding 128 bit register numbers. | |
9854 | */ | |
b1c96ed3 PM |
9855 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
9856 | { | |
49499c3e PM |
9857 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
9858 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
9859 | else | |
9860 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
9861 | ||
b1c96ed3 PM |
9862 | return nla_put_be32(skb, attr, htonl(reg)); |
9863 | } | |
9864 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
9865 | ||
4f16d25c | 9866 | static int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 9867 | { |
49499c3e | 9868 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 9869 | return -EINVAL; |
d07db988 PM |
9870 | if (len == 0) |
9871 | return -EINVAL; | |
c593642c | 9872 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 9873 | return -ERANGE; |
49499c3e | 9874 | |
96518518 PM |
9875 | return 0; |
9876 | } | |
4f16d25c PNA |
9877 | |
9878 | int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len) | |
9879 | { | |
9880 | u32 reg; | |
9881 | int err; | |
9882 | ||
6e1acfa3 PNA |
9883 | err = nft_parse_register(attr, ®); |
9884 | if (err < 0) | |
9885 | return err; | |
9886 | ||
4f16d25c PNA |
9887 | err = nft_validate_register_load(reg, len); |
9888 | if (err < 0) | |
9889 | return err; | |
9890 | ||
9891 | *sreg = reg; | |
9892 | return 0; | |
9893 | } | |
9894 | EXPORT_SYMBOL_GPL(nft_parse_register_load); | |
96518518 | 9895 | |
345023b0 PNA |
9896 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
9897 | enum nft_registers reg, | |
9898 | const struct nft_data *data, | |
9899 | enum nft_data_types type, | |
9900 | unsigned int len) | |
96518518 | 9901 | { |
20a69341 PM |
9902 | int err; |
9903 | ||
96518518 PM |
9904 | switch (reg) { |
9905 | case NFT_REG_VERDICT: | |
58f40ab6 | 9906 | if (type != NFT_DATA_VERDICT) |
96518518 | 9907 | return -EINVAL; |
20a69341 | 9908 | |
58f40ab6 | 9909 | if (data != NULL && |
1ca2e170 PM |
9910 | (data->verdict.code == NFT_GOTO || |
9911 | data->verdict.code == NFT_JUMP)) { | |
9912 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
9913 | if (err < 0) |
9914 | return err; | |
20a69341 PM |
9915 | } |
9916 | ||
96518518 PM |
9917 | return 0; |
9918 | default: | |
49499c3e | 9919 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 9920 | return -EINVAL; |
45d9bcda PM |
9921 | if (len == 0) |
9922 | return -EINVAL; | |
49499c3e | 9923 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 9924 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 9925 | return -ERANGE; |
27e6d201 | 9926 | |
96518518 PM |
9927 | if (data != NULL && type != NFT_DATA_VALUE) |
9928 | return -EINVAL; | |
9929 | return 0; | |
9930 | } | |
9931 | } | |
345023b0 PNA |
9932 | |
9933 | int nft_parse_register_store(const struct nft_ctx *ctx, | |
9934 | const struct nlattr *attr, u8 *dreg, | |
9935 | const struct nft_data *data, | |
9936 | enum nft_data_types type, unsigned int len) | |
9937 | { | |
9938 | int err; | |
9939 | u32 reg; | |
9940 | ||
6e1acfa3 PNA |
9941 | err = nft_parse_register(attr, ®); |
9942 | if (err < 0) | |
9943 | return err; | |
9944 | ||
345023b0 PNA |
9945 | err = nft_validate_register_store(ctx, reg, data, type, len); |
9946 | if (err < 0) | |
9947 | return err; | |
9948 | ||
9949 | *dreg = reg; | |
9950 | return 0; | |
9951 | } | |
9952 | EXPORT_SYMBOL_GPL(nft_parse_register_store); | |
96518518 PM |
9953 | |
9954 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
9955 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
9956 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
9957 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
51d70f18 | 9958 | [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
9959 | }; |
9960 | ||
9961 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
9962 | struct nft_data_desc *desc, const struct nlattr *nla) | |
9963 | { | |
664b0f8c | 9964 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
9965 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
9966 | struct nft_chain *chain; | |
9967 | int err; | |
9968 | ||
8cb08174 JB |
9969 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
9970 | nft_verdict_policy, NULL); | |
96518518 PM |
9971 | if (err < 0) |
9972 | return err; | |
9973 | ||
9974 | if (!tb[NFTA_VERDICT_CODE]) | |
9975 | return -EINVAL; | |
1ca2e170 | 9976 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 9977 | |
1ca2e170 | 9978 | switch (data->verdict.code) { |
e0abdadc | 9979 | default: |
1ca2e170 | 9980 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
9981 | case NF_ACCEPT: |
9982 | case NF_DROP: | |
9983 | case NF_QUEUE: | |
9984 | break; | |
9985 | default: | |
9986 | return -EINVAL; | |
9987 | } | |
954d8297 | 9988 | fallthrough; |
96518518 PM |
9989 | case NFT_CONTINUE: |
9990 | case NFT_BREAK: | |
9991 | case NFT_RETURN: | |
96518518 PM |
9992 | break; |
9993 | case NFT_JUMP: | |
9994 | case NFT_GOTO: | |
51d70f18 PNA |
9995 | if (tb[NFTA_VERDICT_CHAIN]) { |
9996 | chain = nft_chain_lookup(ctx->net, ctx->table, | |
9997 | tb[NFTA_VERDICT_CHAIN], | |
9998 | genmask); | |
9999 | } else if (tb[NFTA_VERDICT_CHAIN_ID]) { | |
95f466d2 | 10000 | chain = nft_chain_lookup_byid(ctx->net, ctx->table, |
51d70f18 PNA |
10001 | tb[NFTA_VERDICT_CHAIN_ID]); |
10002 | if (IS_ERR(chain)) | |
10003 | return PTR_ERR(chain); | |
10004 | } else { | |
96518518 | 10005 | return -EINVAL; |
51d70f18 PNA |
10006 | } |
10007 | ||
96518518 PM |
10008 | if (IS_ERR(chain)) |
10009 | return PTR_ERR(chain); | |
f323d954 | 10010 | if (nft_is_base_chain(chain)) |
96518518 | 10011 | return -EOPNOTSUPP; |
e02f0d39 PNA |
10012 | if (nft_chain_is_bound(chain)) |
10013 | return -EINVAL; | |
f323ef3a PNA |
10014 | if (desc->flags & NFT_DATA_DESC_SETELEM && |
10015 | chain->flags & NFT_CHAIN_BINDING) | |
10016 | return -EINVAL; | |
96518518 | 10017 | |
96518518 | 10018 | chain->use++; |
1ca2e170 | 10019 | data->verdict.chain = chain; |
96518518 | 10020 | break; |
96518518 PM |
10021 | } |
10022 | ||
4c4ed074 | 10023 | desc->len = sizeof(data->verdict); |
341b6941 | 10024 | |
96518518 PM |
10025 | return 0; |
10026 | } | |
10027 | ||
10028 | static void nft_verdict_uninit(const struct nft_data *data) | |
10029 | { | |
d0e2c7de PNA |
10030 | struct nft_chain *chain; |
10031 | struct nft_rule *rule; | |
10032 | ||
1ca2e170 | 10033 | switch (data->verdict.code) { |
96518518 PM |
10034 | case NFT_JUMP: |
10035 | case NFT_GOTO: | |
d0e2c7de PNA |
10036 | chain = data->verdict.chain; |
10037 | chain->use--; | |
10038 | ||
10039 | if (!nft_chain_is_bound(chain)) | |
10040 | break; | |
10041 | ||
10042 | chain->table->use--; | |
10043 | list_for_each_entry(rule, &chain->rules, list) | |
10044 | chain->use--; | |
10045 | ||
10046 | nft_chain_del(chain); | |
96518518 PM |
10047 | break; |
10048 | } | |
10049 | } | |
10050 | ||
33d5a7b1 | 10051 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
10052 | { |
10053 | struct nlattr *nest; | |
10054 | ||
ae0be8de | 10055 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
10056 | if (!nest) |
10057 | goto nla_put_failure; | |
10058 | ||
33d5a7b1 | 10059 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
10060 | goto nla_put_failure; |
10061 | ||
33d5a7b1 | 10062 | switch (v->code) { |
96518518 PM |
10063 | case NFT_JUMP: |
10064 | case NFT_GOTO: | |
1ca2e170 | 10065 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 10066 | v->chain->name)) |
96518518 PM |
10067 | goto nla_put_failure; |
10068 | } | |
10069 | nla_nest_end(skb, nest); | |
10070 | return 0; | |
10071 | ||
10072 | nla_put_failure: | |
10073 | return -1; | |
10074 | } | |
10075 | ||
d0a11fc3 | 10076 | static int nft_value_init(const struct nft_ctx *ctx, |
341b6941 PNA |
10077 | struct nft_data *data, struct nft_data_desc *desc, |
10078 | const struct nlattr *nla) | |
96518518 PM |
10079 | { |
10080 | unsigned int len; | |
10081 | ||
10082 | len = nla_len(nla); | |
10083 | if (len == 0) | |
10084 | return -EINVAL; | |
341b6941 | 10085 | if (len > desc->size) |
96518518 | 10086 | return -EOVERFLOW; |
341b6941 PNA |
10087 | if (desc->len) { |
10088 | if (len != desc->len) | |
10089 | return -EINVAL; | |
10090 | } else { | |
10091 | desc->len = len; | |
10092 | } | |
96518518 | 10093 | |
d0a11fc3 | 10094 | nla_memcpy(data->data, nla, len); |
341b6941 | 10095 | |
96518518 PM |
10096 | return 0; |
10097 | } | |
10098 | ||
10099 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
10100 | unsigned int len) | |
10101 | { | |
10102 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
10103 | } | |
10104 | ||
10105 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 10106 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
10107 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
10108 | }; | |
10109 | ||
10110 | /** | |
10111 | * nft_data_init - parse nf_tables data netlink attributes | |
10112 | * | |
10113 | * @ctx: context of the expression using the data | |
10114 | * @data: destination struct nft_data | |
10115 | * @desc: data description | |
10116 | * @nla: netlink attribute containing data | |
10117 | * | |
10118 | * Parse the netlink data attributes and initialize a struct nft_data. | |
10119 | * The type and length of data are returned in the data description. | |
10120 | * | |
10121 | * The caller can indicate that it only wants to accept data of type | |
10122 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
10123 | */ | |
341b6941 | 10124 | int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
96518518 PM |
10125 | struct nft_data_desc *desc, const struct nlattr *nla) |
10126 | { | |
10127 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
10128 | int err; | |
10129 | ||
341b6941 PNA |
10130 | if (WARN_ON_ONCE(!desc->size)) |
10131 | return -EINVAL; | |
10132 | ||
8cb08174 JB |
10133 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
10134 | nft_data_policy, NULL); | |
96518518 PM |
10135 | if (err < 0) |
10136 | return err; | |
10137 | ||
341b6941 PNA |
10138 | if (tb[NFTA_DATA_VALUE]) { |
10139 | if (desc->type != NFT_DATA_VALUE) | |
10140 | return -EINVAL; | |
10141 | ||
10142 | err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]); | |
10143 | } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) { | |
10144 | if (desc->type != NFT_DATA_VERDICT) | |
10145 | return -EINVAL; | |
10146 | ||
10147 | err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
10148 | } else { | |
10149 | err = -EINVAL; | |
10150 | } | |
10151 | ||
10152 | return err; | |
96518518 PM |
10153 | } |
10154 | EXPORT_SYMBOL_GPL(nft_data_init); | |
10155 | ||
10156 | /** | |
59105446 | 10157 | * nft_data_release - release a nft_data item |
96518518 PM |
10158 | * |
10159 | * @data: struct nft_data to release | |
10160 | * @type: type of data | |
10161 | * | |
10162 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
10163 | * all others need to be released by calling this function. | |
10164 | */ | |
59105446 | 10165 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 10166 | { |
960bd2c2 | 10167 | if (type < NFT_DATA_VERDICT) |
96518518 | 10168 | return; |
960bd2c2 | 10169 | switch (type) { |
96518518 PM |
10170 | case NFT_DATA_VERDICT: |
10171 | return nft_verdict_uninit(data); | |
10172 | default: | |
10173 | WARN_ON(1); | |
10174 | } | |
10175 | } | |
59105446 | 10176 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
10177 | |
10178 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
10179 | enum nft_data_types type, unsigned int len) | |
10180 | { | |
10181 | struct nlattr *nest; | |
10182 | int err; | |
10183 | ||
ae0be8de | 10184 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
10185 | if (nest == NULL) |
10186 | return -1; | |
10187 | ||
10188 | switch (type) { | |
10189 | case NFT_DATA_VALUE: | |
10190 | err = nft_value_dump(skb, data, len); | |
10191 | break; | |
10192 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 10193 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
10194 | break; |
10195 | default: | |
10196 | err = -EINVAL; | |
10197 | WARN_ON(1); | |
10198 | } | |
10199 | ||
10200 | nla_nest_end(skb, nest); | |
10201 | return err; | |
10202 | } | |
10203 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
10204 | ||
5ebe0b0e PNA |
10205 | int __nft_release_basechain(struct nft_ctx *ctx) |
10206 | { | |
10207 | struct nft_rule *rule, *nr; | |
10208 | ||
fa5950e4 FW |
10209 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
10210 | return 0; | |
5ebe0b0e | 10211 | |
c974a3a3 | 10212 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
10213 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
10214 | list_del(&rule->list); | |
10215 | ctx->chain->use--; | |
bb7b40ae | 10216 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 10217 | } |
1b2470e5 | 10218 | nft_chain_del(ctx->chain); |
5ebe0b0e | 10219 | ctx->table->use--; |
43a605f2 | 10220 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
10221 | |
10222 | return 0; | |
10223 | } | |
10224 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
10225 | ||
00dfe9be PNA |
10226 | static void __nft_release_hook(struct net *net, struct nft_table *table) |
10227 | { | |
6069da44 | 10228 | struct nft_flowtable *flowtable; |
00dfe9be PNA |
10229 | struct nft_chain *chain; |
10230 | ||
10231 | list_for_each_entry(chain, &table->chains, list) | |
f9a43007 | 10232 | __nf_tables_unregister_hook(net, table, chain, true); |
6069da44 | 10233 | list_for_each_entry(flowtable, &table->flowtables, list) |
f9a43007 PNA |
10234 | __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list, |
10235 | true); | |
00dfe9be PNA |
10236 | } |
10237 | ||
767d1216 PNA |
10238 | static void __nft_release_hooks(struct net *net) |
10239 | { | |
d59d2f82 | 10240 | struct nftables_pernet *nft_net = nft_pernet(net); |
767d1216 | 10241 | struct nft_table *table; |
767d1216 | 10242 | |
0854db2a | 10243 | list_for_each_entry(table, &nft_net->tables, list) { |
2888b080 PNA |
10244 | if (nft_table_has_owner(table)) |
10245 | continue; | |
10246 | ||
00dfe9be | 10247 | __nft_release_hook(net, table); |
2888b080 | 10248 | } |
767d1216 PNA |
10249 | } |
10250 | ||
fd020332 | 10251 | static void __nft_release_table(struct net *net, struct nft_table *table) |
df05ef87 | 10252 | { |
3b49e2e9 | 10253 | struct nft_flowtable *flowtable, *nf; |
df05ef87 | 10254 | struct nft_chain *chain, *nc; |
e5009240 | 10255 | struct nft_object *obj, *ne; |
df05ef87 PNA |
10256 | struct nft_rule *rule, *nr; |
10257 | struct nft_set *set, *ns; | |
10258 | struct nft_ctx ctx = { | |
10259 | .net = net, | |
43a605f2 | 10260 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
10261 | }; |
10262 | ||
fd020332 PNA |
10263 | ctx.family = table->family; |
10264 | ctx.table = table; | |
10265 | list_for_each_entry(chain, &table->chains, list) { | |
10266 | ctx.chain = chain; | |
10267 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
10268 | list_del(&rule->list); | |
10269 | chain->use--; | |
10270 | nf_tables_rule_release(&ctx, rule); | |
df05ef87 | 10271 | } |
df05ef87 | 10272 | } |
fd020332 PNA |
10273 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
10274 | list_del(&flowtable->list); | |
10275 | table->use--; | |
10276 | nf_tables_flowtable_destroy(flowtable); | |
10277 | } | |
10278 | list_for_each_entry_safe(set, ns, &table->sets, list) { | |
10279 | list_del(&set->list); | |
10280 | table->use--; | |
10281 | nft_set_destroy(&ctx, set); | |
10282 | } | |
10283 | list_for_each_entry_safe(obj, ne, &table->objects, list) { | |
10284 | nft_obj_del(obj); | |
10285 | table->use--; | |
10286 | nft_obj_destroy(&ctx, obj); | |
10287 | } | |
10288 | list_for_each_entry_safe(chain, nc, &table->chains, list) { | |
10289 | ctx.chain = chain; | |
10290 | nft_chain_del(chain); | |
10291 | table->use--; | |
10292 | nf_tables_chain_destroy(&ctx); | |
10293 | } | |
fd020332 PNA |
10294 | nf_tables_table_destroy(&ctx); |
10295 | } | |
10296 | ||
bd1777b3 | 10297 | static void __nft_release_tables(struct net *net) |
fd020332 | 10298 | { |
d59d2f82 | 10299 | struct nftables_pernet *nft_net = nft_pernet(net); |
fd020332 PNA |
10300 | struct nft_table *table, *nt; |
10301 | ||
0854db2a | 10302 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
bd1777b3 | 10303 | if (nft_table_has_owner(table)) |
6001a930 PNA |
10304 | continue; |
10305 | ||
a499b03b FW |
10306 | list_del(&table->list); |
10307 | ||
fd020332 | 10308 | __nft_release_table(net, table); |
6001a930 PNA |
10309 | } |
10310 | } | |
10311 | ||
10312 | static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event, | |
10313 | void *ptr) | |
10314 | { | |
a499b03b | 10315 | struct nft_table *table, *to_delete[8]; |
0854db2a | 10316 | struct nftables_pernet *nft_net; |
6001a930 | 10317 | struct netlink_notify *n = ptr; |
6001a930 | 10318 | struct net *net = n->net; |
a499b03b FW |
10319 | unsigned int deleted; |
10320 | bool restart = false; | |
6001a930 PNA |
10321 | |
10322 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER) | |
10323 | return NOTIFY_DONE; | |
10324 | ||
d59d2f82 | 10325 | nft_net = nft_pernet(net); |
a499b03b | 10326 | deleted = 0; |
0854db2a | 10327 | mutex_lock(&nft_net->commit_mutex); |
d4bc8271 PNA |
10328 | if (!list_empty(&nf_tables_destroy_list)) |
10329 | rcu_barrier(); | |
a499b03b | 10330 | again: |
0854db2a | 10331 | list_for_each_entry(table, &nft_net->tables, list) { |
6001a930 PNA |
10332 | if (nft_table_has_owner(table) && |
10333 | n->portid == table->nlpid) { | |
10334 | __nft_release_hook(net, table); | |
a499b03b FW |
10335 | list_del_rcu(&table->list); |
10336 | to_delete[deleted++] = table; | |
10337 | if (deleted >= ARRAY_SIZE(to_delete)) | |
10338 | break; | |
6001a930 PNA |
10339 | } |
10340 | } | |
a499b03b FW |
10341 | if (deleted) { |
10342 | restart = deleted >= ARRAY_SIZE(to_delete); | |
6001a930 | 10343 | synchronize_rcu(); |
a499b03b FW |
10344 | while (deleted) |
10345 | __nft_release_table(net, to_delete[--deleted]); | |
10346 | ||
10347 | if (restart) | |
10348 | goto again; | |
6001a930 | 10349 | } |
0854db2a | 10350 | mutex_unlock(&nft_net->commit_mutex); |
6001a930 PNA |
10351 | |
10352 | return NOTIFY_DONE; | |
df05ef87 PNA |
10353 | } |
10354 | ||
6001a930 PNA |
10355 | static struct notifier_block nft_nl_notifier = { |
10356 | .notifier_call = nft_rcv_nl_event, | |
10357 | }; | |
10358 | ||
dd4cbef7 PNA |
10359 | static int __net_init nf_tables_init_net(struct net *net) |
10360 | { | |
d59d2f82 | 10361 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10362 | |
10363 | INIT_LIST_HEAD(&nft_net->tables); | |
10364 | INIT_LIST_HEAD(&nft_net->commit_list); | |
10365 | INIT_LIST_HEAD(&nft_net->module_list); | |
10366 | INIT_LIST_HEAD(&nft_net->notify_list); | |
10367 | mutex_init(&nft_net->commit_mutex); | |
10368 | nft_net->base_seq = 1; | |
10369 | nft_net->validate_state = NFT_VALIDATE_SKIP; | |
a654de8f | 10370 | |
dd4cbef7 PNA |
10371 | return 0; |
10372 | } | |
10373 | ||
767d1216 PNA |
10374 | static void __net_exit nf_tables_pre_exit_net(struct net *net) |
10375 | { | |
3923b1e4 PNA |
10376 | struct nftables_pernet *nft_net = nft_pernet(net); |
10377 | ||
10378 | mutex_lock(&nft_net->commit_mutex); | |
767d1216 | 10379 | __nft_release_hooks(net); |
3923b1e4 | 10380 | mutex_unlock(&nft_net->commit_mutex); |
767d1216 PNA |
10381 | } |
10382 | ||
dd4cbef7 PNA |
10383 | static void __net_exit nf_tables_exit_net(struct net *net) |
10384 | { | |
d59d2f82 | 10385 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10386 | |
10387 | mutex_lock(&nft_net->commit_mutex); | |
03c1f1ef SY |
10388 | if (!list_empty(&nft_net->commit_list) || |
10389 | !list_empty(&nft_net->module_list)) | |
c0391b6a | 10390 | __nf_tables_abort(net, NFNL_ABORT_NONE); |
bd1777b3 | 10391 | __nft_release_tables(net); |
0854db2a FW |
10392 | mutex_unlock(&nft_net->commit_mutex); |
10393 | WARN_ON_ONCE(!list_empty(&nft_net->tables)); | |
10394 | WARN_ON_ONCE(!list_empty(&nft_net->module_list)); | |
10395 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); | |
dd4cbef7 PNA |
10396 | } |
10397 | ||
99633ab2 | 10398 | static struct pernet_operations nf_tables_net_ops = { |
767d1216 PNA |
10399 | .init = nf_tables_init_net, |
10400 | .pre_exit = nf_tables_pre_exit_net, | |
10401 | .exit = nf_tables_exit_net, | |
0854db2a FW |
10402 | .id = &nf_tables_net_id, |
10403 | .size = sizeof(struct nftables_pernet), | |
99633ab2 PNA |
10404 | }; |
10405 | ||
96518518 PM |
10406 | static int __init nf_tables_module_init(void) |
10407 | { | |
10408 | int err; | |
10409 | ||
d209df3e FW |
10410 | err = register_pernet_subsys(&nf_tables_net_ops); |
10411 | if (err < 0) | |
10412 | return err; | |
10413 | ||
10414 | err = nft_chain_filter_init(); | |
10415 | if (err < 0) | |
6001a930 | 10416 | goto err_chain_filter; |
02c7b25e | 10417 | |
96518518 PM |
10418 | err = nf_tables_core_module_init(); |
10419 | if (err < 0) | |
6001a930 | 10420 | goto err_core_module; |
96518518 | 10421 | |
d209df3e | 10422 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 10423 | if (err < 0) |
6001a930 | 10424 | goto err_netdev_notifier; |
96518518 | 10425 | |
4d44175a FW |
10426 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
10427 | if (err < 0) | |
6001a930 | 10428 | goto err_rht_objname; |
4d44175a | 10429 | |
06d392cb | 10430 | err = nft_offload_init(); |
10431 | if (err < 0) | |
6001a930 PNA |
10432 | goto err_offload; |
10433 | ||
10434 | err = netlink_register_notifier(&nft_nl_notifier); | |
10435 | if (err < 0) | |
10436 | goto err_netlink_notifier; | |
06d392cb | 10437 | |
d209df3e FW |
10438 | /* must be last */ |
10439 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
10440 | if (err < 0) | |
6001a930 | 10441 | goto err_nfnl_subsys; |
3b49e2e9 | 10442 | |
c1deb065 | 10443 | nft_chain_route_init(); |
3474a2c6 | 10444 | |
d209df3e | 10445 | return err; |
6001a930 PNA |
10446 | |
10447 | err_nfnl_subsys: | |
10448 | netlink_unregister_notifier(&nft_nl_notifier); | |
10449 | err_netlink_notifier: | |
06d392cb | 10450 | nft_offload_exit(); |
6001a930 | 10451 | err_offload: |
4d44175a | 10452 | rhltable_destroy(&nft_objname_ht); |
6001a930 | 10453 | err_rht_objname: |
d209df3e | 10454 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
6001a930 | 10455 | err_netdev_notifier: |
96518518 | 10456 | nf_tables_core_module_exit(); |
6001a930 | 10457 | err_core_module: |
d209df3e | 10458 | nft_chain_filter_fini(); |
6001a930 | 10459 | err_chain_filter: |
d209df3e | 10460 | unregister_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
10461 | return err; |
10462 | } | |
10463 | ||
10464 | static void __exit nf_tables_module_exit(void) | |
10465 | { | |
10466 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
6001a930 | 10467 | netlink_unregister_notifier(&nft_nl_notifier); |
06d392cb | 10468 | nft_offload_exit(); |
3b49e2e9 | 10469 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 10470 | nft_chain_filter_fini(); |
c1deb065 | 10471 | nft_chain_route_fini(); |
71ad00c5 | 10472 | unregister_pernet_subsys(&nf_tables_net_ops); |
0935d558 | 10473 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 10474 | rcu_barrier(); |
4d44175a | 10475 | rhltable_destroy(&nft_objname_ht); |
96518518 | 10476 | nf_tables_core_module_exit(); |
96518518 PM |
10477 | } |
10478 | ||
10479 | module_init(nf_tables_module_init); | |
10480 | module_exit(nf_tables_module_exit); | |
10481 | ||
10482 | MODULE_LICENSE("GPL"); | |
10483 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
10484 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |