blob: 7ac607319562d90283d88c89ec73c6bbc8f37796 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* (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.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 */
8
9#include <linux/types.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010#include <linux/timer.h>
11#include <linux/module.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080012#include <linux/udp.h>
13#include <linux/seq_file.h>
14#include <linux/skbuff.h>
15#include <linux/ipv6.h>
16#include <net/ip6_checksum.h>
17#include <net/checksum.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010018
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080019#include <linux/netfilter.h>
20#include <linux/netfilter_ipv4.h>
21#include <linux/netfilter_ipv6.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010022#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010023#include <net/netfilter/nf_conntrack_ecache.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080024#include <net/netfilter/nf_log.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025
Patrick McHardy933a41e2006-11-29 02:35:18 +010026static unsigned int nf_ct_udp_timeout __read_mostly = 30*HZ;
27static unsigned int nf_ct_udp_timeout_stream __read_mostly = 180*HZ;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080028
29static int udp_pkt_to_tuple(const struct sk_buff *skb,
30 unsigned int dataoff,
31 struct nf_conntrack_tuple *tuple)
32{
33 struct udphdr _hdr, *hp;
34
35 /* Actually only need first 8 bytes. */
36 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
37 if (hp == NULL)
38 return 0;
39
40 tuple->src.u.udp.port = hp->source;
41 tuple->dst.u.udp.port = hp->dest;
42
43 return 1;
44}
45
46static int udp_invert_tuple(struct nf_conntrack_tuple *tuple,
47 const struct nf_conntrack_tuple *orig)
48{
49 tuple->src.u.udp.port = orig->dst.u.udp.port;
50 tuple->dst.u.udp.port = orig->src.u.udp.port;
51 return 1;
52}
53
54/* Print out the per-protocol part of the tuple. */
55static int udp_print_tuple(struct seq_file *s,
56 const struct nf_conntrack_tuple *tuple)
57{
58 return seq_printf(s, "sport=%hu dport=%hu ",
59 ntohs(tuple->src.u.udp.port),
60 ntohs(tuple->dst.u.udp.port));
61}
62
63/* Print out the private part of the conntrack. */
64static int udp_print_conntrack(struct seq_file *s,
65 const struct nf_conn *conntrack)
66{
67 return 0;
68}
69
70/* Returns verdict for packet, and may modify conntracktype */
71static int udp_packet(struct nf_conn *conntrack,
72 const struct sk_buff *skb,
73 unsigned int dataoff,
74 enum ip_conntrack_info ctinfo,
75 int pf,
76 unsigned int hooknum)
77{
78 /* If we've seen traffic both ways, this is some kind of UDP
79 stream. Extend timeout. */
80 if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
81 nf_ct_refresh_acct(conntrack, ctinfo, skb,
82 nf_ct_udp_timeout_stream);
83 /* Also, more likely to be important, and not a probe */
84 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
85 nf_conntrack_event_cache(IPCT_STATUS, skb);
86 } else
87 nf_ct_refresh_acct(conntrack, ctinfo, skb, nf_ct_udp_timeout);
88
89 return NF_ACCEPT;
90}
91
92/* Called when a new connection for this protocol found. */
93static int udp_new(struct nf_conn *conntrack, const struct sk_buff *skb,
94 unsigned int dataoff)
95{
96 return 1;
97}
98
99static int udp_error(struct sk_buff *skb, unsigned int dataoff,
100 enum ip_conntrack_info *ctinfo,
101 int pf,
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700102 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800103{
104 unsigned int udplen = skb->len - dataoff;
105 struct udphdr _hdr, *hdr;
106
107 /* Header is too small? */
108 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
109 if (hdr == NULL) {
110 if (LOG_INVALID(IPPROTO_UDP))
111 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
112 "nf_ct_udp: short packet ");
113 return -NF_ACCEPT;
114 }
115
116 /* Truncated/malformed packets */
117 if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
118 if (LOG_INVALID(IPPROTO_UDP))
119 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
120 "nf_ct_udp: truncated/malformed packet ");
121 return -NF_ACCEPT;
122 }
123
124 /* Packet with no checksum */
125 if (!hdr->check)
126 return NF_ACCEPT;
127
128 /* Checksum invalid? Ignore.
129 * We skip checking packets on the outgoing path
Patrick McHardy84fa7932006-08-29 16:44:56 -0700130 * because the checksum is assumed to be correct.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800131 * FIXME: Source route IP option packets --RR */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800132 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700133 nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800134 if (LOG_INVALID(IPPROTO_UDP))
135 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
136 "nf_ct_udp: bad UDP checksum ");
137 return -NF_ACCEPT;
138 }
139
140 return NF_ACCEPT;
141}
142
Patrick McHardy933a41e2006-11-29 02:35:18 +0100143#ifdef CONFIG_SYSCTL
144static unsigned int udp_sysctl_table_users;
145static struct ctl_table_header *udp_sysctl_header;
146static struct ctl_table udp_sysctl_table[] = {
147 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100148 .procname = "nf_conntrack_udp_timeout",
149 .data = &nf_ct_udp_timeout,
150 .maxlen = sizeof(unsigned int),
151 .mode = 0644,
152 .proc_handler = &proc_dointvec_jiffies,
153 },
154 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100155 .procname = "nf_conntrack_udp_timeout_stream",
156 .data = &nf_ct_udp_timeout_stream,
157 .maxlen = sizeof(unsigned int),
158 .mode = 0644,
159 .proc_handler = &proc_dointvec_jiffies,
160 },
161 {
162 .ctl_name = 0
163 }
164};
Patrick McHardya999e682006-11-29 02:35:20 +0100165#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
166static struct ctl_table udp_compat_sysctl_table[] = {
167 {
Patrick McHardya999e682006-11-29 02:35:20 +0100168 .procname = "ip_conntrack_udp_timeout",
169 .data = &nf_ct_udp_timeout,
170 .maxlen = sizeof(unsigned int),
171 .mode = 0644,
172 .proc_handler = &proc_dointvec_jiffies,
173 },
174 {
Patrick McHardya999e682006-11-29 02:35:20 +0100175 .procname = "ip_conntrack_udp_timeout_stream",
176 .data = &nf_ct_udp_timeout_stream,
177 .maxlen = sizeof(unsigned int),
178 .mode = 0644,
179 .proc_handler = &proc_dointvec_jiffies,
180 },
181 {
182 .ctl_name = 0
183 }
184};
185#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100186#endif /* CONFIG_SYSCTL */
187
Patrick McHardy61075af2007-07-14 20:48:19 -0700188struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189{
190 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100191 .l4proto = IPPROTO_UDP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800192 .name = "udp",
193 .pkt_to_tuple = udp_pkt_to_tuple,
194 .invert_tuple = udp_invert_tuple,
195 .print_tuple = udp_print_tuple,
196 .print_conntrack = udp_print_conntrack,
197 .packet = udp_packet,
198 .new = udp_new,
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700199 .error = udp_error,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800200#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700201 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
202 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700203 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800204#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100205#ifdef CONFIG_SYSCTL
206 .ctl_table_users = &udp_sysctl_table_users,
207 .ctl_table_header = &udp_sysctl_header,
208 .ctl_table = udp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +0100209#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
210 .ctl_compat_table = udp_compat_sysctl_table,
211#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100212#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213};
Patrick McHardy13b18332006-12-02 22:11:25 -0800214EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215
Patrick McHardy61075af2007-07-14 20:48:19 -0700216struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800217{
218 .l3proto = PF_INET6,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100219 .l4proto = IPPROTO_UDP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800220 .name = "udp",
221 .pkt_to_tuple = udp_pkt_to_tuple,
222 .invert_tuple = udp_invert_tuple,
223 .print_tuple = udp_print_tuple,
224 .print_conntrack = udp_print_conntrack,
225 .packet = udp_packet,
226 .new = udp_new,
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700227 .error = udp_error,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800228#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700229 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
230 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700231 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800232#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100233#ifdef CONFIG_SYSCTL
234 .ctl_table_users = &udp_sysctl_table_users,
235 .ctl_table_header = &udp_sysctl_header,
236 .ctl_table = udp_sysctl_table,
237#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800238};
Patrick McHardy13b18332006-12-02 22:11:25 -0800239EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);