Merge remote-tracking branch 'spi/for-5.12' into spi-linus
[platform/kernel/linux-rpi.git] / net / ipv4 / netfilter / nf_defrag_ipv4.c
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>
4  */
5
6 #include <linux/types.h>
7 #include <linux/ip.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>
13 #include <net/ip.h>
14
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>
20 #endif
21 #include <net/netfilter/nf_conntrack_zones.h>
22
23 static unsigned int defrag4_pernet_id __read_mostly;
24 static DEFINE_MUTEX(defrag4_mutex);
25
26 struct defrag4_pernet {
27         unsigned int users;
28 };
29
30 static int nf_ct_ipv4_gather_frags(struct net *net, struct sk_buff *skb,
31                                    u_int32_t user)
32 {
33         int err;
34
35         local_bh_disable();
36         err = ip_defrag(net, skb, user);
37         local_bh_enable();
38
39         if (!err)
40                 skb->ignore_df = 1;
41
42         return err;
43 }
44
45 static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum,
46                                               struct sk_buff *skb)
47 {
48         u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
49 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
50         if (skb_nfct(skb)) {
51                 enum ip_conntrack_info ctinfo;
52                 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
53
54                 zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
55         }
56 #endif
57         if (nf_bridge_in_prerouting(skb))
58                 return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
59
60         if (hooknum == NF_INET_PRE_ROUTING)
61                 return IP_DEFRAG_CONNTRACK_IN + zone_id;
62         else
63                 return IP_DEFRAG_CONNTRACK_OUT + zone_id;
64 }
65
66 static unsigned int ipv4_conntrack_defrag(void *priv,
67                                           struct sk_buff *skb,
68                                           const struct nf_hook_state *state)
69 {
70         struct sock *sk = skb->sk;
71
72         if (sk && sk_fullsock(sk) && (sk->sk_family == PF_INET) &&
73             inet_sk(sk)->nodefrag)
74                 return NF_ACCEPT;
75
76 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
77 #if !IS_ENABLED(CONFIG_NF_NAT)
78         /* Previously seen (loopback)?  Ignore.  Do this before
79            fragment check. */
80         if (skb_nfct(skb) && !nf_ct_is_template((struct nf_conn *)skb_nfct(skb)))
81                 return NF_ACCEPT;
82 #endif
83         if (skb->_nfct == IP_CT_UNTRACKED)
84                 return NF_ACCEPT;
85 #endif
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);
90
91                 if (nf_ct_ipv4_gather_frags(state->net, skb, user))
92                         return NF_STOLEN;
93         }
94         return NF_ACCEPT;
95 }
96
97 static const struct nf_hook_ops ipv4_defrag_ops[] = {
98         {
99                 .hook           = ipv4_conntrack_defrag,
100                 .pf             = NFPROTO_IPV4,
101                 .hooknum        = NF_INET_PRE_ROUTING,
102                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
103         },
104         {
105                 .hook           = ipv4_conntrack_defrag,
106                 .pf             = NFPROTO_IPV4,
107                 .hooknum        = NF_INET_LOCAL_OUT,
108                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
109         },
110 };
111
112 static void __net_exit defrag4_net_exit(struct net *net)
113 {
114         struct defrag4_pernet *nf_defrag = net_generic(net, defrag4_pernet_id);
115
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;
120         }
121 }
122
123 static struct pernet_operations defrag4_net_ops = {
124         .exit = defrag4_net_exit,
125         .id   = &defrag4_pernet_id,
126         .size = sizeof(struct defrag4_pernet),
127 };
128
129 static int __init nf_defrag_init(void)
130 {
131         return register_pernet_subsys(&defrag4_net_ops);
132 }
133
134 static void __exit nf_defrag_fini(void)
135 {
136         unregister_pernet_subsys(&defrag4_net_ops);
137 }
138
139 int nf_defrag_ipv4_enable(struct net *net)
140 {
141         struct defrag4_pernet *nf_defrag = net_generic(net, defrag4_pernet_id);
142         int err = 0;
143
144         mutex_lock(&defrag4_mutex);
145         if (nf_defrag->users == UINT_MAX) {
146                 err = -EOVERFLOW;
147                 goto out_unlock;
148         }
149
150         if (nf_defrag->users) {
151                 nf_defrag->users++;
152                 goto out_unlock;
153         }
154
155         err = nf_register_net_hooks(net, ipv4_defrag_ops,
156                                     ARRAY_SIZE(ipv4_defrag_ops));
157         if (err == 0)
158                 nf_defrag->users = 1;
159
160  out_unlock:
161         mutex_unlock(&defrag4_mutex);
162         return err;
163 }
164 EXPORT_SYMBOL_GPL(nf_defrag_ipv4_enable);
165
166 void nf_defrag_ipv4_disable(struct net *net)
167 {
168         struct defrag4_pernet *nf_defrag = net_generic(net, defrag4_pernet_id);
169
170         mutex_lock(&defrag4_mutex);
171         if (nf_defrag->users) {
172                 nf_defrag->users--;
173                 if (nf_defrag->users == 0)
174                         nf_unregister_net_hooks(net, ipv4_defrag_ops,
175                                                 ARRAY_SIZE(ipv4_defrag_ops));
176         }
177
178         mutex_unlock(&defrag4_mutex);
179 }
180 EXPORT_SYMBOL_GPL(nf_defrag_ipv4_disable);
181
182 module_init(nf_defrag_init);
183 module_exit(nf_defrag_fini);
184
185 MODULE_LICENSE("GPL");