GNU Linux-libre 4.14.290-gnu1
[releases.git] / net / ipv6 / netfilter / nf_defrag_ipv6_hooks.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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
9 #include <linux/types.h>
10 #include <linux/ipv6.h>
11 #include <linux/in6.h>
12 #include <linux/netfilter.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/icmp.h>
16 #include <linux/sysctl.h>
17 #include <net/ipv6_frag.h>
18
19 #include <linux/netfilter_ipv6.h>
20 #include <linux/netfilter_bridge.h>
21 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_helper.h>
24 #include <net/netfilter/nf_conntrack_l4proto.h>
25 #include <net/netfilter/nf_conntrack_l3proto.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
28 #endif
29 #include <net/netfilter/nf_conntrack_zones.h>
30 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
31
32 static DEFINE_MUTEX(defrag6_mutex);
33
34 static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
35                                                 struct sk_buff *skb)
36 {
37         u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
38 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
39         if (skb_nfct(skb)) {
40                 enum ip_conntrack_info ctinfo;
41                 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
42
43                 zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
44         }
45 #endif
46         if (nf_bridge_in_prerouting(skb))
47                 return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
48
49         if (hooknum == NF_INET_PRE_ROUTING)
50                 return IP6_DEFRAG_CONNTRACK_IN + zone_id;
51         else
52                 return IP6_DEFRAG_CONNTRACK_OUT + zone_id;
53 }
54
55 static unsigned int ipv6_defrag(void *priv,
56                                 struct sk_buff *skb,
57                                 const struct nf_hook_state *state)
58 {
59         int err;
60
61 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
62         /* Previously seen (loopback)?  */
63         if (skb_nfct(skb) && !nf_ct_is_template((struct nf_conn *)skb_nfct(skb)))
64                 return NF_ACCEPT;
65 #endif
66
67         err = nf_ct_frag6_gather(state->net, skb,
68                                  nf_ct6_defrag_user(state->hook, skb));
69         /* queued */
70         if (err == -EINPROGRESS)
71                 return NF_STOLEN;
72
73         return err == 0 ? NF_ACCEPT : NF_DROP;
74 }
75
76 static const struct nf_hook_ops ipv6_defrag_ops[] = {
77         {
78                 .hook           = ipv6_defrag,
79                 .pf             = NFPROTO_IPV6,
80                 .hooknum        = NF_INET_PRE_ROUTING,
81                 .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
82         },
83         {
84                 .hook           = ipv6_defrag,
85                 .pf             = NFPROTO_IPV6,
86                 .hooknum        = NF_INET_LOCAL_OUT,
87                 .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
88         },
89 };
90
91 static void __net_exit defrag6_net_exit(struct net *net)
92 {
93         if (net->nf.defrag_ipv6) {
94                 nf_unregister_net_hooks(net, ipv6_defrag_ops,
95                                         ARRAY_SIZE(ipv6_defrag_ops));
96                 net->nf.defrag_ipv6 = false;
97         }
98 }
99
100 static struct pernet_operations defrag6_net_ops = {
101         .exit = defrag6_net_exit,
102 };
103
104 static int __init nf_defrag_init(void)
105 {
106         int ret = 0;
107
108         ret = nf_ct_frag6_init();
109         if (ret < 0) {
110                 pr_err("nf_defrag_ipv6: can't initialize frag6.\n");
111                 return ret;
112         }
113         ret = register_pernet_subsys(&defrag6_net_ops);
114         if (ret < 0) {
115                 pr_err("nf_defrag_ipv6: can't register pernet ops\n");
116                 goto cleanup_frag6;
117         }
118         return ret;
119
120 cleanup_frag6:
121         nf_ct_frag6_cleanup();
122         return ret;
123
124 }
125
126 static void __exit nf_defrag_fini(void)
127 {
128         unregister_pernet_subsys(&defrag6_net_ops);
129         nf_ct_frag6_cleanup();
130 }
131
132 int nf_defrag_ipv6_enable(struct net *net)
133 {
134         int err = 0;
135
136         might_sleep();
137
138         if (net->nf.defrag_ipv6)
139                 return 0;
140
141         mutex_lock(&defrag6_mutex);
142         if (net->nf.defrag_ipv6)
143                 goto out_unlock;
144
145         err = nf_register_net_hooks(net, ipv6_defrag_ops,
146                                     ARRAY_SIZE(ipv6_defrag_ops));
147         if (err == 0)
148                 net->nf.defrag_ipv6 = true;
149
150  out_unlock:
151         mutex_unlock(&defrag6_mutex);
152         return err;
153 }
154 EXPORT_SYMBOL_GPL(nf_defrag_ipv6_enable);
155
156 module_init(nf_defrag_init);
157 module_exit(nf_defrag_fini);
158
159 MODULE_LICENSE("GPL");