allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / netfilter / nf_conntrack_proto_udp.c
blob8ecff36b5b03da2f6e58b241f679c87082364cbc
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/netfilter.h>
13 #include <linux/udp.h>
14 #include <linux/seq_file.h>
15 #include <linux/skbuff.h>
16 #include <linux/ipv6.h>
17 #include <net/ip6_checksum.h>
18 #include <net/checksum.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter_ipv4.h>
22 #include <linux/netfilter_ipv6.h>
23 #include <net/netfilter/nf_conntrack_l4proto.h>
24 #include <net/netfilter/nf_conntrack_ecache.h>
26 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
27 extern int ipv4_conntrack_fastnat;
28 #endif
30 static unsigned int nf_ct_udp_timeout __read_mostly = 30*HZ;
31 static unsigned int nf_ct_udp_timeout_stream __read_mostly = 180*HZ;
33 static int udp_pkt_to_tuple(const struct sk_buff *skb,
34 unsigned int dataoff,
35 struct nf_conntrack_tuple *tuple)
37 struct udphdr _hdr, *hp;
39 /* Actually only need first 8 bytes. */
40 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
41 if (hp == NULL)
42 return 0;
44 tuple->src.u.udp.port = hp->source;
45 tuple->dst.u.udp.port = hp->dest;
47 return 1;
50 static int udp_invert_tuple(struct nf_conntrack_tuple *tuple,
51 const struct nf_conntrack_tuple *orig)
53 tuple->src.u.udp.port = orig->dst.u.udp.port;
54 tuple->dst.u.udp.port = orig->src.u.udp.port;
55 return 1;
58 /* Print out the per-protocol part of the tuple. */
59 static int udp_print_tuple(struct seq_file *s,
60 const struct nf_conntrack_tuple *tuple)
62 return seq_printf(s, "sport=%hu dport=%hu ",
63 ntohs(tuple->src.u.udp.port),
64 ntohs(tuple->dst.u.udp.port));
67 /* Returns verdict for packet, and may modify conntracktype */
68 static int udp_packet(struct nf_conn *conntrack,
69 const struct sk_buff *skb,
70 unsigned int dataoff,
71 enum ip_conntrack_info ctinfo,
72 int pf,
73 unsigned int hooknum)
75 /* If we've seen traffic both ways, this is some kind of UDP
76 stream. Extend timeout. */
77 if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
78 nf_ct_refresh_acct(conntrack, ctinfo, skb,
79 nf_ct_udp_timeout_stream);
80 /* Also, more likely to be important, and not a probe */
81 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
82 nf_conntrack_event_cache(IPCT_STATUS, skb);
83 } else
84 nf_ct_refresh_acct(conntrack, ctinfo, skb, nf_ct_udp_timeout);
86 return NF_ACCEPT;
89 /* Called when a new connection for this protocol found. */
90 static int udp_new(struct nf_conn *conntrack, const struct sk_buff *skb,
91 unsigned int dataoff)
93 return 1;
96 static int udp_error(struct sk_buff *skb, unsigned int dataoff,
97 enum ip_conntrack_info *ctinfo,
98 int pf,
99 unsigned int hooknum)
101 unsigned int udplen = skb->len - dataoff;
102 struct udphdr _hdr, *hdr;
104 /* Header is too small? */
105 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
106 if (hdr == NULL) {
107 if (LOG_INVALID(IPPROTO_UDP))
108 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
109 "nf_ct_udp: short packet ");
110 return -NF_ACCEPT;
113 /* Truncated/malformed packets */
114 if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
115 if (LOG_INVALID(IPPROTO_UDP))
116 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
117 "nf_ct_udp: truncated/malformed packet ");
118 return -NF_ACCEPT;
121 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
122 if (ipv4_conntrack_fastnat)
123 return NF_ACCEPT;
124 #endif
126 /* Packet with no checksum */
127 if (!hdr->check)
128 return NF_ACCEPT;
130 /* Checksum invalid? Ignore.
131 * We skip checking packets on the outgoing path
132 * because the checksum is assumed to be correct.
133 * FIXME: Source route IP option packets --RR */
134 if (nf_conntrack_checksum &&
135 ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) ||
136 (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) &&
137 nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
138 if (LOG_INVALID(IPPROTO_UDP))
139 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
140 "nf_ct_udp: bad UDP checksum ");
141 return -NF_ACCEPT;
144 return NF_ACCEPT;
147 #ifdef CONFIG_SYSCTL
148 static unsigned int udp_sysctl_table_users;
149 static struct ctl_table_header *udp_sysctl_header;
150 static struct ctl_table udp_sysctl_table[] = {
152 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT,
153 .procname = "nf_conntrack_udp_timeout",
154 .data = &nf_ct_udp_timeout,
155 .maxlen = sizeof(unsigned int),
156 .mode = 0644,
157 .proc_handler = &proc_dointvec_jiffies,
160 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
161 .procname = "nf_conntrack_udp_timeout_stream",
162 .data = &nf_ct_udp_timeout_stream,
163 .maxlen = sizeof(unsigned int),
164 .mode = 0644,
165 .proc_handler = &proc_dointvec_jiffies,
168 .ctl_name = 0
171 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
172 static struct ctl_table udp_compat_sysctl_table[] = {
174 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
175 .procname = "ip_conntrack_udp_timeout",
176 .data = &nf_ct_udp_timeout,
177 .maxlen = sizeof(unsigned int),
178 .mode = 0644,
179 .proc_handler = &proc_dointvec_jiffies,
182 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
183 .procname = "ip_conntrack_udp_timeout_stream",
184 .data = &nf_ct_udp_timeout_stream,
185 .maxlen = sizeof(unsigned int),
186 .mode = 0644,
187 .proc_handler = &proc_dointvec_jiffies,
190 .ctl_name = 0
193 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
194 #endif /* CONFIG_SYSCTL */
196 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 =
198 .l3proto = PF_INET,
199 .l4proto = IPPROTO_UDP,
200 .name = "udp",
201 .pkt_to_tuple = udp_pkt_to_tuple,
202 .invert_tuple = udp_invert_tuple,
203 .print_tuple = udp_print_tuple,
204 .packet = udp_packet,
205 .new = udp_new,
206 .error = udp_error,
207 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
208 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
209 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
210 #endif
211 #ifdef CONFIG_SYSCTL
212 .ctl_table_users = &udp_sysctl_table_users,
213 .ctl_table_header = &udp_sysctl_header,
214 .ctl_table = udp_sysctl_table,
215 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
216 .ctl_compat_table = udp_compat_sysctl_table,
217 #endif
218 #endif
220 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
222 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 =
224 .l3proto = PF_INET6,
225 .l4proto = IPPROTO_UDP,
226 .name = "udp",
227 .pkt_to_tuple = udp_pkt_to_tuple,
228 .invert_tuple = udp_invert_tuple,
229 .print_tuple = udp_print_tuple,
230 .packet = udp_packet,
231 .new = udp_new,
232 .error = udp_error,
233 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
234 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
235 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
236 #endif
237 #ifdef CONFIG_SYSCTL
238 .ctl_table_users = &udp_sysctl_table_users,
239 .ctl_table_header = &udp_sysctl_header,
240 .ctl_table = udp_sysctl_table,
241 #endif
243 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);