xref: /openbmc/linux/net/ipv6/icmp.c (revision 0bc19985)
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 {
8475efc250SKefeng Wang 	return *this_cpu_ptr(net->ipv6.icmp_sk);
8598c6d1b2SDenis V. Lunev }
861da177e4SLinus Torvalds 
8732bbd879SStefano Brivio static int 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)
955f379ef5SGeorg Kohmann 		ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 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));
10332bbd879SStefano Brivio 
10432bbd879SStefano Brivio 	return 0;
1056f809da2SSteffen Klassert }
1066f809da2SSteffen Klassert 
107e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb);
1081da177e4SLinus Torvalds 
10941135cc8SAlexey Dobriyan static const struct inet6_protocol icmpv6_protocol = {
1101da177e4SLinus Torvalds 	.handler	=	icmpv6_rcv,
1116f809da2SSteffen Klassert 	.err_handler	=	icmpv6_err,
1128b7817f3SHerbert Xu 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1131da177e4SLinus Torvalds };
1141da177e4SLinus Torvalds 
1157ba91ecbSJesper Dangaard Brouer /* Called with BH disabled */
116fdc0bde9SDenis V. Lunev static __inline__ struct sock *icmpv6_xmit_lock(struct net *net)
1171da177e4SLinus Torvalds {
118fdc0bde9SDenis V. Lunev 	struct sock *sk;
119fdc0bde9SDenis V. Lunev 
120fdc0bde9SDenis V. Lunev 	sk = icmpv6_sk(net);
121405666dbSDenis V. Lunev 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
1221da177e4SLinus Torvalds 		/* This can happen if the output path (f.e. SIT or
1231da177e4SLinus Torvalds 		 * ip6ip6 tunnel) signals dst_link_failure() for an
1241da177e4SLinus Torvalds 		 * outgoing ICMP6 packet.
1251da177e4SLinus Torvalds 		 */
126fdc0bde9SDenis V. Lunev 		return NULL;
1271da177e4SLinus Torvalds 	}
128fdc0bde9SDenis V. Lunev 	return sk;
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
131405666dbSDenis V. Lunev static __inline__ void icmpv6_xmit_unlock(struct sock *sk)
1321da177e4SLinus Torvalds {
1337ba91ecbSJesper Dangaard Brouer 	spin_unlock(&sk->sk_lock.slock);
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /*
1371da177e4SLinus Torvalds  * Figure out, may we reply to this packet with icmp error.
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  * We do not reply, if:
1401da177e4SLinus Torvalds  *	- it was icmp error message.
1411da177e4SLinus Torvalds  *	- it is truncated, so that it is known, that protocol is ICMPV6
1421da177e4SLinus Torvalds  *	  (i.e. in the middle of some exthdr)
1431da177e4SLinus Torvalds  *
1441da177e4SLinus Torvalds  *	--ANK (980726)
1451da177e4SLinus Torvalds  */
1461da177e4SLinus Torvalds 
147a50feda5SEric Dumazet static bool is_ineligible(const struct sk_buff *skb)
1481da177e4SLinus Torvalds {
1490660e03fSArnaldo Carvalho de Melo 	int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
1501da177e4SLinus Torvalds 	int len = skb->len - ptr;
1510660e03fSArnaldo Carvalho de Melo 	__u8 nexthdr = ipv6_hdr(skb)->nexthdr;
15275f2811cSJesse Gross 	__be16 frag_off;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	if (len < 0)
155a50feda5SEric Dumazet 		return true;
1561da177e4SLinus Torvalds 
15775f2811cSJesse Gross 	ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off);
1581da177e4SLinus Torvalds 	if (ptr < 0)
159a50feda5SEric Dumazet 		return false;
1601da177e4SLinus Torvalds 	if (nexthdr == IPPROTO_ICMPV6) {
1611da177e4SLinus Torvalds 		u8 _type, *tp;
1621da177e4SLinus Torvalds 		tp = skb_header_pointer(skb,
1631da177e4SLinus Torvalds 			ptr+offsetof(struct icmp6hdr, icmp6_type),
1641da177e4SLinus Torvalds 			sizeof(_type), &_type);
16563159f29SIan Morris 		if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
166a50feda5SEric Dumazet 			return true;
1671da177e4SLinus Torvalds 	}
168a50feda5SEric Dumazet 	return false;
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1710bc19985SStephen Suryaputra static bool icmpv6_mask_allow(struct net *net, int type)
172c0303efeSJesper Dangaard Brouer {
1730bc19985SStephen Suryaputra 	if (type > ICMPV6_MSG_MAX)
174c0303efeSJesper Dangaard Brouer 		return true;
175c0303efeSJesper Dangaard Brouer 
1760bc19985SStephen Suryaputra 	/* Limit if icmp type is set in ratemask. */
1770bc19985SStephen Suryaputra 	if (!test_bit(type, net->ipv6.sysctl.icmpv6_ratemask))
178c0303efeSJesper Dangaard Brouer 		return true;
179c0303efeSJesper Dangaard Brouer 
180c0303efeSJesper Dangaard Brouer 	return false;
181c0303efeSJesper Dangaard Brouer }
182c0303efeSJesper Dangaard Brouer 
1830bc19985SStephen Suryaputra static bool icmpv6_global_allow(struct net *net, int type)
184c0303efeSJesper Dangaard Brouer {
1850bc19985SStephen Suryaputra 	if (icmpv6_mask_allow(net, type))
186c0303efeSJesper Dangaard Brouer 		return true;
187c0303efeSJesper Dangaard Brouer 
188c0303efeSJesper Dangaard Brouer 	if (icmp_global_allow())
189c0303efeSJesper Dangaard Brouer 		return true;
190c0303efeSJesper Dangaard Brouer 
191c0303efeSJesper Dangaard Brouer 	return false;
192c0303efeSJesper Dangaard Brouer }
193c0303efeSJesper Dangaard Brouer 
1941da177e4SLinus Torvalds /*
1951da177e4SLinus Torvalds  * Check the ICMP output rate limit
1961da177e4SLinus Torvalds  */
1974cdf507dSEric Dumazet static bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
1984c9483b2SDavid S. Miller 			       struct flowi6 *fl6)
1991da177e4SLinus Torvalds {
2003b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2014cdf507dSEric Dumazet 	struct dst_entry *dst;
20292d86829SDavid S. Miller 	bool res = false;
2031da177e4SLinus Torvalds 
2040bc19985SStephen Suryaputra 	if (icmpv6_mask_allow(net, type))
20592d86829SDavid S. Miller 		return true;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/*
2081da177e4SLinus Torvalds 	 * Look up the output route.
2091da177e4SLinus Torvalds 	 * XXX: perhaps the expire for routing entries cloned by
2101da177e4SLinus Torvalds 	 * this lookup should be more aggressive (not longer than timeout).
2111da177e4SLinus Torvalds 	 */
2124c9483b2SDavid S. Miller 	dst = ip6_route_output(net, sk, fl6);
2131da177e4SLinus Torvalds 	if (dst->error) {
2143bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst),
215a11d206dSYOSHIFUJI Hideaki 			      IPSTATS_MIB_OUTNOROUTES);
2161da177e4SLinus Torvalds 	} else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
21792d86829SDavid S. Miller 		res = true;
2181da177e4SLinus Torvalds 	} else {
2191da177e4SLinus Torvalds 		struct rt6_info *rt = (struct rt6_info *)dst;
2209a43b709SBenjamin Thery 		int tmo = net->ipv6.sysctl.icmpv6_time;
221c0303efeSJesper Dangaard Brouer 		struct inet_peer *peer;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 		/* Give more bandwidth to wider prefixes. */
2241da177e4SLinus Torvalds 		if (rt->rt6i_dst.plen < 128)
2251da177e4SLinus Torvalds 			tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
2261da177e4SLinus Torvalds 
227c0303efeSJesper Dangaard Brouer 		peer = inet_getpeer_v6(net->ipv6.peers, &fl6->daddr, 1);
228fbfe95a4SDavid S. Miller 		res = inet_peer_xrlim_allow(peer, tmo);
2291d861aa4SDavid S. Miller 		if (peer)
2301d861aa4SDavid S. Miller 			inet_putpeer(peer);
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 	dst_release(dst);
2331da177e4SLinus Torvalds 	return res;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /*
2371da177e4SLinus Torvalds  *	an inline helper for the "simple" if statement below
2381da177e4SLinus Torvalds  *	checks if parameter problem report is caused by an
2391da177e4SLinus Torvalds  *	unrecognized IPv6 option that has the Option Type
2401da177e4SLinus Torvalds  *	highest-order two bits set to 10
2411da177e4SLinus Torvalds  */
2421da177e4SLinus Torvalds 
243a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	u8 _optval, *op;
2461da177e4SLinus Torvalds 
247bbe735e4SArnaldo Carvalho de Melo 	offset += skb_network_offset(skb);
2481da177e4SLinus Torvalds 	op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
24963159f29SIan Morris 	if (!op)
250a50feda5SEric Dumazet 		return true;
2511da177e4SLinus Torvalds 	return (*op & 0xC0) == 0x80;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2544e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
2556d0bfe22SLorenzo Colitti 				struct icmp6hdr *thdr, int len)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	struct sk_buff *skb;
2581da177e4SLinus Torvalds 	struct icmp6hdr *icmp6h;
2591da177e4SLinus Torvalds 
260e5d08d71SIan Morris 	skb = skb_peek(&sk->sk_write_queue);
26163159f29SIan Morris 	if (!skb)
2624e64b1edSJoe Perches 		return;
2631da177e4SLinus Torvalds 
264cc70ab26SArnaldo Carvalho de Melo 	icmp6h = icmp6_hdr(skb);
2651da177e4SLinus Torvalds 	memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
2661da177e4SLinus Torvalds 	icmp6h->icmp6_cksum = 0;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
26907f0757aSJoe Perches 		skb->csum = csum_partial(icmp6h,
2701da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), skb->csum);
2714c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2724c9483b2SDavid S. Miller 						      &fl6->daddr,
2734c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
2741da177e4SLinus Torvalds 						      skb->csum);
2751da177e4SLinus Torvalds 	} else {
276868c86bcSAl Viro 		__wsum tmp_csum = 0;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
2791da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
2801da177e4SLinus Torvalds 		}
2811da177e4SLinus Torvalds 
28207f0757aSJoe Perches 		tmp_csum = csum_partial(icmp6h,
2831da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), tmp_csum);
2844c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2854c9483b2SDavid S. Miller 						      &fl6->daddr,
2864c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
287868c86bcSAl Viro 						      tmp_csum);
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 	ip6_push_pending_frames(sk);
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds struct icmpv6_msg {
2931da177e4SLinus Torvalds 	struct sk_buff	*skb;
2941da177e4SLinus Torvalds 	int		offset;
295763ecff1SYasuyuki Kozakai 	uint8_t		type;
2961da177e4SLinus Torvalds };
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
3011da177e4SLinus Torvalds 	struct sk_buff *org_skb = msg->skb;
3025f92a738SAl Viro 	__wsum csum = 0;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
3051da177e4SLinus Torvalds 				      to, len, csum);
3061da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
307763ecff1SYasuyuki Kozakai 	if (!(msg->type & ICMPV6_INFOMSG_MASK))
308763ecff1SYasuyuki Kozakai 		nf_ct_attach(skb, org_skb);
3091da177e4SLinus Torvalds 	return 0;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
31207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
31379383236SMasahide NAKAMURA static void mip6_addr_swap(struct sk_buff *skb)
31479383236SMasahide NAKAMURA {
3150660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
31679383236SMasahide NAKAMURA 	struct inet6_skb_parm *opt = IP6CB(skb);
31779383236SMasahide NAKAMURA 	struct ipv6_destopt_hao *hao;
31879383236SMasahide NAKAMURA 	struct in6_addr tmp;
31979383236SMasahide NAKAMURA 	int off;
32079383236SMasahide NAKAMURA 
32179383236SMasahide NAKAMURA 	if (opt->dsthao) {
32279383236SMasahide NAKAMURA 		off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
32379383236SMasahide NAKAMURA 		if (likely(off >= 0)) {
324d56f90a7SArnaldo Carvalho de Melo 			hao = (struct ipv6_destopt_hao *)
325d56f90a7SArnaldo Carvalho de Melo 					(skb_network_header(skb) + off);
3264e3fd7a0SAlexey Dobriyan 			tmp = iph->saddr;
3274e3fd7a0SAlexey Dobriyan 			iph->saddr = hao->addr;
3284e3fd7a0SAlexey Dobriyan 			hao->addr = tmp;
32979383236SMasahide NAKAMURA 		}
33079383236SMasahide NAKAMURA 	}
33179383236SMasahide NAKAMURA }
33279383236SMasahide NAKAMURA #else
33379383236SMasahide NAKAMURA static inline void mip6_addr_swap(struct sk_buff *skb) {}
33479383236SMasahide NAKAMURA #endif
33579383236SMasahide NAKAMURA 
336e8243534Sstephen hemminger static struct dst_entry *icmpv6_route_lookup(struct net *net,
337e8243534Sstephen hemminger 					     struct sk_buff *skb,
338e8243534Sstephen hemminger 					     struct sock *sk,
339e8243534Sstephen hemminger 					     struct flowi6 *fl6)
340b42835dbSDavid S. Miller {
341b42835dbSDavid S. Miller 	struct dst_entry *dst, *dst2;
3424c9483b2SDavid S. Miller 	struct flowi6 fl2;
343b42835dbSDavid S. Miller 	int err;
344b42835dbSDavid S. Miller 
345343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst, fl6);
346b42835dbSDavid S. Miller 	if (err)
347b42835dbSDavid S. Miller 		return ERR_PTR(err);
348b42835dbSDavid S. Miller 
349b42835dbSDavid S. Miller 	/*
350b42835dbSDavid S. Miller 	 * We won't send icmp if the destination is known
351b42835dbSDavid S. Miller 	 * anycast.
352b42835dbSDavid S. Miller 	 */
3532647a9b0SMartin KaFai Lau 	if (ipv6_anycast_destination(dst, &fl6->daddr)) {
354ba7a46f1SJoe Perches 		net_dbg_ratelimited("icmp6_send: acast source\n");
355b42835dbSDavid S. Miller 		dst_release(dst);
356b42835dbSDavid S. Miller 		return ERR_PTR(-EINVAL);
357b42835dbSDavid S. Miller 	}
358b42835dbSDavid S. Miller 
359b42835dbSDavid S. Miller 	/* No need to clone since we're just using its address. */
360b42835dbSDavid S. Miller 	dst2 = dst;
361b42835dbSDavid S. Miller 
3624c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
363452edd59SDavid S. Miller 	if (!IS_ERR(dst)) {
364b42835dbSDavid S. Miller 		if (dst != dst2)
365b42835dbSDavid S. Miller 			return dst;
366452edd59SDavid S. Miller 	} else {
367452edd59SDavid S. Miller 		if (PTR_ERR(dst) == -EPERM)
368b42835dbSDavid S. Miller 			dst = NULL;
369452edd59SDavid S. Miller 		else
370452edd59SDavid S. Miller 			return dst;
371b42835dbSDavid S. Miller 	}
372b42835dbSDavid S. Miller 
3734c9483b2SDavid S. Miller 	err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6);
374b42835dbSDavid S. Miller 	if (err)
375b42835dbSDavid S. Miller 		goto relookup_failed;
376b42835dbSDavid S. Miller 
377343d60aaSRoopa Prabhu 	err = ip6_dst_lookup(net, sk, &dst2, &fl2);
378b42835dbSDavid S. Miller 	if (err)
379b42835dbSDavid S. Miller 		goto relookup_failed;
380b42835dbSDavid S. Miller 
3814c9483b2SDavid S. Miller 	dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
382452edd59SDavid S. Miller 	if (!IS_ERR(dst2)) {
383b42835dbSDavid S. Miller 		dst_release(dst);
384b42835dbSDavid S. Miller 		dst = dst2;
385452edd59SDavid S. Miller 	} else {
386452edd59SDavid S. Miller 		err = PTR_ERR(dst2);
387452edd59SDavid S. Miller 		if (err == -EPERM) {
388b42835dbSDavid S. Miller 			dst_release(dst);
389452edd59SDavid S. Miller 			return dst2;
390452edd59SDavid S. Miller 		} else
391b42835dbSDavid S. Miller 			goto relookup_failed;
392b42835dbSDavid S. Miller 	}
393b42835dbSDavid S. Miller 
394b42835dbSDavid S. Miller relookup_failed:
395b42835dbSDavid S. Miller 	if (dst)
396b42835dbSDavid S. Miller 		return dst;
397b42835dbSDavid S. Miller 	return ERR_PTR(err);
398b42835dbSDavid S. Miller }
399b42835dbSDavid S. Miller 
4001b70d792SDavid Ahern static int icmp6_iif(const struct sk_buff *skb)
4011b70d792SDavid Ahern {
4021b70d792SDavid Ahern 	int iif = skb->dev->ifindex;
4031b70d792SDavid Ahern 
4041b70d792SDavid Ahern 	/* for local traffic to local address, skb dev is the loopback
4051b70d792SDavid Ahern 	 * device. Check if there is a dst attached to the skb and if so
40624b711edSDavid Ahern 	 * get the real device index. Same is needed for replies to a link
40724b711edSDavid Ahern 	 * local address on a device enslaved to an L3 master device
4081b70d792SDavid Ahern 	 */
40924b711edSDavid Ahern 	if (unlikely(iif == LOOPBACK_IFINDEX || netif_is_l3_master(skb->dev))) {
4101b70d792SDavid Ahern 		const struct rt6_info *rt6 = skb_rt6_info(skb);
4111b70d792SDavid Ahern 
4121b70d792SDavid Ahern 		if (rt6)
4131b70d792SDavid Ahern 			iif = rt6->rt6i_idev->dev->ifindex;
4141b70d792SDavid Ahern 	}
4151b70d792SDavid Ahern 
4161b70d792SDavid Ahern 	return iif;
4171b70d792SDavid Ahern }
4181b70d792SDavid Ahern 
4191da177e4SLinus Torvalds /*
4201da177e4SLinus Torvalds  *	Send an ICMP message in response to a packet in error
4211da177e4SLinus Torvalds  */
422b1cadc1aSEric Dumazet static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
423b1cadc1aSEric Dumazet 		       const struct in6_addr *force_saddr)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
4260660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
42784427d53SYOSHIFUJI Hideaki 	struct sock *sk;
4288d933670SEric Dumazet 	struct net *net;
42984427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
430b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
4311da177e4SLinus Torvalds 	struct dst_entry *dst;
4321da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
4334c9483b2SDavid S. Miller 	struct flowi6 fl6;
4341da177e4SLinus Torvalds 	struct icmpv6_msg msg;
43526879da5SWei Wang 	struct ipcm6_cookie ipc6;
4361da177e4SLinus Torvalds 	int iif = 0;
4371da177e4SLinus Torvalds 	int addr_type = 0;
4381da177e4SLinus Torvalds 	int len;
4398d933670SEric Dumazet 	u32 mark;
4401da177e4SLinus Torvalds 
44127a884dcSArnaldo Carvalho de Melo 	if ((u8 *)hdr < skb->head ||
44229a3cad5SSimon Horman 	    (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
4431da177e4SLinus Torvalds 		return;
4441da177e4SLinus Torvalds 
4458d933670SEric Dumazet 	if (!skb->dev)
4468d933670SEric Dumazet 		return;
4478d933670SEric Dumazet 	net = dev_net(skb->dev);
4488d933670SEric Dumazet 	mark = IP6_REPLY_MARK(net, skb->mark);
4491da177e4SLinus Torvalds 	/*
4501da177e4SLinus Torvalds 	 *	Make sure we respect the rules
4511da177e4SLinus Torvalds 	 *	i.e. RFC 1885 2.4(e)
4525f5624cfSPravin B Shelar 	 *	Rule (e.1) is enforced by not using icmp6_send
4531da177e4SLinus Torvalds 	 *	in any code that processes icmp errors.
4541da177e4SLinus Torvalds 	 */
4551da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->daddr);
4561da177e4SLinus Torvalds 
457446fab59SFX Le Bail 	if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) ||
458d94c1f92SFX Le Bail 	    ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr))
4591da177e4SLinus Torvalds 		saddr = &hdr->daddr;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	/*
4621da177e4SLinus Torvalds 	 *	Dest addr check
4631da177e4SLinus Torvalds 	 */
4641da177e4SLinus Torvalds 
4659a6b4b39Szhuyj 	if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) {
4661da177e4SLinus Torvalds 		if (type != ICMPV6_PKT_TOOBIG &&
4671da177e4SLinus Torvalds 		    !(type == ICMPV6_PARAMPROB &&
4681da177e4SLinus Torvalds 		      code == ICMPV6_UNK_OPTION &&
4691da177e4SLinus Torvalds 		      (opt_unrec(skb, info))))
4701da177e4SLinus Torvalds 			return;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 		saddr = NULL;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->saddr);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/*
4781da177e4SLinus Torvalds 	 *	Source addr check
4791da177e4SLinus Torvalds 	 */
4801da177e4SLinus Torvalds 
4814832c30dSDavid Ahern 	if (__ipv6_addr_needs_scope_id(addr_type)) {
4821b70d792SDavid Ahern 		iif = icmp6_iif(skb);
4834832c30dSDavid Ahern 	} else {
48479dc7e3fSDavid Ahern 		dst = skb_dst(skb);
48579dc7e3fSDavid Ahern 		iif = l3mdev_master_ifindex(dst ? dst->dev : skb->dev);
48679dc7e3fSDavid Ahern 	}
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	/*
4898de3351eSYOSHIFUJI Hideaki 	 *	Must not send error if the source does not uniquely
4908de3351eSYOSHIFUJI Hideaki 	 *	identify a single node (RFC2463 Section 2.4).
4918de3351eSYOSHIFUJI Hideaki 	 *	We check unspecified / multicast addresses here,
4928de3351eSYOSHIFUJI Hideaki 	 *	and anycast addresses will be checked later.
4931da177e4SLinus Torvalds 	 */
4941da177e4SLinus Torvalds 	if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
4954b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n",
4964b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
4971da177e4SLinus Torvalds 		return;
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	/*
5011da177e4SLinus Torvalds 	 *	Never answer to a ICMP packet.
5021da177e4SLinus Torvalds 	 */
5031da177e4SLinus Torvalds 	if (is_ineligible(skb)) {
5044b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n",
5054b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
5061da177e4SLinus Torvalds 		return;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
5097ba91ecbSJesper Dangaard Brouer 	/* Needed by both icmp_global_allow and icmpv6_xmit_lock */
5107ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
5117ba91ecbSJesper Dangaard Brouer 
5127ba91ecbSJesper Dangaard Brouer 	/* Check global sysctl_icmp_msgs_per_sec ratelimit */
5130bc19985SStephen Suryaputra 	if (!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, type))
5147ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
5157ba91ecbSJesper Dangaard Brouer 
51679383236SMasahide NAKAMURA 	mip6_addr_swap(skb);
51779383236SMasahide NAKAMURA 
5184c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
5194c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
5204e3fd7a0SAlexey Dobriyan 	fl6.daddr = hdr->saddr;
521b1cadc1aSEric Dumazet 	if (force_saddr)
522b1cadc1aSEric Dumazet 		saddr = force_saddr;
5231da177e4SLinus Torvalds 	if (saddr)
5244e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
525e110861fSLorenzo Colitti 	fl6.flowi6_mark = mark;
5264c9483b2SDavid S. Miller 	fl6.flowi6_oif = iif;
5271958b856SDavid S. Miller 	fl6.fl6_icmp_type = type;
5281958b856SDavid S. Miller 	fl6.fl6_icmp_code = code;
529e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
530b4bac172SDavid Ahern 	fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, NULL);
5314c9483b2SDavid S. Miller 	security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
5321da177e4SLinus Torvalds 
533fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
53463159f29SIan Morris 	if (!sk)
5357ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
536c0303efeSJesper Dangaard Brouer 
537e110861fSLorenzo Colitti 	sk->sk_mark = mark;
538fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
539405666dbSDenis V. Lunev 
5404c9483b2SDavid S. Miller 	if (!icmpv6_xrlim_allow(sk, type, &fl6))
5411da177e4SLinus Torvalds 		goto out;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = type;
5441da177e4SLinus Torvalds 	tmp_hdr.icmp6_code = code;
5451da177e4SLinus Torvalds 	tmp_hdr.icmp6_cksum = 0;
5461da177e4SLinus Torvalds 	tmp_hdr.icmp6_pointer = htonl(info);
5471da177e4SLinus Torvalds 
5484c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
5494c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
550c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
551c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
5521da177e4SLinus Torvalds 
553b515430aSWillem de Bruijn 	ipcm6_init_sk(&ipc6, np);
55438b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
55538b7097bSHannes Frederic Sowa 
5564c9483b2SDavid S. Miller 	dst = icmpv6_route_lookup(net, skb, sk, &fl6);
557b42835dbSDavid S. Miller 	if (IS_ERR(dst))
5581da177e4SLinus Torvalds 		goto out;
5598de3351eSYOSHIFUJI Hideaki 
56026879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	msg.skb = skb;
563bbe735e4SArnaldo Carvalho de Melo 	msg.offset = skb_network_offset(skb);
564763ecff1SYasuyuki Kozakai 	msg.type = type;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	len = skb->len - msg.offset;
5671da177e4SLinus Torvalds 	len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr));
5681da177e4SLinus Torvalds 	if (len < 0) {
5694b3418fbSBjørn Mork 		net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n",
5704b3418fbSBjørn Mork 				    &hdr->saddr, &hdr->daddr);
5711da177e4SLinus Torvalds 		goto out_dst_release;
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 
574cfdf7647SEric Dumazet 	rcu_read_lock();
575cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
5761da177e4SLinus Torvalds 
5774e64b1edSJoe Perches 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
5781da177e4SLinus Torvalds 			    len + sizeof(struct icmp6hdr),
57926879da5SWei Wang 			    sizeof(struct icmp6hdr),
58026879da5SWei Wang 			    &ipc6, &fl6, (struct rt6_info *)dst,
5815fdaa88dSWillem de Bruijn 			    MSG_DONTWAIT)) {
58243a43b60SHannes Frederic Sowa 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
5831da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
584cfdf7647SEric Dumazet 	} else {
5854e64b1edSJoe Perches 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
586cfdf7647SEric Dumazet 					   len + sizeof(struct icmp6hdr));
5871da177e4SLinus Torvalds 	}
588cfdf7647SEric Dumazet 	rcu_read_unlock();
5891da177e4SLinus Torvalds out_dst_release:
5901da177e4SLinus Torvalds 	dst_release(dst);
5911da177e4SLinus Torvalds out:
592405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
5937ba91ecbSJesper Dangaard Brouer out_bh_enable:
5947ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
5951da177e4SLinus Torvalds }
5965f5624cfSPravin B Shelar 
5975f5624cfSPravin B Shelar /* Slightly more convenient version of icmp6_send.
5985f5624cfSPravin B Shelar  */
5995f5624cfSPravin B Shelar void icmpv6_param_prob(struct sk_buff *skb, u8 code, int pos)
6005f5624cfSPravin B Shelar {
601b1cadc1aSEric Dumazet 	icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL);
6025f5624cfSPravin B Shelar 	kfree_skb(skb);
6035f5624cfSPravin B Shelar }
6047159039aSYOSHIFUJI Hideaki 
6055fbba8acSEric Dumazet /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
6065fbba8acSEric Dumazet  * if sufficient data bytes are available
6075fbba8acSEric Dumazet  * @nhs is the size of the tunnel header(s) :
6085fbba8acSEric Dumazet  *  Either an IPv4 header for SIT encap
6095fbba8acSEric Dumazet  *         an IPv4 header + GRE header for GRE encap
6105fbba8acSEric Dumazet  */
61120e1954fSEric Dumazet int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type,
61220e1954fSEric Dumazet 			       unsigned int data_len)
6135fbba8acSEric Dumazet {
6142d7a3b27SEric Dumazet 	struct in6_addr temp_saddr;
6155fbba8acSEric Dumazet 	struct rt6_info *rt;
6165fbba8acSEric Dumazet 	struct sk_buff *skb2;
61720e1954fSEric Dumazet 	u32 info = 0;
6185fbba8acSEric Dumazet 
6195fbba8acSEric Dumazet 	if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8))
6205fbba8acSEric Dumazet 		return 1;
6215fbba8acSEric Dumazet 
62220e1954fSEric Dumazet 	/* RFC 4884 (partial) support for ICMP extensions */
62320e1954fSEric Dumazet 	if (data_len < 128 || (data_len & 7) || skb->len < data_len)
62420e1954fSEric Dumazet 		data_len = 0;
62520e1954fSEric Dumazet 
62620e1954fSEric Dumazet 	skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC);
6275fbba8acSEric Dumazet 
6285fbba8acSEric Dumazet 	if (!skb2)
6295fbba8acSEric Dumazet 		return 1;
6305fbba8acSEric Dumazet 
6315fbba8acSEric Dumazet 	skb_dst_drop(skb2);
6325fbba8acSEric Dumazet 	skb_pull(skb2, nhs);
6335fbba8acSEric Dumazet 	skb_reset_network_header(skb2);
6345fbba8acSEric Dumazet 
635b75cc8f9SDavid Ahern 	rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0,
636b75cc8f9SDavid Ahern 			skb, 0);
6375fbba8acSEric Dumazet 
6385fbba8acSEric Dumazet 	if (rt && rt->dst.dev)
6395fbba8acSEric Dumazet 		skb2->dev = rt->dst.dev;
6405fbba8acSEric Dumazet 
6412d7a3b27SEric Dumazet 	ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr);
64220e1954fSEric Dumazet 
64320e1954fSEric Dumazet 	if (data_len) {
64420e1954fSEric Dumazet 		/* RFC 4884 (partial) support :
64520e1954fSEric Dumazet 		 * insert 0 padding at the end, before the extensions
64620e1954fSEric Dumazet 		 */
64720e1954fSEric Dumazet 		__skb_push(skb2, nhs);
64820e1954fSEric Dumazet 		skb_reset_network_header(skb2);
64920e1954fSEric Dumazet 		memmove(skb2->data, skb2->data + nhs, data_len - nhs);
65020e1954fSEric Dumazet 		memset(skb2->data + data_len - nhs, 0, nhs);
65120e1954fSEric Dumazet 		/* RFC 4884 4.5 : Length is measured in 64-bit words,
65220e1954fSEric Dumazet 		 * and stored in reserved[0]
65320e1954fSEric Dumazet 		 */
65420e1954fSEric Dumazet 		info = (data_len/8) << 24;
65520e1954fSEric Dumazet 	}
6562d7a3b27SEric Dumazet 	if (type == ICMP_TIME_EXCEEDED)
6572d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
65820e1954fSEric Dumazet 			   info, &temp_saddr);
6592d7a3b27SEric Dumazet 	else
6602d7a3b27SEric Dumazet 		icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH,
66120e1954fSEric Dumazet 			   info, &temp_saddr);
6625fbba8acSEric Dumazet 	if (rt)
6635fbba8acSEric Dumazet 		ip6_rt_put(rt);
6645fbba8acSEric Dumazet 
6655fbba8acSEric Dumazet 	kfree_skb(skb2);
6665fbba8acSEric Dumazet 
6675fbba8acSEric Dumazet 	return 0;
6685fbba8acSEric Dumazet }
6695fbba8acSEric Dumazet EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach);
6705fbba8acSEric Dumazet 
6711da177e4SLinus Torvalds static void icmpv6_echo_reply(struct sk_buff *skb)
6721da177e4SLinus Torvalds {
673c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
67484427d53SYOSHIFUJI Hideaki 	struct sock *sk;
6751da177e4SLinus Torvalds 	struct inet6_dev *idev;
67684427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
677b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
678cc70ab26SArnaldo Carvalho de Melo 	struct icmp6hdr *icmph = icmp6_hdr(skb);
6791da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
6804c9483b2SDavid S. Miller 	struct flowi6 fl6;
6811da177e4SLinus Torvalds 	struct icmpv6_msg msg;
6821da177e4SLinus Torvalds 	struct dst_entry *dst;
68326879da5SWei Wang 	struct ipcm6_cookie ipc6;
684e110861fSLorenzo Colitti 	u32 mark = IP6_REPLY_MARK(net, skb->mark);
6850b03a5caSStephen Suryaputra 	bool acast;
6861da177e4SLinus Torvalds 
68703f1ecccSStephen Suryaputra 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
68803f1ecccSStephen Suryaputra 	    net->ipv6.sysctl.icmpv6_echo_ignore_multicast)
68903f1ecccSStephen Suryaputra 		return;
69003f1ecccSStephen Suryaputra 
6910660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->daddr;
6921da177e4SLinus Torvalds 
6930b03a5caSStephen Suryaputra 	acast = ipv6_anycast_destination(skb_dst(skb), saddr);
6940b03a5caSStephen Suryaputra 	if (acast && net->ipv6.sysctl.icmpv6_echo_ignore_anycast)
6950b03a5caSStephen Suryaputra 		return;
6960b03a5caSStephen Suryaputra 
697509aba3bSFX Le Bail 	if (!ipv6_unicast_destination(skb) &&
6980b03a5caSStephen Suryaputra 	    !(net->ipv6.sysctl.anycast_src_echo_reply && acast))
6991da177e4SLinus Torvalds 		saddr = NULL;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
7021da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = ICMPV6_ECHO_REPLY;
7031da177e4SLinus Torvalds 
7044c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
7054c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
7064e3fd7a0SAlexey Dobriyan 	fl6.daddr = ipv6_hdr(skb)->saddr;
7071da177e4SLinus Torvalds 	if (saddr)
7084e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
7091b70d792SDavid Ahern 	fl6.flowi6_oif = icmp6_iif(skb);
7101958b856SDavid S. Miller 	fl6.fl6_icmp_type = ICMPV6_ECHO_REPLY;
711e110861fSLorenzo Colitti 	fl6.flowi6_mark = mark;
712e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
7134c9483b2SDavid S. Miller 	security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
7141da177e4SLinus Torvalds 
7157ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
716fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
71763159f29SIan Morris 	if (!sk)
7187ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
719e110861fSLorenzo Colitti 	sk->sk_mark = mark;
720fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
721405666dbSDenis V. Lunev 
7224c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
7234c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
724c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
725c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
7261da177e4SLinus Torvalds 
7274e64b1edSJoe Perches 	if (ip6_dst_lookup(net, sk, &dst, &fl6))
7281da177e4SLinus Torvalds 		goto out;
7294c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
730452edd59SDavid S. Miller 	if (IS_ERR(dst))
731e104411bSPatrick McHardy 		goto out;
7321da177e4SLinus Torvalds 
7330bc19985SStephen Suryaputra 	/* Check the ratelimit */
7340bc19985SStephen Suryaputra 	if ((!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, ICMPV6_ECHO_REPLY)) ||
7350bc19985SStephen Suryaputra 	    !icmpv6_xrlim_allow(sk, ICMPV6_ECHO_REPLY, &fl6))
7360bc19985SStephen Suryaputra 		goto out_dst_release;
7370bc19985SStephen Suryaputra 
738cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	msg.skb = skb;
7411da177e4SLinus Torvalds 	msg.offset = 0;
742763ecff1SYasuyuki Kozakai 	msg.type = ICMPV6_ECHO_REPLY;
7431da177e4SLinus Torvalds 
744b515430aSWillem de Bruijn 	ipcm6_init_sk(&ipc6, np);
74526879da5SWei Wang 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
74626879da5SWei Wang 	ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb));
74726879da5SWei Wang 
7484e64b1edSJoe Perches 	if (ip6_append_data(sk, icmpv6_getfrag, &msg,
7494e64b1edSJoe Perches 			    skb->len + sizeof(struct icmp6hdr),
75026879da5SWei Wang 			    sizeof(struct icmp6hdr), &ipc6, &fl6,
7515fdaa88dSWillem de Bruijn 			    (struct rt6_info *)dst, MSG_DONTWAIT)) {
752a16292a0SEric Dumazet 		__ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
7531da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
754cfdf7647SEric Dumazet 	} else {
7554e64b1edSJoe Perches 		icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
756cfdf7647SEric Dumazet 					   skb->len + sizeof(struct icmp6hdr));
7571da177e4SLinus Torvalds 	}
7580bc19985SStephen Suryaputra out_dst_release:
7591da177e4SLinus Torvalds 	dst_release(dst);
7601da177e4SLinus Torvalds out:
761405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
7627ba91ecbSJesper Dangaard Brouer out_bh_enable:
7637ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
766b94f1c09SDavid S. Miller void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
7671da177e4SLinus Torvalds {
76841135cc8SAlexey Dobriyan 	const struct inet6_protocol *ipprot;
7691da177e4SLinus Torvalds 	int inner_offset;
77075f2811cSJesse Gross 	__be16 frag_off;
771f9242b6bSDavid S. Miller 	u8 nexthdr;
7727304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
7757304fe46SDuan Jiong 		goto out;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
7781da177e4SLinus Torvalds 	if (ipv6_ext_hdr(nexthdr)) {
7791da177e4SLinus Torvalds 		/* now skip over extension headers */
78075f2811cSJesse Gross 		inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
78175f2811cSJesse Gross 						&nexthdr, &frag_off);
7821da177e4SLinus Torvalds 		if (inner_offset < 0)
7837304fe46SDuan Jiong 			goto out;
7841da177e4SLinus Torvalds 	} else {
7851da177e4SLinus Torvalds 		inner_offset = sizeof(struct ipv6hdr);
7861da177e4SLinus Torvalds 	}
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	/* Checkin header including 8 bytes of inner protocol header. */
7891da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, inner_offset+8))
7907304fe46SDuan Jiong 		goto out;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	/* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
7931da177e4SLinus Torvalds 	   Without this we will not able f.e. to make source routed
7941da177e4SLinus Torvalds 	   pmtu discovery.
7951da177e4SLinus Torvalds 	   Corresponding argument (opt) to notifiers is already added.
7961da177e4SLinus Torvalds 	   --ANK (980726)
7971da177e4SLinus Torvalds 	 */
7981da177e4SLinus Torvalds 
799f9242b6bSDavid S. Miller 	ipprot = rcu_dereference(inet6_protos[nexthdr]);
8001da177e4SLinus Torvalds 	if (ipprot && ipprot->err_handler)
8011da177e4SLinus Torvalds 		ipprot->err_handler(skb, NULL, type, code, inner_offset, info);
8021da177e4SLinus Torvalds 
80369d6da0bSPavel Emelyanov 	raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
8047304fe46SDuan Jiong 	return;
8057304fe46SDuan Jiong 
8067304fe46SDuan Jiong out:
807a16292a0SEric Dumazet 	__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds /*
8111da177e4SLinus Torvalds  *	Handle icmp messages
8121da177e4SLinus Torvalds  */
8131da177e4SLinus Torvalds 
814e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb)
8151da177e4SLinus Torvalds {
816e6f86b0fSVirgile Jarry 	struct net *net = dev_net(skb->dev);
8171da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
8181da177e4SLinus Torvalds 	struct inet6_dev *idev = __in6_dev_get(dev);
819b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
8201da177e4SLinus Torvalds 	struct icmp6hdr *hdr;
821d5fdd6baSBrian Haley 	u8 type;
822e3e32170SRick Jones 	bool success = false;
8231da177e4SLinus Torvalds 
824aebcf82cSHerbert Xu 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
825def8b4faSAlexey Dobriyan 		struct sec_path *sp = skb_sec_path(skb);
8268b7817f3SHerbert Xu 		int nh;
8278b7817f3SHerbert Xu 
828def8b4faSAlexey Dobriyan 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
829aebcf82cSHerbert Xu 				 XFRM_STATE_ICMP))
830aebcf82cSHerbert Xu 			goto drop_no_count;
831aebcf82cSHerbert Xu 
83281aded24SDavid S. Miller 		if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
8338b7817f3SHerbert Xu 			goto drop_no_count;
8348b7817f3SHerbert Xu 
8358b7817f3SHerbert Xu 		nh = skb_network_offset(skb);
8368b7817f3SHerbert Xu 		skb_set_network_header(skb, sizeof(*hdr));
8378b7817f3SHerbert Xu 
8388b7817f3SHerbert Xu 		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
8398b7817f3SHerbert Xu 			goto drop_no_count;
8408b7817f3SHerbert Xu 
8418b7817f3SHerbert Xu 		skb_set_network_header(skb, nh);
8428b7817f3SHerbert Xu 	}
8438b7817f3SHerbert Xu 
844a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INMSGS);
8451da177e4SLinus Torvalds 
8460660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
8470660e03fSArnaldo Carvalho de Melo 	daddr = &ipv6_hdr(skb)->daddr;
8481da177e4SLinus Torvalds 
84939471ac8STom Herbert 	if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) {
850ba7a46f1SJoe Perches 		net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n",
8510c6ce78aSHarvey Harrison 				    saddr, daddr);
8526a5dc9e5SEric Dumazet 		goto csum_error;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8558cf22943SHerbert Xu 	if (!pskb_pull(skb, sizeof(*hdr)))
8568cf22943SHerbert Xu 		goto discard_it;
8571da177e4SLinus Torvalds 
858cc70ab26SArnaldo Carvalho de Melo 	hdr = icmp6_hdr(skb);
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	type = hdr->icmp6_type;
8611da177e4SLinus Torvalds 
862f3832ed2SEric Dumazet 	ICMP6MSGIN_INC_STATS(dev_net(dev), idev, type);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	switch (type) {
8651da177e4SLinus Torvalds 	case ICMPV6_ECHO_REQUEST:
866e6f86b0fSVirgile Jarry 		if (!net->ipv6.sysctl.icmpv6_echo_ignore_all)
8671da177e4SLinus Torvalds 			icmpv6_echo_reply(skb);
8681da177e4SLinus Torvalds 		break;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	case ICMPV6_ECHO_REPLY:
871e3e32170SRick Jones 		success = ping_rcv(skb);
8721da177e4SLinus Torvalds 		break;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
8751da177e4SLinus Torvalds 		/* BUGGG_FUTURE: if packet contains rthdr, we cannot update
8761da177e4SLinus Torvalds 		   standard destination cache. Seems, only "advanced"
8771da177e4SLinus Torvalds 		   destination cache will allow to solve this problem
8781da177e4SLinus Torvalds 		   --ANK (980726)
8791da177e4SLinus Torvalds 		 */
8801da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
8811da177e4SLinus Torvalds 			goto discard_it;
882cc70ab26SArnaldo Carvalho de Melo 		hdr = icmp6_hdr(skb);
8831da177e4SLinus Torvalds 
884275757e6SGustavo A. R. Silva 		/* to notify */
885275757e6SGustavo A. R. Silva 		/* fall through */
8861da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
8871da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
8881da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
8891da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
8901da177e4SLinus Torvalds 		break;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	case NDISC_ROUTER_SOLICITATION:
8931da177e4SLinus Torvalds 	case NDISC_ROUTER_ADVERTISEMENT:
8941da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_SOLICITATION:
8951da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_ADVERTISEMENT:
8961da177e4SLinus Torvalds 	case NDISC_REDIRECT:
8971da177e4SLinus Torvalds 		ndisc_rcv(skb);
8981da177e4SLinus Torvalds 		break;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	case ICMPV6_MGM_QUERY:
9011da177e4SLinus Torvalds 		igmp6_event_query(skb);
9021da177e4SLinus Torvalds 		break;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	case ICMPV6_MGM_REPORT:
9051da177e4SLinus Torvalds 		igmp6_event_report(skb);
9061da177e4SLinus Torvalds 		break;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	case ICMPV6_MGM_REDUCTION:
9091da177e4SLinus Torvalds 	case ICMPV6_NI_QUERY:
9101da177e4SLinus Torvalds 	case ICMPV6_NI_REPLY:
9111da177e4SLinus Torvalds 	case ICMPV6_MLD2_REPORT:
9121da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REQUEST:
9131da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REPLY:
9141da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_SOL:
9151da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_ADV:
9161da177e4SLinus Torvalds 		break;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	default:
9191da177e4SLinus Torvalds 		/* informational */
9201da177e4SLinus Torvalds 		if (type & ICMPV6_INFOMSG_MASK)
9211da177e4SLinus Torvalds 			break;
9221da177e4SLinus Torvalds 
9234b3418fbSBjørn Mork 		net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n",
9244b3418fbSBjørn Mork 				    saddr, daddr);
925ea85a0a2SDavid S. Miller 
9261da177e4SLinus Torvalds 		/*
9271da177e4SLinus Torvalds 		 * error of unknown type.
9281da177e4SLinus Torvalds 		 * must pass to upper level
9291da177e4SLinus Torvalds 		 */
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
9323ff50b79SStephen Hemminger 	}
9333ff50b79SStephen Hemminger 
934e3e32170SRick Jones 	/* until the v6 path can be better sorted assume failure and
935e3e32170SRick Jones 	 * preserve the status quo behaviour for the rest of the paths to here
936e3e32170SRick Jones 	 */
937e3e32170SRick Jones 	if (success)
938e3e32170SRick Jones 		consume_skb(skb);
939e3e32170SRick Jones 	else
9401da177e4SLinus Torvalds 		kfree_skb(skb);
941e3e32170SRick Jones 
9421da177e4SLinus Torvalds 	return 0;
9431da177e4SLinus Torvalds 
9446a5dc9e5SEric Dumazet csum_error:
945a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS);
9461da177e4SLinus Torvalds discard_it:
947a16292a0SEric Dumazet 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INERRORS);
9488b7817f3SHerbert Xu drop_no_count:
9491da177e4SLinus Torvalds 	kfree_skb(skb);
9501da177e4SLinus Torvalds 	return 0;
9511da177e4SLinus Torvalds }
9521da177e4SLinus Torvalds 
9534c9483b2SDavid S. Miller void icmpv6_flow_init(struct sock *sk, struct flowi6 *fl6,
95495e41e93SYOSHIFUJI Hideaki 		      u8 type,
95595e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *saddr,
95695e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *daddr,
95795e41e93SYOSHIFUJI Hideaki 		      int oif)
95895e41e93SYOSHIFUJI Hideaki {
9594c9483b2SDavid S. Miller 	memset(fl6, 0, sizeof(*fl6));
9604e3fd7a0SAlexey Dobriyan 	fl6->saddr = *saddr;
9614e3fd7a0SAlexey Dobriyan 	fl6->daddr = *daddr;
9624c9483b2SDavid S. Miller 	fl6->flowi6_proto	= IPPROTO_ICMPV6;
9631958b856SDavid S. Miller 	fl6->fl6_icmp_type	= type;
9641958b856SDavid S. Miller 	fl6->fl6_icmp_code	= 0;
9654c9483b2SDavid S. Miller 	fl6->flowi6_oif		= oif;
9664c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
96795e41e93SYOSHIFUJI Hideaki }
96895e41e93SYOSHIFUJI Hideaki 
9693232a1efSKefeng Wang static void __net_exit icmpv6_sk_exit(struct net *net)
9703232a1efSKefeng Wang {
9713232a1efSKefeng Wang 	int i;
9723232a1efSKefeng Wang 
9733232a1efSKefeng Wang 	for_each_possible_cpu(i)
97475efc250SKefeng Wang 		inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv6.icmp_sk, i));
97575efc250SKefeng Wang 	free_percpu(net->ipv6.icmp_sk);
9763232a1efSKefeng Wang }
9773232a1efSKefeng Wang 
97898c6d1b2SDenis V. Lunev static int __net_init icmpv6_sk_init(struct net *net)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	struct sock *sk;
9813232a1efSKefeng Wang 	int err, i;
9821da177e4SLinus Torvalds 
98375efc250SKefeng Wang 	net->ipv6.icmp_sk = alloc_percpu(struct sock *);
98463159f29SIan Morris 	if (!net->ipv6.icmp_sk)
98579c91159SDenis V. Lunev 		return -ENOMEM;
98679c91159SDenis V. Lunev 
9876f912042SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
9881ed8516fSDenis V. Lunev 		err = inet_ctl_sock_create(&sk, PF_INET6,
9891ed8516fSDenis V. Lunev 					   SOCK_RAW, IPPROTO_ICMPV6, net);
9901da177e4SLinus Torvalds 		if (err < 0) {
991f3213831SJoe Perches 			pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
9921da177e4SLinus Torvalds 			       err);
9931da177e4SLinus Torvalds 			goto fail;
9941da177e4SLinus Torvalds 		}
9951da177e4SLinus Torvalds 
99675efc250SKefeng Wang 		*per_cpu_ptr(net->ipv6.icmp_sk, i) = sk;
9975c8cafd6SDenis V. Lunev 
9981da177e4SLinus Torvalds 		/* Enough space for 2 64K ICMP packets, including
9991da177e4SLinus Torvalds 		 * sk_buff struct overhead.
10001da177e4SLinus Torvalds 		 */
100187fb4b7bSEric Dumazet 		sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 	return 0;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds  fail:
10063232a1efSKefeng Wang 	icmpv6_sk_exit(net);
10071da177e4SLinus Torvalds 	return err;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10108ed7edceSAlexey Dobriyan static struct pernet_operations icmpv6_sk_ops = {
101198c6d1b2SDenis V. Lunev 	.init = icmpv6_sk_init,
101298c6d1b2SDenis V. Lunev 	.exit = icmpv6_sk_exit,
101398c6d1b2SDenis V. Lunev };
101498c6d1b2SDenis V. Lunev 
101598c6d1b2SDenis V. Lunev int __init icmpv6_init(void)
101698c6d1b2SDenis V. Lunev {
101798c6d1b2SDenis V. Lunev 	int err;
101898c6d1b2SDenis V. Lunev 
101998c6d1b2SDenis V. Lunev 	err = register_pernet_subsys(&icmpv6_sk_ops);
102098c6d1b2SDenis V. Lunev 	if (err < 0)
102198c6d1b2SDenis V. Lunev 		return err;
102298c6d1b2SDenis V. Lunev 
102398c6d1b2SDenis V. Lunev 	err = -EAGAIN;
102498c6d1b2SDenis V. Lunev 	if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
102598c6d1b2SDenis V. Lunev 		goto fail;
10265f5624cfSPravin B Shelar 
10275f5624cfSPravin B Shelar 	err = inet6_register_icmp_sender(icmp6_send);
10285f5624cfSPravin B Shelar 	if (err)
10295f5624cfSPravin B Shelar 		goto sender_reg_err;
103098c6d1b2SDenis V. Lunev 	return 0;
103198c6d1b2SDenis V. Lunev 
10325f5624cfSPravin B Shelar sender_reg_err:
10335f5624cfSPravin B Shelar 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
103498c6d1b2SDenis V. Lunev fail:
1035f3213831SJoe Perches 	pr_err("Failed to register ICMP6 protocol\n");
103698c6d1b2SDenis V. Lunev 	unregister_pernet_subsys(&icmpv6_sk_ops);
103798c6d1b2SDenis V. Lunev 	return err;
103898c6d1b2SDenis V. Lunev }
103998c6d1b2SDenis V. Lunev 
10408ed7edceSAlexey Dobriyan void icmpv6_cleanup(void)
104198c6d1b2SDenis V. Lunev {
10425f5624cfSPravin B Shelar 	inet6_unregister_icmp_sender(icmp6_send);
104398c6d1b2SDenis V. Lunev 	unregister_pernet_subsys(&icmpv6_sk_ops);
10441da177e4SLinus Torvalds 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
104798c6d1b2SDenis V. Lunev 
10489b5b5cffSArjan van de Ven static const struct icmp6_err {
10491da177e4SLinus Torvalds 	int err;
10501da177e4SLinus Torvalds 	int fatal;
10511da177e4SLinus Torvalds } tab_unreach[] = {
10521da177e4SLinus Torvalds 	{	/* NOROUTE */
10531da177e4SLinus Torvalds 		.err	= ENETUNREACH,
10541da177e4SLinus Torvalds 		.fatal	= 0,
10551da177e4SLinus Torvalds 	},
10561da177e4SLinus Torvalds 	{	/* ADM_PROHIBITED */
10571da177e4SLinus Torvalds 		.err	= EACCES,
10581da177e4SLinus Torvalds 		.fatal	= 1,
10591da177e4SLinus Torvalds 	},
10601da177e4SLinus Torvalds 	{	/* Was NOT_NEIGHBOUR, now reserved */
10611da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
10621da177e4SLinus Torvalds 		.fatal	= 0,
10631da177e4SLinus Torvalds 	},
10641da177e4SLinus Torvalds 	{	/* ADDR_UNREACH	*/
10651da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
10661da177e4SLinus Torvalds 		.fatal	= 0,
10671da177e4SLinus Torvalds 	},
10681da177e4SLinus Torvalds 	{	/* PORT_UNREACH	*/
10691da177e4SLinus Torvalds 		.err	= ECONNREFUSED,
10701da177e4SLinus Torvalds 		.fatal	= 1,
10711da177e4SLinus Torvalds 	},
107261e76b17SJiri Bohac 	{	/* POLICY_FAIL */
107361e76b17SJiri Bohac 		.err	= EACCES,
107461e76b17SJiri Bohac 		.fatal	= 1,
107561e76b17SJiri Bohac 	},
107661e76b17SJiri Bohac 	{	/* REJECT_ROUTE	*/
107761e76b17SJiri Bohac 		.err	= EACCES,
107861e76b17SJiri Bohac 		.fatal	= 1,
107961e76b17SJiri Bohac 	},
10801da177e4SLinus Torvalds };
10811da177e4SLinus Torvalds 
1082d5fdd6baSBrian Haley int icmpv6_err_convert(u8 type, u8 code, int *err)
10831da177e4SLinus Torvalds {
10841da177e4SLinus Torvalds 	int fatal = 0;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	*err = EPROTO;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	switch (type) {
10891da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
10901da177e4SLinus Torvalds 		fatal = 1;
109161e76b17SJiri Bohac 		if (code < ARRAY_SIZE(tab_unreach)) {
10921da177e4SLinus Torvalds 			*err  = tab_unreach[code].err;
10931da177e4SLinus Torvalds 			fatal = tab_unreach[code].fatal;
10941da177e4SLinus Torvalds 		}
10951da177e4SLinus Torvalds 		break;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
10981da177e4SLinus Torvalds 		*err = EMSGSIZE;
10991da177e4SLinus Torvalds 		break;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
11021da177e4SLinus Torvalds 		*err = EPROTO;
11031da177e4SLinus Torvalds 		fatal = 1;
11041da177e4SLinus Torvalds 		break;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
11071da177e4SLinus Torvalds 		*err = EHOSTUNREACH;
11081da177e4SLinus Torvalds 		break;
11093ff50b79SStephen Hemminger 	}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	return fatal;
11121da177e4SLinus Torvalds }
11137159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert);
11147159039aSYOSHIFUJI Hideaki 
11151da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1116e8243534Sstephen hemminger static struct ctl_table ipv6_icmp_table_template[] = {
11171da177e4SLinus Torvalds 	{
11181da177e4SLinus Torvalds 		.procname	= "ratelimit",
111941a76906SDaniel Lezcano 		.data		= &init_net.ipv6.sysctl.icmpv6_time,
11201da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
11211da177e4SLinus Torvalds 		.mode		= 0644,
11226d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
11231da177e4SLinus Torvalds 	},
1124e6f86b0fSVirgile Jarry 	{
1125e6f86b0fSVirgile Jarry 		.procname	= "echo_ignore_all",
1126e6f86b0fSVirgile Jarry 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_all,
1127e6f86b0fSVirgile Jarry 		.maxlen		= sizeof(int),
1128e6f86b0fSVirgile Jarry 		.mode		= 0644,
1129e6f86b0fSVirgile Jarry 		.proc_handler = proc_dointvec,
1130e6f86b0fSVirgile Jarry 	},
113103f1ecccSStephen Suryaputra 	{
113203f1ecccSStephen Suryaputra 		.procname	= "echo_ignore_multicast",
113303f1ecccSStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_multicast,
113403f1ecccSStephen Suryaputra 		.maxlen		= sizeof(int),
113503f1ecccSStephen Suryaputra 		.mode		= 0644,
113603f1ecccSStephen Suryaputra 		.proc_handler = proc_dointvec,
113703f1ecccSStephen Suryaputra 	},
11380b03a5caSStephen Suryaputra 	{
11390b03a5caSStephen Suryaputra 		.procname	= "echo_ignore_anycast",
11400b03a5caSStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_echo_ignore_anycast,
11410b03a5caSStephen Suryaputra 		.maxlen		= sizeof(int),
11420b03a5caSStephen Suryaputra 		.mode		= 0644,
11430b03a5caSStephen Suryaputra 		.proc_handler = proc_dointvec,
11440b03a5caSStephen Suryaputra 	},
11450bc19985SStephen Suryaputra 	{
11460bc19985SStephen Suryaputra 		.procname	= "ratemask",
11470bc19985SStephen Suryaputra 		.data		= &init_net.ipv6.sysctl.icmpv6_ratemask_ptr,
11480bc19985SStephen Suryaputra 		.maxlen		= ICMPV6_MSG_MAX + 1,
11490bc19985SStephen Suryaputra 		.mode		= 0644,
11500bc19985SStephen Suryaputra 		.proc_handler = proc_do_large_bitmap,
11510bc19985SStephen Suryaputra 	},
1152f8572d8fSEric W. Biederman 	{ },
11531da177e4SLinus Torvalds };
1154760f2d01SDaniel Lezcano 
11552c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
1156760f2d01SDaniel Lezcano {
1157760f2d01SDaniel Lezcano 	struct ctl_table *table;
1158760f2d01SDaniel Lezcano 
1159760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_icmp_table_template,
1160760f2d01SDaniel Lezcano 			sizeof(ipv6_icmp_table_template),
1161760f2d01SDaniel Lezcano 			GFP_KERNEL);
11625ee09105SYOSHIFUJI Hideaki 
1163e6f86b0fSVirgile Jarry 	if (table) {
11645ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.icmpv6_time;
1165e6f86b0fSVirgile Jarry 		table[1].data = &net->ipv6.sysctl.icmpv6_echo_ignore_all;
116603f1ecccSStephen Suryaputra 		table[2].data = &net->ipv6.sysctl.icmpv6_echo_ignore_multicast;
11670b03a5caSStephen Suryaputra 		table[3].data = &net->ipv6.sysctl.icmpv6_echo_ignore_anycast;
11680bc19985SStephen Suryaputra 		table[4].data = &net->ipv6.sysctl.icmpv6_ratemask_ptr;
1169e6f86b0fSVirgile Jarry 	}
1170760f2d01SDaniel Lezcano 	return table;
1171760f2d01SDaniel Lezcano }
11721da177e4SLinus Torvalds #endif
1173