[DCCP]: Make code assumptions explicit
[linux-2.6/linux-loongson.git] / net / netfilter / nf_conntrack_proto_udp.c
blob570a2e109478809c04b1741cab1fb84b5484d218
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>
25 static unsigned int nf_ct_udp_timeout __read_mostly = 30*HZ;
26 static unsigned int nf_ct_udp_timeout_stream __read_mostly = 180*HZ;
28 static int udp_pkt_to_tuple(const struct sk_buff *skb,
29 unsigned int dataoff,
30 struct nf_conntrack_tuple *tuple)
32 struct udphdr _hdr, *hp;
34 /* Actually only need first 8 bytes. */
35 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
36 if (hp == NULL)
37 return 0;
39 tuple->src.u.udp.port = hp->source;
40 tuple->dst.u.udp.port = hp->dest;
42 return 1;
45 static int udp_invert_tuple(struct nf_conntrack_tuple *tuple,
46 const struct nf_conntrack_tuple *orig)
48 tuple->src.u.udp.port = orig->dst.u.udp.port;
49 tuple->dst.u.udp.port = orig->src.u.udp.port;
50 return 1;
53 /* Print out the per-protocol part of the tuple. */
54 static int udp_print_tuple(struct seq_file *s,
55 const struct nf_conntrack_tuple *tuple)
57 return seq_printf(s, "sport=%hu dport=%hu ",
58 ntohs(tuple->src.u.udp.port),
59 ntohs(tuple->dst.u.udp.port));
62 /* Print out the private part of the conntrack. */
63 static int udp_print_conntrack(struct seq_file *s,
64 const struct nf_conn *conntrack)
66 return 0;
69 /* Returns verdict for packet, and may modify conntracktype */
70 static int udp_packet(struct nf_conn *conntrack,
71 const struct sk_buff *skb,
72 unsigned int dataoff,
73 enum ip_conntrack_info ctinfo,
74 int pf,
75 unsigned int hooknum)
77 /* If we've seen traffic both ways, this is some kind of UDP
78 stream. Extend timeout. */
79 if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
80 nf_ct_refresh_acct(conntrack, ctinfo, skb,
81 nf_ct_udp_timeout_stream);
82 /* Also, more likely to be important, and not a probe */
83 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
84 nf_conntrack_event_cache(IPCT_STATUS, skb);
85 } else
86 nf_ct_refresh_acct(conntrack, ctinfo, skb, nf_ct_udp_timeout);
88 return NF_ACCEPT;
91 /* Called when a new connection for this protocol found. */
92 static int udp_new(struct nf_conn *conntrack, const struct sk_buff *skb,
93 unsigned int dataoff)
95 return 1;
98 static int udp_error(struct sk_buff *skb, unsigned int dataoff,
99 enum ip_conntrack_info *ctinfo,
100 int pf,
101 unsigned int hooknum)
103 unsigned int udplen = skb->len - dataoff;
104 struct udphdr _hdr, *hdr;
106 /* Header is too small? */
107 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
108 if (hdr == NULL) {
109 if (LOG_INVALID(IPPROTO_UDP))
110 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
111 "nf_ct_udp: short packet ");
112 return -NF_ACCEPT;
115 /* Truncated/malformed packets */
116 if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
117 if (LOG_INVALID(IPPROTO_UDP))
118 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
119 "nf_ct_udp: truncated/malformed packet ");
120 return -NF_ACCEPT;
123 /* Packet with no checksum */
124 if (!hdr->check)
125 return NF_ACCEPT;
127 /* Checksum invalid? Ignore.
128 * We skip checking packets on the outgoing path
129 * because the checksum is assumed to be correct.
130 * FIXME: Source route IP option packets --RR */
131 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
132 nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
133 if (LOG_INVALID(IPPROTO_UDP))
134 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
135 "nf_ct_udp: bad UDP checksum ");
136 return -NF_ACCEPT;
139 return NF_ACCEPT;
142 #ifdef CONFIG_SYSCTL
143 static unsigned int udp_sysctl_table_users;
144 static struct ctl_table_header *udp_sysctl_header;
145 static struct ctl_table udp_sysctl_table[] = {
147 .procname = "nf_conntrack_udp_timeout",
148 .data = &nf_ct_udp_timeout,
149 .maxlen = sizeof(unsigned int),
150 .mode = 0644,
151 .proc_handler = &proc_dointvec_jiffies,
154 .procname = "nf_conntrack_udp_timeout_stream",
155 .data = &nf_ct_udp_timeout_stream,
156 .maxlen = sizeof(unsigned int),
157 .mode = 0644,
158 .proc_handler = &proc_dointvec_jiffies,
161 .ctl_name = 0
164 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
165 static struct ctl_table udp_compat_sysctl_table[] = {
167 .procname = "ip_conntrack_udp_timeout",
168 .data = &nf_ct_udp_timeout,
169 .maxlen = sizeof(unsigned int),
170 .mode = 0644,
171 .proc_handler = &proc_dointvec_jiffies,
174 .procname = "ip_conntrack_udp_timeout_stream",
175 .data = &nf_ct_udp_timeout_stream,
176 .maxlen = sizeof(unsigned int),
177 .mode = 0644,
178 .proc_handler = &proc_dointvec_jiffies,
181 .ctl_name = 0
184 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
185 #endif /* CONFIG_SYSCTL */
187 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
189 .l3proto = PF_INET,
190 .l4proto = IPPROTO_UDP,
191 .name = "udp",
192 .pkt_to_tuple = udp_pkt_to_tuple,
193 .invert_tuple = udp_invert_tuple,
194 .print_tuple = udp_print_tuple,
195 .print_conntrack = udp_print_conntrack,
196 .packet = udp_packet,
197 .new = udp_new,
198 .error = udp_error,
199 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
200 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
201 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
202 .nla_policy = nf_ct_port_nla_policy,
203 #endif
204 #ifdef CONFIG_SYSCTL
205 .ctl_table_users = &udp_sysctl_table_users,
206 .ctl_table_header = &udp_sysctl_header,
207 .ctl_table = udp_sysctl_table,
208 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
209 .ctl_compat_table = udp_compat_sysctl_table,
210 #endif
211 #endif
213 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
215 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
217 .l3proto = PF_INET6,
218 .l4proto = IPPROTO_UDP,
219 .name = "udp",
220 .pkt_to_tuple = udp_pkt_to_tuple,
221 .invert_tuple = udp_invert_tuple,
222 .print_tuple = udp_print_tuple,
223 .print_conntrack = udp_print_conntrack,
224 .packet = udp_packet,
225 .new = udp_new,
226 .error = udp_error,
227 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
228 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
229 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
230 .nla_policy = nf_ct_port_nla_policy,
231 #endif
232 #ifdef CONFIG_SYSCTL
233 .ctl_table_users = &udp_sysctl_table_users,
234 .ctl_table_header = &udp_sysctl_header,
235 .ctl_table = udp_sysctl_table,
236 #endif
238 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);