]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c
d60b26f6524d4933c8fdfc5b5bc4271f9c27e9b5
[mirror_edk2.git] / NetworkPkg / Mtftp6Dxe / Mtftp6Rrq.c
1 /** @file
2 Mtftp6 Rrq process functions implementation.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Mtftp6Impl.h"
17
18
19 /**
20 Build and send a ACK packet for download.
21
22 @param[in] Instance The pointer to the Mtftp6 instance.
23 @param[in] BlockNum The block number to be acked.
24
25 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet.
26 @retval EFI_SUCCESS The ACK has been sent.
27 @retval Others Failed to send the ACK.
28
29 **/
30 EFI_STATUS
31 Mtftp6RrqSendAck (
32 IN MTFTP6_INSTANCE *Instance,
33 IN UINT16 BlockNum
34 )
35 {
36 EFI_MTFTP6_PACKET *Ack;
37 NET_BUF *Packet;
38 EFI_STATUS Status;
39
40 Status = EFI_SUCCESS;
41
42 //
43 // Allocate net buffer to create ack packet.
44 //
45 Packet = NetbufAlloc (sizeof (EFI_MTFTP6_ACK_HEADER));
46
47 if (Packet == NULL) {
48 return EFI_OUT_OF_RESOURCES;
49 }
50
51 Ack = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (
52 Packet,
53 sizeof (EFI_MTFTP6_ACK_HEADER),
54 FALSE
55 );
56 ASSERT (Ack != NULL);
57
58 Ack->Ack.OpCode = HTONS (EFI_MTFTP6_OPCODE_ACK);
59 Ack->Ack.Block[0] = HTONS (BlockNum);
60
61 //
62 // Reset current retry count of the instance.
63 //
64 Instance->CurRetry = 0;
65 Instance->LastPacket = Packet;
66
67 Status = Mtftp6TransmitPacket (Instance, Packet);
68 if (!EFI_ERROR (Status)) {
69 Instance->AckedBlock = Instance->TotalBlock;
70 }
71
72 return Status;
73 }
74
75
76 /**
77 Deliver the received data block to the user, which can be saved
78 in the user provide buffer or through the CheckPacket callback.
79
80 @param[in] Instance The pointer to the Mtftp6 instance.
81 @param[in] Packet The pointer to the received packet.
82 @param[in] Len The packet length.
83 @param[out] UdpPacket The net buf of the received packet.
84
85 @retval EFI_SUCCESS The data was saved successfully.
86 @retval EFI_ABORTED The user tells to abort by return an error through
87 CheckPacket.
88 @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small, and buffer length is
89 updated to the actual buffer size needed.
90
91 **/
92 EFI_STATUS
93 Mtftp6RrqSaveBlock (
94 IN MTFTP6_INSTANCE *Instance,
95 IN EFI_MTFTP6_PACKET *Packet,
96 IN UINT32 Len,
97 OUT NET_BUF **UdpPacket
98 )
99 {
100 EFI_MTFTP6_TOKEN *Token;
101 EFI_STATUS Status;
102 UINT16 Block;
103 UINT64 Start;
104 UINT32 DataLen;
105 UINT64 BlockCounter;
106 BOOLEAN Completed;
107
108 Completed = FALSE;
109 Token = Instance->Token;
110 Block = NTOHS (Packet->Data.Block);
111 DataLen = Len - MTFTP6_DATA_HEAD_LEN;
112
113 //
114 // This is the last block, save the block num
115 //
116 if (DataLen < Instance->BlkSize) {
117 Completed = TRUE;
118 Instance->LastBlk = Block;
119 Mtftp6SetLastBlockNum (&Instance->BlkList, Block);
120 }
121
122 //
123 // Remove this block number from the file hole. If Mtftp6RemoveBlockNum
124 // returns EFI_NOT_FOUND, the block has been saved, don't save it again.
125 // Note that : For bigger files, allowing the block counter to roll over
126 // to accept transfers of unlimited size. So BlockCounter is memorised as
127 // continuous block counter.
128 //
129 Status = Mtftp6RemoveBlockNum (&Instance->BlkList, Block, Completed, &BlockCounter);
130
131 if (Status == EFI_NOT_FOUND) {
132 return EFI_SUCCESS;
133 } else if (EFI_ERROR (Status)) {
134 return Status;
135 }
136
137 if (Token->CheckPacket != NULL) {
138 //
139 // Callback to the check packet routine with the received packet.
140 //
141 Status = Token->CheckPacket (&Instance->Mtftp6, Token, (UINT16) Len, Packet);
142
143 if (EFI_ERROR (Status)) {
144 //
145 // Free the received packet before send new packet in ReceiveNotify,
146 // since the Udp6Io might need to be reconfigured.
147 //
148 NetbufFree (*UdpPacket);
149 *UdpPacket = NULL;
150 //
151 // Send the Mtftp6 error message if user aborted the current session.
152 //
153 Mtftp6SendError (
154 Instance,
155 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,
156 (UINT8 *) "User aborted download"
157 );
158
159 return EFI_ABORTED;
160 }
161 }
162
163 if (Token->Buffer != NULL) {
164
165 Start = MultU64x32 (BlockCounter - 1, Instance->BlkSize);
166 if (Start + DataLen <= Token->BufferSize) {
167 CopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen);
168 //
169 // Update the file size when received the last block
170 //
171 if ((Instance->LastBlk == Block) && Completed) {
172 Token->BufferSize = Start + DataLen;
173 }
174 } else if (Instance->LastBlk != 0) {
175 //
176 // Don't save the data if the buffer is too small, return
177 // EFI_BUFFER_TOO_SMALL if received the last packet. This
178 // will give a accurate file length.
179 //
180 Token->BufferSize = Start + DataLen;
181
182 //
183 // Free the received packet before send new packet in ReceiveNotify,
184 // since the udpio might need to be reconfigured.
185 //
186 NetbufFree (*UdpPacket);
187 *UdpPacket = NULL;
188 //
189 // Send the Mtftp6 error message if no enough buffer.
190 //
191 Mtftp6SendError (
192 Instance,
193 EFI_MTFTP6_ERRORCODE_DISK_FULL,
194 (UINT8 *) "User provided memory block is too small"
195 );
196
197 return EFI_BUFFER_TOO_SMALL;
198 }
199 }
200
201 return EFI_SUCCESS;
202 }
203
204
205 /**
206 Process the received data packets. It will save the block
207 then send back an ACK if it is active.
208
209 @param[in] Instance The pointer to the Mtftp6 instance.
210 @param[in] Packet The pointer to the received packet.
211 @param[in] Len The length of the packet.
212 @param[out] UdpPacket The net buf of received packet.
213 @param[out] IsCompleted If TRUE, the download has been completed.
214 Otherwise, the download has not been completed.
215
216 @retval EFI_SUCCESS The data packet was successfully processed.
217 @retval EFI_ABORTED The download was aborted by the user.
218 @retval EFI_BUFFER_TOO_SMALL The user-provided buffer is too small.
219
220 **/
221 EFI_STATUS
222 Mtftp6RrqHandleData (
223 IN MTFTP6_INSTANCE *Instance,
224 IN EFI_MTFTP6_PACKET *Packet,
225 IN UINT32 Len,
226 OUT NET_BUF **UdpPacket,
227 OUT BOOLEAN *IsCompleted
228 )
229 {
230 EFI_STATUS Status;
231 UINT16 BlockNum;
232 INTN Expected;
233
234 *IsCompleted = FALSE;
235 Status = EFI_SUCCESS;
236 BlockNum = NTOHS (Packet->Data.Block);
237 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);
238
239 ASSERT (Expected >= 0);
240
241 //
242 // If we are active (Master) and received an unexpected packet, transmit
243 // the ACK for the block we received, then restart receiving the
244 // expected one. If we are passive (Slave), save the block.
245 //
246 if (Instance->IsMaster && (Expected != BlockNum)) {
247 //
248 // Free the received packet before send new packet in ReceiveNotify,
249 // since the udpio might need to be reconfigured.
250 //
251 NetbufFree (*UdpPacket);
252 *UdpPacket = NULL;
253
254 //
255 // If Expected is 0, (UINT16) (Expected - 1) is also the expected Ack number (65535).
256 //
257 return Mtftp6RrqSendAck (Instance, (UINT16) (Expected - 1));
258 }
259
260 Status = Mtftp6RrqSaveBlock (Instance, Packet, Len, UdpPacket);
261
262 if (EFI_ERROR (Status)) {
263 return Status;
264 }
265
266 //
267 // Record the total received and saved block number.
268 //
269 Instance->TotalBlock ++;
270
271 //
272 // Reset the passive client's timer whenever it received a valid data packet.
273 //
274 if (!Instance->IsMaster) {
275 Instance->PacketToLive = Instance->Timeout * 2;
276 }
277
278 //
279 // Check whether we have received all the blocks. Send the ACK if we
280 // are active (unicast client or master client for multicast download).
281 // If we have received all the blocks, send an ACK even if we are passive
282 // to tell the server that we are done.
283 //
284 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);
285
286 if (Instance->IsMaster || Expected < 0) {
287 if (Expected < 0) {
288 //
289 // If we are passive client, then the just received Block maybe
290 // isn't the last block. We need to send an ACK to the last block
291 // to inform the server that we are done. If we are active client,
292 // the Block == Instance->LastBlock.
293 //
294 BlockNum = Instance->LastBlk;
295 *IsCompleted = TRUE;
296
297 } else {
298 BlockNum = (UINT16) (Expected - 1);
299 }
300 //
301 // Free the received packet before send new packet in ReceiveNotify,
302 // since the udpio might need to be reconfigured.
303 //
304 NetbufFree (*UdpPacket);
305 *UdpPacket = NULL;
306
307 if (Instance->WindowSize == (Instance->TotalBlock - Instance->AckedBlock) || Expected < 0) {
308 Status = Mtftp6RrqSendAck (Instance, BlockNum);
309 }
310 }
311
312 return Status;
313 }
314
315
316 /**
317 Validate whether the options received in the server's OACK packet is valid.
318 The options are valid only if:
319 1. The server doesn't include options not requested by us.
320 2. The server can only use smaller blksize than that is requested.
321 3. The server can only use the same timeout as requested.
322 4. The server doesn't change its multicast channel.
323
324 @param[in] Instance The pointer to the Mtftp6 instance.
325 @param[in] ReplyInfo The pointer to options information in reply packet.
326 @param[in] RequestInfo The pointer to requested options info.
327
328 @retval TRUE If the option in the OACK is valid.
329 @retval FALSE If the option is invalid.
330
331 **/
332 BOOLEAN
333 Mtftp6RrqOackValid (
334 IN MTFTP6_INSTANCE *Instance,
335 IN MTFTP6_EXT_OPTION_INFO *ReplyInfo,
336 IN MTFTP6_EXT_OPTION_INFO *RequestInfo
337 )
338 {
339 //
340 // It is invalid for server to return options we don't request
341 //
342 if ((ReplyInfo->BitMap & ~RequestInfo->BitMap) != 0) {
343 return FALSE;
344 }
345
346 //
347 // Server can only specify a smaller block size and windowsize to be used and
348 // return the timeout matches that requested.
349 //
350 if ((((ReplyInfo->BitMap & MTFTP6_OPT_BLKSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) ||
351 (((ReplyInfo->BitMap & MTFTP6_OPT_WINDOWSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) ||
352 (((ReplyInfo->BitMap & MTFTP6_OPT_TIMEOUT_BIT) != 0) && (ReplyInfo->Timeout != RequestInfo->Timeout))
353 ) {
354 return FALSE;
355 }
356
357 //
358 // The server can send ",,master" to client to change its master
359 // setting. But if it use the specific multicast channel, it can't
360 // change the setting.
361 //
362 if (((ReplyInfo->BitMap & MTFTP6_OPT_MCAST_BIT) != 0) && !NetIp6IsUnspecifiedAddr (&Instance->McastIp)) {
363
364 if (!NetIp6IsUnspecifiedAddr (&ReplyInfo->McastIp) && CompareMem (
365 &ReplyInfo->McastIp,
366 &Instance->McastIp,
367 sizeof (EFI_IPv6_ADDRESS)
368 ) != 0) {
369 return FALSE;
370 }
371
372 if ((ReplyInfo->McastPort != 0) && (ReplyInfo->McastPort != Instance->McastPort)) {
373 return FALSE;
374 }
375 }
376
377 return TRUE;
378 }
379
380
381 /**
382 Configure Udp6Io to receive a packet from a multicast address.
383
384 @param[in] McastIo The pointer to the mcast Udp6Io.
385 @param[in] Context The pointer to the context.
386
387 @retval EFI_SUCCESS The mcast Udp6Io was successfully configured.
388 @retval Others Failed to configure the Udp6Io.
389
390 **/
391 EFI_STATUS
392 EFIAPI
393 Mtftp6RrqConfigMcastUdpIo (
394 IN UDP_IO *McastIo,
395 IN VOID *Context
396 )
397 {
398 EFI_STATUS Status;
399 EFI_UDP6_PROTOCOL *Udp6;
400 EFI_UDP6_CONFIG_DATA *Udp6Cfg;
401 EFI_IPv6_ADDRESS Group;
402 MTFTP6_INSTANCE *Instance;
403
404 Udp6 = McastIo->Protocol.Udp6;
405 Udp6Cfg = &(McastIo->Config.Udp6);
406 Instance = (MTFTP6_INSTANCE *) Context;
407
408 //
409 // Set the configure data for the mcast Udp6Io.
410 //
411 ZeroMem (Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA));
412
413 Udp6Cfg->AcceptPromiscuous = FALSE;
414 Udp6Cfg->AcceptAnyPort = FALSE;
415 Udp6Cfg->AllowDuplicatePort = FALSE;
416 Udp6Cfg->TrafficClass = 0;
417 Udp6Cfg->HopLimit = 128;
418 Udp6Cfg->ReceiveTimeout = 0;
419 Udp6Cfg->TransmitTimeout = 0;
420 Udp6Cfg->StationPort = Instance->McastPort;
421 Udp6Cfg->RemotePort = 0;
422
423 CopyMem (
424 &Udp6Cfg->RemoteAddress,
425 &Instance->ServerIp,
426 sizeof (EFI_IPv6_ADDRESS)
427 );
428
429 //
430 // Configure the mcast Udp6Io.
431 //
432 Status = Udp6->Configure (Udp6, Udp6Cfg);
433
434 if (EFI_ERROR (Status)) {
435 return Status;
436 }
437
438 //
439 // Join the multicast group
440 //
441 CopyMem (&Group, &Instance->McastIp, sizeof (EFI_IPv6_ADDRESS));
442
443 return Udp6->Groups (Udp6, TRUE, &Group);
444 }
445
446
447 /**
448 Process the OACK packet for Rrq.
449
450 @param[in] Instance The pointer to the Mtftp6 instance.
451 @param[in] Packet The pointer to the received packet.
452 @param[in] Len The length of the packet.
453 @param[out] UdpPacket The net buf of received packet.
454 @param[out] IsCompleted If TRUE, the download has been completed.
455 Otherwise, the download has not been completed.
456
457 @retval EFI_DEVICE_ERROR Failed to create/start a multicast Udp6 child.
458 @retval EFI_TFTP_ERROR An error happened during the process.
459 @retval EFI_SUCCESS The OACK packet successfully processed.
460
461 **/
462 EFI_STATUS
463 Mtftp6RrqHandleOack (
464 IN MTFTP6_INSTANCE *Instance,
465 IN EFI_MTFTP6_PACKET *Packet,
466 IN UINT32 Len,
467 OUT NET_BUF **UdpPacket,
468 OUT BOOLEAN *IsCompleted
469 )
470 {
471 EFI_MTFTP6_OPTION *Options;
472 UINT32 Count;
473 MTFTP6_EXT_OPTION_INFO ExtInfo;
474 EFI_STATUS Status;
475 INTN Expected;
476 EFI_UDP6_PROTOCOL *Udp6;
477
478 *IsCompleted = FALSE;
479 Options = NULL;
480
481 //
482 // If already started the master download, don't change the
483 // setting. Master download always succeeds.
484 //
485 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);
486 ASSERT (Expected != -1);
487
488 if (Instance->IsMaster && Expected != 1) {
489 return EFI_SUCCESS;
490 }
491
492 ZeroMem (&ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));
493
494 //
495 // Parse the options in the packet.
496 //
497 Status = Mtftp6ParseStart (Packet, Len, &Count, &Options);
498
499 if (EFI_ERROR (Status)) {
500 return Status;
501 }
502 ASSERT (Options != NULL);
503
504 //
505 // Parse the extensive options in the packet.
506 //
507 Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, Instance->Operation, &ExtInfo);
508
509 if (EFI_ERROR (Status) || !Mtftp6RrqOackValid (Instance, &ExtInfo, &Instance->ExtInfo)) {
510 //
511 // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.
512 //
513 if (Status != EFI_OUT_OF_RESOURCES) {
514 //
515 // Free the received packet before send new packet in ReceiveNotify,
516 // since the udpio might need to be reconfigured.
517 //
518 NetbufFree (*UdpPacket);
519 *UdpPacket = NULL;
520 //
521 // Send the Mtftp6 error message if invalid packet.
522 //
523 Mtftp6SendError (
524 Instance,
525 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,
526 (UINT8 *) "Mal-formated OACK packet"
527 );
528 }
529
530 return EFI_TFTP_ERROR;
531 }
532
533 if ((ExtInfo.BitMap & MTFTP6_OPT_MCAST_BIT) != 0) {
534
535 //
536 // Save the multicast info. Always update the Master, only update the
537 // multicast IP address, block size, window size, timeoute at the first time. If IP
538 // address is updated, create a UDP child to receive the multicast.
539 //
540 Instance->IsMaster = ExtInfo.IsMaster;
541
542 if (NetIp6IsUnspecifiedAddr (&Instance->McastIp)) {
543 if (NetIp6IsUnspecifiedAddr (&ExtInfo.McastIp) || ExtInfo.McastPort == 0) {
544 //
545 // Free the received packet before send new packet in ReceiveNotify,
546 // since the udpio might need to be reconfigured.
547 //
548 NetbufFree (*UdpPacket);
549 *UdpPacket = NULL;
550 //
551 // Send the Mtftp6 error message if invalid multi-cast setting.
552 //
553 Mtftp6SendError (
554 Instance,
555 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,
556 (UINT8 *) "Illegal multicast setting"
557 );
558
559 return EFI_TFTP_ERROR;
560 }
561
562 //
563 // Create a UDP child then start receive the multicast from it.
564 //
565 CopyMem (
566 &Instance->McastIp,
567 &ExtInfo.McastIp,
568 sizeof (EFI_IP_ADDRESS)
569 );
570
571 Instance->McastPort = ExtInfo.McastPort;
572 if (Instance->McastUdpIo == NULL) {
573 Instance->McastUdpIo = UdpIoCreateIo (
574 Instance->Service->Controller,
575 Instance->Service->Image,
576 Mtftp6RrqConfigMcastUdpIo,
577 UDP_IO_UDP6_VERSION,
578 Instance
579 );
580 if (Instance->McastUdpIo != NULL) {
581 Status = gBS->OpenProtocol (
582 Instance->McastUdpIo->UdpHandle,
583 &gEfiUdp6ProtocolGuid,
584 (VOID **) &Udp6,
585 Instance->Service->Image,
586 Instance->Handle,
587 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
588 );
589 if (EFI_ERROR (Status)) {
590 UdpIoFreeIo (Instance->McastUdpIo);
591 Instance->McastUdpIo = NULL;
592 return EFI_DEVICE_ERROR;
593 }
594 }
595 }
596
597 if (Instance->McastUdpIo == NULL) {
598 return EFI_DEVICE_ERROR;
599 }
600
601 Status = UdpIoRecvDatagram (
602 Instance->McastUdpIo,
603 Mtftp6RrqInput,
604 Instance,
605 0
606 );
607
608 if (EFI_ERROR (Status)) {
609 //
610 // Free the received packet before send new packet in ReceiveNotify,
611 // since the udpio might need to be reconfigured.
612 //
613 NetbufFree (*UdpPacket);
614 *UdpPacket = NULL;
615 //
616 // Send the Mtftp6 error message if failed to create Udp6Io to receive.
617 //
618 Mtftp6SendError (
619 Instance,
620 EFI_MTFTP6_ERRORCODE_ACCESS_VIOLATION,
621 (UINT8 *) "Failed to create socket to receive multicast packet"
622 );
623
624 return Status;
625 }
626
627 //
628 // Update the parameters used.
629 //
630 if (ExtInfo.BlkSize != 0) {
631 Instance->BlkSize = ExtInfo.BlkSize;
632 }
633
634 if (ExtInfo.WindowSize != 0) {
635 Instance->WindowSize = ExtInfo.WindowSize;
636 }
637
638 if (ExtInfo.Timeout != 0) {
639 Instance->Timeout = ExtInfo.Timeout;
640 }
641 }
642
643 } else {
644
645 Instance->IsMaster = TRUE;
646
647 if (ExtInfo.BlkSize != 0) {
648 Instance->BlkSize = ExtInfo.BlkSize;
649 }
650
651 if (ExtInfo.WindowSize != 0) {
652 Instance->WindowSize = ExtInfo.WindowSize;
653 }
654
655 if (ExtInfo.Timeout != 0) {
656 Instance->Timeout = ExtInfo.Timeout;
657 }
658 }
659
660 //
661 // Free the received packet before send new packet in ReceiveNotify,
662 // since the udpio might need to be reconfigured.
663 //
664 NetbufFree (*UdpPacket);
665 *UdpPacket = NULL;
666 //
667 // Send an ACK to (Expected - 1) which is 0 for unicast download,
668 // or tell the server we want to receive the Expected block.
669 //
670 return Mtftp6RrqSendAck (Instance, (UINT16) (Expected - 1));
671 }
672
673
674 /**
675 The packet process callback for Mtftp6 download.
676
677 @param[in] UdpPacket The pointer to the packet received.
678 @param[in] UdpEpt The pointer to the Udp6 access point.
679 @param[in] IoStatus The status from Udp6 instance.
680 @param[in] Context The pointer to the context.
681
682 **/
683 VOID
684 EFIAPI
685 Mtftp6RrqInput (
686 IN NET_BUF *UdpPacket,
687 IN UDP_END_POINT *UdpEpt,
688 IN EFI_STATUS IoStatus,
689 IN VOID *Context
690 )
691 {
692 MTFTP6_INSTANCE *Instance;
693 EFI_MTFTP6_PACKET *Packet;
694 BOOLEAN IsCompleted;
695 BOOLEAN IsMcast;
696 EFI_STATUS Status;
697 UINT16 Opcode;
698 UINT32 TotalNum;
699 UINT32 Len;
700
701 Instance = (MTFTP6_INSTANCE *) Context;
702
703 NET_CHECK_SIGNATURE (Instance, MTFTP6_INSTANCE_SIGNATURE);
704
705 Status = EFI_SUCCESS;
706 Packet = NULL;
707 IsCompleted = FALSE;
708 IsMcast = FALSE;
709 TotalNum = 0;
710
711 //
712 // Return error status if Udp6 instance failed to receive.
713 //
714 if (EFI_ERROR (IoStatus)) {
715 Status = IoStatus;
716 goto ON_EXIT;
717 }
718
719 ASSERT (UdpPacket != NULL);
720
721 if (UdpPacket->TotalSize < MTFTP6_OPCODE_LEN) {
722 goto ON_EXIT;
723 }
724
725 //
726 // Find the port this packet is from to restart receive correctly.
727 //
728 if (CompareMem (
729 Ip6Swap128 (&UdpEpt->LocalAddr.v6),
730 &Instance->McastIp,
731 sizeof (EFI_IPv6_ADDRESS)
732 ) == 0) {
733 IsMcast = TRUE;
734 } else {
735 IsMcast = FALSE;
736 }
737
738 //
739 // Client send initial request to server's listening port. Server
740 // will select a UDP port to communicate with the client. The server
741 // is required to use the same port as RemotePort to multicast the
742 // data.
743 //
744 if (UdpEpt->RemotePort != Instance->ServerDataPort) {
745 if (Instance->ServerDataPort != 0) {
746 goto ON_EXIT;
747 } else {
748 //
749 // For the subsequent exchange of requests, reconfigure the udpio as
750 // (serverip, serverport, localip, localport).
751 // Ususally, the client set serverport as 0 to receive and reset it
752 // once the first packet arrives to send ack.
753 //
754 Instance->ServerDataPort = UdpEpt->RemotePort;
755 }
756 }
757
758 //
759 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
760 //
761 Len = UdpPacket->TotalSize;
762 TotalNum = UdpPacket->BlockOpNum;
763
764 if (TotalNum > 1) {
765 Packet = AllocateZeroPool (Len);
766
767 if (Packet == NULL) {
768 Status = EFI_OUT_OF_RESOURCES;
769 goto ON_EXIT;
770 }
771
772 NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet);
773
774 } else {
775 Packet = (EFI_MTFTP6_PACKET *) NetbufGetByte (UdpPacket, 0, NULL);
776 ASSERT (Packet != NULL);
777 }
778
779 Opcode = NTOHS (Packet->OpCode);
780
781 //
782 // Callback to the user's CheckPacket if provided. Abort the transmission
783 // if CheckPacket returns an EFI_ERROR code.
784 //
785 if ((Instance->Token->CheckPacket != NULL) &&
786 (Opcode == EFI_MTFTP6_OPCODE_OACK || Opcode == EFI_MTFTP6_OPCODE_ERROR)
787 ) {
788
789 Status = Instance->Token->CheckPacket (
790 &Instance->Mtftp6,
791 Instance->Token,
792 (UINT16) Len,
793 Packet
794 );
795
796 if (EFI_ERROR (Status)) {
797 //
798 // Send an error message to the server to inform it
799 //
800 if (Opcode != EFI_MTFTP6_OPCODE_ERROR) {
801 //
802 // Free the received packet before send new packet in ReceiveNotify,
803 // since the udpio might need to be reconfigured.
804 //
805 NetbufFree (UdpPacket);
806 UdpPacket = NULL;
807 //
808 // Send the Mtftp6 error message if user aborted the current session.
809 //
810 Mtftp6SendError (
811 Instance,
812 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,
813 (UINT8 *) "User aborted the transfer"
814 );
815 }
816
817 Status = EFI_ABORTED;
818 goto ON_EXIT;
819 }
820 }
821
822 //
823 // Switch the process routines by the operation code.
824 //
825 switch (Opcode) {
826 case EFI_MTFTP6_OPCODE_DATA:
827 if ((Len > (UINT32) (MTFTP6_DATA_HEAD_LEN + Instance->BlkSize)) || (Len < (UINT32) MTFTP6_DATA_HEAD_LEN)) {
828 goto ON_EXIT;
829 }
830 //
831 // Handle the data packet of Rrq.
832 //
833 Status = Mtftp6RrqHandleData (
834 Instance,
835 Packet,
836 Len,
837 &UdpPacket,
838 &IsCompleted
839 );
840 break;
841
842 case EFI_MTFTP6_OPCODE_OACK:
843 if (IsMcast || Len <= MTFTP6_OPCODE_LEN) {
844 goto ON_EXIT;
845 }
846 //
847 // Handle the Oack packet of Rrq.
848 //
849 Status = Mtftp6RrqHandleOack (
850 Instance,
851 Packet,
852 Len,
853 &UdpPacket,
854 &IsCompleted
855 );
856 break;
857
858 default:
859 //
860 // Drop and return eror if received error message.
861 //
862 Status = EFI_TFTP_ERROR;
863 break;
864 }
865
866 ON_EXIT:
867 //
868 // Free the resources, then if !EFI_ERROR (Status), restart the
869 // receive, otherwise end the session.
870 //
871 if (Packet != NULL && TotalNum > 1) {
872 FreePool (Packet);
873 }
874 if (UdpPacket != NULL) {
875 NetbufFree (UdpPacket);
876 }
877 if (!EFI_ERROR (Status) && !IsCompleted) {
878 if (IsMcast) {
879 Status = UdpIoRecvDatagram (
880 Instance->McastUdpIo,
881 Mtftp6RrqInput,
882 Instance,
883 0
884 );
885 } else {
886 Status = UdpIoRecvDatagram (
887 Instance->UdpIo,
888 Mtftp6RrqInput,
889 Instance,
890 0
891 );
892 }
893 }
894 //
895 // Clean up the current session if failed to continue.
896 //
897 if (EFI_ERROR (Status) || IsCompleted) {
898 Mtftp6OperationClean (Instance, Status);
899 }
900 }
901
902
903 /**
904 Start the Mtftp6 instance to download. It first initializes some
905 of the internal states, then builds and sends an RRQ reqeuest packet.
906 Finally, it starts receive for the downloading.
907
908 @param[in] Instance The pointer to the Mtftp6 instance.
909 @param[in] Operation The operation code of current packet.
910
911 @retval EFI_SUCCESS The Mtftp6 is started to download.
912 @retval Others Failed to start to download.
913
914 **/
915 EFI_STATUS
916 Mtftp6RrqStart (
917 IN MTFTP6_INSTANCE *Instance,
918 IN UINT16 Operation
919 )
920 {
921 EFI_STATUS Status;
922
923 //
924 // The valid block number range are [1, 0xffff]. For example:
925 // the client sends an RRQ request to the server, the server
926 // transfers the DATA1 block. If option negoitation is ongoing,
927 // the server will send back an OACK, then client will send ACK0.
928 //
929 Status = Mtftp6InitBlockRange (&Instance->BlkList, 1, 0xffff);
930
931 if (EFI_ERROR (Status)) {
932 return Status;
933 }
934
935 Status = Mtftp6SendRequest (Instance, Operation);
936
937 if (EFI_ERROR (Status)) {
938 return Status;
939 }
940
941 return UdpIoRecvDatagram (
942 Instance->UdpIo,
943 Mtftp6RrqInput,
944 Instance,
945 0
946 );
947 }
948