nf_conntrack_proto_icmp.c 10.3 KB
Newer Older
1 2
/* (C) 1999-2001 Paul `Rusty' Russell
 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3
 * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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/netfilter.h>
#include <linux/in.h>
#include <linux/icmp.h>
#include <linux/seq_file.h>
#include <net/ip.h>
#include <net/checksum.h>
#include <linux/netfilter_ipv4.h>
#include <net/netfilter/nf_conntrack_tuple.h>
20
#include <net/netfilter/nf_conntrack_l4proto.h>
21
#include <net/netfilter/nf_conntrack_core.h>
22
#include <net/netfilter/nf_conntrack_zones.h>
23
#include <net/netfilter/nf_log.h>
24

25
static const unsigned int nf_ct_icmp_timeout = 30*HZ;
26

27 28 29 30 31
static inline struct nf_icmp_net *icmp_pernet(struct net *net)
{
	return &net->ct.nf_ct_proto.icmp;
}

32
static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
33
			      struct net *net, struct nf_conntrack_tuple *tuple)
34
{
35 36
	const struct icmphdr *hp;
	struct icmphdr _hdr;
37 38 39

	hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
	if (hp == NULL)
40
		return false;
41 42 43 44 45

	tuple->dst.u.icmp.type = hp->type;
	tuple->src.u.icmp.id = hp->un.echo.id;
	tuple->dst.u.icmp.code = hp->code;

46
	return true;
47 48
}

49 50 51 52 53 54 55 56 57 58 59 60
/* Add 1; spaces filled with 0. */
static const u_int8_t invmap[] = {
	[ICMP_ECHO] = ICMP_ECHOREPLY + 1,
	[ICMP_ECHOREPLY] = ICMP_ECHO + 1,
	[ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
	[ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
	[ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
	[ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
	[ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
	[ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
};

61 62
static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
			      const struct nf_conntrack_tuple *orig)
63
{
64 65
	if (orig->dst.u.icmp.type >= sizeof(invmap) ||
	    !invmap[orig->dst.u.icmp.type])
66
		return false;
67 68 69 70

	tuple->src.u.icmp.id = orig->src.u.icmp.id;
	tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
	tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
71
	return true;
72 73
}

74 75
static unsigned int *icmp_get_timeouts(struct net *net)
{
76
	return &icmp_pernet(net)->timeout;
77 78
}

79 80 81 82 83
/* Returns verdict for packet, or -1 for invalid. */
static int icmp_packet(struct nf_conn *ct,
		       const struct sk_buff *skb,
		       unsigned int dataoff,
		       enum ip_conntrack_info ctinfo,
84
		       unsigned int *timeout)
85
{
86 87 88
	/* Do not immediately delete the connection after the first
	   successful reply to avoid excessive conntrackd traffic
	   and also to handle correctly ICMP echo reply duplicates. */
89
	nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
90 91 92 93 94

	return NF_ACCEPT;
}

/* Called when a new connection for this protocol found. */
95
static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
96
		     unsigned int dataoff, unsigned int *timeouts)
97
{
98 99 100 101 102 103
	static const u_int8_t valid_new[] = {
		[ICMP_ECHO] = 1,
		[ICMP_TIMESTAMP] = 1,
		[ICMP_INFO_REQUEST] = 1,
		[ICMP_ADDRESS] = 1
	};
104

105 106
	if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
	    !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
107
		/* Can't create a new ICMP `conn' with this. */
108
		pr_debug("icmp: can't create new conn with type %u\n",
109
			 ct->tuplehash[0].tuple.dst.u.icmp.type);
110
		nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
111
		return false;
112
	}
113
	return true;
114 115 116 117
}

/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
static int
118
icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
119
		 unsigned int hooknum)
120 121
{
	struct nf_conntrack_tuple innertuple, origtuple;
122 123
	const struct nf_conntrack_l4proto *innerproto;
	const struct nf_conntrack_tuple_hash *h;
124
	const struct nf_conntrack_zone *zone;
125
	enum ip_conntrack_info ctinfo;
126
	struct nf_conntrack_zone tmp;
127

128
	WARN_ON(skb_nfct(skb));
129
	zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
130

131 132 133 134
	/* Are they talking about one of our connections? */
	if (!nf_ct_get_tuplepr(skb,
			       skb_network_offset(skb) + ip_hdrlen(skb)
						       + sizeof(struct icmphdr),
135
			       PF_INET, net, &origtuple)) {
136
		pr_debug("icmp_error_message: failed to get tuple\n");
137 138 139
		return -NF_ACCEPT;
	}

140
	/* rcu_read_lock()ed by nf_hook_thresh */
141
	innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
142

143 144 145
	/* Ordinarily, we'd expect the inverted tupleproto, but it's
	   been preserved inside the ICMP. */
	if (!nf_ct_invert_tuple(&innertuple, &origtuple,
146
				&nf_conntrack_l3proto_ipv4, innerproto)) {
147
		pr_debug("icmp_error_message: no match\n");
148 149 150
		return -NF_ACCEPT;
	}

151
	ctinfo = IP_CT_RELATED;
152

153
	h = nf_conntrack_find_get(net, zone, &innertuple);
154
	if (!h) {
155 156
		pr_debug("icmp_error_message: no match\n");
		return -NF_ACCEPT;
157 158
	}

159
	if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
160
		ctinfo += IP_CT_IS_REPLY;
161

162
	/* Update skb to refer to this connection */
163
	nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
164
	return NF_ACCEPT;
165 166
}

167 168 169 170 171 172
static void icmp_error_log(const struct sk_buff *skb, struct net *net,
			   u8 pf, const char *msg)
{
	nf_l4proto_log_invalid(skb, net, pf, IPPROTO_ICMP, "%s", msg);
}

173 174
/* Small and modified version of icmp_rcv */
static int
175 176
icmp_error(struct net *net, struct nf_conn *tmpl,
	   struct sk_buff *skb, unsigned int dataoff,
177
	   u8 pf, unsigned int hooknum)
178
{
179 180
	const struct icmphdr *icmph;
	struct icmphdr _ih;
181 182

	/* Not enough header? */
183
	icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
184
	if (icmph == NULL) {
185
		icmp_error_log(skb, net, pf, "short packet");
186 187 188 189
		return -NF_ACCEPT;
	}

	/* See ip_conntrack_proto_tcp.c */
190
	if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
191
	    nf_ip_checksum(skb, hooknum, dataoff, 0)) {
192
		icmp_error_log(skb, net, pf, "bad hw icmp checksum");
193 194 195 196 197 198 199 200 201 202
		return -NF_ACCEPT;
	}

	/*
	 *	18 is the highest 'known' ICMP type. Anything else is a mystery
	 *
	 *	RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
	 *		  discarded.
	 */
	if (icmph->type > NR_ICMP_TYPES) {
203
		icmp_error_log(skb, net, pf, "invalid icmp type");
204 205 206 207
		return -NF_ACCEPT;
	}

	/* Need to track icmp error message? */
208 209 210 211 212
	if (icmph->type != ICMP_DEST_UNREACH &&
	    icmph->type != ICMP_SOURCE_QUENCH &&
	    icmph->type != ICMP_TIME_EXCEEDED &&
	    icmph->type != ICMP_PARAMETERPROB &&
	    icmph->type != ICMP_REDIRECT)
213 214
		return NF_ACCEPT;

215
	return icmp_error_message(net, tmpl, skb, hooknum);
216 217
}

D
Duan Jiong 已提交
218
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
219 220 221 222

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

223
static int icmp_tuple_to_nlattr(struct sk_buff *skb,
224 225
				const struct nf_conntrack_tuple *t)
{
226 227 228 229
	if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
	    nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
	    nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
		goto nla_put_failure;
230 231
	return 0;

232
nla_put_failure:
233 234 235
	return -1;
}

236 237 238 239
static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
	[CTA_PROTO_ICMP_TYPE]	= { .type = NLA_U8 },
	[CTA_PROTO_ICMP_CODE]	= { .type = NLA_U8 },
	[CTA_PROTO_ICMP_ID]	= { .type = NLA_U16 },
240 241
};

