Skip to contentSkip to conversation

net: psample: Fix the netlink skb length

PATCH In review Chris Mi 3 participants 5 messages Feb 3, 2021 – Feb 4, 2021

Subsystems (3)
lore
Share & tools
lore

Chris Mi

author
#0 lore raw

PATCH 1/1 net: psample: Fix the netlink skb length

Currently, the netlink skb length only includes metadata and data length. It doesn't include the psample generic netlink header length. Fix it by adding it.

Fixes 6ae0a6286171 ("net: Introduce psample, a new genetlink channel for packet sampling")
CC Yotam Gigi <[email protected]>
---
 net/psample/psample.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
net/psample/psample.c +6 -4
index 33e238c965bd..807d75f5a40f 100644
@@ -363,6 +363,7 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
 	struct ip_tunnel_info *tun_info;
 #endif
 	struct sk_buff *nl_skb;
+	int header_len;
 	int data_len;
 	int meta_len;
 	void *data;
@@ -381,12 +382,13 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
 		meta_len += psample_tunnel_meta_len(tun_info);
 #endif
 
+	/* psample generic netlink header size */
+	header_len = nlmsg_total_size(GENL_HDRLEN + psample_nl_family.hdrsize);
 	data_len = min(skb->len, trunc_size);
-	if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
-		data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
+	if (header_len + meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
+		data_len = PSAMPLE_MAX_PACKET_SIZE - header_len - meta_len - NLA_HDRLEN
 			    - NLA_ALIGNTO;
-
-	nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
+	nl_skb = genlmsg_new(header_len + meta_len + nla_total_size(data_len), GFP_ATOMIC);
 	if (unlikely(!nl_skb))
 		return;
 
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 2 lines · On Wed, 3 Feb 2021 11:10:28 +0800 Chris Mi wrote:

Currently, the netlink skb length only includes metadata and data length. It doesn't include the psample generic netlink header length.

But what's the bug? Did you see oversized messages on the socket? Did one of the nla_put() fail?

Quoted 21 lines
Fixes 6ae0a6286171 ("net: Introduce psample, a new genetlink channel for packet sampling")
CC Yotam Gigi <[email protected]>
---
 net/psample/psample.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
net/psample/psample.c +1 -0
index 33e238c965bd..807d75f5a40f 100644
@@ -363,6 +363,7 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
 	struct ip_tunnel_info *tun_info;
 #endif
 	struct sk_buff *nl_skb;
+	int header_len;
 	int data_len;
 	int meta_len;
 	void *data;

net/psample/psample.c void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · line 386

Quoted 5 lines
net/psample/psample.c +2 -0
@@ -381,12 +382,13 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
 		meta_len += psample_tunnel_meta_len(tun_info);
 #endif
 
+	/* psample generic netlink header size */
+	header_len = nlmsg_total_size(GENL_HDRLEN + psample_nl_family.hdrsize);

GENL_HDRLEN is already included by genlmsg_new() and fam->hdrsize is 0 / uninitialized for psample_nl_family. What am I missing? Ido?

Quoted 12 lines
net/psample/psample.c +3 -4
 	data_len = min(skb->len, trunc_size);
-	if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
-		data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
+	if (header_len + meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
+		data_len = PSAMPLE_MAX_PACKET_SIZE - header_len - meta_len - NLA_HDRLEN
 			    - NLA_ALIGNTO;
-
-	nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
+	nl_skb = genlmsg_new(header_len + meta_len + nla_total_size(data_len), GFP_ATOMIC);
 	if (unlikely(!nl_skb))
 		return;
 
#2 lore raw
Quoted 6 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 3 Feb 2021 11:10:28 +0800 Chris Mi wrote:

Currently, the netlink skb length only includes metadata and data length. It doesn't include the psample generic netlink header length.

But what's the bug? Did you see oversized messages on the socket? Did one of the nla_put() fail?

I didn't ask, but I assumed the problem was nla_put(). Agree it needs to be noted in the commit message.

Quoted 25 lines
Quoted 25 lines
Fixes 6ae0a6286171 ("net: Introduce psample, a new genetlink channel for packet sampling")
CC Yotam Gigi <[email protected]>
---
 net/psample/psample.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
net/psample/psample.c +1 -0
index 33e238c965bd..807d75f5a40f 100644
@@ -363,6 +363,7 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
 	struct ip_tunnel_info *tun_info;
 #endif
 	struct sk_buff *nl_skb;
+	int header_len;
 	int data_len;
 	int meta_len;
 	void *data;
@@ -381,12 +382,13 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
 		meta_len += psample_tunnel_meta_len(tun_info);
 #endif
 
Quoted 5 lines

net/psample/psample.c void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · line 386

Quoted 2 lines
net/psample/psample.c +2 -0
+	/* psample generic netlink header size */
+	header_len = nlmsg_total_size(GENL_HDRLEN + psample_nl_family.hdrsize);

GENL_HDRLEN is already included by genlmsg_new() and fam->hdrsize is 0 / uninitialized for psample_nl_family. What am I missing? Ido?

Yea, I missed that genlmsg_new() eventually accounts for 'GENL_HDRLEN'.

Chris, assuming the problem is nla_put(), I think some other attribute is not accounted for when calculating the size of the skb. Does it only happen with packets that include tunnel metadata? Because I think I see a few problems there:

net/psample/psample.c +3 -1
index 33e238c965bd..1a233cd128c7 100644
@@ -311,8 +311,10 @@ static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info)
        int tun_opts_len = tun_info->options_len;
        int sum = 0;
 
+       sum += nla_total_size(0);       /* PSAMPLE_ATTR_TUNNEL */
+
        if (tun_key->tun_flags & TUNNEL_KEY)
-               sum += nla_total_size(sizeof(u64));
+               sum += nla_total_size_64bit(sizeof(u64));
 
        if (tun_info->mode & IP_TUNNEL_INFO_BRIDGE)
                sum += nla_total_size(0);
Quoted 14 lines
Quoted 13 lines
net/psample/psample.c +3 -4
 	data_len = min(skb->len, trunc_size);
-	if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
-		data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
+	if (header_len + meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
+		data_len = PSAMPLE_MAX_PACKET_SIZE - header_len - meta_len - NLA_HDRLEN
 			    - NLA_ALIGNTO;
-
-	nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
+	nl_skb = genlmsg_new(header_len + meta_len + nla_total_size(data_len), GFP_ATOMIC);
 	if (unlikely(!nl_skb))
 		return;
 
author
#3 lore raw
Quoted 37 lines · Ido Schimmel
Quoted 5 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 3 Feb 2021 11:10:28 +0800 Chris Mi wrote:

Currently, the netlink skb length only includes metadata and data length. It doesn't include the psample generic netlink header length.

But what's the bug? Did you see oversized messages on the socket? Did one of the nla_put() fail?

I didn't ask, but I assumed the problem was nla_put(). Agree it needs to be noted in the commit message.

Quoted 24 lines
Quoted 24 lines
Fixes 6ae0a6286171 ("net: Introduce psample, a new genetlink channel for packet sampling")
CC Yotam Gigi <[email protected]>
---
  net/psample/psample.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
net/psample/psample.c +1 -0
index 33e238c965bd..807d75f5a40f 100644
@@ -363,6 +363,7 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
  	struct ip_tunnel_info *tun_info;
  #endif
  	struct sk_buff *nl_skb;
+	int header_len;
  	int data_len;
  	int meta_len;
  	void *data;
@@ -381,12 +382,13 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
  		meta_len += psample_tunnel_meta_len(tun_info);
  #endif
 
Quoted 4 lines

net/psample/psample.c void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · line 386

Quoted 2 lines
net/psample/psample.c +2 -0
+	/* psample generic netlink header size */
+	header_len = nlmsg_total_size(GENL_HDRLEN + psample_nl_family.hdrsize);

GENL_HDRLEN is already included by genlmsg_new() and fam->hdrsize is 0 / uninitialized for psample_nl_family. What am I missing? Ido?

Quoted 5 lines

Yea, I missed that genlmsg_new() eventually accounts for 'GENL_HDRLEN'.

Chris, assuming the problem is nla_put(), I think some other attribute is not accounted for when calculating the size of the skb. Does it only happen with packets that include tunnel metadata?

Yes.

Quoted 9 lines

Because I think I see
a few problems there:

net/psample/psample.c +0 -0
index 33e238c965bd..1a233cd128c7 100644
@@ -311,8 +311,10 @@ static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info)
         int tun_opts_len = tun_info->options_len;

net/psample/psample.c static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info) · line 320

