netfilter: nf_ct_tcp: merge tcpv[4,6]_net_init into tcp_net_init
[linux-2.6/btrfs-unstable.git] / net / netfilter / nf_conntrack_proto_udp.c
blob2b978e6fd1c25d6c797408633c132c1f29fa33f6
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.
7 */
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/module.h>
12 #include <linux/udp.h>
13 #include <linux/seq_file.h>
14 #include <linux/skbuff.h>
15 #include <linux/ipv6.h>
16 #include <net/ip6_checksum.h>
17 #include <net/checksum.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/netfilter_ipv6.h>
22 #include <net/netfilter/nf_conntrack_l4proto.h>
23 #include <net/netfilter/nf_conntrack_ecache.h>
24 #include <net/netfilter/nf_log.h>
25 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
26 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
28 static unsigned int udp_timeouts[UDP_CT_MAX] = {
29 [UDP_CT_UNREPLIED] = 30*HZ,
30 [UDP_CT_REPLIED] = 180*HZ,
33 static inline struct nf_udp_net *udp_pernet(struct net *net)
35 return &net->ct.nf_ct_proto.udp;
38 static bool udp_pkt_to_tuple(const struct sk_buff *skb,
39 unsigned int dataoff,
40 struct nf_conntrack_tuple *tuple)
42 const struct udphdr *hp;
43 struct udphdr _hdr;
45 /* Actually only need first 8 bytes. */
46 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
47 if (hp == NULL)
48 return false;
50 tuple->src.u.udp.port = hp->source;
51 tuple->dst.u.udp.port = hp->dest;
53 return true;
56 static bool udp_invert_tuple(struct nf_conntrack_tuple *tuple,
57 const struct nf_conntrack_tuple *orig)
59 tuple->src.u.udp.port = orig->dst.u.udp.port;
60 tuple->dst.u.udp.port = orig->src.u.udp.port;
61 return true;
64 /* Print out the per-protocol part of the tuple. */
65 static int udp_print_tuple(struct seq_file *s,
66 const struct nf_conntrack_tuple *tuple)
68 return seq_printf(s, "sport=%hu dport=%hu ",
69 ntohs(tuple->src.u.udp.port),
70 ntohs(tuple->dst.u.udp.port));
73 static unsigned int *udp_get_timeouts(struct net *net)
75 return udp_pernet(net)->timeouts;
78 /* Returns verdict for packet, and may modify conntracktype */
79 static int udp_packet(struct nf_conn *ct,
80 const struct sk_buff *skb,
81 unsigned int dataoff,
82 enum ip_conntrack_info ctinfo,
83 u_int8_t pf,
84 unsigned int hooknum,
85 unsigned int *timeouts)
87 /* If we've seen traffic both ways, this is some kind of UDP
88 stream. Extend timeout. */
89 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
90 nf_ct_refresh_acct(ct, ctinfo, skb,
91 timeouts[UDP_CT_REPLIED]);
92 /* Also, more likely to be important, and not a probe */
93 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
94 nf_conntrack_event_cache(IPCT_ASSURED, ct);
95 } else {
96 nf_ct_refresh_acct(ct, ctinfo, skb,
97 timeouts[UDP_CT_UNREPLIED]);
99 return NF_ACCEPT;
102 /* Called when a new connection for this protocol found. */
103 static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb,
104 unsigned int dataoff, unsigned int *timeouts)
106 return true;
109 static int udp_error(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
110 unsigned int dataoff, enum ip_conntrack_info *ctinfo,
111 u_int8_t pf,
112 unsigned int hooknum)
114 unsigned int udplen = skb->len - dataoff;
115 const struct udphdr *hdr;
116 struct udphdr _hdr;
118 /* Header is too small? */
119 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
120 if (hdr == NULL) {
121 if (LOG_INVALID(net, IPPROTO_UDP))
122 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
123 "nf_ct_udp: short packet ");
124 return -NF_ACCEPT;
127 /* Truncated/malformed packets */
128 if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
129 if (LOG_INVALID(net, IPPROTO_UDP))
130 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
131 "nf_ct_udp: truncated/malformed packet ");
132 return -NF_ACCEPT;
135 /* Packet with no checksum */
136 if (!hdr->check)
137 return NF_ACCEPT;
139 /* Checksum invalid? Ignore.
140 * We skip checking packets on the outgoing path
141 * because the checksum is assumed to be correct.
142 * FIXME: Source route IP option packets --RR */
143 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
144 nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
145 if (LOG_INVALID(net, IPPROTO_UDP))
146 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
147 "nf_ct_udp: bad UDP checksum ");
148 return -NF_ACCEPT;
151 return NF_ACCEPT;
154 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
156 #include <linux/netfilter/nfnetlink.h>
157 #include <linux/netfilter/nfnetlink_cttimeout.h>
159 static int udp_timeout_nlattr_to_obj(struct nlattr *tb[],
160 struct net *net, void *data)
162 unsigned int *timeouts = data;
163 struct nf_udp_net *un = udp_pernet(net);
165 /* set default timeouts for UDP. */
166 timeouts[UDP_CT_UNREPLIED] = un->timeouts[UDP_CT_UNREPLIED];
167 timeouts[UDP_CT_REPLIED] = un->timeouts[UDP_CT_REPLIED];
169 if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) {
170 timeouts[UDP_CT_UNREPLIED] =
171 ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ;
173 if (tb[CTA_TIMEOUT_UDP_REPLIED]) {
174 timeouts[UDP_CT_REPLIED] =
175 ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ;
177 return 0;
180 static int
181 udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
183 const unsigned int *timeouts = data;
185 if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED,
186 htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) ||
187 nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED,
188 htonl(timeouts[UDP_CT_REPLIED] / HZ)))
189 goto nla_put_failure;
190 return 0;
192 nla_put_failure:
193 return -ENOSPC;
196 static const struct nla_policy
197 udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = {
198 [CTA_TIMEOUT_UDP_UNREPLIED] = { .type = NLA_U32 },
199 [CTA_TIMEOUT_UDP_REPLIED] = { .type = NLA_U32 },
201 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
203 #ifdef CONFIG_SYSCTL
204 static struct ctl_table udp_sysctl_table[] = {
206 .procname = "nf_conntrack_udp_timeout",
207 .maxlen = sizeof(unsigned int),
208 .mode = 0644,
209 .proc_handler = proc_dointvec_jiffies,
212 .procname = "nf_conntrack_udp_timeout_stream",
213 .maxlen = sizeof(unsigned int),
214 .mode = 0644,
215 .proc_handler = proc_dointvec_jiffies,
219 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
220 static struct ctl_table udp_compat_sysctl_table[] = {
222 .procname = "ip_conntrack_udp_timeout",
223 .maxlen = sizeof(unsigned int),
224 .mode = 0644,
225 .proc_handler = proc_dointvec_jiffies,
228 .procname = "ip_conntrack_udp_timeout_stream",
229 .maxlen = sizeof(unsigned int),
230 .mode = 0644,
231 .proc_handler = proc_dointvec_jiffies,
235 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
236 #endif /* CONFIG_SYSCTL */
238 static int udp_kmemdup_sysctl_table(struct nf_proto_net *pn)
240 #ifdef CONFIG_SYSCTL
241 struct nf_udp_net *un = (struct nf_udp_net *)pn;
242 if (pn->ctl_table)
243 return 0;
244 pn->ctl_table = kmemdup(udp_sysctl_table,
245 sizeof(udp_sysctl_table),
246 GFP_KERNEL);
247 if (!pn->ctl_table)
248 return -ENOMEM;
249 pn->ctl_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
250 pn->ctl_table[1].data = &un->timeouts[UDP_CT_REPLIED];
251 #endif
252 return 0;
255 static int udp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn)
257 #ifdef CONFIG_SYSCTL
258 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
259 struct nf_udp_net *un = (struct nf_udp_net *)pn;
260 pn->ctl_compat_table = kmemdup(udp_compat_sysctl_table,
261 sizeof(udp_compat_sysctl_table),
262 GFP_KERNEL);
263 if (!pn->ctl_compat_table)
264 return -ENOMEM;
266 pn->ctl_compat_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
267 pn->ctl_compat_table[1].data = &un->timeouts[UDP_CT_REPLIED];
268 #endif
269 #endif
270 return 0;
273 static void udp_init_net_data(struct nf_udp_net *un)
275 int i;
276 #ifdef CONFIG_SYSCTL
277 if (!un->pn.ctl_table) {
278 #else
279 if (!un->pn.users++) {
280 #endif
281 for (i = 0; i < UDP_CT_MAX; i++)
282 un->timeouts[i] = udp_timeouts[i];
286 static int udpv4_init_net(struct net *net, u_int16_t proto)
288 int ret;
289 struct nf_udp_net *un = udp_pernet(net);
290 struct nf_proto_net *pn = (struct nf_proto_net *)un;
292 udp_init_net_data(un);
294 ret = udp_kmemdup_compat_sysctl_table(pn);
295 if (ret < 0)
296 return ret;
298 ret = udp_kmemdup_sysctl_table(pn);
299 #ifdef CONFIG_SYSCTL
300 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
301 if (ret < 0) {
302 kfree(pn->ctl_compat_table);
303 pn->ctl_compat_table = NULL;
305 #endif
306 #endif
307 return ret;
310 static int udpv6_init_net(struct net *net, u_int16_t proto)
312 struct nf_udp_net *un = udp_pernet(net);
313 struct nf_proto_net *pn = (struct nf_proto_net *)un;
315 udp_init_net_data(un);
316 return udp_kmemdup_sysctl_table(pn);
319 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
321 .l3proto = PF_INET,
322 .l4proto = IPPROTO_UDP,
323 .name = "udp",
324 .pkt_to_tuple = udp_pkt_to_tuple,
325 .invert_tuple = udp_invert_tuple,
326 .print_tuple = udp_print_tuple,
327 .packet = udp_packet,
328 .get_timeouts = udp_get_timeouts,
329 .new = udp_new,
330 .error = udp_error,
331 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
332 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
333 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
334 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
335 .nla_policy = nf_ct_port_nla_policy,
336 #endif
337 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
338 .ctnl_timeout = {
339 .nlattr_to_obj = udp_timeout_nlattr_to_obj,
340 .obj_to_nlattr = udp_timeout_obj_to_nlattr,
341 .nlattr_max = CTA_TIMEOUT_UDP_MAX,
342 .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
343 .nla_policy = udp_timeout_nla_policy,
345 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
346 .init_net = udpv4_init_net,
348 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
350 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
352 .l3proto = PF_INET6,
353 .l4proto = IPPROTO_UDP,
354 .name = "udp",
355 .pkt_to_tuple = udp_pkt_to_tuple,
356 .invert_tuple = udp_invert_tuple,
357 .print_tuple = udp_print_tuple,
358 .packet = udp_packet,
359 .get_timeouts = udp_get_timeouts,
360 .new = udp_new,
361 .error = udp_error,
362 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
363 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
364 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
365 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
366 .nla_policy = nf_ct_port_nla_policy,
367 #endif
368 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
369 .ctnl_timeout = {
370 .nlattr_to_obj = udp_timeout_nlattr_to_obj,
371 .obj_to_nlattr = udp_timeout_obj_to_nlattr,
372 .nlattr_max = CTA_TIMEOUT_UDP_MAX,
373 .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
374 .nla_policy = udp_timeout_nla_policy,
376 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
377 .init_net = udpv6_init_net,
379 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);