xref: /openbmc/linux/net/ipv4/icmp.c (revision 0f9b4c3ca5fdf3e177266ef994071b1a03f07318)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	NET3:	Implementation of the ICMP protocol layer.
41da177e4SLinus Torvalds  *
5113aa838SAlan Cox  *		Alan Cox, <alan@lxorguk.ukuu.org.uk>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	Some of the function names and the icmp unreach table for this
81da177e4SLinus Torvalds  *	module were derived from [icmp.c 1.0.11 06/02/93] by
91da177e4SLinus Torvalds  *	Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
101da177e4SLinus Torvalds  *	Other than that this module is a complete rewrite.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	Fixes:
131da177e4SLinus Torvalds  *	Clemens Fruhwirth	:	introduce global icmp rate limiting
141da177e4SLinus Torvalds  *					with icmp type masking ability instead
151da177e4SLinus Torvalds  *					of broken per type icmp timeouts.
161da177e4SLinus Torvalds  *		Mike Shaver	:	RFC1122 checks.
171da177e4SLinus Torvalds  *		Alan Cox	:	Multicast ping reply as self.
181da177e4SLinus Torvalds  *		Alan Cox	:	Fix atomicity lockup in ip_build_xmit
191da177e4SLinus Torvalds  *					call.
201da177e4SLinus Torvalds  *		Alan Cox	:	Added 216,128 byte paths to the MTU
211da177e4SLinus Torvalds  *					code.
221da177e4SLinus Torvalds  *		Martin Mares	:	RFC1812 checks.
231da177e4SLinus Torvalds  *		Martin Mares	:	Can be configured to follow redirects
241da177e4SLinus Torvalds  *					if acting as a router _without_ a
251da177e4SLinus Torvalds  *					routing protocol (RFC 1812).
261da177e4SLinus Torvalds  *		Martin Mares	:	Echo requests may be configured to
271da177e4SLinus Torvalds  *					be ignored (RFC 1812).
281da177e4SLinus Torvalds  *		Martin Mares	:	Limitation of ICMP error message
291da177e4SLinus Torvalds  *					transmit rate (RFC 1812).
301da177e4SLinus Torvalds  *		Martin Mares	:	TOS and Precedence set correctly
311da177e4SLinus Torvalds  *					(RFC 1812).
321da177e4SLinus Torvalds  *		Martin Mares	:	Now copying as much data from the
331da177e4SLinus Torvalds  *					original packet as we can without
341da177e4SLinus Torvalds  *					exceeding 576 bytes (RFC 1812).
351da177e4SLinus Torvalds  *	Willy Konynenberg	:	Transparent proxying support.
361da177e4SLinus Torvalds  *		Keith Owens	:	RFC1191 correction for 4.2BSD based
371da177e4SLinus Torvalds  *					path MTU bug.
381da177e4SLinus Torvalds  *		Thomas Quinot	:	ICMP Dest Unreach codes up to 15 are
391da177e4SLinus Torvalds  *					valid (RFC 1812).
401da177e4SLinus Torvalds  *		Andi Kleen	:	Check all packet lengths properly
411da177e4SLinus Torvalds  *					and moved all kfree_skb() up to
421da177e4SLinus Torvalds  *					icmp_rcv.
431da177e4SLinus Torvalds  *		Andi Kleen	:	Move the rate limit bookkeeping
441da177e4SLinus Torvalds  *					into the dest entry and use a token
451da177e4SLinus Torvalds  *					bucket filter (thanks to ANK). Make
461da177e4SLinus Torvalds  *					the rates sysctl configurable.
471da177e4SLinus Torvalds  *		Yu Tianli	:	Fixed two ugly bugs in icmp_send
481da177e4SLinus Torvalds  *					- IP option length was accounted wrongly
491da177e4SLinus Torvalds  *					- ICMP header length was not accounted
501da177e4SLinus Torvalds  *					  at all.
511da177e4SLinus Torvalds  *              Tristan Greaves :       Added sysctl option to ignore bogus
521da177e4SLinus Torvalds  *              			broadcast responses from broken routers.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * To Fix:
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  *	- Should use skb_pull() instead of all the manual checking.
571da177e4SLinus Torvalds  *	  This would also greatly simply some upper layer error handlers. --AK
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds 
60afd46503SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
61afd46503SJoe Perches 
621da177e4SLinus Torvalds #include <linux/module.h>
631da177e4SLinus Torvalds #include <linux/types.h>
641da177e4SLinus Torvalds #include <linux/jiffies.h>
651da177e4SLinus Torvalds #include <linux/kernel.h>
661da177e4SLinus Torvalds #include <linux/fcntl.h>
671da177e4SLinus Torvalds #include <linux/socket.h>
681da177e4SLinus Torvalds #include <linux/in.h>
691da177e4SLinus Torvalds #include <linux/inet.h>
7014c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
711da177e4SLinus Torvalds #include <linux/netdevice.h>
721da177e4SLinus Torvalds #include <linux/string.h>
731da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
745a0e3ad6STejun Heo #include <linux/slab.h>
751da177e4SLinus Torvalds #include <net/snmp.h>
761da177e4SLinus Torvalds #include <net/ip.h>
771da177e4SLinus Torvalds #include <net/route.h>
781da177e4SLinus Torvalds #include <net/protocol.h>
791da177e4SLinus Torvalds #include <net/icmp.h>
801da177e4SLinus Torvalds #include <net/tcp.h>
811da177e4SLinus Torvalds #include <net/udp.h>
821da177e4SLinus Torvalds #include <net/raw.h>
83c319b4d7SVasiliy Kulikov #include <net/ping.h>
841da177e4SLinus Torvalds #include <linux/skbuff.h>
851da177e4SLinus Torvalds #include <net/sock.h>
861da177e4SLinus Torvalds #include <linux/errno.h>
871da177e4SLinus Torvalds #include <linux/timer.h>
881da177e4SLinus Torvalds #include <linux/init.h>
897c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
901da177e4SLinus Torvalds #include <net/checksum.h>
918b7817f3SHerbert Xu #include <net/xfrm.h>
92c1e9894dSDenis V. Lunev #include <net/inet_common.h>
9335ebf65eSDavid S. Miller #include <net/ip_fib.h>
94385add90SDavid Ahern #include <net/l3mdev.h>
95d68dc711SEric Dumazet #include <net/addrconf.h>
9635c4d957SIdo Schimmel #include <net/inet_dscp.h>
9721b28f97SPeilin He #define CREATE_TRACE_POINTS
9821b28f97SPeilin He #include <trace/events/icmp.h>
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /*
1011da177e4SLinus Torvalds  *	Build xmit assembly blocks
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds struct icmp_bxm {
1051da177e4SLinus Torvalds 	struct sk_buff *skb;
1061da177e4SLinus Torvalds 	int offset;
1071da177e4SLinus Torvalds 	int data_len;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	struct {
1101da177e4SLinus Torvalds 		struct icmphdr icmph;
111b03d73e3SAl Viro 		__be32	       times[3];
1121da177e4SLinus Torvalds 	} data;
1131da177e4SLinus Torvalds 	int head_len;
114f6d8bd05SEric Dumazet 	struct ip_options_data replyopts;
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* An array of errno for error messages from dest unreach. */
1181da177e4SLinus Torvalds /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
1191da177e4SLinus Torvalds 
120e754834eSAlexey Dobriyan const struct icmp_err icmp_err_convert[] = {
1211da177e4SLinus Torvalds 	{
1221da177e4SLinus Torvalds 		.errno = ENETUNREACH,	/* ICMP_NET_UNREACH */
1231da177e4SLinus Torvalds 		.fatal = 0,
1241da177e4SLinus Torvalds 	},
1251da177e4SLinus Torvalds 	{
1261da177e4SLinus Torvalds 		.errno = EHOSTUNREACH,	/* ICMP_HOST_UNREACH */
1271da177e4SLinus Torvalds 		.fatal = 0,
1281da177e4SLinus Torvalds 	},
1291da177e4SLinus Torvalds 	{
1301da177e4SLinus Torvalds 		.errno = ENOPROTOOPT	/* ICMP_PROT_UNREACH */,
1311da177e4SLinus Torvalds 		.fatal = 1,
1321da177e4SLinus Torvalds 	},
1331da177e4SLinus Torvalds 	{
1341da177e4SLinus Torvalds 		.errno = ECONNREFUSED,	/* ICMP_PORT_UNREACH */
1351da177e4SLinus Torvalds 		.fatal = 1,
1361da177e4SLinus Torvalds 	},
1371da177e4SLinus Torvalds 	{
1381da177e4SLinus Torvalds 		.errno = EMSGSIZE,	/* ICMP_FRAG_NEEDED */
1391da177e4SLinus Torvalds 		.fatal = 0,
1401da177e4SLinus Torvalds 	},
1411da177e4SLinus Torvalds 	{
1421da177e4SLinus Torvalds 		.errno = EOPNOTSUPP,	/* ICMP_SR_FAILED */
1431da177e4SLinus Torvalds 		.fatal = 0,
1441da177e4SLinus Torvalds 	},
1451da177e4SLinus Torvalds 	{
1461da177e4SLinus Torvalds 		.errno = ENETUNREACH,	/* ICMP_NET_UNKNOWN */
1471da177e4SLinus Torvalds 		.fatal = 1,
1481da177e4SLinus Torvalds 	},
1491da177e4SLinus Torvalds 	{
1501da177e4SLinus Torvalds 		.errno = EHOSTDOWN,	/* ICMP_HOST_UNKNOWN */
1511da177e4SLinus Torvalds 		.fatal = 1,
1521da177e4SLinus Torvalds 	},
1531da177e4SLinus Torvalds 	{
1541da177e4SLinus Torvalds 		.errno = ENONET,	/* ICMP_HOST_ISOLATED */
1551da177e4SLinus Torvalds 		.fatal = 1,
1561da177e4SLinus Torvalds 	},
1571da177e4SLinus Torvalds 	{
1581da177e4SLinus Torvalds 		.errno = ENETUNREACH,	/* ICMP_NET_ANO	*/
1591da177e4SLinus Torvalds 		.fatal = 1,
1601da177e4SLinus Torvalds 	},
1611da177e4SLinus Torvalds 	{
1621da177e4SLinus Torvalds 		.errno = EHOSTUNREACH,	/* ICMP_HOST_ANO */
1631da177e4SLinus Torvalds 		.fatal = 1,
1641da177e4SLinus Torvalds 	},
1651da177e4SLinus Torvalds 	{
1661da177e4SLinus Torvalds 		.errno = ENETUNREACH,	/* ICMP_NET_UNR_TOS */
1671da177e4SLinus Torvalds 		.fatal = 0,
1681da177e4SLinus Torvalds 	},
1691da177e4SLinus Torvalds 	{
1701da177e4SLinus Torvalds 		.errno = EHOSTUNREACH,	/* ICMP_HOST_UNR_TOS */
1711da177e4SLinus Torvalds 		.fatal = 0,
1721da177e4SLinus Torvalds 	},
1731da177e4SLinus Torvalds 	{
1741da177e4SLinus Torvalds 		.errno = EHOSTUNREACH,	/* ICMP_PKT_FILTERED */
1751da177e4SLinus Torvalds 		.fatal = 1,
1761da177e4SLinus Torvalds 	},
1771da177e4SLinus Torvalds 	{
1781da177e4SLinus Torvalds 		.errno = EHOSTUNREACH,	/* ICMP_PREC_VIOLATION */
1791da177e4SLinus Torvalds 		.fatal = 1,
1801da177e4SLinus Torvalds 	},
1811da177e4SLinus Torvalds 	{
1821da177e4SLinus Torvalds 		.errno = EHOSTUNREACH,	/* ICMP_PREC_CUTOFF */
1831da177e4SLinus Torvalds 		.fatal = 1,
1841da177e4SLinus Torvalds 	},
1851da177e4SLinus Torvalds };
1864bc2f18bSEric Dumazet EXPORT_SYMBOL(icmp_err_convert);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /*
1891da177e4SLinus Torvalds  *	ICMP control array. This specifies what to do with each ICMP.
1901da177e4SLinus Torvalds  */
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds struct icmp_control {
193b384c95aSMenglong Dong 	enum skb_drop_reason (*handler)(struct sk_buff *skb);
1941da177e4SLinus Torvalds 	short   error;		/* This ICMP is classed as an error message */
1951da177e4SLinus Torvalds };
1961da177e4SLinus Torvalds 
1979b5b5cffSArjan van de Ven static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
1981da177e4SLinus Torvalds 
199a15c89c7SEric Dumazet static DEFINE_PER_CPU(struct sock *, ipv4_icmp_sk);
2001da177e4SLinus Torvalds 
2017ba91ecbSJesper Dangaard Brouer /* Called with BH disabled */
icmp_xmit_lock(struct net * net)202fdc0bde9SDenis V. Lunev static inline struct sock *icmp_xmit_lock(struct net *net)
2031da177e4SLinus Torvalds {
204fdc0bde9SDenis V. Lunev 	struct sock *sk;
205fdc0bde9SDenis V. Lunev 
206a15c89c7SEric Dumazet 	sk = this_cpu_read(ipv4_icmp_sk);
207fdc0bde9SDenis V. Lunev 
208405666dbSDenis V. Lunev 	if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
2091da177e4SLinus Torvalds 		/* This can happen if the output path signals a
2101da177e4SLinus Torvalds 		 * dst_link_failure() for an outgoing ICMP packet.
2111da177e4SLinus Torvalds 		 */
212fdc0bde9SDenis V. Lunev 		return NULL;
2131da177e4SLinus Torvalds 	}
214a15c89c7SEric Dumazet 	sock_net_set(sk, net);
215fdc0bde9SDenis V. Lunev 	return sk;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
icmp_xmit_unlock(struct sock * sk)218405666dbSDenis V. Lunev static inline void icmp_xmit_unlock(struct sock *sk)
2191da177e4SLinus Torvalds {
220a15c89c7SEric Dumazet 	sock_net_set(sk, &init_net);
2217ba91ecbSJesper Dangaard Brouer 	spin_unlock(&sk->sk_lock.slock);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2244cdf507dSEric Dumazet int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
2254cdf507dSEric Dumazet int sysctl_icmp_msgs_burst __read_mostly = 50;
2264cdf507dSEric Dumazet 
2274cdf507dSEric Dumazet static struct {
228662ec522SEric Dumazet 	atomic_t	credit;
2294cdf507dSEric Dumazet 	u32		stamp;
230662ec522SEric Dumazet } icmp_global;
2314cdf507dSEric Dumazet 
2324cdf507dSEric Dumazet /**
2334cdf507dSEric Dumazet  * icmp_global_allow - Are we allowed to send one more ICMP message ?
2344cdf507dSEric Dumazet  *
235b38e7819SEric Dumazet  * Uses a token bucket to limit our ICMP messages to ~sysctl_icmp_msgs_per_sec.
2364cdf507dSEric Dumazet  * Returns false if we reached the limit and can not send another packet.
237662ec522SEric Dumazet  * Works in tandem with icmp_global_consume().
2384cdf507dSEric Dumazet  */
icmp_global_allow(void)2394cdf507dSEric Dumazet bool icmp_global_allow(void)
2404cdf507dSEric Dumazet {
241662ec522SEric Dumazet 	u32 delta, now, oldstamp;
242662ec522SEric Dumazet 	int incr, new, old;
2434cdf507dSEric Dumazet 
244662ec522SEric Dumazet 	/* Note: many cpus could find this condition true.
245662ec522SEric Dumazet 	 * Then later icmp_global_consume() could consume more credits,
246662ec522SEric Dumazet 	 * this is an acceptable race.
2474cdf507dSEric Dumazet 	 */
248662ec522SEric Dumazet 	if (atomic_read(&icmp_global.credit) > 0)
249662ec522SEric Dumazet 		return true;
250662ec522SEric Dumazet 
251662ec522SEric Dumazet 	now = jiffies;
252662ec522SEric Dumazet 	oldstamp = READ_ONCE(icmp_global.stamp);
253662ec522SEric Dumazet 	delta = min_t(u32, now - oldstamp, HZ);
2544cdf507dSEric Dumazet 	if (delta < HZ / 50)
2554cdf507dSEric Dumazet 		return false;
2564cdf507dSEric Dumazet 
25748d7ee32SKuniyuki Iwashima 	incr = READ_ONCE(sysctl_icmp_msgs_per_sec) * delta / HZ;
258662ec522SEric Dumazet 	if (!incr)
259662ec522SEric Dumazet 		return false;
260662ec522SEric Dumazet 
261662ec522SEric Dumazet 	if (cmpxchg(&icmp_global.stamp, oldstamp, now) == oldstamp) {
262662ec522SEric Dumazet 		old = atomic_read(&icmp_global.credit);
263662ec522SEric Dumazet 		do {
264662ec522SEric Dumazet 			new = min(old + incr, READ_ONCE(sysctl_icmp_msgs_burst));
265662ec522SEric Dumazet 		} while (!atomic_try_cmpxchg(&icmp_global.credit, &old, new));
2664cdf507dSEric Dumazet 	}
267662ec522SEric Dumazet 	return true;
2684cdf507dSEric Dumazet }
2694cdf507dSEric Dumazet EXPORT_SYMBOL(icmp_global_allow);
2704cdf507dSEric Dumazet 
icmp_global_consume(void)271662ec522SEric Dumazet void icmp_global_consume(void)
272662ec522SEric Dumazet {
273662ec522SEric Dumazet 	int credits = get_random_u32_below(3);
274662ec522SEric Dumazet 
275662ec522SEric Dumazet 	/* Note: this might make icmp_global.credit negative. */
276662ec522SEric Dumazet 	if (credits)
277662ec522SEric Dumazet 		atomic_sub(credits, &icmp_global.credit);
278662ec522SEric Dumazet }
279662ec522SEric Dumazet EXPORT_SYMBOL(icmp_global_consume);
280662ec522SEric Dumazet 
icmpv4_mask_allow(struct net * net,int type,int code)281c0303efeSJesper Dangaard Brouer static bool icmpv4_mask_allow(struct net *net, int type, int code)
282c0303efeSJesper Dangaard Brouer {
283c0303efeSJesper Dangaard Brouer 	if (type > NR_ICMP_TYPES)
284c0303efeSJesper Dangaard Brouer 		return true;
285c0303efeSJesper Dangaard Brouer 
286c0303efeSJesper Dangaard Brouer 	/* Don't limit PMTU discovery. */
287c0303efeSJesper Dangaard Brouer 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
288c0303efeSJesper Dangaard Brouer 		return true;
289c0303efeSJesper Dangaard Brouer 
290c0303efeSJesper Dangaard Brouer 	/* Limit if icmp type is enabled in ratemask. */
2911ebcb25aSKuniyuki Iwashima 	if (!((1 << type) & READ_ONCE(net->ipv4.sysctl_icmp_ratemask)))
292c0303efeSJesper Dangaard Brouer 		return true;
293c0303efeSJesper Dangaard Brouer 
294c0303efeSJesper Dangaard Brouer 	return false;
295c0303efeSJesper Dangaard Brouer }
296c0303efeSJesper Dangaard Brouer 
icmpv4_global_allow(struct net * net,int type,int code,bool * apply_ratelimit)297662ec522SEric Dumazet static bool icmpv4_global_allow(struct net *net, int type, int code,
298662ec522SEric Dumazet 				bool *apply_ratelimit)
299c0303efeSJesper Dangaard Brouer {
300c0303efeSJesper Dangaard Brouer 	if (icmpv4_mask_allow(net, type, code))
301c0303efeSJesper Dangaard Brouer 		return true;
302c0303efeSJesper Dangaard Brouer 
303662ec522SEric Dumazet 	if (icmp_global_allow()) {
304662ec522SEric Dumazet 		*apply_ratelimit = true;
305c0303efeSJesper Dangaard Brouer 		return true;
306662ec522SEric Dumazet 	}
307d0941130SJamie Bainbridge 	__ICMP_INC_STATS(net, ICMP_MIB_RATELIMITGLOBAL);
308c0303efeSJesper Dangaard Brouer 	return false;
309c0303efeSJesper Dangaard Brouer }
310c0303efeSJesper Dangaard Brouer 
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds  *	Send an ICMP frame.
3131da177e4SLinus Torvalds  */
3141da177e4SLinus Torvalds 
icmpv4_xrlim_allow(struct net * net,struct rtable * rt,struct flowi4 * fl4,int type,int code,bool apply_ratelimit)3154cdf507dSEric Dumazet static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
316662ec522SEric Dumazet 			       struct flowi4 *fl4, int type, int code,
317662ec522SEric Dumazet 			       bool apply_ratelimit)
3181da177e4SLinus Torvalds {
319d8d1f30bSChangli Gao 	struct dst_entry *dst = &rt->dst;
320c0303efeSJesper Dangaard Brouer 	struct inet_peer *peer;
32192d86829SDavid S. Miller 	bool rc = true;
3221da177e4SLinus Torvalds 
323662ec522SEric Dumazet 	if (!apply_ratelimit)
324662ec522SEric Dumazet 		return true;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* No rate limit on loopback */
3271da177e4SLinus Torvalds 	if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
3281da177e4SLinus Torvalds 		goto out;
3291da177e4SLinus Torvalds 
330280fb099SEric Dumazet 	rcu_read_lock();
331280fb099SEric Dumazet 	peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr,
332280fb099SEric Dumazet 			       l3mdev_master_ifindex_rcu(dst->dev));
3332a4eb714SKuniyuki Iwashima 	rc = inet_peer_xrlim_allow(peer,
3342a4eb714SKuniyuki Iwashima 				   READ_ONCE(net->ipv4.sysctl_icmp_ratelimit));
335280fb099SEric Dumazet 	rcu_read_unlock();
3361da177e4SLinus Torvalds out:
337d0941130SJamie Bainbridge 	if (!rc)
338d0941130SJamie Bainbridge 		__ICMP_INC_STATS(net, ICMP_MIB_RATELIMITHOST);
339662ec522SEric Dumazet 	else
340662ec522SEric Dumazet 		icmp_global_consume();
3411da177e4SLinus Torvalds 	return rc;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds /*
3451da177e4SLinus Torvalds  *	Maintain the counters used in the SNMP statistics for outgoing ICMP
3461da177e4SLinus Torvalds  */
icmp_out_count(struct net * net,unsigned char type)3470388b004SPavel Emelyanov void icmp_out_count(struct net *net, unsigned char type)
3481da177e4SLinus Torvalds {
349903fc196SPavel Emelyanov 	ICMPMSGOUT_INC_STATS(net, type);
35075c939bbSPavel Emelyanov 	ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  *	Checksum each fragment, and on the first include the headers and final
3551da177e4SLinus Torvalds  *	checksum.
3561da177e4SLinus Torvalds  */
icmp_glue_bits(void * from,char * to,int offset,int len,int odd,struct sk_buff * skb)3571da177e4SLinus Torvalds static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
3581da177e4SLinus Torvalds 			  struct sk_buff *skb)
3591da177e4SLinus Torvalds {
3602e47eeceSYu Zhe 	struct icmp_bxm *icmp_param = from;
3615f92a738SAl Viro 	__wsum csum;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	csum = skb_copy_and_csum_bits(icmp_param->skb,
3641da177e4SLinus Torvalds 				      icmp_param->offset + offset,
3658d5930dfSAl Viro 				      to, len);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
3681da177e4SLinus Torvalds 	if (icmp_pointers[icmp_param->data.icmph.type].error)
3691da177e4SLinus Torvalds 		nf_ct_attach(skb, icmp_param->skb);
3701da177e4SLinus Torvalds 	return 0;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
icmp_push_reply(struct sock * sk,struct icmp_bxm * icmp_param,struct flowi4 * fl4,struct ipcm_cookie * ipc,struct rtable ** rt)373a15c89c7SEric Dumazet static void icmp_push_reply(struct sock *sk,
374a15c89c7SEric Dumazet 			    struct icmp_bxm *icmp_param,
37577968b78SDavid S. Miller 			    struct flowi4 *fl4,
3762e77d89bSEric Dumazet 			    struct ipcm_cookie *ipc, struct rtable **rt)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct sk_buff *skb;
3791da177e4SLinus Torvalds 
380f5fca608SDavid S. Miller 	if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
3811da177e4SLinus Torvalds 			   icmp_param->data_len+icmp_param->head_len,
3821da177e4SLinus Torvalds 			   icmp_param->head_len,
3831f8438a8SEric Dumazet 			   ipc, rt, MSG_DONTWAIT) < 0) {
3845d3848bcSEric Dumazet 		__ICMP_INC_STATS(sock_net(sk), ICMP_MIB_OUTERRORS);
3851e3cf683SDenis V. Lunev 		ip_flush_pending_frames(sk);
3861f8438a8SEric Dumazet 	} else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
38788c7664fSArnaldo Carvalho de Melo 		struct icmphdr *icmph = icmp_hdr(skb);
3883ea7ca80SAl Viro 		__wsum csum;
3891da177e4SLinus Torvalds 		struct sk_buff *skb1;
3901da177e4SLinus Torvalds 
3913ea7ca80SAl Viro 		csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
3923ea7ca80SAl Viro 						 (char *)icmph,
393cc44c17bSAl Viro 						 icmp_param->head_len);
3941e3cf683SDenis V. Lunev 		skb_queue_walk(&sk->sk_write_queue, skb1) {
3951da177e4SLinus Torvalds 			csum = csum_add(csum, skb1->csum);
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 		icmph->checksum = csum_fold(csum);
3981da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_NONE;
39977968b78SDavid S. Miller 		ip_push_pending_frames(sk, fl4);
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds /*
4041da177e4SLinus Torvalds  *	Driving logic for building and sending ICMP messages.
4051da177e4SLinus Torvalds  */
4061da177e4SLinus Torvalds 
icmp_reply(struct icmp_bxm * icmp_param,struct sk_buff * skb)4071da177e4SLinus Torvalds static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
4081da177e4SLinus Torvalds {
409511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
410338f6418SEric Dumazet 	struct net *net = dev_net_rcu(rt->dst.dev);
411662ec522SEric Dumazet 	bool apply_ratelimit = false;
412338f6418SEric Dumazet 	struct ipcm_cookie ipc;
41377968b78SDavid S. Miller 	struct flowi4 fl4;
414fdc0bde9SDenis V. Lunev 	struct sock *sk;
415fdc0bde9SDenis V. Lunev 	struct inet_sock *inet;
41635ebf65eSDavid S. Miller 	__be32 daddr, saddr;
417e110861fSLorenzo Colitti 	u32 mark = IP4_REPLY_MARK(net, skb->mark);
418c0303efeSJesper Dangaard Brouer 	int type = icmp_param->data.icmph.type;
419c0303efeSJesper Dangaard Brouer 	int code = icmp_param->data.icmph.code;
4201da177e4SLinus Torvalds 
42191ed1e66SPaolo Abeni 	if (ip_options_echo(net, &icmp_param->replyopts.opt.opt, skb))
422f00c401bSHorms 		return;
4231da177e4SLinus Torvalds 
424662ec522SEric Dumazet 	/* Needed by both icmpv4_global_allow and icmp_xmit_lock */
4257ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
4261da177e4SLinus Torvalds 
427662ec522SEric Dumazet 	/* is global icmp_msgs_per_sec exhausted ? */
428662ec522SEric Dumazet 	if (!icmpv4_global_allow(net, type, code, &apply_ratelimit))
4297ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
4307ba91ecbSJesper Dangaard Brouer 
4317ba91ecbSJesper Dangaard Brouer 	sk = icmp_xmit_lock(net);
4327ba91ecbSJesper Dangaard Brouer 	if (!sk)
4337ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
4347ba91ecbSJesper Dangaard Brouer 	inet = inet_sk(sk);
435c0303efeSJesper Dangaard Brouer 
4361da177e4SLinus Torvalds 	icmp_param->data.icmph.checksum = 0;
4371da177e4SLinus Torvalds 
43835178206SWillem de Bruijn 	ipcm_init(&ipc);
439eddc9ec5SArnaldo Carvalho de Melo 	inet->tos = ip_hdr(skb)->tos;
4400da7536fSWillem de Bruijn 	ipc.sockc.mark = mark;
4419f6abb5fSDavid S. Miller 	daddr = ipc.addr = ip_hdr(skb)->saddr;
44235ebf65eSDavid S. Miller 	saddr = fib_compute_spec_dst(skb);
443aa661581SFrancesco Fusco 
444f6d8bd05SEric Dumazet 	if (icmp_param->replyopts.opt.opt.optlen) {
445f6d8bd05SEric Dumazet 		ipc.opt = &icmp_param->replyopts.opt;
446f6d8bd05SEric Dumazet 		if (ipc.opt->opt.srr)
447f6d8bd05SEric Dumazet 			daddr = icmp_param->replyopts.opt.opt.faddr;
4481da177e4SLinus Torvalds 	}
44977968b78SDavid S. Miller 	memset(&fl4, 0, sizeof(fl4));
45077968b78SDavid S. Miller 	fl4.daddr = daddr;
45135ebf65eSDavid S. Miller 	fl4.saddr = saddr;
452e110861fSLorenzo Colitti 	fl4.flowi4_mark = mark;
453e2d118a1SLorenzo Colitti 	fl4.flowi4_uid = sock_net_uid(net, NULL);
45477968b78SDavid S. Miller 	fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
45577968b78SDavid S. Miller 	fl4.flowi4_proto = IPPROTO_ICMP;
456385add90SDavid Ahern 	fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
4573df98d79SPaul Moore 	security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
4589d6ec938SDavid S. Miller 	rt = ip_route_output_key(net, &fl4);
459b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
4601da177e4SLinus Torvalds 		goto out_unlock;
461662ec522SEric Dumazet 	if (icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
462a15c89c7SEric Dumazet 		icmp_push_reply(sk, icmp_param, &fl4, &ipc, &rt);
4631da177e4SLinus Torvalds 	ip_rt_put(rt);
4641da177e4SLinus Torvalds out_unlock:
465405666dbSDenis V. Lunev 	icmp_xmit_unlock(sk);
4667ba91ecbSJesper Dangaard Brouer out_bh_enable:
4677ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
470e1e84eb5SMathieu Desnoyers /*
471e1e84eb5SMathieu Desnoyers  * The device used for looking up which routing table to use for sending an ICMP
472e1e84eb5SMathieu Desnoyers  * error is preferably the source whenever it is set, which should ensure the
473e1e84eb5SMathieu Desnoyers  * icmp error can be sent to the source host, else lookup using the routing
474e1e84eb5SMathieu Desnoyers  * table of the destination device, else use the main routing table (index 0).
475e1e84eb5SMathieu Desnoyers  */
icmp_get_route_lookup_dev(struct sk_buff * skb)476e1e84eb5SMathieu Desnoyers static struct net_device *icmp_get_route_lookup_dev(struct sk_buff *skb)
477e1e84eb5SMathieu Desnoyers {
478e1e84eb5SMathieu Desnoyers 	struct net_device *route_lookup_dev = NULL;
479e1e84eb5SMathieu Desnoyers 
480e1e84eb5SMathieu Desnoyers 	if (skb->dev)
481e1e84eb5SMathieu Desnoyers 		route_lookup_dev = skb->dev;
482e1e84eb5SMathieu Desnoyers 	else if (skb_dst(skb))
483e1e84eb5SMathieu Desnoyers 		route_lookup_dev = skb_dst(skb)->dev;
484e1e84eb5SMathieu Desnoyers 	return route_lookup_dev;
485e1e84eb5SMathieu Desnoyers }
486e1e84eb5SMathieu Desnoyers 
icmp_route_lookup(struct net * net,struct flowi4 * fl4,struct sk_buff * skb_in,const struct iphdr * iph,__be32 saddr,dscp_t dscp,u32 mark,int type,int code,struct icmp_bxm * param)4877dde0adeSGuillaume Nault static struct rtable *icmp_route_lookup(struct net *net, struct flowi4 *fl4,
48877968b78SDavid S. Miller 					struct sk_buff *skb_in,
4897dde0adeSGuillaume Nault 					const struct iphdr *iph, __be32 saddr,
4907dde0adeSGuillaume Nault 					dscp_t dscp, u32 mark, int type,
4917dde0adeSGuillaume Nault 					int code, struct icmp_bxm *param)
492f6d460cfSDavid S. Miller {
493e1e84eb5SMathieu Desnoyers 	struct net_device *route_lookup_dev;
494f6d460cfSDavid S. Miller 	struct rtable *rt, *rt2;
495415b3334SDavid S. Miller 	struct flowi4 fl4_dec;
496f6d460cfSDavid S. Miller 	int err;
497f6d460cfSDavid S. Miller 
49877968b78SDavid S. Miller 	memset(fl4, 0, sizeof(*fl4));
49977968b78SDavid S. Miller 	fl4->daddr = (param->replyopts.opt.opt.srr ?
50077968b78SDavid S. Miller 		      param->replyopts.opt.opt.faddr : iph->saddr);
50177968b78SDavid S. Miller 	fl4->saddr = saddr;
502e110861fSLorenzo Colitti 	fl4->flowi4_mark = mark;
503e2d118a1SLorenzo Colitti 	fl4->flowi4_uid = sock_net_uid(net, NULL);
5047dde0adeSGuillaume Nault 	fl4->flowi4_tos = inet_dscp_to_dsfield(dscp);
50577968b78SDavid S. Miller 	fl4->flowi4_proto = IPPROTO_ICMP;
50677968b78SDavid S. Miller 	fl4->fl4_icmp_type = type;
50777968b78SDavid S. Miller 	fl4->fl4_icmp_code = code;
508e1e84eb5SMathieu Desnoyers 	route_lookup_dev = icmp_get_route_lookup_dev(skb_in);
509e1e84eb5SMathieu Desnoyers 	fl4->flowi4_oif = l3mdev_master_ifindex(route_lookup_dev);
510613d09b3SDavid Ahern 
5113df98d79SPaul Moore 	security_skb_classify_flow(skb_in, flowi4_to_flowi_common(fl4));
5123abd1adeSDavid Ahern 	rt = ip_route_output_key_hash(net, fl4, skb_in);
513b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
514b23dd4feSDavid S. Miller 		return rt;
515f6d460cfSDavid S. Miller 
516f6d460cfSDavid S. Miller 	/* No need to clone since we're just using its address. */
517f6d460cfSDavid S. Miller 	rt2 = rt;
518f6d460cfSDavid S. Miller 
5199d6ec938SDavid S. Miller 	rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
52077968b78SDavid S. Miller 					   flowi4_to_flowi(fl4), NULL, 0);
521452edd59SDavid S. Miller 	if (!IS_ERR(rt)) {
522f6d460cfSDavid S. Miller 		if (rt != rt2)
523f6d460cfSDavid S. Miller 			return rt;
524452edd59SDavid S. Miller 	} else if (PTR_ERR(rt) == -EPERM) {
525f6d460cfSDavid S. Miller 		rt = NULL;
526452edd59SDavid S. Miller 	} else
527452edd59SDavid S. Miller 		return rt;
528f6d460cfSDavid S. Miller 
529415b3334SDavid S. Miller 	err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
530f6d460cfSDavid S. Miller 	if (err)
531f6d460cfSDavid S. Miller 		goto relookup_failed;
532f6d460cfSDavid S. Miller 
533e1e84eb5SMathieu Desnoyers 	if (inet_addr_type_dev_table(net, route_lookup_dev,
53430bbaa19SDavid Ahern 				     fl4_dec.saddr) == RTN_LOCAL) {
535415b3334SDavid S. Miller 		rt2 = __ip_route_output_key(net, &fl4_dec);
536b23dd4feSDavid S. Miller 		if (IS_ERR(rt2))
537b23dd4feSDavid S. Miller 			err = PTR_ERR(rt2);
538f6d460cfSDavid S. Miller 	} else {
5399d6ec938SDavid S. Miller 		struct flowi4 fl4_2 = {};
540f6d460cfSDavid S. Miller 		unsigned long orefdst;
541f6d460cfSDavid S. Miller 
542415b3334SDavid S. Miller 		fl4_2.daddr = fl4_dec.saddr;
5439d6ec938SDavid S. Miller 		rt2 = ip_route_output_key(net, &fl4_2);
544b23dd4feSDavid S. Miller 		if (IS_ERR(rt2)) {
545b23dd4feSDavid S. Miller 			err = PTR_ERR(rt2);
546f6d460cfSDavid S. Miller 			goto relookup_failed;
547b23dd4feSDavid S. Miller 		}
548f6d460cfSDavid S. Miller 		/* Ugh! */
549f6d460cfSDavid S. Miller 		orefdst = skb_in->_skb_refdst; /* save old refdst */
550773a69d6SThomas Graf 		skb_dst_set(skb_in, NULL);
551415b3334SDavid S. Miller 		err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
552*661c63cbSGuillaume Nault 				     dscp, rt2->dst.dev);
553f6d460cfSDavid S. Miller 
554f6d460cfSDavid S. Miller 		dst_release(&rt2->dst);
555f6d460cfSDavid S. Miller 		rt2 = skb_rtable(skb_in);
556f6d460cfSDavid S. Miller 		skb_in->_skb_refdst = orefdst; /* restore old refdst */
557f6d460cfSDavid S. Miller 	}
558f6d460cfSDavid S. Miller 
559f6d460cfSDavid S. Miller 	if (err)
560f6d460cfSDavid S. Miller 		goto relookup_failed;
561f6d460cfSDavid S. Miller 
5629d6ec938SDavid S. Miller 	rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
563415b3334SDavid S. Miller 					    flowi4_to_flowi(&fl4_dec), NULL,
5649d6ec938SDavid S. Miller 					    XFRM_LOOKUP_ICMP);
565452edd59SDavid S. Miller 	if (!IS_ERR(rt2)) {
566f6d460cfSDavid S. Miller 		dst_release(&rt->dst);
567415b3334SDavid S. Miller 		memcpy(fl4, &fl4_dec, sizeof(*fl4));
568f6d460cfSDavid S. Miller 		rt = rt2;
569452edd59SDavid S. Miller 	} else if (PTR_ERR(rt2) == -EPERM) {
570452edd59SDavid S. Miller 		if (rt)
571452edd59SDavid S. Miller 			dst_release(&rt->dst);
572452edd59SDavid S. Miller 		return rt2;
573452edd59SDavid S. Miller 	} else {
574452edd59SDavid S. Miller 		err = PTR_ERR(rt2);
575452edd59SDavid S. Miller 		goto relookup_failed;
576f6d460cfSDavid S. Miller 	}
577f6d460cfSDavid S. Miller 	return rt;
578f6d460cfSDavid S. Miller 
579f6d460cfSDavid S. Miller relookup_failed:
580f6d460cfSDavid S. Miller 	if (rt)
581f6d460cfSDavid S. Miller 		return rt;
582f6d460cfSDavid S. Miller 	return ERR_PTR(err);
583f6d460cfSDavid S. Miller }
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds /*
5861da177e4SLinus Torvalds  *	Send an ICMP message in response to a situation
5871da177e4SLinus Torvalds  *
5881da177e4SLinus Torvalds  *	RFC 1122: 3.2.2	MUST send at least the IP header and 8 bytes of header.
5891da177e4SLinus Torvalds  *		  MAY send more (we do).
5901da177e4SLinus Torvalds  *			MUST NOT change this header information.
5911da177e4SLinus Torvalds  *			MUST NOT reply to a multicast/broadcast IP address.
5921da177e4SLinus Torvalds  *			MUST NOT reply to a multicast/broadcast MAC address.
5931da177e4SLinus Torvalds  *			MUST reply to only the first fragment.
5941da177e4SLinus Torvalds  */
5951da177e4SLinus Torvalds 
__icmp_send(struct sk_buff * skb_in,int type,int code,__be32 info,const struct ip_options * opt)5969ef6b42aSNazarov Sergey void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info,
5979ef6b42aSNazarov Sergey 		 const struct ip_options *opt)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	struct iphdr *iph;
6001da177e4SLinus Torvalds 	int room;
6018d9ba388SJesper Dangaard Brouer 	struct icmp_bxm icmp_param;
602511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb_in);
603662ec522SEric Dumazet 	bool apply_ratelimit = false;
6041da177e4SLinus Torvalds 	struct ipcm_cookie ipc;
60577968b78SDavid S. Miller 	struct flowi4 fl4;
606a61ced5dSAl Viro 	__be32 saddr;
6071da177e4SLinus Torvalds 	u8  tos;
608e110861fSLorenzo Colitti 	u32 mark;
609dde1bc0eSDenis V. Lunev 	struct net *net;
6104a6ad7a1SDenis V. Lunev 	struct sock *sk;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	if (!rt)
613338f6418SEric Dumazet 		return;
614338f6418SEric Dumazet 
615338f6418SEric Dumazet 	rcu_read_lock();
616e2c69393SHangbin Liu 
617e2c69393SHangbin Liu 	if (rt->dst.dev)
618338f6418SEric Dumazet 		net = dev_net_rcu(rt->dst.dev);
619e2c69393SHangbin Liu 	else if (skb_in->dev)
620338f6418SEric Dumazet 		net = dev_net_rcu(skb_in->dev);
621e2c69393SHangbin Liu 	else
622e2c69393SHangbin Liu 		goto out;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	/*
6251da177e4SLinus Torvalds 	 *	Find the original header. It is expected to be valid, of course.
6261da177e4SLinus Torvalds 	 *	Check this, icmp_send is called from the most obscure devices
6271da177e4SLinus Torvalds 	 *	sometimes.
6281da177e4SLinus Torvalds 	 */
629eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb_in);
6301da177e4SLinus Torvalds 
63127a884dcSArnaldo Carvalho de Melo 	if ((u8 *)iph < skb_in->head ||
632f7c0c2aeSSimon Horman 	    (skb_network_header(skb_in) + sizeof(*iph)) >
633f7c0c2aeSSimon Horman 	    skb_tail_pointer(skb_in))
6341da177e4SLinus Torvalds 		goto out;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	/*
6371da177e4SLinus Torvalds 	 *	No replies to physical multicast/broadcast
6381da177e4SLinus Torvalds 	 */
6391da177e4SLinus Torvalds 	if (skb_in->pkt_type != PACKET_HOST)
6401da177e4SLinus Torvalds 		goto out;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	/*
6431da177e4SLinus Torvalds 	 *	Now check at the protocol level
6441da177e4SLinus Torvalds 	 */
6451da177e4SLinus Torvalds 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
6461da177e4SLinus Torvalds 		goto out;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	/*
6491da177e4SLinus Torvalds 	 *	Only reply to fragment 0. We byte re-order the constant
6501da177e4SLinus Torvalds 	 *	mask for efficiency.
6511da177e4SLinus Torvalds 	 */
6521da177e4SLinus Torvalds 	if (iph->frag_off & htons(IP_OFFSET))
6531da177e4SLinus Torvalds 		goto out;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	/*
6561da177e4SLinus Torvalds 	 *	If we send an ICMP error to an ICMP error a mess would result..
6571da177e4SLinus Torvalds 	 */
6581da177e4SLinus Torvalds 	if (icmp_pointers[type].error) {
6591da177e4SLinus Torvalds 		/*
6601da177e4SLinus Torvalds 		 *	We are an error, check if we are replying to an
6611da177e4SLinus Torvalds 		 *	ICMP error
6621da177e4SLinus Torvalds 		 */
6631da177e4SLinus Torvalds 		if (iph->protocol == IPPROTO_ICMP) {
6641da177e4SLinus Torvalds 			u8 _inner_type, *itp;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 			itp = skb_header_pointer(skb_in,
667d56f90a7SArnaldo Carvalho de Melo 						 skb_network_header(skb_in) +
6681da177e4SLinus Torvalds 						 (iph->ihl << 2) +
6691da177e4SLinus Torvalds 						 offsetof(struct icmphdr,
6701da177e4SLinus Torvalds 							  type) -
6711da177e4SLinus Torvalds 						 skb_in->data,
6721da177e4SLinus Torvalds 						 sizeof(_inner_type),
6731da177e4SLinus Torvalds 						 &_inner_type);
67451456b29SIan Morris 			if (!itp)
6751da177e4SLinus Torvalds 				goto out;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 			/*
6781da177e4SLinus Torvalds 			 *	Assume any unknown ICMP type is an error. This
6791da177e4SLinus Torvalds 			 *	isn't specified by the RFC, but think about it..
6801da177e4SLinus Torvalds 			 */
6811da177e4SLinus Torvalds 			if (*itp > NR_ICMP_TYPES ||
6821da177e4SLinus Torvalds 			    icmp_pointers[*itp].error)
6831da177e4SLinus Torvalds 				goto out;
6841da177e4SLinus Torvalds 		}
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 
687662ec522SEric Dumazet 	/* Needed by both icmpv4_global_allow and icmp_xmit_lock */
6887ba91ecbSJesper Dangaard Brouer 	local_bh_disable();
689c0303efeSJesper Dangaard Brouer 
690849a44deSJesper Dangaard Brouer 	/* Check global sysctl_icmp_msgs_per_sec ratelimit, unless
691849a44deSJesper Dangaard Brouer 	 * incoming dev is loopback.  If outgoing dev change to not be
692849a44deSJesper Dangaard Brouer 	 * loopback, then peer ratelimit still work (in icmpv4_xrlim_allow)
693849a44deSJesper Dangaard Brouer 	 */
694849a44deSJesper Dangaard Brouer 	if (!(skb_in->dev && (skb_in->dev->flags&IFF_LOOPBACK)) &&
695662ec522SEric Dumazet 	      !icmpv4_global_allow(net, type, code, &apply_ratelimit))
6967ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
6977ba91ecbSJesper Dangaard Brouer 
6987ba91ecbSJesper Dangaard Brouer 	sk = icmp_xmit_lock(net);
6997ba91ecbSJesper Dangaard Brouer 	if (!sk)
7007ba91ecbSJesper Dangaard Brouer 		goto out_bh_enable;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	/*
7031da177e4SLinus Torvalds 	 *	Construct source address and options.
7041da177e4SLinus Torvalds 	 */
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	saddr = iph->daddr;
70702a6d613SPaolo Abeni 	if (!(rt->rt_flags & RTCF_LOCAL)) {
7086e1d9103SPatrick McHardy 		struct net_device *dev = NULL;
7096e1d9103SPatrick McHardy 
710685c7944SEric Dumazet 		rcu_read_lock();
711c7537967SDavid S. Miller 		if (rt_is_input_route(rt) &&
712d2efabceSKuniyuki Iwashima 		    READ_ONCE(net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr))
71392101b3bSDavid S. Miller 			dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
7146e1d9103SPatrick McHardy 
715685c7944SEric Dumazet 		if (dev)
7162adf81c0SFrancesco Ruggeri 			saddr = inet_select_addr(dev, iph->saddr,
7172adf81c0SFrancesco Ruggeri 						 RT_SCOPE_LINK);
718685c7944SEric Dumazet 		else
7191da177e4SLinus Torvalds 			saddr = 0;
720685c7944SEric Dumazet 		rcu_read_unlock();
7211c2fb7f9SJ. Simonetti 	}
7221da177e4SLinus Torvalds 
723373c15c2SMiaohe Lin 	tos = icmp_pointers[type].error ? (RT_TOS(iph->tos) |
7241da177e4SLinus Torvalds 					   IPTOS_PREC_INTERNETCONTROL) :
7251da177e4SLinus Torvalds 					   iph->tos;
726e110861fSLorenzo Colitti 	mark = IP4_REPLY_MARK(net, skb_in->mark);
7271da177e4SLinus Torvalds 
7289ef6b42aSNazarov Sergey 	if (__ip_options_echo(net, &icmp_param.replyopts.opt.opt, skb_in, opt))
729fa60cf7fSHerbert Xu 		goto out_unlock;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	/*
7331da177e4SLinus Torvalds 	 *	Prepare data for ICMP header.
7341da177e4SLinus Torvalds 	 */
7351da177e4SLinus Torvalds 
7368d9ba388SJesper Dangaard Brouer 	icmp_param.data.icmph.type	 = type;
7378d9ba388SJesper Dangaard Brouer 	icmp_param.data.icmph.code	 = code;
7388d9ba388SJesper Dangaard Brouer 	icmp_param.data.icmph.un.gateway = info;
7398d9ba388SJesper Dangaard Brouer 	icmp_param.data.icmph.checksum	 = 0;
7408d9ba388SJesper Dangaard Brouer 	icmp_param.skb	  = skb_in;
7418d9ba388SJesper Dangaard Brouer 	icmp_param.offset = skb_network_offset(skb_in);
742405666dbSDenis V. Lunev 	inet_sk(sk)->tos = tos;
74335178206SWillem de Bruijn 	ipcm_init(&ipc);
7441da177e4SLinus Torvalds 	ipc.addr = iph->saddr;
7458d9ba388SJesper Dangaard Brouer 	ipc.opt = &icmp_param.replyopts.opt;
7460da7536fSWillem de Bruijn 	ipc.sockc.mark = mark;
7471da177e4SLinus Torvalds 
7487dde0adeSGuillaume Nault 	rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr,
7497dde0adeSGuillaume Nault 			       inet_dsfield_to_dscp(tos), mark, type, code,
7507dde0adeSGuillaume Nault 			       &icmp_param);
751f6d460cfSDavid S. Miller 	if (IS_ERR(rt))
7528b7817f3SHerbert Xu 		goto out_unlock;
7538b7817f3SHerbert Xu 
754c0303efeSJesper Dangaard Brouer 	/* peer icmp_ratelimit */
755662ec522SEric Dumazet 	if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
7561da177e4SLinus Torvalds 		goto ende;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	/* RFC says return as much as we can without exceeding 576 bytes. */
7591da177e4SLinus Torvalds 
760d8d1f30bSChangli Gao 	room = dst_mtu(&rt->dst);
7611da177e4SLinus Torvalds 	if (room > 576)
7621da177e4SLinus Torvalds 		room = 576;
7638d9ba388SJesper Dangaard Brouer 	room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
7641da177e4SLinus Torvalds 	room -= sizeof(struct icmphdr);
7657d63b671SEric Dumazet 	/* Guard against tiny mtu. We need to include at least one
7667d63b671SEric Dumazet 	 * IP network header for this message to make any sense.
7677d63b671SEric Dumazet 	 */
7687d63b671SEric Dumazet 	if (room <= (int)sizeof(struct iphdr))
7697d63b671SEric Dumazet 		goto ende;
7701da177e4SLinus Torvalds 
7718d9ba388SJesper Dangaard Brouer 	icmp_param.data_len = skb_in->len - icmp_param.offset;
7728d9ba388SJesper Dangaard Brouer 	if (icmp_param.data_len > room)
7738d9ba388SJesper Dangaard Brouer 		icmp_param.data_len = room;
7748d9ba388SJesper Dangaard Brouer 	icmp_param.head_len = sizeof(struct icmphdr);
7751da177e4SLinus Torvalds 
77632182747SToke Høiland-Jørgensen 	/* if we don't have a source address at this point, fall back to the
77732182747SToke Høiland-Jørgensen 	 * dummy address instead of sending out a packet with a source address
77832182747SToke Høiland-Jørgensen 	 * of 0.0.0.0
77932182747SToke Høiland-Jørgensen 	 */
78032182747SToke Høiland-Jørgensen 	if (!fl4.saddr)
78132182747SToke Høiland-Jørgensen 		fl4.saddr = htonl(INADDR_DUMMY);
78232182747SToke Høiland-Jørgensen 
78321b28f97SPeilin He 	trace_icmp_send(skb_in, type, code);
78421b28f97SPeilin He 
785a15c89c7SEric Dumazet 	icmp_push_reply(sk, &icmp_param, &fl4, &ipc, &rt);
7861da177e4SLinus Torvalds ende:
7871da177e4SLinus Torvalds 	ip_rt_put(rt);
7881da177e4SLinus Torvalds out_unlock:
789405666dbSDenis V. Lunev 	icmp_xmit_unlock(sk);
7907ba91ecbSJesper Dangaard Brouer out_bh_enable:
7917ba91ecbSJesper Dangaard Brouer 	local_bh_enable();
792338f6418SEric Dumazet out:
793338f6418SEric Dumazet 	rcu_read_unlock();
7941da177e4SLinus Torvalds }
7959ef6b42aSNazarov Sergey EXPORT_SYMBOL(__icmp_send);
7961da177e4SLinus Torvalds 
7970b41713bSJason A. Donenfeld #if IS_ENABLED(CONFIG_NF_NAT)
7980b41713bSJason A. Donenfeld #include <net/netfilter/nf_conntrack.h>
icmp_ndo_send(struct sk_buff * skb_in,int type,int code,__be32 info)7990b41713bSJason A. Donenfeld void icmp_ndo_send(struct sk_buff *skb_in, int type, int code, __be32 info)
8000b41713bSJason A. Donenfeld {
8010b41713bSJason A. Donenfeld 	struct sk_buff *cloned_skb = NULL;
802ee576c47SJason A. Donenfeld 	struct ip_options opts = { 0 };
8030b41713bSJason A. Donenfeld 	enum ip_conntrack_info ctinfo;
8040b41713bSJason A. Donenfeld 	struct nf_conn *ct;
8050b41713bSJason A. Donenfeld 	__be32 orig_ip;
8060b41713bSJason A. Donenfeld 
8070b41713bSJason A. Donenfeld 	ct = nf_ct_get(skb_in, &ctinfo);
8080b41713bSJason A. Donenfeld 	if (!ct || !(ct->status & IPS_SRC_NAT)) {
809ee576c47SJason A. Donenfeld 		__icmp_send(skb_in, type, code, info, &opts);
8100b41713bSJason A. Donenfeld 		return;
8110b41713bSJason A. Donenfeld 	}
8120b41713bSJason A. Donenfeld 
8130b41713bSJason A. Donenfeld 	if (skb_shared(skb_in))
8140b41713bSJason A. Donenfeld 		skb_in = cloned_skb = skb_clone(skb_in, GFP_ATOMIC);
8150b41713bSJason A. Donenfeld 
8160b41713bSJason A. Donenfeld 	if (unlikely(!skb_in || skb_network_header(skb_in) < skb_in->head ||
8170b41713bSJason A. Donenfeld 	    (skb_network_header(skb_in) + sizeof(struct iphdr)) >
8180b41713bSJason A. Donenfeld 	    skb_tail_pointer(skb_in) || skb_ensure_writable(skb_in,
8190b41713bSJason A. Donenfeld 	    skb_network_offset(skb_in) + sizeof(struct iphdr))))
8200b41713bSJason A. Donenfeld 		goto out;
8210b41713bSJason A. Donenfeld 
8220b41713bSJason A. Donenfeld 	orig_ip = ip_hdr(skb_in)->saddr;
8230b41713bSJason A. Donenfeld 	ip_hdr(skb_in)->saddr = ct->tuplehash[0].tuple.src.u3.ip;
824ee576c47SJason A. Donenfeld 	__icmp_send(skb_in, type, code, info, &opts);
8250b41713bSJason A. Donenfeld 	ip_hdr(skb_in)->saddr = orig_ip;
8260b41713bSJason A. Donenfeld out:
8270b41713bSJason A. Donenfeld 	consume_skb(cloned_skb);
8280b41713bSJason A. Donenfeld }
8290b41713bSJason A. Donenfeld EXPORT_SYMBOL(icmp_ndo_send);
8300b41713bSJason A. Donenfeld #endif
8311da177e4SLinus Torvalds 
icmp_socket_deliver(struct sk_buff * skb,u32 info)8321de9243bSDavid S. Miller static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
8331de9243bSDavid S. Miller {
8341de9243bSDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *)skb->data;
8351de9243bSDavid S. Miller 	const struct net_protocol *ipprot;
8361de9243bSDavid S. Miller 	int protocol = iph->protocol;
8371de9243bSDavid S. Miller 
838f0a70e90SDavid S. Miller 	/* Checkin full IP header plus 8 bytes of protocol to
839f0a70e90SDavid S. Miller 	 * avoid additional coding at protocol handlers.
840f0a70e90SDavid S. Miller 	 */
8417304fe46SDuan Jiong 	if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
842338f6418SEric Dumazet 		__ICMP_INC_STATS(dev_net_rcu(skb->dev), ICMP_MIB_INERRORS);
843f0a70e90SDavid S. Miller 		return;
8447304fe46SDuan Jiong 	}
845f0a70e90SDavid S. Miller 
8461de9243bSDavid S. Miller 	raw_icmp_error(skb, protocol, info);
8471de9243bSDavid S. Miller 
8481de9243bSDavid S. Miller 	ipprot = rcu_dereference(inet_protos[protocol]);
8491de9243bSDavid S. Miller 	if (ipprot && ipprot->err_handler)
8501de9243bSDavid S. Miller 		ipprot->err_handler(skb, info);
8511de9243bSDavid S. Miller }
8521de9243bSDavid S. Miller 
icmp_tag_validation(int proto)8538ed1dc44SHannes Frederic Sowa static bool icmp_tag_validation(int proto)
8548ed1dc44SHannes Frederic Sowa {
8558ed1dc44SHannes Frederic Sowa 	bool ok;
8568ed1dc44SHannes Frederic Sowa 
8578ed1dc44SHannes Frederic Sowa 	rcu_read_lock();
8588ed1dc44SHannes Frederic Sowa 	ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
8598ed1dc44SHannes Frederic Sowa 	rcu_read_unlock();
8608ed1dc44SHannes Frederic Sowa 	return ok;
8618ed1dc44SHannes Frederic Sowa }
8628ed1dc44SHannes Frederic Sowa 
8631da177e4SLinus Torvalds /*
864258bbb1bSMatteo Croce  *	Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEEDED, ICMP_QUENCH, and
86508578d8dSRami Rosen  *	ICMP_PARAMETERPROB.
8661da177e4SLinus Torvalds  */
8671da177e4SLinus Torvalds 
icmp_unreach(struct sk_buff * skb)868b384c95aSMenglong Dong static enum skb_drop_reason icmp_unreach(struct sk_buff *skb)
8691da177e4SLinus Torvalds {
870b384c95aSMenglong Dong 	enum skb_drop_reason reason = SKB_NOT_DROPPED_YET;
871b71d1d42SEric Dumazet 	const struct iphdr *iph;
8721da177e4SLinus Torvalds 	struct icmphdr *icmph;
873dde1bc0eSDenis V. Lunev 	struct net *net;
874f9242b6bSDavid S. Miller 	u32 info = 0;
875dde1bc0eSDenis V. Lunev 
876338f6418SEric Dumazet 	net = dev_net_rcu(skb_dst(skb)->dev);
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	/*
8791da177e4SLinus Torvalds 	 *	Incomplete header ?
8801da177e4SLinus Torvalds 	 * 	Only checks for the IP header, there should be an
8811da177e4SLinus Torvalds 	 *	additional check for longer headers in upper levels.
8821da177e4SLinus Torvalds 	 */
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
8851da177e4SLinus Torvalds 		goto out_err;
8861da177e4SLinus Torvalds 
88788c7664fSArnaldo Carvalho de Melo 	icmph = icmp_hdr(skb);
888b71d1d42SEric Dumazet 	iph   = (const struct iphdr *)skb->data;
8891da177e4SLinus Torvalds 
890b384c95aSMenglong Dong 	if (iph->ihl < 5)  { /* Mangled header, drop. */
891b384c95aSMenglong Dong 		reason = SKB_DROP_REASON_IP_INHDR;
8921da177e4SLinus Torvalds 		goto out_err;
893b384c95aSMenglong Dong 	}
8941da177e4SLinus Torvalds 
895258bbb1bSMatteo Croce 	switch (icmph->type) {
896258bbb1bSMatteo Croce 	case ICMP_DEST_UNREACH:
8971da177e4SLinus Torvalds 		switch (icmph->code & 15) {
8981da177e4SLinus Torvalds 		case ICMP_NET_UNREACH:
8991da177e4SLinus Torvalds 		case ICMP_HOST_UNREACH:
9001da177e4SLinus Torvalds 		case ICMP_PROT_UNREACH:
9011da177e4SLinus Torvalds 		case ICMP_PORT_UNREACH:
9021da177e4SLinus Torvalds 			break;
9031da177e4SLinus Torvalds 		case ICMP_FRAG_NEEDED:
9048ed1dc44SHannes Frederic Sowa 			/* for documentation of the ip_no_pmtu_disc
9058ed1dc44SHannes Frederic Sowa 			 * values please see
9061cec2cacSMauro Carvalho Chehab 			 * Documentation/networking/ip-sysctl.rst
9078ed1dc44SHannes Frederic Sowa 			 */
9080968d2a4SKuniyuki Iwashima 			switch (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc)) {
9098ed1dc44SHannes Frederic Sowa 			default:
910ba7a46f1SJoe Perches 				net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
911673d57e7SHarvey Harrison 						    &iph->daddr);
9128ed1dc44SHannes Frederic Sowa 				break;
9138ed1dc44SHannes Frederic Sowa 			case 2:
9148ed1dc44SHannes Frederic Sowa 				goto out;
9158ed1dc44SHannes Frederic Sowa 			case 3:
9168ed1dc44SHannes Frederic Sowa 				if (!icmp_tag_validation(iph->protocol))
9178ed1dc44SHannes Frederic Sowa 					goto out;
918a8eceea8SJoe Perches 				fallthrough;
9198ed1dc44SHannes Frederic Sowa 			case 0:
92046517008SDavid S. Miller 				info = ntohs(icmph->un.frag.mtu);
9211da177e4SLinus Torvalds 			}
9221da177e4SLinus Torvalds 			break;
9231da177e4SLinus Torvalds 		case ICMP_SR_FAILED:
924ba7a46f1SJoe Perches 			net_dbg_ratelimited("%pI4: Source Route Failed\n",
925673d57e7SHarvey Harrison 					    &iph->daddr);
9261da177e4SLinus Torvalds 			break;
9271da177e4SLinus Torvalds 		default:
9281da177e4SLinus Torvalds 			break;
9291da177e4SLinus Torvalds 		}
9301da177e4SLinus Torvalds 		if (icmph->code > NR_ICMP_UNREACH)
9311da177e4SLinus Torvalds 			goto out;
932258bbb1bSMatteo Croce 		break;
933258bbb1bSMatteo Croce 	case ICMP_PARAMETERPROB:
9341da177e4SLinus Torvalds 		info = ntohl(icmph->un.gateway) >> 24;
935258bbb1bSMatteo Croce 		break;
936258bbb1bSMatteo Croce 	case ICMP_TIME_EXCEEDED:
937258bbb1bSMatteo Croce 		__ICMP_INC_STATS(net, ICMP_MIB_INTIMEEXCDS);
938258bbb1bSMatteo Croce 		if (icmph->code == ICMP_EXC_FRAGTIME)
939258bbb1bSMatteo Croce 			goto out;
940258bbb1bSMatteo Croce 		break;
941258bbb1bSMatteo Croce 	}
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/*
9441da177e4SLinus Torvalds 	 *	Throw it at our lower layers
9451da177e4SLinus Torvalds 	 *
9461da177e4SLinus Torvalds 	 *	RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
9471da177e4SLinus Torvalds 	 *		  header.
9481da177e4SLinus Torvalds 	 *	RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
9491da177e4SLinus Torvalds 	 *		  transport layer.
9501da177e4SLinus Torvalds 	 *	RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
9511da177e4SLinus Torvalds 	 *		  transport layer.
9521da177e4SLinus Torvalds 	 */
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/*
95525985edcSLucas De Marchi 	 *	Check the other end isn't violating RFC 1122. Some routers send
9561da177e4SLinus Torvalds 	 *	bogus responses to broadcast frames. If you see this message
9571da177e4SLinus Torvalds 	 *	first check your netmask matches at both ends, if it does then
9581da177e4SLinus Torvalds 	 *	get the other vendor to fix their kit.
9591da177e4SLinus Torvalds 	 */
9601da177e4SLinus Torvalds 
961b04f9b7eSKuniyuki Iwashima 	if (!READ_ONCE(net->ipv4.sysctl_icmp_ignore_bogus_error_responses) &&
96230bbaa19SDavid Ahern 	    inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
963e87cc472SJoe Perches 		net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
964673d57e7SHarvey Harrison 				     &ip_hdr(skb)->saddr,
9651da177e4SLinus Torvalds 				     icmph->type, icmph->code,
966058bd4d2SJoe Perches 				     &iph->daddr, skb->dev->name);
9671da177e4SLinus Torvalds 		goto out;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 
9701de9243bSDavid S. Miller 	icmp_socket_deliver(skb, info);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds out:
973b384c95aSMenglong Dong 	return reason;
9741da177e4SLinus Torvalds out_err:
9755d3848bcSEric Dumazet 	__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
976b384c95aSMenglong Dong 	return reason ?: SKB_DROP_REASON_NOT_SPECIFIED;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds /*
9811da177e4SLinus Torvalds  *	Handle ICMP_REDIRECT.
9821da177e4SLinus Torvalds  */
9831da177e4SLinus Torvalds 
icmp_redirect(struct sk_buff * skb)984b384c95aSMenglong Dong static enum skb_drop_reason icmp_redirect(struct sk_buff *skb)
9851da177e4SLinus Torvalds {
98694206125SDavid S. Miller 	if (skb->len < sizeof(struct iphdr)) {
987338f6418SEric Dumazet 		__ICMP_INC_STATS(dev_net_rcu(skb->dev), ICMP_MIB_INERRORS);
988b384c95aSMenglong Dong 		return SKB_DROP_REASON_PKT_TOO_SMALL;
9891da177e4SLinus Torvalds 	}
990c319b4d7SVasiliy Kulikov 
991e3e32170SRick Jones 	if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
992e3e32170SRick Jones 		/* there aught to be a stat */
993b384c95aSMenglong Dong 		return SKB_DROP_REASON_NOMEM;
994e3e32170SRick Jones 	}
99594206125SDavid S. Miller 
9960f404bbdSLi RongQing 	icmp_socket_deliver(skb, ntohl(icmp_hdr(skb)->un.gateway));
997b384c95aSMenglong Dong 	return SKB_NOT_DROPPED_YET;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds /*
1001d329ea5bSAndreas Roeseler  *	Handle ICMP_ECHO ("ping") and ICMP_EXT_ECHO ("PROBE") requests.
10021da177e4SLinus Torvalds  *
10031da177e4SLinus Torvalds  *	RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
10041da177e4SLinus Torvalds  *		  requests.
10051da177e4SLinus Torvalds  *	RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
10061da177e4SLinus Torvalds  *		  included in the reply.
10071da177e4SLinus Torvalds  *	RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
10081da177e4SLinus Torvalds  *		  echo requests, MUST have default=NOT.
1009d329ea5bSAndreas Roeseler  *	RFC 8335: 8 MUST have a config option to enable/disable ICMP
1010d329ea5bSAndreas Roeseler  *		  Extended Echo Functionality, MUST be disabled by default
10111da177e4SLinus Torvalds  *	See also WRT handling of options once they are done and working.
10121da177e4SLinus Torvalds  */
10131da177e4SLinus Torvalds 
icmp_echo(struct sk_buff * skb)1014b384c95aSMenglong Dong static enum skb_drop_reason icmp_echo(struct sk_buff *skb)
10151da177e4SLinus Torvalds {
1016d329ea5bSAndreas Roeseler 	struct icmp_bxm icmp_param;
1017b34a95eeSPavel Emelyanov 	struct net *net;
1018b34a95eeSPavel Emelyanov 
1019338f6418SEric Dumazet 	net = dev_net_rcu(skb_dst(skb)->dev);
1020d329ea5bSAndreas Roeseler 	/* should there be an ICMP stat for ignored echos? */
1021bb7bb35aSKuniyuki Iwashima 	if (READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_all))
1022b384c95aSMenglong Dong 		return SKB_NOT_DROPPED_YET;
10231da177e4SLinus Torvalds 
102488c7664fSArnaldo Carvalho de Melo 	icmp_param.data.icmph	   = *icmp_hdr(skb);
10251da177e4SLinus Torvalds 	icmp_param.skb		   = skb;
10261da177e4SLinus Torvalds 	icmp_param.offset	   = 0;
10271da177e4SLinus Torvalds 	icmp_param.data_len	   = skb->len;
10281da177e4SLinus Torvalds 	icmp_param.head_len	   = sizeof(struct icmphdr);
1029d329ea5bSAndreas Roeseler 
10301fd07f33SAndreas Roeseler 	if (icmp_param.data.icmph.type == ICMP_ECHO)
1031d329ea5bSAndreas Roeseler 		icmp_param.data.icmph.type = ICMP_ECHOREPLY;
10321fd07f33SAndreas Roeseler 	else if (!icmp_build_probe(skb, &icmp_param.data.icmph))
1033b384c95aSMenglong Dong 		return SKB_NOT_DROPPED_YET;
10341fd07f33SAndreas Roeseler 
10351fd07f33SAndreas Roeseler 	icmp_reply(&icmp_param, skb);
1036b384c95aSMenglong Dong 	return SKB_NOT_DROPPED_YET;
10371fd07f33SAndreas Roeseler }
10381fd07f33SAndreas Roeseler 
10391fd07f33SAndreas Roeseler /*	Helper for icmp_echo and icmpv6_echo_reply.
10401fd07f33SAndreas Roeseler  *	Searches for net_device that matches PROBE interface identifier
10411fd07f33SAndreas Roeseler  *		and builds PROBE reply message in icmphdr.
10421fd07f33SAndreas Roeseler  *
10431fd07f33SAndreas Roeseler  *	Returns false if PROBE responses are disabled via sysctl
10441fd07f33SAndreas Roeseler  */
10451fd07f33SAndreas Roeseler 
icmp_build_probe(struct sk_buff * skb,struct icmphdr * icmphdr)10461fd07f33SAndreas Roeseler bool icmp_build_probe(struct sk_buff *skb, struct icmphdr *icmphdr)
10471fd07f33SAndreas Roeseler {
1048338f6418SEric Dumazet 	struct net *net = dev_net_rcu(skb->dev);
10491fd07f33SAndreas Roeseler 	struct icmp_ext_hdr *ext_hdr, _ext_hdr;
10501fd07f33SAndreas Roeseler 	struct icmp_ext_echo_iio *iio, _iio;
1051d68dc711SEric Dumazet 	struct inet6_dev *in6_dev;
1052d68dc711SEric Dumazet 	struct in_device *in_dev;
10531fd07f33SAndreas Roeseler 	struct net_device *dev;
10541fd07f33SAndreas Roeseler 	char buff[IFNAMSIZ];
10551fd07f33SAndreas Roeseler 	u16 ident_len;
10561fd07f33SAndreas Roeseler 	u8 status;
10571fd07f33SAndreas Roeseler 
10584a2f7083SKuniyuki Iwashima 	if (!READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe))
10591fd07f33SAndreas Roeseler 		return false;
10601fd07f33SAndreas Roeseler 
1061d329ea5bSAndreas Roeseler 	/* We currently only support probing interfaces on the proxy node
1062d329ea5bSAndreas Roeseler 	 * Check to ensure L-bit is set
1063d329ea5bSAndreas Roeseler 	 */
10641fd07f33SAndreas Roeseler 	if (!(ntohs(icmphdr->un.echo.sequence) & 1))
10651fd07f33SAndreas Roeseler 		return false;
1066d329ea5bSAndreas Roeseler 	/* Clear status bits in reply message */
10671fd07f33SAndreas Roeseler 	icmphdr->un.echo.sequence &= htons(0xFF00);
10681fd07f33SAndreas Roeseler 	if (icmphdr->type == ICMP_EXT_ECHO)
10691fd07f33SAndreas Roeseler 		icmphdr->type = ICMP_EXT_ECHOREPLY;
10701fd07f33SAndreas Roeseler 	else
10711fd07f33SAndreas Roeseler 		icmphdr->type = ICMPV6_EXT_ECHO_REPLY;
1072d329ea5bSAndreas Roeseler 	ext_hdr = skb_header_pointer(skb, 0, sizeof(_ext_hdr), &_ext_hdr);
1073d329ea5bSAndreas Roeseler 	/* Size of iio is class_type dependent.
1074d329ea5bSAndreas Roeseler 	 * Only check header here and assign length based on ctype in the switch statement
1075d329ea5bSAndreas Roeseler 	 */
1076d329ea5bSAndreas Roeseler 	iio = skb_header_pointer(skb, sizeof(_ext_hdr), sizeof(iio->extobj_hdr), &_iio);
1077d329ea5bSAndreas Roeseler 	if (!ext_hdr || !iio)
1078d329ea5bSAndreas Roeseler 		goto send_mal_query;
10791fcd7945SXin Long 	if (ntohs(iio->extobj_hdr.length) <= sizeof(iio->extobj_hdr) ||
10801fcd7945SXin Long 	    ntohs(iio->extobj_hdr.length) > sizeof(_iio))
1081d329ea5bSAndreas Roeseler 		goto send_mal_query;
1082d329ea5bSAndreas Roeseler 	ident_len = ntohs(iio->extobj_hdr.length) - sizeof(iio->extobj_hdr);
10831fcd7945SXin Long 	iio = skb_header_pointer(skb, sizeof(_ext_hdr),
10841fcd7945SXin Long 				 sizeof(iio->extobj_hdr) + ident_len, &_iio);
10851fcd7945SXin Long 	if (!iio)
10861fcd7945SXin Long 		goto send_mal_query;
10871fcd7945SXin Long 
1088d329ea5bSAndreas Roeseler 	status = 0;
1089d329ea5bSAndreas Roeseler 	dev = NULL;
1090d329ea5bSAndreas Roeseler 	switch (iio->extobj_hdr.class_type) {
1091e542d29cSAndreas Roeseler 	case ICMP_EXT_ECHO_CTYPE_NAME:
1092d329ea5bSAndreas Roeseler 		if (ident_len >= IFNAMSIZ)
1093d329ea5bSAndreas Roeseler 			goto send_mal_query;
1094d329ea5bSAndreas Roeseler 		memset(buff, 0, sizeof(buff));
1095d329ea5bSAndreas Roeseler 		memcpy(buff, &iio->ident.name, ident_len);
1096d329ea5bSAndreas Roeseler 		dev = dev_get_by_name(net, buff);
1097d329ea5bSAndreas Roeseler 		break;
1098e542d29cSAndreas Roeseler 	case ICMP_EXT_ECHO_CTYPE_INDEX:
1099d329ea5bSAndreas Roeseler 		if (ident_len != sizeof(iio->ident.ifindex))
1100d329ea5bSAndreas Roeseler 			goto send_mal_query;
1101d329ea5bSAndreas Roeseler 		dev = dev_get_by_index(net, ntohl(iio->ident.ifindex));
1102d329ea5bSAndreas Roeseler 		break;
1103e542d29cSAndreas Roeseler 	case ICMP_EXT_ECHO_CTYPE_ADDR:
11041fcd7945SXin Long 		if (ident_len < sizeof(iio->ident.addr.ctype3_hdr) ||
11051fcd7945SXin Long 		    ident_len != sizeof(iio->ident.addr.ctype3_hdr) +
1106d329ea5bSAndreas Roeseler 				 iio->ident.addr.ctype3_hdr.addrlen)
1107d329ea5bSAndreas Roeseler 			goto send_mal_query;
1108d329ea5bSAndreas Roeseler 		switch (ntohs(iio->ident.addr.ctype3_hdr.afi)) {
1109d329ea5bSAndreas Roeseler 		case ICMP_AFI_IP:
11101fcd7945SXin Long 			if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in_addr))
1111d329ea5bSAndreas Roeseler 				goto send_mal_query;
1112e32ea44cSAndreas Roeseler 			dev = ip_dev_find(net, iio->ident.addr.ip_addr.ipv4_addr);
1113d329ea5bSAndreas Roeseler 			break;
1114d329ea5bSAndreas Roeseler #if IS_ENABLED(CONFIG_IPV6)
1115d329ea5bSAndreas Roeseler 		case ICMP_AFI_IP6:
11161fcd7945SXin Long 			if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in6_addr))
1117d329ea5bSAndreas Roeseler 				goto send_mal_query;
1118d329ea5bSAndreas Roeseler 			dev = ipv6_stub->ipv6_dev_find(net, &iio->ident.addr.ip_addr.ipv6_addr, dev);
1119d329ea5bSAndreas Roeseler 			dev_hold(dev);
1120d329ea5bSAndreas Roeseler 			break;
1121d329ea5bSAndreas Roeseler #endif
1122d329ea5bSAndreas Roeseler 		default:
1123d329ea5bSAndreas Roeseler 			goto send_mal_query;
1124d329ea5bSAndreas Roeseler 		}
1125d329ea5bSAndreas Roeseler 		break;
1126d329ea5bSAndreas Roeseler 	default:
1127d329ea5bSAndreas Roeseler 		goto send_mal_query;
1128d329ea5bSAndreas Roeseler 	}
1129d329ea5bSAndreas Roeseler 	if (!dev) {
11301fd07f33SAndreas Roeseler 		icmphdr->code = ICMP_EXT_CODE_NO_IF;
11311fd07f33SAndreas Roeseler 		return true;
1132d329ea5bSAndreas Roeseler 	}
1133d329ea5bSAndreas Roeseler 	/* Fill bits in reply message */
1134d329ea5bSAndreas Roeseler 	if (dev->flags & IFF_UP)
1135e542d29cSAndreas Roeseler 		status |= ICMP_EXT_ECHOREPLY_ACTIVE;
1136d68dc711SEric Dumazet 
1137d68dc711SEric Dumazet 	in_dev = __in_dev_get_rcu(dev);
1138d68dc711SEric Dumazet 	if (in_dev && rcu_access_pointer(in_dev->ifa_list))
1139e542d29cSAndreas Roeseler 		status |= ICMP_EXT_ECHOREPLY_IPV4;
1140d68dc711SEric Dumazet 
1141d68dc711SEric Dumazet 	in6_dev = __in6_dev_get(dev);
1142d68dc711SEric Dumazet 	if (in6_dev && !list_empty(&in6_dev->addr_list))
1143e542d29cSAndreas Roeseler 		status |= ICMP_EXT_ECHOREPLY_IPV6;
1144d68dc711SEric Dumazet 
1145d329ea5bSAndreas Roeseler 	dev_put(dev);
11461fd07f33SAndreas Roeseler 	icmphdr->un.echo.sequence |= htons(status);
1147d329ea5bSAndreas Roeseler 	return true;
1148d329ea5bSAndreas Roeseler send_mal_query:
11491fd07f33SAndreas Roeseler 	icmphdr->code = ICMP_EXT_CODE_MAL_QUERY;
11501fd07f33SAndreas Roeseler 	return true;
11511da177e4SLinus Torvalds }
11521fd07f33SAndreas Roeseler EXPORT_SYMBOL_GPL(icmp_build_probe);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds /*
11551da177e4SLinus Torvalds  *	Handle ICMP Timestamp requests.
11561da177e4SLinus Torvalds  *	RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
11571da177e4SLinus Torvalds  *		  SHOULD be in the kernel for minimum random latency.
11581da177e4SLinus Torvalds  *		  MUST be accurate to a few minutes.
11591da177e4SLinus Torvalds  *		  MUST be updated at least at 15Hz.
11601da177e4SLinus Torvalds  */
icmp_timestamp(struct sk_buff * skb)1161b384c95aSMenglong Dong static enum skb_drop_reason icmp_timestamp(struct sk_buff *skb)
11621da177e4SLinus Torvalds {
11631da177e4SLinus Torvalds 	struct icmp_bxm icmp_param;
11641da177e4SLinus Torvalds 	/*
11651da177e4SLinus Torvalds 	 *	Too short.
11661da177e4SLinus Torvalds 	 */
11671da177e4SLinus Torvalds 	if (skb->len < 4)
11681da177e4SLinus Torvalds 		goto out_err;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	/*
11711da177e4SLinus Torvalds 	 *	Fill in the current time as ms since midnight UT:
11721da177e4SLinus Torvalds 	 */
1173822c8685SDeepa Dinamani 	icmp_param.data.times[1] = inet_current_timestamp();
11741da177e4SLinus Torvalds 	icmp_param.data.times[2] = icmp_param.data.times[1];
117515285402SGustavo A. R. Silva 
117615285402SGustavo A. R. Silva 	BUG_ON(skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4));
117715285402SGustavo A. R. Silva 
117888c7664fSArnaldo Carvalho de Melo 	icmp_param.data.icmph	   = *icmp_hdr(skb);
11791da177e4SLinus Torvalds 	icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
11801da177e4SLinus Torvalds 	icmp_param.data.icmph.code = 0;
11811da177e4SLinus Torvalds 	icmp_param.skb		   = skb;
11821da177e4SLinus Torvalds 	icmp_param.offset	   = 0;
11831da177e4SLinus Torvalds 	icmp_param.data_len	   = 0;
11841da177e4SLinus Torvalds 	icmp_param.head_len	   = sizeof(struct icmphdr) + 12;
11851da177e4SLinus Torvalds 	icmp_reply(&icmp_param, skb);
1186b384c95aSMenglong Dong 	return SKB_NOT_DROPPED_YET;
1187e3e32170SRick Jones 
11881da177e4SLinus Torvalds out_err:
1189338f6418SEric Dumazet 	__ICMP_INC_STATS(dev_net_rcu(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
1190b384c95aSMenglong Dong 	return SKB_DROP_REASON_PKT_TOO_SMALL;
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
icmp_discard(struct sk_buff * skb)1193b384c95aSMenglong Dong static enum skb_drop_reason icmp_discard(struct sk_buff *skb)
11941da177e4SLinus Torvalds {
1195e3e32170SRick Jones 	/* pretend it was a success */
1196b384c95aSMenglong Dong 	return SKB_NOT_DROPPED_YET;
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds /*
12001da177e4SLinus Torvalds  *	Deal with incoming ICMP packets.
12011da177e4SLinus Torvalds  */
icmp_rcv(struct sk_buff * skb)12021da177e4SLinus Torvalds int icmp_rcv(struct sk_buff *skb)
12031da177e4SLinus Torvalds {
1204b384c95aSMenglong Dong 	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
1205511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
1206338f6418SEric Dumazet 	struct net *net = dev_net_rcu(rt->dst.dev);
1207b384c95aSMenglong Dong 	struct icmphdr *icmph;
12081da177e4SLinus Torvalds 
1209aebcf82cSHerbert Xu 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1210def8b4faSAlexey Dobriyan 		struct sec_path *sp = skb_sec_path(skb);
12118b7817f3SHerbert Xu 		int nh;
12128b7817f3SHerbert Xu 
1213def8b4faSAlexey Dobriyan 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
1214b384c95aSMenglong Dong 				 XFRM_STATE_ICMP)) {
1215b384c95aSMenglong Dong 			reason = SKB_DROP_REASON_XFRM_POLICY;
1216aebcf82cSHerbert Xu 			goto drop;
1217b384c95aSMenglong Dong 		}
1218aebcf82cSHerbert Xu 
12198b7817f3SHerbert Xu 		if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
12208b7817f3SHerbert Xu 			goto drop;
12218b7817f3SHerbert Xu 
12228b7817f3SHerbert Xu 		nh = skb_network_offset(skb);
12238b7817f3SHerbert Xu 		skb_set_network_header(skb, sizeof(*icmph));
12248b7817f3SHerbert Xu 
1225b384c95aSMenglong Dong 		if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN,
1226b384c95aSMenglong Dong 						skb)) {
1227b384c95aSMenglong Dong 			reason = SKB_DROP_REASON_XFRM_POLICY;
12288b7817f3SHerbert Xu 			goto drop;
1229b384c95aSMenglong Dong 		}
12308b7817f3SHerbert Xu 
12318b7817f3SHerbert Xu 		skb_set_network_header(skb, nh);
12328b7817f3SHerbert Xu 	}
12338b7817f3SHerbert Xu 
12345d3848bcSEric Dumazet 	__ICMP_INC_STATS(net, ICMP_MIB_INMSGS);
12351da177e4SLinus Torvalds 
123629a96e1fSTom Herbert 	if (skb_checksum_simple_validate(skb))
12376a5dc9e5SEric Dumazet 		goto csum_error;
12381da177e4SLinus Torvalds 
12398cf22943SHerbert Xu 	if (!pskb_pull(skb, sizeof(*icmph)))
12408cf22943SHerbert Xu 		goto error;
12411da177e4SLinus Torvalds 
124288c7664fSArnaldo Carvalho de Melo 	icmph = icmp_hdr(skb);
12431da177e4SLinus Torvalds 
1244214d3f1fSEric Dumazet 	ICMPMSGIN_INC_STATS(net, icmph->type);
1245d329ea5bSAndreas Roeseler 
1246d329ea5bSAndreas Roeseler 	/* Check for ICMP Extended Echo (PROBE) messages */
1247d329ea5bSAndreas Roeseler 	if (icmph->type == ICMP_EXT_ECHO) {
1248d329ea5bSAndreas Roeseler 		/* We can't use icmp_pointers[].handler() because it is an array of
1249d329ea5bSAndreas Roeseler 		 * size NR_ICMP_TYPES + 1 (19 elements) and PROBE has code 42.
1250d329ea5bSAndreas Roeseler 		 */
1251b384c95aSMenglong Dong 		reason = icmp_echo(skb);
1252b384c95aSMenglong Dong 		goto reason_check;
1253d329ea5bSAndreas Roeseler 	}
1254d329ea5bSAndreas Roeseler 
125531433202SAndreas Roeseler 	if (icmph->type == ICMP_EXT_ECHOREPLY) {
1256b384c95aSMenglong Dong 		reason = ping_rcv(skb);
1257b384c95aSMenglong Dong 		goto reason_check;
125831433202SAndreas Roeseler 	}
125931433202SAndreas Roeseler 
12601da177e4SLinus Torvalds 	/*
12611da177e4SLinus Torvalds 	 *	18 is the highest 'known' ICMP type. Anything else is a mystery
12621da177e4SLinus Torvalds 	 *
12631da177e4SLinus Torvalds 	 *	RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
12641da177e4SLinus Torvalds 	 *		  discarded.
12651da177e4SLinus Torvalds 	 */
1266b384c95aSMenglong Dong 	if (icmph->type > NR_ICMP_TYPES) {
1267b384c95aSMenglong Dong 		reason = SKB_DROP_REASON_UNHANDLED_PROTO;
12681da177e4SLinus Torvalds 		goto error;
1269b384c95aSMenglong Dong 	}
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	/*
12721da177e4SLinus Torvalds 	 *	Parse the ICMP message
12731da177e4SLinus Torvalds 	 */
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
12761da177e4SLinus Torvalds 		/*
12771da177e4SLinus Torvalds 		 *	RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
12781da177e4SLinus Torvalds 		 *	  silently ignored (we let user decide with a sysctl).
12791da177e4SLinus Torvalds 		 *	RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
12801da177e4SLinus Torvalds 		 *	  discarded if to broadcast/multicast.
12811da177e4SLinus Torvalds 		 */
12824c866aa7SAlexey Kuznetsov 		if ((icmph->type == ICMP_ECHO ||
12834c866aa7SAlexey Kuznetsov 		     icmph->type == ICMP_TIMESTAMP) &&
128466484bb9SKuniyuki Iwashima 		    READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_broadcasts)) {
1285b384c95aSMenglong Dong 			reason = SKB_DROP_REASON_INVALID_PROTO;
12861da177e4SLinus Torvalds 			goto error;
12871da177e4SLinus Torvalds 		}
12881da177e4SLinus Torvalds 		if (icmph->type != ICMP_ECHO &&
12891da177e4SLinus Torvalds 		    icmph->type != ICMP_TIMESTAMP &&
12901da177e4SLinus Torvalds 		    icmph->type != ICMP_ADDRESS &&
12911da177e4SLinus Torvalds 		    icmph->type != ICMP_ADDRESSREPLY) {
1292b384c95aSMenglong Dong 			reason = SKB_DROP_REASON_INVALID_PROTO;
12931da177e4SLinus Torvalds 			goto error;
12941da177e4SLinus Torvalds 		}
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 
1297b384c95aSMenglong Dong 	reason = icmp_pointers[icmph->type].handler(skb);
1298b384c95aSMenglong Dong reason_check:
1299b384c95aSMenglong Dong 	if (!reason)  {
1300e3e32170SRick Jones 		consume_skb(skb);
1301f91c58d6SZhang Shengju 		return NET_RX_SUCCESS;
1302e3e32170SRick Jones 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds drop:
1305b384c95aSMenglong Dong 	kfree_skb_reason(skb, reason);
1306f91c58d6SZhang Shengju 	return NET_RX_DROP;
13076a5dc9e5SEric Dumazet csum_error:
1308b384c95aSMenglong Dong 	reason = SKB_DROP_REASON_ICMP_CSUM;
13095d3848bcSEric Dumazet 	__ICMP_INC_STATS(net, ICMP_MIB_CSUMERRORS);
13101da177e4SLinus Torvalds error:
13115d3848bcSEric Dumazet 	__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
13121da177e4SLinus Torvalds 	goto drop;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
ip_icmp_error_rfc4884_validate(const struct sk_buff * skb,int off)1315eba75c58SWillem de Bruijn static bool ip_icmp_error_rfc4884_validate(const struct sk_buff *skb, int off)
1316eba75c58SWillem de Bruijn {
1317eba75c58SWillem de Bruijn 	struct icmp_extobj_hdr *objh, _objh;
1318eba75c58SWillem de Bruijn 	struct icmp_ext_hdr *exth, _exth;
1319eba75c58SWillem de Bruijn 	u16 olen;
1320eba75c58SWillem de Bruijn 
1321eba75c58SWillem de Bruijn 	exth = skb_header_pointer(skb, off, sizeof(_exth), &_exth);
1322eba75c58SWillem de Bruijn 	if (!exth)
1323eba75c58SWillem de Bruijn 		return false;
1324eba75c58SWillem de Bruijn 	if (exth->version != 2)
1325eba75c58SWillem de Bruijn 		return true;
1326eba75c58SWillem de Bruijn 
1327eba75c58SWillem de Bruijn 	if (exth->checksum &&
1328eba75c58SWillem de Bruijn 	    csum_fold(skb_checksum(skb, off, skb->len - off, 0)))
1329eba75c58SWillem de Bruijn 		return false;
1330eba75c58SWillem de Bruijn 
1331eba75c58SWillem de Bruijn 	off += sizeof(_exth);
1332eba75c58SWillem de Bruijn 	while (off < skb->len) {
1333eba75c58SWillem de Bruijn 		objh = skb_header_pointer(skb, off, sizeof(_objh), &_objh);
1334eba75c58SWillem de Bruijn 		if (!objh)
1335eba75c58SWillem de Bruijn 			return false;
1336eba75c58SWillem de Bruijn 
1337eba75c58SWillem de Bruijn 		olen = ntohs(objh->length);
1338eba75c58SWillem de Bruijn 		if (olen < sizeof(_objh))
1339eba75c58SWillem de Bruijn 			return false;
1340eba75c58SWillem de Bruijn 
1341eba75c58SWillem de Bruijn 		off += olen;
1342eba75c58SWillem de Bruijn 		if (off > skb->len)
1343eba75c58SWillem de Bruijn 			return false;
1344eba75c58SWillem de Bruijn 	}
1345eba75c58SWillem de Bruijn 
1346eba75c58SWillem de Bruijn 	return true;
1347eba75c58SWillem de Bruijn }
1348eba75c58SWillem de Bruijn 
ip_icmp_error_rfc4884(const struct sk_buff * skb,struct sock_ee_data_rfc4884 * out,int thlen,int off)1349eba75c58SWillem de Bruijn void ip_icmp_error_rfc4884(const struct sk_buff *skb,
1350178c49d9SWillem de Bruijn 			   struct sock_ee_data_rfc4884 *out,
1351178c49d9SWillem de Bruijn 			   int thlen, int off)
1352eba75c58SWillem de Bruijn {
1353178c49d9SWillem de Bruijn 	int hlen;
1354eba75c58SWillem de Bruijn 
1355c4e9e09fSWillem de Bruijn 	/* original datagram headers: end of icmph to payload (skb->data) */
1356178c49d9SWillem de Bruijn 	hlen = -skb_transport_offset(skb) - thlen;
1357eba75c58SWillem de Bruijn 
1358eba75c58SWillem de Bruijn 	/* per rfc 4884: minimal datagram length of 128 bytes */
1359c4e9e09fSWillem de Bruijn 	if (off < 128 || off < hlen)
1360eba75c58SWillem de Bruijn 		return;
1361eba75c58SWillem de Bruijn 
1362eba75c58SWillem de Bruijn 	/* kernel has stripped headers: return payload offset in bytes */
1363eba75c58SWillem de Bruijn 	off -= hlen;
1364eba75c58SWillem de Bruijn 	if (off + sizeof(struct icmp_ext_hdr) > skb->len)
1365eba75c58SWillem de Bruijn 		return;
1366eba75c58SWillem de Bruijn 
1367eba75c58SWillem de Bruijn 	out->len = off;
1368eba75c58SWillem de Bruijn 
1369eba75c58SWillem de Bruijn 	if (!ip_icmp_error_rfc4884_validate(skb, off))
1370eba75c58SWillem de Bruijn 		out->flags |= SO_EE_RFC4884_FLAG_INVALID;
1371eba75c58SWillem de Bruijn }
137201370434SWillem de Bruijn EXPORT_SYMBOL_GPL(ip_icmp_error_rfc4884);
1373eba75c58SWillem de Bruijn 
icmp_err(struct sk_buff * skb,u32 info)137432bbd879SStefano Brivio int icmp_err(struct sk_buff *skb, u32 info)
13755b052042SLi Wei {
13765b052042SLi Wei 	struct iphdr *iph = (struct iphdr *)skb->data;
13776d0bfe22SLorenzo Colitti 	int offset = iph->ihl<<2;
13786d0bfe22SLorenzo Colitti 	struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
1379338f6418SEric Dumazet 	struct net *net = dev_net_rcu(skb->dev);
13805b052042SLi Wei 	int type = icmp_hdr(skb)->type;
13815b052042SLi Wei 	int code = icmp_hdr(skb)->code;
13825b052042SLi Wei 
13835b052042SLi Wei 	/*
13845b052042SLi Wei 	 * Use ping_err to handle all icmp errors except those
13855b052042SLi Wei 	 * triggered by ICMP_ECHOREPLY which sent from kernel.
13865b052042SLi Wei 	 */
13875b052042SLi Wei 	if (icmph->type != ICMP_ECHOREPLY) {
13886d0bfe22SLorenzo Colitti 		ping_err(skb, offset, info);
138932bbd879SStefano Brivio 		return 0;
13905b052042SLi Wei 	}
13915b052042SLi Wei 
13925b052042SLi Wei 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
1393d888f396SMaciej Żenczykowski 		ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ICMP);
13945b052042SLi Wei 	else if (type == ICMP_REDIRECT)
13951042caa7SMaciej Żenczykowski 		ipv4_redirect(skb, net, 0, IPPROTO_ICMP);
139632bbd879SStefano Brivio 
139732bbd879SStefano Brivio 	return 0;
13985b052042SLi Wei }
13995b052042SLi Wei 
14001da177e4SLinus Torvalds /*
14011da177e4SLinus Torvalds  *	This table is the definition of how we handle ICMP.
14021da177e4SLinus Torvalds  */
14039b5b5cffSArjan van de Ven static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
14041da177e4SLinus Torvalds 	[ICMP_ECHOREPLY] = {
1405c319b4d7SVasiliy Kulikov 		.handler = ping_rcv,
14061da177e4SLinus Torvalds 	},
14071da177e4SLinus Torvalds 	[1] = {
14081da177e4SLinus Torvalds 		.handler = icmp_discard,
14091da177e4SLinus Torvalds 		.error = 1,
14101da177e4SLinus Torvalds 	},
14111da177e4SLinus Torvalds 	[2] = {
14121da177e4SLinus Torvalds 		.handler = icmp_discard,
14131da177e4SLinus Torvalds 		.error = 1,
14141da177e4SLinus Torvalds 	},
14151da177e4SLinus Torvalds 	[ICMP_DEST_UNREACH] = {
14161da177e4SLinus Torvalds 		.handler = icmp_unreach,
14171da177e4SLinus Torvalds 		.error = 1,
14181da177e4SLinus Torvalds 	},
14191da177e4SLinus Torvalds 	[ICMP_SOURCE_QUENCH] = {
14201da177e4SLinus Torvalds 		.handler = icmp_unreach,
14211da177e4SLinus Torvalds 		.error = 1,
14221da177e4SLinus Torvalds 	},
14231da177e4SLinus Torvalds 	[ICMP_REDIRECT] = {
14241da177e4SLinus Torvalds 		.handler = icmp_redirect,
14251da177e4SLinus Torvalds 		.error = 1,
14261da177e4SLinus Torvalds 	},
14271da177e4SLinus Torvalds 	[6] = {
14281da177e4SLinus Torvalds 		.handler = icmp_discard,
14291da177e4SLinus Torvalds 		.error = 1,
14301da177e4SLinus Torvalds 	},
14311da177e4SLinus Torvalds 	[7] = {
14321da177e4SLinus Torvalds 		.handler = icmp_discard,
14331da177e4SLinus Torvalds 		.error = 1,
14341da177e4SLinus Torvalds 	},
14351da177e4SLinus Torvalds 	[ICMP_ECHO] = {
14361da177e4SLinus Torvalds 		.handler = icmp_echo,
14371da177e4SLinus Torvalds 	},
14381da177e4SLinus Torvalds 	[9] = {
14391da177e4SLinus Torvalds 		.handler = icmp_discard,
14401da177e4SLinus Torvalds 		.error = 1,
14411da177e4SLinus Torvalds 	},
14421da177e4SLinus Torvalds 	[10] = {
14431da177e4SLinus Torvalds 		.handler = icmp_discard,
14441da177e4SLinus Torvalds 		.error = 1,
14451da177e4SLinus Torvalds 	},
14461da177e4SLinus Torvalds 	[ICMP_TIME_EXCEEDED] = {
14471da177e4SLinus Torvalds 		.handler = icmp_unreach,
14481da177e4SLinus Torvalds 		.error = 1,
14491da177e4SLinus Torvalds 	},
14501da177e4SLinus Torvalds 	[ICMP_PARAMETERPROB] = {
14511da177e4SLinus Torvalds 		.handler = icmp_unreach,
14521da177e4SLinus Torvalds 		.error = 1,
14531da177e4SLinus Torvalds 	},
14541da177e4SLinus Torvalds 	[ICMP_TIMESTAMP] = {
14551da177e4SLinus Torvalds 		.handler = icmp_timestamp,
14561da177e4SLinus Torvalds 	},
14571da177e4SLinus Torvalds 	[ICMP_TIMESTAMPREPLY] = {
14581da177e4SLinus Torvalds 		.handler = icmp_discard,
14591da177e4SLinus Torvalds 	},
14601da177e4SLinus Torvalds 	[ICMP_INFO_REQUEST] = {
14611da177e4SLinus Torvalds 		.handler = icmp_discard,
14621da177e4SLinus Torvalds 	},
14631da177e4SLinus Torvalds 	[ICMP_INFO_REPLY] = {
14641da177e4SLinus Torvalds 		.handler = icmp_discard,
14651da177e4SLinus Torvalds 	},
14661da177e4SLinus Torvalds 	[ICMP_ADDRESS] = {
1467838942a5SDavid S. Miller 		.handler = icmp_discard,
14681da177e4SLinus Torvalds 	},
14691da177e4SLinus Torvalds 	[ICMP_ADDRESSREPLY] = {
1470838942a5SDavid S. Miller 		.handler = icmp_discard,
14711da177e4SLinus Torvalds 	},
14721da177e4SLinus Torvalds };
14731da177e4SLinus Torvalds 
icmp_sk_init(struct net * net)1474263173afSAdrian Bunk static int __net_init icmp_sk_init(struct net *net)
1475a5710d65SDenis V. Lunev {
1476a24022e1SPavel Emelyanov 	/* Control parameters for ECHO replies. */
1477a24022e1SPavel Emelyanov 	net->ipv4.sysctl_icmp_echo_ignore_all = 0;
1478d329ea5bSAndreas Roeseler 	net->ipv4.sysctl_icmp_echo_enable_probe = 0;
1479a24022e1SPavel Emelyanov 	net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
1480a24022e1SPavel Emelyanov 
1481a24022e1SPavel Emelyanov 	/* Control parameter - ignore bogus broadcast responses? */
1482a24022e1SPavel Emelyanov 	net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
1483a24022e1SPavel Emelyanov 
1484a24022e1SPavel Emelyanov 	/*
1485a24022e1SPavel Emelyanov 	 * 	Configurable global rate limit.
1486a24022e1SPavel Emelyanov 	 *
1487a24022e1SPavel Emelyanov 	 *	ratelimit defines tokens/packet consumed for dst->rate_token
1488a24022e1SPavel Emelyanov 	 *	bucket ratemask defines which icmp types are ratelimited by
1489a24022e1SPavel Emelyanov 	 *	setting	it's bit position.
1490a24022e1SPavel Emelyanov 	 *
1491a24022e1SPavel Emelyanov 	 *	default:
1492a24022e1SPavel Emelyanov 	 *	dest unreachable (3), source quench (4),
1493a24022e1SPavel Emelyanov 	 *	time exceeded (11), parameter problem (12)
1494a24022e1SPavel Emelyanov 	 */
1495a24022e1SPavel Emelyanov 
1496a24022e1SPavel Emelyanov 	net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
1497a24022e1SPavel Emelyanov 	net->ipv4.sysctl_icmp_ratemask = 0x1818;
1498a24022e1SPavel Emelyanov 	net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
1499a24022e1SPavel Emelyanov 
1500a5710d65SDenis V. Lunev 	return 0;
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
15034a6ad7a1SDenis V. Lunev static struct pernet_operations __net_initdata icmp_sk_ops = {
15044a6ad7a1SDenis V. Lunev        .init = icmp_sk_init,
15054a6ad7a1SDenis V. Lunev };
15064a6ad7a1SDenis V. Lunev 
icmp_init(void)15074a6ad7a1SDenis V. Lunev int __init icmp_init(void)
15084a6ad7a1SDenis V. Lunev {
1509a15c89c7SEric Dumazet 	int err, i;
1510a15c89c7SEric Dumazet 
1511a15c89c7SEric Dumazet 	for_each_possible_cpu(i) {
1512a15c89c7SEric Dumazet 		struct sock *sk;
1513a15c89c7SEric Dumazet 
1514a15c89c7SEric Dumazet 		err = inet_ctl_sock_create(&sk, PF_INET,
1515a15c89c7SEric Dumazet 					   SOCK_RAW, IPPROTO_ICMP, &init_net);
1516a15c89c7SEric Dumazet 		if (err < 0)
1517a15c89c7SEric Dumazet 			return err;
1518a15c89c7SEric Dumazet 
1519a15c89c7SEric Dumazet 		per_cpu(ipv4_icmp_sk, i) = sk;
1520a15c89c7SEric Dumazet 
1521a15c89c7SEric Dumazet 		/* Enough space for 2 64K ICMP packets, including
1522a15c89c7SEric Dumazet 		 * sk_buff/skb_shared_info struct overhead.
1523a15c89c7SEric Dumazet 		 */
1524a15c89c7SEric Dumazet 		sk->sk_sndbuf =	2 * SKB_TRUESIZE(64 * 1024);
1525a15c89c7SEric Dumazet 
1526a15c89c7SEric Dumazet 		/*
1527a15c89c7SEric Dumazet 		 * Speedup sock_wfree()
1528a15c89c7SEric Dumazet 		 */
1529a15c89c7SEric Dumazet 		sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1530a15c89c7SEric Dumazet 		inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
1531a15c89c7SEric Dumazet 	}
1532959d2726SEric W. Biederman 	return register_pernet_subsys(&icmp_sk_ops);
15334a6ad7a1SDenis V. Lunev }
1534