xref: /openbmc/linux/net/ipv6/icmp.c (revision 1fd07f33)
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>
601da177e4SLinus Torvalds #include <net/transp_v6.h>
611da177e4SLinus Torvalds #include <net/ip6_route.h>
621da177e4SLinus Torvalds #include <net/addrconf.h>
631da177e4SLinus Torvalds #include <net/icmp.h>
648b7817f3SHerbert Xu #include <net/xfrm.h>
651ed8516fSDenis V. Lunev #include <net/inet_common.h>
66825edac4SHannes Frederic Sowa #include <net/dsfield.h>
67ca254490SDavid Ahern #include <net/l3mdev.h>
681da177e4SLinus Torvalds 
697c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds  *	The ICMP socket(s). This is the most convenient way to flow control
731da177e4SLinus Torvalds  *	our ICMP output as well as maintain a clean interface throughout
741da177e4SLinus Torvalds  *	all layers. All Socketless IP sends will soon be gone.
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  *	On SMP we have one ICMP socket per-cpu.
771da177e4SLinus Torvalds  */
782789c14dSEric Dumazet static struct sock *icmpv6_sk(struct net *net)
7998c6d1b2SDenis V. Lunev {
802789c14dSEric Dumazet 	return this_cpu_read(*net->ipv6.icmp_sk);
8198c6d1b2SDenis V. Lunev }
821da177e4SLinus Torvalds 
8332bbd879SStefano Brivio static int icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
846f809da2SSteffen Klassert 		       u8 type, u8 code, int offset, __be32 info)
856f809da2SSteffen Klassert {
866d0bfe22SLorenzo Colitti 	/* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */
876d0bfe22SLorenzo Colitti 	struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset);
886f809da2SSteffen Klassert 	struct net *net = dev_net(skb->dev);
896f809da2SSteffen Klassert 
906f809da2SSteffen Klassert 	if (type == ICMPV6_PKT_TOOBIG)
915f379ef5SGeorg Kohmann 		ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0, sock_net_uid(net, NULL));
926f809da2SSteffen Klassert 	else if (type == NDISC_REDIRECT)
93e2d118a1SLorenzo Colitti 		ip6_redirect(skb, net, skb->dev->ifindex, 0,
94e2d118a1SLorenzo Colitti 			     sock_net_uid(net, NULL));
956d0bfe22SLorenzo Colitti 
966d0bfe22SLorenzo Colitti 	if (!(type & ICMPV6_INFOMSG_MASK))
976d0bfe22SLorenzo Colitti 		if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST)
98dcb94b88SHannes Frederic Sowa 			ping_err(skb, offset, ntohl(info));
9932bbd879SStefano Brivio 
10032bbd879SStefano Brivio 	return 0;
1016f809da2SSteffen Klassert }
1026f809da2SSteffen Klassert 
103e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb);
1041da177e4SLinus Torvalds 
10541135cc8SAlexey Dobriyan static const struct inet6_protocol icmpv6_protocol = {
1061da177e4SLinus Torvalds 	.handler	=	icmpv6_rcv,
1076f809da2SSteffen Klassert 	.err_handler	=	icmpv6_err,
1088b7817f3SHerbert Xu 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1091da177e4SLinus Torvalds };
1101da177e4SLinus Torvalds 
1117ba91ecbSJesper Dangaard Brouer /* Called with BH disabled */
112fdc0bde9SDenis V. Lunev static __inline__ struct sock *icmpv6_xmit_lock(struct net *net)
1131da177e4SLinus Torvalds {
114fdc0bde9SDenis V. Lunev 	struct sock *sk;
115fdc0bde9SDenis V. Lunev 
116fdc0bde9SDenis V. Lunev 	sk = icmpv6_sk(net);
117405666dbSDenis V. Lunev 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
1181da177e4SLinus Torvalds 		/* This can happen if the output path (f.e. SIT or
1191da177e4SLinus Torvalds 		 * ip6ip6 tunnel) signals dst_link_failure() for an
1201da177e4SLinus Torvalds 		 * outgoing ICMP6 packet.
1211da177e4SLinus Torvalds 		 */
122fdc0bde9SDenis V. Lunev 		return NULL;
1231da177e4SLinus Torvalds 	}
124fdc0bde9SDenis V. Lunev 	return sk;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
127405666dbSDenis V. Lunev static __inline__ void icmpv6_xmit_unlock(struct sock *sk)
1281da177e4SLinus Torvalds {
1297ba91ecbSJesper Dangaard Brouer 	spin_unlock(&sk->sk_lock.slock);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /*
1331da177e4SLinus Torvalds  * Figure out, may we reply to this packet with icmp error.
1341da177e4SLinus Torvalds  *
1351da177e4SLinus Torvalds  * We do not reply, if:
1361da177e4SLinus Torvalds  *	- it was icmp error message.
1371da177e4SLinus Torvalds  *	- it is truncated, so that it is known, that protocol is ICMPV6
1381da177e4SLinus Torvalds  *	  (i.e. in the middle of some exthdr)
1391da177e4SLinus Torvalds  *
1401da177e4SLinus Torvalds  *	--ANK (980726)
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds 
143a50feda5SEric Dumazet static bool is_ineligible(const struct sk_buff *skb)
1441da177e4SLinus Torvalds {
1450660e03fSArnaldo Carvalho de Melo 	int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
1461da177e4SLinus Torvalds 	int len = skb->len - ptr;
1470660e03fSArnaldo Carvalho de Melo 	__u8 nexthdr = ipv6_hdr(skb)->nexthdr;
14875f2811cSJesse Gross 	__be16 frag_off;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	if (len < 0)
151a50feda5SEric Dumazet 		return true;
1521da177e4SLinus Torvalds 
15375f2811cSJesse Gross 	ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off);
1541da177e4SLinus Torvalds 	if (ptr < 0)
155a50feda5SEric Dumazet 		return false;
1561da177e4SLinus Torvalds 	if (nexthdr == IPPROTO_ICMPV6) {
1571da177e4SLinus Torvalds 		u8 _type, *tp;
1581da177e4SLinus Torvalds 		tp = skb_header_pointer(skb,
1591da177e4SLinus Torvalds 			ptr+offsetof(struct icmp6hdr, icmp6_type),
1601da177e4SLinus Torvalds 			sizeof(_type), &_type);
1612efdaaafSHangbin Liu 
1622efdaaafSHangbin Liu 		/* Based on RFC 8200, Section 4.5 Fragment Header, return
1632efdaaafSHangbin Liu 		 * false if this is a fragment packet with no icmp header info.
1642efdaaafSHangbin Liu 		 */
1652efdaaafSHangbin Liu 		if (!tp && frag_off != 0)
1662efdaaafSHangbin Liu 			return false;
1672efdaaafSHangbin Liu 		else if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
168a50feda5SEric Dumazet 			return true;
1691da177e4SLinus Torvalds 	}
170a50feda5SEric Dumazet 	return false;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1730bc19985SStephen Suryaputra static bool icmpv6_mask_allow(struct net *net, int type)
174c0303efeSJesper Dangaard Brouer {
1750bc19985SStephen Suryaputra 	if (type > ICMPV6_MSG_MAX)
176c0303efeSJesper Dangaard Brouer 		return true;
177c0303efeSJesper Dangaard Brouer 
1780bc19985SStephen Suryaputra 	/* Limit if icmp type is set in ratemask. */
1790bc19985SStephen Suryaputra 	if (!test_bit(type, net->ipv6.sysctl.icmpv6_ratemask))
180c0303efeSJesper Dangaard Brouer 		return true;
181c0303efeSJesper Dangaard Brouer 
182c0303efeSJesper Dangaard Brouer 	return false;
183c0303efeSJesper Dangaard Brouer }
184c0303efeSJesper Dangaard Brouer 
1850bc19985SStephen Suryaputra static bool icmpv6_global_allow(struct net *net, int type)
186c0303efeSJesper Dangaard Brouer {
1870bc19985SStephen Suryaputra 	if (icmpv6_mask_allow(net, type))
188c0303efeSJesper Dangaard Brouer 		return true;
189c0303efeSJesper Dangaard Brouer 
190c0303efeSJesper Dangaard Brouer 	if (icmp_global_allow())
191c0303efeSJesper Dangaard Brouer 		return true;
192c0303efeSJesper Dangaard Brouer 
193c0303efeSJesper Dangaard Brouer 	return false;
194c0303efeSJesper Dangaard Brouer }
195c0303efeSJesper Dangaard Brouer 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  * Check the ICMP output rate limit
1981da177e4SLinus Torvalds  */
1994cdf507dSEric Dumazet static bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
2004c9483b2SDavid S. Miller 			       struct flowi6 *fl6)
2011da177e4SLinus Torvalds {
2023b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2034cdf507dSEric Dumazet 	struct dst_entry *dst;
20492d86829SDavid S. Miller 	bool res = false;
2051da177e4SLinus Torvalds 
2060bc19985SStephen Suryaputra 	if (icmpv6_mask_allow(net, type))
20792d86829SDavid S. Miller 		return true;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	/*
2101da177e4SLinus Torvalds 	 * Look up the output route.
2111da177e4SLinus Torvalds 	 * XXX: perhaps the expire for routing entries cloned by
2121da177e4SLinus Torvalds 	 * this lookup should be more aggressive (not longer than timeout).
2131da177e4SLinus Torvalds 	 */
2144c9483b2SDavid S. Miller 	dst = ip6_route_output(net, sk, fl6);
2151da177e4SLinus Torvalds 	if (dst->error) {
2163bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst),
217a11d206dSYOSHIFUJI Hideaki 			      IPSTATS_MIB_OUTNOROUTES);
2181da177e4SLinus Torvalds 	} else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
21992d86829SDavid S. Miller 		res = true;
2201da177e4SLinus Torvalds 	} else {
2211da177e4SLinus Torvalds 		struct rt6_info *rt = (struct rt6_info *)dst;
2229a43b709SBenjamin Thery 		int tmo = net->ipv6.sysctl.icmpv6_time;
223c0303efeSJesper Dangaard Brouer 		struct inet_peer *peer;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 		/* Give more bandwidth to wider prefixes. */
2261da177e4SLinus Torvalds 		if (rt->rt6i_dst.plen < 128)
2271da177e4SLinus Torvalds 			tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
2281da177e4SLinus Torvalds 
229c0303efeSJesper Dangaard Brouer 		peer = inet_getpeer_v6(net->ipv6.peers, &fl6->daddr, 1);
230fbfe95a4SDavid S. Miller 		res = inet_peer_xrlim_allow(peer, tmo);
2311d861aa4SDavid S. Miller 		if (peer)
2321d861aa4SDavid S. Miller 			inet_putpeer(peer);
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 	dst_release(dst);
2351da177e4SLinus Torvalds 	return res;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238b93cfb9cSTim Stallard static bool icmpv6_rt_has_prefsrc(struct sock *sk, u8 type,
239b93cfb9cSTim Stallard 				  struct flowi6 *fl6)
240b93cfb9cSTim Stallard {
241b93cfb9cSTim Stallard 	struct net *net = sock_net(sk);
242b93cfb9cSTim Stallard 	struct dst_entry *dst;
243b93cfb9cSTim Stallard 	bool res = false;
244b93cfb9cSTim Stallard 
245b93cfb9cSTim Stallard 	dst = ip6_route_output(net, sk, fl6);
246b93cfb9cSTim Stallard 	if (!dst->error) {
247b93cfb9cSTim Stallard 		struct rt6_info *rt = (struct rt6_info *)dst;
248b93cfb9cSTim Stallard 		struct in6_addr prefsrc;
249b93cfb9cSTim Stallard 
250b93cfb9cSTim Stallard 		rt6_get_prefsrc(rt, &prefsrc);
251b93cfb9cSTim Stallard 		res = !ipv6_addr_any(&prefsrc);
252b93cfb9cSTim Stallard 	}
253b93cfb9cSTim Stallard 	dst_release(dst);
254b93cfb9cSTim Stallard 	return res;
255b93cfb9cSTim Stallard }
256b93cfb9cSTim Stallard 
2571da177e4SLinus Torvalds /*
2581da177e4SLinus Torvalds  *	an inline helper for the "simple" if statement below
2591da177e4SLinus Torvalds  *	checks if parameter problem report is caused by an
2601da177e4SLinus Torvalds  *	unrecognized IPv6 option that has the Option Type
2611da177e4SLinus Torvalds  *	highest-order two bits set to 10
2621da177e4SLinus Torvalds  */
2631da177e4SLinus Torvalds 
264a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	u8 _optval, *op;
2671da177e4SLinus Torvalds 
268bbe735e4SArnaldo Carvalho de Melo 	offset += skb_network_offset(skb);
2691da177e4SLinus Torvalds 	op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
27063159f29SIan Morris 	if (!op)
271a50feda5SEric Dumazet 		return true;
2721da177e4SLinus Torvalds 	return (*op & 0xC0) == 0x80;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2754e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
2766d0bfe22SLorenzo Colitti 				struct icmp6hdr *thdr, int len)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	struct sk_buff *skb;
2791da177e4SLinus Torvalds 	struct icmp6hdr *icmp6h;
2801da177e4SLinus Torvalds 
281e5d08d71SIan Morris 	skb = skb_peek(&sk->sk_write_queue);
28263159f29SIan Morris 	if (!skb)
2834e64b1edSJoe Perches 		return;
2841da177e4SLinus Torvalds 
285cc70ab26SArnaldo Carvalho de Melo 	icmp6h = icmp6_hdr(skb);
2861da177e4SLinus Torvalds 	memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
2871da177e4SLinus Torvalds 	icmp6h->icmp6_cksum = 0;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
29007f0757aSJoe Perches 		skb->csum = csum_partial(icmp6h,
2911da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), skb->csum);
2924c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2934c9483b2SDavid S. Miller 						      &fl6->daddr,
2944c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
2951da177e4SLinus Torvalds 						      skb->csum);
2961da177e4SLinus Torvalds 	} else {
297868c86bcSAl Viro 		__wsum tmp_csum = 0;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
3001da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
3011da177e4SLinus Torvalds 		}
3021da177e4SLinus Torvalds 
30307f0757aSJoe Perches 		tmp_csum = csum_partial(icmp6h,
3041da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), tmp_csum);
3054c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
3064c9483b2SDavid S. Miller 						      &fl6->daddr,
3074c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
308868c86bcSAl Viro 						      tmp_csum);
3091da177e4SLinus Torvalds 	}
3101da177e4SLinus Torvalds 	ip6_push_pending_frames(sk);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds struct icmpv6_msg {
3141da177e4SLinus Torvalds 	struct sk_buff	*skb;
3151da177e4SLinus Torvalds 	int		offset;
316763ecff1SYasuyuki Kozakai 	uint8_t		type;
3171da177e4SLinus Torvalds };
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
3221da177e4SLinus Torvalds 	struct sk_buff *org_skb = msg->skb;
3238d5930dfSAl Viro 	__wsum csum;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
3268d5930dfSAl Viro 				      to, len);
3271da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
328763ecff1SYasuyuki Kozakai 	if (!(msg->type & ICMPV6_INFOMSG_MASK))
329763ecff1SYasuyuki Kozakai 		nf_ct_attach(skb, org_skb);
3301da177e4SLinus Torvalds 	return 0;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
33307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
334ee576c47SJason A. Donenfeld static void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt)
33579383236SMasahide NAKAMURA {
3360660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
33779383236SMasahide NAKAMURA 	struct ipv6_destopt_hao *hao;
33879383236SMasahide NAKAMURA 	struct in6_addr tmp;
33979383236SMasahide NAKAMURA 	int off;
34079383236SMasahide NAKAMURA 
34179383236SMasahide NAKAMURA 	if (opt->dsthao) {
34279383236SMasahide NAKAMURA 		off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
34379383236SMasahide NAKAMURA 		if (likely(off >= 0)) {
344d56f90a7SArnaldo Carvalho de Melo 			hao = (struct ipv6_destopt_hao *)
345d56f90a7SArnaldo Carvalho de Melo 					(skb_network_header(skb) + off);
3464e3fd7a0SAlexey Dobriyan 			tmp = iph->saddr;
3474e3fd7a0SAlexey Dobriyan 			iph->saddr = hao->addr;
3484e3fd7a0SAlexey Dobriyan 			hao->addr = tmp;
34979383236SMasahide NAKAMURA 		}
35079383236SMasahide NAKAMURA 	}
35179383236SMasahide NAKAMURA }
35279383236SMasahide NAKAMURA #else
353ee576c47SJason A. Donenfeld static inline void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt) {}
35479383236SMasahide NAKAMURA #endif
35579383236SMasahide NAKAMURA 
356e8243534Sstephen hemminger static struct dst_entry *icmpv6_route_lookup(struct net *net,
357e8243534Sstephen hemminger 					     struct sk_buff *skb,
358e8243534Sstephen hemminger 					     struct sock *sk,
359e8243534Sstephen hemminger 					     struct flowi6 *fl6)
360b42835dbSDavid S. Miller {
361b42835dbSDavid S. Miller 	struct dst_entry *dst, *dst2;
3624c9483b2SDavid S. Miller 	struct flowi6 fl2;
363b42835dbSDavid S. Miller 	int err;
364b42835dbSDavid S. Miller 
365343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst, fl6);
366b42835dbSDavid S. Miller 	if (err)
367b42835dbSDavid S. Miller 		return ERR_PTR(err);
368b42835dbSDavid S. Miller 
369b42835dbSDavid S. Miller 	/*
370b42835dbSDavid S. Miller 	 * We won't send icmp if the destination is known
371b42835dbSDavid S. Miller 	 * anycast.
372b42835dbSDavid S. Miller 	 */
3732647a9b0SMartin KaFai Lau 	if (ipv6_anycast_destination(dst, &fl6->daddr)) {
374ba7a46f1SJoe Perches 		net_dbg_ratelimited("icmp6_send: acast source\n");
375b42835dbSDavid S. Miller 		dst_release(dst);
376b42835dbSDavid S. Miller 		return ERR_PTR(-EINVAL);
377b42835dbSDavid S. Miller 	}
378b42835dbSDavid S. Miller 
379b42835dbSDavid S. Miller 	/* No need to clone since we're just using its address. */
380b42835dbSDavid S. Miller 	dst2 = dst;
381b42835dbSDavid S. Miller 
3824c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
383452edd59SDavid S. Miller 	if (!IS_ERR(dst)) {
384b42835dbSDavid S. Miller 		if (dst != dst2)
385b42835dbSDavid S. Miller 			return dst;
386452edd59SDavid S. Miller 	} else {
387452edd59SDavid S. Miller 		if (PTR_ERR(dst) == -EPERM)
388b42835dbSDavid S. Miller 			dst = NULL;
389452edd59SDavid S. Miller 		else
390452edd59SDavid S. Miller 			return dst;
391b42835dbSDavid S. Miller 	}
392b42835dbSDavid S. Miller 
3934c9483b2SDavid S. Miller 	err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6);
394b42835dbSDavid S. Miller 	if (err)
395b42835dbSDavid S. Miller 		goto relookup_failed;
396b42835dbSDavid S. Miller 
397343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst2, &fl2);
398b42835dbSDavid S. Miller 	if (err)
399b42835dbSDavid S. Miller 		goto relookup_failed;
400b42835dbSDavid S. Miller 
4014c9483b2SDavid S. Miller 	dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
402452edd59SDavid S. Miller 	if (!IS_ERR(dst2)) {
403b42835dbSDavid S. Miller 		dst_release(dst);
404b42835dbSDavid S. Miller 		dst = dst2;
405452edd59SDavid S. Miller 	} else {
406452edd59SDavid S. Miller 		err = PTR_ERR(dst2);
407452edd59SDavid S. Miller 		if (err == -EPERM) {
408b42835dbSDavid S. Miller 			dst_release(dst);
409452edd59SDavid S. Miller 			return dst2;
410452edd59SDavid S. Miller 		} else
411b42835dbSDavid S. Miller 			goto relookup_failed;
412b42835dbSDavid S. Miller 	}
413b42835dbSDavid S. Miller 
414b42835dbSDavid S. Miller relookup_failed:
415b42835dbSDavid S. Miller 	if (dst)
416b42835dbSDavid S. Miller 		return dst;
417b42835dbSDavid S. Miller 	return ERR_PTR(err);
418b42835dbSDavid S. Miller }
419b42835dbSDavid S. Miller 
420e1ae5c2eSStephen Suryaputra static struct net_device *icmp6_dev(const struct sk_buff *skb)
4211b70d792SDavid Ahern {
422e1ae5c2eSStephen Suryaputra 	struct net_device *dev = skb->dev;
4231b70d792SDavid Ahern 
4241b70d792SDavid Ahern 	/* for local traffic to local address, skb dev is the loopback
4251b70d792SDavid Ahern 	 * device. Check if there is a dst attached to the skb and if so
42624b711edSDavid Ahern 	 * get the real device index. Same is needed for replies to a link
42724b711edSDavid Ahern 	 * local address on a device enslaved to an L3 master device
4281b70d792SDavid Ahern 	 */
429e1ae5c2eSStephen Suryaputra 	if (unlikely(dev->ifindex == LOOPBACK_IFINDEX || netif_is_l3_master(skb->dev))) {
4301b70d792SDavid Ahern 		const struct rt6_info *rt6 = skb_rt6_info(skb);
4311b70d792SDavid Ahern 
4321b70d792SDavid Ahern 		if (rt6)
433e1ae5c2eSStephen Suryaputra 			dev = rt6->rt6i_idev->dev;
4341b70d792SDavid Ahern 	}
4351b70d792SDavid Ahern 
436e1ae5c2eSStephen Suryaputra 	return dev;
437e1ae5c2eSStephen Suryaputra }
438e1ae5c2eSStephen Suryaputra 
439e1ae5c2eSStephen Suryaputra static int icmp6_iif(const struct sk_buff *skb)
440e1ae5c2eSStephen Suryaputra {
441e1ae5c2eSStephen Suryaputra 	return icmp6_dev(skb)->ifindex;
4421b70d792SDavid Ahern }
4431b70d792SDavid Ahern 
4441da177e4SLinus Torvalds /*
4451da177e4SLinus Torvalds  *	Send an ICMP message in response to a packet in error
4461da177e4SLinus Torvalds  */
447cc7a21b6SEric Dumazet void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
448ee576c47SJason A. Donenfeld 		const struct in6_addr *force_saddr,
449ee576c47SJason A. Donenfeld 		const struct inet6_skb_parm *parm)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
4520660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
45384427d53SYOSHIFUJI Hideaki 	struct sock *sk;
4548d933670SEric Dumazet 	struct net *net;
45584427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
456b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
4571da177e4SLinus Torvalds 	struct dst_entry *dst;
4581da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
4594c9483b2SDavid S. Miller 	struct flowi6 fl6;
4601da177e4SLinus Torvalds 	struct icmpv6_msg msg;
46126879da5SWei Wang 	struct ipcm6_cookie ipc6;
4621da177e4SLinus Torvalds 	int iif = 0;
4631da177e4SLinus Torvalds 	int addr_type = 0;
4641da177e4SLinus Torvalds 	int len;
4658d933670SEric Dumazet 	u32 mark;
4661da177e4SLinus Torvalds 
46727a884dcSArnaldo Carvalho de Melo 	if ((u8 *)hdr < skb->head ||
46829a3cad5SSimon Horman 	    (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
4691da177e4SLinus Torvalds 		return;
4701da177e4SLinus Torvalds 
4718d933670SEric Dumazet 	if (!skb->dev)
4728d933670SEric Dumazet 		return;
4738d933670SEric Dumazet 	net = dev_net(skb->dev);
4748d933670SEric Dumazet 	mark = IP6_REPLY_MARK(net, skb->mark);
4751da177e4SLinus Torvalds 	/*
4761da177e4SLinus Torvalds 	 *	Make sure we respect the rules
4771da177e4SLinus Torvalds 	 *	i.e. RFC 1885 2.4(e)
4785f5624cfSPravin B Shelar 	 *	Rule (e.1) is enforced by not using icmp6_send
4791da177e4SLinus Torvalds 	 *	in any code that processes icmp errors.
4801da177e4SLinus Torvalds 	 */
4811da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->daddr);
4821da177e4SLinus Torvalds 
483446fab59SFX Le Bail 	if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) ||
484d94c1f92SFX Le Bail 	    ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr))
4851da177e4SLinus Torvalds 		saddr = &hdr->daddr;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/*
4881da177e4SLinus Torvalds 	 *	Dest addr check
4891da177e4SLinus Torvalds 	 */
4901da177e4SLinus Torvalds 
4919a6b4b39Szhuyj 	if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) {
4921da177e4SLinus Torvalds 		if (type != ICMPV6_PKT_TOOBIG &&
4931da177e4SLinus Torvalds 		    !(type == ICMPV6_PARAMPROB &&
4941da177e4SLinus Torvalds 		      code == ICMPV6_UNK_OPTION &&
4951da177e4SLinus Torvalds 		      (opt_unrec(skb, info))))
4961da177e4SLinus Torvalds 			return;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 		saddr = NULL;
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->saddr);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	/*
5041da177e4SLinus Torvalds 	 *	Source addr check
5051da177e4SLinus Torvalds 	 */
5061da177e4SLinus Torvalds 
5074832c30dSDavid Ahern 	if (__ipv6_addr_needs_scope_id(addr_type)) {
5081b70d792SDavid Ahern 		iif = icmp6_iif(skb);
5094832c30dSDavid Ahern 	} else {
510272928d1SMathieu Desnoyers 		/*
511272928d1SMathieu Desnoyers 		 * The source device is used for looking up which routing table
512272928d1SMathieu Desnoyers 		 * to use for sending an ICMP error.
513272928d1SMathieu Desnoyers 		 */
514272928d1SMathieu Desnoyers 		iif = l3mdev_master_ifindex(skb->dev);
51579dc7e3fSDavid Ahern 	}
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	/*
5188de3351eSYOSHIFUJI Hideaki 	 *	Must not send error if the source does not uniquely
5198de3351eSYOSHIFUJI Hideaki 	 *	identify a single node (RFC2463 Section 2.4).
5208de3351eSYOSHIFUJI Hideaki 	 *	We check unspecified / multicast addresses here,
5218de3351eSYOSHIFUJI Hideaki 	 *	and anycast addresses will be checked later.
5221da177e4SLinus Torvalds 	 */
5231da177e4SLinus Torvalds 	if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
5244b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n",
5254b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
5261da177e4SLinus Torvalds 		return;
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	/*
5301da177e4SLinus Torvalds 	 *	Never answer to a ICMP packet.
5311da177e4SLinus Torvalds 	 */
5321da177e4SLinus Torvalds 	if (is_ineligible(skb)) {
5334b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n",
5344b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
5351da177e4SLinus Torvalds 		return;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 
5387ba91ecbSJesper Dangaard Brouer 	/* Needed by both icmp_global_allow and icmpv6_xmit_lock */
5397ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
5407ba91ecbSJesper Dangaard Brouer 
5417ba91ecbSJesper Dangaard Brouer 	/* Check global sysctl_icmp_msgs_per_sec ratelimit */
5420bc19985SStephen Suryaputra 	if (!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, type))
5437ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
5447ba91ecbSJesper Dangaard Brouer 
545ee576c47SJason A. Donenfeld 	mip6_addr_swap(skb, parm);
54679383236SMasahide NAKAMURA 
547fac6fce9SFrancesco Ruggeri 	sk = icmpv6_xmit_lock(net);
548fac6fce9SFrancesco Ruggeri 	if (!sk)
549fac6fce9SFrancesco Ruggeri 		goto out_bh_enable;
550fac6fce9SFrancesco Ruggeri 
5514c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
5524c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
5534e3fd7a0SAlexey Dobriyan 	fl6.daddr = hdr->saddr;
554b1cadc1aSEric Dumazet 	if (force_saddr)
555b1cadc1aSEric Dumazet 		saddr = force_saddr;
556fac6fce9SFrancesco Ruggeri 	if (saddr) {
5574e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
558b93cfb9cSTim Stallard 	} else if (!icmpv6_rt_has_prefsrc(sk, type, &fl6)) {
559fac6fce9SFrancesco Ruggeri 		/* select a more meaningful saddr from input if */
560fac6fce9SFrancesco Ruggeri 		struct net_device *in_netdev;
561fac6fce9SFrancesco Ruggeri 
562ee576c47SJason A. Donenfeld 		in_netdev = dev_get_by_index(net, parm->iif);
563fac6fce9SFrancesco Ruggeri 		if (in_netdev) {
564fac6fce9SFrancesco Ruggeri 			ipv6_dev_get_saddr(net, in_netdev, &fl6.daddr,
565fac6fce9SFrancesco Ruggeri 					   inet6_sk(sk)->srcprefs,
566fac6fce9SFrancesco Ruggeri 					   &fl6.saddr);
567fac6fce9SFrancesco Ruggeri 			dev_put(in_netdev);
568fac6fce9SFrancesco Ruggeri 		}
569fac6fce9SFrancesco Ruggeri 	}
570e110861fSLorenzo Colitti 	fl6.flowi6_mark = mark;
5714c9483b2SDavid S. Miller 	fl6.flowi6_oif = iif;
5721958b856SDavid S. Miller 	fl6.fl6_icmp_type = type;
5731958b856SDavid S. Miller 	fl6.fl6_icmp_code = code;
574e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
575b4bac172SDavid Ahern 	fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, NULL);
5763df98d79SPaul Moore 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
5771da177e4SLinus Torvalds 
578fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
579405666dbSDenis V. Lunev 
5804c9483b2SDavid S. Miller 	if (!icmpv6_xrlim_allow(sk, type, &fl6))
5811da177e4SLinus Torvalds 		goto out;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = type;
5841da177e4SLinus Torvalds 	tmp_hdr.icmp6_code = code;
5851da177e4SLinus Torvalds 	tmp_hdr.icmp6_cksum = 0;
5861da177e4SLinus Torvalds 	tmp_hdr.icmp6_pointer = htonl(info);
5871da177e4SLinus Torvalds 
5884c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
5894c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
590c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
591c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
5921da177e4SLinus Torvalds 
593b515430aSWillem de Bruijn 	ipcm6_init_sk(&ipc6, np);
5940da7536fSWillem de Bruijn 	ipc6.sockc.mark = mark;
59538b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
59638b7097bSHannes Frederic Sowa 
5974c9483b2SDavid S. Miller 	dst = icmpv6_route_lookup(net, skb, sk, &fl6);
598b42835dbSDavid S. Miller 	if (IS_ERR(dst))
5991da177e4SLinus Torvalds 		goto out;
6008de3351eSYOSHIFUJI Hideaki 
60126879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	msg.skb = skb;
604bbe735e4SArnaldo Carvalho de Melo 	msg.offset = skb_network_offset(skb);
605763ecff1SYasuyuki Kozakai 	msg.type = type;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	len = skb->len - msg.offset;
6081da177e4SLinus Torvalds 	len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr));
6091da177e4SLinus Torvalds 	if (len < 0) {
6104b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n",
6114b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
6121da177e4SLinus Torvalds 		goto out_dst_release;
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 
615cfdf7647SEric Dumazet 	rcu_read_lock();
616cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
6171da177e4SLinus Torvalds 
6184e64b1edSJoe Perches 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
6191da177e4SLinus Torvalds 			    len + sizeof(struct icmp6hdr),
62026879da5SWei Wang 			    sizeof(struct icmp6hdr),
62126879da5SWei Wang 			    &ipc6, &fl6, (struct rt6_info *)dst,
6225fdaa88dSWillem de Bruijn 			    MSG_DONTWAIT)) {
62343a43b60SHannes Frederic Sowa 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
6241da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
625cfdf7647SEric Dumazet 	} else {
6264e64b1edSJoe Perches 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
627cfdf7647SEric Dumazet 					   len + sizeof(struct icmp6hdr));
6281da177e4SLinus Torvalds 	}
629cfdf7647SEric Dumazet 	rcu_read_unlock();
6301da177e4SLinus Torvalds out_dst_release:
6311da177e4SLinus Torvalds 	dst_release(dst);
6321da177e4SLinus Torvalds out:
633405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
6347ba91ecbSJesper Dangaard Brouer out_bh_enable:
6357ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
6361da177e4SLinus Torvalds }
637cc7a21b6SEric Dumazet EXPORT_SYMBOL(icmp6_send);
6385f5624cfSPravin B Shelar 
6395f5624cfSPravin B Shelar /* Slightly more convenient version of icmp6_send.
6405f5624cfSPravin B Shelar  */
6415f5624cfSPravin B Shelar void icmpv6_param_prob(struct sk_buff *skb, u8 code, int pos)
6425f5624cfSPravin B Shelar {
643ee576c47SJason A. Donenfeld 	icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL, IP6CB(skb));
6445f5624cfSPravin B Shelar 	kfree_skb(skb);
6455f5624cfSPravin B Shelar }
6467159039aSYOSHIFUJI Hideaki 
6475fbba8acSEric Dumazet /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
6485fbba8acSEric Dumazet  * if sufficient data bytes are available
6495fbba8acSEric Dumazet  * @nhs is the size of the tunnel header(s) :
6505fbba8acSEric Dumazet  *  Either an IPv4 header for SIT encap
6515fbba8acSEric Dumazet  *         an IPv4 header + GRE header for GRE encap
6525fbba8acSEric Dumazet  */
65320e1954fSEric Dumazet int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type,
65420e1954fSEric Dumazet 			       unsigned int data_len)
6555fbba8acSEric Dumazet {
6562d7a3b27SEric Dumazet 	struct in6_addr temp_saddr;
6575fbba8acSEric Dumazet 	struct rt6_info *rt;
6585fbba8acSEric Dumazet 	struct sk_buff *skb2;
65920e1954fSEric Dumazet 	u32 info = 0;
6605fbba8acSEric Dumazet 
6615fbba8acSEric Dumazet 	if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8))
6625fbba8acSEric Dumazet 		return 1;
6635fbba8acSEric Dumazet 
66420e1954fSEric Dumazet 	/* RFC 4884 (partial) support for ICMP extensions */
66520e1954fSEric Dumazet 	if (data_len < 128 || (data_len & 7) || skb->len < data_len)
66620e1954fSEric Dumazet 		data_len = 0;
66720e1954fSEric Dumazet 
66820e1954fSEric Dumazet 	skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC);
6695fbba8acSEric Dumazet 
6705fbba8acSEric Dumazet 	if (!skb2)
6715fbba8acSEric Dumazet 		return 1;
6725fbba8acSEric Dumazet 
6735fbba8acSEric Dumazet 	skb_dst_drop(skb2);
6745fbba8acSEric Dumazet 	skb_pull(skb2, nhs);
6755fbba8acSEric Dumazet 	skb_reset_network_header(skb2);
6765fbba8acSEric Dumazet 
677b75cc8f9SDavid Ahern 	rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0,
678b75cc8f9SDavid Ahern 			skb, 0);
6795fbba8acSEric Dumazet 
6805fbba8acSEric Dumazet 	if (rt && rt->dst.dev)
6815fbba8acSEric Dumazet 		skb2->dev = rt->dst.dev;
6825fbba8acSEric Dumazet 
6832d7a3b27SEric Dumazet 	ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr);
68420e1954fSEric Dumazet 
68520e1954fSEric Dumazet 	if (data_len) {
68620e1954fSEric Dumazet 		/* RFC 4884 (partial) support :
68720e1954fSEric Dumazet 		 * insert 0 padding at the end, before the extensions
68820e1954fSEric Dumazet 		 */
68920e1954fSEric Dumazet 		__skb_push(skb2, nhs);
69020e1954fSEric Dumazet 		skb_reset_network_header(skb2);
69120e1954fSEric Dumazet 		memmove(skb2->data, skb2->data + nhs, data_len - nhs);
69220e1954fSEric Dumazet 		memset(skb2->data + data_len - nhs, 0, nhs);
69320e1954fSEric Dumazet 		/* RFC 4884 4.5 : Length is measured in 64-bit words,
69420e1954fSEric Dumazet 		 * and stored in reserved[0]
69520e1954fSEric Dumazet 		 */
69620e1954fSEric Dumazet 		info = (data_len/8) << 24;
69720e1954fSEric Dumazet 	}
6982d7a3b27SEric Dumazet 	if (type == ICMP_TIME_EXCEEDED)
6992d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
700ee576c47SJason A. Donenfeld 			   info, &temp_saddr, IP6CB(skb2));
7012d7a3b27SEric Dumazet 	else
7022d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH,
703ee576c47SJason A. Donenfeld 			   info, &temp_saddr, IP6CB(skb2));
7045fbba8acSEric Dumazet 	if (rt)
7055fbba8acSEric Dumazet 		ip6_rt_put(rt);
7065fbba8acSEric Dumazet 
7075fbba8acSEric Dumazet 	kfree_skb(skb2);
7085fbba8acSEric Dumazet 
7095fbba8acSEric Dumazet 	return 0;
7105fbba8acSEric Dumazet }
7115fbba8acSEric Dumazet EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach);
7125fbba8acSEric Dumazet 
7131da177e4SLinus Torvalds static void icmpv6_echo_reply(struct sk_buff *skb)
7141da177e4SLinus Torvalds {
715c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
71684427d53SYOSHIFUJI Hideaki 	struct sock *sk;
7171da177e4SLinus Torvalds 	struct inet6_dev *idev;
71884427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
719b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
720cc70ab26SArnaldo Carvalho de Melo 	struct icmp6hdr *icmph = icmp6_hdr(skb);
7211da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
7224c9483b2SDavid S. Miller 	struct flowi6 fl6;
7231da177e4SLinus Torvalds 	struct icmpv6_msg msg;
7241da177e4SLinus Torvalds 	struct dst_entry *dst;
72526879da5SWei Wang 	struct ipcm6_cookie ipc6;
726e110861fSLorenzo Colitti 	u32 mark = IP6_REPLY_MARK(net, skb->mark);
7270b03a5caSStephen Suryaputra 	bool acast;
728*1fd07f33SAndreas Roeseler 	u8 type;
7291da177e4SLinus Torvalds 
73003f1ecccSStephen Suryaputra 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
73103f1ecccSStephen Suryaputra 	    net->ipv6.sysctl.icmpv6_echo_ignore_multicast)
73203f1ecccSStephen Suryaputra 		return;
73303f1ecccSStephen Suryaputra 
7340660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->daddr;
7351da177e4SLinus Torvalds 
7360b03a5caSStephen Suryaputra 	acast = ipv6_anycast_destination(skb_dst(skb), saddr);
7370b03a5caSStephen Suryaputra 	if (acast && net->ipv6.sysctl.icmpv6_echo_ignore_anycast)
7380b03a5caSStephen Suryaputra 		return;
7390b03a5caSStephen Suryaputra 
740509aba3bSFX Le Bail 	if (!ipv6_unicast_destination(skb) &&
7410b03a5caSStephen Suryaputra 	    !(net->ipv6.sysctl.anycast_src_echo_reply && acast))
7421da177e4SLinus Torvalds 		saddr = NULL;
7431da177e4SLinus Torvalds 
744*1fd07f33SAndreas Roeseler 	if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
745*1fd07f33SAndreas Roeseler 		type = ICMPV6_EXT_ECHO_REPLY;
746*1fd07f33SAndreas Roeseler 	else
747*1fd07f33SAndreas Roeseler 		type = ICMPV6_ECHO_REPLY;
748*1fd07f33SAndreas Roeseler 
7491da177e4SLinus Torvalds 	memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
750*1fd07f33SAndreas Roeseler 	tmp_hdr.icmp6_type = type;
7511da177e4SLinus Torvalds 
7524c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
753a346abe0SEric Dumazet 	if (net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES)
754a346abe0SEric Dumazet 		fl6.flowlabel = ip6_flowlabel(ipv6_hdr(skb));
755a346abe0SEric Dumazet 
7564c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
7574e3fd7a0SAlexey Dobriyan 	fl6.daddr = ipv6_hdr(skb)->saddr;
7581da177e4SLinus Torvalds 	if (saddr)
7594e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
7601b70d792SDavid Ahern 	fl6.flowi6_oif = icmp6_iif(skb);
761*1fd07f33SAndreas Roeseler 	fl6.fl6_icmp_type = type;
762e110861fSLorenzo Colitti 	fl6.flowi6_mark = mark;
763e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
7643df98d79SPaul Moore 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
7651da177e4SLinus Torvalds 
7667ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
767fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
76863159f29SIan Morris 	if (!sk)
7697ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
770fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
771405666dbSDenis V. Lunev 
7724c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
7734c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
774c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
775c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
7761da177e4SLinus Torvalds 
7774e64b1edSJoe Perches 	if (ip6_dst_lookup(net, sk, &dst, &fl6))
7781da177e4SLinus Torvalds 		goto out;
7794c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
780452edd59SDavid S. Miller 	if (IS_ERR(dst))
781e104411bSPatrick McHardy 		goto out;
7821da177e4SLinus Torvalds 
7830bc19985SStephen Suryaputra 	/* Check the ratelimit */
7840bc19985SStephen Suryaputra 	if ((!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, ICMPV6_ECHO_REPLY)) ||
7850bc19985SStephen Suryaputra 	    !icmpv6_xrlim_allow(sk, ICMPV6_ECHO_REPLY, &fl6))
7860bc19985SStephen Suryaputra 		goto out_dst_release;
7870bc19985SStephen Suryaputra 
788cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	msg.skb = skb;
7911da177e4SLinus Torvalds 	msg.offset = 0;
792*1fd07f33SAndreas Roeseler 	msg.type = type;
7931da177e4SLinus Torvalds 
794b515430aSWillem de Bruijn 	ipcm6_init_sk(&ipc6, np);
79526879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
79626879da5SWei Wang 	ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb));
7970da7536fSWillem de Bruijn 	ipc6.sockc.mark = mark;
79826879da5SWei Wang 
799*1fd07f33SAndreas Roeseler 	if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
800*1fd07f33SAndreas Roeseler 		if (!icmp_build_probe(skb, (struct icmphdr *)&tmp_hdr))
801*1fd07f33SAndreas Roeseler 			goto out_dst_release;
802*1fd07f33SAndreas Roeseler 
8034e64b1edSJoe Perches 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
8044e64b1edSJoe Perches 			    skb->len + sizeof(struct icmp6hdr),
80526879da5SWei Wang 			    sizeof(struct icmp6hdr), &ipc6, &fl6,
8065fdaa88dSWillem de Bruijn 			    (struct rt6_info *)dst, MSG_DONTWAIT)) {
807a16292a0SEric Dumazet 		__ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
8081da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
809cfdf7647SEric Dumazet 	} else {
8104e64b1edSJoe Perches 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
811cfdf7647SEric Dumazet 					   skb->len + sizeof(struct icmp6hdr));
8121da177e4SLinus Torvalds 	}
8130bc19985SStephen Suryaputra out_dst_release:
8141da177e4SLinus Torvalds 	dst_release(dst);
8151da177e4SLinus Torvalds out:
816405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
8177ba91ecbSJesper Dangaard Brouer out_bh_enable:
8187ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
821b94f1c09SDavid S. Miller void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
8221da177e4SLinus Torvalds {
82341135cc8SAlexey Dobriyan 	const struct inet6_protocol *ipprot;
8241da177e4SLinus Torvalds 	int inner_offset;
82575f2811cSJesse Gross 	__be16 frag_off;
826f9242b6bSDavid S. Miller 	u8 nexthdr;
8277304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
8307304fe46SDuan Jiong 		goto out;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
8331da177e4SLinus Torvalds 	if (ipv6_ext_hdr(nexthdr)) {
8341da177e4SLinus Torvalds 		/* now skip over extension headers */
83575f2811cSJesse Gross 		inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
83675f2811cSJesse Gross 						&nexthdr, &frag_off);
8371da177e4SLinus Torvalds 		if (inner_offset < 0)
8387304fe46SDuan Jiong 			goto out;
8391da177e4SLinus Torvalds 	} else {
8401da177e4SLinus Torvalds 		inner_offset = sizeof(struct ipv6hdr);
8411da177e4SLinus Torvalds 	}
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	/* Checkin header including 8 bytes of inner protocol header. */
8441da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, inner_offset+8))
8457304fe46SDuan Jiong 		goto out;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
8481da177e4SLinus Torvalds 	   Without this we will not able f.e. to make source routed
8491da177e4SLinus Torvalds 	   pmtu discovery.
8501da177e4SLinus Torvalds 	   Corresponding argument (opt) to notifiers is already added.
8511da177e4SLinus Torvalds 	   --ANK (980726)
8521da177e4SLinus Torvalds 	 */
8531da177e4SLinus Torvalds 
854f9242b6bSDavid S. Miller 	ipprot = rcu_dereference(inet6_protos[nexthdr]);
8551da177e4SLinus Torvalds 	if (ipprot && ipprot->err_handler)
8561da177e4SLinus Torvalds 		ipprot->err_handler(skb, NULL, type, code, inner_offset, info);
8571da177e4SLinus Torvalds 
85869d6da0bSPavel Emelyanov 	raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
8597304fe46SDuan Jiong 	return;
8607304fe46SDuan Jiong 
8617304fe46SDuan Jiong out:
862a16292a0SEric Dumazet 	__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds /*
8661da177e4SLinus Torvalds  *	Handle icmp messages
8671da177e4SLinus Torvalds  */
8681da177e4SLinus Torvalds 
869e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb)
8701da177e4SLinus Torvalds {
871e6f86b0fSVirgile Jarry 	struct net *net = dev_net(skb->dev);
872e1ae5c2eSStephen Suryaputra 	struct net_device *dev = icmp6_dev(skb);
8731da177e4SLinus Torvalds 	struct inet6_dev *idev = __in6_dev_get(dev);
874b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
8751da177e4SLinus Torvalds 	struct icmp6hdr *hdr;
876d5fdd6baSBrian Haley 	u8 type;
877e3e32170SRick Jones 	bool success = false;
8781da177e4SLinus Torvalds 
879aebcf82cSHerbert Xu 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
880def8b4faSAlexey Dobriyan 		struct sec_path *sp = skb_sec_path(skb);
8818b7817f3SHerbert Xu 		int nh;
8828b7817f3SHerbert Xu 
883def8b4faSAlexey Dobriyan 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
884aebcf82cSHerbert Xu 				 XFRM_STATE_ICMP))
885aebcf82cSHerbert Xu 			goto drop_no_count;
886aebcf82cSHerbert Xu 
88781aded24SDavid S. Miller 		if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
8888b7817f3SHerbert Xu 			goto drop_no_count;
8898b7817f3SHerbert Xu 
8908b7817f3SHerbert Xu 		nh = skb_network_offset(skb);
8918b7817f3SHerbert Xu 		skb_set_network_header(skb, sizeof(*hdr));
8928b7817f3SHerbert Xu 
8938b7817f3SHerbert Xu 		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
8948b7817f3SHerbert Xu 			goto drop_no_count;
8958b7817f3SHerbert Xu 
8968b7817f3SHerbert Xu 		skb_set_network_header(skb, nh);
8978b7817f3SHerbert Xu 	}
8988b7817f3SHerbert Xu 
899a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INMSGS);
9001da177e4SLinus Torvalds 
9010660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
9020660e03fSArnaldo Carvalho de Melo 	daddr = &ipv6_hdr(skb)->daddr;
9031da177e4SLinus Torvalds 
90439471ac8STom Herbert 	if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) {
905ba7a46f1SJoe Perches 		net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n",
9060c6ce78aSHarvey Harrison 				    saddr, daddr);
9076a5dc9e5SEric Dumazet 		goto csum_error;
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 
9108cf22943SHerbert Xu 	if (!pskb_pull(skb, sizeof(*hdr)))
9118cf22943SHerbert Xu 		goto discard_it;
9121da177e4SLinus Torvalds 
913cc70ab26SArnaldo Carvalho de Melo 	hdr = icmp6_hdr(skb);
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	type = hdr->icmp6_type;
9161da177e4SLinus Torvalds 
917f3832ed2SEric Dumazet 	ICMP6MSGIN_INC_STATS(dev_net(dev), idev, type);
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	switch (type) {
9201da177e4SLinus Torvalds 	case ICMPV6_ECHO_REQUEST:
921e6f86b0fSVirgile Jarry 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all)
9221da177e4SLinus Torvalds 			icmpv6_echo_reply(skb);
9231da177e4SLinus Torvalds 		break;
924*1fd07f33SAndreas Roeseler 	case ICMPV6_EXT_ECHO_REQUEST:
925*1fd07f33SAndreas Roeseler 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all &&
926*1fd07f33SAndreas Roeseler 		    net->ipv4.sysctl_icmp_echo_enable_probe)
927*1fd07f33SAndreas Roeseler 			icmpv6_echo_reply(skb);
928*1fd07f33SAndreas Roeseler 		break;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	case ICMPV6_ECHO_REPLY:
931e3e32170SRick Jones 		success = ping_rcv(skb);
9321da177e4SLinus Torvalds 		break;
9331da177e4SLinus Torvalds 
93431433202SAndreas Roeseler 	case ICMPV6_EXT_ECHO_REPLY:
93531433202SAndreas Roeseler 		success = ping_rcv(skb);
93631433202SAndreas Roeseler 		break;
93731433202SAndreas Roeseler 
9381da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
9391da177e4SLinus Torvalds 		/* BUGGG_FUTURE: if packet contains rthdr, we cannot update
9401da177e4SLinus Torvalds 		   standard destination cache. Seems, only "advanced"
9411da177e4SLinus Torvalds 		   destination cache will allow to solve this problem
9421da177e4SLinus Torvalds 		   --ANK (980726)
9431da177e4SLinus Torvalds 		 */
9441da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
9451da177e4SLinus Torvalds 			goto discard_it;
946cc70ab26SArnaldo Carvalho de Melo 		hdr = icmp6_hdr(skb);
9471da177e4SLinus Torvalds 
948275757e6SGustavo A. R. Silva 		/* to notify */
949a8eceea8SJoe Perches 		fallthrough;
9501da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
9511da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
9521da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
9531da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
9541da177e4SLinus Torvalds 		break;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	case NDISC_ROUTER_SOLICITATION:
9571da177e4SLinus Torvalds 	case NDISC_ROUTER_ADVERTISEMENT:
9581da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_SOLICITATION:
9591da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_ADVERTISEMENT:
9601da177e4SLinus Torvalds 	case NDISC_REDIRECT:
9611da177e4SLinus Torvalds 		ndisc_rcv(skb);
9621da177e4SLinus Torvalds 		break;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	case ICMPV6_MGM_QUERY:
9651da177e4SLinus Torvalds 		igmp6_event_query(skb);
966f185de28STaehee Yoo 		return 0;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	case ICMPV6_MGM_REPORT:
9691da177e4SLinus Torvalds 		igmp6_event_report(skb);
970f185de28STaehee Yoo 		return 0;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	case ICMPV6_MGM_REDUCTION:
9731da177e4SLinus Torvalds 	case ICMPV6_NI_QUERY:
9741da177e4SLinus Torvalds 	case ICMPV6_NI_REPLY:
9751da177e4SLinus Torvalds 	case ICMPV6_MLD2_REPORT:
9761da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REQUEST:
9771da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REPLY:
9781da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_SOL:
9791da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_ADV:
9801da177e4SLinus Torvalds 		break;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	default:
9831da177e4SLinus Torvalds 		/* informational */
9841da177e4SLinus Torvalds 		if (type & ICMPV6_INFOMSG_MASK)
9851da177e4SLinus Torvalds 			break;
9861da177e4SLinus Torvalds 
9874b3418fbSBjørn Mork 		net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n",
9884b3418fbSBjørn Mork 				    saddr, daddr);
989ea85a0a2SDavid S. Miller 
9901da177e4SLinus Torvalds 		/*
9911da177e4SLinus Torvalds 		 * error of unknown type.
9921da177e4SLinus Torvalds 		 * must pass to upper level
9931da177e4SLinus Torvalds 		 */
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
9963ff50b79SStephen Hemminger 	}
9973ff50b79SStephen Hemminger 
998e3e32170SRick Jones 	/* until the v6 path can be better sorted assume failure and
999e3e32170SRick Jones 	 * preserve the status quo behaviour for the rest of the paths to here
1000e3e32170SRick Jones 	 */
1001e3e32170SRick Jones 	if (success)
1002e3e32170SRick Jones 		consume_skb(skb);
1003e3e32170SRick Jones 	else
10041da177e4SLinus Torvalds 		kfree_skb(skb);
1005e3e32170SRick Jones 
10061da177e4SLinus Torvalds 	return 0;
10071da177e4SLinus Torvalds 
10086a5dc9e5SEric Dumazet csum_error:
1009a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS);
10101da177e4SLinus Torvalds discard_it:
1011a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INERRORS);
10128b7817f3SHerbert Xu drop_no_count:
10131da177e4SLinus Torvalds 	kfree_skb(skb);
10141da177e4SLinus Torvalds 	return 0;
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
10174c9483b2SDavid S. Miller void icmpv6_flow_init(struct sock *sk, struct flowi6 *fl6,
101895e41e93SYOSHIFUJI Hideaki 		      u8 type,
101995e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *saddr,
102095e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *daddr,
102195e41e93SYOSHIFUJI Hideaki 		      int oif)
102295e41e93SYOSHIFUJI Hideaki {
10234c9483b2SDavid S. Miller 	memset(fl6, 0, sizeof(*fl6));
10244e3fd7a0SAlexey Dobriyan 	fl6->saddr = *saddr;
10254e3fd7a0SAlexey Dobriyan 	fl6->daddr = *daddr;
10264c9483b2SDavid S. Miller 	fl6->flowi6_proto	= IPPROTO_ICMPV6;
10271958b856SDavid S. Miller 	fl6->fl6_icmp_type	= type;
10281958b856SDavid S. Miller 	fl6->fl6_icmp_code	= 0;
10294c9483b2SDavid S. Miller 	fl6->flowi6_oif		= oif;
10303df98d79SPaul Moore 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
103195e41e93SYOSHIFUJI Hideaki }
103295e41e93SYOSHIFUJI Hideaki 
10333232a1efSKefeng Wang static void __net_exit icmpv6_sk_exit(struct net *net)
10343232a1efSKefeng Wang {
10353232a1efSKefeng Wang 	int i;
10363232a1efSKefeng Wang 
10373232a1efSKefeng Wang 	for_each_possible_cpu(i)
103875efc250SKefeng Wang 		inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv6.icmp_sk, i));
103975efc250SKefeng Wang 	free_percpu(net->ipv6.icmp_sk);
10403232a1efSKefeng Wang }
10413232a1efSKefeng Wang 
104298c6d1b2SDenis V. Lunev static int __net_init icmpv6_sk_init(struct net *net)
10431da177e4SLinus Torvalds {
10441da177e4SLinus Torvalds 	struct sock *sk;
10453232a1efSKefeng Wang 	int err, i;
10461da177e4SLinus Torvalds 
104775efc250SKefeng Wang 	net->ipv6.icmp_sk = alloc_percpu(struct sock *);
104863159f29SIan Morris 	if (!net->ipv6.icmp_sk)
104979c91159SDenis V. Lunev 		return -ENOMEM;
105079c91159SDenis V. Lunev 
10516f912042SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
10521ed8516fSDenis V. Lunev 		err = inet_ctl_sock_create(&sk, PF_INET6,
10531ed8516fSDenis V. Lunev 					   SOCK_RAW, IPPROTO_ICMPV6, net);
10541da177e4SLinus Torvalds 		if (err < 0) {
1055f3213831SJoe Perches 			pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
10561da177e4SLinus Torvalds 			       err);
10571da177e4SLinus Torvalds 			goto fail;
10581da177e4SLinus Torvalds 		}
10591da177e4SLinus Torvalds 
106075efc250SKefeng Wang 		*per_cpu_ptr(net->ipv6.icmp_sk, i) = sk;
10615c8cafd6SDenis V. Lunev 
10621da177e4SLinus Torvalds 		/* Enough space for 2 64K ICMP packets, including
10631da177e4SLinus Torvalds 		 * sk_buff struct overhead.
10641da177e4SLinus Torvalds 		 */
106587fb4b7bSEric Dumazet 		sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 	return 0;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds  fail:
10703232a1efSKefeng Wang 	icmpv6_sk_exit(net);
10711da177e4SLinus Torvalds 	return err;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10748ed7edceSAlexey Dobriyan static struct pernet_operations icmpv6_sk_ops = {
107598c6d1b2SDenis V. Lunev 	.init = icmpv6_sk_init,
107698c6d1b2SDenis V. Lunev 	.exit = icmpv6_sk_exit,
107798c6d1b2SDenis V. Lunev };
107898c6d1b2SDenis V. Lunev 
107998c6d1b2SDenis V. Lunev int __init icmpv6_init(void)
108098c6d1b2SDenis V. Lunev {
108198c6d1b2SDenis V. Lunev 	int err;
108298c6d1b2SDenis V. Lunev 
108398c6d1b2SDenis V. Lunev 	err = register_pernet_subsys(&icmpv6_sk_ops);
108498c6d1b2SDenis V. Lunev 	if (err < 0)
108598c6d1b2SDenis V. Lunev 		return err;
108698c6d1b2SDenis V. Lunev 
108798c6d1b2SDenis V. Lunev 	err = -EAGAIN;
108898c6d1b2SDenis V. Lunev 	if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
108998c6d1b2SDenis V. Lunev 		goto fail;
10905f5624cfSPravin B Shelar 
10915f5624cfSPravin B Shelar 	err = inet6_register_icmp_sender(icmp6_send);
10925f5624cfSPravin B Shelar 	if (err)
10935f5624cfSPravin B Shelar 		goto sender_reg_err;
109498c6d1b2SDenis V. Lunev 	return 0;
109598c6d1b2SDenis V. Lunev 
10965f5624cfSPravin B Shelar sender_reg_err:
10975f5624cfSPravin B Shelar 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
109898c6d1b2SDenis V. Lunev fail:
1099f3213831SJoe Perches 	pr_err("Failed to register ICMP6 protocol\n");
110098c6d1b2SDenis V. Lunev 	unregister_pernet_subsys(&icmpv6_sk_ops);
110198c6d1b2SDenis V. Lunev 	return err;
110298c6d1b2SDenis V. Lunev }
110398c6d1b2SDenis V. Lunev 
11048ed7edceSAlexey Dobriyan void icmpv6_cleanup(void)
110598c6d1b2SDenis V. Lunev {
11065f5624cfSPravin B Shelar 	inet6_unregister_icmp_sender(icmp6_send);
110798c6d1b2SDenis V. Lunev 	unregister_pernet_subsys(&icmpv6_sk_ops);
11081da177e4SLinus Torvalds 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
111198c6d1b2SDenis V. Lunev 
11129b5b5cffSArjan van de Ven static const struct icmp6_err {
11131da177e4SLinus Torvalds 	int err;
11141da177e4SLinus Torvalds 	int fatal;
11151da177e4SLinus Torvalds } tab_unreach[] = {
11161da177e4SLinus Torvalds 	{	/* NOROUTE */
11171da177e4SLinus Torvalds 		.err	= ENETUNREACH,
11181da177e4SLinus Torvalds 		.fatal	= 0,
11191da177e4SLinus Torvalds 	},
11201da177e4SLinus Torvalds 	{	/* ADM_PROHIBITED */
11211da177e4SLinus Torvalds 		.err	= EACCES,
11221da177e4SLinus Torvalds 		.fatal	= 1,
11231da177e4SLinus Torvalds 	},
11241da177e4SLinus Torvalds 	{	/* Was NOT_NEIGHBOUR, now reserved */
11251da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
11261da177e4SLinus Torvalds 		.fatal	= 0,
11271da177e4SLinus Torvalds 	},
11281da177e4SLinus Torvalds 	{	/* ADDR_UNREACH	*/
11291da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
11301da177e4SLinus Torvalds 		.fatal	= 0,
11311da177e4SLinus Torvalds 	},
11321da177e4SLinus Torvalds 	{	/* PORT_UNREACH	*/
11331da177e4SLinus Torvalds 		.err	= ECONNREFUSED,
11341da177e4SLinus Torvalds 		.fatal	= 1,
11351da177e4SLinus Torvalds 	},
113661e76b17SJiri Bohac 	{	/* POLICY_FAIL */
113761e76b17SJiri Bohac 		.err	= EACCES,
113861e76b17SJiri Bohac 		.fatal	= 1,
113961e76b17SJiri Bohac 	},
114061e76b17SJiri Bohac 	{	/* REJECT_ROUTE	*/
114161e76b17SJiri Bohac 		.err	= EACCES,
114261e76b17SJiri Bohac 		.fatal	= 1,
114361e76b17SJiri Bohac 	},
11441da177e4SLinus Torvalds };
11451da177e4SLinus Torvalds 
1146d5fdd6baSBrian Haley int icmpv6_err_convert(u8 type, u8 code, int *err)
11471da177e4SLinus Torvalds {
11481da177e4SLinus Torvalds 	int fatal = 0;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	*err = EPROTO;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	switch (type) {
11531da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
11541da177e4SLinus Torvalds 		fatal = 1;
115561e76b17SJiri Bohac 		if (code < ARRAY_SIZE(tab_unreach)) {
11561da177e4SLinus Torvalds 			*err  = tab_unreach[code].err;
11571da177e4SLinus Torvalds 			fatal = tab_unreach[code].fatal;
11581da177e4SLinus Torvalds 		}
11591da177e4SLinus Torvalds 		break;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
11621da177e4SLinus Torvalds 		*err = EMSGSIZE;
11631da177e4SLinus Torvalds 		break;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
11661da177e4SLinus Torvalds 		*err = EPROTO;
11671da177e4SLinus Torvalds 		fatal = 1;
11681da177e4SLinus Torvalds 		break;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
11711da177e4SLinus Torvalds 		*err = EHOSTUNREACH;
11721da177e4SLinus Torvalds 		break;
11733ff50b79SStephen Hemminger 	}
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	return fatal;
11761da177e4SLinus Torvalds }
11777159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert);
11787159039aSYOSHIFUJI Hideaki 
11791da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1180e8243534Sstephen hemminger static struct ctl_table ipv6_icmp_table_template[] = {
11811da177e4SLinus Torvalds 	{
11821da177e4SLinus Torvalds 		.procname	= "ratelimit",
118341a76906SDaniel Lezcano 		.data		= &init_net.ipv6.sysctl.icmpv6_time,
11841da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
11851da177e4SLinus Torvalds 		.mode		= 0644,
11866d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
11871da177e4SLinus Torvalds 	},
1188e6f86b0fSVirgile Jarry 	{
1189e6f86b0fSVirgile Jarry 		.procname	= "echo_ignore_all",
1190e6f86b0fSVirgile Jarry 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_all,
1191a6175633SEric Dumazet 		.maxlen		= sizeof(u8),
1192e6f86b0fSVirgile Jarry 		.mode		= 0644,
1193a6175633SEric Dumazet 		.proc_handler = proc_dou8vec_minmax,
1194e6f86b0fSVirgile Jarry 	},
119503f1ecccSStephen Suryaputra 	{
119603f1ecccSStephen Suryaputra 		.procname	= "echo_ignore_multicast",
119703f1ecccSStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_multicast,
1198a6175633SEric Dumazet 		.maxlen		= sizeof(u8),
119903f1ecccSStephen Suryaputra 		.mode		= 0644,
1200a6175633SEric Dumazet 		.proc_handler = proc_dou8vec_minmax,
120103f1ecccSStephen Suryaputra 	},
12020b03a5caSStephen Suryaputra 	{
12030b03a5caSStephen Suryaputra 		.procname	= "echo_ignore_anycast",
12040b03a5caSStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_anycast,
1205a6175633SEric Dumazet 		.maxlen		= sizeof(u8),
12060b03a5caSStephen Suryaputra 		.mode		= 0644,
1207a6175633SEric Dumazet 		.proc_handler = proc_dou8vec_minmax,
12080b03a5caSStephen Suryaputra 	},
12090bc19985SStephen Suryaputra 	{
12100bc19985SStephen Suryaputra 		.procname	= "ratemask",
12110bc19985SStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_ratemask_ptr,
12120bc19985SStephen Suryaputra 		.maxlen		= ICMPV6_MSG_MAX + 1,
12130bc19985SStephen Suryaputra 		.mode		= 0644,
12140bc19985SStephen Suryaputra 		.proc_handler = proc_do_large_bitmap,
12150bc19985SStephen Suryaputra 	},
1216f8572d8fSEric W. Biederman 	{ },
12171da177e4SLinus Torvalds };
1218760f2d01SDaniel Lezcano 
12192c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
1220760f2d01SDaniel Lezcano {
1221760f2d01SDaniel Lezcano 	struct ctl_table *table;
1222760f2d01SDaniel Lezcano 
1223760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_icmp_table_template,
1224760f2d01SDaniel Lezcano 			sizeof(ipv6_icmp_table_template),
1225760f2d01SDaniel Lezcano 			GFP_KERNEL);
12265ee09105SYOSHIFUJI Hideaki 
1227e6f86b0fSVirgile Jarry 	if (table) {
12285ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.icmpv6_time;
1229e6f86b0fSVirgile Jarry 		table[1].data = &net->ipv6.sysctl.icmpv6_echo_ignore_all;
123003f1ecccSStephen Suryaputra 		table[2].data = &net->ipv6.sysctl.icmpv6_echo_ignore_multicast;
12310b03a5caSStephen Suryaputra 		table[3].data = &net->ipv6.sysctl.icmpv6_echo_ignore_anycast;
12320bc19985SStephen Suryaputra 		table[4].data = &net->ipv6.sysctl.icmpv6_ratemask_ptr;
1233e6f86b0fSVirgile Jarry 	}
1234760f2d01SDaniel Lezcano 	return table;
1235760f2d01SDaniel Lezcano }
12361da177e4SLinus Torvalds #endif
1237