blob: 55b7d3210adf9e0e48ff3b7a10c4c78e6a91f985 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
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 */
8
9#include <linux/types.h>
10#include <linux/sched.h>
11#include <linux/timer.h>
12#include <linux/netfilter.h>
13#include <linux/in.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020014#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/udp.h>
16#include <linux/seq_file.h>
17#include <net/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/netfilter_ipv4.h>
19#include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
20
Patrick McHardybabbdb12006-01-09 17:48:09 -080021unsigned int ip_ct_udp_timeout = 30*HZ;
22unsigned int ip_ct_udp_timeout_stream = 180*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static int udp_pkt_to_tuple(const struct sk_buff *skb,
25 unsigned int dataoff,
26 struct ip_conntrack_tuple *tuple)
27{
28 struct udphdr _hdr, *hp;
29
30 /* Actually only need first 8 bytes. */
31 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
32 if (hp == NULL)
33 return 0;
34
35 tuple->src.u.udp.port = hp->source;
36 tuple->dst.u.udp.port = hp->dest;
37
38 return 1;
39}
40
41static int udp_invert_tuple(struct ip_conntrack_tuple *tuple,
42 const struct ip_conntrack_tuple *orig)
43{
44 tuple->src.u.udp.port = orig->dst.u.udp.port;
45 tuple->dst.u.udp.port = orig->src.u.udp.port;
46 return 1;
47}
48
49/* Print out the per-protocol part of the tuple. */
50static int udp_print_tuple(struct seq_file *s,
51 const struct ip_conntrack_tuple *tuple)
52{
53 return seq_printf(s, "sport=%hu dport=%hu ",
54 ntohs(tuple->src.u.udp.port),
55 ntohs(tuple->dst.u.udp.port));
56}
57
58/* Print out the private part of the conntrack. */
59static int udp_print_conntrack(struct seq_file *s,
60 const struct ip_conntrack *conntrack)
61{
62 return 0;
63}
64
65/* Returns verdict for packet, and may modify conntracktype */
66static int udp_packet(struct ip_conntrack *conntrack,
67 const struct sk_buff *skb,
68 enum ip_conntrack_info ctinfo)
69{
70 /* If we've seen traffic both ways, this is some kind of UDP
71 stream. Extend timeout. */
72 if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
73 ip_ct_refresh_acct(conntrack, ctinfo, skb,
74 ip_ct_udp_timeout_stream);
75 /* Also, more likely to be important, and not a probe */
Harald Welteac3247b2005-08-09 19:28:03 -070076 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
77 ip_conntrack_event_cache(IPCT_STATUS, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 } else
79 ip_ct_refresh_acct(conntrack, ctinfo, skb, ip_ct_udp_timeout);
80
81 return NF_ACCEPT;
82}
83
84/* Called when a new connection for this protocol found. */
85static int udp_new(struct ip_conntrack *conntrack, const struct sk_buff *skb)
86{
87 return 1;
88}
89
90static int udp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
91 unsigned int hooknum)
92{
93 struct iphdr *iph = skb->nh.iph;
94 unsigned int udplen = skb->len - iph->ihl * 4;
95 struct udphdr _hdr, *hdr;
96
97 /* Header is too small? */
98 hdr = skb_header_pointer(skb, iph->ihl*4, sizeof(_hdr), &_hdr);
99 if (hdr == NULL) {
100 if (LOG_INVALID(IPPROTO_UDP))
Harald Welte608c8e42005-08-09 19:58:27 -0700101 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 "ip_ct_udp: short packet ");
103 return -NF_ACCEPT;
104 }
105
106 /* Truncated/malformed packets */
107 if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
108 if (LOG_INVALID(IPPROTO_UDP))
Harald Welte608c8e42005-08-09 19:58:27 -0700109 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 "ip_ct_udp: truncated/malformed packet ");
111 return -NF_ACCEPT;
112 }
113
114 /* Packet with no checksum */
115 if (!hdr->check)
116 return NF_ACCEPT;
117
118 /* Checksum invalid? Ignore.
119 * We skip checking packets on the outgoing path
120 * because the semantic of CHECKSUM_HW is different there
121 * and moreover root might send raw packets.
122 * FIXME: Source route IP option packets --RR */
123 if (hooknum == NF_IP_PRE_ROUTING
Keir Frasere3be8ba2005-06-21 14:03:23 -0700124 && skb->ip_summed != CHECKSUM_UNNECESSARY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 && csum_tcpudp_magic(iph->saddr, iph->daddr, udplen, IPPROTO_UDP,
126 skb->ip_summed == CHECKSUM_HW ? skb->csum
127 : skb_checksum(skb, iph->ihl*4, udplen, 0))) {
128 if (LOG_INVALID(IPPROTO_UDP))
Harald Welte608c8e42005-08-09 19:58:27 -0700129 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 "ip_ct_udp: bad UDP checksum ");
131 return -NF_ACCEPT;
132 }
133
134 return NF_ACCEPT;
135}
136
137struct ip_conntrack_protocol ip_conntrack_protocol_udp =
138{
139 .proto = IPPROTO_UDP,
140 .name = "udp",
141 .pkt_to_tuple = udp_pkt_to_tuple,
142 .invert_tuple = udp_invert_tuple,
143 .print_tuple = udp_print_tuple,
144 .print_conntrack = udp_print_conntrack,
145 .packet = udp_packet,
146 .new = udp_new,
147 .error = udp_error,
Harald Welte080774a2005-08-09 19:32:58 -0700148#if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
149 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
150 .tuple_to_nfattr = ip_ct_port_tuple_to_nfattr,
151 .nfattr_to_tuple = ip_ct_port_nfattr_to_tuple,
152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};