[NETFILTER]: Fix timeout sysctls on big-endian 64bit architectures
[linux-2.6.git] / net / ipv4 / netfilter / ip_conntrack_proto_udp.c
1 /* (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>
14 #include <linux/ip.h>
15 #include <linux/udp.h>
16 #include <linux/seq_file.h>
17 #include <net/checksum.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
21
22 unsigned int ip_ct_udp_timeout = 30*HZ;
23 unsigned int ip_ct_udp_timeout_stream = 180*HZ;
24
25 static int udp_pkt_to_tuple(const struct sk_buff *skb,
26                              unsigned int dataoff,
27                              struct ip_conntrack_tuple *tuple)
28 {
29         struct udphdr _hdr, *hp;
30
31         /* Actually only need first 8 bytes. */
32         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
33         if (hp == NULL)
34                 return 0;
35
36         tuple->src.u.udp.port = hp->source;
37         tuple->dst.u.udp.port = hp->dest;
38
39         return 1;
40 }
41
42 static int udp_invert_tuple(struct ip_conntrack_tuple *tuple,
43                             const struct ip_conntrack_tuple *orig)
44 {
45         tuple->src.u.udp.port = orig->dst.u.udp.port;
46         tuple->dst.u.udp.port = orig->src.u.udp.port;
47         return 1;
48 }
49
50 /* Print out the per-protocol part of the tuple. */
51 static int udp_print_tuple(struct seq_file *s,
52                            const struct ip_conntrack_tuple *tuple)
53 {
54         return seq_printf(s, "sport=%hu dport=%hu ",
55                           ntohs(tuple->src.u.udp.port),
56                           ntohs(tuple->dst.u.udp.port));
57 }
58
59 /* Print out the private part of the conntrack. */
60 static int udp_print_conntrack(struct seq_file *s,
61                                const struct ip_conntrack *conntrack)
62 {
63         return 0;
64 }
65
66 /* Returns verdict for packet, and may modify conntracktype */
67 static int udp_packet(struct ip_conntrack *conntrack,
68                       const struct sk_buff *skb,
69                       enum ip_conntrack_info ctinfo)
70 {
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, &conntrack->status)) {
74                 ip_ct_refresh_acct(conntrack, ctinfo, skb, 
75                                    ip_ct_udp_timeout_stream);
76                 /* Also, more likely to be important, and not a probe */
77                 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
78                         ip_conntrack_event_cache(IPCT_STATUS, skb);
79         } else
80                 ip_ct_refresh_acct(conntrack, ctinfo, skb, ip_ct_udp_timeout);
81
82         return NF_ACCEPT;
83 }
84
85 /* Called when a new connection for this protocol found. */
86 static int udp_new(struct ip_conntrack *conntrack, const struct sk_buff *skb)
87 {
88         return 1;
89 }
90
91 static int udp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
92                      unsigned int hooknum)
93 {
94         struct iphdr *iph = skb->nh.iph;
95         unsigned int udplen = skb->len - iph->ihl * 4;
96         struct udphdr _hdr, *hdr;
97
98         /* Header is too small? */
99         hdr = skb_header_pointer(skb, iph->ihl*4, sizeof(_hdr), &_hdr);
100         if (hdr == NULL) {
101                 if (LOG_INVALID(IPPROTO_UDP))
102                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
103                                   "ip_ct_udp: short packet ");
104                 return -NF_ACCEPT;
105         }
106         
107         /* Truncated/malformed packets */
108         if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
109                 if (LOG_INVALID(IPPROTO_UDP))
110                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
111                                   "ip_ct_udp: truncated/malformed packet ");
112                 return -NF_ACCEPT;
113         }
114         
115         /* Packet with no checksum */
116         if (!hdr->check)
117                 return NF_ACCEPT;
118
119         /* Checksum invalid? Ignore.
120          * We skip checking packets on the outgoing path
121          * because the semantic of CHECKSUM_HW is different there 
122          * and moreover root might send raw packets.
123          * FIXME: Source route IP option packets --RR */
124         if (hooknum == NF_IP_PRE_ROUTING
125             && skb->ip_summed != CHECKSUM_UNNECESSARY
126             && csum_tcpudp_magic(iph->saddr, iph->daddr, udplen, IPPROTO_UDP,
127                                  skb->ip_summed == CHECKSUM_HW ? skb->csum
128                                  : skb_checksum(skb, iph->ihl*4, udplen, 0))) {
129                 if (LOG_INVALID(IPPROTO_UDP))
130                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
131                                   "ip_ct_udp: bad UDP checksum ");
132                 return -NF_ACCEPT;
133         }
134         
135         return NF_ACCEPT;
136 }
137
138 struct ip_conntrack_protocol ip_conntrack_protocol_udp =
139 {
140         .proto                  = IPPROTO_UDP,
141         .name                   = "udp",
142         .pkt_to_tuple           = udp_pkt_to_tuple,
143         .invert_tuple           = udp_invert_tuple,
144         .print_tuple            = udp_print_tuple,
145         .print_conntrack        = udp_print_conntrack,
146         .packet                 = udp_packet,
147         .new                    = udp_new,
148         .error                  = udp_error,
149 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
150     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
151         .tuple_to_nfattr        = ip_ct_port_tuple_to_nfattr,
152         .nfattr_to_tuple        = ip_ct_port_nfattr_to_tuple,
153 #endif
154 };