]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nf_conntrack_proto_udplite.c
Merge tag 'armsoc-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nf_conntrack_proto_udplite.c
CommitLineData
59eecdfb
PM
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>
59eecdfb
PM
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>
f01ffbd6 25#include <net/netfilter/nf_log.h>
59eecdfb 26
5a41db94
PNA
27enum udplite_conntrack {
28 UDPLITE_CT_UNREPLIED,
29 UDPLITE_CT_REPLIED,
30 UDPLITE_CT_MAX
31};
32
33static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = {
34 [UDPLITE_CT_UNREPLIED] = 30*HZ,
35 [UDPLITE_CT_REPLIED] = 180*HZ,
36};
59eecdfb 37
a8021fed
G
38static int udplite_net_id __read_mostly;
39struct udplite_net {
40 struct nf_proto_net pn;
41 unsigned int timeouts[UDPLITE_CT_MAX];
42};
43
44static inline struct udplite_net *udplite_pernet(struct net *net)
45{
46 return net_generic(net, udplite_net_id);
47}
48
09f263cd
JE
49static bool udplite_pkt_to_tuple(const struct sk_buff *skb,
50 unsigned int dataoff,
a31f1adc 51 struct net *net,
09f263cd 52 struct nf_conntrack_tuple *tuple)
59eecdfb 53{
da3f13c9
JE
54 const struct udphdr *hp;
55 struct udphdr _hdr;
59eecdfb
PM
56
57 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
58 if (hp == NULL)
09f263cd 59 return false;
59eecdfb
PM
60
61 tuple->src.u.udp.port = hp->source;
62 tuple->dst.u.udp.port = hp->dest;
09f263cd 63 return true;
59eecdfb
PM
64}
65
09f263cd
JE
66static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
67 const struct nf_conntrack_tuple *orig)
59eecdfb
PM
68{
69 tuple->src.u.udp.port = orig->dst.u.udp.port;
70 tuple->dst.u.udp.port = orig->src.u.udp.port;
09f263cd 71 return true;
59eecdfb
PM
72}
73
74/* Print out the per-protocol part of the tuple. */
824f1fbe
JP
75static void udplite_print_tuple(struct seq_file *s,
76 const struct nf_conntrack_tuple *tuple)
59eecdfb 77{
824f1fbe
JP
78 seq_printf(s, "sport=%hu dport=%hu ",
79 ntohs(tuple->src.u.udp.port),
80 ntohs(tuple->dst.u.udp.port));
59eecdfb
PM
81}
82
2c8503f5
PNA
83static unsigned int *udplite_get_timeouts(struct net *net)
84{
a8021fed 85 return udplite_pernet(net)->timeouts;
2c8503f5
PNA
86}
87
59eecdfb 88/* Returns verdict for packet, and may modify conntracktype */
c88130bc 89static int udplite_packet(struct nf_conn *ct,
59eecdfb
PM
90 const struct sk_buff *skb,
91 unsigned int dataoff,
92 enum ip_conntrack_info ctinfo,
76108cea 93 u_int8_t pf,
2c8503f5
PNA
94 unsigned int hooknum,
95 unsigned int *timeouts)
59eecdfb
PM
96{
97 /* If we've seen traffic both ways, this is some kind of UDP
98 stream. Extend timeout. */
c88130bc
PM
99 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
100 nf_ct_refresh_acct(ct, ctinfo, skb,
2c8503f5 101 timeouts[UDPLITE_CT_REPLIED]);
59eecdfb 102 /* Also, more likely to be important, and not a probe */
c88130bc 103 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
858b3133 104 nf_conntrack_event_cache(IPCT_ASSURED, ct);
5a41db94
PNA
105 } else {
106 nf_ct_refresh_acct(ct, ctinfo, skb,
2c8503f5 107 timeouts[UDPLITE_CT_UNREPLIED]);
5a41db94 108 }
59eecdfb
PM
109 return NF_ACCEPT;
110}
111
112/* Called when a new connection for this protocol found. */
09f263cd 113static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
2c8503f5 114 unsigned int dataoff, unsigned int *timeouts)
59eecdfb 115{
09f263cd 116 return true;
59eecdfb
PM
117}
118
8fea97ec 119static int udplite_error(struct net *net, struct nf_conn *tmpl,
74c51a14
AD
120 struct sk_buff *skb,
121 unsigned int dataoff,
59eecdfb 122 enum ip_conntrack_info *ctinfo,
76108cea 123 u_int8_t pf,
59eecdfb
PM
124 unsigned int hooknum)
125{
126 unsigned int udplen = skb->len - dataoff;
da3f13c9
JE
127 const struct udphdr *hdr;
128 struct udphdr _hdr;
59eecdfb
PM
129 unsigned int cscov;
130
131 /* Header is too small? */
132 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
133 if (hdr == NULL) {
c2a2c7e0 134 if (LOG_INVALID(net, IPPROTO_UDPLITE))
30e0c6a6 135 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
59eecdfb
PM
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) {
c2a2c7e0 144 if (LOG_INVALID(net, IPPROTO_UDPLITE))
30e0c6a6 145 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
59eecdfb
PM
146 "nf_ct_udplite: invalid checksum coverage ");
147 return -NF_ACCEPT;
148 }
149
150 /* UDPLITE mandates checksums */
151 if (!hdr->check) {
c2a2c7e0 152 if (LOG_INVALID(net, IPPROTO_UDPLITE))
30e0c6a6 153 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
59eecdfb
PM
154 "nf_ct_udplite: checksum missing ");
155 return -NF_ACCEPT;
156 }
157
158 /* Checksum invalid? Ignore. */
c04d0552 159 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
d63a6507
PM
160 nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP,
161 pf)) {
c2a2c7e0 162 if (LOG_INVALID(net, IPPROTO_UDPLITE))
30e0c6a6 163 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
d63a6507
PM
164 "nf_ct_udplite: bad UDPLite checksum ");
165 return -NF_ACCEPT;
59eecdfb
PM
166 }
167
168 return NF_ACCEPT;
169}
170
50978462
PNA
171#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
172
173#include <linux/netfilter/nfnetlink.h>
174#include <linux/netfilter/nfnetlink_cttimeout.h>
175
8264deb8
G
176static int udplite_timeout_nlattr_to_obj(struct nlattr *tb[],
177 struct net *net, void *data)
50978462
PNA
178{
179 unsigned int *timeouts = data;
8264deb8 180 struct udplite_net *un = udplite_pernet(net);
50978462
PNA
181
182 /* set default timeouts for UDPlite. */
8264deb8
G
183 timeouts[UDPLITE_CT_UNREPLIED] = un->timeouts[UDPLITE_CT_UNREPLIED];
184 timeouts[UDPLITE_CT_REPLIED] = un->timeouts[UDPLITE_CT_REPLIED];
50978462
PNA
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
197static int
198udplite_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
199{
200 const unsigned int *timeouts = data;
201
3c60a17b
DM
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;
50978462
PNA
207 return 0;
208
209nla_put_failure:
210 return -ENOSPC;
211}
212
213static const struct nla_policy
214udplite_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
59eecdfb 220#ifdef CONFIG_SYSCTL
59eecdfb
PM
221static struct ctl_table udplite_sysctl_table[] = {
222 {
59eecdfb 223 .procname = "nf_conntrack_udplite_timeout",
59eecdfb
PM
224 .maxlen = sizeof(unsigned int),
225 .mode = 0644,
6d9f239a 226 .proc_handler = proc_dointvec_jiffies,
59eecdfb
PM
227 },
228 {
59eecdfb 229 .procname = "nf_conntrack_udplite_timeout_stream",
59eecdfb
PM
230 .maxlen = sizeof(unsigned int),
231 .mode = 0644,
6d9f239a 232 .proc_handler = proc_dointvec_jiffies,
59eecdfb 233 },
f8572d8f 234 { }
59eecdfb
PM
235};
236#endif /* CONFIG_SYSCTL */
237
51b4c824
G
238static int udplite_kmemdup_sysctl_table(struct nf_proto_net *pn,
239 struct udplite_net *un)
a8021fed 240{
a8021fed 241#ifdef CONFIG_SYSCTL
51b4c824
G
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];
a8021fed 253#endif
51b4c824
G
254 return 0;
255}
256
257static 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
a8021fed
G
265 for (i = 0 ; i < UDPLITE_CT_MAX; i++)
266 un->timeouts[i] = udplite_timeouts[i];
a8021fed 267 }
51b4c824
G
268
269 return udplite_kmemdup_sysctl_table(pn, un);
a8021fed
G
270}
271
59eecdfb
PM
272static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
273{
274 .l3proto = PF_INET,
275 .l4proto = IPPROTO_UDPLITE,
276 .name = "udplite",
71d8c47f 277 .allow_clash = true,
59eecdfb
PM
278 .pkt_to_tuple = udplite_pkt_to_tuple,
279 .invert_tuple = udplite_invert_tuple,
280 .print_tuple = udplite_print_tuple,
59eecdfb 281 .packet = udplite_packet,
2c8503f5 282 .get_timeouts = udplite_get_timeouts,
59eecdfb
PM
283 .new = udplite_new,
284 .error = udplite_error,
c0cd1156 285#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 286 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
a400c30e 287 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
fdf70832 288 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
f73e924c 289 .nla_policy = nf_ct_port_nla_policy,
59eecdfb 290#endif
50978462
PNA
291#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
292 .ctnl_timeout = {
293 .nlattr_to_obj = udplite_timeout_nlattr_to_obj,
294 .obj_to_nlattr = udplite_timeout_obj_to_nlattr,
295 .nlattr_max = CTA_TIMEOUT_UDPLITE_MAX,
296 .obj_size = sizeof(unsigned int) *
297 CTA_TIMEOUT_UDPLITE_MAX,
298 .nla_policy = udplite_timeout_nla_policy,
299 },
300#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
a8021fed
G
301 .net_id = &udplite_net_id,
302 .init_net = udplite_init_net,
59eecdfb
PM
303};
304
305static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
306{
307 .l3proto = PF_INET6,
308 .l4proto = IPPROTO_UDPLITE,
309 .name = "udplite",
71d8c47f 310 .allow_clash = true,
59eecdfb
PM
311 .pkt_to_tuple = udplite_pkt_to_tuple,
312 .invert_tuple = udplite_invert_tuple,
313 .print_tuple = udplite_print_tuple,
59eecdfb 314 .packet = udplite_packet,
2c8503f5 315 .get_timeouts = udplite_get_timeouts,
59eecdfb
PM
316 .new = udplite_new,
317 .error = udplite_error,
c0cd1156 318#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 319 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
5ff48294 320 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
fdf70832 321 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
f73e924c 322 .nla_policy = nf_ct_port_nla_policy,
59eecdfb 323#endif
50978462
PNA
324#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
325 .ctnl_timeout = {
326 .nlattr_to_obj = udplite_timeout_nlattr_to_obj,
327 .obj_to_nlattr = udplite_timeout_obj_to_nlattr,
328 .nlattr_max = CTA_TIMEOUT_UDPLITE_MAX,
329 .obj_size = sizeof(unsigned int) *
330 CTA_TIMEOUT_UDPLITE_MAX,
331 .nla_policy = udplite_timeout_nla_policy,
332 },
333#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
a8021fed
G
334 .net_id = &udplite_net_id,
335 .init_net = udplite_init_net,
59eecdfb
PM
336};
337
a8021fed 338static int udplite_net_init(struct net *net)
59eecdfb 339{
a8021fed
G
340 int ret = 0;
341
c296bb4d 342 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite4);
a8021fed 343 if (ret < 0) {
c296bb4d 344 pr_err("nf_conntrack_udplite4: pernet registration failed.\n");
a8021fed
G
345 goto out;
346 }
c296bb4d 347 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite6);
a8021fed 348 if (ret < 0) {
c296bb4d 349 pr_err("nf_conntrack_udplite6: pernet registration failed.\n");
a8021fed
G
350 goto cleanup_udplite4;
351 }
59eecdfb 352 return 0;
a8021fed
G
353
354cleanup_udplite4:
c296bb4d 355 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4);
a8021fed
G
356out:
357 return ret;
358}
359
360static void udplite_net_exit(struct net *net)
361{
c296bb4d
G
362 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite6);
363 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4);
a8021fed
G
364}
365
366static struct pernet_operations udplite_net_ops = {
367 .init = udplite_net_init,
368 .exit = udplite_net_exit,
369 .id = &udplite_net_id,
370 .size = sizeof(struct udplite_net),
371};
372
373static int __init nf_conntrack_proto_udplite_init(void)
374{
c296bb4d
G
375 int ret;
376
0d98da5d
G
377 ret = register_pernet_subsys(&udplite_net_ops);
378 if (ret < 0)
379 goto out_pernet;
380
c296bb4d
G
381 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite4);
382 if (ret < 0)
383 goto out_udplite4;
384
385 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite6);
386 if (ret < 0)
387 goto out_udplite6;
388
c296bb4d 389 return 0;
c296bb4d
G
390out_udplite6:
391 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
392out_udplite4:
0d98da5d
G
393 unregister_pernet_subsys(&udplite_net_ops);
394out_pernet:
c296bb4d 395 return ret;
59eecdfb
PM
396}
397
398static void __exit nf_conntrack_proto_udplite_exit(void)
399{
c296bb4d
G
400 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
401 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
a8021fed 402 unregister_pernet_subsys(&udplite_net_ops);
59eecdfb
PM
403}
404
405module_init(nf_conntrack_proto_udplite_init);
406module_exit(nf_conntrack_proto_udplite_exit);
407
408MODULE_LICENSE("GPL");