• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	bool (*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 /*
196  *	The ICMP socket(s). This is the most convenient way to flow control
197  *	our ICMP output as well as maintain a clean interface throughout
198  *	all layers. All Socketless IP sends will soon be gone.
199  *
200  *	On SMP we have one ICMP socket per-cpu.
201  */
icmp_sk(struct net * net)202 static struct sock *icmp_sk(struct net *net)
203 {
204 	return this_cpu_read(*net->ipv4.icmp_sk);
205 }
206 
207 /* Called with BH disabled */
icmp_xmit_lock(struct net * net)208 static inline struct sock *icmp_xmit_lock(struct net *net)
209 {
210 	struct sock *sk;
211 
212 	sk = icmp_sk(net);
213 
214 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
215 		/* This can happen if the output path signals a
216 		 * dst_link_failure() for an outgoing ICMP packet.
217 		 */
218 		return NULL;
219 	}
220 	return sk;
221 }
222 
icmp_xmit_unlock(struct sock * sk)223 static inline void icmp_xmit_unlock(struct sock *sk)
224 {
225 	spin_unlock(&sk->sk_lock.slock);
226 }
227 
228 int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
229 int sysctl_icmp_msgs_burst __read_mostly = 50;
230 
231 static struct {
232 	atomic_t	credit;
233 	u32		stamp;
234 } icmp_global;
235 
236 /**
237  * icmp_global_allow - Are we allowed to send one more ICMP message ?
238  *
239  * Uses a token bucket to limit our ICMP messages to ~sysctl_icmp_msgs_per_sec.
240  * Returns false if we reached the limit and can not send another packet.
241  * Works in tandem with icmp_global_consume().
242  */
icmp_global_allow(void)243 bool icmp_global_allow(void)
244 {
245 	u32 delta, now, oldstamp;
246 	int incr, new, old;
247 
248 	/* Note: many cpus could find this condition true.
249 	 * Then later icmp_global_consume() could consume more credits,
250 	 * this is an acceptable race.
251 	 */
252 	if (atomic_read(&icmp_global.credit) > 0)
253 		return true;
254 
255 	now = jiffies;
256 	oldstamp = READ_ONCE(icmp_global.stamp);
257 	delta = min_t(u32, now - oldstamp, HZ);
258 	if (delta < HZ / 50)
259 		return false;
260 
261 	incr = READ_ONCE(sysctl_icmp_msgs_per_sec) * delta / HZ;
262 	if (!incr)
263 		return false;
264 
265 	if (cmpxchg(&icmp_global.stamp, oldstamp, now) == oldstamp) {
266 		old = atomic_read(&icmp_global.credit);
267 		do {
268 			new = min(old + incr, READ_ONCE(sysctl_icmp_msgs_burst));
269 		} while (!atomic_try_cmpxchg(&icmp_global.credit, &old, new));
270 	}
271 	return true;
272 }
273 EXPORT_SYMBOL(icmp_global_allow);
274 
icmp_global_consume(void)275 void icmp_global_consume(void)
276 {
277 	int credits = prandom_u32_max(3);
278 
279 	/* Note: this might make icmp_global.credit negative. */
280 	if (credits)
281 		atomic_sub(credits, &icmp_global.credit);
282 }
283 EXPORT_SYMBOL(icmp_global_consume);
284 
icmpv4_mask_allow(struct net * net,int type,int code)285 static bool icmpv4_mask_allow(struct net *net, int type, int code)
286 {
287 	if (type > NR_ICMP_TYPES)
288 		return true;
289 
290 	/* Don't limit PMTU discovery. */
291 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
292 		return true;
293 
294 	/* Limit if icmp type is enabled in ratemask. */
295 	if (!((1 << type) & READ_ONCE(net->ipv4.sysctl_icmp_ratemask)))
296 		return true;
297 
298 	return false;
299 }
300 
icmpv4_global_allow(struct net * net,int type,int code,bool * apply_ratelimit)301 static bool icmpv4_global_allow(struct net *net, int type, int code,
302 				bool *apply_ratelimit)
303 {
304 	if (icmpv4_mask_allow(net, type, code))
305 		return true;
306 
307 	if (icmp_global_allow()) {
308 		*apply_ratelimit = true;
309 		return true;
310 	}
311 	return false;
312 }
313 
314 /*
315  *	Send an ICMP frame.
316  */
317 
icmpv4_xrlim_allow(struct net * net,struct rtable * rt,struct flowi4 * fl4,int type,int code,bool apply_ratelimit)318 static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
319 			       struct flowi4 *fl4, int type, int code,
320 			       bool apply_ratelimit)
321 {
322 	struct dst_entry *dst = &rt->dst;
323 	struct inet_peer *peer;
324 	bool rc = true;
325 	int vif;
326 
327 	if (!apply_ratelimit)
328 		return true;
329 
330 	/* No rate limit on loopback */
331 	if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
332 		goto out;
333 
334 	vif = l3mdev_master_ifindex(dst->dev);
335 	peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, vif, 1);
336 	rc = inet_peer_xrlim_allow(peer,
337 				   READ_ONCE(net->ipv4.sysctl_icmp_ratelimit));
338 	if (peer)
339 		inet_putpeer(peer);
340 out:
341 	if (rc)
342 		icmp_global_consume();
343 	return rc;
344 }
345 
346 /*
347  *	Maintain the counters used in the SNMP statistics for outgoing ICMP
348  */
icmp_out_count(struct net * net,unsigned char type)349 void icmp_out_count(struct net *net, unsigned char type)
350 {
351 	ICMPMSGOUT_INC_STATS(net, type);
352 	ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
353 }
354 
355 /*
356  *	Checksum each fragment, and on the first include the headers and final
357  *	checksum.
358  */
icmp_glue_bits(void * from,char * to,int offset,int len,int odd,struct sk_buff * skb)359 static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
360 			  struct sk_buff *skb)
361 {
362 	struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
363 	__wsum csum;
364 
365 	csum = skb_copy_and_csum_bits(icmp_param->skb,
366 				      icmp_param->offset + offset,
367 				      to, len);
368 
369 	skb->csum = csum_block_add(skb->csum, csum, odd);
370 	if (icmp_pointers[icmp_param->data.icmph.type].error)
371 		nf_ct_attach(skb, icmp_param->skb);
372 	return 0;
373 }
374 
icmp_push_reply(struct icmp_bxm * icmp_param,struct flowi4 * fl4,struct ipcm_cookie * ipc,struct rtable ** rt)375 static void icmp_push_reply(struct icmp_bxm *icmp_param,
376 			    struct flowi4 *fl4,
377 			    struct ipcm_cookie *ipc, struct rtable **rt)
378 {
379 	struct sock *sk;
380 	struct sk_buff *skb;
381 
382 	sk = icmp_sk(dev_net((*rt)->dst.dev));
383 	if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
384 			   icmp_param->data_len+icmp_param->head_len,
385 			   icmp_param->head_len,
386 			   ipc, rt, MSG_DONTWAIT) < 0) {
387 		__ICMP_INC_STATS(sock_net(sk), ICMP_MIB_OUTERRORS);
388 		ip_flush_pending_frames(sk);
389 	} else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
390 		struct icmphdr *icmph = icmp_hdr(skb);
391 		__wsum csum;
392 		struct sk_buff *skb1;
393 
394 		csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
395 						 (char *)icmph,
396 						 icmp_param->head_len);
397 		skb_queue_walk(&sk->sk_write_queue, skb1) {
398 			csum = csum_add(csum, skb1->csum);
399 		}
400 		icmph->checksum = csum_fold(csum);
401 		skb->ip_summed = CHECKSUM_NONE;
402 		ip_push_pending_frames(sk, fl4);
403 	}
404 }
405 
406 /*
407  *	Driving logic for building and sending ICMP messages.
408  */
409 
icmp_reply(struct icmp_bxm * icmp_param,struct sk_buff * skb)410 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
411 {
412 	struct ipcm_cookie ipc;
413 	struct rtable *rt = skb_rtable(skb);
414 	struct net *net = dev_net(rt->dst.dev);
415 	bool apply_ratelimit = false;
416 	struct flowi4 fl4;
417 	struct sock *sk;
418 	struct inet_sock *inet;
419 	__be32 daddr, saddr;
420 	u32 mark = IP4_REPLY_MARK(net, skb->mark);
421 	int type = icmp_param->data.icmph.type;
422 	int code = icmp_param->data.icmph.code;
423 
424 	if (ip_options_echo(net, &icmp_param->replyopts.opt.opt, skb))
425 		return;
426 
427 	/* Needed by both icmpv4_global_allow and icmp_xmit_lock */
428 	local_bh_disable();
429 
430 	/* is global icmp_msgs_per_sec exhausted ? */
431 	if (!icmpv4_global_allow(net, type, code, &apply_ratelimit))
432 		goto out_bh_enable;
433 
434 	sk = icmp_xmit_lock(net);
435 	if (!sk)
436 		goto out_bh_enable;
437 	inet = inet_sk(sk);
438 
439 	icmp_param->data.icmph.checksum = 0;
440 
441 	ipcm_init(&ipc);
442 	inet->tos = ip_hdr(skb)->tos;
443 	ipc.sockc.mark = mark;
444 	daddr = ipc.addr = ip_hdr(skb)->saddr;
445 	saddr = fib_compute_spec_dst(skb);
446 
447 	if (icmp_param->replyopts.opt.opt.optlen) {
448 		ipc.opt = &icmp_param->replyopts.opt;
449 		if (ipc.opt->opt.srr)
450 			daddr = icmp_param->replyopts.opt.opt.faddr;
451 	}
452 	memset(&fl4, 0, sizeof(fl4));
453 	fl4.daddr = daddr;
454 	fl4.saddr = saddr;
455 	fl4.flowi4_mark = mark;
456 	fl4.flowi4_uid = sock_net_uid(net, NULL);
457 	fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
458 	fl4.flowi4_proto = IPPROTO_ICMP;
459 	fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
460 	security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
461 	rt = ip_route_output_key(net, &fl4);
462 	if (IS_ERR(rt))
463 		goto out_unlock;
464 	if (icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
465 		icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
466 	ip_rt_put(rt);
467 out_unlock:
468 	icmp_xmit_unlock(sk);
469 out_bh_enable:
470 	local_bh_enable();
471 }
472 
473 /*
474  * The device used for looking up which routing table to use for sending an ICMP
475  * error is preferably the source whenever it is set, which should ensure the
476  * icmp error can be sent to the source host, else lookup using the routing
477  * table of the destination device, else use the main routing table (index 0).
478  */
icmp_get_route_lookup_dev(struct sk_buff * skb)479 static struct net_device *icmp_get_route_lookup_dev(struct sk_buff *skb)
480 {
481 	struct net_device *route_lookup_dev = NULL;
482 
483 	if (skb->dev)
484 		route_lookup_dev = skb->dev;
485 	else if (skb_dst(skb))
486 		route_lookup_dev = skb_dst(skb)->dev;
487 	return route_lookup_dev;
488 }
489 
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)490 static struct rtable *icmp_route_lookup(struct net *net,
491 					struct flowi4 *fl4,
492 					struct sk_buff *skb_in,
493 					const struct iphdr *iph,
494 					__be32 saddr, u8 tos, u32 mark,
495 					int type, int code,
496 					struct icmp_bxm *param)
497 {
498 	struct net_device *route_lookup_dev;
499 	struct rtable *rt, *rt2;
500 	struct flowi4 fl4_dec;
501 	int err;
502 
503 	memset(fl4, 0, sizeof(*fl4));
504 	fl4->daddr = (param->replyopts.opt.opt.srr ?
505 		      param->replyopts.opt.opt.faddr : iph->saddr);
506 	fl4->saddr = saddr;
507 	fl4->flowi4_mark = mark;
508 	fl4->flowi4_uid = sock_net_uid(net, NULL);
509 	fl4->flowi4_tos = RT_TOS(tos);
510 	fl4->flowi4_proto = IPPROTO_ICMP;
511 	fl4->fl4_icmp_type = type;
512 	fl4->fl4_icmp_code = code;
513 	route_lookup_dev = icmp_get_route_lookup_dev(skb_in);
514 	fl4->flowi4_oif = l3mdev_master_ifindex(route_lookup_dev);
515 
516 	security_skb_classify_flow(skb_in, flowi4_to_flowi_common(fl4));
517 	rt = ip_route_output_key_hash(net, fl4, skb_in);
518 	if (IS_ERR(rt))
519 		return rt;
520 
521 	/* No need to clone since we're just using its address. */
522 	rt2 = rt;
523 
524 	rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
525 					   flowi4_to_flowi(fl4), NULL, 0);
526 	if (!IS_ERR(rt)) {
527 		if (rt != rt2)
528 			return rt;
529 		if (inet_addr_type_dev_table(net, route_lookup_dev,
530 					     fl4->daddr) == RTN_LOCAL)
531 			return rt;
532 	} else if (PTR_ERR(rt) == -EPERM) {
533 		rt = NULL;
534 	} else
535 		return rt;
536 
537 	err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
538 	if (err)
539 		goto relookup_failed;
540 
541 	if (inet_addr_type_dev_table(net, route_lookup_dev,
542 				     fl4_dec.saddr) == RTN_LOCAL) {
543 		rt2 = __ip_route_output_key(net, &fl4_dec);
544 		if (IS_ERR(rt2))
545 			err = PTR_ERR(rt2);
546 	} else {
547 		struct flowi4 fl4_2 = {};
548 		unsigned long orefdst;
549 
550 		fl4_2.daddr = fl4_dec.saddr;
551 		rt2 = ip_route_output_key(net, &fl4_2);
552 		if (IS_ERR(rt2)) {
553 			err = PTR_ERR(rt2);
554 			goto relookup_failed;
555 		}
556 		/* Ugh! */
557 		orefdst = skb_in->_skb_refdst; /* save old refdst */
558 		skb_dst_set(skb_in, NULL);
559 		err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
560 				     RT_TOS(tos), rt2->dst.dev);
561 
562 		dst_release(&rt2->dst);
563 		rt2 = skb_rtable(skb_in);
564 		skb_in->_skb_refdst = orefdst; /* restore old refdst */
565 	}
566 
567 	if (err)
568 		goto relookup_failed;
569 
570 	rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
571 					    flowi4_to_flowi(&fl4_dec), NULL,
572 					    XFRM_LOOKUP_ICMP);
573 	if (!IS_ERR(rt2)) {
574 		dst_release(&rt->dst);
575 		memcpy(fl4, &fl4_dec, sizeof(*fl4));
576 		rt = rt2;
577 	} else if (PTR_ERR(rt2) == -EPERM) {
578 		if (rt)
579 			dst_release(&rt->dst);
580 		return rt2;
581 	} else {
582 		err = PTR_ERR(rt2);
583 		goto relookup_failed;
584 	}
585 	return rt;
586 
587 relookup_failed:
588 	if (rt)
589 		return rt;
590 	return ERR_PTR(err);
591 }
592 
593 /*
594  *	Send an ICMP message in response to a situation
595  *
596  *	RFC 1122: 3.2.2	MUST send at least the IP header and 8 bytes of header.
597  *		  MAY send more (we do).
598  *			MUST NOT change this header information.
599  *			MUST NOT reply to a multicast/broadcast IP address.
600  *			MUST NOT reply to a multicast/broadcast MAC address.
601  *			MUST reply to only the first fragment.
602  */
603 
__icmp_send(struct sk_buff * skb_in,int type,int code,__be32 info,const struct ip_options * opt)604 void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info,
605 		 const struct ip_options *opt)
606 {
607 	struct iphdr *iph;
608 	int room;
609 	struct icmp_bxm icmp_param;
610 	struct rtable *rt = skb_rtable(skb_in);
611 	bool apply_ratelimit = false;
612 	struct ipcm_cookie ipc;
613 	struct flowi4 fl4;
614 	__be32 saddr;
615 	u8  tos;
616 	u32 mark;
617 	struct net *net;
618 	struct sock *sk;
619 
620 	if (!rt)
621 		goto out;
622 
623 	if (rt->dst.dev)
624 		net = dev_net(rt->dst.dev);
625 	else if (skb_in->dev)
626 		net = dev_net(skb_in->dev);
627 	else
628 		goto out;
629 
630 	/*
631 	 *	Find the original header. It is expected to be valid, of course.
632 	 *	Check this, icmp_send is called from the most obscure devices
633 	 *	sometimes.
634 	 */
635 	iph = ip_hdr(skb_in);
636 
637 	if ((u8 *)iph < skb_in->head ||
638 	    (skb_network_header(skb_in) + sizeof(*iph)) >
639 	    skb_tail_pointer(skb_in))
640 		goto out;
641 
642 	/*
643 	 *	No replies to physical multicast/broadcast
644 	 */
645 	if (skb_in->pkt_type != PACKET_HOST)
646 		goto out;
647 
648 	/*
649 	 *	Now check at the protocol level
650 	 */
651 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
652 		goto out;
653 
654 	/*
655 	 *	Only reply to fragment 0. We byte re-order the constant
656 	 *	mask for efficiency.
657 	 */
658 	if (iph->frag_off & htons(IP_OFFSET))
659 		goto out;
660 
661 	/*
662 	 *	If we send an ICMP error to an ICMP error a mess would result..
663 	 */
664 	if (icmp_pointers[type].error) {
665 		/*
666 		 *	We are an error, check if we are replying to an
667 		 *	ICMP error
668 		 */
669 		if (iph->protocol == IPPROTO_ICMP) {
670 			u8 _inner_type, *itp;
671 
672 			itp = skb_header_pointer(skb_in,
673 						 skb_network_header(skb_in) +
674 						 (iph->ihl << 2) +
675 						 offsetof(struct icmphdr,
676 							  type) -
677 						 skb_in->data,
678 						 sizeof(_inner_type),
679 						 &_inner_type);
680 			if (!itp)
681 				goto out;
682 
683 			/*
684 			 *	Assume any unknown ICMP type is an error. This
685 			 *	isn't specified by the RFC, but think about it..
686 			 */
687 			if (*itp > NR_ICMP_TYPES ||
688 			    icmp_pointers[*itp].error)
689 				goto out;
690 		}
691 	}
692 
693 	/* Needed by both icmpv4_global_allow and icmp_xmit_lock */
694 	local_bh_disable();
695 
696 	/* Check global sysctl_icmp_msgs_per_sec ratelimit, unless
697 	 * incoming dev is loopback.  If outgoing dev change to not be
698 	 * loopback, then peer ratelimit still work (in icmpv4_xrlim_allow)
699 	 */
700 	if (!(skb_in->dev && (skb_in->dev->flags&IFF_LOOPBACK)) &&
701 	      !icmpv4_global_allow(net, type, code, &apply_ratelimit))
702 		goto out_bh_enable;
703 
704 	sk = icmp_xmit_lock(net);
705 	if (!sk)
706 		goto out_bh_enable;
707 
708 	/*
709 	 *	Construct source address and options.
710 	 */
711 
712 	saddr = iph->daddr;
713 	if (!(rt->rt_flags & RTCF_LOCAL)) {
714 		struct net_device *dev = NULL;
715 
716 		rcu_read_lock();
717 		if (rt_is_input_route(rt) &&
718 		    READ_ONCE(net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr))
719 			dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
720 
721 		if (dev)
722 			saddr = inet_select_addr(dev, iph->saddr,
723 						 RT_SCOPE_LINK);
724 		else
725 			saddr = 0;
726 		rcu_read_unlock();
727 	}
728 
729 	tos = icmp_pointers[type].error ? (RT_TOS(iph->tos) |
730 					   IPTOS_PREC_INTERNETCONTROL) :
731 					   iph->tos;
732 	mark = IP4_REPLY_MARK(net, skb_in->mark);
733 
734 	if (__ip_options_echo(net, &icmp_param.replyopts.opt.opt, skb_in, opt))
735 		goto out_unlock;
736 
737 
738 	/*
739 	 *	Prepare data for ICMP header.
740 	 */
741 
742 	icmp_param.data.icmph.type	 = type;
743 	icmp_param.data.icmph.code	 = code;
744 	icmp_param.data.icmph.un.gateway = info;
745 	icmp_param.data.icmph.checksum	 = 0;
746 	icmp_param.skb	  = skb_in;
747 	icmp_param.offset = skb_network_offset(skb_in);
748 	inet_sk(sk)->tos = tos;
749 	ipcm_init(&ipc);
750 	ipc.addr = iph->saddr;
751 	ipc.opt = &icmp_param.replyopts.opt;
752 	ipc.sockc.mark = mark;
753 
754 	rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
755 			       type, code, &icmp_param);
756 	if (IS_ERR(rt))
757 		goto out_unlock;
758 
759 	/* peer icmp_ratelimit */
760 	if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
761 		goto ende;
762 
763 	/* RFC says return as much as we can without exceeding 576 bytes. */
764 
765 	room = dst_mtu(&rt->dst);
766 	if (room > 576)
767 		room = 576;
768 	room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
769 	room -= sizeof(struct icmphdr);
770 	/* Guard against tiny mtu. We need to include at least one
771 	 * IP network header for this message to make any sense.
772 	 */
773 	if (room <= (int)sizeof(struct iphdr))
774 		goto ende;
775 
776 	icmp_param.data_len = skb_in->len - icmp_param.offset;
777 	if (icmp_param.data_len > room)
778 		icmp_param.data_len = room;
779 	icmp_param.head_len = sizeof(struct icmphdr);
780 
781 	/* if we don't have a source address at this point, fall back to the
782 	 * dummy address instead of sending out a packet with a source address
783 	 * of 0.0.0.0
784 	 */
785 	if (!fl4.saddr)
786 		fl4.saddr = htonl(INADDR_DUMMY);
787 
788 	icmp_push_reply(&icmp_param, &fl4, &ipc, &rt);
789 ende:
790 	ip_rt_put(rt);
791 out_unlock:
792 	icmp_xmit_unlock(sk);
793 out_bh_enable:
794 	local_bh_enable();
795 out:;
796 }
797 EXPORT_SYMBOL(__icmp_send);
798 
799 #if IS_ENABLED(CONFIG_NF_NAT)
800 #include <net/netfilter/nf_conntrack.h>
icmp_ndo_send(struct sk_buff * skb_in,int type,int code,__be32 info)801 void icmp_ndo_send(struct sk_buff *skb_in, int type, int code, __be32 info)
802 {
803 	struct sk_buff *cloned_skb = NULL;
804 	struct ip_options opts = { 0 };
805 	enum ip_conntrack_info ctinfo;
806 	struct nf_conn *ct;
807 	__be32 orig_ip;
808 
809 	ct = nf_ct_get(skb_in, &ctinfo);
810 	if (!ct || !(ct->status & IPS_SRC_NAT)) {
811 		__icmp_send(skb_in, type, code, info, &opts);
812 		return;
813 	}
814 
815 	if (skb_shared(skb_in))
816 		skb_in = cloned_skb = skb_clone(skb_in, GFP_ATOMIC);
817 
818 	if (unlikely(!skb_in || skb_network_header(skb_in) < skb_in->head ||
819 	    (skb_network_header(skb_in) + sizeof(struct iphdr)) >
820 	    skb_tail_pointer(skb_in) || skb_ensure_writable(skb_in,
821 	    skb_network_offset(skb_in) + sizeof(struct iphdr))))
822 		goto out;
823 
824 	orig_ip = ip_hdr(skb_in)->saddr;
825 	ip_hdr(skb_in)->saddr = ct->tuplehash[0].tuple.src.u3.ip;
826 	__icmp_send(skb_in, type, code, info, &opts);
827 	ip_hdr(skb_in)->saddr = orig_ip;
828 out:
829 	consume_skb(cloned_skb);
830 }
831 EXPORT_SYMBOL(icmp_ndo_send);
832 #endif
833 
icmp_socket_deliver(struct sk_buff * skb,u32 info)834 static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
835 {
836 	const struct iphdr *iph = (const struct iphdr *)skb->data;
837 	const struct net_protocol *ipprot;
838 	int protocol = iph->protocol;
839 
840 	/* Checkin full IP header plus 8 bytes of protocol to
841 	 * avoid additional coding at protocol handlers.
842 	 */
843 	if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
844 		__ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
845 		return;
846 	}
847 
848 	raw_icmp_error(skb, protocol, info);
849 
850 	ipprot = rcu_dereference(inet_protos[protocol]);
851 	if (ipprot && ipprot->err_handler)
852 		ipprot->err_handler(skb, info);
853 }
854 
icmp_tag_validation(int proto)855 static bool icmp_tag_validation(int proto)
856 {
857 	bool ok;
858 
859 	rcu_read_lock();
860 	ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
861 	rcu_read_unlock();
862 	return ok;
863 }
864 
865 /*
866  *	Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEEDED, ICMP_QUENCH, and
867  *	ICMP_PARAMETERPROB.
868  */
869 
icmp_unreach(struct sk_buff * skb)870 static bool icmp_unreach(struct sk_buff *skb)
871 {
872 	const struct iphdr *iph;
873 	struct icmphdr *icmph;
874 	struct net *net;
875 	u32 info = 0;
876 
877 	net = dev_net(skb_dst(skb)->dev);
878 
879 	/*
880 	 *	Incomplete header ?
881 	 * 	Only checks for the IP header, there should be an
882 	 *	additional check for longer headers in upper levels.
883 	 */
884 
885 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
886 		goto out_err;
887 
888 	icmph = icmp_hdr(skb);
889 	iph   = (const struct iphdr *)skb->data;
890 
891 	if (iph->ihl < 5) /* Mangled header, drop. */
892 		goto out_err;
893 
894 	switch (icmph->type) {
895 	case ICMP_DEST_UNREACH:
896 		switch (icmph->code & 15) {
897 		case ICMP_NET_UNREACH:
898 		case ICMP_HOST_UNREACH:
899 		case ICMP_PROT_UNREACH:
900 		case ICMP_PORT_UNREACH:
901 			break;
902 		case ICMP_FRAG_NEEDED:
903 			/* for documentation of the ip_no_pmtu_disc
904 			 * values please see
905 			 * Documentation/networking/ip-sysctl.rst
906 			 */
907 			switch (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc)) {
908 			default:
909 				net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
910 						    &iph->daddr);
911 				break;
912 			case 2:
913 				goto out;
914 			case 3:
915 				if (!icmp_tag_validation(iph->protocol))
916 					goto out;
917 				fallthrough;
918 			case 0:
919 				info = ntohs(icmph->un.frag.mtu);
920 			}
921 			break;
922 		case ICMP_SR_FAILED:
923 			net_dbg_ratelimited("%pI4: Source Route Failed\n",
924 					    &iph->daddr);
925 			break;
926 		default:
927 			break;
928 		}
929 		if (icmph->code > NR_ICMP_UNREACH)
930 			goto out;
931 		break;
932 	case ICMP_PARAMETERPROB:
933 		info = ntohl(icmph->un.gateway) >> 24;
934 		break;
935 	case ICMP_TIME_EXCEEDED:
936 		__ICMP_INC_STATS(net, ICMP_MIB_INTIMEEXCDS);
937 		if (icmph->code == ICMP_EXC_FRAGTIME)
938 			goto out;
939 		break;
940 	}
941 
942 	/*
943 	 *	Throw it at our lower layers
944 	 *
945 	 *	RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
946 	 *		  header.
947 	 *	RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
948 	 *		  transport layer.
949 	 *	RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
950 	 *		  transport layer.
951 	 */
952 
953 	/*
954 	 *	Check the other end isn't violating RFC 1122. Some routers send
955 	 *	bogus responses to broadcast frames. If you see this message
956 	 *	first check your netmask matches at both ends, if it does then
957 	 *	get the other vendor to fix their kit.
958 	 */
959 
960 	if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
961 	    inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
962 		net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
963 				     &ip_hdr(skb)->saddr,
964 				     icmph->type, icmph->code,
965 				     &iph->daddr, skb->dev->name);
966 		goto out;
967 	}
968 
969 	icmp_socket_deliver(skb, info);
970 
971 out:
972 	return true;
973 out_err:
974 	__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
975 	return false;
976 }
977 
978 
979 /*
980  *	Handle ICMP_REDIRECT.
981  */
982 
icmp_redirect(struct sk_buff * skb)983 static bool icmp_redirect(struct sk_buff *skb)
984 {
985 	if (skb->len < sizeof(struct iphdr)) {
986 		__ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
987 		return false;
988 	}
989 
990 	if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
991 		/* there aught to be a stat */
992 		return false;
993 	}
994 
995 	icmp_socket_deliver(skb, ntohl(icmp_hdr(skb)->un.gateway));
996 	return true;
997 }
998 
999 /*
1000  *	Handle ICMP_ECHO ("ping") requests.
1001  *
1002  *	RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
1003  *		  requests.
1004  *	RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
1005  *		  included in the reply.
1006  *	RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
1007  *		  echo requests, MUST have default=NOT.
1008  *	See also WRT handling of options once they are done and working.
1009  */
1010 
icmp_echo(struct sk_buff * skb)1011 static bool icmp_echo(struct sk_buff *skb)
1012 {
1013 	struct net *net;
1014 
1015 	net = dev_net(skb_dst(skb)->dev);
1016 	if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
1017 		struct icmp_bxm icmp_param;
1018 
1019 		icmp_param.data.icmph	   = *icmp_hdr(skb);
1020 		icmp_param.data.icmph.type = ICMP_ECHOREPLY;
1021 		icmp_param.skb		   = skb;
1022 		icmp_param.offset	   = 0;
1023 		icmp_param.data_len	   = skb->len;
1024 		icmp_param.head_len	   = sizeof(struct icmphdr);
1025 		icmp_reply(&icmp_param, skb);
1026 	}
1027 	/* should there be an ICMP stat for ignored echos? */
1028 	return true;
1029 }
1030 
1031 /*
1032  *	Handle ICMP Timestamp requests.
1033  *	RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
1034  *		  SHOULD be in the kernel for minimum random latency.
1035  *		  MUST be accurate to a few minutes.
1036  *		  MUST be updated at least at 15Hz.
1037  */
icmp_timestamp(struct sk_buff * skb)1038 static bool icmp_timestamp(struct sk_buff *skb)
1039 {
1040 	struct icmp_bxm icmp_param;
1041 	/*
1042 	 *	Too short.
1043 	 */
1044 	if (skb->len < 4)
1045 		goto out_err;
1046 
1047 	/*
1048 	 *	Fill in the current time as ms since midnight UT:
1049 	 */
1050 	icmp_param.data.times[1] = inet_current_timestamp();
1051 	icmp_param.data.times[2] = icmp_param.data.times[1];
1052 
1053 	BUG_ON(skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4));
1054 
1055 	icmp_param.data.icmph	   = *icmp_hdr(skb);
1056 	icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
1057 	icmp_param.data.icmph.code = 0;
1058 	icmp_param.skb		   = skb;
1059 	icmp_param.offset	   = 0;
1060 	icmp_param.data_len	   = 0;
1061 	icmp_param.head_len	   = sizeof(struct icmphdr) + 12;
1062 	icmp_reply(&icmp_param, skb);
1063 	return true;
1064 
1065 out_err:
1066 	__ICMP_INC_STATS(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
1067 	return false;
1068 }
1069 
icmp_discard(struct sk_buff * skb)1070 static bool icmp_discard(struct sk_buff *skb)
1071 {
1072 	/* pretend it was a success */
1073 	return true;
1074 }
1075 
1076 /*
1077  *	Deal with incoming ICMP packets.
1078  */
icmp_rcv(struct sk_buff * skb)1079 int icmp_rcv(struct sk_buff *skb)
1080 {
1081 	struct icmphdr *icmph;
1082 	struct rtable *rt = skb_rtable(skb);
1083 	struct net *net = dev_net(rt->dst.dev);
1084 	bool success;
1085 
1086 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1087 		struct sec_path *sp = skb_sec_path(skb);
1088 		int nh;
1089 
1090 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
1091 				 XFRM_STATE_ICMP))
1092 			goto drop;
1093 
1094 		if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
1095 			goto drop;
1096 
1097 		nh = skb_network_offset(skb);
1098 		skb_set_network_header(skb, sizeof(*icmph));
1099 
1100 		if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
1101 			goto drop;
1102 
1103 		skb_set_network_header(skb, nh);
1104 	}
1105 
1106 	__ICMP_INC_STATS(net, ICMP_MIB_INMSGS);
1107 
1108 	if (skb_checksum_simple_validate(skb))
1109 		goto csum_error;
1110 
1111 	if (!pskb_pull(skb, sizeof(*icmph)))
1112 		goto error;
1113 
1114 	icmph = icmp_hdr(skb);
1115 
1116 	ICMPMSGIN_INC_STATS(net, icmph->type);
1117 	/*
1118 	 *	18 is the highest 'known' ICMP type. Anything else is a mystery
1119 	 *
1120 	 *	RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
1121 	 *		  discarded.
1122 	 */
1123 	if (icmph->type > NR_ICMP_TYPES)
1124 		goto error;
1125 
1126 
1127 	/*
1128 	 *	Parse the ICMP message
1129 	 */
1130 
1131 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
1132 		/*
1133 		 *	RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
1134 		 *	  silently ignored (we let user decide with a sysctl).
1135 		 *	RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
1136 		 *	  discarded if to broadcast/multicast.
1137 		 */
1138 		if ((icmph->type == ICMP_ECHO ||
1139 		     icmph->type == ICMP_TIMESTAMP) &&
1140 		    net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
1141 			goto error;
1142 		}
1143 		if (icmph->type != ICMP_ECHO &&
1144 		    icmph->type != ICMP_TIMESTAMP &&
1145 		    icmph->type != ICMP_ADDRESS &&
1146 		    icmph->type != ICMP_ADDRESSREPLY) {
1147 			goto error;
1148 		}
1149 	}
1150 
1151 	success = icmp_pointers[icmph->type].handler(skb);
1152 
1153 	if (success)  {
1154 		consume_skb(skb);
1155 		return NET_RX_SUCCESS;
1156 	}
1157 
1158 drop:
1159 	kfree_skb(skb);
1160 	return NET_RX_DROP;
1161 csum_error:
1162 	__ICMP_INC_STATS(net, ICMP_MIB_CSUMERRORS);
1163 error:
1164 	__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
1165 	goto drop;
1166 }
1167 
ip_icmp_error_rfc4884_validate(const struct sk_buff * skb,int off)1168 static bool ip_icmp_error_rfc4884_validate(const struct sk_buff *skb, int off)
1169 {
1170 	struct icmp_extobj_hdr *objh, _objh;
1171 	struct icmp_ext_hdr *exth, _exth;
1172 	u16 olen;
1173 
1174 	exth = skb_header_pointer(skb, off, sizeof(_exth), &_exth);
1175 	if (!exth)
1176 		return false;
1177 	if (exth->version != 2)
1178 		return true;
1179 
1180 	if (exth->checksum &&
1181 	    csum_fold(skb_checksum(skb, off, skb->len - off, 0)))
1182 		return false;
1183 
1184 	off += sizeof(_exth);
1185 	while (off < skb->len) {
1186 		objh = skb_header_pointer(skb, off, sizeof(_objh), &_objh);
1187 		if (!objh)
1188 			return false;
1189 
1190 		olen = ntohs(objh->length);
1191 		if (olen < sizeof(_objh))
1192 			return false;
1193 
1194 		off += olen;
1195 		if (off > skb->len)
1196 			return false;
1197 	}
1198 
1199 	return true;
1200 }
1201 
ip_icmp_error_rfc4884(const struct sk_buff * skb,struct sock_ee_data_rfc4884 * out,int thlen,int off)1202 void ip_icmp_error_rfc4884(const struct sk_buff *skb,
1203 			   struct sock_ee_data_rfc4884 *out,
1204 			   int thlen, int off)
1205 {
1206 	int hlen;
1207 
1208 	/* original datagram headers: end of icmph to payload (skb->data) */
1209 	hlen = -skb_transport_offset(skb) - thlen;
1210 
1211 	/* per rfc 4884: minimal datagram length of 128 bytes */
1212 	if (off < 128 || off < hlen)
1213 		return;
1214 
1215 	/* kernel has stripped headers: return payload offset in bytes */
1216 	off -= hlen;
1217 	if (off + sizeof(struct icmp_ext_hdr) > skb->len)
1218 		return;
1219 
1220 	out->len = off;
1221 
1222 	if (!ip_icmp_error_rfc4884_validate(skb, off))
1223 		out->flags |= SO_EE_RFC4884_FLAG_INVALID;
1224 }
1225 EXPORT_SYMBOL_GPL(ip_icmp_error_rfc4884);
1226 
icmp_err(struct sk_buff * skb,u32 info)1227 int icmp_err(struct sk_buff *skb, u32 info)
1228 {
1229 	struct iphdr *iph = (struct iphdr *)skb->data;
1230 	int offset = iph->ihl<<2;
1231 	struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
1232 	int type = icmp_hdr(skb)->type;
1233 	int code = icmp_hdr(skb)->code;
1234 	struct net *net = dev_net(skb->dev);
1235 
1236 	/*
1237 	 * Use ping_err to handle all icmp errors except those
1238 	 * triggered by ICMP_ECHOREPLY which sent from kernel.
1239 	 */
1240 	if (icmph->type != ICMP_ECHOREPLY) {
1241 		ping_err(skb, offset, info);
1242 		return 0;
1243 	}
1244 
1245 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
1246 		ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ICMP);
1247 	else if (type == ICMP_REDIRECT)
1248 		ipv4_redirect(skb, net, 0, IPPROTO_ICMP);
1249 
1250 	return 0;
1251 }
1252 
1253 /*
1254  *	This table is the definition of how we handle ICMP.
1255  */
1256 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
1257 	[ICMP_ECHOREPLY] = {
1258 		.handler = ping_rcv,
1259 	},
1260 	[1] = {
1261 		.handler = icmp_discard,
1262 		.error = 1,
1263 	},
1264 	[2] = {
1265 		.handler = icmp_discard,
1266 		.error = 1,
1267 	},
1268 	[ICMP_DEST_UNREACH] = {
1269 		.handler = icmp_unreach,
1270 		.error = 1,
1271 	},
1272 	[ICMP_SOURCE_QUENCH] = {
1273 		.handler = icmp_unreach,
1274 		.error = 1,
1275 	},
1276 	[ICMP_REDIRECT] = {
1277 		.handler = icmp_redirect,
1278 		.error = 1,
1279 	},
1280 	[6] = {
1281 		.handler = icmp_discard,
1282 		.error = 1,
1283 	},
1284 	[7] = {
1285 		.handler = icmp_discard,
1286 		.error = 1,
1287 	},
1288 	[ICMP_ECHO] = {
1289 		.handler = icmp_echo,
1290 	},
1291 	[9] = {
1292 		.handler = icmp_discard,
1293 		.error = 1,
1294 	},
1295 	[10] = {
1296 		.handler = icmp_discard,
1297 		.error = 1,
1298 	},
1299 	[ICMP_TIME_EXCEEDED] = {
1300 		.handler = icmp_unreach,
1301 		.error = 1,
1302 	},
1303 	[ICMP_PARAMETERPROB] = {
1304 		.handler = icmp_unreach,
1305 		.error = 1,
1306 	},
1307 	[ICMP_TIMESTAMP] = {
1308 		.handler = icmp_timestamp,
1309 	},
1310 	[ICMP_TIMESTAMPREPLY] = {
1311 		.handler = icmp_discard,
1312 	},
1313 	[ICMP_INFO_REQUEST] = {
1314 		.handler = icmp_discard,
1315 	},
1316 	[ICMP_INFO_REPLY] = {
1317 		.handler = icmp_discard,
1318 	},
1319 	[ICMP_ADDRESS] = {
1320 		.handler = icmp_discard,
1321 	},
1322 	[ICMP_ADDRESSREPLY] = {
1323 		.handler = icmp_discard,
1324 	},
1325 };
1326 
icmp_sk_exit(struct net * net)1327 static void __net_exit icmp_sk_exit(struct net *net)
1328 {
1329 	int i;
1330 
1331 	for_each_possible_cpu(i)
1332 		inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
1333 	free_percpu(net->ipv4.icmp_sk);
1334 	net->ipv4.icmp_sk = NULL;
1335 }
1336 
icmp_sk_init(struct net * net)1337 static int __net_init icmp_sk_init(struct net *net)
1338 {
1339 	int i, err;
1340 
1341 	net->ipv4.icmp_sk = alloc_percpu(struct sock *);
1342 	if (!net->ipv4.icmp_sk)
1343 		return -ENOMEM;
1344 
1345 	for_each_possible_cpu(i) {
1346 		struct sock *sk;
1347 
1348 		err = inet_ctl_sock_create(&sk, PF_INET,
1349 					   SOCK_RAW, IPPROTO_ICMP, net);
1350 		if (err < 0)
1351 			goto fail;
1352 
1353 		*per_cpu_ptr(net->ipv4.icmp_sk, i) = sk;
1354 
1355 		/* Enough space for 2 64K ICMP packets, including
1356 		 * sk_buff/skb_shared_info struct overhead.
1357 		 */
1358 		sk->sk_sndbuf =	2 * SKB_TRUESIZE(64 * 1024);
1359 
1360 		/*
1361 		 * Speedup sock_wfree()
1362 		 */
1363 		sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1364 		inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
1365 	}
1366 
1367 	/* Control parameters for ECHO replies. */
1368 	net->ipv4.sysctl_icmp_echo_ignore_all = 0;
1369 	net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
1370 
1371 	/* Control parameter - ignore bogus broadcast responses? */
1372 	net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
1373 
1374 	/*
1375 	 * 	Configurable global rate limit.
1376 	 *
1377 	 *	ratelimit defines tokens/packet consumed for dst->rate_token
1378 	 *	bucket ratemask defines which icmp types are ratelimited by
1379 	 *	setting	it's bit position.
1380 	 *
1381 	 *	default:
1382 	 *	dest unreachable (3), source quench (4),
1383 	 *	time exceeded (11), parameter problem (12)
1384 	 */
1385 
1386 	net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
1387 	net->ipv4.sysctl_icmp_ratemask = 0x1818;
1388 	net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
1389 
1390 	return 0;
1391 
1392 fail:
1393 	icmp_sk_exit(net);
1394 	return err;
1395 }
1396 
1397 static struct pernet_operations __net_initdata icmp_sk_ops = {
1398        .init = icmp_sk_init,
1399        .exit = icmp_sk_exit,
1400 };
1401 
icmp_init(void)1402 int __init icmp_init(void)
1403 {
1404 	return register_pernet_subsys(&icmp_sk_ops);
1405 }
1406