xref: /openbmc/linux/net/ipv4/icmp.c (revision 4a2f7083)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	NET3:	Implementation of the ICMP protocol layer.
4  *
5  *		Alan Cox, <alan@lxorguk.ukuu.org.uk>
6  *
7  *	Some of the function names and the icmp unreach table for this
8  *	module were derived from [icmp.c 1.0.11 06/02/93] by
9  *	Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
10  *	Other than that this module is a complete rewrite.
11  *
12  *	Fixes:
13  *	Clemens Fruhwirth	:	introduce global icmp rate limiting
14  *					with icmp type masking ability instead
15  *					of broken per type icmp timeouts.
16  *		Mike Shaver	:	RFC1122 checks.
17  *		Alan Cox	:	Multicast ping reply as self.
18  *		Alan Cox	:	Fix atomicity lockup in ip_build_xmit
19  *					call.
20  *		Alan Cox	:	Added 216,128 byte paths to the MTU
21  *					code.
22  *		Martin Mares	:	RFC1812 checks.
23  *		Martin Mares	:	Can be configured to follow redirects
24  *					if acting as a router _without_ a
25  *					routing protocol (RFC 1812).
26  *		Martin Mares	:	Echo requests may be configured to
27  *					be ignored (RFC 1812).
28  *		Martin Mares	:	Limitation of ICMP error message
29  *					transmit rate (RFC 1812).
30  *		Martin Mares	:	TOS and Precedence set correctly
31  *					(RFC 1812).
32  *		Martin Mares	:	Now copying as much data from the
33  *					original packet as we can without
34  *					exceeding 576 bytes (RFC 1812).
35  *	Willy Konynenberg	:	Transparent proxying support.
36  *		Keith Owens	:	RFC1191 correction for 4.2BSD based
37  *					path MTU bug.
38  *		Thomas Quinot	:	ICMP Dest Unreach codes up to 15 are
39  *					valid (RFC 1812).
40  *		Andi Kleen	:	Check all packet lengths properly
41  *					and moved all kfree_skb() up to
42  *					icmp_rcv.
43  *		Andi Kleen	:	Move the rate limit bookkeeping
44  *					into the dest entry and use a token
45  *					bucket filter (thanks to ANK). Make
46  *					the rates sysctl configurable.
47  *		Yu Tianli	:	Fixed two ugly bugs in icmp_send
48  *					- IP option length was accounted wrongly
49  *					- ICMP header length was not accounted
50  *					  at all.
51  *              Tristan Greaves :       Added sysctl option to ignore bogus
52  *              			broadcast responses from broken routers.
53  *
54  * To Fix:
55  *
56  *	- Should use skb_pull() instead of all the manual checking.
57  *	  This would also greatly simply some upper layer error handlers. --AK
58  */
59 
60 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
61 
62 #include <linux/module.h>
63 #include <linux/types.h>
64 #include <linux/jiffies.h>
65 #include <linux/kernel.h>
66 #include <linux/fcntl.h>
67 #include <linux/socket.h>
68 #include <linux/in.h>
69 #include <linux/inet.h>
70 #include <linux/inetdevice.h>
71 #include <linux/netdevice.h>
72 #include <linux/string.h>
73 #include <linux/netfilter_ipv4.h>
74 #include <linux/slab.h>
75 #include <net/snmp.h>
76 #include <net/ip.h>
77 #include <net/route.h>
78 #include <net/protocol.h>
79 #include <net/icmp.h>
80 #include <net/tcp.h>
81 #include <net/udp.h>
82 #include <net/raw.h>
83 #include <net/ping.h>
84 #include <linux/skbuff.h>
85 #include <net/sock.h>
86 #include <linux/errno.h>
87 #include <linux/timer.h>
88 #include <linux/init.h>
89 #include <linux/uaccess.h>
90 #include <net/checksum.h>
91 #include <net/xfrm.h>
92 #include <net/inet_common.h>
93 #include <net/ip_fib.h>
94 #include <net/l3mdev.h>
95 
96 /*
97  *	Build xmit assembly blocks
98  */
99 
100 struct icmp_bxm {
101 	struct sk_buff *skb;
102 	int offset;
103 	int data_len;
104 
105 	struct {
106 		struct icmphdr icmph;
107 		__be32	       times[3];
108 	} data;
109 	int head_len;
110 	struct ip_options_data replyopts;
111 };
112 
113 /* An array of errno for error messages from dest unreach. */
114 /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
115 
116 const struct icmp_err icmp_err_convert[] = {
117 	{
118 		.errno = ENETUNREACH,	/* ICMP_NET_UNREACH */
119 		.fatal = 0,
120 	},
121 	{
122 		.errno = EHOSTUNREACH,	/* ICMP_HOST_UNREACH */
123 		.fatal = 0,
124 	},
125 	{
126 		.errno = ENOPROTOOPT	/* ICMP_PROT_UNREACH */,
127 		.fatal = 1,
128 	},
129 	{
130 		.errno = ECONNREFUSED,	/* ICMP_PORT_UNREACH */
131 		.fatal = 1,
132 	},
133 	{
134 		.errno = EMSGSIZE,	/* ICMP_FRAG_NEEDED */
135 		.fatal = 0,
136 	},
137 	{
138 		.errno = EOPNOTSUPP,	/* ICMP_SR_FAILED */
139 		.fatal = 0,
140 	},
141 	{
142 		.errno = ENETUNREACH,	/* ICMP_NET_UNKNOWN */
143 		.fatal = 1,
144 	},
145 	{
146 		.errno = EHOSTDOWN,	/* ICMP_HOST_UNKNOWN */
147 		.fatal = 1,
148 	},
149 	{
150 		.errno = ENONET,	/* ICMP_HOST_ISOLATED */
151 		.fatal = 1,
152 	},
153 	{
154 		.errno = ENETUNREACH,	/* ICMP_NET_ANO	*/
155 		.fatal = 1,
156 	},
157 	{
158 		.errno = EHOSTUNREACH,	/* ICMP_HOST_ANO */
159 		.fatal = 1,
160 	},
161 	{
162 		.errno = ENETUNREACH,	/* ICMP_NET_UNR_TOS */
163 		.fatal = 0,
164 	},
165 	{
166 		.errno = EHOSTUNREACH,	/* ICMP_HOST_UNR_TOS */
167 		.fatal = 0,
168 	},
169 	{
170 		.errno = EHOSTUNREACH,	/* ICMP_PKT_FILTERED */
171 		.fatal = 1,
172 	},
173 	{
174 		.errno = EHOSTUNREACH,	/* ICMP_PREC_VIOLATION */
175 		.fatal = 1,
176 	},
177 	{
178 		.errno = EHOSTUNREACH,	/* ICMP_PREC_CUTOFF */
179 		.fatal = 1,
180 	},
181 };
182 EXPORT_SYMBOL(icmp_err_convert);
183 
184 /*
185  *	ICMP control array. This specifies what to do with each ICMP.
186  */
187 
188 struct icmp_control {
189 	enum skb_drop_reason (*handler)(struct sk_buff *skb);
190 	short   error;		/* This ICMP is classed as an error message */
191 };
192 
193 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
194 
195 static DEFINE_PER_CPU(struct sock *, ipv4_icmp_sk);
196 
197 /* Called with BH disabled */
198 static inline struct sock *icmp_xmit_lock(struct net *net)
199 {
200 	struct sock *sk;
201 
202 	sk = this_cpu_read(ipv4_icmp_sk);
203 
204 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
205 		/* This can happen if the output path signals a
206 		 * dst_link_failure() for an outgoing ICMP packet.
207 		 */
208 		return NULL;
209 	}
210 	sock_net_set(sk, net);
211 	return sk;
212 }
213 
214 static inline void icmp_xmit_unlock(struct sock *sk)
215 {
216 	sock_net_set(sk, &init_net);
217 	spin_unlock(&sk->sk_lock.slock);
218 }
219 
220 int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
221 int sysctl_icmp_msgs_burst __read_mostly = 50;
222 
223 static struct {
224 	spinlock_t	lock;
225 	u32		credit;
226 	u32		stamp;
227 } icmp_global = {
228 	.lock		= __SPIN_LOCK_UNLOCKED(icmp_global.lock),
229 };
230 
231 /**
232  * icmp_global_allow - Are we allowed to send one more ICMP message ?
233  *
234  * Uses a token bucket to limit our ICMP messages to ~sysctl_icmp_msgs_per_sec.
235  * Returns false if we reached the limit and can not send another packet.
236  * Note: called with BH disabled
237  */
238 bool icmp_global_allow(void)
239 {
240 	u32 credit, delta, incr = 0, now = (u32)jiffies;
241 	bool rc = false;
242 
243 	/* Check if token bucket is empty and cannot be refilled
244 	 * without taking the spinlock. The READ_ONCE() are paired
245 	 * with the following WRITE_ONCE() in this same function.
246 	 */
247 	if (!READ_ONCE(icmp_global.credit)) {
248 		delta = min_t(u32, now - READ_ONCE(icmp_global.stamp), HZ);
249 		if (delta < HZ / 50)
250 			return false;
251 	}
252 
253 	spin_lock(&icmp_global.lock);
254 	delta = min_t(u32, now - icmp_global.stamp, HZ);
255 	if (delta >= HZ / 50) {
256 		incr = READ_ONCE(sysctl_icmp_msgs_per_sec) * delta / HZ;
257 		if (incr)
258 			WRITE_ONCE(icmp_global.stamp, now);
259 	}
260 	credit = min_t(u32, icmp_global.credit + incr,
261 		       READ_ONCE(sysctl_icmp_msgs_burst));
262 	if (credit) {
263 		/* We want to use a credit of one in average, but need to randomize
264 		 * it for security reasons.
265 		 */
266 		credit = max_t(int, credit - prandom_u32_max(3), 0);
267 		rc = true;
268 	}
269 	WRITE_ONCE(icmp_global.credit, credit);
270 	spin_unlock(&icmp_global.lock);
271 	return rc;
272 }
273 EXPORT_SYMBOL(icmp_global_allow);
274 
275 static bool icmpv4_mask_allow(struct net *net, int type, int code)
276 {
277 	if (type > NR_ICMP_TYPES)
278 		return true;
279 
280 	/* Don't limit PMTU discovery. */
281 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
282 		return true;
283 
284 	/* Limit if icmp type is enabled in ratemask. */
285 	if (!((1 << type) & net->ipv4.sysctl_icmp_ratemask))
286 		return true;
287 
288 	return false;
289 }
290 
291 static bool icmpv4_global_allow(struct net *net, int type, int code)
292 {
293 	if (icmpv4_mask_allow(net, type, code))
294 		return true;
295 
296 	if (icmp_global_allow())
297 		return true;
298 
299 	return false;
300 }
301 
302 /*
303  *	Send an ICMP frame.
304  */
305 
306 static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
307 			       struct flowi4 *fl4, int type, int code)
308 {
309 	struct dst_entry *dst = &rt->dst;
310 	struct inet_peer *peer;
311 	bool rc = true;
312 	int vif;
313 
314 	if (icmpv4_mask_allow(net, type, code))
315 		goto out;
316 
317 	/* No rate limit on loopback */
318 	if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
319 		goto out;
320 
321 	vif = l3mdev_master_ifindex(dst->dev);
322 	peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, vif, 1);
323 	rc = inet_peer_xrlim_allow(peer, net->ipv4.sysctl_icmp_ratelimit);
324 	if (peer)
325 		inet_putpeer(peer);
326 out:
327 	return rc;
328 }
329 
330 /*
331  *	Maintain the counters used in the SNMP statistics for outgoing ICMP
332  */
333 void icmp_out_count(struct net *net, unsigned char type)
334 {
335 	ICMPMSGOUT_INC_STATS(net, type);
336 	ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
337 }
338 
339 /*
340  *	Checksum each fragment, and on the first include the headers and final
341  *	checksum.
342  */
343 static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
344 			  struct sk_buff *skb)
345 {
346 	struct icmp_bxm *icmp_param = from;
347 	__wsum csum;
348 
349 	csum = skb_copy_and_csum_bits(icmp_param->skb,
350 				      icmp_param->offset + offset,
351 				      to, len);
352 
353 	skb->csum = csum_block_add(skb->csum, csum, odd);
354 	if (icmp_pointers[icmp_param->data.icmph.type].error)
355 		nf_ct_attach(skb, icmp_param->skb);
356 	return 0;
357 }
358 
359 static void icmp_push_reply(struct sock *sk,
360 			    struct icmp_bxm *icmp_param,
361 			    struct flowi4 *fl4,
362 			    struct ipcm_cookie *ipc, struct rtable **rt)
363 {
364 	struct sk_buff *skb;
365 
366 	if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
367 			   icmp_param->data_len+icmp_param->head_len,
368 			   icmp_param->head_len,
369 			   ipc, rt, MSG_DONTWAIT) < 0) {
370 		__ICMP_INC_STATS(sock_net(sk), ICMP_MIB_OUTERRORS);
371 		ip_flush_pending_frames(sk);
372 	} else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
373 		struct icmphdr *icmph = icmp_hdr(skb);
374 		__wsum csum;
375 		struct sk_buff *skb1;
376 
377 		csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
378 						 (char *)icmph,
379 						 icmp_param->head_len);
380 		skb_queue_walk(&sk->sk_write_queue, skb1) {
381 			csum = csum_add(csum, skb1->csum);
382 		}
383 		icmph->checksum = csum_fold(csum);
384 		skb->ip_summed = CHECKSUM_NONE;
385 		ip_push_pending_frames(sk, fl4);
386 	}
387 }
388 
389 /*
390  *	Driving logic for building and sending ICMP messages.
391  */
392 
393 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
394 {
395 	struct ipcm_cookie ipc;
396 	struct rtable *rt = skb_rtable(skb);
397 	struct net *net = dev_net(rt->dst.dev);
398 	struct flowi4 fl4;
399 	struct sock *sk;
400 	struct inet_sock *inet;
401 	__be32 daddr, saddr;
402 	u32 mark = IP4_REPLY_MARK(net, skb->mark);
403 	int type = icmp_param->data.icmph.type;
404 	int code = icmp_param->data.icmph.code;
405 
406 	if (ip_options_echo(net, &icmp_param->replyopts.opt.opt, skb))
407 		return;
408 
409 	/* Needed by both icmp_global_allow and icmp_xmit_lock */
410 	local_bh_disable();
411 
412 	/* global icmp_msgs_per_sec */
413 	if (!icmpv4_global_allow(net, type, code))
414 		goto out_bh_enable;
415 
416 	sk = icmp_xmit_lock(net);
417 	if (!sk)
418 		goto out_bh_enable;
419 	inet = inet_sk(sk);
420 
421 	icmp_param->data.icmph.checksum = 0;
422 
423 	ipcm_init(&ipc);
424 	inet->tos = ip_hdr(skb)->tos;
425 	ipc.sockc.mark = mark;
426 	daddr = ipc.addr = ip_hdr(skb)->saddr;
427 	saddr = fib_compute_spec_dst(skb);
428 
429 	if (icmp_param->replyopts.opt.opt.optlen) {
430 		ipc.opt = &icmp_param->replyopts.opt;
431 		if (ipc.opt->opt.srr)
432 			daddr = icmp_param->replyopts.opt.opt.faddr;
433 	}
434 	memset(&fl4, 0, sizeof(fl4));
435 	fl4.daddr = daddr;
436 	fl4.saddr = saddr;
437 	fl4.flowi4_mark = mark;
438 	fl4.flowi4_uid = sock_net_uid(net, NULL);
439 	fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
440 	fl4.flowi4_proto = IPPROTO_ICMP;
441 	fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
442 	security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
443 	rt = ip_route_output_key(net, &fl4);
444 	if (IS_ERR(rt))
445 		goto out_unlock;
446 	if (icmpv4_xrlim_allow(net, rt, &fl4, type, code))
447 		icmp_push_reply(sk, icmp_param, &fl4, &ipc, &rt);
448 	ip_rt_put(rt);
449 out_unlock:
450 	icmp_xmit_unlock(sk);
451 out_bh_enable:
452 	local_bh_enable();
453 }
454 
455 /*
456  * The device used for looking up which routing table to use for sending an ICMP
457  * error is preferably the source whenever it is set, which should ensure the
458  * icmp error can be sent to the source host, else lookup using the routing
459  * table of the destination device, else use the main routing table (index 0).
460  */
461 static struct net_device *icmp_get_route_lookup_dev(struct sk_buff *skb)
462 {
463 	struct net_device *route_lookup_dev = NULL;
464 
465 	if (skb->dev)
466 		route_lookup_dev = skb->dev;
467 	else if (skb_dst(skb))
468 		route_lookup_dev = skb_dst(skb)->dev;
469 	return route_lookup_dev;
470 }
471 
472 static struct rtable *icmp_route_lookup(struct net *net,
473 					struct flowi4 *fl4,
474 					struct sk_buff *skb_in,
475 					const struct iphdr *iph,
476 					__be32 saddr, u8 tos, u32 mark,
477 					int type, int code,
478 					struct icmp_bxm *param)
479 {
480 	struct net_device *route_lookup_dev;
481 	struct rtable *rt, *rt2;
482 	struct flowi4 fl4_dec;
483 	int err;
484 
485 	memset(fl4, 0, sizeof(*fl4));
486 	fl4->daddr = (param->replyopts.opt.opt.srr ?
487 		      param->replyopts.opt.opt.faddr : iph->saddr);
488 	fl4->saddr = saddr;
489 	fl4->flowi4_mark = mark;
490 	fl4->flowi4_uid = sock_net_uid(net, NULL);
491 	fl4->flowi4_tos = RT_TOS(tos);
492 	fl4->flowi4_proto = IPPROTO_ICMP;
493 	fl4->fl4_icmp_type = type;
494 	fl4->fl4_icmp_code = code;
495 	route_lookup_dev = icmp_get_route_lookup_dev(skb_in);
496 	fl4->flowi4_oif = l3mdev_master_ifindex(route_lookup_dev);
497 
498 	security_skb_classify_flow(skb_in, flowi4_to_flowi_common(fl4));
499 	rt = ip_route_output_key_hash(net, fl4, skb_in);
500 	if (IS_ERR(rt))
501 		return rt;
502 
503 	/* No need to clone since we're just using its address. */
504 	rt2 = rt;
505 
506 	rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
507 					   flowi4_to_flowi(fl4), NULL, 0);
508 	if (!IS_ERR(rt)) {
509 		if (rt != rt2)
510 			return rt;
511 	} else if (PTR_ERR(rt) == -EPERM) {
512 		rt = NULL;
513 	} else
514 		return rt;
515 
516 	err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
517 	if (err)
518 		goto relookup_failed;
519 
520 	if (inet_addr_type_dev_table(net, route_lookup_dev,
521 				     fl4_dec.saddr) == RTN_LOCAL) {
522 		rt2 = __ip_route_output_key(net, &fl4_dec);
523 		if (IS_ERR(rt2))
524 			err = PTR_ERR(rt2);
525 	} else {
526 		struct flowi4 fl4_2 = {};
527 		unsigned long orefdst;
528 
529 		fl4_2.daddr = fl4_dec.saddr;
530 		rt2 = ip_route_output_key(net, &fl4_2);
531 		if (IS_ERR(rt2)) {
532 			err = PTR_ERR(rt2);
533 			goto relookup_failed;
534 		}
535 		/* Ugh! */
536 		orefdst = skb_in->_skb_refdst; /* save old refdst */
537 		skb_dst_set(skb_in, NULL);
538 		err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
539 				     RT_TOS(tos), rt2->dst.dev);
540 
541 		dst_release(&rt2->dst);
542 		rt2 = skb_rtable(skb_in);
543 		skb_in->_skb_refdst = orefdst; /* restore old refdst */
544 	}
545 
546 	if (err)
547 		goto relookup_failed;
548 
549 	rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
550 					    flowi4_to_flowi(&fl4_dec), NULL,
551 					    XFRM_LOOKUP_ICMP);
552 	if (!IS_ERR(rt2)) {
553 		dst_release(&rt->dst);
554 		memcpy(fl4, &fl4_dec, sizeof(*fl4));
555 		rt = rt2;
556 	} else if (PTR_ERR(rt2) == -EPERM) {
557 		if (rt)
558 			dst_release(&rt->dst);
559 		return rt2;
560 	} else {
561 		err = PTR_ERR(rt2);
562 		goto relookup_failed;
563 	}
564 	return rt;
565 
566 relookup_failed:
567 	if (rt)
568 		return rt;
569 	return ERR_PTR(err);
570 }
571 
572 /*
573  *	Send an ICMP message in response to a situation
574  *
575  *	RFC 1122: 3.2.2	MUST send at least the IP header and 8 bytes of header.
576  *		  MAY send more (we do).
577  *			MUST NOT change this header information.
578  *			MUST NOT reply to a multicast/broadcast IP address.
579  *			MUST NOT reply to a multicast/broadcast MAC address.
580  *			MUST reply to only the first fragment.
581  */
582 
583 void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info,
584 		 const struct ip_options *opt)
585 {
586 	struct iphdr *iph;
587 	int room;
588 	struct icmp_bxm icmp_param;
589 	struct rtable *rt = skb_rtable(skb_in);
590 	struct ipcm_cookie ipc;
591 	struct flowi4 fl4;
592 	__be32 saddr;
593 	u8  tos;
594 	u32 mark;
595 	struct net *net;
596 	struct sock *sk;
597 
598 	if (!rt)
599 		goto out;
600 
601 	if (rt->dst.dev)
602 		net = dev_net(rt->dst.dev);
603 	else if (skb_in->dev)
604 		net = dev_net(skb_in->dev);
605 	else
606 		goto out;
607 
608 	/*
609 	 *	Find the original header. It is expected to be valid, of course.
610 	 *	Check this, icmp_send is called from the most obscure devices
611 	 *	sometimes.
612 	 */
613 	iph = ip_hdr(skb_in);
614 
615 	if ((u8 *)iph < skb_in->head ||
616 	    (skb_network_header(skb_in) + sizeof(*iph)) >
617 	    skb_tail_pointer(skb_in))
618 		goto out;
619 
620 	/*
621 	 *	No replies to physical multicast/broadcast
622 	 */
623 	if (skb_in->pkt_type != PACKET_HOST)
624 		goto out;
625 
626 	/*
627 	 *	Now check at the protocol level
628 	 */
629 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
630 		goto out;
631 
632 	/*
633 	 *	Only reply to fragment 0. We byte re-order the constant
634 	 *	mask for efficiency.
635 	 */
636 	if (iph->frag_off & htons(IP_OFFSET))
637 		goto out;
638 
639 	/*
640 	 *	If we send an ICMP error to an ICMP error a mess would result..
641 	 */
642 	if (icmp_pointers[type].error) {
643 		/*
644 		 *	We are an error, check if we are replying to an
645 		 *	ICMP error
646 		 */
647 		if (iph->protocol == IPPROTO_ICMP) {
648 			u8 _inner_type, *itp;
649 
650 			itp = skb_header_pointer(skb_in,
651 						 skb_network_header(skb_in) +
652 						 (iph->ihl << 2) +
653 						 offsetof(struct icmphdr,
654 							  type) -
655 						 skb_in->data,
656 						 sizeof(_inner_type),
657 						 &_inner_type);
658 			if (!itp)
659 				goto out;
660 
661 			/*
662 			 *	Assume any unknown ICMP type is an error. This
663 			 *	isn't specified by the RFC, but think about it..
664 			 */
665 			if (*itp > NR_ICMP_TYPES ||
666 			    icmp_pointers[*itp].error)
667 				goto out;
668 		}
669 	}
670 
671 	/* Needed by both icmp_global_allow and icmp_xmit_lock */
672 	local_bh_disable();
673 
674 	/* Check global sysctl_icmp_msgs_per_sec ratelimit, unless
675 	 * incoming dev is loopback.  If outgoing dev change to not be
676 	 * loopback, then peer ratelimit still work (in icmpv4_xrlim_allow)
677 	 */
678 	if (!(skb_in->dev && (skb_in->dev->flags&IFF_LOOPBACK)) &&
679 	      !icmpv4_global_allow(net, type, code))
680 		goto out_bh_enable;
681 
682 	sk = icmp_xmit_lock(net);
683 	if (!sk)
684 		goto out_bh_enable;
685 
686 	/*
687 	 *	Construct source address and options.
688 	 */
689 
690 	saddr = iph->daddr;
691 	if (!(rt->rt_flags & RTCF_LOCAL)) {
692 		struct net_device *dev = NULL;
693 
694 		rcu_read_lock();
695 		if (rt_is_input_route(rt) &&
696 		    net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
697 			dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
698 
699 		if (dev)
700 			saddr = inet_select_addr(dev, iph->saddr,
701 						 RT_SCOPE_LINK);
702 		else
703 			saddr = 0;
704 		rcu_read_unlock();
705 	}
706 
707 	tos = icmp_pointers[type].error ? (RT_TOS(iph->tos) |
708 					   IPTOS_PREC_INTERNETCONTROL) :
709 					   iph->tos;
710 	mark = IP4_REPLY_MARK(net, skb_in->mark);
711 
712 	if (__ip_options_echo(net, &icmp_param.replyopts.opt.opt, skb_in, opt))
713 		goto out_unlock;
714 
715 
716 	/*
717 	 *	Prepare data for ICMP header.
718 	 */
719 
720 	icmp_param.data.icmph.type	 = type;
721 	icmp_param.data.icmph.code	 = code;
722 	icmp_param.data.icmph.un.gateway = info;
723 	icmp_param.data.icmph.checksum	 = 0;
724 	icmp_param.skb	  = skb_in;
725 	icmp_param.offset = skb_network_offset(skb_in);
726 	inet_sk(sk)->tos = tos;
727 	ipcm_init(&ipc);
728 	ipc.addr = iph->saddr;
729 	ipc.opt = &icmp_param.replyopts.opt;
730 	ipc.sockc.mark = mark;
731 
732 	rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
733 			       type, code, &icmp_param);
734 	if (IS_ERR(rt))
735 		goto out_unlock;
736 
737 	/* peer icmp_ratelimit */
738 	if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
739 		goto ende;
740 
741 	/* RFC says return as much as we can without exceeding 576 bytes. */
742 
743 	room = dst_mtu(&rt->dst);
744 	if (room > 576)
745 		room = 576;
746 	room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
747 	room -= sizeof(struct icmphdr);
748 
749 	icmp_param.data_len = skb_in->len - icmp_param.offset;
750 	if (icmp_param.data_len > room)
751 		icmp_param.data_len = room;
752 	icmp_param.head_len = sizeof(struct icmphdr);
753 
754 	/* if we don't have a source address at this point, fall back to the
755 	 * dummy address instead of sending out a packet with a source address
756 	 * of 0.0.0.0
757 	 */
758 	if (!fl4.saddr)
759 		fl4.saddr = htonl(INADDR_DUMMY);
760 
761 	icmp_push_reply(sk, &icmp_param, &fl4, &ipc, &rt);
762 ende:
763 	ip_rt_put(rt);
764 out_unlock:
765 	icmp_xmit_unlock(sk);
766 out_bh_enable:
767 	local_bh_enable();
768 out:;
769 }
770 EXPORT_SYMBOL(__icmp_send);
771 
772 #if IS_ENABLED(CONFIG_NF_NAT)
773 #include <net/netfilter/nf_conntrack.h>
774 void icmp_ndo_send(struct sk_buff *skb_in, int type, int code, __be32 info)
775 {
776 	struct sk_buff *cloned_skb = NULL;
777 	struct ip_options opts = { 0 };
778 	enum ip_conntrack_info ctinfo;
779 	struct nf_conn *ct;
780 	__be32 orig_ip;
781 
782 	ct = nf_ct_get(skb_in, &ctinfo);
783 	if (!ct || !(ct->status & IPS_SRC_NAT)) {
784 		__icmp_send(skb_in, type, code, info, &opts);
785 		return;
786 	}
787 
788 	if (skb_shared(skb_in))
789 		skb_in = cloned_skb = skb_clone(skb_in, GFP_ATOMIC);
790 
791 	if (unlikely(!skb_in || skb_network_header(skb_in) < skb_in->head ||
792 	    (skb_network_header(skb_in) + sizeof(struct iphdr)) >
793 	    skb_tail_pointer(skb_in) || skb_ensure_writable(skb_in,
794 	    skb_network_offset(skb_in) + sizeof(struct iphdr))))
795 		goto out;
796 
797 	orig_ip = ip_hdr(skb_in)->saddr;
798 	ip_hdr(skb_in)->saddr = ct->tuplehash[0].tuple.src.u3.ip;
799 	__icmp_send(skb_in, type, code, info, &opts);
800 	ip_hdr(skb_in)->saddr = orig_ip;
801 out:
802 	consume_skb(cloned_skb);
803 }
804 EXPORT_SYMBOL(icmp_ndo_send);
805 #endif
806 
807 static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
808 {
809 	const struct iphdr *iph = (const struct iphdr *)skb->data;
810 	const struct net_protocol *ipprot;
811 	int protocol = iph->protocol;
812 
813 	/* Checkin full IP header plus 8 bytes of protocol to
814 	 * avoid additional coding at protocol handlers.
815 	 */
816 	if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
817 		__ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
818 		return;
819 	}
820 
821 	raw_icmp_error(skb, protocol, info);
822 
823 	ipprot = rcu_dereference(inet_protos[protocol]);
824 	if (ipprot && ipprot->err_handler)
825 		ipprot->err_handler(skb, info);
826 }
827 
828 static bool icmp_tag_validation(int proto)
829 {
830 	bool ok;
831 
832 	rcu_read_lock();
833 	ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
834 	rcu_read_unlock();
835 	return ok;
836 }
837 
838 /*
839  *	Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEEDED, ICMP_QUENCH, and
840  *	ICMP_PARAMETERPROB.
841  */
842 
843 static enum skb_drop_reason icmp_unreach(struct sk_buff *skb)
844 {
845 	enum skb_drop_reason reason = SKB_NOT_DROPPED_YET;
846 	const struct iphdr *iph;
847 	struct icmphdr *icmph;
848 	struct net *net;
849 	u32 info = 0;
850 
851 	net = dev_net(skb_dst(skb)->dev);
852 
853 	/*
854 	 *	Incomplete header ?
855 	 * 	Only checks for the IP header, there should be an
856 	 *	additional check for longer headers in upper levels.
857 	 */
858 
859 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
860 		goto out_err;
861 
862 	icmph = icmp_hdr(skb);
863 	iph   = (const struct iphdr *)skb->data;
864 
865 	if (iph->ihl < 5)  { /* Mangled header, drop. */
866 		reason = SKB_DROP_REASON_IP_INHDR;
867 		goto out_err;
868 	}
869 
870 	switch (icmph->type) {
871 	case ICMP_DEST_UNREACH:
872 		switch (icmph->code & 15) {
873 		case ICMP_NET_UNREACH:
874 		case ICMP_HOST_UNREACH:
875 		case ICMP_PROT_UNREACH:
876 		case ICMP_PORT_UNREACH:
877 			break;
878 		case ICMP_FRAG_NEEDED:
879 			/* for documentation of the ip_no_pmtu_disc
880 			 * values please see
881 			 * Documentation/networking/ip-sysctl.rst
882 			 */
883 			switch (net->ipv4.sysctl_ip_no_pmtu_disc) {
884 			default:
885 				net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
886 						    &iph->daddr);
887 				break;
888 			case 2:
889 				goto out;
890 			case 3:
891 				if (!icmp_tag_validation(iph->protocol))
892 					goto out;
893 				fallthrough;
894 			case 0:
895 				info = ntohs(icmph->un.frag.mtu);
896 			}
897 			break;
898 		case ICMP_SR_FAILED:
899 			net_dbg_ratelimited("%pI4: Source Route Failed\n",
900 					    &iph->daddr);
901 			break;
902 		default:
903 			break;
904 		}
905 		if (icmph->code > NR_ICMP_UNREACH)
906 			goto out;
907 		break;
908 	case ICMP_PARAMETERPROB:
909 		info = ntohl(icmph->un.gateway) >> 24;
910 		break;
911 	case ICMP_TIME_EXCEEDED:
912 		__ICMP_INC_STATS(net, ICMP_MIB_INTIMEEXCDS);
913 		if (icmph->code == ICMP_EXC_FRAGTIME)
914 			goto out;
915 		break;
916 	}
917 
918 	/*
919 	 *	Throw it at our lower layers
920 	 *
921 	 *	RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
922 	 *		  header.
923 	 *	RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
924 	 *		  transport layer.
925 	 *	RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
926 	 *		  transport layer.
927 	 */
928 
929 	/*
930 	 *	Check the other end isn't violating RFC 1122. Some routers send
931 	 *	bogus responses to broadcast frames. If you see this message
932 	 *	first check your netmask matches at both ends, if it does then
933 	 *	get the other vendor to fix their kit.
934 	 */
935 
936 	if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
937 	    inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
938 		net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
939 				     &ip_hdr(skb)->saddr,
940 				     icmph->type, icmph->code,
941 				     &iph->daddr, skb->dev->name);
942 		goto out;
943 	}
944 
945 	icmp_socket_deliver(skb, info);
946 
947 out:
948 	return reason;
949 out_err:
950 	__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
951 	return reason ?: SKB_DROP_REASON_NOT_SPECIFIED;
952 }
953 
954 
955 /*
956  *	Handle ICMP_REDIRECT.
957  */
958 
959 static enum skb_drop_reason icmp_redirect(struct sk_buff *skb)
960 {
961 	if (skb->len < sizeof(struct iphdr)) {
962 		__ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
963 		return SKB_DROP_REASON_PKT_TOO_SMALL;
964 	}
965 
966 	if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
967 		/* there aught to be a stat */
968 		return SKB_DROP_REASON_NOMEM;
969 	}
970 
971 	icmp_socket_deliver(skb, ntohl(icmp_hdr(skb)->un.gateway));
972 	return SKB_NOT_DROPPED_YET;
973 }
974 
975 /*
976  *	Handle ICMP_ECHO ("ping") and ICMP_EXT_ECHO ("PROBE") requests.
977  *
978  *	RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
979  *		  requests.
980  *	RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
981  *		  included in the reply.
982  *	RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
983  *		  echo requests, MUST have default=NOT.
984  *	RFC 8335: 8 MUST have a config option to enable/disable ICMP
985  *		  Extended Echo Functionality, MUST be disabled by default
986  *	See also WRT handling of options once they are done and working.
987  */
988 
989 static enum skb_drop_reason icmp_echo(struct sk_buff *skb)
990 {
991 	struct icmp_bxm icmp_param;
992 	struct net *net;
993 
994 	net = dev_net(skb_dst(skb)->dev);
995 	/* should there be an ICMP stat for ignored echos? */
996 	if (READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_all))
997 		return SKB_NOT_DROPPED_YET;
998 
999 	icmp_param.data.icmph	   = *icmp_hdr(skb);
1000 	icmp_param.skb		   = skb;
1001 	icmp_param.offset	   = 0;
1002 	icmp_param.data_len	   = skb->len;
1003 	icmp_param.head_len	   = sizeof(struct icmphdr);
1004 
1005 	if (icmp_param.data.icmph.type == ICMP_ECHO)
1006 		icmp_param.data.icmph.type = ICMP_ECHOREPLY;
1007 	else if (!icmp_build_probe(skb, &icmp_param.data.icmph))
1008 		return SKB_NOT_DROPPED_YET;
1009 
1010 	icmp_reply(&icmp_param, skb);
1011 	return SKB_NOT_DROPPED_YET;
1012 }
1013 
1014 /*	Helper for icmp_echo and icmpv6_echo_reply.
1015  *	Searches for net_device that matches PROBE interface identifier
1016  *		and builds PROBE reply message in icmphdr.
1017  *
1018  *	Returns false if PROBE responses are disabled via sysctl
1019  */
1020 
1021 bool icmp_build_probe(struct sk_buff *skb, struct icmphdr *icmphdr)
1022 {
1023 	struct icmp_ext_hdr *ext_hdr, _ext_hdr;
1024 	struct icmp_ext_echo_iio *iio, _iio;
1025 	struct net *net = dev_net(skb->dev);
1026 	struct net_device *dev;
1027 	char buff[IFNAMSIZ];
1028 	u16 ident_len;
1029 	u8 status;
1030 
1031 	if (!READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe))
1032 		return false;
1033 
1034 	/* We currently only support probing interfaces on the proxy node
1035 	 * Check to ensure L-bit is set
1036 	 */
1037 	if (!(ntohs(icmphdr->un.echo.sequence) & 1))
1038 		return false;
1039 	/* Clear status bits in reply message */
1040 	icmphdr->un.echo.sequence &= htons(0xFF00);
1041 	if (icmphdr->type == ICMP_EXT_ECHO)
1042 		icmphdr->type = ICMP_EXT_ECHOREPLY;
1043 	else
1044 		icmphdr->type = ICMPV6_EXT_ECHO_REPLY;
1045 	ext_hdr = skb_header_pointer(skb, 0, sizeof(_ext_hdr), &_ext_hdr);
1046 	/* Size of iio is class_type dependent.
1047 	 * Only check header here and assign length based on ctype in the switch statement
1048 	 */
1049 	iio = skb_header_pointer(skb, sizeof(_ext_hdr), sizeof(iio->extobj_hdr), &_iio);
1050 	if (!ext_hdr || !iio)
1051 		goto send_mal_query;
1052 	if (ntohs(iio->extobj_hdr.length) <= sizeof(iio->extobj_hdr) ||
1053 	    ntohs(iio->extobj_hdr.length) > sizeof(_iio))
1054 		goto send_mal_query;
1055 	ident_len = ntohs(iio->extobj_hdr.length) - sizeof(iio->extobj_hdr);
1056 	iio = skb_header_pointer(skb, sizeof(_ext_hdr),
1057 				 sizeof(iio->extobj_hdr) + ident_len, &_iio);
1058 	if (!iio)
1059 		goto send_mal_query;
1060 
1061 	status = 0;
1062 	dev = NULL;
1063 	switch (iio->extobj_hdr.class_type) {
1064 	case ICMP_EXT_ECHO_CTYPE_NAME:
1065 		if (ident_len >= IFNAMSIZ)
1066 			goto send_mal_query;
1067 		memset(buff, 0, sizeof(buff));
1068 		memcpy(buff, &iio->ident.name, ident_len);
1069 		dev = dev_get_by_name(net, buff);
1070 		break;
1071 	case ICMP_EXT_ECHO_CTYPE_INDEX:
1072 		if (ident_len != sizeof(iio->ident.ifindex))
1073 			goto send_mal_query;
1074 		dev = dev_get_by_index(net, ntohl(iio->ident.ifindex));
1075 		break;
1076 	case ICMP_EXT_ECHO_CTYPE_ADDR:
1077 		if (ident_len < sizeof(iio->ident.addr.ctype3_hdr) ||
1078 		    ident_len != sizeof(iio->ident.addr.ctype3_hdr) +
1079 				 iio->ident.addr.ctype3_hdr.addrlen)
1080 			goto send_mal_query;
1081 		switch (ntohs(iio->ident.addr.ctype3_hdr.afi)) {
1082 		case ICMP_AFI_IP:
1083 			if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in_addr))
1084 				goto send_mal_query;
1085 			dev = ip_dev_find(net, iio->ident.addr.ip_addr.ipv4_addr);
1086 			break;
1087 #if IS_ENABLED(CONFIG_IPV6)
1088 		case ICMP_AFI_IP6:
1089 			if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in6_addr))
1090 				goto send_mal_query;
1091 			dev = ipv6_stub->ipv6_dev_find(net, &iio->ident.addr.ip_addr.ipv6_addr, dev);
1092 			dev_hold(dev);
1093 			break;
1094 #endif
1095 		default:
1096 			goto send_mal_query;
1097 		}
1098 		break;
1099 	default:
1100 		goto send_mal_query;
1101 	}
1102 	if (!dev) {
1103 		icmphdr->code = ICMP_EXT_CODE_NO_IF;
1104 		return true;
1105 	}
1106 	/* Fill bits in reply message */
1107 	if (dev->flags & IFF_UP)
1108 		status |= ICMP_EXT_ECHOREPLY_ACTIVE;
1109 	if (__in_dev_get_rcu(dev) && __in_dev_get_rcu(dev)->ifa_list)
1110 		status |= ICMP_EXT_ECHOREPLY_IPV4;
1111 	if (!list_empty(&rcu_dereference(dev->ip6_ptr)->addr_list))
1112 		status |= ICMP_EXT_ECHOREPLY_IPV6;
1113 	dev_put(dev);
1114 	icmphdr->un.echo.sequence |= htons(status);
1115 	return true;
1116 send_mal_query:
1117 	icmphdr->code = ICMP_EXT_CODE_MAL_QUERY;
1118 	return true;
1119 }
1120 EXPORT_SYMBOL_GPL(icmp_build_probe);
1121 
1122 /*
1123  *	Handle ICMP Timestamp requests.
1124  *	RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
1125  *		  SHOULD be in the kernel for minimum random latency.
1126  *		  MUST be accurate to a few minutes.
1127  *		  MUST be updated at least at 15Hz.
1128  */
1129 static enum skb_drop_reason icmp_timestamp(struct sk_buff *skb)
1130 {
1131 	struct icmp_bxm icmp_param;
1132 	/*
1133 	 *	Too short.
1134 	 */
1135 	if (skb->len < 4)
1136 		goto out_err;
1137 
1138 	/*
1139 	 *	Fill in the current time as ms since midnight UT:
1140 	 */
1141 	icmp_param.data.times[1] = inet_current_timestamp();
1142 	icmp_param.data.times[2] = icmp_param.data.times[1];
1143 
1144 	BUG_ON(skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4));
1145 
1146 	icmp_param.data.icmph	   = *icmp_hdr(skb);
1147 	icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
1148 	icmp_param.data.icmph.code = 0;
1149 	icmp_param.skb		   = skb;
1150 	icmp_param.offset	   = 0;
1151 	icmp_param.data_len	   = 0;
1152 	icmp_param.head_len	   = sizeof(struct icmphdr) + 12;
1153 	icmp_reply(&icmp_param, skb);
1154 	return SKB_NOT_DROPPED_YET;
1155 
1156 out_err:
1157 	__ICMP_INC_STATS(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
1158 	return SKB_DROP_REASON_PKT_TOO_SMALL;
1159 }
1160 
1161 static enum skb_drop_reason icmp_discard(struct sk_buff *skb)
1162 {
1163 	/* pretend it was a success */
1164 	return SKB_NOT_DROPPED_YET;
1165 }
1166 
1167 /*
1168  *	Deal with incoming ICMP packets.
1169  */
1170 int icmp_rcv(struct sk_buff *skb)
1171 {
1172 	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
1173 	struct rtable *rt = skb_rtable(skb);
1174 	struct net *net = dev_net(rt->dst.dev);
1175 	struct icmphdr *icmph;
1176 
1177 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1178 		struct sec_path *sp = skb_sec_path(skb);
1179 		int nh;
1180 
1181 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
1182 				 XFRM_STATE_ICMP)) {
1183 			reason = SKB_DROP_REASON_XFRM_POLICY;
1184 			goto drop;
1185 		}
1186 
1187 		if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
1188 			goto drop;
1189 
1190 		nh = skb_network_offset(skb);
1191 		skb_set_network_header(skb, sizeof(*icmph));
1192 
1193 		if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN,
1194 						skb)) {
1195 			reason = SKB_DROP_REASON_XFRM_POLICY;
1196 			goto drop;
1197 		}
1198 
1199 		skb_set_network_header(skb, nh);
1200 	}
1201 
1202 	__ICMP_INC_STATS(net, ICMP_MIB_INMSGS);
1203 
1204 	if (skb_checksum_simple_validate(skb))
1205 		goto csum_error;
1206 
1207 	if (!pskb_pull(skb, sizeof(*icmph)))
1208 		goto error;
1209 
1210 	icmph = icmp_hdr(skb);
1211 
1212 	ICMPMSGIN_INC_STATS(net, icmph->type);
1213 
1214 	/* Check for ICMP Extended Echo (PROBE) messages */
1215 	if (icmph->type == ICMP_EXT_ECHO) {
1216 		/* We can't use icmp_pointers[].handler() because it is an array of
1217 		 * size NR_ICMP_TYPES + 1 (19 elements) and PROBE has code 42.
1218 		 */
1219 		reason = icmp_echo(skb);
1220 		goto reason_check;
1221 	}
1222 
1223 	if (icmph->type == ICMP_EXT_ECHOREPLY) {
1224 		reason = ping_rcv(skb);
1225 		goto reason_check;
1226 	}
1227 
1228 	/*
1229 	 *	18 is the highest 'known' ICMP type. Anything else is a mystery
1230 	 *
1231 	 *	RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
1232 	 *		  discarded.
1233 	 */
1234 	if (icmph->type > NR_ICMP_TYPES) {
1235 		reason = SKB_DROP_REASON_UNHANDLED_PROTO;
1236 		goto error;
1237 	}
1238 
1239 	/*
1240 	 *	Parse the ICMP message
1241 	 */
1242 
1243 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
1244 		/*
1245 		 *	RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
1246 		 *	  silently ignored (we let user decide with a sysctl).
1247 		 *	RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
1248 		 *	  discarded if to broadcast/multicast.
1249 		 */
1250 		if ((icmph->type == ICMP_ECHO ||
1251 		     icmph->type == ICMP_TIMESTAMP) &&
1252 		    net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
1253 			reason = SKB_DROP_REASON_INVALID_PROTO;
1254 			goto error;
1255 		}
1256 		if (icmph->type != ICMP_ECHO &&
1257 		    icmph->type != ICMP_TIMESTAMP &&
1258 		    icmph->type != ICMP_ADDRESS &&
1259 		    icmph->type != ICMP_ADDRESSREPLY) {
1260 			reason = SKB_DROP_REASON_INVALID_PROTO;
1261 			goto error;
1262 		}
1263 	}
1264 
1265 	reason = icmp_pointers[icmph->type].handler(skb);
1266 reason_check:
1267 	if (!reason)  {
1268 		consume_skb(skb);
1269 		return NET_RX_SUCCESS;
1270 	}
1271 
1272 drop:
1273 	kfree_skb_reason(skb, reason);
1274 	return NET_RX_DROP;
1275 csum_error:
1276 	reason = SKB_DROP_REASON_ICMP_CSUM;
1277 	__ICMP_INC_STATS(net, ICMP_MIB_CSUMERRORS);
1278 error:
1279 	__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
1280 	goto drop;
1281 }
1282 
1283 static bool ip_icmp_error_rfc4884_validate(const struct sk_buff *skb, int off)
1284 {
1285 	struct icmp_extobj_hdr *objh, _objh;
1286 	struct icmp_ext_hdr *exth, _exth;
1287 	u16 olen;
1288 
1289 	exth = skb_header_pointer(skb, off, sizeof(_exth), &_exth);
1290 	if (!exth)
1291 		return false;
1292 	if (exth->version != 2)
1293 		return true;
1294 
1295 	if (exth->checksum &&
1296 	    csum_fold(skb_checksum(skb, off, skb->len - off, 0)))
1297 		return false;
1298 
1299 	off += sizeof(_exth);
1300 	while (off < skb->len) {
1301 		objh = skb_header_pointer(skb, off, sizeof(_objh), &_objh);
1302 		if (!objh)
1303 			return false;
1304 
1305 		olen = ntohs(objh->length);
1306 		if (olen < sizeof(_objh))
1307 			return false;
1308 
1309 		off += olen;
1310 		if (off > skb->len)
1311 			return false;
1312 	}
1313 
1314 	return true;
1315 }
1316 
1317 void ip_icmp_error_rfc4884(const struct sk_buff *skb,
1318 			   struct sock_ee_data_rfc4884 *out,
1319 			   int thlen, int off)
1320 {
1321 	int hlen;
1322 
1323 	/* original datagram headers: end of icmph to payload (skb->data) */
1324 	hlen = -skb_transport_offset(skb) - thlen;
1325 
1326 	/* per rfc 4884: minimal datagram length of 128 bytes */
1327 	if (off < 128 || off < hlen)
1328 		return;
1329 
1330 	/* kernel has stripped headers: return payload offset in bytes */
1331 	off -= hlen;
1332 	if (off + sizeof(struct icmp_ext_hdr) > skb->len)
1333 		return;
1334 
1335 	out->len = off;
1336 
1337 	if (!ip_icmp_error_rfc4884_validate(skb, off))
1338 		out->flags |= SO_EE_RFC4884_FLAG_INVALID;
1339 }
1340 EXPORT_SYMBOL_GPL(ip_icmp_error_rfc4884);
1341 
1342 int icmp_err(struct sk_buff *skb, u32 info)
1343 {
1344 	struct iphdr *iph = (struct iphdr *)skb->data;
1345 	int offset = iph->ihl<<2;
1346 	struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
1347 	int type = icmp_hdr(skb)->type;
1348 	int code = icmp_hdr(skb)->code;
1349 	struct net *net = dev_net(skb->dev);
1350 
1351 	/*
1352 	 * Use ping_err to handle all icmp errors except those
1353 	 * triggered by ICMP_ECHOREPLY which sent from kernel.
1354 	 */
1355 	if (icmph->type != ICMP_ECHOREPLY) {
1356 		ping_err(skb, offset, info);
1357 		return 0;
1358 	}
1359 
1360 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
1361 		ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ICMP);
1362 	else if (type == ICMP_REDIRECT)
1363 		ipv4_redirect(skb, net, 0, IPPROTO_ICMP);
1364 
1365 	return 0;
1366 }
1367 
1368 /*
1369  *	This table is the definition of how we handle ICMP.
1370  */
1371 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
1372 	[ICMP_ECHOREPLY] = {
1373 		.handler = ping_rcv,
1374 	},
1375 	[1] = {
1376 		.handler = icmp_discard,
1377 		.error = 1,
1378 	},
1379 	[2] = {
1380 		.handler = icmp_discard,
1381 		.error = 1,
1382 	},
1383 	[ICMP_DEST_UNREACH] = {
1384 		.handler = icmp_unreach,
1385 		.error = 1,
1386 	},
1387 	[ICMP_SOURCE_QUENCH] = {
1388 		.handler = icmp_unreach,
1389 		.error = 1,
1390 	},
1391 	[ICMP_REDIRECT] = {
1392 		.handler = icmp_redirect,
1393 		.error = 1,
1394 	},
1395 	[6] = {
1396 		.handler = icmp_discard,
1397 		.error = 1,
1398 	},
1399 	[7] = {
1400 		.handler = icmp_discard,
1401 		.error = 1,
1402 	},
1403 	[ICMP_ECHO] = {
1404 		.handler = icmp_echo,
1405 	},
1406 	[9] = {
1407 		.handler = icmp_discard,
1408 		.error = 1,
1409 	},
1410 	[10] = {
1411 		.handler = icmp_discard,
1412 		.error = 1,
1413 	},
1414 	[ICMP_TIME_EXCEEDED] = {
1415 		.handler = icmp_unreach,
1416 		.error = 1,
1417 	},
1418 	[ICMP_PARAMETERPROB] = {
1419 		.handler = icmp_unreach,
1420 		.error = 1,
1421 	},
1422 	[ICMP_TIMESTAMP] = {
1423 		.handler = icmp_timestamp,
1424 	},
1425 	[ICMP_TIMESTAMPREPLY] = {
1426 		.handler = icmp_discard,
1427 	},
1428 	[ICMP_INFO_REQUEST] = {
1429 		.handler = icmp_discard,
1430 	},
1431 	[ICMP_INFO_REPLY] = {
1432 		.handler = icmp_discard,
1433 	},
1434 	[ICMP_ADDRESS] = {
1435 		.handler = icmp_discard,
1436 	},
1437 	[ICMP_ADDRESSREPLY] = {
1438 		.handler = icmp_discard,
1439 	},
1440 };
1441 
1442 static int __net_init icmp_sk_init(struct net *net)
1443 {
1444 	/* Control parameters for ECHO replies. */
1445 	net->ipv4.sysctl_icmp_echo_ignore_all = 0;
1446 	net->ipv4.sysctl_icmp_echo_enable_probe = 0;
1447 	net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
1448 
1449 	/* Control parameter - ignore bogus broadcast responses? */
1450 	net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
1451 
1452 	/*
1453 	 * 	Configurable global rate limit.
1454 	 *
1455 	 *	ratelimit defines tokens/packet consumed for dst->rate_token
1456 	 *	bucket ratemask defines which icmp types are ratelimited by
1457 	 *	setting	it's bit position.
1458 	 *
1459 	 *	default:
1460 	 *	dest unreachable (3), source quench (4),
1461 	 *	time exceeded (11), parameter problem (12)
1462 	 */
1463 
1464 	net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
1465 	net->ipv4.sysctl_icmp_ratemask = 0x1818;
1466 	net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
1467 
1468 	return 0;
1469 }
1470 
1471 static struct pernet_operations __net_initdata icmp_sk_ops = {
1472        .init = icmp_sk_init,
1473 };
1474 
1475 int __init icmp_init(void)
1476 {
1477 	int err, i;
1478 
1479 	for_each_possible_cpu(i) {
1480 		struct sock *sk;
1481 
1482 		err = inet_ctl_sock_create(&sk, PF_INET,
1483 					   SOCK_RAW, IPPROTO_ICMP, &init_net);
1484 		if (err < 0)
1485 			return err;
1486 
1487 		per_cpu(ipv4_icmp_sk, i) = sk;
1488 
1489 		/* Enough space for 2 64K ICMP packets, including
1490 		 * sk_buff/skb_shared_info struct overhead.
1491 		 */
1492 		sk->sk_sndbuf =	2 * SKB_TRUESIZE(64 * 1024);
1493 
1494 		/*
1495 		 * Speedup sock_wfree()
1496 		 */
1497 		sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1498 		inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
1499 	}
1500 	return register_pernet_subsys(&icmp_sk_ops);
1501 }
1502