xref: /openbmc/linux/net/ipv6/icmp.c (revision c899710f)
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 
icmpv6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)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 */
icmpv6_xmit_lock(struct net * net)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 
icmpv6_xmit_unlock(struct sock * sk)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 
is_ineligible(const struct sk_buff * skb)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 
icmpv6_mask_allow(struct net * net,int type)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 
icmpv6_global_allow(struct net * net,int type)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 
186d0941130SJamie Bainbridge 	__ICMP_INC_STATS(net, ICMP_MIB_RATELIMITGLOBAL);
187c0303efeSJesper Dangaard Brouer 	return false;
188c0303efeSJesper Dangaard Brouer }
189c0303efeSJesper Dangaard Brouer 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * Check the ICMP output rate limit
1921da177e4SLinus Torvalds  */
icmpv6_xrlim_allow(struct sock * sk,u8 type,struct flowi6 * fl6)1934cdf507dSEric Dumazet static bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
1944c9483b2SDavid S. Miller 			       struct flowi6 *fl6)
1951da177e4SLinus Torvalds {
1963b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
1974cdf507dSEric Dumazet 	struct dst_entry *dst;
19892d86829SDavid S. Miller 	bool res = false;
1991da177e4SLinus Torvalds 
2000bc19985SStephen Suryaputra 	if (icmpv6_mask_allow(net, type))
20192d86829SDavid S. Miller 		return true;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	/*
2041da177e4SLinus Torvalds 	 * Look up the output route.
2051da177e4SLinus Torvalds 	 * XXX: perhaps the expire for routing entries cloned by
2061da177e4SLinus Torvalds 	 * this lookup should be more aggressive (not longer than timeout).
2071da177e4SLinus Torvalds 	 */
2084c9483b2SDavid S. Miller 	dst = ip6_route_output(net, sk, fl6);
2091da177e4SLinus Torvalds 	if (dst->error) {
2103bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst),
211a11d206dSYOSHIFUJI Hideaki 			      IPSTATS_MIB_OUTNOROUTES);
2121da177e4SLinus Torvalds 	} else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
21392d86829SDavid S. Miller 		res = true;
2141da177e4SLinus Torvalds 	} else {
2151da177e4SLinus Torvalds 		struct rt6_info *rt = (struct rt6_info *)dst;
2169a43b709SBenjamin Thery 		int tmo = net->ipv6.sysctl.icmpv6_time;
217c0303efeSJesper Dangaard Brouer 		struct inet_peer *peer;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 		/* Give more bandwidth to wider prefixes. */
2201da177e4SLinus Torvalds 		if (rt->rt6i_dst.plen < 128)
2211da177e4SLinus Torvalds 			tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
2221da177e4SLinus Torvalds 
223c0303efeSJesper Dangaard Brouer 		peer = inet_getpeer_v6(net->ipv6.peers, &fl6->daddr, 1);
224fbfe95a4SDavid S. Miller 		res = inet_peer_xrlim_allow(peer, tmo);
2251d861aa4SDavid S. Miller 		if (peer)
2261d861aa4SDavid S. Miller 			inet_putpeer(peer);
2271da177e4SLinus Torvalds 	}
228d0941130SJamie Bainbridge 	if (!res)
229d0941130SJamie Bainbridge 		__ICMP6_INC_STATS(net, ip6_dst_idev(dst),
230d0941130SJamie Bainbridge 				  ICMP6_MIB_RATELIMITHOST);
2311da177e4SLinus Torvalds 	dst_release(dst);
2321da177e4SLinus Torvalds 	return res;
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
icmpv6_rt_has_prefsrc(struct sock * sk,u8 type,struct flowi6 * fl6)235b93cfb9cSTim Stallard static bool icmpv6_rt_has_prefsrc(struct sock *sk, u8 type,
236b93cfb9cSTim Stallard 				  struct flowi6 *fl6)
237b93cfb9cSTim Stallard {
238b93cfb9cSTim Stallard 	struct net *net = sock_net(sk);
239b93cfb9cSTim Stallard 	struct dst_entry *dst;
240b93cfb9cSTim Stallard 	bool res = false;
241b93cfb9cSTim Stallard 
242b93cfb9cSTim Stallard 	dst = ip6_route_output(net, sk, fl6);
243b93cfb9cSTim Stallard 	if (!dst->error) {
244b93cfb9cSTim Stallard 		struct rt6_info *rt = (struct rt6_info *)dst;
245b93cfb9cSTim Stallard 		struct in6_addr prefsrc;
246b93cfb9cSTim Stallard 
247b93cfb9cSTim Stallard 		rt6_get_prefsrc(rt, &prefsrc);
248b93cfb9cSTim Stallard 		res = !ipv6_addr_any(&prefsrc);
249b93cfb9cSTim Stallard 	}
250b93cfb9cSTim Stallard 	dst_release(dst);
251b93cfb9cSTim Stallard 	return res;
252b93cfb9cSTim Stallard }
253b93cfb9cSTim Stallard 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  *	an inline helper for the "simple" if statement below
2561da177e4SLinus Torvalds  *	checks if parameter problem report is caused by an
2571da177e4SLinus Torvalds  *	unrecognized IPv6 option that has the Option Type
2581da177e4SLinus Torvalds  *	highest-order two bits set to 10
2591da177e4SLinus Torvalds  */
2601da177e4SLinus Torvalds 
opt_unrec(struct sk_buff * skb,__u32 offset)261a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	u8 _optval, *op;
2641da177e4SLinus Torvalds 
265bbe735e4SArnaldo Carvalho de Melo 	offset += skb_network_offset(skb);
2661da177e4SLinus Torvalds 	op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
26763159f29SIan Morris 	if (!op)
268a50feda5SEric Dumazet 		return true;
2691da177e4SLinus Torvalds 	return (*op & 0xC0) == 0x80;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
icmpv6_push_pending_frames(struct sock * sk,struct flowi6 * fl6,struct icmp6hdr * thdr,int len)2724e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
2736d0bfe22SLorenzo Colitti 				struct icmp6hdr *thdr, int len)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	struct sk_buff *skb;
2761da177e4SLinus Torvalds 	struct icmp6hdr *icmp6h;
2771da177e4SLinus Torvalds 
278e5d08d71SIan Morris 	skb = skb_peek(&sk->sk_write_queue);
27963159f29SIan Morris 	if (!skb)
2804e64b1edSJoe Perches 		return;
2811da177e4SLinus Torvalds 
282cc70ab26SArnaldo Carvalho de Melo 	icmp6h = icmp6_hdr(skb);
2831da177e4SLinus Torvalds 	memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
2841da177e4SLinus Torvalds 	icmp6h->icmp6_cksum = 0;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
28707f0757aSJoe Perches 		skb->csum = csum_partial(icmp6h,
2881da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), skb->csum);
2894c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2904c9483b2SDavid S. Miller 						      &fl6->daddr,
2914c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
2921da177e4SLinus Torvalds 						      skb->csum);
2931da177e4SLinus Torvalds 	} else {
294868c86bcSAl Viro 		__wsum tmp_csum = 0;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
2971da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
2981da177e4SLinus Torvalds 		}
2991da177e4SLinus Torvalds 
30007f0757aSJoe Perches 		tmp_csum = csum_partial(icmp6h,
3011da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), tmp_csum);
3024c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
3034c9483b2SDavid S. Miller 						      &fl6->daddr,
3044c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
305868c86bcSAl Viro 						      tmp_csum);
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 	ip6_push_pending_frames(sk);
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds struct icmpv6_msg {
3111da177e4SLinus Torvalds 	struct sk_buff	*skb;
3121da177e4SLinus Torvalds 	int		offset;
313763ecff1SYasuyuki Kozakai 	uint8_t		type;
3141da177e4SLinus Torvalds };
3151da177e4SLinus Torvalds 
icmpv6_getfrag(void * from,char * to,int offset,int len,int odd,struct sk_buff * skb)3161da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
3191da177e4SLinus Torvalds 	struct sk_buff *org_skb = msg->skb;
3208d5930dfSAl Viro 	__wsum csum;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
3238d5930dfSAl Viro 				      to, len);
3241da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
325763ecff1SYasuyuki Kozakai 	if (!(msg->type & ICMPV6_INFOMSG_MASK))
326763ecff1SYasuyuki Kozakai 		nf_ct_attach(skb, org_skb);
3271da177e4SLinus Torvalds 	return 0;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
33007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
mip6_addr_swap(struct sk_buff * skb,const struct inet6_skb_parm * opt)331ee576c47SJason A. Donenfeld static void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt)
33279383236SMasahide NAKAMURA {
3330660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
33479383236SMasahide NAKAMURA 	struct ipv6_destopt_hao *hao;
33579383236SMasahide NAKAMURA 	int off;
33679383236SMasahide NAKAMURA 
33779383236SMasahide NAKAMURA 	if (opt->dsthao) {
33879383236SMasahide NAKAMURA 		off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
33979383236SMasahide NAKAMURA 		if (likely(off >= 0)) {
340d56f90a7SArnaldo Carvalho de Melo 			hao = (struct ipv6_destopt_hao *)
341d56f90a7SArnaldo Carvalho de Melo 					(skb_network_header(skb) + off);
342bc617613SJiapeng Chong 			swap(iph->saddr, hao->addr);
34379383236SMasahide NAKAMURA 		}
34479383236SMasahide NAKAMURA 	}
34579383236SMasahide NAKAMURA }
34679383236SMasahide NAKAMURA #else
mip6_addr_swap(struct sk_buff * skb,const struct inet6_skb_parm * opt)347ee576c47SJason A. Donenfeld static inline void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt) {}
34879383236SMasahide NAKAMURA #endif
34979383236SMasahide NAKAMURA 
icmpv6_route_lookup(struct net * net,struct sk_buff * skb,struct sock * sk,struct flowi6 * fl6)350e8243534Sstephen hemminger static struct dst_entry *icmpv6_route_lookup(struct net *net,
351e8243534Sstephen hemminger 					     struct sk_buff *skb,
352e8243534Sstephen hemminger 					     struct sock *sk,
353e8243534Sstephen hemminger 					     struct flowi6 *fl6)
354b42835dbSDavid S. Miller {
355b42835dbSDavid S. Miller 	struct dst_entry *dst, *dst2;
3564c9483b2SDavid S. Miller 	struct flowi6 fl2;
357b42835dbSDavid S. Miller 	int err;
358b42835dbSDavid S. Miller 
359343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst, fl6);
360b42835dbSDavid S. Miller 	if (err)
361b42835dbSDavid S. Miller 		return ERR_PTR(err);
362b42835dbSDavid S. Miller 
363b42835dbSDavid S. Miller 	/*
364b42835dbSDavid S. Miller 	 * We won't send icmp if the destination is known
3657ab75456SMahesh Bandewar 	 * anycast unless we need to treat anycast as unicast.
366b42835dbSDavid S. Miller 	 */
3677ab75456SMahesh Bandewar 	if (!READ_ONCE(net->ipv6.sysctl.icmpv6_error_anycast_as_unicast) &&
3687ab75456SMahesh Bandewar 	    ipv6_anycast_destination(dst, &fl6->daddr)) {
369ba7a46f1SJoe Perches 		net_dbg_ratelimited("icmp6_send: acast source\n");
370b42835dbSDavid S. Miller 		dst_release(dst);
371b42835dbSDavid S. Miller 		return ERR_PTR(-EINVAL);
372b42835dbSDavid S. Miller 	}
373b42835dbSDavid S. Miller 
374b42835dbSDavid S. Miller 	/* No need to clone since we're just using its address. */
375b42835dbSDavid S. Miller 	dst2 = dst;
376b42835dbSDavid S. Miller 
3774c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
378452edd59SDavid S. Miller 	if (!IS_ERR(dst)) {
379b42835dbSDavid S. Miller 		if (dst != dst2)
380b42835dbSDavid S. Miller 			return dst;
381452edd59SDavid S. Miller 	} else {
382452edd59SDavid S. Miller 		if (PTR_ERR(dst) == -EPERM)
383b42835dbSDavid S. Miller 			dst = NULL;
384452edd59SDavid S. Miller 		else
385452edd59SDavid S. Miller 			return dst;
386b42835dbSDavid S. Miller 	}
387b42835dbSDavid S. Miller 
3884c9483b2SDavid S. Miller 	err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6);
389b42835dbSDavid S. Miller 	if (err)
390b42835dbSDavid S. Miller 		goto relookup_failed;
391b42835dbSDavid S. Miller 
392343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst2, &fl2);
393b42835dbSDavid S. Miller 	if (err)
394b42835dbSDavid S. Miller 		goto relookup_failed;
395b42835dbSDavid S. Miller 
3964c9483b2SDavid S. Miller 	dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
397452edd59SDavid S. Miller 	if (!IS_ERR(dst2)) {
398b42835dbSDavid S. Miller 		dst_release(dst);
399b42835dbSDavid S. Miller 		dst = dst2;
400452edd59SDavid S. Miller 	} else {
401452edd59SDavid S. Miller 		err = PTR_ERR(dst2);
402452edd59SDavid S. Miller 		if (err == -EPERM) {
403b42835dbSDavid S. Miller 			dst_release(dst);
404452edd59SDavid S. Miller 			return dst2;
405452edd59SDavid S. Miller 		} else
406b42835dbSDavid S. Miller 			goto relookup_failed;
407b42835dbSDavid S. Miller 	}
408b42835dbSDavid S. Miller 
409b42835dbSDavid S. Miller relookup_failed:
410b42835dbSDavid S. Miller 	if (dst)
411b42835dbSDavid S. Miller 		return dst;
412b42835dbSDavid S. Miller 	return ERR_PTR(err);
413b42835dbSDavid S. Miller }
414b42835dbSDavid S. Miller 
icmp6_dev(const struct sk_buff * skb)415e1ae5c2eSStephen Suryaputra static struct net_device *icmp6_dev(const struct sk_buff *skb)
4161b70d792SDavid Ahern {
417e1ae5c2eSStephen Suryaputra 	struct net_device *dev = skb->dev;
4181b70d792SDavid Ahern 
4191b70d792SDavid Ahern 	/* for local traffic to local address, skb dev is the loopback
4201b70d792SDavid Ahern 	 * device. Check if there is a dst attached to the skb and if so
42124b711edSDavid Ahern 	 * get the real device index. Same is needed for replies to a link
42224b711edSDavid Ahern 	 * local address on a device enslaved to an L3 master device
4231b70d792SDavid Ahern 	 */
424e1ae5c2eSStephen Suryaputra 	if (unlikely(dev->ifindex == LOOPBACK_IFINDEX || netif_is_l3_master(skb->dev))) {
4251b70d792SDavid Ahern 		const struct rt6_info *rt6 = skb_rt6_info(skb);
4261b70d792SDavid Ahern 
4271b70d792SDavid Ahern 		/* The destination could be an external IP in Ext Hdr (SRv6, RPL, etc.),
428e1ae5c2eSStephen Suryaputra 		 * and ip6_null_entry could be set to skb if no route is found.
4291b70d792SDavid Ahern 		 */
4301b70d792SDavid Ahern 		if (rt6 && rt6->rt6i_idev)
431e1ae5c2eSStephen Suryaputra 			dev = rt6->rt6i_idev->dev;
432e1ae5c2eSStephen Suryaputra 	}
433e1ae5c2eSStephen Suryaputra 
434e1ae5c2eSStephen Suryaputra 	return dev;
435e1ae5c2eSStephen Suryaputra }
436e1ae5c2eSStephen Suryaputra 
icmp6_iif(const struct sk_buff * skb)4371b70d792SDavid Ahern static int icmp6_iif(const struct sk_buff *skb)
4381b70d792SDavid Ahern {
4391da177e4SLinus Torvalds 	return icmp6_dev(skb)->ifindex;
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
442cc7a21b6SEric Dumazet /*
443ee576c47SJason A. Donenfeld  *	Send an ICMP message in response to a packet in error
444ee576c47SJason A. Donenfeld  */
icmp6_send(struct sk_buff * skb,u8 type,u8 code,__u32 info,const struct in6_addr * force_saddr,const struct inet6_skb_parm * parm)4451da177e4SLinus Torvalds void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
4461da177e4SLinus Torvalds 		const struct in6_addr *force_saddr,
4470660e03fSArnaldo Carvalho de Melo 		const struct inet6_skb_parm *parm)
44884427d53SYOSHIFUJI Hideaki {
4498d933670SEric Dumazet 	struct inet6_dev *idev = NULL;
45084427d53SYOSHIFUJI Hideaki 	struct ipv6hdr *hdr = ipv6_hdr(skb);
451b71d1d42SEric Dumazet 	struct sock *sk;
4521da177e4SLinus Torvalds 	struct net *net;
4531da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
4544c9483b2SDavid S. Miller 	const struct in6_addr *saddr = NULL;
4551da177e4SLinus Torvalds 	struct dst_entry *dst;
45626879da5SWei Wang 	struct icmp6hdr tmp_hdr;
4571da177e4SLinus Torvalds 	struct flowi6 fl6;
4581da177e4SLinus Torvalds 	struct icmpv6_msg msg;
4591da177e4SLinus Torvalds 	struct ipcm6_cookie ipc6;
4608d933670SEric Dumazet 	int iif = 0;
4611da177e4SLinus Torvalds 	int addr_type = 0;
46227a884dcSArnaldo Carvalho de Melo 	int len;
46329a3cad5SSimon Horman 	u32 mark;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	if ((u8 *)hdr < skb->head ||
4668d933670SEric Dumazet 	    (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
4678d933670SEric Dumazet 		return;
4688d933670SEric Dumazet 
4698d933670SEric Dumazet 	if (!skb->dev)
4701da177e4SLinus Torvalds 		return;
4711da177e4SLinus Torvalds 	net = dev_net(skb->dev);
4721da177e4SLinus Torvalds 	mark = IP6_REPLY_MARK(net, skb->mark);
4735f5624cfSPravin B Shelar 	/*
4741da177e4SLinus Torvalds 	 *	Make sure we respect the rules
4751da177e4SLinus Torvalds 	 *	i.e. RFC 1885 2.4(e)
4761da177e4SLinus Torvalds 	 *	Rule (e.1) is enforced by not using icmp6_send
4771da177e4SLinus Torvalds 	 *	in any code that processes icmp errors.
478446fab59SFX Le Bail 	 */
479d94c1f92SFX Le Bail 	addr_type = ipv6_addr_type(&hdr->daddr);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) ||
4821da177e4SLinus Torvalds 	    ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr))
4831da177e4SLinus Torvalds 		saddr = &hdr->daddr;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/*
4869a6b4b39Szhuyj 	 *	Dest addr check
4871da177e4SLinus Torvalds 	 */
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) {
4901da177e4SLinus Torvalds 		if (type != ICMPV6_PKT_TOOBIG &&
4911da177e4SLinus Torvalds 		    !(type == ICMPV6_PARAMPROB &&
4921da177e4SLinus Torvalds 		      code == ICMPV6_UNK_OPTION &&
4931da177e4SLinus Torvalds 		      (opt_unrec(skb, info))))
4941da177e4SLinus Torvalds 			return;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 		saddr = NULL;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->saddr);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	/*
5024832c30dSDavid Ahern 	 *	Source addr check
5031b70d792SDavid Ahern 	 */
5044832c30dSDavid Ahern 
505272928d1SMathieu Desnoyers 	if (__ipv6_addr_needs_scope_id(addr_type)) {
506272928d1SMathieu Desnoyers 		iif = icmp6_iif(skb);
507272928d1SMathieu Desnoyers 	} else {
508272928d1SMathieu Desnoyers 		/*
509272928d1SMathieu Desnoyers 		 * The source device is used for looking up which routing table
51079dc7e3fSDavid Ahern 		 * to use for sending an ICMP error.
5111da177e4SLinus Torvalds 		 */
5121da177e4SLinus Torvalds 		iif = l3mdev_master_ifindex(skb->dev);
5138de3351eSYOSHIFUJI Hideaki 	}
5148de3351eSYOSHIFUJI Hideaki 
5158de3351eSYOSHIFUJI Hideaki 	/*
5168de3351eSYOSHIFUJI Hideaki 	 *	Must not send error if the source does not uniquely
5171da177e4SLinus Torvalds 	 *	identify a single node (RFC2463 Section 2.4).
5181da177e4SLinus Torvalds 	 *	We check unspecified / multicast addresses here,
5194b3418fbSBjørn Mork 	 *	and anycast addresses will be checked later.
5204b3418fbSBjørn Mork 	 */
5211da177e4SLinus Torvalds 	if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
5221da177e4SLinus Torvalds 		net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n",
5231da177e4SLinus Torvalds 				    &hdr->saddr, &hdr->daddr);
5241da177e4SLinus Torvalds 		return;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	/*
5284b3418fbSBjørn Mork 	 *	Never answer to a ICMP packet.
5294b3418fbSBjørn Mork 	 */
5301da177e4SLinus Torvalds 	if (is_ineligible(skb)) {
5311da177e4SLinus Torvalds 		net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n",
5321da177e4SLinus Torvalds 				    &hdr->saddr, &hdr->daddr);
5337ba91ecbSJesper Dangaard Brouer 		return;
5347ba91ecbSJesper Dangaard Brouer 	}
5357ba91ecbSJesper Dangaard Brouer 
5367ba91ecbSJesper Dangaard Brouer 	/* Needed by both icmp_global_allow and icmpv6_xmit_lock */
5370bc19985SStephen Suryaputra 	local_bh_disable();
5387ba91ecbSJesper Dangaard Brouer 
5397ba91ecbSJesper Dangaard Brouer 	/* Check global sysctl_icmp_msgs_per_sec ratelimit */
540ee576c47SJason A. Donenfeld 	if (!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, type))
54179383236SMasahide NAKAMURA 		goto out_bh_enable;
542fac6fce9SFrancesco Ruggeri 
543fac6fce9SFrancesco Ruggeri 	mip6_addr_swap(skb, parm);
544fac6fce9SFrancesco Ruggeri 
545fac6fce9SFrancesco Ruggeri 	sk = icmpv6_xmit_lock(net);
5464c9483b2SDavid S. Miller 	if (!sk)
5474c9483b2SDavid S. Miller 		goto out_bh_enable;
5484e3fd7a0SAlexey Dobriyan 
549b1cadc1aSEric Dumazet 	memset(&fl6, 0, sizeof(fl6));
550b1cadc1aSEric Dumazet 	fl6.flowi6_proto = IPPROTO_ICMPV6;
551fac6fce9SFrancesco Ruggeri 	fl6.daddr = hdr->saddr;
5524e3fd7a0SAlexey Dobriyan 	if (force_saddr)
553b93cfb9cSTim Stallard 		saddr = force_saddr;
554fac6fce9SFrancesco Ruggeri 	if (saddr) {
555fac6fce9SFrancesco Ruggeri 		fl6.saddr = *saddr;
556fac6fce9SFrancesco Ruggeri 	} else if (!icmpv6_rt_has_prefsrc(sk, type, &fl6)) {
557ee576c47SJason A. Donenfeld 		/* select a more meaningful saddr from input if */
558fac6fce9SFrancesco Ruggeri 		struct net_device *in_netdev;
559fac6fce9SFrancesco Ruggeri 
560fac6fce9SFrancesco Ruggeri 		in_netdev = dev_get_by_index(net, parm->iif);
561fac6fce9SFrancesco Ruggeri 		if (in_netdev) {
562fac6fce9SFrancesco Ruggeri 			ipv6_dev_get_saddr(net, in_netdev, &fl6.daddr,
563fac6fce9SFrancesco Ruggeri 					   inet6_sk(sk)->srcprefs,
564fac6fce9SFrancesco Ruggeri 					   &fl6.saddr);
565e110861fSLorenzo Colitti 			dev_put(in_netdev);
5664c9483b2SDavid S. Miller 		}
5671958b856SDavid S. Miller 	}
5681958b856SDavid S. Miller 	fl6.flowi6_mark = mark;
569e2d118a1SLorenzo Colitti 	fl6.flowi6_oif = iif;
570b4bac172SDavid Ahern 	fl6.fl6_icmp_type = type;
5713df98d79SPaul Moore 	fl6.fl6_icmp_code = code;
5721da177e4SLinus Torvalds 	fl6.flowi6_uid = sock_net_uid(net, NULL);
573fdc0bde9SDenis V. Lunev 	fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, NULL);
574405666dbSDenis V. Lunev 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
5754c9483b2SDavid S. Miller 
5761da177e4SLinus Torvalds 	np = inet6_sk(sk);
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	if (!icmpv6_xrlim_allow(sk, type, &fl6))
5791da177e4SLinus Torvalds 		goto out;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = type;
5821da177e4SLinus Torvalds 	tmp_hdr.icmp6_code = code;
5834c9483b2SDavid S. Miller 	tmp_hdr.icmp6_cksum = 0;
5844c9483b2SDavid S. Miller 	tmp_hdr.icmp6_pointer = htonl(info);
585c4062dfcSErich E. Hoover 
586c4062dfcSErich E. Hoover 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
5871da177e4SLinus Torvalds 		fl6.flowi6_oif = np->mcast_oif;
588b515430aSWillem de Bruijn 	else if (!fl6.flowi6_oif)
5890da7536fSWillem de Bruijn 		fl6.flowi6_oif = np->ucast_oif;
59038b7097bSHannes Frederic Sowa 
59138b7097bSHannes Frederic Sowa 	ipcm6_init_sk(&ipc6, np);
5924c9483b2SDavid S. Miller 	ipc6.sockc.mark = mark;
593b42835dbSDavid S. Miller 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
5941da177e4SLinus Torvalds 
5958de3351eSYOSHIFUJI Hideaki 	dst = icmpv6_route_lookup(net, skb, sk, &fl6);
59626879da5SWei Wang 	if (IS_ERR(dst))
5971da177e4SLinus Torvalds 		goto out;
5981da177e4SLinus Torvalds 
599bbe735e4SArnaldo Carvalho de Melo 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
600763ecff1SYasuyuki Kozakai 
6011da177e4SLinus Torvalds 	msg.skb = skb;
6021da177e4SLinus Torvalds 	msg.offset = skb_network_offset(skb);
6031da177e4SLinus Torvalds 	msg.type = type;
6041da177e4SLinus Torvalds 
6054b3418fbSBjørn Mork 	len = skb->len - msg.offset;
6064b3418fbSBjørn Mork 	len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr));
6071da177e4SLinus Torvalds 	if (len < 0) {
6081da177e4SLinus Torvalds 		net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n",
6091da177e4SLinus Torvalds 				    &hdr->saddr, &hdr->daddr);
610cfdf7647SEric Dumazet 		goto out_dst_release;
611cfdf7647SEric Dumazet 	}
6121da177e4SLinus Torvalds 
6134e64b1edSJoe Perches 	rcu_read_lock();
6141da177e4SLinus Torvalds 	idev = __in6_dev_get(skb->dev);
61526879da5SWei Wang 
61626879da5SWei Wang 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
6175fdaa88dSWillem de Bruijn 			    len + sizeof(struct icmp6hdr),
61843a43b60SHannes Frederic Sowa 			    sizeof(struct icmp6hdr),
6191da177e4SLinus Torvalds 			    &ipc6, &fl6, (struct rt6_info *)dst,
620cfdf7647SEric Dumazet 			    MSG_DONTWAIT)) {
6214e64b1edSJoe Perches 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
622cfdf7647SEric Dumazet 		ip6_flush_pending_frames(sk);
6231da177e4SLinus Torvalds 	} else {
624cfdf7647SEric Dumazet 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
6251da177e4SLinus Torvalds 					   len + sizeof(struct icmp6hdr));
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 	rcu_read_unlock();
628405666dbSDenis V. Lunev out_dst_release:
6297ba91ecbSJesper Dangaard Brouer 	dst_release(dst);
6307ba91ecbSJesper Dangaard Brouer out:
6311da177e4SLinus Torvalds 	icmpv6_xmit_unlock(sk);
632cc7a21b6SEric Dumazet out_bh_enable:
6335f5624cfSPravin B Shelar 	local_bh_enable();
6341ad6d548SMenglong Dong }
6355f5624cfSPravin B Shelar EXPORT_SYMBOL(icmp6_send);
6361ad6d548SMenglong Dong 
6371ad6d548SMenglong Dong /* Slightly more convenient version of icmp6_send with drop reasons.
6385f5624cfSPravin B Shelar  */
icmpv6_param_prob_reason(struct sk_buff * skb,u8 code,int pos,enum skb_drop_reason reason)639ee576c47SJason A. Donenfeld void icmpv6_param_prob_reason(struct sk_buff *skb, u8 code, int pos,
6401ad6d548SMenglong Dong 			      enum skb_drop_reason reason)
6415f5624cfSPravin B Shelar {
6427159039aSYOSHIFUJI Hideaki 	icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL, IP6CB(skb));
6435fbba8acSEric Dumazet 	kfree_skb_reason(skb, reason);
6445fbba8acSEric Dumazet }
6455fbba8acSEric Dumazet 
6465fbba8acSEric Dumazet /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
6475fbba8acSEric Dumazet  * if sufficient data bytes are available
6485fbba8acSEric Dumazet  * @nhs is the size of the tunnel header(s) :
64920e1954fSEric Dumazet  *  Either an IPv4 header for SIT encap
65020e1954fSEric Dumazet  *         an IPv4 header + GRE header for GRE encap
6515fbba8acSEric Dumazet  */
ip6_err_gen_icmpv6_unreach(struct sk_buff * skb,int nhs,int type,unsigned int data_len)6522d7a3b27SEric Dumazet int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type,
6535fbba8acSEric Dumazet 			       unsigned int data_len)
6545fbba8acSEric Dumazet {
65520e1954fSEric Dumazet 	struct in6_addr temp_saddr;
6565fbba8acSEric Dumazet 	struct rt6_info *rt;
6575fbba8acSEric Dumazet 	struct sk_buff *skb2;
6585fbba8acSEric Dumazet 	u32 info = 0;
6595fbba8acSEric Dumazet 
66020e1954fSEric Dumazet 	if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8))
66120e1954fSEric Dumazet 		return 1;
66220e1954fSEric Dumazet 
66320e1954fSEric Dumazet 	/* RFC 4884 (partial) support for ICMP extensions */
66420e1954fSEric Dumazet 	if (data_len < 128 || (data_len & 7) || skb->len < data_len)
6655fbba8acSEric Dumazet 		data_len = 0;
6665fbba8acSEric Dumazet 
6675fbba8acSEric Dumazet 	skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC);
6685fbba8acSEric Dumazet 
6695fbba8acSEric Dumazet 	if (!skb2)
6705fbba8acSEric Dumazet 		return 1;
6715fbba8acSEric Dumazet 
6725fbba8acSEric Dumazet 	skb_dst_drop(skb2);
673b75cc8f9SDavid Ahern 	skb_pull(skb2, nhs);
674b75cc8f9SDavid Ahern 	skb_reset_network_header(skb2);
6755fbba8acSEric Dumazet 
6765fbba8acSEric Dumazet 	rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0,
6775fbba8acSEric Dumazet 			skb, 0);
6785fbba8acSEric Dumazet 
6792d7a3b27SEric Dumazet 	if (rt && rt->dst.dev)
68020e1954fSEric Dumazet 		skb2->dev = rt->dst.dev;
68120e1954fSEric Dumazet 
68220e1954fSEric Dumazet 	ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr);
68320e1954fSEric Dumazet 
68420e1954fSEric Dumazet 	if (data_len) {
68520e1954fSEric Dumazet 		/* RFC 4884 (partial) support :
68620e1954fSEric Dumazet 		 * insert 0 padding at the end, before the extensions
68720e1954fSEric Dumazet 		 */
68820e1954fSEric Dumazet 		__skb_push(skb2, nhs);
68920e1954fSEric Dumazet 		skb_reset_network_header(skb2);
69020e1954fSEric Dumazet 		memmove(skb2->data, skb2->data + nhs, data_len - nhs);
69120e1954fSEric Dumazet 		memset(skb2->data + data_len - nhs, 0, nhs);
69220e1954fSEric Dumazet 		/* RFC 4884 4.5 : Length is measured in 64-bit words,
69320e1954fSEric Dumazet 		 * and stored in reserved[0]
6942d7a3b27SEric Dumazet 		 */
6952d7a3b27SEric Dumazet 		info = (data_len/8) << 24;
696ee576c47SJason A. Donenfeld 	}
6972d7a3b27SEric Dumazet 	if (type == ICMP_TIME_EXCEEDED)
6982d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
699ee576c47SJason A. Donenfeld 			   info, &temp_saddr, IP6CB(skb2));
7005fbba8acSEric Dumazet 	else
7015fbba8acSEric Dumazet 		icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH,
7025fbba8acSEric Dumazet 			   info, &temp_saddr, IP6CB(skb2));
7035fbba8acSEric Dumazet 	if (rt)
7045fbba8acSEric Dumazet 		ip6_rt_put(rt);
7055fbba8acSEric Dumazet 
7065fbba8acSEric Dumazet 	kfree_skb(skb2);
7075fbba8acSEric Dumazet 
7085fbba8acSEric Dumazet 	return 0;
709ac03694bSEric Dumazet }
7101da177e4SLinus Torvalds EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach);
711c346dca1SYOSHIFUJI Hideaki 
icmpv6_echo_reply(struct sk_buff * skb)71284427d53SYOSHIFUJI Hideaki static enum skb_drop_reason icmpv6_echo_reply(struct sk_buff *skb)
7131da177e4SLinus Torvalds {
71484427d53SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
715b71d1d42SEric Dumazet 	struct sock *sk;
716cc70ab26SArnaldo Carvalho de Melo 	struct inet6_dev *idev;
7171da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
7184c9483b2SDavid S. Miller 	const struct in6_addr *saddr = NULL;
7191da177e4SLinus Torvalds 	struct icmp6hdr *icmph = icmp6_hdr(skb);
7201da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
72126879da5SWei Wang 	struct flowi6 fl6;
722e110861fSLorenzo Colitti 	struct icmpv6_msg msg;
723ac03694bSEric Dumazet 	struct dst_entry *dst;
7240b03a5caSStephen Suryaputra 	struct ipcm6_cookie ipc6;
7251fd07f33SAndreas Roeseler 	u32 mark = IP6_REPLY_MARK(net, skb->mark);
7261da177e4SLinus Torvalds 	SKB_DR(reason);
72703f1ecccSStephen Suryaputra 	bool acast;
72803f1ecccSStephen Suryaputra 	u8 type;
729ac03694bSEric Dumazet 
73003f1ecccSStephen Suryaputra 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
7310660e03fSArnaldo Carvalho de Melo 	    net->ipv6.sysctl.icmpv6_echo_ignore_multicast)
7321da177e4SLinus Torvalds 		return reason;
7330b03a5caSStephen Suryaputra 
7340b03a5caSStephen Suryaputra 	saddr = &ipv6_hdr(skb)->daddr;
735ac03694bSEric Dumazet 
7360b03a5caSStephen Suryaputra 	acast = ipv6_anycast_destination(skb_dst(skb), saddr);
737509aba3bSFX Le Bail 	if (acast && net->ipv6.sysctl.icmpv6_echo_ignore_anycast)
7380b03a5caSStephen Suryaputra 		return reason;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	if (!ipv6_unicast_destination(skb) &&
7411fd07f33SAndreas Roeseler 	    !(net->ipv6.sysctl.anycast_src_echo_reply && acast))
7421fd07f33SAndreas Roeseler 		saddr = NULL;
7431fd07f33SAndreas Roeseler 
7441fd07f33SAndreas Roeseler 	if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
7451fd07f33SAndreas Roeseler 		type = ICMPV6_EXT_ECHO_REPLY;
7461da177e4SLinus Torvalds 	else
7471fd07f33SAndreas Roeseler 		type = ICMPV6_ECHO_REPLY;
7481da177e4SLinus Torvalds 
7494c9483b2SDavid S. Miller 	memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
750a346abe0SEric Dumazet 	tmp_hdr.icmp6_type = type;
751a346abe0SEric Dumazet 
752a346abe0SEric Dumazet 	memset(&fl6, 0, sizeof(fl6));
7534c9483b2SDavid S. Miller 	if (net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES)
7544e3fd7a0SAlexey Dobriyan 		fl6.flowlabel = ip6_flowlabel(ipv6_hdr(skb));
7551da177e4SLinus Torvalds 
7564e3fd7a0SAlexey Dobriyan 	fl6.flowi6_proto = IPPROTO_ICMPV6;
7571b70d792SDavid Ahern 	fl6.daddr = ipv6_hdr(skb)->saddr;
7581fd07f33SAndreas Roeseler 	if (saddr)
759e110861fSLorenzo Colitti 		fl6.saddr = *saddr;
760e2d118a1SLorenzo Colitti 	fl6.flowi6_oif = icmp6_iif(skb);
7613df98d79SPaul Moore 	fl6.fl6_icmp_type = type;
7621da177e4SLinus Torvalds 	fl6.flowi6_mark = mark;
7637ba91ecbSJesper Dangaard Brouer 	fl6.flowi6_uid = sock_net_uid(net, NULL);
764fdc0bde9SDenis V. Lunev 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
76563159f29SIan Morris 
7667ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
767fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
768405666dbSDenis V. Lunev 	if (!sk)
7694c9483b2SDavid S. Miller 		goto out_bh_enable;
7704c9483b2SDavid S. Miller 	np = inet6_sk(sk);
771c4062dfcSErich E. Hoover 
772c4062dfcSErich E. Hoover 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
7731da177e4SLinus Torvalds 		fl6.flowi6_oif = np->mcast_oif;
7744e64b1edSJoe Perches 	else if (!fl6.flowi6_oif)
7751da177e4SLinus Torvalds 		fl6.flowi6_oif = np->ucast_oif;
7764c9483b2SDavid S. Miller 
777452edd59SDavid S. Miller 	if (ip6_dst_lookup(net, sk, &dst, &fl6))
778e104411bSPatrick McHardy 		goto out;
7791da177e4SLinus Torvalds 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
7800bc19985SStephen Suryaputra 	if (IS_ERR(dst))
7810bc19985SStephen Suryaputra 		goto out;
7820bc19985SStephen Suryaputra 
7830bc19985SStephen Suryaputra 	/* Check the ratelimit */
7840bc19985SStephen Suryaputra 	if ((!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, ICMPV6_ECHO_REPLY)) ||
785cfdf7647SEric Dumazet 	    !icmpv6_xrlim_allow(sk, ICMPV6_ECHO_REPLY, &fl6))
7861da177e4SLinus Torvalds 		goto out_dst_release;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	idev = __in6_dev_get(skb->dev);
7891fd07f33SAndreas Roeseler 
7901da177e4SLinus Torvalds 	msg.skb = skb;
791b515430aSWillem de Bruijn 	msg.offset = 0;
79226879da5SWei Wang 	msg.type = type;
79326879da5SWei Wang 
7940da7536fSWillem de Bruijn 	ipcm6_init_sk(&ipc6, np);
79526879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
7961fd07f33SAndreas Roeseler 	ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb));
7971fd07f33SAndreas Roeseler 	ipc6.sockc.mark = mark;
7981fd07f33SAndreas Roeseler 
7991fd07f33SAndreas Roeseler 	if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
8004e64b1edSJoe Perches 		if (!icmp_build_probe(skb, (struct icmphdr *)&tmp_hdr))
8014e64b1edSJoe Perches 			goto out_dst_release;
80226879da5SWei Wang 
8035fdaa88dSWillem de Bruijn 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
804a16292a0SEric Dumazet 			    skb->len + sizeof(struct icmp6hdr),
8051da177e4SLinus Torvalds 			    sizeof(struct icmp6hdr), &ipc6, &fl6,
806cfdf7647SEric Dumazet 			    (struct rt6_info *)dst, MSG_DONTWAIT)) {
8074e64b1edSJoe Perches 		__ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
808cfdf7647SEric Dumazet 		ip6_flush_pending_frames(sk);
809ac03694bSEric Dumazet 	} else {
8101da177e4SLinus Torvalds 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
8110bc19985SStephen Suryaputra 					   skb->len + sizeof(struct icmp6hdr));
8121da177e4SLinus Torvalds 		reason = SKB_CONSUMED;
8131da177e4SLinus Torvalds 	}
814405666dbSDenis V. Lunev out_dst_release:
8157ba91ecbSJesper Dangaard Brouer 	dst_release(dst);
8167ba91ecbSJesper Dangaard Brouer out:
817ac03694bSEric Dumazet 	icmpv6_xmit_unlock(sk);
8181da177e4SLinus Torvalds out_bh_enable:
8191da177e4SLinus Torvalds 	local_bh_enable();
82030c89badSEric Dumazet 	return reason;
82130c89badSEric Dumazet }
8221da177e4SLinus Torvalds 
icmpv6_notify(struct sk_buff * skb,u8 type,u8 code,__be32 info)823e4129440SAndrew Lunn enum skb_drop_reason icmpv6_notify(struct sk_buff *skb, u8 type,
82430c89badSEric Dumazet 				   u8 code, __be32 info)
82541135cc8SAlexey Dobriyan {
82630c89badSEric Dumazet 	struct inet6_skb_parm *opt = IP6CB(skb);
8271da177e4SLinus Torvalds 	struct net *net = dev_net(skb->dev);
82875f2811cSJesse Gross 	const struct inet6_protocol *ipprot;
829f9242b6bSDavid S. Miller 	enum skb_drop_reason reason;
8301da177e4SLinus Torvalds 	int inner_offset;
83130c89badSEric Dumazet 	__be16 frag_off;
83230c89badSEric Dumazet 	u8 nexthdr;
8337304fe46SDuan Jiong 
8341da177e4SLinus Torvalds 	reason = pskb_may_pull_reason(skb, sizeof(struct ipv6hdr));
835e4129440SAndrew Lunn 	if (reason != SKB_NOT_DROPPED_YET)
836e4129440SAndrew Lunn 		goto out;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	seg6_icmp_srh(skb, opt);
8391da177e4SLinus Torvalds 
84075f2811cSJesse Gross 	nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
84175f2811cSJesse Gross 	if (ipv6_ext_hdr(nexthdr)) {
84230c89badSEric Dumazet 		/* now skip over extension headers */
84330c89badSEric Dumazet 		inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
8447304fe46SDuan Jiong 						&nexthdr, &frag_off);
84530c89badSEric Dumazet 		if (inner_offset < 0) {
8461da177e4SLinus Torvalds 			SKB_DR_SET(reason, IPV6_BAD_EXTHDR);
8471da177e4SLinus Torvalds 			goto out;
8481da177e4SLinus Torvalds 		}
8491da177e4SLinus Torvalds 	} else {
8501da177e4SLinus Torvalds 		inner_offset = sizeof(struct ipv6hdr);
85130c89badSEric Dumazet 	}
85230c89badSEric Dumazet 
8537304fe46SDuan Jiong 	/* Checkin header including 8 bytes of inner protocol header. */
8541da177e4SLinus Torvalds 	reason = pskb_may_pull_reason(skb, inner_offset + 8);
8551da177e4SLinus Torvalds 	if (reason != SKB_NOT_DROPPED_YET)
8561da177e4SLinus Torvalds 		goto out;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	/* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
8591da177e4SLinus Torvalds 	   Without this we will not able f.e. to make source routed
8601da177e4SLinus Torvalds 	   pmtu discovery.
8611da177e4SLinus Torvalds 	   Corresponding argument (opt) to notifiers is already added.
862f9242b6bSDavid S. Miller 	   --ANK (980726)
8631da177e4SLinus Torvalds 	 */
864e4129440SAndrew Lunn 
8651da177e4SLinus Torvalds 	ipprot = rcu_dereference(inet6_protos[nexthdr]);
86669d6da0bSPavel Emelyanov 	if (ipprot && ipprot->err_handler)
86730c89badSEric Dumazet 		ipprot->err_handler(skb, opt, type, code, inner_offset, info);
8687304fe46SDuan Jiong 
8697304fe46SDuan Jiong 	raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
870a16292a0SEric Dumazet 	return SKB_CONSUMED;
87130c89badSEric Dumazet 
8721da177e4SLinus Torvalds out:
8731da177e4SLinus Torvalds 	__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
8741da177e4SLinus Torvalds 	return reason;
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds /*
878e5bbef20SHerbert Xu  *	Handle icmp messages
8791da177e4SLinus Torvalds  */
880b384c95aSMenglong Dong 
icmpv6_rcv(struct sk_buff * skb)881e6f86b0fSVirgile Jarry static int icmpv6_rcv(struct sk_buff *skb)
882e1ae5c2eSStephen Suryaputra {
8831da177e4SLinus Torvalds 	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
884b71d1d42SEric Dumazet 	struct net *net = dev_net(skb->dev);
8851da177e4SLinus Torvalds 	struct net_device *dev = icmp6_dev(skb);
886d5fdd6baSBrian Haley 	struct inet6_dev *idev = __in6_dev_get(dev);
8871da177e4SLinus Torvalds 	const struct in6_addr *saddr, *daddr;
888aebcf82cSHerbert Xu 	struct icmp6hdr *hdr;
889def8b4faSAlexey Dobriyan 	u8 type;
8908b7817f3SHerbert Xu 
8918b7817f3SHerbert Xu 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
892def8b4faSAlexey Dobriyan 		struct sec_path *sp = skb_sec_path(skb);
893b384c95aSMenglong Dong 		int nh;
894b384c95aSMenglong Dong 
895aebcf82cSHerbert Xu 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
896b384c95aSMenglong Dong 				 XFRM_STATE_ICMP)) {
897aebcf82cSHerbert Xu 			reason = SKB_DROP_REASON_XFRM_POLICY;
89881aded24SDavid S. Miller 			goto drop_no_count;
8998b7817f3SHerbert Xu 		}
9008b7817f3SHerbert Xu 
9018b7817f3SHerbert Xu 		if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
9028b7817f3SHerbert Xu 			goto drop_no_count;
9038b7817f3SHerbert Xu 
904b384c95aSMenglong Dong 		nh = skb_network_offset(skb);
905b384c95aSMenglong Dong 		skb_set_network_header(skb, sizeof(*hdr));
906b384c95aSMenglong Dong 
9078b7817f3SHerbert Xu 		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN,
908b384c95aSMenglong Dong 						skb)) {
9098b7817f3SHerbert Xu 			reason = SKB_DROP_REASON_XFRM_POLICY;
9108b7817f3SHerbert Xu 			goto drop_no_count;
9118b7817f3SHerbert Xu 		}
9128b7817f3SHerbert Xu 
913a16292a0SEric Dumazet 		skb_set_network_header(skb, nh);
9141da177e4SLinus Torvalds 	}
9150660e03fSArnaldo Carvalho de Melo 
9160660e03fSArnaldo Carvalho de Melo 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INMSGS);
9171da177e4SLinus Torvalds 
91839471ac8STom Herbert 	saddr = &ipv6_hdr(skb)->saddr;
919ba7a46f1SJoe Perches 	daddr = &ipv6_hdr(skb)->daddr;
9200c6ce78aSHarvey Harrison 
9216a5dc9e5SEric Dumazet 	if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) {
9221da177e4SLinus Torvalds 		net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n",
9231da177e4SLinus Torvalds 				    saddr, daddr);
9248cf22943SHerbert Xu 		goto csum_error;
9258cf22943SHerbert Xu 	}
9261da177e4SLinus Torvalds 
927cc70ab26SArnaldo Carvalho de Melo 	if (!pskb_pull(skb, sizeof(*hdr)))
9281da177e4SLinus Torvalds 		goto discard_it;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	hdr = icmp6_hdr(skb);
931f3832ed2SEric Dumazet 
9321da177e4SLinus Torvalds 	type = hdr->icmp6_type;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	ICMP6MSGIN_INC_STATS(dev_net(dev), idev, type);
935e6f86b0fSVirgile Jarry 
936ac03694bSEric Dumazet 	switch (type) {
9371da177e4SLinus Torvalds 	case ICMPV6_ECHO_REQUEST:
9381fd07f33SAndreas Roeseler 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all)
9391fd07f33SAndreas Roeseler 			reason = icmpv6_echo_reply(skb);
9404a2f7083SKuniyuki Iwashima 		break;
941ac03694bSEric Dumazet 	case ICMPV6_EXT_ECHO_REQUEST:
9421fd07f33SAndreas Roeseler 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all &&
9431da177e4SLinus Torvalds 		    READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe))
9441da177e4SLinus Torvalds 			reason = icmpv6_echo_reply(skb);
945b384c95aSMenglong Dong 		break;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	case ICMPV6_ECHO_REPLY:
94831433202SAndreas Roeseler 		reason = ping_rcv(skb);
949b384c95aSMenglong Dong 		break;
95031433202SAndreas Roeseler 
95131433202SAndreas Roeseler 	case ICMPV6_EXT_ECHO_REPLY:
9521da177e4SLinus Torvalds 		reason = ping_rcv(skb);
9531da177e4SLinus Torvalds 		break;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
9561da177e4SLinus Torvalds 		/* BUGGG_FUTURE: if packet contains rthdr, we cannot update
9571da177e4SLinus Torvalds 		   standard destination cache. Seems, only "advanced"
9581da177e4SLinus Torvalds 		   destination cache will allow to solve this problem
9591da177e4SLinus Torvalds 		   --ANK (980726)
960cc70ab26SArnaldo Carvalho de Melo 		 */
9611da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
962275757e6SGustavo A. R. Silva 			goto discard_it;
963a8eceea8SJoe Perches 		hdr = icmp6_hdr(skb);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 		/* to notify */
9661da177e4SLinus Torvalds 		fallthrough;
96730c89badSEric Dumazet 	case ICMPV6_DEST_UNREACH:
96830c89badSEric Dumazet 	case ICMPV6_TIME_EXCEED:
9691da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
9701da177e4SLinus Torvalds 		reason = icmpv6_notify(skb, type, hdr->icmp6_code,
9711da177e4SLinus Torvalds 				       hdr->icmp6_mtu);
9721da177e4SLinus Torvalds 		break;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	case NDISC_ROUTER_SOLICITATION:
9751da177e4SLinus Torvalds 	case NDISC_ROUTER_ADVERTISEMENT:
976545dbcd1SEric Dumazet 	case NDISC_NEIGHBOUR_SOLICITATION:
9771da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_ADVERTISEMENT:
9781da177e4SLinus Torvalds 	case NDISC_REDIRECT:
9791da177e4SLinus Torvalds 		reason = ndisc_rcv(skb);
9801da177e4SLinus Torvalds 		break;
981f185de28STaehee Yoo 
9821da177e4SLinus Torvalds 	case ICMPV6_MGM_QUERY:
9831da177e4SLinus Torvalds 		igmp6_event_query(skb);
9841da177e4SLinus Torvalds 		return 0;
985f185de28STaehee Yoo 
9861da177e4SLinus Torvalds 	case ICMPV6_MGM_REPORT:
9871da177e4SLinus Torvalds 		igmp6_event_report(skb);
9881da177e4SLinus Torvalds 		return 0;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	case ICMPV6_MGM_REDUCTION:
9911da177e4SLinus Torvalds 	case ICMPV6_NI_QUERY:
9921da177e4SLinus Torvalds 	case ICMPV6_NI_REPLY:
9931da177e4SLinus Torvalds 	case ICMPV6_MLD2_REPORT:
9941da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REQUEST:
9951da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REPLY:
9961da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_SOL:
9971da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_ADV:
9981da177e4SLinus Torvalds 		break;
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	default:
10011da177e4SLinus Torvalds 		/* informational */
10024b3418fbSBjørn Mork 		if (type & ICMPV6_INFOMSG_MASK)
10034b3418fbSBjørn Mork 			break;
1004ea85a0a2SDavid S. Miller 
10051da177e4SLinus Torvalds 		net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n",
10061da177e4SLinus Torvalds 				    saddr, daddr);
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 		/*
10091da177e4SLinus Torvalds 		 * error of unknown type.
101030c89badSEric Dumazet 		 * must pass to upper level
101130c89badSEric Dumazet 		 */
10123ff50b79SStephen Hemminger 
10133ff50b79SStephen Hemminger 		reason = icmpv6_notify(skb, type, hdr->icmp6_code,
1014e3e32170SRick Jones 				       hdr->icmp6_mtu);
1015e3e32170SRick Jones 	}
1016e3e32170SRick Jones 
1017b384c95aSMenglong Dong 	/* until the v6 path can be better sorted assume failure and
1018b384c95aSMenglong Dong 	 * preserve the status quo behaviour for the rest of the paths to here
1019e3e32170SRick Jones 	 */
1020b384c95aSMenglong Dong 	if (reason)
1021e3e32170SRick Jones 		kfree_skb_reason(skb, reason);
10221da177e4SLinus Torvalds 	else
10231da177e4SLinus Torvalds 		consume_skb(skb);
10246a5dc9e5SEric Dumazet 
1025b384c95aSMenglong Dong 	return 0;
1026a16292a0SEric Dumazet 
10271da177e4SLinus Torvalds csum_error:
1028a16292a0SEric Dumazet 	reason = SKB_DROP_REASON_ICMP_CSUM;
10298b7817f3SHerbert Xu 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS);
1030b384c95aSMenglong Dong discard_it:
10311da177e4SLinus Torvalds 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INERRORS);
10321da177e4SLinus Torvalds drop_no_count:
10331da177e4SLinus Torvalds 	kfree_skb_reason(skb, reason);
10344c9483b2SDavid S. Miller 	return 0;
103595e41e93SYOSHIFUJI Hideaki }
103695e41e93SYOSHIFUJI Hideaki 
icmpv6_flow_init(const struct sock * sk,struct flowi6 * fl6,u8 type,const struct in6_addr * saddr,const struct in6_addr * daddr,int oif)103795e41e93SYOSHIFUJI Hideaki void icmpv6_flow_init(const struct sock *sk, struct flowi6 *fl6, u8 type,
103895e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *saddr,
103995e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *daddr, int oif)
10404c9483b2SDavid S. Miller {
10414e3fd7a0SAlexey Dobriyan 	memset(fl6, 0, sizeof(*fl6));
10424e3fd7a0SAlexey Dobriyan 	fl6->saddr = *saddr;
10434c9483b2SDavid S. Miller 	fl6->daddr = *daddr;
10441958b856SDavid S. Miller 	fl6->flowi6_proto	= IPPROTO_ICMPV6;
10451958b856SDavid S. Miller 	fl6->fl6_icmp_type	= type;
10464c9483b2SDavid S. Miller 	fl6->fl6_icmp_code	= 0;
10473df98d79SPaul Moore 	fl6->flowi6_oif		= oif;
104895e41e93SYOSHIFUJI Hideaki 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
104995e41e93SYOSHIFUJI Hideaki }
10506a17b961SEric Dumazet 
icmpv6_init(void)10511da177e4SLinus Torvalds int __init icmpv6_init(void)
10521da177e4SLinus Torvalds {
10533232a1efSKefeng Wang 	struct sock *sk;
10541da177e4SLinus Torvalds 	int err, i;
10556f912042SKAMEZAWA Hiroyuki 
10561ed8516fSDenis V. Lunev 	for_each_possible_cpu(i) {
10576a17b961SEric Dumazet 		err = inet_ctl_sock_create(&sk, PF_INET6,
10581da177e4SLinus Torvalds 					   SOCK_RAW, IPPROTO_ICMPV6, &init_net);
1059f3213831SJoe Perches 		if (err < 0) {
10601da177e4SLinus Torvalds 			pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
10616a17b961SEric Dumazet 			       err);
10621da177e4SLinus Torvalds 			return err;
10631da177e4SLinus Torvalds 		}
10646a17b961SEric Dumazet 
10655c8cafd6SDenis V. Lunev 		per_cpu(ipv6_icmp_sk, i) = sk;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 		/* Enough space for 2 64K ICMP packets, including
10681da177e4SLinus Torvalds 		 * sk_buff struct overhead.
106987fb4b7bSEric Dumazet 		 */
10701da177e4SLinus Torvalds 		sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
107198c6d1b2SDenis V. Lunev 	}
107298c6d1b2SDenis V. Lunev 
107398c6d1b2SDenis V. Lunev 	err = -EAGAIN;
107498c6d1b2SDenis V. Lunev 	if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
10755f5624cfSPravin B Shelar 		goto fail;
10765f5624cfSPravin B Shelar 
10775f5624cfSPravin B Shelar 	err = inet6_register_icmp_sender(icmp6_send);
10785f5624cfSPravin B Shelar 	if (err)
107998c6d1b2SDenis V. Lunev 		goto sender_reg_err;
108098c6d1b2SDenis V. Lunev 	return 0;
10815f5624cfSPravin B Shelar 
10825f5624cfSPravin B Shelar sender_reg_err:
108398c6d1b2SDenis V. Lunev 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
1084f3213831SJoe Perches fail:
108598c6d1b2SDenis V. Lunev 	pr_err("Failed to register ICMP6 protocol\n");
108698c6d1b2SDenis V. Lunev 	return err;
108798c6d1b2SDenis V. Lunev }
10888ed7edceSAlexey Dobriyan 
icmpv6_cleanup(void)108998c6d1b2SDenis V. Lunev void icmpv6_cleanup(void)
10905f5624cfSPravin B Shelar {
10911da177e4SLinus Torvalds 	inet6_unregister_icmp_sender(icmp6_send);
10921da177e4SLinus Torvalds 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
10931da177e4SLinus Torvalds }
109498c6d1b2SDenis V. Lunev 
10959b5b5cffSArjan van de Ven 
10961da177e4SLinus Torvalds static const struct icmp6_err {
10971da177e4SLinus Torvalds 	int err;
10981da177e4SLinus Torvalds 	int fatal;
10991da177e4SLinus Torvalds } tab_unreach[] = {
11001da177e4SLinus Torvalds 	{	/* NOROUTE */
11011da177e4SLinus Torvalds 		.err	= ENETUNREACH,
11021da177e4SLinus Torvalds 		.fatal	= 0,
11031da177e4SLinus Torvalds 	},
11041da177e4SLinus Torvalds 	{	/* ADM_PROHIBITED */
11051da177e4SLinus Torvalds 		.err	= EACCES,
11061da177e4SLinus Torvalds 		.fatal	= 1,
11071da177e4SLinus Torvalds 	},
11081da177e4SLinus Torvalds 	{	/* Was NOT_NEIGHBOUR, now reserved */
11091da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
11101da177e4SLinus Torvalds 		.fatal	= 0,
11111da177e4SLinus Torvalds 	},
11121da177e4SLinus Torvalds 	{	/* ADDR_UNREACH	*/
11131da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
11141da177e4SLinus Torvalds 		.fatal	= 0,
11151da177e4SLinus Torvalds 	},
11161da177e4SLinus Torvalds 	{	/* PORT_UNREACH	*/
11171da177e4SLinus Torvalds 		.err	= ECONNREFUSED,
11181da177e4SLinus Torvalds 		.fatal	= 1,
111961e76b17SJiri Bohac 	},
112061e76b17SJiri Bohac 	{	/* POLICY_FAIL */
112161e76b17SJiri Bohac 		.err	= EACCES,
112261e76b17SJiri Bohac 		.fatal	= 1,
112361e76b17SJiri Bohac 	},
112461e76b17SJiri Bohac 	{	/* REJECT_ROUTE	*/
112561e76b17SJiri Bohac 		.err	= EACCES,
112661e76b17SJiri Bohac 		.fatal	= 1,
11271da177e4SLinus Torvalds 	},
11281da177e4SLinus Torvalds };
1129d5fdd6baSBrian Haley 
icmpv6_err_convert(u8 type,u8 code,int * err)11301da177e4SLinus Torvalds int icmpv6_err_convert(u8 type, u8 code, int *err)
11311da177e4SLinus Torvalds {
11321da177e4SLinus Torvalds 	int fatal = 0;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	*err = EPROTO;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	switch (type) {
11371da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
113861e76b17SJiri Bohac 		fatal = 1;
11391da177e4SLinus Torvalds 		if (code < ARRAY_SIZE(tab_unreach)) {
11401da177e4SLinus Torvalds 			*err  = tab_unreach[code].err;
11411da177e4SLinus Torvalds 			fatal = tab_unreach[code].fatal;
11421da177e4SLinus Torvalds 		}
11431da177e4SLinus Torvalds 		break;
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
11461da177e4SLinus Torvalds 		*err = EMSGSIZE;
11471da177e4SLinus Torvalds 		break;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
11501da177e4SLinus Torvalds 		*err = EPROTO;
11511da177e4SLinus Torvalds 		fatal = 1;
11521da177e4SLinus Torvalds 		break;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
11551da177e4SLinus Torvalds 		*err = EHOSTUNREACH;
11563ff50b79SStephen Hemminger 		break;
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	return fatal;
11607159039aSYOSHIFUJI Hideaki }
11617159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert);
11621da177e4SLinus Torvalds 
1163e8243534Sstephen hemminger #ifdef CONFIG_SYSCTL
11641da177e4SLinus Torvalds static struct ctl_table ipv6_icmp_table_template[] = {
11651da177e4SLinus Torvalds 	{
116641a76906SDaniel Lezcano 		.procname	= "ratelimit",
11671da177e4SLinus Torvalds 		.data		= &init_net.ipv6.sysctl.icmpv6_time,
11681da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
11696d9f239aSAlexey Dobriyan 		.mode		= 0644,
11701da177e4SLinus Torvalds 		.proc_handler	= proc_dointvec_ms_jiffies,
1171e6f86b0fSVirgile Jarry 	},
1172e6f86b0fSVirgile Jarry 	{
1173e6f86b0fSVirgile Jarry 		.procname	= "echo_ignore_all",
1174a6175633SEric Dumazet 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_all,
1175e6f86b0fSVirgile Jarry 		.maxlen		= sizeof(u8),
1176a6175633SEric Dumazet 		.mode		= 0644,
1177e6f86b0fSVirgile Jarry 		.proc_handler = proc_dou8vec_minmax,
117803f1ecccSStephen Suryaputra 	},
117903f1ecccSStephen Suryaputra 	{
118003f1ecccSStephen Suryaputra 		.procname	= "echo_ignore_multicast",
1181a6175633SEric Dumazet 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_multicast,
118203f1ecccSStephen Suryaputra 		.maxlen		= sizeof(u8),
1183a6175633SEric Dumazet 		.mode		= 0644,
118403f1ecccSStephen Suryaputra 		.proc_handler = proc_dou8vec_minmax,
11850b03a5caSStephen Suryaputra 	},
11860b03a5caSStephen Suryaputra 	{
11870b03a5caSStephen Suryaputra 		.procname	= "echo_ignore_anycast",
1188a6175633SEric Dumazet 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_anycast,
11890b03a5caSStephen Suryaputra 		.maxlen		= sizeof(u8),
1190a6175633SEric Dumazet 		.mode		= 0644,
11910b03a5caSStephen Suryaputra 		.proc_handler = proc_dou8vec_minmax,
11920bc19985SStephen Suryaputra 	},
11930bc19985SStephen Suryaputra 	{
11940bc19985SStephen Suryaputra 		.procname	= "ratemask",
11950bc19985SStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_ratemask_ptr,
11960bc19985SStephen Suryaputra 		.maxlen		= ICMPV6_MSG_MAX + 1,
11970bc19985SStephen Suryaputra 		.mode		= 0644,
11980bc19985SStephen Suryaputra 		.proc_handler = proc_do_large_bitmap,
11997ab75456SMahesh Bandewar 	},
12007ab75456SMahesh Bandewar 	{
12017ab75456SMahesh Bandewar 		.procname	= "error_anycast_as_unicast",
12027ab75456SMahesh Bandewar 		.data		= &init_net.ipv6.sysctl.icmpv6_error_anycast_as_unicast,
12037ab75456SMahesh Bandewar 		.maxlen		= sizeof(u8),
12047ab75456SMahesh Bandewar 		.mode		= 0644,
12057ab75456SMahesh Bandewar 		.proc_handler	= proc_dou8vec_minmax,
12067ab75456SMahesh Bandewar 		.extra1		= SYSCTL_ZERO,
12077ab75456SMahesh Bandewar 		.extra2		= SYSCTL_ONE,
1208f8572d8fSEric W. Biederman 	},
12091da177e4SLinus Torvalds 	{ },
1210760f2d01SDaniel Lezcano };
12112c8c1e72SAlexey Dobriyan 
ipv6_icmp_sysctl_init(struct net * net)1212760f2d01SDaniel Lezcano struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
1213760f2d01SDaniel Lezcano {
1214760f2d01SDaniel Lezcano 	struct ctl_table *table;
1215760f2d01SDaniel Lezcano 
1216760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_icmp_table_template,
1217760f2d01SDaniel Lezcano 			sizeof(ipv6_icmp_table_template),
12185ee09105SYOSHIFUJI Hideaki 			GFP_KERNEL);
1219e6f86b0fSVirgile Jarry 
12205ee09105SYOSHIFUJI Hideaki 	if (table) {
1221e6f86b0fSVirgile Jarry 		table[0].data = &net->ipv6.sysctl.icmpv6_time;
122203f1ecccSStephen Suryaputra 		table[1].data = &net->ipv6.sysctl.icmpv6_echo_ignore_all;
12230b03a5caSStephen Suryaputra 		table[2].data = &net->ipv6.sysctl.icmpv6_echo_ignore_multicast;
12240bc19985SStephen Suryaputra 		table[3].data = &net->ipv6.sysctl.icmpv6_echo_ignore_anycast;
12257ab75456SMahesh Bandewar 		table[4].data = &net->ipv6.sysctl.icmpv6_ratemask_ptr;
1226e6f86b0fSVirgile Jarry 		table[5].data = &net->ipv6.sysctl.icmpv6_error_anycast_as_unicast;
1227760f2d01SDaniel Lezcano 	}
1228760f2d01SDaniel Lezcano 	return table;
1229*c899710fSJoel Granados }
1230*c899710fSJoel Granados 
ipv6_icmp_sysctl_table_size(void)1231*c899710fSJoel Granados size_t ipv6_icmp_sysctl_table_size(void)
1232*c899710fSJoel Granados {
1233*c899710fSJoel Granados 	return ARRAY_SIZE(ipv6_icmp_table_template);
12341da177e4SLinus Torvalds }
1235 #endif
1236