Skip to contentSkip to conversation

net: icmp: add skb drop reasons to icmp

PATCH In review [email protected] 2 participants 6 messages Mar 14, 2022 – Mar 16, 2022

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover net: icmp: add skb drop reasons to icmp

From: Menglong Dong <[email protected]>

In the commit c504e5c2f964 ("net: skb: introduce kfree_skb_reason()"), we added the support of reporting the reasons of skb drops to kfree_skb tracepoint. And in this series patches, reasons for skb drops are added to ICMP protocol.

In order to report the reasons of skb drops in 'sock_queue_rcv_skb()', the function 'sock_queue_rcv_skb_reason()' is introduced in the 1th patch, which is used in the 2th patch.

In the 2th patch, we add skb drop reasons to ping_queue_rcv_skb().

In the 3th patch, we make ICMP message handler functions return drop reasons, which means we change the return type of 'handler()' in 'struct icmp_control' from 'bool' to 'enum skb_drop_reason'. This changed its original intention, as 'false' means failure, but 'SKB_NOT_DROPPED_YET', which is 0, means success now. Therefore, we have to change all usages of these handler. Following "handler" functions are involved:

icmp_unreach()
icmp_redirect()
icmp_echo()
icmp_timestamp()
icmp_discard()

And following drop reasons are added(what they mean can be see in the document for them):

SKB_DROP_REASON_ICMP_CSUM
SKB_DROP_REASON_ICMP_TYPE
SKB_DROP_REASON_ICMP_BROADCAST

Menglong Dong (3):
net: sock: introduce sock_queue_rcv_skb_reason()
net: icmp: add skb drop reasons to ping_queue_rcv_skb()
net: icmp: add reasons of the skb drops to icmp protocol

 include/linux/skbuff.h     |  5 +++
 include/net/ping.h         |  2 +-
 include/net/sock.h         |  9 ++++-
 include/trace/events/skb.h |  3 ++
 net/core/sock.c            | 30 ++++++++++++---
 net/ipv4/icmp.c            | 75 ++++++++++++++++++++++----------------
 net/ipv4/ping.c            | 21 ++++++-----
 net/ipv6/icmp.c            | 24 +++++++-----
 8 files changed, 112 insertions(+), 57 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/3 net: sock: introduce sock_queue_rcv_skb_reason()

From: Menglong Dong <[email protected]>

In order to report the reasons of skb drops in 'sock_queue_rcv_skb()', introduce the function 'sock_queue_rcv_skb_reason()'.

As the return value of 'sock_queue_rcv_skb()' is used as the error code, we can't make it as drop reason and have to pass extra output argument. 'sock_queue_rcv_skb()' is used in many places, so we can't change it directly.

Introduce the new function 'sock_queue_rcv_skb_reason()' and make 'sock_queue_rcv_skb()' an inline call to it.

---
 include/net/sock.h |  9 ++++++++-
 net/core/sock.c    | 30 ++++++++++++++++++++++++------
 2 files changed, 32 insertions(+), 7 deletions(-)
include/net/sock.h +8 -1
index c4b91fc19b9c..1a988e605f09 100644
@@ -2392,7 +2392,14 @@ int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,
 			void (*destructor)(struct sock *sk,
 					   struct sk_buff *skb));
 int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
-int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
+
+int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb,
+			      enum skb_drop_reason *reason);
+
+static inline int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
+{
+	return sock_queue_rcv_skb_reason(sk, skb, NULL);
+}
 
 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb);
 struct sk_buff *sock_dequeue_err_skb(struct sock *sk);
net/core/sock.c +24 -6
index 1180a0cb0110..2cae991f817e 100644
@@ -503,17 +503,35 @@ int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
 }
 EXPORT_SYMBOL(__sock_queue_rcv_skb);
 
-int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
+int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb,
+			      enum skb_drop_reason *reason)
 {
+	enum skb_drop_reason drop_reason;
 	int err;
 
 	err = sk_filter(sk, skb);
-	if (err)
-		return err;
-
-	return __sock_queue_rcv_skb(sk, skb);
+	if (err) {
+		drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
+		goto out;
+	}
+	err = __sock_queue_rcv_skb(sk, skb);
+	switch (err) {
+	case -ENOMEM:
+		drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
+		break;
+	case -ENOBUFS:
+		drop_reason = SKB_DROP_REASON_PROTO_MEM;
+		break;
+	default:
+		drop_reason = SKB_NOT_DROPPED_YET;
+		break;
+	}
+out:
+	if (reason)
+		*reason = drop_reason;
+	return err;
 }
-EXPORT_SYMBOL(sock_queue_rcv_skb);
+EXPORT_SYMBOL(sock_queue_rcv_skb_reason);
 
 int __sk_receive_skb(struct sock *sk, struct sk_buff *skb,
 		     const int nested, unsigned int trim_cap, bool refcounted)
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/3 net: icmp: add skb drop reasons to ping_queue_rcv_skb()

