nf_conntrack_proto_udplite.c 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* (C) 1999-2001 Paul `Rusty' Russell
 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
 * (C) 2007 Patrick McHardy <kaber@trash.net>
 *
 * 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>

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv6.h>
#include <net/netfilter/nf_conntrack_l4proto.h>
#include <net/netfilter/nf_conntrack_ecache.h>
25
#include <net/netfilter/nf_log.h>
26

27 28 29 30 31 32 33 34 35 36
enum udplite_conntrack {
	UDPLITE_CT_UNREPLIED,
	UDPLITE_CT_REPLIED,
	UDPLITE_CT_MAX
};

static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = {
	[UDPLITE_CT_UNREPLIED]	= 30*HZ,
	[UDPLITE_CT_REPLIED]	= 180*HZ,
};
37

38 39 40 41 42 43 44 45 46 47 48
static int udplite_net_id __read_mostly;
struct udplite_net {
	struct nf_proto_net pn;
	unsigned int timeouts[UDPLITE_CT_MAX];
};

static inline struct udplite_net *udplite_pernet(struct net *net)
{
	return net_generic(net, udplite_net_id);
}

49 50 51
static bool udplite_pkt_to_tuple(const struct sk_buff *skb,
				 unsigned int dataoff,
				 struct nf_conntrack_tuple *tuple)
52
{
53 54
	const struct udphdr *hp;
	struct udphdr _hdr;
55 56 57

	hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
	if (hp == NULL)
58
		return false;
59 60 61

	tuple->src.u.udp.port = hp->source;
	tuple->dst.u.udp.port = hp->dest;
62
	return true;
63 64
}

65 66
static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
				 const struct nf_conntrack_tuple *orig)
67 68 69
{
	tuple->src.u.udp.port = orig->dst.u.udp.port;
	tuple->dst.u.udp.port = orig->src.u.udp.port;
70
	return true;
71 72 73
}

/* Print out the per-protocol part of the tuple. */
74 75
static void udplite_print_tuple(struct seq_file *s,
				const struct nf_conntrack_tuple *tuple)
76
{
77 78 79
	seq_printf(s, "sport=%hu dport=%hu ",
		   ntohs(tuple->src.u.udp.port),
		   ntohs(tuple->dst.u.udp.port));
80 81
}

82 83
static unsigned int *udplite_get_timeouts(struct net *net)
{
84
	return udplite_pernet(net)->timeouts;
85 86
}

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

/* Called when a new connection for this protocol found. */
112
static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
113
			unsigned int dataoff, unsigned int *timeouts)
114
{
115
	return true;
116 117
}

118
static int udplite_error(struct net *net, struct nf_conn *tmpl,
119 120
			 struct sk_buff *skb,
			 unsigned int dataoff,
121
			 enum ip_conntrack_info *ctinfo,
122
			 u_int8_t pf,
123 124 125
			 unsigned int hooknum)
{
	unsigned int udplen = skb->len - dataoff;
126 127
	const struct udphdr *hdr;
	struct udphdr _hdr;
128 129 130 131 132
	unsigned int cscov;

	/* Header is too small? */
	hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
	if (hdr == NULL) {
133
		if (LOG_INVALID(net, IPPROTO_UDPLITE))
134
			nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
135 136 137 138 139 140 141 142
				      "nf_ct_udplite: short packet ");
		return -NF_ACCEPT;
	}

	cscov = ntohs(hdr->len);
	if (cscov == 0)
		cscov = udplen;
	else if (cscov < sizeof(*hdr) || cscov > udplen) {
143
		if (LOG_INVALID(net, IPPROTO_UDPLITE))
144
			nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
145 146 147 148 149 150
				"nf_ct_udplite: invalid checksum coverage ");
		return -NF_ACCEPT;
	}

	/* UDPLITE mandates checksums */
	if (!hdr->check) {
151
		if (LOG_INVALID(net, IPPROTO_UDPLITE))
152
			nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
153 154 155 156 157
				      "nf_ct_udplite: checksum missing ");
		return -NF_ACCEPT;
	}

	/* Checksum invalid? Ignore. */
158
	if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
159 160
	    nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP,
	    			pf)) {
161
		if (LOG_INVALID(net, IPPROTO_UDPLITE))
162
			nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
163 164
				      "nf_ct_udplite: bad UDPLite checksum ");
		return -NF_ACCEPT;
165 166 167 168 169
	}

	return NF_ACCEPT;
}

170 171 172 173 174
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)

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

175 176
static int udplite_timeout_nlattr_to_obj(struct nlattr *tb[],
					 struct net *net, void *data)
177 178
{
	unsigned int *timeouts = data;
179
	struct udplite_net *un = udplite_pernet(net);
180 181

	/* set default timeouts for UDPlite. */
182 183
	timeouts[UDPLITE_CT_UNREPLIED] = un->timeouts[UDPLITE_CT_UNREPLIED];
	timeouts[UDPLITE_CT_REPLIED] = un->timeouts[UDPLITE_CT_REPLIED];
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

	if (tb[CTA_TIMEOUT_UDPLITE_UNREPLIED]) {
		timeouts[UDPLITE_CT_UNREPLIED] =
		  ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_UNREPLIED])) * HZ;
	}
	if (tb[CTA_TIMEOUT_UDPLITE_REPLIED]) {
		timeouts[UDPLITE_CT_REPLIED] =
		  ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_REPLIED])) * HZ;
	}
	return 0;
}

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

201 202 203 204 205
	if (nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_UNREPLIED,
			 htonl(timeouts[UDPLITE_CT_UNREPLIED] / HZ)) ||
	    nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_REPLIED,
			 htonl(timeouts[UDPLITE_CT_REPLIED] / HZ)))
		goto nla_put_failure;
206 207 208 209 210 211 212 213 214 215 216 217 218
	return 0;

nla_put_failure:
	return -ENOSPC;
}

static const struct nla_policy
udplite_timeout_nla_policy[CTA_TIMEOUT_UDPLITE_MAX+1] = {
	[CTA_TIMEOUT_UDPLITE_UNREPLIED]	= { .type = NLA_U32 },
	[CTA_TIMEOUT_UDPLITE_REPLIED]	= { .type = NLA_U32 },
};
#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */

219 220 221 222 223 224
#ifdef CONFIG_SYSCTL
static struct ctl_table udplite_sysctl_table[] = {
	{
		.procname	= "nf_conntrack_udplite_timeout",
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
225
		.proc_handler	= proc_dointvec_jiffies,
226 227 228 229 230
	},
	{
		.procname	= "nf_conntrack_udplite_timeout_stream",
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
231
		.proc_handler	= proc_dointvec_jiffies,
232
	},
233
	{ }
234 235 236
};
#endif /* CONFIG_SYSCTL */

237 238
static int udplite_kmemdup_sysctl_table(struct nf_proto_net *pn,
					struct udplite_net *un)
239 240
{
#ifdef CONFIG_SYSCTL
241 242 243 244 245 246 247 248 249 250 251
	if (pn->ctl_table)
		return 0;

	pn->ctl_table = kmemdup(udplite_sysctl_table,
				sizeof(udplite_sysctl_table),
				GFP_KERNEL);
	if (!pn->ctl_table)
		return -ENOMEM;

	pn->ctl_table[0].data = &un->timeouts[UDPLITE_CT_UNREPLIED];
	pn->ctl_table[1].data = &un->timeouts[UDPLITE_CT_REPLIED];
252
#endif
253 254 255 256 257 258 259 260 261 262 263
	return 0;
}

static int udplite_init_net(struct net *net, u_int16_t proto)
{
	struct udplite_net *un = udplite_pernet(net);
	struct nf_proto_net *pn = &un->pn;

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

264 265 266
		for (i = 0 ; i < UDPLITE_CT_MAX; i++)
			un->timeouts[i] = udplite_timeouts[i];
	}
267 268

	return udplite_kmemdup_sysctl_table(pn, un);
269 270
}

271 272 273 274 275 276 277 278 279
static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
{
	.l3proto		= PF_INET,
	.l4proto		= IPPROTO_UDPLITE,
	.name			= "udplite",
	.pkt_to_tuple		= udplite_pkt_to_tuple,
	.invert_tuple		= udplite_invert_tuple,
	.print_tuple		= udplite_print_tuple,
	.packet			= udplite_packet,
280
	.get_timeouts		= udplite_get_timeouts,
281 282
	.new			= udplite_new,
	.error			= udplite_error,
I
Igor Maravić 已提交
283
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
284
	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
285
	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
286
	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
287
	.nla_policy		= nf_ct_port_nla_policy,
288
#endif
289 290 291 292 293 294 295 296 297 298
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
	.ctnl_timeout		= {
		.nlattr_to_obj	= udplite_timeout_nlattr_to_obj,
		.obj_to_nlattr	= udplite_timeout_obj_to_nlattr,
		.nlattr_max	= CTA_TIMEOUT_UDPLITE_MAX,
		.obj_size	= sizeof(unsigned int) *
					CTA_TIMEOUT_UDPLITE_MAX,
		.nla_policy	= udplite_timeout_nla_policy,
	},
#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
299 300
	.net_id			= &udplite_net_id,
	.init_net		= udplite_init_net,
301 302 303 304 305 306 307 308 309 310 311
};

static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
{
	.l3proto		= PF_INET6,
	.l4proto		= IPPROTO_UDPLITE,
	.name			= "udplite",
	.pkt_to_tuple		= udplite_pkt_to_tuple,
	.invert_tuple		= udplite_invert_tuple,
	.print_tuple		= udplite_print_tuple,
	.packet			= udplite_packet,
312
	.get_timeouts		= udplite_get_timeouts,
313 314
	.new			= udplite_new,
	.error			= udplite_error,
I
Igor Maravić 已提交
315
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
316
	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
317
	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
318
	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
319
	.nla_policy		= nf_ct_port_nla_policy,
320
#endif
321 322 323 324 325 326 327 328 329 330
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
	.ctnl_timeout		= {
		.nlattr_to_obj	= udplite_timeout_nlattr_to_obj,
		.obj_to_nlattr	= udplite_timeout_obj_to_nlattr,
		.nlattr_max	= CTA_TIMEOUT_UDPLITE_MAX,
		.obj_size	= sizeof(unsigned int) *
					CTA_TIMEOUT_UDPLITE_MAX,
		.nla_policy	= udplite_timeout_nla_policy,
	},
#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
331 332
	.net_id			= &udplite_net_id,
	.init_net		= udplite_init_net,
333 334
};

335
static int udplite_net_init(struct net *net)
336
{
337 338
	int ret = 0;

339
	ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite4);
340
	if (ret < 0) {
341
		pr_err("nf_conntrack_udplite4: pernet registration failed.\n");
342 343
		goto out;
	}
344
	ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite6);
345
	if (ret < 0) {
346
		pr_err("nf_conntrack_udplite6: pernet registration failed.\n");
347 348
		goto cleanup_udplite4;
	}
349
	return 0;
350 351

cleanup_udplite4:
352
	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4);
353 354 355 356 357 358
out:
	return ret;
}

static void udplite_net_exit(struct net *net)
{
359 360
	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite6);
	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4);
361 362 363 364 365 366 367 368 369 370 371
}

static struct pernet_operations udplite_net_ops = {
	.init = udplite_net_init,
	.exit = udplite_net_exit,
	.id   = &udplite_net_id,
	.size = sizeof(struct udplite_net),
};

static int __init nf_conntrack_proto_udplite_init(void)
{
372 373
	int ret;

374 375 376 377
	ret = register_pernet_subsys(&udplite_net_ops);
	if (ret < 0)
		goto out_pernet;

378 379 380 381 382 383 384 385 386 387 388 389
	ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite4);
	if (ret < 0)
		goto out_udplite4;

	ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite6);
	if (ret < 0)
		goto out_udplite6;

	return 0;
out_udplite6:
	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
out_udplite4:
390 391
	unregister_pernet_subsys(&udplite_net_ops);
out_pernet:
392
	return ret;
393 394 395 396
}

static void __exit nf_conntrack_proto_udplite_exit(void)
{
397 398
	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
399
	unregister_pernet_subsys(&udplite_net_ops);
400 401 402 403 404 405
}

module_init(nf_conntrack_proto_udplite_init);
module_exit(nf_conntrack_proto_udplite_exit);

MODULE_LICENSE("GPL");