blob: 60865f1103099383c4263a1a56e691b3c86c3720 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
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.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 */
8
9#include <linux/types.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080010#include <linux/jiffies.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080011#include <linux/timer.h>
12#include <linux/netfilter.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010013#include <net/netfilter/nf_conntrack_l4proto.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080014
Patrick McHardy933a41e2006-11-29 02:35:18 +010015static unsigned int nf_ct_generic_timeout __read_mostly = 600*HZ;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080016
Florian Westphaldb29a952014-09-26 11:35:42 +020017static bool nf_generic_should_process(u8 proto)
18{
19 switch (proto) {
20#ifdef CONFIG_NF_CT_PROTO_SCTP_MODULE
21 case IPPROTO_SCTP:
22 return false;
23#endif
24#ifdef CONFIG_NF_CT_PROTO_DCCP_MODULE
25 case IPPROTO_DCCP:
26 return false;
27#endif
28#ifdef CONFIG_NF_CT_PROTO_GRE_MODULE
29 case IPPROTO_GRE:
30 return false;
31#endif
32#ifdef CONFIG_NF_CT_PROTO_UDPLITE_MODULE
33 case IPPROTO_UDPLITE:
34 return false;
35#endif
36 default:
37 return true;
38 }
39}
40
Gao feng15f585b2012-05-28 21:04:11 +000041static inline struct nf_generic_net *generic_pernet(struct net *net)
42{
43 return &net->ct.nf_ct_proto.generic;
44}
45
Jan Engelhardt09f263c2008-04-14 11:15:53 +020046static bool generic_pkt_to_tuple(const struct sk_buff *skb,
47 unsigned int dataoff,
48 struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049{
50 tuple->src.u.all = 0;
51 tuple->dst.u.all = 0;
52
Jan Engelhardt09f263c2008-04-14 11:15:53 +020053 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054}
55
Jan Engelhardt09f263c2008-04-14 11:15:53 +020056static bool generic_invert_tuple(struct nf_conntrack_tuple *tuple,
57 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080058{
59 tuple->src.u.all = 0;
60 tuple->dst.u.all = 0;
61
Jan Engelhardt09f263c2008-04-14 11:15:53 +020062 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063}
64
65/* Print out the per-protocol part of the tuple. */
Joe Perches824f1fb2014-09-29 16:08:22 -070066static void generic_print_tuple(struct seq_file *s,
67 const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080069}
70
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010071static unsigned int *generic_get_timeouts(struct net *net)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080072{
Gao feng15f585b2012-05-28 21:04:11 +000073 return &(generic_pernet(net)->timeout);
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010074}
75
76/* Returns verdict for packet, or -1 for invalid. */
77static int generic_packet(struct nf_conn *ct,
78 const struct sk_buff *skb,
79 unsigned int dataoff,
80 enum ip_conntrack_info ctinfo,
81 u_int8_t pf,
82 unsigned int hooknum,
83 unsigned int *timeout)
84{
85 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080086 return NF_ACCEPT;
87}
88
89/* Called when a new connection for this protocol found. */
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010090static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb,
91 unsigned int dataoff, unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092{
Florian Westphaldb29a952014-09-26 11:35:42 +020093 return nf_generic_should_process(nf_ct_protonum(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080094}
95
Pablo Neira Ayuso50978462012-02-28 19:13:48 +010096#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
97
98#include <linux/netfilter/nfnetlink.h>
99#include <linux/netfilter/nfnetlink_cttimeout.h>
100
Gao feng8264deb2012-05-28 21:04:23 +0000101static int generic_timeout_nlattr_to_obj(struct nlattr *tb[],
102 struct net *net, void *data)
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100103{
104 unsigned int *timeout = data;
Gao feng8264deb2012-05-28 21:04:23 +0000105 struct nf_generic_net *gn = generic_pernet(net);
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100106
107 if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
108 *timeout =
109 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ;
110 else {
111 /* Set default generic timeout. */
Gao feng8264deb2012-05-28 21:04:23 +0000112 *timeout = gn->timeout;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100113 }
114
115 return 0;
116}
117
118static int
119generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
120{
121 const unsigned int *timeout = data;
122
David S. Millerf5776942012-04-01 18:52:31 -0400123 if (nla_put_be32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ)))
124 goto nla_put_failure;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100125
126 return 0;
127
128nla_put_failure:
129 return -ENOSPC;
130}
131
132static const struct nla_policy
133generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = {
134 [CTA_TIMEOUT_GENERIC_TIMEOUT] = { .type = NLA_U32 },
135};
136#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
137
Patrick McHardy933a41e2006-11-29 02:35:18 +0100138#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100139static struct ctl_table generic_sysctl_table[] = {
140 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100141 .procname = "nf_conntrack_generic_timeout",
Patrick McHardy933a41e2006-11-29 02:35:18 +0100142 .maxlen = sizeof(unsigned int),
143 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800144 .proc_handler = proc_dointvec_jiffies,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100145 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800146 { }
Patrick McHardy933a41e2006-11-29 02:35:18 +0100147};
Patrick McHardya999e682006-11-29 02:35:20 +0100148#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
149static struct ctl_table generic_compat_sysctl_table[] = {
150 {
Patrick McHardya999e682006-11-29 02:35:20 +0100151 .procname = "ip_conntrack_generic_timeout",
Patrick McHardya999e682006-11-29 02:35:20 +0100152 .maxlen = sizeof(unsigned int),
153 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800154 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100155 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800156 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100157};
158#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100159#endif /* CONFIG_SYSCTL */
160
Gao feng22ac0372012-06-21 04:36:47 +0000161static int generic_kmemdup_sysctl_table(struct nf_proto_net *pn,
162 struct nf_generic_net *gn)
Gao feng15f585b2012-05-28 21:04:11 +0000163{
Gao feng15f585b2012-05-28 21:04:11 +0000164#ifdef CONFIG_SYSCTL
165 pn->ctl_table = kmemdup(generic_sysctl_table,
166 sizeof(generic_sysctl_table),
167 GFP_KERNEL);
168 if (!pn->ctl_table)
169 return -ENOMEM;
Gao feng15f585b2012-05-28 21:04:11 +0000170
Gao feng22ac0372012-06-21 04:36:47 +0000171 pn->ctl_table[0].data = &gn->timeout;
172#endif
173 return 0;
174}
175
176static int generic_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
177 struct nf_generic_net *gn)
178{
179#ifdef CONFIG_SYSCTL
Gao feng15f585b2012-05-28 21:04:11 +0000180#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
181 pn->ctl_compat_table = kmemdup(generic_compat_sysctl_table,
182 sizeof(generic_compat_sysctl_table),
183 GFP_KERNEL);
Gao feng22ac0372012-06-21 04:36:47 +0000184 if (!pn->ctl_compat_table)
Gao feng15f585b2012-05-28 21:04:11 +0000185 return -ENOMEM;
Gao feng22ac0372012-06-21 04:36:47 +0000186
Gao feng15f585b2012-05-28 21:04:11 +0000187 pn->ctl_compat_table[0].data = &gn->timeout;
188#endif
189#endif
190 return 0;
191}
192
Gao feng22ac0372012-06-21 04:36:47 +0000193static int generic_init_net(struct net *net, u_int16_t proto)
194{
195 int ret;
196 struct nf_generic_net *gn = generic_pernet(net);
197 struct nf_proto_net *pn = &gn->pn;
198
199 gn->timeout = nf_ct_generic_timeout;
200
201 ret = generic_kmemdup_compat_sysctl_table(pn, gn);
202 if (ret < 0)
203 return ret;
204
205 ret = generic_kmemdup_sysctl_table(pn, gn);
206 if (ret < 0)
207 nf_ct_kfree_compat_sysctl_table(pn);
208
209 return ret;
210}
211
Pablo Neira Ayuso08911472012-06-29 05:23:24 +0000212static struct nf_proto_net *generic_get_net_proto(struct net *net)
213{
214 return &net->ct.nf_ct_proto.generic.pn;
215}
216
Patrick McHardy61075af2007-07-14 20:48:19 -0700217struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800218{
219 .l3proto = PF_UNSPEC,
Christoph Paaschfe2a7ce2009-02-18 16:28:35 +0100220 .l4proto = 255,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800221 .name = "unknown",
222 .pkt_to_tuple = generic_pkt_to_tuple,
223 .invert_tuple = generic_invert_tuple,
224 .print_tuple = generic_print_tuple,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100225 .packet = generic_packet,
226 .get_timeouts = generic_get_timeouts,
227 .new = generic_new,
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100228#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
229 .ctnl_timeout = {
230 .nlattr_to_obj = generic_timeout_nlattr_to_obj,
231 .obj_to_nlattr = generic_timeout_obj_to_nlattr,
232 .nlattr_max = CTA_TIMEOUT_GENERIC_MAX,
233 .obj_size = sizeof(unsigned int),
234 .nla_policy = generic_timeout_nla_policy,
235 },
236#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Gao feng15f585b2012-05-28 21:04:11 +0000237 .init_net = generic_init_net,
Pablo Neira Ayuso08911472012-06-29 05:23:24 +0000238 .get_net_proto = generic_get_net_proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239};