In reply to [email protected]

From: Menglong Dong <[email protected]>

In order to get the reasons of skb drops, replace sock_queue_rcv_skb() used in ping_queue_rcv_skb() with sock_queue_rcv_skb_reason(). Meanwhile, use kfree_skb_reason() instead of kfree_skb().

As we can see in ping_rcv(), 'skb' will be freed if '-1' is returned by ping_queue_rcv_skb(). In order to get the drop reason of 'skb', make ping_queue_rcv_skb() return the drop reason.

As ping_queue_rcv_skb() is used as 'ping_prot.backlog_rcv()', we can't change its return type. (Seems ping_prot.backlog_rcv() is not used?) Therefore, make it return 'drop_reason * -1' to keep the origin logic.

---
 net/ipv4/ping.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/ipv4/ping.c +4 -3
index 3ee947557b88..cd4eb211431a 100644
@@ -936,12 +936,13 @@ EXPORT_SYMBOL_GPL(ping_recvmsg);
 
 int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
 {
+	enum skb_drop_reason reason;
 	pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
 		 inet_sk(sk), inet_sk(sk)->inet_num, skb);
-	if (sock_queue_rcv_skb(sk, skb) < 0) {
-		kfree_skb(skb);
+	if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) {
+		kfree_skb_reason(skb, reason);
 		pr_debug("ping_queue_rcv_skb -> failed\n");
-		return -1;
+		return -reason;
 	}
 	return 0;
 }
Signature · 1 line
2.35.1
maintainer
#3 lore raw

Re: [PATCH net-next 2/3] net: icmp: add skb drop reasons to ping_queue_rcv_skb()

Hello,

Quoted 24 lines · On Mon, 2022-03-14 at 19:32 +0800, [email protected] wrote:

From: Menglong Dong <[email protected]>

In order to get the reasons of skb drops, replace sock_queue_rcv_skb() used in ping_queue_rcv_skb() with sock_queue_rcv_skb_reason(). Meanwhile, use kfree_skb_reason() instead of kfree_skb().

As we can see in ping_rcv(), 'skb' will be freed if '-1' is returned by ping_queue_rcv_skb(). In order to get the drop reason of 'skb', make ping_queue_rcv_skb() return the drop reason.

As ping_queue_rcv_skb() is used as 'ping_prot.backlog_rcv()', we can't change its return type. (Seems ping_prot.backlog_rcv() is not used?) Therefore, make it return 'drop_reason * -1' to keep the origin logic.

---
 net/ipv4/ping.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/ipv4/ping.c +0 -0
index 3ee947557b88..cd4eb211431a 100644

net/ipv4/ping.c EXPORT_SYMBOL_GPL(ping_recvmsg); · line 939

Quoted 4 lines
net/ipv4/ping.c +1 -0
@@ -936,12 +936,13 @@ EXPORT_SYMBOL_GPL(ping_recvmsg);
 
 int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
 {
+	enum skb_drop_reason reason;

Please insert an empty line between variable declaration and code.

net/ipv4/ping.c EXPORT_SYMBOL_GPL(ping_recvmsg); · line 945

Quoted 9 lines
net/ipv4/ping.c +3 -3
 	pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
 		 inet_sk(sk), inet_sk(sk)->inet_num, skb);
