]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - net/netfilter/nf_tables_api.c
Merge tag 'drivers-3.15' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / nf_tables_api.c
1 /*
2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
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>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/net_namespace.h>
22 #include <net/sock.h>
23
24 static LIST_HEAD(nf_tables_expressions);
25
26 /**
27 * nft_register_afinfo - register nf_tables address family info
28 *
29 * @afi: address family info to register
30 *
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
33 */
34 int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
35 {
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
38 list_add_tail(&afi->list, &net->nft.af_info);
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41 }
42 EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
44 /**
45 * nft_unregister_afinfo - unregister nf_tables address family info
46 *
47 * @afi: address family info to unregister
48 *
49 * Unregister the address family for use with nf_tables.
50 */
51 void nft_unregister_afinfo(struct nft_af_info *afi)
52 {
53 nfnl_lock(NFNL_SUBSYS_NFTABLES);
54 list_del(&afi->list);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56 }
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
59 static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
60 {
61 struct nft_af_info *afi;
62
63 list_for_each_entry(afi, &net->nft.af_info, list) {
64 if (afi->family == family)
65 return afi;
66 }
67 return NULL;
68 }
69
70 static struct nft_af_info *
71 nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
72 {
73 struct nft_af_info *afi;
74
75 afi = nft_afinfo_lookup(net, family);
76 if (afi != NULL)
77 return afi;
78 #ifdef CONFIG_MODULES
79 if (autoload) {
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81 request_module("nft-afinfo-%u", family);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES);
83 afi = nft_afinfo_lookup(net, family);
84 if (afi != NULL)
85 return ERR_PTR(-EAGAIN);
86 }
87 #endif
88 return ERR_PTR(-EAFNOSUPPORT);
89 }
90
91 /*
92 * Tables
93 */
94
95 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
96 const struct nlattr *nla)
97 {
98 struct nft_table *table;
99
100 list_for_each_entry(table, &afi->tables, list) {
101 if (!nla_strcmp(nla, table->name))
102 return table;
103 }
104 return NULL;
105 }
106
107 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
108 const struct nlattr *nla)
109 {
110 struct nft_table *table;
111
112 if (nla == NULL)
113 return ERR_PTR(-EINVAL);
114
115 table = nft_table_lookup(afi, nla);
116 if (table != NULL)
117 return table;
118
119 return ERR_PTR(-ENOENT);
120 }
121
122 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
123 {
124 return ++table->hgenerator;
125 }
126
127 static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
128
129 static const struct nf_chain_type *
130 __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
131 {
132 int i;
133
134 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
135 if (chain_type[family][i] != NULL &&
136 !nla_strcmp(nla, chain_type[family][i]->name))
137 return chain_type[family][i];
138 }
139 return NULL;
140 }
141
142 static const struct nf_chain_type *
143 nf_tables_chain_type_lookup(const struct nft_af_info *afi,
144 const struct nlattr *nla,
145 bool autoload)
146 {
147 const struct nf_chain_type *type;
148
149 type = __nf_tables_chain_type_lookup(afi->family, nla);
150 if (type != NULL)
151 return type;
152 #ifdef CONFIG_MODULES
153 if (autoload) {
154 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
155 request_module("nft-chain-%u-%*.s", afi->family,
156 nla_len(nla)-1, (const char *)nla_data(nla));
157 nfnl_lock(NFNL_SUBSYS_NFTABLES);
158 type = __nf_tables_chain_type_lookup(afi->family, nla);
159 if (type != NULL)
160 return ERR_PTR(-EAGAIN);
161 }
162 #endif
163 return ERR_PTR(-ENOENT);
164 }
165
166 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
167 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
168 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
169 };
170
171 static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
172 int event, u32 flags, int family,
173 const struct nft_table *table)
174 {
175 struct nlmsghdr *nlh;
176 struct nfgenmsg *nfmsg;
177
178 event |= NFNL_SUBSYS_NFTABLES << 8;
179 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
180 if (nlh == NULL)
181 goto nla_put_failure;
182
183 nfmsg = nlmsg_data(nlh);
184 nfmsg->nfgen_family = family;
185 nfmsg->version = NFNETLINK_V0;
186 nfmsg->res_id = 0;
187
188 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
189 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
190 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
191 goto nla_put_failure;
192
193 return nlmsg_end(skb, nlh);
194
195 nla_put_failure:
196 nlmsg_trim(skb, nlh);
197 return -1;
198 }
199
200 static int nf_tables_table_notify(const struct sk_buff *oskb,
201 const struct nlmsghdr *nlh,
202 const struct nft_table *table,
203 int event, int family)
204 {
205 struct sk_buff *skb;
206 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
207 u32 seq = nlh ? nlh->nlmsg_seq : 0;
208 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
209 bool report;
210 int err;
211
212 report = nlh ? nlmsg_report(nlh) : false;
213 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
214 return 0;
215
216 err = -ENOBUFS;
217 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
218 if (skb == NULL)
219 goto err;
220
221 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
222 family, table);
223 if (err < 0) {
224 kfree_skb(skb);
225 goto err;
226 }
227
228 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
229 GFP_KERNEL);
230 err:
231 if (err < 0)
232 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
233 return err;
234 }
235
236 static int nf_tables_dump_tables(struct sk_buff *skb,
237 struct netlink_callback *cb)
238 {
239 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
240 const struct nft_af_info *afi;
241 const struct nft_table *table;
242 unsigned int idx = 0, s_idx = cb->args[0];
243 struct net *net = sock_net(skb->sk);
244 int family = nfmsg->nfgen_family;
245
246 list_for_each_entry(afi, &net->nft.af_info, list) {
247 if (family != NFPROTO_UNSPEC && family != afi->family)
248 continue;
249
250 list_for_each_entry(table, &afi->tables, list) {
251 if (idx < s_idx)
252 goto cont;
253 if (idx > s_idx)
254 memset(&cb->args[1], 0,
255 sizeof(cb->args) - sizeof(cb->args[0]));
256 if (nf_tables_fill_table_info(skb,
257 NETLINK_CB(cb->skb).portid,
258 cb->nlh->nlmsg_seq,
259 NFT_MSG_NEWTABLE,
260 NLM_F_MULTI,
261 afi->family, table) < 0)
262 goto done;
263 cont:
264 idx++;
265 }
266 }
267 done:
268 cb->args[0] = idx;
269 return skb->len;
270 }
271
272 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
273 const struct nlmsghdr *nlh,
274 const struct nlattr * const nla[])
275 {
276 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
277 const struct nft_af_info *afi;
278 const struct nft_table *table;
279 struct sk_buff *skb2;
280 struct net *net = sock_net(skb->sk);
281 int family = nfmsg->nfgen_family;
282 int err;
283
284 if (nlh->nlmsg_flags & NLM_F_DUMP) {
285 struct netlink_dump_control c = {
286 .dump = nf_tables_dump_tables,
287 };
288 return netlink_dump_start(nlsk, skb, nlh, &c);
289 }
290
291 afi = nf_tables_afinfo_lookup(net, family, false);
292 if (IS_ERR(afi))
293 return PTR_ERR(afi);
294
295 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
296 if (IS_ERR(table))
297 return PTR_ERR(table);
298
299 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
300 if (!skb2)
301 return -ENOMEM;
302
303 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
304 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
305 family, table);
306 if (err < 0)
307 goto err;
308
309 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
310
311 err:
312 kfree_skb(skb2);
313 return err;
314 }
315
316 static int nf_tables_table_enable(const struct nft_af_info *afi,
317 struct nft_table *table)
318 {
319 struct nft_chain *chain;
320 int err, i = 0;
321
322 list_for_each_entry(chain, &table->chains, list) {
323 if (!(chain->flags & NFT_BASE_CHAIN))
324 continue;
325
326 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
327 if (err < 0)
328 goto err;
329
330 i++;
331 }
332 return 0;
333 err:
334 list_for_each_entry(chain, &table->chains, list) {
335 if (!(chain->flags & NFT_BASE_CHAIN))
336 continue;
337
338 if (i-- <= 0)
339 break;
340
341 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
342 }
343 return err;
344 }
345
346 static int nf_tables_table_disable(const struct nft_af_info *afi,
347 struct nft_table *table)
348 {
349 struct nft_chain *chain;
350
351 list_for_each_entry(chain, &table->chains, list) {
352 if (chain->flags & NFT_BASE_CHAIN)
353 nf_unregister_hooks(nft_base_chain(chain)->ops,
354 afi->nops);
355 }
356
357 return 0;
358 }
359
360 static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
361 const struct nlmsghdr *nlh,
362 const struct nlattr * const nla[],
363 struct nft_af_info *afi, struct nft_table *table)
364 {
365 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
366 int family = nfmsg->nfgen_family, ret = 0;
367
368 if (nla[NFTA_TABLE_FLAGS]) {
369 u32 flags;
370
371 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
372 if (flags & ~NFT_TABLE_F_DORMANT)
373 return -EINVAL;
374
375 if ((flags & NFT_TABLE_F_DORMANT) &&
376 !(table->flags & NFT_TABLE_F_DORMANT)) {
377 ret = nf_tables_table_disable(afi, table);
378 if (ret >= 0)
379 table->flags |= NFT_TABLE_F_DORMANT;
380 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
381 table->flags & NFT_TABLE_F_DORMANT) {
382 ret = nf_tables_table_enable(afi, table);
383 if (ret >= 0)
384 table->flags &= ~NFT_TABLE_F_DORMANT;
385 }
386 if (ret < 0)
387 goto err;
388 }
389
390 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
391 err:
392 return ret;
393 }
394
395 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
396 const struct nlmsghdr *nlh,
397 const struct nlattr * const nla[])
398 {
399 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
400 const struct nlattr *name;
401 struct nft_af_info *afi;
402 struct nft_table *table;
403 struct net *net = sock_net(skb->sk);
404 int family = nfmsg->nfgen_family;
405 u32 flags = 0;
406
407 afi = nf_tables_afinfo_lookup(net, family, true);
408 if (IS_ERR(afi))
409 return PTR_ERR(afi);
410
411 name = nla[NFTA_TABLE_NAME];
412 table = nf_tables_table_lookup(afi, name);
413 if (IS_ERR(table)) {
414 if (PTR_ERR(table) != -ENOENT)
415 return PTR_ERR(table);
416 table = NULL;
417 }
418
419 if (table != NULL) {
420 if (nlh->nlmsg_flags & NLM_F_EXCL)
421 return -EEXIST;
422 if (nlh->nlmsg_flags & NLM_F_REPLACE)
423 return -EOPNOTSUPP;
424 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
425 }
426
427 if (nla[NFTA_TABLE_FLAGS]) {
428 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
429 if (flags & ~NFT_TABLE_F_DORMANT)
430 return -EINVAL;
431 }
432
433 if (!try_module_get(afi->owner))
434 return -EAFNOSUPPORT;
435
436 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
437 if (table == NULL) {
438 module_put(afi->owner);
439 return -ENOMEM;
440 }
441
442 nla_strlcpy(table->name, name, nla_len(name));
443 INIT_LIST_HEAD(&table->chains);
444 INIT_LIST_HEAD(&table->sets);
445 table->flags = flags;
446
447 list_add_tail(&table->list, &afi->tables);
448 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
449 return 0;
450 }
451
452 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
453 const struct nlmsghdr *nlh,
454 const struct nlattr * const nla[])
455 {
456 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
457 struct nft_af_info *afi;
458 struct nft_table *table;
459 struct net *net = sock_net(skb->sk);
460 int family = nfmsg->nfgen_family;
461
462 afi = nf_tables_afinfo_lookup(net, family, false);
463 if (IS_ERR(afi))
464 return PTR_ERR(afi);
465
466 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
467 if (IS_ERR(table))
468 return PTR_ERR(table);
469
470 if (!list_empty(&table->chains) || !list_empty(&table->sets))
471 return -EBUSY;
472
473 list_del(&table->list);
474 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
475 kfree(table);
476 module_put(afi->owner);
477 return 0;
478 }
479
480 int nft_register_chain_type(const struct nf_chain_type *ctype)
481 {
482 int err = 0;
483
484 nfnl_lock(NFNL_SUBSYS_NFTABLES);
485 if (chain_type[ctype->family][ctype->type] != NULL) {
486 err = -EBUSY;
487 goto out;
488 }
489 chain_type[ctype->family][ctype->type] = ctype;
490 out:
491 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
492 return err;
493 }
494 EXPORT_SYMBOL_GPL(nft_register_chain_type);
495
496 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
497 {
498 nfnl_lock(NFNL_SUBSYS_NFTABLES);
499 chain_type[ctype->family][ctype->type] = NULL;
500 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
501 }
502 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
503
504 /*
505 * Chains
506 */
507
508 static struct nft_chain *
509 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
510 {
511 struct nft_chain *chain;
512
513 list_for_each_entry(chain, &table->chains, list) {
514 if (chain->handle == handle)
515 return chain;
516 }
517
518 return ERR_PTR(-ENOENT);
519 }
520
521 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
522 const struct nlattr *nla)
523 {
524 struct nft_chain *chain;
525
526 if (nla == NULL)
527 return ERR_PTR(-EINVAL);
528
529 list_for_each_entry(chain, &table->chains, list) {
530 if (!nla_strcmp(nla, chain->name))
531 return chain;
532 }
533
534 return ERR_PTR(-ENOENT);
535 }
536
537 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
538 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
539 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
540 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
541 .len = NFT_CHAIN_MAXNAMELEN - 1 },
542 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
543 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
544 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
545 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
546 };
547
548 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
549 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
550 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
551 };
552
553 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
554 {
555 struct nft_stats *cpu_stats, total;
556 struct nlattr *nest;
557 int cpu;
558
559 memset(&total, 0, sizeof(total));
560 for_each_possible_cpu(cpu) {
561 cpu_stats = per_cpu_ptr(stats, cpu);
562 total.pkts += cpu_stats->pkts;
563 total.bytes += cpu_stats->bytes;
564 }
565 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
566 if (nest == NULL)
567 goto nla_put_failure;
568
569 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
570 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
571 goto nla_put_failure;
572
573 nla_nest_end(skb, nest);
574 return 0;
575
576 nla_put_failure:
577 return -ENOSPC;
578 }
579
580 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
581 int event, u32 flags, int family,
582 const struct nft_table *table,
583 const struct nft_chain *chain)
584 {
585 struct nlmsghdr *nlh;
586 struct nfgenmsg *nfmsg;
587
588 event |= NFNL_SUBSYS_NFTABLES << 8;
589 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
590 if (nlh == NULL)
591 goto nla_put_failure;
592
593 nfmsg = nlmsg_data(nlh);
594 nfmsg->nfgen_family = family;
595 nfmsg->version = NFNETLINK_V0;
596 nfmsg->res_id = 0;
597
598 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
599 goto nla_put_failure;
600 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
601 goto nla_put_failure;
602 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
603 goto nla_put_failure;
604
605 if (chain->flags & NFT_BASE_CHAIN) {
606 const struct nft_base_chain *basechain = nft_base_chain(chain);
607 const struct nf_hook_ops *ops = &basechain->ops[0];
608 struct nlattr *nest;
609
610 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
611 if (nest == NULL)
612 goto nla_put_failure;
613 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
614 goto nla_put_failure;
615 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
616 goto nla_put_failure;
617 nla_nest_end(skb, nest);
618
619 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
620 htonl(basechain->policy)))
621 goto nla_put_failure;
622
623 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
624 goto nla_put_failure;
625
626 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
627 goto nla_put_failure;
628 }
629
630 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
631 goto nla_put_failure;
632
633 return nlmsg_end(skb, nlh);
634
635 nla_put_failure:
636 nlmsg_trim(skb, nlh);
637 return -1;
638 }
639
640 static int nf_tables_chain_notify(const struct sk_buff *oskb,
641 const struct nlmsghdr *nlh,
642 const struct nft_table *table,
643 const struct nft_chain *chain,
644 int event, int family)
645 {
646 struct sk_buff *skb;
647 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
648 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
649 u32 seq = nlh ? nlh->nlmsg_seq : 0;
650 bool report;
651 int err;
652
653 report = nlh ? nlmsg_report(nlh) : false;
654 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
655 return 0;
656
657 err = -ENOBUFS;
658 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
659 if (skb == NULL)
660 goto err;
661
662 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
663 table, chain);
664 if (err < 0) {
665 kfree_skb(skb);
666 goto err;
667 }
668
669 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
670 GFP_KERNEL);
671 err:
672 if (err < 0)
673 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
674 return err;
675 }
676
677 static int nf_tables_dump_chains(struct sk_buff *skb,
678 struct netlink_callback *cb)
679 {
680 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
681 const struct nft_af_info *afi;
682 const struct nft_table *table;
683 const struct nft_chain *chain;
684 unsigned int idx = 0, s_idx = cb->args[0];
685 struct net *net = sock_net(skb->sk);
686 int family = nfmsg->nfgen_family;
687
688 list_for_each_entry(afi, &net->nft.af_info, list) {
689 if (family != NFPROTO_UNSPEC && family != afi->family)
690 continue;
691
692 list_for_each_entry(table, &afi->tables, list) {
693 list_for_each_entry(chain, &table->chains, list) {
694 if (idx < s_idx)
695 goto cont;
696 if (idx > s_idx)
697 memset(&cb->args[1], 0,
698 sizeof(cb->args) - sizeof(cb->args[0]));
699 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
700 cb->nlh->nlmsg_seq,
701 NFT_MSG_NEWCHAIN,
702 NLM_F_MULTI,
703 afi->family, table, chain) < 0)
704 goto done;
705 cont:
706 idx++;
707 }
708 }
709 }
710 done:
711 cb->args[0] = idx;
712 return skb->len;
713 }
714
715
716 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
717 const struct nlmsghdr *nlh,
718 const struct nlattr * const nla[])
719 {
720 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
721 const struct nft_af_info *afi;
722 const struct nft_table *table;
723 const struct nft_chain *chain;
724 struct sk_buff *skb2;
725 struct net *net = sock_net(skb->sk);
726 int family = nfmsg->nfgen_family;
727 int err;
728
729 if (nlh->nlmsg_flags & NLM_F_DUMP) {
730 struct netlink_dump_control c = {
731 .dump = nf_tables_dump_chains,
732 };
733 return netlink_dump_start(nlsk, skb, nlh, &c);
734 }
735
736 afi = nf_tables_afinfo_lookup(net, family, false);
737 if (IS_ERR(afi))
738 return PTR_ERR(afi);
739
740 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
741 if (IS_ERR(table))
742 return PTR_ERR(table);
743
744 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
745 if (IS_ERR(chain))
746 return PTR_ERR(chain);
747
748 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
749 if (!skb2)
750 return -ENOMEM;
751
752 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
753 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
754 family, table, chain);
755 if (err < 0)
756 goto err;
757
758 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
759
760 err:
761 kfree_skb(skb2);
762 return err;
763 }
764
765 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
766 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
767 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
768 };
769
770 static int
771 nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
772 {
773 struct nlattr *tb[NFTA_COUNTER_MAX+1];
774 struct nft_stats __percpu *newstats;
775 struct nft_stats *stats;
776 int err;
777
778 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
779 if (err < 0)
780 return err;
781
782 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
783 return -EINVAL;
784
785 newstats = alloc_percpu(struct nft_stats);
786 if (newstats == NULL)
787 return -ENOMEM;
788
789 /* Restore old counters on this cpu, no problem. Per-cpu statistics
790 * are not exposed to userspace.
791 */
792 stats = this_cpu_ptr(newstats);
793 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
794 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
795
796 if (chain->stats) {
797 struct nft_stats __percpu *oldstats =
798 nft_dereference(chain->stats);
799
800 rcu_assign_pointer(chain->stats, newstats);
801 synchronize_rcu();
802 free_percpu(oldstats);
803 } else
804 rcu_assign_pointer(chain->stats, newstats);
805
806 return 0;
807 }
808
809 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
810 const struct nlmsghdr *nlh,
811 const struct nlattr * const nla[])
812 {
813 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
814 const struct nlattr * uninitialized_var(name);
815 const struct nft_af_info *afi;
816 struct nft_table *table;
817 struct nft_chain *chain;
818 struct nft_base_chain *basechain = NULL;
819 struct nlattr *ha[NFTA_HOOK_MAX + 1];
820 struct net *net = sock_net(skb->sk);
821 int family = nfmsg->nfgen_family;
822 u8 policy = NF_ACCEPT;
823 u64 handle = 0;
824 unsigned int i;
825 int err;
826 bool create;
827
828 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
829
830 afi = nf_tables_afinfo_lookup(net, family, true);
831 if (IS_ERR(afi))
832 return PTR_ERR(afi);
833
834 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
835 if (IS_ERR(table))
836 return PTR_ERR(table);
837
838 chain = NULL;
839 name = nla[NFTA_CHAIN_NAME];
840
841 if (nla[NFTA_CHAIN_HANDLE]) {
842 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
843 chain = nf_tables_chain_lookup_byhandle(table, handle);
844 if (IS_ERR(chain))
845 return PTR_ERR(chain);
846 } else {
847 chain = nf_tables_chain_lookup(table, name);
848 if (IS_ERR(chain)) {
849 if (PTR_ERR(chain) != -ENOENT)
850 return PTR_ERR(chain);
851 chain = NULL;
852 }
853 }
854
855 if (nla[NFTA_CHAIN_POLICY]) {
856 if ((chain != NULL &&
857 !(chain->flags & NFT_BASE_CHAIN)) ||
858 nla[NFTA_CHAIN_HOOK] == NULL)
859 return -EOPNOTSUPP;
860
861 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
862 switch (policy) {
863 case NF_DROP:
864 case NF_ACCEPT:
865 break;
866 default:
867 return -EINVAL;
868 }
869 }
870
871 if (chain != NULL) {
872 if (nlh->nlmsg_flags & NLM_F_EXCL)
873 return -EEXIST;
874 if (nlh->nlmsg_flags & NLM_F_REPLACE)
875 return -EOPNOTSUPP;
876
877 if (nla[NFTA_CHAIN_HANDLE] && name &&
878 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
879 return -EEXIST;
880
881 if (nla[NFTA_CHAIN_COUNTERS]) {
882 if (!(chain->flags & NFT_BASE_CHAIN))
883 return -EOPNOTSUPP;
884
885 err = nf_tables_counters(nft_base_chain(chain),
886 nla[NFTA_CHAIN_COUNTERS]);
887 if (err < 0)
888 return err;
889 }
890
891 if (nla[NFTA_CHAIN_POLICY])
892 nft_base_chain(chain)->policy = policy;
893
894 if (nla[NFTA_CHAIN_HANDLE] && name)
895 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
896
897 goto notify;
898 }
899
900 if (table->use == UINT_MAX)
901 return -EOVERFLOW;
902
903 if (nla[NFTA_CHAIN_HOOK]) {
904 const struct nf_chain_type *type;
905 struct nf_hook_ops *ops;
906 nf_hookfn *hookfn;
907 u32 hooknum, priority;
908
909 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
910 if (nla[NFTA_CHAIN_TYPE]) {
911 type = nf_tables_chain_type_lookup(afi,
912 nla[NFTA_CHAIN_TYPE],
913 create);
914 if (IS_ERR(type))
915 return PTR_ERR(type);
916 }
917
918 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
919 nft_hook_policy);
920 if (err < 0)
921 return err;
922 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
923 ha[NFTA_HOOK_PRIORITY] == NULL)
924 return -EINVAL;
925
926 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
927 if (hooknum >= afi->nhooks)
928 return -EINVAL;
929 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
930
931 if (!(type->hook_mask & (1 << hooknum)))
932 return -EOPNOTSUPP;
933 if (!try_module_get(type->owner))
934 return -ENOENT;
935 hookfn = type->hooks[hooknum];
936
937 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
938 if (basechain == NULL)
939 return -ENOMEM;
940
941 if (nla[NFTA_CHAIN_COUNTERS]) {
942 err = nf_tables_counters(basechain,
943 nla[NFTA_CHAIN_COUNTERS]);
944 if (err < 0) {
945 module_put(type->owner);
946 kfree(basechain);
947 return err;
948 }
949 } else {
950 struct nft_stats __percpu *newstats;
951
952 newstats = alloc_percpu(struct nft_stats);
953 if (newstats == NULL) {
954 module_put(type->owner);
955 kfree(basechain);
956 return -ENOMEM;
957 }
958 rcu_assign_pointer(basechain->stats, newstats);
959 }
960
961 basechain->type = type;
962 chain = &basechain->chain;
963
964 for (i = 0; i < afi->nops; i++) {
965 ops = &basechain->ops[i];
966 ops->pf = family;
967 ops->owner = afi->owner;
968 ops->hooknum = hooknum;
969 ops->priority = priority;
970 ops->priv = chain;
971 ops->hook = afi->hooks[ops->hooknum];
972 if (hookfn)
973 ops->hook = hookfn;
974 if (afi->hook_ops_init)
975 afi->hook_ops_init(ops, i);
976 }
977
978 chain->flags |= NFT_BASE_CHAIN;
979 basechain->policy = policy;
980 } else {
981 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
982 if (chain == NULL)
983 return -ENOMEM;
984 }
985
986 INIT_LIST_HEAD(&chain->rules);
987 chain->handle = nf_tables_alloc_handle(table);
988 chain->net = net;
989 chain->table = table;
990 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
991
992 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
993 chain->flags & NFT_BASE_CHAIN) {
994 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
995 if (err < 0) {
996 module_put(basechain->type->owner);
997 free_percpu(basechain->stats);
998 kfree(basechain);
999 return err;
1000 }
1001 }
1002 list_add_tail(&chain->list, &table->chains);
1003 table->use++;
1004 notify:
1005 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1006 family);
1007 return 0;
1008 }
1009
1010 static void nf_tables_chain_destroy(struct nft_chain *chain)
1011 {
1012 BUG_ON(chain->use > 0);
1013
1014 if (chain->flags & NFT_BASE_CHAIN) {
1015 module_put(nft_base_chain(chain)->type->owner);
1016 free_percpu(nft_base_chain(chain)->stats);
1017 kfree(nft_base_chain(chain));
1018 } else
1019 kfree(chain);
1020 }
1021
1022 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1023 const struct nlmsghdr *nlh,
1024 const struct nlattr * const nla[])
1025 {
1026 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1027 const struct nft_af_info *afi;
1028 struct nft_table *table;
1029 struct nft_chain *chain;
1030 struct net *net = sock_net(skb->sk);
1031 int family = nfmsg->nfgen_family;
1032
1033 afi = nf_tables_afinfo_lookup(net, family, false);
1034 if (IS_ERR(afi))
1035 return PTR_ERR(afi);
1036
1037 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1038 if (IS_ERR(table))
1039 return PTR_ERR(table);
1040
1041 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1042 if (IS_ERR(chain))
1043 return PTR_ERR(chain);
1044
1045 if (!list_empty(&chain->rules) || chain->use > 0)
1046 return -EBUSY;
1047
1048 list_del(&chain->list);
1049 table->use--;
1050
1051 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1052 chain->flags & NFT_BASE_CHAIN)
1053 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
1054
1055 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1056 family);
1057
1058 /* Make sure all rule references are gone before this is released */
1059 synchronize_rcu();
1060
1061 nf_tables_chain_destroy(chain);
1062 return 0;
1063 }
1064
1065 static void nft_ctx_init(struct nft_ctx *ctx,
1066 const struct sk_buff *skb,
1067 const struct nlmsghdr *nlh,
1068 const struct nft_af_info *afi,
1069 const struct nft_table *table,
1070 const struct nft_chain *chain,
1071 const struct nlattr * const *nla)
1072 {
1073 ctx->net = sock_net(skb->sk);
1074 ctx->skb = skb;
1075 ctx->nlh = nlh;
1076 ctx->afi = afi;
1077 ctx->table = table;
1078 ctx->chain = chain;
1079 ctx->nla = nla;
1080 }
1081
1082 /*
1083 * Expressions
1084 */
1085
1086 /**
1087 * nft_register_expr - register nf_tables expr type
1088 * @ops: expr type
1089 *
1090 * Registers the expr type for use with nf_tables. Returns zero on
1091 * success or a negative errno code otherwise.
1092 */
1093 int nft_register_expr(struct nft_expr_type *type)
1094 {
1095 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1096 list_add_tail(&type->list, &nf_tables_expressions);
1097 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1098 return 0;
1099 }
1100 EXPORT_SYMBOL_GPL(nft_register_expr);
1101
1102 /**
1103 * nft_unregister_expr - unregister nf_tables expr type
1104 * @ops: expr type
1105 *
1106 * Unregisters the expr typefor use with nf_tables.
1107 */
1108 void nft_unregister_expr(struct nft_expr_type *type)
1109 {
1110 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1111 list_del(&type->list);
1112 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1113 }
1114 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1115
1116 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1117 struct nlattr *nla)
1118 {
1119 const struct nft_expr_type *type;
1120
1121 list_for_each_entry(type, &nf_tables_expressions, list) {
1122 if (!nla_strcmp(nla, type->name) &&
1123 (!type->family || type->family == family))
1124 return type;
1125 }
1126 return NULL;
1127 }
1128
1129 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1130 struct nlattr *nla)
1131 {
1132 const struct nft_expr_type *type;
1133
1134 if (nla == NULL)
1135 return ERR_PTR(-EINVAL);
1136
1137 type = __nft_expr_type_get(family, nla);
1138 if (type != NULL && try_module_get(type->owner))
1139 return type;
1140
1141 #ifdef CONFIG_MODULES
1142 if (type == NULL) {
1143 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1144 request_module("nft-expr-%u-%.*s", family,
1145 nla_len(nla), (char *)nla_data(nla));
1146 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1147 if (__nft_expr_type_get(family, nla))
1148 return ERR_PTR(-EAGAIN);
1149
1150 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1151 request_module("nft-expr-%.*s",
1152 nla_len(nla), (char *)nla_data(nla));
1153 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1154 if (__nft_expr_type_get(family, nla))
1155 return ERR_PTR(-EAGAIN);
1156 }
1157 #endif
1158 return ERR_PTR(-ENOENT);
1159 }
1160
1161 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1162 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1163 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1164 };
1165
1166 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1167 const struct nft_expr *expr)
1168 {
1169 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1170 goto nla_put_failure;
1171
1172 if (expr->ops->dump) {
1173 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1174 if (data == NULL)
1175 goto nla_put_failure;
1176 if (expr->ops->dump(skb, expr) < 0)
1177 goto nla_put_failure;
1178 nla_nest_end(skb, data);
1179 }
1180
1181 return skb->len;
1182
1183 nla_put_failure:
1184 return -1;
1185 };
1186
1187 struct nft_expr_info {
1188 const struct nft_expr_ops *ops;
1189 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1190 };
1191
1192 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1193 const struct nlattr *nla,
1194 struct nft_expr_info *info)
1195 {
1196 const struct nft_expr_type *type;
1197 const struct nft_expr_ops *ops;
1198 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1199 int err;
1200
1201 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1202 if (err < 0)
1203 return err;
1204
1205 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1206 if (IS_ERR(type))
1207 return PTR_ERR(type);
1208
1209 if (tb[NFTA_EXPR_DATA]) {
1210 err = nla_parse_nested(info->tb, type->maxattr,
1211 tb[NFTA_EXPR_DATA], type->policy);
1212 if (err < 0)
1213 goto err1;
1214 } else
1215 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1216
1217 if (type->select_ops != NULL) {
1218 ops = type->select_ops(ctx,
1219 (const struct nlattr * const *)info->tb);
1220 if (IS_ERR(ops)) {
1221 err = PTR_ERR(ops);
1222 goto err1;
1223 }
1224 } else
1225 ops = type->ops;
1226
1227 info->ops = ops;
1228 return 0;
1229
1230 err1:
1231 module_put(type->owner);
1232 return err;
1233 }
1234
1235 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1236 const struct nft_expr_info *info,
1237 struct nft_expr *expr)
1238 {
1239 const struct nft_expr_ops *ops = info->ops;
1240 int err;
1241
1242 expr->ops = ops;
1243 if (ops->init) {
1244 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1245 if (err < 0)
1246 goto err1;
1247 }
1248
1249 return 0;
1250
1251 err1:
1252 expr->ops = NULL;
1253 return err;
1254 }
1255
1256 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1257 struct nft_expr *expr)
1258 {
1259 if (expr->ops->destroy)
1260 expr->ops->destroy(ctx, expr);
1261 module_put(expr->ops->type->owner);
1262 }
1263
1264 /*
1265 * Rules
1266 */
1267
1268 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1269 u64 handle)
1270 {
1271 struct nft_rule *rule;
1272
1273 // FIXME: this sucks
1274 list_for_each_entry(rule, &chain->rules, list) {
1275 if (handle == rule->handle)
1276 return rule;
1277 }
1278
1279 return ERR_PTR(-ENOENT);
1280 }
1281
1282 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1283 const struct nlattr *nla)
1284 {
1285 if (nla == NULL)
1286 return ERR_PTR(-EINVAL);
1287
1288 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1289 }
1290
1291 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1292 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1293 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1294 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1295 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1296 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1297 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1298 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1299 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1300 .len = NFT_USERDATA_MAXLEN },
1301 };
1302
1303 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1304 int event, u32 flags, int family,
1305 const struct nft_table *table,
1306 const struct nft_chain *chain,
1307 const struct nft_rule *rule)
1308 {
1309 struct nlmsghdr *nlh;
1310 struct nfgenmsg *nfmsg;
1311 const struct nft_expr *expr, *next;
1312 struct nlattr *list;
1313 const struct nft_rule *prule;
1314 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1315
1316 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1317 flags);
1318 if (nlh == NULL)
1319 goto nla_put_failure;
1320
1321 nfmsg = nlmsg_data(nlh);
1322 nfmsg->nfgen_family = family;
1323 nfmsg->version = NFNETLINK_V0;
1324 nfmsg->res_id = 0;
1325
1326 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1327 goto nla_put_failure;
1328 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1329 goto nla_put_failure;
1330 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1331 goto nla_put_failure;
1332
1333 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1334 prule = list_entry(rule->list.prev, struct nft_rule, list);
1335 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1336 cpu_to_be64(prule->handle)))
1337 goto nla_put_failure;
1338 }
1339
1340 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1341 if (list == NULL)
1342 goto nla_put_failure;
1343 nft_rule_for_each_expr(expr, next, rule) {
1344 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1345 if (elem == NULL)
1346 goto nla_put_failure;
1347 if (nf_tables_fill_expr_info(skb, expr) < 0)
1348 goto nla_put_failure;
1349 nla_nest_end(skb, elem);
1350 }
1351 nla_nest_end(skb, list);
1352
1353 if (rule->ulen &&
1354 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1355 goto nla_put_failure;
1356
1357 return nlmsg_end(skb, nlh);
1358
1359 nla_put_failure:
1360 nlmsg_trim(skb, nlh);
1361 return -1;
1362 }
1363
1364 static int nf_tables_rule_notify(const struct sk_buff *oskb,
1365 const struct nlmsghdr *nlh,
1366 const struct nft_table *table,
1367 const struct nft_chain *chain,
1368 const struct nft_rule *rule,
1369 int event, u32 flags, int family)
1370 {
1371 struct sk_buff *skb;
1372 u32 portid = NETLINK_CB(oskb).portid;
1373 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1374 u32 seq = nlh->nlmsg_seq;
1375 bool report;
1376 int err;
1377
1378 report = nlmsg_report(nlh);
1379 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1380 return 0;
1381
1382 err = -ENOBUFS;
1383 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1384 if (skb == NULL)
1385 goto err;
1386
1387 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1388 family, table, chain, rule);
1389 if (err < 0) {
1390 kfree_skb(skb);
1391 goto err;
1392 }
1393
1394 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1395 GFP_KERNEL);
1396 err:
1397 if (err < 0)
1398 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1399 return err;
1400 }
1401
1402 static inline bool
1403 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1404 {
1405 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1406 }
1407
1408 static inline int gencursor_next(struct net *net)
1409 {
1410 return net->nft.gencursor+1 == 1 ? 1 : 0;
1411 }
1412
1413 static inline int
1414 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1415 {
1416 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1417 }
1418
1419 static inline void
1420 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1421 {
1422 /* Now inactive, will be active in the future */
1423 rule->genmask = (1 << net->nft.gencursor);
1424 }
1425
1426 static inline void
1427 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1428 {
1429 rule->genmask = (1 << gencursor_next(net));
1430 }
1431
1432 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1433 {
1434 rule->genmask = 0;
1435 }
1436
1437 static int nf_tables_dump_rules(struct sk_buff *skb,
1438 struct netlink_callback *cb)
1439 {
1440 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1441 const struct nft_af_info *afi;
1442 const struct nft_table *table;
1443 const struct nft_chain *chain;
1444 const struct nft_rule *rule;
1445 unsigned int idx = 0, s_idx = cb->args[0];
1446 struct net *net = sock_net(skb->sk);
1447 int family = nfmsg->nfgen_family;
1448 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1449 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1450
1451 list_for_each_entry(afi, &net->nft.af_info, list) {
1452 if (family != NFPROTO_UNSPEC && family != afi->family)
1453 continue;
1454
1455 list_for_each_entry(table, &afi->tables, list) {
1456 list_for_each_entry(chain, &table->chains, list) {
1457 list_for_each_entry(rule, &chain->rules, list) {
1458 if (!nft_rule_is_active(net, rule))
1459 goto cont;
1460 if (idx < s_idx)
1461 goto cont;
1462 if (idx > s_idx)
1463 memset(&cb->args[1], 0,
1464 sizeof(cb->args) - sizeof(cb->args[0]));
1465 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1466 cb->nlh->nlmsg_seq,
1467 NFT_MSG_NEWRULE,
1468 NLM_F_MULTI | NLM_F_APPEND,
1469 afi->family, table, chain, rule) < 0)
1470 goto done;
1471 cont:
1472 idx++;
1473 }
1474 }
1475 }
1476 }
1477 done:
1478 /* Invalidate this dump, a transition to the new generation happened */
1479 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1480 return -EBUSY;
1481
1482 cb->args[0] = idx;
1483 return skb->len;
1484 }
1485
1486 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1487 const struct nlmsghdr *nlh,
1488 const struct nlattr * const nla[])
1489 {
1490 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1491 const struct nft_af_info *afi;
1492 const struct nft_table *table;
1493 const struct nft_chain *chain;
1494 const struct nft_rule *rule;
1495 struct sk_buff *skb2;
1496 struct net *net = sock_net(skb->sk);
1497 int family = nfmsg->nfgen_family;
1498 int err;
1499
1500 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1501 struct netlink_dump_control c = {
1502 .dump = nf_tables_dump_rules,
1503 };
1504 return netlink_dump_start(nlsk, skb, nlh, &c);
1505 }
1506
1507 afi = nf_tables_afinfo_lookup(net, family, false);
1508 if (IS_ERR(afi))
1509 return PTR_ERR(afi);
1510
1511 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1512 if (IS_ERR(table))
1513 return PTR_ERR(table);
1514
1515 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1516 if (IS_ERR(chain))
1517 return PTR_ERR(chain);
1518
1519 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1520 if (IS_ERR(rule))
1521 return PTR_ERR(rule);
1522
1523 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1524 if (!skb2)
1525 return -ENOMEM;
1526
1527 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1528 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1529 family, table, chain, rule);
1530 if (err < 0)
1531 goto err;
1532
1533 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1534
1535 err:
1536 kfree_skb(skb2);
1537 return err;
1538 }
1539
1540 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1541 struct nft_rule *rule)
1542 {
1543 struct nft_expr *expr;
1544
1545 /*
1546 * Careful: some expressions might not be initialized in case this
1547 * is called on error from nf_tables_newrule().
1548 */
1549 expr = nft_expr_first(rule);
1550 while (expr->ops && expr != nft_expr_last(rule)) {
1551 nf_tables_expr_destroy(ctx, expr);
1552 expr = nft_expr_next(expr);
1553 }
1554 kfree(rule);
1555 }
1556
1557 #define NFT_RULE_MAXEXPRS 128
1558
1559 static struct nft_expr_info *info;
1560
1561 static struct nft_rule_trans *
1562 nf_tables_trans_add(struct nft_ctx *ctx, struct nft_rule *rule)
1563 {
1564 struct nft_rule_trans *rupd;
1565
1566 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1567 if (rupd == NULL)
1568 return NULL;
1569
1570 rupd->ctx = *ctx;
1571 rupd->rule = rule;
1572 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1573
1574 return rupd;
1575 }
1576
1577 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1578 const struct nlmsghdr *nlh,
1579 const struct nlattr * const nla[])
1580 {
1581 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1582 const struct nft_af_info *afi;
1583 struct net *net = sock_net(skb->sk);
1584 struct nft_table *table;
1585 struct nft_chain *chain;
1586 struct nft_rule *rule, *old_rule = NULL;
1587 struct nft_rule_trans *repl = NULL;
1588 struct nft_expr *expr;
1589 struct nft_ctx ctx;
1590 struct nlattr *tmp;
1591 unsigned int size, i, n, ulen = 0;
1592 int err, rem;
1593 bool create;
1594 u64 handle, pos_handle;
1595
1596 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1597
1598 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1599 if (IS_ERR(afi))
1600 return PTR_ERR(afi);
1601
1602 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1603 if (IS_ERR(table))
1604 return PTR_ERR(table);
1605
1606 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1607 if (IS_ERR(chain))
1608 return PTR_ERR(chain);
1609
1610 if (nla[NFTA_RULE_HANDLE]) {
1611 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1612 rule = __nf_tables_rule_lookup(chain, handle);
1613 if (IS_ERR(rule))
1614 return PTR_ERR(rule);
1615
1616 if (nlh->nlmsg_flags & NLM_F_EXCL)
1617 return -EEXIST;
1618 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1619 old_rule = rule;
1620 else
1621 return -EOPNOTSUPP;
1622 } else {
1623 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1624 return -EINVAL;
1625 handle = nf_tables_alloc_handle(table);
1626 }
1627
1628 if (nla[NFTA_RULE_POSITION]) {
1629 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1630 return -EOPNOTSUPP;
1631
1632 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1633 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1634 if (IS_ERR(old_rule))
1635 return PTR_ERR(old_rule);
1636 }
1637
1638 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1639
1640 n = 0;
1641 size = 0;
1642 if (nla[NFTA_RULE_EXPRESSIONS]) {
1643 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1644 err = -EINVAL;
1645 if (nla_type(tmp) != NFTA_LIST_ELEM)
1646 goto err1;
1647 if (n == NFT_RULE_MAXEXPRS)
1648 goto err1;
1649 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1650 if (err < 0)
1651 goto err1;
1652 size += info[n].ops->size;
1653 n++;
1654 }
1655 }
1656
1657 if (nla[NFTA_RULE_USERDATA])
1658 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1659
1660 err = -ENOMEM;
1661 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
1662 if (rule == NULL)
1663 goto err1;
1664
1665 nft_rule_activate_next(net, rule);
1666
1667 rule->handle = handle;
1668 rule->dlen = size;
1669 rule->ulen = ulen;
1670
1671 if (ulen)
1672 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
1673
1674 expr = nft_expr_first(rule);
1675 for (i = 0; i < n; i++) {
1676 err = nf_tables_newexpr(&ctx, &info[i], expr);
1677 if (err < 0)
1678 goto err2;
1679 info[i].ops = NULL;
1680 expr = nft_expr_next(expr);
1681 }
1682
1683 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1684 if (nft_rule_is_active_next(net, old_rule)) {
1685 repl = nf_tables_trans_add(&ctx, old_rule);
1686 if (repl == NULL) {
1687 err = -ENOMEM;
1688 goto err2;
1689 }
1690 nft_rule_disactivate_next(net, old_rule);
1691 list_add_tail(&rule->list, &old_rule->list);
1692 } else {
1693 err = -ENOENT;
1694 goto err2;
1695 }
1696 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1697 if (old_rule)
1698 list_add_rcu(&rule->list, &old_rule->list);
1699 else
1700 list_add_tail_rcu(&rule->list, &chain->rules);
1701 else {
1702 if (old_rule)
1703 list_add_tail_rcu(&rule->list, &old_rule->list);
1704 else
1705 list_add_rcu(&rule->list, &chain->rules);
1706 }
1707
1708 if (nf_tables_trans_add(&ctx, rule) == NULL) {
1709 err = -ENOMEM;
1710 goto err3;
1711 }
1712 return 0;
1713
1714 err3:
1715 list_del_rcu(&rule->list);
1716 if (repl) {
1717 list_del_rcu(&repl->rule->list);
1718 list_del(&repl->list);
1719 nft_rule_clear(net, repl->rule);
1720 kfree(repl);
1721 }
1722 err2:
1723 nf_tables_rule_destroy(&ctx, rule);
1724 err1:
1725 for (i = 0; i < n; i++) {
1726 if (info[i].ops != NULL)
1727 module_put(info[i].ops->type->owner);
1728 }
1729 return err;
1730 }
1731
1732 static int
1733 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1734 {
1735 /* You cannot delete the same rule twice */
1736 if (nft_rule_is_active_next(ctx->net, rule)) {
1737 if (nf_tables_trans_add(ctx, rule) == NULL)
1738 return -ENOMEM;
1739 nft_rule_disactivate_next(ctx->net, rule);
1740 return 0;
1741 }
1742 return -ENOENT;
1743 }
1744
1745 static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1746 {
1747 struct nft_rule *rule;
1748 int err;
1749
1750 list_for_each_entry(rule, &ctx->chain->rules, list) {
1751 err = nf_tables_delrule_one(ctx, rule);
1752 if (err < 0)
1753 return err;
1754 }
1755 return 0;
1756 }
1757
1758 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1759 const struct nlmsghdr *nlh,
1760 const struct nlattr * const nla[])
1761 {
1762 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1763 const struct nft_af_info *afi;
1764 struct net *net = sock_net(skb->sk);
1765 const struct nft_table *table;
1766 struct nft_chain *chain = NULL;
1767 struct nft_rule *rule;
1768 int family = nfmsg->nfgen_family, err = 0;
1769 struct nft_ctx ctx;
1770
1771 afi = nf_tables_afinfo_lookup(net, family, false);
1772 if (IS_ERR(afi))
1773 return PTR_ERR(afi);
1774
1775 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1776 if (IS_ERR(table))
1777 return PTR_ERR(table);
1778
1779 if (nla[NFTA_RULE_CHAIN]) {
1780 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1781 if (IS_ERR(chain))
1782 return PTR_ERR(chain);
1783 }
1784
1785 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1786
1787 if (chain) {
1788 if (nla[NFTA_RULE_HANDLE]) {
1789 rule = nf_tables_rule_lookup(chain,
1790 nla[NFTA_RULE_HANDLE]);
1791 if (IS_ERR(rule))
1792 return PTR_ERR(rule);
1793
1794 err = nf_tables_delrule_one(&ctx, rule);
1795 } else {
1796 err = nf_table_delrule_by_chain(&ctx);
1797 }
1798 } else {
1799 list_for_each_entry(chain, &table->chains, list) {
1800 ctx.chain = chain;
1801 err = nf_table_delrule_by_chain(&ctx);
1802 if (err < 0)
1803 break;
1804 }
1805 }
1806
1807 return err;
1808 }
1809
1810 static int nf_tables_commit(struct sk_buff *skb)
1811 {
1812 struct net *net = sock_net(skb->sk);
1813 struct nft_rule_trans *rupd, *tmp;
1814
1815 /* Bump generation counter, invalidate any dump in progress */
1816 net->nft.genctr++;
1817
1818 /* A new generation has just started */
1819 net->nft.gencursor = gencursor_next(net);
1820
1821 /* Make sure all packets have left the previous generation before
1822 * purging old rules.
1823 */
1824 synchronize_rcu();
1825
1826 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1827 /* This rule was inactive in the past and just became active.
1828 * Clear the next bit of the genmask since its meaning has
1829 * changed, now it is the future.
1830 */
1831 if (nft_rule_is_active(net, rupd->rule)) {
1832 nft_rule_clear(net, rupd->rule);
1833 nf_tables_rule_notify(skb, rupd->ctx.nlh,
1834 rupd->ctx.table, rupd->ctx.chain,
1835 rupd->rule, NFT_MSG_NEWRULE, 0,
1836 rupd->ctx.afi->family);
1837 list_del(&rupd->list);
1838 kfree(rupd);
1839 continue;
1840 }
1841
1842 /* This rule is in the past, get rid of it */
1843 list_del_rcu(&rupd->rule->list);
1844 nf_tables_rule_notify(skb, rupd->ctx.nlh,
1845 rupd->ctx.table, rupd->ctx.chain,
1846 rupd->rule, NFT_MSG_DELRULE, 0,
1847 rupd->ctx.afi->family);
1848 }
1849
1850 /* Make sure we don't see any packet traversing old rules */
1851 synchronize_rcu();
1852
1853 /* Now we can safely release unused old rules */
1854 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1855 nf_tables_rule_destroy(&rupd->ctx, rupd->rule);
1856 list_del(&rupd->list);
1857 kfree(rupd);
1858 }
1859
1860 return 0;
1861 }
1862
1863 static int nf_tables_abort(struct sk_buff *skb)
1864 {
1865 struct net *net = sock_net(skb->sk);
1866 struct nft_rule_trans *rupd, *tmp;
1867
1868 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1869 if (!nft_rule_is_active_next(net, rupd->rule)) {
1870 nft_rule_clear(net, rupd->rule);
1871 list_del(&rupd->list);
1872 kfree(rupd);
1873 continue;
1874 }
1875
1876 /* This rule is inactive, get rid of it */
1877 list_del_rcu(&rupd->rule->list);
1878 }
1879
1880 /* Make sure we don't see any packet accessing aborted rules */
1881 synchronize_rcu();
1882
1883 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1884 nf_tables_rule_destroy(&rupd->ctx, rupd->rule);
1885 list_del(&rupd->list);
1886 kfree(rupd);
1887 }
1888
1889 return 0;
1890 }
1891
1892 /*
1893 * Sets
1894 */
1895
1896 static LIST_HEAD(nf_tables_set_ops);
1897
1898 int nft_register_set(struct nft_set_ops *ops)
1899 {
1900 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1901 list_add_tail(&ops->list, &nf_tables_set_ops);
1902 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1903 return 0;
1904 }
1905 EXPORT_SYMBOL_GPL(nft_register_set);
1906
1907 void nft_unregister_set(struct nft_set_ops *ops)
1908 {
1909 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1910 list_del(&ops->list);
1911 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1912 }
1913 EXPORT_SYMBOL_GPL(nft_unregister_set);
1914
1915 static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1916 {
1917 const struct nft_set_ops *ops;
1918 u32 features;
1919
1920 #ifdef CONFIG_MODULES
1921 if (list_empty(&nf_tables_set_ops)) {
1922 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1923 request_module("nft-set");
1924 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1925 if (!list_empty(&nf_tables_set_ops))
1926 return ERR_PTR(-EAGAIN);
1927 }
1928 #endif
1929 features = 0;
1930 if (nla[NFTA_SET_FLAGS] != NULL) {
1931 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1932 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1933 }
1934
1935 // FIXME: implement selection properly
1936 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1937 if ((ops->features & features) != features)
1938 continue;
1939 if (!try_module_get(ops->owner))
1940 continue;
1941 return ops;
1942 }
1943
1944 return ERR_PTR(-EOPNOTSUPP);
1945 }
1946
1947 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1948 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1949 [NFTA_SET_NAME] = { .type = NLA_STRING },
1950 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1951 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1952 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1953 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1954 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1955 };
1956
1957 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1958 const struct sk_buff *skb,
1959 const struct nlmsghdr *nlh,
1960 const struct nlattr * const nla[])
1961 {
1962 struct net *net = sock_net(skb->sk);
1963 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1964 const struct nft_af_info *afi = NULL;
1965 const struct nft_table *table = NULL;
1966
1967 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1968 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1969 if (IS_ERR(afi))
1970 return PTR_ERR(afi);
1971 }
1972
1973 if (nla[NFTA_SET_TABLE] != NULL) {
1974 if (afi == NULL)
1975 return -EAFNOSUPPORT;
1976
1977 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1978 if (IS_ERR(table))
1979 return PTR_ERR(table);
1980 }
1981
1982 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1983 return 0;
1984 }
1985
1986 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1987 const struct nlattr *nla)
1988 {
1989 struct nft_set *set;
1990
1991 if (nla == NULL)
1992 return ERR_PTR(-EINVAL);
1993
1994 list_for_each_entry(set, &table->sets, list) {
1995 if (!nla_strcmp(nla, set->name))
1996 return set;
1997 }
1998 return ERR_PTR(-ENOENT);
1999 }
2000
2001 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2002 const char *name)
2003 {
2004 const struct nft_set *i;
2005 const char *p;
2006 unsigned long *inuse;
2007 unsigned int n = 0;
2008
2009 p = strnchr(name, IFNAMSIZ, '%');
2010 if (p != NULL) {
2011 if (p[1] != 'd' || strchr(p + 2, '%'))
2012 return -EINVAL;
2013
2014 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2015 if (inuse == NULL)
2016 return -ENOMEM;
2017
2018 list_for_each_entry(i, &ctx->table->sets, list) {
2019 int tmp;
2020
2021 if (!sscanf(i->name, name, &tmp))
2022 continue;
2023 if (tmp < 0 || tmp >= BITS_PER_BYTE * PAGE_SIZE)
2024 continue;
2025
2026 set_bit(tmp, inuse);
2027 }
2028
2029 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2030 free_page((unsigned long)inuse);
2031 }
2032
2033 snprintf(set->name, sizeof(set->name), name, n);
2034 list_for_each_entry(i, &ctx->table->sets, list) {
2035 if (!strcmp(set->name, i->name))
2036 return -ENFILE;
2037 }
2038 return 0;
2039 }
2040
2041 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2042 const struct nft_set *set, u16 event, u16 flags)
2043 {
2044 struct nfgenmsg *nfmsg;
2045 struct nlmsghdr *nlh;
2046 u32 portid = NETLINK_CB(ctx->skb).portid;
2047 u32 seq = ctx->nlh->nlmsg_seq;
2048
2049 event |= NFNL_SUBSYS_NFTABLES << 8;
2050 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2051 flags);
2052 if (nlh == NULL)
2053 goto nla_put_failure;
2054
2055 nfmsg = nlmsg_data(nlh);
2056 nfmsg->nfgen_family = ctx->afi->family;
2057 nfmsg->version = NFNETLINK_V0;
2058 nfmsg->res_id = 0;
2059
2060 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2061 goto nla_put_failure;
2062 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2063 goto nla_put_failure;
2064 if (set->flags != 0)
2065 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2066 goto nla_put_failure;
2067
2068 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2069 goto nla_put_failure;
2070 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2071 goto nla_put_failure;
2072 if (set->flags & NFT_SET_MAP) {
2073 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2074 goto nla_put_failure;
2075 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2076 goto nla_put_failure;
2077 }
2078
2079 return nlmsg_end(skb, nlh);
2080
2081 nla_put_failure:
2082 nlmsg_trim(skb, nlh);
2083 return -1;
2084 }
2085
2086 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2087 const struct nft_set *set,
2088 int event)
2089 {
2090 struct sk_buff *skb;
2091 u32 portid = NETLINK_CB(ctx->skb).portid;
2092 bool report;
2093 int err;
2094
2095 report = nlmsg_report(ctx->nlh);
2096 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2097 return 0;
2098
2099 err = -ENOBUFS;
2100 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2101 if (skb == NULL)
2102 goto err;
2103
2104 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2105 if (err < 0) {
2106 kfree_skb(skb);
2107 goto err;
2108 }
2109
2110 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2111 GFP_KERNEL);
2112 err:
2113 if (err < 0)
2114 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2115 return err;
2116 }
2117
2118 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2119 struct netlink_callback *cb)
2120 {
2121 const struct nft_set *set;
2122 unsigned int idx = 0, s_idx = cb->args[0];
2123
2124 if (cb->args[1])
2125 return skb->len;
2126
2127 list_for_each_entry(set, &ctx->table->sets, list) {
2128 if (idx < s_idx)
2129 goto cont;
2130 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2131 NLM_F_MULTI) < 0) {
2132 cb->args[0] = idx;
2133 goto done;
2134 }
2135 cont:
2136 idx++;
2137 }
2138 cb->args[1] = 1;
2139 done:
2140 return skb->len;
2141 }
2142
2143 static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2144 struct netlink_callback *cb)
2145 {
2146 const struct nft_set *set;
2147 unsigned int idx, s_idx = cb->args[0];
2148 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2149
2150 if (cb->args[1])
2151 return skb->len;
2152
2153 list_for_each_entry(table, &ctx->afi->tables, list) {
2154 if (cur_table) {
2155 if (cur_table != table)
2156 continue;
2157
2158 cur_table = NULL;
2159 }
2160 ctx->table = table;
2161 idx = 0;
2162 list_for_each_entry(set, &ctx->table->sets, list) {
2163 if (idx < s_idx)
2164 goto cont;
2165 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2166 NLM_F_MULTI) < 0) {
2167 cb->args[0] = idx;
2168 cb->args[2] = (unsigned long) table;
2169 goto done;
2170 }
2171 cont:
2172 idx++;
2173 }
2174 }
2175 cb->args[1] = 1;
2176 done:
2177 return skb->len;
2178 }
2179
2180 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2181 struct netlink_callback *cb)
2182 {
2183 const struct nft_set *set;
2184 unsigned int idx, s_idx = cb->args[0];
2185 const struct nft_af_info *afi;
2186 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2187 struct net *net = sock_net(skb->sk);
2188 int cur_family = cb->args[3];
2189
2190 if (cb->args[1])
2191 return skb->len;
2192
2193 list_for_each_entry(afi, &net->nft.af_info, list) {
2194 if (cur_family) {
2195 if (afi->family != cur_family)
2196 continue;
2197
2198 cur_family = 0;
2199 }
2200
2201 list_for_each_entry(table, &afi->tables, list) {
2202 if (cur_table) {
2203 if (cur_table != table)
2204 continue;
2205
2206 cur_table = NULL;
2207 }
2208
2209 ctx->table = table;
2210 ctx->afi = afi;
2211 idx = 0;
2212 list_for_each_entry(set, &ctx->table->sets, list) {
2213 if (idx < s_idx)
2214 goto cont;
2215 if (nf_tables_fill_set(skb, ctx, set,
2216 NFT_MSG_NEWSET,
2217 NLM_F_MULTI) < 0) {
2218 cb->args[0] = idx;
2219 cb->args[2] = (unsigned long) table;
2220 cb->args[3] = afi->family;
2221 goto done;
2222 }
2223 cont:
2224 idx++;
2225 }
2226 if (s_idx)
2227 s_idx = 0;
2228 }
2229 }
2230 cb->args[1] = 1;
2231 done:
2232 return skb->len;
2233 }
2234
2235 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2236 {
2237 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2238 struct nlattr *nla[NFTA_SET_MAX + 1];
2239 struct nft_ctx ctx;
2240 int err, ret;
2241
2242 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2243 nft_set_policy);
2244 if (err < 0)
2245 return err;
2246
2247 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2248 if (err < 0)
2249 return err;
2250
2251 if (ctx.table == NULL) {
2252 if (ctx.afi == NULL)
2253 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2254 else
2255 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2256 } else
2257 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2258
2259 return ret;
2260 }
2261
2262 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2263 const struct nlmsghdr *nlh,
2264 const struct nlattr * const nla[])
2265 {
2266 const struct nft_set *set;
2267 struct nft_ctx ctx;
2268 struct sk_buff *skb2;
2269 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2270 int err;
2271
2272 /* Verify existance before starting dump */
2273 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2274 if (err < 0)
2275 return err;
2276
2277 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2278 struct netlink_dump_control c = {
2279 .dump = nf_tables_dump_sets,
2280 };
2281 return netlink_dump_start(nlsk, skb, nlh, &c);
2282 }
2283
2284 /* Only accept unspec with dump */
2285 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2286 return -EAFNOSUPPORT;
2287
2288 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2289 if (IS_ERR(set))
2290 return PTR_ERR(set);
2291
2292 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2293 if (skb2 == NULL)
2294 return -ENOMEM;
2295
2296 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2297 if (err < 0)
2298 goto err;
2299
2300 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2301
2302 err:
2303 kfree_skb(skb2);
2304 return err;
2305 }
2306
2307 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2308 const struct nlmsghdr *nlh,
2309 const struct nlattr * const nla[])
2310 {
2311 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2312 const struct nft_set_ops *ops;
2313 const struct nft_af_info *afi;
2314 struct net *net = sock_net(skb->sk);
2315 struct nft_table *table;
2316 struct nft_set *set;
2317 struct nft_ctx ctx;
2318 char name[IFNAMSIZ];
2319 unsigned int size;
2320 bool create;
2321 u32 ktype, klen, dlen, dtype, flags;
2322 int err;
2323
2324 if (nla[NFTA_SET_TABLE] == NULL ||
2325 nla[NFTA_SET_NAME] == NULL ||
2326 nla[NFTA_SET_KEY_LEN] == NULL)
2327 return -EINVAL;
2328
2329 ktype = NFT_DATA_VALUE;
2330 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2331 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2332 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2333 return -EINVAL;
2334 }
2335
2336 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2337 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2338 return -EINVAL;
2339
2340 flags = 0;
2341 if (nla[NFTA_SET_FLAGS] != NULL) {
2342 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2343 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2344 NFT_SET_INTERVAL | NFT_SET_MAP))
2345 return -EINVAL;
2346 }
2347
2348 dtype = 0;
2349 dlen = 0;
2350 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2351 if (!(flags & NFT_SET_MAP))
2352 return -EINVAL;
2353
2354 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2355 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2356 dtype != NFT_DATA_VERDICT)
2357 return -EINVAL;
2358
2359 if (dtype != NFT_DATA_VERDICT) {
2360 if (nla[NFTA_SET_DATA_LEN] == NULL)
2361 return -EINVAL;
2362 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2363 if (dlen == 0 ||
2364 dlen > FIELD_SIZEOF(struct nft_data, data))
2365 return -EINVAL;
2366 } else
2367 dlen = sizeof(struct nft_data);
2368 } else if (flags & NFT_SET_MAP)
2369 return -EINVAL;
2370
2371 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2372
2373 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2374 if (IS_ERR(afi))
2375 return PTR_ERR(afi);
2376
2377 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2378 if (IS_ERR(table))
2379 return PTR_ERR(table);
2380
2381 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2382
2383 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2384 if (IS_ERR(set)) {
2385 if (PTR_ERR(set) != -ENOENT)
2386 return PTR_ERR(set);
2387 set = NULL;
2388 }
2389
2390 if (set != NULL) {
2391 if (nlh->nlmsg_flags & NLM_F_EXCL)
2392 return -EEXIST;
2393 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2394 return -EOPNOTSUPP;
2395 return 0;
2396 }
2397
2398 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2399 return -ENOENT;
2400
2401 ops = nft_select_set_ops(nla);
2402 if (IS_ERR(ops))
2403 return PTR_ERR(ops);
2404
2405 size = 0;
2406 if (ops->privsize != NULL)
2407 size = ops->privsize(nla);
2408
2409 err = -ENOMEM;
2410 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2411 if (set == NULL)
2412 goto err1;
2413
2414 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2415 err = nf_tables_set_alloc_name(&ctx, set, name);
2416 if (err < 0)
2417 goto err2;
2418
2419 INIT_LIST_HEAD(&set->bindings);
2420 set->ops = ops;
2421 set->ktype = ktype;
2422 set->klen = klen;
2423 set->dtype = dtype;
2424 set->dlen = dlen;
2425 set->flags = flags;
2426
2427 err = ops->init(set, nla);
2428 if (err < 0)
2429 goto err2;
2430
2431 list_add_tail(&set->list, &table->sets);
2432 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2433 return 0;
2434
2435 err2:
2436 kfree(set);
2437 err1:
2438 module_put(ops->owner);
2439 return err;
2440 }
2441
2442 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2443 {
2444 list_del(&set->list);
2445 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2446
2447 set->ops->destroy(set);
2448 module_put(set->ops->owner);
2449 kfree(set);
2450 }
2451
2452 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2453 const struct nlmsghdr *nlh,
2454 const struct nlattr * const nla[])
2455 {
2456 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2457 struct nft_set *set;
2458 struct nft_ctx ctx;
2459 int err;
2460
2461 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2462 return -EAFNOSUPPORT;
2463 if (nla[NFTA_SET_TABLE] == NULL)
2464 return -EINVAL;
2465
2466 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2467 if (err < 0)
2468 return err;
2469
2470 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2471 if (IS_ERR(set))
2472 return PTR_ERR(set);
2473 if (!list_empty(&set->bindings))
2474 return -EBUSY;
2475
2476 nf_tables_set_destroy(&ctx, set);
2477 return 0;
2478 }
2479
2480 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2481 const struct nft_set *set,
2482 const struct nft_set_iter *iter,
2483 const struct nft_set_elem *elem)
2484 {
2485 enum nft_registers dreg;
2486
2487 dreg = nft_type_to_reg(set->dtype);
2488 return nft_validate_data_load(ctx, dreg, &elem->data,
2489 set->dtype == NFT_DATA_VERDICT ?
2490 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2491 }
2492
2493 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2494 struct nft_set_binding *binding)
2495 {
2496 struct nft_set_binding *i;
2497 struct nft_set_iter iter;
2498
2499 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2500 return -EBUSY;
2501
2502 if (set->flags & NFT_SET_MAP) {
2503 /* If the set is already bound to the same chain all
2504 * jumps are already validated for that chain.
2505 */
2506 list_for_each_entry(i, &set->bindings, list) {
2507 if (i->chain == binding->chain)
2508 goto bind;
2509 }
2510
2511 iter.skip = 0;
2512 iter.count = 0;
2513 iter.err = 0;
2514 iter.fn = nf_tables_bind_check_setelem;
2515
2516 set->ops->walk(ctx, set, &iter);
2517 if (iter.err < 0) {
2518 /* Destroy anonymous sets if binding fails */
2519 if (set->flags & NFT_SET_ANONYMOUS)
2520 nf_tables_set_destroy(ctx, set);
2521
2522 return iter.err;
2523 }
2524 }
2525 bind:
2526 binding->chain = ctx->chain;
2527 list_add_tail(&binding->list, &set->bindings);
2528 return 0;
2529 }
2530
2531 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2532 struct nft_set_binding *binding)
2533 {
2534 list_del(&binding->list);
2535
2536 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2537 nf_tables_set_destroy(ctx, set);
2538 }
2539
2540 /*
2541 * Set elements
2542 */
2543
2544 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2545 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2546 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2547 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2548 };
2549
2550 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2551 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2552 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2553 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2554 };
2555
2556 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2557 const struct sk_buff *skb,
2558 const struct nlmsghdr *nlh,
2559 const struct nlattr * const nla[])
2560 {
2561 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2562 const struct nft_af_info *afi;
2563 const struct nft_table *table;
2564 struct net *net = sock_net(skb->sk);
2565
2566 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2567 if (IS_ERR(afi))
2568 return PTR_ERR(afi);
2569
2570 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2571 if (IS_ERR(table))
2572 return PTR_ERR(table);
2573
2574 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2575 return 0;
2576 }
2577
2578 static int nf_tables_fill_setelem(struct sk_buff *skb,
2579 const struct nft_set *set,
2580 const struct nft_set_elem *elem)
2581 {
2582 unsigned char *b = skb_tail_pointer(skb);
2583 struct nlattr *nest;
2584
2585 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2586 if (nest == NULL)
2587 goto nla_put_failure;
2588
2589 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2590 set->klen) < 0)
2591 goto nla_put_failure;
2592
2593 if (set->flags & NFT_SET_MAP &&
2594 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2595 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2596 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2597 set->dlen) < 0)
2598 goto nla_put_failure;
2599
2600 if (elem->flags != 0)
2601 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2602 goto nla_put_failure;
2603
2604 nla_nest_end(skb, nest);
2605 return 0;
2606
2607 nla_put_failure:
2608 nlmsg_trim(skb, b);
2609 return -EMSGSIZE;
2610 }
2611
2612 struct nft_set_dump_args {
2613 const struct netlink_callback *cb;
2614 struct nft_set_iter iter;
2615 struct sk_buff *skb;
2616 };
2617
2618 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2619 const struct nft_set *set,
2620 const struct nft_set_iter *iter,
2621 const struct nft_set_elem *elem)
2622 {
2623 struct nft_set_dump_args *args;
2624
2625 args = container_of(iter, struct nft_set_dump_args, iter);
2626 return nf_tables_fill_setelem(args->skb, set, elem);
2627 }
2628
2629 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2630 {
2631 const struct nft_set *set;
2632 struct nft_set_dump_args args;
2633 struct nft_ctx ctx;
2634 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2635 struct nfgenmsg *nfmsg;
2636 struct nlmsghdr *nlh;
2637 struct nlattr *nest;
2638 u32 portid, seq;
2639 int event, err;
2640
2641 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2642 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2643 if (err < 0)
2644 return err;
2645
2646 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2647 if (err < 0)
2648 return err;
2649
2650 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2651 if (IS_ERR(set))
2652 return PTR_ERR(set);
2653
2654 event = NFT_MSG_NEWSETELEM;
2655 event |= NFNL_SUBSYS_NFTABLES << 8;
2656 portid = NETLINK_CB(cb->skb).portid;
2657 seq = cb->nlh->nlmsg_seq;
2658
2659 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2660 NLM_F_MULTI);
2661 if (nlh == NULL)
2662 goto nla_put_failure;
2663
2664 nfmsg = nlmsg_data(nlh);
2665 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2666 nfmsg->version = NFNETLINK_V0;
2667 nfmsg->res_id = 0;
2668
2669 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2670 goto nla_put_failure;
2671 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2672 goto nla_put_failure;
2673
2674 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2675 if (nest == NULL)
2676 goto nla_put_failure;
2677
2678 args.cb = cb;
2679 args.skb = skb;
2680 args.iter.skip = cb->args[0];
2681 args.iter.count = 0;
2682 args.iter.err = 0;
2683 args.iter.fn = nf_tables_dump_setelem;
2684 set->ops->walk(&ctx, set, &args.iter);
2685
2686 nla_nest_end(skb, nest);
2687 nlmsg_end(skb, nlh);
2688
2689 if (args.iter.err && args.iter.err != -EMSGSIZE)
2690 return args.iter.err;
2691 if (args.iter.count == cb->args[0])
2692 return 0;
2693
2694 cb->args[0] = args.iter.count;
2695 return skb->len;
2696
2697 nla_put_failure:
2698 return -ENOSPC;
2699 }
2700
2701 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2702 const struct nlmsghdr *nlh,
2703 const struct nlattr * const nla[])
2704 {
2705 const struct nft_set *set;
2706 struct nft_ctx ctx;
2707 int err;
2708
2709 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2710 if (err < 0)
2711 return err;
2712
2713 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2714 if (IS_ERR(set))
2715 return PTR_ERR(set);
2716
2717 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2718 struct netlink_dump_control c = {
2719 .dump = nf_tables_dump_set,
2720 };
2721 return netlink_dump_start(nlsk, skb, nlh, &c);
2722 }
2723 return -EOPNOTSUPP;
2724 }
2725
2726 static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2727 const struct nlattr *attr)
2728 {
2729 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2730 struct nft_data_desc d1, d2;
2731 struct nft_set_elem elem;
2732 struct nft_set_binding *binding;
2733 enum nft_registers dreg;
2734 int err;
2735
2736 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2737 nft_set_elem_policy);
2738 if (err < 0)
2739 return err;
2740
2741 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2742 return -EINVAL;
2743
2744 elem.flags = 0;
2745 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2746 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2747 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2748 return -EINVAL;
2749 }
2750
2751 if (set->flags & NFT_SET_MAP) {
2752 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2753 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2754 return -EINVAL;
2755 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
2756 elem.flags & NFT_SET_ELEM_INTERVAL_END)
2757 return -EINVAL;
2758 } else {
2759 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2760 return -EINVAL;
2761 }
2762
2763 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2764 if (err < 0)
2765 goto err1;
2766 err = -EINVAL;
2767 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2768 goto err2;
2769
2770 err = -EEXIST;
2771 if (set->ops->get(set, &elem) == 0)
2772 goto err2;
2773
2774 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2775 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2776 if (err < 0)
2777 goto err2;
2778
2779 err = -EINVAL;
2780 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2781 goto err3;
2782
2783 dreg = nft_type_to_reg(set->dtype);
2784 list_for_each_entry(binding, &set->bindings, list) {
2785 struct nft_ctx bind_ctx = {
2786 .afi = ctx->afi,
2787 .table = ctx->table,
2788 .chain = binding->chain,
2789 };
2790
2791 err = nft_validate_data_load(&bind_ctx, dreg,
2792 &elem.data, d2.type);
2793 if (err < 0)
2794 goto err3;
2795 }
2796 }
2797
2798 err = set->ops->insert(set, &elem);
2799 if (err < 0)
2800 goto err3;
2801
2802 return 0;
2803
2804 err3:
2805 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2806 nft_data_uninit(&elem.data, d2.type);
2807 err2:
2808 nft_data_uninit(&elem.key, d1.type);
2809 err1:
2810 return err;
2811 }
2812
2813 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2814 const struct nlmsghdr *nlh,
2815 const struct nlattr * const nla[])
2816 {
2817 const struct nlattr *attr;
2818 struct nft_set *set;
2819 struct nft_ctx ctx;
2820 int rem, err;
2821
2822 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2823 if (err < 0)
2824 return err;
2825
2826 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2827 if (IS_ERR(set))
2828 return PTR_ERR(set);
2829 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2830 return -EBUSY;
2831
2832 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2833 err = nft_add_set_elem(&ctx, set, attr);
2834 if (err < 0)
2835 return err;
2836 }
2837 return 0;
2838 }
2839
2840 static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2841 const struct nlattr *attr)
2842 {
2843 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2844 struct nft_data_desc desc;
2845 struct nft_set_elem elem;
2846 int err;
2847
2848 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2849 nft_set_elem_policy);
2850 if (err < 0)
2851 goto err1;
2852
2853 err = -EINVAL;
2854 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2855 goto err1;
2856
2857 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2858 if (err < 0)
2859 goto err1;
2860
2861 err = -EINVAL;
2862 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2863 goto err2;
2864
2865 err = set->ops->get(set, &elem);
2866 if (err < 0)
2867 goto err2;
2868
2869 set->ops->remove(set, &elem);
2870
2871 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2872 if (set->flags & NFT_SET_MAP)
2873 nft_data_uninit(&elem.data, set->dtype);
2874
2875 err2:
2876 nft_data_uninit(&elem.key, desc.type);
2877 err1:
2878 return err;
2879 }
2880
2881 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2882 const struct nlmsghdr *nlh,
2883 const struct nlattr * const nla[])
2884 {
2885 const struct nlattr *attr;
2886 struct nft_set *set;
2887 struct nft_ctx ctx;
2888 int rem, err;
2889
2890 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2891 if (err < 0)
2892 return err;
2893
2894 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2895 if (IS_ERR(set))
2896 return PTR_ERR(set);
2897 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2898 return -EBUSY;
2899
2900 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2901 err = nft_del_setelem(&ctx, set, attr);
2902 if (err < 0)
2903 return err;
2904 }
2905 return 0;
2906 }
2907
2908 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2909 [NFT_MSG_NEWTABLE] = {
2910 .call = nf_tables_newtable,
2911 .attr_count = NFTA_TABLE_MAX,
2912 .policy = nft_table_policy,
2913 },
2914 [NFT_MSG_GETTABLE] = {
2915 .call = nf_tables_gettable,
2916 .attr_count = NFTA_TABLE_MAX,
2917 .policy = nft_table_policy,
2918 },
2919 [NFT_MSG_DELTABLE] = {
2920 .call = nf_tables_deltable,
2921 .attr_count = NFTA_TABLE_MAX,
2922 .policy = nft_table_policy,
2923 },
2924 [NFT_MSG_NEWCHAIN] = {
2925 .call = nf_tables_newchain,
2926 .attr_count = NFTA_CHAIN_MAX,
2927 .policy = nft_chain_policy,
2928 },
2929 [NFT_MSG_GETCHAIN] = {
2930 .call = nf_tables_getchain,
2931 .attr_count = NFTA_CHAIN_MAX,
2932 .policy = nft_chain_policy,
2933 },
2934 [NFT_MSG_DELCHAIN] = {
2935 .call = nf_tables_delchain,
2936 .attr_count = NFTA_CHAIN_MAX,
2937 .policy = nft_chain_policy,
2938 },
2939 [NFT_MSG_NEWRULE] = {
2940 .call_batch = nf_tables_newrule,
2941 .attr_count = NFTA_RULE_MAX,
2942 .policy = nft_rule_policy,
2943 },
2944 [NFT_MSG_GETRULE] = {
2945 .call = nf_tables_getrule,
2946 .attr_count = NFTA_RULE_MAX,
2947 .policy = nft_rule_policy,
2948 },
2949 [NFT_MSG_DELRULE] = {
2950 .call_batch = nf_tables_delrule,
2951 .attr_count = NFTA_RULE_MAX,
2952 .policy = nft_rule_policy,
2953 },
2954 [NFT_MSG_NEWSET] = {
2955 .call = nf_tables_newset,
2956 .attr_count = NFTA_SET_MAX,
2957 .policy = nft_set_policy,
2958 },
2959 [NFT_MSG_GETSET] = {
2960 .call = nf_tables_getset,
2961 .attr_count = NFTA_SET_MAX,
2962 .policy = nft_set_policy,
2963 },
2964 [NFT_MSG_DELSET] = {
2965 .call = nf_tables_delset,
2966 .attr_count = NFTA_SET_MAX,
2967 .policy = nft_set_policy,
2968 },
2969 [NFT_MSG_NEWSETELEM] = {
2970 .call = nf_tables_newsetelem,
2971 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2972 .policy = nft_set_elem_list_policy,
2973 },
2974 [NFT_MSG_GETSETELEM] = {
2975 .call = nf_tables_getsetelem,
2976 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2977 .policy = nft_set_elem_list_policy,
2978 },
2979 [NFT_MSG_DELSETELEM] = {
2980 .call = nf_tables_delsetelem,
2981 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2982 .policy = nft_set_elem_list_policy,
2983 },
2984 };
2985
2986 static const struct nfnetlink_subsystem nf_tables_subsys = {
2987 .name = "nf_tables",
2988 .subsys_id = NFNL_SUBSYS_NFTABLES,
2989 .cb_count = NFT_MSG_MAX,
2990 .cb = nf_tables_cb,
2991 .commit = nf_tables_commit,
2992 .abort = nf_tables_abort,
2993 };
2994
2995 /*
2996 * Loop detection - walk through the ruleset beginning at the destination chain
2997 * of a new jump until either the source chain is reached (loop) or all
2998 * reachable chains have been traversed.
2999 *
3000 * The loop check is performed whenever a new jump verdict is added to an
3001 * expression or verdict map or a verdict map is bound to a new chain.
3002 */
3003
3004 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3005 const struct nft_chain *chain);
3006
3007 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3008 const struct nft_set *set,
3009 const struct nft_set_iter *iter,
3010 const struct nft_set_elem *elem)
3011 {
3012 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3013 return 0;
3014
3015 switch (elem->data.verdict) {
3016 case NFT_JUMP:
3017 case NFT_GOTO:
3018 return nf_tables_check_loops(ctx, elem->data.chain);
3019 default:
3020 return 0;
3021 }
3022 }
3023
3024 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3025 const struct nft_chain *chain)
3026 {
3027 const struct nft_rule *rule;
3028 const struct nft_expr *expr, *last;
3029 const struct nft_set *set;
3030 struct nft_set_binding *binding;
3031 struct nft_set_iter iter;
3032
3033 if (ctx->chain == chain)
3034 return -ELOOP;
3035
3036 list_for_each_entry(rule, &chain->rules, list) {
3037 nft_rule_for_each_expr(expr, last, rule) {
3038 const struct nft_data *data = NULL;
3039 int err;
3040
3041 if (!expr->ops->validate)
3042 continue;
3043
3044 err = expr->ops->validate(ctx, expr, &data);
3045 if (err < 0)
3046 return err;
3047
3048 if (data == NULL)
3049 continue;
3050
3051 switch (data->verdict) {
3052 case NFT_JUMP:
3053 case NFT_GOTO:
3054 err = nf_tables_check_loops(ctx, data->chain);
3055 if (err < 0)
3056 return err;
3057 default:
3058 break;
3059 }
3060 }
3061 }
3062
3063 list_for_each_entry(set, &ctx->table->sets, list) {
3064 if (!(set->flags & NFT_SET_MAP) ||
3065 set->dtype != NFT_DATA_VERDICT)
3066 continue;
3067
3068 list_for_each_entry(binding, &set->bindings, list) {
3069 if (binding->chain != chain)
3070 continue;
3071
3072 iter.skip = 0;
3073 iter.count = 0;
3074 iter.err = 0;
3075 iter.fn = nf_tables_loop_check_setelem;
3076
3077 set->ops->walk(ctx, set, &iter);
3078 if (iter.err < 0)
3079 return iter.err;
3080 }
3081 }
3082
3083 return 0;
3084 }
3085
3086 /**
3087 * nft_validate_input_register - validate an expressions' input register
3088 *
3089 * @reg: the register number
3090 *
3091 * Validate that the input register is one of the general purpose
3092 * registers.
3093 */
3094 int nft_validate_input_register(enum nft_registers reg)
3095 {
3096 if (reg <= NFT_REG_VERDICT)
3097 return -EINVAL;
3098 if (reg > NFT_REG_MAX)
3099 return -ERANGE;
3100 return 0;
3101 }
3102 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3103
3104 /**
3105 * nft_validate_output_register - validate an expressions' output register
3106 *
3107 * @reg: the register number
3108 *
3109 * Validate that the output register is one of the general purpose
3110 * registers or the verdict register.
3111 */
3112 int nft_validate_output_register(enum nft_registers reg)
3113 {
3114 if (reg < NFT_REG_VERDICT)
3115 return -EINVAL;
3116 if (reg > NFT_REG_MAX)
3117 return -ERANGE;
3118 return 0;
3119 }
3120 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3121
3122 /**
3123 * nft_validate_data_load - validate an expressions' data load
3124 *
3125 * @ctx: context of the expression performing the load
3126 * @reg: the destination register number
3127 * @data: the data to load
3128 * @type: the data type
3129 *
3130 * Validate that a data load uses the appropriate data type for
3131 * the destination register. A value of NULL for the data means
3132 * that its runtime gathered data, which is always of type
3133 * NFT_DATA_VALUE.
3134 */
3135 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3136 const struct nft_data *data,
3137 enum nft_data_types type)
3138 {
3139 int err;
3140
3141 switch (reg) {
3142 case NFT_REG_VERDICT:
3143 if (data == NULL || type != NFT_DATA_VERDICT)
3144 return -EINVAL;
3145
3146 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3147 err = nf_tables_check_loops(ctx, data->chain);
3148 if (err < 0)
3149 return err;
3150
3151 if (ctx->chain->level + 1 > data->chain->level) {
3152 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3153 return -EMLINK;
3154 data->chain->level = ctx->chain->level + 1;
3155 }
3156 }
3157
3158 return 0;
3159 default:
3160 if (data != NULL && type != NFT_DATA_VALUE)
3161 return -EINVAL;
3162 return 0;
3163 }
3164 }
3165 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3166
3167 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3168 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3169 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3170 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3171 };
3172
3173 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3174 struct nft_data_desc *desc, const struct nlattr *nla)
3175 {
3176 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3177 struct nft_chain *chain;
3178 int err;
3179
3180 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3181 if (err < 0)
3182 return err;
3183
3184 if (!tb[NFTA_VERDICT_CODE])
3185 return -EINVAL;
3186 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3187
3188 switch (data->verdict) {
3189 default:
3190 switch (data->verdict & NF_VERDICT_MASK) {
3191 case NF_ACCEPT:
3192 case NF_DROP:
3193 case NF_QUEUE:
3194 break;
3195 default:
3196 return -EINVAL;
3197 }
3198 /* fall through */
3199 case NFT_CONTINUE:
3200 case NFT_BREAK:
3201 case NFT_RETURN:
3202 desc->len = sizeof(data->verdict);
3203 break;
3204 case NFT_JUMP:
3205 case NFT_GOTO:
3206 if (!tb[NFTA_VERDICT_CHAIN])
3207 return -EINVAL;
3208 chain = nf_tables_chain_lookup(ctx->table,
3209 tb[NFTA_VERDICT_CHAIN]);
3210 if (IS_ERR(chain))
3211 return PTR_ERR(chain);
3212 if (chain->flags & NFT_BASE_CHAIN)
3213 return -EOPNOTSUPP;
3214
3215 chain->use++;
3216 data->chain = chain;
3217 desc->len = sizeof(data);
3218 break;
3219 }
3220
3221 desc->type = NFT_DATA_VERDICT;
3222 return 0;
3223 }
3224
3225 static void nft_verdict_uninit(const struct nft_data *data)
3226 {
3227 switch (data->verdict) {
3228 case NFT_JUMP:
3229 case NFT_GOTO:
3230 data->chain->use--;
3231 break;
3232 }
3233 }
3234
3235 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3236 {
3237 struct nlattr *nest;
3238
3239 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3240 if (!nest)
3241 goto nla_put_failure;
3242
3243 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3244 goto nla_put_failure;
3245
3246 switch (data->verdict) {
3247 case NFT_JUMP:
3248 case NFT_GOTO:
3249 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3250 goto nla_put_failure;
3251 }
3252 nla_nest_end(skb, nest);
3253 return 0;
3254
3255 nla_put_failure:
3256 return -1;
3257 }
3258
3259 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3260 struct nft_data_desc *desc, const struct nlattr *nla)
3261 {
3262 unsigned int len;
3263
3264 len = nla_len(nla);
3265 if (len == 0)
3266 return -EINVAL;
3267 if (len > sizeof(data->data))
3268 return -EOVERFLOW;
3269
3270 nla_memcpy(data->data, nla, sizeof(data->data));
3271 desc->type = NFT_DATA_VALUE;
3272 desc->len = len;
3273 return 0;
3274 }
3275
3276 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3277 unsigned int len)
3278 {
3279 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3280 }
3281
3282 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3283 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3284 .len = FIELD_SIZEOF(struct nft_data, data) },
3285 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3286 };
3287
3288 /**
3289 * nft_data_init - parse nf_tables data netlink attributes
3290 *
3291 * @ctx: context of the expression using the data
3292 * @data: destination struct nft_data
3293 * @desc: data description
3294 * @nla: netlink attribute containing data
3295 *
3296 * Parse the netlink data attributes and initialize a struct nft_data.
3297 * The type and length of data are returned in the data description.
3298 *
3299 * The caller can indicate that it only wants to accept data of type
3300 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3301 */
3302 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3303 struct nft_data_desc *desc, const struct nlattr *nla)
3304 {
3305 struct nlattr *tb[NFTA_DATA_MAX + 1];
3306 int err;
3307
3308 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3309 if (err < 0)
3310 return err;
3311
3312 if (tb[NFTA_DATA_VALUE])
3313 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3314 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3315 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3316 return -EINVAL;
3317 }
3318 EXPORT_SYMBOL_GPL(nft_data_init);
3319
3320 /**
3321 * nft_data_uninit - release a nft_data item
3322 *
3323 * @data: struct nft_data to release
3324 * @type: type of data
3325 *
3326 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3327 * all others need to be released by calling this function.
3328 */
3329 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3330 {
3331 switch (type) {
3332 case NFT_DATA_VALUE:
3333 return;
3334 case NFT_DATA_VERDICT:
3335 return nft_verdict_uninit(data);
3336 default:
3337 WARN_ON(1);
3338 }
3339 }
3340 EXPORT_SYMBOL_GPL(nft_data_uninit);
3341
3342 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3343 enum nft_data_types type, unsigned int len)
3344 {
3345 struct nlattr *nest;
3346 int err;
3347
3348 nest = nla_nest_start(skb, attr);
3349 if (nest == NULL)
3350 return -1;
3351
3352 switch (type) {
3353 case NFT_DATA_VALUE:
3354 err = nft_value_dump(skb, data, len);
3355 break;
3356 case NFT_DATA_VERDICT:
3357 err = nft_verdict_dump(skb, data);
3358 break;
3359 default:
3360 err = -EINVAL;
3361 WARN_ON(1);
3362 }
3363
3364 nla_nest_end(skb, nest);
3365 return err;
3366 }
3367 EXPORT_SYMBOL_GPL(nft_data_dump);
3368
3369 static int nf_tables_init_net(struct net *net)
3370 {
3371 INIT_LIST_HEAD(&net->nft.af_info);
3372 INIT_LIST_HEAD(&net->nft.commit_list);
3373 return 0;
3374 }
3375
3376 static struct pernet_operations nf_tables_net_ops = {
3377 .init = nf_tables_init_net,
3378 };
3379
3380 static int __init nf_tables_module_init(void)
3381 {
3382 int err;
3383
3384 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3385 GFP_KERNEL);
3386 if (info == NULL) {
3387 err = -ENOMEM;
3388 goto err1;
3389 }
3390
3391 err = nf_tables_core_module_init();
3392 if (err < 0)
3393 goto err2;
3394
3395 err = nfnetlink_subsys_register(&nf_tables_subsys);
3396 if (err < 0)
3397 goto err3;
3398
3399 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3400 return register_pernet_subsys(&nf_tables_net_ops);
3401 err3:
3402 nf_tables_core_module_exit();
3403 err2:
3404 kfree(info);
3405 err1:
3406 return err;
3407 }
3408
3409 static void __exit nf_tables_module_exit(void)
3410 {
3411 unregister_pernet_subsys(&nf_tables_net_ops);
3412 nfnetlink_subsys_unregister(&nf_tables_subsys);
3413 nf_tables_core_module_exit();
3414 kfree(info);
3415 }
3416
3417 module_init(nf_tables_module_init);
3418 module_exit(nf_tables_module_exit);
3419
3420 MODULE_LICENSE("GPL");
3421 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3422 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);