]>
Commit | Line | Data |
---|---|---|
96518518 | 1 | /* |
20a69341 | 2 | * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
96518518 PM |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/list.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/netlink.h> | |
1ff75a3e | 16 | #include <linux/vmalloc.h> |
96518518 PM |
17 | #include <linux/netfilter.h> |
18 | #include <linux/netfilter/nfnetlink.h> | |
19 | #include <linux/netfilter/nf_tables.h> | |
3b49e2e9 | 20 | #include <net/netfilter/nf_flow_table.h> |
96518518 PM |
21 | #include <net/netfilter/nf_tables_core.h> |
22 | #include <net/netfilter/nf_tables.h> | |
99633ab2 | 23 | #include <net/net_namespace.h> |
96518518 PM |
24 | #include <net/sock.h> |
25 | ||
96518518 | 26 | static LIST_HEAD(nf_tables_expressions); |
e5009240 | 27 | static LIST_HEAD(nf_tables_objects); |
3b49e2e9 | 28 | static LIST_HEAD(nf_tables_flowtables); |
3ecbfd65 | 29 | static u64 table_handle; |
96518518 | 30 | |
a654de8f PNA |
31 | enum { |
32 | NFT_VALIDATE_SKIP = 0, | |
33 | NFT_VALIDATE_NEED, | |
34 | NFT_VALIDATE_DO, | |
35 | }; | |
36 | ||
1b2470e5 FW |
37 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed); |
38 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed); | |
39 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
40 | ||
41 | static const struct rhashtable_params nft_chain_ht_params = { | |
42 | .head_offset = offsetof(struct nft_chain, rhlhead), | |
43 | .key_offset = offsetof(struct nft_chain, name), | |
44 | .hashfn = nft_chain_hash, | |
45 | .obj_hashfn = nft_chain_hash_obj, | |
46 | .obj_cmpfn = nft_chain_hash_cmp, | |
47 | .locks_mul = 1, | |
48 | .automatic_shrinking = true, | |
49 | }; | |
50 | ||
a654de8f PNA |
51 | static void nft_validate_state_update(struct net *net, u8 new_validate_state) |
52 | { | |
53 | switch (net->nft.validate_state) { | |
54 | case NFT_VALIDATE_SKIP: | |
55 | WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO); | |
56 | break; | |
57 | case NFT_VALIDATE_NEED: | |
58 | break; | |
59 | case NFT_VALIDATE_DO: | |
60 | if (new_validate_state == NFT_VALIDATE_NEED) | |
61 | return; | |
62 | } | |
63 | ||
64 | net->nft.validate_state = new_validate_state; | |
65 | } | |
66 | ||
7c95f6d8 | 67 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 68 | struct net *net, |
7c95f6d8 PNA |
69 | const struct sk_buff *skb, |
70 | const struct nlmsghdr *nlh, | |
36596dad | 71 | u8 family, |
7c95f6d8 PNA |
72 | struct nft_table *table, |
73 | struct nft_chain *chain, | |
74 | const struct nlattr * const *nla) | |
75 | { | |
633c9a84 | 76 | ctx->net = net; |
36596dad | 77 | ctx->family = family; |
26b2f552 | 78 | ctx->level = 0; |
128ad332 PNA |
79 | ctx->table = table; |
80 | ctx->chain = chain; | |
81 | ctx->nla = nla; | |
82 | ctx->portid = NETLINK_CB(skb).portid; | |
83 | ctx->report = nlmsg_report(nlh); | |
84 | ctx->seq = nlh->nlmsg_seq; | |
7c95f6d8 PNA |
85 | } |
86 | ||
8411b644 PNA |
87 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
88 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
89 | { |
90 | struct nft_trans *trans; | |
91 | ||
8411b644 | 92 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
93 | if (trans == NULL) |
94 | return NULL; | |
95 | ||
b380e5c7 | 96 | trans->msg_type = msg_type; |
1081d11b PNA |
97 | trans->ctx = *ctx; |
98 | ||
99 | return trans; | |
100 | } | |
101 | ||
8411b644 PNA |
102 | static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
103 | int msg_type, u32 size) | |
104 | { | |
105 | return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); | |
106 | } | |
107 | ||
1081d11b PNA |
108 | static void nft_trans_destroy(struct nft_trans *trans) |
109 | { | |
110 | list_del(&trans->list); | |
111 | kfree(trans); | |
112 | } | |
113 | ||
c974a3a3 PNA |
114 | static int nf_tables_register_hook(struct net *net, |
115 | const struct nft_table *table, | |
116 | struct nft_chain *chain) | |
d8ee8f7c | 117 | { |
4e25ceb8 | 118 | const struct nft_base_chain *basechain; |
a37061a6 | 119 | const struct nf_hook_ops *ops; |
ae6153b5 | 120 | |
d8ee8f7c | 121 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 122 | !nft_is_base_chain(chain)) |
d8ee8f7c PNA |
123 | return 0; |
124 | ||
4e25ceb8 FW |
125 | basechain = nft_base_chain(chain); |
126 | ops = &basechain->ops; | |
ae6153b5 | 127 | |
4e25ceb8 FW |
128 | if (basechain->type->ops_register) |
129 | return basechain->type->ops_register(net, ops); | |
130 | ||
a37061a6 | 131 | return nf_register_net_hook(net, ops); |
d8ee8f7c PNA |
132 | } |
133 | ||
c974a3a3 PNA |
134 | static void nf_tables_unregister_hook(struct net *net, |
135 | const struct nft_table *table, | |
136 | struct nft_chain *chain) | |
c5598794 | 137 | { |
4e25ceb8 FW |
138 | const struct nft_base_chain *basechain; |
139 | const struct nf_hook_ops *ops; | |
140 | ||
d8ee8f7c | 141 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 142 | !nft_is_base_chain(chain)) |
d8ee8f7c | 143 | return; |
4e25ceb8 FW |
144 | basechain = nft_base_chain(chain); |
145 | ops = &basechain->ops; | |
d8ee8f7c | 146 | |
4e25ceb8 FW |
147 | if (basechain->type->ops_unregister) |
148 | return basechain->type->ops_unregister(net, ops); | |
149 | ||
150 | nf_unregister_net_hook(net, ops); | |
c5598794 AB |
151 | } |
152 | ||
ee01d542 AB |
153 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
154 | { | |
155 | struct nft_trans *trans; | |
156 | ||
157 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
158 | if (trans == NULL) | |
159 | return -ENOMEM; | |
160 | ||
161 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 162 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 AB |
163 | |
164 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
165 | return 0; | |
166 | } | |
167 | ||
168 | static int nft_deltable(struct nft_ctx *ctx) | |
169 | { | |
170 | int err; | |
171 | ||
172 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
173 | if (err < 0) | |
174 | return err; | |
175 | ||
f2a6d766 | 176 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
177 | return err; |
178 | } | |
179 | ||
180 | static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) | |
181 | { | |
182 | struct nft_trans *trans; | |
183 | ||
184 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
185 | if (trans == NULL) | |
186 | return -ENOMEM; | |
187 | ||
188 | if (msg_type == NFT_MSG_NEWCHAIN) | |
664b0f8c | 189 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 AB |
190 | |
191 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
192 | return 0; | |
193 | } | |
194 | ||
195 | static int nft_delchain(struct nft_ctx *ctx) | |
196 | { | |
197 | int err; | |
198 | ||
199 | err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); | |
200 | if (err < 0) | |
201 | return err; | |
202 | ||
203 | ctx->table->use--; | |
664b0f8c | 204 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 AB |
205 | |
206 | return err; | |
207 | } | |
208 | ||
bb7b40ae PNA |
209 | static void nft_rule_expr_activate(const struct nft_ctx *ctx, |
210 | struct nft_rule *rule) | |
211 | { | |
212 | struct nft_expr *expr; | |
213 | ||
214 | expr = nft_expr_first(rule); | |
215 | while (expr != nft_expr_last(rule) && expr->ops) { | |
216 | if (expr->ops->activate) | |
217 | expr->ops->activate(ctx, expr); | |
218 | ||
219 | expr = nft_expr_next(expr); | |
220 | } | |
221 | } | |
222 | ||
223 | static void nft_rule_expr_deactivate(const struct nft_ctx *ctx, | |
224 | struct nft_rule *rule) | |
225 | { | |
226 | struct nft_expr *expr; | |
227 | ||
228 | expr = nft_expr_first(rule); | |
229 | while (expr != nft_expr_last(rule) && expr->ops) { | |
230 | if (expr->ops->deactivate) | |
231 | expr->ops->deactivate(ctx, expr); | |
232 | ||
233 | expr = nft_expr_next(expr); | |
234 | } | |
235 | } | |
236 | ||
ee01d542 AB |
237 | static int |
238 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
239 | { | |
240 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
241 | if (nft_is_active_next(ctx->net, rule)) { |
242 | nft_deactivate_next(ctx->net, rule); | |
ee01d542 AB |
243 | ctx->chain->use--; |
244 | return 0; | |
245 | } | |
246 | return -ENOENT; | |
247 | } | |
248 | ||
249 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
250 | struct nft_rule *rule) | |
251 | { | |
252 | struct nft_trans *trans; | |
253 | ||
254 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
255 | if (trans == NULL) | |
256 | return NULL; | |
257 | ||
1a94e38d PNA |
258 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
259 | nft_trans_rule_id(trans) = | |
260 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
261 | } | |
ee01d542 AB |
262 | nft_trans_rule(trans) = rule; |
263 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
264 | ||
265 | return trans; | |
266 | } | |
267 | ||
268 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
269 | { | |
270 | struct nft_trans *trans; | |
271 | int err; | |
272 | ||
273 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
274 | if (trans == NULL) | |
275 | return -ENOMEM; | |
276 | ||
277 | err = nf_tables_delrule_deactivate(ctx, rule); | |
278 | if (err < 0) { | |
279 | nft_trans_destroy(trans); | |
280 | return err; | |
281 | } | |
bb7b40ae | 282 | nft_rule_expr_deactivate(ctx, rule); |
ee01d542 AB |
283 | |
284 | return 0; | |
285 | } | |
286 | ||
287 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
288 | { | |
289 | struct nft_rule *rule; | |
290 | int err; | |
291 | ||
292 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
293 | err = nft_delrule(ctx, rule); | |
294 | if (err < 0) | |
295 | return err; | |
296 | } | |
297 | return 0; | |
298 | } | |
299 | ||
ee01d542 AB |
300 | static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type, |
301 | struct nft_set *set) | |
302 | { | |
303 | struct nft_trans *trans; | |
304 | ||
305 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
306 | if (trans == NULL) | |
307 | return -ENOMEM; | |
308 | ||
309 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { | |
310 | nft_trans_set_id(trans) = | |
311 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 312 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
313 | } |
314 | nft_trans_set(trans) = set; | |
315 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
316 | ||
317 | return 0; | |
318 | } | |
319 | ||
320 | static int nft_delset(struct nft_ctx *ctx, struct nft_set *set) | |
321 | { | |
322 | int err; | |
323 | ||
324 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
325 | if (err < 0) | |
326 | return err; | |
327 | ||
37a9cc52 | 328 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
329 | ctx->table->use--; |
330 | ||
331 | return err; | |
332 | } | |
333 | ||
e5009240 PNA |
334 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
335 | struct nft_object *obj) | |
336 | { | |
337 | struct nft_trans *trans; | |
338 | ||
339 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
340 | if (trans == NULL) | |
341 | return -ENOMEM; | |
342 | ||
343 | if (msg_type == NFT_MSG_NEWOBJ) | |
344 | nft_activate_next(ctx->net, obj); | |
345 | ||
346 | nft_trans_obj(trans) = obj; | |
347 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
353 | { | |
354 | int err; | |
355 | ||
356 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
357 | if (err < 0) | |
358 | return err; | |
359 | ||
360 | nft_deactivate_next(ctx->net, obj); | |
361 | ctx->table->use--; | |
362 | ||
363 | return err; | |
364 | } | |
365 | ||
3b49e2e9 PNA |
366 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
367 | struct nft_flowtable *flowtable) | |
368 | { | |
369 | struct nft_trans *trans; | |
370 | ||
371 | trans = nft_trans_alloc(ctx, msg_type, | |
372 | sizeof(struct nft_trans_flowtable)); | |
373 | if (trans == NULL) | |
374 | return -ENOMEM; | |
375 | ||
376 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
377 | nft_activate_next(ctx->net, flowtable); | |
378 | ||
379 | nft_trans_flowtable(trans) = flowtable; | |
380 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
381 | ||
382 | return 0; | |
383 | } | |
384 | ||
385 | static int nft_delflowtable(struct nft_ctx *ctx, | |
386 | struct nft_flowtable *flowtable) | |
387 | { | |
388 | int err; | |
389 | ||
390 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
391 | if (err < 0) | |
392 | return err; | |
393 | ||
394 | nft_deactivate_next(ctx->net, flowtable); | |
395 | ctx->table->use--; | |
396 | ||
397 | return err; | |
398 | } | |
399 | ||
96518518 PM |
400 | /* |
401 | * Tables | |
402 | */ | |
403 | ||
36596dad | 404 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 405 | const struct nlattr *nla, |
36596dad | 406 | u8 family, u8 genmask) |
96518518 PM |
407 | { |
408 | struct nft_table *table; | |
409 | ||
cac20fcd PNA |
410 | if (nla == NULL) |
411 | return ERR_PTR(-EINVAL); | |
412 | ||
d9adf22a | 413 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
f2a6d766 | 414 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 415 | table->family == family && |
f2a6d766 | 416 | nft_active_genmask(table, genmask)) |
96518518 PM |
417 | return table; |
418 | } | |
cac20fcd PNA |
419 | |
420 | return ERR_PTR(-ENOENT); | |
96518518 PM |
421 | } |
422 | ||
3ecbfd65 HS |
423 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
424 | const struct nlattr *nla, | |
425 | u8 genmask) | |
426 | { | |
427 | struct nft_table *table; | |
428 | ||
429 | list_for_each_entry(table, &net->nft.tables, list) { | |
430 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && | |
431 | nft_active_genmask(table, genmask)) | |
432 | return table; | |
433 | } | |
3ecbfd65 HS |
434 | |
435 | return ERR_PTR(-ENOENT); | |
436 | } | |
437 | ||
96518518 PM |
438 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
439 | { | |
440 | return ++table->hgenerator; | |
441 | } | |
442 | ||
32537e91 | 443 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 444 | |
32537e91 | 445 | static const struct nft_chain_type * |
1ea26cca | 446 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c PNA |
447 | { |
448 | int i; | |
449 | ||
baae3e62 | 450 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
9370761c PNA |
451 | if (chain_type[family][i] != NULL && |
452 | !nla_strcmp(nla, chain_type[family][i]->name)) | |
baae3e62 | 453 | return chain_type[family][i]; |
9370761c | 454 | } |
baae3e62 | 455 | return NULL; |
9370761c PNA |
456 | } |
457 | ||
32537e91 | 458 | static const struct nft_chain_type * |
1ea26cca | 459 | nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family, bool autoload) |
9370761c | 460 | { |
32537e91 | 461 | const struct nft_chain_type *type; |
9370761c | 462 | |
1ea26cca | 463 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
464 | if (type != NULL) |
465 | return type; | |
9370761c | 466 | #ifdef CONFIG_MODULES |
93b0806f | 467 | if (autoload) { |
9370761c | 468 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1ea26cca | 469 | request_module("nft-chain-%u-%.*s", family, |
2fec6bb6 | 470 | nla_len(nla), (const char *)nla_data(nla)); |
9370761c | 471 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
1ea26cca | 472 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
473 | if (type != NULL) |
474 | return ERR_PTR(-EAGAIN); | |
9370761c PNA |
475 | } |
476 | #endif | |
93b0806f | 477 | return ERR_PTR(-ENOENT); |
9370761c PNA |
478 | } |
479 | ||
96518518 | 480 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
481 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
482 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 483 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 484 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
96518518 PM |
485 | }; |
486 | ||
84d7fce6 PNA |
487 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
488 | u32 portid, u32 seq, int event, u32 flags, | |
489 | int family, const struct nft_table *table) | |
96518518 PM |
490 | { |
491 | struct nlmsghdr *nlh; | |
492 | struct nfgenmsg *nfmsg; | |
493 | ||
dedb67c4 | 494 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 PM |
495 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
496 | if (nlh == NULL) | |
497 | goto nla_put_failure; | |
498 | ||
499 | nfmsg = nlmsg_data(nlh); | |
500 | nfmsg->nfgen_family = family; | |
501 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 502 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 | 503 | |
9ddf6323 | 504 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
d8bcc768 | 505 | nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) || |
3ecbfd65 HS |
506 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
507 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
508 | NFTA_TABLE_PAD)) | |
96518518 PM |
509 | goto nla_put_failure; |
510 | ||
053c095a JB |
511 | nlmsg_end(skb, nlh); |
512 | return 0; | |
96518518 PM |
513 | |
514 | nla_put_failure: | |
515 | nlmsg_trim(skb, nlh); | |
516 | return -1; | |
517 | } | |
518 | ||
25e94a99 | 519 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 PM |
520 | { |
521 | struct sk_buff *skb; | |
96518518 PM |
522 | int err; |
523 | ||
128ad332 PNA |
524 | if (!ctx->report && |
525 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 526 | return; |
96518518 | 527 | |
96518518 PM |
528 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
529 | if (skb == NULL) | |
530 | goto err; | |
531 | ||
84d7fce6 | 532 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 533 | event, 0, ctx->family, ctx->table); |
96518518 PM |
534 | if (err < 0) { |
535 | kfree_skb(skb); | |
536 | goto err; | |
537 | } | |
538 | ||
25e94a99 PNA |
539 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
540 | ctx->report, GFP_KERNEL); | |
541 | return; | |
96518518 | 542 | err: |
25e94a99 | 543 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
544 | } |
545 | ||
546 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
547 | struct netlink_callback *cb) | |
548 | { | |
549 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 PM |
550 | const struct nft_table *table; |
551 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 552 | struct net *net = sock_net(skb->sk); |
96518518 PM |
553 | int family = nfmsg->nfgen_family; |
554 | ||
e688a7f8 | 555 | rcu_read_lock(); |
38e029f1 PNA |
556 | cb->seq = net->nft.base_seq; |
557 | ||
36596dad | 558 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 559 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
560 | continue; |
561 | ||
36596dad PNA |
562 | if (idx < s_idx) |
563 | goto cont; | |
564 | if (idx > s_idx) | |
565 | memset(&cb->args[1], 0, | |
566 | sizeof(cb->args) - sizeof(cb->args[0])); | |
567 | if (!nft_is_active(net, table)) | |
568 | continue; | |
569 | if (nf_tables_fill_table_info(skb, net, | |
570 | NETLINK_CB(cb->skb).portid, | |
571 | cb->nlh->nlmsg_seq, | |
572 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 573 | table->family, table) < 0) |
36596dad PNA |
574 | goto done; |
575 | ||
576 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 577 | cont: |
36596dad | 578 | idx++; |
96518518 PM |
579 | } |
580 | done: | |
e688a7f8 | 581 | rcu_read_unlock(); |
96518518 PM |
582 | cb->args[0] = idx; |
583 | return skb->len; | |
584 | } | |
585 | ||
d9adf22a FW |
586 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
587 | const struct nlmsghdr *nlh, | |
588 | struct netlink_dump_control *c) | |
589 | { | |
590 | int err; | |
591 | ||
592 | if (!try_module_get(THIS_MODULE)) | |
593 | return -EINVAL; | |
594 | ||
595 | rcu_read_unlock(); | |
596 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
597 | rcu_read_lock(); | |
598 | module_put(THIS_MODULE); | |
599 | ||
600 | return err; | |
601 | } | |
602 | ||
603 | /* called with rcu_read_lock held */ | |
7b8002a1 PNA |
604 | static int nf_tables_gettable(struct net *net, struct sock *nlsk, |
605 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
606 | const struct nlattr * const nla[], |
607 | struct netlink_ext_ack *extack) | |
96518518 PM |
608 | { |
609 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 610 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
611 | const struct nft_table *table; |
612 | struct sk_buff *skb2; | |
613 | int family = nfmsg->nfgen_family; | |
614 | int err; | |
615 | ||
616 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
617 | struct netlink_dump_control c = { | |
618 | .dump = nf_tables_dump_tables, | |
d9adf22a | 619 | .module = THIS_MODULE, |
96518518 | 620 | }; |
d9adf22a FW |
621 | |
622 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); | |
96518518 PM |
623 | } |
624 | ||
cac20fcd | 625 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask); |
36dd1bcc PNA |
626 | if (IS_ERR(table)) { |
627 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 628 | return PTR_ERR(table); |
36dd1bcc | 629 | } |
96518518 | 630 | |
d9adf22a | 631 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
632 | if (!skb2) |
633 | return -ENOMEM; | |
634 | ||
84d7fce6 | 635 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
636 | nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0, |
637 | family, table); | |
638 | if (err < 0) | |
639 | goto err; | |
640 | ||
641 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
642 | ||
643 | err: | |
644 | kfree_skb(skb2); | |
645 | return err; | |
646 | } | |
647 | ||
c9c17211 | 648 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
649 | { |
650 | struct nft_chain *chain; | |
651 | u32 i = 0; | |
652 | ||
653 | list_for_each_entry(chain, &table->chains, list) { | |
654 | if (!nft_is_active_next(net, chain)) | |
655 | continue; | |
f323d954 | 656 | if (!nft_is_base_chain(chain)) |
10435c11 F |
657 | continue; |
658 | ||
659 | if (cnt && i++ == cnt) | |
660 | break; | |
661 | ||
c974a3a3 | 662 | nf_unregister_net_hook(net, &nft_base_chain(chain)->ops); |
10435c11 F |
663 | } |
664 | } | |
665 | ||
c9c17211 | 666 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
667 | { |
668 | struct nft_chain *chain; | |
669 | int err, i = 0; | |
670 | ||
671 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
672 | if (!nft_is_active_next(net, chain)) |
673 | continue; | |
f323d954 | 674 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
675 | continue; |
676 | ||
c974a3a3 | 677 | err = nf_register_net_hook(net, &nft_base_chain(chain)->ops); |
9ddf6323 PNA |
678 | if (err < 0) |
679 | goto err; | |
680 | ||
681 | i++; | |
682 | } | |
683 | return 0; | |
684 | err: | |
10435c11 | 685 | if (i) |
c9c17211 | 686 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
687 | return err; |
688 | } | |
689 | ||
c9c17211 | 690 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 691 | { |
c9c17211 | 692 | nft_table_disable(net, table, 0); |
9ddf6323 PNA |
693 | } |
694 | ||
e1aaca93 | 695 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 696 | { |
55dd6f93 | 697 | struct nft_trans *trans; |
e1aaca93 | 698 | u32 flags; |
55dd6f93 | 699 | int ret = 0; |
9ddf6323 | 700 | |
e1aaca93 PNA |
701 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
702 | return 0; | |
9ddf6323 | 703 | |
e1aaca93 PNA |
704 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
705 | if (flags & ~NFT_TABLE_F_DORMANT) | |
706 | return -EINVAL; | |
707 | ||
63283dd2 PNA |
708 | if (flags == ctx->table->flags) |
709 | return 0; | |
710 | ||
55dd6f93 PNA |
711 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
712 | sizeof(struct nft_trans_table)); | |
713 | if (trans == NULL) | |
714 | return -ENOMEM; | |
9ddf6323 | 715 | |
e1aaca93 PNA |
716 | if ((flags & NFT_TABLE_F_DORMANT) && |
717 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
55dd6f93 | 718 | nft_trans_table_enable(trans) = false; |
e1aaca93 PNA |
719 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
720 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
c9c17211 | 721 | ret = nf_tables_table_enable(ctx->net, ctx->table); |
55dd6f93 | 722 | if (ret >= 0) { |
e1aaca93 | 723 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
55dd6f93 | 724 | nft_trans_table_enable(trans) = true; |
9ddf6323 | 725 | } |
9ddf6323 | 726 | } |
e1aaca93 PNA |
727 | if (ret < 0) |
728 | goto err; | |
9ddf6323 | 729 | |
55dd6f93 PNA |
730 | nft_trans_table_update(trans) = true; |
731 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
732 | return 0; | |
9ddf6323 | 733 | err: |
55dd6f93 | 734 | nft_trans_destroy(trans); |
9ddf6323 PNA |
735 | return ret; |
736 | } | |
737 | ||
1b2470e5 FW |
738 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
739 | { | |
740 | const char *name = data; | |
741 | ||
742 | return jhash(name, strlen(name), seed); | |
743 | } | |
744 | ||
745 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
746 | { | |
747 | const struct nft_chain *chain = data; | |
748 | ||
749 | return nft_chain_hash(chain->name, 0, seed); | |
750 | } | |
751 | ||
752 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
753 | const void *ptr) | |
754 | { | |
755 | const struct nft_chain *chain = ptr; | |
756 | const char *name = arg->key; | |
757 | ||
758 | return strcmp(chain->name, name); | |
759 | } | |
760 | ||
633c9a84 PNA |
761 | static int nf_tables_newtable(struct net *net, struct sock *nlsk, |
762 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
763 | const struct nlattr * const nla[], |
764 | struct netlink_ext_ack *extack) | |
96518518 PM |
765 | { |
766 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 767 | u8 genmask = nft_genmask_next(net); |
96518518 | 768 | int family = nfmsg->nfgen_family; |
36dd1bcc PNA |
769 | const struct nlattr *attr; |
770 | struct nft_table *table; | |
c5c1f975 | 771 | u32 flags = 0; |
e1aaca93 | 772 | struct nft_ctx ctx; |
55dd6f93 | 773 | int err; |
96518518 | 774 | |
36dd1bcc PNA |
775 | attr = nla[NFTA_TABLE_NAME]; |
776 | table = nft_table_lookup(net, attr, family, genmask); | |
96518518 PM |
777 | if (IS_ERR(table)) { |
778 | if (PTR_ERR(table) != -ENOENT) | |
779 | return PTR_ERR(table); | |
1a28ad74 | 780 | } else { |
36dd1bcc PNA |
781 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
782 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 783 | return -EEXIST; |
36dd1bcc | 784 | } |
96518518 PM |
785 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
786 | return -EOPNOTSUPP; | |
e1aaca93 | 787 | |
98319cb9 | 788 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e1aaca93 | 789 | return nf_tables_updtable(&ctx); |
96518518 PM |
790 | } |
791 | ||
c5c1f975 PM |
792 | if (nla[NFTA_TABLE_FLAGS]) { |
793 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
794 | if (flags & ~NFT_TABLE_F_DORMANT) | |
795 | return -EINVAL; | |
796 | } | |
797 | ||
ffdb210e | 798 | err = -ENOMEM; |
1cae565e | 799 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
ffdb210e | 800 | if (table == NULL) |
98319cb9 | 801 | goto err_kzalloc; |
96518518 | 802 | |
36dd1bcc | 803 | table->name = nla_strdup(attr, GFP_KERNEL); |
e46abbcc | 804 | if (table->name == NULL) |
98319cb9 | 805 | goto err_strdup; |
e46abbcc | 806 | |
1b2470e5 FW |
807 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
808 | if (err) | |
809 | goto err_chain_ht; | |
810 | ||
96518518 | 811 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 812 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 813 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 814 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 815 | table->family = family; |
c5c1f975 | 816 | table->flags = flags; |
3ecbfd65 | 817 | table->handle = ++table_handle; |
9ddf6323 | 818 | |
98319cb9 | 819 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
55dd6f93 | 820 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 821 | if (err < 0) |
98319cb9 | 822 | goto err_trans; |
ffdb210e | 823 | |
36596dad | 824 | list_add_tail_rcu(&table->list, &net->nft.tables); |
96518518 | 825 | return 0; |
98319cb9 | 826 | err_trans: |
1b2470e5 FW |
827 | rhltable_destroy(&table->chains_ht); |
828 | err_chain_ht: | |
e46abbcc | 829 | kfree(table->name); |
98319cb9 | 830 | err_strdup: |
ffdb210e | 831 | kfree(table); |
98319cb9 | 832 | err_kzalloc: |
ffdb210e | 833 | return err; |
96518518 PM |
834 | } |
835 | ||
b9ac12ef AB |
836 | static int nft_flush_table(struct nft_ctx *ctx) |
837 | { | |
3b49e2e9 | 838 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 839 | struct nft_chain *chain, *nc; |
e5009240 | 840 | struct nft_object *obj, *ne; |
b9ac12ef | 841 | struct nft_set *set, *ns; |
3b49e2e9 | 842 | int err; |
b9ac12ef | 843 | |
a2f18db0 | 844 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
845 | if (!nft_is_active_next(ctx->net, chain)) |
846 | continue; | |
847 | ||
b9ac12ef AB |
848 | ctx->chain = chain; |
849 | ||
850 | err = nft_delrule_by_chain(ctx); | |
851 | if (err < 0) | |
852 | goto out; | |
b9ac12ef AB |
853 | } |
854 | ||
855 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
856 | if (!nft_is_active_next(ctx->net, set)) |
857 | continue; | |
858 | ||
408070d6 | 859 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
860 | !list_empty(&set->bindings)) |
861 | continue; | |
862 | ||
863 | err = nft_delset(ctx, set); | |
864 | if (err < 0) | |
865 | goto out; | |
866 | } | |
867 | ||
3b49e2e9 PNA |
868 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
869 | err = nft_delflowtable(ctx, flowtable); | |
870 | if (err < 0) | |
871 | goto out; | |
872 | } | |
873 | ||
e5009240 PNA |
874 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
875 | err = nft_delobj(ctx, obj); | |
876 | if (err < 0) | |
877 | goto out; | |
878 | } | |
879 | ||
a2f18db0 | 880 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
881 | if (!nft_is_active_next(ctx->net, chain)) |
882 | continue; | |
883 | ||
a2f18db0 PNA |
884 | ctx->chain = chain; |
885 | ||
886 | err = nft_delchain(ctx); | |
887 | if (err < 0) | |
888 | goto out; | |
889 | } | |
890 | ||
b9ac12ef AB |
891 | err = nft_deltable(ctx); |
892 | out: | |
893 | return err; | |
894 | } | |
895 | ||
896 | static int nft_flush(struct nft_ctx *ctx, int family) | |
897 | { | |
b9ac12ef AB |
898 | struct nft_table *table, *nt; |
899 | const struct nlattr * const *nla = ctx->nla; | |
900 | int err = 0; | |
901 | ||
36596dad | 902 | list_for_each_entry_safe(table, nt, &ctx->net->nft.tables, list) { |
98319cb9 | 903 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
904 | continue; |
905 | ||
98319cb9 | 906 | ctx->family = table->family; |
f2a6d766 | 907 | |
36596dad PNA |
908 | if (!nft_is_active_next(ctx->net, table)) |
909 | continue; | |
b9ac12ef | 910 | |
36596dad PNA |
911 | if (nla[NFTA_TABLE_NAME] && |
912 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
913 | continue; | |
b9ac12ef | 914 | |
36596dad PNA |
915 | ctx->table = table; |
916 | ||
917 | err = nft_flush_table(ctx); | |
918 | if (err < 0) | |
919 | goto out; | |
b9ac12ef AB |
920 | } |
921 | out: | |
922 | return err; | |
923 | } | |
924 | ||
633c9a84 PNA |
925 | static int nf_tables_deltable(struct net *net, struct sock *nlsk, |
926 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
927 | const struct nlattr * const nla[], |
928 | struct netlink_ext_ack *extack) | |
96518518 PM |
929 | { |
930 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 931 | u8 genmask = nft_genmask_next(net); |
ee01d542 | 932 | int family = nfmsg->nfgen_family; |
36dd1bcc PNA |
933 | const struct nlattr *attr; |
934 | struct nft_table *table; | |
55dd6f93 | 935 | struct nft_ctx ctx; |
96518518 | 936 | |
36596dad | 937 | nft_ctx_init(&ctx, net, skb, nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
938 | if (family == AF_UNSPEC || |
939 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
940 | return nft_flush(&ctx, family); |
941 | ||
36dd1bcc PNA |
942 | if (nla[NFTA_TABLE_HANDLE]) { |
943 | attr = nla[NFTA_TABLE_HANDLE]; | |
944 | table = nft_table_lookup_byhandle(net, attr, genmask); | |
945 | } else { | |
946 | attr = nla[NFTA_TABLE_NAME]; | |
947 | table = nft_table_lookup(net, attr, family, genmask); | |
948 | } | |
3ecbfd65 | 949 | |
36dd1bcc PNA |
950 | if (IS_ERR(table)) { |
951 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 952 | return PTR_ERR(table); |
36dd1bcc | 953 | } |
96518518 | 954 | |
a8278400 PNA |
955 | if (nlh->nlmsg_flags & NLM_F_NONREC && |
956 | table->use > 0) | |
957 | return -EBUSY; | |
958 | ||
98319cb9 | 959 | ctx.family = family; |
b9ac12ef | 960 | ctx.table = table; |
55dd6f93 | 961 | |
b9ac12ef | 962 | return nft_flush_table(&ctx); |
96518518 PM |
963 | } |
964 | ||
55dd6f93 PNA |
965 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
966 | { | |
4fefee57 PNA |
967 | BUG_ON(ctx->table->use > 0); |
968 | ||
1b2470e5 | 969 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 970 | kfree(ctx->table->name); |
55dd6f93 | 971 | kfree(ctx->table); |
55dd6f93 PNA |
972 | } |
973 | ||
cc07eeb0 | 974 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 975 | { |
d8297d4f | 976 | if (WARN_ON(ctype->family >= NFPROTO_NUMPROTO)) |
cc07eeb0 | 977 | return; |
d8297d4f | 978 | |
96518518 | 979 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
cc07eeb0 PNA |
980 | if (WARN_ON(chain_type[ctype->family][ctype->type] != NULL)) { |
981 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
982 | return; | |
96518518 | 983 | } |
9370761c | 984 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 985 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 986 | } |
9370761c | 987 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 988 | |
32537e91 | 989 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 990 | { |
96518518 | 991 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 992 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
993 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
994 | } | |
9370761c | 995 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
996 | |
997 | /* | |
998 | * Chains | |
999 | */ | |
1000 | ||
1001 | static struct nft_chain * | |
cac20fcd | 1002 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1003 | { |
1004 | struct nft_chain *chain; | |
1005 | ||
1006 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1007 | if (chain->handle == handle && |
1008 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1009 | return chain; |
1010 | } | |
1011 | ||
1012 | return ERR_PTR(-ENOENT); | |
1013 | } | |
1014 | ||
1b2470e5 | 1015 | static struct nft_chain *nft_chain_lookup(struct nft_table *table, |
cac20fcd | 1016 | const struct nlattr *nla, u8 genmask) |
96518518 | 1017 | { |
1b2470e5 FW |
1018 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1019 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1020 | struct nft_chain *chain; |
1021 | ||
1022 | if (nla == NULL) | |
1023 | return ERR_PTR(-EINVAL); | |
1024 | ||
1b2470e5 | 1025 | nla_strlcpy(search, nla, sizeof(search)); |
96518518 | 1026 | |
1b2470e5 FW |
1027 | WARN_ON(!rcu_read_lock_held() && |
1028 | !lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
1029 | ||
1030 | chain = ERR_PTR(-ENOENT); | |
1031 | rcu_read_lock(); | |
1032 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1033 | if (!list) | |
1034 | goto out_unlock; | |
1035 | ||
1036 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1037 | if (nft_active_genmask(chain, genmask)) | |
1038 | goto out_unlock; | |
1039 | } | |
1040 | chain = ERR_PTR(-ENOENT); | |
1041 | out_unlock: | |
1042 | rcu_read_unlock(); | |
1043 | return chain; | |
96518518 PM |
1044 | } |
1045 | ||
1046 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1047 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1048 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1049 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1050 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1051 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1052 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1053 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
4c1f7818 | 1054 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING }, |
0ca743a5 | 1055 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
96518518 PM |
1056 | }; |
1057 | ||
1058 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1059 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1060 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1061 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1062 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1063 | }; |
1064 | ||
0ca743a5 PNA |
1065 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1066 | { | |
1067 | struct nft_stats *cpu_stats, total; | |
1068 | struct nlattr *nest; | |
ce355e20 ED |
1069 | unsigned int seq; |
1070 | u64 pkts, bytes; | |
0ca743a5 PNA |
1071 | int cpu; |
1072 | ||
1073 | memset(&total, 0, sizeof(total)); | |
1074 | for_each_possible_cpu(cpu) { | |
1075 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 ED |
1076 | do { |
1077 | seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); | |
1078 | pkts = cpu_stats->pkts; | |
1079 | bytes = cpu_stats->bytes; | |
1080 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); | |
1081 | total.pkts += pkts; | |
1082 | total.bytes += bytes; | |
0ca743a5 PNA |
1083 | } |
1084 | nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS); | |
1085 | if (nest == NULL) | |
1086 | goto nla_put_failure; | |
1087 | ||
b46f6ded ND |
1088 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
1089 | NFTA_COUNTER_PAD) || | |
1090 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
1091 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
1092 | goto nla_put_failure; |
1093 | ||
1094 | nla_nest_end(skb, nest); | |
1095 | return 0; | |
1096 | ||
1097 | nla_put_failure: | |
1098 | return -ENOSPC; | |
1099 | } | |
1100 | ||
84d7fce6 PNA |
1101 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
1102 | u32 portid, u32 seq, int event, u32 flags, | |
1103 | int family, const struct nft_table *table, | |
96518518 PM |
1104 | const struct nft_chain *chain) |
1105 | { | |
1106 | struct nlmsghdr *nlh; | |
1107 | struct nfgenmsg *nfmsg; | |
1108 | ||
dedb67c4 | 1109 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 PM |
1110 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
1111 | if (nlh == NULL) | |
1112 | goto nla_put_failure; | |
1113 | ||
1114 | nfmsg = nlmsg_data(nlh); | |
1115 | nfmsg->nfgen_family = family; | |
1116 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 1117 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 PM |
1118 | |
1119 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) | |
1120 | goto nla_put_failure; | |
b46f6ded ND |
1121 | if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), |
1122 | NFTA_CHAIN_PAD)) | |
96518518 PM |
1123 | goto nla_put_failure; |
1124 | if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) | |
1125 | goto nla_put_failure; | |
1126 | ||
f323d954 | 1127 | if (nft_is_base_chain(chain)) { |
0ca743a5 | 1128 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
c974a3a3 | 1129 | const struct nf_hook_ops *ops = &basechain->ops; |
0ca743a5 PNA |
1130 | struct nlattr *nest; |
1131 | ||
1132 | nest = nla_nest_start(skb, NFTA_CHAIN_HOOK); | |
96518518 PM |
1133 | if (nest == NULL) |
1134 | goto nla_put_failure; | |
1135 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1136 | goto nla_put_failure; | |
1137 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1138 | goto nla_put_failure; | |
2cbce139 PNA |
1139 | if (basechain->dev_name[0] && |
1140 | nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name)) | |
1141 | goto nla_put_failure; | |
96518518 | 1142 | nla_nest_end(skb, nest); |
9370761c | 1143 | |
0ca743a5 PNA |
1144 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1145 | htonl(basechain->policy))) | |
1146 | goto nla_put_failure; | |
1147 | ||
baae3e62 PM |
1148 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1149 | goto nla_put_failure; | |
0ca743a5 | 1150 | |
5f9bfe0e | 1151 | if (basechain->stats && nft_dump_stats(skb, basechain->stats)) |
0ca743a5 | 1152 | goto nla_put_failure; |
96518518 PM |
1153 | } |
1154 | ||
0ca743a5 PNA |
1155 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1156 | goto nla_put_failure; | |
1157 | ||
053c095a JB |
1158 | nlmsg_end(skb, nlh); |
1159 | return 0; | |
96518518 PM |
1160 | |
1161 | nla_put_failure: | |
1162 | nlmsg_trim(skb, nlh); | |
1163 | return -1; | |
1164 | } | |
1165 | ||
25e94a99 | 1166 | static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
96518518 PM |
1167 | { |
1168 | struct sk_buff *skb; | |
96518518 PM |
1169 | int err; |
1170 | ||
128ad332 PNA |
1171 | if (!ctx->report && |
1172 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1173 | return; |
96518518 | 1174 | |
96518518 PM |
1175 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1176 | if (skb == NULL) | |
1177 | goto err; | |
1178 | ||
84d7fce6 | 1179 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 1180 | event, 0, ctx->family, ctx->table, |
35151d84 | 1181 | ctx->chain); |
96518518 PM |
1182 | if (err < 0) { |
1183 | kfree_skb(skb); | |
1184 | goto err; | |
1185 | } | |
1186 | ||
25e94a99 PNA |
1187 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
1188 | ctx->report, GFP_KERNEL); | |
1189 | return; | |
96518518 | 1190 | err: |
25e94a99 | 1191 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1192 | } |
1193 | ||
1194 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1195 | struct netlink_callback *cb) | |
1196 | { | |
1197 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 PM |
1198 | const struct nft_table *table; |
1199 | const struct nft_chain *chain; | |
1200 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 1201 | struct net *net = sock_net(skb->sk); |
96518518 PM |
1202 | int family = nfmsg->nfgen_family; |
1203 | ||
e688a7f8 | 1204 | rcu_read_lock(); |
38e029f1 PNA |
1205 | cb->seq = net->nft.base_seq; |
1206 | ||
36596dad | 1207 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 1208 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1209 | continue; |
1210 | ||
36596dad PNA |
1211 | list_for_each_entry_rcu(chain, &table->chains, list) { |
1212 | if (idx < s_idx) | |
1213 | goto cont; | |
1214 | if (idx > s_idx) | |
1215 | memset(&cb->args[1], 0, | |
1216 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1217 | if (!nft_is_active(net, chain)) | |
1218 | continue; | |
1219 | if (nf_tables_fill_chain_info(skb, net, | |
1220 | NETLINK_CB(cb->skb).portid, | |
1221 | cb->nlh->nlmsg_seq, | |
1222 | NFT_MSG_NEWCHAIN, | |
1223 | NLM_F_MULTI, | |
98319cb9 | 1224 | table->family, table, |
36596dad PNA |
1225 | chain) < 0) |
1226 | goto done; | |
38e029f1 | 1227 | |
36596dad | 1228 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
96518518 | 1229 | cont: |
36596dad | 1230 | idx++; |
96518518 PM |
1231 | } |
1232 | } | |
1233 | done: | |
e688a7f8 | 1234 | rcu_read_unlock(); |
96518518 PM |
1235 | cb->args[0] = idx; |
1236 | return skb->len; | |
1237 | } | |
1238 | ||
d9adf22a | 1239 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
1240 | static int nf_tables_getchain(struct net *net, struct sock *nlsk, |
1241 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
1242 | const struct nlattr * const nla[], |
1243 | struct netlink_ext_ack *extack) | |
96518518 PM |
1244 | { |
1245 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 1246 | u8 genmask = nft_genmask_cur(net); |
96518518 | 1247 | const struct nft_chain *chain; |
1b2470e5 | 1248 | struct nft_table *table; |
96518518 PM |
1249 | struct sk_buff *skb2; |
1250 | int family = nfmsg->nfgen_family; | |
1251 | int err; | |
1252 | ||
1253 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1254 | struct netlink_dump_control c = { | |
1255 | .dump = nf_tables_dump_chains, | |
d9adf22a | 1256 | .module = THIS_MODULE, |
96518518 | 1257 | }; |
d9adf22a FW |
1258 | |
1259 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); | |
96518518 PM |
1260 | } |
1261 | ||
cac20fcd | 1262 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
1263 | if (IS_ERR(table)) { |
1264 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1265 | return PTR_ERR(table); |
36dd1bcc | 1266 | } |
96518518 | 1267 | |
cac20fcd | 1268 | chain = nft_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask); |
36dd1bcc PNA |
1269 | if (IS_ERR(chain)) { |
1270 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
96518518 | 1271 | return PTR_ERR(chain); |
36dd1bcc | 1272 | } |
96518518 | 1273 | |
d9adf22a | 1274 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1275 | if (!skb2) |
1276 | return -ENOMEM; | |
1277 | ||
84d7fce6 | 1278 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
1279 | nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0, |
1280 | family, table, chain); | |
1281 | if (err < 0) | |
1282 | goto err; | |
1283 | ||
1284 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
1285 | ||
1286 | err: | |
1287 | kfree_skb(skb2); | |
1288 | return err; | |
1289 | } | |
1290 | ||
0ca743a5 PNA |
1291 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1292 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1293 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1294 | }; | |
1295 | ||
ff3cd7b3 | 1296 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1297 | { |
1298 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1299 | struct nft_stats __percpu *newstats; | |
1300 | struct nft_stats *stats; | |
1301 | int err; | |
1302 | ||
fceb6435 JB |
1303 | err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy, |
1304 | NULL); | |
0ca743a5 | 1305 | if (err < 0) |
ff3cd7b3 | 1306 | return ERR_PTR(err); |
0ca743a5 PNA |
1307 | |
1308 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1309 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1310 | |
ce355e20 | 1311 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1312 | if (newstats == NULL) |
ff3cd7b3 | 1313 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1314 | |
1315 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1316 | * are not exposed to userspace. | |
1317 | */ | |
e8781f70 | 1318 | preempt_disable(); |
0ca743a5 PNA |
1319 | stats = this_cpu_ptr(newstats); |
1320 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
1321 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 1322 | preempt_enable(); |
0ca743a5 | 1323 | |
ff3cd7b3 PNA |
1324 | return newstats; |
1325 | } | |
1326 | ||
1327 | static void nft_chain_stats_replace(struct nft_base_chain *chain, | |
1328 | struct nft_stats __percpu *newstats) | |
1329 | { | |
0befd061 PNA |
1330 | struct nft_stats __percpu *oldstats; |
1331 | ||
b88825de PNA |
1332 | if (newstats == NULL) |
1333 | return; | |
1334 | ||
0ca743a5 | 1335 | if (chain->stats) { |
0befd061 | 1336 | oldstats = nfnl_dereference(chain->stats, NFNL_SUBSYS_NFTABLES); |
0ca743a5 PNA |
1337 | rcu_assign_pointer(chain->stats, newstats); |
1338 | synchronize_rcu(); | |
1339 | free_percpu(oldstats); | |
bbb8c61f | 1340 | } else { |
0ca743a5 | 1341 | rcu_assign_pointer(chain->stats, newstats); |
bbb8c61f TY |
1342 | static_branch_inc(&nft_counters_enabled); |
1343 | } | |
0ca743a5 PNA |
1344 | } |
1345 | ||
0cbc06b3 FW |
1346 | static void nf_tables_chain_free_chain_rules(struct nft_chain *chain) |
1347 | { | |
1348 | struct nft_rule **g0 = rcu_dereference_raw(chain->rules_gen_0); | |
1349 | struct nft_rule **g1 = rcu_dereference_raw(chain->rules_gen_1); | |
1350 | ||
1351 | if (g0 != g1) | |
1352 | kvfree(g1); | |
1353 | kvfree(g0); | |
1354 | ||
1355 | /* should be NULL either via abort or via successful commit */ | |
1356 | WARN_ON_ONCE(chain->rules_next); | |
1357 | kvfree(chain->rules_next); | |
1358 | } | |
1359 | ||
43a605f2 | 1360 | static void nf_tables_chain_destroy(struct nft_ctx *ctx) |
91c7b38d | 1361 | { |
43a605f2 PNA |
1362 | struct nft_chain *chain = ctx->chain; |
1363 | ||
91c7b38d PNA |
1364 | BUG_ON(chain->use > 0); |
1365 | ||
0cbc06b3 FW |
1366 | /* no concurrent access possible anymore */ |
1367 | nf_tables_chain_free_chain_rules(chain); | |
1368 | ||
f323d954 | 1369 | if (nft_is_base_chain(chain)) { |
2cbce139 PNA |
1370 | struct nft_base_chain *basechain = nft_base_chain(chain); |
1371 | ||
1372 | module_put(basechain->type->owner); | |
1373 | free_percpu(basechain->stats); | |
9f08ea84 PNA |
1374 | if (basechain->stats) |
1375 | static_branch_dec(&nft_counters_enabled); | |
b7263e07 | 1376 | kfree(chain->name); |
2cbce139 | 1377 | kfree(basechain); |
91c7b38d | 1378 | } else { |
b7263e07 | 1379 | kfree(chain->name); |
91c7b38d PNA |
1380 | kfree(chain); |
1381 | } | |
1382 | } | |
1383 | ||
508f8ccd PNA |
1384 | struct nft_chain_hook { |
1385 | u32 num; | |
84ba7dd7 | 1386 | s32 priority; |
32537e91 | 1387 | const struct nft_chain_type *type; |
508f8ccd PNA |
1388 | struct net_device *dev; |
1389 | }; | |
1390 | ||
1391 | static int nft_chain_parse_hook(struct net *net, | |
1392 | const struct nlattr * const nla[], | |
36596dad PNA |
1393 | struct nft_chain_hook *hook, u8 family, |
1394 | bool create) | |
508f8ccd PNA |
1395 | { |
1396 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; | |
32537e91 | 1397 | const struct nft_chain_type *type; |
508f8ccd PNA |
1398 | struct net_device *dev; |
1399 | int err; | |
1400 | ||
1401 | err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK], | |
fceb6435 | 1402 | nft_hook_policy, NULL); |
508f8ccd PNA |
1403 | if (err < 0) |
1404 | return err; | |
1405 | ||
1406 | if (ha[NFTA_HOOK_HOOKNUM] == NULL || | |
1407 | ha[NFTA_HOOK_PRIORITY] == NULL) | |
1408 | return -EINVAL; | |
1409 | ||
1410 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
508f8ccd PNA |
1411 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); |
1412 | ||
36596dad | 1413 | type = chain_type[family][NFT_CHAIN_T_DEFAULT]; |
508f8ccd | 1414 | if (nla[NFTA_CHAIN_TYPE]) { |
1ea26cca | 1415 | type = nf_tables_chain_type_lookup(nla[NFTA_CHAIN_TYPE], |
36596dad | 1416 | family, create); |
508f8ccd PNA |
1417 | if (IS_ERR(type)) |
1418 | return PTR_ERR(type); | |
1419 | } | |
1420 | if (!(type->hook_mask & (1 << hook->num))) | |
1421 | return -EOPNOTSUPP; | |
84ba7dd7 FW |
1422 | |
1423 | if (type->type == NFT_CHAIN_T_NAT && | |
1424 | hook->priority <= NF_IP_PRI_CONNTRACK) | |
1425 | return -EOPNOTSUPP; | |
1426 | ||
508f8ccd PNA |
1427 | if (!try_module_get(type->owner)) |
1428 | return -ENOENT; | |
1429 | ||
1430 | hook->type = type; | |
1431 | ||
1432 | hook->dev = NULL; | |
36596dad | 1433 | if (family == NFPROTO_NETDEV) { |
508f8ccd PNA |
1434 | char ifname[IFNAMSIZ]; |
1435 | ||
1436 | if (!ha[NFTA_HOOK_DEV]) { | |
1437 | module_put(type->owner); | |
1438 | return -EOPNOTSUPP; | |
1439 | } | |
1440 | ||
1441 | nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ); | |
90d2723c | 1442 | dev = __dev_get_by_name(net, ifname); |
508f8ccd PNA |
1443 | if (!dev) { |
1444 | module_put(type->owner); | |
1445 | return -ENOENT; | |
1446 | } | |
1447 | hook->dev = dev; | |
1448 | } else if (ha[NFTA_HOOK_DEV]) { | |
1449 | module_put(type->owner); | |
1450 | return -EOPNOTSUPP; | |
1451 | } | |
1452 | ||
1453 | return 0; | |
1454 | } | |
1455 | ||
1456 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
1457 | { | |
1458 | module_put(hook->type->owner); | |
508f8ccd PNA |
1459 | } |
1460 | ||
0cbc06b3 FW |
1461 | struct nft_rules_old { |
1462 | struct rcu_head h; | |
1463 | struct nft_rule **start; | |
1464 | }; | |
1465 | ||
1466 | static struct nft_rule **nf_tables_chain_alloc_rules(const struct nft_chain *chain, | |
1467 | unsigned int alloc) | |
1468 | { | |
1469 | if (alloc > INT_MAX) | |
1470 | return NULL; | |
1471 | ||
1472 | alloc += 1; /* NULL, ends rules */ | |
1473 | if (sizeof(struct nft_rule *) > INT_MAX / alloc) | |
1474 | return NULL; | |
1475 | ||
1476 | alloc *= sizeof(struct nft_rule *); | |
1477 | alloc += sizeof(struct nft_rules_old); | |
1478 | ||
1479 | return kvmalloc(alloc, GFP_KERNEL); | |
1480 | } | |
1481 | ||
4035285f PNA |
1482 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
1483 | u8 policy, bool create) | |
1484 | { | |
1485 | const struct nlattr * const *nla = ctx->nla; | |
1486 | struct nft_table *table = ctx->table; | |
4035285f PNA |
1487 | struct nft_base_chain *basechain; |
1488 | struct nft_stats __percpu *stats; | |
1489 | struct net *net = ctx->net; | |
1490 | struct nft_chain *chain; | |
0cbc06b3 | 1491 | struct nft_rule **rules; |
4035285f PNA |
1492 | int err; |
1493 | ||
1494 | if (table->use == UINT_MAX) | |
1495 | return -EOVERFLOW; | |
1496 | ||
1497 | if (nla[NFTA_CHAIN_HOOK]) { | |
1498 | struct nft_chain_hook hook; | |
1499 | struct nf_hook_ops *ops; | |
4035285f | 1500 | |
36596dad | 1501 | err = nft_chain_parse_hook(net, nla, &hook, family, create); |
4035285f PNA |
1502 | if (err < 0) |
1503 | return err; | |
1504 | ||
1505 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL); | |
1506 | if (basechain == NULL) { | |
1507 | nft_chain_release_hook(&hook); | |
1508 | return -ENOMEM; | |
1509 | } | |
1510 | ||
1511 | if (hook.dev != NULL) | |
1512 | strncpy(basechain->dev_name, hook.dev->name, IFNAMSIZ); | |
1513 | ||
1514 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
1515 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
1516 | if (IS_ERR(stats)) { | |
1517 | nft_chain_release_hook(&hook); | |
1518 | kfree(basechain); | |
1519 | return PTR_ERR(stats); | |
1520 | } | |
1521 | basechain->stats = stats; | |
1522 | static_branch_inc(&nft_counters_enabled); | |
1523 | } | |
1524 | ||
4035285f PNA |
1525 | basechain->type = hook.type; |
1526 | chain = &basechain->chain; | |
1527 | ||
c974a3a3 PNA |
1528 | ops = &basechain->ops; |
1529 | ops->pf = family; | |
1530 | ops->hooknum = hook.num; | |
1531 | ops->priority = hook.priority; | |
1532 | ops->priv = chain; | |
c2f9eafe | 1533 | ops->hook = hook.type->hooks[ops->hooknum]; |
c974a3a3 | 1534 | ops->dev = hook.dev; |
c974a3a3 | 1535 | |
4035285f PNA |
1536 | chain->flags |= NFT_BASE_CHAIN; |
1537 | basechain->policy = policy; | |
1538 | } else { | |
1539 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); | |
1540 | if (chain == NULL) | |
1541 | return -ENOMEM; | |
1542 | } | |
43a605f2 PNA |
1543 | ctx->chain = chain; |
1544 | ||
4035285f PNA |
1545 | INIT_LIST_HEAD(&chain->rules); |
1546 | chain->handle = nf_tables_alloc_handle(table); | |
1547 | chain->table = table; | |
1548 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); | |
1549 | if (!chain->name) { | |
1550 | err = -ENOMEM; | |
1551 | goto err1; | |
1552 | } | |
1553 | ||
0cbc06b3 FW |
1554 | rules = nf_tables_chain_alloc_rules(chain, 0); |
1555 | if (!rules) { | |
1556 | err = -ENOMEM; | |
1557 | goto err1; | |
1558 | } | |
1559 | ||
1560 | *rules = NULL; | |
1561 | rcu_assign_pointer(chain->rules_gen_0, rules); | |
1562 | rcu_assign_pointer(chain->rules_gen_1, rules); | |
1563 | ||
c974a3a3 | 1564 | err = nf_tables_register_hook(net, table, chain); |
4035285f PNA |
1565 | if (err < 0) |
1566 | goto err1; | |
1567 | ||
1b2470e5 FW |
1568 | err = rhltable_insert_key(&table->chains_ht, chain->name, |
1569 | &chain->rhlhead, nft_chain_ht_params); | |
1570 | if (err) | |
1571 | goto err2; | |
1572 | ||
4035285f | 1573 | err = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
1b2470e5 FW |
1574 | if (err < 0) { |
1575 | rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
1576 | nft_chain_ht_params); | |
4035285f | 1577 | goto err2; |
1b2470e5 | 1578 | } |
4035285f PNA |
1579 | |
1580 | table->use++; | |
1581 | list_add_tail_rcu(&chain->list, &table->chains); | |
1582 | ||
1583 | return 0; | |
1584 | err2: | |
c974a3a3 | 1585 | nf_tables_unregister_hook(net, table, chain); |
4035285f | 1586 | err1: |
43a605f2 | 1587 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
1588 | |
1589 | return err; | |
1590 | } | |
1591 | ||
2c4a488a PNA |
1592 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
1593 | bool create) | |
1594 | { | |
1595 | const struct nlattr * const *nla = ctx->nla; | |
1596 | struct nft_table *table = ctx->table; | |
1597 | struct nft_chain *chain = ctx->chain; | |
2c4a488a PNA |
1598 | struct nft_base_chain *basechain; |
1599 | struct nft_stats *stats = NULL; | |
1600 | struct nft_chain_hook hook; | |
2c4a488a PNA |
1601 | struct nf_hook_ops *ops; |
1602 | struct nft_trans *trans; | |
c974a3a3 | 1603 | int err; |
2c4a488a PNA |
1604 | |
1605 | if (nla[NFTA_CHAIN_HOOK]) { | |
1606 | if (!nft_is_base_chain(chain)) | |
1607 | return -EBUSY; | |
1608 | ||
36596dad | 1609 | err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family, |
2c4a488a PNA |
1610 | create); |
1611 | if (err < 0) | |
1612 | return err; | |
1613 | ||
1614 | basechain = nft_base_chain(chain); | |
1615 | if (basechain->type != hook.type) { | |
1616 | nft_chain_release_hook(&hook); | |
1617 | return -EBUSY; | |
1618 | } | |
1619 | ||
c974a3a3 PNA |
1620 | ops = &basechain->ops; |
1621 | if (ops->hooknum != hook.num || | |
1622 | ops->priority != hook.priority || | |
1623 | ops->dev != hook.dev) { | |
1624 | nft_chain_release_hook(&hook); | |
1625 | return -EBUSY; | |
2c4a488a PNA |
1626 | } |
1627 | nft_chain_release_hook(&hook); | |
1628 | } | |
1629 | ||
1630 | if (nla[NFTA_CHAIN_HANDLE] && | |
1631 | nla[NFTA_CHAIN_NAME]) { | |
1632 | struct nft_chain *chain2; | |
1633 | ||
cac20fcd | 1634 | chain2 = nft_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask); |
0d18779b JC |
1635 | if (!IS_ERR(chain2)) |
1636 | return -EEXIST; | |
2c4a488a PNA |
1637 | } |
1638 | ||
1639 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
1640 | if (!nft_is_base_chain(chain)) | |
1641 | return -EOPNOTSUPP; | |
1642 | ||
1643 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
1644 | if (IS_ERR(stats)) | |
1645 | return PTR_ERR(stats); | |
1646 | } | |
1647 | ||
c6cc94df | 1648 | err = -ENOMEM; |
2c4a488a PNA |
1649 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
1650 | sizeof(struct nft_trans_chain)); | |
c6cc94df FW |
1651 | if (trans == NULL) |
1652 | goto err; | |
2c4a488a PNA |
1653 | |
1654 | nft_trans_chain_stats(trans) = stats; | |
1655 | nft_trans_chain_update(trans) = true; | |
1656 | ||
1657 | if (nla[NFTA_CHAIN_POLICY]) | |
1658 | nft_trans_chain_policy(trans) = policy; | |
1659 | else | |
1660 | nft_trans_chain_policy(trans) = -1; | |
1661 | ||
c6cc94df FW |
1662 | if (nla[NFTA_CHAIN_HANDLE] && |
1663 | nla[NFTA_CHAIN_NAME]) { | |
1664 | struct nft_trans *tmp; | |
1665 | char *name; | |
1666 | ||
1667 | err = -ENOMEM; | |
1668 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); | |
1669 | if (!name) | |
1670 | goto err; | |
1671 | ||
1672 | err = -EEXIST; | |
1673 | list_for_each_entry(tmp, &ctx->net->nft.commit_list, list) { | |
1674 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && | |
1675 | tmp->ctx.table == table && | |
1676 | nft_trans_chain_update(tmp) && | |
1677 | nft_trans_chain_name(tmp) && | |
1678 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
1679 | kfree(name); | |
1680 | goto err; | |
1681 | } | |
2c4a488a | 1682 | } |
c6cc94df FW |
1683 | |
1684 | nft_trans_chain_name(trans) = name; | |
2c4a488a PNA |
1685 | } |
1686 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
1687 | ||
1688 | return 0; | |
c6cc94df FW |
1689 | err: |
1690 | free_percpu(stats); | |
1691 | kfree(trans); | |
1692 | return err; | |
2c4a488a PNA |
1693 | } |
1694 | ||
633c9a84 PNA |
1695 | static int nf_tables_newchain(struct net *net, struct sock *nlsk, |
1696 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
1697 | const struct nlattr * const nla[], |
1698 | struct netlink_ext_ack *extack) | |
96518518 PM |
1699 | { |
1700 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
4035285f PNA |
1701 | u8 genmask = nft_genmask_next(net); |
1702 | int family = nfmsg->nfgen_family; | |
36dd1bcc | 1703 | const struct nlattr *attr; |
96518518 PM |
1704 | struct nft_table *table; |
1705 | struct nft_chain *chain; | |
57de2a0c | 1706 | u8 policy = NF_ACCEPT; |
4035285f | 1707 | struct nft_ctx ctx; |
96518518 | 1708 | u64 handle = 0; |
96518518 PM |
1709 | bool create; |
1710 | ||
1711 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; | |
1712 | ||
cac20fcd | 1713 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
1714 | if (IS_ERR(table)) { |
1715 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1716 | return PTR_ERR(table); |
36dd1bcc | 1717 | } |
96518518 | 1718 | |
96518518 | 1719 | chain = NULL; |
36dd1bcc | 1720 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
1721 | |
1722 | if (nla[NFTA_CHAIN_HANDLE]) { | |
1723 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 1724 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
1725 | if (IS_ERR(chain)) { |
1726 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 1727 | return PTR_ERR(chain); |
36dd1bcc PNA |
1728 | } |
1729 | attr = nla[NFTA_CHAIN_HANDLE]; | |
96518518 | 1730 | } else { |
36dd1bcc | 1731 | chain = nft_chain_lookup(table, attr, genmask); |
96518518 | 1732 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
1733 | if (PTR_ERR(chain) != -ENOENT) { |
1734 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 1735 | return PTR_ERR(chain); |
36dd1bcc | 1736 | } |
96518518 PM |
1737 | chain = NULL; |
1738 | } | |
1739 | } | |
1740 | ||
57de2a0c | 1741 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 1742 | if (chain != NULL && |
36dd1bcc PNA |
1743 | !nft_is_base_chain(chain)) { |
1744 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 1745 | return -EOPNOTSUPP; |
36dd1bcc | 1746 | } |
d6b6cb1d PNA |
1747 | |
1748 | if (chain == NULL && | |
36dd1bcc PNA |
1749 | nla[NFTA_CHAIN_HOOK] == NULL) { |
1750 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 1751 | return -EOPNOTSUPP; |
36dd1bcc | 1752 | } |
57de2a0c | 1753 | |
8f46df18 | 1754 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
1755 | switch (policy) { |
1756 | case NF_DROP: | |
1757 | case NF_ACCEPT: | |
1758 | break; | |
1759 | default: | |
1760 | return -EINVAL; | |
1761 | } | |
1762 | } | |
1763 | ||
98319cb9 | 1764 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
4035285f | 1765 | |
96518518 | 1766 | if (chain != NULL) { |
36dd1bcc PNA |
1767 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
1768 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 1769 | return -EEXIST; |
36dd1bcc | 1770 | } |
96518518 PM |
1771 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
1772 | return -EOPNOTSUPP; | |
1773 | ||
2c4a488a | 1774 | return nf_tables_updchain(&ctx, genmask, policy, create); |
96518518 PM |
1775 | } |
1776 | ||
4035285f | 1777 | return nf_tables_addchain(&ctx, family, genmask, policy, create); |
96518518 PM |
1778 | } |
1779 | ||
633c9a84 PNA |
1780 | static int nf_tables_delchain(struct net *net, struct sock *nlsk, |
1781 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
1782 | const struct nlattr * const nla[], |
1783 | struct netlink_ext_ack *extack) | |
96518518 PM |
1784 | { |
1785 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 1786 | u8 genmask = nft_genmask_next(net); |
36dd1bcc PNA |
1787 | int family = nfmsg->nfgen_family; |
1788 | const struct nlattr *attr; | |
96518518 PM |
1789 | struct nft_table *table; |
1790 | struct nft_chain *chain; | |
9dee1474 | 1791 | struct nft_rule *rule; |
91c7b38d | 1792 | struct nft_ctx ctx; |
3ecbfd65 | 1793 | u64 handle; |
9dee1474 PNA |
1794 | u32 use; |
1795 | int err; | |
96518518 | 1796 | |
cac20fcd | 1797 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
1798 | if (IS_ERR(table)) { |
1799 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1800 | return PTR_ERR(table); |
36dd1bcc | 1801 | } |
96518518 | 1802 | |
3ecbfd65 | 1803 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
1804 | attr = nla[NFTA_CHAIN_HANDLE]; |
1805 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 1806 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 1807 | } else { |
36dd1bcc PNA |
1808 | attr = nla[NFTA_CHAIN_NAME]; |
1809 | chain = nft_chain_lookup(table, attr, genmask); | |
3ecbfd65 | 1810 | } |
36dd1bcc PNA |
1811 | if (IS_ERR(chain)) { |
1812 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 1813 | return PTR_ERR(chain); |
36dd1bcc | 1814 | } |
9dee1474 PNA |
1815 | |
1816 | if (nlh->nlmsg_flags & NLM_F_NONREC && | |
1817 | chain->use > 0) | |
96518518 PM |
1818 | return -EBUSY; |
1819 | ||
98319cb9 | 1820 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0165d932 | 1821 | |
9dee1474 PNA |
1822 | use = chain->use; |
1823 | list_for_each_entry(rule, &chain->rules, list) { | |
1824 | if (!nft_is_active_next(net, rule)) | |
1825 | continue; | |
1826 | use--; | |
1827 | ||
1828 | err = nft_delrule(&ctx, rule); | |
1829 | if (err < 0) | |
1830 | return err; | |
1831 | } | |
1832 | ||
1833 | /* There are rules and elements that are still holding references to us, | |
1834 | * we cannot do a recursive removal in this case. | |
1835 | */ | |
36dd1bcc PNA |
1836 | if (use > 0) { |
1837 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 1838 | return -EBUSY; |
36dd1bcc | 1839 | } |
9dee1474 | 1840 | |
ee01d542 | 1841 | return nft_delchain(&ctx); |
96518518 PM |
1842 | } |
1843 | ||
96518518 PM |
1844 | /* |
1845 | * Expressions | |
1846 | */ | |
1847 | ||
1848 | /** | |
ef1f7df9 PM |
1849 | * nft_register_expr - register nf_tables expr type |
1850 | * @ops: expr type | |
96518518 | 1851 | * |
ef1f7df9 | 1852 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
1853 | * success or a negative errno code otherwise. |
1854 | */ | |
ef1f7df9 | 1855 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
1856 | { |
1857 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 1858 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 1859 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 1860 | else |
e688a7f8 | 1861 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
1862 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1863 | return 0; | |
1864 | } | |
1865 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
1866 | ||
1867 | /** | |
ef1f7df9 PM |
1868 | * nft_unregister_expr - unregister nf_tables expr type |
1869 | * @ops: expr type | |
96518518 | 1870 | * |
ef1f7df9 | 1871 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 1872 | */ |
ef1f7df9 | 1873 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
1874 | { |
1875 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 1876 | list_del_rcu(&type->list); |
96518518 PM |
1877 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1878 | } | |
1879 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
1880 | ||
64d46806 PM |
1881 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
1882 | struct nlattr *nla) | |
96518518 | 1883 | { |
ef1f7df9 | 1884 | const struct nft_expr_type *type; |
96518518 | 1885 | |
ef1f7df9 | 1886 | list_for_each_entry(type, &nf_tables_expressions, list) { |
64d46806 PM |
1887 | if (!nla_strcmp(nla, type->name) && |
1888 | (!type->family || type->family == family)) | |
ef1f7df9 | 1889 | return type; |
96518518 PM |
1890 | } |
1891 | return NULL; | |
1892 | } | |
1893 | ||
64d46806 PM |
1894 | static const struct nft_expr_type *nft_expr_type_get(u8 family, |
1895 | struct nlattr *nla) | |
96518518 | 1896 | { |
ef1f7df9 | 1897 | const struct nft_expr_type *type; |
96518518 PM |
1898 | |
1899 | if (nla == NULL) | |
1900 | return ERR_PTR(-EINVAL); | |
1901 | ||
64d46806 | 1902 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
1903 | if (type != NULL && try_module_get(type->owner)) |
1904 | return type; | |
96518518 PM |
1905 | |
1906 | #ifdef CONFIG_MODULES | |
ef1f7df9 | 1907 | if (type == NULL) { |
64d46806 PM |
1908 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1909 | request_module("nft-expr-%u-%.*s", family, | |
1910 | nla_len(nla), (char *)nla_data(nla)); | |
1911 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
1912 | if (__nft_expr_type_get(family, nla)) | |
1913 | return ERR_PTR(-EAGAIN); | |
1914 | ||
96518518 PM |
1915 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1916 | request_module("nft-expr-%.*s", | |
1917 | nla_len(nla), (char *)nla_data(nla)); | |
1918 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
64d46806 | 1919 | if (__nft_expr_type_get(family, nla)) |
96518518 PM |
1920 | return ERR_PTR(-EAGAIN); |
1921 | } | |
1922 | #endif | |
1923 | return ERR_PTR(-ENOENT); | |
1924 | } | |
1925 | ||
1926 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
1927 | [NFTA_EXPR_NAME] = { .type = NLA_STRING }, | |
1928 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, | |
1929 | }; | |
1930 | ||
1931 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
1932 | const struct nft_expr *expr) | |
1933 | { | |
ef1f7df9 | 1934 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
1935 | goto nla_put_failure; |
1936 | ||
1937 | if (expr->ops->dump) { | |
1938 | struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA); | |
1939 | if (data == NULL) | |
1940 | goto nla_put_failure; | |
1941 | if (expr->ops->dump(skb, expr) < 0) | |
1942 | goto nla_put_failure; | |
1943 | nla_nest_end(skb, data); | |
1944 | } | |
1945 | ||
1946 | return skb->len; | |
1947 | ||
1948 | nla_put_failure: | |
1949 | return -1; | |
1950 | }; | |
1951 | ||
0b2d8a7b PM |
1952 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
1953 | const struct nft_expr *expr) | |
1954 | { | |
1955 | struct nlattr *nest; | |
1956 | ||
1957 | nest = nla_nest_start(skb, attr); | |
1958 | if (!nest) | |
1959 | goto nla_put_failure; | |
1960 | if (nf_tables_fill_expr_info(skb, expr) < 0) | |
1961 | goto nla_put_failure; | |
1962 | nla_nest_end(skb, nest); | |
1963 | return 0; | |
1964 | ||
1965 | nla_put_failure: | |
1966 | return -1; | |
1967 | } | |
1968 | ||
96518518 PM |
1969 | struct nft_expr_info { |
1970 | const struct nft_expr_ops *ops; | |
ef1f7df9 | 1971 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
1972 | }; |
1973 | ||
0ca743a5 PNA |
1974 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
1975 | const struct nlattr *nla, | |
96518518 PM |
1976 | struct nft_expr_info *info) |
1977 | { | |
ef1f7df9 | 1978 | const struct nft_expr_type *type; |
96518518 | 1979 | const struct nft_expr_ops *ops; |
ef1f7df9 | 1980 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
1981 | int err; |
1982 | ||
fceb6435 | 1983 | err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy, NULL); |
96518518 PM |
1984 | if (err < 0) |
1985 | return err; | |
1986 | ||
36596dad | 1987 | type = nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
1988 | if (IS_ERR(type)) |
1989 | return PTR_ERR(type); | |
1990 | ||
1991 | if (tb[NFTA_EXPR_DATA]) { | |
1992 | err = nla_parse_nested(info->tb, type->maxattr, | |
fceb6435 | 1993 | tb[NFTA_EXPR_DATA], type->policy, NULL); |
ef1f7df9 PM |
1994 | if (err < 0) |
1995 | goto err1; | |
1996 | } else | |
1997 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
1998 | ||
1999 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
2000 | ops = type->select_ops(ctx, |
2001 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
2002 | if (IS_ERR(ops)) { |
2003 | err = PTR_ERR(ops); | |
2004 | goto err1; | |
2005 | } | |
2006 | } else | |
2007 | ops = type->ops; | |
2008 | ||
96518518 PM |
2009 | info->ops = ops; |
2010 | return 0; | |
ef1f7df9 PM |
2011 | |
2012 | err1: | |
2013 | module_put(type->owner); | |
2014 | return err; | |
96518518 PM |
2015 | } |
2016 | ||
2017 | static int nf_tables_newexpr(const struct nft_ctx *ctx, | |
ef1f7df9 | 2018 | const struct nft_expr_info *info, |
96518518 PM |
2019 | struct nft_expr *expr) |
2020 | { | |
2021 | const struct nft_expr_ops *ops = info->ops; | |
2022 | int err; | |
2023 | ||
2024 | expr->ops = ops; | |
2025 | if (ops->init) { | |
ef1f7df9 | 2026 | err = ops->init(ctx, expr, (const struct nlattr **)info->tb); |
96518518 PM |
2027 | if (err < 0) |
2028 | goto err1; | |
2029 | } | |
2030 | ||
96518518 | 2031 | return 0; |
96518518 PM |
2032 | err1: |
2033 | expr->ops = NULL; | |
2034 | return err; | |
2035 | } | |
2036 | ||
62472bce PM |
2037 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
2038 | struct nft_expr *expr) | |
96518518 PM |
2039 | { |
2040 | if (expr->ops->destroy) | |
62472bce | 2041 | expr->ops->destroy(ctx, expr); |
ef1f7df9 | 2042 | module_put(expr->ops->type->owner); |
96518518 PM |
2043 | } |
2044 | ||
0b2d8a7b PM |
2045 | struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
2046 | const struct nlattr *nla) | |
2047 | { | |
2048 | struct nft_expr_info info; | |
2049 | struct nft_expr *expr; | |
2050 | int err; | |
2051 | ||
2052 | err = nf_tables_expr_parse(ctx, nla, &info); | |
2053 | if (err < 0) | |
2054 | goto err1; | |
2055 | ||
2056 | err = -ENOMEM; | |
2057 | expr = kzalloc(info.ops->size, GFP_KERNEL); | |
2058 | if (expr == NULL) | |
2059 | goto err2; | |
2060 | ||
2061 | err = nf_tables_newexpr(ctx, &info, expr); | |
2062 | if (err < 0) | |
6cafaf47 | 2063 | goto err3; |
0b2d8a7b PM |
2064 | |
2065 | return expr; | |
6cafaf47 LZ |
2066 | err3: |
2067 | kfree(expr); | |
0b2d8a7b PM |
2068 | err2: |
2069 | module_put(info.ops->type->owner); | |
2070 | err1: | |
2071 | return ERR_PTR(err); | |
2072 | } | |
2073 | ||
2074 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) | |
2075 | { | |
2076 | nf_tables_expr_destroy(ctx, expr); | |
2077 | kfree(expr); | |
2078 | } | |
2079 | ||
96518518 PM |
2080 | /* |
2081 | * Rules | |
2082 | */ | |
2083 | ||
cac20fcd PNA |
2084 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
2085 | u64 handle) | |
96518518 PM |
2086 | { |
2087 | struct nft_rule *rule; | |
2088 | ||
2089 | // FIXME: this sucks | |
d9adf22a | 2090 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
2091 | if (handle == rule->handle) |
2092 | return rule; | |
2093 | } | |
2094 | ||
2095 | return ERR_PTR(-ENOENT); | |
2096 | } | |
2097 | ||
cac20fcd PNA |
2098 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
2099 | const struct nlattr *nla) | |
96518518 PM |
2100 | { |
2101 | if (nla == NULL) | |
2102 | return ERR_PTR(-EINVAL); | |
2103 | ||
cac20fcd | 2104 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
2105 | } |
2106 | ||
2107 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
2108 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
2109 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
2110 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
2111 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
2112 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
2113 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 2114 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 2115 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
2116 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
2117 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 2118 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
96518518 PM |
2119 | }; |
2120 | ||
84d7fce6 PNA |
2121 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
2122 | u32 portid, u32 seq, int event, | |
2123 | u32 flags, int family, | |
96518518 PM |
2124 | const struct nft_table *table, |
2125 | const struct nft_chain *chain, | |
2126 | const struct nft_rule *rule) | |
2127 | { | |
2128 | struct nlmsghdr *nlh; | |
2129 | struct nfgenmsg *nfmsg; | |
2130 | const struct nft_expr *expr, *next; | |
2131 | struct nlattr *list; | |
5e948466 | 2132 | const struct nft_rule *prule; |
dedb67c4 | 2133 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 2134 | |
dedb67c4 | 2135 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags); |
96518518 PM |
2136 | if (nlh == NULL) |
2137 | goto nla_put_failure; | |
2138 | ||
2139 | nfmsg = nlmsg_data(nlh); | |
2140 | nfmsg->nfgen_family = family; | |
2141 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 2142 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 PM |
2143 | |
2144 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) | |
2145 | goto nla_put_failure; | |
2146 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
2147 | goto nla_put_failure; | |
b46f6ded ND |
2148 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
2149 | NFTA_RULE_PAD)) | |
96518518 PM |
2150 | goto nla_put_failure; |
2151 | ||
5e948466 | 2152 | if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) { |
cbbb40e2 | 2153 | prule = list_prev_entry(rule, list); |
5e948466 | 2154 | if (nla_put_be64(skb, NFTA_RULE_POSITION, |
b46f6ded ND |
2155 | cpu_to_be64(prule->handle), |
2156 | NFTA_RULE_PAD)) | |
5e948466 EL |
2157 | goto nla_put_failure; |
2158 | } | |
2159 | ||
96518518 PM |
2160 | list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS); |
2161 | if (list == NULL) | |
2162 | goto nla_put_failure; | |
2163 | nft_rule_for_each_expr(expr, next, rule) { | |
0b2d8a7b | 2164 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
96518518 | 2165 | goto nla_put_failure; |
96518518 PM |
2166 | } |
2167 | nla_nest_end(skb, list); | |
2168 | ||
86f1ec32 PM |
2169 | if (rule->udata) { |
2170 | struct nft_userdata *udata = nft_userdata(rule); | |
2171 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
2172 | udata->data) < 0) | |
2173 | goto nla_put_failure; | |
2174 | } | |
0768b3b3 | 2175 | |
053c095a JB |
2176 | nlmsg_end(skb, nlh); |
2177 | return 0; | |
96518518 PM |
2178 | |
2179 | nla_put_failure: | |
2180 | nlmsg_trim(skb, nlh); | |
2181 | return -1; | |
2182 | } | |
2183 | ||
25e94a99 PNA |
2184 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
2185 | const struct nft_rule *rule, int event) | |
96518518 PM |
2186 | { |
2187 | struct sk_buff *skb; | |
96518518 PM |
2188 | int err; |
2189 | ||
128ad332 PNA |
2190 | if (!ctx->report && |
2191 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 2192 | return; |
96518518 | 2193 | |
96518518 PM |
2194 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
2195 | if (skb == NULL) | |
2196 | goto err; | |
2197 | ||
84d7fce6 | 2198 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 2199 | event, 0, ctx->family, ctx->table, |
35151d84 | 2200 | ctx->chain, rule); |
96518518 PM |
2201 | if (err < 0) { |
2202 | kfree_skb(skb); | |
2203 | goto err; | |
2204 | } | |
2205 | ||
25e94a99 PNA |
2206 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
2207 | ctx->report, GFP_KERNEL); | |
2208 | return; | |
96518518 | 2209 | err: |
25e94a99 | 2210 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
2211 | } |
2212 | ||
6e1f760e | 2213 | struct nft_rule_dump_ctx { |
e46abbcc | 2214 | char *table; |
b7263e07 | 2215 | char *chain; |
6e1f760e PNA |
2216 | }; |
2217 | ||
96518518 PM |
2218 | static int nf_tables_dump_rules(struct sk_buff *skb, |
2219 | struct netlink_callback *cb) | |
2220 | { | |
2221 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 2222 | const struct nft_rule_dump_ctx *ctx = cb->data; |
96518518 PM |
2223 | const struct nft_table *table; |
2224 | const struct nft_chain *chain; | |
2225 | const struct nft_rule *rule; | |
2226 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 2227 | struct net *net = sock_net(skb->sk); |
96518518 PM |
2228 | int family = nfmsg->nfgen_family; |
2229 | ||
e688a7f8 | 2230 | rcu_read_lock(); |
38e029f1 PNA |
2231 | cb->seq = net->nft.base_seq; |
2232 | ||
36596dad | 2233 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 2234 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
2235 | continue; |
2236 | ||
2237 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
2238 | continue; |
2239 | ||
36596dad PNA |
2240 | list_for_each_entry_rcu(chain, &table->chains, list) { |
2241 | if (ctx && ctx->chain && | |
2242 | strcmp(ctx->chain, chain->name) != 0) | |
6e1f760e PNA |
2243 | continue; |
2244 | ||
36596dad PNA |
2245 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
2246 | if (!nft_is_active(net, rule)) | |
2247 | goto cont; | |
2248 | if (idx < s_idx) | |
2249 | goto cont; | |
2250 | if (idx > s_idx) | |
2251 | memset(&cb->args[1], 0, | |
2252 | sizeof(cb->args) - sizeof(cb->args[0])); | |
2253 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, | |
2254 | cb->nlh->nlmsg_seq, | |
2255 | NFT_MSG_NEWRULE, | |
2256 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 2257 | table->family, |
36596dad PNA |
2258 | table, chain, rule) < 0) |
2259 | goto done; | |
2260 | ||
2261 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 2262 | cont: |
36596dad | 2263 | idx++; |
96518518 PM |
2264 | } |
2265 | } | |
2266 | } | |
2267 | done: | |
e688a7f8 PNA |
2268 | rcu_read_unlock(); |
2269 | ||
96518518 PM |
2270 | cb->args[0] = idx; |
2271 | return skb->len; | |
2272 | } | |
2273 | ||
90fd131a FW |
2274 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
2275 | { | |
2276 | const struct nlattr * const *nla = cb->data; | |
2277 | struct nft_rule_dump_ctx *ctx = NULL; | |
2278 | ||
2279 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
2280 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
2281 | if (!ctx) | |
2282 | return -ENOMEM; | |
2283 | ||
2284 | if (nla[NFTA_RULE_TABLE]) { | |
2285 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
2286 | GFP_ATOMIC); | |
2287 | if (!ctx->table) { | |
2288 | kfree(ctx); | |
2289 | return -ENOMEM; | |
2290 | } | |
2291 | } | |
2292 | if (nla[NFTA_RULE_CHAIN]) { | |
2293 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
2294 | GFP_ATOMIC); | |
2295 | if (!ctx->chain) { | |
2296 | kfree(ctx->table); | |
2297 | kfree(ctx); | |
2298 | return -ENOMEM; | |
2299 | } | |
2300 | } | |
2301 | } | |
2302 | ||
2303 | cb->data = ctx; | |
2304 | return 0; | |
2305 | } | |
2306 | ||
6e1f760e PNA |
2307 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
2308 | { | |
e46abbcc PS |
2309 | struct nft_rule_dump_ctx *ctx = cb->data; |
2310 | ||
2311 | if (ctx) { | |
2312 | kfree(ctx->table); | |
b7263e07 | 2313 | kfree(ctx->chain); |
e46abbcc PS |
2314 | kfree(ctx); |
2315 | } | |
6e1f760e PNA |
2316 | return 0; |
2317 | } | |
2318 | ||
d9adf22a | 2319 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
2320 | static int nf_tables_getrule(struct net *net, struct sock *nlsk, |
2321 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2322 | const struct nlattr * const nla[], |
2323 | struct netlink_ext_ack *extack) | |
96518518 PM |
2324 | { |
2325 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2326 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
2327 | const struct nft_chain *chain; |
2328 | const struct nft_rule *rule; | |
1b2470e5 | 2329 | struct nft_table *table; |
96518518 PM |
2330 | struct sk_buff *skb2; |
2331 | int family = nfmsg->nfgen_family; | |
2332 | int err; | |
2333 | ||
2334 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
2335 | struct netlink_dump_control c = { | |
90fd131a | 2336 | .start= nf_tables_dump_rules_start, |
96518518 | 2337 | .dump = nf_tables_dump_rules, |
6e1f760e | 2338 | .done = nf_tables_dump_rules_done, |
d9adf22a | 2339 | .module = THIS_MODULE, |
90fd131a | 2340 | .data = (void *)nla, |
96518518 | 2341 | }; |
6e1f760e | 2342 | |
d9adf22a | 2343 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
96518518 PM |
2344 | } |
2345 | ||
cac20fcd | 2346 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
2347 | if (IS_ERR(table)) { |
2348 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 2349 | return PTR_ERR(table); |
36dd1bcc | 2350 | } |
96518518 | 2351 | |
cac20fcd | 2352 | chain = nft_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
2353 | if (IS_ERR(chain)) { |
2354 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 2355 | return PTR_ERR(chain); |
36dd1bcc | 2356 | } |
96518518 | 2357 | |
cac20fcd | 2358 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
2359 | if (IS_ERR(rule)) { |
2360 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 2361 | return PTR_ERR(rule); |
36dd1bcc | 2362 | } |
96518518 | 2363 | |
d9adf22a | 2364 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
2365 | if (!skb2) |
2366 | return -ENOMEM; | |
2367 | ||
84d7fce6 | 2368 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
2369 | nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
2370 | family, table, chain, rule); | |
2371 | if (err < 0) | |
2372 | goto err; | |
2373 | ||
2374 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
2375 | ||
2376 | err: | |
2377 | kfree_skb(skb2); | |
2378 | return err; | |
2379 | } | |
2380 | ||
62472bce PM |
2381 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
2382 | struct nft_rule *rule) | |
96518518 | 2383 | { |
96518518 PM |
2384 | struct nft_expr *expr; |
2385 | ||
2386 | /* | |
2387 | * Careful: some expressions might not be initialized in case this | |
2388 | * is called on error from nf_tables_newrule(). | |
2389 | */ | |
2390 | expr = nft_expr_first(rule); | |
3e38df13 | 2391 | while (expr != nft_expr_last(rule) && expr->ops) { |
62472bce | 2392 | nf_tables_expr_destroy(ctx, expr); |
96518518 PM |
2393 | expr = nft_expr_next(expr); |
2394 | } | |
2395 | kfree(rule); | |
2396 | } | |
2397 | ||
bb7b40ae PNA |
2398 | static void nf_tables_rule_release(const struct nft_ctx *ctx, |
2399 | struct nft_rule *rule) | |
2400 | { | |
2401 | nft_rule_expr_deactivate(ctx, rule); | |
2402 | nf_tables_rule_destroy(ctx, rule); | |
2403 | } | |
2404 | ||
a654de8f PNA |
2405 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
2406 | { | |
2407 | struct nft_expr *expr, *last; | |
2408 | const struct nft_data *data; | |
2409 | struct nft_rule *rule; | |
2410 | int err; | |
2411 | ||
26b2f552 TY |
2412 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
2413 | return -EMLINK; | |
2414 | ||
a654de8f PNA |
2415 | list_for_each_entry(rule, &chain->rules, list) { |
2416 | if (!nft_is_active_next(ctx->net, rule)) | |
2417 | continue; | |
2418 | ||
2419 | nft_rule_for_each_expr(expr, last, rule) { | |
2420 | if (!expr->ops->validate) | |
2421 | continue; | |
2422 | ||
2423 | err = expr->ops->validate(ctx, expr, &data); | |
2424 | if (err < 0) | |
2425 | return err; | |
2426 | } | |
2427 | } | |
2428 | ||
2429 | return 0; | |
2430 | } | |
2431 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
2432 | ||
2433 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
2434 | { | |
2435 | struct nft_chain *chain; | |
2436 | struct nft_ctx ctx = { | |
2437 | .net = net, | |
2438 | .family = table->family, | |
2439 | }; | |
2440 | int err; | |
2441 | ||
2442 | list_for_each_entry(chain, &table->chains, list) { | |
2443 | if (!nft_is_base_chain(chain)) | |
2444 | continue; | |
2445 | ||
2446 | ctx.chain = chain; | |
2447 | err = nft_chain_validate(&ctx, chain); | |
2448 | if (err < 0) | |
2449 | return err; | |
2450 | } | |
2451 | ||
2452 | return 0; | |
2453 | } | |
2454 | ||
1081d11b PNA |
2455 | #define NFT_RULE_MAXEXPRS 128 |
2456 | ||
2457 | static struct nft_expr_info *info; | |
2458 | ||
633c9a84 PNA |
2459 | static int nf_tables_newrule(struct net *net, struct sock *nlsk, |
2460 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2461 | const struct nlattr * const nla[], |
2462 | struct netlink_ext_ack *extack) | |
96518518 PM |
2463 | { |
2464 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2465 | u8 genmask = nft_genmask_next(net); |
98319cb9 | 2466 | int family = nfmsg->nfgen_family; |
96518518 PM |
2467 | struct nft_table *table; |
2468 | struct nft_chain *chain; | |
2469 | struct nft_rule *rule, *old_rule = NULL; | |
86f1ec32 | 2470 | struct nft_userdata *udata; |
1081d11b | 2471 | struct nft_trans *trans = NULL; |
96518518 PM |
2472 | struct nft_expr *expr; |
2473 | struct nft_ctx ctx; | |
2474 | struct nlattr *tmp; | |
86f1ec32 | 2475 | unsigned int size, i, n, ulen = 0, usize = 0; |
96518518 PM |
2476 | int err, rem; |
2477 | bool create; | |
5e948466 | 2478 | u64 handle, pos_handle; |
96518518 PM |
2479 | |
2480 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; | |
2481 | ||
cac20fcd | 2482 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
2483 | if (IS_ERR(table)) { |
2484 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 2485 | return PTR_ERR(table); |
36dd1bcc | 2486 | } |
96518518 | 2487 | |
cac20fcd | 2488 | chain = nft_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
2489 | if (IS_ERR(chain)) { |
2490 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 2491 | return PTR_ERR(chain); |
36dd1bcc | 2492 | } |
96518518 PM |
2493 | |
2494 | if (nla[NFTA_RULE_HANDLE]) { | |
2495 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 2496 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
2497 | if (IS_ERR(rule)) { |
2498 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 2499 | return PTR_ERR(rule); |
36dd1bcc | 2500 | } |
96518518 | 2501 | |
36dd1bcc PNA |
2502 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
2503 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 2504 | return -EEXIST; |
36dd1bcc | 2505 | } |
96518518 PM |
2506 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
2507 | old_rule = rule; | |
2508 | else | |
2509 | return -EOPNOTSUPP; | |
2510 | } else { | |
2511 | if (!create || nlh->nlmsg_flags & NLM_F_REPLACE) | |
2512 | return -EINVAL; | |
2513 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
2514 | |
2515 | if (chain->use == UINT_MAX) | |
2516 | return -EOVERFLOW; | |
96518518 PM |
2517 | } |
2518 | ||
5e948466 EL |
2519 | if (nla[NFTA_RULE_POSITION]) { |
2520 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
2521 | return -EOPNOTSUPP; | |
2522 | ||
2523 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
cac20fcd | 2524 | old_rule = __nft_rule_lookup(chain, pos_handle); |
36dd1bcc PNA |
2525 | if (IS_ERR(old_rule)) { |
2526 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
5e948466 | 2527 | return PTR_ERR(old_rule); |
36dd1bcc | 2528 | } |
5e948466 EL |
2529 | } |
2530 | ||
98319cb9 | 2531 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0ca743a5 | 2532 | |
96518518 PM |
2533 | n = 0; |
2534 | size = 0; | |
2535 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
2536 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { | |
2537 | err = -EINVAL; | |
2538 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
2539 | goto err1; | |
2540 | if (n == NFT_RULE_MAXEXPRS) | |
2541 | goto err1; | |
0ca743a5 | 2542 | err = nf_tables_expr_parse(&ctx, tmp, &info[n]); |
96518518 PM |
2543 | if (err < 0) |
2544 | goto err1; | |
2545 | size += info[n].ops->size; | |
2546 | n++; | |
2547 | } | |
2548 | } | |
9889840f PM |
2549 | /* Check for overflow of dlen field */ |
2550 | err = -EFBIG; | |
2551 | if (size >= 1 << 12) | |
2552 | goto err1; | |
96518518 | 2553 | |
86f1ec32 | 2554 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 2555 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
2556 | if (ulen > 0) |
2557 | usize = sizeof(struct nft_userdata) + ulen; | |
2558 | } | |
0768b3b3 | 2559 | |
96518518 | 2560 | err = -ENOMEM; |
86f1ec32 | 2561 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL); |
96518518 PM |
2562 | if (rule == NULL) |
2563 | goto err1; | |
2564 | ||
889f7ee7 | 2565 | nft_activate_next(net, rule); |
0628b123 | 2566 | |
96518518 PM |
2567 | rule->handle = handle; |
2568 | rule->dlen = size; | |
86f1ec32 | 2569 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 2570 | |
86f1ec32 PM |
2571 | if (ulen) { |
2572 | udata = nft_userdata(rule); | |
2573 | udata->len = ulen - 1; | |
2574 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
2575 | } | |
96518518 | 2576 | |
96518518 PM |
2577 | expr = nft_expr_first(rule); |
2578 | for (i = 0; i < n; i++) { | |
2579 | err = nf_tables_newexpr(&ctx, &info[i], expr); | |
2580 | if (err < 0) | |
2581 | goto err2; | |
a654de8f PNA |
2582 | |
2583 | if (info[i].ops->validate) | |
2584 | nft_validate_state_update(net, NFT_VALIDATE_NEED); | |
2585 | ||
ef1f7df9 | 2586 | info[i].ops = NULL; |
96518518 PM |
2587 | expr = nft_expr_next(expr); |
2588 | } | |
2589 | ||
96518518 | 2590 | if (nlh->nlmsg_flags & NLM_F_REPLACE) { |
569ccae6 | 2591 | if (!nft_is_active_next(net, old_rule)) { |
0628b123 PNA |
2592 | err = -ENOENT; |
2593 | goto err2; | |
2594 | } | |
569ccae6 FW |
2595 | trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE, |
2596 | old_rule); | |
2597 | if (trans == NULL) { | |
2598 | err = -ENOMEM; | |
2599 | goto err2; | |
2600 | } | |
2601 | nft_deactivate_next(net, old_rule); | |
2602 | chain->use--; | |
96518518 | 2603 | |
569ccae6 FW |
2604 | if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) { |
2605 | err = -ENOMEM; | |
2606 | goto err2; | |
2607 | } | |
2608 | ||
2609 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
2610 | } else { | |
2611 | if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) { | |
2612 | err = -ENOMEM; | |
2613 | goto err2; | |
2614 | } | |
2615 | ||
2616 | if (nlh->nlmsg_flags & NLM_F_APPEND) { | |
2617 | if (old_rule) | |
2618 | list_add_rcu(&rule->list, &old_rule->list); | |
2619 | else | |
2620 | list_add_tail_rcu(&rule->list, &chain->rules); | |
2621 | } else { | |
2622 | if (old_rule) | |
2623 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
2624 | else | |
2625 | list_add_rcu(&rule->list, &chain->rules); | |
2626 | } | |
0628b123 | 2627 | } |
4fefee57 | 2628 | chain->use++; |
96518518 | 2629 | |
a654de8f PNA |
2630 | if (net->nft.validate_state == NFT_VALIDATE_DO) |
2631 | return nft_table_validate(net, table); | |
2632 | ||
2633 | return 0; | |
96518518 | 2634 | err2: |
bb7b40ae | 2635 | nf_tables_rule_release(&ctx, rule); |
96518518 PM |
2636 | err1: |
2637 | for (i = 0; i < n; i++) { | |
2638 | if (info[i].ops != NULL) | |
ef1f7df9 | 2639 | module_put(info[i].ops->type->owner); |
96518518 PM |
2640 | } |
2641 | return err; | |
2642 | } | |
2643 | ||
1a94e38d PNA |
2644 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
2645 | const struct nlattr *nla) | |
2646 | { | |
2647 | u32 id = ntohl(nla_get_be32(nla)); | |
2648 | struct nft_trans *trans; | |
2649 | ||
2650 | list_for_each_entry(trans, &net->nft.commit_list, list) { | |
2651 | struct nft_rule *rule = nft_trans_rule(trans); | |
2652 | ||
2653 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
2654 | id == nft_trans_rule_id(trans)) | |
2655 | return rule; | |
2656 | } | |
2657 | return ERR_PTR(-ENOENT); | |
2658 | } | |
2659 | ||
633c9a84 PNA |
2660 | static int nf_tables_delrule(struct net *net, struct sock *nlsk, |
2661 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2662 | const struct nlattr * const nla[], |
2663 | struct netlink_ext_ack *extack) | |
96518518 PM |
2664 | { |
2665 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2666 | u8 genmask = nft_genmask_next(net); |
7c95f6d8 | 2667 | struct nft_table *table; |
cf9dc09d PNA |
2668 | struct nft_chain *chain = NULL; |
2669 | struct nft_rule *rule; | |
0628b123 PNA |
2670 | int family = nfmsg->nfgen_family, err = 0; |
2671 | struct nft_ctx ctx; | |
96518518 | 2672 | |
cac20fcd | 2673 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
2674 | if (IS_ERR(table)) { |
2675 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 2676 | return PTR_ERR(table); |
36dd1bcc | 2677 | } |
96518518 | 2678 | |
cf9dc09d | 2679 | if (nla[NFTA_RULE_CHAIN]) { |
cac20fcd | 2680 | chain = nft_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
2681 | if (IS_ERR(chain)) { |
2682 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
cf9dc09d | 2683 | return PTR_ERR(chain); |
36dd1bcc | 2684 | } |
cf9dc09d | 2685 | } |
96518518 | 2686 | |
98319cb9 | 2687 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0628b123 | 2688 | |
cf9dc09d PNA |
2689 | if (chain) { |
2690 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 2691 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
2692 | if (IS_ERR(rule)) { |
2693 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
cf9dc09d | 2694 | return PTR_ERR(rule); |
36dd1bcc | 2695 | } |
96518518 | 2696 | |
1a94e38d PNA |
2697 | err = nft_delrule(&ctx, rule); |
2698 | } else if (nla[NFTA_RULE_ID]) { | |
2699 | rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]); | |
36dd1bcc PNA |
2700 | if (IS_ERR(rule)) { |
2701 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 2702 | return PTR_ERR(rule); |
36dd1bcc | 2703 | } |
1a94e38d | 2704 | |
5e266fe7 | 2705 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 2706 | } else { |
ce24b721 | 2707 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
2708 | } |
2709 | } else { | |
2710 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
2711 | if (!nft_is_active_next(net, chain)) |
2712 | continue; | |
2713 | ||
cf9dc09d | 2714 | ctx.chain = chain; |
ce24b721 | 2715 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
2716 | if (err < 0) |
2717 | break; | |
2718 | } | |
2719 | } | |
2720 | ||
2721 | return err; | |
2722 | } | |
2723 | ||
20a69341 PM |
2724 | /* |
2725 | * Sets | |
2726 | */ | |
2727 | ||
2b664957 | 2728 | static LIST_HEAD(nf_tables_set_types); |
20a69341 | 2729 | |
2b664957 | 2730 | int nft_register_set(struct nft_set_type *type) |
20a69341 PM |
2731 | { |
2732 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
2b664957 | 2733 | list_add_tail_rcu(&type->list, &nf_tables_set_types); |
20a69341 PM |
2734 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2735 | return 0; | |
2736 | } | |
2737 | EXPORT_SYMBOL_GPL(nft_register_set); | |
2738 | ||
2b664957 | 2739 | void nft_unregister_set(struct nft_set_type *type) |
20a69341 PM |
2740 | { |
2741 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
2b664957 | 2742 | list_del_rcu(&type->list); |
20a69341 PM |
2743 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2744 | } | |
2745 | EXPORT_SYMBOL_GPL(nft_unregister_set); | |
2746 | ||
2b664957 | 2747 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
2748 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
2749 | NFT_SET_EVAL) | |
2b664957 | 2750 | |
71cc0873 | 2751 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 2752 | { |
71cc0873 | 2753 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
2754 | } |
2755 | ||
c50b960c PM |
2756 | /* |
2757 | * Select a set implementation based on the data characteristics and the | |
2758 | * given policy. The total memory use might not be known if no size is | |
2759 | * given, in that case the amount of memory per element is used. | |
2760 | */ | |
2761 | static const struct nft_set_ops * | |
2b664957 PNA |
2762 | nft_select_set_ops(const struct nft_ctx *ctx, |
2763 | const struct nlattr * const nla[], | |
c50b960c PM |
2764 | const struct nft_set_desc *desc, |
2765 | enum nft_set_policies policy) | |
20a69341 | 2766 | { |
c50b960c PM |
2767 | const struct nft_set_ops *ops, *bops; |
2768 | struct nft_set_estimate est, best; | |
2b664957 PNA |
2769 | const struct nft_set_type *type; |
2770 | u32 flags = 0; | |
20a69341 PM |
2771 | |
2772 | #ifdef CONFIG_MODULES | |
2b664957 | 2773 | if (list_empty(&nf_tables_set_types)) { |
20a69341 PM |
2774 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2775 | request_module("nft-set"); | |
2776 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
2b664957 | 2777 | if (!list_empty(&nf_tables_set_types)) |
20a69341 PM |
2778 | return ERR_PTR(-EAGAIN); |
2779 | } | |
2780 | #endif | |
2b664957 PNA |
2781 | if (nla[NFTA_SET_FLAGS] != NULL) |
2782 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 2783 | |
55af753c PNA |
2784 | bops = NULL; |
2785 | best.size = ~0; | |
2786 | best.lookup = ~0; | |
0b5a7874 | 2787 | best.space = ~0; |
c50b960c | 2788 | |
2b664957 | 2789 | list_for_each_entry(type, &nf_tables_set_types, list) { |
71cc0873 | 2790 | ops = &type->ops; |
2b664957 | 2791 | |
71cc0873 | 2792 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 2793 | continue; |
2b664957 | 2794 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
2795 | continue; |
2796 | ||
2797 | switch (policy) { | |
2798 | case NFT_SET_POL_PERFORMANCE: | |
55af753c | 2799 | if (est.lookup < best.lookup) |
c50b960c | 2800 | break; |
644e334e PNA |
2801 | if (est.lookup == best.lookup && |
2802 | est.space < best.space) | |
2803 | break; | |
c50b960c PM |
2804 | continue; |
2805 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
2806 | if (!desc->size) { |
2807 | if (est.space < best.space) | |
2808 | break; | |
2809 | if (est.space == best.space && | |
2810 | est.lookup < best.lookup) | |
2811 | break; | |
4f2921ca | 2812 | } else if (est.size < best.size || !bops) { |
c50b960c | 2813 | break; |
0b5a7874 | 2814 | } |
c50b960c PM |
2815 | continue; |
2816 | default: | |
2817 | break; | |
2818 | } | |
2819 | ||
2b664957 | 2820 | if (!try_module_get(type->owner)) |
20a69341 | 2821 | continue; |
c50b960c | 2822 | if (bops != NULL) |
71cc0873 | 2823 | module_put(to_set_type(bops)->owner); |
c50b960c PM |
2824 | |
2825 | bops = ops; | |
2826 | best = est; | |
20a69341 PM |
2827 | } |
2828 | ||
c50b960c PM |
2829 | if (bops != NULL) |
2830 | return bops; | |
2831 | ||
20a69341 PM |
2832 | return ERR_PTR(-EOPNOTSUPP); |
2833 | } | |
2834 | ||
2835 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
2836 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
2837 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 2838 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 2839 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
2840 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
2841 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
2842 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
2843 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
2844 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
2845 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
2846 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 2847 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
2848 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
2849 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
2850 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
2851 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 2852 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 2853 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
c50b960c PM |
2854 | }; |
2855 | ||
2856 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
2857 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
20a69341 PM |
2858 | }; |
2859 | ||
633c9a84 | 2860 | static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net, |
20a69341 PM |
2861 | const struct sk_buff *skb, |
2862 | const struct nlmsghdr *nlh, | |
f2a6d766 | 2863 | const struct nlattr * const nla[], |
36dd1bcc | 2864 | struct netlink_ext_ack *extack, |
f2a6d766 | 2865 | u8 genmask) |
20a69341 PM |
2866 | { |
2867 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
98319cb9 | 2868 | int family = nfmsg->nfgen_family; |
7c95f6d8 | 2869 | struct nft_table *table = NULL; |
20a69341 | 2870 | |
20a69341 | 2871 | if (nla[NFTA_SET_TABLE] != NULL) { |
cac20fcd PNA |
2872 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
2873 | genmask); | |
36dd1bcc PNA |
2874 | if (IS_ERR(table)) { |
2875 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 2876 | return PTR_ERR(table); |
36dd1bcc | 2877 | } |
20a69341 PM |
2878 | } |
2879 | ||
98319cb9 | 2880 | nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 PM |
2881 | return 0; |
2882 | } | |
2883 | ||
cac20fcd PNA |
2884 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
2885 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
2886 | { |
2887 | struct nft_set *set; | |
2888 | ||
2889 | if (nla == NULL) | |
2890 | return ERR_PTR(-EINVAL); | |
2891 | ||
d9adf22a | 2892 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
2893 | if (!nla_strcmp(nla, set->name) && |
2894 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
2895 | return set; |
2896 | } | |
2897 | return ERR_PTR(-ENOENT); | |
2898 | } | |
2899 | ||
cac20fcd PNA |
2900 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
2901 | const struct nlattr *nla, | |
2902 | u8 genmask) | |
3ecbfd65 HS |
2903 | { |
2904 | struct nft_set *set; | |
2905 | ||
3ecbfd65 HS |
2906 | list_for_each_entry(set, &table->sets, list) { |
2907 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
2908 | nft_active_genmask(set, genmask)) | |
2909 | return set; | |
2910 | } | |
2911 | return ERR_PTR(-ENOENT); | |
2912 | } | |
2913 | ||
cac20fcd PNA |
2914 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
2915 | const struct nlattr *nla, u8 genmask) | |
958bee14 PNA |
2916 | { |
2917 | struct nft_trans *trans; | |
2918 | u32 id = ntohl(nla_get_be32(nla)); | |
2919 | ||
2920 | list_for_each_entry(trans, &net->nft.commit_list, list) { | |
9c7f96fd AK |
2921 | if (trans->msg_type == NFT_MSG_NEWSET) { |
2922 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 2923 | |
9c7f96fd AK |
2924 | if (id == nft_trans_set_id(trans) && |
2925 | nft_active_genmask(set, genmask)) | |
2926 | return set; | |
2927 | } | |
958bee14 PNA |
2928 | } |
2929 | return ERR_PTR(-ENOENT); | |
2930 | } | |
c7a72e3f | 2931 | |
10659cba PNA |
2932 | struct nft_set *nft_set_lookup_global(const struct net *net, |
2933 | const struct nft_table *table, | |
2934 | const struct nlattr *nla_set_name, | |
2935 | const struct nlattr *nla_set_id, | |
2936 | u8 genmask) | |
c7a72e3f PNA |
2937 | { |
2938 | struct nft_set *set; | |
2939 | ||
cac20fcd | 2940 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
2941 | if (IS_ERR(set)) { |
2942 | if (!nla_set_id) | |
2943 | return set; | |
2944 | ||
cac20fcd | 2945 | set = nft_set_lookup_byid(net, nla_set_id, genmask); |
c7a72e3f PNA |
2946 | } |
2947 | return set; | |
2948 | } | |
10659cba | 2949 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 2950 | |
20a69341 PM |
2951 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
2952 | const char *name) | |
2953 | { | |
2954 | const struct nft_set *i; | |
2955 | const char *p; | |
2956 | unsigned long *inuse; | |
60eb1894 | 2957 | unsigned int n = 0, min = 0; |
20a69341 | 2958 | |
38745490 | 2959 | p = strchr(name, '%'); |
20a69341 PM |
2960 | if (p != NULL) { |
2961 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
2962 | return -EINVAL; | |
2963 | ||
2964 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
2965 | if (inuse == NULL) | |
2966 | return -ENOMEM; | |
60eb1894 | 2967 | cont: |
20a69341 | 2968 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
2969 | int tmp; |
2970 | ||
37a9cc52 PNA |
2971 | if (!nft_is_active_next(ctx->net, set)) |
2972 | continue; | |
14662917 | 2973 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 2974 | continue; |
60eb1894 | 2975 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 2976 | continue; |
14662917 | 2977 | |
60eb1894 | 2978 | set_bit(tmp - min, inuse); |
20a69341 PM |
2979 | } |
2980 | ||
53b70287 | 2981 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
2982 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
2983 | min += BITS_PER_BYTE * PAGE_SIZE; | |
2984 | memset(inuse, 0, PAGE_SIZE); | |
2985 | goto cont; | |
2986 | } | |
20a69341 PM |
2987 | free_page((unsigned long)inuse); |
2988 | } | |
2989 | ||
38745490 PS |
2990 | set->name = kasprintf(GFP_KERNEL, name, min + n); |
2991 | if (!set->name) | |
2992 | return -ENOMEM; | |
2993 | ||
20a69341 | 2994 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
2995 | if (!nft_is_active_next(ctx->net, i)) |
2996 | continue; | |
e63aaaa6 AY |
2997 | if (!strcmp(set->name, i->name)) { |
2998 | kfree(set->name); | |
20a69341 | 2999 | return -ENFILE; |
e63aaaa6 | 3000 | } |
20a69341 PM |
3001 | } |
3002 | return 0; | |
3003 | } | |
3004 | ||
8e1102d5 FW |
3005 | static int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
3006 | { | |
3007 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
3008 | u64 max = (u64)(~((u64)0)); | |
3009 | ||
3010 | max = div_u64(max, NSEC_PER_MSEC); | |
3011 | if (ms >= max) | |
3012 | return -ERANGE; | |
3013 | ||
3014 | ms *= NSEC_PER_MSEC; | |
3015 | *result = nsecs_to_jiffies64(ms); | |
3016 | return 0; | |
3017 | } | |
3018 | ||
d6501de8 | 3019 | static __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 FW |
3020 | { |
3021 | u64 ms = jiffies64_to_nsecs(input); | |
3022 | ||
3023 | return cpu_to_be64(div_u64(ms, NSEC_PER_MSEC)); | |
3024 | } | |
3025 | ||
20a69341 PM |
3026 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
3027 | const struct nft_set *set, u16 event, u16 flags) | |
3028 | { | |
3029 | struct nfgenmsg *nfmsg; | |
3030 | struct nlmsghdr *nlh; | |
c50b960c | 3031 | struct nlattr *desc; |
128ad332 PNA |
3032 | u32 portid = ctx->portid; |
3033 | u32 seq = ctx->seq; | |
20a69341 | 3034 | |
dedb67c4 | 3035 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
20a69341 PM |
3036 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
3037 | flags); | |
3038 | if (nlh == NULL) | |
3039 | goto nla_put_failure; | |
3040 | ||
3041 | nfmsg = nlmsg_data(nlh); | |
36596dad | 3042 | nfmsg->nfgen_family = ctx->family; |
20a69341 | 3043 | nfmsg->version = NFNETLINK_V0; |
84d7fce6 | 3044 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
20a69341 PM |
3045 | |
3046 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) | |
3047 | goto nla_put_failure; | |
3048 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
3049 | goto nla_put_failure; | |
3ecbfd65 HS |
3050 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
3051 | NFTA_SET_PAD)) | |
3052 | goto nla_put_failure; | |
20a69341 PM |
3053 | if (set->flags != 0) |
3054 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
3055 | goto nla_put_failure; | |
3056 | ||
3057 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
3058 | goto nla_put_failure; | |
3059 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
3060 | goto nla_put_failure; | |
3061 | if (set->flags & NFT_SET_MAP) { | |
3062 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
3063 | goto nla_put_failure; | |
3064 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
3065 | goto nla_put_failure; | |
3066 | } | |
8aeff920 PNA |
3067 | if (set->flags & NFT_SET_OBJECT && |
3068 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
3069 | goto nla_put_failure; | |
20a69341 | 3070 | |
761da293 | 3071 | if (set->timeout && |
d3e2a111 | 3072 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
8e1102d5 | 3073 | nf_jiffies64_to_msecs(set->timeout), |
b46f6ded | 3074 | NFTA_SET_PAD)) |
761da293 PM |
3075 | goto nla_put_failure; |
3076 | if (set->gc_int && | |
3077 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) | |
3078 | goto nla_put_failure; | |
3079 | ||
9363dc4b AB |
3080 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
3081 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
3082 | goto nla_put_failure; | |
3083 | } | |
3084 | ||
e6d8ecac CFG |
3085 | if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) |
3086 | goto nla_put_failure; | |
3087 | ||
c50b960c PM |
3088 | desc = nla_nest_start(skb, NFTA_SET_DESC); |
3089 | if (desc == NULL) | |
3090 | goto nla_put_failure; | |
3091 | if (set->size && | |
3092 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
3093 | goto nla_put_failure; | |
3094 | nla_nest_end(skb, desc); | |
3095 | ||
053c095a JB |
3096 | nlmsg_end(skb, nlh); |
3097 | return 0; | |
20a69341 PM |
3098 | |
3099 | nla_put_failure: | |
3100 | nlmsg_trim(skb, nlh); | |
3101 | return -1; | |
3102 | } | |
3103 | ||
25e94a99 PNA |
3104 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
3105 | const struct nft_set *set, int event, | |
3106 | gfp_t gfp_flags) | |
20a69341 PM |
3107 | { |
3108 | struct sk_buff *skb; | |
128ad332 | 3109 | u32 portid = ctx->portid; |
20a69341 PM |
3110 | int err; |
3111 | ||
128ad332 PNA |
3112 | if (!ctx->report && |
3113 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3114 | return; |
20a69341 | 3115 | |
31f8441c | 3116 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
3117 | if (skb == NULL) |
3118 | goto err; | |
3119 | ||
3120 | err = nf_tables_fill_set(skb, ctx, set, event, 0); | |
3121 | if (err < 0) { | |
3122 | kfree_skb(skb); | |
3123 | goto err; | |
3124 | } | |
3125 | ||
25e94a99 PNA |
3126 | nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, ctx->report, |
3127 | gfp_flags); | |
3128 | return; | |
20a69341 | 3129 | err: |
25e94a99 | 3130 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
3131 | } |
3132 | ||
5b96af77 | 3133 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
3134 | { |
3135 | const struct nft_set *set; | |
3136 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
3137 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
3138 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 3139 | struct nft_ctx *ctx = cb->data, ctx_set; |
c9c8e485 PNA |
3140 | |
3141 | if (cb->args[1]) | |
3142 | return skb->len; | |
3143 | ||
e688a7f8 | 3144 | rcu_read_lock(); |
38e029f1 PNA |
3145 | cb->seq = net->nft.base_seq; |
3146 | ||
36596dad PNA |
3147 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
3148 | if (ctx->family != NFPROTO_UNSPEC && | |
98319cb9 | 3149 | ctx->family != table->family) |
36596dad PNA |
3150 | continue; |
3151 | ||
3152 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
3153 | continue; |
3154 | ||
36596dad PNA |
3155 | if (cur_table) { |
3156 | if (cur_table != table) | |
c9c8e485 PNA |
3157 | continue; |
3158 | ||
36596dad | 3159 | cur_table = NULL; |
c9c8e485 | 3160 | } |
36596dad PNA |
3161 | idx = 0; |
3162 | list_for_each_entry_rcu(set, &table->sets, list) { | |
3163 | if (idx < s_idx) | |
3164 | goto cont; | |
3165 | if (!nft_is_active(net, set)) | |
3166 | goto cont; | |
5b96af77 | 3167 | |
36596dad PNA |
3168 | ctx_set = *ctx; |
3169 | ctx_set.table = table; | |
98319cb9 | 3170 | ctx_set.family = table->family; |
c9c8e485 | 3171 | |
36596dad PNA |
3172 | if (nf_tables_fill_set(skb, &ctx_set, set, |
3173 | NFT_MSG_NEWSET, | |
3174 | NLM_F_MULTI) < 0) { | |
3175 | cb->args[0] = idx; | |
3176 | cb->args[2] = (unsigned long) table; | |
3177 | goto done; | |
c9c8e485 | 3178 | } |
36596dad | 3179 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 3180 | cont: |
36596dad | 3181 | idx++; |
c9c8e485 | 3182 | } |
36596dad PNA |
3183 | if (s_idx) |
3184 | s_idx = 0; | |
c9c8e485 PNA |
3185 | } |
3186 | cb->args[1] = 1; | |
3187 | done: | |
e688a7f8 | 3188 | rcu_read_unlock(); |
c9c8e485 PNA |
3189 | return skb->len; |
3190 | } | |
3191 | ||
90fd131a FW |
3192 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
3193 | { | |
3194 | struct nft_ctx *ctx_dump = NULL; | |
3195 | ||
3196 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
3197 | if (ctx_dump == NULL) | |
3198 | return -ENOMEM; | |
3199 | ||
3200 | cb->data = ctx_dump; | |
3201 | return 0; | |
3202 | } | |
3203 | ||
5b96af77 | 3204 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 3205 | { |
5b96af77 PNA |
3206 | kfree(cb->data); |
3207 | return 0; | |
20a69341 PM |
3208 | } |
3209 | ||
d9adf22a | 3210 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
3211 | static int nf_tables_getset(struct net *net, struct sock *nlsk, |
3212 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3213 | const struct nlattr * const nla[], |
3214 | struct netlink_ext_ack *extack) | |
20a69341 | 3215 | { |
f2a6d766 | 3216 | u8 genmask = nft_genmask_cur(net); |
20a69341 PM |
3217 | const struct nft_set *set; |
3218 | struct nft_ctx ctx; | |
3219 | struct sk_buff *skb2; | |
c9c8e485 | 3220 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
20a69341 PM |
3221 | int err; |
3222 | ||
01cfa0a4 | 3223 | /* Verify existence before starting dump */ |
36dd1bcc PNA |
3224 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack, |
3225 | genmask); | |
20a69341 PM |
3226 | if (err < 0) |
3227 | return err; | |
3228 | ||
3229 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
3230 | struct netlink_dump_control c = { | |
90fd131a | 3231 | .start = nf_tables_dump_sets_start, |
20a69341 | 3232 | .dump = nf_tables_dump_sets, |
5b96af77 | 3233 | .done = nf_tables_dump_sets_done, |
90fd131a | 3234 | .data = &ctx, |
d9adf22a | 3235 | .module = THIS_MODULE, |
20a69341 | 3236 | }; |
5b96af77 | 3237 | |
d9adf22a | 3238 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
20a69341 PM |
3239 | } |
3240 | ||
c9c8e485 PNA |
3241 | /* Only accept unspec with dump */ |
3242 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) | |
3243 | return -EAFNOSUPPORT; | |
eaa2bcd6 PT |
3244 | if (!nla[NFTA_SET_TABLE]) |
3245 | return -EINVAL; | |
c9c8e485 | 3246 | |
cac20fcd | 3247 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
3248 | if (IS_ERR(set)) |
3249 | return PTR_ERR(set); | |
3250 | ||
d9adf22a | 3251 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
3252 | if (skb2 == NULL) |
3253 | return -ENOMEM; | |
3254 | ||
3255 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
3256 | if (err < 0) | |
3257 | goto err; | |
3258 | ||
3259 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
3260 | ||
3261 | err: | |
3262 | kfree_skb(skb2); | |
3263 | return err; | |
3264 | } | |
3265 | ||
c50b960c PM |
3266 | static int nf_tables_set_desc_parse(const struct nft_ctx *ctx, |
3267 | struct nft_set_desc *desc, | |
3268 | const struct nlattr *nla) | |
3269 | { | |
3270 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
3271 | int err; | |
3272 | ||
fceb6435 JB |
3273 | err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, |
3274 | nft_set_desc_policy, NULL); | |
c50b960c PM |
3275 | if (err < 0) |
3276 | return err; | |
3277 | ||
3278 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
3279 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
3280 | ||
3281 | return 0; | |
3282 | } | |
3283 | ||
633c9a84 PNA |
3284 | static int nf_tables_newset(struct net *net, struct sock *nlsk, |
3285 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3286 | const struct nlattr * const nla[], |
3287 | struct netlink_ext_ack *extack) | |
20a69341 PM |
3288 | { |
3289 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 3290 | u8 genmask = nft_genmask_next(net); |
98319cb9 | 3291 | int family = nfmsg->nfgen_family; |
20a69341 | 3292 | const struct nft_set_ops *ops; |
20a69341 PM |
3293 | struct nft_table *table; |
3294 | struct nft_set *set; | |
3295 | struct nft_ctx ctx; | |
38745490 | 3296 | char *name; |
20a69341 PM |
3297 | unsigned int size; |
3298 | bool create; | |
761da293 | 3299 | u64 timeout; |
8aeff920 | 3300 | u32 ktype, dtype, flags, policy, gc_int, objtype; |
c50b960c | 3301 | struct nft_set_desc desc; |
e6d8ecac CFG |
3302 | unsigned char *udata; |
3303 | u16 udlen; | |
20a69341 PM |
3304 | int err; |
3305 | ||
3306 | if (nla[NFTA_SET_TABLE] == NULL || | |
3307 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
3308 | nla[NFTA_SET_KEY_LEN] == NULL || |
3309 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
3310 | return -EINVAL; |
3311 | ||
c50b960c PM |
3312 | memset(&desc, 0, sizeof(desc)); |
3313 | ||
20a69341 PM |
3314 | ktype = NFT_DATA_VALUE; |
3315 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { | |
3316 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); | |
3317 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
3318 | return -EINVAL; | |
3319 | } | |
3320 | ||
c50b960c | 3321 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 3322 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
3323 | return -EINVAL; |
3324 | ||
3325 | flags = 0; | |
3326 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
3327 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
3328 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 3329 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 PNA |
3330 | NFT_SET_MAP | NFT_SET_EVAL | |
3331 | NFT_SET_OBJECT)) | |
20a69341 | 3332 | return -EINVAL; |
8aeff920 PNA |
3333 | /* Only one of these operations is supported */ |
3334 | if ((flags & (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
3335 | (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 3336 | return -EOPNOTSUPP; |
20a69341 PM |
3337 | } |
3338 | ||
3339 | dtype = 0; | |
20a69341 PM |
3340 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
3341 | if (!(flags & NFT_SET_MAP)) | |
3342 | return -EINVAL; | |
3343 | ||
3344 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); | |
3345 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
3346 | dtype != NFT_DATA_VERDICT) | |
3347 | return -EINVAL; | |
3348 | ||
3349 | if (dtype != NFT_DATA_VERDICT) { | |
3350 | if (nla[NFTA_SET_DATA_LEN] == NULL) | |
3351 | return -EINVAL; | |
c50b960c | 3352 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 3353 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
3354 | return -EINVAL; |
3355 | } else | |
7d740264 | 3356 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
3357 | } else if (flags & NFT_SET_MAP) |
3358 | return -EINVAL; | |
3359 | ||
8aeff920 PNA |
3360 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
3361 | if (!(flags & NFT_SET_OBJECT)) | |
3362 | return -EINVAL; | |
3363 | ||
3364 | objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); | |
3365 | if (objtype == NFT_OBJECT_UNSPEC || | |
3366 | objtype > NFT_OBJECT_MAX) | |
3367 | return -EINVAL; | |
3368 | } else if (flags & NFT_SET_OBJECT) | |
3369 | return -EINVAL; | |
3370 | else | |
3371 | objtype = NFT_OBJECT_UNSPEC; | |
3372 | ||
761da293 PM |
3373 | timeout = 0; |
3374 | if (nla[NFTA_SET_TIMEOUT] != NULL) { | |
3375 | if (!(flags & NFT_SET_TIMEOUT)) | |
3376 | return -EINVAL; | |
8e1102d5 FW |
3377 | |
3378 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout); | |
3379 | if (err) | |
3380 | return err; | |
761da293 PM |
3381 | } |
3382 | gc_int = 0; | |
3383 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { | |
3384 | if (!(flags & NFT_SET_TIMEOUT)) | |
3385 | return -EINVAL; | |
3386 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); | |
3387 | } | |
3388 | ||
c50b960c PM |
3389 | policy = NFT_SET_POL_PERFORMANCE; |
3390 | if (nla[NFTA_SET_POLICY] != NULL) | |
3391 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); | |
3392 | ||
3393 | if (nla[NFTA_SET_DESC] != NULL) { | |
3394 | err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]); | |
3395 | if (err < 0) | |
3396 | return err; | |
3397 | } | |
3398 | ||
20a69341 PM |
3399 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; |
3400 | ||
cac20fcd | 3401 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask); |
36dd1bcc PNA |
3402 | if (IS_ERR(table)) { |
3403 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 3404 | return PTR_ERR(table); |
36dd1bcc | 3405 | } |
20a69341 | 3406 | |
98319cb9 | 3407 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 | 3408 | |
cac20fcd | 3409 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 3410 | if (IS_ERR(set)) { |
36dd1bcc PNA |
3411 | if (PTR_ERR(set) != -ENOENT) { |
3412 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 3413 | return PTR_ERR(set); |
36dd1bcc | 3414 | } |
1a28ad74 | 3415 | } else { |
36dd1bcc PNA |
3416 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
3417 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 3418 | return -EEXIST; |
36dd1bcc | 3419 | } |
20a69341 PM |
3420 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
3421 | return -EOPNOTSUPP; | |
36dd1bcc | 3422 | |
20a69341 PM |
3423 | return 0; |
3424 | } | |
3425 | ||
3426 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
3427 | return -ENOENT; | |
3428 | ||
2b664957 | 3429 | ops = nft_select_set_ops(&ctx, nla, &desc, policy); |
20a69341 PM |
3430 | if (IS_ERR(ops)) |
3431 | return PTR_ERR(ops); | |
3432 | ||
e6d8ecac CFG |
3433 | udlen = 0; |
3434 | if (nla[NFTA_SET_USERDATA]) | |
3435 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
3436 | ||
20a69341 PM |
3437 | size = 0; |
3438 | if (ops->privsize != NULL) | |
347b408d | 3439 | size = ops->privsize(nla, &desc); |
20a69341 | 3440 | |
1ff75a3e PNA |
3441 | set = kvzalloc(sizeof(*set) + size + udlen, GFP_KERNEL); |
3442 | if (!set) { | |
3443 | err = -ENOMEM; | |
20a69341 | 3444 | goto err1; |
1ff75a3e | 3445 | } |
20a69341 | 3446 | |
38745490 PS |
3447 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL); |
3448 | if (!name) { | |
3449 | err = -ENOMEM; | |
3450 | goto err2; | |
3451 | } | |
3452 | ||
20a69341 | 3453 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 3454 | kfree(name); |
20a69341 PM |
3455 | if (err < 0) |
3456 | goto err2; | |
3457 | ||
e6d8ecac CFG |
3458 | udata = NULL; |
3459 | if (udlen) { | |
3460 | udata = set->data + size; | |
3461 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
3462 | } | |
3463 | ||
20a69341 | 3464 | INIT_LIST_HEAD(&set->bindings); |
3453c927 PNA |
3465 | set->table = table; |
3466 | write_pnet(&set->net, net); | |
20a69341 PM |
3467 | set->ops = ops; |
3468 | set->ktype = ktype; | |
c50b960c | 3469 | set->klen = desc.klen; |
20a69341 | 3470 | set->dtype = dtype; |
8aeff920 | 3471 | set->objtype = objtype; |
c50b960c | 3472 | set->dlen = desc.dlen; |
20a69341 | 3473 | set->flags = flags; |
c50b960c | 3474 | set->size = desc.size; |
9363dc4b | 3475 | set->policy = policy; |
e6d8ecac CFG |
3476 | set->udlen = udlen; |
3477 | set->udata = udata; | |
761da293 PM |
3478 | set->timeout = timeout; |
3479 | set->gc_int = gc_int; | |
3ecbfd65 | 3480 | set->handle = nf_tables_alloc_handle(table); |
20a69341 | 3481 | |
c50b960c | 3482 | err = ops->init(set, &desc, nla); |
20a69341 | 3483 | if (err < 0) |
2f6adf48 | 3484 | goto err3; |
20a69341 | 3485 | |
958bee14 | 3486 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 3487 | if (err < 0) |
2f6adf48 | 3488 | goto err4; |
20a69341 | 3489 | |
e688a7f8 | 3490 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 3491 | table->use++; |
20a69341 PM |
3492 | return 0; |
3493 | ||
2f6adf48 | 3494 | err4: |
c17c3cdf | 3495 | ops->destroy(set); |
2f6adf48 FW |
3496 | err3: |
3497 | kfree(set->name); | |
20a69341 | 3498 | err2: |
1ff75a3e | 3499 | kvfree(set); |
20a69341 | 3500 | err1: |
71cc0873 | 3501 | module_put(to_set_type(ops)->owner); |
20a69341 PM |
3502 | return err; |
3503 | } | |
3504 | ||
958bee14 | 3505 | static void nft_set_destroy(struct nft_set *set) |
20a69341 | 3506 | { |
20a69341 | 3507 | set->ops->destroy(set); |
71cc0873 | 3508 | module_put(to_set_type(set->ops)->owner); |
38745490 | 3509 | kfree(set->name); |
1ff75a3e | 3510 | kvfree(set); |
20a69341 PM |
3511 | } |
3512 | ||
958bee14 PNA |
3513 | static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
3514 | { | |
e688a7f8 | 3515 | list_del_rcu(&set->list); |
31f8441c | 3516 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC); |
958bee14 PNA |
3517 | nft_set_destroy(set); |
3518 | } | |
3519 | ||
633c9a84 PNA |
3520 | static int nf_tables_delset(struct net *net, struct sock *nlsk, |
3521 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3522 | const struct nlattr * const nla[], |
3523 | struct netlink_ext_ack *extack) | |
20a69341 | 3524 | { |
c9c8e485 | 3525 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
f2a6d766 | 3526 | u8 genmask = nft_genmask_next(net); |
36dd1bcc | 3527 | const struct nlattr *attr; |
20a69341 PM |
3528 | struct nft_set *set; |
3529 | struct nft_ctx ctx; | |
3530 | int err; | |
3531 | ||
ec2c9935 PM |
3532 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) |
3533 | return -EAFNOSUPPORT; | |
20a69341 PM |
3534 | if (nla[NFTA_SET_TABLE] == NULL) |
3535 | return -EINVAL; | |
3536 | ||
36dd1bcc PNA |
3537 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack, |
3538 | genmask); | |
20a69341 PM |
3539 | if (err < 0) |
3540 | return err; | |
3541 | ||
36dd1bcc PNA |
3542 | if (nla[NFTA_SET_HANDLE]) { |
3543 | attr = nla[NFTA_SET_HANDLE]; | |
3544 | set = nft_set_lookup_byhandle(ctx.table, attr, genmask); | |
3545 | } else { | |
3546 | attr = nla[NFTA_SET_NAME]; | |
3547 | set = nft_set_lookup(ctx.table, attr, genmask); | |
3548 | } | |
a8278400 | 3549 | |
36dd1bcc PNA |
3550 | if (IS_ERR(set)) { |
3551 | NL_SET_BAD_ATTR(extack, attr); | |
3552 | return PTR_ERR(set); | |
3553 | } | |
a8278400 | 3554 | if (!list_empty(&set->bindings) || |
36dd1bcc PNA |
3555 | (nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) { |
3556 | NL_SET_BAD_ATTR(extack, attr); | |
20a69341 | 3557 | return -EBUSY; |
36dd1bcc | 3558 | } |
20a69341 | 3559 | |
ee01d542 | 3560 | return nft_delset(&ctx, set); |
20a69341 PM |
3561 | } |
3562 | ||
3563 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, | |
de70185d | 3564 | struct nft_set *set, |
20a69341 | 3565 | const struct nft_set_iter *iter, |
de70185d | 3566 | struct nft_set_elem *elem) |
20a69341 | 3567 | { |
fe2811eb | 3568 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
3569 | enum nft_registers dreg; |
3570 | ||
3571 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
3572 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
3573 | set->dtype == NFT_DATA_VERDICT ? | |
3574 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
3575 | set->dlen); | |
20a69341 PM |
3576 | } |
3577 | ||
3578 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
3579 | struct nft_set_binding *binding) | |
3580 | { | |
3581 | struct nft_set_binding *i; | |
3582 | struct nft_set_iter iter; | |
3583 | ||
408070d6 | 3584 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
3585 | return -EBUSY; |
3586 | ||
11113e19 | 3587 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
3588 | /* If the set is already bound to the same chain all |
3589 | * jumps are already validated for that chain. | |
3590 | */ | |
3591 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 3592 | if (i->flags & NFT_SET_MAP && |
11113e19 | 3593 | i->chain == binding->chain) |
20a69341 PM |
3594 | goto bind; |
3595 | } | |
3596 | ||
8588ac09 | 3597 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
3598 | iter.skip = 0; |
3599 | iter.count = 0; | |
3600 | iter.err = 0; | |
3601 | iter.fn = nf_tables_bind_check_setelem; | |
3602 | ||
3603 | set->ops->walk(ctx, set, &iter); | |
a02f4248 | 3604 | if (iter.err < 0) |
20a69341 | 3605 | return iter.err; |
20a69341 PM |
3606 | } |
3607 | bind: | |
3608 | binding->chain = ctx->chain; | |
e688a7f8 | 3609 | list_add_tail_rcu(&binding->list, &set->bindings); |
20a69341 PM |
3610 | return 0; |
3611 | } | |
63aea290 | 3612 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 PM |
3613 | |
3614 | void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
3615 | struct nft_set_binding *binding) | |
3616 | { | |
e688a7f8 | 3617 | list_del_rcu(&binding->list); |
20a69341 | 3618 | |
408070d6 | 3619 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set) && |
37a9cc52 | 3620 | nft_is_active(ctx->net, set)) |
20a69341 PM |
3621 | nf_tables_set_destroy(ctx, set); |
3622 | } | |
63aea290 | 3623 | EXPORT_SYMBOL_GPL(nf_tables_unbind_set); |
20a69341 | 3624 | |
3ac4c07a PM |
3625 | const struct nft_set_ext_type nft_set_ext_types[] = { |
3626 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 3627 | .align = __alignof__(u32), |
3ac4c07a PM |
3628 | }, |
3629 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 3630 | .align = __alignof__(u32), |
3ac4c07a | 3631 | }, |
f25ad2e9 PM |
3632 | [NFT_SET_EXT_EXPR] = { |
3633 | .align = __alignof__(struct nft_expr), | |
3634 | }, | |
8aeff920 PNA |
3635 | [NFT_SET_EXT_OBJREF] = { |
3636 | .len = sizeof(struct nft_object *), | |
3637 | .align = __alignof__(struct nft_object *), | |
3638 | }, | |
3ac4c07a PM |
3639 | [NFT_SET_EXT_FLAGS] = { |
3640 | .len = sizeof(u8), | |
3641 | .align = __alignof__(u8), | |
3642 | }, | |
c3e1b005 PM |
3643 | [NFT_SET_EXT_TIMEOUT] = { |
3644 | .len = sizeof(u64), | |
3645 | .align = __alignof__(u64), | |
3646 | }, | |
3647 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
3648 | .len = sizeof(u64), |
3649 | .align = __alignof__(u64), | |
c3e1b005 | 3650 | }, |
68e942e8 PM |
3651 | [NFT_SET_EXT_USERDATA] = { |
3652 | .len = sizeof(struct nft_userdata), | |
3653 | .align = __alignof__(struct nft_userdata), | |
3654 | }, | |
3ac4c07a PM |
3655 | }; |
3656 | EXPORT_SYMBOL_GPL(nft_set_ext_types); | |
3657 | ||
20a69341 PM |
3658 | /* |
3659 | * Set elements | |
3660 | */ | |
3661 | ||
3662 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
3663 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
3664 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
3665 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 3666 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
68e942e8 PM |
3667 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
3668 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 FW |
3669 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
3670 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING }, | |
20a69341 PM |
3671 | }; |
3672 | ||
3673 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
3674 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
3675 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
3676 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
3677 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 3678 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 3679 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
3680 | }; |
3681 | ||
633c9a84 | 3682 | static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net, |
20a69341 PM |
3683 | const struct sk_buff *skb, |
3684 | const struct nlmsghdr *nlh, | |
f2a6d766 | 3685 | const struct nlattr * const nla[], |
36dd1bcc | 3686 | struct netlink_ext_ack *extack, |
f2a6d766 | 3687 | u8 genmask) |
20a69341 PM |
3688 | { |
3689 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
98319cb9 | 3690 | int family = nfmsg->nfgen_family; |
7c95f6d8 | 3691 | struct nft_table *table; |
20a69341 | 3692 | |
cac20fcd PNA |
3693 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
3694 | genmask); | |
36dd1bcc PNA |
3695 | if (IS_ERR(table)) { |
3696 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
20a69341 | 3697 | return PTR_ERR(table); |
36dd1bcc | 3698 | } |
20a69341 | 3699 | |
98319cb9 | 3700 | nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 PM |
3701 | return 0; |
3702 | } | |
3703 | ||
3704 | static int nf_tables_fill_setelem(struct sk_buff *skb, | |
3705 | const struct nft_set *set, | |
3706 | const struct nft_set_elem *elem) | |
3707 | { | |
fe2811eb | 3708 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
3709 | unsigned char *b = skb_tail_pointer(skb); |
3710 | struct nlattr *nest; | |
3711 | ||
3712 | nest = nla_nest_start(skb, NFTA_LIST_ELEM); | |
3713 | if (nest == NULL) | |
3714 | goto nla_put_failure; | |
3715 | ||
fe2811eb PM |
3716 | if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), |
3717 | NFT_DATA_VALUE, set->klen) < 0) | |
20a69341 PM |
3718 | goto nla_put_failure; |
3719 | ||
fe2811eb PM |
3720 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
3721 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
3722 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
3723 | set->dlen) < 0) | |
3724 | goto nla_put_failure; | |
3725 | ||
f25ad2e9 PM |
3726 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) && |
3727 | nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0) | |
3728 | goto nla_put_failure; | |
3729 | ||
8aeff920 PNA |
3730 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
3731 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
3732 | (*nft_set_ext_obj(ext))->name) < 0) | |
3733 | goto nla_put_failure; | |
3734 | ||
fe2811eb PM |
3735 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
3736 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
3737 | htonl(*nft_set_ext_flags(ext)))) | |
3738 | goto nla_put_failure; | |
20a69341 | 3739 | |
c3e1b005 PM |
3740 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
3741 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 3742 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 3743 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
3744 | goto nla_put_failure; |
3745 | ||
3746 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 3747 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
3748 | |
3749 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 3750 | if (time_before64(now, expires)) |
c3e1b005 PM |
3751 | expires -= now; |
3752 | else | |
3753 | expires = 0; | |
3754 | ||
3755 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 3756 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 3757 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
3758 | goto nla_put_failure; |
3759 | } | |
3760 | ||
68e942e8 PM |
3761 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
3762 | struct nft_userdata *udata; | |
3763 | ||
3764 | udata = nft_set_ext_userdata(ext); | |
3765 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
3766 | udata->len + 1, udata->data)) | |
3767 | goto nla_put_failure; | |
3768 | } | |
3769 | ||
20a69341 PM |
3770 | nla_nest_end(skb, nest); |
3771 | return 0; | |
3772 | ||
3773 | nla_put_failure: | |
3774 | nlmsg_trim(skb, b); | |
3775 | return -EMSGSIZE; | |
3776 | } | |
3777 | ||
3778 | struct nft_set_dump_args { | |
3779 | const struct netlink_callback *cb; | |
3780 | struct nft_set_iter iter; | |
3781 | struct sk_buff *skb; | |
3782 | }; | |
3783 | ||
3784 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 3785 | struct nft_set *set, |
20a69341 | 3786 | const struct nft_set_iter *iter, |
de70185d | 3787 | struct nft_set_elem *elem) |
20a69341 PM |
3788 | { |
3789 | struct nft_set_dump_args *args; | |
3790 | ||
3791 | args = container_of(iter, struct nft_set_dump_args, iter); | |
3792 | return nf_tables_fill_setelem(args->skb, set, elem); | |
3793 | } | |
3794 | ||
fa803605 LZ |
3795 | struct nft_set_dump_ctx { |
3796 | const struct nft_set *set; | |
3797 | struct nft_ctx ctx; | |
3798 | }; | |
3799 | ||
20a69341 PM |
3800 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
3801 | { | |
fa803605 | 3802 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 3803 | struct net *net = sock_net(skb->sk); |
fa803605 | 3804 | struct nft_table *table; |
de70185d | 3805 | struct nft_set *set; |
20a69341 | 3806 | struct nft_set_dump_args args; |
fa803605 | 3807 | bool set_found = false; |
20a69341 PM |
3808 | struct nfgenmsg *nfmsg; |
3809 | struct nlmsghdr *nlh; | |
3810 | struct nlattr *nest; | |
3811 | u32 portid, seq; | |
fa803605 | 3812 | int event; |
20a69341 | 3813 | |
fa803605 | 3814 | rcu_read_lock(); |
36596dad PNA |
3815 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
3816 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && | |
98319cb9 | 3817 | dump_ctx->ctx.family != table->family) |
fa803605 | 3818 | continue; |
20a69341 | 3819 | |
36596dad PNA |
3820 | if (table != dump_ctx->ctx.table) |
3821 | continue; | |
20a69341 | 3822 | |
36596dad PNA |
3823 | list_for_each_entry_rcu(set, &table->sets, list) { |
3824 | if (set == dump_ctx->set) { | |
3825 | set_found = true; | |
3826 | break; | |
fa803605 | 3827 | } |
fa803605 LZ |
3828 | } |
3829 | break; | |
3830 | } | |
3831 | ||
3832 | if (!set_found) { | |
3833 | rcu_read_unlock(); | |
3834 | return -ENOENT; | |
3835 | } | |
20a69341 | 3836 | |
dedb67c4 | 3837 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
3838 | portid = NETLINK_CB(cb->skb).portid; |
3839 | seq = cb->nlh->nlmsg_seq; | |
3840 | ||
3841 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), | |
3842 | NLM_F_MULTI); | |
3843 | if (nlh == NULL) | |
3844 | goto nla_put_failure; | |
3845 | ||
3846 | nfmsg = nlmsg_data(nlh); | |
98319cb9 | 3847 | nfmsg->nfgen_family = table->family; |
20a69341 | 3848 | nfmsg->version = NFNETLINK_V0; |
fa803605 | 3849 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
20a69341 | 3850 | |
fa803605 | 3851 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
3852 | goto nla_put_failure; |
3853 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
3854 | goto nla_put_failure; | |
3855 | ||
3856 | nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS); | |
3857 | if (nest == NULL) | |
3858 | goto nla_put_failure; | |
3859 | ||
8588ac09 PNA |
3860 | args.cb = cb; |
3861 | args.skb = skb; | |
fa803605 | 3862 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
3863 | args.iter.skip = cb->args[0]; |
3864 | args.iter.count = 0; | |
3865 | args.iter.err = 0; | |
3866 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 LZ |
3867 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
3868 | rcu_read_unlock(); | |
20a69341 PM |
3869 | |
3870 | nla_nest_end(skb, nest); | |
3871 | nlmsg_end(skb, nlh); | |
3872 | ||
3873 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
3874 | return args.iter.err; | |
3875 | if (args.iter.count == cb->args[0]) | |
3876 | return 0; | |
3877 | ||
3878 | cb->args[0] = args.iter.count; | |
3879 | return skb->len; | |
3880 | ||
3881 | nla_put_failure: | |
fa803605 | 3882 | rcu_read_unlock(); |
20a69341 PM |
3883 | return -ENOSPC; |
3884 | } | |
3885 | ||
90fd131a FW |
3886 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
3887 | { | |
3888 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
3889 | ||
3890 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
3891 | ||
3892 | return cb->data ? 0 : -ENOMEM; | |
3893 | } | |
3894 | ||
fa803605 LZ |
3895 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
3896 | { | |
3897 | kfree(cb->data); | |
3898 | return 0; | |
3899 | } | |
3900 | ||
d60ce62f AB |
3901 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
3902 | const struct nft_ctx *ctx, u32 seq, | |
3903 | u32 portid, int event, u16 flags, | |
3904 | const struct nft_set *set, | |
3905 | const struct nft_set_elem *elem) | |
3906 | { | |
3907 | struct nfgenmsg *nfmsg; | |
3908 | struct nlmsghdr *nlh; | |
3909 | struct nlattr *nest; | |
3910 | int err; | |
3911 | ||
dedb67c4 | 3912 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
d60ce62f AB |
3913 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
3914 | flags); | |
3915 | if (nlh == NULL) | |
3916 | goto nla_put_failure; | |
3917 | ||
3918 | nfmsg = nlmsg_data(nlh); | |
36596dad | 3919 | nfmsg->nfgen_family = ctx->family; |
d60ce62f | 3920 | nfmsg->version = NFNETLINK_V0; |
84d7fce6 | 3921 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
d60ce62f AB |
3922 | |
3923 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) | |
3924 | goto nla_put_failure; | |
3925 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
3926 | goto nla_put_failure; | |
3927 | ||
3928 | nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS); | |
3929 | if (nest == NULL) | |
3930 | goto nla_put_failure; | |
3931 | ||
3932 | err = nf_tables_fill_setelem(skb, set, elem); | |
3933 | if (err < 0) | |
3934 | goto nla_put_failure; | |
3935 | ||
3936 | nla_nest_end(skb, nest); | |
3937 | ||
053c095a JB |
3938 | nlmsg_end(skb, nlh); |
3939 | return 0; | |
d60ce62f AB |
3940 | |
3941 | nla_put_failure: | |
3942 | nlmsg_trim(skb, nlh); | |
3943 | return -1; | |
3944 | } | |
3945 | ||
ba0e4d99 PNA |
3946 | static int nft_setelem_parse_flags(const struct nft_set *set, |
3947 | const struct nlattr *attr, u32 *flags) | |
3948 | { | |
3949 | if (attr == NULL) | |
3950 | return 0; | |
3951 | ||
3952 | *flags = ntohl(nla_get_be32(attr)); | |
3953 | if (*flags & ~NFT_SET_ELEM_INTERVAL_END) | |
3954 | return -EINVAL; | |
3955 | if (!(set->flags & NFT_SET_INTERVAL) && | |
3956 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
3957 | return -EINVAL; | |
3958 | ||
3959 | return 0; | |
3960 | } | |
3961 | ||
3962 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, | |
3963 | const struct nlattr *attr) | |
3964 | { | |
3965 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3966 | const struct nft_set_ext *ext; | |
3967 | struct nft_data_desc desc; | |
3968 | struct nft_set_elem elem; | |
3969 | struct sk_buff *skb; | |
3970 | uint32_t flags = 0; | |
3971 | void *priv; | |
3972 | int err; | |
3973 | ||
3974 | err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr, | |
3975 | nft_set_elem_policy, NULL); | |
3976 | if (err < 0) | |
3977 | return err; | |
3978 | ||
3979 | if (!nla[NFTA_SET_ELEM_KEY]) | |
3980 | return -EINVAL; | |
3981 | ||
3982 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); | |
3983 | if (err < 0) | |
3984 | return err; | |
3985 | ||
3986 | err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc, | |
3987 | nla[NFTA_SET_ELEM_KEY]); | |
3988 | if (err < 0) | |
3989 | return err; | |
3990 | ||
3991 | err = -EINVAL; | |
3992 | if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) | |
3993 | return err; | |
3994 | ||
3995 | priv = set->ops->get(ctx->net, set, &elem, flags); | |
3996 | if (IS_ERR(priv)) | |
3997 | return PTR_ERR(priv); | |
3998 | ||
3999 | elem.priv = priv; | |
4000 | ext = nft_set_elem_ext(set, &elem); | |
4001 | ||
4002 | err = -ENOMEM; | |
d9adf22a | 4003 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 PNA |
4004 | if (skb == NULL) |
4005 | goto err1; | |
4006 | ||
4007 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
4008 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
4009 | if (err < 0) | |
4010 | goto err2; | |
4011 | ||
4012 | err = nfnetlink_unicast(skb, ctx->net, ctx->portid, MSG_DONTWAIT); | |
4013 | /* This avoids a loop in nfnetlink. */ | |
4014 | if (err < 0) | |
4015 | goto err1; | |
4016 | ||
4017 | return 0; | |
4018 | err2: | |
4019 | kfree_skb(skb); | |
4020 | err1: | |
4021 | /* this avoids a loop in nfnetlink. */ | |
4022 | return err == -EAGAIN ? -ENOBUFS : err; | |
4023 | } | |
4024 | ||
d9adf22a | 4025 | /* called with rcu_read_lock held */ |
ba0e4d99 PNA |
4026 | static int nf_tables_getsetelem(struct net *net, struct sock *nlsk, |
4027 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
4028 | const struct nlattr * const nla[], | |
4029 | struct netlink_ext_ack *extack) | |
4030 | { | |
4031 | u8 genmask = nft_genmask_cur(net); | |
4032 | struct nft_set *set; | |
4033 | struct nlattr *attr; | |
4034 | struct nft_ctx ctx; | |
4035 | int rem, err = 0; | |
4036 | ||
36dd1bcc PNA |
4037 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
4038 | genmask); | |
ba0e4d99 PNA |
4039 | if (err < 0) |
4040 | return err; | |
4041 | ||
cac20fcd | 4042 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
4043 | if (IS_ERR(set)) |
4044 | return PTR_ERR(set); | |
4045 | ||
4046 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
4047 | struct netlink_dump_control c = { | |
90fd131a | 4048 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
4049 | .dump = nf_tables_dump_set, |
4050 | .done = nf_tables_dump_set_done, | |
d9adf22a | 4051 | .module = THIS_MODULE, |
ba0e4d99 | 4052 | }; |
90fd131a FW |
4053 | struct nft_set_dump_ctx dump_ctx = { |
4054 | .set = set, | |
4055 | .ctx = ctx, | |
4056 | }; | |
ba0e4d99 | 4057 | |
90fd131a | 4058 | c.data = &dump_ctx; |
d9adf22a | 4059 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
ba0e4d99 PNA |
4060 | } |
4061 | ||
4062 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
4063 | return -EINVAL; | |
4064 | ||
4065 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
4066 | err = nft_get_set_elem(&ctx, set, attr); | |
4067 | if (err < 0) | |
4068 | break; | |
4069 | } | |
4070 | ||
4071 | return err; | |
4072 | } | |
4073 | ||
25e94a99 PNA |
4074 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
4075 | const struct nft_set *set, | |
4076 | const struct nft_set_elem *elem, | |
4077 | int event, u16 flags) | |
d60ce62f | 4078 | { |
128ad332 PNA |
4079 | struct net *net = ctx->net; |
4080 | u32 portid = ctx->portid; | |
d60ce62f AB |
4081 | struct sk_buff *skb; |
4082 | int err; | |
4083 | ||
128ad332 | 4084 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 4085 | return; |
d60ce62f | 4086 | |
d60ce62f AB |
4087 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
4088 | if (skb == NULL) | |
4089 | goto err; | |
4090 | ||
4091 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, | |
4092 | set, elem); | |
4093 | if (err < 0) { | |
4094 | kfree_skb(skb); | |
4095 | goto err; | |
4096 | } | |
4097 | ||
25e94a99 PNA |
4098 | nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report, |
4099 | GFP_KERNEL); | |
4100 | return; | |
d60ce62f | 4101 | err: |
25e94a99 | 4102 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
4103 | } |
4104 | ||
60319eb1 PNA |
4105 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
4106 | int msg_type, | |
4107 | struct nft_set *set) | |
4108 | { | |
4109 | struct nft_trans *trans; | |
4110 | ||
4111 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
4112 | if (trans == NULL) | |
4113 | return NULL; | |
4114 | ||
4115 | nft_trans_elem_set(trans) = set; | |
4116 | return trans; | |
4117 | } | |
4118 | ||
22fe54d5 PM |
4119 | void *nft_set_elem_init(const struct nft_set *set, |
4120 | const struct nft_set_ext_tmpl *tmpl, | |
49499c3e | 4121 | const u32 *key, const u32 *data, |
22fe54d5 | 4122 | u64 timeout, gfp_t gfp) |
fe2811eb PM |
4123 | { |
4124 | struct nft_set_ext *ext; | |
4125 | void *elem; | |
4126 | ||
4127 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
4128 | if (elem == NULL) | |
4129 | return NULL; | |
4130 | ||
4131 | ext = nft_set_elem_ext(set, elem); | |
4132 | nft_set_ext_init(ext, tmpl); | |
4133 | ||
4134 | memcpy(nft_set_ext_key(ext), key, set->klen); | |
4135 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
4136 | memcpy(nft_set_ext_data(ext), data, set->dlen); | |
c3e1b005 PM |
4137 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) |
4138 | *nft_set_ext_expiration(ext) = | |
8e1102d5 | 4139 | get_jiffies_64() + timeout; |
c3e1b005 PM |
4140 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
4141 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
4142 | |
4143 | return elem; | |
4144 | } | |
4145 | ||
61f9e292 LZ |
4146 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
4147 | bool destroy_expr) | |
61edafbb PM |
4148 | { |
4149 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
4150 | struct nft_ctx ctx = { |
4151 | .net = read_pnet(&set->net), | |
4152 | .family = set->table->family, | |
4153 | }; | |
61edafbb | 4154 | |
59105446 | 4155 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 4156 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 4157 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
371ebcbb PNA |
4158 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) { |
4159 | struct nft_expr *expr = nft_set_ext_expr(ext); | |
4160 | ||
4161 | if (expr->ops->destroy_clone) { | |
4162 | expr->ops->destroy_clone(&ctx, expr); | |
4163 | module_put(expr->ops->type->owner); | |
4164 | } else { | |
4165 | nf_tables_expr_destroy(&ctx, expr); | |
4166 | } | |
4167 | } | |
8aeff920 PNA |
4168 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
4169 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
4170 | kfree(elem); |
4171 | } | |
4172 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
4173 | ||
59105446 PNA |
4174 | /* Only called from commit path, nft_set_elem_deactivate() already deals with |
4175 | * the refcounting from the preparation phase. | |
4176 | */ | |
3453c927 PNA |
4177 | static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
4178 | const struct nft_set *set, void *elem) | |
59105446 PNA |
4179 | { |
4180 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
4181 | ||
4182 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) | |
3453c927 | 4183 | nf_tables_expr_destroy(ctx, nft_set_ext_expr(ext)); |
59105446 PNA |
4184 | kfree(elem); |
4185 | } | |
4186 | ||
60319eb1 | 4187 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 4188 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 PM |
4189 | { |
4190 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
8aeff920 | 4191 | u8 genmask = nft_genmask_next(ctx->net); |
20a69341 | 4192 | struct nft_data_desc d1, d2; |
fe2811eb | 4193 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 4194 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
4195 | struct nft_set_elem elem; |
4196 | struct nft_set_binding *binding; | |
8aeff920 | 4197 | struct nft_object *obj = NULL; |
68e942e8 | 4198 | struct nft_userdata *udata; |
fe2811eb | 4199 | struct nft_data data; |
20a69341 | 4200 | enum nft_registers dreg; |
60319eb1 | 4201 | struct nft_trans *trans; |
0e9091d6 | 4202 | u32 flags = 0; |
c3e1b005 | 4203 | u64 timeout; |
68e942e8 | 4204 | u8 ulen; |
20a69341 PM |
4205 | int err; |
4206 | ||
4207 | err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr, | |
fceb6435 | 4208 | nft_set_elem_policy, NULL); |
20a69341 PM |
4209 | if (err < 0) |
4210 | return err; | |
4211 | ||
4212 | if (nla[NFTA_SET_ELEM_KEY] == NULL) | |
4213 | return -EINVAL; | |
4214 | ||
fe2811eb PM |
4215 | nft_set_ext_prepare(&tmpl); |
4216 | ||
0e9091d6 PNA |
4217 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
4218 | if (err < 0) | |
4219 | return err; | |
4220 | if (flags != 0) | |
4221 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
20a69341 PM |
4222 | |
4223 | if (set->flags & NFT_SET_MAP) { | |
4224 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 4225 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 | 4226 | return -EINVAL; |
bd7fc645 | 4227 | if (nla[NFTA_SET_ELEM_DATA] != NULL && |
fe2811eb | 4228 | flags & NFT_SET_ELEM_INTERVAL_END) |
bd7fc645 | 4229 | return -EINVAL; |
20a69341 PM |
4230 | } else { |
4231 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
4232 | return -EINVAL; | |
4233 | } | |
4234 | ||
c3e1b005 PM |
4235 | timeout = 0; |
4236 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
4237 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
4238 | return -EINVAL; | |
8e1102d5 FW |
4239 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
4240 | &timeout); | |
4241 | if (err) | |
4242 | return err; | |
c3e1b005 PM |
4243 | } else if (set->flags & NFT_SET_TIMEOUT) { |
4244 | timeout = set->timeout; | |
4245 | } | |
4246 | ||
7d740264 | 4247 | err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1, |
d0a11fc3 | 4248 | nla[NFTA_SET_ELEM_KEY]); |
20a69341 PM |
4249 | if (err < 0) |
4250 | goto err1; | |
4251 | err = -EINVAL; | |
4252 | if (d1.type != NFT_DATA_VALUE || d1.len != set->klen) | |
4253 | goto err2; | |
4254 | ||
7d740264 | 4255 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len); |
c3e1b005 PM |
4256 | if (timeout > 0) { |
4257 | nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); | |
4258 | if (timeout != set->timeout) | |
4259 | nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); | |
4260 | } | |
fe2811eb | 4261 | |
8aeff920 PNA |
4262 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
4263 | if (!(set->flags & NFT_SET_OBJECT)) { | |
4264 | err = -EINVAL; | |
4265 | goto err2; | |
4266 | } | |
cac20fcd PNA |
4267 | obj = nft_obj_lookup(ctx->table, nla[NFTA_SET_ELEM_OBJREF], |
4268 | set->objtype, genmask); | |
8aeff920 PNA |
4269 | if (IS_ERR(obj)) { |
4270 | err = PTR_ERR(obj); | |
4271 | goto err2; | |
4272 | } | |
4273 | nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); | |
4274 | } | |
4275 | ||
20a69341 | 4276 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
d0a11fc3 PM |
4277 | err = nft_data_init(ctx, &data, sizeof(data), &d2, |
4278 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 PM |
4279 | if (err < 0) |
4280 | goto err2; | |
4281 | ||
4282 | err = -EINVAL; | |
4283 | if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen) | |
4284 | goto err3; | |
4285 | ||
4286 | dreg = nft_type_to_reg(set->dtype); | |
4287 | list_for_each_entry(binding, &set->bindings, list) { | |
4288 | struct nft_ctx bind_ctx = { | |
58c78e10 | 4289 | .net = ctx->net, |
36596dad | 4290 | .family = ctx->family, |
20a69341 | 4291 | .table = ctx->table, |
7c95f6d8 | 4292 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
4293 | }; |
4294 | ||
11113e19 PM |
4295 | if (!(binding->flags & NFT_SET_MAP)) |
4296 | continue; | |
4297 | ||
1ec10212 PM |
4298 | err = nft_validate_register_store(&bind_ctx, dreg, |
4299 | &data, | |
4300 | d2.type, d2.len); | |
20a69341 PM |
4301 | if (err < 0) |
4302 | goto err3; | |
a654de8f PNA |
4303 | |
4304 | if (d2.type == NFT_DATA_VERDICT && | |
4305 | (data.verdict.code == NFT_GOTO || | |
4306 | data.verdict.code == NFT_JUMP)) | |
4307 | nft_validate_state_update(ctx->net, | |
4308 | NFT_VALIDATE_NEED); | |
20a69341 | 4309 | } |
fe2811eb | 4310 | |
7d740264 | 4311 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len); |
20a69341 PM |
4312 | } |
4313 | ||
68e942e8 PM |
4314 | /* The full maximum length of userdata can exceed the maximum |
4315 | * offset value (U8_MAX) for following extensions, therefor it | |
4316 | * must be the last extension added. | |
4317 | */ | |
4318 | ulen = 0; | |
4319 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
4320 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
4321 | if (ulen > 0) | |
4322 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
4323 | ulen); | |
4324 | } | |
4325 | ||
fe2811eb | 4326 | err = -ENOMEM; |
7d740264 | 4327 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data, |
c3e1b005 | 4328 | timeout, GFP_KERNEL); |
fe2811eb PM |
4329 | if (elem.priv == NULL) |
4330 | goto err3; | |
4331 | ||
4332 | ext = nft_set_elem_ext(set, elem.priv); | |
4333 | if (flags) | |
4334 | *nft_set_ext_flags(ext) = flags; | |
68e942e8 PM |
4335 | if (ulen > 0) { |
4336 | udata = nft_set_ext_userdata(ext); | |
4337 | udata->len = ulen - 1; | |
4338 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
4339 | } | |
8aeff920 PNA |
4340 | if (obj) { |
4341 | *nft_set_ext_obj(ext) = obj; | |
4342 | obj->use++; | |
4343 | } | |
fe2811eb | 4344 | |
60319eb1 PNA |
4345 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
4346 | if (trans == NULL) | |
fe2811eb | 4347 | goto err4; |
60319eb1 | 4348 | |
69086658 | 4349 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
c016c7e4 PNA |
4350 | err = set->ops->insert(ctx->net, set, &elem, &ext2); |
4351 | if (err) { | |
4352 | if (err == -EEXIST) { | |
9744a6fc PNA |
4353 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
4354 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
4355 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
f0dfd7a2 CIK |
4356 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) { |
4357 | err = -EBUSY; | |
4358 | goto err5; | |
4359 | } | |
8aeff920 PNA |
4360 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
4361 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
4362 | memcmp(nft_set_ext_data(ext), | |
4363 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
4364 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
4365 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
4366 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
c016c7e4 PNA |
4367 | err = -EBUSY; |
4368 | else if (!(nlmsg_flags & NLM_F_EXCL)) | |
4369 | err = 0; | |
4370 | } | |
fe2811eb | 4371 | goto err5; |
c016c7e4 | 4372 | } |
20a69341 | 4373 | |
35d0ac90 PNA |
4374 | if (set->size && |
4375 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { | |
4376 | err = -ENFILE; | |
4377 | goto err6; | |
4378 | } | |
4379 | ||
60319eb1 | 4380 | nft_trans_elem(trans) = elem; |
46bbafce | 4381 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
20a69341 PM |
4382 | return 0; |
4383 | ||
35d0ac90 | 4384 | err6: |
5cb82a38 | 4385 | set->ops->remove(ctx->net, set, &elem); |
fe2811eb | 4386 | err5: |
60319eb1 | 4387 | kfree(trans); |
fe2811eb PM |
4388 | err4: |
4389 | kfree(elem.priv); | |
20a69341 PM |
4390 | err3: |
4391 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
59105446 | 4392 | nft_data_release(&data, d2.type); |
20a69341 | 4393 | err2: |
59105446 | 4394 | nft_data_release(&elem.key.val, d1.type); |
20a69341 PM |
4395 | err1: |
4396 | return err; | |
4397 | } | |
4398 | ||
633c9a84 PNA |
4399 | static int nf_tables_newsetelem(struct net *net, struct sock *nlsk, |
4400 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
4401 | const struct nlattr * const nla[], |
4402 | struct netlink_ext_ack *extack) | |
20a69341 | 4403 | { |
f2a6d766 | 4404 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
4405 | const struct nlattr *attr; |
4406 | struct nft_set *set; | |
4407 | struct nft_ctx ctx; | |
a654de8f | 4408 | int rem, err; |
20a69341 | 4409 | |
7d5570ca PNA |
4410 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
4411 | return -EINVAL; | |
4412 | ||
36dd1bcc PNA |
4413 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
4414 | genmask); | |
20a69341 PM |
4415 | if (err < 0) |
4416 | return err; | |
4417 | ||
a3073c17 PNA |
4418 | set = nft_set_lookup_global(net, ctx.table, nla[NFTA_SET_ELEM_LIST_SET], |
4419 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); | |
4420 | if (IS_ERR(set)) | |
4421 | return PTR_ERR(set); | |
958bee14 | 4422 | |
20a69341 PM |
4423 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
4424 | return -EBUSY; | |
4425 | ||
4426 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
c016c7e4 | 4427 | err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags); |
35d0ac90 | 4428 | if (err < 0) |
a654de8f | 4429 | return err; |
20a69341 | 4430 | } |
a654de8f PNA |
4431 | |
4432 | if (net->nft.validate_state == NFT_VALIDATE_DO) | |
4433 | return nft_table_validate(net, ctx.table); | |
4434 | ||
4435 | return 0; | |
20a69341 PM |
4436 | } |
4437 | ||
59105446 PNA |
4438 | /** |
4439 | * nft_data_hold - hold a nft_data item | |
4440 | * | |
4441 | * @data: struct nft_data to release | |
4442 | * @type: type of data | |
4443 | * | |
4444 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
4445 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
4446 | * NFT_GOTO verdicts. This function must be called on active data objects | |
4447 | * from the second phase of the commit protocol. | |
4448 | */ | |
bb7b40ae | 4449 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 PNA |
4450 | { |
4451 | if (type == NFT_DATA_VERDICT) { | |
4452 | switch (data->verdict.code) { | |
4453 | case NFT_JUMP: | |
4454 | case NFT_GOTO: | |
4455 | data->verdict.chain->use++; | |
4456 | break; | |
4457 | } | |
4458 | } | |
4459 | } | |
4460 | ||
4461 | static void nft_set_elem_activate(const struct net *net, | |
4462 | const struct nft_set *set, | |
4463 | struct nft_set_elem *elem) | |
4464 | { | |
4465 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
4466 | ||
4467 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
4468 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
4469 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
4470 | (*nft_set_ext_obj(ext))->use++; | |
4471 | } | |
4472 | ||
4473 | static void nft_set_elem_deactivate(const struct net *net, | |
4474 | const struct nft_set *set, | |
4475 | struct nft_set_elem *elem) | |
4476 | { | |
4477 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
4478 | ||
4479 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
4480 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
4481 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
4482 | (*nft_set_ext_obj(ext))->use--; | |
4483 | } | |
4484 | ||
60319eb1 | 4485 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
4486 | const struct nlattr *attr) |
4487 | { | |
4488 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 4489 | struct nft_set_ext_tmpl tmpl; |
20a69341 PM |
4490 | struct nft_data_desc desc; |
4491 | struct nft_set_elem elem; | |
3971ca14 | 4492 | struct nft_set_ext *ext; |
60319eb1 | 4493 | struct nft_trans *trans; |
3971ca14 PNA |
4494 | u32 flags = 0; |
4495 | void *priv; | |
20a69341 PM |
4496 | int err; |
4497 | ||
4498 | err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr, | |
fceb6435 | 4499 | nft_set_elem_policy, NULL); |
20a69341 PM |
4500 | if (err < 0) |
4501 | goto err1; | |
4502 | ||
4503 | err = -EINVAL; | |
4504 | if (nla[NFTA_SET_ELEM_KEY] == NULL) | |
4505 | goto err1; | |
4506 | ||
3971ca14 PNA |
4507 | nft_set_ext_prepare(&tmpl); |
4508 | ||
4509 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); | |
4510 | if (err < 0) | |
4511 | return err; | |
4512 | if (flags != 0) | |
4513 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
4514 | ||
7d740264 | 4515 | err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc, |
d0a11fc3 | 4516 | nla[NFTA_SET_ELEM_KEY]); |
20a69341 PM |
4517 | if (err < 0) |
4518 | goto err1; | |
4519 | ||
4520 | err = -EINVAL; | |
4521 | if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) | |
4522 | goto err2; | |
4523 | ||
3971ca14 PNA |
4524 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, desc.len); |
4525 | ||
4526 | err = -ENOMEM; | |
4527 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0, | |
4528 | GFP_KERNEL); | |
4529 | if (elem.priv == NULL) | |
4530 | goto err2; | |
4531 | ||
4532 | ext = nft_set_elem_ext(set, elem.priv); | |
4533 | if (flags) | |
4534 | *nft_set_ext_flags(ext) = flags; | |
4535 | ||
60319eb1 | 4536 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
609ccf08 JL |
4537 | if (trans == NULL) { |
4538 | err = -ENOMEM; | |
3971ca14 | 4539 | goto err3; |
609ccf08 | 4540 | } |
20a69341 | 4541 | |
42a55769 | 4542 | priv = set->ops->deactivate(ctx->net, set, &elem); |
3971ca14 | 4543 | if (priv == NULL) { |
cc02e457 | 4544 | err = -ENOENT; |
3971ca14 | 4545 | goto err4; |
cc02e457 | 4546 | } |
3971ca14 PNA |
4547 | kfree(elem.priv); |
4548 | elem.priv = priv; | |
cc02e457 | 4549 | |
59105446 PNA |
4550 | nft_set_elem_deactivate(ctx->net, set, &elem); |
4551 | ||
60319eb1 | 4552 | nft_trans_elem(trans) = elem; |
46bbafce | 4553 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
0dc13625 | 4554 | return 0; |
cc02e457 | 4555 | |
3971ca14 | 4556 | err4: |
cc02e457 | 4557 | kfree(trans); |
3971ca14 PNA |
4558 | err3: |
4559 | kfree(elem.priv); | |
20a69341 | 4560 | err2: |
59105446 | 4561 | nft_data_release(&elem.key.val, desc.type); |
20a69341 PM |
4562 | err1: |
4563 | return err; | |
4564 | } | |
4565 | ||
8411b644 | 4566 | static int nft_flush_set(const struct nft_ctx *ctx, |
de70185d | 4567 | struct nft_set *set, |
8411b644 | 4568 | const struct nft_set_iter *iter, |
de70185d | 4569 | struct nft_set_elem *elem) |
8411b644 PNA |
4570 | { |
4571 | struct nft_trans *trans; | |
4572 | int err; | |
4573 | ||
4574 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
4575 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
4576 | if (!trans) | |
4577 | return -ENOMEM; | |
4578 | ||
1ba1c414 | 4579 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
4580 | err = -ENOENT; |
4581 | goto err1; | |
4582 | } | |
b2c11e4b | 4583 | set->ndeact++; |
8411b644 | 4584 | |
de70185d PNA |
4585 | nft_trans_elem_set(trans) = set; |
4586 | nft_trans_elem(trans) = *elem; | |
8411b644 PNA |
4587 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
4588 | ||
4589 | return 0; | |
4590 | err1: | |
4591 | kfree(trans); | |
4592 | return err; | |
4593 | } | |
4594 | ||
633c9a84 PNA |
4595 | static int nf_tables_delsetelem(struct net *net, struct sock *nlsk, |
4596 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
4597 | const struct nlattr * const nla[], |
4598 | struct netlink_ext_ack *extack) | |
20a69341 | 4599 | { |
f2a6d766 | 4600 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
4601 | const struct nlattr *attr; |
4602 | struct nft_set *set; | |
4603 | struct nft_ctx ctx; | |
60319eb1 | 4604 | int rem, err = 0; |
20a69341 | 4605 | |
36dd1bcc PNA |
4606 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
4607 | genmask); | |
20a69341 PM |
4608 | if (err < 0) |
4609 | return err; | |
4610 | ||
cac20fcd | 4611 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
4612 | if (IS_ERR(set)) |
4613 | return PTR_ERR(set); | |
4614 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
4615 | return -EBUSY; | |
4616 | ||
8411b644 | 4617 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) { |
baa2d42c PNA |
4618 | struct nft_set_iter iter = { |
4619 | .genmask = genmask, | |
4620 | .fn = nft_flush_set, | |
8411b644 | 4621 | }; |
baa2d42c | 4622 | set->ops->walk(&ctx, set, &iter); |
8411b644 | 4623 | |
baa2d42c | 4624 | return iter.err; |
8411b644 PNA |
4625 | } |
4626 | ||
20a69341 PM |
4627 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
4628 | err = nft_del_setelem(&ctx, set, attr); | |
4629 | if (err < 0) | |
60319eb1 | 4630 | break; |
4fefee57 | 4631 | |
3dd0673a | 4632 | set->ndeact++; |
20a69341 | 4633 | } |
60319eb1 | 4634 | return err; |
20a69341 PM |
4635 | } |
4636 | ||
cfed7e1b PM |
4637 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
4638 | { | |
4639 | struct nft_set_gc_batch *gcb; | |
4640 | unsigned int i; | |
4641 | ||
4642 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
4643 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 4644 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
4645 | kfree(gcb); |
4646 | } | |
4647 | EXPORT_SYMBOL_GPL(nft_set_gc_batch_release); | |
4648 | ||
4649 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
4650 | gfp_t gfp) | |
4651 | { | |
4652 | struct nft_set_gc_batch *gcb; | |
4653 | ||
4654 | gcb = kzalloc(sizeof(*gcb), gfp); | |
4655 | if (gcb == NULL) | |
4656 | return gcb; | |
4657 | gcb->head.set = set; | |
4658 | return gcb; | |
4659 | } | |
4660 | EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc); | |
4661 | ||
e5009240 PNA |
4662 | /* |
4663 | * Stateful objects | |
4664 | */ | |
4665 | ||
4666 | /** | |
4667 | * nft_register_obj- register nf_tables stateful object type | |
4668 | * @obj: object type | |
4669 | * | |
4670 | * Registers the object type for use with nf_tables. Returns zero on | |
4671 | * success or a negative errno code otherwise. | |
4672 | */ | |
4673 | int nft_register_obj(struct nft_object_type *obj_type) | |
4674 | { | |
4675 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
4676 | return -EINVAL; | |
4677 | ||
4678 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
4679 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
4680 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
4681 | return 0; | |
4682 | } | |
4683 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
4684 | ||
4685 | /** | |
4686 | * nft_unregister_obj - unregister nf_tables object type | |
4687 | * @obj: object type | |
4688 | * | |
4689 | * Unregisters the object type for use with nf_tables. | |
4690 | */ | |
4691 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
4692 | { | |
4693 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
4694 | list_del_rcu(&obj_type->list); | |
4695 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
4696 | } | |
4697 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
4698 | ||
cac20fcd PNA |
4699 | struct nft_object *nft_obj_lookup(const struct nft_table *table, |
4700 | const struct nlattr *nla, u32 objtype, | |
4701 | u8 genmask) | |
e5009240 PNA |
4702 | { |
4703 | struct nft_object *obj; | |
4704 | ||
d9adf22a | 4705 | list_for_each_entry_rcu(obj, &table->objects, list) { |
e5009240 | 4706 | if (!nla_strcmp(nla, obj->name) && |
dfc46034 | 4707 | objtype == obj->ops->type->type && |
e5009240 PNA |
4708 | nft_active_genmask(obj, genmask)) |
4709 | return obj; | |
4710 | } | |
4711 | return ERR_PTR(-ENOENT); | |
4712 | } | |
cac20fcd | 4713 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 4714 | |
cac20fcd PNA |
4715 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
4716 | const struct nlattr *nla, | |
4717 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
4718 | { |
4719 | struct nft_object *obj; | |
4720 | ||
4721 | list_for_each_entry(obj, &table->objects, list) { | |
4722 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
4723 | objtype == obj->ops->type->type && | |
4724 | nft_active_genmask(obj, genmask)) | |
4725 | return obj; | |
4726 | } | |
4727 | return ERR_PTR(-ENOENT); | |
4728 | } | |
4729 | ||
e5009240 | 4730 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
4731 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
4732 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
4733 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
4734 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
4735 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
4736 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 4737 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
e5009240 PNA |
4738 | }; |
4739 | ||
84fba055 FW |
4740 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
4741 | const struct nft_object_type *type, | |
e5009240 PNA |
4742 | const struct nlattr *attr) |
4743 | { | |
5b4c6e38 | 4744 | struct nlattr **tb; |
dfc46034 | 4745 | const struct nft_object_ops *ops; |
e5009240 | 4746 | struct nft_object *obj; |
5b4c6e38 GS |
4747 | int err = -ENOMEM; |
4748 | ||
4749 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
4750 | if (!tb) | |
4751 | goto err1; | |
e5009240 PNA |
4752 | |
4753 | if (attr) { | |
fceb6435 JB |
4754 | err = nla_parse_nested(tb, type->maxattr, attr, type->policy, |
4755 | NULL); | |
e5009240 | 4756 | if (err < 0) |
5b4c6e38 | 4757 | goto err2; |
e5009240 PNA |
4758 | } else { |
4759 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
4760 | } | |
4761 | ||
dfc46034 PBG |
4762 | if (type->select_ops) { |
4763 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
4764 | if (IS_ERR(ops)) { | |
4765 | err = PTR_ERR(ops); | |
5b4c6e38 | 4766 | goto err2; |
dfc46034 PBG |
4767 | } |
4768 | } else { | |
4769 | ops = type->ops; | |
4770 | } | |
4771 | ||
e5009240 | 4772 | err = -ENOMEM; |
dfc46034 | 4773 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL); |
5b4c6e38 GS |
4774 | if (!obj) |
4775 | goto err2; | |
e5009240 | 4776 | |
dfc46034 | 4777 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 4778 | if (err < 0) |
5b4c6e38 | 4779 | goto err3; |
e5009240 | 4780 | |
dfc46034 PBG |
4781 | obj->ops = ops; |
4782 | ||
5b4c6e38 | 4783 | kfree(tb); |
e5009240 | 4784 | return obj; |
5b4c6e38 | 4785 | err3: |
e5009240 | 4786 | kfree(obj); |
5b4c6e38 GS |
4787 | err2: |
4788 | kfree(tb); | |
e5009240 PNA |
4789 | err1: |
4790 | return ERR_PTR(err); | |
4791 | } | |
4792 | ||
4793 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 4794 | struct nft_object *obj, bool reset) |
e5009240 PNA |
4795 | { |
4796 | struct nlattr *nest; | |
4797 | ||
4798 | nest = nla_nest_start(skb, attr); | |
4799 | if (!nest) | |
4800 | goto nla_put_failure; | |
dfc46034 | 4801 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
4802 | goto nla_put_failure; |
4803 | nla_nest_end(skb, nest); | |
4804 | return 0; | |
4805 | ||
4806 | nla_put_failure: | |
4807 | return -1; | |
4808 | } | |
4809 | ||
4810 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
4811 | { | |
4812 | const struct nft_object_type *type; | |
4813 | ||
4814 | list_for_each_entry(type, &nf_tables_objects, list) { | |
4815 | if (objtype == type->type) | |
4816 | return type; | |
4817 | } | |
4818 | return NULL; | |
4819 | } | |
4820 | ||
4821 | static const struct nft_object_type *nft_obj_type_get(u32 objtype) | |
4822 | { | |
4823 | const struct nft_object_type *type; | |
4824 | ||
4825 | type = __nft_obj_type_get(objtype); | |
4826 | if (type != NULL && try_module_get(type->owner)) | |
4827 | return type; | |
4828 | ||
4829 | #ifdef CONFIG_MODULES | |
4830 | if (type == NULL) { | |
4831 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
4832 | request_module("nft-obj-%u", objtype); | |
4833 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
4834 | if (__nft_obj_type_get(objtype)) | |
4835 | return ERR_PTR(-EAGAIN); | |
4836 | } | |
4837 | #endif | |
4838 | return ERR_PTR(-ENOENT); | |
4839 | } | |
4840 | ||
4841 | static int nf_tables_newobj(struct net *net, struct sock *nlsk, | |
4842 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
4843 | const struct nlattr * const nla[], |
4844 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
4845 | { |
4846 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
4847 | const struct nft_object_type *type; | |
4848 | u8 genmask = nft_genmask_next(net); | |
4849 | int family = nfmsg->nfgen_family; | |
e5009240 PNA |
4850 | struct nft_table *table; |
4851 | struct nft_object *obj; | |
4852 | struct nft_ctx ctx; | |
4853 | u32 objtype; | |
4854 | int err; | |
4855 | ||
4856 | if (!nla[NFTA_OBJ_TYPE] || | |
4857 | !nla[NFTA_OBJ_NAME] || | |
4858 | !nla[NFTA_OBJ_DATA]) | |
4859 | return -EINVAL; | |
4860 | ||
cac20fcd | 4861 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
4862 | if (IS_ERR(table)) { |
4863 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 4864 | return PTR_ERR(table); |
36dd1bcc | 4865 | } |
e5009240 PNA |
4866 | |
4867 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
cac20fcd | 4868 | obj = nft_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
4869 | if (IS_ERR(obj)) { |
4870 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
4871 | if (err != -ENOENT) { |
4872 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 4873 | return err; |
36dd1bcc | 4874 | } |
1a28ad74 | 4875 | } else { |
36dd1bcc PNA |
4876 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
4877 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 4878 | return -EEXIST; |
36dd1bcc | 4879 | } |
e5009240 PNA |
4880 | return 0; |
4881 | } | |
4882 | ||
98319cb9 | 4883 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e5009240 PNA |
4884 | |
4885 | type = nft_obj_type_get(objtype); | |
4886 | if (IS_ERR(type)) | |
4887 | return PTR_ERR(type); | |
4888 | ||
84fba055 | 4889 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
4890 | if (IS_ERR(obj)) { |
4891 | err = PTR_ERR(obj); | |
4892 | goto err1; | |
4893 | } | |
18965317 | 4894 | obj->table = table; |
3ecbfd65 HS |
4895 | obj->handle = nf_tables_alloc_handle(table); |
4896 | ||
61509575 PS |
4897 | obj->name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL); |
4898 | if (!obj->name) { | |
4899 | err = -ENOMEM; | |
4900 | goto err2; | |
4901 | } | |
e5009240 PNA |
4902 | |
4903 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
4904 | if (err < 0) | |
61509575 | 4905 | goto err3; |
e5009240 PNA |
4906 | |
4907 | list_add_tail_rcu(&obj->list, &table->objects); | |
4908 | table->use++; | |
4909 | return 0; | |
61509575 PS |
4910 | err3: |
4911 | kfree(obj->name); | |
e5009240 | 4912 | err2: |
dfc46034 | 4913 | if (obj->ops->destroy) |
00bfb320 | 4914 | obj->ops->destroy(&ctx, obj); |
e5009240 PNA |
4915 | kfree(obj); |
4916 | err1: | |
4917 | module_put(type->owner); | |
4918 | return err; | |
4919 | } | |
4920 | ||
4921 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
4922 | u32 portid, u32 seq, int event, u32 flags, | |
4923 | int family, const struct nft_table *table, | |
43da04a5 | 4924 | struct nft_object *obj, bool reset) |
e5009240 PNA |
4925 | { |
4926 | struct nfgenmsg *nfmsg; | |
4927 | struct nlmsghdr *nlh; | |
4928 | ||
dedb67c4 | 4929 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
e5009240 PNA |
4930 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
4931 | if (nlh == NULL) | |
4932 | goto nla_put_failure; | |
4933 | ||
4934 | nfmsg = nlmsg_data(nlh); | |
4935 | nfmsg->nfgen_family = family; | |
4936 | nfmsg->version = NFNETLINK_V0; | |
4937 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
4938 | ||
4939 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || | |
4940 | nla_put_string(skb, NFTA_OBJ_NAME, obj->name) || | |
dfc46034 | 4941 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || |
e5009240 | 4942 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || |
3ecbfd65 HS |
4943 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) || |
4944 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), | |
4945 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
4946 | goto nla_put_failure; |
4947 | ||
4948 | nlmsg_end(skb, nlh); | |
4949 | return 0; | |
4950 | ||
4951 | nla_put_failure: | |
4952 | nlmsg_trim(skb, nlh); | |
4953 | return -1; | |
4954 | } | |
4955 | ||
a9fea2a3 | 4956 | struct nft_obj_filter { |
e46abbcc | 4957 | char *table; |
a9fea2a3 PNA |
4958 | u32 type; |
4959 | }; | |
4960 | ||
e5009240 PNA |
4961 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
4962 | { | |
4963 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 4964 | const struct nft_table *table; |
e5009240 | 4965 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 4966 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
4967 | struct net *net = sock_net(skb->sk); |
4968 | int family = nfmsg->nfgen_family; | |
43da04a5 PNA |
4969 | struct nft_object *obj; |
4970 | bool reset = false; | |
4971 | ||
4972 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
4973 | reset = true; | |
e5009240 PNA |
4974 | |
4975 | rcu_read_lock(); | |
4976 | cb->seq = net->nft.base_seq; | |
4977 | ||
36596dad | 4978 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 4979 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
4980 | continue; |
4981 | ||
36596dad PNA |
4982 | list_for_each_entry_rcu(obj, &table->objects, list) { |
4983 | if (!nft_is_active(net, obj)) | |
4984 | goto cont; | |
4985 | if (idx < s_idx) | |
4986 | goto cont; | |
4987 | if (idx > s_idx) | |
4988 | memset(&cb->args[1], 0, | |
4989 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 4990 | if (filter && filter->table && |
36596dad PNA |
4991 | strcmp(filter->table, table->name)) |
4992 | goto cont; | |
4993 | if (filter && | |
4994 | filter->type != NFT_OBJECT_UNSPEC && | |
4995 | obj->ops->type->type != filter->type) | |
4996 | goto cont; | |
a9fea2a3 | 4997 | |
36596dad PNA |
4998 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
4999 | cb->nlh->nlmsg_seq, | |
5000 | NFT_MSG_NEWOBJ, | |
5001 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 5002 | table->family, table, |
36596dad PNA |
5003 | obj, reset) < 0) |
5004 | goto done; | |
e5009240 | 5005 | |
36596dad | 5006 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 5007 | cont: |
36596dad | 5008 | idx++; |
e5009240 PNA |
5009 | } |
5010 | } | |
5011 | done: | |
5012 | rcu_read_unlock(); | |
5013 | ||
5014 | cb->args[0] = idx; | |
5015 | return skb->len; | |
5016 | } | |
5017 | ||
90fd131a | 5018 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 5019 | { |
90fd131a FW |
5020 | const struct nlattr * const *nla = cb->data; |
5021 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 5022 | |
90fd131a FW |
5023 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
5024 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
5025 | if (!filter) | |
5026 | return -ENOMEM; | |
5027 | ||
5028 | if (nla[NFTA_OBJ_TABLE]) { | |
5029 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
5030 | if (!filter->table) { | |
5031 | kfree(filter); | |
5032 | return -ENOMEM; | |
5033 | } | |
5034 | } | |
5035 | ||
5036 | if (nla[NFTA_OBJ_TYPE]) | |
5037 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 5038 | } |
a9fea2a3 | 5039 | |
90fd131a | 5040 | cb->data = filter; |
a9fea2a3 PNA |
5041 | return 0; |
5042 | } | |
5043 | ||
90fd131a | 5044 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 5045 | { |
90fd131a | 5046 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 5047 | |
90fd131a FW |
5048 | if (filter) { |
5049 | kfree(filter->table); | |
5050 | kfree(filter); | |
e46abbcc | 5051 | } |
a9fea2a3 | 5052 | |
90fd131a | 5053 | return 0; |
a9fea2a3 PNA |
5054 | } |
5055 | ||
d9adf22a | 5056 | /* called with rcu_read_lock held */ |
e5009240 PNA |
5057 | static int nf_tables_getobj(struct net *net, struct sock *nlsk, |
5058 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5059 | const struct nlattr * const nla[], |
5060 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
5061 | { |
5062 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5063 | u8 genmask = nft_genmask_cur(net); | |
5064 | int family = nfmsg->nfgen_family; | |
e5009240 PNA |
5065 | const struct nft_table *table; |
5066 | struct nft_object *obj; | |
5067 | struct sk_buff *skb2; | |
43da04a5 | 5068 | bool reset = false; |
e5009240 PNA |
5069 | u32 objtype; |
5070 | int err; | |
5071 | ||
5072 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
5073 | struct netlink_dump_control c = { | |
90fd131a | 5074 | .start = nf_tables_dump_obj_start, |
e5009240 | 5075 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 5076 | .done = nf_tables_dump_obj_done, |
d9adf22a | 5077 | .module = THIS_MODULE, |
90fd131a | 5078 | .data = (void *)nla, |
e5009240 | 5079 | }; |
a9fea2a3 | 5080 | |
d9adf22a | 5081 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
e5009240 PNA |
5082 | } |
5083 | ||
5084 | if (!nla[NFTA_OBJ_NAME] || | |
5085 | !nla[NFTA_OBJ_TYPE]) | |
5086 | return -EINVAL; | |
5087 | ||
cac20fcd | 5088 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
5089 | if (IS_ERR(table)) { |
5090 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 5091 | return PTR_ERR(table); |
36dd1bcc | 5092 | } |
e5009240 PNA |
5093 | |
5094 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
cac20fcd | 5095 | obj = nft_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
5096 | if (IS_ERR(obj)) { |
5097 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 5098 | return PTR_ERR(obj); |
36dd1bcc | 5099 | } |
e5009240 | 5100 | |
d9adf22a | 5101 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
5102 | if (!skb2) |
5103 | return -ENOMEM; | |
5104 | ||
43da04a5 PNA |
5105 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
5106 | reset = true; | |
5107 | ||
e5009240 PNA |
5108 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
5109 | nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, | |
43da04a5 | 5110 | family, table, obj, reset); |
e5009240 PNA |
5111 | if (err < 0) |
5112 | goto err; | |
5113 | ||
5114 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
5115 | err: | |
5116 | kfree_skb(skb2); | |
5117 | return err; | |
e5009240 PNA |
5118 | } |
5119 | ||
00bfb320 | 5120 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 5121 | { |
dfc46034 | 5122 | if (obj->ops->destroy) |
00bfb320 | 5123 | obj->ops->destroy(ctx, obj); |
e5009240 | 5124 | |
dfc46034 | 5125 | module_put(obj->ops->type->owner); |
61509575 | 5126 | kfree(obj->name); |
e5009240 PNA |
5127 | kfree(obj); |
5128 | } | |
5129 | ||
5130 | static int nf_tables_delobj(struct net *net, struct sock *nlsk, | |
04ba724b PNA |
5131 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
5132 | const struct nlattr * const nla[], | |
5133 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
5134 | { |
5135 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5136 | u8 genmask = nft_genmask_next(net); | |
5137 | int family = nfmsg->nfgen_family; | |
36dd1bcc | 5138 | const struct nlattr *attr; |
e5009240 PNA |
5139 | struct nft_table *table; |
5140 | struct nft_object *obj; | |
5141 | struct nft_ctx ctx; | |
5142 | u32 objtype; | |
5143 | ||
5144 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 5145 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
5146 | return -EINVAL; |
5147 | ||
cac20fcd | 5148 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
5149 | if (IS_ERR(table)) { |
5150 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 5151 | return PTR_ERR(table); |
36dd1bcc | 5152 | } |
e5009240 PNA |
5153 | |
5154 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
5155 | if (nla[NFTA_OBJ_HANDLE]) { |
5156 | attr = nla[NFTA_OBJ_HANDLE]; | |
5157 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
5158 | } else { | |
5159 | attr = nla[NFTA_OBJ_NAME]; | |
5160 | obj = nft_obj_lookup(table, attr, objtype, genmask); | |
5161 | } | |
5162 | ||
5163 | if (IS_ERR(obj)) { | |
5164 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 5165 | return PTR_ERR(obj); |
36dd1bcc PNA |
5166 | } |
5167 | if (obj->use > 0) { | |
5168 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 5169 | return -EBUSY; |
36dd1bcc | 5170 | } |
e5009240 | 5171 | |
98319cb9 | 5172 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e5009240 PNA |
5173 | |
5174 | return nft_delobj(&ctx, obj); | |
5175 | } | |
5176 | ||
25e94a99 PNA |
5177 | void nft_obj_notify(struct net *net, struct nft_table *table, |
5178 | struct nft_object *obj, u32 portid, u32 seq, int event, | |
5179 | int family, int report, gfp_t gfp) | |
e5009240 PNA |
5180 | { |
5181 | struct sk_buff *skb; | |
5182 | int err; | |
5183 | ||
2599e989 PNA |
5184 | if (!report && |
5185 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 5186 | return; |
e5009240 | 5187 | |
2599e989 | 5188 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
5189 | if (skb == NULL) |
5190 | goto err; | |
5191 | ||
2599e989 PNA |
5192 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family, |
5193 | table, obj, false); | |
e5009240 PNA |
5194 | if (err < 0) { |
5195 | kfree_skb(skb); | |
5196 | goto err; | |
5197 | } | |
5198 | ||
25e94a99 PNA |
5199 | nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp); |
5200 | return; | |
e5009240 | 5201 | err: |
25e94a99 | 5202 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 5203 | } |
2599e989 PNA |
5204 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
5205 | ||
25e94a99 PNA |
5206 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
5207 | struct nft_object *obj, int event) | |
2599e989 | 5208 | { |
25e94a99 | 5209 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
36596dad | 5210 | ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 5211 | } |
e5009240 | 5212 | |
3b49e2e9 PNA |
5213 | /* |
5214 | * Flow tables | |
5215 | */ | |
5216 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
5217 | { | |
5218 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
5219 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
5220 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
5221 | } | |
5222 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
5223 | ||
5224 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
5225 | { | |
5226 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
5227 | list_del_rcu(&type->list); | |
5228 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
5229 | } | |
5230 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
5231 | ||
5232 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
5233 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
5234 | .len = NFT_NAME_MAXLEN - 1 }, | |
5235 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
5236 | .len = NFT_NAME_MAXLEN - 1 }, | |
5237 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 5238 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
3b49e2e9 PNA |
5239 | }; |
5240 | ||
cac20fcd PNA |
5241 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
5242 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
5243 | { |
5244 | struct nft_flowtable *flowtable; | |
5245 | ||
d9adf22a | 5246 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
5247 | if (!nla_strcmp(nla, flowtable->name) && |
5248 | nft_active_genmask(flowtable, genmask)) | |
5249 | return flowtable; | |
5250 | } | |
5251 | return ERR_PTR(-ENOENT); | |
5252 | } | |
cac20fcd | 5253 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 5254 | |
ae0662f8 | 5255 | static struct nft_flowtable * |
cac20fcd PNA |
5256 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
5257 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
5258 | { |
5259 | struct nft_flowtable *flowtable; | |
5260 | ||
5261 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
5262 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
5263 | nft_active_genmask(flowtable, genmask)) | |
5264 | return flowtable; | |
5265 | } | |
5266 | return ERR_PTR(-ENOENT); | |
5267 | } | |
5268 | ||
3b49e2e9 PNA |
5269 | static int nf_tables_parse_devices(const struct nft_ctx *ctx, |
5270 | const struct nlattr *attr, | |
5271 | struct net_device *dev_array[], int *len) | |
5272 | { | |
5273 | const struct nlattr *tmp; | |
5274 | struct net_device *dev; | |
5275 | char ifname[IFNAMSIZ]; | |
5276 | int rem, n = 0, err; | |
5277 | ||
5278 | nla_for_each_nested(tmp, attr, rem) { | |
5279 | if (nla_type(tmp) != NFTA_DEVICE_NAME) { | |
5280 | err = -EINVAL; | |
5281 | goto err1; | |
5282 | } | |
5283 | ||
5284 | nla_strlcpy(ifname, tmp, IFNAMSIZ); | |
90d2723c | 5285 | dev = __dev_get_by_name(ctx->net, ifname); |
3b49e2e9 PNA |
5286 | if (!dev) { |
5287 | err = -ENOENT; | |
5288 | goto err1; | |
5289 | } | |
5290 | ||
5291 | dev_array[n++] = dev; | |
5292 | if (n == NFT_FLOWTABLE_DEVICE_MAX) { | |
5293 | err = -EFBIG; | |
5294 | goto err1; | |
5295 | } | |
5296 | } | |
5297 | if (!len) | |
5298 | return -EINVAL; | |
5299 | ||
5300 | err = 0; | |
5301 | err1: | |
5302 | *len = n; | |
5303 | return err; | |
5304 | } | |
5305 | ||
5306 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { | |
5307 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
5308 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
5309 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
5310 | }; | |
5311 | ||
5312 | static int nf_tables_flowtable_parse_hook(const struct nft_ctx *ctx, | |
5313 | const struct nlattr *attr, | |
5314 | struct nft_flowtable *flowtable) | |
5315 | { | |
5316 | struct net_device *dev_array[NFT_FLOWTABLE_DEVICE_MAX]; | |
5317 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; | |
5318 | struct nf_hook_ops *ops; | |
5319 | int hooknum, priority; | |
5320 | int err, n = 0, i; | |
5321 | ||
5322 | err = nla_parse_nested(tb, NFTA_FLOWTABLE_HOOK_MAX, attr, | |
5323 | nft_flowtable_hook_policy, NULL); | |
5324 | if (err < 0) | |
5325 | return err; | |
5326 | ||
5327 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
5328 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY] || | |
5329 | !tb[NFTA_FLOWTABLE_HOOK_DEVS]) | |
5330 | return -EINVAL; | |
5331 | ||
5332 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
fe19c04c | 5333 | if (hooknum != NF_NETDEV_INGRESS) |
3b49e2e9 PNA |
5334 | return -EINVAL; |
5335 | ||
5336 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
5337 | ||
5338 | err = nf_tables_parse_devices(ctx, tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
5339 | dev_array, &n); | |
5340 | if (err < 0) | |
d92191aa | 5341 | return err; |
3b49e2e9 | 5342 | |
6396bb22 | 5343 | ops = kcalloc(n, sizeof(struct nf_hook_ops), GFP_KERNEL); |
90d2723c PNA |
5344 | if (!ops) |
5345 | return -ENOMEM; | |
3b49e2e9 | 5346 | |
0e839dfa PNA |
5347 | flowtable->hooknum = hooknum; |
5348 | flowtable->priority = priority; | |
3b49e2e9 PNA |
5349 | flowtable->ops = ops; |
5350 | flowtable->ops_len = n; | |
5351 | ||
5352 | for (i = 0; i < n; i++) { | |
5353 | flowtable->ops[i].pf = NFPROTO_NETDEV; | |
5354 | flowtable->ops[i].hooknum = hooknum; | |
5355 | flowtable->ops[i].priority = priority; | |
17857d92 | 5356 | flowtable->ops[i].priv = &flowtable->data; |
3b49e2e9 PNA |
5357 | flowtable->ops[i].hook = flowtable->data.type->hook; |
5358 | flowtable->ops[i].dev = dev_array[i]; | |
5359 | } | |
5360 | ||
3b49e2e9 PNA |
5361 | return err; |
5362 | } | |
5363 | ||
98319cb9 | 5364 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
5365 | { |
5366 | const struct nf_flowtable_type *type; | |
5367 | ||
5368 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 5369 | if (family == type->family) |
3b49e2e9 PNA |
5370 | return type; |
5371 | } | |
5372 | return NULL; | |
5373 | } | |
5374 | ||
98319cb9 | 5375 | static const struct nf_flowtable_type *nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
5376 | { |
5377 | const struct nf_flowtable_type *type; | |
5378 | ||
98319cb9 | 5379 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
5380 | if (type != NULL && try_module_get(type->owner)) |
5381 | return type; | |
5382 | ||
5383 | #ifdef CONFIG_MODULES | |
5384 | if (type == NULL) { | |
5385 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
98319cb9 | 5386 | request_module("nf-flowtable-%u", family); |
3b49e2e9 | 5387 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
98319cb9 | 5388 | if (__nft_flowtable_type_get(family)) |
3b49e2e9 PNA |
5389 | return ERR_PTR(-EAGAIN); |
5390 | } | |
5391 | #endif | |
5392 | return ERR_PTR(-ENOENT); | |
5393 | } | |
5394 | ||
3b49e2e9 PNA |
5395 | static void nft_unregister_flowtable_net_hooks(struct net *net, |
5396 | struct nft_flowtable *flowtable) | |
5397 | { | |
5398 | int i; | |
5399 | ||
5400 | for (i = 0; i < flowtable->ops_len; i++) { | |
5401 | if (!flowtable->ops[i].dev) | |
5402 | continue; | |
5403 | ||
5404 | nf_unregister_net_hook(net, &flowtable->ops[i]); | |
5405 | } | |
5406 | } | |
5407 | ||
5408 | static int nf_tables_newflowtable(struct net *net, struct sock *nlsk, | |
5409 | struct sk_buff *skb, | |
5410 | const struct nlmsghdr *nlh, | |
5411 | const struct nlattr * const nla[], | |
5412 | struct netlink_ext_ack *extack) | |
5413 | { | |
5414 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5415 | const struct nf_flowtable_type *type; | |
32fc7187 | 5416 | struct nft_flowtable *flowtable, *ft; |
3b49e2e9 PNA |
5417 | u8 genmask = nft_genmask_next(net); |
5418 | int family = nfmsg->nfgen_family; | |
3b49e2e9 PNA |
5419 | struct nft_table *table; |
5420 | struct nft_ctx ctx; | |
5421 | int err, i, k; | |
5422 | ||
5423 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
5424 | !nla[NFTA_FLOWTABLE_NAME] || | |
5425 | !nla[NFTA_FLOWTABLE_HOOK]) | |
5426 | return -EINVAL; | |
5427 | ||
cac20fcd PNA |
5428 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
5429 | genmask); | |
36dd1bcc PNA |
5430 | if (IS_ERR(table)) { |
5431 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 5432 | return PTR_ERR(table); |
36dd1bcc | 5433 | } |
3b49e2e9 | 5434 | |
cac20fcd PNA |
5435 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
5436 | genmask); | |
3b49e2e9 PNA |
5437 | if (IS_ERR(flowtable)) { |
5438 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
5439 | if (err != -ENOENT) { |
5440 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 5441 | return err; |
36dd1bcc | 5442 | } |
3b49e2e9 | 5443 | } else { |
36dd1bcc PNA |
5444 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
5445 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 5446 | return -EEXIST; |
36dd1bcc | 5447 | } |
3b49e2e9 PNA |
5448 | |
5449 | return 0; | |
5450 | } | |
5451 | ||
98319cb9 | 5452 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
3b49e2e9 PNA |
5453 | |
5454 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL); | |
5455 | if (!flowtable) | |
5456 | return -ENOMEM; | |
5457 | ||
5458 | flowtable->table = table; | |
3ecbfd65 HS |
5459 | flowtable->handle = nf_tables_alloc_handle(table); |
5460 | ||
3b49e2e9 PNA |
5461 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL); |
5462 | if (!flowtable->name) { | |
5463 | err = -ENOMEM; | |
5464 | goto err1; | |
5465 | } | |
5466 | ||
98319cb9 | 5467 | type = nft_flowtable_type_get(family); |
3b49e2e9 PNA |
5468 | if (IS_ERR(type)) { |
5469 | err = PTR_ERR(type); | |
5470 | goto err2; | |
5471 | } | |
5472 | ||
5473 | flowtable->data.type = type; | |
a268de77 | 5474 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
5475 | if (err < 0) |
5476 | goto err3; | |
5477 | ||
5478 | err = nf_tables_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5479 | flowtable); | |
5480 | if (err < 0) | |
a268de77 | 5481 | goto err4; |
3b49e2e9 PNA |
5482 | |
5483 | for (i = 0; i < flowtable->ops_len; i++) { | |
32fc7187 PNA |
5484 | if (!flowtable->ops[i].dev) |
5485 | continue; | |
5486 | ||
5487 | list_for_each_entry(ft, &table->flowtables, list) { | |
5488 | for (k = 0; k < ft->ops_len; k++) { | |
5489 | if (!ft->ops[k].dev) | |
5490 | continue; | |
5491 | ||
5492 | if (flowtable->ops[i].dev == ft->ops[k].dev && | |
5493 | flowtable->ops[i].pf == ft->ops[k].pf) { | |
5494 | err = -EBUSY; | |
a268de77 | 5495 | goto err5; |
32fc7187 PNA |
5496 | } |
5497 | } | |
5498 | } | |
5499 | ||
3b49e2e9 PNA |
5500 | err = nf_register_net_hook(net, &flowtable->ops[i]); |
5501 | if (err < 0) | |
a268de77 | 5502 | goto err5; |
3b49e2e9 PNA |
5503 | } |
5504 | ||
5505 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
5506 | if (err < 0) | |
a268de77 | 5507 | goto err6; |
3b49e2e9 PNA |
5508 | |
5509 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
5510 | table->use++; | |
5511 | ||
5512 | return 0; | |
a268de77 | 5513 | err6: |
3b49e2e9 | 5514 | i = flowtable->ops_len; |
a268de77 | 5515 | err5: |
b8088dda | 5516 | for (k = i - 1; k >= 0; k--) |
0e0d5002 | 5517 | nf_unregister_net_hook(net, &flowtable->ops[k]); |
3b49e2e9 PNA |
5518 | |
5519 | kfree(flowtable->ops); | |
a268de77 FF |
5520 | err4: |
5521 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
5522 | err3: |
5523 | module_put(type->owner); | |
5524 | err2: | |
5525 | kfree(flowtable->name); | |
5526 | err1: | |
5527 | kfree(flowtable); | |
5528 | return err; | |
5529 | } | |
5530 | ||
5531 | static int nf_tables_delflowtable(struct net *net, struct sock *nlsk, | |
5532 | struct sk_buff *skb, | |
5533 | const struct nlmsghdr *nlh, | |
5534 | const struct nlattr * const nla[], | |
5535 | struct netlink_ext_ack *extack) | |
5536 | { | |
5537 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5538 | u8 genmask = nft_genmask_next(net); | |
5539 | int family = nfmsg->nfgen_family; | |
5540 | struct nft_flowtable *flowtable; | |
36dd1bcc | 5541 | const struct nlattr *attr; |
3b49e2e9 PNA |
5542 | struct nft_table *table; |
5543 | struct nft_ctx ctx; | |
5544 | ||
e603ea4b PNA |
5545 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
5546 | (!nla[NFTA_FLOWTABLE_NAME] && | |
5547 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
5548 | return -EINVAL; | |
5549 | ||
cac20fcd PNA |
5550 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
5551 | genmask); | |
36dd1bcc PNA |
5552 | if (IS_ERR(table)) { |
5553 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 5554 | return PTR_ERR(table); |
36dd1bcc | 5555 | } |
3b49e2e9 | 5556 | |
36dd1bcc PNA |
5557 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
5558 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
5559 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
5560 | } else { | |
5561 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
5562 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
5563 | } | |
5564 | ||
5565 | if (IS_ERR(flowtable)) { | |
5566 | NL_SET_BAD_ATTR(extack, attr); | |
5567 | return PTR_ERR(flowtable); | |
5568 | } | |
5569 | if (flowtable->use > 0) { | |
5570 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 5571 | return -EBUSY; |
36dd1bcc | 5572 | } |
3b49e2e9 | 5573 | |
98319cb9 | 5574 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
3b49e2e9 PNA |
5575 | |
5576 | return nft_delflowtable(&ctx, flowtable); | |
5577 | } | |
5578 | ||
5579 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
5580 | u32 portid, u32 seq, int event, | |
5581 | u32 flags, int family, | |
5582 | struct nft_flowtable *flowtable) | |
5583 | { | |
5584 | struct nlattr *nest, *nest_devs; | |
5585 | struct nfgenmsg *nfmsg; | |
5586 | struct nlmsghdr *nlh; | |
5587 | int i; | |
5588 | ||
5589 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
5590 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); | |
5591 | if (nlh == NULL) | |
5592 | goto nla_put_failure; | |
5593 | ||
5594 | nfmsg = nlmsg_data(nlh); | |
5595 | nfmsg->nfgen_family = family; | |
5596 | nfmsg->version = NFNETLINK_V0; | |
5597 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
5598 | ||
5599 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || | |
5600 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 HS |
5601 | nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || |
5602 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), | |
5603 | NFTA_FLOWTABLE_PAD)) | |
3b49e2e9 PNA |
5604 | goto nla_put_failure; |
5605 | ||
5606 | nest = nla_nest_start(skb, NFTA_FLOWTABLE_HOOK); | |
5607 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || | |
5608 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->priority))) | |
5609 | goto nla_put_failure; | |
5610 | ||
5611 | nest_devs = nla_nest_start(skb, NFTA_FLOWTABLE_HOOK_DEVS); | |
5612 | if (!nest_devs) | |
5613 | goto nla_put_failure; | |
5614 | ||
5615 | for (i = 0; i < flowtable->ops_len; i++) { | |
b8088dda FW |
5616 | const struct net_device *dev = READ_ONCE(flowtable->ops[i].dev); |
5617 | ||
5618 | if (dev && | |
5619 | nla_put_string(skb, NFTA_DEVICE_NAME, dev->name)) | |
3b49e2e9 PNA |
5620 | goto nla_put_failure; |
5621 | } | |
5622 | nla_nest_end(skb, nest_devs); | |
5623 | nla_nest_end(skb, nest); | |
5624 | ||
5625 | nlmsg_end(skb, nlh); | |
5626 | return 0; | |
5627 | ||
5628 | nla_put_failure: | |
5629 | nlmsg_trim(skb, nlh); | |
5630 | return -1; | |
5631 | } | |
5632 | ||
5633 | struct nft_flowtable_filter { | |
5634 | char *table; | |
5635 | }; | |
5636 | ||
5637 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
5638 | struct netlink_callback *cb) | |
5639 | { | |
5640 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
5641 | struct nft_flowtable_filter *filter = cb->data; | |
5642 | unsigned int idx = 0, s_idx = cb->args[0]; | |
5643 | struct net *net = sock_net(skb->sk); | |
5644 | int family = nfmsg->nfgen_family; | |
5645 | struct nft_flowtable *flowtable; | |
3b49e2e9 PNA |
5646 | const struct nft_table *table; |
5647 | ||
5648 | rcu_read_lock(); | |
5649 | cb->seq = net->nft.base_seq; | |
5650 | ||
36596dad | 5651 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 5652 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
5653 | continue; |
5654 | ||
36596dad PNA |
5655 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
5656 | if (!nft_is_active(net, flowtable)) | |
5657 | goto cont; | |
5658 | if (idx < s_idx) | |
5659 | goto cont; | |
5660 | if (idx > s_idx) | |
5661 | memset(&cb->args[1], 0, | |
5662 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 5663 | if (filter && filter->table && |
36596dad PNA |
5664 | strcmp(filter->table, table->name)) |
5665 | goto cont; | |
3b49e2e9 | 5666 | |
36596dad PNA |
5667 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
5668 | cb->nlh->nlmsg_seq, | |
5669 | NFT_MSG_NEWFLOWTABLE, | |
5670 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 5671 | table->family, flowtable) < 0) |
36596dad | 5672 | goto done; |
3b49e2e9 | 5673 | |
36596dad | 5674 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 5675 | cont: |
36596dad | 5676 | idx++; |
3b49e2e9 PNA |
5677 | } |
5678 | } | |
5679 | done: | |
5680 | rcu_read_unlock(); | |
5681 | ||
5682 | cb->args[0] = idx; | |
5683 | return skb->len; | |
5684 | } | |
5685 | ||
90fd131a | 5686 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 5687 | { |
90fd131a FW |
5688 | const struct nlattr * const *nla = cb->data; |
5689 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 5690 | |
90fd131a FW |
5691 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
5692 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
5693 | if (!filter) | |
5694 | return -ENOMEM; | |
3b49e2e9 | 5695 | |
90fd131a FW |
5696 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
5697 | GFP_ATOMIC); | |
5698 | if (!filter->table) { | |
5699 | kfree(filter); | |
5700 | return -ENOMEM; | |
5701 | } | |
5702 | } | |
3b49e2e9 | 5703 | |
90fd131a | 5704 | cb->data = filter; |
3b49e2e9 PNA |
5705 | return 0; |
5706 | } | |
5707 | ||
90fd131a | 5708 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 5709 | { |
90fd131a | 5710 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 5711 | |
3b49e2e9 | 5712 | if (!filter) |
90fd131a | 5713 | return 0; |
3b49e2e9 | 5714 | |
90fd131a FW |
5715 | kfree(filter->table); |
5716 | kfree(filter); | |
5717 | ||
5718 | return 0; | |
3b49e2e9 PNA |
5719 | } |
5720 | ||
d9adf22a | 5721 | /* called with rcu_read_lock held */ |
3b49e2e9 PNA |
5722 | static int nf_tables_getflowtable(struct net *net, struct sock *nlsk, |
5723 | struct sk_buff *skb, | |
5724 | const struct nlmsghdr *nlh, | |
5725 | const struct nlattr * const nla[], | |
5726 | struct netlink_ext_ack *extack) | |
5727 | { | |
5728 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5729 | u8 genmask = nft_genmask_cur(net); | |
5730 | int family = nfmsg->nfgen_family; | |
5731 | struct nft_flowtable *flowtable; | |
3b49e2e9 PNA |
5732 | const struct nft_table *table; |
5733 | struct sk_buff *skb2; | |
5734 | int err; | |
5735 | ||
5736 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
5737 | struct netlink_dump_control c = { | |
90fd131a | 5738 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
5739 | .dump = nf_tables_dump_flowtable, |
5740 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 5741 | .module = THIS_MODULE, |
90fd131a | 5742 | .data = (void *)nla, |
3b49e2e9 PNA |
5743 | }; |
5744 | ||
d9adf22a | 5745 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
3b49e2e9 PNA |
5746 | } |
5747 | ||
5748 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
5749 | return -EINVAL; | |
5750 | ||
cac20fcd PNA |
5751 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
5752 | genmask); | |
3b49e2e9 PNA |
5753 | if (IS_ERR(table)) |
5754 | return PTR_ERR(table); | |
5755 | ||
cac20fcd PNA |
5756 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
5757 | genmask); | |
03a0120f | 5758 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
5759 | return PTR_ERR(flowtable); |
5760 | ||
d9adf22a | 5761 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
5762 | if (!skb2) |
5763 | return -ENOMEM; | |
5764 | ||
5765 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
5766 | nlh->nlmsg_seq, | |
5767 | NFT_MSG_NEWFLOWTABLE, 0, family, | |
5768 | flowtable); | |
5769 | if (err < 0) | |
5770 | goto err; | |
5771 | ||
5772 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
5773 | err: | |
5774 | kfree_skb(skb2); | |
5775 | return err; | |
5776 | } | |
5777 | ||
5778 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
5779 | struct nft_flowtable *flowtable, | |
5780 | int event) | |
5781 | { | |
5782 | struct sk_buff *skb; | |
5783 | int err; | |
5784 | ||
5785 | if (ctx->report && | |
5786 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
5787 | return; | |
5788 | ||
5789 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
5790 | if (skb == NULL) | |
5791 | goto err; | |
5792 | ||
5793 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, | |
5794 | ctx->seq, event, 0, | |
36596dad | 5795 | ctx->family, flowtable); |
3b49e2e9 PNA |
5796 | if (err < 0) { |
5797 | kfree_skb(skb); | |
5798 | goto err; | |
5799 | } | |
5800 | ||
5801 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, | |
5802 | ctx->report, GFP_KERNEL); | |
5803 | return; | |
5804 | err: | |
5805 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
5806 | } | |
5807 | ||
3b49e2e9 PNA |
5808 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
5809 | { | |
c04a3f73 | 5810 | kfree(flowtable->ops); |
3b49e2e9 | 5811 | kfree(flowtable->name); |
b408c5b0 | 5812 | flowtable->data.type->free(&flowtable->data); |
3b49e2e9 | 5813 | module_put(flowtable->data.type->owner); |
a12486eb | 5814 | kfree(flowtable); |
3b49e2e9 PNA |
5815 | } |
5816 | ||
84d7fce6 PNA |
5817 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
5818 | u32 portid, u32 seq) | |
5819 | { | |
5820 | struct nlmsghdr *nlh; | |
5821 | struct nfgenmsg *nfmsg; | |
784b4e61 | 5822 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 5823 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 PNA |
5824 | |
5825 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0); | |
5826 | if (nlh == NULL) | |
5827 | goto nla_put_failure; | |
5828 | ||
5829 | nfmsg = nlmsg_data(nlh); | |
5830 | nfmsg->nfgen_family = AF_UNSPEC; | |
5831 | nfmsg->version = NFNETLINK_V0; | |
5832 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
5833 | ||
784b4e61 PS |
5834 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)) || |
5835 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || | |
5836 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
5837 | goto nla_put_failure; |
5838 | ||
053c095a JB |
5839 | nlmsg_end(skb, nlh); |
5840 | return 0; | |
84d7fce6 PNA |
5841 | |
5842 | nla_put_failure: | |
5843 | nlmsg_trim(skb, nlh); | |
5844 | return -EMSGSIZE; | |
5845 | } | |
5846 | ||
3b49e2e9 PNA |
5847 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
5848 | struct nft_flowtable *flowtable) | |
5849 | { | |
5850 | int i; | |
5851 | ||
5852 | for (i = 0; i < flowtable->ops_len; i++) { | |
5853 | if (flowtable->ops[i].dev != dev) | |
5854 | continue; | |
5855 | ||
5856 | nf_unregister_net_hook(dev_net(dev), &flowtable->ops[i]); | |
5857 | flowtable->ops[i].dev = NULL; | |
5858 | break; | |
5859 | } | |
5860 | } | |
5861 | ||
5862 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
5863 | unsigned long event, void *ptr) | |
5864 | { | |
5865 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
5866 | struct nft_flowtable *flowtable; | |
5867 | struct nft_table *table; | |
0a2cf5ee | 5868 | struct net *net; |
3b49e2e9 PNA |
5869 | |
5870 | if (event != NETDEV_UNREGISTER) | |
5871 | return 0; | |
5872 | ||
0a2cf5ee FW |
5873 | net = maybe_get_net(dev_net(dev)); |
5874 | if (!net) | |
5875 | return 0; | |
5876 | ||
3b49e2e9 | 5877 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
0a2cf5ee | 5878 | list_for_each_entry(table, &net->nft.tables, list) { |
36596dad PNA |
5879 | list_for_each_entry(flowtable, &table->flowtables, list) { |
5880 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
5881 | } |
5882 | } | |
5883 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
0a2cf5ee | 5884 | put_net(net); |
3b49e2e9 PNA |
5885 | return NOTIFY_DONE; |
5886 | } | |
5887 | ||
5888 | static struct notifier_block nf_tables_flowtable_notifier = { | |
5889 | .notifier_call = nf_tables_flowtable_event, | |
5890 | }; | |
5891 | ||
25e94a99 PNA |
5892 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
5893 | int event) | |
84d7fce6 PNA |
5894 | { |
5895 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
5896 | struct sk_buff *skb2; | |
5897 | int err; | |
5898 | ||
5899 | if (nlmsg_report(nlh) && | |
5900 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 5901 | return; |
84d7fce6 | 5902 | |
84d7fce6 PNA |
5903 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
5904 | if (skb2 == NULL) | |
5905 | goto err; | |
5906 | ||
5907 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
5908 | nlh->nlmsg_seq); | |
5909 | if (err < 0) { | |
5910 | kfree_skb(skb2); | |
5911 | goto err; | |
5912 | } | |
5913 | ||
25e94a99 PNA |
5914 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
5915 | nlmsg_report(nlh), GFP_KERNEL); | |
5916 | return; | |
84d7fce6 | 5917 | err: |
25e94a99 PNA |
5918 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
5919 | -ENOBUFS); | |
84d7fce6 PNA |
5920 | } |
5921 | ||
7b8002a1 PNA |
5922 | static int nf_tables_getgen(struct net *net, struct sock *nlsk, |
5923 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5924 | const struct nlattr * const nla[], |
5925 | struct netlink_ext_ack *extack) | |
84d7fce6 | 5926 | { |
84d7fce6 PNA |
5927 | struct sk_buff *skb2; |
5928 | int err; | |
5929 | ||
d9adf22a | 5930 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
5931 | if (skb2 == NULL) |
5932 | return -ENOMEM; | |
5933 | ||
5934 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
5935 | nlh->nlmsg_seq); | |
5936 | if (err < 0) | |
5937 | goto err; | |
5938 | ||
5939 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
5940 | err: | |
5941 | kfree_skb(skb2); | |
5942 | return err; | |
5943 | } | |
5944 | ||
96518518 PM |
5945 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
5946 | [NFT_MSG_NEWTABLE] = { | |
55dd6f93 | 5947 | .call_batch = nf_tables_newtable, |
96518518 PM |
5948 | .attr_count = NFTA_TABLE_MAX, |
5949 | .policy = nft_table_policy, | |
5950 | }, | |
5951 | [NFT_MSG_GETTABLE] = { | |
d9adf22a | 5952 | .call_rcu = nf_tables_gettable, |
96518518 PM |
5953 | .attr_count = NFTA_TABLE_MAX, |
5954 | .policy = nft_table_policy, | |
5955 | }, | |
5956 | [NFT_MSG_DELTABLE] = { | |
55dd6f93 | 5957 | .call_batch = nf_tables_deltable, |
96518518 PM |
5958 | .attr_count = NFTA_TABLE_MAX, |
5959 | .policy = nft_table_policy, | |
5960 | }, | |
5961 | [NFT_MSG_NEWCHAIN] = { | |
91c7b38d | 5962 | .call_batch = nf_tables_newchain, |
96518518 PM |
5963 | .attr_count = NFTA_CHAIN_MAX, |
5964 | .policy = nft_chain_policy, | |
5965 | }, | |
5966 | [NFT_MSG_GETCHAIN] = { | |
d9adf22a | 5967 | .call_rcu = nf_tables_getchain, |
96518518 PM |
5968 | .attr_count = NFTA_CHAIN_MAX, |
5969 | .policy = nft_chain_policy, | |
5970 | }, | |
5971 | [NFT_MSG_DELCHAIN] = { | |
91c7b38d | 5972 | .call_batch = nf_tables_delchain, |
96518518 PM |
5973 | .attr_count = NFTA_CHAIN_MAX, |
5974 | .policy = nft_chain_policy, | |
5975 | }, | |
5976 | [NFT_MSG_NEWRULE] = { | |
0628b123 | 5977 | .call_batch = nf_tables_newrule, |
96518518 PM |
5978 | .attr_count = NFTA_RULE_MAX, |
5979 | .policy = nft_rule_policy, | |
5980 | }, | |
5981 | [NFT_MSG_GETRULE] = { | |
d9adf22a | 5982 | .call_rcu = nf_tables_getrule, |
96518518 PM |
5983 | .attr_count = NFTA_RULE_MAX, |
5984 | .policy = nft_rule_policy, | |
5985 | }, | |
5986 | [NFT_MSG_DELRULE] = { | |
0628b123 | 5987 | .call_batch = nf_tables_delrule, |
96518518 PM |
5988 | .attr_count = NFTA_RULE_MAX, |
5989 | .policy = nft_rule_policy, | |
5990 | }, | |
20a69341 | 5991 | [NFT_MSG_NEWSET] = { |
958bee14 | 5992 | .call_batch = nf_tables_newset, |
20a69341 PM |
5993 | .attr_count = NFTA_SET_MAX, |
5994 | .policy = nft_set_policy, | |
5995 | }, | |
5996 | [NFT_MSG_GETSET] = { | |
d9adf22a | 5997 | .call_rcu = nf_tables_getset, |
20a69341 PM |
5998 | .attr_count = NFTA_SET_MAX, |
5999 | .policy = nft_set_policy, | |
6000 | }, | |
6001 | [NFT_MSG_DELSET] = { | |
958bee14 | 6002 | .call_batch = nf_tables_delset, |
20a69341 PM |
6003 | .attr_count = NFTA_SET_MAX, |
6004 | .policy = nft_set_policy, | |
6005 | }, | |
6006 | [NFT_MSG_NEWSETELEM] = { | |
958bee14 | 6007 | .call_batch = nf_tables_newsetelem, |
20a69341 PM |
6008 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
6009 | .policy = nft_set_elem_list_policy, | |
6010 | }, | |
6011 | [NFT_MSG_GETSETELEM] = { | |
d9adf22a | 6012 | .call_rcu = nf_tables_getsetelem, |
20a69341 PM |
6013 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
6014 | .policy = nft_set_elem_list_policy, | |
6015 | }, | |
6016 | [NFT_MSG_DELSETELEM] = { | |
958bee14 | 6017 | .call_batch = nf_tables_delsetelem, |
20a69341 PM |
6018 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
6019 | .policy = nft_set_elem_list_policy, | |
6020 | }, | |
84d7fce6 | 6021 | [NFT_MSG_GETGEN] = { |
d9adf22a | 6022 | .call_rcu = nf_tables_getgen, |
84d7fce6 | 6023 | }, |
e5009240 PNA |
6024 | [NFT_MSG_NEWOBJ] = { |
6025 | .call_batch = nf_tables_newobj, | |
6026 | .attr_count = NFTA_OBJ_MAX, | |
6027 | .policy = nft_obj_policy, | |
6028 | }, | |
6029 | [NFT_MSG_GETOBJ] = { | |
d9adf22a | 6030 | .call_rcu = nf_tables_getobj, |
e5009240 PNA |
6031 | .attr_count = NFTA_OBJ_MAX, |
6032 | .policy = nft_obj_policy, | |
6033 | }, | |
6034 | [NFT_MSG_DELOBJ] = { | |
6035 | .call_batch = nf_tables_delobj, | |
6036 | .attr_count = NFTA_OBJ_MAX, | |
6037 | .policy = nft_obj_policy, | |
6038 | }, | |
43da04a5 | 6039 | [NFT_MSG_GETOBJ_RESET] = { |
d9adf22a | 6040 | .call_rcu = nf_tables_getobj, |
43da04a5 PNA |
6041 | .attr_count = NFTA_OBJ_MAX, |
6042 | .policy = nft_obj_policy, | |
6043 | }, | |
3b49e2e9 PNA |
6044 | [NFT_MSG_NEWFLOWTABLE] = { |
6045 | .call_batch = nf_tables_newflowtable, | |
6046 | .attr_count = NFTA_FLOWTABLE_MAX, | |
6047 | .policy = nft_flowtable_policy, | |
6048 | }, | |
6049 | [NFT_MSG_GETFLOWTABLE] = { | |
d9adf22a | 6050 | .call_rcu = nf_tables_getflowtable, |
3b49e2e9 PNA |
6051 | .attr_count = NFTA_FLOWTABLE_MAX, |
6052 | .policy = nft_flowtable_policy, | |
6053 | }, | |
6054 | [NFT_MSG_DELFLOWTABLE] = { | |
6055 | .call_batch = nf_tables_delflowtable, | |
6056 | .attr_count = NFTA_FLOWTABLE_MAX, | |
6057 | .policy = nft_flowtable_policy, | |
6058 | }, | |
96518518 PM |
6059 | }; |
6060 | ||
a654de8f PNA |
6061 | static int nf_tables_validate(struct net *net) |
6062 | { | |
6063 | struct nft_table *table; | |
6064 | ||
6065 | switch (net->nft.validate_state) { | |
6066 | case NFT_VALIDATE_SKIP: | |
6067 | break; | |
6068 | case NFT_VALIDATE_NEED: | |
6069 | nft_validate_state_update(net, NFT_VALIDATE_DO); | |
6070 | /* fall through */ | |
6071 | case NFT_VALIDATE_DO: | |
6072 | list_for_each_entry(table, &net->nft.tables, list) { | |
6073 | if (nft_table_validate(net, table) < 0) | |
6074 | return -EAGAIN; | |
6075 | } | |
6076 | break; | |
6077 | } | |
6078 | ||
6079 | return 0; | |
6080 | } | |
6081 | ||
91c7b38d PNA |
6082 | static void nft_chain_commit_update(struct nft_trans *trans) |
6083 | { | |
6084 | struct nft_base_chain *basechain; | |
6085 | ||
1b2470e5 FW |
6086 | if (nft_trans_chain_name(trans)) { |
6087 | rhltable_remove(&trans->ctx.table->chains_ht, | |
6088 | &trans->ctx.chain->rhlhead, | |
6089 | nft_chain_ht_params); | |
d71efb59 | 6090 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
6091 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
6092 | trans->ctx.chain->name, | |
6093 | &trans->ctx.chain->rhlhead, | |
6094 | nft_chain_ht_params); | |
6095 | } | |
91c7b38d | 6096 | |
f323d954 | 6097 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
6098 | return; |
6099 | ||
6100 | basechain = nft_base_chain(trans->ctx.chain); | |
6101 | nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans)); | |
6102 | ||
6103 | switch (nft_trans_chain_policy(trans)) { | |
6104 | case NF_DROP: | |
6105 | case NF_ACCEPT: | |
6106 | basechain->policy = nft_trans_chain_policy(trans); | |
6107 | break; | |
6108 | } | |
6109 | } | |
6110 | ||
2f99aa31 | 6111 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 6112 | { |
c7c32e72 PNA |
6113 | switch (trans->msg_type) { |
6114 | case NFT_MSG_DELTABLE: | |
6115 | nf_tables_table_destroy(&trans->ctx); | |
6116 | break; | |
9f8aac0b FW |
6117 | case NFT_MSG_NEWCHAIN: |
6118 | kfree(nft_trans_chain_name(trans)); | |
6119 | break; | |
c7c32e72 | 6120 | case NFT_MSG_DELCHAIN: |
43a605f2 | 6121 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
6122 | break; |
6123 | case NFT_MSG_DELRULE: | |
6124 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
6125 | break; | |
6126 | case NFT_MSG_DELSET: | |
6127 | nft_set_destroy(nft_trans_set(trans)); | |
6128 | break; | |
61edafbb | 6129 | case NFT_MSG_DELSETELEM: |
3453c927 PNA |
6130 | nf_tables_set_elem_destroy(&trans->ctx, |
6131 | nft_trans_elem_set(trans), | |
59105446 | 6132 | nft_trans_elem(trans).priv); |
61edafbb | 6133 | break; |
e5009240 | 6134 | case NFT_MSG_DELOBJ: |
00bfb320 | 6135 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 6136 | break; |
3b49e2e9 PNA |
6137 | case NFT_MSG_DELFLOWTABLE: |
6138 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
6139 | break; | |
c7c32e72 PNA |
6140 | } |
6141 | kfree(trans); | |
6142 | } | |
6143 | ||
2f99aa31 FW |
6144 | static void nf_tables_commit_release(struct net *net) |
6145 | { | |
6146 | struct nft_trans *trans, *next; | |
6147 | ||
6148 | if (list_empty(&net->nft.commit_list)) | |
6149 | return; | |
6150 | ||
6151 | synchronize_rcu(); | |
6152 | ||
6153 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
6154 | list_del(&trans->list); | |
6155 | nft_commit_release(trans); | |
6156 | } | |
6157 | } | |
6158 | ||
0cbc06b3 FW |
6159 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
6160 | { | |
6161 | struct nft_rule *rule; | |
6162 | unsigned int alloc = 0; | |
6163 | int i; | |
6164 | ||
6165 | /* already handled or inactive chain? */ | |
6166 | if (chain->rules_next || !nft_is_active_next(net, chain)) | |
6167 | return 0; | |
6168 | ||
6169 | rule = list_entry(&chain->rules, struct nft_rule, list); | |
6170 | i = 0; | |
6171 | ||
6172 | list_for_each_entry_continue(rule, &chain->rules, list) { | |
6173 | if (nft_is_active_next(net, rule)) | |
6174 | alloc++; | |
6175 | } | |
6176 | ||
6177 | chain->rules_next = nf_tables_chain_alloc_rules(chain, alloc); | |
6178 | if (!chain->rules_next) | |
6179 | return -ENOMEM; | |
6180 | ||
6181 | list_for_each_entry_continue(rule, &chain->rules, list) { | |
6182 | if (nft_is_active_next(net, rule)) | |
6183 | chain->rules_next[i++] = rule; | |
6184 | } | |
6185 | ||
6186 | chain->rules_next[i] = NULL; | |
6187 | return 0; | |
6188 | } | |
6189 | ||
6190 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
6191 | { | |
6192 | struct nft_trans *trans, *next; | |
6193 | ||
6194 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
6195 | struct nft_chain *chain = trans->ctx.chain; | |
6196 | ||
6197 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
6198 | trans->msg_type == NFT_MSG_DELRULE) { | |
6199 | kvfree(chain->rules_next); | |
6200 | chain->rules_next = NULL; | |
6201 | } | |
6202 | } | |
6203 | } | |
6204 | ||
6205 | static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h) | |
6206 | { | |
6207 | struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); | |
6208 | ||
6209 | kvfree(o->start); | |
6210 | } | |
6211 | ||
6212 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule **rules) | |
6213 | { | |
6214 | struct nft_rule **r = rules; | |
6215 | struct nft_rules_old *old; | |
6216 | ||
6217 | while (*r) | |
6218 | r++; | |
6219 | ||
6220 | r++; /* rcu_head is after end marker */ | |
6221 | old = (void *) r; | |
6222 | old->start = rules; | |
6223 | ||
6224 | call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old); | |
6225 | } | |
6226 | ||
6227 | static void nf_tables_commit_chain_active(struct net *net, struct nft_chain *chain) | |
6228 | { | |
6229 | struct nft_rule **g0, **g1; | |
6230 | bool next_genbit; | |
6231 | ||
6232 | next_genbit = nft_gencursor_next(net); | |
6233 | ||
6234 | g0 = rcu_dereference_protected(chain->rules_gen_0, | |
6235 | lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
6236 | g1 = rcu_dereference_protected(chain->rules_gen_1, | |
6237 | lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
6238 | ||
6239 | /* No changes to this chain? */ | |
6240 | if (chain->rules_next == NULL) { | |
6241 | /* chain had no change in last or next generation */ | |
6242 | if (g0 == g1) | |
6243 | return; | |
6244 | /* | |
6245 | * chain had no change in this generation; make sure next | |
6246 | * one uses same rules as current generation. | |
6247 | */ | |
6248 | if (next_genbit) { | |
6249 | rcu_assign_pointer(chain->rules_gen_1, g0); | |
6250 | nf_tables_commit_chain_free_rules_old(g1); | |
6251 | } else { | |
6252 | rcu_assign_pointer(chain->rules_gen_0, g1); | |
6253 | nf_tables_commit_chain_free_rules_old(g0); | |
6254 | } | |
6255 | ||
6256 | return; | |
6257 | } | |
6258 | ||
6259 | if (next_genbit) | |
6260 | rcu_assign_pointer(chain->rules_gen_1, chain->rules_next); | |
6261 | else | |
6262 | rcu_assign_pointer(chain->rules_gen_0, chain->rules_next); | |
6263 | ||
6264 | chain->rules_next = NULL; | |
6265 | ||
6266 | if (g0 == g1) | |
6267 | return; | |
6268 | ||
6269 | if (next_genbit) | |
6270 | nf_tables_commit_chain_free_rules_old(g1); | |
6271 | else | |
6272 | nf_tables_commit_chain_free_rules_old(g0); | |
6273 | } | |
6274 | ||
1b2470e5 FW |
6275 | static void nft_chain_del(struct nft_chain *chain) |
6276 | { | |
6277 | struct nft_table *table = chain->table; | |
6278 | ||
6279 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
6280 | nft_chain_ht_params)); | |
6281 | list_del_rcu(&chain->list); | |
6282 | } | |
6283 | ||
5913beaf | 6284 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 6285 | { |
37082f93 | 6286 | struct nft_trans *trans, *next; |
a3716e70 | 6287 | struct nft_trans_elem *te; |
0cbc06b3 FW |
6288 | struct nft_chain *chain; |
6289 | struct nft_table *table; | |
37082f93 | 6290 | |
a654de8f PNA |
6291 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
6292 | if (nf_tables_validate(net) < 0) | |
6293 | return -EAGAIN; | |
6294 | ||
0cbc06b3 FW |
6295 | /* 1. Allocate space for next generation rules_gen_X[] */ |
6296 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
6297 | int ret; | |
37082f93 | 6298 | |
0cbc06b3 FW |
6299 | if (trans->msg_type == NFT_MSG_NEWRULE || |
6300 | trans->msg_type == NFT_MSG_DELRULE) { | |
6301 | chain = trans->ctx.chain; | |
6302 | ||
6303 | ret = nf_tables_commit_chain_prepare(net, chain); | |
6304 | if (ret < 0) { | |
6305 | nf_tables_commit_chain_prepare_cancel(net); | |
6306 | return ret; | |
6307 | } | |
6308 | } | |
6309 | } | |
37082f93 | 6310 | |
0cbc06b3 FW |
6311 | /* step 2. Make rules_gen_X visible to packet path */ |
6312 | list_for_each_entry(table, &net->nft.tables, list) { | |
6313 | list_for_each_entry(chain, &table->chains, list) { | |
6314 | if (!nft_is_active_next(net, chain)) | |
6315 | continue; | |
6316 | nf_tables_commit_chain_active(net, chain); | |
6317 | } | |
6318 | } | |
6319 | ||
6320 | /* | |
6321 | * Bump generation counter, invalidate any dump in progress. | |
6322 | * Cannot fail after this point. | |
37082f93 | 6323 | */ |
0cbc06b3 FW |
6324 | while (++net->nft.base_seq == 0); |
6325 | ||
6326 | /* step 3. Start new generation, rules_gen_X now in use. */ | |
6327 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 PNA |
6328 | |
6329 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
b380e5c7 | 6330 | switch (trans->msg_type) { |
55dd6f93 PNA |
6331 | case NFT_MSG_NEWTABLE: |
6332 | if (nft_trans_table_update(trans)) { | |
6333 | if (!nft_trans_table_enable(trans)) { | |
664b0f8c | 6334 | nf_tables_table_disable(net, |
55dd6f93 PNA |
6335 | trans->ctx.table); |
6336 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; | |
6337 | } | |
6338 | } else { | |
f2a6d766 | 6339 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 6340 | } |
35151d84 | 6341 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
6342 | nft_trans_destroy(trans); |
6343 | break; | |
6344 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 6345 | list_del_rcu(&trans->ctx.table->list); |
35151d84 | 6346 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
55dd6f93 | 6347 | break; |
91c7b38d | 6348 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 6349 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 6350 | nft_chain_commit_update(trans); |
9f8aac0b FW |
6351 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
6352 | /* trans destroyed after rcu grace period */ | |
6353 | } else { | |
664b0f8c | 6354 | nft_clear(net, trans->ctx.chain); |
9f8aac0b FW |
6355 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
6356 | nft_trans_destroy(trans); | |
6357 | } | |
91c7b38d PNA |
6358 | break; |
6359 | case NFT_MSG_DELCHAIN: | |
1b2470e5 | 6360 | nft_chain_del(trans->ctx.chain); |
35151d84 | 6361 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
c974a3a3 PNA |
6362 | nf_tables_unregister_hook(trans->ctx.net, |
6363 | trans->ctx.table, | |
6364 | trans->ctx.chain); | |
91c7b38d | 6365 | break; |
b380e5c7 | 6366 | case NFT_MSG_NEWRULE: |
889f7ee7 | 6367 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 6368 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 6369 | nft_trans_rule(trans), |
35151d84 | 6370 | NFT_MSG_NEWRULE); |
37082f93 | 6371 | nft_trans_destroy(trans); |
b380e5c7 PNA |
6372 | break; |
6373 | case NFT_MSG_DELRULE: | |
6374 | list_del_rcu(&nft_trans_rule(trans)->list); | |
35151d84 PNA |
6375 | nf_tables_rule_notify(&trans->ctx, |
6376 | nft_trans_rule(trans), | |
6377 | NFT_MSG_DELRULE); | |
b380e5c7 | 6378 | break; |
958bee14 | 6379 | case NFT_MSG_NEWSET: |
37a9cc52 | 6380 | nft_clear(net, nft_trans_set(trans)); |
4fefee57 PNA |
6381 | /* This avoids hitting -EBUSY when deleting the table |
6382 | * from the transaction. | |
6383 | */ | |
408070d6 | 6384 | if (nft_set_is_anonymous(nft_trans_set(trans)) && |
4fefee57 PNA |
6385 | !list_empty(&nft_trans_set(trans)->bindings)) |
6386 | trans->ctx.table->use--; | |
6387 | ||
958bee14 | 6388 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 6389 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
6390 | nft_trans_destroy(trans); |
6391 | break; | |
6392 | case NFT_MSG_DELSET: | |
37a9cc52 | 6393 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 6394 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 6395 | NFT_MSG_DELSET, GFP_KERNEL); |
958bee14 | 6396 | break; |
60319eb1 | 6397 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
6398 | te = (struct nft_trans_elem *)trans->data; |
6399 | ||
42a55769 | 6400 | te->set->ops->activate(net, te->set, &te->elem); |
cc02e457 PM |
6401 | nf_tables_setelem_notify(&trans->ctx, te->set, |
6402 | &te->elem, | |
60319eb1 PNA |
6403 | NFT_MSG_NEWSETELEM, 0); |
6404 | nft_trans_destroy(trans); | |
6405 | break; | |
6406 | case NFT_MSG_DELSETELEM: | |
a3716e70 | 6407 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 6408 | |
a3716e70 PNA |
6409 | nf_tables_setelem_notify(&trans->ctx, te->set, |
6410 | &te->elem, | |
60319eb1 | 6411 | NFT_MSG_DELSETELEM, 0); |
5cb82a38 | 6412 | te->set->ops->remove(net, te->set, &te->elem); |
3dd0673a PM |
6413 | atomic_dec(&te->set->nelems); |
6414 | te->set->ndeact--; | |
60319eb1 | 6415 | break; |
e5009240 PNA |
6416 | case NFT_MSG_NEWOBJ: |
6417 | nft_clear(net, nft_trans_obj(trans)); | |
6418 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), | |
6419 | NFT_MSG_NEWOBJ); | |
6420 | nft_trans_destroy(trans); | |
6421 | break; | |
6422 | case NFT_MSG_DELOBJ: | |
6423 | list_del_rcu(&nft_trans_obj(trans)->list); | |
6424 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), | |
6425 | NFT_MSG_DELOBJ); | |
6426 | break; | |
3b49e2e9 PNA |
6427 | case NFT_MSG_NEWFLOWTABLE: |
6428 | nft_clear(net, nft_trans_flowtable(trans)); | |
6429 | nf_tables_flowtable_notify(&trans->ctx, | |
6430 | nft_trans_flowtable(trans), | |
6431 | NFT_MSG_NEWFLOWTABLE); | |
6432 | nft_trans_destroy(trans); | |
6433 | break; | |
6434 | case NFT_MSG_DELFLOWTABLE: | |
6435 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
6436 | nf_tables_flowtable_notify(&trans->ctx, | |
6437 | nft_trans_flowtable(trans), | |
6438 | NFT_MSG_DELFLOWTABLE); | |
6439 | nft_unregister_flowtable_net_hooks(net, | |
6440 | nft_trans_flowtable(trans)); | |
6441 | break; | |
37082f93 | 6442 | } |
37082f93 PNA |
6443 | } |
6444 | ||
2f99aa31 | 6445 | nf_tables_commit_release(net); |
84d7fce6 | 6446 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
37082f93 PNA |
6447 | |
6448 | return 0; | |
6449 | } | |
6450 | ||
b326dd37 | 6451 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 6452 | { |
c7c32e72 PNA |
6453 | switch (trans->msg_type) { |
6454 | case NFT_MSG_NEWTABLE: | |
6455 | nf_tables_table_destroy(&trans->ctx); | |
6456 | break; | |
6457 | case NFT_MSG_NEWCHAIN: | |
43a605f2 | 6458 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
6459 | break; |
6460 | case NFT_MSG_NEWRULE: | |
6461 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
6462 | break; | |
6463 | case NFT_MSG_NEWSET: | |
6464 | nft_set_destroy(nft_trans_set(trans)); | |
6465 | break; | |
61edafbb PM |
6466 | case NFT_MSG_NEWSETELEM: |
6467 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 6468 | nft_trans_elem(trans).priv, true); |
61edafbb | 6469 | break; |
e5009240 | 6470 | case NFT_MSG_NEWOBJ: |
00bfb320 | 6471 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 6472 | break; |
3b49e2e9 PNA |
6473 | case NFT_MSG_NEWFLOWTABLE: |
6474 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
6475 | break; | |
c7c32e72 PNA |
6476 | } |
6477 | kfree(trans); | |
6478 | } | |
6479 | ||
71ad00c5 | 6480 | static int __nf_tables_abort(struct net *net) |
37082f93 | 6481 | { |
37082f93 | 6482 | struct nft_trans *trans, *next; |
02263db0 | 6483 | struct nft_trans_elem *te; |
37082f93 | 6484 | |
a907e36d XL |
6485 | list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list, |
6486 | list) { | |
b380e5c7 | 6487 | switch (trans->msg_type) { |
55dd6f93 PNA |
6488 | case NFT_MSG_NEWTABLE: |
6489 | if (nft_trans_table_update(trans)) { | |
6490 | if (nft_trans_table_enable(trans)) { | |
664b0f8c | 6491 | nf_tables_table_disable(net, |
55dd6f93 PNA |
6492 | trans->ctx.table); |
6493 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; | |
6494 | } | |
6495 | nft_trans_destroy(trans); | |
6496 | } else { | |
e688a7f8 | 6497 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
6498 | } |
6499 | break; | |
6500 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 6501 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
6502 | nft_trans_destroy(trans); |
6503 | break; | |
91c7b38d PNA |
6504 | case NFT_MSG_NEWCHAIN: |
6505 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 6506 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 6507 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
6508 | nft_trans_destroy(trans); |
6509 | } else { | |
4fefee57 | 6510 | trans->ctx.table->use--; |
1b2470e5 | 6511 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
6512 | nf_tables_unregister_hook(trans->ctx.net, |
6513 | trans->ctx.table, | |
6514 | trans->ctx.chain); | |
91c7b38d PNA |
6515 | } |
6516 | break; | |
6517 | case NFT_MSG_DELCHAIN: | |
4fefee57 | 6518 | trans->ctx.table->use++; |
664b0f8c | 6519 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
6520 | nft_trans_destroy(trans); |
6521 | break; | |
b380e5c7 | 6522 | case NFT_MSG_NEWRULE: |
4fefee57 | 6523 | trans->ctx.chain->use--; |
b380e5c7 | 6524 | list_del_rcu(&nft_trans_rule(trans)->list); |
bb7b40ae | 6525 | nft_rule_expr_deactivate(&trans->ctx, nft_trans_rule(trans)); |
b380e5c7 PNA |
6526 | break; |
6527 | case NFT_MSG_DELRULE: | |
4fefee57 | 6528 | trans->ctx.chain->use++; |
889f7ee7 | 6529 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 6530 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
37082f93 | 6531 | nft_trans_destroy(trans); |
b380e5c7 | 6532 | break; |
958bee14 | 6533 | case NFT_MSG_NEWSET: |
4fefee57 | 6534 | trans->ctx.table->use--; |
e688a7f8 | 6535 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 PNA |
6536 | break; |
6537 | case NFT_MSG_DELSET: | |
4fefee57 | 6538 | trans->ctx.table->use++; |
37a9cc52 | 6539 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
6540 | nft_trans_destroy(trans); |
6541 | break; | |
60319eb1 | 6542 | case NFT_MSG_NEWSETELEM: |
02263db0 | 6543 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 6544 | |
5cb82a38 | 6545 | te->set->ops->remove(net, te->set, &te->elem); |
3dd0673a | 6546 | atomic_dec(&te->set->nelems); |
60319eb1 PNA |
6547 | break; |
6548 | case NFT_MSG_DELSETELEM: | |
cc02e457 PM |
6549 | te = (struct nft_trans_elem *)trans->data; |
6550 | ||
59105446 | 6551 | nft_set_elem_activate(net, te->set, &te->elem); |
42a55769 | 6552 | te->set->ops->activate(net, te->set, &te->elem); |
3dd0673a | 6553 | te->set->ndeact--; |
cc02e457 | 6554 | |
e5009240 PNA |
6555 | nft_trans_destroy(trans); |
6556 | break; | |
6557 | case NFT_MSG_NEWOBJ: | |
6558 | trans->ctx.table->use--; | |
6559 | list_del_rcu(&nft_trans_obj(trans)->list); | |
6560 | break; | |
6561 | case NFT_MSG_DELOBJ: | |
6562 | trans->ctx.table->use++; | |
6563 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
6564 | nft_trans_destroy(trans); |
6565 | break; | |
3b49e2e9 PNA |
6566 | case NFT_MSG_NEWFLOWTABLE: |
6567 | trans->ctx.table->use--; | |
6568 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
6569 | nft_unregister_flowtable_net_hooks(net, | |
6570 | nft_trans_flowtable(trans)); | |
6571 | break; | |
6572 | case NFT_MSG_DELFLOWTABLE: | |
6573 | trans->ctx.table->use++; | |
6574 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
6575 | nft_trans_destroy(trans); | |
6576 | break; | |
37082f93 | 6577 | } |
37082f93 PNA |
6578 | } |
6579 | ||
b326dd37 PNA |
6580 | synchronize_rcu(); |
6581 | ||
a1cee076 PNA |
6582 | list_for_each_entry_safe_reverse(trans, next, |
6583 | &net->nft.commit_list, list) { | |
c7c32e72 | 6584 | list_del(&trans->list); |
b326dd37 | 6585 | nf_tables_abort_release(trans); |
37082f93 PNA |
6586 | } |
6587 | ||
6588 | return 0; | |
6589 | } | |
6590 | ||
a654de8f PNA |
6591 | static void nf_tables_cleanup(struct net *net) |
6592 | { | |
6593 | nft_validate_state_update(net, NFT_VALIDATE_SKIP); | |
6594 | } | |
6595 | ||
71ad00c5 FW |
6596 | static int nf_tables_abort(struct net *net, struct sk_buff *skb) |
6597 | { | |
6598 | return __nf_tables_abort(net); | |
6599 | } | |
6600 | ||
74e8bcd2 PNA |
6601 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
6602 | { | |
6603 | return net->nft.base_seq == genid; | |
6604 | } | |
6605 | ||
96518518 PM |
6606 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
6607 | .name = "nf_tables", | |
6608 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
6609 | .cb_count = NFT_MSG_MAX, | |
6610 | .cb = nf_tables_cb, | |
0628b123 PNA |
6611 | .commit = nf_tables_commit, |
6612 | .abort = nf_tables_abort, | |
a654de8f | 6613 | .cleanup = nf_tables_cleanup, |
74e8bcd2 | 6614 | .valid_genid = nf_tables_valid_genid, |
96518518 PM |
6615 | }; |
6616 | ||
7210e4e3 | 6617 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 6618 | enum nft_chain_types type) |
7210e4e3 PNA |
6619 | { |
6620 | const struct nft_base_chain *basechain; | |
6621 | ||
f323d954 | 6622 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
6623 | basechain = nft_base_chain(chain); |
6624 | if (basechain->type->type != type) | |
6625 | return -EOPNOTSUPP; | |
6626 | } | |
6627 | return 0; | |
6628 | } | |
6629 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
6630 | ||
75e8d06d PNA |
6631 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
6632 | unsigned int hook_flags) | |
6633 | { | |
6634 | struct nft_base_chain *basechain; | |
6635 | ||
f323d954 | 6636 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
6637 | basechain = nft_base_chain(chain); |
6638 | ||
c974a3a3 | 6639 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
6640 | return 0; |
6641 | ||
6642 | return -EOPNOTSUPP; | |
6643 | } | |
6644 | ||
6645 | return 0; | |
6646 | } | |
6647 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
6648 | ||
20a69341 PM |
6649 | /* |
6650 | * Loop detection - walk through the ruleset beginning at the destination chain | |
6651 | * of a new jump until either the source chain is reached (loop) or all | |
6652 | * reachable chains have been traversed. | |
6653 | * | |
6654 | * The loop check is performed whenever a new jump verdict is added to an | |
6655 | * expression or verdict map or a verdict map is bound to a new chain. | |
6656 | */ | |
6657 | ||
6658 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
6659 | const struct nft_chain *chain); | |
6660 | ||
6661 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, | |
de70185d | 6662 | struct nft_set *set, |
20a69341 | 6663 | const struct nft_set_iter *iter, |
de70185d | 6664 | struct nft_set_elem *elem) |
20a69341 | 6665 | { |
fe2811eb PM |
6666 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
6667 | const struct nft_data *data; | |
6668 | ||
6669 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
6670 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
6671 | return 0; |
6672 | ||
fe2811eb | 6673 | data = nft_set_ext_data(ext); |
1ca2e170 | 6674 | switch (data->verdict.code) { |
20a69341 PM |
6675 | case NFT_JUMP: |
6676 | case NFT_GOTO: | |
1ca2e170 | 6677 | return nf_tables_check_loops(ctx, data->verdict.chain); |
20a69341 PM |
6678 | default: |
6679 | return 0; | |
6680 | } | |
6681 | } | |
6682 | ||
6683 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
6684 | const struct nft_chain *chain) | |
6685 | { | |
6686 | const struct nft_rule *rule; | |
6687 | const struct nft_expr *expr, *last; | |
de70185d | 6688 | struct nft_set *set; |
20a69341 PM |
6689 | struct nft_set_binding *binding; |
6690 | struct nft_set_iter iter; | |
20a69341 PM |
6691 | |
6692 | if (ctx->chain == chain) | |
6693 | return -ELOOP; | |
6694 | ||
6695 | list_for_each_entry(rule, &chain->rules, list) { | |
6696 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
6697 | struct nft_immediate_expr *priv; |
6698 | const struct nft_data *data; | |
0ca743a5 PNA |
6699 | int err; |
6700 | ||
a654de8f | 6701 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
6702 | continue; |
6703 | ||
a654de8f PNA |
6704 | priv = nft_expr_priv(expr); |
6705 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 6706 | continue; |
20a69341 | 6707 | |
a654de8f | 6708 | data = &priv->data; |
1ca2e170 | 6709 | switch (data->verdict.code) { |
20a69341 PM |
6710 | case NFT_JUMP: |
6711 | case NFT_GOTO: | |
1ca2e170 PM |
6712 | err = nf_tables_check_loops(ctx, |
6713 | data->verdict.chain); | |
20a69341 PM |
6714 | if (err < 0) |
6715 | return err; | |
6716 | default: | |
6717 | break; | |
6718 | } | |
6719 | } | |
6720 | } | |
6721 | ||
6722 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
6723 | if (!nft_is_active_next(ctx->net, set)) |
6724 | continue; | |
20a69341 PM |
6725 | if (!(set->flags & NFT_SET_MAP) || |
6726 | set->dtype != NFT_DATA_VERDICT) | |
6727 | continue; | |
6728 | ||
6729 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
6730 | if (!(binding->flags & NFT_SET_MAP) || |
6731 | binding->chain != chain) | |
20a69341 PM |
6732 | continue; |
6733 | ||
8588ac09 | 6734 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
6735 | iter.skip = 0; |
6736 | iter.count = 0; | |
6737 | iter.err = 0; | |
6738 | iter.fn = nf_tables_loop_check_setelem; | |
6739 | ||
6740 | set->ops->walk(ctx, set, &iter); | |
6741 | if (iter.err < 0) | |
6742 | return iter.err; | |
6743 | } | |
6744 | } | |
6745 | ||
6746 | return 0; | |
6747 | } | |
6748 | ||
36b701fa LGL |
6749 | /** |
6750 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
6751 | * | |
6752 | * @attr: netlink attribute to fetch value from | |
6753 | * @max: maximum value to be stored in dest | |
6754 | * @dest: pointer to the variable | |
6755 | * | |
6756 | * Parse, check and store a given u32 netlink attribute into variable. | |
6757 | * This function returns -ERANGE if the value goes over maximum value. | |
6758 | * Otherwise a 0 is returned and the attribute value is stored in the | |
6759 | * destination variable. | |
6760 | */ | |
f1d505bb | 6761 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 6762 | { |
09525a09 | 6763 | u32 val; |
36b701fa LGL |
6764 | |
6765 | val = ntohl(nla_get_be32(attr)); | |
6766 | if (val > max) | |
6767 | return -ERANGE; | |
6768 | ||
6769 | *dest = val; | |
6770 | return 0; | |
6771 | } | |
6772 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
6773 | ||
49499c3e PM |
6774 | /** |
6775 | * nft_parse_register - parse a register value from a netlink attribute | |
6776 | * | |
6777 | * @attr: netlink attribute | |
6778 | * | |
6779 | * Parse and translate a register value from a netlink attribute. | |
6780 | * Registers used to be 128 bit wide, these register numbers will be | |
6781 | * mapped to the corresponding 32 bit register numbers. | |
6782 | */ | |
b1c96ed3 PM |
6783 | unsigned int nft_parse_register(const struct nlattr *attr) |
6784 | { | |
49499c3e PM |
6785 | unsigned int reg; |
6786 | ||
6787 | reg = ntohl(nla_get_be32(attr)); | |
6788 | switch (reg) { | |
6789 | case NFT_REG_VERDICT...NFT_REG_4: | |
6790 | return reg * NFT_REG_SIZE / NFT_REG32_SIZE; | |
6791 | default: | |
6792 | return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
6793 | } | |
b1c96ed3 PM |
6794 | } |
6795 | EXPORT_SYMBOL_GPL(nft_parse_register); | |
6796 | ||
49499c3e PM |
6797 | /** |
6798 | * nft_dump_register - dump a register value to a netlink attribute | |
6799 | * | |
6800 | * @skb: socket buffer | |
6801 | * @attr: attribute number | |
6802 | * @reg: register number | |
6803 | * | |
6804 | * Construct a netlink attribute containing the register number. For | |
6805 | * compatibility reasons, register numbers being a multiple of 4 are | |
6806 | * translated to the corresponding 128 bit register numbers. | |
6807 | */ | |
b1c96ed3 PM |
6808 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
6809 | { | |
49499c3e PM |
6810 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
6811 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
6812 | else | |
6813 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
6814 | ||
b1c96ed3 PM |
6815 | return nla_put_be32(skb, attr, htonl(reg)); |
6816 | } | |
6817 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
6818 | ||
96518518 | 6819 | /** |
d07db988 | 6820 | * nft_validate_register_load - validate a load from a register |
96518518 PM |
6821 | * |
6822 | * @reg: the register number | |
d07db988 | 6823 | * @len: the length of the data |
96518518 PM |
6824 | * |
6825 | * Validate that the input register is one of the general purpose | |
d07db988 | 6826 | * registers and that the length of the load is within the bounds. |
96518518 | 6827 | */ |
d07db988 | 6828 | int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 6829 | { |
49499c3e | 6830 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 6831 | return -EINVAL; |
d07db988 PM |
6832 | if (len == 0) |
6833 | return -EINVAL; | |
49499c3e | 6834 | if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data)) |
d07db988 | 6835 | return -ERANGE; |
49499c3e | 6836 | |
96518518 PM |
6837 | return 0; |
6838 | } | |
d07db988 | 6839 | EXPORT_SYMBOL_GPL(nft_validate_register_load); |
96518518 | 6840 | |
96518518 | 6841 | /** |
1ec10212 | 6842 | * nft_validate_register_store - validate an expressions' register store |
96518518 PM |
6843 | * |
6844 | * @ctx: context of the expression performing the load | |
6845 | * @reg: the destination register number | |
6846 | * @data: the data to load | |
6847 | * @type: the data type | |
45d9bcda | 6848 | * @len: the length of the data |
96518518 PM |
6849 | * |
6850 | * Validate that a data load uses the appropriate data type for | |
45d9bcda PM |
6851 | * the destination register and the length is within the bounds. |
6852 | * A value of NULL for the data means that its runtime gathered | |
58f40ab6 | 6853 | * data. |
96518518 | 6854 | */ |
1ec10212 PM |
6855 | int nft_validate_register_store(const struct nft_ctx *ctx, |
6856 | enum nft_registers reg, | |
6857 | const struct nft_data *data, | |
6858 | enum nft_data_types type, unsigned int len) | |
96518518 | 6859 | { |
20a69341 PM |
6860 | int err; |
6861 | ||
96518518 PM |
6862 | switch (reg) { |
6863 | case NFT_REG_VERDICT: | |
58f40ab6 | 6864 | if (type != NFT_DATA_VERDICT) |
96518518 | 6865 | return -EINVAL; |
20a69341 | 6866 | |
58f40ab6 | 6867 | if (data != NULL && |
1ca2e170 PM |
6868 | (data->verdict.code == NFT_GOTO || |
6869 | data->verdict.code == NFT_JUMP)) { | |
6870 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
6871 | if (err < 0) |
6872 | return err; | |
20a69341 PM |
6873 | } |
6874 | ||
96518518 PM |
6875 | return 0; |
6876 | default: | |
49499c3e | 6877 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 6878 | return -EINVAL; |
45d9bcda PM |
6879 | if (len == 0) |
6880 | return -EINVAL; | |
49499c3e PM |
6881 | if (reg * NFT_REG32_SIZE + len > |
6882 | FIELD_SIZEOF(struct nft_regs, data)) | |
45d9bcda | 6883 | return -ERANGE; |
27e6d201 | 6884 | |
96518518 PM |
6885 | if (data != NULL && type != NFT_DATA_VALUE) |
6886 | return -EINVAL; | |
6887 | return 0; | |
6888 | } | |
6889 | } | |
1ec10212 | 6890 | EXPORT_SYMBOL_GPL(nft_validate_register_store); |
96518518 PM |
6891 | |
6892 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
6893 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
6894 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
6895 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
6896 | }; | |
6897 | ||
6898 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
6899 | struct nft_data_desc *desc, const struct nlattr *nla) | |
6900 | { | |
664b0f8c | 6901 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
6902 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
6903 | struct nft_chain *chain; | |
6904 | int err; | |
6905 | ||
fceb6435 JB |
6906 | err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy, |
6907 | NULL); | |
96518518 PM |
6908 | if (err < 0) |
6909 | return err; | |
6910 | ||
6911 | if (!tb[NFTA_VERDICT_CODE]) | |
6912 | return -EINVAL; | |
1ca2e170 | 6913 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 6914 | |
1ca2e170 | 6915 | switch (data->verdict.code) { |
e0abdadc | 6916 | default: |
1ca2e170 | 6917 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
6918 | case NF_ACCEPT: |
6919 | case NF_DROP: | |
6920 | case NF_QUEUE: | |
6921 | break; | |
6922 | default: | |
6923 | return -EINVAL; | |
6924 | } | |
6925 | /* fall through */ | |
96518518 PM |
6926 | case NFT_CONTINUE: |
6927 | case NFT_BREAK: | |
6928 | case NFT_RETURN: | |
96518518 PM |
6929 | break; |
6930 | case NFT_JUMP: | |
6931 | case NFT_GOTO: | |
6932 | if (!tb[NFTA_VERDICT_CHAIN]) | |
6933 | return -EINVAL; | |
cac20fcd PNA |
6934 | chain = nft_chain_lookup(ctx->table, tb[NFTA_VERDICT_CHAIN], |
6935 | genmask); | |
96518518 PM |
6936 | if (IS_ERR(chain)) |
6937 | return PTR_ERR(chain); | |
f323d954 | 6938 | if (nft_is_base_chain(chain)) |
96518518 PM |
6939 | return -EOPNOTSUPP; |
6940 | ||
96518518 | 6941 | chain->use++; |
1ca2e170 | 6942 | data->verdict.chain = chain; |
96518518 | 6943 | break; |
96518518 PM |
6944 | } |
6945 | ||
4c4ed074 | 6946 | desc->len = sizeof(data->verdict); |
96518518 PM |
6947 | desc->type = NFT_DATA_VERDICT; |
6948 | return 0; | |
6949 | } | |
6950 | ||
6951 | static void nft_verdict_uninit(const struct nft_data *data) | |
6952 | { | |
1ca2e170 | 6953 | switch (data->verdict.code) { |
96518518 PM |
6954 | case NFT_JUMP: |
6955 | case NFT_GOTO: | |
1ca2e170 | 6956 | data->verdict.chain->use--; |
96518518 PM |
6957 | break; |
6958 | } | |
6959 | } | |
6960 | ||
33d5a7b1 | 6961 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
6962 | { |
6963 | struct nlattr *nest; | |
6964 | ||
33d5a7b1 | 6965 | nest = nla_nest_start(skb, type); |
96518518 PM |
6966 | if (!nest) |
6967 | goto nla_put_failure; | |
6968 | ||
33d5a7b1 | 6969 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
6970 | goto nla_put_failure; |
6971 | ||
33d5a7b1 | 6972 | switch (v->code) { |
96518518 PM |
6973 | case NFT_JUMP: |
6974 | case NFT_GOTO: | |
1ca2e170 | 6975 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 6976 | v->chain->name)) |
96518518 PM |
6977 | goto nla_put_failure; |
6978 | } | |
6979 | nla_nest_end(skb, nest); | |
6980 | return 0; | |
6981 | ||
6982 | nla_put_failure: | |
6983 | return -1; | |
6984 | } | |
6985 | ||
d0a11fc3 PM |
6986 | static int nft_value_init(const struct nft_ctx *ctx, |
6987 | struct nft_data *data, unsigned int size, | |
96518518 PM |
6988 | struct nft_data_desc *desc, const struct nlattr *nla) |
6989 | { | |
6990 | unsigned int len; | |
6991 | ||
6992 | len = nla_len(nla); | |
6993 | if (len == 0) | |
6994 | return -EINVAL; | |
d0a11fc3 | 6995 | if (len > size) |
96518518 PM |
6996 | return -EOVERFLOW; |
6997 | ||
d0a11fc3 | 6998 | nla_memcpy(data->data, nla, len); |
96518518 PM |
6999 | desc->type = NFT_DATA_VALUE; |
7000 | desc->len = len; | |
7001 | return 0; | |
7002 | } | |
7003 | ||
7004 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
7005 | unsigned int len) | |
7006 | { | |
7007 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
7008 | } | |
7009 | ||
7010 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 7011 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
7012 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
7013 | }; | |
7014 | ||
7015 | /** | |
7016 | * nft_data_init - parse nf_tables data netlink attributes | |
7017 | * | |
7018 | * @ctx: context of the expression using the data | |
7019 | * @data: destination struct nft_data | |
d0a11fc3 | 7020 | * @size: maximum data length |
96518518 PM |
7021 | * @desc: data description |
7022 | * @nla: netlink attribute containing data | |
7023 | * | |
7024 | * Parse the netlink data attributes and initialize a struct nft_data. | |
7025 | * The type and length of data are returned in the data description. | |
7026 | * | |
7027 | * The caller can indicate that it only wants to accept data of type | |
7028 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
7029 | */ | |
d0a11fc3 PM |
7030 | int nft_data_init(const struct nft_ctx *ctx, |
7031 | struct nft_data *data, unsigned int size, | |
96518518 PM |
7032 | struct nft_data_desc *desc, const struct nlattr *nla) |
7033 | { | |
7034 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
7035 | int err; | |
7036 | ||
fceb6435 | 7037 | err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy, NULL); |
96518518 PM |
7038 | if (err < 0) |
7039 | return err; | |
7040 | ||
7041 | if (tb[NFTA_DATA_VALUE]) | |
d0a11fc3 PM |
7042 | return nft_value_init(ctx, data, size, desc, |
7043 | tb[NFTA_DATA_VALUE]); | |
96518518 PM |
7044 | if (tb[NFTA_DATA_VERDICT] && ctx != NULL) |
7045 | return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
7046 | return -EINVAL; | |
7047 | } | |
7048 | EXPORT_SYMBOL_GPL(nft_data_init); | |
7049 | ||
7050 | /** | |
59105446 | 7051 | * nft_data_release - release a nft_data item |
96518518 PM |
7052 | * |
7053 | * @data: struct nft_data to release | |
7054 | * @type: type of data | |
7055 | * | |
7056 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
7057 | * all others need to be released by calling this function. | |
7058 | */ | |
59105446 | 7059 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 7060 | { |
960bd2c2 | 7061 | if (type < NFT_DATA_VERDICT) |
96518518 | 7062 | return; |
960bd2c2 | 7063 | switch (type) { |
96518518 PM |
7064 | case NFT_DATA_VERDICT: |
7065 | return nft_verdict_uninit(data); | |
7066 | default: | |
7067 | WARN_ON(1); | |
7068 | } | |
7069 | } | |
59105446 | 7070 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
7071 | |
7072 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
7073 | enum nft_data_types type, unsigned int len) | |
7074 | { | |
7075 | struct nlattr *nest; | |
7076 | int err; | |
7077 | ||
7078 | nest = nla_nest_start(skb, attr); | |
7079 | if (nest == NULL) | |
7080 | return -1; | |
7081 | ||
7082 | switch (type) { | |
7083 | case NFT_DATA_VALUE: | |
7084 | err = nft_value_dump(skb, data, len); | |
7085 | break; | |
7086 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 7087 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
7088 | break; |
7089 | default: | |
7090 | err = -EINVAL; | |
7091 | WARN_ON(1); | |
7092 | } | |
7093 | ||
7094 | nla_nest_end(skb, nest); | |
7095 | return err; | |
7096 | } | |
7097 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
7098 | ||
5ebe0b0e PNA |
7099 | int __nft_release_basechain(struct nft_ctx *ctx) |
7100 | { | |
7101 | struct nft_rule *rule, *nr; | |
7102 | ||
f323d954 | 7103 | BUG_ON(!nft_is_base_chain(ctx->chain)); |
5ebe0b0e | 7104 | |
c974a3a3 | 7105 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
7106 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
7107 | list_del(&rule->list); | |
7108 | ctx->chain->use--; | |
bb7b40ae | 7109 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 7110 | } |
1b2470e5 | 7111 | nft_chain_del(ctx->chain); |
5ebe0b0e | 7112 | ctx->table->use--; |
43a605f2 | 7113 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
7114 | |
7115 | return 0; | |
7116 | } | |
7117 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
7118 | ||
98319cb9 | 7119 | static void __nft_release_tables(struct net *net) |
df05ef87 | 7120 | { |
3b49e2e9 | 7121 | struct nft_flowtable *flowtable, *nf; |
df05ef87 PNA |
7122 | struct nft_table *table, *nt; |
7123 | struct nft_chain *chain, *nc; | |
e5009240 | 7124 | struct nft_object *obj, *ne; |
df05ef87 PNA |
7125 | struct nft_rule *rule, *nr; |
7126 | struct nft_set *set, *ns; | |
7127 | struct nft_ctx ctx = { | |
7128 | .net = net, | |
43a605f2 | 7129 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
7130 | }; |
7131 | ||
36596dad | 7132 | list_for_each_entry_safe(table, nt, &net->nft.tables, list) { |
98319cb9 | 7133 | ctx.family = table->family; |
dd4cbef7 | 7134 | |
df05ef87 | 7135 | list_for_each_entry(chain, &table->chains, list) |
c974a3a3 | 7136 | nf_tables_unregister_hook(net, table, chain); |
3b49e2e9 PNA |
7137 | list_for_each_entry(flowtable, &table->flowtables, list) |
7138 | nf_unregister_net_hooks(net, flowtable->ops, | |
7139 | flowtable->ops_len); | |
df05ef87 PNA |
7140 | /* No packets are walking on these chains anymore. */ |
7141 | ctx.table = table; | |
7142 | list_for_each_entry(chain, &table->chains, list) { | |
7143 | ctx.chain = chain; | |
7144 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
7145 | list_del(&rule->list); | |
7146 | chain->use--; | |
bb7b40ae | 7147 | nf_tables_rule_release(&ctx, rule); |
df05ef87 PNA |
7148 | } |
7149 | } | |
3b49e2e9 PNA |
7150 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
7151 | list_del(&flowtable->list); | |
7152 | table->use--; | |
7153 | nf_tables_flowtable_destroy(flowtable); | |
7154 | } | |
df05ef87 PNA |
7155 | list_for_each_entry_safe(set, ns, &table->sets, list) { |
7156 | list_del(&set->list); | |
7157 | table->use--; | |
7158 | nft_set_destroy(set); | |
7159 | } | |
e5009240 PNA |
7160 | list_for_each_entry_safe(obj, ne, &table->objects, list) { |
7161 | list_del(&obj->list); | |
7162 | table->use--; | |
00bfb320 | 7163 | nft_obj_destroy(&ctx, obj); |
e5009240 | 7164 | } |
df05ef87 | 7165 | list_for_each_entry_safe(chain, nc, &table->chains, list) { |
43a605f2 | 7166 | ctx.chain = chain; |
1b2470e5 | 7167 | nft_chain_del(chain); |
df05ef87 | 7168 | table->use--; |
43a605f2 | 7169 | nf_tables_chain_destroy(&ctx); |
df05ef87 PNA |
7170 | } |
7171 | list_del(&table->list); | |
7172 | nf_tables_table_destroy(&ctx); | |
7173 | } | |
7174 | } | |
7175 | ||
dd4cbef7 PNA |
7176 | static int __net_init nf_tables_init_net(struct net *net) |
7177 | { | |
7178 | INIT_LIST_HEAD(&net->nft.tables); | |
7179 | INIT_LIST_HEAD(&net->nft.commit_list); | |
7180 | net->nft.base_seq = 1; | |
a654de8f PNA |
7181 | net->nft.validate_state = NFT_VALIDATE_SKIP; |
7182 | ||
dd4cbef7 PNA |
7183 | return 0; |
7184 | } | |
7185 | ||
7186 | static void __net_exit nf_tables_exit_net(struct net *net) | |
7187 | { | |
0a2cf5ee | 7188 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
71ad00c5 FW |
7189 | if (!list_empty(&net->nft.commit_list)) |
7190 | __nf_tables_abort(net); | |
98319cb9 | 7191 | __nft_release_tables(net); |
0a2cf5ee | 7192 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
dd4cbef7 | 7193 | WARN_ON_ONCE(!list_empty(&net->nft.tables)); |
dd4cbef7 PNA |
7194 | } |
7195 | ||
99633ab2 PNA |
7196 | static struct pernet_operations nf_tables_net_ops = { |
7197 | .init = nf_tables_init_net, | |
613d0776 | 7198 | .exit = nf_tables_exit_net, |
99633ab2 PNA |
7199 | }; |
7200 | ||
96518518 PM |
7201 | static int __init nf_tables_module_init(void) |
7202 | { | |
7203 | int err; | |
7204 | ||
02c7b25e PNA |
7205 | nft_chain_filter_init(); |
7206 | ||
6da2ec56 KC |
7207 | info = kmalloc_array(NFT_RULE_MAXEXPRS, sizeof(struct nft_expr_info), |
7208 | GFP_KERNEL); | |
96518518 PM |
7209 | if (info == NULL) { |
7210 | err = -ENOMEM; | |
7211 | goto err1; | |
7212 | } | |
7213 | ||
7214 | err = nf_tables_core_module_init(); | |
7215 | if (err < 0) | |
7216 | goto err2; | |
7217 | ||
7218 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
7219 | if (err < 0) | |
7220 | goto err3; | |
7221 | ||
3b49e2e9 PNA |
7222 | register_netdevice_notifier(&nf_tables_flowtable_notifier); |
7223 | ||
99633ab2 | 7224 | return register_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
7225 | err3: |
7226 | nf_tables_core_module_exit(); | |
7227 | err2: | |
7228 | kfree(info); | |
7229 | err1: | |
7230 | return err; | |
7231 | } | |
7232 | ||
7233 | static void __exit nf_tables_module_exit(void) | |
7234 | { | |
7235 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
3b49e2e9 | 7236 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 7237 | nft_chain_filter_fini(); |
71ad00c5 | 7238 | unregister_pernet_subsys(&nf_tables_net_ops); |
1b1bc49c | 7239 | rcu_barrier(); |
96518518 PM |
7240 | nf_tables_core_module_exit(); |
7241 | kfree(info); | |
7242 | } | |
7243 | ||
7244 | module_init(nf_tables_module_init); | |
7245 | module_exit(nf_tables_module_exit); | |
7246 | ||
7247 | MODULE_LICENSE("GPL"); | |
7248 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
7249 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |