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; | |
745 | strlcpy(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 ED |
1536 | do { |
1537 | seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); | |
1538 | pkts = cpu_stats->pkts; | |
1539 | bytes = cpu_stats->bytes; | |
1540 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); | |
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 && | |
3a41c64d | 2169 | !nft_chain_offload_support(basechain)) |
d54725cd PNA |
2170 | return -EOPNOTSUPP; |
2171 | ||
2172 | flow_block_init(&basechain->flow_block); | |
2173 | ||
2174 | return 0; | |
2175 | } | |
2176 | ||
04b7db41 PNA |
2177 | static int nft_chain_add(struct nft_table *table, struct nft_chain *chain) |
2178 | { | |
2179 | int err; | |
2180 | ||
2181 | err = rhltable_insert_key(&table->chains_ht, chain->name, | |
2182 | &chain->rhlhead, nft_chain_ht_params); | |
2183 | if (err) | |
2184 | return err; | |
2185 | ||
2186 | list_add_tail_rcu(&chain->list, &table->chains); | |
2187 | ||
2188 | return 0; | |
2189 | } | |
2190 | ||
d0e2c7de PNA |
2191 | static u64 chain_id; |
2192 | ||
4035285f | 2193 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
983c4fcb PNA |
2194 | u8 policy, u32 flags, |
2195 | struct netlink_ext_ack *extack) | |
4035285f PNA |
2196 | { |
2197 | const struct nlattr * const *nla = ctx->nla; | |
43eb8949 | 2198 | struct nft_stats __percpu *stats = NULL; |
4035285f | 2199 | struct nft_table *table = ctx->table; |
4035285f | 2200 | struct nft_base_chain *basechain; |
4035285f | 2201 | struct net *net = ctx->net; |
d0e2c7de | 2202 | char name[NFT_NAME_MAXLEN]; |
2c865a8a | 2203 | struct nft_rule_blob *blob; |
66293c46 | 2204 | struct nft_trans *trans; |
4035285f | 2205 | struct nft_chain *chain; |
2c865a8a | 2206 | unsigned int data_size; |
4035285f PNA |
2207 | int err; |
2208 | ||
2209 | if (table->use == UINT_MAX) | |
2210 | return -EOVERFLOW; | |
2211 | ||
2212 | if (nla[NFTA_CHAIN_HOOK]) { | |
2213 | struct nft_chain_hook hook; | |
4035285f | 2214 | |
d0e2c7de PNA |
2215 | if (flags & NFT_CHAIN_BINDING) |
2216 | return -EOPNOTSUPP; | |
2217 | ||
983c4fcb PNA |
2218 | err = nft_chain_parse_hook(net, nla, &hook, family, extack, |
2219 | true); | |
4035285f PNA |
2220 | if (err < 0) |
2221 | return err; | |
2222 | ||
33758c89 | 2223 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2224 | if (basechain == NULL) { |
2225 | nft_chain_release_hook(&hook); | |
2226 | return -ENOMEM; | |
2227 | } | |
d54725cd | 2228 | chain = &basechain->chain; |
4035285f PNA |
2229 | |
2230 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2231 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2232 | if (IS_ERR(stats)) { | |
2233 | nft_chain_release_hook(&hook); | |
2234 | kfree(basechain); | |
2235 | return PTR_ERR(stats); | |
2236 | } | |
4c05ec47 | 2237 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
2238 | } |
2239 | ||
d54725cd PNA |
2240 | err = nft_basechain_init(basechain, family, &hook, flags); |
2241 | if (err < 0) { | |
2242 | nft_chain_release_hook(&hook); | |
2243 | kfree(basechain); | |
2244 | return err; | |
2245 | } | |
4035285f | 2246 | } else { |
d0e2c7de PNA |
2247 | if (flags & NFT_CHAIN_BASE) |
2248 | return -EINVAL; | |
2249 | if (flags & NFT_CHAIN_HW_OFFLOAD) | |
2250 | return -EOPNOTSUPP; | |
2251 | ||
33758c89 | 2252 | chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2253 | if (chain == NULL) |
2254 | return -ENOMEM; | |
d0e2c7de PNA |
2255 | |
2256 | chain->flags = flags; | |
4035285f | 2257 | } |
43a605f2 PNA |
2258 | ctx->chain = chain; |
2259 | ||
4035285f PNA |
2260 | INIT_LIST_HEAD(&chain->rules); |
2261 | chain->handle = nf_tables_alloc_handle(table); | |
2262 | chain->table = table; | |
d0e2c7de PNA |
2263 | |
2264 | if (nla[NFTA_CHAIN_NAME]) { | |
33758c89 | 2265 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
d0e2c7de | 2266 | } else { |
59136aa3 FW |
2267 | if (!(flags & NFT_CHAIN_BINDING)) { |
2268 | err = -EINVAL; | |
002f2176 | 2269 | goto err_destroy_chain; |
59136aa3 | 2270 | } |
d0e2c7de PNA |
2271 | |
2272 | snprintf(name, sizeof(name), "__chain%llu", ++chain_id); | |
33758c89 | 2273 | chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT); |
d0e2c7de PNA |
2274 | } |
2275 | ||
4035285f PNA |
2276 | if (!chain->name) { |
2277 | err = -ENOMEM; | |
002f2176 JGG |
2278 | goto err_destroy_chain; |
2279 | } | |
2280 | ||
2281 | if (nla[NFTA_CHAIN_USERDATA]) { | |
33758c89 | 2282 | chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT); |
002f2176 JGG |
2283 | if (chain->udata == NULL) { |
2284 | err = -ENOMEM; | |
2285 | goto err_destroy_chain; | |
2286 | } | |
2287 | chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]); | |
4035285f PNA |
2288 | } |
2289 | ||
2c865a8a PNA |
2290 | data_size = offsetof(struct nft_rule_dp, data); /* last rule */ |
2291 | blob = nf_tables_chain_alloc_rules(data_size); | |
2292 | if (!blob) { | |
0cbc06b3 | 2293 | err = -ENOMEM; |
002f2176 | 2294 | goto err_destroy_chain; |
0cbc06b3 FW |
2295 | } |
2296 | ||
2c865a8a PNA |
2297 | RCU_INIT_POINTER(chain->blob_gen_0, blob); |
2298 | RCU_INIT_POINTER(chain->blob_gen_1, blob); | |
0cbc06b3 | 2299 | |
c974a3a3 | 2300 | err = nf_tables_register_hook(net, table, chain); |
4035285f | 2301 | if (err < 0) |
002f2176 | 2302 | goto err_destroy_chain; |
4035285f | 2303 | |
66293c46 FW |
2304 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
2305 | if (IS_ERR(trans)) { | |
2306 | err = PTR_ERR(trans); | |
002f2176 | 2307 | goto err_unregister_hook; |
1b2470e5 | 2308 | } |
4035285f | 2309 | |
ad652f38 | 2310 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
2311 | if (nft_is_base_chain(chain)) |
2312 | nft_trans_chain_policy(trans) = policy; | |
2313 | ||
04b7db41 PNA |
2314 | err = nft_chain_add(table, chain); |
2315 | if (err < 0) { | |
2316 | nft_trans_destroy(trans); | |
002f2176 | 2317 | goto err_unregister_hook; |
04b7db41 PNA |
2318 | } |
2319 | ||
43eb8949 PNA |
2320 | if (stats) |
2321 | static_branch_inc(&nft_counters_enabled); | |
2322 | ||
4035285f | 2323 | table->use++; |
4035285f PNA |
2324 | |
2325 | return 0; | |
002f2176 | 2326 | err_unregister_hook: |
c974a3a3 | 2327 | nf_tables_unregister_hook(net, table, chain); |
002f2176 | 2328 | err_destroy_chain: |
43a605f2 | 2329 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2330 | |
2331 | return err; | |
2332 | } | |
2333 | ||
d54725cd PNA |
2334 | static bool nft_hook_list_equal(struct list_head *hook_list1, |
2335 | struct list_head *hook_list2) | |
2336 | { | |
2337 | struct nft_hook *hook; | |
2338 | int n = 0, m = 0; | |
2339 | ||
2340 | n = 0; | |
2341 | list_for_each_entry(hook, hook_list2, list) { | |
2342 | if (!nft_hook_list_find(hook_list1, hook)) | |
2343 | return false; | |
2344 | ||
2345 | n++; | |
2346 | } | |
2347 | list_for_each_entry(hook, hook_list1, list) | |
2348 | m++; | |
2349 | ||
2350 | return n == m; | |
2351 | } | |
2352 | ||
c9626a2c | 2353 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
98a381a7 PNA |
2354 | u32 flags, const struct nlattr *attr, |
2355 | struct netlink_ext_ack *extack) | |
2c4a488a PNA |
2356 | { |
2357 | const struct nlattr * const *nla = ctx->nla; | |
2358 | struct nft_table *table = ctx->table; | |
2359 | struct nft_chain *chain = ctx->chain; | |
2c4a488a PNA |
2360 | struct nft_base_chain *basechain; |
2361 | struct nft_stats *stats = NULL; | |
2362 | struct nft_chain_hook hook; | |
2c4a488a PNA |
2363 | struct nf_hook_ops *ops; |
2364 | struct nft_trans *trans; | |
c974a3a3 | 2365 | int err; |
2c4a488a | 2366 | |
c9626a2c PNA |
2367 | if (chain->flags ^ flags) |
2368 | return -EOPNOTSUPP; | |
2369 | ||
2c4a488a | 2370 | if (nla[NFTA_CHAIN_HOOK]) { |
98a381a7 PNA |
2371 | if (!nft_is_base_chain(chain)) { |
2372 | NL_SET_BAD_ATTR(extack, attr); | |
77a92189 | 2373 | return -EEXIST; |
98a381a7 | 2374 | } |
36596dad | 2375 | err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family, |
983c4fcb | 2376 | extack, false); |
2c4a488a PNA |
2377 | if (err < 0) |
2378 | return err; | |
2379 | ||
2380 | basechain = nft_base_chain(chain); | |
2381 | if (basechain->type != hook.type) { | |
2382 | nft_chain_release_hook(&hook); | |
98a381a7 | 2383 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2384 | return -EEXIST; |
2c4a488a PNA |
2385 | } |
2386 | ||
d3519cb8 | 2387 | if (nft_base_chain_netdev(ctx->family, hook.num)) { |
d54725cd PNA |
2388 | if (!nft_hook_list_equal(&basechain->hook_list, |
2389 | &hook.list)) { | |
2390 | nft_chain_release_hook(&hook); | |
98a381a7 | 2391 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2392 | return -EEXIST; |
d54725cd PNA |
2393 | } |
2394 | } else { | |
2395 | ops = &basechain->ops; | |
2396 | if (ops->hooknum != hook.num || | |
2397 | ops->priority != hook.priority) { | |
2398 | nft_chain_release_hook(&hook); | |
98a381a7 | 2399 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2400 | return -EEXIST; |
d54725cd | 2401 | } |
2c4a488a PNA |
2402 | } |
2403 | nft_chain_release_hook(&hook); | |
2404 | } | |
2405 | ||
2406 | if (nla[NFTA_CHAIN_HANDLE] && | |
2407 | nla[NFTA_CHAIN_NAME]) { | |
2408 | struct nft_chain *chain2; | |
2409 | ||
f102d66b FW |
2410 | chain2 = nft_chain_lookup(ctx->net, table, |
2411 | nla[NFTA_CHAIN_NAME], genmask); | |
98a381a7 PNA |
2412 | if (!IS_ERR(chain2)) { |
2413 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
0d18779b | 2414 | return -EEXIST; |
98a381a7 | 2415 | } |
2c4a488a PNA |
2416 | } |
2417 | ||
2418 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2419 | if (!nft_is_base_chain(chain)) | |
2420 | return -EOPNOTSUPP; | |
2421 | ||
2422 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2423 | if (IS_ERR(stats)) | |
2424 | return PTR_ERR(stats); | |
2425 | } | |
2426 | ||
c6cc94df | 2427 | err = -ENOMEM; |
2c4a488a PNA |
2428 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2429 | sizeof(struct nft_trans_chain)); | |
c6cc94df FW |
2430 | if (trans == NULL) |
2431 | goto err; | |
2c4a488a PNA |
2432 | |
2433 | nft_trans_chain_stats(trans) = stats; | |
2434 | nft_trans_chain_update(trans) = true; | |
2435 | ||
2436 | if (nla[NFTA_CHAIN_POLICY]) | |
2437 | nft_trans_chain_policy(trans) = policy; | |
2438 | else | |
2439 | nft_trans_chain_policy(trans) = -1; | |
2440 | ||
c6cc94df FW |
2441 | if (nla[NFTA_CHAIN_HANDLE] && |
2442 | nla[NFTA_CHAIN_NAME]) { | |
d59d2f82 | 2443 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c6cc94df FW |
2444 | struct nft_trans *tmp; |
2445 | char *name; | |
2446 | ||
2447 | err = -ENOMEM; | |
33758c89 | 2448 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
c6cc94df FW |
2449 | if (!name) |
2450 | goto err; | |
2451 | ||
2452 | err = -EEXIST; | |
0854db2a | 2453 | list_for_each_entry(tmp, &nft_net->commit_list, list) { |
c6cc94df FW |
2454 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && |
2455 | tmp->ctx.table == table && | |
2456 | nft_trans_chain_update(tmp) && | |
2457 | nft_trans_chain_name(tmp) && | |
2458 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
98a381a7 | 2459 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); |
c6cc94df FW |
2460 | kfree(name); |
2461 | goto err; | |
2462 | } | |
2c4a488a | 2463 | } |
c6cc94df FW |
2464 | |
2465 | nft_trans_chain_name(trans) = name; | |
2c4a488a | 2466 | } |
0854db2a | 2467 | nft_trans_commit_list_add_tail(ctx->net, trans); |
2c4a488a PNA |
2468 | |
2469 | return 0; | |
c6cc94df FW |
2470 | err: |
2471 | free_percpu(stats); | |
2472 | kfree(trans); | |
2473 | return err; | |
2c4a488a PNA |
2474 | } |
2475 | ||
837830a4 | 2476 | static struct nft_chain *nft_chain_lookup_byid(const struct net *net, |
95f466d2 | 2477 | const struct nft_table *table, |
837830a4 PNA |
2478 | const struct nlattr *nla) |
2479 | { | |
d59d2f82 | 2480 | struct nftables_pernet *nft_net = nft_pernet(net); |
837830a4 PNA |
2481 | u32 id = ntohl(nla_get_be32(nla)); |
2482 | struct nft_trans *trans; | |
2483 | ||
0854db2a | 2484 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
837830a4 PNA |
2485 | struct nft_chain *chain = trans->ctx.chain; |
2486 | ||
2487 | if (trans->msg_type == NFT_MSG_NEWCHAIN && | |
95f466d2 | 2488 | chain->table == table && |
837830a4 PNA |
2489 | id == nft_trans_chain_id(trans)) |
2490 | return chain; | |
2491 | } | |
2492 | return ERR_PTR(-ENOENT); | |
2493 | } | |
2494 | ||
7dab8ee3 PNA |
2495 | static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info, |
2496 | const struct nlattr * const nla[]) | |
96518518 | 2497 | { |
7dab8ee3 | 2498 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
2499 | struct netlink_ext_ack *extack = info->extack; |
2500 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2501 | u8 family = info->nfmsg->nfgen_family; |
74cccc3d | 2502 | struct nft_chain *chain = NULL; |
7dab8ee3 | 2503 | struct net *net = info->net; |
36dd1bcc | 2504 | const struct nlattr *attr; |
96518518 | 2505 | struct nft_table *table; |
57de2a0c | 2506 | u8 policy = NF_ACCEPT; |
4035285f | 2507 | struct nft_ctx ctx; |
96518518 | 2508 | u64 handle = 0; |
c9626a2c | 2509 | u32 flags = 0; |
96518518 | 2510 | |
0854db2a | 2511 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 2512 | |
6001a930 PNA |
2513 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2514 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2515 | if (IS_ERR(table)) { |
2516 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2517 | return PTR_ERR(table); |
36dd1bcc | 2518 | } |
96518518 | 2519 | |
96518518 | 2520 | chain = NULL; |
36dd1bcc | 2521 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2522 | |
2523 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2524 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2525 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2526 | if (IS_ERR(chain)) { |
2527 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2528 | return PTR_ERR(chain); |
36dd1bcc PNA |
2529 | } |
2530 | attr = nla[NFTA_CHAIN_HANDLE]; | |
74cccc3d | 2531 | } else if (nla[NFTA_CHAIN_NAME]) { |
f102d66b | 2532 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2533 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2534 | if (PTR_ERR(chain) != -ENOENT) { |
2535 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2536 | return PTR_ERR(chain); |
36dd1bcc | 2537 | } |
96518518 PM |
2538 | chain = NULL; |
2539 | } | |
74cccc3d PNA |
2540 | } else if (!nla[NFTA_CHAIN_ID]) { |
2541 | return -EINVAL; | |
96518518 PM |
2542 | } |
2543 | ||
57de2a0c | 2544 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2545 | if (chain != NULL && |
36dd1bcc PNA |
2546 | !nft_is_base_chain(chain)) { |
2547 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2548 | return -EOPNOTSUPP; |
36dd1bcc | 2549 | } |
d6b6cb1d PNA |
2550 | |
2551 | if (chain == NULL && | |
36dd1bcc PNA |
2552 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2553 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2554 | return -EOPNOTSUPP; |
36dd1bcc | 2555 | } |
57de2a0c | 2556 | |
8f46df18 | 2557 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2558 | switch (policy) { |
2559 | case NF_DROP: | |
2560 | case NF_ACCEPT: | |
2561 | break; | |
2562 | default: | |
2563 | return -EINVAL; | |
2564 | } | |
2565 | } | |
2566 | ||
c9626a2c PNA |
2567 | if (nla[NFTA_CHAIN_FLAGS]) |
2568 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2569 | else if (chain) |
2570 | flags = chain->flags; | |
c9626a2c | 2571 | |
c1f79a2e PNA |
2572 | if (flags & ~NFT_CHAIN_FLAGS) |
2573 | return -EOPNOTSUPP; | |
2574 | ||
7dab8ee3 | 2575 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
4035285f | 2576 | |
96518518 | 2577 | if (chain != NULL) { |
5dc52d83 PNA |
2578 | if (chain->flags & NFT_CHAIN_BINDING) |
2579 | return -EINVAL; | |
2580 | ||
7dab8ee3 | 2581 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 2582 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2583 | return -EEXIST; |
36dd1bcc | 2584 | } |
7dab8ee3 | 2585 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
2586 | return -EOPNOTSUPP; |
2587 | ||
67c49de4 | 2588 | flags |= chain->flags & NFT_CHAIN_BASE; |
98a381a7 PNA |
2589 | return nf_tables_updchain(&ctx, genmask, policy, flags, attr, |
2590 | extack); | |
96518518 PM |
2591 | } |
2592 | ||
983c4fcb | 2593 | return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack); |
96518518 PM |
2594 | } |
2595 | ||
7dab8ee3 PNA |
2596 | static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info, |
2597 | const struct nlattr * const nla[]) | |
96518518 | 2598 | { |
7dab8ee3 PNA |
2599 | struct netlink_ext_ack *extack = info->extack; |
2600 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2601 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 2602 | struct net *net = info->net; |
36dd1bcc | 2603 | const struct nlattr *attr; |
96518518 PM |
2604 | struct nft_table *table; |
2605 | struct nft_chain *chain; | |
9dee1474 | 2606 | struct nft_rule *rule; |
91c7b38d | 2607 | struct nft_ctx ctx; |
3ecbfd65 | 2608 | u64 handle; |
9dee1474 PNA |
2609 | u32 use; |
2610 | int err; | |
96518518 | 2611 | |
6001a930 PNA |
2612 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2613 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2614 | if (IS_ERR(table)) { |
2615 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2616 | return PTR_ERR(table); |
36dd1bcc | 2617 | } |
96518518 | 2618 | |
3ecbfd65 | 2619 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2620 | attr = nla[NFTA_CHAIN_HANDLE]; |
2621 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2622 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2623 | } else { |
36dd1bcc | 2624 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2625 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2626 | } |
36dd1bcc PNA |
2627 | if (IS_ERR(chain)) { |
2628 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2629 | return PTR_ERR(chain); |
36dd1bcc | 2630 | } |
9dee1474 | 2631 | |
7dab8ee3 | 2632 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
9dee1474 | 2633 | chain->use > 0) |
96518518 PM |
2634 | return -EBUSY; |
2635 | ||
7dab8ee3 | 2636 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0165d932 | 2637 | |
9dee1474 PNA |
2638 | use = chain->use; |
2639 | list_for_each_entry(rule, &chain->rules, list) { | |
2640 | if (!nft_is_active_next(net, rule)) | |
2641 | continue; | |
2642 | use--; | |
2643 | ||
2644 | err = nft_delrule(&ctx, rule); | |
2645 | if (err < 0) | |
2646 | return err; | |
2647 | } | |
2648 | ||
2649 | /* There are rules and elements that are still holding references to us, | |
2650 | * we cannot do a recursive removal in this case. | |
2651 | */ | |
36dd1bcc PNA |
2652 | if (use > 0) { |
2653 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2654 | return -EBUSY; |
36dd1bcc | 2655 | } |
9dee1474 | 2656 | |
ee01d542 | 2657 | return nft_delchain(&ctx); |
96518518 PM |
2658 | } |
2659 | ||
96518518 PM |
2660 | /* |
2661 | * Expressions | |
2662 | */ | |
2663 | ||
2664 | /** | |
ef1f7df9 | 2665 | * nft_register_expr - register nf_tables expr type |
3db86c39 | 2666 | * @type: expr type |
96518518 | 2667 | * |
ef1f7df9 | 2668 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2669 | * success or a negative errno code otherwise. |
2670 | */ | |
ef1f7df9 | 2671 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2672 | { |
2673 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2674 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2675 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2676 | else |
e688a7f8 | 2677 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2678 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2679 | return 0; | |
2680 | } | |
2681 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2682 | ||
2683 | /** | |
ef1f7df9 | 2684 | * nft_unregister_expr - unregister nf_tables expr type |
3db86c39 | 2685 | * @type: expr type |
96518518 | 2686 | * |
ef1f7df9 | 2687 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2688 | */ |
ef1f7df9 | 2689 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2690 | { |
2691 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2692 | list_del_rcu(&type->list); |
96518518 PM |
2693 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2694 | } | |
2695 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2696 | ||
64d46806 PM |
2697 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2698 | struct nlattr *nla) | |
96518518 | 2699 | { |
9cff126f | 2700 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2701 | |
ef1f7df9 | 2702 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2703 | if (!nla_strcmp(nla, type->name)) { |
2704 | if (!type->family && !candidate) | |
2705 | candidate = type; | |
2706 | else if (type->family == family) | |
2707 | candidate = type; | |
2708 | } | |
96518518 | 2709 | } |
9cff126f | 2710 | return candidate; |
96518518 PM |
2711 | } |
2712 | ||
b9c04ae7 PNA |
2713 | #ifdef CONFIG_MODULES |
2714 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
2715 | struct nlattr *nla) | |
2716 | { | |
eb014de4 PNA |
2717 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
2718 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
2719 | return -EAGAIN; |
2720 | ||
2721 | return 0; | |
2722 | } | |
2723 | #endif | |
2724 | ||
452238e8 FW |
2725 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
2726 | u8 family, | |
64d46806 | 2727 | struct nlattr *nla) |
96518518 | 2728 | { |
ef1f7df9 | 2729 | const struct nft_expr_type *type; |
96518518 PM |
2730 | |
2731 | if (nla == NULL) | |
2732 | return ERR_PTR(-EINVAL); | |
2733 | ||
64d46806 | 2734 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
2735 | if (type != NULL && try_module_get(type->owner)) |
2736 | return type; | |
96518518 | 2737 | |
f102d66b | 2738 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 2739 | #ifdef CONFIG_MODULES |
ef1f7df9 | 2740 | if (type == NULL) { |
b9c04ae7 | 2741 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
2742 | return ERR_PTR(-EAGAIN); |
2743 | ||
eb014de4 PNA |
2744 | if (nft_request_module(net, "nft-expr-%.*s", |
2745 | nla_len(nla), | |
2746 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
2747 | return ERR_PTR(-EAGAIN); |
2748 | } | |
2749 | #endif | |
2750 | return ERR_PTR(-ENOENT); | |
2751 | } | |
2752 | ||
2753 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
2754 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
2755 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
2756 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
2757 | }; | |
2758 | ||
2759 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
2760 | const struct nft_expr *expr) | |
2761 | { | |
ef1f7df9 | 2762 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
2763 | goto nla_put_failure; |
2764 | ||
2765 | if (expr->ops->dump) { | |
ae0be8de MK |
2766 | struct nlattr *data = nla_nest_start_noflag(skb, |
2767 | NFTA_EXPR_DATA); | |
96518518 PM |
2768 | if (data == NULL) |
2769 | goto nla_put_failure; | |
2770 | if (expr->ops->dump(skb, expr) < 0) | |
2771 | goto nla_put_failure; | |
2772 | nla_nest_end(skb, data); | |
2773 | } | |
2774 | ||
2775 | return skb->len; | |
2776 | ||
2777 | nla_put_failure: | |
2778 | return -1; | |
2779 | }; | |
2780 | ||
0b2d8a7b PM |
2781 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
2782 | const struct nft_expr *expr) | |
2783 | { | |
2784 | struct nlattr *nest; | |
2785 | ||
ae0be8de | 2786 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
2787 | if (!nest) |
2788 | goto nla_put_failure; | |
2789 | if (nf_tables_fill_expr_info(skb, expr) < 0) | |
2790 | goto nla_put_failure; | |
2791 | nla_nest_end(skb, nest); | |
2792 | return 0; | |
2793 | ||
2794 | nla_put_failure: | |
2795 | return -1; | |
2796 | } | |
2797 | ||
96518518 PM |
2798 | struct nft_expr_info { |
2799 | const struct nft_expr_ops *ops; | |
83d9dcba | 2800 | const struct nlattr *attr; |
ef1f7df9 | 2801 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
2802 | }; |
2803 | ||
0ca743a5 PNA |
2804 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
2805 | const struct nlattr *nla, | |
96518518 PM |
2806 | struct nft_expr_info *info) |
2807 | { | |
ef1f7df9 | 2808 | const struct nft_expr_type *type; |
96518518 | 2809 | const struct nft_expr_ops *ops; |
ef1f7df9 | 2810 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
2811 | int err; |
2812 | ||
8cb08174 JB |
2813 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
2814 | nft_expr_policy, NULL); | |
96518518 PM |
2815 | if (err < 0) |
2816 | return err; | |
2817 | ||
452238e8 | 2818 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
2819 | if (IS_ERR(type)) |
2820 | return PTR_ERR(type); | |
2821 | ||
2822 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
2823 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
2824 | tb[NFTA_EXPR_DATA], | |
2825 | type->policy, NULL); | |
ef1f7df9 PM |
2826 | if (err < 0) |
2827 | goto err1; | |
2828 | } else | |
2829 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
2830 | ||
2831 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
2832 | ops = type->select_ops(ctx, |
2833 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
2834 | if (IS_ERR(ops)) { |
2835 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
2836 | #ifdef CONFIG_MODULES |
2837 | if (err == -EAGAIN) | |
eb014de4 PNA |
2838 | if (nft_expr_type_request_module(ctx->net, |
2839 | ctx->family, | |
2840 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
2841 | err = -ENOENT; | |
0ef1efd1 | 2842 | #endif |
ef1f7df9 PM |
2843 | goto err1; |
2844 | } | |
2845 | } else | |
2846 | ops = type->ops; | |
2847 | ||
83d9dcba | 2848 | info->attr = nla; |
96518518 | 2849 | info->ops = ops; |
83d9dcba | 2850 | |
96518518 | 2851 | return 0; |
ef1f7df9 PM |
2852 | |
2853 | err1: | |
2854 | module_put(type->owner); | |
2855 | return err; | |
96518518 PM |
2856 | } |
2857 | ||
2858 | static int nf_tables_newexpr(const struct nft_ctx *ctx, | |
7dab8ee3 | 2859 | const struct nft_expr_info *expr_info, |
96518518 PM |
2860 | struct nft_expr *expr) |
2861 | { | |
7dab8ee3 | 2862 | const struct nft_expr_ops *ops = expr_info->ops; |
96518518 PM |
2863 | int err; |
2864 | ||
2865 | expr->ops = ops; | |
2866 | if (ops->init) { | |
7dab8ee3 | 2867 | err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb); |
96518518 PM |
2868 | if (err < 0) |
2869 | goto err1; | |
2870 | } | |
2871 | ||
96518518 | 2872 | return 0; |
96518518 PM |
2873 | err1: |
2874 | expr->ops = NULL; | |
2875 | return err; | |
2876 | } | |
2877 | ||
62472bce PM |
2878 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
2879 | struct nft_expr *expr) | |
96518518 | 2880 | { |
3f3a390d PNA |
2881 | const struct nft_expr_type *type = expr->ops->type; |
2882 | ||
96518518 | 2883 | if (expr->ops->destroy) |
62472bce | 2884 | expr->ops->destroy(ctx, expr); |
3f3a390d | 2885 | module_put(type->owner); |
96518518 PM |
2886 | } |
2887 | ||
795a6d6b PNA |
2888 | static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
2889 | const struct nlattr *nla) | |
0b2d8a7b | 2890 | { |
7dab8ee3 | 2891 | struct nft_expr_info expr_info; |
0b2d8a7b | 2892 | struct nft_expr *expr; |
b8e20400 | 2893 | struct module *owner; |
0b2d8a7b PM |
2894 | int err; |
2895 | ||
7dab8ee3 | 2896 | err = nf_tables_expr_parse(ctx, nla, &expr_info); |
0b2d8a7b | 2897 | if (err < 0) |
52077804 PNA |
2898 | goto err_expr_parse; |
2899 | ||
2900 | err = -EOPNOTSUPP; | |
2901 | if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL)) | |
2902 | goto err_expr_stateful; | |
0b2d8a7b PM |
2903 | |
2904 | err = -ENOMEM; | |
33758c89 | 2905 | expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT); |
0b2d8a7b | 2906 | if (expr == NULL) |
52077804 | 2907 | goto err_expr_stateful; |
0b2d8a7b | 2908 | |
7dab8ee3 | 2909 | err = nf_tables_newexpr(ctx, &expr_info, expr); |
0b2d8a7b | 2910 | if (err < 0) |
52077804 | 2911 | goto err_expr_new; |
0b2d8a7b PM |
2912 | |
2913 | return expr; | |
52077804 | 2914 | err_expr_new: |
6cafaf47 | 2915 | kfree(expr); |
52077804 | 2916 | err_expr_stateful: |
7dab8ee3 PNA |
2917 | owner = expr_info.ops->type->owner; |
2918 | if (expr_info.ops->type->release_ops) | |
2919 | expr_info.ops->type->release_ops(expr_info.ops); | |
b8e20400 PNA |
2920 | |
2921 | module_put(owner); | |
52077804 | 2922 | err_expr_parse: |
0b2d8a7b PM |
2923 | return ERR_PTR(err); |
2924 | } | |
2925 | ||
c604cc69 PNA |
2926 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
2927 | { | |
2928 | int err; | |
2929 | ||
2930 | if (src->ops->clone) { | |
2931 | dst->ops = src->ops; | |
2932 | err = src->ops->clone(dst, src); | |
2933 | if (err < 0) | |
2934 | return err; | |
2935 | } else { | |
2936 | memcpy(dst, src, src->ops->size); | |
2937 | } | |
2938 | ||
2939 | __module_get(src->ops->type->owner); | |
2940 | ||
2941 | return 0; | |
2942 | } | |
2943 | ||
0b2d8a7b PM |
2944 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
2945 | { | |
2946 | nf_tables_expr_destroy(ctx, expr); | |
2947 | kfree(expr); | |
2948 | } | |
2949 | ||
96518518 PM |
2950 | /* |
2951 | * Rules | |
2952 | */ | |
2953 | ||
cac20fcd PNA |
2954 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
2955 | u64 handle) | |
96518518 PM |
2956 | { |
2957 | struct nft_rule *rule; | |
2958 | ||
2959 | // FIXME: this sucks | |
d9adf22a | 2960 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
2961 | if (handle == rule->handle) |
2962 | return rule; | |
2963 | } | |
2964 | ||
2965 | return ERR_PTR(-ENOENT); | |
2966 | } | |
2967 | ||
cac20fcd PNA |
2968 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
2969 | const struct nlattr *nla) | |
96518518 PM |
2970 | { |
2971 | if (nla == NULL) | |
2972 | return ERR_PTR(-EINVAL); | |
2973 | ||
cac20fcd | 2974 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
2975 | } |
2976 | ||
2977 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
2978 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
2979 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
2980 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
2981 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
2982 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
2983 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 2984 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 2985 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
2986 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
2987 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 2988 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 2989 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
837830a4 | 2990 | [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
2991 | }; |
2992 | ||
84d7fce6 PNA |
2993 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
2994 | u32 portid, u32 seq, int event, | |
2995 | u32 flags, int family, | |
96518518 PM |
2996 | const struct nft_table *table, |
2997 | const struct nft_chain *chain, | |
e189ae16 | 2998 | const struct nft_rule *rule, u64 handle) |
96518518 PM |
2999 | { |
3000 | struct nlmsghdr *nlh; | |
96518518 PM |
3001 | const struct nft_expr *expr, *next; |
3002 | struct nlattr *list; | |
dedb67c4 | 3003 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 3004 | |
19c28b13 PNA |
3005 | nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0, |
3006 | nft_base_seq(net)); | |
3007 | if (!nlh) | |
96518518 PM |
3008 | goto nla_put_failure; |
3009 | ||
96518518 PM |
3010 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
3011 | goto nla_put_failure; | |
3012 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
3013 | goto nla_put_failure; | |
b46f6ded ND |
3014 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
3015 | NFTA_RULE_PAD)) | |
96518518 PM |
3016 | goto nla_put_failure; |
3017 | ||
e189ae16 PNA |
3018 | if (event != NFT_MSG_DELRULE && handle) { |
3019 | if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle), | |
b46f6ded | 3020 | NFTA_RULE_PAD)) |
5e948466 EL |
3021 | goto nla_put_failure; |
3022 | } | |
3023 | ||
b72920f6 PNA |
3024 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) |
3025 | nft_flow_rule_stats(chain, rule); | |
3026 | ||
ae0be8de | 3027 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
3028 | if (list == NULL) |
3029 | goto nla_put_failure; | |
3030 | nft_rule_for_each_expr(expr, next, rule) { | |
0b2d8a7b | 3031 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
96518518 | 3032 | goto nla_put_failure; |
96518518 PM |
3033 | } |
3034 | nla_nest_end(skb, list); | |
3035 | ||
86f1ec32 PM |
3036 | if (rule->udata) { |
3037 | struct nft_userdata *udata = nft_userdata(rule); | |
3038 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
3039 | udata->data) < 0) | |
3040 | goto nla_put_failure; | |
3041 | } | |
0768b3b3 | 3042 | |
053c095a JB |
3043 | nlmsg_end(skb, nlh); |
3044 | return 0; | |
96518518 PM |
3045 | |
3046 | nla_put_failure: | |
3047 | nlmsg_trim(skb, nlh); | |
3048 | return -1; | |
3049 | } | |
3050 | ||
25e94a99 PNA |
3051 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
3052 | const struct nft_rule *rule, int event) | |
96518518 | 3053 | { |
d59d2f82 | 3054 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
e189ae16 | 3055 | const struct nft_rule *prule; |
96518518 | 3056 | struct sk_buff *skb; |
e189ae16 PNA |
3057 | u64 handle = 0; |
3058 | u16 flags = 0; | |
96518518 PM |
3059 | int err; |
3060 | ||
128ad332 PNA |
3061 | if (!ctx->report && |
3062 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3063 | return; |
96518518 | 3064 | |
96518518 PM |
3065 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
3066 | if (skb == NULL) | |
3067 | goto err; | |
3068 | ||
e189ae16 PNA |
3069 | if (event == NFT_MSG_NEWRULE && |
3070 | !list_is_first(&rule->list, &ctx->chain->rules) && | |
3071 | !list_is_last(&rule->list, &ctx->chain->rules)) { | |
3072 | prule = list_prev_entry(rule, list); | |
3073 | handle = prule->handle; | |
3074 | } | |
3075 | if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE)) | |
3076 | flags |= NLM_F_APPEND; | |
6fb721cf PNA |
3077 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
3078 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
e189ae16 | 3079 | |
84d7fce6 | 3080 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
e189ae16 PNA |
3081 | event, flags, ctx->family, ctx->table, |
3082 | ctx->chain, rule, handle); | |
96518518 PM |
3083 | if (err < 0) { |
3084 | kfree_skb(skb); | |
3085 | goto err; | |
3086 | } | |
3087 | ||
0854db2a | 3088 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 3089 | return; |
96518518 | 3090 | err: |
25e94a99 | 3091 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
3092 | } |
3093 | ||
6e1f760e | 3094 | struct nft_rule_dump_ctx { |
e46abbcc | 3095 | char *table; |
b7263e07 | 3096 | char *chain; |
6e1f760e PNA |
3097 | }; |
3098 | ||
241faece PS |
3099 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
3100 | unsigned int *idx, | |
3101 | struct netlink_callback *cb, | |
3102 | const struct nft_table *table, | |
3103 | const struct nft_chain *chain) | |
3104 | { | |
3105 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 3106 | const struct nft_rule *rule, *prule; |
241faece | 3107 | unsigned int s_idx = cb->args[0]; |
e189ae16 | 3108 | u64 handle; |
241faece | 3109 | |
2c82c7e7 | 3110 | prule = NULL; |
241faece PS |
3111 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
3112 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 3113 | goto cont_skip; |
241faece PS |
3114 | if (*idx < s_idx) |
3115 | goto cont; | |
3116 | if (*idx > s_idx) { | |
3117 | memset(&cb->args[1], 0, | |
3118 | sizeof(cb->args) - sizeof(cb->args[0])); | |
3119 | } | |
e189ae16 PNA |
3120 | if (prule) |
3121 | handle = prule->handle; | |
3122 | else | |
3123 | handle = 0; | |
3124 | ||
241faece PS |
3125 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
3126 | cb->nlh->nlmsg_seq, | |
3127 | NFT_MSG_NEWRULE, | |
3128 | NLM_F_MULTI | NLM_F_APPEND, | |
3129 | table->family, | |
e189ae16 | 3130 | table, chain, rule, handle) < 0) |
310529e6 | 3131 | return 1; |
241faece PS |
3132 | |
3133 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
3134 | cont: | |
2c82c7e7 FW |
3135 | prule = rule; |
3136 | cont_skip: | |
241faece PS |
3137 | (*idx)++; |
3138 | } | |
310529e6 | 3139 | return 0; |
241faece PS |
3140 | } |
3141 | ||
96518518 PM |
3142 | static int nf_tables_dump_rules(struct sk_buff *skb, |
3143 | struct netlink_callback *cb) | |
3144 | { | |
3145 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 3146 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 3147 | struct nft_table *table; |
96518518 | 3148 | const struct nft_chain *chain; |
241faece | 3149 | unsigned int idx = 0; |
99633ab2 | 3150 | struct net *net = sock_net(skb->sk); |
96518518 | 3151 | int family = nfmsg->nfgen_family; |
0854db2a | 3152 | struct nftables_pernet *nft_net; |
96518518 | 3153 | |
e688a7f8 | 3154 | rcu_read_lock(); |
d59d2f82 | 3155 | nft_net = nft_pernet(net); |
34002783 | 3156 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 3157 | |
0854db2a | 3158 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 3159 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
3160 | continue; |
3161 | ||
3162 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
3163 | continue; |
3164 | ||
715849ab | 3165 | if (ctx && ctx->table && ctx->chain) { |
241faece | 3166 | struct rhlist_head *list, *tmp; |
6e1f760e | 3167 | |
241faece PS |
3168 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
3169 | nft_chain_ht_params); | |
3170 | if (!list) | |
3171 | goto done; | |
3172 | ||
3173 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
3174 | if (!nft_is_active(net, chain)) | |
3175 | continue; | |
3176 | __nf_tables_dump_rules(skb, &idx, | |
3177 | cb, table, chain); | |
3178 | break; | |
96518518 | 3179 | } |
241faece | 3180 | goto done; |
96518518 | 3181 | } |
241faece PS |
3182 | |
3183 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
3184 | if (__nf_tables_dump_rules(skb, &idx, cb, table, chain)) | |
3185 | goto done; | |
3186 | } | |
3187 | ||
3188 | if (ctx && ctx->table) | |
3189 | break; | |
96518518 PM |
3190 | } |
3191 | done: | |
e688a7f8 | 3192 | rcu_read_unlock(); |
310529e6 PS |
3193 | |
3194 | cb->args[0] = idx; | |
96518518 PM |
3195 | return skb->len; |
3196 | } | |
3197 | ||
90fd131a FW |
3198 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
3199 | { | |
3200 | const struct nlattr * const *nla = cb->data; | |
3201 | struct nft_rule_dump_ctx *ctx = NULL; | |
3202 | ||
3203 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
3204 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
3205 | if (!ctx) | |
3206 | return -ENOMEM; | |
3207 | ||
3208 | if (nla[NFTA_RULE_TABLE]) { | |
3209 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
3210 | GFP_ATOMIC); | |
3211 | if (!ctx->table) { | |
3212 | kfree(ctx); | |
3213 | return -ENOMEM; | |
3214 | } | |
3215 | } | |
3216 | if (nla[NFTA_RULE_CHAIN]) { | |
3217 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
3218 | GFP_ATOMIC); | |
3219 | if (!ctx->chain) { | |
3220 | kfree(ctx->table); | |
3221 | kfree(ctx); | |
3222 | return -ENOMEM; | |
3223 | } | |
3224 | } | |
3225 | } | |
3226 | ||
3227 | cb->data = ctx; | |
3228 | return 0; | |
3229 | } | |
3230 | ||
6e1f760e PNA |
3231 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
3232 | { | |
e46abbcc PS |
3233 | struct nft_rule_dump_ctx *ctx = cb->data; |
3234 | ||
3235 | if (ctx) { | |
3236 | kfree(ctx->table); | |
b7263e07 | 3237 | kfree(ctx->chain); |
e46abbcc PS |
3238 | kfree(ctx); |
3239 | } | |
6e1f760e PNA |
3240 | return 0; |
3241 | } | |
3242 | ||
d9adf22a | 3243 | /* called with rcu_read_lock held */ |
797d4980 PNA |
3244 | static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info, |
3245 | const struct nlattr * const nla[]) | |
96518518 | 3246 | { |
797d4980 PNA |
3247 | struct netlink_ext_ack *extack = info->extack; |
3248 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 3249 | u8 family = info->nfmsg->nfgen_family; |
96518518 PM |
3250 | const struct nft_chain *chain; |
3251 | const struct nft_rule *rule; | |
797d4980 | 3252 | struct net *net = info->net; |
1b2470e5 | 3253 | struct nft_table *table; |
96518518 | 3254 | struct sk_buff *skb2; |
96518518 PM |
3255 | int err; |
3256 | ||
797d4980 | 3257 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 | 3258 | struct netlink_dump_control c = { |
90fd131a | 3259 | .start= nf_tables_dump_rules_start, |
96518518 | 3260 | .dump = nf_tables_dump_rules, |
6e1f760e | 3261 | .done = nf_tables_dump_rules_done, |
d9adf22a | 3262 | .module = THIS_MODULE, |
90fd131a | 3263 | .data = (void *)nla, |
96518518 | 3264 | }; |
6e1f760e | 3265 | |
797d4980 | 3266 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
3267 | } |
3268 | ||
6001a930 | 3269 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0); |
36dd1bcc PNA |
3270 | if (IS_ERR(table)) { |
3271 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3272 | return PTR_ERR(table); |
36dd1bcc | 3273 | } |
96518518 | 3274 | |
f102d66b | 3275 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3276 | if (IS_ERR(chain)) { |
3277 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3278 | return PTR_ERR(chain); |
36dd1bcc | 3279 | } |
96518518 | 3280 | |
cac20fcd | 3281 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3282 | if (IS_ERR(rule)) { |
3283 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3284 | return PTR_ERR(rule); |
36dd1bcc | 3285 | } |
96518518 | 3286 | |
d9adf22a | 3287 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
3288 | if (!skb2) |
3289 | return -ENOMEM; | |
3290 | ||
84d7fce6 | 3291 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 3292 | info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
e189ae16 | 3293 | family, table, chain, rule, 0); |
96518518 | 3294 | if (err < 0) |
ee921183 | 3295 | goto err_fill_rule_info; |
96518518 | 3296 | |
ee921183 | 3297 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 3298 | |
ee921183 | 3299 | err_fill_rule_info: |
96518518 PM |
3300 | kfree_skb(skb2); |
3301 | return err; | |
3302 | } | |
3303 | ||
62472bce PM |
3304 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
3305 | struct nft_rule *rule) | |
96518518 | 3306 | { |
29e38801 | 3307 | struct nft_expr *expr, *next; |
96518518 PM |
3308 | |
3309 | /* | |
3310 | * Careful: some expressions might not be initialized in case this | |
3311 | * is called on error from nf_tables_newrule(). | |
3312 | */ | |
3313 | expr = nft_expr_first(rule); | |
31cc578a | 3314 | while (nft_expr_more(rule, expr)) { |
29e38801 | 3315 | next = nft_expr_next(expr); |
62472bce | 3316 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 3317 | expr = next; |
96518518 PM |
3318 | } |
3319 | kfree(rule); | |
3320 | } | |
3321 | ||
d0e2c7de | 3322 | void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae | 3323 | { |
f6ac8585 | 3324 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
3325 | nf_tables_rule_destroy(ctx, rule); |
3326 | } | |
3327 | ||
a654de8f PNA |
3328 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
3329 | { | |
3330 | struct nft_expr *expr, *last; | |
3331 | const struct nft_data *data; | |
3332 | struct nft_rule *rule; | |
3333 | int err; | |
3334 | ||
26b2f552 TY |
3335 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
3336 | return -EMLINK; | |
3337 | ||
a654de8f PNA |
3338 | list_for_each_entry(rule, &chain->rules, list) { |
3339 | if (!nft_is_active_next(ctx->net, rule)) | |
3340 | continue; | |
3341 | ||
3342 | nft_rule_for_each_expr(expr, last, rule) { | |
3343 | if (!expr->ops->validate) | |
3344 | continue; | |
3345 | ||
3346 | err = expr->ops->validate(ctx, expr, &data); | |
3347 | if (err < 0) | |
3348 | return err; | |
3349 | } | |
81ea0106 FW |
3350 | |
3351 | cond_resched(); | |
a654de8f PNA |
3352 | } |
3353 | ||
3354 | return 0; | |
3355 | } | |
3356 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
3357 | ||
3358 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
3359 | { | |
3360 | struct nft_chain *chain; | |
3361 | struct nft_ctx ctx = { | |
3362 | .net = net, | |
3363 | .family = table->family, | |
3364 | }; | |
3365 | int err; | |
3366 | ||
3367 | list_for_each_entry(chain, &table->chains, list) { | |
3368 | if (!nft_is_base_chain(chain)) | |
3369 | continue; | |
3370 | ||
3371 | ctx.chain = chain; | |
3372 | err = nft_chain_validate(&ctx, chain); | |
3373 | if (err < 0) | |
3374 | return err; | |
3375 | } | |
3376 | ||
3377 | return 0; | |
3378 | } | |
3379 | ||
75dd48e2 | 3380 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3381 | const struct nft_chain *chain, |
75dd48e2 PS |
3382 | const struct nlattr *nla); |
3383 | ||
1081d11b PNA |
3384 | #define NFT_RULE_MAXEXPRS 128 |
3385 | ||
7dab8ee3 PNA |
3386 | static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, |
3387 | const struct nlattr * const nla[]) | |
96518518 | 3388 | { |
7dab8ee3 | 3389 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
3390 | struct netlink_ext_ack *extack = info->extack; |
3391 | unsigned int size, i, n, ulen = 0, usize = 0; | |
3392 | u8 genmask = nft_genmask_next(info->net); | |
3393 | struct nft_rule *rule, *old_rule = NULL; | |
3394 | struct nft_expr_info *expr_info = NULL; | |
ef4b65e5 | 3395 | u8 family = info->nfmsg->nfgen_family; |
3c5e4462 | 3396 | struct nft_flow_rule *flow = NULL; |
7dab8ee3 | 3397 | struct net *net = info->net; |
7dab8ee3 | 3398 | struct nft_userdata *udata; |
96518518 PM |
3399 | struct nft_table *table; |
3400 | struct nft_chain *chain; | |
7dab8ee3 PNA |
3401 | struct nft_trans *trans; |
3402 | u64 handle, pos_handle; | |
96518518 PM |
3403 | struct nft_expr *expr; |
3404 | struct nft_ctx ctx; | |
3405 | struct nlattr *tmp; | |
96518518 | 3406 | int err, rem; |
96518518 | 3407 | |
0854db2a | 3408 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3409 | |
6001a930 PNA |
3410 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3411 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3412 | if (IS_ERR(table)) { |
3413 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3414 | return PTR_ERR(table); |
36dd1bcc | 3415 | } |
96518518 | 3416 | |
837830a4 PNA |
3417 | if (nla[NFTA_RULE_CHAIN]) { |
3418 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], | |
3419 | genmask); | |
3420 | if (IS_ERR(chain)) { | |
3421 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
3422 | return PTR_ERR(chain); | |
3423 | } | |
d0e2c7de PNA |
3424 | if (nft_chain_is_bound(chain)) |
3425 | return -EOPNOTSUPP; | |
3426 | ||
837830a4 | 3427 | } else if (nla[NFTA_RULE_CHAIN_ID]) { |
95f466d2 | 3428 | chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID]); |
837830a4 PNA |
3429 | if (IS_ERR(chain)) { |
3430 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]); | |
3431 | return PTR_ERR(chain); | |
3432 | } | |
3433 | } else { | |
3434 | return -EINVAL; | |
36dd1bcc | 3435 | } |
96518518 PM |
3436 | |
3437 | if (nla[NFTA_RULE_HANDLE]) { | |
3438 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3439 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3440 | if (IS_ERR(rule)) { |
3441 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3442 | return PTR_ERR(rule); |
36dd1bcc | 3443 | } |
96518518 | 3444 | |
7dab8ee3 | 3445 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 3446 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
96518518 | 3447 | return -EEXIST; |
36dd1bcc | 3448 | } |
7dab8ee3 | 3449 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
3450 | old_rule = rule; |
3451 | else | |
3452 | return -EOPNOTSUPP; | |
3453 | } else { | |
7dab8ee3 PNA |
3454 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) || |
3455 | info->nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3456 | return -EINVAL; |
3457 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
3458 | |
3459 | if (chain->use == UINT_MAX) | |
3460 | return -EOVERFLOW; | |
5e948466 | 3461 | |
447750f2 FW |
3462 | if (nla[NFTA_RULE_POSITION]) { |
3463 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3464 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3465 | if (IS_ERR(old_rule)) { | |
3466 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3467 | return PTR_ERR(old_rule); | |
3468 | } | |
75dd48e2 | 3469 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
36d5b291 | 3470 | old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]); |
75dd48e2 PS |
3471 | if (IS_ERR(old_rule)) { |
3472 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3473 | return PTR_ERR(old_rule); | |
3474 | } | |
36dd1bcc | 3475 | } |
5e948466 EL |
3476 | } |
3477 | ||
7dab8ee3 | 3478 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0ca743a5 | 3479 | |
96518518 PM |
3480 | n = 0; |
3481 | size = 0; | |
3482 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
7dab8ee3 PNA |
3483 | expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3484 | sizeof(struct nft_expr_info), | |
3485 | GFP_KERNEL); | |
3486 | if (!expr_info) | |
2a43ecf9 FW |
3487 | return -ENOMEM; |
3488 | ||
96518518 PM |
3489 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3490 | err = -EINVAL; | |
3491 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3c5e4462 | 3492 | goto err_release_expr; |
96518518 | 3493 | if (n == NFT_RULE_MAXEXPRS) |
3c5e4462 | 3494 | goto err_release_expr; |
7dab8ee3 | 3495 | err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]); |
c781471d PNA |
3496 | if (err < 0) { |
3497 | NL_SET_BAD_ATTR(extack, tmp); | |
3c5e4462 | 3498 | goto err_release_expr; |
c781471d | 3499 | } |
7dab8ee3 | 3500 | size += expr_info[n].ops->size; |
96518518 PM |
3501 | n++; |
3502 | } | |
3503 | } | |
9889840f PM |
3504 | /* Check for overflow of dlen field */ |
3505 | err = -EFBIG; | |
3506 | if (size >= 1 << 12) | |
3c5e4462 | 3507 | goto err_release_expr; |
96518518 | 3508 | |
86f1ec32 | 3509 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3510 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3511 | if (ulen > 0) |
3512 | usize = sizeof(struct nft_userdata) + ulen; | |
3513 | } | |
0768b3b3 | 3514 | |
96518518 | 3515 | err = -ENOMEM; |
33758c89 | 3516 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT); |
96518518 | 3517 | if (rule == NULL) |
3c5e4462 | 3518 | goto err_release_expr; |
96518518 | 3519 | |
889f7ee7 | 3520 | nft_activate_next(net, rule); |
0628b123 | 3521 | |
96518518 PM |
3522 | rule->handle = handle; |
3523 | rule->dlen = size; | |
86f1ec32 | 3524 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3525 | |
86f1ec32 PM |
3526 | if (ulen) { |
3527 | udata = nft_userdata(rule); | |
3528 | udata->len = ulen - 1; | |
3529 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3530 | } | |
96518518 | 3531 | |
96518518 PM |
3532 | expr = nft_expr_first(rule); |
3533 | for (i = 0; i < n; i++) { | |
7dab8ee3 | 3534 | err = nf_tables_newexpr(&ctx, &expr_info[i], expr); |
83d9dcba | 3535 | if (err < 0) { |
7dab8ee3 | 3536 | NL_SET_BAD_ATTR(extack, expr_info[i].attr); |
3c5e4462 | 3537 | goto err_release_rule; |
83d9dcba | 3538 | } |
a654de8f | 3539 | |
7dab8ee3 | 3540 | if (expr_info[i].ops->validate) |
a654de8f PNA |
3541 | nft_validate_state_update(net, NFT_VALIDATE_NEED); |
3542 | ||
7dab8ee3 | 3543 | expr_info[i].ops = NULL; |
96518518 PM |
3544 | expr = nft_expr_next(expr); |
3545 | } | |
3546 | ||
3c5e4462 PNA |
3547 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
3548 | flow = nft_flow_rule_create(net, rule); | |
3549 | if (IS_ERR(flow)) { | |
3550 | err = PTR_ERR(flow); | |
3551 | goto err_release_rule; | |
3552 | } | |
3553 | } | |
3554 | ||
7dab8ee3 | 3555 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) { |
2c964c55 PNA |
3556 | err = nft_delrule(&ctx, old_rule); |
3557 | if (err < 0) | |
3558 | goto err_destroy_flow_rule; | |
3559 | ||
ca089878 | 3560 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3561 | if (trans == NULL) { |
3562 | err = -ENOMEM; | |
3c5e4462 | 3563 | goto err_destroy_flow_rule; |
569ccae6 | 3564 | } |
569ccae6 FW |
3565 | list_add_tail_rcu(&rule->list, &old_rule->list); |
3566 | } else { | |
c9626a2c PNA |
3567 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3568 | if (!trans) { | |
569ccae6 | 3569 | err = -ENOMEM; |
3c5e4462 | 3570 | goto err_destroy_flow_rule; |
569ccae6 FW |
3571 | } |
3572 | ||
7dab8ee3 | 3573 | if (info->nlh->nlmsg_flags & NLM_F_APPEND) { |
569ccae6 FW |
3574 | if (old_rule) |
3575 | list_add_rcu(&rule->list, &old_rule->list); | |
3576 | else | |
3577 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3578 | } else { | |
3579 | if (old_rule) | |
3580 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3581 | else | |
3582 | list_add_rcu(&rule->list, &chain->rules); | |
3583 | } | |
0628b123 | 3584 | } |
7dab8ee3 | 3585 | kvfree(expr_info); |
4fefee57 | 3586 | chain->use++; |
96518518 | 3587 | |
3c5e4462 PNA |
3588 | if (flow) |
3589 | nft_trans_flow_rule(trans) = flow; | |
3590 | ||
0854db2a | 3591 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
a654de8f PNA |
3592 | return nft_table_validate(net, table); |
3593 | ||
3594 | return 0; | |
3c5e4462 PNA |
3595 | |
3596 | err_destroy_flow_rule: | |
4ca041f9 CIK |
3597 | if (flow) |
3598 | nft_flow_rule_destroy(flow); | |
3c5e4462 | 3599 | err_release_rule: |
bb7b40ae | 3600 | nf_tables_rule_release(&ctx, rule); |
3c5e4462 | 3601 | err_release_expr: |
96518518 | 3602 | for (i = 0; i < n; i++) { |
7dab8ee3 PNA |
3603 | if (expr_info[i].ops) { |
3604 | module_put(expr_info[i].ops->type->owner); | |
3605 | if (expr_info[i].ops->type->release_ops) | |
3606 | expr_info[i].ops->type->release_ops(expr_info[i].ops); | |
b25a31bf | 3607 | } |
96518518 | 3608 | } |
7dab8ee3 PNA |
3609 | kvfree(expr_info); |
3610 | ||
96518518 PM |
3611 | return err; |
3612 | } | |
3613 | ||
1a94e38d | 3614 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3615 | const struct nft_chain *chain, |
1a94e38d PNA |
3616 | const struct nlattr *nla) |
3617 | { | |
d59d2f82 | 3618 | struct nftables_pernet *nft_net = nft_pernet(net); |
1a94e38d PNA |
3619 | u32 id = ntohl(nla_get_be32(nla)); |
3620 | struct nft_trans *trans; | |
3621 | ||
0854db2a | 3622 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
1a94e38d PNA |
3623 | struct nft_rule *rule = nft_trans_rule(trans); |
3624 | ||
3625 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
36d5b291 | 3626 | trans->ctx.chain == chain && |
1a94e38d PNA |
3627 | id == nft_trans_rule_id(trans)) |
3628 | return rule; | |
3629 | } | |
3630 | return ERR_PTR(-ENOENT); | |
3631 | } | |
3632 | ||
7dab8ee3 PNA |
3633 | static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info, |
3634 | const struct nlattr * const nla[]) | |
96518518 | 3635 | { |
7dab8ee3 | 3636 | struct netlink_ext_ack *extack = info->extack; |
7dab8ee3 | 3637 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 3638 | u8 family = info->nfmsg->nfgen_family; |
cf9dc09d | 3639 | struct nft_chain *chain = NULL; |
7dab8ee3 PNA |
3640 | struct net *net = info->net; |
3641 | struct nft_table *table; | |
cf9dc09d | 3642 | struct nft_rule *rule; |
0628b123 | 3643 | struct nft_ctx ctx; |
ef4b65e5 | 3644 | int err = 0; |
96518518 | 3645 | |
6001a930 PNA |
3646 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3647 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3648 | if (IS_ERR(table)) { |
3649 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3650 | return PTR_ERR(table); |
36dd1bcc | 3651 | } |
96518518 | 3652 | |
cf9dc09d | 3653 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
3654 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
3655 | genmask); | |
36dd1bcc PNA |
3656 | if (IS_ERR(chain)) { |
3657 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
cf9dc09d | 3658 | return PTR_ERR(chain); |
36dd1bcc | 3659 | } |
d0e2c7de PNA |
3660 | if (nft_chain_is_bound(chain)) |
3661 | return -EOPNOTSUPP; | |
cf9dc09d | 3662 | } |
96518518 | 3663 | |
7dab8ee3 | 3664 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0628b123 | 3665 | |
cf9dc09d PNA |
3666 | if (chain) { |
3667 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 3668 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3669 | if (IS_ERR(rule)) { |
3670 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
cf9dc09d | 3671 | return PTR_ERR(rule); |
36dd1bcc | 3672 | } |
96518518 | 3673 | |
1a94e38d PNA |
3674 | err = nft_delrule(&ctx, rule); |
3675 | } else if (nla[NFTA_RULE_ID]) { | |
36d5b291 | 3676 | rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]); |
36dd1bcc PNA |
3677 | if (IS_ERR(rule)) { |
3678 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 3679 | return PTR_ERR(rule); |
36dd1bcc | 3680 | } |
1a94e38d | 3681 | |
5e266fe7 | 3682 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 3683 | } else { |
ce24b721 | 3684 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
3685 | } |
3686 | } else { | |
3687 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
3688 | if (!nft_is_active_next(net, chain)) |
3689 | continue; | |
3690 | ||
cf9dc09d | 3691 | ctx.chain = chain; |
ce24b721 | 3692 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
3693 | if (err < 0) |
3694 | break; | |
3695 | } | |
3696 | } | |
3697 | ||
3698 | return err; | |
3699 | } | |
3700 | ||
20a69341 PM |
3701 | /* |
3702 | * Sets | |
3703 | */ | |
e32a4dc6 FW |
3704 | static const struct nft_set_type *nft_set_types[] = { |
3705 | &nft_set_hash_fast_type, | |
3706 | &nft_set_hash_type, | |
3707 | &nft_set_rhash_type, | |
3708 | &nft_set_bitmap_type, | |
3709 | &nft_set_rbtree_type, | |
e6abef61 | 3710 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
7400b063 SB |
3711 | &nft_set_pipapo_avx2_type, |
3712 | #endif | |
e32a4dc6 FW |
3713 | &nft_set_pipapo_type, |
3714 | }; | |
20a69341 | 3715 | |
2b664957 | 3716 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
3717 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
3718 | NFT_SET_EVAL) | |
2b664957 | 3719 | |
71cc0873 | 3720 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 3721 | { |
71cc0873 | 3722 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
3723 | } |
3724 | ||
c50b960c PM |
3725 | /* |
3726 | * Select a set implementation based on the data characteristics and the | |
3727 | * given policy. The total memory use might not be known if no size is | |
3728 | * given, in that case the amount of memory per element is used. | |
3729 | */ | |
3730 | static const struct nft_set_ops * | |
2b664957 PNA |
3731 | nft_select_set_ops(const struct nft_ctx *ctx, |
3732 | const struct nlattr * const nla[], | |
c50b960c PM |
3733 | const struct nft_set_desc *desc, |
3734 | enum nft_set_policies policy) | |
20a69341 | 3735 | { |
d59d2f82 | 3736 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c50b960c PM |
3737 | const struct nft_set_ops *ops, *bops; |
3738 | struct nft_set_estimate est, best; | |
2b664957 PNA |
3739 | const struct nft_set_type *type; |
3740 | u32 flags = 0; | |
e32a4dc6 | 3741 | int i; |
20a69341 | 3742 | |
0854db2a | 3743 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3744 | lockdep_nfnl_nft_mutex_not_held(); |
e32a4dc6 | 3745 | |
2b664957 PNA |
3746 | if (nla[NFTA_SET_FLAGS] != NULL) |
3747 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 3748 | |
55af753c PNA |
3749 | bops = NULL; |
3750 | best.size = ~0; | |
3751 | best.lookup = ~0; | |
0b5a7874 | 3752 | best.space = ~0; |
c50b960c | 3753 | |
e32a4dc6 FW |
3754 | for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) { |
3755 | type = nft_set_types[i]; | |
71cc0873 | 3756 | ops = &type->ops; |
2b664957 | 3757 | |
71cc0873 | 3758 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 3759 | continue; |
2b664957 | 3760 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
3761 | continue; |
3762 | ||
3763 | switch (policy) { | |
3764 | case NFT_SET_POL_PERFORMANCE: | |
55af753c | 3765 | if (est.lookup < best.lookup) |
c50b960c | 3766 | break; |
644e334e PNA |
3767 | if (est.lookup == best.lookup && |
3768 | est.space < best.space) | |
3769 | break; | |
c50b960c PM |
3770 | continue; |
3771 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
3772 | if (!desc->size) { |
3773 | if (est.space < best.space) | |
3774 | break; | |
3775 | if (est.space == best.space && | |
3776 | est.lookup < best.lookup) | |
3777 | break; | |
4f2921ca | 3778 | } else if (est.size < best.size || !bops) { |
c50b960c | 3779 | break; |
0b5a7874 | 3780 | } |
c50b960c PM |
3781 | continue; |
3782 | default: | |
3783 | break; | |
3784 | } | |
3785 | ||
c50b960c PM |
3786 | bops = ops; |
3787 | best = est; | |
20a69341 PM |
3788 | } |
3789 | ||
c50b960c PM |
3790 | if (bops != NULL) |
3791 | return bops; | |
3792 | ||
20a69341 PM |
3793 | return ERR_PTR(-EOPNOTSUPP); |
3794 | } | |
3795 | ||
3796 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
3797 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
3798 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 3799 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 3800 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
3801 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
3802 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
3803 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
3804 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
3805 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
3806 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
3807 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 3808 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
3809 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
3810 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
3811 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
3812 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 3813 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 3814 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
65038428 | 3815 | [NFTA_SET_EXPR] = { .type = NLA_NESTED }, |
48b0ae04 | 3816 | [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED }, |
c50b960c PM |
3817 | }; |
3818 | ||
3819 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
3820 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 3821 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
3822 | }; |
3823 | ||
cac20fcd PNA |
3824 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
3825 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
3826 | { |
3827 | struct nft_set *set; | |
3828 | ||
3829 | if (nla == NULL) | |
3830 | return ERR_PTR(-EINVAL); | |
3831 | ||
d9adf22a | 3832 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
3833 | if (!nla_strcmp(nla, set->name) && |
3834 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
3835 | return set; |
3836 | } | |
3837 | return ERR_PTR(-ENOENT); | |
3838 | } | |
3839 | ||
cac20fcd PNA |
3840 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
3841 | const struct nlattr *nla, | |
3842 | u8 genmask) | |
3ecbfd65 HS |
3843 | { |
3844 | struct nft_set *set; | |
3845 | ||
3ecbfd65 HS |
3846 | list_for_each_entry(set, &table->sets, list) { |
3847 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
3848 | nft_active_genmask(set, genmask)) | |
3849 | return set; | |
3850 | } | |
3851 | return ERR_PTR(-ENOENT); | |
3852 | } | |
3853 | ||
cac20fcd | 3854 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
470ee20e | 3855 | const struct nft_table *table, |
cac20fcd | 3856 | const struct nlattr *nla, u8 genmask) |
958bee14 | 3857 | { |
d59d2f82 | 3858 | struct nftables_pernet *nft_net = nft_pernet(net); |
958bee14 | 3859 | u32 id = ntohl(nla_get_be32(nla)); |
d59d2f82 | 3860 | struct nft_trans *trans; |
958bee14 | 3861 | |
0854db2a | 3862 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
9c7f96fd AK |
3863 | if (trans->msg_type == NFT_MSG_NEWSET) { |
3864 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 3865 | |
9c7f96fd | 3866 | if (id == nft_trans_set_id(trans) && |
470ee20e | 3867 | set->table == table && |
9c7f96fd AK |
3868 | nft_active_genmask(set, genmask)) |
3869 | return set; | |
3870 | } | |
958bee14 PNA |
3871 | } |
3872 | return ERR_PTR(-ENOENT); | |
3873 | } | |
c7a72e3f | 3874 | |
10659cba PNA |
3875 | struct nft_set *nft_set_lookup_global(const struct net *net, |
3876 | const struct nft_table *table, | |
3877 | const struct nlattr *nla_set_name, | |
3878 | const struct nlattr *nla_set_id, | |
3879 | u8 genmask) | |
c7a72e3f PNA |
3880 | { |
3881 | struct nft_set *set; | |
3882 | ||
cac20fcd | 3883 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
3884 | if (IS_ERR(set)) { |
3885 | if (!nla_set_id) | |
3886 | return set; | |
3887 | ||
470ee20e | 3888 | set = nft_set_lookup_byid(net, table, nla_set_id, genmask); |
c7a72e3f PNA |
3889 | } |
3890 | return set; | |
3891 | } | |
10659cba | 3892 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 3893 | |
20a69341 PM |
3894 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
3895 | const char *name) | |
3896 | { | |
3897 | const struct nft_set *i; | |
3898 | const char *p; | |
3899 | unsigned long *inuse; | |
60eb1894 | 3900 | unsigned int n = 0, min = 0; |
20a69341 | 3901 | |
38745490 | 3902 | p = strchr(name, '%'); |
20a69341 PM |
3903 | if (p != NULL) { |
3904 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
3905 | return -EINVAL; | |
3906 | ||
3907 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
3908 | if (inuse == NULL) | |
3909 | return -ENOMEM; | |
60eb1894 | 3910 | cont: |
20a69341 | 3911 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
3912 | int tmp; |
3913 | ||
271c5ca8 | 3914 | if (!nft_is_active_next(ctx->net, i)) |
37a9cc52 | 3915 | continue; |
14662917 | 3916 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 3917 | continue; |
60eb1894 | 3918 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 3919 | continue; |
14662917 | 3920 | |
60eb1894 | 3921 | set_bit(tmp - min, inuse); |
20a69341 PM |
3922 | } |
3923 | ||
53b70287 | 3924 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
3925 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
3926 | min += BITS_PER_BYTE * PAGE_SIZE; | |
3927 | memset(inuse, 0, PAGE_SIZE); | |
3928 | goto cont; | |
3929 | } | |
20a69341 PM |
3930 | free_page((unsigned long)inuse); |
3931 | } | |
3932 | ||
33758c89 | 3933 | set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n); |
38745490 PS |
3934 | if (!set->name) |
3935 | return -ENOMEM; | |
3936 | ||
20a69341 | 3937 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
3938 | if (!nft_is_active_next(ctx->net, i)) |
3939 | continue; | |
e63aaaa6 AY |
3940 | if (!strcmp(set->name, i->name)) { |
3941 | kfree(set->name); | |
7fb6f78d | 3942 | set->name = NULL; |
20a69341 | 3943 | return -ENFILE; |
e63aaaa6 | 3944 | } |
20a69341 PM |
3945 | } |
3946 | return 0; | |
3947 | } | |
3948 | ||
917d80d3 | 3949 | int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
8e1102d5 FW |
3950 | { |
3951 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
3952 | u64 max = (u64)(~((u64)0)); | |
3953 | ||
3954 | max = div_u64(max, NSEC_PER_MSEC); | |
3955 | if (ms >= max) | |
3956 | return -ERANGE; | |
3957 | ||
3958 | ms *= NSEC_PER_MSEC; | |
3959 | *result = nsecs_to_jiffies64(ms); | |
3960 | return 0; | |
3961 | } | |
3962 | ||
917d80d3 | 3963 | __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 3964 | { |
3b15d09f | 3965 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
3966 | } |
3967 | ||
f3a2181e SB |
3968 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
3969 | const struct nft_set *set) | |
3970 | { | |
3971 | struct nlattr *concat, *field; | |
3972 | int i; | |
3973 | ||
3974 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
3975 | if (!concat) | |
3976 | return -ENOMEM; | |
3977 | ||
3978 | for (i = 0; i < set->field_count; i++) { | |
3979 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
3980 | if (!field) | |
3981 | return -ENOMEM; | |
3982 | ||
3983 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
3984 | htonl(set->field_len[i]))) | |
3985 | return -ENOMEM; | |
3986 | ||
3987 | nla_nest_end(skb, field); | |
3988 | } | |
3989 | ||
3990 | nla_nest_end(skb, concat); | |
3991 | ||
3992 | return 0; | |
3993 | } | |
3994 | ||
20a69341 PM |
3995 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
3996 | const struct nft_set *set, u16 event, u16 flags) | |
3997 | { | |
20a69341 | 3998 | struct nlmsghdr *nlh; |
128ad332 | 3999 | u32 portid = ctx->portid; |
65038428 | 4000 | struct nlattr *nest; |
128ad332 | 4001 | u32 seq = ctx->seq; |
48b0ae04 | 4002 | int i; |
20a69341 | 4003 | |
dedb67c4 | 4004 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
4005 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
4006 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
4007 | if (!nlh) | |
20a69341 PM |
4008 | goto nla_put_failure; |
4009 | ||
20a69341 PM |
4010 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
4011 | goto nla_put_failure; | |
4012 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
4013 | goto nla_put_failure; | |
3ecbfd65 HS |
4014 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
4015 | NFTA_SET_PAD)) | |
4016 | goto nla_put_failure; | |
20a69341 PM |
4017 | if (set->flags != 0) |
4018 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
4019 | goto nla_put_failure; | |
4020 | ||
4021 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
4022 | goto nla_put_failure; | |
4023 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
4024 | goto nla_put_failure; | |
4025 | if (set->flags & NFT_SET_MAP) { | |
4026 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
4027 | goto nla_put_failure; | |
4028 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
4029 | goto nla_put_failure; | |
4030 | } | |
8aeff920 PNA |
4031 | if (set->flags & NFT_SET_OBJECT && |
4032 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
4033 | goto nla_put_failure; | |
20a69341 | 4034 | |
761da293 | 4035 | if (set->timeout && |
d3e2a111 | 4036 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
8e1102d5 | 4037 | nf_jiffies64_to_msecs(set->timeout), |
b46f6ded | 4038 | NFTA_SET_PAD)) |
761da293 PM |
4039 | goto nla_put_failure; |
4040 | if (set->gc_int && | |
4041 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) | |
4042 | goto nla_put_failure; | |
4043 | ||
9363dc4b AB |
4044 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
4045 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
4046 | goto nla_put_failure; | |
4047 | } | |
4048 | ||
6f03bf43 PNA |
4049 | if (set->udata && |
4050 | nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) | |
e6d8ecac CFG |
4051 | goto nla_put_failure; |
4052 | ||
65038428 PNA |
4053 | nest = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
4054 | if (!nest) | |
c50b960c PM |
4055 | goto nla_put_failure; |
4056 | if (set->size && | |
4057 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
4058 | goto nla_put_failure; | |
f3a2181e SB |
4059 | |
4060 | if (set->field_count > 1 && | |
4061 | nf_tables_fill_set_concat(skb, set)) | |
4062 | goto nla_put_failure; | |
4063 | ||
65038428 PNA |
4064 | nla_nest_end(skb, nest); |
4065 | ||
8cfd9b0f | 4066 | if (set->num_exprs == 1) { |
65038428 | 4067 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR); |
8cfd9b0f | 4068 | if (nf_tables_fill_expr_info(skb, set->exprs[0]) < 0) |
65038428 PNA |
4069 | goto nla_put_failure; |
4070 | ||
4071 | nla_nest_end(skb, nest); | |
48b0ae04 PNA |
4072 | } else if (set->num_exprs > 1) { |
4073 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS); | |
4074 | if (nest == NULL) | |
65038428 PNA |
4075 | goto nla_put_failure; |
4076 | ||
48b0ae04 PNA |
4077 | for (i = 0; i < set->num_exprs; i++) { |
4078 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, | |
4079 | set->exprs[i]) < 0) | |
4080 | goto nla_put_failure; | |
4081 | } | |
65038428 PNA |
4082 | nla_nest_end(skb, nest); |
4083 | } | |
c50b960c | 4084 | |
053c095a JB |
4085 | nlmsg_end(skb, nlh); |
4086 | return 0; | |
20a69341 PM |
4087 | |
4088 | nla_put_failure: | |
4089 | nlmsg_trim(skb, nlh); | |
4090 | return -1; | |
4091 | } | |
4092 | ||
25e94a99 PNA |
4093 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
4094 | const struct nft_set *set, int event, | |
4095 | gfp_t gfp_flags) | |
20a69341 | 4096 | { |
d59d2f82 | 4097 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
128ad332 | 4098 | u32 portid = ctx->portid; |
6fb721cf PNA |
4099 | struct sk_buff *skb; |
4100 | u16 flags = 0; | |
20a69341 PM |
4101 | int err; |
4102 | ||
128ad332 PNA |
4103 | if (!ctx->report && |
4104 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 4105 | return; |
20a69341 | 4106 | |
31f8441c | 4107 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
4108 | if (skb == NULL) |
4109 | goto err; | |
4110 | ||
6fb721cf PNA |
4111 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
4112 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
4113 | ||
4114 | err = nf_tables_fill_set(skb, ctx, set, event, flags); | |
20a69341 PM |
4115 | if (err < 0) { |
4116 | kfree_skb(skb); | |
4117 | goto err; | |
4118 | } | |
4119 | ||
0854db2a | 4120 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 4121 | return; |
20a69341 | 4122 | err: |
25e94a99 | 4123 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
4124 | } |
4125 | ||
5b96af77 | 4126 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
4127 | { |
4128 | const struct nft_set *set; | |
4129 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
4130 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
4131 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 4132 | struct nft_ctx *ctx = cb->data, ctx_set; |
0854db2a | 4133 | struct nftables_pernet *nft_net; |
c9c8e485 PNA |
4134 | |
4135 | if (cb->args[1]) | |
4136 | return skb->len; | |
4137 | ||
e688a7f8 | 4138 | rcu_read_lock(); |
d59d2f82 | 4139 | nft_net = nft_pernet(net); |
34002783 | 4140 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 4141 | |
0854db2a | 4142 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 4143 | if (ctx->family != NFPROTO_UNSPEC && |
98319cb9 | 4144 | ctx->family != table->family) |
36596dad PNA |
4145 | continue; |
4146 | ||
4147 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
4148 | continue; |
4149 | ||
36596dad PNA |
4150 | if (cur_table) { |
4151 | if (cur_table != table) | |
c9c8e485 PNA |
4152 | continue; |
4153 | ||
36596dad | 4154 | cur_table = NULL; |
c9c8e485 | 4155 | } |
36596dad PNA |
4156 | idx = 0; |
4157 | list_for_each_entry_rcu(set, &table->sets, list) { | |
4158 | if (idx < s_idx) | |
4159 | goto cont; | |
4160 | if (!nft_is_active(net, set)) | |
4161 | goto cont; | |
5b96af77 | 4162 | |
36596dad PNA |
4163 | ctx_set = *ctx; |
4164 | ctx_set.table = table; | |
98319cb9 | 4165 | ctx_set.family = table->family; |
c9c8e485 | 4166 | |
36596dad PNA |
4167 | if (nf_tables_fill_set(skb, &ctx_set, set, |
4168 | NFT_MSG_NEWSET, | |
4169 | NLM_F_MULTI) < 0) { | |
4170 | cb->args[0] = idx; | |
4171 | cb->args[2] = (unsigned long) table; | |
4172 | goto done; | |
c9c8e485 | 4173 | } |
36596dad | 4174 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 4175 | cont: |
36596dad | 4176 | idx++; |
c9c8e485 | 4177 | } |
36596dad PNA |
4178 | if (s_idx) |
4179 | s_idx = 0; | |
c9c8e485 PNA |
4180 | } |
4181 | cb->args[1] = 1; | |
4182 | done: | |
e688a7f8 | 4183 | rcu_read_unlock(); |
c9c8e485 PNA |
4184 | return skb->len; |
4185 | } | |
4186 | ||
90fd131a FW |
4187 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
4188 | { | |
4189 | struct nft_ctx *ctx_dump = NULL; | |
4190 | ||
4191 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
4192 | if (ctx_dump == NULL) | |
4193 | return -ENOMEM; | |
4194 | ||
4195 | cb->data = ctx_dump; | |
4196 | return 0; | |
4197 | } | |
4198 | ||
5b96af77 | 4199 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 4200 | { |
5b96af77 PNA |
4201 | kfree(cb->data); |
4202 | return 0; | |
20a69341 PM |
4203 | } |
4204 | ||
d9adf22a | 4205 | /* called with rcu_read_lock held */ |
797d4980 PNA |
4206 | static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info, |
4207 | const struct nlattr * const nla[]) | |
20a69341 | 4208 | { |
797d4980 PNA |
4209 | struct netlink_ext_ack *extack = info->extack; |
4210 | u8 genmask = nft_genmask_cur(info->net); | |
67086651 PNA |
4211 | u8 family = info->nfmsg->nfgen_family; |
4212 | struct nft_table *table = NULL; | |
797d4980 | 4213 | struct net *net = info->net; |
20a69341 | 4214 | const struct nft_set *set; |
20a69341 | 4215 | struct sk_buff *skb2; |
797d4980 | 4216 | struct nft_ctx ctx; |
20a69341 PM |
4217 | int err; |
4218 | ||
67086651 PNA |
4219 | if (nla[NFTA_SET_TABLE]) { |
4220 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, | |
4221 | genmask, 0); | |
4222 | if (IS_ERR(table)) { | |
4223 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4224 | return PTR_ERR(table); | |
4225 | } | |
4226 | } | |
4227 | ||
4228 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); | |
20a69341 | 4229 | |
797d4980 | 4230 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
20a69341 | 4231 | struct netlink_dump_control c = { |
90fd131a | 4232 | .start = nf_tables_dump_sets_start, |
20a69341 | 4233 | .dump = nf_tables_dump_sets, |
5b96af77 | 4234 | .done = nf_tables_dump_sets_done, |
90fd131a | 4235 | .data = &ctx, |
d9adf22a | 4236 | .module = THIS_MODULE, |
20a69341 | 4237 | }; |
5b96af77 | 4238 | |
797d4980 | 4239 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
20a69341 PM |
4240 | } |
4241 | ||
c9c8e485 | 4242 | /* Only accept unspec with dump */ |
ef4b65e5 | 4243 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
c9c8e485 | 4244 | return -EAFNOSUPPORT; |
eaa2bcd6 PT |
4245 | if (!nla[NFTA_SET_TABLE]) |
4246 | return -EINVAL; | |
c9c8e485 | 4247 | |
67086651 | 4248 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
4249 | if (IS_ERR(set)) |
4250 | return PTR_ERR(set); | |
4251 | ||
d9adf22a | 4252 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
4253 | if (skb2 == NULL) |
4254 | return -ENOMEM; | |
4255 | ||
4256 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
4257 | if (err < 0) | |
ee921183 | 4258 | goto err_fill_set_info; |
20a69341 | 4259 | |
ee921183 | 4260 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
20a69341 | 4261 | |
ee921183 | 4262 | err_fill_set_info: |
20a69341 PM |
4263 | kfree_skb(skb2); |
4264 | return err; | |
4265 | } | |
4266 | ||
f3a2181e SB |
4267 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
4268 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
4269 | }; | |
4270 | ||
4271 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
4272 | struct nft_set_desc *desc) | |
4273 | { | |
4274 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
4275 | u32 len; | |
4276 | int err; | |
4277 | ||
fecf31ee PNA |
4278 | if (desc->field_count >= ARRAY_SIZE(desc->field_len)) |
4279 | return -E2BIG; | |
4280 | ||
f3a2181e SB |
4281 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, |
4282 | nft_concat_policy, NULL); | |
4283 | if (err < 0) | |
4284 | return err; | |
4285 | ||
4286 | if (!tb[NFTA_SET_FIELD_LEN]) | |
4287 | return -EINVAL; | |
4288 | ||
4289 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
fecf31ee PNA |
4290 | if (!len || len > U8_MAX) |
4291 | return -EINVAL; | |
f3a2181e SB |
4292 | |
4293 | desc->field_len[desc->field_count++] = len; | |
4294 | ||
4295 | return 0; | |
4296 | } | |
4297 | ||
4298 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
4299 | const struct nlattr *nla) | |
4300 | { | |
4301 | struct nlattr *attr; | |
fecf31ee PNA |
4302 | u32 num_regs = 0; |
4303 | int rem, err, i; | |
f3a2181e SB |
4304 | |
4305 | nla_for_each_nested(attr, nla, rem) { | |
4306 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
4307 | return -EINVAL; | |
4308 | ||
4309 | err = nft_set_desc_concat_parse(attr, desc); | |
4310 | if (err < 0) | |
4311 | return err; | |
4312 | } | |
4313 | ||
fecf31ee PNA |
4314 | for (i = 0; i < desc->field_count; i++) |
4315 | num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32)); | |
4316 | ||
4317 | if (num_regs > NFT_REG32_COUNT) | |
4318 | return -E2BIG; | |
4319 | ||
f3a2181e SB |
4320 | return 0; |
4321 | } | |
4322 | ||
f7e840ee | 4323 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
4324 | const struct nlattr *nla) |
4325 | { | |
4326 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
4327 | int err; | |
4328 | ||
8cb08174 JB |
4329 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
4330 | nft_set_desc_policy, NULL); | |
c50b960c PM |
4331 | if (err < 0) |
4332 | return err; | |
4333 | ||
4334 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
4335 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
4336 | if (da[NFTA_SET_DESC_CONCAT]) |
4337 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 4338 | |
f3a2181e | 4339 | return err; |
c50b960c PM |
4340 | } |
4341 | ||
7dab8ee3 PNA |
4342 | static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, |
4343 | const struct nlattr * const nla[]) | |
20a69341 | 4344 | { |
7dab8ee3 PNA |
4345 | u32 ktype, dtype, flags, policy, gc_int, objtype; |
4346 | struct netlink_ext_ack *extack = info->extack; | |
4347 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 4348 | u8 family = info->nfmsg->nfgen_family; |
20a69341 | 4349 | const struct nft_set_ops *ops; |
65038428 | 4350 | struct nft_expr *expr = NULL; |
7dab8ee3 PNA |
4351 | struct net *net = info->net; |
4352 | struct nft_set_desc desc; | |
20a69341 | 4353 | struct nft_table *table; |
7dab8ee3 | 4354 | unsigned char *udata; |
20a69341 PM |
4355 | struct nft_set *set; |
4356 | struct nft_ctx ctx; | |
6c8774a9 | 4357 | size_t alloc_size; |
761da293 | 4358 | u64 timeout; |
7dab8ee3 PNA |
4359 | char *name; |
4360 | int err, i; | |
e6d8ecac | 4361 | u16 udlen; |
7dab8ee3 | 4362 | u64 size; |
20a69341 PM |
4363 | |
4364 | if (nla[NFTA_SET_TABLE] == NULL || | |
4365 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
4366 | nla[NFTA_SET_KEY_LEN] == NULL || |
4367 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
4368 | return -EINVAL; |
4369 | ||
c50b960c PM |
4370 | memset(&desc, 0, sizeof(desc)); |
4371 | ||
20a69341 PM |
4372 | ktype = NFT_DATA_VALUE; |
4373 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { | |
4374 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); | |
4375 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
4376 | return -EINVAL; | |
4377 | } | |
4378 | ||
c50b960c | 4379 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 4380 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4381 | return -EINVAL; |
4382 | ||
4383 | flags = 0; | |
4384 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
4385 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
4386 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 4387 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 | 4388 | NFT_SET_MAP | NFT_SET_EVAL | |
b4e70d8d | 4389 | NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR)) |
d9583cdf | 4390 | return -EOPNOTSUPP; |
8aeff920 | 4391 | /* Only one of these operations is supported */ |
acab7131 FW |
4392 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
4393 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
4394 | return -EOPNOTSUPP; | |
4395 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
4396 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 4397 | return -EOPNOTSUPP; |
20a69341 PM |
4398 | } |
4399 | ||
4400 | dtype = 0; | |
20a69341 PM |
4401 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
4402 | if (!(flags & NFT_SET_MAP)) | |
4403 | return -EINVAL; | |
4404 | ||
4405 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); | |
4406 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
4407 | dtype != NFT_DATA_VERDICT) | |
4408 | return -EINVAL; | |
4409 | ||
4410 | if (dtype != NFT_DATA_VERDICT) { | |
4411 | if (nla[NFTA_SET_DATA_LEN] == NULL) | |
4412 | return -EINVAL; | |
c50b960c | 4413 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 4414 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4415 | return -EINVAL; |
4416 | } else | |
7d740264 | 4417 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
4418 | } else if (flags & NFT_SET_MAP) |
4419 | return -EINVAL; | |
4420 | ||
8aeff920 PNA |
4421 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
4422 | if (!(flags & NFT_SET_OBJECT)) | |
4423 | return -EINVAL; | |
4424 | ||
4425 | objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); | |
4426 | if (objtype == NFT_OBJECT_UNSPEC || | |
4427 | objtype > NFT_OBJECT_MAX) | |
d9583cdf | 4428 | return -EOPNOTSUPP; |
8aeff920 PNA |
4429 | } else if (flags & NFT_SET_OBJECT) |
4430 | return -EINVAL; | |
4431 | else | |
4432 | objtype = NFT_OBJECT_UNSPEC; | |
4433 | ||
761da293 PM |
4434 | timeout = 0; |
4435 | if (nla[NFTA_SET_TIMEOUT] != NULL) { | |
4436 | if (!(flags & NFT_SET_TIMEOUT)) | |
4437 | return -EINVAL; | |
8e1102d5 FW |
4438 | |
4439 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout); | |
4440 | if (err) | |
4441 | return err; | |
761da293 PM |
4442 | } |
4443 | gc_int = 0; | |
4444 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { | |
4445 | if (!(flags & NFT_SET_TIMEOUT)) | |
4446 | return -EINVAL; | |
4447 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); | |
4448 | } | |
4449 | ||
c50b960c PM |
4450 | policy = NFT_SET_POL_PERFORMANCE; |
4451 | if (nla[NFTA_SET_POLICY] != NULL) | |
4452 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); | |
4453 | ||
4454 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4455 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4456 | if (err < 0) |
4457 | return err; | |
1b6345d4 PNA |
4458 | |
4459 | if (desc.field_count > 1 && !(flags & NFT_SET_CONCAT)) | |
4460 | return -EINVAL; | |
4461 | } else if (flags & NFT_SET_CONCAT) { | |
4462 | return -EINVAL; | |
c50b960c PM |
4463 | } |
4464 | ||
48b0ae04 | 4465 | if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS]) |
d56aab26 PNA |
4466 | desc.expr = true; |
4467 | ||
6001a930 PNA |
4468 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask, |
4469 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4470 | if (IS_ERR(table)) { |
4471 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4472 | return PTR_ERR(table); |
36dd1bcc | 4473 | } |
20a69341 | 4474 | |
7dab8ee3 | 4475 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
20a69341 | 4476 | |
cac20fcd | 4477 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4478 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4479 | if (PTR_ERR(set) != -ENOENT) { |
4480 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4481 | return PTR_ERR(set); |
36dd1bcc | 4482 | } |
1a28ad74 | 4483 | } else { |
7dab8ee3 | 4484 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 4485 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); |
20a69341 | 4486 | return -EEXIST; |
36dd1bcc | 4487 | } |
7dab8ee3 | 4488 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
20a69341 | 4489 | return -EOPNOTSUPP; |
36dd1bcc | 4490 | |
20a69341 PM |
4491 | return 0; |
4492 | } | |
4493 | ||
7dab8ee3 | 4494 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
20a69341 PM |
4495 | return -ENOENT; |
4496 | ||
2b664957 | 4497 | ops = nft_select_set_ops(&ctx, nla, &desc, policy); |
20a69341 PM |
4498 | if (IS_ERR(ops)) |
4499 | return PTR_ERR(ops); | |
4500 | ||
e6d8ecac CFG |
4501 | udlen = 0; |
4502 | if (nla[NFTA_SET_USERDATA]) | |
4503 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
4504 | ||
20a69341 PM |
4505 | size = 0; |
4506 | if (ops->privsize != NULL) | |
347b408d | 4507 | size = ops->privsize(nla, &desc); |
6c8774a9 | 4508 | alloc_size = sizeof(*set) + size + udlen; |
45928afe | 4509 | if (alloc_size < size || alloc_size > INT_MAX) |
6c8774a9 | 4510 | return -ENOMEM; |
33758c89 | 4511 | set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT); |
24d19826 FW |
4512 | if (!set) |
4513 | return -ENOMEM; | |
20a69341 | 4514 | |
33758c89 | 4515 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT); |
38745490 PS |
4516 | if (!name) { |
4517 | err = -ENOMEM; | |
65038428 | 4518 | goto err_set_name; |
38745490 PS |
4519 | } |
4520 | ||
20a69341 | 4521 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 4522 | kfree(name); |
20a69341 | 4523 | if (err < 0) |
ad9f151e PNA |
4524 | goto err_set_name; |
4525 | ||
4526 | udata = NULL; | |
4527 | if (udlen) { | |
4528 | udata = set->data + size; | |
4529 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
4530 | } | |
4531 | ||
4532 | INIT_LIST_HEAD(&set->bindings); | |
4533 | INIT_LIST_HEAD(&set->catchall_list); | |
4534 | set->table = table; | |
4535 | write_pnet(&set->net, net); | |
4536 | set->ops = ops; | |
4537 | set->ktype = ktype; | |
4538 | set->klen = desc.klen; | |
4539 | set->dtype = dtype; | |
4540 | set->objtype = objtype; | |
4541 | set->dlen = desc.dlen; | |
4542 | set->flags = flags; | |
4543 | set->size = desc.size; | |
4544 | set->policy = policy; | |
4545 | set->udlen = udlen; | |
4546 | set->udata = udata; | |
4547 | set->timeout = timeout; | |
4548 | set->gc_int = gc_int; | |
4549 | ||
4550 | set->field_count = desc.field_count; | |
4551 | for (i = 0; i < desc.field_count; i++) | |
4552 | set->field_len[i] = desc.field_len[i]; | |
4553 | ||
4554 | err = ops->init(set, &desc, nla); | |
4555 | if (err < 0) | |
4556 | goto err_set_init; | |
65038428 PNA |
4557 | |
4558 | if (nla[NFTA_SET_EXPR]) { | |
4559 | expr = nft_set_elem_expr_alloc(&ctx, set, nla[NFTA_SET_EXPR]); | |
4560 | if (IS_ERR(expr)) { | |
4561 | err = PTR_ERR(expr); | |
ad9f151e | 4562 | goto err_set_expr_alloc; |
65038428 | 4563 | } |
8cfd9b0f PNA |
4564 | set->exprs[0] = expr; |
4565 | set->num_exprs++; | |
48b0ae04 PNA |
4566 | } else if (nla[NFTA_SET_EXPRESSIONS]) { |
4567 | struct nft_expr *expr; | |
4568 | struct nlattr *tmp; | |
4569 | int left; | |
4570 | ||
b4e70d8d PNA |
4571 | if (!(flags & NFT_SET_EXPR)) { |
4572 | err = -EINVAL; | |
ad9f151e | 4573 | goto err_set_expr_alloc; |
b4e70d8d | 4574 | } |
48b0ae04 PNA |
4575 | i = 0; |
4576 | nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) { | |
4577 | if (i == NFT_SET_EXPR_MAX) { | |
4578 | err = -E2BIG; | |
ad9f151e | 4579 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4580 | } |
4581 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
4582 | err = -EINVAL; | |
ad9f151e | 4583 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4584 | } |
4585 | expr = nft_set_elem_expr_alloc(&ctx, set, tmp); | |
4586 | if (IS_ERR(expr)) { | |
4587 | err = PTR_ERR(expr); | |
ad9f151e | 4588 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4589 | } |
4590 | set->exprs[i++] = expr; | |
4591 | set->num_exprs++; | |
4592 | } | |
65038428 | 4593 | } |
20a69341 | 4594 | |
3ecbfd65 | 4595 | set->handle = nf_tables_alloc_handle(table); |
20a69341 | 4596 | |
958bee14 | 4597 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 4598 | if (err < 0) |
ad9f151e | 4599 | goto err_set_expr_alloc; |
20a69341 | 4600 | |
e688a7f8 | 4601 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 4602 | table->use++; |
20a69341 PM |
4603 | return 0; |
4604 | ||
ad9f151e | 4605 | err_set_expr_alloc: |
8cfd9b0f PNA |
4606 | for (i = 0; i < set->num_exprs; i++) |
4607 | nft_expr_destroy(&ctx, set->exprs[i]); | |
ad9f151e PNA |
4608 | |
4609 | ops->destroy(set); | |
4610 | err_set_init: | |
2f6adf48 | 4611 | kfree(set->name); |
65038428 | 4612 | err_set_name: |
1ff75a3e | 4613 | kvfree(set); |
20a69341 PM |
4614 | return err; |
4615 | } | |
4616 | ||
aaa31047 PNA |
4617 | struct nft_set_elem_catchall { |
4618 | struct list_head list; | |
4619 | struct rcu_head rcu; | |
4620 | void *elem; | |
4621 | }; | |
4622 | ||
4623 | static void nft_set_catchall_destroy(const struct nft_ctx *ctx, | |
4624 | struct nft_set *set) | |
4625 | { | |
0f7d9b31 | 4626 | struct nft_set_elem_catchall *next, *catchall; |
aaa31047 | 4627 | |
0f7d9b31 | 4628 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
aaa31047 PNA |
4629 | list_del_rcu(&catchall->list); |
4630 | nft_set_elem_destroy(set, catchall->elem, true); | |
ae089831 | 4631 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
4632 | } |
4633 | } | |
4634 | ||
0c2a85ed | 4635 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
20a69341 | 4636 | { |
8cfd9b0f PNA |
4637 | int i; |
4638 | ||
273fe3f1 PNA |
4639 | if (WARN_ON(set->use > 0)) |
4640 | return; | |
4641 | ||
8cfd9b0f PNA |
4642 | for (i = 0; i < set->num_exprs; i++) |
4643 | nft_expr_destroy(ctx, set->exprs[i]); | |
65038428 | 4644 | |
20a69341 | 4645 | set->ops->destroy(set); |
aaa31047 | 4646 | nft_set_catchall_destroy(ctx, set); |
38745490 | 4647 | kfree(set->name); |
1ff75a3e | 4648 | kvfree(set); |
20a69341 PM |
4649 | } |
4650 | ||
7dab8ee3 PNA |
4651 | static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info, |
4652 | const struct nlattr * const nla[]) | |
20a69341 | 4653 | { |
7dab8ee3 PNA |
4654 | struct netlink_ext_ack *extack = info->extack; |
4655 | u8 genmask = nft_genmask_next(info->net); | |
67086651 | 4656 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 4657 | struct net *net = info->net; |
36dd1bcc | 4658 | const struct nlattr *attr; |
67086651 | 4659 | struct nft_table *table; |
20a69341 PM |
4660 | struct nft_set *set; |
4661 | struct nft_ctx ctx; | |
20a69341 | 4662 | |
ef4b65e5 | 4663 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
ec2c9935 | 4664 | return -EAFNOSUPPORT; |
20a69341 | 4665 | |
67086651 PNA |
4666 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
4667 | genmask, NETLINK_CB(skb).portid); | |
4668 | if (IS_ERR(table)) { | |
4669 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4670 | return PTR_ERR(table); | |
4671 | } | |
20a69341 | 4672 | |
36dd1bcc PNA |
4673 | if (nla[NFTA_SET_HANDLE]) { |
4674 | attr = nla[NFTA_SET_HANDLE]; | |
67086651 | 4675 | set = nft_set_lookup_byhandle(table, attr, genmask); |
36dd1bcc PNA |
4676 | } else { |
4677 | attr = nla[NFTA_SET_NAME]; | |
67086651 | 4678 | set = nft_set_lookup(table, attr, genmask); |
36dd1bcc | 4679 | } |
a8278400 | 4680 | |
36dd1bcc PNA |
4681 | if (IS_ERR(set)) { |
4682 | NL_SET_BAD_ATTR(extack, attr); | |
4683 | return PTR_ERR(set); | |
4684 | } | |
273fe3f1 | 4685 | if (set->use || |
7dab8ee3 PNA |
4686 | (info->nlh->nlmsg_flags & NLM_F_NONREC && |
4687 | atomic_read(&set->nelems) > 0)) { | |
36dd1bcc | 4688 | NL_SET_BAD_ATTR(extack, attr); |
20a69341 | 4689 | return -EBUSY; |
36dd1bcc | 4690 | } |
20a69341 | 4691 | |
67086651 PNA |
4692 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
4693 | ||
ee01d542 | 4694 | return nft_delset(&ctx, set); |
20a69341 PM |
4695 | } |
4696 | ||
345023b0 PNA |
4697 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
4698 | enum nft_registers reg, | |
4699 | const struct nft_data *data, | |
4700 | enum nft_data_types type, | |
4701 | unsigned int len); | |
4702 | ||
97c976d6 PNA |
4703 | static int nft_setelem_data_validate(const struct nft_ctx *ctx, |
4704 | struct nft_set *set, | |
4705 | struct nft_set_elem *elem) | |
20a69341 | 4706 | { |
fe2811eb | 4707 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4708 | enum nft_registers dreg; |
4709 | ||
4710 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
4711 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
4712 | set->dtype == NFT_DATA_VERDICT ? | |
4713 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
4714 | set->dlen); | |
20a69341 PM |
4715 | } |
4716 | ||
97c976d6 PNA |
4717 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, |
4718 | struct nft_set *set, | |
4719 | const struct nft_set_iter *iter, | |
4720 | struct nft_set_elem *elem) | |
4721 | { | |
4722 | return nft_setelem_data_validate(ctx, set, elem); | |
4723 | } | |
4724 | ||
aaa31047 PNA |
4725 | static int nft_set_catchall_bind_check(const struct nft_ctx *ctx, |
4726 | struct nft_set *set) | |
4727 | { | |
4728 | u8 genmask = nft_genmask_next(ctx->net); | |
4729 | struct nft_set_elem_catchall *catchall; | |
4730 | struct nft_set_elem elem; | |
4731 | struct nft_set_ext *ext; | |
4732 | int ret = 0; | |
4733 | ||
4734 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
4735 | ext = nft_set_elem_ext(set, catchall->elem); | |
4736 | if (!nft_set_elem_active(ext, genmask)) | |
4737 | continue; | |
4738 | ||
4739 | elem.priv = catchall->elem; | |
4740 | ret = nft_setelem_data_validate(ctx, set, &elem); | |
4741 | if (ret < 0) | |
4742 | break; | |
4743 | } | |
4744 | ||
4745 | return ret; | |
4746 | } | |
4747 | ||
20a69341 PM |
4748 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4749 | struct nft_set_binding *binding) | |
4750 | { | |
4751 | struct nft_set_binding *i; | |
4752 | struct nft_set_iter iter; | |
4753 | ||
273fe3f1 PNA |
4754 | if (set->use == UINT_MAX) |
4755 | return -EOVERFLOW; | |
4756 | ||
408070d6 | 4757 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
4758 | return -EBUSY; |
4759 | ||
11113e19 | 4760 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
4761 | /* If the set is already bound to the same chain all |
4762 | * jumps are already validated for that chain. | |
4763 | */ | |
4764 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 4765 | if (i->flags & NFT_SET_MAP && |
11113e19 | 4766 | i->chain == binding->chain) |
20a69341 PM |
4767 | goto bind; |
4768 | } | |
4769 | ||
8588ac09 | 4770 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
4771 | iter.skip = 0; |
4772 | iter.count = 0; | |
4773 | iter.err = 0; | |
4774 | iter.fn = nf_tables_bind_check_setelem; | |
4775 | ||
4776 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
4777 | if (!iter.err) |
4778 | iter.err = nft_set_catchall_bind_check(ctx, set); | |
4779 | ||
a02f4248 | 4780 | if (iter.err < 0) |
20a69341 | 4781 | return iter.err; |
20a69341 PM |
4782 | } |
4783 | bind: | |
4784 | binding->chain = ctx->chain; | |
e688a7f8 | 4785 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 | 4786 | nft_set_trans_bind(ctx, set); |
273fe3f1 | 4787 | set->use++; |
f6ac8585 | 4788 | |
20a69341 PM |
4789 | return 0; |
4790 | } | |
63aea290 | 4791 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 4792 | |
3b0a081d FW |
4793 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4794 | struct nft_set_binding *binding, bool event) | |
20a69341 | 4795 | { |
e688a7f8 | 4796 | list_del_rcu(&binding->list); |
20a69341 | 4797 | |
f6ac8585 | 4798 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 4799 | list_del_rcu(&set->list); |
f6ac8585 PNA |
4800 | if (event) |
4801 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
4802 | GFP_KERNEL); | |
4803 | } | |
20a69341 PM |
4804 | } |
4805 | ||
273fe3f1 PNA |
4806 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
4807 | struct nft_set_binding *binding, | |
4808 | enum nft_trans_phase phase) | |
4809 | { | |
4810 | switch (phase) { | |
4811 | case NFT_TRANS_PREPARE: | |
4812 | set->use--; | |
4813 | return; | |
4814 | case NFT_TRANS_ABORT: | |
4815 | case NFT_TRANS_RELEASE: | |
4816 | set->use--; | |
954d8297 | 4817 | fallthrough; |
273fe3f1 PNA |
4818 | default: |
4819 | nf_tables_unbind_set(ctx, set, binding, | |
4820 | phase == NFT_TRANS_COMMIT); | |
4821 | } | |
4822 | } | |
4823 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
4824 | ||
cd5125d8 FW |
4825 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
4826 | { | |
f6ac8585 | 4827 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
0c2a85ed | 4828 | nft_set_destroy(ctx, set); |
cd5125d8 FW |
4829 | } |
4830 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
4831 | ||
3ac4c07a PM |
4832 | const struct nft_set_ext_type nft_set_ext_types[] = { |
4833 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 4834 | .align = __alignof__(u32), |
3ac4c07a PM |
4835 | }, |
4836 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 4837 | .align = __alignof__(u32), |
3ac4c07a | 4838 | }, |
563125a7 PNA |
4839 | [NFT_SET_EXT_EXPRESSIONS] = { |
4840 | .align = __alignof__(struct nft_set_elem_expr), | |
f25ad2e9 | 4841 | }, |
8aeff920 PNA |
4842 | [NFT_SET_EXT_OBJREF] = { |
4843 | .len = sizeof(struct nft_object *), | |
4844 | .align = __alignof__(struct nft_object *), | |
4845 | }, | |
3ac4c07a PM |
4846 | [NFT_SET_EXT_FLAGS] = { |
4847 | .len = sizeof(u8), | |
4848 | .align = __alignof__(u8), | |
4849 | }, | |
c3e1b005 PM |
4850 | [NFT_SET_EXT_TIMEOUT] = { |
4851 | .len = sizeof(u64), | |
4852 | .align = __alignof__(u64), | |
4853 | }, | |
4854 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
4855 | .len = sizeof(u64), |
4856 | .align = __alignof__(u64), | |
c3e1b005 | 4857 | }, |
68e942e8 PM |
4858 | [NFT_SET_EXT_USERDATA] = { |
4859 | .len = sizeof(struct nft_userdata), | |
4860 | .align = __alignof__(struct nft_userdata), | |
4861 | }, | |
7b225d0b PNA |
4862 | [NFT_SET_EXT_KEY_END] = { |
4863 | .align = __alignof__(u32), | |
4864 | }, | |
3ac4c07a | 4865 | }; |
3ac4c07a | 4866 | |
20a69341 PM |
4867 | /* |
4868 | * Set elements | |
4869 | */ | |
4870 | ||
4871 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
4872 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
4873 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
4874 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 4875 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 4876 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
4877 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
4878 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 4879 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
4880 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
4881 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 4882 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
48b0ae04 | 4883 | [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED }, |
20a69341 PM |
4884 | }; |
4885 | ||
4886 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
4887 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
4888 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
4889 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
4890 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 4891 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 4892 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
4893 | }; |
4894 | ||
563125a7 PNA |
4895 | static int nft_set_elem_expr_dump(struct sk_buff *skb, |
4896 | const struct nft_set *set, | |
4897 | const struct nft_set_ext *ext) | |
4898 | { | |
4899 | struct nft_set_elem_expr *elem_expr; | |
4900 | u32 size, num_exprs = 0; | |
4901 | struct nft_expr *expr; | |
48b0ae04 | 4902 | struct nlattr *nest; |
563125a7 PNA |
4903 | |
4904 | elem_expr = nft_set_ext_expr(ext); | |
4905 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
4906 | num_exprs++; | |
4907 | ||
4908 | if (num_exprs == 1) { | |
4909 | expr = nft_setelem_expr_at(elem_expr, 0); | |
4910 | if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr) < 0) | |
4911 | return -1; | |
4912 | ||
4913 | return 0; | |
48b0ae04 PNA |
4914 | } else if (num_exprs > 1) { |
4915 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS); | |
4916 | if (nest == NULL) | |
4917 | goto nla_put_failure; | |
563125a7 | 4918 | |
48b0ae04 PNA |
4919 | nft_setelem_expr_foreach(expr, elem_expr, size) { |
4920 | expr = nft_setelem_expr_at(elem_expr, size); | |
4921 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) | |
4922 | goto nla_put_failure; | |
4923 | } | |
4924 | nla_nest_end(skb, nest); | |
4925 | } | |
563125a7 | 4926 | return 0; |
48b0ae04 PNA |
4927 | |
4928 | nla_put_failure: | |
4929 | return -1; | |
563125a7 PNA |
4930 | } |
4931 | ||
20a69341 PM |
4932 | static int nf_tables_fill_setelem(struct sk_buff *skb, |
4933 | const struct nft_set *set, | |
4934 | const struct nft_set_elem *elem) | |
4935 | { | |
fe2811eb | 4936 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4937 | unsigned char *b = skb_tail_pointer(skb); |
4938 | struct nlattr *nest; | |
4939 | ||
ae0be8de | 4940 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
4941 | if (nest == NULL) |
4942 | goto nla_put_failure; | |
4943 | ||
aaa31047 PNA |
4944 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
4945 | nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), | |
fe2811eb | 4946 | NFT_DATA_VALUE, set->klen) < 0) |
20a69341 PM |
4947 | goto nla_put_failure; |
4948 | ||
7b225d0b PNA |
4949 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
4950 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
4951 | NFT_DATA_VALUE, set->klen) < 0) | |
4952 | goto nla_put_failure; | |
4953 | ||
fe2811eb PM |
4954 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
4955 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
4956 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
4957 | set->dlen) < 0) | |
4958 | goto nla_put_failure; | |
4959 | ||
563125a7 PNA |
4960 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) && |
4961 | nft_set_elem_expr_dump(skb, set, ext)) | |
f25ad2e9 PM |
4962 | goto nla_put_failure; |
4963 | ||
8aeff920 PNA |
4964 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
4965 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 4966 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
4967 | goto nla_put_failure; |
4968 | ||
fe2811eb PM |
4969 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
4970 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
4971 | htonl(*nft_set_ext_flags(ext)))) | |
4972 | goto nla_put_failure; | |
20a69341 | 4973 | |
c3e1b005 PM |
4974 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
4975 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 4976 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 4977 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4978 | goto nla_put_failure; |
4979 | ||
4980 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 4981 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
4982 | |
4983 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 4984 | if (time_before64(now, expires)) |
c3e1b005 PM |
4985 | expires -= now; |
4986 | else | |
4987 | expires = 0; | |
4988 | ||
4989 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 4990 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 4991 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4992 | goto nla_put_failure; |
4993 | } | |
4994 | ||
68e942e8 PM |
4995 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
4996 | struct nft_userdata *udata; | |
4997 | ||
4998 | udata = nft_set_ext_userdata(ext); | |
4999 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
5000 | udata->len + 1, udata->data)) | |
5001 | goto nla_put_failure; | |
5002 | } | |
5003 | ||
20a69341 PM |
5004 | nla_nest_end(skb, nest); |
5005 | return 0; | |
5006 | ||
5007 | nla_put_failure: | |
5008 | nlmsg_trim(skb, b); | |
5009 | return -EMSGSIZE; | |
5010 | } | |
5011 | ||
5012 | struct nft_set_dump_args { | |
5013 | const struct netlink_callback *cb; | |
5014 | struct nft_set_iter iter; | |
5015 | struct sk_buff *skb; | |
5016 | }; | |
5017 | ||
5018 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 5019 | struct nft_set *set, |
20a69341 | 5020 | const struct nft_set_iter *iter, |
de70185d | 5021 | struct nft_set_elem *elem) |
20a69341 PM |
5022 | { |
5023 | struct nft_set_dump_args *args; | |
5024 | ||
5025 | args = container_of(iter, struct nft_set_dump_args, iter); | |
5026 | return nf_tables_fill_setelem(args->skb, set, elem); | |
5027 | } | |
5028 | ||
fa803605 LZ |
5029 | struct nft_set_dump_ctx { |
5030 | const struct nft_set *set; | |
5031 | struct nft_ctx ctx; | |
5032 | }; | |
5033 | ||
aaa31047 PNA |
5034 | static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb, |
5035 | const struct nft_set *set) | |
5036 | { | |
5037 | struct nft_set_elem_catchall *catchall; | |
5038 | u8 genmask = nft_genmask_cur(net); | |
5039 | struct nft_set_elem elem; | |
5040 | struct nft_set_ext *ext; | |
5041 | int ret = 0; | |
5042 | ||
5043 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5044 | ext = nft_set_elem_ext(set, catchall->elem); | |
5045 | if (!nft_set_elem_active(ext, genmask) || | |
5046 | nft_set_elem_expired(ext)) | |
5047 | continue; | |
5048 | ||
5049 | elem.priv = catchall->elem; | |
5050 | ret = nf_tables_fill_setelem(skb, set, &elem); | |
5051 | break; | |
5052 | } | |
5053 | ||
5054 | return ret; | |
5055 | } | |
5056 | ||
20a69341 PM |
5057 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
5058 | { | |
fa803605 | 5059 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 5060 | struct net *net = sock_net(skb->sk); |
0854db2a | 5061 | struct nftables_pernet *nft_net; |
fa803605 | 5062 | struct nft_table *table; |
de70185d | 5063 | struct nft_set *set; |
20a69341 | 5064 | struct nft_set_dump_args args; |
fa803605 | 5065 | bool set_found = false; |
20a69341 PM |
5066 | struct nlmsghdr *nlh; |
5067 | struct nlattr *nest; | |
5068 | u32 portid, seq; | |
fa803605 | 5069 | int event; |
20a69341 | 5070 | |
fa803605 | 5071 | rcu_read_lock(); |
d59d2f82 | 5072 | nft_net = nft_pernet(net); |
34002783 PNA |
5073 | cb->seq = READ_ONCE(nft_net->base_seq); |
5074 | ||
0854db2a | 5075 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 5076 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && |
98319cb9 | 5077 | dump_ctx->ctx.family != table->family) |
fa803605 | 5078 | continue; |
20a69341 | 5079 | |
36596dad PNA |
5080 | if (table != dump_ctx->ctx.table) |
5081 | continue; | |
20a69341 | 5082 | |
36596dad PNA |
5083 | list_for_each_entry_rcu(set, &table->sets, list) { |
5084 | if (set == dump_ctx->set) { | |
5085 | set_found = true; | |
5086 | break; | |
fa803605 | 5087 | } |
fa803605 LZ |
5088 | } |
5089 | break; | |
5090 | } | |
5091 | ||
5092 | if (!set_found) { | |
5093 | rcu_read_unlock(); | |
5094 | return -ENOENT; | |
5095 | } | |
20a69341 | 5096 | |
dedb67c4 | 5097 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
5098 | portid = NETLINK_CB(cb->skb).portid; |
5099 | seq = cb->nlh->nlmsg_seq; | |
5100 | ||
19c28b13 PNA |
5101 | nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI, |
5102 | table->family, NFNETLINK_V0, nft_base_seq(net)); | |
5103 | if (!nlh) | |
20a69341 PM |
5104 | goto nla_put_failure; |
5105 | ||
fa803605 | 5106 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
5107 | goto nla_put_failure; |
5108 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
5109 | goto nla_put_failure; | |
5110 | ||
ae0be8de | 5111 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
5112 | if (nest == NULL) |
5113 | goto nla_put_failure; | |
5114 | ||
8588ac09 PNA |
5115 | args.cb = cb; |
5116 | args.skb = skb; | |
fa803605 | 5117 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
5118 | args.iter.skip = cb->args[0]; |
5119 | args.iter.count = 0; | |
5120 | args.iter.err = 0; | |
5121 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 | 5122 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
aaa31047 PNA |
5123 | |
5124 | if (!args.iter.err && args.iter.count == cb->args[0]) | |
5125 | args.iter.err = nft_set_catchall_dump(net, skb, set); | |
fa803605 | 5126 | rcu_read_unlock(); |
20a69341 PM |
5127 | |
5128 | nla_nest_end(skb, nest); | |
5129 | nlmsg_end(skb, nlh); | |
5130 | ||
5131 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
5132 | return args.iter.err; | |
5133 | if (args.iter.count == cb->args[0]) | |
5134 | return 0; | |
5135 | ||
5136 | cb->args[0] = args.iter.count; | |
5137 | return skb->len; | |
5138 | ||
5139 | nla_put_failure: | |
fa803605 | 5140 | rcu_read_unlock(); |
20a69341 PM |
5141 | return -ENOSPC; |
5142 | } | |
5143 | ||
90fd131a FW |
5144 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
5145 | { | |
5146 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
5147 | ||
5148 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
5149 | ||
5150 | return cb->data ? 0 : -ENOMEM; | |
5151 | } | |
5152 | ||
fa803605 LZ |
5153 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
5154 | { | |
5155 | kfree(cb->data); | |
5156 | return 0; | |
5157 | } | |
5158 | ||
d60ce62f AB |
5159 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
5160 | const struct nft_ctx *ctx, u32 seq, | |
5161 | u32 portid, int event, u16 flags, | |
5162 | const struct nft_set *set, | |
5163 | const struct nft_set_elem *elem) | |
5164 | { | |
d60ce62f AB |
5165 | struct nlmsghdr *nlh; |
5166 | struct nlattr *nest; | |
5167 | int err; | |
5168 | ||
dedb67c4 | 5169 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
5170 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
5171 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
5172 | if (!nlh) | |
d60ce62f AB |
5173 | goto nla_put_failure; |
5174 | ||
d60ce62f AB |
5175 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
5176 | goto nla_put_failure; | |
5177 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
5178 | goto nla_put_failure; | |
5179 | ||
ae0be8de | 5180 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
5181 | if (nest == NULL) |
5182 | goto nla_put_failure; | |
5183 | ||
5184 | err = nf_tables_fill_setelem(skb, set, elem); | |
5185 | if (err < 0) | |
5186 | goto nla_put_failure; | |
5187 | ||
5188 | nla_nest_end(skb, nest); | |
5189 | ||
053c095a JB |
5190 | nlmsg_end(skb, nlh); |
5191 | return 0; | |
d60ce62f AB |
5192 | |
5193 | nla_put_failure: | |
5194 | nlmsg_trim(skb, nlh); | |
5195 | return -1; | |
5196 | } | |
5197 | ||
ba0e4d99 PNA |
5198 | static int nft_setelem_parse_flags(const struct nft_set *set, |
5199 | const struct nlattr *attr, u32 *flags) | |
5200 | { | |
5201 | if (attr == NULL) | |
5202 | return 0; | |
5203 | ||
5204 | *flags = ntohl(nla_get_be32(attr)); | |
aaa31047 PNA |
5205 | if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) |
5206 | return -EOPNOTSUPP; | |
ba0e4d99 PNA |
5207 | if (!(set->flags & NFT_SET_INTERVAL) && |
5208 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
5209 | return -EINVAL; | |
fc0ae524 PNA |
5210 | if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) == |
5211 | (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) | |
5212 | return -EINVAL; | |
ba0e4d99 PNA |
5213 | |
5214 | return 0; | |
5215 | } | |
5216 | ||
20a1452c PNA |
5217 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
5218 | struct nft_data *key, struct nlattr *attr) | |
5219 | { | |
341b6941 PNA |
5220 | struct nft_data_desc desc = { |
5221 | .type = NFT_DATA_VALUE, | |
5222 | .size = NFT_DATA_VALUE_MAXLEN, | |
5223 | .len = set->klen, | |
5224 | }; | |
20a1452c | 5225 | |
341b6941 | 5226 | return nft_data_init(ctx, key, &desc, attr); |
20a1452c PNA |
5227 | } |
5228 | ||
fdb9c405 PNA |
5229 | static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, |
5230 | struct nft_data_desc *desc, | |
5231 | struct nft_data *data, | |
5232 | struct nlattr *attr) | |
5233 | { | |
7e6bc1f6 | 5234 | u32 dtype; |
fdb9c405 | 5235 | |
7e6bc1f6 PNA |
5236 | if (set->dtype == NFT_DATA_VERDICT) |
5237 | dtype = NFT_DATA_VERDICT; | |
5238 | else | |
5239 | dtype = NFT_DATA_VALUE; | |
5240 | ||
341b6941 PNA |
5241 | desc->type = dtype; |
5242 | desc->size = NFT_DATA_VALUE_MAXLEN; | |
5243 | desc->len = set->dlen; | |
f323ef3a | 5244 | desc->flags = NFT_DATA_DESC_SETELEM; |
fdb9c405 | 5245 | |
341b6941 | 5246 | return nft_data_init(ctx, data, desc, attr); |
fdb9c405 PNA |
5247 | } |
5248 | ||
aaa31047 PNA |
5249 | static void *nft_setelem_catchall_get(const struct net *net, |
5250 | const struct nft_set *set) | |
5251 | { | |
5252 | struct nft_set_elem_catchall *catchall; | |
5253 | u8 genmask = nft_genmask_cur(net); | |
5254 | struct nft_set_ext *ext; | |
5255 | void *priv = NULL; | |
5256 | ||
5257 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5258 | ext = nft_set_elem_ext(set, catchall->elem); | |
5259 | if (!nft_set_elem_active(ext, genmask) || | |
5260 | nft_set_elem_expired(ext)) | |
5261 | continue; | |
5262 | ||
5263 | priv = catchall->elem; | |
5264 | break; | |
5265 | } | |
5266 | ||
5267 | return priv; | |
5268 | } | |
5269 | ||
5270 | static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set, | |
5271 | struct nft_set_elem *elem, u32 flags) | |
5272 | { | |
5273 | void *priv; | |
5274 | ||
5275 | if (!(flags & NFT_SET_ELEM_CATCHALL)) { | |
5276 | priv = set->ops->get(ctx->net, set, elem, flags); | |
5277 | if (IS_ERR(priv)) | |
5278 | return PTR_ERR(priv); | |
5279 | } else { | |
5280 | priv = nft_setelem_catchall_get(ctx->net, set); | |
5281 | if (!priv) | |
5282 | return -ENOENT; | |
5283 | } | |
5284 | elem->priv = priv; | |
5285 | ||
5286 | return 0; | |
5287 | } | |
5288 | ||
ba0e4d99 PNA |
5289 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
5290 | const struct nlattr *attr) | |
5291 | { | |
5292 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
5293 | struct nft_set_elem elem; |
5294 | struct sk_buff *skb; | |
5295 | uint32_t flags = 0; | |
ba0e4d99 PNA |
5296 | int err; |
5297 | ||
8cb08174 JB |
5298 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5299 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
5300 | if (err < 0) |
5301 | return err; | |
5302 | ||
ba0e4d99 PNA |
5303 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5304 | if (err < 0) | |
5305 | return err; | |
5306 | ||
aaa31047 PNA |
5307 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
5308 | return -EINVAL; | |
5309 | ||
5310 | if (nla[NFTA_SET_ELEM_KEY]) { | |
5311 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
5312 | nla[NFTA_SET_ELEM_KEY]); | |
5313 | if (err < 0) | |
5314 | return err; | |
5315 | } | |
ba0e4d99 | 5316 | |
7b225d0b PNA |
5317 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5318 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5319 | nla[NFTA_SET_ELEM_KEY_END]); | |
5320 | if (err < 0) | |
5321 | return err; | |
5322 | } | |
5323 | ||
aaa31047 PNA |
5324 | err = nft_setelem_get(ctx, set, &elem, flags); |
5325 | if (err < 0) | |
5326 | return err; | |
ba0e4d99 PNA |
5327 | |
5328 | err = -ENOMEM; | |
d9adf22a | 5329 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 | 5330 | if (skb == NULL) |
ee921183 | 5331 | return err; |
ba0e4d99 PNA |
5332 | |
5333 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
5334 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
5335 | if (err < 0) | |
ee921183 | 5336 | goto err_fill_setelem; |
ba0e4d99 | 5337 | |
ee921183 | 5338 | return nfnetlink_unicast(skb, ctx->net, ctx->portid); |
ba0e4d99 | 5339 | |
ee921183 | 5340 | err_fill_setelem: |
ba0e4d99 | 5341 | kfree_skb(skb); |
ee921183 | 5342 | return err; |
ba0e4d99 PNA |
5343 | } |
5344 | ||
d9adf22a | 5345 | /* called with rcu_read_lock held */ |
797d4980 PNA |
5346 | static int nf_tables_getsetelem(struct sk_buff *skb, |
5347 | const struct nfnl_info *info, | |
5348 | const struct nlattr * const nla[]) | |
ba0e4d99 | 5349 | { |
797d4980 PNA |
5350 | struct netlink_ext_ack *extack = info->extack; |
5351 | u8 genmask = nft_genmask_cur(info->net); | |
e2b750d7 | 5352 | u8 family = info->nfmsg->nfgen_family; |
797d4980 | 5353 | struct net *net = info->net; |
e2b750d7 | 5354 | struct nft_table *table; |
ba0e4d99 PNA |
5355 | struct nft_set *set; |
5356 | struct nlattr *attr; | |
5357 | struct nft_ctx ctx; | |
5358 | int rem, err = 0; | |
5359 | ||
e2b750d7 PNA |
5360 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
5361 | genmask, NETLINK_CB(skb).portid); | |
5362 | if (IS_ERR(table)) { | |
5363 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
5364 | return PTR_ERR(table); | |
5365 | } | |
ba0e4d99 | 5366 | |
e2b750d7 | 5367 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
5368 | if (IS_ERR(set)) |
5369 | return PTR_ERR(set); | |
5370 | ||
e2b750d7 PNA |
5371 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5372 | ||
797d4980 | 5373 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
ba0e4d99 | 5374 | struct netlink_dump_control c = { |
90fd131a | 5375 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
5376 | .dump = nf_tables_dump_set, |
5377 | .done = nf_tables_dump_set_done, | |
d9adf22a | 5378 | .module = THIS_MODULE, |
ba0e4d99 | 5379 | }; |
90fd131a FW |
5380 | struct nft_set_dump_ctx dump_ctx = { |
5381 | .set = set, | |
5382 | .ctx = ctx, | |
5383 | }; | |
ba0e4d99 | 5384 | |
90fd131a | 5385 | c.data = &dump_ctx; |
797d4980 | 5386 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
ba0e4d99 PNA |
5387 | } |
5388 | ||
5389 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
5390 | return -EINVAL; | |
5391 | ||
5392 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
5393 | err = nft_get_set_elem(&ctx, set, attr); | |
b53c1166 PNA |
5394 | if (err < 0) { |
5395 | NL_SET_BAD_ATTR(extack, attr); | |
ba0e4d99 | 5396 | break; |
b53c1166 | 5397 | } |
ba0e4d99 PNA |
5398 | } |
5399 | ||
5400 | return err; | |
5401 | } | |
5402 | ||
25e94a99 PNA |
5403 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
5404 | const struct nft_set *set, | |
5405 | const struct nft_set_elem *elem, | |
6fb721cf | 5406 | int event) |
d60ce62f | 5407 | { |
0854db2a | 5408 | struct nftables_pernet *nft_net; |
128ad332 PNA |
5409 | struct net *net = ctx->net; |
5410 | u32 portid = ctx->portid; | |
d60ce62f | 5411 | struct sk_buff *skb; |
6fb721cf | 5412 | u16 flags = 0; |
d60ce62f AB |
5413 | int err; |
5414 | ||
128ad332 | 5415 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 5416 | return; |
d60ce62f | 5417 | |
d60ce62f AB |
5418 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
5419 | if (skb == NULL) | |
5420 | goto err; | |
5421 | ||
6fb721cf PNA |
5422 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
5423 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
5424 | ||
d60ce62f AB |
5425 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, |
5426 | set, elem); | |
5427 | if (err < 0) { | |
5428 | kfree_skb(skb); | |
5429 | goto err; | |
5430 | } | |
5431 | ||
d59d2f82 | 5432 | nft_net = nft_pernet(net); |
0854db2a | 5433 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 5434 | return; |
d60ce62f | 5435 | err: |
25e94a99 | 5436 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
5437 | } |
5438 | ||
60319eb1 PNA |
5439 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
5440 | int msg_type, | |
5441 | struct nft_set *set) | |
5442 | { | |
5443 | struct nft_trans *trans; | |
5444 | ||
5445 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
5446 | if (trans == NULL) | |
5447 | return NULL; | |
5448 | ||
5449 | nft_trans_elem_set(trans) = set; | |
5450 | return trans; | |
5451 | } | |
5452 | ||
a7fc9368 PNA |
5453 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
5454 | const struct nft_set *set, | |
5455 | const struct nlattr *attr) | |
5456 | { | |
5457 | struct nft_expr *expr; | |
5458 | int err; | |
5459 | ||
5460 | expr = nft_expr_init(ctx, attr); | |
5461 | if (IS_ERR(expr)) | |
5462 | return expr; | |
5463 | ||
5464 | err = -EOPNOTSUPP; | |
a7fc9368 PNA |
5465 | if (expr->ops->type->flags & NFT_EXPR_GC) { |
5466 | if (set->flags & NFT_SET_TIMEOUT) | |
5467 | goto err_set_elem_expr; | |
5468 | if (!set->ops->gc_init) | |
5469 | goto err_set_elem_expr; | |
5470 | set->ops->gc_init(set); | |
5471 | } | |
5472 | ||
5473 | return expr; | |
5474 | ||
5475 | err_set_elem_expr: | |
5476 | nft_expr_destroy(ctx, expr); | |
5477 | return ERR_PTR(err); | |
5478 | } | |
5479 | ||
34aae2c2 PNA |
5480 | static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len) |
5481 | { | |
5482 | len += nft_set_ext_types[id].len; | |
5483 | if (len > tmpl->ext_len[id] || | |
5484 | len > U8_MAX) | |
5485 | return -1; | |
5486 | ||
5487 | return 0; | |
5488 | } | |
5489 | ||
5490 | static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id, | |
5491 | void *to, const void *from, u32 len) | |
5492 | { | |
5493 | if (nft_set_ext_check(tmpl, id, len) < 0) | |
5494 | return -1; | |
5495 | ||
5496 | memcpy(to, from, len); | |
5497 | ||
5498 | return 0; | |
5499 | } | |
5500 | ||
22fe54d5 PM |
5501 | void *nft_set_elem_init(const struct nft_set *set, |
5502 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
5503 | const u32 *key, const u32 *key_end, |
5504 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
5505 | { |
5506 | struct nft_set_ext *ext; | |
5507 | void *elem; | |
5508 | ||
5509 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
5510 | if (elem == NULL) | |
34aae2c2 | 5511 | return ERR_PTR(-ENOMEM); |
fe2811eb PM |
5512 | |
5513 | ext = nft_set_elem_ext(set, elem); | |
5514 | nft_set_ext_init(ext, tmpl); | |
5515 | ||
34aae2c2 PNA |
5516 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
5517 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY, | |
5518 | nft_set_ext_key(ext), key, set->klen) < 0) | |
5519 | goto err_ext_check; | |
5520 | ||
5521 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && | |
5522 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END, | |
5523 | nft_set_ext_key_end(ext), key_end, set->klen) < 0) | |
5524 | goto err_ext_check; | |
5525 | ||
5526 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && | |
5527 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA, | |
5528 | nft_set_ext_data(ext), data, set->dlen) < 0) | |
5529 | goto err_ext_check; | |
5530 | ||
79ebb5bb LGL |
5531 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
5532 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
5533 | if (expiration == 0) | |
5534 | *nft_set_ext_expiration(ext) += timeout; | |
5535 | } | |
c3e1b005 PM |
5536 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
5537 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
5538 | |
5539 | return elem; | |
34aae2c2 PNA |
5540 | |
5541 | err_ext_check: | |
5542 | kfree(elem); | |
5543 | ||
5544 | return ERR_PTR(-EINVAL); | |
fe2811eb PM |
5545 | } |
5546 | ||
563125a7 PNA |
5547 | static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5548 | struct nft_expr *expr) | |
475beb9c PNA |
5549 | { |
5550 | if (expr->ops->destroy_clone) { | |
5551 | expr->ops->destroy_clone(ctx, expr); | |
5552 | module_put(expr->ops->type->owner); | |
5553 | } else { | |
5554 | nf_tables_expr_destroy(ctx, expr); | |
5555 | } | |
5556 | } | |
5557 | ||
563125a7 PNA |
5558 | static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5559 | struct nft_set_elem_expr *elem_expr) | |
5560 | { | |
5561 | struct nft_expr *expr; | |
5562 | u32 size; | |
5563 | ||
5564 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
5565 | __nft_set_elem_expr_destroy(ctx, expr); | |
5566 | } | |
5567 | ||
61f9e292 LZ |
5568 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
5569 | bool destroy_expr) | |
61edafbb PM |
5570 | { |
5571 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
5572 | struct nft_ctx ctx = { |
5573 | .net = read_pnet(&set->net), | |
5574 | .family = set->table->family, | |
5575 | }; | |
61edafbb | 5576 | |
59105446 | 5577 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 5578 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 5579 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
563125a7 | 5580 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c | 5581 | nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext)); |
371ebcbb | 5582 | |
8aeff920 PNA |
5583 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
5584 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
5585 | kfree(elem); |
5586 | } | |
5587 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
5588 | ||
f8bb7889 PNA |
5589 | /* Only called from commit path, nft_setelem_data_deactivate() already deals |
5590 | * with the refcounting from the preparation phase. | |
59105446 | 5591 | */ |
3453c927 PNA |
5592 | static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
5593 | const struct nft_set *set, void *elem) | |
59105446 PNA |
5594 | { |
5595 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
5596 | ||
563125a7 | 5597 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c PNA |
5598 | nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext)); |
5599 | ||
59105446 PNA |
5600 | kfree(elem); |
5601 | } | |
5602 | ||
fca05d4d PNA |
5603 | int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
5604 | struct nft_expr *expr_array[]) | |
8cfd9b0f PNA |
5605 | { |
5606 | struct nft_expr *expr; | |
5607 | int err, i, k; | |
5608 | ||
5609 | for (i = 0; i < set->num_exprs; i++) { | |
42193ffd | 5610 | expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT); |
8cfd9b0f PNA |
5611 | if (!expr) |
5612 | goto err_expr; | |
5613 | ||
5614 | err = nft_expr_clone(expr, set->exprs[i]); | |
5615 | if (err < 0) { | |
c485c35f | 5616 | kfree(expr); |
8cfd9b0f PNA |
5617 | goto err_expr; |
5618 | } | |
5619 | expr_array[i] = expr; | |
5620 | } | |
5621 | ||
5622 | return 0; | |
5623 | ||
5624 | err_expr: | |
161b838e CIK |
5625 | for (k = i - 1; k >= 0; k--) |
5626 | nft_expr_destroy(ctx, expr_array[k]); | |
8cfd9b0f PNA |
5627 | |
5628 | return -ENOMEM; | |
5629 | } | |
5630 | ||
4d8f9065 | 5631 | static int nft_set_elem_expr_setup(struct nft_ctx *ctx, |
34aae2c2 | 5632 | const struct nft_set_ext_tmpl *tmpl, |
4d8f9065 PNA |
5633 | const struct nft_set_ext *ext, |
5634 | struct nft_expr *expr_array[], | |
5635 | u32 num_exprs) | |
563125a7 PNA |
5636 | { |
5637 | struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext); | |
34aae2c2 | 5638 | u32 len = sizeof(struct nft_set_elem_expr); |
4d8f9065 PNA |
5639 | struct nft_expr *expr; |
5640 | int i, err; | |
5641 | ||
34aae2c2 PNA |
5642 | if (num_exprs == 0) |
5643 | return 0; | |
5644 | ||
5645 | for (i = 0; i < num_exprs; i++) | |
5646 | len += expr_array[i]->ops->size; | |
5647 | ||
5648 | if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0) | |
5649 | return -EINVAL; | |
5650 | ||
4d8f9065 PNA |
5651 | for (i = 0; i < num_exprs; i++) { |
5652 | expr = nft_setelem_expr_at(elem_expr, elem_expr->size); | |
5653 | err = nft_expr_clone(expr, expr_array[i]); | |
5654 | if (err < 0) | |
5655 | goto err_elem_expr_setup; | |
5656 | ||
5657 | elem_expr->size += expr_array[i]->ops->size; | |
5658 | nft_expr_destroy(ctx, expr_array[i]); | |
5659 | expr_array[i] = NULL; | |
5660 | } | |
5661 | ||
5662 | return 0; | |
5663 | ||
5664 | err_elem_expr_setup: | |
5665 | for (; i < num_exprs; i++) { | |
5666 | nft_expr_destroy(ctx, expr_array[i]); | |
5667 | expr_array[i] = NULL; | |
5668 | } | |
563125a7 | 5669 | |
4d8f9065 | 5670 | return -ENOMEM; |
563125a7 PNA |
5671 | } |
5672 | ||
aaa31047 PNA |
5673 | struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
5674 | const struct nft_set *set) | |
5675 | { | |
5676 | struct nft_set_elem_catchall *catchall; | |
5677 | u8 genmask = nft_genmask_cur(net); | |
5678 | struct nft_set_ext *ext; | |
5679 | ||
5680 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5681 | ext = nft_set_elem_ext(set, catchall->elem); | |
5682 | if (nft_set_elem_active(ext, genmask) && | |
5683 | !nft_set_elem_expired(ext)) | |
5684 | return ext; | |
5685 | } | |
5686 | ||
5687 | return NULL; | |
5688 | } | |
5689 | EXPORT_SYMBOL_GPL(nft_set_catchall_lookup); | |
5690 | ||
5691 | void *nft_set_catchall_gc(const struct nft_set *set) | |
5692 | { | |
5693 | struct nft_set_elem_catchall *catchall, *next; | |
5694 | struct nft_set_ext *ext; | |
5695 | void *elem = NULL; | |
5696 | ||
5697 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5698 | ext = nft_set_elem_ext(set, catchall->elem); | |
5699 | ||
5700 | if (!nft_set_elem_expired(ext) || | |
5701 | nft_set_elem_mark_busy(ext)) | |
5702 | continue; | |
5703 | ||
5704 | elem = catchall->elem; | |
5705 | list_del_rcu(&catchall->list); | |
5706 | kfree_rcu(catchall, rcu); | |
5707 | break; | |
5708 | } | |
5709 | ||
5710 | return elem; | |
5711 | } | |
5712 | EXPORT_SYMBOL_GPL(nft_set_catchall_gc); | |
5713 | ||
5714 | static int nft_setelem_catchall_insert(const struct net *net, | |
5715 | struct nft_set *set, | |
5716 | const struct nft_set_elem *elem, | |
5717 | struct nft_set_ext **pext) | |
5718 | { | |
5719 | struct nft_set_elem_catchall *catchall; | |
5720 | u8 genmask = nft_genmask_next(net); | |
5721 | struct nft_set_ext *ext; | |
5722 | ||
5723 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5724 | ext = nft_set_elem_ext(set, catchall->elem); | |
5725 | if (nft_set_elem_active(ext, genmask)) { | |
5726 | *pext = ext; | |
5727 | return -EEXIST; | |
5728 | } | |
5729 | } | |
5730 | ||
5731 | catchall = kmalloc(sizeof(*catchall), GFP_KERNEL); | |
5732 | if (!catchall) | |
5733 | return -ENOMEM; | |
5734 | ||
5735 | catchall->elem = elem->priv; | |
5736 | list_add_tail_rcu(&catchall->list, &set->catchall_list); | |
5737 | ||
5738 | return 0; | |
5739 | } | |
5740 | ||
5741 | static int nft_setelem_insert(const struct net *net, | |
5742 | struct nft_set *set, | |
5743 | const struct nft_set_elem *elem, | |
5744 | struct nft_set_ext **ext, unsigned int flags) | |
5745 | { | |
5746 | int ret; | |
5747 | ||
5748 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5749 | ret = nft_setelem_catchall_insert(net, set, elem, ext); | |
5750 | else | |
5751 | ret = set->ops->insert(net, set, elem, ext); | |
5752 | ||
5753 | return ret; | |
5754 | } | |
5755 | ||
5756 | static bool nft_setelem_is_catchall(const struct nft_set *set, | |
5757 | const struct nft_set_elem *elem) | |
5758 | { | |
5759 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5760 | ||
5761 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
5762 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL) | |
5763 | return true; | |
5764 | ||
5765 | return false; | |
5766 | } | |
5767 | ||
5768 | static void nft_setelem_activate(struct net *net, struct nft_set *set, | |
5769 | struct nft_set_elem *elem) | |
5770 | { | |
5771 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5772 | ||
5773 | if (nft_setelem_is_catchall(set, elem)) { | |
5774 | nft_set_elem_change_active(net, set, ext); | |
5775 | nft_set_elem_clear_busy(ext); | |
5776 | } else { | |
5777 | set->ops->activate(net, set, elem); | |
5778 | } | |
5779 | } | |
5780 | ||
5781 | static int nft_setelem_catchall_deactivate(const struct net *net, | |
5782 | struct nft_set *set, | |
5783 | struct nft_set_elem *elem) | |
5784 | { | |
5785 | struct nft_set_elem_catchall *catchall; | |
5786 | struct nft_set_ext *ext; | |
5787 | ||
5788 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5789 | ext = nft_set_elem_ext(set, catchall->elem); | |
5790 | if (!nft_is_active(net, ext) || | |
5791 | nft_set_elem_mark_busy(ext)) | |
5792 | continue; | |
5793 | ||
5794 | kfree(elem->priv); | |
5795 | elem->priv = catchall->elem; | |
5796 | nft_set_elem_change_active(net, set, ext); | |
5797 | return 0; | |
5798 | } | |
5799 | ||
5800 | return -ENOENT; | |
5801 | } | |
5802 | ||
5803 | static int __nft_setelem_deactivate(const struct net *net, | |
5804 | struct nft_set *set, | |
5805 | struct nft_set_elem *elem) | |
5806 | { | |
5807 | void *priv; | |
5808 | ||
5809 | priv = set->ops->deactivate(net, set, elem); | |
5810 | if (!priv) | |
5811 | return -ENOENT; | |
5812 | ||
5813 | kfree(elem->priv); | |
5814 | elem->priv = priv; | |
5815 | set->ndeact++; | |
5816 | ||
5817 | return 0; | |
5818 | } | |
5819 | ||
5820 | static int nft_setelem_deactivate(const struct net *net, | |
5821 | struct nft_set *set, | |
5822 | struct nft_set_elem *elem, u32 flags) | |
5823 | { | |
5824 | int ret; | |
5825 | ||
5826 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5827 | ret = nft_setelem_catchall_deactivate(net, set, elem); | |
5828 | else | |
5829 | ret = __nft_setelem_deactivate(net, set, elem); | |
5830 | ||
5831 | return ret; | |
5832 | } | |
5833 | ||
5834 | static void nft_setelem_catchall_remove(const struct net *net, | |
5835 | const struct nft_set *set, | |
5836 | const struct nft_set_elem *elem) | |
5837 | { | |
5838 | struct nft_set_elem_catchall *catchall, *next; | |
5839 | ||
5840 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5841 | if (catchall->elem == elem->priv) { | |
5842 | list_del_rcu(&catchall->list); | |
ae089831 | 5843 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
5844 | break; |
5845 | } | |
5846 | } | |
5847 | } | |
5848 | ||
5849 | static void nft_setelem_remove(const struct net *net, | |
5850 | const struct nft_set *set, | |
5851 | const struct nft_set_elem *elem) | |
5852 | { | |
5853 | if (nft_setelem_is_catchall(set, elem)) | |
5854 | nft_setelem_catchall_remove(net, set, elem); | |
5855 | else | |
5856 | set->ops->remove(net, set, elem); | |
5857 | } | |
5858 | ||
88cccd90 PNA |
5859 | static bool nft_setelem_valid_key_end(const struct nft_set *set, |
5860 | struct nlattr **nla, u32 flags) | |
5861 | { | |
5862 | if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) == | |
5863 | (NFT_SET_CONCAT | NFT_SET_INTERVAL)) { | |
5864 | if (flags & NFT_SET_ELEM_INTERVAL_END) | |
5865 | return false; | |
5866 | if (!nla[NFTA_SET_ELEM_KEY_END] && | |
5867 | !(flags & NFT_SET_ELEM_CATCHALL)) | |
5868 | return false; | |
5869 | } else { | |
5870 | if (nla[NFTA_SET_ELEM_KEY_END]) | |
5871 | return false; | |
5872 | } | |
5873 | ||
5874 | return true; | |
5875 | } | |
5876 | ||
60319eb1 | 5877 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 5878 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 | 5879 | { |
8cfd9b0f | 5880 | struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {}; |
20a69341 | 5881 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
8aeff920 | 5882 | u8 genmask = nft_genmask_next(ctx->net); |
664899e8 | 5883 | u32 flags = 0, size = 0, num_exprs = 0; |
fe2811eb | 5884 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 5885 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
5886 | struct nft_set_elem elem; |
5887 | struct nft_set_binding *binding; | |
8aeff920 | 5888 | struct nft_object *obj = NULL; |
68e942e8 | 5889 | struct nft_userdata *udata; |
20a1452c | 5890 | struct nft_data_desc desc; |
20a69341 | 5891 | enum nft_registers dreg; |
60319eb1 | 5892 | struct nft_trans *trans; |
c3e1b005 | 5893 | u64 timeout; |
79ebb5bb | 5894 | u64 expiration; |
8cfd9b0f | 5895 | int err, i; |
68e942e8 | 5896 | u8 ulen; |
20a69341 | 5897 | |
8cb08174 JB |
5898 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5899 | nft_set_elem_policy, NULL); | |
20a69341 PM |
5900 | if (err < 0) |
5901 | return err; | |
5902 | ||
fe2811eb PM |
5903 | nft_set_ext_prepare(&tmpl); |
5904 | ||
0e9091d6 PNA |
5905 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5906 | if (err < 0) | |
5907 | return err; | |
aaa31047 PNA |
5908 | |
5909 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
5910 | return -EINVAL; | |
5911 | ||
c39ba4de PNA |
5912 | if (flags != 0) { |
5913 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
5914 | if (err < 0) | |
5915 | return err; | |
5916 | } | |
20a69341 PM |
5917 | |
5918 | if (set->flags & NFT_SET_MAP) { | |
5919 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 5920 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
5921 | return -EINVAL; |
5922 | } else { | |
5923 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
5924 | return -EINVAL; | |
5925 | } | |
5926 | ||
5a2f3dc3 PNA |
5927 | if (set->flags & NFT_SET_OBJECT) { |
5928 | if (!nla[NFTA_SET_ELEM_OBJREF] && | |
5929 | !(flags & NFT_SET_ELEM_INTERVAL_END)) | |
5930 | return -EINVAL; | |
5931 | } else { | |
5932 | if (nla[NFTA_SET_ELEM_OBJREF]) | |
5933 | return -EINVAL; | |
5934 | } | |
5935 | ||
88cccd90 PNA |
5936 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
5937 | return -EINVAL; | |
5938 | ||
bffc124b PNA |
5939 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
5940 | (nla[NFTA_SET_ELEM_DATA] || | |
5941 | nla[NFTA_SET_ELEM_OBJREF] || | |
5942 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
5943 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
5944 | nla[NFTA_SET_ELEM_USERDATA] || | |
48b0ae04 | 5945 | nla[NFTA_SET_ELEM_EXPR] || |
4963674c | 5946 | nla[NFTA_SET_ELEM_KEY_END] || |
48b0ae04 | 5947 | nla[NFTA_SET_ELEM_EXPRESSIONS])) |
bffc124b PNA |
5948 | return -EINVAL; |
5949 | ||
c3e1b005 PM |
5950 | timeout = 0; |
5951 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
5952 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
5953 | return -EINVAL; | |
8e1102d5 FW |
5954 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
5955 | &timeout); | |
5956 | if (err) | |
5957 | return err; | |
c3e1b005 PM |
5958 | } else if (set->flags & NFT_SET_TIMEOUT) { |
5959 | timeout = set->timeout; | |
5960 | } | |
5961 | ||
79ebb5bb LGL |
5962 | expiration = 0; |
5963 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
5964 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
5965 | return -EINVAL; | |
5966 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
5967 | &expiration); | |
5968 | if (err) | |
5969 | return err; | |
5970 | } | |
5971 | ||
8cfd9b0f PNA |
5972 | if (nla[NFTA_SET_ELEM_EXPR]) { |
5973 | struct nft_expr *expr; | |
5974 | ||
664899e8 | 5975 | if (set->num_exprs && set->num_exprs != 1) |
8cfd9b0f PNA |
5976 | return -EOPNOTSUPP; |
5977 | ||
40944452 PNA |
5978 | expr = nft_set_elem_expr_alloc(ctx, set, |
5979 | nla[NFTA_SET_ELEM_EXPR]); | |
5980 | if (IS_ERR(expr)) | |
5981 | return PTR_ERR(expr); | |
65038428 | 5982 | |
8cfd9b0f | 5983 | expr_array[0] = expr; |
664899e8 | 5984 | num_exprs = 1; |
65038428 | 5985 | |
664899e8 | 5986 | if (set->num_exprs && set->exprs[0]->ops != expr->ops) { |
8cfd9b0f | 5987 | err = -EOPNOTSUPP; |
65038428 | 5988 | goto err_set_elem_expr; |
8cfd9b0f | 5989 | } |
48b0ae04 PNA |
5990 | } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) { |
5991 | struct nft_expr *expr; | |
5992 | struct nlattr *tmp; | |
5993 | int left; | |
65038428 | 5994 | |
48b0ae04 PNA |
5995 | i = 0; |
5996 | nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) { | |
664899e8 PNA |
5997 | if (i == NFT_SET_EXPR_MAX || |
5998 | (set->num_exprs && set->num_exprs == i)) { | |
48b0ae04 PNA |
5999 | err = -E2BIG; |
6000 | goto err_set_elem_expr; | |
6001 | } | |
6002 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
6003 | err = -EINVAL; | |
6004 | goto err_set_elem_expr; | |
6005 | } | |
6006 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
6007 | if (IS_ERR(expr)) { | |
6008 | err = PTR_ERR(expr); | |
6009 | goto err_set_elem_expr; | |
6010 | } | |
6011 | expr_array[i] = expr; | |
664899e8 | 6012 | num_exprs++; |
48b0ae04 | 6013 | |
664899e8 | 6014 | if (set->num_exprs && expr->ops != set->exprs[i]->ops) { |
48b0ae04 PNA |
6015 | err = -EOPNOTSUPP; |
6016 | goto err_set_elem_expr; | |
6017 | } | |
6018 | i++; | |
6019 | } | |
664899e8 | 6020 | if (set->num_exprs && set->num_exprs != i) { |
48b0ae04 | 6021 | err = -EOPNOTSUPP; |
65038428 | 6022 | goto err_set_elem_expr; |
48b0ae04 | 6023 | } |
8cfd9b0f PNA |
6024 | } else if (set->num_exprs > 0) { |
6025 | err = nft_set_elem_expr_clone(ctx, set, expr_array); | |
6026 | if (err < 0) | |
6027 | goto err_set_elem_expr_clone; | |
664899e8 PNA |
6028 | |
6029 | num_exprs = set->num_exprs; | |
40944452 PNA |
6030 | } |
6031 | ||
aaa31047 PNA |
6032 | if (nla[NFTA_SET_ELEM_KEY]) { |
6033 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6034 | nla[NFTA_SET_ELEM_KEY]); | |
6035 | if (err < 0) | |
6036 | goto err_set_elem_expr; | |
20a69341 | 6037 | |
c39ba4de PNA |
6038 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6039 | if (err < 0) | |
6040 | goto err_parse_key; | |
aaa31047 | 6041 | } |
7b225d0b PNA |
6042 | |
6043 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
6044 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6045 | nla[NFTA_SET_ELEM_KEY_END]); | |
6046 | if (err < 0) | |
6047 | goto err_parse_key; | |
6048 | ||
c39ba4de PNA |
6049 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6050 | if (err < 0) | |
6051 | goto err_parse_key_end; | |
7b225d0b PNA |
6052 | } |
6053 | ||
c3e1b005 | 6054 | if (timeout > 0) { |
c39ba4de PNA |
6055 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); |
6056 | if (err < 0) | |
6057 | goto err_parse_key_end; | |
6058 | ||
6059 | if (timeout != set->timeout) { | |
6060 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); | |
6061 | if (err < 0) | |
6062 | goto err_parse_key_end; | |
6063 | } | |
c3e1b005 | 6064 | } |
fe2811eb | 6065 | |
664899e8 PNA |
6066 | if (num_exprs) { |
6067 | for (i = 0; i < num_exprs; i++) | |
563125a7 PNA |
6068 | size += expr_array[i]->ops->size; |
6069 | ||
c39ba4de PNA |
6070 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS, |
6071 | sizeof(struct nft_set_elem_expr) + size); | |
6072 | if (err < 0) | |
6073 | goto err_parse_key_end; | |
563125a7 | 6074 | } |
40944452 | 6075 | |
8aeff920 | 6076 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
4d44175a FW |
6077 | obj = nft_obj_lookup(ctx->net, ctx->table, |
6078 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 6079 | set->objtype, genmask); |
8aeff920 PNA |
6080 | if (IS_ERR(obj)) { |
6081 | err = PTR_ERR(obj); | |
7b225d0b | 6082 | goto err_parse_key_end; |
8aeff920 | 6083 | } |
c39ba4de PNA |
6084 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); |
6085 | if (err < 0) | |
6086 | goto err_parse_key_end; | |
8aeff920 PNA |
6087 | } |
6088 | ||
20a69341 | 6089 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
fdb9c405 PNA |
6090 | err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val, |
6091 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 | 6092 | if (err < 0) |
7b225d0b | 6093 | goto err_parse_key_end; |
20a69341 | 6094 | |
20a69341 PM |
6095 | dreg = nft_type_to_reg(set->dtype); |
6096 | list_for_each_entry(binding, &set->bindings, list) { | |
6097 | struct nft_ctx bind_ctx = { | |
58c78e10 | 6098 | .net = ctx->net, |
36596dad | 6099 | .family = ctx->family, |
20a69341 | 6100 | .table = ctx->table, |
7c95f6d8 | 6101 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
6102 | }; |
6103 | ||
11113e19 PM |
6104 | if (!(binding->flags & NFT_SET_MAP)) |
6105 | continue; | |
6106 | ||
1ec10212 | 6107 | err = nft_validate_register_store(&bind_ctx, dreg, |
fdb9c405 | 6108 | &elem.data.val, |
20a1452c | 6109 | desc.type, desc.len); |
20a69341 | 6110 | if (err < 0) |
7b225d0b | 6111 | goto err_parse_data; |
a654de8f | 6112 | |
20a1452c | 6113 | if (desc.type == NFT_DATA_VERDICT && |
fdb9c405 PNA |
6114 | (elem.data.val.verdict.code == NFT_GOTO || |
6115 | elem.data.val.verdict.code == NFT_JUMP)) | |
a654de8f PNA |
6116 | nft_validate_state_update(ctx->net, |
6117 | NFT_VALIDATE_NEED); | |
20a69341 | 6118 | } |
fe2811eb | 6119 | |
c39ba4de PNA |
6120 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
6121 | if (err < 0) | |
6122 | goto err_parse_data; | |
20a69341 PM |
6123 | } |
6124 | ||
68e942e8 PM |
6125 | /* The full maximum length of userdata can exceed the maximum |
6126 | * offset value (U8_MAX) for following extensions, therefor it | |
6127 | * must be the last extension added. | |
6128 | */ | |
6129 | ulen = 0; | |
6130 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
6131 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
c39ba4de PNA |
6132 | if (ulen > 0) { |
6133 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
6134 | ulen); | |
6135 | if (err < 0) | |
6136 | goto err_parse_data; | |
6137 | } | |
68e942e8 PM |
6138 | } |
6139 | ||
7b225d0b | 6140 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
fdb9c405 | 6141 | elem.key_end.val.data, elem.data.val.data, |
33758c89 | 6142 | timeout, expiration, GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6143 | if (IS_ERR(elem.priv)) { |
6144 | err = PTR_ERR(elem.priv); | |
7b225d0b | 6145 | goto err_parse_data; |
34aae2c2 | 6146 | } |
fe2811eb PM |
6147 | |
6148 | ext = nft_set_elem_ext(set, elem.priv); | |
6149 | if (flags) | |
6150 | *nft_set_ext_flags(ext) = flags; | |
34aae2c2 | 6151 | |
68e942e8 | 6152 | if (ulen > 0) { |
34aae2c2 PNA |
6153 | if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) { |
6154 | err = -EINVAL; | |
6155 | goto err_elem_userdata; | |
6156 | } | |
68e942e8 PM |
6157 | udata = nft_set_ext_userdata(ext); |
6158 | udata->len = ulen - 1; | |
6159 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
6160 | } | |
8aeff920 PNA |
6161 | if (obj) { |
6162 | *nft_set_ext_obj(ext) = obj; | |
6163 | obj->use++; | |
6164 | } | |
34aae2c2 | 6165 | err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs); |
4d8f9065 | 6166 | if (err < 0) |
34aae2c2 | 6167 | goto err_elem_free; |
fe2811eb | 6168 | |
60319eb1 | 6169 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
4d8f9065 PNA |
6170 | if (trans == NULL) { |
6171 | err = -ENOMEM; | |
34aae2c2 | 6172 | goto err_elem_free; |
4d8f9065 | 6173 | } |
60319eb1 | 6174 | |
69086658 | 6175 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
aaa31047 PNA |
6176 | |
6177 | err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags); | |
c016c7e4 PNA |
6178 | if (err) { |
6179 | if (err == -EEXIST) { | |
9744a6fc PNA |
6180 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
6181 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
6182 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
77a92189 | 6183 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) |
7b225d0b | 6184 | goto err_element_clash; |
8aeff920 PNA |
6185 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
6186 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
6187 | memcmp(nft_set_ext_data(ext), | |
6188 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
6189 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
6190 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
6191 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
77a92189 | 6192 | goto err_element_clash; |
c016c7e4 PNA |
6193 | else if (!(nlmsg_flags & NLM_F_EXCL)) |
6194 | err = 0; | |
8c2d45b2 PNA |
6195 | } else if (err == -ENOTEMPTY) { |
6196 | /* ENOTEMPTY reports overlapping between this element | |
6197 | * and an existing one. | |
6198 | */ | |
6199 | err = -EEXIST; | |
c016c7e4 | 6200 | } |
7b225d0b | 6201 | goto err_element_clash; |
c016c7e4 | 6202 | } |
20a69341 | 6203 | |
aaa31047 | 6204 | if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size && |
35d0ac90 PNA |
6205 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { |
6206 | err = -ENFILE; | |
7b225d0b | 6207 | goto err_set_full; |
35d0ac90 PNA |
6208 | } |
6209 | ||
60319eb1 | 6210 | nft_trans_elem(trans) = elem; |
0854db2a | 6211 | nft_trans_commit_list_add_tail(ctx->net, trans); |
20a69341 PM |
6212 | return 0; |
6213 | ||
7b225d0b | 6214 | err_set_full: |
aaa31047 | 6215 | nft_setelem_remove(ctx->net, set, &elem); |
7b225d0b | 6216 | err_element_clash: |
60319eb1 | 6217 | kfree(trans); |
34aae2c2 | 6218 | err_elem_free: |
b91d9036 TY |
6219 | if (obj) |
6220 | obj->use--; | |
34aae2c2 | 6221 | err_elem_userdata: |
475beb9c | 6222 | nf_tables_set_elem_destroy(ctx, set, elem.priv); |
7b225d0b | 6223 | err_parse_data: |
20a69341 | 6224 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
fdb9c405 | 6225 | nft_data_release(&elem.data.val, desc.type); |
7b225d0b PNA |
6226 | err_parse_key_end: |
6227 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
6228 | err_parse_key: | |
20a1452c | 6229 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
40944452 | 6230 | err_set_elem_expr: |
664899e8 | 6231 | for (i = 0; i < num_exprs && expr_array[i]; i++) |
8cfd9b0f PNA |
6232 | nft_expr_destroy(ctx, expr_array[i]); |
6233 | err_set_elem_expr_clone: | |
20a69341 PM |
6234 | return err; |
6235 | } | |
6236 | ||
7dab8ee3 PNA |
6237 | static int nf_tables_newsetelem(struct sk_buff *skb, |
6238 | const struct nfnl_info *info, | |
6239 | const struct nlattr * const nla[]) | |
20a69341 | 6240 | { |
7dab8ee3 PNA |
6241 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
6242 | struct netlink_ext_ack *extack = info->extack; | |
6243 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6244 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6245 | struct net *net = info->net; |
20a69341 | 6246 | const struct nlattr *attr; |
e2b750d7 | 6247 | struct nft_table *table; |
20a69341 PM |
6248 | struct nft_set *set; |
6249 | struct nft_ctx ctx; | |
a654de8f | 6250 | int rem, err; |
20a69341 | 6251 | |
7d5570ca PNA |
6252 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
6253 | return -EINVAL; | |
6254 | ||
e2b750d7 PNA |
6255 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6256 | genmask, NETLINK_CB(skb).portid); | |
6257 | if (IS_ERR(table)) { | |
6258 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6259 | return PTR_ERR(table); | |
6260 | } | |
20a69341 | 6261 | |
e2b750d7 | 6262 | set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET], |
a3073c17 PNA |
6263 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); |
6264 | if (IS_ERR(set)) | |
6265 | return PTR_ERR(set); | |
958bee14 | 6266 | |
20a69341 PM |
6267 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
6268 | return -EBUSY; | |
6269 | ||
e2b750d7 PNA |
6270 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6271 | ||
20a69341 | 6272 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7dab8ee3 | 6273 | err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags); |
b53c1166 PNA |
6274 | if (err < 0) { |
6275 | NL_SET_BAD_ATTR(extack, attr); | |
a654de8f | 6276 | return err; |
b53c1166 | 6277 | } |
20a69341 | 6278 | } |
a654de8f | 6279 | |
0854db2a | 6280 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
e2b750d7 | 6281 | return nft_table_validate(net, table); |
a654de8f PNA |
6282 | |
6283 | return 0; | |
20a69341 PM |
6284 | } |
6285 | ||
59105446 PNA |
6286 | /** |
6287 | * nft_data_hold - hold a nft_data item | |
6288 | * | |
6289 | * @data: struct nft_data to release | |
6290 | * @type: type of data | |
6291 | * | |
6292 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
6293 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
6294 | * NFT_GOTO verdicts. This function must be called on active data objects | |
6295 | * from the second phase of the commit protocol. | |
6296 | */ | |
bb7b40ae | 6297 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 | 6298 | { |
d0e2c7de PNA |
6299 | struct nft_chain *chain; |
6300 | struct nft_rule *rule; | |
6301 | ||
59105446 PNA |
6302 | if (type == NFT_DATA_VERDICT) { |
6303 | switch (data->verdict.code) { | |
6304 | case NFT_JUMP: | |
6305 | case NFT_GOTO: | |
d0e2c7de PNA |
6306 | chain = data->verdict.chain; |
6307 | chain->use++; | |
6308 | ||
6309 | if (!nft_chain_is_bound(chain)) | |
6310 | break; | |
6311 | ||
6312 | chain->table->use++; | |
6313 | list_for_each_entry(rule, &chain->rules, list) | |
6314 | chain->use++; | |
6315 | ||
6316 | nft_chain_add(chain->table, chain); | |
59105446 PNA |
6317 | break; |
6318 | } | |
6319 | } | |
6320 | } | |
6321 | ||
f8bb7889 PNA |
6322 | static void nft_setelem_data_activate(const struct net *net, |
6323 | const struct nft_set *set, | |
6324 | struct nft_set_elem *elem) | |
59105446 PNA |
6325 | { |
6326 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6327 | ||
6328 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6329 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
6330 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6331 | (*nft_set_ext_obj(ext))->use++; | |
6332 | } | |
6333 | ||
f8bb7889 PNA |
6334 | static void nft_setelem_data_deactivate(const struct net *net, |
6335 | const struct nft_set *set, | |
6336 | struct nft_set_elem *elem) | |
59105446 PNA |
6337 | { |
6338 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6339 | ||
6340 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6341 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
6342 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6343 | (*nft_set_ext_obj(ext))->use--; | |
6344 | } | |
6345 | ||
60319eb1 | 6346 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
6347 | const struct nlattr *attr) |
6348 | { | |
6349 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 6350 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 6351 | struct nft_set_elem elem; |
3971ca14 | 6352 | struct nft_set_ext *ext; |
60319eb1 | 6353 | struct nft_trans *trans; |
3971ca14 | 6354 | u32 flags = 0; |
20a69341 PM |
6355 | int err; |
6356 | ||
8cb08174 JB |
6357 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6358 | nft_set_elem_policy, NULL); | |
20a69341 | 6359 | if (err < 0) |
20a1452c | 6360 | return err; |
20a69341 | 6361 | |
aaa31047 PNA |
6362 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6363 | if (err < 0) | |
6364 | return err; | |
6365 | ||
6366 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
20a1452c | 6367 | return -EINVAL; |
20a69341 | 6368 | |
88cccd90 PNA |
6369 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6370 | return -EINVAL; | |
6371 | ||
3971ca14 PNA |
6372 | nft_set_ext_prepare(&tmpl); |
6373 | ||
c39ba4de PNA |
6374 | if (flags != 0) { |
6375 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6376 | if (err < 0) | |
6377 | return err; | |
6378 | } | |
3971ca14 | 6379 | |
aaa31047 PNA |
6380 | if (nla[NFTA_SET_ELEM_KEY]) { |
6381 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6382 | nla[NFTA_SET_ELEM_KEY]); | |
6383 | if (err < 0) | |
6384 | return err; | |
20a69341 | 6385 | |
c39ba4de PNA |
6386 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6387 | if (err < 0) | |
6388 | goto fail_elem; | |
aaa31047 | 6389 | } |
3971ca14 | 6390 | |
7b225d0b PNA |
6391 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
6392 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6393 | nla[NFTA_SET_ELEM_KEY_END]); | |
6394 | if (err < 0) | |
c39ba4de | 6395 | goto fail_elem; |
7b225d0b | 6396 | |
c39ba4de PNA |
6397 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6398 | if (err < 0) | |
6399 | goto fail_elem_key_end; | |
7b225d0b PNA |
6400 | } |
6401 | ||
3971ca14 | 6402 | err = -ENOMEM; |
7b225d0b PNA |
6403 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
6404 | elem.key_end.val.data, NULL, 0, 0, | |
33758c89 | 6405 | GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6406 | if (IS_ERR(elem.priv)) { |
6407 | err = PTR_ERR(elem.priv); | |
c39ba4de | 6408 | goto fail_elem_key_end; |
34aae2c2 | 6409 | } |
3971ca14 PNA |
6410 | |
6411 | ext = nft_set_elem_ext(set, elem.priv); | |
6412 | if (flags) | |
6413 | *nft_set_ext_flags(ext) = flags; | |
6414 | ||
60319eb1 | 6415 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
6416 | if (trans == NULL) |
6417 | goto fail_trans; | |
20a69341 | 6418 | |
aaa31047 PNA |
6419 | err = nft_setelem_deactivate(ctx->net, set, &elem, flags); |
6420 | if (err < 0) | |
20a1452c | 6421 | goto fail_ops; |
cc02e457 | 6422 | |
f8bb7889 | 6423 | nft_setelem_data_deactivate(ctx->net, set, &elem); |
59105446 | 6424 | |
60319eb1 | 6425 | nft_trans_elem(trans) = elem; |
0854db2a | 6426 | nft_trans_commit_list_add_tail(ctx->net, trans); |
0dc13625 | 6427 | return 0; |
cc02e457 | 6428 | |
20a1452c | 6429 | fail_ops: |
cc02e457 | 6430 | kfree(trans); |
20a1452c | 6431 | fail_trans: |
3971ca14 | 6432 | kfree(elem.priv); |
c39ba4de PNA |
6433 | fail_elem_key_end: |
6434 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
20a1452c PNA |
6435 | fail_elem: |
6436 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
6437 | return err; |
6438 | } | |
6439 | ||
e6ba7cb6 PNA |
6440 | static int nft_setelem_flush(const struct nft_ctx *ctx, |
6441 | struct nft_set *set, | |
6442 | const struct nft_set_iter *iter, | |
6443 | struct nft_set_elem *elem) | |
8411b644 PNA |
6444 | { |
6445 | struct nft_trans *trans; | |
6446 | int err; | |
6447 | ||
6448 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6449 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
6450 | if (!trans) | |
6451 | return -ENOMEM; | |
6452 | ||
1ba1c414 | 6453 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
6454 | err = -ENOENT; |
6455 | goto err1; | |
6456 | } | |
b2c11e4b | 6457 | set->ndeact++; |
8411b644 | 6458 | |
f8bb7889 | 6459 | nft_setelem_data_deactivate(ctx->net, set, elem); |
de70185d PNA |
6460 | nft_trans_elem_set(trans) = set; |
6461 | nft_trans_elem(trans) = *elem; | |
0854db2a | 6462 | nft_trans_commit_list_add_tail(ctx->net, trans); |
8411b644 PNA |
6463 | |
6464 | return 0; | |
6465 | err1: | |
6466 | kfree(trans); | |
6467 | return err; | |
6468 | } | |
6469 | ||
aaa31047 PNA |
6470 | static int __nft_set_catchall_flush(const struct nft_ctx *ctx, |
6471 | struct nft_set *set, | |
6472 | struct nft_set_elem *elem) | |
6473 | { | |
6474 | struct nft_trans *trans; | |
6475 | ||
6476 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6477 | sizeof(struct nft_trans_elem), GFP_KERNEL); | |
6478 | if (!trans) | |
6479 | return -ENOMEM; | |
6480 | ||
6481 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
6482 | nft_trans_elem_set(trans) = set; | |
6483 | nft_trans_elem(trans) = *elem; | |
6484 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
6485 | ||
6486 | return 0; | |
6487 | } | |
6488 | ||
6489 | static int nft_set_catchall_flush(const struct nft_ctx *ctx, | |
6490 | struct nft_set *set) | |
6491 | { | |
6492 | u8 genmask = nft_genmask_next(ctx->net); | |
6493 | struct nft_set_elem_catchall *catchall; | |
6494 | struct nft_set_elem elem; | |
6495 | struct nft_set_ext *ext; | |
6496 | int ret = 0; | |
6497 | ||
6498 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
6499 | ext = nft_set_elem_ext(set, catchall->elem); | |
6500 | if (!nft_set_elem_active(ext, genmask) || | |
6501 | nft_set_elem_mark_busy(ext)) | |
6502 | continue; | |
6503 | ||
6504 | elem.priv = catchall->elem; | |
6505 | ret = __nft_set_catchall_flush(ctx, set, &elem); | |
6506 | if (ret < 0) | |
6507 | break; | |
6508 | } | |
6509 | ||
6510 | return ret; | |
6511 | } | |
6512 | ||
e6ba7cb6 PNA |
6513 | static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) |
6514 | { | |
6515 | struct nft_set_iter iter = { | |
6516 | .genmask = genmask, | |
6517 | .fn = nft_setelem_flush, | |
6518 | }; | |
6519 | ||
6520 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
6521 | if (!iter.err) |
6522 | iter.err = nft_set_catchall_flush(ctx, set); | |
e6ba7cb6 PNA |
6523 | |
6524 | return iter.err; | |
6525 | } | |
6526 | ||
7dab8ee3 PNA |
6527 | static int nf_tables_delsetelem(struct sk_buff *skb, |
6528 | const struct nfnl_info *info, | |
6529 | const struct nlattr * const nla[]) | |
20a69341 | 6530 | { |
7dab8ee3 PNA |
6531 | struct netlink_ext_ack *extack = info->extack; |
6532 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6533 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6534 | struct net *net = info->net; |
20a69341 | 6535 | const struct nlattr *attr; |
e2b750d7 | 6536 | struct nft_table *table; |
20a69341 PM |
6537 | struct nft_set *set; |
6538 | struct nft_ctx ctx; | |
60319eb1 | 6539 | int rem, err = 0; |
20a69341 | 6540 | |
e2b750d7 PNA |
6541 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6542 | genmask, NETLINK_CB(skb).portid); | |
6543 | if (IS_ERR(table)) { | |
6544 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6545 | return PTR_ERR(table); | |
6546 | } | |
20a69341 | 6547 | |
e2b750d7 | 6548 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
6549 | if (IS_ERR(set)) |
6550 | return PTR_ERR(set); | |
6551 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
6552 | return -EBUSY; | |
6553 | ||
e2b750d7 PNA |
6554 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6555 | ||
e6ba7cb6 PNA |
6556 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) |
6557 | return nft_set_flush(&ctx, set, genmask); | |
8411b644 | 6558 | |
20a69341 PM |
6559 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
6560 | err = nft_del_setelem(&ctx, set, attr); | |
b53c1166 PNA |
6561 | if (err < 0) { |
6562 | NL_SET_BAD_ATTR(extack, attr); | |
60319eb1 | 6563 | break; |
b53c1166 | 6564 | } |
20a69341 | 6565 | } |
60319eb1 | 6566 | return err; |
20a69341 PM |
6567 | } |
6568 | ||
cfed7e1b PM |
6569 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
6570 | { | |
6571 | struct nft_set_gc_batch *gcb; | |
6572 | unsigned int i; | |
6573 | ||
6574 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
6575 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 6576 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
6577 | kfree(gcb); |
6578 | } | |
cfed7e1b PM |
6579 | |
6580 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
6581 | gfp_t gfp) | |
6582 | { | |
6583 | struct nft_set_gc_batch *gcb; | |
6584 | ||
6585 | gcb = kzalloc(sizeof(*gcb), gfp); | |
6586 | if (gcb == NULL) | |
6587 | return gcb; | |
6588 | gcb->head.set = set; | |
6589 | return gcb; | |
6590 | } | |
cfed7e1b | 6591 | |
e5009240 PNA |
6592 | /* |
6593 | * Stateful objects | |
6594 | */ | |
6595 | ||
6596 | /** | |
6597 | * nft_register_obj- register nf_tables stateful object type | |
3db86c39 | 6598 | * @obj_type: object type |
e5009240 PNA |
6599 | * |
6600 | * Registers the object type for use with nf_tables. Returns zero on | |
6601 | * success or a negative errno code otherwise. | |
6602 | */ | |
6603 | int nft_register_obj(struct nft_object_type *obj_type) | |
6604 | { | |
6605 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
6606 | return -EINVAL; | |
6607 | ||
6608 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6609 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
6610 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6611 | return 0; | |
6612 | } | |
6613 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
6614 | ||
6615 | /** | |
6616 | * nft_unregister_obj - unregister nf_tables object type | |
3db86c39 | 6617 | * @obj_type: object type |
e5009240 PNA |
6618 | * |
6619 | * Unregisters the object type for use with nf_tables. | |
6620 | */ | |
6621 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
6622 | { | |
6623 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6624 | list_del_rcu(&obj_type->list); | |
6625 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6626 | } | |
6627 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
6628 | ||
4d44175a FW |
6629 | struct nft_object *nft_obj_lookup(const struct net *net, |
6630 | const struct nft_table *table, | |
cac20fcd PNA |
6631 | const struct nlattr *nla, u32 objtype, |
6632 | u8 genmask) | |
e5009240 | 6633 | { |
4d44175a FW |
6634 | struct nft_object_hash_key k = { .table = table }; |
6635 | char search[NFT_OBJ_MAXNAMELEN]; | |
6636 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
6637 | struct nft_object *obj; |
6638 | ||
872f6903 | 6639 | nla_strscpy(search, nla, sizeof(search)); |
4d44175a FW |
6640 | k.name = search; |
6641 | ||
6642 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
6643 | !lockdep_commit_lock_is_held(net)); | |
6644 | ||
6645 | rcu_read_lock(); | |
6646 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
6647 | if (!list) | |
6648 | goto out; | |
6649 | ||
6650 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
6651 | if (objtype == obj->ops->type->type && | |
6652 | nft_active_genmask(obj, genmask)) { | |
6653 | rcu_read_unlock(); | |
e5009240 | 6654 | return obj; |
4d44175a | 6655 | } |
e5009240 | 6656 | } |
4d44175a FW |
6657 | out: |
6658 | rcu_read_unlock(); | |
e5009240 PNA |
6659 | return ERR_PTR(-ENOENT); |
6660 | } | |
cac20fcd | 6661 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 6662 | |
cac20fcd PNA |
6663 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
6664 | const struct nlattr *nla, | |
6665 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
6666 | { |
6667 | struct nft_object *obj; | |
6668 | ||
6669 | list_for_each_entry(obj, &table->objects, list) { | |
6670 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
6671 | objtype == obj->ops->type->type && | |
6672 | nft_active_genmask(obj, genmask)) | |
6673 | return obj; | |
6674 | } | |
6675 | return ERR_PTR(-ENOENT); | |
6676 | } | |
6677 | ||
e5009240 | 6678 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
6679 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
6680 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
6681 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
6682 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
6683 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
6684 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 6685 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
b131c964 JGG |
6686 | [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY, |
6687 | .len = NFT_USERDATA_MAXLEN }, | |
e5009240 PNA |
6688 | }; |
6689 | ||
84fba055 FW |
6690 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
6691 | const struct nft_object_type *type, | |
e5009240 PNA |
6692 | const struct nlattr *attr) |
6693 | { | |
5b4c6e38 | 6694 | struct nlattr **tb; |
dfc46034 | 6695 | const struct nft_object_ops *ops; |
e5009240 | 6696 | struct nft_object *obj; |
5b4c6e38 GS |
6697 | int err = -ENOMEM; |
6698 | ||
6699 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
6700 | if (!tb) | |
6701 | goto err1; | |
e5009240 PNA |
6702 | |
6703 | if (attr) { | |
8cb08174 JB |
6704 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
6705 | type->policy, NULL); | |
e5009240 | 6706 | if (err < 0) |
5b4c6e38 | 6707 | goto err2; |
e5009240 PNA |
6708 | } else { |
6709 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
6710 | } | |
6711 | ||
dfc46034 PBG |
6712 | if (type->select_ops) { |
6713 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
6714 | if (IS_ERR(ops)) { | |
6715 | err = PTR_ERR(ops); | |
5b4c6e38 | 6716 | goto err2; |
dfc46034 PBG |
6717 | } |
6718 | } else { | |
6719 | ops = type->ops; | |
6720 | } | |
6721 | ||
e5009240 | 6722 | err = -ENOMEM; |
33758c89 | 6723 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT); |
5b4c6e38 GS |
6724 | if (!obj) |
6725 | goto err2; | |
e5009240 | 6726 | |
dfc46034 | 6727 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 6728 | if (err < 0) |
5b4c6e38 | 6729 | goto err3; |
e5009240 | 6730 | |
dfc46034 PBG |
6731 | obj->ops = ops; |
6732 | ||
5b4c6e38 | 6733 | kfree(tb); |
e5009240 | 6734 | return obj; |
5b4c6e38 | 6735 | err3: |
e5009240 | 6736 | kfree(obj); |
5b4c6e38 GS |
6737 | err2: |
6738 | kfree(tb); | |
e5009240 PNA |
6739 | err1: |
6740 | return ERR_PTR(err); | |
6741 | } | |
6742 | ||
6743 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 6744 | struct nft_object *obj, bool reset) |
e5009240 PNA |
6745 | { |
6746 | struct nlattr *nest; | |
6747 | ||
ae0be8de | 6748 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
6749 | if (!nest) |
6750 | goto nla_put_failure; | |
dfc46034 | 6751 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
6752 | goto nla_put_failure; |
6753 | nla_nest_end(skb, nest); | |
6754 | return 0; | |
6755 | ||
6756 | nla_put_failure: | |
6757 | return -1; | |
6758 | } | |
6759 | ||
6760 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
6761 | { | |
6762 | const struct nft_object_type *type; | |
6763 | ||
6764 | list_for_each_entry(type, &nf_tables_objects, list) { | |
6765 | if (objtype == type->type) | |
6766 | return type; | |
6767 | } | |
6768 | return NULL; | |
6769 | } | |
6770 | ||
452238e8 FW |
6771 | static const struct nft_object_type * |
6772 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
6773 | { |
6774 | const struct nft_object_type *type; | |
6775 | ||
6776 | type = __nft_obj_type_get(objtype); | |
6777 | if (type != NULL && try_module_get(type->owner)) | |
6778 | return type; | |
6779 | ||
f102d66b | 6780 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
6781 | #ifdef CONFIG_MODULES |
6782 | if (type == NULL) { | |
eb014de4 | 6783 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
6784 | return ERR_PTR(-EAGAIN); |
6785 | } | |
6786 | #endif | |
6787 | return ERR_PTR(-ENOENT); | |
6788 | } | |
6789 | ||
d62d0ba9 FFM |
6790 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
6791 | const struct nft_object_type *type, | |
6792 | const struct nlattr *attr, | |
6793 | struct nft_object *obj) | |
6794 | { | |
6795 | struct nft_object *newobj; | |
6796 | struct nft_trans *trans; | |
dad3bdee FW |
6797 | int err = -ENOMEM; |
6798 | ||
6799 | if (!try_module_get(type->owner)) | |
6800 | return -ENOENT; | |
d62d0ba9 FFM |
6801 | |
6802 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
6803 | sizeof(struct nft_trans_obj)); | |
6804 | if (!trans) | |
dad3bdee | 6805 | goto err_trans; |
d62d0ba9 FFM |
6806 | |
6807 | newobj = nft_obj_init(ctx, type, attr); | |
6808 | if (IS_ERR(newobj)) { | |
6809 | err = PTR_ERR(newobj); | |
b74ae961 | 6810 | goto err_free_trans; |
d62d0ba9 FFM |
6811 | } |
6812 | ||
6813 | nft_trans_obj(trans) = obj; | |
6814 | nft_trans_obj_update(trans) = true; | |
6815 | nft_trans_obj_newobj(trans) = newobj; | |
0854db2a | 6816 | nft_trans_commit_list_add_tail(ctx->net, trans); |
d62d0ba9 FFM |
6817 | |
6818 | return 0; | |
b74ae961 DC |
6819 | |
6820 | err_free_trans: | |
d62d0ba9 | 6821 | kfree(trans); |
dad3bdee FW |
6822 | err_trans: |
6823 | module_put(type->owner); | |
d62d0ba9 FFM |
6824 | return err; |
6825 | } | |
6826 | ||
7dab8ee3 PNA |
6827 | static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info, |
6828 | const struct nlattr * const nla[]) | |
e5009240 | 6829 | { |
7dab8ee3 PNA |
6830 | struct netlink_ext_ack *extack = info->extack; |
6831 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 6832 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 6833 | const struct nft_object_type *type; |
7dab8ee3 | 6834 | struct net *net = info->net; |
e5009240 PNA |
6835 | struct nft_table *table; |
6836 | struct nft_object *obj; | |
6837 | struct nft_ctx ctx; | |
6838 | u32 objtype; | |
6839 | int err; | |
6840 | ||
6841 | if (!nla[NFTA_OBJ_TYPE] || | |
6842 | !nla[NFTA_OBJ_NAME] || | |
6843 | !nla[NFTA_OBJ_DATA]) | |
6844 | return -EINVAL; | |
6845 | ||
6001a930 PNA |
6846 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
6847 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
6848 | if (IS_ERR(table)) { |
6849 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 6850 | return PTR_ERR(table); |
36dd1bcc | 6851 | } |
e5009240 PNA |
6852 | |
6853 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 6854 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
6855 | if (IS_ERR(obj)) { |
6856 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
6857 | if (err != -ENOENT) { |
6858 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 6859 | return err; |
36dd1bcc | 6860 | } |
1a28ad74 | 6861 | } else { |
7dab8ee3 | 6862 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 6863 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); |
e5009240 | 6864 | return -EEXIST; |
36dd1bcc | 6865 | } |
7dab8ee3 | 6866 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
d62d0ba9 FFM |
6867 | return -EOPNOTSUPP; |
6868 | ||
fd57d0cb | 6869 | type = __nft_obj_type_get(objtype); |
7dab8ee3 | 6870 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
d62d0ba9 FFM |
6871 | |
6872 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
6873 | } |
6874 | ||
7dab8ee3 | 6875 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 | 6876 | |
452238e8 | 6877 | type = nft_obj_type_get(net, objtype); |
e5009240 PNA |
6878 | if (IS_ERR(type)) |
6879 | return PTR_ERR(type); | |
6880 | ||
84fba055 | 6881 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
6882 | if (IS_ERR(obj)) { |
6883 | err = PTR_ERR(obj); | |
b131c964 | 6884 | goto err_init; |
e5009240 | 6885 | } |
d152159b | 6886 | obj->key.table = table; |
3ecbfd65 HS |
6887 | obj->handle = nf_tables_alloc_handle(table); |
6888 | ||
33758c89 | 6889 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT); |
d152159b | 6890 | if (!obj->key.name) { |
61509575 | 6891 | err = -ENOMEM; |
b131c964 JGG |
6892 | goto err_strdup; |
6893 | } | |
6894 | ||
6895 | if (nla[NFTA_OBJ_USERDATA]) { | |
85db827a | 6896 | obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL); |
b131c964 JGG |
6897 | if (obj->udata == NULL) |
6898 | goto err_userdata; | |
6899 | ||
85db827a | 6900 | obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]); |
61509575 | 6901 | } |
e5009240 PNA |
6902 | |
6903 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
6904 | if (err < 0) | |
b131c964 | 6905 | goto err_trans; |
e5009240 | 6906 | |
4d44175a FW |
6907 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
6908 | nft_objname_ht_params); | |
6909 | if (err < 0) | |
b131c964 | 6910 | goto err_obj_ht; |
4d44175a | 6911 | |
e5009240 PNA |
6912 | list_add_tail_rcu(&obj->list, &table->objects); |
6913 | table->use++; | |
6914 | return 0; | |
b131c964 | 6915 | err_obj_ht: |
4d44175a FW |
6916 | /* queued in transaction log */ |
6917 | INIT_LIST_HEAD(&obj->list); | |
6918 | return err; | |
b131c964 | 6919 | err_trans: |
b131c964 | 6920 | kfree(obj->udata); |
85dfd816 PNA |
6921 | err_userdata: |
6922 | kfree(obj->key.name); | |
b131c964 | 6923 | err_strdup: |
dfc46034 | 6924 | if (obj->ops->destroy) |
00bfb320 | 6925 | obj->ops->destroy(&ctx, obj); |
e5009240 | 6926 | kfree(obj); |
b131c964 | 6927 | err_init: |
e5009240 PNA |
6928 | module_put(type->owner); |
6929 | return err; | |
6930 | } | |
6931 | ||
6932 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
6933 | u32 portid, u32 seq, int event, u32 flags, | |
6934 | int family, const struct nft_table *table, | |
43da04a5 | 6935 | struct nft_object *obj, bool reset) |
e5009240 | 6936 | { |
e5009240 PNA |
6937 | struct nlmsghdr *nlh; |
6938 | ||
dedb67c4 | 6939 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
6940 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
6941 | NFNETLINK_V0, nft_base_seq(net)); | |
6942 | if (!nlh) | |
e5009240 PNA |
6943 | goto nla_put_failure; |
6944 | ||
e5009240 | 6945 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || |
d152159b | 6946 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
dfc46034 | 6947 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || |
e5009240 | 6948 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || |
3ecbfd65 HS |
6949 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) || |
6950 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), | |
6951 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
6952 | goto nla_put_failure; |
6953 | ||
b131c964 JGG |
6954 | if (obj->udata && |
6955 | nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata)) | |
6956 | goto nla_put_failure; | |
6957 | ||
e5009240 PNA |
6958 | nlmsg_end(skb, nlh); |
6959 | return 0; | |
6960 | ||
6961 | nla_put_failure: | |
6962 | nlmsg_trim(skb, nlh); | |
6963 | return -1; | |
6964 | } | |
6965 | ||
a9fea2a3 | 6966 | struct nft_obj_filter { |
e46abbcc | 6967 | char *table; |
a9fea2a3 PNA |
6968 | u32 type; |
6969 | }; | |
6970 | ||
e5009240 PNA |
6971 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
6972 | { | |
6973 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 6974 | const struct nft_table *table; |
e5009240 | 6975 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 6976 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
6977 | struct net *net = sock_net(skb->sk); |
6978 | int family = nfmsg->nfgen_family; | |
0854db2a | 6979 | struct nftables_pernet *nft_net; |
43da04a5 PNA |
6980 | struct nft_object *obj; |
6981 | bool reset = false; | |
6982 | ||
6983 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
6984 | reset = true; | |
e5009240 PNA |
6985 | |
6986 | rcu_read_lock(); | |
d59d2f82 | 6987 | nft_net = nft_pernet(net); |
34002783 | 6988 | cb->seq = READ_ONCE(nft_net->base_seq); |
e5009240 | 6989 | |
0854db2a | 6990 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 6991 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
6992 | continue; |
6993 | ||
36596dad PNA |
6994 | list_for_each_entry_rcu(obj, &table->objects, list) { |
6995 | if (!nft_is_active(net, obj)) | |
6996 | goto cont; | |
6997 | if (idx < s_idx) | |
6998 | goto cont; | |
6999 | if (idx > s_idx) | |
7000 | memset(&cb->args[1], 0, | |
7001 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7002 | if (filter && filter->table && |
36596dad PNA |
7003 | strcmp(filter->table, table->name)) |
7004 | goto cont; | |
7005 | if (filter && | |
7006 | filter->type != NFT_OBJECT_UNSPEC && | |
7007 | obj->ops->type->type != filter->type) | |
7008 | goto cont; | |
8e6cf365 | 7009 | if (reset) { |
68df2ed5 | 7010 | char *buf = kasprintf(GFP_ATOMIC, |
c520292f | 7011 | "%s:%u", |
8e6cf365 | 7012 | table->name, |
0854db2a | 7013 | nft_net->base_seq); |
8e6cf365 RGB |
7014 | |
7015 | audit_log_nfcfg(buf, | |
7016 | family, | |
7017 | obj->handle, | |
14224039 | 7018 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7019 | GFP_ATOMIC); |
8e6cf365 RGB |
7020 | kfree(buf); |
7021 | } | |
7022 | ||
36596dad PNA |
7023 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
7024 | cb->nlh->nlmsg_seq, | |
7025 | NFT_MSG_NEWOBJ, | |
7026 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 7027 | table->family, table, |
36596dad PNA |
7028 | obj, reset) < 0) |
7029 | goto done; | |
e5009240 | 7030 | |
36596dad | 7031 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 7032 | cont: |
36596dad | 7033 | idx++; |
e5009240 PNA |
7034 | } |
7035 | } | |
7036 | done: | |
7037 | rcu_read_unlock(); | |
7038 | ||
7039 | cb->args[0] = idx; | |
7040 | return skb->len; | |
7041 | } | |
7042 | ||
90fd131a | 7043 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 7044 | { |
90fd131a FW |
7045 | const struct nlattr * const *nla = cb->data; |
7046 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 7047 | |
90fd131a FW |
7048 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
7049 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7050 | if (!filter) | |
7051 | return -ENOMEM; | |
7052 | ||
7053 | if (nla[NFTA_OBJ_TABLE]) { | |
7054 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
7055 | if (!filter->table) { | |
7056 | kfree(filter); | |
7057 | return -ENOMEM; | |
7058 | } | |
7059 | } | |
7060 | ||
7061 | if (nla[NFTA_OBJ_TYPE]) | |
7062 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 7063 | } |
a9fea2a3 | 7064 | |
90fd131a | 7065 | cb->data = filter; |
a9fea2a3 PNA |
7066 | return 0; |
7067 | } | |
7068 | ||
90fd131a | 7069 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 7070 | { |
90fd131a | 7071 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 7072 | |
90fd131a FW |
7073 | if (filter) { |
7074 | kfree(filter->table); | |
7075 | kfree(filter); | |
e46abbcc | 7076 | } |
a9fea2a3 | 7077 | |
90fd131a | 7078 | return 0; |
a9fea2a3 PNA |
7079 | } |
7080 | ||
d9adf22a | 7081 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7082 | static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info, |
7083 | const struct nlattr * const nla[]) | |
e5009240 | 7084 | { |
797d4980 PNA |
7085 | struct netlink_ext_ack *extack = info->extack; |
7086 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 7087 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7088 | const struct nft_table *table; |
797d4980 | 7089 | struct net *net = info->net; |
e5009240 PNA |
7090 | struct nft_object *obj; |
7091 | struct sk_buff *skb2; | |
43da04a5 | 7092 | bool reset = false; |
e5009240 PNA |
7093 | u32 objtype; |
7094 | int err; | |
7095 | ||
797d4980 | 7096 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
e5009240 | 7097 | struct netlink_dump_control c = { |
90fd131a | 7098 | .start = nf_tables_dump_obj_start, |
e5009240 | 7099 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 7100 | .done = nf_tables_dump_obj_done, |
d9adf22a | 7101 | .module = THIS_MODULE, |
90fd131a | 7102 | .data = (void *)nla, |
e5009240 | 7103 | }; |
a9fea2a3 | 7104 | |
797d4980 | 7105 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
e5009240 PNA |
7106 | } |
7107 | ||
7108 | if (!nla[NFTA_OBJ_NAME] || | |
7109 | !nla[NFTA_OBJ_TYPE]) | |
7110 | return -EINVAL; | |
7111 | ||
6001a930 | 7112 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0); |
36dd1bcc PNA |
7113 | if (IS_ERR(table)) { |
7114 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7115 | return PTR_ERR(table); |
36dd1bcc | 7116 | } |
e5009240 PNA |
7117 | |
7118 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7119 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
7120 | if (IS_ERR(obj)) { |
7121 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7122 | return PTR_ERR(obj); |
36dd1bcc | 7123 | } |
e5009240 | 7124 | |
d9adf22a | 7125 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
7126 | if (!skb2) |
7127 | return -ENOMEM; | |
7128 | ||
797d4980 | 7129 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
43da04a5 PNA |
7130 | reset = true; |
7131 | ||
8e6cf365 | 7132 | if (reset) { |
0854db2a FW |
7133 | const struct nftables_pernet *nft_net; |
7134 | char *buf; | |
7135 | ||
d59d2f82 | 7136 | nft_net = nft_pernet(net); |
0854db2a | 7137 | buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq); |
8e6cf365 RGB |
7138 | |
7139 | audit_log_nfcfg(buf, | |
7140 | family, | |
7141 | obj->handle, | |
14224039 | 7142 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7143 | GFP_ATOMIC); |
8e6cf365 RGB |
7144 | kfree(buf); |
7145 | } | |
7146 | ||
e5009240 | 7147 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 7148 | info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, |
43da04a5 | 7149 | family, table, obj, reset); |
e5009240 | 7150 | if (err < 0) |
ee921183 | 7151 | goto err_fill_obj_info; |
e5009240 | 7152 | |
ee921183 PNA |
7153 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
7154 | ||
7155 | err_fill_obj_info: | |
e5009240 PNA |
7156 | kfree_skb(skb2); |
7157 | return err; | |
e5009240 PNA |
7158 | } |
7159 | ||
00bfb320 | 7160 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 7161 | { |
dfc46034 | 7162 | if (obj->ops->destroy) |
00bfb320 | 7163 | obj->ops->destroy(ctx, obj); |
e5009240 | 7164 | |
dfc46034 | 7165 | module_put(obj->ops->type->owner); |
d152159b | 7166 | kfree(obj->key.name); |
bc7a7082 | 7167 | kfree(obj->udata); |
e5009240 PNA |
7168 | kfree(obj); |
7169 | } | |
7170 | ||
7dab8ee3 PNA |
7171 | static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info, |
7172 | const struct nlattr * const nla[]) | |
e5009240 | 7173 | { |
7dab8ee3 PNA |
7174 | struct netlink_ext_ack *extack = info->extack; |
7175 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7176 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 7177 | struct net *net = info->net; |
36dd1bcc | 7178 | const struct nlattr *attr; |
e5009240 PNA |
7179 | struct nft_table *table; |
7180 | struct nft_object *obj; | |
7181 | struct nft_ctx ctx; | |
7182 | u32 objtype; | |
7183 | ||
7184 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 7185 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
7186 | return -EINVAL; |
7187 | ||
6001a930 PNA |
7188 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7189 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7190 | if (IS_ERR(table)) { |
7191 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7192 | return PTR_ERR(table); |
36dd1bcc | 7193 | } |
e5009240 PNA |
7194 | |
7195 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
7196 | if (nla[NFTA_OBJ_HANDLE]) { |
7197 | attr = nla[NFTA_OBJ_HANDLE]; | |
7198 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
7199 | } else { | |
7200 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 7201 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
7202 | } |
7203 | ||
7204 | if (IS_ERR(obj)) { | |
7205 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7206 | return PTR_ERR(obj); |
36dd1bcc PNA |
7207 | } |
7208 | if (obj->use > 0) { | |
7209 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7210 | return -EBUSY; |
36dd1bcc | 7211 | } |
e5009240 | 7212 | |
7dab8ee3 | 7213 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 PNA |
7214 | |
7215 | return nft_delobj(&ctx, obj); | |
7216 | } | |
7217 | ||
d152159b | 7218 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 | 7219 | struct nft_object *obj, u32 portid, u32 seq, int event, |
6fb721cf | 7220 | u16 flags, int family, int report, gfp_t gfp) |
e5009240 | 7221 | { |
d59d2f82 | 7222 | struct nftables_pernet *nft_net = nft_pernet(net); |
e5009240 PNA |
7223 | struct sk_buff *skb; |
7224 | int err; | |
c520292f | 7225 | char *buf = kasprintf(gfp, "%s:%u", |
0854db2a | 7226 | table->name, nft_net->base_seq); |
8e6cf365 RGB |
7227 | |
7228 | audit_log_nfcfg(buf, | |
7229 | family, | |
7230 | obj->handle, | |
7231 | event == NFT_MSG_NEWOBJ ? | |
c520292f RGB |
7232 | AUDIT_NFT_OP_OBJ_REGISTER : |
7233 | AUDIT_NFT_OP_OBJ_UNREGISTER, | |
68df2ed5 | 7234 | gfp); |
8e6cf365 | 7235 | kfree(buf); |
e5009240 | 7236 | |
2599e989 PNA |
7237 | if (!report && |
7238 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 7239 | return; |
e5009240 | 7240 | |
2599e989 | 7241 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
7242 | if (skb == NULL) |
7243 | goto err; | |
7244 | ||
6fb721cf PNA |
7245 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, |
7246 | flags & (NLM_F_CREATE | NLM_F_EXCL), | |
7247 | family, table, obj, false); | |
e5009240 PNA |
7248 | if (err < 0) { |
7249 | kfree_skb(skb); | |
7250 | goto err; | |
7251 | } | |
7252 | ||
0854db2a | 7253 | nft_notify_enqueue(skb, report, &nft_net->notify_list); |
25e94a99 | 7254 | return; |
e5009240 | 7255 | err: |
25e94a99 | 7256 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 7257 | } |
2599e989 PNA |
7258 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
7259 | ||
25e94a99 PNA |
7260 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
7261 | struct nft_object *obj, int event) | |
2599e989 | 7262 | { |
25e94a99 | 7263 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
6fb721cf | 7264 | ctx->flags, ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 7265 | } |
e5009240 | 7266 | |
3b49e2e9 PNA |
7267 | /* |
7268 | * Flow tables | |
7269 | */ | |
7270 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
7271 | { | |
7272 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7273 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
7274 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7275 | } | |
7276 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
7277 | ||
7278 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
7279 | { | |
7280 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7281 | list_del_rcu(&type->list); | |
7282 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7283 | } | |
7284 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
7285 | ||
7286 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
7287 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
7288 | .len = NFT_NAME_MAXLEN - 1 }, | |
7289 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
7290 | .len = NFT_NAME_MAXLEN - 1 }, | |
7291 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7292 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 7293 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
7294 | }; |
7295 | ||
cac20fcd PNA |
7296 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
7297 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
7298 | { |
7299 | struct nft_flowtable *flowtable; | |
7300 | ||
d9adf22a | 7301 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
7302 | if (!nla_strcmp(nla, flowtable->name) && |
7303 | nft_active_genmask(flowtable, genmask)) | |
7304 | return flowtable; | |
7305 | } | |
7306 | return ERR_PTR(-ENOENT); | |
7307 | } | |
cac20fcd | 7308 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 7309 | |
9b05b6e1 LGL |
7310 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
7311 | struct nft_flowtable *flowtable, | |
7312 | enum nft_trans_phase phase) | |
7313 | { | |
7314 | switch (phase) { | |
7315 | case NFT_TRANS_PREPARE: | |
7316 | case NFT_TRANS_ABORT: | |
7317 | case NFT_TRANS_RELEASE: | |
7318 | flowtable->use--; | |
954d8297 | 7319 | fallthrough; |
9b05b6e1 LGL |
7320 | default: |
7321 | return; | |
7322 | } | |
7323 | } | |
7324 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
7325 | ||
ae0662f8 | 7326 | static struct nft_flowtable * |
cac20fcd PNA |
7327 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
7328 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
7329 | { |
7330 | struct nft_flowtable *flowtable; | |
7331 | ||
7332 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
7333 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
7334 | nft_active_genmask(flowtable, genmask)) | |
7335 | return flowtable; | |
7336 | } | |
7337 | return ERR_PTR(-ENOENT); | |
7338 | } | |
7339 | ||
d9246a53 PNA |
7340 | struct nft_flowtable_hook { |
7341 | u32 num; | |
7342 | int priority; | |
7343 | struct list_head list; | |
7344 | }; | |
7345 | ||
3b49e2e9 PNA |
7346 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
7347 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
7348 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
7349 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
7350 | }; | |
7351 | ||
d9246a53 PNA |
7352 | static int nft_flowtable_parse_hook(const struct nft_ctx *ctx, |
7353 | const struct nlattr *attr, | |
7354 | struct nft_flowtable_hook *flowtable_hook, | |
5b6743fb | 7355 | struct nft_flowtable *flowtable, bool add) |
3b49e2e9 | 7356 | { |
3b49e2e9 | 7357 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 7358 | struct nft_hook *hook; |
3b49e2e9 | 7359 | int hooknum, priority; |
3f0465a9 | 7360 | int err; |
3b49e2e9 | 7361 | |
d9246a53 PNA |
7362 | INIT_LIST_HEAD(&flowtable_hook->list); |
7363 | ||
8cb08174 JB |
7364 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr, |
7365 | nft_flowtable_hook_policy, NULL); | |
3b49e2e9 PNA |
7366 | if (err < 0) |
7367 | return err; | |
7368 | ||
5b6743fb PNA |
7369 | if (add) { |
7370 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
7371 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) | |
7372 | return -EINVAL; | |
3b49e2e9 | 7373 | |
5b6743fb PNA |
7374 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); |
7375 | if (hooknum != NF_NETDEV_INGRESS) | |
7376 | return -EOPNOTSUPP; | |
7377 | ||
7378 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7379 | ||
7380 | flowtable_hook->priority = priority; | |
7381 | flowtable_hook->num = hooknum; | |
7382 | } else { | |
7383 | if (tb[NFTA_FLOWTABLE_HOOK_NUM]) { | |
7384 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
7385 | if (hooknum != flowtable->hooknum) | |
7386 | return -EOPNOTSUPP; | |
7387 | } | |
7388 | ||
7389 | if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { | |
7390 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7391 | if (priority != flowtable->data.priority) | |
7392 | return -EOPNOTSUPP; | |
7393 | } | |
3b49e2e9 | 7394 | |
5b6743fb PNA |
7395 | flowtable_hook->priority = flowtable->data.priority; |
7396 | flowtable_hook->num = flowtable->hooknum; | |
7397 | } | |
3b49e2e9 | 7398 | |
05abe445 PNA |
7399 | if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) { |
7400 | err = nf_tables_parse_netdev_hooks(ctx->net, | |
7401 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
7402 | &flowtable_hook->list); | |
7403 | if (err < 0) | |
7404 | return err; | |
7405 | } | |
3b49e2e9 | 7406 | |
d9246a53 | 7407 | list_for_each_entry(hook, &flowtable_hook->list, list) { |
3f0465a9 | 7408 | hook->ops.pf = NFPROTO_NETDEV; |
5b6743fb PNA |
7409 | hook->ops.hooknum = flowtable_hook->num; |
7410 | hook->ops.priority = flowtable_hook->priority; | |
7411 | hook->ops.priv = &flowtable->data; | |
7412 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
7413 | } |
7414 | ||
3b49e2e9 PNA |
7415 | return err; |
7416 | } | |
7417 | ||
98319cb9 | 7418 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
7419 | { |
7420 | const struct nf_flowtable_type *type; | |
7421 | ||
7422 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 7423 | if (family == type->family) |
3b49e2e9 PNA |
7424 | return type; |
7425 | } | |
7426 | return NULL; | |
7427 | } | |
7428 | ||
452238e8 FW |
7429 | static const struct nf_flowtable_type * |
7430 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
7431 | { |
7432 | const struct nf_flowtable_type *type; | |
7433 | ||
98319cb9 | 7434 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
7435 | if (type != NULL && try_module_get(type->owner)) |
7436 | return type; | |
7437 | ||
f102d66b | 7438 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
7439 | #ifdef CONFIG_MODULES |
7440 | if (type == NULL) { | |
eb014de4 | 7441 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
7442 | return ERR_PTR(-EAGAIN); |
7443 | } | |
7444 | #endif | |
7445 | return ERR_PTR(-ENOENT); | |
7446 | } | |
7447 | ||
5acab914 | 7448 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
7449 | static void nft_unregister_flowtable_hook(struct net *net, |
7450 | struct nft_flowtable *flowtable, | |
7451 | struct nft_hook *hook) | |
7452 | { | |
7453 | nf_unregister_net_hook(net, &hook->ops); | |
7454 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
7455 | FLOW_BLOCK_UNBIND); | |
7456 | } | |
7457 | ||
f9a43007 PNA |
7458 | static void __nft_unregister_flowtable_net_hooks(struct net *net, |
7459 | struct list_head *hook_list, | |
7460 | bool release_netdev) | |
3b49e2e9 | 7461 | { |
f9a43007 | 7462 | struct nft_hook *hook, *next; |
3b49e2e9 | 7463 | |
f9a43007 | 7464 | list_for_each_entry_safe(hook, next, hook_list, list) { |
5acab914 | 7465 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
7466 | if (release_netdev) { |
7467 | list_del(&hook->list); | |
ab5e5c06 | 7468 | kfree_rcu(hook, rcu); |
f9a43007 PNA |
7469 | } |
7470 | } | |
7471 | } | |
7472 | ||
7473 | static void nft_unregister_flowtable_net_hooks(struct net *net, | |
7474 | struct list_head *hook_list) | |
7475 | { | |
7476 | __nft_unregister_flowtable_net_hooks(net, hook_list, false); | |
3f0465a9 PNA |
7477 | } |
7478 | ||
7479 | static int nft_register_flowtable_net_hooks(struct net *net, | |
7480 | struct nft_table *table, | |
f9382669 | 7481 | struct list_head *hook_list, |
3f0465a9 PNA |
7482 | struct nft_flowtable *flowtable) |
7483 | { | |
7484 | struct nft_hook *hook, *hook2, *next; | |
7485 | struct nft_flowtable *ft; | |
7486 | int err, i = 0; | |
7487 | ||
f9382669 | 7488 | list_for_each_entry(hook, hook_list, list) { |
3f0465a9 | 7489 | list_for_each_entry(ft, &table->flowtables, list) { |
86fe2c19 PNA |
7490 | if (!nft_is_active_next(net, ft)) |
7491 | continue; | |
7492 | ||
3f0465a9 PNA |
7493 | list_for_each_entry(hook2, &ft->hook_list, list) { |
7494 | if (hook->ops.dev == hook2->ops.dev && | |
7495 | hook->ops.pf == hook2->ops.pf) { | |
77a92189 | 7496 | err = -EEXIST; |
3f0465a9 PNA |
7497 | goto err_unregister_net_hooks; |
7498 | } | |
7499 | } | |
7500 | } | |
3b49e2e9 | 7501 | |
d7c03a9f | 7502 | err = flowtable->data.type->setup(&flowtable->data, |
7503 | hook->ops.dev, | |
7504 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
7505 | if (err < 0) |
7506 | goto err_unregister_net_hooks; | |
7507 | ||
d7c03a9f | 7508 | err = nf_register_net_hook(net, &hook->ops); |
7509 | if (err < 0) { | |
7510 | flowtable->data.type->setup(&flowtable->data, | |
7511 | hook->ops.dev, | |
7512 | FLOW_BLOCK_UNBIND); | |
7513 | goto err_unregister_net_hooks; | |
7514 | } | |
7515 | ||
3f0465a9 | 7516 | i++; |
3b49e2e9 | 7517 | } |
3f0465a9 PNA |
7518 | |
7519 | return 0; | |
7520 | ||
7521 | err_unregister_net_hooks: | |
f9382669 | 7522 | list_for_each_entry_safe(hook, next, hook_list, list) { |
3f0465a9 PNA |
7523 | if (i-- <= 0) |
7524 | break; | |
7525 | ||
ff4bf2f4 | 7526 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7527 | list_del_rcu(&hook->list); |
7528 | kfree_rcu(hook, rcu); | |
7529 | } | |
7530 | ||
7531 | return err; | |
3b49e2e9 PNA |
7532 | } |
7533 | ||
389a2cbc PNA |
7534 | static void nft_flowtable_hooks_destroy(struct list_head *hook_list) |
7535 | { | |
7536 | struct nft_hook *hook, *next; | |
7537 | ||
7538 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
7539 | list_del_rcu(&hook->list); | |
7540 | kfree_rcu(hook, rcu); | |
7541 | } | |
7542 | } | |
7543 | ||
78d9f48f PNA |
7544 | static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, |
7545 | struct nft_flowtable *flowtable) | |
7546 | { | |
7547 | const struct nlattr * const *nla = ctx->nla; | |
7548 | struct nft_flowtable_hook flowtable_hook; | |
7549 | struct nft_hook *hook, *next; | |
7550 | struct nft_trans *trans; | |
7551 | bool unregister = false; | |
7b35582c | 7552 | u32 flags; |
78d9f48f PNA |
7553 | int err; |
7554 | ||
7555 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7556 | &flowtable_hook, flowtable, false); |
78d9f48f PNA |
7557 | if (err < 0) |
7558 | return err; | |
7559 | ||
7560 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7561 | if (nft_hook_list_find(&flowtable->hook_list, hook)) { | |
7562 | list_del(&hook->list); | |
7563 | kfree(hook); | |
7564 | } | |
7565 | } | |
7566 | ||
7b35582c PNA |
7567 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7568 | flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
c271cc9f PNA |
7569 | if (flags & ~NFT_FLOWTABLE_MASK) { |
7570 | err = -EOPNOTSUPP; | |
7571 | goto err_flowtable_update_hook; | |
7572 | } | |
7b35582c | 7573 | if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^ |
c271cc9f PNA |
7574 | (flags & NFT_FLOWTABLE_HW_OFFLOAD)) { |
7575 | err = -EOPNOTSUPP; | |
7576 | goto err_flowtable_update_hook; | |
7577 | } | |
7b35582c PNA |
7578 | } else { |
7579 | flags = flowtable->data.flags; | |
7580 | } | |
7581 | ||
78d9f48f PNA |
7582 | err = nft_register_flowtable_net_hooks(ctx->net, ctx->table, |
7583 | &flowtable_hook.list, flowtable); | |
7584 | if (err < 0) | |
7585 | goto err_flowtable_update_hook; | |
7586 | ||
7587 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE, | |
7588 | sizeof(struct nft_trans_flowtable)); | |
7589 | if (!trans) { | |
7590 | unregister = true; | |
7591 | err = -ENOMEM; | |
7592 | goto err_flowtable_update_hook; | |
7593 | } | |
7594 | ||
7b35582c | 7595 | nft_trans_flowtable_flags(trans) = flags; |
78d9f48f PNA |
7596 | nft_trans_flowtable(trans) = flowtable; |
7597 | nft_trans_flowtable_update(trans) = true; | |
7598 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
7599 | list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans)); | |
7600 | ||
0854db2a | 7601 | nft_trans_commit_list_add_tail(ctx->net, trans); |
78d9f48f PNA |
7602 | |
7603 | return 0; | |
7604 | ||
7605 | err_flowtable_update_hook: | |
7606 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7607 | if (unregister) | |
7608 | nft_unregister_flowtable_hook(ctx->net, flowtable, hook); | |
7609 | list_del_rcu(&hook->list); | |
7610 | kfree_rcu(hook, rcu); | |
7611 | } | |
7612 | ||
7613 | return err; | |
7614 | ||
7615 | } | |
7616 | ||
7dab8ee3 PNA |
7617 | static int nf_tables_newflowtable(struct sk_buff *skb, |
7618 | const struct nfnl_info *info, | |
7619 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7620 | { |
7dab8ee3 | 7621 | struct netlink_ext_ack *extack = info->extack; |
d9246a53 | 7622 | struct nft_flowtable_hook flowtable_hook; |
7dab8ee3 | 7623 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 7624 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7625 | const struct nf_flowtable_type *type; |
3f0465a9 PNA |
7626 | struct nft_flowtable *flowtable; |
7627 | struct nft_hook *hook, *next; | |
7dab8ee3 | 7628 | struct net *net = info->net; |
3b49e2e9 PNA |
7629 | struct nft_table *table; |
7630 | struct nft_ctx ctx; | |
3f0465a9 | 7631 | int err; |
3b49e2e9 PNA |
7632 | |
7633 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
7634 | !nla[NFTA_FLOWTABLE_NAME] || | |
7635 | !nla[NFTA_FLOWTABLE_HOOK]) | |
7636 | return -EINVAL; | |
7637 | ||
cac20fcd | 7638 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7639 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7640 | if (IS_ERR(table)) { |
7641 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7642 | return PTR_ERR(table); |
36dd1bcc | 7643 | } |
3b49e2e9 | 7644 | |
cac20fcd PNA |
7645 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
7646 | genmask); | |
3b49e2e9 PNA |
7647 | if (IS_ERR(flowtable)) { |
7648 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
7649 | if (err != -ENOENT) { |
7650 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 7651 | return err; |
36dd1bcc | 7652 | } |
3b49e2e9 | 7653 | } else { |
7dab8ee3 | 7654 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 7655 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); |
3b49e2e9 | 7656 | return -EEXIST; |
36dd1bcc | 7657 | } |
3b49e2e9 | 7658 | |
7dab8ee3 | 7659 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
78d9f48f | 7660 | |
7dab8ee3 | 7661 | return nft_flowtable_update(&ctx, info->nlh, flowtable); |
3b49e2e9 PNA |
7662 | } |
7663 | ||
7dab8ee3 | 7664 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
3b49e2e9 | 7665 | |
33758c89 | 7666 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
7667 | if (!flowtable) |
7668 | return -ENOMEM; | |
7669 | ||
7670 | flowtable->table = table; | |
3ecbfd65 | 7671 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 7672 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 7673 | |
33758c89 | 7674 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
7675 | if (!flowtable->name) { |
7676 | err = -ENOMEM; | |
7677 | goto err1; | |
7678 | } | |
7679 | ||
452238e8 | 7680 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
7681 | if (IS_ERR(type)) { |
7682 | err = PTR_ERR(type); | |
7683 | goto err2; | |
7684 | } | |
7685 | ||
8bb69f3b PNA |
7686 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7687 | flowtable->data.flags = | |
7688 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
7e6136f1 PNA |
7689 | if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) { |
7690 | err = -EOPNOTSUPP; | |
8bb69f3b | 7691 | goto err3; |
7e6136f1 | 7692 | } |
8bb69f3b PNA |
7693 | } |
7694 | ||
7695 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 7696 | flowtable->data.type = type; |
a268de77 | 7697 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
7698 | if (err < 0) |
7699 | goto err3; | |
7700 | ||
d9246a53 | 7701 | err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK], |
5b6743fb | 7702 | &flowtable_hook, flowtable, true); |
3b49e2e9 | 7703 | if (err < 0) |
a268de77 | 7704 | goto err4; |
3b49e2e9 | 7705 | |
d9246a53 PNA |
7706 | list_splice(&flowtable_hook.list, &flowtable->hook_list); |
7707 | flowtable->data.priority = flowtable_hook.priority; | |
7708 | flowtable->hooknum = flowtable_hook.num; | |
7709 | ||
f9382669 PNA |
7710 | err = nft_register_flowtable_net_hooks(ctx.net, table, |
7711 | &flowtable->hook_list, | |
7712 | flowtable); | |
2d285f26 | 7713 | if (err < 0) { |
389a2cbc | 7714 | nft_flowtable_hooks_destroy(&flowtable->hook_list); |
3f0465a9 | 7715 | goto err4; |
2d285f26 | 7716 | } |
3b49e2e9 PNA |
7717 | |
7718 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
7719 | if (err < 0) | |
3f0465a9 | 7720 | goto err5; |
3b49e2e9 PNA |
7721 | |
7722 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
7723 | table->use++; | |
7724 | ||
7725 | return 0; | |
a268de77 | 7726 | err5: |
3f0465a9 | 7727 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 7728 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7729 | list_del_rcu(&hook->list); |
7730 | kfree_rcu(hook, rcu); | |
7731 | } | |
a268de77 FF |
7732 | err4: |
7733 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
7734 | err3: |
7735 | module_put(type->owner); | |
7736 | err2: | |
7737 | kfree(flowtable->name); | |
7738 | err1: | |
7739 | kfree(flowtable); | |
7740 | return err; | |
7741 | } | |
7742 | ||
3003055f PNA |
7743 | static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook) |
7744 | { | |
7745 | struct nft_hook *this, *next; | |
7746 | ||
7747 | list_for_each_entry_safe(this, next, &flowtable_hook->list, list) { | |
7748 | list_del(&this->list); | |
7749 | kfree(this); | |
7750 | } | |
7751 | } | |
7752 | ||
abadb2f8 PNA |
7753 | static int nft_delflowtable_hook(struct nft_ctx *ctx, |
7754 | struct nft_flowtable *flowtable) | |
7755 | { | |
7756 | const struct nlattr * const *nla = ctx->nla; | |
7757 | struct nft_flowtable_hook flowtable_hook; | |
b6d9014a | 7758 | LIST_HEAD(flowtable_del_list); |
3003055f | 7759 | struct nft_hook *this, *hook; |
abadb2f8 PNA |
7760 | struct nft_trans *trans; |
7761 | int err; | |
7762 | ||
7763 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7764 | &flowtable_hook, flowtable, false); |
abadb2f8 PNA |
7765 | if (err < 0) |
7766 | return err; | |
7767 | ||
3003055f | 7768 | list_for_each_entry(this, &flowtable_hook.list, list) { |
abadb2f8 PNA |
7769 | hook = nft_hook_list_find(&flowtable->hook_list, this); |
7770 | if (!hook) { | |
7771 | err = -ENOENT; | |
7772 | goto err_flowtable_del_hook; | |
7773 | } | |
b6d9014a | 7774 | list_move(&hook->list, &flowtable_del_list); |
abadb2f8 PNA |
7775 | } |
7776 | ||
7777 | trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE, | |
7778 | sizeof(struct nft_trans_flowtable)); | |
3003055f PNA |
7779 | if (!trans) { |
7780 | err = -ENOMEM; | |
7781 | goto err_flowtable_del_hook; | |
7782 | } | |
abadb2f8 PNA |
7783 | |
7784 | nft_trans_flowtable(trans) = flowtable; | |
7785 | nft_trans_flowtable_update(trans) = true; | |
7786 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
b6d9014a | 7787 | list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans)); |
3003055f | 7788 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 | 7789 | |
0854db2a | 7790 | nft_trans_commit_list_add_tail(ctx->net, trans); |
abadb2f8 PNA |
7791 | |
7792 | return 0; | |
7793 | ||
7794 | err_flowtable_del_hook: | |
b6d9014a | 7795 | list_splice(&flowtable_del_list, &flowtable->hook_list); |
3003055f | 7796 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 PNA |
7797 | |
7798 | return err; | |
7799 | } | |
7800 | ||
7dab8ee3 PNA |
7801 | static int nf_tables_delflowtable(struct sk_buff *skb, |
7802 | const struct nfnl_info *info, | |
7803 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7804 | { |
7dab8ee3 PNA |
7805 | struct netlink_ext_ack *extack = info->extack; |
7806 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7807 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7808 | struct nft_flowtable *flowtable; |
7dab8ee3 | 7809 | struct net *net = info->net; |
36dd1bcc | 7810 | const struct nlattr *attr; |
3b49e2e9 PNA |
7811 | struct nft_table *table; |
7812 | struct nft_ctx ctx; | |
7813 | ||
e603ea4b PNA |
7814 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
7815 | (!nla[NFTA_FLOWTABLE_NAME] && | |
7816 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
7817 | return -EINVAL; | |
7818 | ||
cac20fcd | 7819 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7820 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7821 | if (IS_ERR(table)) { |
7822 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7823 | return PTR_ERR(table); |
36dd1bcc | 7824 | } |
3b49e2e9 | 7825 | |
36dd1bcc PNA |
7826 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
7827 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
7828 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
7829 | } else { | |
7830 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
7831 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
7832 | } | |
7833 | ||
7834 | if (IS_ERR(flowtable)) { | |
7835 | NL_SET_BAD_ATTR(extack, attr); | |
7836 | return PTR_ERR(flowtable); | |
7837 | } | |
abadb2f8 | 7838 | |
7dab8ee3 | 7839 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
abadb2f8 PNA |
7840 | |
7841 | if (nla[NFTA_FLOWTABLE_HOOK]) | |
7842 | return nft_delflowtable_hook(&ctx, flowtable); | |
7843 | ||
36dd1bcc PNA |
7844 | if (flowtable->use > 0) { |
7845 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 7846 | return -EBUSY; |
36dd1bcc | 7847 | } |
3b49e2e9 | 7848 | |
3b49e2e9 PNA |
7849 | return nft_delflowtable(&ctx, flowtable); |
7850 | } | |
7851 | ||
7852 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
7853 | u32 portid, u32 seq, int event, | |
7854 | u32 flags, int family, | |
c42d8bda PNA |
7855 | struct nft_flowtable *flowtable, |
7856 | struct list_head *hook_list) | |
3b49e2e9 PNA |
7857 | { |
7858 | struct nlattr *nest, *nest_devs; | |
3f0465a9 | 7859 | struct nft_hook *hook; |
3b49e2e9 | 7860 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
7861 | |
7862 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
19c28b13 PNA |
7863 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
7864 | NFNETLINK_V0, nft_base_seq(net)); | |
7865 | if (!nlh) | |
3b49e2e9 PNA |
7866 | goto nla_put_failure; |
7867 | ||
3b49e2e9 PNA |
7868 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || |
7869 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 HS |
7870 | nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || |
7871 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), | |
8bb69f3b PNA |
7872 | NFTA_FLOWTABLE_PAD) || |
7873 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) | |
3b49e2e9 PNA |
7874 | goto nla_put_failure; |
7875 | ||
ae0be8de | 7876 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
7877 | if (!nest) |
7878 | goto nla_put_failure; | |
3b49e2e9 | 7879 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 7880 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
7881 | goto nla_put_failure; |
7882 | ||
ae0be8de | 7883 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
7884 | if (!nest_devs) |
7885 | goto nla_put_failure; | |
7886 | ||
c42d8bda | 7887 | list_for_each_entry_rcu(hook, hook_list, list) { |
3f0465a9 | 7888 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) |
3b49e2e9 PNA |
7889 | goto nla_put_failure; |
7890 | } | |
7891 | nla_nest_end(skb, nest_devs); | |
7892 | nla_nest_end(skb, nest); | |
7893 | ||
7894 | nlmsg_end(skb, nlh); | |
7895 | return 0; | |
7896 | ||
7897 | nla_put_failure: | |
7898 | nlmsg_trim(skb, nlh); | |
7899 | return -1; | |
7900 | } | |
7901 | ||
7902 | struct nft_flowtable_filter { | |
7903 | char *table; | |
7904 | }; | |
7905 | ||
7906 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
7907 | struct netlink_callback *cb) | |
7908 | { | |
7909 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
7910 | struct nft_flowtable_filter *filter = cb->data; | |
7911 | unsigned int idx = 0, s_idx = cb->args[0]; | |
7912 | struct net *net = sock_net(skb->sk); | |
7913 | int family = nfmsg->nfgen_family; | |
7914 | struct nft_flowtable *flowtable; | |
0854db2a | 7915 | struct nftables_pernet *nft_net; |
3b49e2e9 PNA |
7916 | const struct nft_table *table; |
7917 | ||
7918 | rcu_read_lock(); | |
d59d2f82 | 7919 | nft_net = nft_pernet(net); |
34002783 | 7920 | cb->seq = READ_ONCE(nft_net->base_seq); |
3b49e2e9 | 7921 | |
0854db2a | 7922 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 7923 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
7924 | continue; |
7925 | ||
36596dad PNA |
7926 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
7927 | if (!nft_is_active(net, flowtable)) | |
7928 | goto cont; | |
7929 | if (idx < s_idx) | |
7930 | goto cont; | |
7931 | if (idx > s_idx) | |
7932 | memset(&cb->args[1], 0, | |
7933 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7934 | if (filter && filter->table && |
36596dad PNA |
7935 | strcmp(filter->table, table->name)) |
7936 | goto cont; | |
3b49e2e9 | 7937 | |
36596dad PNA |
7938 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
7939 | cb->nlh->nlmsg_seq, | |
7940 | NFT_MSG_NEWFLOWTABLE, | |
7941 | NLM_F_MULTI | NLM_F_APPEND, | |
c42d8bda PNA |
7942 | table->family, |
7943 | flowtable, | |
7944 | &flowtable->hook_list) < 0) | |
36596dad | 7945 | goto done; |
3b49e2e9 | 7946 | |
36596dad | 7947 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 7948 | cont: |
36596dad | 7949 | idx++; |
3b49e2e9 PNA |
7950 | } |
7951 | } | |
7952 | done: | |
7953 | rcu_read_unlock(); | |
7954 | ||
7955 | cb->args[0] = idx; | |
7956 | return skb->len; | |
7957 | } | |
7958 | ||
90fd131a | 7959 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 7960 | { |
90fd131a FW |
7961 | const struct nlattr * const *nla = cb->data; |
7962 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 7963 | |
90fd131a FW |
7964 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
7965 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7966 | if (!filter) | |
7967 | return -ENOMEM; | |
3b49e2e9 | 7968 | |
90fd131a FW |
7969 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
7970 | GFP_ATOMIC); | |
7971 | if (!filter->table) { | |
7972 | kfree(filter); | |
7973 | return -ENOMEM; | |
7974 | } | |
7975 | } | |
3b49e2e9 | 7976 | |
90fd131a | 7977 | cb->data = filter; |
3b49e2e9 PNA |
7978 | return 0; |
7979 | } | |
7980 | ||
90fd131a | 7981 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 7982 | { |
90fd131a | 7983 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 7984 | |
3b49e2e9 | 7985 | if (!filter) |
90fd131a | 7986 | return 0; |
3b49e2e9 | 7987 | |
90fd131a FW |
7988 | kfree(filter->table); |
7989 | kfree(filter); | |
7990 | ||
7991 | return 0; | |
3b49e2e9 PNA |
7992 | } |
7993 | ||
d9adf22a | 7994 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7995 | static int nf_tables_getflowtable(struct sk_buff *skb, |
7996 | const struct nfnl_info *info, | |
7997 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7998 | { |
797d4980 | 7999 | u8 genmask = nft_genmask_cur(info->net); |
ef4b65e5 | 8000 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8001 | struct nft_flowtable *flowtable; |
3b49e2e9 | 8002 | const struct nft_table *table; |
797d4980 | 8003 | struct net *net = info->net; |
3b49e2e9 PNA |
8004 | struct sk_buff *skb2; |
8005 | int err; | |
8006 | ||
797d4980 | 8007 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
3b49e2e9 | 8008 | struct netlink_dump_control c = { |
90fd131a | 8009 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
8010 | .dump = nf_tables_dump_flowtable, |
8011 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 8012 | .module = THIS_MODULE, |
90fd131a | 8013 | .data = (void *)nla, |
3b49e2e9 PNA |
8014 | }; |
8015 | ||
797d4980 | 8016 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
3b49e2e9 PNA |
8017 | } |
8018 | ||
8019 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
8020 | return -EINVAL; | |
8021 | ||
cac20fcd | 8022 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8023 | genmask, 0); |
3b49e2e9 PNA |
8024 | if (IS_ERR(table)) |
8025 | return PTR_ERR(table); | |
8026 | ||
cac20fcd PNA |
8027 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
8028 | genmask); | |
03a0120f | 8029 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
8030 | return PTR_ERR(flowtable); |
8031 | ||
d9adf22a | 8032 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
8033 | if (!skb2) |
8034 | return -ENOMEM; | |
8035 | ||
8036 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
797d4980 | 8037 | info->nlh->nlmsg_seq, |
3b49e2e9 | 8038 | NFT_MSG_NEWFLOWTABLE, 0, family, |
c42d8bda | 8039 | flowtable, &flowtable->hook_list); |
3b49e2e9 | 8040 | if (err < 0) |
ee921183 | 8041 | goto err_fill_flowtable_info; |
3b49e2e9 | 8042 | |
ee921183 PNA |
8043 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
8044 | ||
8045 | err_fill_flowtable_info: | |
3b49e2e9 PNA |
8046 | kfree_skb(skb2); |
8047 | return err; | |
8048 | } | |
8049 | ||
8050 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
8051 | struct nft_flowtable *flowtable, | |
c42d8bda | 8052 | struct list_head *hook_list, |
3b49e2e9 PNA |
8053 | int event) |
8054 | { | |
d59d2f82 | 8055 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
3b49e2e9 | 8056 | struct sk_buff *skb; |
6fb721cf | 8057 | u16 flags = 0; |
3b49e2e9 PNA |
8058 | int err; |
8059 | ||
dceababa | 8060 | if (!ctx->report && |
3b49e2e9 PNA |
8061 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
8062 | return; | |
8063 | ||
8064 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
8065 | if (skb == NULL) | |
8066 | goto err; | |
8067 | ||
6fb721cf PNA |
8068 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
8069 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
8070 | ||
3b49e2e9 | 8071 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, |
6fb721cf | 8072 | ctx->seq, event, flags, |
c42d8bda | 8073 | ctx->family, flowtable, hook_list); |
3b49e2e9 PNA |
8074 | if (err < 0) { |
8075 | kfree_skb(skb); | |
8076 | goto err; | |
8077 | } | |
8078 | ||
0854db2a | 8079 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
3b49e2e9 PNA |
8080 | return; |
8081 | err: | |
8082 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
8083 | } | |
8084 | ||
3b49e2e9 PNA |
8085 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
8086 | { | |
3f0465a9 PNA |
8087 | struct nft_hook *hook, *next; |
8088 | ||
5acab914 | 8089 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 8090 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
8091 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
8092 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
8093 | list_del_rcu(&hook->list); |
8094 | kfree(hook); | |
8095 | } | |
3b49e2e9 | 8096 | kfree(flowtable->name); |
3b49e2e9 | 8097 | module_put(flowtable->data.type->owner); |
a12486eb | 8098 | kfree(flowtable); |
3b49e2e9 PNA |
8099 | } |
8100 | ||
84d7fce6 PNA |
8101 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
8102 | u32 portid, u32 seq) | |
8103 | { | |
d59d2f82 | 8104 | struct nftables_pernet *nft_net = nft_pernet(net); |
84d7fce6 | 8105 | struct nlmsghdr *nlh; |
784b4e61 | 8106 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 8107 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 | 8108 | |
19c28b13 PNA |
8109 | nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC, |
8110 | NFNETLINK_V0, nft_base_seq(net)); | |
8111 | if (!nlh) | |
84d7fce6 PNA |
8112 | goto nla_put_failure; |
8113 | ||
0854db2a | 8114 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) || |
784b4e61 PS |
8115 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || |
8116 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
8117 | goto nla_put_failure; |
8118 | ||
053c095a JB |
8119 | nlmsg_end(skb, nlh); |
8120 | return 0; | |
84d7fce6 PNA |
8121 | |
8122 | nla_put_failure: | |
8123 | nlmsg_trim(skb, nlh); | |
8124 | return -EMSGSIZE; | |
8125 | } | |
8126 | ||
3b49e2e9 PNA |
8127 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
8128 | struct nft_flowtable *flowtable) | |
8129 | { | |
3f0465a9 | 8130 | struct nft_hook *hook; |
3b49e2e9 | 8131 | |
3f0465a9 PNA |
8132 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
8133 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
8134 | continue; |
8135 | ||
5acab914 | 8136 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 8137 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
8138 | list_del_rcu(&hook->list); |
8139 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
8140 | break; |
8141 | } | |
8142 | } | |
8143 | ||
8144 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
8145 | unsigned long event, void *ptr) | |
8146 | { | |
8147 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
8148 | struct nft_flowtable *flowtable; | |
0854db2a | 8149 | struct nftables_pernet *nft_net; |
3b49e2e9 | 8150 | struct nft_table *table; |
0a2cf5ee | 8151 | struct net *net; |
3b49e2e9 PNA |
8152 | |
8153 | if (event != NETDEV_UNREGISTER) | |
8154 | return 0; | |
8155 | ||
6a48de01 | 8156 | net = dev_net(dev); |
d59d2f82 | 8157 | nft_net = nft_pernet(net); |
0854db2a FW |
8158 | mutex_lock(&nft_net->commit_mutex); |
8159 | list_for_each_entry(table, &nft_net->tables, list) { | |
36596dad PNA |
8160 | list_for_each_entry(flowtable, &table->flowtables, list) { |
8161 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
8162 | } |
8163 | } | |
0854db2a | 8164 | mutex_unlock(&nft_net->commit_mutex); |
6a48de01 | 8165 | |
3b49e2e9 PNA |
8166 | return NOTIFY_DONE; |
8167 | } | |
8168 | ||
8169 | static struct notifier_block nf_tables_flowtable_notifier = { | |
8170 | .notifier_call = nf_tables_flowtable_event, | |
8171 | }; | |
8172 | ||
25e94a99 PNA |
8173 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
8174 | int event) | |
84d7fce6 PNA |
8175 | { |
8176 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
8177 | struct sk_buff *skb2; | |
8178 | int err; | |
8179 | ||
dceababa | 8180 | if (!nlmsg_report(nlh) && |
84d7fce6 | 8181 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 8182 | return; |
84d7fce6 | 8183 | |
84d7fce6 PNA |
8184 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
8185 | if (skb2 == NULL) | |
8186 | goto err; | |
8187 | ||
8188 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
8189 | nlh->nlmsg_seq); | |
8190 | if (err < 0) { | |
8191 | kfree_skb(skb2); | |
8192 | goto err; | |
8193 | } | |
8194 | ||
25e94a99 PNA |
8195 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8196 | nlmsg_report(nlh), GFP_KERNEL); | |
8197 | return; | |
84d7fce6 | 8198 | err: |
25e94a99 PNA |
8199 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8200 | -ENOBUFS); | |
84d7fce6 PNA |
8201 | } |
8202 | ||
797d4980 PNA |
8203 | static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info, |
8204 | const struct nlattr * const nla[]) | |
84d7fce6 | 8205 | { |
84d7fce6 PNA |
8206 | struct sk_buff *skb2; |
8207 | int err; | |
8208 | ||
d9adf22a | 8209 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
8210 | if (skb2 == NULL) |
8211 | return -ENOMEM; | |
8212 | ||
797d4980 PNA |
8213 | err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid, |
8214 | info->nlh->nlmsg_seq); | |
84d7fce6 | 8215 | if (err < 0) |
ee921183 | 8216 | goto err_fill_gen_info; |
84d7fce6 | 8217 | |
797d4980 | 8218 | return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); |
ee921183 PNA |
8219 | |
8220 | err_fill_gen_info: | |
84d7fce6 PNA |
8221 | kfree_skb(skb2); |
8222 | return err; | |
8223 | } | |
8224 | ||
96518518 PM |
8225 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
8226 | [NFT_MSG_NEWTABLE] = { | |
50f2db9e PNA |
8227 | .call = nf_tables_newtable, |
8228 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8229 | .attr_count = NFTA_TABLE_MAX, |
8230 | .policy = nft_table_policy, | |
8231 | }, | |
8232 | [NFT_MSG_GETTABLE] = { | |
50f2db9e PNA |
8233 | .call = nf_tables_gettable, |
8234 | .type = NFNL_CB_RCU, | |
96518518 PM |
8235 | .attr_count = NFTA_TABLE_MAX, |
8236 | .policy = nft_table_policy, | |
8237 | }, | |
8238 | [NFT_MSG_DELTABLE] = { | |
50f2db9e PNA |
8239 | .call = nf_tables_deltable, |
8240 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8241 | .attr_count = NFTA_TABLE_MAX, |
8242 | .policy = nft_table_policy, | |
8243 | }, | |
8244 | [NFT_MSG_NEWCHAIN] = { | |
50f2db9e PNA |
8245 | .call = nf_tables_newchain, |
8246 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8247 | .attr_count = NFTA_CHAIN_MAX, |
8248 | .policy = nft_chain_policy, | |
8249 | }, | |
8250 | [NFT_MSG_GETCHAIN] = { | |
50f2db9e PNA |
8251 | .call = nf_tables_getchain, |
8252 | .type = NFNL_CB_RCU, | |
96518518 PM |
8253 | .attr_count = NFTA_CHAIN_MAX, |
8254 | .policy = nft_chain_policy, | |
8255 | }, | |
8256 | [NFT_MSG_DELCHAIN] = { | |
50f2db9e PNA |
8257 | .call = nf_tables_delchain, |
8258 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8259 | .attr_count = NFTA_CHAIN_MAX, |
8260 | .policy = nft_chain_policy, | |
8261 | }, | |
8262 | [NFT_MSG_NEWRULE] = { | |
50f2db9e PNA |
8263 | .call = nf_tables_newrule, |
8264 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8265 | .attr_count = NFTA_RULE_MAX, |
8266 | .policy = nft_rule_policy, | |
8267 | }, | |
8268 | [NFT_MSG_GETRULE] = { | |
50f2db9e PNA |
8269 | .call = nf_tables_getrule, |
8270 | .type = NFNL_CB_RCU, | |
96518518 PM |
8271 | .attr_count = NFTA_RULE_MAX, |
8272 | .policy = nft_rule_policy, | |
8273 | }, | |
8274 | [NFT_MSG_DELRULE] = { | |
50f2db9e PNA |
8275 | .call = nf_tables_delrule, |
8276 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8277 | .attr_count = NFTA_RULE_MAX, |
8278 | .policy = nft_rule_policy, | |
8279 | }, | |
20a69341 | 8280 | [NFT_MSG_NEWSET] = { |
50f2db9e PNA |
8281 | .call = nf_tables_newset, |
8282 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8283 | .attr_count = NFTA_SET_MAX, |
8284 | .policy = nft_set_policy, | |
8285 | }, | |
8286 | [NFT_MSG_GETSET] = { | |
50f2db9e PNA |
8287 | .call = nf_tables_getset, |
8288 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8289 | .attr_count = NFTA_SET_MAX, |
8290 | .policy = nft_set_policy, | |
8291 | }, | |
8292 | [NFT_MSG_DELSET] = { | |
50f2db9e PNA |
8293 | .call = nf_tables_delset, |
8294 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8295 | .attr_count = NFTA_SET_MAX, |
8296 | .policy = nft_set_policy, | |
8297 | }, | |
8298 | [NFT_MSG_NEWSETELEM] = { | |
50f2db9e PNA |
8299 | .call = nf_tables_newsetelem, |
8300 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8301 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8302 | .policy = nft_set_elem_list_policy, | |
8303 | }, | |
8304 | [NFT_MSG_GETSETELEM] = { | |
50f2db9e PNA |
8305 | .call = nf_tables_getsetelem, |
8306 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8307 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8308 | .policy = nft_set_elem_list_policy, | |
8309 | }, | |
8310 | [NFT_MSG_DELSETELEM] = { | |
50f2db9e PNA |
8311 | .call = nf_tables_delsetelem, |
8312 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8313 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8314 | .policy = nft_set_elem_list_policy, | |
8315 | }, | |
84d7fce6 | 8316 | [NFT_MSG_GETGEN] = { |
50f2db9e PNA |
8317 | .call = nf_tables_getgen, |
8318 | .type = NFNL_CB_RCU, | |
84d7fce6 | 8319 | }, |
e5009240 | 8320 | [NFT_MSG_NEWOBJ] = { |
50f2db9e PNA |
8321 | .call = nf_tables_newobj, |
8322 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8323 | .attr_count = NFTA_OBJ_MAX, |
8324 | .policy = nft_obj_policy, | |
8325 | }, | |
8326 | [NFT_MSG_GETOBJ] = { | |
50f2db9e PNA |
8327 | .call = nf_tables_getobj, |
8328 | .type = NFNL_CB_RCU, | |
e5009240 PNA |
8329 | .attr_count = NFTA_OBJ_MAX, |
8330 | .policy = nft_obj_policy, | |
8331 | }, | |
8332 | [NFT_MSG_DELOBJ] = { | |
50f2db9e PNA |
8333 | .call = nf_tables_delobj, |
8334 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8335 | .attr_count = NFTA_OBJ_MAX, |
8336 | .policy = nft_obj_policy, | |
8337 | }, | |
43da04a5 | 8338 | [NFT_MSG_GETOBJ_RESET] = { |
50f2db9e PNA |
8339 | .call = nf_tables_getobj, |
8340 | .type = NFNL_CB_RCU, | |
43da04a5 PNA |
8341 | .attr_count = NFTA_OBJ_MAX, |
8342 | .policy = nft_obj_policy, | |
8343 | }, | |
3b49e2e9 | 8344 | [NFT_MSG_NEWFLOWTABLE] = { |
50f2db9e PNA |
8345 | .call = nf_tables_newflowtable, |
8346 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8347 | .attr_count = NFTA_FLOWTABLE_MAX, |
8348 | .policy = nft_flowtable_policy, | |
8349 | }, | |
8350 | [NFT_MSG_GETFLOWTABLE] = { | |
50f2db9e PNA |
8351 | .call = nf_tables_getflowtable, |
8352 | .type = NFNL_CB_RCU, | |
3b49e2e9 PNA |
8353 | .attr_count = NFTA_FLOWTABLE_MAX, |
8354 | .policy = nft_flowtable_policy, | |
8355 | }, | |
8356 | [NFT_MSG_DELFLOWTABLE] = { | |
50f2db9e PNA |
8357 | .call = nf_tables_delflowtable, |
8358 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8359 | .attr_count = NFTA_FLOWTABLE_MAX, |
8360 | .policy = nft_flowtable_policy, | |
8361 | }, | |
96518518 PM |
8362 | }; |
8363 | ||
a654de8f PNA |
8364 | static int nf_tables_validate(struct net *net) |
8365 | { | |
d59d2f82 | 8366 | struct nftables_pernet *nft_net = nft_pernet(net); |
a654de8f PNA |
8367 | struct nft_table *table; |
8368 | ||
0854db2a | 8369 | switch (nft_net->validate_state) { |
a654de8f PNA |
8370 | case NFT_VALIDATE_SKIP: |
8371 | break; | |
8372 | case NFT_VALIDATE_NEED: | |
8373 | nft_validate_state_update(net, NFT_VALIDATE_DO); | |
954d8297 | 8374 | fallthrough; |
a654de8f | 8375 | case NFT_VALIDATE_DO: |
0854db2a | 8376 | list_for_each_entry(table, &nft_net->tables, list) { |
a654de8f PNA |
8377 | if (nft_table_validate(net, table) < 0) |
8378 | return -EAGAIN; | |
8379 | } | |
8380 | break; | |
8381 | } | |
8382 | ||
8383 | return 0; | |
8384 | } | |
8385 | ||
66293c46 FW |
8386 | /* a drop policy has to be deferred until all rules have been activated, |
8387 | * otherwise a large ruleset that contains a drop-policy base chain will | |
8388 | * cause all packets to get dropped until the full transaction has been | |
8389 | * processed. | |
8390 | * | |
8391 | * We defer the drop policy until the transaction has been finalized. | |
8392 | */ | |
8393 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
8394 | { | |
8395 | struct nft_base_chain *basechain; | |
8396 | ||
8397 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
8398 | return; | |
8399 | ||
8400 | if (!nft_is_base_chain(trans->ctx.chain)) | |
8401 | return; | |
8402 | ||
8403 | basechain = nft_base_chain(trans->ctx.chain); | |
8404 | basechain->policy = NF_DROP; | |
8405 | } | |
8406 | ||
91c7b38d PNA |
8407 | static void nft_chain_commit_update(struct nft_trans *trans) |
8408 | { | |
8409 | struct nft_base_chain *basechain; | |
8410 | ||
1b2470e5 FW |
8411 | if (nft_trans_chain_name(trans)) { |
8412 | rhltable_remove(&trans->ctx.table->chains_ht, | |
8413 | &trans->ctx.chain->rhlhead, | |
8414 | nft_chain_ht_params); | |
d71efb59 | 8415 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
8416 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
8417 | trans->ctx.chain->name, | |
8418 | &trans->ctx.chain->rhlhead, | |
8419 | nft_chain_ht_params); | |
8420 | } | |
91c7b38d | 8421 | |
f323d954 | 8422 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
8423 | return; |
8424 | ||
53315ac6 FW |
8425 | nft_chain_stats_replace(trans); |
8426 | ||
91c7b38d | 8427 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
8428 | |
8429 | switch (nft_trans_chain_policy(trans)) { | |
8430 | case NF_DROP: | |
8431 | case NF_ACCEPT: | |
8432 | basechain->policy = nft_trans_chain_policy(trans); | |
8433 | break; | |
8434 | } | |
8435 | } | |
8436 | ||
d62d0ba9 FFM |
8437 | static void nft_obj_commit_update(struct nft_trans *trans) |
8438 | { | |
8439 | struct nft_object *newobj; | |
8440 | struct nft_object *obj; | |
8441 | ||
8442 | obj = nft_trans_obj(trans); | |
8443 | newobj = nft_trans_obj_newobj(trans); | |
8444 | ||
9fedd894 FFM |
8445 | if (obj->ops->update) |
8446 | obj->ops->update(obj, newobj); | |
d62d0ba9 | 8447 | |
dad3bdee | 8448 | nft_obj_destroy(&trans->ctx, newobj); |
d62d0ba9 FFM |
8449 | } |
8450 | ||
2f99aa31 | 8451 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 8452 | { |
c7c32e72 PNA |
8453 | switch (trans->msg_type) { |
8454 | case NFT_MSG_DELTABLE: | |
8455 | nf_tables_table_destroy(&trans->ctx); | |
8456 | break; | |
9f8aac0b | 8457 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 8458 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
8459 | kfree(nft_trans_chain_name(trans)); |
8460 | break; | |
c7c32e72 | 8461 | case NFT_MSG_DELCHAIN: |
43a605f2 | 8462 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
8463 | break; |
8464 | case NFT_MSG_DELRULE: | |
9dd732e0 PNA |
8465 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
8466 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
8467 | ||
c7c32e72 PNA |
8468 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); |
8469 | break; | |
8470 | case NFT_MSG_DELSET: | |
0c2a85ed | 8471 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 8472 | break; |
61edafbb | 8473 | case NFT_MSG_DELSETELEM: |
3453c927 PNA |
8474 | nf_tables_set_elem_destroy(&trans->ctx, |
8475 | nft_trans_elem_set(trans), | |
59105446 | 8476 | nft_trans_elem(trans).priv); |
61edafbb | 8477 | break; |
e5009240 | 8478 | case NFT_MSG_DELOBJ: |
00bfb320 | 8479 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 8480 | break; |
3b49e2e9 | 8481 | case NFT_MSG_DELFLOWTABLE: |
abadb2f8 PNA |
8482 | if (nft_trans_flowtable_update(trans)) |
8483 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
8484 | else | |
8485 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 8486 | break; |
c7c32e72 | 8487 | } |
0935d558 FW |
8488 | |
8489 | if (trans->put_net) | |
8490 | put_net(trans->ctx.net); | |
8491 | ||
c7c32e72 PNA |
8492 | kfree(trans); |
8493 | } | |
8494 | ||
0935d558 | 8495 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
8496 | { |
8497 | struct nft_trans *trans, *next; | |
0935d558 FW |
8498 | LIST_HEAD(head); |
8499 | ||
8500 | spin_lock(&nf_tables_destroy_list_lock); | |
8501 | list_splice_init(&nf_tables_destroy_list, &head); | |
8502 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 8503 | |
0935d558 | 8504 | if (list_empty(&head)) |
2f99aa31 FW |
8505 | return; |
8506 | ||
8507 | synchronize_rcu(); | |
8508 | ||
0935d558 | 8509 | list_for_each_entry_safe(trans, next, &head, list) { |
2f99aa31 FW |
8510 | list_del(&trans->list); |
8511 | nft_commit_release(trans); | |
8512 | } | |
8513 | } | |
8514 | ||
ffe8923f FW |
8515 | void nf_tables_trans_destroy_flush_work(void) |
8516 | { | |
8517 | flush_work(&trans_destroy_work); | |
8518 | } | |
8519 | EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work); | |
8520 | ||
ed5f85d4 PNA |
8521 | static bool nft_expr_reduce(struct nft_regs_track *track, |
8522 | const struct nft_expr *expr) | |
8523 | { | |
9e539c5b | 8524 | return false; |
ed5f85d4 PNA |
8525 | } |
8526 | ||
0cbc06b3 FW |
8527 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
8528 | { | |
2c865a8a | 8529 | const struct nft_expr *expr, *last; |
12e4ecfa | 8530 | struct nft_regs_track track = {}; |
2c865a8a PNA |
8531 | unsigned int size, data_size; |
8532 | void *data, *data_boundary; | |
8533 | struct nft_rule_dp *prule; | |
0cbc06b3 | 8534 | struct nft_rule *rule; |
0cbc06b3 FW |
8535 | |
8536 | /* already handled or inactive chain? */ | |
2c865a8a | 8537 | if (chain->blob_next || !nft_is_active_next(net, chain)) |
0cbc06b3 FW |
8538 | return 0; |
8539 | ||
63045bfd | 8540 | data_size = 0; |
10377d42 | 8541 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
8542 | if (nft_is_active_next(net, rule)) { |
8543 | data_size += sizeof(*prule) + rule->dlen; | |
8544 | if (data_size > INT_MAX) | |
8545 | return -ENOMEM; | |
8546 | } | |
0cbc06b3 | 8547 | } |
2c865a8a | 8548 | data_size += offsetof(struct nft_rule_dp, data); /* last rule */ |
0cbc06b3 | 8549 | |
2c865a8a PNA |
8550 | chain->blob_next = nf_tables_chain_alloc_rules(data_size); |
8551 | if (!chain->blob_next) | |
0cbc06b3 FW |
8552 | return -ENOMEM; |
8553 | ||
2c865a8a PNA |
8554 | data = (void *)chain->blob_next->data; |
8555 | data_boundary = data + data_size; | |
8556 | size = 0; | |
8557 | ||
10377d42 | 8558 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
8559 | if (!nft_is_active_next(net, rule)) |
8560 | continue; | |
8561 | ||
8562 | prule = (struct nft_rule_dp *)data; | |
8563 | data += offsetof(struct nft_rule_dp, data); | |
8564 | if (WARN_ON_ONCE(data > data_boundary)) | |
8565 | return -ENOMEM; | |
8566 | ||
12e4ecfa | 8567 | size = 0; |
fe75e84a | 8568 | track.last = nft_expr_last(rule); |
2c865a8a | 8569 | nft_rule_for_each_expr(expr, last, rule) { |
12e4ecfa PNA |
8570 | track.cur = expr; |
8571 | ||
ed5f85d4 | 8572 | if (nft_expr_reduce(&track, expr)) { |
12e4ecfa PNA |
8573 | expr = track.cur; |
8574 | continue; | |
8575 | } | |
8576 | ||
2c865a8a PNA |
8577 | if (WARN_ON_ONCE(data + expr->ops->size > data_boundary)) |
8578 | return -ENOMEM; | |
8579 | ||
8580 | memcpy(data + size, expr, expr->ops->size); | |
8581 | size += expr->ops->size; | |
8582 | } | |
8583 | if (WARN_ON_ONCE(size >= 1 << 12)) | |
8584 | return -ENOMEM; | |
8585 | ||
8586 | prule->handle = rule->handle; | |
8587 | prule->dlen = size; | |
8588 | prule->is_last = 0; | |
8589 | ||
8590 | data += size; | |
8591 | size = 0; | |
8592 | chain->blob_next->size += (unsigned long)(data - (void *)prule); | |
0cbc06b3 FW |
8593 | } |
8594 | ||
2c865a8a PNA |
8595 | prule = (struct nft_rule_dp *)data; |
8596 | data += offsetof(struct nft_rule_dp, data); | |
8597 | if (WARN_ON_ONCE(data > data_boundary)) | |
8598 | return -ENOMEM; | |
8599 | ||
8600 | nft_last_rule(chain->blob_next, prule); | |
8601 | ||
0cbc06b3 FW |
8602 | return 0; |
8603 | } | |
8604 | ||
8605 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
8606 | { | |
d59d2f82 | 8607 | struct nftables_pernet *nft_net = nft_pernet(net); |
0cbc06b3 FW |
8608 | struct nft_trans *trans, *next; |
8609 | ||
0854db2a | 8610 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 FW |
8611 | struct nft_chain *chain = trans->ctx.chain; |
8612 | ||
8613 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
8614 | trans->msg_type == NFT_MSG_DELRULE) { | |
2c865a8a PNA |
8615 | kvfree(chain->blob_next); |
8616 | chain->blob_next = NULL; | |
0cbc06b3 FW |
8617 | } |
8618 | } | |
8619 | } | |
8620 | ||
8621 | static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h) | |
8622 | { | |
8623 | struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); | |
8624 | ||
2c865a8a | 8625 | kvfree(o->blob); |
0cbc06b3 FW |
8626 | } |
8627 | ||
2c865a8a | 8628 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob) |
0cbc06b3 | 8629 | { |
0cbc06b3 FW |
8630 | struct nft_rules_old *old; |
8631 | ||
2c865a8a PNA |
8632 | /* rcu_head is after end marker */ |
8633 | old = (void *)blob + sizeof(*blob) + blob->size; | |
8634 | old->blob = blob; | |
0cbc06b3 FW |
8635 | |
8636 | call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old); | |
8637 | } | |
8638 | ||
0fb39bbe | 8639 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 | 8640 | { |
2c865a8a | 8641 | struct nft_rule_blob *g0, *g1; |
0cbc06b3 FW |
8642 | bool next_genbit; |
8643 | ||
8644 | next_genbit = nft_gencursor_next(net); | |
8645 | ||
2c865a8a | 8646 | g0 = rcu_dereference_protected(chain->blob_gen_0, |
f102d66b | 8647 | lockdep_commit_lock_is_held(net)); |
2c865a8a | 8648 | g1 = rcu_dereference_protected(chain->blob_gen_1, |
f102d66b | 8649 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
8650 | |
8651 | /* No changes to this chain? */ | |
2c865a8a | 8652 | if (chain->blob_next == NULL) { |
0cbc06b3 FW |
8653 | /* chain had no change in last or next generation */ |
8654 | if (g0 == g1) | |
8655 | return; | |
8656 | /* | |
8657 | * chain had no change in this generation; make sure next | |
8658 | * one uses same rules as current generation. | |
8659 | */ | |
8660 | if (next_genbit) { | |
2c865a8a | 8661 | rcu_assign_pointer(chain->blob_gen_1, g0); |
0cbc06b3 FW |
8662 | nf_tables_commit_chain_free_rules_old(g1); |
8663 | } else { | |
2c865a8a | 8664 | rcu_assign_pointer(chain->blob_gen_0, g1); |
0cbc06b3 FW |
8665 | nf_tables_commit_chain_free_rules_old(g0); |
8666 | } | |
8667 | ||
8668 | return; | |
8669 | } | |
8670 | ||
8671 | if (next_genbit) | |
2c865a8a | 8672 | rcu_assign_pointer(chain->blob_gen_1, chain->blob_next); |
0cbc06b3 | 8673 | else |
2c865a8a | 8674 | rcu_assign_pointer(chain->blob_gen_0, chain->blob_next); |
0cbc06b3 | 8675 | |
2c865a8a | 8676 | chain->blob_next = NULL; |
0cbc06b3 FW |
8677 | |
8678 | if (g0 == g1) | |
8679 | return; | |
8680 | ||
8681 | if (next_genbit) | |
8682 | nf_tables_commit_chain_free_rules_old(g1); | |
8683 | else | |
8684 | nf_tables_commit_chain_free_rules_old(g0); | |
8685 | } | |
8686 | ||
d152159b FW |
8687 | static void nft_obj_del(struct nft_object *obj) |
8688 | { | |
4d44175a | 8689 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
8690 | list_del_rcu(&obj->list); |
8691 | } | |
8692 | ||
d0e2c7de | 8693 | void nft_chain_del(struct nft_chain *chain) |
1b2470e5 FW |
8694 | { |
8695 | struct nft_table *table = chain->table; | |
8696 | ||
8697 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
8698 | nft_chain_ht_params)); | |
8699 | list_del_rcu(&chain->list); | |
8700 | } | |
8701 | ||
eb014de4 PNA |
8702 | static void nf_tables_module_autoload_cleanup(struct net *net) |
8703 | { | |
d59d2f82 | 8704 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
8705 | struct nft_module_request *req, *next; |
8706 | ||
0854db2a FW |
8707 | WARN_ON_ONCE(!list_empty(&nft_net->commit_list)); |
8708 | list_for_each_entry_safe(req, next, &nft_net->module_list, list) { | |
eb014de4 PNA |
8709 | WARN_ON_ONCE(!req->done); |
8710 | list_del(&req->list); | |
8711 | kfree(req); | |
8712 | } | |
8713 | } | |
8714 | ||
0935d558 FW |
8715 | static void nf_tables_commit_release(struct net *net) |
8716 | { | |
d59d2f82 | 8717 | struct nftables_pernet *nft_net = nft_pernet(net); |
0935d558 FW |
8718 | struct nft_trans *trans; |
8719 | ||
8720 | /* all side effects have to be made visible. | |
8721 | * For example, if a chain named 'foo' has been deleted, a | |
8722 | * new transaction must not find it anymore. | |
8723 | * | |
8724 | * Memory reclaim happens asynchronously from work queue | |
8725 | * to prevent expensive synchronize_rcu() in commit phase. | |
8726 | */ | |
0854db2a | 8727 | if (list_empty(&nft_net->commit_list)) { |
eb014de4 | 8728 | nf_tables_module_autoload_cleanup(net); |
0854db2a | 8729 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
8730 | return; |
8731 | } | |
8732 | ||
0854db2a | 8733 | trans = list_last_entry(&nft_net->commit_list, |
0935d558 FW |
8734 | struct nft_trans, list); |
8735 | get_net(trans->ctx.net); | |
8736 | WARN_ON_ONCE(trans->put_net); | |
8737 | ||
8738 | trans->put_net = true; | |
8739 | spin_lock(&nf_tables_destroy_list_lock); | |
0854db2a | 8740 | list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list); |
0935d558 FW |
8741 | spin_unlock(&nf_tables_destroy_list_lock); |
8742 | ||
eb014de4 | 8743 | nf_tables_module_autoload_cleanup(net); |
0935d558 | 8744 | schedule_work(&trans_destroy_work); |
ffe8923f | 8745 | |
0854db2a | 8746 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
8747 | } |
8748 | ||
67cc570e PNA |
8749 | static void nft_commit_notify(struct net *net, u32 portid) |
8750 | { | |
d59d2f82 | 8751 | struct nftables_pernet *nft_net = nft_pernet(net); |
67cc570e PNA |
8752 | struct sk_buff *batch_skb = NULL, *nskb, *skb; |
8753 | unsigned char *data; | |
8754 | int len; | |
8755 | ||
0854db2a | 8756 | list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) { |
67cc570e PNA |
8757 | if (!batch_skb) { |
8758 | new_batch: | |
8759 | batch_skb = skb; | |
8760 | len = NLMSG_GOODSIZE - skb->len; | |
8761 | list_del(&skb->list); | |
8762 | continue; | |
8763 | } | |
8764 | len -= skb->len; | |
8765 | if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) { | |
8766 | data = skb_put(batch_skb, skb->len); | |
8767 | memcpy(data, skb->data, skb->len); | |
8768 | list_del(&skb->list); | |
8769 | kfree_skb(skb); | |
8770 | continue; | |
8771 | } | |
8772 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
8773 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
8774 | goto new_batch; | |
8775 | } | |
8776 | ||
8777 | if (batch_skb) { | |
8778 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
8779 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
8780 | } | |
8781 | ||
0854db2a | 8782 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); |
67cc570e PNA |
8783 | } |
8784 | ||
c520292f RGB |
8785 | static int nf_tables_commit_audit_alloc(struct list_head *adl, |
8786 | struct nft_table *table) | |
8787 | { | |
8788 | struct nft_audit_data *adp; | |
8789 | ||
8790 | list_for_each_entry(adp, adl, list) { | |
8791 | if (adp->table == table) | |
8792 | return 0; | |
8793 | } | |
8794 | adp = kzalloc(sizeof(*adp), GFP_KERNEL); | |
8795 | if (!adp) | |
8796 | return -ENOMEM; | |
8797 | adp->table = table; | |
8798 | list_add(&adp->list, adl); | |
8799 | return 0; | |
8800 | } | |
8801 | ||
cfbe3650 DM |
8802 | static void nf_tables_commit_audit_free(struct list_head *adl) |
8803 | { | |
8804 | struct nft_audit_data *adp, *adn; | |
8805 | ||
8806 | list_for_each_entry_safe(adp, adn, adl, list) { | |
8807 | list_del(&adp->list); | |
8808 | kfree(adp); | |
8809 | } | |
8810 | } | |
8811 | ||
c520292f RGB |
8812 | static void nf_tables_commit_audit_collect(struct list_head *adl, |
8813 | struct nft_table *table, u32 op) | |
8814 | { | |
8815 | struct nft_audit_data *adp; | |
8816 | ||
8817 | list_for_each_entry(adp, adl, list) { | |
8818 | if (adp->table == table) | |
8819 | goto found; | |
8820 | } | |
dadf33c9 | 8821 | WARN_ONCE(1, "table=%s not expected in commit list", table->name); |
c520292f RGB |
8822 | return; |
8823 | found: | |
8824 | adp->entries++; | |
8825 | if (!adp->op || adp->op > op) | |
8826 | adp->op = op; | |
8827 | } | |
8828 | ||
8829 | #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22) | |
8830 | ||
8831 | static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation) | |
8832 | { | |
8833 | struct nft_audit_data *adp, *adn; | |
8834 | char aubuf[AUNFTABLENAMELEN]; | |
8835 | ||
8836 | list_for_each_entry_safe(adp, adn, adl, list) { | |
8837 | snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name, | |
8838 | generation); | |
8839 | audit_log_nfcfg(aubuf, adp->table->family, adp->entries, | |
8840 | nft2audit_op[adp->op], GFP_KERNEL); | |
8841 | list_del(&adp->list); | |
8842 | kfree(adp); | |
8843 | } | |
8844 | } | |
8845 | ||
5913beaf | 8846 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 8847 | { |
d59d2f82 | 8848 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 8849 | struct nft_trans *trans, *next; |
a3716e70 | 8850 | struct nft_trans_elem *te; |
0cbc06b3 FW |
8851 | struct nft_chain *chain; |
8852 | struct nft_table *table; | |
34002783 | 8853 | unsigned int base_seq; |
c520292f | 8854 | LIST_HEAD(adl); |
c9626a2c | 8855 | int err; |
37082f93 | 8856 | |
0854db2a FW |
8857 | if (list_empty(&nft_net->commit_list)) { |
8858 | mutex_unlock(&nft_net->commit_mutex); | |
b8b27498 FW |
8859 | return 0; |
8860 | } | |
8861 | ||
a654de8f PNA |
8862 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
8863 | if (nf_tables_validate(net) < 0) | |
8864 | return -EAGAIN; | |
8865 | ||
c9626a2c PNA |
8866 | err = nft_flow_rule_offload_commit(net); |
8867 | if (err < 0) | |
8868 | return err; | |
8869 | ||
0cbc06b3 | 8870 | /* 1. Allocate space for next generation rules_gen_X[] */ |
0854db2a | 8871 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 | 8872 | int ret; |
37082f93 | 8873 | |
c520292f RGB |
8874 | ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table); |
8875 | if (ret) { | |
8876 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 8877 | nf_tables_commit_audit_free(&adl); |
c520292f RGB |
8878 | return ret; |
8879 | } | |
0cbc06b3 FW |
8880 | if (trans->msg_type == NFT_MSG_NEWRULE || |
8881 | trans->msg_type == NFT_MSG_DELRULE) { | |
8882 | chain = trans->ctx.chain; | |
8883 | ||
8884 | ret = nf_tables_commit_chain_prepare(net, chain); | |
8885 | if (ret < 0) { | |
8886 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 8887 | nf_tables_commit_audit_free(&adl); |
0cbc06b3 FW |
8888 | return ret; |
8889 | } | |
8890 | } | |
8891 | } | |
37082f93 | 8892 | |
0cbc06b3 | 8893 | /* step 2. Make rules_gen_X visible to packet path */ |
0854db2a | 8894 | list_for_each_entry(table, &nft_net->tables, list) { |
0fb39bbe FW |
8895 | list_for_each_entry(chain, &table->chains, list) |
8896 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
8897 | } |
8898 | ||
8899 | /* | |
8900 | * Bump generation counter, invalidate any dump in progress. | |
8901 | * Cannot fail after this point. | |
37082f93 | 8902 | */ |
34002783 PNA |
8903 | base_seq = READ_ONCE(nft_net->base_seq); |
8904 | while (++base_seq == 0) | |
0854db2a | 8905 | ; |
0cbc06b3 | 8906 | |
34002783 PNA |
8907 | WRITE_ONCE(nft_net->base_seq, base_seq); |
8908 | ||
0cbc06b3 FW |
8909 | /* step 3. Start new generation, rules_gen_X now in use. */ |
8910 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 | 8911 | |
0854db2a | 8912 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
c520292f RGB |
8913 | nf_tables_commit_audit_collect(&adl, trans->ctx.table, |
8914 | trans->msg_type); | |
b380e5c7 | 8915 | switch (trans->msg_type) { |
55dd6f93 PNA |
8916 | case NFT_MSG_NEWTABLE: |
8917 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
8918 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
8919 | nft_trans_destroy(trans); | |
8920 | break; | |
8921 | } | |
8922 | if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT) | |
0ce7cf41 PNA |
8923 | nf_tables_table_disable(net, trans->ctx.table); |
8924 | ||
179d9ba5 | 8925 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; |
55dd6f93 | 8926 | } else { |
f2a6d766 | 8927 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 8928 | } |
35151d84 | 8929 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
8930 | nft_trans_destroy(trans); |
8931 | break; | |
8932 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 8933 | list_del_rcu(&trans->ctx.table->list); |
35151d84 | 8934 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
55dd6f93 | 8935 | break; |
91c7b38d | 8936 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 8937 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 8938 | nft_chain_commit_update(trans); |
9f8aac0b FW |
8939 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
8940 | /* trans destroyed after rcu grace period */ | |
8941 | } else { | |
66293c46 | 8942 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 8943 | nft_clear(net, trans->ctx.chain); |
9f8aac0b FW |
8944 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
8945 | nft_trans_destroy(trans); | |
8946 | } | |
91c7b38d PNA |
8947 | break; |
8948 | case NFT_MSG_DELCHAIN: | |
1b2470e5 | 8949 | nft_chain_del(trans->ctx.chain); |
35151d84 | 8950 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
c974a3a3 PNA |
8951 | nf_tables_unregister_hook(trans->ctx.net, |
8952 | trans->ctx.table, | |
8953 | trans->ctx.chain); | |
91c7b38d | 8954 | break; |
b380e5c7 | 8955 | case NFT_MSG_NEWRULE: |
889f7ee7 | 8956 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 8957 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 8958 | nft_trans_rule(trans), |
35151d84 | 8959 | NFT_MSG_NEWRULE); |
9dd732e0 PNA |
8960 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
8961 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
8962 | ||
37082f93 | 8963 | nft_trans_destroy(trans); |
b380e5c7 PNA |
8964 | break; |
8965 | case NFT_MSG_DELRULE: | |
8966 | list_del_rcu(&nft_trans_rule(trans)->list); | |
35151d84 PNA |
8967 | nf_tables_rule_notify(&trans->ctx, |
8968 | nft_trans_rule(trans), | |
8969 | NFT_MSG_DELRULE); | |
f6ac8585 PNA |
8970 | nft_rule_expr_deactivate(&trans->ctx, |
8971 | nft_trans_rule(trans), | |
8972 | NFT_TRANS_COMMIT); | |
b380e5c7 | 8973 | break; |
958bee14 | 8974 | case NFT_MSG_NEWSET: |
37a9cc52 | 8975 | nft_clear(net, nft_trans_set(trans)); |
4fefee57 PNA |
8976 | /* This avoids hitting -EBUSY when deleting the table |
8977 | * from the transaction. | |
8978 | */ | |
408070d6 | 8979 | if (nft_set_is_anonymous(nft_trans_set(trans)) && |
4fefee57 PNA |
8980 | !list_empty(&nft_trans_set(trans)->bindings)) |
8981 | trans->ctx.table->use--; | |
8982 | ||
958bee14 | 8983 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 8984 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
8985 | nft_trans_destroy(trans); |
8986 | break; | |
8987 | case NFT_MSG_DELSET: | |
37a9cc52 | 8988 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 8989 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 8990 | NFT_MSG_DELSET, GFP_KERNEL); |
958bee14 | 8991 | break; |
60319eb1 | 8992 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
8993 | te = (struct nft_trans_elem *)trans->data; |
8994 | ||
aaa31047 | 8995 | nft_setelem_activate(net, te->set, &te->elem); |
cc02e457 PM |
8996 | nf_tables_setelem_notify(&trans->ctx, te->set, |
8997 | &te->elem, | |
6fb721cf | 8998 | NFT_MSG_NEWSETELEM); |
60319eb1 PNA |
8999 | nft_trans_destroy(trans); |
9000 | break; | |
9001 | case NFT_MSG_DELSETELEM: | |
a3716e70 | 9002 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 9003 | |
a3716e70 PNA |
9004 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9005 | &te->elem, | |
6fb721cf | 9006 | NFT_MSG_DELSETELEM); |
aaa31047 PNA |
9007 | nft_setelem_remove(net, te->set, &te->elem); |
9008 | if (!nft_setelem_is_catchall(te->set, &te->elem)) { | |
9009 | atomic_dec(&te->set->nelems); | |
9010 | te->set->ndeact--; | |
9011 | } | |
60319eb1 | 9012 | break; |
e5009240 | 9013 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
9014 | if (nft_trans_obj_update(trans)) { |
9015 | nft_obj_commit_update(trans); | |
9016 | nf_tables_obj_notify(&trans->ctx, | |
9017 | nft_trans_obj(trans), | |
9018 | NFT_MSG_NEWOBJ); | |
9019 | } else { | |
9020 | nft_clear(net, nft_trans_obj(trans)); | |
9021 | nf_tables_obj_notify(&trans->ctx, | |
9022 | nft_trans_obj(trans), | |
9023 | NFT_MSG_NEWOBJ); | |
9024 | nft_trans_destroy(trans); | |
9025 | } | |
e5009240 PNA |
9026 | break; |
9027 | case NFT_MSG_DELOBJ: | |
d152159b | 9028 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 PNA |
9029 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
9030 | NFT_MSG_DELOBJ); | |
9031 | break; | |
3b49e2e9 | 9032 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 9033 | if (nft_trans_flowtable_update(trans)) { |
7b35582c PNA |
9034 | nft_trans_flowtable(trans)->data.flags = |
9035 | nft_trans_flowtable_flags(trans); | |
78d9f48f PNA |
9036 | nf_tables_flowtable_notify(&trans->ctx, |
9037 | nft_trans_flowtable(trans), | |
9038 | &nft_trans_flowtable_hooks(trans), | |
9039 | NFT_MSG_NEWFLOWTABLE); | |
9040 | list_splice(&nft_trans_flowtable_hooks(trans), | |
9041 | &nft_trans_flowtable(trans)->hook_list); | |
9042 | } else { | |
9043 | nft_clear(net, nft_trans_flowtable(trans)); | |
9044 | nf_tables_flowtable_notify(&trans->ctx, | |
9045 | nft_trans_flowtable(trans), | |
9046 | &nft_trans_flowtable(trans)->hook_list, | |
9047 | NFT_MSG_NEWFLOWTABLE); | |
9048 | } | |
3b49e2e9 PNA |
9049 | nft_trans_destroy(trans); |
9050 | break; | |
9051 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 | 9052 | if (nft_trans_flowtable_update(trans)) { |
abadb2f8 PNA |
9053 | nf_tables_flowtable_notify(&trans->ctx, |
9054 | nft_trans_flowtable(trans), | |
9055 | &nft_trans_flowtable_hooks(trans), | |
9056 | NFT_MSG_DELFLOWTABLE); | |
9057 | nft_unregister_flowtable_net_hooks(net, | |
9058 | &nft_trans_flowtable_hooks(trans)); | |
9059 | } else { | |
9060 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9061 | nf_tables_flowtable_notify(&trans->ctx, | |
9062 | nft_trans_flowtable(trans), | |
9063 | &nft_trans_flowtable(trans)->hook_list, | |
9064 | NFT_MSG_DELFLOWTABLE); | |
9065 | nft_unregister_flowtable_net_hooks(net, | |
9066 | &nft_trans_flowtable(trans)->hook_list); | |
9067 | } | |
3b49e2e9 | 9068 | break; |
37082f93 | 9069 | } |
37082f93 PNA |
9070 | } |
9071 | ||
67cc570e | 9072 | nft_commit_notify(net, NETLINK_CB(skb).portid); |
84d7fce6 | 9073 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0854db2a | 9074 | nf_tables_commit_audit_log(&adl, nft_net->base_seq); |
0935d558 | 9075 | nf_tables_commit_release(net); |
37082f93 PNA |
9076 | |
9077 | return 0; | |
9078 | } | |
9079 | ||
eb014de4 PNA |
9080 | static void nf_tables_module_autoload(struct net *net) |
9081 | { | |
d59d2f82 | 9082 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
9083 | struct nft_module_request *req, *next; |
9084 | LIST_HEAD(module_list); | |
9085 | ||
0854db2a FW |
9086 | list_splice_init(&nft_net->module_list, &module_list); |
9087 | mutex_unlock(&nft_net->commit_mutex); | |
eb014de4 | 9088 | list_for_each_entry_safe(req, next, &module_list, list) { |
1d305ba4 FW |
9089 | request_module("%s", req->module); |
9090 | req->done = true; | |
eb014de4 | 9091 | } |
0854db2a FW |
9092 | mutex_lock(&nft_net->commit_mutex); |
9093 | list_splice(&module_list, &nft_net->module_list); | |
eb014de4 PNA |
9094 | } |
9095 | ||
b326dd37 | 9096 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 9097 | { |
c7c32e72 PNA |
9098 | switch (trans->msg_type) { |
9099 | case NFT_MSG_NEWTABLE: | |
9100 | nf_tables_table_destroy(&trans->ctx); | |
9101 | break; | |
9102 | case NFT_MSG_NEWCHAIN: | |
43a605f2 | 9103 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
9104 | break; |
9105 | case NFT_MSG_NEWRULE: | |
9106 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
9107 | break; | |
9108 | case NFT_MSG_NEWSET: | |
0c2a85ed | 9109 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 9110 | break; |
61edafbb PM |
9111 | case NFT_MSG_NEWSETELEM: |
9112 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 9113 | nft_trans_elem(trans).priv, true); |
61edafbb | 9114 | break; |
e5009240 | 9115 | case NFT_MSG_NEWOBJ: |
00bfb320 | 9116 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 9117 | break; |
3b49e2e9 | 9118 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9119 | if (nft_trans_flowtable_update(trans)) |
9120 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
9121 | else | |
9122 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 9123 | break; |
c7c32e72 PNA |
9124 | } |
9125 | kfree(trans); | |
9126 | } | |
9127 | ||
c0391b6a | 9128 | static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) |
37082f93 | 9129 | { |
d59d2f82 | 9130 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 9131 | struct nft_trans *trans, *next; |
02263db0 | 9132 | struct nft_trans_elem *te; |
37082f93 | 9133 | |
c0391b6a PNA |
9134 | if (action == NFNL_ABORT_VALIDATE && |
9135 | nf_tables_validate(net) < 0) | |
9136 | return -EAGAIN; | |
9137 | ||
0854db2a | 9138 | list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list, |
a907e36d | 9139 | list) { |
b380e5c7 | 9140 | switch (trans->msg_type) { |
55dd6f93 PNA |
9141 | case NFT_MSG_NEWTABLE: |
9142 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
9143 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
9144 | nft_trans_destroy(trans); | |
9145 | break; | |
9146 | } | |
9147 | if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) { | |
0ce7cf41 | 9148 | nf_tables_table_disable(net, trans->ctx.table); |
179d9ba5 PNA |
9149 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
9150 | } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) { | |
9151 | trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT; | |
9152 | } | |
9153 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; | |
55dd6f93 PNA |
9154 | nft_trans_destroy(trans); |
9155 | } else { | |
e688a7f8 | 9156 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
9157 | } |
9158 | break; | |
9159 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 9160 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
9161 | nft_trans_destroy(trans); |
9162 | break; | |
91c7b38d PNA |
9163 | case NFT_MSG_NEWCHAIN: |
9164 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 9165 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 9166 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
9167 | nft_trans_destroy(trans); |
9168 | } else { | |
d0e2c7de PNA |
9169 | if (nft_chain_is_bound(trans->ctx.chain)) { |
9170 | nft_trans_destroy(trans); | |
9171 | break; | |
9172 | } | |
4fefee57 | 9173 | trans->ctx.table->use--; |
1b2470e5 | 9174 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
9175 | nf_tables_unregister_hook(trans->ctx.net, |
9176 | trans->ctx.table, | |
9177 | trans->ctx.chain); | |
91c7b38d PNA |
9178 | } |
9179 | break; | |
9180 | case NFT_MSG_DELCHAIN: | |
4fefee57 | 9181 | trans->ctx.table->use++; |
664b0f8c | 9182 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
9183 | nft_trans_destroy(trans); |
9184 | break; | |
b380e5c7 | 9185 | case NFT_MSG_NEWRULE: |
4fefee57 | 9186 | trans->ctx.chain->use--; |
b380e5c7 | 9187 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
9188 | nft_rule_expr_deactivate(&trans->ctx, |
9189 | nft_trans_rule(trans), | |
9190 | NFT_TRANS_ABORT); | |
3c5e4462 PNA |
9191 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9192 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 PNA |
9193 | break; |
9194 | case NFT_MSG_DELRULE: | |
4fefee57 | 9195 | trans->ctx.chain->use++; |
889f7ee7 | 9196 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 9197 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
3c5e4462 PNA |
9198 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9199 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9200 | ||
37082f93 | 9201 | nft_trans_destroy(trans); |
b380e5c7 | 9202 | break; |
958bee14 | 9203 | case NFT_MSG_NEWSET: |
4fefee57 | 9204 | trans->ctx.table->use--; |
6a0a8d10 | 9205 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
9206 | nft_trans_destroy(trans); |
9207 | break; | |
9208 | } | |
9209 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
9210 | break; |
9211 | case NFT_MSG_DELSET: | |
4fefee57 | 9212 | trans->ctx.table->use++; |
37a9cc52 | 9213 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
9214 | nft_trans_destroy(trans); |
9215 | break; | |
60319eb1 | 9216 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 9217 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
9218 | nft_trans_destroy(trans); |
9219 | break; | |
9220 | } | |
02263db0 | 9221 | te = (struct nft_trans_elem *)trans->data; |
aaa31047 PNA |
9222 | nft_setelem_remove(net, te->set, &te->elem); |
9223 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9224 | atomic_dec(&te->set->nelems); | |
60319eb1 PNA |
9225 | break; |
9226 | case NFT_MSG_DELSETELEM: | |
cc02e457 PM |
9227 | te = (struct nft_trans_elem *)trans->data; |
9228 | ||
f8bb7889 | 9229 | nft_setelem_data_activate(net, te->set, &te->elem); |
aaa31047 PNA |
9230 | nft_setelem_activate(net, te->set, &te->elem); |
9231 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9232 | te->set->ndeact--; | |
cc02e457 | 9233 | |
e5009240 PNA |
9234 | nft_trans_destroy(trans); |
9235 | break; | |
9236 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 | 9237 | if (nft_trans_obj_update(trans)) { |
dad3bdee | 9238 | nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans)); |
d62d0ba9 FFM |
9239 | nft_trans_destroy(trans); |
9240 | } else { | |
9241 | trans->ctx.table->use--; | |
9242 | nft_obj_del(nft_trans_obj(trans)); | |
9243 | } | |
e5009240 PNA |
9244 | break; |
9245 | case NFT_MSG_DELOBJ: | |
9246 | trans->ctx.table->use++; | |
9247 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
9248 | nft_trans_destroy(trans); |
9249 | break; | |
3b49e2e9 | 9250 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9251 | if (nft_trans_flowtable_update(trans)) { |
9252 | nft_unregister_flowtable_net_hooks(net, | |
9253 | &nft_trans_flowtable_hooks(trans)); | |
9254 | } else { | |
9255 | trans->ctx.table->use--; | |
9256 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9257 | nft_unregister_flowtable_net_hooks(net, | |
9258 | &nft_trans_flowtable(trans)->hook_list); | |
9259 | } | |
3b49e2e9 PNA |
9260 | break; |
9261 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 | 9262 | if (nft_trans_flowtable_update(trans)) { |
b6d9014a PNA |
9263 | list_splice(&nft_trans_flowtable_hooks(trans), |
9264 | &nft_trans_flowtable(trans)->hook_list); | |
abadb2f8 PNA |
9265 | } else { |
9266 | trans->ctx.table->use++; | |
9267 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
9268 | } | |
3b49e2e9 PNA |
9269 | nft_trans_destroy(trans); |
9270 | break; | |
37082f93 | 9271 | } |
37082f93 PNA |
9272 | } |
9273 | ||
b326dd37 PNA |
9274 | synchronize_rcu(); |
9275 | ||
a1cee076 | 9276 | list_for_each_entry_safe_reverse(trans, next, |
0854db2a | 9277 | &nft_net->commit_list, list) { |
c7c32e72 | 9278 | list_del(&trans->list); |
b326dd37 | 9279 | nf_tables_abort_release(trans); |
37082f93 PNA |
9280 | } |
9281 | ||
c0391b6a | 9282 | if (action == NFNL_ABORT_AUTOLOAD) |
eb014de4 PNA |
9283 | nf_tables_module_autoload(net); |
9284 | else | |
9285 | nf_tables_module_autoload_cleanup(net); | |
9286 | ||
37082f93 PNA |
9287 | return 0; |
9288 | } | |
9289 | ||
a654de8f PNA |
9290 | static void nf_tables_cleanup(struct net *net) |
9291 | { | |
9292 | nft_validate_state_update(net, NFT_VALIDATE_SKIP); | |
9293 | } | |
9294 | ||
c0391b6a PNA |
9295 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, |
9296 | enum nfnl_abort_action action) | |
71ad00c5 | 9297 | { |
d59d2f82 | 9298 | struct nftables_pernet *nft_net = nft_pernet(net); |
c0391b6a | 9299 | int ret = __nf_tables_abort(net, action); |
f102d66b | 9300 | |
0854db2a | 9301 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9302 | |
9303 | return ret; | |
71ad00c5 FW |
9304 | } |
9305 | ||
74e8bcd2 PNA |
9306 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
9307 | { | |
d59d2f82 | 9308 | struct nftables_pernet *nft_net = nft_pernet(net); |
f102d66b FW |
9309 | bool genid_ok; |
9310 | ||
0854db2a | 9311 | mutex_lock(&nft_net->commit_mutex); |
f102d66b | 9312 | |
0854db2a | 9313 | genid_ok = genid == 0 || nft_net->base_seq == genid; |
f102d66b | 9314 | if (!genid_ok) |
0854db2a | 9315 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9316 | |
9317 | /* else, commit mutex has to be released by commit or abort function */ | |
9318 | return genid_ok; | |
74e8bcd2 PNA |
9319 | } |
9320 | ||
96518518 PM |
9321 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
9322 | .name = "nf_tables", | |
9323 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
9324 | .cb_count = NFT_MSG_MAX, | |
9325 | .cb = nf_tables_cb, | |
0628b123 PNA |
9326 | .commit = nf_tables_commit, |
9327 | .abort = nf_tables_abort, | |
a654de8f | 9328 | .cleanup = nf_tables_cleanup, |
74e8bcd2 | 9329 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 9330 | .owner = THIS_MODULE, |
96518518 PM |
9331 | }; |
9332 | ||
7210e4e3 | 9333 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 9334 | enum nft_chain_types type) |
7210e4e3 PNA |
9335 | { |
9336 | const struct nft_base_chain *basechain; | |
9337 | ||
f323d954 | 9338 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
9339 | basechain = nft_base_chain(chain); |
9340 | if (basechain->type->type != type) | |
9341 | return -EOPNOTSUPP; | |
9342 | } | |
9343 | return 0; | |
9344 | } | |
9345 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
9346 | ||
75e8d06d PNA |
9347 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
9348 | unsigned int hook_flags) | |
9349 | { | |
9350 | struct nft_base_chain *basechain; | |
9351 | ||
f323d954 | 9352 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
9353 | basechain = nft_base_chain(chain); |
9354 | ||
c974a3a3 | 9355 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
9356 | return 0; |
9357 | ||
9358 | return -EOPNOTSUPP; | |
9359 | } | |
9360 | ||
9361 | return 0; | |
9362 | } | |
9363 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
9364 | ||
20a69341 PM |
9365 | /* |
9366 | * Loop detection - walk through the ruleset beginning at the destination chain | |
9367 | * of a new jump until either the source chain is reached (loop) or all | |
9368 | * reachable chains have been traversed. | |
9369 | * | |
9370 | * The loop check is performed whenever a new jump verdict is added to an | |
9371 | * expression or verdict map or a verdict map is bound to a new chain. | |
9372 | */ | |
9373 | ||
9374 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
9375 | const struct nft_chain *chain); | |
9376 | ||
6387aa6e PNA |
9377 | static int nft_check_loops(const struct nft_ctx *ctx, |
9378 | const struct nft_set_ext *ext) | |
9379 | { | |
9380 | const struct nft_data *data; | |
9381 | int ret; | |
9382 | ||
9383 | data = nft_set_ext_data(ext); | |
9384 | switch (data->verdict.code) { | |
9385 | case NFT_JUMP: | |
9386 | case NFT_GOTO: | |
9387 | ret = nf_tables_check_loops(ctx, data->verdict.chain); | |
9388 | break; | |
9389 | default: | |
9390 | ret = 0; | |
9391 | break; | |
9392 | } | |
9393 | ||
9394 | return ret; | |
9395 | } | |
9396 | ||
20a69341 | 9397 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, |
de70185d | 9398 | struct nft_set *set, |
20a69341 | 9399 | const struct nft_set_iter *iter, |
de70185d | 9400 | struct nft_set_elem *elem) |
20a69341 | 9401 | { |
fe2811eb | 9402 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
fe2811eb PM |
9403 | |
9404 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
9405 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
9406 | return 0; |
9407 | ||
6387aa6e | 9408 | return nft_check_loops(ctx, ext); |
20a69341 PM |
9409 | } |
9410 | ||
aaa31047 PNA |
9411 | static int nft_set_catchall_loops(const struct nft_ctx *ctx, |
9412 | struct nft_set *set) | |
9413 | { | |
9414 | u8 genmask = nft_genmask_next(ctx->net); | |
9415 | struct nft_set_elem_catchall *catchall; | |
9416 | struct nft_set_ext *ext; | |
9417 | int ret = 0; | |
9418 | ||
9419 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
9420 | ext = nft_set_elem_ext(set, catchall->elem); | |
9421 | if (!nft_set_elem_active(ext, genmask)) | |
9422 | continue; | |
9423 | ||
9424 | ret = nft_check_loops(ctx, ext); | |
9425 | if (ret < 0) | |
9426 | return ret; | |
9427 | } | |
9428 | ||
9429 | return ret; | |
9430 | } | |
9431 | ||
20a69341 PM |
9432 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
9433 | const struct nft_chain *chain) | |
9434 | { | |
9435 | const struct nft_rule *rule; | |
9436 | const struct nft_expr *expr, *last; | |
de70185d | 9437 | struct nft_set *set; |
20a69341 PM |
9438 | struct nft_set_binding *binding; |
9439 | struct nft_set_iter iter; | |
20a69341 PM |
9440 | |
9441 | if (ctx->chain == chain) | |
9442 | return -ELOOP; | |
9443 | ||
9444 | list_for_each_entry(rule, &chain->rules, list) { | |
9445 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
9446 | struct nft_immediate_expr *priv; |
9447 | const struct nft_data *data; | |
0ca743a5 PNA |
9448 | int err; |
9449 | ||
a654de8f | 9450 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
9451 | continue; |
9452 | ||
a654de8f PNA |
9453 | priv = nft_expr_priv(expr); |
9454 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 9455 | continue; |
20a69341 | 9456 | |
a654de8f | 9457 | data = &priv->data; |
1ca2e170 | 9458 | switch (data->verdict.code) { |
20a69341 PM |
9459 | case NFT_JUMP: |
9460 | case NFT_GOTO: | |
1ca2e170 PM |
9461 | err = nf_tables_check_loops(ctx, |
9462 | data->verdict.chain); | |
20a69341 PM |
9463 | if (err < 0) |
9464 | return err; | |
c2168e6b | 9465 | break; |
20a69341 PM |
9466 | default: |
9467 | break; | |
9468 | } | |
9469 | } | |
9470 | } | |
9471 | ||
9472 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
9473 | if (!nft_is_active_next(ctx->net, set)) |
9474 | continue; | |
20a69341 PM |
9475 | if (!(set->flags & NFT_SET_MAP) || |
9476 | set->dtype != NFT_DATA_VERDICT) | |
9477 | continue; | |
9478 | ||
9479 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
9480 | if (!(binding->flags & NFT_SET_MAP) || |
9481 | binding->chain != chain) | |
20a69341 PM |
9482 | continue; |
9483 | ||
8588ac09 | 9484 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
9485 | iter.skip = 0; |
9486 | iter.count = 0; | |
9487 | iter.err = 0; | |
9488 | iter.fn = nf_tables_loop_check_setelem; | |
9489 | ||
9490 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
9491 | if (!iter.err) |
9492 | iter.err = nft_set_catchall_loops(ctx, set); | |
9493 | ||
20a69341 PM |
9494 | if (iter.err < 0) |
9495 | return iter.err; | |
9496 | } | |
9497 | } | |
9498 | ||
9499 | return 0; | |
9500 | } | |
9501 | ||
36b701fa LGL |
9502 | /** |
9503 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
9504 | * | |
9505 | * @attr: netlink attribute to fetch value from | |
9506 | * @max: maximum value to be stored in dest | |
9507 | * @dest: pointer to the variable | |
9508 | * | |
9509 | * Parse, check and store a given u32 netlink attribute into variable. | |
9510 | * This function returns -ERANGE if the value goes over maximum value. | |
9511 | * Otherwise a 0 is returned and the attribute value is stored in the | |
9512 | * destination variable. | |
9513 | */ | |
f1d505bb | 9514 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 9515 | { |
09525a09 | 9516 | u32 val; |
36b701fa LGL |
9517 | |
9518 | val = ntohl(nla_get_be32(attr)); | |
9519 | if (val > max) | |
9520 | return -ERANGE; | |
9521 | ||
9522 | *dest = val; | |
9523 | return 0; | |
9524 | } | |
9525 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
9526 | ||
6c6f9f31 | 9527 | static int nft_parse_register(const struct nlattr *attr, u32 *preg) |
b1c96ed3 | 9528 | { |
49499c3e PM |
9529 | unsigned int reg; |
9530 | ||
9531 | reg = ntohl(nla_get_be32(attr)); | |
9532 | switch (reg) { | |
9533 | case NFT_REG_VERDICT...NFT_REG_4: | |
6e1acfa3 PNA |
9534 | *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE; |
9535 | break; | |
9536 | case NFT_REG32_00...NFT_REG32_15: | |
9537 | *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
9538 | break; | |
49499c3e | 9539 | default: |
6e1acfa3 | 9540 | return -ERANGE; |
49499c3e | 9541 | } |
6e1acfa3 PNA |
9542 | |
9543 | return 0; | |
b1c96ed3 | 9544 | } |
b1c96ed3 | 9545 | |
49499c3e PM |
9546 | /** |
9547 | * nft_dump_register - dump a register value to a netlink attribute | |
9548 | * | |
9549 | * @skb: socket buffer | |
9550 | * @attr: attribute number | |
9551 | * @reg: register number | |
9552 | * | |
9553 | * Construct a netlink attribute containing the register number. For | |
9554 | * compatibility reasons, register numbers being a multiple of 4 are | |
9555 | * translated to the corresponding 128 bit register numbers. | |
9556 | */ | |
b1c96ed3 PM |
9557 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
9558 | { | |
49499c3e PM |
9559 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
9560 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
9561 | else | |
9562 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
9563 | ||
b1c96ed3 PM |
9564 | return nla_put_be32(skb, attr, htonl(reg)); |
9565 | } | |
9566 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
9567 | ||
4f16d25c | 9568 | static int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 9569 | { |
49499c3e | 9570 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 9571 | return -EINVAL; |
d07db988 PM |
9572 | if (len == 0) |
9573 | return -EINVAL; | |
c593642c | 9574 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 9575 | return -ERANGE; |
49499c3e | 9576 | |
96518518 PM |
9577 | return 0; |
9578 | } | |
4f16d25c PNA |
9579 | |
9580 | int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len) | |
9581 | { | |
9582 | u32 reg; | |
9583 | int err; | |
9584 | ||
6e1acfa3 PNA |
9585 | err = nft_parse_register(attr, ®); |
9586 | if (err < 0) | |
9587 | return err; | |
9588 | ||
4f16d25c PNA |
9589 | err = nft_validate_register_load(reg, len); |
9590 | if (err < 0) | |
9591 | return err; | |
9592 | ||
9593 | *sreg = reg; | |
9594 | return 0; | |
9595 | } | |
9596 | EXPORT_SYMBOL_GPL(nft_parse_register_load); | |
96518518 | 9597 | |
345023b0 PNA |
9598 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
9599 | enum nft_registers reg, | |
9600 | const struct nft_data *data, | |
9601 | enum nft_data_types type, | |
9602 | unsigned int len) | |
96518518 | 9603 | { |
20a69341 PM |
9604 | int err; |
9605 | ||
96518518 PM |
9606 | switch (reg) { |
9607 | case NFT_REG_VERDICT: | |
58f40ab6 | 9608 | if (type != NFT_DATA_VERDICT) |
96518518 | 9609 | return -EINVAL; |
20a69341 | 9610 | |
58f40ab6 | 9611 | if (data != NULL && |
1ca2e170 PM |
9612 | (data->verdict.code == NFT_GOTO || |
9613 | data->verdict.code == NFT_JUMP)) { | |
9614 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
9615 | if (err < 0) |
9616 | return err; | |
20a69341 PM |
9617 | } |
9618 | ||
96518518 PM |
9619 | return 0; |
9620 | default: | |
49499c3e | 9621 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 9622 | return -EINVAL; |
45d9bcda PM |
9623 | if (len == 0) |
9624 | return -EINVAL; | |
49499c3e | 9625 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 9626 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 9627 | return -ERANGE; |
27e6d201 | 9628 | |
96518518 PM |
9629 | if (data != NULL && type != NFT_DATA_VALUE) |
9630 | return -EINVAL; | |
9631 | return 0; | |
9632 | } | |
9633 | } | |
345023b0 PNA |
9634 | |
9635 | int nft_parse_register_store(const struct nft_ctx *ctx, | |
9636 | const struct nlattr *attr, u8 *dreg, | |
9637 | const struct nft_data *data, | |
9638 | enum nft_data_types type, unsigned int len) | |
9639 | { | |
9640 | int err; | |
9641 | u32 reg; | |
9642 | ||
6e1acfa3 PNA |
9643 | err = nft_parse_register(attr, ®); |
9644 | if (err < 0) | |
9645 | return err; | |
9646 | ||
345023b0 PNA |
9647 | err = nft_validate_register_store(ctx, reg, data, type, len); |
9648 | if (err < 0) | |
9649 | return err; | |
9650 | ||
9651 | *dreg = reg; | |
9652 | return 0; | |
9653 | } | |
9654 | EXPORT_SYMBOL_GPL(nft_parse_register_store); | |
96518518 PM |
9655 | |
9656 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
9657 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
9658 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
9659 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
51d70f18 | 9660 | [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
9661 | }; |
9662 | ||
9663 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
9664 | struct nft_data_desc *desc, const struct nlattr *nla) | |
9665 | { | |
664b0f8c | 9666 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
9667 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
9668 | struct nft_chain *chain; | |
9669 | int err; | |
9670 | ||
8cb08174 JB |
9671 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
9672 | nft_verdict_policy, NULL); | |
96518518 PM |
9673 | if (err < 0) |
9674 | return err; | |
9675 | ||
9676 | if (!tb[NFTA_VERDICT_CODE]) | |
9677 | return -EINVAL; | |
1ca2e170 | 9678 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 9679 | |
1ca2e170 | 9680 | switch (data->verdict.code) { |
e0abdadc | 9681 | default: |
1ca2e170 | 9682 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
9683 | case NF_ACCEPT: |
9684 | case NF_DROP: | |
9685 | case NF_QUEUE: | |
9686 | break; | |
9687 | default: | |
9688 | return -EINVAL; | |
9689 | } | |
954d8297 | 9690 | fallthrough; |
96518518 PM |
9691 | case NFT_CONTINUE: |
9692 | case NFT_BREAK: | |
9693 | case NFT_RETURN: | |
96518518 PM |
9694 | break; |
9695 | case NFT_JUMP: | |
9696 | case NFT_GOTO: | |
51d70f18 PNA |
9697 | if (tb[NFTA_VERDICT_CHAIN]) { |
9698 | chain = nft_chain_lookup(ctx->net, ctx->table, | |
9699 | tb[NFTA_VERDICT_CHAIN], | |
9700 | genmask); | |
9701 | } else if (tb[NFTA_VERDICT_CHAIN_ID]) { | |
95f466d2 | 9702 | chain = nft_chain_lookup_byid(ctx->net, ctx->table, |
51d70f18 PNA |
9703 | tb[NFTA_VERDICT_CHAIN_ID]); |
9704 | if (IS_ERR(chain)) | |
9705 | return PTR_ERR(chain); | |
9706 | } else { | |
96518518 | 9707 | return -EINVAL; |
51d70f18 PNA |
9708 | } |
9709 | ||
96518518 PM |
9710 | if (IS_ERR(chain)) |
9711 | return PTR_ERR(chain); | |
f323d954 | 9712 | if (nft_is_base_chain(chain)) |
96518518 | 9713 | return -EOPNOTSUPP; |
f323ef3a PNA |
9714 | if (desc->flags & NFT_DATA_DESC_SETELEM && |
9715 | chain->flags & NFT_CHAIN_BINDING) | |
9716 | return -EINVAL; | |
96518518 | 9717 | |
96518518 | 9718 | chain->use++; |
1ca2e170 | 9719 | data->verdict.chain = chain; |
96518518 | 9720 | break; |
96518518 PM |
9721 | } |
9722 | ||
4c4ed074 | 9723 | desc->len = sizeof(data->verdict); |
341b6941 | 9724 | |
96518518 PM |
9725 | return 0; |
9726 | } | |
9727 | ||
9728 | static void nft_verdict_uninit(const struct nft_data *data) | |
9729 | { | |
d0e2c7de PNA |
9730 | struct nft_chain *chain; |
9731 | struct nft_rule *rule; | |
9732 | ||
1ca2e170 | 9733 | switch (data->verdict.code) { |
96518518 PM |
9734 | case NFT_JUMP: |
9735 | case NFT_GOTO: | |
d0e2c7de PNA |
9736 | chain = data->verdict.chain; |
9737 | chain->use--; | |
9738 | ||
9739 | if (!nft_chain_is_bound(chain)) | |
9740 | break; | |
9741 | ||
9742 | chain->table->use--; | |
9743 | list_for_each_entry(rule, &chain->rules, list) | |
9744 | chain->use--; | |
9745 | ||
9746 | nft_chain_del(chain); | |
96518518 PM |
9747 | break; |
9748 | } | |
9749 | } | |
9750 | ||
33d5a7b1 | 9751 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
9752 | { |
9753 | struct nlattr *nest; | |
9754 | ||
ae0be8de | 9755 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
9756 | if (!nest) |
9757 | goto nla_put_failure; | |
9758 | ||
33d5a7b1 | 9759 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
9760 | goto nla_put_failure; |
9761 | ||
33d5a7b1 | 9762 | switch (v->code) { |
96518518 PM |
9763 | case NFT_JUMP: |
9764 | case NFT_GOTO: | |
1ca2e170 | 9765 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 9766 | v->chain->name)) |
96518518 PM |
9767 | goto nla_put_failure; |
9768 | } | |
9769 | nla_nest_end(skb, nest); | |
9770 | return 0; | |
9771 | ||
9772 | nla_put_failure: | |
9773 | return -1; | |
9774 | } | |
9775 | ||
d0a11fc3 | 9776 | static int nft_value_init(const struct nft_ctx *ctx, |
341b6941 PNA |
9777 | struct nft_data *data, struct nft_data_desc *desc, |
9778 | const struct nlattr *nla) | |
96518518 PM |
9779 | { |
9780 | unsigned int len; | |
9781 | ||
9782 | len = nla_len(nla); | |
9783 | if (len == 0) | |
9784 | return -EINVAL; | |
341b6941 | 9785 | if (len > desc->size) |
96518518 | 9786 | return -EOVERFLOW; |
341b6941 PNA |
9787 | if (desc->len) { |
9788 | if (len != desc->len) | |
9789 | return -EINVAL; | |
9790 | } else { | |
9791 | desc->len = len; | |
9792 | } | |
96518518 | 9793 | |
d0a11fc3 | 9794 | nla_memcpy(data->data, nla, len); |
341b6941 | 9795 | |
96518518 PM |
9796 | return 0; |
9797 | } | |
9798 | ||
9799 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
9800 | unsigned int len) | |
9801 | { | |
9802 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
9803 | } | |
9804 | ||
9805 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 9806 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
9807 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
9808 | }; | |
9809 | ||
9810 | /** | |
9811 | * nft_data_init - parse nf_tables data netlink attributes | |
9812 | * | |
9813 | * @ctx: context of the expression using the data | |
9814 | * @data: destination struct nft_data | |
9815 | * @desc: data description | |
9816 | * @nla: netlink attribute containing data | |
9817 | * | |
9818 | * Parse the netlink data attributes and initialize a struct nft_data. | |
9819 | * The type and length of data are returned in the data description. | |
9820 | * | |
9821 | * The caller can indicate that it only wants to accept data of type | |
9822 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
9823 | */ | |
341b6941 | 9824 | int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
96518518 PM |
9825 | struct nft_data_desc *desc, const struct nlattr *nla) |
9826 | { | |
9827 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
9828 | int err; | |
9829 | ||
341b6941 PNA |
9830 | if (WARN_ON_ONCE(!desc->size)) |
9831 | return -EINVAL; | |
9832 | ||
8cb08174 JB |
9833 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
9834 | nft_data_policy, NULL); | |
96518518 PM |
9835 | if (err < 0) |
9836 | return err; | |
9837 | ||
341b6941 PNA |
9838 | if (tb[NFTA_DATA_VALUE]) { |
9839 | if (desc->type != NFT_DATA_VALUE) | |
9840 | return -EINVAL; | |
9841 | ||
9842 | err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]); | |
9843 | } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) { | |
9844 | if (desc->type != NFT_DATA_VERDICT) | |
9845 | return -EINVAL; | |
9846 | ||
9847 | err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
9848 | } else { | |
9849 | err = -EINVAL; | |
9850 | } | |
9851 | ||
9852 | return err; | |
96518518 PM |
9853 | } |
9854 | EXPORT_SYMBOL_GPL(nft_data_init); | |
9855 | ||
9856 | /** | |
59105446 | 9857 | * nft_data_release - release a nft_data item |
96518518 PM |
9858 | * |
9859 | * @data: struct nft_data to release | |
9860 | * @type: type of data | |
9861 | * | |
9862 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
9863 | * all others need to be released by calling this function. | |
9864 | */ | |
59105446 | 9865 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 9866 | { |
960bd2c2 | 9867 | if (type < NFT_DATA_VERDICT) |
96518518 | 9868 | return; |
960bd2c2 | 9869 | switch (type) { |
96518518 PM |
9870 | case NFT_DATA_VERDICT: |
9871 | return nft_verdict_uninit(data); | |
9872 | default: | |
9873 | WARN_ON(1); | |
9874 | } | |
9875 | } | |
59105446 | 9876 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
9877 | |
9878 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
9879 | enum nft_data_types type, unsigned int len) | |
9880 | { | |
9881 | struct nlattr *nest; | |
9882 | int err; | |
9883 | ||
ae0be8de | 9884 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
9885 | if (nest == NULL) |
9886 | return -1; | |
9887 | ||
9888 | switch (type) { | |
9889 | case NFT_DATA_VALUE: | |
9890 | err = nft_value_dump(skb, data, len); | |
9891 | break; | |
9892 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 9893 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
9894 | break; |
9895 | default: | |
9896 | err = -EINVAL; | |
9897 | WARN_ON(1); | |
9898 | } | |
9899 | ||
9900 | nla_nest_end(skb, nest); | |
9901 | return err; | |
9902 | } | |
9903 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
9904 | ||
5ebe0b0e PNA |
9905 | int __nft_release_basechain(struct nft_ctx *ctx) |
9906 | { | |
9907 | struct nft_rule *rule, *nr; | |
9908 | ||
fa5950e4 FW |
9909 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
9910 | return 0; | |
5ebe0b0e | 9911 | |
c974a3a3 | 9912 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
9913 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
9914 | list_del(&rule->list); | |
9915 | ctx->chain->use--; | |
bb7b40ae | 9916 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 9917 | } |
1b2470e5 | 9918 | nft_chain_del(ctx->chain); |
5ebe0b0e | 9919 | ctx->table->use--; |
43a605f2 | 9920 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
9921 | |
9922 | return 0; | |
9923 | } | |
9924 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
9925 | ||
00dfe9be PNA |
9926 | static void __nft_release_hook(struct net *net, struct nft_table *table) |
9927 | { | |
6069da44 | 9928 | struct nft_flowtable *flowtable; |
00dfe9be PNA |
9929 | struct nft_chain *chain; |
9930 | ||
9931 | list_for_each_entry(chain, &table->chains, list) | |
f9a43007 | 9932 | __nf_tables_unregister_hook(net, table, chain, true); |
6069da44 | 9933 | list_for_each_entry(flowtable, &table->flowtables, list) |
f9a43007 PNA |
9934 | __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list, |
9935 | true); | |
00dfe9be PNA |
9936 | } |
9937 | ||
767d1216 PNA |
9938 | static void __nft_release_hooks(struct net *net) |
9939 | { | |
d59d2f82 | 9940 | struct nftables_pernet *nft_net = nft_pernet(net); |
767d1216 | 9941 | struct nft_table *table; |
767d1216 | 9942 | |
0854db2a | 9943 | list_for_each_entry(table, &nft_net->tables, list) { |
2888b080 PNA |
9944 | if (nft_table_has_owner(table)) |
9945 | continue; | |
9946 | ||
00dfe9be | 9947 | __nft_release_hook(net, table); |
2888b080 | 9948 | } |
767d1216 PNA |
9949 | } |
9950 | ||
fd020332 | 9951 | static void __nft_release_table(struct net *net, struct nft_table *table) |
df05ef87 | 9952 | { |
3b49e2e9 | 9953 | struct nft_flowtable *flowtable, *nf; |
df05ef87 | 9954 | struct nft_chain *chain, *nc; |
e5009240 | 9955 | struct nft_object *obj, *ne; |
df05ef87 PNA |
9956 | struct nft_rule *rule, *nr; |
9957 | struct nft_set *set, *ns; | |
9958 | struct nft_ctx ctx = { | |
9959 | .net = net, | |
43a605f2 | 9960 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
9961 | }; |
9962 | ||
fd020332 PNA |
9963 | ctx.family = table->family; |
9964 | ctx.table = table; | |
9965 | list_for_each_entry(chain, &table->chains, list) { | |
9966 | ctx.chain = chain; | |
9967 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
9968 | list_del(&rule->list); | |
9969 | chain->use--; | |
9970 | nf_tables_rule_release(&ctx, rule); | |
df05ef87 | 9971 | } |
df05ef87 | 9972 | } |
fd020332 PNA |
9973 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
9974 | list_del(&flowtable->list); | |
9975 | table->use--; | |
9976 | nf_tables_flowtable_destroy(flowtable); | |
9977 | } | |
9978 | list_for_each_entry_safe(set, ns, &table->sets, list) { | |
9979 | list_del(&set->list); | |
9980 | table->use--; | |
9981 | nft_set_destroy(&ctx, set); | |
9982 | } | |
9983 | list_for_each_entry_safe(obj, ne, &table->objects, list) { | |
9984 | nft_obj_del(obj); | |
9985 | table->use--; | |
9986 | nft_obj_destroy(&ctx, obj); | |
9987 | } | |
9988 | list_for_each_entry_safe(chain, nc, &table->chains, list) { | |
9989 | ctx.chain = chain; | |
9990 | nft_chain_del(chain); | |
9991 | table->use--; | |
9992 | nf_tables_chain_destroy(&ctx); | |
9993 | } | |
fd020332 PNA |
9994 | nf_tables_table_destroy(&ctx); |
9995 | } | |
9996 | ||
bd1777b3 | 9997 | static void __nft_release_tables(struct net *net) |
fd020332 | 9998 | { |
d59d2f82 | 9999 | struct nftables_pernet *nft_net = nft_pernet(net); |
fd020332 PNA |
10000 | struct nft_table *table, *nt; |
10001 | ||
0854db2a | 10002 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
bd1777b3 | 10003 | if (nft_table_has_owner(table)) |
6001a930 PNA |
10004 | continue; |
10005 | ||
a499b03b FW |
10006 | list_del(&table->list); |
10007 | ||
fd020332 | 10008 | __nft_release_table(net, table); |
6001a930 PNA |
10009 | } |
10010 | } | |
10011 | ||
10012 | static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event, | |
10013 | void *ptr) | |
10014 | { | |
a499b03b | 10015 | struct nft_table *table, *to_delete[8]; |
0854db2a | 10016 | struct nftables_pernet *nft_net; |
6001a930 | 10017 | struct netlink_notify *n = ptr; |
6001a930 | 10018 | struct net *net = n->net; |
a499b03b FW |
10019 | unsigned int deleted; |
10020 | bool restart = false; | |
6001a930 PNA |
10021 | |
10022 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER) | |
10023 | return NOTIFY_DONE; | |
10024 | ||
d59d2f82 | 10025 | nft_net = nft_pernet(net); |
a499b03b | 10026 | deleted = 0; |
0854db2a | 10027 | mutex_lock(&nft_net->commit_mutex); |
a499b03b | 10028 | again: |
0854db2a | 10029 | list_for_each_entry(table, &nft_net->tables, list) { |
6001a930 PNA |
10030 | if (nft_table_has_owner(table) && |
10031 | n->portid == table->nlpid) { | |
10032 | __nft_release_hook(net, table); | |
a499b03b FW |
10033 | list_del_rcu(&table->list); |
10034 | to_delete[deleted++] = table; | |
10035 | if (deleted >= ARRAY_SIZE(to_delete)) | |
10036 | break; | |
6001a930 PNA |
10037 | } |
10038 | } | |
a499b03b FW |
10039 | if (deleted) { |
10040 | restart = deleted >= ARRAY_SIZE(to_delete); | |
6001a930 | 10041 | synchronize_rcu(); |
a499b03b FW |
10042 | while (deleted) |
10043 | __nft_release_table(net, to_delete[--deleted]); | |
10044 | ||
10045 | if (restart) | |
10046 | goto again; | |
6001a930 | 10047 | } |
0854db2a | 10048 | mutex_unlock(&nft_net->commit_mutex); |
6001a930 PNA |
10049 | |
10050 | return NOTIFY_DONE; | |
df05ef87 PNA |
10051 | } |
10052 | ||
6001a930 PNA |
10053 | static struct notifier_block nft_nl_notifier = { |
10054 | .notifier_call = nft_rcv_nl_event, | |
10055 | }; | |
10056 | ||
dd4cbef7 PNA |
10057 | static int __net_init nf_tables_init_net(struct net *net) |
10058 | { | |
d59d2f82 | 10059 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10060 | |
10061 | INIT_LIST_HEAD(&nft_net->tables); | |
10062 | INIT_LIST_HEAD(&nft_net->commit_list); | |
10063 | INIT_LIST_HEAD(&nft_net->module_list); | |
10064 | INIT_LIST_HEAD(&nft_net->notify_list); | |
10065 | mutex_init(&nft_net->commit_mutex); | |
10066 | nft_net->base_seq = 1; | |
10067 | nft_net->validate_state = NFT_VALIDATE_SKIP; | |
a654de8f | 10068 | |
dd4cbef7 PNA |
10069 | return 0; |
10070 | } | |
10071 | ||
767d1216 PNA |
10072 | static void __net_exit nf_tables_pre_exit_net(struct net *net) |
10073 | { | |
3923b1e4 PNA |
10074 | struct nftables_pernet *nft_net = nft_pernet(net); |
10075 | ||
10076 | mutex_lock(&nft_net->commit_mutex); | |
767d1216 | 10077 | __nft_release_hooks(net); |
3923b1e4 | 10078 | mutex_unlock(&nft_net->commit_mutex); |
767d1216 PNA |
10079 | } |
10080 | ||
dd4cbef7 PNA |
10081 | static void __net_exit nf_tables_exit_net(struct net *net) |
10082 | { | |
d59d2f82 | 10083 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10084 | |
10085 | mutex_lock(&nft_net->commit_mutex); | |
10086 | if (!list_empty(&nft_net->commit_list)) | |
c0391b6a | 10087 | __nf_tables_abort(net, NFNL_ABORT_NONE); |
bd1777b3 | 10088 | __nft_release_tables(net); |
0854db2a FW |
10089 | mutex_unlock(&nft_net->commit_mutex); |
10090 | WARN_ON_ONCE(!list_empty(&nft_net->tables)); | |
10091 | WARN_ON_ONCE(!list_empty(&nft_net->module_list)); | |
10092 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); | |
dd4cbef7 PNA |
10093 | } |
10094 | ||
99633ab2 | 10095 | static struct pernet_operations nf_tables_net_ops = { |
767d1216 PNA |
10096 | .init = nf_tables_init_net, |
10097 | .pre_exit = nf_tables_pre_exit_net, | |
10098 | .exit = nf_tables_exit_net, | |
0854db2a FW |
10099 | .id = &nf_tables_net_id, |
10100 | .size = sizeof(struct nftables_pernet), | |
99633ab2 PNA |
10101 | }; |
10102 | ||
96518518 PM |
10103 | static int __init nf_tables_module_init(void) |
10104 | { | |
10105 | int err; | |
10106 | ||
d209df3e FW |
10107 | err = register_pernet_subsys(&nf_tables_net_ops); |
10108 | if (err < 0) | |
10109 | return err; | |
10110 | ||
10111 | err = nft_chain_filter_init(); | |
10112 | if (err < 0) | |
6001a930 | 10113 | goto err_chain_filter; |
02c7b25e | 10114 | |
96518518 PM |
10115 | err = nf_tables_core_module_init(); |
10116 | if (err < 0) | |
6001a930 | 10117 | goto err_core_module; |
96518518 | 10118 | |
d209df3e | 10119 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 10120 | if (err < 0) |
6001a930 | 10121 | goto err_netdev_notifier; |
96518518 | 10122 | |
4d44175a FW |
10123 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
10124 | if (err < 0) | |
6001a930 | 10125 | goto err_rht_objname; |
4d44175a | 10126 | |
06d392cb | 10127 | err = nft_offload_init(); |
10128 | if (err < 0) | |
6001a930 PNA |
10129 | goto err_offload; |
10130 | ||
10131 | err = netlink_register_notifier(&nft_nl_notifier); | |
10132 | if (err < 0) | |
10133 | goto err_netlink_notifier; | |
06d392cb | 10134 | |
d209df3e FW |
10135 | /* must be last */ |
10136 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
10137 | if (err < 0) | |
6001a930 | 10138 | goto err_nfnl_subsys; |
3b49e2e9 | 10139 | |
c1deb065 | 10140 | nft_chain_route_init(); |
3474a2c6 | 10141 | |
d209df3e | 10142 | return err; |
6001a930 PNA |
10143 | |
10144 | err_nfnl_subsys: | |
10145 | netlink_unregister_notifier(&nft_nl_notifier); | |
10146 | err_netlink_notifier: | |
06d392cb | 10147 | nft_offload_exit(); |
6001a930 | 10148 | err_offload: |
4d44175a | 10149 | rhltable_destroy(&nft_objname_ht); |
6001a930 | 10150 | err_rht_objname: |
d209df3e | 10151 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
6001a930 | 10152 | err_netdev_notifier: |
96518518 | 10153 | nf_tables_core_module_exit(); |
6001a930 | 10154 | err_core_module: |
d209df3e | 10155 | nft_chain_filter_fini(); |
6001a930 | 10156 | err_chain_filter: |
d209df3e | 10157 | unregister_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
10158 | return err; |
10159 | } | |
10160 | ||
10161 | static void __exit nf_tables_module_exit(void) | |
10162 | { | |
10163 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
6001a930 | 10164 | netlink_unregister_notifier(&nft_nl_notifier); |
06d392cb | 10165 | nft_offload_exit(); |
3b49e2e9 | 10166 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 10167 | nft_chain_filter_fini(); |
c1deb065 | 10168 | nft_chain_route_fini(); |
71ad00c5 | 10169 | unregister_pernet_subsys(&nf_tables_net_ops); |
0935d558 | 10170 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 10171 | rcu_barrier(); |
4d44175a | 10172 | rhltable_destroy(&nft_objname_ht); |
96518518 | 10173 | nf_tables_core_module_exit(); |
96518518 PM |
10174 | } |
10175 | ||
10176 | module_init(nf_tables_module_init); | |
10177 | module_exit(nf_tables_module_exit); | |
10178 | ||
10179 | MODULE_LICENSE("GPL"); | |
10180 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
10181 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |