nf_conntrack_proto_udp.c 9.9 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
static unsigned int udp_timeouts[UDP_CT_MAX] = {
	[UDP_CT_UNREPLIED]	= 30*HZ,
	[UDP_CT_REPLIED]	= 180*HZ,
};
32

33 34 35 36 37
static inline struct nf_udp_net *udp_pernet(struct net *net)
{
	return &net->ct.nf_ct_proto.udp;
}

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

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

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

53
	return true;
54 55
}

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

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

73 74
static unsigned int *udp_get_timeouts(struct net *net)
{
75
	return udp_pernet(net)->timeouts;
76 77
}

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

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

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

	/* Header is too small? */
	hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
	if (hdr == NULL) {
121
		if (LOG_INVALID(net, IPPROTO_UDP))
122
			nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
123 124 125 126 127 128
				      "nf_ct_udp: short packet ");
		return -NF_ACCEPT;
	}

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

	return NF_ACCEPT;
}

154 155 156 157 158
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)

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

159 160
static int udp_timeout_nlattr_to_obj(struct nlattr *tb[],
				     struct net *net, void *data)
161 162
{
	unsigned int *timeouts = data;
163
	struct nf_udp_net *un = udp_pernet(net);
164 165

	/* set default timeouts for UDP. */
166 167
	timeouts[UDP_CT_UNREPLIED] = un->timeouts[UDP_CT_UNREPLIED];
	timeouts[UDP_CT_REPLIED] = un->timeouts[UDP_CT_REPLIED];
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

	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;

185 186 187 188 189
	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;
190 191 192 193 194 195 196 197 198 199 200 201 202
	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 */

203 204 205 206 207 208
#ifdef CONFIG_SYSCTL
static struct ctl_table udp_sysctl_table[] = {
	{
		.procname	= "nf_conntrack_udp_timeout",
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
209
		.proc_handler	= proc_dointvec_jiffies,
210 211 212 213 214
	},
	{
		.procname	= "nf_conntrack_udp_timeout_stream",
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
215
		.proc_handler	= proc_dointvec_jiffies,
216
	},
217
	{ }
218
};
219 220 221 222 223 224
#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
static struct ctl_table udp_compat_sysctl_table[] = {
	{
		.procname	= "ip_conntrack_udp_timeout",
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
225
		.proc_handler	= proc_dointvec_jiffies,
226 227 228 229 230
	},
	{
		.procname	= "ip_conntrack_udp_timeout_stream",
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
231
		.proc_handler	= proc_dointvec_jiffies,
232
	},
233
	{ }
234 235
};
#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
236 237
#endif /* CONFIG_SYSCTL */

238 239
static int udp_kmemdup_sysctl_table(struct nf_proto_net *pn,
				    struct nf_udp_net *un)
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
{
#ifdef CONFIG_SYSCTL
	if (pn->ctl_table)
		return 0;
	pn->ctl_table = kmemdup(udp_sysctl_table,
				sizeof(udp_sysctl_table),
				GFP_KERNEL);
	if (!pn->ctl_table)
		return -ENOMEM;
	pn->ctl_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
	pn->ctl_table[1].data = &un->timeouts[UDP_CT_REPLIED];
#endif
	return 0;
}

255 256
static int udp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
					   struct nf_udp_net *un)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
{
#ifdef CONFIG_SYSCTL
#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
	pn->ctl_compat_table = kmemdup(udp_compat_sysctl_table,
				       sizeof(udp_compat_sysctl_table),
				       GFP_KERNEL);
	if (!pn->ctl_compat_table)
		return -ENOMEM;

	pn->ctl_compat_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
	pn->ctl_compat_table[1].data = &un->timeouts[UDP_CT_REPLIED];
#endif
#endif
	return 0;
}

273
static int udp_init_net(struct net *net, u_int16_t proto)
274 275 276
{
	int ret;
	struct nf_udp_net *un = udp_pernet(net);
277
	struct nf_proto_net *pn = &un->pn;
278

279 280
	if (!pn->users) {
		int i;
281

282 283
		for (i = 0; i < UDP_CT_MAX; i++)
			un->timeouts[i] = udp_timeouts[i];
284 285
	}

286 287 288 289
	if (proto == AF_INET) {
		ret = udp_kmemdup_compat_sysctl_table(pn, un);
		if (ret < 0)
			return ret;
290

291 292 293 294 295 296 297
		ret = udp_kmemdup_sysctl_table(pn, un);
		if (ret < 0)
			nf_ct_kfree_compat_sysctl_table(pn);
	} else
		ret = udp_kmemdup_sysctl_table(pn, un);

	return ret;
298 299
}

300 301 302 303 304
static struct nf_proto_net *udp_get_net_proto(struct net *net)
{
	return &net->ct.nf_ct_proto.udp.pn;
}

305
struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
306 307
{
	.l3proto		= PF_INET,
308
	.l4proto		= IPPROTO_UDP,
309 310 311 312 313
	.name			= "udp",
	.pkt_to_tuple		= udp_pkt_to_tuple,
	.invert_tuple		= udp_invert_tuple,
	.print_tuple		= udp_print_tuple,
	.packet			= udp_packet,
314
	.get_timeouts		= udp_get_timeouts,
315
	.new			= udp_new,
316
	.error			= udp_error,
I
Igor Maravić 已提交
317
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
318 319
	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
320
	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
321
	.nla_policy		= nf_ct_port_nla_policy,
322
#endif
323 324 325 326 327 328 329 330 331
#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 */
332
	.init_net		= udp_init_net,
333
	.get_net_proto		= udp_get_net_proto,
334
};
335
EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
336

337
struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
338 339
{
	.l3proto		= PF_INET6,
340
	.l4proto		= IPPROTO_UDP,
341 342 343 344 345
	.name			= "udp",
	.pkt_to_tuple		= udp_pkt_to_tuple,
	.invert_tuple		= udp_invert_tuple,
	.print_tuple		= udp_print_tuple,
	.packet			= udp_packet,
346
	.get_timeouts		= udp_get_timeouts,
347
	.new			= udp_new,
348
	.error			= udp_error,
I
Igor Maravić 已提交
349
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
350 351
	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
352
	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
353
	.nla_policy		= nf_ct_port_nla_policy,
354
#endif
355 356 357 358 359 360 361 362 363
#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 */
364
	.init_net		= udp_init_net,
365
	.get_net_proto		= udp_get_net_proto,
366
};
367
EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);