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