nf_conntrack_proto_udp.c 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* (C) 1999-2001 Paul `Rusty' Russell
 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/types.h>
#include <linux/timer.h>
#include <linux/module.h>
#include <linux/udp.h>
#include <linux/seq_file.h>
#include <linux/skbuff.h>
#include <linux/ipv6.h>
#include <net/ip6_checksum.h>
#include <net/checksum.h>
18

19 20 21
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv6.h>
22
#include <net/netfilter/nf_conntrack_l4proto.h>
23
#include <net/netfilter/nf_conntrack_ecache.h>
24
#include <net/netfilter/nf_log.h>
25 26
#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
27

28 29
static unsigned int nf_ct_udp_timeout __read_mostly = 30*HZ;
static unsigned int nf_ct_udp_timeout_stream __read_mostly = 180*HZ;
30

31
static bool udp_pkt_to_tuple(const struct sk_buff *skb,
32 33 34
			     unsigned int dataoff,
			     struct nf_conntrack_tuple *tuple)
{
35 36
	const struct udphdr *hp;
	struct udphdr _hdr;
37 38 39 40

	/* Actually only need first 8 bytes. */
	hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
	if (hp == NULL)
41
		return false;
42 43 44 45

	tuple->src.u.udp.port = hp->source;
	tuple->dst.u.udp.port = hp->dest;

46
	return true;
47 48
}

49 50
static bool udp_invert_tuple(struct nf_conntrack_tuple *tuple,
			     const struct nf_conntrack_tuple *orig)
51 52 53
{
	tuple->src.u.udp.port = orig->dst.u.udp.port;
	tuple->dst.u.udp.port = orig->src.u.udp.port;
54
	return true;
55 56 57 58 59 60 61 62 63 64 65 66
}

/* Print out the per-protocol part of the tuple. */
static int udp_print_tuple(struct seq_file *s,
			   const struct nf_conntrack_tuple *tuple)
{
	return seq_printf(s, "sport=%hu dport=%hu ",
			  ntohs(tuple->src.u.udp.port),
			  ntohs(tuple->dst.u.udp.port));
}

/* Returns verdict for packet, and may modify conntracktype */
67
static int udp_packet(struct nf_conn *ct,
68 69 70
		      const struct sk_buff *skb,
		      unsigned int dataoff,
		      enum ip_conntrack_info ctinfo,
71
		      u_int8_t pf,
72 73 74 75
		      unsigned int hooknum)
{
	/* If we've seen traffic both ways, this is some kind of UDP
	   stream.  Extend timeout. */
76 77
	if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
		nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udp_timeout_stream);
78
		/* Also, more likely to be important, and not a probe */
79
		if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
80
			nf_conntrack_event_cache(IPCT_ASSURED, ct);
81
	} else
82
		nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udp_timeout);
83 84 85 86 87

	return NF_ACCEPT;
}

/* Called when a new connection for this protocol found. */
88 89
static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb,
		    unsigned int dataoff)
90
{
91
	return true;
92 93
}

94
static int udp_error(struct net *net, struct sk_buff *skb, unsigned int dataoff,
95
		     enum ip_conntrack_info *ctinfo,
96
		     u_int8_t pf,
97
		     unsigned int hooknum)
98 99
{
	unsigned int udplen = skb->len - dataoff;
100 101
	const struct udphdr *hdr;
	struct udphdr _hdr;
102 103 104 105

	/* Header is too small? */
	hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
	if (hdr == NULL) {
106
		if (LOG_INVALID(net, IPPROTO_UDP))
107 108 109 110 111 112 113
			nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
				      "nf_ct_udp: short packet ");
		return -NF_ACCEPT;
	}

	/* Truncated/malformed packets */
	if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
114
		if (LOG_INVALID(net, IPPROTO_UDP))
115 116 117 118 119 120 121 122 123 124 125
			nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
				"nf_ct_udp: truncated/malformed packet ");
		return -NF_ACCEPT;
	}

	/* Packet with no checksum */
	if (!hdr->check)
		return NF_ACCEPT;

	/* Checksum invalid? Ignore.
	 * We skip checking packets on the outgoing path
126
	 * because the checksum is assumed to be correct.
127
	 * FIXME: Source route IP option packets --RR */
128
	if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
129
	    nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
130
		if (LOG_INVALID(net, IPPROTO_UDP))
131 132 133 134 135 136 137 138
			nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
				"nf_ct_udp: bad UDP checksum ");
		return -NF_ACCEPT;
	}

	return NF_ACCEPT;
}

139 140 141 142 143 144 145 146 147
#ifdef CONFIG_SYSCTL
static unsigned int udp_sysctl_table_users;
static struct ctl_table_header *udp_sysctl_header;
static struct ctl_table udp_sysctl_table[] = {
	{
		.procname	= "nf_conntrack_udp_timeout",
		.data		= &nf_ct_udp_timeout,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
148
		.proc_handler	= proc_dointvec_jiffies,
149 150 151 152 153 154
	},
	{
		.procname	= "nf_conntrack_udp_timeout_stream",
		.data		= &nf_ct_udp_timeout_stream,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
155
		.proc_handler	= proc_dointvec_jiffies,
156
	},
157
	{ }
158
};
159 160 161 162 163 164 165
#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
static struct ctl_table udp_compat_sysctl_table[] = {
	{
		.procname	= "ip_conntrack_udp_timeout",
		.data		= &nf_ct_udp_timeout,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
166
		.proc_handler	= proc_dointvec_jiffies,
167 168 169 170 171 172
	},
	{
		.procname	= "ip_conntrack_udp_timeout_stream",
		.data		= &nf_ct_udp_timeout_stream,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
173
		.proc_handler	= proc_dointvec_jiffies,
174
	},
175
	{ }
176 177
};
#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
178 179
#endif /* CONFIG_SYSCTL */

180
struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
181 182
{
	.l3proto		= PF_INET,
183
	.l4proto		= IPPROTO_UDP,
184 185 186 187 188 189
	.name			= "udp",
	.pkt_to_tuple		= udp_pkt_to_tuple,
	.invert_tuple		= udp_invert_tuple,
	.print_tuple		= udp_print_tuple,
	.packet			= udp_packet,
	.new			= udp_new,
190
	.error			= udp_error,
191
#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
192 193
	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
194
	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
195
	.nla_policy		= nf_ct_port_nla_policy,
196
#endif
197 198 199 200
#ifdef CONFIG_SYSCTL
	.ctl_table_users	= &udp_sysctl_table_users,
	.ctl_table_header	= &udp_sysctl_header,
	.ctl_table		= udp_sysctl_table,
201 202 203
#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
	.ctl_compat_table	= udp_compat_sysctl_table,
#endif
204
#endif
205
};
206
EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
207

208
struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
209 210
{
	.l3proto		= PF_INET6,
211
	.l4proto		= IPPROTO_UDP,
212 213 214 215 216 217
	.name			= "udp",
	.pkt_to_tuple		= udp_pkt_to_tuple,
	.invert_tuple		= udp_invert_tuple,
	.print_tuple		= udp_print_tuple,
	.packet			= udp_packet,
	.new			= udp_new,
218
	.error			= udp_error,
219
#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
220 221
	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
222
	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
223
	.nla_policy		= nf_ct_port_nla_policy,
224
#endif
225 226 227 228 229
#ifdef CONFIG_SYSCTL
	.ctl_table_users	= &udp_sysctl_table_users,
	.ctl_table_header	= &udp_sysctl_header,
	.ctl_table		= udp_sysctl_table,
#endif
230
};
231
EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);