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>
8 #include <linux/netfilter.h>
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <net/netns/generic.h>
12 #include <net/route.h>
15 #include <linux/netfilter_bridge.h>
16 #include <linux/netfilter_ipv4.h>
17 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
18 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
19 #include <net/netfilter/nf_conntrack.h>
21 #include <net/netfilter/nf_conntrack_zones.h>
23 static unsigned int defrag4_pernet_id __read_mostly;
24 static DEFINE_MUTEX(defrag4_mutex);
26 struct defrag4_pernet {
30 static int nf_ct_ipv4_gather_frags(struct net *net, struct sk_buff *skb,
36 err = ip_defrag(net, skb, user);
45 static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum,
48 u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
49 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
51 enum ip_conntrack_info ctinfo;
52 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
54 zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
57 if (nf_bridge_in_prerouting(skb))
58 return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
60 if (hooknum == NF_INET_PRE_ROUTING)
61 return IP_DEFRAG_CONNTRACK_IN + zone_id;
63 return IP_DEFRAG_CONNTRACK_OUT + zone_id;
66 static unsigned int ipv4_conntrack_defrag(void *priv,
68 const struct nf_hook_state *state)
70 struct sock *sk = skb->sk;
72 if (sk && sk_fullsock(sk) && (sk->sk_family == PF_INET) &&
73 inet_sk(sk)->nodefrag)
76 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
77 #if !IS_ENABLED(CONFIG_NF_NAT)
78 /* Previously seen (loopback)? Ignore. Do this before
80 if (skb_nfct(skb) && !nf_ct_is_template((struct nf_conn *)skb_nfct(skb)))
83 if (skb->_nfct == IP_CT_UNTRACKED)
86 /* Gather fragments. */
87 if (ip_is_fragment(ip_hdr(skb))) {
88 enum ip_defrag_users user =
89 nf_ct_defrag_user(state->hook, skb);
91 if (nf_ct_ipv4_gather_frags(state->net, skb, user))
97 static const struct nf_hook_ops ipv4_defrag_ops[] = {
99 .hook = ipv4_conntrack_defrag,
101 .hooknum = NF_INET_PRE_ROUTING,
102 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
105 .hook = ipv4_conntrack_defrag,
107 .hooknum = NF_INET_LOCAL_OUT,
108 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
112 static void __net_exit defrag4_net_exit(struct net *net)
114 struct defrag4_pernet *nf_defrag = net_generic(net, defrag4_pernet_id);
116 if (nf_defrag->users) {
117 nf_unregister_net_hooks(net, ipv4_defrag_ops,
118 ARRAY_SIZE(ipv4_defrag_ops));
119 nf_defrag->users = 0;
123 static struct pernet_operations defrag4_net_ops = {
124 .exit = defrag4_net_exit,
125 .id = &defrag4_pernet_id,
126 .size = sizeof(struct defrag4_pernet),
129 static int __init nf_defrag_init(void)
131 return register_pernet_subsys(&defrag4_net_ops);
134 static void __exit nf_defrag_fini(void)
136 unregister_pernet_subsys(&defrag4_net_ops);
139 int nf_defrag_ipv4_enable(struct net *net)
141 struct defrag4_pernet *nf_defrag = net_generic(net, defrag4_pernet_id);
144 mutex_lock(&defrag4_mutex);
145 if (nf_defrag->users == UINT_MAX) {
150 if (nf_defrag->users) {
155 err = nf_register_net_hooks(net, ipv4_defrag_ops,
156 ARRAY_SIZE(ipv4_defrag_ops));
158 nf_defrag->users = 1;
161 mutex_unlock(&defrag4_mutex);
164 EXPORT_SYMBOL_GPL(nf_defrag_ipv4_enable);
166 void nf_defrag_ipv4_disable(struct net *net)
168 struct defrag4_pernet *nf_defrag = net_generic(net, defrag4_pernet_id);
170 mutex_lock(&defrag4_mutex);
171 if (nf_defrag->users) {
173 if (nf_defrag->users == 0)
174 nf_unregister_net_hooks(net, ipv4_defrag_ops,
175 ARRAY_SIZE(ipv4_defrag_ops));
178 mutex_unlock(&defrag4_mutex);
180 EXPORT_SYMBOL_GPL(nf_defrag_ipv4_disable);
182 module_init(nf_defrag_init);
183 module_exit(nf_defrag_fini);
185 MODULE_LICENSE("GPL");