nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0294-tftp-Do-not-use-priority-queue.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Alexey Makhalov <amakhalov@vmware.com>
80913e
Date: Thu, 9 Jul 2020 08:10:40 +0000
80913e
Subject: [PATCH] tftp: Do not use priority queue
80913e
80913e
There is not need to reassemble the order of blocks. Per RFC 1350,
80913e
server must wait for the ACK, before sending next block. Data packets
80913e
can be served immediately without putting them to priority queue.
80913e
80913e
Logic to handle incoming packet is this:
80913e
  - if packet block id equal to expected block id, then
80913e
    process the packet,
80913e
  - if packet block id is less than expected - this is retransmit
80913e
    of old packet, then ACK it and drop the packet,
80913e
  - if packet block id is more than expected - that shouldn't
80913e
    happen, just drop the packet.
80913e
80913e
It makes the tftp receive path code simpler, smaller and faster.
80913e
As a benefit, this change fixes CID# 73624 and CID# 96690, caused
80913e
by following while loop:
80913e
80913e
  while (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) == 0)
80913e
80913e
where tftph pointer is not moving from one iteration to another, causing
80913e
to serve same packet again. Luckily, double serving didn't happen due to
80913e
data->block++ during the first iteration.
80913e
80913e
Fixes: CID 73624, CID 96690
80913e
80913e
Signed-off-by: Alexey Makhalov <amakhalov@vmware.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
Upstream-commit-id: 8316694c4f7
80913e
---
80913e
 grub-core/net/tftp.c | 174 ++++++++++++++++-----------------------------------
80913e
 1 file changed, 54 insertions(+), 120 deletions(-)
80913e
80913e
diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c
b32e65
index e267af354..79c16f9b0 100644
80913e
--- a/grub-core/net/tftp.c
80913e
+++ b/grub-core/net/tftp.c
80913e
@@ -25,7 +25,6 @@
80913e
 #include <grub/mm.h>
80913e
 #include <grub/dl.h>
80913e
 #include <grub/file.h>
80913e
-#include <grub/priority_queue.h>
80913e
 #include <grub/i18n.h>
80913e
 
80913e
 GRUB_MOD_LICENSE ("GPLv3+");
80913e
@@ -106,31 +105,8 @@ typedef struct tftp_data
80913e
   int have_oack;
80913e
   struct grub_error_saved save_err;
80913e
   grub_net_udp_socket_t sock;
80913e
-  grub_priority_queue_t pq;
80913e
 } *tftp_data_t;
80913e
 
80913e
-static int
80913e
-cmp_block (grub_uint16_t a, grub_uint16_t b)
80913e
-{
80913e
-  grub_int16_t i = (grub_int16_t) (a - b);
80913e
-  if (i > 0)
80913e
-    return +1;
80913e
-  if (i < 0)
80913e
-    return -1;
80913e
-  return 0;
80913e
-}
80913e
-
80913e
-static int
80913e
-cmp (const void *a__, const void *b__)
80913e
-{
80913e
-  struct grub_net_buff *a_ = *(struct grub_net_buff **) a__;
80913e
-  struct grub_net_buff *b_ = *(struct grub_net_buff **) b__;
80913e
-  struct tftphdr *a = (struct tftphdr *) a_->data;
80913e
-  struct tftphdr *b = (struct tftphdr *) b_->data;
80913e
-  /* We want the first elements to be on top.  */
80913e
-  return -cmp_block (grub_be_to_cpu16 (a->u.data.block), grub_be_to_cpu16 (b->u.data.block));
80913e
-}
80913e
-
80913e
 static grub_err_t
80913e
 ack (tftp_data_t data, grub_uint64_t block)
80913e
 {
80913e
@@ -207,73 +183,60 @@ tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)),
80913e
 	  return GRUB_ERR_NONE;
80913e
 	}
80913e
 
