• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *	NET3:	Implementation of the ICMP protocol layer.
3  *
4  *		Alan Cox, <alan@lxorguk.ukuu.org.uk>
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License
8  *	as published by the Free Software Foundation; either version
9  *	2 of the License, or (at your option) any later version.
10  *
11  *	Some of the function names and the icmp unreach table for this
12  *	module were derived from [icmp.c 1.0.11 06/02/93] by
13  *	Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
14  *	Other than that this module is a complete rewrite.
15  *
16  *	Fixes:
17  *	Clemens Fruhwirth	:	introduce global icmp rate limiting
18  *					with icmp type masking ability instead
19  *					of broken per type icmp timeouts.
20  *		Mike Shaver	:	RFC1122 checks.
21  *		Alan Cox	:	Multicast ping reply as self.
22  *		Alan Cox	:	Fix atomicity lockup in ip_build_xmit
23  *					call.
24  *		Alan Cox	:	Added 216,128 byte paths to the MTU
25  *					code.
26  *		Martin Mares	:	RFC1812 checks.
27  *		Martin Mares	:	Can be configured to follow redirects
28  *					if acting as a router _without_ a
29  *					routing protocol (RFC 1812).
30  *		Martin Mares	:	Echo requests may be configured to
31  *					be ignored (RFC 1812).
32  *		Martin Mares	:	Limitation of ICMP error message
33  *					transmit rate (RFC 1812).
34  *		Martin Mares	:	TOS and Precedence set correctly
35  *					(RFC 1812).
36  *		Martin Mares	:	Now copying as much data from the
37  *					original packet as we can without
38  *					exceeding 576 bytes (RFC 1812).
39  *	Willy Konynenberg	:	Transparent proxying support.
40  *		Keith Owens	:	RFC1191 correction for 4.2BSD based
41  *					path MTU bug.
42  *		Thomas Quinot	:	ICMP Dest Unreach codes up to 15 are
43  *					valid (RFC 1812).
44  *		Andi Kleen	:	Check all packet lengths properly
45  *					and moved all kfree_skb() up to
46  *					icmp_rcv.
47  *		Andi Kleen	:	Move the rate limit bookkeeping
48  *					into the dest entry and use a token
49  *					bucket filter (thanks to ANK). Make
50  *					the rates sysctl configurable.
51  *		Yu Tianli	:	Fixed two ugly bugs in icmp_send
52  *					- IP option length was accounted wrongly
53  *					- ICMP header length was not accounted
54  *					  at all.
55  *              Tristan Greaves :       Added sysctl option to ignore bogus
56  *              			broadcast responses from broken routers.
57  *
58  * To Fix:
59  *
60  *	- Should use skb_pull() instead of all the manual checking.
61  *	  This would also greatly simply some upper layer error handlers. --AK
62  *
63  */
64 
65 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
66 
67 #include <linux/module.h>
68 #include <linux/types.h>
69 #include <linux/jiffies.h>
70 #include <linux/kernel.h>
71 #include <linux/fcntl.h>
72 #include <linux/socket.h>
73 #include <linux/in.h>
74 #include <linux/inet.h>
75 #include <linux/inetdevice.h>
76 #include <linux/netdevice.h>
77 #include <linux/string.h>
78 #include <linux/netfilter_ipv4.h>
79 #include <linux/slab.h>
80 #include <net/snmp.h>
81 #include <net/ip.h>
82 #include <net/route.h>
83 #include <net/protocol.h>
84 #include <net/icmp.h>
85 #include <net/tcp.h>
86 #include <net/udp.h>
87 #include <net/raw.h>
88 #include <net/ping.h>
89 #include <linux/skbuff.h>
90 #include <net/sock.h>
91 #include <linux/errno.h>
92 #include <linux/timer.h>
93 #include <linux/init.h>
94 #include <asm/uaccess.h>
95 #include <net/checksum.h>
96 #include <net/xfrm.h>
97 #include <net/inet_common.h>
98 #include <net/ip_fib.h>
99 
100 /*
101  *	Build xmit assembly blocks
102  */
103 
104 struct icmp_bxm {
105 	struct sk_buff *skb;
106 	int offset;
107 	int data_len;
108 
109 	struct {
110 		struct icmphdr icmph;
111 		__be32	       times[3];
112 	} data;
113 	int head_len;
114 	struct ip_options_data replyopts;
115 };
116 
117 /* An array of errno for error messages from dest unreach. */
118 /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
119 
120 const struct icmp_err icmp_err_convert[] = {
121 	{
122 		.errno = ENETUNREACH,	/* ICMP_NET_UNREACH */
123 		.fatal = 0,
124 	},
125 	{
126 		.errno = EHOSTUNREACH,	/* ICMP_HOST_UNREACH */
127 		.fatal = 0,
128 	},
129 	{
130 		.errno = ENOPROTOOPT	/* ICMP_PROT_UNREACH */,
131 		.fatal = 1,
132 	},
133 	{
134 		.errno = ECONNREFUSED,	/* ICMP_PORT_UNREACH */
135 		.fatal = 1,
136 	},
137 	{
138 		.errno = EMSGSIZE,	/* ICMP_FRAG_NEEDED */
139 		.fatal = 0,
140 	},
141 	{
142 		.errno = EOPNOTSUPP,	/* ICMP_SR_FAILED */
143 		.fatal = 0,
144 	},
145 	{
146 		.errno = ENETUNREACH,	/* ICMP_NET_UNKNOWN */
147 		.fatal = 1,
148 	},
149 	{
150 		.errno = EHOSTDOWN,	/* ICMP_HOST_UNKNOWN */
151 		.fatal = 1,
152 	},
153 	{
154 		.errno = ENONET,	/* ICMP_HOST_ISOLATED */
155 		.fatal = 1,
156 	},
157 	{
158 		.errno = ENETUNREACH,	/* ICMP_NET_ANO	*/
159 		.fatal = 1,
160 	},
161 	{
162 		.errno = EHOSTUNREACH,	/* ICMP_HOST_ANO */
163 		.fatal = 1,
164 	},
165 	{
166 		.errno = ENETUNREACH,	/* ICMP_NET_UNR_TOS */
167 		.fatal = 0,
168 	},
169 	{
170 		.errno = EHOSTUNREACH,	/* ICMP_HOST_UNR_TOS */
171 		.fatal = 0,
172 	},
173 	{
174 		.errno = EHOSTUNREACH,	/* ICMP_PKT_FILTERED */
175 		.fatal = 1,
176 	},
177 	{
178 		.errno = EHOSTUNREACH,	/* ICMP_PREC_VIOLATION */
179 		.fatal = 1,
180 	},
181 	{
182 		.errno = EHOSTUNREACH,	/* ICMP_PREC_CUTOFF */
183 		.fatal = 1,
184 	},
185 };
186 EXPORT_SYMBOL(icmp_err_convert);
187 
188 /*
189  *	ICMP control array. This specifies what to do with each ICMP.
190  */
191 
192 struct icmp_control {
193 	void (*handler)(struct sk_buff *skb);
194 	short   error;		/* This ICMP is classed as an error message */
195 };
196 
197 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
198 
199 /*
200  *	The ICMP socket(s). This is the most convenient way to flow control
201  *	our ICMP output as well as maintain a clean interface throughout
202  *	all layers. All Socketless IP sends will soon be gone.
203  *
204  *	On SMP we have one ICMP socket per-cpu.
205  */
icmp_sk(struct net * net)206 static struct sock *icmp_sk(struct net *net)
207 {
208 	return net->ipv4.icmp_sk[smp_processor_id()];
209 }
210 
icmp_xmit_lock(struct net * net)211 static inline struct sock *icmp_xmit_lock(struct net *net)
212 {
213 	struct sock *sk;
214 
215 	local_bh_disable();
216 
217 	sk = icmp_sk(net);
218 
219 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
220 		/* This can happen if the output path signals a
221 		 * dst_link_failure() for an outgoing ICMP packet.
222 		 */
223 		local_bh_enable();
224 		return NULL;
225 	}
226 	return sk;
227 }
228 
icmp_xmit_unlock(struct sock * sk)229 static inline void icmp_xmit_unlock(struct sock *sk)
230 {
231 	spin_unlock_bh(&sk->sk_lock.slock);
232 }
233 
234 int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
235 int sysctl_icmp_msgs_burst __read_mostly = 50;
236 
237 static struct {
238 	spinlock_t	lock;
239 	u32		credit;
240 	u32		stamp;
241 } icmp_global = {
242 	.lock		= __SPIN_LOCK_UNLOCKED(icmp_global.lock),
243 };
244 
245 /**
246  * icmp_global_allow - Are we allowed to send one more ICMP message ?
247  *
248  * Uses a token bucket to limit our ICMP messages to sysctl_icmp_msgs_per_sec.
249  * Returns false if we reached the limit and can not send another packet.
250  * Note: called with BH disabled
251  */
icmp_global_allow(void)252 bool icmp_global_allow(void)
253 {
254 	u32 credit, delta, incr = 0, now = (u32)jiffies;
255 	bool rc = false;
256 
257 	/* Check if token bucket is empty and cannot be refilled
258 	 * without taking the spinlock.
259 	 */
260 	if (!icmp_global.credit) {
261 		delta = min_t(u32, now - icmp_global.stamp, HZ);
262 		if (delta < HZ / 50)
263 			return false;
264 	}
265 
266 	spin_lock(&icmp_global.lock);
267 	delta = min_t(u32, now - icmp_global.stamp, HZ);
268 	if (delta >= HZ / 50) {
269 		incr = sysctl_icmp_msgs_per_sec * delta / HZ ;
270 		if (incr)
271 			icmp_global.stamp = now;
272 	}
273 	credit = min_t(u32, icmp_global.credit + incr, sysctl_icmp_msgs_burst);
274 	if (credit) {
275 		credit--;
276 		rc = true;
277 	}
278 	icmp_global.credit = credit;
279 	spin_unlock(&icmp_global.lock);
280 	return rc;
281 }
282 EXPORT_SYMBOL(icmp_global_allow);
283 
284 /*
285  *	Send an ICMP frame.
286  */
287 
icmpv4_xrlim_allow(struct net * net,struct rtable * rt,struct flowi4 * fl4,int type,int code)288 static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
289 			       struct flowi4 *fl4, int type, int code)
290 {
291 	struct dst_entry *dst = &rt->dst;
292 	bool rc = true;
293 
294 	if (type > NR_ICMP_TYPES)
295 		goto out;
296 
297 	/* Don't limit PMTU discovery. */
298 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
299 		goto out;
300 
301 	/* No rate limit on loopback */
302 	if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
303 		goto out;
304 
305 	/* Limit if icmp type is enabled in ratemask. */
306 	if (!((1 << type) & net->ipv4.sysctl_icmp_ratemask))
307 		goto out;
308 
309 	rc = false;
310 	if (icmp_global_allow()) {
311 		struct inet_peer *peer;
312 
313 		peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, 1);
314 		rc = inet_peer_xrlim_allow(peer,
315 					   net->ipv4.sysctl_icmp_ratelimit);
316 		if (peer)
317 			inet_putpeer(peer);
318 	}
319 out:
320 	return rc;
321 }
322 
323 /*
324  *	Maintain the counters used in the SNMP statistics for outgoing ICMP
325  */
icmp_out_count(struct net * net,unsigned char type)326 void icmp_out_count(struct net *net, unsigned char type)
327 {
328 	ICMPMSGOUT_INC_STATS(net, type);
329 	ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
330 }
331 
332 /*
333  *	Checksum each fragment, and on the first include the headers and final
334  *	checksum.
335  */
icmp_glue_bits(void * from,char * to,int offset,int len,int odd,struct sk_buff * skb)336 static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
337 			  struct sk_buff *skb)
338 {
339 	struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
340 	__wsum csum;
341 
342 	csum = skb_copy_and_csum_bits(icmp_param->skb,
343 				      icmp_param->offset + offset,
344 				      to, len, 0);
345 
346 	skb->csum = csum_block_add(skb->csum, csum, odd);
347 	if (icmp_pointers[icmp_param->data.icmph.type].error)
348 		nf_ct_attach(skb, icmp_param->skb);
349 	return 0;
350 }
351 
icmp_push_reply(struct icmp_bxm * icmp_param,struct flowi4 * fl4,struct ipcm_cookie * ipc,struct rtable ** rt)352 static void icmp_push_reply(struct icmp_bxm *icmp_param,
353 			    struct flowi4 *fl4,
354 			    struct ipcm_cookie *ipc, struct rtable **rt)
355 {
356 	struct sock *sk;
357 	struct sk_buff *skb;
358 
359 	sk = icmp_sk(dev_net((*rt)->dst.dev));
360 	if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
361 			   icmp_param->data_len+icmp_param->head_len,
362 			   icmp_param->head_len,
363 			   ipc, rt, MSG_DONTWAIT) < 0) {
364 		ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_OUTERRORS);
365 		ip_flush_pending_frames(sk);
366 	} else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
367 		struct icmphdr *icmph = icmp_hdr(skb);
368 		__wsum csum = 0;
369 		struct sk_buff *skb1;
370 
371 		skb_queue_walk(&sk->sk_write_queue, skb1) {
372 			csum = csum_add(csum, skb1->csum);
373 		}
374 		csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
375 						 (char *)icmph,
376 						 icmp_param->head_len, csum);
377 		icmph->checksum = csum_fold(csum);
378 		skb->ip_summed = CHECKSUM_NONE;
379 		ip_push_pending_frames(sk, fl4);
380 	}
381 }
382 
383 /*
384  *	Driving logic for building and sending ICMP messages.
385  */
386 
icmp_reply(struct icmp_bxm * icmp_param,struct sk_buff * skb)387 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
388 {
389 	struct ipcm_cookie ipc;
390 	struct rtable *rt = skb_rtable(skb);
391 	struct net *net = dev_net(rt->dst.dev);
392 	struct flowi4 fl4;
393 	struct sock *sk;
394 	struct inet_sock *inet;
395 	__be32 daddr, saddr;
396 	u32 mark = IP4_REPLY_MARK(net, skb->mark);
397 
398 	if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb))
399 		return;
400 
401 	sk = icmp_xmit_lock(net);
402 	if (sk == NULL)
403 		return;
404 	inet = inet_sk(sk);
405 
406 	icmp_param->data.icmph.checksum = 0;
407 
408 	inet->tos = ip_hdr(skb)->tos;
409 	sk->sk_mark = mark;
410 	daddr = ipc.addr = ip_hdr(skb)->saddr;
411 	saddr = fib_compute_spec_dst(skb);
412 	ipc.opt = NULL;
413 	ipc.tx_flags = 0;
414 	ipc.ttl = 0;
415 	ipc.tos = -1;
416 
417 	if (icmp_param->replyopts.opt.opt.optlen) {
418 		ipc.opt = &icmp_param->replyopts.opt;
419 		if (ipc.opt->opt.srr)
420 			daddr = icmp_param->replyopts.opt.opt.faddr;
421 	}
422 	memset(&fl4, 0, sizeof(fl4));
423 	fl4.daddr = daddr;
424 	fl4.saddr = saddr;
425 	fl4.flowi4_mark = mark;
426 	fl4.flowi4_uid = sock_net_uid(net, NULL);
427 	fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
428 	fl4.flowi4_proto = IPPROTO_ICMP;
429 	security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
430 	rt = ip_route_output_key(net, &fl4);
431 	if (IS_ERR(rt))
432 		goto out_unlock;
433 	if (icmpv4_xrlim_allow(net, rt, &fl4, icmp_param->data.icmph.type,
434 			       icmp_param->data.icmph.code))
435 		icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
436 	ip_rt_put(rt);
437 out_unlock:
438 	icmp_xmit_unlock(sk);
439 }
440 
icmp_route_lookup(struct net * net,struct flowi4 * fl4,struct sk_buff * skb_in,const struct iphdr * iph,__be32 saddr,u8 tos,u32 mark,int type,int code,struct icmp_bxm * param)441 static struct rtable *icmp_route_lookup(struct net *net,
442 					struct flowi4 *fl4,
443 					struct sk_buff *skb_in,
444 					const struct iphdr *iph,
445 					__be32 saddr, u8 tos, u32 mark,
446 					int type, int code,
447 					struct icmp_bxm *param)
448 {
449 	struct rtable *rt, *rt2;
450 	struct flowi4 fl4_dec;
451 	int err;
452 
453 	memset(fl4, 0, sizeof(*fl4));
454 	fl4->daddr = (param->replyopts.opt.opt.srr ?
455 		      param->replyopts.opt.opt.faddr : iph->saddr);
456 	fl4->saddr = saddr;
457 	fl4->flowi4_mark = mark;
458 	fl4->flowi4_uid = sock_net_uid(net, NULL);
459 	fl4->flowi4_tos = RT_TOS(tos);
460 	fl4->flowi4_proto = IPPROTO_ICMP;
461 	fl4->fl4_icmp_type = type;
462 	fl4->fl4_icmp_code = code;
463 	security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
464 	rt = __ip_route_output_key(net, fl4);
465 	if (IS_ERR(rt))
466 		return rt;
467 
468 	/* No need to clone since we're just using its address. */
469 	rt2 = rt;
470 
471 	rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
472 					   flowi4_to_flowi(fl4), NULL, 0);
473 	if (!IS_ERR(rt)) {
474 		if (rt != rt2)
475 			return rt;
476 	} else if (PTR_ERR(rt) == -EPERM) {
477 		rt = NULL;
478 	} else
479 		return rt;
480 
481 	err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
482 	if (err)
483 		goto relookup_failed;
484 
485 	if (inet_addr_type(net, fl4_dec.saddr) == RTN_LOCAL) {
486 		rt2 = __ip_route_output_key(net, &fl4_dec);
487 		if (IS_ERR(rt2))
488 			err = PTR_ERR(rt2);
489 	} else {
490 		struct flowi4 fl4_2 = {};
491 		unsigned long orefdst;
492 
493 		fl4_2.daddr = fl4_dec.saddr;
494 		rt2 = ip_route_output_key(net, &fl4_2);
495 		if (IS_ERR(rt2)) {
496 			err = PTR_ERR(rt2);
497 			goto relookup_failed;
498 		}
499 		/* Ugh! */
500 		orefdst = skb_in->_skb_refdst; /* save old refdst */
501 		err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
502 				     RT_TOS(tos), rt2->dst.dev);
503 
504 		dst_release(&rt2->dst);
505 		rt2 = skb_rtable(skb_in);
506 		skb_in->_skb_refdst = orefdst; /* restore old refdst */
507 	}
508 
509 	if (err)
510 		goto relookup_failed;
511 
512 	rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
513 					    flowi4_to_flowi(&fl4_dec), NULL,
514 					    XFRM_LOOKUP_ICMP);
515 	if (!IS_ERR(rt2)) {
516 		dst_release(&rt->dst);
517 		memcpy(fl4, &fl4_dec, sizeof(*fl4));
518 		rt = rt2;
519 	} else if (PTR_ERR(rt2) == -EPERM) {
520 		if (rt)
521 			dst_release(&rt->dst);
522 		return rt2;
523 	} else {
524 		err = PTR_ERR(rt2);
525 		goto relookup_failed;
526 	}
527 	return rt;
528 
529 relookup_failed:
530 	if (rt)
531 		return rt;
532 	return ERR_PTR(err);
533 }
534 
535 /*
536  *	Send an ICMP message in response to a situation
537  *
538  *	RFC 1122: 3.2.2	MUST send at least the IP header and 8 bytes of header.
539  *		  MAY send more (we do).
540  *			MUST NOT change this header information.
541  *			MUST NOT reply to a multicast/broadcast IP address.
542  *			MUST NOT reply to a multicast/broadcast MAC address.
543  *			MUST reply to only the first fragment.
544  */
545 
icmp_send(struct sk_buff * skb_in,int type,int code,__be32 info)546 void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
547 {
548 	struct iphdr *iph;
549 	int room;
550 	struct icmp_bxm *icmp_param;
551 	struct rtable *rt = skb_rtable(skb_in);
552 	struct ipcm_cookie ipc;
553 	struct flowi4 fl4;
554 	__be32 saddr;
555 	u8  tos;
556 	u32 mark;
557 	struct net *net;
558 	struct sock *sk;
559 
560 	if (!rt)
561 		goto out;
562 	net = dev_net(rt->dst.dev);
563 
564 	/*
565 	 *	Find the original header. It is expected to be valid, of course.
566 	 *	Check this, icmp_send is called from the most obscure devices
567 	 *	sometimes.
568 	 */
569 	iph = ip_hdr(skb_in);
570 
571 	if ((u8 *)iph < skb_in->head ||
572 	    (skb_network_header(skb_in) + sizeof(*iph)) >
573 	    skb_tail_pointer(skb_in))
574 		goto out;
575 
576 	/*
577 	 *	No replies to physical multicast/broadcast
578 	 */
579 	if (skb_in->pkt_type != PACKET_HOST)
580 		goto out;
581 
582 	/*
583 	 *	Now check at the protocol level
584 	 */
585 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
586 		goto out;
587 
588 	/*
589 	 *	Only reply to fragment 0. We byte re-order the constant
590 	 *	mask for efficiency.
591 	 */
592 	if (iph->frag_off & htons(IP_OFFSET))
593 		goto out;
594 
595 	/*
596 	 *	If we send an ICMP error to an ICMP error a mess would result..
597 	 */
598 	if (icmp_pointers[type].error) {
599 		/*
600 		 *	We are an error, check if we are replying to an
601 		 *	ICMP error
602 		 */
603 		if (iph->protocol == IPPROTO_ICMP) {
604 			u8 _inner_type, *itp;
605 
606 			itp = skb_header_pointer(skb_in,
607 						 skb_network_header(skb_in) +
608 						 (iph->ihl << 2) +
609 						 offsetof(struct icmphdr,
610 							  type) -
611 						 skb_in->data,
612 						 sizeof(_inner_type),
613 						 &_inner_type);
614 			if (itp == NULL)
615 				goto out;
616 
617 			/*
618 			 *	Assume any unknown ICMP type is an error. This
619 			 *	isn't specified by the RFC, but think about it..
620 			 */
621 			if (*itp > NR_ICMP_TYPES ||
622 			    icmp_pointers[*itp].error)
623 				goto out;
624 		}
625 	}
626 
627 	icmp_param = kmalloc(sizeof(*icmp_param), GFP_ATOMIC);
628 	if (!icmp_param)
629 		return;
630 
631 	sk = icmp_xmit_lock(net);
632 	if (sk == NULL)
633 		goto out_free;
634 
635 	/*
636 	 *	Construct source address and options.
637 	 */
638 
639 	saddr = iph->daddr;
640 	if (!(rt->rt_flags & RTCF_LOCAL)) {
641 		struct net_device *dev = NULL;
642 
643 		rcu_read_lock();
644 		if (rt_is_input_route(rt) &&
645 		    net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
646 			dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
647 
648 		if (dev)
649 			saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
650 		else
651 			saddr = 0;
652 		rcu_read_unlock();
653 	}
654 
655 	tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
656 					   IPTOS_PREC_INTERNETCONTROL) :
657 					  iph->tos;
658 	mark = IP4_REPLY_MARK(net, skb_in->mark);
659 
660 	if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb_in))
661 		goto out_unlock;
662 
663 
664 	/*
665 	 *	Prepare data for ICMP header.
666 	 */
667 
668 	icmp_param->data.icmph.type	 = type;
669 	icmp_param->data.icmph.code	 = code;
670 	icmp_param->data.icmph.un.gateway = info;
671 	icmp_param->data.icmph.checksum	 = 0;
672 	icmp_param->skb	  = skb_in;
673 	icmp_param->offset = skb_network_offset(skb_in);
674 	inet_sk(sk)->tos = tos;
675 	sk->sk_mark = mark;
676 	ipc.addr = iph->saddr;
677 	ipc.opt = &icmp_param->replyopts.opt;
678 	ipc.tx_flags = 0;
679 	ipc.ttl = 0;
680 	ipc.tos = -1;
681 
682 	rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
683 			       type, code, icmp_param);
684 	if (IS_ERR(rt))
685 		goto out_unlock;
686 
687 	if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
688 		goto ende;
689 
690 	/* RFC says return as much as we can without exceeding 576 bytes. */
691 
692 	room = dst_mtu(&rt->dst);
693 	if (room > 576)
694 		room = 576;
695 	room -= sizeof(struct iphdr) + icmp_param->replyopts.opt.opt.optlen;
696 	room -= sizeof(struct icmphdr);
697 
698 	icmp_param->data_len = skb_in->len - icmp_param->offset;
699 	if (icmp_param->data_len > room)
700 		icmp_param->data_len = room;
701 	icmp_param->head_len = sizeof(struct icmphdr);
702 
703 	icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
704 ende:
705 	ip_rt_put(rt);
706 out_unlock:
707 	icmp_xmit_unlock(sk);
708 out_free:
709 	kfree(icmp_param);
710 out:;
711 }
712 EXPORT_SYMBOL(icmp_send);
713 
714 
icmp_socket_deliver(struct sk_buff * skb,u32 info)715 static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
716 {
717 	const struct iphdr *iph = (const struct iphdr *) skb->data;
718 	const struct net_protocol *ipprot;
719 	int protocol = iph->protocol;
720 
721 	/* Checkin full IP header plus 8 bytes of protocol to
722 	 * avoid additional coding at protocol handlers.
723 	 */
724 	if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
725 		ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
726 		return;
727 	}
728 
729 	raw_icmp_error(skb, protocol, info);
730 
731 	ipprot = rcu_dereference(inet_protos[protocol]);
732 	if (ipprot && ipprot->err_handler)
733 		ipprot->err_handler(skb, info);
734 }
735 
icmp_tag_validation(int proto)736 static bool icmp_tag_validation(int proto)
737 {
738 	bool ok;
739 
740 	rcu_read_lock();
741 	ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
742 	rcu_read_unlock();
743 	return ok;
744 }
745 
746 /*
747  *	Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, ICMP_QUENCH, and
748  *	ICMP_PARAMETERPROB.
749  */
750 
icmp_unreach(struct sk_buff * skb)751 static void icmp_unreach(struct sk_buff *skb)
752 {
753 	const struct iphdr *iph;
754 	struct icmphdr *icmph;
755 	struct net *net;
756 	u32 info = 0;
757 
758 	net = dev_net(skb_dst(skb)->dev);
759 
760 	/*
761 	 *	Incomplete header ?
762 	 * 	Only checks for the IP header, there should be an
763 	 *	additional check for longer headers in upper levels.
764 	 */
765 
766 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
767 		goto out_err;
768 
769 	icmph = icmp_hdr(skb);
770 	iph   = (const struct iphdr *)skb->data;
771 
772 	if (iph->ihl < 5) /* Mangled header, drop. */
773 		goto out_err;
774 
775 	if (icmph->type == ICMP_DEST_UNREACH) {
776 		switch (icmph->code & 15) {
777 		case ICMP_NET_UNREACH:
778 		case ICMP_HOST_UNREACH:
779 		case ICMP_PROT_UNREACH:
780 		case ICMP_PORT_UNREACH:
781 			break;
782 		case ICMP_FRAG_NEEDED:
783 			/* for documentation of the ip_no_pmtu_disc
784 			 * values please see
785 			 * Documentation/networking/ip-sysctl.txt
786 			 */
787 			switch (net->ipv4.sysctl_ip_no_pmtu_disc) {
788 			default:
789 				LIMIT_NETDEBUG(KERN_INFO pr_fmt("%pI4: fragmentation needed and DF set\n"),
790 					       &iph->daddr);
791 				break;
792 			case 2:
793 				goto out;
794 			case 3:
795 				if (!icmp_tag_validation(iph->protocol))
796 					goto out;
797 				/* fall through */
798 			case 0:
799 				info = ntohs(icmph->un.frag.mtu);
800 			}
801 			break;
802 		case ICMP_SR_FAILED:
803 			LIMIT_NETDEBUG(KERN_INFO pr_fmt("%pI4: Source Route Failed\n"),
804 				       &iph->daddr);
805 			break;
806 		default:
807 			break;
808 		}
809 		if (icmph->code > NR_ICMP_UNREACH)
810 			goto out;
811 	} else if (icmph->type == ICMP_PARAMETERPROB)
812 		info = ntohl(icmph->un.gateway) >> 24;
813 
814 	/*
815 	 *	Throw it at our lower layers
816 	 *
817 	 *	RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
818 	 *		  header.
819 	 *	RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
820 	 *		  transport layer.
821 	 *	RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
822 	 *		  transport layer.
823 	 */
824 
825 	/*
826 	 *	Check the other end isn't violating RFC 1122. Some routers send
827 	 *	bogus responses to broadcast frames. If you see this message
828 	 *	first check your netmask matches at both ends, if it does then
829 	 *	get the other vendor to fix their kit.
830 	 */
831 
832 	if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
833 	    inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
834 		net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
835 				     &ip_hdr(skb)->saddr,
836 				     icmph->type, icmph->code,
837 				     &iph->daddr, skb->dev->name);
838 		goto out;
839 	}
840 
841 	icmp_socket_deliver(skb, info);
842 
843 out:
844 	return;
845 out_err:
846 	ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
847 	goto out;
848 }
849 
850 
851 /*
852  *	Handle ICMP_REDIRECT.
853  */
854 
icmp_redirect(struct sk_buff * skb)855 static void icmp_redirect(struct sk_buff *skb)
856 {
857 	if (skb->len < sizeof(struct iphdr)) {
858 		ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
859 		return;
860 	}
861 
862 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
863 		return;
864 
865 	icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
866 }
867 
868 /*
869  *	Handle ICMP_ECHO ("ping") requests.
870  *
871  *	RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
872  *		  requests.
873  *	RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
874  *		  included in the reply.
875  *	RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
876  *		  echo requests, MUST have default=NOT.
877  *	See also WRT handling of options once they are done and working.
878  */
879 
icmp_echo(struct sk_buff * skb)880 static void icmp_echo(struct sk_buff *skb)
881 {
882 	struct net *net;
883 
884 	net = dev_net(skb_dst(skb)->dev);
885 	if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
886 		struct icmp_bxm icmp_param;
887 
888 		icmp_param.data.icmph	   = *icmp_hdr(skb);
889 		icmp_param.data.icmph.type = ICMP_ECHOREPLY;
890 		icmp_param.skb		   = skb;
891 		icmp_param.offset	   = 0;
892 		icmp_param.data_len	   = skb->len;
893 		icmp_param.head_len	   = sizeof(struct icmphdr);
894 		icmp_reply(&icmp_param, skb);
895 	}
896 }
897 
898 /*
899  *	Handle ICMP Timestamp requests.
900  *	RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
901  *		  SHOULD be in the kernel for minimum random latency.
902  *		  MUST be accurate to a few minutes.
903  *		  MUST be updated at least at 15Hz.
904  */
icmp_timestamp(struct sk_buff * skb)905 static void icmp_timestamp(struct sk_buff *skb)
906 {
907 	struct icmp_bxm icmp_param;
908 	/*
909 	 *	Too short.
910 	 */
911 	if (skb->len < 4)
912 		goto out_err;
913 
914 	/*
915 	 *	Fill in the current time as ms since midnight UT:
916 	 */
917 	icmp_param.data.times[1] = inet_current_timestamp();
918 	icmp_param.data.times[2] = icmp_param.data.times[1];
919 	if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
920 		BUG();
921 	icmp_param.data.icmph	   = *icmp_hdr(skb);
922 	icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
923 	icmp_param.data.icmph.code = 0;
924 	icmp_param.skb		   = skb;
925 	icmp_param.offset	   = 0;
926 	icmp_param.data_len	   = 0;
927 	icmp_param.head_len	   = sizeof(struct icmphdr) + 12;
928 	icmp_reply(&icmp_param, skb);
929 out:
930 	return;
931 out_err:
932 	ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
933 	goto out;
934 }
935 
icmp_discard(struct sk_buff * skb)936 static void icmp_discard(struct sk_buff *skb)
937 {
938 }
939 
940 /*
941  *	Deal with incoming ICMP packets.
942  */
icmp_rcv(struct sk_buff * skb)943 int icmp_rcv(struct sk_buff *skb)
944 {
945 	struct icmphdr *icmph;
946 	struct rtable *rt = skb_rtable(skb);
947 	struct net *net = dev_net(rt->dst.dev);
948 
949 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
950 		struct sec_path *sp = skb_sec_path(skb);
951 		int nh;
952 
953 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
954 				 XFRM_STATE_ICMP))
955 			goto drop;
956 
957 		if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
958 			goto drop;
959 
960 		nh = skb_network_offset(skb);
961 		skb_set_network_header(skb, sizeof(*icmph));
962 
963 		if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
964 			goto drop;
965 
966 		skb_set_network_header(skb, nh);
967 	}
968 
969 	ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
970 
971 	if (skb_checksum_simple_validate(skb))
972 		goto csum_error;
973 
974 	if (!pskb_pull(skb, sizeof(*icmph)))
975 		goto error;
976 
977 	icmph = icmp_hdr(skb);
978 
979 	ICMPMSGIN_INC_STATS_BH(net, icmph->type);
980 	/*
981 	 *	18 is the highest 'known' ICMP type. Anything else is a mystery
982 	 *
983 	 *	RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
984 	 *		  discarded.
985 	 */
986 	if (icmph->type > NR_ICMP_TYPES)
987 		goto error;
988 
989 
990 	/*
991 	 *	Parse the ICMP message
992 	 */
993 
994 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
995 		/*
996 		 *	RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
997 		 *	  silently ignored (we let user decide with a sysctl).
998 		 *	RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
999 		 *	  discarded if to broadcast/multicast.
1000 		 */
1001 		if ((icmph->type == ICMP_ECHO ||
1002 		     icmph->type == ICMP_TIMESTAMP) &&
1003 		    net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
1004 			goto error;
1005 		}
1006 		if (icmph->type != ICMP_ECHO &&
1007 		    icmph->type != ICMP_TIMESTAMP &&
1008 		    icmph->type != ICMP_ADDRESS &&
1009 		    icmph->type != ICMP_ADDRESSREPLY) {
1010 			goto error;
1011 		}
1012 	}
1013 
1014 	icmp_pointers[icmph->type].handler(skb);
1015 
1016 drop:
1017 	kfree_skb(skb);
1018 	return 0;
1019 csum_error:
1020 	ICMP_INC_STATS_BH(net, ICMP_MIB_CSUMERRORS);
1021 error:
1022 	ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
1023 	goto drop;
1024 }
1025 
icmp_err(struct sk_buff * skb,u32 info)1026 void icmp_err(struct sk_buff *skb, u32 info)
1027 {
1028 	struct iphdr *iph = (struct iphdr *)skb->data;
1029 	int offset = iph->ihl<<2;
1030 	struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
1031 	int type = icmp_hdr(skb)->type;
1032 	int code = icmp_hdr(skb)->code;
1033 	struct net *net = dev_net(skb->dev);
1034 
1035 	/*
1036 	 * Use ping_err to handle all icmp errors except those
1037 	 * triggered by ICMP_ECHOREPLY which sent from kernel.
1038 	 */
1039 	if (icmph->type != ICMP_ECHOREPLY) {
1040 		ping_err(skb, offset, info);
1041 		return;
1042 	}
1043 
1044 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
1045 		ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ICMP, 0);
1046 	else if (type == ICMP_REDIRECT)
1047 		ipv4_redirect(skb, net, 0, 0, IPPROTO_ICMP, 0);
1048 }
1049 
1050 /*
1051  *	This table is the definition of how we handle ICMP.
1052  */
1053 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
1054 	[ICMP_ECHOREPLY] = {
1055 		.handler = ping_rcv,
1056 	},
1057 	[1] = {
1058 		.handler = icmp_discard,
1059 		.error = 1,
1060 	},
1061 	[2] = {
1062 		.handler = icmp_discard,
1063 		.error = 1,
1064 	},
1065 	[ICMP_DEST_UNREACH] = {
1066 		.handler = icmp_unreach,
1067 		.error = 1,
1068 	},
1069 	[ICMP_SOURCE_QUENCH] = {
1070 		.handler = icmp_unreach,
1071 		.error = 1,
1072 	},
1073 	[ICMP_REDIRECT] = {
1074 		.handler = icmp_redirect,
1075 		.error = 1,
1076 	},
1077 	[6] = {
1078 		.handler = icmp_discard,
1079 		.error = 1,
1080 	},
1081 	[7] = {
1082 		.handler = icmp_discard,
1083 		.error = 1,
1084 	},
1085 	[ICMP_ECHO] = {
1086 		.handler = icmp_echo,
1087 	},
1088 	[9] = {
1089 		.handler = icmp_discard,
1090 		.error = 1,
1091 	},
1092 	[10] = {
1093 		.handler = icmp_discard,
1094 		.error = 1,
1095 	},
1096 	[ICMP_TIME_EXCEEDED] = {
1097 		.handler = icmp_unreach,
1098 		.error = 1,
1099 	},
1100 	[ICMP_PARAMETERPROB] = {
1101 		.handler = icmp_unreach,
1102 		.error = 1,
1103 	},
1104 	[ICMP_TIMESTAMP] = {
1105 		.handler = icmp_timestamp,
1106 	},
1107 	[ICMP_TIMESTAMPREPLY] = {
1108 		.handler = icmp_discard,
1109 	},
1110 	[ICMP_INFO_REQUEST] = {
1111 		.handler = icmp_discard,
1112 	},
1113 	[ICMP_INFO_REPLY] = {
1114 		.handler = icmp_discard,
1115 	},
1116 	[ICMP_ADDRESS] = {
1117 		.handler = icmp_discard,
1118 	},
1119 	[ICMP_ADDRESSREPLY] = {
1120 		.handler = icmp_discard,
1121 	},
1122 };
1123 
icmp_sk_exit(struct net * net)1124 static void __net_exit icmp_sk_exit(struct net *net)
1125 {
1126 	int i;
1127 
1128 	for_each_possible_cpu(i)
1129 		inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
1130 	kfree(net->ipv4.icmp_sk);
1131 	net->ipv4.icmp_sk = NULL;
1132 }
1133 
icmp_sk_init(struct net * net)1134 static int __net_init icmp_sk_init(struct net *net)
1135 {
1136 	int i, err;
1137 
1138 	net->ipv4.icmp_sk =
1139 		kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
1140 	if (net->ipv4.icmp_sk == NULL)
1141 		return -ENOMEM;
1142 
1143 	for_each_possible_cpu(i) {
1144 		struct sock *sk;
1145 
1146 		err = inet_ctl_sock_create(&sk, PF_INET,
1147 					   SOCK_RAW, IPPROTO_ICMP, net);
1148 		if (err < 0)
1149 			goto fail;
1150 
1151 		net->ipv4.icmp_sk[i] = sk;
1152 
1153 		/* Enough space for 2 64K ICMP packets, including
1154 		 * sk_buff/skb_shared_info struct overhead.
1155 		 */
1156 		sk->sk_sndbuf =	2 * SKB_TRUESIZE(64 * 1024);
1157 
1158 		/*
1159 		 * Speedup sock_wfree()
1160 		 */
1161 		sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1162 		inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
1163 	}
1164 
1165 	/* Control parameters for ECHO replies. */
1166 	net->ipv4.sysctl_icmp_echo_ignore_all = 0;
1167 	net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
1168 
1169 	/* Control parameter - ignore bogus broadcast responses? */
1170 	net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
1171 
1172 	/*
1173 	 * 	Configurable global rate limit.
1174 	 *
1175 	 *	ratelimit defines tokens/packet consumed for dst->rate_token
1176 	 *	bucket ratemask defines which icmp types are ratelimited by
1177 	 *	setting	it's bit position.
1178 	 *
1179 	 *	default:
1180 	 *	dest unreachable (3), source quench (4),
1181 	 *	time exceeded (11), parameter problem (12)
1182 	 */
1183 
1184 	net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
1185 	net->ipv4.sysctl_icmp_ratemask = 0x1818;
1186 	net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
1187 
1188 	return 0;
1189 
1190 fail:
1191 	for_each_possible_cpu(i)
1192 		inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
1193 	kfree(net->ipv4.icmp_sk);
1194 	return err;
1195 }
1196 
1197 static struct pernet_operations __net_initdata icmp_sk_ops = {
1198        .init = icmp_sk_init,
1199        .exit = icmp_sk_exit,
1200 };
1201 
icmp_init(void)1202 int __init icmp_init(void)
1203 {
1204 	return register_pernet_subsys(&icmp_sk_ops);
1205 }
1206