diff mbox series

[v3] Add payload to be 32-bit aligned to fix dropped packets

Message ID 20211026144953.GA15611@gmail.com
State New
Headers show
Series [v3] Add payload to be 32-bit aligned to fix dropped packets | expand

Commit Message

Kumar Thangavel Oct. 26, 2021, 2:49 p.m. UTC
Update NC-SI command handler (both standard and OEM) to take into
account of payload paddings in allocating skb (in case of payload
size is not 32-bit aligned).

The checksum field follows payload field, without taking payload
padding into account can cause checksum being truncated, leading to
dropped packets.

Signed-off-by: Kumar Thangavel <kumarthangavel.hcl@gmail.com>
Acked-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
Reviewed-by: Paul Menzel <pmenzel@molgen.mpg.de>

---
  v3:
   - Added Macro for MAX
   - Fixed the missed semicolon

  v2:
   - Added NC-SI spec version and section
   - Removed blank line
   - corrected spellings

  v1:
   - Initial draft

---
 net/ncsi/ncsi-cmd.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

Comments

Jakub Kicinski Oct. 26, 2021, 3:33 p.m. UTC | #1
On Tue, 26 Oct 2021 20:19:53 +0530 Kumar Thangavel wrote:
> Update NC-SI command handler (both standard and OEM) to take into
> account of payload paddings in allocating skb (in case of payload
> size is not 32-bit aligned).
> 
> The checksum field follows payload field, without taking payload
> padding into account can cause checksum being truncated, leading to
> dropped packets.
> 
> Signed-off-by: Kumar Thangavel <kumarthangavel.hcl@gmail.com>
> Acked-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
> Reviewed-by: Paul Menzel <pmenzel@molgen.mpg.de>

You need to CC netdev@ if you want this applied to networking trees.

> diff --git a/net/ncsi/ncsi-cmd.c b/net/ncsi/ncsi-cmd.c
> index ba9ae482141b..179e39105072 100644
> --- a/net/ncsi/ncsi-cmd.c
> +++ b/net/ncsi/ncsi-cmd.c
> @@ -18,6 +18,8 @@
>  #include "internal.h"
>  #include "ncsi-pkt.h"
>  
> +#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))

Please use one of the existing max() macroes in the kernel.
Kumar Thangavel Oct. 26, 2021, 4:38 p.m. UTC | #2
On Tue, Oct 26, 2021 at 9:03 PM Jakub Kicinski <kuba@kernel.org> wrote:

> On Tue, 26 Oct 2021 20:19:53 +0530 Kumar Thangavel wrote:
> > Update NC-SI command handler (both standard and OEM) to take into
> > account of payload paddings in allocating skb (in case of payload
> > size is not 32-bit aligned).
> >
> > The checksum field follows payload field, without taking payload
> > padding into account can cause checksum being truncated, leading to
> > dropped packets.
> >
> > Signed-off-by: Kumar Thangavel <kumarthangavel.hcl@gmail.com>
> > Acked-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
> > Reviewed-by: Paul Menzel <pmenzel@molgen.mpg.de>
>
> You need to CC netdev@ if you want this applied to networking trees.
>
> > diff --git a/net/ncsi/ncsi-cmd.c b/net/ncsi/ncsi-cmd.c
> > index ba9ae482141b..179e39105072 100644
> > --- a/net/ncsi/ncsi-cmd.c
> > +++ b/net/ncsi/ncsi-cmd.c
> > @@ -18,6 +18,8 @@
> >  #include "internal.h"
> >  #include "ncsi-pkt.h"
> >
> > +#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
>
> Please use one of the existing max() macroes in the kernel.
>

Ack.  I will use existing macros.

Thanks,
Kumar.
diff mbox series

Patch

diff --git a/net/ncsi/ncsi-cmd.c b/net/ncsi/ncsi-cmd.c
index ba9ae482141b..179e39105072 100644
--- a/net/ncsi/ncsi-cmd.c
+++ b/net/ncsi/ncsi-cmd.c
@@ -18,6 +18,8 @@ 
 #include "internal.h"
 #include "ncsi-pkt.h"
 
+#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
+
 u32 ncsi_calculate_checksum(unsigned char *data, int len)
 {
 	u32 checksum = 0;
@@ -213,12 +215,16 @@  static int ncsi_cmd_handler_oem(struct sk_buff *skb,
 {
 	struct ncsi_cmd_oem_pkt *cmd;
 	unsigned int len;
+	/* NC-SI spec DSP_0222_1.2.0, section 8.2.2.2
+	 * requires payload to be padded with 0 to
+	 * 32-bit boundary before the checksum field.
+	 * Ensure the padding bytes are accounted for in
+	 * skb allocation
+	 */
 
+	unsigned short payload = ALIGN(nca->payload, 4);
 	len = sizeof(struct ncsi_cmd_pkt_hdr) + 4;
-	if (nca->payload < 26)
-		len += 26;
-	else
-		len += nca->payload;
+	len += MAX(payload, 26);
 
 	cmd = skb_put_zero(skb, len);
 	memcpy(&cmd->mfr_id, nca->data, nca->payload);
@@ -272,6 +278,7 @@  static struct ncsi_request *ncsi_alloc_command(struct ncsi_cmd_arg *nca)
 	struct net_device *dev = nd->dev;
 	int hlen = LL_RESERVED_SPACE(dev);
 	int tlen = dev->needed_tailroom;
+	int payload;
 	int len = hlen + tlen;
 	struct sk_buff *skb;
 	struct ncsi_request *nr;
@@ -281,14 +288,14 @@  static struct ncsi_request *ncsi_alloc_command(struct ncsi_cmd_arg *nca)
 		return NULL;
 
 	/* NCSI command packet has 16-bytes header, payload, 4 bytes checksum.
+	 * Payload needs padding so that the checksum field following payload is
+	 * aligned to 32-bit boundary.
 	 * The packet needs padding if its payload is less than 26 bytes to
 	 * meet 64 bytes minimal ethernet frame length.
 	 */
 	len += sizeof(struct ncsi_cmd_pkt_hdr) + 4;
-	if (nca->payload < 26)
-		len += 26;
-	else
-		len += nca->payload;
+	payload = ALIGN(nca->payload, 4);
+	len += MAX(payload, 26);
 
 	/* Allocate skb */
 	skb = alloc_skb(len, GFP_ATOMIC);