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.
8 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * - enable working with Layer 3 protocol independent connection tracking.
11 * Derived from net/ipv4/netfilter/ip_conntrack_proto_udp.c
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>
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>
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
;
34 static int udp_pkt_to_tuple(const struct sk_buff
*skb
,
36 struct nf_conntrack_tuple
*tuple
)
38 struct udphdr _hdr
, *hp
;
40 /* Actually only need first 8 bytes. */
41 hp
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
45 tuple
->src
.u
.udp
.port
= hp
->source
;
46 tuple
->dst
.u
.udp
.port
= hp
->dest
;
51 static int udp_invert_tuple(struct nf_conntrack_tuple
*tuple
,
52 const struct nf_conntrack_tuple
*orig
)
54 tuple
->src
.u
.udp
.port
= orig
->dst
.u
.udp
.port
;
55 tuple
->dst
.u
.udp
.port
= orig
->src
.u
.udp
.port
;
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
)
63 return seq_printf(s
, "sport=%hu dport=%hu ",
64 ntohs(tuple
->src
.u
.udp
.port
),
65 ntohs(tuple
->dst
.u
.udp
.port
));
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
)
75 /* Returns verdict for packet, and may modify conntracktype */
76 static int udp_packet(struct nf_conn
*conntrack
,
77 const struct sk_buff
*skb
,
79 enum ip_conntrack_info ctinfo
,
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
);
92 nf_ct_refresh_acct(conntrack
, ctinfo
, skb
, nf_ct_udp_timeout
);
97 /* Called when a new connection for this protocol found. */
98 static int udp_new(struct nf_conn
*conntrack
, const struct sk_buff
*skb
,
104 static int udp_error(struct sk_buff
*skb
, unsigned int dataoff
,
105 enum ip_conntrack_info
*ctinfo
,
107 unsigned int hooknum
)
109 unsigned int udplen
= skb
->len
- dataoff
;
110 struct udphdr _hdr
, *hdr
;
112 /* Header is too small? */
113 hdr
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
115 if (LOG_INVALID(IPPROTO_UDP
))
116 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
117 "nf_ct_udp: short packet ");
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 ");
129 /* Packet with no checksum */
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 ");
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
[] = {
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),
160 .proc_handler
= &proc_dointvec_jiffies
,
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),
168 .proc_handler
= &proc_dointvec_jiffies
,
174 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
175 static struct ctl_table udp_compat_sysctl_table
[] = {
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),
182 .proc_handler
= &proc_dointvec_jiffies
,
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),
190 .proc_handler
= &proc_dointvec_jiffies
,
196 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
197 #endif /* CONFIG_SYSCTL */
199 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4
=
202 .l4proto
= IPPROTO_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
,
211 #if defined(CONFIG_NF_CT_NETLINK) || \
212 defined(CONFIG_NF_CT_NETLINK_MODULE)
213 .tuple_to_nfattr
= nf_ct_port_tuple_to_nfattr
,
214 .nfattr_to_tuple
= nf_ct_port_nfattr_to_tuple
,
217 .ctl_table_users
= &udp_sysctl_table_users
,
218 .ctl_table_header
= &udp_sysctl_header
,
219 .ctl_table
= udp_sysctl_table
,
220 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
221 .ctl_compat_table
= udp_compat_sysctl_table
,
225 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4
);
227 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6
=
230 .l4proto
= IPPROTO_UDP
,
232 .pkt_to_tuple
= udp_pkt_to_tuple
,
233 .invert_tuple
= udp_invert_tuple
,
234 .print_tuple
= udp_print_tuple
,
235 .print_conntrack
= udp_print_conntrack
,
236 .packet
= udp_packet
,
239 #if defined(CONFIG_NF_CT_NETLINK) || \
240 defined(CONFIG_NF_CT_NETLINK_MODULE)
241 .tuple_to_nfattr
= nf_ct_port_tuple_to_nfattr
,
242 .nfattr_to_tuple
= nf_ct_port_nfattr_to_tuple
,
245 .ctl_table_users
= &udp_sysctl_table_users
,
246 .ctl_table_header
= &udp_sysctl_header
,
247 .ctl_table
= udp_sysctl_table
,
250 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6
);