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