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 | ||
cd5125d8 | 468 | static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
ee01d542 AB |
469 | struct nft_set *set) |
470 | { | |
471 | struct nft_trans *trans; | |
472 | ||
473 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
474 | if (trans == NULL) | |
475 | return -ENOMEM; | |
476 | ||
477 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { | |
478 | nft_trans_set_id(trans) = | |
479 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 480 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
481 | } |
482 | nft_trans_set(trans) = set; | |
0854db2a | 483 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
484 | |
485 | return 0; | |
486 | } | |
487 | ||
cd5125d8 | 488 | static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set) |
ee01d542 AB |
489 | { |
490 | int err; | |
491 | ||
492 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
493 | if (err < 0) | |
494 | return err; | |
495 | ||
37a9cc52 | 496 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
497 | ctx->table->use--; |
498 | ||
499 | return err; | |
500 | } | |
501 | ||
e5009240 PNA |
502 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
503 | struct nft_object *obj) | |
504 | { | |
505 | struct nft_trans *trans; | |
506 | ||
507 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
508 | if (trans == NULL) | |
509 | return -ENOMEM; | |
510 | ||
511 | if (msg_type == NFT_MSG_NEWOBJ) | |
512 | nft_activate_next(ctx->net, obj); | |
513 | ||
514 | nft_trans_obj(trans) = obj; | |
0854db2a | 515 | nft_trans_commit_list_add_tail(ctx->net, trans); |
e5009240 PNA |
516 | |
517 | return 0; | |
518 | } | |
519 | ||
520 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
521 | { | |
522 | int err; | |
523 | ||
524 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
525 | if (err < 0) | |
526 | return err; | |
527 | ||
528 | nft_deactivate_next(ctx->net, obj); | |
529 | ctx->table->use--; | |
530 | ||
531 | return err; | |
532 | } | |
533 | ||
3b49e2e9 PNA |
534 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
535 | struct nft_flowtable *flowtable) | |
536 | { | |
537 | struct nft_trans *trans; | |
538 | ||
539 | trans = nft_trans_alloc(ctx, msg_type, | |
540 | sizeof(struct nft_trans_flowtable)); | |
541 | if (trans == NULL) | |
542 | return -ENOMEM; | |
543 | ||
544 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
545 | nft_activate_next(ctx->net, flowtable); | |
546 | ||
2c9e4559 | 547 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); |
3b49e2e9 | 548 | nft_trans_flowtable(trans) = flowtable; |
0854db2a | 549 | nft_trans_commit_list_add_tail(ctx->net, trans); |
3b49e2e9 PNA |
550 | |
551 | return 0; | |
552 | } | |
553 | ||
554 | static int nft_delflowtable(struct nft_ctx *ctx, | |
555 | struct nft_flowtable *flowtable) | |
556 | { | |
557 | int err; | |
558 | ||
559 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
560 | if (err < 0) | |
561 | return err; | |
562 | ||
563 | nft_deactivate_next(ctx->net, flowtable); | |
564 | ctx->table->use--; | |
565 | ||
566 | return err; | |
567 | } | |
568 | ||
34cc9e52 PNA |
569 | static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg) |
570 | { | |
571 | int i; | |
572 | ||
573 | for (i = track->regs[dreg].num_reg; i > 0; i--) | |
574 | __nft_reg_track_cancel(track, dreg - i); | |
575 | } | |
576 | ||
577 | static void __nft_reg_track_update(struct nft_regs_track *track, | |
578 | const struct nft_expr *expr, | |
579 | u8 dreg, u8 num_reg) | |
580 | { | |
581 | track->regs[dreg].selector = expr; | |
582 | track->regs[dreg].bitwise = NULL; | |
583 | track->regs[dreg].num_reg = num_reg; | |
584 | } | |
585 | ||
586 | void nft_reg_track_update(struct nft_regs_track *track, | |
587 | const struct nft_expr *expr, u8 dreg, u8 len) | |
588 | { | |
589 | unsigned int regcount; | |
590 | int i; | |
591 | ||
592 | __nft_reg_track_clobber(track, dreg); | |
593 | ||
594 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
595 | for (i = 0; i < regcount; i++, dreg++) | |
596 | __nft_reg_track_update(track, expr, dreg, i); | |
597 | } | |
598 | EXPORT_SYMBOL_GPL(nft_reg_track_update); | |
599 | ||
600 | void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len) | |
601 | { | |
602 | unsigned int regcount; | |
603 | int i; | |
604 | ||
605 | __nft_reg_track_clobber(track, dreg); | |
606 | ||
607 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
608 | for (i = 0; i < regcount; i++, dreg++) | |
609 | __nft_reg_track_cancel(track, dreg); | |
610 | } | |
611 | EXPORT_SYMBOL_GPL(nft_reg_track_cancel); | |
612 | ||
613 | void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) | |
614 | { | |
615 | track->regs[dreg].selector = NULL; | |
616 | track->regs[dreg].bitwise = NULL; | |
617 | track->regs[dreg].num_reg = 0; | |
618 | } | |
619 | EXPORT_SYMBOL_GPL(__nft_reg_track_cancel); | |
620 | ||
96518518 PM |
621 | /* |
622 | * Tables | |
623 | */ | |
624 | ||
36596dad | 625 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 626 | const struct nlattr *nla, |
6001a930 | 627 | u8 family, u8 genmask, u32 nlpid) |
96518518 | 628 | { |
0854db2a | 629 | struct nftables_pernet *nft_net; |
96518518 PM |
630 | struct nft_table *table; |
631 | ||
cac20fcd PNA |
632 | if (nla == NULL) |
633 | return ERR_PTR(-EINVAL); | |
634 | ||
d59d2f82 | 635 | nft_net = nft_pernet(net); |
0854db2a FW |
636 | list_for_each_entry_rcu(table, &nft_net->tables, list, |
637 | lockdep_is_held(&nft_net->commit_mutex)) { | |
f2a6d766 | 638 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 639 | table->family == family && |
6001a930 PNA |
640 | nft_active_genmask(table, genmask)) { |
641 | if (nft_table_has_owner(table) && | |
53479909 | 642 | nlpid && table->nlpid != nlpid) |
6001a930 PNA |
643 | return ERR_PTR(-EPERM); |
644 | ||
96518518 | 645 | return table; |
6001a930 | 646 | } |
96518518 | 647 | } |
cac20fcd PNA |
648 | |
649 | return ERR_PTR(-ENOENT); | |
96518518 PM |
650 | } |
651 | ||
3ecbfd65 HS |
652 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
653 | const struct nlattr *nla, | |
e31f072f | 654 | u8 genmask, u32 nlpid) |
3ecbfd65 | 655 | { |
0854db2a | 656 | struct nftables_pernet *nft_net; |
3ecbfd65 HS |
657 | struct nft_table *table; |
658 | ||
d59d2f82 | 659 | nft_net = nft_pernet(net); |
0854db2a | 660 | list_for_each_entry(table, &nft_net->tables, list) { |
3ecbfd65 | 661 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && |
e31f072f PNA |
662 | nft_active_genmask(table, genmask)) { |
663 | if (nft_table_has_owner(table) && | |
664 | nlpid && table->nlpid != nlpid) | |
665 | return ERR_PTR(-EPERM); | |
666 | ||
3ecbfd65 | 667 | return table; |
e31f072f | 668 | } |
3ecbfd65 | 669 | } |
3ecbfd65 HS |
670 | |
671 | return ERR_PTR(-ENOENT); | |
672 | } | |
673 | ||
96518518 PM |
674 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
675 | { | |
676 | return ++table->hgenerator; | |
677 | } | |
678 | ||
32537e91 | 679 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 680 | |
82603549 PNA |
681 | static const struct nft_chain_type * |
682 | __nft_chain_type_get(u8 family, enum nft_chain_types type) | |
683 | { | |
684 | if (family >= NFPROTO_NUMPROTO || | |
685 | type >= NFT_CHAIN_T_MAX) | |
686 | return NULL; | |
687 | ||
688 | return chain_type[family][type]; | |
689 | } | |
690 | ||
32537e91 | 691 | static const struct nft_chain_type * |
1ea26cca | 692 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c | 693 | { |
82603549 | 694 | const struct nft_chain_type *type; |
9370761c PNA |
695 | int i; |
696 | ||
baae3e62 | 697 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
82603549 PNA |
698 | type = __nft_chain_type_get(family, i); |
699 | if (!type) | |
700 | continue; | |
701 | if (!nla_strcmp(nla, type->name)) | |
702 | return type; | |
9370761c | 703 | } |
baae3e62 | 704 | return NULL; |
9370761c PNA |
705 | } |
706 | ||
eb014de4 PNA |
707 | struct nft_module_request { |
708 | struct list_head list; | |
709 | char module[MODULE_NAME_LEN]; | |
710 | bool done; | |
711 | }; | |
712 | ||
452238e8 | 713 | #ifdef CONFIG_MODULES |
cefa31a9 FW |
714 | __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, |
715 | ...) | |
452238e8 FW |
716 | { |
717 | char module_name[MODULE_NAME_LEN]; | |
0854db2a | 718 | struct nftables_pernet *nft_net; |
eb014de4 | 719 | struct nft_module_request *req; |
452238e8 FW |
720 | va_list args; |
721 | int ret; | |
722 | ||
452238e8 FW |
723 | va_start(args, fmt); |
724 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | |
725 | va_end(args); | |
9332d27d | 726 | if (ret >= MODULE_NAME_LEN) |
eb014de4 | 727 | return 0; |
452238e8 | 728 | |
d59d2f82 | 729 | nft_net = nft_pernet(net); |
0854db2a | 730 | list_for_each_entry(req, &nft_net->module_list, list) { |
eb014de4 PNA |
731 | if (!strcmp(req->module, module_name)) { |
732 | if (req->done) | |
733 | return 0; | |
ec7470b8 | 734 | |
eb014de4 PNA |
735 | /* A request to load this module already exists. */ |
736 | return -EAGAIN; | |
737 | } | |
738 | } | |
739 | ||
740 | req = kmalloc(sizeof(*req), GFP_KERNEL); | |
741 | if (!req) | |
742 | return -ENOMEM; | |
743 | ||
744 | req->done = false; | |
8556bceb | 745 | strscpy(req->module, module_name, MODULE_NAME_LEN); |
0854db2a | 746 | list_add_tail(&req->list, &nft_net->module_list); |
eb014de4 PNA |
747 | |
748 | return -EAGAIN; | |
452238e8 | 749 | } |
cefa31a9 | 750 | EXPORT_SYMBOL_GPL(nft_request_module); |
452238e8 FW |
751 | #endif |
752 | ||
f102d66b FW |
753 | static void lockdep_nfnl_nft_mutex_not_held(void) |
754 | { | |
755 | #ifdef CONFIG_PROVE_LOCKING | |
c0700dfa FW |
756 | if (debug_locks) |
757 | WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
f102d66b FW |
758 | #endif |
759 | } | |
760 | ||
32537e91 | 761 | static const struct nft_chain_type * |
452238e8 FW |
762 | nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla, |
763 | u8 family, bool autoload) | |
9370761c | 764 | { |
32537e91 | 765 | const struct nft_chain_type *type; |
9370761c | 766 | |
1ea26cca | 767 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
768 | if (type != NULL) |
769 | return type; | |
f102d66b FW |
770 | |
771 | lockdep_nfnl_nft_mutex_not_held(); | |
9370761c | 772 | #ifdef CONFIG_MODULES |
93b0806f | 773 | if (autoload) { |
eb014de4 PNA |
774 | if (nft_request_module(net, "nft-chain-%u-%.*s", family, |
775 | nla_len(nla), | |
776 | (const char *)nla_data(nla)) == -EAGAIN) | |
93b0806f | 777 | return ERR_PTR(-EAGAIN); |
9370761c PNA |
778 | } |
779 | #endif | |
93b0806f | 780 | return ERR_PTR(-ENOENT); |
9370761c PNA |
781 | } |
782 | ||
802b8051 PNA |
783 | static __be16 nft_base_seq(const struct net *net) |
784 | { | |
d59d2f82 | 785 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
786 | |
787 | return htons(nft_net->base_seq & 0xffff); | |
802b8051 PNA |
788 | } |
789 | ||
96518518 | 790 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
791 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
792 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 793 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 794 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
7a81575b JGG |
795 | [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY, |
796 | .len = NFT_USERDATA_MAXLEN } | |
96518518 PM |
797 | }; |
798 | ||
84d7fce6 PNA |
799 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
800 | u32 portid, u32 seq, int event, u32 flags, | |
801 | int family, const struct nft_table *table) | |
96518518 PM |
802 | { |
803 | struct nlmsghdr *nlh; | |
96518518 | 804 | |
dedb67c4 | 805 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
806 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
807 | NFNETLINK_V0, nft_base_seq(net)); | |
808 | if (!nlh) | |
96518518 PM |
809 | goto nla_put_failure; |
810 | ||
9ddf6323 | 811 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
179d9ba5 PNA |
812 | nla_put_be32(skb, NFTA_TABLE_FLAGS, |
813 | htonl(table->flags & NFT_TABLE_F_MASK)) || | |
3ecbfd65 HS |
814 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
815 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
816 | NFTA_TABLE_PAD)) | |
96518518 | 817 | goto nla_put_failure; |
6001a930 PNA |
818 | if (nft_table_has_owner(table) && |
819 | nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid))) | |
820 | goto nla_put_failure; | |
96518518 | 821 | |
7a81575b JGG |
822 | if (table->udata) { |
823 | if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata)) | |
824 | goto nla_put_failure; | |
825 | } | |
826 | ||
053c095a JB |
827 | nlmsg_end(skb, nlh); |
828 | return 0; | |
96518518 PM |
829 | |
830 | nla_put_failure: | |
831 | nlmsg_trim(skb, nlh); | |
832 | return -1; | |
833 | } | |
834 | ||
67cc570e PNA |
835 | struct nftnl_skb_parms { |
836 | bool report; | |
837 | }; | |
838 | #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb)) | |
839 | ||
840 | static void nft_notify_enqueue(struct sk_buff *skb, bool report, | |
841 | struct list_head *notify_list) | |
842 | { | |
843 | NFT_CB(skb).report = report; | |
844 | list_add_tail(&skb->list, notify_list); | |
845 | } | |
846 | ||
25e94a99 | 847 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 | 848 | { |
0854db2a | 849 | struct nftables_pernet *nft_net; |
96518518 | 850 | struct sk_buff *skb; |
6fb721cf | 851 | u16 flags = 0; |
96518518 PM |
852 | int err; |
853 | ||
128ad332 PNA |
854 | if (!ctx->report && |
855 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 856 | return; |
96518518 | 857 | |
96518518 PM |
858 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
859 | if (skb == NULL) | |
860 | goto err; | |
861 | ||
6fb721cf PNA |
862 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
863 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
864 | ||
84d7fce6 | 865 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 866 | event, flags, ctx->family, ctx->table); |
96518518 PM |
867 | if (err < 0) { |
868 | kfree_skb(skb); | |
869 | goto err; | |
870 | } | |
871 | ||
d59d2f82 | 872 | nft_net = nft_pernet(ctx->net); |
0854db2a | 873 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 874 | return; |
96518518 | 875 | err: |
25e94a99 | 876 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
877 | } |
878 | ||
879 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
880 | struct netlink_callback *cb) | |
881 | { | |
882 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
0854db2a | 883 | struct nftables_pernet *nft_net; |
96518518 PM |
884 | const struct nft_table *table; |
885 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 886 | struct net *net = sock_net(skb->sk); |
96518518 PM |
887 | int family = nfmsg->nfgen_family; |
888 | ||
e688a7f8 | 889 | rcu_read_lock(); |
d59d2f82 | 890 | nft_net = nft_pernet(net); |
34002783 | 891 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 892 | |
0854db2a | 893 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 894 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
895 | continue; |
896 | ||
36596dad PNA |
897 | if (idx < s_idx) |
898 | goto cont; | |
899 | if (idx > s_idx) | |
900 | memset(&cb->args[1], 0, | |
901 | sizeof(cb->args) - sizeof(cb->args[0])); | |
902 | if (!nft_is_active(net, table)) | |
903 | continue; | |
904 | if (nf_tables_fill_table_info(skb, net, | |
905 | NETLINK_CB(cb->skb).portid, | |
906 | cb->nlh->nlmsg_seq, | |
907 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 908 | table->family, table) < 0) |
36596dad PNA |
909 | goto done; |
910 | ||
911 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 912 | cont: |
36596dad | 913 | idx++; |
96518518 PM |
914 | } |
915 | done: | |
e688a7f8 | 916 | rcu_read_unlock(); |
96518518 PM |
917 | cb->args[0] = idx; |
918 | return skb->len; | |
919 | } | |
920 | ||
d9adf22a FW |
921 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
922 | const struct nlmsghdr *nlh, | |
923 | struct netlink_dump_control *c) | |
924 | { | |
925 | int err; | |
926 | ||
927 | if (!try_module_get(THIS_MODULE)) | |
928 | return -EINVAL; | |
929 | ||
930 | rcu_read_unlock(); | |
931 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
932 | rcu_read_lock(); | |
933 | module_put(THIS_MODULE); | |
934 | ||
935 | return err; | |
936 | } | |
937 | ||
938 | /* called with rcu_read_lock held */ | |
797d4980 PNA |
939 | static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info, |
940 | const struct nlattr * const nla[]) | |
96518518 | 941 | { |
797d4980 PNA |
942 | struct netlink_ext_ack *extack = info->extack; |
943 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 944 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 945 | const struct nft_table *table; |
797d4980 | 946 | struct net *net = info->net; |
96518518 | 947 | struct sk_buff *skb2; |
96518518 PM |
948 | int err; |
949 | ||
797d4980 | 950 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
951 | struct netlink_dump_control c = { |
952 | .dump = nf_tables_dump_tables, | |
d9adf22a | 953 | .module = THIS_MODULE, |
96518518 | 954 | }; |
d9adf22a | 955 | |
797d4980 | 956 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
957 | } |
958 | ||
6001a930 | 959 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0); |
36dd1bcc PNA |
960 | if (IS_ERR(table)) { |
961 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 962 | return PTR_ERR(table); |
36dd1bcc | 963 | } |
96518518 | 964 | |
d9adf22a | 965 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
966 | if (!skb2) |
967 | return -ENOMEM; | |
968 | ||
84d7fce6 | 969 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
970 | info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE, |
971 | 0, family, table); | |
96518518 | 972 | if (err < 0) |
ee921183 | 973 | goto err_fill_table_info; |
96518518 | 974 | |
ee921183 | 975 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 976 | |
ee921183 | 977 | err_fill_table_info: |
96518518 PM |
978 | kfree_skb(skb2); |
979 | return err; | |
980 | } | |
981 | ||
c9c17211 | 982 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
983 | { |
984 | struct nft_chain *chain; | |
985 | u32 i = 0; | |
986 | ||
987 | list_for_each_entry(chain, &table->chains, list) { | |
988 | if (!nft_is_active_next(net, chain)) | |
989 | continue; | |
f323d954 | 990 | if (!nft_is_base_chain(chain)) |
10435c11 F |
991 | continue; |
992 | ||
993 | if (cnt && i++ == cnt) | |
994 | break; | |
995 | ||
1e9451cb | 996 | nf_tables_unregister_hook(net, table, chain); |
10435c11 F |
997 | } |
998 | } | |
999 | ||
c9c17211 | 1000 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
1001 | { |
1002 | struct nft_chain *chain; | |
1003 | int err, i = 0; | |
1004 | ||
1005 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1006 | if (!nft_is_active_next(net, chain)) |
1007 | continue; | |
f323d954 | 1008 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
1009 | continue; |
1010 | ||
1e9451cb | 1011 | err = nf_tables_register_hook(net, table, chain); |
9ddf6323 | 1012 | if (err < 0) |
d54725cd | 1013 | goto err_register_hooks; |
9ddf6323 PNA |
1014 | |
1015 | i++; | |
1016 | } | |
1017 | return 0; | |
d54725cd PNA |
1018 | |
1019 | err_register_hooks: | |
10435c11 | 1020 | if (i) |
c9c17211 | 1021 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
1022 | return err; |
1023 | } | |
1024 | ||
c9c17211 | 1025 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 1026 | { |
179d9ba5 | 1027 | table->flags &= ~NFT_TABLE_F_DORMANT; |
c9c17211 | 1028 | nft_table_disable(net, table, 0); |
179d9ba5 | 1029 | table->flags |= NFT_TABLE_F_DORMANT; |
9ddf6323 PNA |
1030 | } |
1031 | ||
179d9ba5 PNA |
1032 | #define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1) |
1033 | #define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0) | |
1034 | #define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1) | |
1035 | #define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \ | |
1036 | __NFT_TABLE_F_WAS_AWAKEN) | |
0ce7cf41 | 1037 | |
e1aaca93 | 1038 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 1039 | { |
55dd6f93 | 1040 | struct nft_trans *trans; |
e1aaca93 | 1041 | u32 flags; |
179d9ba5 | 1042 | int ret; |
9ddf6323 | 1043 | |
e1aaca93 PNA |
1044 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
1045 | return 0; | |
9ddf6323 | 1046 | |
e1aaca93 | 1047 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
6001a930 PNA |
1048 | if (flags & ~NFT_TABLE_F_MASK) |
1049 | return -EOPNOTSUPP; | |
e1aaca93 | 1050 | |
63283dd2 PNA |
1051 | if (flags == ctx->table->flags) |
1052 | return 0; | |
1053 | ||
9cc0001a PNA |
1054 | if ((nft_table_has_owner(ctx->table) && |
1055 | !(flags & NFT_TABLE_F_OWNER)) || | |
1056 | (!nft_table_has_owner(ctx->table) && | |
1057 | flags & NFT_TABLE_F_OWNER)) | |
1058 | return -EOPNOTSUPP; | |
1059 | ||
55dd6f93 PNA |
1060 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
1061 | sizeof(struct nft_trans_table)); | |
1062 | if (trans == NULL) | |
1063 | return -ENOMEM; | |
9ddf6323 | 1064 | |
e1aaca93 PNA |
1065 | if ((flags & NFT_TABLE_F_DORMANT) && |
1066 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
179d9ba5 PNA |
1067 | ctx->table->flags |= NFT_TABLE_F_DORMANT; |
1068 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) | |
1069 | ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN; | |
e1aaca93 PNA |
1070 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
1071 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
179d9ba5 PNA |
1072 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
1073 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) { | |
1074 | ret = nf_tables_table_enable(ctx->net, ctx->table); | |
1075 | if (ret < 0) | |
1076 | goto err_register_hooks; | |
1077 | ||
1078 | ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT; | |
1079 | } | |
9ddf6323 PNA |
1080 | } |
1081 | ||
55dd6f93 | 1082 | nft_trans_table_update(trans) = true; |
0854db2a | 1083 | nft_trans_commit_list_add_tail(ctx->net, trans); |
179d9ba5 | 1084 | |
55dd6f93 | 1085 | return 0; |
179d9ba5 PNA |
1086 | |
1087 | err_register_hooks: | |
55dd6f93 | 1088 | nft_trans_destroy(trans); |
9ddf6323 PNA |
1089 | return ret; |
1090 | } | |
1091 | ||
1b2470e5 FW |
1092 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
1093 | { | |
1094 | const char *name = data; | |
1095 | ||
1096 | return jhash(name, strlen(name), seed); | |
1097 | } | |
1098 | ||
1099 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
1100 | { | |
1101 | const struct nft_chain *chain = data; | |
1102 | ||
1103 | return nft_chain_hash(chain->name, 0, seed); | |
1104 | } | |
1105 | ||
1106 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
1107 | const void *ptr) | |
1108 | { | |
1109 | const struct nft_chain *chain = ptr; | |
1110 | const char *name = arg->key; | |
1111 | ||
1112 | return strcmp(chain->name, name); | |
1113 | } | |
1114 | ||
4d44175a FW |
1115 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed) |
1116 | { | |
1117 | const struct nft_object_hash_key *k = data; | |
1118 | ||
1119 | seed ^= hash_ptr(k->table, 32); | |
1120 | ||
1121 | return jhash(k->name, strlen(k->name), seed); | |
1122 | } | |
1123 | ||
1124 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed) | |
1125 | { | |
1126 | const struct nft_object *obj = data; | |
1127 | ||
1128 | return nft_objname_hash(&obj->key, 0, seed); | |
1129 | } | |
1130 | ||
1131 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg, | |
1132 | const void *ptr) | |
1133 | { | |
1134 | const struct nft_object_hash_key *k = arg->key; | |
1135 | const struct nft_object *obj = ptr; | |
1136 | ||
1137 | if (obj->key.table != k->table) | |
1138 | return -1; | |
1139 | ||
1140 | return strcmp(obj->key.name, k->name); | |
1141 | } | |
1142 | ||
f1082dd3 PS |
1143 | static bool nft_supported_family(u8 family) |
1144 | { | |
1145 | return false | |
1146 | #ifdef CONFIG_NF_TABLES_INET | |
1147 | || family == NFPROTO_INET | |
1148 | #endif | |
1149 | #ifdef CONFIG_NF_TABLES_IPV4 | |
1150 | || family == NFPROTO_IPV4 | |
1151 | #endif | |
1152 | #ifdef CONFIG_NF_TABLES_ARP | |
1153 | || family == NFPROTO_ARP | |
1154 | #endif | |
1155 | #ifdef CONFIG_NF_TABLES_NETDEV | |
1156 | || family == NFPROTO_NETDEV | |
1157 | #endif | |
1158 | #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE) | |
1159 | || family == NFPROTO_BRIDGE | |
1160 | #endif | |
1161 | #ifdef CONFIG_NF_TABLES_IPV6 | |
1162 | || family == NFPROTO_IPV6 | |
1163 | #endif | |
1164 | ; | |
1165 | } | |
1166 | ||
7dab8ee3 PNA |
1167 | static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, |
1168 | const struct nlattr * const nla[]) | |
96518518 | 1169 | { |
7dab8ee3 | 1170 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
1171 | struct netlink_ext_ack *extack = info->extack; |
1172 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1173 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1174 | struct net *net = info->net; |
36dd1bcc PNA |
1175 | const struct nlattr *attr; |
1176 | struct nft_table *table; | |
e1aaca93 | 1177 | struct nft_ctx ctx; |
7a81575b | 1178 | u32 flags = 0; |
55dd6f93 | 1179 | int err; |
96518518 | 1180 | |
f1082dd3 PS |
1181 | if (!nft_supported_family(family)) |
1182 | return -EOPNOTSUPP; | |
1183 | ||
0854db2a | 1184 | lockdep_assert_held(&nft_net->commit_mutex); |
36dd1bcc | 1185 | attr = nla[NFTA_TABLE_NAME]; |
6001a930 PNA |
1186 | table = nft_table_lookup(net, attr, family, genmask, |
1187 | NETLINK_CB(skb).portid); | |
96518518 PM |
1188 | if (IS_ERR(table)) { |
1189 | if (PTR_ERR(table) != -ENOENT) | |
1190 | return PTR_ERR(table); | |
1a28ad74 | 1191 | } else { |
7dab8ee3 | 1192 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 1193 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1194 | return -EEXIST; |
36dd1bcc | 1195 | } |
7dab8ee3 | 1196 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 | 1197 | return -EOPNOTSUPP; |
e1aaca93 | 1198 | |
7dab8ee3 PNA |
1199 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
1200 | ||
e1aaca93 | 1201 | return nf_tables_updtable(&ctx); |
96518518 PM |
1202 | } |
1203 | ||
c5c1f975 PM |
1204 | if (nla[NFTA_TABLE_FLAGS]) { |
1205 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
6001a930 PNA |
1206 | if (flags & ~NFT_TABLE_F_MASK) |
1207 | return -EOPNOTSUPP; | |
c5c1f975 PM |
1208 | } |
1209 | ||
ffdb210e | 1210 | err = -ENOMEM; |
33758c89 | 1211 | table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT); |
ffdb210e | 1212 | if (table == NULL) |
98319cb9 | 1213 | goto err_kzalloc; |
96518518 | 1214 | |
33758c89 | 1215 | table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT); |
e46abbcc | 1216 | if (table->name == NULL) |
98319cb9 | 1217 | goto err_strdup; |
e46abbcc | 1218 | |
7a81575b | 1219 | if (nla[NFTA_TABLE_USERDATA]) { |
33758c89 | 1220 | table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT); |
7a81575b JGG |
1221 | if (table->udata == NULL) |
1222 | goto err_table_udata; | |
1223 | ||
85db827a | 1224 | table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]); |
7a81575b JGG |
1225 | } |
1226 | ||
1b2470e5 FW |
1227 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
1228 | if (err) | |
1229 | goto err_chain_ht; | |
1230 | ||
96518518 | 1231 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 1232 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 1233 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 1234 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 1235 | table->family = family; |
c5c1f975 | 1236 | table->flags = flags; |
ab482c6b | 1237 | table->handle = ++nft_net->table_handle; |
6001a930 PNA |
1238 | if (table->flags & NFT_TABLE_F_OWNER) |
1239 | table->nlpid = NETLINK_CB(skb).portid; | |
9ddf6323 | 1240 | |
7dab8ee3 | 1241 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
55dd6f93 | 1242 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 1243 | if (err < 0) |
98319cb9 | 1244 | goto err_trans; |
ffdb210e | 1245 | |
0854db2a | 1246 | list_add_tail_rcu(&table->list, &nft_net->tables); |
96518518 | 1247 | return 0; |
98319cb9 | 1248 | err_trans: |
1b2470e5 FW |
1249 | rhltable_destroy(&table->chains_ht); |
1250 | err_chain_ht: | |
7a81575b JGG |
1251 | kfree(table->udata); |
1252 | err_table_udata: | |
e46abbcc | 1253 | kfree(table->name); |
98319cb9 | 1254 | err_strdup: |
ffdb210e | 1255 | kfree(table); |
98319cb9 | 1256 | err_kzalloc: |
ffdb210e | 1257 | return err; |
96518518 PM |
1258 | } |
1259 | ||
b9ac12ef AB |
1260 | static int nft_flush_table(struct nft_ctx *ctx) |
1261 | { | |
3b49e2e9 | 1262 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 1263 | struct nft_chain *chain, *nc; |
e5009240 | 1264 | struct nft_object *obj, *ne; |
b9ac12ef | 1265 | struct nft_set *set, *ns; |
3b49e2e9 | 1266 | int err; |
b9ac12ef | 1267 | |
a2f18db0 | 1268 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
1269 | if (!nft_is_active_next(ctx->net, chain)) |
1270 | continue; | |
1271 | ||
d0e2c7de PNA |
1272 | if (nft_chain_is_bound(chain)) |
1273 | continue; | |
1274 | ||
b9ac12ef AB |
1275 | ctx->chain = chain; |
1276 | ||
1277 | err = nft_delrule_by_chain(ctx); | |
1278 | if (err < 0) | |
1279 | goto out; | |
b9ac12ef AB |
1280 | } |
1281 | ||
1282 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
1283 | if (!nft_is_active_next(ctx->net, set)) |
1284 | continue; | |
1285 | ||
408070d6 | 1286 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
1287 | !list_empty(&set->bindings)) |
1288 | continue; | |
1289 | ||
1290 | err = nft_delset(ctx, set); | |
1291 | if (err < 0) | |
1292 | goto out; | |
1293 | } | |
1294 | ||
3b49e2e9 | 1295 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
335178d5 FW |
1296 | if (!nft_is_active_next(ctx->net, flowtable)) |
1297 | continue; | |
1298 | ||
3b49e2e9 PNA |
1299 | err = nft_delflowtable(ctx, flowtable); |
1300 | if (err < 0) | |
1301 | goto out; | |
1302 | } | |
1303 | ||
e5009240 | 1304 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
335178d5 FW |
1305 | if (!nft_is_active_next(ctx->net, obj)) |
1306 | continue; | |
1307 | ||
e5009240 PNA |
1308 | err = nft_delobj(ctx, obj); |
1309 | if (err < 0) | |
1310 | goto out; | |
1311 | } | |
1312 | ||
a2f18db0 | 1313 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
1314 | if (!nft_is_active_next(ctx->net, chain)) |
1315 | continue; | |
1316 | ||
d0e2c7de PNA |
1317 | if (nft_chain_is_bound(chain)) |
1318 | continue; | |
1319 | ||
a2f18db0 PNA |
1320 | ctx->chain = chain; |
1321 | ||
1322 | err = nft_delchain(ctx); | |
1323 | if (err < 0) | |
1324 | goto out; | |
1325 | } | |
1326 | ||
b9ac12ef AB |
1327 | err = nft_deltable(ctx); |
1328 | out: | |
1329 | return err; | |
1330 | } | |
1331 | ||
1332 | static int nft_flush(struct nft_ctx *ctx, int family) | |
1333 | { | |
d59d2f82 | 1334 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
b9ac12ef | 1335 | const struct nlattr * const *nla = ctx->nla; |
d59d2f82 | 1336 | struct nft_table *table, *nt; |
b9ac12ef AB |
1337 | int err = 0; |
1338 | ||
0854db2a | 1339 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
98319cb9 | 1340 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
1341 | continue; |
1342 | ||
98319cb9 | 1343 | ctx->family = table->family; |
f2a6d766 | 1344 | |
36596dad PNA |
1345 | if (!nft_is_active_next(ctx->net, table)) |
1346 | continue; | |
b9ac12ef | 1347 | |
6001a930 PNA |
1348 | if (nft_table_has_owner(table) && table->nlpid != ctx->portid) |
1349 | continue; | |
1350 | ||
36596dad PNA |
1351 | if (nla[NFTA_TABLE_NAME] && |
1352 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
1353 | continue; | |
b9ac12ef | 1354 | |
36596dad PNA |
1355 | ctx->table = table; |
1356 | ||
1357 | err = nft_flush_table(ctx); | |
1358 | if (err < 0) | |
1359 | goto out; | |
b9ac12ef AB |
1360 | } |
1361 | out: | |
1362 | return err; | |
1363 | } | |
1364 | ||
7dab8ee3 PNA |
1365 | static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info, |
1366 | const struct nlattr * const nla[]) | |
96518518 | 1367 | { |
7dab8ee3 PNA |
1368 | struct netlink_ext_ack *extack = info->extack; |
1369 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1370 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1371 | struct net *net = info->net; |
36dd1bcc PNA |
1372 | const struct nlattr *attr; |
1373 | struct nft_table *table; | |
55dd6f93 | 1374 | struct nft_ctx ctx; |
96518518 | 1375 | |
7dab8ee3 | 1376 | nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
1377 | if (family == AF_UNSPEC || |
1378 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
1379 | return nft_flush(&ctx, family); |
1380 | ||
36dd1bcc PNA |
1381 | if (nla[NFTA_TABLE_HANDLE]) { |
1382 | attr = nla[NFTA_TABLE_HANDLE]; | |
e31f072f PNA |
1383 | table = nft_table_lookup_byhandle(net, attr, genmask, |
1384 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
1385 | } else { |
1386 | attr = nla[NFTA_TABLE_NAME]; | |
6001a930 PNA |
1387 | table = nft_table_lookup(net, attr, family, genmask, |
1388 | NETLINK_CB(skb).portid); | |
36dd1bcc | 1389 | } |
3ecbfd65 | 1390 | |
36dd1bcc PNA |
1391 | if (IS_ERR(table)) { |
1392 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 1393 | return PTR_ERR(table); |
36dd1bcc | 1394 | } |
96518518 | 1395 | |
7dab8ee3 | 1396 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
a8278400 PNA |
1397 | table->use > 0) |
1398 | return -EBUSY; | |
1399 | ||
98319cb9 | 1400 | ctx.family = family; |
b9ac12ef | 1401 | ctx.table = table; |
55dd6f93 | 1402 | |
b9ac12ef | 1403 | return nft_flush_table(&ctx); |
96518518 PM |
1404 | } |
1405 | ||
55dd6f93 PNA |
1406 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
1407 | { | |
fa5950e4 FW |
1408 | if (WARN_ON(ctx->table->use > 0)) |
1409 | return; | |
4fefee57 | 1410 | |
1b2470e5 | 1411 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 1412 | kfree(ctx->table->name); |
bc7a7082 | 1413 | kfree(ctx->table->udata); |
55dd6f93 | 1414 | kfree(ctx->table); |
55dd6f93 PNA |
1415 | } |
1416 | ||
cc07eeb0 | 1417 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1418 | { |
96518518 | 1419 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
82603549 | 1420 | if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) { |
cc07eeb0 PNA |
1421 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1422 | return; | |
96518518 | 1423 | } |
9370761c | 1424 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 1425 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 1426 | } |
9370761c | 1427 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 1428 | |
32537e91 | 1429 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1430 | { |
96518518 | 1431 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 1432 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
1433 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1434 | } | |
9370761c | 1435 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
1436 | |
1437 | /* | |
1438 | * Chains | |
1439 | */ | |
1440 | ||
1441 | static struct nft_chain * | |
cac20fcd | 1442 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1443 | { |
1444 | struct nft_chain *chain; | |
1445 | ||
1446 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1447 | if (chain->handle == handle && |
1448 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1449 | return chain; |
1450 | } | |
1451 | ||
1452 | return ERR_PTR(-ENOENT); | |
1453 | } | |
1454 | ||
4d44175a | 1455 | static bool lockdep_commit_lock_is_held(const struct net *net) |
f102d66b FW |
1456 | { |
1457 | #ifdef CONFIG_PROVE_LOCKING | |
d59d2f82 | 1458 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
1459 | |
1460 | return lockdep_is_held(&nft_net->commit_mutex); | |
f102d66b FW |
1461 | #else |
1462 | return true; | |
1463 | #endif | |
1464 | } | |
1465 | ||
1466 | static struct nft_chain *nft_chain_lookup(struct net *net, | |
1467 | struct nft_table *table, | |
cac20fcd | 1468 | const struct nlattr *nla, u8 genmask) |
96518518 | 1469 | { |
1b2470e5 FW |
1470 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1471 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1472 | struct nft_chain *chain; |
1473 | ||
1474 | if (nla == NULL) | |
1475 | return ERR_PTR(-EINVAL); | |
1476 | ||
872f6903 | 1477 | nla_strscpy(search, nla, sizeof(search)); |
96518518 | 1478 | |
1b2470e5 | 1479 | WARN_ON(!rcu_read_lock_held() && |
f102d66b | 1480 | !lockdep_commit_lock_is_held(net)); |
1b2470e5 FW |
1481 | |
1482 | chain = ERR_PTR(-ENOENT); | |
1483 | rcu_read_lock(); | |
1484 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1485 | if (!list) | |
1486 | goto out_unlock; | |
1487 | ||
1488 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1489 | if (nft_active_genmask(chain, genmask)) | |
1490 | goto out_unlock; | |
1491 | } | |
1492 | chain = ERR_PTR(-ENOENT); | |
1493 | out_unlock: | |
1494 | rcu_read_unlock(); | |
1495 | return chain; | |
96518518 PM |
1496 | } |
1497 | ||
1498 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1499 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1500 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1501 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1502 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1503 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1504 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1505 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
9332d27d FW |
1506 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, |
1507 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
0ca743a5 | 1508 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
c9626a2c | 1509 | [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, |
74cccc3d | 1510 | [NFTA_CHAIN_ID] = { .type = NLA_U32 }, |
002f2176 JGG |
1511 | [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY, |
1512 | .len = NFT_USERDATA_MAXLEN }, | |
96518518 PM |
1513 | }; |
1514 | ||
1515 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1516 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1517 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1518 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1519 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1520 | }; |
1521 | ||
0ca743a5 PNA |
1522 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1523 | { | |
1524 | struct nft_stats *cpu_stats, total; | |
1525 | struct nlattr *nest; | |
ce355e20 ED |
1526 | unsigned int seq; |
1527 | u64 pkts, bytes; | |
0ca743a5 PNA |
1528 | int cpu; |
1529 | ||
edbd82c5 FW |
1530 | if (!stats) |
1531 | return 0; | |
1532 | ||
0ca743a5 PNA |
1533 | memset(&total, 0, sizeof(total)); |
1534 | for_each_possible_cpu(cpu) { | |
1535 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 | 1536 | do { |
d120d1a6 | 1537 | seq = u64_stats_fetch_begin(&cpu_stats->syncp); |
ce355e20 ED |
1538 | pkts = cpu_stats->pkts; |
1539 | bytes = cpu_stats->bytes; | |
d120d1a6 | 1540 | } while (u64_stats_fetch_retry(&cpu_stats->syncp, seq)); |
ce355e20 ED |
1541 | total.pkts += pkts; |
1542 | total.bytes += bytes; | |
0ca743a5 | 1543 | } |
ae0be8de | 1544 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS); |
0ca743a5 PNA |
1545 | if (nest == NULL) |
1546 | goto nla_put_failure; | |
1547 | ||
b46f6ded ND |
1548 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
1549 | NFTA_COUNTER_PAD) || | |
1550 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
1551 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
1552 | goto nla_put_failure; |
1553 | ||
1554 | nla_nest_end(skb, nest); | |
1555 | return 0; | |
1556 | ||
1557 | nla_put_failure: | |
1558 | return -ENOSPC; | |
1559 | } | |
1560 | ||
d54725cd PNA |
1561 | static int nft_dump_basechain_hook(struct sk_buff *skb, int family, |
1562 | const struct nft_base_chain *basechain) | |
1563 | { | |
1564 | const struct nf_hook_ops *ops = &basechain->ops; | |
1565 | struct nft_hook *hook, *first = NULL; | |
1566 | struct nlattr *nest, *nest_devs; | |
1567 | int n = 0; | |
1568 | ||
1569 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK); | |
1570 | if (nest == NULL) | |
1571 | goto nla_put_failure; | |
1572 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1573 | goto nla_put_failure; | |
1574 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1575 | goto nla_put_failure; | |
1576 | ||
d3519cb8 | 1577 | if (nft_base_chain_netdev(family, ops->hooknum)) { |
d54725cd PNA |
1578 | nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS); |
1579 | list_for_each_entry(hook, &basechain->hook_list, list) { | |
1580 | if (!first) | |
1581 | first = hook; | |
1582 | ||
1583 | if (nla_put_string(skb, NFTA_DEVICE_NAME, | |
1584 | hook->ops.dev->name)) | |
1585 | goto nla_put_failure; | |
1586 | n++; | |
1587 | } | |
1588 | nla_nest_end(skb, nest_devs); | |
1589 | ||
1590 | if (n == 1 && | |
1591 | nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name)) | |
1592 | goto nla_put_failure; | |
1593 | } | |
1594 | nla_nest_end(skb, nest); | |
1595 | ||
1596 | return 0; | |
1597 | nla_put_failure: | |
1598 | return -1; | |
1599 | } | |
1600 | ||
84d7fce6 PNA |
1601 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
1602 | u32 portid, u32 seq, int event, u32 flags, | |
1603 | int family, const struct nft_table *table, | |
96518518 PM |
1604 | const struct nft_chain *chain) |
1605 | { | |
1606 | struct nlmsghdr *nlh; | |
96518518 | 1607 | |
dedb67c4 | 1608 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
1609 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
1610 | NFNETLINK_V0, nft_base_seq(net)); | |
1611 | if (!nlh) | |
96518518 PM |
1612 | goto nla_put_failure; |
1613 | ||
96518518 PM |
1614 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) |
1615 | goto nla_put_failure; | |
b46f6ded ND |
1616 | if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), |
1617 | NFTA_CHAIN_PAD)) | |
96518518 PM |
1618 | goto nla_put_failure; |
1619 | if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) | |
1620 | goto nla_put_failure; | |
1621 | ||
f323d954 | 1622 | if (nft_is_base_chain(chain)) { |
0ca743a5 | 1623 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
edbd82c5 | 1624 | struct nft_stats __percpu *stats; |
0ca743a5 | 1625 | |
d54725cd | 1626 | if (nft_dump_basechain_hook(skb, family, basechain)) |
2cbce139 | 1627 | goto nla_put_failure; |
9370761c | 1628 | |
0ca743a5 PNA |
1629 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1630 | htonl(basechain->policy))) | |
1631 | goto nla_put_failure; | |
1632 | ||
baae3e62 PM |
1633 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1634 | goto nla_put_failure; | |
0ca743a5 | 1635 | |
edbd82c5 FW |
1636 | stats = rcu_dereference_check(basechain->stats, |
1637 | lockdep_commit_lock_is_held(net)); | |
1638 | if (nft_dump_stats(skb, stats)) | |
0ca743a5 | 1639 | goto nla_put_failure; |
96518518 PM |
1640 | } |
1641 | ||
d0e2c7de PNA |
1642 | if (chain->flags && |
1643 | nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags))) | |
1644 | goto nla_put_failure; | |
1645 | ||
0ca743a5 PNA |
1646 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1647 | goto nla_put_failure; | |
1648 | ||
002f2176 JGG |
1649 | if (chain->udata && |
1650 | nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata)) | |
1651 | goto nla_put_failure; | |
1652 | ||
053c095a JB |
1653 | nlmsg_end(skb, nlh); |
1654 | return 0; | |
96518518 PM |
1655 | |
1656 | nla_put_failure: | |
1657 | nlmsg_trim(skb, nlh); | |
1658 | return -1; | |
1659 | } | |
1660 | ||
25e94a99 | 1661 | static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
96518518 | 1662 | { |
0854db2a | 1663 | struct nftables_pernet *nft_net; |
96518518 | 1664 | struct sk_buff *skb; |
6fb721cf | 1665 | u16 flags = 0; |
96518518 PM |
1666 | int err; |
1667 | ||
128ad332 PNA |
1668 | if (!ctx->report && |
1669 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1670 | return; |
96518518 | 1671 | |
96518518 PM |
1672 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1673 | if (skb == NULL) | |
1674 | goto err; | |
1675 | ||
6fb721cf PNA |
1676 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
1677 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
1678 | ||
84d7fce6 | 1679 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 1680 | event, flags, ctx->family, ctx->table, |
35151d84 | 1681 | ctx->chain); |
96518518 PM |
1682 | if (err < 0) { |
1683 | kfree_skb(skb); | |
1684 | goto err; | |
1685 | } | |
1686 | ||
d59d2f82 | 1687 | nft_net = nft_pernet(ctx->net); |
0854db2a | 1688 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 1689 | return; |
96518518 | 1690 | err: |
25e94a99 | 1691 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1692 | } |
1693 | ||
1694 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1695 | struct netlink_callback *cb) | |
1696 | { | |
1697 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 | 1698 | unsigned int idx = 0, s_idx = cb->args[0]; |
99633ab2 | 1699 | struct net *net = sock_net(skb->sk); |
96518518 | 1700 | int family = nfmsg->nfgen_family; |
0854db2a | 1701 | struct nftables_pernet *nft_net; |
d59d2f82 PNA |
1702 | const struct nft_table *table; |
1703 | const struct nft_chain *chain; | |
96518518 | 1704 | |
e688a7f8 | 1705 | rcu_read_lock(); |
d59d2f82 | 1706 | nft_net = nft_pernet(net); |
34002783 | 1707 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 1708 | |
0854db2a | 1709 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 1710 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1711 | continue; |
1712 | ||
36596dad PNA |
1713 | list_for_each_entry_rcu(chain, &table->chains, list) { |
1714 | if (idx < s_idx) | |
1715 | goto cont; | |
1716 | if (idx > s_idx) | |
1717 | memset(&cb->args[1], 0, | |
1718 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1719 | if (!nft_is_active(net, chain)) | |
1720 | continue; | |
1721 | if (nf_tables_fill_chain_info(skb, net, | |
1722 | NETLINK_CB(cb->skb).portid, | |
1723 | cb->nlh->nlmsg_seq, | |
1724 | NFT_MSG_NEWCHAIN, | |
1725 | NLM_F_MULTI, | |
98319cb9 | 1726 | table->family, table, |
36596dad PNA |
1727 | chain) < 0) |
1728 | goto done; | |
38e029f1 | 1729 | |
36596dad | 1730 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
96518518 | 1731 | cont: |
36596dad | 1732 | idx++; |
96518518 PM |
1733 | } |
1734 | } | |
1735 | done: | |
e688a7f8 | 1736 | rcu_read_unlock(); |
96518518 PM |
1737 | cb->args[0] = idx; |
1738 | return skb->len; | |
1739 | } | |
1740 | ||
d9adf22a | 1741 | /* called with rcu_read_lock held */ |
797d4980 PNA |
1742 | static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info, |
1743 | const struct nlattr * const nla[]) | |
96518518 | 1744 | { |
797d4980 PNA |
1745 | struct netlink_ext_ack *extack = info->extack; |
1746 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 1747 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 1748 | const struct nft_chain *chain; |
797d4980 | 1749 | struct net *net = info->net; |
1b2470e5 | 1750 | struct nft_table *table; |
96518518 | 1751 | struct sk_buff *skb2; |
96518518 PM |
1752 | int err; |
1753 | ||
797d4980 | 1754 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
1755 | struct netlink_dump_control c = { |
1756 | .dump = nf_tables_dump_chains, | |
d9adf22a | 1757 | .module = THIS_MODULE, |
96518518 | 1758 | }; |
d9adf22a | 1759 | |
797d4980 | 1760 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
1761 | } |
1762 | ||
6001a930 | 1763 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0); |
36dd1bcc PNA |
1764 | if (IS_ERR(table)) { |
1765 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1766 | return PTR_ERR(table); |
36dd1bcc | 1767 | } |
96518518 | 1768 | |
f102d66b | 1769 | chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask); |
36dd1bcc PNA |
1770 | if (IS_ERR(chain)) { |
1771 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
96518518 | 1772 | return PTR_ERR(chain); |
36dd1bcc | 1773 | } |
96518518 | 1774 | |
d9adf22a | 1775 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1776 | if (!skb2) |
1777 | return -ENOMEM; | |
1778 | ||
84d7fce6 | 1779 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
1780 | info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, |
1781 | 0, family, table, chain); | |
96518518 | 1782 | if (err < 0) |
ee921183 | 1783 | goto err_fill_chain_info; |
96518518 | 1784 | |
ee921183 | 1785 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 1786 | |
ee921183 | 1787 | err_fill_chain_info: |
96518518 PM |
1788 | kfree_skb(skb2); |
1789 | return err; | |
1790 | } | |
1791 | ||
0ca743a5 PNA |
1792 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1793 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1794 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1795 | }; | |
1796 | ||
ff3cd7b3 | 1797 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1798 | { |
1799 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1800 | struct nft_stats __percpu *newstats; | |
1801 | struct nft_stats *stats; | |
1802 | int err; | |
1803 | ||
8cb08174 JB |
1804 | err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr, |
1805 | nft_counter_policy, NULL); | |
0ca743a5 | 1806 | if (err < 0) |
ff3cd7b3 | 1807 | return ERR_PTR(err); |
0ca743a5 PNA |
1808 | |
1809 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1810 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1811 | |
ce355e20 | 1812 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1813 | if (newstats == NULL) |
ff3cd7b3 | 1814 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1815 | |
1816 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1817 | * are not exposed to userspace. | |
1818 | */ | |
e8781f70 | 1819 | preempt_disable(); |
0ca743a5 PNA |
1820 | stats = this_cpu_ptr(newstats); |
1821 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
1822 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 1823 | preempt_enable(); |
0ca743a5 | 1824 | |
ff3cd7b3 PNA |
1825 | return newstats; |
1826 | } | |
1827 | ||
53315ac6 | 1828 | static void nft_chain_stats_replace(struct nft_trans *trans) |
ff3cd7b3 | 1829 | { |
53315ac6 | 1830 | struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain); |
0befd061 | 1831 | |
53315ac6 | 1832 | if (!nft_trans_chain_stats(trans)) |
b88825de PNA |
1833 | return; |
1834 | ||
b685b534 PM |
1835 | nft_trans_chain_stats(trans) = |
1836 | rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans), | |
1837 | lockdep_commit_lock_is_held(trans->ctx.net)); | |
53315ac6 FW |
1838 | |
1839 | if (!nft_trans_chain_stats(trans)) | |
bbb8c61f | 1840 | static_branch_inc(&nft_counters_enabled); |
0ca743a5 PNA |
1841 | } |
1842 | ||
0cbc06b3 FW |
1843 | static void nf_tables_chain_free_chain_rules(struct nft_chain *chain) |
1844 | { | |
2c865a8a PNA |
1845 | struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0); |
1846 | struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1); | |
0cbc06b3 FW |
1847 | |
1848 | if (g0 != g1) | |
1849 | kvfree(g1); | |
1850 | kvfree(g0); | |
1851 | ||
1852 | /* should be NULL either via abort or via successful commit */ | |
2c865a8a PNA |
1853 | WARN_ON_ONCE(chain->blob_next); |
1854 | kvfree(chain->blob_next); | |
0cbc06b3 FW |
1855 | } |
1856 | ||
d0e2c7de | 1857 | void nf_tables_chain_destroy(struct nft_ctx *ctx) |
91c7b38d | 1858 | { |
43a605f2 | 1859 | struct nft_chain *chain = ctx->chain; |
d54725cd | 1860 | struct nft_hook *hook, *next; |
43a605f2 | 1861 | |
fa5950e4 FW |
1862 | if (WARN_ON(chain->use > 0)) |
1863 | return; | |
91c7b38d | 1864 | |
0cbc06b3 FW |
1865 | /* no concurrent access possible anymore */ |
1866 | nf_tables_chain_free_chain_rules(chain); | |
1867 | ||
f323d954 | 1868 | if (nft_is_base_chain(chain)) { |
2cbce139 PNA |
1869 | struct nft_base_chain *basechain = nft_base_chain(chain); |
1870 | ||
d3519cb8 | 1871 | if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) { |
d54725cd PNA |
1872 | list_for_each_entry_safe(hook, next, |
1873 | &basechain->hook_list, list) { | |
1874 | list_del_rcu(&hook->list); | |
1875 | kfree_rcu(hook, rcu); | |
1876 | } | |
1877 | } | |
2cbce139 | 1878 | module_put(basechain->type->owner); |
4c05ec47 | 1879 | if (rcu_access_pointer(basechain->stats)) { |
9f08ea84 | 1880 | static_branch_dec(&nft_counters_enabled); |
4c05ec47 TY |
1881 | free_percpu(rcu_dereference_raw(basechain->stats)); |
1882 | } | |
b7263e07 | 1883 | kfree(chain->name); |
002f2176 | 1884 | kfree(chain->udata); |
2cbce139 | 1885 | kfree(basechain); |
91c7b38d | 1886 | } else { |
b7263e07 | 1887 | kfree(chain->name); |
002f2176 | 1888 | kfree(chain->udata); |
91c7b38d PNA |
1889 | kfree(chain); |
1890 | } | |
1891 | } | |
1892 | ||
3f0465a9 PNA |
1893 | static struct nft_hook *nft_netdev_hook_alloc(struct net *net, |
1894 | const struct nlattr *attr) | |
1895 | { | |
1896 | struct net_device *dev; | |
1897 | char ifname[IFNAMSIZ]; | |
1898 | struct nft_hook *hook; | |
1899 | int err; | |
1900 | ||
33758c89 | 1901 | hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT); |
3f0465a9 PNA |
1902 | if (!hook) { |
1903 | err = -ENOMEM; | |
1904 | goto err_hook_alloc; | |
1905 | } | |
1906 | ||
872f6903 | 1907 | nla_strscpy(ifname, attr, IFNAMSIZ); |
42f1c271 PNA |
1908 | /* nf_tables_netdev_event() is called under rtnl_mutex, this is |
1909 | * indirectly serializing all the other holders of the commit_mutex with | |
1910 | * the rtnl_mutex. | |
1911 | */ | |
3f0465a9 PNA |
1912 | dev = __dev_get_by_name(net, ifname); |
1913 | if (!dev) { | |
1914 | err = -ENOENT; | |
1915 | goto err_hook_dev; | |
1916 | } | |
1917 | hook->ops.dev = dev; | |
1918 | ||
1919 | return hook; | |
1920 | ||
1921 | err_hook_dev: | |
1922 | kfree(hook); | |
1923 | err_hook_alloc: | |
1924 | return ERR_PTR(err); | |
1925 | } | |
1926 | ||
abadb2f8 PNA |
1927 | static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, |
1928 | const struct nft_hook *this) | |
b75a3e83 PNA |
1929 | { |
1930 | struct nft_hook *hook; | |
1931 | ||
1932 | list_for_each_entry(hook, hook_list, list) { | |
1933 | if (this->ops.dev == hook->ops.dev) | |
abadb2f8 | 1934 | return hook; |
b75a3e83 PNA |
1935 | } |
1936 | ||
abadb2f8 | 1937 | return NULL; |
b75a3e83 PNA |
1938 | } |
1939 | ||
3f0465a9 PNA |
1940 | static int nf_tables_parse_netdev_hooks(struct net *net, |
1941 | const struct nlattr *attr, | |
1942 | struct list_head *hook_list) | |
1943 | { | |
1944 | struct nft_hook *hook, *next; | |
1945 | const struct nlattr *tmp; | |
1946 | int rem, n = 0, err; | |
1947 | ||
1948 | nla_for_each_nested(tmp, attr, rem) { | |
1949 | if (nla_type(tmp) != NFTA_DEVICE_NAME) { | |
1950 | err = -EINVAL; | |
1951 | goto err_hook; | |
1952 | } | |
1953 | ||
1954 | hook = nft_netdev_hook_alloc(net, tmp); | |
1955 | if (IS_ERR(hook)) { | |
1956 | err = PTR_ERR(hook); | |
1957 | goto err_hook; | |
1958 | } | |
b75a3e83 | 1959 | if (nft_hook_list_find(hook_list, hook)) { |
cd77e75b | 1960 | kfree(hook); |
b75a3e83 PNA |
1961 | err = -EEXIST; |
1962 | goto err_hook; | |
1963 | } | |
3f0465a9 PNA |
1964 | list_add_tail(&hook->list, hook_list); |
1965 | n++; | |
1966 | ||
cb662ac6 | 1967 | if (n == NFT_NETDEVICE_MAX) { |
3f0465a9 PNA |
1968 | err = -EFBIG; |
1969 | goto err_hook; | |
1970 | } | |
1971 | } | |
3f0465a9 PNA |
1972 | |
1973 | return 0; | |
1974 | ||
1975 | err_hook: | |
1976 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
1977 | list_del(&hook->list); | |
1978 | kfree(hook); | |
1979 | } | |
1980 | return err; | |
1981 | } | |
1982 | ||
508f8ccd PNA |
1983 | struct nft_chain_hook { |
1984 | u32 num; | |
84ba7dd7 | 1985 | s32 priority; |
32537e91 | 1986 | const struct nft_chain_type *type; |
d54725cd | 1987 | struct list_head list; |
508f8ccd PNA |
1988 | }; |
1989 | ||
d54725cd PNA |
1990 | static int nft_chain_parse_netdev(struct net *net, |
1991 | struct nlattr *tb[], | |
1992 | struct list_head *hook_list) | |
1993 | { | |
1994 | struct nft_hook *hook; | |
1995 | int err; | |
1996 | ||
1997 | if (tb[NFTA_HOOK_DEV]) { | |
1998 | hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]); | |
1999 | if (IS_ERR(hook)) | |
2000 | return PTR_ERR(hook); | |
2001 | ||
2002 | list_add_tail(&hook->list, hook_list); | |
2003 | } else if (tb[NFTA_HOOK_DEVS]) { | |
2004 | err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS], | |
2005 | hook_list); | |
2006 | if (err < 0) | |
2007 | return err; | |
05abe445 PNA |
2008 | |
2009 | if (list_empty(hook_list)) | |
2010 | return -EINVAL; | |
d54725cd PNA |
2011 | } else { |
2012 | return -EINVAL; | |
2013 | } | |
2014 | ||
2015 | return 0; | |
2016 | } | |
2017 | ||
508f8ccd PNA |
2018 | static int nft_chain_parse_hook(struct net *net, |
2019 | const struct nlattr * const nla[], | |
36596dad | 2020 | struct nft_chain_hook *hook, u8 family, |
983c4fcb | 2021 | struct netlink_ext_ack *extack, bool autoload) |
508f8ccd | 2022 | { |
d59d2f82 | 2023 | struct nftables_pernet *nft_net = nft_pernet(net); |
508f8ccd | 2024 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; |
32537e91 | 2025 | const struct nft_chain_type *type; |
508f8ccd PNA |
2026 | int err; |
2027 | ||
0854db2a | 2028 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b FW |
2029 | lockdep_nfnl_nft_mutex_not_held(); |
2030 | ||
8cb08174 JB |
2031 | err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX, |
2032 | nla[NFTA_CHAIN_HOOK], | |
2033 | nft_hook_policy, NULL); | |
508f8ccd PNA |
2034 | if (err < 0) |
2035 | return err; | |
2036 | ||
2037 | if (ha[NFTA_HOOK_HOOKNUM] == NULL || | |
2038 | ha[NFTA_HOOK_PRIORITY] == NULL) | |
2039 | return -EINVAL; | |
2040 | ||
2041 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
508f8ccd PNA |
2042 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); |
2043 | ||
82603549 PNA |
2044 | type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT); |
2045 | if (!type) | |
2046 | return -EOPNOTSUPP; | |
2047 | ||
508f8ccd | 2048 | if (nla[NFTA_CHAIN_TYPE]) { |
452238e8 | 2049 | type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE], |
445509eb | 2050 | family, autoload); |
983c4fcb PNA |
2051 | if (IS_ERR(type)) { |
2052 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
508f8ccd | 2053 | return PTR_ERR(type); |
983c4fcb | 2054 | } |
508f8ccd | 2055 | } |
d25e2e93 | 2056 | if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num))) |
508f8ccd | 2057 | return -EOPNOTSUPP; |
84ba7dd7 FW |
2058 | |
2059 | if (type->type == NFT_CHAIN_T_NAT && | |
2060 | hook->priority <= NF_IP_PRI_CONNTRACK) | |
2061 | return -EOPNOTSUPP; | |
2062 | ||
983c4fcb PNA |
2063 | if (!try_module_get(type->owner)) { |
2064 | if (nla[NFTA_CHAIN_TYPE]) | |
2065 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
508f8ccd | 2066 | return -ENOENT; |
983c4fcb | 2067 | } |
508f8ccd PNA |
2068 | |
2069 | hook->type = type; | |
2070 | ||
d54725cd | 2071 | INIT_LIST_HEAD(&hook->list); |
d3519cb8 | 2072 | if (nft_base_chain_netdev(family, hook->num)) { |
d54725cd PNA |
2073 | err = nft_chain_parse_netdev(net, ha, &hook->list); |
2074 | if (err < 0) { | |
508f8ccd | 2075 | module_put(type->owner); |
d54725cd | 2076 | return err; |
508f8ccd | 2077 | } |
d54725cd | 2078 | } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) { |
508f8ccd PNA |
2079 | module_put(type->owner); |
2080 | return -EOPNOTSUPP; | |
2081 | } | |
2082 | ||
2083 | return 0; | |
2084 | } | |
2085 | ||
2086 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
2087 | { | |
d54725cd PNA |
2088 | struct nft_hook *h, *next; |
2089 | ||
2090 | list_for_each_entry_safe(h, next, &hook->list, list) { | |
2091 | list_del(&h->list); | |
2092 | kfree(h); | |
2093 | } | |
508f8ccd | 2094 | module_put(hook->type->owner); |
508f8ccd PNA |
2095 | } |
2096 | ||
0cbc06b3 FW |
2097 | struct nft_rules_old { |
2098 | struct rcu_head h; | |
2c865a8a | 2099 | struct nft_rule_blob *blob; |
0cbc06b3 FW |
2100 | }; |
2101 | ||
2c865a8a | 2102 | static void nft_last_rule(struct nft_rule_blob *blob, const void *ptr) |
0cbc06b3 | 2103 | { |
2c865a8a PNA |
2104 | struct nft_rule_dp *prule; |
2105 | ||
2106 | prule = (struct nft_rule_dp *)ptr; | |
2107 | prule->is_last = 1; | |
2c865a8a PNA |
2108 | /* blob size does not include the trailer rule */ |
2109 | } | |
2110 | ||
2111 | static struct nft_rule_blob *nf_tables_chain_alloc_rules(unsigned int size) | |
2112 | { | |
2113 | struct nft_rule_blob *blob; | |
2114 | ||
2115 | /* size must include room for the last rule */ | |
2116 | if (size < offsetof(struct nft_rule_dp, data)) | |
2117 | return NULL; | |
2118 | ||
2119 | size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rules_old); | |
2120 | if (size > INT_MAX) | |
0cbc06b3 FW |
2121 | return NULL; |
2122 | ||
33758c89 | 2123 | blob = kvmalloc(size, GFP_KERNEL_ACCOUNT); |
2c865a8a | 2124 | if (!blob) |
0cbc06b3 FW |
2125 | return NULL; |
2126 | ||
2c865a8a PNA |
2127 | blob->size = 0; |
2128 | nft_last_rule(blob, blob->data); | |
0cbc06b3 | 2129 | |
2c865a8a | 2130 | return blob; |
0cbc06b3 FW |
2131 | } |
2132 | ||
d54725cd PNA |
2133 | static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family, |
2134 | const struct nft_chain_hook *hook, | |
2135 | struct nft_chain *chain) | |
2136 | { | |
c5c6accd FW |
2137 | ops->pf = family; |
2138 | ops->hooknum = hook->num; | |
2139 | ops->priority = hook->priority; | |
2140 | ops->priv = chain; | |
2141 | ops->hook = hook->type->hooks[ops->hooknum]; | |
2142 | ops->hook_ops_type = NF_HOOK_OP_NF_TABLES; | |
d54725cd PNA |
2143 | } |
2144 | ||
2145 | static int nft_basechain_init(struct nft_base_chain *basechain, u8 family, | |
2146 | struct nft_chain_hook *hook, u32 flags) | |
2147 | { | |
2148 | struct nft_chain *chain; | |
2149 | struct nft_hook *h; | |
2150 | ||
2151 | basechain->type = hook->type; | |
2152 | INIT_LIST_HEAD(&basechain->hook_list); | |
2153 | chain = &basechain->chain; | |
2154 | ||
d3519cb8 | 2155 | if (nft_base_chain_netdev(family, hook->num)) { |
d54725cd PNA |
2156 | list_splice_init(&hook->list, &basechain->hook_list); |
2157 | list_for_each_entry(h, &basechain->hook_list, list) | |
2158 | nft_basechain_hook_init(&h->ops, family, hook, chain); | |
2159 | ||
2160 | basechain->ops.hooknum = hook->num; | |
2161 | basechain->ops.priority = hook->priority; | |
2162 | } else { | |
2163 | nft_basechain_hook_init(&basechain->ops, family, hook, chain); | |
2164 | } | |
2165 | ||
67c49de4 | 2166 | chain->flags |= NFT_CHAIN_BASE | flags; |
d54725cd PNA |
2167 | basechain->policy = NF_ACCEPT; |
2168 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD && | |
77972a36 PNA |
2169 | !nft_chain_offload_support(basechain)) { |
2170 | list_splice_init(&basechain->hook_list, &hook->list); | |
d54725cd | 2171 | return -EOPNOTSUPP; |
77972a36 | 2172 | } |
d54725cd PNA |
2173 | |
2174 | flow_block_init(&basechain->flow_block); | |
2175 | ||
2176 | return 0; | |
2177 | } | |
2178 | ||
04b7db41 PNA |
2179 | static int nft_chain_add(struct nft_table *table, struct nft_chain *chain) |
2180 | { | |
2181 | int err; | |
2182 | ||
2183 | err = rhltable_insert_key(&table->chains_ht, chain->name, | |
2184 | &chain->rhlhead, nft_chain_ht_params); | |
2185 | if (err) | |
2186 | return err; | |
2187 | ||
2188 | list_add_tail_rcu(&chain->list, &table->chains); | |
2189 | ||
2190 | return 0; | |
2191 | } | |
2192 | ||
d0e2c7de PNA |
2193 | static u64 chain_id; |
2194 | ||
4035285f | 2195 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
983c4fcb PNA |
2196 | u8 policy, u32 flags, |
2197 | struct netlink_ext_ack *extack) | |
4035285f PNA |
2198 | { |
2199 | const struct nlattr * const *nla = ctx->nla; | |
2200 | struct nft_table *table = ctx->table; | |
4035285f | 2201 | struct nft_base_chain *basechain; |
4035285f | 2202 | struct net *net = ctx->net; |
d0e2c7de | 2203 | char name[NFT_NAME_MAXLEN]; |
2c865a8a | 2204 | struct nft_rule_blob *blob; |
66293c46 | 2205 | struct nft_trans *trans; |
4035285f | 2206 | struct nft_chain *chain; |
2c865a8a | 2207 | unsigned int data_size; |
4035285f PNA |
2208 | int err; |
2209 | ||
2210 | if (table->use == UINT_MAX) | |
2211 | return -EOVERFLOW; | |
2212 | ||
2213 | if (nla[NFTA_CHAIN_HOOK]) { | |
921ebde3 | 2214 | struct nft_stats __percpu *stats = NULL; |
4035285f | 2215 | struct nft_chain_hook hook; |
4035285f | 2216 | |
d0e2c7de PNA |
2217 | if (flags & NFT_CHAIN_BINDING) |
2218 | return -EOPNOTSUPP; | |
2219 | ||
983c4fcb PNA |
2220 | err = nft_chain_parse_hook(net, nla, &hook, family, extack, |
2221 | true); | |
4035285f PNA |
2222 | if (err < 0) |
2223 | return err; | |
2224 | ||
33758c89 | 2225 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2226 | if (basechain == NULL) { |
2227 | nft_chain_release_hook(&hook); | |
2228 | return -ENOMEM; | |
2229 | } | |
d54725cd | 2230 | chain = &basechain->chain; |
4035285f PNA |
2231 | |
2232 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2233 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2234 | if (IS_ERR(stats)) { | |
2235 | nft_chain_release_hook(&hook); | |
2236 | kfree(basechain); | |
2237 | return PTR_ERR(stats); | |
2238 | } | |
4c05ec47 | 2239 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
2240 | } |
2241 | ||
d54725cd PNA |
2242 | err = nft_basechain_init(basechain, family, &hook, flags); |
2243 | if (err < 0) { | |
2244 | nft_chain_release_hook(&hook); | |
2245 | kfree(basechain); | |
9a4d6dd5 | 2246 | free_percpu(stats); |
d54725cd PNA |
2247 | return err; |
2248 | } | |
921ebde3 TH |
2249 | if (stats) |
2250 | static_branch_inc(&nft_counters_enabled); | |
4035285f | 2251 | } else { |
d0e2c7de PNA |
2252 | if (flags & NFT_CHAIN_BASE) |
2253 | return -EINVAL; | |
2254 | if (flags & NFT_CHAIN_HW_OFFLOAD) | |
2255 | return -EOPNOTSUPP; | |
2256 | ||
33758c89 | 2257 | chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2258 | if (chain == NULL) |
2259 | return -ENOMEM; | |
d0e2c7de PNA |
2260 | |
2261 | chain->flags = flags; | |
4035285f | 2262 | } |
43a605f2 PNA |
2263 | ctx->chain = chain; |
2264 | ||
4035285f PNA |
2265 | INIT_LIST_HEAD(&chain->rules); |
2266 | chain->handle = nf_tables_alloc_handle(table); | |
2267 | chain->table = table; | |
d0e2c7de PNA |
2268 | |
2269 | if (nla[NFTA_CHAIN_NAME]) { | |
33758c89 | 2270 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
d0e2c7de | 2271 | } else { |
59136aa3 FW |
2272 | if (!(flags & NFT_CHAIN_BINDING)) { |
2273 | err = -EINVAL; | |
002f2176 | 2274 | goto err_destroy_chain; |
59136aa3 | 2275 | } |
d0e2c7de PNA |
2276 | |
2277 | snprintf(name, sizeof(name), "__chain%llu", ++chain_id); | |
33758c89 | 2278 | chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT); |
d0e2c7de PNA |
2279 | } |
2280 | ||
4035285f PNA |
2281 | if (!chain->name) { |
2282 | err = -ENOMEM; | |
002f2176 JGG |
2283 | goto err_destroy_chain; |
2284 | } | |
2285 | ||
2286 | if (nla[NFTA_CHAIN_USERDATA]) { | |
33758c89 | 2287 | chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT); |
002f2176 JGG |
2288 | if (chain->udata == NULL) { |
2289 | err = -ENOMEM; | |
2290 | goto err_destroy_chain; | |
2291 | } | |
2292 | chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]); | |
4035285f PNA |
2293 | } |
2294 | ||
2c865a8a PNA |
2295 | data_size = offsetof(struct nft_rule_dp, data); /* last rule */ |
2296 | blob = nf_tables_chain_alloc_rules(data_size); | |
2297 | if (!blob) { | |
0cbc06b3 | 2298 | err = -ENOMEM; |
002f2176 | 2299 | goto err_destroy_chain; |
0cbc06b3 FW |
2300 | } |
2301 | ||
2c865a8a PNA |
2302 | RCU_INIT_POINTER(chain->blob_gen_0, blob); |
2303 | RCU_INIT_POINTER(chain->blob_gen_1, blob); | |
0cbc06b3 | 2304 | |
c974a3a3 | 2305 | err = nf_tables_register_hook(net, table, chain); |
4035285f | 2306 | if (err < 0) |
002f2176 | 2307 | goto err_destroy_chain; |
4035285f | 2308 | |
66293c46 FW |
2309 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
2310 | if (IS_ERR(trans)) { | |
2311 | err = PTR_ERR(trans); | |
002f2176 | 2312 | goto err_unregister_hook; |
1b2470e5 | 2313 | } |
4035285f | 2314 | |
ad652f38 | 2315 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
2316 | if (nft_is_base_chain(chain)) |
2317 | nft_trans_chain_policy(trans) = policy; | |
2318 | ||
04b7db41 PNA |
2319 | err = nft_chain_add(table, chain); |
2320 | if (err < 0) { | |
2321 | nft_trans_destroy(trans); | |
002f2176 | 2322 | goto err_unregister_hook; |
04b7db41 PNA |
2323 | } |
2324 | ||
4035285f | 2325 | table->use++; |
4035285f PNA |
2326 | |
2327 | return 0; | |
002f2176 | 2328 | err_unregister_hook: |
c974a3a3 | 2329 | nf_tables_unregister_hook(net, table, chain); |
002f2176 | 2330 | err_destroy_chain: |
43a605f2 | 2331 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2332 | |
2333 | return err; | |
2334 | } | |
2335 | ||
d54725cd PNA |
2336 | static bool nft_hook_list_equal(struct list_head *hook_list1, |
2337 | struct list_head *hook_list2) | |
2338 | { | |
2339 | struct nft_hook *hook; | |
2340 | int n = 0, m = 0; | |
2341 | ||
2342 | n = 0; | |
2343 | list_for_each_entry(hook, hook_list2, list) { | |
2344 | if (!nft_hook_list_find(hook_list1, hook)) | |
2345 | return false; | |
2346 | ||
2347 | n++; | |
2348 | } | |
2349 | list_for_each_entry(hook, hook_list1, list) | |
2350 | m++; | |
2351 | ||
2352 | return n == m; | |
2353 | } | |
2354 | ||
c9626a2c | 2355 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
98a381a7 PNA |
2356 | u32 flags, const struct nlattr *attr, |
2357 | struct netlink_ext_ack *extack) | |
2c4a488a PNA |
2358 | { |
2359 | const struct nlattr * const *nla = ctx->nla; | |
2360 | struct nft_table *table = ctx->table; | |
2361 | struct nft_chain *chain = ctx->chain; | |
2c4a488a PNA |
2362 | struct nft_base_chain *basechain; |
2363 | struct nft_stats *stats = NULL; | |
2364 | struct nft_chain_hook hook; | |
2c4a488a PNA |
2365 | struct nf_hook_ops *ops; |
2366 | struct nft_trans *trans; | |
c974a3a3 | 2367 | int err; |
2c4a488a | 2368 | |
c9626a2c PNA |
2369 | if (chain->flags ^ flags) |
2370 | return -EOPNOTSUPP; | |
2371 | ||
2c4a488a | 2372 | if (nla[NFTA_CHAIN_HOOK]) { |
98a381a7 PNA |
2373 | if (!nft_is_base_chain(chain)) { |
2374 | NL_SET_BAD_ATTR(extack, attr); | |
77a92189 | 2375 | return -EEXIST; |
98a381a7 | 2376 | } |
36596dad | 2377 | err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family, |
983c4fcb | 2378 | extack, false); |
2c4a488a PNA |
2379 | if (err < 0) |
2380 | return err; | |
2381 | ||
2382 | basechain = nft_base_chain(chain); | |
2383 | if (basechain->type != hook.type) { | |
2384 | nft_chain_release_hook(&hook); | |
98a381a7 | 2385 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2386 | return -EEXIST; |
2c4a488a PNA |
2387 | } |
2388 | ||
d3519cb8 | 2389 | if (nft_base_chain_netdev(ctx->family, hook.num)) { |
d54725cd PNA |
2390 | if (!nft_hook_list_equal(&basechain->hook_list, |
2391 | &hook.list)) { | |
2392 | nft_chain_release_hook(&hook); | |
98a381a7 | 2393 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2394 | return -EEXIST; |
d54725cd PNA |
2395 | } |
2396 | } else { | |
2397 | ops = &basechain->ops; | |
2398 | if (ops->hooknum != hook.num || | |
2399 | ops->priority != hook.priority) { | |
2400 | nft_chain_release_hook(&hook); | |
98a381a7 | 2401 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2402 | return -EEXIST; |
d54725cd | 2403 | } |
2c4a488a PNA |
2404 | } |
2405 | nft_chain_release_hook(&hook); | |
2406 | } | |
2407 | ||
2408 | if (nla[NFTA_CHAIN_HANDLE] && | |
2409 | nla[NFTA_CHAIN_NAME]) { | |
2410 | struct nft_chain *chain2; | |
2411 | ||
f102d66b FW |
2412 | chain2 = nft_chain_lookup(ctx->net, table, |
2413 | nla[NFTA_CHAIN_NAME], genmask); | |
98a381a7 PNA |
2414 | if (!IS_ERR(chain2)) { |
2415 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
0d18779b | 2416 | return -EEXIST; |
98a381a7 | 2417 | } |
2c4a488a PNA |
2418 | } |
2419 | ||
2420 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2421 | if (!nft_is_base_chain(chain)) | |
2422 | return -EOPNOTSUPP; | |
2423 | ||
2424 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2425 | if (IS_ERR(stats)) | |
2426 | return PTR_ERR(stats); | |
2427 | } | |
2428 | ||
c6cc94df | 2429 | err = -ENOMEM; |
2c4a488a PNA |
2430 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2431 | sizeof(struct nft_trans_chain)); | |
c6cc94df FW |
2432 | if (trans == NULL) |
2433 | goto err; | |
2c4a488a PNA |
2434 | |
2435 | nft_trans_chain_stats(trans) = stats; | |
2436 | nft_trans_chain_update(trans) = true; | |
2437 | ||
2438 | if (nla[NFTA_CHAIN_POLICY]) | |
2439 | nft_trans_chain_policy(trans) = policy; | |
2440 | else | |
2441 | nft_trans_chain_policy(trans) = -1; | |
2442 | ||
c6cc94df FW |
2443 | if (nla[NFTA_CHAIN_HANDLE] && |
2444 | nla[NFTA_CHAIN_NAME]) { | |
d59d2f82 | 2445 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c6cc94df FW |
2446 | struct nft_trans *tmp; |
2447 | char *name; | |
2448 | ||
2449 | err = -ENOMEM; | |
33758c89 | 2450 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
c6cc94df FW |
2451 | if (!name) |
2452 | goto err; | |
2453 | ||
2454 | err = -EEXIST; | |
0854db2a | 2455 | list_for_each_entry(tmp, &nft_net->commit_list, list) { |
c6cc94df FW |
2456 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && |
2457 | tmp->ctx.table == table && | |
2458 | nft_trans_chain_update(tmp) && | |
2459 | nft_trans_chain_name(tmp) && | |
2460 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
98a381a7 | 2461 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); |
c6cc94df FW |
2462 | kfree(name); |
2463 | goto err; | |
2464 | } | |
2c4a488a | 2465 | } |
c6cc94df FW |
2466 | |
2467 | nft_trans_chain_name(trans) = name; | |
2c4a488a | 2468 | } |
0854db2a | 2469 | nft_trans_commit_list_add_tail(ctx->net, trans); |
2c4a488a PNA |
2470 | |
2471 | return 0; | |
c6cc94df FW |
2472 | err: |
2473 | free_percpu(stats); | |
2474 | kfree(trans); | |
2475 | return err; | |
2c4a488a PNA |
2476 | } |
2477 | ||
837830a4 | 2478 | static struct nft_chain *nft_chain_lookup_byid(const struct net *net, |
95f466d2 | 2479 | const struct nft_table *table, |
837830a4 PNA |
2480 | const struct nlattr *nla) |
2481 | { | |
d59d2f82 | 2482 | struct nftables_pernet *nft_net = nft_pernet(net); |
837830a4 PNA |
2483 | u32 id = ntohl(nla_get_be32(nla)); |
2484 | struct nft_trans *trans; | |
2485 | ||
0854db2a | 2486 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
837830a4 PNA |
2487 | struct nft_chain *chain = trans->ctx.chain; |
2488 | ||
2489 | if (trans->msg_type == NFT_MSG_NEWCHAIN && | |
95f466d2 | 2490 | chain->table == table && |
837830a4 PNA |
2491 | id == nft_trans_chain_id(trans)) |
2492 | return chain; | |
2493 | } | |
2494 | return ERR_PTR(-ENOENT); | |
2495 | } | |
2496 | ||
7dab8ee3 PNA |
2497 | static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info, |
2498 | const struct nlattr * const nla[]) | |
96518518 | 2499 | { |
7dab8ee3 | 2500 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
2501 | struct netlink_ext_ack *extack = info->extack; |
2502 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2503 | u8 family = info->nfmsg->nfgen_family; |
74cccc3d | 2504 | struct nft_chain *chain = NULL; |
7dab8ee3 | 2505 | struct net *net = info->net; |
36dd1bcc | 2506 | const struct nlattr *attr; |
96518518 | 2507 | struct nft_table *table; |
57de2a0c | 2508 | u8 policy = NF_ACCEPT; |
4035285f | 2509 | struct nft_ctx ctx; |
96518518 | 2510 | u64 handle = 0; |
c9626a2c | 2511 | u32 flags = 0; |
96518518 | 2512 | |
0854db2a | 2513 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 2514 | |
6001a930 PNA |
2515 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2516 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2517 | if (IS_ERR(table)) { |
2518 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2519 | return PTR_ERR(table); |
36dd1bcc | 2520 | } |
96518518 | 2521 | |
96518518 | 2522 | chain = NULL; |
36dd1bcc | 2523 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2524 | |
2525 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2526 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2527 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2528 | if (IS_ERR(chain)) { |
2529 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2530 | return PTR_ERR(chain); |
36dd1bcc PNA |
2531 | } |
2532 | attr = nla[NFTA_CHAIN_HANDLE]; | |
74cccc3d | 2533 | } else if (nla[NFTA_CHAIN_NAME]) { |
f102d66b | 2534 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2535 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2536 | if (PTR_ERR(chain) != -ENOENT) { |
2537 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2538 | return PTR_ERR(chain); |
36dd1bcc | 2539 | } |
96518518 PM |
2540 | chain = NULL; |
2541 | } | |
74cccc3d PNA |
2542 | } else if (!nla[NFTA_CHAIN_ID]) { |
2543 | return -EINVAL; | |
96518518 PM |
2544 | } |
2545 | ||
57de2a0c | 2546 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2547 | if (chain != NULL && |
36dd1bcc PNA |
2548 | !nft_is_base_chain(chain)) { |
2549 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2550 | return -EOPNOTSUPP; |
36dd1bcc | 2551 | } |
d6b6cb1d PNA |
2552 | |
2553 | if (chain == NULL && | |
36dd1bcc PNA |
2554 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2555 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2556 | return -EOPNOTSUPP; |
36dd1bcc | 2557 | } |
57de2a0c | 2558 | |
8f46df18 | 2559 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2560 | switch (policy) { |
2561 | case NF_DROP: | |
2562 | case NF_ACCEPT: | |
2563 | break; | |
2564 | default: | |
2565 | return -EINVAL; | |
2566 | } | |
2567 | } | |
2568 | ||
c9626a2c PNA |
2569 | if (nla[NFTA_CHAIN_FLAGS]) |
2570 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2571 | else if (chain) |
2572 | flags = chain->flags; | |
c9626a2c | 2573 | |
c1f79a2e PNA |
2574 | if (flags & ~NFT_CHAIN_FLAGS) |
2575 | return -EOPNOTSUPP; | |
2576 | ||
7dab8ee3 | 2577 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
4035285f | 2578 | |
96518518 | 2579 | if (chain != NULL) { |
5dc52d83 PNA |
2580 | if (chain->flags & NFT_CHAIN_BINDING) |
2581 | return -EINVAL; | |
2582 | ||
7dab8ee3 | 2583 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 2584 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2585 | return -EEXIST; |
36dd1bcc | 2586 | } |
7dab8ee3 | 2587 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
2588 | return -EOPNOTSUPP; |
2589 | ||
67c49de4 | 2590 | flags |= chain->flags & NFT_CHAIN_BASE; |
98a381a7 PNA |
2591 | return nf_tables_updchain(&ctx, genmask, policy, flags, attr, |
2592 | extack); | |
96518518 PM |
2593 | } |
2594 | ||
983c4fcb | 2595 | return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack); |
96518518 PM |
2596 | } |
2597 | ||
7dab8ee3 PNA |
2598 | static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info, |
2599 | const struct nlattr * const nla[]) | |
96518518 | 2600 | { |
7dab8ee3 PNA |
2601 | struct netlink_ext_ack *extack = info->extack; |
2602 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2603 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 2604 | struct net *net = info->net; |
36dd1bcc | 2605 | const struct nlattr *attr; |
96518518 PM |
2606 | struct nft_table *table; |
2607 | struct nft_chain *chain; | |
9dee1474 | 2608 | struct nft_rule *rule; |
91c7b38d | 2609 | struct nft_ctx ctx; |
3ecbfd65 | 2610 | u64 handle; |
9dee1474 PNA |
2611 | u32 use; |
2612 | int err; | |
96518518 | 2613 | |
6001a930 PNA |
2614 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2615 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2616 | if (IS_ERR(table)) { |
2617 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2618 | return PTR_ERR(table); |
36dd1bcc | 2619 | } |
96518518 | 2620 | |
3ecbfd65 | 2621 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2622 | attr = nla[NFTA_CHAIN_HANDLE]; |
2623 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2624 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2625 | } else { |
36dd1bcc | 2626 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2627 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2628 | } |
36dd1bcc PNA |
2629 | if (IS_ERR(chain)) { |
2630 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2631 | return PTR_ERR(chain); |
36dd1bcc | 2632 | } |
9dee1474 | 2633 | |
7dab8ee3 | 2634 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
9dee1474 | 2635 | chain->use > 0) |
96518518 PM |
2636 | return -EBUSY; |
2637 | ||
7dab8ee3 | 2638 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0165d932 | 2639 | |
9dee1474 PNA |
2640 | use = chain->use; |
2641 | list_for_each_entry(rule, &chain->rules, list) { | |
2642 | if (!nft_is_active_next(net, rule)) | |
2643 | continue; | |
2644 | use--; | |
2645 | ||
2646 | err = nft_delrule(&ctx, rule); | |
2647 | if (err < 0) | |
2648 | return err; | |
2649 | } | |
2650 | ||
2651 | /* There are rules and elements that are still holding references to us, | |
2652 | * we cannot do a recursive removal in this case. | |
2653 | */ | |
36dd1bcc PNA |
2654 | if (use > 0) { |
2655 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2656 | return -EBUSY; |
36dd1bcc | 2657 | } |
9dee1474 | 2658 | |
ee01d542 | 2659 | return nft_delchain(&ctx); |
96518518 PM |
2660 | } |
2661 | ||
96518518 PM |
2662 | /* |
2663 | * Expressions | |
2664 | */ | |
2665 | ||
2666 | /** | |
ef1f7df9 | 2667 | * nft_register_expr - register nf_tables expr type |
3db86c39 | 2668 | * @type: expr type |
96518518 | 2669 | * |
ef1f7df9 | 2670 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2671 | * success or a negative errno code otherwise. |
2672 | */ | |
ef1f7df9 | 2673 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2674 | { |
2675 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2676 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2677 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2678 | else |
e688a7f8 | 2679 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2680 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2681 | return 0; | |
2682 | } | |
2683 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2684 | ||
2685 | /** | |
ef1f7df9 | 2686 | * nft_unregister_expr - unregister nf_tables expr type |
3db86c39 | 2687 | * @type: expr type |
96518518 | 2688 | * |
ef1f7df9 | 2689 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2690 | */ |
ef1f7df9 | 2691 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2692 | { |
2693 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2694 | list_del_rcu(&type->list); |
96518518 PM |
2695 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2696 | } | |
2697 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2698 | ||
64d46806 PM |
2699 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2700 | struct nlattr *nla) | |
96518518 | 2701 | { |
9cff126f | 2702 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2703 | |
ef1f7df9 | 2704 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2705 | if (!nla_strcmp(nla, type->name)) { |
2706 | if (!type->family && !candidate) | |
2707 | candidate = type; | |
2708 | else if (type->family == family) | |
2709 | candidate = type; | |
2710 | } | |
96518518 | 2711 | } |
9cff126f | 2712 | return candidate; |
96518518 PM |
2713 | } |
2714 | ||
b9c04ae7 PNA |
2715 | #ifdef CONFIG_MODULES |
2716 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
2717 | struct nlattr *nla) | |
2718 | { | |
eb014de4 PNA |
2719 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
2720 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
2721 | return -EAGAIN; |
2722 | ||
2723 | return 0; | |
2724 | } | |
2725 | #endif | |
2726 | ||
452238e8 FW |
2727 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
2728 | u8 family, | |
64d46806 | 2729 | struct nlattr *nla) |
96518518 | 2730 | { |
ef1f7df9 | 2731 | const struct nft_expr_type *type; |
96518518 PM |
2732 | |
2733 | if (nla == NULL) | |
2734 | return ERR_PTR(-EINVAL); | |
2735 | ||
64d46806 | 2736 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
2737 | if (type != NULL && try_module_get(type->owner)) |
2738 | return type; | |
96518518 | 2739 | |
f102d66b | 2740 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 2741 | #ifdef CONFIG_MODULES |
ef1f7df9 | 2742 | if (type == NULL) { |
b9c04ae7 | 2743 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
2744 | return ERR_PTR(-EAGAIN); |
2745 | ||
eb014de4 PNA |
2746 | if (nft_request_module(net, "nft-expr-%.*s", |
2747 | nla_len(nla), | |
2748 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
2749 | return ERR_PTR(-EAGAIN); |
2750 | } | |
2751 | #endif | |
2752 | return ERR_PTR(-ENOENT); | |
2753 | } | |
2754 | ||
2755 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
2756 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
2757 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
2758 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
2759 | }; | |
2760 | ||
2761 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
8daa8fde | 2762 | const struct nft_expr *expr, bool reset) |
96518518 | 2763 | { |
ef1f7df9 | 2764 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
2765 | goto nla_put_failure; |
2766 | ||
2767 | if (expr->ops->dump) { | |
ae0be8de MK |
2768 | struct nlattr *data = nla_nest_start_noflag(skb, |
2769 | NFTA_EXPR_DATA); | |
96518518 PM |
2770 | if (data == NULL) |
2771 | goto nla_put_failure; | |
8daa8fde | 2772 | if (expr->ops->dump(skb, expr, reset) < 0) |
96518518 PM |
2773 | goto nla_put_failure; |
2774 | nla_nest_end(skb, data); | |
2775 | } | |
2776 | ||
2777 | return skb->len; | |
2778 | ||
2779 | nla_put_failure: | |
2780 | return -1; | |
2781 | }; | |
2782 | ||
0b2d8a7b | 2783 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
8daa8fde | 2784 | const struct nft_expr *expr, bool reset) |
0b2d8a7b PM |
2785 | { |
2786 | struct nlattr *nest; | |
2787 | ||
ae0be8de | 2788 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
2789 | if (!nest) |
2790 | goto nla_put_failure; | |
8daa8fde | 2791 | if (nf_tables_fill_expr_info(skb, expr, reset) < 0) |
0b2d8a7b PM |
2792 | goto nla_put_failure; |
2793 | nla_nest_end(skb, nest); | |
2794 | return 0; | |
2795 | ||
2796 | nla_put_failure: | |
2797 | return -1; | |
2798 | } | |
2799 | ||
96518518 PM |
2800 | struct nft_expr_info { |
2801 | const struct nft_expr_ops *ops; | |
83d9dcba | 2802 | const struct nlattr *attr; |
ef1f7df9 | 2803 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
2804 | }; |
2805 | ||
0ca743a5 PNA |
2806 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
2807 | const struct nlattr *nla, | |
96518518 PM |
2808 | struct nft_expr_info *info) |
2809 | { | |
ef1f7df9 | 2810 | const struct nft_expr_type *type; |
96518518 | 2811 | const struct nft_expr_ops *ops; |
ef1f7df9 | 2812 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
2813 | int err; |
2814 | ||
8cb08174 JB |
2815 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
2816 | nft_expr_policy, NULL); | |
96518518 PM |
2817 | if (err < 0) |
2818 | return err; | |
2819 | ||
452238e8 | 2820 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
2821 | if (IS_ERR(type)) |
2822 | return PTR_ERR(type); | |
2823 | ||
2824 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
2825 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
2826 | tb[NFTA_EXPR_DATA], | |
2827 | type->policy, NULL); | |
ef1f7df9 PM |
2828 | if (err < 0) |
2829 | goto err1; | |
2830 | } else | |
2831 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
2832 | ||
2833 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
2834 | ops = type->select_ops(ctx, |
2835 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
2836 | if (IS_ERR(ops)) { |
2837 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
2838 | #ifdef CONFIG_MODULES |
2839 | if (err == -EAGAIN) | |
eb014de4 PNA |
2840 | if (nft_expr_type_request_module(ctx->net, |
2841 | ctx->family, | |
2842 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
2843 | err = -ENOENT; | |
0ef1efd1 | 2844 | #endif |
ef1f7df9 PM |
2845 | goto err1; |
2846 | } | |
2847 | } else | |
2848 | ops = type->ops; | |
2849 | ||
83d9dcba | 2850 | info->attr = nla; |
96518518 | 2851 | info->ops = ops; |
83d9dcba | 2852 | |
96518518 | 2853 | return 0; |
ef1f7df9 PM |
2854 | |
2855 | err1: | |
2856 | module_put(type->owner); | |
2857 | return err; | |
96518518 PM |
2858 | } |
2859 | ||
3a07327d PNA |
2860 | int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla, |
2861 | struct nft_expr_info *info) | |
2862 | { | |
2863 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; | |
2864 | const struct nft_expr_type *type; | |
2865 | int err; | |
2866 | ||
2867 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, | |
2868 | nft_expr_policy, NULL); | |
2869 | if (err < 0) | |
2870 | return err; | |
2871 | ||
2872 | if (!tb[NFTA_EXPR_DATA]) | |
2873 | return -EINVAL; | |
2874 | ||
2875 | type = __nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]); | |
98cbc40e DC |
2876 | if (!type) |
2877 | return -ENOENT; | |
3a07327d PNA |
2878 | |
2879 | if (!type->inner_ops) | |
2880 | return -EOPNOTSUPP; | |
2881 | ||
2882 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, | |
2883 | tb[NFTA_EXPR_DATA], | |
2884 | type->policy, NULL); | |
2885 | if (err < 0) | |
2886 | goto err_nla_parse; | |
2887 | ||
2888 | info->attr = nla; | |
2889 | info->ops = type->inner_ops; | |
2890 | ||
2891 | return 0; | |
2892 | ||
2893 | err_nla_parse: | |
2894 | return err; | |
2895 | } | |
2896 | ||
96518518 | 2897 | static int nf_tables_newexpr(const struct nft_ctx *ctx, |
7dab8ee3 | 2898 | const struct nft_expr_info *expr_info, |
96518518 PM |
2899 | struct nft_expr *expr) |
2900 | { | |
7dab8ee3 | 2901 | const struct nft_expr_ops *ops = expr_info->ops; |
96518518 PM |
2902 | int err; |
2903 | ||
2904 | expr->ops = ops; | |
2905 | if (ops->init) { | |
7dab8ee3 | 2906 | err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb); |
96518518 PM |
2907 | if (err < 0) |
2908 | goto err1; | |
2909 | } | |
2910 | ||
96518518 | 2911 | return 0; |
96518518 PM |
2912 | err1: |
2913 | expr->ops = NULL; | |
2914 | return err; | |
2915 | } | |
2916 | ||
62472bce PM |
2917 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
2918 | struct nft_expr *expr) | |
96518518 | 2919 | { |
3f3a390d PNA |
2920 | const struct nft_expr_type *type = expr->ops->type; |
2921 | ||
96518518 | 2922 | if (expr->ops->destroy) |
62472bce | 2923 | expr->ops->destroy(ctx, expr); |
3f3a390d | 2924 | module_put(type->owner); |
96518518 PM |
2925 | } |
2926 | ||
795a6d6b PNA |
2927 | static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
2928 | const struct nlattr *nla) | |
0b2d8a7b | 2929 | { |
7dab8ee3 | 2930 | struct nft_expr_info expr_info; |
0b2d8a7b | 2931 | struct nft_expr *expr; |
b8e20400 | 2932 | struct module *owner; |
0b2d8a7b PM |
2933 | int err; |
2934 | ||
7dab8ee3 | 2935 | err = nf_tables_expr_parse(ctx, nla, &expr_info); |
0b2d8a7b | 2936 | if (err < 0) |
52077804 PNA |
2937 | goto err_expr_parse; |
2938 | ||
2939 | err = -EOPNOTSUPP; | |
2940 | if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL)) | |
2941 | goto err_expr_stateful; | |
0b2d8a7b PM |
2942 | |
2943 | err = -ENOMEM; | |
33758c89 | 2944 | expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT); |
0b2d8a7b | 2945 | if (expr == NULL) |
52077804 | 2946 | goto err_expr_stateful; |
0b2d8a7b | 2947 | |
7dab8ee3 | 2948 | err = nf_tables_newexpr(ctx, &expr_info, expr); |
0b2d8a7b | 2949 | if (err < 0) |
52077804 | 2950 | goto err_expr_new; |
0b2d8a7b PM |
2951 | |
2952 | return expr; | |
52077804 | 2953 | err_expr_new: |
6cafaf47 | 2954 | kfree(expr); |
52077804 | 2955 | err_expr_stateful: |
7dab8ee3 PNA |
2956 | owner = expr_info.ops->type->owner; |
2957 | if (expr_info.ops->type->release_ops) | |
2958 | expr_info.ops->type->release_ops(expr_info.ops); | |
b8e20400 PNA |
2959 | |
2960 | module_put(owner); | |
52077804 | 2961 | err_expr_parse: |
0b2d8a7b PM |
2962 | return ERR_PTR(err); |
2963 | } | |
2964 | ||
c604cc69 PNA |
2965 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
2966 | { | |
2967 | int err; | |
2968 | ||
2969 | if (src->ops->clone) { | |
2970 | dst->ops = src->ops; | |
2971 | err = src->ops->clone(dst, src); | |
2972 | if (err < 0) | |
2973 | return err; | |
2974 | } else { | |
2975 | memcpy(dst, src, src->ops->size); | |
2976 | } | |
2977 | ||
2978 | __module_get(src->ops->type->owner); | |
2979 | ||
2980 | return 0; | |
2981 | } | |
2982 | ||
0b2d8a7b PM |
2983 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
2984 | { | |
2985 | nf_tables_expr_destroy(ctx, expr); | |
2986 | kfree(expr); | |
2987 | } | |
2988 | ||
96518518 PM |
2989 | /* |
2990 | * Rules | |
2991 | */ | |
2992 | ||
cac20fcd PNA |
2993 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
2994 | u64 handle) | |
96518518 PM |
2995 | { |
2996 | struct nft_rule *rule; | |
2997 | ||
2998 | // FIXME: this sucks | |
d9adf22a | 2999 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
3000 | if (handle == rule->handle) |
3001 | return rule; | |
3002 | } | |
3003 | ||
3004 | return ERR_PTR(-ENOENT); | |
3005 | } | |
3006 | ||
cac20fcd PNA |
3007 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
3008 | const struct nlattr *nla) | |
96518518 PM |
3009 | { |
3010 | if (nla == NULL) | |
3011 | return ERR_PTR(-EINVAL); | |
3012 | ||
cac20fcd | 3013 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
3014 | } |
3015 | ||
3016 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
3017 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
3018 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
3019 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
3020 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
3021 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
3022 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 3023 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 3024 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
3025 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
3026 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 3027 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 3028 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
837830a4 | 3029 | [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
3030 | }; |
3031 | ||
84d7fce6 PNA |
3032 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
3033 | u32 portid, u32 seq, int event, | |
3034 | u32 flags, int family, | |
96518518 PM |
3035 | const struct nft_table *table, |
3036 | const struct nft_chain *chain, | |
8daa8fde PS |
3037 | const struct nft_rule *rule, u64 handle, |
3038 | bool reset) | |
96518518 PM |
3039 | { |
3040 | struct nlmsghdr *nlh; | |
96518518 PM |
3041 | const struct nft_expr *expr, *next; |
3042 | struct nlattr *list; | |
dedb67c4 | 3043 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 3044 | |
19c28b13 PNA |
3045 | nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0, |
3046 | nft_base_seq(net)); | |
3047 | if (!nlh) | |
96518518 PM |
3048 | goto nla_put_failure; |
3049 | ||
96518518 PM |
3050 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
3051 | goto nla_put_failure; | |
3052 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
3053 | goto nla_put_failure; | |
b46f6ded ND |
3054 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
3055 | NFTA_RULE_PAD)) | |
96518518 PM |
3056 | goto nla_put_failure; |
3057 | ||
e189ae16 PNA |
3058 | if (event != NFT_MSG_DELRULE && handle) { |
3059 | if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle), | |
b46f6ded | 3060 | NFTA_RULE_PAD)) |
5e948466 EL |
3061 | goto nla_put_failure; |
3062 | } | |
3063 | ||
b72920f6 PNA |
3064 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) |
3065 | nft_flow_rule_stats(chain, rule); | |
3066 | ||
ae0be8de | 3067 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
3068 | if (list == NULL) |
3069 | goto nla_put_failure; | |
3070 | nft_rule_for_each_expr(expr, next, rule) { | |
8daa8fde | 3071 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0) |
96518518 | 3072 | goto nla_put_failure; |
96518518 PM |
3073 | } |
3074 | nla_nest_end(skb, list); | |
3075 | ||
86f1ec32 PM |
3076 | if (rule->udata) { |
3077 | struct nft_userdata *udata = nft_userdata(rule); | |
3078 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
3079 | udata->data) < 0) | |
3080 | goto nla_put_failure; | |
3081 | } | |
0768b3b3 | 3082 | |
053c095a JB |
3083 | nlmsg_end(skb, nlh); |
3084 | return 0; | |
96518518 PM |
3085 | |
3086 | nla_put_failure: | |
3087 | nlmsg_trim(skb, nlh); | |
3088 | return -1; | |
3089 | } | |
3090 | ||
25e94a99 PNA |
3091 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
3092 | const struct nft_rule *rule, int event) | |
96518518 | 3093 | { |
d59d2f82 | 3094 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
e189ae16 | 3095 | const struct nft_rule *prule; |
96518518 | 3096 | struct sk_buff *skb; |
e189ae16 PNA |
3097 | u64 handle = 0; |
3098 | u16 flags = 0; | |
96518518 PM |
3099 | int err; |
3100 | ||
128ad332 PNA |
3101 | if (!ctx->report && |
3102 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3103 | return; |
96518518 | 3104 | |
96518518 PM |
3105 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
3106 | if (skb == NULL) | |
3107 | goto err; | |
3108 | ||
e189ae16 PNA |
3109 | if (event == NFT_MSG_NEWRULE && |
3110 | !list_is_first(&rule->list, &ctx->chain->rules) && | |
3111 | !list_is_last(&rule->list, &ctx->chain->rules)) { | |
3112 | prule = list_prev_entry(rule, list); | |
3113 | handle = prule->handle; | |
3114 | } | |
3115 | if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE)) | |
3116 | flags |= NLM_F_APPEND; | |
6fb721cf PNA |
3117 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
3118 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
e189ae16 | 3119 | |
84d7fce6 | 3120 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
e189ae16 | 3121 | event, flags, ctx->family, ctx->table, |
8daa8fde | 3122 | ctx->chain, rule, handle, false); |
96518518 PM |
3123 | if (err < 0) { |
3124 | kfree_skb(skb); | |
3125 | goto err; | |
3126 | } | |
3127 | ||
0854db2a | 3128 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 3129 | return; |
96518518 | 3130 | err: |
25e94a99 | 3131 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
3132 | } |
3133 | ||
6e1f760e | 3134 | struct nft_rule_dump_ctx { |
e46abbcc | 3135 | char *table; |
b7263e07 | 3136 | char *chain; |
6e1f760e PNA |
3137 | }; |
3138 | ||
241faece PS |
3139 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
3140 | unsigned int *idx, | |
3141 | struct netlink_callback *cb, | |
3142 | const struct nft_table *table, | |
8daa8fde PS |
3143 | const struct nft_chain *chain, |
3144 | bool reset) | |
241faece PS |
3145 | { |
3146 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 3147 | const struct nft_rule *rule, *prule; |
241faece | 3148 | unsigned int s_idx = cb->args[0]; |
e189ae16 | 3149 | u64 handle; |
241faece | 3150 | |
2c82c7e7 | 3151 | prule = NULL; |
241faece PS |
3152 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
3153 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 3154 | goto cont_skip; |
241faece PS |
3155 | if (*idx < s_idx) |
3156 | goto cont; | |
3157 | if (*idx > s_idx) { | |
3158 | memset(&cb->args[1], 0, | |
3159 | sizeof(cb->args) - sizeof(cb->args[0])); | |
3160 | } | |
e189ae16 PNA |
3161 | if (prule) |
3162 | handle = prule->handle; | |
3163 | else | |
3164 | handle = 0; | |
3165 | ||
241faece PS |
3166 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
3167 | cb->nlh->nlmsg_seq, | |
3168 | NFT_MSG_NEWRULE, | |
3169 | NLM_F_MULTI | NLM_F_APPEND, | |
3170 | table->family, | |
8daa8fde | 3171 | table, chain, rule, handle, reset) < 0) |
310529e6 | 3172 | return 1; |
241faece PS |
3173 | |
3174 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
3175 | cont: | |
2c82c7e7 FW |
3176 | prule = rule; |
3177 | cont_skip: | |
241faece PS |
3178 | (*idx)++; |
3179 | } | |
310529e6 | 3180 | return 0; |
241faece PS |
3181 | } |
3182 | ||
96518518 PM |
3183 | static int nf_tables_dump_rules(struct sk_buff *skb, |
3184 | struct netlink_callback *cb) | |
3185 | { | |
3186 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 3187 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 3188 | struct nft_table *table; |
96518518 | 3189 | const struct nft_chain *chain; |
241faece | 3190 | unsigned int idx = 0; |
99633ab2 | 3191 | struct net *net = sock_net(skb->sk); |
96518518 | 3192 | int family = nfmsg->nfgen_family; |
0854db2a | 3193 | struct nftables_pernet *nft_net; |
8daa8fde PS |
3194 | bool reset = false; |
3195 | ||
3196 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET) | |
3197 | reset = true; | |
96518518 | 3198 | |
e688a7f8 | 3199 | rcu_read_lock(); |
d59d2f82 | 3200 | nft_net = nft_pernet(net); |
34002783 | 3201 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 3202 | |
0854db2a | 3203 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 3204 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
3205 | continue; |
3206 | ||
3207 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
3208 | continue; |
3209 | ||
715849ab | 3210 | if (ctx && ctx->table && ctx->chain) { |
241faece | 3211 | struct rhlist_head *list, *tmp; |
6e1f760e | 3212 | |
241faece PS |
3213 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
3214 | nft_chain_ht_params); | |
3215 | if (!list) | |
3216 | goto done; | |
3217 | ||
3218 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
3219 | if (!nft_is_active(net, chain)) | |
3220 | continue; | |
3221 | __nf_tables_dump_rules(skb, &idx, | |
8daa8fde | 3222 | cb, table, chain, reset); |
241faece | 3223 | break; |
96518518 | 3224 | } |
241faece | 3225 | goto done; |
96518518 | 3226 | } |
241faece PS |
3227 | |
3228 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
8daa8fde PS |
3229 | if (__nf_tables_dump_rules(skb, &idx, |
3230 | cb, table, chain, reset)) | |
241faece PS |
3231 | goto done; |
3232 | } | |
3233 | ||
3234 | if (ctx && ctx->table) | |
3235 | break; | |
96518518 PM |
3236 | } |
3237 | done: | |
e688a7f8 | 3238 | rcu_read_unlock(); |
310529e6 PS |
3239 | |
3240 | cb->args[0] = idx; | |
96518518 PM |
3241 | return skb->len; |
3242 | } | |
3243 | ||
90fd131a FW |
3244 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
3245 | { | |
3246 | const struct nlattr * const *nla = cb->data; | |
3247 | struct nft_rule_dump_ctx *ctx = NULL; | |
3248 | ||
3249 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
3250 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
3251 | if (!ctx) | |
3252 | return -ENOMEM; | |
3253 | ||
3254 | if (nla[NFTA_RULE_TABLE]) { | |
3255 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
3256 | GFP_ATOMIC); | |
3257 | if (!ctx->table) { | |
3258 | kfree(ctx); | |
3259 | return -ENOMEM; | |
3260 | } | |
3261 | } | |
3262 | if (nla[NFTA_RULE_CHAIN]) { | |
3263 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
3264 | GFP_ATOMIC); | |
3265 | if (!ctx->chain) { | |
3266 | kfree(ctx->table); | |
3267 | kfree(ctx); | |
3268 | return -ENOMEM; | |
3269 | } | |
3270 | } | |
3271 | } | |
3272 | ||
3273 | cb->data = ctx; | |
3274 | return 0; | |
3275 | } | |
3276 | ||
6e1f760e PNA |
3277 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
3278 | { | |
e46abbcc PS |
3279 | struct nft_rule_dump_ctx *ctx = cb->data; |
3280 | ||
3281 | if (ctx) { | |
3282 | kfree(ctx->table); | |
b7263e07 | 3283 | kfree(ctx->chain); |
e46abbcc PS |
3284 | kfree(ctx); |
3285 | } | |
6e1f760e PNA |
3286 | return 0; |
3287 | } | |
3288 | ||
d9adf22a | 3289 | /* called with rcu_read_lock held */ |
797d4980 PNA |
3290 | static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info, |
3291 | const struct nlattr * const nla[]) | |
96518518 | 3292 | { |
797d4980 PNA |
3293 | struct netlink_ext_ack *extack = info->extack; |
3294 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 3295 | u8 family = info->nfmsg->nfgen_family; |
96518518 PM |
3296 | const struct nft_chain *chain; |
3297 | const struct nft_rule *rule; | |
797d4980 | 3298 | struct net *net = info->net; |
1b2470e5 | 3299 | struct nft_table *table; |
96518518 | 3300 | struct sk_buff *skb2; |
8daa8fde | 3301 | bool reset = false; |
96518518 PM |
3302 | int err; |
3303 | ||
797d4980 | 3304 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 | 3305 | struct netlink_dump_control c = { |
90fd131a | 3306 | .start= nf_tables_dump_rules_start, |
96518518 | 3307 | .dump = nf_tables_dump_rules, |
6e1f760e | 3308 | .done = nf_tables_dump_rules_done, |
d9adf22a | 3309 | .module = THIS_MODULE, |
90fd131a | 3310 | .data = (void *)nla, |
96518518 | 3311 | }; |
6e1f760e | 3312 | |
797d4980 | 3313 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
3314 | } |
3315 | ||
6001a930 | 3316 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0); |
36dd1bcc PNA |
3317 | if (IS_ERR(table)) { |
3318 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3319 | return PTR_ERR(table); |
36dd1bcc | 3320 | } |
96518518 | 3321 | |
f102d66b | 3322 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3323 | if (IS_ERR(chain)) { |
3324 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3325 | return PTR_ERR(chain); |
36dd1bcc | 3326 | } |
96518518 | 3327 | |
cac20fcd | 3328 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3329 | if (IS_ERR(rule)) { |
3330 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3331 | return PTR_ERR(rule); |
36dd1bcc | 3332 | } |
96518518 | 3333 | |
d9adf22a | 3334 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
3335 | if (!skb2) |
3336 | return -ENOMEM; | |
3337 | ||
8daa8fde PS |
3338 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET) |
3339 | reset = true; | |
3340 | ||
84d7fce6 | 3341 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 3342 | info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
8daa8fde | 3343 | family, table, chain, rule, 0, reset); |
96518518 | 3344 | if (err < 0) |
ee921183 | 3345 | goto err_fill_rule_info; |
96518518 | 3346 | |
ee921183 | 3347 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 3348 | |
ee921183 | 3349 | err_fill_rule_info: |
96518518 PM |
3350 | kfree_skb(skb2); |
3351 | return err; | |
3352 | } | |
3353 | ||
62472bce PM |
3354 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
3355 | struct nft_rule *rule) | |
96518518 | 3356 | { |
29e38801 | 3357 | struct nft_expr *expr, *next; |
96518518 PM |
3358 | |
3359 | /* | |
3360 | * Careful: some expressions might not be initialized in case this | |
3361 | * is called on error from nf_tables_newrule(). | |
3362 | */ | |
3363 | expr = nft_expr_first(rule); | |
31cc578a | 3364 | while (nft_expr_more(rule, expr)) { |
29e38801 | 3365 | next = nft_expr_next(expr); |
62472bce | 3366 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 3367 | expr = next; |
96518518 PM |
3368 | } |
3369 | kfree(rule); | |
3370 | } | |
3371 | ||
d0e2c7de | 3372 | void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae | 3373 | { |
f6ac8585 | 3374 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
3375 | nf_tables_rule_destroy(ctx, rule); |
3376 | } | |
3377 | ||
a654de8f PNA |
3378 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
3379 | { | |
3380 | struct nft_expr *expr, *last; | |
3381 | const struct nft_data *data; | |
3382 | struct nft_rule *rule; | |
3383 | int err; | |
3384 | ||
26b2f552 TY |
3385 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
3386 | return -EMLINK; | |
3387 | ||
a654de8f PNA |
3388 | list_for_each_entry(rule, &chain->rules, list) { |
3389 | if (!nft_is_active_next(ctx->net, rule)) | |
3390 | continue; | |
3391 | ||
3392 | nft_rule_for_each_expr(expr, last, rule) { | |
3393 | if (!expr->ops->validate) | |
3394 | continue; | |
3395 | ||
3396 | err = expr->ops->validate(ctx, expr, &data); | |
3397 | if (err < 0) | |
3398 | return err; | |
3399 | } | |
81ea0106 FW |
3400 | |
3401 | cond_resched(); | |
a654de8f PNA |
3402 | } |
3403 | ||
3404 | return 0; | |
3405 | } | |
3406 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
3407 | ||
3408 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
3409 | { | |
3410 | struct nft_chain *chain; | |
3411 | struct nft_ctx ctx = { | |
3412 | .net = net, | |
3413 | .family = table->family, | |
3414 | }; | |
3415 | int err; | |
3416 | ||
3417 | list_for_each_entry(chain, &table->chains, list) { | |
3418 | if (!nft_is_base_chain(chain)) | |
3419 | continue; | |
3420 | ||
3421 | ctx.chain = chain; | |
3422 | err = nft_chain_validate(&ctx, chain); | |
3423 | if (err < 0) | |
3424 | return err; | |
3425 | } | |
3426 | ||
3427 | return 0; | |
3428 | } | |
3429 | ||
75dd48e2 | 3430 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3431 | const struct nft_chain *chain, |
75dd48e2 PS |
3432 | const struct nlattr *nla); |
3433 | ||
1081d11b PNA |
3434 | #define NFT_RULE_MAXEXPRS 128 |
3435 | ||
7dab8ee3 PNA |
3436 | static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, |
3437 | const struct nlattr * const nla[]) | |
96518518 | 3438 | { |
7dab8ee3 | 3439 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
3440 | struct netlink_ext_ack *extack = info->extack; |
3441 | unsigned int size, i, n, ulen = 0, usize = 0; | |
3442 | u8 genmask = nft_genmask_next(info->net); | |
3443 | struct nft_rule *rule, *old_rule = NULL; | |
3444 | struct nft_expr_info *expr_info = NULL; | |
ef4b65e5 | 3445 | u8 family = info->nfmsg->nfgen_family; |
3c5e4462 | 3446 | struct nft_flow_rule *flow = NULL; |
7dab8ee3 | 3447 | struct net *net = info->net; |
7dab8ee3 | 3448 | struct nft_userdata *udata; |
96518518 PM |
3449 | struct nft_table *table; |
3450 | struct nft_chain *chain; | |
7dab8ee3 PNA |
3451 | struct nft_trans *trans; |
3452 | u64 handle, pos_handle; | |
96518518 PM |
3453 | struct nft_expr *expr; |
3454 | struct nft_ctx ctx; | |
3455 | struct nlattr *tmp; | |
96518518 | 3456 | int err, rem; |
96518518 | 3457 | |
0854db2a | 3458 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3459 | |
6001a930 PNA |
3460 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3461 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3462 | if (IS_ERR(table)) { |
3463 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3464 | return PTR_ERR(table); |
36dd1bcc | 3465 | } |
96518518 | 3466 | |
837830a4 PNA |
3467 | if (nla[NFTA_RULE_CHAIN]) { |
3468 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], | |
3469 | genmask); | |
3470 | if (IS_ERR(chain)) { | |
3471 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
3472 | return PTR_ERR(chain); | |
3473 | } | |
d0e2c7de PNA |
3474 | if (nft_chain_is_bound(chain)) |
3475 | return -EOPNOTSUPP; | |
3476 | ||
837830a4 | 3477 | } else if (nla[NFTA_RULE_CHAIN_ID]) { |
95f466d2 | 3478 | chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID]); |
837830a4 PNA |
3479 | if (IS_ERR(chain)) { |
3480 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]); | |
3481 | return PTR_ERR(chain); | |
3482 | } | |
3483 | } else { | |
3484 | return -EINVAL; | |
36dd1bcc | 3485 | } |
96518518 PM |
3486 | |
3487 | if (nla[NFTA_RULE_HANDLE]) { | |
3488 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3489 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3490 | if (IS_ERR(rule)) { |
3491 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3492 | return PTR_ERR(rule); |
36dd1bcc | 3493 | } |
96518518 | 3494 | |
7dab8ee3 | 3495 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 3496 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
96518518 | 3497 | return -EEXIST; |
36dd1bcc | 3498 | } |
7dab8ee3 | 3499 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
3500 | old_rule = rule; |
3501 | else | |
3502 | return -EOPNOTSUPP; | |
3503 | } else { | |
7dab8ee3 PNA |
3504 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) || |
3505 | info->nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3506 | return -EINVAL; |
3507 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
3508 | |
3509 | if (chain->use == UINT_MAX) | |
3510 | return -EOVERFLOW; | |
5e948466 | 3511 | |
447750f2 FW |
3512 | if (nla[NFTA_RULE_POSITION]) { |
3513 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3514 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3515 | if (IS_ERR(old_rule)) { | |
3516 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3517 | return PTR_ERR(old_rule); | |
3518 | } | |
75dd48e2 | 3519 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
36d5b291 | 3520 | old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]); |
75dd48e2 PS |
3521 | if (IS_ERR(old_rule)) { |
3522 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3523 | return PTR_ERR(old_rule); | |
3524 | } | |
36dd1bcc | 3525 | } |
5e948466 EL |
3526 | } |
3527 | ||
7dab8ee3 | 3528 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0ca743a5 | 3529 | |
96518518 PM |
3530 | n = 0; |
3531 | size = 0; | |
3532 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
7dab8ee3 PNA |
3533 | expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3534 | sizeof(struct nft_expr_info), | |
3535 | GFP_KERNEL); | |
3536 | if (!expr_info) | |
2a43ecf9 FW |
3537 | return -ENOMEM; |
3538 | ||
96518518 PM |
3539 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3540 | err = -EINVAL; | |
3541 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3c5e4462 | 3542 | goto err_release_expr; |
96518518 | 3543 | if (n == NFT_RULE_MAXEXPRS) |
3c5e4462 | 3544 | goto err_release_expr; |
7dab8ee3 | 3545 | err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]); |
c781471d PNA |
3546 | if (err < 0) { |
3547 | NL_SET_BAD_ATTR(extack, tmp); | |
3c5e4462 | 3548 | goto err_release_expr; |
c781471d | 3549 | } |
7dab8ee3 | 3550 | size += expr_info[n].ops->size; |
96518518 PM |
3551 | n++; |
3552 | } | |
3553 | } | |
9889840f PM |
3554 | /* Check for overflow of dlen field */ |
3555 | err = -EFBIG; | |
3556 | if (size >= 1 << 12) | |
3c5e4462 | 3557 | goto err_release_expr; |
96518518 | 3558 | |
86f1ec32 | 3559 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3560 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3561 | if (ulen > 0) |
3562 | usize = sizeof(struct nft_userdata) + ulen; | |
3563 | } | |
0768b3b3 | 3564 | |
96518518 | 3565 | err = -ENOMEM; |
33758c89 | 3566 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT); |
96518518 | 3567 | if (rule == NULL) |
3c5e4462 | 3568 | goto err_release_expr; |
96518518 | 3569 | |
889f7ee7 | 3570 | nft_activate_next(net, rule); |
0628b123 | 3571 | |
96518518 PM |
3572 | rule->handle = handle; |
3573 | rule->dlen = size; | |
86f1ec32 | 3574 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3575 | |
86f1ec32 PM |
3576 | if (ulen) { |
3577 | udata = nft_userdata(rule); | |
3578 | udata->len = ulen - 1; | |
3579 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3580 | } | |
96518518 | 3581 | |
96518518 PM |
3582 | expr = nft_expr_first(rule); |
3583 | for (i = 0; i < n; i++) { | |
7dab8ee3 | 3584 | err = nf_tables_newexpr(&ctx, &expr_info[i], expr); |
83d9dcba | 3585 | if (err < 0) { |
7dab8ee3 | 3586 | NL_SET_BAD_ATTR(extack, expr_info[i].attr); |
3c5e4462 | 3587 | goto err_release_rule; |
83d9dcba | 3588 | } |
a654de8f | 3589 | |
7dab8ee3 | 3590 | if (expr_info[i].ops->validate) |
a654de8f PNA |
3591 | nft_validate_state_update(net, NFT_VALIDATE_NEED); |
3592 | ||
7dab8ee3 | 3593 | expr_info[i].ops = NULL; |
96518518 PM |
3594 | expr = nft_expr_next(expr); |
3595 | } | |
3596 | ||
3c5e4462 PNA |
3597 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
3598 | flow = nft_flow_rule_create(net, rule); | |
3599 | if (IS_ERR(flow)) { | |
3600 | err = PTR_ERR(flow); | |
3601 | goto err_release_rule; | |
3602 | } | |
3603 | } | |
3604 | ||
7dab8ee3 | 3605 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) { |
2c964c55 PNA |
3606 | err = nft_delrule(&ctx, old_rule); |
3607 | if (err < 0) | |
3608 | goto err_destroy_flow_rule; | |
3609 | ||
ca089878 | 3610 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3611 | if (trans == NULL) { |
3612 | err = -ENOMEM; | |
3c5e4462 | 3613 | goto err_destroy_flow_rule; |
569ccae6 | 3614 | } |
569ccae6 FW |
3615 | list_add_tail_rcu(&rule->list, &old_rule->list); |
3616 | } else { | |
c9626a2c PNA |
3617 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3618 | if (!trans) { | |
569ccae6 | 3619 | err = -ENOMEM; |
3c5e4462 | 3620 | goto err_destroy_flow_rule; |
569ccae6 FW |
3621 | } |
3622 | ||
7dab8ee3 | 3623 | if (info->nlh->nlmsg_flags & NLM_F_APPEND) { |
569ccae6 FW |
3624 | if (old_rule) |
3625 | list_add_rcu(&rule->list, &old_rule->list); | |
3626 | else | |
3627 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3628 | } else { | |
3629 | if (old_rule) | |
3630 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3631 | else | |
3632 | list_add_rcu(&rule->list, &chain->rules); | |
3633 | } | |
0628b123 | 3634 | } |
7dab8ee3 | 3635 | kvfree(expr_info); |
4fefee57 | 3636 | chain->use++; |
96518518 | 3637 | |
3c5e4462 PNA |
3638 | if (flow) |
3639 | nft_trans_flow_rule(trans) = flow; | |
3640 | ||
0854db2a | 3641 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
a654de8f PNA |
3642 | return nft_table_validate(net, table); |
3643 | ||
3644 | return 0; | |
3c5e4462 PNA |
3645 | |
3646 | err_destroy_flow_rule: | |
4ca041f9 CIK |
3647 | if (flow) |
3648 | nft_flow_rule_destroy(flow); | |
3c5e4462 | 3649 | err_release_rule: |
bb7b40ae | 3650 | nf_tables_rule_release(&ctx, rule); |
3c5e4462 | 3651 | err_release_expr: |
96518518 | 3652 | for (i = 0; i < n; i++) { |
7dab8ee3 PNA |
3653 | if (expr_info[i].ops) { |
3654 | module_put(expr_info[i].ops->type->owner); | |
3655 | if (expr_info[i].ops->type->release_ops) | |
3656 | expr_info[i].ops->type->release_ops(expr_info[i].ops); | |
b25a31bf | 3657 | } |
96518518 | 3658 | } |
7dab8ee3 PNA |
3659 | kvfree(expr_info); |
3660 | ||
96518518 PM |
3661 | return err; |
3662 | } | |
3663 | ||
1a94e38d | 3664 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3665 | const struct nft_chain *chain, |
1a94e38d PNA |
3666 | const struct nlattr *nla) |
3667 | { | |
d59d2f82 | 3668 | struct nftables_pernet *nft_net = nft_pernet(net); |
1a94e38d PNA |
3669 | u32 id = ntohl(nla_get_be32(nla)); |
3670 | struct nft_trans *trans; | |
3671 | ||
0854db2a | 3672 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
1a94e38d PNA |
3673 | struct nft_rule *rule = nft_trans_rule(trans); |
3674 | ||
3675 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
36d5b291 | 3676 | trans->ctx.chain == chain && |
1a94e38d PNA |
3677 | id == nft_trans_rule_id(trans)) |
3678 | return rule; | |
3679 | } | |
3680 | return ERR_PTR(-ENOENT); | |
3681 | } | |
3682 | ||
7dab8ee3 PNA |
3683 | static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info, |
3684 | const struct nlattr * const nla[]) | |
96518518 | 3685 | { |
7dab8ee3 | 3686 | struct netlink_ext_ack *extack = info->extack; |
7dab8ee3 | 3687 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 3688 | u8 family = info->nfmsg->nfgen_family; |
cf9dc09d | 3689 | struct nft_chain *chain = NULL; |
7dab8ee3 PNA |
3690 | struct net *net = info->net; |
3691 | struct nft_table *table; | |
cf9dc09d | 3692 | struct nft_rule *rule; |
0628b123 | 3693 | struct nft_ctx ctx; |
ef4b65e5 | 3694 | int err = 0; |
96518518 | 3695 | |
6001a930 PNA |
3696 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3697 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3698 | if (IS_ERR(table)) { |
3699 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3700 | return PTR_ERR(table); |
36dd1bcc | 3701 | } |
96518518 | 3702 | |
cf9dc09d | 3703 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
3704 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
3705 | genmask); | |
36dd1bcc PNA |
3706 | if (IS_ERR(chain)) { |
3707 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
cf9dc09d | 3708 | return PTR_ERR(chain); |
36dd1bcc | 3709 | } |
d0e2c7de PNA |
3710 | if (nft_chain_is_bound(chain)) |
3711 | return -EOPNOTSUPP; | |
cf9dc09d | 3712 | } |
96518518 | 3713 | |
7dab8ee3 | 3714 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0628b123 | 3715 | |
cf9dc09d PNA |
3716 | if (chain) { |
3717 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 3718 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3719 | if (IS_ERR(rule)) { |
3720 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
cf9dc09d | 3721 | return PTR_ERR(rule); |
36dd1bcc | 3722 | } |
96518518 | 3723 | |
1a94e38d PNA |
3724 | err = nft_delrule(&ctx, rule); |
3725 | } else if (nla[NFTA_RULE_ID]) { | |
36d5b291 | 3726 | rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]); |
36dd1bcc PNA |
3727 | if (IS_ERR(rule)) { |
3728 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 3729 | return PTR_ERR(rule); |
36dd1bcc | 3730 | } |
1a94e38d | 3731 | |
5e266fe7 | 3732 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 3733 | } else { |
ce24b721 | 3734 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
3735 | } |
3736 | } else { | |
3737 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
3738 | if (!nft_is_active_next(net, chain)) |
3739 | continue; | |
3740 | ||
cf9dc09d | 3741 | ctx.chain = chain; |
ce24b721 | 3742 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
3743 | if (err < 0) |
3744 | break; | |
3745 | } | |
3746 | } | |
3747 | ||
3748 | return err; | |
3749 | } | |
3750 | ||
20a69341 PM |
3751 | /* |
3752 | * Sets | |
3753 | */ | |
e32a4dc6 FW |
3754 | static const struct nft_set_type *nft_set_types[] = { |
3755 | &nft_set_hash_fast_type, | |
3756 | &nft_set_hash_type, | |
3757 | &nft_set_rhash_type, | |
3758 | &nft_set_bitmap_type, | |
3759 | &nft_set_rbtree_type, | |
e6abef61 | 3760 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
7400b063 SB |
3761 | &nft_set_pipapo_avx2_type, |
3762 | #endif | |
e32a4dc6 FW |
3763 | &nft_set_pipapo_type, |
3764 | }; | |
20a69341 | 3765 | |
2b664957 | 3766 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
3767 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
3768 | NFT_SET_EVAL) | |
2b664957 | 3769 | |
71cc0873 | 3770 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 3771 | { |
71cc0873 | 3772 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
3773 | } |
3774 | ||
c50b960c PM |
3775 | /* |
3776 | * Select a set implementation based on the data characteristics and the | |
3777 | * given policy. The total memory use might not be known if no size is | |
3778 | * given, in that case the amount of memory per element is used. | |
3779 | */ | |
3780 | static const struct nft_set_ops * | |
2b664957 PNA |
3781 | nft_select_set_ops(const struct nft_ctx *ctx, |
3782 | const struct nlattr * const nla[], | |
c50b960c PM |
3783 | const struct nft_set_desc *desc, |
3784 | enum nft_set_policies policy) | |
20a69341 | 3785 | { |
d59d2f82 | 3786 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c50b960c PM |
3787 | const struct nft_set_ops *ops, *bops; |
3788 | struct nft_set_estimate est, best; | |
2b664957 PNA |
3789 | const struct nft_set_type *type; |
3790 | u32 flags = 0; | |
e32a4dc6 | 3791 | int i; |
20a69341 | 3792 | |
0854db2a | 3793 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3794 | lockdep_nfnl_nft_mutex_not_held(); |
e32a4dc6 | 3795 | |
2b664957 PNA |
3796 | if (nla[NFTA_SET_FLAGS] != NULL) |
3797 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 3798 | |
55af753c PNA |
3799 | bops = NULL; |
3800 | best.size = ~0; | |
3801 | best.lookup = ~0; | |
0b5a7874 | 3802 | best.space = ~0; |
c50b960c | 3803 | |
e32a4dc6 FW |
3804 | for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) { |
3805 | type = nft_set_types[i]; | |
71cc0873 | 3806 | ops = &type->ops; |
2b664957 | 3807 | |
71cc0873 | 3808 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 3809 | continue; |
2b664957 | 3810 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
3811 | continue; |
3812 | ||
3813 | switch (policy) { | |
3814 | case NFT_SET_POL_PERFORMANCE: | |
55af753c | 3815 | if (est.lookup < best.lookup) |
c50b960c | 3816 | break; |
644e334e PNA |
3817 | if (est.lookup == best.lookup && |
3818 | est.space < best.space) | |
3819 | break; | |
c50b960c PM |
3820 | continue; |
3821 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
3822 | if (!desc->size) { |
3823 | if (est.space < best.space) | |
3824 | break; | |
3825 | if (est.space == best.space && | |
3826 | est.lookup < best.lookup) | |
3827 | break; | |
4f2921ca | 3828 | } else if (est.size < best.size || !bops) { |
c50b960c | 3829 | break; |
0b5a7874 | 3830 | } |
c50b960c PM |
3831 | continue; |
3832 | default: | |
3833 | break; | |
3834 | } | |
3835 | ||
c50b960c PM |
3836 | bops = ops; |
3837 | best = est; | |
20a69341 PM |
3838 | } |
3839 | ||
c50b960c PM |
3840 | if (bops != NULL) |
3841 | return bops; | |
3842 | ||
20a69341 PM |
3843 | return ERR_PTR(-EOPNOTSUPP); |
3844 | } | |
3845 | ||
3846 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
3847 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
3848 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 3849 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 3850 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
3851 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
3852 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
3853 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
3854 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
3855 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
3856 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
3857 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 3858 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
3859 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
3860 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
3861 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
3862 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 3863 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 3864 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
65038428 | 3865 | [NFTA_SET_EXPR] = { .type = NLA_NESTED }, |
48b0ae04 | 3866 | [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED }, |
c50b960c PM |
3867 | }; |
3868 | ||
3869 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
3870 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 3871 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
3872 | }; |
3873 | ||
cac20fcd PNA |
3874 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
3875 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
3876 | { |
3877 | struct nft_set *set; | |
3878 | ||
3879 | if (nla == NULL) | |
3880 | return ERR_PTR(-EINVAL); | |
3881 | ||
d9adf22a | 3882 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
3883 | if (!nla_strcmp(nla, set->name) && |
3884 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
3885 | return set; |
3886 | } | |
3887 | return ERR_PTR(-ENOENT); | |
3888 | } | |
3889 | ||
cac20fcd PNA |
3890 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
3891 | const struct nlattr *nla, | |
3892 | u8 genmask) | |
3ecbfd65 HS |
3893 | { |
3894 | struct nft_set *set; | |
3895 | ||
3ecbfd65 HS |
3896 | list_for_each_entry(set, &table->sets, list) { |
3897 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
3898 | nft_active_genmask(set, genmask)) | |
3899 | return set; | |
3900 | } | |
3901 | return ERR_PTR(-ENOENT); | |
3902 | } | |
3903 | ||
cac20fcd | 3904 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
470ee20e | 3905 | const struct nft_table *table, |
cac20fcd | 3906 | const struct nlattr *nla, u8 genmask) |
958bee14 | 3907 | { |
d59d2f82 | 3908 | struct nftables_pernet *nft_net = nft_pernet(net); |
958bee14 | 3909 | u32 id = ntohl(nla_get_be32(nla)); |
d59d2f82 | 3910 | struct nft_trans *trans; |
958bee14 | 3911 | |
0854db2a | 3912 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
9c7f96fd AK |
3913 | if (trans->msg_type == NFT_MSG_NEWSET) { |
3914 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 3915 | |
9c7f96fd | 3916 | if (id == nft_trans_set_id(trans) && |
470ee20e | 3917 | set->table == table && |
9c7f96fd AK |
3918 | nft_active_genmask(set, genmask)) |
3919 | return set; | |
3920 | } | |
958bee14 PNA |
3921 | } |
3922 | return ERR_PTR(-ENOENT); | |
3923 | } | |
c7a72e3f | 3924 | |
10659cba PNA |
3925 | struct nft_set *nft_set_lookup_global(const struct net *net, |
3926 | const struct nft_table *table, | |
3927 | const struct nlattr *nla_set_name, | |
3928 | const struct nlattr *nla_set_id, | |
3929 | u8 genmask) | |
c7a72e3f PNA |
3930 | { |
3931 | struct nft_set *set; | |
3932 | ||
cac20fcd | 3933 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
3934 | if (IS_ERR(set)) { |
3935 | if (!nla_set_id) | |
3936 | return set; | |
3937 | ||
470ee20e | 3938 | set = nft_set_lookup_byid(net, table, nla_set_id, genmask); |
c7a72e3f PNA |
3939 | } |
3940 | return set; | |
3941 | } | |
10659cba | 3942 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 3943 | |
20a69341 PM |
3944 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
3945 | const char *name) | |
3946 | { | |
3947 | const struct nft_set *i; | |
3948 | const char *p; | |
3949 | unsigned long *inuse; | |
60eb1894 | 3950 | unsigned int n = 0, min = 0; |
20a69341 | 3951 | |
38745490 | 3952 | p = strchr(name, '%'); |
20a69341 PM |
3953 | if (p != NULL) { |
3954 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
3955 | return -EINVAL; | |
3956 | ||
3957 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
3958 | if (inuse == NULL) | |
3959 | return -ENOMEM; | |
60eb1894 | 3960 | cont: |
20a69341 | 3961 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
3962 | int tmp; |
3963 | ||
271c5ca8 | 3964 | if (!nft_is_active_next(ctx->net, i)) |
37a9cc52 | 3965 | continue; |
14662917 | 3966 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 3967 | continue; |
60eb1894 | 3968 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 3969 | continue; |
14662917 | 3970 | |
60eb1894 | 3971 | set_bit(tmp - min, inuse); |
20a69341 PM |
3972 | } |
3973 | ||
53b70287 | 3974 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
3975 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
3976 | min += BITS_PER_BYTE * PAGE_SIZE; | |
3977 | memset(inuse, 0, PAGE_SIZE); | |
3978 | goto cont; | |
3979 | } | |
20a69341 PM |
3980 | free_page((unsigned long)inuse); |
3981 | } | |
3982 | ||
33758c89 | 3983 | set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n); |
38745490 PS |
3984 | if (!set->name) |
3985 | return -ENOMEM; | |
3986 | ||
20a69341 | 3987 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
3988 | if (!nft_is_active_next(ctx->net, i)) |
3989 | continue; | |
e63aaaa6 AY |
3990 | if (!strcmp(set->name, i->name)) { |
3991 | kfree(set->name); | |
7fb6f78d | 3992 | set->name = NULL; |
20a69341 | 3993 | return -ENFILE; |
e63aaaa6 | 3994 | } |
20a69341 PM |
3995 | } |
3996 | return 0; | |
3997 | } | |
3998 | ||
917d80d3 | 3999 | int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
8e1102d5 FW |
4000 | { |
4001 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
4002 | u64 max = (u64)(~((u64)0)); | |
4003 | ||
4004 | max = div_u64(max, NSEC_PER_MSEC); | |
4005 | if (ms >= max) | |
4006 | return -ERANGE; | |
4007 | ||
4008 | ms *= NSEC_PER_MSEC; | |
4009 | *result = nsecs_to_jiffies64(ms); | |
4010 | return 0; | |
4011 | } | |
4012 | ||
917d80d3 | 4013 | __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 4014 | { |
3b15d09f | 4015 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
4016 | } |
4017 | ||
f3a2181e SB |
4018 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
4019 | const struct nft_set *set) | |
4020 | { | |
4021 | struct nlattr *concat, *field; | |
4022 | int i; | |
4023 | ||
4024 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
4025 | if (!concat) | |
4026 | return -ENOMEM; | |
4027 | ||
4028 | for (i = 0; i < set->field_count; i++) { | |
4029 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
4030 | if (!field) | |
4031 | return -ENOMEM; | |
4032 | ||
4033 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
4034 | htonl(set->field_len[i]))) | |
4035 | return -ENOMEM; | |
4036 | ||
4037 | nla_nest_end(skb, field); | |
4038 | } | |
4039 | ||
4040 | nla_nest_end(skb, concat); | |
4041 | ||
4042 | return 0; | |
4043 | } | |
4044 | ||
20a69341 PM |
4045 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
4046 | const struct nft_set *set, u16 event, u16 flags) | |
4047 | { | |
20a69341 | 4048 | struct nlmsghdr *nlh; |
128ad332 | 4049 | u32 portid = ctx->portid; |
65038428 | 4050 | struct nlattr *nest; |
128ad332 | 4051 | u32 seq = ctx->seq; |
48b0ae04 | 4052 | int i; |
20a69341 | 4053 | |
dedb67c4 | 4054 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
4055 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
4056 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
4057 | if (!nlh) | |
20a69341 PM |
4058 | goto nla_put_failure; |
4059 | ||
20a69341 PM |
4060 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
4061 | goto nla_put_failure; | |
4062 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
4063 | goto nla_put_failure; | |
3ecbfd65 HS |
4064 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
4065 | NFTA_SET_PAD)) | |
4066 | goto nla_put_failure; | |
20a69341 PM |
4067 | if (set->flags != 0) |
4068 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
4069 | goto nla_put_failure; | |
4070 | ||
4071 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
4072 | goto nla_put_failure; | |
4073 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
4074 | goto nla_put_failure; | |
4075 | if (set->flags & NFT_SET_MAP) { | |
4076 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
4077 | goto nla_put_failure; | |
4078 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
4079 | goto nla_put_failure; | |
4080 | } | |
8aeff920 PNA |
4081 | if (set->flags & NFT_SET_OBJECT && |
4082 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
4083 | goto nla_put_failure; | |
20a69341 | 4084 | |
761da293 | 4085 | if (set->timeout && |
d3e2a111 | 4086 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
8e1102d5 | 4087 | nf_jiffies64_to_msecs(set->timeout), |
b46f6ded | 4088 | NFTA_SET_PAD)) |
761da293 PM |
4089 | goto nla_put_failure; |
4090 | if (set->gc_int && | |
4091 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) | |
4092 | goto nla_put_failure; | |
4093 | ||
9363dc4b AB |
4094 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
4095 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
4096 | goto nla_put_failure; | |
4097 | } | |
4098 | ||
6f03bf43 PNA |
4099 | if (set->udata && |
4100 | nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) | |
e6d8ecac CFG |
4101 | goto nla_put_failure; |
4102 | ||
65038428 PNA |
4103 | nest = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
4104 | if (!nest) | |
c50b960c PM |
4105 | goto nla_put_failure; |
4106 | if (set->size && | |
4107 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
4108 | goto nla_put_failure; | |
f3a2181e SB |
4109 | |
4110 | if (set->field_count > 1 && | |
4111 | nf_tables_fill_set_concat(skb, set)) | |
4112 | goto nla_put_failure; | |
4113 | ||
65038428 PNA |
4114 | nla_nest_end(skb, nest); |
4115 | ||
8cfd9b0f | 4116 | if (set->num_exprs == 1) { |
65038428 | 4117 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR); |
8daa8fde | 4118 | if (nf_tables_fill_expr_info(skb, set->exprs[0], false) < 0) |
65038428 PNA |
4119 | goto nla_put_failure; |
4120 | ||
4121 | nla_nest_end(skb, nest); | |
48b0ae04 PNA |
4122 | } else if (set->num_exprs > 1) { |
4123 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS); | |
4124 | if (nest == NULL) | |
65038428 PNA |
4125 | goto nla_put_failure; |
4126 | ||
48b0ae04 PNA |
4127 | for (i = 0; i < set->num_exprs; i++) { |
4128 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, | |
8daa8fde | 4129 | set->exprs[i], false) < 0) |
48b0ae04 PNA |
4130 | goto nla_put_failure; |
4131 | } | |
65038428 PNA |
4132 | nla_nest_end(skb, nest); |
4133 | } | |
c50b960c | 4134 | |
053c095a JB |
4135 | nlmsg_end(skb, nlh); |
4136 | return 0; | |
20a69341 PM |
4137 | |
4138 | nla_put_failure: | |
4139 | nlmsg_trim(skb, nlh); | |
4140 | return -1; | |
4141 | } | |
4142 | ||
25e94a99 PNA |
4143 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
4144 | const struct nft_set *set, int event, | |
4145 | gfp_t gfp_flags) | |
20a69341 | 4146 | { |
d59d2f82 | 4147 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
128ad332 | 4148 | u32 portid = ctx->portid; |
6fb721cf PNA |
4149 | struct sk_buff *skb; |
4150 | u16 flags = 0; | |
20a69341 PM |
4151 | int err; |
4152 | ||
128ad332 PNA |
4153 | if (!ctx->report && |
4154 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 4155 | return; |
20a69341 | 4156 | |
31f8441c | 4157 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
4158 | if (skb == NULL) |
4159 | goto err; | |
4160 | ||
6fb721cf PNA |
4161 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
4162 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
4163 | ||
4164 | err = nf_tables_fill_set(skb, ctx, set, event, flags); | |
20a69341 PM |
4165 | if (err < 0) { |
4166 | kfree_skb(skb); | |
4167 | goto err; | |
4168 | } | |
4169 | ||
0854db2a | 4170 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 4171 | return; |
20a69341 | 4172 | err: |
25e94a99 | 4173 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
4174 | } |
4175 | ||
5b96af77 | 4176 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
4177 | { |
4178 | const struct nft_set *set; | |
4179 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
4180 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
4181 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 4182 | struct nft_ctx *ctx = cb->data, ctx_set; |
0854db2a | 4183 | struct nftables_pernet *nft_net; |
c9c8e485 PNA |
4184 | |
4185 | if (cb->args[1]) | |
4186 | return skb->len; | |
4187 | ||
e688a7f8 | 4188 | rcu_read_lock(); |
d59d2f82 | 4189 | nft_net = nft_pernet(net); |
34002783 | 4190 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 4191 | |
0854db2a | 4192 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 4193 | if (ctx->family != NFPROTO_UNSPEC && |
98319cb9 | 4194 | ctx->family != table->family) |
36596dad PNA |
4195 | continue; |
4196 | ||
4197 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
4198 | continue; |
4199 | ||
36596dad PNA |
4200 | if (cur_table) { |
4201 | if (cur_table != table) | |
c9c8e485 PNA |
4202 | continue; |
4203 | ||
36596dad | 4204 | cur_table = NULL; |
c9c8e485 | 4205 | } |
36596dad PNA |
4206 | idx = 0; |
4207 | list_for_each_entry_rcu(set, &table->sets, list) { | |
4208 | if (idx < s_idx) | |
4209 | goto cont; | |
4210 | if (!nft_is_active(net, set)) | |
4211 | goto cont; | |
5b96af77 | 4212 | |
36596dad PNA |
4213 | ctx_set = *ctx; |
4214 | ctx_set.table = table; | |
98319cb9 | 4215 | ctx_set.family = table->family; |
c9c8e485 | 4216 | |
36596dad PNA |
4217 | if (nf_tables_fill_set(skb, &ctx_set, set, |
4218 | NFT_MSG_NEWSET, | |
4219 | NLM_F_MULTI) < 0) { | |
4220 | cb->args[0] = idx; | |
4221 | cb->args[2] = (unsigned long) table; | |
4222 | goto done; | |
c9c8e485 | 4223 | } |
36596dad | 4224 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 4225 | cont: |
36596dad | 4226 | idx++; |
c9c8e485 | 4227 | } |
36596dad PNA |
4228 | if (s_idx) |
4229 | s_idx = 0; | |
c9c8e485 PNA |
4230 | } |
4231 | cb->args[1] = 1; | |
4232 | done: | |
e688a7f8 | 4233 | rcu_read_unlock(); |
c9c8e485 PNA |
4234 | return skb->len; |
4235 | } | |
4236 | ||
90fd131a FW |
4237 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
4238 | { | |
4239 | struct nft_ctx *ctx_dump = NULL; | |
4240 | ||
4241 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
4242 | if (ctx_dump == NULL) | |
4243 | return -ENOMEM; | |
4244 | ||
4245 | cb->data = ctx_dump; | |
4246 | return 0; | |
4247 | } | |
4248 | ||
5b96af77 | 4249 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 4250 | { |
5b96af77 PNA |
4251 | kfree(cb->data); |
4252 | return 0; | |
20a69341 PM |
4253 | } |
4254 | ||
d9adf22a | 4255 | /* called with rcu_read_lock held */ |
797d4980 PNA |
4256 | static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info, |
4257 | const struct nlattr * const nla[]) | |
20a69341 | 4258 | { |
797d4980 PNA |
4259 | struct netlink_ext_ack *extack = info->extack; |
4260 | u8 genmask = nft_genmask_cur(info->net); | |
67086651 PNA |
4261 | u8 family = info->nfmsg->nfgen_family; |
4262 | struct nft_table *table = NULL; | |
797d4980 | 4263 | struct net *net = info->net; |
20a69341 | 4264 | const struct nft_set *set; |
20a69341 | 4265 | struct sk_buff *skb2; |
797d4980 | 4266 | struct nft_ctx ctx; |
20a69341 PM |
4267 | int err; |
4268 | ||
67086651 PNA |
4269 | if (nla[NFTA_SET_TABLE]) { |
4270 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, | |
4271 | genmask, 0); | |
4272 | if (IS_ERR(table)) { | |
4273 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4274 | return PTR_ERR(table); | |
4275 | } | |
4276 | } | |
4277 | ||
4278 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); | |
20a69341 | 4279 | |
797d4980 | 4280 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
20a69341 | 4281 | struct netlink_dump_control c = { |
90fd131a | 4282 | .start = nf_tables_dump_sets_start, |
20a69341 | 4283 | .dump = nf_tables_dump_sets, |
5b96af77 | 4284 | .done = nf_tables_dump_sets_done, |
90fd131a | 4285 | .data = &ctx, |
d9adf22a | 4286 | .module = THIS_MODULE, |
20a69341 | 4287 | }; |
5b96af77 | 4288 | |
797d4980 | 4289 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
20a69341 PM |
4290 | } |
4291 | ||
c9c8e485 | 4292 | /* Only accept unspec with dump */ |
ef4b65e5 | 4293 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
c9c8e485 | 4294 | return -EAFNOSUPPORT; |
eaa2bcd6 PT |
4295 | if (!nla[NFTA_SET_TABLE]) |
4296 | return -EINVAL; | |
c9c8e485 | 4297 | |
67086651 | 4298 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
4299 | if (IS_ERR(set)) |
4300 | return PTR_ERR(set); | |
4301 | ||
d9adf22a | 4302 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
4303 | if (skb2 == NULL) |
4304 | return -ENOMEM; | |
4305 | ||
4306 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
4307 | if (err < 0) | |
ee921183 | 4308 | goto err_fill_set_info; |
20a69341 | 4309 | |
ee921183 | 4310 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
20a69341 | 4311 | |
ee921183 | 4312 | err_fill_set_info: |
20a69341 PM |
4313 | kfree_skb(skb2); |
4314 | return err; | |
4315 | } | |
4316 | ||
f3a2181e SB |
4317 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
4318 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
4319 | }; | |
4320 | ||
4321 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
4322 | struct nft_set_desc *desc) | |
4323 | { | |
4324 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
4325 | u32 len; | |
4326 | int err; | |
4327 | ||
fecf31ee PNA |
4328 | if (desc->field_count >= ARRAY_SIZE(desc->field_len)) |
4329 | return -E2BIG; | |
4330 | ||
f3a2181e SB |
4331 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, |
4332 | nft_concat_policy, NULL); | |
4333 | if (err < 0) | |
4334 | return err; | |
4335 | ||
4336 | if (!tb[NFTA_SET_FIELD_LEN]) | |
4337 | return -EINVAL; | |
4338 | ||
4339 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
fecf31ee PNA |
4340 | if (!len || len > U8_MAX) |
4341 | return -EINVAL; | |
f3a2181e SB |
4342 | |
4343 | desc->field_len[desc->field_count++] = len; | |
4344 | ||
4345 | return 0; | |
4346 | } | |
4347 | ||
4348 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
4349 | const struct nlattr *nla) | |
4350 | { | |
4351 | struct nlattr *attr; | |
fecf31ee PNA |
4352 | u32 num_regs = 0; |
4353 | int rem, err, i; | |
f3a2181e SB |
4354 | |
4355 | nla_for_each_nested(attr, nla, rem) { | |
4356 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
4357 | return -EINVAL; | |
4358 | ||
4359 | err = nft_set_desc_concat_parse(attr, desc); | |
4360 | if (err < 0) | |
4361 | return err; | |
4362 | } | |
4363 | ||
fecf31ee PNA |
4364 | for (i = 0; i < desc->field_count; i++) |
4365 | num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32)); | |
4366 | ||
4367 | if (num_regs > NFT_REG32_COUNT) | |
4368 | return -E2BIG; | |
4369 | ||
f3a2181e SB |
4370 | return 0; |
4371 | } | |
4372 | ||
f7e840ee | 4373 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
4374 | const struct nlattr *nla) |
4375 | { | |
4376 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
4377 | int err; | |
4378 | ||
8cb08174 JB |
4379 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
4380 | nft_set_desc_policy, NULL); | |
c50b960c PM |
4381 | if (err < 0) |
4382 | return err; | |
4383 | ||
4384 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
4385 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
4386 | if (da[NFTA_SET_DESC_CONCAT]) |
4387 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 4388 | |
f3a2181e | 4389 | return err; |
c50b960c PM |
4390 | } |
4391 | ||
7dab8ee3 PNA |
4392 | static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, |
4393 | const struct nlattr * const nla[]) | |
20a69341 | 4394 | { |
7dab8ee3 PNA |
4395 | u32 ktype, dtype, flags, policy, gc_int, objtype; |
4396 | struct netlink_ext_ack *extack = info->extack; | |
4397 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 4398 | u8 family = info->nfmsg->nfgen_family; |
20a69341 | 4399 | const struct nft_set_ops *ops; |
65038428 | 4400 | struct nft_expr *expr = NULL; |
7dab8ee3 PNA |
4401 | struct net *net = info->net; |
4402 | struct nft_set_desc desc; | |
20a69341 | 4403 | struct nft_table *table; |
7dab8ee3 | 4404 | unsigned char *udata; |
20a69341 PM |
4405 | struct nft_set *set; |
4406 | struct nft_ctx ctx; | |
6c8774a9 | 4407 | size_t alloc_size; |
761da293 | 4408 | u64 timeout; |
7dab8ee3 PNA |
4409 | char *name; |
4410 | int err, i; | |
e6d8ecac | 4411 | u16 udlen; |
7dab8ee3 | 4412 | u64 size; |
20a69341 PM |
4413 | |
4414 | if (nla[NFTA_SET_TABLE] == NULL || | |
4415 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
4416 | nla[NFTA_SET_KEY_LEN] == NULL || |
4417 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
4418 | return -EINVAL; |
4419 | ||
c50b960c PM |
4420 | memset(&desc, 0, sizeof(desc)); |
4421 | ||
20a69341 PM |
4422 | ktype = NFT_DATA_VALUE; |
4423 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { | |
4424 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); | |
4425 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
4426 | return -EINVAL; | |
4427 | } | |
4428 | ||
c50b960c | 4429 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 4430 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4431 | return -EINVAL; |
4432 | ||
4433 | flags = 0; | |
4434 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
4435 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
4436 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 4437 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 | 4438 | NFT_SET_MAP | NFT_SET_EVAL | |
b4e70d8d | 4439 | NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR)) |
d9583cdf | 4440 | return -EOPNOTSUPP; |
8aeff920 | 4441 | /* Only one of these operations is supported */ |
acab7131 FW |
4442 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
4443 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
4444 | return -EOPNOTSUPP; | |
4445 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
4446 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 4447 | return -EOPNOTSUPP; |
20a69341 PM |
4448 | } |
4449 | ||
4450 | dtype = 0; | |
20a69341 PM |
4451 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
4452 | if (!(flags & NFT_SET_MAP)) | |
4453 | return -EINVAL; | |
4454 | ||
4455 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); | |
4456 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
4457 | dtype != NFT_DATA_VERDICT) | |
4458 | return -EINVAL; | |
4459 | ||
4460 | if (dtype != NFT_DATA_VERDICT) { | |
4461 | if (nla[NFTA_SET_DATA_LEN] == NULL) | |
4462 | return -EINVAL; | |
c50b960c | 4463 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 4464 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4465 | return -EINVAL; |
4466 | } else | |
7d740264 | 4467 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
4468 | } else if (flags & NFT_SET_MAP) |
4469 | return -EINVAL; | |
4470 | ||
8aeff920 PNA |
4471 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
4472 | if (!(flags & NFT_SET_OBJECT)) | |
4473 | return -EINVAL; | |
4474 | ||
4475 | objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); | |
4476 | if (objtype == NFT_OBJECT_UNSPEC || | |
4477 | objtype > NFT_OBJECT_MAX) | |
d9583cdf | 4478 | return -EOPNOTSUPP; |
8aeff920 PNA |
4479 | } else if (flags & NFT_SET_OBJECT) |
4480 | return -EINVAL; | |
4481 | else | |
4482 | objtype = NFT_OBJECT_UNSPEC; | |
4483 | ||
761da293 PM |
4484 | timeout = 0; |
4485 | if (nla[NFTA_SET_TIMEOUT] != NULL) { | |
4486 | if (!(flags & NFT_SET_TIMEOUT)) | |
4487 | return -EINVAL; | |
8e1102d5 FW |
4488 | |
4489 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout); | |
4490 | if (err) | |
4491 | return err; | |
761da293 PM |
4492 | } |
4493 | gc_int = 0; | |
4494 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { | |
4495 | if (!(flags & NFT_SET_TIMEOUT)) | |
4496 | return -EINVAL; | |
4497 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); | |
4498 | } | |
4499 | ||
c50b960c PM |
4500 | policy = NFT_SET_POL_PERFORMANCE; |
4501 | if (nla[NFTA_SET_POLICY] != NULL) | |
4502 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); | |
4503 | ||
4504 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4505 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4506 | if (err < 0) |
4507 | return err; | |
1b6345d4 PNA |
4508 | |
4509 | if (desc.field_count > 1 && !(flags & NFT_SET_CONCAT)) | |
4510 | return -EINVAL; | |
4511 | } else if (flags & NFT_SET_CONCAT) { | |
4512 | return -EINVAL; | |
c50b960c PM |
4513 | } |
4514 | ||
48b0ae04 | 4515 | if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS]) |
d56aab26 PNA |
4516 | desc.expr = true; |
4517 | ||
6001a930 PNA |
4518 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask, |
4519 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4520 | if (IS_ERR(table)) { |
4521 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4522 | return PTR_ERR(table); |
36dd1bcc | 4523 | } |
20a69341 | 4524 | |
7dab8ee3 | 4525 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
20a69341 | 4526 | |
cac20fcd | 4527 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4528 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4529 | if (PTR_ERR(set) != -ENOENT) { |
4530 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4531 | return PTR_ERR(set); |
36dd1bcc | 4532 | } |
1a28ad74 | 4533 | } else { |
7dab8ee3 | 4534 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 4535 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); |
20a69341 | 4536 | return -EEXIST; |
36dd1bcc | 4537 | } |
7dab8ee3 | 4538 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
20a69341 | 4539 | return -EOPNOTSUPP; |
36dd1bcc | 4540 | |
20a69341 PM |
4541 | return 0; |
4542 | } | |
4543 | ||
7dab8ee3 | 4544 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
20a69341 PM |
4545 | return -ENOENT; |
4546 | ||
2b664957 | 4547 | ops = nft_select_set_ops(&ctx, nla, &desc, policy); |
20a69341 PM |
4548 | if (IS_ERR(ops)) |
4549 | return PTR_ERR(ops); | |
4550 | ||
e6d8ecac CFG |
4551 | udlen = 0; |
4552 | if (nla[NFTA_SET_USERDATA]) | |
4553 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
4554 | ||
20a69341 PM |
4555 | size = 0; |
4556 | if (ops->privsize != NULL) | |
347b408d | 4557 | size = ops->privsize(nla, &desc); |
6c8774a9 | 4558 | alloc_size = sizeof(*set) + size + udlen; |
45928afe | 4559 | if (alloc_size < size || alloc_size > INT_MAX) |
6c8774a9 | 4560 | return -ENOMEM; |
33758c89 | 4561 | set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT); |
24d19826 FW |
4562 | if (!set) |
4563 | return -ENOMEM; | |
20a69341 | 4564 | |
33758c89 | 4565 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT); |
38745490 PS |
4566 | if (!name) { |
4567 | err = -ENOMEM; | |
65038428 | 4568 | goto err_set_name; |
38745490 PS |
4569 | } |
4570 | ||
20a69341 | 4571 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 4572 | kfree(name); |
20a69341 | 4573 | if (err < 0) |
ad9f151e PNA |
4574 | goto err_set_name; |
4575 | ||
4576 | udata = NULL; | |
4577 | if (udlen) { | |
4578 | udata = set->data + size; | |
4579 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
4580 | } | |
4581 | ||
4582 | INIT_LIST_HEAD(&set->bindings); | |
4583 | INIT_LIST_HEAD(&set->catchall_list); | |
4584 | set->table = table; | |
4585 | write_pnet(&set->net, net); | |
4586 | set->ops = ops; | |
4587 | set->ktype = ktype; | |
4588 | set->klen = desc.klen; | |
4589 | set->dtype = dtype; | |
4590 | set->objtype = objtype; | |
4591 | set->dlen = desc.dlen; | |
4592 | set->flags = flags; | |
4593 | set->size = desc.size; | |
4594 | set->policy = policy; | |
4595 | set->udlen = udlen; | |
4596 | set->udata = udata; | |
4597 | set->timeout = timeout; | |
4598 | set->gc_int = gc_int; | |
4599 | ||
4600 | set->field_count = desc.field_count; | |
4601 | for (i = 0; i < desc.field_count; i++) | |
4602 | set->field_len[i] = desc.field_len[i]; | |
4603 | ||
4604 | err = ops->init(set, &desc, nla); | |
4605 | if (err < 0) | |
4606 | goto err_set_init; | |
65038428 PNA |
4607 | |
4608 | if (nla[NFTA_SET_EXPR]) { | |
4609 | expr = nft_set_elem_expr_alloc(&ctx, set, nla[NFTA_SET_EXPR]); | |
4610 | if (IS_ERR(expr)) { | |
4611 | err = PTR_ERR(expr); | |
ad9f151e | 4612 | goto err_set_expr_alloc; |
65038428 | 4613 | } |
8cfd9b0f PNA |
4614 | set->exprs[0] = expr; |
4615 | set->num_exprs++; | |
48b0ae04 PNA |
4616 | } else if (nla[NFTA_SET_EXPRESSIONS]) { |
4617 | struct nft_expr *expr; | |
4618 | struct nlattr *tmp; | |
4619 | int left; | |
4620 | ||
b4e70d8d PNA |
4621 | if (!(flags & NFT_SET_EXPR)) { |
4622 | err = -EINVAL; | |
ad9f151e | 4623 | goto err_set_expr_alloc; |
b4e70d8d | 4624 | } |
48b0ae04 PNA |
4625 | i = 0; |
4626 | nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) { | |
4627 | if (i == NFT_SET_EXPR_MAX) { | |
4628 | err = -E2BIG; | |
ad9f151e | 4629 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4630 | } |
4631 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
4632 | err = -EINVAL; | |
ad9f151e | 4633 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4634 | } |
4635 | expr = nft_set_elem_expr_alloc(&ctx, set, tmp); | |
4636 | if (IS_ERR(expr)) { | |
4637 | err = PTR_ERR(expr); | |
ad9f151e | 4638 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4639 | } |
4640 | set->exprs[i++] = expr; | |
4641 | set->num_exprs++; | |
4642 | } | |
65038428 | 4643 | } |
20a69341 | 4644 | |
3ecbfd65 | 4645 | set->handle = nf_tables_alloc_handle(table); |
20a69341 | 4646 | |
958bee14 | 4647 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 4648 | if (err < 0) |
ad9f151e | 4649 | goto err_set_expr_alloc; |
20a69341 | 4650 | |
e688a7f8 | 4651 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 4652 | table->use++; |
20a69341 PM |
4653 | return 0; |
4654 | ||
ad9f151e | 4655 | err_set_expr_alloc: |
8cfd9b0f PNA |
4656 | for (i = 0; i < set->num_exprs; i++) |
4657 | nft_expr_destroy(&ctx, set->exprs[i]); | |
ad9f151e PNA |
4658 | |
4659 | ops->destroy(set); | |
4660 | err_set_init: | |
2f6adf48 | 4661 | kfree(set->name); |
65038428 | 4662 | err_set_name: |
1ff75a3e | 4663 | kvfree(set); |
20a69341 PM |
4664 | return err; |
4665 | } | |
4666 | ||
aaa31047 PNA |
4667 | struct nft_set_elem_catchall { |
4668 | struct list_head list; | |
4669 | struct rcu_head rcu; | |
4670 | void *elem; | |
4671 | }; | |
4672 | ||
4673 | static void nft_set_catchall_destroy(const struct nft_ctx *ctx, | |
4674 | struct nft_set *set) | |
4675 | { | |
0f7d9b31 | 4676 | struct nft_set_elem_catchall *next, *catchall; |
aaa31047 | 4677 | |
0f7d9b31 | 4678 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
aaa31047 PNA |
4679 | list_del_rcu(&catchall->list); |
4680 | nft_set_elem_destroy(set, catchall->elem, true); | |
ae089831 | 4681 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
4682 | } |
4683 | } | |
4684 | ||
0c2a85ed | 4685 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
20a69341 | 4686 | { |
8cfd9b0f PNA |
4687 | int i; |
4688 | ||
273fe3f1 PNA |
4689 | if (WARN_ON(set->use > 0)) |
4690 | return; | |
4691 | ||
8cfd9b0f PNA |
4692 | for (i = 0; i < set->num_exprs; i++) |
4693 | nft_expr_destroy(ctx, set->exprs[i]); | |
65038428 | 4694 | |
20a69341 | 4695 | set->ops->destroy(set); |
aaa31047 | 4696 | nft_set_catchall_destroy(ctx, set); |
38745490 | 4697 | kfree(set->name); |
1ff75a3e | 4698 | kvfree(set); |
20a69341 PM |
4699 | } |
4700 | ||
7dab8ee3 PNA |
4701 | static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info, |
4702 | const struct nlattr * const nla[]) | |
20a69341 | 4703 | { |
7dab8ee3 PNA |
4704 | struct netlink_ext_ack *extack = info->extack; |
4705 | u8 genmask = nft_genmask_next(info->net); | |
67086651 | 4706 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 4707 | struct net *net = info->net; |
36dd1bcc | 4708 | const struct nlattr *attr; |
67086651 | 4709 | struct nft_table *table; |
20a69341 PM |
4710 | struct nft_set *set; |
4711 | struct nft_ctx ctx; | |
20a69341 | 4712 | |
ef4b65e5 | 4713 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
ec2c9935 | 4714 | return -EAFNOSUPPORT; |
20a69341 | 4715 | |
67086651 PNA |
4716 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
4717 | genmask, NETLINK_CB(skb).portid); | |
4718 | if (IS_ERR(table)) { | |
4719 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4720 | return PTR_ERR(table); | |
4721 | } | |
20a69341 | 4722 | |
36dd1bcc PNA |
4723 | if (nla[NFTA_SET_HANDLE]) { |
4724 | attr = nla[NFTA_SET_HANDLE]; | |
67086651 | 4725 | set = nft_set_lookup_byhandle(table, attr, genmask); |
36dd1bcc PNA |
4726 | } else { |
4727 | attr = nla[NFTA_SET_NAME]; | |
67086651 | 4728 | set = nft_set_lookup(table, attr, genmask); |
36dd1bcc | 4729 | } |
a8278400 | 4730 | |
36dd1bcc PNA |
4731 | if (IS_ERR(set)) { |
4732 | NL_SET_BAD_ATTR(extack, attr); | |
4733 | return PTR_ERR(set); | |
4734 | } | |
273fe3f1 | 4735 | if (set->use || |
7dab8ee3 PNA |
4736 | (info->nlh->nlmsg_flags & NLM_F_NONREC && |
4737 | atomic_read(&set->nelems) > 0)) { | |
36dd1bcc | 4738 | NL_SET_BAD_ATTR(extack, attr); |
20a69341 | 4739 | return -EBUSY; |
36dd1bcc | 4740 | } |
20a69341 | 4741 | |
67086651 PNA |
4742 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
4743 | ||
ee01d542 | 4744 | return nft_delset(&ctx, set); |
20a69341 PM |
4745 | } |
4746 | ||
345023b0 PNA |
4747 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
4748 | enum nft_registers reg, | |
4749 | const struct nft_data *data, | |
4750 | enum nft_data_types type, | |
4751 | unsigned int len); | |
4752 | ||
97c976d6 PNA |
4753 | static int nft_setelem_data_validate(const struct nft_ctx *ctx, |
4754 | struct nft_set *set, | |
4755 | struct nft_set_elem *elem) | |
20a69341 | 4756 | { |
fe2811eb | 4757 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4758 | enum nft_registers dreg; |
4759 | ||
4760 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
4761 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
4762 | set->dtype == NFT_DATA_VERDICT ? | |
4763 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
4764 | set->dlen); | |
20a69341 PM |
4765 | } |
4766 | ||
97c976d6 PNA |
4767 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, |
4768 | struct nft_set *set, | |
4769 | const struct nft_set_iter *iter, | |
4770 | struct nft_set_elem *elem) | |
4771 | { | |
4772 | return nft_setelem_data_validate(ctx, set, elem); | |
4773 | } | |
4774 | ||
aaa31047 PNA |
4775 | static int nft_set_catchall_bind_check(const struct nft_ctx *ctx, |
4776 | struct nft_set *set) | |
4777 | { | |
4778 | u8 genmask = nft_genmask_next(ctx->net); | |
4779 | struct nft_set_elem_catchall *catchall; | |
4780 | struct nft_set_elem elem; | |
4781 | struct nft_set_ext *ext; | |
4782 | int ret = 0; | |
4783 | ||
4784 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
4785 | ext = nft_set_elem_ext(set, catchall->elem); | |
4786 | if (!nft_set_elem_active(ext, genmask)) | |
4787 | continue; | |
4788 | ||
4789 | elem.priv = catchall->elem; | |
4790 | ret = nft_setelem_data_validate(ctx, set, &elem); | |
4791 | if (ret < 0) | |
4792 | break; | |
4793 | } | |
4794 | ||
4795 | return ret; | |
4796 | } | |
4797 | ||
20a69341 PM |
4798 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4799 | struct nft_set_binding *binding) | |
4800 | { | |
4801 | struct nft_set_binding *i; | |
4802 | struct nft_set_iter iter; | |
4803 | ||
273fe3f1 PNA |
4804 | if (set->use == UINT_MAX) |
4805 | return -EOVERFLOW; | |
4806 | ||
408070d6 | 4807 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
4808 | return -EBUSY; |
4809 | ||
11113e19 | 4810 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
4811 | /* If the set is already bound to the same chain all |
4812 | * jumps are already validated for that chain. | |
4813 | */ | |
4814 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 4815 | if (i->flags & NFT_SET_MAP && |
11113e19 | 4816 | i->chain == binding->chain) |
20a69341 PM |
4817 | goto bind; |
4818 | } | |
4819 | ||
8588ac09 | 4820 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
4821 | iter.skip = 0; |
4822 | iter.count = 0; | |
4823 | iter.err = 0; | |
4824 | iter.fn = nf_tables_bind_check_setelem; | |
4825 | ||
4826 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
4827 | if (!iter.err) |
4828 | iter.err = nft_set_catchall_bind_check(ctx, set); | |
4829 | ||
a02f4248 | 4830 | if (iter.err < 0) |
20a69341 | 4831 | return iter.err; |
20a69341 PM |
4832 | } |
4833 | bind: | |
4834 | binding->chain = ctx->chain; | |
e688a7f8 | 4835 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 | 4836 | nft_set_trans_bind(ctx, set); |
273fe3f1 | 4837 | set->use++; |
f6ac8585 | 4838 | |
20a69341 PM |
4839 | return 0; |
4840 | } | |
63aea290 | 4841 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 4842 | |
3b0a081d FW |
4843 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4844 | struct nft_set_binding *binding, bool event) | |
20a69341 | 4845 | { |
e688a7f8 | 4846 | list_del_rcu(&binding->list); |
20a69341 | 4847 | |
f6ac8585 | 4848 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 4849 | list_del_rcu(&set->list); |
f6ac8585 PNA |
4850 | if (event) |
4851 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
4852 | GFP_KERNEL); | |
4853 | } | |
20a69341 PM |
4854 | } |
4855 | ||
273fe3f1 PNA |
4856 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
4857 | struct nft_set_binding *binding, | |
4858 | enum nft_trans_phase phase) | |
4859 | { | |
4860 | switch (phase) { | |
4861 | case NFT_TRANS_PREPARE: | |
4862 | set->use--; | |
4863 | return; | |
4864 | case NFT_TRANS_ABORT: | |
4865 | case NFT_TRANS_RELEASE: | |
4866 | set->use--; | |
954d8297 | 4867 | fallthrough; |
273fe3f1 PNA |
4868 | default: |
4869 | nf_tables_unbind_set(ctx, set, binding, | |
4870 | phase == NFT_TRANS_COMMIT); | |
4871 | } | |
4872 | } | |
4873 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
4874 | ||
cd5125d8 FW |
4875 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
4876 | { | |
f6ac8585 | 4877 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
0c2a85ed | 4878 | nft_set_destroy(ctx, set); |
cd5125d8 FW |
4879 | } |
4880 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
4881 | ||
3ac4c07a PM |
4882 | const struct nft_set_ext_type nft_set_ext_types[] = { |
4883 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 4884 | .align = __alignof__(u32), |
3ac4c07a PM |
4885 | }, |
4886 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 4887 | .align = __alignof__(u32), |
3ac4c07a | 4888 | }, |
563125a7 PNA |
4889 | [NFT_SET_EXT_EXPRESSIONS] = { |
4890 | .align = __alignof__(struct nft_set_elem_expr), | |
f25ad2e9 | 4891 | }, |
8aeff920 PNA |
4892 | [NFT_SET_EXT_OBJREF] = { |
4893 | .len = sizeof(struct nft_object *), | |
4894 | .align = __alignof__(struct nft_object *), | |
4895 | }, | |
3ac4c07a PM |
4896 | [NFT_SET_EXT_FLAGS] = { |
4897 | .len = sizeof(u8), | |
4898 | .align = __alignof__(u8), | |
4899 | }, | |
c3e1b005 PM |
4900 | [NFT_SET_EXT_TIMEOUT] = { |
4901 | .len = sizeof(u64), | |
4902 | .align = __alignof__(u64), | |
4903 | }, | |
4904 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
4905 | .len = sizeof(u64), |
4906 | .align = __alignof__(u64), | |
c3e1b005 | 4907 | }, |
68e942e8 PM |
4908 | [NFT_SET_EXT_USERDATA] = { |
4909 | .len = sizeof(struct nft_userdata), | |
4910 | .align = __alignof__(struct nft_userdata), | |
4911 | }, | |
7b225d0b PNA |
4912 | [NFT_SET_EXT_KEY_END] = { |
4913 | .align = __alignof__(u32), | |
4914 | }, | |
3ac4c07a | 4915 | }; |
3ac4c07a | 4916 | |
20a69341 PM |
4917 | /* |
4918 | * Set elements | |
4919 | */ | |
4920 | ||
4921 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
4922 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
4923 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
4924 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 4925 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 4926 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
4927 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
4928 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 4929 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
4930 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
4931 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 4932 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
48b0ae04 | 4933 | [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED }, |
20a69341 PM |
4934 | }; |
4935 | ||
4936 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
4937 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
4938 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
4939 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
4940 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 4941 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 4942 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
4943 | }; |
4944 | ||
563125a7 PNA |
4945 | static int nft_set_elem_expr_dump(struct sk_buff *skb, |
4946 | const struct nft_set *set, | |
4947 | const struct nft_set_ext *ext) | |
4948 | { | |
4949 | struct nft_set_elem_expr *elem_expr; | |
4950 | u32 size, num_exprs = 0; | |
4951 | struct nft_expr *expr; | |
48b0ae04 | 4952 | struct nlattr *nest; |
563125a7 PNA |
4953 | |
4954 | elem_expr = nft_set_ext_expr(ext); | |
4955 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
4956 | num_exprs++; | |
4957 | ||
4958 | if (num_exprs == 1) { | |
4959 | expr = nft_setelem_expr_at(elem_expr, 0); | |
8daa8fde | 4960 | if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr, false) < 0) |
563125a7 PNA |
4961 | return -1; |
4962 | ||
4963 | return 0; | |
48b0ae04 PNA |
4964 | } else if (num_exprs > 1) { |
4965 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS); | |
4966 | if (nest == NULL) | |
4967 | goto nla_put_failure; | |
563125a7 | 4968 | |
48b0ae04 PNA |
4969 | nft_setelem_expr_foreach(expr, elem_expr, size) { |
4970 | expr = nft_setelem_expr_at(elem_expr, size); | |
8daa8fde | 4971 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, false) < 0) |
48b0ae04 PNA |
4972 | goto nla_put_failure; |
4973 | } | |
4974 | nla_nest_end(skb, nest); | |
4975 | } | |
563125a7 | 4976 | return 0; |
48b0ae04 PNA |
4977 | |
4978 | nla_put_failure: | |
4979 | return -1; | |
563125a7 PNA |
4980 | } |
4981 | ||
20a69341 PM |
4982 | static int nf_tables_fill_setelem(struct sk_buff *skb, |
4983 | const struct nft_set *set, | |
4984 | const struct nft_set_elem *elem) | |
4985 | { | |
fe2811eb | 4986 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4987 | unsigned char *b = skb_tail_pointer(skb); |
4988 | struct nlattr *nest; | |
4989 | ||
ae0be8de | 4990 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
4991 | if (nest == NULL) |
4992 | goto nla_put_failure; | |
4993 | ||
aaa31047 PNA |
4994 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
4995 | nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), | |
fe2811eb | 4996 | NFT_DATA_VALUE, set->klen) < 0) |
20a69341 PM |
4997 | goto nla_put_failure; |
4998 | ||
7b225d0b PNA |
4999 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
5000 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
5001 | NFT_DATA_VALUE, set->klen) < 0) | |
5002 | goto nla_put_failure; | |
5003 | ||
fe2811eb PM |
5004 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
5005 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
5006 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
5007 | set->dlen) < 0) | |
5008 | goto nla_put_failure; | |
5009 | ||
563125a7 PNA |
5010 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) && |
5011 | nft_set_elem_expr_dump(skb, set, ext)) | |
f25ad2e9 PM |
5012 | goto nla_put_failure; |
5013 | ||
8aeff920 PNA |
5014 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
5015 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 5016 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
5017 | goto nla_put_failure; |
5018 | ||
fe2811eb PM |
5019 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
5020 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
5021 | htonl(*nft_set_ext_flags(ext)))) | |
5022 | goto nla_put_failure; | |
20a69341 | 5023 | |
c3e1b005 PM |
5024 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
5025 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 5026 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 5027 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
5028 | goto nla_put_failure; |
5029 | ||
5030 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 5031 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
5032 | |
5033 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 5034 | if (time_before64(now, expires)) |
c3e1b005 PM |
5035 | expires -= now; |
5036 | else | |
5037 | expires = 0; | |
5038 | ||
5039 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 5040 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 5041 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
5042 | goto nla_put_failure; |
5043 | } | |
5044 | ||
68e942e8 PM |
5045 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
5046 | struct nft_userdata *udata; | |
5047 | ||
5048 | udata = nft_set_ext_userdata(ext); | |
5049 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
5050 | udata->len + 1, udata->data)) | |
5051 | goto nla_put_failure; | |
5052 | } | |
5053 | ||
20a69341 PM |
5054 | nla_nest_end(skb, nest); |
5055 | return 0; | |
5056 | ||
5057 | nla_put_failure: | |
5058 | nlmsg_trim(skb, b); | |
5059 | return -EMSGSIZE; | |
5060 | } | |
5061 | ||
5062 | struct nft_set_dump_args { | |
5063 | const struct netlink_callback *cb; | |
5064 | struct nft_set_iter iter; | |
5065 | struct sk_buff *skb; | |
5066 | }; | |
5067 | ||
5068 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 5069 | struct nft_set *set, |
20a69341 | 5070 | const struct nft_set_iter *iter, |
de70185d | 5071 | struct nft_set_elem *elem) |
20a69341 PM |
5072 | { |
5073 | struct nft_set_dump_args *args; | |
5074 | ||
5075 | args = container_of(iter, struct nft_set_dump_args, iter); | |
5076 | return nf_tables_fill_setelem(args->skb, set, elem); | |
5077 | } | |
5078 | ||
fa803605 LZ |
5079 | struct nft_set_dump_ctx { |
5080 | const struct nft_set *set; | |
5081 | struct nft_ctx ctx; | |
5082 | }; | |
5083 | ||
aaa31047 PNA |
5084 | static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb, |
5085 | const struct nft_set *set) | |
5086 | { | |
5087 | struct nft_set_elem_catchall *catchall; | |
5088 | u8 genmask = nft_genmask_cur(net); | |
5089 | struct nft_set_elem elem; | |
5090 | struct nft_set_ext *ext; | |
5091 | int ret = 0; | |
5092 | ||
5093 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5094 | ext = nft_set_elem_ext(set, catchall->elem); | |
5095 | if (!nft_set_elem_active(ext, genmask) || | |
5096 | nft_set_elem_expired(ext)) | |
5097 | continue; | |
5098 | ||
5099 | elem.priv = catchall->elem; | |
5100 | ret = nf_tables_fill_setelem(skb, set, &elem); | |
5101 | break; | |
5102 | } | |
5103 | ||
5104 | return ret; | |
5105 | } | |
5106 | ||
20a69341 PM |
5107 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
5108 | { | |
fa803605 | 5109 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 5110 | struct net *net = sock_net(skb->sk); |
0854db2a | 5111 | struct nftables_pernet *nft_net; |
fa803605 | 5112 | struct nft_table *table; |
de70185d | 5113 | struct nft_set *set; |
20a69341 | 5114 | struct nft_set_dump_args args; |
fa803605 | 5115 | bool set_found = false; |
20a69341 PM |
5116 | struct nlmsghdr *nlh; |
5117 | struct nlattr *nest; | |
5118 | u32 portid, seq; | |
fa803605 | 5119 | int event; |
20a69341 | 5120 | |
fa803605 | 5121 | rcu_read_lock(); |
d59d2f82 | 5122 | nft_net = nft_pernet(net); |
34002783 PNA |
5123 | cb->seq = READ_ONCE(nft_net->base_seq); |
5124 | ||
0854db2a | 5125 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 5126 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && |
98319cb9 | 5127 | dump_ctx->ctx.family != table->family) |
fa803605 | 5128 | continue; |
20a69341 | 5129 | |
36596dad PNA |
5130 | if (table != dump_ctx->ctx.table) |
5131 | continue; | |
20a69341 | 5132 | |
36596dad PNA |
5133 | list_for_each_entry_rcu(set, &table->sets, list) { |
5134 | if (set == dump_ctx->set) { | |
5135 | set_found = true; | |
5136 | break; | |
fa803605 | 5137 | } |
fa803605 LZ |
5138 | } |
5139 | break; | |
5140 | } | |
5141 | ||
5142 | if (!set_found) { | |
5143 | rcu_read_unlock(); | |
5144 | return -ENOENT; | |
5145 | } | |
20a69341 | 5146 | |
dedb67c4 | 5147 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
5148 | portid = NETLINK_CB(cb->skb).portid; |
5149 | seq = cb->nlh->nlmsg_seq; | |
5150 | ||
19c28b13 PNA |
5151 | nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI, |
5152 | table->family, NFNETLINK_V0, nft_base_seq(net)); | |
5153 | if (!nlh) | |
20a69341 PM |
5154 | goto nla_put_failure; |
5155 | ||
fa803605 | 5156 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
5157 | goto nla_put_failure; |
5158 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
5159 | goto nla_put_failure; | |
5160 | ||
ae0be8de | 5161 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
5162 | if (nest == NULL) |
5163 | goto nla_put_failure; | |
5164 | ||
8588ac09 PNA |
5165 | args.cb = cb; |
5166 | args.skb = skb; | |
fa803605 | 5167 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
5168 | args.iter.skip = cb->args[0]; |
5169 | args.iter.count = 0; | |
5170 | args.iter.err = 0; | |
5171 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 | 5172 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
aaa31047 PNA |
5173 | |
5174 | if (!args.iter.err && args.iter.count == cb->args[0]) | |
5175 | args.iter.err = nft_set_catchall_dump(net, skb, set); | |
fa803605 | 5176 | rcu_read_unlock(); |
20a69341 PM |
5177 | |
5178 | nla_nest_end(skb, nest); | |
5179 | nlmsg_end(skb, nlh); | |
5180 | ||
5181 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
5182 | return args.iter.err; | |
5183 | if (args.iter.count == cb->args[0]) | |
5184 | return 0; | |
5185 | ||
5186 | cb->args[0] = args.iter.count; | |
5187 | return skb->len; | |
5188 | ||
5189 | nla_put_failure: | |
fa803605 | 5190 | rcu_read_unlock(); |
20a69341 PM |
5191 | return -ENOSPC; |
5192 | } | |
5193 | ||
90fd131a FW |
5194 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
5195 | { | |
5196 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
5197 | ||
5198 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
5199 | ||
5200 | return cb->data ? 0 : -ENOMEM; | |
5201 | } | |
5202 | ||
fa803605 LZ |
5203 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
5204 | { | |
5205 | kfree(cb->data); | |
5206 | return 0; | |
5207 | } | |
5208 | ||
d60ce62f AB |
5209 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
5210 | const struct nft_ctx *ctx, u32 seq, | |
5211 | u32 portid, int event, u16 flags, | |
5212 | const struct nft_set *set, | |
5213 | const struct nft_set_elem *elem) | |
5214 | { | |
d60ce62f AB |
5215 | struct nlmsghdr *nlh; |
5216 | struct nlattr *nest; | |
5217 | int err; | |
5218 | ||
dedb67c4 | 5219 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
5220 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
5221 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
5222 | if (!nlh) | |
d60ce62f AB |
5223 | goto nla_put_failure; |
5224 | ||
d60ce62f AB |
5225 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
5226 | goto nla_put_failure; | |
5227 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
5228 | goto nla_put_failure; | |
5229 | ||
ae0be8de | 5230 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
5231 | if (nest == NULL) |
5232 | goto nla_put_failure; | |
5233 | ||
5234 | err = nf_tables_fill_setelem(skb, set, elem); | |
5235 | if (err < 0) | |
5236 | goto nla_put_failure; | |
5237 | ||
5238 | nla_nest_end(skb, nest); | |
5239 | ||
053c095a JB |
5240 | nlmsg_end(skb, nlh); |
5241 | return 0; | |
d60ce62f AB |
5242 | |
5243 | nla_put_failure: | |
5244 | nlmsg_trim(skb, nlh); | |
5245 | return -1; | |
5246 | } | |
5247 | ||
ba0e4d99 PNA |
5248 | static int nft_setelem_parse_flags(const struct nft_set *set, |
5249 | const struct nlattr *attr, u32 *flags) | |
5250 | { | |
5251 | if (attr == NULL) | |
5252 | return 0; | |
5253 | ||
5254 | *flags = ntohl(nla_get_be32(attr)); | |
aaa31047 PNA |
5255 | if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) |
5256 | return -EOPNOTSUPP; | |
ba0e4d99 PNA |
5257 | if (!(set->flags & NFT_SET_INTERVAL) && |
5258 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
5259 | return -EINVAL; | |
fc0ae524 PNA |
5260 | if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) == |
5261 | (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) | |
5262 | return -EINVAL; | |
ba0e4d99 PNA |
5263 | |
5264 | return 0; | |
5265 | } | |
5266 | ||
20a1452c PNA |
5267 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
5268 | struct nft_data *key, struct nlattr *attr) | |
5269 | { | |
341b6941 PNA |
5270 | struct nft_data_desc desc = { |
5271 | .type = NFT_DATA_VALUE, | |
5272 | .size = NFT_DATA_VALUE_MAXLEN, | |
5273 | .len = set->klen, | |
5274 | }; | |
20a1452c | 5275 | |
341b6941 | 5276 | return nft_data_init(ctx, key, &desc, attr); |
20a1452c PNA |
5277 | } |
5278 | ||
fdb9c405 PNA |
5279 | static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, |
5280 | struct nft_data_desc *desc, | |
5281 | struct nft_data *data, | |
5282 | struct nlattr *attr) | |
5283 | { | |
7e6bc1f6 | 5284 | u32 dtype; |
fdb9c405 | 5285 | |
7e6bc1f6 PNA |
5286 | if (set->dtype == NFT_DATA_VERDICT) |
5287 | dtype = NFT_DATA_VERDICT; | |
5288 | else | |
5289 | dtype = NFT_DATA_VALUE; | |
5290 | ||
341b6941 PNA |
5291 | desc->type = dtype; |
5292 | desc->size = NFT_DATA_VALUE_MAXLEN; | |
5293 | desc->len = set->dlen; | |
f323ef3a | 5294 | desc->flags = NFT_DATA_DESC_SETELEM; |
fdb9c405 | 5295 | |
341b6941 | 5296 | return nft_data_init(ctx, data, desc, attr); |
fdb9c405 PNA |
5297 | } |
5298 | ||
aaa31047 PNA |
5299 | static void *nft_setelem_catchall_get(const struct net *net, |
5300 | const struct nft_set *set) | |
5301 | { | |
5302 | struct nft_set_elem_catchall *catchall; | |
5303 | u8 genmask = nft_genmask_cur(net); | |
5304 | struct nft_set_ext *ext; | |
5305 | void *priv = NULL; | |
5306 | ||
5307 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5308 | ext = nft_set_elem_ext(set, catchall->elem); | |
5309 | if (!nft_set_elem_active(ext, genmask) || | |
5310 | nft_set_elem_expired(ext)) | |
5311 | continue; | |
5312 | ||
5313 | priv = catchall->elem; | |
5314 | break; | |
5315 | } | |
5316 | ||
5317 | return priv; | |
5318 | } | |
5319 | ||
5320 | static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set, | |
5321 | struct nft_set_elem *elem, u32 flags) | |
5322 | { | |
5323 | void *priv; | |
5324 | ||
5325 | if (!(flags & NFT_SET_ELEM_CATCHALL)) { | |
5326 | priv = set->ops->get(ctx->net, set, elem, flags); | |
5327 | if (IS_ERR(priv)) | |
5328 | return PTR_ERR(priv); | |
5329 | } else { | |
5330 | priv = nft_setelem_catchall_get(ctx->net, set); | |
5331 | if (!priv) | |
5332 | return -ENOENT; | |
5333 | } | |
5334 | elem->priv = priv; | |
5335 | ||
5336 | return 0; | |
5337 | } | |
5338 | ||
ba0e4d99 PNA |
5339 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
5340 | const struct nlattr *attr) | |
5341 | { | |
5342 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
5343 | struct nft_set_elem elem; |
5344 | struct sk_buff *skb; | |
5345 | uint32_t flags = 0; | |
ba0e4d99 PNA |
5346 | int err; |
5347 | ||
8cb08174 JB |
5348 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5349 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
5350 | if (err < 0) |
5351 | return err; | |
5352 | ||
ba0e4d99 PNA |
5353 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5354 | if (err < 0) | |
5355 | return err; | |
5356 | ||
aaa31047 PNA |
5357 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
5358 | return -EINVAL; | |
5359 | ||
5360 | if (nla[NFTA_SET_ELEM_KEY]) { | |
5361 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
5362 | nla[NFTA_SET_ELEM_KEY]); | |
5363 | if (err < 0) | |
5364 | return err; | |
5365 | } | |
ba0e4d99 | 5366 | |
7b225d0b PNA |
5367 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5368 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5369 | nla[NFTA_SET_ELEM_KEY_END]); | |
5370 | if (err < 0) | |
5371 | return err; | |
5372 | } | |
5373 | ||
aaa31047 PNA |
5374 | err = nft_setelem_get(ctx, set, &elem, flags); |
5375 | if (err < 0) | |
5376 | return err; | |
ba0e4d99 PNA |
5377 | |
5378 | err = -ENOMEM; | |
d9adf22a | 5379 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 | 5380 | if (skb == NULL) |
ee921183 | 5381 | return err; |
ba0e4d99 PNA |
5382 | |
5383 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
5384 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
5385 | if (err < 0) | |
ee921183 | 5386 | goto err_fill_setelem; |
ba0e4d99 | 5387 | |
ee921183 | 5388 | return nfnetlink_unicast(skb, ctx->net, ctx->portid); |
ba0e4d99 | 5389 | |
ee921183 | 5390 | err_fill_setelem: |
ba0e4d99 | 5391 | kfree_skb(skb); |
ee921183 | 5392 | return err; |
ba0e4d99 PNA |
5393 | } |
5394 | ||
d9adf22a | 5395 | /* called with rcu_read_lock held */ |
797d4980 PNA |
5396 | static int nf_tables_getsetelem(struct sk_buff *skb, |
5397 | const struct nfnl_info *info, | |
5398 | const struct nlattr * const nla[]) | |
ba0e4d99 | 5399 | { |
797d4980 PNA |
5400 | struct netlink_ext_ack *extack = info->extack; |
5401 | u8 genmask = nft_genmask_cur(info->net); | |
e2b750d7 | 5402 | u8 family = info->nfmsg->nfgen_family; |
797d4980 | 5403 | struct net *net = info->net; |
e2b750d7 | 5404 | struct nft_table *table; |
ba0e4d99 PNA |
5405 | struct nft_set *set; |
5406 | struct nlattr *attr; | |
5407 | struct nft_ctx ctx; | |
5408 | int rem, err = 0; | |
5409 | ||
e2b750d7 PNA |
5410 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
5411 | genmask, NETLINK_CB(skb).portid); | |
5412 | if (IS_ERR(table)) { | |
5413 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
5414 | return PTR_ERR(table); | |
5415 | } | |
ba0e4d99 | 5416 | |
e2b750d7 | 5417 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
5418 | if (IS_ERR(set)) |
5419 | return PTR_ERR(set); | |
5420 | ||
e2b750d7 PNA |
5421 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5422 | ||
797d4980 | 5423 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
ba0e4d99 | 5424 | struct netlink_dump_control c = { |
90fd131a | 5425 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
5426 | .dump = nf_tables_dump_set, |
5427 | .done = nf_tables_dump_set_done, | |
d9adf22a | 5428 | .module = THIS_MODULE, |
ba0e4d99 | 5429 | }; |
90fd131a FW |
5430 | struct nft_set_dump_ctx dump_ctx = { |
5431 | .set = set, | |
5432 | .ctx = ctx, | |
5433 | }; | |
ba0e4d99 | 5434 | |
90fd131a | 5435 | c.data = &dump_ctx; |
797d4980 | 5436 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
ba0e4d99 PNA |
5437 | } |
5438 | ||
5439 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
5440 | return -EINVAL; | |
5441 | ||
5442 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
5443 | err = nft_get_set_elem(&ctx, set, attr); | |
b53c1166 PNA |
5444 | if (err < 0) { |
5445 | NL_SET_BAD_ATTR(extack, attr); | |
ba0e4d99 | 5446 | break; |
b53c1166 | 5447 | } |
ba0e4d99 PNA |
5448 | } |
5449 | ||
5450 | return err; | |
5451 | } | |
5452 | ||
25e94a99 PNA |
5453 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
5454 | const struct nft_set *set, | |
5455 | const struct nft_set_elem *elem, | |
6fb721cf | 5456 | int event) |
d60ce62f | 5457 | { |
0854db2a | 5458 | struct nftables_pernet *nft_net; |
128ad332 PNA |
5459 | struct net *net = ctx->net; |
5460 | u32 portid = ctx->portid; | |
d60ce62f | 5461 | struct sk_buff *skb; |
6fb721cf | 5462 | u16 flags = 0; |
d60ce62f AB |
5463 | int err; |
5464 | ||
128ad332 | 5465 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 5466 | return; |
d60ce62f | 5467 | |
d60ce62f AB |
5468 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
5469 | if (skb == NULL) | |
5470 | goto err; | |
5471 | ||
6fb721cf PNA |
5472 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
5473 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
5474 | ||
d60ce62f AB |
5475 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, |
5476 | set, elem); | |
5477 | if (err < 0) { | |
5478 | kfree_skb(skb); | |
5479 | goto err; | |
5480 | } | |
5481 | ||
d59d2f82 | 5482 | nft_net = nft_pernet(net); |
0854db2a | 5483 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 5484 | return; |
d60ce62f | 5485 | err: |
25e94a99 | 5486 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
5487 | } |
5488 | ||
60319eb1 PNA |
5489 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
5490 | int msg_type, | |
5491 | struct nft_set *set) | |
5492 | { | |
5493 | struct nft_trans *trans; | |
5494 | ||
5495 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
5496 | if (trans == NULL) | |
5497 | return NULL; | |
5498 | ||
5499 | nft_trans_elem_set(trans) = set; | |
5500 | return trans; | |
5501 | } | |
5502 | ||
a7fc9368 PNA |
5503 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
5504 | const struct nft_set *set, | |
5505 | const struct nlattr *attr) | |
5506 | { | |
5507 | struct nft_expr *expr; | |
5508 | int err; | |
5509 | ||
5510 | expr = nft_expr_init(ctx, attr); | |
5511 | if (IS_ERR(expr)) | |
5512 | return expr; | |
5513 | ||
5514 | err = -EOPNOTSUPP; | |
a7fc9368 PNA |
5515 | if (expr->ops->type->flags & NFT_EXPR_GC) { |
5516 | if (set->flags & NFT_SET_TIMEOUT) | |
5517 | goto err_set_elem_expr; | |
5518 | if (!set->ops->gc_init) | |
5519 | goto err_set_elem_expr; | |
5520 | set->ops->gc_init(set); | |
5521 | } | |
5522 | ||
5523 | return expr; | |
5524 | ||
5525 | err_set_elem_expr: | |
5526 | nft_expr_destroy(ctx, expr); | |
5527 | return ERR_PTR(err); | |
5528 | } | |
5529 | ||
34aae2c2 PNA |
5530 | static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len) |
5531 | { | |
5532 | len += nft_set_ext_types[id].len; | |
5533 | if (len > tmpl->ext_len[id] || | |
5534 | len > U8_MAX) | |
5535 | return -1; | |
5536 | ||
5537 | return 0; | |
5538 | } | |
5539 | ||
5540 | static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id, | |
5541 | void *to, const void *from, u32 len) | |
5542 | { | |
5543 | if (nft_set_ext_check(tmpl, id, len) < 0) | |
5544 | return -1; | |
5545 | ||
5546 | memcpy(to, from, len); | |
5547 | ||
5548 | return 0; | |
5549 | } | |
5550 | ||
22fe54d5 PM |
5551 | void *nft_set_elem_init(const struct nft_set *set, |
5552 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
5553 | const u32 *key, const u32 *key_end, |
5554 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
5555 | { |
5556 | struct nft_set_ext *ext; | |
5557 | void *elem; | |
5558 | ||
5559 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
5560 | if (elem == NULL) | |
34aae2c2 | 5561 | return ERR_PTR(-ENOMEM); |
fe2811eb PM |
5562 | |
5563 | ext = nft_set_elem_ext(set, elem); | |
5564 | nft_set_ext_init(ext, tmpl); | |
5565 | ||
34aae2c2 PNA |
5566 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
5567 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY, | |
5568 | nft_set_ext_key(ext), key, set->klen) < 0) | |
5569 | goto err_ext_check; | |
5570 | ||
5571 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && | |
5572 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END, | |
5573 | nft_set_ext_key_end(ext), key_end, set->klen) < 0) | |
5574 | goto err_ext_check; | |
5575 | ||
5576 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && | |
5577 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA, | |
5578 | nft_set_ext_data(ext), data, set->dlen) < 0) | |
5579 | goto err_ext_check; | |
5580 | ||
79ebb5bb LGL |
5581 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
5582 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
5583 | if (expiration == 0) | |
5584 | *nft_set_ext_expiration(ext) += timeout; | |
5585 | } | |
c3e1b005 PM |
5586 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
5587 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
5588 | |
5589 | return elem; | |
34aae2c2 PNA |
5590 | |
5591 | err_ext_check: | |
5592 | kfree(elem); | |
5593 | ||
5594 | return ERR_PTR(-EINVAL); | |
fe2811eb PM |
5595 | } |
5596 | ||
563125a7 PNA |
5597 | static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5598 | struct nft_expr *expr) | |
475beb9c PNA |
5599 | { |
5600 | if (expr->ops->destroy_clone) { | |
5601 | expr->ops->destroy_clone(ctx, expr); | |
5602 | module_put(expr->ops->type->owner); | |
5603 | } else { | |
5604 | nf_tables_expr_destroy(ctx, expr); | |
5605 | } | |
5606 | } | |
5607 | ||
563125a7 PNA |
5608 | static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5609 | struct nft_set_elem_expr *elem_expr) | |
5610 | { | |
5611 | struct nft_expr *expr; | |
5612 | u32 size; | |
5613 | ||
5614 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
5615 | __nft_set_elem_expr_destroy(ctx, expr); | |
5616 | } | |
5617 | ||
61f9e292 LZ |
5618 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
5619 | bool destroy_expr) | |
61edafbb PM |
5620 | { |
5621 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
5622 | struct nft_ctx ctx = { |
5623 | .net = read_pnet(&set->net), | |
5624 | .family = set->table->family, | |
5625 | }; | |
61edafbb | 5626 | |
59105446 | 5627 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 5628 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 5629 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
563125a7 | 5630 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c | 5631 | nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext)); |
371ebcbb | 5632 | |
8aeff920 PNA |
5633 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
5634 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
5635 | kfree(elem); |
5636 | } | |
5637 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
5638 | ||
f8bb7889 PNA |
5639 | /* Only called from commit path, nft_setelem_data_deactivate() already deals |
5640 | * with the refcounting from the preparation phase. | |
59105446 | 5641 | */ |
3453c927 PNA |
5642 | static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
5643 | const struct nft_set *set, void *elem) | |
59105446 PNA |
5644 | { |
5645 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
5646 | ||
563125a7 | 5647 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c PNA |
5648 | nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext)); |
5649 | ||
59105446 PNA |
5650 | kfree(elem); |
5651 | } | |
5652 | ||
fca05d4d PNA |
5653 | int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
5654 | struct nft_expr *expr_array[]) | |
8cfd9b0f PNA |
5655 | { |
5656 | struct nft_expr *expr; | |
5657 | int err, i, k; | |
5658 | ||
5659 | for (i = 0; i < set->num_exprs; i++) { | |
42193ffd | 5660 | expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT); |
8cfd9b0f PNA |
5661 | if (!expr) |
5662 | goto err_expr; | |
5663 | ||
5664 | err = nft_expr_clone(expr, set->exprs[i]); | |
5665 | if (err < 0) { | |
c485c35f | 5666 | kfree(expr); |
8cfd9b0f PNA |
5667 | goto err_expr; |
5668 | } | |
5669 | expr_array[i] = expr; | |
5670 | } | |
5671 | ||
5672 | return 0; | |
5673 | ||
5674 | err_expr: | |
161b838e CIK |
5675 | for (k = i - 1; k >= 0; k--) |
5676 | nft_expr_destroy(ctx, expr_array[k]); | |
8cfd9b0f PNA |
5677 | |
5678 | return -ENOMEM; | |
5679 | } | |
5680 | ||
4d8f9065 | 5681 | static int nft_set_elem_expr_setup(struct nft_ctx *ctx, |
34aae2c2 | 5682 | const struct nft_set_ext_tmpl *tmpl, |
4d8f9065 PNA |
5683 | const struct nft_set_ext *ext, |
5684 | struct nft_expr *expr_array[], | |
5685 | u32 num_exprs) | |
563125a7 PNA |
5686 | { |
5687 | struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext); | |
34aae2c2 | 5688 | u32 len = sizeof(struct nft_set_elem_expr); |
4d8f9065 PNA |
5689 | struct nft_expr *expr; |
5690 | int i, err; | |
5691 | ||
34aae2c2 PNA |
5692 | if (num_exprs == 0) |
5693 | return 0; | |
5694 | ||
5695 | for (i = 0; i < num_exprs; i++) | |
5696 | len += expr_array[i]->ops->size; | |
5697 | ||
5698 | if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0) | |
5699 | return -EINVAL; | |
5700 | ||
4d8f9065 PNA |
5701 | for (i = 0; i < num_exprs; i++) { |
5702 | expr = nft_setelem_expr_at(elem_expr, elem_expr->size); | |
5703 | err = nft_expr_clone(expr, expr_array[i]); | |
5704 | if (err < 0) | |
5705 | goto err_elem_expr_setup; | |
5706 | ||
5707 | elem_expr->size += expr_array[i]->ops->size; | |
5708 | nft_expr_destroy(ctx, expr_array[i]); | |
5709 | expr_array[i] = NULL; | |
5710 | } | |
5711 | ||
5712 | return 0; | |
5713 | ||
5714 | err_elem_expr_setup: | |
5715 | for (; i < num_exprs; i++) { | |
5716 | nft_expr_destroy(ctx, expr_array[i]); | |
5717 | expr_array[i] = NULL; | |
5718 | } | |
563125a7 | 5719 | |
4d8f9065 | 5720 | return -ENOMEM; |
563125a7 PNA |
5721 | } |
5722 | ||
aaa31047 PNA |
5723 | struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
5724 | const struct nft_set *set) | |
5725 | { | |
5726 | struct nft_set_elem_catchall *catchall; | |
5727 | u8 genmask = nft_genmask_cur(net); | |
5728 | struct nft_set_ext *ext; | |
5729 | ||
5730 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5731 | ext = nft_set_elem_ext(set, catchall->elem); | |
5732 | if (nft_set_elem_active(ext, genmask) && | |
5733 | !nft_set_elem_expired(ext)) | |
5734 | return ext; | |
5735 | } | |
5736 | ||
5737 | return NULL; | |
5738 | } | |
5739 | EXPORT_SYMBOL_GPL(nft_set_catchall_lookup); | |
5740 | ||
5741 | void *nft_set_catchall_gc(const struct nft_set *set) | |
5742 | { | |
5743 | struct nft_set_elem_catchall *catchall, *next; | |
5744 | struct nft_set_ext *ext; | |
5745 | void *elem = NULL; | |
5746 | ||
5747 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5748 | ext = nft_set_elem_ext(set, catchall->elem); | |
5749 | ||
5750 | if (!nft_set_elem_expired(ext) || | |
5751 | nft_set_elem_mark_busy(ext)) | |
5752 | continue; | |
5753 | ||
5754 | elem = catchall->elem; | |
5755 | list_del_rcu(&catchall->list); | |
5756 | kfree_rcu(catchall, rcu); | |
5757 | break; | |
5758 | } | |
5759 | ||
5760 | return elem; | |
5761 | } | |
5762 | EXPORT_SYMBOL_GPL(nft_set_catchall_gc); | |
5763 | ||
5764 | static int nft_setelem_catchall_insert(const struct net *net, | |
5765 | struct nft_set *set, | |
5766 | const struct nft_set_elem *elem, | |
5767 | struct nft_set_ext **pext) | |
5768 | { | |
5769 | struct nft_set_elem_catchall *catchall; | |
5770 | u8 genmask = nft_genmask_next(net); | |
5771 | struct nft_set_ext *ext; | |
5772 | ||
5773 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5774 | ext = nft_set_elem_ext(set, catchall->elem); | |
5775 | if (nft_set_elem_active(ext, genmask)) { | |
5776 | *pext = ext; | |
5777 | return -EEXIST; | |
5778 | } | |
5779 | } | |
5780 | ||
5781 | catchall = kmalloc(sizeof(*catchall), GFP_KERNEL); | |
5782 | if (!catchall) | |
5783 | return -ENOMEM; | |
5784 | ||
5785 | catchall->elem = elem->priv; | |
5786 | list_add_tail_rcu(&catchall->list, &set->catchall_list); | |
5787 | ||
5788 | return 0; | |
5789 | } | |
5790 | ||
5791 | static int nft_setelem_insert(const struct net *net, | |
5792 | struct nft_set *set, | |
5793 | const struct nft_set_elem *elem, | |
5794 | struct nft_set_ext **ext, unsigned int flags) | |
5795 | { | |
5796 | int ret; | |
5797 | ||
5798 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5799 | ret = nft_setelem_catchall_insert(net, set, elem, ext); | |
5800 | else | |
5801 | ret = set->ops->insert(net, set, elem, ext); | |
5802 | ||
5803 | return ret; | |
5804 | } | |
5805 | ||
5806 | static bool nft_setelem_is_catchall(const struct nft_set *set, | |
5807 | const struct nft_set_elem *elem) | |
5808 | { | |
5809 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5810 | ||
5811 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
5812 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL) | |
5813 | return true; | |
5814 | ||
5815 | return false; | |
5816 | } | |
5817 | ||
5818 | static void nft_setelem_activate(struct net *net, struct nft_set *set, | |
5819 | struct nft_set_elem *elem) | |
5820 | { | |
5821 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5822 | ||
5823 | if (nft_setelem_is_catchall(set, elem)) { | |
5824 | nft_set_elem_change_active(net, set, ext); | |
5825 | nft_set_elem_clear_busy(ext); | |
5826 | } else { | |
5827 | set->ops->activate(net, set, elem); | |
5828 | } | |
5829 | } | |
5830 | ||
5831 | static int nft_setelem_catchall_deactivate(const struct net *net, | |
5832 | struct nft_set *set, | |
5833 | struct nft_set_elem *elem) | |
5834 | { | |
5835 | struct nft_set_elem_catchall *catchall; | |
5836 | struct nft_set_ext *ext; | |
5837 | ||
5838 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5839 | ext = nft_set_elem_ext(set, catchall->elem); | |
5840 | if (!nft_is_active(net, ext) || | |
5841 | nft_set_elem_mark_busy(ext)) | |
5842 | continue; | |
5843 | ||
5844 | kfree(elem->priv); | |
5845 | elem->priv = catchall->elem; | |
5846 | nft_set_elem_change_active(net, set, ext); | |
5847 | return 0; | |
5848 | } | |
5849 | ||
5850 | return -ENOENT; | |
5851 | } | |
5852 | ||
5853 | static int __nft_setelem_deactivate(const struct net *net, | |
5854 | struct nft_set *set, | |
5855 | struct nft_set_elem *elem) | |
5856 | { | |
5857 | void *priv; | |
5858 | ||
5859 | priv = set->ops->deactivate(net, set, elem); | |
5860 | if (!priv) | |
5861 | return -ENOENT; | |
5862 | ||
5863 | kfree(elem->priv); | |
5864 | elem->priv = priv; | |
5865 | set->ndeact++; | |
5866 | ||
5867 | return 0; | |
5868 | } | |
5869 | ||
5870 | static int nft_setelem_deactivate(const struct net *net, | |
5871 | struct nft_set *set, | |
5872 | struct nft_set_elem *elem, u32 flags) | |
5873 | { | |
5874 | int ret; | |
5875 | ||
5876 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5877 | ret = nft_setelem_catchall_deactivate(net, set, elem); | |
5878 | else | |
5879 | ret = __nft_setelem_deactivate(net, set, elem); | |
5880 | ||
5881 | return ret; | |
5882 | } | |
5883 | ||
5884 | static void nft_setelem_catchall_remove(const struct net *net, | |
5885 | const struct nft_set *set, | |
5886 | const struct nft_set_elem *elem) | |
5887 | { | |
5888 | struct nft_set_elem_catchall *catchall, *next; | |
5889 | ||
5890 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5891 | if (catchall->elem == elem->priv) { | |
5892 | list_del_rcu(&catchall->list); | |
ae089831 | 5893 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
5894 | break; |
5895 | } | |
5896 | } | |
5897 | } | |
5898 | ||
5899 | static void nft_setelem_remove(const struct net *net, | |
5900 | const struct nft_set *set, | |
5901 | const struct nft_set_elem *elem) | |
5902 | { | |
5903 | if (nft_setelem_is_catchall(set, elem)) | |
5904 | nft_setelem_catchall_remove(net, set, elem); | |
5905 | else | |
5906 | set->ops->remove(net, set, elem); | |
5907 | } | |
5908 | ||
88cccd90 PNA |
5909 | static bool nft_setelem_valid_key_end(const struct nft_set *set, |
5910 | struct nlattr **nla, u32 flags) | |
5911 | { | |
5912 | if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) == | |
5913 | (NFT_SET_CONCAT | NFT_SET_INTERVAL)) { | |
5914 | if (flags & NFT_SET_ELEM_INTERVAL_END) | |
5915 | return false; | |
96df8360 PNA |
5916 | |
5917 | if (nla[NFTA_SET_ELEM_KEY_END] && | |
5918 | flags & NFT_SET_ELEM_CATCHALL) | |
88cccd90 PNA |
5919 | return false; |
5920 | } else { | |
5921 | if (nla[NFTA_SET_ELEM_KEY_END]) | |
5922 | return false; | |
5923 | } | |
5924 | ||
5925 | return true; | |
5926 | } | |
5927 | ||
60319eb1 | 5928 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 5929 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 | 5930 | { |
8cfd9b0f | 5931 | struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {}; |
20a69341 | 5932 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
8aeff920 | 5933 | u8 genmask = nft_genmask_next(ctx->net); |
664899e8 | 5934 | u32 flags = 0, size = 0, num_exprs = 0; |
fe2811eb | 5935 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 5936 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
5937 | struct nft_set_elem elem; |
5938 | struct nft_set_binding *binding; | |
8aeff920 | 5939 | struct nft_object *obj = NULL; |
68e942e8 | 5940 | struct nft_userdata *udata; |
20a1452c | 5941 | struct nft_data_desc desc; |
20a69341 | 5942 | enum nft_registers dreg; |
60319eb1 | 5943 | struct nft_trans *trans; |
c3e1b005 | 5944 | u64 timeout; |
79ebb5bb | 5945 | u64 expiration; |
8cfd9b0f | 5946 | int err, i; |
68e942e8 | 5947 | u8 ulen; |
20a69341 | 5948 | |
8cb08174 JB |
5949 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5950 | nft_set_elem_policy, NULL); | |
20a69341 PM |
5951 | if (err < 0) |
5952 | return err; | |
5953 | ||
fe2811eb PM |
5954 | nft_set_ext_prepare(&tmpl); |
5955 | ||
0e9091d6 PNA |
5956 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5957 | if (err < 0) | |
5958 | return err; | |
aaa31047 PNA |
5959 | |
5960 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
5961 | return -EINVAL; | |
5962 | ||
c39ba4de PNA |
5963 | if (flags != 0) { |
5964 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
5965 | if (err < 0) | |
5966 | return err; | |
5967 | } | |
20a69341 PM |
5968 | |
5969 | if (set->flags & NFT_SET_MAP) { | |
5970 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 5971 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
5972 | return -EINVAL; |
5973 | } else { | |
5974 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
5975 | return -EINVAL; | |
5976 | } | |
5977 | ||
5a2f3dc3 PNA |
5978 | if (set->flags & NFT_SET_OBJECT) { |
5979 | if (!nla[NFTA_SET_ELEM_OBJREF] && | |
5980 | !(flags & NFT_SET_ELEM_INTERVAL_END)) | |
5981 | return -EINVAL; | |
5982 | } else { | |
5983 | if (nla[NFTA_SET_ELEM_OBJREF]) | |
5984 | return -EINVAL; | |
5985 | } | |
5986 | ||
88cccd90 PNA |
5987 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
5988 | return -EINVAL; | |
5989 | ||
bffc124b PNA |
5990 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
5991 | (nla[NFTA_SET_ELEM_DATA] || | |
5992 | nla[NFTA_SET_ELEM_OBJREF] || | |
5993 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
5994 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
5995 | nla[NFTA_SET_ELEM_USERDATA] || | |
48b0ae04 | 5996 | nla[NFTA_SET_ELEM_EXPR] || |
4963674c | 5997 | nla[NFTA_SET_ELEM_KEY_END] || |
48b0ae04 | 5998 | nla[NFTA_SET_ELEM_EXPRESSIONS])) |
bffc124b PNA |
5999 | return -EINVAL; |
6000 | ||
c3e1b005 PM |
6001 | timeout = 0; |
6002 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
6003 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
6004 | return -EINVAL; | |
8e1102d5 FW |
6005 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
6006 | &timeout); | |
6007 | if (err) | |
6008 | return err; | |
33c7aba0 PNA |
6009 | } else if (set->flags & NFT_SET_TIMEOUT && |
6010 | !(flags & NFT_SET_ELEM_INTERVAL_END)) { | |
c3e1b005 PM |
6011 | timeout = set->timeout; |
6012 | } | |
6013 | ||
79ebb5bb LGL |
6014 | expiration = 0; |
6015 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
6016 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
6017 | return -EINVAL; | |
6018 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
6019 | &expiration); | |
6020 | if (err) | |
6021 | return err; | |
6022 | } | |
6023 | ||
8cfd9b0f PNA |
6024 | if (nla[NFTA_SET_ELEM_EXPR]) { |
6025 | struct nft_expr *expr; | |
6026 | ||
664899e8 | 6027 | if (set->num_exprs && set->num_exprs != 1) |
8cfd9b0f PNA |
6028 | return -EOPNOTSUPP; |
6029 | ||
40944452 PNA |
6030 | expr = nft_set_elem_expr_alloc(ctx, set, |
6031 | nla[NFTA_SET_ELEM_EXPR]); | |
6032 | if (IS_ERR(expr)) | |
6033 | return PTR_ERR(expr); | |
65038428 | 6034 | |
8cfd9b0f | 6035 | expr_array[0] = expr; |
664899e8 | 6036 | num_exprs = 1; |
65038428 | 6037 | |
664899e8 | 6038 | if (set->num_exprs && set->exprs[0]->ops != expr->ops) { |
8cfd9b0f | 6039 | err = -EOPNOTSUPP; |
65038428 | 6040 | goto err_set_elem_expr; |
8cfd9b0f | 6041 | } |
48b0ae04 PNA |
6042 | } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) { |
6043 | struct nft_expr *expr; | |
6044 | struct nlattr *tmp; | |
6045 | int left; | |
65038428 | 6046 | |
48b0ae04 PNA |
6047 | i = 0; |
6048 | nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) { | |
664899e8 PNA |
6049 | if (i == NFT_SET_EXPR_MAX || |
6050 | (set->num_exprs && set->num_exprs == i)) { | |
48b0ae04 PNA |
6051 | err = -E2BIG; |
6052 | goto err_set_elem_expr; | |
6053 | } | |
6054 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
6055 | err = -EINVAL; | |
6056 | goto err_set_elem_expr; | |
6057 | } | |
6058 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
6059 | if (IS_ERR(expr)) { | |
6060 | err = PTR_ERR(expr); | |
6061 | goto err_set_elem_expr; | |
6062 | } | |
6063 | expr_array[i] = expr; | |
664899e8 | 6064 | num_exprs++; |
48b0ae04 | 6065 | |
664899e8 | 6066 | if (set->num_exprs && expr->ops != set->exprs[i]->ops) { |
48b0ae04 PNA |
6067 | err = -EOPNOTSUPP; |
6068 | goto err_set_elem_expr; | |
6069 | } | |
6070 | i++; | |
6071 | } | |
664899e8 | 6072 | if (set->num_exprs && set->num_exprs != i) { |
48b0ae04 | 6073 | err = -EOPNOTSUPP; |
65038428 | 6074 | goto err_set_elem_expr; |
48b0ae04 | 6075 | } |
33c7aba0 PNA |
6076 | } else if (set->num_exprs > 0 && |
6077 | !(flags & NFT_SET_ELEM_INTERVAL_END)) { | |
8cfd9b0f PNA |
6078 | err = nft_set_elem_expr_clone(ctx, set, expr_array); |
6079 | if (err < 0) | |
6080 | goto err_set_elem_expr_clone; | |
664899e8 PNA |
6081 | |
6082 | num_exprs = set->num_exprs; | |
40944452 PNA |
6083 | } |
6084 | ||
aaa31047 PNA |
6085 | if (nla[NFTA_SET_ELEM_KEY]) { |
6086 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6087 | nla[NFTA_SET_ELEM_KEY]); | |
6088 | if (err < 0) | |
6089 | goto err_set_elem_expr; | |
20a69341 | 6090 | |
c39ba4de PNA |
6091 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6092 | if (err < 0) | |
6093 | goto err_parse_key; | |
aaa31047 | 6094 | } |
7b225d0b PNA |
6095 | |
6096 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
6097 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6098 | nla[NFTA_SET_ELEM_KEY_END]); | |
6099 | if (err < 0) | |
6100 | goto err_parse_key; | |
6101 | ||
c39ba4de PNA |
6102 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6103 | if (err < 0) | |
6104 | goto err_parse_key_end; | |
7b225d0b PNA |
6105 | } |
6106 | ||
c3e1b005 | 6107 | if (timeout > 0) { |
c39ba4de PNA |
6108 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); |
6109 | if (err < 0) | |
6110 | goto err_parse_key_end; | |
6111 | ||
6112 | if (timeout != set->timeout) { | |
6113 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); | |
6114 | if (err < 0) | |
6115 | goto err_parse_key_end; | |
6116 | } | |
c3e1b005 | 6117 | } |
fe2811eb | 6118 | |
664899e8 PNA |
6119 | if (num_exprs) { |
6120 | for (i = 0; i < num_exprs; i++) | |
563125a7 PNA |
6121 | size += expr_array[i]->ops->size; |
6122 | ||
c39ba4de PNA |
6123 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS, |
6124 | sizeof(struct nft_set_elem_expr) + size); | |
6125 | if (err < 0) | |
6126 | goto err_parse_key_end; | |
563125a7 | 6127 | } |
40944452 | 6128 | |
8aeff920 | 6129 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
4d44175a FW |
6130 | obj = nft_obj_lookup(ctx->net, ctx->table, |
6131 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 6132 | set->objtype, genmask); |
8aeff920 PNA |
6133 | if (IS_ERR(obj)) { |
6134 | err = PTR_ERR(obj); | |
7b225d0b | 6135 | goto err_parse_key_end; |
8aeff920 | 6136 | } |
c39ba4de PNA |
6137 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); |
6138 | if (err < 0) | |
6139 | goto err_parse_key_end; | |
8aeff920 PNA |
6140 | } |
6141 | ||
20a69341 | 6142 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
fdb9c405 PNA |
6143 | err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val, |
6144 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 | 6145 | if (err < 0) |
7b225d0b | 6146 | goto err_parse_key_end; |
20a69341 | 6147 | |
20a69341 PM |
6148 | dreg = nft_type_to_reg(set->dtype); |
6149 | list_for_each_entry(binding, &set->bindings, list) { | |
6150 | struct nft_ctx bind_ctx = { | |
58c78e10 | 6151 | .net = ctx->net, |
36596dad | 6152 | .family = ctx->family, |
20a69341 | 6153 | .table = ctx->table, |
7c95f6d8 | 6154 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
6155 | }; |
6156 | ||
11113e19 PM |
6157 | if (!(binding->flags & NFT_SET_MAP)) |
6158 | continue; | |
6159 | ||
1ec10212 | 6160 | err = nft_validate_register_store(&bind_ctx, dreg, |
fdb9c405 | 6161 | &elem.data.val, |
20a1452c | 6162 | desc.type, desc.len); |
20a69341 | 6163 | if (err < 0) |
7b225d0b | 6164 | goto err_parse_data; |
a654de8f | 6165 | |
20a1452c | 6166 | if (desc.type == NFT_DATA_VERDICT && |
fdb9c405 PNA |
6167 | (elem.data.val.verdict.code == NFT_GOTO || |
6168 | elem.data.val.verdict.code == NFT_JUMP)) | |
a654de8f PNA |
6169 | nft_validate_state_update(ctx->net, |
6170 | NFT_VALIDATE_NEED); | |
20a69341 | 6171 | } |
fe2811eb | 6172 | |
c39ba4de PNA |
6173 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
6174 | if (err < 0) | |
6175 | goto err_parse_data; | |
20a69341 PM |
6176 | } |
6177 | ||
68e942e8 PM |
6178 | /* The full maximum length of userdata can exceed the maximum |
6179 | * offset value (U8_MAX) for following extensions, therefor it | |
6180 | * must be the last extension added. | |
6181 | */ | |
6182 | ulen = 0; | |
6183 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
6184 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
c39ba4de PNA |
6185 | if (ulen > 0) { |
6186 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
6187 | ulen); | |
6188 | if (err < 0) | |
6189 | goto err_parse_data; | |
6190 | } | |
68e942e8 PM |
6191 | } |
6192 | ||
7b225d0b | 6193 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
fdb9c405 | 6194 | elem.key_end.val.data, elem.data.val.data, |
33758c89 | 6195 | timeout, expiration, GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6196 | if (IS_ERR(elem.priv)) { |
6197 | err = PTR_ERR(elem.priv); | |
7b225d0b | 6198 | goto err_parse_data; |
34aae2c2 | 6199 | } |
fe2811eb PM |
6200 | |
6201 | ext = nft_set_elem_ext(set, elem.priv); | |
6202 | if (flags) | |
6203 | *nft_set_ext_flags(ext) = flags; | |
34aae2c2 | 6204 | |
68e942e8 | 6205 | if (ulen > 0) { |
34aae2c2 PNA |
6206 | if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) { |
6207 | err = -EINVAL; | |
6208 | goto err_elem_userdata; | |
6209 | } | |
68e942e8 PM |
6210 | udata = nft_set_ext_userdata(ext); |
6211 | udata->len = ulen - 1; | |
6212 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
6213 | } | |
8aeff920 PNA |
6214 | if (obj) { |
6215 | *nft_set_ext_obj(ext) = obj; | |
6216 | obj->use++; | |
6217 | } | |
34aae2c2 | 6218 | err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs); |
4d8f9065 | 6219 | if (err < 0) |
34aae2c2 | 6220 | goto err_elem_free; |
fe2811eb | 6221 | |
60319eb1 | 6222 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
4d8f9065 PNA |
6223 | if (trans == NULL) { |
6224 | err = -ENOMEM; | |
34aae2c2 | 6225 | goto err_elem_free; |
4d8f9065 | 6226 | } |
60319eb1 | 6227 | |
69086658 | 6228 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
aaa31047 PNA |
6229 | |
6230 | err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags); | |
c016c7e4 PNA |
6231 | if (err) { |
6232 | if (err == -EEXIST) { | |
9744a6fc PNA |
6233 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
6234 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
6235 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
77a92189 | 6236 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) |
7b225d0b | 6237 | goto err_element_clash; |
8aeff920 PNA |
6238 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
6239 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
6240 | memcmp(nft_set_ext_data(ext), | |
6241 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
6242 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
6243 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
6244 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
77a92189 | 6245 | goto err_element_clash; |
c016c7e4 PNA |
6246 | else if (!(nlmsg_flags & NLM_F_EXCL)) |
6247 | err = 0; | |
8c2d45b2 PNA |
6248 | } else if (err == -ENOTEMPTY) { |
6249 | /* ENOTEMPTY reports overlapping between this element | |
6250 | * and an existing one. | |
6251 | */ | |
6252 | err = -EEXIST; | |
c016c7e4 | 6253 | } |
7b225d0b | 6254 | goto err_element_clash; |
c016c7e4 | 6255 | } |
20a69341 | 6256 | |
aaa31047 | 6257 | if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size && |
35d0ac90 PNA |
6258 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { |
6259 | err = -ENFILE; | |
7b225d0b | 6260 | goto err_set_full; |
35d0ac90 PNA |
6261 | } |
6262 | ||
60319eb1 | 6263 | nft_trans_elem(trans) = elem; |
0854db2a | 6264 | nft_trans_commit_list_add_tail(ctx->net, trans); |
20a69341 PM |
6265 | return 0; |
6266 | ||
7b225d0b | 6267 | err_set_full: |
aaa31047 | 6268 | nft_setelem_remove(ctx->net, set, &elem); |
7b225d0b | 6269 | err_element_clash: |
60319eb1 | 6270 | kfree(trans); |
34aae2c2 | 6271 | err_elem_free: |
b91d9036 TY |
6272 | if (obj) |
6273 | obj->use--; | |
34aae2c2 | 6274 | err_elem_userdata: |
475beb9c | 6275 | nf_tables_set_elem_destroy(ctx, set, elem.priv); |
7b225d0b | 6276 | err_parse_data: |
20a69341 | 6277 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
fdb9c405 | 6278 | nft_data_release(&elem.data.val, desc.type); |
7b225d0b PNA |
6279 | err_parse_key_end: |
6280 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
6281 | err_parse_key: | |
20a1452c | 6282 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
40944452 | 6283 | err_set_elem_expr: |
664899e8 | 6284 | for (i = 0; i < num_exprs && expr_array[i]; i++) |
8cfd9b0f PNA |
6285 | nft_expr_destroy(ctx, expr_array[i]); |
6286 | err_set_elem_expr_clone: | |
20a69341 PM |
6287 | return err; |
6288 | } | |
6289 | ||
7dab8ee3 PNA |
6290 | static int nf_tables_newsetelem(struct sk_buff *skb, |
6291 | const struct nfnl_info *info, | |
6292 | const struct nlattr * const nla[]) | |
20a69341 | 6293 | { |
7dab8ee3 PNA |
6294 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
6295 | struct netlink_ext_ack *extack = info->extack; | |
6296 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6297 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6298 | struct net *net = info->net; |
20a69341 | 6299 | const struct nlattr *attr; |
e2b750d7 | 6300 | struct nft_table *table; |
20a69341 PM |
6301 | struct nft_set *set; |
6302 | struct nft_ctx ctx; | |
a654de8f | 6303 | int rem, err; |
20a69341 | 6304 | |
7d5570ca PNA |
6305 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
6306 | return -EINVAL; | |
6307 | ||
e2b750d7 PNA |
6308 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6309 | genmask, NETLINK_CB(skb).portid); | |
6310 | if (IS_ERR(table)) { | |
6311 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6312 | return PTR_ERR(table); | |
6313 | } | |
20a69341 | 6314 | |
e2b750d7 | 6315 | set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET], |
a3073c17 PNA |
6316 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); |
6317 | if (IS_ERR(set)) | |
6318 | return PTR_ERR(set); | |
958bee14 | 6319 | |
20a69341 PM |
6320 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
6321 | return -EBUSY; | |
6322 | ||
e2b750d7 PNA |
6323 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6324 | ||
20a69341 | 6325 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7dab8ee3 | 6326 | err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags); |
b53c1166 PNA |
6327 | if (err < 0) { |
6328 | NL_SET_BAD_ATTR(extack, attr); | |
a654de8f | 6329 | return err; |
b53c1166 | 6330 | } |
20a69341 | 6331 | } |
a654de8f | 6332 | |
0854db2a | 6333 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
e2b750d7 | 6334 | return nft_table_validate(net, table); |
a654de8f PNA |
6335 | |
6336 | return 0; | |
20a69341 PM |
6337 | } |
6338 | ||
59105446 PNA |
6339 | /** |
6340 | * nft_data_hold - hold a nft_data item | |
6341 | * | |
6342 | * @data: struct nft_data to release | |
6343 | * @type: type of data | |
6344 | * | |
6345 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
6346 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
6347 | * NFT_GOTO verdicts. This function must be called on active data objects | |
6348 | * from the second phase of the commit protocol. | |
6349 | */ | |
bb7b40ae | 6350 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 | 6351 | { |
d0e2c7de PNA |
6352 | struct nft_chain *chain; |
6353 | struct nft_rule *rule; | |
6354 | ||
59105446 PNA |
6355 | if (type == NFT_DATA_VERDICT) { |
6356 | switch (data->verdict.code) { | |
6357 | case NFT_JUMP: | |
6358 | case NFT_GOTO: | |
d0e2c7de PNA |
6359 | chain = data->verdict.chain; |
6360 | chain->use++; | |
6361 | ||
6362 | if (!nft_chain_is_bound(chain)) | |
6363 | break; | |
6364 | ||
6365 | chain->table->use++; | |
6366 | list_for_each_entry(rule, &chain->rules, list) | |
6367 | chain->use++; | |
6368 | ||
6369 | nft_chain_add(chain->table, chain); | |
59105446 PNA |
6370 | break; |
6371 | } | |
6372 | } | |
6373 | } | |
6374 | ||
f8bb7889 PNA |
6375 | static void nft_setelem_data_activate(const struct net *net, |
6376 | const struct nft_set *set, | |
6377 | struct nft_set_elem *elem) | |
59105446 PNA |
6378 | { |
6379 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6380 | ||
6381 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6382 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
6383 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6384 | (*nft_set_ext_obj(ext))->use++; | |
6385 | } | |
6386 | ||
f8bb7889 PNA |
6387 | static void nft_setelem_data_deactivate(const struct net *net, |
6388 | const struct nft_set *set, | |
6389 | struct nft_set_elem *elem) | |
59105446 PNA |
6390 | { |
6391 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6392 | ||
6393 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6394 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
6395 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6396 | (*nft_set_ext_obj(ext))->use--; | |
6397 | } | |
6398 | ||
60319eb1 | 6399 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
6400 | const struct nlattr *attr) |
6401 | { | |
6402 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 6403 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 6404 | struct nft_set_elem elem; |
3971ca14 | 6405 | struct nft_set_ext *ext; |
60319eb1 | 6406 | struct nft_trans *trans; |
3971ca14 | 6407 | u32 flags = 0; |
20a69341 PM |
6408 | int err; |
6409 | ||
8cb08174 JB |
6410 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6411 | nft_set_elem_policy, NULL); | |
20a69341 | 6412 | if (err < 0) |
20a1452c | 6413 | return err; |
20a69341 | 6414 | |
aaa31047 PNA |
6415 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6416 | if (err < 0) | |
6417 | return err; | |
6418 | ||
6419 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
20a1452c | 6420 | return -EINVAL; |
20a69341 | 6421 | |
88cccd90 PNA |
6422 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6423 | return -EINVAL; | |
6424 | ||
3971ca14 PNA |
6425 | nft_set_ext_prepare(&tmpl); |
6426 | ||
c39ba4de PNA |
6427 | if (flags != 0) { |
6428 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6429 | if (err < 0) | |
6430 | return err; | |
6431 | } | |
3971ca14 | 6432 | |
aaa31047 PNA |
6433 | if (nla[NFTA_SET_ELEM_KEY]) { |
6434 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6435 | nla[NFTA_SET_ELEM_KEY]); | |
6436 | if (err < 0) | |
6437 | return err; | |
20a69341 | 6438 | |
c39ba4de PNA |
6439 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6440 | if (err < 0) | |
6441 | goto fail_elem; | |
aaa31047 | 6442 | } |
3971ca14 | 6443 | |
7b225d0b PNA |
6444 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
6445 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6446 | nla[NFTA_SET_ELEM_KEY_END]); | |
6447 | if (err < 0) | |
c39ba4de | 6448 | goto fail_elem; |
7b225d0b | 6449 | |
c39ba4de PNA |
6450 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6451 | if (err < 0) | |
6452 | goto fail_elem_key_end; | |
7b225d0b PNA |
6453 | } |
6454 | ||
3971ca14 | 6455 | err = -ENOMEM; |
7b225d0b PNA |
6456 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
6457 | elem.key_end.val.data, NULL, 0, 0, | |
33758c89 | 6458 | GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6459 | if (IS_ERR(elem.priv)) { |
6460 | err = PTR_ERR(elem.priv); | |
c39ba4de | 6461 | goto fail_elem_key_end; |
34aae2c2 | 6462 | } |
3971ca14 PNA |
6463 | |
6464 | ext = nft_set_elem_ext(set, elem.priv); | |
6465 | if (flags) | |
6466 | *nft_set_ext_flags(ext) = flags; | |
6467 | ||
60319eb1 | 6468 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
6469 | if (trans == NULL) |
6470 | goto fail_trans; | |
20a69341 | 6471 | |
aaa31047 PNA |
6472 | err = nft_setelem_deactivate(ctx->net, set, &elem, flags); |
6473 | if (err < 0) | |
20a1452c | 6474 | goto fail_ops; |
cc02e457 | 6475 | |
f8bb7889 | 6476 | nft_setelem_data_deactivate(ctx->net, set, &elem); |
59105446 | 6477 | |
60319eb1 | 6478 | nft_trans_elem(trans) = elem; |
0854db2a | 6479 | nft_trans_commit_list_add_tail(ctx->net, trans); |
0dc13625 | 6480 | return 0; |
cc02e457 | 6481 | |
20a1452c | 6482 | fail_ops: |
cc02e457 | 6483 | kfree(trans); |
20a1452c | 6484 | fail_trans: |
3971ca14 | 6485 | kfree(elem.priv); |
c39ba4de PNA |
6486 | fail_elem_key_end: |
6487 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
20a1452c PNA |
6488 | fail_elem: |
6489 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
6490 | return err; |
6491 | } | |
6492 | ||
e6ba7cb6 PNA |
6493 | static int nft_setelem_flush(const struct nft_ctx *ctx, |
6494 | struct nft_set *set, | |
6495 | const struct nft_set_iter *iter, | |
6496 | struct nft_set_elem *elem) | |
8411b644 PNA |
6497 | { |
6498 | struct nft_trans *trans; | |
6499 | int err; | |
6500 | ||
6501 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6502 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
6503 | if (!trans) | |
6504 | return -ENOMEM; | |
6505 | ||
1ba1c414 | 6506 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
6507 | err = -ENOENT; |
6508 | goto err1; | |
6509 | } | |
b2c11e4b | 6510 | set->ndeact++; |
8411b644 | 6511 | |
f8bb7889 | 6512 | nft_setelem_data_deactivate(ctx->net, set, elem); |
de70185d PNA |
6513 | nft_trans_elem_set(trans) = set; |
6514 | nft_trans_elem(trans) = *elem; | |
0854db2a | 6515 | nft_trans_commit_list_add_tail(ctx->net, trans); |
8411b644 PNA |
6516 | |
6517 | return 0; | |
6518 | err1: | |
6519 | kfree(trans); | |
6520 | return err; | |
6521 | } | |
6522 | ||
aaa31047 PNA |
6523 | static int __nft_set_catchall_flush(const struct nft_ctx *ctx, |
6524 | struct nft_set *set, | |
6525 | struct nft_set_elem *elem) | |
6526 | { | |
6527 | struct nft_trans *trans; | |
6528 | ||
6529 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6530 | sizeof(struct nft_trans_elem), GFP_KERNEL); | |
6531 | if (!trans) | |
6532 | return -ENOMEM; | |
6533 | ||
6534 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
6535 | nft_trans_elem_set(trans) = set; | |
6536 | nft_trans_elem(trans) = *elem; | |
6537 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
6538 | ||
6539 | return 0; | |
6540 | } | |
6541 | ||
6542 | static int nft_set_catchall_flush(const struct nft_ctx *ctx, | |
6543 | struct nft_set *set) | |
6544 | { | |
6545 | u8 genmask = nft_genmask_next(ctx->net); | |
6546 | struct nft_set_elem_catchall *catchall; | |
6547 | struct nft_set_elem elem; | |
6548 | struct nft_set_ext *ext; | |
6549 | int ret = 0; | |
6550 | ||
6551 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
6552 | ext = nft_set_elem_ext(set, catchall->elem); | |
6553 | if (!nft_set_elem_active(ext, genmask) || | |
6554 | nft_set_elem_mark_busy(ext)) | |
6555 | continue; | |
6556 | ||
6557 | elem.priv = catchall->elem; | |
6558 | ret = __nft_set_catchall_flush(ctx, set, &elem); | |
6559 | if (ret < 0) | |
6560 | break; | |
6561 | } | |
6562 | ||
6563 | return ret; | |
6564 | } | |
6565 | ||
e6ba7cb6 PNA |
6566 | static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) |
6567 | { | |
6568 | struct nft_set_iter iter = { | |
6569 | .genmask = genmask, | |
6570 | .fn = nft_setelem_flush, | |
6571 | }; | |
6572 | ||
6573 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
6574 | if (!iter.err) |
6575 | iter.err = nft_set_catchall_flush(ctx, set); | |
e6ba7cb6 PNA |
6576 | |
6577 | return iter.err; | |
6578 | } | |
6579 | ||
7dab8ee3 PNA |
6580 | static int nf_tables_delsetelem(struct sk_buff *skb, |
6581 | const struct nfnl_info *info, | |
6582 | const struct nlattr * const nla[]) | |
20a69341 | 6583 | { |
7dab8ee3 PNA |
6584 | struct netlink_ext_ack *extack = info->extack; |
6585 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6586 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6587 | struct net *net = info->net; |
20a69341 | 6588 | const struct nlattr *attr; |
e2b750d7 | 6589 | struct nft_table *table; |
20a69341 PM |
6590 | struct nft_set *set; |
6591 | struct nft_ctx ctx; | |
60319eb1 | 6592 | int rem, err = 0; |
20a69341 | 6593 | |
e2b750d7 PNA |
6594 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6595 | genmask, NETLINK_CB(skb).portid); | |
6596 | if (IS_ERR(table)) { | |
6597 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6598 | return PTR_ERR(table); | |
6599 | } | |
20a69341 | 6600 | |
e2b750d7 | 6601 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
6602 | if (IS_ERR(set)) |
6603 | return PTR_ERR(set); | |
6604 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
6605 | return -EBUSY; | |
6606 | ||
e2b750d7 PNA |
6607 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6608 | ||
e6ba7cb6 PNA |
6609 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) |
6610 | return nft_set_flush(&ctx, set, genmask); | |
8411b644 | 6611 | |
20a69341 PM |
6612 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
6613 | err = nft_del_setelem(&ctx, set, attr); | |
b53c1166 PNA |
6614 | if (err < 0) { |
6615 | NL_SET_BAD_ATTR(extack, attr); | |
60319eb1 | 6616 | break; |
b53c1166 | 6617 | } |
20a69341 | 6618 | } |
60319eb1 | 6619 | return err; |
20a69341 PM |
6620 | } |
6621 | ||
cfed7e1b PM |
6622 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
6623 | { | |
6624 | struct nft_set_gc_batch *gcb; | |
6625 | unsigned int i; | |
6626 | ||
6627 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
6628 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 6629 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
6630 | kfree(gcb); |
6631 | } | |
cfed7e1b PM |
6632 | |
6633 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
6634 | gfp_t gfp) | |
6635 | { | |
6636 | struct nft_set_gc_batch *gcb; | |
6637 | ||
6638 | gcb = kzalloc(sizeof(*gcb), gfp); | |
6639 | if (gcb == NULL) | |
6640 | return gcb; | |
6641 | gcb->head.set = set; | |
6642 | return gcb; | |
6643 | } | |
cfed7e1b | 6644 | |
e5009240 PNA |
6645 | /* |
6646 | * Stateful objects | |
6647 | */ | |
6648 | ||
6649 | /** | |
6650 | * nft_register_obj- register nf_tables stateful object type | |
3db86c39 | 6651 | * @obj_type: object type |
e5009240 PNA |
6652 | * |
6653 | * Registers the object type for use with nf_tables. Returns zero on | |
6654 | * success or a negative errno code otherwise. | |
6655 | */ | |
6656 | int nft_register_obj(struct nft_object_type *obj_type) | |
6657 | { | |
6658 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
6659 | return -EINVAL; | |
6660 | ||
6661 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6662 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
6663 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6664 | return 0; | |
6665 | } | |
6666 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
6667 | ||
6668 | /** | |
6669 | * nft_unregister_obj - unregister nf_tables object type | |
3db86c39 | 6670 | * @obj_type: object type |
e5009240 PNA |
6671 | * |
6672 | * Unregisters the object type for use with nf_tables. | |
6673 | */ | |
6674 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
6675 | { | |
6676 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6677 | list_del_rcu(&obj_type->list); | |
6678 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6679 | } | |
6680 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
6681 | ||
4d44175a FW |
6682 | struct nft_object *nft_obj_lookup(const struct net *net, |
6683 | const struct nft_table *table, | |
cac20fcd PNA |
6684 | const struct nlattr *nla, u32 objtype, |
6685 | u8 genmask) | |
e5009240 | 6686 | { |
4d44175a FW |
6687 | struct nft_object_hash_key k = { .table = table }; |
6688 | char search[NFT_OBJ_MAXNAMELEN]; | |
6689 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
6690 | struct nft_object *obj; |
6691 | ||
872f6903 | 6692 | nla_strscpy(search, nla, sizeof(search)); |
4d44175a FW |
6693 | k.name = search; |
6694 | ||
6695 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
6696 | !lockdep_commit_lock_is_held(net)); | |
6697 | ||
6698 | rcu_read_lock(); | |
6699 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
6700 | if (!list) | |
6701 | goto out; | |
6702 | ||
6703 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
6704 | if (objtype == obj->ops->type->type && | |
6705 | nft_active_genmask(obj, genmask)) { | |
6706 | rcu_read_unlock(); | |
e5009240 | 6707 | return obj; |
4d44175a | 6708 | } |
e5009240 | 6709 | } |
4d44175a FW |
6710 | out: |
6711 | rcu_read_unlock(); | |
e5009240 PNA |
6712 | return ERR_PTR(-ENOENT); |
6713 | } | |
cac20fcd | 6714 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 6715 | |
cac20fcd PNA |
6716 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
6717 | const struct nlattr *nla, | |
6718 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
6719 | { |
6720 | struct nft_object *obj; | |
6721 | ||
6722 | list_for_each_entry(obj, &table->objects, list) { | |
6723 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
6724 | objtype == obj->ops->type->type && | |
6725 | nft_active_genmask(obj, genmask)) | |
6726 | return obj; | |
6727 | } | |
6728 | return ERR_PTR(-ENOENT); | |
6729 | } | |
6730 | ||
e5009240 | 6731 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
6732 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
6733 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
6734 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
6735 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
6736 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
6737 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 6738 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
b131c964 JGG |
6739 | [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY, |
6740 | .len = NFT_USERDATA_MAXLEN }, | |
e5009240 PNA |
6741 | }; |
6742 | ||
84fba055 FW |
6743 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
6744 | const struct nft_object_type *type, | |
e5009240 PNA |
6745 | const struct nlattr *attr) |
6746 | { | |
5b4c6e38 | 6747 | struct nlattr **tb; |
dfc46034 | 6748 | const struct nft_object_ops *ops; |
e5009240 | 6749 | struct nft_object *obj; |
5b4c6e38 GS |
6750 | int err = -ENOMEM; |
6751 | ||
6752 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
6753 | if (!tb) | |
6754 | goto err1; | |
e5009240 PNA |
6755 | |
6756 | if (attr) { | |
8cb08174 JB |
6757 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
6758 | type->policy, NULL); | |
e5009240 | 6759 | if (err < 0) |
5b4c6e38 | 6760 | goto err2; |
e5009240 PNA |
6761 | } else { |
6762 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
6763 | } | |
6764 | ||
dfc46034 PBG |
6765 | if (type->select_ops) { |
6766 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
6767 | if (IS_ERR(ops)) { | |
6768 | err = PTR_ERR(ops); | |
5b4c6e38 | 6769 | goto err2; |
dfc46034 PBG |
6770 | } |
6771 | } else { | |
6772 | ops = type->ops; | |
6773 | } | |
6774 | ||
e5009240 | 6775 | err = -ENOMEM; |
33758c89 | 6776 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT); |
5b4c6e38 GS |
6777 | if (!obj) |
6778 | goto err2; | |
e5009240 | 6779 | |
dfc46034 | 6780 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 6781 | if (err < 0) |
5b4c6e38 | 6782 | goto err3; |
e5009240 | 6783 | |
dfc46034 PBG |
6784 | obj->ops = ops; |
6785 | ||
5b4c6e38 | 6786 | kfree(tb); |
e5009240 | 6787 | return obj; |
5b4c6e38 | 6788 | err3: |
e5009240 | 6789 | kfree(obj); |
5b4c6e38 GS |
6790 | err2: |
6791 | kfree(tb); | |
e5009240 PNA |
6792 | err1: |
6793 | return ERR_PTR(err); | |
6794 | } | |
6795 | ||
6796 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 6797 | struct nft_object *obj, bool reset) |
e5009240 PNA |
6798 | { |
6799 | struct nlattr *nest; | |
6800 | ||
ae0be8de | 6801 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
6802 | if (!nest) |
6803 | goto nla_put_failure; | |
dfc46034 | 6804 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
6805 | goto nla_put_failure; |
6806 | nla_nest_end(skb, nest); | |
6807 | return 0; | |
6808 | ||
6809 | nla_put_failure: | |
6810 | return -1; | |
6811 | } | |
6812 | ||
6813 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
6814 | { | |
6815 | const struct nft_object_type *type; | |
6816 | ||
6817 | list_for_each_entry(type, &nf_tables_objects, list) { | |
6818 | if (objtype == type->type) | |
6819 | return type; | |
6820 | } | |
6821 | return NULL; | |
6822 | } | |
6823 | ||
452238e8 FW |
6824 | static const struct nft_object_type * |
6825 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
6826 | { |
6827 | const struct nft_object_type *type; | |
6828 | ||
6829 | type = __nft_obj_type_get(objtype); | |
6830 | if (type != NULL && try_module_get(type->owner)) | |
6831 | return type; | |
6832 | ||
f102d66b | 6833 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
6834 | #ifdef CONFIG_MODULES |
6835 | if (type == NULL) { | |
eb014de4 | 6836 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
6837 | return ERR_PTR(-EAGAIN); |
6838 | } | |
6839 | #endif | |
6840 | return ERR_PTR(-ENOENT); | |
6841 | } | |
6842 | ||
d62d0ba9 FFM |
6843 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
6844 | const struct nft_object_type *type, | |
6845 | const struct nlattr *attr, | |
6846 | struct nft_object *obj) | |
6847 | { | |
6848 | struct nft_object *newobj; | |
6849 | struct nft_trans *trans; | |
dad3bdee FW |
6850 | int err = -ENOMEM; |
6851 | ||
6852 | if (!try_module_get(type->owner)) | |
6853 | return -ENOENT; | |
d62d0ba9 FFM |
6854 | |
6855 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
6856 | sizeof(struct nft_trans_obj)); | |
6857 | if (!trans) | |
dad3bdee | 6858 | goto err_trans; |
d62d0ba9 FFM |
6859 | |
6860 | newobj = nft_obj_init(ctx, type, attr); | |
6861 | if (IS_ERR(newobj)) { | |
6862 | err = PTR_ERR(newobj); | |
b74ae961 | 6863 | goto err_free_trans; |
d62d0ba9 FFM |
6864 | } |
6865 | ||
6866 | nft_trans_obj(trans) = obj; | |
6867 | nft_trans_obj_update(trans) = true; | |
6868 | nft_trans_obj_newobj(trans) = newobj; | |
0854db2a | 6869 | nft_trans_commit_list_add_tail(ctx->net, trans); |
d62d0ba9 FFM |
6870 | |
6871 | return 0; | |
b74ae961 DC |
6872 | |
6873 | err_free_trans: | |
d62d0ba9 | 6874 | kfree(trans); |
dad3bdee FW |
6875 | err_trans: |
6876 | module_put(type->owner); | |
d62d0ba9 FFM |
6877 | return err; |
6878 | } | |
6879 | ||
7dab8ee3 PNA |
6880 | static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info, |
6881 | const struct nlattr * const nla[]) | |
e5009240 | 6882 | { |
7dab8ee3 PNA |
6883 | struct netlink_ext_ack *extack = info->extack; |
6884 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 6885 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 6886 | const struct nft_object_type *type; |
7dab8ee3 | 6887 | struct net *net = info->net; |
e5009240 PNA |
6888 | struct nft_table *table; |
6889 | struct nft_object *obj; | |
6890 | struct nft_ctx ctx; | |
6891 | u32 objtype; | |
6892 | int err; | |
6893 | ||
6894 | if (!nla[NFTA_OBJ_TYPE] || | |
6895 | !nla[NFTA_OBJ_NAME] || | |
6896 | !nla[NFTA_OBJ_DATA]) | |
6897 | return -EINVAL; | |
6898 | ||
6001a930 PNA |
6899 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
6900 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
6901 | if (IS_ERR(table)) { |
6902 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 6903 | return PTR_ERR(table); |
36dd1bcc | 6904 | } |
e5009240 PNA |
6905 | |
6906 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 6907 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
6908 | if (IS_ERR(obj)) { |
6909 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
6910 | if (err != -ENOENT) { |
6911 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 6912 | return err; |
36dd1bcc | 6913 | } |
1a28ad74 | 6914 | } else { |
7dab8ee3 | 6915 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 6916 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); |
e5009240 | 6917 | return -EEXIST; |
36dd1bcc | 6918 | } |
7dab8ee3 | 6919 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
d62d0ba9 FFM |
6920 | return -EOPNOTSUPP; |
6921 | ||
fd57d0cb | 6922 | type = __nft_obj_type_get(objtype); |
7dab8ee3 | 6923 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
d62d0ba9 FFM |
6924 | |
6925 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
6926 | } |
6927 | ||
7dab8ee3 | 6928 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 | 6929 | |
452238e8 | 6930 | type = nft_obj_type_get(net, objtype); |
e5009240 PNA |
6931 | if (IS_ERR(type)) |
6932 | return PTR_ERR(type); | |
6933 | ||
84fba055 | 6934 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
6935 | if (IS_ERR(obj)) { |
6936 | err = PTR_ERR(obj); | |
b131c964 | 6937 | goto err_init; |
e5009240 | 6938 | } |
d152159b | 6939 | obj->key.table = table; |
3ecbfd65 HS |
6940 | obj->handle = nf_tables_alloc_handle(table); |
6941 | ||
33758c89 | 6942 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT); |
d152159b | 6943 | if (!obj->key.name) { |
61509575 | 6944 | err = -ENOMEM; |
b131c964 JGG |
6945 | goto err_strdup; |
6946 | } | |
6947 | ||
6948 | if (nla[NFTA_OBJ_USERDATA]) { | |
85db827a | 6949 | obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL); |
b131c964 JGG |
6950 | if (obj->udata == NULL) |
6951 | goto err_userdata; | |
6952 | ||
85db827a | 6953 | obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]); |
61509575 | 6954 | } |
e5009240 PNA |
6955 | |
6956 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
6957 | if (err < 0) | |
b131c964 | 6958 | goto err_trans; |
e5009240 | 6959 | |
4d44175a FW |
6960 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
6961 | nft_objname_ht_params); | |
6962 | if (err < 0) | |
b131c964 | 6963 | goto err_obj_ht; |
4d44175a | 6964 | |
e5009240 PNA |
6965 | list_add_tail_rcu(&obj->list, &table->objects); |
6966 | table->use++; | |
6967 | return 0; | |
b131c964 | 6968 | err_obj_ht: |
4d44175a FW |
6969 | /* queued in transaction log */ |
6970 | INIT_LIST_HEAD(&obj->list); | |
6971 | return err; | |
b131c964 | 6972 | err_trans: |
b131c964 | 6973 | kfree(obj->udata); |
85dfd816 PNA |
6974 | err_userdata: |
6975 | kfree(obj->key.name); | |
b131c964 | 6976 | err_strdup: |
dfc46034 | 6977 | if (obj->ops->destroy) |
00bfb320 | 6978 | obj->ops->destroy(&ctx, obj); |
e5009240 | 6979 | kfree(obj); |
b131c964 | 6980 | err_init: |
e5009240 PNA |
6981 | module_put(type->owner); |
6982 | return err; | |
6983 | } | |
6984 | ||
6985 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
6986 | u32 portid, u32 seq, int event, u32 flags, | |
6987 | int family, const struct nft_table *table, | |
43da04a5 | 6988 | struct nft_object *obj, bool reset) |
e5009240 | 6989 | { |
e5009240 PNA |
6990 | struct nlmsghdr *nlh; |
6991 | ||
dedb67c4 | 6992 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
6993 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
6994 | NFNETLINK_V0, nft_base_seq(net)); | |
6995 | if (!nlh) | |
e5009240 PNA |
6996 | goto nla_put_failure; |
6997 | ||
e5009240 | 6998 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || |
d152159b | 6999 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
dfc46034 | 7000 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || |
e5009240 | 7001 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || |
3ecbfd65 HS |
7002 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) || |
7003 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), | |
7004 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
7005 | goto nla_put_failure; |
7006 | ||
b131c964 JGG |
7007 | if (obj->udata && |
7008 | nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata)) | |
7009 | goto nla_put_failure; | |
7010 | ||
e5009240 PNA |
7011 | nlmsg_end(skb, nlh); |
7012 | return 0; | |
7013 | ||
7014 | nla_put_failure: | |
7015 | nlmsg_trim(skb, nlh); | |
7016 | return -1; | |
7017 | } | |
7018 | ||
a9fea2a3 | 7019 | struct nft_obj_filter { |
e46abbcc | 7020 | char *table; |
a9fea2a3 PNA |
7021 | u32 type; |
7022 | }; | |
7023 | ||
e5009240 PNA |
7024 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
7025 | { | |
7026 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 7027 | const struct nft_table *table; |
e5009240 | 7028 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 7029 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
7030 | struct net *net = sock_net(skb->sk); |
7031 | int family = nfmsg->nfgen_family; | |
0854db2a | 7032 | struct nftables_pernet *nft_net; |
43da04a5 PNA |
7033 | struct nft_object *obj; |
7034 | bool reset = false; | |
7035 | ||
7036 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
7037 | reset = true; | |
e5009240 PNA |
7038 | |
7039 | rcu_read_lock(); | |
d59d2f82 | 7040 | nft_net = nft_pernet(net); |
34002783 | 7041 | cb->seq = READ_ONCE(nft_net->base_seq); |
e5009240 | 7042 | |
0854db2a | 7043 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 7044 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
7045 | continue; |
7046 | ||
36596dad PNA |
7047 | list_for_each_entry_rcu(obj, &table->objects, list) { |
7048 | if (!nft_is_active(net, obj)) | |
7049 | goto cont; | |
7050 | if (idx < s_idx) | |
7051 | goto cont; | |
7052 | if (idx > s_idx) | |
7053 | memset(&cb->args[1], 0, | |
7054 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7055 | if (filter && filter->table && |
36596dad PNA |
7056 | strcmp(filter->table, table->name)) |
7057 | goto cont; | |
7058 | if (filter && | |
7059 | filter->type != NFT_OBJECT_UNSPEC && | |
7060 | obj->ops->type->type != filter->type) | |
7061 | goto cont; | |
8e6cf365 | 7062 | if (reset) { |
68df2ed5 | 7063 | char *buf = kasprintf(GFP_ATOMIC, |
c520292f | 7064 | "%s:%u", |
8e6cf365 | 7065 | table->name, |
0854db2a | 7066 | nft_net->base_seq); |
8e6cf365 RGB |
7067 | |
7068 | audit_log_nfcfg(buf, | |
7069 | family, | |
7070 | obj->handle, | |
14224039 | 7071 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7072 | GFP_ATOMIC); |
8e6cf365 RGB |
7073 | kfree(buf); |
7074 | } | |
7075 | ||
36596dad PNA |
7076 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
7077 | cb->nlh->nlmsg_seq, | |
7078 | NFT_MSG_NEWOBJ, | |
7079 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 7080 | table->family, table, |
36596dad PNA |
7081 | obj, reset) < 0) |
7082 | goto done; | |
e5009240 | 7083 | |
36596dad | 7084 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 7085 | cont: |
36596dad | 7086 | idx++; |
e5009240 PNA |
7087 | } |
7088 | } | |
7089 | done: | |
7090 | rcu_read_unlock(); | |
7091 | ||
7092 | cb->args[0] = idx; | |
7093 | return skb->len; | |
7094 | } | |
7095 | ||
90fd131a | 7096 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 7097 | { |
90fd131a FW |
7098 | const struct nlattr * const *nla = cb->data; |
7099 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 7100 | |
90fd131a FW |
7101 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
7102 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7103 | if (!filter) | |
7104 | return -ENOMEM; | |
7105 | ||
7106 | if (nla[NFTA_OBJ_TABLE]) { | |
7107 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
7108 | if (!filter->table) { | |
7109 | kfree(filter); | |
7110 | return -ENOMEM; | |
7111 | } | |
7112 | } | |
7113 | ||
7114 | if (nla[NFTA_OBJ_TYPE]) | |
7115 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 7116 | } |
a9fea2a3 | 7117 | |
90fd131a | 7118 | cb->data = filter; |
a9fea2a3 PNA |
7119 | return 0; |
7120 | } | |
7121 | ||
90fd131a | 7122 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 7123 | { |
90fd131a | 7124 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 7125 | |
90fd131a FW |
7126 | if (filter) { |
7127 | kfree(filter->table); | |
7128 | kfree(filter); | |
e46abbcc | 7129 | } |
a9fea2a3 | 7130 | |
90fd131a | 7131 | return 0; |
a9fea2a3 PNA |
7132 | } |
7133 | ||
d9adf22a | 7134 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7135 | static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info, |
7136 | const struct nlattr * const nla[]) | |
e5009240 | 7137 | { |
797d4980 PNA |
7138 | struct netlink_ext_ack *extack = info->extack; |
7139 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 7140 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7141 | const struct nft_table *table; |
797d4980 | 7142 | struct net *net = info->net; |
e5009240 PNA |
7143 | struct nft_object *obj; |
7144 | struct sk_buff *skb2; | |
43da04a5 | 7145 | bool reset = false; |
e5009240 PNA |
7146 | u32 objtype; |
7147 | int err; | |
7148 | ||
797d4980 | 7149 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
e5009240 | 7150 | struct netlink_dump_control c = { |
90fd131a | 7151 | .start = nf_tables_dump_obj_start, |
e5009240 | 7152 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 7153 | .done = nf_tables_dump_obj_done, |
d9adf22a | 7154 | .module = THIS_MODULE, |
90fd131a | 7155 | .data = (void *)nla, |
e5009240 | 7156 | }; |
a9fea2a3 | 7157 | |
797d4980 | 7158 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
e5009240 PNA |
7159 | } |
7160 | ||
7161 | if (!nla[NFTA_OBJ_NAME] || | |
7162 | !nla[NFTA_OBJ_TYPE]) | |
7163 | return -EINVAL; | |
7164 | ||
6001a930 | 7165 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0); |
36dd1bcc PNA |
7166 | if (IS_ERR(table)) { |
7167 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7168 | return PTR_ERR(table); |
36dd1bcc | 7169 | } |
e5009240 PNA |
7170 | |
7171 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7172 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
7173 | if (IS_ERR(obj)) { |
7174 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7175 | return PTR_ERR(obj); |
36dd1bcc | 7176 | } |
e5009240 | 7177 | |
d9adf22a | 7178 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
7179 | if (!skb2) |
7180 | return -ENOMEM; | |
7181 | ||
797d4980 | 7182 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
43da04a5 PNA |
7183 | reset = true; |
7184 | ||
8e6cf365 | 7185 | if (reset) { |
0854db2a FW |
7186 | const struct nftables_pernet *nft_net; |
7187 | char *buf; | |
7188 | ||
d59d2f82 | 7189 | nft_net = nft_pernet(net); |
0854db2a | 7190 | buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq); |
8e6cf365 RGB |
7191 | |
7192 | audit_log_nfcfg(buf, | |
7193 | family, | |
7194 | obj->handle, | |
14224039 | 7195 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7196 | GFP_ATOMIC); |
8e6cf365 RGB |
7197 | kfree(buf); |
7198 | } | |
7199 | ||
e5009240 | 7200 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 7201 | info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, |
43da04a5 | 7202 | family, table, obj, reset); |
e5009240 | 7203 | if (err < 0) |
ee921183 | 7204 | goto err_fill_obj_info; |
e5009240 | 7205 | |
ee921183 PNA |
7206 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
7207 | ||
7208 | err_fill_obj_info: | |
e5009240 PNA |
7209 | kfree_skb(skb2); |
7210 | return err; | |
e5009240 PNA |
7211 | } |
7212 | ||
00bfb320 | 7213 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 7214 | { |
dfc46034 | 7215 | if (obj->ops->destroy) |
00bfb320 | 7216 | obj->ops->destroy(ctx, obj); |
e5009240 | 7217 | |
dfc46034 | 7218 | module_put(obj->ops->type->owner); |
d152159b | 7219 | kfree(obj->key.name); |
bc7a7082 | 7220 | kfree(obj->udata); |
e5009240 PNA |
7221 | kfree(obj); |
7222 | } | |
7223 | ||
7dab8ee3 PNA |
7224 | static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info, |
7225 | const struct nlattr * const nla[]) | |
e5009240 | 7226 | { |
7dab8ee3 PNA |
7227 | struct netlink_ext_ack *extack = info->extack; |
7228 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7229 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 7230 | struct net *net = info->net; |
36dd1bcc | 7231 | const struct nlattr *attr; |
e5009240 PNA |
7232 | struct nft_table *table; |
7233 | struct nft_object *obj; | |
7234 | struct nft_ctx ctx; | |
7235 | u32 objtype; | |
7236 | ||
7237 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 7238 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
7239 | return -EINVAL; |
7240 | ||
6001a930 PNA |
7241 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7242 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7243 | if (IS_ERR(table)) { |
7244 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7245 | return PTR_ERR(table); |
36dd1bcc | 7246 | } |
e5009240 PNA |
7247 | |
7248 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
7249 | if (nla[NFTA_OBJ_HANDLE]) { |
7250 | attr = nla[NFTA_OBJ_HANDLE]; | |
7251 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
7252 | } else { | |
7253 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 7254 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
7255 | } |
7256 | ||
7257 | if (IS_ERR(obj)) { | |
7258 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7259 | return PTR_ERR(obj); |
36dd1bcc PNA |
7260 | } |
7261 | if (obj->use > 0) { | |
7262 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7263 | return -EBUSY; |
36dd1bcc | 7264 | } |
e5009240 | 7265 | |
7dab8ee3 | 7266 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 PNA |
7267 | |
7268 | return nft_delobj(&ctx, obj); | |
7269 | } | |
7270 | ||
d152159b | 7271 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 | 7272 | struct nft_object *obj, u32 portid, u32 seq, int event, |
6fb721cf | 7273 | u16 flags, int family, int report, gfp_t gfp) |
e5009240 | 7274 | { |
d59d2f82 | 7275 | struct nftables_pernet *nft_net = nft_pernet(net); |
e5009240 PNA |
7276 | struct sk_buff *skb; |
7277 | int err; | |
c520292f | 7278 | char *buf = kasprintf(gfp, "%s:%u", |
0854db2a | 7279 | table->name, nft_net->base_seq); |
8e6cf365 RGB |
7280 | |
7281 | audit_log_nfcfg(buf, | |
7282 | family, | |
7283 | obj->handle, | |
7284 | event == NFT_MSG_NEWOBJ ? | |
c520292f RGB |
7285 | AUDIT_NFT_OP_OBJ_REGISTER : |
7286 | AUDIT_NFT_OP_OBJ_UNREGISTER, | |
68df2ed5 | 7287 | gfp); |
8e6cf365 | 7288 | kfree(buf); |
e5009240 | 7289 | |
2599e989 PNA |
7290 | if (!report && |
7291 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 7292 | return; |
e5009240 | 7293 | |
2599e989 | 7294 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
7295 | if (skb == NULL) |
7296 | goto err; | |
7297 | ||
6fb721cf PNA |
7298 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, |
7299 | flags & (NLM_F_CREATE | NLM_F_EXCL), | |
7300 | family, table, obj, false); | |
e5009240 PNA |
7301 | if (err < 0) { |
7302 | kfree_skb(skb); | |
7303 | goto err; | |
7304 | } | |
7305 | ||
0854db2a | 7306 | nft_notify_enqueue(skb, report, &nft_net->notify_list); |
25e94a99 | 7307 | return; |
e5009240 | 7308 | err: |
25e94a99 | 7309 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 7310 | } |
2599e989 PNA |
7311 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
7312 | ||
25e94a99 PNA |
7313 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
7314 | struct nft_object *obj, int event) | |
2599e989 | 7315 | { |
25e94a99 | 7316 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
6fb721cf | 7317 | ctx->flags, ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 7318 | } |
e5009240 | 7319 | |
3b49e2e9 PNA |
7320 | /* |
7321 | * Flow tables | |
7322 | */ | |
7323 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
7324 | { | |
7325 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7326 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
7327 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7328 | } | |
7329 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
7330 | ||
7331 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
7332 | { | |
7333 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7334 | list_del_rcu(&type->list); | |
7335 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7336 | } | |
7337 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
7338 | ||
7339 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
7340 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
7341 | .len = NFT_NAME_MAXLEN - 1 }, | |
7342 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
7343 | .len = NFT_NAME_MAXLEN - 1 }, | |
7344 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7345 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 7346 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
7347 | }; |
7348 | ||
cac20fcd PNA |
7349 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
7350 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
7351 | { |
7352 | struct nft_flowtable *flowtable; | |
7353 | ||
d9adf22a | 7354 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
7355 | if (!nla_strcmp(nla, flowtable->name) && |
7356 | nft_active_genmask(flowtable, genmask)) | |
7357 | return flowtable; | |
7358 | } | |
7359 | return ERR_PTR(-ENOENT); | |
7360 | } | |
cac20fcd | 7361 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 7362 | |
9b05b6e1 LGL |
7363 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
7364 | struct nft_flowtable *flowtable, | |
7365 | enum nft_trans_phase phase) | |
7366 | { | |
7367 | switch (phase) { | |
7368 | case NFT_TRANS_PREPARE: | |
7369 | case NFT_TRANS_ABORT: | |
7370 | case NFT_TRANS_RELEASE: | |
7371 | flowtable->use--; | |
954d8297 | 7372 | fallthrough; |
9b05b6e1 LGL |
7373 | default: |
7374 | return; | |
7375 | } | |
7376 | } | |
7377 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
7378 | ||
ae0662f8 | 7379 | static struct nft_flowtable * |
cac20fcd PNA |
7380 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
7381 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
7382 | { |
7383 | struct nft_flowtable *flowtable; | |
7384 | ||
7385 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
7386 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
7387 | nft_active_genmask(flowtable, genmask)) | |
7388 | return flowtable; | |
7389 | } | |
7390 | return ERR_PTR(-ENOENT); | |
7391 | } | |
7392 | ||
d9246a53 PNA |
7393 | struct nft_flowtable_hook { |
7394 | u32 num; | |
7395 | int priority; | |
7396 | struct list_head list; | |
7397 | }; | |
7398 | ||
3b49e2e9 PNA |
7399 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
7400 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
7401 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
7402 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
7403 | }; | |
7404 | ||
d9246a53 PNA |
7405 | static int nft_flowtable_parse_hook(const struct nft_ctx *ctx, |
7406 | const struct nlattr *attr, | |
7407 | struct nft_flowtable_hook *flowtable_hook, | |
5b6743fb | 7408 | struct nft_flowtable *flowtable, bool add) |
3b49e2e9 | 7409 | { |
3b49e2e9 | 7410 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 7411 | struct nft_hook *hook; |
3b49e2e9 | 7412 | int hooknum, priority; |
3f0465a9 | 7413 | int err; |
3b49e2e9 | 7414 | |
d9246a53 PNA |
7415 | INIT_LIST_HEAD(&flowtable_hook->list); |
7416 | ||
8cb08174 JB |
7417 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr, |
7418 | nft_flowtable_hook_policy, NULL); | |
3b49e2e9 PNA |
7419 | if (err < 0) |
7420 | return err; | |
7421 | ||
5b6743fb PNA |
7422 | if (add) { |
7423 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
7424 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) | |
7425 | return -EINVAL; | |
3b49e2e9 | 7426 | |
5b6743fb PNA |
7427 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); |
7428 | if (hooknum != NF_NETDEV_INGRESS) | |
7429 | return -EOPNOTSUPP; | |
7430 | ||
7431 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7432 | ||
7433 | flowtable_hook->priority = priority; | |
7434 | flowtable_hook->num = hooknum; | |
7435 | } else { | |
7436 | if (tb[NFTA_FLOWTABLE_HOOK_NUM]) { | |
7437 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
7438 | if (hooknum != flowtable->hooknum) | |
7439 | return -EOPNOTSUPP; | |
7440 | } | |
7441 | ||
7442 | if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { | |
7443 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7444 | if (priority != flowtable->data.priority) | |
7445 | return -EOPNOTSUPP; | |
7446 | } | |
3b49e2e9 | 7447 | |
5b6743fb PNA |
7448 | flowtable_hook->priority = flowtable->data.priority; |
7449 | flowtable_hook->num = flowtable->hooknum; | |
7450 | } | |
3b49e2e9 | 7451 | |
05abe445 PNA |
7452 | if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) { |
7453 | err = nf_tables_parse_netdev_hooks(ctx->net, | |
7454 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
7455 | &flowtable_hook->list); | |
7456 | if (err < 0) | |
7457 | return err; | |
7458 | } | |
3b49e2e9 | 7459 | |
d9246a53 | 7460 | list_for_each_entry(hook, &flowtable_hook->list, list) { |
3f0465a9 | 7461 | hook->ops.pf = NFPROTO_NETDEV; |
5b6743fb PNA |
7462 | hook->ops.hooknum = flowtable_hook->num; |
7463 | hook->ops.priority = flowtable_hook->priority; | |
7464 | hook->ops.priv = &flowtable->data; | |
7465 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
7466 | } |
7467 | ||
3b49e2e9 PNA |
7468 | return err; |
7469 | } | |
7470 | ||
98319cb9 | 7471 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
7472 | { |
7473 | const struct nf_flowtable_type *type; | |
7474 | ||
7475 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 7476 | if (family == type->family) |
3b49e2e9 PNA |
7477 | return type; |
7478 | } | |
7479 | return NULL; | |
7480 | } | |
7481 | ||
452238e8 FW |
7482 | static const struct nf_flowtable_type * |
7483 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
7484 | { |
7485 | const struct nf_flowtable_type *type; | |
7486 | ||
98319cb9 | 7487 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
7488 | if (type != NULL && try_module_get(type->owner)) |
7489 | return type; | |
7490 | ||
f102d66b | 7491 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
7492 | #ifdef CONFIG_MODULES |
7493 | if (type == NULL) { | |
eb014de4 | 7494 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
7495 | return ERR_PTR(-EAGAIN); |
7496 | } | |
7497 | #endif | |
7498 | return ERR_PTR(-ENOENT); | |
7499 | } | |
7500 | ||
5acab914 | 7501 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
7502 | static void nft_unregister_flowtable_hook(struct net *net, |
7503 | struct nft_flowtable *flowtable, | |
7504 | struct nft_hook *hook) | |
7505 | { | |
7506 | nf_unregister_net_hook(net, &hook->ops); | |
7507 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
7508 | FLOW_BLOCK_UNBIND); | |
7509 | } | |
7510 | ||
f9a43007 PNA |
7511 | static void __nft_unregister_flowtable_net_hooks(struct net *net, |
7512 | struct list_head *hook_list, | |
7513 | bool release_netdev) | |
3b49e2e9 | 7514 | { |
f9a43007 | 7515 | struct nft_hook *hook, *next; |
3b49e2e9 | 7516 | |
f9a43007 | 7517 | list_for_each_entry_safe(hook, next, hook_list, list) { |
5acab914 | 7518 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
7519 | if (release_netdev) { |
7520 | list_del(&hook->list); | |
ab5e5c06 | 7521 | kfree_rcu(hook, rcu); |
f9a43007 PNA |
7522 | } |
7523 | } | |
7524 | } | |
7525 | ||
7526 | static void nft_unregister_flowtable_net_hooks(struct net *net, | |
7527 | struct list_head *hook_list) | |
7528 | { | |
7529 | __nft_unregister_flowtable_net_hooks(net, hook_list, false); | |
3f0465a9 PNA |
7530 | } |
7531 | ||
7532 | static int nft_register_flowtable_net_hooks(struct net *net, | |
7533 | struct nft_table *table, | |
f9382669 | 7534 | struct list_head *hook_list, |
3f0465a9 PNA |
7535 | struct nft_flowtable *flowtable) |
7536 | { | |
7537 | struct nft_hook *hook, *hook2, *next; | |
7538 | struct nft_flowtable *ft; | |
7539 | int err, i = 0; | |
7540 | ||
f9382669 | 7541 | list_for_each_entry(hook, hook_list, list) { |
3f0465a9 | 7542 | list_for_each_entry(ft, &table->flowtables, list) { |
86fe2c19 PNA |
7543 | if (!nft_is_active_next(net, ft)) |
7544 | continue; | |
7545 | ||
3f0465a9 PNA |
7546 | list_for_each_entry(hook2, &ft->hook_list, list) { |
7547 | if (hook->ops.dev == hook2->ops.dev && | |
7548 | hook->ops.pf == hook2->ops.pf) { | |
77a92189 | 7549 | err = -EEXIST; |
3f0465a9 PNA |
7550 | goto err_unregister_net_hooks; |
7551 | } | |
7552 | } | |
7553 | } | |
3b49e2e9 | 7554 | |
d7c03a9f | 7555 | err = flowtable->data.type->setup(&flowtable->data, |
7556 | hook->ops.dev, | |
7557 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
7558 | if (err < 0) |
7559 | goto err_unregister_net_hooks; | |
7560 | ||
d7c03a9f | 7561 | err = nf_register_net_hook(net, &hook->ops); |
7562 | if (err < 0) { | |
7563 | flowtable->data.type->setup(&flowtable->data, | |
7564 | hook->ops.dev, | |
7565 | FLOW_BLOCK_UNBIND); | |
7566 | goto err_unregister_net_hooks; | |
7567 | } | |
7568 | ||
3f0465a9 | 7569 | i++; |
3b49e2e9 | 7570 | } |
3f0465a9 PNA |
7571 | |
7572 | return 0; | |
7573 | ||
7574 | err_unregister_net_hooks: | |
f9382669 | 7575 | list_for_each_entry_safe(hook, next, hook_list, list) { |
3f0465a9 PNA |
7576 | if (i-- <= 0) |
7577 | break; | |
7578 | ||
ff4bf2f4 | 7579 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7580 | list_del_rcu(&hook->list); |
7581 | kfree_rcu(hook, rcu); | |
7582 | } | |
7583 | ||
7584 | return err; | |
3b49e2e9 PNA |
7585 | } |
7586 | ||
389a2cbc PNA |
7587 | static void nft_flowtable_hooks_destroy(struct list_head *hook_list) |
7588 | { | |
7589 | struct nft_hook *hook, *next; | |
7590 | ||
7591 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
7592 | list_del_rcu(&hook->list); | |
7593 | kfree_rcu(hook, rcu); | |
7594 | } | |
7595 | } | |
7596 | ||
78d9f48f PNA |
7597 | static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, |
7598 | struct nft_flowtable *flowtable) | |
7599 | { | |
7600 | const struct nlattr * const *nla = ctx->nla; | |
7601 | struct nft_flowtable_hook flowtable_hook; | |
7602 | struct nft_hook *hook, *next; | |
7603 | struct nft_trans *trans; | |
7604 | bool unregister = false; | |
7b35582c | 7605 | u32 flags; |
78d9f48f PNA |
7606 | int err; |
7607 | ||
7608 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7609 | &flowtable_hook, flowtable, false); |
78d9f48f PNA |
7610 | if (err < 0) |
7611 | return err; | |
7612 | ||
7613 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7614 | if (nft_hook_list_find(&flowtable->hook_list, hook)) { | |
7615 | list_del(&hook->list); | |
7616 | kfree(hook); | |
7617 | } | |
7618 | } | |
7619 | ||
7b35582c PNA |
7620 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7621 | flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
c271cc9f PNA |
7622 | if (flags & ~NFT_FLOWTABLE_MASK) { |
7623 | err = -EOPNOTSUPP; | |
7624 | goto err_flowtable_update_hook; | |
7625 | } | |
7b35582c | 7626 | if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^ |
c271cc9f PNA |
7627 | (flags & NFT_FLOWTABLE_HW_OFFLOAD)) { |
7628 | err = -EOPNOTSUPP; | |
7629 | goto err_flowtable_update_hook; | |
7630 | } | |
7b35582c PNA |
7631 | } else { |
7632 | flags = flowtable->data.flags; | |
7633 | } | |
7634 | ||
78d9f48f PNA |
7635 | err = nft_register_flowtable_net_hooks(ctx->net, ctx->table, |
7636 | &flowtable_hook.list, flowtable); | |
7637 | if (err < 0) | |
7638 | goto err_flowtable_update_hook; | |
7639 | ||
7640 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE, | |
7641 | sizeof(struct nft_trans_flowtable)); | |
7642 | if (!trans) { | |
7643 | unregister = true; | |
7644 | err = -ENOMEM; | |
7645 | goto err_flowtable_update_hook; | |
7646 | } | |
7647 | ||
7b35582c | 7648 | nft_trans_flowtable_flags(trans) = flags; |
78d9f48f PNA |
7649 | nft_trans_flowtable(trans) = flowtable; |
7650 | nft_trans_flowtable_update(trans) = true; | |
7651 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
7652 | list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans)); | |
7653 | ||
0854db2a | 7654 | nft_trans_commit_list_add_tail(ctx->net, trans); |
78d9f48f PNA |
7655 | |
7656 | return 0; | |
7657 | ||
7658 | err_flowtable_update_hook: | |
7659 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7660 | if (unregister) | |
7661 | nft_unregister_flowtable_hook(ctx->net, flowtable, hook); | |
7662 | list_del_rcu(&hook->list); | |
7663 | kfree_rcu(hook, rcu); | |
7664 | } | |
7665 | ||
7666 | return err; | |
7667 | ||
7668 | } | |
7669 | ||
7dab8ee3 PNA |
7670 | static int nf_tables_newflowtable(struct sk_buff *skb, |
7671 | const struct nfnl_info *info, | |
7672 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7673 | { |
7dab8ee3 | 7674 | struct netlink_ext_ack *extack = info->extack; |
d9246a53 | 7675 | struct nft_flowtable_hook flowtable_hook; |
7dab8ee3 | 7676 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 7677 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7678 | const struct nf_flowtable_type *type; |
3f0465a9 PNA |
7679 | struct nft_flowtable *flowtable; |
7680 | struct nft_hook *hook, *next; | |
7dab8ee3 | 7681 | struct net *net = info->net; |
3b49e2e9 PNA |
7682 | struct nft_table *table; |
7683 | struct nft_ctx ctx; | |
3f0465a9 | 7684 | int err; |
3b49e2e9 PNA |
7685 | |
7686 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
7687 | !nla[NFTA_FLOWTABLE_NAME] || | |
7688 | !nla[NFTA_FLOWTABLE_HOOK]) | |
7689 | return -EINVAL; | |
7690 | ||
cac20fcd | 7691 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7692 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7693 | if (IS_ERR(table)) { |
7694 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7695 | return PTR_ERR(table); |
36dd1bcc | 7696 | } |
3b49e2e9 | 7697 | |
cac20fcd PNA |
7698 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
7699 | genmask); | |
3b49e2e9 PNA |
7700 | if (IS_ERR(flowtable)) { |
7701 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
7702 | if (err != -ENOENT) { |
7703 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 7704 | return err; |
36dd1bcc | 7705 | } |
3b49e2e9 | 7706 | } else { |
7dab8ee3 | 7707 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 7708 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); |
3b49e2e9 | 7709 | return -EEXIST; |
36dd1bcc | 7710 | } |
3b49e2e9 | 7711 | |
7dab8ee3 | 7712 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
78d9f48f | 7713 | |
7dab8ee3 | 7714 | return nft_flowtable_update(&ctx, info->nlh, flowtable); |
3b49e2e9 PNA |
7715 | } |
7716 | ||
7dab8ee3 | 7717 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
3b49e2e9 | 7718 | |
33758c89 | 7719 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
7720 | if (!flowtable) |
7721 | return -ENOMEM; | |
7722 | ||
7723 | flowtable->table = table; | |
3ecbfd65 | 7724 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 7725 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 7726 | |
33758c89 | 7727 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
7728 | if (!flowtable->name) { |
7729 | err = -ENOMEM; | |
7730 | goto err1; | |
7731 | } | |
7732 | ||
452238e8 | 7733 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
7734 | if (IS_ERR(type)) { |
7735 | err = PTR_ERR(type); | |
7736 | goto err2; | |
7737 | } | |
7738 | ||
8bb69f3b PNA |
7739 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7740 | flowtable->data.flags = | |
7741 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
7e6136f1 PNA |
7742 | if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) { |
7743 | err = -EOPNOTSUPP; | |
8bb69f3b | 7744 | goto err3; |
7e6136f1 | 7745 | } |
8bb69f3b PNA |
7746 | } |
7747 | ||
7748 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 7749 | flowtable->data.type = type; |
a268de77 | 7750 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
7751 | if (err < 0) |
7752 | goto err3; | |
7753 | ||
d9246a53 | 7754 | err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK], |
5b6743fb | 7755 | &flowtable_hook, flowtable, true); |
3b49e2e9 | 7756 | if (err < 0) |
a268de77 | 7757 | goto err4; |
3b49e2e9 | 7758 | |
d9246a53 PNA |
7759 | list_splice(&flowtable_hook.list, &flowtable->hook_list); |
7760 | flowtable->data.priority = flowtable_hook.priority; | |
7761 | flowtable->hooknum = flowtable_hook.num; | |
7762 | ||
f9382669 PNA |
7763 | err = nft_register_flowtable_net_hooks(ctx.net, table, |
7764 | &flowtable->hook_list, | |
7765 | flowtable); | |
2d285f26 | 7766 | if (err < 0) { |
389a2cbc | 7767 | nft_flowtable_hooks_destroy(&flowtable->hook_list); |
3f0465a9 | 7768 | goto err4; |
2d285f26 | 7769 | } |
3b49e2e9 PNA |
7770 | |
7771 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
7772 | if (err < 0) | |
3f0465a9 | 7773 | goto err5; |
3b49e2e9 PNA |
7774 | |
7775 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
7776 | table->use++; | |
7777 | ||
7778 | return 0; | |
a268de77 | 7779 | err5: |
3f0465a9 | 7780 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 7781 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7782 | list_del_rcu(&hook->list); |
7783 | kfree_rcu(hook, rcu); | |
7784 | } | |
a268de77 FF |
7785 | err4: |
7786 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
7787 | err3: |
7788 | module_put(type->owner); | |
7789 | err2: | |
7790 | kfree(flowtable->name); | |
7791 | err1: | |
7792 | kfree(flowtable); | |
7793 | return err; | |
7794 | } | |
7795 | ||
3003055f PNA |
7796 | static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook) |
7797 | { | |
7798 | struct nft_hook *this, *next; | |
7799 | ||
7800 | list_for_each_entry_safe(this, next, &flowtable_hook->list, list) { | |
7801 | list_del(&this->list); | |
7802 | kfree(this); | |
7803 | } | |
7804 | } | |
7805 | ||
abadb2f8 PNA |
7806 | static int nft_delflowtable_hook(struct nft_ctx *ctx, |
7807 | struct nft_flowtable *flowtable) | |
7808 | { | |
7809 | const struct nlattr * const *nla = ctx->nla; | |
7810 | struct nft_flowtable_hook flowtable_hook; | |
b6d9014a | 7811 | LIST_HEAD(flowtable_del_list); |
3003055f | 7812 | struct nft_hook *this, *hook; |
abadb2f8 PNA |
7813 | struct nft_trans *trans; |
7814 | int err; | |
7815 | ||
7816 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7817 | &flowtable_hook, flowtable, false); |
abadb2f8 PNA |
7818 | if (err < 0) |
7819 | return err; | |
7820 | ||
3003055f | 7821 | list_for_each_entry(this, &flowtable_hook.list, list) { |
abadb2f8 PNA |
7822 | hook = nft_hook_list_find(&flowtable->hook_list, this); |
7823 | if (!hook) { | |
7824 | err = -ENOENT; | |
7825 | goto err_flowtable_del_hook; | |
7826 | } | |
b6d9014a | 7827 | list_move(&hook->list, &flowtable_del_list); |
abadb2f8 PNA |
7828 | } |
7829 | ||
7830 | trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE, | |
7831 | sizeof(struct nft_trans_flowtable)); | |
3003055f PNA |
7832 | if (!trans) { |
7833 | err = -ENOMEM; | |
7834 | goto err_flowtable_del_hook; | |
7835 | } | |
abadb2f8 PNA |
7836 | |
7837 | nft_trans_flowtable(trans) = flowtable; | |
7838 | nft_trans_flowtable_update(trans) = true; | |
7839 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
b6d9014a | 7840 | list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans)); |
3003055f | 7841 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 | 7842 | |
0854db2a | 7843 | nft_trans_commit_list_add_tail(ctx->net, trans); |
abadb2f8 PNA |
7844 | |
7845 | return 0; | |
7846 | ||
7847 | err_flowtable_del_hook: | |
b6d9014a | 7848 | list_splice(&flowtable_del_list, &flowtable->hook_list); |
3003055f | 7849 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 PNA |
7850 | |
7851 | return err; | |
7852 | } | |
7853 | ||
7dab8ee3 PNA |
7854 | static int nf_tables_delflowtable(struct sk_buff *skb, |
7855 | const struct nfnl_info *info, | |
7856 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7857 | { |
7dab8ee3 PNA |
7858 | struct netlink_ext_ack *extack = info->extack; |
7859 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7860 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7861 | struct nft_flowtable *flowtable; |
7dab8ee3 | 7862 | struct net *net = info->net; |
36dd1bcc | 7863 | const struct nlattr *attr; |
3b49e2e9 PNA |
7864 | struct nft_table *table; |
7865 | struct nft_ctx ctx; | |
7866 | ||
e603ea4b PNA |
7867 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
7868 | (!nla[NFTA_FLOWTABLE_NAME] && | |
7869 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
7870 | return -EINVAL; | |
7871 | ||
cac20fcd | 7872 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7873 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7874 | if (IS_ERR(table)) { |
7875 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7876 | return PTR_ERR(table); |
36dd1bcc | 7877 | } |
3b49e2e9 | 7878 | |
36dd1bcc PNA |
7879 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
7880 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
7881 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
7882 | } else { | |
7883 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
7884 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
7885 | } | |
7886 | ||
7887 | if (IS_ERR(flowtable)) { | |
7888 | NL_SET_BAD_ATTR(extack, attr); | |
7889 | return PTR_ERR(flowtable); | |
7890 | } | |
abadb2f8 | 7891 | |
7dab8ee3 | 7892 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
abadb2f8 PNA |
7893 | |
7894 | if (nla[NFTA_FLOWTABLE_HOOK]) | |
7895 | return nft_delflowtable_hook(&ctx, flowtable); | |
7896 | ||
36dd1bcc PNA |
7897 | if (flowtable->use > 0) { |
7898 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 7899 | return -EBUSY; |
36dd1bcc | 7900 | } |
3b49e2e9 | 7901 | |
3b49e2e9 PNA |
7902 | return nft_delflowtable(&ctx, flowtable); |
7903 | } | |
7904 | ||
7905 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
7906 | u32 portid, u32 seq, int event, | |
7907 | u32 flags, int family, | |
c42d8bda PNA |
7908 | struct nft_flowtable *flowtable, |
7909 | struct list_head *hook_list) | |
3b49e2e9 PNA |
7910 | { |
7911 | struct nlattr *nest, *nest_devs; | |
3f0465a9 | 7912 | struct nft_hook *hook; |
3b49e2e9 | 7913 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
7914 | |
7915 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
19c28b13 PNA |
7916 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
7917 | NFNETLINK_V0, nft_base_seq(net)); | |
7918 | if (!nlh) | |
3b49e2e9 PNA |
7919 | goto nla_put_failure; |
7920 | ||
3b49e2e9 PNA |
7921 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || |
7922 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 HS |
7923 | nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || |
7924 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), | |
8bb69f3b PNA |
7925 | NFTA_FLOWTABLE_PAD) || |
7926 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) | |
3b49e2e9 PNA |
7927 | goto nla_put_failure; |
7928 | ||
ae0be8de | 7929 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
7930 | if (!nest) |
7931 | goto nla_put_failure; | |
3b49e2e9 | 7932 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 7933 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
7934 | goto nla_put_failure; |
7935 | ||
ae0be8de | 7936 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
7937 | if (!nest_devs) |
7938 | goto nla_put_failure; | |
7939 | ||
c42d8bda | 7940 | list_for_each_entry_rcu(hook, hook_list, list) { |
3f0465a9 | 7941 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) |
3b49e2e9 PNA |
7942 | goto nla_put_failure; |
7943 | } | |
7944 | nla_nest_end(skb, nest_devs); | |
7945 | nla_nest_end(skb, nest); | |
7946 | ||
7947 | nlmsg_end(skb, nlh); | |
7948 | return 0; | |
7949 | ||
7950 | nla_put_failure: | |
7951 | nlmsg_trim(skb, nlh); | |
7952 | return -1; | |
7953 | } | |
7954 | ||
7955 | struct nft_flowtable_filter { | |
7956 | char *table; | |
7957 | }; | |
7958 | ||
7959 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
7960 | struct netlink_callback *cb) | |
7961 | { | |
7962 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
7963 | struct nft_flowtable_filter *filter = cb->data; | |
7964 | unsigned int idx = 0, s_idx = cb->args[0]; | |
7965 | struct net *net = sock_net(skb->sk); | |
7966 | int family = nfmsg->nfgen_family; | |
7967 | struct nft_flowtable *flowtable; | |
0854db2a | 7968 | struct nftables_pernet *nft_net; |
3b49e2e9 PNA |
7969 | const struct nft_table *table; |
7970 | ||
7971 | rcu_read_lock(); | |
d59d2f82 | 7972 | nft_net = nft_pernet(net); |
34002783 | 7973 | cb->seq = READ_ONCE(nft_net->base_seq); |
3b49e2e9 | 7974 | |
0854db2a | 7975 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 7976 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
7977 | continue; |
7978 | ||
36596dad PNA |
7979 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
7980 | if (!nft_is_active(net, flowtable)) | |
7981 | goto cont; | |
7982 | if (idx < s_idx) | |
7983 | goto cont; | |
7984 | if (idx > s_idx) | |
7985 | memset(&cb->args[1], 0, | |
7986 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7987 | if (filter && filter->table && |
36596dad PNA |
7988 | strcmp(filter->table, table->name)) |
7989 | goto cont; | |
3b49e2e9 | 7990 | |
36596dad PNA |
7991 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
7992 | cb->nlh->nlmsg_seq, | |
7993 | NFT_MSG_NEWFLOWTABLE, | |
7994 | NLM_F_MULTI | NLM_F_APPEND, | |
c42d8bda PNA |
7995 | table->family, |
7996 | flowtable, | |
7997 | &flowtable->hook_list) < 0) | |
36596dad | 7998 | goto done; |
3b49e2e9 | 7999 | |
36596dad | 8000 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 8001 | cont: |
36596dad | 8002 | idx++; |
3b49e2e9 PNA |
8003 | } |
8004 | } | |
8005 | done: | |
8006 | rcu_read_unlock(); | |
8007 | ||
8008 | cb->args[0] = idx; | |
8009 | return skb->len; | |
8010 | } | |
8011 | ||
90fd131a | 8012 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 8013 | { |
90fd131a FW |
8014 | const struct nlattr * const *nla = cb->data; |
8015 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 8016 | |
90fd131a FW |
8017 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
8018 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
8019 | if (!filter) | |
8020 | return -ENOMEM; | |
3b49e2e9 | 8021 | |
90fd131a FW |
8022 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
8023 | GFP_ATOMIC); | |
8024 | if (!filter->table) { | |
8025 | kfree(filter); | |
8026 | return -ENOMEM; | |
8027 | } | |
8028 | } | |
3b49e2e9 | 8029 | |
90fd131a | 8030 | cb->data = filter; |
3b49e2e9 PNA |
8031 | return 0; |
8032 | } | |
8033 | ||
90fd131a | 8034 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 8035 | { |
90fd131a | 8036 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 8037 | |
3b49e2e9 | 8038 | if (!filter) |
90fd131a | 8039 | return 0; |
3b49e2e9 | 8040 | |
90fd131a FW |
8041 | kfree(filter->table); |
8042 | kfree(filter); | |
8043 | ||
8044 | return 0; | |
3b49e2e9 PNA |
8045 | } |
8046 | ||
d9adf22a | 8047 | /* called with rcu_read_lock held */ |
797d4980 PNA |
8048 | static int nf_tables_getflowtable(struct sk_buff *skb, |
8049 | const struct nfnl_info *info, | |
8050 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8051 | { |
797d4980 | 8052 | u8 genmask = nft_genmask_cur(info->net); |
ef4b65e5 | 8053 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8054 | struct nft_flowtable *flowtable; |
3b49e2e9 | 8055 | const struct nft_table *table; |
797d4980 | 8056 | struct net *net = info->net; |
3b49e2e9 PNA |
8057 | struct sk_buff *skb2; |
8058 | int err; | |
8059 | ||
797d4980 | 8060 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
3b49e2e9 | 8061 | struct netlink_dump_control c = { |
90fd131a | 8062 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
8063 | .dump = nf_tables_dump_flowtable, |
8064 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 8065 | .module = THIS_MODULE, |
90fd131a | 8066 | .data = (void *)nla, |
3b49e2e9 PNA |
8067 | }; |
8068 | ||
797d4980 | 8069 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
3b49e2e9 PNA |
8070 | } |
8071 | ||
8072 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
8073 | return -EINVAL; | |
8074 | ||
cac20fcd | 8075 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8076 | genmask, 0); |
3b49e2e9 PNA |
8077 | if (IS_ERR(table)) |
8078 | return PTR_ERR(table); | |
8079 | ||
cac20fcd PNA |
8080 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
8081 | genmask); | |
03a0120f | 8082 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
8083 | return PTR_ERR(flowtable); |
8084 | ||
d9adf22a | 8085 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
8086 | if (!skb2) |
8087 | return -ENOMEM; | |
8088 | ||
8089 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
797d4980 | 8090 | info->nlh->nlmsg_seq, |
3b49e2e9 | 8091 | NFT_MSG_NEWFLOWTABLE, 0, family, |
c42d8bda | 8092 | flowtable, &flowtable->hook_list); |
3b49e2e9 | 8093 | if (err < 0) |
ee921183 | 8094 | goto err_fill_flowtable_info; |
3b49e2e9 | 8095 | |
ee921183 PNA |
8096 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
8097 | ||
8098 | err_fill_flowtable_info: | |
3b49e2e9 PNA |
8099 | kfree_skb(skb2); |
8100 | return err; | |
8101 | } | |
8102 | ||
8103 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
8104 | struct nft_flowtable *flowtable, | |
c42d8bda | 8105 | struct list_head *hook_list, |
3b49e2e9 PNA |
8106 | int event) |
8107 | { | |
d59d2f82 | 8108 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
3b49e2e9 | 8109 | struct sk_buff *skb; |
6fb721cf | 8110 | u16 flags = 0; |
3b49e2e9 PNA |
8111 | int err; |
8112 | ||
dceababa | 8113 | if (!ctx->report && |
3b49e2e9 PNA |
8114 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
8115 | return; | |
8116 | ||
8117 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
8118 | if (skb == NULL) | |
8119 | goto err; | |
8120 | ||
6fb721cf PNA |
8121 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
8122 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
8123 | ||
3b49e2e9 | 8124 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, |
6fb721cf | 8125 | ctx->seq, event, flags, |
c42d8bda | 8126 | ctx->family, flowtable, hook_list); |
3b49e2e9 PNA |
8127 | if (err < 0) { |
8128 | kfree_skb(skb); | |
8129 | goto err; | |
8130 | } | |
8131 | ||
0854db2a | 8132 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
3b49e2e9 PNA |
8133 | return; |
8134 | err: | |
8135 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
8136 | } | |
8137 | ||
3b49e2e9 PNA |
8138 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
8139 | { | |
3f0465a9 PNA |
8140 | struct nft_hook *hook, *next; |
8141 | ||
5acab914 | 8142 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 8143 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
8144 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
8145 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
8146 | list_del_rcu(&hook->list); |
8147 | kfree(hook); | |
8148 | } | |
3b49e2e9 | 8149 | kfree(flowtable->name); |
3b49e2e9 | 8150 | module_put(flowtable->data.type->owner); |
a12486eb | 8151 | kfree(flowtable); |
3b49e2e9 PNA |
8152 | } |
8153 | ||
84d7fce6 PNA |
8154 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
8155 | u32 portid, u32 seq) | |
8156 | { | |
d59d2f82 | 8157 | struct nftables_pernet *nft_net = nft_pernet(net); |
84d7fce6 | 8158 | struct nlmsghdr *nlh; |
784b4e61 | 8159 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 8160 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 | 8161 | |
19c28b13 PNA |
8162 | nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC, |
8163 | NFNETLINK_V0, nft_base_seq(net)); | |
8164 | if (!nlh) | |
84d7fce6 PNA |
8165 | goto nla_put_failure; |
8166 | ||
0854db2a | 8167 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) || |
784b4e61 PS |
8168 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || |
8169 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
8170 | goto nla_put_failure; |
8171 | ||
053c095a JB |
8172 | nlmsg_end(skb, nlh); |
8173 | return 0; | |
84d7fce6 PNA |
8174 | |
8175 | nla_put_failure: | |
8176 | nlmsg_trim(skb, nlh); | |
8177 | return -EMSGSIZE; | |
8178 | } | |
8179 | ||
3b49e2e9 PNA |
8180 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
8181 | struct nft_flowtable *flowtable) | |
8182 | { | |
3f0465a9 | 8183 | struct nft_hook *hook; |
3b49e2e9 | 8184 | |
3f0465a9 PNA |
8185 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
8186 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
8187 | continue; |
8188 | ||
5acab914 | 8189 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 8190 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
8191 | list_del_rcu(&hook->list); |
8192 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
8193 | break; |
8194 | } | |
8195 | } | |
8196 | ||
8197 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
8198 | unsigned long event, void *ptr) | |
8199 | { | |
8200 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
8201 | struct nft_flowtable *flowtable; | |
0854db2a | 8202 | struct nftables_pernet *nft_net; |
3b49e2e9 | 8203 | struct nft_table *table; |
0a2cf5ee | 8204 | struct net *net; |
3b49e2e9 PNA |
8205 | |
8206 | if (event != NETDEV_UNREGISTER) | |
8207 | return 0; | |
8208 | ||
6a48de01 | 8209 | net = dev_net(dev); |
d59d2f82 | 8210 | nft_net = nft_pernet(net); |
0854db2a FW |
8211 | mutex_lock(&nft_net->commit_mutex); |
8212 | list_for_each_entry(table, &nft_net->tables, list) { | |
36596dad PNA |
8213 | list_for_each_entry(flowtable, &table->flowtables, list) { |
8214 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
8215 | } |
8216 | } | |
0854db2a | 8217 | mutex_unlock(&nft_net->commit_mutex); |
6a48de01 | 8218 | |
3b49e2e9 PNA |
8219 | return NOTIFY_DONE; |
8220 | } | |
8221 | ||
8222 | static struct notifier_block nf_tables_flowtable_notifier = { | |
8223 | .notifier_call = nf_tables_flowtable_event, | |
8224 | }; | |
8225 | ||
25e94a99 PNA |
8226 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
8227 | int event) | |
84d7fce6 PNA |
8228 | { |
8229 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
8230 | struct sk_buff *skb2; | |
8231 | int err; | |
8232 | ||
dceababa | 8233 | if (!nlmsg_report(nlh) && |
84d7fce6 | 8234 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 8235 | return; |
84d7fce6 | 8236 | |
84d7fce6 PNA |
8237 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
8238 | if (skb2 == NULL) | |
8239 | goto err; | |
8240 | ||
8241 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
8242 | nlh->nlmsg_seq); | |
8243 | if (err < 0) { | |
8244 | kfree_skb(skb2); | |
8245 | goto err; | |
8246 | } | |
8247 | ||
25e94a99 PNA |
8248 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8249 | nlmsg_report(nlh), GFP_KERNEL); | |
8250 | return; | |
84d7fce6 | 8251 | err: |
25e94a99 PNA |
8252 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8253 | -ENOBUFS); | |
84d7fce6 PNA |
8254 | } |
8255 | ||
797d4980 PNA |
8256 | static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info, |
8257 | const struct nlattr * const nla[]) | |
84d7fce6 | 8258 | { |
84d7fce6 PNA |
8259 | struct sk_buff *skb2; |
8260 | int err; | |
8261 | ||
d9adf22a | 8262 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
8263 | if (skb2 == NULL) |
8264 | return -ENOMEM; | |
8265 | ||
797d4980 PNA |
8266 | err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid, |
8267 | info->nlh->nlmsg_seq); | |
84d7fce6 | 8268 | if (err < 0) |
ee921183 | 8269 | goto err_fill_gen_info; |
84d7fce6 | 8270 | |
797d4980 | 8271 | return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); |
ee921183 PNA |
8272 | |
8273 | err_fill_gen_info: | |
84d7fce6 PNA |
8274 | kfree_skb(skb2); |
8275 | return err; | |
8276 | } | |
8277 | ||
96518518 PM |
8278 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
8279 | [NFT_MSG_NEWTABLE] = { | |
50f2db9e PNA |
8280 | .call = nf_tables_newtable, |
8281 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8282 | .attr_count = NFTA_TABLE_MAX, |
8283 | .policy = nft_table_policy, | |
8284 | }, | |
8285 | [NFT_MSG_GETTABLE] = { | |
50f2db9e PNA |
8286 | .call = nf_tables_gettable, |
8287 | .type = NFNL_CB_RCU, | |
96518518 PM |
8288 | .attr_count = NFTA_TABLE_MAX, |
8289 | .policy = nft_table_policy, | |
8290 | }, | |
8291 | [NFT_MSG_DELTABLE] = { | |
50f2db9e PNA |
8292 | .call = nf_tables_deltable, |
8293 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8294 | .attr_count = NFTA_TABLE_MAX, |
8295 | .policy = nft_table_policy, | |
8296 | }, | |
8297 | [NFT_MSG_NEWCHAIN] = { | |
50f2db9e PNA |
8298 | .call = nf_tables_newchain, |
8299 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8300 | .attr_count = NFTA_CHAIN_MAX, |
8301 | .policy = nft_chain_policy, | |
8302 | }, | |
8303 | [NFT_MSG_GETCHAIN] = { | |
50f2db9e PNA |
8304 | .call = nf_tables_getchain, |
8305 | .type = NFNL_CB_RCU, | |
96518518 PM |
8306 | .attr_count = NFTA_CHAIN_MAX, |
8307 | .policy = nft_chain_policy, | |
8308 | }, | |
8309 | [NFT_MSG_DELCHAIN] = { | |
50f2db9e PNA |
8310 | .call = nf_tables_delchain, |
8311 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8312 | .attr_count = NFTA_CHAIN_MAX, |
8313 | .policy = nft_chain_policy, | |
8314 | }, | |
8315 | [NFT_MSG_NEWRULE] = { | |
50f2db9e PNA |
8316 | .call = nf_tables_newrule, |
8317 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8318 | .attr_count = NFTA_RULE_MAX, |
8319 | .policy = nft_rule_policy, | |
8320 | }, | |
8321 | [NFT_MSG_GETRULE] = { | |
50f2db9e PNA |
8322 | .call = nf_tables_getrule, |
8323 | .type = NFNL_CB_RCU, | |
96518518 PM |
8324 | .attr_count = NFTA_RULE_MAX, |
8325 | .policy = nft_rule_policy, | |
8326 | }, | |
8daa8fde PS |
8327 | [NFT_MSG_GETRULE_RESET] = { |
8328 | .call = nf_tables_getrule, | |
8329 | .type = NFNL_CB_RCU, | |
8330 | .attr_count = NFTA_RULE_MAX, | |
8331 | .policy = nft_rule_policy, | |
8332 | }, | |
96518518 | 8333 | [NFT_MSG_DELRULE] = { |
50f2db9e PNA |
8334 | .call = nf_tables_delrule, |
8335 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8336 | .attr_count = NFTA_RULE_MAX, |
8337 | .policy = nft_rule_policy, | |
8338 | }, | |
20a69341 | 8339 | [NFT_MSG_NEWSET] = { |
50f2db9e PNA |
8340 | .call = nf_tables_newset, |
8341 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8342 | .attr_count = NFTA_SET_MAX, |
8343 | .policy = nft_set_policy, | |
8344 | }, | |
8345 | [NFT_MSG_GETSET] = { | |
50f2db9e PNA |
8346 | .call = nf_tables_getset, |
8347 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8348 | .attr_count = NFTA_SET_MAX, |
8349 | .policy = nft_set_policy, | |
8350 | }, | |
8351 | [NFT_MSG_DELSET] = { | |
50f2db9e PNA |
8352 | .call = nf_tables_delset, |
8353 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8354 | .attr_count = NFTA_SET_MAX, |
8355 | .policy = nft_set_policy, | |
8356 | }, | |
8357 | [NFT_MSG_NEWSETELEM] = { | |
50f2db9e PNA |
8358 | .call = nf_tables_newsetelem, |
8359 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8360 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8361 | .policy = nft_set_elem_list_policy, | |
8362 | }, | |
8363 | [NFT_MSG_GETSETELEM] = { | |
50f2db9e PNA |
8364 | .call = nf_tables_getsetelem, |
8365 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8366 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8367 | .policy = nft_set_elem_list_policy, | |
8368 | }, | |
8369 | [NFT_MSG_DELSETELEM] = { | |
50f2db9e PNA |
8370 | .call = nf_tables_delsetelem, |
8371 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8372 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8373 | .policy = nft_set_elem_list_policy, | |
8374 | }, | |
84d7fce6 | 8375 | [NFT_MSG_GETGEN] = { |
50f2db9e PNA |
8376 | .call = nf_tables_getgen, |
8377 | .type = NFNL_CB_RCU, | |
84d7fce6 | 8378 | }, |
e5009240 | 8379 | [NFT_MSG_NEWOBJ] = { |
50f2db9e PNA |
8380 | .call = nf_tables_newobj, |
8381 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8382 | .attr_count = NFTA_OBJ_MAX, |
8383 | .policy = nft_obj_policy, | |
8384 | }, | |
8385 | [NFT_MSG_GETOBJ] = { | |
50f2db9e PNA |
8386 | .call = nf_tables_getobj, |
8387 | .type = NFNL_CB_RCU, | |
e5009240 PNA |
8388 | .attr_count = NFTA_OBJ_MAX, |
8389 | .policy = nft_obj_policy, | |
8390 | }, | |
8391 | [NFT_MSG_DELOBJ] = { | |
50f2db9e PNA |
8392 | .call = nf_tables_delobj, |
8393 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8394 | .attr_count = NFTA_OBJ_MAX, |
8395 | .policy = nft_obj_policy, | |
8396 | }, | |
43da04a5 | 8397 | [NFT_MSG_GETOBJ_RESET] = { |
50f2db9e PNA |
8398 | .call = nf_tables_getobj, |
8399 | .type = NFNL_CB_RCU, | |
43da04a5 PNA |
8400 | .attr_count = NFTA_OBJ_MAX, |
8401 | .policy = nft_obj_policy, | |
8402 | }, | |
3b49e2e9 | 8403 | [NFT_MSG_NEWFLOWTABLE] = { |
50f2db9e PNA |
8404 | .call = nf_tables_newflowtable, |
8405 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8406 | .attr_count = NFTA_FLOWTABLE_MAX, |
8407 | .policy = nft_flowtable_policy, | |
8408 | }, | |
8409 | [NFT_MSG_GETFLOWTABLE] = { | |
50f2db9e PNA |
8410 | .call = nf_tables_getflowtable, |
8411 | .type = NFNL_CB_RCU, | |
3b49e2e9 PNA |
8412 | .attr_count = NFTA_FLOWTABLE_MAX, |
8413 | .policy = nft_flowtable_policy, | |
8414 | }, | |
8415 | [NFT_MSG_DELFLOWTABLE] = { | |
50f2db9e PNA |
8416 | .call = nf_tables_delflowtable, |
8417 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8418 | .attr_count = NFTA_FLOWTABLE_MAX, |
8419 | .policy = nft_flowtable_policy, | |
8420 | }, | |
96518518 PM |
8421 | }; |
8422 | ||
a654de8f PNA |
8423 | static int nf_tables_validate(struct net *net) |
8424 | { | |
d59d2f82 | 8425 | struct nftables_pernet *nft_net = nft_pernet(net); |
a654de8f PNA |
8426 | struct nft_table *table; |
8427 | ||
0854db2a | 8428 | switch (nft_net->validate_state) { |
a654de8f PNA |
8429 | case NFT_VALIDATE_SKIP: |
8430 | break; | |
8431 | case NFT_VALIDATE_NEED: | |
8432 | nft_validate_state_update(net, NFT_VALIDATE_DO); | |
954d8297 | 8433 | fallthrough; |
a654de8f | 8434 | case NFT_VALIDATE_DO: |
0854db2a | 8435 | list_for_each_entry(table, &nft_net->tables, list) { |
a654de8f PNA |
8436 | if (nft_table_validate(net, table) < 0) |
8437 | return -EAGAIN; | |
8438 | } | |
8439 | break; | |
8440 | } | |
8441 | ||
8442 | return 0; | |
8443 | } | |
8444 | ||
66293c46 FW |
8445 | /* a drop policy has to be deferred until all rules have been activated, |
8446 | * otherwise a large ruleset that contains a drop-policy base chain will | |
8447 | * cause all packets to get dropped until the full transaction has been | |
8448 | * processed. | |
8449 | * | |
8450 | * We defer the drop policy until the transaction has been finalized. | |
8451 | */ | |
8452 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
8453 | { | |
8454 | struct nft_base_chain *basechain; | |
8455 | ||
8456 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
8457 | return; | |
8458 | ||
8459 | if (!nft_is_base_chain(trans->ctx.chain)) | |
8460 | return; | |
8461 | ||
8462 | basechain = nft_base_chain(trans->ctx.chain); | |
8463 | basechain->policy = NF_DROP; | |
8464 | } | |
8465 | ||
91c7b38d PNA |
8466 | static void nft_chain_commit_update(struct nft_trans *trans) |
8467 | { | |
8468 | struct nft_base_chain *basechain; | |
8469 | ||
1b2470e5 FW |
8470 | if (nft_trans_chain_name(trans)) { |
8471 | rhltable_remove(&trans->ctx.table->chains_ht, | |
8472 | &trans->ctx.chain->rhlhead, | |
8473 | nft_chain_ht_params); | |
d71efb59 | 8474 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
8475 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
8476 | trans->ctx.chain->name, | |
8477 | &trans->ctx.chain->rhlhead, | |
8478 | nft_chain_ht_params); | |
8479 | } | |
91c7b38d | 8480 | |
f323d954 | 8481 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
8482 | return; |
8483 | ||
53315ac6 FW |
8484 | nft_chain_stats_replace(trans); |
8485 | ||
91c7b38d | 8486 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
8487 | |
8488 | switch (nft_trans_chain_policy(trans)) { | |
8489 | case NF_DROP: | |
8490 | case NF_ACCEPT: | |
8491 | basechain->policy = nft_trans_chain_policy(trans); | |
8492 | break; | |
8493 | } | |
8494 | } | |
8495 | ||
d62d0ba9 FFM |
8496 | static void nft_obj_commit_update(struct nft_trans *trans) |
8497 | { | |
8498 | struct nft_object *newobj; | |
8499 | struct nft_object *obj; | |
8500 | ||
8501 | obj = nft_trans_obj(trans); | |
8502 | newobj = nft_trans_obj_newobj(trans); | |
8503 | ||
9fedd894 FFM |
8504 | if (obj->ops->update) |
8505 | obj->ops->update(obj, newobj); | |
d62d0ba9 | 8506 | |
dad3bdee | 8507 | nft_obj_destroy(&trans->ctx, newobj); |
d62d0ba9 FFM |
8508 | } |
8509 | ||
2f99aa31 | 8510 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 8511 | { |
c7c32e72 PNA |
8512 | switch (trans->msg_type) { |
8513 | case NFT_MSG_DELTABLE: | |
8514 | nf_tables_table_destroy(&trans->ctx); | |
8515 | break; | |
9f8aac0b | 8516 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 8517 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
8518 | kfree(nft_trans_chain_name(trans)); |
8519 | break; | |
c7c32e72 | 8520 | case NFT_MSG_DELCHAIN: |
43a605f2 | 8521 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
8522 | break; |
8523 | case NFT_MSG_DELRULE: | |
8524 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
8525 | break; | |
8526 | case NFT_MSG_DELSET: | |
0c2a85ed | 8527 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 8528 | break; |
61edafbb | 8529 | case NFT_MSG_DELSETELEM: |
3453c927 PNA |
8530 | nf_tables_set_elem_destroy(&trans->ctx, |
8531 | nft_trans_elem_set(trans), | |
59105446 | 8532 | nft_trans_elem(trans).priv); |
61edafbb | 8533 | break; |
e5009240 | 8534 | case NFT_MSG_DELOBJ: |
00bfb320 | 8535 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 8536 | break; |
3b49e2e9 | 8537 | case NFT_MSG_DELFLOWTABLE: |
abadb2f8 PNA |
8538 | if (nft_trans_flowtable_update(trans)) |
8539 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
8540 | else | |
8541 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 8542 | break; |
c7c32e72 | 8543 | } |
0935d558 FW |
8544 | |
8545 | if (trans->put_net) | |
8546 | put_net(trans->ctx.net); | |
8547 | ||
c7c32e72 PNA |
8548 | kfree(trans); |
8549 | } | |
8550 | ||
0935d558 | 8551 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
8552 | { |
8553 | struct nft_trans *trans, *next; | |
0935d558 FW |
8554 | LIST_HEAD(head); |
8555 | ||
8556 | spin_lock(&nf_tables_destroy_list_lock); | |
8557 | list_splice_init(&nf_tables_destroy_list, &head); | |
8558 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 8559 | |
0935d558 | 8560 | if (list_empty(&head)) |
2f99aa31 FW |
8561 | return; |
8562 | ||
8563 | synchronize_rcu(); | |
8564 | ||
0935d558 | 8565 | list_for_each_entry_safe(trans, next, &head, list) { |
2f99aa31 FW |
8566 | list_del(&trans->list); |
8567 | nft_commit_release(trans); | |
8568 | } | |
8569 | } | |
8570 | ||
ffe8923f FW |
8571 | void nf_tables_trans_destroy_flush_work(void) |
8572 | { | |
8573 | flush_work(&trans_destroy_work); | |
8574 | } | |
8575 | EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work); | |
8576 | ||
ed5f85d4 PNA |
8577 | static bool nft_expr_reduce(struct nft_regs_track *track, |
8578 | const struct nft_expr *expr) | |
8579 | { | |
9e539c5b | 8580 | return false; |
ed5f85d4 PNA |
8581 | } |
8582 | ||
0cbc06b3 FW |
8583 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
8584 | { | |
2c865a8a | 8585 | const struct nft_expr *expr, *last; |
12e4ecfa | 8586 | struct nft_regs_track track = {}; |
2c865a8a PNA |
8587 | unsigned int size, data_size; |
8588 | void *data, *data_boundary; | |
8589 | struct nft_rule_dp *prule; | |
0cbc06b3 | 8590 | struct nft_rule *rule; |
0cbc06b3 FW |
8591 | |
8592 | /* already handled or inactive chain? */ | |
2c865a8a | 8593 | if (chain->blob_next || !nft_is_active_next(net, chain)) |
0cbc06b3 FW |
8594 | return 0; |
8595 | ||
63045bfd | 8596 | data_size = 0; |
10377d42 | 8597 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
8598 | if (nft_is_active_next(net, rule)) { |
8599 | data_size += sizeof(*prule) + rule->dlen; | |
8600 | if (data_size > INT_MAX) | |
8601 | return -ENOMEM; | |
8602 | } | |
0cbc06b3 | 8603 | } |
2c865a8a | 8604 | data_size += offsetof(struct nft_rule_dp, data); /* last rule */ |
0cbc06b3 | 8605 | |
2c865a8a PNA |
8606 | chain->blob_next = nf_tables_chain_alloc_rules(data_size); |
8607 | if (!chain->blob_next) | |
0cbc06b3 FW |
8608 | return -ENOMEM; |
8609 | ||
2c865a8a PNA |
8610 | data = (void *)chain->blob_next->data; |
8611 | data_boundary = data + data_size; | |
8612 | size = 0; | |
8613 | ||
10377d42 | 8614 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
8615 | if (!nft_is_active_next(net, rule)) |
8616 | continue; | |
8617 | ||
8618 | prule = (struct nft_rule_dp *)data; | |
8619 | data += offsetof(struct nft_rule_dp, data); | |
8620 | if (WARN_ON_ONCE(data > data_boundary)) | |
8621 | return -ENOMEM; | |
8622 | ||
12e4ecfa | 8623 | size = 0; |
fe75e84a | 8624 | track.last = nft_expr_last(rule); |
2c865a8a | 8625 | nft_rule_for_each_expr(expr, last, rule) { |
12e4ecfa PNA |
8626 | track.cur = expr; |
8627 | ||
ed5f85d4 | 8628 | if (nft_expr_reduce(&track, expr)) { |
12e4ecfa PNA |
8629 | expr = track.cur; |
8630 | continue; | |
8631 | } | |
8632 | ||
2c865a8a PNA |
8633 | if (WARN_ON_ONCE(data + expr->ops->size > data_boundary)) |
8634 | return -ENOMEM; | |
8635 | ||
8636 | memcpy(data + size, expr, expr->ops->size); | |
8637 | size += expr->ops->size; | |
8638 | } | |
8639 | if (WARN_ON_ONCE(size >= 1 << 12)) | |
8640 | return -ENOMEM; | |
8641 | ||
8642 | prule->handle = rule->handle; | |
8643 | prule->dlen = size; | |
8644 | prule->is_last = 0; | |
8645 | ||
8646 | data += size; | |
8647 | size = 0; | |
8648 | chain->blob_next->size += (unsigned long)(data - (void *)prule); | |
0cbc06b3 FW |
8649 | } |
8650 | ||
2c865a8a PNA |
8651 | prule = (struct nft_rule_dp *)data; |
8652 | data += offsetof(struct nft_rule_dp, data); | |
8653 | if (WARN_ON_ONCE(data > data_boundary)) | |
8654 | return -ENOMEM; | |
8655 | ||
8656 | nft_last_rule(chain->blob_next, prule); | |
8657 | ||
0cbc06b3 FW |
8658 | return 0; |
8659 | } | |
8660 | ||
8661 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
8662 | { | |
d59d2f82 | 8663 | struct nftables_pernet *nft_net = nft_pernet(net); |
0cbc06b3 FW |
8664 | struct nft_trans *trans, *next; |
8665 | ||
0854db2a | 8666 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 FW |
8667 | struct nft_chain *chain = trans->ctx.chain; |
8668 | ||
8669 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
8670 | trans->msg_type == NFT_MSG_DELRULE) { | |
2c865a8a PNA |
8671 | kvfree(chain->blob_next); |
8672 | chain->blob_next = NULL; | |
0cbc06b3 FW |
8673 | } |
8674 | } | |
8675 | } | |
8676 | ||
8677 | static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h) | |
8678 | { | |
8679 | struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); | |
8680 | ||
2c865a8a | 8681 | kvfree(o->blob); |
0cbc06b3 FW |
8682 | } |
8683 | ||
2c865a8a | 8684 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob) |
0cbc06b3 | 8685 | { |
0cbc06b3 FW |
8686 | struct nft_rules_old *old; |
8687 | ||
2c865a8a PNA |
8688 | /* rcu_head is after end marker */ |
8689 | old = (void *)blob + sizeof(*blob) + blob->size; | |
8690 | old->blob = blob; | |
0cbc06b3 FW |
8691 | |
8692 | call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old); | |
8693 | } | |
8694 | ||
0fb39bbe | 8695 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 | 8696 | { |
2c865a8a | 8697 | struct nft_rule_blob *g0, *g1; |
0cbc06b3 FW |
8698 | bool next_genbit; |
8699 | ||
8700 | next_genbit = nft_gencursor_next(net); | |
8701 | ||
2c865a8a | 8702 | g0 = rcu_dereference_protected(chain->blob_gen_0, |
f102d66b | 8703 | lockdep_commit_lock_is_held(net)); |
2c865a8a | 8704 | g1 = rcu_dereference_protected(chain->blob_gen_1, |
f102d66b | 8705 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
8706 | |
8707 | /* No changes to this chain? */ | |
2c865a8a | 8708 | if (chain->blob_next == NULL) { |
0cbc06b3 FW |
8709 | /* chain had no change in last or next generation */ |
8710 | if (g0 == g1) | |
8711 | return; | |
8712 | /* | |
8713 | * chain had no change in this generation; make sure next | |
8714 | * one uses same rules as current generation. | |
8715 | */ | |
8716 | if (next_genbit) { | |
2c865a8a | 8717 | rcu_assign_pointer(chain->blob_gen_1, g0); |
0cbc06b3 FW |
8718 | nf_tables_commit_chain_free_rules_old(g1); |
8719 | } else { | |
2c865a8a | 8720 | rcu_assign_pointer(chain->blob_gen_0, g1); |
0cbc06b3 FW |
8721 | nf_tables_commit_chain_free_rules_old(g0); |
8722 | } | |
8723 | ||
8724 | return; | |
8725 | } | |
8726 | ||
8727 | if (next_genbit) | |
2c865a8a | 8728 | rcu_assign_pointer(chain->blob_gen_1, chain->blob_next); |
0cbc06b3 | 8729 | else |
2c865a8a | 8730 | rcu_assign_pointer(chain->blob_gen_0, chain->blob_next); |
0cbc06b3 | 8731 | |
2c865a8a | 8732 | chain->blob_next = NULL; |
0cbc06b3 FW |
8733 | |
8734 | if (g0 == g1) | |
8735 | return; | |
8736 | ||
8737 | if (next_genbit) | |
8738 | nf_tables_commit_chain_free_rules_old(g1); | |
8739 | else | |
8740 | nf_tables_commit_chain_free_rules_old(g0); | |
8741 | } | |
8742 | ||
d152159b FW |
8743 | static void nft_obj_del(struct nft_object *obj) |
8744 | { | |
4d44175a | 8745 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
8746 | list_del_rcu(&obj->list); |
8747 | } | |
8748 | ||
d0e2c7de | 8749 | void nft_chain_del(struct nft_chain *chain) |
1b2470e5 FW |
8750 | { |
8751 | struct nft_table *table = chain->table; | |
8752 | ||
8753 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
8754 | nft_chain_ht_params)); | |
8755 | list_del_rcu(&chain->list); | |
8756 | } | |
8757 | ||
eb014de4 PNA |
8758 | static void nf_tables_module_autoload_cleanup(struct net *net) |
8759 | { | |
d59d2f82 | 8760 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
8761 | struct nft_module_request *req, *next; |
8762 | ||
0854db2a FW |
8763 | WARN_ON_ONCE(!list_empty(&nft_net->commit_list)); |
8764 | list_for_each_entry_safe(req, next, &nft_net->module_list, list) { | |
eb014de4 PNA |
8765 | WARN_ON_ONCE(!req->done); |
8766 | list_del(&req->list); | |
8767 | kfree(req); | |
8768 | } | |
8769 | } | |
8770 | ||
0935d558 FW |
8771 | static void nf_tables_commit_release(struct net *net) |
8772 | { | |
d59d2f82 | 8773 | struct nftables_pernet *nft_net = nft_pernet(net); |
0935d558 FW |
8774 | struct nft_trans *trans; |
8775 | ||
8776 | /* all side effects have to be made visible. | |
8777 | * For example, if a chain named 'foo' has been deleted, a | |
8778 | * new transaction must not find it anymore. | |
8779 | * | |
8780 | * Memory reclaim happens asynchronously from work queue | |
8781 | * to prevent expensive synchronize_rcu() in commit phase. | |
8782 | */ | |
0854db2a | 8783 | if (list_empty(&nft_net->commit_list)) { |
eb014de4 | 8784 | nf_tables_module_autoload_cleanup(net); |
0854db2a | 8785 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
8786 | return; |
8787 | } | |
8788 | ||
0854db2a | 8789 | trans = list_last_entry(&nft_net->commit_list, |
0935d558 FW |
8790 | struct nft_trans, list); |
8791 | get_net(trans->ctx.net); | |
8792 | WARN_ON_ONCE(trans->put_net); | |
8793 | ||
8794 | trans->put_net = true; | |
8795 | spin_lock(&nf_tables_destroy_list_lock); | |
0854db2a | 8796 | list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list); |
0935d558 FW |
8797 | spin_unlock(&nf_tables_destroy_list_lock); |
8798 | ||
eb014de4 | 8799 | nf_tables_module_autoload_cleanup(net); |
0935d558 | 8800 | schedule_work(&trans_destroy_work); |
ffe8923f | 8801 | |
0854db2a | 8802 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
8803 | } |
8804 | ||
67cc570e PNA |
8805 | static void nft_commit_notify(struct net *net, u32 portid) |
8806 | { | |
d59d2f82 | 8807 | struct nftables_pernet *nft_net = nft_pernet(net); |
67cc570e PNA |
8808 | struct sk_buff *batch_skb = NULL, *nskb, *skb; |
8809 | unsigned char *data; | |
8810 | int len; | |
8811 | ||
0854db2a | 8812 | list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) { |
67cc570e PNA |
8813 | if (!batch_skb) { |
8814 | new_batch: | |
8815 | batch_skb = skb; | |
8816 | len = NLMSG_GOODSIZE - skb->len; | |
8817 | list_del(&skb->list); | |
8818 | continue; | |
8819 | } | |
8820 | len -= skb->len; | |
8821 | if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) { | |
8822 | data = skb_put(batch_skb, skb->len); | |
8823 | memcpy(data, skb->data, skb->len); | |
8824 | list_del(&skb->list); | |
8825 | kfree_skb(skb); | |
8826 | continue; | |
8827 | } | |
8828 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
8829 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
8830 | goto new_batch; | |
8831 | } | |
8832 | ||
8833 | if (batch_skb) { | |
8834 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
8835 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
8836 | } | |
8837 | ||
0854db2a | 8838 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); |
67cc570e PNA |
8839 | } |
8840 | ||
c520292f RGB |
8841 | static int nf_tables_commit_audit_alloc(struct list_head *adl, |
8842 | struct nft_table *table) | |
8843 | { | |
8844 | struct nft_audit_data *adp; | |
8845 | ||
8846 | list_for_each_entry(adp, adl, list) { | |
8847 | if (adp->table == table) | |
8848 | return 0; | |
8849 | } | |
8850 | adp = kzalloc(sizeof(*adp), GFP_KERNEL); | |
8851 | if (!adp) | |
8852 | return -ENOMEM; | |
8853 | adp->table = table; | |
8854 | list_add(&adp->list, adl); | |
8855 | return 0; | |
8856 | } | |
8857 | ||
cfbe3650 DM |
8858 | static void nf_tables_commit_audit_free(struct list_head *adl) |
8859 | { | |
8860 | struct nft_audit_data *adp, *adn; | |
8861 | ||
8862 | list_for_each_entry_safe(adp, adn, adl, list) { | |
8863 | list_del(&adp->list); | |
8864 | kfree(adp); | |
8865 | } | |
8866 | } | |
8867 | ||
c520292f RGB |
8868 | static void nf_tables_commit_audit_collect(struct list_head *adl, |
8869 | struct nft_table *table, u32 op) | |
8870 | { | |
8871 | struct nft_audit_data *adp; | |
8872 | ||
8873 | list_for_each_entry(adp, adl, list) { | |
8874 | if (adp->table == table) | |
8875 | goto found; | |
8876 | } | |
dadf33c9 | 8877 | WARN_ONCE(1, "table=%s not expected in commit list", table->name); |
c520292f RGB |
8878 | return; |
8879 | found: | |
8880 | adp->entries++; | |
8881 | if (!adp->op || adp->op > op) | |
8882 | adp->op = op; | |
8883 | } | |
8884 | ||
8885 | #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22) | |
8886 | ||
8887 | static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation) | |
8888 | { | |
8889 | struct nft_audit_data *adp, *adn; | |
8890 | char aubuf[AUNFTABLENAMELEN]; | |
8891 | ||
8892 | list_for_each_entry_safe(adp, adn, adl, list) { | |
8893 | snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name, | |
8894 | generation); | |
8895 | audit_log_nfcfg(aubuf, adp->table->family, adp->entries, | |
8896 | nft2audit_op[adp->op], GFP_KERNEL); | |
8897 | list_del(&adp->list); | |
8898 | kfree(adp); | |
8899 | } | |
8900 | } | |
8901 | ||
5913beaf | 8902 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 8903 | { |
d59d2f82 | 8904 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 8905 | struct nft_trans *trans, *next; |
a3716e70 | 8906 | struct nft_trans_elem *te; |
0cbc06b3 FW |
8907 | struct nft_chain *chain; |
8908 | struct nft_table *table; | |
34002783 | 8909 | unsigned int base_seq; |
c520292f | 8910 | LIST_HEAD(adl); |
c9626a2c | 8911 | int err; |
37082f93 | 8912 | |
0854db2a FW |
8913 | if (list_empty(&nft_net->commit_list)) { |
8914 | mutex_unlock(&nft_net->commit_mutex); | |
b8b27498 FW |
8915 | return 0; |
8916 | } | |
8917 | ||
a654de8f PNA |
8918 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
8919 | if (nf_tables_validate(net) < 0) | |
8920 | return -EAGAIN; | |
8921 | ||
c9626a2c PNA |
8922 | err = nft_flow_rule_offload_commit(net); |
8923 | if (err < 0) | |
8924 | return err; | |
8925 | ||
0cbc06b3 | 8926 | /* 1. Allocate space for next generation rules_gen_X[] */ |
0854db2a | 8927 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 | 8928 | int ret; |
37082f93 | 8929 | |
c520292f RGB |
8930 | ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table); |
8931 | if (ret) { | |
8932 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 8933 | nf_tables_commit_audit_free(&adl); |
c520292f RGB |
8934 | return ret; |
8935 | } | |
0cbc06b3 FW |
8936 | if (trans->msg_type == NFT_MSG_NEWRULE || |
8937 | trans->msg_type == NFT_MSG_DELRULE) { | |
8938 | chain = trans->ctx.chain; | |
8939 | ||
8940 | ret = nf_tables_commit_chain_prepare(net, chain); | |
8941 | if (ret < 0) { | |
8942 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 8943 | nf_tables_commit_audit_free(&adl); |
0cbc06b3 FW |
8944 | return ret; |
8945 | } | |
8946 | } | |
8947 | } | |
37082f93 | 8948 | |
0cbc06b3 | 8949 | /* step 2. Make rules_gen_X visible to packet path */ |
0854db2a | 8950 | list_for_each_entry(table, &nft_net->tables, list) { |
0fb39bbe FW |
8951 | list_for_each_entry(chain, &table->chains, list) |
8952 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
8953 | } |
8954 | ||
8955 | /* | |
8956 | * Bump generation counter, invalidate any dump in progress. | |
8957 | * Cannot fail after this point. | |
37082f93 | 8958 | */ |
34002783 PNA |
8959 | base_seq = READ_ONCE(nft_net->base_seq); |
8960 | while (++base_seq == 0) | |
0854db2a | 8961 | ; |
0cbc06b3 | 8962 | |
34002783 PNA |
8963 | WRITE_ONCE(nft_net->base_seq, base_seq); |
8964 | ||
0cbc06b3 FW |
8965 | /* step 3. Start new generation, rules_gen_X now in use. */ |
8966 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 | 8967 | |
0854db2a | 8968 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
c520292f RGB |
8969 | nf_tables_commit_audit_collect(&adl, trans->ctx.table, |
8970 | trans->msg_type); | |
b380e5c7 | 8971 | switch (trans->msg_type) { |
55dd6f93 PNA |
8972 | case NFT_MSG_NEWTABLE: |
8973 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
8974 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
8975 | nft_trans_destroy(trans); | |
8976 | break; | |
8977 | } | |
8978 | if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT) | |
0ce7cf41 PNA |
8979 | nf_tables_table_disable(net, trans->ctx.table); |
8980 | ||
179d9ba5 | 8981 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; |
55dd6f93 | 8982 | } else { |
f2a6d766 | 8983 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 8984 | } |
35151d84 | 8985 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
8986 | nft_trans_destroy(trans); |
8987 | break; | |
8988 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 8989 | list_del_rcu(&trans->ctx.table->list); |
35151d84 | 8990 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
55dd6f93 | 8991 | break; |
91c7b38d | 8992 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 8993 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 8994 | nft_chain_commit_update(trans); |
9f8aac0b FW |
8995 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
8996 | /* trans destroyed after rcu grace period */ | |
8997 | } else { | |
66293c46 | 8998 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 8999 | nft_clear(net, trans->ctx.chain); |
9f8aac0b FW |
9000 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
9001 | nft_trans_destroy(trans); | |
9002 | } | |
91c7b38d PNA |
9003 | break; |
9004 | case NFT_MSG_DELCHAIN: | |
1b2470e5 | 9005 | nft_chain_del(trans->ctx.chain); |
35151d84 | 9006 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
c974a3a3 PNA |
9007 | nf_tables_unregister_hook(trans->ctx.net, |
9008 | trans->ctx.table, | |
9009 | trans->ctx.chain); | |
91c7b38d | 9010 | break; |
b380e5c7 | 9011 | case NFT_MSG_NEWRULE: |
889f7ee7 | 9012 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 9013 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 9014 | nft_trans_rule(trans), |
35151d84 | 9015 | NFT_MSG_NEWRULE); |
9dd732e0 PNA |
9016 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9017 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9018 | ||
37082f93 | 9019 | nft_trans_destroy(trans); |
b380e5c7 PNA |
9020 | break; |
9021 | case NFT_MSG_DELRULE: | |
9022 | list_del_rcu(&nft_trans_rule(trans)->list); | |
35151d84 PNA |
9023 | nf_tables_rule_notify(&trans->ctx, |
9024 | nft_trans_rule(trans), | |
9025 | NFT_MSG_DELRULE); | |
f6ac8585 PNA |
9026 | nft_rule_expr_deactivate(&trans->ctx, |
9027 | nft_trans_rule(trans), | |
9028 | NFT_TRANS_COMMIT); | |
26b5934f PNA |
9029 | |
9030 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) | |
9031 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 | 9032 | break; |
958bee14 | 9033 | case NFT_MSG_NEWSET: |
37a9cc52 | 9034 | nft_clear(net, nft_trans_set(trans)); |
4fefee57 PNA |
9035 | /* This avoids hitting -EBUSY when deleting the table |
9036 | * from the transaction. | |
9037 | */ | |
408070d6 | 9038 | if (nft_set_is_anonymous(nft_trans_set(trans)) && |
4fefee57 PNA |
9039 | !list_empty(&nft_trans_set(trans)->bindings)) |
9040 | trans->ctx.table->use--; | |
9041 | ||
958bee14 | 9042 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 9043 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
9044 | nft_trans_destroy(trans); |
9045 | break; | |
9046 | case NFT_MSG_DELSET: | |
37a9cc52 | 9047 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 9048 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 9049 | NFT_MSG_DELSET, GFP_KERNEL); |
958bee14 | 9050 | break; |
60319eb1 | 9051 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
9052 | te = (struct nft_trans_elem *)trans->data; |
9053 | ||
aaa31047 | 9054 | nft_setelem_activate(net, te->set, &te->elem); |
cc02e457 PM |
9055 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9056 | &te->elem, | |
6fb721cf | 9057 | NFT_MSG_NEWSETELEM); |
60319eb1 PNA |
9058 | nft_trans_destroy(trans); |
9059 | break; | |
9060 | case NFT_MSG_DELSETELEM: | |
a3716e70 | 9061 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 9062 | |
a3716e70 PNA |
9063 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9064 | &te->elem, | |
6fb721cf | 9065 | NFT_MSG_DELSETELEM); |
aaa31047 PNA |
9066 | nft_setelem_remove(net, te->set, &te->elem); |
9067 | if (!nft_setelem_is_catchall(te->set, &te->elem)) { | |
9068 | atomic_dec(&te->set->nelems); | |
9069 | te->set->ndeact--; | |
9070 | } | |
60319eb1 | 9071 | break; |
e5009240 | 9072 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
9073 | if (nft_trans_obj_update(trans)) { |
9074 | nft_obj_commit_update(trans); | |
9075 | nf_tables_obj_notify(&trans->ctx, | |
9076 | nft_trans_obj(trans), | |
9077 | NFT_MSG_NEWOBJ); | |
9078 | } else { | |
9079 | nft_clear(net, nft_trans_obj(trans)); | |
9080 | nf_tables_obj_notify(&trans->ctx, | |
9081 | nft_trans_obj(trans), | |
9082 | NFT_MSG_NEWOBJ); | |
9083 | nft_trans_destroy(trans); | |
9084 | } | |
e5009240 PNA |
9085 | break; |
9086 | case NFT_MSG_DELOBJ: | |
d152159b | 9087 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 PNA |
9088 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
9089 | NFT_MSG_DELOBJ); | |
9090 | break; | |
3b49e2e9 | 9091 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 9092 | if (nft_trans_flowtable_update(trans)) { |
7b35582c PNA |
9093 | nft_trans_flowtable(trans)->data.flags = |
9094 | nft_trans_flowtable_flags(trans); | |
78d9f48f PNA |
9095 | nf_tables_flowtable_notify(&trans->ctx, |
9096 | nft_trans_flowtable(trans), | |
9097 | &nft_trans_flowtable_hooks(trans), | |
9098 | NFT_MSG_NEWFLOWTABLE); | |
9099 | list_splice(&nft_trans_flowtable_hooks(trans), | |
9100 | &nft_trans_flowtable(trans)->hook_list); | |
9101 | } else { | |
9102 | nft_clear(net, nft_trans_flowtable(trans)); | |
9103 | nf_tables_flowtable_notify(&trans->ctx, | |
9104 | nft_trans_flowtable(trans), | |
9105 | &nft_trans_flowtable(trans)->hook_list, | |
9106 | NFT_MSG_NEWFLOWTABLE); | |
9107 | } | |
3b49e2e9 PNA |
9108 | nft_trans_destroy(trans); |
9109 | break; | |
9110 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 | 9111 | if (nft_trans_flowtable_update(trans)) { |
abadb2f8 PNA |
9112 | nf_tables_flowtable_notify(&trans->ctx, |
9113 | nft_trans_flowtable(trans), | |
9114 | &nft_trans_flowtable_hooks(trans), | |
9115 | NFT_MSG_DELFLOWTABLE); | |
9116 | nft_unregister_flowtable_net_hooks(net, | |
9117 | &nft_trans_flowtable_hooks(trans)); | |
9118 | } else { | |
9119 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9120 | nf_tables_flowtable_notify(&trans->ctx, | |
9121 | nft_trans_flowtable(trans), | |
9122 | &nft_trans_flowtable(trans)->hook_list, | |
9123 | NFT_MSG_DELFLOWTABLE); | |
9124 | nft_unregister_flowtable_net_hooks(net, | |
9125 | &nft_trans_flowtable(trans)->hook_list); | |
9126 | } | |
3b49e2e9 | 9127 | break; |
37082f93 | 9128 | } |
37082f93 PNA |
9129 | } |
9130 | ||
67cc570e | 9131 | nft_commit_notify(net, NETLINK_CB(skb).portid); |
84d7fce6 | 9132 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0854db2a | 9133 | nf_tables_commit_audit_log(&adl, nft_net->base_seq); |
0935d558 | 9134 | nf_tables_commit_release(net); |
37082f93 PNA |
9135 | |
9136 | return 0; | |
9137 | } | |
9138 | ||
eb014de4 PNA |
9139 | static void nf_tables_module_autoload(struct net *net) |
9140 | { | |
d59d2f82 | 9141 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
9142 | struct nft_module_request *req, *next; |
9143 | LIST_HEAD(module_list); | |
9144 | ||
0854db2a FW |
9145 | list_splice_init(&nft_net->module_list, &module_list); |
9146 | mutex_unlock(&nft_net->commit_mutex); | |
eb014de4 | 9147 | list_for_each_entry_safe(req, next, &module_list, list) { |
1d305ba4 FW |
9148 | request_module("%s", req->module); |
9149 | req->done = true; | |
eb014de4 | 9150 | } |
0854db2a FW |
9151 | mutex_lock(&nft_net->commit_mutex); |
9152 | list_splice(&module_list, &nft_net->module_list); | |
eb014de4 PNA |
9153 | } |
9154 | ||
b326dd37 | 9155 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 9156 | { |
c7c32e72 PNA |
9157 | switch (trans->msg_type) { |
9158 | case NFT_MSG_NEWTABLE: | |
9159 | nf_tables_table_destroy(&trans->ctx); | |
9160 | break; | |
9161 | case NFT_MSG_NEWCHAIN: | |
43a605f2 | 9162 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
9163 | break; |
9164 | case NFT_MSG_NEWRULE: | |
9165 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
9166 | break; | |
9167 | case NFT_MSG_NEWSET: | |
0c2a85ed | 9168 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 9169 | break; |
61edafbb PM |
9170 | case NFT_MSG_NEWSETELEM: |
9171 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 9172 | nft_trans_elem(trans).priv, true); |
61edafbb | 9173 | break; |
e5009240 | 9174 | case NFT_MSG_NEWOBJ: |
00bfb320 | 9175 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 9176 | break; |
3b49e2e9 | 9177 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9178 | if (nft_trans_flowtable_update(trans)) |
9179 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
9180 | else | |
9181 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 9182 | break; |
c7c32e72 PNA |
9183 | } |
9184 | kfree(trans); | |
9185 | } | |
9186 | ||
c0391b6a | 9187 | static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) |
37082f93 | 9188 | { |
d59d2f82 | 9189 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 9190 | struct nft_trans *trans, *next; |
02263db0 | 9191 | struct nft_trans_elem *te; |
37082f93 | 9192 | |
c0391b6a PNA |
9193 | if (action == NFNL_ABORT_VALIDATE && |
9194 | nf_tables_validate(net) < 0) | |
9195 | return -EAGAIN; | |
9196 | ||
0854db2a | 9197 | list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list, |
a907e36d | 9198 | list) { |
b380e5c7 | 9199 | switch (trans->msg_type) { |
55dd6f93 PNA |
9200 | case NFT_MSG_NEWTABLE: |
9201 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
9202 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
9203 | nft_trans_destroy(trans); | |
9204 | break; | |
9205 | } | |
9206 | if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) { | |
0ce7cf41 | 9207 | nf_tables_table_disable(net, trans->ctx.table); |
179d9ba5 PNA |
9208 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
9209 | } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) { | |
9210 | trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT; | |
9211 | } | |
9212 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; | |
55dd6f93 PNA |
9213 | nft_trans_destroy(trans); |
9214 | } else { | |
e688a7f8 | 9215 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
9216 | } |
9217 | break; | |
9218 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 9219 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
9220 | nft_trans_destroy(trans); |
9221 | break; | |
91c7b38d PNA |
9222 | case NFT_MSG_NEWCHAIN: |
9223 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 9224 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 9225 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
9226 | nft_trans_destroy(trans); |
9227 | } else { | |
d0e2c7de PNA |
9228 | if (nft_chain_is_bound(trans->ctx.chain)) { |
9229 | nft_trans_destroy(trans); | |
9230 | break; | |
9231 | } | |
4fefee57 | 9232 | trans->ctx.table->use--; |
1b2470e5 | 9233 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
9234 | nf_tables_unregister_hook(trans->ctx.net, |
9235 | trans->ctx.table, | |
9236 | trans->ctx.chain); | |
91c7b38d PNA |
9237 | } |
9238 | break; | |
9239 | case NFT_MSG_DELCHAIN: | |
4fefee57 | 9240 | trans->ctx.table->use++; |
664b0f8c | 9241 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
9242 | nft_trans_destroy(trans); |
9243 | break; | |
b380e5c7 | 9244 | case NFT_MSG_NEWRULE: |
4fefee57 | 9245 | trans->ctx.chain->use--; |
b380e5c7 | 9246 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
9247 | nft_rule_expr_deactivate(&trans->ctx, |
9248 | nft_trans_rule(trans), | |
9249 | NFT_TRANS_ABORT); | |
3c5e4462 PNA |
9250 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9251 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 PNA |
9252 | break; |
9253 | case NFT_MSG_DELRULE: | |
4fefee57 | 9254 | trans->ctx.chain->use++; |
889f7ee7 | 9255 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 9256 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
3c5e4462 PNA |
9257 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9258 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9259 | ||
37082f93 | 9260 | nft_trans_destroy(trans); |
b380e5c7 | 9261 | break; |
958bee14 | 9262 | case NFT_MSG_NEWSET: |
4fefee57 | 9263 | trans->ctx.table->use--; |
6a0a8d10 | 9264 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
9265 | nft_trans_destroy(trans); |
9266 | break; | |
9267 | } | |
9268 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
9269 | break; |
9270 | case NFT_MSG_DELSET: | |
4fefee57 | 9271 | trans->ctx.table->use++; |
37a9cc52 | 9272 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
9273 | nft_trans_destroy(trans); |
9274 | break; | |
60319eb1 | 9275 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 9276 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
9277 | nft_trans_destroy(trans); |
9278 | break; | |
9279 | } | |
02263db0 | 9280 | te = (struct nft_trans_elem *)trans->data; |
aaa31047 PNA |
9281 | nft_setelem_remove(net, te->set, &te->elem); |
9282 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9283 | atomic_dec(&te->set->nelems); | |
60319eb1 PNA |
9284 | break; |
9285 | case NFT_MSG_DELSETELEM: | |
cc02e457 PM |
9286 | te = (struct nft_trans_elem *)trans->data; |
9287 | ||
f8bb7889 | 9288 | nft_setelem_data_activate(net, te->set, &te->elem); |
aaa31047 PNA |
9289 | nft_setelem_activate(net, te->set, &te->elem); |
9290 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9291 | te->set->ndeact--; | |
cc02e457 | 9292 | |
e5009240 PNA |
9293 | nft_trans_destroy(trans); |
9294 | break; | |
9295 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 | 9296 | if (nft_trans_obj_update(trans)) { |
dad3bdee | 9297 | nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans)); |
d62d0ba9 FFM |
9298 | nft_trans_destroy(trans); |
9299 | } else { | |
9300 | trans->ctx.table->use--; | |
9301 | nft_obj_del(nft_trans_obj(trans)); | |
9302 | } | |
e5009240 PNA |
9303 | break; |
9304 | case NFT_MSG_DELOBJ: | |
9305 | trans->ctx.table->use++; | |
9306 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
9307 | nft_trans_destroy(trans); |
9308 | break; | |
3b49e2e9 | 9309 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9310 | if (nft_trans_flowtable_update(trans)) { |
9311 | nft_unregister_flowtable_net_hooks(net, | |
9312 | &nft_trans_flowtable_hooks(trans)); | |
9313 | } else { | |
9314 | trans->ctx.table->use--; | |
9315 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9316 | nft_unregister_flowtable_net_hooks(net, | |
9317 | &nft_trans_flowtable(trans)->hook_list); | |
9318 | } | |
3b49e2e9 PNA |
9319 | break; |
9320 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 | 9321 | if (nft_trans_flowtable_update(trans)) { |
b6d9014a PNA |
9322 | list_splice(&nft_trans_flowtable_hooks(trans), |
9323 | &nft_trans_flowtable(trans)->hook_list); | |
abadb2f8 PNA |
9324 | } else { |
9325 | trans->ctx.table->use++; | |
9326 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
9327 | } | |
3b49e2e9 PNA |
9328 | nft_trans_destroy(trans); |
9329 | break; | |
37082f93 | 9330 | } |
37082f93 PNA |
9331 | } |
9332 | ||
b326dd37 PNA |
9333 | synchronize_rcu(); |
9334 | ||
a1cee076 | 9335 | list_for_each_entry_safe_reverse(trans, next, |
0854db2a | 9336 | &nft_net->commit_list, list) { |
c7c32e72 | 9337 | list_del(&trans->list); |
b326dd37 | 9338 | nf_tables_abort_release(trans); |
37082f93 PNA |
9339 | } |
9340 | ||
c0391b6a | 9341 | if (action == NFNL_ABORT_AUTOLOAD) |
eb014de4 PNA |
9342 | nf_tables_module_autoload(net); |
9343 | else | |
9344 | nf_tables_module_autoload_cleanup(net); | |
9345 | ||
37082f93 PNA |
9346 | return 0; |
9347 | } | |
9348 | ||
a654de8f PNA |
9349 | static void nf_tables_cleanup(struct net *net) |
9350 | { | |
9351 | nft_validate_state_update(net, NFT_VALIDATE_SKIP); | |
9352 | } | |
9353 | ||
c0391b6a PNA |
9354 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, |
9355 | enum nfnl_abort_action action) | |
71ad00c5 | 9356 | { |
d59d2f82 | 9357 | struct nftables_pernet *nft_net = nft_pernet(net); |
c0391b6a | 9358 | int ret = __nf_tables_abort(net, action); |
f102d66b | 9359 | |
0854db2a | 9360 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9361 | |
9362 | return ret; | |
71ad00c5 FW |
9363 | } |
9364 | ||
74e8bcd2 PNA |
9365 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
9366 | { | |
d59d2f82 | 9367 | struct nftables_pernet *nft_net = nft_pernet(net); |
f102d66b FW |
9368 | bool genid_ok; |
9369 | ||
0854db2a | 9370 | mutex_lock(&nft_net->commit_mutex); |
f102d66b | 9371 | |
0854db2a | 9372 | genid_ok = genid == 0 || nft_net->base_seq == genid; |
f102d66b | 9373 | if (!genid_ok) |
0854db2a | 9374 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9375 | |
9376 | /* else, commit mutex has to be released by commit or abort function */ | |
9377 | return genid_ok; | |
74e8bcd2 PNA |
9378 | } |
9379 | ||
96518518 PM |
9380 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
9381 | .name = "nf_tables", | |
9382 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
9383 | .cb_count = NFT_MSG_MAX, | |
9384 | .cb = nf_tables_cb, | |
0628b123 PNA |
9385 | .commit = nf_tables_commit, |
9386 | .abort = nf_tables_abort, | |
a654de8f | 9387 | .cleanup = nf_tables_cleanup, |
74e8bcd2 | 9388 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 9389 | .owner = THIS_MODULE, |
96518518 PM |
9390 | }; |
9391 | ||
7210e4e3 | 9392 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 9393 | enum nft_chain_types type) |
7210e4e3 PNA |
9394 | { |
9395 | const struct nft_base_chain *basechain; | |
9396 | ||
f323d954 | 9397 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
9398 | basechain = nft_base_chain(chain); |
9399 | if (basechain->type->type != type) | |
9400 | return -EOPNOTSUPP; | |
9401 | } | |
9402 | return 0; | |
9403 | } | |
9404 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
9405 | ||
75e8d06d PNA |
9406 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
9407 | unsigned int hook_flags) | |
9408 | { | |
9409 | struct nft_base_chain *basechain; | |
9410 | ||
f323d954 | 9411 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
9412 | basechain = nft_base_chain(chain); |
9413 | ||
c974a3a3 | 9414 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
9415 | return 0; |
9416 | ||
9417 | return -EOPNOTSUPP; | |
9418 | } | |
9419 | ||
9420 | return 0; | |
9421 | } | |
9422 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
9423 | ||
20a69341 PM |
9424 | /* |
9425 | * Loop detection - walk through the ruleset beginning at the destination chain | |
9426 | * of a new jump until either the source chain is reached (loop) or all | |
9427 | * reachable chains have been traversed. | |
9428 | * | |
9429 | * The loop check is performed whenever a new jump verdict is added to an | |
9430 | * expression or verdict map or a verdict map is bound to a new chain. | |
9431 | */ | |
9432 | ||
9433 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
9434 | const struct nft_chain *chain); | |
9435 | ||
6387aa6e PNA |
9436 | static int nft_check_loops(const struct nft_ctx *ctx, |
9437 | const struct nft_set_ext *ext) | |
9438 | { | |
9439 | const struct nft_data *data; | |
9440 | int ret; | |
9441 | ||
9442 | data = nft_set_ext_data(ext); | |
9443 | switch (data->verdict.code) { | |
9444 | case NFT_JUMP: | |
9445 | case NFT_GOTO: | |
9446 | ret = nf_tables_check_loops(ctx, data->verdict.chain); | |
9447 | break; | |
9448 | default: | |
9449 | ret = 0; | |
9450 | break; | |
9451 | } | |
9452 | ||
9453 | return ret; | |
9454 | } | |
9455 | ||
20a69341 | 9456 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, |
de70185d | 9457 | struct nft_set *set, |
20a69341 | 9458 | const struct nft_set_iter *iter, |
de70185d | 9459 | struct nft_set_elem *elem) |
20a69341 | 9460 | { |
fe2811eb | 9461 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
fe2811eb PM |
9462 | |
9463 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
9464 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
9465 | return 0; |
9466 | ||
6387aa6e | 9467 | return nft_check_loops(ctx, ext); |
20a69341 PM |
9468 | } |
9469 | ||
aaa31047 PNA |
9470 | static int nft_set_catchall_loops(const struct nft_ctx *ctx, |
9471 | struct nft_set *set) | |
9472 | { | |
9473 | u8 genmask = nft_genmask_next(ctx->net); | |
9474 | struct nft_set_elem_catchall *catchall; | |
9475 | struct nft_set_ext *ext; | |
9476 | int ret = 0; | |
9477 | ||
9478 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
9479 | ext = nft_set_elem_ext(set, catchall->elem); | |
9480 | if (!nft_set_elem_active(ext, genmask)) | |
9481 | continue; | |
9482 | ||
9483 | ret = nft_check_loops(ctx, ext); | |
9484 | if (ret < 0) | |
9485 | return ret; | |
9486 | } | |
9487 | ||
9488 | return ret; | |
9489 | } | |
9490 | ||
20a69341 PM |
9491 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
9492 | const struct nft_chain *chain) | |
9493 | { | |
9494 | const struct nft_rule *rule; | |
9495 | const struct nft_expr *expr, *last; | |
de70185d | 9496 | struct nft_set *set; |
20a69341 PM |
9497 | struct nft_set_binding *binding; |
9498 | struct nft_set_iter iter; | |
20a69341 PM |
9499 | |
9500 | if (ctx->chain == chain) | |
9501 | return -ELOOP; | |
9502 | ||
9503 | list_for_each_entry(rule, &chain->rules, list) { | |
9504 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
9505 | struct nft_immediate_expr *priv; |
9506 | const struct nft_data *data; | |
0ca743a5 PNA |
9507 | int err; |
9508 | ||
a654de8f | 9509 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
9510 | continue; |
9511 | ||
a654de8f PNA |
9512 | priv = nft_expr_priv(expr); |
9513 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 9514 | continue; |
20a69341 | 9515 | |
a654de8f | 9516 | data = &priv->data; |
1ca2e170 | 9517 | switch (data->verdict.code) { |
20a69341 PM |
9518 | case NFT_JUMP: |
9519 | case NFT_GOTO: | |
1ca2e170 PM |
9520 | err = nf_tables_check_loops(ctx, |
9521 | data->verdict.chain); | |
20a69341 PM |
9522 | if (err < 0) |
9523 | return err; | |
c2168e6b | 9524 | break; |
20a69341 PM |
9525 | default: |
9526 | break; | |
9527 | } | |
9528 | } | |
9529 | } | |
9530 | ||
9531 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
9532 | if (!nft_is_active_next(ctx->net, set)) |
9533 | continue; | |
20a69341 PM |
9534 | if (!(set->flags & NFT_SET_MAP) || |
9535 | set->dtype != NFT_DATA_VERDICT) | |
9536 | continue; | |
9537 | ||
9538 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
9539 | if (!(binding->flags & NFT_SET_MAP) || |
9540 | binding->chain != chain) | |
20a69341 PM |
9541 | continue; |
9542 | ||
8588ac09 | 9543 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
9544 | iter.skip = 0; |
9545 | iter.count = 0; | |
9546 | iter.err = 0; | |
9547 | iter.fn = nf_tables_loop_check_setelem; | |
9548 | ||
9549 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
9550 | if (!iter.err) |
9551 | iter.err = nft_set_catchall_loops(ctx, set); | |
9552 | ||
20a69341 PM |
9553 | if (iter.err < 0) |
9554 | return iter.err; | |
9555 | } | |
9556 | } | |
9557 | ||
9558 | return 0; | |
9559 | } | |
9560 | ||
36b701fa LGL |
9561 | /** |
9562 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
9563 | * | |
9564 | * @attr: netlink attribute to fetch value from | |
9565 | * @max: maximum value to be stored in dest | |
9566 | * @dest: pointer to the variable | |
9567 | * | |
9568 | * Parse, check and store a given u32 netlink attribute into variable. | |
9569 | * This function returns -ERANGE if the value goes over maximum value. | |
9570 | * Otherwise a 0 is returned and the attribute value is stored in the | |
9571 | * destination variable. | |
9572 | */ | |
f1d505bb | 9573 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 9574 | { |
09525a09 | 9575 | u32 val; |
36b701fa LGL |
9576 | |
9577 | val = ntohl(nla_get_be32(attr)); | |
9578 | if (val > max) | |
9579 | return -ERANGE; | |
9580 | ||
9581 | *dest = val; | |
9582 | return 0; | |
9583 | } | |
9584 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
9585 | ||
6c6f9f31 | 9586 | static int nft_parse_register(const struct nlattr *attr, u32 *preg) |
b1c96ed3 | 9587 | { |
49499c3e PM |
9588 | unsigned int reg; |
9589 | ||
9590 | reg = ntohl(nla_get_be32(attr)); | |
9591 | switch (reg) { | |
9592 | case NFT_REG_VERDICT...NFT_REG_4: | |
6e1acfa3 PNA |
9593 | *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE; |
9594 | break; | |
9595 | case NFT_REG32_00...NFT_REG32_15: | |
9596 | *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
9597 | break; | |
49499c3e | 9598 | default: |
6e1acfa3 | 9599 | return -ERANGE; |
49499c3e | 9600 | } |
6e1acfa3 PNA |
9601 | |
9602 | return 0; | |
b1c96ed3 | 9603 | } |
b1c96ed3 | 9604 | |
49499c3e PM |
9605 | /** |
9606 | * nft_dump_register - dump a register value to a netlink attribute | |
9607 | * | |
9608 | * @skb: socket buffer | |
9609 | * @attr: attribute number | |
9610 | * @reg: register number | |
9611 | * | |
9612 | * Construct a netlink attribute containing the register number. For | |
9613 | * compatibility reasons, register numbers being a multiple of 4 are | |
9614 | * translated to the corresponding 128 bit register numbers. | |
9615 | */ | |
b1c96ed3 PM |
9616 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
9617 | { | |
49499c3e PM |
9618 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
9619 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
9620 | else | |
9621 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
9622 | ||
b1c96ed3 PM |
9623 | return nla_put_be32(skb, attr, htonl(reg)); |
9624 | } | |
9625 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
9626 | ||
4f16d25c | 9627 | static int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 9628 | { |
49499c3e | 9629 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 9630 | return -EINVAL; |
d07db988 PM |
9631 | if (len == 0) |
9632 | return -EINVAL; | |
c593642c | 9633 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 9634 | return -ERANGE; |
49499c3e | 9635 | |
96518518 PM |
9636 | return 0; |
9637 | } | |
4f16d25c PNA |
9638 | |
9639 | int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len) | |
9640 | { | |
9641 | u32 reg; | |
9642 | int err; | |
9643 | ||
6e1acfa3 PNA |
9644 | err = nft_parse_register(attr, ®); |
9645 | if (err < 0) | |
9646 | return err; | |
9647 | ||
4f16d25c PNA |
9648 | err = nft_validate_register_load(reg, len); |
9649 | if (err < 0) | |
9650 | return err; | |
9651 | ||
9652 | *sreg = reg; | |
9653 | return 0; | |
9654 | } | |
9655 | EXPORT_SYMBOL_GPL(nft_parse_register_load); | |
96518518 | 9656 | |
345023b0 PNA |
9657 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
9658 | enum nft_registers reg, | |
9659 | const struct nft_data *data, | |
9660 | enum nft_data_types type, | |
9661 | unsigned int len) | |
96518518 | 9662 | { |
20a69341 PM |
9663 | int err; |
9664 | ||
96518518 PM |
9665 | switch (reg) { |
9666 | case NFT_REG_VERDICT: | |
58f40ab6 | 9667 | if (type != NFT_DATA_VERDICT) |
96518518 | 9668 | return -EINVAL; |
20a69341 | 9669 | |
58f40ab6 | 9670 | if (data != NULL && |
1ca2e170 PM |
9671 | (data->verdict.code == NFT_GOTO || |
9672 | data->verdict.code == NFT_JUMP)) { | |
9673 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
9674 | if (err < 0) |
9675 | return err; | |
20a69341 PM |
9676 | } |
9677 | ||
96518518 PM |
9678 | return 0; |
9679 | default: | |
49499c3e | 9680 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 9681 | return -EINVAL; |
45d9bcda PM |
9682 | if (len == 0) |
9683 | return -EINVAL; | |
49499c3e | 9684 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 9685 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 9686 | return -ERANGE; |
27e6d201 | 9687 | |
96518518 PM |
9688 | if (data != NULL && type != NFT_DATA_VALUE) |
9689 | return -EINVAL; | |
9690 | return 0; | |
9691 | } | |
9692 | } | |
345023b0 PNA |
9693 | |
9694 | int nft_parse_register_store(const struct nft_ctx *ctx, | |
9695 | const struct nlattr *attr, u8 *dreg, | |
9696 | const struct nft_data *data, | |
9697 | enum nft_data_types type, unsigned int len) | |
9698 | { | |
9699 | int err; | |
9700 | u32 reg; | |
9701 | ||
6e1acfa3 PNA |
9702 | err = nft_parse_register(attr, ®); |
9703 | if (err < 0) | |
9704 | return err; | |
9705 | ||
345023b0 PNA |
9706 | err = nft_validate_register_store(ctx, reg, data, type, len); |
9707 | if (err < 0) | |
9708 | return err; | |
9709 | ||
9710 | *dreg = reg; | |
9711 | return 0; | |
9712 | } | |
9713 | EXPORT_SYMBOL_GPL(nft_parse_register_store); | |
96518518 PM |
9714 | |
9715 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
9716 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
9717 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
9718 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
51d70f18 | 9719 | [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
9720 | }; |
9721 | ||
9722 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
9723 | struct nft_data_desc *desc, const struct nlattr *nla) | |
9724 | { | |
664b0f8c | 9725 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
9726 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
9727 | struct nft_chain *chain; | |
9728 | int err; | |
9729 | ||
8cb08174 JB |
9730 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
9731 | nft_verdict_policy, NULL); | |
96518518 PM |
9732 | if (err < 0) |
9733 | return err; | |
9734 | ||
9735 | if (!tb[NFTA_VERDICT_CODE]) | |
9736 | return -EINVAL; | |
1ca2e170 | 9737 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 9738 | |
1ca2e170 | 9739 | switch (data->verdict.code) { |
e0abdadc | 9740 | default: |
1ca2e170 | 9741 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
9742 | case NF_ACCEPT: |
9743 | case NF_DROP: | |
9744 | case NF_QUEUE: | |
9745 | break; | |
9746 | default: | |
9747 | return -EINVAL; | |
9748 | } | |
954d8297 | 9749 | fallthrough; |
96518518 PM |
9750 | case NFT_CONTINUE: |
9751 | case NFT_BREAK: | |
9752 | case NFT_RETURN: | |
96518518 PM |
9753 | break; |
9754 | case NFT_JUMP: | |
9755 | case NFT_GOTO: | |
51d70f18 PNA |
9756 | if (tb[NFTA_VERDICT_CHAIN]) { |
9757 | chain = nft_chain_lookup(ctx->net, ctx->table, | |
9758 | tb[NFTA_VERDICT_CHAIN], | |
9759 | genmask); | |
9760 | } else if (tb[NFTA_VERDICT_CHAIN_ID]) { | |
95f466d2 | 9761 | chain = nft_chain_lookup_byid(ctx->net, ctx->table, |
51d70f18 PNA |
9762 | tb[NFTA_VERDICT_CHAIN_ID]); |
9763 | if (IS_ERR(chain)) | |
9764 | return PTR_ERR(chain); | |
9765 | } else { | |
96518518 | 9766 | return -EINVAL; |
51d70f18 PNA |
9767 | } |
9768 | ||
96518518 PM |
9769 | if (IS_ERR(chain)) |
9770 | return PTR_ERR(chain); | |
f323d954 | 9771 | if (nft_is_base_chain(chain)) |
96518518 | 9772 | return -EOPNOTSUPP; |
e02f0d39 PNA |
9773 | if (nft_chain_is_bound(chain)) |
9774 | return -EINVAL; | |
f323ef3a PNA |
9775 | if (desc->flags & NFT_DATA_DESC_SETELEM && |
9776 | chain->flags & NFT_CHAIN_BINDING) | |
9777 | return -EINVAL; | |
96518518 | 9778 | |
96518518 | 9779 | chain->use++; |
1ca2e170 | 9780 | data->verdict.chain = chain; |
96518518 | 9781 | break; |
96518518 PM |
9782 | } |
9783 | ||
4c4ed074 | 9784 | desc->len = sizeof(data->verdict); |
341b6941 | 9785 | |
96518518 PM |
9786 | return 0; |
9787 | } | |
9788 | ||
9789 | static void nft_verdict_uninit(const struct nft_data *data) | |
9790 | { | |
d0e2c7de PNA |
9791 | struct nft_chain *chain; |
9792 | struct nft_rule *rule; | |
9793 | ||
1ca2e170 | 9794 | switch (data->verdict.code) { |
96518518 PM |
9795 | case NFT_JUMP: |
9796 | case NFT_GOTO: | |
d0e2c7de PNA |
9797 | chain = data->verdict.chain; |
9798 | chain->use--; | |
9799 | ||
9800 | if (!nft_chain_is_bound(chain)) | |
9801 | break; | |
9802 | ||
9803 | chain->table->use--; | |
9804 | list_for_each_entry(rule, &chain->rules, list) | |
9805 | chain->use--; | |
9806 | ||
9807 | nft_chain_del(chain); | |
96518518 PM |
9808 | break; |
9809 | } | |
9810 | } | |
9811 | ||
33d5a7b1 | 9812 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
9813 | { |
9814 | struct nlattr *nest; | |
9815 | ||
ae0be8de | 9816 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
9817 | if (!nest) |
9818 | goto nla_put_failure; | |
9819 | ||
33d5a7b1 | 9820 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
9821 | goto nla_put_failure; |
9822 | ||
33d5a7b1 | 9823 | switch (v->code) { |
96518518 PM |
9824 | case NFT_JUMP: |
9825 | case NFT_GOTO: | |
1ca2e170 | 9826 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 9827 | v->chain->name)) |
96518518 PM |
9828 | goto nla_put_failure; |
9829 | } | |
9830 | nla_nest_end(skb, nest); | |
9831 | return 0; | |
9832 | ||
9833 | nla_put_failure: | |
9834 | return -1; | |
9835 | } | |
9836 | ||
d0a11fc3 | 9837 | static int nft_value_init(const struct nft_ctx *ctx, |
341b6941 PNA |
9838 | struct nft_data *data, struct nft_data_desc *desc, |
9839 | const struct nlattr *nla) | |
96518518 PM |
9840 | { |
9841 | unsigned int len; | |
9842 | ||
9843 | len = nla_len(nla); | |
9844 | if (len == 0) | |
9845 | return -EINVAL; | |
341b6941 | 9846 | if (len > desc->size) |
96518518 | 9847 | return -EOVERFLOW; |
341b6941 PNA |
9848 | if (desc->len) { |
9849 | if (len != desc->len) | |
9850 | return -EINVAL; | |
9851 | } else { | |
9852 | desc->len = len; | |
9853 | } | |
96518518 | 9854 | |
d0a11fc3 | 9855 | nla_memcpy(data->data, nla, len); |
341b6941 | 9856 | |
96518518 PM |
9857 | return 0; |
9858 | } | |
9859 | ||
9860 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
9861 | unsigned int len) | |
9862 | { | |
9863 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
9864 | } | |
9865 | ||
9866 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 9867 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
9868 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
9869 | }; | |
9870 | ||
9871 | /** | |
9872 | * nft_data_init - parse nf_tables data netlink attributes | |
9873 | * | |
9874 | * @ctx: context of the expression using the data | |
9875 | * @data: destination struct nft_data | |
9876 | * @desc: data description | |
9877 | * @nla: netlink attribute containing data | |
9878 | * | |
9879 | * Parse the netlink data attributes and initialize a struct nft_data. | |
9880 | * The type and length of data are returned in the data description. | |
9881 | * | |
9882 | * The caller can indicate that it only wants to accept data of type | |
9883 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
9884 | */ | |
341b6941 | 9885 | int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
96518518 PM |
9886 | struct nft_data_desc *desc, const struct nlattr *nla) |
9887 | { | |
9888 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
9889 | int err; | |
9890 | ||
341b6941 PNA |
9891 | if (WARN_ON_ONCE(!desc->size)) |
9892 | return -EINVAL; | |
9893 | ||
8cb08174 JB |
9894 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
9895 | nft_data_policy, NULL); | |
96518518 PM |
9896 | if (err < 0) |
9897 | return err; | |
9898 | ||
341b6941 PNA |
9899 | if (tb[NFTA_DATA_VALUE]) { |
9900 | if (desc->type != NFT_DATA_VALUE) | |
9901 | return -EINVAL; | |
9902 | ||
9903 | err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]); | |
9904 | } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) { | |
9905 | if (desc->type != NFT_DATA_VERDICT) | |
9906 | return -EINVAL; | |
9907 | ||
9908 | err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
9909 | } else { | |
9910 | err = -EINVAL; | |
9911 | } | |
9912 | ||
9913 | return err; | |
96518518 PM |
9914 | } |
9915 | EXPORT_SYMBOL_GPL(nft_data_init); | |
9916 | ||
9917 | /** | |
59105446 | 9918 | * nft_data_release - release a nft_data item |
96518518 PM |
9919 | * |
9920 | * @data: struct nft_data to release | |
9921 | * @type: type of data | |
9922 | * | |
9923 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
9924 | * all others need to be released by calling this function. | |
9925 | */ | |
59105446 | 9926 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 9927 | { |
960bd2c2 | 9928 | if (type < NFT_DATA_VERDICT) |
96518518 | 9929 | return; |
960bd2c2 | 9930 | switch (type) { |
96518518 PM |
9931 | case NFT_DATA_VERDICT: |
9932 | return nft_verdict_uninit(data); | |
9933 | default: | |
9934 | WARN_ON(1); | |
9935 | } | |
9936 | } | |
59105446 | 9937 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
9938 | |
9939 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
9940 | enum nft_data_types type, unsigned int len) | |
9941 | { | |
9942 | struct nlattr *nest; | |
9943 | int err; | |
9944 | ||
ae0be8de | 9945 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
9946 | if (nest == NULL) |
9947 | return -1; | |
9948 | ||
9949 | switch (type) { | |
9950 | case NFT_DATA_VALUE: | |
9951 | err = nft_value_dump(skb, data, len); | |
9952 | break; | |
9953 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 9954 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
9955 | break; |
9956 | default: | |
9957 | err = -EINVAL; | |
9958 | WARN_ON(1); | |
9959 | } | |
9960 | ||
9961 | nla_nest_end(skb, nest); | |
9962 | return err; | |
9963 | } | |
9964 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
9965 | ||
5ebe0b0e PNA |
9966 | int __nft_release_basechain(struct nft_ctx *ctx) |
9967 | { | |
9968 | struct nft_rule *rule, *nr; | |
9969 | ||
fa5950e4 FW |
9970 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
9971 | return 0; | |
5ebe0b0e | 9972 | |
c974a3a3 | 9973 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
9974 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
9975 | list_del(&rule->list); | |
9976 | ctx->chain->use--; | |
bb7b40ae | 9977 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 9978 | } |
1b2470e5 | 9979 | nft_chain_del(ctx->chain); |
5ebe0b0e | 9980 | ctx->table->use--; |
43a605f2 | 9981 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
9982 | |
9983 | return 0; | |
9984 | } | |
9985 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
9986 | ||
00dfe9be PNA |
9987 | static void __nft_release_hook(struct net *net, struct nft_table *table) |
9988 | { | |
6069da44 | 9989 | struct nft_flowtable *flowtable; |
00dfe9be PNA |
9990 | struct nft_chain *chain; |
9991 | ||
9992 | list_for_each_entry(chain, &table->chains, list) | |
f9a43007 | 9993 | __nf_tables_unregister_hook(net, table, chain, true); |
6069da44 | 9994 | list_for_each_entry(flowtable, &table->flowtables, list) |
f9a43007 PNA |
9995 | __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list, |
9996 | true); | |
00dfe9be PNA |
9997 | } |
9998 | ||
767d1216 PNA |
9999 | static void __nft_release_hooks(struct net *net) |
10000 | { | |
d59d2f82 | 10001 | struct nftables_pernet *nft_net = nft_pernet(net); |
767d1216 | 10002 | struct nft_table *table; |
767d1216 | 10003 | |
0854db2a | 10004 | list_for_each_entry(table, &nft_net->tables, list) { |
2888b080 PNA |
10005 | if (nft_table_has_owner(table)) |
10006 | continue; | |
10007 | ||
00dfe9be | 10008 | __nft_release_hook(net, table); |
2888b080 | 10009 | } |
767d1216 PNA |
10010 | } |
10011 | ||
fd020332 | 10012 | static void __nft_release_table(struct net *net, struct nft_table *table) |
df05ef87 | 10013 | { |
3b49e2e9 | 10014 | struct nft_flowtable *flowtable, *nf; |
df05ef87 | 10015 | struct nft_chain *chain, *nc; |
e5009240 | 10016 | struct nft_object *obj, *ne; |
df05ef87 PNA |
10017 | struct nft_rule *rule, *nr; |
10018 | struct nft_set *set, *ns; | |
10019 | struct nft_ctx ctx = { | |
10020 | .net = net, | |
43a605f2 | 10021 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
10022 | }; |
10023 | ||
fd020332 PNA |
10024 | ctx.family = table->family; |
10025 | ctx.table = table; | |
10026 | list_for_each_entry(chain, &table->chains, list) { | |
10027 | ctx.chain = chain; | |
10028 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
10029 | list_del(&rule->list); | |
10030 | chain->use--; | |
10031 | nf_tables_rule_release(&ctx, rule); | |
df05ef87 | 10032 | } |
df05ef87 | 10033 | } |
fd020332 PNA |
10034 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
10035 | list_del(&flowtable->list); | |
10036 | table->use--; | |
10037 | nf_tables_flowtable_destroy(flowtable); | |
10038 | } | |
10039 | list_for_each_entry_safe(set, ns, &table->sets, list) { | |
10040 | list_del(&set->list); | |
10041 | table->use--; | |
10042 | nft_set_destroy(&ctx, set); | |
10043 | } | |
10044 | list_for_each_entry_safe(obj, ne, &table->objects, list) { | |
10045 | nft_obj_del(obj); | |
10046 | table->use--; | |
10047 | nft_obj_destroy(&ctx, obj); | |
10048 | } | |
10049 | list_for_each_entry_safe(chain, nc, &table->chains, list) { | |
10050 | ctx.chain = chain; | |
10051 | nft_chain_del(chain); | |
10052 | table->use--; | |
10053 | nf_tables_chain_destroy(&ctx); | |
10054 | } | |
fd020332 PNA |
10055 | nf_tables_table_destroy(&ctx); |
10056 | } | |
10057 | ||
bd1777b3 | 10058 | static void __nft_release_tables(struct net *net) |
fd020332 | 10059 | { |
d59d2f82 | 10060 | struct nftables_pernet *nft_net = nft_pernet(net); |
fd020332 PNA |
10061 | struct nft_table *table, *nt; |
10062 | ||
0854db2a | 10063 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
bd1777b3 | 10064 | if (nft_table_has_owner(table)) |
6001a930 PNA |
10065 | continue; |
10066 | ||
a499b03b FW |
10067 | list_del(&table->list); |
10068 | ||
fd020332 | 10069 | __nft_release_table(net, table); |
6001a930 PNA |
10070 | } |
10071 | } | |
10072 | ||
10073 | static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event, | |
10074 | void *ptr) | |
10075 | { | |
a499b03b | 10076 | struct nft_table *table, *to_delete[8]; |
0854db2a | 10077 | struct nftables_pernet *nft_net; |
6001a930 | 10078 | struct netlink_notify *n = ptr; |
6001a930 | 10079 | struct net *net = n->net; |
a499b03b FW |
10080 | unsigned int deleted; |
10081 | bool restart = false; | |
6001a930 PNA |
10082 | |
10083 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER) | |
10084 | return NOTIFY_DONE; | |
10085 | ||
d59d2f82 | 10086 | nft_net = nft_pernet(net); |
a499b03b | 10087 | deleted = 0; |
0854db2a | 10088 | mutex_lock(&nft_net->commit_mutex); |
d4bc8271 PNA |
10089 | if (!list_empty(&nf_tables_destroy_list)) |
10090 | rcu_barrier(); | |
a499b03b | 10091 | again: |
0854db2a | 10092 | list_for_each_entry(table, &nft_net->tables, list) { |
6001a930 PNA |
10093 | if (nft_table_has_owner(table) && |
10094 | n->portid == table->nlpid) { | |
10095 | __nft_release_hook(net, table); | |
a499b03b FW |
10096 | list_del_rcu(&table->list); |
10097 | to_delete[deleted++] = table; | |
10098 | if (deleted >= ARRAY_SIZE(to_delete)) | |
10099 | break; | |
6001a930 PNA |
10100 | } |
10101 | } | |
a499b03b FW |
10102 | if (deleted) { |
10103 | restart = deleted >= ARRAY_SIZE(to_delete); | |
6001a930 | 10104 | synchronize_rcu(); |
a499b03b FW |
10105 | while (deleted) |
10106 | __nft_release_table(net, to_delete[--deleted]); | |
10107 | ||
10108 | if (restart) | |
10109 | goto again; | |
6001a930 | 10110 | } |
0854db2a | 10111 | mutex_unlock(&nft_net->commit_mutex); |
6001a930 PNA |
10112 | |
10113 | return NOTIFY_DONE; | |
df05ef87 PNA |
10114 | } |
10115 | ||
6001a930 PNA |
10116 | static struct notifier_block nft_nl_notifier = { |
10117 | .notifier_call = nft_rcv_nl_event, | |
10118 | }; | |
10119 | ||
dd4cbef7 PNA |
10120 | static int __net_init nf_tables_init_net(struct net *net) |
10121 | { | |
d59d2f82 | 10122 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10123 | |
10124 | INIT_LIST_HEAD(&nft_net->tables); | |
10125 | INIT_LIST_HEAD(&nft_net->commit_list); | |
10126 | INIT_LIST_HEAD(&nft_net->module_list); | |
10127 | INIT_LIST_HEAD(&nft_net->notify_list); | |
10128 | mutex_init(&nft_net->commit_mutex); | |
10129 | nft_net->base_seq = 1; | |
10130 | nft_net->validate_state = NFT_VALIDATE_SKIP; | |
a654de8f | 10131 | |
dd4cbef7 PNA |
10132 | return 0; |
10133 | } | |
10134 | ||
767d1216 PNA |
10135 | static void __net_exit nf_tables_pre_exit_net(struct net *net) |
10136 | { | |
3923b1e4 PNA |
10137 | struct nftables_pernet *nft_net = nft_pernet(net); |
10138 | ||
10139 | mutex_lock(&nft_net->commit_mutex); | |
767d1216 | 10140 | __nft_release_hooks(net); |
3923b1e4 | 10141 | mutex_unlock(&nft_net->commit_mutex); |
767d1216 PNA |
10142 | } |
10143 | ||
dd4cbef7 PNA |
10144 | static void __net_exit nf_tables_exit_net(struct net *net) |
10145 | { | |
d59d2f82 | 10146 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10147 | |
10148 | mutex_lock(&nft_net->commit_mutex); | |
03c1f1ef SY |
10149 | if (!list_empty(&nft_net->commit_list) || |
10150 | !list_empty(&nft_net->module_list)) | |
c0391b6a | 10151 | __nf_tables_abort(net, NFNL_ABORT_NONE); |
bd1777b3 | 10152 | __nft_release_tables(net); |
0854db2a FW |
10153 | mutex_unlock(&nft_net->commit_mutex); |
10154 | WARN_ON_ONCE(!list_empty(&nft_net->tables)); | |
10155 | WARN_ON_ONCE(!list_empty(&nft_net->module_list)); | |
10156 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); | |
dd4cbef7 PNA |
10157 | } |
10158 | ||
99633ab2 | 10159 | static struct pernet_operations nf_tables_net_ops = { |
767d1216 PNA |
10160 | .init = nf_tables_init_net, |
10161 | .pre_exit = nf_tables_pre_exit_net, | |
10162 | .exit = nf_tables_exit_net, | |
0854db2a FW |
10163 | .id = &nf_tables_net_id, |
10164 | .size = sizeof(struct nftables_pernet), | |
99633ab2 PNA |
10165 | }; |
10166 | ||
96518518 PM |
10167 | static int __init nf_tables_module_init(void) |
10168 | { | |
10169 | int err; | |
10170 | ||
d209df3e FW |
10171 | err = register_pernet_subsys(&nf_tables_net_ops); |
10172 | if (err < 0) | |
10173 | return err; | |
10174 | ||
10175 | err = nft_chain_filter_init(); | |
10176 | if (err < 0) | |
6001a930 | 10177 | goto err_chain_filter; |
02c7b25e | 10178 | |
96518518 PM |
10179 | err = nf_tables_core_module_init(); |
10180 | if (err < 0) | |
6001a930 | 10181 | goto err_core_module; |
96518518 | 10182 | |
d209df3e | 10183 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 10184 | if (err < 0) |
6001a930 | 10185 | goto err_netdev_notifier; |
96518518 | 10186 | |
4d44175a FW |
10187 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
10188 | if (err < 0) | |
6001a930 | 10189 | goto err_rht_objname; |
4d44175a | 10190 | |
06d392cb | 10191 | err = nft_offload_init(); |
10192 | if (err < 0) | |
6001a930 PNA |
10193 | goto err_offload; |
10194 | ||
10195 | err = netlink_register_notifier(&nft_nl_notifier); | |
10196 | if (err < 0) | |
10197 | goto err_netlink_notifier; | |
06d392cb | 10198 | |
d209df3e FW |
10199 | /* must be last */ |
10200 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
10201 | if (err < 0) | |
6001a930 | 10202 | goto err_nfnl_subsys; |
3b49e2e9 | 10203 | |
c1deb065 | 10204 | nft_chain_route_init(); |
3474a2c6 | 10205 | |
d209df3e | 10206 | return err; |
6001a930 PNA |
10207 | |
10208 | err_nfnl_subsys: | |
10209 | netlink_unregister_notifier(&nft_nl_notifier); | |
10210 | err_netlink_notifier: | |
06d392cb | 10211 | nft_offload_exit(); |
6001a930 | 10212 | err_offload: |
4d44175a | 10213 | rhltable_destroy(&nft_objname_ht); |
6001a930 | 10214 | err_rht_objname: |
d209df3e | 10215 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
6001a930 | 10216 | err_netdev_notifier: |
96518518 | 10217 | nf_tables_core_module_exit(); |
6001a930 | 10218 | err_core_module: |
d209df3e | 10219 | nft_chain_filter_fini(); |
6001a930 | 10220 | err_chain_filter: |
d209df3e | 10221 | unregister_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
10222 | return err; |
10223 | } | |
10224 | ||
10225 | static void __exit nf_tables_module_exit(void) | |
10226 | { | |
10227 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
6001a930 | 10228 | netlink_unregister_notifier(&nft_nl_notifier); |
06d392cb | 10229 | nft_offload_exit(); |
3b49e2e9 | 10230 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 10231 | nft_chain_filter_fini(); |
c1deb065 | 10232 | nft_chain_route_fini(); |
71ad00c5 | 10233 | unregister_pernet_subsys(&nf_tables_net_ops); |
0935d558 | 10234 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 10235 | rcu_barrier(); |
4d44175a | 10236 | rhltable_destroy(&nft_objname_ht); |
96518518 | 10237 | nf_tables_core_module_exit(); |
96518518 PM |
10238 | } |
10239 | ||
10240 | module_init(nf_tables_module_init); | |
10241 | module_exit(nf_tables_module_exit); | |
10242 | ||
10243 | MODULE_LICENSE("GPL"); | |
10244 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
10245 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |