Merge tag 'xfs-4.20-fixes-1' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[sfrench/cifs-2.6.git] / net / netfilter / nf_conntrack_proto_udp.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/types.h>
11 #include <linux/timer.h>
12 #include <linux/module.h>
13 #include <linux/udp.h>
14 #include <linux/seq_file.h>
15 #include <linux/skbuff.h>
16 #include <linux/ipv6.h>
17 #include <net/ip6_checksum.h>
18 #include <net/checksum.h>
19
20 #include <linux/netfilter.h>
21 #include <linux/netfilter_ipv4.h>
22 #include <linux/netfilter_ipv6.h>
23 #include <net/netfilter/nf_conntrack_l4proto.h>
24 #include <net/netfilter/nf_conntrack_ecache.h>
25 #include <net/netfilter/nf_conntrack_timeout.h>
26 #include <net/netfilter/nf_log.h>
27 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
28 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
29
30 static const unsigned int udp_timeouts[UDP_CT_MAX] = {
31         [UDP_CT_UNREPLIED]      = 30*HZ,
32         [UDP_CT_REPLIED]        = 180*HZ,
33 };
34
35 static unsigned int *udp_get_timeouts(struct net *net)
36 {
37         return nf_udp_pernet(net)->timeouts;
38 }
39
40 static void udp_error_log(const struct sk_buff *skb,
41                           const struct nf_hook_state *state,
42                           const char *msg)
43 {
44         nf_l4proto_log_invalid(skb, state->net, state->pf,
45                                IPPROTO_UDP, "%s", msg);
46 }
47
48 static bool udp_error(struct sk_buff *skb,
49                       unsigned int dataoff,
50                       const struct nf_hook_state *state)
51 {
52         unsigned int udplen = skb->len - dataoff;
53         const struct udphdr *hdr;
54         struct udphdr _hdr;
55
56         /* Header is too small? */
57         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
58         if (!hdr) {
59                 udp_error_log(skb, state, "short packet");
60                 return true;
61         }
62
63         /* Truncated/malformed packets */
64         if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
65                 udp_error_log(skb, state, "truncated/malformed packet");
66                 return true;
67         }
68
69         /* Packet with no checksum */
70         if (!hdr->check)
71                 return false;
72
73         /* Checksum invalid? Ignore.
74          * We skip checking packets on the outgoing path
75          * because the checksum is assumed to be correct.
76          * FIXME: Source route IP option packets --RR */
77         if (state->hook == NF_INET_PRE_ROUTING &&
78             state->net->ct.sysctl_checksum &&
79             nf_checksum(skb, state->hook, dataoff, IPPROTO_UDP, state->pf)) {
80                 udp_error_log(skb, state, "bad checksum");
81                 return true;
82         }
83
84         return false;
85 }
86
87 /* Returns verdict for packet, and may modify conntracktype */
88 static int udp_packet(struct nf_conn *ct,
89                       struct sk_buff *skb,
90                       unsigned int dataoff,
91                       enum ip_conntrack_info ctinfo,
92                       const struct nf_hook_state *state)
93 {
94         unsigned int *timeouts;
95
96         if (udp_error(skb, dataoff, state))
97                 return -NF_ACCEPT;
98
99         timeouts = nf_ct_timeout_lookup(ct);
100         if (!timeouts)
101                 timeouts = udp_get_timeouts(nf_ct_net(ct));
102
103         /* If we've seen traffic both ways, this is some kind of UDP
104            stream.  Extend timeout. */
105         if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
106                 nf_ct_refresh_acct(ct, ctinfo, skb,
107                                    timeouts[UDP_CT_REPLIED]);
108                 /* Also, more likely to be important, and not a probe */
109                 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
110                         nf_conntrack_event_cache(IPCT_ASSURED, ct);
111         } else {
112                 nf_ct_refresh_acct(ct, ctinfo, skb,
113                                    timeouts[UDP_CT_UNREPLIED]);
114         }
115         return NF_ACCEPT;
116 }
117
118 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
119 static void udplite_error_log(const struct sk_buff *skb,
120                               const struct nf_hook_state *state,
121                               const char *msg)
122 {
123         nf_l4proto_log_invalid(skb, state->net, state->pf,
124                                IPPROTO_UDPLITE, "%s", msg);
125 }
126
127 static bool udplite_error(struct sk_buff *skb,
128                           unsigned int dataoff,
129                           const struct nf_hook_state *state)
130 {
131         unsigned int udplen = skb->len - dataoff;
132         const struct udphdr *hdr;
133         struct udphdr _hdr;
134         unsigned int cscov;
135
136         /* Header is too small? */
137         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
138         if (!hdr) {
139                 udplite_error_log(skb, state, "short packet");
140                 return true;
141         }
142
143         cscov = ntohs(hdr->len);
144         if (cscov == 0) {
145                 cscov = udplen;
146         } else if (cscov < sizeof(*hdr) || cscov > udplen) {
147                 udplite_error_log(skb, state, "invalid checksum coverage");
148                 return true;
149         }
150
151         /* UDPLITE mandates checksums */
152         if (!hdr->check) {
153                 udplite_error_log(skb, state, "checksum missing");
154                 return true;
155         }
156
157         /* Checksum invalid? Ignore. */
158         if (state->hook == NF_INET_PRE_ROUTING &&
159             state->net->ct.sysctl_checksum &&
160             nf_checksum_partial(skb, state->hook, dataoff, cscov, IPPROTO_UDP,
161                                 state->pf)) {
162                 udplite_error_log(skb, state, "bad checksum");
163                 return true;
164         }
165
166         return false;
167 }
168
169 /* Returns verdict for packet, and may modify conntracktype */
170 static int udplite_packet(struct nf_conn *ct,
171                           struct sk_buff *skb,
172                           unsigned int dataoff,
173                           enum ip_conntrack_info ctinfo,
174                           const struct nf_hook_state *state)
175 {
176         unsigned int *timeouts;
177
178         if (udplite_error(skb, dataoff, state))
179                 return -NF_ACCEPT;
180
181         timeouts = nf_ct_timeout_lookup(ct);
182         if (!timeouts)
183                 timeouts = udp_get_timeouts(nf_ct_net(ct));
184
185         /* If we've seen traffic both ways, this is some kind of UDP
186            stream.  Extend timeout. */
187         if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
188                 nf_ct_refresh_acct(ct, ctinfo, skb,
189                                    timeouts[UDP_CT_REPLIED]);
190                 /* Also, more likely to be important, and not a probe */
191                 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
192                         nf_conntrack_event_cache(IPCT_ASSURED, ct);
193         } else {
194                 nf_ct_refresh_acct(ct, ctinfo, skb,
195                                    timeouts[UDP_CT_UNREPLIED]);
196         }
197         return NF_ACCEPT;
198 }
199 #endif
200
201 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
202
203 #include <linux/netfilter/nfnetlink.h>
204 #include <linux/netfilter/nfnetlink_cttimeout.h>
205
206 static int udp_timeout_nlattr_to_obj(struct nlattr *tb[],
207                                      struct net *net, void *data)
208 {
209         unsigned int *timeouts = data;
210         struct nf_udp_net *un = nf_udp_pernet(net);
211
212         if (!timeouts)
213                 timeouts = un->timeouts;
214
215         /* set default timeouts for UDP. */
216         timeouts[UDP_CT_UNREPLIED] = un->timeouts[UDP_CT_UNREPLIED];
217         timeouts[UDP_CT_REPLIED] = un->timeouts[UDP_CT_REPLIED];
218
219         if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) {
220                 timeouts[UDP_CT_UNREPLIED] =
221                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ;
222         }
223         if (tb[CTA_TIMEOUT_UDP_REPLIED]) {
224                 timeouts[UDP_CT_REPLIED] =
225                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ;
226         }
227         return 0;
228 }
229
230 static int
231 udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
232 {
233         const unsigned int *timeouts = data;
234
235         if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED,
236                          htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) ||
237             nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED,
238                          htonl(timeouts[UDP_CT_REPLIED] / HZ)))
239                 goto nla_put_failure;
240         return 0;
241
242 nla_put_failure:
243         return -ENOSPC;
244 }
245
246 static const struct nla_policy
247 udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = {
248        [CTA_TIMEOUT_UDP_UNREPLIED]      = { .type = NLA_U32 },
249        [CTA_TIMEOUT_UDP_REPLIED]        = { .type = NLA_U32 },
250 };
251 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
252
253 #ifdef CONFIG_SYSCTL
254 static struct ctl_table udp_sysctl_table[] = {
255         {
256                 .procname       = "nf_conntrack_udp_timeout",
257                 .maxlen         = sizeof(unsigned int),
258                 .mode           = 0644,
259                 .proc_handler   = proc_dointvec_jiffies,
260         },
261         {
262                 .procname       = "nf_conntrack_udp_timeout_stream",
263                 .maxlen         = sizeof(unsigned int),
264                 .mode           = 0644,
265                 .proc_handler   = proc_dointvec_jiffies,
266         },
267         { }
268 };
269 #endif /* CONFIG_SYSCTL */
270
271 static int udp_kmemdup_sysctl_table(struct nf_proto_net *pn,
272                                     struct nf_udp_net *un)
273 {
274 #ifdef CONFIG_SYSCTL
275         if (pn->ctl_table)
276                 return 0;
277         pn->ctl_table = kmemdup(udp_sysctl_table,
278                                 sizeof(udp_sysctl_table),
279                                 GFP_KERNEL);
280         if (!pn->ctl_table)
281                 return -ENOMEM;
282         pn->ctl_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
283         pn->ctl_table[1].data = &un->timeouts[UDP_CT_REPLIED];
284 #endif
285         return 0;
286 }
287
288 static int udp_init_net(struct net *net)
289 {
290         struct nf_udp_net *un = nf_udp_pernet(net);
291         struct nf_proto_net *pn = &un->pn;
292
293         if (!pn->users) {
294                 int i;
295
296                 for (i = 0; i < UDP_CT_MAX; i++)
297                         un->timeouts[i] = udp_timeouts[i];
298         }
299
300         return udp_kmemdup_sysctl_table(pn, un);
301 }
302
303 static struct nf_proto_net *udp_get_net_proto(struct net *net)
304 {
305         return &net->ct.nf_ct_proto.udp.pn;
306 }
307
308 const struct nf_conntrack_l4proto nf_conntrack_l4proto_udp =
309 {
310         .l4proto                = IPPROTO_UDP,
311         .allow_clash            = true,
312         .packet                 = udp_packet,
313 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
314         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
315         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
316         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
317         .nla_policy             = nf_ct_port_nla_policy,
318 #endif
319 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
320         .ctnl_timeout           = {
321                 .nlattr_to_obj  = udp_timeout_nlattr_to_obj,
322                 .obj_to_nlattr  = udp_timeout_obj_to_nlattr,
323                 .nlattr_max     = CTA_TIMEOUT_UDP_MAX,
324                 .obj_size       = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
325                 .nla_policy     = udp_timeout_nla_policy,
326         },
327 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
328         .init_net               = udp_init_net,
329         .get_net_proto          = udp_get_net_proto,
330 };
331
332 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
333 const struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite =
334 {
335         .l4proto                = IPPROTO_UDPLITE,
336         .allow_clash            = true,
337         .packet                 = udplite_packet,
338 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
339         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
340         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
341         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
342         .nla_policy             = nf_ct_port_nla_policy,
343 #endif
344 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
345         .ctnl_timeout           = {
346                 .nlattr_to_obj  = udp_timeout_nlattr_to_obj,
347                 .obj_to_nlattr  = udp_timeout_obj_to_nlattr,
348                 .nlattr_max     = CTA_TIMEOUT_UDP_MAX,
349                 .obj_size       = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
350                 .nla_policy     = udp_timeout_nla_policy,
351         },
352 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
353         .init_net               = udp_init_net,
354         .get_net_proto          = udp_get_net_proto,
355 };
356 #endif