nf_conntrack_proto_udp.c 8.8 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 30 31 32 33 34 35 36 37
enum udp_conntrack {
	UDP_CT_UNREPLIED,
	UDP_CT_REPLIED,
	UDP_CT_MAX
};

static unsigned int udp_timeouts[UDP_CT_MAX] = {
	[UDP_CT_UNREPLIED]	= 30*HZ,
	[UDP_CT_REPLIED]	= 180*HZ,
};
38

39
static bool udp_pkt_to_tuple(const struct sk_buff *skb,
40 41 42
			     unsigned int dataoff,
			     struct nf_conntrack_tuple *tuple)
{
43 44
	const struct udphdr *hp;
	struct udphdr _hdr;
45 46 47 48

	/* Actually only need first 8 bytes. */
	hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
	if (hp == NULL)
49
		return false;
50 51 52 53

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

54
	return true;
55 56
}

57 58
static bool udp_invert_tuple(struct nf_conntrack_tuple *tuple,
			     const struct nf_conntrack_tuple *orig)
59 60 61
{
	tuple->src.u.udp.port = orig->dst.u.udp.port;
	tuple->dst.u.udp.port = orig->src.u.udp.port;
62
	return true;
63 64 65 66 67 68 69 70 71 72 73
}

/* 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));
}

74 75 76 77 78
static unsigned int *udp_get_timeouts(struct net *net)
{
	return udp_timeouts;
}

79
/* Returns verdict for packet, and may modify conntracktype */
80
static int udp_packet(struct nf_conn *ct,
81 82 83
		      const struct sk_buff *skb,
		      unsigned int dataoff,
		      enum ip_conntrack_info ctinfo,
84
		      u_int8_t pf,
85 86
		      unsigned int hooknum,
		      unsigned int *timeouts)
87 88 89
{
	/* If we've seen traffic both ways, this is some kind of UDP
	   stream.  Extend timeout. */
90
	if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
91
		nf_ct_refresh_acct(ct, ctinfo, skb,
92
				   timeouts[UDP_CT_REPLIED]);
93
		/* Also, more likely to be important, and not a probe */
94
		if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
95
			nf_conntrack_event_cache(IPCT_ASSURED, ct);
96 97
	} else {
		nf_ct_refresh_acct(ct, ctinfo, skb,
98
				   timeouts[UDP_CT_UNREPLIED]);
99
	}
100 101 102 103
	return NF_ACCEPT;
}

/* Called when a new connection for this protocol found. */
104
static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb,
105
		    unsigned int dataoff, unsigned int *timeouts)
106
{
107
	return true;
108 109
}

110 111
static int udp_error(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
		     unsigned int dataoff, enum ip_conntrack_info *ctinfo,
112
		     u_int8_t pf,
113
		     unsigned int hooknum)
114 115
{
	unsigned int udplen = skb->len - dataoff;
116 117
	const struct udphdr *hdr;
	struct udphdr _hdr;
118 119 120 121

	/* Header is too small? */
	hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
	if (hdr == NULL) {
122
		if (LOG_INVALID(net, IPPROTO_UDP))
123 124 125 126 127 128 129
			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)) {
130
		if (LOG_INVALID(net, IPPROTO_UDP))
131 132 133 134 135 136 137 138 139 140 141
			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
142
	 * because the checksum is assumed to be correct.
143
	 * FIXME: Source route IP option packets --RR */
144
	if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
145
	    nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
146
		if (LOG_INVALID(net, IPPROTO_UDP))
147 148 149 150 151 152 153 154
			nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
				"nf_ct_udp: bad UDP checksum ");
		return -NF_ACCEPT;
	}

	return NF_ACCEPT;
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)

#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nfnetlink_cttimeout.h>

static int udp_timeout_nlattr_to_obj(struct nlattr *tb[], void *data)
{
	unsigned int *timeouts = data;

	/* set default timeouts for UDP. */
	timeouts[UDP_CT_UNREPLIED] = udp_timeouts[UDP_CT_UNREPLIED];
	timeouts[UDP_CT_REPLIED] = udp_timeouts[UDP_CT_REPLIED];

	if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) {
		timeouts[UDP_CT_UNREPLIED] =
			ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ;
	}
	if (tb[CTA_TIMEOUT_UDP_REPLIED]) {
		timeouts[UDP_CT_REPLIED] =
			ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ;
	}
	return 0;
}

