2 Mtftp6 Rrq process functions implementation.
4 Copyright (c) 2009 - 2014, 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;
62 Instance
->LastPacket
= Packet
;
64 return Mtftp6TransmitPacket (Instance
, Packet
);
69 Deliver the received data block to the user, which can be saved
70 in the user provide buffer or through the CheckPacket callback.
72 @param[in] Instance The pointer to the Mtftp6 instance.
73 @param[in] Packet The pointer to the received packet.
74 @param[in] Len The packet length.
75 @param[out] UdpPacket The net buf of the received packet.
77 @retval EFI_SUCCESS The data was saved successfully.
78 @retval EFI_ABORTED The user tells to abort by return an error through
80 @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small, and buffer length is
81 updated to the actual buffer size needed.
86 IN MTFTP6_INSTANCE
*Instance
,
87 IN EFI_MTFTP6_PACKET
*Packet
,
89 OUT NET_BUF
**UdpPacket
92 EFI_MTFTP6_TOKEN
*Token
;
101 Token
= Instance
->Token
;
102 Block
= NTOHS (Packet
->Data
.Block
);
103 DataLen
= Len
- MTFTP6_DATA_HEAD_LEN
;
106 // This is the last block, save the block num
108 if (DataLen
< Instance
->BlkSize
) {
110 Instance
->LastBlk
= Block
;
111 Mtftp6SetLastBlockNum (&Instance
->BlkList
, Block
);
115 // Remove this block number from the file hole. If Mtftp6RemoveBlockNum
116 // returns EFI_NOT_FOUND, the block has been saved, don't save it again.
117 // Note that : For bigger files, allowing the block counter to roll over
118 // to accept transfers of unlimited size. So TotalBlock is memorised as
119 // continuous block counter.
121 Status
= Mtftp6RemoveBlockNum (&Instance
->BlkList
, Block
, Completed
, &TotalBlock
);
123 if (Status
== EFI_NOT_FOUND
) {
125 } else if (EFI_ERROR (Status
)) {
129 if (Token
->CheckPacket
!= NULL
) {
131 // Callback to the check packet routine with the received packet.
133 Status
= Token
->CheckPacket (&Instance
->Mtftp6
, Token
, (UINT16
) Len
, Packet
);
135 if (EFI_ERROR (Status
)) {
137 // Free the received packet before send new packet in ReceiveNotify,
138 // since the Udp6Io might need to be reconfigured.
140 NetbufFree (*UdpPacket
);
143 // Send the Mtftp6 error message if user aborted the current session.
147 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
148 (UINT8
*) "User aborted download"
155 if (Token
->Buffer
!= NULL
) {
157 Start
= MultU64x32 (TotalBlock
- 1, Instance
->BlkSize
);
158 if (Start
+ DataLen
<= Token
->BufferSize
) {
159 CopyMem ((UINT8
*) Token
->Buffer
+ Start
, Packet
->Data
.Data
, DataLen
);
161 // Update the file size when received the last block
163 if ((Instance
->LastBlk
== Block
) && Completed
) {
164 Token
->BufferSize
= Start
+ DataLen
;
166 } else if (Instance
->LastBlk
!= 0) {
168 // Don't save the data if the buffer is too small, return
169 // EFI_BUFFER_TOO_SMALL if received the last packet. This
170 // will give a accurate file length.
172 Token
->BufferSize
= Start
+ DataLen
;
175 // Free the received packet before send new packet in ReceiveNotify,
176 // since the udpio might need to be reconfigured.
178 NetbufFree (*UdpPacket
);
181 // Send the Mtftp6 error message if no enough buffer.
185 EFI_MTFTP6_ERRORCODE_DISK_FULL
,
186 (UINT8
*) "User provided memory block is too small"
189 return EFI_BUFFER_TOO_SMALL
;
198 Process the received data packets. It will save the block
199 then send back an ACK if it is active.
201 @param[in] Instance The pointer to the Mtftp6 instance.
202 @param[in] Packet The pointer to the received packet.
203 @param[in] Len The length of the packet.
204 @param[out] UdpPacket The net buf of received packet.
205 @param[out] IsCompleted If TRUE, the download has been completed.
206 Otherwise, the download has not been completed.
208 @retval EFI_SUCCESS The data packet was successfully processed.
209 @retval EFI_ABORTED The download was aborted by the user.
210 @retval EFI_BUFFER_TOO_SMALL The user-provided buffer is too small.
214 Mtftp6RrqHandleData (
215 IN MTFTP6_INSTANCE
*Instance
,
216 IN EFI_MTFTP6_PACKET
*Packet
,
218 OUT NET_BUF
**UdpPacket
,
219 OUT BOOLEAN
*IsCompleted
226 *IsCompleted
= FALSE
;
227 BlockNum
= NTOHS (Packet
->Data
.Block
);
228 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
230 ASSERT (Expected
>= 0);
233 // If we are active and received an unexpected packet, retransmit
234 // the last ACK then restart receiving. If we are passive, save
237 if (Instance
->IsMaster
&& (Expected
!= BlockNum
)) {
239 // Free the received packet before send new packet in ReceiveNotify,
240 // since the udpio might need to be reconfigured.
242 NetbufFree (*UdpPacket
);
245 Mtftp6TransmitPacket (Instance
, Instance
->LastPacket
);
249 Status
= Mtftp6RrqSaveBlock (Instance
, Packet
, Len
, UdpPacket
);
251 if (EFI_ERROR (Status
)) {
256 // Reset the passive client's timer whenever it received a valid data packet.
258 if (!Instance
->IsMaster
) {
259 Instance
->PacketToLive
= Instance
->Timeout
* 2;
263 // Check whether we have received all the blocks. Send the ACK if we
264 // are active (unicast client or master client for multicast download).
265 // If we have received all the blocks, send an ACK even if we are passive
266 // to tell the server that we are done.
268 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
270 if (Instance
->IsMaster
|| Expected
< 0) {
273 // If we are passive client, then the just received Block maybe
274 // isn't the last block. We need to send an ACK to the last block
275 // to inform the server that we are done. If we are active client,
276 // the Block == Instance->LastBlock.
278 BlockNum
= Instance
->LastBlk
;
282 BlockNum
= (UINT16
) (Expected
- 1);
285 // Free the received packet before send new packet in ReceiveNotify,
286 // since the udpio might need to be reconfigured.
288 NetbufFree (*UdpPacket
);
291 Mtftp6RrqSendAck (Instance
, BlockNum
);
299 Validate whether the options received in the server's OACK packet is valid.
300 The options are valid only if:
301 1. The server doesn't include options not requested by us.
302 2. The server can only use smaller blksize than that is requested.
303 3. The server can only use the same timeout as requested.
304 4. The server doesn't change its multicast channel.
306 @param[in] Instance The pointer to the Mtftp6 instance.
307 @param[in] ReplyInfo The pointer to options information in reply packet.
308 @param[in] RequestInfo The pointer to requested options info.
310 @retval TRUE If the option in the OACK is valid.
311 @retval FALSE If the option is invalid.
316 IN MTFTP6_INSTANCE
*Instance
,
317 IN MTFTP6_EXT_OPTION_INFO
*ReplyInfo
,
318 IN MTFTP6_EXT_OPTION_INFO
*RequestInfo
322 // It is invalid for server to return options we don't request
324 if ((ReplyInfo
->BitMap
& ~RequestInfo
->BitMap
) != 0) {
329 // Server can only specify a smaller block size to be used and
330 // return the timeout matches that requested.
332 if ((((ReplyInfo
->BitMap
& MTFTP6_OPT_BLKSIZE_BIT
) != 0) && (ReplyInfo
->BlkSize
> RequestInfo
->BlkSize
)) ||
333 (((ReplyInfo
->BitMap
& MTFTP6_OPT_TIMEOUT_BIT
) != 0) && (ReplyInfo
->Timeout
!= RequestInfo
->Timeout
))
339 // The server can send ",,master" to client to change its master
340 // setting. But if it use the specific multicast channel, it can't
341 // change the setting.
343 if (((ReplyInfo
->BitMap
& MTFTP6_OPT_MCAST_BIT
) != 0) && !NetIp6IsUnspecifiedAddr (&Instance
->McastIp
)) {
345 if (!NetIp6IsUnspecifiedAddr (&ReplyInfo
->McastIp
) && CompareMem (
348 sizeof (EFI_IPv6_ADDRESS
)
353 if ((ReplyInfo
->McastPort
!= 0) && (ReplyInfo
->McastPort
!= Instance
->McastPort
)) {
363 Configure Udp6Io to receive a packet from a multicast address.
365 @param[in] McastIo The pointer to the mcast Udp6Io.
366 @param[in] Context The pointer to the context.
368 @retval EFI_SUCCESS The mcast Udp6Io was successfully configured.
369 @retval Others Failed to configure the Udp6Io.
374 Mtftp6RrqConfigMcastUdpIo (
380 EFI_UDP6_PROTOCOL
*Udp6
;
381 EFI_UDP6_CONFIG_DATA
*Udp6Cfg
;
382 EFI_IPv6_ADDRESS Group
;
383 MTFTP6_INSTANCE
*Instance
;
385 Udp6
= McastIo
->Protocol
.Udp6
;
386 Udp6Cfg
= &(McastIo
->Config
.Udp6
);
387 Instance
= (MTFTP6_INSTANCE
*) Context
;
390 // Set the configure data for the mcast Udp6Io.
392 ZeroMem (Udp6Cfg
, sizeof (EFI_UDP6_CONFIG_DATA
));
394 Udp6Cfg
->AcceptPromiscuous
= FALSE
;
395 Udp6Cfg
->AcceptAnyPort
= FALSE
;
396 Udp6Cfg
->AllowDuplicatePort
= FALSE
;
397 Udp6Cfg
->TrafficClass
= 0;
398 Udp6Cfg
->HopLimit
= 128;
399 Udp6Cfg
->ReceiveTimeout
= 0;
400 Udp6Cfg
->TransmitTimeout
= 0;
401 Udp6Cfg
->StationPort
= Instance
->McastPort
;
402 Udp6Cfg
->RemotePort
= 0;
405 &Udp6Cfg
->RemoteAddress
,
407 sizeof (EFI_IPv6_ADDRESS
)
411 // Configure the mcast Udp6Io.
413 Status
= Udp6
->Configure (Udp6
, Udp6Cfg
);
415 if (EFI_ERROR (Status
)) {
420 // Join the multicast group
422 CopyMem (&Group
, &Instance
->McastIp
, sizeof (EFI_IPv6_ADDRESS
));
424 return Udp6
->Groups (Udp6
, TRUE
, &Group
);
429 Process the OACK packet for Rrq.
431 @param[in] Instance The pointer to the Mtftp6 instance.
432 @param[in] Packet The pointer to the received packet.
433 @param[in] Len The length of the packet.
434 @param[out] UdpPacket The net buf of received packet.
435 @param[out] IsCompleted If TRUE, the download has been completed.
436 Otherwise, the download has not been completed.
438 @retval EFI_DEVICE_ERROR Failed to create/start a multicast Udp6 child.
439 @retval EFI_TFTP_ERROR An error happened during the process.
440 @retval EFI_SUCCESS The OACK packet successfully processed.
444 Mtftp6RrqHandleOack (
445 IN MTFTP6_INSTANCE
*Instance
,
446 IN EFI_MTFTP6_PACKET
*Packet
,
448 OUT NET_BUF
**UdpPacket
,
449 OUT BOOLEAN
*IsCompleted
452 EFI_MTFTP6_OPTION
*Options
;
454 MTFTP6_EXT_OPTION_INFO ExtInfo
;
457 EFI_UDP6_PROTOCOL
*Udp6
;
459 *IsCompleted
= FALSE
;
463 // If already started the master download, don't change the
464 // setting. Master download always succeeds.
466 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
467 ASSERT (Expected
!= -1);
469 if (Instance
->IsMaster
&& Expected
!= 1) {
473 ZeroMem (&ExtInfo
, sizeof (MTFTP6_EXT_OPTION_INFO
));
476 // Parse the options in the packet.
478 Status
= Mtftp6ParseStart (Packet
, Len
, &Count
, &Options
);
480 if (EFI_ERROR (Status
)) {
483 ASSERT (Options
!= NULL
);
486 // Parse the extensive options in the packet.
488 Status
= Mtftp6ParseExtensionOption (Options
, Count
, FALSE
, &ExtInfo
);
490 if (EFI_ERROR (Status
) || !Mtftp6RrqOackValid (Instance
, &ExtInfo
, &Instance
->ExtInfo
)) {
492 // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.
494 if (Status
!= EFI_OUT_OF_RESOURCES
) {
496 // Free the received packet before send new packet in ReceiveNotify,
497 // since the udpio might need to be reconfigured.
499 NetbufFree (*UdpPacket
);
502 // Send the Mtftp6 error message if invalid packet.
506 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
507 (UINT8
*) "Mal-formated OACK packet"
511 return EFI_TFTP_ERROR
;
514 if ((ExtInfo
.BitMap
& MTFTP6_OPT_MCAST_BIT
) != 0) {
517 // Save the multicast info. Always update the Master, only update the
518 // multicast IP address, block size, timeoute at the first time. If IP
519 // address is updated, create a UDP child to receive the multicast.
521 Instance
->IsMaster
= ExtInfo
.IsMaster
;
523 if (NetIp6IsUnspecifiedAddr (&Instance
->McastIp
)) {
524 if (NetIp6IsUnspecifiedAddr (&ExtInfo
.McastIp
) || ExtInfo
.McastPort
== 0) {
526 // Free the received packet before send new packet in ReceiveNotify,
527 // since the udpio might need to be reconfigured.
529 NetbufFree (*UdpPacket
);
532 // Send the Mtftp6 error message if invalid multi-cast setting.
536 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
537 (UINT8
*) "Illegal multicast setting"
540 return EFI_TFTP_ERROR
;
544 // Create a UDP child then start receive the multicast from it.
549 sizeof (EFI_IP_ADDRESS
)
552 Instance
->McastPort
= ExtInfo
.McastPort
;
553 if (Instance
->McastUdpIo
== NULL
) {
554 Instance
->McastUdpIo
= UdpIoCreateIo (
555 Instance
->Service
->Controller
,
556 Instance
->Service
->Image
,
557 Mtftp6RrqConfigMcastUdpIo
,
561 if (Instance
->McastUdpIo
!= NULL
) {
562 Status
= gBS
->OpenProtocol (
563 Instance
->McastUdpIo
->UdpHandle
,
564 &gEfiUdp6ProtocolGuid
,
566 Instance
->Service
->Image
,
568 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
570 if (EFI_ERROR (Status
)) {
571 UdpIoFreeIo (Instance
->McastUdpIo
);
572 Instance
->McastUdpIo
= NULL
;
573 return EFI_DEVICE_ERROR
;
578 if (Instance
->McastUdpIo
== NULL
) {
579 return EFI_DEVICE_ERROR
;
582 Status
= UdpIoRecvDatagram (
583 Instance
->McastUdpIo
,
589 if (EFI_ERROR (Status
)) {
591 // Free the received packet before send new packet in ReceiveNotify,
592 // since the udpio might need to be reconfigured.
594 NetbufFree (*UdpPacket
);
597 // Send the Mtftp6 error message if failed to create Udp6Io to receive.
601 EFI_MTFTP6_ERRORCODE_ACCESS_VIOLATION
,
602 (UINT8
*) "Failed to create socket to receive multicast packet"
609 // Update the parameters used.
611 if (ExtInfo
.BlkSize
!= 0) {
612 Instance
->BlkSize
= ExtInfo
.BlkSize
;
615 if (ExtInfo
.Timeout
!= 0) {
616 Instance
->Timeout
= ExtInfo
.Timeout
;
622 Instance
->IsMaster
= TRUE
;
624 if (ExtInfo
.BlkSize
!= 0) {
625 Instance
->BlkSize
= ExtInfo
.BlkSize
;
628 if (ExtInfo
.Timeout
!= 0) {
629 Instance
->Timeout
= ExtInfo
.Timeout
;
634 // Free the received packet before send new packet in ReceiveNotify,
635 // since the udpio might need to be reconfigured.
637 NetbufFree (*UdpPacket
);
640 // Send an ACK to (Expected - 1) which is 0 for unicast download,
641 // or tell the server we want to receive the Expected block.
643 return Mtftp6RrqSendAck (Instance
, (UINT16
) (Expected
- 1));
648 The packet process callback for Mtftp6 download.
650 @param[in] UdpPacket The pointer to the packet received.
651 @param[in] UdpEpt The pointer to the Udp6 access point.
652 @param[in] IoStatus The status from Udp6 instance.
653 @param[in] Context The pointer to the context.
659 IN NET_BUF
*UdpPacket
,
660 IN UDP_END_POINT
*UdpEpt
,
661 IN EFI_STATUS IoStatus
,
665 MTFTP6_INSTANCE
*Instance
;
666 EFI_MTFTP6_PACKET
*Packet
;
674 Instance
= (MTFTP6_INSTANCE
*) Context
;
676 NET_CHECK_SIGNATURE (Instance
, MTFTP6_INSTANCE_SIGNATURE
);
678 Status
= EFI_SUCCESS
;
685 // Return error status if Udp6 instance failed to receive.
687 if (EFI_ERROR (IoStatus
)) {
692 ASSERT (UdpPacket
!= NULL
);
694 if (UdpPacket
->TotalSize
< MTFTP6_OPCODE_LEN
) {
699 // Find the port this packet is from to restart receive correctly.
702 Ip6Swap128 (&UdpEpt
->LocalAddr
.v6
),
704 sizeof (EFI_IPv6_ADDRESS
)
712 // Client send initial request to server's listening port. Server
713 // will select a UDP port to communicate with the client. The server
714 // is required to use the same port as RemotePort to multicast the
717 if (UdpEpt
->RemotePort
!= Instance
->ServerDataPort
) {
718 if (Instance
->ServerDataPort
!= 0) {
722 // For the subsequent exchange of requests, reconfigure the udpio as
723 // (serverip, serverport, localip, localport).
724 // Ususally, the client set serverport as 0 to receive and reset it
725 // once the first packet arrives to send ack.
727 Instance
->ServerDataPort
= UdpEpt
->RemotePort
;
732 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
734 Len
= UdpPacket
->TotalSize
;
735 TotalNum
= UdpPacket
->BlockOpNum
;
738 Packet
= AllocateZeroPool (Len
);
740 if (Packet
== NULL
) {
741 Status
= EFI_OUT_OF_RESOURCES
;
745 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
748 Packet
= (EFI_MTFTP6_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
749 ASSERT (Packet
!= NULL
);
752 Opcode
= NTOHS (Packet
->OpCode
);
755 // Callback to the user's CheckPacket if provided. Abort the transmission
756 // if CheckPacket returns an EFI_ERROR code.
758 if ((Instance
->Token
->CheckPacket
!= NULL
) &&
759 (Opcode
== EFI_MTFTP6_OPCODE_OACK
|| Opcode
== EFI_MTFTP6_OPCODE_ERROR
)
762 Status
= Instance
->Token
->CheckPacket (
769 if (EFI_ERROR (Status
)) {
771 // Send an error message to the server to inform it
773 if (Opcode
!= EFI_MTFTP6_OPCODE_ERROR
) {
775 // Free the received packet before send new packet in ReceiveNotify,
776 // since the udpio might need to be reconfigured.
778 NetbufFree (UdpPacket
);
781 // Send the Mtftp6 error message if user aborted the current session.
785 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
786 (UINT8
*) "User aborted the transfer"
790 Status
= EFI_ABORTED
;
796 // Switch the process routines by the operation code.
799 case EFI_MTFTP6_OPCODE_DATA
:
800 if ((Len
> (UINT32
) (MTFTP6_DATA_HEAD_LEN
+ Instance
->BlkSize
)) || (Len
< (UINT32
) MTFTP6_DATA_HEAD_LEN
)) {
804 // Handle the data packet of Rrq.
806 Status
= Mtftp6RrqHandleData (
815 case EFI_MTFTP6_OPCODE_OACK
:
816 if (IsMcast
|| Len
<= MTFTP6_OPCODE_LEN
) {
820 // Handle the Oack packet of Rrq.
822 Status
= Mtftp6RrqHandleOack (
833 // Drop and return eror if received error message.
835 Status
= EFI_TFTP_ERROR
;
841 // Free the resources, then if !EFI_ERROR (Status), restart the
842 // receive, otherwise end the session.
844 if (Packet
!= NULL
&& TotalNum
> 1) {
847 if (UdpPacket
!= NULL
) {
848 NetbufFree (UdpPacket
);
850 if (!EFI_ERROR (Status
) && !IsCompleted
) {
852 Status
= UdpIoRecvDatagram (
853 Instance
->McastUdpIo
,
859 Status
= UdpIoRecvDatagram (
868 // Clean up the current session if failed to continue.
870 if (EFI_ERROR (Status
) || IsCompleted
) {
871 Mtftp6OperationClean (Instance
, Status
);
877 Start the Mtftp6 instance to download. It first initializes some
878 of the internal states, then builds and sends an RRQ reqeuest packet.
879 Finally, it starts receive for the downloading.
881 @param[in] Instance The pointer to the Mtftp6 instance.
882 @param[in] Operation The operation code of current packet.
884 @retval EFI_SUCCESS The Mtftp6 is started to download.
885 @retval Others Failed to start to download.
890 IN MTFTP6_INSTANCE
*Instance
,
897 // The valid block number range are [1, 0xffff]. For example:
898 // the client sends an RRQ request to the server, the server
899 // transfers the DATA1 block. If option negoitation is ongoing,
900 // the server will send back an OACK, then client will send ACK0.
902 Status
= Mtftp6InitBlockRange (&Instance
->BlkList
, 1, 0xffff);
904 if (EFI_ERROR (Status
)) {
908 Status
= Mtftp6SendRequest (Instance
, Operation
);
910 if (EFI_ERROR (Status
)) {
914 return UdpIoRecvDatagram (