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.
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>
26 static unsigned int nf_ct_udp_timeout __read_mostly
= 30*HZ
;
27 static unsigned int nf_ct_udp_timeout_stream __read_mostly
= 180*HZ
;
29 static int udp_pkt_to_tuple(const struct sk_buff
*skb
,
31 struct nf_conntrack_tuple
*tuple
)
33 struct udphdr _hdr
, *hp
;
35 /* Actually only need first 8 bytes. */
36 hp
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
40 tuple
->src
.u
.udp
.port
= hp
->source
;
41 tuple
->dst
.u
.udp
.port
= hp
->dest
;
46 static int udp_invert_tuple(struct nf_conntrack_tuple
*tuple
,
47 const struct nf_conntrack_tuple
*orig
)
49 tuple
->src
.u
.udp
.port
= orig
->dst
.u
.udp
.port
;
50 tuple
->dst
.u
.udp
.port
= orig
->src
.u
.udp
.port
;
54 /* Print out the per-protocol part of the tuple. */
55 static int udp_print_tuple(struct seq_file
*s
,
56 const struct nf_conntrack_tuple
*tuple
)
58 return seq_printf(s
, "sport=%hu dport=%hu ",
59 ntohs(tuple
->src
.u
.udp
.port
),
60 ntohs(tuple
->dst
.u
.udp
.port
));
63 /* Returns verdict for packet, and may modify conntracktype */
64 static int udp_packet(struct nf_conn
*ct
,
65 const struct sk_buff
*skb
,
67 enum ip_conntrack_info ctinfo
,
71 /* If we've seen traffic both ways, this is some kind of UDP
72 stream. Extend timeout. */
73 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
74 nf_ct_refresh_acct(ct
, ctinfo
, skb
, nf_ct_udp_timeout_stream
);
75 /* Also, more likely to be important, and not a probe */
76 if (!test_and_set_bit(IPS_ASSURED_BIT
, &ct
->status
))
77 nf_conntrack_event_cache(IPCT_STATUS
, skb
);
79 nf_ct_refresh_acct(ct
, ctinfo
, skb
, nf_ct_udp_timeout
);
84 /* Called when a new connection for this protocol found. */
85 static int udp_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
91 static int udp_error(struct sk_buff
*skb
, unsigned int dataoff
,
92 enum ip_conntrack_info
*ctinfo
,
96 unsigned int udplen
= skb
->len
- dataoff
;
97 struct udphdr _hdr
, *hdr
;
99 /* Header is too small? */
100 hdr
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
102 if (LOG_INVALID(IPPROTO_UDP
))
103 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
104 "nf_ct_udp: short packet ");
108 /* Truncated/malformed packets */
109 if (ntohs(hdr
->len
) > udplen
|| ntohs(hdr
->len
) < sizeof(*hdr
)) {
110 if (LOG_INVALID(IPPROTO_UDP
))
111 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
112 "nf_ct_udp: truncated/malformed packet ");
116 /* Packet with no checksum */
120 /* Checksum invalid? Ignore.
121 * We skip checking packets on the outgoing path
122 * because the checksum is assumed to be correct.
123 * FIXME: Source route IP option packets --RR */
124 if (nf_conntrack_checksum
&& hooknum
== NF_INET_PRE_ROUTING
&&
125 nf_checksum(skb
, hooknum
, dataoff
, IPPROTO_UDP
, pf
)) {
126 if (LOG_INVALID(IPPROTO_UDP
))
127 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
128 "nf_ct_udp: bad UDP checksum ");
136 static unsigned int udp_sysctl_table_users
;
137 static struct ctl_table_header
*udp_sysctl_header
;
138 static struct ctl_table udp_sysctl_table
[] = {
140 .procname
= "nf_conntrack_udp_timeout",
141 .data
= &nf_ct_udp_timeout
,
142 .maxlen
= sizeof(unsigned int),
144 .proc_handler
= &proc_dointvec_jiffies
,
147 .procname
= "nf_conntrack_udp_timeout_stream",
148 .data
= &nf_ct_udp_timeout_stream
,
149 .maxlen
= sizeof(unsigned int),
151 .proc_handler
= &proc_dointvec_jiffies
,
157 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
158 static struct ctl_table udp_compat_sysctl_table
[] = {
160 .procname
= "ip_conntrack_udp_timeout",
161 .data
= &nf_ct_udp_timeout
,
162 .maxlen
= sizeof(unsigned int),
164 .proc_handler
= &proc_dointvec_jiffies
,
167 .procname
= "ip_conntrack_udp_timeout_stream",
168 .data
= &nf_ct_udp_timeout_stream
,
169 .maxlen
= sizeof(unsigned int),
171 .proc_handler
= &proc_dointvec_jiffies
,
177 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
178 #endif /* CONFIG_SYSCTL */
180 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly
=
183 .l4proto
= IPPROTO_UDP
,
185 .pkt_to_tuple
= udp_pkt_to_tuple
,
186 .invert_tuple
= udp_invert_tuple
,
187 .print_tuple
= udp_print_tuple
,
188 .packet
= udp_packet
,
191 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
192 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
193 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
194 .nla_policy
= nf_ct_port_nla_policy
,
197 .ctl_table_users
= &udp_sysctl_table_users
,
198 .ctl_table_header
= &udp_sysctl_header
,
199 .ctl_table
= udp_sysctl_table
,
200 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
201 .ctl_compat_table
= udp_compat_sysctl_table
,
205 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4
);
207 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly
=
210 .l4proto
= IPPROTO_UDP
,
212 .pkt_to_tuple
= udp_pkt_to_tuple
,
213 .invert_tuple
= udp_invert_tuple
,
214 .print_tuple
= udp_print_tuple
,
215 .packet
= udp_packet
,
218 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
219 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
220 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
221 .nla_policy
= nf_ct_port_nla_policy
,
224 .ctl_table_users
= &udp_sysctl_table_users
,
225 .ctl_table_header
= &udp_sysctl_header
,
226 .ctl_table
= udp_sysctl_table
,
229 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6
);