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