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.
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 nf_generic_should_process(u8 proto
)
20 #ifdef CONFIG_NF_CT_PROTO_SCTP_MODULE
24 #ifdef CONFIG_NF_CT_PROTO_DCCP_MODULE
28 #ifdef CONFIG_NF_CT_PROTO_GRE_MODULE
32 #ifdef CONFIG_NF_CT_PROTO_UDPLITE_MODULE
41 static inline struct nf_generic_net
*generic_pernet(struct net
*net
)
43 return &net
->ct
.nf_ct_proto
.generic
;
46 static bool generic_pkt_to_tuple(const struct sk_buff
*skb
,
48 struct nf_conntrack_tuple
*tuple
)
56 static bool generic_invert_tuple(struct nf_conntrack_tuple
*tuple
,
57 const struct nf_conntrack_tuple
*orig
)
65 /* Print out the per-protocol part of the tuple. */
66 static int generic_print_tuple(struct seq_file
*s
,
67 const struct nf_conntrack_tuple
*tuple
)
72 static unsigned int *generic_get_timeouts(struct net
*net
)
74 return &(generic_pernet(net
)->timeout
);
77 /* Returns verdict for packet, or -1 for invalid. */
78 static int generic_packet(struct nf_conn
*ct
,
79 const struct sk_buff
*skb
,
81 enum ip_conntrack_info ctinfo
,
84 unsigned int *timeout
)
86 nf_ct_refresh_acct(ct
, ctinfo
, skb
, *timeout
);
90 /* Called when a new connection for this protocol found. */
91 static bool generic_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
92 unsigned int dataoff
, unsigned int *timeouts
)
94 return nf_generic_should_process(nf_ct_protonum(ct
));
97 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
99 #include <linux/netfilter/nfnetlink.h>
100 #include <linux/netfilter/nfnetlink_cttimeout.h>
102 static int generic_timeout_nlattr_to_obj(struct nlattr
*tb
[],
103 struct net
*net
, void *data
)
105 unsigned int *timeout
= data
;
106 struct nf_generic_net
*gn
= generic_pernet(net
);
108 if (tb
[CTA_TIMEOUT_GENERIC_TIMEOUT
])
110 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_GENERIC_TIMEOUT
])) * HZ
;
112 /* Set default generic timeout. */
113 *timeout
= gn
->timeout
;
120 generic_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
122 const unsigned int *timeout
= data
;
124 if (nla_put_be32(skb
, CTA_TIMEOUT_GENERIC_TIMEOUT
, htonl(*timeout
/ HZ
)))
125 goto nla_put_failure
;
133 static const struct nla_policy
134 generic_timeout_nla_policy
[CTA_TIMEOUT_GENERIC_MAX
+1] = {
135 [CTA_TIMEOUT_GENERIC_TIMEOUT
] = { .type
= NLA_U32
},
137 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
140 static struct ctl_table generic_sysctl_table
[] = {
142 .procname
= "nf_conntrack_generic_timeout",
143 .maxlen
= sizeof(unsigned int),
145 .proc_handler
= proc_dointvec_jiffies
,
149 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
150 static struct ctl_table generic_compat_sysctl_table
[] = {
152 .procname
= "ip_conntrack_generic_timeout",
153 .maxlen
= sizeof(unsigned int),
155 .proc_handler
= proc_dointvec_jiffies
,
159 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
160 #endif /* CONFIG_SYSCTL */
162 static int generic_kmemdup_sysctl_table(struct nf_proto_net
*pn
,
163 struct nf_generic_net
*gn
)
166 pn
->ctl_table
= kmemdup(generic_sysctl_table
,
167 sizeof(generic_sysctl_table
),
172 pn
->ctl_table
[0].data
= &gn
->timeout
;
177 static int generic_kmemdup_compat_sysctl_table(struct nf_proto_net
*pn
,
178 struct nf_generic_net
*gn
)
181 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
182 pn
->ctl_compat_table
= kmemdup(generic_compat_sysctl_table
,
183 sizeof(generic_compat_sysctl_table
),
185 if (!pn
->ctl_compat_table
)
188 pn
->ctl_compat_table
[0].data
= &gn
->timeout
;
194 static int generic_init_net(struct net
*net
, u_int16_t proto
)
197 struct nf_generic_net
*gn
= generic_pernet(net
);
198 struct nf_proto_net
*pn
= &gn
->pn
;
200 gn
->timeout
= nf_ct_generic_timeout
;
202 ret
= generic_kmemdup_compat_sysctl_table(pn
, gn
);
206 ret
= generic_kmemdup_sysctl_table(pn
, gn
);
208 nf_ct_kfree_compat_sysctl_table(pn
);
213 static struct nf_proto_net
*generic_get_net_proto(struct net
*net
)
215 return &net
->ct
.nf_ct_proto
.generic
.pn
;
218 struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly
=
220 .l3proto
= PF_UNSPEC
,
223 .pkt_to_tuple
= generic_pkt_to_tuple
,
224 .invert_tuple
= generic_invert_tuple
,
225 .print_tuple
= generic_print_tuple
,
226 .packet
= generic_packet
,
227 .get_timeouts
= generic_get_timeouts
,
229 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
231 .nlattr_to_obj
= generic_timeout_nlattr_to_obj
,
232 .obj_to_nlattr
= generic_timeout_obj_to_nlattr
,
233 .nlattr_max
= CTA_TIMEOUT_GENERIC_MAX
,
234 .obj_size
= sizeof(unsigned int),
235 .nla_policy
= generic_timeout_nla_policy
,
237 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
238 .init_net
= generic_init_net
,
239 .get_net_proto
= generic_get_net_proto
,