netfilter: nf_tables: minor nf_chain_type cleanups
[cascardo/linux.git] / net / bridge / netfilter / nf_tables_bridge.c
1 /*
2  * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3  * Copyright (c) 2013 Pablo Neira Ayuso <pablo@netfilter.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Development of this code funded by Astaro AG (http://www.astaro.com/)
10  */
11
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/netfilter_bridge.h>
15 #include <net/netfilter/nf_tables.h>
16
17 static unsigned int
18 nft_do_chain_bridge(const struct nf_hook_ops *ops,
19                     struct sk_buff *skb,
20                     const struct net_device *in,
21                     const struct net_device *out,
22                     int (*okfn)(struct sk_buff *))
23 {
24         struct nft_pktinfo pkt;
25
26         nft_set_pktinfo(&pkt, ops, skb, in, out);
27
28         return nft_do_chain_pktinfo(&pkt, ops);
29 }
30
31 static struct nft_af_info nft_af_bridge __read_mostly = {
32         .family         = NFPROTO_BRIDGE,
33         .nhooks         = NF_BR_NUMHOOKS,
34         .owner          = THIS_MODULE,
35         .nops           = 1,
36         .hooks          = {
37                 [NF_BR_LOCAL_IN]        = nft_do_chain_bridge,
38                 [NF_BR_FORWARD]         = nft_do_chain_bridge,
39                 [NF_BR_LOCAL_OUT]       = nft_do_chain_bridge,
40         },
41 };
42
43 static int nf_tables_bridge_init_net(struct net *net)
44 {
45         net->nft.bridge = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
46         if (net->nft.bridge == NULL)
47                 return -ENOMEM;
48
49         memcpy(net->nft.bridge, &nft_af_bridge, sizeof(nft_af_bridge));
50
51         if (nft_register_afinfo(net, net->nft.bridge) < 0)
52                 goto err;
53
54         return 0;
55 err:
56         kfree(net->nft.bridge);
57         return -ENOMEM;
58 }
59
60 static void nf_tables_bridge_exit_net(struct net *net)
61 {
62         nft_unregister_afinfo(net->nft.bridge);
63         kfree(net->nft.bridge);
64 }
65
66 static struct pernet_operations nf_tables_bridge_net_ops = {
67         .init   = nf_tables_bridge_init_net,
68         .exit   = nf_tables_bridge_exit_net,
69 };
70
71 static const struct nf_chain_type filter_bridge = {
72         .name           = "filter",
73         .type           = NFT_CHAIN_T_DEFAULT,
74         .family         = NFPROTO_BRIDGE,
75         .owner          = THIS_MODULE,
76         .hook_mask      = (1 << NF_BR_LOCAL_IN) |
77                           (1 << NF_BR_FORWARD) |
78                           (1 << NF_BR_LOCAL_OUT),
79 };
80
81 static int __init nf_tables_bridge_init(void)
82 {
83         int ret;
84
85         nft_register_chain_type(&filter_bridge);
86         ret = register_pernet_subsys(&nf_tables_bridge_net_ops);
87         if (ret < 0)
88                 nft_unregister_chain_type(&filter_bridge);
89
90         return ret;
91 }
92
93 static void __exit nf_tables_bridge_exit(void)
94 {
95         unregister_pernet_subsys(&nf_tables_bridge_net_ops);
96         nft_unregister_chain_type(&filter_bridge);
97 }
98
99 module_init(nf_tables_bridge_init);
100 module_exit(nf_tables_bridge_exit);
101
102 MODULE_LICENSE("GPL");
103 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
104 MODULE_ALIAS_NFT_FAMILY(AF_BRIDGE);