Quoted 10 lines
net/psample/psample.c +3 -1
         int sum = 0;
 
+       sum += nla_total_size(0);       /* PSAMPLE_ATTR_TUNNEL */
+
         if (tun_key->tun_flags & TUNNEL_KEY)
-               sum += nla_total_size(sizeof(u64));
+               sum += nla_total_size_64bit(sizeof(u64));
 
         if (tun_info->mode & IP_TUNNEL_INFO_BRIDGE)
                 sum += nla_total_size(0);

Thanks for this patch. I'll check it.

BTW, maybe I should not mention it, if we have the psample dependency removal patch which is rejected, I think we can debug the psample issue easily. Because we can unload and load psample easily. But if NIC driver calls psample api directly. We have to unload the driver first. After loading the NIC driver, we have to enable sriov and enable switchdev mode again which is time consuming.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
net/psample/psample.c +3 -4
  	data_len = min(skb->len, trunc_size);
-	if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
-		data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
+	if (header_len + meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
+		data_len = PSAMPLE_MAX_PACKET_SIZE - header_len - meta_len - NLA_HDRLEN
  			    - NLA_ALIGNTO;
-
-	nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
+	nl_skb = genlmsg_new(header_len + meta_len + nla_total_size(data_len), GFP_ATOMIC);
  	if (unlikely(!nl_skb))
  		return;
 
author
#4 lore raw
In reply to Jakub Kicinski
Quoted 4 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 3 Feb 2021 11:10:28 +0800 Chris Mi wrote:

Currently, the netlink skb length only includes metadata and data length. It doesn't include the psample generic netlink header length.

But what's the bug? Did you see oversized messages on the socket?

Yes.

Quoted 2 lines

Did
one of the nla_put() fail?

Yes.

Quoted 25 lines
Quoted 25 lines
Fixes 6ae0a6286171 ("net: Introduce psample, a new genetlink channel for packet sampling")
CC Yotam Gigi <[email protected]>
---
  net/psample/psample.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
net/psample/psample.c +1 -0
index 33e238c965bd..807d75f5a40f 100644
@@ -363,6 +363,7 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
  	struct ip_tunnel_info *tun_info;
  #endif
  	struct sk_buff *nl_skb;
+	int header_len;
  	int data_len;
  	int meta_len;
  	void *data;
@@ -381,12 +382,13 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
  		meta_len += psample_tunnel_meta_len(tun_info);
  #endif
 
Quoted 4 lines

net/psample/psample.c void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · line 386

Quoted 2 lines
net/psample/psample.c +2 -0
+	/* psample generic netlink header size */
+	header_len = nlmsg_total_size(GENL_HDRLEN + psample_nl_family.hdrsize);

GENL_HDRLEN is already included by genlmsg_new() and fam->hdrsize is 0 / uninitialized for psample_nl_family. What am I missing? Ido?

Thanks for pointing this out. If so, it seems this patch is incorrect.

Quoted 13 lines
Quoted 13 lines
net/psample/psample.c +3 -4
  	data_len = min(skb->len, trunc_size);
-	if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
-		data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
+	if (header_len + meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
+		data_len = PSAMPLE_MAX_PACKET_SIZE - header_len - meta_len - NLA_HDRLEN
  			    - NLA_ALIGNTO;
-
-	nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
+	nl_skb = genlmsg_new(header_len + meta_len + nla_total_size(data_len), GFP_ATOMIC);
  	if (unlikely(!nl_skb))
  		return;