80913e
-      err = grub_priority_queue_push (data->pq, &nb);
80913e
-      if (err)
80913e
-	return err;
80913e
+      /* Ack old/retransmitted block. */
80913e
+      if (grub_be_to_cpu16 (tftph->u.data.block) < data->block + 1)
80913e
+	ack (data, grub_be_to_cpu16 (tftph->u.data.block));
80913e
+      /* Ignore unexpected block. */
80913e
+      else if (grub_be_to_cpu16 (tftph->u.data.block) > data->block + 1)
80913e
+	grub_dprintf ("tftp", "TFTP unexpected block # %d\n", tftph->u.data.block);
80913e
+      else
80913e
+	{
80913e
+	  unsigned size;
80913e
 
80913e
-      {
80913e
-	struct grub_net_buff **nb_top_p, *nb_top;
80913e
-	while (1)
80913e
-	  {
80913e
-	    nb_top_p = grub_priority_queue_top (data->pq);
80913e
-	    if (!nb_top_p)
80913e
-	      return GRUB_ERR_NONE;
80913e
-	    nb_top = *nb_top_p;
80913e
-	    tftph = (struct tftphdr *) nb_top->data;
80913e
-	    if (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) >= 0)
80913e
-	      break;
80913e
-	    ack (data, grub_be_to_cpu16 (tftph->u.data.block));
80913e
-	    grub_netbuff_free (nb_top);
80913e
-	    grub_priority_queue_pop (data->pq);
80913e
-	  }
80913e
-	while (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) == 0)
80913e
-	  {
80913e
-	    unsigned size;
80913e
-
80913e
-	    grub_priority_queue_pop (data->pq);
80913e
-
80913e
-	    if (file->device->net->packs.count < 50)
80913e
+	  if (file->device->net->packs.count < 50)
80913e
+	    {
80913e
 	      err = ack (data, data->block + 1);
80913e
-	    else
80913e
-	      {
80913e
-		file->device->net->stall = 1;
80913e
-		err = 0;
80913e
-	      }
80913e
-	    if (err)
80913e
-	      return err;
80913e
+	      if (err)
80913e
+		return err;
80913e
+	    }
80913e
+	  else
80913e
+	    file->device->net->stall = 1;
80913e
 
80913e
-	    err = grub_netbuff_pull (nb_top, sizeof (tftph->opcode) +
80913e
-				     sizeof (tftph->u.data.block));
80913e
-	    if (err)
80913e
-	      return err;
80913e
-	    size = nb_top->tail - nb_top->data;
80913e
+	  err = grub_netbuff_pull (nb, sizeof (tftph->opcode) +
80913e
+				   sizeof (tftph->u.data.block));
80913e
+	  if (err)
80913e
+	    return err;
80913e
+	  size = nb->tail - nb->data;
80913e
 
80913e
-	    data->block++;
80913e
-	    if (size < data->block_size)
80913e
-	      {
80913e
-		if (data->ack_sent < data->block)
80913e
-		  ack (data, data->block);
80913e
-		file->device->net->eof = 1;
80913e
-		file->device->net->stall = 1;
80913e
-		grub_net_udp_close (data->sock);
80913e
-		data->sock = NULL;
80913e
-	      }
80913e
-	    /* Prevent garbage in broken cards. Is it still necessary
80913e
-	       given that IP implementation has been fixed?
80913e
-	     */
80913e
-	    if (size > data->block_size)
80913e
-	      {
80913e
-		err = grub_netbuff_unput (nb_top, size - data->block_size);
80913e
-		if (err)
80913e
-		  return err;
80913e
-	      }
80913e
-	    /* If there is data, puts packet in socket list. */
80913e
-	    if ((nb_top->tail - nb_top->data) > 0)
80913e
-	      grub_net_put_packet (&file->device->net->packs, nb_top);
80913e
-	    else
80913e
-	      grub_netbuff_free (nb_top);
80913e
-	  }
80913e
-      }
80913e
+	  data->block++;
80913e
+	  if (size < data->block_size)
80913e
+	    {
80913e
+	      if (data->ack_sent < data->block)
80913e
+		ack (data, data->block);
80913e
+	      file->device->net->eof = 1;
80913e
+	      file->device->net->stall = 1;
80913e
+	      grub_net_udp_close (data->sock);
80913e
+	      data->sock = NULL;
80913e
+	    }
80913e
+	  /*
80913e
+	   * Prevent garbage in broken cards. Is it still necessary
80913e
+	   * given that IP implementation has been fixed?
80913e
+	   */
80913e
+	  if (size > data->block_size)
80913e
+	    {
80913e
+	      err = grub_netbuff_unput (nb, size - data->block_size);
80913e
+	      if (err)
80913e
+		return err;
80913e
+	    }
80913e
+	  /* If there is data, puts packet in socket list. */
80913e
+	  if ((nb->tail - nb->data) > 0)
80913e
+	    {
80913e
+	      grub_net_put_packet (&file->device->net->packs, nb);
80913e
+	      /* Do not free nb. */
80913e
+	      return GRUB_ERR_NONE;
80913e
+	    }
80913e
+	}
80913e
+      grub_netbuff_free (nb);
80913e
       return GRUB_ERR_NONE;