-	if (sock_queue_rcv_skb(sk, skb) < 0) {
-		kfree_skb(skb);
+	if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) {
+		kfree_skb_reason(skb, reason);
 		pr_debug("ping_queue_rcv_skb -> failed\n");
-		return -1;
+		return -reason;

This changes the return value for the release callback. Such callback has a long and non trivial call chain via sk_backlog_rcv.

It *should* be safe, but have you considered factoring out an __ping_queue_rcv_skb() variant returning the full drop reason, use it in the next patch and build ping_queue_rcv_skb() on top of such helper to that backlog_rcv() return code will not change?

The above should additionally avoid the IMHO not so nice:

	reason = ping_queue_rcv_skb(sk, skb2) * -1;

in the next patch - it will become:

	reason = __ping_queue_rcv_skb(sk, skb2);

Thanks!

Paolo

#4 lore raw

Re: [PATCH net-next 2/3] net: icmp: add skb drop reasons to ping_queue_rcv_skb()

Quoted 29 lines · Paolo Abeni

Hello,

Quoted 25 lines · On Mon, 2022-03-14 at 19:32 +0800, [email protected] wrote:

From: Menglong Dong <[email protected]>

In order to get the reasons of skb drops, replace sock_queue_rcv_skb() used in ping_queue_rcv_skb() with sock_queue_rcv_skb_reason(). Meanwhile, use kfree_skb_reason() instead of kfree_skb().

As we can see in ping_rcv(), 'skb' will be freed if '-1' is returned by ping_queue_rcv_skb(). In order to get the drop reason of 'skb', make ping_queue_rcv_skb() return the drop reason.

As ping_queue_rcv_skb() is used as 'ping_prot.backlog_rcv()', we can't change its return type. (Seems ping_prot.backlog_rcv() is not used?) Therefore, make it return 'drop_reason * -1' to keep the origin logic.

---
 net/ipv4/ping.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/ipv4/ping.c +0 -0
index 3ee947557b88..cd4eb211431a 100644
@@ -936,12 +936,13 @@ EXPORT_SYMBOL_GPL(ping_recvmsg);
 
Quoted 5 lines

net/ipv4/ping.c EXPORT_SYMBOL_GPL(ping_recvmsg); · line 939

Quoted 3 lines
net/ipv4/ping.c +1 -0
 int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
 {
+     enum skb_drop_reason reason;

Please insert an empty line between variable declaration and code.

Ok

Quoted 19 lines

net/ipv4/ping.c EXPORT_SYMBOL_GPL(ping_recvmsg); · line 945

Quoted 10 lines
net/ipv4/ping.c +3 -3
      pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
               inet_sk(sk), inet_sk(sk)->inet_num, skb);
-     if (sock_queue_rcv_skb(sk, skb) < 0) {
-             kfree_skb(skb);
+     if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) {
+             kfree_skb_reason(skb, reason);
              pr_debug("ping_queue_rcv_skb -> failed\n");
-             return -1;
+             return -reason;

This changes the return value for the release callback. Such callback has a long and non trivial call chain via sk_backlog_rcv.

It *should* be safe, but have you considered factoring out an __ping_queue_rcv_skb() variant returning the full drop reason, use it in the next patch and build ping_queue_rcv_skb() on top of such helper to that backlog_rcv() return code will not change?

Quoted 8 lines

The above should additionally avoid the IMHO not so nice:

        reason = ping_queue_rcv_skb(sk, skb2) * -1;

in the next patch - it will become:

        reason = __ping_queue_rcv_skb(sk, skb2);

Good idea, thanks!

Menglong Dong

#5 lore raw

PATCH 3/3 net: icmp: add reasons of the skb drops to icmp protocol

In reply to [email protected]

From: Menglong Dong <[email protected]>

Replace kfree_skb() used in icmp_rcv() and icmpv6_rcv() with kfree_skb_reason().

In order to get the reasons of the skb drops after icmp message handle, we change the return type of 'handler()' in 'struct icmp_control' from 'bool' to 'enum skb_drop_reason'. This may change its original intention, as 'false' means failure, but 'SKB_NOT_DROPPED_YET' means success now. Following functions are involved:

icmp_unreach()
icmp_redirect()
icmp_echo()
icmp_timestamp()
icmp_discard()

And following new drop reasons are added:

SKB_DROP_REASON_ICMP_CSUM
SKB_DROP_REASON_ICMP_TYPE
SKB_DROP_REASON_ICMP_BROADCAST

---
 include/linux/skbuff.h     |  5 +++
 include/net/ping.h         |  2 +-
 include/trace/events/skb.h |  3 ++
 net/ipv4/icmp.c            | 75 ++++++++++++++++++++++----------------
 net/ipv4/ping.c            | 14 ++++---
 net/ipv6/icmp.c            | 24 +++++++-----
 6 files changed, 76 insertions(+), 47 deletions(-)
include/linux/skbuff.h +5 -0
index 26538ceb4b01..18c678b340d3 100644
@@ -444,6 +444,11 @@ enum skb_drop_reason {
 	SKB_DROP_REASON_TAP_TXFILTER,	/* dropped by tx filter implemented
 					 * at tun/tap, e.g., check_filter()
 					 */
+	SKB_DROP_REASON_ICMP_CSUM,	/* ICMP checksum error */
+	SKB_DROP_REASON_ICMP_TYPE,	/* unknown ICMP type */
+	SKB_DROP_REASON_ICMP_BROADCAST,	/* unacceptable broadcast(multicast)
+					 * ICMP message
+					 */
 	SKB_DROP_REASON_MAX,
 };
 
include/net/ping.h +1 -1
index 2fe78874318c..b68fbfdb606f 100644
@@ -76,7 +76,7 @@ int  ping_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
 int  ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
 			 void *user_icmph, size_t icmph_len);
 int  ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
-bool ping_rcv(struct sk_buff *skb);
+enum skb_drop_reason ping_rcv(struct sk_buff *skb);
 
 #ifdef CONFIG_PROC_FS
 void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family);
