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/sched.h>
16 #include <linux/timer.h>
17 #include <linux/module.h>
18 #include <linux/netfilter.h>
19 #include <linux/udp.h>
20 #include <linux/seq_file.h>
21 #include <linux/skbuff.h>
22 #include <linux/ipv6.h>
23 #include <net/ip6_checksum.h>
24 #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_protocol.h>
30 unsigned int nf_ct_udp_timeout
= 30*HZ
;
31 unsigned int nf_ct_udp_timeout_stream
= 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 /* Print out the private part of the conntrack. */
68 static int udp_print_conntrack(struct seq_file
*s
,
69 const struct nf_conn
*conntrack
)
74 /* Returns verdict for packet, and may modify conntracktype */
75 static int udp_packet(struct nf_conn
*conntrack
,
76 const struct sk_buff
*skb
,
78 enum ip_conntrack_info ctinfo
,
82 /* If we've seen traffic both ways, this is some kind of UDP
83 stream. Extend timeout. */
84 if (test_bit(IPS_SEEN_REPLY_BIT
, &conntrack
->status
)) {
85 nf_ct_refresh_acct(conntrack
, ctinfo
, skb
,
86 nf_ct_udp_timeout_stream
);
87 /* Also, more likely to be important, and not a probe */
88 if (!test_and_set_bit(IPS_ASSURED_BIT
, &conntrack
->status
))
89 nf_conntrack_event_cache(IPCT_STATUS
, skb
);
91 nf_ct_refresh_acct(conntrack
, ctinfo
, skb
, nf_ct_udp_timeout
);
96 /* Called when a new connection for this protocol found. */
97 static int udp_new(struct nf_conn
*conntrack
, const struct sk_buff
*skb
,
103 static int udp_error(struct sk_buff
*skb
, unsigned int dataoff
,
104 enum ip_conntrack_info
*ctinfo
,
106 unsigned int hooknum
,
107 int (*csum
)(const struct sk_buff
*, unsigned int))
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 semantic of CHECKSUM_HW is different there
136 * and moreover root might send raw packets.
137 * FIXME: Source route IP option packets --RR */
138 if (((pf
== PF_INET
&& hooknum
== NF_IP_PRE_ROUTING
) ||
139 (pf
== PF_INET6
&& hooknum
== NF_IP6_PRE_ROUTING
))
140 && skb
->ip_summed
!= CHECKSUM_UNNECESSARY
141 && csum(skb
, dataoff
)) {
142 if (LOG_INVALID(IPPROTO_UDP
))
143 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
144 "nf_ct_udp: bad UDP checksum ");
151 static int csum4(const struct sk_buff
*skb
, unsigned int dataoff
)
153 return csum_tcpudp_magic(skb
->nh
.iph
->saddr
, skb
->nh
.iph
->daddr
,
154 skb
->len
- dataoff
, IPPROTO_UDP
,
155 skb
->ip_summed
== CHECKSUM_HW
? skb
->csum
156 : skb_checksum(skb
, dataoff
,
157 skb
->len
- dataoff
, 0));
160 static int csum6(const struct sk_buff
*skb
, unsigned int dataoff
)
162 return csum_ipv6_magic(&skb
->nh
.ipv6h
->saddr
, &skb
->nh
.ipv6h
->daddr
,
163 skb
->len
- dataoff
, IPPROTO_UDP
,
164 skb
->ip_summed
== CHECKSUM_HW
165 ? csum_sub(skb
->csum
,
166 skb_checksum(skb
, 0, dataoff
, 0))
167 : skb_checksum(skb
, dataoff
, skb
->len
- dataoff
,
171 static int udp_error4(struct sk_buff
*skb
,
172 unsigned int dataoff
,
173 enum ip_conntrack_info
*ctinfo
,
175 unsigned int hooknum
)
177 return udp_error(skb
, dataoff
, ctinfo
, pf
, hooknum
, csum4
);
180 static int udp_error6(struct sk_buff
*skb
,
181 unsigned int dataoff
,
182 enum ip_conntrack_info
*ctinfo
,
184 unsigned int hooknum
)
186 return udp_error(skb
, dataoff
, ctinfo
, pf
, hooknum
, csum6
);
189 struct nf_conntrack_protocol nf_conntrack_protocol_udp4
=
192 .proto
= IPPROTO_UDP
,
194 .pkt_to_tuple
= udp_pkt_to_tuple
,
195 .invert_tuple
= udp_invert_tuple
,
196 .print_tuple
= udp_print_tuple
,
197 .print_conntrack
= udp_print_conntrack
,
198 .packet
= udp_packet
,
201 #if defined(CONFIG_NF_CT_NETLINK) || \
202 defined(CONFIG_NF_CT_NETLINK_MODULE)
203 .tuple_to_nfattr
= nf_ct_port_tuple_to_nfattr
,
204 .nfattr_to_tuple
= nf_ct_port_nfattr_to_tuple
,
208 struct nf_conntrack_protocol nf_conntrack_protocol_udp6
=
211 .proto
= IPPROTO_UDP
,
213 .pkt_to_tuple
= udp_pkt_to_tuple
,
214 .invert_tuple
= udp_invert_tuple
,
215 .print_tuple
= udp_print_tuple
,
216 .print_conntrack
= udp_print_conntrack
,
217 .packet
= udp_packet
,
220 #if defined(CONFIG_NF_CT_NETLINK) || \
221 defined(CONFIG_NF_CT_NETLINK_MODULE)
222 .tuple_to_nfattr
= nf_ct_port_tuple_to_nfattr
,
223 .nfattr_to_tuple
= nf_ct_port_nfattr_to_tuple
,
227 EXPORT_SYMBOL(nf_conntrack_protocol_udp4
);
228 EXPORT_SYMBOL(nf_conntrack_protocol_udp6
);