blob: 4c1e67ed63bdac33e00f117fb04b98054e67cd8c [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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063/* Returns verdict for packet, and may modify conntracktype */
Patrick McHardyc88130b2008-01-31 04:42:11 -080064static int udp_packet(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080065 const struct sk_buff *skb,
66 unsigned int dataoff,
67 enum ip_conntrack_info ctinfo,
68 int pf,
69 unsigned int hooknum)
70{
71 /* If we've seen traffic both ways, this is some kind of UDP
72 stream. Extend timeout. */
Patrick McHardyc88130b2008-01-31 04:42:11 -080073 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
74 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udp_timeout_stream);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080075 /* Also, more likely to be important, and not a probe */
Patrick McHardyc88130b2008-01-31 04:42:11 -080076 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077 nf_conntrack_event_cache(IPCT_STATUS, skb);
78 } else
Patrick McHardyc88130b2008-01-31 04:42:11 -080079 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udp_timeout);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080080
81 return NF_ACCEPT;
82}
83
84/* Called when a new connection for this protocol found. */
Patrick McHardyc88130b2008-01-31 04:42:11 -080085static int udp_new(struct nf_conn *ct, const struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080086 unsigned int dataoff)
87{
88 return 1;
89}
90
91static int udp_error(struct sk_buff *skb, unsigned int dataoff,
92 enum ip_conntrack_info *ctinfo,
93 int pf,
Patrick McHardy96f6bf82006-04-06 14:19:24 -070094 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095{
96 unsigned int udplen = skb->len - dataoff;
97 struct udphdr _hdr, *hdr;
98
99 /* Header is too small? */
100 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
101 if (hdr == NULL) {
102 if (LOG_INVALID(IPPROTO_UDP))
103 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
104 "nf_ct_udp: short packet ");
105 return -NF_ACCEPT;
106 }
107
108 /* Truncated/malformed packets */
109 if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
110 if (LOG_INVALID(IPPROTO_UDP))
111 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
112 "nf_ct_udp: truncated/malformed packet ");
113 return -NF_ACCEPT;
114 }
115
116 /* Packet with no checksum */
117 if (!hdr->check)
118 return NF_ACCEPT;
119
120 /* Checksum invalid? Ignore.
121 * We skip checking packets on the outgoing path
Patrick McHardy84fa7932006-08-29 16:44:56 -0700122 * because the checksum is assumed to be correct.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800123 * FIXME: Source route IP option packets --RR */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800124 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700125 nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800126 if (LOG_INVALID(IPPROTO_UDP))
127 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
128 "nf_ct_udp: bad UDP checksum ");
129 return -NF_ACCEPT;
130 }
131
132 return NF_ACCEPT;
133}
134
Patrick McHardy933a41e2006-11-29 02:35:18 +0100135#ifdef CONFIG_SYSCTL
136static unsigned int udp_sysctl_table_users;
137static struct ctl_table_header *udp_sysctl_header;
138static struct ctl_table udp_sysctl_table[] = {
139 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100140 .procname = "nf_conntrack_udp_timeout",
141 .data = &nf_ct_udp_timeout,
142 .maxlen = sizeof(unsigned int),
143 .mode = 0644,
144 .proc_handler = &proc_dointvec_jiffies,
145 },
146 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100147 .procname = "nf_conntrack_udp_timeout_stream",
148 .data = &nf_ct_udp_timeout_stream,
149 .maxlen = sizeof(unsigned int),
150 .mode = 0644,
151 .proc_handler = &proc_dointvec_jiffies,
152 },
153 {
154 .ctl_name = 0
155 }
156};
Patrick McHardya999e682006-11-29 02:35:20 +0100157#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
158static struct ctl_table udp_compat_sysctl_table[] = {
159 {
Patrick McHardya999e682006-11-29 02:35:20 +0100160 .procname = "ip_conntrack_udp_timeout",
161 .data = &nf_ct_udp_timeout,
162 .maxlen = sizeof(unsigned int),
163 .mode = 0644,
164 .proc_handler = &proc_dointvec_jiffies,
165 },
166 {
Patrick McHardya999e682006-11-29 02:35:20 +0100167 .procname = "ip_conntrack_udp_timeout_stream",
168 .data = &nf_ct_udp_timeout_stream,
169 .maxlen = sizeof(unsigned int),
170 .mode = 0644,
171 .proc_handler = &proc_dointvec_jiffies,
172 },
173 {
174 .ctl_name = 0
175 }
176};
177#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100178#endif /* CONFIG_SYSCTL */
179
Patrick McHardy61075af2007-07-14 20:48:19 -0700180struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800181{
182 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100183 .l4proto = IPPROTO_UDP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800184 .name = "udp",
185 .pkt_to_tuple = udp_pkt_to_tuple,
186 .invert_tuple = udp_invert_tuple,
187 .print_tuple = udp_print_tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188 .packet = udp_packet,
189 .new = udp_new,
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700190 .error = udp_error,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800191#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700192 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
193 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700194 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800195#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100196#ifdef CONFIG_SYSCTL
197 .ctl_table_users = &udp_sysctl_table_users,
198 .ctl_table_header = &udp_sysctl_header,
199 .ctl_table = udp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +0100200#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
201 .ctl_compat_table = udp_compat_sysctl_table,
202#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100203#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800204};
Patrick McHardy13b18332006-12-02 22:11:25 -0800205EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800206
Patrick McHardy61075af2007-07-14 20:48:19 -0700207struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800208{
209 .l3proto = PF_INET6,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100210 .l4proto = IPPROTO_UDP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211 .name = "udp",
212 .pkt_to_tuple = udp_pkt_to_tuple,
213 .invert_tuple = udp_invert_tuple,
214 .print_tuple = udp_print_tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215 .packet = udp_packet,
216 .new = udp_new,
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700217 .error = udp_error,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800218#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700219 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
220 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700221 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800222#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100223#ifdef CONFIG_SYSCTL
224 .ctl_table_users = &udp_sysctl_table_users,
225 .ctl_table_header = &udp_sysctl_header,
226 .ctl_table = udp_sysctl_table,
227#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228};
Patrick McHardy13b18332006-12-02 22:11:25 -0800229EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);