]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Wrq.c
Fix a bug that mtftp4 driver reply 2 ACK in answer to incoming No.65535 block data...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Mtftp4Dxe / Mtftp4Wrq.c
1 /** @file
2 Routines to process Wrq (upload).
3
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Mtftp4Impl.h"
16
17
18
19 /**
20 Build then send a MTFTP data packet for the MTFTP upload session.
21
22 @param Instance The MTFTP upload session.
23 @param BlockNum The block number to send.
24
25 @retval EFI_OUT_OF_RESOURCES Failed to build the packet.
26 @retval EFI_ABORTED The consumer of this child directs to abort the
27 transmission by return an error through PacketNeeded.
28 @retval EFI_SUCCESS The data is sent.
29
30 **/
31 EFI_STATUS
32 Mtftp4WrqSendBlock (
33 IN OUT MTFTP4_PROTOCOL *Instance,
34 IN UINT16 BlockNum
35 )
36 {
37 EFI_MTFTP4_PACKET *Packet;
38 EFI_MTFTP4_TOKEN *Token;
39 NET_BUF *UdpPacket;
40 EFI_STATUS Status;
41 UINT16 DataLen;
42 UINT8 *DataBuf;
43 UINT64 Start;
44
45 //
46 // Allocate a buffer to hold the user data
47 //
48 UdpPacket = NetbufAlloc (Instance->BlkSize + MTFTP4_DATA_HEAD_LEN);
49
50 if (UdpPacket == NULL) {
51 return EFI_OUT_OF_RESOURCES;
52 }
53
54 Packet = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (UdpPacket, MTFTP4_DATA_HEAD_LEN, FALSE);
55 ASSERT (Packet != NULL);
56
57 Packet->Data.OpCode = HTONS (EFI_MTFTP4_OPCODE_DATA);
58 Packet->Data.Block = HTONS (BlockNum);
59
60 //
61 // Read the block from either the buffer or PacketNeeded callback
62 //
63 Token = Instance->Token;
64 DataLen = Instance->BlkSize;
65
66 if (Token->Buffer != NULL) {
67 Start = MultU64x32 (BlockNum - 1, Instance->BlkSize);
68
69 if (Token->BufferSize < Start + Instance->BlkSize) {
70 DataLen = (UINT16) (Token->BufferSize - Start);
71 Instance->LastBlock = BlockNum;
72 Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum);
73 }
74
75 if (DataLen > 0) {
76 NetbufAllocSpace (UdpPacket, DataLen, FALSE);
77 CopyMem (Packet->Data.Data, (UINT8 *) Token->Buffer + Start, DataLen);
78 }
79
80 } else {
81 //
82 // Get data from PacketNeeded
83 //
84 DataBuf = NULL;
85 Status = Token->PacketNeeded (
86 &Instance->Mtftp4,
87 Token,
88 &DataLen,
89 (VOID **) &DataBuf
90 );
91
92 if (EFI_ERROR (Status) || (DataLen > Instance->BlkSize)) {
93 if (DataBuf != NULL) {
94 FreePool (DataBuf);
95 }
96
97 Mtftp4SendError (
98 Instance,
99 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
100 (UINT8 *) "User aborted the transfer"
101 );
102
103 return EFI_ABORTED;
104 }
105
106 if (DataLen < Instance->BlkSize) {
107 Instance->LastBlock = BlockNum;
108 Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum);
109 }
110
111 if (DataLen > 0) {
112 NetbufAllocSpace (UdpPacket, DataLen, FALSE);
113 CopyMem (Packet->Data.Data, DataBuf, DataLen);
114 FreePool (DataBuf);
115 }
116 }
117
118 return Mtftp4SendPacket (Instance, UdpPacket);
119 }
120
121
122 /**
123 Function to handle received ACK packet.
124
125 If the ACK number matches the expected block number, and there are more
126 data pending, send the next block. Otherwise tell the caller that we are done.
127
128 @param Instance The MTFTP upload session
129 @param Packet The MTFTP packet received
130 @param Len The packet length
131 @param Completed Return whether the upload has finished.
132
133 @retval EFI_SUCCESS The ACK is successfully processed.
134 @retval EFI_TFTP_ERROR The block number loops back.
135 @retval Others Failed to transmit the next data packet.
136
137 **/
138 EFI_STATUS
139 Mtftp4WrqHandleAck (
140 IN MTFTP4_PROTOCOL *Instance,
141 IN EFI_MTFTP4_PACKET *Packet,
142 IN UINT32 Len,
143 OUT BOOLEAN *Completed
144 )
145 {
146 UINT16 AckNum;
147 INTN Expected;
148 UINT64 TotalBlock;
149
150 *Completed = FALSE;
151 AckNum = NTOHS (Packet->Ack.Block[0]);
152 Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
153
154 ASSERT (Expected >= 0);
155
156 //
157 // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp4WrqInput
158 // restart receive.
159 //
160 if (Expected != AckNum) {
161 return EFI_SUCCESS;
162 }
163
164 //
165 // Remove the acked block number, if this is the last block number,
166 // tell the Mtftp4WrqInput to finish the transfer. This is the last
167 // block number if the block range are empty..
168 //
169 Mtftp4RemoveBlockNum (&Instance->Blocks, AckNum, *Completed,&TotalBlock);
170
171 Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
172
173 if (Expected < 0) {
174
175 //
176 // The block range is empty. It may either because the the last
177 // block has been ACKed, or the sequence number just looped back,
178 // that is, there is more than 0xffff blocks.
179 //
180 if (Instance->LastBlock == AckNum) {
181 ASSERT (Instance->LastBlock >= 1);
182 *Completed = TRUE;
183 return EFI_SUCCESS;
184
185 } else {
186 Mtftp4SendError (
187 Instance,
188 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
189 (UINT8 *) "Block number rolls back, not supported, try blksize option"
190 );
191
192 return EFI_TFTP_ERROR;
193 }
194 }
195
196 return Mtftp4WrqSendBlock (Instance, (UINT16) Expected);
197 }
198
199
200 /**
201 Check whether the received OACK is valid.
202
203 The OACK is valid only if:
204 1. It only include options requested by us
205 2. It can only include a smaller block size
206 3. It can't change the proposed time out value.
207 4. Other requirements of the individal MTFTP options as required.
208
209 @param Reply The options included in the OACK
210 @param Request The options we requested
211
212 @retval TRUE The options included in OACK is valid.
213 @retval FALSE The options included in OACK is invalid.
214
215 **/
216 BOOLEAN
217 Mtftp4WrqOackValid (
218 IN MTFTP4_OPTION *Reply,
219 IN MTFTP4_OPTION *Request
220 )
221 {
222 //
223 // It is invalid for server to return options we don't request
224 //
225 if ((Reply->Exist & ~Request->Exist) != 0) {
226 return FALSE;
227 }
228
229 //
230 // Server can only specify a smaller block size to be used and
231 // return the timeout matches that requested.
232 //
233 if ((((Reply->Exist & MTFTP4_BLKSIZE_EXIST) != 0) && (Reply->BlkSize > Request->BlkSize)) ||
234 (((Reply->Exist & MTFTP4_TIMEOUT_EXIST) != 0) && (Reply->Timeout != Request->Timeout))) {
235 return FALSE;
236 }
237
238 return TRUE;
239 }
240
241
242 /**
243 Function to handle the MTFTP OACK packet.
244
245 It parses the packet's options, and update the internal states of the session.
246
247 @param Instance The MTFTP session
248 @param Packet The received OACK packet
249 @param Len The length of the packet
250 @param Completed Whether the transmisson has completed. NOT used by
251 this function.
252
253 @retval EFI_SUCCESS The OACK process is OK
254 @retval EFI_TFTP_ERROR Some error occured, and the session reset.
255
256 **/
257 EFI_STATUS
258 Mtftp4WrqHandleOack (
259 IN OUT MTFTP4_PROTOCOL *Instance,
260 IN EFI_MTFTP4_PACKET *Packet,
261 IN UINT32 Len,
262 OUT BOOLEAN *Completed
263 )
264 {
265 MTFTP4_OPTION Reply;
266 EFI_MTFTP4_PACKET Bogus;
267 EFI_STATUS Status;
268 INTN Expected;
269
270 *Completed = FALSE;
271
272 //
273 // Ignore the OACK if already started the upload
274 //
275 Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
276
277 if (Expected != 0) {
278 return EFI_SUCCESS;
279 }
280
281 //
282 // Parse and validate the options from server
283 //
284 ZeroMem (&Reply, sizeof (MTFTP4_OPTION));
285 Status = Mtftp4ParseOptionOack (Packet, Len, &Reply);
286
287 if (EFI_ERROR (Status) || !Mtftp4WrqOackValid (&Reply, &Instance->RequestOption)) {
288 //
289 // Don't send a MTFTP error packet when out of resource, it can
290 // only make it worse.
291 //
292 if (Status != EFI_OUT_OF_RESOURCES) {
293 Mtftp4SendError (
294 Instance,
295 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION,
296 (UINT8 *) "Mal-formated OACK packet"
297 );
298 }
299
300 return EFI_TFTP_ERROR;
301 }
302
303 if (Reply.BlkSize != 0) {
304 Instance->BlkSize = Reply.BlkSize;
305 }
306
307 if (Reply.Timeout != 0) {
308 Instance->Timeout = Reply.Timeout;
309 }
310
311 //
312 // Build a bogus ACK0 packet then pass it to the Mtftp4WrqHandleAck,
313 // which will start the transmission of the first data block.
314 //
315 Bogus.Ack.OpCode = HTONS (EFI_MTFTP4_OPCODE_ACK);
316 Bogus.Ack.Block[0] = 0;
317
318 Status = Mtftp4WrqHandleAck (
319 Instance,
320 &Bogus,
321 sizeof (EFI_MTFTP4_ACK_HEADER),
322 Completed
323 );
324
325 return Status;
326 }
327
328
329 /**
330 The input process routine for MTFTP upload.
331
332 @param UdpPacket The received MTFTP packet.
333 @param EndPoint The local/remote access point
334 @param IoStatus The result of the packet receiving
335 @param Context Opaque parameter for the callback, which is the
336 MTFTP session.
337 **/
338 VOID
339 EFIAPI
340 Mtftp4WrqInput (
341 IN NET_BUF *UdpPacket,
342 IN UDP_END_POINT *EndPoint,
343 IN EFI_STATUS IoStatus,
344 IN VOID *Context
345 )
346 {
347 MTFTP4_PROTOCOL *Instance;
348 EFI_MTFTP4_PACKET *Packet;
349 BOOLEAN Completed;
350 EFI_STATUS Status;
351 UINT32 Len;
352 UINT16 Opcode;
353
354 Instance = (MTFTP4_PROTOCOL *) Context;
355 NET_CHECK_SIGNATURE (Instance, MTFTP4_PROTOCOL_SIGNATURE);
356
357 Completed = FALSE;
358 Packet = NULL;
359 Status = EFI_SUCCESS;
360
361 if (EFI_ERROR (IoStatus)) {
362 Status = IoStatus;
363 goto ON_EXIT;
364 }
365
366 ASSERT (UdpPacket != NULL);
367
368 if (UdpPacket->TotalSize < MTFTP4_OPCODE_LEN) {
369 goto ON_EXIT;
370 }
371
372 //
373 // Client send initial request to server's listening port. Server
374 // will select a UDP port to communicate with the client.
375 //
376 if (EndPoint->RemotePort != Instance->ConnectedPort) {
377 if (Instance->ConnectedPort != 0) {
378 goto ON_EXIT;
379 } else {
380 Instance->ConnectedPort = EndPoint->RemotePort;
381 }
382 }
383
384 //
385 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
386 //
387 Len = UdpPacket->TotalSize;
388
389 if (UdpPacket->BlockOpNum > 1) {
390 Packet = AllocatePool (Len);
391
392 if (Packet == NULL) {
393 Status = EFI_OUT_OF_RESOURCES;
394 goto ON_EXIT;
395 }
396
397 NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet);
398
399 } else {
400 Packet = (EFI_MTFTP4_PACKET *) NetbufGetByte (UdpPacket, 0, NULL);
401 }
402
403 Opcode = NTOHS (Packet->OpCode);
404
405 //
406 // Call the user's CheckPacket if provided. Abort the transmission
407 // if CheckPacket returns an EFI_ERROR code.
408 //
409 if ((Instance->Token->CheckPacket != NULL) &&
410 ((Opcode == EFI_MTFTP4_OPCODE_OACK) || (Opcode == EFI_MTFTP4_OPCODE_ERROR))) {
411
412 Status = Instance->Token->CheckPacket (
413 &Instance->Mtftp4,
414 Instance->Token,
415 (UINT16) Len,
416 Packet
417 );
418
419 if (EFI_ERROR (Status)) {
420 //
421 // Send an error message to the server to inform it
422 //
423 if (Opcode != EFI_MTFTP4_OPCODE_ERROR) {
424 Mtftp4SendError (
425 Instance,
426 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
427 (UINT8 *) "User aborted the transfer"
428 );
429 }
430
431 Status = EFI_ABORTED;
432 goto ON_EXIT;
433 }
434 }
435
436 switch (Opcode) {
437 case EFI_MTFTP4_OPCODE_ACK:
438 if (Len != MTFTP4_OPCODE_LEN + MTFTP4_BLKNO_LEN) {
439 goto ON_EXIT;
440 }
441
442 Status = Mtftp4WrqHandleAck (Instance, Packet, Len, &Completed);
443 break;
444
445 case EFI_MTFTP4_OPCODE_OACK:
446 if (Len <= MTFTP4_OPCODE_LEN) {
447 goto ON_EXIT;
448 }
449
450 Status = Mtftp4WrqHandleOack (Instance, Packet, Len, &Completed);
451 break;
452
453 case EFI_MTFTP4_OPCODE_ERROR:
454 Status = EFI_TFTP_ERROR;
455 break;
456
457 default:
458 break;
459 }
460
461 ON_EXIT:
462 //
463 // Free the resources, then if !EFI_ERROR (Status) and not completed,
464 // restart the receive, otherwise end the session.
465 //
466 if ((Packet != NULL) && (UdpPacket->BlockOpNum > 1)) {
467 FreePool (Packet);
468 }
469
470 if (UdpPacket != NULL) {
471 NetbufFree (UdpPacket);
472 }
473
474 if (!EFI_ERROR (Status) && !Completed) {
475 Status = UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0);
476 }
477
478 //
479 // Status may have been updated by UdpIoRecvDatagram
480 //
481 if (EFI_ERROR (Status) || Completed) {
482 Mtftp4CleanOperation (Instance, Status);
483 }
484 }
485
486
487
488 /**
489 Start the MTFTP session for upload.
490
491 It will first init some states, then send the WRQ request packet,
492 and start receiving the packet.
493
494 @param Instance The MTFTP session
495 @param Operation Redundant parameter, which is always
496 EFI_MTFTP4_OPCODE_WRQ here.
497
498 @retval EFI_SUCCESS The upload process has been started.
499 @retval Others Failed to start the upload.
500
501 **/
502 EFI_STATUS
503 Mtftp4WrqStart (
504 IN MTFTP4_PROTOCOL *Instance,
505 IN UINT16 Operation
506 )
507 {
508 EFI_STATUS Status;
509
510 //
511 // The valid block number range are [0, 0xffff]. For example:
512 // the client sends an WRQ request to the server, the server
513 // ACK with an ACK0 to let client start transfer the first
514 // packet.
515 //
516 Status = Mtftp4InitBlockRange (&Instance->Blocks, 0, 0xffff);
517
518 if (EFI_ERROR (Status)) {
519 return Status;
520 }
521
522 Status = Mtftp4SendRequest (Instance);
523
524 if (EFI_ERROR (Status)) {
525 return Status;
526 }
527
528 return UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0);
529 }
530