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;
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
;
462 // If already started the master download, don't change the
463 // setting. Master download always succeeds.
465 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
466 ASSERT (Expected
!= -1);
468 if (Instance
->IsMaster
&& Expected
!= 1) {
472 ZeroMem (&ExtInfo
, sizeof (MTFTP6_EXT_OPTION_INFO
));
475 // Parse the options in the packet.
477 Status
= Mtftp6ParseStart (Packet
, Len
, &Count
, &Options
);
479 if (EFI_ERROR (Status
)) {
482 ASSERT (Options
!= NULL
);
485 // Parse the extensive options in the packet.
487 Status
= Mtftp6ParseExtensionOption (Options
, Count
, FALSE
, &ExtInfo
);
489 if (EFI_ERROR (Status
) || !Mtftp6RrqOackValid (Instance
, &ExtInfo
, &Instance
->ExtInfo
)) {
491 // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.
493 if (Status
!= EFI_OUT_OF_RESOURCES
) {
495 // Free the received packet before send new packet in ReceiveNotify,
496 // since the udpio might need to be reconfigured.
498 NetbufFree (*UdpPacket
);
501 // Send the Mtftp6 error message if invalid packet.
505 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
506 (UINT8
*) "Mal-formated OACK packet"
510 return EFI_TFTP_ERROR
;
513 if ((ExtInfo
.BitMap
& MTFTP6_OPT_MCAST_BIT
) != 0) {
516 // Save the multicast info. Always update the Master, only update the
517 // multicast IP address, block size, timeoute at the first time. If IP
518 // address is updated, create a UDP child to receive the multicast.
520 Instance
->IsMaster
= ExtInfo
.IsMaster
;
522 if (NetIp6IsUnspecifiedAddr (&Instance
->McastIp
)) {
523 if (NetIp6IsUnspecifiedAddr (&ExtInfo
.McastIp
) || ExtInfo
.McastPort
== 0) {
525 // Free the received packet before send new packet in ReceiveNotify,
526 // since the udpio might need to be reconfigured.
528 NetbufFree (*UdpPacket
);
531 // Send the Mtftp6 error message if invalid multi-cast setting.
535 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
536 (UINT8
*) "Illegal multicast setting"
539 return EFI_TFTP_ERROR
;
543 // Create a UDP child then start receive the multicast from it.
548 sizeof (EFI_IP_ADDRESS
)
551 Instance
->McastPort
= ExtInfo
.McastPort
;
552 if (Instance
->McastUdpIo
== NULL
) {
553 Instance
->McastUdpIo
= UdpIoCreateIo (
554 Instance
->Service
->Controller
,
555 Instance
->Service
->Image
,
556 Mtftp6RrqConfigMcastUdpIo
,
560 if (Instance
->McastUdpIo
!= NULL
) {
561 Status
= gBS
->OpenProtocol (
562 Instance
->McastUdpIo
->UdpHandle
,
563 &gEfiUdp6ProtocolGuid
,
565 Instance
->Service
->Image
,
567 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
569 if (EFI_ERROR (Status
)) {
570 UdpIoFreeIo (Instance
->McastUdpIo
);
571 Instance
->McastUdpIo
= NULL
;
572 return EFI_DEVICE_ERROR
;
577 if (Instance
->McastUdpIo
== NULL
) {
578 return EFI_DEVICE_ERROR
;
581 Status
= UdpIoRecvDatagram (
582 Instance
->McastUdpIo
,
588 if (EFI_ERROR (Status
)) {
590 // Free the received packet before send new packet in ReceiveNotify,
591 // since the udpio might need to be reconfigured.
593 NetbufFree (*UdpPacket
);
596 // Send the Mtftp6 error message if failed to create Udp6Io to receive.
600 EFI_MTFTP6_ERRORCODE_ACCESS_VIOLATION
,
601 (UINT8
*) "Failed to create socket to receive multicast packet"
608 // Update the parameters used.
610 if (ExtInfo
.BlkSize
!= 0) {
611 Instance
->BlkSize
= ExtInfo
.BlkSize
;
614 if (ExtInfo
.Timeout
!= 0) {
615 Instance
->Timeout
= ExtInfo
.Timeout
;
621 Instance
->IsMaster
= TRUE
;
623 if (ExtInfo
.BlkSize
!= 0) {
624 Instance
->BlkSize
= ExtInfo
.BlkSize
;
627 if (ExtInfo
.Timeout
!= 0) {
628 Instance
->Timeout
= ExtInfo
.Timeout
;
633 // Free the received packet before send new packet in ReceiveNotify,
634 // since the udpio might need to be reconfigured.
636 NetbufFree (*UdpPacket
);
639 // Send an ACK to (Expected - 1) which is 0 for unicast download,
640 // or tell the server we want to receive the Expected block.
642 return Mtftp6RrqSendAck (Instance
, (UINT16
) (Expected
- 1));
647 The packet process callback for Mtftp6 download.
649 @param[in] UdpPacket The pointer to the packet received.
650 @param[in] UdpEpt The pointer to the Udp6 access point.
651 @param[in] IoStatus The status from Udp6 instance.
652 @param[in] Context The pointer to the context.
658 IN NET_BUF
*UdpPacket
,
659 IN UDP_END_POINT
*UdpEpt
,
660 IN EFI_STATUS IoStatus
,
664 MTFTP6_INSTANCE
*Instance
;
665 EFI_MTFTP6_PACKET
*Packet
;
673 Instance
= (MTFTP6_INSTANCE
*) Context
;
675 NET_CHECK_SIGNATURE (Instance
, MTFTP6_INSTANCE_SIGNATURE
);
677 Status
= EFI_SUCCESS
;
684 // Return error status if Udp6 instance failed to receive.
686 if (EFI_ERROR (IoStatus
)) {
691 ASSERT (UdpPacket
!= NULL
);
693 if (UdpPacket
->TotalSize
< MTFTP6_OPCODE_LEN
) {
698 // Find the port this packet is from to restart receive correctly.
701 Ip6Swap128 (&UdpEpt
->LocalAddr
.v6
),
703 sizeof (EFI_IPv6_ADDRESS
)
711 // Client send initial request to server's listening port. Server
712 // will select a UDP port to communicate with the client. The server
713 // is required to use the same port as RemotePort to multicast the
716 if (UdpEpt
->RemotePort
!= Instance
->ServerDataPort
) {
717 if (Instance
->ServerDataPort
!= 0) {
721 // For the subsequent exchange of requests, reconfigure the udpio as
722 // (serverip, serverport, localip, localport).
723 // Ususally, the client set serverport as 0 to receive and reset it
724 // once the first packet arrives to send ack.
726 Instance
->ServerDataPort
= UdpEpt
->RemotePort
;
731 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
733 Len
= UdpPacket
->TotalSize
;
734 TotalNum
= UdpPacket
->BlockOpNum
;
737 Packet
= AllocateZeroPool (Len
);
739 if (Packet
== NULL
) {
740 Status
= EFI_OUT_OF_RESOURCES
;
744 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
747 Packet
= (EFI_MTFTP6_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
748 ASSERT (Packet
!= NULL
);
751 Opcode
= NTOHS (Packet
->OpCode
);
754 // Callback to the user's CheckPacket if provided. Abort the transmission
755 // if CheckPacket returns an EFI_ERROR code.
757 if ((Instance
->Token
->CheckPacket
!= NULL
) &&
758 (Opcode
== EFI_MTFTP6_OPCODE_OACK
|| Opcode
== EFI_MTFTP6_OPCODE_ERROR
)
761 Status
= Instance
->Token
->CheckPacket (
768 if (EFI_ERROR (Status
)) {
770 // Send an error message to the server to inform it
772 if (Opcode
!= EFI_MTFTP6_OPCODE_ERROR
) {
774 // Free the received packet before send new packet in ReceiveNotify,
775 // since the udpio might need to be reconfigured.
777 NetbufFree (UdpPacket
);
780 // Send the Mtftp6 error message if user aborted the current session.
784 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
785 (UINT8
*) "User aborted the transfer"
789 Status
= EFI_ABORTED
;
795 // Switch the process routines by the operation code.
798 case EFI_MTFTP6_OPCODE_DATA
:
799 if ((Len
> (UINT32
) (MTFTP6_DATA_HEAD_LEN
+ Instance
->BlkSize
)) || (Len
< (UINT32
) MTFTP6_DATA_HEAD_LEN
)) {
803 // Handle the data packet of Rrq.
805 Status
= Mtftp6RrqHandleData (
814 case EFI_MTFTP6_OPCODE_OACK
:
815 if (IsMcast
|| Len
<= MTFTP6_OPCODE_LEN
) {
819 // Handle the Oack packet of Rrq.
821 Status
= Mtftp6RrqHandleOack (
832 // Drop and return eror if received error message.
834 Status
= EFI_TFTP_ERROR
;
840 // Free the resources, then if !EFI_ERROR (Status), restart the
841 // receive, otherwise end the session.
843 if (Packet
!= NULL
&& TotalNum
> 1) {
846 if (UdpPacket
!= NULL
) {
847 NetbufFree (UdpPacket
);
849 if (!EFI_ERROR (Status
) && !IsCompleted
) {
851 Status
= UdpIoRecvDatagram (
852 Instance
->McastUdpIo
,
858 Status
= UdpIoRecvDatagram (
867 // Clean up the current session if failed to continue.
869 if (EFI_ERROR (Status
) || IsCompleted
) {
870 Mtftp6OperationClean (Instance
, Status
);
876 Start the Mtftp6 instance to download. It first initializes some
877 of the internal states, then builds and sends an RRQ reqeuest packet.
878 Finally, it starts receive for the downloading.
880 @param[in] Instance The pointer to the Mtftp6 instance.
881 @param[in] Operation The operation code of current packet.
883 @retval EFI_SUCCESS The Mtftp6 is started to download.
884 @retval Others Failed to start to download.
889 IN MTFTP6_INSTANCE
*Instance
,
896 // The valid block number range are [1, 0xffff]. For example:
897 // the client sends an RRQ request to the server, the server
898 // transfers the DATA1 block. If option negoitation is ongoing,
899 // the server will send back an OACK, then client will send ACK0.
901 Status
= Mtftp6InitBlockRange (&Instance
->BlkList
, 1, 0xffff);
903 if (EFI_ERROR (Status
)) {
907 Status
= Mtftp6SendRequest (Instance
, Operation
);
909 if (EFI_ERROR (Status
)) {
913 return UdpIoRecvDatagram (