1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/types.h>
10 #include <linux/jiffies.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
13 #include <net/netfilter/nf_conntrack_l4proto.h>
14 #include <net/netfilter/nf_conntrack_timeout.h>
15 
16 static const unsigned int nf_ct_generic_timeout = 600*HZ;
17 
18 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
19 
20 #include <linux/netfilter/nfnetlink.h>
21 #include <linux/netfilter/nfnetlink_cttimeout.h>
22 
23 static int generic_timeout_nlattr_to_obj(struct nlattr *tb[],
24 					 struct net *net, void *data)
25 {
26 	struct nf_generic_net *gn = nf_generic_pernet(net);
27 	unsigned int *timeout = data;
28 
29 	if (!timeout)
30 		timeout = &gn->timeout;
31 
32 	if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
33 		*timeout =
34 		    ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ;
35 	else {
36 		/* Set default generic timeout. */
37 		*timeout = gn->timeout;
38 	}
39 
40 	return 0;
41 }
42 
43 static int
44 generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
45 {
46 	const unsigned int *timeout = data;
47 
48 	if (nla_put_be32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ)))
49 		goto nla_put_failure;
50 
51 	return 0;
52 
53 nla_put_failure:
54         return -ENOSPC;
55 }
56 
57 static const struct nla_policy
58 generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = {
59 	[CTA_TIMEOUT_GENERIC_TIMEOUT]	= { .type = NLA_U32 },
60 };
61 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
62 
63 void nf_conntrack_generic_init_net(struct net *net)
64 {
65 	struct nf_generic_net *gn = nf_generic_pernet(net);
66 
67 	gn->timeout = nf_ct_generic_timeout;
68 }
69 
70 const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic =
71 {
72 	.l4proto		= 255,
73 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
74 	.ctnl_timeout		= {
75 		.nlattr_to_obj	= generic_timeout_nlattr_to_obj,
76 		.obj_to_nlattr	= generic_timeout_obj_to_nlattr,
77 		.nlattr_max	= CTA_TIMEOUT_GENERIC_MAX,
78 		.obj_size	= sizeof(unsigned int),
79 		.nla_policy	= generic_timeout_nla_policy,
80 	},
81 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
82 };
83