]>
Commit | Line | Data |
---|---|---|
a3bcde70 HT |
1 | /** @file\r |
2 | Mtftp6 Rrq process functions implementation.\r | |
3 | \r | |
76389e18 | 4 | Copyright (c) 2009 - 2011, 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 | |
30 | EFI_STATUS\r | |
31 | Mtftp6RrqSendAck (\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 | |
83 | EFI_STATUS\r | |
84 | Mtftp6RrqSaveBlock (\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 | |
212 | EFI_STATUS\r | |
213 | Mtftp6RrqHandleData (\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 | |
313 | BOOLEAN\r | |
314 | Mtftp6RrqOackValid (\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 | |
371 | EFI_STATUS\r | |
372 | EFIAPI\r | |
373 | Mtftp6RrqConfigMcastUdpIo (\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 | |
442 | EFI_STATUS\r | |
443 | Mtftp6RrqHandleOack (\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 | |
480 | \r | |
481 | //\r | |
482 | // Parse the extensive options in the packet.\r | |
483 | //\r | |
484 | Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, &ExtInfo);\r | |
485 | \r | |
486 | if (EFI_ERROR (Status) || !Mtftp6RrqOackValid (Instance, &ExtInfo, &Instance->ExtInfo)) {\r | |
487 | //\r | |
488 | // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.\r | |
489 | //\r | |
490 | if (Status != EFI_OUT_OF_RESOURCES) {\r | |
491 | //\r | |
492 | // Free the received packet before send new packet in ReceiveNotify,\r | |
493 | // since the udpio might need to be reconfigured.\r | |
494 | //\r | |
495 | NetbufFree (*UdpPacket);\r | |
496 | *UdpPacket = NULL;\r | |
497 | //\r | |
498 | // Send the Mtftp6 error message if invalid packet.\r | |
499 | //\r | |
500 | Mtftp6SendError (\r | |
501 | Instance,\r | |
502 | EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,\r | |
503 | (UINT8 *) "Mal-formated OACK packet"\r | |
504 | );\r | |
505 | }\r | |
506 | \r | |
507 | return EFI_TFTP_ERROR;\r | |
508 | }\r | |
509 | \r | |
510 | if ((ExtInfo.BitMap & MTFTP6_OPT_MCAST_BIT) != 0) {\r | |
511 | \r | |
512 | //\r | |
513 | // Save the multicast info. Always update the Master, only update the\r | |
514 | // multicast IP address, block size, timeoute at the first time. If IP\r | |
515 | // address is updated, create a UDP child to receive the multicast.\r | |
516 | //\r | |
517 | Instance->IsMaster = ExtInfo.IsMaster;\r | |
518 | \r | |
519 | if (NetIp6IsUnspecifiedAddr (&Instance->McastIp)) {\r | |
520 | if (NetIp6IsUnspecifiedAddr (&ExtInfo.McastIp) || ExtInfo.McastPort == 0) {\r | |
521 | //\r | |
522 | // Free the received packet before send new packet in ReceiveNotify,\r | |
523 | // since the udpio might need to be reconfigured.\r | |
524 | //\r | |
525 | NetbufFree (*UdpPacket);\r | |
526 | *UdpPacket = NULL;\r | |
527 | //\r | |
528 | // Send the Mtftp6 error message if invalid multi-cast setting.\r | |
529 | //\r | |
530 | Mtftp6SendError (\r | |
531 | Instance,\r | |
532 | EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,\r | |
533 | (UINT8 *) "Illegal multicast setting"\r | |
534 | );\r | |
535 | \r | |
536 | return EFI_TFTP_ERROR;\r | |
537 | }\r | |
538 | \r | |
539 | //\r | |
540 | // Create a UDP child then start receive the multicast from it.\r | |
541 | //\r | |
542 | CopyMem (\r | |
543 | &Instance->McastIp,\r | |
544 | &ExtInfo.McastIp,\r | |
545 | sizeof (EFI_IP_ADDRESS)\r | |
546 | );\r | |
547 | \r | |
548 | Instance->McastPort = ExtInfo.McastPort;\r | |
549 | Instance->McastUdpIo = UdpIoCreateIo (\r | |
550 | Instance->Service->Controller,\r | |
551 | Instance->Service->Image,\r | |
552 | Mtftp6RrqConfigMcastUdpIo,\r | |
553 | UDP_IO_UDP6_VERSION,\r | |
554 | Instance\r | |
555 | );\r | |
556 | \r | |
557 | if (Instance->McastUdpIo == NULL) {\r | |
558 | return EFI_DEVICE_ERROR;\r | |
559 | }\r | |
560 | \r | |
561 | Status = UdpIoRecvDatagram (\r | |
562 | Instance->McastUdpIo,\r | |
563 | Mtftp6RrqInput,\r | |
564 | Instance,\r | |
565 | 0\r | |
566 | );\r | |
567 | \r | |
568 | if (EFI_ERROR (Status)) {\r | |
569 | //\r | |
570 | // Free the received packet before send new packet in ReceiveNotify,\r | |
571 | // since the udpio might need to be reconfigured.\r | |
572 | //\r | |
573 | NetbufFree (*UdpPacket);\r | |
574 | *UdpPacket = NULL;\r | |
575 | //\r | |
576 | // Send the Mtftp6 error message if failed to create Udp6Io to receive.\r | |
577 | //\r | |
578 | Mtftp6SendError (\r | |
579 | Instance,\r | |
580 | EFI_MTFTP6_ERRORCODE_ACCESS_VIOLATION,\r | |
581 | (UINT8 *) "Failed to create socket to receive multicast packet"\r | |
582 | );\r | |
583 | \r | |
584 | return Status;\r | |
585 | }\r | |
586 | \r | |
587 | //\r | |
588 | // Update the parameters used.\r | |
589 | //\r | |
590 | if (ExtInfo.BlkSize != 0) {\r | |
591 | Instance->BlkSize = ExtInfo.BlkSize;\r | |
592 | }\r | |
593 | \r | |
594 | if (ExtInfo.Timeout != 0) {\r | |
595 | Instance->Timeout = ExtInfo.Timeout;\r | |
596 | }\r | |
597 | }\r | |
598 | \r | |
599 | } else {\r | |
600 | \r | |
601 | Instance->IsMaster = TRUE;\r | |
602 | \r | |
603 | if (ExtInfo.BlkSize != 0) {\r | |
604 | Instance->BlkSize = ExtInfo.BlkSize;\r | |
605 | }\r | |
606 | \r | |
607 | if (ExtInfo.Timeout != 0) {\r | |
608 | Instance->Timeout = ExtInfo.Timeout;\r | |
609 | }\r | |
610 | }\r | |
611 | \r | |
612 | //\r | |
613 | // Free the received packet before send new packet in ReceiveNotify,\r | |
614 | // since the udpio might need to be reconfigured.\r | |
615 | //\r | |
616 | NetbufFree (*UdpPacket);\r | |
617 | *UdpPacket = NULL;\r | |
618 | //\r | |
619 | // Send an ACK to (Expected - 1) which is 0 for unicast download,\r | |
620 | // or tell the server we want to receive the Expected block.\r | |
621 | //\r | |
622 | return Mtftp6RrqSendAck (Instance, (UINT16) (Expected - 1));\r | |
623 | }\r | |
624 | \r | |
625 | \r | |
626 | /**\r | |
627 | The packet process callback for Mtftp6 download.\r | |
628 | \r | |
629 | @param[in] UdpPacket The pointer to the packet received.\r | |
630 | @param[in] UdpEpt The pointer to the Udp6 access point.\r | |
631 | @param[in] IoStatus The status from Udp6 instance.\r | |
632 | @param[in] Context The pointer to the context.\r | |
633 | \r | |
634 | **/\r | |
635 | VOID\r | |
636 | EFIAPI\r | |
637 | Mtftp6RrqInput (\r | |
638 | IN NET_BUF *UdpPacket,\r | |
639 | IN UDP_END_POINT *UdpEpt,\r | |
640 | IN EFI_STATUS IoStatus,\r | |
641 | IN VOID *Context\r | |
642 | )\r | |
643 | {\r | |
644 | MTFTP6_INSTANCE *Instance;\r | |
645 | EFI_MTFTP6_PACKET *Packet;\r | |
646 | BOOLEAN IsCompleted;\r | |
647 | BOOLEAN IsMcast;\r | |
648 | EFI_STATUS Status;\r | |
649 | UINT16 Opcode;\r | |
650 | UINT32 TotalNum;\r | |
651 | UINT32 Len;\r | |
652 | \r | |
653 | Instance = (MTFTP6_INSTANCE *) Context;\r | |
654 | \r | |
655 | NET_CHECK_SIGNATURE (Instance, MTFTP6_INSTANCE_SIGNATURE);\r | |
656 | \r | |
657 | Status = EFI_SUCCESS;\r | |
658 | Packet = NULL;\r | |
659 | IsCompleted = FALSE;\r | |
660 | IsMcast = FALSE;\r | |
661 | TotalNum = 0;\r | |
662 | \r | |
663 | //\r | |
664 | // Return error status if Udp6 instance failed to receive.\r | |
665 | //\r | |
666 | if (EFI_ERROR (IoStatus)) {\r | |
667 | Status = IoStatus;\r | |
668 | goto ON_EXIT;\r | |
669 | }\r | |
670 | \r | |
671 | ASSERT (UdpPacket != NULL);\r | |
672 | \r | |
673 | if (UdpPacket->TotalSize < MTFTP6_OPCODE_LEN) {\r | |
674 | goto ON_EXIT;\r | |
675 | }\r | |
676 | \r | |
677 | //\r | |
678 | // Find the port this packet is from to restart receive correctly.\r | |
679 | //\r | |
680 | if (CompareMem (\r | |
681 | Ip6Swap128 (&UdpEpt->LocalAddr.v6),\r | |
682 | &Instance->McastIp,\r | |
683 | sizeof (EFI_IPv6_ADDRESS)\r | |
684 | ) == 0) {\r | |
685 | IsMcast = TRUE;\r | |
686 | } else {\r | |
687 | IsMcast = FALSE;\r | |
688 | }\r | |
689 | \r | |
690 | //\r | |
691 | // Client send initial request to server's listening port. Server\r | |
692 | // will select a UDP port to communicate with the client. The server\r | |
693 | // is required to use the same port as RemotePort to multicast the\r | |
694 | // data.\r | |
695 | //\r | |
696 | if (UdpEpt->RemotePort != Instance->ServerDataPort) {\r | |
697 | if (Instance->ServerDataPort != 0) {\r | |
698 | goto ON_EXIT;\r | |
699 | } else {\r | |
700 | //\r | |
701 | // For the subsequent exchange of requests, reconfigure the udpio as\r | |
702 | // (serverip, serverport, localip, localport).\r | |
703 | // Ususally, the client set serverport as 0 to receive and reset it\r | |
704 | // once the first packet arrives to send ack.\r | |
705 | //\r | |
706 | Instance->ServerDataPort = UdpEpt->RemotePort;\r | |
707 | }\r | |
708 | }\r | |
709 | \r | |
710 | //\r | |
711 | // Copy the MTFTP packet to a continuous buffer if it isn't already so.\r | |
712 | //\r | |
713 | Len = UdpPacket->TotalSize;\r | |
714 | TotalNum = UdpPacket->BlockOpNum;\r | |
715 | \r | |
716 | if (TotalNum > 1) {\r | |
717 | Packet = AllocateZeroPool (Len);\r | |
718 | \r | |
719 | if (Packet == NULL) {\r | |
720 | Status = EFI_OUT_OF_RESOURCES;\r | |
721 | goto ON_EXIT;\r | |
722 | }\r | |
723 | \r | |
724 | NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet);\r | |
725 | \r | |
726 | } else {\r | |
727 | Packet = (EFI_MTFTP6_PACKET *) NetbufGetByte (UdpPacket, 0, NULL);\r | |
728 | ASSERT (Packet != NULL);\r | |
729 | }\r | |
730 | \r | |
731 | Opcode = NTOHS (Packet->OpCode);\r | |
732 | \r | |
733 | //\r | |
734 | // Callback to the user's CheckPacket if provided. Abort the transmission\r | |
735 | // if CheckPacket returns an EFI_ERROR code.\r | |
736 | //\r | |
737 | if ((Instance->Token->CheckPacket != NULL) &&\r | |
738 | (Opcode == EFI_MTFTP6_OPCODE_OACK || Opcode == EFI_MTFTP6_OPCODE_ERROR)\r | |
739 | ) {\r | |
740 | \r | |
741 | Status = Instance->Token->CheckPacket (\r | |
742 | &Instance->Mtftp6,\r | |
743 | Instance->Token,\r | |
744 | (UINT16) Len,\r | |
745 | Packet\r | |
746 | );\r | |
747 | \r | |
748 | if (EFI_ERROR (Status)) {\r | |
749 | //\r | |
750 | // Send an error message to the server to inform it\r | |
751 | //\r | |
752 | if (Opcode != EFI_MTFTP6_OPCODE_ERROR) {\r | |
753 | //\r | |
754 | // Free the received packet before send new packet in ReceiveNotify,\r | |
755 | // since the udpio might need to be reconfigured.\r | |
756 | //\r | |
757 | NetbufFree (UdpPacket);\r | |
758 | UdpPacket = NULL;\r | |
759 | //\r | |
760 | // Send the Mtftp6 error message if user aborted the current session.\r | |
761 | //\r | |
762 | Mtftp6SendError (\r | |
763 | Instance,\r | |
764 | EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,\r | |
765 | (UINT8 *) "User aborted the transfer"\r | |
766 | );\r | |
767 | }\r | |
768 | \r | |
769 | Status = EFI_ABORTED;\r | |
770 | goto ON_EXIT;\r | |
771 | }\r | |
772 | }\r | |
773 | \r | |
774 | //\r | |
775 | // Switch the process routines by the operation code.\r | |
776 | //\r | |
777 | switch (Opcode) {\r | |
778 | case EFI_MTFTP6_OPCODE_DATA:\r | |
779 | if ((Len > (UINT32) (MTFTP6_DATA_HEAD_LEN + Instance->BlkSize)) || (Len < (UINT32) MTFTP6_DATA_HEAD_LEN)) {\r | |
780 | goto ON_EXIT;\r | |
781 | }\r | |
782 | //\r | |
783 | // Handle the data packet of Rrq.\r | |
784 | //\r | |
785 | Status = Mtftp6RrqHandleData (\r | |
786 | Instance,\r | |
787 | Packet,\r | |
788 | Len,\r | |
789 | &UdpPacket,\r | |
790 | &IsCompleted\r | |
791 | );\r | |
792 | break;\r | |
793 | \r | |
794 | case EFI_MTFTP6_OPCODE_OACK:\r | |
795 | if (IsMcast || Len <= MTFTP6_OPCODE_LEN) {\r | |
796 | goto ON_EXIT;\r | |
797 | }\r | |
798 | //\r | |
799 | // Handle the Oack packet of Rrq.\r | |
800 | //\r | |
801 | Status = Mtftp6RrqHandleOack (\r | |
802 | Instance,\r | |
803 | Packet,\r | |
804 | Len,\r | |
805 | &UdpPacket,\r | |
806 | &IsCompleted\r | |
807 | );\r | |
808 | break;\r | |
809 | \r | |
810 | default:\r | |
811 | //\r | |
812 | // Drop and return eror if received error message.\r | |
813 | //\r | |
814 | Status = EFI_TFTP_ERROR;\r | |
815 | break;\r | |
816 | }\r | |
817 | \r | |
818 | ON_EXIT:\r | |
819 | //\r | |
820 | // Free the resources, then if !EFI_ERROR (Status), restart the\r | |
821 | // receive, otherwise end the session.\r | |
822 | //\r | |
823 | if (Packet != NULL && TotalNum > 1) {\r | |
824 | FreePool (Packet);\r | |
825 | }\r | |
826 | if (UdpPacket != NULL) {\r | |
827 | NetbufFree (UdpPacket);\r | |
828 | }\r | |
829 | if (!EFI_ERROR (Status) && !IsCompleted) {\r | |
830 | if (IsMcast) {\r | |
831 | Status = UdpIoRecvDatagram (\r | |
832 | Instance->McastUdpIo,\r | |
833 | Mtftp6RrqInput,\r | |
834 | Instance,\r | |
835 | 0\r | |
836 | );\r | |
837 | } else {\r | |
838 | Status = UdpIoRecvDatagram (\r | |
839 | Instance->UdpIo,\r | |
840 | Mtftp6RrqInput,\r | |
841 | Instance,\r | |
842 | 0\r | |
843 | );\r | |
844 | }\r | |
845 | }\r | |
846 | //\r | |
847 | // Clean up the current session if failed to continue.\r | |
848 | //\r | |
849 | if (EFI_ERROR (Status) || IsCompleted) {\r | |
850 | Mtftp6OperationClean (Instance, Status);\r | |
851 | }\r | |
852 | }\r | |
853 | \r | |
854 | \r | |
855 | /**\r | |
856 | Start the Mtftp6 instance to download. It first initializes some\r | |
857 | of the internal states, then builds and sends an RRQ reqeuest packet.\r | |
858 | Finally, it starts receive for the downloading.\r | |
859 | \r | |
860 | @param[in] Instance The pointer to the Mtftp6 instance.\r | |
861 | @param[in] Operation The operation code of current packet.\r | |
862 | \r | |
863 | @retval EFI_SUCCESS The Mtftp6 is started to download.\r | |
864 | @retval Others Failed to start to download.\r | |
865 | \r | |
866 | **/\r | |
867 | EFI_STATUS\r | |
868 | Mtftp6RrqStart (\r | |
869 | IN MTFTP6_INSTANCE *Instance,\r | |
870 | IN UINT16 Operation\r | |
871 | )\r | |
872 | {\r | |
873 | EFI_STATUS Status;\r | |
874 | \r | |
875 | //\r | |
876 | // The valid block number range are [1, 0xffff]. For example:\r | |
877 | // the client sends an RRQ request to the server, the server\r | |
878 | // transfers the DATA1 block. If option negoitation is ongoing,\r | |
879 | // the server will send back an OACK, then client will send ACK0.\r | |
880 | //\r | |
881 | Status = Mtftp6InitBlockRange (&Instance->BlkList, 1, 0xffff);\r | |
882 | \r | |
883 | if (EFI_ERROR (Status)) {\r | |
884 | return Status;\r | |
885 | }\r | |
886 | \r | |
887 | Status = Mtftp6SendRequest (Instance, Operation);\r | |
888 | \r | |
889 | if (EFI_ERROR (Status)) {\r | |
890 | return Status;\r | |
891 | }\r | |
892 | \r | |
893 | return UdpIoRecvDatagram (\r | |
894 | Instance->UdpIo,\r | |
895 | Mtftp6RrqInput,\r | |
896 | Instance,\r | |
897 | 0\r | |
898 | );\r | |
899 | }\r | |
900 | \r |