1 // SPDX-License-Identifier: GPL-2.0-only
2 /* (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
6 #include <linux/types.h>
7 #include <linux/ipv6.h>
9 #include <linux/netfilter.h>
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/icmp.h>
13 #include <linux/sysctl.h>
14 #include <net/ipv6_frag.h>
16 #include <linux/netfilter_ipv6.h>
17 #include <linux/netfilter_bridge.h>
18 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_helper.h>
21 #include <net/netfilter/nf_conntrack_l4proto.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
25 #include <net/netfilter/nf_conntrack_zones.h>
26 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
28 extern unsigned int nf_frag_pernet_id;
30 static DEFINE_MUTEX(defrag6_mutex);
32 static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
35 u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
36 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
38 enum ip_conntrack_info ctinfo;
39 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
41 zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
44 if (nf_bridge_in_prerouting(skb))
45 return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
47 if (hooknum == NF_INET_PRE_ROUTING)
48 return IP6_DEFRAG_CONNTRACK_IN + zone_id;
50 return IP6_DEFRAG_CONNTRACK_OUT + zone_id;
53 static unsigned int ipv6_defrag(void *priv,
55 const struct nf_hook_state *state)
59 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
60 /* Previously seen (loopback)? */
61 if (skb_nfct(skb) && !nf_ct_is_template((struct nf_conn *)skb_nfct(skb)))
64 if (skb->_nfct == IP_CT_UNTRACKED)
68 err = nf_ct_frag6_gather(state->net, skb,
69 nf_ct6_defrag_user(state->hook, skb));
71 if (err == -EINPROGRESS)
74 return err == 0 ? NF_ACCEPT : NF_DROP;
77 static const struct nf_hook_ops ipv6_defrag_ops[] = {
81 .hooknum = NF_INET_PRE_ROUTING,
82 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
87 .hooknum = NF_INET_LOCAL_OUT,
88 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
92 static void __net_exit defrag6_net_exit(struct net *net)
94 struct nft_ct_frag6_pernet *nf_frag = net_generic(net, nf_frag_pernet_id);
97 nf_unregister_net_hooks(net, ipv6_defrag_ops,
98 ARRAY_SIZE(ipv6_defrag_ops));
103 static struct pernet_operations defrag6_net_ops = {
104 .exit = defrag6_net_exit,
107 static int __init nf_defrag_init(void)
111 ret = nf_ct_frag6_init();
113 pr_err("nf_defrag_ipv6: can't initialize frag6.\n");
116 ret = register_pernet_subsys(&defrag6_net_ops);
118 pr_err("nf_defrag_ipv6: can't register pernet ops\n");
124 nf_ct_frag6_cleanup();
129 static void __exit nf_defrag_fini(void)
131 unregister_pernet_subsys(&defrag6_net_ops);
132 nf_ct_frag6_cleanup();
135 int nf_defrag_ipv6_enable(struct net *net)
137 struct nft_ct_frag6_pernet *nf_frag = net_generic(net, nf_frag_pernet_id);
140 mutex_lock(&defrag6_mutex);
141 if (nf_frag->users == UINT_MAX) {
146 if (nf_frag->users) {
151 err = nf_register_net_hooks(net, ipv6_defrag_ops,
152 ARRAY_SIZE(ipv6_defrag_ops));
157 mutex_unlock(&defrag6_mutex);
160 EXPORT_SYMBOL_GPL(nf_defrag_ipv6_enable);
162 void nf_defrag_ipv6_disable(struct net *net)
164 struct nft_ct_frag6_pernet *nf_frag = net_generic(net, nf_frag_pernet_id);
166 mutex_lock(&defrag6_mutex);
167 if (nf_frag->users) {
169 if (nf_frag->users == 0)
170 nf_unregister_net_hooks(net, ipv6_defrag_ops,
171 ARRAY_SIZE(ipv6_defrag_ops));
173 mutex_unlock(&defrag6_mutex);
175 EXPORT_SYMBOL_GPL(nf_defrag_ipv6_disable);
177 module_init(nf_defrag_init);
178 module_exit(nf_defrag_fini);
180 MODULE_LICENSE("GPL");