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 inline struct nf_generic_net
*generic_pernet(struct net
*net
)
19 return &net
->ct
.nf_ct_proto
.generic
;
22 static bool generic_pkt_to_tuple(const struct sk_buff
*skb
,
24 struct nf_conntrack_tuple
*tuple
)
32 static bool generic_invert_tuple(struct nf_conntrack_tuple
*tuple
,
33 const struct nf_conntrack_tuple
*orig
)
41 /* Print out the per-protocol part of the tuple. */
42 static int generic_print_tuple(struct seq_file
*s
,
43 const struct nf_conntrack_tuple
*tuple
)
48 static unsigned int *generic_get_timeouts(struct net
*net
)
50 return &(generic_pernet(net
)->timeout
);
53 /* Returns verdict for packet, or -1 for invalid. */
54 static int generic_packet(struct nf_conn
*ct
,
55 const struct sk_buff
*skb
,
57 enum ip_conntrack_info ctinfo
,
60 unsigned int *timeout
)
62 nf_ct_refresh_acct(ct
, ctinfo
, skb
, *timeout
);
66 /* Called when a new connection for this protocol found. */
67 static bool generic_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
68 unsigned int dataoff
, unsigned int *timeouts
)
73 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
75 #include <linux/netfilter/nfnetlink.h>
76 #include <linux/netfilter/nfnetlink_cttimeout.h>
78 static int generic_timeout_nlattr_to_obj(struct nlattr
*tb
[],
79 struct net
*net
, void *data
)
81 unsigned int *timeout
= data
;
82 struct nf_generic_net
*gn
= generic_pernet(net
);
84 if (tb
[CTA_TIMEOUT_GENERIC_TIMEOUT
])
86 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_GENERIC_TIMEOUT
])) * HZ
;
88 /* Set default generic timeout. */
89 *timeout
= gn
->timeout
;
96 generic_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
98 const unsigned int *timeout
= data
;
100 if (nla_put_be32(skb
, CTA_TIMEOUT_GENERIC_TIMEOUT
, htonl(*timeout
/ HZ
)))
101 goto nla_put_failure
;
109 static const struct nla_policy
110 generic_timeout_nla_policy
[CTA_TIMEOUT_GENERIC_MAX
+1] = {
111 [CTA_TIMEOUT_GENERIC_TIMEOUT
] = { .type
= NLA_U32
},
113 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
116 static struct ctl_table generic_sysctl_table
[] = {
118 .procname
= "nf_conntrack_generic_timeout",
119 .maxlen
= sizeof(unsigned int),
121 .proc_handler
= proc_dointvec_jiffies
,
125 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
126 static struct ctl_table generic_compat_sysctl_table
[] = {
128 .procname
= "ip_conntrack_generic_timeout",
129 .maxlen
= sizeof(unsigned int),
131 .proc_handler
= proc_dointvec_jiffies
,
135 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
136 #endif /* CONFIG_SYSCTL */
138 static int generic_kmemdup_sysctl_table(struct nf_proto_net
*pn
,
139 struct nf_generic_net
*gn
)
142 pn
->ctl_table
= kmemdup(generic_sysctl_table
,
143 sizeof(generic_sysctl_table
),
148 pn
->ctl_table
[0].data
= &gn
->timeout
;
153 static int generic_kmemdup_compat_sysctl_table(struct nf_proto_net
*pn
,
154 struct nf_generic_net
*gn
)
157 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
158 pn
->ctl_compat_table
= kmemdup(generic_compat_sysctl_table
,
159 sizeof(generic_compat_sysctl_table
),
161 if (!pn
->ctl_compat_table
)
164 pn
->ctl_compat_table
[0].data
= &gn
->timeout
;
170 static int generic_init_net(struct net
*net
, u_int16_t proto
)
173 struct nf_generic_net
*gn
= generic_pernet(net
);
174 struct nf_proto_net
*pn
= &gn
->pn
;
176 gn
->timeout
= nf_ct_generic_timeout
;
178 ret
= generic_kmemdup_compat_sysctl_table(pn
, gn
);
182 ret
= generic_kmemdup_sysctl_table(pn
, gn
);
184 nf_ct_kfree_compat_sysctl_table(pn
);
189 static struct nf_proto_net
*generic_get_net_proto(struct net
*net
)
191 return &net
->ct
.nf_ct_proto
.generic
.pn
;
194 struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly
=
196 .l3proto
= PF_UNSPEC
,
199 .pkt_to_tuple
= generic_pkt_to_tuple
,
200 .invert_tuple
= generic_invert_tuple
,
201 .print_tuple
= generic_print_tuple
,
202 .packet
= generic_packet
,
203 .get_timeouts
= generic_get_timeouts
,
205 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
207 .nlattr_to_obj
= generic_timeout_nlattr_to_obj
,
208 .obj_to_nlattr
= generic_timeout_obj_to_nlattr
,
209 .nlattr_max
= CTA_TIMEOUT_GENERIC_MAX
,
210 .obj_size
= sizeof(unsigned int),
211 .nla_policy
= generic_timeout_nla_policy
,
213 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
214 .init_net
= generic_init_net
,
215 .get_net_proto
= generic_get_net_proto
,