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/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
;
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
,
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
);
44 tuple
->src
.u
.udp
.port
= hp
->source
;
45 tuple
->dst
.u
.udp
.port
= hp
->dest
;
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
;
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
,
71 enum ip_conntrack_info ctinfo
,
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
);
84 nf_ct_refresh_acct(conntrack
, ctinfo
, skb
, nf_ct_udp_timeout
);
89 /* Called when a new connection for this protocol found. */
90 static int udp_new(struct nf_conn
*conntrack
, const struct sk_buff
*skb
,
96 static int udp_error(struct sk_buff
*skb
, unsigned int dataoff
,
97 enum ip_conntrack_info
*ctinfo
,
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
);
107 if (LOG_INVALID(IPPROTO_UDP
))
108 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
109 "nf_ct_udp: short packet ");
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 ");
121 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
122 if (ipv4_conntrack_fastnat
)
126 /* Packet with no checksum */
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 ");
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),
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),
165 .proc_handler
= &proc_dointvec_jiffies
,
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),
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),
187 .proc_handler
= &proc_dointvec_jiffies
,
193 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
194 #endif /* CONFIG_SYSCTL */
196 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4
=
199 .l4proto
= IPPROTO_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
,
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
,
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
,
220 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4
);
222 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6
=
225 .l4proto
= IPPROTO_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
,
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
,
238 .ctl_table_users
= &udp_sysctl_table_users
,
239 .ctl_table_header
= &udp_sysctl_header
,
240 .ctl_table
= udp_sysctl_table
,
243 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6
);