These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / net / netfilter / nf_conntrack_proto_udplite.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  * (C) 2007 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_log.h>
26
27 enum udplite_conntrack {
28         UDPLITE_CT_UNREPLIED,
29         UDPLITE_CT_REPLIED,
30         UDPLITE_CT_MAX
31 };
32
33 static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = {
34         [UDPLITE_CT_UNREPLIED]  = 30*HZ,
35         [UDPLITE_CT_REPLIED]    = 180*HZ,
36 };
37
38 static int udplite_net_id __read_mostly;
39 struct udplite_net {
40         struct nf_proto_net pn;
41         unsigned int timeouts[UDPLITE_CT_MAX];
42 };
43
44 static inline struct udplite_net *udplite_pernet(struct net *net)
45 {
46         return net_generic(net, udplite_net_id);
47 }
48
49 static bool udplite_pkt_to_tuple(const struct sk_buff *skb,
50                                  unsigned int dataoff,
51                                  struct net *net,
52                                  struct nf_conntrack_tuple *tuple)
53 {
54         const struct udphdr *hp;
55         struct udphdr _hdr;
56
57         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
58         if (hp == NULL)
59                 return false;
60
61         tuple->src.u.udp.port = hp->source;
62         tuple->dst.u.udp.port = hp->dest;
63         return true;
64 }
65
66 static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
67                                  const struct nf_conntrack_tuple *orig)
68 {
69         tuple->src.u.udp.port = orig->dst.u.udp.port;
70         tuple->dst.u.udp.port = orig->src.u.udp.port;
71         return true;
72 }
73
74 /* Print out the per-protocol part of the tuple. */
75 static void udplite_print_tuple(struct seq_file *s,
76                                 const struct nf_conntrack_tuple *tuple)
77 {
78         seq_printf(s, "sport=%hu dport=%hu ",
79                    ntohs(tuple->src.u.udp.port),
80                    ntohs(tuple->dst.u.udp.port));
81 }
82
83 static unsigned int *udplite_get_timeouts(struct net *net)
84 {
85         return udplite_pernet(net)->timeouts;
86 }
87
88 /* Returns verdict for packet, and may modify conntracktype */
89 static int udplite_packet(struct nf_conn *ct,
90                           const struct sk_buff *skb,
91                           unsigned int dataoff,
92                           enum ip_conntrack_info ctinfo,
93                           u_int8_t pf,
94                           unsigned int hooknum,
95                           unsigned int *timeouts)
96 {
97         /* If we've seen traffic both ways, this is some kind of UDP
98            stream.  Extend timeout. */
99         if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
100                 nf_ct_refresh_acct(ct, ctinfo, skb,
101                                    timeouts[UDPLITE_CT_REPLIED]);
102                 /* Also, more likely to be important, and not a probe */
103                 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
104                         nf_conntrack_event_cache(IPCT_ASSURED, ct);
105         } else {
106                 nf_ct_refresh_acct(ct, ctinfo, skb,
107                                    timeouts[UDPLITE_CT_UNREPLIED]);
108         }
109         return NF_ACCEPT;
110 }
111
112 /* Called when a new connection for this protocol found. */
113 static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
114                         unsigned int dataoff, unsigned int *timeouts)
115 {
116         return true;
117 }
118
119 static int udplite_error(struct net *net, struct nf_conn *tmpl,
120                          struct sk_buff *skb,
121                          unsigned int dataoff,
122                          enum ip_conntrack_info *ctinfo,
123                          u_int8_t pf,
124                          unsigned int hooknum)
125 {
126         unsigned int udplen = skb->len - dataoff;
127         const struct udphdr *hdr;
128         struct udphdr _hdr;
129         unsigned int cscov;
130
131         /* Header is too small? */
132         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
133         if (hdr == NULL) {
134                 if (LOG_INVALID(net, IPPROTO_UDPLITE))
135                         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
136                                       "nf_ct_udplite: short packet ");
137                 return -NF_ACCEPT;
138         }
139
140         cscov = ntohs(hdr->len);
141         if (cscov == 0)
142                 cscov = udplen;
143         else if (cscov < sizeof(*hdr) || cscov > udplen) {
144                 if (LOG_INVALID(net, IPPROTO_UDPLITE))
145                         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
146                                 "nf_ct_udplite: invalid checksum coverage ");
147                 return -NF_ACCEPT;
148         }
149
150         /* UDPLITE mandates checksums */
151         if (!hdr->check) {
152                 if (LOG_INVALID(net, IPPROTO_UDPLITE))
153                         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
154                                       "nf_ct_udplite: checksum missing ");
155                 return -NF_ACCEPT;
156         }
157
158         /* Checksum invalid? Ignore. */
159         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
160             nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP,
161                                 pf)) {
162                 if (LOG_INVALID(net, IPPROTO_UDPLITE))
163                         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
164                                       "nf_ct_udplite: bad UDPLite checksum ");
165                 return -NF_ACCEPT;
166         }
167
168         return NF_ACCEPT;
169 }
170
171 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
172
173 #include <linux/netfilter/nfnetlink.h>
174 #include <linux/netfilter/nfnetlink_cttimeout.h>
175
176 static int udplite_timeout_nlattr_to_obj(struct nlattr *tb[],
177                                          struct net *net, void *data)
178 {
179         unsigned int *timeouts = data;
180         struct udplite_net *un = udplite_pernet(net);
181
182         /* set default timeouts for UDPlite. */
183         timeouts[UDPLITE_CT_UNREPLIED] = un->timeouts[UDPLITE_CT_UNREPLIED];
184         timeouts[UDPLITE_CT_REPLIED] = un->timeouts[UDPLITE_CT_REPLIED];
185
186         if (tb[CTA_TIMEOUT_UDPLITE_UNREPLIED]) {
187                 timeouts[UDPLITE_CT_UNREPLIED] =
188                   ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_UNREPLIED])) * HZ;
189         }
190         if (tb[CTA_TIMEOUT_UDPLITE_REPLIED]) {
191                 timeouts[UDPLITE_CT_REPLIED] =
192                   ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_REPLIED])) * HZ;
193         }
194         return 0;
195 }
196
197 static int
198 udplite_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
199 {
200         const unsigned int *timeouts = data;
201
202         if (nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_UNREPLIED,
203                          htonl(timeouts[UDPLITE_CT_UNREPLIED] / HZ)) ||
204             nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_REPLIED,
205                          htonl(timeouts[UDPLITE_CT_REPLIED] / HZ)))
206                 goto nla_put_failure;
207         return 0;
208
209 nla_put_failure:
210         return -ENOSPC;
211 }
212
213 static const struct nla_policy
214 udplite_timeout_nla_policy[CTA_TIMEOUT_UDPLITE_MAX+1] = {
215         [CTA_TIMEOUT_UDPLITE_UNREPLIED] = { .type = NLA_U32 },
216         [CTA_TIMEOUT_UDPLITE_REPLIED]   = { .type = NLA_U32 },
217 };
218 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
219
220 #ifdef CONFIG_SYSCTL
221 static struct ctl_table udplite_sysctl_table[] = {
222         {
223                 .procname       = "nf_conntrack_udplite_timeout",
224                 .maxlen         = sizeof(unsigned int),
225                 .mode           = 0644,
226                 .proc_handler   = proc_dointvec_jiffies,
227         },
228         {
229                 .procname       = "nf_conntrack_udplite_timeout_stream",
230                 .maxlen         = sizeof(unsigned int),
231                 .mode           = 0644,
232                 .proc_handler   = proc_dointvec_jiffies,
233         },
234         { }
235 };
236 #endif /* CONFIG_SYSCTL */
237
238 static int udplite_kmemdup_sysctl_table(struct nf_proto_net *pn,
239                                         struct udplite_net *un)
240 {
241 #ifdef CONFIG_SYSCTL
242         if (pn->ctl_table)
243                 return 0;
244
245         pn->ctl_table = kmemdup(udplite_sysctl_table,
246                                 sizeof(udplite_sysctl_table),
247                                 GFP_KERNEL);
248         if (!pn->ctl_table)
249                 return -ENOMEM;
250
251         pn->ctl_table[0].data = &un->timeouts[UDPLITE_CT_UNREPLIED];
252         pn->ctl_table[1].data = &un->timeouts[UDPLITE_CT_REPLIED];
253 #endif
254         return 0;
255 }
256
257 static int udplite_init_net(struct net *net, u_int16_t proto)
258 {
259         struct udplite_net *un = udplite_pernet(net);
260         struct nf_proto_net *pn = &un->pn;
261
262         if (!pn->users) {
263                 int i;
264
265                 for (i = 0 ; i < UDPLITE_CT_MAX; i++)
266                         un->timeouts[i] = udplite_timeouts[i];
267         }
268
269         return udplite_kmemdup_sysctl_table(pn, un);
270 }
271
272 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
273 {
274         .l3proto                = PF_INET,
275         .l4proto                = IPPROTO_UDPLITE,
276         .name                   = "udplite",
277         .pkt_to_tuple           = udplite_pkt_to_tuple,
278         .invert_tuple           = udplite_invert_tuple,
279         .print_tuple            = udplite_print_tuple,
280         .packet                 = udplite_packet,
281         .get_timeouts           = udplite_get_timeouts,
282         .new                    = udplite_new,
283         .error                  = udplite_error,
284 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
285         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
286         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
287         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
288         .nla_policy             = nf_ct_port_nla_policy,
289 #endif
290 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
291         .ctnl_timeout           = {
292                 .nlattr_to_obj  = udplite_timeout_nlattr_to_obj,
293                 .obj_to_nlattr  = udplite_timeout_obj_to_nlattr,
294                 .nlattr_max     = CTA_TIMEOUT_UDPLITE_MAX,
295                 .obj_size       = sizeof(unsigned int) *
296                                         CTA_TIMEOUT_UDPLITE_MAX,
297                 .nla_policy     = udplite_timeout_nla_policy,
298         },
299 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
300         .net_id                 = &udplite_net_id,
301         .init_net               = udplite_init_net,
302 };
303
304 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
305 {
306         .l3proto                = PF_INET6,
307         .l4proto                = IPPROTO_UDPLITE,
308         .name                   = "udplite",
309         .pkt_to_tuple           = udplite_pkt_to_tuple,
310         .invert_tuple           = udplite_invert_tuple,
311         .print_tuple            = udplite_print_tuple,
312         .packet                 = udplite_packet,
313         .get_timeouts           = udplite_get_timeouts,
314         .new                    = udplite_new,
315         .error                  = udplite_error,
316 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
317         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
318         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
319         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
320         .nla_policy             = nf_ct_port_nla_policy,
321 #endif
322 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
323         .ctnl_timeout           = {
324                 .nlattr_to_obj  = udplite_timeout_nlattr_to_obj,
325                 .obj_to_nlattr  = udplite_timeout_obj_to_nlattr,
326                 .nlattr_max     = CTA_TIMEOUT_UDPLITE_MAX,
327                 .obj_size       = sizeof(unsigned int) *
328                                         CTA_TIMEOUT_UDPLITE_MAX,
329                 .nla_policy     = udplite_timeout_nla_policy,
330         },
331 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
332         .net_id                 = &udplite_net_id,
333         .init_net               = udplite_init_net,
334 };
335
336 static int udplite_net_init(struct net *net)
337 {
338         int ret = 0;
339
340         ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite4);
341         if (ret < 0) {
342                 pr_err("nf_conntrack_udplite4: pernet registration failed.\n");
343                 goto out;
344         }
345         ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite6);
346         if (ret < 0) {
347                 pr_err("nf_conntrack_udplite6: pernet registration failed.\n");
348                 goto cleanup_udplite4;
349         }
350         return 0;
351
352 cleanup_udplite4:
353         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4);
354 out:
355         return ret;
356 }
357
358 static void udplite_net_exit(struct net *net)
359 {
360         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite6);
361         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4);
362 }
363
364 static struct pernet_operations udplite_net_ops = {
365         .init = udplite_net_init,
366         .exit = udplite_net_exit,
367         .id   = &udplite_net_id,
368         .size = sizeof(struct udplite_net),
369 };
370
371 static int __init nf_conntrack_proto_udplite_init(void)
372 {
373         int ret;
374
375         ret = register_pernet_subsys(&udplite_net_ops);
376         if (ret < 0)
377                 goto out_pernet;
378
379         ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite4);
380         if (ret < 0)
381                 goto out_udplite4;
382
383         ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite6);
384         if (ret < 0)
385                 goto out_udplite6;
386
387         return 0;
388 out_udplite6:
389         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
390 out_udplite4:
391         unregister_pernet_subsys(&udplite_net_ops);
392 out_pernet:
393         return ret;
394 }
395
396 static void __exit nf_conntrack_proto_udplite_exit(void)
397 {
398         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
399         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
400         unregister_pernet_subsys(&udplite_net_ops);
401 }
402
403 module_init(nf_conntrack_proto_udplite_init);
404 module_exit(nf_conntrack_proto_udplite_exit);
405
406 MODULE_LICENSE("GPL");