242
static int icmp_nlattr_to_tuple(struct nlattr *tb[],
243 244
				struct nf_conntrack_tuple *tuple)
{
245 246 247
	if (!tb[CTA_PROTO_ICMP_TYPE] ||
	    !tb[CTA_PROTO_ICMP_CODE] ||
	    !tb[CTA_PROTO_ICMP_ID])
248 249
		return -EINVAL;

250 251 252
	tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
	tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
	tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
253

254 255
	if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
	    !invmap[tuple->dst.u.icmp.type])
256 257 258 259
		return -EINVAL;

	return 0;
}
260

261
static unsigned int icmp_nlattr_tuple_size(void)
262
{
263 264 265 266 267 268
	static unsigned int size __read_mostly;

	if (!size)
		size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);

	return size;
269
}
270 271
#endif

272 273 274 275 276
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)

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

277 278
static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
				      struct net *net, void *data)
279 280
{
	unsigned int *timeout = data;
281
	struct nf_icmp_net *in = icmp_pernet(net);
282 283 284 285 286 287

	if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
		*timeout =
			ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
	} else {
		/* Set default ICMP timeout. */
288
		*timeout = in->timeout;
289 290 291 292 293 294 295 296 297
	}
	return 0;
}

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

298 299
	if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
		goto nla_put_failure;
