blob: 5f35757fbff031ab224e758c0c3d8c502c86db55 [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>
Christoph Paasch9d2493f2009-03-16 15:15:35 +010025#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
26#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027
Patrick McHardy933a41e2006-11-29 02:35:18 +010028static unsigned int nf_ct_udp_timeout __read_mostly = 30*HZ;
29static unsigned int nf_ct_udp_timeout_stream __read_mostly = 180*HZ;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030
Jan Engelhardt09f263c2008-04-14 11:15:53 +020031static bool udp_pkt_to_tuple(const struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032 unsigned int dataoff,
33 struct nf_conntrack_tuple *tuple)
34{
Jan Engelhardtda3f13c2008-01-31 04:52:29 -080035 const struct udphdr *hp;
36 struct udphdr _hdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080037
38 /* Actually only need first 8 bytes. */
39 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
40 if (hp == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +020041 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080042
43 tuple->src.u.udp.port = hp->source;
44 tuple->dst.u.udp.port = hp->dest;
45
Jan Engelhardt09f263c2008-04-14 11:15:53 +020046 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080047}
48
Jan Engelhardt09f263c2008-04-14 11:15:53 +020049static bool udp_invert_tuple(struct nf_conntrack_tuple *tuple,
50 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051{
52 tuple->src.u.udp.port = orig->dst.u.udp.port;
53 tuple->dst.u.udp.port = orig->src.u.udp.port;
Jan Engelhardt09f263c2008-04-14 11:15:53 +020054 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055}
56
57/* Print out the per-protocol part of the tuple. */
58static int udp_print_tuple(struct seq_file *s,
59 const struct nf_conntrack_tuple *tuple)
60{
61 return seq_printf(s, "sport=%hu dport=%hu ",
62 ntohs(tuple->src.u.udp.port),
63 ntohs(tuple->dst.u.udp.port));
64}
65
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080066/* Returns verdict for packet, and may modify conntracktype */
Patrick McHardyc88130b2008-01-31 04:42:11 -080067static int udp_packet(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068 const struct sk_buff *skb,
69 unsigned int dataoff,
70 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +020071 u_int8_t pf,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080072 unsigned int hooknum)
73{
74 /* If we've seen traffic both ways, this is some kind of UDP
75 stream. Extend timeout. */
Patrick McHardyc88130b2008-01-31 04:42:11 -080076 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
77 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udp_timeout_stream);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080078 /* Also, more likely to be important, and not a probe */
Patrick McHardyc88130b2008-01-31 04:42:11 -080079 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
Patrick McHardy858b31332010-02-03 13:48:53 +010080 nf_conntrack_event_cache(IPCT_ASSURED, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080081 } else
Patrick McHardyc88130b2008-01-31 04:42:11 -080082 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udp_timeout);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083
84 return NF_ACCEPT;
85}
86
87/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +020088static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb,
89 unsigned int dataoff)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080090{
Jan Engelhardt09f263c2008-04-14 11:15:53 +020091 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092}
93
Patrick McHardy8fea97e2010-02-15 17:45:08 +010094static int udp_error(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
95 unsigned int dataoff, enum ip_conntrack_info *ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +020096 u_int8_t pf,
Patrick McHardy96f6bf82006-04-06 14:19:24 -070097 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080098{
99 unsigned int udplen = skb->len - dataoff;
Jan Engelhardtda3f13c2008-01-31 04:52:29 -0800100 const struct udphdr *hdr;
101 struct udphdr _hdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102
103 /* Header is too small? */
104 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
105 if (hdr == NULL) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200106 if (LOG_INVALID(net, IPPROTO_UDP))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800107 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
108 "nf_ct_udp: short packet ");
109 return -NF_ACCEPT;
110 }
111
112 /* Truncated/malformed packets */
113 if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200114 if (LOG_INVALID(net, IPPROTO_UDP))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800115 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
116 "nf_ct_udp: truncated/malformed packet ");
117 return -NF_ACCEPT;
118 }
119
120 /* Packet with no checksum */
121 if (!hdr->check)
122 return NF_ACCEPT;
123
124 /* Checksum invalid? Ignore.
125 * We skip checking packets on the outgoing path
Patrick McHardy84fa7932006-08-29 16:44:56 -0700126 * because the checksum is assumed to be correct.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800127 * FIXME: Source route IP option packets --RR */
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200128 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700129 nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200130 if (LOG_INVALID(net, IPPROTO_UDP))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800131 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
132 "nf_ct_udp: bad UDP checksum ");
133 return -NF_ACCEPT;
134 }
135
136 return NF_ACCEPT;
137}
138
Patrick McHardy933a41e2006-11-29 02:35:18 +0100139#ifdef CONFIG_SYSCTL
140static unsigned int udp_sysctl_table_users;
141static struct ctl_table_header *udp_sysctl_header;
142static struct ctl_table udp_sysctl_table[] = {
143 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100144 .procname = "nf_conntrack_udp_timeout",
145 .data = &nf_ct_udp_timeout,
146 .maxlen = sizeof(unsigned int),
147 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800148 .proc_handler = proc_dointvec_jiffies,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100149 },
150 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100151 .procname = "nf_conntrack_udp_timeout_stream",
152 .data = &nf_ct_udp_timeout_stream,
153 .maxlen = sizeof(unsigned int),
154 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800155 .proc_handler = proc_dointvec_jiffies,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100156 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800157 { }
Patrick McHardy933a41e2006-11-29 02:35:18 +0100158};
Patrick McHardya999e682006-11-29 02:35:20 +0100159#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
160static struct ctl_table udp_compat_sysctl_table[] = {
161 {
Patrick McHardya999e682006-11-29 02:35:20 +0100162 .procname = "ip_conntrack_udp_timeout",
163 .data = &nf_ct_udp_timeout,
164 .maxlen = sizeof(unsigned int),
165 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800166 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100167 },
168 {
Patrick McHardya999e682006-11-29 02:35:20 +0100169 .procname = "ip_conntrack_udp_timeout_stream",
170 .data = &nf_ct_udp_timeout_stream,
171 .maxlen = sizeof(unsigned int),
172 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800173 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100174 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800175 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100176};
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,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000191#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
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,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100194 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700195 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800196#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100197#ifdef CONFIG_SYSCTL
198 .ctl_table_users = &udp_sysctl_table_users,
199 .ctl_table_header = &udp_sysctl_header,
200 .ctl_table = udp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +0100201#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
202 .ctl_compat_table = udp_compat_sysctl_table,
203#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100204#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205};
Patrick McHardy13b18332006-12-02 22:11:25 -0800206EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800207
Patrick McHardy61075af2007-07-14 20:48:19 -0700208struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209{
210 .l3proto = PF_INET6,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100211 .l4proto = IPPROTO_UDP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800212 .name = "udp",
213 .pkt_to_tuple = udp_pkt_to_tuple,
214 .invert_tuple = udp_invert_tuple,
215 .print_tuple = udp_print_tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800216 .packet = udp_packet,
217 .new = udp_new,
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700218 .error = udp_error,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000219#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700220 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
221 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100222 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700223 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800224#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100225#ifdef CONFIG_SYSCTL
226 .ctl_table_users = &udp_sysctl_table_users,
227 .ctl_table_header = &udp_sysctl_header,
228 .ctl_table = udp_sysctl_table,
229#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230};
Patrick McHardy13b18332006-12-02 22:11:25 -0800231EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);