xref: /openbmc/linux/net/ipv6/icmp.c (revision 61e76b17)
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>
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds #include <asm/uaccess.h>
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  *	The ICMP socket(s). This is the most convenient way to flow control
751da177e4SLinus Torvalds  *	our ICMP output as well as maintain a clean interface throughout
761da177e4SLinus Torvalds  *	all layers. All Socketless IP sends will soon be gone.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  *	On SMP we have one ICMP socket per-cpu.
791da177e4SLinus Torvalds  */
8098c6d1b2SDenis V. Lunev static inline struct sock *icmpv6_sk(struct net *net)
8198c6d1b2SDenis V. Lunev {
8298c6d1b2SDenis V. Lunev 	return net->ipv6.icmp_sk[smp_processor_id()];
8398c6d1b2SDenis V. Lunev }
841da177e4SLinus Torvalds 
856f809da2SSteffen Klassert static void icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
866f809da2SSteffen Klassert 		       u8 type, u8 code, int offset, __be32 info)
876f809da2SSteffen Klassert {
886d0bfe22SLorenzo Colitti 	/* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */
896d0bfe22SLorenzo Colitti 	struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset);
906f809da2SSteffen Klassert 	struct net *net = dev_net(skb->dev);
916f809da2SSteffen Klassert 
926f809da2SSteffen Klassert 	if (type == ICMPV6_PKT_TOOBIG)
936f809da2SSteffen Klassert 		ip6_update_pmtu(skb, net, info, 0, 0);
946f809da2SSteffen Klassert 	else if (type == NDISC_REDIRECT)
956f809da2SSteffen Klassert 		ip6_redirect(skb, net, 0, 0);
966d0bfe22SLorenzo Colitti 
976d0bfe22SLorenzo Colitti 	if (!(type & ICMPV6_INFOMSG_MASK))
986d0bfe22SLorenzo Colitti 		if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST)
996d0bfe22SLorenzo Colitti 			ping_err(skb, offset, info);
1006f809da2SSteffen Klassert }
1016f809da2SSteffen Klassert 
102e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb);
1031da177e4SLinus Torvalds 
10441135cc8SAlexey Dobriyan static const struct inet6_protocol icmpv6_protocol = {
1051da177e4SLinus Torvalds 	.handler	=	icmpv6_rcv,
1066f809da2SSteffen Klassert 	.err_handler	=	icmpv6_err,
1078b7817f3SHerbert Xu 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
110fdc0bde9SDenis V. Lunev static __inline__ struct sock *icmpv6_xmit_lock(struct net *net)
1111da177e4SLinus Torvalds {
112fdc0bde9SDenis V. Lunev 	struct sock *sk;
113fdc0bde9SDenis V. Lunev 
1141da177e4SLinus Torvalds 	local_bh_disable();
1151da177e4SLinus Torvalds 
116fdc0bde9SDenis V. Lunev 	sk = icmpv6_sk(net);
117405666dbSDenis V. Lunev 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
1181da177e4SLinus Torvalds 		/* This can happen if the output path (f.e. SIT or
1191da177e4SLinus Torvalds 		 * ip6ip6 tunnel) signals dst_link_failure() for an
1201da177e4SLinus Torvalds 		 * outgoing ICMP6 packet.
1211da177e4SLinus Torvalds 		 */
1221da177e4SLinus Torvalds 		local_bh_enable();
123fdc0bde9SDenis V. Lunev 		return NULL;
1241da177e4SLinus Torvalds 	}
125fdc0bde9SDenis V. Lunev 	return sk;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
128405666dbSDenis V. Lunev static __inline__ void icmpv6_xmit_unlock(struct sock *sk)
1291da177e4SLinus Torvalds {
130405666dbSDenis V. Lunev 	spin_unlock_bh(&sk->sk_lock.slock);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /*
1341da177e4SLinus Torvalds  * Figure out, may we reply to this packet with icmp error.
1351da177e4SLinus Torvalds  *
1361da177e4SLinus Torvalds  * We do not reply, if:
1371da177e4SLinus Torvalds  *	- it was icmp error message.
1381da177e4SLinus Torvalds  *	- it is truncated, so that it is known, that protocol is ICMPV6
1391da177e4SLinus Torvalds  *	  (i.e. in the middle of some exthdr)
1401da177e4SLinus Torvalds  *
1411da177e4SLinus Torvalds  *	--ANK (980726)
1421da177e4SLinus Torvalds  */
1431da177e4SLinus Torvalds 
144a50feda5SEric Dumazet static bool is_ineligible(const struct sk_buff *skb)
1451da177e4SLinus Torvalds {
1460660e03fSArnaldo Carvalho de Melo 	int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
1471da177e4SLinus Torvalds 	int len = skb->len - ptr;
1480660e03fSArnaldo Carvalho de Melo 	__u8 nexthdr = ipv6_hdr(skb)->nexthdr;
14975f2811cSJesse Gross 	__be16 frag_off;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	if (len < 0)
152a50feda5SEric Dumazet 		return true;
1531da177e4SLinus Torvalds 
15475f2811cSJesse Gross 	ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off);
1551da177e4SLinus Torvalds 	if (ptr < 0)
156a50feda5SEric Dumazet 		return false;
1571da177e4SLinus Torvalds 	if (nexthdr == IPPROTO_ICMPV6) {
1581da177e4SLinus Torvalds 		u8 _type, *tp;
1591da177e4SLinus Torvalds 		tp = skb_header_pointer(skb,
1601da177e4SLinus Torvalds 			ptr+offsetof(struct icmp6hdr, icmp6_type),
1611da177e4SLinus Torvalds 			sizeof(_type), &_type);
1621da177e4SLinus Torvalds 		if (tp == NULL ||
1631da177e4SLinus Torvalds 		    !(*tp & ICMPV6_INFOMSG_MASK))
164a50feda5SEric Dumazet 			return true;
1651da177e4SLinus Torvalds 	}
166a50feda5SEric Dumazet 	return false;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /*
1701da177e4SLinus Torvalds  * Check the ICMP output rate limit
1711da177e4SLinus Torvalds  */
17292d86829SDavid S. Miller static inline bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
1734c9483b2SDavid S. Miller 				      struct flowi6 *fl6)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct dst_entry *dst;
1763b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
17792d86829SDavid S. Miller 	bool res = false;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* Informational messages are not limited. */
1801da177e4SLinus Torvalds 	if (type & ICMPV6_INFOMSG_MASK)
18192d86829SDavid S. Miller 		return true;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	/* Do not limit pmtu discovery, it would break it. */
1841da177e4SLinus Torvalds 	if (type == ICMPV6_PKT_TOOBIG)
18592d86829SDavid S. Miller 		return true;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	/*
1881da177e4SLinus Torvalds 	 * Look up the output route.
1891da177e4SLinus Torvalds 	 * XXX: perhaps the expire for routing entries cloned by
1901da177e4SLinus Torvalds 	 * this lookup should be more aggressive (not longer than timeout).
1911da177e4SLinus Torvalds 	 */
1924c9483b2SDavid S. Miller 	dst = ip6_route_output(net, sk, fl6);
1931da177e4SLinus Torvalds 	if (dst->error) {
1943bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst),
195a11d206dSYOSHIFUJI Hideaki 			      IPSTATS_MIB_OUTNOROUTES);
1961da177e4SLinus Torvalds 	} else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
19792d86829SDavid S. Miller 		res = true;
1981da177e4SLinus Torvalds 	} else {
1991da177e4SLinus Torvalds 		struct rt6_info *rt = (struct rt6_info *)dst;
2009a43b709SBenjamin Thery 		int tmo = net->ipv6.sysctl.icmpv6_time;
201fbfe95a4SDavid S. Miller 		struct inet_peer *peer;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 		/* Give more bandwidth to wider prefixes. */
2041da177e4SLinus Torvalds 		if (rt->rt6i_dst.plen < 128)
2051da177e4SLinus Torvalds 			tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
2061da177e4SLinus Torvalds 
2071d861aa4SDavid S. Miller 		peer = inet_getpeer_v6(net->ipv6.peers, &rt->rt6i_dst.addr, 1);
208fbfe95a4SDavid S. Miller 		res = inet_peer_xrlim_allow(peer, tmo);
2091d861aa4SDavid S. Miller 		if (peer)
2101d861aa4SDavid S. Miller 			inet_putpeer(peer);
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 	dst_release(dst);
2131da177e4SLinus Torvalds 	return res;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds  *	an inline helper for the "simple" if statement below
2181da177e4SLinus Torvalds  *	checks if parameter problem report is caused by an
2191da177e4SLinus Torvalds  *	unrecognized IPv6 option that has the Option Type
2201da177e4SLinus Torvalds  *	highest-order two bits set to 10
2211da177e4SLinus Torvalds  */
2221da177e4SLinus Torvalds 
223a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	u8 _optval, *op;
2261da177e4SLinus Torvalds 
227bbe735e4SArnaldo Carvalho de Melo 	offset += skb_network_offset(skb);
2281da177e4SLinus Torvalds 	op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
2291da177e4SLinus Torvalds 	if (op == NULL)
230a50feda5SEric Dumazet 		return true;
2311da177e4SLinus Torvalds 	return (*op & 0xC0) == 0x80;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2346d0bfe22SLorenzo Colitti int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
2356d0bfe22SLorenzo Colitti 			       struct icmp6hdr *thdr, int len)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct sk_buff *skb;
2381da177e4SLinus Torvalds 	struct icmp6hdr *icmp6h;
2391da177e4SLinus Torvalds 	int err = 0;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
2421da177e4SLinus Torvalds 		goto out;
2431da177e4SLinus Torvalds 
244cc70ab26SArnaldo Carvalho de Melo 	icmp6h = icmp6_hdr(skb);
2451da177e4SLinus Torvalds 	memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
2461da177e4SLinus Torvalds 	icmp6h->icmp6_cksum = 0;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
24907f0757aSJoe Perches 		skb->csum = csum_partial(icmp6h,
2501da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), skb->csum);
2514c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2524c9483b2SDavid S. Miller 						      &fl6->daddr,
2534c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
2541da177e4SLinus Torvalds 						      skb->csum);
2551da177e4SLinus Torvalds 	} else {
256868c86bcSAl Viro 		__wsum tmp_csum = 0;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
2591da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
2601da177e4SLinus Torvalds 		}
2611da177e4SLinus Torvalds 
26207f0757aSJoe Perches 		tmp_csum = csum_partial(icmp6h,
2631da177e4SLinus Torvalds 					sizeof(struct icmp6hdr), tmp_csum);
2644c9483b2SDavid S. Miller 		icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2654c9483b2SDavid S. Miller 						      &fl6->daddr,
2664c9483b2SDavid S. Miller 						      len, fl6->flowi6_proto,
267868c86bcSAl Viro 						      tmp_csum);
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 	ip6_push_pending_frames(sk);
2701da177e4SLinus Torvalds out:
2711da177e4SLinus Torvalds 	return err;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds struct icmpv6_msg {
2751da177e4SLinus Torvalds 	struct sk_buff	*skb;
2761da177e4SLinus Torvalds 	int		offset;
277763ecff1SYasuyuki Kozakai 	uint8_t		type;
2781da177e4SLinus Torvalds };
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
2831da177e4SLinus Torvalds 	struct sk_buff *org_skb = msg->skb;
2845f92a738SAl Viro 	__wsum csum = 0;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
2871da177e4SLinus Torvalds 				      to, len, csum);
2881da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
289763ecff1SYasuyuki Kozakai 	if (!(msg->type & ICMPV6_INFOMSG_MASK))
290763ecff1SYasuyuki Kozakai 		nf_ct_attach(skb, org_skb);
2911da177e4SLinus Torvalds 	return 0;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
29407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
29579383236SMasahide NAKAMURA static void mip6_addr_swap(struct sk_buff *skb)
29679383236SMasahide NAKAMURA {
2970660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
29879383236SMasahide NAKAMURA 	struct inet6_skb_parm *opt = IP6CB(skb);
29979383236SMasahide NAKAMURA 	struct ipv6_destopt_hao *hao;
30079383236SMasahide NAKAMURA 	struct in6_addr tmp;
30179383236SMasahide NAKAMURA 	int off;
30279383236SMasahide NAKAMURA 
30379383236SMasahide NAKAMURA 	if (opt->dsthao) {
30479383236SMasahide NAKAMURA 		off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
30579383236SMasahide NAKAMURA 		if (likely(off >= 0)) {
306d56f90a7SArnaldo Carvalho de Melo 			hao = (struct ipv6_destopt_hao *)
307d56f90a7SArnaldo Carvalho de Melo 					(skb_network_header(skb) + off);
3084e3fd7a0SAlexey Dobriyan 			tmp = iph->saddr;
3094e3fd7a0SAlexey Dobriyan 			iph->saddr = hao->addr;
3104e3fd7a0SAlexey Dobriyan 			hao->addr = tmp;
31179383236SMasahide NAKAMURA 		}
31279383236SMasahide NAKAMURA 	}
31379383236SMasahide NAKAMURA }
31479383236SMasahide NAKAMURA #else
31579383236SMasahide NAKAMURA static inline void mip6_addr_swap(struct sk_buff *skb) {}
31679383236SMasahide NAKAMURA #endif
31779383236SMasahide NAKAMURA 
3186d0bfe22SLorenzo Colitti struct dst_entry *icmpv6_route_lookup(struct net *net, struct sk_buff *skb,
3194c9483b2SDavid S. Miller 				      struct sock *sk, struct flowi6 *fl6)
320b42835dbSDavid S. Miller {
321b42835dbSDavid S. Miller 	struct dst_entry *dst, *dst2;
3224c9483b2SDavid S. Miller 	struct flowi6 fl2;
323b42835dbSDavid S. Miller 	int err;
324b42835dbSDavid S. Miller 
3254c9483b2SDavid S. Miller 	err = ip6_dst_lookup(sk, &dst, fl6);
326b42835dbSDavid S. Miller 	if (err)
327b42835dbSDavid S. Miller 		return ERR_PTR(err);
328b42835dbSDavid S. Miller 
329b42835dbSDavid S. Miller 	/*
330b42835dbSDavid S. Miller 	 * We won't send icmp if the destination is known
331b42835dbSDavid S. Miller 	 * anycast.
332b42835dbSDavid S. Miller 	 */
333b42835dbSDavid S. Miller 	if (((struct rt6_info *)dst)->rt6i_flags & RTF_ANYCAST) {
3345f5624cfSPravin B Shelar 		LIMIT_NETDEBUG(KERN_DEBUG "icmp6_send: acast source\n");
335b42835dbSDavid S. Miller 		dst_release(dst);
336b42835dbSDavid S. Miller 		return ERR_PTR(-EINVAL);
337b42835dbSDavid S. Miller 	}
338b42835dbSDavid S. Miller 
339b42835dbSDavid S. Miller 	/* No need to clone since we're just using its address. */
340b42835dbSDavid S. Miller 	dst2 = dst;
341b42835dbSDavid S. Miller 
3424c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
343452edd59SDavid S. Miller 	if (!IS_ERR(dst)) {
344b42835dbSDavid S. Miller 		if (dst != dst2)
345b42835dbSDavid S. Miller 			return dst;
346452edd59SDavid S. Miller 	} else {
347452edd59SDavid S. Miller 		if (PTR_ERR(dst) == -EPERM)
348b42835dbSDavid S. Miller 			dst = NULL;
349452edd59SDavid S. Miller 		else
350452edd59SDavid S. Miller 			return dst;
351b42835dbSDavid S. Miller 	}
352b42835dbSDavid S. Miller 
3534c9483b2SDavid S. Miller 	err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6);
354b42835dbSDavid S. Miller 	if (err)
355b42835dbSDavid S. Miller 		goto relookup_failed;
356b42835dbSDavid S. Miller 
357b42835dbSDavid S. Miller 	err = ip6_dst_lookup(sk, &dst2, &fl2);
358b42835dbSDavid S. Miller 	if (err)
359b42835dbSDavid S. Miller 		goto relookup_failed;
360b42835dbSDavid S. Miller 
3614c9483b2SDavid S. Miller 	dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
362452edd59SDavid S. Miller 	if (!IS_ERR(dst2)) {
363b42835dbSDavid S. Miller 		dst_release(dst);
364b42835dbSDavid S. Miller 		dst = dst2;
365452edd59SDavid S. Miller 	} else {
366452edd59SDavid S. Miller 		err = PTR_ERR(dst2);
367452edd59SDavid S. Miller 		if (err == -EPERM) {
368b42835dbSDavid S. Miller 			dst_release(dst);
369452edd59SDavid S. Miller 			return dst2;
370452edd59SDavid S. Miller 		} else
371b42835dbSDavid S. Miller 			goto relookup_failed;
372b42835dbSDavid S. Miller 	}
373b42835dbSDavid S. Miller 
374b42835dbSDavid S. Miller relookup_failed:
375b42835dbSDavid S. Miller 	if (dst)
376b42835dbSDavid S. Miller 		return dst;
377b42835dbSDavid S. Miller 	return ERR_PTR(err);
378b42835dbSDavid S. Miller }
379b42835dbSDavid S. Miller 
3801da177e4SLinus Torvalds /*
3811da177e4SLinus Torvalds  *	Send an ICMP message in response to a packet in error
3821da177e4SLinus Torvalds  */
3835f5624cfSPravin B Shelar static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
3841da177e4SLinus Torvalds {
385c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
3861da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
3870660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
38884427d53SYOSHIFUJI Hideaki 	struct sock *sk;
38984427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
390b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
3911da177e4SLinus Torvalds 	struct dst_entry *dst;
3921da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
3934c9483b2SDavid S. Miller 	struct flowi6 fl6;
3941da177e4SLinus Torvalds 	struct icmpv6_msg msg;
3951da177e4SLinus Torvalds 	int iif = 0;
3961da177e4SLinus Torvalds 	int addr_type = 0;
3971da177e4SLinus Torvalds 	int len;
398e651f03aSGerrit Renker 	int hlimit;
3991da177e4SLinus Torvalds 	int err = 0;
4001da177e4SLinus Torvalds 
40127a884dcSArnaldo Carvalho de Melo 	if ((u8 *)hdr < skb->head ||
40229a3cad5SSimon Horman 	    (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
4031da177e4SLinus Torvalds 		return;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	/*
4061da177e4SLinus Torvalds 	 *	Make sure we respect the rules
4071da177e4SLinus Torvalds 	 *	i.e. RFC 1885 2.4(e)
4085f5624cfSPravin B Shelar 	 *	Rule (e.1) is enforced by not using icmp6_send
4091da177e4SLinus Torvalds 	 *	in any code that processes icmp errors.
4101da177e4SLinus Torvalds 	 */
4111da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->daddr);
4121da177e4SLinus Torvalds 
4139a43b709SBenjamin Thery 	if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0))
4141da177e4SLinus Torvalds 		saddr = &hdr->daddr;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	/*
4171da177e4SLinus Torvalds 	 *	Dest addr check
4181da177e4SLinus Torvalds 	 */
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if ((addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST)) {
4211da177e4SLinus Torvalds 		if (type != ICMPV6_PKT_TOOBIG &&
4221da177e4SLinus Torvalds 		    !(type == ICMPV6_PARAMPROB &&
4231da177e4SLinus Torvalds 		      code == ICMPV6_UNK_OPTION &&
4241da177e4SLinus Torvalds 		      (opt_unrec(skb, info))))
4251da177e4SLinus Torvalds 			return;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 		saddr = NULL;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&hdr->saddr);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	/*
4331da177e4SLinus Torvalds 	 *	Source addr check
4341da177e4SLinus Torvalds 	 */
4351da177e4SLinus Torvalds 
436842df073SHannes Frederic Sowa 	if (__ipv6_addr_needs_scope_id(addr_type))
4371da177e4SLinus Torvalds 		iif = skb->dev->ifindex;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	/*
4408de3351eSYOSHIFUJI Hideaki 	 *	Must not send error if the source does not uniquely
4418de3351eSYOSHIFUJI Hideaki 	 *	identify a single node (RFC2463 Section 2.4).
4428de3351eSYOSHIFUJI Hideaki 	 *	We check unspecified / multicast addresses here,
4438de3351eSYOSHIFUJI Hideaki 	 *	and anycast addresses will be checked later.
4441da177e4SLinus Torvalds 	 */
4451da177e4SLinus Torvalds 	if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
4465f5624cfSPravin B Shelar 		LIMIT_NETDEBUG(KERN_DEBUG "icmp6_send: addr_any/mcast source\n");
4471da177e4SLinus Torvalds 		return;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	/*
4511da177e4SLinus Torvalds 	 *	Never answer to a ICMP packet.
4521da177e4SLinus Torvalds 	 */
4531da177e4SLinus Torvalds 	if (is_ineligible(skb)) {
4545f5624cfSPravin B Shelar 		LIMIT_NETDEBUG(KERN_DEBUG "icmp6_send: no reply to icmp error\n");
4551da177e4SLinus Torvalds 		return;
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds 
45879383236SMasahide NAKAMURA 	mip6_addr_swap(skb);
45979383236SMasahide NAKAMURA 
4604c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
4614c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
4624e3fd7a0SAlexey Dobriyan 	fl6.daddr = hdr->saddr;
4631da177e4SLinus Torvalds 	if (saddr)
4644e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
4654c9483b2SDavid S. Miller 	fl6.flowi6_oif = iif;
4661958b856SDavid S. Miller 	fl6.fl6_icmp_type = type;
4671958b856SDavid S. Miller 	fl6.fl6_icmp_code = code;
4684c9483b2SDavid S. Miller 	security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
4691da177e4SLinus Torvalds 
470fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
471fdc0bde9SDenis V. Lunev 	if (sk == NULL)
472405666dbSDenis V. Lunev 		return;
473fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
474405666dbSDenis V. Lunev 
4754c9483b2SDavid S. Miller 	if (!icmpv6_xrlim_allow(sk, type, &fl6))
4761da177e4SLinus Torvalds 		goto out;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = type;
4791da177e4SLinus Torvalds 	tmp_hdr.icmp6_code = code;
4801da177e4SLinus Torvalds 	tmp_hdr.icmp6_cksum = 0;
4811da177e4SLinus Torvalds 	tmp_hdr.icmp6_pointer = htonl(info);
4821da177e4SLinus Torvalds 
4834c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
4844c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
485c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
486c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
4871da177e4SLinus Torvalds 
4884c9483b2SDavid S. Miller 	dst = icmpv6_route_lookup(net, skb, sk, &fl6);
489b42835dbSDavid S. Miller 	if (IS_ERR(dst))
4901da177e4SLinus Torvalds 		goto out;
4918de3351eSYOSHIFUJI Hideaki 
4924c9483b2SDavid S. Miller 	if (ipv6_addr_is_multicast(&fl6.daddr))
4931da177e4SLinus Torvalds 		hlimit = np->mcast_hops;
4941da177e4SLinus Torvalds 	else
4951da177e4SLinus Torvalds 		hlimit = np->hop_limit;
4961da177e4SLinus Torvalds 	if (hlimit < 0)
4976b75d090SYOSHIFUJI Hideaki 		hlimit = ip6_dst_hoplimit(dst);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	msg.skb = skb;
500bbe735e4SArnaldo Carvalho de Melo 	msg.offset = skb_network_offset(skb);
501763ecff1SYasuyuki Kozakai 	msg.type = type;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	len = skb->len - msg.offset;
5041da177e4SLinus Torvalds 	len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) -sizeof(struct icmp6hdr));
5051da177e4SLinus Torvalds 	if (len < 0) {
50664ce2073SPatrick McHardy 		LIMIT_NETDEBUG(KERN_DEBUG "icmp: len problem\n");
5071da177e4SLinus Torvalds 		goto out_dst_release;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
510cfdf7647SEric Dumazet 	rcu_read_lock();
511cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	err = ip6_append_data(sk, icmpv6_getfrag, &msg,
5141da177e4SLinus Torvalds 			      len + sizeof(struct icmp6hdr),
515e651f03aSGerrit Renker 			      sizeof(struct icmp6hdr), hlimit,
5164c9483b2SDavid S. Miller 			      np->tclass, NULL, &fl6, (struct rt6_info *)dst,
51713b52cd4SBrian Haley 			      MSG_DONTWAIT, np->dontfrag);
5181da177e4SLinus Torvalds 	if (err) {
51900d9d6a1SEric Dumazet 		ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTERRORS);
5201da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
521cfdf7647SEric Dumazet 	} else {
522cfdf7647SEric Dumazet 		err = icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
523cfdf7647SEric Dumazet 						 len + sizeof(struct icmp6hdr));
5241da177e4SLinus Torvalds 	}
525cfdf7647SEric Dumazet 	rcu_read_unlock();
5261da177e4SLinus Torvalds out_dst_release:
5271da177e4SLinus Torvalds 	dst_release(dst);
5281da177e4SLinus Torvalds out:
529405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
5301da177e4SLinus Torvalds }
5315f5624cfSPravin B Shelar 
5325f5624cfSPravin B Shelar /* Slightly more convenient version of icmp6_send.
5335f5624cfSPravin B Shelar  */
5345f5624cfSPravin B Shelar void icmpv6_param_prob(struct sk_buff *skb, u8 code, int pos)
5355f5624cfSPravin B Shelar {
5365f5624cfSPravin B Shelar 	icmp6_send(skb, ICMPV6_PARAMPROB, code, pos);
5375f5624cfSPravin B Shelar 	kfree_skb(skb);
5385f5624cfSPravin B Shelar }
5397159039aSYOSHIFUJI Hideaki 
5401da177e4SLinus Torvalds static void icmpv6_echo_reply(struct sk_buff *skb)
5411da177e4SLinus Torvalds {
542c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
54384427d53SYOSHIFUJI Hideaki 	struct sock *sk;
5441da177e4SLinus Torvalds 	struct inet6_dev *idev;
54584427d53SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np;
546b71d1d42SEric Dumazet 	const struct in6_addr *saddr = NULL;
547cc70ab26SArnaldo Carvalho de Melo 	struct icmp6hdr *icmph = icmp6_hdr(skb);
5481da177e4SLinus Torvalds 	struct icmp6hdr tmp_hdr;
5494c9483b2SDavid S. Miller 	struct flowi6 fl6;
5501da177e4SLinus Torvalds 	struct icmpv6_msg msg;
5511da177e4SLinus Torvalds 	struct dst_entry *dst;
5521da177e4SLinus Torvalds 	int err = 0;
5531da177e4SLinus Torvalds 	int hlimit;
5541da177e4SLinus Torvalds 
5550660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->daddr;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	if (!ipv6_unicast_destination(skb))
5581da177e4SLinus Torvalds 		saddr = NULL;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
5611da177e4SLinus Torvalds 	tmp_hdr.icmp6_type = ICMPV6_ECHO_REPLY;
5621da177e4SLinus Torvalds 
5634c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
5644c9483b2SDavid S. Miller 	fl6.flowi6_proto = IPPROTO_ICMPV6;
5654e3fd7a0SAlexey Dobriyan 	fl6.daddr = ipv6_hdr(skb)->saddr;
5661da177e4SLinus Torvalds 	if (saddr)
5674e3fd7a0SAlexey Dobriyan 		fl6.saddr = *saddr;
5684c9483b2SDavid S. Miller 	fl6.flowi6_oif = skb->dev->ifindex;
5691958b856SDavid S. Miller 	fl6.fl6_icmp_type = ICMPV6_ECHO_REPLY;
5704c9483b2SDavid S. Miller 	security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
5711da177e4SLinus Torvalds 
572fdc0bde9SDenis V. Lunev 	sk = icmpv6_xmit_lock(net);
573fdc0bde9SDenis V. Lunev 	if (sk == NULL)
574405666dbSDenis V. Lunev 		return;
575fdc0bde9SDenis V. Lunev 	np = inet6_sk(sk);
576405666dbSDenis V. Lunev 
5774c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
5784c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
579c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
580c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
5811da177e4SLinus Torvalds 
5824c9483b2SDavid S. Miller 	err = ip6_dst_lookup(sk, &dst, &fl6);
5831da177e4SLinus Torvalds 	if (err)
5841da177e4SLinus Torvalds 		goto out;
5854c9483b2SDavid S. Miller 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
586452edd59SDavid S. Miller 	if (IS_ERR(dst))
587e104411bSPatrick McHardy 		goto out;
5881da177e4SLinus Torvalds 
5894c9483b2SDavid S. Miller 	if (ipv6_addr_is_multicast(&fl6.daddr))
5901da177e4SLinus Torvalds 		hlimit = np->mcast_hops;
5911da177e4SLinus Torvalds 	else
5921da177e4SLinus Torvalds 		hlimit = np->hop_limit;
5931da177e4SLinus Torvalds 	if (hlimit < 0)
5946b75d090SYOSHIFUJI Hideaki 		hlimit = ip6_dst_hoplimit(dst);
5951da177e4SLinus Torvalds 
596cfdf7647SEric Dumazet 	idev = __in6_dev_get(skb->dev);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	msg.skb = skb;
5991da177e4SLinus Torvalds 	msg.offset = 0;
600763ecff1SYasuyuki Kozakai 	msg.type = ICMPV6_ECHO_REPLY;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	err = ip6_append_data(sk, icmpv6_getfrag, &msg, skb->len + sizeof(struct icmp6hdr),
6034c9483b2SDavid S. Miller 				sizeof(struct icmp6hdr), hlimit, np->tclass, NULL, &fl6,
60413b52cd4SBrian Haley 				(struct rt6_info *)dst, MSG_DONTWAIT,
60513b52cd4SBrian Haley 				np->dontfrag);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	if (err) {
60800d9d6a1SEric Dumazet 		ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTERRORS);
6091da177e4SLinus Torvalds 		ip6_flush_pending_frames(sk);
610cfdf7647SEric Dumazet 	} else {
611cfdf7647SEric Dumazet 		err = icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
612cfdf7647SEric Dumazet 						 skb->len + sizeof(struct icmp6hdr));
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 	dst_release(dst);
6151da177e4SLinus Torvalds out:
616405666dbSDenis V. Lunev 	icmpv6_xmit_unlock(sk);
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
619b94f1c09SDavid S. Miller void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
6201da177e4SLinus Torvalds {
62141135cc8SAlexey Dobriyan 	const struct inet6_protocol *ipprot;
6221da177e4SLinus Torvalds 	int inner_offset;
62375f2811cSJesse Gross 	__be16 frag_off;
624f9242b6bSDavid S. Miller 	u8 nexthdr;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
6271da177e4SLinus Torvalds 		return;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
6301da177e4SLinus Torvalds 	if (ipv6_ext_hdr(nexthdr)) {
6311da177e4SLinus Torvalds 		/* now skip over extension headers */
63275f2811cSJesse Gross 		inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
63375f2811cSJesse Gross 						&nexthdr, &frag_off);
6341da177e4SLinus Torvalds 		if (inner_offset<0)
6351da177e4SLinus Torvalds 			return;
6361da177e4SLinus Torvalds 	} else {
6371da177e4SLinus Torvalds 		inner_offset = sizeof(struct ipv6hdr);
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	/* Checkin header including 8 bytes of inner protocol header. */
6411da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, inner_offset+8))
6421da177e4SLinus Torvalds 		return;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	/* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
6451da177e4SLinus Torvalds 	   Without this we will not able f.e. to make source routed
6461da177e4SLinus Torvalds 	   pmtu discovery.
6471da177e4SLinus Torvalds 	   Corresponding argument (opt) to notifiers is already added.
6481da177e4SLinus Torvalds 	   --ANK (980726)
6491da177e4SLinus Torvalds 	 */
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	rcu_read_lock();
652f9242b6bSDavid S. Miller 	ipprot = rcu_dereference(inet6_protos[nexthdr]);
6531da177e4SLinus Torvalds 	if (ipprot && ipprot->err_handler)
6541da177e4SLinus Torvalds 		ipprot->err_handler(skb, NULL, type, code, inner_offset, info);
6551da177e4SLinus Torvalds 	rcu_read_unlock();
6561da177e4SLinus Torvalds 
65769d6da0bSPavel Emelyanov 	raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds /*
6611da177e4SLinus Torvalds  *	Handle icmp messages
6621da177e4SLinus Torvalds  */
6631da177e4SLinus Torvalds 
664e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb)
6651da177e4SLinus Torvalds {
6661da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
6671da177e4SLinus Torvalds 	struct inet6_dev *idev = __in6_dev_get(dev);
668b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
6691da177e4SLinus Torvalds 	struct icmp6hdr *hdr;
670d5fdd6baSBrian Haley 	u8 type;
6711da177e4SLinus Torvalds 
672aebcf82cSHerbert Xu 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
673def8b4faSAlexey Dobriyan 		struct sec_path *sp = skb_sec_path(skb);
6748b7817f3SHerbert Xu 		int nh;
6758b7817f3SHerbert Xu 
676def8b4faSAlexey Dobriyan 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
677aebcf82cSHerbert Xu 				 XFRM_STATE_ICMP))
678aebcf82cSHerbert Xu 			goto drop_no_count;
679aebcf82cSHerbert Xu 
68081aded24SDavid S. Miller 		if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
6818b7817f3SHerbert Xu 			goto drop_no_count;
6828b7817f3SHerbert Xu 
6838b7817f3SHerbert Xu 		nh = skb_network_offset(skb);
6848b7817f3SHerbert Xu 		skb_set_network_header(skb, sizeof(*hdr));
6858b7817f3SHerbert Xu 
6868b7817f3SHerbert Xu 		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
6878b7817f3SHerbert Xu 			goto drop_no_count;
6888b7817f3SHerbert Xu 
6898b7817f3SHerbert Xu 		skb_set_network_header(skb, nh);
6908b7817f3SHerbert Xu 	}
6918b7817f3SHerbert Xu 
692e41b5368SDenis V. Lunev 	ICMP6_INC_STATS_BH(dev_net(dev), idev, ICMP6_MIB_INMSGS);
6931da177e4SLinus Torvalds 
6940660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
6950660e03fSArnaldo Carvalho de Melo 	daddr = &ipv6_hdr(skb)->daddr;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	/* Perform checksum. */
698fb286bb2SHerbert Xu 	switch (skb->ip_summed) {
69984fa7933SPatrick McHardy 	case CHECKSUM_COMPLETE:
700fb286bb2SHerbert Xu 		if (!csum_ipv6_magic(saddr, daddr, skb->len, IPPROTO_ICMPV6,
701fb286bb2SHerbert Xu 				     skb->csum))
702fb286bb2SHerbert Xu 			break;
703fb286bb2SHerbert Xu 		/* fall through */
704fb286bb2SHerbert Xu 	case CHECKSUM_NONE:
705868c86bcSAl Viro 		skb->csum = ~csum_unfold(csum_ipv6_magic(saddr, daddr, skb->len,
706868c86bcSAl Viro 					     IPPROTO_ICMPV6, 0));
707fb286bb2SHerbert Xu 		if (__skb_checksum_complete(skb)) {
7086d0bfe22SLorenzo Colitti 			LIMIT_NETDEBUG(KERN_DEBUG
7096d0bfe22SLorenzo Colitti 				       "ICMPv6 checksum failed [%pI6c > %pI6c]\n",
7100c6ce78aSHarvey Harrison 				       saddr, daddr);
7116a5dc9e5SEric Dumazet 			goto csum_error;
7121da177e4SLinus Torvalds 		}
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 
7158cf22943SHerbert Xu 	if (!pskb_pull(skb, sizeof(*hdr)))
7168cf22943SHerbert Xu 		goto discard_it;
7171da177e4SLinus Torvalds 
718cc70ab26SArnaldo Carvalho de Melo 	hdr = icmp6_hdr(skb);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	type = hdr->icmp6_type;
7211da177e4SLinus Torvalds 
72255d43808SDenis V. Lunev 	ICMP6MSGIN_INC_STATS_BH(dev_net(dev), idev, type);
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	switch (type) {
7251da177e4SLinus Torvalds 	case ICMPV6_ECHO_REQUEST:
7261da177e4SLinus Torvalds 		icmpv6_echo_reply(skb);
7271da177e4SLinus Torvalds 		break;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	case ICMPV6_ECHO_REPLY:
7306d0bfe22SLorenzo Colitti 		ping_rcv(skb);
7311da177e4SLinus Torvalds 		break;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
7341da177e4SLinus Torvalds 		/* BUGGG_FUTURE: if packet contains rthdr, we cannot update
7351da177e4SLinus Torvalds 		   standard destination cache. Seems, only "advanced"
7361da177e4SLinus Torvalds 		   destination cache will allow to solve this problem
7371da177e4SLinus Torvalds 		   --ANK (980726)
7381da177e4SLinus Torvalds 		 */
7391da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
7401da177e4SLinus Torvalds 			goto discard_it;
741cc70ab26SArnaldo Carvalho de Melo 		hdr = icmp6_hdr(skb);
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 		/*
7441da177e4SLinus Torvalds 		 *	Drop through to notify
7451da177e4SLinus Torvalds 		 */
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
7481da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
7491da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
7501da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
7511da177e4SLinus Torvalds 		break;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	case NDISC_ROUTER_SOLICITATION:
7541da177e4SLinus Torvalds 	case NDISC_ROUTER_ADVERTISEMENT:
7551da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_SOLICITATION:
7561da177e4SLinus Torvalds 	case NDISC_NEIGHBOUR_ADVERTISEMENT:
7571da177e4SLinus Torvalds 	case NDISC_REDIRECT:
7581da177e4SLinus Torvalds 		ndisc_rcv(skb);
7591da177e4SLinus Torvalds 		break;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	case ICMPV6_MGM_QUERY:
7621da177e4SLinus Torvalds 		igmp6_event_query(skb);
7631da177e4SLinus Torvalds 		break;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	case ICMPV6_MGM_REPORT:
7661da177e4SLinus Torvalds 		igmp6_event_report(skb);
7671da177e4SLinus Torvalds 		break;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	case ICMPV6_MGM_REDUCTION:
7701da177e4SLinus Torvalds 	case ICMPV6_NI_QUERY:
7711da177e4SLinus Torvalds 	case ICMPV6_NI_REPLY:
7721da177e4SLinus Torvalds 	case ICMPV6_MLD2_REPORT:
7731da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REQUEST:
7741da177e4SLinus Torvalds 	case ICMPV6_DHAAD_REPLY:
7751da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_SOL:
7761da177e4SLinus Torvalds 	case ICMPV6_MOBILE_PREFIX_ADV:
7771da177e4SLinus Torvalds 		break;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	default:
78064ce2073SPatrick McHardy 		LIMIT_NETDEBUG(KERN_DEBUG "icmpv6: msg of unknown type\n");
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 		/* informational */
7831da177e4SLinus Torvalds 		if (type & ICMPV6_INFOMSG_MASK)
7841da177e4SLinus Torvalds 			break;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 		/*
7871da177e4SLinus Torvalds 		 * error of unknown type.
7881da177e4SLinus Torvalds 		 * must pass to upper level
7891da177e4SLinus Torvalds 		 */
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
7923ff50b79SStephen Hemminger 	}
7933ff50b79SStephen Hemminger 
7941da177e4SLinus Torvalds 	kfree_skb(skb);
7951da177e4SLinus Torvalds 	return 0;
7961da177e4SLinus Torvalds 
7976a5dc9e5SEric Dumazet csum_error:
7986a5dc9e5SEric Dumazet 	ICMP6_INC_STATS_BH(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS);
7991da177e4SLinus Torvalds discard_it:
800e41b5368SDenis V. Lunev 	ICMP6_INC_STATS_BH(dev_net(dev), idev, ICMP6_MIB_INERRORS);
8018b7817f3SHerbert Xu drop_no_count:
8021da177e4SLinus Torvalds 	kfree_skb(skb);
8031da177e4SLinus Torvalds 	return 0;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8064c9483b2SDavid S. Miller void icmpv6_flow_init(struct sock *sk, struct flowi6 *fl6,
80795e41e93SYOSHIFUJI Hideaki 		      u8 type,
80895e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *saddr,
80995e41e93SYOSHIFUJI Hideaki 		      const struct in6_addr *daddr,
81095e41e93SYOSHIFUJI Hideaki 		      int oif)
81195e41e93SYOSHIFUJI Hideaki {
8124c9483b2SDavid S. Miller 	memset(fl6, 0, sizeof(*fl6));
8134e3fd7a0SAlexey Dobriyan 	fl6->saddr = *saddr;
8144e3fd7a0SAlexey Dobriyan 	fl6->daddr = *daddr;
8154c9483b2SDavid S. Miller 	fl6->flowi6_proto 	= IPPROTO_ICMPV6;
8161958b856SDavid S. Miller 	fl6->fl6_icmp_type	= type;
8171958b856SDavid S. Miller 	fl6->fl6_icmp_code	= 0;
8184c9483b2SDavid S. Miller 	fl6->flowi6_oif		= oif;
8194c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
82095e41e93SYOSHIFUJI Hideaki }
82195e41e93SYOSHIFUJI Hideaki 
822640c41c7SIngo Molnar /*
823b7e729c4SDenis V. Lunev  * Special lock-class for __icmpv6_sk:
824640c41c7SIngo Molnar  */
825640c41c7SIngo Molnar static struct lock_class_key icmpv6_socket_sk_dst_lock_key;
826640c41c7SIngo Molnar 
82798c6d1b2SDenis V. Lunev static int __net_init icmpv6_sk_init(struct net *net)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	struct sock *sk;
8301da177e4SLinus Torvalds 	int err, i, j;
8311da177e4SLinus Torvalds 
83298c6d1b2SDenis V. Lunev 	net->ipv6.icmp_sk =
83398c6d1b2SDenis V. Lunev 		kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
83498c6d1b2SDenis V. Lunev 	if (net->ipv6.icmp_sk == NULL)
83579c91159SDenis V. Lunev 		return -ENOMEM;
83679c91159SDenis V. Lunev 
8376f912042SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
8381ed8516fSDenis V. Lunev 		err = inet_ctl_sock_create(&sk, PF_INET6,
8391ed8516fSDenis V. Lunev 					   SOCK_RAW, IPPROTO_ICMPV6, net);
8401da177e4SLinus Torvalds 		if (err < 0) {
841f3213831SJoe Perches 			pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
8421da177e4SLinus Torvalds 			       err);
8431da177e4SLinus Torvalds 			goto fail;
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 
8461ed8516fSDenis V. Lunev 		net->ipv6.icmp_sk[i] = sk;
8475c8cafd6SDenis V. Lunev 
848640c41c7SIngo Molnar 		/*
849640c41c7SIngo Molnar 		 * Split off their lock-class, because sk->sk_dst_lock
850640c41c7SIngo Molnar 		 * gets used from softirqs, which is safe for
851b7e729c4SDenis V. Lunev 		 * __icmpv6_sk (because those never get directly used
852640c41c7SIngo Molnar 		 * via userspace syscalls), but unsafe for normal sockets.
853640c41c7SIngo Molnar 		 */
854640c41c7SIngo Molnar 		lockdep_set_class(&sk->sk_dst_lock,
855640c41c7SIngo Molnar 				  &icmpv6_socket_sk_dst_lock_key);
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 		/* Enough space for 2 64K ICMP packets, including
8581da177e4SLinus Torvalds 		 * sk_buff struct overhead.
8591da177e4SLinus Torvalds 		 */
86087fb4b7bSEric Dumazet 		sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 	return 0;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds  fail:
8655c8cafd6SDenis V. Lunev 	for (j = 0; j < i; j++)
8661ed8516fSDenis V. Lunev 		inet_ctl_sock_destroy(net->ipv6.icmp_sk[j]);
86798c6d1b2SDenis V. Lunev 	kfree(net->ipv6.icmp_sk);
8681da177e4SLinus Torvalds 	return err;
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
87198c6d1b2SDenis V. Lunev static void __net_exit icmpv6_sk_exit(struct net *net)
8721da177e4SLinus Torvalds {
8731da177e4SLinus Torvalds 	int i;
8741da177e4SLinus Torvalds 
8756f912042SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
8761ed8516fSDenis V. Lunev 		inet_ctl_sock_destroy(net->ipv6.icmp_sk[i]);
8771da177e4SLinus Torvalds 	}
87898c6d1b2SDenis V. Lunev 	kfree(net->ipv6.icmp_sk);
87998c6d1b2SDenis V. Lunev }
88098c6d1b2SDenis V. Lunev 
8818ed7edceSAlexey Dobriyan static struct pernet_operations icmpv6_sk_ops = {
88298c6d1b2SDenis V. Lunev        .init = icmpv6_sk_init,
88398c6d1b2SDenis V. Lunev        .exit = icmpv6_sk_exit,
88498c6d1b2SDenis V. Lunev };
88598c6d1b2SDenis V. Lunev 
88698c6d1b2SDenis V. Lunev int __init icmpv6_init(void)
88798c6d1b2SDenis V. Lunev {
88898c6d1b2SDenis V. Lunev 	int err;
88998c6d1b2SDenis V. Lunev 
89098c6d1b2SDenis V. Lunev 	err = register_pernet_subsys(&icmpv6_sk_ops);
89198c6d1b2SDenis V. Lunev 	if (err < 0)
89298c6d1b2SDenis V. Lunev 		return err;
89398c6d1b2SDenis V. Lunev 
89498c6d1b2SDenis V. Lunev 	err = -EAGAIN;
89598c6d1b2SDenis V. Lunev 	if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
89698c6d1b2SDenis V. Lunev 		goto fail;
8975f5624cfSPravin B Shelar 
8985f5624cfSPravin B Shelar 	err = inet6_register_icmp_sender(icmp6_send);
8995f5624cfSPravin B Shelar 	if (err)
9005f5624cfSPravin B Shelar 		goto sender_reg_err;
90198c6d1b2SDenis V. Lunev 	return 0;
90298c6d1b2SDenis V. Lunev 
9035f5624cfSPravin B Shelar sender_reg_err:
9045f5624cfSPravin B Shelar 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
90598c6d1b2SDenis V. Lunev fail:
906f3213831SJoe Perches 	pr_err("Failed to register ICMP6 protocol\n");
90798c6d1b2SDenis V. Lunev 	unregister_pernet_subsys(&icmpv6_sk_ops);
90898c6d1b2SDenis V. Lunev 	return err;
90998c6d1b2SDenis V. Lunev }
91098c6d1b2SDenis V. Lunev 
9118ed7edceSAlexey Dobriyan void icmpv6_cleanup(void)
91298c6d1b2SDenis V. Lunev {
9135f5624cfSPravin B Shelar 	inet6_unregister_icmp_sender(icmp6_send);
91498c6d1b2SDenis V. Lunev 	unregister_pernet_subsys(&icmpv6_sk_ops);
9151da177e4SLinus Torvalds 	inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
91898c6d1b2SDenis V. Lunev 
9199b5b5cffSArjan van de Ven static const struct icmp6_err {
9201da177e4SLinus Torvalds 	int err;
9211da177e4SLinus Torvalds 	int fatal;
9221da177e4SLinus Torvalds } tab_unreach[] = {
9231da177e4SLinus Torvalds 	{	/* NOROUTE */
9241da177e4SLinus Torvalds 		.err	= ENETUNREACH,
9251da177e4SLinus Torvalds 		.fatal	= 0,
9261da177e4SLinus Torvalds 	},
9271da177e4SLinus Torvalds 	{	/* ADM_PROHIBITED */
9281da177e4SLinus Torvalds 		.err	= EACCES,
9291da177e4SLinus Torvalds 		.fatal	= 1,
9301da177e4SLinus Torvalds 	},
9311da177e4SLinus Torvalds 	{	/* Was NOT_NEIGHBOUR, now reserved */
9321da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
9331da177e4SLinus Torvalds 		.fatal	= 0,
9341da177e4SLinus Torvalds 	},
9351da177e4SLinus Torvalds 	{	/* ADDR_UNREACH	*/
9361da177e4SLinus Torvalds 		.err	= EHOSTUNREACH,
9371da177e4SLinus Torvalds 		.fatal	= 0,
9381da177e4SLinus Torvalds 	},
9391da177e4SLinus Torvalds 	{	/* PORT_UNREACH	*/
9401da177e4SLinus Torvalds 		.err	= ECONNREFUSED,
9411da177e4SLinus Torvalds 		.fatal	= 1,
9421da177e4SLinus Torvalds 	},
94361e76b17SJiri Bohac 	{	/* POLICY_FAIL */
94461e76b17SJiri Bohac 		.err	= EACCES,
94561e76b17SJiri Bohac 		.fatal	= 1,
94661e76b17SJiri Bohac 	},
94761e76b17SJiri Bohac 	{	/* REJECT_ROUTE	*/
94861e76b17SJiri Bohac 		.err	= EACCES,
94961e76b17SJiri Bohac 		.fatal	= 1,
95061e76b17SJiri Bohac 	},
9511da177e4SLinus Torvalds };
9521da177e4SLinus Torvalds 
953d5fdd6baSBrian Haley int icmpv6_err_convert(u8 type, u8 code, int *err)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	int fatal = 0;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	*err = EPROTO;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	switch (type) {
9601da177e4SLinus Torvalds 	case ICMPV6_DEST_UNREACH:
9611da177e4SLinus Torvalds 		fatal = 1;
96261e76b17SJiri Bohac 		if (code < ARRAY_SIZE(tab_unreach)) {
9631da177e4SLinus Torvalds 			*err  = tab_unreach[code].err;
9641da177e4SLinus Torvalds 			fatal = tab_unreach[code].fatal;
9651da177e4SLinus Torvalds 		}
9661da177e4SLinus Torvalds 		break;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
9691da177e4SLinus Torvalds 		*err = EMSGSIZE;
9701da177e4SLinus Torvalds 		break;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
9731da177e4SLinus Torvalds 		*err = EPROTO;
9741da177e4SLinus Torvalds 		fatal = 1;
9751da177e4SLinus Torvalds 		break;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	case ICMPV6_TIME_EXCEED:
9781da177e4SLinus Torvalds 		*err = EHOSTUNREACH;
9791da177e4SLinus Torvalds 		break;
9803ff50b79SStephen Hemminger 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	return fatal;
9831da177e4SLinus Torvalds }
9847159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert);
9857159039aSYOSHIFUJI Hideaki 
9861da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
987fe2c6338SJoe Perches struct ctl_table ipv6_icmp_table_template[] = {
9881da177e4SLinus Torvalds 	{
9891da177e4SLinus Torvalds 		.procname	= "ratelimit",
99041a76906SDaniel Lezcano 		.data		= &init_net.ipv6.sysctl.icmpv6_time,
9911da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
9921da177e4SLinus Torvalds 		.mode		= 0644,
9936d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
9941da177e4SLinus Torvalds 	},
995f8572d8fSEric W. Biederman 	{ },
9961da177e4SLinus Torvalds };
997760f2d01SDaniel Lezcano 
9982c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
999760f2d01SDaniel Lezcano {
1000760f2d01SDaniel Lezcano 	struct ctl_table *table;
1001760f2d01SDaniel Lezcano 
1002760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_icmp_table_template,
1003760f2d01SDaniel Lezcano 			sizeof(ipv6_icmp_table_template),
1004760f2d01SDaniel Lezcano 			GFP_KERNEL);
10055ee09105SYOSHIFUJI Hideaki 
1006c027aab4SEric W. Biederman 	if (table)
10075ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.icmpv6_time;
10085ee09105SYOSHIFUJI Hideaki 
1009760f2d01SDaniel Lezcano 	return table;
1010760f2d01SDaniel Lezcano }
10111da177e4SLinus Torvalds #endif
10121da177e4SLinus Torvalds 
1013