xref: /linux/net/ipv6/icmp.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Internet Control Message Protocol (ICMPv6)
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *
9  *	Based on net/ipv4/icmp.c
10  *
11  *	RFC 1885
12  */
13 
14 /*
15  *	Changes:
16  *
17  *	Andi Kleen		:	exception handling
18  *	Andi Kleen			add rate limits. never reply to a icmp.
19  *					add more length checks and other fixes.
20  *	yoshfuji		:	ensure to sent parameter problem for
21  *					fragments.
22  *	YOSHIFUJI Hideaki @USAGI:	added sysctl for icmp rate limit.
23  *	Randy Dunlap and
24  *	YOSHIFUJI Hideaki @USAGI:	Per-interface statistics support
25  *	Kazunori MIYAZAWA @USAGI:       change output process to use ip6_append_data
26  */
27 
28 #define pr_fmt(fmt) "IPv6: " fmt
29 
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/types.h>
33 #include <linux/socket.h>
34 #include <linux/in.h>
35 #include <linux/kernel.h>
36 #include <linux/sockios.h>
37 #include <linux/net.h>
38 #include <linux/skbuff.h>
39 #include <linux/init.h>
40 #include <linux/netfilter.h>
41 #include <linux/slab.h>
42 
43 #ifdef CONFIG_SYSCTL
44 #include <linux/sysctl.h>
45 #endif
46 
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/icmpv6.h>
50 
51 #include <net/ip.h>
52 #include <net/sock.h>
53 
54 #include <net/ipv6.h>
55 #include <net/ip6_checksum.h>
56 #include <net/ping.h>
57 #include <net/protocol.h>
58 #include <net/raw.h>
59 #include <net/rawv6.h>
60 #include <net/seg6.h>
61 #include <net/transp_v6.h>
62 #include <net/ip6_route.h>
63 #include <net/addrconf.h>
64 #include <net/icmp.h>
65 #include <net/xfrm.h>
66 #include <net/inet_common.h>
67 #include <net/dsfield.h>
68 #include <net/l3mdev.h>
69 
70 #include <linux/uaccess.h>
71 
72 static DEFINE_PER_CPU(struct sock *, ipv6_icmp_sk);
73 
74 static int icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
75 		       u8 type, u8 code, int offset, __be32 info)
76 {
77 	/* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */
78 	struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset);
79 	struct net *net = dev_net_rcu(skb->dev);
80 
81 	if (type == ICMPV6_PKT_TOOBIG)
82 		ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0, sock_net_uid(net, NULL));
83 	else if (type == NDISC_REDIRECT)
84 		ip6_redirect(skb, net, skb->dev->ifindex, 0,
85 			     sock_net_uid(net, NULL));
86 
87 	if (!(type & ICMPV6_INFOMSG_MASK))
88 		if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST)
89 			ping_err(skb, offset, ntohl(info));
90 
91 	return 0;
92 }
93 
94 static int icmpv6_rcv(struct sk_buff *skb);
95 
96 static const struct inet6_protocol icmpv6_protocol = {
97 	.handler	=	icmpv6_rcv,
98 	.err_handler	=	icmpv6_err,
99 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
100 };
101 
102 /* Called with BH disabled */
103 static struct sock *icmpv6_xmit_lock(struct net *net)
104 {
105 	struct sock *sk;
106 
107 	sk = this_cpu_read(ipv6_icmp_sk);
108 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
109 		/* This can happen if the output path (f.e. SIT or
110 		 * ip6ip6 tunnel) signals dst_link_failure() for an
111 		 * outgoing ICMP6 packet.
112 		 */
113 		return NULL;
114 	}
115 	sock_net_set(sk, net);
116 	return sk;
117 }
118 
119 static void icmpv6_xmit_unlock(struct sock *sk)
120 {
121 	sock_net_set(sk, &init_net);
122 	spin_unlock(&sk->sk_lock.slock);
123 }
124 
125 /*
126  * Figure out, may we reply to this packet with icmp error.
127  *
128  * We do not reply, if:
129  *	- it was icmp error message.
130  *	- it is truncated, so that it is known, that protocol is ICMPV6
131  *	  (i.e. in the middle of some exthdr)
132  *
133  *	--ANK (980726)
134  */
135 
136 static bool is_ineligible(const struct sk_buff *skb)
137 {
138 	int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
139 	int len = skb->len - ptr;
140 	__u8 nexthdr = ipv6_hdr(skb)->nexthdr;
141 	__be16 frag_off;
142 
143 	if (len < 0)
144 		return true;
145 
146 	ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off);
147 	if (ptr < 0)
148 		return false;
149 	if (nexthdr == IPPROTO_ICMPV6) {
150 		u8 _type, *tp;
151 		tp = skb_header_pointer(skb,
152 			ptr+offsetof(struct icmp6hdr, icmp6_type),
153 			sizeof(_type), &_type);
154 
155 		/* Based on RFC 8200, Section 4.5 Fragment Header, return
156 		 * false if this is a fragment packet with no icmp header info.
157 		 */
158 		if (!tp && frag_off != 0)
159 			return false;
160 		else if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
161 			return true;
162 	}
163 	return false;
164 }
165 
166 static bool icmpv6_mask_allow(struct net *net, int type)
167 {
168 	if (type > ICMPV6_MSG_MAX)
169 		return true;
170 
171 	/* Limit if icmp type is set in ratemask. */
172 	if (!test_bit(type, net->ipv6.sysctl.icmpv6_ratemask))
173 		return true;
174 
175 	return false;
176 }
177 
178 static bool icmpv6_global_allow(struct net *net, int type,
179 				bool *apply_ratelimit)
180 {
181 	if (icmpv6_mask_allow(net, type))
182 		return true;
183 
184 	if (icmp_global_allow(net)) {
185 		*apply_ratelimit = true;
186 		return true;
187 	}
188 	__ICMP_INC_STATS(net, ICMP_MIB_RATELIMITGLOBAL);
189 	return false;
190 }
191 
192 /*
193  * Check the ICMP output rate limit
194  */
195 static bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
196 			       struct flowi6 *fl6, bool apply_ratelimit)
197 {
198 	struct net *net = sock_net(sk);
199 	struct net_device *dev;
200 	struct dst_entry *dst;
201 	bool res = false;
202 
203 	if (!apply_ratelimit)
204 		return true;
205 
206 	/*
207 	 * Look up the output route.
208 	 * XXX: perhaps the expire for routing entries cloned by
209 	 * this lookup should be more aggressive (not longer than timeout).
210 	 */
211 	dst = ip6_route_output(net, sk, fl6);
212 	rcu_read_lock();
213 	dev = dst_dev_rcu(dst);
214 	if (dst->error) {
215 		IP6_INC_STATS(net, ip6_dst_idev(dst),
216 			      IPSTATS_MIB_OUTNOROUTES);
217 	} else if (dev && (dev->flags & IFF_LOOPBACK)) {
218 		res = true;
219 	} else {
220 		int tmo = READ_ONCE(net->ipv6.sysctl.icmpv6_time);
221 		struct inet_peer *peer;
222 
223 		if (!tmo) {
224 			res = true;
225 		} else {
226 			peer = inet_getpeer_v6(net->ipv6.peers, &fl6->daddr);
227 			res = inet_peer_xrlim_allow(peer, tmo);
228 		}
229 	}
230 	rcu_read_unlock();
231 	if (!res)
232 		__ICMP6_INC_STATS(net, NULL, ICMP6_MIB_RATELIMITHOST);
233 	else
234 		icmp_global_consume(net);
235 	dst_release(dst);
236 	return res;
237 }
238 
239 static bool icmpv6_rt_has_prefsrc(struct sock *sk, u8 type,
240 				  struct flowi6 *fl6)
241 {
242 	struct net *net = sock_net(sk);
243 	struct dst_entry *dst;
244 	bool res = false;
245 
246 	dst = ip6_route_output(net, sk, fl6);
247 	if (!dst->error) {
248 		struct rt6_info *rt = dst_rt6_info(dst);
249 		struct in6_addr prefsrc;
250 
251 		rt6_get_prefsrc(rt, &prefsrc);
252 		res = !ipv6_addr_any(&prefsrc);
253 	}
254 	dst_release(dst);
255 	return res;
256 }
257 
258 /*
259  *	an inline helper for the "simple" if statement below
260  *	checks if parameter problem report is caused by an
261  *	unrecognized IPv6 option that has the Option Type
262  *	highest-order two bits set to 10
263  */
264 
265 static bool opt_unrec(struct sk_buff *skb, __u32 offset)
266 {
267 	u8 _optval, *op;
268 
269 	offset += skb_network_offset(skb);
270 	op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
271 	if (!op)
272 		return true;
273 	return (*op & 0xC0) == 0x80;
274 }
275 
276 void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
277 				struct icmp6hdr *thdr, int len)
278 {
279 	struct sk_buff *skb;
280 	struct icmp6hdr *icmp6h;
281 
282 	skb = skb_peek(&sk->sk_write_queue);
283 	if (!skb)
284 		return;
285 
286 	icmp6h = icmp6_hdr(skb);
287 	memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
288 	icmp6h->icmp6_cksum = 0;
289 
290 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
291 		skb->csum = csum_partial(icmp6h,
292 					sizeof(struct icmp6hdr), skb->csum);
293 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
294 						      &fl6->daddr,
295 						      len, fl6->flowi6_proto,
296 						      skb->csum);
297 	} else {
298 		__wsum tmp_csum = 0;
299 
300 		skb_queue_walk(&sk->sk_write_queue, skb) {
301 			tmp_csum = csum_add(tmp_csum, skb->csum);
302 		}
303 
304 		tmp_csum = csum_partial(icmp6h,
305 					sizeof(struct icmp6hdr), tmp_csum);
306 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
307 						      &fl6->daddr,
308 						      len, fl6->flowi6_proto,
309 						      tmp_csum);
310 	}
311 	ip6_push_pending_frames(sk);
312 }
313 
314 struct icmpv6_msg {
315 	struct sk_buff	*skb;
316 	int		offset;
317 	uint8_t		type;
318 };
319 
320 static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
321 {
322 	struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
323 	struct sk_buff *org_skb = msg->skb;
324 	__wsum csum;
325 
326 	csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
327 				      to, len);
328 	skb->csum = csum_block_add(skb->csum, csum, odd);
329 	if (!(msg->type & ICMPV6_INFOMSG_MASK))
330 		nf_ct_attach(skb, org_skb);
331 	return 0;
332 }
333 
334 #if IS_ENABLED(CONFIG_IPV6_MIP6)
335 static void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt)
336 {
337 	struct ipv6hdr *iph = ipv6_hdr(skb);
338 	struct ipv6_destopt_hao *hao;
339 	int off;
340 
341 	if (opt->dsthao) {
342 		off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
343 		if (likely(off >= 0)) {
344 			hao = (struct ipv6_destopt_hao *)
345 					(skb_network_header(skb) + off);
346 			/*
347 			 * The header can have moved or changed after
348 			 * ipv6_dest_hao() checked the length.
349 			 */
350 			if (hao->length == 16)
351 				swap(iph->saddr, hao->addr);
352 		}
353 	}
354 }
355 #else
356 static inline void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt) {}
357 #endif
358 
359 static struct dst_entry *icmpv6_route_lookup(struct net *net,
360 					     struct sk_buff *skb,
361 					     struct sock *sk,
362 					     struct flowi6 *fl6)
363 {
364 	struct dst_entry *dst, *dst2;
365 	struct flowi6 fl2;
366 	int err;
367 
368 	err = ip6_dst_lookup(net, sk, &dst, fl6);
369 	if (err)
370 		return ERR_PTR(err);
371 
372 	/*
373 	 * We won't send icmp if the destination is known
374 	 * anycast unless we need to treat anycast as unicast.
375 	 */
376 	if (!READ_ONCE(net->ipv6.sysctl.icmpv6_error_anycast_as_unicast) &&
377 	    ipv6_anycast_destination(dst, &fl6->daddr)) {
378 		net_dbg_ratelimited("icmp6_send: acast source\n");
379 		dst_release(dst);
380 		return ERR_PTR(-EINVAL);
381 	}
382 
383 	/* No need to clone since we're just using its address. */
384 	dst2 = dst;
385 
386 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
387 	if (!IS_ERR(dst)) {
388 		if (dst != dst2)
389 			return dst;
390 	} else {
391 		if (PTR_ERR(dst) == -EPERM)
392 			dst = NULL;
393 		else
394 			return dst;
395 	}
396 
397 	err = xfrm_decode_session_reverse(net, skb, flowi6_to_flowi(&fl2), AF_INET6);
398 	if (err)
399 		goto relookup_failed;
400 
401 	err = ip6_dst_lookup(net, sk, &dst2, &fl2);
402 	if (err)
403 		goto relookup_failed;
404 
405 	dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
406 	if (!IS_ERR(dst2)) {
407 		dst_release(dst);
408 		dst = dst2;
409 	} else {
410 		err = PTR_ERR(dst2);
411 		if (err == -EPERM) {
412 			dst_release(dst);
413 			return dst2;
414 		} else
415 			goto relookup_failed;
416 	}
417 
418 relookup_failed:
419 	if (dst)
420 		return dst;
421 	return ERR_PTR(err);
422 }
423 
424 static struct net_device *icmp6_dev(const struct sk_buff *skb)
425 {
426 	struct net_device *dev = skb->dev;
427 
428 	/* for local traffic to local address, skb dev is the loopback
429 	 * device. Check if there is a dst attached to the skb and if so
430 	 * get the real device index. Same is needed for replies to a link
431 	 * local address on a device enslaved to an L3 master device
432 	 */
433 	if (unlikely(dev->ifindex == LOOPBACK_IFINDEX || netif_is_l3_master(skb->dev))) {
434 		const struct rt6_info *rt6 = skb_rt6_info(skb);
435 
436 		/* The destination could be an external IP in Ext Hdr (SRv6, RPL, etc.),
437 		 * and ip6_null_entry could be set to skb if no route is found.
438 		 */
439 		if (rt6 && rt6->rt6i_idev)
440 			dev = rt6->rt6i_idev->dev;
441 	}
442 
443 	return dev;
444 }
445 
446 static int icmp6_iif(const struct sk_buff *skb)
447 {
448 	return icmp6_dev(skb)->ifindex;
449 }
450 
451 struct icmp6_ext_iio_addr6_subobj {
452 	__be16 afi;
453 	__be16 reserved;
454 	struct in6_addr addr6;
455 };
456 
457 static unsigned int icmp6_ext_iio_len(void)
458 {
459 	return sizeof(struct icmp_extobj_hdr) +
460 		/* ifIndex */
461 		sizeof(__be32) +
462 		/* Interface Address Sub-Object */
463 		sizeof(struct icmp6_ext_iio_addr6_subobj) +
464 		/* Interface Name Sub-Object. Length must be a multiple of 4
465 		 * bytes.
466 		 */
467 		ALIGN(sizeof(struct icmp_ext_iio_name_subobj), 4) +
468 		/* MTU */
469 		sizeof(__be32);
470 }
471 
472 static unsigned int icmp6_ext_max_len(u8 ext_objs)
473 {
474 	unsigned int ext_max_len;
475 
476 	ext_max_len = sizeof(struct icmp_ext_hdr);
477 
478 	if (ext_objs & BIT(ICMP_ERR_EXT_IIO_IIF))
479 		ext_max_len += icmp6_ext_iio_len();
480 
481 	return ext_max_len;
482 }
483 
484 static struct in6_addr *icmp6_ext_iio_addr6_find(const struct net_device *dev)
485 {
486 	struct inet6_dev *in6_dev;
487 	struct inet6_ifaddr *ifa;
488 
489 	in6_dev = __in6_dev_get(dev);
490 	if (!in6_dev)
491 		return NULL;
492 
493 	/* It is unclear from RFC 5837 which IP address should be chosen, but
494 	 * it makes sense to choose a global unicast address.
495 	 */
496 	list_for_each_entry_rcu(ifa, &in6_dev->addr_list, if_list) {
497 		if (ifa->flags & (IFA_F_TENTATIVE | IFA_F_DADFAILED))
498 			continue;
499 		if (ipv6_addr_type(&ifa->addr) != IPV6_ADDR_UNICAST ||
500 		    ipv6_addr_src_scope(&ifa->addr) != IPV6_ADDR_SCOPE_GLOBAL)
501 			continue;
502 		return &ifa->addr;
503 	}
504 
505 	return NULL;
506 }
507 
508 static void icmp6_ext_iio_iif_append(struct net *net, struct sk_buff *skb,
509 				     int iif)
510 {
511 	struct icmp_ext_iio_name_subobj *name_subobj;
512 	struct icmp_extobj_hdr *objh;
513 	struct net_device *dev;
514 	struct in6_addr *addr6;
515 	__be32 data;
516 
517 	if (!iif)
518 		return;
519 
520 	/* Add the fields in the order specified by RFC 5837. */
521 	objh = skb_put(skb, sizeof(*objh));
522 	objh->class_num = ICMP_EXT_OBJ_CLASS_IIO;
523 	objh->class_type = ICMP_EXT_CTYPE_IIO_ROLE(ICMP_EXT_CTYPE_IIO_ROLE_IIF);
524 
525 	data = htonl(iif);
526 	skb_put_data(skb, &data, sizeof(__be32));
527 	objh->class_type |= ICMP_EXT_CTYPE_IIO_IFINDEX;
528 
529 	rcu_read_lock();
530 
531 	dev = dev_get_by_index_rcu(net, iif);
532 	if (!dev)
533 		goto out;
534 
535 	addr6 = icmp6_ext_iio_addr6_find(dev);
536 	if (addr6) {
537 		struct icmp6_ext_iio_addr6_subobj *addr6_subobj;
538 
539 		addr6_subobj = skb_put_zero(skb, sizeof(*addr6_subobj));
540 		addr6_subobj->afi = htons(ICMP_AFI_IP6);
541 		addr6_subobj->addr6 = *addr6;
542 		objh->class_type |= ICMP_EXT_CTYPE_IIO_IPADDR;
543 	}
544 
545 	name_subobj = skb_put_zero(skb, ALIGN(sizeof(*name_subobj), 4));
546 	name_subobj->len = ALIGN(sizeof(*name_subobj), 4);
547 	netdev_copy_name(dev, name_subobj->name);
548 	objh->class_type |= ICMP_EXT_CTYPE_IIO_NAME;
549 
550 	data = htonl(READ_ONCE(dev->mtu));
551 	skb_put_data(skb, &data, sizeof(__be32));
552 	objh->class_type |= ICMP_EXT_CTYPE_IIO_MTU;
553 
554 out:
555 	rcu_read_unlock();
556 	objh->length = htons(skb_tail_pointer(skb) - (unsigned char *)objh);
557 }
558 
559 static void icmp6_ext_objs_append(struct net *net, struct sk_buff *skb,
560 				  u8 ext_objs, int iif)
561 {
562 	if (ext_objs & BIT(ICMP_ERR_EXT_IIO_IIF))
563 		icmp6_ext_iio_iif_append(net, skb, iif);
564 }
565 
566 static struct sk_buff *
567 icmp6_ext_append(struct net *net, struct sk_buff *skb_in,
568 		 struct icmp6hdr *icmp6h, unsigned int room, int iif)
569 {
570 	unsigned int payload_len, ext_max_len, ext_len;
571 	struct icmp_ext_hdr *ext_hdr;
572 	struct sk_buff *skb;
573 	u8 ext_objs;
574 	int nhoff;
575 
576 	switch (icmp6h->icmp6_type) {
577 	case ICMPV6_DEST_UNREACH:
578 	case ICMPV6_TIME_EXCEED:
579 		break;
580 	default:
581 		return NULL;
582 	}
583 
584 	/* Do not overwrite existing extensions. This can happen when we
585 	 * receive an ICMPv4 message with extensions from a tunnel and
586 	 * translate it to an ICMPv6 message towards an IPv6 host in the
587 	 * overlay network.
588 	 */
589 	if (icmp6h->icmp6_datagram_len)
590 		return NULL;
591 
592 	ext_objs = READ_ONCE(net->ipv6.sysctl.icmpv6_errors_extension_mask);
593 	if (!ext_objs)
594 		return NULL;
595 
596 	ext_max_len = icmp6_ext_max_len(ext_objs);
597 	if (ICMP_EXT_ORIG_DGRAM_MIN_LEN + ext_max_len > room)
598 		return NULL;
599 
600 	skb = skb_clone(skb_in, GFP_ATOMIC);
601 	if (!skb)
602 		return NULL;
603 
604 	nhoff = skb_network_offset(skb);
605 	payload_len = min(skb->len - nhoff, ICMP_EXT_ORIG_DGRAM_MIN_LEN);
606 
607 	if (!pskb_network_may_pull(skb, payload_len))
608 		goto free_skb;
609 
610 	if (pskb_trim(skb, nhoff + ICMP_EXT_ORIG_DGRAM_MIN_LEN) ||
611 	    __skb_put_padto(skb, nhoff + ICMP_EXT_ORIG_DGRAM_MIN_LEN, false))
612 		goto free_skb;
613 
614 	if (pskb_expand_head(skb, 0, ext_max_len, GFP_ATOMIC))
615 		goto free_skb;
616 
617 	ext_hdr = skb_put_zero(skb, sizeof(*ext_hdr));
618 	ext_hdr->version = ICMP_EXT_VERSION_2;
619 
620 	icmp6_ext_objs_append(net, skb, ext_objs, iif);
621 
622 	/* Do not send an empty extension structure. */
623 	ext_len = skb_tail_pointer(skb) - (unsigned char *)ext_hdr;
624 	if (ext_len == sizeof(*ext_hdr))
625 		goto free_skb;
626 
627 	ext_hdr->checksum = ip_compute_csum(ext_hdr, ext_len);
628 	/* The length of the original datagram in 64-bit words (RFC 4884). */
629 	icmp6h->icmp6_datagram_len = ICMP_EXT_ORIG_DGRAM_MIN_LEN / sizeof(u64);
630 
631 	return skb;
632 
633 free_skb:
634 	consume_skb(skb);
635 	return NULL;
636 }
637 
638 /*
639  *	Send an ICMP message in response to a packet in error
640  */
641 void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
642 		const struct in6_addr *force_saddr,
643 		const struct inet6_skb_parm *parm)
644 {
645 	struct inet6_dev *idev = NULL;
646 	struct ipv6hdr *hdr = ipv6_hdr(skb);
647 	struct sock *sk;
648 	struct net *net;
649 	struct ipv6_pinfo *np;
650 	const struct in6_addr *saddr = NULL;
651 	bool apply_ratelimit = false;
652 	struct sk_buff *ext_skb;
653 	struct dst_entry *dst;
654 	unsigned int room;
655 	struct icmp6hdr tmp_hdr;
656 	struct flowi6 fl6;
657 	struct icmpv6_msg msg;
658 	struct ipcm6_cookie ipc6;
659 	int iif = 0;
660 	int addr_type = 0;
661 	int len;
662 	u32 mark;
663 
664 	if ((u8 *)hdr < skb->head ||
665 	    (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
666 		return;
667 
668 	if (!skb->dev)
669 		return;
670 
671 	rcu_read_lock();
672 
673 	net = dev_net_rcu(skb->dev);
674 	mark = IP6_REPLY_MARK(net, skb->mark);
675 	/*
676 	 *	Make sure we respect the rules
677 	 *	i.e. RFC 1885 2.4(e)
678 	 *	Rule (e.1) is enforced by not using icmp6_send
679 	 *	in any code that processes icmp errors.
680 	 */
681 	addr_type = ipv6_addr_type(&hdr->daddr);
682 
683 	if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) ||
684 	    ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr))
685 		saddr = &hdr->daddr;
686 
687 	/*
688 	 *	Dest addr check
689 	 */
690 
691 	if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) {
692 		if (type != ICMPV6_PKT_TOOBIG &&
693 		    !(type == ICMPV6_PARAMPROB &&
694 		      code == ICMPV6_UNK_OPTION &&
695 		      (opt_unrec(skb, info))))
696 			goto out;
697 
698 		saddr = NULL;
699 	}
700 
701 	addr_type = ipv6_addr_type(&hdr->saddr);
702 
703 	/*
704 	 *	Source addr check
705 	 */
706 
707 	if (__ipv6_addr_needs_scope_id(addr_type)) {
708 		iif = icmp6_iif(skb);
709 	} else {
710 		/*
711 		 * The source device is used for looking up which routing table
712 		 * to use for sending an ICMP error.
713 		 */
714 		iif = l3mdev_master_ifindex(skb->dev);
715 	}
716 
717 	/*
718 	 *	Must not send error if the source does not uniquely
719 	 *	identify a single node (RFC2463 Section 2.4).
720 	 *	We check unspecified / multicast addresses here,
721 	 *	and anycast addresses will be checked later.
722 	 */
723 	if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
724 		net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n",
725 				    &hdr->saddr, &hdr->daddr);
726 		goto out;
727 	}
728 
729 	/*
730 	 *	Never answer to a ICMP packet.
731 	 */
732 	if (is_ineligible(skb)) {
733 		net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n",
734 				    &hdr->saddr, &hdr->daddr);
735 		goto out;
736 	}
737 
738 	/* Needed by both icmpv6_global_allow and icmpv6_xmit_lock */
739 	local_bh_disable();
740 
741 	/* Check global sysctl_icmp_msgs_per_sec ratelimit */
742 	if (!(skb->dev->flags & IFF_LOOPBACK) &&
743 	    !icmpv6_global_allow(net, type, &apply_ratelimit))
744 		goto out_bh_enable;
745 
746 	mip6_addr_swap(skb, parm);
747 
748 	sk = icmpv6_xmit_lock(net);
749 	if (!sk)
750 		goto out_bh_enable;
751 
752 	memset(&fl6, 0, sizeof(fl6));
753 	fl6.flowi6_proto = IPPROTO_ICMPV6;
754 	fl6.daddr = hdr->saddr;
755 	if (force_saddr)
756 		saddr = force_saddr;
757 	if (saddr) {
758 		fl6.saddr = *saddr;
759 	} else if (!icmpv6_rt_has_prefsrc(sk, type, &fl6)) {
760 		/* select a more meaningful saddr from input if */
761 		struct net_device *in_netdev;
762 
763 		in_netdev = dev_get_by_index(net, parm->iif);
764 		if (in_netdev) {
765 			ipv6_dev_get_saddr(net, in_netdev, &fl6.daddr,
766 					   inet6_sk(sk)->srcprefs,
767 					   &fl6.saddr);
768 			dev_put(in_netdev);
769 		}
770 	}
771 	fl6.flowi6_mark = mark;
772 	fl6.flowi6_oif = iif;
773 	fl6.fl6_icmp_type = type;
774 	fl6.fl6_icmp_code = code;
775 	fl6.flowi6_uid = sock_net_uid(net, NULL);
776 	fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, NULL);
777 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
778 
779 	np = inet6_sk(sk);
780 
781 	if (!icmpv6_xrlim_allow(sk, type, &fl6, apply_ratelimit))
782 		goto out_unlock;
783 
784 	tmp_hdr.icmp6_type = type;
785 	tmp_hdr.icmp6_code = code;
786 	tmp_hdr.icmp6_cksum = 0;
787 	tmp_hdr.icmp6_pointer = htonl(info);
788 
789 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
790 		fl6.flowi6_oif = READ_ONCE(np->mcast_oif);
791 	else if (!fl6.flowi6_oif)
792 		fl6.flowi6_oif = READ_ONCE(np->ucast_oif);
793 
794 	ipcm6_init_sk(&ipc6, sk);
795 	ipc6.sockc.mark = mark;
796 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
797 
798 	dst = icmpv6_route_lookup(net, skb, sk, &fl6);
799 	if (IS_ERR(dst))
800 		goto out_unlock;
801 
802 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
803 
804 	msg.skb = skb;
805 	msg.offset = skb_network_offset(skb);
806 	msg.type = type;
807 
808 	room = IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr);
809 	ext_skb = icmp6_ext_append(net, skb, &tmp_hdr, room, parm->iif);
810 	if (ext_skb)
811 		msg.skb = ext_skb;
812 
813 	len = msg.skb->len - msg.offset;
814 	len = min_t(unsigned int, len, room);
815 	if (len < 0) {
816 		net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n",
817 				    &hdr->saddr, &hdr->daddr);
818 		goto out_dst_release;
819 	}
820 
821 	idev = __in6_dev_get(skb->dev);
822 
823 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
824 			    len + sizeof(struct icmp6hdr),
825 			    sizeof(struct icmp6hdr),
826 			    &ipc6, &fl6, dst_rt6_info(dst),
827 			    MSG_DONTWAIT)) {
828 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
829 		ip6_flush_pending_frames(sk);
830 	} else {
831 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
832 					   len + sizeof(struct icmp6hdr));
833 	}
834 
835 out_dst_release:
836 	if (ext_skb)
837 		consume_skb(ext_skb);
838 	dst_release(dst);
839 out_unlock:
840 	icmpv6_xmit_unlock(sk);
841 out_bh_enable:
842 	local_bh_enable();
843 out:
844 	rcu_read_unlock();
845 }
846 EXPORT_SYMBOL(icmp6_send);
847 
848 /* Slightly more convenient version of icmp6_send with drop reasons.
849  */
850 void icmpv6_param_prob_reason(struct sk_buff *skb, u8 code, int pos,
851 			      enum skb_drop_reason reason)
852 {
853 	icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL, IP6CB(skb));
854 	kfree_skb_reason(skb, reason);
855 }
856 
857 /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
858  * if sufficient data bytes are available
859  * @nhs is the size of the tunnel header(s) :
860  *  Either an IPv4 header for SIT encap
861  *         an IPv4 header + GRE header for GRE encap
862  */
863 int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type,
864 			       unsigned int data_len)
865 {
866 	struct in6_addr temp_saddr;
867 	struct rt6_info *rt;
868 	struct sk_buff *skb2;
869 	u32 info = 0;
870 
871 	if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8))
872 		return 1;
873 
874 	/* RFC 4884 (partial) support for ICMP extensions */
875 	if (data_len < 128 || (data_len & 7) || skb->len < data_len)
876 		data_len = 0;
877 
878 	skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC);
879 
880 	if (!skb2)
881 		return 1;
882 
883 	/* Remove debris left by IPv4 stack. */
884 	memset(IP6CB(skb2), 0, sizeof(*IP6CB(skb2)));
885 
886 	skb_dst_drop(skb2);
887 	skb_pull(skb2, nhs);
888 	skb_reset_network_header(skb2);
889 
890 	rt = rt6_lookup(dev_net_rcu(skb->dev), &ipv6_hdr(skb2)->saddr,
891 			NULL, 0, skb, 0);
892 
893 	if (rt && rt->dst.dev)
894 		skb2->dev = rt->dst.dev;
895 
896 	ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr);
897 
898 	if (data_len) {
899 		/* RFC 4884 (partial) support :
900 		 * insert 0 padding at the end, before the extensions
901 		 */
902 		__skb_push(skb2, nhs);
903 		skb_reset_network_header(skb2);
904 		memmove(skb2->data, skb2->data + nhs, data_len - nhs);
905 		memset(skb2->data + data_len - nhs, 0, nhs);
906 		/* RFC 4884 4.5 : Length is measured in 64-bit words,
907 		 * and stored in reserved[0]
908 		 */
909 		info = (data_len/8) << 24;
910 	}
911 	if (type == ICMP_TIME_EXCEEDED)
912 		icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
913 			   info, &temp_saddr, IP6CB(skb2));
914 	else
915 		icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH,
916 			   info, &temp_saddr, IP6CB(skb2));
917 	if (rt)
918 		ip6_rt_put(rt);
919 
920 	kfree_skb(skb2);
921 
922 	return 0;
923 }
924 EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach);
925 
926 static enum skb_drop_reason icmpv6_echo_reply(struct sk_buff *skb)
927 {
928 	struct net *net = dev_net_rcu(skb->dev);
929 	struct sock *sk;
930 	struct inet6_dev *idev;
931 	struct ipv6_pinfo *np;
932 	const struct in6_addr *saddr = NULL;
933 	struct icmp6hdr *icmph = icmp6_hdr(skb);
934 	bool apply_ratelimit = false;
935 	struct icmp6hdr tmp_hdr;
936 	struct flowi6 fl6;
937 	struct icmpv6_msg msg;
938 	struct dst_entry *dst;
939 	struct ipcm6_cookie ipc6;
940 	u32 mark = IP6_REPLY_MARK(net, skb->mark);
941 	SKB_DR(reason);
942 	bool acast;
943 	u8 type;
944 
945 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
946 	    net->ipv6.sysctl.icmpv6_echo_ignore_multicast)
947 		return reason;
948 
949 	saddr = &ipv6_hdr(skb)->daddr;
950 
951 	acast = ipv6_anycast_destination(skb_dst(skb), saddr);
952 	if (acast && net->ipv6.sysctl.icmpv6_echo_ignore_anycast)
953 		return reason;
954 
955 	if (!ipv6_unicast_destination(skb) &&
956 	    !(net->ipv6.sysctl.anycast_src_echo_reply && acast))
957 		saddr = NULL;
958 
959 	if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
960 		type = ICMPV6_EXT_ECHO_REPLY;
961 	else
962 		type = ICMPV6_ECHO_REPLY;
963 
964 	memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
965 	tmp_hdr.icmp6_type = type;
966 
967 	memset(&fl6, 0, sizeof(fl6));
968 	if (READ_ONCE(net->ipv6.sysctl.flowlabel_reflect) &
969 	    FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES)
970 		fl6.flowlabel = ip6_flowlabel(ipv6_hdr(skb));
971 
972 	fl6.flowi6_proto = IPPROTO_ICMPV6;
973 	fl6.daddr = ipv6_hdr(skb)->saddr;
974 	if (saddr)
975 		fl6.saddr = *saddr;
976 	fl6.flowi6_oif = ipv6_addr_loopback(&fl6.daddr) ?
977 			 skb->dev->ifindex :
978 			 icmp6_iif(skb);
979 	fl6.fl6_icmp_type = type;
980 	fl6.flowi6_mark = mark;
981 	fl6.flowi6_uid = sock_net_uid(net, NULL);
982 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
983 
984 	local_bh_disable();
985 	sk = icmpv6_xmit_lock(net);
986 	if (!sk)
987 		goto out_bh_enable;
988 	np = inet6_sk(sk);
989 
990 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
991 		fl6.flowi6_oif = READ_ONCE(np->mcast_oif);
992 	else if (!fl6.flowi6_oif)
993 		fl6.flowi6_oif = READ_ONCE(np->ucast_oif);
994 
995 	if (ip6_dst_lookup(net, sk, &dst, &fl6))
996 		goto out;
997 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
998 	if (IS_ERR(dst))
999 		goto out;
1000 
1001 	/* Check the ratelimit */
1002 	if ((!(skb->dev->flags & IFF_LOOPBACK) &&
1003 	    !icmpv6_global_allow(net, ICMPV6_ECHO_REPLY, &apply_ratelimit)) ||
1004 	    !icmpv6_xrlim_allow(sk, ICMPV6_ECHO_REPLY, &fl6, apply_ratelimit))
1005 		goto out_dst_release;
1006 
1007 	idev = __in6_dev_get(skb->dev);
1008 
1009 	msg.skb = skb;
1010 	msg.offset = 0;
1011 	msg.type = type;
1012 
1013 	ipcm6_init_sk(&ipc6, sk);
1014 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1015 	ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb));
1016 	ipc6.sockc.mark = mark;
1017 
1018 	if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
1019 		if (!icmp_build_probe(skb, (struct icmphdr *)&tmp_hdr))
1020 			goto out_dst_release;
1021 
1022 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
1023 			    skb->len + sizeof(struct icmp6hdr),
1024 			    sizeof(struct icmp6hdr), &ipc6, &fl6,
1025 			    dst_rt6_info(dst), MSG_DONTWAIT)) {
1026 		__ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
1027 		ip6_flush_pending_frames(sk);
1028 	} else {
1029 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
1030 					   skb->len + sizeof(struct icmp6hdr));
1031 		reason = SKB_CONSUMED;
1032 	}
1033 out_dst_release:
1034 	dst_release(dst);
1035 out:
1036 	icmpv6_xmit_unlock(sk);
1037 out_bh_enable:
1038 	local_bh_enable();
1039 	return reason;
1040 }
1041 
1042 enum skb_drop_reason icmpv6_notify(struct sk_buff *skb, u8 type,
1043 				   u8 code, __be32 info)
1044 {
1045 	struct inet6_skb_parm *opt = IP6CB(skb);
1046 	struct net *net = dev_net_rcu(skb->dev);
1047 	const struct inet6_protocol *ipprot;
1048 	enum skb_drop_reason reason;
1049 	int inner_offset;
1050 	__be16 frag_off;
1051 	u8 nexthdr;
1052 
1053 	reason = pskb_may_pull_reason(skb, sizeof(struct ipv6hdr));
1054 	if (reason != SKB_NOT_DROPPED_YET)
1055 		goto out;
1056 
1057 	seg6_icmp_srh(skb, opt);
1058 
1059 	nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
1060 	if (ipv6_ext_hdr(nexthdr)) {
1061 		/* now skip over extension headers */
1062 		inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
1063 						&nexthdr, &frag_off);
1064 		if (inner_offset < 0) {
1065 			SKB_DR_SET(reason, IPV6_BAD_EXTHDR);
1066 			goto out;
1067 		}
1068 	} else {
1069 		inner_offset = sizeof(struct ipv6hdr);
1070 	}
1071 
1072 	/* Checkin header including 8 bytes of inner protocol header. */
1073 	reason = pskb_may_pull_reason(skb, inner_offset + 8);
1074 	if (reason != SKB_NOT_DROPPED_YET)
1075 		goto out;
1076 
1077 	if (nexthdr == IPPROTO_RAW) {
1078 		/* Add a more specific reason later ? */
1079 		reason = SKB_DROP_REASON_NOT_SPECIFIED;
1080 		goto out;
1081 	}
1082 
1083 	/* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
1084 	   Without this we will not able f.e. to make source routed
1085 	   pmtu discovery.
1086 	   Corresponding argument (opt) to notifiers is already added.
1087 	   --ANK (980726)
1088 	 */
1089 
1090 	ipprot = rcu_dereference(inet6_protos[nexthdr]);
1091 	if (ipprot && ipprot->err_handler)
1092 		ipprot->err_handler(skb, opt, type, code, inner_offset, info);
1093 
1094 	raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
1095 	return SKB_CONSUMED;
1096 
1097 out:
1098 	__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
1099 	return reason;
1100 }
1101 
1102 /*
1103  *	Handle icmp messages
1104  */
1105 
1106 static int icmpv6_rcv(struct sk_buff *skb)
1107 {
1108 	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
1109 	struct net *net = dev_net_rcu(skb->dev);
1110 	struct net_device *dev = icmp6_dev(skb);
1111 	struct inet6_dev *idev = __in6_dev_get(dev);
1112 	struct icmp6hdr *hdr;
1113 	u8 type;
1114 
1115 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1116 		struct sec_path *sp = skb_sec_path(skb);
1117 		int nh;
1118 
1119 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
1120 				 XFRM_STATE_ICMP)) {
1121 			reason = SKB_DROP_REASON_XFRM_POLICY;
1122 			goto drop_no_count;
1123 		}
1124 
1125 		if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
1126 			goto drop_no_count;
1127 
1128 		nh = skb_network_offset(skb);
1129 		skb_set_network_header(skb, sizeof(*hdr));
1130 
1131 		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN,
1132 						skb)) {
1133 			reason = SKB_DROP_REASON_XFRM_POLICY;
1134 			goto drop_no_count;
1135 		}
1136 
1137 		skb_set_network_header(skb, nh);
1138 	}
1139 
1140 	__ICMP6_INC_STATS(dev_net_rcu(dev), idev, ICMP6_MIB_INMSGS);
1141 
1142 	if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) {
1143 		net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n",
1144 				    &ipv6_hdr(skb)->saddr,
1145 				    &ipv6_hdr(skb)->daddr);
1146 		goto csum_error;
1147 	}
1148 
1149 	if (!pskb_pull(skb, sizeof(*hdr)))
1150 		goto discard_it;
1151 
1152 	hdr = icmp6_hdr(skb);
1153 
1154 	type = hdr->icmp6_type;
1155 
1156 	ICMP6MSGIN_INC_STATS(dev_net_rcu(dev), idev, type);
1157 
1158 	switch (type) {
1159 	case ICMPV6_ECHO_REQUEST:
1160 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all)
1161 			reason = icmpv6_echo_reply(skb);
1162 		break;
1163 	case ICMPV6_EXT_ECHO_REQUEST:
1164 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all &&
1165 		    READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe))
1166 			reason = icmpv6_echo_reply(skb);
1167 		break;
1168 
1169 	case ICMPV6_ECHO_REPLY:
1170 	case ICMPV6_EXT_ECHO_REPLY:
1171 		ping_rcv(skb);
1172 		return 0;
1173 
1174 	case ICMPV6_PKT_TOOBIG:
1175 		/* BUGGG_FUTURE: if packet contains rthdr, we cannot update
1176 		   standard destination cache. Seems, only "advanced"
1177 		   destination cache will allow to solve this problem
1178 		   --ANK (980726)
1179 		 */
1180 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1181 			goto discard_it;
1182 		hdr = icmp6_hdr(skb);
1183 
1184 		/* to notify */
1185 		fallthrough;
1186 	case ICMPV6_DEST_UNREACH:
1187 	case ICMPV6_TIME_EXCEED:
1188 	case ICMPV6_PARAMPROB:
1189 		reason = icmpv6_notify(skb, type, hdr->icmp6_code,
1190 				       hdr->icmp6_mtu);
1191 		break;
1192 
1193 	case NDISC_ROUTER_SOLICITATION:
1194 	case NDISC_ROUTER_ADVERTISEMENT:
1195 	case NDISC_NEIGHBOUR_SOLICITATION:
1196 	case NDISC_NEIGHBOUR_ADVERTISEMENT:
1197 	case NDISC_REDIRECT:
1198 		reason = ndisc_rcv(skb);
1199 		break;
1200 
1201 	case ICMPV6_MGM_QUERY:
1202 		igmp6_event_query(skb);
1203 		return 0;
1204 
1205 	case ICMPV6_MGM_REPORT:
1206 		igmp6_event_report(skb);
1207 		return 0;
1208 
1209 	case ICMPV6_MGM_REDUCTION:
1210 	case ICMPV6_NI_QUERY:
1211 	case ICMPV6_NI_REPLY:
1212 	case ICMPV6_MLD2_REPORT:
1213 	case ICMPV6_DHAAD_REQUEST:
1214 	case ICMPV6_DHAAD_REPLY:
1215 	case ICMPV6_MOBILE_PREFIX_SOL:
1216 	case ICMPV6_MOBILE_PREFIX_ADV:
1217 		break;
1218 
1219 	default:
1220 		/* informational */
1221 		if (type & ICMPV6_INFOMSG_MASK)
1222 			break;
1223 
1224 		net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n",
1225 				    &ipv6_hdr(skb)->saddr,
1226 				    &ipv6_hdr(skb)->daddr);
1227 
1228 		/*
1229 		 * error of unknown type.
1230 		 * must pass to upper level
1231 		 */
1232 
1233 		reason = icmpv6_notify(skb, type, hdr->icmp6_code,
1234 				       hdr->icmp6_mtu);
1235 	}
1236 
1237 	/* until the v6 path can be better sorted assume failure and
1238 	 * preserve the status quo behaviour for the rest of the paths to here
1239 	 */
1240 	if (reason)
1241 		kfree_skb_reason(skb, reason);
1242 	else
1243 		consume_skb(skb);
1244 
1245 	return 0;
1246 
1247 csum_error:
1248 	reason = SKB_DROP_REASON_ICMP_CSUM;
1249 	__ICMP6_INC_STATS(dev_net_rcu(dev), idev, ICMP6_MIB_CSUMERRORS);
1250 discard_it:
1251 	__ICMP6_INC_STATS(dev_net_rcu(dev), idev, ICMP6_MIB_INERRORS);
1252 drop_no_count:
1253 	kfree_skb_reason(skb, reason);
1254 	return 0;
1255 }
1256 
1257 void icmpv6_flow_init(const struct sock *sk, struct flowi6 *fl6, u8 type,
1258 		      const struct in6_addr *saddr,
1259 		      const struct in6_addr *daddr, int oif)
1260 {
1261 	memset(fl6, 0, sizeof(*fl6));
1262 	fl6->saddr = *saddr;
1263 	fl6->daddr = *daddr;
1264 	fl6->flowi6_proto	= IPPROTO_ICMPV6;
1265 	fl6->fl6_icmp_type	= type;
1266 	fl6->fl6_icmp_code	= 0;
1267 	fl6->flowi6_oif		= oif;
1268 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
1269 }
1270 
1271 int __init icmpv6_init(void)
1272 {
1273 	struct sock *sk;
1274 	int err, i;
1275 
1276 	for_each_possible_cpu(i) {
1277 		err = inet_ctl_sock_create(&sk, PF_INET6,
1278 					   SOCK_RAW, IPPROTO_ICMPV6, &init_net);
1279 		if (err < 0) {
1280 			pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
1281 			       err);
1282 			return err;
1283 		}
1284 
1285 		per_cpu(ipv6_icmp_sk, i) = sk;
1286 
1287 		/* Enough space for 2 64K ICMP packets, including
1288 		 * sk_buff struct overhead.
1289 		 */
1290 		sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
1291 	}
1292 
1293 	err = -EAGAIN;
1294 	if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
1295 		goto fail;
1296 
1297 	return 0;
1298 
1299 fail:
1300 	pr_err("Failed to register ICMP6 protocol\n");
1301 	return err;
1302 }
1303 
1304 void icmpv6_cleanup(void)
1305 {
1306 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
1307 }
1308 
1309 
1310 static const struct icmp6_err {
1311 	int err;
1312 	int fatal;
1313 } tab_unreach[] = {
1314 	{	/* NOROUTE */
1315 		.err	= ENETUNREACH,
1316 		.fatal	= 0,
1317 	},
1318 	{	/* ADM_PROHIBITED */
1319 		.err	= EACCES,
1320 		.fatal	= 1,
1321 	},
1322 	{	/* Was NOT_NEIGHBOUR, now reserved */
1323 		.err	= EHOSTUNREACH,
1324 		.fatal	= 0,
1325 	},
1326 	{	/* ADDR_UNREACH	*/
1327 		.err	= EHOSTUNREACH,
1328 		.fatal	= 0,
1329 	},
1330 	{	/* PORT_UNREACH	*/
1331 		.err	= ECONNREFUSED,
1332 		.fatal	= 1,
1333 	},
1334 	{	/* POLICY_FAIL */
1335 		.err	= EACCES,
1336 		.fatal	= 1,
1337 	},
1338 	{	/* REJECT_ROUTE	*/
1339 		.err	= EACCES,
1340 		.fatal	= 1,
1341 	},
1342 };
1343 
1344 int icmpv6_err_convert(u8 type, u8 code, int *err)
1345 {
1346 	int fatal = 0;
1347 
1348 	*err = EPROTO;
1349 
1350 	switch (type) {
1351 	case ICMPV6_DEST_UNREACH:
1352 		fatal = 1;
1353 		if (code < ARRAY_SIZE(tab_unreach)) {
1354 			*err  = tab_unreach[code].err;
1355 			fatal = tab_unreach[code].fatal;
1356 		}
1357 		break;
1358 
1359 	case ICMPV6_PKT_TOOBIG:
1360 		*err = EMSGSIZE;
1361 		break;
1362 
1363 	case ICMPV6_PARAMPROB:
1364 		*err = EPROTO;
1365 		fatal = 1;
1366 		break;
1367 
1368 	case ICMPV6_TIME_EXCEED:
1369 		*err = EHOSTUNREACH;
1370 		break;
1371 	}
1372 
1373 	return fatal;
1374 }
1375 EXPORT_SYMBOL(icmpv6_err_convert);
1376 
1377 #ifdef CONFIG_SYSCTL
1378 
1379 static u32 icmpv6_errors_extension_mask_all =
1380 	GENMASK_U8(ICMP_ERR_EXT_COUNT - 1, 0);
1381 
1382 static const struct ctl_table ipv6_icmp_table_template[] = {
1383 	{
1384 		.procname	= "ratelimit",
1385 		.data		= &init_net.ipv6.sysctl.icmpv6_time,
1386 		.maxlen		= sizeof(int),
1387 		.mode		= 0644,
1388 		.proc_handler	= proc_dointvec_ms_jiffies,
1389 	},
1390 	{
1391 		.procname	= "echo_ignore_all",
1392 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_all,
1393 		.maxlen		= sizeof(u8),
1394 		.mode		= 0644,
1395 		.proc_handler = proc_dou8vec_minmax,
1396 	},
1397 	{
1398 		.procname	= "echo_ignore_multicast",
1399 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_multicast,
1400 		.maxlen		= sizeof(u8),
1401 		.mode		= 0644,
1402 		.proc_handler = proc_dou8vec_minmax,
1403 	},
1404 	{
1405 		.procname	= "echo_ignore_anycast",
1406 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_anycast,
1407 		.maxlen		= sizeof(u8),
1408 		.mode		= 0644,
1409 		.proc_handler = proc_dou8vec_minmax,
1410 	},
1411 	{
1412 		.procname	= "ratemask",
1413 		.data		= &init_net.ipv6.sysctl.icmpv6_ratemask_ptr,
1414 		.maxlen		= ICMPV6_MSG_MAX + 1,
1415 		.mode		= 0644,
1416 		.proc_handler = proc_do_large_bitmap,
1417 	},
1418 	{
1419 		.procname	= "error_anycast_as_unicast",
1420 		.data		= &init_net.ipv6.sysctl.icmpv6_error_anycast_as_unicast,
1421 		.maxlen		= sizeof(u8),
1422 		.mode		= 0644,
1423 		.proc_handler	= proc_dou8vec_minmax,
1424 		.extra1		= SYSCTL_ZERO,
1425 		.extra2		= SYSCTL_ONE,
1426 	},
1427 	{
1428 		.procname	= "errors_extension_mask",
1429 		.data		= &init_net.ipv6.sysctl.icmpv6_errors_extension_mask,
1430 		.maxlen		= sizeof(u8),
1431 		.mode		= 0644,
1432 		.proc_handler	= proc_dou8vec_minmax,
1433 		.extra1		= SYSCTL_ZERO,
1434 		.extra2		= &icmpv6_errors_extension_mask_all,
1435 	},
1436 };
1437 
1438 struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
1439 {
1440 	struct ctl_table *table;
1441 
1442 	table = kmemdup(ipv6_icmp_table_template,
1443 			sizeof(ipv6_icmp_table_template),
1444 			GFP_KERNEL);
1445 
1446 	if (table) {
1447 		table[0].data = &net->ipv6.sysctl.icmpv6_time;
1448 		table[1].data = &net->ipv6.sysctl.icmpv6_echo_ignore_all;
1449 		table[2].data = &net->ipv6.sysctl.icmpv6_echo_ignore_multicast;
1450 		table[3].data = &net->ipv6.sysctl.icmpv6_echo_ignore_anycast;
1451 		table[4].data = &net->ipv6.sysctl.icmpv6_ratemask_ptr;
1452 		table[5].data = &net->ipv6.sysctl.icmpv6_error_anycast_as_unicast;
1453 		table[6].data = &net->ipv6.sysctl.icmpv6_errors_extension_mask;
1454 	}
1455 	return table;
1456 }
1457 
1458 size_t ipv6_icmp_sysctl_table_size(void)
1459 {
1460 	return ARRAY_SIZE(ipv6_icmp_table_template);
1461 }
1462 #endif
1463