1 /* (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.
7 */
8
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>
15
16 static const unsigned int nf_ct_generic_timeout = 600*HZ;
17
nf_generic_should_process(u8 proto)18 static bool nf_generic_should_process(u8 proto)
19 {
20 switch (proto) {
21 #ifdef CONFIG_NF_CT_PROTO_GRE_MODULE
22 case IPPROTO_GRE:
23 return false;
24 #endif
25 default:
26 return true;
27 }
28 }
29
generic_pernet(struct net * net)30 static inline struct nf_generic_net *generic_pernet(struct net *net)
31 {
32 return &net->ct.nf_ct_proto.generic;
33 }
34
generic_pkt_to_tuple(const struct sk_buff * skb,unsigned int dataoff,struct net * net,struct nf_conntrack_tuple * tuple)35 static bool generic_pkt_to_tuple(const struct sk_buff *skb,
36 unsigned int dataoff,
37 struct net *net, struct nf_conntrack_tuple *tuple)
38 {
39 tuple->src.u.all = 0;
40 tuple->dst.u.all = 0;
41
42 return true;
43 }
44
45 /* Returns verdict for packet, or -1 for invalid. */
generic_packet(struct nf_conn * ct,const struct sk_buff * skb,unsigned int dataoff,enum ip_conntrack_info ctinfo)46 static int generic_packet(struct nf_conn *ct,
47 const struct sk_buff *skb,
48 unsigned int dataoff,
49 enum ip_conntrack_info ctinfo)
50 {
51 const unsigned int *timeout = nf_ct_timeout_lookup(ct);
52
53 if (!timeout)
54 timeout = &generic_pernet(nf_ct_net(ct))->timeout;
55
56 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
57 return NF_ACCEPT;
58 }
59
60 /* Called when a new connection for this protocol found. */
generic_new(struct nf_conn * ct,const struct sk_buff * skb,unsigned int dataoff)61 static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb,
62 unsigned int dataoff)
63 {
64 bool ret;
65
66 ret = nf_generic_should_process(nf_ct_protonum(ct));
67 if (!ret)
68 pr_warn_once("conntrack: generic helper won't handle protocol %d. Please consider loading the specific helper module.\n",
69 nf_ct_protonum(ct));
70 return ret;
71 }
72
73 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
74
75 #include <linux/netfilter/nfnetlink.h>
76 #include <linux/netfilter/nfnetlink_cttimeout.h>
77
generic_timeout_nlattr_to_obj(struct nlattr * tb[],struct net * net,void * data)78 static int generic_timeout_nlattr_to_obj(struct nlattr *tb[],
79 struct net *net, void *data)
80 {
81 struct nf_generic_net *gn = generic_pernet(net);
82 unsigned int *timeout = data;
83
84 if (!timeout)
85 timeout = &gn->timeout;
86
87 if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
88 *timeout =
89 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ;
90 else {
91 /* Set default generic timeout. */
92 *timeout = gn->timeout;
93 }
94
95 return 0;
96 }
97
98 static int
generic_timeout_obj_to_nlattr(struct sk_buff * skb,const void * data)99 generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
100 {
101 const unsigned int *timeout = data;
102
103 if (nla_put_be32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ)))
104 goto nla_put_failure;
105
106 return 0;
107
108 nla_put_failure:
109 return -ENOSPC;
110 }
111
112 static const struct nla_policy
113 generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = {
114 [CTA_TIMEOUT_GENERIC_TIMEOUT] = { .type = NLA_U32 },
115 };
116 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
117
118 #ifdef CONFIG_SYSCTL
119 static struct ctl_table generic_sysctl_table[] = {
120 {
121 .procname = "nf_conntrack_generic_timeout",
122 .maxlen = sizeof(unsigned int),
123 .mode = 0644,
124 .proc_handler = proc_dointvec_jiffies,
125 },
126 { }
127 };
128 #endif /* CONFIG_SYSCTL */
129
generic_kmemdup_sysctl_table(struct nf_proto_net * pn,struct nf_generic_net * gn)130 static int generic_kmemdup_sysctl_table(struct nf_proto_net *pn,
131 struct nf_generic_net *gn)
132 {
133 #ifdef CONFIG_SYSCTL
134 pn->ctl_table = kmemdup(generic_sysctl_table,
135 sizeof(generic_sysctl_table),
136 GFP_KERNEL);
137 if (!pn->ctl_table)
138 return -ENOMEM;
139
140 pn->ctl_table[0].data = &gn->timeout;
141 #endif
142 return 0;
143 }
144
generic_init_net(struct net * net,u_int16_t proto)145 static int generic_init_net(struct net *net, u_int16_t proto)
146 {
147 struct nf_generic_net *gn = generic_pernet(net);
148 struct nf_proto_net *pn = &gn->pn;
149
150 gn->timeout = nf_ct_generic_timeout;
151
152 return generic_kmemdup_sysctl_table(pn, gn);
153 }
154
generic_get_net_proto(struct net * net)155 static struct nf_proto_net *generic_get_net_proto(struct net *net)
156 {
157 return &net->ct.nf_ct_proto.generic.pn;
158 }
159
160 const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic =
161 {
162 .l3proto = PF_UNSPEC,
163 .l4proto = 255,
164 .pkt_to_tuple = generic_pkt_to_tuple,
165 .packet = generic_packet,
166 .new = generic_new,
167 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
168 .ctnl_timeout = {
169 .nlattr_to_obj = generic_timeout_nlattr_to_obj,
170 .obj_to_nlattr = generic_timeout_obj_to_nlattr,
171 .nlattr_max = CTA_TIMEOUT_GENERIC_MAX,
172 .obj_size = sizeof(unsigned int),
173 .nla_policy = generic_timeout_nla_policy,
174 },
175 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
176 .init_net = generic_init_net,
177 .get_net_proto = generic_get_net_proto,
178 };
179