ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv4 / netfilter / ip_conntrack_proto_udp.c
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/sched.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
13 #include <linux/in.h>
14 #include <linux/udp.h>
15 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
16
17 unsigned long ip_ct_udp_timeout = 30*HZ;
18 unsigned long ip_ct_udp_timeout_stream = 180*HZ;
19
20 static int udp_pkt_to_tuple(const struct sk_buff *skb,
21                              unsigned int dataoff,
22                              struct ip_conntrack_tuple *tuple)
23 {
24         struct udphdr hdr;
25
26         /* Actually only need first 8 bytes. */
27         if (skb_copy_bits(skb, dataoff, &hdr, 8) != 0)
28                 return 0;
29
30         tuple->src.u.udp.port = hdr.source;
31         tuple->dst.u.udp.port = hdr.dest;
32
33         return 1;
34 }
35
36 static int udp_invert_tuple(struct ip_conntrack_tuple *tuple,
37                             const struct ip_conntrack_tuple *orig)
38 {
39         tuple->src.u.udp.port = orig->dst.u.udp.port;
40         tuple->dst.u.udp.port = orig->src.u.udp.port;
41         return 1;
42 }
43
44 /* Print out the per-protocol part of the tuple. */
45 static unsigned int udp_print_tuple(char *buffer,
46                                     const struct ip_conntrack_tuple *tuple)
47 {
48         return sprintf(buffer, "sport=%hu dport=%hu ",
49                        ntohs(tuple->src.u.udp.port),
50                        ntohs(tuple->dst.u.udp.port));
51 }
52
53 /* Print out the private part of the conntrack. */
54 static unsigned int udp_print_conntrack(char *buffer,
55                                         const struct ip_conntrack *conntrack)
56 {
57         return 0;
58 }
59
60 /* Returns verdict for packet, and may modify conntracktype */
61 static int udp_packet(struct ip_conntrack *conntrack,
62                       const struct sk_buff *skb,
63                       enum ip_conntrack_info conntrackinfo)
64 {
65         /* If we've seen traffic both ways, this is some kind of UDP
66            stream.  Extend timeout. */
67         if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
68                 ip_ct_refresh(conntrack, ip_ct_udp_timeout_stream);
69                 /* Also, more likely to be important, and not a probe */
70                 set_bit(IPS_ASSURED_BIT, &conntrack->status);
71         } else
72                 ip_ct_refresh(conntrack, ip_ct_udp_timeout);
73
74         return NF_ACCEPT;
75 }
76
77 /* Called when a new connection for this protocol found. */
78 static int udp_new(struct ip_conntrack *conntrack, const struct sk_buff *skb)
79 {
80         return 1;
81 }
82
83 struct ip_conntrack_protocol ip_conntrack_protocol_udp
84 = { { NULL, NULL }, IPPROTO_UDP, "udp",
85     udp_pkt_to_tuple, udp_invert_tuple, udp_print_tuple, udp_print_conntrack,
86     udp_packet, udp_new, NULL, NULL, NULL };