[NETFILTER]: nf_conntrack/nf_nat: fix incorrect config ifdefs
[linux-2.6.git] / net / netfilter / nf_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  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9  *      - enable working with Layer 3 protocol independent connection tracking.
10  *
11  * Derived from net/ipv4/netfilter/ip_conntrack_proto_udp.c
12  */
13
14 #include <linux/types.h>
15 #include <linux/timer.h>
16 #include <linux/module.h>
17 #include <linux/netfilter.h>
18 #include <linux/udp.h>
19 #include <linux/seq_file.h>
20 #include <linux/skbuff.h>
21 #include <linux/ipv6.h>
22 #include <net/ip6_checksum.h>
23 #include <net/checksum.h>
24
25 #include <linux/netfilter.h>
26 #include <linux/netfilter_ipv4.h>
27 #include <linux/netfilter_ipv6.h>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_conntrack_ecache.h>
30
31 static unsigned int nf_ct_udp_timeout __read_mostly = 30*HZ;
32 static unsigned int nf_ct_udp_timeout_stream __read_mostly = 180*HZ;
33
34 static int udp_pkt_to_tuple(const struct sk_buff *skb,
35                              unsigned int dataoff,
36                              struct nf_conntrack_tuple *tuple)
37 {
38         struct udphdr _hdr, *hp;
39
40         /* Actually only need first 8 bytes. */
41         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
42         if (hp == NULL)
43                 return 0;
44
45         tuple->src.u.udp.port = hp->source;
46         tuple->dst.u.udp.port = hp->dest;
47
48         return 1;
49 }
50
51 static int udp_invert_tuple(struct nf_conntrack_tuple *tuple,
52                             const struct nf_conntrack_tuple *orig)
53 {
54         tuple->src.u.udp.port = orig->dst.u.udp.port;
55         tuple->dst.u.udp.port = orig->src.u.udp.port;
56         return 1;
57 }
58
59 /* Print out the per-protocol part of the tuple. */
60 static int udp_print_tuple(struct seq_file *s,
61                            const struct nf_conntrack_tuple *tuple)
62 {
63         return seq_printf(s, "sport=%hu dport=%hu ",
64                           ntohs(tuple->src.u.udp.port),
65                           ntohs(tuple->dst.u.udp.port));
66 }
67
68 /* Print out the private part of the conntrack. */
69 static int udp_print_conntrack(struct seq_file *s,
70                                const struct nf_conn *conntrack)
71 {
72         return 0;
73 }
74
75 /* Returns verdict for packet, and may modify conntracktype */
76 static int udp_packet(struct nf_conn *conntrack,
77                       const struct sk_buff *skb,
78                       unsigned int dataoff,
79                       enum ip_conntrack_info ctinfo,
80                       int pf,
81                       unsigned int hooknum)
82 {
83         /* If we've seen traffic both ways, this is some kind of UDP
84            stream.  Extend timeout. */
85         if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
86                 nf_ct_refresh_acct(conntrack, ctinfo, skb,
87                                    nf_ct_udp_timeout_stream);
88                 /* Also, more likely to be important, and not a probe */
89                 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
90                         nf_conntrack_event_cache(IPCT_STATUS, skb);
91         } else
92                 nf_ct_refresh_acct(conntrack, ctinfo, skb, nf_ct_udp_timeout);
93
94         return NF_ACCEPT;
95 }
96
97 /* Called when a new connection for this protocol found. */
98 static int udp_new(struct nf_conn *conntrack, const struct sk_buff *skb,
99                    unsigned int dataoff)
100 {
101         return 1;
102 }
103
104 static int udp_error(struct sk_buff *skb, unsigned int dataoff,
105                      enum ip_conntrack_info *ctinfo,
106                      int pf,
107                      unsigned int hooknum)
108 {
109         unsigned int udplen = skb->len - dataoff;
110         struct udphdr _hdr, *hdr;
111
112         /* Header is too small? */
113         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
114         if (hdr == NULL) {
115                 if (LOG_INVALID(IPPROTO_UDP))
116                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
117                                       "nf_ct_udp: short packet ");
118                 return -NF_ACCEPT;
119         }
120
121         /* Truncated/malformed packets */
122         if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
123                 if (LOG_INVALID(IPPROTO_UDP))
124                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
125                                 "nf_ct_udp: truncated/malformed packet ");
126                 return -NF_ACCEPT;
127         }
128
129         /* Packet with no checksum */
130         if (!hdr->check)
131                 return NF_ACCEPT;
132
133         /* Checksum invalid? Ignore.
134          * We skip checking packets on the outgoing path
135          * because the checksum is assumed to be correct.
136          * FIXME: Source route IP option packets --RR */
137         if (nf_conntrack_checksum &&
138             ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) ||
139              (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) &&
140             nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
141                 if (LOG_INVALID(IPPROTO_UDP))
142                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
143                                 "nf_ct_udp: bad UDP checksum ");
144                 return -NF_ACCEPT;
145         }
146
147         return NF_ACCEPT;
148 }
149
150 #ifdef CONFIG_SYSCTL
151 static unsigned int udp_sysctl_table_users;
152 static struct ctl_table_header *udp_sysctl_header;
153 static struct ctl_table udp_sysctl_table[] = {
154         {
155                 .ctl_name       = NET_NF_CONNTRACK_UDP_TIMEOUT,
156                 .procname       = "nf_conntrack_udp_timeout",
157                 .data           = &nf_ct_udp_timeout,
158                 .maxlen         = sizeof(unsigned int),
159                 .mode           = 0644,
160                 .proc_handler   = &proc_dointvec_jiffies,
161         },
162         {
163                 .ctl_name       = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
164                 .procname       = "nf_conntrack_udp_timeout_stream",
165                 .data           = &nf_ct_udp_timeout_stream,
166                 .maxlen         = sizeof(unsigned int),
167                 .mode           = 0644,
168                 .proc_handler   = &proc_dointvec_jiffies,
169         },
170         {
171                 .ctl_name       = 0
172         }
173 };
174 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
175 static struct ctl_table udp_compat_sysctl_table[] = {
176         {
177                 .ctl_name       = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
178                 .procname       = "ip_conntrack_udp_timeout",
179                 .data           = &nf_ct_udp_timeout,
180                 .maxlen         = sizeof(unsigned int),
181                 .mode           = 0644,
182                 .proc_handler   = &proc_dointvec_jiffies,
183         },
184         {
185                 .ctl_name       = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
186                 .procname       = "ip_conntrack_udp_timeout_stream",
187                 .data           = &nf_ct_udp_timeout_stream,
188                 .maxlen         = sizeof(unsigned int),
189                 .mode           = 0644,
190                 .proc_handler   = &proc_dointvec_jiffies,
191         },
192         {
193                 .ctl_name       = 0
194         }
195 };
196 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
197 #endif /* CONFIG_SYSCTL */
198
199 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 =
200 {
201         .l3proto                = PF_INET,
202         .l4proto                = IPPROTO_UDP,
203         .name                   = "udp",
204         .pkt_to_tuple           = udp_pkt_to_tuple,
205         .invert_tuple           = udp_invert_tuple,
206         .print_tuple            = udp_print_tuple,
207         .print_conntrack        = udp_print_conntrack,
208         .packet                 = udp_packet,
209         .new                    = udp_new,
210         .error                  = udp_error,
211 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
212         .tuple_to_nfattr        = nf_ct_port_tuple_to_nfattr,
213         .nfattr_to_tuple        = nf_ct_port_nfattr_to_tuple,
214 #endif
215 #ifdef CONFIG_SYSCTL
216         .ctl_table_users        = &udp_sysctl_table_users,
217         .ctl_table_header       = &udp_sysctl_header,
218         .ctl_table              = udp_sysctl_table,
219 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
220         .ctl_compat_table       = udp_compat_sysctl_table,
221 #endif
222 #endif
223 };
224 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
225
226 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 =
227 {
228         .l3proto                = PF_INET6,
229         .l4proto                = IPPROTO_UDP,
230         .name                   = "udp",
231         .pkt_to_tuple           = udp_pkt_to_tuple,
232         .invert_tuple           = udp_invert_tuple,
233         .print_tuple            = udp_print_tuple,
234         .print_conntrack        = udp_print_conntrack,
235         .packet                 = udp_packet,
236         .new                    = udp_new,
237         .error                  = udp_error,
238 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
239         .tuple_to_nfattr        = nf_ct_port_tuple_to_nfattr,
240         .nfattr_to_tuple        = nf_ct_port_nfattr_to_tuple,
241 #endif
242 #ifdef CONFIG_SYSCTL
243         .ctl_table_users        = &udp_sysctl_table_users,
244         .ctl_table_header       = &udp_sysctl_header,
245         .ctl_table              = udp_sysctl_table,
246 #endif
247 };
248 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);