netfilter: add cttimeout infrastructure for fine timeout tuning
[linux-2.6.git] / net / netfilter / nf_conntrack_proto_generic.c
blob835e24c58f0de3ab67977aad017d456a840e28ce
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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 */
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>
15 static unsigned int nf_ct_generic_timeout __read_mostly = 600*HZ;
17 static bool generic_pkt_to_tuple(const struct sk_buff *skb,
18 unsigned int dataoff,
19 struct nf_conntrack_tuple *tuple)
21 tuple->src.u.all = 0;
22 tuple->dst.u.all = 0;
24 return true;
27 static bool generic_invert_tuple(struct nf_conntrack_tuple *tuple,
28 const struct nf_conntrack_tuple *orig)
30 tuple->src.u.all = 0;
31 tuple->dst.u.all = 0;
33 return true;
36 /* Print out the per-protocol part of the tuple. */
37 static int generic_print_tuple(struct seq_file *s,
38 const struct nf_conntrack_tuple *tuple)
40 return 0;
43 static unsigned int *generic_get_timeouts(struct net *net)
45 return &nf_ct_generic_timeout;
48 /* Returns verdict for packet, or -1 for invalid. */
49 static int generic_packet(struct nf_conn *ct,
50 const struct sk_buff *skb,
51 unsigned int dataoff,
52 enum ip_conntrack_info ctinfo,
53 u_int8_t pf,
54 unsigned int hooknum,
55 unsigned int *timeout)
57 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
58 return NF_ACCEPT;
61 /* Called when a new connection for this protocol found. */
62 static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb,
63 unsigned int dataoff, unsigned int *timeouts)
65 return true;
68 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
70 #include <linux/netfilter/nfnetlink.h>
71 #include <linux/netfilter/nfnetlink_cttimeout.h>
73 static int generic_timeout_nlattr_to_obj(struct nlattr *tb[], void *data)
75 unsigned int *timeout = data;
77 if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
78 *timeout =
79 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ;
80 else {
81 /* Set default generic timeout. */
82 *timeout = nf_ct_generic_timeout;
85 return 0;
88 static int
89 generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
91 const unsigned int *timeout = data;
93 NLA_PUT_BE32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ));
95 return 0;
97 nla_put_failure:
98 return -ENOSPC;
101 static const struct nla_policy
102 generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = {
103 [CTA_TIMEOUT_GENERIC_TIMEOUT] = { .type = NLA_U32 },
105 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
107 #ifdef CONFIG_SYSCTL
108 static struct ctl_table_header *generic_sysctl_header;
109 static struct ctl_table generic_sysctl_table[] = {
111 .procname = "nf_conntrack_generic_timeout",
112 .data = &nf_ct_generic_timeout,
113 .maxlen = sizeof(unsigned int),
114 .mode = 0644,
115 .proc_handler = proc_dointvec_jiffies,
119 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
120 static struct ctl_table generic_compat_sysctl_table[] = {
122 .procname = "ip_conntrack_generic_timeout",
123 .data = &nf_ct_generic_timeout,
124 .maxlen = sizeof(unsigned int),
125 .mode = 0644,
126 .proc_handler = proc_dointvec_jiffies,
130 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
131 #endif /* CONFIG_SYSCTL */
133 struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly =
135 .l3proto = PF_UNSPEC,
136 .l4proto = 255,
137 .name = "unknown",
138 .pkt_to_tuple = generic_pkt_to_tuple,
139 .invert_tuple = generic_invert_tuple,
140 .print_tuple = generic_print_tuple,
141 .packet = generic_packet,
142 .get_timeouts = generic_get_timeouts,
143 .new = generic_new,
144 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
145 .ctnl_timeout = {
146 .nlattr_to_obj = generic_timeout_nlattr_to_obj,
147 .obj_to_nlattr = generic_timeout_obj_to_nlattr,
148 .nlattr_max = CTA_TIMEOUT_GENERIC_MAX,
149 .obj_size = sizeof(unsigned int),
150 .nla_policy = generic_timeout_nla_policy,
152 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
153 #ifdef CONFIG_SYSCTL
154 .ctl_table_header = &generic_sysctl_header,
155 .ctl_table = generic_sysctl_table,
156 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
157 .ctl_compat_table = generic_compat_sysctl_table,
158 #endif
159 #endif