Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
20a69341 | 3 | * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
96518518 | 4 | * |
96518518 PM |
5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
6 | */ | |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/list.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/netlink.h> | |
1ff75a3e | 13 | #include <linux/vmalloc.h> |
0eb71a9d | 14 | #include <linux/rhashtable.h> |
8e6cf365 | 15 | #include <linux/audit.h> |
96518518 PM |
16 | #include <linux/netfilter.h> |
17 | #include <linux/netfilter/nfnetlink.h> | |
18 | #include <linux/netfilter/nf_tables.h> | |
3b49e2e9 | 19 | #include <net/netfilter/nf_flow_table.h> |
96518518 PM |
20 | #include <net/netfilter/nf_tables_core.h> |
21 | #include <net/netfilter/nf_tables.h> | |
c9626a2c | 22 | #include <net/netfilter/nf_tables_offload.h> |
99633ab2 | 23 | #include <net/net_namespace.h> |
96518518 PM |
24 | #include <net/sock.h> |
25 | ||
9332d27d FW |
26 | #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-")) |
27 | ||
0854db2a FW |
28 | unsigned int nf_tables_net_id __read_mostly; |
29 | ||
96518518 | 30 | static LIST_HEAD(nf_tables_expressions); |
e5009240 | 31 | static LIST_HEAD(nf_tables_objects); |
3b49e2e9 | 32 | static LIST_HEAD(nf_tables_flowtables); |
0935d558 FW |
33 | static LIST_HEAD(nf_tables_destroy_list); |
34 | static DEFINE_SPINLOCK(nf_tables_destroy_list_lock); | |
96518518 | 35 | |
a654de8f PNA |
36 | enum { |
37 | NFT_VALIDATE_SKIP = 0, | |
38 | NFT_VALIDATE_NEED, | |
39 | NFT_VALIDATE_DO, | |
40 | }; | |
41 | ||
4d44175a FW |
42 | static struct rhltable nft_objname_ht; |
43 | ||
1b2470e5 FW |
44 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed); |
45 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed); | |
46 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
47 | ||
4d44175a FW |
48 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed); |
49 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed); | |
50 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
51 | ||
1b2470e5 FW |
52 | static const struct rhashtable_params nft_chain_ht_params = { |
53 | .head_offset = offsetof(struct nft_chain, rhlhead), | |
54 | .key_offset = offsetof(struct nft_chain, name), | |
55 | .hashfn = nft_chain_hash, | |
56 | .obj_hashfn = nft_chain_hash_obj, | |
57 | .obj_cmpfn = nft_chain_hash_cmp, | |
1b2470e5 FW |
58 | .automatic_shrinking = true, |
59 | }; | |
60 | ||
4d44175a FW |
61 | static const struct rhashtable_params nft_objname_ht_params = { |
62 | .head_offset = offsetof(struct nft_object, rhlhead), | |
63 | .key_offset = offsetof(struct nft_object, key), | |
64 | .hashfn = nft_objname_hash, | |
65 | .obj_hashfn = nft_objname_hash_obj, | |
66 | .obj_cmpfn = nft_objname_hash_cmp, | |
67 | .automatic_shrinking = true, | |
68 | }; | |
69 | ||
c520292f RGB |
70 | struct nft_audit_data { |
71 | struct nft_table *table; | |
72 | int entries; | |
73 | int op; | |
74 | struct list_head list; | |
75 | }; | |
76 | ||
77 | static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types | |
78 | [NFT_MSG_NEWTABLE] = AUDIT_NFT_OP_TABLE_REGISTER, | |
79 | [NFT_MSG_GETTABLE] = AUDIT_NFT_OP_INVALID, | |
80 | [NFT_MSG_DELTABLE] = AUDIT_NFT_OP_TABLE_UNREGISTER, | |
81 | [NFT_MSG_NEWCHAIN] = AUDIT_NFT_OP_CHAIN_REGISTER, | |
82 | [NFT_MSG_GETCHAIN] = AUDIT_NFT_OP_INVALID, | |
83 | [NFT_MSG_DELCHAIN] = AUDIT_NFT_OP_CHAIN_UNREGISTER, | |
84 | [NFT_MSG_NEWRULE] = AUDIT_NFT_OP_RULE_REGISTER, | |
85 | [NFT_MSG_GETRULE] = AUDIT_NFT_OP_INVALID, | |
86 | [NFT_MSG_DELRULE] = AUDIT_NFT_OP_RULE_UNREGISTER, | |
87 | [NFT_MSG_NEWSET] = AUDIT_NFT_OP_SET_REGISTER, | |
88 | [NFT_MSG_GETSET] = AUDIT_NFT_OP_INVALID, | |
89 | [NFT_MSG_DELSET] = AUDIT_NFT_OP_SET_UNREGISTER, | |
90 | [NFT_MSG_NEWSETELEM] = AUDIT_NFT_OP_SETELEM_REGISTER, | |
91 | [NFT_MSG_GETSETELEM] = AUDIT_NFT_OP_INVALID, | |
92 | [NFT_MSG_DELSETELEM] = AUDIT_NFT_OP_SETELEM_UNREGISTER, | |
93 | [NFT_MSG_NEWGEN] = AUDIT_NFT_OP_GEN_REGISTER, | |
94 | [NFT_MSG_GETGEN] = AUDIT_NFT_OP_INVALID, | |
95 | [NFT_MSG_TRACE] = AUDIT_NFT_OP_INVALID, | |
96 | [NFT_MSG_NEWOBJ] = AUDIT_NFT_OP_OBJ_REGISTER, | |
97 | [NFT_MSG_GETOBJ] = AUDIT_NFT_OP_INVALID, | |
98 | [NFT_MSG_DELOBJ] = AUDIT_NFT_OP_OBJ_UNREGISTER, | |
99 | [NFT_MSG_GETOBJ_RESET] = AUDIT_NFT_OP_OBJ_RESET, | |
100 | [NFT_MSG_NEWFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_REGISTER, | |
101 | [NFT_MSG_GETFLOWTABLE] = AUDIT_NFT_OP_INVALID, | |
102 | [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, | |
103 | }; | |
104 | ||
a654de8f PNA |
105 | static void nft_validate_state_update(struct net *net, u8 new_validate_state) |
106 | { | |
d59d2f82 | 107 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
108 | |
109 | switch (nft_net->validate_state) { | |
a654de8f PNA |
110 | case NFT_VALIDATE_SKIP: |
111 | WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO); | |
112 | break; | |
113 | case NFT_VALIDATE_NEED: | |
114 | break; | |
115 | case NFT_VALIDATE_DO: | |
116 | if (new_validate_state == NFT_VALIDATE_NEED) | |
117 | return; | |
118 | } | |
119 | ||
0854db2a | 120 | nft_net->validate_state = new_validate_state; |
a654de8f | 121 | } |
0935d558 FW |
122 | static void nf_tables_trans_destroy_work(struct work_struct *w); |
123 | static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work); | |
a654de8f | 124 | |
7c95f6d8 | 125 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 126 | struct net *net, |
7c95f6d8 PNA |
127 | const struct sk_buff *skb, |
128 | const struct nlmsghdr *nlh, | |
36596dad | 129 | u8 family, |
7c95f6d8 PNA |
130 | struct nft_table *table, |
131 | struct nft_chain *chain, | |
132 | const struct nlattr * const *nla) | |
133 | { | |
633c9a84 | 134 | ctx->net = net; |
36596dad | 135 | ctx->family = family; |
26b2f552 | 136 | ctx->level = 0; |
128ad332 PNA |
137 | ctx->table = table; |
138 | ctx->chain = chain; | |
139 | ctx->nla = nla; | |
140 | ctx->portid = NETLINK_CB(skb).portid; | |
141 | ctx->report = nlmsg_report(nlh); | |
c9626a2c | 142 | ctx->flags = nlh->nlmsg_flags; |
128ad332 | 143 | ctx->seq = nlh->nlmsg_seq; |
7c95f6d8 PNA |
144 | } |
145 | ||
8411b644 PNA |
146 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
147 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
148 | { |
149 | struct nft_trans *trans; | |
150 | ||
8411b644 | 151 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
152 | if (trans == NULL) |
153 | return NULL; | |
154 | ||
58007785 | 155 | INIT_LIST_HEAD(&trans->list); |
b380e5c7 | 156 | trans->msg_type = msg_type; |
1081d11b PNA |
157 | trans->ctx = *ctx; |
158 | ||
159 | return trans; | |
160 | } | |
161 | ||
8411b644 PNA |
162 | static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
163 | int msg_type, u32 size) | |
164 | { | |
165 | return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); | |
166 | } | |
167 | ||
1081d11b PNA |
168 | static void nft_trans_destroy(struct nft_trans *trans) |
169 | { | |
170 | list_del(&trans->list); | |
171 | kfree(trans); | |
172 | } | |
173 | ||
f6ac8585 PNA |
174 | static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set) |
175 | { | |
0854db2a | 176 | struct nftables_pernet *nft_net; |
f6ac8585 PNA |
177 | struct net *net = ctx->net; |
178 | struct nft_trans *trans; | |
179 | ||
180 | if (!nft_set_is_anonymous(set)) | |
181 | return; | |
182 | ||
d59d2f82 | 183 | nft_net = nft_pernet(net); |
0854db2a | 184 | list_for_each_entry_reverse(trans, &nft_net->commit_list, list) { |
6a0a8d10 PNA |
185 | switch (trans->msg_type) { |
186 | case NFT_MSG_NEWSET: | |
187 | if (nft_trans_set(trans) == set) | |
188 | nft_trans_set_bound(trans) = true; | |
189 | break; | |
190 | case NFT_MSG_NEWSETELEM: | |
191 | if (nft_trans_elem_set(trans) == set) | |
192 | nft_trans_elem_set_bound(trans) = true; | |
f6ac8585 PNA |
193 | break; |
194 | } | |
195 | } | |
196 | } | |
197 | ||
d54725cd PNA |
198 | static int nft_netdev_register_hooks(struct net *net, |
199 | struct list_head *hook_list) | |
200 | { | |
201 | struct nft_hook *hook; | |
202 | int err, j; | |
203 | ||
204 | j = 0; | |
205 | list_for_each_entry(hook, hook_list, list) { | |
206 | err = nf_register_net_hook(net, &hook->ops); | |
207 | if (err < 0) | |
208 | goto err_register; | |
209 | ||
210 | j++; | |
211 | } | |
212 | return 0; | |
213 | ||
214 | err_register: | |
215 | list_for_each_entry(hook, hook_list, list) { | |
216 | if (j-- <= 0) | |
217 | break; | |
218 | ||
219 | nf_unregister_net_hook(net, &hook->ops); | |
220 | } | |
221 | return err; | |
222 | } | |
223 | ||
224 | static void nft_netdev_unregister_hooks(struct net *net, | |
f9a43007 PNA |
225 | struct list_head *hook_list, |
226 | bool release_netdev) | |
d54725cd | 227 | { |
f9a43007 | 228 | struct nft_hook *hook, *next; |
d54725cd | 229 | |
f9a43007 | 230 | list_for_each_entry_safe(hook, next, hook_list, list) { |
d54725cd | 231 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
232 | if (release_netdev) { |
233 | list_del(&hook->list); | |
234 | kfree_rcu(hook, rcu); | |
235 | } | |
236 | } | |
d54725cd PNA |
237 | } |
238 | ||
c974a3a3 PNA |
239 | static int nf_tables_register_hook(struct net *net, |
240 | const struct nft_table *table, | |
241 | struct nft_chain *chain) | |
d8ee8f7c | 242 | { |
d54725cd | 243 | struct nft_base_chain *basechain; |
a37061a6 | 244 | const struct nf_hook_ops *ops; |
ae6153b5 | 245 | |
d8ee8f7c | 246 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 247 | !nft_is_base_chain(chain)) |
d8ee8f7c PNA |
248 | return 0; |
249 | ||
4e25ceb8 FW |
250 | basechain = nft_base_chain(chain); |
251 | ops = &basechain->ops; | |
ae6153b5 | 252 | |
4e25ceb8 FW |
253 | if (basechain->type->ops_register) |
254 | return basechain->type->ops_register(net, ops); | |
255 | ||
d3519cb8 | 256 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
1e9451cb FW |
257 | return nft_netdev_register_hooks(net, &basechain->hook_list); |
258 | ||
259 | return nf_register_net_hook(net, &basechain->ops); | |
d8ee8f7c PNA |
260 | } |
261 | ||
f9a43007 PNA |
262 | static void __nf_tables_unregister_hook(struct net *net, |
263 | const struct nft_table *table, | |
264 | struct nft_chain *chain, | |
265 | bool release_netdev) | |
c5598794 | 266 | { |
d54725cd | 267 | struct nft_base_chain *basechain; |
4e25ceb8 FW |
268 | const struct nf_hook_ops *ops; |
269 | ||
d8ee8f7c | 270 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 271 | !nft_is_base_chain(chain)) |
d8ee8f7c | 272 | return; |
4e25ceb8 FW |
273 | basechain = nft_base_chain(chain); |
274 | ops = &basechain->ops; | |
d8ee8f7c | 275 | |
4e25ceb8 FW |
276 | if (basechain->type->ops_unregister) |
277 | return basechain->type->ops_unregister(net, ops); | |
278 | ||
d3519cb8 | 279 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
f9a43007 PNA |
280 | nft_netdev_unregister_hooks(net, &basechain->hook_list, |
281 | release_netdev); | |
1e9451cb FW |
282 | else |
283 | nf_unregister_net_hook(net, &basechain->ops); | |
c5598794 AB |
284 | } |
285 | ||
f9a43007 PNA |
286 | static void nf_tables_unregister_hook(struct net *net, |
287 | const struct nft_table *table, | |
288 | struct nft_chain *chain) | |
289 | { | |
290 | return __nf_tables_unregister_hook(net, table, chain, false); | |
291 | } | |
292 | ||
0854db2a FW |
293 | static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans) |
294 | { | |
d59d2f82 | 295 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a | 296 | |
0854db2a FW |
297 | list_add_tail(&trans->list, &nft_net->commit_list); |
298 | } | |
299 | ||
ee01d542 AB |
300 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
301 | { | |
302 | struct nft_trans *trans; | |
303 | ||
304 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
305 | if (trans == NULL) | |
306 | return -ENOMEM; | |
307 | ||
308 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 309 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 | 310 | |
0854db2a | 311 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
312 | return 0; |
313 | } | |
314 | ||
315 | static int nft_deltable(struct nft_ctx *ctx) | |
316 | { | |
317 | int err; | |
318 | ||
319 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
320 | if (err < 0) | |
321 | return err; | |
322 | ||
f2a6d766 | 323 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
324 | return err; |
325 | } | |
326 | ||
66293c46 | 327 | static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
ee01d542 AB |
328 | { |
329 | struct nft_trans *trans; | |
330 | ||
331 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
332 | if (trans == NULL) | |
66293c46 | 333 | return ERR_PTR(-ENOMEM); |
ee01d542 | 334 | |
74cccc3d | 335 | if (msg_type == NFT_MSG_NEWCHAIN) { |
664b0f8c | 336 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 | 337 | |
74cccc3d PNA |
338 | if (ctx->nla[NFTA_CHAIN_ID]) { |
339 | nft_trans_chain_id(trans) = | |
340 | ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID])); | |
341 | } | |
342 | } | |
343 | ||
0854db2a | 344 | nft_trans_commit_list_add_tail(ctx->net, trans); |
66293c46 | 345 | return trans; |
ee01d542 AB |
346 | } |
347 | ||
348 | static int nft_delchain(struct nft_ctx *ctx) | |
349 | { | |
66293c46 | 350 | struct nft_trans *trans; |
ee01d542 | 351 | |
66293c46 FW |
352 | trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
353 | if (IS_ERR(trans)) | |
354 | return PTR_ERR(trans); | |
ee01d542 AB |
355 | |
356 | ctx->table->use--; | |
664b0f8c | 357 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 | 358 | |
66293c46 | 359 | return 0; |
ee01d542 AB |
360 | } |
361 | ||
bb7b40ae PNA |
362 | static void nft_rule_expr_activate(const struct nft_ctx *ctx, |
363 | struct nft_rule *rule) | |
364 | { | |
365 | struct nft_expr *expr; | |
366 | ||
367 | expr = nft_expr_first(rule); | |
31cc578a | 368 | while (nft_expr_more(rule, expr)) { |
bb7b40ae PNA |
369 | if (expr->ops->activate) |
370 | expr->ops->activate(ctx, expr); | |
371 | ||
372 | expr = nft_expr_next(expr); | |
373 | } | |
374 | } | |
375 | ||
376 | static void nft_rule_expr_deactivate(const struct nft_ctx *ctx, | |
f6ac8585 PNA |
377 | struct nft_rule *rule, |
378 | enum nft_trans_phase phase) | |
bb7b40ae PNA |
379 | { |
380 | struct nft_expr *expr; | |
381 | ||
382 | expr = nft_expr_first(rule); | |
31cc578a | 383 | while (nft_expr_more(rule, expr)) { |
bb7b40ae | 384 | if (expr->ops->deactivate) |
f6ac8585 | 385 | expr->ops->deactivate(ctx, expr, phase); |
bb7b40ae PNA |
386 | |
387 | expr = nft_expr_next(expr); | |
388 | } | |
389 | } | |
390 | ||
ee01d542 AB |
391 | static int |
392 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
393 | { | |
394 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
395 | if (nft_is_active_next(ctx->net, rule)) { |
396 | nft_deactivate_next(ctx->net, rule); | |
ee01d542 AB |
397 | ctx->chain->use--; |
398 | return 0; | |
399 | } | |
400 | return -ENOENT; | |
401 | } | |
402 | ||
403 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
404 | struct nft_rule *rule) | |
405 | { | |
406 | struct nft_trans *trans; | |
407 | ||
408 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
409 | if (trans == NULL) | |
410 | return NULL; | |
411 | ||
1a94e38d PNA |
412 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
413 | nft_trans_rule_id(trans) = | |
414 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
415 | } | |
ee01d542 | 416 | nft_trans_rule(trans) = rule; |
0854db2a | 417 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
418 | |
419 | return trans; | |
420 | } | |
421 | ||
422 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
423 | { | |
63b48c73 | 424 | struct nft_flow_rule *flow; |
ee01d542 AB |
425 | struct nft_trans *trans; |
426 | int err; | |
427 | ||
428 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
429 | if (trans == NULL) | |
430 | return -ENOMEM; | |
431 | ||
63b48c73 PNA |
432 | if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
433 | flow = nft_flow_rule_create(ctx->net, rule); | |
434 | if (IS_ERR(flow)) { | |
435 | nft_trans_destroy(trans); | |
436 | return PTR_ERR(flow); | |
437 | } | |
438 | ||
439 | nft_trans_flow_rule(trans) = flow; | |
440 | } | |
441 | ||
ee01d542 AB |
442 | err = nf_tables_delrule_deactivate(ctx, rule); |
443 | if (err < 0) { | |
444 | nft_trans_destroy(trans); | |
445 | return err; | |
446 | } | |
f6ac8585 | 447 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE); |
ee01d542 AB |
448 | |
449 | return 0; | |
450 | } | |
451 | ||
452 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
453 | { | |
454 | struct nft_rule *rule; | |
455 | int err; | |
456 | ||
457 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
23b7ca4f PNA |
458 | if (!nft_is_active_next(ctx->net, rule)) |
459 | continue; | |
460 | ||
ee01d542 AB |
461 | err = nft_delrule(ctx, rule); |
462 | if (err < 0) | |
463 | return err; | |
464 | } | |
465 | return 0; | |
466 | } | |
467 | ||
cd5125d8 | 468 | static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
ee01d542 AB |
469 | struct nft_set *set) |
470 | { | |
471 | struct nft_trans *trans; | |
472 | ||
473 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
474 | if (trans == NULL) | |
475 | return -ENOMEM; | |
476 | ||
477 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { | |
478 | nft_trans_set_id(trans) = | |
479 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 480 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
481 | } |
482 | nft_trans_set(trans) = set; | |
0854db2a | 483 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
484 | |
485 | return 0; | |
486 | } | |
487 | ||
cd5125d8 | 488 | static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set) |
ee01d542 AB |
489 | { |
490 | int err; | |
491 | ||
492 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
493 | if (err < 0) | |
494 | return err; | |
495 | ||
37a9cc52 | 496 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
497 | ctx->table->use--; |
498 | ||
499 | return err; | |
500 | } | |
501 | ||
e5009240 PNA |
502 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
503 | struct nft_object *obj) | |
504 | { | |
505 | struct nft_trans *trans; | |
506 | ||
507 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
508 | if (trans == NULL) | |
509 | return -ENOMEM; | |
510 | ||
511 | if (msg_type == NFT_MSG_NEWOBJ) | |
512 | nft_activate_next(ctx->net, obj); | |
513 | ||
514 | nft_trans_obj(trans) = obj; | |
0854db2a | 515 | nft_trans_commit_list_add_tail(ctx->net, trans); |
e5009240 PNA |
516 | |
517 | return 0; | |
518 | } | |
519 | ||
520 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
521 | { | |
522 | int err; | |
523 | ||
524 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
525 | if (err < 0) | |
526 | return err; | |
527 | ||
528 | nft_deactivate_next(ctx->net, obj); | |
529 | ctx->table->use--; | |
530 | ||
531 | return err; | |
532 | } | |
533 | ||
3b49e2e9 PNA |
534 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
535 | struct nft_flowtable *flowtable) | |
536 | { | |
537 | struct nft_trans *trans; | |
538 | ||
539 | trans = nft_trans_alloc(ctx, msg_type, | |
540 | sizeof(struct nft_trans_flowtable)); | |
541 | if (trans == NULL) | |
542 | return -ENOMEM; | |
543 | ||
544 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
545 | nft_activate_next(ctx->net, flowtable); | |
546 | ||
2c9e4559 | 547 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); |
3b49e2e9 | 548 | nft_trans_flowtable(trans) = flowtable; |
0854db2a | 549 | nft_trans_commit_list_add_tail(ctx->net, trans); |
3b49e2e9 PNA |
550 | |
551 | return 0; | |
552 | } | |
553 | ||
554 | static int nft_delflowtable(struct nft_ctx *ctx, | |
555 | struct nft_flowtable *flowtable) | |
556 | { | |
557 | int err; | |
558 | ||
559 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
560 | if (err < 0) | |
561 | return err; | |
562 | ||
563 | nft_deactivate_next(ctx->net, flowtable); | |
564 | ctx->table->use--; | |
565 | ||
566 | return err; | |
567 | } | |
568 | ||
34cc9e52 PNA |
569 | static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg) |
570 | { | |
571 | int i; | |
572 | ||
573 | for (i = track->regs[dreg].num_reg; i > 0; i--) | |
574 | __nft_reg_track_cancel(track, dreg - i); | |
575 | } | |
576 | ||
577 | static void __nft_reg_track_update(struct nft_regs_track *track, | |
578 | const struct nft_expr *expr, | |
579 | u8 dreg, u8 num_reg) | |
580 | { | |
581 | track->regs[dreg].selector = expr; | |
582 | track->regs[dreg].bitwise = NULL; | |
583 | track->regs[dreg].num_reg = num_reg; | |
584 | } | |
585 | ||
586 | void nft_reg_track_update(struct nft_regs_track *track, | |
587 | const struct nft_expr *expr, u8 dreg, u8 len) | |
588 | { | |
589 | unsigned int regcount; | |
590 | int i; | |
591 | ||
592 | __nft_reg_track_clobber(track, dreg); | |
593 | ||
594 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
595 | for (i = 0; i < regcount; i++, dreg++) | |
596 | __nft_reg_track_update(track, expr, dreg, i); | |
597 | } | |
598 | EXPORT_SYMBOL_GPL(nft_reg_track_update); | |
599 | ||
600 | void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len) | |
601 | { | |
602 | unsigned int regcount; | |
603 | int i; | |
604 | ||
605 | __nft_reg_track_clobber(track, dreg); | |
606 | ||
607 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
608 | for (i = 0; i < regcount; i++, dreg++) | |
609 | __nft_reg_track_cancel(track, dreg); | |
610 | } | |
611 | EXPORT_SYMBOL_GPL(nft_reg_track_cancel); | |
612 | ||
613 | void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) | |
614 | { | |
615 | track->regs[dreg].selector = NULL; | |
616 | track->regs[dreg].bitwise = NULL; | |
617 | track->regs[dreg].num_reg = 0; | |
618 | } | |
619 | EXPORT_SYMBOL_GPL(__nft_reg_track_cancel); | |
620 | ||
96518518 PM |
621 | /* |
622 | * Tables | |
623 | */ | |
624 | ||
36596dad | 625 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 626 | const struct nlattr *nla, |
6001a930 | 627 | u8 family, u8 genmask, u32 nlpid) |
96518518 | 628 | { |
0854db2a | 629 | struct nftables_pernet *nft_net; |
96518518 PM |
630 | struct nft_table *table; |
631 | ||
cac20fcd PNA |
632 | if (nla == NULL) |
633 | return ERR_PTR(-EINVAL); | |
634 | ||
d59d2f82 | 635 | nft_net = nft_pernet(net); |
0854db2a FW |
636 | list_for_each_entry_rcu(table, &nft_net->tables, list, |
637 | lockdep_is_held(&nft_net->commit_mutex)) { | |
f2a6d766 | 638 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 639 | table->family == family && |
6001a930 PNA |
640 | nft_active_genmask(table, genmask)) { |
641 | if (nft_table_has_owner(table) && | |
53479909 | 642 | nlpid && table->nlpid != nlpid) |
6001a930 PNA |
643 | return ERR_PTR(-EPERM); |
644 | ||
96518518 | 645 | return table; |
6001a930 | 646 | } |
96518518 | 647 | } |
cac20fcd PNA |
648 | |
649 | return ERR_PTR(-ENOENT); | |
96518518 PM |
650 | } |
651 | ||
3ecbfd65 HS |
652 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
653 | const struct nlattr *nla, | |
e31f072f | 654 | u8 genmask, u32 nlpid) |
3ecbfd65 | 655 | { |
0854db2a | 656 | struct nftables_pernet *nft_net; |
3ecbfd65 HS |
657 | struct nft_table *table; |
658 | ||
d59d2f82 | 659 | nft_net = nft_pernet(net); |
0854db2a | 660 | list_for_each_entry(table, &nft_net->tables, list) { |
3ecbfd65 | 661 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && |
e31f072f PNA |
662 | nft_active_genmask(table, genmask)) { |
663 | if (nft_table_has_owner(table) && | |
664 | nlpid && table->nlpid != nlpid) | |
665 | return ERR_PTR(-EPERM); | |
666 | ||
3ecbfd65 | 667 | return table; |
e31f072f | 668 | } |
3ecbfd65 | 669 | } |
3ecbfd65 HS |
670 | |
671 | return ERR_PTR(-ENOENT); | |
672 | } | |
673 | ||
96518518 PM |
674 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
675 | { | |
676 | return ++table->hgenerator; | |
677 | } | |
678 | ||
32537e91 | 679 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 680 | |
82603549 PNA |
681 | static const struct nft_chain_type * |
682 | __nft_chain_type_get(u8 family, enum nft_chain_types type) | |
683 | { | |
684 | if (family >= NFPROTO_NUMPROTO || | |
685 | type >= NFT_CHAIN_T_MAX) | |
686 | return NULL; | |
687 | ||
688 | return chain_type[family][type]; | |
689 | } | |
690 | ||
32537e91 | 691 | static const struct nft_chain_type * |
1ea26cca | 692 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c | 693 | { |
82603549 | 694 | const struct nft_chain_type *type; |
9370761c PNA |
695 | int i; |
696 | ||
baae3e62 | 697 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
82603549 PNA |
698 | type = __nft_chain_type_get(family, i); |
699 | if (!type) | |
700 | continue; | |
701 | if (!nla_strcmp(nla, type->name)) | |
702 | return type; | |
9370761c | 703 | } |
baae3e62 | 704 | return NULL; |
9370761c PNA |
705 | } |
706 | ||
eb014de4 PNA |
707 | struct nft_module_request { |
708 | struct list_head list; | |
709 | char module[MODULE_NAME_LEN]; | |
710 | bool done; | |
711 | }; | |
712 | ||
452238e8 | 713 | #ifdef CONFIG_MODULES |
cefa31a9 FW |
714 | __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, |
715 | ...) | |
452238e8 FW |
716 | { |
717 | char module_name[MODULE_NAME_LEN]; | |
0854db2a | 718 | struct nftables_pernet *nft_net; |
eb014de4 | 719 | struct nft_module_request *req; |
452238e8 FW |
720 | va_list args; |
721 | int ret; | |
722 | ||
452238e8 FW |
723 | va_start(args, fmt); |
724 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | |
725 | va_end(args); | |
9332d27d | 726 | if (ret >= MODULE_NAME_LEN) |
eb014de4 | 727 | return 0; |
452238e8 | 728 | |
d59d2f82 | 729 | nft_net = nft_pernet(net); |
0854db2a | 730 | list_for_each_entry(req, &nft_net->module_list, list) { |
eb014de4 PNA |
731 | if (!strcmp(req->module, module_name)) { |
732 | if (req->done) | |
733 | return 0; | |
ec7470b8 | 734 | |
eb014de4 PNA |
735 | /* A request to load this module already exists. */ |
736 | return -EAGAIN; | |
737 | } | |
738 | } | |
739 | ||
740 | req = kmalloc(sizeof(*req), GFP_KERNEL); | |
741 | if (!req) | |
742 | return -ENOMEM; | |
743 | ||
744 | req->done = false; | |
8556bceb | 745 | strscpy(req->module, module_name, MODULE_NAME_LEN); |
0854db2a | 746 | list_add_tail(&req->list, &nft_net->module_list); |
eb014de4 PNA |
747 | |
748 | return -EAGAIN; | |
452238e8 | 749 | } |
cefa31a9 | 750 | EXPORT_SYMBOL_GPL(nft_request_module); |
452238e8 FW |
751 | #endif |
752 | ||
f102d66b FW |
753 | static void lockdep_nfnl_nft_mutex_not_held(void) |
754 | { | |
755 | #ifdef CONFIG_PROVE_LOCKING | |
c0700dfa FW |
756 | if (debug_locks) |
757 | WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
f102d66b FW |
758 | #endif |
759 | } | |
760 | ||
32537e91 | 761 | static const struct nft_chain_type * |
452238e8 FW |
762 | nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla, |
763 | u8 family, bool autoload) | |
9370761c | 764 | { |
32537e91 | 765 | const struct nft_chain_type *type; |
9370761c | 766 | |
1ea26cca | 767 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
768 | if (type != NULL) |
769 | return type; | |
f102d66b FW |
770 | |
771 | lockdep_nfnl_nft_mutex_not_held(); | |
9370761c | 772 | #ifdef CONFIG_MODULES |
93b0806f | 773 | if (autoload) { |
eb014de4 PNA |
774 | if (nft_request_module(net, "nft-chain-%u-%.*s", family, |
775 | nla_len(nla), | |
776 | (const char *)nla_data(nla)) == -EAGAIN) | |
93b0806f | 777 | return ERR_PTR(-EAGAIN); |
9370761c PNA |
778 | } |
779 | #endif | |
93b0806f | 780 | return ERR_PTR(-ENOENT); |
9370761c PNA |
781 | } |
782 | ||
802b8051 PNA |
783 | static __be16 nft_base_seq(const struct net *net) |
784 | { | |
d59d2f82 | 785 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
786 | |
787 | return htons(nft_net->base_seq & 0xffff); | |
802b8051 PNA |
788 | } |
789 | ||
96518518 | 790 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
791 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
792 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 793 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 794 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
7a81575b JGG |
795 | [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY, |
796 | .len = NFT_USERDATA_MAXLEN } | |
96518518 PM |
797 | }; |
798 | ||
84d7fce6 PNA |
799 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
800 | u32 portid, u32 seq, int event, u32 flags, | |
801 | int family, const struct nft_table *table) | |
96518518 PM |
802 | { |
803 | struct nlmsghdr *nlh; | |
96518518 | 804 | |
dedb67c4 | 805 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
806 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
807 | NFNETLINK_V0, nft_base_seq(net)); | |
808 | if (!nlh) | |
96518518 PM |
809 | goto nla_put_failure; |
810 | ||
9ddf6323 | 811 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
179d9ba5 PNA |
812 | nla_put_be32(skb, NFTA_TABLE_FLAGS, |
813 | htonl(table->flags & NFT_TABLE_F_MASK)) || | |
3ecbfd65 HS |
814 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
815 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
816 | NFTA_TABLE_PAD)) | |
96518518 | 817 | goto nla_put_failure; |
6001a930 PNA |
818 | if (nft_table_has_owner(table) && |
819 | nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid))) | |
820 | goto nla_put_failure; | |
96518518 | 821 | |
7a81575b JGG |
822 | if (table->udata) { |
823 | if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata)) | |
824 | goto nla_put_failure; | |
825 | } | |
826 | ||
053c095a JB |
827 | nlmsg_end(skb, nlh); |
828 | return 0; | |
96518518 PM |
829 | |
830 | nla_put_failure: | |
831 | nlmsg_trim(skb, nlh); | |
832 | return -1; | |
833 | } | |
834 | ||
67cc570e PNA |
835 | struct nftnl_skb_parms { |
836 | bool report; | |
837 | }; | |
838 | #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb)) | |
839 | ||
840 | static void nft_notify_enqueue(struct sk_buff *skb, bool report, | |
841 | struct list_head *notify_list) | |
842 | { | |
843 | NFT_CB(skb).report = report; | |
844 | list_add_tail(&skb->list, notify_list); | |
845 | } | |
846 | ||
25e94a99 | 847 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 | 848 | { |
0854db2a | 849 | struct nftables_pernet *nft_net; |
96518518 | 850 | struct sk_buff *skb; |
6fb721cf | 851 | u16 flags = 0; |
96518518 PM |
852 | int err; |
853 | ||
128ad332 PNA |
854 | if (!ctx->report && |
855 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 856 | return; |
96518518 | 857 | |
96518518 PM |
858 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
859 | if (skb == NULL) | |
860 | goto err; | |
861 | ||
6fb721cf PNA |
862 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
863 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
864 | ||
84d7fce6 | 865 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 866 | event, flags, ctx->family, ctx->table); |
96518518 PM |
867 | if (err < 0) { |
868 | kfree_skb(skb); | |
869 | goto err; | |
870 | } | |
871 | ||
d59d2f82 | 872 | nft_net = nft_pernet(ctx->net); |
0854db2a | 873 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 874 | return; |
96518518 | 875 | err: |
25e94a99 | 876 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
877 | } |
878 | ||
879 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
880 | struct netlink_callback *cb) | |
881 | { | |
882 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
0854db2a | 883 | struct nftables_pernet *nft_net; |
96518518 PM |
884 | const struct nft_table *table; |
885 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 886 | struct net *net = sock_net(skb->sk); |
96518518 PM |
887 | int family = nfmsg->nfgen_family; |
888 | ||
e688a7f8 | 889 | rcu_read_lock(); |
d59d2f82 | 890 | nft_net = nft_pernet(net); |
34002783 | 891 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 892 | |
0854db2a | 893 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 894 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
895 | continue; |
896 | ||
36596dad PNA |
897 | if (idx < s_idx) |
898 | goto cont; | |
899 | if (idx > s_idx) | |
900 | memset(&cb->args[1], 0, | |
901 | sizeof(cb->args) - sizeof(cb->args[0])); | |
902 | if (!nft_is_active(net, table)) | |
903 | continue; | |
904 | if (nf_tables_fill_table_info(skb, net, | |
905 | NETLINK_CB(cb->skb).portid, | |
906 | cb->nlh->nlmsg_seq, | |
907 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 908 | table->family, table) < 0) |
36596dad PNA |
909 | goto done; |
910 | ||
911 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 912 | cont: |
36596dad | 913 | idx++; |
96518518 PM |
914 | } |
915 | done: | |
e688a7f8 | 916 | rcu_read_unlock(); |
96518518 PM |
917 | cb->args[0] = idx; |
918 | return skb->len; | |
919 | } | |
920 | ||
d9adf22a FW |
921 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
922 | const struct nlmsghdr *nlh, | |
923 | struct netlink_dump_control *c) | |
924 | { | |
925 | int err; | |
926 | ||
927 | if (!try_module_get(THIS_MODULE)) | |
928 | return -EINVAL; | |
929 | ||
930 | rcu_read_unlock(); | |
931 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
932 | rcu_read_lock(); | |
933 | module_put(THIS_MODULE); | |
934 | ||
935 | return err; | |
936 | } | |
937 | ||
938 | /* called with rcu_read_lock held */ | |
797d4980 PNA |
939 | static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info, |
940 | const struct nlattr * const nla[]) | |
96518518 | 941 | { |
797d4980 PNA |
942 | struct netlink_ext_ack *extack = info->extack; |
943 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 944 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 945 | const struct nft_table *table; |
797d4980 | 946 | struct net *net = info->net; |
96518518 | 947 | struct sk_buff *skb2; |
96518518 PM |
948 | int err; |
949 | ||
797d4980 | 950 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
951 | struct netlink_dump_control c = { |
952 | .dump = nf_tables_dump_tables, | |
d9adf22a | 953 | .module = THIS_MODULE, |
96518518 | 954 | }; |
d9adf22a | 955 | |
797d4980 | 956 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
957 | } |
958 | ||
6001a930 | 959 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0); |
36dd1bcc PNA |
960 | if (IS_ERR(table)) { |
961 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 962 | return PTR_ERR(table); |
36dd1bcc | 963 | } |
96518518 | 964 | |
d9adf22a | 965 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
966 | if (!skb2) |
967 | return -ENOMEM; | |
968 | ||
84d7fce6 | 969 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
970 | info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE, |
971 | 0, family, table); | |
96518518 | 972 | if (err < 0) |
ee921183 | 973 | goto err_fill_table_info; |
96518518 | 974 | |
ee921183 | 975 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 976 | |
ee921183 | 977 | err_fill_table_info: |
96518518 PM |
978 | kfree_skb(skb2); |
979 | return err; | |
980 | } | |
981 | ||
c9c17211 | 982 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
983 | { |
984 | struct nft_chain *chain; | |
985 | u32 i = 0; | |
986 | ||
987 | list_for_each_entry(chain, &table->chains, list) { | |
988 | if (!nft_is_active_next(net, chain)) | |
989 | continue; | |
f323d954 | 990 | if (!nft_is_base_chain(chain)) |
10435c11 F |
991 | continue; |
992 | ||
993 | if (cnt && i++ == cnt) | |
994 | break; | |
995 | ||
1e9451cb | 996 | nf_tables_unregister_hook(net, table, chain); |
10435c11 F |
997 | } |
998 | } | |
999 | ||
c9c17211 | 1000 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
1001 | { |
1002 | struct nft_chain *chain; | |
1003 | int err, i = 0; | |
1004 | ||
1005 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1006 | if (!nft_is_active_next(net, chain)) |
1007 | continue; | |
f323d954 | 1008 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
1009 | continue; |
1010 | ||
1e9451cb | 1011 | err = nf_tables_register_hook(net, table, chain); |
9ddf6323 | 1012 | if (err < 0) |
d54725cd | 1013 | goto err_register_hooks; |
9ddf6323 PNA |
1014 | |
1015 | i++; | |
1016 | } | |
1017 | return 0; | |
d54725cd PNA |
1018 | |
1019 | err_register_hooks: | |
10435c11 | 1020 | if (i) |
c9c17211 | 1021 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
1022 | return err; |
1023 | } | |
1024 | ||
c9c17211 | 1025 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 1026 | { |
179d9ba5 | 1027 | table->flags &= ~NFT_TABLE_F_DORMANT; |
c9c17211 | 1028 | nft_table_disable(net, table, 0); |
179d9ba5 | 1029 | table->flags |= NFT_TABLE_F_DORMANT; |
9ddf6323 PNA |
1030 | } |
1031 | ||
179d9ba5 PNA |
1032 | #define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1) |
1033 | #define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0) | |
1034 | #define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1) | |
1035 | #define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \ | |
1036 | __NFT_TABLE_F_WAS_AWAKEN) | |
0ce7cf41 | 1037 | |
e1aaca93 | 1038 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 1039 | { |
55dd6f93 | 1040 | struct nft_trans *trans; |
e1aaca93 | 1041 | u32 flags; |
179d9ba5 | 1042 | int ret; |
9ddf6323 | 1043 | |
e1aaca93 PNA |
1044 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
1045 | return 0; | |
9ddf6323 | 1046 | |
e1aaca93 | 1047 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
6001a930 PNA |
1048 | if (flags & ~NFT_TABLE_F_MASK) |
1049 | return -EOPNOTSUPP; | |
e1aaca93 | 1050 | |
63283dd2 PNA |
1051 | if (flags == ctx->table->flags) |
1052 | return 0; | |
1053 | ||
9cc0001a PNA |
1054 | if ((nft_table_has_owner(ctx->table) && |
1055 | !(flags & NFT_TABLE_F_OWNER)) || | |
1056 | (!nft_table_has_owner(ctx->table) && | |
1057 | flags & NFT_TABLE_F_OWNER)) | |
1058 | return -EOPNOTSUPP; | |
1059 | ||
55dd6f93 PNA |
1060 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
1061 | sizeof(struct nft_trans_table)); | |
1062 | if (trans == NULL) | |
1063 | return -ENOMEM; | |
9ddf6323 | 1064 | |
e1aaca93 PNA |
1065 | if ((flags & NFT_TABLE_F_DORMANT) && |
1066 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
179d9ba5 PNA |
1067 | ctx->table->flags |= NFT_TABLE_F_DORMANT; |
1068 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) | |
1069 | ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN; | |
e1aaca93 PNA |
1070 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
1071 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
179d9ba5 PNA |
1072 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
1073 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) { | |
1074 | ret = nf_tables_table_enable(ctx->net, ctx->table); | |
1075 | if (ret < 0) | |
1076 | goto err_register_hooks; | |
1077 | ||
1078 | ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT; | |
1079 | } | |
9ddf6323 PNA |
1080 | } |
1081 | ||
55dd6f93 | 1082 | nft_trans_table_update(trans) = true; |
0854db2a | 1083 | nft_trans_commit_list_add_tail(ctx->net, trans); |
179d9ba5 | 1084 | |
55dd6f93 | 1085 | return 0; |
179d9ba5 PNA |
1086 | |
1087 | err_register_hooks: | |
55dd6f93 | 1088 | nft_trans_destroy(trans); |
9ddf6323 PNA |
1089 | return ret; |
1090 | } | |
1091 | ||
1b2470e5 FW |
1092 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
1093 | { | |
1094 | const char *name = data; | |
1095 | ||
1096 | return jhash(name, strlen(name), seed); | |
1097 | } | |
1098 | ||
1099 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
1100 | { | |
1101 | const struct nft_chain *chain = data; | |
1102 | ||
1103 | return nft_chain_hash(chain->name, 0, seed); | |
1104 | } | |
1105 | ||
1106 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
1107 | const void *ptr) | |
1108 | { | |
1109 | const struct nft_chain *chain = ptr; | |
1110 | const char *name = arg->key; | |
1111 | ||
1112 | return strcmp(chain->name, name); | |
1113 | } | |
1114 | ||
4d44175a FW |
1115 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed) |
1116 | { | |
1117 | const struct nft_object_hash_key *k = data; | |
1118 | ||
1119 | seed ^= hash_ptr(k->table, 32); | |
1120 | ||
1121 | return jhash(k->name, strlen(k->name), seed); | |
1122 | } | |
1123 | ||
1124 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed) | |
1125 | { | |
1126 | const struct nft_object *obj = data; | |
1127 | ||
1128 | return nft_objname_hash(&obj->key, 0, seed); | |
1129 | } | |
1130 | ||
1131 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg, | |
1132 | const void *ptr) | |
1133 | { | |
1134 | const struct nft_object_hash_key *k = arg->key; | |
1135 | const struct nft_object *obj = ptr; | |
1136 | ||
1137 | if (obj->key.table != k->table) | |
1138 | return -1; | |
1139 | ||
1140 | return strcmp(obj->key.name, k->name); | |
1141 | } | |
1142 | ||
f1082dd3 PS |
1143 | static bool nft_supported_family(u8 family) |
1144 | { | |
1145 | return false | |
1146 | #ifdef CONFIG_NF_TABLES_INET | |
1147 | || family == NFPROTO_INET | |
1148 | #endif | |
1149 | #ifdef CONFIG_NF_TABLES_IPV4 | |
1150 | || family == NFPROTO_IPV4 | |
1151 | #endif | |
1152 | #ifdef CONFIG_NF_TABLES_ARP | |
1153 | || family == NFPROTO_ARP | |
1154 | #endif | |
1155 | #ifdef CONFIG_NF_TABLES_NETDEV | |
1156 | || family == NFPROTO_NETDEV | |
1157 | #endif | |
1158 | #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE) | |
1159 | || family == NFPROTO_BRIDGE | |
1160 | #endif | |
1161 | #ifdef CONFIG_NF_TABLES_IPV6 | |
1162 | || family == NFPROTO_IPV6 | |
1163 | #endif | |
1164 | ; | |
1165 | } | |
1166 | ||
7dab8ee3 PNA |
1167 | static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, |
1168 | const struct nlattr * const nla[]) | |
96518518 | 1169 | { |
7dab8ee3 | 1170 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
1171 | struct netlink_ext_ack *extack = info->extack; |
1172 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1173 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1174 | struct net *net = info->net; |
36dd1bcc PNA |
1175 | const struct nlattr *attr; |
1176 | struct nft_table *table; | |
e1aaca93 | 1177 | struct nft_ctx ctx; |
7a81575b | 1178 | u32 flags = 0; |
55dd6f93 | 1179 | int err; |
96518518 | 1180 | |
f1082dd3 PS |
1181 | if (!nft_supported_family(family)) |
1182 | return -EOPNOTSUPP; | |
1183 | ||
0854db2a | 1184 | lockdep_assert_held(&nft_net->commit_mutex); |
36dd1bcc | 1185 | attr = nla[NFTA_TABLE_NAME]; |
6001a930 PNA |
1186 | table = nft_table_lookup(net, attr, family, genmask, |
1187 | NETLINK_CB(skb).portid); | |
96518518 PM |
1188 | if (IS_ERR(table)) { |
1189 | if (PTR_ERR(table) != -ENOENT) | |
1190 | return PTR_ERR(table); | |
1a28ad74 | 1191 | } else { |
7dab8ee3 | 1192 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 1193 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1194 | return -EEXIST; |
36dd1bcc | 1195 | } |
7dab8ee3 | 1196 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 | 1197 | return -EOPNOTSUPP; |
e1aaca93 | 1198 | |
7dab8ee3 PNA |
1199 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
1200 | ||
e1aaca93 | 1201 | return nf_tables_updtable(&ctx); |
96518518 PM |
1202 | } |
1203 | ||
c5c1f975 PM |
1204 | if (nla[NFTA_TABLE_FLAGS]) { |
1205 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
6001a930 PNA |
1206 | if (flags & ~NFT_TABLE_F_MASK) |
1207 | return -EOPNOTSUPP; | |
c5c1f975 PM |
1208 | } |
1209 | ||
ffdb210e | 1210 | err = -ENOMEM; |
33758c89 | 1211 | table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT); |
ffdb210e | 1212 | if (table == NULL) |
98319cb9 | 1213 | goto err_kzalloc; |
96518518 | 1214 | |
33758c89 | 1215 | table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT); |
e46abbcc | 1216 | if (table->name == NULL) |
98319cb9 | 1217 | goto err_strdup; |
e46abbcc | 1218 | |
7a81575b | 1219 | if (nla[NFTA_TABLE_USERDATA]) { |
33758c89 | 1220 | table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT); |
7a81575b JGG |
1221 | if (table->udata == NULL) |
1222 | goto err_table_udata; | |
1223 | ||
85db827a | 1224 | table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]); |
7a81575b JGG |
1225 | } |
1226 | ||
1b2470e5 FW |
1227 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
1228 | if (err) | |
1229 | goto err_chain_ht; | |
1230 | ||
96518518 | 1231 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 1232 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 1233 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 1234 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 1235 | table->family = family; |
c5c1f975 | 1236 | table->flags = flags; |
ab482c6b | 1237 | table->handle = ++nft_net->table_handle; |
6001a930 PNA |
1238 | if (table->flags & NFT_TABLE_F_OWNER) |
1239 | table->nlpid = NETLINK_CB(skb).portid; | |
9ddf6323 | 1240 | |
7dab8ee3 | 1241 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
55dd6f93 | 1242 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 1243 | if (err < 0) |
98319cb9 | 1244 | goto err_trans; |
ffdb210e | 1245 | |
0854db2a | 1246 | list_add_tail_rcu(&table->list, &nft_net->tables); |
96518518 | 1247 | return 0; |
98319cb9 | 1248 | err_trans: |
1b2470e5 FW |
1249 | rhltable_destroy(&table->chains_ht); |
1250 | err_chain_ht: | |
7a81575b JGG |
1251 | kfree(table->udata); |
1252 | err_table_udata: | |
e46abbcc | 1253 | kfree(table->name); |
98319cb9 | 1254 | err_strdup: |
ffdb210e | 1255 | kfree(table); |
98319cb9 | 1256 | err_kzalloc: |
ffdb210e | 1257 | return err; |
96518518 PM |
1258 | } |
1259 | ||
b9ac12ef AB |
1260 | static int nft_flush_table(struct nft_ctx *ctx) |
1261 | { | |
3b49e2e9 | 1262 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 1263 | struct nft_chain *chain, *nc; |
e5009240 | 1264 | struct nft_object *obj, *ne; |
b9ac12ef | 1265 | struct nft_set *set, *ns; |
3b49e2e9 | 1266 | int err; |
b9ac12ef | 1267 | |
a2f18db0 | 1268 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
1269 | if (!nft_is_active_next(ctx->net, chain)) |
1270 | continue; | |
1271 | ||
d0e2c7de PNA |
1272 | if (nft_chain_is_bound(chain)) |
1273 | continue; | |
1274 | ||
b9ac12ef AB |
1275 | ctx->chain = chain; |
1276 | ||
1277 | err = nft_delrule_by_chain(ctx); | |
1278 | if (err < 0) | |
1279 | goto out; | |
b9ac12ef AB |
1280 | } |
1281 | ||
1282 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
1283 | if (!nft_is_active_next(ctx->net, set)) |
1284 | continue; | |
1285 | ||
408070d6 | 1286 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
1287 | !list_empty(&set->bindings)) |
1288 | continue; | |
1289 | ||
1290 | err = nft_delset(ctx, set); | |
1291 | if (err < 0) | |
1292 | goto out; | |
1293 | } | |
1294 | ||
3b49e2e9 | 1295 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
335178d5 FW |
1296 | if (!nft_is_active_next(ctx->net, flowtable)) |
1297 | continue; | |
1298 | ||
3b49e2e9 PNA |
1299 | err = nft_delflowtable(ctx, flowtable); |
1300 | if (err < 0) | |
1301 | goto out; | |
1302 | } | |
1303 | ||
e5009240 | 1304 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
335178d5 FW |
1305 | if (!nft_is_active_next(ctx->net, obj)) |
1306 | continue; | |
1307 | ||
e5009240 PNA |
1308 | err = nft_delobj(ctx, obj); |
1309 | if (err < 0) | |
1310 | goto out; | |
1311 | } | |
1312 | ||
a2f18db0 | 1313 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
1314 | if (!nft_is_active_next(ctx->net, chain)) |
1315 | continue; | |
1316 | ||
d0e2c7de PNA |
1317 | if (nft_chain_is_bound(chain)) |
1318 | continue; | |
1319 | ||
a2f18db0 PNA |
1320 | ctx->chain = chain; |
1321 | ||
1322 | err = nft_delchain(ctx); | |
1323 | if (err < 0) | |
1324 | goto out; | |
1325 | } | |
1326 | ||
b9ac12ef AB |
1327 | err = nft_deltable(ctx); |
1328 | out: | |
1329 | return err; | |
1330 | } | |
1331 | ||
1332 | static int nft_flush(struct nft_ctx *ctx, int family) | |
1333 | { | |
d59d2f82 | 1334 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
b9ac12ef | 1335 | const struct nlattr * const *nla = ctx->nla; |
d59d2f82 | 1336 | struct nft_table *table, *nt; |
b9ac12ef AB |
1337 | int err = 0; |
1338 | ||
0854db2a | 1339 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
98319cb9 | 1340 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
1341 | continue; |
1342 | ||
98319cb9 | 1343 | ctx->family = table->family; |
f2a6d766 | 1344 | |
36596dad PNA |
1345 | if (!nft_is_active_next(ctx->net, table)) |
1346 | continue; | |
b9ac12ef | 1347 | |
6001a930 PNA |
1348 | if (nft_table_has_owner(table) && table->nlpid != ctx->portid) |
1349 | continue; | |
1350 | ||
36596dad PNA |
1351 | if (nla[NFTA_TABLE_NAME] && |
1352 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
1353 | continue; | |
b9ac12ef | 1354 | |
36596dad PNA |
1355 | ctx->table = table; |
1356 | ||
1357 | err = nft_flush_table(ctx); | |
1358 | if (err < 0) | |
1359 | goto out; | |
b9ac12ef AB |
1360 | } |
1361 | out: | |
1362 | return err; | |
1363 | } | |
1364 | ||
7dab8ee3 PNA |
1365 | static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info, |
1366 | const struct nlattr * const nla[]) | |
96518518 | 1367 | { |
7dab8ee3 PNA |
1368 | struct netlink_ext_ack *extack = info->extack; |
1369 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1370 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1371 | struct net *net = info->net; |
36dd1bcc PNA |
1372 | const struct nlattr *attr; |
1373 | struct nft_table *table; | |
55dd6f93 | 1374 | struct nft_ctx ctx; |
96518518 | 1375 | |
7dab8ee3 | 1376 | nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
1377 | if (family == AF_UNSPEC || |
1378 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
1379 | return nft_flush(&ctx, family); |
1380 | ||
36dd1bcc PNA |
1381 | if (nla[NFTA_TABLE_HANDLE]) { |
1382 | attr = nla[NFTA_TABLE_HANDLE]; | |
e31f072f PNA |
1383 | table = nft_table_lookup_byhandle(net, attr, genmask, |
1384 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
1385 | } else { |
1386 | attr = nla[NFTA_TABLE_NAME]; | |
6001a930 PNA |
1387 | table = nft_table_lookup(net, attr, family, genmask, |
1388 | NETLINK_CB(skb).portid); | |
36dd1bcc | 1389 | } |
3ecbfd65 | 1390 | |
36dd1bcc PNA |
1391 | if (IS_ERR(table)) { |
1392 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 1393 | return PTR_ERR(table); |
36dd1bcc | 1394 | } |
96518518 | 1395 | |
7dab8ee3 | 1396 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
a8278400 PNA |
1397 | table->use > 0) |
1398 | return -EBUSY; | |
1399 | ||
98319cb9 | 1400 | ctx.family = family; |
b9ac12ef | 1401 | ctx.table = table; |
55dd6f93 | 1402 | |
b9ac12ef | 1403 | return nft_flush_table(&ctx); |
96518518 PM |
1404 | } |
1405 | ||
55dd6f93 PNA |
1406 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
1407 | { | |
fa5950e4 FW |
1408 | if (WARN_ON(ctx->table->use > 0)) |
1409 | return; | |
4fefee57 | 1410 | |
1b2470e5 | 1411 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 1412 | kfree(ctx->table->name); |
bc7a7082 | 1413 | kfree(ctx->table->udata); |
55dd6f93 | 1414 | kfree(ctx->table); |
55dd6f93 PNA |
1415 | } |
1416 | ||
cc07eeb0 | 1417 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1418 | { |
96518518 | 1419 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
82603549 | 1420 | if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) { |
cc07eeb0 PNA |
1421 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1422 | return; | |
96518518 | 1423 | } |
9370761c | 1424 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 1425 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 1426 | } |
9370761c | 1427 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 1428 | |
32537e91 | 1429 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1430 | { |
96518518 | 1431 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 1432 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
1433 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1434 | } | |
9370761c | 1435 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
1436 | |
1437 | /* | |
1438 | * Chains | |
1439 | */ | |
1440 | ||
1441 | static struct nft_chain * | |
cac20fcd | 1442 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1443 | { |
1444 | struct nft_chain *chain; | |
1445 | ||
1446 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1447 | if (chain->handle == handle && |
1448 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1449 | return chain; |
1450 | } | |
1451 | ||
1452 | return ERR_PTR(-ENOENT); | |
1453 | } | |
1454 | ||
4d44175a | 1455 | static bool lockdep_commit_lock_is_held(const struct net *net) |
f102d66b FW |
1456 | { |
1457 | #ifdef CONFIG_PROVE_LOCKING | |
d59d2f82 | 1458 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
1459 | |
1460 | return lockdep_is_held(&nft_net->commit_mutex); | |
f102d66b FW |
1461 | #else |
1462 | return true; | |
1463 | #endif | |
1464 | } | |
1465 | ||
1466 | static struct nft_chain *nft_chain_lookup(struct net *net, | |
1467 | struct nft_table *table, | |
cac20fcd | 1468 | const struct nlattr *nla, u8 genmask) |
96518518 | 1469 | { |
1b2470e5 FW |
1470 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1471 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1472 | struct nft_chain *chain; |
1473 | ||
1474 | if (nla == NULL) | |
1475 | return ERR_PTR(-EINVAL); | |
1476 | ||
872f6903 | 1477 | nla_strscpy(search, nla, sizeof(search)); |
96518518 | 1478 | |
1b2470e5 | 1479 | WARN_ON(!rcu_read_lock_held() && |
f102d66b | 1480 | !lockdep_commit_lock_is_held(net)); |
1b2470e5 FW |
1481 | |
1482 | chain = ERR_PTR(-ENOENT); | |
1483 | rcu_read_lock(); | |
1484 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1485 | if (!list) | |
1486 | goto out_unlock; | |
1487 | ||
1488 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1489 | if (nft_active_genmask(chain, genmask)) | |
1490 | goto out_unlock; | |
1491 | } | |
1492 | chain = ERR_PTR(-ENOENT); | |
1493 | out_unlock: | |
1494 | rcu_read_unlock(); | |
1495 | return chain; | |
96518518 PM |
1496 | } |
1497 | ||
1498 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1499 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1500 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1501 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1502 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1503 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1504 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1505 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
9332d27d FW |
1506 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, |
1507 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
0ca743a5 | 1508 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
c9626a2c | 1509 | [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, |
74cccc3d | 1510 | [NFTA_CHAIN_ID] = { .type = NLA_U32 }, |
002f2176 JGG |
1511 | [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY, |
1512 | .len = NFT_USERDATA_MAXLEN }, | |
96518518 PM |
1513 | }; |
1514 | ||
1515 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1516 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1517 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1518 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1519 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1520 | }; |
1521 | ||
0ca743a5 PNA |
1522 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1523 | { | |
1524 | struct nft_stats *cpu_stats, total; | |
1525 | struct nlattr *nest; | |
ce355e20 ED |
1526 | unsigned int seq; |
1527 | u64 pkts, bytes; | |
0ca743a5 PNA |
1528 | int cpu; |
1529 | ||
edbd82c5 FW |
1530 | if (!stats) |
1531 | return 0; | |
1532 | ||
0ca743a5 PNA |
1533 | memset(&total, 0, sizeof(total)); |
1534 | for_each_possible_cpu(cpu) { | |
1535 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 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 && | |
77972a36 PNA |
2169 | !nft_chain_offload_support(basechain)) { |
2170 | list_splice_init(&basechain->hook_list, &hook->list); | |
d54725cd | 2171 | return -EOPNOTSUPP; |
77972a36 | 2172 | } |
d54725cd PNA |
2173 | |
2174 | flow_block_init(&basechain->flow_block); | |
2175 | ||
2176 | return 0; | |
2177 | } | |
2178 | ||
04b7db41 PNA |
2179 | static int nft_chain_add(struct nft_table *table, struct nft_chain *chain) |
2180 | { | |
2181 | int err; | |
2182 | ||
2183 | err = rhltable_insert_key(&table->chains_ht, chain->name, | |
2184 | &chain->rhlhead, nft_chain_ht_params); | |
2185 | if (err) | |
2186 | return err; | |
2187 | ||
2188 | list_add_tail_rcu(&chain->list, &table->chains); | |
2189 | ||
2190 | return 0; | |
2191 | } | |
2192 | ||
d0e2c7de PNA |
2193 | static u64 chain_id; |
2194 | ||
4035285f | 2195 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
983c4fcb PNA |
2196 | u8 policy, u32 flags, |
2197 | struct netlink_ext_ack *extack) | |
4035285f PNA |
2198 | { |
2199 | const struct nlattr * const *nla = ctx->nla; | |
2200 | struct nft_table *table = ctx->table; | |
4035285f | 2201 | struct nft_base_chain *basechain; |
4035285f | 2202 | struct net *net = ctx->net; |
d0e2c7de | 2203 | char name[NFT_NAME_MAXLEN]; |
2c865a8a | 2204 | struct nft_rule_blob *blob; |
66293c46 | 2205 | struct nft_trans *trans; |
4035285f | 2206 | struct nft_chain *chain; |
2c865a8a | 2207 | unsigned int data_size; |
4035285f PNA |
2208 | int err; |
2209 | ||
2210 | if (table->use == UINT_MAX) | |
2211 | return -EOVERFLOW; | |
2212 | ||
2213 | if (nla[NFTA_CHAIN_HOOK]) { | |
921ebde3 | 2214 | struct nft_stats __percpu *stats = NULL; |
4035285f | 2215 | struct nft_chain_hook hook; |
4035285f | 2216 | |
d0e2c7de PNA |
2217 | if (flags & NFT_CHAIN_BINDING) |
2218 | return -EOPNOTSUPP; | |
2219 | ||
983c4fcb PNA |
2220 | err = nft_chain_parse_hook(net, nla, &hook, family, extack, |
2221 | true); | |
4035285f PNA |
2222 | if (err < 0) |
2223 | return err; | |
2224 | ||
33758c89 | 2225 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2226 | if (basechain == NULL) { |
2227 | nft_chain_release_hook(&hook); | |
2228 | return -ENOMEM; | |
2229 | } | |
d54725cd | 2230 | chain = &basechain->chain; |
4035285f PNA |
2231 | |
2232 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2233 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2234 | if (IS_ERR(stats)) { | |
2235 | nft_chain_release_hook(&hook); | |
2236 | kfree(basechain); | |
2237 | return PTR_ERR(stats); | |
2238 | } | |
4c05ec47 | 2239 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
2240 | } |
2241 | ||
d54725cd PNA |
2242 | err = nft_basechain_init(basechain, family, &hook, flags); |
2243 | if (err < 0) { | |
2244 | nft_chain_release_hook(&hook); | |
2245 | kfree(basechain); | |
9a4d6dd5 | 2246 | free_percpu(stats); |
d54725cd PNA |
2247 | return err; |
2248 | } | |
921ebde3 TH |
2249 | if (stats) |
2250 | static_branch_inc(&nft_counters_enabled); | |
4035285f | 2251 | } else { |
d0e2c7de PNA |
2252 | if (flags & NFT_CHAIN_BASE) |
2253 | return -EINVAL; | |
2254 | if (flags & NFT_CHAIN_HW_OFFLOAD) | |
2255 | return -EOPNOTSUPP; | |
2256 | ||
33758c89 | 2257 | chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2258 | if (chain == NULL) |
2259 | return -ENOMEM; | |
d0e2c7de PNA |
2260 | |
2261 | chain->flags = flags; | |
4035285f | 2262 | } |
43a605f2 PNA |
2263 | ctx->chain = chain; |
2264 | ||
4035285f PNA |
2265 | INIT_LIST_HEAD(&chain->rules); |
2266 | chain->handle = nf_tables_alloc_handle(table); | |
2267 | chain->table = table; | |
d0e2c7de PNA |
2268 | |
2269 | if (nla[NFTA_CHAIN_NAME]) { | |
33758c89 | 2270 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
d0e2c7de | 2271 | } else { |
59136aa3 FW |
2272 | if (!(flags & NFT_CHAIN_BINDING)) { |
2273 | err = -EINVAL; | |
002f2176 | 2274 | goto err_destroy_chain; |
59136aa3 | 2275 | } |
d0e2c7de PNA |
2276 | |
2277 | snprintf(name, sizeof(name), "__chain%llu", ++chain_id); | |
33758c89 | 2278 | chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT); |
d0e2c7de PNA |
2279 | } |
2280 | ||
4035285f PNA |
2281 | if (!chain->name) { |
2282 | err = -ENOMEM; | |
002f2176 JGG |
2283 | goto err_destroy_chain; |
2284 | } | |
2285 | ||
2286 | if (nla[NFTA_CHAIN_USERDATA]) { | |
33758c89 | 2287 | chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT); |
002f2176 JGG |
2288 | if (chain->udata == NULL) { |
2289 | err = -ENOMEM; | |
2290 | goto err_destroy_chain; | |
2291 | } | |
2292 | chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]); | |
4035285f PNA |
2293 | } |
2294 | ||
2c865a8a PNA |
2295 | data_size = offsetof(struct nft_rule_dp, data); /* last rule */ |
2296 | blob = nf_tables_chain_alloc_rules(data_size); | |
2297 | if (!blob) { | |
0cbc06b3 | 2298 | err = -ENOMEM; |
002f2176 | 2299 | goto err_destroy_chain; |
0cbc06b3 FW |
2300 | } |
2301 | ||
2c865a8a PNA |
2302 | RCU_INIT_POINTER(chain->blob_gen_0, blob); |
2303 | RCU_INIT_POINTER(chain->blob_gen_1, blob); | |
0cbc06b3 | 2304 | |
c974a3a3 | 2305 | err = nf_tables_register_hook(net, table, chain); |
4035285f | 2306 | if (err < 0) |
002f2176 | 2307 | goto err_destroy_chain; |
4035285f | 2308 | |
66293c46 FW |
2309 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
2310 | if (IS_ERR(trans)) { | |
2311 | err = PTR_ERR(trans); | |
002f2176 | 2312 | goto err_unregister_hook; |
1b2470e5 | 2313 | } |
4035285f | 2314 | |
ad652f38 | 2315 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
2316 | if (nft_is_base_chain(chain)) |
2317 | nft_trans_chain_policy(trans) = policy; | |
2318 | ||
04b7db41 PNA |
2319 | err = nft_chain_add(table, chain); |
2320 | if (err < 0) { | |
2321 | nft_trans_destroy(trans); | |
002f2176 | 2322 | goto err_unregister_hook; |
04b7db41 PNA |
2323 | } |
2324 | ||
4035285f | 2325 | table->use++; |
4035285f PNA |
2326 | |
2327 | return 0; | |
002f2176 | 2328 | err_unregister_hook: |
c974a3a3 | 2329 | nf_tables_unregister_hook(net, table, chain); |
002f2176 | 2330 | err_destroy_chain: |
43a605f2 | 2331 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2332 | |
2333 | return err; | |
2334 | } | |
2335 | ||
d54725cd PNA |
2336 | static bool nft_hook_list_equal(struct list_head *hook_list1, |
2337 | struct list_head *hook_list2) | |
2338 | { | |
2339 | struct nft_hook *hook; | |
2340 | int n = 0, m = 0; | |
2341 | ||
2342 | n = 0; | |
2343 | list_for_each_entry(hook, hook_list2, list) { | |
2344 | if (!nft_hook_list_find(hook_list1, hook)) | |
2345 | return false; | |
2346 | ||
2347 | n++; | |
2348 | } | |
2349 | list_for_each_entry(hook, hook_list1, list) | |
2350 | m++; | |
2351 | ||
2352 | return n == m; | |
2353 | } | |
2354 | ||
c9626a2c | 2355 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
98a381a7 PNA |
2356 | u32 flags, const struct nlattr *attr, |
2357 | struct netlink_ext_ack *extack) | |
2c4a488a PNA |
2358 | { |
2359 | const struct nlattr * const *nla = ctx->nla; | |
2360 | struct nft_table *table = ctx->table; | |
2361 | struct nft_chain *chain = ctx->chain; | |
2c4a488a PNA |
2362 | struct nft_base_chain *basechain; |
2363 | struct nft_stats *stats = NULL; | |
2364 | struct nft_chain_hook hook; | |
2c4a488a PNA |
2365 | struct nf_hook_ops *ops; |
2366 | struct nft_trans *trans; | |
c974a3a3 | 2367 | int err; |
2c4a488a | 2368 | |
c9626a2c PNA |
2369 | if (chain->flags ^ flags) |
2370 | return -EOPNOTSUPP; | |
2371 | ||
2c4a488a | 2372 | if (nla[NFTA_CHAIN_HOOK]) { |
98a381a7 PNA |
2373 | if (!nft_is_base_chain(chain)) { |
2374 | NL_SET_BAD_ATTR(extack, attr); | |
77a92189 | 2375 | return -EEXIST; |
98a381a7 | 2376 | } |
36596dad | 2377 | err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family, |
983c4fcb | 2378 | extack, false); |
2c4a488a PNA |
2379 | if (err < 0) |
2380 | return err; | |
2381 | ||
2382 | basechain = nft_base_chain(chain); | |
2383 | if (basechain->type != hook.type) { | |
2384 | nft_chain_release_hook(&hook); | |
98a381a7 | 2385 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2386 | return -EEXIST; |
2c4a488a PNA |
2387 | } |
2388 | ||
d3519cb8 | 2389 | if (nft_base_chain_netdev(ctx->family, hook.num)) { |
d54725cd PNA |
2390 | if (!nft_hook_list_equal(&basechain->hook_list, |
2391 | &hook.list)) { | |
2392 | nft_chain_release_hook(&hook); | |
98a381a7 | 2393 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2394 | return -EEXIST; |
d54725cd PNA |
2395 | } |
2396 | } else { | |
2397 | ops = &basechain->ops; | |
2398 | if (ops->hooknum != hook.num || | |
2399 | ops->priority != hook.priority) { | |
2400 | nft_chain_release_hook(&hook); | |
98a381a7 | 2401 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2402 | return -EEXIST; |
d54725cd | 2403 | } |
2c4a488a PNA |
2404 | } |
2405 | nft_chain_release_hook(&hook); | |
2406 | } | |
2407 | ||
2408 | if (nla[NFTA_CHAIN_HANDLE] && | |
2409 | nla[NFTA_CHAIN_NAME]) { | |
2410 | struct nft_chain *chain2; | |
2411 | ||
f102d66b FW |
2412 | chain2 = nft_chain_lookup(ctx->net, table, |
2413 | nla[NFTA_CHAIN_NAME], genmask); | |
98a381a7 PNA |
2414 | if (!IS_ERR(chain2)) { |
2415 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
0d18779b | 2416 | return -EEXIST; |
98a381a7 | 2417 | } |
2c4a488a PNA |
2418 | } |
2419 | ||
2420 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2421 | if (!nft_is_base_chain(chain)) | |
2422 | return -EOPNOTSUPP; | |
2423 | ||
2424 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2425 | if (IS_ERR(stats)) | |
2426 | return PTR_ERR(stats); | |
2427 | } | |
2428 | ||
c6cc94df | 2429 | err = -ENOMEM; |
2c4a488a PNA |
2430 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2431 | sizeof(struct nft_trans_chain)); | |
c6cc94df FW |
2432 | if (trans == NULL) |
2433 | goto err; | |
2c4a488a PNA |
2434 | |
2435 | nft_trans_chain_stats(trans) = stats; | |
2436 | nft_trans_chain_update(trans) = true; | |
2437 | ||
2438 | if (nla[NFTA_CHAIN_POLICY]) | |
2439 | nft_trans_chain_policy(trans) = policy; | |
2440 | else | |
2441 | nft_trans_chain_policy(trans) = -1; | |
2442 | ||
c6cc94df FW |
2443 | if (nla[NFTA_CHAIN_HANDLE] && |
2444 | nla[NFTA_CHAIN_NAME]) { | |
d59d2f82 | 2445 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c6cc94df FW |
2446 | struct nft_trans *tmp; |
2447 | char *name; | |
2448 | ||
2449 | err = -ENOMEM; | |
33758c89 | 2450 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
c6cc94df FW |
2451 | if (!name) |
2452 | goto err; | |
2453 | ||
2454 | err = -EEXIST; | |
0854db2a | 2455 | list_for_each_entry(tmp, &nft_net->commit_list, list) { |
c6cc94df FW |
2456 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && |
2457 | tmp->ctx.table == table && | |
2458 | nft_trans_chain_update(tmp) && | |
2459 | nft_trans_chain_name(tmp) && | |
2460 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
98a381a7 | 2461 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); |
c6cc94df FW |
2462 | kfree(name); |
2463 | goto err; | |
2464 | } | |
2c4a488a | 2465 | } |
c6cc94df FW |
2466 | |
2467 | nft_trans_chain_name(trans) = name; | |
2c4a488a | 2468 | } |
0854db2a | 2469 | nft_trans_commit_list_add_tail(ctx->net, trans); |
2c4a488a PNA |
2470 | |
2471 | return 0; | |
c6cc94df FW |
2472 | err: |
2473 | free_percpu(stats); | |
2474 | kfree(trans); | |
2475 | return err; | |
2c4a488a PNA |
2476 | } |
2477 | ||
837830a4 | 2478 | static struct nft_chain *nft_chain_lookup_byid(const struct net *net, |
95f466d2 | 2479 | const struct nft_table *table, |
837830a4 PNA |
2480 | const struct nlattr *nla) |
2481 | { | |
d59d2f82 | 2482 | struct nftables_pernet *nft_net = nft_pernet(net); |
837830a4 PNA |
2483 | u32 id = ntohl(nla_get_be32(nla)); |
2484 | struct nft_trans *trans; | |
2485 | ||
0854db2a | 2486 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
837830a4 PNA |
2487 | struct nft_chain *chain = trans->ctx.chain; |
2488 | ||
2489 | if (trans->msg_type == NFT_MSG_NEWCHAIN && | |
95f466d2 | 2490 | chain->table == table && |
837830a4 PNA |
2491 | id == nft_trans_chain_id(trans)) |
2492 | return chain; | |
2493 | } | |
2494 | return ERR_PTR(-ENOENT); | |
2495 | } | |
2496 | ||
7dab8ee3 PNA |
2497 | static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info, |
2498 | const struct nlattr * const nla[]) | |
96518518 | 2499 | { |
7dab8ee3 | 2500 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
2501 | struct netlink_ext_ack *extack = info->extack; |
2502 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2503 | u8 family = info->nfmsg->nfgen_family; |
74cccc3d | 2504 | struct nft_chain *chain = NULL; |
7dab8ee3 | 2505 | struct net *net = info->net; |
36dd1bcc | 2506 | const struct nlattr *attr; |
96518518 | 2507 | struct nft_table *table; |
57de2a0c | 2508 | u8 policy = NF_ACCEPT; |
4035285f | 2509 | struct nft_ctx ctx; |
96518518 | 2510 | u64 handle = 0; |
c9626a2c | 2511 | u32 flags = 0; |
96518518 | 2512 | |
0854db2a | 2513 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 2514 | |
6001a930 PNA |
2515 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2516 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2517 | if (IS_ERR(table)) { |
2518 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2519 | return PTR_ERR(table); |
36dd1bcc | 2520 | } |
96518518 | 2521 | |
96518518 | 2522 | chain = NULL; |
36dd1bcc | 2523 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2524 | |
2525 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2526 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2527 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2528 | if (IS_ERR(chain)) { |
2529 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2530 | return PTR_ERR(chain); |
36dd1bcc PNA |
2531 | } |
2532 | attr = nla[NFTA_CHAIN_HANDLE]; | |
74cccc3d | 2533 | } else if (nla[NFTA_CHAIN_NAME]) { |
f102d66b | 2534 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2535 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2536 | if (PTR_ERR(chain) != -ENOENT) { |
2537 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2538 | return PTR_ERR(chain); |
36dd1bcc | 2539 | } |
96518518 PM |
2540 | chain = NULL; |
2541 | } | |
74cccc3d PNA |
2542 | } else if (!nla[NFTA_CHAIN_ID]) { |
2543 | return -EINVAL; | |
96518518 PM |
2544 | } |
2545 | ||
57de2a0c | 2546 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2547 | if (chain != NULL && |
36dd1bcc PNA |
2548 | !nft_is_base_chain(chain)) { |
2549 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2550 | return -EOPNOTSUPP; |
36dd1bcc | 2551 | } |
d6b6cb1d PNA |
2552 | |
2553 | if (chain == NULL && | |
36dd1bcc PNA |
2554 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2555 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2556 | return -EOPNOTSUPP; |
36dd1bcc | 2557 | } |
57de2a0c | 2558 | |
8f46df18 | 2559 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2560 | switch (policy) { |
2561 | case NF_DROP: | |
2562 | case NF_ACCEPT: | |
2563 | break; | |
2564 | default: | |
2565 | return -EINVAL; | |
2566 | } | |
2567 | } | |
2568 | ||
c9626a2c PNA |
2569 | if (nla[NFTA_CHAIN_FLAGS]) |
2570 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2571 | else if (chain) |
2572 | flags = chain->flags; | |
c9626a2c | 2573 | |
c1f79a2e PNA |
2574 | if (flags & ~NFT_CHAIN_FLAGS) |
2575 | return -EOPNOTSUPP; | |
2576 | ||
7dab8ee3 | 2577 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
4035285f | 2578 | |
96518518 | 2579 | if (chain != NULL) { |
5dc52d83 PNA |
2580 | if (chain->flags & NFT_CHAIN_BINDING) |
2581 | return -EINVAL; | |
2582 | ||
7dab8ee3 | 2583 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 2584 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2585 | return -EEXIST; |
36dd1bcc | 2586 | } |
7dab8ee3 | 2587 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
2588 | return -EOPNOTSUPP; |
2589 | ||
67c49de4 | 2590 | flags |= chain->flags & NFT_CHAIN_BASE; |
98a381a7 PNA |
2591 | return nf_tables_updchain(&ctx, genmask, policy, flags, attr, |
2592 | extack); | |
96518518 PM |
2593 | } |
2594 | ||
983c4fcb | 2595 | return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack); |
96518518 PM |
2596 | } |
2597 | ||
7dab8ee3 PNA |
2598 | static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info, |
2599 | const struct nlattr * const nla[]) | |
96518518 | 2600 | { |
7dab8ee3 PNA |
2601 | struct netlink_ext_ack *extack = info->extack; |
2602 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2603 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 2604 | struct net *net = info->net; |
36dd1bcc | 2605 | const struct nlattr *attr; |
96518518 PM |
2606 | struct nft_table *table; |
2607 | struct nft_chain *chain; | |
9dee1474 | 2608 | struct nft_rule *rule; |
91c7b38d | 2609 | struct nft_ctx ctx; |
3ecbfd65 | 2610 | u64 handle; |
9dee1474 PNA |
2611 | u32 use; |
2612 | int err; | |
96518518 | 2613 | |
6001a930 PNA |
2614 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2615 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2616 | if (IS_ERR(table)) { |
2617 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2618 | return PTR_ERR(table); |
36dd1bcc | 2619 | } |
96518518 | 2620 | |
3ecbfd65 | 2621 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2622 | attr = nla[NFTA_CHAIN_HANDLE]; |
2623 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2624 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2625 | } else { |
36dd1bcc | 2626 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2627 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2628 | } |
36dd1bcc PNA |
2629 | if (IS_ERR(chain)) { |
2630 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2631 | return PTR_ERR(chain); |
36dd1bcc | 2632 | } |
9dee1474 | 2633 | |
7dab8ee3 | 2634 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
9dee1474 | 2635 | chain->use > 0) |
96518518 PM |
2636 | return -EBUSY; |
2637 | ||
7dab8ee3 | 2638 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0165d932 | 2639 | |
9dee1474 PNA |
2640 | use = chain->use; |
2641 | list_for_each_entry(rule, &chain->rules, list) { | |
2642 | if (!nft_is_active_next(net, rule)) | |
2643 | continue; | |
2644 | use--; | |
2645 | ||
2646 | err = nft_delrule(&ctx, rule); | |
2647 | if (err < 0) | |
2648 | return err; | |
2649 | } | |
2650 | ||
2651 | /* There are rules and elements that are still holding references to us, | |
2652 | * we cannot do a recursive removal in this case. | |
2653 | */ | |
36dd1bcc PNA |
2654 | if (use > 0) { |
2655 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2656 | return -EBUSY; |
36dd1bcc | 2657 | } |
9dee1474 | 2658 | |
ee01d542 | 2659 | return nft_delchain(&ctx); |
96518518 PM |
2660 | } |
2661 | ||
96518518 PM |
2662 | /* |
2663 | * Expressions | |
2664 | */ | |
2665 | ||
2666 | /** | |
ef1f7df9 | 2667 | * nft_register_expr - register nf_tables expr type |
3db86c39 | 2668 | * @type: expr type |
96518518 | 2669 | * |
ef1f7df9 | 2670 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2671 | * success or a negative errno code otherwise. |
2672 | */ | |
ef1f7df9 | 2673 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2674 | { |
2675 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2676 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2677 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2678 | else |
e688a7f8 | 2679 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2680 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2681 | return 0; | |
2682 | } | |
2683 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2684 | ||
2685 | /** | |
ef1f7df9 | 2686 | * nft_unregister_expr - unregister nf_tables expr type |
3db86c39 | 2687 | * @type: expr type |
96518518 | 2688 | * |
ef1f7df9 | 2689 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2690 | */ |
ef1f7df9 | 2691 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2692 | { |
2693 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2694 | list_del_rcu(&type->list); |
96518518 PM |
2695 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2696 | } | |
2697 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2698 | ||
64d46806 PM |
2699 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2700 | struct nlattr *nla) | |
96518518 | 2701 | { |
9cff126f | 2702 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2703 | |
ef1f7df9 | 2704 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2705 | if (!nla_strcmp(nla, type->name)) { |
2706 | if (!type->family && !candidate) | |
2707 | candidate = type; | |
2708 | else if (type->family == family) | |
2709 | candidate = type; | |
2710 | } | |
96518518 | 2711 | } |
9cff126f | 2712 | return candidate; |
96518518 PM |
2713 | } |
2714 | ||
b9c04ae7 PNA |
2715 | #ifdef CONFIG_MODULES |
2716 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
2717 | struct nlattr *nla) | |
2718 | { | |
eb014de4 PNA |
2719 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
2720 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
2721 | return -EAGAIN; |
2722 | ||
2723 | return 0; | |
2724 | } | |
2725 | #endif | |
2726 | ||
452238e8 FW |
2727 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
2728 | u8 family, | |
64d46806 | 2729 | struct nlattr *nla) |
96518518 | 2730 | { |
ef1f7df9 | 2731 | const struct nft_expr_type *type; |
96518518 PM |
2732 | |
2733 | if (nla == NULL) | |
2734 | return ERR_PTR(-EINVAL); | |
2735 | ||
64d46806 | 2736 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
2737 | if (type != NULL && try_module_get(type->owner)) |
2738 | return type; | |
96518518 | 2739 | |
f102d66b | 2740 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 2741 | #ifdef CONFIG_MODULES |
ef1f7df9 | 2742 | if (type == NULL) { |
b9c04ae7 | 2743 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
2744 | return ERR_PTR(-EAGAIN); |
2745 | ||
eb014de4 PNA |
2746 | if (nft_request_module(net, "nft-expr-%.*s", |
2747 | nla_len(nla), | |
2748 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
2749 | return ERR_PTR(-EAGAIN); |
2750 | } | |
2751 | #endif | |
2752 | return ERR_PTR(-ENOENT); | |
2753 | } | |
2754 | ||
2755 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
2756 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
2757 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
2758 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
2759 | }; | |
2760 | ||
2761 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
2762 | const struct nft_expr *expr) | |
2763 | { | |
ef1f7df9 | 2764 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
2765 | goto nla_put_failure; |
2766 | ||
2767 | if (expr->ops->dump) { | |
ae0be8de MK |
2768 | struct nlattr *data = nla_nest_start_noflag(skb, |
2769 | NFTA_EXPR_DATA); | |
96518518 PM |
2770 | if (data == NULL) |
2771 | goto nla_put_failure; | |
2772 | if (expr->ops->dump(skb, expr) < 0) | |
2773 | goto nla_put_failure; | |
2774 | nla_nest_end(skb, data); | |
2775 | } | |
2776 | ||
2777 | return skb->len; | |
2778 | ||
2779 | nla_put_failure: | |
2780 | return -1; | |
2781 | }; | |
2782 | ||
0b2d8a7b PM |
2783 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
2784 | const struct nft_expr *expr) | |
2785 | { | |
2786 | struct nlattr *nest; | |
2787 | ||
ae0be8de | 2788 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
2789 | if (!nest) |
2790 | goto nla_put_failure; | |
2791 | if (nf_tables_fill_expr_info(skb, expr) < 0) | |
2792 | goto nla_put_failure; | |
2793 | nla_nest_end(skb, nest); | |
2794 | return 0; | |
2795 | ||
2796 | nla_put_failure: | |
2797 | return -1; | |
2798 | } | |
2799 | ||
96518518 PM |
2800 | struct nft_expr_info { |
2801 | const struct nft_expr_ops *ops; | |
83d9dcba | 2802 | const struct nlattr *attr; |
ef1f7df9 | 2803 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
2804 | }; |
2805 | ||
0ca743a5 PNA |
2806 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
2807 | const struct nlattr *nla, | |
96518518 PM |
2808 | struct nft_expr_info *info) |
2809 | { | |
ef1f7df9 | 2810 | const struct nft_expr_type *type; |
96518518 | 2811 | const struct nft_expr_ops *ops; |
ef1f7df9 | 2812 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
2813 | int err; |
2814 | ||
8cb08174 JB |
2815 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
2816 | nft_expr_policy, NULL); | |
96518518 PM |
2817 | if (err < 0) |
2818 | return err; | |
2819 | ||
452238e8 | 2820 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
2821 | if (IS_ERR(type)) |
2822 | return PTR_ERR(type); | |
2823 | ||
2824 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
2825 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
2826 | tb[NFTA_EXPR_DATA], | |
2827 | type->policy, NULL); | |
ef1f7df9 PM |
2828 | if (err < 0) |
2829 | goto err1; | |
2830 | } else | |
2831 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
2832 | ||
2833 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
2834 | ops = type->select_ops(ctx, |
2835 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
2836 | if (IS_ERR(ops)) { |
2837 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
2838 | #ifdef CONFIG_MODULES |
2839 | if (err == -EAGAIN) | |
eb014de4 PNA |
2840 | if (nft_expr_type_request_module(ctx->net, |
2841 | ctx->family, | |
2842 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
2843 | err = -ENOENT; | |
0ef1efd1 | 2844 | #endif |
ef1f7df9 PM |
2845 | goto err1; |
2846 | } | |
2847 | } else | |
2848 | ops = type->ops; | |
2849 | ||
83d9dcba | 2850 | info->attr = nla; |
96518518 | 2851 | info->ops = ops; |
83d9dcba | 2852 | |
96518518 | 2853 | return 0; |
ef1f7df9 PM |
2854 | |
2855 | err1: | |
2856 | module_put(type->owner); | |
2857 | return err; | |
96518518 PM |
2858 | } |
2859 | ||
2860 | static int nf_tables_newexpr(const struct nft_ctx *ctx, | |
7dab8ee3 | 2861 | const struct nft_expr_info *expr_info, |
96518518 PM |
2862 | struct nft_expr *expr) |
2863 | { | |
7dab8ee3 | 2864 | const struct nft_expr_ops *ops = expr_info->ops; |
96518518 PM |
2865 | int err; |
2866 | ||
2867 | expr->ops = ops; | |
2868 | if (ops->init) { | |
7dab8ee3 | 2869 | err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb); |
96518518 PM |
2870 | if (err < 0) |
2871 | goto err1; | |
2872 | } | |
2873 | ||
96518518 | 2874 | return 0; |
96518518 PM |
2875 | err1: |
2876 | expr->ops = NULL; | |
2877 | return err; | |
2878 | } | |
2879 | ||
62472bce PM |
2880 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
2881 | struct nft_expr *expr) | |
96518518 | 2882 | { |
3f3a390d PNA |
2883 | const struct nft_expr_type *type = expr->ops->type; |
2884 | ||
96518518 | 2885 | if (expr->ops->destroy) |
62472bce | 2886 | expr->ops->destroy(ctx, expr); |
3f3a390d | 2887 | module_put(type->owner); |
96518518 PM |
2888 | } |
2889 | ||
795a6d6b PNA |
2890 | static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
2891 | const struct nlattr *nla) | |
0b2d8a7b | 2892 | { |
7dab8ee3 | 2893 | struct nft_expr_info expr_info; |
0b2d8a7b | 2894 | struct nft_expr *expr; |
b8e20400 | 2895 | struct module *owner; |
0b2d8a7b PM |
2896 | int err; |
2897 | ||
7dab8ee3 | 2898 | err = nf_tables_expr_parse(ctx, nla, &expr_info); |
0b2d8a7b | 2899 | if (err < 0) |
52077804 PNA |
2900 | goto err_expr_parse; |
2901 | ||
2902 | err = -EOPNOTSUPP; | |
2903 | if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL)) | |
2904 | goto err_expr_stateful; | |
0b2d8a7b PM |
2905 | |
2906 | err = -ENOMEM; | |
33758c89 | 2907 | expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT); |
0b2d8a7b | 2908 | if (expr == NULL) |
52077804 | 2909 | goto err_expr_stateful; |
0b2d8a7b | 2910 | |
7dab8ee3 | 2911 | err = nf_tables_newexpr(ctx, &expr_info, expr); |
0b2d8a7b | 2912 | if (err < 0) |
52077804 | 2913 | goto err_expr_new; |
0b2d8a7b PM |
2914 | |
2915 | return expr; | |
52077804 | 2916 | err_expr_new: |
6cafaf47 | 2917 | kfree(expr); |
52077804 | 2918 | err_expr_stateful: |
7dab8ee3 PNA |
2919 | owner = expr_info.ops->type->owner; |
2920 | if (expr_info.ops->type->release_ops) | |
2921 | expr_info.ops->type->release_ops(expr_info.ops); | |
b8e20400 PNA |
2922 | |
2923 | module_put(owner); | |
52077804 | 2924 | err_expr_parse: |
0b2d8a7b PM |
2925 | return ERR_PTR(err); |
2926 | } | |
2927 | ||
c604cc69 PNA |
2928 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
2929 | { | |
2930 | int err; | |
2931 | ||
2932 | if (src->ops->clone) { | |
2933 | dst->ops = src->ops; | |
2934 | err = src->ops->clone(dst, src); | |
2935 | if (err < 0) | |
2936 | return err; | |
2937 | } else { | |
2938 | memcpy(dst, src, src->ops->size); | |
2939 | } | |
2940 | ||
2941 | __module_get(src->ops->type->owner); | |
2942 | ||
2943 | return 0; | |
2944 | } | |
2945 | ||
0b2d8a7b PM |
2946 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
2947 | { | |
2948 | nf_tables_expr_destroy(ctx, expr); | |
2949 | kfree(expr); | |
2950 | } | |
2951 | ||
96518518 PM |
2952 | /* |
2953 | * Rules | |
2954 | */ | |
2955 | ||
cac20fcd PNA |
2956 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
2957 | u64 handle) | |
96518518 PM |
2958 | { |
2959 | struct nft_rule *rule; | |
2960 | ||
2961 | // FIXME: this sucks | |
d9adf22a | 2962 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
2963 | if (handle == rule->handle) |
2964 | return rule; | |
2965 | } | |
2966 | ||
2967 | return ERR_PTR(-ENOENT); | |
2968 | } | |
2969 | ||
cac20fcd PNA |
2970 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
2971 | const struct nlattr *nla) | |
96518518 PM |
2972 | { |
2973 | if (nla == NULL) | |
2974 | return ERR_PTR(-EINVAL); | |
2975 | ||
cac20fcd | 2976 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
2977 | } |
2978 | ||
2979 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
2980 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
2981 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
2982 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
2983 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
2984 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
2985 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 2986 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 2987 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
2988 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
2989 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 2990 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 2991 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
837830a4 | 2992 | [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
2993 | }; |
2994 | ||
84d7fce6 PNA |
2995 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
2996 | u32 portid, u32 seq, int event, | |
2997 | u32 flags, int family, | |
96518518 PM |
2998 | const struct nft_table *table, |
2999 | const struct nft_chain *chain, | |
e189ae16 | 3000 | const struct nft_rule *rule, u64 handle) |
96518518 PM |
3001 | { |
3002 | struct nlmsghdr *nlh; | |
96518518 PM |
3003 | const struct nft_expr *expr, *next; |
3004 | struct nlattr *list; | |
dedb67c4 | 3005 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 3006 | |
19c28b13 PNA |
3007 | nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0, |
3008 | nft_base_seq(net)); | |
3009 | if (!nlh) | |
96518518 PM |
3010 | goto nla_put_failure; |
3011 | ||
96518518 PM |
3012 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
3013 | goto nla_put_failure; | |
3014 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
3015 | goto nla_put_failure; | |
b46f6ded ND |
3016 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
3017 | NFTA_RULE_PAD)) | |
96518518 PM |
3018 | goto nla_put_failure; |
3019 | ||
e189ae16 PNA |
3020 | if (event != NFT_MSG_DELRULE && handle) { |
3021 | if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle), | |
b46f6ded | 3022 | NFTA_RULE_PAD)) |
5e948466 EL |
3023 | goto nla_put_failure; |
3024 | } | |
3025 | ||
b72920f6 PNA |
3026 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) |
3027 | nft_flow_rule_stats(chain, rule); | |
3028 | ||
ae0be8de | 3029 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
3030 | if (list == NULL) |
3031 | goto nla_put_failure; | |
3032 | nft_rule_for_each_expr(expr, next, rule) { | |
0b2d8a7b | 3033 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
96518518 | 3034 | goto nla_put_failure; |
96518518 PM |
3035 | } |
3036 | nla_nest_end(skb, list); | |
3037 | ||
86f1ec32 PM |
3038 | if (rule->udata) { |
3039 | struct nft_userdata *udata = nft_userdata(rule); | |
3040 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
3041 | udata->data) < 0) | |
3042 | goto nla_put_failure; | |
3043 | } | |
0768b3b3 | 3044 | |
053c095a JB |
3045 | nlmsg_end(skb, nlh); |
3046 | return 0; | |
96518518 PM |
3047 | |
3048 | nla_put_failure: | |
3049 | nlmsg_trim(skb, nlh); | |
3050 | return -1; | |
3051 | } | |
3052 | ||
25e94a99 PNA |
3053 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
3054 | const struct nft_rule *rule, int event) | |
96518518 | 3055 | { |
d59d2f82 | 3056 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
e189ae16 | 3057 | const struct nft_rule *prule; |
96518518 | 3058 | struct sk_buff *skb; |
e189ae16 PNA |
3059 | u64 handle = 0; |
3060 | u16 flags = 0; | |
96518518 PM |
3061 | int err; |
3062 | ||
128ad332 PNA |
3063 | if (!ctx->report && |
3064 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3065 | return; |
96518518 | 3066 | |
96518518 PM |
3067 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
3068 | if (skb == NULL) | |
3069 | goto err; | |
3070 | ||
e189ae16 PNA |
3071 | if (event == NFT_MSG_NEWRULE && |
3072 | !list_is_first(&rule->list, &ctx->chain->rules) && | |
3073 | !list_is_last(&rule->list, &ctx->chain->rules)) { | |
3074 | prule = list_prev_entry(rule, list); | |
3075 | handle = prule->handle; | |
3076 | } | |
3077 | if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE)) | |
3078 | flags |= NLM_F_APPEND; | |
6fb721cf PNA |
3079 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
3080 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
e189ae16 | 3081 | |
84d7fce6 | 3082 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
e189ae16 PNA |
3083 | event, flags, ctx->family, ctx->table, |
3084 | ctx->chain, rule, handle); | |
96518518 PM |
3085 | if (err < 0) { |
3086 | kfree_skb(skb); | |
3087 | goto err; | |
3088 | } | |
3089 | ||
0854db2a | 3090 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 3091 | return; |
96518518 | 3092 | err: |
25e94a99 | 3093 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
3094 | } |
3095 | ||
6e1f760e | 3096 | struct nft_rule_dump_ctx { |
e46abbcc | 3097 | char *table; |
b7263e07 | 3098 | char *chain; |
6e1f760e PNA |
3099 | }; |
3100 | ||
241faece PS |
3101 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
3102 | unsigned int *idx, | |
3103 | struct netlink_callback *cb, | |
3104 | const struct nft_table *table, | |
3105 | const struct nft_chain *chain) | |
3106 | { | |
3107 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 3108 | const struct nft_rule *rule, *prule; |
241faece | 3109 | unsigned int s_idx = cb->args[0]; |
e189ae16 | 3110 | u64 handle; |
241faece | 3111 | |
2c82c7e7 | 3112 | prule = NULL; |
241faece PS |
3113 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
3114 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 3115 | goto cont_skip; |
241faece PS |
3116 | if (*idx < s_idx) |
3117 | goto cont; | |
3118 | if (*idx > s_idx) { | |
3119 | memset(&cb->args[1], 0, | |
3120 | sizeof(cb->args) - sizeof(cb->args[0])); | |
3121 | } | |
e189ae16 PNA |
3122 | if (prule) |
3123 | handle = prule->handle; | |
3124 | else | |
3125 | handle = 0; | |
3126 | ||
241faece PS |
3127 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
3128 | cb->nlh->nlmsg_seq, | |
3129 | NFT_MSG_NEWRULE, | |
3130 | NLM_F_MULTI | NLM_F_APPEND, | |
3131 | table->family, | |
e189ae16 | 3132 | table, chain, rule, handle) < 0) |
310529e6 | 3133 | return 1; |
241faece PS |
3134 | |
3135 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
3136 | cont: | |
2c82c7e7 FW |
3137 | prule = rule; |
3138 | cont_skip: | |
241faece PS |
3139 | (*idx)++; |
3140 | } | |
310529e6 | 3141 | return 0; |
241faece PS |
3142 | } |
3143 | ||
96518518 PM |
3144 | static int nf_tables_dump_rules(struct sk_buff *skb, |
3145 | struct netlink_callback *cb) | |
3146 | { | |
3147 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 3148 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 3149 | struct nft_table *table; |
96518518 | 3150 | const struct nft_chain *chain; |
241faece | 3151 | unsigned int idx = 0; |
99633ab2 | 3152 | struct net *net = sock_net(skb->sk); |
96518518 | 3153 | int family = nfmsg->nfgen_family; |
0854db2a | 3154 | struct nftables_pernet *nft_net; |
96518518 | 3155 | |
e688a7f8 | 3156 | rcu_read_lock(); |
d59d2f82 | 3157 | nft_net = nft_pernet(net); |
34002783 | 3158 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 3159 | |
0854db2a | 3160 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 3161 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
3162 | continue; |
3163 | ||
3164 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
3165 | continue; |
3166 | ||
715849ab | 3167 | if (ctx && ctx->table && ctx->chain) { |
241faece | 3168 | struct rhlist_head *list, *tmp; |
6e1f760e | 3169 | |
241faece PS |
3170 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
3171 | nft_chain_ht_params); | |
3172 | if (!list) | |
3173 | goto done; | |
3174 | ||
3175 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
3176 | if (!nft_is_active(net, chain)) | |
3177 | continue; | |
3178 | __nf_tables_dump_rules(skb, &idx, | |
3179 | cb, table, chain); | |
3180 | break; | |
96518518 | 3181 | } |
241faece | 3182 | goto done; |
96518518 | 3183 | } |
241faece PS |
3184 | |
3185 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
3186 | if (__nf_tables_dump_rules(skb, &idx, cb, table, chain)) | |
3187 | goto done; | |
3188 | } | |
3189 | ||
3190 | if (ctx && ctx->table) | |
3191 | break; | |
96518518 PM |
3192 | } |
3193 | done: | |
e688a7f8 | 3194 | rcu_read_unlock(); |
310529e6 PS |
3195 | |
3196 | cb->args[0] = idx; | |
96518518 PM |
3197 | return skb->len; |
3198 | } | |
3199 | ||
90fd131a FW |
3200 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
3201 | { | |
3202 | const struct nlattr * const *nla = cb->data; | |
3203 | struct nft_rule_dump_ctx *ctx = NULL; | |
3204 | ||
3205 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
3206 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
3207 | if (!ctx) | |
3208 | return -ENOMEM; | |
3209 | ||
3210 | if (nla[NFTA_RULE_TABLE]) { | |
3211 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
3212 | GFP_ATOMIC); | |
3213 | if (!ctx->table) { | |
3214 | kfree(ctx); | |
3215 | return -ENOMEM; | |
3216 | } | |
3217 | } | |
3218 | if (nla[NFTA_RULE_CHAIN]) { | |
3219 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
3220 | GFP_ATOMIC); | |
3221 | if (!ctx->chain) { | |
3222 | kfree(ctx->table); | |
3223 | kfree(ctx); | |
3224 | return -ENOMEM; | |
3225 | } | |
3226 | } | |
3227 | } | |
3228 | ||
3229 | cb->data = ctx; | |
3230 | return 0; | |
3231 | } | |
3232 | ||
6e1f760e PNA |
3233 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
3234 | { | |
e46abbcc PS |
3235 | struct nft_rule_dump_ctx *ctx = cb->data; |
3236 | ||
3237 | if (ctx) { | |
3238 | kfree(ctx->table); | |
b7263e07 | 3239 | kfree(ctx->chain); |
e46abbcc PS |
3240 | kfree(ctx); |
3241 | } | |
6e1f760e PNA |
3242 | return 0; |
3243 | } | |
3244 | ||
d9adf22a | 3245 | /* called with rcu_read_lock held */ |
797d4980 PNA |
3246 | static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info, |
3247 | const struct nlattr * const nla[]) | |
96518518 | 3248 | { |
797d4980 PNA |
3249 | struct netlink_ext_ack *extack = info->extack; |
3250 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 3251 | u8 family = info->nfmsg->nfgen_family; |
96518518 PM |
3252 | const struct nft_chain *chain; |
3253 | const struct nft_rule *rule; | |
797d4980 | 3254 | struct net *net = info->net; |
1b2470e5 | 3255 | struct nft_table *table; |
96518518 | 3256 | struct sk_buff *skb2; |
96518518 PM |
3257 | int err; |
3258 | ||
797d4980 | 3259 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 | 3260 | struct netlink_dump_control c = { |
90fd131a | 3261 | .start= nf_tables_dump_rules_start, |
96518518 | 3262 | .dump = nf_tables_dump_rules, |
6e1f760e | 3263 | .done = nf_tables_dump_rules_done, |
d9adf22a | 3264 | .module = THIS_MODULE, |
90fd131a | 3265 | .data = (void *)nla, |
96518518 | 3266 | }; |
6e1f760e | 3267 | |
797d4980 | 3268 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
3269 | } |
3270 | ||
6001a930 | 3271 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0); |
36dd1bcc PNA |
3272 | if (IS_ERR(table)) { |
3273 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3274 | return PTR_ERR(table); |
36dd1bcc | 3275 | } |
96518518 | 3276 | |
f102d66b | 3277 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3278 | if (IS_ERR(chain)) { |
3279 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3280 | return PTR_ERR(chain); |
36dd1bcc | 3281 | } |
96518518 | 3282 | |
cac20fcd | 3283 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3284 | if (IS_ERR(rule)) { |
3285 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3286 | return PTR_ERR(rule); |
36dd1bcc | 3287 | } |
96518518 | 3288 | |
d9adf22a | 3289 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
3290 | if (!skb2) |
3291 | return -ENOMEM; | |
3292 | ||
84d7fce6 | 3293 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 3294 | info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
e189ae16 | 3295 | family, table, chain, rule, 0); |
96518518 | 3296 | if (err < 0) |
ee921183 | 3297 | goto err_fill_rule_info; |
96518518 | 3298 | |
ee921183 | 3299 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 3300 | |
ee921183 | 3301 | err_fill_rule_info: |
96518518 PM |
3302 | kfree_skb(skb2); |
3303 | return err; | |
3304 | } | |
3305 | ||
62472bce PM |
3306 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
3307 | struct nft_rule *rule) | |
96518518 | 3308 | { |
29e38801 | 3309 | struct nft_expr *expr, *next; |
96518518 PM |
3310 | |
3311 | /* | |
3312 | * Careful: some expressions might not be initialized in case this | |
3313 | * is called on error from nf_tables_newrule(). | |
3314 | */ | |
3315 | expr = nft_expr_first(rule); | |
31cc578a | 3316 | while (nft_expr_more(rule, expr)) { |
29e38801 | 3317 | next = nft_expr_next(expr); |
62472bce | 3318 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 3319 | expr = next; |
96518518 PM |
3320 | } |
3321 | kfree(rule); | |
3322 | } | |
3323 | ||
d0e2c7de | 3324 | void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae | 3325 | { |
f6ac8585 | 3326 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
3327 | nf_tables_rule_destroy(ctx, rule); |
3328 | } | |
3329 | ||
a654de8f PNA |
3330 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
3331 | { | |
3332 | struct nft_expr *expr, *last; | |
3333 | const struct nft_data *data; | |
3334 | struct nft_rule *rule; | |
3335 | int err; | |
3336 | ||
26b2f552 TY |
3337 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
3338 | return -EMLINK; | |
3339 | ||
a654de8f PNA |
3340 | list_for_each_entry(rule, &chain->rules, list) { |
3341 | if (!nft_is_active_next(ctx->net, rule)) | |
3342 | continue; | |
3343 | ||
3344 | nft_rule_for_each_expr(expr, last, rule) { | |
3345 | if (!expr->ops->validate) | |
3346 | continue; | |
3347 | ||
3348 | err = expr->ops->validate(ctx, expr, &data); | |
3349 | if (err < 0) | |
3350 | return err; | |
3351 | } | |
81ea0106 FW |
3352 | |
3353 | cond_resched(); | |
a654de8f PNA |
3354 | } |
3355 | ||
3356 | return 0; | |
3357 | } | |
3358 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
3359 | ||
3360 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
3361 | { | |
3362 | struct nft_chain *chain; | |
3363 | struct nft_ctx ctx = { | |
3364 | .net = net, | |
3365 | .family = table->family, | |
3366 | }; | |
3367 | int err; | |
3368 | ||
3369 | list_for_each_entry(chain, &table->chains, list) { | |
3370 | if (!nft_is_base_chain(chain)) | |
3371 | continue; | |
3372 | ||
3373 | ctx.chain = chain; | |
3374 | err = nft_chain_validate(&ctx, chain); | |
3375 | if (err < 0) | |
3376 | return err; | |
3377 | } | |
3378 | ||
3379 | return 0; | |
3380 | } | |
3381 | ||
75dd48e2 | 3382 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3383 | const struct nft_chain *chain, |
75dd48e2 PS |
3384 | const struct nlattr *nla); |
3385 | ||
1081d11b PNA |
3386 | #define NFT_RULE_MAXEXPRS 128 |
3387 | ||
7dab8ee3 PNA |
3388 | static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, |
3389 | const struct nlattr * const nla[]) | |
96518518 | 3390 | { |
7dab8ee3 | 3391 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
3392 | struct netlink_ext_ack *extack = info->extack; |
3393 | unsigned int size, i, n, ulen = 0, usize = 0; | |
3394 | u8 genmask = nft_genmask_next(info->net); | |
3395 | struct nft_rule *rule, *old_rule = NULL; | |
3396 | struct nft_expr_info *expr_info = NULL; | |
ef4b65e5 | 3397 | u8 family = info->nfmsg->nfgen_family; |
3c5e4462 | 3398 | struct nft_flow_rule *flow = NULL; |
7dab8ee3 | 3399 | struct net *net = info->net; |
7dab8ee3 | 3400 | struct nft_userdata *udata; |
96518518 PM |
3401 | struct nft_table *table; |
3402 | struct nft_chain *chain; | |
7dab8ee3 PNA |
3403 | struct nft_trans *trans; |
3404 | u64 handle, pos_handle; | |
96518518 PM |
3405 | struct nft_expr *expr; |
3406 | struct nft_ctx ctx; | |
3407 | struct nlattr *tmp; | |
96518518 | 3408 | int err, rem; |
96518518 | 3409 | |
0854db2a | 3410 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3411 | |
6001a930 PNA |
3412 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3413 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3414 | if (IS_ERR(table)) { |
3415 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3416 | return PTR_ERR(table); |
36dd1bcc | 3417 | } |
96518518 | 3418 | |
837830a4 PNA |
3419 | if (nla[NFTA_RULE_CHAIN]) { |
3420 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], | |
3421 | genmask); | |
3422 | if (IS_ERR(chain)) { | |
3423 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
3424 | return PTR_ERR(chain); | |
3425 | } | |
d0e2c7de PNA |
3426 | if (nft_chain_is_bound(chain)) |
3427 | return -EOPNOTSUPP; | |
3428 | ||
837830a4 | 3429 | } else if (nla[NFTA_RULE_CHAIN_ID]) { |
95f466d2 | 3430 | chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID]); |
837830a4 PNA |
3431 | if (IS_ERR(chain)) { |
3432 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]); | |
3433 | return PTR_ERR(chain); | |
3434 | } | |
3435 | } else { | |
3436 | return -EINVAL; | |
36dd1bcc | 3437 | } |
96518518 PM |
3438 | |
3439 | if (nla[NFTA_RULE_HANDLE]) { | |
3440 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3441 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3442 | if (IS_ERR(rule)) { |
3443 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3444 | return PTR_ERR(rule); |
36dd1bcc | 3445 | } |
96518518 | 3446 | |
7dab8ee3 | 3447 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 3448 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
96518518 | 3449 | return -EEXIST; |
36dd1bcc | 3450 | } |
7dab8ee3 | 3451 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
3452 | old_rule = rule; |
3453 | else | |
3454 | return -EOPNOTSUPP; | |
3455 | } else { | |
7dab8ee3 PNA |
3456 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) || |
3457 | info->nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3458 | return -EINVAL; |
3459 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
3460 | |
3461 | if (chain->use == UINT_MAX) | |
3462 | return -EOVERFLOW; | |
5e948466 | 3463 | |
447750f2 FW |
3464 | if (nla[NFTA_RULE_POSITION]) { |
3465 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3466 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3467 | if (IS_ERR(old_rule)) { | |
3468 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3469 | return PTR_ERR(old_rule); | |
3470 | } | |
75dd48e2 | 3471 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
36d5b291 | 3472 | old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]); |
75dd48e2 PS |
3473 | if (IS_ERR(old_rule)) { |
3474 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3475 | return PTR_ERR(old_rule); | |
3476 | } | |
36dd1bcc | 3477 | } |
5e948466 EL |
3478 | } |
3479 | ||
7dab8ee3 | 3480 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0ca743a5 | 3481 | |
96518518 PM |
3482 | n = 0; |
3483 | size = 0; | |
3484 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
7dab8ee3 PNA |
3485 | expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3486 | sizeof(struct nft_expr_info), | |
3487 | GFP_KERNEL); | |
3488 | if (!expr_info) | |
2a43ecf9 FW |
3489 | return -ENOMEM; |
3490 | ||
96518518 PM |
3491 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3492 | err = -EINVAL; | |
3493 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3c5e4462 | 3494 | goto err_release_expr; |
96518518 | 3495 | if (n == NFT_RULE_MAXEXPRS) |
3c5e4462 | 3496 | goto err_release_expr; |
7dab8ee3 | 3497 | err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]); |
c781471d PNA |
3498 | if (err < 0) { |
3499 | NL_SET_BAD_ATTR(extack, tmp); | |
3c5e4462 | 3500 | goto err_release_expr; |
c781471d | 3501 | } |
7dab8ee3 | 3502 | size += expr_info[n].ops->size; |
96518518 PM |
3503 | n++; |
3504 | } | |
3505 | } | |
9889840f PM |
3506 | /* Check for overflow of dlen field */ |
3507 | err = -EFBIG; | |
3508 | if (size >= 1 << 12) | |
3c5e4462 | 3509 | goto err_release_expr; |
96518518 | 3510 | |
86f1ec32 | 3511 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3512 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3513 | if (ulen > 0) |
3514 | usize = sizeof(struct nft_userdata) + ulen; | |
3515 | } | |
0768b3b3 | 3516 | |
96518518 | 3517 | err = -ENOMEM; |
33758c89 | 3518 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT); |
96518518 | 3519 | if (rule == NULL) |
3c5e4462 | 3520 | goto err_release_expr; |
96518518 | 3521 | |
889f7ee7 | 3522 | nft_activate_next(net, rule); |
0628b123 | 3523 | |
96518518 PM |
3524 | rule->handle = handle; |
3525 | rule->dlen = size; | |
86f1ec32 | 3526 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3527 | |
86f1ec32 PM |
3528 | if (ulen) { |
3529 | udata = nft_userdata(rule); | |
3530 | udata->len = ulen - 1; | |
3531 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3532 | } | |
96518518 | 3533 | |
96518518 PM |
3534 | expr = nft_expr_first(rule); |
3535 | for (i = 0; i < n; i++) { | |
7dab8ee3 | 3536 | err = nf_tables_newexpr(&ctx, &expr_info[i], expr); |
83d9dcba | 3537 | if (err < 0) { |
7dab8ee3 | 3538 | NL_SET_BAD_ATTR(extack, expr_info[i].attr); |
3c5e4462 | 3539 | goto err_release_rule; |
83d9dcba | 3540 | } |
a654de8f | 3541 | |
7dab8ee3 | 3542 | if (expr_info[i].ops->validate) |
a654de8f PNA |
3543 | nft_validate_state_update(net, NFT_VALIDATE_NEED); |
3544 | ||
7dab8ee3 | 3545 | expr_info[i].ops = NULL; |
96518518 PM |
3546 | expr = nft_expr_next(expr); |
3547 | } | |
3548 | ||
3c5e4462 PNA |
3549 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
3550 | flow = nft_flow_rule_create(net, rule); | |
3551 | if (IS_ERR(flow)) { | |
3552 | err = PTR_ERR(flow); | |
3553 | goto err_release_rule; | |
3554 | } | |
3555 | } | |
3556 | ||
7dab8ee3 | 3557 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) { |
2c964c55 PNA |
3558 | err = nft_delrule(&ctx, old_rule); |
3559 | if (err < 0) | |
3560 | goto err_destroy_flow_rule; | |
3561 | ||
ca089878 | 3562 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3563 | if (trans == NULL) { |
3564 | err = -ENOMEM; | |
3c5e4462 | 3565 | goto err_destroy_flow_rule; |
569ccae6 | 3566 | } |
569ccae6 FW |
3567 | list_add_tail_rcu(&rule->list, &old_rule->list); |
3568 | } else { | |
c9626a2c PNA |
3569 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3570 | if (!trans) { | |
569ccae6 | 3571 | err = -ENOMEM; |
3c5e4462 | 3572 | goto err_destroy_flow_rule; |
569ccae6 FW |
3573 | } |
3574 | ||
7dab8ee3 | 3575 | if (info->nlh->nlmsg_flags & NLM_F_APPEND) { |
569ccae6 FW |
3576 | if (old_rule) |
3577 | list_add_rcu(&rule->list, &old_rule->list); | |
3578 | else | |
3579 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3580 | } else { | |
3581 | if (old_rule) | |
3582 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3583 | else | |
3584 | list_add_rcu(&rule->list, &chain->rules); | |
3585 | } | |
0628b123 | 3586 | } |
7dab8ee3 | 3587 | kvfree(expr_info); |
4fefee57 | 3588 | chain->use++; |
96518518 | 3589 | |
3c5e4462 PNA |
3590 | if (flow) |
3591 | nft_trans_flow_rule(trans) = flow; | |
3592 | ||
0854db2a | 3593 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
a654de8f PNA |
3594 | return nft_table_validate(net, table); |
3595 | ||
3596 | return 0; | |
3c5e4462 PNA |
3597 | |
3598 | err_destroy_flow_rule: | |
4ca041f9 CIK |
3599 | if (flow) |
3600 | nft_flow_rule_destroy(flow); | |
3c5e4462 | 3601 | err_release_rule: |
bb7b40ae | 3602 | nf_tables_rule_release(&ctx, rule); |
3c5e4462 | 3603 | err_release_expr: |
96518518 | 3604 | for (i = 0; i < n; i++) { |
7dab8ee3 PNA |
3605 | if (expr_info[i].ops) { |
3606 | module_put(expr_info[i].ops->type->owner); | |
3607 | if (expr_info[i].ops->type->release_ops) | |
3608 | expr_info[i].ops->type->release_ops(expr_info[i].ops); | |
b25a31bf | 3609 | } |
96518518 | 3610 | } |
7dab8ee3 PNA |
3611 | kvfree(expr_info); |
3612 | ||
96518518 PM |
3613 | return err; |
3614 | } | |
3615 | ||
1a94e38d | 3616 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3617 | const struct nft_chain *chain, |
1a94e38d PNA |
3618 | const struct nlattr *nla) |
3619 | { | |
d59d2f82 | 3620 | struct nftables_pernet *nft_net = nft_pernet(net); |
1a94e38d PNA |
3621 | u32 id = ntohl(nla_get_be32(nla)); |
3622 | struct nft_trans *trans; | |
3623 | ||
0854db2a | 3624 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
1a94e38d PNA |
3625 | struct nft_rule *rule = nft_trans_rule(trans); |
3626 | ||
3627 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
36d5b291 | 3628 | trans->ctx.chain == chain && |
1a94e38d PNA |
3629 | id == nft_trans_rule_id(trans)) |
3630 | return rule; | |
3631 | } | |
3632 | return ERR_PTR(-ENOENT); | |
3633 | } | |
3634 | ||
7dab8ee3 PNA |
3635 | static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info, |
3636 | const struct nlattr * const nla[]) | |
96518518 | 3637 | { |
7dab8ee3 | 3638 | struct netlink_ext_ack *extack = info->extack; |
7dab8ee3 | 3639 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 3640 | u8 family = info->nfmsg->nfgen_family; |
cf9dc09d | 3641 | struct nft_chain *chain = NULL; |
7dab8ee3 PNA |
3642 | struct net *net = info->net; |
3643 | struct nft_table *table; | |
cf9dc09d | 3644 | struct nft_rule *rule; |
0628b123 | 3645 | struct nft_ctx ctx; |
ef4b65e5 | 3646 | int err = 0; |
96518518 | 3647 | |
6001a930 PNA |
3648 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3649 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3650 | if (IS_ERR(table)) { |
3651 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3652 | return PTR_ERR(table); |
36dd1bcc | 3653 | } |
96518518 | 3654 | |
cf9dc09d | 3655 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
3656 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
3657 | genmask); | |
36dd1bcc PNA |
3658 | if (IS_ERR(chain)) { |
3659 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
cf9dc09d | 3660 | return PTR_ERR(chain); |
36dd1bcc | 3661 | } |
d0e2c7de PNA |
3662 | if (nft_chain_is_bound(chain)) |
3663 | return -EOPNOTSUPP; | |
cf9dc09d | 3664 | } |
96518518 | 3665 | |
7dab8ee3 | 3666 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0628b123 | 3667 | |
cf9dc09d PNA |
3668 | if (chain) { |
3669 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 3670 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3671 | if (IS_ERR(rule)) { |
3672 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
cf9dc09d | 3673 | return PTR_ERR(rule); |
36dd1bcc | 3674 | } |
96518518 | 3675 | |
1a94e38d PNA |
3676 | err = nft_delrule(&ctx, rule); |
3677 | } else if (nla[NFTA_RULE_ID]) { | |
36d5b291 | 3678 | rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]); |
36dd1bcc PNA |
3679 | if (IS_ERR(rule)) { |
3680 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 3681 | return PTR_ERR(rule); |
36dd1bcc | 3682 | } |
1a94e38d | 3683 | |
5e266fe7 | 3684 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 3685 | } else { |
ce24b721 | 3686 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
3687 | } |
3688 | } else { | |
3689 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
3690 | if (!nft_is_active_next(net, chain)) |
3691 | continue; | |
3692 | ||
cf9dc09d | 3693 | ctx.chain = chain; |
ce24b721 | 3694 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
3695 | if (err < 0) |
3696 | break; | |
3697 | } | |
3698 | } | |
3699 | ||
3700 | return err; | |
3701 | } | |
3702 | ||
20a69341 PM |
3703 | /* |
3704 | * Sets | |
3705 | */ | |
e32a4dc6 FW |
3706 | static const struct nft_set_type *nft_set_types[] = { |
3707 | &nft_set_hash_fast_type, | |
3708 | &nft_set_hash_type, | |
3709 | &nft_set_rhash_type, | |
3710 | &nft_set_bitmap_type, | |
3711 | &nft_set_rbtree_type, | |
e6abef61 | 3712 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
7400b063 SB |
3713 | &nft_set_pipapo_avx2_type, |
3714 | #endif | |
e32a4dc6 FW |
3715 | &nft_set_pipapo_type, |
3716 | }; | |
20a69341 | 3717 | |
2b664957 | 3718 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
3719 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
3720 | NFT_SET_EVAL) | |
2b664957 | 3721 | |
71cc0873 | 3722 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 3723 | { |
71cc0873 | 3724 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
3725 | } |
3726 | ||
c50b960c PM |
3727 | /* |
3728 | * Select a set implementation based on the data characteristics and the | |
3729 | * given policy. The total memory use might not be known if no size is | |
3730 | * given, in that case the amount of memory per element is used. | |
3731 | */ | |
3732 | static const struct nft_set_ops * | |
2b664957 PNA |
3733 | nft_select_set_ops(const struct nft_ctx *ctx, |
3734 | const struct nlattr * const nla[], | |
c50b960c PM |
3735 | const struct nft_set_desc *desc, |
3736 | enum nft_set_policies policy) | |
20a69341 | 3737 | { |
d59d2f82 | 3738 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c50b960c PM |
3739 | const struct nft_set_ops *ops, *bops; |
3740 | struct nft_set_estimate est, best; | |
2b664957 PNA |
3741 | const struct nft_set_type *type; |
3742 | u32 flags = 0; | |
e32a4dc6 | 3743 | int i; |
20a69341 | 3744 | |
0854db2a | 3745 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3746 | lockdep_nfnl_nft_mutex_not_held(); |
e32a4dc6 | 3747 | |
2b664957 PNA |
3748 | if (nla[NFTA_SET_FLAGS] != NULL) |
3749 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 3750 | |
55af753c PNA |
3751 | bops = NULL; |
3752 | best.size = ~0; | |
3753 | best.lookup = ~0; | |
0b5a7874 | 3754 | best.space = ~0; |
c50b960c | 3755 | |
e32a4dc6 FW |
3756 | for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) { |
3757 | type = nft_set_types[i]; | |
71cc0873 | 3758 | ops = &type->ops; |
2b664957 | 3759 | |
71cc0873 | 3760 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 3761 | continue; |
2b664957 | 3762 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
3763 | continue; |
3764 | ||
3765 | switch (policy) { | |
3766 | case NFT_SET_POL_PERFORMANCE: | |
55af753c | 3767 | if (est.lookup < best.lookup) |
c50b960c | 3768 | break; |
644e334e PNA |
3769 | if (est.lookup == best.lookup && |
3770 | est.space < best.space) | |
3771 | break; | |
c50b960c PM |
3772 | continue; |
3773 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
3774 | if (!desc->size) { |
3775 | if (est.space < best.space) | |
3776 | break; | |
3777 | if (est.space == best.space && | |
3778 | est.lookup < best.lookup) | |
3779 | break; | |
4f2921ca | 3780 | } else if (est.size < best.size || !bops) { |
c50b960c | 3781 | break; |
0b5a7874 | 3782 | } |
c50b960c PM |
3783 | continue; |
3784 | default: | |
3785 | break; | |
3786 | } | |
3787 | ||
c50b960c PM |
3788 | bops = ops; |
3789 | best = est; | |
20a69341 PM |
3790 | } |
3791 | ||
c50b960c PM |
3792 | if (bops != NULL) |
3793 | return bops; | |
3794 | ||
20a69341 PM |
3795 | return ERR_PTR(-EOPNOTSUPP); |
3796 | } | |
3797 | ||
3798 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
3799 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
3800 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 3801 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 3802 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
3803 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
3804 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
3805 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
3806 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
3807 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
3808 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
3809 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 3810 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
3811 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
3812 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
3813 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
3814 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 3815 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 3816 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
65038428 | 3817 | [NFTA_SET_EXPR] = { .type = NLA_NESTED }, |
48b0ae04 | 3818 | [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED }, |
c50b960c PM |
3819 | }; |
3820 | ||
3821 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
3822 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 3823 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
3824 | }; |
3825 | ||
cac20fcd PNA |
3826 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
3827 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
3828 | { |
3829 | struct nft_set *set; | |
3830 | ||
3831 | if (nla == NULL) | |
3832 | return ERR_PTR(-EINVAL); | |
3833 | ||
d9adf22a | 3834 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
3835 | if (!nla_strcmp(nla, set->name) && |
3836 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
3837 | return set; |
3838 | } | |
3839 | return ERR_PTR(-ENOENT); | |
3840 | } | |
3841 | ||
cac20fcd PNA |
3842 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
3843 | const struct nlattr *nla, | |
3844 | u8 genmask) | |
3ecbfd65 HS |
3845 | { |
3846 | struct nft_set *set; | |
3847 | ||
3ecbfd65 HS |
3848 | list_for_each_entry(set, &table->sets, list) { |
3849 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
3850 | nft_active_genmask(set, genmask)) | |
3851 | return set; | |
3852 | } | |
3853 | return ERR_PTR(-ENOENT); | |
3854 | } | |
3855 | ||
cac20fcd | 3856 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
470ee20e | 3857 | const struct nft_table *table, |
cac20fcd | 3858 | const struct nlattr *nla, u8 genmask) |
958bee14 | 3859 | { |
d59d2f82 | 3860 | struct nftables_pernet *nft_net = nft_pernet(net); |
958bee14 | 3861 | u32 id = ntohl(nla_get_be32(nla)); |
d59d2f82 | 3862 | struct nft_trans *trans; |
958bee14 | 3863 | |
0854db2a | 3864 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
9c7f96fd AK |
3865 | if (trans->msg_type == NFT_MSG_NEWSET) { |
3866 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 3867 | |
9c7f96fd | 3868 | if (id == nft_trans_set_id(trans) && |
470ee20e | 3869 | set->table == table && |
9c7f96fd AK |
3870 | nft_active_genmask(set, genmask)) |
3871 | return set; | |
3872 | } | |
958bee14 PNA |
3873 | } |
3874 | return ERR_PTR(-ENOENT); | |
3875 | } | |
c7a72e3f | 3876 | |
10659cba PNA |
3877 | struct nft_set *nft_set_lookup_global(const struct net *net, |
3878 | const struct nft_table *table, | |
3879 | const struct nlattr *nla_set_name, | |
3880 | const struct nlattr *nla_set_id, | |
3881 | u8 genmask) | |
c7a72e3f PNA |
3882 | { |
3883 | struct nft_set *set; | |
3884 | ||
cac20fcd | 3885 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
3886 | if (IS_ERR(set)) { |
3887 | if (!nla_set_id) | |
3888 | return set; | |
3889 | ||
470ee20e | 3890 | set = nft_set_lookup_byid(net, table, nla_set_id, genmask); |
c7a72e3f PNA |
3891 | } |
3892 | return set; | |
3893 | } | |
10659cba | 3894 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 3895 | |
20a69341 PM |
3896 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
3897 | const char *name) | |
3898 | { | |
3899 | const struct nft_set *i; | |
3900 | const char *p; | |
3901 | unsigned long *inuse; | |
60eb1894 | 3902 | unsigned int n = 0, min = 0; |
20a69341 | 3903 | |
38745490 | 3904 | p = strchr(name, '%'); |
20a69341 PM |
3905 | if (p != NULL) { |
3906 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
3907 | return -EINVAL; | |
3908 | ||
3909 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
3910 | if (inuse == NULL) | |
3911 | return -ENOMEM; | |
60eb1894 | 3912 | cont: |
20a69341 | 3913 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
3914 | int tmp; |
3915 | ||
271c5ca8 | 3916 | if (!nft_is_active_next(ctx->net, i)) |
37a9cc52 | 3917 | continue; |
14662917 | 3918 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 3919 | continue; |
60eb1894 | 3920 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 3921 | continue; |
14662917 | 3922 | |
60eb1894 | 3923 | set_bit(tmp - min, inuse); |
20a69341 PM |
3924 | } |
3925 | ||
53b70287 | 3926 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
3927 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
3928 | min += BITS_PER_BYTE * PAGE_SIZE; | |
3929 | memset(inuse, 0, PAGE_SIZE); | |
3930 | goto cont; | |
3931 | } | |
20a69341 PM |
3932 | free_page((unsigned long)inuse); |
3933 | } | |
3934 | ||
33758c89 | 3935 | set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n); |
38745490 PS |
3936 | if (!set->name) |
3937 | return -ENOMEM; | |
3938 | ||
20a69341 | 3939 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
3940 | if (!nft_is_active_next(ctx->net, i)) |
3941 | continue; | |
e63aaaa6 AY |
3942 | if (!strcmp(set->name, i->name)) { |
3943 | kfree(set->name); | |
7fb6f78d | 3944 | set->name = NULL; |
20a69341 | 3945 | return -ENFILE; |
e63aaaa6 | 3946 | } |
20a69341 PM |
3947 | } |
3948 | return 0; | |
3949 | } | |
3950 | ||
917d80d3 | 3951 | int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
8e1102d5 FW |
3952 | { |
3953 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
3954 | u64 max = (u64)(~((u64)0)); | |
3955 | ||
3956 | max = div_u64(max, NSEC_PER_MSEC); | |
3957 | if (ms >= max) | |
3958 | return -ERANGE; | |
3959 | ||
3960 | ms *= NSEC_PER_MSEC; | |
3961 | *result = nsecs_to_jiffies64(ms); | |
3962 | return 0; | |
3963 | } | |
3964 | ||
917d80d3 | 3965 | __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 3966 | { |
3b15d09f | 3967 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
3968 | } |
3969 | ||
f3a2181e SB |
3970 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
3971 | const struct nft_set *set) | |
3972 | { | |
3973 | struct nlattr *concat, *field; | |
3974 | int i; | |
3975 | ||
3976 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
3977 | if (!concat) | |
3978 | return -ENOMEM; | |
3979 | ||
3980 | for (i = 0; i < set->field_count; i++) { | |
3981 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
3982 | if (!field) | |
3983 | return -ENOMEM; | |
3984 | ||
3985 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
3986 | htonl(set->field_len[i]))) | |
3987 | return -ENOMEM; | |
3988 | ||
3989 | nla_nest_end(skb, field); | |
3990 | } | |
3991 | ||
3992 | nla_nest_end(skb, concat); | |
3993 | ||
3994 | return 0; | |
3995 | } | |
3996 | ||
20a69341 PM |
3997 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
3998 | const struct nft_set *set, u16 event, u16 flags) | |
3999 | { | |
20a69341 | 4000 | struct nlmsghdr *nlh; |
128ad332 | 4001 | u32 portid = ctx->portid; |
65038428 | 4002 | struct nlattr *nest; |
128ad332 | 4003 | u32 seq = ctx->seq; |
48b0ae04 | 4004 | int i; |
20a69341 | 4005 | |
dedb67c4 | 4006 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
4007 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
4008 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
4009 | if (!nlh) | |
20a69341 PM |
4010 | goto nla_put_failure; |
4011 | ||
20a69341 PM |
4012 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
4013 | goto nla_put_failure; | |
4014 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
4015 | goto nla_put_failure; | |
3ecbfd65 HS |
4016 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
4017 | NFTA_SET_PAD)) | |
4018 | goto nla_put_failure; | |
20a69341 PM |
4019 | if (set->flags != 0) |
4020 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
4021 | goto nla_put_failure; | |
4022 | ||
4023 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
4024 | goto nla_put_failure; | |
4025 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
4026 | goto nla_put_failure; | |
4027 | if (set->flags & NFT_SET_MAP) { | |
4028 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
4029 | goto nla_put_failure; | |
4030 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
4031 | goto nla_put_failure; | |
4032 | } | |
8aeff920 PNA |
4033 | if (set->flags & NFT_SET_OBJECT && |
4034 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
4035 | goto nla_put_failure; | |
20a69341 | 4036 | |
761da293 | 4037 | if (set->timeout && |
d3e2a111 | 4038 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
8e1102d5 | 4039 | nf_jiffies64_to_msecs(set->timeout), |
b46f6ded | 4040 | NFTA_SET_PAD)) |
761da293 PM |
4041 | goto nla_put_failure; |
4042 | if (set->gc_int && | |
4043 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) | |
4044 | goto nla_put_failure; | |
4045 | ||
9363dc4b AB |
4046 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
4047 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
4048 | goto nla_put_failure; | |
4049 | } | |
4050 | ||
6f03bf43 PNA |
4051 | if (set->udata && |
4052 | nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) | |
e6d8ecac CFG |
4053 | goto nla_put_failure; |
4054 | ||
65038428 PNA |
4055 | nest = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
4056 | if (!nest) | |
c50b960c PM |
4057 | goto nla_put_failure; |
4058 | if (set->size && | |
4059 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
4060 | goto nla_put_failure; | |
f3a2181e SB |
4061 | |
4062 | if (set->field_count > 1 && | |
4063 | nf_tables_fill_set_concat(skb, set)) | |
4064 | goto nla_put_failure; | |
4065 | ||
65038428 PNA |
4066 | nla_nest_end(skb, nest); |
4067 | ||
8cfd9b0f | 4068 | if (set->num_exprs == 1) { |
65038428 | 4069 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR); |
8cfd9b0f | 4070 | if (nf_tables_fill_expr_info(skb, set->exprs[0]) < 0) |
65038428 PNA |
4071 | goto nla_put_failure; |
4072 | ||
4073 | nla_nest_end(skb, nest); | |
48b0ae04 PNA |
4074 | } else if (set->num_exprs > 1) { |
4075 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS); | |
4076 | if (nest == NULL) | |
65038428 PNA |
4077 | goto nla_put_failure; |
4078 | ||
48b0ae04 PNA |
4079 | for (i = 0; i < set->num_exprs; i++) { |
4080 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, | |
4081 | set->exprs[i]) < 0) | |
4082 | goto nla_put_failure; | |
4083 | } | |
65038428 PNA |
4084 | nla_nest_end(skb, nest); |
4085 | } | |
c50b960c | 4086 | |
053c095a JB |
4087 | nlmsg_end(skb, nlh); |
4088 | return 0; | |
20a69341 PM |
4089 | |
4090 | nla_put_failure: | |
4091 | nlmsg_trim(skb, nlh); | |
4092 | return -1; | |
4093 | } | |
4094 | ||
25e94a99 PNA |
4095 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
4096 | const struct nft_set *set, int event, | |
4097 | gfp_t gfp_flags) | |
20a69341 | 4098 | { |
d59d2f82 | 4099 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
128ad332 | 4100 | u32 portid = ctx->portid; |
6fb721cf PNA |
4101 | struct sk_buff *skb; |
4102 | u16 flags = 0; | |
20a69341 PM |
4103 | int err; |
4104 | ||
128ad332 PNA |
4105 | if (!ctx->report && |
4106 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 4107 | return; |
20a69341 | 4108 | |
31f8441c | 4109 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
4110 | if (skb == NULL) |
4111 | goto err; | |
4112 | ||
6fb721cf PNA |
4113 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
4114 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
4115 | ||
4116 | err = nf_tables_fill_set(skb, ctx, set, event, flags); | |
20a69341 PM |
4117 | if (err < 0) { |
4118 | kfree_skb(skb); | |
4119 | goto err; | |
4120 | } | |
4121 | ||
0854db2a | 4122 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 4123 | return; |
20a69341 | 4124 | err: |
25e94a99 | 4125 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
4126 | } |
4127 | ||
5b96af77 | 4128 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
4129 | { |
4130 | const struct nft_set *set; | |
4131 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
4132 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
4133 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 4134 | struct nft_ctx *ctx = cb->data, ctx_set; |
0854db2a | 4135 | struct nftables_pernet *nft_net; |
c9c8e485 PNA |
4136 | |
4137 | if (cb->args[1]) | |
4138 | return skb->len; | |
4139 | ||
e688a7f8 | 4140 | rcu_read_lock(); |
d59d2f82 | 4141 | nft_net = nft_pernet(net); |
34002783 | 4142 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 4143 | |
0854db2a | 4144 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 4145 | if (ctx->family != NFPROTO_UNSPEC && |
98319cb9 | 4146 | ctx->family != table->family) |
36596dad PNA |
4147 | continue; |
4148 | ||
4149 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
4150 | continue; |
4151 | ||
36596dad PNA |
4152 | if (cur_table) { |
4153 | if (cur_table != table) | |
c9c8e485 PNA |
4154 | continue; |
4155 | ||
36596dad | 4156 | cur_table = NULL; |
c9c8e485 | 4157 | } |
36596dad PNA |
4158 | idx = 0; |
4159 | list_for_each_entry_rcu(set, &table->sets, list) { | |
4160 | if (idx < s_idx) | |
4161 | goto cont; | |
4162 | if (!nft_is_active(net, set)) | |
4163 | goto cont; | |
5b96af77 | 4164 | |
36596dad PNA |
4165 | ctx_set = *ctx; |
4166 | ctx_set.table = table; | |
98319cb9 | 4167 | ctx_set.family = table->family; |
c9c8e485 | 4168 | |
36596dad PNA |
4169 | if (nf_tables_fill_set(skb, &ctx_set, set, |
4170 | NFT_MSG_NEWSET, | |
4171 | NLM_F_MULTI) < 0) { | |
4172 | cb->args[0] = idx; | |
4173 | cb->args[2] = (unsigned long) table; | |
4174 | goto done; | |
c9c8e485 | 4175 | } |
36596dad | 4176 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 4177 | cont: |
36596dad | 4178 | idx++; |
c9c8e485 | 4179 | } |
36596dad PNA |
4180 | if (s_idx) |
4181 | s_idx = 0; | |
c9c8e485 PNA |
4182 | } |
4183 | cb->args[1] = 1; | |
4184 | done: | |
e688a7f8 | 4185 | rcu_read_unlock(); |
c9c8e485 PNA |
4186 | return skb->len; |
4187 | } | |
4188 | ||
90fd131a FW |
4189 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
4190 | { | |
4191 | struct nft_ctx *ctx_dump = NULL; | |
4192 | ||
4193 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
4194 | if (ctx_dump == NULL) | |
4195 | return -ENOMEM; | |
4196 | ||
4197 | cb->data = ctx_dump; | |
4198 | return 0; | |
4199 | } | |
4200 | ||
5b96af77 | 4201 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 4202 | { |
5b96af77 PNA |
4203 | kfree(cb->data); |
4204 | return 0; | |
20a69341 PM |
4205 | } |
4206 | ||
d9adf22a | 4207 | /* called with rcu_read_lock held */ |
797d4980 PNA |
4208 | static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info, |
4209 | const struct nlattr * const nla[]) | |
20a69341 | 4210 | { |
797d4980 PNA |
4211 | struct netlink_ext_ack *extack = info->extack; |
4212 | u8 genmask = nft_genmask_cur(info->net); | |
67086651 PNA |
4213 | u8 family = info->nfmsg->nfgen_family; |
4214 | struct nft_table *table = NULL; | |
797d4980 | 4215 | struct net *net = info->net; |
20a69341 | 4216 | const struct nft_set *set; |
20a69341 | 4217 | struct sk_buff *skb2; |
797d4980 | 4218 | struct nft_ctx ctx; |
20a69341 PM |
4219 | int err; |
4220 | ||
67086651 PNA |
4221 | if (nla[NFTA_SET_TABLE]) { |
4222 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, | |
4223 | genmask, 0); | |
4224 | if (IS_ERR(table)) { | |
4225 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4226 | return PTR_ERR(table); | |
4227 | } | |
4228 | } | |
4229 | ||
4230 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); | |
20a69341 | 4231 | |
797d4980 | 4232 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
20a69341 | 4233 | struct netlink_dump_control c = { |
90fd131a | 4234 | .start = nf_tables_dump_sets_start, |
20a69341 | 4235 | .dump = nf_tables_dump_sets, |
5b96af77 | 4236 | .done = nf_tables_dump_sets_done, |
90fd131a | 4237 | .data = &ctx, |
d9adf22a | 4238 | .module = THIS_MODULE, |
20a69341 | 4239 | }; |
5b96af77 | 4240 | |
797d4980 | 4241 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
20a69341 PM |
4242 | } |
4243 | ||
c9c8e485 | 4244 | /* Only accept unspec with dump */ |
ef4b65e5 | 4245 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
c9c8e485 | 4246 | return -EAFNOSUPPORT; |
eaa2bcd6 PT |
4247 | if (!nla[NFTA_SET_TABLE]) |
4248 | return -EINVAL; | |
c9c8e485 | 4249 | |
67086651 | 4250 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
4251 | if (IS_ERR(set)) |
4252 | return PTR_ERR(set); | |
4253 | ||
d9adf22a | 4254 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
4255 | if (skb2 == NULL) |
4256 | return -ENOMEM; | |
4257 | ||
4258 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
4259 | if (err < 0) | |
ee921183 | 4260 | goto err_fill_set_info; |
20a69341 | 4261 | |
ee921183 | 4262 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
20a69341 | 4263 | |
ee921183 | 4264 | err_fill_set_info: |
20a69341 PM |
4265 | kfree_skb(skb2); |
4266 | return err; | |
4267 | } | |
4268 | ||
f3a2181e SB |
4269 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
4270 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
4271 | }; | |
4272 | ||
4273 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
4274 | struct nft_set_desc *desc) | |
4275 | { | |
4276 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
4277 | u32 len; | |
4278 | int err; | |
4279 | ||
fecf31ee PNA |
4280 | if (desc->field_count >= ARRAY_SIZE(desc->field_len)) |
4281 | return -E2BIG; | |
4282 | ||
f3a2181e SB |
4283 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, |
4284 | nft_concat_policy, NULL); | |
4285 | if (err < 0) | |
4286 | return err; | |
4287 | ||
4288 | if (!tb[NFTA_SET_FIELD_LEN]) | |
4289 | return -EINVAL; | |
4290 | ||
4291 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
fecf31ee PNA |
4292 | if (!len || len > U8_MAX) |
4293 | return -EINVAL; | |
f3a2181e SB |
4294 | |
4295 | desc->field_len[desc->field_count++] = len; | |
4296 | ||
4297 | return 0; | |
4298 | } | |
4299 | ||
4300 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
4301 | const struct nlattr *nla) | |
4302 | { | |
4303 | struct nlattr *attr; | |
fecf31ee PNA |
4304 | u32 num_regs = 0; |
4305 | int rem, err, i; | |
f3a2181e SB |
4306 | |
4307 | nla_for_each_nested(attr, nla, rem) { | |
4308 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
4309 | return -EINVAL; | |
4310 | ||
4311 | err = nft_set_desc_concat_parse(attr, desc); | |
4312 | if (err < 0) | |
4313 | return err; | |
4314 | } | |
4315 | ||
fecf31ee PNA |
4316 | for (i = 0; i < desc->field_count; i++) |
4317 | num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32)); | |
4318 | ||
4319 | if (num_regs > NFT_REG32_COUNT) | |
4320 | return -E2BIG; | |
4321 | ||
f3a2181e SB |
4322 | return 0; |
4323 | } | |
4324 | ||
f7e840ee | 4325 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
4326 | const struct nlattr *nla) |
4327 | { | |
4328 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
4329 | int err; | |
4330 | ||
8cb08174 JB |
4331 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
4332 | nft_set_desc_policy, NULL); | |
c50b960c PM |
4333 | if (err < 0) |
4334 | return err; | |
4335 | ||
4336 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
4337 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
4338 | if (da[NFTA_SET_DESC_CONCAT]) |
4339 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 4340 | |
f3a2181e | 4341 | return err; |
c50b960c PM |
4342 | } |
4343 | ||
7dab8ee3 PNA |
4344 | static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, |
4345 | const struct nlattr * const nla[]) | |
20a69341 | 4346 | { |
7dab8ee3 PNA |
4347 | u32 ktype, dtype, flags, policy, gc_int, objtype; |
4348 | struct netlink_ext_ack *extack = info->extack; | |
4349 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 4350 | u8 family = info->nfmsg->nfgen_family; |
20a69341 | 4351 | const struct nft_set_ops *ops; |
65038428 | 4352 | struct nft_expr *expr = NULL; |
7dab8ee3 PNA |
4353 | struct net *net = info->net; |
4354 | struct nft_set_desc desc; | |
20a69341 | 4355 | struct nft_table *table; |
7dab8ee3 | 4356 | unsigned char *udata; |
20a69341 PM |
4357 | struct nft_set *set; |
4358 | struct nft_ctx ctx; | |
6c8774a9 | 4359 | size_t alloc_size; |
761da293 | 4360 | u64 timeout; |
7dab8ee3 PNA |
4361 | char *name; |
4362 | int err, i; | |
e6d8ecac | 4363 | u16 udlen; |
7dab8ee3 | 4364 | u64 size; |
20a69341 PM |
4365 | |
4366 | if (nla[NFTA_SET_TABLE] == NULL || | |
4367 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
4368 | nla[NFTA_SET_KEY_LEN] == NULL || |
4369 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
4370 | return -EINVAL; |
4371 | ||
c50b960c PM |
4372 | memset(&desc, 0, sizeof(desc)); |
4373 | ||
20a69341 PM |
4374 | ktype = NFT_DATA_VALUE; |
4375 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { | |
4376 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); | |
4377 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
4378 | return -EINVAL; | |
4379 | } | |
4380 | ||
c50b960c | 4381 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 4382 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4383 | return -EINVAL; |
4384 | ||
4385 | flags = 0; | |
4386 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
4387 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
4388 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 4389 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 | 4390 | NFT_SET_MAP | NFT_SET_EVAL | |
b4e70d8d | 4391 | NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR)) |
d9583cdf | 4392 | return -EOPNOTSUPP; |
8aeff920 | 4393 | /* Only one of these operations is supported */ |
acab7131 FW |
4394 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
4395 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
4396 | return -EOPNOTSUPP; | |
4397 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
4398 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 4399 | return -EOPNOTSUPP; |
20a69341 PM |
4400 | } |
4401 | ||
4402 | dtype = 0; | |
20a69341 PM |
4403 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
4404 | if (!(flags & NFT_SET_MAP)) | |
4405 | return -EINVAL; | |
4406 | ||
4407 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); | |
4408 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
4409 | dtype != NFT_DATA_VERDICT) | |
4410 | return -EINVAL; | |
4411 | ||
4412 | if (dtype != NFT_DATA_VERDICT) { | |
4413 | if (nla[NFTA_SET_DATA_LEN] == NULL) | |
4414 | return -EINVAL; | |
c50b960c | 4415 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 4416 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4417 | return -EINVAL; |
4418 | } else | |
7d740264 | 4419 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
4420 | } else if (flags & NFT_SET_MAP) |
4421 | return -EINVAL; | |
4422 | ||
8aeff920 PNA |
4423 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
4424 | if (!(flags & NFT_SET_OBJECT)) | |
4425 | return -EINVAL; | |
4426 | ||
4427 | objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); | |
4428 | if (objtype == NFT_OBJECT_UNSPEC || | |
4429 | objtype > NFT_OBJECT_MAX) | |
d9583cdf | 4430 | return -EOPNOTSUPP; |
8aeff920 PNA |
4431 | } else if (flags & NFT_SET_OBJECT) |
4432 | return -EINVAL; | |
4433 | else | |
4434 | objtype = NFT_OBJECT_UNSPEC; | |
4435 | ||
761da293 PM |
4436 | timeout = 0; |
4437 | if (nla[NFTA_SET_TIMEOUT] != NULL) { | |
4438 | if (!(flags & NFT_SET_TIMEOUT)) | |
4439 | return -EINVAL; | |
8e1102d5 FW |
4440 | |
4441 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout); | |
4442 | if (err) | |
4443 | return err; | |
761da293 PM |
4444 | } |
4445 | gc_int = 0; | |
4446 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { | |
4447 | if (!(flags & NFT_SET_TIMEOUT)) | |
4448 | return -EINVAL; | |
4449 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); | |
4450 | } | |
4451 | ||
c50b960c PM |
4452 | policy = NFT_SET_POL_PERFORMANCE; |
4453 | if (nla[NFTA_SET_POLICY] != NULL) | |
4454 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); | |
4455 | ||
4456 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4457 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4458 | if (err < 0) |
4459 | return err; | |
1b6345d4 PNA |
4460 | |
4461 | if (desc.field_count > 1 && !(flags & NFT_SET_CONCAT)) | |
4462 | return -EINVAL; | |
4463 | } else if (flags & NFT_SET_CONCAT) { | |
4464 | return -EINVAL; | |
c50b960c PM |
4465 | } |
4466 | ||
48b0ae04 | 4467 | if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS]) |
d56aab26 PNA |
4468 | desc.expr = true; |
4469 | ||
6001a930 PNA |
4470 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask, |
4471 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4472 | if (IS_ERR(table)) { |
4473 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4474 | return PTR_ERR(table); |
36dd1bcc | 4475 | } |
20a69341 | 4476 | |
7dab8ee3 | 4477 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
20a69341 | 4478 | |
cac20fcd | 4479 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4480 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4481 | if (PTR_ERR(set) != -ENOENT) { |
4482 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4483 | return PTR_ERR(set); |
36dd1bcc | 4484 | } |
1a28ad74 | 4485 | } else { |
7dab8ee3 | 4486 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 4487 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); |
20a69341 | 4488 | return -EEXIST; |
36dd1bcc | 4489 | } |
7dab8ee3 | 4490 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
20a69341 | 4491 | return -EOPNOTSUPP; |
36dd1bcc | 4492 | |
20a69341 PM |
4493 | return 0; |
4494 | } | |
4495 | ||
7dab8ee3 | 4496 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
20a69341 PM |
4497 | return -ENOENT; |
4498 | ||
2b664957 | 4499 | ops = nft_select_set_ops(&ctx, nla, &desc, policy); |
20a69341 PM |
4500 | if (IS_ERR(ops)) |
4501 | return PTR_ERR(ops); | |
4502 | ||
e6d8ecac CFG |
4503 | udlen = 0; |
4504 | if (nla[NFTA_SET_USERDATA]) | |
4505 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
4506 | ||
20a69341 PM |
4507 | size = 0; |
4508 | if (ops->privsize != NULL) | |
347b408d | 4509 | size = ops->privsize(nla, &desc); |
6c8774a9 | 4510 | alloc_size = sizeof(*set) + size + udlen; |
45928afe | 4511 | if (alloc_size < size || alloc_size > INT_MAX) |
6c8774a9 | 4512 | return -ENOMEM; |
33758c89 | 4513 | set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT); |
24d19826 FW |
4514 | if (!set) |
4515 | return -ENOMEM; | |
20a69341 | 4516 | |
33758c89 | 4517 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT); |
38745490 PS |
4518 | if (!name) { |
4519 | err = -ENOMEM; | |
65038428 | 4520 | goto err_set_name; |
38745490 PS |
4521 | } |
4522 | ||
20a69341 | 4523 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 4524 | kfree(name); |
20a69341 | 4525 | if (err < 0) |
ad9f151e PNA |
4526 | goto err_set_name; |
4527 | ||
4528 | udata = NULL; | |
4529 | if (udlen) { | |
4530 | udata = set->data + size; | |
4531 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
4532 | } | |
4533 | ||
4534 | INIT_LIST_HEAD(&set->bindings); | |
4535 | INIT_LIST_HEAD(&set->catchall_list); | |
4536 | set->table = table; | |
4537 | write_pnet(&set->net, net); | |
4538 | set->ops = ops; | |
4539 | set->ktype = ktype; | |
4540 | set->klen = desc.klen; | |
4541 | set->dtype = dtype; | |
4542 | set->objtype = objtype; | |
4543 | set->dlen = desc.dlen; | |
4544 | set->flags = flags; | |
4545 | set->size = desc.size; | |
4546 | set->policy = policy; | |
4547 | set->udlen = udlen; | |
4548 | set->udata = udata; | |
4549 | set->timeout = timeout; | |
4550 | set->gc_int = gc_int; | |
4551 | ||
4552 | set->field_count = desc.field_count; | |
4553 | for (i = 0; i < desc.field_count; i++) | |
4554 | set->field_len[i] = desc.field_len[i]; | |
4555 | ||
4556 | err = ops->init(set, &desc, nla); | |
4557 | if (err < 0) | |
4558 | goto err_set_init; | |
65038428 PNA |
4559 | |
4560 | if (nla[NFTA_SET_EXPR]) { | |
4561 | expr = nft_set_elem_expr_alloc(&ctx, set, nla[NFTA_SET_EXPR]); | |
4562 | if (IS_ERR(expr)) { | |
4563 | err = PTR_ERR(expr); | |
ad9f151e | 4564 | goto err_set_expr_alloc; |
65038428 | 4565 | } |
8cfd9b0f PNA |
4566 | set->exprs[0] = expr; |
4567 | set->num_exprs++; | |
48b0ae04 PNA |
4568 | } else if (nla[NFTA_SET_EXPRESSIONS]) { |
4569 | struct nft_expr *expr; | |
4570 | struct nlattr *tmp; | |
4571 | int left; | |
4572 | ||
b4e70d8d PNA |
4573 | if (!(flags & NFT_SET_EXPR)) { |
4574 | err = -EINVAL; | |
ad9f151e | 4575 | goto err_set_expr_alloc; |
b4e70d8d | 4576 | } |
48b0ae04 PNA |
4577 | i = 0; |
4578 | nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) { | |
4579 | if (i == NFT_SET_EXPR_MAX) { | |
4580 | err = -E2BIG; | |
ad9f151e | 4581 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4582 | } |
4583 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
4584 | err = -EINVAL; | |
ad9f151e | 4585 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4586 | } |
4587 | expr = nft_set_elem_expr_alloc(&ctx, set, tmp); | |
4588 | if (IS_ERR(expr)) { | |
4589 | err = PTR_ERR(expr); | |
ad9f151e | 4590 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4591 | } |
4592 | set->exprs[i++] = expr; | |
4593 | set->num_exprs++; | |
4594 | } | |
65038428 | 4595 | } |
20a69341 | 4596 | |
3ecbfd65 | 4597 | set->handle = nf_tables_alloc_handle(table); |
20a69341 | 4598 | |
958bee14 | 4599 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 4600 | if (err < 0) |
ad9f151e | 4601 | goto err_set_expr_alloc; |
20a69341 | 4602 | |
e688a7f8 | 4603 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 4604 | table->use++; |
20a69341 PM |
4605 | return 0; |
4606 | ||
ad9f151e | 4607 | err_set_expr_alloc: |
8cfd9b0f PNA |
4608 | for (i = 0; i < set->num_exprs; i++) |
4609 | nft_expr_destroy(&ctx, set->exprs[i]); | |
ad9f151e PNA |
4610 | |
4611 | ops->destroy(set); | |
4612 | err_set_init: | |
2f6adf48 | 4613 | kfree(set->name); |
65038428 | 4614 | err_set_name: |
1ff75a3e | 4615 | kvfree(set); |
20a69341 PM |
4616 | return err; |
4617 | } | |
4618 | ||
aaa31047 PNA |
4619 | struct nft_set_elem_catchall { |
4620 | struct list_head list; | |
4621 | struct rcu_head rcu; | |
4622 | void *elem; | |
4623 | }; | |
4624 | ||
4625 | static void nft_set_catchall_destroy(const struct nft_ctx *ctx, | |
4626 | struct nft_set *set) | |
4627 | { | |
0f7d9b31 | 4628 | struct nft_set_elem_catchall *next, *catchall; |
aaa31047 | 4629 | |
0f7d9b31 | 4630 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
aaa31047 PNA |
4631 | list_del_rcu(&catchall->list); |
4632 | nft_set_elem_destroy(set, catchall->elem, true); | |
ae089831 | 4633 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
4634 | } |
4635 | } | |
4636 | ||
0c2a85ed | 4637 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
20a69341 | 4638 | { |
8cfd9b0f PNA |
4639 | int i; |
4640 | ||
273fe3f1 PNA |
4641 | if (WARN_ON(set->use > 0)) |
4642 | return; | |
4643 | ||
8cfd9b0f PNA |
4644 | for (i = 0; i < set->num_exprs; i++) |
4645 | nft_expr_destroy(ctx, set->exprs[i]); | |
65038428 | 4646 | |
20a69341 | 4647 | set->ops->destroy(set); |
aaa31047 | 4648 | nft_set_catchall_destroy(ctx, set); |
38745490 | 4649 | kfree(set->name); |
1ff75a3e | 4650 | kvfree(set); |
20a69341 PM |
4651 | } |
4652 | ||
7dab8ee3 PNA |
4653 | static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info, |
4654 | const struct nlattr * const nla[]) | |
20a69341 | 4655 | { |
7dab8ee3 PNA |
4656 | struct netlink_ext_ack *extack = info->extack; |
4657 | u8 genmask = nft_genmask_next(info->net); | |
67086651 | 4658 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 4659 | struct net *net = info->net; |
36dd1bcc | 4660 | const struct nlattr *attr; |
67086651 | 4661 | struct nft_table *table; |
20a69341 PM |
4662 | struct nft_set *set; |
4663 | struct nft_ctx ctx; | |
20a69341 | 4664 | |
ef4b65e5 | 4665 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
ec2c9935 | 4666 | return -EAFNOSUPPORT; |
20a69341 | 4667 | |
67086651 PNA |
4668 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
4669 | genmask, NETLINK_CB(skb).portid); | |
4670 | if (IS_ERR(table)) { | |
4671 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4672 | return PTR_ERR(table); | |
4673 | } | |
20a69341 | 4674 | |
36dd1bcc PNA |
4675 | if (nla[NFTA_SET_HANDLE]) { |
4676 | attr = nla[NFTA_SET_HANDLE]; | |
67086651 | 4677 | set = nft_set_lookup_byhandle(table, attr, genmask); |
36dd1bcc PNA |
4678 | } else { |
4679 | attr = nla[NFTA_SET_NAME]; | |
67086651 | 4680 | set = nft_set_lookup(table, attr, genmask); |
36dd1bcc | 4681 | } |
a8278400 | 4682 | |
36dd1bcc PNA |
4683 | if (IS_ERR(set)) { |
4684 | NL_SET_BAD_ATTR(extack, attr); | |
4685 | return PTR_ERR(set); | |
4686 | } | |
273fe3f1 | 4687 | if (set->use || |
7dab8ee3 PNA |
4688 | (info->nlh->nlmsg_flags & NLM_F_NONREC && |
4689 | atomic_read(&set->nelems) > 0)) { | |
36dd1bcc | 4690 | NL_SET_BAD_ATTR(extack, attr); |
20a69341 | 4691 | return -EBUSY; |
36dd1bcc | 4692 | } |
20a69341 | 4693 | |
67086651 PNA |
4694 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
4695 | ||
ee01d542 | 4696 | return nft_delset(&ctx, set); |
20a69341 PM |
4697 | } |
4698 | ||
345023b0 PNA |
4699 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
4700 | enum nft_registers reg, | |
4701 | const struct nft_data *data, | |
4702 | enum nft_data_types type, | |
4703 | unsigned int len); | |
4704 | ||
97c976d6 PNA |
4705 | static int nft_setelem_data_validate(const struct nft_ctx *ctx, |
4706 | struct nft_set *set, | |
4707 | struct nft_set_elem *elem) | |
20a69341 | 4708 | { |
fe2811eb | 4709 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4710 | enum nft_registers dreg; |
4711 | ||
4712 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
4713 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
4714 | set->dtype == NFT_DATA_VERDICT ? | |
4715 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
4716 | set->dlen); | |
20a69341 PM |
4717 | } |
4718 | ||
97c976d6 PNA |
4719 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, |
4720 | struct nft_set *set, | |
4721 | const struct nft_set_iter *iter, | |
4722 | struct nft_set_elem *elem) | |
4723 | { | |
4724 | return nft_setelem_data_validate(ctx, set, elem); | |
4725 | } | |
4726 | ||
aaa31047 PNA |
4727 | static int nft_set_catchall_bind_check(const struct nft_ctx *ctx, |
4728 | struct nft_set *set) | |
4729 | { | |
4730 | u8 genmask = nft_genmask_next(ctx->net); | |
4731 | struct nft_set_elem_catchall *catchall; | |
4732 | struct nft_set_elem elem; | |
4733 | struct nft_set_ext *ext; | |
4734 | int ret = 0; | |
4735 | ||
4736 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
4737 | ext = nft_set_elem_ext(set, catchall->elem); | |
4738 | if (!nft_set_elem_active(ext, genmask)) | |
4739 | continue; | |
4740 | ||
4741 | elem.priv = catchall->elem; | |
4742 | ret = nft_setelem_data_validate(ctx, set, &elem); | |
4743 | if (ret < 0) | |
4744 | break; | |
4745 | } | |
4746 | ||
4747 | return ret; | |
4748 | } | |
4749 | ||
20a69341 PM |
4750 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4751 | struct nft_set_binding *binding) | |
4752 | { | |
4753 | struct nft_set_binding *i; | |
4754 | struct nft_set_iter iter; | |
4755 | ||
273fe3f1 PNA |
4756 | if (set->use == UINT_MAX) |
4757 | return -EOVERFLOW; | |
4758 | ||
408070d6 | 4759 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
4760 | return -EBUSY; |
4761 | ||
11113e19 | 4762 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
4763 | /* If the set is already bound to the same chain all |
4764 | * jumps are already validated for that chain. | |
4765 | */ | |
4766 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 4767 | if (i->flags & NFT_SET_MAP && |
11113e19 | 4768 | i->chain == binding->chain) |
20a69341 PM |
4769 | goto bind; |
4770 | } | |
4771 | ||
8588ac09 | 4772 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
4773 | iter.skip = 0; |
4774 | iter.count = 0; | |
4775 | iter.err = 0; | |
4776 | iter.fn = nf_tables_bind_check_setelem; | |
4777 | ||
4778 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
4779 | if (!iter.err) |
4780 | iter.err = nft_set_catchall_bind_check(ctx, set); | |
4781 | ||
a02f4248 | 4782 | if (iter.err < 0) |
20a69341 | 4783 | return iter.err; |
20a69341 PM |
4784 | } |
4785 | bind: | |
4786 | binding->chain = ctx->chain; | |
e688a7f8 | 4787 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 | 4788 | nft_set_trans_bind(ctx, set); |
273fe3f1 | 4789 | set->use++; |
f6ac8585 | 4790 | |
20a69341 PM |
4791 | return 0; |
4792 | } | |
63aea290 | 4793 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 4794 | |
3b0a081d FW |
4795 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4796 | struct nft_set_binding *binding, bool event) | |
20a69341 | 4797 | { |
e688a7f8 | 4798 | list_del_rcu(&binding->list); |
20a69341 | 4799 | |
f6ac8585 | 4800 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 4801 | list_del_rcu(&set->list); |
f6ac8585 PNA |
4802 | if (event) |
4803 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
4804 | GFP_KERNEL); | |
4805 | } | |
20a69341 PM |
4806 | } |
4807 | ||
273fe3f1 PNA |
4808 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
4809 | struct nft_set_binding *binding, | |
4810 | enum nft_trans_phase phase) | |
4811 | { | |
4812 | switch (phase) { | |
4813 | case NFT_TRANS_PREPARE: | |
4814 | set->use--; | |
4815 | return; | |
4816 | case NFT_TRANS_ABORT: | |
4817 | case NFT_TRANS_RELEASE: | |
4818 | set->use--; | |
954d8297 | 4819 | fallthrough; |
273fe3f1 PNA |
4820 | default: |
4821 | nf_tables_unbind_set(ctx, set, binding, | |
4822 | phase == NFT_TRANS_COMMIT); | |
4823 | } | |
4824 | } | |
4825 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
4826 | ||
cd5125d8 FW |
4827 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
4828 | { | |
f6ac8585 | 4829 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
0c2a85ed | 4830 | nft_set_destroy(ctx, set); |
cd5125d8 FW |
4831 | } |
4832 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
4833 | ||
3ac4c07a PM |
4834 | const struct nft_set_ext_type nft_set_ext_types[] = { |
4835 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 4836 | .align = __alignof__(u32), |
3ac4c07a PM |
4837 | }, |
4838 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 4839 | .align = __alignof__(u32), |
3ac4c07a | 4840 | }, |
563125a7 PNA |
4841 | [NFT_SET_EXT_EXPRESSIONS] = { |
4842 | .align = __alignof__(struct nft_set_elem_expr), | |
f25ad2e9 | 4843 | }, |
8aeff920 PNA |
4844 | [NFT_SET_EXT_OBJREF] = { |
4845 | .len = sizeof(struct nft_object *), | |
4846 | .align = __alignof__(struct nft_object *), | |
4847 | }, | |
3ac4c07a PM |
4848 | [NFT_SET_EXT_FLAGS] = { |
4849 | .len = sizeof(u8), | |
4850 | .align = __alignof__(u8), | |
4851 | }, | |
c3e1b005 PM |
4852 | [NFT_SET_EXT_TIMEOUT] = { |
4853 | .len = sizeof(u64), | |
4854 | .align = __alignof__(u64), | |
4855 | }, | |
4856 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
4857 | .len = sizeof(u64), |
4858 | .align = __alignof__(u64), | |
c3e1b005 | 4859 | }, |
68e942e8 PM |
4860 | [NFT_SET_EXT_USERDATA] = { |
4861 | .len = sizeof(struct nft_userdata), | |
4862 | .align = __alignof__(struct nft_userdata), | |
4863 | }, | |
7b225d0b PNA |
4864 | [NFT_SET_EXT_KEY_END] = { |
4865 | .align = __alignof__(u32), | |
4866 | }, | |
3ac4c07a | 4867 | }; |
3ac4c07a | 4868 | |
20a69341 PM |
4869 | /* |
4870 | * Set elements | |
4871 | */ | |
4872 | ||
4873 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
4874 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
4875 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
4876 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 4877 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 4878 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
4879 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
4880 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 4881 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
4882 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
4883 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 4884 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
48b0ae04 | 4885 | [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED }, |
20a69341 PM |
4886 | }; |
4887 | ||
4888 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
4889 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
4890 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
4891 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
4892 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 4893 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 4894 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
4895 | }; |
4896 | ||
563125a7 PNA |
4897 | static int nft_set_elem_expr_dump(struct sk_buff *skb, |
4898 | const struct nft_set *set, | |
4899 | const struct nft_set_ext *ext) | |
4900 | { | |
4901 | struct nft_set_elem_expr *elem_expr; | |
4902 | u32 size, num_exprs = 0; | |
4903 | struct nft_expr *expr; | |
48b0ae04 | 4904 | struct nlattr *nest; |
563125a7 PNA |
4905 | |
4906 | elem_expr = nft_set_ext_expr(ext); | |
4907 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
4908 | num_exprs++; | |
4909 | ||
4910 | if (num_exprs == 1) { | |
4911 | expr = nft_setelem_expr_at(elem_expr, 0); | |
4912 | if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr) < 0) | |
4913 | return -1; | |
4914 | ||
4915 | return 0; | |
48b0ae04 PNA |
4916 | } else if (num_exprs > 1) { |
4917 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS); | |
4918 | if (nest == NULL) | |
4919 | goto nla_put_failure; | |
563125a7 | 4920 | |
48b0ae04 PNA |
4921 | nft_setelem_expr_foreach(expr, elem_expr, size) { |
4922 | expr = nft_setelem_expr_at(elem_expr, size); | |
4923 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) | |
4924 | goto nla_put_failure; | |
4925 | } | |
4926 | nla_nest_end(skb, nest); | |
4927 | } | |
563125a7 | 4928 | return 0; |
48b0ae04 PNA |
4929 | |
4930 | nla_put_failure: | |
4931 | return -1; | |
563125a7 PNA |
4932 | } |
4933 | ||
20a69341 PM |
4934 | static int nf_tables_fill_setelem(struct sk_buff *skb, |
4935 | const struct nft_set *set, | |
4936 | const struct nft_set_elem *elem) | |
4937 | { | |
fe2811eb | 4938 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4939 | unsigned char *b = skb_tail_pointer(skb); |
4940 | struct nlattr *nest; | |
4941 | ||
ae0be8de | 4942 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
4943 | if (nest == NULL) |
4944 | goto nla_put_failure; | |
4945 | ||
aaa31047 PNA |
4946 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
4947 | nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), | |
fe2811eb | 4948 | NFT_DATA_VALUE, set->klen) < 0) |
20a69341 PM |
4949 | goto nla_put_failure; |
4950 | ||
7b225d0b PNA |
4951 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
4952 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
4953 | NFT_DATA_VALUE, set->klen) < 0) | |
4954 | goto nla_put_failure; | |
4955 | ||
fe2811eb PM |
4956 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
4957 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
4958 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
4959 | set->dlen) < 0) | |
4960 | goto nla_put_failure; | |
4961 | ||
563125a7 PNA |
4962 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) && |
4963 | nft_set_elem_expr_dump(skb, set, ext)) | |
f25ad2e9 PM |
4964 | goto nla_put_failure; |
4965 | ||
8aeff920 PNA |
4966 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
4967 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 4968 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
4969 | goto nla_put_failure; |
4970 | ||
fe2811eb PM |
4971 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
4972 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
4973 | htonl(*nft_set_ext_flags(ext)))) | |
4974 | goto nla_put_failure; | |
20a69341 | 4975 | |
c3e1b005 PM |
4976 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
4977 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 4978 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 4979 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4980 | goto nla_put_failure; |
4981 | ||
4982 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 4983 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
4984 | |
4985 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 4986 | if (time_before64(now, expires)) |
c3e1b005 PM |
4987 | expires -= now; |
4988 | else | |
4989 | expires = 0; | |
4990 | ||
4991 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 4992 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 4993 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4994 | goto nla_put_failure; |
4995 | } | |
4996 | ||
68e942e8 PM |
4997 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
4998 | struct nft_userdata *udata; | |
4999 | ||
5000 | udata = nft_set_ext_userdata(ext); | |
5001 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
5002 | udata->len + 1, udata->data)) | |
5003 | goto nla_put_failure; | |
5004 | } | |
5005 | ||
20a69341 PM |
5006 | nla_nest_end(skb, nest); |
5007 | return 0; | |
5008 | ||
5009 | nla_put_failure: | |
5010 | nlmsg_trim(skb, b); | |
5011 | return -EMSGSIZE; | |
5012 | } | |
5013 | ||
5014 | struct nft_set_dump_args { | |
5015 | const struct netlink_callback *cb; | |
5016 | struct nft_set_iter iter; | |
5017 | struct sk_buff *skb; | |
5018 | }; | |
5019 | ||
5020 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 5021 | struct nft_set *set, |
20a69341 | 5022 | const struct nft_set_iter *iter, |
de70185d | 5023 | struct nft_set_elem *elem) |
20a69341 PM |
5024 | { |
5025 | struct nft_set_dump_args *args; | |
5026 | ||
5027 | args = container_of(iter, struct nft_set_dump_args, iter); | |
5028 | return nf_tables_fill_setelem(args->skb, set, elem); | |
5029 | } | |
5030 | ||
fa803605 LZ |
5031 | struct nft_set_dump_ctx { |
5032 | const struct nft_set *set; | |
5033 | struct nft_ctx ctx; | |
5034 | }; | |
5035 | ||
aaa31047 PNA |
5036 | static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb, |
5037 | const struct nft_set *set) | |
5038 | { | |
5039 | struct nft_set_elem_catchall *catchall; | |
5040 | u8 genmask = nft_genmask_cur(net); | |
5041 | struct nft_set_elem elem; | |
5042 | struct nft_set_ext *ext; | |
5043 | int ret = 0; | |
5044 | ||
5045 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5046 | ext = nft_set_elem_ext(set, catchall->elem); | |
5047 | if (!nft_set_elem_active(ext, genmask) || | |
5048 | nft_set_elem_expired(ext)) | |
5049 | continue; | |
5050 | ||
5051 | elem.priv = catchall->elem; | |
5052 | ret = nf_tables_fill_setelem(skb, set, &elem); | |
5053 | break; | |
5054 | } | |
5055 | ||
5056 | return ret; | |
5057 | } | |
5058 | ||
20a69341 PM |
5059 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
5060 | { | |
fa803605 | 5061 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 5062 | struct net *net = sock_net(skb->sk); |
0854db2a | 5063 | struct nftables_pernet *nft_net; |
fa803605 | 5064 | struct nft_table *table; |
de70185d | 5065 | struct nft_set *set; |
20a69341 | 5066 | struct nft_set_dump_args args; |
fa803605 | 5067 | bool set_found = false; |
20a69341 PM |
5068 | struct nlmsghdr *nlh; |
5069 | struct nlattr *nest; | |
5070 | u32 portid, seq; | |
fa803605 | 5071 | int event; |
20a69341 | 5072 | |
fa803605 | 5073 | rcu_read_lock(); |
d59d2f82 | 5074 | nft_net = nft_pernet(net); |
34002783 PNA |
5075 | cb->seq = READ_ONCE(nft_net->base_seq); |
5076 | ||
0854db2a | 5077 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 5078 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && |
98319cb9 | 5079 | dump_ctx->ctx.family != table->family) |
fa803605 | 5080 | continue; |
20a69341 | 5081 | |
36596dad PNA |
5082 | if (table != dump_ctx->ctx.table) |
5083 | continue; | |
20a69341 | 5084 | |
36596dad PNA |
5085 | list_for_each_entry_rcu(set, &table->sets, list) { |
5086 | if (set == dump_ctx->set) { | |
5087 | set_found = true; | |
5088 | break; | |
fa803605 | 5089 | } |
fa803605 LZ |
5090 | } |
5091 | break; | |
5092 | } | |
5093 | ||
5094 | if (!set_found) { | |
5095 | rcu_read_unlock(); | |
5096 | return -ENOENT; | |
5097 | } | |
20a69341 | 5098 | |
dedb67c4 | 5099 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
5100 | portid = NETLINK_CB(cb->skb).portid; |
5101 | seq = cb->nlh->nlmsg_seq; | |
5102 | ||
19c28b13 PNA |
5103 | nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI, |
5104 | table->family, NFNETLINK_V0, nft_base_seq(net)); | |
5105 | if (!nlh) | |
20a69341 PM |
5106 | goto nla_put_failure; |
5107 | ||
fa803605 | 5108 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
5109 | goto nla_put_failure; |
5110 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
5111 | goto nla_put_failure; | |
5112 | ||
ae0be8de | 5113 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
5114 | if (nest == NULL) |
5115 | goto nla_put_failure; | |
5116 | ||
8588ac09 PNA |
5117 | args.cb = cb; |
5118 | args.skb = skb; | |
fa803605 | 5119 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
5120 | args.iter.skip = cb->args[0]; |
5121 | args.iter.count = 0; | |
5122 | args.iter.err = 0; | |
5123 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 | 5124 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
aaa31047 PNA |
5125 | |
5126 | if (!args.iter.err && args.iter.count == cb->args[0]) | |
5127 | args.iter.err = nft_set_catchall_dump(net, skb, set); | |
fa803605 | 5128 | rcu_read_unlock(); |
20a69341 PM |
5129 | |
5130 | nla_nest_end(skb, nest); | |
5131 | nlmsg_end(skb, nlh); | |
5132 | ||
5133 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
5134 | return args.iter.err; | |
5135 | if (args.iter.count == cb->args[0]) | |
5136 | return 0; | |
5137 | ||
5138 | cb->args[0] = args.iter.count; | |
5139 | return skb->len; | |
5140 | ||
5141 | nla_put_failure: | |
fa803605 | 5142 | rcu_read_unlock(); |
20a69341 PM |
5143 | return -ENOSPC; |
5144 | } | |
5145 | ||
90fd131a FW |
5146 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
5147 | { | |
5148 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
5149 | ||
5150 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
5151 | ||
5152 | return cb->data ? 0 : -ENOMEM; | |
5153 | } | |
5154 | ||
fa803605 LZ |
5155 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
5156 | { | |
5157 | kfree(cb->data); | |
5158 | return 0; | |
5159 | } | |
5160 | ||
d60ce62f AB |
5161 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
5162 | const struct nft_ctx *ctx, u32 seq, | |
5163 | u32 portid, int event, u16 flags, | |
5164 | const struct nft_set *set, | |
5165 | const struct nft_set_elem *elem) | |
5166 | { | |
d60ce62f AB |
5167 | struct nlmsghdr *nlh; |
5168 | struct nlattr *nest; | |
5169 | int err; | |
5170 | ||
dedb67c4 | 5171 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
5172 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
5173 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
5174 | if (!nlh) | |
d60ce62f AB |
5175 | goto nla_put_failure; |
5176 | ||
d60ce62f AB |
5177 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
5178 | goto nla_put_failure; | |
5179 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
5180 | goto nla_put_failure; | |
5181 | ||
ae0be8de | 5182 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
5183 | if (nest == NULL) |
5184 | goto nla_put_failure; | |
5185 | ||
5186 | err = nf_tables_fill_setelem(skb, set, elem); | |
5187 | if (err < 0) | |
5188 | goto nla_put_failure; | |
5189 | ||
5190 | nla_nest_end(skb, nest); | |
5191 | ||
053c095a JB |
5192 | nlmsg_end(skb, nlh); |
5193 | return 0; | |
d60ce62f AB |
5194 | |
5195 | nla_put_failure: | |
5196 | nlmsg_trim(skb, nlh); | |
5197 | return -1; | |
5198 | } | |
5199 | ||
ba0e4d99 PNA |
5200 | static int nft_setelem_parse_flags(const struct nft_set *set, |
5201 | const struct nlattr *attr, u32 *flags) | |
5202 | { | |
5203 | if (attr == NULL) | |
5204 | return 0; | |
5205 | ||
5206 | *flags = ntohl(nla_get_be32(attr)); | |
aaa31047 PNA |
5207 | if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) |
5208 | return -EOPNOTSUPP; | |
ba0e4d99 PNA |
5209 | if (!(set->flags & NFT_SET_INTERVAL) && |
5210 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
5211 | return -EINVAL; | |
fc0ae524 PNA |
5212 | if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) == |
5213 | (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) | |
5214 | return -EINVAL; | |
ba0e4d99 PNA |
5215 | |
5216 | return 0; | |
5217 | } | |
5218 | ||
20a1452c PNA |
5219 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
5220 | struct nft_data *key, struct nlattr *attr) | |
5221 | { | |
341b6941 PNA |
5222 | struct nft_data_desc desc = { |
5223 | .type = NFT_DATA_VALUE, | |
5224 | .size = NFT_DATA_VALUE_MAXLEN, | |
5225 | .len = set->klen, | |
5226 | }; | |
20a1452c | 5227 | |
341b6941 | 5228 | return nft_data_init(ctx, key, &desc, attr); |
20a1452c PNA |
5229 | } |
5230 | ||
fdb9c405 PNA |
5231 | static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, |
5232 | struct nft_data_desc *desc, | |
5233 | struct nft_data *data, | |
5234 | struct nlattr *attr) | |
5235 | { | |
7e6bc1f6 | 5236 | u32 dtype; |
fdb9c405 | 5237 | |
7e6bc1f6 PNA |
5238 | if (set->dtype == NFT_DATA_VERDICT) |
5239 | dtype = NFT_DATA_VERDICT; | |
5240 | else | |
5241 | dtype = NFT_DATA_VALUE; | |
5242 | ||
341b6941 PNA |
5243 | desc->type = dtype; |
5244 | desc->size = NFT_DATA_VALUE_MAXLEN; | |
5245 | desc->len = set->dlen; | |
f323ef3a | 5246 | desc->flags = NFT_DATA_DESC_SETELEM; |
fdb9c405 | 5247 | |
341b6941 | 5248 | return nft_data_init(ctx, data, desc, attr); |
fdb9c405 PNA |
5249 | } |
5250 | ||
aaa31047 PNA |
5251 | static void *nft_setelem_catchall_get(const struct net *net, |
5252 | const struct nft_set *set) | |
5253 | { | |
5254 | struct nft_set_elem_catchall *catchall; | |
5255 | u8 genmask = nft_genmask_cur(net); | |
5256 | struct nft_set_ext *ext; | |
5257 | void *priv = NULL; | |
5258 | ||
5259 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5260 | ext = nft_set_elem_ext(set, catchall->elem); | |
5261 | if (!nft_set_elem_active(ext, genmask) || | |
5262 | nft_set_elem_expired(ext)) | |
5263 | continue; | |
5264 | ||
5265 | priv = catchall->elem; | |
5266 | break; | |
5267 | } | |
5268 | ||
5269 | return priv; | |
5270 | } | |
5271 | ||
5272 | static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set, | |
5273 | struct nft_set_elem *elem, u32 flags) | |
5274 | { | |
5275 | void *priv; | |
5276 | ||
5277 | if (!(flags & NFT_SET_ELEM_CATCHALL)) { | |
5278 | priv = set->ops->get(ctx->net, set, elem, flags); | |
5279 | if (IS_ERR(priv)) | |
5280 | return PTR_ERR(priv); | |
5281 | } else { | |
5282 | priv = nft_setelem_catchall_get(ctx->net, set); | |
5283 | if (!priv) | |
5284 | return -ENOENT; | |
5285 | } | |
5286 | elem->priv = priv; | |
5287 | ||
5288 | return 0; | |
5289 | } | |
5290 | ||
ba0e4d99 PNA |
5291 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
5292 | const struct nlattr *attr) | |
5293 | { | |
5294 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
5295 | struct nft_set_elem elem; |
5296 | struct sk_buff *skb; | |
5297 | uint32_t flags = 0; | |
ba0e4d99 PNA |
5298 | int err; |
5299 | ||
8cb08174 JB |
5300 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5301 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
5302 | if (err < 0) |
5303 | return err; | |
5304 | ||
ba0e4d99 PNA |
5305 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5306 | if (err < 0) | |
5307 | return err; | |
5308 | ||
aaa31047 PNA |
5309 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
5310 | return -EINVAL; | |
5311 | ||
5312 | if (nla[NFTA_SET_ELEM_KEY]) { | |
5313 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
5314 | nla[NFTA_SET_ELEM_KEY]); | |
5315 | if (err < 0) | |
5316 | return err; | |
5317 | } | |
ba0e4d99 | 5318 | |
7b225d0b PNA |
5319 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5320 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5321 | nla[NFTA_SET_ELEM_KEY_END]); | |
5322 | if (err < 0) | |
5323 | return err; | |
5324 | } | |
5325 | ||
aaa31047 PNA |
5326 | err = nft_setelem_get(ctx, set, &elem, flags); |
5327 | if (err < 0) | |
5328 | return err; | |
ba0e4d99 PNA |
5329 | |
5330 | err = -ENOMEM; | |
d9adf22a | 5331 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 | 5332 | if (skb == NULL) |
ee921183 | 5333 | return err; |
ba0e4d99 PNA |
5334 | |
5335 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
5336 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
5337 | if (err < 0) | |
ee921183 | 5338 | goto err_fill_setelem; |
ba0e4d99 | 5339 | |
ee921183 | 5340 | return nfnetlink_unicast(skb, ctx->net, ctx->portid); |
ba0e4d99 | 5341 | |
ee921183 | 5342 | err_fill_setelem: |
ba0e4d99 | 5343 | kfree_skb(skb); |
ee921183 | 5344 | return err; |
ba0e4d99 PNA |
5345 | } |
5346 | ||
d9adf22a | 5347 | /* called with rcu_read_lock held */ |
797d4980 PNA |
5348 | static int nf_tables_getsetelem(struct sk_buff *skb, |
5349 | const struct nfnl_info *info, | |
5350 | const struct nlattr * const nla[]) | |
ba0e4d99 | 5351 | { |
797d4980 PNA |
5352 | struct netlink_ext_ack *extack = info->extack; |
5353 | u8 genmask = nft_genmask_cur(info->net); | |
e2b750d7 | 5354 | u8 family = info->nfmsg->nfgen_family; |
797d4980 | 5355 | struct net *net = info->net; |
e2b750d7 | 5356 | struct nft_table *table; |
ba0e4d99 PNA |
5357 | struct nft_set *set; |
5358 | struct nlattr *attr; | |
5359 | struct nft_ctx ctx; | |
5360 | int rem, err = 0; | |
5361 | ||
e2b750d7 PNA |
5362 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
5363 | genmask, NETLINK_CB(skb).portid); | |
5364 | if (IS_ERR(table)) { | |
5365 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
5366 | return PTR_ERR(table); | |
5367 | } | |
ba0e4d99 | 5368 | |
e2b750d7 | 5369 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
5370 | if (IS_ERR(set)) |
5371 | return PTR_ERR(set); | |
5372 | ||
e2b750d7 PNA |
5373 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5374 | ||
797d4980 | 5375 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
ba0e4d99 | 5376 | struct netlink_dump_control c = { |
90fd131a | 5377 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
5378 | .dump = nf_tables_dump_set, |
5379 | .done = nf_tables_dump_set_done, | |
d9adf22a | 5380 | .module = THIS_MODULE, |
ba0e4d99 | 5381 | }; |
90fd131a FW |
5382 | struct nft_set_dump_ctx dump_ctx = { |
5383 | .set = set, | |
5384 | .ctx = ctx, | |
5385 | }; | |
ba0e4d99 | 5386 | |
90fd131a | 5387 | c.data = &dump_ctx; |
797d4980 | 5388 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
ba0e4d99 PNA |
5389 | } |
5390 | ||
5391 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
5392 | return -EINVAL; | |
5393 | ||
5394 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
5395 | err = nft_get_set_elem(&ctx, set, attr); | |
b53c1166 PNA |
5396 | if (err < 0) { |
5397 | NL_SET_BAD_ATTR(extack, attr); | |
ba0e4d99 | 5398 | break; |
b53c1166 | 5399 | } |
ba0e4d99 PNA |
5400 | } |
5401 | ||
5402 | return err; | |
5403 | } | |
5404 | ||
25e94a99 PNA |
5405 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
5406 | const struct nft_set *set, | |
5407 | const struct nft_set_elem *elem, | |
6fb721cf | 5408 | int event) |
d60ce62f | 5409 | { |
0854db2a | 5410 | struct nftables_pernet *nft_net; |
128ad332 PNA |
5411 | struct net *net = ctx->net; |
5412 | u32 portid = ctx->portid; | |
d60ce62f | 5413 | struct sk_buff *skb; |
6fb721cf | 5414 | u16 flags = 0; |
d60ce62f AB |
5415 | int err; |
5416 | ||
128ad332 | 5417 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 5418 | return; |
d60ce62f | 5419 | |
d60ce62f AB |
5420 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
5421 | if (skb == NULL) | |
5422 | goto err; | |
5423 | ||
6fb721cf PNA |
5424 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
5425 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
5426 | ||
d60ce62f AB |
5427 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, |
5428 | set, elem); | |
5429 | if (err < 0) { | |
5430 | kfree_skb(skb); | |
5431 | goto err; | |
5432 | } | |
5433 | ||
d59d2f82 | 5434 | nft_net = nft_pernet(net); |
0854db2a | 5435 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 5436 | return; |
d60ce62f | 5437 | err: |
25e94a99 | 5438 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
5439 | } |
5440 | ||
60319eb1 PNA |
5441 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
5442 | int msg_type, | |
5443 | struct nft_set *set) | |
5444 | { | |
5445 | struct nft_trans *trans; | |
5446 | ||
5447 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
5448 | if (trans == NULL) | |
5449 | return NULL; | |
5450 | ||
5451 | nft_trans_elem_set(trans) = set; | |
5452 | return trans; | |
5453 | } | |
5454 | ||
a7fc9368 PNA |
5455 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
5456 | const struct nft_set *set, | |
5457 | const struct nlattr *attr) | |
5458 | { | |
5459 | struct nft_expr *expr; | |
5460 | int err; | |
5461 | ||
5462 | expr = nft_expr_init(ctx, attr); | |
5463 | if (IS_ERR(expr)) | |
5464 | return expr; | |
5465 | ||
5466 | err = -EOPNOTSUPP; | |
a7fc9368 PNA |
5467 | if (expr->ops->type->flags & NFT_EXPR_GC) { |
5468 | if (set->flags & NFT_SET_TIMEOUT) | |
5469 | goto err_set_elem_expr; | |
5470 | if (!set->ops->gc_init) | |
5471 | goto err_set_elem_expr; | |
5472 | set->ops->gc_init(set); | |
5473 | } | |
5474 | ||
5475 | return expr; | |
5476 | ||
5477 | err_set_elem_expr: | |
5478 | nft_expr_destroy(ctx, expr); | |
5479 | return ERR_PTR(err); | |
5480 | } | |
5481 | ||
34aae2c2 PNA |
5482 | static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len) |
5483 | { | |
5484 | len += nft_set_ext_types[id].len; | |
5485 | if (len > tmpl->ext_len[id] || | |
5486 | len > U8_MAX) | |
5487 | return -1; | |
5488 | ||
5489 | return 0; | |
5490 | } | |
5491 | ||
5492 | static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id, | |
5493 | void *to, const void *from, u32 len) | |
5494 | { | |
5495 | if (nft_set_ext_check(tmpl, id, len) < 0) | |
5496 | return -1; | |
5497 | ||
5498 | memcpy(to, from, len); | |
5499 | ||
5500 | return 0; | |
5501 | } | |
5502 | ||
22fe54d5 PM |
5503 | void *nft_set_elem_init(const struct nft_set *set, |
5504 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
5505 | const u32 *key, const u32 *key_end, |
5506 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
5507 | { |
5508 | struct nft_set_ext *ext; | |
5509 | void *elem; | |
5510 | ||
5511 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
5512 | if (elem == NULL) | |
34aae2c2 | 5513 | return ERR_PTR(-ENOMEM); |
fe2811eb PM |
5514 | |
5515 | ext = nft_set_elem_ext(set, elem); | |
5516 | nft_set_ext_init(ext, tmpl); | |
5517 | ||
34aae2c2 PNA |
5518 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
5519 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY, | |
5520 | nft_set_ext_key(ext), key, set->klen) < 0) | |
5521 | goto err_ext_check; | |
5522 | ||
5523 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && | |
5524 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END, | |
5525 | nft_set_ext_key_end(ext), key_end, set->klen) < 0) | |
5526 | goto err_ext_check; | |
5527 | ||
5528 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && | |
5529 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA, | |
5530 | nft_set_ext_data(ext), data, set->dlen) < 0) | |
5531 | goto err_ext_check; | |
5532 | ||
79ebb5bb LGL |
5533 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
5534 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
5535 | if (expiration == 0) | |
5536 | *nft_set_ext_expiration(ext) += timeout; | |
5537 | } | |
c3e1b005 PM |
5538 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
5539 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
5540 | |
5541 | return elem; | |
34aae2c2 PNA |
5542 | |
5543 | err_ext_check: | |
5544 | kfree(elem); | |
5545 | ||
5546 | return ERR_PTR(-EINVAL); | |
fe2811eb PM |
5547 | } |
5548 | ||
563125a7 PNA |
5549 | static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5550 | struct nft_expr *expr) | |
475beb9c PNA |
5551 | { |
5552 | if (expr->ops->destroy_clone) { | |
5553 | expr->ops->destroy_clone(ctx, expr); | |
5554 | module_put(expr->ops->type->owner); | |
5555 | } else { | |
5556 | nf_tables_expr_destroy(ctx, expr); | |
5557 | } | |
5558 | } | |
5559 | ||
563125a7 PNA |
5560 | static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5561 | struct nft_set_elem_expr *elem_expr) | |
5562 | { | |
5563 | struct nft_expr *expr; | |
5564 | u32 size; | |
5565 | ||
5566 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
5567 | __nft_set_elem_expr_destroy(ctx, expr); | |
5568 | } | |
5569 | ||
61f9e292 LZ |
5570 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
5571 | bool destroy_expr) | |
61edafbb PM |
5572 | { |
5573 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
5574 | struct nft_ctx ctx = { |
5575 | .net = read_pnet(&set->net), | |
5576 | .family = set->table->family, | |
5577 | }; | |
61edafbb | 5578 | |
59105446 | 5579 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 5580 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 5581 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
563125a7 | 5582 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c | 5583 | nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext)); |
371ebcbb | 5584 | |
8aeff920 PNA |
5585 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
5586 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
5587 | kfree(elem); |
5588 | } | |
5589 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
5590 | ||
f8bb7889 PNA |
5591 | /* Only called from commit path, nft_setelem_data_deactivate() already deals |
5592 | * with the refcounting from the preparation phase. | |
59105446 | 5593 | */ |
3453c927 PNA |
5594 | static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
5595 | const struct nft_set *set, void *elem) | |
59105446 PNA |
5596 | { |
5597 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
5598 | ||
563125a7 | 5599 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c PNA |
5600 | nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext)); |
5601 | ||
59105446 PNA |
5602 | kfree(elem); |
5603 | } | |
5604 | ||
fca05d4d PNA |
5605 | int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
5606 | struct nft_expr *expr_array[]) | |
8cfd9b0f PNA |
5607 | { |
5608 | struct nft_expr *expr; | |
5609 | int err, i, k; | |
5610 | ||
5611 | for (i = 0; i < set->num_exprs; i++) { | |
42193ffd | 5612 | expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT); |
8cfd9b0f PNA |
5613 | if (!expr) |
5614 | goto err_expr; | |
5615 | ||
5616 | err = nft_expr_clone(expr, set->exprs[i]); | |
5617 | if (err < 0) { | |
c485c35f | 5618 | kfree(expr); |
8cfd9b0f PNA |
5619 | goto err_expr; |
5620 | } | |
5621 | expr_array[i] = expr; | |
5622 | } | |
5623 | ||
5624 | return 0; | |
5625 | ||
5626 | err_expr: | |
161b838e CIK |
5627 | for (k = i - 1; k >= 0; k--) |
5628 | nft_expr_destroy(ctx, expr_array[k]); | |
8cfd9b0f PNA |
5629 | |
5630 | return -ENOMEM; | |
5631 | } | |
5632 | ||
4d8f9065 | 5633 | static int nft_set_elem_expr_setup(struct nft_ctx *ctx, |
34aae2c2 | 5634 | const struct nft_set_ext_tmpl *tmpl, |
4d8f9065 PNA |
5635 | const struct nft_set_ext *ext, |
5636 | struct nft_expr *expr_array[], | |
5637 | u32 num_exprs) | |
563125a7 PNA |
5638 | { |
5639 | struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext); | |
34aae2c2 | 5640 | u32 len = sizeof(struct nft_set_elem_expr); |
4d8f9065 PNA |
5641 | struct nft_expr *expr; |
5642 | int i, err; | |
5643 | ||
34aae2c2 PNA |
5644 | if (num_exprs == 0) |
5645 | return 0; | |
5646 | ||
5647 | for (i = 0; i < num_exprs; i++) | |
5648 | len += expr_array[i]->ops->size; | |
5649 | ||
5650 | if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0) | |
5651 | return -EINVAL; | |
5652 | ||
4d8f9065 PNA |
5653 | for (i = 0; i < num_exprs; i++) { |
5654 | expr = nft_setelem_expr_at(elem_expr, elem_expr->size); | |
5655 | err = nft_expr_clone(expr, expr_array[i]); | |
5656 | if (err < 0) | |
5657 | goto err_elem_expr_setup; | |
5658 | ||
5659 | elem_expr->size += expr_array[i]->ops->size; | |
5660 | nft_expr_destroy(ctx, expr_array[i]); | |
5661 | expr_array[i] = NULL; | |
5662 | } | |
5663 | ||
5664 | return 0; | |
5665 | ||
5666 | err_elem_expr_setup: | |
5667 | for (; i < num_exprs; i++) { | |
5668 | nft_expr_destroy(ctx, expr_array[i]); | |
5669 | expr_array[i] = NULL; | |
5670 | } | |
563125a7 | 5671 | |
4d8f9065 | 5672 | return -ENOMEM; |
563125a7 PNA |
5673 | } |
5674 | ||
aaa31047 PNA |
5675 | struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
5676 | const struct nft_set *set) | |
5677 | { | |
5678 | struct nft_set_elem_catchall *catchall; | |
5679 | u8 genmask = nft_genmask_cur(net); | |
5680 | struct nft_set_ext *ext; | |
5681 | ||
5682 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5683 | ext = nft_set_elem_ext(set, catchall->elem); | |
5684 | if (nft_set_elem_active(ext, genmask) && | |
5685 | !nft_set_elem_expired(ext)) | |
5686 | return ext; | |
5687 | } | |
5688 | ||
5689 | return NULL; | |
5690 | } | |
5691 | EXPORT_SYMBOL_GPL(nft_set_catchall_lookup); | |
5692 | ||
5693 | void *nft_set_catchall_gc(const struct nft_set *set) | |
5694 | { | |
5695 | struct nft_set_elem_catchall *catchall, *next; | |
5696 | struct nft_set_ext *ext; | |
5697 | void *elem = NULL; | |
5698 | ||
5699 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5700 | ext = nft_set_elem_ext(set, catchall->elem); | |
5701 | ||
5702 | if (!nft_set_elem_expired(ext) || | |
5703 | nft_set_elem_mark_busy(ext)) | |
5704 | continue; | |
5705 | ||
5706 | elem = catchall->elem; | |
5707 | list_del_rcu(&catchall->list); | |
5708 | kfree_rcu(catchall, rcu); | |
5709 | break; | |
5710 | } | |
5711 | ||
5712 | return elem; | |
5713 | } | |
5714 | EXPORT_SYMBOL_GPL(nft_set_catchall_gc); | |
5715 | ||
5716 | static int nft_setelem_catchall_insert(const struct net *net, | |
5717 | struct nft_set *set, | |
5718 | const struct nft_set_elem *elem, | |
5719 | struct nft_set_ext **pext) | |
5720 | { | |
5721 | struct nft_set_elem_catchall *catchall; | |
5722 | u8 genmask = nft_genmask_next(net); | |
5723 | struct nft_set_ext *ext; | |
5724 | ||
5725 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5726 | ext = nft_set_elem_ext(set, catchall->elem); | |
5727 | if (nft_set_elem_active(ext, genmask)) { | |
5728 | *pext = ext; | |
5729 | return -EEXIST; | |
5730 | } | |
5731 | } | |
5732 | ||
5733 | catchall = kmalloc(sizeof(*catchall), GFP_KERNEL); | |
5734 | if (!catchall) | |
5735 | return -ENOMEM; | |
5736 | ||
5737 | catchall->elem = elem->priv; | |
5738 | list_add_tail_rcu(&catchall->list, &set->catchall_list); | |
5739 | ||
5740 | return 0; | |
5741 | } | |
5742 | ||
5743 | static int nft_setelem_insert(const struct net *net, | |
5744 | struct nft_set *set, | |
5745 | const struct nft_set_elem *elem, | |
5746 | struct nft_set_ext **ext, unsigned int flags) | |
5747 | { | |
5748 | int ret; | |
5749 | ||
5750 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5751 | ret = nft_setelem_catchall_insert(net, set, elem, ext); | |
5752 | else | |
5753 | ret = set->ops->insert(net, set, elem, ext); | |
5754 | ||
5755 | return ret; | |
5756 | } | |
5757 | ||
5758 | static bool nft_setelem_is_catchall(const struct nft_set *set, | |
5759 | const struct nft_set_elem *elem) | |
5760 | { | |
5761 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5762 | ||
5763 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
5764 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL) | |
5765 | return true; | |
5766 | ||
5767 | return false; | |
5768 | } | |
5769 | ||
5770 | static void nft_setelem_activate(struct net *net, struct nft_set *set, | |
5771 | struct nft_set_elem *elem) | |
5772 | { | |
5773 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5774 | ||
5775 | if (nft_setelem_is_catchall(set, elem)) { | |
5776 | nft_set_elem_change_active(net, set, ext); | |
5777 | nft_set_elem_clear_busy(ext); | |
5778 | } else { | |
5779 | set->ops->activate(net, set, elem); | |
5780 | } | |
5781 | } | |
5782 | ||
5783 | static int nft_setelem_catchall_deactivate(const struct net *net, | |
5784 | struct nft_set *set, | |
5785 | struct nft_set_elem *elem) | |
5786 | { | |
5787 | struct nft_set_elem_catchall *catchall; | |
5788 | struct nft_set_ext *ext; | |
5789 | ||
5790 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5791 | ext = nft_set_elem_ext(set, catchall->elem); | |
5792 | if (!nft_is_active(net, ext) || | |
5793 | nft_set_elem_mark_busy(ext)) | |
5794 | continue; | |
5795 | ||
5796 | kfree(elem->priv); | |
5797 | elem->priv = catchall->elem; | |
5798 | nft_set_elem_change_active(net, set, ext); | |
5799 | return 0; | |
5800 | } | |
5801 | ||
5802 | return -ENOENT; | |
5803 | } | |
5804 | ||
5805 | static int __nft_setelem_deactivate(const struct net *net, | |
5806 | struct nft_set *set, | |
5807 | struct nft_set_elem *elem) | |
5808 | { | |
5809 | void *priv; | |
5810 | ||
5811 | priv = set->ops->deactivate(net, set, elem); | |
5812 | if (!priv) | |
5813 | return -ENOENT; | |
5814 | ||
5815 | kfree(elem->priv); | |
5816 | elem->priv = priv; | |
5817 | set->ndeact++; | |
5818 | ||
5819 | return 0; | |
5820 | } | |
5821 | ||
5822 | static int nft_setelem_deactivate(const struct net *net, | |
5823 | struct nft_set *set, | |
5824 | struct nft_set_elem *elem, u32 flags) | |
5825 | { | |
5826 | int ret; | |
5827 | ||
5828 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5829 | ret = nft_setelem_catchall_deactivate(net, set, elem); | |
5830 | else | |
5831 | ret = __nft_setelem_deactivate(net, set, elem); | |
5832 | ||
5833 | return ret; | |
5834 | } | |
5835 | ||
5836 | static void nft_setelem_catchall_remove(const struct net *net, | |
5837 | const struct nft_set *set, | |
5838 | const struct nft_set_elem *elem) | |
5839 | { | |
5840 | struct nft_set_elem_catchall *catchall, *next; | |
5841 | ||
5842 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5843 | if (catchall->elem == elem->priv) { | |
5844 | list_del_rcu(&catchall->list); | |
ae089831 | 5845 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
5846 | break; |
5847 | } | |
5848 | } | |
5849 | } | |
5850 | ||
5851 | static void nft_setelem_remove(const struct net *net, | |
5852 | const struct nft_set *set, | |
5853 | const struct nft_set_elem *elem) | |
5854 | { | |
5855 | if (nft_setelem_is_catchall(set, elem)) | |
5856 | nft_setelem_catchall_remove(net, set, elem); | |
5857 | else | |
5858 | set->ops->remove(net, set, elem); | |
5859 | } | |
5860 | ||
88cccd90 PNA |
5861 | static bool nft_setelem_valid_key_end(const struct nft_set *set, |
5862 | struct nlattr **nla, u32 flags) | |
5863 | { | |
5864 | if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) == | |
5865 | (NFT_SET_CONCAT | NFT_SET_INTERVAL)) { | |
5866 | if (flags & NFT_SET_ELEM_INTERVAL_END) | |
5867 | return false; | |
5868 | if (!nla[NFTA_SET_ELEM_KEY_END] && | |
5869 | !(flags & NFT_SET_ELEM_CATCHALL)) | |
5870 | return false; | |
5871 | } else { | |
5872 | if (nla[NFTA_SET_ELEM_KEY_END]) | |
5873 | return false; | |
5874 | } | |
5875 | ||
5876 | return true; | |
5877 | } | |
5878 | ||
60319eb1 | 5879 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 5880 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 | 5881 | { |
8cfd9b0f | 5882 | struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {}; |
20a69341 | 5883 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
8aeff920 | 5884 | u8 genmask = nft_genmask_next(ctx->net); |
664899e8 | 5885 | u32 flags = 0, size = 0, num_exprs = 0; |
fe2811eb | 5886 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 5887 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
5888 | struct nft_set_elem elem; |
5889 | struct nft_set_binding *binding; | |
8aeff920 | 5890 | struct nft_object *obj = NULL; |
68e942e8 | 5891 | struct nft_userdata *udata; |
20a1452c | 5892 | struct nft_data_desc desc; |
20a69341 | 5893 | enum nft_registers dreg; |
60319eb1 | 5894 | struct nft_trans *trans; |
c3e1b005 | 5895 | u64 timeout; |
79ebb5bb | 5896 | u64 expiration; |
8cfd9b0f | 5897 | int err, i; |
68e942e8 | 5898 | u8 ulen; |
20a69341 | 5899 | |
8cb08174 JB |
5900 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5901 | nft_set_elem_policy, NULL); | |
20a69341 PM |
5902 | if (err < 0) |
5903 | return err; | |
5904 | ||
fe2811eb PM |
5905 | nft_set_ext_prepare(&tmpl); |
5906 | ||
0e9091d6 PNA |
5907 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5908 | if (err < 0) | |
5909 | return err; | |
aaa31047 PNA |
5910 | |
5911 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
5912 | return -EINVAL; | |
5913 | ||
c39ba4de PNA |
5914 | if (flags != 0) { |
5915 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
5916 | if (err < 0) | |
5917 | return err; | |
5918 | } | |
20a69341 PM |
5919 | |
5920 | if (set->flags & NFT_SET_MAP) { | |
5921 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 5922 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
5923 | return -EINVAL; |
5924 | } else { | |
5925 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
5926 | return -EINVAL; | |
5927 | } | |
5928 | ||
5a2f3dc3 PNA |
5929 | if (set->flags & NFT_SET_OBJECT) { |
5930 | if (!nla[NFTA_SET_ELEM_OBJREF] && | |
5931 | !(flags & NFT_SET_ELEM_INTERVAL_END)) | |
5932 | return -EINVAL; | |
5933 | } else { | |
5934 | if (nla[NFTA_SET_ELEM_OBJREF]) | |
5935 | return -EINVAL; | |
5936 | } | |
5937 | ||
88cccd90 PNA |
5938 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
5939 | return -EINVAL; | |
5940 | ||
bffc124b PNA |
5941 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
5942 | (nla[NFTA_SET_ELEM_DATA] || | |
5943 | nla[NFTA_SET_ELEM_OBJREF] || | |
5944 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
5945 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
5946 | nla[NFTA_SET_ELEM_USERDATA] || | |
48b0ae04 | 5947 | nla[NFTA_SET_ELEM_EXPR] || |
4963674c | 5948 | nla[NFTA_SET_ELEM_KEY_END] || |
48b0ae04 | 5949 | nla[NFTA_SET_ELEM_EXPRESSIONS])) |
bffc124b PNA |
5950 | return -EINVAL; |
5951 | ||
c3e1b005 PM |
5952 | timeout = 0; |
5953 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
5954 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
5955 | return -EINVAL; | |
8e1102d5 FW |
5956 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
5957 | &timeout); | |
5958 | if (err) | |
5959 | return err; | |
c3e1b005 PM |
5960 | } else if (set->flags & NFT_SET_TIMEOUT) { |
5961 | timeout = set->timeout; | |
5962 | } | |
5963 | ||
79ebb5bb LGL |
5964 | expiration = 0; |
5965 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
5966 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
5967 | return -EINVAL; | |
5968 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
5969 | &expiration); | |
5970 | if (err) | |
5971 | return err; | |
5972 | } | |
5973 | ||
8cfd9b0f PNA |
5974 | if (nla[NFTA_SET_ELEM_EXPR]) { |
5975 | struct nft_expr *expr; | |
5976 | ||
664899e8 | 5977 | if (set->num_exprs && set->num_exprs != 1) |
8cfd9b0f PNA |
5978 | return -EOPNOTSUPP; |
5979 | ||
40944452 PNA |
5980 | expr = nft_set_elem_expr_alloc(ctx, set, |
5981 | nla[NFTA_SET_ELEM_EXPR]); | |
5982 | if (IS_ERR(expr)) | |
5983 | return PTR_ERR(expr); | |
65038428 | 5984 | |
8cfd9b0f | 5985 | expr_array[0] = expr; |
664899e8 | 5986 | num_exprs = 1; |
65038428 | 5987 | |
664899e8 | 5988 | if (set->num_exprs && set->exprs[0]->ops != expr->ops) { |
8cfd9b0f | 5989 | err = -EOPNOTSUPP; |
65038428 | 5990 | goto err_set_elem_expr; |
8cfd9b0f | 5991 | } |
48b0ae04 PNA |
5992 | } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) { |
5993 | struct nft_expr *expr; | |
5994 | struct nlattr *tmp; | |
5995 | int left; | |
65038428 | 5996 | |
48b0ae04 PNA |
5997 | i = 0; |
5998 | nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) { | |
664899e8 PNA |
5999 | if (i == NFT_SET_EXPR_MAX || |
6000 | (set->num_exprs && set->num_exprs == i)) { | |
48b0ae04 PNA |
6001 | err = -E2BIG; |
6002 | goto err_set_elem_expr; | |
6003 | } | |
6004 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
6005 | err = -EINVAL; | |
6006 | goto err_set_elem_expr; | |
6007 | } | |
6008 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
6009 | if (IS_ERR(expr)) { | |
6010 | err = PTR_ERR(expr); | |
6011 | goto err_set_elem_expr; | |
6012 | } | |
6013 | expr_array[i] = expr; | |
664899e8 | 6014 | num_exprs++; |
48b0ae04 | 6015 | |
664899e8 | 6016 | if (set->num_exprs && expr->ops != set->exprs[i]->ops) { |
48b0ae04 PNA |
6017 | err = -EOPNOTSUPP; |
6018 | goto err_set_elem_expr; | |
6019 | } | |
6020 | i++; | |
6021 | } | |
664899e8 | 6022 | if (set->num_exprs && set->num_exprs != i) { |
48b0ae04 | 6023 | err = -EOPNOTSUPP; |
65038428 | 6024 | goto err_set_elem_expr; |
48b0ae04 | 6025 | } |
8cfd9b0f PNA |
6026 | } else if (set->num_exprs > 0) { |
6027 | err = nft_set_elem_expr_clone(ctx, set, expr_array); | |
6028 | if (err < 0) | |
6029 | goto err_set_elem_expr_clone; | |
664899e8 PNA |
6030 | |
6031 | num_exprs = set->num_exprs; | |
40944452 PNA |
6032 | } |
6033 | ||
aaa31047 PNA |
6034 | if (nla[NFTA_SET_ELEM_KEY]) { |
6035 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6036 | nla[NFTA_SET_ELEM_KEY]); | |
6037 | if (err < 0) | |
6038 | goto err_set_elem_expr; | |
20a69341 | 6039 | |
c39ba4de PNA |
6040 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6041 | if (err < 0) | |
6042 | goto err_parse_key; | |
aaa31047 | 6043 | } |
7b225d0b PNA |
6044 | |
6045 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
6046 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6047 | nla[NFTA_SET_ELEM_KEY_END]); | |
6048 | if (err < 0) | |
6049 | goto err_parse_key; | |
6050 | ||
c39ba4de PNA |
6051 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6052 | if (err < 0) | |
6053 | goto err_parse_key_end; | |
7b225d0b PNA |
6054 | } |
6055 | ||
c3e1b005 | 6056 | if (timeout > 0) { |
c39ba4de PNA |
6057 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); |
6058 | if (err < 0) | |
6059 | goto err_parse_key_end; | |
6060 | ||
6061 | if (timeout != set->timeout) { | |
6062 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); | |
6063 | if (err < 0) | |
6064 | goto err_parse_key_end; | |
6065 | } | |
c3e1b005 | 6066 | } |
fe2811eb | 6067 | |
664899e8 PNA |
6068 | if (num_exprs) { |
6069 | for (i = 0; i < num_exprs; i++) | |
563125a7 PNA |
6070 | size += expr_array[i]->ops->size; |
6071 | ||
c39ba4de PNA |
6072 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS, |
6073 | sizeof(struct nft_set_elem_expr) + size); | |
6074 | if (err < 0) | |
6075 | goto err_parse_key_end; | |
563125a7 | 6076 | } |
40944452 | 6077 | |
8aeff920 | 6078 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
4d44175a FW |
6079 | obj = nft_obj_lookup(ctx->net, ctx->table, |
6080 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 6081 | set->objtype, genmask); |
8aeff920 PNA |
6082 | if (IS_ERR(obj)) { |
6083 | err = PTR_ERR(obj); | |
7b225d0b | 6084 | goto err_parse_key_end; |
8aeff920 | 6085 | } |
c39ba4de PNA |
6086 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); |
6087 | if (err < 0) | |
6088 | goto err_parse_key_end; | |
8aeff920 PNA |
6089 | } |
6090 | ||
20a69341 | 6091 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
fdb9c405 PNA |
6092 | err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val, |
6093 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 | 6094 | if (err < 0) |
7b225d0b | 6095 | goto err_parse_key_end; |
20a69341 | 6096 | |
20a69341 PM |
6097 | dreg = nft_type_to_reg(set->dtype); |
6098 | list_for_each_entry(binding, &set->bindings, list) { | |
6099 | struct nft_ctx bind_ctx = { | |
58c78e10 | 6100 | .net = ctx->net, |
36596dad | 6101 | .family = ctx->family, |
20a69341 | 6102 | .table = ctx->table, |
7c95f6d8 | 6103 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
6104 | }; |
6105 | ||
11113e19 PM |
6106 | if (!(binding->flags & NFT_SET_MAP)) |
6107 | continue; | |
6108 | ||
1ec10212 | 6109 | err = nft_validate_register_store(&bind_ctx, dreg, |
fdb9c405 | 6110 | &elem.data.val, |
20a1452c | 6111 | desc.type, desc.len); |
20a69341 | 6112 | if (err < 0) |
7b225d0b | 6113 | goto err_parse_data; |
a654de8f | 6114 | |
20a1452c | 6115 | if (desc.type == NFT_DATA_VERDICT && |
fdb9c405 PNA |
6116 | (elem.data.val.verdict.code == NFT_GOTO || |
6117 | elem.data.val.verdict.code == NFT_JUMP)) | |
a654de8f PNA |
6118 | nft_validate_state_update(ctx->net, |
6119 | NFT_VALIDATE_NEED); | |
20a69341 | 6120 | } |
fe2811eb | 6121 | |
c39ba4de PNA |
6122 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
6123 | if (err < 0) | |
6124 | goto err_parse_data; | |
20a69341 PM |
6125 | } |
6126 | ||
68e942e8 PM |
6127 | /* The full maximum length of userdata can exceed the maximum |
6128 | * offset value (U8_MAX) for following extensions, therefor it | |
6129 | * must be the last extension added. | |
6130 | */ | |
6131 | ulen = 0; | |
6132 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
6133 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
c39ba4de PNA |
6134 | if (ulen > 0) { |
6135 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
6136 | ulen); | |
6137 | if (err < 0) | |
6138 | goto err_parse_data; | |
6139 | } | |
68e942e8 PM |
6140 | } |
6141 | ||
7b225d0b | 6142 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
fdb9c405 | 6143 | elem.key_end.val.data, elem.data.val.data, |
33758c89 | 6144 | timeout, expiration, GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6145 | if (IS_ERR(elem.priv)) { |
6146 | err = PTR_ERR(elem.priv); | |
7b225d0b | 6147 | goto err_parse_data; |
34aae2c2 | 6148 | } |
fe2811eb PM |
6149 | |
6150 | ext = nft_set_elem_ext(set, elem.priv); | |
6151 | if (flags) | |
6152 | *nft_set_ext_flags(ext) = flags; | |
34aae2c2 | 6153 | |
68e942e8 | 6154 | if (ulen > 0) { |
34aae2c2 PNA |
6155 | if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) { |
6156 | err = -EINVAL; | |
6157 | goto err_elem_userdata; | |
6158 | } | |
68e942e8 PM |
6159 | udata = nft_set_ext_userdata(ext); |
6160 | udata->len = ulen - 1; | |
6161 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
6162 | } | |
8aeff920 PNA |
6163 | if (obj) { |
6164 | *nft_set_ext_obj(ext) = obj; | |
6165 | obj->use++; | |
6166 | } | |
34aae2c2 | 6167 | err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs); |
4d8f9065 | 6168 | if (err < 0) |
34aae2c2 | 6169 | goto err_elem_free; |
fe2811eb | 6170 | |
60319eb1 | 6171 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
4d8f9065 PNA |
6172 | if (trans == NULL) { |
6173 | err = -ENOMEM; | |
34aae2c2 | 6174 | goto err_elem_free; |
4d8f9065 | 6175 | } |
60319eb1 | 6176 | |
69086658 | 6177 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
aaa31047 PNA |
6178 | |
6179 | err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags); | |
c016c7e4 PNA |
6180 | if (err) { |
6181 | if (err == -EEXIST) { | |
9744a6fc PNA |
6182 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
6183 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
6184 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
77a92189 | 6185 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) |
7b225d0b | 6186 | goto err_element_clash; |
8aeff920 PNA |
6187 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
6188 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
6189 | memcmp(nft_set_ext_data(ext), | |
6190 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
6191 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
6192 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
6193 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
77a92189 | 6194 | goto err_element_clash; |
c016c7e4 PNA |
6195 | else if (!(nlmsg_flags & NLM_F_EXCL)) |
6196 | err = 0; | |
8c2d45b2 PNA |
6197 | } else if (err == -ENOTEMPTY) { |
6198 | /* ENOTEMPTY reports overlapping between this element | |
6199 | * and an existing one. | |
6200 | */ | |
6201 | err = -EEXIST; | |
c016c7e4 | 6202 | } |
7b225d0b | 6203 | goto err_element_clash; |
c016c7e4 | 6204 | } |
20a69341 | 6205 | |
aaa31047 | 6206 | if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size && |
35d0ac90 PNA |
6207 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { |
6208 | err = -ENFILE; | |
7b225d0b | 6209 | goto err_set_full; |
35d0ac90 PNA |
6210 | } |
6211 | ||
60319eb1 | 6212 | nft_trans_elem(trans) = elem; |
0854db2a | 6213 | nft_trans_commit_list_add_tail(ctx->net, trans); |
20a69341 PM |
6214 | return 0; |
6215 | ||
7b225d0b | 6216 | err_set_full: |
aaa31047 | 6217 | nft_setelem_remove(ctx->net, set, &elem); |
7b225d0b | 6218 | err_element_clash: |
60319eb1 | 6219 | kfree(trans); |
34aae2c2 | 6220 | err_elem_free: |
b91d9036 TY |
6221 | if (obj) |
6222 | obj->use--; | |
34aae2c2 | 6223 | err_elem_userdata: |
475beb9c | 6224 | nf_tables_set_elem_destroy(ctx, set, elem.priv); |
7b225d0b | 6225 | err_parse_data: |
20a69341 | 6226 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
fdb9c405 | 6227 | nft_data_release(&elem.data.val, desc.type); |
7b225d0b PNA |
6228 | err_parse_key_end: |
6229 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
6230 | err_parse_key: | |
20a1452c | 6231 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
40944452 | 6232 | err_set_elem_expr: |
664899e8 | 6233 | for (i = 0; i < num_exprs && expr_array[i]; i++) |
8cfd9b0f PNA |
6234 | nft_expr_destroy(ctx, expr_array[i]); |
6235 | err_set_elem_expr_clone: | |
20a69341 PM |
6236 | return err; |
6237 | } | |
6238 | ||
7dab8ee3 PNA |
6239 | static int nf_tables_newsetelem(struct sk_buff *skb, |
6240 | const struct nfnl_info *info, | |
6241 | const struct nlattr * const nla[]) | |
20a69341 | 6242 | { |
7dab8ee3 PNA |
6243 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
6244 | struct netlink_ext_ack *extack = info->extack; | |
6245 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6246 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6247 | struct net *net = info->net; |
20a69341 | 6248 | const struct nlattr *attr; |
e2b750d7 | 6249 | struct nft_table *table; |
20a69341 PM |
6250 | struct nft_set *set; |
6251 | struct nft_ctx ctx; | |
a654de8f | 6252 | int rem, err; |
20a69341 | 6253 | |
7d5570ca PNA |
6254 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
6255 | return -EINVAL; | |
6256 | ||
e2b750d7 PNA |
6257 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6258 | genmask, NETLINK_CB(skb).portid); | |
6259 | if (IS_ERR(table)) { | |
6260 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6261 | return PTR_ERR(table); | |
6262 | } | |
20a69341 | 6263 | |
e2b750d7 | 6264 | set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET], |
a3073c17 PNA |
6265 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); |
6266 | if (IS_ERR(set)) | |
6267 | return PTR_ERR(set); | |
958bee14 | 6268 | |
20a69341 PM |
6269 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
6270 | return -EBUSY; | |
6271 | ||
e2b750d7 PNA |
6272 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6273 | ||
20a69341 | 6274 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7dab8ee3 | 6275 | err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags); |
b53c1166 PNA |
6276 | if (err < 0) { |
6277 | NL_SET_BAD_ATTR(extack, attr); | |
a654de8f | 6278 | return err; |
b53c1166 | 6279 | } |
20a69341 | 6280 | } |
a654de8f | 6281 | |
0854db2a | 6282 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
e2b750d7 | 6283 | return nft_table_validate(net, table); |
a654de8f PNA |
6284 | |
6285 | return 0; | |
20a69341 PM |
6286 | } |
6287 | ||
59105446 PNA |
6288 | /** |
6289 | * nft_data_hold - hold a nft_data item | |
6290 | * | |
6291 | * @data: struct nft_data to release | |
6292 | * @type: type of data | |
6293 | * | |
6294 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
6295 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
6296 | * NFT_GOTO verdicts. This function must be called on active data objects | |
6297 | * from the second phase of the commit protocol. | |
6298 | */ | |
bb7b40ae | 6299 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 | 6300 | { |
d0e2c7de PNA |
6301 | struct nft_chain *chain; |
6302 | struct nft_rule *rule; | |
6303 | ||
59105446 PNA |
6304 | if (type == NFT_DATA_VERDICT) { |
6305 | switch (data->verdict.code) { | |
6306 | case NFT_JUMP: | |
6307 | case NFT_GOTO: | |
d0e2c7de PNA |
6308 | chain = data->verdict.chain; |
6309 | chain->use++; | |
6310 | ||
6311 | if (!nft_chain_is_bound(chain)) | |
6312 | break; | |
6313 | ||
6314 | chain->table->use++; | |
6315 | list_for_each_entry(rule, &chain->rules, list) | |
6316 | chain->use++; | |
6317 | ||
6318 | nft_chain_add(chain->table, chain); | |
59105446 PNA |
6319 | break; |
6320 | } | |
6321 | } | |
6322 | } | |
6323 | ||
f8bb7889 PNA |
6324 | static void nft_setelem_data_activate(const struct net *net, |
6325 | const struct nft_set *set, | |
6326 | struct nft_set_elem *elem) | |
59105446 PNA |
6327 | { |
6328 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6329 | ||
6330 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6331 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
6332 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6333 | (*nft_set_ext_obj(ext))->use++; | |
6334 | } | |
6335 | ||
f8bb7889 PNA |
6336 | static void nft_setelem_data_deactivate(const struct net *net, |
6337 | const struct nft_set *set, | |
6338 | struct nft_set_elem *elem) | |
59105446 PNA |
6339 | { |
6340 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6341 | ||
6342 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6343 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
6344 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6345 | (*nft_set_ext_obj(ext))->use--; | |
6346 | } | |
6347 | ||
60319eb1 | 6348 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
6349 | const struct nlattr *attr) |
6350 | { | |
6351 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 6352 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 6353 | struct nft_set_elem elem; |
3971ca14 | 6354 | struct nft_set_ext *ext; |
60319eb1 | 6355 | struct nft_trans *trans; |
3971ca14 | 6356 | u32 flags = 0; |
20a69341 PM |
6357 | int err; |
6358 | ||
8cb08174 JB |
6359 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6360 | nft_set_elem_policy, NULL); | |
20a69341 | 6361 | if (err < 0) |
20a1452c | 6362 | return err; |
20a69341 | 6363 | |
aaa31047 PNA |
6364 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6365 | if (err < 0) | |
6366 | return err; | |
6367 | ||
6368 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
20a1452c | 6369 | return -EINVAL; |
20a69341 | 6370 | |
88cccd90 PNA |
6371 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6372 | return -EINVAL; | |
6373 | ||
3971ca14 PNA |
6374 | nft_set_ext_prepare(&tmpl); |
6375 | ||
c39ba4de PNA |
6376 | if (flags != 0) { |
6377 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6378 | if (err < 0) | |
6379 | return err; | |
6380 | } | |
3971ca14 | 6381 | |
aaa31047 PNA |
6382 | if (nla[NFTA_SET_ELEM_KEY]) { |
6383 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6384 | nla[NFTA_SET_ELEM_KEY]); | |
6385 | if (err < 0) | |
6386 | return err; | |
20a69341 | 6387 | |
c39ba4de PNA |
6388 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6389 | if (err < 0) | |
6390 | goto fail_elem; | |
aaa31047 | 6391 | } |
3971ca14 | 6392 | |
7b225d0b PNA |
6393 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
6394 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6395 | nla[NFTA_SET_ELEM_KEY_END]); | |
6396 | if (err < 0) | |
c39ba4de | 6397 | goto fail_elem; |
7b225d0b | 6398 | |
c39ba4de PNA |
6399 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6400 | if (err < 0) | |
6401 | goto fail_elem_key_end; | |
7b225d0b PNA |
6402 | } |
6403 | ||
3971ca14 | 6404 | err = -ENOMEM; |
7b225d0b PNA |
6405 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
6406 | elem.key_end.val.data, NULL, 0, 0, | |
33758c89 | 6407 | GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6408 | if (IS_ERR(elem.priv)) { |
6409 | err = PTR_ERR(elem.priv); | |
c39ba4de | 6410 | goto fail_elem_key_end; |
34aae2c2 | 6411 | } |
3971ca14 PNA |
6412 | |
6413 | ext = nft_set_elem_ext(set, elem.priv); | |
6414 | if (flags) | |
6415 | *nft_set_ext_flags(ext) = flags; | |
6416 | ||
60319eb1 | 6417 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
6418 | if (trans == NULL) |
6419 | goto fail_trans; | |
20a69341 | 6420 | |
aaa31047 PNA |
6421 | err = nft_setelem_deactivate(ctx->net, set, &elem, flags); |
6422 | if (err < 0) | |
20a1452c | 6423 | goto fail_ops; |
cc02e457 | 6424 | |
f8bb7889 | 6425 | nft_setelem_data_deactivate(ctx->net, set, &elem); |
59105446 | 6426 | |
60319eb1 | 6427 | nft_trans_elem(trans) = elem; |
0854db2a | 6428 | nft_trans_commit_list_add_tail(ctx->net, trans); |
0dc13625 | 6429 | return 0; |
cc02e457 | 6430 | |
20a1452c | 6431 | fail_ops: |
cc02e457 | 6432 | kfree(trans); |
20a1452c | 6433 | fail_trans: |
3971ca14 | 6434 | kfree(elem.priv); |
c39ba4de PNA |
6435 | fail_elem_key_end: |
6436 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
20a1452c PNA |
6437 | fail_elem: |
6438 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
6439 | return err; |
6440 | } | |
6441 | ||
e6ba7cb6 PNA |
6442 | static int nft_setelem_flush(const struct nft_ctx *ctx, |
6443 | struct nft_set *set, | |
6444 | const struct nft_set_iter *iter, | |
6445 | struct nft_set_elem *elem) | |
8411b644 PNA |
6446 | { |
6447 | struct nft_trans *trans; | |
6448 | int err; | |
6449 | ||
6450 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6451 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
6452 | if (!trans) | |
6453 | return -ENOMEM; | |
6454 | ||
1ba1c414 | 6455 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
6456 | err = -ENOENT; |
6457 | goto err1; | |
6458 | } | |
b2c11e4b | 6459 | set->ndeact++; |
8411b644 | 6460 | |
f8bb7889 | 6461 | nft_setelem_data_deactivate(ctx->net, set, elem); |
de70185d PNA |
6462 | nft_trans_elem_set(trans) = set; |
6463 | nft_trans_elem(trans) = *elem; | |
0854db2a | 6464 | nft_trans_commit_list_add_tail(ctx->net, trans); |
8411b644 PNA |
6465 | |
6466 | return 0; | |
6467 | err1: | |
6468 | kfree(trans); | |
6469 | return err; | |
6470 | } | |
6471 | ||
aaa31047 PNA |
6472 | static int __nft_set_catchall_flush(const struct nft_ctx *ctx, |
6473 | struct nft_set *set, | |
6474 | struct nft_set_elem *elem) | |
6475 | { | |
6476 | struct nft_trans *trans; | |
6477 | ||
6478 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6479 | sizeof(struct nft_trans_elem), GFP_KERNEL); | |
6480 | if (!trans) | |
6481 | return -ENOMEM; | |
6482 | ||
6483 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
6484 | nft_trans_elem_set(trans) = set; | |
6485 | nft_trans_elem(trans) = *elem; | |
6486 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
6487 | ||
6488 | return 0; | |
6489 | } | |
6490 | ||
6491 | static int nft_set_catchall_flush(const struct nft_ctx *ctx, | |
6492 | struct nft_set *set) | |
6493 | { | |
6494 | u8 genmask = nft_genmask_next(ctx->net); | |
6495 | struct nft_set_elem_catchall *catchall; | |
6496 | struct nft_set_elem elem; | |
6497 | struct nft_set_ext *ext; | |
6498 | int ret = 0; | |
6499 | ||
6500 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
6501 | ext = nft_set_elem_ext(set, catchall->elem); | |
6502 | if (!nft_set_elem_active(ext, genmask) || | |
6503 | nft_set_elem_mark_busy(ext)) | |
6504 | continue; | |
6505 | ||
6506 | elem.priv = catchall->elem; | |
6507 | ret = __nft_set_catchall_flush(ctx, set, &elem); | |
6508 | if (ret < 0) | |
6509 | break; | |
6510 | } | |
6511 | ||
6512 | return ret; | |
6513 | } | |
6514 | ||
e6ba7cb6 PNA |
6515 | static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) |
6516 | { | |
6517 | struct nft_set_iter iter = { | |
6518 | .genmask = genmask, | |
6519 | .fn = nft_setelem_flush, | |
6520 | }; | |
6521 | ||
6522 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
6523 | if (!iter.err) |
6524 | iter.err = nft_set_catchall_flush(ctx, set); | |
e6ba7cb6 PNA |
6525 | |
6526 | return iter.err; | |
6527 | } | |
6528 | ||
7dab8ee3 PNA |
6529 | static int nf_tables_delsetelem(struct sk_buff *skb, |
6530 | const struct nfnl_info *info, | |
6531 | const struct nlattr * const nla[]) | |
20a69341 | 6532 | { |
7dab8ee3 PNA |
6533 | struct netlink_ext_ack *extack = info->extack; |
6534 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6535 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6536 | struct net *net = info->net; |
20a69341 | 6537 | const struct nlattr *attr; |
e2b750d7 | 6538 | struct nft_table *table; |
20a69341 PM |
6539 | struct nft_set *set; |
6540 | struct nft_ctx ctx; | |
60319eb1 | 6541 | int rem, err = 0; |
20a69341 | 6542 | |
e2b750d7 PNA |
6543 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6544 | genmask, NETLINK_CB(skb).portid); | |
6545 | if (IS_ERR(table)) { | |
6546 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6547 | return PTR_ERR(table); | |
6548 | } | |
20a69341 | 6549 | |
e2b750d7 | 6550 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
6551 | if (IS_ERR(set)) |
6552 | return PTR_ERR(set); | |
6553 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
6554 | return -EBUSY; | |
6555 | ||
e2b750d7 PNA |
6556 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6557 | ||
e6ba7cb6 PNA |
6558 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) |
6559 | return nft_set_flush(&ctx, set, genmask); | |
8411b644 | 6560 | |
20a69341 PM |
6561 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
6562 | err = nft_del_setelem(&ctx, set, attr); | |
b53c1166 PNA |
6563 | if (err < 0) { |
6564 | NL_SET_BAD_ATTR(extack, attr); | |
60319eb1 | 6565 | break; |
b53c1166 | 6566 | } |
20a69341 | 6567 | } |
60319eb1 | 6568 | return err; |
20a69341 PM |
6569 | } |
6570 | ||
cfed7e1b PM |
6571 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
6572 | { | |
6573 | struct nft_set_gc_batch *gcb; | |
6574 | unsigned int i; | |
6575 | ||
6576 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
6577 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 6578 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
6579 | kfree(gcb); |
6580 | } | |
cfed7e1b PM |
6581 | |
6582 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
6583 | gfp_t gfp) | |
6584 | { | |
6585 | struct nft_set_gc_batch *gcb; | |
6586 | ||
6587 | gcb = kzalloc(sizeof(*gcb), gfp); | |
6588 | if (gcb == NULL) | |
6589 | return gcb; | |
6590 | gcb->head.set = set; | |
6591 | return gcb; | |
6592 | } | |
cfed7e1b | 6593 | |
e5009240 PNA |
6594 | /* |
6595 | * Stateful objects | |
6596 | */ | |
6597 | ||
6598 | /** | |
6599 | * nft_register_obj- register nf_tables stateful object type | |
3db86c39 | 6600 | * @obj_type: object type |
e5009240 PNA |
6601 | * |
6602 | * Registers the object type for use with nf_tables. Returns zero on | |
6603 | * success or a negative errno code otherwise. | |
6604 | */ | |
6605 | int nft_register_obj(struct nft_object_type *obj_type) | |
6606 | { | |
6607 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
6608 | return -EINVAL; | |
6609 | ||
6610 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6611 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
6612 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6613 | return 0; | |
6614 | } | |
6615 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
6616 | ||
6617 | /** | |
6618 | * nft_unregister_obj - unregister nf_tables object type | |
3db86c39 | 6619 | * @obj_type: object type |
e5009240 PNA |
6620 | * |
6621 | * Unregisters the object type for use with nf_tables. | |
6622 | */ | |
6623 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
6624 | { | |
6625 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6626 | list_del_rcu(&obj_type->list); | |
6627 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6628 | } | |
6629 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
6630 | ||
4d44175a FW |
6631 | struct nft_object *nft_obj_lookup(const struct net *net, |
6632 | const struct nft_table *table, | |
cac20fcd PNA |
6633 | const struct nlattr *nla, u32 objtype, |
6634 | u8 genmask) | |
e5009240 | 6635 | { |
4d44175a FW |
6636 | struct nft_object_hash_key k = { .table = table }; |
6637 | char search[NFT_OBJ_MAXNAMELEN]; | |
6638 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
6639 | struct nft_object *obj; |
6640 | ||
872f6903 | 6641 | nla_strscpy(search, nla, sizeof(search)); |
4d44175a FW |
6642 | k.name = search; |
6643 | ||
6644 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
6645 | !lockdep_commit_lock_is_held(net)); | |
6646 | ||
6647 | rcu_read_lock(); | |
6648 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
6649 | if (!list) | |
6650 | goto out; | |
6651 | ||
6652 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
6653 | if (objtype == obj->ops->type->type && | |
6654 | nft_active_genmask(obj, genmask)) { | |
6655 | rcu_read_unlock(); | |
e5009240 | 6656 | return obj; |
4d44175a | 6657 | } |
e5009240 | 6658 | } |
4d44175a FW |
6659 | out: |
6660 | rcu_read_unlock(); | |
e5009240 PNA |
6661 | return ERR_PTR(-ENOENT); |
6662 | } | |
cac20fcd | 6663 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 6664 | |
cac20fcd PNA |
6665 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
6666 | const struct nlattr *nla, | |
6667 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
6668 | { |
6669 | struct nft_object *obj; | |
6670 | ||
6671 | list_for_each_entry(obj, &table->objects, list) { | |
6672 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
6673 | objtype == obj->ops->type->type && | |
6674 | nft_active_genmask(obj, genmask)) | |
6675 | return obj; | |
6676 | } | |
6677 | return ERR_PTR(-ENOENT); | |
6678 | } | |
6679 | ||
e5009240 | 6680 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
6681 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
6682 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
6683 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
6684 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
6685 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
6686 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 6687 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
b131c964 JGG |
6688 | [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY, |
6689 | .len = NFT_USERDATA_MAXLEN }, | |
e5009240 PNA |
6690 | }; |
6691 | ||
84fba055 FW |
6692 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
6693 | const struct nft_object_type *type, | |
e5009240 PNA |
6694 | const struct nlattr *attr) |
6695 | { | |
5b4c6e38 | 6696 | struct nlattr **tb; |
dfc46034 | 6697 | const struct nft_object_ops *ops; |
e5009240 | 6698 | struct nft_object *obj; |
5b4c6e38 GS |
6699 | int err = -ENOMEM; |
6700 | ||
6701 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
6702 | if (!tb) | |
6703 | goto err1; | |
e5009240 PNA |
6704 | |
6705 | if (attr) { | |
8cb08174 JB |
6706 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
6707 | type->policy, NULL); | |
e5009240 | 6708 | if (err < 0) |
5b4c6e38 | 6709 | goto err2; |
e5009240 PNA |
6710 | } else { |
6711 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
6712 | } | |
6713 | ||
dfc46034 PBG |
6714 | if (type->select_ops) { |
6715 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
6716 | if (IS_ERR(ops)) { | |
6717 | err = PTR_ERR(ops); | |
5b4c6e38 | 6718 | goto err2; |
dfc46034 PBG |
6719 | } |
6720 | } else { | |
6721 | ops = type->ops; | |
6722 | } | |
6723 | ||
e5009240 | 6724 | err = -ENOMEM; |
33758c89 | 6725 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT); |
5b4c6e38 GS |
6726 | if (!obj) |
6727 | goto err2; | |
e5009240 | 6728 | |
dfc46034 | 6729 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 6730 | if (err < 0) |
5b4c6e38 | 6731 | goto err3; |
e5009240 | 6732 | |
dfc46034 PBG |
6733 | obj->ops = ops; |
6734 | ||
5b4c6e38 | 6735 | kfree(tb); |
e5009240 | 6736 | return obj; |
5b4c6e38 | 6737 | err3: |
e5009240 | 6738 | kfree(obj); |
5b4c6e38 GS |
6739 | err2: |
6740 | kfree(tb); | |
e5009240 PNA |
6741 | err1: |
6742 | return ERR_PTR(err); | |
6743 | } | |
6744 | ||
6745 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 6746 | struct nft_object *obj, bool reset) |
e5009240 PNA |
6747 | { |
6748 | struct nlattr *nest; | |
6749 | ||
ae0be8de | 6750 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
6751 | if (!nest) |
6752 | goto nla_put_failure; | |
dfc46034 | 6753 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
6754 | goto nla_put_failure; |
6755 | nla_nest_end(skb, nest); | |
6756 | return 0; | |
6757 | ||
6758 | nla_put_failure: | |
6759 | return -1; | |
6760 | } | |
6761 | ||
6762 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
6763 | { | |
6764 | const struct nft_object_type *type; | |
6765 | ||
6766 | list_for_each_entry(type, &nf_tables_objects, list) { | |
6767 | if (objtype == type->type) | |
6768 | return type; | |
6769 | } | |
6770 | return NULL; | |
6771 | } | |
6772 | ||
452238e8 FW |
6773 | static const struct nft_object_type * |
6774 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
6775 | { |
6776 | const struct nft_object_type *type; | |
6777 | ||
6778 | type = __nft_obj_type_get(objtype); | |
6779 | if (type != NULL && try_module_get(type->owner)) | |
6780 | return type; | |
6781 | ||
f102d66b | 6782 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
6783 | #ifdef CONFIG_MODULES |
6784 | if (type == NULL) { | |
eb014de4 | 6785 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
6786 | return ERR_PTR(-EAGAIN); |
6787 | } | |
6788 | #endif | |
6789 | return ERR_PTR(-ENOENT); | |
6790 | } | |
6791 | ||
d62d0ba9 FFM |
6792 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
6793 | const struct nft_object_type *type, | |
6794 | const struct nlattr *attr, | |
6795 | struct nft_object *obj) | |
6796 | { | |
6797 | struct nft_object *newobj; | |
6798 | struct nft_trans *trans; | |
dad3bdee FW |
6799 | int err = -ENOMEM; |
6800 | ||
6801 | if (!try_module_get(type->owner)) | |
6802 | return -ENOENT; | |
d62d0ba9 FFM |
6803 | |
6804 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
6805 | sizeof(struct nft_trans_obj)); | |
6806 | if (!trans) | |
dad3bdee | 6807 | goto err_trans; |
d62d0ba9 FFM |
6808 | |
6809 | newobj = nft_obj_init(ctx, type, attr); | |
6810 | if (IS_ERR(newobj)) { | |
6811 | err = PTR_ERR(newobj); | |
b74ae961 | 6812 | goto err_free_trans; |
d62d0ba9 FFM |
6813 | } |
6814 | ||
6815 | nft_trans_obj(trans) = obj; | |
6816 | nft_trans_obj_update(trans) = true; | |
6817 | nft_trans_obj_newobj(trans) = newobj; | |
0854db2a | 6818 | nft_trans_commit_list_add_tail(ctx->net, trans); |
d62d0ba9 FFM |
6819 | |
6820 | return 0; | |
b74ae961 DC |
6821 | |
6822 | err_free_trans: | |
d62d0ba9 | 6823 | kfree(trans); |
dad3bdee FW |
6824 | err_trans: |
6825 | module_put(type->owner); | |
d62d0ba9 FFM |
6826 | return err; |
6827 | } | |
6828 | ||
7dab8ee3 PNA |
6829 | static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info, |
6830 | const struct nlattr * const nla[]) | |
e5009240 | 6831 | { |
7dab8ee3 PNA |
6832 | struct netlink_ext_ack *extack = info->extack; |
6833 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 6834 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 6835 | const struct nft_object_type *type; |
7dab8ee3 | 6836 | struct net *net = info->net; |
e5009240 PNA |
6837 | struct nft_table *table; |
6838 | struct nft_object *obj; | |
6839 | struct nft_ctx ctx; | |
6840 | u32 objtype; | |
6841 | int err; | |
6842 | ||
6843 | if (!nla[NFTA_OBJ_TYPE] || | |
6844 | !nla[NFTA_OBJ_NAME] || | |
6845 | !nla[NFTA_OBJ_DATA]) | |
6846 | return -EINVAL; | |
6847 | ||
6001a930 PNA |
6848 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
6849 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
6850 | if (IS_ERR(table)) { |
6851 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 6852 | return PTR_ERR(table); |
36dd1bcc | 6853 | } |
e5009240 PNA |
6854 | |
6855 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 6856 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
6857 | if (IS_ERR(obj)) { |
6858 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
6859 | if (err != -ENOENT) { |
6860 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 6861 | return err; |
36dd1bcc | 6862 | } |
1a28ad74 | 6863 | } else { |
7dab8ee3 | 6864 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 6865 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); |
e5009240 | 6866 | return -EEXIST; |
36dd1bcc | 6867 | } |
7dab8ee3 | 6868 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
d62d0ba9 FFM |
6869 | return -EOPNOTSUPP; |
6870 | ||
fd57d0cb | 6871 | type = __nft_obj_type_get(objtype); |
7dab8ee3 | 6872 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
d62d0ba9 FFM |
6873 | |
6874 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
6875 | } |
6876 | ||
7dab8ee3 | 6877 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 | 6878 | |
452238e8 | 6879 | type = nft_obj_type_get(net, objtype); |
e5009240 PNA |
6880 | if (IS_ERR(type)) |
6881 | return PTR_ERR(type); | |
6882 | ||
84fba055 | 6883 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
6884 | if (IS_ERR(obj)) { |
6885 | err = PTR_ERR(obj); | |
b131c964 | 6886 | goto err_init; |
e5009240 | 6887 | } |
d152159b | 6888 | obj->key.table = table; |
3ecbfd65 HS |
6889 | obj->handle = nf_tables_alloc_handle(table); |
6890 | ||
33758c89 | 6891 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT); |
d152159b | 6892 | if (!obj->key.name) { |
61509575 | 6893 | err = -ENOMEM; |
b131c964 JGG |
6894 | goto err_strdup; |
6895 | } | |
6896 | ||
6897 | if (nla[NFTA_OBJ_USERDATA]) { | |
85db827a | 6898 | obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL); |
b131c964 JGG |
6899 | if (obj->udata == NULL) |
6900 | goto err_userdata; | |
6901 | ||
85db827a | 6902 | obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]); |
61509575 | 6903 | } |
e5009240 PNA |
6904 | |
6905 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
6906 | if (err < 0) | |
b131c964 | 6907 | goto err_trans; |
e5009240 | 6908 | |
4d44175a FW |
6909 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
6910 | nft_objname_ht_params); | |
6911 | if (err < 0) | |
b131c964 | 6912 | goto err_obj_ht; |
4d44175a | 6913 | |
e5009240 PNA |
6914 | list_add_tail_rcu(&obj->list, &table->objects); |
6915 | table->use++; | |
6916 | return 0; | |
b131c964 | 6917 | err_obj_ht: |
4d44175a FW |
6918 | /* queued in transaction log */ |
6919 | INIT_LIST_HEAD(&obj->list); | |
6920 | return err; | |
b131c964 | 6921 | err_trans: |
b131c964 | 6922 | kfree(obj->udata); |
85dfd816 PNA |
6923 | err_userdata: |
6924 | kfree(obj->key.name); | |
b131c964 | 6925 | err_strdup: |
dfc46034 | 6926 | if (obj->ops->destroy) |
00bfb320 | 6927 | obj->ops->destroy(&ctx, obj); |
e5009240 | 6928 | kfree(obj); |
b131c964 | 6929 | err_init: |
e5009240 PNA |
6930 | module_put(type->owner); |
6931 | return err; | |
6932 | } | |
6933 | ||
6934 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
6935 | u32 portid, u32 seq, int event, u32 flags, | |
6936 | int family, const struct nft_table *table, | |
43da04a5 | 6937 | struct nft_object *obj, bool reset) |
e5009240 | 6938 | { |
e5009240 PNA |
6939 | struct nlmsghdr *nlh; |
6940 | ||
dedb67c4 | 6941 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
6942 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
6943 | NFNETLINK_V0, nft_base_seq(net)); | |
6944 | if (!nlh) | |
e5009240 PNA |
6945 | goto nla_put_failure; |
6946 | ||
e5009240 | 6947 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || |
d152159b | 6948 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
dfc46034 | 6949 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || |
e5009240 | 6950 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || |
3ecbfd65 HS |
6951 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) || |
6952 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), | |
6953 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
6954 | goto nla_put_failure; |
6955 | ||
b131c964 JGG |
6956 | if (obj->udata && |
6957 | nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata)) | |
6958 | goto nla_put_failure; | |
6959 | ||
e5009240 PNA |
6960 | nlmsg_end(skb, nlh); |
6961 | return 0; | |
6962 | ||
6963 | nla_put_failure: | |
6964 | nlmsg_trim(skb, nlh); | |
6965 | return -1; | |
6966 | } | |
6967 | ||
a9fea2a3 | 6968 | struct nft_obj_filter { |
e46abbcc | 6969 | char *table; |
a9fea2a3 PNA |
6970 | u32 type; |
6971 | }; | |
6972 | ||
e5009240 PNA |
6973 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
6974 | { | |
6975 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 6976 | const struct nft_table *table; |
e5009240 | 6977 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 6978 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
6979 | struct net *net = sock_net(skb->sk); |
6980 | int family = nfmsg->nfgen_family; | |
0854db2a | 6981 | struct nftables_pernet *nft_net; |
43da04a5 PNA |
6982 | struct nft_object *obj; |
6983 | bool reset = false; | |
6984 | ||
6985 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
6986 | reset = true; | |
e5009240 PNA |
6987 | |
6988 | rcu_read_lock(); | |
d59d2f82 | 6989 | nft_net = nft_pernet(net); |
34002783 | 6990 | cb->seq = READ_ONCE(nft_net->base_seq); |
e5009240 | 6991 | |
0854db2a | 6992 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 6993 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
6994 | continue; |
6995 | ||
36596dad PNA |
6996 | list_for_each_entry_rcu(obj, &table->objects, list) { |
6997 | if (!nft_is_active(net, obj)) | |
6998 | goto cont; | |
6999 | if (idx < s_idx) | |
7000 | goto cont; | |
7001 | if (idx > s_idx) | |
7002 | memset(&cb->args[1], 0, | |
7003 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7004 | if (filter && filter->table && |
36596dad PNA |
7005 | strcmp(filter->table, table->name)) |
7006 | goto cont; | |
7007 | if (filter && | |
7008 | filter->type != NFT_OBJECT_UNSPEC && | |
7009 | obj->ops->type->type != filter->type) | |
7010 | goto cont; | |
8e6cf365 | 7011 | if (reset) { |
68df2ed5 | 7012 | char *buf = kasprintf(GFP_ATOMIC, |
c520292f | 7013 | "%s:%u", |
8e6cf365 | 7014 | table->name, |
0854db2a | 7015 | nft_net->base_seq); |
8e6cf365 RGB |
7016 | |
7017 | audit_log_nfcfg(buf, | |
7018 | family, | |
7019 | obj->handle, | |
14224039 | 7020 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7021 | GFP_ATOMIC); |
8e6cf365 RGB |
7022 | kfree(buf); |
7023 | } | |
7024 | ||
36596dad PNA |
7025 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
7026 | cb->nlh->nlmsg_seq, | |
7027 | NFT_MSG_NEWOBJ, | |
7028 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 7029 | table->family, table, |
36596dad PNA |
7030 | obj, reset) < 0) |
7031 | goto done; | |
e5009240 | 7032 | |
36596dad | 7033 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 7034 | cont: |
36596dad | 7035 | idx++; |
e5009240 PNA |
7036 | } |
7037 | } | |
7038 | done: | |
7039 | rcu_read_unlock(); | |
7040 | ||
7041 | cb->args[0] = idx; | |
7042 | return skb->len; | |
7043 | } | |
7044 | ||
90fd131a | 7045 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 7046 | { |
90fd131a FW |
7047 | const struct nlattr * const *nla = cb->data; |
7048 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 7049 | |
90fd131a FW |
7050 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
7051 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7052 | if (!filter) | |
7053 | return -ENOMEM; | |
7054 | ||
7055 | if (nla[NFTA_OBJ_TABLE]) { | |
7056 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
7057 | if (!filter->table) { | |
7058 | kfree(filter); | |
7059 | return -ENOMEM; | |
7060 | } | |
7061 | } | |
7062 | ||
7063 | if (nla[NFTA_OBJ_TYPE]) | |
7064 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 7065 | } |
a9fea2a3 | 7066 | |
90fd131a | 7067 | cb->data = filter; |
a9fea2a3 PNA |
7068 | return 0; |
7069 | } | |
7070 | ||
90fd131a | 7071 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 7072 | { |
90fd131a | 7073 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 7074 | |
90fd131a FW |
7075 | if (filter) { |
7076 | kfree(filter->table); | |
7077 | kfree(filter); | |
e46abbcc | 7078 | } |
a9fea2a3 | 7079 | |
90fd131a | 7080 | return 0; |
a9fea2a3 PNA |
7081 | } |
7082 | ||
d9adf22a | 7083 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7084 | static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info, |
7085 | const struct nlattr * const nla[]) | |
e5009240 | 7086 | { |
797d4980 PNA |
7087 | struct netlink_ext_ack *extack = info->extack; |
7088 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 7089 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7090 | const struct nft_table *table; |
797d4980 | 7091 | struct net *net = info->net; |
e5009240 PNA |
7092 | struct nft_object *obj; |
7093 | struct sk_buff *skb2; | |
43da04a5 | 7094 | bool reset = false; |
e5009240 PNA |
7095 | u32 objtype; |
7096 | int err; | |
7097 | ||
797d4980 | 7098 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
e5009240 | 7099 | struct netlink_dump_control c = { |
90fd131a | 7100 | .start = nf_tables_dump_obj_start, |
e5009240 | 7101 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 7102 | .done = nf_tables_dump_obj_done, |
d9adf22a | 7103 | .module = THIS_MODULE, |
90fd131a | 7104 | .data = (void *)nla, |
e5009240 | 7105 | }; |
a9fea2a3 | 7106 | |
797d4980 | 7107 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
e5009240 PNA |
7108 | } |
7109 | ||
7110 | if (!nla[NFTA_OBJ_NAME] || | |
7111 | !nla[NFTA_OBJ_TYPE]) | |
7112 | return -EINVAL; | |
7113 | ||
6001a930 | 7114 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0); |
36dd1bcc PNA |
7115 | if (IS_ERR(table)) { |
7116 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7117 | return PTR_ERR(table); |
36dd1bcc | 7118 | } |
e5009240 PNA |
7119 | |
7120 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7121 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
7122 | if (IS_ERR(obj)) { |
7123 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7124 | return PTR_ERR(obj); |
36dd1bcc | 7125 | } |
e5009240 | 7126 | |
d9adf22a | 7127 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
7128 | if (!skb2) |
7129 | return -ENOMEM; | |
7130 | ||
797d4980 | 7131 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
43da04a5 PNA |
7132 | reset = true; |
7133 | ||
8e6cf365 | 7134 | if (reset) { |
0854db2a FW |
7135 | const struct nftables_pernet *nft_net; |
7136 | char *buf; | |
7137 | ||
d59d2f82 | 7138 | nft_net = nft_pernet(net); |
0854db2a | 7139 | buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq); |
8e6cf365 RGB |
7140 | |
7141 | audit_log_nfcfg(buf, | |
7142 | family, | |
7143 | obj->handle, | |
14224039 | 7144 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7145 | GFP_ATOMIC); |
8e6cf365 RGB |
7146 | kfree(buf); |
7147 | } | |
7148 | ||
e5009240 | 7149 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 7150 | info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, |
43da04a5 | 7151 | family, table, obj, reset); |
e5009240 | 7152 | if (err < 0) |
ee921183 | 7153 | goto err_fill_obj_info; |
e5009240 | 7154 | |
ee921183 PNA |
7155 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
7156 | ||
7157 | err_fill_obj_info: | |
e5009240 PNA |
7158 | kfree_skb(skb2); |
7159 | return err; | |
e5009240 PNA |
7160 | } |
7161 | ||
00bfb320 | 7162 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 7163 | { |
dfc46034 | 7164 | if (obj->ops->destroy) |
00bfb320 | 7165 | obj->ops->destroy(ctx, obj); |
e5009240 | 7166 | |
dfc46034 | 7167 | module_put(obj->ops->type->owner); |
d152159b | 7168 | kfree(obj->key.name); |
bc7a7082 | 7169 | kfree(obj->udata); |
e5009240 PNA |
7170 | kfree(obj); |
7171 | } | |
7172 | ||
7dab8ee3 PNA |
7173 | static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info, |
7174 | const struct nlattr * const nla[]) | |
e5009240 | 7175 | { |
7dab8ee3 PNA |
7176 | struct netlink_ext_ack *extack = info->extack; |
7177 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7178 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 7179 | struct net *net = info->net; |
36dd1bcc | 7180 | const struct nlattr *attr; |
e5009240 PNA |
7181 | struct nft_table *table; |
7182 | struct nft_object *obj; | |
7183 | struct nft_ctx ctx; | |
7184 | u32 objtype; | |
7185 | ||
7186 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 7187 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
7188 | return -EINVAL; |
7189 | ||
6001a930 PNA |
7190 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7191 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7192 | if (IS_ERR(table)) { |
7193 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7194 | return PTR_ERR(table); |
36dd1bcc | 7195 | } |
e5009240 PNA |
7196 | |
7197 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
7198 | if (nla[NFTA_OBJ_HANDLE]) { |
7199 | attr = nla[NFTA_OBJ_HANDLE]; | |
7200 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
7201 | } else { | |
7202 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 7203 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
7204 | } |
7205 | ||
7206 | if (IS_ERR(obj)) { | |
7207 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7208 | return PTR_ERR(obj); |
36dd1bcc PNA |
7209 | } |
7210 | if (obj->use > 0) { | |
7211 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7212 | return -EBUSY; |
36dd1bcc | 7213 | } |
e5009240 | 7214 | |
7dab8ee3 | 7215 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 PNA |
7216 | |
7217 | return nft_delobj(&ctx, obj); | |
7218 | } | |
7219 | ||
d152159b | 7220 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 | 7221 | struct nft_object *obj, u32 portid, u32 seq, int event, |
6fb721cf | 7222 | u16 flags, int family, int report, gfp_t gfp) |
e5009240 | 7223 | { |
d59d2f82 | 7224 | struct nftables_pernet *nft_net = nft_pernet(net); |
e5009240 PNA |
7225 | struct sk_buff *skb; |
7226 | int err; | |
c520292f | 7227 | char *buf = kasprintf(gfp, "%s:%u", |
0854db2a | 7228 | table->name, nft_net->base_seq); |
8e6cf365 RGB |
7229 | |
7230 | audit_log_nfcfg(buf, | |
7231 | family, | |
7232 | obj->handle, | |
7233 | event == NFT_MSG_NEWOBJ ? | |
c520292f RGB |
7234 | AUDIT_NFT_OP_OBJ_REGISTER : |
7235 | AUDIT_NFT_OP_OBJ_UNREGISTER, | |
68df2ed5 | 7236 | gfp); |
8e6cf365 | 7237 | kfree(buf); |
e5009240 | 7238 | |
2599e989 PNA |
7239 | if (!report && |
7240 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 7241 | return; |
e5009240 | 7242 | |
2599e989 | 7243 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
7244 | if (skb == NULL) |
7245 | goto err; | |
7246 | ||
6fb721cf PNA |
7247 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, |
7248 | flags & (NLM_F_CREATE | NLM_F_EXCL), | |
7249 | family, table, obj, false); | |
e5009240 PNA |
7250 | if (err < 0) { |
7251 | kfree_skb(skb); | |
7252 | goto err; | |
7253 | } | |
7254 | ||
0854db2a | 7255 | nft_notify_enqueue(skb, report, &nft_net->notify_list); |
25e94a99 | 7256 | return; |
e5009240 | 7257 | err: |
25e94a99 | 7258 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 7259 | } |
2599e989 PNA |
7260 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
7261 | ||
25e94a99 PNA |
7262 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
7263 | struct nft_object *obj, int event) | |
2599e989 | 7264 | { |
25e94a99 | 7265 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
6fb721cf | 7266 | ctx->flags, ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 7267 | } |
e5009240 | 7268 | |
3b49e2e9 PNA |
7269 | /* |
7270 | * Flow tables | |
7271 | */ | |
7272 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
7273 | { | |
7274 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7275 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
7276 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7277 | } | |
7278 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
7279 | ||
7280 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
7281 | { | |
7282 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7283 | list_del_rcu(&type->list); | |
7284 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7285 | } | |
7286 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
7287 | ||
7288 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
7289 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
7290 | .len = NFT_NAME_MAXLEN - 1 }, | |
7291 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
7292 | .len = NFT_NAME_MAXLEN - 1 }, | |
7293 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7294 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 7295 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
7296 | }; |
7297 | ||
cac20fcd PNA |
7298 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
7299 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
7300 | { |
7301 | struct nft_flowtable *flowtable; | |
7302 | ||
d9adf22a | 7303 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
7304 | if (!nla_strcmp(nla, flowtable->name) && |
7305 | nft_active_genmask(flowtable, genmask)) | |
7306 | return flowtable; | |
7307 | } | |
7308 | return ERR_PTR(-ENOENT); | |
7309 | } | |
cac20fcd | 7310 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 7311 | |
9b05b6e1 LGL |
7312 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
7313 | struct nft_flowtable *flowtable, | |
7314 | enum nft_trans_phase phase) | |
7315 | { | |
7316 | switch (phase) { | |
7317 | case NFT_TRANS_PREPARE: | |
7318 | case NFT_TRANS_ABORT: | |
7319 | case NFT_TRANS_RELEASE: | |
7320 | flowtable->use--; | |
954d8297 | 7321 | fallthrough; |
9b05b6e1 LGL |
7322 | default: |
7323 | return; | |
7324 | } | |
7325 | } | |
7326 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
7327 | ||
ae0662f8 | 7328 | static struct nft_flowtable * |
cac20fcd PNA |
7329 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
7330 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
7331 | { |
7332 | struct nft_flowtable *flowtable; | |
7333 | ||
7334 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
7335 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
7336 | nft_active_genmask(flowtable, genmask)) | |
7337 | return flowtable; | |
7338 | } | |
7339 | return ERR_PTR(-ENOENT); | |
7340 | } | |
7341 | ||
d9246a53 PNA |
7342 | struct nft_flowtable_hook { |
7343 | u32 num; | |
7344 | int priority; | |
7345 | struct list_head list; | |
7346 | }; | |
7347 | ||
3b49e2e9 PNA |
7348 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
7349 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
7350 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
7351 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
7352 | }; | |
7353 | ||
d9246a53 PNA |
7354 | static int nft_flowtable_parse_hook(const struct nft_ctx *ctx, |
7355 | const struct nlattr *attr, | |
7356 | struct nft_flowtable_hook *flowtable_hook, | |
5b6743fb | 7357 | struct nft_flowtable *flowtable, bool add) |
3b49e2e9 | 7358 | { |
3b49e2e9 | 7359 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 7360 | struct nft_hook *hook; |
3b49e2e9 | 7361 | int hooknum, priority; |
3f0465a9 | 7362 | int err; |
3b49e2e9 | 7363 | |
d9246a53 PNA |
7364 | INIT_LIST_HEAD(&flowtable_hook->list); |
7365 | ||
8cb08174 JB |
7366 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr, |
7367 | nft_flowtable_hook_policy, NULL); | |
3b49e2e9 PNA |
7368 | if (err < 0) |
7369 | return err; | |
7370 | ||
5b6743fb PNA |
7371 | if (add) { |
7372 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
7373 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) | |
7374 | return -EINVAL; | |
3b49e2e9 | 7375 | |
5b6743fb PNA |
7376 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); |
7377 | if (hooknum != NF_NETDEV_INGRESS) | |
7378 | return -EOPNOTSUPP; | |
7379 | ||
7380 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7381 | ||
7382 | flowtable_hook->priority = priority; | |
7383 | flowtable_hook->num = hooknum; | |
7384 | } else { | |
7385 | if (tb[NFTA_FLOWTABLE_HOOK_NUM]) { | |
7386 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
7387 | if (hooknum != flowtable->hooknum) | |
7388 | return -EOPNOTSUPP; | |
7389 | } | |
7390 | ||
7391 | if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { | |
7392 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7393 | if (priority != flowtable->data.priority) | |
7394 | return -EOPNOTSUPP; | |
7395 | } | |
3b49e2e9 | 7396 | |
5b6743fb PNA |
7397 | flowtable_hook->priority = flowtable->data.priority; |
7398 | flowtable_hook->num = flowtable->hooknum; | |
7399 | } | |
3b49e2e9 | 7400 | |
05abe445 PNA |
7401 | if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) { |
7402 | err = nf_tables_parse_netdev_hooks(ctx->net, | |
7403 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
7404 | &flowtable_hook->list); | |
7405 | if (err < 0) | |
7406 | return err; | |
7407 | } | |
3b49e2e9 | 7408 | |
d9246a53 | 7409 | list_for_each_entry(hook, &flowtable_hook->list, list) { |
3f0465a9 | 7410 | hook->ops.pf = NFPROTO_NETDEV; |
5b6743fb PNA |
7411 | hook->ops.hooknum = flowtable_hook->num; |
7412 | hook->ops.priority = flowtable_hook->priority; | |
7413 | hook->ops.priv = &flowtable->data; | |
7414 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
7415 | } |
7416 | ||
3b49e2e9 PNA |
7417 | return err; |
7418 | } | |
7419 | ||
98319cb9 | 7420 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
7421 | { |
7422 | const struct nf_flowtable_type *type; | |
7423 | ||
7424 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 7425 | if (family == type->family) |
3b49e2e9 PNA |
7426 | return type; |
7427 | } | |
7428 | return NULL; | |
7429 | } | |
7430 | ||
452238e8 FW |
7431 | static const struct nf_flowtable_type * |
7432 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
7433 | { |
7434 | const struct nf_flowtable_type *type; | |
7435 | ||
98319cb9 | 7436 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
7437 | if (type != NULL && try_module_get(type->owner)) |
7438 | return type; | |
7439 | ||
f102d66b | 7440 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
7441 | #ifdef CONFIG_MODULES |
7442 | if (type == NULL) { | |
eb014de4 | 7443 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
7444 | return ERR_PTR(-EAGAIN); |
7445 | } | |
7446 | #endif | |
7447 | return ERR_PTR(-ENOENT); | |
7448 | } | |
7449 | ||
5acab914 | 7450 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
7451 | static void nft_unregister_flowtable_hook(struct net *net, |
7452 | struct nft_flowtable *flowtable, | |
7453 | struct nft_hook *hook) | |
7454 | { | |
7455 | nf_unregister_net_hook(net, &hook->ops); | |
7456 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
7457 | FLOW_BLOCK_UNBIND); | |
7458 | } | |
7459 | ||
f9a43007 PNA |
7460 | static void __nft_unregister_flowtable_net_hooks(struct net *net, |
7461 | struct list_head *hook_list, | |
7462 | bool release_netdev) | |
3b49e2e9 | 7463 | { |
f9a43007 | 7464 | struct nft_hook *hook, *next; |
3b49e2e9 | 7465 | |
f9a43007 | 7466 | list_for_each_entry_safe(hook, next, hook_list, list) { |
5acab914 | 7467 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
7468 | if (release_netdev) { |
7469 | list_del(&hook->list); | |
ab5e5c06 | 7470 | kfree_rcu(hook, rcu); |
f9a43007 PNA |
7471 | } |
7472 | } | |
7473 | } | |
7474 | ||
7475 | static void nft_unregister_flowtable_net_hooks(struct net *net, | |
7476 | struct list_head *hook_list) | |
7477 | { | |
7478 | __nft_unregister_flowtable_net_hooks(net, hook_list, false); | |
3f0465a9 PNA |
7479 | } |
7480 | ||
7481 | static int nft_register_flowtable_net_hooks(struct net *net, | |
7482 | struct nft_table *table, | |
f9382669 | 7483 | struct list_head *hook_list, |
3f0465a9 PNA |
7484 | struct nft_flowtable *flowtable) |
7485 | { | |
7486 | struct nft_hook *hook, *hook2, *next; | |
7487 | struct nft_flowtable *ft; | |
7488 | int err, i = 0; | |
7489 | ||
f9382669 | 7490 | list_for_each_entry(hook, hook_list, list) { |
3f0465a9 | 7491 | list_for_each_entry(ft, &table->flowtables, list) { |
86fe2c19 PNA |
7492 | if (!nft_is_active_next(net, ft)) |
7493 | continue; | |
7494 | ||
3f0465a9 PNA |
7495 | list_for_each_entry(hook2, &ft->hook_list, list) { |
7496 | if (hook->ops.dev == hook2->ops.dev && | |
7497 | hook->ops.pf == hook2->ops.pf) { | |
77a92189 | 7498 | err = -EEXIST; |
3f0465a9 PNA |
7499 | goto err_unregister_net_hooks; |
7500 | } | |
7501 | } | |
7502 | } | |
3b49e2e9 | 7503 | |
d7c03a9f | 7504 | err = flowtable->data.type->setup(&flowtable->data, |
7505 | hook->ops.dev, | |
7506 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
7507 | if (err < 0) |
7508 | goto err_unregister_net_hooks; | |
7509 | ||
d7c03a9f | 7510 | err = nf_register_net_hook(net, &hook->ops); |
7511 | if (err < 0) { | |
7512 | flowtable->data.type->setup(&flowtable->data, | |
7513 | hook->ops.dev, | |
7514 | FLOW_BLOCK_UNBIND); | |
7515 | goto err_unregister_net_hooks; | |
7516 | } | |
7517 | ||
3f0465a9 | 7518 | i++; |
3b49e2e9 | 7519 | } |
3f0465a9 PNA |
7520 | |
7521 | return 0; | |
7522 | ||
7523 | err_unregister_net_hooks: | |
f9382669 | 7524 | list_for_each_entry_safe(hook, next, hook_list, list) { |
3f0465a9 PNA |
7525 | if (i-- <= 0) |
7526 | break; | |
7527 | ||
ff4bf2f4 | 7528 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7529 | list_del_rcu(&hook->list); |
7530 | kfree_rcu(hook, rcu); | |
7531 | } | |
7532 | ||
7533 | return err; | |
3b49e2e9 PNA |
7534 | } |
7535 | ||
389a2cbc PNA |
7536 | static void nft_flowtable_hooks_destroy(struct list_head *hook_list) |
7537 | { | |
7538 | struct nft_hook *hook, *next; | |
7539 | ||
7540 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
7541 | list_del_rcu(&hook->list); | |
7542 | kfree_rcu(hook, rcu); | |
7543 | } | |
7544 | } | |
7545 | ||
78d9f48f PNA |
7546 | static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, |
7547 | struct nft_flowtable *flowtable) | |
7548 | { | |
7549 | const struct nlattr * const *nla = ctx->nla; | |
7550 | struct nft_flowtable_hook flowtable_hook; | |
7551 | struct nft_hook *hook, *next; | |
7552 | struct nft_trans *trans; | |
7553 | bool unregister = false; | |
7b35582c | 7554 | u32 flags; |
78d9f48f PNA |
7555 | int err; |
7556 | ||
7557 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7558 | &flowtable_hook, flowtable, false); |
78d9f48f PNA |
7559 | if (err < 0) |
7560 | return err; | |
7561 | ||
7562 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7563 | if (nft_hook_list_find(&flowtable->hook_list, hook)) { | |
7564 | list_del(&hook->list); | |
7565 | kfree(hook); | |
7566 | } | |
7567 | } | |
7568 | ||
7b35582c PNA |
7569 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7570 | flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
c271cc9f PNA |
7571 | if (flags & ~NFT_FLOWTABLE_MASK) { |
7572 | err = -EOPNOTSUPP; | |
7573 | goto err_flowtable_update_hook; | |
7574 | } | |
7b35582c | 7575 | if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^ |
c271cc9f PNA |
7576 | (flags & NFT_FLOWTABLE_HW_OFFLOAD)) { |
7577 | err = -EOPNOTSUPP; | |
7578 | goto err_flowtable_update_hook; | |
7579 | } | |
7b35582c PNA |
7580 | } else { |
7581 | flags = flowtable->data.flags; | |
7582 | } | |
7583 | ||
78d9f48f PNA |
7584 | err = nft_register_flowtable_net_hooks(ctx->net, ctx->table, |
7585 | &flowtable_hook.list, flowtable); | |
7586 | if (err < 0) | |
7587 | goto err_flowtable_update_hook; | |
7588 | ||
7589 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE, | |
7590 | sizeof(struct nft_trans_flowtable)); | |
7591 | if (!trans) { | |
7592 | unregister = true; | |
7593 | err = -ENOMEM; | |
7594 | goto err_flowtable_update_hook; | |
7595 | } | |
7596 | ||
7b35582c | 7597 | nft_trans_flowtable_flags(trans) = flags; |
78d9f48f PNA |
7598 | nft_trans_flowtable(trans) = flowtable; |
7599 | nft_trans_flowtable_update(trans) = true; | |
7600 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
7601 | list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans)); | |
7602 | ||
0854db2a | 7603 | nft_trans_commit_list_add_tail(ctx->net, trans); |
78d9f48f PNA |
7604 | |
7605 | return 0; | |
7606 | ||
7607 | err_flowtable_update_hook: | |
7608 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7609 | if (unregister) | |
7610 | nft_unregister_flowtable_hook(ctx->net, flowtable, hook); | |
7611 | list_del_rcu(&hook->list); | |
7612 | kfree_rcu(hook, rcu); | |
7613 | } | |
7614 | ||
7615 | return err; | |
7616 | ||
7617 | } | |
7618 | ||
7dab8ee3 PNA |
7619 | static int nf_tables_newflowtable(struct sk_buff *skb, |
7620 | const struct nfnl_info *info, | |
7621 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7622 | { |
7dab8ee3 | 7623 | struct netlink_ext_ack *extack = info->extack; |
d9246a53 | 7624 | struct nft_flowtable_hook flowtable_hook; |
7dab8ee3 | 7625 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 7626 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7627 | const struct nf_flowtable_type *type; |
3f0465a9 PNA |
7628 | struct nft_flowtable *flowtable; |
7629 | struct nft_hook *hook, *next; | |
7dab8ee3 | 7630 | struct net *net = info->net; |
3b49e2e9 PNA |
7631 | struct nft_table *table; |
7632 | struct nft_ctx ctx; | |
3f0465a9 | 7633 | int err; |
3b49e2e9 PNA |
7634 | |
7635 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
7636 | !nla[NFTA_FLOWTABLE_NAME] || | |
7637 | !nla[NFTA_FLOWTABLE_HOOK]) | |
7638 | return -EINVAL; | |
7639 | ||
cac20fcd | 7640 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7641 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7642 | if (IS_ERR(table)) { |
7643 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7644 | return PTR_ERR(table); |
36dd1bcc | 7645 | } |
3b49e2e9 | 7646 | |
cac20fcd PNA |
7647 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
7648 | genmask); | |
3b49e2e9 PNA |
7649 | if (IS_ERR(flowtable)) { |
7650 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
7651 | if (err != -ENOENT) { |
7652 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 7653 | return err; |
36dd1bcc | 7654 | } |
3b49e2e9 | 7655 | } else { |
7dab8ee3 | 7656 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 7657 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); |
3b49e2e9 | 7658 | return -EEXIST; |
36dd1bcc | 7659 | } |
3b49e2e9 | 7660 | |
7dab8ee3 | 7661 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
78d9f48f | 7662 | |
7dab8ee3 | 7663 | return nft_flowtable_update(&ctx, info->nlh, flowtable); |
3b49e2e9 PNA |
7664 | } |
7665 | ||
7dab8ee3 | 7666 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
3b49e2e9 | 7667 | |
33758c89 | 7668 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
7669 | if (!flowtable) |
7670 | return -ENOMEM; | |
7671 | ||
7672 | flowtable->table = table; | |
3ecbfd65 | 7673 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 7674 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 7675 | |
33758c89 | 7676 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
7677 | if (!flowtable->name) { |
7678 | err = -ENOMEM; | |
7679 | goto err1; | |
7680 | } | |
7681 | ||
452238e8 | 7682 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
7683 | if (IS_ERR(type)) { |
7684 | err = PTR_ERR(type); | |
7685 | goto err2; | |
7686 | } | |
7687 | ||
8bb69f3b PNA |
7688 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7689 | flowtable->data.flags = | |
7690 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
7e6136f1 PNA |
7691 | if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) { |
7692 | err = -EOPNOTSUPP; | |
8bb69f3b | 7693 | goto err3; |
7e6136f1 | 7694 | } |
8bb69f3b PNA |
7695 | } |
7696 | ||
7697 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 7698 | flowtable->data.type = type; |
a268de77 | 7699 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
7700 | if (err < 0) |
7701 | goto err3; | |
7702 | ||
d9246a53 | 7703 | err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK], |
5b6743fb | 7704 | &flowtable_hook, flowtable, true); |
3b49e2e9 | 7705 | if (err < 0) |
a268de77 | 7706 | goto err4; |
3b49e2e9 | 7707 | |
d9246a53 PNA |
7708 | list_splice(&flowtable_hook.list, &flowtable->hook_list); |
7709 | flowtable->data.priority = flowtable_hook.priority; | |
7710 | flowtable->hooknum = flowtable_hook.num; | |
7711 | ||
f9382669 PNA |
7712 | err = nft_register_flowtable_net_hooks(ctx.net, table, |
7713 | &flowtable->hook_list, | |
7714 | flowtable); | |
2d285f26 | 7715 | if (err < 0) { |
389a2cbc | 7716 | nft_flowtable_hooks_destroy(&flowtable->hook_list); |
3f0465a9 | 7717 | goto err4; |
2d285f26 | 7718 | } |
3b49e2e9 PNA |
7719 | |
7720 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
7721 | if (err < 0) | |
3f0465a9 | 7722 | goto err5; |
3b49e2e9 PNA |
7723 | |
7724 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
7725 | table->use++; | |
7726 | ||
7727 | return 0; | |
a268de77 | 7728 | err5: |
3f0465a9 | 7729 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 7730 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7731 | list_del_rcu(&hook->list); |
7732 | kfree_rcu(hook, rcu); | |
7733 | } | |
a268de77 FF |
7734 | err4: |
7735 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
7736 | err3: |
7737 | module_put(type->owner); | |
7738 | err2: | |
7739 | kfree(flowtable->name); | |
7740 | err1: | |
7741 | kfree(flowtable); | |
7742 | return err; | |
7743 | } | |
7744 | ||
3003055f PNA |
7745 | static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook) |
7746 | { | |
7747 | struct nft_hook *this, *next; | |
7748 | ||
7749 | list_for_each_entry_safe(this, next, &flowtable_hook->list, list) { | |
7750 | list_del(&this->list); | |
7751 | kfree(this); | |
7752 | } | |
7753 | } | |
7754 | ||
abadb2f8 PNA |
7755 | static int nft_delflowtable_hook(struct nft_ctx *ctx, |
7756 | struct nft_flowtable *flowtable) | |
7757 | { | |
7758 | const struct nlattr * const *nla = ctx->nla; | |
7759 | struct nft_flowtable_hook flowtable_hook; | |
b6d9014a | 7760 | LIST_HEAD(flowtable_del_list); |
3003055f | 7761 | struct nft_hook *this, *hook; |
abadb2f8 PNA |
7762 | struct nft_trans *trans; |
7763 | int err; | |
7764 | ||
7765 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7766 | &flowtable_hook, flowtable, false); |
abadb2f8 PNA |
7767 | if (err < 0) |
7768 | return err; | |
7769 | ||
3003055f | 7770 | list_for_each_entry(this, &flowtable_hook.list, list) { |
abadb2f8 PNA |
7771 | hook = nft_hook_list_find(&flowtable->hook_list, this); |
7772 | if (!hook) { | |
7773 | err = -ENOENT; | |
7774 | goto err_flowtable_del_hook; | |
7775 | } | |
b6d9014a | 7776 | list_move(&hook->list, &flowtable_del_list); |
abadb2f8 PNA |
7777 | } |
7778 | ||
7779 | trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE, | |
7780 | sizeof(struct nft_trans_flowtable)); | |
3003055f PNA |
7781 | if (!trans) { |
7782 | err = -ENOMEM; | |
7783 | goto err_flowtable_del_hook; | |
7784 | } | |
abadb2f8 PNA |
7785 | |
7786 | nft_trans_flowtable(trans) = flowtable; | |
7787 | nft_trans_flowtable_update(trans) = true; | |
7788 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
b6d9014a | 7789 | list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans)); |
3003055f | 7790 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 | 7791 | |
0854db2a | 7792 | nft_trans_commit_list_add_tail(ctx->net, trans); |
abadb2f8 PNA |
7793 | |
7794 | return 0; | |
7795 | ||
7796 | err_flowtable_del_hook: | |
b6d9014a | 7797 | list_splice(&flowtable_del_list, &flowtable->hook_list); |
3003055f | 7798 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 PNA |
7799 | |
7800 | return err; | |
7801 | } | |
7802 | ||
7dab8ee3 PNA |
7803 | static int nf_tables_delflowtable(struct sk_buff *skb, |
7804 | const struct nfnl_info *info, | |
7805 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7806 | { |
7dab8ee3 PNA |
7807 | struct netlink_ext_ack *extack = info->extack; |
7808 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7809 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7810 | struct nft_flowtable *flowtable; |
7dab8ee3 | 7811 | struct net *net = info->net; |
36dd1bcc | 7812 | const struct nlattr *attr; |
3b49e2e9 PNA |
7813 | struct nft_table *table; |
7814 | struct nft_ctx ctx; | |
7815 | ||
e603ea4b PNA |
7816 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
7817 | (!nla[NFTA_FLOWTABLE_NAME] && | |
7818 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
7819 | return -EINVAL; | |
7820 | ||
cac20fcd | 7821 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7822 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7823 | if (IS_ERR(table)) { |
7824 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7825 | return PTR_ERR(table); |
36dd1bcc | 7826 | } |
3b49e2e9 | 7827 | |
36dd1bcc PNA |
7828 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
7829 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
7830 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
7831 | } else { | |
7832 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
7833 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
7834 | } | |
7835 | ||
7836 | if (IS_ERR(flowtable)) { | |
7837 | NL_SET_BAD_ATTR(extack, attr); | |
7838 | return PTR_ERR(flowtable); | |
7839 | } | |
abadb2f8 | 7840 | |
7dab8ee3 | 7841 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
abadb2f8 PNA |
7842 | |
7843 | if (nla[NFTA_FLOWTABLE_HOOK]) | |
7844 | return nft_delflowtable_hook(&ctx, flowtable); | |
7845 | ||
36dd1bcc PNA |
7846 | if (flowtable->use > 0) { |
7847 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 7848 | return -EBUSY; |
36dd1bcc | 7849 | } |
3b49e2e9 | 7850 | |
3b49e2e9 PNA |
7851 | return nft_delflowtable(&ctx, flowtable); |
7852 | } | |
7853 | ||
7854 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
7855 | u32 portid, u32 seq, int event, | |
7856 | u32 flags, int family, | |
c42d8bda PNA |
7857 | struct nft_flowtable *flowtable, |
7858 | struct list_head *hook_list) | |
3b49e2e9 PNA |
7859 | { |
7860 | struct nlattr *nest, *nest_devs; | |
3f0465a9 | 7861 | struct nft_hook *hook; |
3b49e2e9 | 7862 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
7863 | |
7864 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
19c28b13 PNA |
7865 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
7866 | NFNETLINK_V0, nft_base_seq(net)); | |
7867 | if (!nlh) | |
3b49e2e9 PNA |
7868 | goto nla_put_failure; |
7869 | ||
3b49e2e9 PNA |
7870 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || |
7871 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 HS |
7872 | nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || |
7873 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), | |
8bb69f3b PNA |
7874 | NFTA_FLOWTABLE_PAD) || |
7875 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) | |
3b49e2e9 PNA |
7876 | goto nla_put_failure; |
7877 | ||
ae0be8de | 7878 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
7879 | if (!nest) |
7880 | goto nla_put_failure; | |
3b49e2e9 | 7881 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 7882 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
7883 | goto nla_put_failure; |
7884 | ||
ae0be8de | 7885 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
7886 | if (!nest_devs) |
7887 | goto nla_put_failure; | |
7888 | ||
c42d8bda | 7889 | list_for_each_entry_rcu(hook, hook_list, list) { |
3f0465a9 | 7890 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) |
3b49e2e9 PNA |
7891 | goto nla_put_failure; |
7892 | } | |
7893 | nla_nest_end(skb, nest_devs); | |
7894 | nla_nest_end(skb, nest); | |
7895 | ||
7896 | nlmsg_end(skb, nlh); | |
7897 | return 0; | |
7898 | ||
7899 | nla_put_failure: | |
7900 | nlmsg_trim(skb, nlh); | |
7901 | return -1; | |
7902 | } | |
7903 | ||
7904 | struct nft_flowtable_filter { | |
7905 | char *table; | |
7906 | }; | |
7907 | ||
7908 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
7909 | struct netlink_callback *cb) | |
7910 | { | |
7911 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
7912 | struct nft_flowtable_filter *filter = cb->data; | |
7913 | unsigned int idx = 0, s_idx = cb->args[0]; | |
7914 | struct net *net = sock_net(skb->sk); | |
7915 | int family = nfmsg->nfgen_family; | |
7916 | struct nft_flowtable *flowtable; | |
0854db2a | 7917 | struct nftables_pernet *nft_net; |
3b49e2e9 PNA |
7918 | const struct nft_table *table; |
7919 | ||
7920 | rcu_read_lock(); | |
d59d2f82 | 7921 | nft_net = nft_pernet(net); |
34002783 | 7922 | cb->seq = READ_ONCE(nft_net->base_seq); |
3b49e2e9 | 7923 | |
0854db2a | 7924 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 7925 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
7926 | continue; |
7927 | ||
36596dad PNA |
7928 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
7929 | if (!nft_is_active(net, flowtable)) | |
7930 | goto cont; | |
7931 | if (idx < s_idx) | |
7932 | goto cont; | |
7933 | if (idx > s_idx) | |
7934 | memset(&cb->args[1], 0, | |
7935 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7936 | if (filter && filter->table && |
36596dad PNA |
7937 | strcmp(filter->table, table->name)) |
7938 | goto cont; | |
3b49e2e9 | 7939 | |
36596dad PNA |
7940 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
7941 | cb->nlh->nlmsg_seq, | |
7942 | NFT_MSG_NEWFLOWTABLE, | |
7943 | NLM_F_MULTI | NLM_F_APPEND, | |
c42d8bda PNA |
7944 | table->family, |
7945 | flowtable, | |
7946 | &flowtable->hook_list) < 0) | |
36596dad | 7947 | goto done; |
3b49e2e9 | 7948 | |
36596dad | 7949 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 7950 | cont: |
36596dad | 7951 | idx++; |
3b49e2e9 PNA |
7952 | } |
7953 | } | |
7954 | done: | |
7955 | rcu_read_unlock(); | |
7956 | ||
7957 | cb->args[0] = idx; | |
7958 | return skb->len; | |
7959 | } | |
7960 | ||
90fd131a | 7961 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 7962 | { |
90fd131a FW |
7963 | const struct nlattr * const *nla = cb->data; |
7964 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 7965 | |
90fd131a FW |
7966 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
7967 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7968 | if (!filter) | |
7969 | return -ENOMEM; | |
3b49e2e9 | 7970 | |
90fd131a FW |
7971 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
7972 | GFP_ATOMIC); | |
7973 | if (!filter->table) { | |
7974 | kfree(filter); | |
7975 | return -ENOMEM; | |
7976 | } | |
7977 | } | |
3b49e2e9 | 7978 | |
90fd131a | 7979 | cb->data = filter; |
3b49e2e9 PNA |
7980 | return 0; |
7981 | } | |
7982 | ||
90fd131a | 7983 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 7984 | { |
90fd131a | 7985 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 7986 | |
3b49e2e9 | 7987 | if (!filter) |
90fd131a | 7988 | return 0; |
3b49e2e9 | 7989 | |
90fd131a FW |
7990 | kfree(filter->table); |
7991 | kfree(filter); | |
7992 | ||
7993 | return 0; | |
3b49e2e9 PNA |
7994 | } |
7995 | ||
d9adf22a | 7996 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7997 | static int nf_tables_getflowtable(struct sk_buff *skb, |
7998 | const struct nfnl_info *info, | |
7999 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8000 | { |
797d4980 | 8001 | u8 genmask = nft_genmask_cur(info->net); |
ef4b65e5 | 8002 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8003 | struct nft_flowtable *flowtable; |
3b49e2e9 | 8004 | const struct nft_table *table; |
797d4980 | 8005 | struct net *net = info->net; |
3b49e2e9 PNA |
8006 | struct sk_buff *skb2; |
8007 | int err; | |
8008 | ||
797d4980 | 8009 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
3b49e2e9 | 8010 | struct netlink_dump_control c = { |
90fd131a | 8011 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
8012 | .dump = nf_tables_dump_flowtable, |
8013 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 8014 | .module = THIS_MODULE, |
90fd131a | 8015 | .data = (void *)nla, |
3b49e2e9 PNA |
8016 | }; |
8017 | ||
797d4980 | 8018 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
3b49e2e9 PNA |
8019 | } |
8020 | ||
8021 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
8022 | return -EINVAL; | |
8023 | ||
cac20fcd | 8024 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8025 | genmask, 0); |
3b49e2e9 PNA |
8026 | if (IS_ERR(table)) |
8027 | return PTR_ERR(table); | |
8028 | ||
cac20fcd PNA |
8029 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
8030 | genmask); | |
03a0120f | 8031 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
8032 | return PTR_ERR(flowtable); |
8033 | ||
d9adf22a | 8034 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
8035 | if (!skb2) |
8036 | return -ENOMEM; | |
8037 | ||
8038 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
797d4980 | 8039 | info->nlh->nlmsg_seq, |
3b49e2e9 | 8040 | NFT_MSG_NEWFLOWTABLE, 0, family, |
c42d8bda | 8041 | flowtable, &flowtable->hook_list); |
3b49e2e9 | 8042 | if (err < 0) |
ee921183 | 8043 | goto err_fill_flowtable_info; |
3b49e2e9 | 8044 | |
ee921183 PNA |
8045 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
8046 | ||
8047 | err_fill_flowtable_info: | |
3b49e2e9 PNA |
8048 | kfree_skb(skb2); |
8049 | return err; | |
8050 | } | |
8051 | ||
8052 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
8053 | struct nft_flowtable *flowtable, | |
c42d8bda | 8054 | struct list_head *hook_list, |
3b49e2e9 PNA |
8055 | int event) |
8056 | { | |
d59d2f82 | 8057 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
3b49e2e9 | 8058 | struct sk_buff *skb; |
6fb721cf | 8059 | u16 flags = 0; |
3b49e2e9 PNA |
8060 | int err; |
8061 | ||
dceababa | 8062 | if (!ctx->report && |
3b49e2e9 PNA |
8063 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
8064 | return; | |
8065 | ||
8066 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
8067 | if (skb == NULL) | |
8068 | goto err; | |
8069 | ||
6fb721cf PNA |
8070 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
8071 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
8072 | ||
3b49e2e9 | 8073 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, |
6fb721cf | 8074 | ctx->seq, event, flags, |
c42d8bda | 8075 | ctx->family, flowtable, hook_list); |
3b49e2e9 PNA |
8076 | if (err < 0) { |
8077 | kfree_skb(skb); | |
8078 | goto err; | |
8079 | } | |
8080 | ||
0854db2a | 8081 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
3b49e2e9 PNA |
8082 | return; |
8083 | err: | |
8084 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
8085 | } | |
8086 | ||
3b49e2e9 PNA |
8087 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
8088 | { | |
3f0465a9 PNA |
8089 | struct nft_hook *hook, *next; |
8090 | ||
5acab914 | 8091 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 8092 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
8093 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
8094 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
8095 | list_del_rcu(&hook->list); |
8096 | kfree(hook); | |
8097 | } | |
3b49e2e9 | 8098 | kfree(flowtable->name); |
3b49e2e9 | 8099 | module_put(flowtable->data.type->owner); |
a12486eb | 8100 | kfree(flowtable); |
3b49e2e9 PNA |
8101 | } |
8102 | ||
84d7fce6 PNA |
8103 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
8104 | u32 portid, u32 seq) | |
8105 | { | |
d59d2f82 | 8106 | struct nftables_pernet *nft_net = nft_pernet(net); |
84d7fce6 | 8107 | struct nlmsghdr *nlh; |
784b4e61 | 8108 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 8109 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 | 8110 | |
19c28b13 PNA |
8111 | nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC, |
8112 | NFNETLINK_V0, nft_base_seq(net)); | |
8113 | if (!nlh) | |
84d7fce6 PNA |
8114 | goto nla_put_failure; |
8115 | ||
0854db2a | 8116 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) || |
784b4e61 PS |
8117 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || |
8118 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
8119 | goto nla_put_failure; |
8120 | ||
053c095a JB |
8121 | nlmsg_end(skb, nlh); |
8122 | return 0; | |
84d7fce6 PNA |
8123 | |
8124 | nla_put_failure: | |
8125 | nlmsg_trim(skb, nlh); | |
8126 | return -EMSGSIZE; | |
8127 | } | |
8128 | ||
3b49e2e9 PNA |
8129 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
8130 | struct nft_flowtable *flowtable) | |
8131 | { | |
3f0465a9 | 8132 | struct nft_hook *hook; |
3b49e2e9 | 8133 | |
3f0465a9 PNA |
8134 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
8135 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
8136 | continue; |
8137 | ||
5acab914 | 8138 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 8139 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
8140 | list_del_rcu(&hook->list); |
8141 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
8142 | break; |
8143 | } | |
8144 | } | |
8145 | ||
8146 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
8147 | unsigned long event, void *ptr) | |
8148 | { | |
8149 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
8150 | struct nft_flowtable *flowtable; | |
0854db2a | 8151 | struct nftables_pernet *nft_net; |
3b49e2e9 | 8152 | struct nft_table *table; |
0a2cf5ee | 8153 | struct net *net; |
3b49e2e9 PNA |
8154 | |
8155 | if (event != NETDEV_UNREGISTER) | |
8156 | return 0; | |
8157 | ||
6a48de01 | 8158 | net = dev_net(dev); |
d59d2f82 | 8159 | nft_net = nft_pernet(net); |
0854db2a FW |
8160 | mutex_lock(&nft_net->commit_mutex); |
8161 | list_for_each_entry(table, &nft_net->tables, list) { | |
36596dad PNA |
8162 | list_for_each_entry(flowtable, &table->flowtables, list) { |
8163 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
8164 | } |
8165 | } | |
0854db2a | 8166 | mutex_unlock(&nft_net->commit_mutex); |
6a48de01 | 8167 | |
3b49e2e9 PNA |
8168 | return NOTIFY_DONE; |
8169 | } | |
8170 | ||
8171 | static struct notifier_block nf_tables_flowtable_notifier = { | |
8172 | .notifier_call = nf_tables_flowtable_event, | |
8173 | }; | |
8174 | ||
25e94a99 PNA |
8175 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
8176 | int event) | |
84d7fce6 PNA |
8177 | { |
8178 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
8179 | struct sk_buff *skb2; | |
8180 | int err; | |
8181 | ||
dceababa | 8182 | if (!nlmsg_report(nlh) && |
84d7fce6 | 8183 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 8184 | return; |
84d7fce6 | 8185 | |
84d7fce6 PNA |
8186 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
8187 | if (skb2 == NULL) | |
8188 | goto err; | |
8189 | ||
8190 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
8191 | nlh->nlmsg_seq); | |
8192 | if (err < 0) { | |
8193 | kfree_skb(skb2); | |
8194 | goto err; | |
8195 | } | |
8196 | ||
25e94a99 PNA |
8197 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8198 | nlmsg_report(nlh), GFP_KERNEL); | |
8199 | return; | |
84d7fce6 | 8200 | err: |
25e94a99 PNA |
8201 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8202 | -ENOBUFS); | |
84d7fce6 PNA |
8203 | } |
8204 | ||
797d4980 PNA |
8205 | static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info, |
8206 | const struct nlattr * const nla[]) | |
84d7fce6 | 8207 | { |
84d7fce6 PNA |
8208 | struct sk_buff *skb2; |
8209 | int err; | |
8210 | ||
d9adf22a | 8211 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
8212 | if (skb2 == NULL) |
8213 | return -ENOMEM; | |
8214 | ||
797d4980 PNA |
8215 | err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid, |
8216 | info->nlh->nlmsg_seq); | |
84d7fce6 | 8217 | if (err < 0) |
ee921183 | 8218 | goto err_fill_gen_info; |
84d7fce6 | 8219 | |
797d4980 | 8220 | return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); |
ee921183 PNA |
8221 | |
8222 | err_fill_gen_info: | |
84d7fce6 PNA |
8223 | kfree_skb(skb2); |
8224 | return err; | |
8225 | } | |
8226 | ||
96518518 PM |
8227 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
8228 | [NFT_MSG_NEWTABLE] = { | |
50f2db9e PNA |
8229 | .call = nf_tables_newtable, |
8230 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8231 | .attr_count = NFTA_TABLE_MAX, |
8232 | .policy = nft_table_policy, | |
8233 | }, | |
8234 | [NFT_MSG_GETTABLE] = { | |
50f2db9e PNA |
8235 | .call = nf_tables_gettable, |
8236 | .type = NFNL_CB_RCU, | |
96518518 PM |
8237 | .attr_count = NFTA_TABLE_MAX, |
8238 | .policy = nft_table_policy, | |
8239 | }, | |
8240 | [NFT_MSG_DELTABLE] = { | |
50f2db9e PNA |
8241 | .call = nf_tables_deltable, |
8242 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8243 | .attr_count = NFTA_TABLE_MAX, |
8244 | .policy = nft_table_policy, | |
8245 | }, | |
8246 | [NFT_MSG_NEWCHAIN] = { | |
50f2db9e PNA |
8247 | .call = nf_tables_newchain, |
8248 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8249 | .attr_count = NFTA_CHAIN_MAX, |
8250 | .policy = nft_chain_policy, | |
8251 | }, | |
8252 | [NFT_MSG_GETCHAIN] = { | |
50f2db9e PNA |
8253 | .call = nf_tables_getchain, |
8254 | .type = NFNL_CB_RCU, | |
96518518 PM |
8255 | .attr_count = NFTA_CHAIN_MAX, |
8256 | .policy = nft_chain_policy, | |
8257 | }, | |
8258 | [NFT_MSG_DELCHAIN] = { | |
50f2db9e PNA |
8259 | .call = nf_tables_delchain, |
8260 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8261 | .attr_count = NFTA_CHAIN_MAX, |
8262 | .policy = nft_chain_policy, | |
8263 | }, | |
8264 | [NFT_MSG_NEWRULE] = { | |
50f2db9e PNA |
8265 | .call = nf_tables_newrule, |
8266 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8267 | .attr_count = NFTA_RULE_MAX, |
8268 | .policy = nft_rule_policy, | |
8269 | }, | |
8270 | [NFT_MSG_GETRULE] = { | |
50f2db9e PNA |
8271 | .call = nf_tables_getrule, |
8272 | .type = NFNL_CB_RCU, | |
96518518 PM |
8273 | .attr_count = NFTA_RULE_MAX, |
8274 | .policy = nft_rule_policy, | |
8275 | }, | |
8276 | [NFT_MSG_DELRULE] = { | |
50f2db9e PNA |
8277 | .call = nf_tables_delrule, |
8278 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8279 | .attr_count = NFTA_RULE_MAX, |
8280 | .policy = nft_rule_policy, | |
8281 | }, | |
20a69341 | 8282 | [NFT_MSG_NEWSET] = { |
50f2db9e PNA |
8283 | .call = nf_tables_newset, |
8284 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8285 | .attr_count = NFTA_SET_MAX, |
8286 | .policy = nft_set_policy, | |
8287 | }, | |
8288 | [NFT_MSG_GETSET] = { | |
50f2db9e PNA |
8289 | .call = nf_tables_getset, |
8290 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8291 | .attr_count = NFTA_SET_MAX, |
8292 | .policy = nft_set_policy, | |
8293 | }, | |
8294 | [NFT_MSG_DELSET] = { | |
50f2db9e PNA |
8295 | .call = nf_tables_delset, |
8296 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8297 | .attr_count = NFTA_SET_MAX, |
8298 | .policy = nft_set_policy, | |
8299 | }, | |
8300 | [NFT_MSG_NEWSETELEM] = { | |
50f2db9e PNA |
8301 | .call = nf_tables_newsetelem, |
8302 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8303 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8304 | .policy = nft_set_elem_list_policy, | |
8305 | }, | |
8306 | [NFT_MSG_GETSETELEM] = { | |
50f2db9e PNA |
8307 | .call = nf_tables_getsetelem, |
8308 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8309 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8310 | .policy = nft_set_elem_list_policy, | |
8311 | }, | |
8312 | [NFT_MSG_DELSETELEM] = { | |
50f2db9e PNA |
8313 | .call = nf_tables_delsetelem, |
8314 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8315 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8316 | .policy = nft_set_elem_list_policy, | |
8317 | }, | |
84d7fce6 | 8318 | [NFT_MSG_GETGEN] = { |
50f2db9e PNA |
8319 | .call = nf_tables_getgen, |
8320 | .type = NFNL_CB_RCU, | |
84d7fce6 | 8321 | }, |
e5009240 | 8322 | [NFT_MSG_NEWOBJ] = { |
50f2db9e PNA |
8323 | .call = nf_tables_newobj, |
8324 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8325 | .attr_count = NFTA_OBJ_MAX, |
8326 | .policy = nft_obj_policy, | |
8327 | }, | |
8328 | [NFT_MSG_GETOBJ] = { | |
50f2db9e PNA |
8329 | .call = nf_tables_getobj, |
8330 | .type = NFNL_CB_RCU, | |
e5009240 PNA |
8331 | .attr_count = NFTA_OBJ_MAX, |
8332 | .policy = nft_obj_policy, | |
8333 | }, | |
8334 | [NFT_MSG_DELOBJ] = { | |
50f2db9e PNA |
8335 | .call = nf_tables_delobj, |
8336 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8337 | .attr_count = NFTA_OBJ_MAX, |
8338 | .policy = nft_obj_policy, | |
8339 | }, | |
43da04a5 | 8340 | [NFT_MSG_GETOBJ_RESET] = { |
50f2db9e PNA |
8341 | .call = nf_tables_getobj, |
8342 | .type = NFNL_CB_RCU, | |
43da04a5 PNA |
8343 | .attr_count = NFTA_OBJ_MAX, |
8344 | .policy = nft_obj_policy, | |
8345 | }, | |
3b49e2e9 | 8346 | [NFT_MSG_NEWFLOWTABLE] = { |
50f2db9e PNA |
8347 | .call = nf_tables_newflowtable, |
8348 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8349 | .attr_count = NFTA_FLOWTABLE_MAX, |
8350 | .policy = nft_flowtable_policy, | |
8351 | }, | |
8352 | [NFT_MSG_GETFLOWTABLE] = { | |
50f2db9e PNA |
8353 | .call = nf_tables_getflowtable, |
8354 | .type = NFNL_CB_RCU, | |
3b49e2e9 PNA |
8355 | .attr_count = NFTA_FLOWTABLE_MAX, |
8356 | .policy = nft_flowtable_policy, | |
8357 | }, | |
8358 | [NFT_MSG_DELFLOWTABLE] = { | |
50f2db9e PNA |
8359 | .call = nf_tables_delflowtable, |
8360 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8361 | .attr_count = NFTA_FLOWTABLE_MAX, |
8362 | .policy = nft_flowtable_policy, | |
8363 | }, | |
96518518 PM |
8364 | }; |
8365 | ||
a654de8f PNA |
8366 | static int nf_tables_validate(struct net *net) |
8367 | { | |
d59d2f82 | 8368 | struct nftables_pernet *nft_net = nft_pernet(net); |
a654de8f PNA |
8369 | struct nft_table *table; |
8370 | ||
0854db2a | 8371 | switch (nft_net->validate_state) { |
a654de8f PNA |
8372 | case NFT_VALIDATE_SKIP: |
8373 | break; | |
8374 | case NFT_VALIDATE_NEED: | |
8375 | nft_validate_state_update(net, NFT_VALIDATE_DO); | |
954d8297 | 8376 | fallthrough; |
a654de8f | 8377 | case NFT_VALIDATE_DO: |
0854db2a | 8378 | list_for_each_entry(table, &nft_net->tables, list) { |
a654de8f PNA |
8379 | if (nft_table_validate(net, table) < 0) |
8380 | return -EAGAIN; | |
8381 | } | |
8382 | break; | |
8383 | } | |
8384 | ||
8385 | return 0; | |
8386 | } | |
8387 | ||
66293c46 FW |
8388 | /* a drop policy has to be deferred until all rules have been activated, |
8389 | * otherwise a large ruleset that contains a drop-policy base chain will | |
8390 | * cause all packets to get dropped until the full transaction has been | |
8391 | * processed. | |
8392 | * | |
8393 | * We defer the drop policy until the transaction has been finalized. | |
8394 | */ | |
8395 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
8396 | { | |
8397 | struct nft_base_chain *basechain; | |
8398 | ||
8399 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
8400 | return; | |
8401 | ||
8402 | if (!nft_is_base_chain(trans->ctx.chain)) | |
8403 | return; | |
8404 | ||
8405 | basechain = nft_base_chain(trans->ctx.chain); | |
8406 | basechain->policy = NF_DROP; | |
8407 | } | |
8408 | ||
91c7b38d PNA |
8409 | static void nft_chain_commit_update(struct nft_trans *trans) |
8410 | { | |
8411 | struct nft_base_chain *basechain; | |
8412 | ||
1b2470e5 FW |
8413 | if (nft_trans_chain_name(trans)) { |
8414 | rhltable_remove(&trans->ctx.table->chains_ht, | |
8415 | &trans->ctx.chain->rhlhead, | |
8416 | nft_chain_ht_params); | |
d71efb59 | 8417 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
8418 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
8419 | trans->ctx.chain->name, | |
8420 | &trans->ctx.chain->rhlhead, | |
8421 | nft_chain_ht_params); | |
8422 | } | |
91c7b38d | 8423 | |
f323d954 | 8424 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
8425 | return; |
8426 | ||
53315ac6 FW |
8427 | nft_chain_stats_replace(trans); |
8428 | ||
91c7b38d | 8429 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
8430 | |
8431 | switch (nft_trans_chain_policy(trans)) { | |
8432 | case NF_DROP: | |
8433 | case NF_ACCEPT: | |
8434 | basechain->policy = nft_trans_chain_policy(trans); | |
8435 | break; | |
8436 | } | |
8437 | } | |
8438 | ||
d62d0ba9 FFM |
8439 | static void nft_obj_commit_update(struct nft_trans *trans) |
8440 | { | |
8441 | struct nft_object *newobj; | |
8442 | struct nft_object *obj; | |
8443 | ||
8444 | obj = nft_trans_obj(trans); | |
8445 | newobj = nft_trans_obj_newobj(trans); | |
8446 | ||
9fedd894 FFM |
8447 | if (obj->ops->update) |
8448 | obj->ops->update(obj, newobj); | |
d62d0ba9 | 8449 | |
dad3bdee | 8450 | nft_obj_destroy(&trans->ctx, newobj); |
d62d0ba9 FFM |
8451 | } |
8452 | ||
2f99aa31 | 8453 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 8454 | { |
c7c32e72 PNA |
8455 | switch (trans->msg_type) { |
8456 | case NFT_MSG_DELTABLE: | |
8457 | nf_tables_table_destroy(&trans->ctx); | |
8458 | break; | |
9f8aac0b | 8459 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 8460 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
8461 | kfree(nft_trans_chain_name(trans)); |
8462 | break; | |
c7c32e72 | 8463 | case NFT_MSG_DELCHAIN: |
43a605f2 | 8464 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
8465 | break; |
8466 | case NFT_MSG_DELRULE: | |
9dd732e0 PNA |
8467 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
8468 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
8469 | ||
c7c32e72 PNA |
8470 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); |
8471 | break; | |
8472 | case NFT_MSG_DELSET: | |
0c2a85ed | 8473 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 8474 | break; |
61edafbb | 8475 | case NFT_MSG_DELSETELEM: |
3453c927 PNA |
8476 | nf_tables_set_elem_destroy(&trans->ctx, |
8477 | nft_trans_elem_set(trans), | |
59105446 | 8478 | nft_trans_elem(trans).priv); |
61edafbb | 8479 | break; |
e5009240 | 8480 | case NFT_MSG_DELOBJ: |
00bfb320 | 8481 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 8482 | break; |
3b49e2e9 | 8483 | case NFT_MSG_DELFLOWTABLE: |
abadb2f8 PNA |
8484 | if (nft_trans_flowtable_update(trans)) |
8485 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
8486 | else | |
8487 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 8488 | break; |
c7c32e72 | 8489 | } |
0935d558 FW |
8490 | |
8491 | if (trans->put_net) | |
8492 | put_net(trans->ctx.net); | |
8493 | ||
c7c32e72 PNA |
8494 | kfree(trans); |
8495 | } | |
8496 | ||
0935d558 | 8497 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
8498 | { |
8499 | struct nft_trans *trans, *next; | |
0935d558 FW |
8500 | LIST_HEAD(head); |
8501 | ||
8502 | spin_lock(&nf_tables_destroy_list_lock); | |
8503 | list_splice_init(&nf_tables_destroy_list, &head); | |
8504 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 8505 | |
0935d558 | 8506 | if (list_empty(&head)) |
2f99aa31 FW |
8507 | return; |
8508 | ||
8509 | synchronize_rcu(); | |
8510 | ||
0935d558 | 8511 | list_for_each_entry_safe(trans, next, &head, list) { |
2f99aa31 FW |
8512 | list_del(&trans->list); |
8513 | nft_commit_release(trans); | |
8514 | } | |
8515 | } | |
8516 | ||
ffe8923f FW |
8517 | void nf_tables_trans_destroy_flush_work(void) |
8518 | { | |
8519 | flush_work(&trans_destroy_work); | |
8520 | } | |
8521 | EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work); | |
8522 | ||
ed5f85d4 PNA |
8523 | static bool nft_expr_reduce(struct nft_regs_track *track, |
8524 | const struct nft_expr *expr) | |
8525 | { | |
9e539c5b | 8526 | return false; |
ed5f85d4 PNA |
8527 | } |
8528 | ||
0cbc06b3 FW |
8529 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
8530 | { | |
2c865a8a | 8531 | const struct nft_expr *expr, *last; |
12e4ecfa | 8532 | struct nft_regs_track track = {}; |
2c865a8a PNA |
8533 | unsigned int size, data_size; |
8534 | void *data, *data_boundary; | |
8535 | struct nft_rule_dp *prule; | |
0cbc06b3 | 8536 | struct nft_rule *rule; |
0cbc06b3 FW |
8537 | |
8538 | /* already handled or inactive chain? */ | |
2c865a8a | 8539 | if (chain->blob_next || !nft_is_active_next(net, chain)) |
0cbc06b3 FW |
8540 | return 0; |
8541 | ||
63045bfd | 8542 | data_size = 0; |
10377d42 | 8543 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
8544 | if (nft_is_active_next(net, rule)) { |
8545 | data_size += sizeof(*prule) + rule->dlen; | |
8546 | if (data_size > INT_MAX) | |
8547 | return -ENOMEM; | |
8548 | } | |
0cbc06b3 | 8549 | } |
2c865a8a | 8550 | data_size += offsetof(struct nft_rule_dp, data); /* last rule */ |
0cbc06b3 | 8551 | |
2c865a8a PNA |
8552 | chain->blob_next = nf_tables_chain_alloc_rules(data_size); |
8553 | if (!chain->blob_next) | |
0cbc06b3 FW |
8554 | return -ENOMEM; |
8555 | ||
2c865a8a PNA |
8556 | data = (void *)chain->blob_next->data; |
8557 | data_boundary = data + data_size; | |
8558 | size = 0; | |
8559 | ||
10377d42 | 8560 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
8561 | if (!nft_is_active_next(net, rule)) |
8562 | continue; | |
8563 | ||
8564 | prule = (struct nft_rule_dp *)data; | |
8565 | data += offsetof(struct nft_rule_dp, data); | |
8566 | if (WARN_ON_ONCE(data > data_boundary)) | |
8567 | return -ENOMEM; | |
8568 | ||
12e4ecfa | 8569 | size = 0; |
fe75e84a | 8570 | track.last = nft_expr_last(rule); |
2c865a8a | 8571 | nft_rule_for_each_expr(expr, last, rule) { |
12e4ecfa PNA |
8572 | track.cur = expr; |
8573 | ||
ed5f85d4 | 8574 | if (nft_expr_reduce(&track, expr)) { |
12e4ecfa PNA |
8575 | expr = track.cur; |
8576 | continue; | |
8577 | } | |
8578 | ||
2c865a8a PNA |
8579 | if (WARN_ON_ONCE(data + expr->ops->size > data_boundary)) |
8580 | return -ENOMEM; | |
8581 | ||
8582 | memcpy(data + size, expr, expr->ops->size); | |
8583 | size += expr->ops->size; | |
8584 | } | |
8585 | if (WARN_ON_ONCE(size >= 1 << 12)) | |
8586 | return -ENOMEM; | |
8587 | ||
8588 | prule->handle = rule->handle; | |
8589 | prule->dlen = size; | |
8590 | prule->is_last = 0; | |
8591 | ||
8592 | data += size; | |
8593 | size = 0; | |
8594 | chain->blob_next->size += (unsigned long)(data - (void *)prule); | |
0cbc06b3 FW |
8595 | } |
8596 | ||
2c865a8a PNA |
8597 | prule = (struct nft_rule_dp *)data; |
8598 | data += offsetof(struct nft_rule_dp, data); | |
8599 | if (WARN_ON_ONCE(data > data_boundary)) | |
8600 | return -ENOMEM; | |
8601 | ||
8602 | nft_last_rule(chain->blob_next, prule); | |
8603 | ||
0cbc06b3 FW |
8604 | return 0; |
8605 | } | |
8606 | ||
8607 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
8608 | { | |
d59d2f82 | 8609 | struct nftables_pernet *nft_net = nft_pernet(net); |
0cbc06b3 FW |
8610 | struct nft_trans *trans, *next; |
8611 | ||
0854db2a | 8612 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 FW |
8613 | struct nft_chain *chain = trans->ctx.chain; |
8614 | ||
8615 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
8616 | trans->msg_type == NFT_MSG_DELRULE) { | |
2c865a8a PNA |
8617 | kvfree(chain->blob_next); |
8618 | chain->blob_next = NULL; | |
0cbc06b3 FW |
8619 | } |
8620 | } | |
8621 | } | |
8622 | ||
8623 | static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h) | |
8624 | { | |
8625 | struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); | |
8626 | ||
2c865a8a | 8627 | kvfree(o->blob); |
0cbc06b3 FW |
8628 | } |
8629 | ||
2c865a8a | 8630 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob) |
0cbc06b3 | 8631 | { |
0cbc06b3 FW |
8632 | struct nft_rules_old *old; |
8633 | ||
2c865a8a PNA |
8634 | /* rcu_head is after end marker */ |
8635 | old = (void *)blob + sizeof(*blob) + blob->size; | |
8636 | old->blob = blob; | |
0cbc06b3 FW |
8637 | |
8638 | call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old); | |
8639 | } | |
8640 | ||
0fb39bbe | 8641 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 | 8642 | { |
2c865a8a | 8643 | struct nft_rule_blob *g0, *g1; |
0cbc06b3 FW |
8644 | bool next_genbit; |
8645 | ||
8646 | next_genbit = nft_gencursor_next(net); | |
8647 | ||
2c865a8a | 8648 | g0 = rcu_dereference_protected(chain->blob_gen_0, |
f102d66b | 8649 | lockdep_commit_lock_is_held(net)); |
2c865a8a | 8650 | g1 = rcu_dereference_protected(chain->blob_gen_1, |
f102d66b | 8651 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
8652 | |
8653 | /* No changes to this chain? */ | |
2c865a8a | 8654 | if (chain->blob_next == NULL) { |
0cbc06b3 FW |
8655 | /* chain had no change in last or next generation */ |
8656 | if (g0 == g1) | |
8657 | return; | |
8658 | /* | |
8659 | * chain had no change in this generation; make sure next | |
8660 | * one uses same rules as current generation. | |
8661 | */ | |
8662 | if (next_genbit) { | |
2c865a8a | 8663 | rcu_assign_pointer(chain->blob_gen_1, g0); |
0cbc06b3 FW |
8664 | nf_tables_commit_chain_free_rules_old(g1); |
8665 | } else { | |
2c865a8a | 8666 | rcu_assign_pointer(chain->blob_gen_0, g1); |
0cbc06b3 FW |
8667 | nf_tables_commit_chain_free_rules_old(g0); |
8668 | } | |
8669 | ||
8670 | return; | |
8671 | } | |
8672 | ||
8673 | if (next_genbit) | |
2c865a8a | 8674 | rcu_assign_pointer(chain->blob_gen_1, chain->blob_next); |
0cbc06b3 | 8675 | else |
2c865a8a | 8676 | rcu_assign_pointer(chain->blob_gen_0, chain->blob_next); |
0cbc06b3 | 8677 | |
2c865a8a | 8678 | chain->blob_next = NULL; |
0cbc06b3 FW |
8679 | |
8680 | if (g0 == g1) | |
8681 | return; | |
8682 | ||
8683 | if (next_genbit) | |
8684 | nf_tables_commit_chain_free_rules_old(g1); | |
8685 | else | |
8686 | nf_tables_commit_chain_free_rules_old(g0); | |
8687 | } | |
8688 | ||
d152159b FW |
8689 | static void nft_obj_del(struct nft_object *obj) |
8690 | { | |
4d44175a | 8691 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
8692 | list_del_rcu(&obj->list); |
8693 | } | |
8694 | ||
d0e2c7de | 8695 | void nft_chain_del(struct nft_chain *chain) |
1b2470e5 FW |
8696 | { |
8697 | struct nft_table *table = chain->table; | |
8698 | ||
8699 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
8700 | nft_chain_ht_params)); | |
8701 | list_del_rcu(&chain->list); | |
8702 | } | |
8703 | ||
eb014de4 PNA |
8704 | static void nf_tables_module_autoload_cleanup(struct net *net) |
8705 | { | |
d59d2f82 | 8706 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
8707 | struct nft_module_request *req, *next; |
8708 | ||
0854db2a FW |
8709 | WARN_ON_ONCE(!list_empty(&nft_net->commit_list)); |
8710 | list_for_each_entry_safe(req, next, &nft_net->module_list, list) { | |
eb014de4 PNA |
8711 | WARN_ON_ONCE(!req->done); |
8712 | list_del(&req->list); | |
8713 | kfree(req); | |
8714 | } | |
8715 | } | |
8716 | ||
0935d558 FW |
8717 | static void nf_tables_commit_release(struct net *net) |
8718 | { | |
d59d2f82 | 8719 | struct nftables_pernet *nft_net = nft_pernet(net); |
0935d558 FW |
8720 | struct nft_trans *trans; |
8721 | ||
8722 | /* all side effects have to be made visible. | |
8723 | * For example, if a chain named 'foo' has been deleted, a | |
8724 | * new transaction must not find it anymore. | |
8725 | * | |
8726 | * Memory reclaim happens asynchronously from work queue | |
8727 | * to prevent expensive synchronize_rcu() in commit phase. | |
8728 | */ | |
0854db2a | 8729 | if (list_empty(&nft_net->commit_list)) { |
eb014de4 | 8730 | nf_tables_module_autoload_cleanup(net); |
0854db2a | 8731 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
8732 | return; |
8733 | } | |
8734 | ||
0854db2a | 8735 | trans = list_last_entry(&nft_net->commit_list, |
0935d558 FW |
8736 | struct nft_trans, list); |
8737 | get_net(trans->ctx.net); | |
8738 | WARN_ON_ONCE(trans->put_net); | |
8739 | ||
8740 | trans->put_net = true; | |
8741 | spin_lock(&nf_tables_destroy_list_lock); | |
0854db2a | 8742 | list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list); |
0935d558 FW |
8743 | spin_unlock(&nf_tables_destroy_list_lock); |
8744 | ||
eb014de4 | 8745 | nf_tables_module_autoload_cleanup(net); |
0935d558 | 8746 | schedule_work(&trans_destroy_work); |
ffe8923f | 8747 | |
0854db2a | 8748 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
8749 | } |
8750 | ||
67cc570e PNA |
8751 | static void nft_commit_notify(struct net *net, u32 portid) |
8752 | { | |
d59d2f82 | 8753 | struct nftables_pernet *nft_net = nft_pernet(net); |
67cc570e PNA |
8754 | struct sk_buff *batch_skb = NULL, *nskb, *skb; |
8755 | unsigned char *data; | |
8756 | int len; | |
8757 | ||
0854db2a | 8758 | list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) { |
67cc570e PNA |
8759 | if (!batch_skb) { |
8760 | new_batch: | |
8761 | batch_skb = skb; | |
8762 | len = NLMSG_GOODSIZE - skb->len; | |
8763 | list_del(&skb->list); | |
8764 | continue; | |
8765 | } | |
8766 | len -= skb->len; | |
8767 | if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) { | |
8768 | data = skb_put(batch_skb, skb->len); | |
8769 | memcpy(data, skb->data, skb->len); | |
8770 | list_del(&skb->list); | |
8771 | kfree_skb(skb); | |
8772 | continue; | |
8773 | } | |
8774 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
8775 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
8776 | goto new_batch; | |
8777 | } | |
8778 | ||
8779 | if (batch_skb) { | |
8780 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
8781 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
8782 | } | |
8783 | ||
0854db2a | 8784 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); |
67cc570e PNA |
8785 | } |
8786 | ||
c520292f RGB |
8787 | static int nf_tables_commit_audit_alloc(struct list_head *adl, |
8788 | struct nft_table *table) | |
8789 | { | |
8790 | struct nft_audit_data *adp; | |
8791 | ||
8792 | list_for_each_entry(adp, adl, list) { | |
8793 | if (adp->table == table) | |
8794 | return 0; | |
8795 | } | |
8796 | adp = kzalloc(sizeof(*adp), GFP_KERNEL); | |
8797 | if (!adp) | |
8798 | return -ENOMEM; | |
8799 | adp->table = table; | |
8800 | list_add(&adp->list, adl); | |
8801 | return 0; | |
8802 | } | |
8803 | ||
cfbe3650 DM |
8804 | static void nf_tables_commit_audit_free(struct list_head *adl) |
8805 | { | |
8806 | struct nft_audit_data *adp, *adn; | |
8807 | ||
8808 | list_for_each_entry_safe(adp, adn, adl, list) { | |
8809 | list_del(&adp->list); | |
8810 | kfree(adp); | |
8811 | } | |
8812 | } | |
8813 | ||
c520292f RGB |
8814 | static void nf_tables_commit_audit_collect(struct list_head *adl, |
8815 | struct nft_table *table, u32 op) | |
8816 | { | |
8817 | struct nft_audit_data *adp; | |
8818 | ||
8819 | list_for_each_entry(adp, adl, list) { | |
8820 | if (adp->table == table) | |
8821 | goto found; | |
8822 | } | |
dadf33c9 | 8823 | WARN_ONCE(1, "table=%s not expected in commit list", table->name); |
c520292f RGB |
8824 | return; |
8825 | found: | |
8826 | adp->entries++; | |
8827 | if (!adp->op || adp->op > op) | |
8828 | adp->op = op; | |
8829 | } | |
8830 | ||
8831 | #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22) | |
8832 | ||
8833 | static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation) | |
8834 | { | |
8835 | struct nft_audit_data *adp, *adn; | |
8836 | char aubuf[AUNFTABLENAMELEN]; | |
8837 | ||
8838 | list_for_each_entry_safe(adp, adn, adl, list) { | |
8839 | snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name, | |
8840 | generation); | |
8841 | audit_log_nfcfg(aubuf, adp->table->family, adp->entries, | |
8842 | nft2audit_op[adp->op], GFP_KERNEL); | |
8843 | list_del(&adp->list); | |
8844 | kfree(adp); | |
8845 | } | |
8846 | } | |
8847 | ||
5913beaf | 8848 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 8849 | { |
d59d2f82 | 8850 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 8851 | struct nft_trans *trans, *next; |
a3716e70 | 8852 | struct nft_trans_elem *te; |
0cbc06b3 FW |
8853 | struct nft_chain *chain; |
8854 | struct nft_table *table; | |
34002783 | 8855 | unsigned int base_seq; |
c520292f | 8856 | LIST_HEAD(adl); |
c9626a2c | 8857 | int err; |
37082f93 | 8858 | |
0854db2a FW |
8859 | if (list_empty(&nft_net->commit_list)) { |
8860 | mutex_unlock(&nft_net->commit_mutex); | |
b8b27498 FW |
8861 | return 0; |
8862 | } | |
8863 | ||
a654de8f PNA |
8864 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
8865 | if (nf_tables_validate(net) < 0) | |
8866 | return -EAGAIN; | |
8867 | ||
c9626a2c PNA |
8868 | err = nft_flow_rule_offload_commit(net); |
8869 | if (err < 0) | |
8870 | return err; | |
8871 | ||
0cbc06b3 | 8872 | /* 1. Allocate space for next generation rules_gen_X[] */ |
0854db2a | 8873 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 | 8874 | int ret; |
37082f93 | 8875 | |
c520292f RGB |
8876 | ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table); |
8877 | if (ret) { | |
8878 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 8879 | nf_tables_commit_audit_free(&adl); |
c520292f RGB |
8880 | return ret; |
8881 | } | |
0cbc06b3 FW |
8882 | if (trans->msg_type == NFT_MSG_NEWRULE || |
8883 | trans->msg_type == NFT_MSG_DELRULE) { | |
8884 | chain = trans->ctx.chain; | |
8885 | ||
8886 | ret = nf_tables_commit_chain_prepare(net, chain); | |
8887 | if (ret < 0) { | |
8888 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 8889 | nf_tables_commit_audit_free(&adl); |
0cbc06b3 FW |
8890 | return ret; |
8891 | } | |
8892 | } | |
8893 | } | |
37082f93 | 8894 | |
0cbc06b3 | 8895 | /* step 2. Make rules_gen_X visible to packet path */ |
0854db2a | 8896 | list_for_each_entry(table, &nft_net->tables, list) { |
0fb39bbe FW |
8897 | list_for_each_entry(chain, &table->chains, list) |
8898 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
8899 | } |
8900 | ||
8901 | /* | |
8902 | * Bump generation counter, invalidate any dump in progress. | |
8903 | * Cannot fail after this point. | |
37082f93 | 8904 | */ |
34002783 PNA |
8905 | base_seq = READ_ONCE(nft_net->base_seq); |
8906 | while (++base_seq == 0) | |
0854db2a | 8907 | ; |
0cbc06b3 | 8908 | |
34002783 PNA |
8909 | WRITE_ONCE(nft_net->base_seq, base_seq); |
8910 | ||
0cbc06b3 FW |
8911 | /* step 3. Start new generation, rules_gen_X now in use. */ |
8912 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 | 8913 | |
0854db2a | 8914 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
c520292f RGB |
8915 | nf_tables_commit_audit_collect(&adl, trans->ctx.table, |
8916 | trans->msg_type); | |
b380e5c7 | 8917 | switch (trans->msg_type) { |
55dd6f93 PNA |
8918 | case NFT_MSG_NEWTABLE: |
8919 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
8920 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
8921 | nft_trans_destroy(trans); | |
8922 | break; | |
8923 | } | |
8924 | if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT) | |
0ce7cf41 PNA |
8925 | nf_tables_table_disable(net, trans->ctx.table); |
8926 | ||
179d9ba5 | 8927 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; |
55dd6f93 | 8928 | } else { |
f2a6d766 | 8929 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 8930 | } |
35151d84 | 8931 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
8932 | nft_trans_destroy(trans); |
8933 | break; | |
8934 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 8935 | list_del_rcu(&trans->ctx.table->list); |
35151d84 | 8936 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
55dd6f93 | 8937 | break; |
91c7b38d | 8938 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 8939 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 8940 | nft_chain_commit_update(trans); |
9f8aac0b FW |
8941 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
8942 | /* trans destroyed after rcu grace period */ | |
8943 | } else { | |
66293c46 | 8944 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 8945 | nft_clear(net, trans->ctx.chain); |
9f8aac0b FW |
8946 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
8947 | nft_trans_destroy(trans); | |
8948 | } | |
91c7b38d PNA |
8949 | break; |
8950 | case NFT_MSG_DELCHAIN: | |
1b2470e5 | 8951 | nft_chain_del(trans->ctx.chain); |
35151d84 | 8952 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
c974a3a3 PNA |
8953 | nf_tables_unregister_hook(trans->ctx.net, |
8954 | trans->ctx.table, | |
8955 | trans->ctx.chain); | |
91c7b38d | 8956 | break; |
b380e5c7 | 8957 | case NFT_MSG_NEWRULE: |
889f7ee7 | 8958 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 8959 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 8960 | nft_trans_rule(trans), |
35151d84 | 8961 | NFT_MSG_NEWRULE); |
9dd732e0 PNA |
8962 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
8963 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
8964 | ||
37082f93 | 8965 | nft_trans_destroy(trans); |
b380e5c7 PNA |
8966 | break; |
8967 | case NFT_MSG_DELRULE: | |
8968 | list_del_rcu(&nft_trans_rule(trans)->list); | |
35151d84 PNA |
8969 | nf_tables_rule_notify(&trans->ctx, |
8970 | nft_trans_rule(trans), | |
8971 | NFT_MSG_DELRULE); | |
f6ac8585 PNA |
8972 | nft_rule_expr_deactivate(&trans->ctx, |
8973 | nft_trans_rule(trans), | |
8974 | NFT_TRANS_COMMIT); | |
b380e5c7 | 8975 | break; |
958bee14 | 8976 | case NFT_MSG_NEWSET: |
37a9cc52 | 8977 | nft_clear(net, nft_trans_set(trans)); |
4fefee57 PNA |
8978 | /* This avoids hitting -EBUSY when deleting the table |
8979 | * from the transaction. | |
8980 | */ | |
408070d6 | 8981 | if (nft_set_is_anonymous(nft_trans_set(trans)) && |
4fefee57 PNA |
8982 | !list_empty(&nft_trans_set(trans)->bindings)) |
8983 | trans->ctx.table->use--; | |
8984 | ||
958bee14 | 8985 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 8986 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
8987 | nft_trans_destroy(trans); |
8988 | break; | |
8989 | case NFT_MSG_DELSET: | |
37a9cc52 | 8990 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 8991 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 8992 | NFT_MSG_DELSET, GFP_KERNEL); |
958bee14 | 8993 | break; |
60319eb1 | 8994 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
8995 | te = (struct nft_trans_elem *)trans->data; |
8996 | ||
aaa31047 | 8997 | nft_setelem_activate(net, te->set, &te->elem); |
cc02e457 PM |
8998 | nf_tables_setelem_notify(&trans->ctx, te->set, |
8999 | &te->elem, | |
6fb721cf | 9000 | NFT_MSG_NEWSETELEM); |
60319eb1 PNA |
9001 | nft_trans_destroy(trans); |
9002 | break; | |
9003 | case NFT_MSG_DELSETELEM: | |
a3716e70 | 9004 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 9005 | |
a3716e70 PNA |
9006 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9007 | &te->elem, | |
6fb721cf | 9008 | NFT_MSG_DELSETELEM); |
aaa31047 PNA |
9009 | nft_setelem_remove(net, te->set, &te->elem); |
9010 | if (!nft_setelem_is_catchall(te->set, &te->elem)) { | |
9011 | atomic_dec(&te->set->nelems); | |
9012 | te->set->ndeact--; | |
9013 | } | |
60319eb1 | 9014 | break; |
e5009240 | 9015 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
9016 | if (nft_trans_obj_update(trans)) { |
9017 | nft_obj_commit_update(trans); | |
9018 | nf_tables_obj_notify(&trans->ctx, | |
9019 | nft_trans_obj(trans), | |
9020 | NFT_MSG_NEWOBJ); | |
9021 | } else { | |
9022 | nft_clear(net, nft_trans_obj(trans)); | |
9023 | nf_tables_obj_notify(&trans->ctx, | |
9024 | nft_trans_obj(trans), | |
9025 | NFT_MSG_NEWOBJ); | |
9026 | nft_trans_destroy(trans); | |
9027 | } | |
e5009240 PNA |
9028 | break; |
9029 | case NFT_MSG_DELOBJ: | |
d152159b | 9030 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 PNA |
9031 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
9032 | NFT_MSG_DELOBJ); | |
9033 | break; | |
3b49e2e9 | 9034 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 9035 | if (nft_trans_flowtable_update(trans)) { |
7b35582c PNA |
9036 | nft_trans_flowtable(trans)->data.flags = |
9037 | nft_trans_flowtable_flags(trans); | |
78d9f48f PNA |
9038 | nf_tables_flowtable_notify(&trans->ctx, |
9039 | nft_trans_flowtable(trans), | |
9040 | &nft_trans_flowtable_hooks(trans), | |
9041 | NFT_MSG_NEWFLOWTABLE); | |
9042 | list_splice(&nft_trans_flowtable_hooks(trans), | |
9043 | &nft_trans_flowtable(trans)->hook_list); | |
9044 | } else { | |
9045 | nft_clear(net, nft_trans_flowtable(trans)); | |
9046 | nf_tables_flowtable_notify(&trans->ctx, | |
9047 | nft_trans_flowtable(trans), | |
9048 | &nft_trans_flowtable(trans)->hook_list, | |
9049 | NFT_MSG_NEWFLOWTABLE); | |
9050 | } | |
3b49e2e9 PNA |
9051 | nft_trans_destroy(trans); |
9052 | break; | |
9053 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 | 9054 | if (nft_trans_flowtable_update(trans)) { |
abadb2f8 PNA |
9055 | nf_tables_flowtable_notify(&trans->ctx, |
9056 | nft_trans_flowtable(trans), | |
9057 | &nft_trans_flowtable_hooks(trans), | |
9058 | NFT_MSG_DELFLOWTABLE); | |
9059 | nft_unregister_flowtable_net_hooks(net, | |
9060 | &nft_trans_flowtable_hooks(trans)); | |
9061 | } else { | |
9062 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9063 | nf_tables_flowtable_notify(&trans->ctx, | |
9064 | nft_trans_flowtable(trans), | |
9065 | &nft_trans_flowtable(trans)->hook_list, | |
9066 | NFT_MSG_DELFLOWTABLE); | |
9067 | nft_unregister_flowtable_net_hooks(net, | |
9068 | &nft_trans_flowtable(trans)->hook_list); | |
9069 | } | |
3b49e2e9 | 9070 | break; |
37082f93 | 9071 | } |
37082f93 PNA |
9072 | } |
9073 | ||
67cc570e | 9074 | nft_commit_notify(net, NETLINK_CB(skb).portid); |
84d7fce6 | 9075 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0854db2a | 9076 | nf_tables_commit_audit_log(&adl, nft_net->base_seq); |
0935d558 | 9077 | nf_tables_commit_release(net); |
37082f93 PNA |
9078 | |
9079 | return 0; | |
9080 | } | |
9081 | ||
eb014de4 PNA |
9082 | static void nf_tables_module_autoload(struct net *net) |
9083 | { | |
d59d2f82 | 9084 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
9085 | struct nft_module_request *req, *next; |
9086 | LIST_HEAD(module_list); | |
9087 | ||
0854db2a FW |
9088 | list_splice_init(&nft_net->module_list, &module_list); |
9089 | mutex_unlock(&nft_net->commit_mutex); | |
eb014de4 | 9090 | list_for_each_entry_safe(req, next, &module_list, list) { |
1d305ba4 FW |
9091 | request_module("%s", req->module); |
9092 | req->done = true; | |
eb014de4 | 9093 | } |
0854db2a FW |
9094 | mutex_lock(&nft_net->commit_mutex); |
9095 | list_splice(&module_list, &nft_net->module_list); | |
eb014de4 PNA |
9096 | } |
9097 | ||
b326dd37 | 9098 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 9099 | { |
c7c32e72 PNA |
9100 | switch (trans->msg_type) { |
9101 | case NFT_MSG_NEWTABLE: | |
9102 | nf_tables_table_destroy(&trans->ctx); | |
9103 | break; | |
9104 | case NFT_MSG_NEWCHAIN: | |
43a605f2 | 9105 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
9106 | break; |
9107 | case NFT_MSG_NEWRULE: | |
9108 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
9109 | break; | |
9110 | case NFT_MSG_NEWSET: | |
0c2a85ed | 9111 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 9112 | break; |
61edafbb PM |
9113 | case NFT_MSG_NEWSETELEM: |
9114 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 9115 | nft_trans_elem(trans).priv, true); |
61edafbb | 9116 | break; |
e5009240 | 9117 | case NFT_MSG_NEWOBJ: |
00bfb320 | 9118 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 9119 | break; |
3b49e2e9 | 9120 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9121 | if (nft_trans_flowtable_update(trans)) |
9122 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
9123 | else | |
9124 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 9125 | break; |
c7c32e72 PNA |
9126 | } |
9127 | kfree(trans); | |
9128 | } | |
9129 | ||
c0391b6a | 9130 | static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) |
37082f93 | 9131 | { |
d59d2f82 | 9132 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 9133 | struct nft_trans *trans, *next; |
02263db0 | 9134 | struct nft_trans_elem *te; |
37082f93 | 9135 | |
c0391b6a PNA |
9136 | if (action == NFNL_ABORT_VALIDATE && |
9137 | nf_tables_validate(net) < 0) | |
9138 | return -EAGAIN; | |
9139 | ||
0854db2a | 9140 | list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list, |
a907e36d | 9141 | list) { |
b380e5c7 | 9142 | switch (trans->msg_type) { |
55dd6f93 PNA |
9143 | case NFT_MSG_NEWTABLE: |
9144 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
9145 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
9146 | nft_trans_destroy(trans); | |
9147 | break; | |
9148 | } | |
9149 | if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) { | |
0ce7cf41 | 9150 | nf_tables_table_disable(net, trans->ctx.table); |
179d9ba5 PNA |
9151 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
9152 | } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) { | |
9153 | trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT; | |
9154 | } | |
9155 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; | |
55dd6f93 PNA |
9156 | nft_trans_destroy(trans); |
9157 | } else { | |
e688a7f8 | 9158 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
9159 | } |
9160 | break; | |
9161 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 9162 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
9163 | nft_trans_destroy(trans); |
9164 | break; | |
91c7b38d PNA |
9165 | case NFT_MSG_NEWCHAIN: |
9166 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 9167 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 9168 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
9169 | nft_trans_destroy(trans); |
9170 | } else { | |
d0e2c7de PNA |
9171 | if (nft_chain_is_bound(trans->ctx.chain)) { |
9172 | nft_trans_destroy(trans); | |
9173 | break; | |
9174 | } | |
4fefee57 | 9175 | trans->ctx.table->use--; |
1b2470e5 | 9176 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
9177 | nf_tables_unregister_hook(trans->ctx.net, |
9178 | trans->ctx.table, | |
9179 | trans->ctx.chain); | |
91c7b38d PNA |
9180 | } |
9181 | break; | |
9182 | case NFT_MSG_DELCHAIN: | |
4fefee57 | 9183 | trans->ctx.table->use++; |
664b0f8c | 9184 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
9185 | nft_trans_destroy(trans); |
9186 | break; | |
b380e5c7 | 9187 | case NFT_MSG_NEWRULE: |
4fefee57 | 9188 | trans->ctx.chain->use--; |
b380e5c7 | 9189 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
9190 | nft_rule_expr_deactivate(&trans->ctx, |
9191 | nft_trans_rule(trans), | |
9192 | NFT_TRANS_ABORT); | |
3c5e4462 PNA |
9193 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9194 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 PNA |
9195 | break; |
9196 | case NFT_MSG_DELRULE: | |
4fefee57 | 9197 | trans->ctx.chain->use++; |
889f7ee7 | 9198 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 9199 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
3c5e4462 PNA |
9200 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9201 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9202 | ||
37082f93 | 9203 | nft_trans_destroy(trans); |
b380e5c7 | 9204 | break; |
958bee14 | 9205 | case NFT_MSG_NEWSET: |
4fefee57 | 9206 | trans->ctx.table->use--; |
6a0a8d10 | 9207 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
9208 | nft_trans_destroy(trans); |
9209 | break; | |
9210 | } | |
9211 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
9212 | break; |
9213 | case NFT_MSG_DELSET: | |
4fefee57 | 9214 | trans->ctx.table->use++; |
37a9cc52 | 9215 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
9216 | nft_trans_destroy(trans); |
9217 | break; | |
60319eb1 | 9218 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 9219 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
9220 | nft_trans_destroy(trans); |
9221 | break; | |
9222 | } | |
02263db0 | 9223 | te = (struct nft_trans_elem *)trans->data; |
aaa31047 PNA |
9224 | nft_setelem_remove(net, te->set, &te->elem); |
9225 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9226 | atomic_dec(&te->set->nelems); | |
60319eb1 PNA |
9227 | break; |
9228 | case NFT_MSG_DELSETELEM: | |
cc02e457 PM |
9229 | te = (struct nft_trans_elem *)trans->data; |
9230 | ||
f8bb7889 | 9231 | nft_setelem_data_activate(net, te->set, &te->elem); |
aaa31047 PNA |
9232 | nft_setelem_activate(net, te->set, &te->elem); |
9233 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
9234 | te->set->ndeact--; | |
cc02e457 | 9235 | |
e5009240 PNA |
9236 | nft_trans_destroy(trans); |
9237 | break; | |
9238 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 | 9239 | if (nft_trans_obj_update(trans)) { |
dad3bdee | 9240 | nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans)); |
d62d0ba9 FFM |
9241 | nft_trans_destroy(trans); |
9242 | } else { | |
9243 | trans->ctx.table->use--; | |
9244 | nft_obj_del(nft_trans_obj(trans)); | |
9245 | } | |
e5009240 PNA |
9246 | break; |
9247 | case NFT_MSG_DELOBJ: | |
9248 | trans->ctx.table->use++; | |
9249 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
9250 | nft_trans_destroy(trans); |
9251 | break; | |
3b49e2e9 | 9252 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9253 | if (nft_trans_flowtable_update(trans)) { |
9254 | nft_unregister_flowtable_net_hooks(net, | |
9255 | &nft_trans_flowtable_hooks(trans)); | |
9256 | } else { | |
9257 | trans->ctx.table->use--; | |
9258 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9259 | nft_unregister_flowtable_net_hooks(net, | |
9260 | &nft_trans_flowtable(trans)->hook_list); | |
9261 | } | |
3b49e2e9 PNA |
9262 | break; |
9263 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 | 9264 | if (nft_trans_flowtable_update(trans)) { |
b6d9014a PNA |
9265 | list_splice(&nft_trans_flowtable_hooks(trans), |
9266 | &nft_trans_flowtable(trans)->hook_list); | |
abadb2f8 PNA |
9267 | } else { |
9268 | trans->ctx.table->use++; | |
9269 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
9270 | } | |
3b49e2e9 PNA |
9271 | nft_trans_destroy(trans); |
9272 | break; | |
37082f93 | 9273 | } |
37082f93 PNA |
9274 | } |
9275 | ||
b326dd37 PNA |
9276 | synchronize_rcu(); |
9277 | ||
a1cee076 | 9278 | list_for_each_entry_safe_reverse(trans, next, |
0854db2a | 9279 | &nft_net->commit_list, list) { |
c7c32e72 | 9280 | list_del(&trans->list); |
b326dd37 | 9281 | nf_tables_abort_release(trans); |
37082f93 PNA |
9282 | } |
9283 | ||
c0391b6a | 9284 | if (action == NFNL_ABORT_AUTOLOAD) |
eb014de4 PNA |
9285 | nf_tables_module_autoload(net); |
9286 | else | |
9287 | nf_tables_module_autoload_cleanup(net); | |
9288 | ||
37082f93 PNA |
9289 | return 0; |
9290 | } | |
9291 | ||
a654de8f PNA |
9292 | static void nf_tables_cleanup(struct net *net) |
9293 | { | |
9294 | nft_validate_state_update(net, NFT_VALIDATE_SKIP); | |
9295 | } | |
9296 | ||
c0391b6a PNA |
9297 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, |
9298 | enum nfnl_abort_action action) | |
71ad00c5 | 9299 | { |
d59d2f82 | 9300 | struct nftables_pernet *nft_net = nft_pernet(net); |
c0391b6a | 9301 | int ret = __nf_tables_abort(net, action); |
f102d66b | 9302 | |
0854db2a | 9303 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9304 | |
9305 | return ret; | |
71ad00c5 FW |
9306 | } |
9307 | ||
74e8bcd2 PNA |
9308 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
9309 | { | |
d59d2f82 | 9310 | struct nftables_pernet *nft_net = nft_pernet(net); |
f102d66b FW |
9311 | bool genid_ok; |
9312 | ||
0854db2a | 9313 | mutex_lock(&nft_net->commit_mutex); |
f102d66b | 9314 | |
0854db2a | 9315 | genid_ok = genid == 0 || nft_net->base_seq == genid; |
f102d66b | 9316 | if (!genid_ok) |
0854db2a | 9317 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9318 | |
9319 | /* else, commit mutex has to be released by commit or abort function */ | |
9320 | return genid_ok; | |
74e8bcd2 PNA |
9321 | } |
9322 | ||
96518518 PM |
9323 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
9324 | .name = "nf_tables", | |
9325 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
9326 | .cb_count = NFT_MSG_MAX, | |
9327 | .cb = nf_tables_cb, | |
0628b123 PNA |
9328 | .commit = nf_tables_commit, |
9329 | .abort = nf_tables_abort, | |
a654de8f | 9330 | .cleanup = nf_tables_cleanup, |
74e8bcd2 | 9331 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 9332 | .owner = THIS_MODULE, |
96518518 PM |
9333 | }; |
9334 | ||
7210e4e3 | 9335 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 9336 | enum nft_chain_types type) |
7210e4e3 PNA |
9337 | { |
9338 | const struct nft_base_chain *basechain; | |
9339 | ||
f323d954 | 9340 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
9341 | basechain = nft_base_chain(chain); |
9342 | if (basechain->type->type != type) | |
9343 | return -EOPNOTSUPP; | |
9344 | } | |
9345 | return 0; | |
9346 | } | |
9347 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
9348 | ||
75e8d06d PNA |
9349 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
9350 | unsigned int hook_flags) | |
9351 | { | |
9352 | struct nft_base_chain *basechain; | |
9353 | ||
f323d954 | 9354 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
9355 | basechain = nft_base_chain(chain); |
9356 | ||
c974a3a3 | 9357 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
9358 | return 0; |
9359 | ||
9360 | return -EOPNOTSUPP; | |
9361 | } | |
9362 | ||
9363 | return 0; | |
9364 | } | |
9365 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
9366 | ||
20a69341 PM |
9367 | /* |
9368 | * Loop detection - walk through the ruleset beginning at the destination chain | |
9369 | * of a new jump until either the source chain is reached (loop) or all | |
9370 | * reachable chains have been traversed. | |
9371 | * | |
9372 | * The loop check is performed whenever a new jump verdict is added to an | |
9373 | * expression or verdict map or a verdict map is bound to a new chain. | |
9374 | */ | |
9375 | ||
9376 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
9377 | const struct nft_chain *chain); | |
9378 | ||
6387aa6e PNA |
9379 | static int nft_check_loops(const struct nft_ctx *ctx, |
9380 | const struct nft_set_ext *ext) | |
9381 | { | |
9382 | const struct nft_data *data; | |
9383 | int ret; | |
9384 | ||
9385 | data = nft_set_ext_data(ext); | |
9386 | switch (data->verdict.code) { | |
9387 | case NFT_JUMP: | |
9388 | case NFT_GOTO: | |
9389 | ret = nf_tables_check_loops(ctx, data->verdict.chain); | |
9390 | break; | |
9391 | default: | |
9392 | ret = 0; | |
9393 | break; | |
9394 | } | |
9395 | ||
9396 | return ret; | |
9397 | } | |
9398 | ||
20a69341 | 9399 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, |
de70185d | 9400 | struct nft_set *set, |
20a69341 | 9401 | const struct nft_set_iter *iter, |
de70185d | 9402 | struct nft_set_elem *elem) |
20a69341 | 9403 | { |
fe2811eb | 9404 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
fe2811eb PM |
9405 | |
9406 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
9407 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
9408 | return 0; |
9409 | ||
6387aa6e | 9410 | return nft_check_loops(ctx, ext); |
20a69341 PM |
9411 | } |
9412 | ||
aaa31047 PNA |
9413 | static int nft_set_catchall_loops(const struct nft_ctx *ctx, |
9414 | struct nft_set *set) | |
9415 | { | |
9416 | u8 genmask = nft_genmask_next(ctx->net); | |
9417 | struct nft_set_elem_catchall *catchall; | |
9418 | struct nft_set_ext *ext; | |
9419 | int ret = 0; | |
9420 | ||
9421 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
9422 | ext = nft_set_elem_ext(set, catchall->elem); | |
9423 | if (!nft_set_elem_active(ext, genmask)) | |
9424 | continue; | |
9425 | ||
9426 | ret = nft_check_loops(ctx, ext); | |
9427 | if (ret < 0) | |
9428 | return ret; | |
9429 | } | |
9430 | ||
9431 | return ret; | |
9432 | } | |
9433 | ||
20a69341 PM |
9434 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
9435 | const struct nft_chain *chain) | |
9436 | { | |
9437 | const struct nft_rule *rule; | |
9438 | const struct nft_expr *expr, *last; | |
de70185d | 9439 | struct nft_set *set; |
20a69341 PM |
9440 | struct nft_set_binding *binding; |
9441 | struct nft_set_iter iter; | |
20a69341 PM |
9442 | |
9443 | if (ctx->chain == chain) | |
9444 | return -ELOOP; | |
9445 | ||
9446 | list_for_each_entry(rule, &chain->rules, list) { | |
9447 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
9448 | struct nft_immediate_expr *priv; |
9449 | const struct nft_data *data; | |
0ca743a5 PNA |
9450 | int err; |
9451 | ||
a654de8f | 9452 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
9453 | continue; |
9454 | ||
a654de8f PNA |
9455 | priv = nft_expr_priv(expr); |
9456 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 9457 | continue; |
20a69341 | 9458 | |
a654de8f | 9459 | data = &priv->data; |
1ca2e170 | 9460 | switch (data->verdict.code) { |
20a69341 PM |
9461 | case NFT_JUMP: |
9462 | case NFT_GOTO: | |
1ca2e170 PM |
9463 | err = nf_tables_check_loops(ctx, |
9464 | data->verdict.chain); | |
20a69341 PM |
9465 | if (err < 0) |
9466 | return err; | |
c2168e6b | 9467 | break; |
20a69341 PM |
9468 | default: |
9469 | break; | |
9470 | } | |
9471 | } | |
9472 | } | |
9473 | ||
9474 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
9475 | if (!nft_is_active_next(ctx->net, set)) |
9476 | continue; | |
20a69341 PM |
9477 | if (!(set->flags & NFT_SET_MAP) || |
9478 | set->dtype != NFT_DATA_VERDICT) | |
9479 | continue; | |
9480 | ||
9481 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
9482 | if (!(binding->flags & NFT_SET_MAP) || |
9483 | binding->chain != chain) | |
20a69341 PM |
9484 | continue; |
9485 | ||
8588ac09 | 9486 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
9487 | iter.skip = 0; |
9488 | iter.count = 0; | |
9489 | iter.err = 0; | |
9490 | iter.fn = nf_tables_loop_check_setelem; | |
9491 | ||
9492 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
9493 | if (!iter.err) |
9494 | iter.err = nft_set_catchall_loops(ctx, set); | |
9495 | ||
20a69341 PM |
9496 | if (iter.err < 0) |
9497 | return iter.err; | |
9498 | } | |
9499 | } | |
9500 | ||
9501 | return 0; | |
9502 | } | |
9503 | ||
36b701fa LGL |
9504 | /** |
9505 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
9506 | * | |
9507 | * @attr: netlink attribute to fetch value from | |
9508 | * @max: maximum value to be stored in dest | |
9509 | * @dest: pointer to the variable | |
9510 | * | |
9511 | * Parse, check and store a given u32 netlink attribute into variable. | |
9512 | * This function returns -ERANGE if the value goes over maximum value. | |
9513 | * Otherwise a 0 is returned and the attribute value is stored in the | |
9514 | * destination variable. | |
9515 | */ | |
f1d505bb | 9516 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 9517 | { |
09525a09 | 9518 | u32 val; |
36b701fa LGL |
9519 | |
9520 | val = ntohl(nla_get_be32(attr)); | |
9521 | if (val > max) | |
9522 | return -ERANGE; | |
9523 | ||
9524 | *dest = val; | |
9525 | return 0; | |
9526 | } | |
9527 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
9528 | ||
6c6f9f31 | 9529 | static int nft_parse_register(const struct nlattr *attr, u32 *preg) |
b1c96ed3 | 9530 | { |
49499c3e PM |
9531 | unsigned int reg; |
9532 | ||
9533 | reg = ntohl(nla_get_be32(attr)); | |
9534 | switch (reg) { | |
9535 | case NFT_REG_VERDICT...NFT_REG_4: | |
6e1acfa3 PNA |
9536 | *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE; |
9537 | break; | |
9538 | case NFT_REG32_00...NFT_REG32_15: | |
9539 | *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
9540 | break; | |
49499c3e | 9541 | default: |
6e1acfa3 | 9542 | return -ERANGE; |
49499c3e | 9543 | } |
6e1acfa3 PNA |
9544 | |
9545 | return 0; | |
b1c96ed3 | 9546 | } |
b1c96ed3 | 9547 | |
49499c3e PM |
9548 | /** |
9549 | * nft_dump_register - dump a register value to a netlink attribute | |
9550 | * | |
9551 | * @skb: socket buffer | |
9552 | * @attr: attribute number | |
9553 | * @reg: register number | |
9554 | * | |
9555 | * Construct a netlink attribute containing the register number. For | |
9556 | * compatibility reasons, register numbers being a multiple of 4 are | |
9557 | * translated to the corresponding 128 bit register numbers. | |
9558 | */ | |
b1c96ed3 PM |
9559 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
9560 | { | |
49499c3e PM |
9561 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
9562 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
9563 | else | |
9564 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
9565 | ||
b1c96ed3 PM |
9566 | return nla_put_be32(skb, attr, htonl(reg)); |
9567 | } | |
9568 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
9569 | ||
4f16d25c | 9570 | static int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 9571 | { |
49499c3e | 9572 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 9573 | return -EINVAL; |
d07db988 PM |
9574 | if (len == 0) |
9575 | return -EINVAL; | |
c593642c | 9576 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 9577 | return -ERANGE; |
49499c3e | 9578 | |
96518518 PM |
9579 | return 0; |
9580 | } | |
4f16d25c PNA |
9581 | |
9582 | int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len) | |
9583 | { | |
9584 | u32 reg; | |
9585 | int err; | |
9586 | ||
6e1acfa3 PNA |
9587 | err = nft_parse_register(attr, ®); |
9588 | if (err < 0) | |
9589 | return err; | |
9590 | ||
4f16d25c PNA |
9591 | err = nft_validate_register_load(reg, len); |
9592 | if (err < 0) | |
9593 | return err; | |
9594 | ||
9595 | *sreg = reg; | |
9596 | return 0; | |
9597 | } | |
9598 | EXPORT_SYMBOL_GPL(nft_parse_register_load); | |
96518518 | 9599 | |
345023b0 PNA |
9600 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
9601 | enum nft_registers reg, | |
9602 | const struct nft_data *data, | |
9603 | enum nft_data_types type, | |
9604 | unsigned int len) | |
96518518 | 9605 | { |
20a69341 PM |
9606 | int err; |
9607 | ||
96518518 PM |
9608 | switch (reg) { |
9609 | case NFT_REG_VERDICT: | |
58f40ab6 | 9610 | if (type != NFT_DATA_VERDICT) |
96518518 | 9611 | return -EINVAL; |
20a69341 | 9612 | |
58f40ab6 | 9613 | if (data != NULL && |
1ca2e170 PM |
9614 | (data->verdict.code == NFT_GOTO || |
9615 | data->verdict.code == NFT_JUMP)) { | |
9616 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
9617 | if (err < 0) |
9618 | return err; | |
20a69341 PM |
9619 | } |
9620 | ||
96518518 PM |
9621 | return 0; |
9622 | default: | |
49499c3e | 9623 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 9624 | return -EINVAL; |
45d9bcda PM |
9625 | if (len == 0) |
9626 | return -EINVAL; | |
49499c3e | 9627 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 9628 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 9629 | return -ERANGE; |
27e6d201 | 9630 | |
96518518 PM |
9631 | if (data != NULL && type != NFT_DATA_VALUE) |
9632 | return -EINVAL; | |
9633 | return 0; | |
9634 | } | |
9635 | } | |
345023b0 PNA |
9636 | |
9637 | int nft_parse_register_store(const struct nft_ctx *ctx, | |
9638 | const struct nlattr *attr, u8 *dreg, | |
9639 | const struct nft_data *data, | |
9640 | enum nft_data_types type, unsigned int len) | |
9641 | { | |
9642 | int err; | |
9643 | u32 reg; | |
9644 | ||
6e1acfa3 PNA |
9645 | err = nft_parse_register(attr, ®); |
9646 | if (err < 0) | |
9647 | return err; | |
9648 | ||
345023b0 PNA |
9649 | err = nft_validate_register_store(ctx, reg, data, type, len); |
9650 | if (err < 0) | |
9651 | return err; | |
9652 | ||
9653 | *dreg = reg; | |
9654 | return 0; | |
9655 | } | |
9656 | EXPORT_SYMBOL_GPL(nft_parse_register_store); | |
96518518 PM |
9657 | |
9658 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
9659 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
9660 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
9661 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
51d70f18 | 9662 | [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
9663 | }; |
9664 | ||
9665 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
9666 | struct nft_data_desc *desc, const struct nlattr *nla) | |
9667 | { | |
664b0f8c | 9668 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
9669 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
9670 | struct nft_chain *chain; | |
9671 | int err; | |
9672 | ||
8cb08174 JB |
9673 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
9674 | nft_verdict_policy, NULL); | |
96518518 PM |
9675 | if (err < 0) |
9676 | return err; | |
9677 | ||
9678 | if (!tb[NFTA_VERDICT_CODE]) | |
9679 | return -EINVAL; | |
1ca2e170 | 9680 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 9681 | |
1ca2e170 | 9682 | switch (data->verdict.code) { |
e0abdadc | 9683 | default: |
1ca2e170 | 9684 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
9685 | case NF_ACCEPT: |
9686 | case NF_DROP: | |
9687 | case NF_QUEUE: | |
9688 | break; | |
9689 | default: | |
9690 | return -EINVAL; | |
9691 | } | |
954d8297 | 9692 | fallthrough; |
96518518 PM |
9693 | case NFT_CONTINUE: |
9694 | case NFT_BREAK: | |
9695 | case NFT_RETURN: | |
96518518 PM |
9696 | break; |
9697 | case NFT_JUMP: | |
9698 | case NFT_GOTO: | |
51d70f18 PNA |
9699 | if (tb[NFTA_VERDICT_CHAIN]) { |
9700 | chain = nft_chain_lookup(ctx->net, ctx->table, | |
9701 | tb[NFTA_VERDICT_CHAIN], | |
9702 | genmask); | |
9703 | } else if (tb[NFTA_VERDICT_CHAIN_ID]) { | |
95f466d2 | 9704 | chain = nft_chain_lookup_byid(ctx->net, ctx->table, |
51d70f18 PNA |
9705 | tb[NFTA_VERDICT_CHAIN_ID]); |
9706 | if (IS_ERR(chain)) | |
9707 | return PTR_ERR(chain); | |
9708 | } else { | |
96518518 | 9709 | return -EINVAL; |
51d70f18 PNA |
9710 | } |
9711 | ||
96518518 PM |
9712 | if (IS_ERR(chain)) |
9713 | return PTR_ERR(chain); | |
f323d954 | 9714 | if (nft_is_base_chain(chain)) |
96518518 | 9715 | return -EOPNOTSUPP; |
e02f0d39 PNA |
9716 | if (nft_chain_is_bound(chain)) |
9717 | return -EINVAL; | |
f323ef3a PNA |
9718 | if (desc->flags & NFT_DATA_DESC_SETELEM && |
9719 | chain->flags & NFT_CHAIN_BINDING) | |
9720 | return -EINVAL; | |
96518518 | 9721 | |
96518518 | 9722 | chain->use++; |
1ca2e170 | 9723 | data->verdict.chain = chain; |
96518518 | 9724 | break; |
96518518 PM |
9725 | } |
9726 | ||
4c4ed074 | 9727 | desc->len = sizeof(data->verdict); |
341b6941 | 9728 | |
96518518 PM |
9729 | return 0; |
9730 | } | |
9731 | ||
9732 | static void nft_verdict_uninit(const struct nft_data *data) | |
9733 | { | |
d0e2c7de PNA |
9734 | struct nft_chain *chain; |
9735 | struct nft_rule *rule; | |
9736 | ||
1ca2e170 | 9737 | switch (data->verdict.code) { |
96518518 PM |
9738 | case NFT_JUMP: |
9739 | case NFT_GOTO: | |
d0e2c7de PNA |
9740 | chain = data->verdict.chain; |
9741 | chain->use--; | |
9742 | ||
9743 | if (!nft_chain_is_bound(chain)) | |
9744 | break; | |
9745 | ||
9746 | chain->table->use--; | |
9747 | list_for_each_entry(rule, &chain->rules, list) | |
9748 | chain->use--; | |
9749 | ||
9750 | nft_chain_del(chain); | |
96518518 PM |
9751 | break; |
9752 | } | |
9753 | } | |
9754 | ||
33d5a7b1 | 9755 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
9756 | { |
9757 | struct nlattr *nest; | |
9758 | ||
ae0be8de | 9759 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
9760 | if (!nest) |
9761 | goto nla_put_failure; | |
9762 | ||
33d5a7b1 | 9763 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
9764 | goto nla_put_failure; |
9765 | ||
33d5a7b1 | 9766 | switch (v->code) { |
96518518 PM |
9767 | case NFT_JUMP: |
9768 | case NFT_GOTO: | |
1ca2e170 | 9769 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 9770 | v->chain->name)) |
96518518 PM |
9771 | goto nla_put_failure; |
9772 | } | |
9773 | nla_nest_end(skb, nest); | |
9774 | return 0; | |
9775 | ||
9776 | nla_put_failure: | |
9777 | return -1; | |
9778 | } | |
9779 | ||
d0a11fc3 | 9780 | static int nft_value_init(const struct nft_ctx *ctx, |
341b6941 PNA |
9781 | struct nft_data *data, struct nft_data_desc *desc, |
9782 | const struct nlattr *nla) | |
96518518 PM |
9783 | { |
9784 | unsigned int len; | |
9785 | ||
9786 | len = nla_len(nla); | |
9787 | if (len == 0) | |
9788 | return -EINVAL; | |
341b6941 | 9789 | if (len > desc->size) |
96518518 | 9790 | return -EOVERFLOW; |
341b6941 PNA |
9791 | if (desc->len) { |
9792 | if (len != desc->len) | |
9793 | return -EINVAL; | |
9794 | } else { | |
9795 | desc->len = len; | |
9796 | } | |
96518518 | 9797 | |
d0a11fc3 | 9798 | nla_memcpy(data->data, nla, len); |
341b6941 | 9799 | |
96518518 PM |
9800 | return 0; |
9801 | } | |
9802 | ||
9803 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
9804 | unsigned int len) | |
9805 | { | |
9806 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
9807 | } | |
9808 | ||
9809 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 9810 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
9811 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
9812 | }; | |
9813 | ||
9814 | /** | |
9815 | * nft_data_init - parse nf_tables data netlink attributes | |
9816 | * | |
9817 | * @ctx: context of the expression using the data | |
9818 | * @data: destination struct nft_data | |
9819 | * @desc: data description | |
9820 | * @nla: netlink attribute containing data | |
9821 | * | |
9822 | * Parse the netlink data attributes and initialize a struct nft_data. | |
9823 | * The type and length of data are returned in the data description. | |
9824 | * | |
9825 | * The caller can indicate that it only wants to accept data of type | |
9826 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
9827 | */ | |
341b6941 | 9828 | int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
96518518 PM |
9829 | struct nft_data_desc *desc, const struct nlattr *nla) |
9830 | { | |
9831 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
9832 | int err; | |
9833 | ||
341b6941 PNA |
9834 | if (WARN_ON_ONCE(!desc->size)) |
9835 | return -EINVAL; | |
9836 | ||
8cb08174 JB |
9837 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
9838 | nft_data_policy, NULL); | |
96518518 PM |
9839 | if (err < 0) |
9840 | return err; | |
9841 | ||
341b6941 PNA |
9842 | if (tb[NFTA_DATA_VALUE]) { |
9843 | if (desc->type != NFT_DATA_VALUE) | |
9844 | return -EINVAL; | |
9845 | ||
9846 | err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]); | |
9847 | } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) { | |
9848 | if (desc->type != NFT_DATA_VERDICT) | |
9849 | return -EINVAL; | |
9850 | ||
9851 | err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
9852 | } else { | |
9853 | err = -EINVAL; | |
9854 | } | |
9855 | ||
9856 | return err; | |
96518518 PM |
9857 | } |
9858 | EXPORT_SYMBOL_GPL(nft_data_init); | |
9859 | ||
9860 | /** | |
59105446 | 9861 | * nft_data_release - release a nft_data item |
96518518 PM |
9862 | * |
9863 | * @data: struct nft_data to release | |
9864 | * @type: type of data | |
9865 | * | |
9866 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
9867 | * all others need to be released by calling this function. | |
9868 | */ | |
59105446 | 9869 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 9870 | { |
960bd2c2 | 9871 | if (type < NFT_DATA_VERDICT) |
96518518 | 9872 | return; |
960bd2c2 | 9873 | switch (type) { |
96518518 PM |
9874 | case NFT_DATA_VERDICT: |
9875 | return nft_verdict_uninit(data); | |
9876 | default: | |
9877 | WARN_ON(1); | |
9878 | } | |
9879 | } | |
59105446 | 9880 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
9881 | |
9882 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
9883 | enum nft_data_types type, unsigned int len) | |
9884 | { | |
9885 | struct nlattr *nest; | |
9886 | int err; | |
9887 | ||
ae0be8de | 9888 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
9889 | if (nest == NULL) |
9890 | return -1; | |
9891 | ||
9892 | switch (type) { | |
9893 | case NFT_DATA_VALUE: | |
9894 | err = nft_value_dump(skb, data, len); | |
9895 | break; | |
9896 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 9897 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
9898 | break; |
9899 | default: | |
9900 | err = -EINVAL; | |
9901 | WARN_ON(1); | |
9902 | } | |
9903 | ||
9904 | nla_nest_end(skb, nest); | |
9905 | return err; | |
9906 | } | |
9907 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
9908 | ||
5ebe0b0e PNA |
9909 | int __nft_release_basechain(struct nft_ctx *ctx) |
9910 | { | |
9911 | struct nft_rule *rule, *nr; | |
9912 | ||
fa5950e4 FW |
9913 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
9914 | return 0; | |
5ebe0b0e | 9915 | |
c974a3a3 | 9916 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
9917 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
9918 | list_del(&rule->list); | |
9919 | ctx->chain->use--; | |
bb7b40ae | 9920 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 9921 | } |
1b2470e5 | 9922 | nft_chain_del(ctx->chain); |
5ebe0b0e | 9923 | ctx->table->use--; |
43a605f2 | 9924 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
9925 | |
9926 | return 0; | |
9927 | } | |
9928 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
9929 | ||
00dfe9be PNA |
9930 | static void __nft_release_hook(struct net *net, struct nft_table *table) |
9931 | { | |
6069da44 | 9932 | struct nft_flowtable *flowtable; |
00dfe9be PNA |
9933 | struct nft_chain *chain; |
9934 | ||
9935 | list_for_each_entry(chain, &table->chains, list) | |
f9a43007 | 9936 | __nf_tables_unregister_hook(net, table, chain, true); |
6069da44 | 9937 | list_for_each_entry(flowtable, &table->flowtables, list) |
f9a43007 PNA |
9938 | __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list, |
9939 | true); | |
00dfe9be PNA |
9940 | } |
9941 | ||
767d1216 PNA |
9942 | static void __nft_release_hooks(struct net *net) |
9943 | { | |
d59d2f82 | 9944 | struct nftables_pernet *nft_net = nft_pernet(net); |
767d1216 | 9945 | struct nft_table *table; |
767d1216 | 9946 | |
0854db2a | 9947 | list_for_each_entry(table, &nft_net->tables, list) { |
2888b080 PNA |
9948 | if (nft_table_has_owner(table)) |
9949 | continue; | |
9950 | ||
00dfe9be | 9951 | __nft_release_hook(net, table); |
2888b080 | 9952 | } |
767d1216 PNA |
9953 | } |
9954 | ||
fd020332 | 9955 | static void __nft_release_table(struct net *net, struct nft_table *table) |
df05ef87 | 9956 | { |
3b49e2e9 | 9957 | struct nft_flowtable *flowtable, *nf; |
df05ef87 | 9958 | struct nft_chain *chain, *nc; |
e5009240 | 9959 | struct nft_object *obj, *ne; |
df05ef87 PNA |
9960 | struct nft_rule *rule, *nr; |
9961 | struct nft_set *set, *ns; | |
9962 | struct nft_ctx ctx = { | |
9963 | .net = net, | |
43a605f2 | 9964 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
9965 | }; |
9966 | ||
fd020332 PNA |
9967 | ctx.family = table->family; |
9968 | ctx.table = table; | |
9969 | list_for_each_entry(chain, &table->chains, list) { | |
9970 | ctx.chain = chain; | |
9971 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
9972 | list_del(&rule->list); | |
9973 | chain->use--; | |
9974 | nf_tables_rule_release(&ctx, rule); | |
df05ef87 | 9975 | } |
df05ef87 | 9976 | } |
fd020332 PNA |
9977 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
9978 | list_del(&flowtable->list); | |
9979 | table->use--; | |
9980 | nf_tables_flowtable_destroy(flowtable); | |
9981 | } | |
9982 | list_for_each_entry_safe(set, ns, &table->sets, list) { | |
9983 | list_del(&set->list); | |
9984 | table->use--; | |
9985 | nft_set_destroy(&ctx, set); | |
9986 | } | |
9987 | list_for_each_entry_safe(obj, ne, &table->objects, list) { | |
9988 | nft_obj_del(obj); | |
9989 | table->use--; | |
9990 | nft_obj_destroy(&ctx, obj); | |
9991 | } | |
9992 | list_for_each_entry_safe(chain, nc, &table->chains, list) { | |
9993 | ctx.chain = chain; | |
9994 | nft_chain_del(chain); | |
9995 | table->use--; | |
9996 | nf_tables_chain_destroy(&ctx); | |
9997 | } | |
fd020332 PNA |
9998 | nf_tables_table_destroy(&ctx); |
9999 | } | |
10000 | ||
bd1777b3 | 10001 | static void __nft_release_tables(struct net *net) |
fd020332 | 10002 | { |
d59d2f82 | 10003 | struct nftables_pernet *nft_net = nft_pernet(net); |
fd020332 PNA |
10004 | struct nft_table *table, *nt; |
10005 | ||
0854db2a | 10006 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
bd1777b3 | 10007 | if (nft_table_has_owner(table)) |
6001a930 PNA |
10008 | continue; |
10009 | ||
a499b03b FW |
10010 | list_del(&table->list); |
10011 | ||
fd020332 | 10012 | __nft_release_table(net, table); |
6001a930 PNA |
10013 | } |
10014 | } | |
10015 | ||
10016 | static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event, | |
10017 | void *ptr) | |
10018 | { | |
a499b03b | 10019 | struct nft_table *table, *to_delete[8]; |
0854db2a | 10020 | struct nftables_pernet *nft_net; |
6001a930 | 10021 | struct netlink_notify *n = ptr; |
6001a930 | 10022 | struct net *net = n->net; |
a499b03b FW |
10023 | unsigned int deleted; |
10024 | bool restart = false; | |
6001a930 PNA |
10025 | |
10026 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER) | |
10027 | return NOTIFY_DONE; | |
10028 | ||
d59d2f82 | 10029 | nft_net = nft_pernet(net); |
a499b03b | 10030 | deleted = 0; |
0854db2a | 10031 | mutex_lock(&nft_net->commit_mutex); |
a499b03b | 10032 | again: |
0854db2a | 10033 | list_for_each_entry(table, &nft_net->tables, list) { |
6001a930 PNA |
10034 | if (nft_table_has_owner(table) && |
10035 | n->portid == table->nlpid) { | |
10036 | __nft_release_hook(net, table); | |
a499b03b FW |
10037 | list_del_rcu(&table->list); |
10038 | to_delete[deleted++] = table; | |
10039 | if (deleted >= ARRAY_SIZE(to_delete)) | |
10040 | break; | |
6001a930 PNA |
10041 | } |
10042 | } | |
a499b03b FW |
10043 | if (deleted) { |
10044 | restart = deleted >= ARRAY_SIZE(to_delete); | |
6001a930 | 10045 | synchronize_rcu(); |
a499b03b FW |
10046 | while (deleted) |
10047 | __nft_release_table(net, to_delete[--deleted]); | |
10048 | ||
10049 | if (restart) | |
10050 | goto again; | |
6001a930 | 10051 | } |
0854db2a | 10052 | mutex_unlock(&nft_net->commit_mutex); |
6001a930 PNA |
10053 | |
10054 | return NOTIFY_DONE; | |
df05ef87 PNA |
10055 | } |
10056 | ||
6001a930 PNA |
10057 | static struct notifier_block nft_nl_notifier = { |
10058 | .notifier_call = nft_rcv_nl_event, | |
10059 | }; | |
10060 | ||
dd4cbef7 PNA |
10061 | static int __net_init nf_tables_init_net(struct net *net) |
10062 | { | |
d59d2f82 | 10063 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10064 | |
10065 | INIT_LIST_HEAD(&nft_net->tables); | |
10066 | INIT_LIST_HEAD(&nft_net->commit_list); | |
10067 | INIT_LIST_HEAD(&nft_net->module_list); | |
10068 | INIT_LIST_HEAD(&nft_net->notify_list); | |
10069 | mutex_init(&nft_net->commit_mutex); | |
10070 | nft_net->base_seq = 1; | |
10071 | nft_net->validate_state = NFT_VALIDATE_SKIP; | |
a654de8f | 10072 | |
dd4cbef7 PNA |
10073 | return 0; |
10074 | } | |
10075 | ||
767d1216 PNA |
10076 | static void __net_exit nf_tables_pre_exit_net(struct net *net) |
10077 | { | |
3923b1e4 PNA |
10078 | struct nftables_pernet *nft_net = nft_pernet(net); |
10079 | ||
10080 | mutex_lock(&nft_net->commit_mutex); | |
767d1216 | 10081 | __nft_release_hooks(net); |
3923b1e4 | 10082 | mutex_unlock(&nft_net->commit_mutex); |
767d1216 PNA |
10083 | } |
10084 | ||
dd4cbef7 PNA |
10085 | static void __net_exit nf_tables_exit_net(struct net *net) |
10086 | { | |
d59d2f82 | 10087 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
10088 | |
10089 | mutex_lock(&nft_net->commit_mutex); | |
10090 | if (!list_empty(&nft_net->commit_list)) | |
c0391b6a | 10091 | __nf_tables_abort(net, NFNL_ABORT_NONE); |
bd1777b3 | 10092 | __nft_release_tables(net); |
0854db2a FW |
10093 | mutex_unlock(&nft_net->commit_mutex); |
10094 | WARN_ON_ONCE(!list_empty(&nft_net->tables)); | |
10095 | WARN_ON_ONCE(!list_empty(&nft_net->module_list)); | |
10096 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); | |
dd4cbef7 PNA |
10097 | } |
10098 | ||
99633ab2 | 10099 | static struct pernet_operations nf_tables_net_ops = { |
767d1216 PNA |
10100 | .init = nf_tables_init_net, |
10101 | .pre_exit = nf_tables_pre_exit_net, | |
10102 | .exit = nf_tables_exit_net, | |
0854db2a FW |
10103 | .id = &nf_tables_net_id, |
10104 | .size = sizeof(struct nftables_pernet), | |
99633ab2 PNA |
10105 | }; |
10106 | ||
96518518 PM |
10107 | static int __init nf_tables_module_init(void) |
10108 | { | |
10109 | int err; | |
10110 | ||
d209df3e FW |
10111 | err = register_pernet_subsys(&nf_tables_net_ops); |
10112 | if (err < 0) | |
10113 | return err; | |
10114 | ||
10115 | err = nft_chain_filter_init(); | |
10116 | if (err < 0) | |
6001a930 | 10117 | goto err_chain_filter; |
02c7b25e | 10118 | |
96518518 PM |
10119 | err = nf_tables_core_module_init(); |
10120 | if (err < 0) | |
6001a930 | 10121 | goto err_core_module; |
96518518 | 10122 | |
d209df3e | 10123 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 10124 | if (err < 0) |
6001a930 | 10125 | goto err_netdev_notifier; |
96518518 | 10126 | |
4d44175a FW |
10127 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
10128 | if (err < 0) | |
6001a930 | 10129 | goto err_rht_objname; |
4d44175a | 10130 | |
06d392cb | 10131 | err = nft_offload_init(); |
10132 | if (err < 0) | |
6001a930 PNA |
10133 | goto err_offload; |
10134 | ||
10135 | err = netlink_register_notifier(&nft_nl_notifier); | |
10136 | if (err < 0) | |
10137 | goto err_netlink_notifier; | |
06d392cb | 10138 | |
d209df3e FW |
10139 | /* must be last */ |
10140 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
10141 | if (err < 0) | |
6001a930 | 10142 | goto err_nfnl_subsys; |
3b49e2e9 | 10143 | |
c1deb065 | 10144 | nft_chain_route_init(); |
3474a2c6 | 10145 | |
d209df3e | 10146 | return err; |
6001a930 PNA |
10147 | |
10148 | err_nfnl_subsys: | |
10149 | netlink_unregister_notifier(&nft_nl_notifier); | |
10150 | err_netlink_notifier: | |
06d392cb | 10151 | nft_offload_exit(); |
6001a930 | 10152 | err_offload: |
4d44175a | 10153 | rhltable_destroy(&nft_objname_ht); |
6001a930 | 10154 | err_rht_objname: |
d209df3e | 10155 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
6001a930 | 10156 | err_netdev_notifier: |
96518518 | 10157 | nf_tables_core_module_exit(); |
6001a930 | 10158 | err_core_module: |
d209df3e | 10159 | nft_chain_filter_fini(); |
6001a930 | 10160 | err_chain_filter: |
d209df3e | 10161 | unregister_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
10162 | return err; |
10163 | } | |
10164 | ||
10165 | static void __exit nf_tables_module_exit(void) | |
10166 | { | |
10167 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
6001a930 | 10168 | netlink_unregister_notifier(&nft_nl_notifier); |
06d392cb | 10169 | nft_offload_exit(); |
3b49e2e9 | 10170 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 10171 | nft_chain_filter_fini(); |
c1deb065 | 10172 | nft_chain_route_fini(); |
71ad00c5 | 10173 | unregister_pernet_subsys(&nf_tables_net_ops); |
0935d558 | 10174 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 10175 | rcu_barrier(); |
4d44175a | 10176 | rhltable_destroy(&nft_objname_ht); |
96518518 | 10177 | nf_tables_core_module_exit(); |
96518518 PM |
10178 | } |
10179 | ||
10180 | module_init(nf_tables_module_init); | |
10181 | module_exit(nf_tables_module_exit); | |
10182 | ||
10183 | MODULE_LICENSE("GPL"); | |
10184 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
10185 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |