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>
14 #include <net/netfilter/nf_conntrack_timeout.h>
16 static const unsigned int nf_ct_generic_timeout = 600*HZ;
18 static bool nf_generic_should_process(u8 proto)
21 #ifdef CONFIG_NF_CT_PROTO_GRE_MODULE
30 static inline struct nf_generic_net *generic_pernet(struct net *net)
32 return &net->ct.nf_ct_proto.generic;
35 static bool generic_pkt_to_tuple(const struct sk_buff *skb,
37 struct net *net, struct nf_conntrack_tuple *tuple)
45 /* Returns verdict for packet, or -1 for invalid. */
46 static int generic_packet(struct nf_conn *ct,
49 enum ip_conntrack_info ctinfo,
50 const struct nf_hook_state *state)
52 const unsigned int *timeout = nf_ct_timeout_lookup(ct);
54 if (!nf_generic_should_process(nf_ct_protonum(ct))) {
55 pr_warn_once("conntrack: generic helper won't handle protocol %d. Please consider loading the specific helper module.\n",
61 timeout = &generic_pernet(nf_ct_net(ct))->timeout;
63 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
67 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
69 #include <linux/netfilter/nfnetlink.h>
70 #include <linux/netfilter/nfnetlink_cttimeout.h>
72 static int generic_timeout_nlattr_to_obj(struct nlattr *tb[],
73 struct net *net, void *data)
75 struct nf_generic_net *gn = generic_pernet(net);
76 unsigned int *timeout = data;
79 timeout = &gn->timeout;
81 if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
83 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ;
85 /* Set default generic timeout. */
86 *timeout = gn->timeout;
93 generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
95 const unsigned int *timeout = data;
97 if (nla_put_be32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ)))
106 static const struct nla_policy
107 generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = {
108 [CTA_TIMEOUT_GENERIC_TIMEOUT] = { .type = NLA_U32 },
110 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
113 static struct ctl_table generic_sysctl_table[] = {
115 .procname = "nf_conntrack_generic_timeout",
116 .maxlen = sizeof(unsigned int),
118 .proc_handler = proc_dointvec_jiffies,
122 #endif /* CONFIG_SYSCTL */
124 static int generic_kmemdup_sysctl_table(struct nf_proto_net *pn,
125 struct nf_generic_net *gn)
128 pn->ctl_table = kmemdup(generic_sysctl_table,
129 sizeof(generic_sysctl_table),
134 pn->ctl_table[0].data = &gn->timeout;
139 static int generic_init_net(struct net *net)
141 struct nf_generic_net *gn = generic_pernet(net);
142 struct nf_proto_net *pn = &gn->pn;
144 gn->timeout = nf_ct_generic_timeout;
146 return generic_kmemdup_sysctl_table(pn, gn);
149 static struct nf_proto_net *generic_get_net_proto(struct net *net)
151 return &net->ct.nf_ct_proto.generic.pn;
154 const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic =
157 .pkt_to_tuple = generic_pkt_to_tuple,
158 .packet = generic_packet,
159 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
161 .nlattr_to_obj = generic_timeout_nlattr_to_obj,
162 .obj_to_nlattr = generic_timeout_obj_to_nlattr,
163 .nlattr_max = CTA_TIMEOUT_GENERIC_MAX,
164 .obj_size = sizeof(unsigned int),
165 .nla_policy = generic_timeout_nla_policy,
167 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
168 .init_net = generic_init_net,
169 .get_net_proto = generic_get_net_proto,