80913e
     case TFTP_ERROR:
80913e
       data->have_oack = 1;
80913e
@@ -287,22 +250,10 @@ tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)),
80913e
     }
80913e
 }
80913e
 
80913e
-static void
80913e
-destroy_pq (tftp_data_t data)
80913e
-{
80913e
-  struct grub_net_buff **nb_p;
80913e
-  while ((nb_p = grub_priority_queue_top (data->pq)))
80913e
-    {
80913e
-      grub_netbuff_free (*nb_p);
80913e
-      grub_priority_queue_pop (data->pq);
80913e
-    }
80913e
-
80913e
-  grub_priority_queue_destroy (data->pq);
80913e
-}
80913e
-
80913e
-/* Create a normalized copy of the filename.
80913e
-   Compress any string of consecutive forward slashes to a single forward
80913e
-   slash. */
80913e
+/*
80913e
+ * Create a normalized copy of the filename. Compress any string of consecutive
80913e
+ * forward slashes to a single forward slash.
80913e
+ */
80913e
 static void
80913e
 grub_normalize_filename (char *normalized, const char *filename)
80913e
 {
80913e
@@ -395,22 +346,9 @@ tftp_open (struct grub_file *file, const char *filename)
80913e
   file->not_easily_seekable = 1;
80913e
   file->data = data;
80913e
 
80913e
-  data->pq = grub_priority_queue_new (sizeof (struct grub_net_buff *), cmp);
80913e
-  if (!data->pq)
80913e
-    {
80913e
-      grub_free (data);
80913e
-      return grub_errno;
80913e
-    }
80913e
-
80913e
-  grub_dprintf("tftp", "resolving address for %s\n", file->device->net->server);
80913e
   err = grub_net_resolve_address (file->device->net->server, &addr);
80913e
   if (err)
80913e
     {
80913e
-      grub_dprintf ("tftp", "Address resolution failed: %d\n", err);
80913e
-      grub_dprintf ("tftp", "file_size is %llu, block_size is %llu\n",
80913e
-		    (unsigned long long)data->file_size,
80913e
-		    (unsigned long long)data->block_size);
80913e
-      destroy_pq (data);
80913e
       grub_free (data);
80913e
       return err;
80913e
     }
80913e
@@ -422,7 +360,6 @@ tftp_open (struct grub_file *file, const char *filename)
80913e
   if (!data->sock)
80913e
     {
80913e
       grub_dprintf("tftp", "connection failed\n");
80913e
-      destroy_pq (data);
80913e
       grub_free (data);
80913e
       return grub_errno;
80913e
     }
80913e
@@ -436,7 +373,6 @@ tftp_open (struct grub_file *file, const char *filename)
80913e
       if (err)
80913e
 	{
80913e
 	  grub_net_udp_close (data->sock);
80913e
-	  destroy_pq (data);
80913e
 	  grub_free (data);
80913e
 	  return err;
80913e
 	}
80913e
@@ -453,7 +389,6 @@ tftp_open (struct grub_file *file, const char *filename)
80913e
   if (grub_errno)
80913e
     {
80913e
       grub_net_udp_close (data->sock);
80913e
-      destroy_pq (data);
80913e
       grub_free (data);
80913e
       return grub_errno;
80913e
     }
80913e
@@ -496,7 +431,6 @@ tftp_close (struct grub_file *file)
80913e
 	grub_print_error ();
80913e
       grub_net_udp_close (data->sock);
80913e
     }
80913e
-  destroy_pq (data);
80913e
   grub_free (data);
80913e
   return GRUB_ERR_NONE;
80913e
 }