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