hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
From b6c4a1b204740fe52b32e7f530831a59f4038e20 Mon Sep 17 00:00:00 2001
From: Alexey Makhalov <amakhalov@vmware.com>
Date: Thu, 9 Jul 2020 08:10:40 +0000
Subject: [PATCH] tftp: Do not use priority queue
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
 
There is not need to reassemble the order of blocks. Per RFC 1350,
server must wait for the ACK, before sending next block. Data packets
can be served immediately without putting them to priority queue.
 
Logic to handle incoming packet is this:
  - if packet block id equal to expected block id, then
    process the packet,
  - if packet block id is less than expected - this is retransmit
    of old packet, then ACK it and drop the packet,
  - if packet block id is more than expected - that shouldn't
    happen, just drop the packet.
 
It makes the tftp receive path code simpler, smaller and faster.
As a benefit, this change fixes CID# 73624 and CID# 96690, caused
by following while loop:
 
  while (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) == 0)
 
where tftph pointer is not moving from one iteration to another, causing
to serve same packet again. Luckily, double serving didn't happen due to
data->block++ during the first iteration.
 
Fixes: CID 73624, CID 96690
 
Signed-off-by: Alexey Makhalov <amakhalov@vmware.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
---
 grub-core/net/tftp.c | 168 ++++++++++++++-----------------------------
 1 file changed, 53 insertions(+), 115 deletions(-)
 
diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c
index 7d90bf66e..b4297bc8d 100644
--- a/grub-core/net/tftp.c
+++ b/grub-core/net/tftp.c
@@ -25,7 +25,6 @@
 #include <grub/mm.h>
 #include <grub/dl.h>
 #include <grub/file.h>
-#include <grub/priority_queue.h>
 #include <grub/i18n.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
@@ -106,31 +105,8 @@ typedef struct tftp_data
   int have_oack;
   struct grub_error_saved save_err;
   grub_net_udp_socket_t sock;
-  grub_priority_queue_t pq;
 } *tftp_data_t;
 
-static int
-cmp_block (grub_uint16_t a, grub_uint16_t b)
-{
-  grub_int16_t i = (grub_int16_t) (a - b);
-  if (i > 0)
-    return +1;
-  if (i < 0)
-    return -1;
-  return 0;
-}
-
-static int
-cmp (const void *a__, const void *b__)
-{
-  struct grub_net_buff *a_ = *(struct grub_net_buff **) a__;
-  struct grub_net_buff *b_ = *(struct grub_net_buff **) b__;
-  struct tftphdr *a = (struct tftphdr *) a_->data;
-  struct tftphdr *b = (struct tftphdr *) b_->data;
-  /* We want the first elements to be on top.  */
-  return -cmp_block (grub_be_to_cpu16 (a->u.data.block), grub_be_to_cpu16 (b->u.data.block));
-}
-
 static grub_err_t
 ack (tftp_data_t data, grub_uint64_t block)
 {
@@ -207,73 +183,60 @@ tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)),
       return GRUB_ERR_NONE;
     }
 