static int
udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
{
	const unsigned int *timeouts = data;

184 185 186 187 188
	if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED,
			 htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) ||
	    nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED,
			 htonl(timeouts[UDP_CT_REPLIED] / HZ)))
		goto nla_put_failure;
189 190 191 192 193 194 195 196 197 198 199 200 201
	return 0;

nla_put_failure:
	return -ENOSPC;
}

static const struct nla_policy
udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = {
       [CTA_TIMEOUT_UDP_UNREPLIED]	= { .type = NLA_U32 },
       [CTA_TIMEOUT_UDP_REPLIED]	= { .type = NLA_U32 },
};
#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */

202 203 204 205 206 207
#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",
208
		.data		= &udp_timeouts[UDP_CT_UNREPLIED],
209 210
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
211
		.proc_handler	= proc_dointvec_jiffies,
212 213 214
	},
	{
		.procname	= "nf_conntrack_udp_timeout_stream",
215
		.data		= &udp_timeouts[UDP_CT_REPLIED],
216 217
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
218
		.proc_handler	= proc_dointvec_jiffies,
219
	},
220
	{ }
221
};
222 223 224 225
#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
static struct ctl_table udp_compat_sysctl_table[] = {
	{
		.procname	= "ip_conntrack_udp_timeout",
226
		.data		= &udp_timeouts[UDP_CT_UNREPLIED],
227 228
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
229
		.proc_handler	= proc_dointvec_jiffies,
230 231 232
	},
	{
		.procname	= "ip_conntrack_udp_timeout_stream",
233
		.data		= &udp_timeouts[UDP_CT_REPLIED],
234 235
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
236
		.proc_handler	= proc_dointvec_jiffies,
237
	},
238
	{ }
239 240
};
#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
241 242
#endif /* CONFIG_SYSCTL */

243
struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
244 245
{
	.l3proto		= PF_INET,
246
	.l4proto		= IPPROTO_UDP,
247 248 249 250 251
	.name			= "udp",
	.pkt_to_tuple		= udp_pkt_to_tuple,
	.invert_tuple		= udp_invert_tuple,
	.print_tuple		= udp_print_tuple,
	.packet			= udp_packet,
252
	.get_timeouts		= udp_get_timeouts,
253
	.new			= udp_new,
254
	.error			= udp_error,
I
Igor Maravić 已提交
255
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
256 257
	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
258
	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
259
	.nla_policy		= nf_ct_port_nla_policy,
260
#endif
261 262 263 264 265 266 267 268 269
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
	.ctnl_timeout		= {
		.nlattr_to_obj	= udp_timeout_nlattr_to_obj,
		.obj_to_nlattr	= udp_timeout_obj_to_nlattr,
		.nlattr_max	= CTA_TIMEOUT_UDP_MAX,
		.obj_size	= sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
		.nla_policy	= udp_timeout_nla_policy,
	},
#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
270 271 272 273
#ifdef CONFIG_SYSCTL
	.ctl_table_users	= &udp_sysctl_table_users,
	.ctl_table_header	= &udp_sysctl_header,
	.ctl_table		= udp_sysctl_table,
274 275 276
#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
	.ctl_compat_table	= udp_compat_sysctl_table,
#endif
277
#endif
278
};
279
EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
280

281
struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
282 283
{
	.l3proto		= PF_INET6,
284
	.l4proto		= IPPROTO_UDP,
285 286 287 288 289
	.name			= "udp",
	.pkt_to_tuple		= udp_pkt_to_tuple,
	.invert_tuple		= udp_invert_tuple,
	.print_tuple		= udp_print_tuple,
	.packet			= udp_packet,
290
	.get_timeouts		= udp_get_timeouts,
291
	.new			= udp_new,
292
	.error			= udp_error,
I
Igor Maravić 已提交
293
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
294 295
	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
296
	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
297
	.nla_policy		= nf_ct_port_nla_policy,
298
#endif
299 300 301 302 303 304 305 306 307
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
	.ctnl_timeout		= {
		.nlattr_to_obj	= udp_timeout_nlattr_to_obj,
		.obj_to_nlattr	= udp_timeout_obj_to_nlattr,
		.nlattr_max	= CTA_TIMEOUT_UDP_MAX,
		.obj_size	= sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
		.nla_policy	= udp_timeout_nla_policy,
	},
#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
308 309 310 311 312
#ifdef CONFIG_SYSCTL
	.ctl_table_users	= &udp_sysctl_table_users,
	.ctl_table_header	= &udp_sysctl_header,
	.ctl_table		= udp_sysctl_table,
#endif
313
};
314
EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);