xref: /openbmc/linux/net/ipv6/icmp.c (revision 4a2f7083)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Internet Control Message Protocol (ICMPv6)
41da177e4SLinus Torvalds  *	Linux INET6 implementation
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	Based on net/ipv4/icmp.c
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	RFC 1885
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*
151da177e4SLinus Torvalds  *	Changes:
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *	Andi Kleen		:	exception handling
181da177e4SLinus Torvalds  *	Andi Kleen			add rate limits. never reply to a icmp.
191da177e4SLinus Torvalds  *					add more length checks and other fixes.
201da177e4SLinus Torvalds  *	yoshfuji		:	ensure to sent parameter problem for
211da177e4SLinus Torvalds  *					fragments.
221da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI:	added sysctl for icmp rate limit.
231da177e4SLinus Torvalds  *	Randy Dunlap and
241da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI:	Per-interface statistics support
251da177e4SLinus Torvalds  *	Kazunori MIYAZAWA @USAGI:       change output process to use ip6_append_data
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
28f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
29f3213831SJoe Perches 
301da177e4SLinus Torvalds #include <linux/module.h>
311da177e4SLinus Torvalds #include <linux/errno.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/socket.h>
341da177e4SLinus Torvalds #include <linux/in.h>
351da177e4SLinus Torvalds #include <linux/kernel.h>
361da177e4SLinus Torvalds #include <linux/sockios.h>
371da177e4SLinus Torvalds #include <linux/net.h>
381da177e4SLinus Torvalds #include <linux/skbuff.h>
391da177e4SLinus Torvalds #include <linux/init.h>
40763ecff1SYasuyuki Kozakai #include <linux/netfilter.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
441da177e4SLinus Torvalds #include <linux/sysctl.h>
451da177e4SLinus Torvalds #endif
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <linux/inet.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491da177e4SLinus Torvalds #include <linux/icmpv6.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include <net/ip.h>
521da177e4SLinus Torvalds #include <net/sock.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include <net/ipv6.h>
551da177e4SLinus Torvalds #include <net/ip6_checksum.h>
566d0bfe22SLorenzo Colitti #include <net/ping.h>
571da177e4SLinus Torvalds #include <net/protocol.h>
581da177e4SLinus Torvalds #include <net/raw.h>
591da177e4SLinus Torvalds #include <net/rawv6.h>
60e4129440SAndrew Lunn #include <net/seg6.h>
611da177e4SLinus Torvalds #include <net/transp_v6.h>
621da177e4SLinus Torvalds #include <net/ip6_route.h>
631da177e4SLinus Torvalds #include <net/addrconf.h>
641da177e4SLinus Torvalds #include <net/icmp.h>
658b7817f3SHerbert Xu #include <net/xfrm.h>
661ed8516fSDenis V. Lunev #include <net/inet_common.h>
67825edac4SHannes Frederic Sowa #include <net/dsfield.h>
68ca254490SDavid Ahern #include <net/l3mdev.h>
691da177e4SLinus Torvalds 
707c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
711da177e4SLinus Torvalds 
726a17b961SEric Dumazet static DEFINE_PER_CPU(struct sock *, ipv6_icmp_sk);
731da177e4SLinus Torvalds 
7432bbd879SStefano Brivio static int icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
756f809da2SSteffen Klassert 		       u8 type, u8 code, int offset, __be32 info)
766f809da2SSteffen Klassert {
776d0bfe22SLorenzo Colitti 	/* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */
786d0bfe22SLorenzo Colitti 	struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset);
796f809da2SSteffen Klassert 	struct net *net = dev_net(skb->dev);
806f809da2SSteffen Klassert 
816f809da2SSteffen Klassert 	if (type == ICMPV6_PKT_TOOBIG)
825f379ef5SGeorg Kohmann 		ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0, sock_net_uid(net, NULL));
836f809da2SSteffen Klassert 	else if (type == NDISC_REDIRECT)
84e2d118a1SLorenzo Colitti 		ip6_redirect(skb, net, skb->dev->ifindex, 0,
85e2d118a1SLorenzo Colitti 			     sock_net_uid(net, NULL));
866d0bfe22SLorenzo Colitti 
876d0bfe22SLorenzo Colitti 	if (!(type & ICMPV6_INFOMSG_MASK))
886d0bfe22SLorenzo Colitti 		if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST)
89dcb94b88SHannes Frederic Sowa 			ping_err(skb, offset, ntohl(info));
9032bbd879SStefano Brivio 
9132bbd879SStefano Brivio 	return 0;
926f809da2SSteffen Klassert }
936f809da2SSteffen Klassert 
94e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb);
951da177e4SLinus Torvalds 
9641135cc8SAlexey Dobriyan static const struct inet6_protocol icmpv6_protocol = {
971da177e4SLinus Torvalds 	.handler	=	icmpv6_rcv,
986f809da2SSteffen Klassert 	.err_handler	=	icmpv6_err,
998b7817f3SHerbert Xu 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1001da177e4SLinus Torvalds };
1011da177e4SLinus Torvalds 
1027ba91ecbSJesper Dangaard Brouer /* Called with BH disabled */
1036a17b961SEric Dumazet static struct sock *icmpv6_xmit_lock(struct net *net)
1041da177e4SLinus Torvalds {
105fdc0bde9SDenis V. Lunev 	struct sock *sk;
106fdc0bde9SDenis V. Lunev 
1076a17b961SEric Dumazet 	sk = this_cpu_read(ipv6_icmp_sk);
108405666dbSDenis V. Lunev 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
1091da177e4SLinus Torvalds 		/* This can happen if the output path (f.e. SIT or
1101da177e4SLinus Torvalds 		 * ip6ip6 tunnel) signals dst_link_failure() for an
1111da177e4SLinus Torvalds 		 * outgoing ICMP6 packet.
1121da177e4SLinus Torvalds 		 */
113fdc0bde9SDenis V. Lunev 		return NULL;
1141da177e4SLinus Torvalds 	}
1156a17b961SEric Dumazet 	sock_net_set(sk, net);
116fdc0bde9SDenis V. Lunev 	return sk;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1196a17b961SEric Dumazet static void icmpv6_xmit_unlock(struct sock *sk)
1201da177e4SLinus Torvalds {
1216a17b961SEric Dumazet 	sock_net_set(sk, &init_net);
1227ba91ecbSJesper Dangaard Brouer 	spin_unlock(&sk->sk_lock.slock);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds  * Figure out, may we reply to this packet with icmp error.
1271da177e4SLinus Torvalds  *
1281da177e4SLinus Torvalds  * We do not reply, if:
1291da177e4SLinus Torvalds  *	- it was icmp error message.
1301da177e4SLinus Torvalds  *	- it is truncated, so that it is known, that protocol is ICMPV6
1311da177e4SLinus Torvalds  *	  (i.e. in the middle of some exthdr)
1321da177e4SLinus Torvalds  *
1331da177e4SLinus Torvalds  *	--ANK (980726)
1341da177e4SLinus Torvalds  */
1351da177e4SLinus Torvalds 
136a50feda5SEric Dumazet static bool is_ineligible(const struct sk_buff *skb)
1371da177e4SLinus Torvalds {
1380660e03fSArnaldo Carvalho de Melo 	int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
1391da177e4SLinus Torvalds 	int len = skb->len - ptr;
1400660e03fSArnaldo Carvalho de Melo 	__u8 nexthdr = ipv6_hdr(skb)->nexthdr;
14175f2811cSJesse Gross 	__be16 frag_off;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	if (len < 0)
144a50feda5SEric Dumazet 		return true;
1451da177e4SLinus Torvalds 
14675f2811cSJesse Gross 	ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off);
1471da177e4SLinus Torvalds 	if (ptr < 0)
148a50feda5SEric Dumazet 		return false;
1491da177e4SLinus Torvalds 	if (nexthdr == IPPROTO_ICMPV6) {
1501da177e4SLinus Torvalds 		u8 _type, *tp;
1511da177e4SLinus Torvalds 		tp = skb_header_pointer(skb,
1521da177e4SLinus Torvalds 			ptr+offsetof(struct icmp6hdr, icmp6_type),
1531da177e4SLinus Torvalds 			sizeof(_type), &_type);
1542efdaaafSHangbin Liu 
1552efdaaafSHangbin Liu 		/* Based on RFC 8200, Section 4.5 Fragment Header, return
1562efdaaafSHangbin Liu 		 * false if this is a fragment packet with no icmp header info.
1572efdaaafSHangbin Liu 		 */
1582efdaaafSHangbin Liu 		if (!tp && frag_off != 0)
1592efdaaafSHangbin Liu 			return false;
1602efdaaafSHangbin Liu 		else if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
161a50feda5SEric Dumazet 			return true;
1621da177e4SLinus Torvalds 	}
163a50feda5SEric Dumazet 	return false;
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1660bc19985SStephen Suryaputra static bool icmpv6_mask_allow(struct net *net, int type)
167c0303efeSJesper Dangaard Brouer {
1680bc19985SStephen Suryaputra 	if (type > ICMPV6_MSG_MAX)
169c0303efeSJesper Dangaard Brouer 		return true;
170c0303efeSJesper Dangaard Brouer 
1710bc19985SStephen Suryaputra 	/* Limit if icmp type is set in ratemask. */
1720bc19985SStephen Suryaputra 	if (!test_bit(type, net->ipv6.sysctl.icmpv6_ratemask))
173c0303efeSJesper Dangaard Brouer 		return true;
174c0303efeSJesper Dangaard Brouer 
175c0303efeSJesper Dangaard Brouer 	return false;
176c0303efeSJesper Dangaard Brouer }
177c0303efeSJesper Dangaard Brouer 
1780bc19985SStephen Suryaputra static bool icmpv6_global_allow(struct net *net, int type)
179c0303efeSJesper Dangaard Brouer {
1800bc19985SStephen Suryaputra 	if (icmpv6_mask_allow(net, type))
181c0303efeSJesper Dangaard Brouer 		return true;
182c0303efeSJesper Dangaard Brouer 
183c0303efeSJesper Dangaard Brouer 	if (icmp_global_allow())
184c0303efeSJesper Dangaard Brouer 		return true;
185c0303efeSJesper Dangaard Brouer 
186c0303efeSJesper Dangaard Brouer 	return false;
187c0303efeSJesper Dangaard Brouer }
188c0303efeSJesper Dangaard Brouer 
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  * Check the ICMP output rate limit
1911da177e4SLinus Torvalds  */
1924cdf507dSEric Dumazet static bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
1934c9483b2SDavid S. Miller 			       struct flowi6 *fl6)
1941da177e4SLinus Torvalds {
1953b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
1964cdf507dSEric Dumazet 	struct dst_entry *dst;
19792d86829SDavid S. Miller 	bool res = false;
1981da177e4SLinus Torvalds 
1990bc19985SStephen Suryaputra 	if (icmpv6_mask_allow(net, type))
20092d86829SDavid S. Miller 		return true;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	/*
2031da177e4SLinus Torvalds 	 * Look up the output route.
2041da177e4SLinus Torvalds 	 * XXX: perhaps the expire for routing entries cloned by
2051da177e4SLinus Torvalds 	 * this lookup should be more aggressive (not longer than timeout).
2061da177e4SLinus Torvalds 	 */
2074c9483b2SDavid S. Miller 	dst = ip6_route_output(net, sk, fl6);
2081da177e4SLinus Torvalds 	if (dst->error) {
2093bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst),
210a11d206dSYOSHIFUJI Hideaki 			      IPSTATS_MIB_OUTNOROUTES);
2111da177e4SLinus Torvalds 	} else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
21292d86829SDavid S. Miller 		res = true;
2131da177e4SLinus Torvalds 	} else {
2141da177e4SLinus Torvalds 		struct rt6_info *rt = (struct rt6_info *)dst;
2159a43b709SBenjamin Thery 		int tmo = net->ipv6.sysctl.icmpv6_time;
216c0303efeSJesper Dangaard Brouer 		struct inet_peer *peer;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 		/* Give more bandwidth to wider prefixes. */
2191da177e4SLinus Torvalds 		if (rt->rt6i_dst.plen < 128)
2201da177e4SLinus Torvalds 			tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
2211da177e4SLinus Torvalds 
222c0303efeSJesper Dangaard Brouer 		peer = inet_getpeer_v6(net->ipv6.peers, &fl6->daddr, 1);
223fbfe95a4SDavid S. Miller 		res = inet_peer_xrlim_allow(peer, tmo);
2241d861aa4SDavid S. Miller 		if (peer)
2251d861aa4SDavid S. Miller 			inet_putpeer(peer);
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 	dst_release(dst);
2281da177e4SLinus Torvalds 	return res;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
231b93cfb9cSTim Stallard static bool icmpv6_rt_has_prefsrc(struct sock *sk, u8 type,
232b93cfb9cSTim Stallard 				  struct flowi6 *fl6)
233b93cfb9cSTim Stallard {
234b93cfb9cSTim Stallard 	struct net *net = sock_net(sk);
235b93cfb9cSTim Stallard 	struct dst_entry *dst;
236b93cfb9cSTim Stallard 	bool res = false;
237b93cfb9cSTim Stallard 
238b93cfb9cSTim Stallard 	dst = ip6_route_output(net, sk, fl6);
239b93cfb9cSTim Stallard 	if (!dst->error) {
240b93cfb9cSTim Stallard 		struct rt6_info *rt = (struct rt6_info *)dst;
241b93cfb9cSTim Stallard 		struct in6_addr prefsrc;
242b93cfb9cSTim Stallard 
243b93cfb9cSTim Stallard 		rt6_get_prefsrc(rt, &prefsrc);
244b93cfb9cSTim Stallard 		res = !ipv6_addr_any(&prefsrc);
245b93cfb9cSTim Stallard 	}
246b93cfb9cSTim Stallard 	dst_release(dst);
247b93cfb9cSTim Stallard 	return res;
248b93cfb9cSTim Stallard }
249b93cfb9cSTim Stallard 
2501da177e4SLinus Torvalds /*
2511da177e4SLinus Torvalds  *	an inline helper for the "simple" if statement below
2521da177e4SLinus Torvalds  *	checks if parameter problem report is caused by an
2531da177e4SLinus Torvalds  *	unrecognized IPv6 option that has the Option Type
2541da177e4SLinus Torvalds  *	highest-order two bits set to 10
2551da177e4SLinus Torvalds  */
2561da177e4SLinus Torvalds 
257a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	u8 _optval, *op;
2601da177e4SLinus Torvalds 
261bbe735e4SArnaldo Carvalho de Melo 	offset += skb_network_offset(skb);
2621da177e4SLinus Torvalds 	op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
26363159f29SIan Morris 	if (!op)
264a50feda5SEric Dumazet 		return true;
2651da177e4SLinus Torvalds 	return (*op & 0xC0) == 0x80;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2684e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
2696d0bfe22SLorenzo Colitti 				struct icmp6hdr *thdr, int len)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct sk_buff *skb;
2721da177e4SLinus Torvalds 	struct icmp6hdr *icmp6h;
2731da177e4SLinus Torvalds 
274e5d08d71SIan Morris 	skb = skb_peek(&sk->sk_write_queue);
27563159f29SIan Morris 	if (!skb)
2764e64b1edSJoe Perches 		return;
2771da177e4SLinus Torvalds 
278cc70ab26SArnaldo Carvalho de Melo 	icmp6h = icmp6_hdr(skb);
2791da177e4SLinus Torvalds 	memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
2801da177e4SLinus Torvalds 	icmp6h->icmp6_cksum = 0;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
28307f0757aSJoe Perches 		skb->csum = csum_partial(icmp6h,
2841da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), skb->csum);
2854c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2864c9483b2SDavid S. Miller 						      &fl6->daddr,
2874c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
2881da177e4SLinus Torvalds 						      skb->csum);
2891da177e4SLinus Torvalds 	} else {
290868c86bcSAl Viro 		__wsum tmp_csum = 0;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
2931da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
2941da177e4SLinus Torvalds 		}
2951da177e4SLinus Torvalds 
29607f0757aSJoe Perches 		tmp_csum = csum_partial(icmp6h,
2971da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), tmp_csum);
2984c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2994c9483b2SDavid S. Miller 						      &fl6->daddr,
3004c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
301868c86bcSAl Viro 						      tmp_csum);
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 	ip6_push_pending_frames(sk);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds struct icmpv6_msg {
3071da177e4SLinus Torvalds 	struct sk_buff	*skb;
3081da177e4SLinus Torvalds 	int		offset;
309763ecff1SYasuyuki Kozakai 	uint8_t		type;
3101da177e4SLinus Torvalds };
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
3151da177e4SLinus Torvalds 	struct sk_buff *org_skb = msg->skb;
3168d5930dfSAl Viro 	__wsum csum;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
3198d5930dfSAl Viro 				      to, len);
3201da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
321763ecff1SYasuyuki Kozakai 	if (!(msg->type & ICMPV6_INFOMSG_MASK))
322763ecff1SYasuyuki Kozakai 		nf_ct_attach(skb, org_skb);
3231da177e4SLinus Torvalds 	return 0;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
32607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
327ee576c47SJason A. Donenfeld static void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt)
32879383236SMasahide NAKAMURA {
3290660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
33079383236SMasahide NAKAMURA 	struct ipv6_destopt_hao *hao;
33179383236SMasahide NAKAMURA 	struct in6_addr tmp;
33279383236SMasahide NAKAMURA 	int off;
33379383236SMasahide NAKAMURA 
33479383236SMasahide NAKAMURA 	if (opt->dsthao) {
33579383236SMasahide NAKAMURA 		off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
33679383236SMasahide NAKAMURA 		if (likely(off >= 0)) {
337d56f90a7SArnaldo Carvalho de Melo 			hao = (struct ipv6_destopt_hao *)
338d56f90a7SArnaldo Carvalho de Melo 					(skb_network_header(skb) + off);
3394e3fd7a0SAlexey Dobriyan 			tmp = iph->saddr;
3404e3fd7a0SAlexey Dobriyan 			iph->saddr = hao->addr;
3414e3fd7a0SAlexey Dobriyan 			hao->addr = tmp;
34279383236SMasahide NAKAMURA 		}
34379383236SMasahide NAKAMURA 	}
34479383236SMasahide NAKAMURA }
34579383236SMasahide NAKAMURA #else
346ee576c47SJason A. Donenfeld static inline void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt) {}
34779383236SMasahide NAKAMURA #endif
34879383236SMasahide NAKAMURA 
349e8243534Sstephen hemminger static struct dst_entry *icmpv6_route_lookup(struct net *net,
350e8243534Sstephen hemminger 					     struct sk_buff *skb,
351e8243534Sstephen hemminger 					     struct sock *sk,
352e8243534Sstephen hemminger 					     struct flowi6 *fl6)
353b42835dbSDavid S. Miller {
354b42835dbSDavid S. Miller 	struct dst_entry *dst, *dst2;
3554c9483b2SDavid S. Miller 	struct flowi6 fl2;
356b42835dbSDavid S. Miller 	int err;
357b42835dbSDavid S. Miller 
358343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst, fl6);
359b42835dbSDavid S. Miller 	if (err)
360b42835dbSDavid S. Miller 		return ERR_PTR(err);
361b42835dbSDavid S. Miller 
362b42835dbSDavid S. Miller 	/*
363b42835dbSDavid S. Miller 	 * We won't send icmp if the destination is known
364b42835dbSDavid S. Miller 	 * anycast.
365b42835dbSDavid S. Miller 	 */
3662647a9b0SMartin KaFai Lau 	if (ipv6_anycast_destination(dst, &fl6->daddr)) {
367ba7a46f1SJoe Perches 		net_dbg_ratelimited("icmp6_send: acast source\n");
368b42835dbSDavid S. Miller 		dst_release(dst);
369b42835dbSDavid S. Miller 		return ERR_PTR(-EINVAL);
370b42835dbSDavid S. Miller 	}
371b42835dbSDavid S. Miller 
372b42835dbSDavid S. Miller 	/* No need to clone since we're just using its address. */
373b42835dbSDavid S. Miller 	dst2 = dst;
374b42835dbSDavid S. Miller 
3754c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
376452edd59SDavid S. Miller 	if (!IS_ERR(dst)) {
377b42835dbSDavid S. Miller 		if (dst != dst2)
378b42835dbSDavid S. Miller 			return dst;
379452edd59SDavid S. Miller 	} else {
380452edd59SDavid S. Miller 		if (PTR_ERR(dst) == -EPERM)
381b42835dbSDavid S. Miller 			dst = NULL;
382452edd59SDavid S. Miller 		else
383452edd59SDavid S. Miller 			return dst;
384b42835dbSDavid S. Miller 	}
385b42835dbSDavid S. Miller 
3864c9483b2SDavid S. Miller 	err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6);
387b42835dbSDavid S. Miller 	if (err)
388b42835dbSDavid S. Miller 		goto relookup_failed;
389b42835dbSDavid S. Miller 
390343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst2, &fl2);
391b42835dbSDavid S. Miller 	if (err)
392b42835dbSDavid S. Miller 		goto relookup_failed;
393b42835dbSDavid S. Miller 
3944c9483b2SDavid S. Miller 	dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
395452edd59SDavid S. Miller 	if (!IS_ERR(dst2)) {
396b42835dbSDavid S. Miller 		dst_release(dst);
397b42835dbSDavid S. Miller 		dst = dst2;
398452edd59SDavid S. Miller 	} else {
399452edd59SDavid S. Miller 		err = PTR_ERR(dst2);
400452edd59SDavid S. Miller 		if (err == -EPERM) {
401b42835dbSDavid S. Miller 			dst_release(dst);
402452edd59SDavid S. Miller 			return dst2;
403452edd59SDavid S. Miller 		} else
404b42835dbSDavid S. Miller 			goto relookup_failed;
405b42835dbSDavid S. Miller 	}
406b42835dbSDavid S. Miller 
407b42835dbSDavid S. Miller relookup_failed:
408b42835dbSDavid S. Miller 	if (dst)
409b42835dbSDavid S. Miller 		return dst;
410b42835dbSDavid S. Miller 	return ERR_PTR(err);
411b42835dbSDavid S. Miller }
412b42835dbSDavid S. Miller 
413e1ae5c2eSStephen Suryaputra static struct net_device *icmp6_dev(const struct sk_buff *skb)
4141b70d792SDavid Ahern {
415e1ae5c2eSStephen Suryaputra 	struct net_device *dev = skb->dev;
4161b70d792SDavid Ahern 
4171b70d792SDavid Ahern 	/* for local traffic to local address, skb dev is the loopback
4181b70d792SDavid Ahern 	 * device. Check if there is a dst attached to the skb and if so
41924b711edSDavid Ahern 	 * get the real device index. Same is needed for replies to a link
42024b711edSDavid Ahern 	 * local address on a device enslaved to an L3 master device
4211b70d792SDavid Ahern 	 */
422e1ae5c2eSStephen Suryaputra 	if (unlikely(dev->ifindex == LOOPBACK_IFINDEX || netif_is_l3_master(skb->dev))) {
4231b70d792SDavid Ahern 		const struct rt6_info *rt6 = skb_rt6_info(skb);
4241b70d792SDavid Ahern 
4251b70d792SDavid Ahern 		if (rt6)
426e1ae5c2eSStephen Suryaputra 			dev = rt6->rt6i_idev->dev;
4271b70d792SDavid Ahern 	}
4281b70d792SDavid Ahern 
429e1ae5c2eSStephen Suryaputra 	return dev;
430e1ae5c2eSStephen Suryaputra }
431e1ae5c2eSStephen Suryaputra 
432e1ae5c2eSStephen Suryaputra static int icmp6_iif(const struct sk_buff *skb)
433e1ae5c2eSStephen Suryaputra {
434e1ae5c2eSStephen Suryaputra 	return icmp6_dev(skb)->ifindex;
4351b70d792SDavid Ahern }
4361b70d792SDavid Ahern 
4371da177e4SLinus Torvalds /*
4381da177e4SLinus Torvalds  *	Send an ICMP message in response to a packet in error
4391da177e4SLinus Torvalds  */
440cc7a21b6SEric Dumazet void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
441ee576c47SJason A. Donenfeld 		const struct in6_addr *force_saddr,
442ee576c47SJason A. Donenfeld 		const struct inet6_skb_parm *parm)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
4450660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
44684427d53SYOSHIFUJI Hideaki 	struct sock *sk;
4478d933670SEric Dumazet 	struct net *net;
44884427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
449b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
4501da177e4SLinus Torvalds 	struct dst_entry *dst;
4511da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
4524c9483b2SDavid S. Miller 	struct flowi6 fl6;
4531da177e4SLinus Torvalds 	struct icmpv6_msg msg;
45426879da5SWei Wang 	struct ipcm6_cookie ipc6;
4551da177e4SLinus Torvalds 	int iif = 0;
4561da177e4SLinus Torvalds 	int addr_type = 0;
4571da177e4SLinus Torvalds 	int len;
4588d933670SEric Dumazet 	u32 mark;
4591da177e4SLinus Torvalds 
46027a884dcSArnaldo Carvalho de Melo 	if ((u8 *)hdr < skb->head ||
46129a3cad5SSimon Horman 	    (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
4621da177e4SLinus Torvalds 		return;
4631da177e4SLinus Torvalds 
4648d933670SEric Dumazet 	if (!skb->dev)
4658d933670SEric Dumazet 		return;
4668d933670SEric Dumazet 	net = dev_net(skb->dev);
4678d933670SEric Dumazet 	mark = IP6_REPLY_MARK(net, skb->mark);
4681da177e4SLinus Torvalds 	/*
4691da177e4SLinus Torvalds 	 *	Make sure we respect the rules
4701da177e4SLinus Torvalds 	 *	i.e. RFC 1885 2.4(e)
4715f5624cfSPravin B Shelar 	 *	Rule (e.1) is enforced by not using icmp6_send
4721da177e4SLinus Torvalds 	 *	in any code that processes icmp errors.
4731da177e4SLinus Torvalds 	 */
4741da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->daddr);
4751da177e4SLinus Torvalds 
476446fab59SFX Le Bail 	if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) ||
477d94c1f92SFX Le Bail 	    ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr))
4781da177e4SLinus Torvalds 		saddr = &hdr->daddr;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	/*
4811da177e4SLinus Torvalds 	 *	Dest addr check
4821da177e4SLinus Torvalds 	 */
4831da177e4SLinus Torvalds 
4849a6b4b39Szhuyj 	if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) {
4851da177e4SLinus Torvalds 		if (type != ICMPV6_PKT_TOOBIG &&
4861da177e4SLinus Torvalds 		    !(type == ICMPV6_PARAMPROB &&
4871da177e4SLinus Torvalds 		      code == ICMPV6_UNK_OPTION &&
4881da177e4SLinus Torvalds 		      (opt_unrec(skb, info))))
4891da177e4SLinus Torvalds 			return;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 		saddr = NULL;
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->saddr);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	/*
4971da177e4SLinus Torvalds 	 *	Source addr check
4981da177e4SLinus Torvalds 	 */
4991da177e4SLinus Torvalds 
5004832c30dSDavid Ahern 	if (__ipv6_addr_needs_scope_id(addr_type)) {
5011b70d792SDavid Ahern 		iif = icmp6_iif(skb);
5024832c30dSDavid Ahern 	} else {
503272928d1SMathieu Desnoyers 		/*
504272928d1SMathieu Desnoyers 		 * The source device is used for looking up which routing table
505272928d1SMathieu Desnoyers 		 * to use for sending an ICMP error.
506272928d1SMathieu Desnoyers 		 */
507272928d1SMathieu Desnoyers 		iif = l3mdev_master_ifindex(skb->dev);
50879dc7e3fSDavid Ahern 	}
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	/*
5118de3351eSYOSHIFUJI Hideaki 	 *	Must not send error if the source does not uniquely
5128de3351eSYOSHIFUJI Hideaki 	 *	identify a single node (RFC2463 Section 2.4).
5138de3351eSYOSHIFUJI Hideaki 	 *	We check unspecified / multicast addresses here,
5148de3351eSYOSHIFUJI Hideaki 	 *	and anycast addresses will be checked later.
5151da177e4SLinus Torvalds 	 */
5161da177e4SLinus Torvalds 	if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
5174b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n",
5184b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
5191da177e4SLinus Torvalds 		return;
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	/*
5231da177e4SLinus Torvalds 	 *	Never answer to a ICMP packet.
5241da177e4SLinus Torvalds 	 */
5251da177e4SLinus Torvalds 	if (is_ineligible(skb)) {
5264b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n",
5274b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
5281da177e4SLinus Torvalds 		return;
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 
5317ba91ecbSJesper Dangaard Brouer 	/* Needed by both icmp_global_allow and icmpv6_xmit_lock */
5327ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
5337ba91ecbSJesper Dangaard Brouer 
5347ba91ecbSJesper Dangaard Brouer 	/* Check global sysctl_icmp_msgs_per_sec ratelimit */
5350bc19985SStephen Suryaputra 	if (!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, type))
5367ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
5377ba91ecbSJesper Dangaard Brouer 
538ee576c47SJason A. Donenfeld 	mip6_addr_swap(skb, parm);
53979383236SMasahide NAKAMURA 
540fac6fce9SFrancesco Ruggeri 	sk = icmpv6_xmit_lock(net);
541fac6fce9SFrancesco Ruggeri 	if (!sk)
542fac6fce9SFrancesco Ruggeri 		goto out_bh_enable;
543fac6fce9SFrancesco Ruggeri 
5444c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
5454c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
5464e3fd7a0SAlexey Dobriyan 	fl6.daddr = hdr->saddr;
547b1cadc1aSEric Dumazet 	if (force_saddr)
548b1cadc1aSEric Dumazet 		saddr = force_saddr;
549fac6fce9SFrancesco Ruggeri 	if (saddr) {
5504e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
551b93cfb9cSTim Stallard 	} else if (!icmpv6_rt_has_prefsrc(sk, type, &fl6)) {
552fac6fce9SFrancesco Ruggeri 		/* select a more meaningful saddr from input if */
553fac6fce9SFrancesco Ruggeri 		struct net_device *in_netdev;
554fac6fce9SFrancesco Ruggeri 
555ee576c47SJason A. Donenfeld 		in_netdev = dev_get_by_index(net, parm->iif);
556fac6fce9SFrancesco Ruggeri 		if (in_netdev) {
557fac6fce9SFrancesco Ruggeri 			ipv6_dev_get_saddr(net, in_netdev, &fl6.daddr,
558fac6fce9SFrancesco Ruggeri 					   inet6_sk(sk)->srcprefs,
559fac6fce9SFrancesco Ruggeri 					   &fl6.saddr);
560fac6fce9SFrancesco Ruggeri 			dev_put(in_netdev);
561fac6fce9SFrancesco Ruggeri 		}
562fac6fce9SFrancesco Ruggeri 	}
563e110861fSLorenzo Colitti 	fl6.flowi6_mark = mark;
5644c9483b2SDavid S. Miller 	fl6.flowi6_oif = iif;
5651958b856SDavid S. Miller 	fl6.fl6_icmp_type = type;
5661958b856SDavid S. Miller 	fl6.fl6_icmp_code = code;
567e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
568b4bac172SDavid Ahern 	fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, NULL);
5693df98d79SPaul Moore 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
5701da177e4SLinus Torvalds 
571fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
572405666dbSDenis V. Lunev 
5734c9483b2SDavid S. Miller 	if (!icmpv6_xrlim_allow(sk, type, &fl6))
5741da177e4SLinus Torvalds 		goto out;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = type;
5771da177e4SLinus Torvalds 	tmp_hdr.icmp6_code = code;
5781da177e4SLinus Torvalds 	tmp_hdr.icmp6_cksum = 0;
5791da177e4SLinus Torvalds 	tmp_hdr.icmp6_pointer = htonl(info);
5801da177e4SLinus Torvalds 
5814c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
5824c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
583c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
584c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
5851da177e4SLinus Torvalds 
586b515430aSWillem de Bruijn 	ipcm6_init_sk(&ipc6, np);
5870da7536fSWillem de Bruijn 	ipc6.sockc.mark = mark;
58838b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
58938b7097bSHannes Frederic Sowa 
5904c9483b2SDavid S. Miller 	dst = icmpv6_route_lookup(net, skb, sk, &fl6);
591b42835dbSDavid S. Miller 	if (IS_ERR(dst))
5921da177e4SLinus Torvalds 		goto out;
5938de3351eSYOSHIFUJI Hideaki 
59426879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	msg.skb = skb;
597bbe735e4SArnaldo Carvalho de Melo 	msg.offset = skb_network_offset(skb);
598763ecff1SYasuyuki Kozakai 	msg.type = type;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	len = skb->len - msg.offset;
6011da177e4SLinus Torvalds 	len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr));
6021da177e4SLinus Torvalds 	if (len < 0) {
6034b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n",
6044b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
6051da177e4SLinus Torvalds 		goto out_dst_release;
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds 
608cfdf7647SEric Dumazet 	rcu_read_lock();
609cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
6101da177e4SLinus Torvalds 
6114e64b1edSJoe Perches 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
6121da177e4SLinus Torvalds 			    len + sizeof(struct icmp6hdr),
61326879da5SWei Wang 			    sizeof(struct icmp6hdr),
61426879da5SWei Wang 			    &ipc6, &fl6, (struct rt6_info *)dst,
6155fdaa88dSWillem de Bruijn 			    MSG_DONTWAIT)) {
61643a43b60SHannes Frederic Sowa 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
6171da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
618cfdf7647SEric Dumazet 	} else {
6194e64b1edSJoe Perches 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
620cfdf7647SEric Dumazet 					   len + sizeof(struct icmp6hdr));
6211da177e4SLinus Torvalds 	}
622cfdf7647SEric Dumazet 	rcu_read_unlock();
6231da177e4SLinus Torvalds out_dst_release:
6241da177e4SLinus Torvalds 	dst_release(dst);
6251da177e4SLinus Torvalds out:
626405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
6277ba91ecbSJesper Dangaard Brouer out_bh_enable:
6287ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
6291da177e4SLinus Torvalds }
630cc7a21b6SEric Dumazet EXPORT_SYMBOL(icmp6_send);
6315f5624cfSPravin B Shelar 
6321ad6d548SMenglong Dong /* Slightly more convenient version of icmp6_send with drop reasons.
6335f5624cfSPravin B Shelar  */
6341ad6d548SMenglong Dong void icmpv6_param_prob_reason(struct sk_buff *skb, u8 code, int pos,
6351ad6d548SMenglong Dong 			      enum skb_drop_reason reason)
6365f5624cfSPravin B Shelar {
637ee576c47SJason A. Donenfeld 	icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL, IP6CB(skb));
6381ad6d548SMenglong Dong 	kfree_skb_reason(skb, reason);
6395f5624cfSPravin B Shelar }
6407159039aSYOSHIFUJI Hideaki 
6415fbba8acSEric Dumazet /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
6425fbba8acSEric Dumazet  * if sufficient data bytes are available
6435fbba8acSEric Dumazet  * @nhs is the size of the tunnel header(s) :
6445fbba8acSEric Dumazet  *  Either an IPv4 header for SIT encap
6455fbba8acSEric Dumazet  *         an IPv4 header + GRE header for GRE encap
6465fbba8acSEric Dumazet  */
64720e1954fSEric Dumazet int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type,
64820e1954fSEric Dumazet 			       unsigned int data_len)
6495fbba8acSEric Dumazet {
6502d7a3b27SEric Dumazet 	struct in6_addr temp_saddr;
6515fbba8acSEric Dumazet 	struct rt6_info *rt;
6525fbba8acSEric Dumazet 	struct sk_buff *skb2;
65320e1954fSEric Dumazet 	u32 info = 0;
6545fbba8acSEric Dumazet 
6555fbba8acSEric Dumazet 	if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8))
6565fbba8acSEric Dumazet 		return 1;
6575fbba8acSEric Dumazet 
65820e1954fSEric Dumazet 	/* RFC 4884 (partial) support for ICMP extensions */
65920e1954fSEric Dumazet 	if (data_len < 128 || (data_len & 7) || skb->len < data_len)
66020e1954fSEric Dumazet 		data_len = 0;
66120e1954fSEric Dumazet 
66220e1954fSEric Dumazet 	skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC);
6635fbba8acSEric Dumazet 
6645fbba8acSEric Dumazet 	if (!skb2)
6655fbba8acSEric Dumazet 		return 1;
6665fbba8acSEric Dumazet 
6675fbba8acSEric Dumazet 	skb_dst_drop(skb2);
6685fbba8acSEric Dumazet 	skb_pull(skb2, nhs);
6695fbba8acSEric Dumazet 	skb_reset_network_header(skb2);
6705fbba8acSEric Dumazet 
671b75cc8f9SDavid Ahern 	rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0,
672b75cc8f9SDavid Ahern 			skb, 0);
6735fbba8acSEric Dumazet 
6745fbba8acSEric Dumazet 	if (rt && rt->dst.dev)
6755fbba8acSEric Dumazet 		skb2->dev = rt->dst.dev;
6765fbba8acSEric Dumazet 
6772d7a3b27SEric Dumazet 	ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr);
67820e1954fSEric Dumazet 
67920e1954fSEric Dumazet 	if (data_len) {
68020e1954fSEric Dumazet 		/* RFC 4884 (partial) support :
68120e1954fSEric Dumazet 		 * insert 0 padding at the end, before the extensions
68220e1954fSEric Dumazet 		 */
68320e1954fSEric Dumazet 		__skb_push(skb2, nhs);
68420e1954fSEric Dumazet 		skb_reset_network_header(skb2);
68520e1954fSEric Dumazet 		memmove(skb2->data, skb2->data + nhs, data_len - nhs);
68620e1954fSEric Dumazet 		memset(skb2->data + data_len - nhs, 0, nhs);
68720e1954fSEric Dumazet 		/* RFC 4884 4.5 : Length is measured in 64-bit words,
68820e1954fSEric Dumazet 		 * and stored in reserved[0]
68920e1954fSEric Dumazet 		 */
69020e1954fSEric Dumazet 		info = (data_len/8) << 24;
69120e1954fSEric Dumazet 	}
6922d7a3b27SEric Dumazet 	if (type == ICMP_TIME_EXCEEDED)
6932d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
694ee576c47SJason A. Donenfeld 			   info, &temp_saddr, IP6CB(skb2));
6952d7a3b27SEric Dumazet 	else
6962d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH,
697ee576c47SJason A. Donenfeld 			   info, &temp_saddr, IP6CB(skb2));
6985fbba8acSEric Dumazet 	if (rt)
6995fbba8acSEric Dumazet 		ip6_rt_put(rt);
7005fbba8acSEric Dumazet 
7015fbba8acSEric Dumazet 	kfree_skb(skb2);
7025fbba8acSEric Dumazet 
7035fbba8acSEric Dumazet 	return 0;
7045fbba8acSEric Dumazet }
7055fbba8acSEric Dumazet EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach);
7065fbba8acSEric Dumazet 
7071da177e4SLinus Torvalds static void icmpv6_echo_reply(struct sk_buff *skb)
7081da177e4SLinus Torvalds {
709c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
71084427d53SYOSHIFUJI Hideaki 	struct sock *sk;
7111da177e4SLinus Torvalds 	struct inet6_dev *idev;
71284427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
713b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
714cc70ab26SArnaldo Carvalho de Melo 	struct icmp6hdr *icmph = icmp6_hdr(skb);
7151da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
7164c9483b2SDavid S. Miller 	struct flowi6 fl6;
7171da177e4SLinus Torvalds 	struct icmpv6_msg msg;
7181da177e4SLinus Torvalds 	struct dst_entry *dst;
71926879da5SWei Wang 	struct ipcm6_cookie ipc6;
720e110861fSLorenzo Colitti 	u32 mark = IP6_REPLY_MARK(net, skb->mark);
7210b03a5caSStephen Suryaputra 	bool acast;
7221fd07f33SAndreas Roeseler 	u8 type;
7231da177e4SLinus Torvalds 
72403f1ecccSStephen Suryaputra 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
72503f1ecccSStephen Suryaputra 	    net->ipv6.sysctl.icmpv6_echo_ignore_multicast)
72603f1ecccSStephen Suryaputra 		return;
72703f1ecccSStephen Suryaputra 
7280660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->daddr;
7291da177e4SLinus Torvalds 
7300b03a5caSStephen Suryaputra 	acast = ipv6_anycast_destination(skb_dst(skb), saddr);
7310b03a5caSStephen Suryaputra 	if (acast && net->ipv6.sysctl.icmpv6_echo_ignore_anycast)
7320b03a5caSStephen Suryaputra 		return;
7330b03a5caSStephen Suryaputra 
734509aba3bSFX Le Bail 	if (!ipv6_unicast_destination(skb) &&
7350b03a5caSStephen Suryaputra 	    !(net->ipv6.sysctl.anycast_src_echo_reply && acast))
7361da177e4SLinus Torvalds 		saddr = NULL;
7371da177e4SLinus Torvalds 
7381fd07f33SAndreas Roeseler 	if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
7391fd07f33SAndreas Roeseler 		type = ICMPV6_EXT_ECHO_REPLY;
7401fd07f33SAndreas Roeseler 	else
7411fd07f33SAndreas Roeseler 		type = ICMPV6_ECHO_REPLY;
7421fd07f33SAndreas Roeseler 
7431da177e4SLinus Torvalds 	memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
7441fd07f33SAndreas Roeseler 	tmp_hdr.icmp6_type = type;
7451da177e4SLinus Torvalds 
7464c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
747a346abe0SEric Dumazet 	if (net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES)
748a346abe0SEric Dumazet 		fl6.flowlabel = ip6_flowlabel(ipv6_hdr(skb));
749a346abe0SEric Dumazet 
7504c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
7514e3fd7a0SAlexey Dobriyan 	fl6.daddr = ipv6_hdr(skb)->saddr;
7521da177e4SLinus Torvalds 	if (saddr)
7534e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
7541b70d792SDavid Ahern 	fl6.flowi6_oif = icmp6_iif(skb);
7551fd07f33SAndreas Roeseler 	fl6.fl6_icmp_type = type;
756e110861fSLorenzo Colitti 	fl6.flowi6_mark = mark;
757e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
7583df98d79SPaul Moore 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
7591da177e4SLinus Torvalds 
7607ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
761fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
76263159f29SIan Morris 	if (!sk)
7637ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
764fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
765405666dbSDenis V. Lunev 
7664c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
7674c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
768c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
769c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
7701da177e4SLinus Torvalds 
7714e64b1edSJoe Perches 	if (ip6_dst_lookup(net, sk, &dst, &fl6))
7721da177e4SLinus Torvalds 		goto out;
7734c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
774452edd59SDavid S. Miller 	if (IS_ERR(dst))
775e104411bSPatrick McHardy 		goto out;
7761da177e4SLinus Torvalds 
7770bc19985SStephen Suryaputra 	/* Check the ratelimit */
7780bc19985SStephen Suryaputra 	if ((!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, ICMPV6_ECHO_REPLY)) ||
7790bc19985SStephen Suryaputra 	    !icmpv6_xrlim_allow(sk, ICMPV6_ECHO_REPLY, &fl6))
7800bc19985SStephen Suryaputra 		goto out_dst_release;
7810bc19985SStephen Suryaputra 
782cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	msg.skb = skb;
7851da177e4SLinus Torvalds 	msg.offset = 0;
7861fd07f33SAndreas Roeseler 	msg.type = type;
7871da177e4SLinus Torvalds 
788b515430aSWillem de Bruijn 	ipcm6_init_sk(&ipc6, np);
78926879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
79026879da5SWei Wang 	ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb));
7910da7536fSWillem de Bruijn 	ipc6.sockc.mark = mark;
79226879da5SWei Wang 
7931fd07f33SAndreas Roeseler 	if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
7941fd07f33SAndreas Roeseler 		if (!icmp_build_probe(skb, (struct icmphdr *)&tmp_hdr))
7951fd07f33SAndreas Roeseler 			goto out_dst_release;
7961fd07f33SAndreas Roeseler 
7974e64b1edSJoe Perches 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
7984e64b1edSJoe Perches 			    skb->len + sizeof(struct icmp6hdr),
79926879da5SWei Wang 			    sizeof(struct icmp6hdr), &ipc6, &fl6,
8005fdaa88dSWillem de Bruijn 			    (struct rt6_info *)dst, MSG_DONTWAIT)) {
801a16292a0SEric Dumazet 		__ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
8021da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
803cfdf7647SEric Dumazet 	} else {
8044e64b1edSJoe Perches 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
805cfdf7647SEric Dumazet 					   skb->len + sizeof(struct icmp6hdr));
8061da177e4SLinus Torvalds 	}
8070bc19985SStephen Suryaputra out_dst_release:
8081da177e4SLinus Torvalds 	dst_release(dst);
8091da177e4SLinus Torvalds out:
810405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
8117ba91ecbSJesper Dangaard Brouer out_bh_enable:
8127ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
815b94f1c09SDavid S. Miller void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
8161da177e4SLinus Torvalds {
817e4129440SAndrew Lunn 	struct inet6_skb_parm *opt = IP6CB(skb);
81841135cc8SAlexey Dobriyan 	const struct inet6_protocol *ipprot;
8191da177e4SLinus Torvalds 	int inner_offset;
82075f2811cSJesse Gross 	__be16 frag_off;
821f9242b6bSDavid S. Miller 	u8 nexthdr;
8227304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
8257304fe46SDuan Jiong 		goto out;
8261da177e4SLinus Torvalds 
827e4129440SAndrew Lunn 	seg6_icmp_srh(skb, opt);
828e4129440SAndrew Lunn 
8291da177e4SLinus Torvalds 	nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
8301da177e4SLinus Torvalds 	if (ipv6_ext_hdr(nexthdr)) {
8311da177e4SLinus Torvalds 		/* now skip over extension headers */
83275f2811cSJesse Gross 		inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
83375f2811cSJesse Gross 						&nexthdr, &frag_off);
8341da177e4SLinus Torvalds 		if (inner_offset < 0)
8357304fe46SDuan Jiong 			goto out;
8361da177e4SLinus Torvalds 	} else {
8371da177e4SLinus Torvalds 		inner_offset = sizeof(struct ipv6hdr);
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	/* Checkin header including 8 bytes of inner protocol header. */
8411da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, inner_offset+8))
8427304fe46SDuan Jiong 		goto out;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
8451da177e4SLinus Torvalds 	   Without this we will not able f.e. to make source routed
8461da177e4SLinus Torvalds 	   pmtu discovery.
8471da177e4SLinus Torvalds 	   Corresponding argument (opt) to notifiers is already added.
8481da177e4SLinus Torvalds 	   --ANK (980726)
8491da177e4SLinus Torvalds 	 */
8501da177e4SLinus Torvalds 
851f9242b6bSDavid S. Miller 	ipprot = rcu_dereference(inet6_protos[nexthdr]);
8521da177e4SLinus Torvalds 	if (ipprot && ipprot->err_handler)
853e4129440SAndrew Lunn 		ipprot->err_handler(skb, opt, type, code, inner_offset, info);
8541da177e4SLinus Torvalds 
85569d6da0bSPavel Emelyanov 	raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
8567304fe46SDuan Jiong 	return;
8577304fe46SDuan Jiong 
8587304fe46SDuan Jiong out:
859a16292a0SEric Dumazet 	__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds /*
8631da177e4SLinus Torvalds  *	Handle icmp messages
8641da177e4SLinus Torvalds  */
8651da177e4SLinus Torvalds 
866e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb)
8671da177e4SLinus Torvalds {
868b384c95aSMenglong Dong 	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
869e6f86b0fSVirgile Jarry 	struct net *net = dev_net(skb->dev);
870e1ae5c2eSStephen Suryaputra 	struct net_device *dev = icmp6_dev(skb);
8711da177e4SLinus Torvalds 	struct inet6_dev *idev = __in6_dev_get(dev);
872b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
8731da177e4SLinus Torvalds 	struct icmp6hdr *hdr;
874d5fdd6baSBrian Haley 	u8 type;
8751da177e4SLinus Torvalds 
876aebcf82cSHerbert Xu 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
877def8b4faSAlexey Dobriyan 		struct sec_path *sp = skb_sec_path(skb);
8788b7817f3SHerbert Xu 		int nh;
8798b7817f3SHerbert Xu 
880def8b4faSAlexey Dobriyan 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
881b384c95aSMenglong Dong 				 XFRM_STATE_ICMP)) {
882b384c95aSMenglong Dong 			reason = SKB_DROP_REASON_XFRM_POLICY;
883aebcf82cSHerbert Xu 			goto drop_no_count;
884b384c95aSMenglong Dong 		}
885aebcf82cSHerbert Xu 
88681aded24SDavid S. Miller 		if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
8878b7817f3SHerbert Xu 			goto drop_no_count;
8888b7817f3SHerbert Xu 
8898b7817f3SHerbert Xu 		nh = skb_network_offset(skb);
8908b7817f3SHerbert Xu 		skb_set_network_header(skb, sizeof(*hdr));
8918b7817f3SHerbert Xu 
892b384c95aSMenglong Dong 		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN,
893b384c95aSMenglong Dong 						skb)) {
894b384c95aSMenglong Dong 			reason = SKB_DROP_REASON_XFRM_POLICY;
8958b7817f3SHerbert Xu 			goto drop_no_count;
896b384c95aSMenglong Dong 		}
8978b7817f3SHerbert Xu 
8988b7817f3SHerbert Xu 		skb_set_network_header(skb, nh);
8998b7817f3SHerbert Xu 	}
9008b7817f3SHerbert Xu 
901a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INMSGS);
9021da177e4SLinus Torvalds 
9030660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
9040660e03fSArnaldo Carvalho de Melo 	daddr = &ipv6_hdr(skb)->daddr;
9051da177e4SLinus Torvalds 
90639471ac8STom Herbert 	if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) {
907ba7a46f1SJoe Perches 		net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n",
9080c6ce78aSHarvey Harrison 				    saddr, daddr);
9096a5dc9e5SEric Dumazet 		goto csum_error;
9101da177e4SLinus Torvalds 	}
9111da177e4SLinus Torvalds 
9128cf22943SHerbert Xu 	if (!pskb_pull(skb, sizeof(*hdr)))
9138cf22943SHerbert Xu 		goto discard_it;
9141da177e4SLinus Torvalds 
915cc70ab26SArnaldo Carvalho de Melo 	hdr = icmp6_hdr(skb);
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	type = hdr->icmp6_type;
9181da177e4SLinus Torvalds 
919f3832ed2SEric Dumazet 	ICMP6MSGIN_INC_STATS(dev_net(dev), idev, type);
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	switch (type) {
9221da177e4SLinus Torvalds 	case ICMPV6_ECHO_REQUEST:
923e6f86b0fSVirgile Jarry 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all)
9241da177e4SLinus Torvalds 			icmpv6_echo_reply(skb);
9251da177e4SLinus Torvalds 		break;
9261fd07f33SAndreas Roeseler 	case ICMPV6_EXT_ECHO_REQUEST:
9271fd07f33SAndreas Roeseler 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all &&
928*4a2f7083SKuniyuki Iwashima 		    READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe))
9291fd07f33SAndreas Roeseler 			icmpv6_echo_reply(skb);
9301fd07f33SAndreas Roeseler 		break;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	case ICMPV6_ECHO_REPLY:
933b384c95aSMenglong Dong 		reason = ping_rcv(skb);
9341da177e4SLinus Torvalds 		break;
9351da177e4SLinus Torvalds 
93631433202SAndreas Roeseler 	case ICMPV6_EXT_ECHO_REPLY:
937b384c95aSMenglong Dong 		reason = ping_rcv(skb);
93831433202SAndreas Roeseler 		break;
93931433202SAndreas Roeseler 
9401da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
9411da177e4SLinus Torvalds 		/* BUGGG_FUTURE: if packet contains rthdr, we cannot update
9421da177e4SLinus Torvalds 		   standard destination cache. Seems, only "advanced"
9431da177e4SLinus Torvalds 		   destination cache will allow to solve this problem
9441da177e4SLinus Torvalds 		   --ANK (980726)
9451da177e4SLinus Torvalds 		 */
9461da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
9471da177e4SLinus Torvalds 			goto discard_it;
948cc70ab26SArnaldo Carvalho de Melo 		hdr = icmp6_hdr(skb);
9491da177e4SLinus Torvalds 
950275757e6SGustavo A. R. Silva 		/* to notify */
951a8eceea8SJoe Perches 		fallthrough;
9521da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
9531da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
9541da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
9551da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
9561da177e4SLinus Torvalds 		break;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	case NDISC_ROUTER_SOLICITATION:
9591da177e4SLinus Torvalds 	case NDISC_ROUTER_ADVERTISEMENT:
9601da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_SOLICITATION:
9611da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_ADVERTISEMENT:
9621da177e4SLinus Torvalds 	case NDISC_REDIRECT:
9631da177e4SLinus Torvalds 		ndisc_rcv(skb);
9641da177e4SLinus Torvalds 		break;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	case ICMPV6_MGM_QUERY:
9671da177e4SLinus Torvalds 		igmp6_event_query(skb);
968f185de28STaehee Yoo 		return 0;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	case ICMPV6_MGM_REPORT:
9711da177e4SLinus Torvalds 		igmp6_event_report(skb);
972f185de28STaehee Yoo 		return 0;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	case ICMPV6_MGM_REDUCTION:
9751da177e4SLinus Torvalds 	case ICMPV6_NI_QUERY:
9761da177e4SLinus Torvalds 	case ICMPV6_NI_REPLY:
9771da177e4SLinus Torvalds 	case ICMPV6_MLD2_REPORT:
9781da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REQUEST:
9791da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REPLY:
9801da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_SOL:
9811da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_ADV:
9821da177e4SLinus Torvalds 		break;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	default:
9851da177e4SLinus Torvalds 		/* informational */
9861da177e4SLinus Torvalds 		if (type & ICMPV6_INFOMSG_MASK)
9871da177e4SLinus Torvalds 			break;
9881da177e4SLinus Torvalds 
9894b3418fbSBjørn Mork 		net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n",
9904b3418fbSBjørn Mork 				    saddr, daddr);
991ea85a0a2SDavid S. Miller 
9921da177e4SLinus Torvalds 		/*
9931da177e4SLinus Torvalds 		 * error of unknown type.
9941da177e4SLinus Torvalds 		 * must pass to upper level
9951da177e4SLinus Torvalds 		 */
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
9983ff50b79SStephen Hemminger 	}
9993ff50b79SStephen Hemminger 
1000e3e32170SRick Jones 	/* until the v6 path can be better sorted assume failure and
1001e3e32170SRick Jones 	 * preserve the status quo behaviour for the rest of the paths to here
1002e3e32170SRick Jones 	 */
1003b384c95aSMenglong Dong 	if (reason)
1004b384c95aSMenglong Dong 		kfree_skb_reason(skb, reason);
1005e3e32170SRick Jones 	else
1006b384c95aSMenglong Dong 		consume_skb(skb);
1007e3e32170SRick Jones 
10081da177e4SLinus Torvalds 	return 0;
10091da177e4SLinus Torvalds 
10106a5dc9e5SEric Dumazet csum_error:
1011b384c95aSMenglong Dong 	reason = SKB_DROP_REASON_ICMP_CSUM;
1012a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS);
10131da177e4SLinus Torvalds discard_it:
1014a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INERRORS);
10158b7817f3SHerbert Xu drop_no_count:
1016b384c95aSMenglong Dong 	kfree_skb_reason(skb, reason);
10171da177e4SLinus Torvalds 	return 0;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10204c9483b2SDavid S. Miller void icmpv6_flow_init(struct sock *sk, struct flowi6 *fl6,
102195e41e93SYOSHIFUJI Hideaki 		      u8 type,
102295e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *saddr,
102395e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *daddr,
102495e41e93SYOSHIFUJI Hideaki 		      int oif)
102595e41e93SYOSHIFUJI Hideaki {
10264c9483b2SDavid S. Miller 	memset(fl6, 0, sizeof(*fl6));
10274e3fd7a0SAlexey Dobriyan 	fl6->saddr = *saddr;
10284e3fd7a0SAlexey Dobriyan 	fl6->daddr = *daddr;
10294c9483b2SDavid S. Miller 	fl6->flowi6_proto	= IPPROTO_ICMPV6;
10301958b856SDavid S. Miller 	fl6->fl6_icmp_type	= type;
10311958b856SDavid S. Miller 	fl6->fl6_icmp_code	= 0;
10324c9483b2SDavid S. Miller 	fl6->flowi6_oif		= oif;
10333df98d79SPaul Moore 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
103495e41e93SYOSHIFUJI Hideaki }
103595e41e93SYOSHIFUJI Hideaki 
10366a17b961SEric Dumazet int __init icmpv6_init(void)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	struct sock *sk;
10393232a1efSKefeng Wang 	int err, i;
10401da177e4SLinus Torvalds 
10416f912042SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
10421ed8516fSDenis V. Lunev 		err = inet_ctl_sock_create(&sk, PF_INET6,
10436a17b961SEric Dumazet 					   SOCK_RAW, IPPROTO_ICMPV6, &init_net);
10441da177e4SLinus Torvalds 		if (err < 0) {
1045f3213831SJoe Perches 			pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
10461da177e4SLinus Torvalds 			       err);
10476a17b961SEric Dumazet 			return err;
10481da177e4SLinus Torvalds 		}
10491da177e4SLinus Torvalds 
10506a17b961SEric Dumazet 		per_cpu(ipv6_icmp_sk, i) = sk;
10515c8cafd6SDenis V. Lunev 
10521da177e4SLinus Torvalds 		/* Enough space for 2 64K ICMP packets, including
10531da177e4SLinus Torvalds 		 * sk_buff struct overhead.
10541da177e4SLinus Torvalds 		 */
105587fb4b7bSEric Dumazet 		sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
10561da177e4SLinus Torvalds 	}
105798c6d1b2SDenis V. Lunev 
105898c6d1b2SDenis V. Lunev 	err = -EAGAIN;
105998c6d1b2SDenis V. Lunev 	if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
106098c6d1b2SDenis V. Lunev 		goto fail;
10615f5624cfSPravin B Shelar 
10625f5624cfSPravin B Shelar 	err = inet6_register_icmp_sender(icmp6_send);
10635f5624cfSPravin B Shelar 	if (err)
10645f5624cfSPravin B Shelar 		goto sender_reg_err;
106598c6d1b2SDenis V. Lunev 	return 0;
106698c6d1b2SDenis V. Lunev 
10675f5624cfSPravin B Shelar sender_reg_err:
10685f5624cfSPravin B Shelar 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
106998c6d1b2SDenis V. Lunev fail:
1070f3213831SJoe Perches 	pr_err("Failed to register ICMP6 protocol\n");
107198c6d1b2SDenis V. Lunev 	return err;
107298c6d1b2SDenis V. Lunev }
107398c6d1b2SDenis V. Lunev 
10748ed7edceSAlexey Dobriyan void icmpv6_cleanup(void)
107598c6d1b2SDenis V. Lunev {
10765f5624cfSPravin B Shelar 	inet6_unregister_icmp_sender(icmp6_send);
10771da177e4SLinus Torvalds 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
108098c6d1b2SDenis V. Lunev 
10819b5b5cffSArjan van de Ven static const struct icmp6_err {
10821da177e4SLinus Torvalds 	int err;
10831da177e4SLinus Torvalds 	int fatal;
10841da177e4SLinus Torvalds } tab_unreach[] = {
10851da177e4SLinus Torvalds 	{	/* NOROUTE */
10861da177e4SLinus Torvalds 		.err	= ENETUNREACH,
10871da177e4SLinus Torvalds 		.fatal	= 0,
10881da177e4SLinus Torvalds 	},
10891da177e4SLinus Torvalds 	{	/* ADM_PROHIBITED */
10901da177e4SLinus Torvalds 		.err	= EACCES,
10911da177e4SLinus Torvalds 		.fatal	= 1,
10921da177e4SLinus Torvalds 	},
10931da177e4SLinus Torvalds 	{	/* Was NOT_NEIGHBOUR, now reserved */
10941da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
10951da177e4SLinus Torvalds 		.fatal	= 0,
10961da177e4SLinus Torvalds 	},
10971da177e4SLinus Torvalds 	{	/* ADDR_UNREACH	*/
10981da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
10991da177e4SLinus Torvalds 		.fatal	= 0,
11001da177e4SLinus Torvalds 	},
11011da177e4SLinus Torvalds 	{	/* PORT_UNREACH	*/
11021da177e4SLinus Torvalds 		.err	= ECONNREFUSED,
11031da177e4SLinus Torvalds 		.fatal	= 1,
11041da177e4SLinus Torvalds 	},
110561e76b17SJiri Bohac 	{	/* POLICY_FAIL */
110661e76b17SJiri Bohac 		.err	= EACCES,
110761e76b17SJiri Bohac 		.fatal	= 1,
110861e76b17SJiri Bohac 	},
110961e76b17SJiri Bohac 	{	/* REJECT_ROUTE	*/
111061e76b17SJiri Bohac 		.err	= EACCES,
111161e76b17SJiri Bohac 		.fatal	= 1,
111261e76b17SJiri Bohac 	},
11131da177e4SLinus Torvalds };
11141da177e4SLinus Torvalds 
1115d5fdd6baSBrian Haley int icmpv6_err_convert(u8 type, u8 code, int *err)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	int fatal = 0;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	*err = EPROTO;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	switch (type) {
11221da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
11231da177e4SLinus Torvalds 		fatal = 1;
112461e76b17SJiri Bohac 		if (code < ARRAY_SIZE(tab_unreach)) {
11251da177e4SLinus Torvalds 			*err  = tab_unreach[code].err;
11261da177e4SLinus Torvalds 			fatal = tab_unreach[code].fatal;
11271da177e4SLinus Torvalds 		}
11281da177e4SLinus Torvalds 		break;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
11311da177e4SLinus Torvalds 		*err = EMSGSIZE;
11321da177e4SLinus Torvalds 		break;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
11351da177e4SLinus Torvalds 		*err = EPROTO;
11361da177e4SLinus Torvalds 		fatal = 1;
11371da177e4SLinus Torvalds 		break;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
11401da177e4SLinus Torvalds 		*err = EHOSTUNREACH;
11411da177e4SLinus Torvalds 		break;
11423ff50b79SStephen Hemminger 	}
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	return fatal;
11451da177e4SLinus Torvalds }
11467159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert);
11477159039aSYOSHIFUJI Hideaki 
11481da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1149e8243534Sstephen hemminger static struct ctl_table ipv6_icmp_table_template[] = {
11501da177e4SLinus Torvalds 	{
11511da177e4SLinus Torvalds 		.procname	= "ratelimit",
115241a76906SDaniel Lezcano 		.data		= &init_net.ipv6.sysctl.icmpv6_time,
11531da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
11541da177e4SLinus Torvalds 		.mode		= 0644,
11556d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
11561da177e4SLinus Torvalds 	},
1157e6f86b0fSVirgile Jarry 	{
1158e6f86b0fSVirgile Jarry 		.procname	= "echo_ignore_all",
1159e6f86b0fSVirgile Jarry 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_all,
1160a6175633SEric Dumazet 		.maxlen		= sizeof(u8),
1161e6f86b0fSVirgile Jarry 		.mode		= 0644,
1162a6175633SEric Dumazet 		.proc_handler = proc_dou8vec_minmax,
1163e6f86b0fSVirgile Jarry 	},
116403f1ecccSStephen Suryaputra 	{
116503f1ecccSStephen Suryaputra 		.procname	= "echo_ignore_multicast",
116603f1ecccSStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_multicast,
1167a6175633SEric Dumazet 		.maxlen		= sizeof(u8),
116803f1ecccSStephen Suryaputra 		.mode		= 0644,
1169a6175633SEric Dumazet 		.proc_handler = proc_dou8vec_minmax,
117003f1ecccSStephen Suryaputra 	},
11710b03a5caSStephen Suryaputra 	{
11720b03a5caSStephen Suryaputra 		.procname	= "echo_ignore_anycast",
11730b03a5caSStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_anycast,
1174a6175633SEric Dumazet 		.maxlen		= sizeof(u8),
11750b03a5caSStephen Suryaputra 		.mode		= 0644,
1176a6175633SEric Dumazet 		.proc_handler = proc_dou8vec_minmax,
11770b03a5caSStephen Suryaputra 	},
11780bc19985SStephen Suryaputra 	{
11790bc19985SStephen Suryaputra 		.procname	= "ratemask",
11800bc19985SStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_ratemask_ptr,
11810bc19985SStephen Suryaputra 		.maxlen		= ICMPV6_MSG_MAX + 1,
11820bc19985SStephen Suryaputra 		.mode		= 0644,
11830bc19985SStephen Suryaputra 		.proc_handler = proc_do_large_bitmap,
11840bc19985SStephen Suryaputra 	},
1185f8572d8fSEric W. Biederman 	{ },
11861da177e4SLinus Torvalds };
1187760f2d01SDaniel Lezcano 
11882c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
1189760f2d01SDaniel Lezcano {
1190760f2d01SDaniel Lezcano 	struct ctl_table *table;
1191760f2d01SDaniel Lezcano 
1192760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_icmp_table_template,
1193760f2d01SDaniel Lezcano 			sizeof(ipv6_icmp_table_template),
1194760f2d01SDaniel Lezcano 			GFP_KERNEL);
11955ee09105SYOSHIFUJI Hideaki 
1196e6f86b0fSVirgile Jarry 	if (table) {
11975ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.icmpv6_time;
1198e6f86b0fSVirgile Jarry 		table[1].data = &net->ipv6.sysctl.icmpv6_echo_ignore_all;
119903f1ecccSStephen Suryaputra 		table[2].data = &net->ipv6.sysctl.icmpv6_echo_ignore_multicast;
12000b03a5caSStephen Suryaputra 		table[3].data = &net->ipv6.sysctl.icmpv6_echo_ignore_anycast;
12010bc19985SStephen Suryaputra 		table[4].data = &net->ipv6.sysctl.icmpv6_ratemask_ptr;
1202e6f86b0fSVirgile Jarry 	}
1203760f2d01SDaniel Lezcano 	return table;
1204760f2d01SDaniel Lezcano }
12051da177e4SLinus Torvalds #endif
1206