include/trace/events/skb.h +3 -0
index e1670e1e4934..70d0dac8e08b 100644
@@ -61,6 +61,9 @@
 	EM(SKB_DROP_REASON_HDR_TRUNC, HDR_TRUNC)		\
 	EM(SKB_DROP_REASON_TAP_FILTER, TAP_FILTER)		\
 	EM(SKB_DROP_REASON_TAP_TXFILTER, TAP_TXFILTER)		\
+	EM(SKB_DROP_REASON_ICMP_CSUM, ICMP_CSUM)		\
+	EM(SKB_DROP_REASON_ICMP_TYPE, ICMP_TYPE)		\
+	EM(SKB_DROP_REASON_ICMP_BROADCAST, ICMP_BROADCAST)	\
 	EMe(SKB_DROP_REASON_MAX, MAX)
 
 #undef EM
net/ipv4/icmp.c +44 -31

219 lines not on this page · raw message

net/ipv4/ping.c +8 -6
index cd4eb211431a..4442de0093f6 100644
@@ -953,12 +953,12 @@ EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
  *	All we need to do is get the socket.
  */
 
-bool ping_rcv(struct sk_buff *skb)
+enum skb_drop_reason ping_rcv(struct sk_buff *skb)
 {
+	enum skb_drop_reason reason = SKB_DROP_REASON_NO_SOCKET;
 	struct sock *sk;
 	struct net *net = dev_net(skb->dev);
 	struct icmphdr *icmph = icmp_hdr(skb);
-	bool rc = false;
 
 	/* We assume the packet has already been checked by icmp_rcv */
 
@@ -973,15 +973,17 @@ bool ping_rcv(struct sk_buff *skb)
 		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
 
 		pr_debug("rcv on socket %p\n", sk);
-		if (skb2 && !ping_queue_rcv_skb(sk, skb2))
-			rc = true;
+		if (skb2)
+			reason = ping_queue_rcv_skb(sk, skb2) * -1;
+		else
+			reason = SKB_DROP_REASON_NOMEM;
 		sock_put(sk);
 	}
 
-	if (!rc)
+	if (reason)
 		pr_debug("no socket, dropping\n");
 
-	return rc;
+	return reason;
 }
 EXPORT_SYMBOL_GPL(ping_rcv);
 
net/ipv6/icmp.c +15 -9
index e6b978ea0e87..01c8003c9fc9 100644
@@ -864,21 +864,23 @@ void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
 
 static int icmpv6_rcv(struct sk_buff *skb)
 {
+	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
 	struct net *net = dev_net(skb->dev);
 	struct net_device *dev = icmp6_dev(skb);
 	struct inet6_dev *idev = __in6_dev_get(dev);
 	const struct in6_addr *saddr, *daddr;
 	struct icmp6hdr *hdr;
 	u8 type;
-	bool success = false;
 
 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
 		struct sec_path *sp = skb_sec_path(skb);
 		int nh;
 
 		if (!(sp && sp->xvec[sp->len - 1]->props.flags &
-				 XFRM_STATE_ICMP))
+				 XFRM_STATE_ICMP)) {
+			reason = SKB_DROP_REASON_XFRM_POLICY;
 			goto drop_no_count;
+		}
 
 		if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
 			goto drop_no_count;
@@ -886,8 +888,11 @@ static int icmpv6_rcv(struct sk_buff *skb)
 		nh = skb_network_offset(skb);
 		skb_set_network_header(skb, sizeof(*hdr));
 
-		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
+		if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN,
+						skb)) {
+			reason = SKB_DROP_REASON_XFRM_POLICY;
 			goto drop_no_count;
+		}
 
 		skb_set_network_header(skb, nh);
 	}
@@ -924,11 +929,11 @@ static int icmpv6_rcv(struct sk_buff *skb)
 		break;
 
 	case ICMPV6_ECHO_REPLY:
-		success = ping_rcv(skb);
+		reason = ping_rcv(skb);
 		break;
 
 	case ICMPV6_EXT_ECHO_REPLY:
-		success = ping_rcv(skb);
+		reason = ping_rcv(skb);
 		break;
 
 	case ICMPV6_PKT_TOOBIG:
@@ -994,19 +999,20 @@ static int icmpv6_rcv(struct sk_buff *skb)
 	/* until the v6 path can be better sorted assume failure and
 	 * preserve the status quo behaviour for the rest of the paths to here
 	 */
-	if (success)
-		consume_skb(skb);
+	if (reason)
+		kfree_skb_reason(skb, reason);
 	else
-		kfree_skb(skb);
+		consume_skb(skb);
 
 	return 0;
 
 csum_error:
+	reason = SKB_DROP_REASON_ICMP_CSUM;
 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS);
 discard_it:
 	__ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INERRORS);
 drop_no_count:
-	kfree_skb(skb);
+	kfree_skb_reason(skb, reason);
 	return 0;
 }
 
Signature · 1 line
2.35.1