300 301 302 303 304 305 306 307 308 309 310 311
	return 0;

nla_put_failure:
	return -ENOSPC;
}

static const struct nla_policy
icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
	[CTA_TIMEOUT_ICMP_TIMEOUT]	= { .type = NLA_U32 },
};
#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */

312 313 314 315 316 317
#ifdef CONFIG_SYSCTL
static struct ctl_table icmp_sysctl_table[] = {
	{
		.procname	= "nf_conntrack_icmp_timeout",
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
318
		.proc_handler	= proc_dointvec_jiffies,
319
	},
320
	{ }
321 322 323
};
#endif /* CONFIG_SYSCTL */

324 325
static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
				     struct nf_icmp_net *in)
326 327 328 329 330 331 332
{
#ifdef CONFIG_SYSCTL
	pn->ctl_table = kmemdup(icmp_sysctl_table,
				sizeof(icmp_sysctl_table),
				GFP_KERNEL);
	if (!pn->ctl_table)
		return -ENOMEM;
333

334
	pn->ctl_table[0].data = &in->timeout;
335 336 337 338 339 340 341 342 343 344 345
#endif
	return 0;
}

static int icmp_init_net(struct net *net, u_int16_t proto)
{
	struct nf_icmp_net *in = icmp_pernet(net);
	struct nf_proto_net *pn = &in->pn;

	in->timeout = nf_ct_icmp_timeout;

346
	return icmp_kmemdup_sysctl_table(pn, in);
347 348
}

349 350 351 352 353
static struct nf_proto_net *icmp_get_net_proto(struct net *net)
{
	return &net->ct.nf_ct_proto.icmp.pn;
}

354
const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
355 356
{
	.l3proto		= PF_INET,
357
	.l4proto		= IPPROTO_ICMP,
358 359 360
	.pkt_to_tuple		= icmp_pkt_to_tuple,
	.invert_tuple		= icmp_invert_tuple,
	.packet			= icmp_packet,
361
	.get_timeouts		= icmp_get_timeouts,
362 363 364
	.new			= icmp_new,
	.error			= icmp_error,
	.destroy		= NULL,
365
	.me			= NULL,
D
Duan Jiong 已提交
366
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
367
	.tuple_to_nlattr	= icmp_tuple_to_nlattr,
368
	.nlattr_tuple_size	= icmp_nlattr_tuple_size,
369
	.nlattr_to_tuple	= icmp_nlattr_to_tuple,
370
	.nla_policy		= icmp_nla_policy,
371
#endif
372 373 374 375 376 377 378 379 380
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
	.ctnl_timeout		= {
		.nlattr_to_obj	= icmp_timeout_nlattr_to_obj,
		.obj_to_nlattr	= icmp_timeout_obj_to_nlattr,
		.nlattr_max	= CTA_TIMEOUT_ICMP_MAX,
		.obj_size	= sizeof(unsigned int),
		.nla_policy	= icmp_timeout_nla_policy,
	},
#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
381
	.init_net		= icmp_init_net,
382
	.get_net_proto		= icmp_get_net_proto,
383
};