xref: /openbmc/linux/net/ipv6/icmp.c (revision b75cc8f9)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Internet Control Message Protocol (ICMPv6)
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	Based on net/ipv4/icmp.c
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	RFC 1885
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
131da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
141da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
151da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /*
191da177e4SLinus Torvalds  *	Changes:
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *	Andi Kleen		:	exception handling
221da177e4SLinus Torvalds  *	Andi Kleen			add rate limits. never reply to a icmp.
231da177e4SLinus Torvalds  *					add more length checks and other fixes.
241da177e4SLinus Torvalds  *	yoshfuji		:	ensure to sent parameter problem for
251da177e4SLinus Torvalds  *					fragments.
261da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI:	added sysctl for icmp rate limit.
271da177e4SLinus Torvalds  *	Randy Dunlap and
281da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI:	Per-interface statistics support
291da177e4SLinus Torvalds  *	Kazunori MIYAZAWA @USAGI:       change output process to use ip6_append_data
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
32f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
33f3213831SJoe Perches 
341da177e4SLinus Torvalds #include <linux/module.h>
351da177e4SLinus Torvalds #include <linux/errno.h>
361da177e4SLinus Torvalds #include <linux/types.h>
371da177e4SLinus Torvalds #include <linux/socket.h>
381da177e4SLinus Torvalds #include <linux/in.h>
391da177e4SLinus Torvalds #include <linux/kernel.h>
401da177e4SLinus Torvalds #include <linux/sockios.h>
411da177e4SLinus Torvalds #include <linux/net.h>
421da177e4SLinus Torvalds #include <linux/skbuff.h>
431da177e4SLinus Torvalds #include <linux/init.h>
44763ecff1SYasuyuki Kozakai #include <linux/netfilter.h>
455a0e3ad6STejun Heo #include <linux/slab.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
481da177e4SLinus Torvalds #include <linux/sysctl.h>
491da177e4SLinus Torvalds #endif
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include <linux/inet.h>
521da177e4SLinus Torvalds #include <linux/netdevice.h>
531da177e4SLinus Torvalds #include <linux/icmpv6.h>
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #include <net/ip.h>
561da177e4SLinus Torvalds #include <net/sock.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include <net/ipv6.h>
591da177e4SLinus Torvalds #include <net/ip6_checksum.h>
606d0bfe22SLorenzo Colitti #include <net/ping.h>
611da177e4SLinus Torvalds #include <net/protocol.h>
621da177e4SLinus Torvalds #include <net/raw.h>
631da177e4SLinus Torvalds #include <net/rawv6.h>
641da177e4SLinus Torvalds #include <net/transp_v6.h>
651da177e4SLinus Torvalds #include <net/ip6_route.h>
661da177e4SLinus Torvalds #include <net/addrconf.h>
671da177e4SLinus Torvalds #include <net/icmp.h>
688b7817f3SHerbert Xu #include <net/xfrm.h>
691ed8516fSDenis V. Lunev #include <net/inet_common.h>
70825edac4SHannes Frederic Sowa #include <net/dsfield.h>
71ca254490SDavid Ahern #include <net/l3mdev.h>
721da177e4SLinus Torvalds 
737c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /*
761da177e4SLinus Torvalds  *	The ICMP socket(s). This is the most convenient way to flow control
771da177e4SLinus Torvalds  *	our ICMP output as well as maintain a clean interface throughout
781da177e4SLinus Torvalds  *	all layers. All Socketless IP sends will soon be gone.
791da177e4SLinus Torvalds  *
801da177e4SLinus Torvalds  *	On SMP we have one ICMP socket per-cpu.
811da177e4SLinus Torvalds  */
8298c6d1b2SDenis V. Lunev static inline struct sock *icmpv6_sk(struct net *net)
8398c6d1b2SDenis V. Lunev {
8498c6d1b2SDenis V. Lunev 	return net->ipv6.icmp_sk[smp_processor_id()];
8598c6d1b2SDenis V. Lunev }
861da177e4SLinus Torvalds 
876f809da2SSteffen Klassert static void icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
886f809da2SSteffen Klassert 		       u8 type, u8 code, int offset, __be32 info)
896f809da2SSteffen Klassert {
906d0bfe22SLorenzo Colitti 	/* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */
916d0bfe22SLorenzo Colitti 	struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset);
926f809da2SSteffen Klassert 	struct net *net = dev_net(skb->dev);
936f809da2SSteffen Klassert 
946f809da2SSteffen Klassert 	if (type == ICMPV6_PKT_TOOBIG)
95e2d118a1SLorenzo Colitti 		ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
966f809da2SSteffen Klassert 	else if (type == NDISC_REDIRECT)
97e2d118a1SLorenzo Colitti 		ip6_redirect(skb, net, skb->dev->ifindex, 0,
98e2d118a1SLorenzo Colitti 			     sock_net_uid(net, NULL));
996d0bfe22SLorenzo Colitti 
1006d0bfe22SLorenzo Colitti 	if (!(type & ICMPV6_INFOMSG_MASK))
1016d0bfe22SLorenzo Colitti 		if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST)
102dcb94b88SHannes Frederic Sowa 			ping_err(skb, offset, ntohl(info));
1036f809da2SSteffen Klassert }
1046f809da2SSteffen Klassert 
105e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb);
1061da177e4SLinus Torvalds 
10741135cc8SAlexey Dobriyan static const struct inet6_protocol icmpv6_protocol = {
1081da177e4SLinus Torvalds 	.handler	=	icmpv6_rcv,
1096f809da2SSteffen Klassert 	.err_handler	=	icmpv6_err,
1108b7817f3SHerbert Xu 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
1137ba91ecbSJesper Dangaard Brouer /* Called with BH disabled */
114fdc0bde9SDenis V. Lunev static __inline__ struct sock *icmpv6_xmit_lock(struct net *net)
1151da177e4SLinus Torvalds {
116fdc0bde9SDenis V. Lunev 	struct sock *sk;
117fdc0bde9SDenis V. Lunev 
118fdc0bde9SDenis V. Lunev 	sk = icmpv6_sk(net);
119405666dbSDenis V. Lunev 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
1201da177e4SLinus Torvalds 		/* This can happen if the output path (f.e. SIT or
1211da177e4SLinus Torvalds 		 * ip6ip6 tunnel) signals dst_link_failure() for an
1221da177e4SLinus Torvalds 		 * outgoing ICMP6 packet.
1231da177e4SLinus Torvalds 		 */
124fdc0bde9SDenis V. Lunev 		return NULL;
1251da177e4SLinus Torvalds 	}
126fdc0bde9SDenis V. Lunev 	return sk;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129405666dbSDenis V. Lunev static __inline__ void icmpv6_xmit_unlock(struct sock *sk)
1301da177e4SLinus Torvalds {
1317ba91ecbSJesper Dangaard Brouer 	spin_unlock(&sk->sk_lock.slock);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  * Figure out, may we reply to this packet with icmp error.
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  * We do not reply, if:
1381da177e4SLinus Torvalds  *	- it was icmp error message.
1391da177e4SLinus Torvalds  *	- it is truncated, so that it is known, that protocol is ICMPV6
1401da177e4SLinus Torvalds  *	  (i.e. in the middle of some exthdr)
1411da177e4SLinus Torvalds  *
1421da177e4SLinus Torvalds  *	--ANK (980726)
1431da177e4SLinus Torvalds  */
1441da177e4SLinus Torvalds 
145a50feda5SEric Dumazet static bool is_ineligible(const struct sk_buff *skb)
1461da177e4SLinus Torvalds {
1470660e03fSArnaldo Carvalho de Melo 	int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
1481da177e4SLinus Torvalds 	int len = skb->len - ptr;
1490660e03fSArnaldo Carvalho de Melo 	__u8 nexthdr = ipv6_hdr(skb)->nexthdr;
15075f2811cSJesse Gross 	__be16 frag_off;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	if (len < 0)
153a50feda5SEric Dumazet 		return true;
1541da177e4SLinus Torvalds 
15575f2811cSJesse Gross 	ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off);
1561da177e4SLinus Torvalds 	if (ptr < 0)
157a50feda5SEric Dumazet 		return false;
1581da177e4SLinus Torvalds 	if (nexthdr == IPPROTO_ICMPV6) {
1591da177e4SLinus Torvalds 		u8 _type, *tp;
1601da177e4SLinus Torvalds 		tp = skb_header_pointer(skb,
1611da177e4SLinus Torvalds 			ptr+offsetof(struct icmp6hdr, icmp6_type),
1621da177e4SLinus Torvalds 			sizeof(_type), &_type);
16363159f29SIan Morris 		if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
164a50feda5SEric Dumazet 			return true;
1651da177e4SLinus Torvalds 	}
166a50feda5SEric Dumazet 	return false;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
169c0303efeSJesper Dangaard Brouer static bool icmpv6_mask_allow(int type)
170c0303efeSJesper Dangaard Brouer {
171c0303efeSJesper Dangaard Brouer 	/* Informational messages are not limited. */
172c0303efeSJesper Dangaard Brouer 	if (type & ICMPV6_INFOMSG_MASK)
173c0303efeSJesper Dangaard Brouer 		return true;
174c0303efeSJesper Dangaard Brouer 
175c0303efeSJesper Dangaard Brouer 	/* Do not limit pmtu discovery, it would break it. */
176c0303efeSJesper Dangaard Brouer 	if (type == ICMPV6_PKT_TOOBIG)
177c0303efeSJesper Dangaard Brouer 		return true;
178c0303efeSJesper Dangaard Brouer 
179c0303efeSJesper Dangaard Brouer 	return false;
180c0303efeSJesper Dangaard Brouer }
181c0303efeSJesper Dangaard Brouer 
182c0303efeSJesper Dangaard Brouer static bool icmpv6_global_allow(int type)
183c0303efeSJesper Dangaard Brouer {
184c0303efeSJesper Dangaard Brouer 	if (icmpv6_mask_allow(type))
185c0303efeSJesper Dangaard Brouer 		return true;
186c0303efeSJesper Dangaard Brouer 
187c0303efeSJesper Dangaard Brouer 	if (icmp_global_allow())
188c0303efeSJesper Dangaard Brouer 		return true;
189c0303efeSJesper Dangaard Brouer 
190c0303efeSJesper Dangaard Brouer 	return false;
191c0303efeSJesper Dangaard Brouer }
192c0303efeSJesper Dangaard Brouer 
1931da177e4SLinus Torvalds /*
1941da177e4SLinus Torvalds  * Check the ICMP output rate limit
1951da177e4SLinus Torvalds  */
1964cdf507dSEric Dumazet static bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
1974c9483b2SDavid S. Miller 			       struct flowi6 *fl6)
1981da177e4SLinus Torvalds {
1993b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2004cdf507dSEric Dumazet 	struct dst_entry *dst;
20192d86829SDavid S. Miller 	bool res = false;
2021da177e4SLinus Torvalds 
203c0303efeSJesper Dangaard Brouer 	if (icmpv6_mask_allow(type))
20492d86829SDavid S. Miller 		return true;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/*
2071da177e4SLinus Torvalds 	 * Look up the output route.
2081da177e4SLinus Torvalds 	 * XXX: perhaps the expire for routing entries cloned by
2091da177e4SLinus Torvalds 	 * this lookup should be more aggressive (not longer than timeout).
2101da177e4SLinus Torvalds 	 */
2114c9483b2SDavid S. Miller 	dst = ip6_route_output(net, sk, fl6);
2121da177e4SLinus Torvalds 	if (dst->error) {
2133bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst),
214a11d206dSYOSHIFUJI Hideaki 			      IPSTATS_MIB_OUTNOROUTES);
2151da177e4SLinus Torvalds 	} else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
21692d86829SDavid S. Miller 		res = true;
2171da177e4SLinus Torvalds 	} else {
2181da177e4SLinus Torvalds 		struct rt6_info *rt = (struct rt6_info *)dst;
2199a43b709SBenjamin Thery 		int tmo = net->ipv6.sysctl.icmpv6_time;
220c0303efeSJesper Dangaard Brouer 		struct inet_peer *peer;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 		/* Give more bandwidth to wider prefixes. */
2231da177e4SLinus Torvalds 		if (rt->rt6i_dst.plen < 128)
2241da177e4SLinus Torvalds 			tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
2251da177e4SLinus Torvalds 
226c0303efeSJesper Dangaard Brouer 		peer = inet_getpeer_v6(net->ipv6.peers, &fl6->daddr, 1);
227fbfe95a4SDavid S. Miller 		res = inet_peer_xrlim_allow(peer, tmo);
2281d861aa4SDavid S. Miller 		if (peer)
2291d861aa4SDavid S. Miller 			inet_putpeer(peer);
2301da177e4SLinus Torvalds 	}
2311da177e4SLinus Torvalds 	dst_release(dst);
2321da177e4SLinus Torvalds 	return res;
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds /*
2361da177e4SLinus Torvalds  *	an inline helper for the "simple" if statement below
2371da177e4SLinus Torvalds  *	checks if parameter problem report is caused by an
2381da177e4SLinus Torvalds  *	unrecognized IPv6 option that has the Option Type
2391da177e4SLinus Torvalds  *	highest-order two bits set to 10
2401da177e4SLinus Torvalds  */
2411da177e4SLinus Torvalds 
242a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	u8 _optval, *op;
2451da177e4SLinus Torvalds 
246bbe735e4SArnaldo Carvalho de Melo 	offset += skb_network_offset(skb);
2471da177e4SLinus Torvalds 	op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
24863159f29SIan Morris 	if (!op)
249a50feda5SEric Dumazet 		return true;
2501da177e4SLinus Torvalds 	return (*op & 0xC0) == 0x80;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2534e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
2546d0bfe22SLorenzo Colitti 				struct icmp6hdr *thdr, int len)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct sk_buff *skb;
2571da177e4SLinus Torvalds 	struct icmp6hdr *icmp6h;
2581da177e4SLinus Torvalds 
259e5d08d71SIan Morris 	skb = skb_peek(&sk->sk_write_queue);
26063159f29SIan Morris 	if (!skb)
2614e64b1edSJoe Perches 		return;
2621da177e4SLinus Torvalds 
263cc70ab26SArnaldo Carvalho de Melo 	icmp6h = icmp6_hdr(skb);
2641da177e4SLinus Torvalds 	memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
2651da177e4SLinus Torvalds 	icmp6h->icmp6_cksum = 0;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
26807f0757aSJoe Perches 		skb->csum = csum_partial(icmp6h,
2691da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), skb->csum);
2704c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2714c9483b2SDavid S. Miller 						      &fl6->daddr,
2724c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
2731da177e4SLinus Torvalds 						      skb->csum);
2741da177e4SLinus Torvalds 	} else {
275868c86bcSAl Viro 		__wsum tmp_csum = 0;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
2781da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
2791da177e4SLinus Torvalds 		}
2801da177e4SLinus Torvalds 
28107f0757aSJoe Perches 		tmp_csum = csum_partial(icmp6h,
2821da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), tmp_csum);
2834c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2844c9483b2SDavid S. Miller 						      &fl6->daddr,
2854c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
286868c86bcSAl Viro 						      tmp_csum);
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 	ip6_push_pending_frames(sk);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds struct icmpv6_msg {
2921da177e4SLinus Torvalds 	struct sk_buff	*skb;
2931da177e4SLinus Torvalds 	int		offset;
294763ecff1SYasuyuki Kozakai 	uint8_t		type;
2951da177e4SLinus Torvalds };
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
3001da177e4SLinus Torvalds 	struct sk_buff *org_skb = msg->skb;
3015f92a738SAl Viro 	__wsum csum = 0;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
3041da177e4SLinus Torvalds 				      to, len, csum);
3051da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
306763ecff1SYasuyuki Kozakai 	if (!(msg->type & ICMPV6_INFOMSG_MASK))
307763ecff1SYasuyuki Kozakai 		nf_ct_attach(skb, org_skb);
3081da177e4SLinus Torvalds 	return 0;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
31107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
31279383236SMasahide NAKAMURA static void mip6_addr_swap(struct sk_buff *skb)
31379383236SMasahide NAKAMURA {
3140660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
31579383236SMasahide NAKAMURA 	struct inet6_skb_parm *opt = IP6CB(skb);
31679383236SMasahide NAKAMURA 	struct ipv6_destopt_hao *hao;
31779383236SMasahide NAKAMURA 	struct in6_addr tmp;
31879383236SMasahide NAKAMURA 	int off;
31979383236SMasahide NAKAMURA 
32079383236SMasahide NAKAMURA 	if (opt->dsthao) {
32179383236SMasahide NAKAMURA 		off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
32279383236SMasahide NAKAMURA 		if (likely(off >= 0)) {
323d56f90a7SArnaldo Carvalho de Melo 			hao = (struct ipv6_destopt_hao *)
324d56f90a7SArnaldo Carvalho de Melo 					(skb_network_header(skb) + off);
3254e3fd7a0SAlexey Dobriyan 			tmp = iph->saddr;
3264e3fd7a0SAlexey Dobriyan 			iph->saddr = hao->addr;
3274e3fd7a0SAlexey Dobriyan 			hao->addr = tmp;
32879383236SMasahide NAKAMURA 		}
32979383236SMasahide NAKAMURA 	}
33079383236SMasahide NAKAMURA }
33179383236SMasahide NAKAMURA #else
33279383236SMasahide NAKAMURA static inline void mip6_addr_swap(struct sk_buff *skb) {}
33379383236SMasahide NAKAMURA #endif
33479383236SMasahide NAKAMURA 
335e8243534Sstephen hemminger static struct dst_entry *icmpv6_route_lookup(struct net *net,
336e8243534Sstephen hemminger 					     struct sk_buff *skb,
337e8243534Sstephen hemminger 					     struct sock *sk,
338e8243534Sstephen hemminger 					     struct flowi6 *fl6)
339b42835dbSDavid S. Miller {
340b42835dbSDavid S. Miller 	struct dst_entry *dst, *dst2;
3414c9483b2SDavid S. Miller 	struct flowi6 fl2;
342b42835dbSDavid S. Miller 	int err;
343b42835dbSDavid S. Miller 
344343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst, fl6);
345b42835dbSDavid S. Miller 	if (err)
346b42835dbSDavid S. Miller 		return ERR_PTR(err);
347b42835dbSDavid S. Miller 
348b42835dbSDavid S. Miller 	/*
349b42835dbSDavid S. Miller 	 * We won't send icmp if the destination is known
350b42835dbSDavid S. Miller 	 * anycast.
351b42835dbSDavid S. Miller 	 */
3522647a9b0SMartin KaFai Lau 	if (ipv6_anycast_destination(dst, &fl6->daddr)) {
353ba7a46f1SJoe Perches 		net_dbg_ratelimited("icmp6_send: acast source\n");
354b42835dbSDavid S. Miller 		dst_release(dst);
355b42835dbSDavid S. Miller 		return ERR_PTR(-EINVAL);
356b42835dbSDavid S. Miller 	}
357b42835dbSDavid S. Miller 
358b42835dbSDavid S. Miller 	/* No need to clone since we're just using its address. */
359b42835dbSDavid S. Miller 	dst2 = dst;
360b42835dbSDavid S. Miller 
3614c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
362452edd59SDavid S. Miller 	if (!IS_ERR(dst)) {
363b42835dbSDavid S. Miller 		if (dst != dst2)
364b42835dbSDavid S. Miller 			return dst;
365452edd59SDavid S. Miller 	} else {
366452edd59SDavid S. Miller 		if (PTR_ERR(dst) == -EPERM)
367b42835dbSDavid S. Miller 			dst = NULL;
368452edd59SDavid S. Miller 		else
369452edd59SDavid S. Miller 			return dst;
370b42835dbSDavid S. Miller 	}
371b42835dbSDavid S. Miller 
3724c9483b2SDavid S. Miller 	err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6);
373b42835dbSDavid S. Miller 	if (err)
374b42835dbSDavid S. Miller 		goto relookup_failed;
375b42835dbSDavid S. Miller 
376343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst2, &fl2);
377b42835dbSDavid S. Miller 	if (err)
378b42835dbSDavid S. Miller 		goto relookup_failed;
379b42835dbSDavid S. Miller 
3804c9483b2SDavid S. Miller 	dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
381452edd59SDavid S. Miller 	if (!IS_ERR(dst2)) {
382b42835dbSDavid S. Miller 		dst_release(dst);
383b42835dbSDavid S. Miller 		dst = dst2;
384452edd59SDavid S. Miller 	} else {
385452edd59SDavid S. Miller 		err = PTR_ERR(dst2);
386452edd59SDavid S. Miller 		if (err == -EPERM) {
387b42835dbSDavid S. Miller 			dst_release(dst);
388452edd59SDavid S. Miller 			return dst2;
389452edd59SDavid S. Miller 		} else
390b42835dbSDavid S. Miller 			goto relookup_failed;
391b42835dbSDavid S. Miller 	}
392b42835dbSDavid S. Miller 
393b42835dbSDavid S. Miller relookup_failed:
394b42835dbSDavid S. Miller 	if (dst)
395b42835dbSDavid S. Miller 		return dst;
396b42835dbSDavid S. Miller 	return ERR_PTR(err);
397b42835dbSDavid S. Miller }
398b42835dbSDavid S. Miller 
3991b70d792SDavid Ahern static int icmp6_iif(const struct sk_buff *skb)
4001b70d792SDavid Ahern {
4011b70d792SDavid Ahern 	int iif = skb->dev->ifindex;
4021b70d792SDavid Ahern 
4031b70d792SDavid Ahern 	/* for local traffic to local address, skb dev is the loopback
4041b70d792SDavid Ahern 	 * device. Check if there is a dst attached to the skb and if so
4051b70d792SDavid Ahern 	 * get the real device index.
4061b70d792SDavid Ahern 	 */
4071b70d792SDavid Ahern 	if (unlikely(iif == LOOPBACK_IFINDEX)) {
4081b70d792SDavid Ahern 		const struct rt6_info *rt6 = skb_rt6_info(skb);
4091b70d792SDavid Ahern 
4101b70d792SDavid Ahern 		if (rt6)
4111b70d792SDavid Ahern 			iif = rt6->rt6i_idev->dev->ifindex;
4121b70d792SDavid Ahern 	}
4131b70d792SDavid Ahern 
4141b70d792SDavid Ahern 	return iif;
4151b70d792SDavid Ahern }
4161b70d792SDavid Ahern 
4171da177e4SLinus Torvalds /*
4181da177e4SLinus Torvalds  *	Send an ICMP message in response to a packet in error
4191da177e4SLinus Torvalds  */
420b1cadc1aSEric Dumazet static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
421b1cadc1aSEric Dumazet 		       const struct in6_addr *force_saddr)
4221da177e4SLinus Torvalds {
423c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
4241da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
4250660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
42684427d53SYOSHIFUJI Hideaki 	struct sock *sk;
42784427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
428b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
4291da177e4SLinus Torvalds 	struct dst_entry *dst;
4301da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
4314c9483b2SDavid S. Miller 	struct flowi6 fl6;
4321da177e4SLinus Torvalds 	struct icmpv6_msg msg;
433c14ac945SSoheil Hassas Yeganeh 	struct sockcm_cookie sockc_unused = {0};
43426879da5SWei Wang 	struct ipcm6_cookie ipc6;
4351da177e4SLinus Torvalds 	int iif = 0;
4361da177e4SLinus Torvalds 	int addr_type = 0;
4371da177e4SLinus Torvalds 	int len;
438e110861fSLorenzo Colitti 	u32 mark = IP6_REPLY_MARK(net, skb->mark);
4391da177e4SLinus Torvalds 
44027a884dcSArnaldo Carvalho de Melo 	if ((u8 *)hdr < skb->head ||
44129a3cad5SSimon Horman 	    (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
4421da177e4SLinus Torvalds 		return;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	/*
4451da177e4SLinus Torvalds 	 *	Make sure we respect the rules
4461da177e4SLinus Torvalds 	 *	i.e. RFC 1885 2.4(e)
4475f5624cfSPravin B Shelar 	 *	Rule (e.1) is enforced by not using icmp6_send
4481da177e4SLinus Torvalds 	 *	in any code that processes icmp errors.
4491da177e4SLinus Torvalds 	 */
4501da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->daddr);
4511da177e4SLinus Torvalds 
452446fab59SFX Le Bail 	if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) ||
453d94c1f92SFX Le Bail 	    ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr))
4541da177e4SLinus Torvalds 		saddr = &hdr->daddr;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	/*
4571da177e4SLinus Torvalds 	 *	Dest addr check
4581da177e4SLinus Torvalds 	 */
4591da177e4SLinus Torvalds 
4609a6b4b39Szhuyj 	if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) {
4611da177e4SLinus Torvalds 		if (type != ICMPV6_PKT_TOOBIG &&
4621da177e4SLinus Torvalds 		    !(type == ICMPV6_PARAMPROB &&
4631da177e4SLinus Torvalds 		      code == ICMPV6_UNK_OPTION &&
4641da177e4SLinus Torvalds 		      (opt_unrec(skb, info))))
4651da177e4SLinus Torvalds 			return;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 		saddr = NULL;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->saddr);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	/*
4731da177e4SLinus Torvalds 	 *	Source addr check
4741da177e4SLinus Torvalds 	 */
4751da177e4SLinus Torvalds 
4764832c30dSDavid Ahern 	if (__ipv6_addr_needs_scope_id(addr_type)) {
4771b70d792SDavid Ahern 		iif = icmp6_iif(skb);
4784832c30dSDavid Ahern 	} else {
47979dc7e3fSDavid Ahern 		dst = skb_dst(skb);
48079dc7e3fSDavid Ahern 		iif = l3mdev_master_ifindex(dst ? dst->dev : skb->dev);
48179dc7e3fSDavid Ahern 	}
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	/*
4848de3351eSYOSHIFUJI Hideaki 	 *	Must not send error if the source does not uniquely
4858de3351eSYOSHIFUJI Hideaki 	 *	identify a single node (RFC2463 Section 2.4).
4868de3351eSYOSHIFUJI Hideaki 	 *	We check unspecified / multicast addresses here,
4878de3351eSYOSHIFUJI Hideaki 	 *	and anycast addresses will be checked later.
4881da177e4SLinus Torvalds 	 */
4891da177e4SLinus Torvalds 	if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
4904b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n",
4914b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
4921da177e4SLinus Torvalds 		return;
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	/*
4961da177e4SLinus Torvalds 	 *	Never answer to a ICMP packet.
4971da177e4SLinus Torvalds 	 */
4981da177e4SLinus Torvalds 	if (is_ineligible(skb)) {
4994b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n",
5004b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
5011da177e4SLinus Torvalds 		return;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 
5047ba91ecbSJesper Dangaard Brouer 	/* Needed by both icmp_global_allow and icmpv6_xmit_lock */
5057ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
5067ba91ecbSJesper Dangaard Brouer 
5077ba91ecbSJesper Dangaard Brouer 	/* Check global sysctl_icmp_msgs_per_sec ratelimit */
508849a44deSJesper Dangaard Brouer 	if (!(skb->dev->flags&IFF_LOOPBACK) && !icmpv6_global_allow(type))
5097ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
5107ba91ecbSJesper Dangaard Brouer 
51179383236SMasahide NAKAMURA 	mip6_addr_swap(skb);
51279383236SMasahide NAKAMURA 
5134c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
5144c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
5154e3fd7a0SAlexey Dobriyan 	fl6.daddr = hdr->saddr;
516b1cadc1aSEric Dumazet 	if (force_saddr)
517b1cadc1aSEric Dumazet 		saddr = force_saddr;
5181da177e4SLinus Torvalds 	if (saddr)
5194e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
520e110861fSLorenzo Colitti 	fl6.flowi6_mark = mark;
5214c9483b2SDavid S. Miller 	fl6.flowi6_oif = iif;
5221958b856SDavid S. Miller 	fl6.fl6_icmp_type = type;
5231958b856SDavid S. Miller 	fl6.fl6_icmp_code = code;
524e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
5255e5d6fedSRoopa Prabhu 	fl6.mp_hash = rt6_multipath_hash(&fl6, skb, NULL);
5264c9483b2SDavid S. Miller 	security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
5271da177e4SLinus Torvalds 
528fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
52963159f29SIan Morris 	if (!sk)
5307ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
531c0303efeSJesper Dangaard Brouer 
532e110861fSLorenzo Colitti 	sk->sk_mark = mark;
533fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
534405666dbSDenis V. Lunev 
5354c9483b2SDavid S. Miller 	if (!icmpv6_xrlim_allow(sk, type, &fl6))
5361da177e4SLinus Torvalds 		goto out;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = type;
5391da177e4SLinus Torvalds 	tmp_hdr.icmp6_code = code;
5401da177e4SLinus Torvalds 	tmp_hdr.icmp6_cksum = 0;
5411da177e4SLinus Torvalds 	tmp_hdr.icmp6_pointer = htonl(info);
5421da177e4SLinus Torvalds 
5434c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
5444c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
545c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
546c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
5471da177e4SLinus Torvalds 
54838b7097bSHannes Frederic Sowa 	ipc6.tclass = np->tclass;
54938b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
55038b7097bSHannes Frederic Sowa 
5514c9483b2SDavid S. Miller 	dst = icmpv6_route_lookup(net, skb, sk, &fl6);
552b42835dbSDavid S. Miller 	if (IS_ERR(dst))
5531da177e4SLinus Torvalds 		goto out;
5548de3351eSYOSHIFUJI Hideaki 
55526879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
55626879da5SWei Wang 	ipc6.dontfrag = np->dontfrag;
55726879da5SWei Wang 	ipc6.opt = NULL;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	msg.skb = skb;
560bbe735e4SArnaldo Carvalho de Melo 	msg.offset = skb_network_offset(skb);
561763ecff1SYasuyuki Kozakai 	msg.type = type;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	len = skb->len - msg.offset;
5641da177e4SLinus Torvalds 	len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr));
5651da177e4SLinus Torvalds 	if (len < 0) {
5664b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n",
5674b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
5681da177e4SLinus Torvalds 		goto out_dst_release;
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 
571cfdf7647SEric Dumazet 	rcu_read_lock();
572cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
5731da177e4SLinus Torvalds 
5744e64b1edSJoe Perches 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
5751da177e4SLinus Torvalds 			    len + sizeof(struct icmp6hdr),
57626879da5SWei Wang 			    sizeof(struct icmp6hdr),
57726879da5SWei Wang 			    &ipc6, &fl6, (struct rt6_info *)dst,
5784e64b1edSJoe Perches 			    MSG_DONTWAIT, &sockc_unused)) {
57943a43b60SHannes Frederic Sowa 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
5801da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
581cfdf7647SEric Dumazet 	} else {
5824e64b1edSJoe Perches 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
583cfdf7647SEric Dumazet 					   len + sizeof(struct icmp6hdr));
5841da177e4SLinus Torvalds 	}
585cfdf7647SEric Dumazet 	rcu_read_unlock();
5861da177e4SLinus Torvalds out_dst_release:
5871da177e4SLinus Torvalds 	dst_release(dst);
5881da177e4SLinus Torvalds out:
589405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
5907ba91ecbSJesper Dangaard Brouer out_bh_enable:
5917ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
5921da177e4SLinus Torvalds }
5935f5624cfSPravin B Shelar 
5945f5624cfSPravin B Shelar /* Slightly more convenient version of icmp6_send.
5955f5624cfSPravin B Shelar  */
5965f5624cfSPravin B Shelar void icmpv6_param_prob(struct sk_buff *skb, u8 code, int pos)
5975f5624cfSPravin B Shelar {
598b1cadc1aSEric Dumazet 	icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL);
5995f5624cfSPravin B Shelar 	kfree_skb(skb);
6005f5624cfSPravin B Shelar }
6017159039aSYOSHIFUJI Hideaki 
6025fbba8acSEric Dumazet /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
6035fbba8acSEric Dumazet  * if sufficient data bytes are available
6045fbba8acSEric Dumazet  * @nhs is the size of the tunnel header(s) :
6055fbba8acSEric Dumazet  *  Either an IPv4 header for SIT encap
6065fbba8acSEric Dumazet  *         an IPv4 header + GRE header for GRE encap
6075fbba8acSEric Dumazet  */
60820e1954fSEric Dumazet int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type,
60920e1954fSEric Dumazet 			       unsigned int data_len)
6105fbba8acSEric Dumazet {
6112d7a3b27SEric Dumazet 	struct in6_addr temp_saddr;
6125fbba8acSEric Dumazet 	struct rt6_info *rt;
6135fbba8acSEric Dumazet 	struct sk_buff *skb2;
61420e1954fSEric Dumazet 	u32 info = 0;
6155fbba8acSEric Dumazet 
6165fbba8acSEric Dumazet 	if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8))
6175fbba8acSEric Dumazet 		return 1;
6185fbba8acSEric Dumazet 
61920e1954fSEric Dumazet 	/* RFC 4884 (partial) support for ICMP extensions */
62020e1954fSEric Dumazet 	if (data_len < 128 || (data_len & 7) || skb->len < data_len)
62120e1954fSEric Dumazet 		data_len = 0;
62220e1954fSEric Dumazet 
62320e1954fSEric Dumazet 	skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC);
6245fbba8acSEric Dumazet 
6255fbba8acSEric Dumazet 	if (!skb2)
6265fbba8acSEric Dumazet 		return 1;
6275fbba8acSEric Dumazet 
6285fbba8acSEric Dumazet 	skb_dst_drop(skb2);
6295fbba8acSEric Dumazet 	skb_pull(skb2, nhs);
6305fbba8acSEric Dumazet 	skb_reset_network_header(skb2);
6315fbba8acSEric Dumazet 
632b75cc8f9SDavid Ahern 	rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0,
633b75cc8f9SDavid Ahern 			skb, 0);
6345fbba8acSEric Dumazet 
6355fbba8acSEric Dumazet 	if (rt && rt->dst.dev)
6365fbba8acSEric Dumazet 		skb2->dev = rt->dst.dev;
6375fbba8acSEric Dumazet 
6382d7a3b27SEric Dumazet 	ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr);
63920e1954fSEric Dumazet 
64020e1954fSEric Dumazet 	if (data_len) {
64120e1954fSEric Dumazet 		/* RFC 4884 (partial) support :
64220e1954fSEric Dumazet 		 * insert 0 padding at the end, before the extensions
64320e1954fSEric Dumazet 		 */
64420e1954fSEric Dumazet 		__skb_push(skb2, nhs);
64520e1954fSEric Dumazet 		skb_reset_network_header(skb2);
64620e1954fSEric Dumazet 		memmove(skb2->data, skb2->data + nhs, data_len - nhs);
64720e1954fSEric Dumazet 		memset(skb2->data + data_len - nhs, 0, nhs);
64820e1954fSEric Dumazet 		/* RFC 4884 4.5 : Length is measured in 64-bit words,
64920e1954fSEric Dumazet 		 * and stored in reserved[0]
65020e1954fSEric Dumazet 		 */
65120e1954fSEric Dumazet 		info = (data_len/8) << 24;
65220e1954fSEric Dumazet 	}
6532d7a3b27SEric Dumazet 	if (type == ICMP_TIME_EXCEEDED)
6542d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
65520e1954fSEric Dumazet 			   info, &temp_saddr);
6562d7a3b27SEric Dumazet 	else
6572d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH,
65820e1954fSEric Dumazet 			   info, &temp_saddr);
6595fbba8acSEric Dumazet 	if (rt)
6605fbba8acSEric Dumazet 		ip6_rt_put(rt);
6615fbba8acSEric Dumazet 
6625fbba8acSEric Dumazet 	kfree_skb(skb2);
6635fbba8acSEric Dumazet 
6645fbba8acSEric Dumazet 	return 0;
6655fbba8acSEric Dumazet }
6665fbba8acSEric Dumazet EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach);
6675fbba8acSEric Dumazet 
6681da177e4SLinus Torvalds static void icmpv6_echo_reply(struct sk_buff *skb)
6691da177e4SLinus Torvalds {
670c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
67184427d53SYOSHIFUJI Hideaki 	struct sock *sk;
6721da177e4SLinus Torvalds 	struct inet6_dev *idev;
67384427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
674b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
675cc70ab26SArnaldo Carvalho de Melo 	struct icmp6hdr *icmph = icmp6_hdr(skb);
6761da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
6774c9483b2SDavid S. Miller 	struct flowi6 fl6;
6781da177e4SLinus Torvalds 	struct icmpv6_msg msg;
6791da177e4SLinus Torvalds 	struct dst_entry *dst;
68026879da5SWei Wang 	struct ipcm6_cookie ipc6;
681e110861fSLorenzo Colitti 	u32 mark = IP6_REPLY_MARK(net, skb->mark);
682c14ac945SSoheil Hassas Yeganeh 	struct sockcm_cookie sockc_unused = {0};
6831da177e4SLinus Torvalds 
6840660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->daddr;
6851da177e4SLinus Torvalds 
686509aba3bSFX Le Bail 	if (!ipv6_unicast_destination(skb) &&
687ec35b61eSFX Le Bail 	    !(net->ipv6.sysctl.anycast_src_echo_reply &&
6882647a9b0SMartin KaFai Lau 	      ipv6_anycast_destination(skb_dst(skb), saddr)))
6891da177e4SLinus Torvalds 		saddr = NULL;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
6921da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = ICMPV6_ECHO_REPLY;
6931da177e4SLinus Torvalds 
6944c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
6954c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
6964e3fd7a0SAlexey Dobriyan 	fl6.daddr = ipv6_hdr(skb)->saddr;
6971da177e4SLinus Torvalds 	if (saddr)
6984e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
6991b70d792SDavid Ahern 	fl6.flowi6_oif = icmp6_iif(skb);
7001958b856SDavid S. Miller 	fl6.fl6_icmp_type = ICMPV6_ECHO_REPLY;
701e110861fSLorenzo Colitti 	fl6.flowi6_mark = mark;
702e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
7034c9483b2SDavid S. Miller 	security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
7041da177e4SLinus Torvalds 
7057ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
706fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
70763159f29SIan Morris 	if (!sk)
7087ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
709e110861fSLorenzo Colitti 	sk->sk_mark = mark;
710fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
711405666dbSDenis V. Lunev 
7124c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
7134c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
714c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
715c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
7161da177e4SLinus Torvalds 
7174e64b1edSJoe Perches 	if (ip6_dst_lookup(net, sk, &dst, &fl6))
7181da177e4SLinus Torvalds 		goto out;
7194c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
720452edd59SDavid S. Miller 	if (IS_ERR(dst))
721e104411bSPatrick McHardy 		goto out;
7221da177e4SLinus Torvalds 
723cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	msg.skb = skb;
7261da177e4SLinus Torvalds 	msg.offset = 0;
727763ecff1SYasuyuki Kozakai 	msg.type = ICMPV6_ECHO_REPLY;
7281da177e4SLinus Torvalds 
72926879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
73026879da5SWei Wang 	ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb));
73126879da5SWei Wang 	ipc6.dontfrag = np->dontfrag;
73226879da5SWei Wang 	ipc6.opt = NULL;
73326879da5SWei Wang 
7344e64b1edSJoe Perches 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
7354e64b1edSJoe Perches 			    skb->len + sizeof(struct icmp6hdr),
73626879da5SWei Wang 			    sizeof(struct icmp6hdr), &ipc6, &fl6,
73713b52cd4SBrian Haley 			    (struct rt6_info *)dst, MSG_DONTWAIT,
7384e64b1edSJoe Perches 			    &sockc_unused)) {
739a16292a0SEric Dumazet 		__ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
7401da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
741cfdf7647SEric Dumazet 	} else {
7424e64b1edSJoe Perches 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
743cfdf7647SEric Dumazet 					   skb->len + sizeof(struct icmp6hdr));
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 	dst_release(dst);
7461da177e4SLinus Torvalds out:
747405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
7487ba91ecbSJesper Dangaard Brouer out_bh_enable:
7497ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
752b94f1c09SDavid S. Miller void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
7531da177e4SLinus Torvalds {
75441135cc8SAlexey Dobriyan 	const struct inet6_protocol *ipprot;
7551da177e4SLinus Torvalds 	int inner_offset;
75675f2811cSJesse Gross 	__be16 frag_off;
757f9242b6bSDavid S. Miller 	u8 nexthdr;
7587304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
7617304fe46SDuan Jiong 		goto out;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
7641da177e4SLinus Torvalds 	if (ipv6_ext_hdr(nexthdr)) {
7651da177e4SLinus Torvalds 		/* now skip over extension headers */
76675f2811cSJesse Gross 		inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
76775f2811cSJesse Gross 						&nexthdr, &frag_off);
7681da177e4SLinus Torvalds 		if (inner_offset < 0)
7697304fe46SDuan Jiong 			goto out;
7701da177e4SLinus Torvalds 	} else {
7711da177e4SLinus Torvalds 		inner_offset = sizeof(struct ipv6hdr);
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	/* Checkin header including 8 bytes of inner protocol header. */
7751da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, inner_offset+8))
7767304fe46SDuan Jiong 		goto out;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	/* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
7791da177e4SLinus Torvalds 	   Without this we will not able f.e. to make source routed
7801da177e4SLinus Torvalds 	   pmtu discovery.
7811da177e4SLinus Torvalds 	   Corresponding argument (opt) to notifiers is already added.
7821da177e4SLinus Torvalds 	   --ANK (980726)
7831da177e4SLinus Torvalds 	 */
7841da177e4SLinus Torvalds 
785f9242b6bSDavid S. Miller 	ipprot = rcu_dereference(inet6_protos[nexthdr]);
7861da177e4SLinus Torvalds 	if (ipprot && ipprot->err_handler)
7871da177e4SLinus Torvalds 		ipprot->err_handler(skb, NULL, type, code, inner_offset, info);
7881da177e4SLinus Torvalds 
78969d6da0bSPavel Emelyanov 	raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
7907304fe46SDuan Jiong 	return;
7917304fe46SDuan Jiong 
7927304fe46SDuan Jiong out:
793a16292a0SEric Dumazet 	__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds /*
7971da177e4SLinus Torvalds  *	Handle icmp messages
7981da177e4SLinus Torvalds  */
7991da177e4SLinus Torvalds 
800e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
8031da177e4SLinus Torvalds 	struct inet6_dev *idev = __in6_dev_get(dev);
804b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
8051da177e4SLinus Torvalds 	struct icmp6hdr *hdr;
806d5fdd6baSBrian Haley 	u8 type;
807e3e32170SRick Jones 	bool success = false;
8081da177e4SLinus Torvalds 
809aebcf82cSHerbert Xu 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
810def8b4faSAlexey Dobriyan 		struct sec_path *sp = skb_sec_path(skb);
8118b7817f3SHerbert Xu 		int nh;
8128b7817f3SHerbert Xu 
813def8b4faSAlexey Dobriyan 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
814aebcf82cSHerbert Xu 				 XFRM_STATE_ICMP))
815aebcf82cSHerbert Xu 			goto drop_no_count;
816aebcf82cSHerbert Xu 
81781aded24SDavid S. Miller 		if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
8188b7817f3SHerbert Xu 			goto drop_no_count;
8198b7817f3SHerbert Xu 
8208b7817f3SHerbert Xu 		nh = skb_network_offset(skb);
8218b7817f3SHerbert Xu 		skb_set_network_header(skb, sizeof(*hdr));
8228b7817f3SHerbert Xu 
8238b7817f3SHerbert Xu 		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
8248b7817f3SHerbert Xu 			goto drop_no_count;
8258b7817f3SHerbert Xu 
8268b7817f3SHerbert Xu 		skb_set_network_header(skb, nh);
8278b7817f3SHerbert Xu 	}
8288b7817f3SHerbert Xu 
829a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INMSGS);
8301da177e4SLinus Torvalds 
8310660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
8320660e03fSArnaldo Carvalho de Melo 	daddr = &ipv6_hdr(skb)->daddr;
8331da177e4SLinus Torvalds 
83439471ac8STom Herbert 	if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) {
835ba7a46f1SJoe Perches 		net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n",
8360c6ce78aSHarvey Harrison 				    saddr, daddr);
8376a5dc9e5SEric Dumazet 		goto csum_error;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8408cf22943SHerbert Xu 	if (!pskb_pull(skb, sizeof(*hdr)))
8418cf22943SHerbert Xu 		goto discard_it;
8421da177e4SLinus Torvalds 
843cc70ab26SArnaldo Carvalho de Melo 	hdr = icmp6_hdr(skb);
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	type = hdr->icmp6_type;
8461da177e4SLinus Torvalds 
847f3832ed2SEric Dumazet 	ICMP6MSGIN_INC_STATS(dev_net(dev), idev, type);
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	switch (type) {
8501da177e4SLinus Torvalds 	case ICMPV6_ECHO_REQUEST:
8511da177e4SLinus Torvalds 		icmpv6_echo_reply(skb);
8521da177e4SLinus Torvalds 		break;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	case ICMPV6_ECHO_REPLY:
855e3e32170SRick Jones 		success = ping_rcv(skb);
8561da177e4SLinus Torvalds 		break;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
8591da177e4SLinus Torvalds 		/* BUGGG_FUTURE: if packet contains rthdr, we cannot update
8601da177e4SLinus Torvalds 		   standard destination cache. Seems, only "advanced"
8611da177e4SLinus Torvalds 		   destination cache will allow to solve this problem
8621da177e4SLinus Torvalds 		   --ANK (980726)
8631da177e4SLinus Torvalds 		 */
8641da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
8651da177e4SLinus Torvalds 			goto discard_it;
866cc70ab26SArnaldo Carvalho de Melo 		hdr = icmp6_hdr(skb);
8671da177e4SLinus Torvalds 
868275757e6SGustavo A. R. Silva 		/* to notify */
869275757e6SGustavo A. R. Silva 		/* fall through */
8701da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
8711da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
8721da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
8731da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
8741da177e4SLinus Torvalds 		break;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	case NDISC_ROUTER_SOLICITATION:
8771da177e4SLinus Torvalds 	case NDISC_ROUTER_ADVERTISEMENT:
8781da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_SOLICITATION:
8791da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_ADVERTISEMENT:
8801da177e4SLinus Torvalds 	case NDISC_REDIRECT:
8811da177e4SLinus Torvalds 		ndisc_rcv(skb);
8821da177e4SLinus Torvalds 		break;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	case ICMPV6_MGM_QUERY:
8851da177e4SLinus Torvalds 		igmp6_event_query(skb);
8861da177e4SLinus Torvalds 		break;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	case ICMPV6_MGM_REPORT:
8891da177e4SLinus Torvalds 		igmp6_event_report(skb);
8901da177e4SLinus Torvalds 		break;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	case ICMPV6_MGM_REDUCTION:
8931da177e4SLinus Torvalds 	case ICMPV6_NI_QUERY:
8941da177e4SLinus Torvalds 	case ICMPV6_NI_REPLY:
8951da177e4SLinus Torvalds 	case ICMPV6_MLD2_REPORT:
8961da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REQUEST:
8971da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REPLY:
8981da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_SOL:
8991da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_ADV:
9001da177e4SLinus Torvalds 		break;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	default:
9031da177e4SLinus Torvalds 		/* informational */
9041da177e4SLinus Torvalds 		if (type & ICMPV6_INFOMSG_MASK)
9051da177e4SLinus Torvalds 			break;
9061da177e4SLinus Torvalds 
9074b3418fbSBjørn Mork 		net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n",
9084b3418fbSBjørn Mork 				    saddr, daddr);
909ea85a0a2SDavid S. Miller 
9101da177e4SLinus Torvalds 		/*
9111da177e4SLinus Torvalds 		 * error of unknown type.
9121da177e4SLinus Torvalds 		 * must pass to upper level
9131da177e4SLinus Torvalds 		 */
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
9163ff50b79SStephen Hemminger 	}
9173ff50b79SStephen Hemminger 
918e3e32170SRick Jones 	/* until the v6 path can be better sorted assume failure and
919e3e32170SRick Jones 	 * preserve the status quo behaviour for the rest of the paths to here
920e3e32170SRick Jones 	 */
921e3e32170SRick Jones 	if (success)
922e3e32170SRick Jones 		consume_skb(skb);
923e3e32170SRick Jones 	else
9241da177e4SLinus Torvalds 		kfree_skb(skb);
925e3e32170SRick Jones 
9261da177e4SLinus Torvalds 	return 0;
9271da177e4SLinus Torvalds 
9286a5dc9e5SEric Dumazet csum_error:
929a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS);
9301da177e4SLinus Torvalds discard_it:
931a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INERRORS);
9328b7817f3SHerbert Xu drop_no_count:
9331da177e4SLinus Torvalds 	kfree_skb(skb);
9341da177e4SLinus Torvalds 	return 0;
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
9374c9483b2SDavid S. Miller void icmpv6_flow_init(struct sock *sk, struct flowi6 *fl6,
93895e41e93SYOSHIFUJI Hideaki 		      u8 type,
93995e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *saddr,
94095e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *daddr,
94195e41e93SYOSHIFUJI Hideaki 		      int oif)
94295e41e93SYOSHIFUJI Hideaki {
9434c9483b2SDavid S. Miller 	memset(fl6, 0, sizeof(*fl6));
9444e3fd7a0SAlexey Dobriyan 	fl6->saddr = *saddr;
9454e3fd7a0SAlexey Dobriyan 	fl6->daddr = *daddr;
9464c9483b2SDavid S. Miller 	fl6->flowi6_proto	= IPPROTO_ICMPV6;
9471958b856SDavid S. Miller 	fl6->fl6_icmp_type	= type;
9481958b856SDavid S. Miller 	fl6->fl6_icmp_code	= 0;
9494c9483b2SDavid S. Miller 	fl6->flowi6_oif		= oif;
9504c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
95195e41e93SYOSHIFUJI Hideaki }
95295e41e93SYOSHIFUJI Hideaki 
95398c6d1b2SDenis V. Lunev static int __net_init icmpv6_sk_init(struct net *net)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	struct sock *sk;
9561da177e4SLinus Torvalds 	int err, i, j;
9571da177e4SLinus Torvalds 
95898c6d1b2SDenis V. Lunev 	net->ipv6.icmp_sk =
95998c6d1b2SDenis V. Lunev 		kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
96063159f29SIan Morris 	if (!net->ipv6.icmp_sk)
96179c91159SDenis V. Lunev 		return -ENOMEM;
96279c91159SDenis V. Lunev 
9636f912042SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
9641ed8516fSDenis V. Lunev 		err = inet_ctl_sock_create(&sk, PF_INET6,
9651ed8516fSDenis V. Lunev 					   SOCK_RAW, IPPROTO_ICMPV6, net);
9661da177e4SLinus Torvalds 		if (err < 0) {
967f3213831SJoe Perches 			pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
9681da177e4SLinus Torvalds 			       err);
9691da177e4SLinus Torvalds 			goto fail;
9701da177e4SLinus Torvalds 		}
9711da177e4SLinus Torvalds 
9721ed8516fSDenis V. Lunev 		net->ipv6.icmp_sk[i] = sk;
9735c8cafd6SDenis V. Lunev 
9741da177e4SLinus Torvalds 		/* Enough space for 2 64K ICMP packets, including
9751da177e4SLinus Torvalds 		 * sk_buff struct overhead.
9761da177e4SLinus Torvalds 		 */
97787fb4b7bSEric Dumazet 		sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 	return 0;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds  fail:
9825c8cafd6SDenis V. Lunev 	for (j = 0; j < i; j++)
9831ed8516fSDenis V. Lunev 		inet_ctl_sock_destroy(net->ipv6.icmp_sk[j]);
98498c6d1b2SDenis V. Lunev 	kfree(net->ipv6.icmp_sk);
9851da177e4SLinus Torvalds 	return err;
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
98898c6d1b2SDenis V. Lunev static void __net_exit icmpv6_sk_exit(struct net *net)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	int i;
9911da177e4SLinus Torvalds 
9926f912042SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
9931ed8516fSDenis V. Lunev 		inet_ctl_sock_destroy(net->ipv6.icmp_sk[i]);
9941da177e4SLinus Torvalds 	}
99598c6d1b2SDenis V. Lunev 	kfree(net->ipv6.icmp_sk);
99698c6d1b2SDenis V. Lunev }
99798c6d1b2SDenis V. Lunev 
9988ed7edceSAlexey Dobriyan static struct pernet_operations icmpv6_sk_ops = {
99998c6d1b2SDenis V. Lunev 	.init = icmpv6_sk_init,
100098c6d1b2SDenis V. Lunev 	.exit = icmpv6_sk_exit,
10011a2e9332SKirill Tkhai 	.async = true,
100298c6d1b2SDenis V. Lunev };
100398c6d1b2SDenis V. Lunev 
100498c6d1b2SDenis V. Lunev int __init icmpv6_init(void)
100598c6d1b2SDenis V. Lunev {
100698c6d1b2SDenis V. Lunev 	int err;
100798c6d1b2SDenis V. Lunev 
100898c6d1b2SDenis V. Lunev 	err = register_pernet_subsys(&icmpv6_sk_ops);
100998c6d1b2SDenis V. Lunev 	if (err < 0)
101098c6d1b2SDenis V. Lunev 		return err;
101198c6d1b2SDenis V. Lunev 
101298c6d1b2SDenis V. Lunev 	err = -EAGAIN;
101398c6d1b2SDenis V. Lunev 	if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
101498c6d1b2SDenis V. Lunev 		goto fail;
10155f5624cfSPravin B Shelar 
10165f5624cfSPravin B Shelar 	err = inet6_register_icmp_sender(icmp6_send);
10175f5624cfSPravin B Shelar 	if (err)
10185f5624cfSPravin B Shelar 		goto sender_reg_err;
101998c6d1b2SDenis V. Lunev 	return 0;
102098c6d1b2SDenis V. Lunev 
10215f5624cfSPravin B Shelar sender_reg_err:
10225f5624cfSPravin B Shelar 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
102398c6d1b2SDenis V. Lunev fail:
1024f3213831SJoe Perches 	pr_err("Failed to register ICMP6 protocol\n");
102598c6d1b2SDenis V. Lunev 	unregister_pernet_subsys(&icmpv6_sk_ops);
102698c6d1b2SDenis V. Lunev 	return err;
102798c6d1b2SDenis V. Lunev }
102898c6d1b2SDenis V. Lunev 
10298ed7edceSAlexey Dobriyan void icmpv6_cleanup(void)
103098c6d1b2SDenis V. Lunev {
10315f5624cfSPravin B Shelar 	inet6_unregister_icmp_sender(icmp6_send);
103298c6d1b2SDenis V. Lunev 	unregister_pernet_subsys(&icmpv6_sk_ops);
10331da177e4SLinus Torvalds 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
10341da177e4SLinus Torvalds }
10351da177e4SLinus Torvalds 
103698c6d1b2SDenis V. Lunev 
10379b5b5cffSArjan van de Ven static const struct icmp6_err {
10381da177e4SLinus Torvalds 	int err;
10391da177e4SLinus Torvalds 	int fatal;
10401da177e4SLinus Torvalds } tab_unreach[] = {
10411da177e4SLinus Torvalds 	{	/* NOROUTE */
10421da177e4SLinus Torvalds 		.err	= ENETUNREACH,
10431da177e4SLinus Torvalds 		.fatal	= 0,
10441da177e4SLinus Torvalds 	},
10451da177e4SLinus Torvalds 	{	/* ADM_PROHIBITED */
10461da177e4SLinus Torvalds 		.err	= EACCES,
10471da177e4SLinus Torvalds 		.fatal	= 1,
10481da177e4SLinus Torvalds 	},
10491da177e4SLinus Torvalds 	{	/* Was NOT_NEIGHBOUR, now reserved */
10501da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
10511da177e4SLinus Torvalds 		.fatal	= 0,
10521da177e4SLinus Torvalds 	},
10531da177e4SLinus Torvalds 	{	/* ADDR_UNREACH	*/
10541da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
10551da177e4SLinus Torvalds 		.fatal	= 0,
10561da177e4SLinus Torvalds 	},
10571da177e4SLinus Torvalds 	{	/* PORT_UNREACH	*/
10581da177e4SLinus Torvalds 		.err	= ECONNREFUSED,
10591da177e4SLinus Torvalds 		.fatal	= 1,
10601da177e4SLinus Torvalds 	},
106161e76b17SJiri Bohac 	{	/* POLICY_FAIL */
106261e76b17SJiri Bohac 		.err	= EACCES,
106361e76b17SJiri Bohac 		.fatal	= 1,
106461e76b17SJiri Bohac 	},
106561e76b17SJiri Bohac 	{	/* REJECT_ROUTE	*/
106661e76b17SJiri Bohac 		.err	= EACCES,
106761e76b17SJiri Bohac 		.fatal	= 1,
106861e76b17SJiri Bohac 	},
10691da177e4SLinus Torvalds };
10701da177e4SLinus Torvalds 
1071d5fdd6baSBrian Haley int icmpv6_err_convert(u8 type, u8 code, int *err)
10721da177e4SLinus Torvalds {
10731da177e4SLinus Torvalds 	int fatal = 0;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	*err = EPROTO;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	switch (type) {
10781da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
10791da177e4SLinus Torvalds 		fatal = 1;
108061e76b17SJiri Bohac 		if (code < ARRAY_SIZE(tab_unreach)) {
10811da177e4SLinus Torvalds 			*err  = tab_unreach[code].err;
10821da177e4SLinus Torvalds 			fatal = tab_unreach[code].fatal;
10831da177e4SLinus Torvalds 		}
10841da177e4SLinus Torvalds 		break;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
10871da177e4SLinus Torvalds 		*err = EMSGSIZE;
10881da177e4SLinus Torvalds 		break;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
10911da177e4SLinus Torvalds 		*err = EPROTO;
10921da177e4SLinus Torvalds 		fatal = 1;
10931da177e4SLinus Torvalds 		break;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
10961da177e4SLinus Torvalds 		*err = EHOSTUNREACH;
10971da177e4SLinus Torvalds 		break;
10983ff50b79SStephen Hemminger 	}
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	return fatal;
11011da177e4SLinus Torvalds }
11027159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert);
11037159039aSYOSHIFUJI Hideaki 
11041da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1105e8243534Sstephen hemminger static struct ctl_table ipv6_icmp_table_template[] = {
11061da177e4SLinus Torvalds 	{
11071da177e4SLinus Torvalds 		.procname	= "ratelimit",
110841a76906SDaniel Lezcano 		.data		= &init_net.ipv6.sysctl.icmpv6_time,
11091da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
11101da177e4SLinus Torvalds 		.mode		= 0644,
11116d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
11121da177e4SLinus Torvalds 	},
1113f8572d8fSEric W. Biederman 	{ },
11141da177e4SLinus Torvalds };
1115760f2d01SDaniel Lezcano 
11162c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
1117760f2d01SDaniel Lezcano {
1118760f2d01SDaniel Lezcano 	struct ctl_table *table;
1119760f2d01SDaniel Lezcano 
1120760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_icmp_table_template,
1121760f2d01SDaniel Lezcano 			sizeof(ipv6_icmp_table_template),
1122760f2d01SDaniel Lezcano 			GFP_KERNEL);
11235ee09105SYOSHIFUJI Hideaki 
1124c027aab4SEric W. Biederman 	if (table)
11255ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.icmpv6_time;
11265ee09105SYOSHIFUJI Hideaki 
1127760f2d01SDaniel Lezcano 	return table;
1128760f2d01SDaniel Lezcano }
11291da177e4SLinus Torvalds #endif
1130