-      err = grub_priority_queue_push (data->pq, &nb);
-      if (err)
-    return err;
-
-      {
-    struct grub_net_buff **nb_top_p, *nb_top;
-    while (1)
-      {
-        nb_top_p = grub_priority_queue_top (data->pq);
-        if (!nb_top_p)
-          return GRUB_ERR_NONE;
-        nb_top = *nb_top_p;
-        tftph = (struct tftphdr *) nb_top->data;
-        if (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) >= 0)
-          break;
-        ack (data, grub_be_to_cpu16 (tftph->u.data.block));
-        grub_netbuff_free (nb_top);
-        grub_priority_queue_pop (data->pq);
-      }
-    while (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) == 0)
-      {
-        unsigned size;
-
-        grub_priority_queue_pop (data->pq);
-
-        if (file->device->net->packs.count < 50)
+      /* Ack old/retransmitted block. */
+      if (grub_be_to_cpu16 (tftph->u.data.block) < data->block + 1)
+    ack (data, grub_be_to_cpu16 (tftph->u.data.block));
+      /* Ignore unexpected block. */
+      else if (grub_be_to_cpu16 (tftph->u.data.block) > data->block + 1)
+    grub_dprintf ("tftp", "TFTP unexpected block # %d\n", tftph->u.data.block);
+      else
+    {
+      unsigned size;
+
+      if (file->device->net->packs.count < 50)
+        {
           err = ack (data, data->block + 1);
-        else
-          {
-        file->device->net->stall = 1;
-        err = 0;
-          }
-        if (err)
-          return err;
-
-        err = grub_netbuff_pull (nb_top, sizeof (tftph->opcode) +
-                     sizeof (tftph->u.data.block));
-        if (err)
-          return err;
-        size = nb_top->tail - nb_top->data;
-
-        data->block++;
-        if (size < data->block_size)
-          {
-        if (data->ack_sent < data->block)
-          ack (data, data->block);
-        file->device->net->eof = 1;
-        file->device->net->stall = 1;
-        grub_net_udp_close (data->sock);
-        data->sock = NULL;
-          }
-        /* Prevent garbage in broken cards. Is it still necessary
-           given that IP implementation has been fixed?
-         */
-        if (size > data->block_size)
-          {
-        err = grub_netbuff_unput (nb_top, size - data->block_size);
-        if (err)
-          return err;
-          }
-        /* If there is data, puts packet in socket list. */
-        if ((nb_top->tail - nb_top->data) > 0)
-          grub_net_put_packet (&file->device->net->packs, nb_top);
-        else
-          grub_netbuff_free (nb_top);
-      }
-      }
+          if (err)
+        return err;
+        }
+      else
+        file->device->net->stall = 1;
+
+      err = grub_netbuff_pull (nb, sizeof (tftph->opcode) +
+                   sizeof (tftph->u.data.block));
+      if (err)
+        return err;
+      size = nb->tail - nb->data;
+
+      data->block++;
+      if (size < data->block_size)
+        {
+          if (data->ack_sent < data->block)
+        ack (data, data->block);
+          file->device->net->eof = 1;
+          file->device->net->stall = 1;
+          grub_net_udp_close (data->sock);
+          data->sock = NULL;
+        }
+      /*
+       * Prevent garbage in broken cards. Is it still necessary
+       * given that IP implementation has been fixed?
+       */
+      if (size > data->block_size)
+        {
+          err = grub_netbuff_unput (nb, size - data->block_size);
+          if (err)
+        return err;
+        }
+      /* If there is data, puts packet in socket list. */
+      if ((nb->tail - nb->data) > 0)
+        {
+          grub_net_put_packet (&file->device->net->packs, nb);
+          /* Do not free nb. */
+          return GRUB_ERR_NONE;
+        }
+    }
+      grub_netbuff_free (nb);
       return GRUB_ERR_NONE;
     case TFTP_ERROR:
       data->have_oack = 1;
@@ -287,19 +250,6 @@ tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)),
     }
 }
 
-static void
-destroy_pq (tftp_data_t data)
-{
-  struct grub_net_buff **nb_p;
-  while ((nb_p = grub_priority_queue_top (data->pq)))
-    {
-      grub_netbuff_free (*nb_p);
-      grub_priority_queue_pop (data->pq);
-    }
-
-  grub_priority_queue_destroy (data->pq);
-}
-
 static grub_err_t
 tftp_open (struct grub_file *file, const char *filename)
 {
@@ -372,17 +322,9 @@ tftp_open (struct grub_file *file, const char *filename)
   file->not_easily_seekable = 1;
   file->data = data;
 
-  data->pq = grub_priority_queue_new (sizeof (struct grub_net_buff *), cmp);
-  if (!data->pq)
-    {
-      grub_free (data);
-      return grub_errno;
-    }
-
   err = grub_net_resolve_address (file->device->net->server, &addr);
   if (err)
     {
-      destroy_pq (data);
       grub_free (data);
       return err;
     }
@@ -392,7 +334,6 @@ tftp_open (struct grub_file *file, const char *filename)
                   file);
   if (!data->sock)
     {
-      destroy_pq (data);
       grub_free (data);
       return grub_errno;
     }
@@ -406,7 +347,6 @@ tftp_open (struct grub_file *file, const char *filename)
       if (err)
     {
       grub_net_udp_close (data->sock);
-      destroy_pq (data);
       grub_free (data);
       return err;
     }
@@ -423,7 +363,6 @@ tftp_open (struct grub_file *file, const char *filename)
   if (grub_errno)
     {
       grub_net_udp_close (data->sock);
-      destroy_pq (data);
       grub_free (data);
       return grub_errno;
     }
@@ -466,7 +405,6 @@ tftp_close (struct grub_file *file)
     grub_print_error ();
       grub_net_udp_close (data->sock);
     }
-  destroy_pq (data);
   grub_free (data);
   return GRUB_ERR_NONE;
 }
-- 
2.26.2