netfilter: nft_payload: add optimized payload implementation for small loads
[deliverable/linux.git] / net / netfilter / nf_tables_api.c
CommitLineData
96518518 1/*
20a69341 2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/skbuff.h>
15#include <linux/netlink.h>
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/sock.h>
22
23static LIST_HEAD(nf_tables_afinfo);
24static 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 */
34int nft_register_afinfo(struct nft_af_info *afi)
35{
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
38 list_add_tail(&afi->list, &nf_tables_afinfo);
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41}
42EXPORT_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 */
51void 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}
57EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
59static struct nft_af_info *nft_afinfo_lookup(int family)
60{
61 struct nft_af_info *afi;
62
63 list_for_each_entry(afi, &nf_tables_afinfo, list) {
64 if (afi->family == family)
65 return afi;
66 }
67 return NULL;
68}
69
70static struct nft_af_info *nf_tables_afinfo_lookup(int family, bool autoload)
71{
72 struct nft_af_info *afi;
73
74 afi = nft_afinfo_lookup(family);
75 if (afi != NULL)
76 return afi;
77#ifdef CONFIG_MODULES
78 if (autoload) {
79 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
80 request_module("nft-afinfo-%u", family);
81 nfnl_lock(NFNL_SUBSYS_NFTABLES);
82 afi = nft_afinfo_lookup(family);
83 if (afi != NULL)
84 return ERR_PTR(-EAGAIN);
85 }
86#endif
87 return ERR_PTR(-EAFNOSUPPORT);
88}
89
90/*
91 * Tables
92 */
93
94static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
95 const struct nlattr *nla)
96{
97 struct nft_table *table;
98
99 list_for_each_entry(table, &afi->tables, list) {
100 if (!nla_strcmp(nla, table->name))
101 return table;
102 }
103 return NULL;
104}
105
106static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
107 const struct nlattr *nla,
108 bool autoload)
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#ifdef CONFIG_MODULES
120 if (autoload) {
121 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
122 request_module("nft-table-%u-%*.s", afi->family,
123 nla_len(nla)-1, (const char *)nla_data(nla));
124 nfnl_lock(NFNL_SUBSYS_NFTABLES);
125 if (nft_table_lookup(afi, nla))
126 return ERR_PTR(-EAGAIN);
127 }
128#endif
129 return ERR_PTR(-ENOENT);
130}
131
132static inline u64 nf_tables_alloc_handle(struct nft_table *table)
133{
134 return ++table->hgenerator;
135}
136
137static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
138 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
139};
140
141static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
142 int event, u32 flags, int family,
143 const struct nft_table *table)
144{
145 struct nlmsghdr *nlh;
146 struct nfgenmsg *nfmsg;
147
148 event |= NFNL_SUBSYS_NFTABLES << 8;
149 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
150 if (nlh == NULL)
151 goto nla_put_failure;
152
153 nfmsg = nlmsg_data(nlh);
154 nfmsg->nfgen_family = family;
155 nfmsg->version = NFNETLINK_V0;
156 nfmsg->res_id = 0;
157
158 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name))
159 goto nla_put_failure;
160
161 return nlmsg_end(skb, nlh);
162
163nla_put_failure:
164 nlmsg_trim(skb, nlh);
165 return -1;
166}
167
168static int nf_tables_table_notify(const struct sk_buff *oskb,
169 const struct nlmsghdr *nlh,
170 const struct nft_table *table,
171 int event, int family)
172{
173 struct sk_buff *skb;
174 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
175 u32 seq = nlh ? nlh->nlmsg_seq : 0;
176 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
177 bool report;
178 int err;
179
180 report = nlh ? nlmsg_report(nlh) : false;
181 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
182 return 0;
183
184 err = -ENOBUFS;
185 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
186 if (skb == NULL)
187 goto err;
188
189 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
190 family, table);
191 if (err < 0) {
192 kfree_skb(skb);
193 goto err;
194 }
195
196 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
197 GFP_KERNEL);
198err:
199 if (err < 0)
200 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
201 return err;
202}
203
204static int nf_tables_dump_tables(struct sk_buff *skb,
205 struct netlink_callback *cb)
206{
207 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
208 const struct nft_af_info *afi;
209 const struct nft_table *table;
210 unsigned int idx = 0, s_idx = cb->args[0];
211 int family = nfmsg->nfgen_family;
212
213 list_for_each_entry(afi, &nf_tables_afinfo, list) {
214 if (family != NFPROTO_UNSPEC && family != afi->family)
215 continue;
216
217 list_for_each_entry(table, &afi->tables, list) {
218 if (idx < s_idx)
219 goto cont;
220 if (idx > s_idx)
221 memset(&cb->args[1], 0,
222 sizeof(cb->args) - sizeof(cb->args[0]));
223 if (nf_tables_fill_table_info(skb,
224 NETLINK_CB(cb->skb).portid,
225 cb->nlh->nlmsg_seq,
226 NFT_MSG_NEWTABLE,
227 NLM_F_MULTI,
228 afi->family, table) < 0)
229 goto done;
230cont:
231 idx++;
232 }
233 }
234done:
235 cb->args[0] = idx;
236 return skb->len;
237}
238
239static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
240 const struct nlmsghdr *nlh,
241 const struct nlattr * const nla[])
242{
243 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
244 const struct nft_af_info *afi;
245 const struct nft_table *table;
246 struct sk_buff *skb2;
247 int family = nfmsg->nfgen_family;
248 int err;
249
250 if (nlh->nlmsg_flags & NLM_F_DUMP) {
251 struct netlink_dump_control c = {
252 .dump = nf_tables_dump_tables,
253 };
254 return netlink_dump_start(nlsk, skb, nlh, &c);
255 }
256
257 afi = nf_tables_afinfo_lookup(family, false);
258 if (IS_ERR(afi))
259 return PTR_ERR(afi);
260
261 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], false);
262 if (IS_ERR(table))
263 return PTR_ERR(table);
264
265 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
266 if (!skb2)
267 return -ENOMEM;
268
269 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
270 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
271 family, table);
272 if (err < 0)
273 goto err;
274
275 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
276
277err:
278 kfree_skb(skb2);
279 return err;
280}
281
282static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
283 const struct nlmsghdr *nlh,
284 const struct nlattr * const nla[])
285{
286 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
287 const struct nlattr *name;
288 struct nft_af_info *afi;
289 struct nft_table *table;
290 int family = nfmsg->nfgen_family;
291
292 afi = nf_tables_afinfo_lookup(family, true);
293 if (IS_ERR(afi))
294 return PTR_ERR(afi);
295
296 name = nla[NFTA_TABLE_NAME];
297 table = nf_tables_table_lookup(afi, name, false);
298 if (IS_ERR(table)) {
299 if (PTR_ERR(table) != -ENOENT)
300 return PTR_ERR(table);
301 table = NULL;
302 }
303
304 if (table != NULL) {
305 if (nlh->nlmsg_flags & NLM_F_EXCL)
306 return -EEXIST;
307 if (nlh->nlmsg_flags & NLM_F_REPLACE)
308 return -EOPNOTSUPP;
309 return 0;
310 }
311
312 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
313 if (table == NULL)
314 return -ENOMEM;
315
316 nla_strlcpy(table->name, name, nla_len(name));
317 INIT_LIST_HEAD(&table->chains);
20a69341 318 INIT_LIST_HEAD(&table->sets);
96518518
PM
319
320 list_add_tail(&table->list, &afi->tables);
321 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
322 return 0;
323}
324
325static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
326 const struct nlmsghdr *nlh,
327 const struct nlattr * const nla[])
328{
329 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
330 struct nft_af_info *afi;
331 struct nft_table *table;
332 int family = nfmsg->nfgen_family;
333
334 afi = nf_tables_afinfo_lookup(family, false);
335 if (IS_ERR(afi))
336 return PTR_ERR(afi);
337
338 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], false);
339 if (IS_ERR(table))
340 return PTR_ERR(table);
341
342 if (table->flags & NFT_TABLE_BUILTIN)
343 return -EOPNOTSUPP;
344
345 if (table->use)
346 return -EBUSY;
347
348 list_del(&table->list);
349 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
350 kfree(table);
351 return 0;
352}
353
354static struct nft_table *__nf_tables_table_lookup(const struct nft_af_info *afi,
355 const char *name)
356{
357 struct nft_table *table;
358
359 list_for_each_entry(table, &afi->tables, list) {
360 if (!strcmp(name, table->name))
361 return table;
362 }
363
364 return ERR_PTR(-ENOENT);
365}
366
367static int nf_tables_chain_notify(const struct sk_buff *oskb,
368 const struct nlmsghdr *nlh,
369 const struct nft_table *table,
370 const struct nft_chain *chain,
371 int event, int family);
372
373/**
374 * nft_register_table - register a built-in table
375 *
376 * @table: the table to register
377 * @family: protocol family to register table with
378 *
379 * Register a built-in table for use with nf_tables. Returns zero on
380 * success or a negative errno code otherwise.
381 */
382int nft_register_table(struct nft_table *table, int family)
383{
384 struct nft_af_info *afi;
385 struct nft_table *t;
386 struct nft_chain *chain;
387 int err;
388
389 nfnl_lock(NFNL_SUBSYS_NFTABLES);
390again:
391 afi = nf_tables_afinfo_lookup(family, true);
392 if (IS_ERR(afi)) {
393 err = PTR_ERR(afi);
394 if (err == -EAGAIN)
395 goto again;
396 goto err;
397 }
398
399 t = __nf_tables_table_lookup(afi, table->name);
400 if (IS_ERR(t)) {
401 err = PTR_ERR(t);
402 if (err != -ENOENT)
403 goto err;
404 t = NULL;
405 }
406
407 if (t != NULL) {
408 err = -EEXIST;
409 goto err;
410 }
411
412 table->flags |= NFT_TABLE_BUILTIN;
20a69341 413 INIT_LIST_HEAD(&table->sets);
96518518
PM
414 list_add_tail(&table->list, &afi->tables);
415 nf_tables_table_notify(NULL, NULL, table, NFT_MSG_NEWTABLE, family);
416 list_for_each_entry(chain, &table->chains, list)
417 nf_tables_chain_notify(NULL, NULL, table, chain,
418 NFT_MSG_NEWCHAIN, family);
419 err = 0;
420err:
421 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
422 return err;
423}
424EXPORT_SYMBOL_GPL(nft_register_table);
425
426/**
427 * nft_unregister_table - unregister a built-in table
428 *
429 * @table: the table to unregister
430 * @family: protocol family to unregister table with
431 *
432 * Unregister a built-in table for use with nf_tables.
433 */
434void nft_unregister_table(struct nft_table *table, int family)
435{
436 struct nft_chain *chain;
437
438 nfnl_lock(NFNL_SUBSYS_NFTABLES);
439 list_del(&table->list);
440 list_for_each_entry(chain, &table->chains, list)
441 nf_tables_chain_notify(NULL, NULL, table, chain,
442 NFT_MSG_DELCHAIN, family);
443 nf_tables_table_notify(NULL, NULL, table, NFT_MSG_DELTABLE, family);
444 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
445}
446EXPORT_SYMBOL_GPL(nft_unregister_table);
447
448/*
449 * Chains
450 */
451
452static struct nft_chain *
453nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
454{
455 struct nft_chain *chain;
456
457 list_for_each_entry(chain, &table->chains, list) {
458 if (chain->handle == handle)
459 return chain;
460 }
461
462 return ERR_PTR(-ENOENT);
463}
464
465static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
466 const struct nlattr *nla)
467{
468 struct nft_chain *chain;
469
470 if (nla == NULL)
471 return ERR_PTR(-EINVAL);
472
473 list_for_each_entry(chain, &table->chains, list) {
474 if (!nla_strcmp(nla, chain->name))
475 return chain;
476 }
477
478 return ERR_PTR(-ENOENT);
479}
480
481static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
482 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
483 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
484 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
485 .len = NFT_CHAIN_MAXNAMELEN - 1 },
486 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
487};
488
489static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
490 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
491 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
492};
493
494static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
495 int event, u32 flags, int family,
496 const struct nft_table *table,
497 const struct nft_chain *chain)
498{
499 struct nlmsghdr *nlh;
500 struct nfgenmsg *nfmsg;
501
502 event |= NFNL_SUBSYS_NFTABLES << 8;
503 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
504 if (nlh == NULL)
505 goto nla_put_failure;
506
507 nfmsg = nlmsg_data(nlh);
508 nfmsg->nfgen_family = family;
509 nfmsg->version = NFNETLINK_V0;
510 nfmsg->res_id = 0;
511
512 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
513 goto nla_put_failure;
514 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
515 goto nla_put_failure;
516 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
517 goto nla_put_failure;
518
519 if (chain->flags & NFT_BASE_CHAIN) {
520 const struct nf_hook_ops *ops = &nft_base_chain(chain)->ops;
521 struct nlattr *nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
522 if (nest == NULL)
523 goto nla_put_failure;
524 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
525 goto nla_put_failure;
526 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
527 goto nla_put_failure;
528 nla_nest_end(skb, nest);
529 }
530
531 return nlmsg_end(skb, nlh);
532
533nla_put_failure:
534 nlmsg_trim(skb, nlh);
535 return -1;
536}
537
538static int nf_tables_chain_notify(const struct sk_buff *oskb,
539 const struct nlmsghdr *nlh,
540 const struct nft_table *table,
541 const struct nft_chain *chain,
542 int event, int family)
543{
544 struct sk_buff *skb;
545 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
546 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
547 u32 seq = nlh ? nlh->nlmsg_seq : 0;
548 bool report;
549 int err;
550
551 report = nlh ? nlmsg_report(nlh) : false;
552 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
553 return 0;
554
555 err = -ENOBUFS;
556 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
557 if (skb == NULL)
558 goto err;
559
560 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
561 table, chain);
562 if (err < 0) {
563 kfree_skb(skb);
564 goto err;
565 }
566
567 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
568 GFP_KERNEL);
569err:
570 if (err < 0)
571 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
572 return err;
573}
574
575static int nf_tables_dump_chains(struct sk_buff *skb,
576 struct netlink_callback *cb)
577{
578 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
579 const struct nft_af_info *afi;
580 const struct nft_table *table;
581 const struct nft_chain *chain;
582 unsigned int idx = 0, s_idx = cb->args[0];
583 int family = nfmsg->nfgen_family;
584
585 list_for_each_entry(afi, &nf_tables_afinfo, list) {
586 if (family != NFPROTO_UNSPEC && family != afi->family)
587 continue;
588
589 list_for_each_entry(table, &afi->tables, list) {
590 list_for_each_entry(chain, &table->chains, list) {
591 if (idx < s_idx)
592 goto cont;
593 if (idx > s_idx)
594 memset(&cb->args[1], 0,
595 sizeof(cb->args) - sizeof(cb->args[0]));
596 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
597 cb->nlh->nlmsg_seq,
598 NFT_MSG_NEWCHAIN,
599 NLM_F_MULTI,
600 afi->family, table, chain) < 0)
601 goto done;
602cont:
603 idx++;
604 }
605 }
606 }
607done:
608 cb->args[0] = idx;
609 return skb->len;
610}
611
612
613static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
614 const struct nlmsghdr *nlh,
615 const struct nlattr * const nla[])
616{
617 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
618 const struct nft_af_info *afi;
619 const struct nft_table *table;
620 const struct nft_chain *chain;
621 struct sk_buff *skb2;
622 int family = nfmsg->nfgen_family;
623 int err;
624
625 if (nlh->nlmsg_flags & NLM_F_DUMP) {
626 struct netlink_dump_control c = {
627 .dump = nf_tables_dump_chains,
628 };
629 return netlink_dump_start(nlsk, skb, nlh, &c);
630 }
631
632 afi = nf_tables_afinfo_lookup(family, false);
633 if (IS_ERR(afi))
634 return PTR_ERR(afi);
635
636 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], false);
637 if (IS_ERR(table))
638 return PTR_ERR(table);
639
640 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
641 if (IS_ERR(chain))
642 return PTR_ERR(chain);
643
644 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
645 if (!skb2)
646 return -ENOMEM;
647
648 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
649 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
650 family, table, chain);
651 if (err < 0)
652 goto err;
653
654 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
655
656err:
657 kfree_skb(skb2);
658 return err;
659}
660
661static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
662 const struct nlmsghdr *nlh,
663 const struct nlattr * const nla[])
664{
665 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
666 const struct nlattr * uninitialized_var(name);
667 const struct nft_af_info *afi;
668 struct nft_table *table;
669 struct nft_chain *chain;
670 struct nft_base_chain *basechain;
671 struct nlattr *ha[NFTA_HOOK_MAX + 1];
672 int family = nfmsg->nfgen_family;
673 u64 handle = 0;
674 int err;
675 bool create;
676
677 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
678
679 afi = nf_tables_afinfo_lookup(family, true);
680 if (IS_ERR(afi))
681 return PTR_ERR(afi);
682
683 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], create);
684 if (IS_ERR(table))
685 return PTR_ERR(table);
686
687 if (table->use == UINT_MAX)
688 return -EOVERFLOW;
689
690 chain = NULL;
691 name = nla[NFTA_CHAIN_NAME];
692
693 if (nla[NFTA_CHAIN_HANDLE]) {
694 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
695 chain = nf_tables_chain_lookup_byhandle(table, handle);
696 if (IS_ERR(chain))
697 return PTR_ERR(chain);
698 } else {
699 chain = nf_tables_chain_lookup(table, name);
700 if (IS_ERR(chain)) {
701 if (PTR_ERR(chain) != -ENOENT)
702 return PTR_ERR(chain);
703 chain = NULL;
704 }
705 }
706
707 if (chain != NULL) {
708 if (nlh->nlmsg_flags & NLM_F_EXCL)
709 return -EEXIST;
710 if (nlh->nlmsg_flags & NLM_F_REPLACE)
711 return -EOPNOTSUPP;
712
713 if (nla[NFTA_CHAIN_HANDLE] && name &&
714 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
715 return -EEXIST;
716
717 if (nla[NFTA_CHAIN_HANDLE] && name)
718 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
719
720 goto notify;
721 }
722
723 if (nla[NFTA_CHAIN_HOOK]) {
724 struct nf_hook_ops *ops;
725
726 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
727 nft_hook_policy);
728 if (err < 0)
729 return err;
730 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
731 ha[NFTA_HOOK_PRIORITY] == NULL)
732 return -EINVAL;
733 if (ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])) >= afi->nhooks)
734 return -EINVAL;
735
736 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
737 if (basechain == NULL)
738 return -ENOMEM;
739 chain = &basechain->chain;
740
741 ops = &basechain->ops;
742 ops->pf = family;
743 ops->owner = afi->owner;
744 ops->hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
745 ops->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
746 ops->priv = chain;
747 ops->hook = nft_do_chain;
748 if (afi->hooks[ops->hooknum])
749 ops->hook = afi->hooks[ops->hooknum];
750
751 chain->flags |= NFT_BASE_CHAIN;
752 } else {
753 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
754 if (chain == NULL)
755 return -ENOMEM;
756 }
757
758 INIT_LIST_HEAD(&chain->rules);
759 chain->handle = nf_tables_alloc_handle(table);
760 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
761
762 list_add_tail(&chain->list, &table->chains);
763 table->use++;
764notify:
765 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
766 family);
767 return 0;
768}
769
770static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
771{
772 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
773
774 BUG_ON(chain->use > 0);
775
776 if (chain->flags & NFT_BASE_CHAIN)
777 kfree(nft_base_chain(chain));
778 else
779 kfree(chain);
780}
781
782static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
783 const struct nlmsghdr *nlh,
784 const struct nlattr * const nla[])
785{
786 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
787 const struct nft_af_info *afi;
788 struct nft_table *table;
789 struct nft_chain *chain;
790 int family = nfmsg->nfgen_family;
791
792 afi = nf_tables_afinfo_lookup(family, false);
793 if (IS_ERR(afi))
794 return PTR_ERR(afi);
795
796 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], false);
797 if (IS_ERR(table))
798 return PTR_ERR(table);
799
800 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
801 if (IS_ERR(chain))
802 return PTR_ERR(chain);
803
804 if (chain->flags & NFT_CHAIN_BUILTIN)
805 return -EOPNOTSUPP;
806
807 if (!list_empty(&chain->rules))
808 return -EBUSY;
809
810 list_del(&chain->list);
811 table->use--;
812
813 if (chain->flags & NFT_BASE_CHAIN)
814 nf_unregister_hook(&nft_base_chain(chain)->ops);
815
816 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
817 family);
818
819 /* Make sure all rule references are gone before this is released */
820 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
821 return 0;
822}
823
824static void nft_ctx_init(struct nft_ctx *ctx,
20a69341
PM
825 const struct sk_buff *skb,
826 const struct nlmsghdr *nlh,
96518518
PM
827 const struct nft_af_info *afi,
828 const struct nft_table *table,
829 const struct nft_chain *chain)
830{
20a69341
PM
831 ctx->skb = skb;
832 ctx->nlh = nlh;
96518518
PM
833 ctx->afi = afi;
834 ctx->table = table;
835 ctx->chain = chain;
836}
837
838/*
839 * Expressions
840 */
841
842/**
ef1f7df9
PM
843 * nft_register_expr - register nf_tables expr type
844 * @ops: expr type
96518518 845 *
ef1f7df9 846 * Registers the expr type for use with nf_tables. Returns zero on
96518518
PM
847 * success or a negative errno code otherwise.
848 */
ef1f7df9 849int nft_register_expr(struct nft_expr_type *type)
96518518
PM
850{
851 nfnl_lock(NFNL_SUBSYS_NFTABLES);
ef1f7df9 852 list_add_tail(&type->list, &nf_tables_expressions);
96518518
PM
853 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
854 return 0;
855}
856EXPORT_SYMBOL_GPL(nft_register_expr);
857
858/**
ef1f7df9
PM
859 * nft_unregister_expr - unregister nf_tables expr type
860 * @ops: expr type
96518518 861 *
ef1f7df9 862 * Unregisters the expr typefor use with nf_tables.
96518518 863 */
ef1f7df9 864void nft_unregister_expr(struct nft_expr_type *type)
96518518
PM
865{
866 nfnl_lock(NFNL_SUBSYS_NFTABLES);
ef1f7df9 867 list_del(&type->list);
96518518
PM
868 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
869}
870EXPORT_SYMBOL_GPL(nft_unregister_expr);
871
ef1f7df9 872static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
96518518 873{
ef1f7df9 874 const struct nft_expr_type *type;
96518518 875
ef1f7df9
PM
876 list_for_each_entry(type, &nf_tables_expressions, list) {
877 if (!nla_strcmp(nla, type->name))
878 return type;
96518518
PM
879 }
880 return NULL;
881}
882
ef1f7df9 883static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
96518518 884{
ef1f7df9 885 const struct nft_expr_type *type;
96518518
PM
886
887 if (nla == NULL)
888 return ERR_PTR(-EINVAL);
889
ef1f7df9
PM
890 type = __nft_expr_type_get(nla);
891 if (type != NULL && try_module_get(type->owner))
892 return type;
96518518
PM
893
894#ifdef CONFIG_MODULES
ef1f7df9 895 if (type == NULL) {
96518518
PM
896 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
897 request_module("nft-expr-%.*s",
898 nla_len(nla), (char *)nla_data(nla));
899 nfnl_lock(NFNL_SUBSYS_NFTABLES);
ef1f7df9 900 if (__nft_expr_type_get(nla))
96518518
PM
901 return ERR_PTR(-EAGAIN);
902 }
903#endif
904 return ERR_PTR(-ENOENT);
905}
906
907static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
908 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
909 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
910};
911
912static int nf_tables_fill_expr_info(struct sk_buff *skb,
913 const struct nft_expr *expr)
914{
ef1f7df9 915 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
96518518
PM
916 goto nla_put_failure;
917
918 if (expr->ops->dump) {
919 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
920 if (data == NULL)
921 goto nla_put_failure;
922 if (expr->ops->dump(skb, expr) < 0)
923 goto nla_put_failure;
924 nla_nest_end(skb, data);
925 }
926
927 return skb->len;
928
929nla_put_failure:
930 return -1;
931};
932
933struct nft_expr_info {
934 const struct nft_expr_ops *ops;
ef1f7df9 935 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
96518518
PM
936};
937
938static int nf_tables_expr_parse(const struct nlattr *nla,
939 struct nft_expr_info *info)
940{
ef1f7df9 941 const struct nft_expr_type *type;
96518518 942 const struct nft_expr_ops *ops;
ef1f7df9 943 struct nlattr *tb[NFTA_EXPR_MAX + 1];
96518518
PM
944 int err;
945
ef1f7df9 946 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
96518518
PM
947 if (err < 0)
948 return err;
949
ef1f7df9
PM
950 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
951 if (IS_ERR(type))
952 return PTR_ERR(type);
953
954 if (tb[NFTA_EXPR_DATA]) {
955 err = nla_parse_nested(info->tb, type->maxattr,
956 tb[NFTA_EXPR_DATA], type->policy);
957 if (err < 0)
958 goto err1;
959 } else
960 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
961
962 if (type->select_ops != NULL) {
963 ops = type->select_ops((const struct nlattr * const *)info->tb);
964 if (IS_ERR(ops)) {
965 err = PTR_ERR(ops);
966 goto err1;
967 }
968 } else
969 ops = type->ops;
970
96518518
PM
971 info->ops = ops;
972 return 0;
ef1f7df9
PM
973
974err1:
975 module_put(type->owner);
976 return err;
96518518
PM
977}
978
979static int nf_tables_newexpr(const struct nft_ctx *ctx,
ef1f7df9 980 const struct nft_expr_info *info,
96518518
PM
981 struct nft_expr *expr)
982{
983 const struct nft_expr_ops *ops = info->ops;
984 int err;
985
986 expr->ops = ops;
987 if (ops->init) {
ef1f7df9 988 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
96518518
PM
989 if (err < 0)
990 goto err1;
991 }
992
96518518
PM
993 return 0;
994
995err1:
996 expr->ops = NULL;
997 return err;
998}
999
1000static void nf_tables_expr_destroy(struct nft_expr *expr)
1001{
1002 if (expr->ops->destroy)
1003 expr->ops->destroy(expr);
ef1f7df9 1004 module_put(expr->ops->type->owner);
96518518
PM
1005}
1006
1007/*
1008 * Rules
1009 */
1010
1011static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1012 u64 handle)
1013{
1014 struct nft_rule *rule;
1015
1016 // FIXME: this sucks
1017 list_for_each_entry(rule, &chain->rules, list) {
1018 if (handle == rule->handle)
1019 return rule;
1020 }
1021
1022 return ERR_PTR(-ENOENT);
1023}
1024
1025static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1026 const struct nlattr *nla)
1027{
1028 if (nla == NULL)
1029 return ERR_PTR(-EINVAL);
1030
1031 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1032}
1033
1034static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1035 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1036 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1037 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1038 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1039 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1040};
1041
1042static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1043 int event, u32 flags, int family,
1044 const struct nft_table *table,
1045 const struct nft_chain *chain,
1046 const struct nft_rule *rule)
1047{
1048 struct nlmsghdr *nlh;
1049 struct nfgenmsg *nfmsg;
1050 const struct nft_expr *expr, *next;
1051 struct nlattr *list;
1052
1053 event |= NFNL_SUBSYS_NFTABLES << 8;
1054 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
1055 flags);
1056 if (nlh == NULL)
1057 goto nla_put_failure;
1058
1059 nfmsg = nlmsg_data(nlh);
1060 nfmsg->nfgen_family = family;
1061 nfmsg->version = NFNETLINK_V0;
1062 nfmsg->res_id = 0;
1063
1064 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1065 goto nla_put_failure;
1066 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1067 goto nla_put_failure;
1068 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1069 goto nla_put_failure;
1070
1071 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1072 if (list == NULL)
1073 goto nla_put_failure;
1074 nft_rule_for_each_expr(expr, next, rule) {
1075 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1076 if (elem == NULL)
1077 goto nla_put_failure;
1078 if (nf_tables_fill_expr_info(skb, expr) < 0)
1079 goto nla_put_failure;
1080 nla_nest_end(skb, elem);
1081 }
1082 nla_nest_end(skb, list);
1083
1084 return nlmsg_end(skb, nlh);
1085
1086nla_put_failure:
1087 nlmsg_trim(skb, nlh);
1088 return -1;
1089}
1090
1091static int nf_tables_rule_notify(const struct sk_buff *oskb,
1092 const struct nlmsghdr *nlh,
1093 const struct nft_table *table,
1094 const struct nft_chain *chain,
1095 const struct nft_rule *rule,
1096 int event, u32 flags, int family)
1097{
1098 struct sk_buff *skb;
1099 u32 portid = NETLINK_CB(oskb).portid;
1100 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1101 u32 seq = nlh->nlmsg_seq;
1102 bool report;
1103 int err;
1104
1105 report = nlmsg_report(nlh);
1106 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1107 return 0;
1108
1109 err = -ENOBUFS;
1110 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1111 if (skb == NULL)
1112 goto err;
1113
1114 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1115 family, table, chain, rule);
1116 if (err < 0) {
1117 kfree_skb(skb);
1118 goto err;
1119 }
1120
1121 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1122 GFP_KERNEL);
1123err:
1124 if (err < 0)
1125 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1126 return err;
1127}
1128
1129static int nf_tables_dump_rules(struct sk_buff *skb,
1130 struct netlink_callback *cb)
1131{
1132 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1133 const struct nft_af_info *afi;
1134 const struct nft_table *table;
1135 const struct nft_chain *chain;
1136 const struct nft_rule *rule;
1137 unsigned int idx = 0, s_idx = cb->args[0];
1138 int family = nfmsg->nfgen_family;
1139
1140 list_for_each_entry(afi, &nf_tables_afinfo, list) {
1141 if (family != NFPROTO_UNSPEC && family != afi->family)
1142 continue;
1143
1144 list_for_each_entry(table, &afi->tables, list) {
1145 list_for_each_entry(chain, &table->chains, list) {
1146 list_for_each_entry(rule, &chain->rules, list) {
1147 if (idx < s_idx)
1148 goto cont;
1149 if (idx > s_idx)
1150 memset(&cb->args[1], 0,
1151 sizeof(cb->args) - sizeof(cb->args[0]));
1152 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1153 cb->nlh->nlmsg_seq,
1154 NFT_MSG_NEWRULE,
1155 NLM_F_MULTI | NLM_F_APPEND,
1156 afi->family, table, chain, rule) < 0)
1157 goto done;
1158cont:
1159 idx++;
1160 }
1161 }
1162 }
1163 }
1164done:
1165 cb->args[0] = idx;
1166 return skb->len;
1167}
1168
1169static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1170 const struct nlmsghdr *nlh,
1171 const struct nlattr * const nla[])
1172{
1173 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1174 const struct nft_af_info *afi;
1175 const struct nft_table *table;
1176 const struct nft_chain *chain;
1177 const struct nft_rule *rule;
1178 struct sk_buff *skb2;
1179 int family = nfmsg->nfgen_family;
1180 int err;
1181
1182 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1183 struct netlink_dump_control c = {
1184 .dump = nf_tables_dump_rules,
1185 };
1186 return netlink_dump_start(nlsk, skb, nlh, &c);
1187 }
1188
1189 afi = nf_tables_afinfo_lookup(family, false);
1190 if (IS_ERR(afi))
1191 return PTR_ERR(afi);
1192
1193 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], false);
1194 if (IS_ERR(table))
1195 return PTR_ERR(table);
1196
1197 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1198 if (IS_ERR(chain))
1199 return PTR_ERR(chain);
1200
1201 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1202 if (IS_ERR(rule))
1203 return PTR_ERR(rule);
1204
1205 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1206 if (!skb2)
1207 return -ENOMEM;
1208
1209 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1210 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1211 family, table, chain, rule);
1212 if (err < 0)
1213 goto err;
1214
1215 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1216
1217err:
1218 kfree_skb(skb2);
1219 return err;
1220}
1221
1222static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1223{
1224 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1225 struct nft_expr *expr;
1226
1227 /*
1228 * Careful: some expressions might not be initialized in case this
1229 * is called on error from nf_tables_newrule().
1230 */
1231 expr = nft_expr_first(rule);
1232 while (expr->ops && expr != nft_expr_last(rule)) {
1233 nf_tables_expr_destroy(expr);
1234 expr = nft_expr_next(expr);
1235 }
1236 kfree(rule);
1237}
1238
1239static void nf_tables_rule_destroy(struct nft_rule *rule)
1240{
1241 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1242}
1243
1244#define NFT_RULE_MAXEXPRS 128
1245
1246static struct nft_expr_info *info;
1247
1248static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1249 const struct nlmsghdr *nlh,
1250 const struct nlattr * const nla[])
1251{
1252 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1253 const struct nft_af_info *afi;
1254 struct nft_table *table;
1255 struct nft_chain *chain;
1256 struct nft_rule *rule, *old_rule = NULL;
1257 struct nft_expr *expr;
1258 struct nft_ctx ctx;
1259 struct nlattr *tmp;
1260 unsigned int size, i, n;
1261 int err, rem;
1262 bool create;
1263 u64 handle;
1264
1265 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1266
1267 afi = nf_tables_afinfo_lookup(nfmsg->nfgen_family, create);
1268 if (IS_ERR(afi))
1269 return PTR_ERR(afi);
1270
1271 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], create);
1272 if (IS_ERR(table))
1273 return PTR_ERR(table);
1274
1275 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1276 if (IS_ERR(chain))
1277 return PTR_ERR(chain);
1278
1279 if (nla[NFTA_RULE_HANDLE]) {
1280 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1281 rule = __nf_tables_rule_lookup(chain, handle);
1282 if (IS_ERR(rule))
1283 return PTR_ERR(rule);
1284
1285 if (nlh->nlmsg_flags & NLM_F_EXCL)
1286 return -EEXIST;
1287 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1288 old_rule = rule;
1289 else
1290 return -EOPNOTSUPP;
1291 } else {
1292 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1293 return -EINVAL;
1294 handle = nf_tables_alloc_handle(table);
1295 }
1296
1297 n = 0;
1298 size = 0;
1299 if (nla[NFTA_RULE_EXPRESSIONS]) {
1300 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1301 err = -EINVAL;
1302 if (nla_type(tmp) != NFTA_LIST_ELEM)
1303 goto err1;
1304 if (n == NFT_RULE_MAXEXPRS)
1305 goto err1;
1306 err = nf_tables_expr_parse(tmp, &info[n]);
1307 if (err < 0)
1308 goto err1;
1309 size += info[n].ops->size;
1310 n++;
1311 }
1312 }
1313
1314 err = -ENOMEM;
1315 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1316 if (rule == NULL)
1317 goto err1;
1318
1319 rule->handle = handle;
1320 rule->dlen = size;
1321
20a69341 1322 nft_ctx_init(&ctx, skb, nlh, afi, table, chain);
96518518
PM
1323 expr = nft_expr_first(rule);
1324 for (i = 0; i < n; i++) {
1325 err = nf_tables_newexpr(&ctx, &info[i], expr);
1326 if (err < 0)
1327 goto err2;
ef1f7df9 1328 info[i].ops = NULL;
96518518
PM
1329 expr = nft_expr_next(expr);
1330 }
1331
1332 /* Register hook when first rule is inserted into a base chain */
1333 if (list_empty(&chain->rules) && chain->flags & NFT_BASE_CHAIN) {
1334 err = nf_register_hook(&nft_base_chain(chain)->ops);
1335 if (err < 0)
1336 goto err2;
1337 }
1338
1339 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1340 list_replace_rcu(&old_rule->list, &rule->list);
1341 nf_tables_rule_destroy(old_rule);
1342 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1343 list_add_tail_rcu(&rule->list, &chain->rules);
1344 else
1345 list_add_rcu(&rule->list, &chain->rules);
1346
1347 nf_tables_rule_notify(skb, nlh, table, chain, rule, NFT_MSG_NEWRULE,
1348 nlh->nlmsg_flags & (NLM_F_APPEND | NLM_F_REPLACE),
1349 nfmsg->nfgen_family);
1350 return 0;
1351
1352err2:
1353 nf_tables_rule_destroy(rule);
1354err1:
1355 for (i = 0; i < n; i++) {
1356 if (info[i].ops != NULL)
ef1f7df9 1357 module_put(info[i].ops->type->owner);
96518518
PM
1358 }
1359 return err;
1360}
1361
1362static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1363 const struct nlmsghdr *nlh,
1364 const struct nlattr * const nla[])
1365{
1366 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1367 const struct nft_af_info *afi;
1368 const struct nft_table *table;
1369 struct nft_chain *chain;
1370 struct nft_rule *rule, *tmp;
1371 int family = nfmsg->nfgen_family;
1372
1373 afi = nf_tables_afinfo_lookup(family, false);
1374 if (IS_ERR(afi))
1375 return PTR_ERR(afi);
1376
1377 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], false);
1378 if (IS_ERR(table))
1379 return PTR_ERR(table);
1380
1381 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1382 if (IS_ERR(chain))
1383 return PTR_ERR(chain);
1384
1385 if (nla[NFTA_RULE_HANDLE]) {
1386 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1387 if (IS_ERR(rule))
1388 return PTR_ERR(rule);
1389
1390 /* List removal must be visible before destroying expressions */
1391 list_del_rcu(&rule->list);
1392
1393 nf_tables_rule_notify(skb, nlh, table, chain, rule,
1394 NFT_MSG_DELRULE, 0, family);
1395 nf_tables_rule_destroy(rule);
1396 } else {
1397 /* Remove all rules in this chain */
1398 list_for_each_entry_safe(rule, tmp, &chain->rules, list) {
1399 list_del_rcu(&rule->list);
1400
1401 nf_tables_rule_notify(skb, nlh, table, chain, rule,
1402 NFT_MSG_DELRULE, 0, family);
1403 nf_tables_rule_destroy(rule);
1404 }
1405 }
1406
1407 /* Unregister hook when last rule from base chain is deleted */
1408 if (list_empty(&chain->rules) && chain->flags & NFT_BASE_CHAIN)
1409 nf_unregister_hook(&nft_base_chain(chain)->ops);
1410
1411 return 0;
1412}
1413
20a69341
PM
1414/*
1415 * Sets
1416 */
1417
1418static LIST_HEAD(nf_tables_set_ops);
1419
1420int nft_register_set(struct nft_set_ops *ops)
1421{
1422 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1423 list_add_tail(&ops->list, &nf_tables_set_ops);
1424 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1425 return 0;
1426}
1427EXPORT_SYMBOL_GPL(nft_register_set);
1428
1429void nft_unregister_set(struct nft_set_ops *ops)
1430{
1431 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1432 list_del(&ops->list);
1433 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1434}
1435EXPORT_SYMBOL_GPL(nft_unregister_set);
1436
1437static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1438{
1439 const struct nft_set_ops *ops;
1440 u32 features;
1441
1442#ifdef CONFIG_MODULES
1443 if (list_empty(&nf_tables_set_ops)) {
1444 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1445 request_module("nft-set");
1446 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1447 if (!list_empty(&nf_tables_set_ops))
1448 return ERR_PTR(-EAGAIN);
1449 }
1450#endif
1451 features = 0;
1452 if (nla[NFTA_SET_FLAGS] != NULL) {
1453 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1454 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1455 }
1456
1457 // FIXME: implement selection properly
1458 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1459 if ((ops->features & features) != features)
1460 continue;
1461 if (!try_module_get(ops->owner))
1462 continue;
1463 return ops;
1464 }
1465
1466 return ERR_PTR(-EOPNOTSUPP);
1467}
1468
1469static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1470 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1471 [NFTA_SET_NAME] = { .type = NLA_STRING },
1472 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1473 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1474 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1475 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1476 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1477};
1478
1479static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1480 const struct sk_buff *skb,
1481 const struct nlmsghdr *nlh,
1482 const struct nlattr * const nla[])
1483{
1484 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1485 const struct nft_af_info *afi;
1486 const struct nft_table *table = NULL;
1487
1488 afi = nf_tables_afinfo_lookup(nfmsg->nfgen_family, false);
1489 if (IS_ERR(afi))
1490 return PTR_ERR(afi);
1491
1492 if (nla[NFTA_SET_TABLE] != NULL) {
1493 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], false);
1494 if (IS_ERR(table))
1495 return PTR_ERR(table);
1496 }
1497
1498 nft_ctx_init(ctx, skb, nlh, afi, table, NULL);
1499 return 0;
1500}
1501
1502struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1503 const struct nlattr *nla)
1504{
1505 struct nft_set *set;
1506
1507 if (nla == NULL)
1508 return ERR_PTR(-EINVAL);
1509
1510 list_for_each_entry(set, &table->sets, list) {
1511 if (!nla_strcmp(nla, set->name))
1512 return set;
1513 }
1514 return ERR_PTR(-ENOENT);
1515}
1516
1517static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1518 const char *name)
1519{
1520 const struct nft_set *i;
1521 const char *p;
1522 unsigned long *inuse;
1523 unsigned int n = 0;
1524
1525 p = strnchr(name, IFNAMSIZ, '%');
1526 if (p != NULL) {
1527 if (p[1] != 'd' || strchr(p + 2, '%'))
1528 return -EINVAL;
1529
1530 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1531 if (inuse == NULL)
1532 return -ENOMEM;
1533
1534 list_for_each_entry(i, &ctx->table->sets, list) {
1535 if (!sscanf(i->name, name, &n))
1536 continue;
1537 if (n < 0 || n > BITS_PER_LONG * PAGE_SIZE)
1538 continue;
1539 set_bit(n, inuse);
1540 }
1541
1542 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1543 free_page((unsigned long)inuse);
1544 }
1545
1546 snprintf(set->name, sizeof(set->name), name, n);
1547 list_for_each_entry(i, &ctx->table->sets, list) {
1548 if (!strcmp(set->name, i->name))
1549 return -ENFILE;
1550 }
1551 return 0;
1552}
1553
1554static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
1555 const struct nft_set *set, u16 event, u16 flags)
1556{
1557 struct nfgenmsg *nfmsg;
1558 struct nlmsghdr *nlh;
1559 u32 portid = NETLINK_CB(ctx->skb).portid;
1560 u32 seq = ctx->nlh->nlmsg_seq;
1561
1562 event |= NFNL_SUBSYS_NFTABLES << 8;
1563 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
1564 flags);
1565 if (nlh == NULL)
1566 goto nla_put_failure;
1567
1568 nfmsg = nlmsg_data(nlh);
1569 nfmsg->nfgen_family = ctx->afi->family;
1570 nfmsg->version = NFNETLINK_V0;
1571 nfmsg->res_id = 0;
1572
1573 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
1574 goto nla_put_failure;
1575 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
1576 goto nla_put_failure;
1577 if (set->flags != 0)
1578 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
1579 goto nla_put_failure;
1580
1581 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
1582 goto nla_put_failure;
1583 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
1584 goto nla_put_failure;
1585 if (set->flags & NFT_SET_MAP) {
1586 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
1587 goto nla_put_failure;
1588 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
1589 goto nla_put_failure;
1590 }
1591
1592 return nlmsg_end(skb, nlh);
1593
1594nla_put_failure:
1595 nlmsg_trim(skb, nlh);
1596 return -1;
1597}
1598
1599static int nf_tables_set_notify(const struct nft_ctx *ctx,
1600 const struct nft_set *set,
1601 int event)
1602{
1603 struct sk_buff *skb;
1604 u32 portid = NETLINK_CB(ctx->skb).portid;
1605 struct net *net = sock_net(ctx->skb->sk);
1606 bool report;
1607 int err;
1608
1609 report = nlmsg_report(ctx->nlh);
1610 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1611 return 0;
1612
1613 err = -ENOBUFS;
1614 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1615 if (skb == NULL)
1616 goto err;
1617
1618 err = nf_tables_fill_set(skb, ctx, set, event, 0);
1619 if (err < 0) {
1620 kfree_skb(skb);
1621 goto err;
1622 }
1623
1624 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1625 GFP_KERNEL);
1626err:
1627 if (err < 0)
1628 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1629 return err;
1630}
1631
1632static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
1633 struct netlink_callback *cb)
1634{
1635 const struct nft_set *set;
1636 unsigned int idx = 0, s_idx = cb->args[0];
1637
1638 if (cb->args[1])
1639 return skb->len;
1640
1641 list_for_each_entry(set, &ctx->table->sets, list) {
1642 if (idx < s_idx)
1643 goto cont;
1644 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
1645 NLM_F_MULTI) < 0) {
1646 cb->args[0] = idx;
1647 goto done;
1648 }
1649cont:
1650 idx++;
1651 }
1652 cb->args[1] = 1;
1653done:
1654 return skb->len;
1655}
1656
1657static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
1658 struct netlink_callback *cb)
1659{
1660 const struct nft_set *set;
1661 unsigned int idx = 0, s_idx = cb->args[0];
1662 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
1663
1664 if (cb->args[1])
1665 return skb->len;
1666
1667 list_for_each_entry(table, &ctx->afi->tables, list) {
1668 if (cur_table && cur_table != table)
1669 continue;
1670
1671 ctx->table = table;
1672 list_for_each_entry(set, &ctx->table->sets, list) {
1673 if (idx < s_idx)
1674 goto cont;
1675 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
1676 NLM_F_MULTI) < 0) {
1677 cb->args[0] = idx;
1678 cb->args[2] = (unsigned long) table;
1679 goto done;
1680 }
1681cont:
1682 idx++;
1683 }
1684 }
1685 cb->args[1] = 1;
1686done:
1687 return skb->len;
1688}
1689
1690static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
1691{
1692 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1693 struct nlattr *nla[NFTA_SET_MAX + 1];
1694 struct nft_ctx ctx;
1695 int err, ret;
1696
1697 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
1698 nft_set_policy);
1699 if (err < 0)
1700 return err;
1701
1702 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
1703 if (err < 0)
1704 return err;
1705
1706 if (ctx.table == NULL)
1707 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
1708 else
1709 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
1710
1711 return ret;
1712}
1713
1714static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
1715 const struct nlmsghdr *nlh,
1716 const struct nlattr * const nla[])
1717{
1718 const struct nft_set *set;
1719 struct nft_ctx ctx;
1720 struct sk_buff *skb2;
1721 int err;
1722
1723 /* Verify existance before starting dump */
1724 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
1725 if (err < 0)
1726 return err;
1727
1728 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1729 struct netlink_dump_control c = {
1730 .dump = nf_tables_dump_sets,
1731 };
1732 return netlink_dump_start(nlsk, skb, nlh, &c);
1733 }
1734
1735 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
1736 if (IS_ERR(set))
1737 return PTR_ERR(set);
1738
1739 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1740 if (skb2 == NULL)
1741 return -ENOMEM;
1742
1743 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
1744 if (err < 0)
1745 goto err;
1746
1747 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1748
1749err:
1750 kfree_skb(skb2);
1751 return err;
1752}
1753
1754static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
1755 const struct nlmsghdr *nlh,
1756 const struct nlattr * const nla[])
1757{
1758 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1759 const struct nft_set_ops *ops;
1760 const struct nft_af_info *afi;
1761 struct nft_table *table;
1762 struct nft_set *set;
1763 struct nft_ctx ctx;
1764 char name[IFNAMSIZ];
1765 unsigned int size;
1766 bool create;
1767 u32 ktype, klen, dlen, dtype, flags;
1768 int err;
1769
1770 if (nla[NFTA_SET_TABLE] == NULL ||
1771 nla[NFTA_SET_NAME] == NULL ||
1772 nla[NFTA_SET_KEY_LEN] == NULL)
1773 return -EINVAL;
1774
1775 ktype = NFT_DATA_VALUE;
1776 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
1777 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
1778 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
1779 return -EINVAL;
1780 }
1781
1782 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
1783 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
1784 return -EINVAL;
1785
1786 flags = 0;
1787 if (nla[NFTA_SET_FLAGS] != NULL) {
1788 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1789 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
1790 NFT_SET_INTERVAL | NFT_SET_MAP))
1791 return -EINVAL;
1792 }
1793
1794 dtype = 0;
1795 dlen = 0;
1796 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
1797 if (!(flags & NFT_SET_MAP))
1798 return -EINVAL;
1799
1800 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
1801 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
1802 dtype != NFT_DATA_VERDICT)
1803 return -EINVAL;
1804
1805 if (dtype != NFT_DATA_VERDICT) {
1806 if (nla[NFTA_SET_DATA_LEN] == NULL)
1807 return -EINVAL;
1808 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
1809 if (dlen == 0 ||
1810 dlen > FIELD_SIZEOF(struct nft_data, data))
1811 return -EINVAL;
1812 } else
1813 dlen = sizeof(struct nft_data);
1814 } else if (flags & NFT_SET_MAP)
1815 return -EINVAL;
1816
1817 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1818
1819 afi = nf_tables_afinfo_lookup(nfmsg->nfgen_family, create);
1820 if (IS_ERR(afi))
1821 return PTR_ERR(afi);
1822
1823 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], create);
1824 if (IS_ERR(table))
1825 return PTR_ERR(table);
1826
1827 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL);
1828
1829 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
1830 if (IS_ERR(set)) {
1831 if (PTR_ERR(set) != -ENOENT)
1832 return PTR_ERR(set);
1833 set = NULL;
1834 }
1835
1836 if (set != NULL) {
1837 if (nlh->nlmsg_flags & NLM_F_EXCL)
1838 return -EEXIST;
1839 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1840 return -EOPNOTSUPP;
1841 return 0;
1842 }
1843
1844 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1845 return -ENOENT;
1846
1847 ops = nft_select_set_ops(nla);
1848 if (IS_ERR(ops))
1849 return PTR_ERR(ops);
1850
1851 size = 0;
1852 if (ops->privsize != NULL)
1853 size = ops->privsize(nla);
1854
1855 err = -ENOMEM;
1856 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
1857 if (set == NULL)
1858 goto err1;
1859
1860 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
1861 err = nf_tables_set_alloc_name(&ctx, set, name);
1862 if (err < 0)
1863 goto err2;
1864
1865 INIT_LIST_HEAD(&set->bindings);
1866 set->ops = ops;
1867 set->ktype = ktype;
1868 set->klen = klen;
1869 set->dtype = dtype;
1870 set->dlen = dlen;
1871 set->flags = flags;
1872
1873 err = ops->init(set, nla);
1874 if (err < 0)
1875 goto err2;
1876
1877 list_add_tail(&set->list, &table->sets);
1878 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
1879 return 0;
1880
1881err2:
1882 kfree(set);
1883err1:
1884 module_put(ops->owner);
1885 return err;
1886}
1887
1888static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
1889{
1890 list_del(&set->list);
1891 if (!(set->flags & NFT_SET_ANONYMOUS))
1892 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
1893
1894 set->ops->destroy(set);
1895 module_put(set->ops->owner);
1896 kfree(set);
1897}
1898
1899static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
1900 const struct nlmsghdr *nlh,
1901 const struct nlattr * const nla[])
1902{
1903 struct nft_set *set;
1904 struct nft_ctx ctx;
1905 int err;
1906
1907 if (nla[NFTA_SET_TABLE] == NULL)
1908 return -EINVAL;
1909
1910 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
1911 if (err < 0)
1912 return err;
1913
1914 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
1915 if (IS_ERR(set))
1916 return PTR_ERR(set);
1917 if (!list_empty(&set->bindings))
1918 return -EBUSY;
1919
1920 nf_tables_set_destroy(&ctx, set);
1921 return 0;
1922}
1923
1924static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
1925 const struct nft_set *set,
1926 const struct nft_set_iter *iter,
1927 const struct nft_set_elem *elem)
1928{
1929 enum nft_registers dreg;
1930
1931 dreg = nft_type_to_reg(set->dtype);
1932 return nft_validate_data_load(ctx, dreg, &elem->data, set->dtype);
1933}
1934
1935int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
1936 struct nft_set_binding *binding)
1937{
1938 struct nft_set_binding *i;
1939 struct nft_set_iter iter;
1940
1941 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
1942 return -EBUSY;
1943
1944 if (set->flags & NFT_SET_MAP) {
1945 /* If the set is already bound to the same chain all
1946 * jumps are already validated for that chain.
1947 */
1948 list_for_each_entry(i, &set->bindings, list) {
1949 if (i->chain == binding->chain)
1950 goto bind;
1951 }
1952
1953 iter.skip = 0;
1954 iter.count = 0;
1955 iter.err = 0;
1956 iter.fn = nf_tables_bind_check_setelem;
1957
1958 set->ops->walk(ctx, set, &iter);
1959 if (iter.err < 0) {
1960 /* Destroy anonymous sets if binding fails */
1961 if (set->flags & NFT_SET_ANONYMOUS)
1962 nf_tables_set_destroy(ctx, set);
1963
1964 return iter.err;
1965 }
1966 }
1967bind:
1968 binding->chain = ctx->chain;
1969 list_add_tail(&binding->list, &set->bindings);
1970 return 0;
1971}
1972
1973void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
1974 struct nft_set_binding *binding)
1975{
1976 list_del(&binding->list);
1977
1978 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
1979 nf_tables_set_destroy(ctx, set);
1980}
1981
1982/*
1983 * Set elements
1984 */
1985
1986static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
1987 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
1988 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
1989 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
1990};
1991
1992static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
1993 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
1994 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
1995 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
1996};
1997
1998static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
1999 const struct sk_buff *skb,
2000 const struct nlmsghdr *nlh,
2001 const struct nlattr * const nla[])
2002{
2003 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2004 const struct nft_af_info *afi;
2005 const struct nft_table *table;
2006
2007 afi = nf_tables_afinfo_lookup(nfmsg->nfgen_family, false);
2008 if (IS_ERR(afi))
2009 return PTR_ERR(afi);
2010
2011 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE], false);
2012 if (IS_ERR(table))
2013 return PTR_ERR(table);
2014
2015 nft_ctx_init(ctx, skb, nlh, afi, table, NULL);
2016 return 0;
2017}
2018
2019static int nf_tables_fill_setelem(struct sk_buff *skb,
2020 const struct nft_set *set,
2021 const struct nft_set_elem *elem)
2022{
2023 unsigned char *b = skb_tail_pointer(skb);
2024 struct nlattr *nest;
2025
2026 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2027 if (nest == NULL)
2028 goto nla_put_failure;
2029
2030 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2031 set->klen) < 0)
2032 goto nla_put_failure;
2033
2034 if (set->flags & NFT_SET_MAP &&
2035 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2036 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2037 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2038 set->dlen) < 0)
2039 goto nla_put_failure;
2040
2041 if (elem->flags != 0)
2042 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2043 goto nla_put_failure;
2044
2045 nla_nest_end(skb, nest);
2046 return 0;
2047
2048nla_put_failure:
2049 nlmsg_trim(skb, b);
2050 return -EMSGSIZE;
2051}
2052
2053struct nft_set_dump_args {
2054 const struct netlink_callback *cb;
2055 struct nft_set_iter iter;
2056 struct sk_buff *skb;
2057};
2058
2059static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2060 const struct nft_set *set,
2061 const struct nft_set_iter *iter,
2062 const struct nft_set_elem *elem)
2063{
2064 struct nft_set_dump_args *args;
2065
2066 args = container_of(iter, struct nft_set_dump_args, iter);
2067 return nf_tables_fill_setelem(args->skb, set, elem);
2068}
2069
2070static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2071{
2072 const struct nft_set *set;
2073 struct nft_set_dump_args args;
2074 struct nft_ctx ctx;
2075 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2076 struct nfgenmsg *nfmsg;
2077 struct nlmsghdr *nlh;
2078 struct nlattr *nest;
2079 u32 portid, seq;
2080 int event, err;
2081
2082 nfmsg = nlmsg_data(cb->nlh);
2083 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
2084 nft_set_elem_list_policy);
2085 if (err < 0)
2086 return err;
2087
2088 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2089 if (err < 0)
2090 return err;
2091
2092 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2093 if (IS_ERR(set))
2094 return PTR_ERR(set);
2095
2096 event = NFT_MSG_NEWSETELEM;
2097 event |= NFNL_SUBSYS_NFTABLES << 8;
2098 portid = NETLINK_CB(cb->skb).portid;
2099 seq = cb->nlh->nlmsg_seq;
2100
2101 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2102 NLM_F_MULTI);
2103 if (nlh == NULL)
2104 goto nla_put_failure;
2105
2106 nfmsg = nlmsg_data(nlh);
2107 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2108 nfmsg->version = NFNETLINK_V0;
2109 nfmsg->res_id = 0;
2110
2111 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2112 goto nla_put_failure;
2113 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2114 goto nla_put_failure;
2115
2116 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2117 if (nest == NULL)
2118 goto nla_put_failure;
2119
2120 args.cb = cb;
2121 args.skb = skb;
2122 args.iter.skip = cb->args[0];
2123 args.iter.count = 0;
2124 args.iter.err = 0;
2125 args.iter.fn = nf_tables_dump_setelem;
2126 set->ops->walk(&ctx, set, &args.iter);
2127
2128 nla_nest_end(skb, nest);
2129 nlmsg_end(skb, nlh);
2130
2131 if (args.iter.err && args.iter.err != -EMSGSIZE)
2132 return args.iter.err;
2133 if (args.iter.count == cb->args[0])
2134 return 0;
2135
2136 cb->args[0] = args.iter.count;
2137 return skb->len;
2138
2139nla_put_failure:
2140 return -ENOSPC;
2141}
2142
2143static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2144 const struct nlmsghdr *nlh,
2145 const struct nlattr * const nla[])
2146{
2147 const struct nft_set *set;
2148 struct nft_ctx ctx;
2149 int err;
2150
2151 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2152 if (err < 0)
2153 return err;
2154
2155 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2156 if (IS_ERR(set))
2157 return PTR_ERR(set);
2158
2159 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2160 struct netlink_dump_control c = {
2161 .dump = nf_tables_dump_set,
2162 };
2163 return netlink_dump_start(nlsk, skb, nlh, &c);
2164 }
2165 return -EOPNOTSUPP;
2166}
2167
2168static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2169 const struct nlattr *attr)
2170{
2171 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2172 struct nft_data_desc d1, d2;
2173 struct nft_set_elem elem;
2174 struct nft_set_binding *binding;
2175 enum nft_registers dreg;
2176 int err;
2177
2178 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2179 nft_set_elem_policy);
2180 if (err < 0)
2181 return err;
2182
2183 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2184 return -EINVAL;
2185
2186 elem.flags = 0;
2187 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2188 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2189 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2190 return -EINVAL;
2191 }
2192
2193 if (set->flags & NFT_SET_MAP) {
2194 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2195 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2196 return -EINVAL;
2197 } else {
2198 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2199 return -EINVAL;
2200 }
2201
2202 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2203 if (err < 0)
2204 goto err1;
2205 err = -EINVAL;
2206 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2207 goto err2;
2208
2209 err = -EEXIST;
2210 if (set->ops->get(set, &elem) == 0)
2211 goto err2;
2212
2213 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2214 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2215 if (err < 0)
2216 goto err2;
2217
2218 err = -EINVAL;
2219 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2220 goto err3;
2221
2222 dreg = nft_type_to_reg(set->dtype);
2223 list_for_each_entry(binding, &set->bindings, list) {
2224 struct nft_ctx bind_ctx = {
2225 .afi = ctx->afi,
2226 .table = ctx->table,
2227 .chain = binding->chain,
2228 };
2229
2230 err = nft_validate_data_load(&bind_ctx, dreg,
2231 &elem.data, d2.type);
2232 if (err < 0)
2233 goto err3;
2234 }
2235 }
2236
2237 err = set->ops->insert(set, &elem);
2238 if (err < 0)
2239 goto err3;
2240
2241 return 0;
2242
2243err3:
2244 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2245 nft_data_uninit(&elem.data, d2.type);
2246err2:
2247 nft_data_uninit(&elem.key, d1.type);
2248err1:
2249 return err;
2250}
2251
2252static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2253 const struct nlmsghdr *nlh,
2254 const struct nlattr * const nla[])
2255{
2256 const struct nlattr *attr;
2257 struct nft_set *set;
2258 struct nft_ctx ctx;
2259 int rem, err;
2260
2261 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2262 if (err < 0)
2263 return err;
2264
2265 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2266 if (IS_ERR(set))
2267 return PTR_ERR(set);
2268 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2269 return -EBUSY;
2270
2271 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2272 err = nft_add_set_elem(&ctx, set, attr);
2273 if (err < 0)
2274 return err;
2275 }
2276 return 0;
2277}
2278
2279static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2280 const struct nlattr *attr)
2281{
2282 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2283 struct nft_data_desc desc;
2284 struct nft_set_elem elem;
2285 int err;
2286
2287 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2288 nft_set_elem_policy);
2289 if (err < 0)
2290 goto err1;
2291
2292 err = -EINVAL;
2293 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2294 goto err1;
2295
2296 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2297 if (err < 0)
2298 goto err1;
2299
2300 err = -EINVAL;
2301 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2302 goto err2;
2303
2304 err = set->ops->get(set, &elem);
2305 if (err < 0)
2306 goto err2;
2307
2308 set->ops->remove(set, &elem);
2309
2310 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2311 if (set->flags & NFT_SET_MAP)
2312 nft_data_uninit(&elem.data, set->dtype);
2313
2314err2:
2315 nft_data_uninit(&elem.key, desc.type);
2316err1:
2317 return err;
2318}
2319
2320static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2321 const struct nlmsghdr *nlh,
2322 const struct nlattr * const nla[])
2323{
2324 const struct nlattr *attr;
2325 struct nft_set *set;
2326 struct nft_ctx ctx;
2327 int rem, err;
2328
2329 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2330 if (err < 0)
2331 return err;
2332
2333 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2334 if (IS_ERR(set))
2335 return PTR_ERR(set);
2336 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2337 return -EBUSY;
2338
2339 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2340 err = nft_del_setelem(&ctx, set, attr);
2341 if (err < 0)
2342 return err;
2343 }
2344 return 0;
2345}
2346
96518518
PM
2347static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2348 [NFT_MSG_NEWTABLE] = {
2349 .call = nf_tables_newtable,
2350 .attr_count = NFTA_TABLE_MAX,
2351 .policy = nft_table_policy,
2352 },
2353 [NFT_MSG_GETTABLE] = {
2354 .call = nf_tables_gettable,
2355 .attr_count = NFTA_TABLE_MAX,
2356 .policy = nft_table_policy,
2357 },
2358 [NFT_MSG_DELTABLE] = {
2359 .call = nf_tables_deltable,
2360 .attr_count = NFTA_TABLE_MAX,
2361 .policy = nft_table_policy,
2362 },
2363 [NFT_MSG_NEWCHAIN] = {
2364 .call = nf_tables_newchain,
2365 .attr_count = NFTA_CHAIN_MAX,
2366 .policy = nft_chain_policy,
2367 },
2368 [NFT_MSG_GETCHAIN] = {
2369 .call = nf_tables_getchain,
2370 .attr_count = NFTA_CHAIN_MAX,
2371 .policy = nft_chain_policy,
2372 },
2373 [NFT_MSG_DELCHAIN] = {
2374 .call = nf_tables_delchain,
2375 .attr_count = NFTA_CHAIN_MAX,
2376 .policy = nft_chain_policy,
2377 },
2378 [NFT_MSG_NEWRULE] = {
2379 .call = nf_tables_newrule,
2380 .attr_count = NFTA_RULE_MAX,
2381 .policy = nft_rule_policy,
2382 },
2383 [NFT_MSG_GETRULE] = {
2384 .call = nf_tables_getrule,
2385 .attr_count = NFTA_RULE_MAX,
2386 .policy = nft_rule_policy,
2387 },
2388 [NFT_MSG_DELRULE] = {
2389 .call = nf_tables_delrule,
2390 .attr_count = NFTA_RULE_MAX,
2391 .policy = nft_rule_policy,
2392 },
20a69341
PM
2393 [NFT_MSG_NEWSET] = {
2394 .call = nf_tables_newset,
2395 .attr_count = NFTA_SET_MAX,
2396 .policy = nft_set_policy,
2397 },
2398 [NFT_MSG_GETSET] = {
2399 .call = nf_tables_getset,
2400 .attr_count = NFTA_SET_MAX,
2401 .policy = nft_set_policy,
2402 },
2403 [NFT_MSG_DELSET] = {
2404 .call = nf_tables_delset,
2405 .attr_count = NFTA_SET_MAX,
2406 .policy = nft_set_policy,
2407 },
2408 [NFT_MSG_NEWSETELEM] = {
2409 .call = nf_tables_newsetelem,
2410 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2411 .policy = nft_set_elem_list_policy,
2412 },
2413 [NFT_MSG_GETSETELEM] = {
2414 .call = nf_tables_getsetelem,
2415 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2416 .policy = nft_set_elem_list_policy,
2417 },
2418 [NFT_MSG_DELSETELEM] = {
2419 .call = nf_tables_delsetelem,
2420 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2421 .policy = nft_set_elem_list_policy,
2422 },
96518518
PM
2423};
2424
2425static const struct nfnetlink_subsystem nf_tables_subsys = {
2426 .name = "nf_tables",
2427 .subsys_id = NFNL_SUBSYS_NFTABLES,
2428 .cb_count = NFT_MSG_MAX,
2429 .cb = nf_tables_cb,
2430};
2431
20a69341
PM
2432/*
2433 * Loop detection - walk through the ruleset beginning at the destination chain
2434 * of a new jump until either the source chain is reached (loop) or all
2435 * reachable chains have been traversed.
2436 *
2437 * The loop check is performed whenever a new jump verdict is added to an
2438 * expression or verdict map or a verdict map is bound to a new chain.
2439 */
2440
2441static int nf_tables_check_loops(const struct nft_ctx *ctx,
2442 const struct nft_chain *chain);
2443
2444static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2445 const struct nft_set *set,
2446 const struct nft_set_iter *iter,
2447 const struct nft_set_elem *elem)
2448{
2449 switch (elem->data.verdict) {
2450 case NFT_JUMP:
2451 case NFT_GOTO:
2452 return nf_tables_check_loops(ctx, elem->data.chain);
2453 default:
2454 return 0;
2455 }
2456}
2457
2458static int nf_tables_check_loops(const struct nft_ctx *ctx,
2459 const struct nft_chain *chain)
2460{
2461 const struct nft_rule *rule;
2462 const struct nft_expr *expr, *last;
2463 const struct nft_data *data;
2464 const struct nft_set *set;
2465 struct nft_set_binding *binding;
2466 struct nft_set_iter iter;
2467 int err;
2468
2469 if (ctx->chain == chain)
2470 return -ELOOP;
2471
2472 list_for_each_entry(rule, &chain->rules, list) {
2473 nft_rule_for_each_expr(expr, last, rule) {
2474 if (!expr->ops->get_verdict)
2475 continue;
2476
2477 data = expr->ops->get_verdict(expr);
2478 if (data == NULL)
2479 break;
2480
2481 switch (data->verdict) {
2482 case NFT_JUMP:
2483 case NFT_GOTO:
2484 err = nf_tables_check_loops(ctx, data->chain);
2485 if (err < 0)
2486 return err;
2487 default:
2488 break;
2489 }
2490 }
2491 }
2492
2493 list_for_each_entry(set, &ctx->table->sets, list) {
2494 if (!(set->flags & NFT_SET_MAP) ||
2495 set->dtype != NFT_DATA_VERDICT)
2496 continue;
2497
2498 list_for_each_entry(binding, &set->bindings, list) {
2499 if (binding->chain != chain)
2500 continue;
2501
2502 iter.skip = 0;
2503 iter.count = 0;
2504 iter.err = 0;
2505 iter.fn = nf_tables_loop_check_setelem;
2506
2507 set->ops->walk(ctx, set, &iter);
2508 if (iter.err < 0)
2509 return iter.err;
2510 }
2511 }
2512
2513 return 0;
2514}
2515
96518518
PM
2516/**
2517 * nft_validate_input_register - validate an expressions' input register
2518 *
2519 * @reg: the register number
2520 *
2521 * Validate that the input register is one of the general purpose
2522 * registers.
2523 */
2524int nft_validate_input_register(enum nft_registers reg)
2525{
2526 if (reg <= NFT_REG_VERDICT)
2527 return -EINVAL;
2528 if (reg > NFT_REG_MAX)
2529 return -ERANGE;
2530 return 0;
2531}
2532EXPORT_SYMBOL_GPL(nft_validate_input_register);
2533
2534/**
2535 * nft_validate_output_register - validate an expressions' output register
2536 *
2537 * @reg: the register number
2538 *
2539 * Validate that the output register is one of the general purpose
2540 * registers or the verdict register.
2541 */
2542int nft_validate_output_register(enum nft_registers reg)
2543{
2544 if (reg < NFT_REG_VERDICT)
2545 return -EINVAL;
2546 if (reg > NFT_REG_MAX)
2547 return -ERANGE;
2548 return 0;
2549}
2550EXPORT_SYMBOL_GPL(nft_validate_output_register);
2551
2552/**
2553 * nft_validate_data_load - validate an expressions' data load
2554 *
2555 * @ctx: context of the expression performing the load
2556 * @reg: the destination register number
2557 * @data: the data to load
2558 * @type: the data type
2559 *
2560 * Validate that a data load uses the appropriate data type for
2561 * the destination register. A value of NULL for the data means
2562 * that its runtime gathered data, which is always of type
2563 * NFT_DATA_VALUE.
2564 */
2565int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
2566 const struct nft_data *data,
2567 enum nft_data_types type)
2568{
20a69341
PM
2569 int err;
2570
96518518
PM
2571 switch (reg) {
2572 case NFT_REG_VERDICT:
2573 if (data == NULL || type != NFT_DATA_VERDICT)
2574 return -EINVAL;
20a69341
PM
2575
2576 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
2577 err = nf_tables_check_loops(ctx, data->chain);
2578 if (err < 0)
2579 return err;
2580
2581 if (ctx->chain->level + 1 > data->chain->level) {
2582 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
2583 return -EMLINK;
2584 data->chain->level = ctx->chain->level + 1;
2585 }
2586 }
2587
96518518
PM
2588 return 0;
2589 default:
2590 if (data != NULL && type != NFT_DATA_VALUE)
2591 return -EINVAL;
2592 return 0;
2593 }
2594}
2595EXPORT_SYMBOL_GPL(nft_validate_data_load);
2596
2597static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
2598 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
2599 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
2600 .len = NFT_CHAIN_MAXNAMELEN - 1 },
2601};
2602
2603static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
2604 struct nft_data_desc *desc, const struct nlattr *nla)
2605{
2606 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
2607 struct nft_chain *chain;
2608 int err;
2609
2610 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
2611 if (err < 0)
2612 return err;
2613
2614 if (!tb[NFTA_VERDICT_CODE])
2615 return -EINVAL;
2616 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
2617
2618 switch (data->verdict) {
2619 case NF_ACCEPT:
2620 case NF_DROP:
2621 case NF_QUEUE:
2622 case NFT_CONTINUE:
2623 case NFT_BREAK:
2624 case NFT_RETURN:
2625 desc->len = sizeof(data->verdict);
2626 break;
2627 case NFT_JUMP:
2628 case NFT_GOTO:
2629 if (!tb[NFTA_VERDICT_CHAIN])
2630 return -EINVAL;
2631 chain = nf_tables_chain_lookup(ctx->table,
2632 tb[NFTA_VERDICT_CHAIN]);
2633 if (IS_ERR(chain))
2634 return PTR_ERR(chain);
2635 if (chain->flags & NFT_BASE_CHAIN)
2636 return -EOPNOTSUPP;
2637
96518518
PM
2638 chain->use++;
2639 data->chain = chain;
2640 desc->len = sizeof(data);
2641 break;
2642 default:
2643 return -EINVAL;
2644 }
2645
2646 desc->type = NFT_DATA_VERDICT;
2647 return 0;
2648}
2649
2650static void nft_verdict_uninit(const struct nft_data *data)
2651{
2652 switch (data->verdict) {
2653 case NFT_JUMP:
2654 case NFT_GOTO:
2655 data->chain->use--;
2656 break;
2657 }
2658}
2659
2660static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
2661{
2662 struct nlattr *nest;
2663
2664 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
2665 if (!nest)
2666 goto nla_put_failure;
2667
2668 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
2669 goto nla_put_failure;
2670
2671 switch (data->verdict) {
2672 case NFT_JUMP:
2673 case NFT_GOTO:
2674 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
2675 goto nla_put_failure;
2676 }
2677 nla_nest_end(skb, nest);
2678 return 0;
2679
2680nla_put_failure:
2681 return -1;
2682}
2683
2684static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
2685 struct nft_data_desc *desc, const struct nlattr *nla)
2686{
2687 unsigned int len;
2688
2689 len = nla_len(nla);
2690 if (len == 0)
2691 return -EINVAL;
2692 if (len > sizeof(data->data))
2693 return -EOVERFLOW;
2694
2695 nla_memcpy(data->data, nla, sizeof(data->data));
2696 desc->type = NFT_DATA_VALUE;
2697 desc->len = len;
2698 return 0;
2699}
2700
2701static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
2702 unsigned int len)
2703{
2704 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
2705}
2706
2707static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
2708 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
2709 .len = FIELD_SIZEOF(struct nft_data, data) },
2710 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
2711};
2712
2713/**
2714 * nft_data_init - parse nf_tables data netlink attributes
2715 *
2716 * @ctx: context of the expression using the data
2717 * @data: destination struct nft_data
2718 * @desc: data description
2719 * @nla: netlink attribute containing data
2720 *
2721 * Parse the netlink data attributes and initialize a struct nft_data.
2722 * The type and length of data are returned in the data description.
2723 *
2724 * The caller can indicate that it only wants to accept data of type
2725 * NFT_DATA_VALUE by passing NULL for the ctx argument.
2726 */
2727int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
2728 struct nft_data_desc *desc, const struct nlattr *nla)
2729{
2730 struct nlattr *tb[NFTA_DATA_MAX + 1];
2731 int err;
2732
2733 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
2734 if (err < 0)
2735 return err;
2736
2737 if (tb[NFTA_DATA_VALUE])
2738 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
2739 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
2740 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
2741 return -EINVAL;
2742}
2743EXPORT_SYMBOL_GPL(nft_data_init);
2744
2745/**
2746 * nft_data_uninit - release a nft_data item
2747 *
2748 * @data: struct nft_data to release
2749 * @type: type of data
2750 *
2751 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
2752 * all others need to be released by calling this function.
2753 */
2754void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
2755{
2756 switch (type) {
2757 case NFT_DATA_VALUE:
2758 return;
2759 case NFT_DATA_VERDICT:
2760 return nft_verdict_uninit(data);
2761 default:
2762 WARN_ON(1);
2763 }
2764}
2765EXPORT_SYMBOL_GPL(nft_data_uninit);
2766
2767int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
2768 enum nft_data_types type, unsigned int len)
2769{
2770 struct nlattr *nest;
2771 int err;
2772
2773 nest = nla_nest_start(skb, attr);
2774 if (nest == NULL)
2775 return -1;
2776
2777 switch (type) {
2778 case NFT_DATA_VALUE:
2779 err = nft_value_dump(skb, data, len);
2780 break;
2781 case NFT_DATA_VERDICT:
2782 err = nft_verdict_dump(skb, data);
2783 break;
2784 default:
2785 err = -EINVAL;
2786 WARN_ON(1);
2787 }
2788
2789 nla_nest_end(skb, nest);
2790 return err;
2791}
2792EXPORT_SYMBOL_GPL(nft_data_dump);
2793
2794static int __init nf_tables_module_init(void)
2795{
2796 int err;
2797
2798 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
2799 GFP_KERNEL);
2800 if (info == NULL) {
2801 err = -ENOMEM;
2802 goto err1;
2803 }
2804
2805 err = nf_tables_core_module_init();
2806 if (err < 0)
2807 goto err2;
2808
2809 err = nfnetlink_subsys_register(&nf_tables_subsys);
2810 if (err < 0)
2811 goto err3;
2812
2813 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
2814 return 0;
2815err3:
2816 nf_tables_core_module_exit();
2817err2:
2818 kfree(info);
2819err1:
2820 return err;
2821}
2822
2823static void __exit nf_tables_module_exit(void)
2824{
2825 nfnetlink_subsys_unregister(&nf_tables_subsys);
2826 nf_tables_core_module_exit();
2827 kfree(info);
2828}
2829
2830module_init(nf_tables_module_init);
2831module_exit(nf_tables_module_exit);
2832
2833MODULE_LICENSE("GPL");
2834MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
2835MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
This page took 0.129297 seconds and 5 git commands to generate.