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); | |
3ecbfd65 | 35 | static u64 table_handle; |
96518518 | 36 | |
a654de8f PNA |
37 | enum { |
38 | NFT_VALIDATE_SKIP = 0, | |
39 | NFT_VALIDATE_NEED, | |
40 | NFT_VALIDATE_DO, | |
41 | }; | |
42 | ||
4d44175a FW |
43 | static struct rhltable nft_objname_ht; |
44 | ||
1b2470e5 FW |
45 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed); |
46 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed); | |
47 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
48 | ||
4d44175a FW |
49 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed); |
50 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed); | |
51 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
52 | ||
1b2470e5 FW |
53 | static const struct rhashtable_params nft_chain_ht_params = { |
54 | .head_offset = offsetof(struct nft_chain, rhlhead), | |
55 | .key_offset = offsetof(struct nft_chain, name), | |
56 | .hashfn = nft_chain_hash, | |
57 | .obj_hashfn = nft_chain_hash_obj, | |
58 | .obj_cmpfn = nft_chain_hash_cmp, | |
1b2470e5 FW |
59 | .automatic_shrinking = true, |
60 | }; | |
61 | ||
4d44175a FW |
62 | static const struct rhashtable_params nft_objname_ht_params = { |
63 | .head_offset = offsetof(struct nft_object, rhlhead), | |
64 | .key_offset = offsetof(struct nft_object, key), | |
65 | .hashfn = nft_objname_hash, | |
66 | .obj_hashfn = nft_objname_hash_obj, | |
67 | .obj_cmpfn = nft_objname_hash_cmp, | |
68 | .automatic_shrinking = true, | |
69 | }; | |
70 | ||
c520292f RGB |
71 | struct nft_audit_data { |
72 | struct nft_table *table; | |
73 | int entries; | |
74 | int op; | |
75 | struct list_head list; | |
76 | }; | |
77 | ||
78 | static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types | |
79 | [NFT_MSG_NEWTABLE] = AUDIT_NFT_OP_TABLE_REGISTER, | |
80 | [NFT_MSG_GETTABLE] = AUDIT_NFT_OP_INVALID, | |
81 | [NFT_MSG_DELTABLE] = AUDIT_NFT_OP_TABLE_UNREGISTER, | |
82 | [NFT_MSG_NEWCHAIN] = AUDIT_NFT_OP_CHAIN_REGISTER, | |
83 | [NFT_MSG_GETCHAIN] = AUDIT_NFT_OP_INVALID, | |
84 | [NFT_MSG_DELCHAIN] = AUDIT_NFT_OP_CHAIN_UNREGISTER, | |
85 | [NFT_MSG_NEWRULE] = AUDIT_NFT_OP_RULE_REGISTER, | |
86 | [NFT_MSG_GETRULE] = AUDIT_NFT_OP_INVALID, | |
87 | [NFT_MSG_DELRULE] = AUDIT_NFT_OP_RULE_UNREGISTER, | |
88 | [NFT_MSG_NEWSET] = AUDIT_NFT_OP_SET_REGISTER, | |
89 | [NFT_MSG_GETSET] = AUDIT_NFT_OP_INVALID, | |
90 | [NFT_MSG_DELSET] = AUDIT_NFT_OP_SET_UNREGISTER, | |
91 | [NFT_MSG_NEWSETELEM] = AUDIT_NFT_OP_SETELEM_REGISTER, | |
92 | [NFT_MSG_GETSETELEM] = AUDIT_NFT_OP_INVALID, | |
93 | [NFT_MSG_DELSETELEM] = AUDIT_NFT_OP_SETELEM_UNREGISTER, | |
94 | [NFT_MSG_NEWGEN] = AUDIT_NFT_OP_GEN_REGISTER, | |
95 | [NFT_MSG_GETGEN] = AUDIT_NFT_OP_INVALID, | |
96 | [NFT_MSG_TRACE] = AUDIT_NFT_OP_INVALID, | |
97 | [NFT_MSG_NEWOBJ] = AUDIT_NFT_OP_OBJ_REGISTER, | |
98 | [NFT_MSG_GETOBJ] = AUDIT_NFT_OP_INVALID, | |
99 | [NFT_MSG_DELOBJ] = AUDIT_NFT_OP_OBJ_UNREGISTER, | |
100 | [NFT_MSG_GETOBJ_RESET] = AUDIT_NFT_OP_OBJ_RESET, | |
101 | [NFT_MSG_NEWFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_REGISTER, | |
102 | [NFT_MSG_GETFLOWTABLE] = AUDIT_NFT_OP_INVALID, | |
103 | [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, | |
104 | }; | |
105 | ||
a654de8f PNA |
106 | static void nft_validate_state_update(struct net *net, u8 new_validate_state) |
107 | { | |
d59d2f82 | 108 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
109 | |
110 | switch (nft_net->validate_state) { | |
a654de8f PNA |
111 | case NFT_VALIDATE_SKIP: |
112 | WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO); | |
113 | break; | |
114 | case NFT_VALIDATE_NEED: | |
115 | break; | |
116 | case NFT_VALIDATE_DO: | |
117 | if (new_validate_state == NFT_VALIDATE_NEED) | |
118 | return; | |
119 | } | |
120 | ||
0854db2a | 121 | nft_net->validate_state = new_validate_state; |
a654de8f | 122 | } |
0935d558 FW |
123 | static void nf_tables_trans_destroy_work(struct work_struct *w); |
124 | static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work); | |
a654de8f | 125 | |
7c95f6d8 | 126 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 127 | struct net *net, |
7c95f6d8 PNA |
128 | const struct sk_buff *skb, |
129 | const struct nlmsghdr *nlh, | |
36596dad | 130 | u8 family, |
7c95f6d8 PNA |
131 | struct nft_table *table, |
132 | struct nft_chain *chain, | |
133 | const struct nlattr * const *nla) | |
134 | { | |
633c9a84 | 135 | ctx->net = net; |
36596dad | 136 | ctx->family = family; |
26b2f552 | 137 | ctx->level = 0; |
128ad332 PNA |
138 | ctx->table = table; |
139 | ctx->chain = chain; | |
140 | ctx->nla = nla; | |
141 | ctx->portid = NETLINK_CB(skb).portid; | |
142 | ctx->report = nlmsg_report(nlh); | |
c9626a2c | 143 | ctx->flags = nlh->nlmsg_flags; |
128ad332 | 144 | ctx->seq = nlh->nlmsg_seq; |
7c95f6d8 PNA |
145 | } |
146 | ||
8411b644 PNA |
147 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
148 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
149 | { |
150 | struct nft_trans *trans; | |
151 | ||
8411b644 | 152 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
153 | if (trans == NULL) |
154 | return NULL; | |
155 | ||
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, | |
225 | struct list_head *hook_list) | |
226 | { | |
227 | struct nft_hook *hook; | |
228 | ||
229 | list_for_each_entry(hook, hook_list, list) | |
230 | nf_unregister_net_hook(net, &hook->ops); | |
231 | } | |
232 | ||
c974a3a3 PNA |
233 | static int nf_tables_register_hook(struct net *net, |
234 | const struct nft_table *table, | |
235 | struct nft_chain *chain) | |
d8ee8f7c | 236 | { |
d54725cd | 237 | struct nft_base_chain *basechain; |
a37061a6 | 238 | const struct nf_hook_ops *ops; |
ae6153b5 | 239 | |
d8ee8f7c | 240 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 241 | !nft_is_base_chain(chain)) |
d8ee8f7c PNA |
242 | return 0; |
243 | ||
4e25ceb8 FW |
244 | basechain = nft_base_chain(chain); |
245 | ops = &basechain->ops; | |
ae6153b5 | 246 | |
4e25ceb8 FW |
247 | if (basechain->type->ops_register) |
248 | return basechain->type->ops_register(net, ops); | |
249 | ||
d3519cb8 | 250 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
1e9451cb FW |
251 | return nft_netdev_register_hooks(net, &basechain->hook_list); |
252 | ||
253 | return nf_register_net_hook(net, &basechain->ops); | |
d8ee8f7c PNA |
254 | } |
255 | ||
c974a3a3 PNA |
256 | static void nf_tables_unregister_hook(struct net *net, |
257 | const struct nft_table *table, | |
258 | struct nft_chain *chain) | |
c5598794 | 259 | { |
d54725cd | 260 | struct nft_base_chain *basechain; |
4e25ceb8 FW |
261 | const struct nf_hook_ops *ops; |
262 | ||
d8ee8f7c | 263 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 264 | !nft_is_base_chain(chain)) |
d8ee8f7c | 265 | return; |
4e25ceb8 FW |
266 | basechain = nft_base_chain(chain); |
267 | ops = &basechain->ops; | |
d8ee8f7c | 268 | |
4e25ceb8 FW |
269 | if (basechain->type->ops_unregister) |
270 | return basechain->type->ops_unregister(net, ops); | |
271 | ||
d3519cb8 | 272 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
1e9451cb FW |
273 | nft_netdev_unregister_hooks(net, &basechain->hook_list); |
274 | else | |
275 | nf_unregister_net_hook(net, &basechain->ops); | |
c5598794 AB |
276 | } |
277 | ||
0854db2a FW |
278 | static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans) |
279 | { | |
d59d2f82 | 280 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a | 281 | |
0854db2a FW |
282 | list_add_tail(&trans->list, &nft_net->commit_list); |
283 | } | |
284 | ||
ee01d542 AB |
285 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
286 | { | |
287 | struct nft_trans *trans; | |
288 | ||
289 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
290 | if (trans == NULL) | |
291 | return -ENOMEM; | |
292 | ||
293 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 294 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 | 295 | |
0854db2a | 296 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
297 | return 0; |
298 | } | |
299 | ||
300 | static int nft_deltable(struct nft_ctx *ctx) | |
301 | { | |
302 | int err; | |
303 | ||
304 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
305 | if (err < 0) | |
306 | return err; | |
307 | ||
f2a6d766 | 308 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
309 | return err; |
310 | } | |
311 | ||
66293c46 | 312 | static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
ee01d542 AB |
313 | { |
314 | struct nft_trans *trans; | |
315 | ||
316 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
317 | if (trans == NULL) | |
66293c46 | 318 | return ERR_PTR(-ENOMEM); |
ee01d542 | 319 | |
74cccc3d | 320 | if (msg_type == NFT_MSG_NEWCHAIN) { |
664b0f8c | 321 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 | 322 | |
74cccc3d PNA |
323 | if (ctx->nla[NFTA_CHAIN_ID]) { |
324 | nft_trans_chain_id(trans) = | |
325 | ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID])); | |
326 | } | |
327 | } | |
328 | ||
0854db2a | 329 | nft_trans_commit_list_add_tail(ctx->net, trans); |
66293c46 | 330 | return trans; |
ee01d542 AB |
331 | } |
332 | ||
333 | static int nft_delchain(struct nft_ctx *ctx) | |
334 | { | |
66293c46 | 335 | struct nft_trans *trans; |
ee01d542 | 336 | |
66293c46 FW |
337 | trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
338 | if (IS_ERR(trans)) | |
339 | return PTR_ERR(trans); | |
ee01d542 AB |
340 | |
341 | ctx->table->use--; | |
664b0f8c | 342 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 | 343 | |
66293c46 | 344 | return 0; |
ee01d542 AB |
345 | } |
346 | ||
bb7b40ae PNA |
347 | static void nft_rule_expr_activate(const struct nft_ctx *ctx, |
348 | struct nft_rule *rule) | |
349 | { | |
350 | struct nft_expr *expr; | |
351 | ||
352 | expr = nft_expr_first(rule); | |
31cc578a | 353 | while (nft_expr_more(rule, expr)) { |
bb7b40ae PNA |
354 | if (expr->ops->activate) |
355 | expr->ops->activate(ctx, expr); | |
356 | ||
357 | expr = nft_expr_next(expr); | |
358 | } | |
359 | } | |
360 | ||
361 | static void nft_rule_expr_deactivate(const struct nft_ctx *ctx, | |
f6ac8585 PNA |
362 | struct nft_rule *rule, |
363 | enum nft_trans_phase phase) | |
bb7b40ae PNA |
364 | { |
365 | struct nft_expr *expr; | |
366 | ||
367 | expr = nft_expr_first(rule); | |
31cc578a | 368 | while (nft_expr_more(rule, expr)) { |
bb7b40ae | 369 | if (expr->ops->deactivate) |
f6ac8585 | 370 | expr->ops->deactivate(ctx, expr, phase); |
bb7b40ae PNA |
371 | |
372 | expr = nft_expr_next(expr); | |
373 | } | |
374 | } | |
375 | ||
ee01d542 AB |
376 | static int |
377 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
378 | { | |
379 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
380 | if (nft_is_active_next(ctx->net, rule)) { |
381 | nft_deactivate_next(ctx->net, rule); | |
ee01d542 AB |
382 | ctx->chain->use--; |
383 | return 0; | |
384 | } | |
385 | return -ENOENT; | |
386 | } | |
387 | ||
388 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
389 | struct nft_rule *rule) | |
390 | { | |
391 | struct nft_trans *trans; | |
392 | ||
393 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
394 | if (trans == NULL) | |
395 | return NULL; | |
396 | ||
1a94e38d PNA |
397 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
398 | nft_trans_rule_id(trans) = | |
399 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
400 | } | |
ee01d542 | 401 | nft_trans_rule(trans) = rule; |
0854db2a | 402 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
403 | |
404 | return trans; | |
405 | } | |
406 | ||
407 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
408 | { | |
63b48c73 | 409 | struct nft_flow_rule *flow; |
ee01d542 AB |
410 | struct nft_trans *trans; |
411 | int err; | |
412 | ||
413 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
414 | if (trans == NULL) | |
415 | return -ENOMEM; | |
416 | ||
63b48c73 PNA |
417 | if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
418 | flow = nft_flow_rule_create(ctx->net, rule); | |
419 | if (IS_ERR(flow)) { | |
420 | nft_trans_destroy(trans); | |
421 | return PTR_ERR(flow); | |
422 | } | |
423 | ||
424 | nft_trans_flow_rule(trans) = flow; | |
425 | } | |
426 | ||
ee01d542 AB |
427 | err = nf_tables_delrule_deactivate(ctx, rule); |
428 | if (err < 0) { | |
429 | nft_trans_destroy(trans); | |
430 | return err; | |
431 | } | |
f6ac8585 | 432 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE); |
ee01d542 AB |
433 | |
434 | return 0; | |
435 | } | |
436 | ||
437 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
438 | { | |
439 | struct nft_rule *rule; | |
440 | int err; | |
441 | ||
442 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
23b7ca4f PNA |
443 | if (!nft_is_active_next(ctx->net, rule)) |
444 | continue; | |
445 | ||
ee01d542 AB |
446 | err = nft_delrule(ctx, rule); |
447 | if (err < 0) | |
448 | return err; | |
449 | } | |
450 | return 0; | |
451 | } | |
452 | ||
cd5125d8 | 453 | static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
ee01d542 AB |
454 | struct nft_set *set) |
455 | { | |
456 | struct nft_trans *trans; | |
457 | ||
458 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
459 | if (trans == NULL) | |
460 | return -ENOMEM; | |
461 | ||
462 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { | |
463 | nft_trans_set_id(trans) = | |
464 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 465 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
466 | } |
467 | nft_trans_set(trans) = set; | |
0854db2a | 468 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
469 | |
470 | return 0; | |
471 | } | |
472 | ||
cd5125d8 | 473 | static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set) |
ee01d542 AB |
474 | { |
475 | int err; | |
476 | ||
477 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
478 | if (err < 0) | |
479 | return err; | |
480 | ||
37a9cc52 | 481 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
482 | ctx->table->use--; |
483 | ||
484 | return err; | |
485 | } | |
486 | ||
e5009240 PNA |
487 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
488 | struct nft_object *obj) | |
489 | { | |
490 | struct nft_trans *trans; | |
491 | ||
492 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
493 | if (trans == NULL) | |
494 | return -ENOMEM; | |
495 | ||
496 | if (msg_type == NFT_MSG_NEWOBJ) | |
497 | nft_activate_next(ctx->net, obj); | |
498 | ||
499 | nft_trans_obj(trans) = obj; | |
0854db2a | 500 | nft_trans_commit_list_add_tail(ctx->net, trans); |
e5009240 PNA |
501 | |
502 | return 0; | |
503 | } | |
504 | ||
505 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
506 | { | |
507 | int err; | |
508 | ||
509 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
510 | if (err < 0) | |
511 | return err; | |
512 | ||
513 | nft_deactivate_next(ctx->net, obj); | |
514 | ctx->table->use--; | |
515 | ||
516 | return err; | |
517 | } | |
518 | ||
3b49e2e9 PNA |
519 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
520 | struct nft_flowtable *flowtable) | |
521 | { | |
522 | struct nft_trans *trans; | |
523 | ||
524 | trans = nft_trans_alloc(ctx, msg_type, | |
525 | sizeof(struct nft_trans_flowtable)); | |
526 | if (trans == NULL) | |
527 | return -ENOMEM; | |
528 | ||
529 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
530 | nft_activate_next(ctx->net, flowtable); | |
531 | ||
532 | nft_trans_flowtable(trans) = flowtable; | |
0854db2a | 533 | nft_trans_commit_list_add_tail(ctx->net, trans); |
3b49e2e9 PNA |
534 | |
535 | return 0; | |
536 | } | |
537 | ||
538 | static int nft_delflowtable(struct nft_ctx *ctx, | |
539 | struct nft_flowtable *flowtable) | |
540 | { | |
541 | int err; | |
542 | ||
543 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
544 | if (err < 0) | |
545 | return err; | |
546 | ||
547 | nft_deactivate_next(ctx->net, flowtable); | |
548 | ctx->table->use--; | |
549 | ||
550 | return err; | |
551 | } | |
552 | ||
96518518 PM |
553 | /* |
554 | * Tables | |
555 | */ | |
556 | ||
36596dad | 557 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 558 | const struct nlattr *nla, |
6001a930 | 559 | u8 family, u8 genmask, u32 nlpid) |
96518518 | 560 | { |
0854db2a | 561 | struct nftables_pernet *nft_net; |
96518518 PM |
562 | struct nft_table *table; |
563 | ||
cac20fcd PNA |
564 | if (nla == NULL) |
565 | return ERR_PTR(-EINVAL); | |
566 | ||
d59d2f82 | 567 | nft_net = nft_pernet(net); |
0854db2a FW |
568 | list_for_each_entry_rcu(table, &nft_net->tables, list, |
569 | lockdep_is_held(&nft_net->commit_mutex)) { | |
f2a6d766 | 570 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 571 | table->family == family && |
6001a930 PNA |
572 | nft_active_genmask(table, genmask)) { |
573 | if (nft_table_has_owner(table) && | |
53479909 | 574 | nlpid && table->nlpid != nlpid) |
6001a930 PNA |
575 | return ERR_PTR(-EPERM); |
576 | ||
96518518 | 577 | return table; |
6001a930 | 578 | } |
96518518 | 579 | } |
cac20fcd PNA |
580 | |
581 | return ERR_PTR(-ENOENT); | |
96518518 PM |
582 | } |
583 | ||
3ecbfd65 HS |
584 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
585 | const struct nlattr *nla, | |
e31f072f | 586 | u8 genmask, u32 nlpid) |
3ecbfd65 | 587 | { |
0854db2a | 588 | struct nftables_pernet *nft_net; |
3ecbfd65 HS |
589 | struct nft_table *table; |
590 | ||
d59d2f82 | 591 | nft_net = nft_pernet(net); |
0854db2a | 592 | list_for_each_entry(table, &nft_net->tables, list) { |
3ecbfd65 | 593 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && |
e31f072f PNA |
594 | nft_active_genmask(table, genmask)) { |
595 | if (nft_table_has_owner(table) && | |
596 | nlpid && table->nlpid != nlpid) | |
597 | return ERR_PTR(-EPERM); | |
598 | ||
3ecbfd65 | 599 | return table; |
e31f072f | 600 | } |
3ecbfd65 | 601 | } |
3ecbfd65 HS |
602 | |
603 | return ERR_PTR(-ENOENT); | |
604 | } | |
605 | ||
96518518 PM |
606 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
607 | { | |
608 | return ++table->hgenerator; | |
609 | } | |
610 | ||
32537e91 | 611 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 612 | |
82603549 PNA |
613 | static const struct nft_chain_type * |
614 | __nft_chain_type_get(u8 family, enum nft_chain_types type) | |
615 | { | |
616 | if (family >= NFPROTO_NUMPROTO || | |
617 | type >= NFT_CHAIN_T_MAX) | |
618 | return NULL; | |
619 | ||
620 | return chain_type[family][type]; | |
621 | } | |
622 | ||
32537e91 | 623 | static const struct nft_chain_type * |
1ea26cca | 624 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c | 625 | { |
82603549 | 626 | const struct nft_chain_type *type; |
9370761c PNA |
627 | int i; |
628 | ||
baae3e62 | 629 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
82603549 PNA |
630 | type = __nft_chain_type_get(family, i); |
631 | if (!type) | |
632 | continue; | |
633 | if (!nla_strcmp(nla, type->name)) | |
634 | return type; | |
9370761c | 635 | } |
baae3e62 | 636 | return NULL; |
9370761c PNA |
637 | } |
638 | ||
eb014de4 PNA |
639 | struct nft_module_request { |
640 | struct list_head list; | |
641 | char module[MODULE_NAME_LEN]; | |
642 | bool done; | |
643 | }; | |
644 | ||
452238e8 | 645 | #ifdef CONFIG_MODULES |
cefa31a9 FW |
646 | __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, |
647 | ...) | |
452238e8 FW |
648 | { |
649 | char module_name[MODULE_NAME_LEN]; | |
0854db2a | 650 | struct nftables_pernet *nft_net; |
eb014de4 | 651 | struct nft_module_request *req; |
452238e8 FW |
652 | va_list args; |
653 | int ret; | |
654 | ||
452238e8 FW |
655 | va_start(args, fmt); |
656 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | |
657 | va_end(args); | |
9332d27d | 658 | if (ret >= MODULE_NAME_LEN) |
eb014de4 | 659 | return 0; |
452238e8 | 660 | |
d59d2f82 | 661 | nft_net = nft_pernet(net); |
0854db2a | 662 | list_for_each_entry(req, &nft_net->module_list, list) { |
eb014de4 PNA |
663 | if (!strcmp(req->module, module_name)) { |
664 | if (req->done) | |
665 | return 0; | |
ec7470b8 | 666 | |
eb014de4 PNA |
667 | /* A request to load this module already exists. */ |
668 | return -EAGAIN; | |
669 | } | |
670 | } | |
671 | ||
672 | req = kmalloc(sizeof(*req), GFP_KERNEL); | |
673 | if (!req) | |
674 | return -ENOMEM; | |
675 | ||
676 | req->done = false; | |
677 | strlcpy(req->module, module_name, MODULE_NAME_LEN); | |
0854db2a | 678 | list_add_tail(&req->list, &nft_net->module_list); |
eb014de4 PNA |
679 | |
680 | return -EAGAIN; | |
452238e8 | 681 | } |
cefa31a9 | 682 | EXPORT_SYMBOL_GPL(nft_request_module); |
452238e8 FW |
683 | #endif |
684 | ||
f102d66b FW |
685 | static void lockdep_nfnl_nft_mutex_not_held(void) |
686 | { | |
687 | #ifdef CONFIG_PROVE_LOCKING | |
c0700dfa FW |
688 | if (debug_locks) |
689 | WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
f102d66b FW |
690 | #endif |
691 | } | |
692 | ||
32537e91 | 693 | static const struct nft_chain_type * |
452238e8 FW |
694 | nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla, |
695 | u8 family, bool autoload) | |
9370761c | 696 | { |
32537e91 | 697 | const struct nft_chain_type *type; |
9370761c | 698 | |
1ea26cca | 699 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
700 | if (type != NULL) |
701 | return type; | |
f102d66b FW |
702 | |
703 | lockdep_nfnl_nft_mutex_not_held(); | |
9370761c | 704 | #ifdef CONFIG_MODULES |
93b0806f | 705 | if (autoload) { |
eb014de4 PNA |
706 | if (nft_request_module(net, "nft-chain-%u-%.*s", family, |
707 | nla_len(nla), | |
708 | (const char *)nla_data(nla)) == -EAGAIN) | |
93b0806f | 709 | return ERR_PTR(-EAGAIN); |
9370761c PNA |
710 | } |
711 | #endif | |
93b0806f | 712 | return ERR_PTR(-ENOENT); |
9370761c PNA |
713 | } |
714 | ||
802b8051 PNA |
715 | static __be16 nft_base_seq(const struct net *net) |
716 | { | |
d59d2f82 | 717 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
718 | |
719 | return htons(nft_net->base_seq & 0xffff); | |
802b8051 PNA |
720 | } |
721 | ||
96518518 | 722 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
723 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
724 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 725 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 726 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
7a81575b JGG |
727 | [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY, |
728 | .len = NFT_USERDATA_MAXLEN } | |
96518518 PM |
729 | }; |
730 | ||
84d7fce6 PNA |
731 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
732 | u32 portid, u32 seq, int event, u32 flags, | |
733 | int family, const struct nft_table *table) | |
96518518 PM |
734 | { |
735 | struct nlmsghdr *nlh; | |
96518518 | 736 | |
dedb67c4 | 737 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
738 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
739 | NFNETLINK_V0, nft_base_seq(net)); | |
740 | if (!nlh) | |
96518518 PM |
741 | goto nla_put_failure; |
742 | ||
9ddf6323 | 743 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
179d9ba5 PNA |
744 | nla_put_be32(skb, NFTA_TABLE_FLAGS, |
745 | htonl(table->flags & NFT_TABLE_F_MASK)) || | |
3ecbfd65 HS |
746 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
747 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
748 | NFTA_TABLE_PAD)) | |
96518518 | 749 | goto nla_put_failure; |
6001a930 PNA |
750 | if (nft_table_has_owner(table) && |
751 | nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid))) | |
752 | goto nla_put_failure; | |
96518518 | 753 | |
7a81575b JGG |
754 | if (table->udata) { |
755 | if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata)) | |
756 | goto nla_put_failure; | |
757 | } | |
758 | ||
053c095a JB |
759 | nlmsg_end(skb, nlh); |
760 | return 0; | |
96518518 PM |
761 | |
762 | nla_put_failure: | |
763 | nlmsg_trim(skb, nlh); | |
764 | return -1; | |
765 | } | |
766 | ||
67cc570e PNA |
767 | struct nftnl_skb_parms { |
768 | bool report; | |
769 | }; | |
770 | #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb)) | |
771 | ||
772 | static void nft_notify_enqueue(struct sk_buff *skb, bool report, | |
773 | struct list_head *notify_list) | |
774 | { | |
775 | NFT_CB(skb).report = report; | |
776 | list_add_tail(&skb->list, notify_list); | |
777 | } | |
778 | ||
25e94a99 | 779 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 | 780 | { |
0854db2a | 781 | struct nftables_pernet *nft_net; |
96518518 | 782 | struct sk_buff *skb; |
6fb721cf | 783 | u16 flags = 0; |
96518518 PM |
784 | int err; |
785 | ||
128ad332 PNA |
786 | if (!ctx->report && |
787 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 788 | return; |
96518518 | 789 | |
96518518 PM |
790 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
791 | if (skb == NULL) | |
792 | goto err; | |
793 | ||
6fb721cf PNA |
794 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
795 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
796 | ||
84d7fce6 | 797 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 798 | event, flags, ctx->family, ctx->table); |
96518518 PM |
799 | if (err < 0) { |
800 | kfree_skb(skb); | |
801 | goto err; | |
802 | } | |
803 | ||
d59d2f82 | 804 | nft_net = nft_pernet(ctx->net); |
0854db2a | 805 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 806 | return; |
96518518 | 807 | err: |
25e94a99 | 808 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
809 | } |
810 | ||
811 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
812 | struct netlink_callback *cb) | |
813 | { | |
814 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
0854db2a | 815 | struct nftables_pernet *nft_net; |
96518518 PM |
816 | const struct nft_table *table; |
817 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 818 | struct net *net = sock_net(skb->sk); |
96518518 PM |
819 | int family = nfmsg->nfgen_family; |
820 | ||
e688a7f8 | 821 | rcu_read_lock(); |
d59d2f82 | 822 | nft_net = nft_pernet(net); |
0854db2a | 823 | cb->seq = nft_net->base_seq; |
38e029f1 | 824 | |
0854db2a | 825 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 826 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
827 | continue; |
828 | ||
36596dad PNA |
829 | if (idx < s_idx) |
830 | goto cont; | |
831 | if (idx > s_idx) | |
832 | memset(&cb->args[1], 0, | |
833 | sizeof(cb->args) - sizeof(cb->args[0])); | |
834 | if (!nft_is_active(net, table)) | |
835 | continue; | |
836 | if (nf_tables_fill_table_info(skb, net, | |
837 | NETLINK_CB(cb->skb).portid, | |
838 | cb->nlh->nlmsg_seq, | |
839 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 840 | table->family, table) < 0) |
36596dad PNA |
841 | goto done; |
842 | ||
843 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 844 | cont: |
36596dad | 845 | idx++; |
96518518 PM |
846 | } |
847 | done: | |
e688a7f8 | 848 | rcu_read_unlock(); |
96518518 PM |
849 | cb->args[0] = idx; |
850 | return skb->len; | |
851 | } | |
852 | ||
d9adf22a FW |
853 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
854 | const struct nlmsghdr *nlh, | |
855 | struct netlink_dump_control *c) | |
856 | { | |
857 | int err; | |
858 | ||
859 | if (!try_module_get(THIS_MODULE)) | |
860 | return -EINVAL; | |
861 | ||
862 | rcu_read_unlock(); | |
863 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
864 | rcu_read_lock(); | |
865 | module_put(THIS_MODULE); | |
866 | ||
867 | return err; | |
868 | } | |
869 | ||
870 | /* called with rcu_read_lock held */ | |
797d4980 PNA |
871 | static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info, |
872 | const struct nlattr * const nla[]) | |
96518518 | 873 | { |
797d4980 PNA |
874 | struct netlink_ext_ack *extack = info->extack; |
875 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 876 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 877 | const struct nft_table *table; |
797d4980 | 878 | struct net *net = info->net; |
96518518 | 879 | struct sk_buff *skb2; |
96518518 PM |
880 | int err; |
881 | ||
797d4980 | 882 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
883 | struct netlink_dump_control c = { |
884 | .dump = nf_tables_dump_tables, | |
d9adf22a | 885 | .module = THIS_MODULE, |
96518518 | 886 | }; |
d9adf22a | 887 | |
797d4980 | 888 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
889 | } |
890 | ||
6001a930 | 891 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0); |
36dd1bcc PNA |
892 | if (IS_ERR(table)) { |
893 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 894 | return PTR_ERR(table); |
36dd1bcc | 895 | } |
96518518 | 896 | |
d9adf22a | 897 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
898 | if (!skb2) |
899 | return -ENOMEM; | |
900 | ||
84d7fce6 | 901 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
902 | info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE, |
903 | 0, family, table); | |
96518518 | 904 | if (err < 0) |
ee921183 | 905 | goto err_fill_table_info; |
96518518 | 906 | |
ee921183 | 907 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 908 | |
ee921183 | 909 | err_fill_table_info: |
96518518 PM |
910 | kfree_skb(skb2); |
911 | return err; | |
912 | } | |
913 | ||
c9c17211 | 914 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
915 | { |
916 | struct nft_chain *chain; | |
917 | u32 i = 0; | |
918 | ||
919 | list_for_each_entry(chain, &table->chains, list) { | |
920 | if (!nft_is_active_next(net, chain)) | |
921 | continue; | |
f323d954 | 922 | if (!nft_is_base_chain(chain)) |
10435c11 F |
923 | continue; |
924 | ||
925 | if (cnt && i++ == cnt) | |
926 | break; | |
927 | ||
1e9451cb | 928 | nf_tables_unregister_hook(net, table, chain); |
10435c11 F |
929 | } |
930 | } | |
931 | ||
c9c17211 | 932 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
933 | { |
934 | struct nft_chain *chain; | |
935 | int err, i = 0; | |
936 | ||
937 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
938 | if (!nft_is_active_next(net, chain)) |
939 | continue; | |
f323d954 | 940 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
941 | continue; |
942 | ||
1e9451cb | 943 | err = nf_tables_register_hook(net, table, chain); |
9ddf6323 | 944 | if (err < 0) |
d54725cd | 945 | goto err_register_hooks; |
9ddf6323 PNA |
946 | |
947 | i++; | |
948 | } | |
949 | return 0; | |
d54725cd PNA |
950 | |
951 | err_register_hooks: | |
10435c11 | 952 | if (i) |
c9c17211 | 953 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
954 | return err; |
955 | } | |
956 | ||
c9c17211 | 957 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 958 | { |
179d9ba5 | 959 | table->flags &= ~NFT_TABLE_F_DORMANT; |
c9c17211 | 960 | nft_table_disable(net, table, 0); |
179d9ba5 | 961 | table->flags |= NFT_TABLE_F_DORMANT; |
9ddf6323 PNA |
962 | } |
963 | ||
179d9ba5 PNA |
964 | #define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1) |
965 | #define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0) | |
966 | #define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1) | |
967 | #define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \ | |
968 | __NFT_TABLE_F_WAS_AWAKEN) | |
0ce7cf41 | 969 | |
e1aaca93 | 970 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 971 | { |
55dd6f93 | 972 | struct nft_trans *trans; |
e1aaca93 | 973 | u32 flags; |
179d9ba5 | 974 | int ret; |
9ddf6323 | 975 | |
e1aaca93 PNA |
976 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
977 | return 0; | |
9ddf6323 | 978 | |
e1aaca93 | 979 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
6001a930 PNA |
980 | if (flags & ~NFT_TABLE_F_MASK) |
981 | return -EOPNOTSUPP; | |
e1aaca93 | 982 | |
63283dd2 PNA |
983 | if (flags == ctx->table->flags) |
984 | return 0; | |
985 | ||
9cc0001a PNA |
986 | if ((nft_table_has_owner(ctx->table) && |
987 | !(flags & NFT_TABLE_F_OWNER)) || | |
988 | (!nft_table_has_owner(ctx->table) && | |
989 | flags & NFT_TABLE_F_OWNER)) | |
990 | return -EOPNOTSUPP; | |
991 | ||
55dd6f93 PNA |
992 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
993 | sizeof(struct nft_trans_table)); | |
994 | if (trans == NULL) | |
995 | return -ENOMEM; | |
9ddf6323 | 996 | |
e1aaca93 PNA |
997 | if ((flags & NFT_TABLE_F_DORMANT) && |
998 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
179d9ba5 PNA |
999 | ctx->table->flags |= NFT_TABLE_F_DORMANT; |
1000 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) | |
1001 | ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN; | |
e1aaca93 PNA |
1002 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
1003 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
179d9ba5 PNA |
1004 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
1005 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) { | |
1006 | ret = nf_tables_table_enable(ctx->net, ctx->table); | |
1007 | if (ret < 0) | |
1008 | goto err_register_hooks; | |
1009 | ||
1010 | ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT; | |
1011 | } | |
9ddf6323 PNA |
1012 | } |
1013 | ||
55dd6f93 | 1014 | nft_trans_table_update(trans) = true; |
0854db2a | 1015 | nft_trans_commit_list_add_tail(ctx->net, trans); |
179d9ba5 | 1016 | |
55dd6f93 | 1017 | return 0; |
179d9ba5 PNA |
1018 | |
1019 | err_register_hooks: | |
55dd6f93 | 1020 | nft_trans_destroy(trans); |
9ddf6323 PNA |
1021 | return ret; |
1022 | } | |
1023 | ||
1b2470e5 FW |
1024 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
1025 | { | |
1026 | const char *name = data; | |
1027 | ||
1028 | return jhash(name, strlen(name), seed); | |
1029 | } | |
1030 | ||
1031 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
1032 | { | |
1033 | const struct nft_chain *chain = data; | |
1034 | ||
1035 | return nft_chain_hash(chain->name, 0, seed); | |
1036 | } | |
1037 | ||
1038 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
1039 | const void *ptr) | |
1040 | { | |
1041 | const struct nft_chain *chain = ptr; | |
1042 | const char *name = arg->key; | |
1043 | ||
1044 | return strcmp(chain->name, name); | |
1045 | } | |
1046 | ||
4d44175a FW |
1047 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed) |
1048 | { | |
1049 | const struct nft_object_hash_key *k = data; | |
1050 | ||
1051 | seed ^= hash_ptr(k->table, 32); | |
1052 | ||
1053 | return jhash(k->name, strlen(k->name), seed); | |
1054 | } | |
1055 | ||
1056 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed) | |
1057 | { | |
1058 | const struct nft_object *obj = data; | |
1059 | ||
1060 | return nft_objname_hash(&obj->key, 0, seed); | |
1061 | } | |
1062 | ||
1063 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg, | |
1064 | const void *ptr) | |
1065 | { | |
1066 | const struct nft_object_hash_key *k = arg->key; | |
1067 | const struct nft_object *obj = ptr; | |
1068 | ||
1069 | if (obj->key.table != k->table) | |
1070 | return -1; | |
1071 | ||
1072 | return strcmp(obj->key.name, k->name); | |
1073 | } | |
1074 | ||
7dab8ee3 PNA |
1075 | static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, |
1076 | const struct nlattr * const nla[]) | |
96518518 | 1077 | { |
7dab8ee3 | 1078 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
1079 | struct netlink_ext_ack *extack = info->extack; |
1080 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1081 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1082 | struct net *net = info->net; |
36dd1bcc PNA |
1083 | const struct nlattr *attr; |
1084 | struct nft_table *table; | |
e1aaca93 | 1085 | struct nft_ctx ctx; |
7a81575b | 1086 | u32 flags = 0; |
55dd6f93 | 1087 | int err; |
96518518 | 1088 | |
0854db2a | 1089 | lockdep_assert_held(&nft_net->commit_mutex); |
36dd1bcc | 1090 | attr = nla[NFTA_TABLE_NAME]; |
6001a930 PNA |
1091 | table = nft_table_lookup(net, attr, family, genmask, |
1092 | NETLINK_CB(skb).portid); | |
96518518 PM |
1093 | if (IS_ERR(table)) { |
1094 | if (PTR_ERR(table) != -ENOENT) | |
1095 | return PTR_ERR(table); | |
1a28ad74 | 1096 | } else { |
7dab8ee3 | 1097 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 1098 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1099 | return -EEXIST; |
36dd1bcc | 1100 | } |
7dab8ee3 | 1101 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 | 1102 | return -EOPNOTSUPP; |
e1aaca93 | 1103 | |
7dab8ee3 PNA |
1104 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
1105 | ||
e1aaca93 | 1106 | return nf_tables_updtable(&ctx); |
96518518 PM |
1107 | } |
1108 | ||
c5c1f975 PM |
1109 | if (nla[NFTA_TABLE_FLAGS]) { |
1110 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
6001a930 PNA |
1111 | if (flags & ~NFT_TABLE_F_MASK) |
1112 | return -EOPNOTSUPP; | |
c5c1f975 PM |
1113 | } |
1114 | ||
ffdb210e | 1115 | err = -ENOMEM; |
1cae565e | 1116 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
ffdb210e | 1117 | if (table == NULL) |
98319cb9 | 1118 | goto err_kzalloc; |
96518518 | 1119 | |
36dd1bcc | 1120 | table->name = nla_strdup(attr, GFP_KERNEL); |
e46abbcc | 1121 | if (table->name == NULL) |
98319cb9 | 1122 | goto err_strdup; |
e46abbcc | 1123 | |
7a81575b | 1124 | if (nla[NFTA_TABLE_USERDATA]) { |
85db827a | 1125 | table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL); |
7a81575b JGG |
1126 | if (table->udata == NULL) |
1127 | goto err_table_udata; | |
1128 | ||
85db827a | 1129 | table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]); |
7a81575b JGG |
1130 | } |
1131 | ||
1b2470e5 FW |
1132 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
1133 | if (err) | |
1134 | goto err_chain_ht; | |
1135 | ||
96518518 | 1136 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 1137 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 1138 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 1139 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 1140 | table->family = family; |
c5c1f975 | 1141 | table->flags = flags; |
3ecbfd65 | 1142 | table->handle = ++table_handle; |
6001a930 PNA |
1143 | if (table->flags & NFT_TABLE_F_OWNER) |
1144 | table->nlpid = NETLINK_CB(skb).portid; | |
9ddf6323 | 1145 | |
7dab8ee3 | 1146 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
55dd6f93 | 1147 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 1148 | if (err < 0) |
98319cb9 | 1149 | goto err_trans; |
ffdb210e | 1150 | |
0854db2a | 1151 | list_add_tail_rcu(&table->list, &nft_net->tables); |
96518518 | 1152 | return 0; |
98319cb9 | 1153 | err_trans: |
1b2470e5 FW |
1154 | rhltable_destroy(&table->chains_ht); |
1155 | err_chain_ht: | |
7a81575b JGG |
1156 | kfree(table->udata); |
1157 | err_table_udata: | |
e46abbcc | 1158 | kfree(table->name); |
98319cb9 | 1159 | err_strdup: |
ffdb210e | 1160 | kfree(table); |
98319cb9 | 1161 | err_kzalloc: |
ffdb210e | 1162 | return err; |
96518518 PM |
1163 | } |
1164 | ||
b9ac12ef AB |
1165 | static int nft_flush_table(struct nft_ctx *ctx) |
1166 | { | |
3b49e2e9 | 1167 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 1168 | struct nft_chain *chain, *nc; |
e5009240 | 1169 | struct nft_object *obj, *ne; |
b9ac12ef | 1170 | struct nft_set *set, *ns; |
3b49e2e9 | 1171 | int err; |
b9ac12ef | 1172 | |
a2f18db0 | 1173 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
1174 | if (!nft_is_active_next(ctx->net, chain)) |
1175 | continue; | |
1176 | ||
d0e2c7de PNA |
1177 | if (nft_chain_is_bound(chain)) |
1178 | continue; | |
1179 | ||
b9ac12ef AB |
1180 | ctx->chain = chain; |
1181 | ||
1182 | err = nft_delrule_by_chain(ctx); | |
1183 | if (err < 0) | |
1184 | goto out; | |
b9ac12ef AB |
1185 | } |
1186 | ||
1187 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
1188 | if (!nft_is_active_next(ctx->net, set)) |
1189 | continue; | |
1190 | ||
408070d6 | 1191 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
1192 | !list_empty(&set->bindings)) |
1193 | continue; | |
1194 | ||
1195 | err = nft_delset(ctx, set); | |
1196 | if (err < 0) | |
1197 | goto out; | |
1198 | } | |
1199 | ||
3b49e2e9 | 1200 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
335178d5 FW |
1201 | if (!nft_is_active_next(ctx->net, flowtable)) |
1202 | continue; | |
1203 | ||
3b49e2e9 PNA |
1204 | err = nft_delflowtable(ctx, flowtable); |
1205 | if (err < 0) | |
1206 | goto out; | |
1207 | } | |
1208 | ||
e5009240 | 1209 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
335178d5 FW |
1210 | if (!nft_is_active_next(ctx->net, obj)) |
1211 | continue; | |
1212 | ||
e5009240 PNA |
1213 | err = nft_delobj(ctx, obj); |
1214 | if (err < 0) | |
1215 | goto out; | |
1216 | } | |
1217 | ||
a2f18db0 | 1218 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
1219 | if (!nft_is_active_next(ctx->net, chain)) |
1220 | continue; | |
1221 | ||
d0e2c7de PNA |
1222 | if (nft_chain_is_bound(chain)) |
1223 | continue; | |
1224 | ||
a2f18db0 PNA |
1225 | ctx->chain = chain; |
1226 | ||
1227 | err = nft_delchain(ctx); | |
1228 | if (err < 0) | |
1229 | goto out; | |
1230 | } | |
1231 | ||
b9ac12ef AB |
1232 | err = nft_deltable(ctx); |
1233 | out: | |
1234 | return err; | |
1235 | } | |
1236 | ||
1237 | static int nft_flush(struct nft_ctx *ctx, int family) | |
1238 | { | |
d59d2f82 | 1239 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
b9ac12ef | 1240 | const struct nlattr * const *nla = ctx->nla; |
d59d2f82 | 1241 | struct nft_table *table, *nt; |
b9ac12ef AB |
1242 | int err = 0; |
1243 | ||
0854db2a | 1244 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
98319cb9 | 1245 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
1246 | continue; |
1247 | ||
98319cb9 | 1248 | ctx->family = table->family; |
f2a6d766 | 1249 | |
36596dad PNA |
1250 | if (!nft_is_active_next(ctx->net, table)) |
1251 | continue; | |
b9ac12ef | 1252 | |
6001a930 PNA |
1253 | if (nft_table_has_owner(table) && table->nlpid != ctx->portid) |
1254 | continue; | |
1255 | ||
36596dad PNA |
1256 | if (nla[NFTA_TABLE_NAME] && |
1257 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
1258 | continue; | |
b9ac12ef | 1259 | |
36596dad PNA |
1260 | ctx->table = table; |
1261 | ||
1262 | err = nft_flush_table(ctx); | |
1263 | if (err < 0) | |
1264 | goto out; | |
b9ac12ef AB |
1265 | } |
1266 | out: | |
1267 | return err; | |
1268 | } | |
1269 | ||
7dab8ee3 PNA |
1270 | static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info, |
1271 | const struct nlattr * const nla[]) | |
96518518 | 1272 | { |
7dab8ee3 PNA |
1273 | struct netlink_ext_ack *extack = info->extack; |
1274 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1275 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1276 | struct net *net = info->net; |
36dd1bcc PNA |
1277 | const struct nlattr *attr; |
1278 | struct nft_table *table; | |
55dd6f93 | 1279 | struct nft_ctx ctx; |
96518518 | 1280 | |
7dab8ee3 | 1281 | nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
1282 | if (family == AF_UNSPEC || |
1283 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
1284 | return nft_flush(&ctx, family); |
1285 | ||
36dd1bcc PNA |
1286 | if (nla[NFTA_TABLE_HANDLE]) { |
1287 | attr = nla[NFTA_TABLE_HANDLE]; | |
e31f072f PNA |
1288 | table = nft_table_lookup_byhandle(net, attr, genmask, |
1289 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
1290 | } else { |
1291 | attr = nla[NFTA_TABLE_NAME]; | |
6001a930 PNA |
1292 | table = nft_table_lookup(net, attr, family, genmask, |
1293 | NETLINK_CB(skb).portid); | |
36dd1bcc | 1294 | } |
3ecbfd65 | 1295 | |
36dd1bcc PNA |
1296 | if (IS_ERR(table)) { |
1297 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 1298 | return PTR_ERR(table); |
36dd1bcc | 1299 | } |
96518518 | 1300 | |
7dab8ee3 | 1301 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
a8278400 PNA |
1302 | table->use > 0) |
1303 | return -EBUSY; | |
1304 | ||
98319cb9 | 1305 | ctx.family = family; |
b9ac12ef | 1306 | ctx.table = table; |
55dd6f93 | 1307 | |
b9ac12ef | 1308 | return nft_flush_table(&ctx); |
96518518 PM |
1309 | } |
1310 | ||
55dd6f93 PNA |
1311 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
1312 | { | |
fa5950e4 FW |
1313 | if (WARN_ON(ctx->table->use > 0)) |
1314 | return; | |
4fefee57 | 1315 | |
1b2470e5 | 1316 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 1317 | kfree(ctx->table->name); |
bc7a7082 | 1318 | kfree(ctx->table->udata); |
55dd6f93 | 1319 | kfree(ctx->table); |
55dd6f93 PNA |
1320 | } |
1321 | ||
cc07eeb0 | 1322 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1323 | { |
96518518 | 1324 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
82603549 | 1325 | if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) { |
cc07eeb0 PNA |
1326 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1327 | return; | |
96518518 | 1328 | } |
9370761c | 1329 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 1330 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 1331 | } |
9370761c | 1332 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 1333 | |
32537e91 | 1334 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1335 | { |
96518518 | 1336 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 1337 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
1338 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1339 | } | |
9370761c | 1340 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
1341 | |
1342 | /* | |
1343 | * Chains | |
1344 | */ | |
1345 | ||
1346 | static struct nft_chain * | |
cac20fcd | 1347 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1348 | { |
1349 | struct nft_chain *chain; | |
1350 | ||
1351 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1352 | if (chain->handle == handle && |
1353 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1354 | return chain; |
1355 | } | |
1356 | ||
1357 | return ERR_PTR(-ENOENT); | |
1358 | } | |
1359 | ||
4d44175a | 1360 | static bool lockdep_commit_lock_is_held(const struct net *net) |
f102d66b FW |
1361 | { |
1362 | #ifdef CONFIG_PROVE_LOCKING | |
d59d2f82 | 1363 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
1364 | |
1365 | return lockdep_is_held(&nft_net->commit_mutex); | |
f102d66b FW |
1366 | #else |
1367 | return true; | |
1368 | #endif | |
1369 | } | |
1370 | ||
1371 | static struct nft_chain *nft_chain_lookup(struct net *net, | |
1372 | struct nft_table *table, | |
cac20fcd | 1373 | const struct nlattr *nla, u8 genmask) |
96518518 | 1374 | { |
1b2470e5 FW |
1375 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1376 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1377 | struct nft_chain *chain; |
1378 | ||
1379 | if (nla == NULL) | |
1380 | return ERR_PTR(-EINVAL); | |
1381 | ||
872f6903 | 1382 | nla_strscpy(search, nla, sizeof(search)); |
96518518 | 1383 | |
1b2470e5 | 1384 | WARN_ON(!rcu_read_lock_held() && |
f102d66b | 1385 | !lockdep_commit_lock_is_held(net)); |
1b2470e5 FW |
1386 | |
1387 | chain = ERR_PTR(-ENOENT); | |
1388 | rcu_read_lock(); | |
1389 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1390 | if (!list) | |
1391 | goto out_unlock; | |
1392 | ||
1393 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1394 | if (nft_active_genmask(chain, genmask)) | |
1395 | goto out_unlock; | |
1396 | } | |
1397 | chain = ERR_PTR(-ENOENT); | |
1398 | out_unlock: | |
1399 | rcu_read_unlock(); | |
1400 | return chain; | |
96518518 PM |
1401 | } |
1402 | ||
1403 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1404 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1405 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1406 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1407 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1408 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1409 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1410 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
9332d27d FW |
1411 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, |
1412 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
0ca743a5 | 1413 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
c9626a2c | 1414 | [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, |
74cccc3d | 1415 | [NFTA_CHAIN_ID] = { .type = NLA_U32 }, |
002f2176 JGG |
1416 | [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY, |
1417 | .len = NFT_USERDATA_MAXLEN }, | |
96518518 PM |
1418 | }; |
1419 | ||
1420 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1421 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1422 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1423 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1424 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1425 | }; |
1426 | ||
0ca743a5 PNA |
1427 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1428 | { | |
1429 | struct nft_stats *cpu_stats, total; | |
1430 | struct nlattr *nest; | |
ce355e20 ED |
1431 | unsigned int seq; |
1432 | u64 pkts, bytes; | |
0ca743a5 PNA |
1433 | int cpu; |
1434 | ||
edbd82c5 FW |
1435 | if (!stats) |
1436 | return 0; | |
1437 | ||
0ca743a5 PNA |
1438 | memset(&total, 0, sizeof(total)); |
1439 | for_each_possible_cpu(cpu) { | |
1440 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 ED |
1441 | do { |
1442 | seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); | |
1443 | pkts = cpu_stats->pkts; | |
1444 | bytes = cpu_stats->bytes; | |
1445 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); | |
1446 | total.pkts += pkts; | |
1447 | total.bytes += bytes; | |
0ca743a5 | 1448 | } |
ae0be8de | 1449 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS); |
0ca743a5 PNA |
1450 | if (nest == NULL) |
1451 | goto nla_put_failure; | |
1452 | ||
b46f6ded ND |
1453 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
1454 | NFTA_COUNTER_PAD) || | |
1455 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
1456 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
1457 | goto nla_put_failure; |
1458 | ||
1459 | nla_nest_end(skb, nest); | |
1460 | return 0; | |
1461 | ||
1462 | nla_put_failure: | |
1463 | return -ENOSPC; | |
1464 | } | |
1465 | ||
d54725cd PNA |
1466 | static int nft_dump_basechain_hook(struct sk_buff *skb, int family, |
1467 | const struct nft_base_chain *basechain) | |
1468 | { | |
1469 | const struct nf_hook_ops *ops = &basechain->ops; | |
1470 | struct nft_hook *hook, *first = NULL; | |
1471 | struct nlattr *nest, *nest_devs; | |
1472 | int n = 0; | |
1473 | ||
1474 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK); | |
1475 | if (nest == NULL) | |
1476 | goto nla_put_failure; | |
1477 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1478 | goto nla_put_failure; | |
1479 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1480 | goto nla_put_failure; | |
1481 | ||
d3519cb8 | 1482 | if (nft_base_chain_netdev(family, ops->hooknum)) { |
d54725cd PNA |
1483 | nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS); |
1484 | list_for_each_entry(hook, &basechain->hook_list, list) { | |
1485 | if (!first) | |
1486 | first = hook; | |
1487 | ||
1488 | if (nla_put_string(skb, NFTA_DEVICE_NAME, | |
1489 | hook->ops.dev->name)) | |
1490 | goto nla_put_failure; | |
1491 | n++; | |
1492 | } | |
1493 | nla_nest_end(skb, nest_devs); | |
1494 | ||
1495 | if (n == 1 && | |
1496 | nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name)) | |
1497 | goto nla_put_failure; | |
1498 | } | |
1499 | nla_nest_end(skb, nest); | |
1500 | ||
1501 | return 0; | |
1502 | nla_put_failure: | |
1503 | return -1; | |
1504 | } | |
1505 | ||
84d7fce6 PNA |
1506 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
1507 | u32 portid, u32 seq, int event, u32 flags, | |
1508 | int family, const struct nft_table *table, | |
96518518 PM |
1509 | const struct nft_chain *chain) |
1510 | { | |
1511 | struct nlmsghdr *nlh; | |
96518518 | 1512 | |
dedb67c4 | 1513 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
1514 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
1515 | NFNETLINK_V0, nft_base_seq(net)); | |
1516 | if (!nlh) | |
96518518 PM |
1517 | goto nla_put_failure; |
1518 | ||
96518518 PM |
1519 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) |
1520 | goto nla_put_failure; | |
b46f6ded ND |
1521 | if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), |
1522 | NFTA_CHAIN_PAD)) | |
96518518 PM |
1523 | goto nla_put_failure; |
1524 | if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) | |
1525 | goto nla_put_failure; | |
1526 | ||
f323d954 | 1527 | if (nft_is_base_chain(chain)) { |
0ca743a5 | 1528 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
edbd82c5 | 1529 | struct nft_stats __percpu *stats; |
0ca743a5 | 1530 | |
d54725cd | 1531 | if (nft_dump_basechain_hook(skb, family, basechain)) |
2cbce139 | 1532 | goto nla_put_failure; |
9370761c | 1533 | |
0ca743a5 PNA |
1534 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1535 | htonl(basechain->policy))) | |
1536 | goto nla_put_failure; | |
1537 | ||
baae3e62 PM |
1538 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1539 | goto nla_put_failure; | |
0ca743a5 | 1540 | |
edbd82c5 FW |
1541 | stats = rcu_dereference_check(basechain->stats, |
1542 | lockdep_commit_lock_is_held(net)); | |
1543 | if (nft_dump_stats(skb, stats)) | |
0ca743a5 | 1544 | goto nla_put_failure; |
96518518 PM |
1545 | } |
1546 | ||
d0e2c7de PNA |
1547 | if (chain->flags && |
1548 | nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags))) | |
1549 | goto nla_put_failure; | |
1550 | ||
0ca743a5 PNA |
1551 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1552 | goto nla_put_failure; | |
1553 | ||
002f2176 JGG |
1554 | if (chain->udata && |
1555 | nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata)) | |
1556 | goto nla_put_failure; | |
1557 | ||
053c095a JB |
1558 | nlmsg_end(skb, nlh); |
1559 | return 0; | |
96518518 PM |
1560 | |
1561 | nla_put_failure: | |
1562 | nlmsg_trim(skb, nlh); | |
1563 | return -1; | |
1564 | } | |
1565 | ||
25e94a99 | 1566 | static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
96518518 | 1567 | { |
0854db2a | 1568 | struct nftables_pernet *nft_net; |
96518518 | 1569 | struct sk_buff *skb; |
6fb721cf | 1570 | u16 flags = 0; |
96518518 PM |
1571 | int err; |
1572 | ||
128ad332 PNA |
1573 | if (!ctx->report && |
1574 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1575 | return; |
96518518 | 1576 | |
96518518 PM |
1577 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1578 | if (skb == NULL) | |
1579 | goto err; | |
1580 | ||
6fb721cf PNA |
1581 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
1582 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
1583 | ||
84d7fce6 | 1584 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 1585 | event, flags, ctx->family, ctx->table, |
35151d84 | 1586 | ctx->chain); |
96518518 PM |
1587 | if (err < 0) { |
1588 | kfree_skb(skb); | |
1589 | goto err; | |
1590 | } | |
1591 | ||
d59d2f82 | 1592 | nft_net = nft_pernet(ctx->net); |
0854db2a | 1593 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 1594 | return; |
96518518 | 1595 | err: |
25e94a99 | 1596 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1597 | } |
1598 | ||
1599 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1600 | struct netlink_callback *cb) | |
1601 | { | |
1602 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 | 1603 | unsigned int idx = 0, s_idx = cb->args[0]; |
99633ab2 | 1604 | struct net *net = sock_net(skb->sk); |
96518518 | 1605 | int family = nfmsg->nfgen_family; |
0854db2a | 1606 | struct nftables_pernet *nft_net; |
d59d2f82 PNA |
1607 | const struct nft_table *table; |
1608 | const struct nft_chain *chain; | |
96518518 | 1609 | |
e688a7f8 | 1610 | rcu_read_lock(); |
d59d2f82 | 1611 | nft_net = nft_pernet(net); |
0854db2a | 1612 | cb->seq = nft_net->base_seq; |
38e029f1 | 1613 | |
0854db2a | 1614 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 1615 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1616 | continue; |
1617 | ||
36596dad PNA |
1618 | list_for_each_entry_rcu(chain, &table->chains, list) { |
1619 | if (idx < s_idx) | |
1620 | goto cont; | |
1621 | if (idx > s_idx) | |
1622 | memset(&cb->args[1], 0, | |
1623 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1624 | if (!nft_is_active(net, chain)) | |
1625 | continue; | |
1626 | if (nf_tables_fill_chain_info(skb, net, | |
1627 | NETLINK_CB(cb->skb).portid, | |
1628 | cb->nlh->nlmsg_seq, | |
1629 | NFT_MSG_NEWCHAIN, | |
1630 | NLM_F_MULTI, | |
98319cb9 | 1631 | table->family, table, |
36596dad PNA |
1632 | chain) < 0) |
1633 | goto done; | |
38e029f1 | 1634 | |
36596dad | 1635 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
96518518 | 1636 | cont: |
36596dad | 1637 | idx++; |
96518518 PM |
1638 | } |
1639 | } | |
1640 | done: | |
e688a7f8 | 1641 | rcu_read_unlock(); |
96518518 PM |
1642 | cb->args[0] = idx; |
1643 | return skb->len; | |
1644 | } | |
1645 | ||
d9adf22a | 1646 | /* called with rcu_read_lock held */ |
797d4980 PNA |
1647 | static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info, |
1648 | const struct nlattr * const nla[]) | |
96518518 | 1649 | { |
797d4980 PNA |
1650 | struct netlink_ext_ack *extack = info->extack; |
1651 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 1652 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 1653 | const struct nft_chain *chain; |
797d4980 | 1654 | struct net *net = info->net; |
1b2470e5 | 1655 | struct nft_table *table; |
96518518 | 1656 | struct sk_buff *skb2; |
96518518 PM |
1657 | int err; |
1658 | ||
797d4980 | 1659 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
1660 | struct netlink_dump_control c = { |
1661 | .dump = nf_tables_dump_chains, | |
d9adf22a | 1662 | .module = THIS_MODULE, |
96518518 | 1663 | }; |
d9adf22a | 1664 | |
797d4980 | 1665 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
1666 | } |
1667 | ||
6001a930 | 1668 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0); |
36dd1bcc PNA |
1669 | if (IS_ERR(table)) { |
1670 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1671 | return PTR_ERR(table); |
36dd1bcc | 1672 | } |
96518518 | 1673 | |
f102d66b | 1674 | chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask); |
36dd1bcc PNA |
1675 | if (IS_ERR(chain)) { |
1676 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
96518518 | 1677 | return PTR_ERR(chain); |
36dd1bcc | 1678 | } |
96518518 | 1679 | |
d9adf22a | 1680 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1681 | if (!skb2) |
1682 | return -ENOMEM; | |
1683 | ||
84d7fce6 | 1684 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
1685 | info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, |
1686 | 0, family, table, chain); | |
96518518 | 1687 | if (err < 0) |
ee921183 | 1688 | goto err_fill_chain_info; |
96518518 | 1689 | |
ee921183 | 1690 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 1691 | |
ee921183 | 1692 | err_fill_chain_info: |
96518518 PM |
1693 | kfree_skb(skb2); |
1694 | return err; | |
1695 | } | |
1696 | ||
0ca743a5 PNA |
1697 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1698 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1699 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1700 | }; | |
1701 | ||
ff3cd7b3 | 1702 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1703 | { |
1704 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1705 | struct nft_stats __percpu *newstats; | |
1706 | struct nft_stats *stats; | |
1707 | int err; | |
1708 | ||
8cb08174 JB |
1709 | err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr, |
1710 | nft_counter_policy, NULL); | |
0ca743a5 | 1711 | if (err < 0) |
ff3cd7b3 | 1712 | return ERR_PTR(err); |
0ca743a5 PNA |
1713 | |
1714 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1715 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1716 | |
ce355e20 | 1717 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1718 | if (newstats == NULL) |
ff3cd7b3 | 1719 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1720 | |
1721 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1722 | * are not exposed to userspace. | |
1723 | */ | |
e8781f70 | 1724 | preempt_disable(); |
0ca743a5 PNA |
1725 | stats = this_cpu_ptr(newstats); |
1726 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
1727 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 1728 | preempt_enable(); |
0ca743a5 | 1729 | |
ff3cd7b3 PNA |
1730 | return newstats; |
1731 | } | |
1732 | ||
53315ac6 | 1733 | static void nft_chain_stats_replace(struct nft_trans *trans) |
ff3cd7b3 | 1734 | { |
53315ac6 | 1735 | struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain); |
0befd061 | 1736 | |
53315ac6 | 1737 | if (!nft_trans_chain_stats(trans)) |
b88825de PNA |
1738 | return; |
1739 | ||
b685b534 PM |
1740 | nft_trans_chain_stats(trans) = |
1741 | rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans), | |
1742 | lockdep_commit_lock_is_held(trans->ctx.net)); | |
53315ac6 FW |
1743 | |
1744 | if (!nft_trans_chain_stats(trans)) | |
bbb8c61f | 1745 | static_branch_inc(&nft_counters_enabled); |
0ca743a5 PNA |
1746 | } |
1747 | ||
0cbc06b3 FW |
1748 | static void nf_tables_chain_free_chain_rules(struct nft_chain *chain) |
1749 | { | |
2c865a8a PNA |
1750 | struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0); |
1751 | struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1); | |
0cbc06b3 FW |
1752 | |
1753 | if (g0 != g1) | |
1754 | kvfree(g1); | |
1755 | kvfree(g0); | |
1756 | ||
1757 | /* should be NULL either via abort or via successful commit */ | |
2c865a8a PNA |
1758 | WARN_ON_ONCE(chain->blob_next); |
1759 | kvfree(chain->blob_next); | |
0cbc06b3 FW |
1760 | } |
1761 | ||
d0e2c7de | 1762 | void nf_tables_chain_destroy(struct nft_ctx *ctx) |
91c7b38d | 1763 | { |
43a605f2 | 1764 | struct nft_chain *chain = ctx->chain; |
d54725cd | 1765 | struct nft_hook *hook, *next; |
43a605f2 | 1766 | |
fa5950e4 FW |
1767 | if (WARN_ON(chain->use > 0)) |
1768 | return; | |
91c7b38d | 1769 | |
0cbc06b3 FW |
1770 | /* no concurrent access possible anymore */ |
1771 | nf_tables_chain_free_chain_rules(chain); | |
1772 | ||
f323d954 | 1773 | if (nft_is_base_chain(chain)) { |
2cbce139 PNA |
1774 | struct nft_base_chain *basechain = nft_base_chain(chain); |
1775 | ||
d3519cb8 | 1776 | if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) { |
d54725cd PNA |
1777 | list_for_each_entry_safe(hook, next, |
1778 | &basechain->hook_list, list) { | |
1779 | list_del_rcu(&hook->list); | |
1780 | kfree_rcu(hook, rcu); | |
1781 | } | |
1782 | } | |
2cbce139 | 1783 | module_put(basechain->type->owner); |
4c05ec47 | 1784 | if (rcu_access_pointer(basechain->stats)) { |
9f08ea84 | 1785 | static_branch_dec(&nft_counters_enabled); |
4c05ec47 TY |
1786 | free_percpu(rcu_dereference_raw(basechain->stats)); |
1787 | } | |
b7263e07 | 1788 | kfree(chain->name); |
002f2176 | 1789 | kfree(chain->udata); |
2cbce139 | 1790 | kfree(basechain); |
91c7b38d | 1791 | } else { |
b7263e07 | 1792 | kfree(chain->name); |
002f2176 | 1793 | kfree(chain->udata); |
91c7b38d PNA |
1794 | kfree(chain); |
1795 | } | |
1796 | } | |
1797 | ||
3f0465a9 PNA |
1798 | static struct nft_hook *nft_netdev_hook_alloc(struct net *net, |
1799 | const struct nlattr *attr) | |
1800 | { | |
1801 | struct net_device *dev; | |
1802 | char ifname[IFNAMSIZ]; | |
1803 | struct nft_hook *hook; | |
1804 | int err; | |
1805 | ||
1806 | hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL); | |
1807 | if (!hook) { | |
1808 | err = -ENOMEM; | |
1809 | goto err_hook_alloc; | |
1810 | } | |
1811 | ||
872f6903 | 1812 | nla_strscpy(ifname, attr, IFNAMSIZ); |
42f1c271 PNA |
1813 | /* nf_tables_netdev_event() is called under rtnl_mutex, this is |
1814 | * indirectly serializing all the other holders of the commit_mutex with | |
1815 | * the rtnl_mutex. | |
1816 | */ | |
3f0465a9 PNA |
1817 | dev = __dev_get_by_name(net, ifname); |
1818 | if (!dev) { | |
1819 | err = -ENOENT; | |
1820 | goto err_hook_dev; | |
1821 | } | |
1822 | hook->ops.dev = dev; | |
abadb2f8 | 1823 | hook->inactive = false; |
3f0465a9 PNA |
1824 | |
1825 | return hook; | |
1826 | ||
1827 | err_hook_dev: | |
1828 | kfree(hook); | |
1829 | err_hook_alloc: | |
1830 | return ERR_PTR(err); | |
1831 | } | |
1832 | ||
abadb2f8 PNA |
1833 | static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, |
1834 | const struct nft_hook *this) | |
b75a3e83 PNA |
1835 | { |
1836 | struct nft_hook *hook; | |
1837 | ||
1838 | list_for_each_entry(hook, hook_list, list) { | |
1839 | if (this->ops.dev == hook->ops.dev) | |
abadb2f8 | 1840 | return hook; |
b75a3e83 PNA |
1841 | } |
1842 | ||
abadb2f8 | 1843 | return NULL; |
b75a3e83 PNA |
1844 | } |
1845 | ||
3f0465a9 PNA |
1846 | static int nf_tables_parse_netdev_hooks(struct net *net, |
1847 | const struct nlattr *attr, | |
1848 | struct list_head *hook_list) | |
1849 | { | |
1850 | struct nft_hook *hook, *next; | |
1851 | const struct nlattr *tmp; | |
1852 | int rem, n = 0, err; | |
1853 | ||
1854 | nla_for_each_nested(tmp, attr, rem) { | |
1855 | if (nla_type(tmp) != NFTA_DEVICE_NAME) { | |
1856 | err = -EINVAL; | |
1857 | goto err_hook; | |
1858 | } | |
1859 | ||
1860 | hook = nft_netdev_hook_alloc(net, tmp); | |
1861 | if (IS_ERR(hook)) { | |
1862 | err = PTR_ERR(hook); | |
1863 | goto err_hook; | |
1864 | } | |
b75a3e83 | 1865 | if (nft_hook_list_find(hook_list, hook)) { |
cd77e75b | 1866 | kfree(hook); |
b75a3e83 PNA |
1867 | err = -EEXIST; |
1868 | goto err_hook; | |
1869 | } | |
3f0465a9 PNA |
1870 | list_add_tail(&hook->list, hook_list); |
1871 | n++; | |
1872 | ||
cb662ac6 | 1873 | if (n == NFT_NETDEVICE_MAX) { |
3f0465a9 PNA |
1874 | err = -EFBIG; |
1875 | goto err_hook; | |
1876 | } | |
1877 | } | |
3f0465a9 PNA |
1878 | |
1879 | return 0; | |
1880 | ||
1881 | err_hook: | |
1882 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
1883 | list_del(&hook->list); | |
1884 | kfree(hook); | |
1885 | } | |
1886 | return err; | |
1887 | } | |
1888 | ||
508f8ccd PNA |
1889 | struct nft_chain_hook { |
1890 | u32 num; | |
84ba7dd7 | 1891 | s32 priority; |
32537e91 | 1892 | const struct nft_chain_type *type; |
d54725cd | 1893 | struct list_head list; |
508f8ccd PNA |
1894 | }; |
1895 | ||
d54725cd PNA |
1896 | static int nft_chain_parse_netdev(struct net *net, |
1897 | struct nlattr *tb[], | |
1898 | struct list_head *hook_list) | |
1899 | { | |
1900 | struct nft_hook *hook; | |
1901 | int err; | |
1902 | ||
1903 | if (tb[NFTA_HOOK_DEV]) { | |
1904 | hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]); | |
1905 | if (IS_ERR(hook)) | |
1906 | return PTR_ERR(hook); | |
1907 | ||
1908 | list_add_tail(&hook->list, hook_list); | |
1909 | } else if (tb[NFTA_HOOK_DEVS]) { | |
1910 | err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS], | |
1911 | hook_list); | |
1912 | if (err < 0) | |
1913 | return err; | |
05abe445 PNA |
1914 | |
1915 | if (list_empty(hook_list)) | |
1916 | return -EINVAL; | |
d54725cd PNA |
1917 | } else { |
1918 | return -EINVAL; | |
1919 | } | |
1920 | ||
1921 | return 0; | |
1922 | } | |
1923 | ||
508f8ccd PNA |
1924 | static int nft_chain_parse_hook(struct net *net, |
1925 | const struct nlattr * const nla[], | |
36596dad | 1926 | struct nft_chain_hook *hook, u8 family, |
983c4fcb | 1927 | struct netlink_ext_ack *extack, bool autoload) |
508f8ccd | 1928 | { |
d59d2f82 | 1929 | struct nftables_pernet *nft_net = nft_pernet(net); |
508f8ccd | 1930 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; |
32537e91 | 1931 | const struct nft_chain_type *type; |
508f8ccd PNA |
1932 | int err; |
1933 | ||
0854db2a | 1934 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b FW |
1935 | lockdep_nfnl_nft_mutex_not_held(); |
1936 | ||
8cb08174 JB |
1937 | err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX, |
1938 | nla[NFTA_CHAIN_HOOK], | |
1939 | nft_hook_policy, NULL); | |
508f8ccd PNA |
1940 | if (err < 0) |
1941 | return err; | |
1942 | ||
1943 | if (ha[NFTA_HOOK_HOOKNUM] == NULL || | |
1944 | ha[NFTA_HOOK_PRIORITY] == NULL) | |
1945 | return -EINVAL; | |
1946 | ||
1947 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
508f8ccd PNA |
1948 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); |
1949 | ||
82603549 PNA |
1950 | type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT); |
1951 | if (!type) | |
1952 | return -EOPNOTSUPP; | |
1953 | ||
508f8ccd | 1954 | if (nla[NFTA_CHAIN_TYPE]) { |
452238e8 | 1955 | type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE], |
445509eb | 1956 | family, autoload); |
983c4fcb PNA |
1957 | if (IS_ERR(type)) { |
1958 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
508f8ccd | 1959 | return PTR_ERR(type); |
983c4fcb | 1960 | } |
508f8ccd | 1961 | } |
d25e2e93 | 1962 | if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num))) |
508f8ccd | 1963 | return -EOPNOTSUPP; |
84ba7dd7 FW |
1964 | |
1965 | if (type->type == NFT_CHAIN_T_NAT && | |
1966 | hook->priority <= NF_IP_PRI_CONNTRACK) | |
1967 | return -EOPNOTSUPP; | |
1968 | ||
983c4fcb PNA |
1969 | if (!try_module_get(type->owner)) { |
1970 | if (nla[NFTA_CHAIN_TYPE]) | |
1971 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
508f8ccd | 1972 | return -ENOENT; |
983c4fcb | 1973 | } |
508f8ccd PNA |
1974 | |
1975 | hook->type = type; | |
1976 | ||
d54725cd | 1977 | INIT_LIST_HEAD(&hook->list); |
d3519cb8 | 1978 | if (nft_base_chain_netdev(family, hook->num)) { |
d54725cd PNA |
1979 | err = nft_chain_parse_netdev(net, ha, &hook->list); |
1980 | if (err < 0) { | |
508f8ccd | 1981 | module_put(type->owner); |
d54725cd | 1982 | return err; |
508f8ccd | 1983 | } |
d54725cd | 1984 | } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) { |
508f8ccd PNA |
1985 | module_put(type->owner); |
1986 | return -EOPNOTSUPP; | |
1987 | } | |
1988 | ||
1989 | return 0; | |
1990 | } | |
1991 | ||
1992 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
1993 | { | |
d54725cd PNA |
1994 | struct nft_hook *h, *next; |
1995 | ||
1996 | list_for_each_entry_safe(h, next, &hook->list, list) { | |
1997 | list_del(&h->list); | |
1998 | kfree(h); | |
1999 | } | |
508f8ccd | 2000 | module_put(hook->type->owner); |
508f8ccd PNA |
2001 | } |
2002 | ||
0cbc06b3 FW |
2003 | struct nft_rules_old { |
2004 | struct rcu_head h; | |
2c865a8a | 2005 | struct nft_rule_blob *blob; |
0cbc06b3 FW |
2006 | }; |
2007 | ||
2c865a8a | 2008 | static void nft_last_rule(struct nft_rule_blob *blob, const void *ptr) |
0cbc06b3 | 2009 | { |
2c865a8a PNA |
2010 | struct nft_rule_dp *prule; |
2011 | ||
2012 | prule = (struct nft_rule_dp *)ptr; | |
2013 | prule->is_last = 1; | |
2c865a8a PNA |
2014 | /* blob size does not include the trailer rule */ |
2015 | } | |
2016 | ||
2017 | static struct nft_rule_blob *nf_tables_chain_alloc_rules(unsigned int size) | |
2018 | { | |
2019 | struct nft_rule_blob *blob; | |
2020 | ||
2021 | /* size must include room for the last rule */ | |
2022 | if (size < offsetof(struct nft_rule_dp, data)) | |
2023 | return NULL; | |
2024 | ||
2025 | size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rules_old); | |
2026 | if (size > INT_MAX) | |
0cbc06b3 FW |
2027 | return NULL; |
2028 | ||
2c865a8a PNA |
2029 | blob = kvmalloc(size, GFP_KERNEL); |
2030 | if (!blob) | |
0cbc06b3 FW |
2031 | return NULL; |
2032 | ||
2c865a8a PNA |
2033 | blob->size = 0; |
2034 | nft_last_rule(blob, blob->data); | |
0cbc06b3 | 2035 | |
2c865a8a | 2036 | return blob; |
0cbc06b3 FW |
2037 | } |
2038 | ||
d54725cd PNA |
2039 | static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family, |
2040 | const struct nft_chain_hook *hook, | |
2041 | struct nft_chain *chain) | |
2042 | { | |
c5c6accd FW |
2043 | ops->pf = family; |
2044 | ops->hooknum = hook->num; | |
2045 | ops->priority = hook->priority; | |
2046 | ops->priv = chain; | |
2047 | ops->hook = hook->type->hooks[ops->hooknum]; | |
2048 | ops->hook_ops_type = NF_HOOK_OP_NF_TABLES; | |
d54725cd PNA |
2049 | } |
2050 | ||
2051 | static int nft_basechain_init(struct nft_base_chain *basechain, u8 family, | |
2052 | struct nft_chain_hook *hook, u32 flags) | |
2053 | { | |
2054 | struct nft_chain *chain; | |
2055 | struct nft_hook *h; | |
2056 | ||
2057 | basechain->type = hook->type; | |
2058 | INIT_LIST_HEAD(&basechain->hook_list); | |
2059 | chain = &basechain->chain; | |
2060 | ||
d3519cb8 | 2061 | if (nft_base_chain_netdev(family, hook->num)) { |
d54725cd PNA |
2062 | list_splice_init(&hook->list, &basechain->hook_list); |
2063 | list_for_each_entry(h, &basechain->hook_list, list) | |
2064 | nft_basechain_hook_init(&h->ops, family, hook, chain); | |
2065 | ||
2066 | basechain->ops.hooknum = hook->num; | |
2067 | basechain->ops.priority = hook->priority; | |
2068 | } else { | |
2069 | nft_basechain_hook_init(&basechain->ops, family, hook, chain); | |
2070 | } | |
2071 | ||
67c49de4 | 2072 | chain->flags |= NFT_CHAIN_BASE | flags; |
d54725cd PNA |
2073 | basechain->policy = NF_ACCEPT; |
2074 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD && | |
2075 | nft_chain_offload_priority(basechain) < 0) | |
2076 | return -EOPNOTSUPP; | |
2077 | ||
2078 | flow_block_init(&basechain->flow_block); | |
2079 | ||
2080 | return 0; | |
2081 | } | |
2082 | ||
04b7db41 PNA |
2083 | static int nft_chain_add(struct nft_table *table, struct nft_chain *chain) |
2084 | { | |
2085 | int err; | |
2086 | ||
2087 | err = rhltable_insert_key(&table->chains_ht, chain->name, | |
2088 | &chain->rhlhead, nft_chain_ht_params); | |
2089 | if (err) | |
2090 | return err; | |
2091 | ||
2092 | list_add_tail_rcu(&chain->list, &table->chains); | |
2093 | ||
2094 | return 0; | |
2095 | } | |
2096 | ||
d0e2c7de PNA |
2097 | static u64 chain_id; |
2098 | ||
4035285f | 2099 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
983c4fcb PNA |
2100 | u8 policy, u32 flags, |
2101 | struct netlink_ext_ack *extack) | |
4035285f PNA |
2102 | { |
2103 | const struct nlattr * const *nla = ctx->nla; | |
2104 | struct nft_table *table = ctx->table; | |
4035285f PNA |
2105 | struct nft_base_chain *basechain; |
2106 | struct nft_stats __percpu *stats; | |
2107 | struct net *net = ctx->net; | |
d0e2c7de | 2108 | char name[NFT_NAME_MAXLEN]; |
2c865a8a | 2109 | struct nft_rule_blob *blob; |
66293c46 | 2110 | struct nft_trans *trans; |
4035285f | 2111 | struct nft_chain *chain; |
2c865a8a | 2112 | unsigned int data_size; |
4035285f PNA |
2113 | int err; |
2114 | ||
2115 | if (table->use == UINT_MAX) | |
2116 | return -EOVERFLOW; | |
2117 | ||
2118 | if (nla[NFTA_CHAIN_HOOK]) { | |
2119 | struct nft_chain_hook hook; | |
4035285f | 2120 | |
d0e2c7de PNA |
2121 | if (flags & NFT_CHAIN_BINDING) |
2122 | return -EOPNOTSUPP; | |
2123 | ||
983c4fcb PNA |
2124 | err = nft_chain_parse_hook(net, nla, &hook, family, extack, |
2125 | true); | |
4035285f PNA |
2126 | if (err < 0) |
2127 | return err; | |
2128 | ||
2129 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL); | |
2130 | if (basechain == NULL) { | |
2131 | nft_chain_release_hook(&hook); | |
2132 | return -ENOMEM; | |
2133 | } | |
d54725cd | 2134 | chain = &basechain->chain; |
4035285f PNA |
2135 | |
2136 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2137 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2138 | if (IS_ERR(stats)) { | |
2139 | nft_chain_release_hook(&hook); | |
2140 | kfree(basechain); | |
2141 | return PTR_ERR(stats); | |
2142 | } | |
4c05ec47 | 2143 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
2144 | static_branch_inc(&nft_counters_enabled); |
2145 | } | |
2146 | ||
d54725cd PNA |
2147 | err = nft_basechain_init(basechain, family, &hook, flags); |
2148 | if (err < 0) { | |
2149 | nft_chain_release_hook(&hook); | |
2150 | kfree(basechain); | |
2151 | return err; | |
2152 | } | |
4035285f | 2153 | } else { |
d0e2c7de PNA |
2154 | if (flags & NFT_CHAIN_BASE) |
2155 | return -EINVAL; | |
2156 | if (flags & NFT_CHAIN_HW_OFFLOAD) | |
2157 | return -EOPNOTSUPP; | |
2158 | ||
4035285f PNA |
2159 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); |
2160 | if (chain == NULL) | |
2161 | return -ENOMEM; | |
d0e2c7de PNA |
2162 | |
2163 | chain->flags = flags; | |
4035285f | 2164 | } |
43a605f2 PNA |
2165 | ctx->chain = chain; |
2166 | ||
4035285f PNA |
2167 | INIT_LIST_HEAD(&chain->rules); |
2168 | chain->handle = nf_tables_alloc_handle(table); | |
2169 | chain->table = table; | |
d0e2c7de PNA |
2170 | |
2171 | if (nla[NFTA_CHAIN_NAME]) { | |
2172 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); | |
2173 | } else { | |
59136aa3 FW |
2174 | if (!(flags & NFT_CHAIN_BINDING)) { |
2175 | err = -EINVAL; | |
002f2176 | 2176 | goto err_destroy_chain; |
59136aa3 | 2177 | } |
d0e2c7de PNA |
2178 | |
2179 | snprintf(name, sizeof(name), "__chain%llu", ++chain_id); | |
2180 | chain->name = kstrdup(name, GFP_KERNEL); | |
2181 | } | |
2182 | ||
4035285f PNA |
2183 | if (!chain->name) { |
2184 | err = -ENOMEM; | |
002f2176 JGG |
2185 | goto err_destroy_chain; |
2186 | } | |
2187 | ||
2188 | if (nla[NFTA_CHAIN_USERDATA]) { | |
2189 | chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL); | |
2190 | if (chain->udata == NULL) { | |
2191 | err = -ENOMEM; | |
2192 | goto err_destroy_chain; | |
2193 | } | |
2194 | chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]); | |
4035285f PNA |
2195 | } |
2196 | ||
2c865a8a PNA |
2197 | data_size = offsetof(struct nft_rule_dp, data); /* last rule */ |
2198 | blob = nf_tables_chain_alloc_rules(data_size); | |
2199 | if (!blob) { | |
0cbc06b3 | 2200 | err = -ENOMEM; |
002f2176 | 2201 | goto err_destroy_chain; |
0cbc06b3 FW |
2202 | } |
2203 | ||
2c865a8a PNA |
2204 | RCU_INIT_POINTER(chain->blob_gen_0, blob); |
2205 | RCU_INIT_POINTER(chain->blob_gen_1, blob); | |
0cbc06b3 | 2206 | |
c974a3a3 | 2207 | err = nf_tables_register_hook(net, table, chain); |
4035285f | 2208 | if (err < 0) |
002f2176 | 2209 | goto err_destroy_chain; |
4035285f | 2210 | |
66293c46 FW |
2211 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
2212 | if (IS_ERR(trans)) { | |
2213 | err = PTR_ERR(trans); | |
002f2176 | 2214 | goto err_unregister_hook; |
1b2470e5 | 2215 | } |
4035285f | 2216 | |
ad652f38 | 2217 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
2218 | if (nft_is_base_chain(chain)) |
2219 | nft_trans_chain_policy(trans) = policy; | |
2220 | ||
04b7db41 PNA |
2221 | err = nft_chain_add(table, chain); |
2222 | if (err < 0) { | |
2223 | nft_trans_destroy(trans); | |
002f2176 | 2224 | goto err_unregister_hook; |
04b7db41 PNA |
2225 | } |
2226 | ||
4035285f | 2227 | table->use++; |
4035285f PNA |
2228 | |
2229 | return 0; | |
002f2176 | 2230 | err_unregister_hook: |
c974a3a3 | 2231 | nf_tables_unregister_hook(net, table, chain); |
002f2176 | 2232 | err_destroy_chain: |
43a605f2 | 2233 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2234 | |
2235 | return err; | |
2236 | } | |
2237 | ||
d54725cd PNA |
2238 | static bool nft_hook_list_equal(struct list_head *hook_list1, |
2239 | struct list_head *hook_list2) | |
2240 | { | |
2241 | struct nft_hook *hook; | |
2242 | int n = 0, m = 0; | |
2243 | ||
2244 | n = 0; | |
2245 | list_for_each_entry(hook, hook_list2, list) { | |
2246 | if (!nft_hook_list_find(hook_list1, hook)) | |
2247 | return false; | |
2248 | ||
2249 | n++; | |
2250 | } | |
2251 | list_for_each_entry(hook, hook_list1, list) | |
2252 | m++; | |
2253 | ||
2254 | return n == m; | |
2255 | } | |
2256 | ||
c9626a2c | 2257 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
98a381a7 PNA |
2258 | u32 flags, const struct nlattr *attr, |
2259 | struct netlink_ext_ack *extack) | |
2c4a488a PNA |
2260 | { |
2261 | const struct nlattr * const *nla = ctx->nla; | |
2262 | struct nft_table *table = ctx->table; | |
2263 | struct nft_chain *chain = ctx->chain; | |
2c4a488a PNA |
2264 | struct nft_base_chain *basechain; |
2265 | struct nft_stats *stats = NULL; | |
2266 | struct nft_chain_hook hook; | |
2c4a488a PNA |
2267 | struct nf_hook_ops *ops; |
2268 | struct nft_trans *trans; | |
c974a3a3 | 2269 | int err; |
2c4a488a | 2270 | |
c9626a2c PNA |
2271 | if (chain->flags ^ flags) |
2272 | return -EOPNOTSUPP; | |
2273 | ||
2c4a488a | 2274 | if (nla[NFTA_CHAIN_HOOK]) { |
98a381a7 PNA |
2275 | if (!nft_is_base_chain(chain)) { |
2276 | NL_SET_BAD_ATTR(extack, attr); | |
77a92189 | 2277 | return -EEXIST; |
98a381a7 | 2278 | } |
36596dad | 2279 | err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family, |
983c4fcb | 2280 | extack, false); |
2c4a488a PNA |
2281 | if (err < 0) |
2282 | return err; | |
2283 | ||
2284 | basechain = nft_base_chain(chain); | |
2285 | if (basechain->type != hook.type) { | |
2286 | nft_chain_release_hook(&hook); | |
98a381a7 | 2287 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2288 | return -EEXIST; |
2c4a488a PNA |
2289 | } |
2290 | ||
d3519cb8 | 2291 | if (nft_base_chain_netdev(ctx->family, hook.num)) { |
d54725cd PNA |
2292 | if (!nft_hook_list_equal(&basechain->hook_list, |
2293 | &hook.list)) { | |
2294 | nft_chain_release_hook(&hook); | |
98a381a7 | 2295 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2296 | return -EEXIST; |
d54725cd PNA |
2297 | } |
2298 | } else { | |
2299 | ops = &basechain->ops; | |
2300 | if (ops->hooknum != hook.num || | |
2301 | ops->priority != hook.priority) { | |
2302 | nft_chain_release_hook(&hook); | |
98a381a7 | 2303 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2304 | return -EEXIST; |
d54725cd | 2305 | } |
2c4a488a PNA |
2306 | } |
2307 | nft_chain_release_hook(&hook); | |
2308 | } | |
2309 | ||
2310 | if (nla[NFTA_CHAIN_HANDLE] && | |
2311 | nla[NFTA_CHAIN_NAME]) { | |
2312 | struct nft_chain *chain2; | |
2313 | ||
f102d66b FW |
2314 | chain2 = nft_chain_lookup(ctx->net, table, |
2315 | nla[NFTA_CHAIN_NAME], genmask); | |
98a381a7 PNA |
2316 | if (!IS_ERR(chain2)) { |
2317 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
0d18779b | 2318 | return -EEXIST; |
98a381a7 | 2319 | } |
2c4a488a PNA |
2320 | } |
2321 | ||
2322 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2323 | if (!nft_is_base_chain(chain)) | |
2324 | return -EOPNOTSUPP; | |
2325 | ||
2326 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2327 | if (IS_ERR(stats)) | |
2328 | return PTR_ERR(stats); | |
2329 | } | |
2330 | ||
c6cc94df | 2331 | err = -ENOMEM; |
2c4a488a PNA |
2332 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2333 | sizeof(struct nft_trans_chain)); | |
c6cc94df FW |
2334 | if (trans == NULL) |
2335 | goto err; | |
2c4a488a PNA |
2336 | |
2337 | nft_trans_chain_stats(trans) = stats; | |
2338 | nft_trans_chain_update(trans) = true; | |
2339 | ||
2340 | if (nla[NFTA_CHAIN_POLICY]) | |
2341 | nft_trans_chain_policy(trans) = policy; | |
2342 | else | |
2343 | nft_trans_chain_policy(trans) = -1; | |
2344 | ||
c6cc94df FW |
2345 | if (nla[NFTA_CHAIN_HANDLE] && |
2346 | nla[NFTA_CHAIN_NAME]) { | |
d59d2f82 | 2347 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c6cc94df FW |
2348 | struct nft_trans *tmp; |
2349 | char *name; | |
2350 | ||
2351 | err = -ENOMEM; | |
2352 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); | |
2353 | if (!name) | |
2354 | goto err; | |
2355 | ||
2356 | err = -EEXIST; | |
0854db2a | 2357 | list_for_each_entry(tmp, &nft_net->commit_list, list) { |
c6cc94df FW |
2358 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && |
2359 | tmp->ctx.table == table && | |
2360 | nft_trans_chain_update(tmp) && | |
2361 | nft_trans_chain_name(tmp) && | |
2362 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
98a381a7 | 2363 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); |
c6cc94df FW |
2364 | kfree(name); |
2365 | goto err; | |
2366 | } | |
2c4a488a | 2367 | } |
c6cc94df FW |
2368 | |
2369 | nft_trans_chain_name(trans) = name; | |
2c4a488a | 2370 | } |
0854db2a | 2371 | nft_trans_commit_list_add_tail(ctx->net, trans); |
2c4a488a PNA |
2372 | |
2373 | return 0; | |
c6cc94df FW |
2374 | err: |
2375 | free_percpu(stats); | |
2376 | kfree(trans); | |
2377 | return err; | |
2c4a488a PNA |
2378 | } |
2379 | ||
837830a4 PNA |
2380 | static struct nft_chain *nft_chain_lookup_byid(const struct net *net, |
2381 | const struct nlattr *nla) | |
2382 | { | |
d59d2f82 | 2383 | struct nftables_pernet *nft_net = nft_pernet(net); |
837830a4 PNA |
2384 | u32 id = ntohl(nla_get_be32(nla)); |
2385 | struct nft_trans *trans; | |
2386 | ||
0854db2a | 2387 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
837830a4 PNA |
2388 | struct nft_chain *chain = trans->ctx.chain; |
2389 | ||
2390 | if (trans->msg_type == NFT_MSG_NEWCHAIN && | |
2391 | id == nft_trans_chain_id(trans)) | |
2392 | return chain; | |
2393 | } | |
2394 | return ERR_PTR(-ENOENT); | |
2395 | } | |
2396 | ||
7dab8ee3 PNA |
2397 | static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info, |
2398 | const struct nlattr * const nla[]) | |
96518518 | 2399 | { |
7dab8ee3 | 2400 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
2401 | struct netlink_ext_ack *extack = info->extack; |
2402 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2403 | u8 family = info->nfmsg->nfgen_family; |
74cccc3d | 2404 | struct nft_chain *chain = NULL; |
7dab8ee3 | 2405 | struct net *net = info->net; |
36dd1bcc | 2406 | const struct nlattr *attr; |
96518518 | 2407 | struct nft_table *table; |
57de2a0c | 2408 | u8 policy = NF_ACCEPT; |
4035285f | 2409 | struct nft_ctx ctx; |
96518518 | 2410 | u64 handle = 0; |
c9626a2c | 2411 | u32 flags = 0; |
96518518 | 2412 | |
0854db2a | 2413 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 2414 | |
6001a930 PNA |
2415 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2416 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2417 | if (IS_ERR(table)) { |
2418 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2419 | return PTR_ERR(table); |
36dd1bcc | 2420 | } |
96518518 | 2421 | |
96518518 | 2422 | chain = NULL; |
36dd1bcc | 2423 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2424 | |
2425 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2426 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2427 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2428 | if (IS_ERR(chain)) { |
2429 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2430 | return PTR_ERR(chain); |
36dd1bcc PNA |
2431 | } |
2432 | attr = nla[NFTA_CHAIN_HANDLE]; | |
74cccc3d | 2433 | } else if (nla[NFTA_CHAIN_NAME]) { |
f102d66b | 2434 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2435 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2436 | if (PTR_ERR(chain) != -ENOENT) { |
2437 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2438 | return PTR_ERR(chain); |
36dd1bcc | 2439 | } |
96518518 PM |
2440 | chain = NULL; |
2441 | } | |
74cccc3d PNA |
2442 | } else if (!nla[NFTA_CHAIN_ID]) { |
2443 | return -EINVAL; | |
96518518 PM |
2444 | } |
2445 | ||
57de2a0c | 2446 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2447 | if (chain != NULL && |
36dd1bcc PNA |
2448 | !nft_is_base_chain(chain)) { |
2449 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2450 | return -EOPNOTSUPP; |
36dd1bcc | 2451 | } |
d6b6cb1d PNA |
2452 | |
2453 | if (chain == NULL && | |
36dd1bcc PNA |
2454 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2455 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2456 | return -EOPNOTSUPP; |
36dd1bcc | 2457 | } |
57de2a0c | 2458 | |
8f46df18 | 2459 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2460 | switch (policy) { |
2461 | case NF_DROP: | |
2462 | case NF_ACCEPT: | |
2463 | break; | |
2464 | default: | |
2465 | return -EINVAL; | |
2466 | } | |
2467 | } | |
2468 | ||
c9626a2c PNA |
2469 | if (nla[NFTA_CHAIN_FLAGS]) |
2470 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2471 | else if (chain) |
2472 | flags = chain->flags; | |
c9626a2c | 2473 | |
c1f79a2e PNA |
2474 | if (flags & ~NFT_CHAIN_FLAGS) |
2475 | return -EOPNOTSUPP; | |
2476 | ||
7dab8ee3 | 2477 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
4035285f | 2478 | |
96518518 | 2479 | if (chain != NULL) { |
7dab8ee3 | 2480 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 2481 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2482 | return -EEXIST; |
36dd1bcc | 2483 | } |
7dab8ee3 | 2484 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
2485 | return -EOPNOTSUPP; |
2486 | ||
67c49de4 | 2487 | flags |= chain->flags & NFT_CHAIN_BASE; |
98a381a7 PNA |
2488 | return nf_tables_updchain(&ctx, genmask, policy, flags, attr, |
2489 | extack); | |
96518518 PM |
2490 | } |
2491 | ||
983c4fcb | 2492 | return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack); |
96518518 PM |
2493 | } |
2494 | ||
7dab8ee3 PNA |
2495 | static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info, |
2496 | const struct nlattr * const nla[]) | |
96518518 | 2497 | { |
7dab8ee3 PNA |
2498 | struct netlink_ext_ack *extack = info->extack; |
2499 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2500 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 2501 | struct net *net = info->net; |
36dd1bcc | 2502 | const struct nlattr *attr; |
96518518 PM |
2503 | struct nft_table *table; |
2504 | struct nft_chain *chain; | |
9dee1474 | 2505 | struct nft_rule *rule; |
91c7b38d | 2506 | struct nft_ctx ctx; |
3ecbfd65 | 2507 | u64 handle; |
9dee1474 PNA |
2508 | u32 use; |
2509 | int err; | |
96518518 | 2510 | |
6001a930 PNA |
2511 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2512 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2513 | if (IS_ERR(table)) { |
2514 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2515 | return PTR_ERR(table); |
36dd1bcc | 2516 | } |
96518518 | 2517 | |
3ecbfd65 | 2518 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2519 | attr = nla[NFTA_CHAIN_HANDLE]; |
2520 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2521 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2522 | } else { |
36dd1bcc | 2523 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2524 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2525 | } |
36dd1bcc PNA |
2526 | if (IS_ERR(chain)) { |
2527 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2528 | return PTR_ERR(chain); |
36dd1bcc | 2529 | } |
9dee1474 | 2530 | |
7dab8ee3 | 2531 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
9dee1474 | 2532 | chain->use > 0) |
96518518 PM |
2533 | return -EBUSY; |
2534 | ||
7dab8ee3 | 2535 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0165d932 | 2536 | |
9dee1474 PNA |
2537 | use = chain->use; |
2538 | list_for_each_entry(rule, &chain->rules, list) { | |
2539 | if (!nft_is_active_next(net, rule)) | |
2540 | continue; | |
2541 | use--; | |
2542 | ||
2543 | err = nft_delrule(&ctx, rule); | |
2544 | if (err < 0) | |
2545 | return err; | |
2546 | } | |
2547 | ||
2548 | /* There are rules and elements that are still holding references to us, | |
2549 | * we cannot do a recursive removal in this case. | |
2550 | */ | |
36dd1bcc PNA |
2551 | if (use > 0) { |
2552 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2553 | return -EBUSY; |
36dd1bcc | 2554 | } |
9dee1474 | 2555 | |
ee01d542 | 2556 | return nft_delchain(&ctx); |
96518518 PM |
2557 | } |
2558 | ||
96518518 PM |
2559 | /* |
2560 | * Expressions | |
2561 | */ | |
2562 | ||
2563 | /** | |
ef1f7df9 | 2564 | * nft_register_expr - register nf_tables expr type |
3db86c39 | 2565 | * @type: expr type |
96518518 | 2566 | * |
ef1f7df9 | 2567 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2568 | * success or a negative errno code otherwise. |
2569 | */ | |
ef1f7df9 | 2570 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2571 | { |
2572 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2573 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2574 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2575 | else |
e688a7f8 | 2576 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2577 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2578 | return 0; | |
2579 | } | |
2580 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2581 | ||
2582 | /** | |
ef1f7df9 | 2583 | * nft_unregister_expr - unregister nf_tables expr type |
3db86c39 | 2584 | * @type: expr type |
96518518 | 2585 | * |
ef1f7df9 | 2586 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2587 | */ |
ef1f7df9 | 2588 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2589 | { |
2590 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2591 | list_del_rcu(&type->list); |
96518518 PM |
2592 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2593 | } | |
2594 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2595 | ||
64d46806 PM |
2596 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2597 | struct nlattr *nla) | |
96518518 | 2598 | { |
9cff126f | 2599 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2600 | |
ef1f7df9 | 2601 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2602 | if (!nla_strcmp(nla, type->name)) { |
2603 | if (!type->family && !candidate) | |
2604 | candidate = type; | |
2605 | else if (type->family == family) | |
2606 | candidate = type; | |
2607 | } | |
96518518 | 2608 | } |
9cff126f | 2609 | return candidate; |
96518518 PM |
2610 | } |
2611 | ||
b9c04ae7 PNA |
2612 | #ifdef CONFIG_MODULES |
2613 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
2614 | struct nlattr *nla) | |
2615 | { | |
eb014de4 PNA |
2616 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
2617 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
2618 | return -EAGAIN; |
2619 | ||
2620 | return 0; | |
2621 | } | |
2622 | #endif | |
2623 | ||
452238e8 FW |
2624 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
2625 | u8 family, | |
64d46806 | 2626 | struct nlattr *nla) |
96518518 | 2627 | { |
ef1f7df9 | 2628 | const struct nft_expr_type *type; |
96518518 PM |
2629 | |
2630 | if (nla == NULL) | |
2631 | return ERR_PTR(-EINVAL); | |
2632 | ||
64d46806 | 2633 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
2634 | if (type != NULL && try_module_get(type->owner)) |
2635 | return type; | |
96518518 | 2636 | |
f102d66b | 2637 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 2638 | #ifdef CONFIG_MODULES |
ef1f7df9 | 2639 | if (type == NULL) { |
b9c04ae7 | 2640 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
2641 | return ERR_PTR(-EAGAIN); |
2642 | ||
eb014de4 PNA |
2643 | if (nft_request_module(net, "nft-expr-%.*s", |
2644 | nla_len(nla), | |
2645 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
2646 | return ERR_PTR(-EAGAIN); |
2647 | } | |
2648 | #endif | |
2649 | return ERR_PTR(-ENOENT); | |
2650 | } | |
2651 | ||
2652 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
2653 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
2654 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
2655 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
2656 | }; | |
2657 | ||
2658 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
2659 | const struct nft_expr *expr) | |
2660 | { | |
ef1f7df9 | 2661 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
2662 | goto nla_put_failure; |
2663 | ||
2664 | if (expr->ops->dump) { | |
ae0be8de MK |
2665 | struct nlattr *data = nla_nest_start_noflag(skb, |
2666 | NFTA_EXPR_DATA); | |
96518518 PM |
2667 | if (data == NULL) |
2668 | goto nla_put_failure; | |
2669 | if (expr->ops->dump(skb, expr) < 0) | |
2670 | goto nla_put_failure; | |
2671 | nla_nest_end(skb, data); | |
2672 | } | |
2673 | ||
2674 | return skb->len; | |
2675 | ||
2676 | nla_put_failure: | |
2677 | return -1; | |
2678 | }; | |
2679 | ||
0b2d8a7b PM |
2680 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
2681 | const struct nft_expr *expr) | |
2682 | { | |
2683 | struct nlattr *nest; | |
2684 | ||
ae0be8de | 2685 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
2686 | if (!nest) |
2687 | goto nla_put_failure; | |
2688 | if (nf_tables_fill_expr_info(skb, expr) < 0) | |
2689 | goto nla_put_failure; | |
2690 | nla_nest_end(skb, nest); | |
2691 | return 0; | |
2692 | ||
2693 | nla_put_failure: | |
2694 | return -1; | |
2695 | } | |
2696 | ||
96518518 PM |
2697 | struct nft_expr_info { |
2698 | const struct nft_expr_ops *ops; | |
83d9dcba | 2699 | const struct nlattr *attr; |
ef1f7df9 | 2700 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
2701 | }; |
2702 | ||
0ca743a5 PNA |
2703 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
2704 | const struct nlattr *nla, | |
96518518 PM |
2705 | struct nft_expr_info *info) |
2706 | { | |
ef1f7df9 | 2707 | const struct nft_expr_type *type; |
96518518 | 2708 | const struct nft_expr_ops *ops; |
ef1f7df9 | 2709 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
2710 | int err; |
2711 | ||
8cb08174 JB |
2712 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
2713 | nft_expr_policy, NULL); | |
96518518 PM |
2714 | if (err < 0) |
2715 | return err; | |
2716 | ||
452238e8 | 2717 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
2718 | if (IS_ERR(type)) |
2719 | return PTR_ERR(type); | |
2720 | ||
2721 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
2722 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
2723 | tb[NFTA_EXPR_DATA], | |
2724 | type->policy, NULL); | |
ef1f7df9 PM |
2725 | if (err < 0) |
2726 | goto err1; | |
2727 | } else | |
2728 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
2729 | ||
2730 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
2731 | ops = type->select_ops(ctx, |
2732 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
2733 | if (IS_ERR(ops)) { |
2734 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
2735 | #ifdef CONFIG_MODULES |
2736 | if (err == -EAGAIN) | |
eb014de4 PNA |
2737 | if (nft_expr_type_request_module(ctx->net, |
2738 | ctx->family, | |
2739 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
2740 | err = -ENOENT; | |
0ef1efd1 | 2741 | #endif |
ef1f7df9 PM |
2742 | goto err1; |
2743 | } | |
2744 | } else | |
2745 | ops = type->ops; | |
2746 | ||
83d9dcba | 2747 | info->attr = nla; |
96518518 | 2748 | info->ops = ops; |
83d9dcba | 2749 | |
96518518 | 2750 | return 0; |
ef1f7df9 PM |
2751 | |
2752 | err1: | |
2753 | module_put(type->owner); | |
2754 | return err; | |
96518518 PM |
2755 | } |
2756 | ||
2757 | static int nf_tables_newexpr(const struct nft_ctx *ctx, | |
7dab8ee3 | 2758 | const struct nft_expr_info *expr_info, |
96518518 PM |
2759 | struct nft_expr *expr) |
2760 | { | |
7dab8ee3 | 2761 | const struct nft_expr_ops *ops = expr_info->ops; |
96518518 PM |
2762 | int err; |
2763 | ||
2764 | expr->ops = ops; | |
2765 | if (ops->init) { | |
7dab8ee3 | 2766 | err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb); |
96518518 PM |
2767 | if (err < 0) |
2768 | goto err1; | |
2769 | } | |
2770 | ||
96518518 | 2771 | return 0; |
96518518 PM |
2772 | err1: |
2773 | expr->ops = NULL; | |
2774 | return err; | |
2775 | } | |
2776 | ||
62472bce PM |
2777 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
2778 | struct nft_expr *expr) | |
96518518 | 2779 | { |
3f3a390d PNA |
2780 | const struct nft_expr_type *type = expr->ops->type; |
2781 | ||
96518518 | 2782 | if (expr->ops->destroy) |
62472bce | 2783 | expr->ops->destroy(ctx, expr); |
3f3a390d | 2784 | module_put(type->owner); |
96518518 PM |
2785 | } |
2786 | ||
795a6d6b PNA |
2787 | static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
2788 | const struct nlattr *nla) | |
0b2d8a7b | 2789 | { |
7dab8ee3 | 2790 | struct nft_expr_info expr_info; |
0b2d8a7b | 2791 | struct nft_expr *expr; |
b8e20400 | 2792 | struct module *owner; |
0b2d8a7b PM |
2793 | int err; |
2794 | ||
7dab8ee3 | 2795 | err = nf_tables_expr_parse(ctx, nla, &expr_info); |
0b2d8a7b PM |
2796 | if (err < 0) |
2797 | goto err1; | |
2798 | ||
2799 | err = -ENOMEM; | |
7dab8ee3 | 2800 | expr = kzalloc(expr_info.ops->size, GFP_KERNEL); |
0b2d8a7b PM |
2801 | if (expr == NULL) |
2802 | goto err2; | |
2803 | ||
7dab8ee3 | 2804 | err = nf_tables_newexpr(ctx, &expr_info, expr); |
0b2d8a7b | 2805 | if (err < 0) |
6cafaf47 | 2806 | goto err3; |
0b2d8a7b PM |
2807 | |
2808 | return expr; | |
6cafaf47 LZ |
2809 | err3: |
2810 | kfree(expr); | |
0b2d8a7b | 2811 | err2: |
7dab8ee3 PNA |
2812 | owner = expr_info.ops->type->owner; |
2813 | if (expr_info.ops->type->release_ops) | |
2814 | expr_info.ops->type->release_ops(expr_info.ops); | |
b8e20400 PNA |
2815 | |
2816 | module_put(owner); | |
0b2d8a7b PM |
2817 | err1: |
2818 | return ERR_PTR(err); | |
2819 | } | |
2820 | ||
c604cc69 PNA |
2821 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
2822 | { | |
2823 | int err; | |
2824 | ||
2825 | if (src->ops->clone) { | |
2826 | dst->ops = src->ops; | |
2827 | err = src->ops->clone(dst, src); | |
2828 | if (err < 0) | |
2829 | return err; | |
2830 | } else { | |
2831 | memcpy(dst, src, src->ops->size); | |
2832 | } | |
2833 | ||
2834 | __module_get(src->ops->type->owner); | |
2835 | ||
2836 | return 0; | |
2837 | } | |
2838 | ||
0b2d8a7b PM |
2839 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
2840 | { | |
2841 | nf_tables_expr_destroy(ctx, expr); | |
2842 | kfree(expr); | |
2843 | } | |
2844 | ||
96518518 PM |
2845 | /* |
2846 | * Rules | |
2847 | */ | |
2848 | ||
cac20fcd PNA |
2849 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
2850 | u64 handle) | |
96518518 PM |
2851 | { |
2852 | struct nft_rule *rule; | |
2853 | ||
2854 | // FIXME: this sucks | |
d9adf22a | 2855 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
2856 | if (handle == rule->handle) |
2857 | return rule; | |
2858 | } | |
2859 | ||
2860 | return ERR_PTR(-ENOENT); | |
2861 | } | |
2862 | ||
cac20fcd PNA |
2863 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
2864 | const struct nlattr *nla) | |
96518518 PM |
2865 | { |
2866 | if (nla == NULL) | |
2867 | return ERR_PTR(-EINVAL); | |
2868 | ||
cac20fcd | 2869 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
2870 | } |
2871 | ||
2872 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
2873 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
2874 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
2875 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
2876 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
2877 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
2878 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 2879 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 2880 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
2881 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
2882 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 2883 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 2884 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
837830a4 | 2885 | [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
2886 | }; |
2887 | ||
84d7fce6 PNA |
2888 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
2889 | u32 portid, u32 seq, int event, | |
2890 | u32 flags, int family, | |
96518518 PM |
2891 | const struct nft_table *table, |
2892 | const struct nft_chain *chain, | |
e189ae16 | 2893 | const struct nft_rule *rule, u64 handle) |
96518518 PM |
2894 | { |
2895 | struct nlmsghdr *nlh; | |
96518518 PM |
2896 | const struct nft_expr *expr, *next; |
2897 | struct nlattr *list; | |
dedb67c4 | 2898 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 2899 | |
19c28b13 PNA |
2900 | nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0, |
2901 | nft_base_seq(net)); | |
2902 | if (!nlh) | |
96518518 PM |
2903 | goto nla_put_failure; |
2904 | ||
96518518 PM |
2905 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
2906 | goto nla_put_failure; | |
2907 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
2908 | goto nla_put_failure; | |
b46f6ded ND |
2909 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
2910 | NFTA_RULE_PAD)) | |
96518518 PM |
2911 | goto nla_put_failure; |
2912 | ||
e189ae16 PNA |
2913 | if (event != NFT_MSG_DELRULE && handle) { |
2914 | if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle), | |
b46f6ded | 2915 | NFTA_RULE_PAD)) |
5e948466 EL |
2916 | goto nla_put_failure; |
2917 | } | |
2918 | ||
b72920f6 PNA |
2919 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) |
2920 | nft_flow_rule_stats(chain, rule); | |
2921 | ||
ae0be8de | 2922 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
2923 | if (list == NULL) |
2924 | goto nla_put_failure; | |
2925 | nft_rule_for_each_expr(expr, next, rule) { | |
0b2d8a7b | 2926 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
96518518 | 2927 | goto nla_put_failure; |
96518518 PM |
2928 | } |
2929 | nla_nest_end(skb, list); | |
2930 | ||
86f1ec32 PM |
2931 | if (rule->udata) { |
2932 | struct nft_userdata *udata = nft_userdata(rule); | |
2933 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
2934 | udata->data) < 0) | |
2935 | goto nla_put_failure; | |
2936 | } | |
0768b3b3 | 2937 | |
053c095a JB |
2938 | nlmsg_end(skb, nlh); |
2939 | return 0; | |
96518518 PM |
2940 | |
2941 | nla_put_failure: | |
2942 | nlmsg_trim(skb, nlh); | |
2943 | return -1; | |
2944 | } | |
2945 | ||
25e94a99 PNA |
2946 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
2947 | const struct nft_rule *rule, int event) | |
96518518 | 2948 | { |
d59d2f82 | 2949 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
e189ae16 | 2950 | const struct nft_rule *prule; |
96518518 | 2951 | struct sk_buff *skb; |
e189ae16 PNA |
2952 | u64 handle = 0; |
2953 | u16 flags = 0; | |
96518518 PM |
2954 | int err; |
2955 | ||
128ad332 PNA |
2956 | if (!ctx->report && |
2957 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 2958 | return; |
96518518 | 2959 | |
96518518 PM |
2960 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
2961 | if (skb == NULL) | |
2962 | goto err; | |
2963 | ||
e189ae16 PNA |
2964 | if (event == NFT_MSG_NEWRULE && |
2965 | !list_is_first(&rule->list, &ctx->chain->rules) && | |
2966 | !list_is_last(&rule->list, &ctx->chain->rules)) { | |
2967 | prule = list_prev_entry(rule, list); | |
2968 | handle = prule->handle; | |
2969 | } | |
2970 | if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE)) | |
2971 | flags |= NLM_F_APPEND; | |
6fb721cf PNA |
2972 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
2973 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
e189ae16 | 2974 | |
84d7fce6 | 2975 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
e189ae16 PNA |
2976 | event, flags, ctx->family, ctx->table, |
2977 | ctx->chain, rule, handle); | |
96518518 PM |
2978 | if (err < 0) { |
2979 | kfree_skb(skb); | |
2980 | goto err; | |
2981 | } | |
2982 | ||
0854db2a | 2983 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 2984 | return; |
96518518 | 2985 | err: |
25e94a99 | 2986 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
2987 | } |
2988 | ||
6e1f760e | 2989 | struct nft_rule_dump_ctx { |
e46abbcc | 2990 | char *table; |
b7263e07 | 2991 | char *chain; |
6e1f760e PNA |
2992 | }; |
2993 | ||
241faece PS |
2994 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
2995 | unsigned int *idx, | |
2996 | struct netlink_callback *cb, | |
2997 | const struct nft_table *table, | |
2998 | const struct nft_chain *chain) | |
2999 | { | |
3000 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 3001 | const struct nft_rule *rule, *prule; |
241faece | 3002 | unsigned int s_idx = cb->args[0]; |
e189ae16 | 3003 | u64 handle; |
241faece | 3004 | |
2c82c7e7 | 3005 | prule = NULL; |
241faece PS |
3006 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
3007 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 3008 | goto cont_skip; |
241faece PS |
3009 | if (*idx < s_idx) |
3010 | goto cont; | |
3011 | if (*idx > s_idx) { | |
3012 | memset(&cb->args[1], 0, | |
3013 | sizeof(cb->args) - sizeof(cb->args[0])); | |
3014 | } | |
e189ae16 PNA |
3015 | if (prule) |
3016 | handle = prule->handle; | |
3017 | else | |
3018 | handle = 0; | |
3019 | ||
241faece PS |
3020 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
3021 | cb->nlh->nlmsg_seq, | |
3022 | NFT_MSG_NEWRULE, | |
3023 | NLM_F_MULTI | NLM_F_APPEND, | |
3024 | table->family, | |
e189ae16 | 3025 | table, chain, rule, handle) < 0) |
310529e6 | 3026 | return 1; |
241faece PS |
3027 | |
3028 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
3029 | cont: | |
2c82c7e7 FW |
3030 | prule = rule; |
3031 | cont_skip: | |
241faece PS |
3032 | (*idx)++; |
3033 | } | |
310529e6 | 3034 | return 0; |
241faece PS |
3035 | } |
3036 | ||
96518518 PM |
3037 | static int nf_tables_dump_rules(struct sk_buff *skb, |
3038 | struct netlink_callback *cb) | |
3039 | { | |
3040 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 3041 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 3042 | struct nft_table *table; |
96518518 | 3043 | const struct nft_chain *chain; |
241faece | 3044 | unsigned int idx = 0; |
99633ab2 | 3045 | struct net *net = sock_net(skb->sk); |
96518518 | 3046 | int family = nfmsg->nfgen_family; |
0854db2a | 3047 | struct nftables_pernet *nft_net; |
96518518 | 3048 | |
e688a7f8 | 3049 | rcu_read_lock(); |
d59d2f82 | 3050 | nft_net = nft_pernet(net); |
0854db2a | 3051 | cb->seq = nft_net->base_seq; |
38e029f1 | 3052 | |
0854db2a | 3053 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 3054 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
3055 | continue; |
3056 | ||
3057 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
3058 | continue; |
3059 | ||
715849ab | 3060 | if (ctx && ctx->table && ctx->chain) { |
241faece | 3061 | struct rhlist_head *list, *tmp; |
6e1f760e | 3062 | |
241faece PS |
3063 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
3064 | nft_chain_ht_params); | |
3065 | if (!list) | |
3066 | goto done; | |
3067 | ||
3068 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
3069 | if (!nft_is_active(net, chain)) | |
3070 | continue; | |
3071 | __nf_tables_dump_rules(skb, &idx, | |
3072 | cb, table, chain); | |
3073 | break; | |
96518518 | 3074 | } |
241faece | 3075 | goto done; |
96518518 | 3076 | } |
241faece PS |
3077 | |
3078 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
3079 | if (__nf_tables_dump_rules(skb, &idx, cb, table, chain)) | |
3080 | goto done; | |
3081 | } | |
3082 | ||
3083 | if (ctx && ctx->table) | |
3084 | break; | |
96518518 PM |
3085 | } |
3086 | done: | |
e688a7f8 | 3087 | rcu_read_unlock(); |
310529e6 PS |
3088 | |
3089 | cb->args[0] = idx; | |
96518518 PM |
3090 | return skb->len; |
3091 | } | |
3092 | ||
90fd131a FW |
3093 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
3094 | { | |
3095 | const struct nlattr * const *nla = cb->data; | |
3096 | struct nft_rule_dump_ctx *ctx = NULL; | |
3097 | ||
3098 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
3099 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
3100 | if (!ctx) | |
3101 | return -ENOMEM; | |
3102 | ||
3103 | if (nla[NFTA_RULE_TABLE]) { | |
3104 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
3105 | GFP_ATOMIC); | |
3106 | if (!ctx->table) { | |
3107 | kfree(ctx); | |
3108 | return -ENOMEM; | |
3109 | } | |
3110 | } | |
3111 | if (nla[NFTA_RULE_CHAIN]) { | |
3112 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
3113 | GFP_ATOMIC); | |
3114 | if (!ctx->chain) { | |
3115 | kfree(ctx->table); | |
3116 | kfree(ctx); | |
3117 | return -ENOMEM; | |
3118 | } | |
3119 | } | |
3120 | } | |
3121 | ||
3122 | cb->data = ctx; | |
3123 | return 0; | |
3124 | } | |
3125 | ||
6e1f760e PNA |
3126 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
3127 | { | |
e46abbcc PS |
3128 | struct nft_rule_dump_ctx *ctx = cb->data; |
3129 | ||
3130 | if (ctx) { | |
3131 | kfree(ctx->table); | |
b7263e07 | 3132 | kfree(ctx->chain); |
e46abbcc PS |
3133 | kfree(ctx); |
3134 | } | |
6e1f760e PNA |
3135 | return 0; |
3136 | } | |
3137 | ||
d9adf22a | 3138 | /* called with rcu_read_lock held */ |
797d4980 PNA |
3139 | static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info, |
3140 | const struct nlattr * const nla[]) | |
96518518 | 3141 | { |
797d4980 PNA |
3142 | struct netlink_ext_ack *extack = info->extack; |
3143 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 3144 | u8 family = info->nfmsg->nfgen_family; |
96518518 PM |
3145 | const struct nft_chain *chain; |
3146 | const struct nft_rule *rule; | |
797d4980 | 3147 | struct net *net = info->net; |
1b2470e5 | 3148 | struct nft_table *table; |
96518518 | 3149 | struct sk_buff *skb2; |
96518518 PM |
3150 | int err; |
3151 | ||
797d4980 | 3152 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 | 3153 | struct netlink_dump_control c = { |
90fd131a | 3154 | .start= nf_tables_dump_rules_start, |
96518518 | 3155 | .dump = nf_tables_dump_rules, |
6e1f760e | 3156 | .done = nf_tables_dump_rules_done, |
d9adf22a | 3157 | .module = THIS_MODULE, |
90fd131a | 3158 | .data = (void *)nla, |
96518518 | 3159 | }; |
6e1f760e | 3160 | |
797d4980 | 3161 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
3162 | } |
3163 | ||
6001a930 | 3164 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0); |
36dd1bcc PNA |
3165 | if (IS_ERR(table)) { |
3166 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3167 | return PTR_ERR(table); |
36dd1bcc | 3168 | } |
96518518 | 3169 | |
f102d66b | 3170 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3171 | if (IS_ERR(chain)) { |
3172 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3173 | return PTR_ERR(chain); |
36dd1bcc | 3174 | } |
96518518 | 3175 | |
cac20fcd | 3176 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3177 | if (IS_ERR(rule)) { |
3178 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3179 | return PTR_ERR(rule); |
36dd1bcc | 3180 | } |
96518518 | 3181 | |
d9adf22a | 3182 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
3183 | if (!skb2) |
3184 | return -ENOMEM; | |
3185 | ||
84d7fce6 | 3186 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 3187 | info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
e189ae16 | 3188 | family, table, chain, rule, 0); |
96518518 | 3189 | if (err < 0) |
ee921183 | 3190 | goto err_fill_rule_info; |
96518518 | 3191 | |
ee921183 | 3192 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 3193 | |
ee921183 | 3194 | err_fill_rule_info: |
96518518 PM |
3195 | kfree_skb(skb2); |
3196 | return err; | |
3197 | } | |
3198 | ||
62472bce PM |
3199 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
3200 | struct nft_rule *rule) | |
96518518 | 3201 | { |
29e38801 | 3202 | struct nft_expr *expr, *next; |
96518518 PM |
3203 | |
3204 | /* | |
3205 | * Careful: some expressions might not be initialized in case this | |
3206 | * is called on error from nf_tables_newrule(). | |
3207 | */ | |
3208 | expr = nft_expr_first(rule); | |
31cc578a | 3209 | while (nft_expr_more(rule, expr)) { |
29e38801 | 3210 | next = nft_expr_next(expr); |
62472bce | 3211 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 3212 | expr = next; |
96518518 PM |
3213 | } |
3214 | kfree(rule); | |
3215 | } | |
3216 | ||
d0e2c7de | 3217 | void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae | 3218 | { |
f6ac8585 | 3219 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
3220 | nf_tables_rule_destroy(ctx, rule); |
3221 | } | |
3222 | ||
a654de8f PNA |
3223 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
3224 | { | |
3225 | struct nft_expr *expr, *last; | |
3226 | const struct nft_data *data; | |
3227 | struct nft_rule *rule; | |
3228 | int err; | |
3229 | ||
26b2f552 TY |
3230 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
3231 | return -EMLINK; | |
3232 | ||
a654de8f PNA |
3233 | list_for_each_entry(rule, &chain->rules, list) { |
3234 | if (!nft_is_active_next(ctx->net, rule)) | |
3235 | continue; | |
3236 | ||
3237 | nft_rule_for_each_expr(expr, last, rule) { | |
3238 | if (!expr->ops->validate) | |
3239 | continue; | |
3240 | ||
3241 | err = expr->ops->validate(ctx, expr, &data); | |
3242 | if (err < 0) | |
3243 | return err; | |
3244 | } | |
3245 | } | |
3246 | ||
3247 | return 0; | |
3248 | } | |
3249 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
3250 | ||
3251 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
3252 | { | |
3253 | struct nft_chain *chain; | |
3254 | struct nft_ctx ctx = { | |
3255 | .net = net, | |
3256 | .family = table->family, | |
3257 | }; | |
3258 | int err; | |
3259 | ||
3260 | list_for_each_entry(chain, &table->chains, list) { | |
3261 | if (!nft_is_base_chain(chain)) | |
3262 | continue; | |
3263 | ||
3264 | ctx.chain = chain; | |
3265 | err = nft_chain_validate(&ctx, chain); | |
3266 | if (err < 0) | |
3267 | return err; | |
3268 | } | |
3269 | ||
3270 | return 0; | |
3271 | } | |
3272 | ||
75dd48e2 PS |
3273 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
3274 | const struct nlattr *nla); | |
3275 | ||
1081d11b PNA |
3276 | #define NFT_RULE_MAXEXPRS 128 |
3277 | ||
7dab8ee3 PNA |
3278 | static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, |
3279 | const struct nlattr * const nla[]) | |
96518518 | 3280 | { |
7dab8ee3 | 3281 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
3282 | struct netlink_ext_ack *extack = info->extack; |
3283 | unsigned int size, i, n, ulen = 0, usize = 0; | |
3284 | u8 genmask = nft_genmask_next(info->net); | |
3285 | struct nft_rule *rule, *old_rule = NULL; | |
3286 | struct nft_expr_info *expr_info = NULL; | |
ef4b65e5 | 3287 | u8 family = info->nfmsg->nfgen_family; |
3c5e4462 | 3288 | struct nft_flow_rule *flow = NULL; |
7dab8ee3 | 3289 | struct net *net = info->net; |
7dab8ee3 | 3290 | struct nft_userdata *udata; |
96518518 PM |
3291 | struct nft_table *table; |
3292 | struct nft_chain *chain; | |
7dab8ee3 PNA |
3293 | struct nft_trans *trans; |
3294 | u64 handle, pos_handle; | |
96518518 PM |
3295 | struct nft_expr *expr; |
3296 | struct nft_ctx ctx; | |
3297 | struct nlattr *tmp; | |
96518518 | 3298 | int err, rem; |
96518518 | 3299 | |
0854db2a | 3300 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3301 | |
6001a930 PNA |
3302 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3303 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3304 | if (IS_ERR(table)) { |
3305 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3306 | return PTR_ERR(table); |
36dd1bcc | 3307 | } |
96518518 | 3308 | |
837830a4 PNA |
3309 | if (nla[NFTA_RULE_CHAIN]) { |
3310 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], | |
3311 | genmask); | |
3312 | if (IS_ERR(chain)) { | |
3313 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
3314 | return PTR_ERR(chain); | |
3315 | } | |
d0e2c7de PNA |
3316 | if (nft_chain_is_bound(chain)) |
3317 | return -EOPNOTSUPP; | |
3318 | ||
837830a4 PNA |
3319 | } else if (nla[NFTA_RULE_CHAIN_ID]) { |
3320 | chain = nft_chain_lookup_byid(net, nla[NFTA_RULE_CHAIN_ID]); | |
3321 | if (IS_ERR(chain)) { | |
3322 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]); | |
3323 | return PTR_ERR(chain); | |
3324 | } | |
3325 | } else { | |
3326 | return -EINVAL; | |
36dd1bcc | 3327 | } |
96518518 PM |
3328 | |
3329 | if (nla[NFTA_RULE_HANDLE]) { | |
3330 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3331 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3332 | if (IS_ERR(rule)) { |
3333 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3334 | return PTR_ERR(rule); |
36dd1bcc | 3335 | } |
96518518 | 3336 | |
7dab8ee3 | 3337 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 3338 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
96518518 | 3339 | return -EEXIST; |
36dd1bcc | 3340 | } |
7dab8ee3 | 3341 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
3342 | old_rule = rule; |
3343 | else | |
3344 | return -EOPNOTSUPP; | |
3345 | } else { | |
7dab8ee3 PNA |
3346 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) || |
3347 | info->nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3348 | return -EINVAL; |
3349 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
3350 | |
3351 | if (chain->use == UINT_MAX) | |
3352 | return -EOVERFLOW; | |
5e948466 | 3353 | |
447750f2 FW |
3354 | if (nla[NFTA_RULE_POSITION]) { |
3355 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3356 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3357 | if (IS_ERR(old_rule)) { | |
3358 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3359 | return PTR_ERR(old_rule); | |
3360 | } | |
75dd48e2 PS |
3361 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
3362 | old_rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_POSITION_ID]); | |
3363 | if (IS_ERR(old_rule)) { | |
3364 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3365 | return PTR_ERR(old_rule); | |
3366 | } | |
36dd1bcc | 3367 | } |
5e948466 EL |
3368 | } |
3369 | ||
7dab8ee3 | 3370 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0ca743a5 | 3371 | |
96518518 PM |
3372 | n = 0; |
3373 | size = 0; | |
3374 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
7dab8ee3 PNA |
3375 | expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3376 | sizeof(struct nft_expr_info), | |
3377 | GFP_KERNEL); | |
3378 | if (!expr_info) | |
2a43ecf9 FW |
3379 | return -ENOMEM; |
3380 | ||
96518518 PM |
3381 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3382 | err = -EINVAL; | |
3383 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3c5e4462 | 3384 | goto err_release_expr; |
96518518 | 3385 | if (n == NFT_RULE_MAXEXPRS) |
3c5e4462 | 3386 | goto err_release_expr; |
7dab8ee3 | 3387 | err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]); |
c781471d PNA |
3388 | if (err < 0) { |
3389 | NL_SET_BAD_ATTR(extack, tmp); | |
3c5e4462 | 3390 | goto err_release_expr; |
c781471d | 3391 | } |
7dab8ee3 | 3392 | size += expr_info[n].ops->size; |
96518518 PM |
3393 | n++; |
3394 | } | |
3395 | } | |
9889840f PM |
3396 | /* Check for overflow of dlen field */ |
3397 | err = -EFBIG; | |
3398 | if (size >= 1 << 12) | |
3c5e4462 | 3399 | goto err_release_expr; |
96518518 | 3400 | |
86f1ec32 | 3401 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3402 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3403 | if (ulen > 0) |
3404 | usize = sizeof(struct nft_userdata) + ulen; | |
3405 | } | |
0768b3b3 | 3406 | |
96518518 | 3407 | err = -ENOMEM; |
86f1ec32 | 3408 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL); |
96518518 | 3409 | if (rule == NULL) |
3c5e4462 | 3410 | goto err_release_expr; |
96518518 | 3411 | |
889f7ee7 | 3412 | nft_activate_next(net, rule); |
0628b123 | 3413 | |
96518518 PM |
3414 | rule->handle = handle; |
3415 | rule->dlen = size; | |
86f1ec32 | 3416 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3417 | |
86f1ec32 PM |
3418 | if (ulen) { |
3419 | udata = nft_userdata(rule); | |
3420 | udata->len = ulen - 1; | |
3421 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3422 | } | |
96518518 | 3423 | |
96518518 PM |
3424 | expr = nft_expr_first(rule); |
3425 | for (i = 0; i < n; i++) { | |
7dab8ee3 | 3426 | err = nf_tables_newexpr(&ctx, &expr_info[i], expr); |
83d9dcba | 3427 | if (err < 0) { |
7dab8ee3 | 3428 | NL_SET_BAD_ATTR(extack, expr_info[i].attr); |
3c5e4462 | 3429 | goto err_release_rule; |
83d9dcba | 3430 | } |
a654de8f | 3431 | |
7dab8ee3 | 3432 | if (expr_info[i].ops->validate) |
a654de8f PNA |
3433 | nft_validate_state_update(net, NFT_VALIDATE_NEED); |
3434 | ||
7dab8ee3 | 3435 | expr_info[i].ops = NULL; |
96518518 PM |
3436 | expr = nft_expr_next(expr); |
3437 | } | |
3438 | ||
3c5e4462 PNA |
3439 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
3440 | flow = nft_flow_rule_create(net, rule); | |
3441 | if (IS_ERR(flow)) { | |
3442 | err = PTR_ERR(flow); | |
3443 | goto err_release_rule; | |
3444 | } | |
3445 | } | |
3446 | ||
7dab8ee3 | 3447 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) { |
2c964c55 PNA |
3448 | err = nft_delrule(&ctx, old_rule); |
3449 | if (err < 0) | |
3450 | goto err_destroy_flow_rule; | |
3451 | ||
ca089878 | 3452 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3453 | if (trans == NULL) { |
3454 | err = -ENOMEM; | |
3c5e4462 | 3455 | goto err_destroy_flow_rule; |
569ccae6 | 3456 | } |
569ccae6 FW |
3457 | list_add_tail_rcu(&rule->list, &old_rule->list); |
3458 | } else { | |
c9626a2c PNA |
3459 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3460 | if (!trans) { | |
569ccae6 | 3461 | err = -ENOMEM; |
3c5e4462 | 3462 | goto err_destroy_flow_rule; |
569ccae6 FW |
3463 | } |
3464 | ||
7dab8ee3 | 3465 | if (info->nlh->nlmsg_flags & NLM_F_APPEND) { |
569ccae6 FW |
3466 | if (old_rule) |
3467 | list_add_rcu(&rule->list, &old_rule->list); | |
3468 | else | |
3469 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3470 | } else { | |
3471 | if (old_rule) | |
3472 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3473 | else | |
3474 | list_add_rcu(&rule->list, &chain->rules); | |
3475 | } | |
0628b123 | 3476 | } |
7dab8ee3 | 3477 | kvfree(expr_info); |
4fefee57 | 3478 | chain->use++; |
96518518 | 3479 | |
3c5e4462 PNA |
3480 | if (flow) |
3481 | nft_trans_flow_rule(trans) = flow; | |
3482 | ||
0854db2a | 3483 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
a654de8f PNA |
3484 | return nft_table_validate(net, table); |
3485 | ||
3486 | return 0; | |
3c5e4462 PNA |
3487 | |
3488 | err_destroy_flow_rule: | |
4ca041f9 CIK |
3489 | if (flow) |
3490 | nft_flow_rule_destroy(flow); | |
3c5e4462 | 3491 | err_release_rule: |
bb7b40ae | 3492 | nf_tables_rule_release(&ctx, rule); |
3c5e4462 | 3493 | err_release_expr: |
96518518 | 3494 | for (i = 0; i < n; i++) { |
7dab8ee3 PNA |
3495 | if (expr_info[i].ops) { |
3496 | module_put(expr_info[i].ops->type->owner); | |
3497 | if (expr_info[i].ops->type->release_ops) | |
3498 | expr_info[i].ops->type->release_ops(expr_info[i].ops); | |
b25a31bf | 3499 | } |
96518518 | 3500 | } |
7dab8ee3 PNA |
3501 | kvfree(expr_info); |
3502 | ||
96518518 PM |
3503 | return err; |
3504 | } | |
3505 | ||
1a94e38d PNA |
3506 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
3507 | const struct nlattr *nla) | |
3508 | { | |
d59d2f82 | 3509 | struct nftables_pernet *nft_net = nft_pernet(net); |
1a94e38d PNA |
3510 | u32 id = ntohl(nla_get_be32(nla)); |
3511 | struct nft_trans *trans; | |
3512 | ||
0854db2a | 3513 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
1a94e38d PNA |
3514 | struct nft_rule *rule = nft_trans_rule(trans); |
3515 | ||
3516 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
3517 | id == nft_trans_rule_id(trans)) | |
3518 | return rule; | |
3519 | } | |
3520 | return ERR_PTR(-ENOENT); | |
3521 | } | |
3522 | ||
7dab8ee3 PNA |
3523 | static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info, |
3524 | const struct nlattr * const nla[]) | |
96518518 | 3525 | { |
7dab8ee3 | 3526 | struct netlink_ext_ack *extack = info->extack; |
7dab8ee3 | 3527 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 3528 | u8 family = info->nfmsg->nfgen_family; |
cf9dc09d | 3529 | struct nft_chain *chain = NULL; |
7dab8ee3 PNA |
3530 | struct net *net = info->net; |
3531 | struct nft_table *table; | |
cf9dc09d | 3532 | struct nft_rule *rule; |
0628b123 | 3533 | struct nft_ctx ctx; |
ef4b65e5 | 3534 | int err = 0; |
96518518 | 3535 | |
6001a930 PNA |
3536 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3537 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3538 | if (IS_ERR(table)) { |
3539 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3540 | return PTR_ERR(table); |
36dd1bcc | 3541 | } |
96518518 | 3542 | |
cf9dc09d | 3543 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
3544 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
3545 | genmask); | |
36dd1bcc PNA |
3546 | if (IS_ERR(chain)) { |
3547 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
cf9dc09d | 3548 | return PTR_ERR(chain); |
36dd1bcc | 3549 | } |
d0e2c7de PNA |
3550 | if (nft_chain_is_bound(chain)) |
3551 | return -EOPNOTSUPP; | |
cf9dc09d | 3552 | } |
96518518 | 3553 | |
7dab8ee3 | 3554 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0628b123 | 3555 | |
cf9dc09d PNA |
3556 | if (chain) { |
3557 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 3558 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3559 | if (IS_ERR(rule)) { |
3560 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
cf9dc09d | 3561 | return PTR_ERR(rule); |
36dd1bcc | 3562 | } |
96518518 | 3563 | |
1a94e38d PNA |
3564 | err = nft_delrule(&ctx, rule); |
3565 | } else if (nla[NFTA_RULE_ID]) { | |
3566 | rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]); | |
36dd1bcc PNA |
3567 | if (IS_ERR(rule)) { |
3568 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 3569 | return PTR_ERR(rule); |
36dd1bcc | 3570 | } |
1a94e38d | 3571 | |
5e266fe7 | 3572 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 3573 | } else { |
ce24b721 | 3574 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
3575 | } |
3576 | } else { | |
3577 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
3578 | if (!nft_is_active_next(net, chain)) |
3579 | continue; | |
3580 | ||
cf9dc09d | 3581 | ctx.chain = chain; |
ce24b721 | 3582 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
3583 | if (err < 0) |
3584 | break; | |
3585 | } | |
3586 | } | |
3587 | ||
3588 | return err; | |
3589 | } | |
3590 | ||
20a69341 PM |
3591 | /* |
3592 | * Sets | |
3593 | */ | |
e32a4dc6 FW |
3594 | static const struct nft_set_type *nft_set_types[] = { |
3595 | &nft_set_hash_fast_type, | |
3596 | &nft_set_hash_type, | |
3597 | &nft_set_rhash_type, | |
3598 | &nft_set_bitmap_type, | |
3599 | &nft_set_rbtree_type, | |
e6abef61 | 3600 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
7400b063 SB |
3601 | &nft_set_pipapo_avx2_type, |
3602 | #endif | |
e32a4dc6 FW |
3603 | &nft_set_pipapo_type, |
3604 | }; | |
20a69341 | 3605 | |
2b664957 | 3606 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
3607 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
3608 | NFT_SET_EVAL) | |
2b664957 | 3609 | |
71cc0873 | 3610 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 3611 | { |
71cc0873 | 3612 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
3613 | } |
3614 | ||
c50b960c PM |
3615 | /* |
3616 | * Select a set implementation based on the data characteristics and the | |
3617 | * given policy. The total memory use might not be known if no size is | |
3618 | * given, in that case the amount of memory per element is used. | |
3619 | */ | |
3620 | static const struct nft_set_ops * | |
2b664957 PNA |
3621 | nft_select_set_ops(const struct nft_ctx *ctx, |
3622 | const struct nlattr * const nla[], | |
c50b960c PM |
3623 | const struct nft_set_desc *desc, |
3624 | enum nft_set_policies policy) | |
20a69341 | 3625 | { |
d59d2f82 | 3626 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c50b960c PM |
3627 | const struct nft_set_ops *ops, *bops; |
3628 | struct nft_set_estimate est, best; | |
2b664957 PNA |
3629 | const struct nft_set_type *type; |
3630 | u32 flags = 0; | |
e32a4dc6 | 3631 | int i; |
20a69341 | 3632 | |
0854db2a | 3633 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3634 | lockdep_nfnl_nft_mutex_not_held(); |
e32a4dc6 | 3635 | |
2b664957 PNA |
3636 | if (nla[NFTA_SET_FLAGS] != NULL) |
3637 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 3638 | |
55af753c PNA |
3639 | bops = NULL; |
3640 | best.size = ~0; | |
3641 | best.lookup = ~0; | |
0b5a7874 | 3642 | best.space = ~0; |
c50b960c | 3643 | |
e32a4dc6 FW |
3644 | for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) { |
3645 | type = nft_set_types[i]; | |
71cc0873 | 3646 | ops = &type->ops; |
2b664957 | 3647 | |
71cc0873 | 3648 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 3649 | continue; |
2b664957 | 3650 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
3651 | continue; |
3652 | ||
3653 | switch (policy) { | |
3654 | case NFT_SET_POL_PERFORMANCE: | |
55af753c | 3655 | if (est.lookup < best.lookup) |
c50b960c | 3656 | break; |
644e334e PNA |
3657 | if (est.lookup == best.lookup && |
3658 | est.space < best.space) | |
3659 | break; | |
c50b960c PM |
3660 | continue; |
3661 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
3662 | if (!desc->size) { |
3663 | if (est.space < best.space) | |
3664 | break; | |
3665 | if (est.space == best.space && | |
3666 | est.lookup < best.lookup) | |
3667 | break; | |
4f2921ca | 3668 | } else if (est.size < best.size || !bops) { |
c50b960c | 3669 | break; |
0b5a7874 | 3670 | } |
c50b960c PM |
3671 | continue; |
3672 | default: | |
3673 | break; | |
3674 | } | |
3675 | ||
c50b960c PM |
3676 | bops = ops; |
3677 | best = est; | |
20a69341 PM |
3678 | } |
3679 | ||
c50b960c PM |
3680 | if (bops != NULL) |
3681 | return bops; | |
3682 | ||
20a69341 PM |
3683 | return ERR_PTR(-EOPNOTSUPP); |
3684 | } | |
3685 | ||
3686 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
3687 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
3688 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 3689 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 3690 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
3691 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
3692 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
3693 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
3694 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
3695 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
3696 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
3697 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 3698 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
3699 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
3700 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
3701 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
3702 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 3703 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 3704 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
65038428 | 3705 | [NFTA_SET_EXPR] = { .type = NLA_NESTED }, |
48b0ae04 | 3706 | [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED }, |
c50b960c PM |
3707 | }; |
3708 | ||
3709 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
3710 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 3711 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
3712 | }; |
3713 | ||
cac20fcd PNA |
3714 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
3715 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
3716 | { |
3717 | struct nft_set *set; | |
3718 | ||
3719 | if (nla == NULL) | |
3720 | return ERR_PTR(-EINVAL); | |
3721 | ||
d9adf22a | 3722 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
3723 | if (!nla_strcmp(nla, set->name) && |
3724 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
3725 | return set; |
3726 | } | |
3727 | return ERR_PTR(-ENOENT); | |
3728 | } | |
3729 | ||
cac20fcd PNA |
3730 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
3731 | const struct nlattr *nla, | |
3732 | u8 genmask) | |
3ecbfd65 HS |
3733 | { |
3734 | struct nft_set *set; | |
3735 | ||
3ecbfd65 HS |
3736 | list_for_each_entry(set, &table->sets, list) { |
3737 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
3738 | nft_active_genmask(set, genmask)) | |
3739 | return set; | |
3740 | } | |
3741 | return ERR_PTR(-ENOENT); | |
3742 | } | |
3743 | ||
cac20fcd PNA |
3744 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
3745 | const struct nlattr *nla, u8 genmask) | |
958bee14 | 3746 | { |
d59d2f82 | 3747 | struct nftables_pernet *nft_net = nft_pernet(net); |
958bee14 | 3748 | u32 id = ntohl(nla_get_be32(nla)); |
d59d2f82 | 3749 | struct nft_trans *trans; |
958bee14 | 3750 | |
0854db2a | 3751 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
9c7f96fd AK |
3752 | if (trans->msg_type == NFT_MSG_NEWSET) { |
3753 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 3754 | |
9c7f96fd AK |
3755 | if (id == nft_trans_set_id(trans) && |
3756 | nft_active_genmask(set, genmask)) | |
3757 | return set; | |
3758 | } | |
958bee14 PNA |
3759 | } |
3760 | return ERR_PTR(-ENOENT); | |
3761 | } | |
c7a72e3f | 3762 | |
10659cba PNA |
3763 | struct nft_set *nft_set_lookup_global(const struct net *net, |
3764 | const struct nft_table *table, | |
3765 | const struct nlattr *nla_set_name, | |
3766 | const struct nlattr *nla_set_id, | |
3767 | u8 genmask) | |
c7a72e3f PNA |
3768 | { |
3769 | struct nft_set *set; | |
3770 | ||
cac20fcd | 3771 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
3772 | if (IS_ERR(set)) { |
3773 | if (!nla_set_id) | |
3774 | return set; | |
3775 | ||
cac20fcd | 3776 | set = nft_set_lookup_byid(net, nla_set_id, genmask); |
c7a72e3f PNA |
3777 | } |
3778 | return set; | |
3779 | } | |
10659cba | 3780 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 3781 | |
20a69341 PM |
3782 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
3783 | const char *name) | |
3784 | { | |
3785 | const struct nft_set *i; | |
3786 | const char *p; | |
3787 | unsigned long *inuse; | |
60eb1894 | 3788 | unsigned int n = 0, min = 0; |
20a69341 | 3789 | |
38745490 | 3790 | p = strchr(name, '%'); |
20a69341 PM |
3791 | if (p != NULL) { |
3792 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
3793 | return -EINVAL; | |
3794 | ||
3795 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
3796 | if (inuse == NULL) | |
3797 | return -ENOMEM; | |
60eb1894 | 3798 | cont: |
20a69341 | 3799 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
3800 | int tmp; |
3801 | ||
37a9cc52 PNA |
3802 | if (!nft_is_active_next(ctx->net, set)) |
3803 | continue; | |
14662917 | 3804 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 3805 | continue; |
60eb1894 | 3806 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 3807 | continue; |
14662917 | 3808 | |
60eb1894 | 3809 | set_bit(tmp - min, inuse); |
20a69341 PM |
3810 | } |
3811 | ||
53b70287 | 3812 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
3813 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
3814 | min += BITS_PER_BYTE * PAGE_SIZE; | |
3815 | memset(inuse, 0, PAGE_SIZE); | |
3816 | goto cont; | |
3817 | } | |
20a69341 PM |
3818 | free_page((unsigned long)inuse); |
3819 | } | |
3820 | ||
38745490 PS |
3821 | set->name = kasprintf(GFP_KERNEL, name, min + n); |
3822 | if (!set->name) | |
3823 | return -ENOMEM; | |
3824 | ||
20a69341 | 3825 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
3826 | if (!nft_is_active_next(ctx->net, i)) |
3827 | continue; | |
e63aaaa6 AY |
3828 | if (!strcmp(set->name, i->name)) { |
3829 | kfree(set->name); | |
7fb6f78d | 3830 | set->name = NULL; |
20a69341 | 3831 | return -ENFILE; |
e63aaaa6 | 3832 | } |
20a69341 PM |
3833 | } |
3834 | return 0; | |
3835 | } | |
3836 | ||
917d80d3 | 3837 | int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
8e1102d5 FW |
3838 | { |
3839 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
3840 | u64 max = (u64)(~((u64)0)); | |
3841 | ||
3842 | max = div_u64(max, NSEC_PER_MSEC); | |
3843 | if (ms >= max) | |
3844 | return -ERANGE; | |
3845 | ||
3846 | ms *= NSEC_PER_MSEC; | |
3847 | *result = nsecs_to_jiffies64(ms); | |
3848 | return 0; | |
3849 | } | |
3850 | ||
917d80d3 | 3851 | __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 3852 | { |
3b15d09f | 3853 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
3854 | } |
3855 | ||
f3a2181e SB |
3856 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
3857 | const struct nft_set *set) | |
3858 | { | |
3859 | struct nlattr *concat, *field; | |
3860 | int i; | |
3861 | ||
3862 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
3863 | if (!concat) | |
3864 | return -ENOMEM; | |
3865 | ||
3866 | for (i = 0; i < set->field_count; i++) { | |
3867 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
3868 | if (!field) | |
3869 | return -ENOMEM; | |
3870 | ||
3871 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
3872 | htonl(set->field_len[i]))) | |
3873 | return -ENOMEM; | |
3874 | ||
3875 | nla_nest_end(skb, field); | |
3876 | } | |
3877 | ||
3878 | nla_nest_end(skb, concat); | |
3879 | ||
3880 | return 0; | |
3881 | } | |
3882 | ||
20a69341 PM |
3883 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
3884 | const struct nft_set *set, u16 event, u16 flags) | |
3885 | { | |
20a69341 | 3886 | struct nlmsghdr *nlh; |
128ad332 | 3887 | u32 portid = ctx->portid; |
65038428 | 3888 | struct nlattr *nest; |
128ad332 | 3889 | u32 seq = ctx->seq; |
48b0ae04 | 3890 | int i; |
20a69341 | 3891 | |
dedb67c4 | 3892 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
3893 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
3894 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
3895 | if (!nlh) | |
20a69341 PM |
3896 | goto nla_put_failure; |
3897 | ||
20a69341 PM |
3898 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
3899 | goto nla_put_failure; | |
3900 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
3901 | goto nla_put_failure; | |
3ecbfd65 HS |
3902 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
3903 | NFTA_SET_PAD)) | |
3904 | goto nla_put_failure; | |
20a69341 PM |
3905 | if (set->flags != 0) |
3906 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
3907 | goto nla_put_failure; | |
3908 | ||
3909 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
3910 | goto nla_put_failure; | |
3911 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
3912 | goto nla_put_failure; | |
3913 | if (set->flags & NFT_SET_MAP) { | |
3914 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
3915 | goto nla_put_failure; | |
3916 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
3917 | goto nla_put_failure; | |
3918 | } | |
8aeff920 PNA |
3919 | if (set->flags & NFT_SET_OBJECT && |
3920 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
3921 | goto nla_put_failure; | |
20a69341 | 3922 | |
761da293 | 3923 | if (set->timeout && |
d3e2a111 | 3924 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
8e1102d5 | 3925 | nf_jiffies64_to_msecs(set->timeout), |
b46f6ded | 3926 | NFTA_SET_PAD)) |
761da293 PM |
3927 | goto nla_put_failure; |
3928 | if (set->gc_int && | |
3929 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) | |
3930 | goto nla_put_failure; | |
3931 | ||
9363dc4b AB |
3932 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
3933 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
3934 | goto nla_put_failure; | |
3935 | } | |
3936 | ||
6f03bf43 PNA |
3937 | if (set->udata && |
3938 | nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) | |
e6d8ecac CFG |
3939 | goto nla_put_failure; |
3940 | ||
65038428 PNA |
3941 | nest = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
3942 | if (!nest) | |
c50b960c PM |
3943 | goto nla_put_failure; |
3944 | if (set->size && | |
3945 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
3946 | goto nla_put_failure; | |
f3a2181e SB |
3947 | |
3948 | if (set->field_count > 1 && | |
3949 | nf_tables_fill_set_concat(skb, set)) | |
3950 | goto nla_put_failure; | |
3951 | ||
65038428 PNA |
3952 | nla_nest_end(skb, nest); |
3953 | ||
8cfd9b0f | 3954 | if (set->num_exprs == 1) { |
65038428 | 3955 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR); |
8cfd9b0f | 3956 | if (nf_tables_fill_expr_info(skb, set->exprs[0]) < 0) |
65038428 PNA |
3957 | goto nla_put_failure; |
3958 | ||
3959 | nla_nest_end(skb, nest); | |
48b0ae04 PNA |
3960 | } else if (set->num_exprs > 1) { |
3961 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS); | |
3962 | if (nest == NULL) | |
65038428 PNA |
3963 | goto nla_put_failure; |
3964 | ||
48b0ae04 PNA |
3965 | for (i = 0; i < set->num_exprs; i++) { |
3966 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, | |
3967 | set->exprs[i]) < 0) | |
3968 | goto nla_put_failure; | |
3969 | } | |
65038428 PNA |
3970 | nla_nest_end(skb, nest); |
3971 | } | |
c50b960c | 3972 | |
053c095a JB |
3973 | nlmsg_end(skb, nlh); |
3974 | return 0; | |
20a69341 PM |
3975 | |
3976 | nla_put_failure: | |
3977 | nlmsg_trim(skb, nlh); | |
3978 | return -1; | |
3979 | } | |
3980 | ||
25e94a99 PNA |
3981 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
3982 | const struct nft_set *set, int event, | |
3983 | gfp_t gfp_flags) | |
20a69341 | 3984 | { |
d59d2f82 | 3985 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
128ad332 | 3986 | u32 portid = ctx->portid; |
6fb721cf PNA |
3987 | struct sk_buff *skb; |
3988 | u16 flags = 0; | |
20a69341 PM |
3989 | int err; |
3990 | ||
128ad332 PNA |
3991 | if (!ctx->report && |
3992 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3993 | return; |
20a69341 | 3994 | |
31f8441c | 3995 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
3996 | if (skb == NULL) |
3997 | goto err; | |
3998 | ||
6fb721cf PNA |
3999 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
4000 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
4001 | ||
4002 | err = nf_tables_fill_set(skb, ctx, set, event, flags); | |
20a69341 PM |
4003 | if (err < 0) { |
4004 | kfree_skb(skb); | |
4005 | goto err; | |
4006 | } | |
4007 | ||
0854db2a | 4008 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 4009 | return; |
20a69341 | 4010 | err: |
25e94a99 | 4011 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
4012 | } |
4013 | ||
5b96af77 | 4014 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
4015 | { |
4016 | const struct nft_set *set; | |
4017 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
4018 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
4019 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 4020 | struct nft_ctx *ctx = cb->data, ctx_set; |
0854db2a | 4021 | struct nftables_pernet *nft_net; |
c9c8e485 PNA |
4022 | |
4023 | if (cb->args[1]) | |
4024 | return skb->len; | |
4025 | ||
e688a7f8 | 4026 | rcu_read_lock(); |
d59d2f82 | 4027 | nft_net = nft_pernet(net); |
0854db2a | 4028 | cb->seq = nft_net->base_seq; |
38e029f1 | 4029 | |
0854db2a | 4030 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 4031 | if (ctx->family != NFPROTO_UNSPEC && |
98319cb9 | 4032 | ctx->family != table->family) |
36596dad PNA |
4033 | continue; |
4034 | ||
4035 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
4036 | continue; |
4037 | ||
36596dad PNA |
4038 | if (cur_table) { |
4039 | if (cur_table != table) | |
c9c8e485 PNA |
4040 | continue; |
4041 | ||
36596dad | 4042 | cur_table = NULL; |
c9c8e485 | 4043 | } |
36596dad PNA |
4044 | idx = 0; |
4045 | list_for_each_entry_rcu(set, &table->sets, list) { | |
4046 | if (idx < s_idx) | |
4047 | goto cont; | |
4048 | if (!nft_is_active(net, set)) | |
4049 | goto cont; | |
5b96af77 | 4050 | |
36596dad PNA |
4051 | ctx_set = *ctx; |
4052 | ctx_set.table = table; | |
98319cb9 | 4053 | ctx_set.family = table->family; |
c9c8e485 | 4054 | |
36596dad PNA |
4055 | if (nf_tables_fill_set(skb, &ctx_set, set, |
4056 | NFT_MSG_NEWSET, | |
4057 | NLM_F_MULTI) < 0) { | |
4058 | cb->args[0] = idx; | |
4059 | cb->args[2] = (unsigned long) table; | |
4060 | goto done; | |
c9c8e485 | 4061 | } |
36596dad | 4062 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 4063 | cont: |
36596dad | 4064 | idx++; |
c9c8e485 | 4065 | } |
36596dad PNA |
4066 | if (s_idx) |
4067 | s_idx = 0; | |
c9c8e485 PNA |
4068 | } |
4069 | cb->args[1] = 1; | |
4070 | done: | |
e688a7f8 | 4071 | rcu_read_unlock(); |
c9c8e485 PNA |
4072 | return skb->len; |
4073 | } | |
4074 | ||
90fd131a FW |
4075 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
4076 | { | |
4077 | struct nft_ctx *ctx_dump = NULL; | |
4078 | ||
4079 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
4080 | if (ctx_dump == NULL) | |
4081 | return -ENOMEM; | |
4082 | ||
4083 | cb->data = ctx_dump; | |
4084 | return 0; | |
4085 | } | |
4086 | ||
5b96af77 | 4087 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 4088 | { |
5b96af77 PNA |
4089 | kfree(cb->data); |
4090 | return 0; | |
20a69341 PM |
4091 | } |
4092 | ||
d9adf22a | 4093 | /* called with rcu_read_lock held */ |
797d4980 PNA |
4094 | static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info, |
4095 | const struct nlattr * const nla[]) | |
20a69341 | 4096 | { |
797d4980 PNA |
4097 | struct netlink_ext_ack *extack = info->extack; |
4098 | u8 genmask = nft_genmask_cur(info->net); | |
67086651 PNA |
4099 | u8 family = info->nfmsg->nfgen_family; |
4100 | struct nft_table *table = NULL; | |
797d4980 | 4101 | struct net *net = info->net; |
20a69341 | 4102 | const struct nft_set *set; |
20a69341 | 4103 | struct sk_buff *skb2; |
797d4980 | 4104 | struct nft_ctx ctx; |
20a69341 PM |
4105 | int err; |
4106 | ||
67086651 PNA |
4107 | if (nla[NFTA_SET_TABLE]) { |
4108 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, | |
4109 | genmask, 0); | |
4110 | if (IS_ERR(table)) { | |
4111 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4112 | return PTR_ERR(table); | |
4113 | } | |
4114 | } | |
4115 | ||
4116 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); | |
20a69341 | 4117 | |
797d4980 | 4118 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
20a69341 | 4119 | struct netlink_dump_control c = { |
90fd131a | 4120 | .start = nf_tables_dump_sets_start, |
20a69341 | 4121 | .dump = nf_tables_dump_sets, |
5b96af77 | 4122 | .done = nf_tables_dump_sets_done, |
90fd131a | 4123 | .data = &ctx, |
d9adf22a | 4124 | .module = THIS_MODULE, |
20a69341 | 4125 | }; |
5b96af77 | 4126 | |
797d4980 | 4127 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
20a69341 PM |
4128 | } |
4129 | ||
c9c8e485 | 4130 | /* Only accept unspec with dump */ |
ef4b65e5 | 4131 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
c9c8e485 | 4132 | return -EAFNOSUPPORT; |
eaa2bcd6 PT |
4133 | if (!nla[NFTA_SET_TABLE]) |
4134 | return -EINVAL; | |
c9c8e485 | 4135 | |
67086651 | 4136 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
4137 | if (IS_ERR(set)) |
4138 | return PTR_ERR(set); | |
4139 | ||
d9adf22a | 4140 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
4141 | if (skb2 == NULL) |
4142 | return -ENOMEM; | |
4143 | ||
4144 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
4145 | if (err < 0) | |
ee921183 | 4146 | goto err_fill_set_info; |
20a69341 | 4147 | |
ee921183 | 4148 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
20a69341 | 4149 | |
ee921183 | 4150 | err_fill_set_info: |
20a69341 PM |
4151 | kfree_skb(skb2); |
4152 | return err; | |
4153 | } | |
4154 | ||
f3a2181e SB |
4155 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
4156 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
4157 | }; | |
4158 | ||
4159 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
4160 | struct nft_set_desc *desc) | |
4161 | { | |
4162 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
4163 | u32 len; | |
4164 | int err; | |
4165 | ||
4166 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, | |
4167 | nft_concat_policy, NULL); | |
4168 | if (err < 0) | |
4169 | return err; | |
4170 | ||
4171 | if (!tb[NFTA_SET_FIELD_LEN]) | |
4172 | return -EINVAL; | |
4173 | ||
4174 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
4175 | ||
4176 | if (len * BITS_PER_BYTE / 32 > NFT_REG32_COUNT) | |
4177 | return -E2BIG; | |
4178 | ||
4179 | desc->field_len[desc->field_count++] = len; | |
4180 | ||
4181 | return 0; | |
4182 | } | |
4183 | ||
4184 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
4185 | const struct nlattr *nla) | |
4186 | { | |
4187 | struct nlattr *attr; | |
4188 | int rem, err; | |
4189 | ||
4190 | nla_for_each_nested(attr, nla, rem) { | |
4191 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
4192 | return -EINVAL; | |
4193 | ||
4194 | err = nft_set_desc_concat_parse(attr, desc); | |
4195 | if (err < 0) | |
4196 | return err; | |
4197 | } | |
4198 | ||
4199 | return 0; | |
4200 | } | |
4201 | ||
f7e840ee | 4202 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
4203 | const struct nlattr *nla) |
4204 | { | |
4205 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
4206 | int err; | |
4207 | ||
8cb08174 JB |
4208 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
4209 | nft_set_desc_policy, NULL); | |
c50b960c PM |
4210 | if (err < 0) |
4211 | return err; | |
4212 | ||
4213 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
4214 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
4215 | if (da[NFTA_SET_DESC_CONCAT]) |
4216 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 4217 | |
f3a2181e | 4218 | return err; |
c50b960c PM |
4219 | } |
4220 | ||
7dab8ee3 PNA |
4221 | static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, |
4222 | const struct nlattr * const nla[]) | |
20a69341 | 4223 | { |
7dab8ee3 PNA |
4224 | u32 ktype, dtype, flags, policy, gc_int, objtype; |
4225 | struct netlink_ext_ack *extack = info->extack; | |
4226 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 4227 | u8 family = info->nfmsg->nfgen_family; |
20a69341 | 4228 | const struct nft_set_ops *ops; |
65038428 | 4229 | struct nft_expr *expr = NULL; |
7dab8ee3 PNA |
4230 | struct net *net = info->net; |
4231 | struct nft_set_desc desc; | |
20a69341 | 4232 | struct nft_table *table; |
7dab8ee3 | 4233 | unsigned char *udata; |
20a69341 PM |
4234 | struct nft_set *set; |
4235 | struct nft_ctx ctx; | |
6c8774a9 | 4236 | size_t alloc_size; |
761da293 | 4237 | u64 timeout; |
7dab8ee3 PNA |
4238 | char *name; |
4239 | int err, i; | |
e6d8ecac | 4240 | u16 udlen; |
7dab8ee3 | 4241 | u64 size; |
20a69341 PM |
4242 | |
4243 | if (nla[NFTA_SET_TABLE] == NULL || | |
4244 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
4245 | nla[NFTA_SET_KEY_LEN] == NULL || |
4246 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
4247 | return -EINVAL; |
4248 | ||
c50b960c PM |
4249 | memset(&desc, 0, sizeof(desc)); |
4250 | ||
20a69341 PM |
4251 | ktype = NFT_DATA_VALUE; |
4252 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { | |
4253 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); | |
4254 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
4255 | return -EINVAL; | |
4256 | } | |
4257 | ||
c50b960c | 4258 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 4259 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4260 | return -EINVAL; |
4261 | ||
4262 | flags = 0; | |
4263 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
4264 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
4265 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 4266 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 | 4267 | NFT_SET_MAP | NFT_SET_EVAL | |
b4e70d8d | 4268 | NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR)) |
d9583cdf | 4269 | return -EOPNOTSUPP; |
8aeff920 | 4270 | /* Only one of these operations is supported */ |
acab7131 FW |
4271 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
4272 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
4273 | return -EOPNOTSUPP; | |
4274 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
4275 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 4276 | return -EOPNOTSUPP; |
20a69341 PM |
4277 | } |
4278 | ||
4279 | dtype = 0; | |
20a69341 PM |
4280 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
4281 | if (!(flags & NFT_SET_MAP)) | |
4282 | return -EINVAL; | |
4283 | ||
4284 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); | |
4285 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
4286 | dtype != NFT_DATA_VERDICT) | |
4287 | return -EINVAL; | |
4288 | ||
4289 | if (dtype != NFT_DATA_VERDICT) { | |
4290 | if (nla[NFTA_SET_DATA_LEN] == NULL) | |
4291 | return -EINVAL; | |
c50b960c | 4292 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 4293 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4294 | return -EINVAL; |
4295 | } else | |
7d740264 | 4296 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
4297 | } else if (flags & NFT_SET_MAP) |
4298 | return -EINVAL; | |
4299 | ||
8aeff920 PNA |
4300 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
4301 | if (!(flags & NFT_SET_OBJECT)) | |
4302 | return -EINVAL; | |
4303 | ||
4304 | objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); | |
4305 | if (objtype == NFT_OBJECT_UNSPEC || | |
4306 | objtype > NFT_OBJECT_MAX) | |
d9583cdf | 4307 | return -EOPNOTSUPP; |
8aeff920 PNA |
4308 | } else if (flags & NFT_SET_OBJECT) |
4309 | return -EINVAL; | |
4310 | else | |
4311 | objtype = NFT_OBJECT_UNSPEC; | |
4312 | ||
761da293 PM |
4313 | timeout = 0; |
4314 | if (nla[NFTA_SET_TIMEOUT] != NULL) { | |
4315 | if (!(flags & NFT_SET_TIMEOUT)) | |
4316 | return -EINVAL; | |
8e1102d5 FW |
4317 | |
4318 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout); | |
4319 | if (err) | |
4320 | return err; | |
761da293 PM |
4321 | } |
4322 | gc_int = 0; | |
4323 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { | |
4324 | if (!(flags & NFT_SET_TIMEOUT)) | |
4325 | return -EINVAL; | |
4326 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); | |
4327 | } | |
4328 | ||
c50b960c PM |
4329 | policy = NFT_SET_POL_PERFORMANCE; |
4330 | if (nla[NFTA_SET_POLICY] != NULL) | |
4331 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); | |
4332 | ||
4333 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4334 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4335 | if (err < 0) |
4336 | return err; | |
4337 | } | |
4338 | ||
48b0ae04 | 4339 | if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS]) |
d56aab26 PNA |
4340 | desc.expr = true; |
4341 | ||
6001a930 PNA |
4342 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask, |
4343 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4344 | if (IS_ERR(table)) { |
4345 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4346 | return PTR_ERR(table); |
36dd1bcc | 4347 | } |
20a69341 | 4348 | |
7dab8ee3 | 4349 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
20a69341 | 4350 | |
cac20fcd | 4351 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4352 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4353 | if (PTR_ERR(set) != -ENOENT) { |
4354 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4355 | return PTR_ERR(set); |
36dd1bcc | 4356 | } |
1a28ad74 | 4357 | } else { |
7dab8ee3 | 4358 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 4359 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); |
20a69341 | 4360 | return -EEXIST; |
36dd1bcc | 4361 | } |
7dab8ee3 | 4362 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
20a69341 | 4363 | return -EOPNOTSUPP; |
36dd1bcc | 4364 | |
20a69341 PM |
4365 | return 0; |
4366 | } | |
4367 | ||
7dab8ee3 | 4368 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
20a69341 PM |
4369 | return -ENOENT; |
4370 | ||
2b664957 | 4371 | ops = nft_select_set_ops(&ctx, nla, &desc, policy); |
20a69341 PM |
4372 | if (IS_ERR(ops)) |
4373 | return PTR_ERR(ops); | |
4374 | ||
e6d8ecac CFG |
4375 | udlen = 0; |
4376 | if (nla[NFTA_SET_USERDATA]) | |
4377 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
4378 | ||
20a69341 PM |
4379 | size = 0; |
4380 | if (ops->privsize != NULL) | |
347b408d | 4381 | size = ops->privsize(nla, &desc); |
6c8774a9 | 4382 | alloc_size = sizeof(*set) + size + udlen; |
45928afe | 4383 | if (alloc_size < size || alloc_size > INT_MAX) |
6c8774a9 ED |
4384 | return -ENOMEM; |
4385 | set = kvzalloc(alloc_size, GFP_KERNEL); | |
24d19826 FW |
4386 | if (!set) |
4387 | return -ENOMEM; | |
20a69341 | 4388 | |
38745490 PS |
4389 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL); |
4390 | if (!name) { | |
4391 | err = -ENOMEM; | |
65038428 | 4392 | goto err_set_name; |
38745490 PS |
4393 | } |
4394 | ||
20a69341 | 4395 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 4396 | kfree(name); |
20a69341 | 4397 | if (err < 0) |
ad9f151e PNA |
4398 | goto err_set_name; |
4399 | ||
4400 | udata = NULL; | |
4401 | if (udlen) { | |
4402 | udata = set->data + size; | |
4403 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
4404 | } | |
4405 | ||
4406 | INIT_LIST_HEAD(&set->bindings); | |
4407 | INIT_LIST_HEAD(&set->catchall_list); | |
4408 | set->table = table; | |
4409 | write_pnet(&set->net, net); | |
4410 | set->ops = ops; | |
4411 | set->ktype = ktype; | |
4412 | set->klen = desc.klen; | |
4413 | set->dtype = dtype; | |
4414 | set->objtype = objtype; | |
4415 | set->dlen = desc.dlen; | |
4416 | set->flags = flags; | |
4417 | set->size = desc.size; | |
4418 | set->policy = policy; | |
4419 | set->udlen = udlen; | |
4420 | set->udata = udata; | |
4421 | set->timeout = timeout; | |
4422 | set->gc_int = gc_int; | |
4423 | ||
4424 | set->field_count = desc.field_count; | |
4425 | for (i = 0; i < desc.field_count; i++) | |
4426 | set->field_len[i] = desc.field_len[i]; | |
4427 | ||
4428 | err = ops->init(set, &desc, nla); | |
4429 | if (err < 0) | |
4430 | goto err_set_init; | |
65038428 PNA |
4431 | |
4432 | if (nla[NFTA_SET_EXPR]) { | |
4433 | expr = nft_set_elem_expr_alloc(&ctx, set, nla[NFTA_SET_EXPR]); | |
4434 | if (IS_ERR(expr)) { | |
4435 | err = PTR_ERR(expr); | |
ad9f151e | 4436 | goto err_set_expr_alloc; |
65038428 | 4437 | } |
8cfd9b0f PNA |
4438 | set->exprs[0] = expr; |
4439 | set->num_exprs++; | |
48b0ae04 PNA |
4440 | } else if (nla[NFTA_SET_EXPRESSIONS]) { |
4441 | struct nft_expr *expr; | |
4442 | struct nlattr *tmp; | |
4443 | int left; | |
4444 | ||
b4e70d8d PNA |
4445 | if (!(flags & NFT_SET_EXPR)) { |
4446 | err = -EINVAL; | |
ad9f151e | 4447 | goto err_set_expr_alloc; |
b4e70d8d | 4448 | } |
48b0ae04 PNA |
4449 | i = 0; |
4450 | nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) { | |
4451 | if (i == NFT_SET_EXPR_MAX) { | |
4452 | err = -E2BIG; | |
ad9f151e | 4453 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4454 | } |
4455 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
4456 | err = -EINVAL; | |
ad9f151e | 4457 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4458 | } |
4459 | expr = nft_set_elem_expr_alloc(&ctx, set, tmp); | |
4460 | if (IS_ERR(expr)) { | |
4461 | err = PTR_ERR(expr); | |
ad9f151e | 4462 | goto err_set_expr_alloc; |
48b0ae04 PNA |
4463 | } |
4464 | set->exprs[i++] = expr; | |
4465 | set->num_exprs++; | |
4466 | } | |
65038428 | 4467 | } |
20a69341 | 4468 | |
3ecbfd65 | 4469 | set->handle = nf_tables_alloc_handle(table); |
20a69341 | 4470 | |
958bee14 | 4471 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 4472 | if (err < 0) |
ad9f151e | 4473 | goto err_set_expr_alloc; |
20a69341 | 4474 | |
e688a7f8 | 4475 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 4476 | table->use++; |
20a69341 PM |
4477 | return 0; |
4478 | ||
ad9f151e | 4479 | err_set_expr_alloc: |
8cfd9b0f PNA |
4480 | for (i = 0; i < set->num_exprs; i++) |
4481 | nft_expr_destroy(&ctx, set->exprs[i]); | |
ad9f151e PNA |
4482 | |
4483 | ops->destroy(set); | |
4484 | err_set_init: | |
2f6adf48 | 4485 | kfree(set->name); |
65038428 | 4486 | err_set_name: |
1ff75a3e | 4487 | kvfree(set); |
20a69341 PM |
4488 | return err; |
4489 | } | |
4490 | ||
aaa31047 PNA |
4491 | struct nft_set_elem_catchall { |
4492 | struct list_head list; | |
4493 | struct rcu_head rcu; | |
4494 | void *elem; | |
4495 | }; | |
4496 | ||
4497 | static void nft_set_catchall_destroy(const struct nft_ctx *ctx, | |
4498 | struct nft_set *set) | |
4499 | { | |
0f7d9b31 | 4500 | struct nft_set_elem_catchall *next, *catchall; |
aaa31047 | 4501 | |
0f7d9b31 | 4502 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
aaa31047 PNA |
4503 | list_del_rcu(&catchall->list); |
4504 | nft_set_elem_destroy(set, catchall->elem, true); | |
ae089831 | 4505 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
4506 | } |
4507 | } | |
4508 | ||
0c2a85ed | 4509 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
20a69341 | 4510 | { |
8cfd9b0f PNA |
4511 | int i; |
4512 | ||
273fe3f1 PNA |
4513 | if (WARN_ON(set->use > 0)) |
4514 | return; | |
4515 | ||
8cfd9b0f PNA |
4516 | for (i = 0; i < set->num_exprs; i++) |
4517 | nft_expr_destroy(ctx, set->exprs[i]); | |
65038428 | 4518 | |
20a69341 | 4519 | set->ops->destroy(set); |
aaa31047 | 4520 | nft_set_catchall_destroy(ctx, set); |
38745490 | 4521 | kfree(set->name); |
1ff75a3e | 4522 | kvfree(set); |
20a69341 PM |
4523 | } |
4524 | ||
7dab8ee3 PNA |
4525 | static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info, |
4526 | const struct nlattr * const nla[]) | |
20a69341 | 4527 | { |
7dab8ee3 PNA |
4528 | struct netlink_ext_ack *extack = info->extack; |
4529 | u8 genmask = nft_genmask_next(info->net); | |
67086651 | 4530 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 4531 | struct net *net = info->net; |
36dd1bcc | 4532 | const struct nlattr *attr; |
67086651 | 4533 | struct nft_table *table; |
20a69341 PM |
4534 | struct nft_set *set; |
4535 | struct nft_ctx ctx; | |
20a69341 | 4536 | |
ef4b65e5 | 4537 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
ec2c9935 | 4538 | return -EAFNOSUPPORT; |
20a69341 | 4539 | |
67086651 PNA |
4540 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
4541 | genmask, NETLINK_CB(skb).portid); | |
4542 | if (IS_ERR(table)) { | |
4543 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4544 | return PTR_ERR(table); | |
4545 | } | |
20a69341 | 4546 | |
36dd1bcc PNA |
4547 | if (nla[NFTA_SET_HANDLE]) { |
4548 | attr = nla[NFTA_SET_HANDLE]; | |
67086651 | 4549 | set = nft_set_lookup_byhandle(table, attr, genmask); |
36dd1bcc PNA |
4550 | } else { |
4551 | attr = nla[NFTA_SET_NAME]; | |
67086651 | 4552 | set = nft_set_lookup(table, attr, genmask); |
36dd1bcc | 4553 | } |
a8278400 | 4554 | |
36dd1bcc PNA |
4555 | if (IS_ERR(set)) { |
4556 | NL_SET_BAD_ATTR(extack, attr); | |
4557 | return PTR_ERR(set); | |
4558 | } | |
273fe3f1 | 4559 | if (set->use || |
7dab8ee3 PNA |
4560 | (info->nlh->nlmsg_flags & NLM_F_NONREC && |
4561 | atomic_read(&set->nelems) > 0)) { | |
36dd1bcc | 4562 | NL_SET_BAD_ATTR(extack, attr); |
20a69341 | 4563 | return -EBUSY; |
36dd1bcc | 4564 | } |
20a69341 | 4565 | |
67086651 PNA |
4566 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
4567 | ||
ee01d542 | 4568 | return nft_delset(&ctx, set); |
20a69341 PM |
4569 | } |
4570 | ||
345023b0 PNA |
4571 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
4572 | enum nft_registers reg, | |
4573 | const struct nft_data *data, | |
4574 | enum nft_data_types type, | |
4575 | unsigned int len); | |
4576 | ||
97c976d6 PNA |
4577 | static int nft_setelem_data_validate(const struct nft_ctx *ctx, |
4578 | struct nft_set *set, | |
4579 | struct nft_set_elem *elem) | |
20a69341 | 4580 | { |
fe2811eb | 4581 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4582 | enum nft_registers dreg; |
4583 | ||
4584 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
4585 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
4586 | set->dtype == NFT_DATA_VERDICT ? | |
4587 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
4588 | set->dlen); | |
20a69341 PM |
4589 | } |
4590 | ||
97c976d6 PNA |
4591 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, |
4592 | struct nft_set *set, | |
4593 | const struct nft_set_iter *iter, | |
4594 | struct nft_set_elem *elem) | |
4595 | { | |
4596 | return nft_setelem_data_validate(ctx, set, elem); | |
4597 | } | |
4598 | ||
aaa31047 PNA |
4599 | static int nft_set_catchall_bind_check(const struct nft_ctx *ctx, |
4600 | struct nft_set *set) | |
4601 | { | |
4602 | u8 genmask = nft_genmask_next(ctx->net); | |
4603 | struct nft_set_elem_catchall *catchall; | |
4604 | struct nft_set_elem elem; | |
4605 | struct nft_set_ext *ext; | |
4606 | int ret = 0; | |
4607 | ||
4608 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
4609 | ext = nft_set_elem_ext(set, catchall->elem); | |
4610 | if (!nft_set_elem_active(ext, genmask)) | |
4611 | continue; | |
4612 | ||
4613 | elem.priv = catchall->elem; | |
4614 | ret = nft_setelem_data_validate(ctx, set, &elem); | |
4615 | if (ret < 0) | |
4616 | break; | |
4617 | } | |
4618 | ||
4619 | return ret; | |
4620 | } | |
4621 | ||
20a69341 PM |
4622 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4623 | struct nft_set_binding *binding) | |
4624 | { | |
4625 | struct nft_set_binding *i; | |
4626 | struct nft_set_iter iter; | |
4627 | ||
273fe3f1 PNA |
4628 | if (set->use == UINT_MAX) |
4629 | return -EOVERFLOW; | |
4630 | ||
408070d6 | 4631 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
4632 | return -EBUSY; |
4633 | ||
11113e19 | 4634 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
4635 | /* If the set is already bound to the same chain all |
4636 | * jumps are already validated for that chain. | |
4637 | */ | |
4638 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 4639 | if (i->flags & NFT_SET_MAP && |
11113e19 | 4640 | i->chain == binding->chain) |
20a69341 PM |
4641 | goto bind; |
4642 | } | |
4643 | ||
8588ac09 | 4644 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
4645 | iter.skip = 0; |
4646 | iter.count = 0; | |
4647 | iter.err = 0; | |
4648 | iter.fn = nf_tables_bind_check_setelem; | |
4649 | ||
4650 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
4651 | if (!iter.err) |
4652 | iter.err = nft_set_catchall_bind_check(ctx, set); | |
4653 | ||
a02f4248 | 4654 | if (iter.err < 0) |
20a69341 | 4655 | return iter.err; |
20a69341 PM |
4656 | } |
4657 | bind: | |
4658 | binding->chain = ctx->chain; | |
e688a7f8 | 4659 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 | 4660 | nft_set_trans_bind(ctx, set); |
273fe3f1 | 4661 | set->use++; |
f6ac8585 | 4662 | |
20a69341 PM |
4663 | return 0; |
4664 | } | |
63aea290 | 4665 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 4666 | |
3b0a081d FW |
4667 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4668 | struct nft_set_binding *binding, bool event) | |
20a69341 | 4669 | { |
e688a7f8 | 4670 | list_del_rcu(&binding->list); |
20a69341 | 4671 | |
f6ac8585 | 4672 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 4673 | list_del_rcu(&set->list); |
f6ac8585 PNA |
4674 | if (event) |
4675 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
4676 | GFP_KERNEL); | |
4677 | } | |
20a69341 PM |
4678 | } |
4679 | ||
273fe3f1 PNA |
4680 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
4681 | struct nft_set_binding *binding, | |
4682 | enum nft_trans_phase phase) | |
4683 | { | |
4684 | switch (phase) { | |
4685 | case NFT_TRANS_PREPARE: | |
4686 | set->use--; | |
4687 | return; | |
4688 | case NFT_TRANS_ABORT: | |
4689 | case NFT_TRANS_RELEASE: | |
4690 | set->use--; | |
954d8297 | 4691 | fallthrough; |
273fe3f1 PNA |
4692 | default: |
4693 | nf_tables_unbind_set(ctx, set, binding, | |
4694 | phase == NFT_TRANS_COMMIT); | |
4695 | } | |
4696 | } | |
4697 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
4698 | ||
cd5125d8 FW |
4699 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
4700 | { | |
f6ac8585 | 4701 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
0c2a85ed | 4702 | nft_set_destroy(ctx, set); |
cd5125d8 FW |
4703 | } |
4704 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
4705 | ||
3ac4c07a PM |
4706 | const struct nft_set_ext_type nft_set_ext_types[] = { |
4707 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 4708 | .align = __alignof__(u32), |
3ac4c07a PM |
4709 | }, |
4710 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 4711 | .align = __alignof__(u32), |
3ac4c07a | 4712 | }, |
563125a7 PNA |
4713 | [NFT_SET_EXT_EXPRESSIONS] = { |
4714 | .align = __alignof__(struct nft_set_elem_expr), | |
f25ad2e9 | 4715 | }, |
8aeff920 PNA |
4716 | [NFT_SET_EXT_OBJREF] = { |
4717 | .len = sizeof(struct nft_object *), | |
4718 | .align = __alignof__(struct nft_object *), | |
4719 | }, | |
3ac4c07a PM |
4720 | [NFT_SET_EXT_FLAGS] = { |
4721 | .len = sizeof(u8), | |
4722 | .align = __alignof__(u8), | |
4723 | }, | |
c3e1b005 PM |
4724 | [NFT_SET_EXT_TIMEOUT] = { |
4725 | .len = sizeof(u64), | |
4726 | .align = __alignof__(u64), | |
4727 | }, | |
4728 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
4729 | .len = sizeof(u64), |
4730 | .align = __alignof__(u64), | |
c3e1b005 | 4731 | }, |
68e942e8 PM |
4732 | [NFT_SET_EXT_USERDATA] = { |
4733 | .len = sizeof(struct nft_userdata), | |
4734 | .align = __alignof__(struct nft_userdata), | |
4735 | }, | |
7b225d0b PNA |
4736 | [NFT_SET_EXT_KEY_END] = { |
4737 | .align = __alignof__(u32), | |
4738 | }, | |
3ac4c07a | 4739 | }; |
3ac4c07a | 4740 | |
20a69341 PM |
4741 | /* |
4742 | * Set elements | |
4743 | */ | |
4744 | ||
4745 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
4746 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
4747 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
4748 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 4749 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 4750 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
4751 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
4752 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 4753 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
4754 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
4755 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 4756 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
48b0ae04 | 4757 | [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED }, |
20a69341 PM |
4758 | }; |
4759 | ||
4760 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
4761 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
4762 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
4763 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
4764 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 4765 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 4766 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
4767 | }; |
4768 | ||
563125a7 PNA |
4769 | static int nft_set_elem_expr_dump(struct sk_buff *skb, |
4770 | const struct nft_set *set, | |
4771 | const struct nft_set_ext *ext) | |
4772 | { | |
4773 | struct nft_set_elem_expr *elem_expr; | |
4774 | u32 size, num_exprs = 0; | |
4775 | struct nft_expr *expr; | |
48b0ae04 | 4776 | struct nlattr *nest; |
563125a7 PNA |
4777 | |
4778 | elem_expr = nft_set_ext_expr(ext); | |
4779 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
4780 | num_exprs++; | |
4781 | ||
4782 | if (num_exprs == 1) { | |
4783 | expr = nft_setelem_expr_at(elem_expr, 0); | |
4784 | if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr) < 0) | |
4785 | return -1; | |
4786 | ||
4787 | return 0; | |
48b0ae04 PNA |
4788 | } else if (num_exprs > 1) { |
4789 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS); | |
4790 | if (nest == NULL) | |
4791 | goto nla_put_failure; | |
563125a7 | 4792 | |
48b0ae04 PNA |
4793 | nft_setelem_expr_foreach(expr, elem_expr, size) { |
4794 | expr = nft_setelem_expr_at(elem_expr, size); | |
4795 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) | |
4796 | goto nla_put_failure; | |
4797 | } | |
4798 | nla_nest_end(skb, nest); | |
4799 | } | |
563125a7 | 4800 | return 0; |
48b0ae04 PNA |
4801 | |
4802 | nla_put_failure: | |
4803 | return -1; | |
563125a7 PNA |
4804 | } |
4805 | ||
20a69341 PM |
4806 | static int nf_tables_fill_setelem(struct sk_buff *skb, |
4807 | const struct nft_set *set, | |
4808 | const struct nft_set_elem *elem) | |
4809 | { | |
fe2811eb | 4810 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4811 | unsigned char *b = skb_tail_pointer(skb); |
4812 | struct nlattr *nest; | |
4813 | ||
ae0be8de | 4814 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
4815 | if (nest == NULL) |
4816 | goto nla_put_failure; | |
4817 | ||
aaa31047 PNA |
4818 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
4819 | nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), | |
fe2811eb | 4820 | NFT_DATA_VALUE, set->klen) < 0) |
20a69341 PM |
4821 | goto nla_put_failure; |
4822 | ||
7b225d0b PNA |
4823 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
4824 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
4825 | NFT_DATA_VALUE, set->klen) < 0) | |
4826 | goto nla_put_failure; | |
4827 | ||
fe2811eb PM |
4828 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
4829 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
4830 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
4831 | set->dlen) < 0) | |
4832 | goto nla_put_failure; | |
4833 | ||
563125a7 PNA |
4834 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) && |
4835 | nft_set_elem_expr_dump(skb, set, ext)) | |
f25ad2e9 PM |
4836 | goto nla_put_failure; |
4837 | ||
8aeff920 PNA |
4838 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
4839 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 4840 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
4841 | goto nla_put_failure; |
4842 | ||
fe2811eb PM |
4843 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
4844 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
4845 | htonl(*nft_set_ext_flags(ext)))) | |
4846 | goto nla_put_failure; | |
20a69341 | 4847 | |
c3e1b005 PM |
4848 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
4849 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 4850 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 4851 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4852 | goto nla_put_failure; |
4853 | ||
4854 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 4855 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
4856 | |
4857 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 4858 | if (time_before64(now, expires)) |
c3e1b005 PM |
4859 | expires -= now; |
4860 | else | |
4861 | expires = 0; | |
4862 | ||
4863 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 4864 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 4865 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4866 | goto nla_put_failure; |
4867 | } | |
4868 | ||
68e942e8 PM |
4869 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
4870 | struct nft_userdata *udata; | |
4871 | ||
4872 | udata = nft_set_ext_userdata(ext); | |
4873 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
4874 | udata->len + 1, udata->data)) | |
4875 | goto nla_put_failure; | |
4876 | } | |
4877 | ||
20a69341 PM |
4878 | nla_nest_end(skb, nest); |
4879 | return 0; | |
4880 | ||
4881 | nla_put_failure: | |
4882 | nlmsg_trim(skb, b); | |
4883 | return -EMSGSIZE; | |
4884 | } | |
4885 | ||
4886 | struct nft_set_dump_args { | |
4887 | const struct netlink_callback *cb; | |
4888 | struct nft_set_iter iter; | |
4889 | struct sk_buff *skb; | |
4890 | }; | |
4891 | ||
4892 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 4893 | struct nft_set *set, |
20a69341 | 4894 | const struct nft_set_iter *iter, |
de70185d | 4895 | struct nft_set_elem *elem) |
20a69341 PM |
4896 | { |
4897 | struct nft_set_dump_args *args; | |
4898 | ||
4899 | args = container_of(iter, struct nft_set_dump_args, iter); | |
4900 | return nf_tables_fill_setelem(args->skb, set, elem); | |
4901 | } | |
4902 | ||
fa803605 LZ |
4903 | struct nft_set_dump_ctx { |
4904 | const struct nft_set *set; | |
4905 | struct nft_ctx ctx; | |
4906 | }; | |
4907 | ||
aaa31047 PNA |
4908 | static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb, |
4909 | const struct nft_set *set) | |
4910 | { | |
4911 | struct nft_set_elem_catchall *catchall; | |
4912 | u8 genmask = nft_genmask_cur(net); | |
4913 | struct nft_set_elem elem; | |
4914 | struct nft_set_ext *ext; | |
4915 | int ret = 0; | |
4916 | ||
4917 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
4918 | ext = nft_set_elem_ext(set, catchall->elem); | |
4919 | if (!nft_set_elem_active(ext, genmask) || | |
4920 | nft_set_elem_expired(ext)) | |
4921 | continue; | |
4922 | ||
4923 | elem.priv = catchall->elem; | |
4924 | ret = nf_tables_fill_setelem(skb, set, &elem); | |
4925 | break; | |
4926 | } | |
4927 | ||
4928 | return ret; | |
4929 | } | |
4930 | ||
20a69341 PM |
4931 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
4932 | { | |
fa803605 | 4933 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 4934 | struct net *net = sock_net(skb->sk); |
0854db2a | 4935 | struct nftables_pernet *nft_net; |
fa803605 | 4936 | struct nft_table *table; |
de70185d | 4937 | struct nft_set *set; |
20a69341 | 4938 | struct nft_set_dump_args args; |
fa803605 | 4939 | bool set_found = false; |
20a69341 PM |
4940 | struct nlmsghdr *nlh; |
4941 | struct nlattr *nest; | |
4942 | u32 portid, seq; | |
fa803605 | 4943 | int event; |
20a69341 | 4944 | |
fa803605 | 4945 | rcu_read_lock(); |
d59d2f82 | 4946 | nft_net = nft_pernet(net); |
0854db2a | 4947 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 4948 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && |
98319cb9 | 4949 | dump_ctx->ctx.family != table->family) |
fa803605 | 4950 | continue; |
20a69341 | 4951 | |
36596dad PNA |
4952 | if (table != dump_ctx->ctx.table) |
4953 | continue; | |
20a69341 | 4954 | |
36596dad PNA |
4955 | list_for_each_entry_rcu(set, &table->sets, list) { |
4956 | if (set == dump_ctx->set) { | |
4957 | set_found = true; | |
4958 | break; | |
fa803605 | 4959 | } |
fa803605 LZ |
4960 | } |
4961 | break; | |
4962 | } | |
4963 | ||
4964 | if (!set_found) { | |
4965 | rcu_read_unlock(); | |
4966 | return -ENOENT; | |
4967 | } | |
20a69341 | 4968 | |
dedb67c4 | 4969 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
4970 | portid = NETLINK_CB(cb->skb).portid; |
4971 | seq = cb->nlh->nlmsg_seq; | |
4972 | ||
19c28b13 PNA |
4973 | nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI, |
4974 | table->family, NFNETLINK_V0, nft_base_seq(net)); | |
4975 | if (!nlh) | |
20a69341 PM |
4976 | goto nla_put_failure; |
4977 | ||
fa803605 | 4978 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
4979 | goto nla_put_failure; |
4980 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
4981 | goto nla_put_failure; | |
4982 | ||
ae0be8de | 4983 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
4984 | if (nest == NULL) |
4985 | goto nla_put_failure; | |
4986 | ||
8588ac09 PNA |
4987 | args.cb = cb; |
4988 | args.skb = skb; | |
fa803605 | 4989 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
4990 | args.iter.skip = cb->args[0]; |
4991 | args.iter.count = 0; | |
4992 | args.iter.err = 0; | |
4993 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 | 4994 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
aaa31047 PNA |
4995 | |
4996 | if (!args.iter.err && args.iter.count == cb->args[0]) | |
4997 | args.iter.err = nft_set_catchall_dump(net, skb, set); | |
fa803605 | 4998 | rcu_read_unlock(); |
20a69341 PM |
4999 | |
5000 | nla_nest_end(skb, nest); | |
5001 | nlmsg_end(skb, nlh); | |
5002 | ||
5003 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
5004 | return args.iter.err; | |
5005 | if (args.iter.count == cb->args[0]) | |
5006 | return 0; | |
5007 | ||
5008 | cb->args[0] = args.iter.count; | |
5009 | return skb->len; | |
5010 | ||
5011 | nla_put_failure: | |
fa803605 | 5012 | rcu_read_unlock(); |
20a69341 PM |
5013 | return -ENOSPC; |
5014 | } | |
5015 | ||
90fd131a FW |
5016 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
5017 | { | |
5018 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
5019 | ||
5020 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
5021 | ||
5022 | return cb->data ? 0 : -ENOMEM; | |
5023 | } | |
5024 | ||
fa803605 LZ |
5025 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
5026 | { | |
5027 | kfree(cb->data); | |
5028 | return 0; | |
5029 | } | |
5030 | ||
d60ce62f AB |
5031 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
5032 | const struct nft_ctx *ctx, u32 seq, | |
5033 | u32 portid, int event, u16 flags, | |
5034 | const struct nft_set *set, | |
5035 | const struct nft_set_elem *elem) | |
5036 | { | |
d60ce62f AB |
5037 | struct nlmsghdr *nlh; |
5038 | struct nlattr *nest; | |
5039 | int err; | |
5040 | ||
dedb67c4 | 5041 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
5042 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
5043 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
5044 | if (!nlh) | |
d60ce62f AB |
5045 | goto nla_put_failure; |
5046 | ||
d60ce62f AB |
5047 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
5048 | goto nla_put_failure; | |
5049 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
5050 | goto nla_put_failure; | |
5051 | ||
ae0be8de | 5052 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
5053 | if (nest == NULL) |
5054 | goto nla_put_failure; | |
5055 | ||
5056 | err = nf_tables_fill_setelem(skb, set, elem); | |
5057 | if (err < 0) | |
5058 | goto nla_put_failure; | |
5059 | ||
5060 | nla_nest_end(skb, nest); | |
5061 | ||
053c095a JB |
5062 | nlmsg_end(skb, nlh); |
5063 | return 0; | |
d60ce62f AB |
5064 | |
5065 | nla_put_failure: | |
5066 | nlmsg_trim(skb, nlh); | |
5067 | return -1; | |
5068 | } | |
5069 | ||
ba0e4d99 PNA |
5070 | static int nft_setelem_parse_flags(const struct nft_set *set, |
5071 | const struct nlattr *attr, u32 *flags) | |
5072 | { | |
5073 | if (attr == NULL) | |
5074 | return 0; | |
5075 | ||
5076 | *flags = ntohl(nla_get_be32(attr)); | |
aaa31047 PNA |
5077 | if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) |
5078 | return -EOPNOTSUPP; | |
ba0e4d99 PNA |
5079 | if (!(set->flags & NFT_SET_INTERVAL) && |
5080 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
5081 | return -EINVAL; | |
5082 | ||
5083 | return 0; | |
5084 | } | |
5085 | ||
20a1452c PNA |
5086 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
5087 | struct nft_data *key, struct nlattr *attr) | |
5088 | { | |
5089 | struct nft_data_desc desc; | |
5090 | int err; | |
5091 | ||
5092 | err = nft_data_init(ctx, key, NFT_DATA_VALUE_MAXLEN, &desc, attr); | |
5093 | if (err < 0) | |
5094 | return err; | |
5095 | ||
5096 | if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) { | |
5097 | nft_data_release(key, desc.type); | |
5098 | return -EINVAL; | |
5099 | } | |
5100 | ||
5101 | return 0; | |
5102 | } | |
5103 | ||
fdb9c405 PNA |
5104 | static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, |
5105 | struct nft_data_desc *desc, | |
5106 | struct nft_data *data, | |
5107 | struct nlattr *attr) | |
5108 | { | |
5109 | int err; | |
5110 | ||
5111 | err = nft_data_init(ctx, data, NFT_DATA_VALUE_MAXLEN, desc, attr); | |
5112 | if (err < 0) | |
5113 | return err; | |
5114 | ||
5115 | if (desc->type != NFT_DATA_VERDICT && desc->len != set->dlen) { | |
5116 | nft_data_release(data, desc->type); | |
5117 | return -EINVAL; | |
5118 | } | |
5119 | ||
5120 | return 0; | |
5121 | } | |
5122 | ||
aaa31047 PNA |
5123 | static void *nft_setelem_catchall_get(const struct net *net, |
5124 | const struct nft_set *set) | |
5125 | { | |
5126 | struct nft_set_elem_catchall *catchall; | |
5127 | u8 genmask = nft_genmask_cur(net); | |
5128 | struct nft_set_ext *ext; | |
5129 | void *priv = NULL; | |
5130 | ||
5131 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5132 | ext = nft_set_elem_ext(set, catchall->elem); | |
5133 | if (!nft_set_elem_active(ext, genmask) || | |
5134 | nft_set_elem_expired(ext)) | |
5135 | continue; | |
5136 | ||
5137 | priv = catchall->elem; | |
5138 | break; | |
5139 | } | |
5140 | ||
5141 | return priv; | |
5142 | } | |
5143 | ||
5144 | static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set, | |
5145 | struct nft_set_elem *elem, u32 flags) | |
5146 | { | |
5147 | void *priv; | |
5148 | ||
5149 | if (!(flags & NFT_SET_ELEM_CATCHALL)) { | |
5150 | priv = set->ops->get(ctx->net, set, elem, flags); | |
5151 | if (IS_ERR(priv)) | |
5152 | return PTR_ERR(priv); | |
5153 | } else { | |
5154 | priv = nft_setelem_catchall_get(ctx->net, set); | |
5155 | if (!priv) | |
5156 | return -ENOENT; | |
5157 | } | |
5158 | elem->priv = priv; | |
5159 | ||
5160 | return 0; | |
5161 | } | |
5162 | ||
ba0e4d99 PNA |
5163 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
5164 | const struct nlattr *attr) | |
5165 | { | |
5166 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
5167 | struct nft_set_elem elem; |
5168 | struct sk_buff *skb; | |
5169 | uint32_t flags = 0; | |
ba0e4d99 PNA |
5170 | int err; |
5171 | ||
8cb08174 JB |
5172 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5173 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
5174 | if (err < 0) |
5175 | return err; | |
5176 | ||
ba0e4d99 PNA |
5177 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5178 | if (err < 0) | |
5179 | return err; | |
5180 | ||
aaa31047 PNA |
5181 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
5182 | return -EINVAL; | |
5183 | ||
5184 | if (nla[NFTA_SET_ELEM_KEY]) { | |
5185 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
5186 | nla[NFTA_SET_ELEM_KEY]); | |
5187 | if (err < 0) | |
5188 | return err; | |
5189 | } | |
ba0e4d99 | 5190 | |
7b225d0b PNA |
5191 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5192 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5193 | nla[NFTA_SET_ELEM_KEY_END]); | |
5194 | if (err < 0) | |
5195 | return err; | |
5196 | } | |
5197 | ||
aaa31047 PNA |
5198 | err = nft_setelem_get(ctx, set, &elem, flags); |
5199 | if (err < 0) | |
5200 | return err; | |
ba0e4d99 PNA |
5201 | |
5202 | err = -ENOMEM; | |
d9adf22a | 5203 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 | 5204 | if (skb == NULL) |
ee921183 | 5205 | return err; |
ba0e4d99 PNA |
5206 | |
5207 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
5208 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
5209 | if (err < 0) | |
ee921183 | 5210 | goto err_fill_setelem; |
ba0e4d99 | 5211 | |
ee921183 | 5212 | return nfnetlink_unicast(skb, ctx->net, ctx->portid); |
ba0e4d99 | 5213 | |
ee921183 | 5214 | err_fill_setelem: |
ba0e4d99 | 5215 | kfree_skb(skb); |
ee921183 | 5216 | return err; |
ba0e4d99 PNA |
5217 | } |
5218 | ||
d9adf22a | 5219 | /* called with rcu_read_lock held */ |
797d4980 PNA |
5220 | static int nf_tables_getsetelem(struct sk_buff *skb, |
5221 | const struct nfnl_info *info, | |
5222 | const struct nlattr * const nla[]) | |
ba0e4d99 | 5223 | { |
797d4980 PNA |
5224 | struct netlink_ext_ack *extack = info->extack; |
5225 | u8 genmask = nft_genmask_cur(info->net); | |
e2b750d7 | 5226 | u8 family = info->nfmsg->nfgen_family; |
797d4980 | 5227 | struct net *net = info->net; |
e2b750d7 | 5228 | struct nft_table *table; |
ba0e4d99 PNA |
5229 | struct nft_set *set; |
5230 | struct nlattr *attr; | |
5231 | struct nft_ctx ctx; | |
5232 | int rem, err = 0; | |
5233 | ||
e2b750d7 PNA |
5234 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
5235 | genmask, NETLINK_CB(skb).portid); | |
5236 | if (IS_ERR(table)) { | |
5237 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
5238 | return PTR_ERR(table); | |
5239 | } | |
ba0e4d99 | 5240 | |
e2b750d7 | 5241 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
5242 | if (IS_ERR(set)) |
5243 | return PTR_ERR(set); | |
5244 | ||
e2b750d7 PNA |
5245 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5246 | ||
797d4980 | 5247 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
ba0e4d99 | 5248 | struct netlink_dump_control c = { |
90fd131a | 5249 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
5250 | .dump = nf_tables_dump_set, |
5251 | .done = nf_tables_dump_set_done, | |
d9adf22a | 5252 | .module = THIS_MODULE, |
ba0e4d99 | 5253 | }; |
90fd131a FW |
5254 | struct nft_set_dump_ctx dump_ctx = { |
5255 | .set = set, | |
5256 | .ctx = ctx, | |
5257 | }; | |
ba0e4d99 | 5258 | |
90fd131a | 5259 | c.data = &dump_ctx; |
797d4980 | 5260 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
ba0e4d99 PNA |
5261 | } |
5262 | ||
5263 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
5264 | return -EINVAL; | |
5265 | ||
5266 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
5267 | err = nft_get_set_elem(&ctx, set, attr); | |
5268 | if (err < 0) | |
5269 | break; | |
5270 | } | |
5271 | ||
5272 | return err; | |
5273 | } | |
5274 | ||
25e94a99 PNA |
5275 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
5276 | const struct nft_set *set, | |
5277 | const struct nft_set_elem *elem, | |
6fb721cf | 5278 | int event) |
d60ce62f | 5279 | { |
0854db2a | 5280 | struct nftables_pernet *nft_net; |
128ad332 PNA |
5281 | struct net *net = ctx->net; |
5282 | u32 portid = ctx->portid; | |
d60ce62f | 5283 | struct sk_buff *skb; |
6fb721cf | 5284 | u16 flags = 0; |
d60ce62f AB |
5285 | int err; |
5286 | ||
128ad332 | 5287 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 5288 | return; |
d60ce62f | 5289 | |
d60ce62f AB |
5290 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
5291 | if (skb == NULL) | |
5292 | goto err; | |
5293 | ||
6fb721cf PNA |
5294 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
5295 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
5296 | ||
d60ce62f AB |
5297 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, |
5298 | set, elem); | |
5299 | if (err < 0) { | |
5300 | kfree_skb(skb); | |
5301 | goto err; | |
5302 | } | |
5303 | ||
d59d2f82 | 5304 | nft_net = nft_pernet(net); |
0854db2a | 5305 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 5306 | return; |
d60ce62f | 5307 | err: |
25e94a99 | 5308 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
5309 | } |
5310 | ||
60319eb1 PNA |
5311 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
5312 | int msg_type, | |
5313 | struct nft_set *set) | |
5314 | { | |
5315 | struct nft_trans *trans; | |
5316 | ||
5317 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
5318 | if (trans == NULL) | |
5319 | return NULL; | |
5320 | ||
5321 | nft_trans_elem_set(trans) = set; | |
5322 | return trans; | |
5323 | } | |
5324 | ||
a7fc9368 PNA |
5325 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
5326 | const struct nft_set *set, | |
5327 | const struct nlattr *attr) | |
5328 | { | |
5329 | struct nft_expr *expr; | |
5330 | int err; | |
5331 | ||
5332 | expr = nft_expr_init(ctx, attr); | |
5333 | if (IS_ERR(expr)) | |
5334 | return expr; | |
5335 | ||
5336 | err = -EOPNOTSUPP; | |
5337 | if (!(expr->ops->type->flags & NFT_EXPR_STATEFUL)) | |
5338 | goto err_set_elem_expr; | |
5339 | ||
5340 | if (expr->ops->type->flags & NFT_EXPR_GC) { | |
5341 | if (set->flags & NFT_SET_TIMEOUT) | |
5342 | goto err_set_elem_expr; | |
5343 | if (!set->ops->gc_init) | |
5344 | goto err_set_elem_expr; | |
5345 | set->ops->gc_init(set); | |
5346 | } | |
5347 | ||
5348 | return expr; | |
5349 | ||
5350 | err_set_elem_expr: | |
5351 | nft_expr_destroy(ctx, expr); | |
5352 | return ERR_PTR(err); | |
5353 | } | |
5354 | ||
22fe54d5 PM |
5355 | void *nft_set_elem_init(const struct nft_set *set, |
5356 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
5357 | const u32 *key, const u32 *key_end, |
5358 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
5359 | { |
5360 | struct nft_set_ext *ext; | |
5361 | void *elem; | |
5362 | ||
5363 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
5364 | if (elem == NULL) | |
5365 | return NULL; | |
5366 | ||
5367 | ext = nft_set_elem_ext(set, elem); | |
5368 | nft_set_ext_init(ext, tmpl); | |
5369 | ||
aaa31047 PNA |
5370 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY)) |
5371 | memcpy(nft_set_ext_key(ext), key, set->klen); | |
7b225d0b PNA |
5372 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END)) |
5373 | memcpy(nft_set_ext_key_end(ext), key_end, set->klen); | |
fe2811eb PM |
5374 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
5375 | memcpy(nft_set_ext_data(ext), data, set->dlen); | |
79ebb5bb LGL |
5376 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
5377 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
5378 | if (expiration == 0) | |
5379 | *nft_set_ext_expiration(ext) += timeout; | |
5380 | } | |
c3e1b005 PM |
5381 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
5382 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
5383 | |
5384 | return elem; | |
5385 | } | |
5386 | ||
563125a7 PNA |
5387 | static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5388 | struct nft_expr *expr) | |
475beb9c PNA |
5389 | { |
5390 | if (expr->ops->destroy_clone) { | |
5391 | expr->ops->destroy_clone(ctx, expr); | |
5392 | module_put(expr->ops->type->owner); | |
5393 | } else { | |
5394 | nf_tables_expr_destroy(ctx, expr); | |
5395 | } | |
5396 | } | |
5397 | ||
563125a7 PNA |
5398 | static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
5399 | struct nft_set_elem_expr *elem_expr) | |
5400 | { | |
5401 | struct nft_expr *expr; | |
5402 | u32 size; | |
5403 | ||
5404 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
5405 | __nft_set_elem_expr_destroy(ctx, expr); | |
5406 | } | |
5407 | ||
61f9e292 LZ |
5408 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
5409 | bool destroy_expr) | |
61edafbb PM |
5410 | { |
5411 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
5412 | struct nft_ctx ctx = { |
5413 | .net = read_pnet(&set->net), | |
5414 | .family = set->table->family, | |
5415 | }; | |
61edafbb | 5416 | |
59105446 | 5417 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 5418 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 5419 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
563125a7 | 5420 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c | 5421 | nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext)); |
371ebcbb | 5422 | |
8aeff920 PNA |
5423 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
5424 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
5425 | kfree(elem); |
5426 | } | |
5427 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
5428 | ||
f8bb7889 PNA |
5429 | /* Only called from commit path, nft_setelem_data_deactivate() already deals |
5430 | * with the refcounting from the preparation phase. | |
59105446 | 5431 | */ |
3453c927 PNA |
5432 | static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
5433 | const struct nft_set *set, void *elem) | |
59105446 PNA |
5434 | { |
5435 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
5436 | ||
563125a7 | 5437 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c PNA |
5438 | nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext)); |
5439 | ||
59105446 PNA |
5440 | kfree(elem); |
5441 | } | |
5442 | ||
fca05d4d PNA |
5443 | int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
5444 | struct nft_expr *expr_array[]) | |
8cfd9b0f PNA |
5445 | { |
5446 | struct nft_expr *expr; | |
5447 | int err, i, k; | |
5448 | ||
5449 | for (i = 0; i < set->num_exprs; i++) { | |
5450 | expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL); | |
5451 | if (!expr) | |
5452 | goto err_expr; | |
5453 | ||
5454 | err = nft_expr_clone(expr, set->exprs[i]); | |
5455 | if (err < 0) { | |
5456 | nft_expr_destroy(ctx, expr); | |
5457 | goto err_expr; | |
5458 | } | |
5459 | expr_array[i] = expr; | |
5460 | } | |
5461 | ||
5462 | return 0; | |
5463 | ||
5464 | err_expr: | |
161b838e CIK |
5465 | for (k = i - 1; k >= 0; k--) |
5466 | nft_expr_destroy(ctx, expr_array[k]); | |
8cfd9b0f PNA |
5467 | |
5468 | return -ENOMEM; | |
5469 | } | |
5470 | ||
4d8f9065 PNA |
5471 | static int nft_set_elem_expr_setup(struct nft_ctx *ctx, |
5472 | const struct nft_set_ext *ext, | |
5473 | struct nft_expr *expr_array[], | |
5474 | u32 num_exprs) | |
563125a7 PNA |
5475 | { |
5476 | struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext); | |
4d8f9065 PNA |
5477 | struct nft_expr *expr; |
5478 | int i, err; | |
5479 | ||
5480 | for (i = 0; i < num_exprs; i++) { | |
5481 | expr = nft_setelem_expr_at(elem_expr, elem_expr->size); | |
5482 | err = nft_expr_clone(expr, expr_array[i]); | |
5483 | if (err < 0) | |
5484 | goto err_elem_expr_setup; | |
5485 | ||
5486 | elem_expr->size += expr_array[i]->ops->size; | |
5487 | nft_expr_destroy(ctx, expr_array[i]); | |
5488 | expr_array[i] = NULL; | |
5489 | } | |
5490 | ||
5491 | return 0; | |
5492 | ||
5493 | err_elem_expr_setup: | |
5494 | for (; i < num_exprs; i++) { | |
5495 | nft_expr_destroy(ctx, expr_array[i]); | |
5496 | expr_array[i] = NULL; | |
5497 | } | |
563125a7 | 5498 | |
4d8f9065 | 5499 | return -ENOMEM; |
563125a7 PNA |
5500 | } |
5501 | ||
aaa31047 PNA |
5502 | struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
5503 | const struct nft_set *set) | |
5504 | { | |
5505 | struct nft_set_elem_catchall *catchall; | |
5506 | u8 genmask = nft_genmask_cur(net); | |
5507 | struct nft_set_ext *ext; | |
5508 | ||
5509 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5510 | ext = nft_set_elem_ext(set, catchall->elem); | |
5511 | if (nft_set_elem_active(ext, genmask) && | |
5512 | !nft_set_elem_expired(ext)) | |
5513 | return ext; | |
5514 | } | |
5515 | ||
5516 | return NULL; | |
5517 | } | |
5518 | EXPORT_SYMBOL_GPL(nft_set_catchall_lookup); | |
5519 | ||
5520 | void *nft_set_catchall_gc(const struct nft_set *set) | |
5521 | { | |
5522 | struct nft_set_elem_catchall *catchall, *next; | |
5523 | struct nft_set_ext *ext; | |
5524 | void *elem = NULL; | |
5525 | ||
5526 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5527 | ext = nft_set_elem_ext(set, catchall->elem); | |
5528 | ||
5529 | if (!nft_set_elem_expired(ext) || | |
5530 | nft_set_elem_mark_busy(ext)) | |
5531 | continue; | |
5532 | ||
5533 | elem = catchall->elem; | |
5534 | list_del_rcu(&catchall->list); | |
5535 | kfree_rcu(catchall, rcu); | |
5536 | break; | |
5537 | } | |
5538 | ||
5539 | return elem; | |
5540 | } | |
5541 | EXPORT_SYMBOL_GPL(nft_set_catchall_gc); | |
5542 | ||
5543 | static int nft_setelem_catchall_insert(const struct net *net, | |
5544 | struct nft_set *set, | |
5545 | const struct nft_set_elem *elem, | |
5546 | struct nft_set_ext **pext) | |
5547 | { | |
5548 | struct nft_set_elem_catchall *catchall; | |
5549 | u8 genmask = nft_genmask_next(net); | |
5550 | struct nft_set_ext *ext; | |
5551 | ||
5552 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5553 | ext = nft_set_elem_ext(set, catchall->elem); | |
5554 | if (nft_set_elem_active(ext, genmask)) { | |
5555 | *pext = ext; | |
5556 | return -EEXIST; | |
5557 | } | |
5558 | } | |
5559 | ||
5560 | catchall = kmalloc(sizeof(*catchall), GFP_KERNEL); | |
5561 | if (!catchall) | |
5562 | return -ENOMEM; | |
5563 | ||
5564 | catchall->elem = elem->priv; | |
5565 | list_add_tail_rcu(&catchall->list, &set->catchall_list); | |
5566 | ||
5567 | return 0; | |
5568 | } | |
5569 | ||
5570 | static int nft_setelem_insert(const struct net *net, | |
5571 | struct nft_set *set, | |
5572 | const struct nft_set_elem *elem, | |
5573 | struct nft_set_ext **ext, unsigned int flags) | |
5574 | { | |
5575 | int ret; | |
5576 | ||
5577 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5578 | ret = nft_setelem_catchall_insert(net, set, elem, ext); | |
5579 | else | |
5580 | ret = set->ops->insert(net, set, elem, ext); | |
5581 | ||
5582 | return ret; | |
5583 | } | |
5584 | ||
5585 | static bool nft_setelem_is_catchall(const struct nft_set *set, | |
5586 | const struct nft_set_elem *elem) | |
5587 | { | |
5588 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5589 | ||
5590 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
5591 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL) | |
5592 | return true; | |
5593 | ||
5594 | return false; | |
5595 | } | |
5596 | ||
5597 | static void nft_setelem_activate(struct net *net, struct nft_set *set, | |
5598 | struct nft_set_elem *elem) | |
5599 | { | |
5600 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5601 | ||
5602 | if (nft_setelem_is_catchall(set, elem)) { | |
5603 | nft_set_elem_change_active(net, set, ext); | |
5604 | nft_set_elem_clear_busy(ext); | |
5605 | } else { | |
5606 | set->ops->activate(net, set, elem); | |
5607 | } | |
5608 | } | |
5609 | ||
5610 | static int nft_setelem_catchall_deactivate(const struct net *net, | |
5611 | struct nft_set *set, | |
5612 | struct nft_set_elem *elem) | |
5613 | { | |
5614 | struct nft_set_elem_catchall *catchall; | |
5615 | struct nft_set_ext *ext; | |
5616 | ||
5617 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5618 | ext = nft_set_elem_ext(set, catchall->elem); | |
5619 | if (!nft_is_active(net, ext) || | |
5620 | nft_set_elem_mark_busy(ext)) | |
5621 | continue; | |
5622 | ||
5623 | kfree(elem->priv); | |
5624 | elem->priv = catchall->elem; | |
5625 | nft_set_elem_change_active(net, set, ext); | |
5626 | return 0; | |
5627 | } | |
5628 | ||
5629 | return -ENOENT; | |
5630 | } | |
5631 | ||
5632 | static int __nft_setelem_deactivate(const struct net *net, | |
5633 | struct nft_set *set, | |
5634 | struct nft_set_elem *elem) | |
5635 | { | |
5636 | void *priv; | |
5637 | ||
5638 | priv = set->ops->deactivate(net, set, elem); | |
5639 | if (!priv) | |
5640 | return -ENOENT; | |
5641 | ||
5642 | kfree(elem->priv); | |
5643 | elem->priv = priv; | |
5644 | set->ndeact++; | |
5645 | ||
5646 | return 0; | |
5647 | } | |
5648 | ||
5649 | static int nft_setelem_deactivate(const struct net *net, | |
5650 | struct nft_set *set, | |
5651 | struct nft_set_elem *elem, u32 flags) | |
5652 | { | |
5653 | int ret; | |
5654 | ||
5655 | if (flags & NFT_SET_ELEM_CATCHALL) | |
5656 | ret = nft_setelem_catchall_deactivate(net, set, elem); | |
5657 | else | |
5658 | ret = __nft_setelem_deactivate(net, set, elem); | |
5659 | ||
5660 | return ret; | |
5661 | } | |
5662 | ||
5663 | static void nft_setelem_catchall_remove(const struct net *net, | |
5664 | const struct nft_set *set, | |
5665 | const struct nft_set_elem *elem) | |
5666 | { | |
5667 | struct nft_set_elem_catchall *catchall, *next; | |
5668 | ||
5669 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
5670 | if (catchall->elem == elem->priv) { | |
5671 | list_del_rcu(&catchall->list); | |
ae089831 | 5672 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
5673 | break; |
5674 | } | |
5675 | } | |
5676 | } | |
5677 | ||
5678 | static void nft_setelem_remove(const struct net *net, | |
5679 | const struct nft_set *set, | |
5680 | const struct nft_set_elem *elem) | |
5681 | { | |
5682 | if (nft_setelem_is_catchall(set, elem)) | |
5683 | nft_setelem_catchall_remove(net, set, elem); | |
5684 | else | |
5685 | set->ops->remove(net, set, elem); | |
5686 | } | |
5687 | ||
60319eb1 | 5688 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 5689 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 | 5690 | { |
8cfd9b0f | 5691 | struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {}; |
20a69341 | 5692 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
8aeff920 | 5693 | u8 genmask = nft_genmask_next(ctx->net); |
664899e8 | 5694 | u32 flags = 0, size = 0, num_exprs = 0; |
fe2811eb | 5695 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 5696 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
5697 | struct nft_set_elem elem; |
5698 | struct nft_set_binding *binding; | |
8aeff920 | 5699 | struct nft_object *obj = NULL; |
68e942e8 | 5700 | struct nft_userdata *udata; |
20a1452c | 5701 | struct nft_data_desc desc; |
20a69341 | 5702 | enum nft_registers dreg; |
60319eb1 | 5703 | struct nft_trans *trans; |
c3e1b005 | 5704 | u64 timeout; |
79ebb5bb | 5705 | u64 expiration; |
8cfd9b0f | 5706 | int err, i; |
68e942e8 | 5707 | u8 ulen; |
20a69341 | 5708 | |
8cb08174 JB |
5709 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5710 | nft_set_elem_policy, NULL); | |
20a69341 PM |
5711 | if (err < 0) |
5712 | return err; | |
5713 | ||
fe2811eb PM |
5714 | nft_set_ext_prepare(&tmpl); |
5715 | ||
0e9091d6 PNA |
5716 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5717 | if (err < 0) | |
5718 | return err; | |
aaa31047 PNA |
5719 | |
5720 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
5721 | return -EINVAL; | |
5722 | ||
0e9091d6 PNA |
5723 | if (flags != 0) |
5724 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
20a69341 PM |
5725 | |
5726 | if (set->flags & NFT_SET_MAP) { | |
5727 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 5728 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
5729 | return -EINVAL; |
5730 | } else { | |
5731 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
5732 | return -EINVAL; | |
5733 | } | |
5734 | ||
bffc124b PNA |
5735 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
5736 | (nla[NFTA_SET_ELEM_DATA] || | |
5737 | nla[NFTA_SET_ELEM_OBJREF] || | |
5738 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
5739 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
5740 | nla[NFTA_SET_ELEM_USERDATA] || | |
48b0ae04 PNA |
5741 | nla[NFTA_SET_ELEM_EXPR] || |
5742 | nla[NFTA_SET_ELEM_EXPRESSIONS])) | |
bffc124b PNA |
5743 | return -EINVAL; |
5744 | ||
c3e1b005 PM |
5745 | timeout = 0; |
5746 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
5747 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
5748 | return -EINVAL; | |
8e1102d5 FW |
5749 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
5750 | &timeout); | |
5751 | if (err) | |
5752 | return err; | |
c3e1b005 PM |
5753 | } else if (set->flags & NFT_SET_TIMEOUT) { |
5754 | timeout = set->timeout; | |
5755 | } | |
5756 | ||
79ebb5bb LGL |
5757 | expiration = 0; |
5758 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
5759 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
5760 | return -EINVAL; | |
5761 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
5762 | &expiration); | |
5763 | if (err) | |
5764 | return err; | |
5765 | } | |
5766 | ||
8cfd9b0f PNA |
5767 | if (nla[NFTA_SET_ELEM_EXPR]) { |
5768 | struct nft_expr *expr; | |
5769 | ||
664899e8 | 5770 | if (set->num_exprs && set->num_exprs != 1) |
8cfd9b0f PNA |
5771 | return -EOPNOTSUPP; |
5772 | ||
40944452 PNA |
5773 | expr = nft_set_elem_expr_alloc(ctx, set, |
5774 | nla[NFTA_SET_ELEM_EXPR]); | |
5775 | if (IS_ERR(expr)) | |
5776 | return PTR_ERR(expr); | |
65038428 | 5777 | |
8cfd9b0f | 5778 | expr_array[0] = expr; |
664899e8 | 5779 | num_exprs = 1; |
65038428 | 5780 | |
664899e8 | 5781 | if (set->num_exprs && set->exprs[0]->ops != expr->ops) { |
8cfd9b0f | 5782 | err = -EOPNOTSUPP; |
65038428 | 5783 | goto err_set_elem_expr; |
8cfd9b0f | 5784 | } |
48b0ae04 PNA |
5785 | } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) { |
5786 | struct nft_expr *expr; | |
5787 | struct nlattr *tmp; | |
5788 | int left; | |
65038428 | 5789 | |
48b0ae04 PNA |
5790 | i = 0; |
5791 | nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) { | |
664899e8 PNA |
5792 | if (i == NFT_SET_EXPR_MAX || |
5793 | (set->num_exprs && set->num_exprs == i)) { | |
48b0ae04 PNA |
5794 | err = -E2BIG; |
5795 | goto err_set_elem_expr; | |
5796 | } | |
5797 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
5798 | err = -EINVAL; | |
5799 | goto err_set_elem_expr; | |
5800 | } | |
5801 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
5802 | if (IS_ERR(expr)) { | |
5803 | err = PTR_ERR(expr); | |
5804 | goto err_set_elem_expr; | |
5805 | } | |
5806 | expr_array[i] = expr; | |
664899e8 | 5807 | num_exprs++; |
48b0ae04 | 5808 | |
664899e8 | 5809 | if (set->num_exprs && expr->ops != set->exprs[i]->ops) { |
48b0ae04 PNA |
5810 | err = -EOPNOTSUPP; |
5811 | goto err_set_elem_expr; | |
5812 | } | |
5813 | i++; | |
5814 | } | |
664899e8 | 5815 | if (set->num_exprs && set->num_exprs != i) { |
48b0ae04 | 5816 | err = -EOPNOTSUPP; |
65038428 | 5817 | goto err_set_elem_expr; |
48b0ae04 | 5818 | } |
8cfd9b0f PNA |
5819 | } else if (set->num_exprs > 0) { |
5820 | err = nft_set_elem_expr_clone(ctx, set, expr_array); | |
5821 | if (err < 0) | |
5822 | goto err_set_elem_expr_clone; | |
664899e8 PNA |
5823 | |
5824 | num_exprs = set->num_exprs; | |
40944452 PNA |
5825 | } |
5826 | ||
aaa31047 PNA |
5827 | if (nla[NFTA_SET_ELEM_KEY]) { |
5828 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
5829 | nla[NFTA_SET_ELEM_KEY]); | |
5830 | if (err < 0) | |
5831 | goto err_set_elem_expr; | |
20a69341 | 5832 | |
aaa31047 PNA |
5833 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
5834 | } | |
7b225d0b PNA |
5835 | |
5836 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
5837 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5838 | nla[NFTA_SET_ELEM_KEY_END]); | |
5839 | if (err < 0) | |
5840 | goto err_parse_key; | |
5841 | ||
5842 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); | |
5843 | } | |
5844 | ||
c3e1b005 PM |
5845 | if (timeout > 0) { |
5846 | nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); | |
5847 | if (timeout != set->timeout) | |
5848 | nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); | |
5849 | } | |
fe2811eb | 5850 | |
664899e8 PNA |
5851 | if (num_exprs) { |
5852 | for (i = 0; i < num_exprs; i++) | |
563125a7 PNA |
5853 | size += expr_array[i]->ops->size; |
5854 | ||
5855 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS, | |
5856 | sizeof(struct nft_set_elem_expr) + | |
5857 | size); | |
5858 | } | |
40944452 | 5859 | |
8aeff920 PNA |
5860 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
5861 | if (!(set->flags & NFT_SET_OBJECT)) { | |
5862 | err = -EINVAL; | |
7b225d0b | 5863 | goto err_parse_key_end; |
8aeff920 | 5864 | } |
4d44175a FW |
5865 | obj = nft_obj_lookup(ctx->net, ctx->table, |
5866 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 5867 | set->objtype, genmask); |
8aeff920 PNA |
5868 | if (IS_ERR(obj)) { |
5869 | err = PTR_ERR(obj); | |
7b225d0b | 5870 | goto err_parse_key_end; |
8aeff920 PNA |
5871 | } |
5872 | nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); | |
5873 | } | |
5874 | ||
20a69341 | 5875 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
fdb9c405 PNA |
5876 | err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val, |
5877 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 | 5878 | if (err < 0) |
7b225d0b | 5879 | goto err_parse_key_end; |
20a69341 | 5880 | |
20a69341 PM |
5881 | dreg = nft_type_to_reg(set->dtype); |
5882 | list_for_each_entry(binding, &set->bindings, list) { | |
5883 | struct nft_ctx bind_ctx = { | |
58c78e10 | 5884 | .net = ctx->net, |
36596dad | 5885 | .family = ctx->family, |
20a69341 | 5886 | .table = ctx->table, |
7c95f6d8 | 5887 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
5888 | }; |
5889 | ||
11113e19 PM |
5890 | if (!(binding->flags & NFT_SET_MAP)) |
5891 | continue; | |
5892 | ||
1ec10212 | 5893 | err = nft_validate_register_store(&bind_ctx, dreg, |
fdb9c405 | 5894 | &elem.data.val, |
20a1452c | 5895 | desc.type, desc.len); |
20a69341 | 5896 | if (err < 0) |
7b225d0b | 5897 | goto err_parse_data; |
a654de8f | 5898 | |
20a1452c | 5899 | if (desc.type == NFT_DATA_VERDICT && |
fdb9c405 PNA |
5900 | (elem.data.val.verdict.code == NFT_GOTO || |
5901 | elem.data.val.verdict.code == NFT_JUMP)) | |
a654de8f PNA |
5902 | nft_validate_state_update(ctx->net, |
5903 | NFT_VALIDATE_NEED); | |
20a69341 | 5904 | } |
fe2811eb | 5905 | |
20a1452c | 5906 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
20a69341 PM |
5907 | } |
5908 | ||
68e942e8 PM |
5909 | /* The full maximum length of userdata can exceed the maximum |
5910 | * offset value (U8_MAX) for following extensions, therefor it | |
5911 | * must be the last extension added. | |
5912 | */ | |
5913 | ulen = 0; | |
5914 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
5915 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
5916 | if (ulen > 0) | |
5917 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
5918 | ulen); | |
5919 | } | |
5920 | ||
fe2811eb | 5921 | err = -ENOMEM; |
7b225d0b | 5922 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
fdb9c405 | 5923 | elem.key_end.val.data, elem.data.val.data, |
79ebb5bb | 5924 | timeout, expiration, GFP_KERNEL); |
fe2811eb | 5925 | if (elem.priv == NULL) |
7b225d0b | 5926 | goto err_parse_data; |
fe2811eb PM |
5927 | |
5928 | ext = nft_set_elem_ext(set, elem.priv); | |
5929 | if (flags) | |
5930 | *nft_set_ext_flags(ext) = flags; | |
68e942e8 PM |
5931 | if (ulen > 0) { |
5932 | udata = nft_set_ext_userdata(ext); | |
5933 | udata->len = ulen - 1; | |
5934 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
5935 | } | |
8aeff920 PNA |
5936 | if (obj) { |
5937 | *nft_set_ext_obj(ext) = obj; | |
5938 | obj->use++; | |
5939 | } | |
4d8f9065 PNA |
5940 | err = nft_set_elem_expr_setup(ctx, ext, expr_array, num_exprs); |
5941 | if (err < 0) | |
5942 | goto err_elem_expr; | |
fe2811eb | 5943 | |
60319eb1 | 5944 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
4d8f9065 PNA |
5945 | if (trans == NULL) { |
5946 | err = -ENOMEM; | |
5947 | goto err_elem_expr; | |
5948 | } | |
60319eb1 | 5949 | |
69086658 | 5950 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
aaa31047 PNA |
5951 | |
5952 | err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags); | |
c016c7e4 PNA |
5953 | if (err) { |
5954 | if (err == -EEXIST) { | |
9744a6fc PNA |
5955 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
5956 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
5957 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
77a92189 | 5958 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) |
7b225d0b | 5959 | goto err_element_clash; |
8aeff920 PNA |
5960 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
5961 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
5962 | memcmp(nft_set_ext_data(ext), | |
5963 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
5964 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
5965 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
5966 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
77a92189 | 5967 | goto err_element_clash; |
c016c7e4 PNA |
5968 | else if (!(nlmsg_flags & NLM_F_EXCL)) |
5969 | err = 0; | |
8c2d45b2 PNA |
5970 | } else if (err == -ENOTEMPTY) { |
5971 | /* ENOTEMPTY reports overlapping between this element | |
5972 | * and an existing one. | |
5973 | */ | |
5974 | err = -EEXIST; | |
c016c7e4 | 5975 | } |
7b225d0b | 5976 | goto err_element_clash; |
c016c7e4 | 5977 | } |
20a69341 | 5978 | |
aaa31047 | 5979 | if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size && |
35d0ac90 PNA |
5980 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { |
5981 | err = -ENFILE; | |
7b225d0b | 5982 | goto err_set_full; |
35d0ac90 PNA |
5983 | } |
5984 | ||
60319eb1 | 5985 | nft_trans_elem(trans) = elem; |
0854db2a | 5986 | nft_trans_commit_list_add_tail(ctx->net, trans); |
20a69341 PM |
5987 | return 0; |
5988 | ||
7b225d0b | 5989 | err_set_full: |
aaa31047 | 5990 | nft_setelem_remove(ctx->net, set, &elem); |
7b225d0b | 5991 | err_element_clash: |
60319eb1 | 5992 | kfree(trans); |
4d8f9065 | 5993 | err_elem_expr: |
b91d9036 TY |
5994 | if (obj) |
5995 | obj->use--; | |
475beb9c PNA |
5996 | |
5997 | nf_tables_set_elem_destroy(ctx, set, elem.priv); | |
7b225d0b | 5998 | err_parse_data: |
20a69341 | 5999 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
fdb9c405 | 6000 | nft_data_release(&elem.data.val, desc.type); |
7b225d0b PNA |
6001 | err_parse_key_end: |
6002 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
6003 | err_parse_key: | |
20a1452c | 6004 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
40944452 | 6005 | err_set_elem_expr: |
664899e8 | 6006 | for (i = 0; i < num_exprs && expr_array[i]; i++) |
8cfd9b0f PNA |
6007 | nft_expr_destroy(ctx, expr_array[i]); |
6008 | err_set_elem_expr_clone: | |
20a69341 PM |
6009 | return err; |
6010 | } | |
6011 | ||
7dab8ee3 PNA |
6012 | static int nf_tables_newsetelem(struct sk_buff *skb, |
6013 | const struct nfnl_info *info, | |
6014 | const struct nlattr * const nla[]) | |
20a69341 | 6015 | { |
7dab8ee3 PNA |
6016 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
6017 | struct netlink_ext_ack *extack = info->extack; | |
6018 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6019 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6020 | struct net *net = info->net; |
20a69341 | 6021 | const struct nlattr *attr; |
e2b750d7 | 6022 | struct nft_table *table; |
20a69341 PM |
6023 | struct nft_set *set; |
6024 | struct nft_ctx ctx; | |
a654de8f | 6025 | int rem, err; |
20a69341 | 6026 | |
7d5570ca PNA |
6027 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
6028 | return -EINVAL; | |
6029 | ||
e2b750d7 PNA |
6030 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6031 | genmask, NETLINK_CB(skb).portid); | |
6032 | if (IS_ERR(table)) { | |
6033 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6034 | return PTR_ERR(table); | |
6035 | } | |
20a69341 | 6036 | |
e2b750d7 | 6037 | set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET], |
a3073c17 PNA |
6038 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); |
6039 | if (IS_ERR(set)) | |
6040 | return PTR_ERR(set); | |
958bee14 | 6041 | |
20a69341 PM |
6042 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
6043 | return -EBUSY; | |
6044 | ||
e2b750d7 PNA |
6045 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6046 | ||
20a69341 | 6047 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7dab8ee3 | 6048 | err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags); |
35d0ac90 | 6049 | if (err < 0) |
a654de8f | 6050 | return err; |
20a69341 | 6051 | } |
a654de8f | 6052 | |
0854db2a | 6053 | if (nft_net->validate_state == NFT_VALIDATE_DO) |
e2b750d7 | 6054 | return nft_table_validate(net, table); |
a654de8f PNA |
6055 | |
6056 | return 0; | |
20a69341 PM |
6057 | } |
6058 | ||
59105446 PNA |
6059 | /** |
6060 | * nft_data_hold - hold a nft_data item | |
6061 | * | |
6062 | * @data: struct nft_data to release | |
6063 | * @type: type of data | |
6064 | * | |
6065 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
6066 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
6067 | * NFT_GOTO verdicts. This function must be called on active data objects | |
6068 | * from the second phase of the commit protocol. | |
6069 | */ | |
bb7b40ae | 6070 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 | 6071 | { |
d0e2c7de PNA |
6072 | struct nft_chain *chain; |
6073 | struct nft_rule *rule; | |
6074 | ||
59105446 PNA |
6075 | if (type == NFT_DATA_VERDICT) { |
6076 | switch (data->verdict.code) { | |
6077 | case NFT_JUMP: | |
6078 | case NFT_GOTO: | |
d0e2c7de PNA |
6079 | chain = data->verdict.chain; |
6080 | chain->use++; | |
6081 | ||
6082 | if (!nft_chain_is_bound(chain)) | |
6083 | break; | |
6084 | ||
6085 | chain->table->use++; | |
6086 | list_for_each_entry(rule, &chain->rules, list) | |
6087 | chain->use++; | |
6088 | ||
6089 | nft_chain_add(chain->table, chain); | |
59105446 PNA |
6090 | break; |
6091 | } | |
6092 | } | |
6093 | } | |
6094 | ||
f8bb7889 PNA |
6095 | static void nft_setelem_data_activate(const struct net *net, |
6096 | const struct nft_set *set, | |
6097 | struct nft_set_elem *elem) | |
59105446 PNA |
6098 | { |
6099 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6100 | ||
6101 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6102 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
6103 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6104 | (*nft_set_ext_obj(ext))->use++; | |
6105 | } | |
6106 | ||
f8bb7889 PNA |
6107 | static void nft_setelem_data_deactivate(const struct net *net, |
6108 | const struct nft_set *set, | |
6109 | struct nft_set_elem *elem) | |
59105446 PNA |
6110 | { |
6111 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6112 | ||
6113 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6114 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
6115 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
6116 | (*nft_set_ext_obj(ext))->use--; | |
6117 | } | |
6118 | ||
60319eb1 | 6119 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
6120 | const struct nlattr *attr) |
6121 | { | |
6122 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 6123 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 6124 | struct nft_set_elem elem; |
3971ca14 | 6125 | struct nft_set_ext *ext; |
60319eb1 | 6126 | struct nft_trans *trans; |
3971ca14 | 6127 | u32 flags = 0; |
20a69341 PM |
6128 | int err; |
6129 | ||
8cb08174 JB |
6130 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6131 | nft_set_elem_policy, NULL); | |
20a69341 | 6132 | if (err < 0) |
20a1452c | 6133 | return err; |
20a69341 | 6134 | |
aaa31047 PNA |
6135 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6136 | if (err < 0) | |
6137 | return err; | |
6138 | ||
6139 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
20a1452c | 6140 | return -EINVAL; |
20a69341 | 6141 | |
3971ca14 PNA |
6142 | nft_set_ext_prepare(&tmpl); |
6143 | ||
3971ca14 PNA |
6144 | if (flags != 0) |
6145 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6146 | ||
aaa31047 PNA |
6147 | if (nla[NFTA_SET_ELEM_KEY]) { |
6148 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6149 | nla[NFTA_SET_ELEM_KEY]); | |
6150 | if (err < 0) | |
6151 | return err; | |
20a69341 | 6152 | |
aaa31047 PNA |
6153 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6154 | } | |
3971ca14 | 6155 | |
7b225d0b PNA |
6156 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
6157 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6158 | nla[NFTA_SET_ELEM_KEY_END]); | |
6159 | if (err < 0) | |
6160 | return err; | |
6161 | ||
6162 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); | |
6163 | } | |
6164 | ||
3971ca14 | 6165 | err = -ENOMEM; |
7b225d0b PNA |
6166 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
6167 | elem.key_end.val.data, NULL, 0, 0, | |
6168 | GFP_KERNEL); | |
3971ca14 | 6169 | if (elem.priv == NULL) |
20a1452c | 6170 | goto fail_elem; |
3971ca14 PNA |
6171 | |
6172 | ext = nft_set_elem_ext(set, elem.priv); | |
6173 | if (flags) | |
6174 | *nft_set_ext_flags(ext) = flags; | |
6175 | ||
60319eb1 | 6176 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
6177 | if (trans == NULL) |
6178 | goto fail_trans; | |
20a69341 | 6179 | |
aaa31047 PNA |
6180 | err = nft_setelem_deactivate(ctx->net, set, &elem, flags); |
6181 | if (err < 0) | |
20a1452c | 6182 | goto fail_ops; |
cc02e457 | 6183 | |
f8bb7889 | 6184 | nft_setelem_data_deactivate(ctx->net, set, &elem); |
59105446 | 6185 | |
60319eb1 | 6186 | nft_trans_elem(trans) = elem; |
0854db2a | 6187 | nft_trans_commit_list_add_tail(ctx->net, trans); |
0dc13625 | 6188 | return 0; |
cc02e457 | 6189 | |
20a1452c | 6190 | fail_ops: |
cc02e457 | 6191 | kfree(trans); |
20a1452c | 6192 | fail_trans: |
3971ca14 | 6193 | kfree(elem.priv); |
20a1452c PNA |
6194 | fail_elem: |
6195 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
6196 | return err; |
6197 | } | |
6198 | ||
e6ba7cb6 PNA |
6199 | static int nft_setelem_flush(const struct nft_ctx *ctx, |
6200 | struct nft_set *set, | |
6201 | const struct nft_set_iter *iter, | |
6202 | struct nft_set_elem *elem) | |
8411b644 PNA |
6203 | { |
6204 | struct nft_trans *trans; | |
6205 | int err; | |
6206 | ||
6207 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6208 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
6209 | if (!trans) | |
6210 | return -ENOMEM; | |
6211 | ||
1ba1c414 | 6212 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
6213 | err = -ENOENT; |
6214 | goto err1; | |
6215 | } | |
b2c11e4b | 6216 | set->ndeact++; |
8411b644 | 6217 | |
f8bb7889 | 6218 | nft_setelem_data_deactivate(ctx->net, set, elem); |
de70185d PNA |
6219 | nft_trans_elem_set(trans) = set; |
6220 | nft_trans_elem(trans) = *elem; | |
0854db2a | 6221 | nft_trans_commit_list_add_tail(ctx->net, trans); |
8411b644 PNA |
6222 | |
6223 | return 0; | |
6224 | err1: | |
6225 | kfree(trans); | |
6226 | return err; | |
6227 | } | |
6228 | ||
aaa31047 PNA |
6229 | static int __nft_set_catchall_flush(const struct nft_ctx *ctx, |
6230 | struct nft_set *set, | |
6231 | struct nft_set_elem *elem) | |
6232 | { | |
6233 | struct nft_trans *trans; | |
6234 | ||
6235 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
6236 | sizeof(struct nft_trans_elem), GFP_KERNEL); | |
6237 | if (!trans) | |
6238 | return -ENOMEM; | |
6239 | ||
6240 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
6241 | nft_trans_elem_set(trans) = set; | |
6242 | nft_trans_elem(trans) = *elem; | |
6243 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
6244 | ||
6245 | return 0; | |
6246 | } | |
6247 | ||
6248 | static int nft_set_catchall_flush(const struct nft_ctx *ctx, | |
6249 | struct nft_set *set) | |
6250 | { | |
6251 | u8 genmask = nft_genmask_next(ctx->net); | |
6252 | struct nft_set_elem_catchall *catchall; | |
6253 | struct nft_set_elem elem; | |
6254 | struct nft_set_ext *ext; | |
6255 | int ret = 0; | |
6256 | ||
6257 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
6258 | ext = nft_set_elem_ext(set, catchall->elem); | |
6259 | if (!nft_set_elem_active(ext, genmask) || | |
6260 | nft_set_elem_mark_busy(ext)) | |
6261 | continue; | |
6262 | ||
6263 | elem.priv = catchall->elem; | |
6264 | ret = __nft_set_catchall_flush(ctx, set, &elem); | |
6265 | if (ret < 0) | |
6266 | break; | |
6267 | } | |
6268 | ||
6269 | return ret; | |
6270 | } | |
6271 | ||
e6ba7cb6 PNA |
6272 | static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) |
6273 | { | |
6274 | struct nft_set_iter iter = { | |
6275 | .genmask = genmask, | |
6276 | .fn = nft_setelem_flush, | |
6277 | }; | |
6278 | ||
6279 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
6280 | if (!iter.err) |
6281 | iter.err = nft_set_catchall_flush(ctx, set); | |
e6ba7cb6 PNA |
6282 | |
6283 | return iter.err; | |
6284 | } | |
6285 | ||
7dab8ee3 PNA |
6286 | static int nf_tables_delsetelem(struct sk_buff *skb, |
6287 | const struct nfnl_info *info, | |
6288 | const struct nlattr * const nla[]) | |
20a69341 | 6289 | { |
7dab8ee3 PNA |
6290 | struct netlink_ext_ack *extack = info->extack; |
6291 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6292 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6293 | struct net *net = info->net; |
20a69341 | 6294 | const struct nlattr *attr; |
e2b750d7 | 6295 | struct nft_table *table; |
20a69341 PM |
6296 | struct nft_set *set; |
6297 | struct nft_ctx ctx; | |
60319eb1 | 6298 | int rem, err = 0; |
20a69341 | 6299 | |
e2b750d7 PNA |
6300 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6301 | genmask, NETLINK_CB(skb).portid); | |
6302 | if (IS_ERR(table)) { | |
6303 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6304 | return PTR_ERR(table); | |
6305 | } | |
20a69341 | 6306 | |
e2b750d7 | 6307 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
6308 | if (IS_ERR(set)) |
6309 | return PTR_ERR(set); | |
6310 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
6311 | return -EBUSY; | |
6312 | ||
e2b750d7 PNA |
6313 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6314 | ||
e6ba7cb6 PNA |
6315 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) |
6316 | return nft_set_flush(&ctx, set, genmask); | |
8411b644 | 6317 | |
20a69341 PM |
6318 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
6319 | err = nft_del_setelem(&ctx, set, attr); | |
6320 | if (err < 0) | |
60319eb1 | 6321 | break; |
20a69341 | 6322 | } |
60319eb1 | 6323 | return err; |
20a69341 PM |
6324 | } |
6325 | ||
cfed7e1b PM |
6326 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
6327 | { | |
6328 | struct nft_set_gc_batch *gcb; | |
6329 | unsigned int i; | |
6330 | ||
6331 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
6332 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 6333 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
6334 | kfree(gcb); |
6335 | } | |
cfed7e1b PM |
6336 | |
6337 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
6338 | gfp_t gfp) | |
6339 | { | |
6340 | struct nft_set_gc_batch *gcb; | |
6341 | ||
6342 | gcb = kzalloc(sizeof(*gcb), gfp); | |
6343 | if (gcb == NULL) | |
6344 | return gcb; | |
6345 | gcb->head.set = set; | |
6346 | return gcb; | |
6347 | } | |
cfed7e1b | 6348 | |
e5009240 PNA |
6349 | /* |
6350 | * Stateful objects | |
6351 | */ | |
6352 | ||
6353 | /** | |
6354 | * nft_register_obj- register nf_tables stateful object type | |
3db86c39 | 6355 | * @obj_type: object type |
e5009240 PNA |
6356 | * |
6357 | * Registers the object type for use with nf_tables. Returns zero on | |
6358 | * success or a negative errno code otherwise. | |
6359 | */ | |
6360 | int nft_register_obj(struct nft_object_type *obj_type) | |
6361 | { | |
6362 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
6363 | return -EINVAL; | |
6364 | ||
6365 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6366 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
6367 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6368 | return 0; | |
6369 | } | |
6370 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
6371 | ||
6372 | /** | |
6373 | * nft_unregister_obj - unregister nf_tables object type | |
3db86c39 | 6374 | * @obj_type: object type |
e5009240 PNA |
6375 | * |
6376 | * Unregisters the object type for use with nf_tables. | |
6377 | */ | |
6378 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
6379 | { | |
6380 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6381 | list_del_rcu(&obj_type->list); | |
6382 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6383 | } | |
6384 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
6385 | ||
4d44175a FW |
6386 | struct nft_object *nft_obj_lookup(const struct net *net, |
6387 | const struct nft_table *table, | |
cac20fcd PNA |
6388 | const struct nlattr *nla, u32 objtype, |
6389 | u8 genmask) | |
e5009240 | 6390 | { |
4d44175a FW |
6391 | struct nft_object_hash_key k = { .table = table }; |
6392 | char search[NFT_OBJ_MAXNAMELEN]; | |
6393 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
6394 | struct nft_object *obj; |
6395 | ||
872f6903 | 6396 | nla_strscpy(search, nla, sizeof(search)); |
4d44175a FW |
6397 | k.name = search; |
6398 | ||
6399 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
6400 | !lockdep_commit_lock_is_held(net)); | |
6401 | ||
6402 | rcu_read_lock(); | |
6403 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
6404 | if (!list) | |
6405 | goto out; | |
6406 | ||
6407 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
6408 | if (objtype == obj->ops->type->type && | |
6409 | nft_active_genmask(obj, genmask)) { | |
6410 | rcu_read_unlock(); | |
e5009240 | 6411 | return obj; |
4d44175a | 6412 | } |
e5009240 | 6413 | } |
4d44175a FW |
6414 | out: |
6415 | rcu_read_unlock(); | |
e5009240 PNA |
6416 | return ERR_PTR(-ENOENT); |
6417 | } | |
cac20fcd | 6418 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 6419 | |
cac20fcd PNA |
6420 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
6421 | const struct nlattr *nla, | |
6422 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
6423 | { |
6424 | struct nft_object *obj; | |
6425 | ||
6426 | list_for_each_entry(obj, &table->objects, list) { | |
6427 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
6428 | objtype == obj->ops->type->type && | |
6429 | nft_active_genmask(obj, genmask)) | |
6430 | return obj; | |
6431 | } | |
6432 | return ERR_PTR(-ENOENT); | |
6433 | } | |
6434 | ||
e5009240 | 6435 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
6436 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
6437 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
6438 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
6439 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
6440 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
6441 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 6442 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
b131c964 JGG |
6443 | [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY, |
6444 | .len = NFT_USERDATA_MAXLEN }, | |
e5009240 PNA |
6445 | }; |
6446 | ||
84fba055 FW |
6447 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
6448 | const struct nft_object_type *type, | |
e5009240 PNA |
6449 | const struct nlattr *attr) |
6450 | { | |
5b4c6e38 | 6451 | struct nlattr **tb; |
dfc46034 | 6452 | const struct nft_object_ops *ops; |
e5009240 | 6453 | struct nft_object *obj; |
5b4c6e38 GS |
6454 | int err = -ENOMEM; |
6455 | ||
6456 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
6457 | if (!tb) | |
6458 | goto err1; | |
e5009240 PNA |
6459 | |
6460 | if (attr) { | |
8cb08174 JB |
6461 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
6462 | type->policy, NULL); | |
e5009240 | 6463 | if (err < 0) |
5b4c6e38 | 6464 | goto err2; |
e5009240 PNA |
6465 | } else { |
6466 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
6467 | } | |
6468 | ||
dfc46034 PBG |
6469 | if (type->select_ops) { |
6470 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
6471 | if (IS_ERR(ops)) { | |
6472 | err = PTR_ERR(ops); | |
5b4c6e38 | 6473 | goto err2; |
dfc46034 PBG |
6474 | } |
6475 | } else { | |
6476 | ops = type->ops; | |
6477 | } | |
6478 | ||
e5009240 | 6479 | err = -ENOMEM; |
dfc46034 | 6480 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL); |
5b4c6e38 GS |
6481 | if (!obj) |
6482 | goto err2; | |
e5009240 | 6483 | |
dfc46034 | 6484 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 6485 | if (err < 0) |
5b4c6e38 | 6486 | goto err3; |
e5009240 | 6487 | |
dfc46034 PBG |
6488 | obj->ops = ops; |
6489 | ||
5b4c6e38 | 6490 | kfree(tb); |
e5009240 | 6491 | return obj; |
5b4c6e38 | 6492 | err3: |
e5009240 | 6493 | kfree(obj); |
5b4c6e38 GS |
6494 | err2: |
6495 | kfree(tb); | |
e5009240 PNA |
6496 | err1: |
6497 | return ERR_PTR(err); | |
6498 | } | |
6499 | ||
6500 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 6501 | struct nft_object *obj, bool reset) |
e5009240 PNA |
6502 | { |
6503 | struct nlattr *nest; | |
6504 | ||
ae0be8de | 6505 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
6506 | if (!nest) |
6507 | goto nla_put_failure; | |
dfc46034 | 6508 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
6509 | goto nla_put_failure; |
6510 | nla_nest_end(skb, nest); | |
6511 | return 0; | |
6512 | ||
6513 | nla_put_failure: | |
6514 | return -1; | |
6515 | } | |
6516 | ||
6517 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
6518 | { | |
6519 | const struct nft_object_type *type; | |
6520 | ||
6521 | list_for_each_entry(type, &nf_tables_objects, list) { | |
6522 | if (objtype == type->type) | |
6523 | return type; | |
6524 | } | |
6525 | return NULL; | |
6526 | } | |
6527 | ||
452238e8 FW |
6528 | static const struct nft_object_type * |
6529 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
6530 | { |
6531 | const struct nft_object_type *type; | |
6532 | ||
6533 | type = __nft_obj_type_get(objtype); | |
6534 | if (type != NULL && try_module_get(type->owner)) | |
6535 | return type; | |
6536 | ||
f102d66b | 6537 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
6538 | #ifdef CONFIG_MODULES |
6539 | if (type == NULL) { | |
eb014de4 | 6540 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
6541 | return ERR_PTR(-EAGAIN); |
6542 | } | |
6543 | #endif | |
6544 | return ERR_PTR(-ENOENT); | |
6545 | } | |
6546 | ||
d62d0ba9 FFM |
6547 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
6548 | const struct nft_object_type *type, | |
6549 | const struct nlattr *attr, | |
6550 | struct nft_object *obj) | |
6551 | { | |
6552 | struct nft_object *newobj; | |
6553 | struct nft_trans *trans; | |
dad3bdee FW |
6554 | int err = -ENOMEM; |
6555 | ||
6556 | if (!try_module_get(type->owner)) | |
6557 | return -ENOENT; | |
d62d0ba9 FFM |
6558 | |
6559 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
6560 | sizeof(struct nft_trans_obj)); | |
6561 | if (!trans) | |
dad3bdee | 6562 | goto err_trans; |
d62d0ba9 FFM |
6563 | |
6564 | newobj = nft_obj_init(ctx, type, attr); | |
6565 | if (IS_ERR(newobj)) { | |
6566 | err = PTR_ERR(newobj); | |
b74ae961 | 6567 | goto err_free_trans; |
d62d0ba9 FFM |
6568 | } |
6569 | ||
6570 | nft_trans_obj(trans) = obj; | |
6571 | nft_trans_obj_update(trans) = true; | |
6572 | nft_trans_obj_newobj(trans) = newobj; | |
0854db2a | 6573 | nft_trans_commit_list_add_tail(ctx->net, trans); |
d62d0ba9 FFM |
6574 | |
6575 | return 0; | |
b74ae961 DC |
6576 | |
6577 | err_free_trans: | |
d62d0ba9 | 6578 | kfree(trans); |
dad3bdee FW |
6579 | err_trans: |
6580 | module_put(type->owner); | |
d62d0ba9 FFM |
6581 | return err; |
6582 | } | |
6583 | ||
7dab8ee3 PNA |
6584 | static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info, |
6585 | const struct nlattr * const nla[]) | |
e5009240 | 6586 | { |
7dab8ee3 PNA |
6587 | struct netlink_ext_ack *extack = info->extack; |
6588 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 6589 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 6590 | const struct nft_object_type *type; |
7dab8ee3 | 6591 | struct net *net = info->net; |
e5009240 PNA |
6592 | struct nft_table *table; |
6593 | struct nft_object *obj; | |
6594 | struct nft_ctx ctx; | |
6595 | u32 objtype; | |
6596 | int err; | |
6597 | ||
6598 | if (!nla[NFTA_OBJ_TYPE] || | |
6599 | !nla[NFTA_OBJ_NAME] || | |
6600 | !nla[NFTA_OBJ_DATA]) | |
6601 | return -EINVAL; | |
6602 | ||
6001a930 PNA |
6603 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
6604 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
6605 | if (IS_ERR(table)) { |
6606 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 6607 | return PTR_ERR(table); |
36dd1bcc | 6608 | } |
e5009240 PNA |
6609 | |
6610 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 6611 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
6612 | if (IS_ERR(obj)) { |
6613 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
6614 | if (err != -ENOENT) { |
6615 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 6616 | return err; |
36dd1bcc | 6617 | } |
1a28ad74 | 6618 | } else { |
7dab8ee3 | 6619 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 6620 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); |
e5009240 | 6621 | return -EEXIST; |
36dd1bcc | 6622 | } |
7dab8ee3 | 6623 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
d62d0ba9 FFM |
6624 | return -EOPNOTSUPP; |
6625 | ||
fd57d0cb | 6626 | type = __nft_obj_type_get(objtype); |
7dab8ee3 | 6627 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
d62d0ba9 FFM |
6628 | |
6629 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
6630 | } |
6631 | ||
7dab8ee3 | 6632 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 | 6633 | |
452238e8 | 6634 | type = nft_obj_type_get(net, objtype); |
e5009240 PNA |
6635 | if (IS_ERR(type)) |
6636 | return PTR_ERR(type); | |
6637 | ||
84fba055 | 6638 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
6639 | if (IS_ERR(obj)) { |
6640 | err = PTR_ERR(obj); | |
b131c964 | 6641 | goto err_init; |
e5009240 | 6642 | } |
d152159b | 6643 | obj->key.table = table; |
3ecbfd65 HS |
6644 | obj->handle = nf_tables_alloc_handle(table); |
6645 | ||
d152159b FW |
6646 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL); |
6647 | if (!obj->key.name) { | |
61509575 | 6648 | err = -ENOMEM; |
b131c964 JGG |
6649 | goto err_strdup; |
6650 | } | |
6651 | ||
6652 | if (nla[NFTA_OBJ_USERDATA]) { | |
85db827a | 6653 | obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL); |
b131c964 JGG |
6654 | if (obj->udata == NULL) |
6655 | goto err_userdata; | |
6656 | ||
85db827a | 6657 | obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]); |
61509575 | 6658 | } |
e5009240 PNA |
6659 | |
6660 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
6661 | if (err < 0) | |
b131c964 | 6662 | goto err_trans; |
e5009240 | 6663 | |
4d44175a FW |
6664 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
6665 | nft_objname_ht_params); | |
6666 | if (err < 0) | |
b131c964 | 6667 | goto err_obj_ht; |
4d44175a | 6668 | |
e5009240 PNA |
6669 | list_add_tail_rcu(&obj->list, &table->objects); |
6670 | table->use++; | |
6671 | return 0; | |
b131c964 | 6672 | err_obj_ht: |
4d44175a FW |
6673 | /* queued in transaction log */ |
6674 | INIT_LIST_HEAD(&obj->list); | |
6675 | return err; | |
b131c964 | 6676 | err_trans: |
b131c964 | 6677 | kfree(obj->udata); |
85dfd816 PNA |
6678 | err_userdata: |
6679 | kfree(obj->key.name); | |
b131c964 | 6680 | err_strdup: |
dfc46034 | 6681 | if (obj->ops->destroy) |
00bfb320 | 6682 | obj->ops->destroy(&ctx, obj); |
e5009240 | 6683 | kfree(obj); |
b131c964 | 6684 | err_init: |
e5009240 PNA |
6685 | module_put(type->owner); |
6686 | return err; | |
6687 | } | |
6688 | ||
6689 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
6690 | u32 portid, u32 seq, int event, u32 flags, | |
6691 | int family, const struct nft_table *table, | |
43da04a5 | 6692 | struct nft_object *obj, bool reset) |
e5009240 | 6693 | { |
e5009240 PNA |
6694 | struct nlmsghdr *nlh; |
6695 | ||
dedb67c4 | 6696 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
6697 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
6698 | NFNETLINK_V0, nft_base_seq(net)); | |
6699 | if (!nlh) | |
e5009240 PNA |
6700 | goto nla_put_failure; |
6701 | ||
e5009240 | 6702 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || |
d152159b | 6703 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
dfc46034 | 6704 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || |
e5009240 | 6705 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || |
3ecbfd65 HS |
6706 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) || |
6707 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), | |
6708 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
6709 | goto nla_put_failure; |
6710 | ||
b131c964 JGG |
6711 | if (obj->udata && |
6712 | nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata)) | |
6713 | goto nla_put_failure; | |
6714 | ||
e5009240 PNA |
6715 | nlmsg_end(skb, nlh); |
6716 | return 0; | |
6717 | ||
6718 | nla_put_failure: | |
6719 | nlmsg_trim(skb, nlh); | |
6720 | return -1; | |
6721 | } | |
6722 | ||
a9fea2a3 | 6723 | struct nft_obj_filter { |
e46abbcc | 6724 | char *table; |
a9fea2a3 PNA |
6725 | u32 type; |
6726 | }; | |
6727 | ||
e5009240 PNA |
6728 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
6729 | { | |
6730 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 6731 | const struct nft_table *table; |
e5009240 | 6732 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 6733 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
6734 | struct net *net = sock_net(skb->sk); |
6735 | int family = nfmsg->nfgen_family; | |
0854db2a | 6736 | struct nftables_pernet *nft_net; |
43da04a5 PNA |
6737 | struct nft_object *obj; |
6738 | bool reset = false; | |
6739 | ||
6740 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
6741 | reset = true; | |
e5009240 PNA |
6742 | |
6743 | rcu_read_lock(); | |
d59d2f82 | 6744 | nft_net = nft_pernet(net); |
0854db2a | 6745 | cb->seq = nft_net->base_seq; |
e5009240 | 6746 | |
0854db2a | 6747 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 6748 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
6749 | continue; |
6750 | ||
36596dad PNA |
6751 | list_for_each_entry_rcu(obj, &table->objects, list) { |
6752 | if (!nft_is_active(net, obj)) | |
6753 | goto cont; | |
6754 | if (idx < s_idx) | |
6755 | goto cont; | |
6756 | if (idx > s_idx) | |
6757 | memset(&cb->args[1], 0, | |
6758 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 6759 | if (filter && filter->table && |
36596dad PNA |
6760 | strcmp(filter->table, table->name)) |
6761 | goto cont; | |
6762 | if (filter && | |
6763 | filter->type != NFT_OBJECT_UNSPEC && | |
6764 | obj->ops->type->type != filter->type) | |
6765 | goto cont; | |
8e6cf365 | 6766 | if (reset) { |
68df2ed5 | 6767 | char *buf = kasprintf(GFP_ATOMIC, |
c520292f | 6768 | "%s:%u", |
8e6cf365 | 6769 | table->name, |
0854db2a | 6770 | nft_net->base_seq); |
8e6cf365 RGB |
6771 | |
6772 | audit_log_nfcfg(buf, | |
6773 | family, | |
6774 | obj->handle, | |
14224039 | 6775 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 6776 | GFP_ATOMIC); |
8e6cf365 RGB |
6777 | kfree(buf); |
6778 | } | |
6779 | ||
36596dad PNA |
6780 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
6781 | cb->nlh->nlmsg_seq, | |
6782 | NFT_MSG_NEWOBJ, | |
6783 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 6784 | table->family, table, |
36596dad PNA |
6785 | obj, reset) < 0) |
6786 | goto done; | |
e5009240 | 6787 | |
36596dad | 6788 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 6789 | cont: |
36596dad | 6790 | idx++; |
e5009240 PNA |
6791 | } |
6792 | } | |
6793 | done: | |
6794 | rcu_read_unlock(); | |
6795 | ||
6796 | cb->args[0] = idx; | |
6797 | return skb->len; | |
6798 | } | |
6799 | ||
90fd131a | 6800 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 6801 | { |
90fd131a FW |
6802 | const struct nlattr * const *nla = cb->data; |
6803 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 6804 | |
90fd131a FW |
6805 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
6806 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
6807 | if (!filter) | |
6808 | return -ENOMEM; | |
6809 | ||
6810 | if (nla[NFTA_OBJ_TABLE]) { | |
6811 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
6812 | if (!filter->table) { | |
6813 | kfree(filter); | |
6814 | return -ENOMEM; | |
6815 | } | |
6816 | } | |
6817 | ||
6818 | if (nla[NFTA_OBJ_TYPE]) | |
6819 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 6820 | } |
a9fea2a3 | 6821 | |
90fd131a | 6822 | cb->data = filter; |
a9fea2a3 PNA |
6823 | return 0; |
6824 | } | |
6825 | ||
90fd131a | 6826 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 6827 | { |
90fd131a | 6828 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 6829 | |
90fd131a FW |
6830 | if (filter) { |
6831 | kfree(filter->table); | |
6832 | kfree(filter); | |
e46abbcc | 6833 | } |
a9fea2a3 | 6834 | |
90fd131a | 6835 | return 0; |
a9fea2a3 PNA |
6836 | } |
6837 | ||
d9adf22a | 6838 | /* called with rcu_read_lock held */ |
797d4980 PNA |
6839 | static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info, |
6840 | const struct nlattr * const nla[]) | |
e5009240 | 6841 | { |
797d4980 PNA |
6842 | struct netlink_ext_ack *extack = info->extack; |
6843 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 6844 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 6845 | const struct nft_table *table; |
797d4980 | 6846 | struct net *net = info->net; |
e5009240 PNA |
6847 | struct nft_object *obj; |
6848 | struct sk_buff *skb2; | |
43da04a5 | 6849 | bool reset = false; |
e5009240 PNA |
6850 | u32 objtype; |
6851 | int err; | |
6852 | ||
797d4980 | 6853 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
e5009240 | 6854 | struct netlink_dump_control c = { |
90fd131a | 6855 | .start = nf_tables_dump_obj_start, |
e5009240 | 6856 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 6857 | .done = nf_tables_dump_obj_done, |
d9adf22a | 6858 | .module = THIS_MODULE, |
90fd131a | 6859 | .data = (void *)nla, |
e5009240 | 6860 | }; |
a9fea2a3 | 6861 | |
797d4980 | 6862 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
e5009240 PNA |
6863 | } |
6864 | ||
6865 | if (!nla[NFTA_OBJ_NAME] || | |
6866 | !nla[NFTA_OBJ_TYPE]) | |
6867 | return -EINVAL; | |
6868 | ||
6001a930 | 6869 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0); |
36dd1bcc PNA |
6870 | if (IS_ERR(table)) { |
6871 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 6872 | return PTR_ERR(table); |
36dd1bcc | 6873 | } |
e5009240 PNA |
6874 | |
6875 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 6876 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
6877 | if (IS_ERR(obj)) { |
6878 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 6879 | return PTR_ERR(obj); |
36dd1bcc | 6880 | } |
e5009240 | 6881 | |
d9adf22a | 6882 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
6883 | if (!skb2) |
6884 | return -ENOMEM; | |
6885 | ||
797d4980 | 6886 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
43da04a5 PNA |
6887 | reset = true; |
6888 | ||
8e6cf365 | 6889 | if (reset) { |
0854db2a FW |
6890 | const struct nftables_pernet *nft_net; |
6891 | char *buf; | |
6892 | ||
d59d2f82 | 6893 | nft_net = nft_pernet(net); |
0854db2a | 6894 | buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq); |
8e6cf365 RGB |
6895 | |
6896 | audit_log_nfcfg(buf, | |
6897 | family, | |
6898 | obj->handle, | |
14224039 | 6899 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 6900 | GFP_ATOMIC); |
8e6cf365 RGB |
6901 | kfree(buf); |
6902 | } | |
6903 | ||
e5009240 | 6904 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 6905 | info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, |
43da04a5 | 6906 | family, table, obj, reset); |
e5009240 | 6907 | if (err < 0) |
ee921183 | 6908 | goto err_fill_obj_info; |
e5009240 | 6909 | |
ee921183 PNA |
6910 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
6911 | ||
6912 | err_fill_obj_info: | |
e5009240 PNA |
6913 | kfree_skb(skb2); |
6914 | return err; | |
e5009240 PNA |
6915 | } |
6916 | ||
00bfb320 | 6917 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 6918 | { |
dfc46034 | 6919 | if (obj->ops->destroy) |
00bfb320 | 6920 | obj->ops->destroy(ctx, obj); |
e5009240 | 6921 | |
dfc46034 | 6922 | module_put(obj->ops->type->owner); |
d152159b | 6923 | kfree(obj->key.name); |
bc7a7082 | 6924 | kfree(obj->udata); |
e5009240 PNA |
6925 | kfree(obj); |
6926 | } | |
6927 | ||
7dab8ee3 PNA |
6928 | static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info, |
6929 | const struct nlattr * const nla[]) | |
e5009240 | 6930 | { |
7dab8ee3 PNA |
6931 | struct netlink_ext_ack *extack = info->extack; |
6932 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 6933 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6934 | struct net *net = info->net; |
36dd1bcc | 6935 | const struct nlattr *attr; |
e5009240 PNA |
6936 | struct nft_table *table; |
6937 | struct nft_object *obj; | |
6938 | struct nft_ctx ctx; | |
6939 | u32 objtype; | |
6940 | ||
6941 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 6942 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
6943 | return -EINVAL; |
6944 | ||
6001a930 PNA |
6945 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
6946 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
6947 | if (IS_ERR(table)) { |
6948 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 6949 | return PTR_ERR(table); |
36dd1bcc | 6950 | } |
e5009240 PNA |
6951 | |
6952 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
6953 | if (nla[NFTA_OBJ_HANDLE]) { |
6954 | attr = nla[NFTA_OBJ_HANDLE]; | |
6955 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
6956 | } else { | |
6957 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 6958 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
6959 | } |
6960 | ||
6961 | if (IS_ERR(obj)) { | |
6962 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 6963 | return PTR_ERR(obj); |
36dd1bcc PNA |
6964 | } |
6965 | if (obj->use > 0) { | |
6966 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 6967 | return -EBUSY; |
36dd1bcc | 6968 | } |
e5009240 | 6969 | |
7dab8ee3 | 6970 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 PNA |
6971 | |
6972 | return nft_delobj(&ctx, obj); | |
6973 | } | |
6974 | ||
d152159b | 6975 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 | 6976 | struct nft_object *obj, u32 portid, u32 seq, int event, |
6fb721cf | 6977 | u16 flags, int family, int report, gfp_t gfp) |
e5009240 | 6978 | { |
d59d2f82 | 6979 | struct nftables_pernet *nft_net = nft_pernet(net); |
e5009240 PNA |
6980 | struct sk_buff *skb; |
6981 | int err; | |
c520292f | 6982 | char *buf = kasprintf(gfp, "%s:%u", |
0854db2a | 6983 | table->name, nft_net->base_seq); |
8e6cf365 RGB |
6984 | |
6985 | audit_log_nfcfg(buf, | |
6986 | family, | |
6987 | obj->handle, | |
6988 | event == NFT_MSG_NEWOBJ ? | |
c520292f RGB |
6989 | AUDIT_NFT_OP_OBJ_REGISTER : |
6990 | AUDIT_NFT_OP_OBJ_UNREGISTER, | |
68df2ed5 | 6991 | gfp); |
8e6cf365 | 6992 | kfree(buf); |
e5009240 | 6993 | |
2599e989 PNA |
6994 | if (!report && |
6995 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 6996 | return; |
e5009240 | 6997 | |
2599e989 | 6998 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
6999 | if (skb == NULL) |
7000 | goto err; | |
7001 | ||
6fb721cf PNA |
7002 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, |
7003 | flags & (NLM_F_CREATE | NLM_F_EXCL), | |
7004 | family, table, obj, false); | |
e5009240 PNA |
7005 | if (err < 0) { |
7006 | kfree_skb(skb); | |
7007 | goto err; | |
7008 | } | |
7009 | ||
0854db2a | 7010 | nft_notify_enqueue(skb, report, &nft_net->notify_list); |
25e94a99 | 7011 | return; |
e5009240 | 7012 | err: |
25e94a99 | 7013 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 7014 | } |
2599e989 PNA |
7015 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
7016 | ||
25e94a99 PNA |
7017 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
7018 | struct nft_object *obj, int event) | |
2599e989 | 7019 | { |
25e94a99 | 7020 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
6fb721cf | 7021 | ctx->flags, ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 7022 | } |
e5009240 | 7023 | |
3b49e2e9 PNA |
7024 | /* |
7025 | * Flow tables | |
7026 | */ | |
7027 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
7028 | { | |
7029 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7030 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
7031 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7032 | } | |
7033 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
7034 | ||
7035 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
7036 | { | |
7037 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7038 | list_del_rcu(&type->list); | |
7039 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7040 | } | |
7041 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
7042 | ||
7043 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
7044 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
7045 | .len = NFT_NAME_MAXLEN - 1 }, | |
7046 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
7047 | .len = NFT_NAME_MAXLEN - 1 }, | |
7048 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7049 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 7050 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
7051 | }; |
7052 | ||
cac20fcd PNA |
7053 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
7054 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
7055 | { |
7056 | struct nft_flowtable *flowtable; | |
7057 | ||
d9adf22a | 7058 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
7059 | if (!nla_strcmp(nla, flowtable->name) && |
7060 | nft_active_genmask(flowtable, genmask)) | |
7061 | return flowtable; | |
7062 | } | |
7063 | return ERR_PTR(-ENOENT); | |
7064 | } | |
cac20fcd | 7065 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 7066 | |
9b05b6e1 LGL |
7067 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
7068 | struct nft_flowtable *flowtable, | |
7069 | enum nft_trans_phase phase) | |
7070 | { | |
7071 | switch (phase) { | |
7072 | case NFT_TRANS_PREPARE: | |
7073 | case NFT_TRANS_ABORT: | |
7074 | case NFT_TRANS_RELEASE: | |
7075 | flowtable->use--; | |
954d8297 | 7076 | fallthrough; |
9b05b6e1 LGL |
7077 | default: |
7078 | return; | |
7079 | } | |
7080 | } | |
7081 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
7082 | ||
ae0662f8 | 7083 | static struct nft_flowtable * |
cac20fcd PNA |
7084 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
7085 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
7086 | { |
7087 | struct nft_flowtable *flowtable; | |
7088 | ||
7089 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
7090 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
7091 | nft_active_genmask(flowtable, genmask)) | |
7092 | return flowtable; | |
7093 | } | |
7094 | return ERR_PTR(-ENOENT); | |
7095 | } | |
7096 | ||
d9246a53 PNA |
7097 | struct nft_flowtable_hook { |
7098 | u32 num; | |
7099 | int priority; | |
7100 | struct list_head list; | |
7101 | }; | |
7102 | ||
3b49e2e9 PNA |
7103 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
7104 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
7105 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
7106 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
7107 | }; | |
7108 | ||
d9246a53 PNA |
7109 | static int nft_flowtable_parse_hook(const struct nft_ctx *ctx, |
7110 | const struct nlattr *attr, | |
7111 | struct nft_flowtable_hook *flowtable_hook, | |
5b6743fb | 7112 | struct nft_flowtable *flowtable, bool add) |
3b49e2e9 | 7113 | { |
3b49e2e9 | 7114 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 7115 | struct nft_hook *hook; |
3b49e2e9 | 7116 | int hooknum, priority; |
3f0465a9 | 7117 | int err; |
3b49e2e9 | 7118 | |
d9246a53 PNA |
7119 | INIT_LIST_HEAD(&flowtable_hook->list); |
7120 | ||
8cb08174 JB |
7121 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr, |
7122 | nft_flowtable_hook_policy, NULL); | |
3b49e2e9 PNA |
7123 | if (err < 0) |
7124 | return err; | |
7125 | ||
5b6743fb PNA |
7126 | if (add) { |
7127 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
7128 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) | |
7129 | return -EINVAL; | |
3b49e2e9 | 7130 | |
5b6743fb PNA |
7131 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); |
7132 | if (hooknum != NF_NETDEV_INGRESS) | |
7133 | return -EOPNOTSUPP; | |
7134 | ||
7135 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7136 | ||
7137 | flowtable_hook->priority = priority; | |
7138 | flowtable_hook->num = hooknum; | |
7139 | } else { | |
7140 | if (tb[NFTA_FLOWTABLE_HOOK_NUM]) { | |
7141 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
7142 | if (hooknum != flowtable->hooknum) | |
7143 | return -EOPNOTSUPP; | |
7144 | } | |
7145 | ||
7146 | if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { | |
7147 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7148 | if (priority != flowtable->data.priority) | |
7149 | return -EOPNOTSUPP; | |
7150 | } | |
3b49e2e9 | 7151 | |
5b6743fb PNA |
7152 | flowtable_hook->priority = flowtable->data.priority; |
7153 | flowtable_hook->num = flowtable->hooknum; | |
7154 | } | |
3b49e2e9 | 7155 | |
05abe445 PNA |
7156 | if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) { |
7157 | err = nf_tables_parse_netdev_hooks(ctx->net, | |
7158 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
7159 | &flowtable_hook->list); | |
7160 | if (err < 0) | |
7161 | return err; | |
7162 | } | |
3b49e2e9 | 7163 | |
d9246a53 | 7164 | list_for_each_entry(hook, &flowtable_hook->list, list) { |
3f0465a9 | 7165 | hook->ops.pf = NFPROTO_NETDEV; |
5b6743fb PNA |
7166 | hook->ops.hooknum = flowtable_hook->num; |
7167 | hook->ops.priority = flowtable_hook->priority; | |
7168 | hook->ops.priv = &flowtable->data; | |
7169 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
7170 | } |
7171 | ||
3b49e2e9 PNA |
7172 | return err; |
7173 | } | |
7174 | ||
98319cb9 | 7175 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
7176 | { |
7177 | const struct nf_flowtable_type *type; | |
7178 | ||
7179 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 7180 | if (family == type->family) |
3b49e2e9 PNA |
7181 | return type; |
7182 | } | |
7183 | return NULL; | |
7184 | } | |
7185 | ||
452238e8 FW |
7186 | static const struct nf_flowtable_type * |
7187 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
7188 | { |
7189 | const struct nf_flowtable_type *type; | |
7190 | ||
98319cb9 | 7191 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
7192 | if (type != NULL && try_module_get(type->owner)) |
7193 | return type; | |
7194 | ||
f102d66b | 7195 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
7196 | #ifdef CONFIG_MODULES |
7197 | if (type == NULL) { | |
eb014de4 | 7198 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
7199 | return ERR_PTR(-EAGAIN); |
7200 | } | |
7201 | #endif | |
7202 | return ERR_PTR(-ENOENT); | |
7203 | } | |
7204 | ||
5acab914 | 7205 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
7206 | static void nft_unregister_flowtable_hook(struct net *net, |
7207 | struct nft_flowtable *flowtable, | |
7208 | struct nft_hook *hook) | |
7209 | { | |
7210 | nf_unregister_net_hook(net, &hook->ops); | |
7211 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
7212 | FLOW_BLOCK_UNBIND); | |
7213 | } | |
7214 | ||
3b49e2e9 | 7215 | static void nft_unregister_flowtable_net_hooks(struct net *net, |
f9382669 | 7216 | struct list_head *hook_list) |
3b49e2e9 | 7217 | { |
3f0465a9 | 7218 | struct nft_hook *hook; |
3b49e2e9 | 7219 | |
f9382669 | 7220 | list_for_each_entry(hook, hook_list, list) |
5acab914 | 7221 | nf_unregister_net_hook(net, &hook->ops); |
3f0465a9 PNA |
7222 | } |
7223 | ||
7224 | static int nft_register_flowtable_net_hooks(struct net *net, | |
7225 | struct nft_table *table, | |
f9382669 | 7226 | struct list_head *hook_list, |
3f0465a9 PNA |
7227 | struct nft_flowtable *flowtable) |
7228 | { | |
7229 | struct nft_hook *hook, *hook2, *next; | |
7230 | struct nft_flowtable *ft; | |
7231 | int err, i = 0; | |
7232 | ||
f9382669 | 7233 | list_for_each_entry(hook, hook_list, list) { |
3f0465a9 | 7234 | list_for_each_entry(ft, &table->flowtables, list) { |
86fe2c19 PNA |
7235 | if (!nft_is_active_next(net, ft)) |
7236 | continue; | |
7237 | ||
3f0465a9 PNA |
7238 | list_for_each_entry(hook2, &ft->hook_list, list) { |
7239 | if (hook->ops.dev == hook2->ops.dev && | |
7240 | hook->ops.pf == hook2->ops.pf) { | |
77a92189 | 7241 | err = -EEXIST; |
3f0465a9 PNA |
7242 | goto err_unregister_net_hooks; |
7243 | } | |
7244 | } | |
7245 | } | |
3b49e2e9 | 7246 | |
d7c03a9f | 7247 | err = flowtable->data.type->setup(&flowtable->data, |
7248 | hook->ops.dev, | |
7249 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
7250 | if (err < 0) |
7251 | goto err_unregister_net_hooks; | |
7252 | ||
d7c03a9f | 7253 | err = nf_register_net_hook(net, &hook->ops); |
7254 | if (err < 0) { | |
7255 | flowtable->data.type->setup(&flowtable->data, | |
7256 | hook->ops.dev, | |
7257 | FLOW_BLOCK_UNBIND); | |
7258 | goto err_unregister_net_hooks; | |
7259 | } | |
7260 | ||
3f0465a9 | 7261 | i++; |
3b49e2e9 | 7262 | } |
3f0465a9 PNA |
7263 | |
7264 | return 0; | |
7265 | ||
7266 | err_unregister_net_hooks: | |
f9382669 | 7267 | list_for_each_entry_safe(hook, next, hook_list, list) { |
3f0465a9 PNA |
7268 | if (i-- <= 0) |
7269 | break; | |
7270 | ||
ff4bf2f4 | 7271 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7272 | list_del_rcu(&hook->list); |
7273 | kfree_rcu(hook, rcu); | |
7274 | } | |
7275 | ||
7276 | return err; | |
3b49e2e9 PNA |
7277 | } |
7278 | ||
389a2cbc PNA |
7279 | static void nft_flowtable_hooks_destroy(struct list_head *hook_list) |
7280 | { | |
7281 | struct nft_hook *hook, *next; | |
7282 | ||
7283 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
7284 | list_del_rcu(&hook->list); | |
7285 | kfree_rcu(hook, rcu); | |
7286 | } | |
7287 | } | |
7288 | ||
78d9f48f PNA |
7289 | static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, |
7290 | struct nft_flowtable *flowtable) | |
7291 | { | |
7292 | const struct nlattr * const *nla = ctx->nla; | |
7293 | struct nft_flowtable_hook flowtable_hook; | |
7294 | struct nft_hook *hook, *next; | |
7295 | struct nft_trans *trans; | |
7296 | bool unregister = false; | |
7b35582c | 7297 | u32 flags; |
78d9f48f PNA |
7298 | int err; |
7299 | ||
7300 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7301 | &flowtable_hook, flowtable, false); |
78d9f48f PNA |
7302 | if (err < 0) |
7303 | return err; | |
7304 | ||
7305 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7306 | if (nft_hook_list_find(&flowtable->hook_list, hook)) { | |
7307 | list_del(&hook->list); | |
7308 | kfree(hook); | |
7309 | } | |
7310 | } | |
7311 | ||
7b35582c PNA |
7312 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7313 | flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
7314 | if (flags & ~NFT_FLOWTABLE_MASK) | |
7315 | return -EOPNOTSUPP; | |
7316 | if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^ | |
7317 | (flags & NFT_FLOWTABLE_HW_OFFLOAD)) | |
7318 | return -EOPNOTSUPP; | |
7319 | } else { | |
7320 | flags = flowtable->data.flags; | |
7321 | } | |
7322 | ||
78d9f48f PNA |
7323 | err = nft_register_flowtable_net_hooks(ctx->net, ctx->table, |
7324 | &flowtable_hook.list, flowtable); | |
7325 | if (err < 0) | |
7326 | goto err_flowtable_update_hook; | |
7327 | ||
7328 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE, | |
7329 | sizeof(struct nft_trans_flowtable)); | |
7330 | if (!trans) { | |
7331 | unregister = true; | |
7332 | err = -ENOMEM; | |
7333 | goto err_flowtable_update_hook; | |
7334 | } | |
7335 | ||
7b35582c | 7336 | nft_trans_flowtable_flags(trans) = flags; |
78d9f48f PNA |
7337 | nft_trans_flowtable(trans) = flowtable; |
7338 | nft_trans_flowtable_update(trans) = true; | |
7339 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
7340 | list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans)); | |
7341 | ||
0854db2a | 7342 | nft_trans_commit_list_add_tail(ctx->net, trans); |
78d9f48f PNA |
7343 | |
7344 | return 0; | |
7345 | ||
7346 | err_flowtable_update_hook: | |
7347 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
7348 | if (unregister) | |
7349 | nft_unregister_flowtable_hook(ctx->net, flowtable, hook); | |
7350 | list_del_rcu(&hook->list); | |
7351 | kfree_rcu(hook, rcu); | |
7352 | } | |
7353 | ||
7354 | return err; | |
7355 | ||
7356 | } | |
7357 | ||
7dab8ee3 PNA |
7358 | static int nf_tables_newflowtable(struct sk_buff *skb, |
7359 | const struct nfnl_info *info, | |
7360 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7361 | { |
7dab8ee3 | 7362 | struct netlink_ext_ack *extack = info->extack; |
d9246a53 | 7363 | struct nft_flowtable_hook flowtable_hook; |
7dab8ee3 | 7364 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 7365 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7366 | const struct nf_flowtable_type *type; |
3f0465a9 PNA |
7367 | struct nft_flowtable *flowtable; |
7368 | struct nft_hook *hook, *next; | |
7dab8ee3 | 7369 | struct net *net = info->net; |
3b49e2e9 PNA |
7370 | struct nft_table *table; |
7371 | struct nft_ctx ctx; | |
3f0465a9 | 7372 | int err; |
3b49e2e9 PNA |
7373 | |
7374 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
7375 | !nla[NFTA_FLOWTABLE_NAME] || | |
7376 | !nla[NFTA_FLOWTABLE_HOOK]) | |
7377 | return -EINVAL; | |
7378 | ||
cac20fcd | 7379 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7380 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7381 | if (IS_ERR(table)) { |
7382 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7383 | return PTR_ERR(table); |
36dd1bcc | 7384 | } |
3b49e2e9 | 7385 | |
cac20fcd PNA |
7386 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
7387 | genmask); | |
3b49e2e9 PNA |
7388 | if (IS_ERR(flowtable)) { |
7389 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
7390 | if (err != -ENOENT) { |
7391 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 7392 | return err; |
36dd1bcc | 7393 | } |
3b49e2e9 | 7394 | } else { |
7dab8ee3 | 7395 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 7396 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); |
3b49e2e9 | 7397 | return -EEXIST; |
36dd1bcc | 7398 | } |
3b49e2e9 | 7399 | |
7dab8ee3 | 7400 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
78d9f48f | 7401 | |
7dab8ee3 | 7402 | return nft_flowtable_update(&ctx, info->nlh, flowtable); |
3b49e2e9 PNA |
7403 | } |
7404 | ||
7dab8ee3 | 7405 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
3b49e2e9 PNA |
7406 | |
7407 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL); | |
7408 | if (!flowtable) | |
7409 | return -ENOMEM; | |
7410 | ||
7411 | flowtable->table = table; | |
3ecbfd65 | 7412 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 7413 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 7414 | |
3b49e2e9 PNA |
7415 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL); |
7416 | if (!flowtable->name) { | |
7417 | err = -ENOMEM; | |
7418 | goto err1; | |
7419 | } | |
7420 | ||
452238e8 | 7421 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
7422 | if (IS_ERR(type)) { |
7423 | err = PTR_ERR(type); | |
7424 | goto err2; | |
7425 | } | |
7426 | ||
8bb69f3b PNA |
7427 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
7428 | flowtable->data.flags = | |
7429 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
7e6136f1 PNA |
7430 | if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) { |
7431 | err = -EOPNOTSUPP; | |
8bb69f3b | 7432 | goto err3; |
7e6136f1 | 7433 | } |
8bb69f3b PNA |
7434 | } |
7435 | ||
7436 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 7437 | flowtable->data.type = type; |
a268de77 | 7438 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
7439 | if (err < 0) |
7440 | goto err3; | |
7441 | ||
d9246a53 | 7442 | err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK], |
5b6743fb | 7443 | &flowtable_hook, flowtable, true); |
3b49e2e9 | 7444 | if (err < 0) |
a268de77 | 7445 | goto err4; |
3b49e2e9 | 7446 | |
d9246a53 PNA |
7447 | list_splice(&flowtable_hook.list, &flowtable->hook_list); |
7448 | flowtable->data.priority = flowtable_hook.priority; | |
7449 | flowtable->hooknum = flowtable_hook.num; | |
7450 | ||
f9382669 PNA |
7451 | err = nft_register_flowtable_net_hooks(ctx.net, table, |
7452 | &flowtable->hook_list, | |
7453 | flowtable); | |
2d285f26 | 7454 | if (err < 0) { |
389a2cbc | 7455 | nft_flowtable_hooks_destroy(&flowtable->hook_list); |
3f0465a9 | 7456 | goto err4; |
2d285f26 | 7457 | } |
3b49e2e9 PNA |
7458 | |
7459 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
7460 | if (err < 0) | |
3f0465a9 | 7461 | goto err5; |
3b49e2e9 PNA |
7462 | |
7463 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
7464 | table->use++; | |
7465 | ||
7466 | return 0; | |
a268de77 | 7467 | err5: |
3f0465a9 | 7468 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 7469 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
7470 | list_del_rcu(&hook->list); |
7471 | kfree_rcu(hook, rcu); | |
7472 | } | |
a268de77 FF |
7473 | err4: |
7474 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
7475 | err3: |
7476 | module_put(type->owner); | |
7477 | err2: | |
7478 | kfree(flowtable->name); | |
7479 | err1: | |
7480 | kfree(flowtable); | |
7481 | return err; | |
7482 | } | |
7483 | ||
3003055f PNA |
7484 | static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook) |
7485 | { | |
7486 | struct nft_hook *this, *next; | |
7487 | ||
7488 | list_for_each_entry_safe(this, next, &flowtable_hook->list, list) { | |
7489 | list_del(&this->list); | |
7490 | kfree(this); | |
7491 | } | |
7492 | } | |
7493 | ||
abadb2f8 PNA |
7494 | static int nft_delflowtable_hook(struct nft_ctx *ctx, |
7495 | struct nft_flowtable *flowtable) | |
7496 | { | |
7497 | const struct nlattr * const *nla = ctx->nla; | |
7498 | struct nft_flowtable_hook flowtable_hook; | |
3003055f | 7499 | struct nft_hook *this, *hook; |
abadb2f8 PNA |
7500 | struct nft_trans *trans; |
7501 | int err; | |
7502 | ||
7503 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 7504 | &flowtable_hook, flowtable, false); |
abadb2f8 PNA |
7505 | if (err < 0) |
7506 | return err; | |
7507 | ||
3003055f | 7508 | list_for_each_entry(this, &flowtable_hook.list, list) { |
abadb2f8 PNA |
7509 | hook = nft_hook_list_find(&flowtable->hook_list, this); |
7510 | if (!hook) { | |
7511 | err = -ENOENT; | |
7512 | goto err_flowtable_del_hook; | |
7513 | } | |
7514 | hook->inactive = true; | |
abadb2f8 PNA |
7515 | } |
7516 | ||
7517 | trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE, | |
7518 | sizeof(struct nft_trans_flowtable)); | |
3003055f PNA |
7519 | if (!trans) { |
7520 | err = -ENOMEM; | |
7521 | goto err_flowtable_del_hook; | |
7522 | } | |
abadb2f8 PNA |
7523 | |
7524 | nft_trans_flowtable(trans) = flowtable; | |
7525 | nft_trans_flowtable_update(trans) = true; | |
7526 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
3003055f | 7527 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 | 7528 | |
0854db2a | 7529 | nft_trans_commit_list_add_tail(ctx->net, trans); |
abadb2f8 PNA |
7530 | |
7531 | return 0; | |
7532 | ||
7533 | err_flowtable_del_hook: | |
3003055f PNA |
7534 | list_for_each_entry(this, &flowtable_hook.list, list) { |
7535 | hook = nft_hook_list_find(&flowtable->hook_list, this); | |
7536 | if (!hook) | |
7537 | break; | |
7538 | ||
abadb2f8 | 7539 | hook->inactive = false; |
3003055f PNA |
7540 | } |
7541 | nft_flowtable_hook_release(&flowtable_hook); | |
abadb2f8 PNA |
7542 | |
7543 | return err; | |
7544 | } | |
7545 | ||
7dab8ee3 PNA |
7546 | static int nf_tables_delflowtable(struct sk_buff *skb, |
7547 | const struct nfnl_info *info, | |
7548 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7549 | { |
7dab8ee3 PNA |
7550 | struct netlink_ext_ack *extack = info->extack; |
7551 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7552 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7553 | struct nft_flowtable *flowtable; |
7dab8ee3 | 7554 | struct net *net = info->net; |
36dd1bcc | 7555 | const struct nlattr *attr; |
3b49e2e9 PNA |
7556 | struct nft_table *table; |
7557 | struct nft_ctx ctx; | |
7558 | ||
e603ea4b PNA |
7559 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
7560 | (!nla[NFTA_FLOWTABLE_NAME] && | |
7561 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
7562 | return -EINVAL; | |
7563 | ||
cac20fcd | 7564 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7565 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
7566 | if (IS_ERR(table)) { |
7567 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 7568 | return PTR_ERR(table); |
36dd1bcc | 7569 | } |
3b49e2e9 | 7570 | |
36dd1bcc PNA |
7571 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
7572 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
7573 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
7574 | } else { | |
7575 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
7576 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
7577 | } | |
7578 | ||
7579 | if (IS_ERR(flowtable)) { | |
7580 | NL_SET_BAD_ATTR(extack, attr); | |
7581 | return PTR_ERR(flowtable); | |
7582 | } | |
abadb2f8 | 7583 | |
7dab8ee3 | 7584 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
abadb2f8 PNA |
7585 | |
7586 | if (nla[NFTA_FLOWTABLE_HOOK]) | |
7587 | return nft_delflowtable_hook(&ctx, flowtable); | |
7588 | ||
36dd1bcc PNA |
7589 | if (flowtable->use > 0) { |
7590 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 7591 | return -EBUSY; |
36dd1bcc | 7592 | } |
3b49e2e9 | 7593 | |
3b49e2e9 PNA |
7594 | return nft_delflowtable(&ctx, flowtable); |
7595 | } | |
7596 | ||
7597 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
7598 | u32 portid, u32 seq, int event, | |
7599 | u32 flags, int family, | |
c42d8bda PNA |
7600 | struct nft_flowtable *flowtable, |
7601 | struct list_head *hook_list) | |
3b49e2e9 PNA |
7602 | { |
7603 | struct nlattr *nest, *nest_devs; | |
3f0465a9 | 7604 | struct nft_hook *hook; |
3b49e2e9 | 7605 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
7606 | |
7607 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
19c28b13 PNA |
7608 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
7609 | NFNETLINK_V0, nft_base_seq(net)); | |
7610 | if (!nlh) | |
3b49e2e9 PNA |
7611 | goto nla_put_failure; |
7612 | ||
3b49e2e9 PNA |
7613 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || |
7614 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 HS |
7615 | nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || |
7616 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), | |
8bb69f3b PNA |
7617 | NFTA_FLOWTABLE_PAD) || |
7618 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) | |
3b49e2e9 PNA |
7619 | goto nla_put_failure; |
7620 | ||
ae0be8de | 7621 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
7622 | if (!nest) |
7623 | goto nla_put_failure; | |
3b49e2e9 | 7624 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 7625 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
7626 | goto nla_put_failure; |
7627 | ||
ae0be8de | 7628 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
7629 | if (!nest_devs) |
7630 | goto nla_put_failure; | |
7631 | ||
c42d8bda | 7632 | list_for_each_entry_rcu(hook, hook_list, list) { |
3f0465a9 | 7633 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) |
3b49e2e9 PNA |
7634 | goto nla_put_failure; |
7635 | } | |
7636 | nla_nest_end(skb, nest_devs); | |
7637 | nla_nest_end(skb, nest); | |
7638 | ||
7639 | nlmsg_end(skb, nlh); | |
7640 | return 0; | |
7641 | ||
7642 | nla_put_failure: | |
7643 | nlmsg_trim(skb, nlh); | |
7644 | return -1; | |
7645 | } | |
7646 | ||
7647 | struct nft_flowtable_filter { | |
7648 | char *table; | |
7649 | }; | |
7650 | ||
7651 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
7652 | struct netlink_callback *cb) | |
7653 | { | |
7654 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
7655 | struct nft_flowtable_filter *filter = cb->data; | |
7656 | unsigned int idx = 0, s_idx = cb->args[0]; | |
7657 | struct net *net = sock_net(skb->sk); | |
7658 | int family = nfmsg->nfgen_family; | |
7659 | struct nft_flowtable *flowtable; | |
0854db2a | 7660 | struct nftables_pernet *nft_net; |
3b49e2e9 PNA |
7661 | const struct nft_table *table; |
7662 | ||
7663 | rcu_read_lock(); | |
d59d2f82 | 7664 | nft_net = nft_pernet(net); |
0854db2a | 7665 | cb->seq = nft_net->base_seq; |
3b49e2e9 | 7666 | |
0854db2a | 7667 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 7668 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
7669 | continue; |
7670 | ||
36596dad PNA |
7671 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
7672 | if (!nft_is_active(net, flowtable)) | |
7673 | goto cont; | |
7674 | if (idx < s_idx) | |
7675 | goto cont; | |
7676 | if (idx > s_idx) | |
7677 | memset(&cb->args[1], 0, | |
7678 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7679 | if (filter && filter->table && |
36596dad PNA |
7680 | strcmp(filter->table, table->name)) |
7681 | goto cont; | |
3b49e2e9 | 7682 | |
36596dad PNA |
7683 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
7684 | cb->nlh->nlmsg_seq, | |
7685 | NFT_MSG_NEWFLOWTABLE, | |
7686 | NLM_F_MULTI | NLM_F_APPEND, | |
c42d8bda PNA |
7687 | table->family, |
7688 | flowtable, | |
7689 | &flowtable->hook_list) < 0) | |
36596dad | 7690 | goto done; |
3b49e2e9 | 7691 | |
36596dad | 7692 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 7693 | cont: |
36596dad | 7694 | idx++; |
3b49e2e9 PNA |
7695 | } |
7696 | } | |
7697 | done: | |
7698 | rcu_read_unlock(); | |
7699 | ||
7700 | cb->args[0] = idx; | |
7701 | return skb->len; | |
7702 | } | |
7703 | ||
90fd131a | 7704 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 7705 | { |
90fd131a FW |
7706 | const struct nlattr * const *nla = cb->data; |
7707 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 7708 | |
90fd131a FW |
7709 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
7710 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7711 | if (!filter) | |
7712 | return -ENOMEM; | |
3b49e2e9 | 7713 | |
90fd131a FW |
7714 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
7715 | GFP_ATOMIC); | |
7716 | if (!filter->table) { | |
7717 | kfree(filter); | |
7718 | return -ENOMEM; | |
7719 | } | |
7720 | } | |
3b49e2e9 | 7721 | |
90fd131a | 7722 | cb->data = filter; |
3b49e2e9 PNA |
7723 | return 0; |
7724 | } | |
7725 | ||
90fd131a | 7726 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 7727 | { |
90fd131a | 7728 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 7729 | |
3b49e2e9 | 7730 | if (!filter) |
90fd131a | 7731 | return 0; |
3b49e2e9 | 7732 | |
90fd131a FW |
7733 | kfree(filter->table); |
7734 | kfree(filter); | |
7735 | ||
7736 | return 0; | |
3b49e2e9 PNA |
7737 | } |
7738 | ||
d9adf22a | 7739 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7740 | static int nf_tables_getflowtable(struct sk_buff *skb, |
7741 | const struct nfnl_info *info, | |
7742 | const struct nlattr * const nla[]) | |
3b49e2e9 | 7743 | { |
797d4980 | 7744 | u8 genmask = nft_genmask_cur(info->net); |
ef4b65e5 | 7745 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 7746 | struct nft_flowtable *flowtable; |
3b49e2e9 | 7747 | const struct nft_table *table; |
797d4980 | 7748 | struct net *net = info->net; |
3b49e2e9 PNA |
7749 | struct sk_buff *skb2; |
7750 | int err; | |
7751 | ||
797d4980 | 7752 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
3b49e2e9 | 7753 | struct netlink_dump_control c = { |
90fd131a | 7754 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
7755 | .dump = nf_tables_dump_flowtable, |
7756 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 7757 | .module = THIS_MODULE, |
90fd131a | 7758 | .data = (void *)nla, |
3b49e2e9 PNA |
7759 | }; |
7760 | ||
797d4980 | 7761 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
3b49e2e9 PNA |
7762 | } |
7763 | ||
7764 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
7765 | return -EINVAL; | |
7766 | ||
cac20fcd | 7767 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 7768 | genmask, 0); |
3b49e2e9 PNA |
7769 | if (IS_ERR(table)) |
7770 | return PTR_ERR(table); | |
7771 | ||
cac20fcd PNA |
7772 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
7773 | genmask); | |
03a0120f | 7774 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
7775 | return PTR_ERR(flowtable); |
7776 | ||
d9adf22a | 7777 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
7778 | if (!skb2) |
7779 | return -ENOMEM; | |
7780 | ||
7781 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
797d4980 | 7782 | info->nlh->nlmsg_seq, |
3b49e2e9 | 7783 | NFT_MSG_NEWFLOWTABLE, 0, family, |
c42d8bda | 7784 | flowtable, &flowtable->hook_list); |
3b49e2e9 | 7785 | if (err < 0) |
ee921183 | 7786 | goto err_fill_flowtable_info; |
3b49e2e9 | 7787 | |
ee921183 PNA |
7788 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
7789 | ||
7790 | err_fill_flowtable_info: | |
3b49e2e9 PNA |
7791 | kfree_skb(skb2); |
7792 | return err; | |
7793 | } | |
7794 | ||
7795 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
7796 | struct nft_flowtable *flowtable, | |
c42d8bda | 7797 | struct list_head *hook_list, |
3b49e2e9 PNA |
7798 | int event) |
7799 | { | |
d59d2f82 | 7800 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
3b49e2e9 | 7801 | struct sk_buff *skb; |
6fb721cf | 7802 | u16 flags = 0; |
3b49e2e9 PNA |
7803 | int err; |
7804 | ||
dceababa | 7805 | if (!ctx->report && |
3b49e2e9 PNA |
7806 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
7807 | return; | |
7808 | ||
7809 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
7810 | if (skb == NULL) | |
7811 | goto err; | |
7812 | ||
6fb721cf PNA |
7813 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
7814 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
7815 | ||
3b49e2e9 | 7816 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, |
6fb721cf | 7817 | ctx->seq, event, flags, |
c42d8bda | 7818 | ctx->family, flowtable, hook_list); |
3b49e2e9 PNA |
7819 | if (err < 0) { |
7820 | kfree_skb(skb); | |
7821 | goto err; | |
7822 | } | |
7823 | ||
0854db2a | 7824 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
3b49e2e9 PNA |
7825 | return; |
7826 | err: | |
7827 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
7828 | } | |
7829 | ||
3b49e2e9 PNA |
7830 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
7831 | { | |
3f0465a9 PNA |
7832 | struct nft_hook *hook, *next; |
7833 | ||
5acab914 | 7834 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 7835 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
7836 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
7837 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
7838 | list_del_rcu(&hook->list); |
7839 | kfree(hook); | |
7840 | } | |
3b49e2e9 | 7841 | kfree(flowtable->name); |
3b49e2e9 | 7842 | module_put(flowtable->data.type->owner); |
a12486eb | 7843 | kfree(flowtable); |
3b49e2e9 PNA |
7844 | } |
7845 | ||
84d7fce6 PNA |
7846 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
7847 | u32 portid, u32 seq) | |
7848 | { | |
d59d2f82 | 7849 | struct nftables_pernet *nft_net = nft_pernet(net); |
84d7fce6 | 7850 | struct nlmsghdr *nlh; |
784b4e61 | 7851 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 7852 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 | 7853 | |
19c28b13 PNA |
7854 | nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC, |
7855 | NFNETLINK_V0, nft_base_seq(net)); | |
7856 | if (!nlh) | |
84d7fce6 PNA |
7857 | goto nla_put_failure; |
7858 | ||
0854db2a | 7859 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) || |
784b4e61 PS |
7860 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || |
7861 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
7862 | goto nla_put_failure; |
7863 | ||
053c095a JB |
7864 | nlmsg_end(skb, nlh); |
7865 | return 0; | |
84d7fce6 PNA |
7866 | |
7867 | nla_put_failure: | |
7868 | nlmsg_trim(skb, nlh); | |
7869 | return -EMSGSIZE; | |
7870 | } | |
7871 | ||
3b49e2e9 PNA |
7872 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
7873 | struct nft_flowtable *flowtable) | |
7874 | { | |
3f0465a9 | 7875 | struct nft_hook *hook; |
3b49e2e9 | 7876 | |
3f0465a9 PNA |
7877 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
7878 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
7879 | continue; |
7880 | ||
5acab914 | 7881 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 7882 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
7883 | list_del_rcu(&hook->list); |
7884 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
7885 | break; |
7886 | } | |
7887 | } | |
7888 | ||
7889 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
7890 | unsigned long event, void *ptr) | |
7891 | { | |
7892 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
7893 | struct nft_flowtable *flowtable; | |
0854db2a | 7894 | struct nftables_pernet *nft_net; |
3b49e2e9 | 7895 | struct nft_table *table; |
0a2cf5ee | 7896 | struct net *net; |
3b49e2e9 PNA |
7897 | |
7898 | if (event != NETDEV_UNREGISTER) | |
7899 | return 0; | |
7900 | ||
6a48de01 | 7901 | net = dev_net(dev); |
d59d2f82 | 7902 | nft_net = nft_pernet(net); |
0854db2a FW |
7903 | mutex_lock(&nft_net->commit_mutex); |
7904 | list_for_each_entry(table, &nft_net->tables, list) { | |
36596dad PNA |
7905 | list_for_each_entry(flowtable, &table->flowtables, list) { |
7906 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
7907 | } |
7908 | } | |
0854db2a | 7909 | mutex_unlock(&nft_net->commit_mutex); |
6a48de01 | 7910 | |
3b49e2e9 PNA |
7911 | return NOTIFY_DONE; |
7912 | } | |
7913 | ||
7914 | static struct notifier_block nf_tables_flowtable_notifier = { | |
7915 | .notifier_call = nf_tables_flowtable_event, | |
7916 | }; | |
7917 | ||
25e94a99 PNA |
7918 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
7919 | int event) | |
84d7fce6 PNA |
7920 | { |
7921 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
7922 | struct sk_buff *skb2; | |
7923 | int err; | |
7924 | ||
dceababa | 7925 | if (!nlmsg_report(nlh) && |
84d7fce6 | 7926 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 7927 | return; |
84d7fce6 | 7928 | |
84d7fce6 PNA |
7929 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
7930 | if (skb2 == NULL) | |
7931 | goto err; | |
7932 | ||
7933 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
7934 | nlh->nlmsg_seq); | |
7935 | if (err < 0) { | |
7936 | kfree_skb(skb2); | |
7937 | goto err; | |
7938 | } | |
7939 | ||
25e94a99 PNA |
7940 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
7941 | nlmsg_report(nlh), GFP_KERNEL); | |
7942 | return; | |
84d7fce6 | 7943 | err: |
25e94a99 PNA |
7944 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
7945 | -ENOBUFS); | |
84d7fce6 PNA |
7946 | } |
7947 | ||
797d4980 PNA |
7948 | static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info, |
7949 | const struct nlattr * const nla[]) | |
84d7fce6 | 7950 | { |
84d7fce6 PNA |
7951 | struct sk_buff *skb2; |
7952 | int err; | |
7953 | ||
d9adf22a | 7954 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
7955 | if (skb2 == NULL) |
7956 | return -ENOMEM; | |
7957 | ||
797d4980 PNA |
7958 | err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid, |
7959 | info->nlh->nlmsg_seq); | |
84d7fce6 | 7960 | if (err < 0) |
ee921183 | 7961 | goto err_fill_gen_info; |
84d7fce6 | 7962 | |
797d4980 | 7963 | return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); |
ee921183 PNA |
7964 | |
7965 | err_fill_gen_info: | |
84d7fce6 PNA |
7966 | kfree_skb(skb2); |
7967 | return err; | |
7968 | } | |
7969 | ||
96518518 PM |
7970 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
7971 | [NFT_MSG_NEWTABLE] = { | |
50f2db9e PNA |
7972 | .call = nf_tables_newtable, |
7973 | .type = NFNL_CB_BATCH, | |
96518518 PM |
7974 | .attr_count = NFTA_TABLE_MAX, |
7975 | .policy = nft_table_policy, | |
7976 | }, | |
7977 | [NFT_MSG_GETTABLE] = { | |
50f2db9e PNA |
7978 | .call = nf_tables_gettable, |
7979 | .type = NFNL_CB_RCU, | |
96518518 PM |
7980 | .attr_count = NFTA_TABLE_MAX, |
7981 | .policy = nft_table_policy, | |
7982 | }, | |
7983 | [NFT_MSG_DELTABLE] = { | |
50f2db9e PNA |
7984 | .call = nf_tables_deltable, |
7985 | .type = NFNL_CB_BATCH, | |
96518518 PM |
7986 | .attr_count = NFTA_TABLE_MAX, |
7987 | .policy = nft_table_policy, | |
7988 | }, | |
7989 | [NFT_MSG_NEWCHAIN] = { | |
50f2db9e PNA |
7990 | .call = nf_tables_newchain, |
7991 | .type = NFNL_CB_BATCH, | |
96518518 PM |
7992 | .attr_count = NFTA_CHAIN_MAX, |
7993 | .policy = nft_chain_policy, | |
7994 | }, | |
7995 | [NFT_MSG_GETCHAIN] = { | |
50f2db9e PNA |
7996 | .call = nf_tables_getchain, |
7997 | .type = NFNL_CB_RCU, | |
96518518 PM |
7998 | .attr_count = NFTA_CHAIN_MAX, |
7999 | .policy = nft_chain_policy, | |
8000 | }, | |
8001 | [NFT_MSG_DELCHAIN] = { | |
50f2db9e PNA |
8002 | .call = nf_tables_delchain, |
8003 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8004 | .attr_count = NFTA_CHAIN_MAX, |
8005 | .policy = nft_chain_policy, | |
8006 | }, | |
8007 | [NFT_MSG_NEWRULE] = { | |
50f2db9e PNA |
8008 | .call = nf_tables_newrule, |
8009 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8010 | .attr_count = NFTA_RULE_MAX, |
8011 | .policy = nft_rule_policy, | |
8012 | }, | |
8013 | [NFT_MSG_GETRULE] = { | |
50f2db9e PNA |
8014 | .call = nf_tables_getrule, |
8015 | .type = NFNL_CB_RCU, | |
96518518 PM |
8016 | .attr_count = NFTA_RULE_MAX, |
8017 | .policy = nft_rule_policy, | |
8018 | }, | |
8019 | [NFT_MSG_DELRULE] = { | |
50f2db9e PNA |
8020 | .call = nf_tables_delrule, |
8021 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8022 | .attr_count = NFTA_RULE_MAX, |
8023 | .policy = nft_rule_policy, | |
8024 | }, | |
20a69341 | 8025 | [NFT_MSG_NEWSET] = { |
50f2db9e PNA |
8026 | .call = nf_tables_newset, |
8027 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8028 | .attr_count = NFTA_SET_MAX, |
8029 | .policy = nft_set_policy, | |
8030 | }, | |
8031 | [NFT_MSG_GETSET] = { | |
50f2db9e PNA |
8032 | .call = nf_tables_getset, |
8033 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8034 | .attr_count = NFTA_SET_MAX, |
8035 | .policy = nft_set_policy, | |
8036 | }, | |
8037 | [NFT_MSG_DELSET] = { | |
50f2db9e PNA |
8038 | .call = nf_tables_delset, |
8039 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8040 | .attr_count = NFTA_SET_MAX, |
8041 | .policy = nft_set_policy, | |
8042 | }, | |
8043 | [NFT_MSG_NEWSETELEM] = { | |
50f2db9e PNA |
8044 | .call = nf_tables_newsetelem, |
8045 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8046 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8047 | .policy = nft_set_elem_list_policy, | |
8048 | }, | |
8049 | [NFT_MSG_GETSETELEM] = { | |
50f2db9e PNA |
8050 | .call = nf_tables_getsetelem, |
8051 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8052 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8053 | .policy = nft_set_elem_list_policy, | |
8054 | }, | |
8055 | [NFT_MSG_DELSETELEM] = { | |
50f2db9e PNA |
8056 | .call = nf_tables_delsetelem, |
8057 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8058 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8059 | .policy = nft_set_elem_list_policy, | |
8060 | }, | |
84d7fce6 | 8061 | [NFT_MSG_GETGEN] = { |
50f2db9e PNA |
8062 | .call = nf_tables_getgen, |
8063 | .type = NFNL_CB_RCU, | |
84d7fce6 | 8064 | }, |
e5009240 | 8065 | [NFT_MSG_NEWOBJ] = { |
50f2db9e PNA |
8066 | .call = nf_tables_newobj, |
8067 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8068 | .attr_count = NFTA_OBJ_MAX, |
8069 | .policy = nft_obj_policy, | |
8070 | }, | |
8071 | [NFT_MSG_GETOBJ] = { | |
50f2db9e PNA |
8072 | .call = nf_tables_getobj, |
8073 | .type = NFNL_CB_RCU, | |
e5009240 PNA |
8074 | .attr_count = NFTA_OBJ_MAX, |
8075 | .policy = nft_obj_policy, | |
8076 | }, | |
8077 | [NFT_MSG_DELOBJ] = { | |
50f2db9e PNA |
8078 | .call = nf_tables_delobj, |
8079 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8080 | .attr_count = NFTA_OBJ_MAX, |
8081 | .policy = nft_obj_policy, | |
8082 | }, | |
43da04a5 | 8083 | [NFT_MSG_GETOBJ_RESET] = { |
50f2db9e PNA |
8084 | .call = nf_tables_getobj, |
8085 | .type = NFNL_CB_RCU, | |
43da04a5 PNA |
8086 | .attr_count = NFTA_OBJ_MAX, |
8087 | .policy = nft_obj_policy, | |
8088 | }, | |
3b49e2e9 | 8089 | [NFT_MSG_NEWFLOWTABLE] = { |
50f2db9e PNA |
8090 | .call = nf_tables_newflowtable, |
8091 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8092 | .attr_count = NFTA_FLOWTABLE_MAX, |
8093 | .policy = nft_flowtable_policy, | |
8094 | }, | |
8095 | [NFT_MSG_GETFLOWTABLE] = { | |
50f2db9e PNA |
8096 | .call = nf_tables_getflowtable, |
8097 | .type = NFNL_CB_RCU, | |
3b49e2e9 PNA |
8098 | .attr_count = NFTA_FLOWTABLE_MAX, |
8099 | .policy = nft_flowtable_policy, | |
8100 | }, | |
8101 | [NFT_MSG_DELFLOWTABLE] = { | |
50f2db9e PNA |
8102 | .call = nf_tables_delflowtable, |
8103 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
8104 | .attr_count = NFTA_FLOWTABLE_MAX, |
8105 | .policy = nft_flowtable_policy, | |
8106 | }, | |
96518518 PM |
8107 | }; |
8108 | ||
a654de8f PNA |
8109 | static int nf_tables_validate(struct net *net) |
8110 | { | |
d59d2f82 | 8111 | struct nftables_pernet *nft_net = nft_pernet(net); |
a654de8f PNA |
8112 | struct nft_table *table; |
8113 | ||
0854db2a | 8114 | switch (nft_net->validate_state) { |
a654de8f PNA |
8115 | case NFT_VALIDATE_SKIP: |
8116 | break; | |
8117 | case NFT_VALIDATE_NEED: | |
8118 | nft_validate_state_update(net, NFT_VALIDATE_DO); | |
954d8297 | 8119 | fallthrough; |
a654de8f | 8120 | case NFT_VALIDATE_DO: |
0854db2a | 8121 | list_for_each_entry(table, &nft_net->tables, list) { |
a654de8f PNA |
8122 | if (nft_table_validate(net, table) < 0) |
8123 | return -EAGAIN; | |
8124 | } | |
8125 | break; | |
8126 | } | |
8127 | ||
8128 | return 0; | |
8129 | } | |
8130 | ||
66293c46 FW |
8131 | /* a drop policy has to be deferred until all rules have been activated, |
8132 | * otherwise a large ruleset that contains a drop-policy base chain will | |
8133 | * cause all packets to get dropped until the full transaction has been | |
8134 | * processed. | |
8135 | * | |
8136 | * We defer the drop policy until the transaction has been finalized. | |
8137 | */ | |
8138 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
8139 | { | |
8140 | struct nft_base_chain *basechain; | |
8141 | ||
8142 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
8143 | return; | |
8144 | ||
8145 | if (!nft_is_base_chain(trans->ctx.chain)) | |
8146 | return; | |
8147 | ||
8148 | basechain = nft_base_chain(trans->ctx.chain); | |
8149 | basechain->policy = NF_DROP; | |
8150 | } | |
8151 | ||
91c7b38d PNA |
8152 | static void nft_chain_commit_update(struct nft_trans *trans) |
8153 | { | |
8154 | struct nft_base_chain *basechain; | |
8155 | ||
1b2470e5 FW |
8156 | if (nft_trans_chain_name(trans)) { |
8157 | rhltable_remove(&trans->ctx.table->chains_ht, | |
8158 | &trans->ctx.chain->rhlhead, | |
8159 | nft_chain_ht_params); | |
d71efb59 | 8160 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
8161 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
8162 | trans->ctx.chain->name, | |
8163 | &trans->ctx.chain->rhlhead, | |
8164 | nft_chain_ht_params); | |
8165 | } | |
91c7b38d | 8166 | |
f323d954 | 8167 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
8168 | return; |
8169 | ||
53315ac6 FW |
8170 | nft_chain_stats_replace(trans); |
8171 | ||
91c7b38d | 8172 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
8173 | |
8174 | switch (nft_trans_chain_policy(trans)) { | |
8175 | case NF_DROP: | |
8176 | case NF_ACCEPT: | |
8177 | basechain->policy = nft_trans_chain_policy(trans); | |
8178 | break; | |
8179 | } | |
8180 | } | |
8181 | ||
d62d0ba9 FFM |
8182 | static void nft_obj_commit_update(struct nft_trans *trans) |
8183 | { | |
8184 | struct nft_object *newobj; | |
8185 | struct nft_object *obj; | |
8186 | ||
8187 | obj = nft_trans_obj(trans); | |
8188 | newobj = nft_trans_obj_newobj(trans); | |
8189 | ||
9fedd894 FFM |
8190 | if (obj->ops->update) |
8191 | obj->ops->update(obj, newobj); | |
d62d0ba9 | 8192 | |
dad3bdee | 8193 | nft_obj_destroy(&trans->ctx, newobj); |
d62d0ba9 FFM |
8194 | } |
8195 | ||
2f99aa31 | 8196 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 8197 | { |
c7c32e72 PNA |
8198 | switch (trans->msg_type) { |
8199 | case NFT_MSG_DELTABLE: | |
8200 | nf_tables_table_destroy(&trans->ctx); | |
8201 | break; | |
9f8aac0b | 8202 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 8203 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
8204 | kfree(nft_trans_chain_name(trans)); |
8205 | break; | |
c7c32e72 | 8206 | case NFT_MSG_DELCHAIN: |
43a605f2 | 8207 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
8208 | break; |
8209 | case NFT_MSG_DELRULE: | |
8210 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
8211 | break; | |
8212 | case NFT_MSG_DELSET: | |
0c2a85ed | 8213 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 8214 | break; |
61edafbb | 8215 | case NFT_MSG_DELSETELEM: |
3453c927 PNA |
8216 | nf_tables_set_elem_destroy(&trans->ctx, |
8217 | nft_trans_elem_set(trans), | |
59105446 | 8218 | nft_trans_elem(trans).priv); |
61edafbb | 8219 | break; |
e5009240 | 8220 | case NFT_MSG_DELOBJ: |
00bfb320 | 8221 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 8222 | break; |
3b49e2e9 | 8223 | case NFT_MSG_DELFLOWTABLE: |
abadb2f8 PNA |
8224 | if (nft_trans_flowtable_update(trans)) |
8225 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
8226 | else | |
8227 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 8228 | break; |
c7c32e72 | 8229 | } |
0935d558 FW |
8230 | |
8231 | if (trans->put_net) | |
8232 | put_net(trans->ctx.net); | |
8233 | ||
c7c32e72 PNA |
8234 | kfree(trans); |
8235 | } | |
8236 | ||
0935d558 | 8237 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
8238 | { |
8239 | struct nft_trans *trans, *next; | |
0935d558 FW |
8240 | LIST_HEAD(head); |
8241 | ||
8242 | spin_lock(&nf_tables_destroy_list_lock); | |
8243 | list_splice_init(&nf_tables_destroy_list, &head); | |
8244 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 8245 | |
0935d558 | 8246 | if (list_empty(&head)) |
2f99aa31 FW |
8247 | return; |
8248 | ||
8249 | synchronize_rcu(); | |
8250 | ||
0935d558 | 8251 | list_for_each_entry_safe(trans, next, &head, list) { |
2f99aa31 FW |
8252 | list_del(&trans->list); |
8253 | nft_commit_release(trans); | |
8254 | } | |
8255 | } | |
8256 | ||
ffe8923f FW |
8257 | void nf_tables_trans_destroy_flush_work(void) |
8258 | { | |
8259 | flush_work(&trans_destroy_work); | |
8260 | } | |
8261 | EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work); | |
8262 | ||
ed5f85d4 PNA |
8263 | static bool nft_expr_reduce(struct nft_regs_track *track, |
8264 | const struct nft_expr *expr) | |
8265 | { | |
8266 | return false; | |
8267 | } | |
8268 | ||
0cbc06b3 FW |
8269 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
8270 | { | |
2c865a8a | 8271 | const struct nft_expr *expr, *last; |
12e4ecfa | 8272 | struct nft_regs_track track = {}; |
2c865a8a PNA |
8273 | unsigned int size, data_size; |
8274 | void *data, *data_boundary; | |
8275 | struct nft_rule_dp *prule; | |
0cbc06b3 | 8276 | struct nft_rule *rule; |
0cbc06b3 FW |
8277 | |
8278 | /* already handled or inactive chain? */ | |
2c865a8a | 8279 | if (chain->blob_next || !nft_is_active_next(net, chain)) |
0cbc06b3 FW |
8280 | return 0; |
8281 | ||
8282 | rule = list_entry(&chain->rules, struct nft_rule, list); | |
0cbc06b3 | 8283 | |
63045bfd | 8284 | data_size = 0; |
0cbc06b3 | 8285 | list_for_each_entry_continue(rule, &chain->rules, list) { |
2c865a8a PNA |
8286 | if (nft_is_active_next(net, rule)) { |
8287 | data_size += sizeof(*prule) + rule->dlen; | |
8288 | if (data_size > INT_MAX) | |
8289 | return -ENOMEM; | |
8290 | } | |
0cbc06b3 | 8291 | } |
2c865a8a | 8292 | data_size += offsetof(struct nft_rule_dp, data); /* last rule */ |
0cbc06b3 | 8293 | |
2c865a8a PNA |
8294 | chain->blob_next = nf_tables_chain_alloc_rules(data_size); |
8295 | if (!chain->blob_next) | |
0cbc06b3 FW |
8296 | return -ENOMEM; |
8297 | ||
2c865a8a PNA |
8298 | data = (void *)chain->blob_next->data; |
8299 | data_boundary = data + data_size; | |
8300 | size = 0; | |
8301 | ||
0cbc06b3 | 8302 | list_for_each_entry_continue(rule, &chain->rules, list) { |
2c865a8a PNA |
8303 | if (!nft_is_active_next(net, rule)) |
8304 | continue; | |
8305 | ||
8306 | prule = (struct nft_rule_dp *)data; | |
8307 | data += offsetof(struct nft_rule_dp, data); | |
8308 | if (WARN_ON_ONCE(data > data_boundary)) | |
8309 | return -ENOMEM; | |
8310 | ||
12e4ecfa | 8311 | size = 0; |
fe75e84a | 8312 | track.last = nft_expr_last(rule); |
2c865a8a | 8313 | nft_rule_for_each_expr(expr, last, rule) { |
12e4ecfa PNA |
8314 | track.cur = expr; |
8315 | ||
ed5f85d4 | 8316 | if (nft_expr_reduce(&track, expr)) { |
12e4ecfa PNA |
8317 | expr = track.cur; |
8318 | continue; | |
8319 | } | |
8320 | ||
2c865a8a PNA |
8321 | if (WARN_ON_ONCE(data + expr->ops->size > data_boundary)) |
8322 | return -ENOMEM; | |
8323 | ||
8324 | memcpy(data + size, expr, expr->ops->size); | |
8325 | size += expr->ops->size; | |
8326 | } | |
8327 | if (WARN_ON_ONCE(size >= 1 << 12)) | |
8328 | return -ENOMEM; | |
8329 | ||
8330 | prule->handle = rule->handle; | |
8331 | prule->dlen = size; | |
8332 | prule->is_last = 0; | |
8333 | ||
8334 | data += size; | |
8335 | size = 0; | |
8336 | chain->blob_next->size += (unsigned long)(data - (void *)prule); | |
0cbc06b3 FW |
8337 | } |
8338 | ||
2c865a8a PNA |
8339 | prule = (struct nft_rule_dp *)data; |
8340 | data += offsetof(struct nft_rule_dp, data); | |
8341 | if (WARN_ON_ONCE(data > data_boundary)) | |
8342 | return -ENOMEM; | |
8343 | ||
8344 | nft_last_rule(chain->blob_next, prule); | |
8345 | ||
0cbc06b3 FW |
8346 | return 0; |
8347 | } | |
8348 | ||
8349 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
8350 | { | |
d59d2f82 | 8351 | struct nftables_pernet *nft_net = nft_pernet(net); |
0cbc06b3 FW |
8352 | struct nft_trans *trans, *next; |
8353 | ||
0854db2a | 8354 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 FW |
8355 | struct nft_chain *chain = trans->ctx.chain; |
8356 | ||
8357 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
8358 | trans->msg_type == NFT_MSG_DELRULE) { | |
2c865a8a PNA |
8359 | kvfree(chain->blob_next); |
8360 | chain->blob_next = NULL; | |
0cbc06b3 FW |
8361 | } |
8362 | } | |
8363 | } | |
8364 | ||
8365 | static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h) | |
8366 | { | |
8367 | struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); | |
8368 | ||
2c865a8a | 8369 | kvfree(o->blob); |
0cbc06b3 FW |
8370 | } |
8371 | ||
2c865a8a | 8372 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob) |
0cbc06b3 | 8373 | { |
0cbc06b3 FW |
8374 | struct nft_rules_old *old; |
8375 | ||
2c865a8a PNA |
8376 | /* rcu_head is after end marker */ |
8377 | old = (void *)blob + sizeof(*blob) + blob->size; | |
8378 | old->blob = blob; | |
0cbc06b3 FW |
8379 | |
8380 | call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old); | |
8381 | } | |
8382 | ||
0fb39bbe | 8383 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 | 8384 | { |
2c865a8a | 8385 | struct nft_rule_blob *g0, *g1; |
0cbc06b3 FW |
8386 | bool next_genbit; |
8387 | ||
8388 | next_genbit = nft_gencursor_next(net); | |
8389 | ||
2c865a8a | 8390 | g0 = rcu_dereference_protected(chain->blob_gen_0, |
f102d66b | 8391 | lockdep_commit_lock_is_held(net)); |
2c865a8a | 8392 | g1 = rcu_dereference_protected(chain->blob_gen_1, |
f102d66b | 8393 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
8394 | |
8395 | /* No changes to this chain? */ | |
2c865a8a | 8396 | if (chain->blob_next == NULL) { |
0cbc06b3 FW |
8397 | /* chain had no change in last or next generation */ |
8398 | if (g0 == g1) | |
8399 | return; | |
8400 | /* | |
8401 | * chain had no change in this generation; make sure next | |
8402 | * one uses same rules as current generation. | |
8403 | */ | |
8404 | if (next_genbit) { | |
2c865a8a | 8405 | rcu_assign_pointer(chain->blob_gen_1, g0); |
0cbc06b3 FW |
8406 | nf_tables_commit_chain_free_rules_old(g1); |
8407 | } else { | |
2c865a8a | 8408 | rcu_assign_pointer(chain->blob_gen_0, g1); |
0cbc06b3 FW |
8409 | nf_tables_commit_chain_free_rules_old(g0); |
8410 | } | |
8411 | ||
8412 | return; | |
8413 | } | |
8414 | ||
8415 | if (next_genbit) | |
2c865a8a | 8416 | rcu_assign_pointer(chain->blob_gen_1, chain->blob_next); |
0cbc06b3 | 8417 | else |
2c865a8a | 8418 | rcu_assign_pointer(chain->blob_gen_0, chain->blob_next); |
0cbc06b3 | 8419 | |
2c865a8a | 8420 | chain->blob_next = NULL; |
0cbc06b3 FW |
8421 | |
8422 | if (g0 == g1) | |
8423 | return; | |
8424 | ||
8425 | if (next_genbit) | |
8426 | nf_tables_commit_chain_free_rules_old(g1); | |
8427 | else | |
8428 | nf_tables_commit_chain_free_rules_old(g0); | |
8429 | } | |
8430 | ||
d152159b FW |
8431 | static void nft_obj_del(struct nft_object *obj) |
8432 | { | |
4d44175a | 8433 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
8434 | list_del_rcu(&obj->list); |
8435 | } | |
8436 | ||
d0e2c7de | 8437 | void nft_chain_del(struct nft_chain *chain) |
1b2470e5 FW |
8438 | { |
8439 | struct nft_table *table = chain->table; | |
8440 | ||
8441 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
8442 | nft_chain_ht_params)); | |
8443 | list_del_rcu(&chain->list); | |
8444 | } | |
8445 | ||
abadb2f8 PNA |
8446 | static void nft_flowtable_hooks_del(struct nft_flowtable *flowtable, |
8447 | struct list_head *hook_list) | |
8448 | { | |
8449 | struct nft_hook *hook, *next; | |
8450 | ||
8451 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { | |
8452 | if (hook->inactive) | |
8453 | list_move(&hook->list, hook_list); | |
8454 | } | |
8455 | } | |
8456 | ||
eb014de4 PNA |
8457 | static void nf_tables_module_autoload_cleanup(struct net *net) |
8458 | { | |
d59d2f82 | 8459 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
8460 | struct nft_module_request *req, *next; |
8461 | ||
0854db2a FW |
8462 | WARN_ON_ONCE(!list_empty(&nft_net->commit_list)); |
8463 | list_for_each_entry_safe(req, next, &nft_net->module_list, list) { | |
eb014de4 PNA |
8464 | WARN_ON_ONCE(!req->done); |
8465 | list_del(&req->list); | |
8466 | kfree(req); | |
8467 | } | |
8468 | } | |
8469 | ||
0935d558 FW |
8470 | static void nf_tables_commit_release(struct net *net) |
8471 | { | |
d59d2f82 | 8472 | struct nftables_pernet *nft_net = nft_pernet(net); |
0935d558 FW |
8473 | struct nft_trans *trans; |
8474 | ||
8475 | /* all side effects have to be made visible. | |
8476 | * For example, if a chain named 'foo' has been deleted, a | |
8477 | * new transaction must not find it anymore. | |
8478 | * | |
8479 | * Memory reclaim happens asynchronously from work queue | |
8480 | * to prevent expensive synchronize_rcu() in commit phase. | |
8481 | */ | |
0854db2a | 8482 | if (list_empty(&nft_net->commit_list)) { |
eb014de4 | 8483 | nf_tables_module_autoload_cleanup(net); |
0854db2a | 8484 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
8485 | return; |
8486 | } | |
8487 | ||
0854db2a | 8488 | trans = list_last_entry(&nft_net->commit_list, |
0935d558 FW |
8489 | struct nft_trans, list); |
8490 | get_net(trans->ctx.net); | |
8491 | WARN_ON_ONCE(trans->put_net); | |
8492 | ||
8493 | trans->put_net = true; | |
8494 | spin_lock(&nf_tables_destroy_list_lock); | |
0854db2a | 8495 | list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list); |
0935d558 FW |
8496 | spin_unlock(&nf_tables_destroy_list_lock); |
8497 | ||
eb014de4 | 8498 | nf_tables_module_autoload_cleanup(net); |
0935d558 | 8499 | schedule_work(&trans_destroy_work); |
ffe8923f | 8500 | |
0854db2a | 8501 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
8502 | } |
8503 | ||
67cc570e PNA |
8504 | static void nft_commit_notify(struct net *net, u32 portid) |
8505 | { | |
d59d2f82 | 8506 | struct nftables_pernet *nft_net = nft_pernet(net); |
67cc570e PNA |
8507 | struct sk_buff *batch_skb = NULL, *nskb, *skb; |
8508 | unsigned char *data; | |
8509 | int len; | |
8510 | ||
0854db2a | 8511 | list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) { |
67cc570e PNA |
8512 | if (!batch_skb) { |
8513 | new_batch: | |
8514 | batch_skb = skb; | |
8515 | len = NLMSG_GOODSIZE - skb->len; | |
8516 | list_del(&skb->list); | |
8517 | continue; | |
8518 | } | |
8519 | len -= skb->len; | |
8520 | if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) { | |
8521 | data = skb_put(batch_skb, skb->len); | |
8522 | memcpy(data, skb->data, skb->len); | |
8523 | list_del(&skb->list); | |
8524 | kfree_skb(skb); | |
8525 | continue; | |
8526 | } | |
8527 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
8528 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
8529 | goto new_batch; | |
8530 | } | |
8531 | ||
8532 | if (batch_skb) { | |
8533 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
8534 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
8535 | } | |
8536 | ||
0854db2a | 8537 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); |
67cc570e PNA |
8538 | } |
8539 | ||
c520292f RGB |
8540 | static int nf_tables_commit_audit_alloc(struct list_head *adl, |
8541 | struct nft_table *table) | |
8542 | { | |
8543 | struct nft_audit_data *adp; | |
8544 | ||
8545 | list_for_each_entry(adp, adl, list) { | |
8546 | if (adp->table == table) | |
8547 | return 0; | |
8548 | } | |
8549 | adp = kzalloc(sizeof(*adp), GFP_KERNEL); | |
8550 | if (!adp) | |
8551 | return -ENOMEM; | |
8552 | adp->table = table; | |
8553 | list_add(&adp->list, adl); | |
8554 | return 0; | |
8555 | } | |
8556 | ||
cfbe3650 DM |
8557 | static void nf_tables_commit_audit_free(struct list_head *adl) |
8558 | { | |
8559 | struct nft_audit_data *adp, *adn; | |
8560 | ||
8561 | list_for_each_entry_safe(adp, adn, adl, list) { | |
8562 | list_del(&adp->list); | |
8563 | kfree(adp); | |
8564 | } | |
8565 | } | |
8566 | ||
c520292f RGB |
8567 | static void nf_tables_commit_audit_collect(struct list_head *adl, |
8568 | struct nft_table *table, u32 op) | |
8569 | { | |
8570 | struct nft_audit_data *adp; | |
8571 | ||
8572 | list_for_each_entry(adp, adl, list) { | |
8573 | if (adp->table == table) | |
8574 | goto found; | |
8575 | } | |
dadf33c9 | 8576 | WARN_ONCE(1, "table=%s not expected in commit list", table->name); |
c520292f RGB |
8577 | return; |
8578 | found: | |
8579 | adp->entries++; | |
8580 | if (!adp->op || adp->op > op) | |
8581 | adp->op = op; | |
8582 | } | |
8583 | ||
8584 | #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22) | |
8585 | ||
8586 | static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation) | |
8587 | { | |
8588 | struct nft_audit_data *adp, *adn; | |
8589 | char aubuf[AUNFTABLENAMELEN]; | |
8590 | ||
8591 | list_for_each_entry_safe(adp, adn, adl, list) { | |
8592 | snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name, | |
8593 | generation); | |
8594 | audit_log_nfcfg(aubuf, adp->table->family, adp->entries, | |
8595 | nft2audit_op[adp->op], GFP_KERNEL); | |
8596 | list_del(&adp->list); | |
8597 | kfree(adp); | |
8598 | } | |
8599 | } | |
8600 | ||
5913beaf | 8601 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 8602 | { |
d59d2f82 | 8603 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 8604 | struct nft_trans *trans, *next; |
a3716e70 | 8605 | struct nft_trans_elem *te; |
0cbc06b3 FW |
8606 | struct nft_chain *chain; |
8607 | struct nft_table *table; | |
c520292f | 8608 | LIST_HEAD(adl); |
c9626a2c | 8609 | int err; |
37082f93 | 8610 | |
0854db2a FW |
8611 | if (list_empty(&nft_net->commit_list)) { |
8612 | mutex_unlock(&nft_net->commit_mutex); | |
b8b27498 FW |
8613 | return 0; |
8614 | } | |
8615 | ||
a654de8f PNA |
8616 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
8617 | if (nf_tables_validate(net) < 0) | |
8618 | return -EAGAIN; | |
8619 | ||
c9626a2c PNA |
8620 | err = nft_flow_rule_offload_commit(net); |
8621 | if (err < 0) | |
8622 | return err; | |
8623 | ||
0cbc06b3 | 8624 | /* 1. Allocate space for next generation rules_gen_X[] */ |
0854db2a | 8625 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 | 8626 | int ret; |
37082f93 | 8627 | |
c520292f RGB |
8628 | ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table); |
8629 | if (ret) { | |
8630 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 8631 | nf_tables_commit_audit_free(&adl); |
c520292f RGB |
8632 | return ret; |
8633 | } | |
0cbc06b3 FW |
8634 | if (trans->msg_type == NFT_MSG_NEWRULE || |
8635 | trans->msg_type == NFT_MSG_DELRULE) { | |
8636 | chain = trans->ctx.chain; | |
8637 | ||
8638 | ret = nf_tables_commit_chain_prepare(net, chain); | |
8639 | if (ret < 0) { | |
8640 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 8641 | nf_tables_commit_audit_free(&adl); |
0cbc06b3 FW |
8642 | return ret; |
8643 | } | |
8644 | } | |
8645 | } | |
37082f93 | 8646 | |
0cbc06b3 | 8647 | /* step 2. Make rules_gen_X visible to packet path */ |
0854db2a | 8648 | list_for_each_entry(table, &nft_net->tables, list) { |
0fb39bbe FW |
8649 | list_for_each_entry(chain, &table->chains, list) |
8650 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
8651 | } |
8652 | ||
8653 | /* | |
8654 | * Bump generation counter, invalidate any dump in progress. | |
8655 | * Cannot fail after this point. | |
37082f93 | 8656 | */ |
0854db2a FW |
8657 | while (++nft_net->base_seq == 0) |
8658 | ; | |
0cbc06b3 FW |
8659 | |
8660 | /* step 3. Start new generation, rules_gen_X now in use. */ | |
8661 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 | 8662 | |
0854db2a | 8663 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
c520292f RGB |
8664 | nf_tables_commit_audit_collect(&adl, trans->ctx.table, |
8665 | trans->msg_type); | |
b380e5c7 | 8666 | switch (trans->msg_type) { |
55dd6f93 PNA |
8667 | case NFT_MSG_NEWTABLE: |
8668 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
8669 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
8670 | nft_trans_destroy(trans); | |
8671 | break; | |
8672 | } | |
8673 | if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT) | |
0ce7cf41 PNA |
8674 | nf_tables_table_disable(net, trans->ctx.table); |
8675 | ||
179d9ba5 | 8676 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; |
55dd6f93 | 8677 | } else { |
f2a6d766 | 8678 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 8679 | } |
35151d84 | 8680 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
8681 | nft_trans_destroy(trans); |
8682 | break; | |
8683 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 8684 | list_del_rcu(&trans->ctx.table->list); |
35151d84 | 8685 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
55dd6f93 | 8686 | break; |
91c7b38d | 8687 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 8688 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 8689 | nft_chain_commit_update(trans); |
9f8aac0b FW |
8690 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
8691 | /* trans destroyed after rcu grace period */ | |
8692 | } else { | |
66293c46 | 8693 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 8694 | nft_clear(net, trans->ctx.chain); |
9f8aac0b FW |
8695 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
8696 | nft_trans_destroy(trans); | |
8697 | } | |
91c7b38d PNA |
8698 | break; |
8699 | case NFT_MSG_DELCHAIN: | |
1b2470e5 | 8700 | nft_chain_del(trans->ctx.chain); |
35151d84 | 8701 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
c974a3a3 PNA |
8702 | nf_tables_unregister_hook(trans->ctx.net, |
8703 | trans->ctx.table, | |
8704 | trans->ctx.chain); | |
91c7b38d | 8705 | break; |
b380e5c7 | 8706 | case NFT_MSG_NEWRULE: |
889f7ee7 | 8707 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 8708 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 8709 | nft_trans_rule(trans), |
35151d84 | 8710 | NFT_MSG_NEWRULE); |
37082f93 | 8711 | nft_trans_destroy(trans); |
b380e5c7 PNA |
8712 | break; |
8713 | case NFT_MSG_DELRULE: | |
8714 | list_del_rcu(&nft_trans_rule(trans)->list); | |
35151d84 PNA |
8715 | nf_tables_rule_notify(&trans->ctx, |
8716 | nft_trans_rule(trans), | |
8717 | NFT_MSG_DELRULE); | |
f6ac8585 PNA |
8718 | nft_rule_expr_deactivate(&trans->ctx, |
8719 | nft_trans_rule(trans), | |
8720 | NFT_TRANS_COMMIT); | |
b380e5c7 | 8721 | break; |
958bee14 | 8722 | case NFT_MSG_NEWSET: |
37a9cc52 | 8723 | nft_clear(net, nft_trans_set(trans)); |
4fefee57 PNA |
8724 | /* This avoids hitting -EBUSY when deleting the table |
8725 | * from the transaction. | |
8726 | */ | |
408070d6 | 8727 | if (nft_set_is_anonymous(nft_trans_set(trans)) && |
4fefee57 PNA |
8728 | !list_empty(&nft_trans_set(trans)->bindings)) |
8729 | trans->ctx.table->use--; | |
8730 | ||
958bee14 | 8731 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 8732 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
8733 | nft_trans_destroy(trans); |
8734 | break; | |
8735 | case NFT_MSG_DELSET: | |
37a9cc52 | 8736 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 8737 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 8738 | NFT_MSG_DELSET, GFP_KERNEL); |
958bee14 | 8739 | break; |
60319eb1 | 8740 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
8741 | te = (struct nft_trans_elem *)trans->data; |
8742 | ||
aaa31047 | 8743 | nft_setelem_activate(net, te->set, &te->elem); |
cc02e457 PM |
8744 | nf_tables_setelem_notify(&trans->ctx, te->set, |
8745 | &te->elem, | |
6fb721cf | 8746 | NFT_MSG_NEWSETELEM); |
60319eb1 PNA |
8747 | nft_trans_destroy(trans); |
8748 | break; | |
8749 | case NFT_MSG_DELSETELEM: | |
a3716e70 | 8750 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 8751 | |
a3716e70 PNA |
8752 | nf_tables_setelem_notify(&trans->ctx, te->set, |
8753 | &te->elem, | |
6fb721cf | 8754 | NFT_MSG_DELSETELEM); |
aaa31047 PNA |
8755 | nft_setelem_remove(net, te->set, &te->elem); |
8756 | if (!nft_setelem_is_catchall(te->set, &te->elem)) { | |
8757 | atomic_dec(&te->set->nelems); | |
8758 | te->set->ndeact--; | |
8759 | } | |
60319eb1 | 8760 | break; |
e5009240 | 8761 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
8762 | if (nft_trans_obj_update(trans)) { |
8763 | nft_obj_commit_update(trans); | |
8764 | nf_tables_obj_notify(&trans->ctx, | |
8765 | nft_trans_obj(trans), | |
8766 | NFT_MSG_NEWOBJ); | |
8767 | } else { | |
8768 | nft_clear(net, nft_trans_obj(trans)); | |
8769 | nf_tables_obj_notify(&trans->ctx, | |
8770 | nft_trans_obj(trans), | |
8771 | NFT_MSG_NEWOBJ); | |
8772 | nft_trans_destroy(trans); | |
8773 | } | |
e5009240 PNA |
8774 | break; |
8775 | case NFT_MSG_DELOBJ: | |
d152159b | 8776 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 PNA |
8777 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
8778 | NFT_MSG_DELOBJ); | |
8779 | break; | |
3b49e2e9 | 8780 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 8781 | if (nft_trans_flowtable_update(trans)) { |
7b35582c PNA |
8782 | nft_trans_flowtable(trans)->data.flags = |
8783 | nft_trans_flowtable_flags(trans); | |
78d9f48f PNA |
8784 | nf_tables_flowtable_notify(&trans->ctx, |
8785 | nft_trans_flowtable(trans), | |
8786 | &nft_trans_flowtable_hooks(trans), | |
8787 | NFT_MSG_NEWFLOWTABLE); | |
8788 | list_splice(&nft_trans_flowtable_hooks(trans), | |
8789 | &nft_trans_flowtable(trans)->hook_list); | |
8790 | } else { | |
8791 | nft_clear(net, nft_trans_flowtable(trans)); | |
8792 | nf_tables_flowtable_notify(&trans->ctx, | |
8793 | nft_trans_flowtable(trans), | |
8794 | &nft_trans_flowtable(trans)->hook_list, | |
8795 | NFT_MSG_NEWFLOWTABLE); | |
8796 | } | |
3b49e2e9 PNA |
8797 | nft_trans_destroy(trans); |
8798 | break; | |
8799 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 PNA |
8800 | if (nft_trans_flowtable_update(trans)) { |
8801 | nft_flowtable_hooks_del(nft_trans_flowtable(trans), | |
8802 | &nft_trans_flowtable_hooks(trans)); | |
8803 | nf_tables_flowtable_notify(&trans->ctx, | |
8804 | nft_trans_flowtable(trans), | |
8805 | &nft_trans_flowtable_hooks(trans), | |
8806 | NFT_MSG_DELFLOWTABLE); | |
8807 | nft_unregister_flowtable_net_hooks(net, | |
8808 | &nft_trans_flowtable_hooks(trans)); | |
8809 | } else { | |
8810 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
8811 | nf_tables_flowtable_notify(&trans->ctx, | |
8812 | nft_trans_flowtable(trans), | |
8813 | &nft_trans_flowtable(trans)->hook_list, | |
8814 | NFT_MSG_DELFLOWTABLE); | |
8815 | nft_unregister_flowtable_net_hooks(net, | |
8816 | &nft_trans_flowtable(trans)->hook_list); | |
8817 | } | |
3b49e2e9 | 8818 | break; |
37082f93 | 8819 | } |
37082f93 PNA |
8820 | } |
8821 | ||
67cc570e | 8822 | nft_commit_notify(net, NETLINK_CB(skb).portid); |
84d7fce6 | 8823 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0854db2a | 8824 | nf_tables_commit_audit_log(&adl, nft_net->base_seq); |
0935d558 | 8825 | nf_tables_commit_release(net); |
37082f93 PNA |
8826 | |
8827 | return 0; | |
8828 | } | |
8829 | ||
eb014de4 PNA |
8830 | static void nf_tables_module_autoload(struct net *net) |
8831 | { | |
d59d2f82 | 8832 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
8833 | struct nft_module_request *req, *next; |
8834 | LIST_HEAD(module_list); | |
8835 | ||
0854db2a FW |
8836 | list_splice_init(&nft_net->module_list, &module_list); |
8837 | mutex_unlock(&nft_net->commit_mutex); | |
eb014de4 | 8838 | list_for_each_entry_safe(req, next, &module_list, list) { |
1d305ba4 FW |
8839 | request_module("%s", req->module); |
8840 | req->done = true; | |
eb014de4 | 8841 | } |
0854db2a FW |
8842 | mutex_lock(&nft_net->commit_mutex); |
8843 | list_splice(&module_list, &nft_net->module_list); | |
eb014de4 PNA |
8844 | } |
8845 | ||
b326dd37 | 8846 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 8847 | { |
c7c32e72 PNA |
8848 | switch (trans->msg_type) { |
8849 | case NFT_MSG_NEWTABLE: | |
8850 | nf_tables_table_destroy(&trans->ctx); | |
8851 | break; | |
8852 | case NFT_MSG_NEWCHAIN: | |
43a605f2 | 8853 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
8854 | break; |
8855 | case NFT_MSG_NEWRULE: | |
8856 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
8857 | break; | |
8858 | case NFT_MSG_NEWSET: | |
0c2a85ed | 8859 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 8860 | break; |
61edafbb PM |
8861 | case NFT_MSG_NEWSETELEM: |
8862 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 8863 | nft_trans_elem(trans).priv, true); |
61edafbb | 8864 | break; |
e5009240 | 8865 | case NFT_MSG_NEWOBJ: |
00bfb320 | 8866 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 8867 | break; |
3b49e2e9 | 8868 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
8869 | if (nft_trans_flowtable_update(trans)) |
8870 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
8871 | else | |
8872 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 8873 | break; |
c7c32e72 PNA |
8874 | } |
8875 | kfree(trans); | |
8876 | } | |
8877 | ||
c0391b6a | 8878 | static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) |
37082f93 | 8879 | { |
d59d2f82 | 8880 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 8881 | struct nft_trans *trans, *next; |
02263db0 | 8882 | struct nft_trans_elem *te; |
abadb2f8 | 8883 | struct nft_hook *hook; |
37082f93 | 8884 | |
c0391b6a PNA |
8885 | if (action == NFNL_ABORT_VALIDATE && |
8886 | nf_tables_validate(net) < 0) | |
8887 | return -EAGAIN; | |
8888 | ||
0854db2a | 8889 | list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list, |
a907e36d | 8890 | list) { |
b380e5c7 | 8891 | switch (trans->msg_type) { |
55dd6f93 PNA |
8892 | case NFT_MSG_NEWTABLE: |
8893 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
8894 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
8895 | nft_trans_destroy(trans); | |
8896 | break; | |
8897 | } | |
8898 | if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) { | |
0ce7cf41 | 8899 | nf_tables_table_disable(net, trans->ctx.table); |
179d9ba5 PNA |
8900 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
8901 | } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) { | |
8902 | trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT; | |
8903 | } | |
8904 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; | |
55dd6f93 PNA |
8905 | nft_trans_destroy(trans); |
8906 | } else { | |
e688a7f8 | 8907 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
8908 | } |
8909 | break; | |
8910 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 8911 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
8912 | nft_trans_destroy(trans); |
8913 | break; | |
91c7b38d PNA |
8914 | case NFT_MSG_NEWCHAIN: |
8915 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 8916 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 8917 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
8918 | nft_trans_destroy(trans); |
8919 | } else { | |
d0e2c7de PNA |
8920 | if (nft_chain_is_bound(trans->ctx.chain)) { |
8921 | nft_trans_destroy(trans); | |
8922 | break; | |
8923 | } | |
4fefee57 | 8924 | trans->ctx.table->use--; |
1b2470e5 | 8925 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
8926 | nf_tables_unregister_hook(trans->ctx.net, |
8927 | trans->ctx.table, | |
8928 | trans->ctx.chain); | |
91c7b38d PNA |
8929 | } |
8930 | break; | |
8931 | case NFT_MSG_DELCHAIN: | |
4fefee57 | 8932 | trans->ctx.table->use++; |
664b0f8c | 8933 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
8934 | nft_trans_destroy(trans); |
8935 | break; | |
b380e5c7 | 8936 | case NFT_MSG_NEWRULE: |
4fefee57 | 8937 | trans->ctx.chain->use--; |
b380e5c7 | 8938 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
8939 | nft_rule_expr_deactivate(&trans->ctx, |
8940 | nft_trans_rule(trans), | |
8941 | NFT_TRANS_ABORT); | |
3c5e4462 PNA |
8942 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
8943 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 PNA |
8944 | break; |
8945 | case NFT_MSG_DELRULE: | |
4fefee57 | 8946 | trans->ctx.chain->use++; |
889f7ee7 | 8947 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 8948 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
3c5e4462 PNA |
8949 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
8950 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
8951 | ||
37082f93 | 8952 | nft_trans_destroy(trans); |
b380e5c7 | 8953 | break; |
958bee14 | 8954 | case NFT_MSG_NEWSET: |
4fefee57 | 8955 | trans->ctx.table->use--; |
6a0a8d10 | 8956 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
8957 | nft_trans_destroy(trans); |
8958 | break; | |
8959 | } | |
8960 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
8961 | break; |
8962 | case NFT_MSG_DELSET: | |
4fefee57 | 8963 | trans->ctx.table->use++; |
37a9cc52 | 8964 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
8965 | nft_trans_destroy(trans); |
8966 | break; | |
60319eb1 | 8967 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 8968 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
8969 | nft_trans_destroy(trans); |
8970 | break; | |
8971 | } | |
02263db0 | 8972 | te = (struct nft_trans_elem *)trans->data; |
aaa31047 PNA |
8973 | nft_setelem_remove(net, te->set, &te->elem); |
8974 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
8975 | atomic_dec(&te->set->nelems); | |
60319eb1 PNA |
8976 | break; |
8977 | case NFT_MSG_DELSETELEM: | |
cc02e457 PM |
8978 | te = (struct nft_trans_elem *)trans->data; |
8979 | ||
f8bb7889 | 8980 | nft_setelem_data_activate(net, te->set, &te->elem); |
aaa31047 PNA |
8981 | nft_setelem_activate(net, te->set, &te->elem); |
8982 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
8983 | te->set->ndeact--; | |
cc02e457 | 8984 | |
e5009240 PNA |
8985 | nft_trans_destroy(trans); |
8986 | break; | |
8987 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 | 8988 | if (nft_trans_obj_update(trans)) { |
dad3bdee | 8989 | nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans)); |
d62d0ba9 FFM |
8990 | nft_trans_destroy(trans); |
8991 | } else { | |
8992 | trans->ctx.table->use--; | |
8993 | nft_obj_del(nft_trans_obj(trans)); | |
8994 | } | |
e5009240 PNA |
8995 | break; |
8996 | case NFT_MSG_DELOBJ: | |
8997 | trans->ctx.table->use++; | |
8998 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
8999 | nft_trans_destroy(trans); |
9000 | break; | |
3b49e2e9 | 9001 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
9002 | if (nft_trans_flowtable_update(trans)) { |
9003 | nft_unregister_flowtable_net_hooks(net, | |
9004 | &nft_trans_flowtable_hooks(trans)); | |
9005 | } else { | |
9006 | trans->ctx.table->use--; | |
9007 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
9008 | nft_unregister_flowtable_net_hooks(net, | |
9009 | &nft_trans_flowtable(trans)->hook_list); | |
9010 | } | |
3b49e2e9 PNA |
9011 | break; |
9012 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 PNA |
9013 | if (nft_trans_flowtable_update(trans)) { |
9014 | list_for_each_entry(hook, &nft_trans_flowtable(trans)->hook_list, list) | |
9015 | hook->inactive = false; | |
9016 | } else { | |
9017 | trans->ctx.table->use++; | |
9018 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
9019 | } | |
3b49e2e9 PNA |
9020 | nft_trans_destroy(trans); |
9021 | break; | |
37082f93 | 9022 | } |
37082f93 PNA |
9023 | } |
9024 | ||
b326dd37 PNA |
9025 | synchronize_rcu(); |
9026 | ||
a1cee076 | 9027 | list_for_each_entry_safe_reverse(trans, next, |
0854db2a | 9028 | &nft_net->commit_list, list) { |
c7c32e72 | 9029 | list_del(&trans->list); |
b326dd37 | 9030 | nf_tables_abort_release(trans); |
37082f93 PNA |
9031 | } |
9032 | ||
c0391b6a | 9033 | if (action == NFNL_ABORT_AUTOLOAD) |
eb014de4 PNA |
9034 | nf_tables_module_autoload(net); |
9035 | else | |
9036 | nf_tables_module_autoload_cleanup(net); | |
9037 | ||
37082f93 PNA |
9038 | return 0; |
9039 | } | |
9040 | ||
a654de8f PNA |
9041 | static void nf_tables_cleanup(struct net *net) |
9042 | { | |
9043 | nft_validate_state_update(net, NFT_VALIDATE_SKIP); | |
9044 | } | |
9045 | ||
c0391b6a PNA |
9046 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, |
9047 | enum nfnl_abort_action action) | |
71ad00c5 | 9048 | { |
d59d2f82 | 9049 | struct nftables_pernet *nft_net = nft_pernet(net); |
c0391b6a | 9050 | int ret = __nf_tables_abort(net, action); |
f102d66b | 9051 | |
0854db2a | 9052 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9053 | |
9054 | return ret; | |
71ad00c5 FW |
9055 | } |
9056 | ||
74e8bcd2 PNA |
9057 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
9058 | { | |
d59d2f82 | 9059 | struct nftables_pernet *nft_net = nft_pernet(net); |
f102d66b FW |
9060 | bool genid_ok; |
9061 | ||
0854db2a | 9062 | mutex_lock(&nft_net->commit_mutex); |
f102d66b | 9063 | |
0854db2a | 9064 | genid_ok = genid == 0 || nft_net->base_seq == genid; |
f102d66b | 9065 | if (!genid_ok) |
0854db2a | 9066 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
9067 | |
9068 | /* else, commit mutex has to be released by commit or abort function */ | |
9069 | return genid_ok; | |
74e8bcd2 PNA |
9070 | } |
9071 | ||
96518518 PM |
9072 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
9073 | .name = "nf_tables", | |
9074 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
9075 | .cb_count = NFT_MSG_MAX, | |
9076 | .cb = nf_tables_cb, | |
0628b123 PNA |
9077 | .commit = nf_tables_commit, |
9078 | .abort = nf_tables_abort, | |
a654de8f | 9079 | .cleanup = nf_tables_cleanup, |
74e8bcd2 | 9080 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 9081 | .owner = THIS_MODULE, |
96518518 PM |
9082 | }; |
9083 | ||
7210e4e3 | 9084 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 9085 | enum nft_chain_types type) |
7210e4e3 PNA |
9086 | { |
9087 | const struct nft_base_chain *basechain; | |
9088 | ||
f323d954 | 9089 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
9090 | basechain = nft_base_chain(chain); |
9091 | if (basechain->type->type != type) | |
9092 | return -EOPNOTSUPP; | |
9093 | } | |
9094 | return 0; | |
9095 | } | |
9096 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
9097 | ||
75e8d06d PNA |
9098 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
9099 | unsigned int hook_flags) | |
9100 | { | |
9101 | struct nft_base_chain *basechain; | |
9102 | ||
f323d954 | 9103 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
9104 | basechain = nft_base_chain(chain); |
9105 | ||
c974a3a3 | 9106 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
9107 | return 0; |
9108 | ||
9109 | return -EOPNOTSUPP; | |
9110 | } | |
9111 | ||
9112 | return 0; | |
9113 | } | |
9114 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
9115 | ||
20a69341 PM |
9116 | /* |
9117 | * Loop detection - walk through the ruleset beginning at the destination chain | |
9118 | * of a new jump until either the source chain is reached (loop) or all | |
9119 | * reachable chains have been traversed. | |
9120 | * | |
9121 | * The loop check is performed whenever a new jump verdict is added to an | |
9122 | * expression or verdict map or a verdict map is bound to a new chain. | |
9123 | */ | |
9124 | ||
9125 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
9126 | const struct nft_chain *chain); | |
9127 | ||
6387aa6e PNA |
9128 | static int nft_check_loops(const struct nft_ctx *ctx, |
9129 | const struct nft_set_ext *ext) | |
9130 | { | |
9131 | const struct nft_data *data; | |
9132 | int ret; | |
9133 | ||
9134 | data = nft_set_ext_data(ext); | |
9135 | switch (data->verdict.code) { | |
9136 | case NFT_JUMP: | |
9137 | case NFT_GOTO: | |
9138 | ret = nf_tables_check_loops(ctx, data->verdict.chain); | |
9139 | break; | |
9140 | default: | |
9141 | ret = 0; | |
9142 | break; | |
9143 | } | |
9144 | ||
9145 | return ret; | |
9146 | } | |
9147 | ||
20a69341 | 9148 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, |
de70185d | 9149 | struct nft_set *set, |
20a69341 | 9150 | const struct nft_set_iter *iter, |
de70185d | 9151 | struct nft_set_elem *elem) |
20a69341 | 9152 | { |
fe2811eb | 9153 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
fe2811eb PM |
9154 | |
9155 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
9156 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
9157 | return 0; |
9158 | ||
6387aa6e | 9159 | return nft_check_loops(ctx, ext); |
20a69341 PM |
9160 | } |
9161 | ||
aaa31047 PNA |
9162 | static int nft_set_catchall_loops(const struct nft_ctx *ctx, |
9163 | struct nft_set *set) | |
9164 | { | |
9165 | u8 genmask = nft_genmask_next(ctx->net); | |
9166 | struct nft_set_elem_catchall *catchall; | |
9167 | struct nft_set_ext *ext; | |
9168 | int ret = 0; | |
9169 | ||
9170 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
9171 | ext = nft_set_elem_ext(set, catchall->elem); | |
9172 | if (!nft_set_elem_active(ext, genmask)) | |
9173 | continue; | |
9174 | ||
9175 | ret = nft_check_loops(ctx, ext); | |
9176 | if (ret < 0) | |
9177 | return ret; | |
9178 | } | |
9179 | ||
9180 | return ret; | |
9181 | } | |
9182 | ||
20a69341 PM |
9183 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
9184 | const struct nft_chain *chain) | |
9185 | { | |
9186 | const struct nft_rule *rule; | |
9187 | const struct nft_expr *expr, *last; | |
de70185d | 9188 | struct nft_set *set; |
20a69341 PM |
9189 | struct nft_set_binding *binding; |
9190 | struct nft_set_iter iter; | |
20a69341 PM |
9191 | |
9192 | if (ctx->chain == chain) | |
9193 | return -ELOOP; | |
9194 | ||
9195 | list_for_each_entry(rule, &chain->rules, list) { | |
9196 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
9197 | struct nft_immediate_expr *priv; |
9198 | const struct nft_data *data; | |
0ca743a5 PNA |
9199 | int err; |
9200 | ||
a654de8f | 9201 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
9202 | continue; |
9203 | ||
a654de8f PNA |
9204 | priv = nft_expr_priv(expr); |
9205 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 9206 | continue; |
20a69341 | 9207 | |
a654de8f | 9208 | data = &priv->data; |
1ca2e170 | 9209 | switch (data->verdict.code) { |
20a69341 PM |
9210 | case NFT_JUMP: |
9211 | case NFT_GOTO: | |
1ca2e170 PM |
9212 | err = nf_tables_check_loops(ctx, |
9213 | data->verdict.chain); | |
20a69341 PM |
9214 | if (err < 0) |
9215 | return err; | |
c2168e6b | 9216 | break; |
20a69341 PM |
9217 | default: |
9218 | break; | |
9219 | } | |
9220 | } | |
9221 | } | |
9222 | ||
9223 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
9224 | if (!nft_is_active_next(ctx->net, set)) |
9225 | continue; | |
20a69341 PM |
9226 | if (!(set->flags & NFT_SET_MAP) || |
9227 | set->dtype != NFT_DATA_VERDICT) | |
9228 | continue; | |
9229 | ||
9230 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
9231 | if (!(binding->flags & NFT_SET_MAP) || |
9232 | binding->chain != chain) | |
20a69341 PM |
9233 | continue; |
9234 | ||
8588ac09 | 9235 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
9236 | iter.skip = 0; |
9237 | iter.count = 0; | |
9238 | iter.err = 0; | |
9239 | iter.fn = nf_tables_loop_check_setelem; | |
9240 | ||
9241 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
9242 | if (!iter.err) |
9243 | iter.err = nft_set_catchall_loops(ctx, set); | |
9244 | ||
20a69341 PM |
9245 | if (iter.err < 0) |
9246 | return iter.err; | |
9247 | } | |
9248 | } | |
9249 | ||
9250 | return 0; | |
9251 | } | |
9252 | ||
36b701fa LGL |
9253 | /** |
9254 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
9255 | * | |
9256 | * @attr: netlink attribute to fetch value from | |
9257 | * @max: maximum value to be stored in dest | |
9258 | * @dest: pointer to the variable | |
9259 | * | |
9260 | * Parse, check and store a given u32 netlink attribute into variable. | |
9261 | * This function returns -ERANGE if the value goes over maximum value. | |
9262 | * Otherwise a 0 is returned and the attribute value is stored in the | |
9263 | * destination variable. | |
9264 | */ | |
f1d505bb | 9265 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 9266 | { |
09525a09 | 9267 | u32 val; |
36b701fa LGL |
9268 | |
9269 | val = ntohl(nla_get_be32(attr)); | |
9270 | if (val > max) | |
9271 | return -ERANGE; | |
9272 | ||
9273 | *dest = val; | |
9274 | return 0; | |
9275 | } | |
9276 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
9277 | ||
08a01c11 | 9278 | static unsigned int nft_parse_register(const struct nlattr *attr) |
b1c96ed3 | 9279 | { |
49499c3e PM |
9280 | unsigned int reg; |
9281 | ||
9282 | reg = ntohl(nla_get_be32(attr)); | |
9283 | switch (reg) { | |
9284 | case NFT_REG_VERDICT...NFT_REG_4: | |
9285 | return reg * NFT_REG_SIZE / NFT_REG32_SIZE; | |
9286 | default: | |
9287 | return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
9288 | } | |
b1c96ed3 | 9289 | } |
b1c96ed3 | 9290 | |
49499c3e PM |
9291 | /** |
9292 | * nft_dump_register - dump a register value to a netlink attribute | |
9293 | * | |
9294 | * @skb: socket buffer | |
9295 | * @attr: attribute number | |
9296 | * @reg: register number | |
9297 | * | |
9298 | * Construct a netlink attribute containing the register number. For | |
9299 | * compatibility reasons, register numbers being a multiple of 4 are | |
9300 | * translated to the corresponding 128 bit register numbers. | |
9301 | */ | |
b1c96ed3 PM |
9302 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
9303 | { | |
49499c3e PM |
9304 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
9305 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
9306 | else | |
9307 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
9308 | ||
b1c96ed3 PM |
9309 | return nla_put_be32(skb, attr, htonl(reg)); |
9310 | } | |
9311 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
9312 | ||
4f16d25c | 9313 | static int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 9314 | { |
49499c3e | 9315 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 9316 | return -EINVAL; |
d07db988 PM |
9317 | if (len == 0) |
9318 | return -EINVAL; | |
c593642c | 9319 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 9320 | return -ERANGE; |
49499c3e | 9321 | |
96518518 PM |
9322 | return 0; |
9323 | } | |
4f16d25c PNA |
9324 | |
9325 | int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len) | |
9326 | { | |
9327 | u32 reg; | |
9328 | int err; | |
9329 | ||
9330 | reg = nft_parse_register(attr); | |
9331 | err = nft_validate_register_load(reg, len); | |
9332 | if (err < 0) | |
9333 | return err; | |
9334 | ||
9335 | *sreg = reg; | |
9336 | return 0; | |
9337 | } | |
9338 | EXPORT_SYMBOL_GPL(nft_parse_register_load); | |
96518518 | 9339 | |
345023b0 PNA |
9340 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
9341 | enum nft_registers reg, | |
9342 | const struct nft_data *data, | |
9343 | enum nft_data_types type, | |
9344 | unsigned int len) | |
96518518 | 9345 | { |
20a69341 PM |
9346 | int err; |
9347 | ||
96518518 PM |
9348 | switch (reg) { |
9349 | case NFT_REG_VERDICT: | |
58f40ab6 | 9350 | if (type != NFT_DATA_VERDICT) |
96518518 | 9351 | return -EINVAL; |
20a69341 | 9352 | |
58f40ab6 | 9353 | if (data != NULL && |
1ca2e170 PM |
9354 | (data->verdict.code == NFT_GOTO || |
9355 | data->verdict.code == NFT_JUMP)) { | |
9356 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
9357 | if (err < 0) |
9358 | return err; | |
20a69341 PM |
9359 | } |
9360 | ||
96518518 PM |
9361 | return 0; |
9362 | default: | |
49499c3e | 9363 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 9364 | return -EINVAL; |
45d9bcda PM |
9365 | if (len == 0) |
9366 | return -EINVAL; | |
49499c3e | 9367 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 9368 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 9369 | return -ERANGE; |
27e6d201 | 9370 | |
96518518 PM |
9371 | if (data != NULL && type != NFT_DATA_VALUE) |
9372 | return -EINVAL; | |
9373 | return 0; | |
9374 | } | |
9375 | } | |
345023b0 PNA |
9376 | |
9377 | int nft_parse_register_store(const struct nft_ctx *ctx, | |
9378 | const struct nlattr *attr, u8 *dreg, | |
9379 | const struct nft_data *data, | |
9380 | enum nft_data_types type, unsigned int len) | |
9381 | { | |
9382 | int err; | |
9383 | u32 reg; | |
9384 | ||
9385 | reg = nft_parse_register(attr); | |
9386 | err = nft_validate_register_store(ctx, reg, data, type, len); | |
9387 | if (err < 0) | |
9388 | return err; | |
9389 | ||
9390 | *dreg = reg; | |
9391 | return 0; | |
9392 | } | |
9393 | EXPORT_SYMBOL_GPL(nft_parse_register_store); | |
96518518 PM |
9394 | |
9395 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
9396 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
9397 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
9398 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
51d70f18 | 9399 | [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
9400 | }; |
9401 | ||
9402 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
9403 | struct nft_data_desc *desc, const struct nlattr *nla) | |
9404 | { | |
664b0f8c | 9405 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
9406 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
9407 | struct nft_chain *chain; | |
9408 | int err; | |
9409 | ||
8cb08174 JB |
9410 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
9411 | nft_verdict_policy, NULL); | |
96518518 PM |
9412 | if (err < 0) |
9413 | return err; | |
9414 | ||
9415 | if (!tb[NFTA_VERDICT_CODE]) | |
9416 | return -EINVAL; | |
1ca2e170 | 9417 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 9418 | |
1ca2e170 | 9419 | switch (data->verdict.code) { |
e0abdadc | 9420 | default: |
1ca2e170 | 9421 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
9422 | case NF_ACCEPT: |
9423 | case NF_DROP: | |
9424 | case NF_QUEUE: | |
9425 | break; | |
9426 | default: | |
9427 | return -EINVAL; | |
9428 | } | |
954d8297 | 9429 | fallthrough; |
96518518 PM |
9430 | case NFT_CONTINUE: |
9431 | case NFT_BREAK: | |
9432 | case NFT_RETURN: | |
96518518 PM |
9433 | break; |
9434 | case NFT_JUMP: | |
9435 | case NFT_GOTO: | |
51d70f18 PNA |
9436 | if (tb[NFTA_VERDICT_CHAIN]) { |
9437 | chain = nft_chain_lookup(ctx->net, ctx->table, | |
9438 | tb[NFTA_VERDICT_CHAIN], | |
9439 | genmask); | |
9440 | } else if (tb[NFTA_VERDICT_CHAIN_ID]) { | |
9441 | chain = nft_chain_lookup_byid(ctx->net, | |
9442 | tb[NFTA_VERDICT_CHAIN_ID]); | |
9443 | if (IS_ERR(chain)) | |
9444 | return PTR_ERR(chain); | |
9445 | } else { | |
96518518 | 9446 | return -EINVAL; |
51d70f18 PNA |
9447 | } |
9448 | ||
96518518 PM |
9449 | if (IS_ERR(chain)) |
9450 | return PTR_ERR(chain); | |
f323d954 | 9451 | if (nft_is_base_chain(chain)) |
96518518 PM |
9452 | return -EOPNOTSUPP; |
9453 | ||
96518518 | 9454 | chain->use++; |
1ca2e170 | 9455 | data->verdict.chain = chain; |
96518518 | 9456 | break; |
96518518 PM |
9457 | } |
9458 | ||
4c4ed074 | 9459 | desc->len = sizeof(data->verdict); |
96518518 PM |
9460 | desc->type = NFT_DATA_VERDICT; |
9461 | return 0; | |
9462 | } | |
9463 | ||
9464 | static void nft_verdict_uninit(const struct nft_data *data) | |
9465 | { | |
d0e2c7de PNA |
9466 | struct nft_chain *chain; |
9467 | struct nft_rule *rule; | |
9468 | ||
1ca2e170 | 9469 | switch (data->verdict.code) { |
96518518 PM |
9470 | case NFT_JUMP: |
9471 | case NFT_GOTO: | |
d0e2c7de PNA |
9472 | chain = data->verdict.chain; |
9473 | chain->use--; | |
9474 | ||
9475 | if (!nft_chain_is_bound(chain)) | |
9476 | break; | |
9477 | ||
9478 | chain->table->use--; | |
9479 | list_for_each_entry(rule, &chain->rules, list) | |
9480 | chain->use--; | |
9481 | ||
9482 | nft_chain_del(chain); | |
96518518 PM |
9483 | break; |
9484 | } | |
9485 | } | |
9486 | ||
33d5a7b1 | 9487 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
9488 | { |
9489 | struct nlattr *nest; | |
9490 | ||
ae0be8de | 9491 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
9492 | if (!nest) |
9493 | goto nla_put_failure; | |
9494 | ||
33d5a7b1 | 9495 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
9496 | goto nla_put_failure; |
9497 | ||
33d5a7b1 | 9498 | switch (v->code) { |
96518518 PM |
9499 | case NFT_JUMP: |
9500 | case NFT_GOTO: | |
1ca2e170 | 9501 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 9502 | v->chain->name)) |
96518518 PM |
9503 | goto nla_put_failure; |
9504 | } | |
9505 | nla_nest_end(skb, nest); | |
9506 | return 0; | |
9507 | ||
9508 | nla_put_failure: | |
9509 | return -1; | |
9510 | } | |
9511 | ||
d0a11fc3 PM |
9512 | static int nft_value_init(const struct nft_ctx *ctx, |
9513 | struct nft_data *data, unsigned int size, | |
96518518 PM |
9514 | struct nft_data_desc *desc, const struct nlattr *nla) |
9515 | { | |
9516 | unsigned int len; | |
9517 | ||
9518 | len = nla_len(nla); | |
9519 | if (len == 0) | |
9520 | return -EINVAL; | |
d0a11fc3 | 9521 | if (len > size) |
96518518 PM |
9522 | return -EOVERFLOW; |
9523 | ||
d0a11fc3 | 9524 | nla_memcpy(data->data, nla, len); |
96518518 PM |
9525 | desc->type = NFT_DATA_VALUE; |
9526 | desc->len = len; | |
9527 | return 0; | |
9528 | } | |
9529 | ||
9530 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
9531 | unsigned int len) | |
9532 | { | |
9533 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
9534 | } | |
9535 | ||
9536 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 9537 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
9538 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
9539 | }; | |
9540 | ||
9541 | /** | |
9542 | * nft_data_init - parse nf_tables data netlink attributes | |
9543 | * | |
9544 | * @ctx: context of the expression using the data | |
9545 | * @data: destination struct nft_data | |
d0a11fc3 | 9546 | * @size: maximum data length |
96518518 PM |
9547 | * @desc: data description |
9548 | * @nla: netlink attribute containing data | |
9549 | * | |
9550 | * Parse the netlink data attributes and initialize a struct nft_data. | |
9551 | * The type and length of data are returned in the data description. | |
9552 | * | |
9553 | * The caller can indicate that it only wants to accept data of type | |
9554 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
9555 | */ | |
d0a11fc3 PM |
9556 | int nft_data_init(const struct nft_ctx *ctx, |
9557 | struct nft_data *data, unsigned int size, | |
96518518 PM |
9558 | struct nft_data_desc *desc, const struct nlattr *nla) |
9559 | { | |
9560 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
9561 | int err; | |
9562 | ||
8cb08174 JB |
9563 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
9564 | nft_data_policy, NULL); | |
96518518 PM |
9565 | if (err < 0) |
9566 | return err; | |
9567 | ||
9568 | if (tb[NFTA_DATA_VALUE]) | |
d0a11fc3 PM |
9569 | return nft_value_init(ctx, data, size, desc, |
9570 | tb[NFTA_DATA_VALUE]); | |
96518518 PM |
9571 | if (tb[NFTA_DATA_VERDICT] && ctx != NULL) |
9572 | return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
9573 | return -EINVAL; | |
9574 | } | |
9575 | EXPORT_SYMBOL_GPL(nft_data_init); | |
9576 | ||
9577 | /** | |
59105446 | 9578 | * nft_data_release - release a nft_data item |
96518518 PM |
9579 | * |
9580 | * @data: struct nft_data to release | |
9581 | * @type: type of data | |
9582 | * | |
9583 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
9584 | * all others need to be released by calling this function. | |
9585 | */ | |
59105446 | 9586 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 9587 | { |
960bd2c2 | 9588 | if (type < NFT_DATA_VERDICT) |
96518518 | 9589 | return; |
960bd2c2 | 9590 | switch (type) { |
96518518 PM |
9591 | case NFT_DATA_VERDICT: |
9592 | return nft_verdict_uninit(data); | |
9593 | default: | |
9594 | WARN_ON(1); | |
9595 | } | |
9596 | } | |
59105446 | 9597 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
9598 | |
9599 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
9600 | enum nft_data_types type, unsigned int len) | |
9601 | { | |
9602 | struct nlattr *nest; | |
9603 | int err; | |
9604 | ||
ae0be8de | 9605 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
9606 | if (nest == NULL) |
9607 | return -1; | |
9608 | ||
9609 | switch (type) { | |
9610 | case NFT_DATA_VALUE: | |
9611 | err = nft_value_dump(skb, data, len); | |
9612 | break; | |
9613 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 9614 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
9615 | break; |
9616 | default: | |
9617 | err = -EINVAL; | |
9618 | WARN_ON(1); | |
9619 | } | |
9620 | ||
9621 | nla_nest_end(skb, nest); | |
9622 | return err; | |
9623 | } | |
9624 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
9625 | ||
5ebe0b0e PNA |
9626 | int __nft_release_basechain(struct nft_ctx *ctx) |
9627 | { | |
9628 | struct nft_rule *rule, *nr; | |
9629 | ||
fa5950e4 FW |
9630 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
9631 | return 0; | |
5ebe0b0e | 9632 | |
c974a3a3 | 9633 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
9634 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
9635 | list_del(&rule->list); | |
9636 | ctx->chain->use--; | |
bb7b40ae | 9637 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 9638 | } |
1b2470e5 | 9639 | nft_chain_del(ctx->chain); |
5ebe0b0e | 9640 | ctx->table->use--; |
43a605f2 | 9641 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
9642 | |
9643 | return 0; | |
9644 | } | |
9645 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
9646 | ||
00dfe9be PNA |
9647 | static void __nft_release_hook(struct net *net, struct nft_table *table) |
9648 | { | |
6069da44 | 9649 | struct nft_flowtable *flowtable; |
00dfe9be PNA |
9650 | struct nft_chain *chain; |
9651 | ||
9652 | list_for_each_entry(chain, &table->chains, list) | |
9653 | nf_tables_unregister_hook(net, table, chain); | |
6069da44 PNA |
9654 | list_for_each_entry(flowtable, &table->flowtables, list) |
9655 | nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list); | |
00dfe9be PNA |
9656 | } |
9657 | ||
767d1216 PNA |
9658 | static void __nft_release_hooks(struct net *net) |
9659 | { | |
d59d2f82 | 9660 | struct nftables_pernet *nft_net = nft_pernet(net); |
767d1216 | 9661 | struct nft_table *table; |
767d1216 | 9662 | |
0854db2a | 9663 | list_for_each_entry(table, &nft_net->tables, list) { |
2888b080 PNA |
9664 | if (nft_table_has_owner(table)) |
9665 | continue; | |
9666 | ||
00dfe9be | 9667 | __nft_release_hook(net, table); |
2888b080 | 9668 | } |
767d1216 PNA |
9669 | } |
9670 | ||
fd020332 | 9671 | static void __nft_release_table(struct net *net, struct nft_table *table) |
df05ef87 | 9672 | { |
3b49e2e9 | 9673 | struct nft_flowtable *flowtable, *nf; |
df05ef87 | 9674 | struct nft_chain *chain, *nc; |
e5009240 | 9675 | struct nft_object *obj, *ne; |
df05ef87 PNA |
9676 | struct nft_rule *rule, *nr; |
9677 | struct nft_set *set, *ns; | |
9678 | struct nft_ctx ctx = { | |
9679 | .net = net, | |
43a605f2 | 9680 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
9681 | }; |
9682 | ||
fd020332 PNA |
9683 | ctx.family = table->family; |
9684 | ctx.table = table; | |
9685 | list_for_each_entry(chain, &table->chains, list) { | |
9686 | ctx.chain = chain; | |
9687 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
9688 | list_del(&rule->list); | |
9689 | chain->use--; | |
9690 | nf_tables_rule_release(&ctx, rule); | |
df05ef87 | 9691 | } |
df05ef87 | 9692 | } |
fd020332 PNA |
9693 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
9694 | list_del(&flowtable->list); | |
9695 | table->use--; | |
9696 | nf_tables_flowtable_destroy(flowtable); | |
9697 | } | |
9698 | list_for_each_entry_safe(set, ns, &table->sets, list) { | |
9699 | list_del(&set->list); | |
9700 | table->use--; | |
9701 | nft_set_destroy(&ctx, set); | |
9702 | } | |
9703 | list_for_each_entry_safe(obj, ne, &table->objects, list) { | |
9704 | nft_obj_del(obj); | |
9705 | table->use--; | |
9706 | nft_obj_destroy(&ctx, obj); | |
9707 | } | |
9708 | list_for_each_entry_safe(chain, nc, &table->chains, list) { | |
9709 | ctx.chain = chain; | |
9710 | nft_chain_del(chain); | |
9711 | table->use--; | |
9712 | nf_tables_chain_destroy(&ctx); | |
9713 | } | |
fd020332 PNA |
9714 | nf_tables_table_destroy(&ctx); |
9715 | } | |
9716 | ||
bd1777b3 | 9717 | static void __nft_release_tables(struct net *net) |
fd020332 | 9718 | { |
d59d2f82 | 9719 | struct nftables_pernet *nft_net = nft_pernet(net); |
fd020332 PNA |
9720 | struct nft_table *table, *nt; |
9721 | ||
0854db2a | 9722 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
bd1777b3 | 9723 | if (nft_table_has_owner(table)) |
6001a930 PNA |
9724 | continue; |
9725 | ||
a499b03b FW |
9726 | list_del(&table->list); |
9727 | ||
fd020332 | 9728 | __nft_release_table(net, table); |
6001a930 PNA |
9729 | } |
9730 | } | |
9731 | ||
9732 | static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event, | |
9733 | void *ptr) | |
9734 | { | |
a499b03b | 9735 | struct nft_table *table, *to_delete[8]; |
0854db2a | 9736 | struct nftables_pernet *nft_net; |
6001a930 | 9737 | struct netlink_notify *n = ptr; |
6001a930 | 9738 | struct net *net = n->net; |
a499b03b FW |
9739 | unsigned int deleted; |
9740 | bool restart = false; | |
6001a930 PNA |
9741 | |
9742 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER) | |
9743 | return NOTIFY_DONE; | |
9744 | ||
d59d2f82 | 9745 | nft_net = nft_pernet(net); |
a499b03b | 9746 | deleted = 0; |
0854db2a | 9747 | mutex_lock(&nft_net->commit_mutex); |
a499b03b | 9748 | again: |
0854db2a | 9749 | list_for_each_entry(table, &nft_net->tables, list) { |
6001a930 PNA |
9750 | if (nft_table_has_owner(table) && |
9751 | n->portid == table->nlpid) { | |
9752 | __nft_release_hook(net, table); | |
a499b03b FW |
9753 | list_del_rcu(&table->list); |
9754 | to_delete[deleted++] = table; | |
9755 | if (deleted >= ARRAY_SIZE(to_delete)) | |
9756 | break; | |
6001a930 PNA |
9757 | } |
9758 | } | |
a499b03b FW |
9759 | if (deleted) { |
9760 | restart = deleted >= ARRAY_SIZE(to_delete); | |
6001a930 | 9761 | synchronize_rcu(); |
a499b03b FW |
9762 | while (deleted) |
9763 | __nft_release_table(net, to_delete[--deleted]); | |
9764 | ||
9765 | if (restart) | |
9766 | goto again; | |
6001a930 | 9767 | } |
0854db2a | 9768 | mutex_unlock(&nft_net->commit_mutex); |
6001a930 PNA |
9769 | |
9770 | return NOTIFY_DONE; | |
df05ef87 PNA |
9771 | } |
9772 | ||
6001a930 PNA |
9773 | static struct notifier_block nft_nl_notifier = { |
9774 | .notifier_call = nft_rcv_nl_event, | |
9775 | }; | |
9776 | ||
dd4cbef7 PNA |
9777 | static int __net_init nf_tables_init_net(struct net *net) |
9778 | { | |
d59d2f82 | 9779 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
9780 | |
9781 | INIT_LIST_HEAD(&nft_net->tables); | |
9782 | INIT_LIST_HEAD(&nft_net->commit_list); | |
9783 | INIT_LIST_HEAD(&nft_net->module_list); | |
9784 | INIT_LIST_HEAD(&nft_net->notify_list); | |
9785 | mutex_init(&nft_net->commit_mutex); | |
9786 | nft_net->base_seq = 1; | |
9787 | nft_net->validate_state = NFT_VALIDATE_SKIP; | |
a654de8f | 9788 | |
dd4cbef7 PNA |
9789 | return 0; |
9790 | } | |
9791 | ||
767d1216 PNA |
9792 | static void __net_exit nf_tables_pre_exit_net(struct net *net) |
9793 | { | |
9794 | __nft_release_hooks(net); | |
9795 | } | |
9796 | ||
dd4cbef7 PNA |
9797 | static void __net_exit nf_tables_exit_net(struct net *net) |
9798 | { | |
d59d2f82 | 9799 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
9800 | |
9801 | mutex_lock(&nft_net->commit_mutex); | |
9802 | if (!list_empty(&nft_net->commit_list)) | |
c0391b6a | 9803 | __nf_tables_abort(net, NFNL_ABORT_NONE); |
bd1777b3 | 9804 | __nft_release_tables(net); |
0854db2a FW |
9805 | mutex_unlock(&nft_net->commit_mutex); |
9806 | WARN_ON_ONCE(!list_empty(&nft_net->tables)); | |
9807 | WARN_ON_ONCE(!list_empty(&nft_net->module_list)); | |
9808 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); | |
dd4cbef7 PNA |
9809 | } |
9810 | ||
99633ab2 | 9811 | static struct pernet_operations nf_tables_net_ops = { |
767d1216 PNA |
9812 | .init = nf_tables_init_net, |
9813 | .pre_exit = nf_tables_pre_exit_net, | |
9814 | .exit = nf_tables_exit_net, | |
0854db2a FW |
9815 | .id = &nf_tables_net_id, |
9816 | .size = sizeof(struct nftables_pernet), | |
99633ab2 PNA |
9817 | }; |
9818 | ||
96518518 PM |
9819 | static int __init nf_tables_module_init(void) |
9820 | { | |
9821 | int err; | |
9822 | ||
d209df3e FW |
9823 | err = register_pernet_subsys(&nf_tables_net_ops); |
9824 | if (err < 0) | |
9825 | return err; | |
9826 | ||
9827 | err = nft_chain_filter_init(); | |
9828 | if (err < 0) | |
6001a930 | 9829 | goto err_chain_filter; |
02c7b25e | 9830 | |
96518518 PM |
9831 | err = nf_tables_core_module_init(); |
9832 | if (err < 0) | |
6001a930 | 9833 | goto err_core_module; |
96518518 | 9834 | |
d209df3e | 9835 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 9836 | if (err < 0) |
6001a930 | 9837 | goto err_netdev_notifier; |
96518518 | 9838 | |
4d44175a FW |
9839 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
9840 | if (err < 0) | |
6001a930 | 9841 | goto err_rht_objname; |
4d44175a | 9842 | |
06d392cb | 9843 | err = nft_offload_init(); |
9844 | if (err < 0) | |
6001a930 PNA |
9845 | goto err_offload; |
9846 | ||
9847 | err = netlink_register_notifier(&nft_nl_notifier); | |
9848 | if (err < 0) | |
9849 | goto err_netlink_notifier; | |
06d392cb | 9850 | |
d209df3e FW |
9851 | /* must be last */ |
9852 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
9853 | if (err < 0) | |
6001a930 | 9854 | goto err_nfnl_subsys; |
3b49e2e9 | 9855 | |
c1deb065 | 9856 | nft_chain_route_init(); |
3474a2c6 | 9857 | |
d209df3e | 9858 | return err; |
6001a930 PNA |
9859 | |
9860 | err_nfnl_subsys: | |
9861 | netlink_unregister_notifier(&nft_nl_notifier); | |
9862 | err_netlink_notifier: | |
06d392cb | 9863 | nft_offload_exit(); |
6001a930 | 9864 | err_offload: |
4d44175a | 9865 | rhltable_destroy(&nft_objname_ht); |
6001a930 | 9866 | err_rht_objname: |
d209df3e | 9867 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
6001a930 | 9868 | err_netdev_notifier: |
96518518 | 9869 | nf_tables_core_module_exit(); |
6001a930 | 9870 | err_core_module: |
d209df3e | 9871 | nft_chain_filter_fini(); |
6001a930 | 9872 | err_chain_filter: |
d209df3e | 9873 | unregister_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
9874 | return err; |
9875 | } | |
9876 | ||
9877 | static void __exit nf_tables_module_exit(void) | |
9878 | { | |
9879 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
6001a930 | 9880 | netlink_unregister_notifier(&nft_nl_notifier); |
06d392cb | 9881 | nft_offload_exit(); |
3b49e2e9 | 9882 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 9883 | nft_chain_filter_fini(); |
c1deb065 | 9884 | nft_chain_route_fini(); |
71ad00c5 | 9885 | unregister_pernet_subsys(&nf_tables_net_ops); |
0935d558 | 9886 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 9887 | rcu_barrier(); |
4d44175a | 9888 | rhltable_destroy(&nft_objname_ht); |
96518518 | 9889 | nf_tables_core_module_exit(); |
96518518 PM |
9890 | } |
9891 | ||
9892 | module_init(nf_tables_module_init); | |
9893 | module_exit(nf_tables_module_exit); | |
9894 | ||
9895 | MODULE_LICENSE("GPL"); | |
9896 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
9897 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |