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