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_init_net(struct net *net)
140 struct nf_generic_net *gn = generic_pernet(net);
141 struct nf_proto_net *pn = (struct nf_proto_net *)gn;
142 gn->timeout = nf_ct_generic_timeout;
144 pn->ctl_table = kmemdup(generic_sysctl_table,
145 sizeof(generic_sysctl_table),
149 pn->ctl_table[0].data = &gn->timeout;
151 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
152 pn->ctl_compat_table = kmemdup(generic_compat_sysctl_table,
153 sizeof(generic_compat_sysctl_table),
155 if (!pn->ctl_compat_table) {
156 kfree(pn->ctl_table);
157 pn->ctl_table = NULL;
160 pn->ctl_compat_table[0].data = &gn->timeout;
166 struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly =
168 .l3proto = PF_UNSPEC,
171 .pkt_to_tuple = generic_pkt_to_tuple,
172 .invert_tuple = generic_invert_tuple,
173 .print_tuple = generic_print_tuple,
174 .packet = generic_packet,
175 .get_timeouts = generic_get_timeouts,
177 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
179 .nlattr_to_obj = generic_timeout_nlattr_to_obj,
180 .obj_to_nlattr = generic_timeout_obj_to_nlattr,
181 .nlattr_max = CTA_TIMEOUT_GENERIC_MAX,
182 .obj_size = sizeof(unsigned int),
183 .nla_policy = generic_timeout_nla_policy,
185 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
186 .init_net = generic_init_net,