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