1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
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>
18 #include <net/inet_frag.h>
20 #include <linux/netfilter_ipv6.h>
21 #include <linux/netfilter_bridge.h>
22 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_helper.h>
25 #include <net/netfilter/nf_conntrack_l4proto.h>
26 #include <net/netfilter/nf_conntrack_l3proto.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
30 #include <net/netfilter/nf_conntrack_zones.h>
31 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
33 static DEFINE_MUTEX(defrag6_mutex
);
35 static enum ip6_defrag_users
nf_ct6_defrag_user(unsigned int hooknum
,
38 u16 zone_id
= NF_CT_DEFAULT_ZONE_ID
;
39 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
41 enum ip_conntrack_info ctinfo
;
42 const struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
44 zone_id
= nf_ct_zone_id(nf_ct_zone(ct
), CTINFO2DIR(ctinfo
));
47 if (nf_bridge_in_prerouting(skb
))
48 return IP6_DEFRAG_CONNTRACK_BRIDGE_IN
+ zone_id
;
50 if (hooknum
== NF_INET_PRE_ROUTING
)
51 return IP6_DEFRAG_CONNTRACK_IN
+ zone_id
;
53 return IP6_DEFRAG_CONNTRACK_OUT
+ zone_id
;
56 static unsigned int ipv6_defrag(void *priv
,
58 const struct nf_hook_state
*state
)
62 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
63 /* Previously seen (loopback)? */
64 if (skb
->nfct
&& !nf_ct_is_template((struct nf_conn
*)skb
->nfct
))
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 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 if (net
->nf
.defrag_ipv6
) {
95 nf_unregister_net_hooks(net
, ipv6_defrag_ops
,
96 ARRAY_SIZE(ipv6_defrag_ops
));
97 net
->nf
.defrag_ipv6
= false;
101 static struct pernet_operations defrag6_net_ops
= {
102 .exit
= defrag6_net_exit
,
105 static int __init
nf_defrag_init(void)
109 ret
= nf_ct_frag6_init();
111 pr_err("nf_defrag_ipv6: can't initialize frag6.\n");
114 ret
= register_pernet_subsys(&defrag6_net_ops
);
116 pr_err("nf_defrag_ipv6: can't register pernet ops\n");
122 nf_ct_frag6_cleanup();
127 static void __exit
nf_defrag_fini(void)
129 unregister_pernet_subsys(&defrag6_net_ops
);
130 nf_ct_frag6_cleanup();
133 int nf_defrag_ipv6_enable(struct net
*net
)
139 if (net
->nf
.defrag_ipv6
)
142 mutex_lock(&defrag6_mutex
);
143 if (net
->nf
.defrag_ipv6
)
146 err
= nf_register_net_hooks(net
, ipv6_defrag_ops
,
147 ARRAY_SIZE(ipv6_defrag_ops
));
149 net
->nf
.defrag_ipv6
= true;
152 mutex_unlock(&defrag6_mutex
);
155 EXPORT_SYMBOL_GPL(nf_defrag_ipv6_enable
);
157 module_init(nf_defrag_init
);
158 module_exit(nf_defrag_fini
);
160 MODULE_LICENSE("GPL");