2 Mtftp6 Rrq process functions implementation.
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
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.
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.
16 #include "Mtftp6Impl.h"
20 Build and send a ACK packet for download.
22 @param[in] Instance The pointer to the Mtftp6 instance.
23 @param[in] BlockNum The block number to be acked.
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.
32 IN MTFTP6_INSTANCE
*Instance
,
36 EFI_MTFTP6_PACKET
*Ack
;
40 // Allocate net buffer to create ack packet.
42 Packet
= NetbufAlloc (sizeof (EFI_MTFTP6_ACK_HEADER
));
45 return EFI_OUT_OF_RESOURCES
;
48 Ack
= (EFI_MTFTP6_PACKET
*) NetbufAllocSpace (
50 sizeof (EFI_MTFTP6_ACK_HEADER
),
55 Ack
->Ack
.OpCode
= HTONS (EFI_MTFTP6_OPCODE_ACK
);
56 Ack
->Ack
.Block
[0] = HTONS (BlockNum
);
59 // Reset current retry count of the instance.
61 Instance
->CurRetry
= 0;
63 return Mtftp6TransmitPacket (Instance
, Packet
);
68 Deliver the received data block to the user, which can be saved
69 in the user provide buffer or through the CheckPacket callback.
71 @param[in] Instance The pointer to the Mtftp6 instance.
72 @param[in] Packet The pointer to the received packet.
73 @param[in] Len The packet length.
74 @param[out] UdpPacket The net buf of the received packet.
76 @retval EFI_SUCCESS The data was saved successfully.
77 @retval EFI_ABORTED The user tells to abort by return an error through
79 @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small, and buffer length is
80 updated to the actual buffer size needed.
85 IN MTFTP6_INSTANCE
*Instance
,
86 IN EFI_MTFTP6_PACKET
*Packet
,
88 OUT NET_BUF
**UdpPacket
91 EFI_MTFTP6_TOKEN
*Token
;
100 Token
= Instance
->Token
;
101 Block
= NTOHS (Packet
->Data
.Block
);
102 DataLen
= Len
- MTFTP6_DATA_HEAD_LEN
;
105 // This is the last block, save the block num
107 if (DataLen
< Instance
->BlkSize
) {
109 Instance
->LastBlk
= Block
;
110 Mtftp6SetLastBlockNum (&Instance
->BlkList
, Block
);
114 // Remove this block number from the file hole. If Mtftp6RemoveBlockNum
115 // returns EFI_NOT_FOUND, the block has been saved, don't save it again.
116 // Note that : For bigger files, allowing the block counter to roll over
117 // to accept transfers of unlimited size. So TotalBlock is memorised as
118 // continuous block counter.
120 Status
= Mtftp6RemoveBlockNum (&Instance
->BlkList
, Block
, Completed
, &TotalBlock
);
122 if (Status
== EFI_NOT_FOUND
) {
124 } else if (EFI_ERROR (Status
)) {
128 if (Token
->CheckPacket
!= NULL
) {
130 // Callback to the check packet routine with the received packet.
132 Status
= Token
->CheckPacket (&Instance
->Mtftp6
, Token
, (UINT16
) Len
, Packet
);
134 if (EFI_ERROR (Status
)) {
136 // Free the received packet before send new packet in ReceiveNotify,
137 // since the Udp6Io might need to be reconfigured.
139 NetbufFree (*UdpPacket
);
142 // Send the Mtftp6 error message if user aborted the current session.
146 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
147 (UINT8
*) "User aborted download"
154 if (Token
->Buffer
!= NULL
) {
156 Start
= MultU64x32 (TotalBlock
- 1, Instance
->BlkSize
);
157 if (Start
+ DataLen
<= Token
->BufferSize
) {
158 CopyMem ((UINT8
*) Token
->Buffer
+ Start
, Packet
->Data
.Data
, DataLen
);
160 // Update the file size when received the last block
162 if ((Instance
->LastBlk
== Block
) && Completed
) {
163 Token
->BufferSize
= Start
+ DataLen
;
165 } else if (Instance
->LastBlk
!= 0) {
167 // Don't save the data if the buffer is too small, return
168 // EFI_BUFFER_TOO_SMALL if received the last packet. This
169 // will give a accurate file length.
171 Token
->BufferSize
= Start
+ DataLen
;
174 // Free the received packet before send new packet in ReceiveNotify,
175 // since the udpio might need to be reconfigured.
177 NetbufFree (*UdpPacket
);
180 // Send the Mtftp6 error message if no enough buffer.
184 EFI_MTFTP6_ERRORCODE_DISK_FULL
,
185 (UINT8
*) "User provided memory block is too small"
188 return EFI_BUFFER_TOO_SMALL
;
197 Process the received data packets. It will save the block
198 then send back an ACK if it is active.
200 @param[in] Instance The pointer to the Mtftp6 instance.
201 @param[in] Packet The pointer to the received packet.
202 @param[in] Len The length of the packet.
203 @param[out] UdpPacket The net buf of received packet.
204 @param[out] IsCompleted If TRUE, the download has been completed.
205 Otherwise, the download has not been completed.
207 @retval EFI_SUCCESS The data packet was successfully processed.
208 @retval EFI_ABORTED The download was aborted by the user.
209 @retval EFI_BUFFER_TOO_SMALL The user-provided buffer is too small.
213 Mtftp6RrqHandleData (
214 IN MTFTP6_INSTANCE
*Instance
,
215 IN EFI_MTFTP6_PACKET
*Packet
,
217 OUT NET_BUF
**UdpPacket
,
218 OUT BOOLEAN
*IsCompleted
225 *IsCompleted
= FALSE
;
226 BlockNum
= NTOHS (Packet
->Data
.Block
);
227 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
229 ASSERT (Expected
>= 0);
232 // If we are active and received an unexpected packet, retransmit
233 // the last ACK then restart receiving. If we are passive, save
236 if (Instance
->IsMaster
&& (Expected
!= BlockNum
)) {
238 // Free the received packet before send new packet in ReceiveNotify,
239 // since the udpio might need to be reconfigured.
241 NetbufFree (*UdpPacket
);
244 Mtftp6TransmitPacket (Instance
, Instance
->LastPacket
);
248 Status
= Mtftp6RrqSaveBlock (Instance
, Packet
, Len
, UdpPacket
);
250 if (EFI_ERROR (Status
)) {
255 // Reset the passive client's timer whenever it received a valid data packet.
257 if (!Instance
->IsMaster
) {
258 Instance
->PacketToLive
= Instance
->Timeout
* 2;
262 // Check whether we have received all the blocks. Send the ACK if we
263 // are active (unicast client or master client for multicast download).
264 // If we have received all the blocks, send an ACK even if we are passive
265 // to tell the server that we are done.
267 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
269 if (Instance
->IsMaster
|| Expected
< 0) {
272 // If we are passive client, then the just received Block maybe
273 // isn't the last block. We need to send an ACK to the last block
274 // to inform the server that we are done. If we are active client,
275 // the Block == Instance->LastBlock.
277 BlockNum
= Instance
->LastBlk
;
281 BlockNum
= (UINT16
) (Expected
- 1);
284 // Free the received packet before send new packet in ReceiveNotify,
285 // since the udpio might need to be reconfigured.
287 NetbufFree (*UdpPacket
);
290 Mtftp6RrqSendAck (Instance
, BlockNum
);
298 Validate whether the options received in the server's OACK packet is valid.
299 The options are valid only if:
300 1. The server doesn't include options not requested by us.
301 2. The server can only use smaller blksize than that is requested.
302 3. The server can only use the same timeout as requested.
303 4. The server doesn't change its multicast channel.
305 @param[in] Instance The pointer to the Mtftp6 instance.
306 @param[in] ReplyInfo The pointer to options information in reply packet.
307 @param[in] RequestInfo The pointer to requested options info.
309 @retval TRUE If the option in the OACK is valid.
310 @retval FALSE If the option is invalid.
315 IN MTFTP6_INSTANCE
*Instance
,
316 IN MTFTP6_EXT_OPTION_INFO
*ReplyInfo
,
317 IN MTFTP6_EXT_OPTION_INFO
*RequestInfo
321 // It is invalid for server to return options we don't request
323 if ((ReplyInfo
->BitMap
& ~RequestInfo
->BitMap
) != 0) {
328 // Server can only specify a smaller block size to be used and
329 // return the timeout matches that requested.
331 if ((((ReplyInfo
->BitMap
& MTFTP6_OPT_BLKSIZE_BIT
) != 0) && (ReplyInfo
->BlkSize
> RequestInfo
->BlkSize
)) ||
332 (((ReplyInfo
->BitMap
& MTFTP6_OPT_TIMEOUT_BIT
) != 0) && (ReplyInfo
->Timeout
!= RequestInfo
->Timeout
))
338 // The server can send ",,master" to client to change its master
339 // setting. But if it use the specific multicast channel, it can't
340 // change the setting.
342 if (((ReplyInfo
->BitMap
& MTFTP6_OPT_MCAST_BIT
) != 0) && !NetIp6IsUnspecifiedAddr (&Instance
->McastIp
)) {
344 if (!NetIp6IsUnspecifiedAddr (&ReplyInfo
->McastIp
) && CompareMem (
347 sizeof (EFI_IPv6_ADDRESS
)
352 if ((ReplyInfo
->McastPort
!= 0) && (ReplyInfo
->McastPort
!= Instance
->McastPort
)) {
362 Configure Udp6Io to receive a packet from a multicast address.
364 @param[in] McastIo The pointer to the mcast Udp6Io.
365 @param[in] Context The pointer to the context.
367 @retval EFI_SUCCESS The mcast Udp6Io was successfully configured.
368 @retval Others Failed to configure the Udp6Io.
373 Mtftp6RrqConfigMcastUdpIo (
379 EFI_UDP6_PROTOCOL
*Udp6
;
380 EFI_UDP6_CONFIG_DATA
*Udp6Cfg
;
381 EFI_IPv6_ADDRESS Group
;
382 MTFTP6_INSTANCE
*Instance
;
384 Udp6
= McastIo
->Protocol
.Udp6
;
385 Udp6Cfg
= &(McastIo
->Config
.Udp6
);
386 Instance
= (MTFTP6_INSTANCE
*) Context
;
389 // Set the configure data for the mcast Udp6Io.
391 ZeroMem (Udp6Cfg
, sizeof (EFI_UDP6_CONFIG_DATA
));
393 Udp6Cfg
->AcceptPromiscuous
= FALSE
;
394 Udp6Cfg
->AcceptAnyPort
= FALSE
;
395 Udp6Cfg
->AllowDuplicatePort
= FALSE
;
396 Udp6Cfg
->TrafficClass
= 0;
397 Udp6Cfg
->HopLimit
= 128;
398 Udp6Cfg
->ReceiveTimeout
= 0;
399 Udp6Cfg
->TransmitTimeout
= 0;
400 Udp6Cfg
->StationPort
= Instance
->McastPort
;
401 Udp6Cfg
->RemotePort
= 0;
404 &Udp6Cfg
->RemoteAddress
,
406 sizeof (EFI_IPv6_ADDRESS
)
410 // Configure the mcast Udp6Io.
412 Status
= Udp6
->Configure (Udp6
, Udp6Cfg
);
414 if (EFI_ERROR (Status
)) {
419 // Join the multicast group
421 CopyMem (&Group
, &Instance
->McastIp
, sizeof (EFI_IPv6_ADDRESS
));
423 return Udp6
->Groups (Udp6
, TRUE
, &Group
);
428 Process the OACK packet for Rrq.
430 @param[in] Instance The pointer to the Mtftp6 instance.
431 @param[in] Packet The pointer to the received packet.
432 @param[in] Len The length of the packet.
433 @param[out] UdpPacket The net buf of received packet.
434 @param[out] IsCompleted If TRUE, the download has been completed.
435 Otherwise, the download has not been completed.
437 @retval EFI_DEVICE_ERROR Failed to create/start a multicast Udp6 child.
438 @retval EFI_TFTP_ERROR An error happened during the process.
439 @retval EFI_SUCCESS The OACK packet successfully processed.
443 Mtftp6RrqHandleOack (
444 IN MTFTP6_INSTANCE
*Instance
,
445 IN EFI_MTFTP6_PACKET
*Packet
,
447 OUT NET_BUF
**UdpPacket
,
448 OUT BOOLEAN
*IsCompleted
451 EFI_MTFTP6_OPTION
*Options
;
453 MTFTP6_EXT_OPTION_INFO ExtInfo
;
457 *IsCompleted
= FALSE
;
460 // If already started the master download, don't change the
461 // setting. Master download always succeeds.
463 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
464 ASSERT (Expected
!= -1);
466 if (Instance
->IsMaster
&& Expected
!= 1) {
470 ZeroMem (&ExtInfo
, sizeof (MTFTP6_EXT_OPTION_INFO
));
473 // Parse the options in the packet.
475 Status
= Mtftp6ParseStart (Packet
, Len
, &Count
, &Options
);
477 if (EFI_ERROR (Status
)) {
480 ASSERT (Options
!= NULL
);
483 // Parse the extensive options in the packet.
485 Status
= Mtftp6ParseExtensionOption (Options
, Count
, FALSE
, &ExtInfo
);
487 if (EFI_ERROR (Status
) || !Mtftp6RrqOackValid (Instance
, &ExtInfo
, &Instance
->ExtInfo
)) {
489 // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.
491 if (Status
!= EFI_OUT_OF_RESOURCES
) {
493 // Free the received packet before send new packet in ReceiveNotify,
494 // since the udpio might need to be reconfigured.
496 NetbufFree (*UdpPacket
);
499 // Send the Mtftp6 error message if invalid packet.
503 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
504 (UINT8
*) "Mal-formated OACK packet"
508 return EFI_TFTP_ERROR
;
511 if ((ExtInfo
.BitMap
& MTFTP6_OPT_MCAST_BIT
) != 0) {
514 // Save the multicast info. Always update the Master, only update the
515 // multicast IP address, block size, timeoute at the first time. If IP
516 // address is updated, create a UDP child to receive the multicast.
518 Instance
->IsMaster
= ExtInfo
.IsMaster
;
520 if (NetIp6IsUnspecifiedAddr (&Instance
->McastIp
)) {
521 if (NetIp6IsUnspecifiedAddr (&ExtInfo
.McastIp
) || ExtInfo
.McastPort
== 0) {
523 // Free the received packet before send new packet in ReceiveNotify,
524 // since the udpio might need to be reconfigured.
526 NetbufFree (*UdpPacket
);
529 // Send the Mtftp6 error message if invalid multi-cast setting.
533 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
534 (UINT8
*) "Illegal multicast setting"
537 return EFI_TFTP_ERROR
;
541 // Create a UDP child then start receive the multicast from it.
546 sizeof (EFI_IP_ADDRESS
)
549 Instance
->McastPort
= ExtInfo
.McastPort
;
550 if (Instance
->McastUdpIo
== NULL
) {
551 Instance
->McastUdpIo
= UdpIoCreateIo (
552 Instance
->Service
->Controller
,
553 Instance
->Service
->Image
,
554 Mtftp6RrqConfigMcastUdpIo
,
560 if (Instance
->McastUdpIo
== NULL
) {
561 return EFI_DEVICE_ERROR
;
564 Status
= UdpIoRecvDatagram (
565 Instance
->McastUdpIo
,
571 if (EFI_ERROR (Status
)) {
573 // Free the received packet before send new packet in ReceiveNotify,
574 // since the udpio might need to be reconfigured.
576 NetbufFree (*UdpPacket
);
579 // Send the Mtftp6 error message if failed to create Udp6Io to receive.
583 EFI_MTFTP6_ERRORCODE_ACCESS_VIOLATION
,
584 (UINT8
*) "Failed to create socket to receive multicast packet"
591 // Update the parameters used.
593 if (ExtInfo
.BlkSize
!= 0) {
594 Instance
->BlkSize
= ExtInfo
.BlkSize
;
597 if (ExtInfo
.Timeout
!= 0) {
598 Instance
->Timeout
= ExtInfo
.Timeout
;
604 Instance
->IsMaster
= TRUE
;
606 if (ExtInfo
.BlkSize
!= 0) {
607 Instance
->BlkSize
= ExtInfo
.BlkSize
;
610 if (ExtInfo
.Timeout
!= 0) {
611 Instance
->Timeout
= ExtInfo
.Timeout
;
616 // Free the received packet before send new packet in ReceiveNotify,
617 // since the udpio might need to be reconfigured.
619 NetbufFree (*UdpPacket
);
622 // Send an ACK to (Expected - 1) which is 0 for unicast download,
623 // or tell the server we want to receive the Expected block.
625 return Mtftp6RrqSendAck (Instance
, (UINT16
) (Expected
- 1));
630 The packet process callback for Mtftp6 download.
632 @param[in] UdpPacket The pointer to the packet received.
633 @param[in] UdpEpt The pointer to the Udp6 access point.
634 @param[in] IoStatus The status from Udp6 instance.
635 @param[in] Context The pointer to the context.
641 IN NET_BUF
*UdpPacket
,
642 IN UDP_END_POINT
*UdpEpt
,
643 IN EFI_STATUS IoStatus
,
647 MTFTP6_INSTANCE
*Instance
;
648 EFI_MTFTP6_PACKET
*Packet
;
656 Instance
= (MTFTP6_INSTANCE
*) Context
;
658 NET_CHECK_SIGNATURE (Instance
, MTFTP6_INSTANCE_SIGNATURE
);
660 Status
= EFI_SUCCESS
;
667 // Return error status if Udp6 instance failed to receive.
669 if (EFI_ERROR (IoStatus
)) {
674 ASSERT (UdpPacket
!= NULL
);
676 if (UdpPacket
->TotalSize
< MTFTP6_OPCODE_LEN
) {
681 // Find the port this packet is from to restart receive correctly.
684 Ip6Swap128 (&UdpEpt
->LocalAddr
.v6
),
686 sizeof (EFI_IPv6_ADDRESS
)
694 // Client send initial request to server's listening port. Server
695 // will select a UDP port to communicate with the client. The server
696 // is required to use the same port as RemotePort to multicast the
699 if (UdpEpt
->RemotePort
!= Instance
->ServerDataPort
) {
700 if (Instance
->ServerDataPort
!= 0) {
704 // For the subsequent exchange of requests, reconfigure the udpio as
705 // (serverip, serverport, localip, localport).
706 // Ususally, the client set serverport as 0 to receive and reset it
707 // once the first packet arrives to send ack.
709 Instance
->ServerDataPort
= UdpEpt
->RemotePort
;
714 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
716 Len
= UdpPacket
->TotalSize
;
717 TotalNum
= UdpPacket
->BlockOpNum
;
720 Packet
= AllocateZeroPool (Len
);
722 if (Packet
== NULL
) {
723 Status
= EFI_OUT_OF_RESOURCES
;
727 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
730 Packet
= (EFI_MTFTP6_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
731 ASSERT (Packet
!= NULL
);
734 Opcode
= NTOHS (Packet
->OpCode
);
737 // Callback to the user's CheckPacket if provided. Abort the transmission
738 // if CheckPacket returns an EFI_ERROR code.
740 if ((Instance
->Token
->CheckPacket
!= NULL
) &&
741 (Opcode
== EFI_MTFTP6_OPCODE_OACK
|| Opcode
== EFI_MTFTP6_OPCODE_ERROR
)
744 Status
= Instance
->Token
->CheckPacket (
751 if (EFI_ERROR (Status
)) {
753 // Send an error message to the server to inform it
755 if (Opcode
!= EFI_MTFTP6_OPCODE_ERROR
) {
757 // Free the received packet before send new packet in ReceiveNotify,
758 // since the udpio might need to be reconfigured.
760 NetbufFree (UdpPacket
);
763 // Send the Mtftp6 error message if user aborted the current session.
767 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
768 (UINT8
*) "User aborted the transfer"
772 Status
= EFI_ABORTED
;
778 // Switch the process routines by the operation code.
781 case EFI_MTFTP6_OPCODE_DATA
:
782 if ((Len
> (UINT32
) (MTFTP6_DATA_HEAD_LEN
+ Instance
->BlkSize
)) || (Len
< (UINT32
) MTFTP6_DATA_HEAD_LEN
)) {
786 // Handle the data packet of Rrq.
788 Status
= Mtftp6RrqHandleData (
797 case EFI_MTFTP6_OPCODE_OACK
:
798 if (IsMcast
|| Len
<= MTFTP6_OPCODE_LEN
) {
802 // Handle the Oack packet of Rrq.
804 Status
= Mtftp6RrqHandleOack (
815 // Drop and return eror if received error message.
817 Status
= EFI_TFTP_ERROR
;
823 // Free the resources, then if !EFI_ERROR (Status), restart the
824 // receive, otherwise end the session.
826 if (Packet
!= NULL
&& TotalNum
> 1) {
829 if (UdpPacket
!= NULL
) {
830 NetbufFree (UdpPacket
);
832 if (!EFI_ERROR (Status
) && !IsCompleted
) {
834 Status
= UdpIoRecvDatagram (
835 Instance
->McastUdpIo
,
841 Status
= UdpIoRecvDatagram (
850 // Clean up the current session if failed to continue.
852 if (EFI_ERROR (Status
) || IsCompleted
) {
853 Mtftp6OperationClean (Instance
, Status
);
859 Start the Mtftp6 instance to download. It first initializes some
860 of the internal states, then builds and sends an RRQ reqeuest packet.
861 Finally, it starts receive for the downloading.
863 @param[in] Instance The pointer to the Mtftp6 instance.
864 @param[in] Operation The operation code of current packet.
866 @retval EFI_SUCCESS The Mtftp6 is started to download.
867 @retval Others Failed to start to download.
872 IN MTFTP6_INSTANCE
*Instance
,
879 // The valid block number range are [1, 0xffff]. For example:
880 // the client sends an RRQ request to the server, the server
881 // transfers the DATA1 block. If option negoitation is ongoing,
882 // the server will send back an OACK, then client will send ACK0.
884 Status
= Mtftp6InitBlockRange (&Instance
->BlkList
, 1, 0xffff);
886 if (EFI_ERROR (Status
)) {
890 Status
= Mtftp6SendRequest (Instance
, Operation
);
892 if (EFI_ERROR (Status
)) {
896 return UdpIoRecvDatagram (