]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Support.c
cfb4b26df2e538d2459aa6476c320606f4d9c560
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Mtftp4Dxe / Mtftp4Support.c
1 /** @file
2 Support routines for Mtftp.
3
4 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Mtftp4Impl.h"
16
17
18 /**
19 Allocate a MTFTP4 block range, then init it to the range of [Start, End]
20
21 @param Start The start block number
22 @param End The last block number in the range
23
24 @return Pointer to the created block range, NULL if failed to allocate memory.
25
26 **/
27 MTFTP4_BLOCK_RANGE *
28 Mtftp4AllocateRange (
29 IN UINT16 Start,
30 IN UINT16 End
31 )
32 {
33 MTFTP4_BLOCK_RANGE *Range;
34
35 Range = AllocateZeroPool (sizeof (MTFTP4_BLOCK_RANGE));
36
37 if (Range == NULL) {
38 return NULL;
39 }
40
41 InitializeListHead (&Range->Link);
42 Range->Start = Start;
43 Range->End = End;
44 Range->Bound = End;
45
46 return Range;
47 }
48
49
50 /**
51 Initialize the block range for either RRQ or WRQ.
52
53 RRQ and WRQ have different requirements for Start and End.
54 For example, during start up, WRQ initializes its whole valid block range
55 to [0, 0xffff]. This is bacause the server will send us a ACK0 to inform us
56 to start the upload. When the client received ACK0, it will remove 0 from the
57 range, get the next block number, which is 1, then upload the BLOCK1. For RRQ
58 without option negotiation, the server will directly send us the BLOCK1 in
59 response to the client's RRQ. When received BLOCK1, the client will remove
60 it from the block range and send an ACK. It also works if there is option
61 negotiation.
62
63 @param Head The block range head to initialize
64 @param Start The Start block number.
65 @param End The last block number.
66
67 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range
68 @retval EFI_SUCCESS The initial block range is created.
69
70 **/
71 EFI_STATUS
72 Mtftp4InitBlockRange (
73 IN LIST_ENTRY *Head,
74 IN UINT16 Start,
75 IN UINT16 End
76 )
77 {
78 MTFTP4_BLOCK_RANGE *Range;
79
80 Range = Mtftp4AllocateRange (Start, End);
81
82 if (Range == NULL) {
83 return EFI_OUT_OF_RESOURCES;
84 }
85
86 InsertTailList (Head, &Range->Link);
87 return EFI_SUCCESS;
88 }
89
90
91 /**
92 Get the first valid block number on the range list.
93
94 @param Head The block range head
95
96 @return The first valid block number, -1 if the block range is empty.
97
98 **/
99 INTN
100 Mtftp4GetNextBlockNum (
101 IN LIST_ENTRY *Head
102 )
103 {
104 MTFTP4_BLOCK_RANGE *Range;
105
106 if (IsListEmpty (Head)) {
107 return -1;
108 }
109
110 Range = NET_LIST_HEAD (Head, MTFTP4_BLOCK_RANGE, Link);
111 return Range->Start;
112 }
113
114
115 /**
116 Set the last block number of the block range list.
117
118 It will remove all the blocks after the Last. MTFTP initialize the block range
119 to the maximum possible range, such as [0, 0xffff] for WRQ. When it gets the
120 last block number, it will call this function to set the last block number.
121
122 @param Head The block range list
123 @param Last The last block number
124
125 **/
126 VOID
127 Mtftp4SetLastBlockNum (
128 IN LIST_ENTRY *Head,
129 IN UINT16 Last
130 )
131 {
132 MTFTP4_BLOCK_RANGE *Range;
133
134 //
135 // Iterate from the tail to head to remove the block number
136 // after the last.
137 //
138 while (!IsListEmpty (Head)) {
139 Range = NET_LIST_TAIL (Head, MTFTP4_BLOCK_RANGE, Link);
140
141 if (Range->Start > Last) {
142 RemoveEntryList (&Range->Link);
143 FreePool (Range);
144 continue;
145 }
146
147 if (Range->End > Last) {
148 Range->End = Last;
149 }
150
151 return ;
152 }
153 }
154
155
156 /**
157 Remove the block number from the block range list.
158
159 @param Head The block range list to remove from
160 @param Num The block number to remove
161 @param Completed Whether Num is the last block number
162 @param TotalBlock The continuous block number in all
163
164 @retval EFI_NOT_FOUND The block number isn't in the block range list
165 @retval EFI_SUCCESS The block number has been removed from the list
166 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource
167
168 **/
169 EFI_STATUS
170 Mtftp4RemoveBlockNum (
171 IN LIST_ENTRY *Head,
172 IN UINT16 Num,
173 IN BOOLEAN Completed,
174 OUT UINT64 *TotalBlock
175 )
176 {
177 MTFTP4_BLOCK_RANGE *Range;
178 MTFTP4_BLOCK_RANGE *NewRange;
179 LIST_ENTRY *Entry;
180
181 NET_LIST_FOR_EACH (Entry, Head) {
182
183 //
184 // Each block represents a hole [Start, End] in the file,
185 // skip to the first range with End >= Num
186 //
187 Range = NET_LIST_USER_STRUCT (Entry, MTFTP4_BLOCK_RANGE, Link);
188
189 if (Range->End < Num) {
190 continue;
191 }
192
193 //
194 // There are three different cases for Start
195 // 1. (Start > Num) && (End >= Num):
196 // because all the holes before this one has the condition of
197 // End < Num, so this block number has been removed.
198 //
199 // 2. (Start == Num) && (End >= Num):
200 // Need to increase the Start by one, and if End == Num, this
201 // hole has been removed completely, remove it.
202 //
203 // 3. (Start < Num) && (End >= Num):
204 // if End == Num, only need to decrease the End by one because
205 // we have (Start < Num) && (Num == End), so (Start <= End - 1).
206 // if (End > Num), the hold is splited into two holes, with
207 // [Start, Num - 1] and [Num + 1, End].
208 //
209 if (Range->Start > Num) {
210 return EFI_NOT_FOUND;
211
212 } else if (Range->Start == Num) {
213 Range->Start++;
214
215 //
216 // Note that: RFC 1350 does not mention block counter roll-over,
217 // but several TFTP hosts implement the roll-over be able to accept
218 // transfers of unlimited size. There is no consensus, however, whether
219 // the counter should wrap around to zero or to one. Many implementations
220 // wrap to zero, because this is the simplest to implement. Here we choose
221 // this solution.
222 //
223 *TotalBlock = Num;
224
225 if (Range->Round > 0) {
226 *TotalBlock += Range->Bound + MultU64x32 ((UINTN) (Range->Round -1), (UINT32) (Range->Bound + 1)) + 1;
227 }
228
229 if (Range->Start > Range->Bound) {
230 Range->Start = 0;
231 Range->Round ++;
232 }
233
234 if ((Range->Start > Range->End) || Completed) {
235 RemoveEntryList (&Range->Link);
236 FreePool (Range);
237 }
238
239 return EFI_SUCCESS;
240
241 } else {
242 if (Range->End == Num) {
243 Range->End--;
244 } else {
245 NewRange = Mtftp4AllocateRange ((UINT16) (Num + 1), (UINT16) Range->End);
246
247 if (NewRange == NULL) {
248 return EFI_OUT_OF_RESOURCES;
249 }
250
251 Range->End = Num - 1;
252 NetListInsertAfter (&Range->Link, &NewRange->Link);
253 }
254
255 return EFI_SUCCESS;
256 }
257 }
258
259 return EFI_NOT_FOUND;
260 }
261
262
263 /**
264 Build then transmit the request packet for the MTFTP session.
265
266 @param Instance The Mtftp session
267
268 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request
269 @retval EFI_SUCCESS The request is built and sent
270 @retval Others Failed to transmit the packet.
271
272 **/
273 EFI_STATUS
274 Mtftp4SendRequest (
275 IN MTFTP4_PROTOCOL *Instance
276 )
277 {
278 EFI_MTFTP4_PACKET *Packet;
279 EFI_MTFTP4_OPTION *Options;
280 EFI_MTFTP4_TOKEN *Token;
281 NET_BUF *Nbuf;
282 UINT8 *Mode;
283 UINT8 *Cur;
284 UINT32 Len;
285 UINTN Index;
286 UINT32 Len1;
287 UINT32 Len2;
288
289 Token = Instance->Token;
290 Options = Token->OptionList;
291 Mode = Instance->Token->ModeStr;
292
293 if (Mode == NULL) {
294 Mode = (UINT8 *) "octet";
295 }
296
297 //
298 // Compute the packet length
299 //
300 Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Token->Filename);
301 Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Mode);
302 Len = (Len1 + Len2 + 4);
303
304 for (Index = 0; Index < Token->OptionCount; Index++) {
305 Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);
306 Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);
307 Len += Len1 + Len2 + 2;
308 }
309
310 //
311 // Allocate a packet then copy the data over
312 //
313 if ((Nbuf = NetbufAlloc (Len)) == NULL) {
314 return EFI_OUT_OF_RESOURCES;
315 }
316
317 Packet = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);
318 ASSERT (Packet != NULL);
319
320 Packet->OpCode = HTONS (Instance->Operation);
321 Cur = Packet->Rrq.Filename;
322 Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2, (CHAR8 *) Token->Filename);
323 Cur += AsciiStrLen ((CHAR8 *) Token->Filename) + 1;
324 Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2 - (AsciiStrLen ((CHAR8 *) Token->Filename) + 1), (CHAR8 *) Mode);
325 Cur += AsciiStrLen ((CHAR8 *) Mode) + 1;
326 Len -= ((UINT32) AsciiStrLen ((CHAR8 *) Token->Filename) + (UINT32) AsciiStrLen ((CHAR8 *) Mode) + 4);
327
328 for (Index = 0; Index < Token->OptionCount; ++Index) {
329 Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].OptionStr);
330 Cur += AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1;
331 Len -= (AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1);
332
333 Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].ValueStr);
334 Cur += AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1;
335 Len -= (AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1);
336 }
337
338 return Mtftp4SendPacket (Instance, Nbuf);
339 }
340
341
342 /**
343 Build then send an error message.
344
345 @param Instance The MTFTP session
346 @param ErrCode The error code
347 @param ErrInfo The error message
348
349 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
350 @retval EFI_SUCCESS The error packet is transmitted.
351 @retval Others Failed to transmit the packet.
352
353 **/
354 EFI_STATUS
355 Mtftp4SendError (
356 IN MTFTP4_PROTOCOL *Instance,
357 IN UINT16 ErrCode,
358 IN UINT8 *ErrInfo
359 )
360 {
361 NET_BUF *Packet;
362 EFI_MTFTP4_PACKET *TftpError;
363 UINT32 Len;
364
365 Len = (UINT32) (AsciiStrLen ((CHAR8 *) ErrInfo) + sizeof (EFI_MTFTP4_ERROR_HEADER));
366 Packet = NetbufAlloc (Len);
367 if (Packet == NULL) {
368 return EFI_OUT_OF_RESOURCES;
369 }
370
371 TftpError = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (Packet, Len, FALSE);
372 ASSERT (TftpError != NULL);
373
374 TftpError->OpCode = HTONS (EFI_MTFTP4_OPCODE_ERROR);
375 TftpError->Error.ErrorCode = HTONS (ErrCode);
376
377 AsciiStrCpyS ((CHAR8 *) TftpError->Error.ErrorMessage, Len, (CHAR8 *) ErrInfo);
378
379 return Mtftp4SendPacket (Instance, Packet);
380 }
381
382
383 /**
384 The callback function called when the packet is transmitted.
385
386 It simply frees the packet.
387
388 @param Packet The transmitted (or failed to) packet
389 @param EndPoint The local and remote UDP access point
390 @param IoStatus The result of the transmission
391 @param Context Opaque parameter to the callback
392
393 **/
394 VOID
395 EFIAPI
396 Mtftp4OnPacketSent (
397 IN NET_BUF *Packet,
398 IN UDP_END_POINT *EndPoint,
399 IN EFI_STATUS IoStatus,
400 IN VOID *Context
401 )
402 {
403 NetbufFree (Packet);
404 }
405
406
407 /**
408 Set the timeout for the instance. User a longer time for passive instances.
409
410 @param Instance The Mtftp session to set time out
411
412 **/
413 VOID
414 Mtftp4SetTimeout (
415 IN OUT MTFTP4_PROTOCOL *Instance
416 )
417 {
418 if (Instance->Master) {
419 Instance->PacketToLive = Instance->Timeout;
420 } else {
421 Instance->PacketToLive = Instance->Timeout * 2;
422 }
423 }
424
425
426 /**
427 Send the packet for the instance.
428
429 It will first save a reference to the packet for later retransmission.
430 Then determine the destination port, listen port for requests, and connected
431 port for others. At last, send the packet out.
432
433 @param Instance The Mtftp instance
434 @param Packet The packet to send
435
436 @retval EFI_SUCCESS The packet is sent out
437 @retval Others Failed to transmit the packet.
438
439 **/
440 EFI_STATUS
441 Mtftp4SendPacket (
442 IN OUT MTFTP4_PROTOCOL *Instance,
443 IN OUT NET_BUF *Packet
444 )
445 {
446 UDP_END_POINT UdpPoint;
447 EFI_STATUS Status;
448 UINT16 OpCode;
449 UINT8 *Buffer;
450
451 //
452 // Save the packet for retransmission
453 //
454 if (Instance->LastPacket != NULL) {
455 NetbufFree (Instance->LastPacket);
456 }
457
458 Instance->LastPacket = Packet;
459
460 Instance->CurRetry = 0;
461 Mtftp4SetTimeout (Instance);
462
463 ZeroMem (&UdpPoint, sizeof (UdpPoint));
464 UdpPoint.RemoteAddr.Addr[0] = Instance->ServerIp;
465
466 //
467 // Send the requests to the listening port, other packets
468 // to the connected port
469 //
470 Buffer = NetbufGetByte (Packet, 0, NULL);
471 ASSERT (Buffer != NULL);
472 OpCode = NTOHS (*(UINT16 *)Buffer);
473
474 if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) ||
475 (OpCode == EFI_MTFTP4_OPCODE_DIR) ||
476 (OpCode == EFI_MTFTP4_OPCODE_WRQ)) {
477 UdpPoint.RemotePort = Instance->ListeningPort;
478 } else {
479 UdpPoint.RemotePort = Instance->ConnectedPort;
480 }
481
482 NET_GET_REF (Packet);
483
484 Status = UdpIoSendDatagram (
485 Instance->UnicastPort,
486 Packet,
487 &UdpPoint,
488 NULL,
489 Mtftp4OnPacketSent,
490 Instance
491 );
492
493 if (EFI_ERROR (Status)) {
494 NET_PUT_REF (Packet);
495 }
496
497 return Status;
498 }
499
500
501 /**
502 Retransmit the last packet for the instance.
503
504 @param Instance The Mtftp instance
505
506 @retval EFI_SUCCESS The last packet is retransmitted.
507 @retval Others Failed to retransmit.
508
509 **/
510 EFI_STATUS
511 Mtftp4Retransmit (
512 IN MTFTP4_PROTOCOL *Instance
513 )
514 {
515 UDP_END_POINT UdpPoint;
516 EFI_STATUS Status;
517 UINT16 OpCode;
518 UINT8 *Buffer;
519
520 ASSERT (Instance->LastPacket != NULL);
521
522 ZeroMem (&UdpPoint, sizeof (UdpPoint));
523 UdpPoint.RemoteAddr.Addr[0] = Instance->ServerIp;
524
525 //
526 // Set the requests to the listening port, other packets to the connected port
527 //
528 Buffer = NetbufGetByte (Instance->LastPacket, 0, NULL);
529 ASSERT (Buffer != NULL);
530 OpCode = NTOHS (*(UINT16 *) Buffer);
531
532 if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) || (OpCode == EFI_MTFTP4_OPCODE_DIR) ||
533 (OpCode == EFI_MTFTP4_OPCODE_WRQ)) {
534 UdpPoint.RemotePort = Instance->ListeningPort;
535 } else {
536 UdpPoint.RemotePort = Instance->ConnectedPort;
537 }
538
539 NET_GET_REF (Instance->LastPacket);
540
541 Status = UdpIoSendDatagram (
542 Instance->UnicastPort,
543 Instance->LastPacket,
544 &UdpPoint,
545 NULL,
546 Mtftp4OnPacketSent,
547 Instance
548 );
549
550 if (EFI_ERROR (Status)) {
551 NET_PUT_REF (Instance->LastPacket);
552 }
553
554 return Status;
555 }
556
557
558 /**
559 The timer ticking function for the Mtftp service instance.
560
561 @param Event The ticking event
562 @param Context The Mtftp service instance
563
564 **/
565 VOID
566 EFIAPI
567 Mtftp4OnTimerTick (
568 IN EFI_EVENT Event,
569 IN VOID *Context
570 )
571 {
572 MTFTP4_SERVICE *MtftpSb;
573 LIST_ENTRY *Entry;
574 LIST_ENTRY *Next;
575 MTFTP4_PROTOCOL *Instance;
576 EFI_MTFTP4_TOKEN *Token;
577
578 MtftpSb = (MTFTP4_SERVICE *) Context;
579
580 //
581 // Iterate through all the children of the Mtftp service instance. Time
582 // out the packet. If maximum retries reached, clean the session up.
583 //
584 NET_LIST_FOR_EACH_SAFE (Entry, Next, &MtftpSb->Children) {
585 Instance = NET_LIST_USER_STRUCT (Entry, MTFTP4_PROTOCOL, Link);
586
587 if ((Instance->PacketToLive == 0) || (--Instance->PacketToLive > 0)) {
588 continue;
589 }
590
591 //
592 // Call the user's time out handler
593 //
594 Token = Instance->Token;
595
596 if ((Token->TimeoutCallback != NULL) &&
597 EFI_ERROR (Token->TimeoutCallback (&Instance->Mtftp4, Token))) {
598
599 Mtftp4SendError (
600 Instance,
601 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
602 (UINT8 *) "User aborted the transfer in time out"
603 );
604
605 Mtftp4CleanOperation (Instance, EFI_ABORTED);
606 continue;
607 }
608
609 //
610 // Retransmit the packet if haven't reach the maxmium retry count,
611 // otherwise exit the transfer.
612 //
613 if (++Instance->CurRetry < Instance->MaxRetry) {
614 Mtftp4Retransmit (Instance);
615 Mtftp4SetTimeout (Instance);
616 } else {
617 Mtftp4CleanOperation (Instance, EFI_TIMEOUT);
618 continue;
619 }
620 }
621 }