1 /*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012-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/ipv6.h>
15 #include <linux/netfilter_ipv6.h>
16 #include <net/netfilter/nf_tables.h>
17 #include <net/netfilter/nf_tables_ipv6.h>
18
nft_do_chain_ipv6(const struct nf_hook_ops * ops,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))19 static unsigned int nft_do_chain_ipv6(const struct nf_hook_ops *ops,
20 struct sk_buff *skb,
21 const struct net_device *in,
22 const struct net_device *out,
23 int (*okfn)(struct sk_buff *))
24 {
25 struct nft_pktinfo pkt;
26
27 /* malformed packet, drop it */
28 if (nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0)
29 return NF_DROP;
30
31 return nft_do_chain(&pkt, ops);
32 }
33
nft_ipv6_output(const struct nf_hook_ops * ops,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))34 static unsigned int nft_ipv6_output(const struct nf_hook_ops *ops,
35 struct sk_buff *skb,
36 const struct net_device *in,
37 const struct net_device *out,
38 int (*okfn)(struct sk_buff *))
39 {
40 if (unlikely(skb->len < sizeof(struct ipv6hdr))) {
41 if (net_ratelimit())
42 pr_info("nf_tables_ipv6: ignoring short SOCK_RAW "
43 "packet\n");
44 return NF_ACCEPT;
45 }
46
47 return nft_do_chain_ipv6(ops, skb, in, out, okfn);
48 }
49
50 struct nft_af_info nft_af_ipv6 __read_mostly = {
51 .family = NFPROTO_IPV6,
52 .nhooks = NF_INET_NUMHOOKS,
53 .owner = THIS_MODULE,
54 .nops = 1,
55 .hooks = {
56 [NF_INET_LOCAL_IN] = nft_do_chain_ipv6,
57 [NF_INET_LOCAL_OUT] = nft_ipv6_output,
58 [NF_INET_FORWARD] = nft_do_chain_ipv6,
59 [NF_INET_PRE_ROUTING] = nft_do_chain_ipv6,
60 [NF_INET_POST_ROUTING] = nft_do_chain_ipv6,
61 },
62 };
63 EXPORT_SYMBOL_GPL(nft_af_ipv6);
64
nf_tables_ipv6_init_net(struct net * net)65 static int nf_tables_ipv6_init_net(struct net *net)
66 {
67 net->nft.ipv6 = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
68 if (net->nft.ipv6 == NULL)
69 return -ENOMEM;
70
71 memcpy(net->nft.ipv6, &nft_af_ipv6, sizeof(nft_af_ipv6));
72
73 if (nft_register_afinfo(net, net->nft.ipv6) < 0)
74 goto err;
75
76 return 0;
77 err:
78 kfree(net->nft.ipv6);
79 return -ENOMEM;
80 }
81
nf_tables_ipv6_exit_net(struct net * net)82 static void nf_tables_ipv6_exit_net(struct net *net)
83 {
84 nft_unregister_afinfo(net->nft.ipv6);
85 kfree(net->nft.ipv6);
86 }
87
88 static struct pernet_operations nf_tables_ipv6_net_ops = {
89 .init = nf_tables_ipv6_init_net,
90 .exit = nf_tables_ipv6_exit_net,
91 };
92
93 static const struct nf_chain_type filter_ipv6 = {
94 .name = "filter",
95 .type = NFT_CHAIN_T_DEFAULT,
96 .family = NFPROTO_IPV6,
97 .owner = THIS_MODULE,
98 .hook_mask = (1 << NF_INET_LOCAL_IN) |
99 (1 << NF_INET_LOCAL_OUT) |
100 (1 << NF_INET_FORWARD) |
101 (1 << NF_INET_PRE_ROUTING) |
102 (1 << NF_INET_POST_ROUTING),
103 };
104
nf_tables_ipv6_init(void)105 static int __init nf_tables_ipv6_init(void)
106 {
107 int ret;
108
109 nft_register_chain_type(&filter_ipv6);
110 ret = register_pernet_subsys(&nf_tables_ipv6_net_ops);
111 if (ret < 0)
112 nft_unregister_chain_type(&filter_ipv6);
113
114 return ret;
115 }
116
nf_tables_ipv6_exit(void)117 static void __exit nf_tables_ipv6_exit(void)
118 {
119 unregister_pernet_subsys(&nf_tables_ipv6_net_ops);
120 nft_unregister_chain_type(&filter_ipv6);
121 }
122
123 module_init(nf_tables_ipv6_init);
124 module_exit(nf_tables_ipv6_exit);
125
126 MODULE_LICENSE("GPL");
127 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
128 MODULE_ALIAS_NFT_FAMILY(AF_INET6);
129