]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Input.c
d985b1aeb693fc33d60c71b5671ba26bb6ea1b34
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Input.c
1 /** @file
2 IP4 input process.
3
4 Copyright (c) 2005 - 2012, 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
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 "Ip4Impl.h"
16
17
18 /**
19 Create an empty assemble entry for the packet identified by
20 (Dst, Src, Id, Protocol). The default life for the packet is
21 120 seconds.
22
23 @param[in] Dst The destination address
24 @param[in] Src The source address
25 @param[in] Id The ID field in IP header
26 @param[in] Protocol The protocol field in IP header
27
28 @return NULL if failed to allocate memory for the entry, otherwise
29 the point to just created reassemble entry.
30
31 **/
32 IP4_ASSEMBLE_ENTRY *
33 Ip4CreateAssembleEntry (
34 IN IP4_ADDR Dst,
35 IN IP4_ADDR Src,
36 IN UINT16 Id,
37 IN UINT8 Protocol
38 )
39 {
40
41 IP4_ASSEMBLE_ENTRY *Assemble;
42
43 Assemble = AllocatePool (sizeof (IP4_ASSEMBLE_ENTRY));
44
45 if (Assemble == NULL) {
46 return NULL;
47 }
48
49 InitializeListHead (&Assemble->Link);
50 InitializeListHead (&Assemble->Fragments);
51
52 Assemble->Dst = Dst;
53 Assemble->Src = Src;
54 Assemble->Id = Id;
55 Assemble->Protocol = Protocol;
56 Assemble->TotalLen = 0;
57 Assemble->CurLen = 0;
58 Assemble->Head = NULL;
59 Assemble->Info = NULL;
60 Assemble->Life = IP4_FRAGMENT_LIFE;
61
62 return Assemble;
63 }
64
65
66 /**
67 Release all the fragments of a packet, then free the assemble entry.
68
69 @param[in] Assemble The assemble entry to free
70
71 **/
72 VOID
73 Ip4FreeAssembleEntry (
74 IN IP4_ASSEMBLE_ENTRY *Assemble
75 )
76 {
77 LIST_ENTRY *Entry;
78 LIST_ENTRY *Next;
79 NET_BUF *Fragment;
80
81 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) {
82 Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
83
84 RemoveEntryList (Entry);
85 NetbufFree (Fragment);
86 }
87
88 FreePool (Assemble);
89 }
90
91
92 /**
93 Initialize an already allocated assemble table. This is generally
94 the assemble table embedded in the IP4 service instance.
95
96 @param[in, out] Table The assemble table to initialize.
97
98 **/
99 VOID
100 Ip4InitAssembleTable (
101 IN OUT IP4_ASSEMBLE_TABLE *Table
102 )
103 {
104 UINT32 Index;
105
106 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
107 InitializeListHead (&Table->Bucket[Index]);
108 }
109 }
110
111
112 /**
113 Clean up the assemble table: remove all the fragments
114 and assemble entries.
115
116 @param[in] Table The assemble table to clean up
117
118 **/
119 VOID
120 Ip4CleanAssembleTable (
121 IN IP4_ASSEMBLE_TABLE *Table
122 )
123 {
124 LIST_ENTRY *Entry;
125 LIST_ENTRY *Next;
126 IP4_ASSEMBLE_ENTRY *Assemble;
127 UINT32 Index;
128
129 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
130 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Table->Bucket[Index]) {
131 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);
132
133 RemoveEntryList (Entry);
134 Ip4FreeAssembleEntry (Assemble);
135 }
136 }
137 }
138
139
140 /**
141 Trim the packet to fit in [Start, End), and update the per
142 packet information.
143
144 @param Packet Packet to trim
145 @param Start The sequence of the first byte to fit in
146 @param End One beyond the sequence of last byte to fit in.
147
148 **/
149 VOID
150 Ip4TrimPacket (
151 IN OUT NET_BUF *Packet,
152 IN INTN Start,
153 IN INTN End
154 )
155 {
156 IP4_CLIP_INFO *Info;
157 INTN Len;
158
159 Info = IP4_GET_CLIP_INFO (Packet);
160
161 ASSERT (Info->Start + Info->Length == Info->End);
162 ASSERT ((Info->Start < End) && (Start < Info->End));
163
164 if (Info->Start < Start) {
165 Len = Start - Info->Start;
166
167 NetbufTrim (Packet, (UINT32) Len, NET_BUF_HEAD);
168 Info->Start = Start;
169 Info->Length -= Len;
170 }
171
172 if (End < Info->End) {
173 Len = End - Info->End;
174
175 NetbufTrim (Packet, (UINT32) Len, NET_BUF_TAIL);
176 Info->End = End;
177 Info->Length -= Len;
178 }
179 }
180
181
182 /**
183 Release all the fragments of the packet. This is the callback for
184 the assembled packet's OnFree. It will free the assemble entry,
185 which in turn will free all the fragments of the packet.
186
187 @param[in] Arg The assemble entry to free
188
189 **/
190 VOID
191 EFIAPI
192 Ip4OnFreeFragments (
193 IN VOID *Arg
194 )
195 {
196 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY *) Arg);
197 }
198
199
200 /**
201 Reassemble the IP fragments. If all the fragments of the packet
202 have been received, it will wrap the packet in a net buffer then
203 return it to caller. If the packet can't be assembled, NULL is
204 return.
205
206 @param Table The assemble table used. New assemble entry will be created
207 if the Packet is from a new chain of fragments.
208 @param Packet The fragment to assemble. It might be freed if the fragment
209 can't be re-assembled.
210
211 @return NULL if the packet can't be reassemble. The point to just assembled
212 packet if all the fragments of the packet have arrived.
213
214 **/
215 NET_BUF *
216 Ip4Reassemble (
217 IN OUT IP4_ASSEMBLE_TABLE *Table,
218 IN OUT NET_BUF *Packet
219 )
220 {
221 IP4_HEAD *IpHead;
222 IP4_CLIP_INFO *This;
223 IP4_CLIP_INFO *Node;
224 IP4_ASSEMBLE_ENTRY *Assemble;
225 LIST_ENTRY *Head;
226 LIST_ENTRY *Prev;
227 LIST_ENTRY *Cur;
228 NET_BUF *Fragment;
229 NET_BUF *NewPacket;
230 INTN Index;
231
232 IpHead = Packet->Ip.Ip4;
233 This = IP4_GET_CLIP_INFO (Packet);
234
235 ASSERT (IpHead != NULL);
236
237 //
238 // First: find the related assemble entry
239 //
240 Assemble = NULL;
241 Index = IP4_ASSEMBLE_HASH (IpHead->Dst, IpHead->Src, IpHead->Id, IpHead->Protocol);
242
243 NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) {
244 Assemble = NET_LIST_USER_STRUCT (Cur, IP4_ASSEMBLE_ENTRY, Link);
245
246 if ((Assemble->Dst == IpHead->Dst) && (Assemble->Src == IpHead->Src) &&
247 (Assemble->Id == IpHead->Id) && (Assemble->Protocol == IpHead->Protocol)) {
248 break;
249 }
250 }
251
252 //
253 // Create a new assemble entry if no assemble entry is related to this packet
254 //
255 if (Cur == &Table->Bucket[Index]) {
256 Assemble = Ip4CreateAssembleEntry (
257 IpHead->Dst,
258 IpHead->Src,
259 IpHead->Id,
260 IpHead->Protocol
261 );
262
263 if (Assemble == NULL) {
264 goto DROP;
265 }
266
267 InsertHeadList (&Table->Bucket[Index], &Assemble->Link);
268 }
269 //
270 // Assemble shouldn't be NULL here
271 //
272 ASSERT (Assemble != NULL);
273
274 //
275 // Find the point to insert the packet: before the first
276 // fragment with THIS.Start < CUR.Start. the previous one
277 // has PREV.Start <= THIS.Start < CUR.Start.
278 //
279 Head = &Assemble->Fragments;
280
281 NET_LIST_FOR_EACH (Cur, Head) {
282 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
283
284 if (This->Start < IP4_GET_CLIP_INFO (Fragment)->Start) {
285 break;
286 }
287 }
288
289 //
290 // Check whether the current fragment overlaps with the previous one.
291 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
292 // check whether THIS.Start < PREV.End for overlap. If two fragments
293 // overlaps, trim the overlapped part off THIS fragment.
294 //
295 if ((Cur != Head) && ((Prev = Cur->BackLink) != Head)) {
296 Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);
297 Node = IP4_GET_CLIP_INFO (Fragment);
298
299 if (This->Start < Node->End) {
300 if (This->End <= Node->End) {
301 NetbufFree (Packet);
302 return NULL;
303 }
304
305 Ip4TrimPacket (Packet, Node->End, This->End);
306 }
307 }
308
309 //
310 // Insert the fragment into the packet. The fragment may be removed
311 // from the list by the following checks.
312 //
313 NetListInsertBefore (Cur, &Packet->List);
314
315 //
316 // Check the packets after the insert point. It holds that:
317 // THIS.Start <= NODE.Start < NODE.End. The equality holds
318 // if PREV and NEXT are continuous. THIS fragment may fill
319 // several holes. Remove the completely overlapped fragments
320 //
321 while (Cur != Head) {
322 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
323 Node = IP4_GET_CLIP_INFO (Fragment);
324
325 //
326 // Remove fragments completely overlapped by this fragment
327 //
328 if (Node->End <= This->End) {
329 Cur = Cur->ForwardLink;
330
331 RemoveEntryList (&Fragment->List);
332 Assemble->CurLen -= Node->Length;
333
334 NetbufFree (Fragment);
335 continue;
336 }
337
338 //
339 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
340 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
341 // If two fragments start at the same offset, remove THIS fragment
342 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
343 //
344 if (Node->Start < This->End) {
345 if (This->Start == Node->Start) {
346 RemoveEntryList (&Packet->List);
347 goto DROP;
348 }
349
350 Ip4TrimPacket (Packet, This->Start, Node->Start);
351 }
352
353 break;
354 }
355
356 //
357 // Update the assemble info: increase the current length. If it is
358 // the frist fragment, update the packet's IP head and per packet
359 // info. If it is the last fragment, update the total length.
360 //
361 Assemble->CurLen += This->Length;
362
363 if (This->Start == 0) {
364 //
365 // Once the first fragment is enqueued, it can't be removed
366 // from the fragment list. So, Assemble->Head always point
367 // to valid memory area.
368 //
369 ASSERT (Assemble->Head == NULL);
370
371 Assemble->Head = IpHead;
372 Assemble->Info = IP4_GET_CLIP_INFO (Packet);
373 }
374
375 //
376 // Don't update the length more than once.
377 //
378 if (IP4_LAST_FRAGMENT (IpHead->Fragment) && (Assemble->TotalLen == 0)) {
379 Assemble->TotalLen = This->End;
380 }
381
382 //
383 // Deliver the whole packet if all the fragments received.
384 // All fragments received if:
385 // 1. received the last one, so, the total length is know
386 // 2. received all the data. If the last fragment on the
387 // queue ends at the total length, all data is received.
388 //
389 if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) {
390
391 RemoveEntryList (&Assemble->Link);
392
393 //
394 // If the packet is properly formated, the last fragment's End
395 // equals to the packet's total length. Otherwise, the packet
396 // is a fake, drop it now.
397 //
398 Fragment = NET_LIST_USER_STRUCT (Head->BackLink, NET_BUF, List);
399
400 if (IP4_GET_CLIP_INFO (Fragment)->End != Assemble->TotalLen) {
401 Ip4FreeAssembleEntry (Assemble);
402 return NULL;
403 }
404
405 //
406 // Wrap the packet in a net buffer then deliver it up
407 //
408 NewPacket = NetbufFromBufList (
409 &Assemble->Fragments,
410 0,
411 0,
412 Ip4OnFreeFragments,
413 Assemble
414 );
415
416 if (NewPacket == NULL) {
417 Ip4FreeAssembleEntry (Assemble);
418 return NULL;
419 }
420
421 NewPacket->Ip.Ip4 = Assemble->Head;
422
423 ASSERT (Assemble->Info != NULL);
424
425 CopyMem (
426 IP4_GET_CLIP_INFO (NewPacket),
427 Assemble->Info,
428 sizeof (*IP4_GET_CLIP_INFO (NewPacket))
429 );
430
431 return NewPacket;
432 }
433
434 return NULL;
435
436 DROP:
437 NetbufFree (Packet);
438 return NULL;
439 }
440
441 /**
442 The callback function for the net buffer which wraps the packet processed by
443 IPsec. It releases the wrap packet and also signals IPsec to free the resources.
444
445 @param[in] Arg The wrap context
446
447 **/
448 VOID
449 EFIAPI
450 Ip4IpSecFree (
451 IN VOID *Arg
452 )
453 {
454 IP4_IPSEC_WRAP *Wrap;
455
456 Wrap = (IP4_IPSEC_WRAP *) Arg;
457
458 if (Wrap->IpSecRecycleSignal != NULL) {
459 gBS->SignalEvent (Wrap->IpSecRecycleSignal);
460 }
461
462 NetbufFree (Wrap->Packet);
463
464 FreePool (Wrap);
465
466 return;
467 }
468
469 /**
470 The work function to locate IPsec protocol to process the inbound or
471 outbound IP packets. The process routine handls the packet with following
472 actions: bypass the packet, discard the packet, or protect the packet.
473
474 @param[in] IpSb The IP4 service instance.
475 @param[in, out] Head The The caller supplied IP4 header.
476 @param[in, out] Netbuf The IP4 packet to be processed by IPsec.
477 @param[in, out] Options The caller supplied options.
478 @param[in, out] OptionsLen The length of the option.
479 @param[in] Direction The directionality in an SPD entry,
480 EfiIPsecInBound or EfiIPsecOutBound.
481 @param[in] Context The token's wrap.
482
483 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
484 @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.
485 @retval EFI_SUCCESS The packet was protected.
486 @retval EFI_ACCESS_DENIED The packet was discarded.
487 @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation.
488 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the
489 number of input data blocks when build a fragment table.
490
491 **/
492 EFI_STATUS
493 Ip4IpSecProcessPacket (
494 IN IP4_SERVICE *IpSb,
495 IN OUT IP4_HEAD **Head,
496 IN OUT NET_BUF **Netbuf,
497 IN OUT UINT8 **Options,
498 IN OUT UINT32 *OptionsLen,
499 IN EFI_IPSEC_TRAFFIC_DIR Direction,
500 IN VOID *Context
501 )
502 {
503 NET_FRAGMENT *FragmentTable;
504 NET_FRAGMENT *OriginalFragmentTable;
505 UINT32 FragmentCount;
506 UINT32 OriginalFragmentCount;
507 EFI_EVENT RecycleEvent;
508 NET_BUF *Packet;
509 IP4_TXTOKEN_WRAP *TxWrap;
510 IP4_IPSEC_WRAP *IpSecWrap;
511 EFI_STATUS Status;
512 IP4_HEAD ZeroHead;
513
514 Status = EFI_SUCCESS;
515 Packet = *Netbuf;
516 RecycleEvent = NULL;
517 IpSecWrap = NULL;
518 FragmentTable = NULL;
519 TxWrap = (IP4_TXTOKEN_WRAP *) Context;
520 FragmentCount = Packet->BlockOpNum;
521
522 ZeroMem (&ZeroHead, sizeof (IP4_HEAD));
523
524 if (mIpSec == NULL) {
525 gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **) &mIpSec);
526 if (mIpSec == NULL) {
527 goto ON_EXIT;
528 }
529 }
530
531 //
532 // Check whether the IPsec enable variable is set.
533 //
534 if (mIpSec->DisabledFlag) {
535 //
536 // If IPsec is disabled, restore the original MTU
537 //
538 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize;
539 goto ON_EXIT;
540 } else {
541 //
542 // If IPsec is enabled, use the MTU which reduce the IPsec header length.
543 //
544 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP4_MAX_IPSEC_HEADLEN;
545 }
546
547 //
548 // Rebuild fragment table from netbuf to ease IPsec process.
549 //
550 FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT));
551
552 if (FragmentTable == NULL) {
553 Status = EFI_OUT_OF_RESOURCES;
554 goto ON_EXIT;
555 }
556
557 Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount);
558
559 //
560 // Record the original FragmentTable and count.
561 //
562 OriginalFragmentTable = FragmentTable;
563 OriginalFragmentCount = FragmentCount;
564
565 if (EFI_ERROR (Status)) {
566 FreePool (FragmentTable);
567 goto ON_EXIT;
568 }
569
570 //
571 // Convert host byte order to network byte order
572 //
573 Ip4NtohHead (*Head);
574
575 Status = mIpSec->ProcessExt (
576 mIpSec,
577 IpSb->Controller,
578 IP_VERSION_4,
579 (VOID *) (*Head),
580 &(*Head)->Protocol,
581 (VOID **) Options,
582 OptionsLen,
583 (EFI_IPSEC_FRAGMENT_DATA **) (&FragmentTable),
584 &FragmentCount,
585 Direction,
586 &RecycleEvent
587 );
588 //
589 // Convert back to host byte order
590 //
591 Ip4NtohHead (*Head);
592
593 if (EFI_ERROR (Status)) {
594 FreePool (OriginalFragmentTable);
595 goto ON_EXIT;
596 }
597
598 if (OriginalFragmentTable == FragmentTable && OriginalFragmentCount == FragmentCount) {
599 //
600 // For ByPass Packet
601 //
602 FreePool (FragmentTable);
603 goto ON_EXIT;
604 } else {
605 //
606 // Free the FragmentTable which allocated before calling the IPsec.
607 //
608 FreePool (OriginalFragmentTable);
609 }
610
611 if (Direction == EfiIPsecOutBound && TxWrap != NULL) {
612
613 TxWrap->IpSecRecycleSignal = RecycleEvent;
614 TxWrap->Packet = NetbufFromExt (
615 FragmentTable,
616 FragmentCount,
617 IP4_MAX_HEADLEN,
618 0,
619 Ip4FreeTxToken,
620 TxWrap
621 );
622 if (TxWrap->Packet == NULL) {
623 //
624 // Recover the TxWrap->Packet, if meet a error, and the caller will free
625 // the TxWrap.
626 //
627 TxWrap->Packet = *Netbuf;
628 Status = EFI_OUT_OF_RESOURCES;
629 goto ON_EXIT;
630 }
631
632 //
633 // Free orginal Netbuf.
634 //
635 NetIpSecNetbufFree (*Netbuf);
636 *Netbuf = TxWrap->Packet;
637
638 } else {
639
640 IpSecWrap = AllocateZeroPool (sizeof (IP4_IPSEC_WRAP));
641
642 if (IpSecWrap == NULL) {
643 Status = EFI_OUT_OF_RESOURCES;
644 gBS->SignalEvent (RecycleEvent);
645 goto ON_EXIT;
646 }
647
648 IpSecWrap->IpSecRecycleSignal = RecycleEvent;
649 IpSecWrap->Packet = Packet;
650 Packet = NetbufFromExt (
651 FragmentTable,
652 FragmentCount,
653 IP4_MAX_HEADLEN,
654 0,
655 Ip4IpSecFree,
656 IpSecWrap
657 );
658
659 if (Packet == NULL) {
660 Packet = IpSecWrap->Packet;
661 gBS->SignalEvent (RecycleEvent);
662 FreePool (IpSecWrap);
663 Status = EFI_OUT_OF_RESOURCES;
664 goto ON_EXIT;
665 }
666
667 if (Direction == EfiIPsecInBound && 0 != CompareMem (*Head, &ZeroHead, sizeof (IP4_HEAD))) {
668 Ip4PrependHead (Packet, *Head, *Options, *OptionsLen);
669 Ip4NtohHead (Packet->Ip.Ip4);
670 NetbufTrim (Packet, ((*Head)->HeadLen << 2), TRUE);
671
672 CopyMem (
673 IP4_GET_CLIP_INFO (Packet),
674 IP4_GET_CLIP_INFO (IpSecWrap->Packet),
675 sizeof (IP4_CLIP_INFO)
676 );
677 }
678 *Netbuf = Packet;
679 }
680
681 ON_EXIT:
682 return Status;
683 }
684
685 /**
686 Pre-process the IPv4 packet. First validates the IPv4 packet, and
687 then reassembles packet if it is necessary.
688
689 @param[in] IpSb Pointer to IP4_SERVICE.
690 @param[in, out] Packet Pointer to the Packet to be processed.
691 @param[in] Head Pointer to the IP4_HEAD.
692 @param[in] Option Pointer to a buffer which contains the IPv4 option.
693 @param[in] OptionLen The length of Option in bytes.
694 @param[in] Flag The link layer flag for the packet received, such
695 as multicast.
696
697 @retval EFI_SEUCCESS The recieved packet is in well form.
698 @retval EFI_INVAILD_PARAMETER The recieved packet is malformed.
699
700 **/
701 EFI_STATUS
702 Ip4PreProcessPacket (
703 IN IP4_SERVICE *IpSb,
704 IN OUT NET_BUF **Packet,
705 IN IP4_HEAD *Head,
706 IN UINT8 *Option,
707 IN UINT32 OptionLen,
708 IN UINT32 Flag
709 )
710 {
711 IP4_CLIP_INFO *Info;
712 UINT32 HeadLen;
713 UINT32 TotalLen;
714 UINT16 Checksum;
715
716 //
717 // Check that the IP4 header is correctly formatted
718 //
719 if ((*Packet)->TotalSize < IP4_MIN_HEADLEN) {
720 return EFI_INVALID_PARAMETER;
721 }
722
723 HeadLen = (Head->HeadLen << 2);
724 TotalLen = NTOHS (Head->TotalLen);
725
726 //
727 // Mnp may deliver frame trailer sequence up, trim it off.
728 //
729 if (TotalLen < (*Packet)->TotalSize) {
730 NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE);
731 }
732
733 if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||
734 (TotalLen < HeadLen) || (TotalLen != (*Packet)->TotalSize)) {
735 return EFI_INVALID_PARAMETER;
736 }
737
738 //
739 // Some OS may send IP packets without checksum.
740 //
741 Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen));
742
743 if ((Head->Checksum != 0) && (Checksum != 0)) {
744 return EFI_INVALID_PARAMETER;
745 }
746
747 //
748 // Convert the IP header to host byte order, then get the per packet info.
749 //
750 (*Packet)->Ip.Ip4 = Ip4NtohHead (Head);
751
752 Info = IP4_GET_CLIP_INFO (*Packet);
753 Info->LinkFlag = Flag;
754 Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);
755 Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;
756 Info->Length = Head->TotalLen - HeadLen;
757 Info->End = Info->Start + Info->Length;
758 Info->Status = EFI_SUCCESS;
759
760 //
761 // The packet is destinated to us if the CastType is non-zero.
762 //
763 if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {
764 return EFI_INVALID_PARAMETER;
765 }
766
767 //
768 // Validate the options. Don't call the Ip4OptionIsValid if
769 // there is no option to save some CPU process.
770 //
771
772 if ((OptionLen > 0) && !Ip4OptionIsValid (Option, OptionLen, TRUE)) {
773 return EFI_INVALID_PARAMETER;
774 }
775
776 //
777 // Trim the head off, after this point, the packet is headless.
778 // and Packet->TotalLen == Info->Length.
779 //
780 NetbufTrim (*Packet, HeadLen, TRUE);
781
782 //
783 // Reassemble the packet if this is a fragment. The packet is a
784 // fragment if its head has MF (more fragment) set, or it starts
785 // at non-zero byte.
786 //
787 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) || (Info->Start != 0)) {
788 //
789 // Drop the fragment if DF is set but it is fragmented. Gateway
790 // need to send a type 4 destination unreache ICMP message here.
791 //
792 if ((Head->Fragment & IP4_HEAD_DF_MASK) != 0) {
793 return EFI_INVALID_PARAMETER;
794 }
795
796 //
797 // The length of all but the last fragments is in the unit of 8 bytes.
798 //
799 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) && (Info->Length % 8 != 0)) {
800 return EFI_INVALID_PARAMETER;
801 }
802
803 *Packet = Ip4Reassemble (&IpSb->Assemble, *Packet);
804
805 //
806 // Packet assembly isn't complete, start receive more packet.
807 //
808 if (*Packet == NULL) {
809 return EFI_INVALID_PARAMETER;
810 }
811 }
812
813 return EFI_SUCCESS;
814 }
815
816 /**
817 The IP4 input routine. It is called by the IP4_INTERFACE when a
818 IP4 fragment is received from MNP.
819
820 @param[in] Ip4Instance The IP4 child that request the receive, most like
821 it is NULL.
822 @param[in] Packet The IP4 packet received.
823 @param[in] IoStatus The return status of receive request.
824 @param[in] Flag The link layer flag for the packet received, such
825 as multicast.
826 @param[in] Context The IP4 service instance that own the MNP.
827
828 **/
829 VOID
830 Ip4AccpetFrame (
831 IN IP4_PROTOCOL *Ip4Instance,
832 IN NET_BUF *Packet,
833 IN EFI_STATUS IoStatus,
834 IN UINT32 Flag,
835 IN VOID *Context
836 )
837 {
838 IP4_SERVICE *IpSb;
839 IP4_HEAD *Head;
840 EFI_STATUS Status;
841 IP4_HEAD ZeroHead;
842 UINT8 *Option;
843 UINT32 OptionLen;
844
845 IpSb = (IP4_SERVICE *) Context;
846 Option = NULL;
847
848 if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTROY)) {
849 goto DROP;
850 }
851
852 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);
853 ASSERT (Head != NULL);
854 OptionLen = (Head->HeadLen << 2) - IP4_MIN_HEADLEN;
855 if (OptionLen > 0) {
856 Option = (UINT8 *) (Head + 1);
857 }
858
859 //
860 // Validate packet format and reassemble packet if it is necessary.
861 //
862 Status = Ip4PreProcessPacket (
863 IpSb,
864 &Packet,
865 Head,
866 Option,
867 OptionLen,
868 Flag
869 );
870
871 if (EFI_ERROR (Status)) {
872 goto RESTART;
873 }
874
875 //
876 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
877 // and no need consider any other ahead ext headers.
878 //
879 Status = Ip4IpSecProcessPacket (
880 IpSb,
881 &Head,
882 &Packet,
883 &Option,
884 &OptionLen,
885 EfiIPsecInBound,
886 NULL
887 );
888
889 if (EFI_ERROR (Status)) {
890 goto RESTART;
891 }
892
893 //
894 // If the packet is protected by tunnel mode, parse the inner Ip Packet.
895 //
896 ZeroMem (&ZeroHead, sizeof (IP4_HEAD));
897 if (0 == CompareMem (Head, &ZeroHead, sizeof (IP4_HEAD))) {
898 // Packet may have been changed. Head, HeadLen, TotalLen, and
899 // info must be reloaded bofore use. The ownership of the packet
900 // is transfered to the packet process logic.
901 //
902 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);
903 ASSERT (Head != NULL);
904 Status = Ip4PreProcessPacket (
905 IpSb,
906 &Packet,
907 Head,
908 Option,
909 OptionLen,
910 Flag
911 );
912 if (EFI_ERROR (Status)) {
913 goto RESTART;
914 }
915 }
916
917 ASSERT (Packet != NULL);
918 Head = Packet->Ip.Ip4;
919 IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;
920
921 switch (Head->Protocol) {
922 case EFI_IP_PROTO_ICMP:
923 Ip4IcmpHandle (IpSb, Head, Packet);
924 break;
925
926 case IP4_PROTO_IGMP:
927 Ip4IgmpHandle (IpSb, Head, Packet);
928 break;
929
930 default:
931 Ip4Demultiplex (IpSb, Head, Packet);
932 }
933
934 Packet = NULL;
935
936 //
937 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
938 // which are signaled with received data.
939 //
940 DispatchDpc ();
941
942 RESTART:
943 Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
944
945 DROP:
946 if (Packet != NULL) {
947 NetbufFree (Packet);
948 }
949
950 return ;
951 }
952
953
954 /**
955 Check whether this IP child accepts the packet.
956
957 @param[in] IpInstance The IP child to check
958 @param[in] Head The IP header of the packet
959 @param[in] Packet The data of the packet
960
961 @retval TRUE If the child wants to receive the packet.
962 @retval FALSE Otherwise.
963
964 **/
965 BOOLEAN
966 Ip4InstanceFrameAcceptable (
967 IN IP4_PROTOCOL *IpInstance,
968 IN IP4_HEAD *Head,
969 IN NET_BUF *Packet
970 )
971 {
972 IP4_ICMP_ERROR_HEAD Icmp;
973 EFI_IP4_CONFIG_DATA *Config;
974 IP4_CLIP_INFO *Info;
975 UINT16 Proto;
976 UINT32 Index;
977
978 Config = &IpInstance->ConfigData;
979
980 //
981 // Dirty trick for the Tiano UEFI network stack implmentation. If
982 // ReceiveTimeout == -1, the receive of the packet for this instance
983 // is disabled. The UEFI spec don't have such capability. We add
984 // this to improve the performance because IP will make a copy of
985 // the received packet for each accepting instance. Some IP instances
986 // used by UDP/TCP only send packets, they don't wants to receive.
987 //
988 if (Config->ReceiveTimeout == (UINT32)(-1)) {
989 return FALSE;
990 }
991
992 if (Config->AcceptPromiscuous) {
993 return TRUE;
994 }
995
996 //
997 // Use protocol from the IP header embedded in the ICMP error
998 // message to filter, instead of ICMP itself. ICMP handle will
999 // call Ip4Demultiplex to deliver ICMP errors.
1000 //
1001 Proto = Head->Protocol;
1002
1003 if ((Proto == EFI_IP_PROTO_ICMP) && (!Config->AcceptAnyProtocol) && (Proto != Config->DefaultProtocol)) {
1004 NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);
1005
1006 if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {
1007 if (!Config->AcceptIcmpErrors) {
1008 return FALSE;
1009 }
1010
1011 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1012 Proto = Icmp.IpHead.Protocol;
1013 }
1014 }
1015
1016 //
1017 // Match the protocol
1018 //
1019 if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {
1020 return FALSE;
1021 }
1022
1023 //
1024 // Check for broadcast, the caller has computed the packet's
1025 // cast type for this child's interface.
1026 //
1027 Info = IP4_GET_CLIP_INFO (Packet);
1028
1029 if (IP4_IS_BROADCAST (Info->CastType)) {
1030 return Config->AcceptBroadcast;
1031 }
1032
1033 //
1034 // If it is a multicast packet, check whether we are in the group.
1035 //
1036 if (Info->CastType == IP4_MULTICAST) {
1037 //
1038 // Receive the multicast if the instance wants to receive all packets.
1039 //
1040 if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {
1041 return TRUE;
1042 }
1043
1044 for (Index = 0; Index < IpInstance->GroupCount; Index++) {
1045 if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {
1046 break;
1047 }
1048 }
1049
1050 return (BOOLEAN)(Index < IpInstance->GroupCount);
1051 }
1052
1053 return TRUE;
1054 }
1055
1056
1057 /**
1058 Enqueue a shared copy of the packet to the IP4 child if the
1059 packet is acceptable to it. Here the data of the packet is
1060 shared, but the net buffer isn't.
1061
1062 @param[in] IpInstance The IP4 child to enqueue the packet to
1063 @param[in] Head The IP header of the received packet
1064 @param[in] Packet The data of the received packet
1065
1066 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1067 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
1068 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
1069 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1070
1071 **/
1072 EFI_STATUS
1073 Ip4InstanceEnquePacket (
1074 IN IP4_PROTOCOL *IpInstance,
1075 IN IP4_HEAD *Head,
1076 IN NET_BUF *Packet
1077 )
1078 {
1079 IP4_CLIP_INFO *Info;
1080 NET_BUF *Clone;
1081
1082 //
1083 // Check whether the packet is acceptable to this instance.
1084 //
1085 if (IpInstance->State != IP4_STATE_CONFIGED) {
1086 return EFI_NOT_STARTED;
1087 }
1088
1089 if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {
1090 return EFI_INVALID_PARAMETER;
1091 }
1092
1093 //
1094 // Enque a shared copy of the packet.
1095 //
1096 Clone = NetbufClone (Packet);
1097
1098 if (Clone == NULL) {
1099 return EFI_OUT_OF_RESOURCES;
1100 }
1101
1102 //
1103 // Set the receive time out for the assembled packet. If it expires,
1104 // packet will be removed from the queue.
1105 //
1106 Info = IP4_GET_CLIP_INFO (Clone);
1107 Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);
1108
1109 InsertTailList (&IpInstance->Received, &Clone->List);
1110 return EFI_SUCCESS;
1111 }
1112
1113
1114 /**
1115 The signal handle of IP4's recycle event. It is called back
1116 when the upper layer release the packet.
1117
1118 @param Event The IP4's recycle event.
1119 @param Context The context of the handle, which is a
1120 IP4_RXDATA_WRAP
1121
1122 **/
1123 VOID
1124 EFIAPI
1125 Ip4OnRecyclePacket (
1126 IN EFI_EVENT Event,
1127 IN VOID *Context
1128 )
1129 {
1130 IP4_RXDATA_WRAP *Wrap;
1131
1132 Wrap = (IP4_RXDATA_WRAP *) Context;
1133
1134 EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock);
1135 RemoveEntryList (&Wrap->Link);
1136 EfiReleaseLock (&Wrap->IpInstance->RecycleLock);
1137
1138 ASSERT (!NET_BUF_SHARED (Wrap->Packet));
1139 NetbufFree (Wrap->Packet);
1140
1141 gBS->CloseEvent (Wrap->RxData.RecycleSignal);
1142 FreePool (Wrap);
1143 }
1144
1145
1146 /**
1147 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1148 delivered to the upper layer. Each IP4 child that accepts the
1149 packet will get a not-shared copy of the packet which is wrapped
1150 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1151 to the upper layer. Upper layer will signal the recycle event in
1152 it when it is done with the packet.
1153
1154 @param[in] IpInstance The IP4 child to receive the packet
1155 @param[in] Packet The packet to deliver up.
1156
1157 @retval Wrap if warp the packet succeed.
1158 @retval NULL failed to wrap the packet .
1159
1160 **/
1161 IP4_RXDATA_WRAP *
1162 Ip4WrapRxData (
1163 IN IP4_PROTOCOL *IpInstance,
1164 IN NET_BUF *Packet
1165 )
1166 {
1167 IP4_RXDATA_WRAP *Wrap;
1168 EFI_IP4_RECEIVE_DATA *RxData;
1169 EFI_STATUS Status;
1170
1171 Wrap = AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));
1172
1173 if (Wrap == NULL) {
1174 return NULL;
1175 }
1176
1177 InitializeListHead (&Wrap->Link);
1178
1179 Wrap->IpInstance = IpInstance;
1180 Wrap->Packet = Packet;
1181 RxData = &Wrap->RxData;
1182
1183 ZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME));
1184
1185 Status = gBS->CreateEvent (
1186 EVT_NOTIFY_SIGNAL,
1187 TPL_NOTIFY,
1188 Ip4OnRecyclePacket,
1189 Wrap,
1190 &RxData->RecycleSignal
1191 );
1192
1193 if (EFI_ERROR (Status)) {
1194 FreePool (Wrap);
1195 return NULL;
1196 }
1197
1198 ASSERT (Packet->Ip.Ip4 != NULL);
1199
1200 //
1201 // The application expects a network byte order header.
1202 //
1203 RxData->HeaderLength = (Packet->Ip.Ip4->HeadLen << 2);
1204 RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip.Ip4);
1205
1206 RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;
1207 RxData->Options = NULL;
1208
1209 if (RxData->OptionsLength != 0) {
1210 RxData->Options = (VOID *) (RxData->Header + 1);
1211 }
1212
1213 RxData->DataLength = Packet->TotalSize;
1214
1215 //
1216 // Build the fragment table to be delivered up.
1217 //
1218 RxData->FragmentCount = Packet->BlockOpNum;
1219 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);
1220
1221 return Wrap;
1222 }
1223
1224
1225 /**
1226 Deliver the received packets to upper layer if there are both received
1227 requests and enqueued packets. If the enqueued packet is shared, it will
1228 duplicate it to a non-shared packet, release the shared packet, then
1229 deliver the non-shared packet up.
1230
1231 @param[in] IpInstance The IP child to deliver the packet up.
1232
1233 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1234 packets.
1235 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1236 are delivered up.
1237
1238 **/
1239 EFI_STATUS
1240 Ip4InstanceDeliverPacket (
1241 IN IP4_PROTOCOL *IpInstance
1242 )
1243 {
1244 EFI_IP4_COMPLETION_TOKEN *Token;
1245 IP4_RXDATA_WRAP *Wrap;
1246 NET_BUF *Packet;
1247 NET_BUF *Dup;
1248 UINT8 *Head;
1249
1250 //
1251 // Deliver a packet if there are both a packet and a receive token.
1252 //
1253 while (!IsListEmpty (&IpInstance->Received) &&
1254 !NetMapIsEmpty (&IpInstance->RxTokens)) {
1255
1256 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);
1257
1258 if (!NET_BUF_SHARED (Packet)) {
1259 //
1260 // If this is the only instance that wants the packet, wrap it up.
1261 //
1262 Wrap = Ip4WrapRxData (IpInstance, Packet);
1263
1264 if (Wrap == NULL) {
1265 return EFI_OUT_OF_RESOURCES;
1266 }
1267
1268 RemoveEntryList (&Packet->List);
1269
1270 } else {
1271 //
1272 // Create a duplicated packet if this packet is shared
1273 //
1274 Dup = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN);
1275
1276 if (Dup == NULL) {
1277 return EFI_OUT_OF_RESOURCES;
1278 }
1279
1280 //
1281 // Copy the IP head over. The packet to deliver up is
1282 // headless. Trim the head off after copy. The IP head
1283 // may be not continuous before the data.
1284 //
1285 Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);
1286 ASSERT (Head != NULL);
1287
1288 Dup->Ip.Ip4 = (IP4_HEAD *) Head;
1289
1290 CopyMem (Head, Packet->Ip.Ip4, Packet->Ip.Ip4->HeadLen << 2);
1291 NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);
1292
1293 Wrap = Ip4WrapRxData (IpInstance, Dup);
1294
1295 if (Wrap == NULL) {
1296 NetbufFree (Dup);
1297 return EFI_OUT_OF_RESOURCES;
1298 }
1299
1300 RemoveEntryList (&Packet->List);
1301 NetbufFree (Packet);
1302
1303 Packet = Dup;
1304 }
1305
1306 //
1307 // Insert it into the delivered packet, then get a user's
1308 // receive token, pass the wrapped packet up.
1309 //
1310 EfiAcquireLockOrFail (&IpInstance->RecycleLock);
1311 InsertHeadList (&IpInstance->Delivered, &Wrap->Link);
1312 EfiReleaseLock (&IpInstance->RecycleLock);
1313
1314 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);
1315 Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;
1316 Token->Packet.RxData = &Wrap->RxData;
1317
1318 gBS->SignalEvent (Token->Event);
1319 }
1320
1321 return EFI_SUCCESS;
1322 }
1323
1324
1325 /**
1326 Enqueue a received packet to all the IP children that share
1327 the same interface.
1328
1329 @param[in] IpSb The IP4 service instance that receive the packet
1330 @param[in] Head The header of the received packet
1331 @param[in] Packet The data of the received packet
1332 @param[in] IpIf The interface to enqueue the packet to
1333
1334 @return The number of the IP4 children that accepts the packet
1335
1336 **/
1337 INTN
1338 Ip4InterfaceEnquePacket (
1339 IN IP4_SERVICE *IpSb,
1340 IN IP4_HEAD *Head,
1341 IN NET_BUF *Packet,
1342 IN IP4_INTERFACE *IpIf
1343 )
1344 {
1345 IP4_PROTOCOL *IpInstance;
1346 IP4_CLIP_INFO *Info;
1347 LIST_ENTRY *Entry;
1348 INTN Enqueued;
1349 INTN LocalType;
1350 INTN SavedType;
1351
1352 //
1353 // First, check that the packet is acceptable to this interface
1354 // and find the local cast type for the interface. A packet sent
1355 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1356 // promiscuous receiving.
1357 //
1358 LocalType = 0;
1359 Info = IP4_GET_CLIP_INFO (Packet);
1360
1361 if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {
1362 //
1363 // If the CastType is multicast, don't need to filter against
1364 // the group address here, Ip4InstanceFrameAcceptable will do
1365 // that later.
1366 //
1367 LocalType = Info->CastType;
1368
1369 } else {
1370 //
1371 // Check the destination againist local IP. If the station
1372 // address is 0.0.0.0, it means receiving all the IP destined
1373 // to local non-zero IP. Otherwise, it is necessary to compare
1374 // the destination to the interface's IP address.
1375 //
1376 if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {
1377 LocalType = IP4_LOCAL_HOST;
1378
1379 } else {
1380 LocalType = Ip4GetNetCast (Head->Dst, IpIf);
1381
1382 if ((LocalType == 0) && IpIf->PromiscRecv) {
1383 LocalType = IP4_PROMISCUOUS;
1384 }
1385 }
1386 }
1387
1388 if (LocalType == 0) {
1389 return 0;
1390 }
1391
1392 //
1393 // Iterate through the ip instances on the interface, enqueue
1394 // the packet if filter passed. Save the original cast type,
1395 // and pass the local cast type to the IP children on the
1396 // interface. The global cast type will be restored later.
1397 //
1398 SavedType = Info->CastType;
1399 Info->CastType = LocalType;
1400
1401 Enqueued = 0;
1402
1403 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1404 IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
1405 NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);
1406
1407 if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {
1408 Enqueued++;
1409 }
1410 }
1411
1412 Info->CastType = SavedType;
1413 return Enqueued;
1414 }
1415
1416
1417 /**
1418 Deliver the packet for each IP4 child on the interface.
1419
1420 @param[in] IpSb The IP4 service instance that received the packet
1421 @param[in] IpIf The IP4 interface to deliver the packet.
1422
1423 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1424
1425 **/
1426 EFI_STATUS
1427 Ip4InterfaceDeliverPacket (
1428 IN IP4_SERVICE *IpSb,
1429 IN IP4_INTERFACE *IpIf
1430 )
1431 {
1432 IP4_PROTOCOL *Ip4Instance;
1433 LIST_ENTRY *Entry;
1434
1435 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1436 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
1437 Ip4InstanceDeliverPacket (Ip4Instance);
1438 }
1439
1440 return EFI_SUCCESS;
1441 }
1442
1443
1444 /**
1445 Demultiple the packet. the packet delivery is processed in two
1446 passes. The first pass will enque a shared copy of the packet
1447 to each IP4 child that accepts the packet. The second pass will
1448 deliver a non-shared copy of the packet to each IP4 child that
1449 has pending receive requests. Data is copied if more than one
1450 child wants to consume the packet because each IP child needs
1451 its own copy of the packet to make changes.
1452
1453 @param[in] IpSb The IP4 service instance that received the packet
1454 @param[in] Head The header of the received packet
1455 @param[in] Packet The data of the received packet
1456
1457 @retval EFI_NOT_FOUND No IP child accepts the packet
1458 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1459 children.
1460
1461 **/
1462 EFI_STATUS
1463 Ip4Demultiplex (
1464 IN IP4_SERVICE *IpSb,
1465 IN IP4_HEAD *Head,
1466 IN NET_BUF *Packet
1467 )
1468 {
1469 LIST_ENTRY *Entry;
1470 IP4_INTERFACE *IpIf;
1471 INTN Enqueued;
1472
1473 //
1474 // Two pass delivery: first, enque a shared copy of the packet
1475 // to each instance that accept the packet.
1476 //
1477 Enqueued = 0;
1478
1479 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1480 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
1481
1482 if (IpIf->Configured) {
1483 Enqueued += Ip4InterfaceEnquePacket (IpSb, Head, Packet, IpIf);
1484 }
1485 }
1486
1487 //
1488 // Second: deliver a duplicate of the packet to each instance.
1489 // Release the local reference first, so that the last instance
1490 // getting the packet will not copy the data.
1491 //
1492 NetbufFree (Packet);
1493
1494 if (Enqueued == 0) {
1495 return EFI_NOT_FOUND;
1496 }
1497
1498 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1499 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
1500
1501 if (IpIf->Configured) {
1502 Ip4InterfaceDeliverPacket (IpSb, IpIf);
1503 }
1504 }
1505
1506 return EFI_SUCCESS;
1507 }
1508
1509
1510 /**
1511 Timeout the fragment and enqueued packets.
1512
1513 @param[in] IpSb The IP4 service instance to timeout
1514
1515 **/
1516 VOID
1517 Ip4PacketTimerTicking (
1518 IN IP4_SERVICE *IpSb
1519 )
1520 {
1521 LIST_ENTRY *InstanceEntry;
1522 LIST_ENTRY *Entry;
1523 LIST_ENTRY *Next;
1524 IP4_PROTOCOL *IpInstance;
1525 IP4_ASSEMBLE_ENTRY *Assemble;
1526 NET_BUF *Packet;
1527 IP4_CLIP_INFO *Info;
1528 UINT32 Index;
1529
1530 //
1531 // First, time out the fragments. The packet's life is counting down
1532 // once the first-arrived fragment was received.
1533 //
1534 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
1535 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {
1536 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);
1537
1538 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {
1539 RemoveEntryList (Entry);
1540 Ip4FreeAssembleEntry (Assemble);
1541 }
1542 }
1543 }
1544
1545 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {
1546 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);
1547
1548 //
1549 // Second, time out the assembled packets enqueued on each IP child.
1550 //
1551 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {
1552 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
1553 Info = IP4_GET_CLIP_INFO (Packet);
1554
1555 if ((Info->Life > 0) && (--Info->Life == 0)) {
1556 RemoveEntryList (Entry);
1557 NetbufFree (Packet);
1558 }
1559 }
1560
1561 //
1562 // Third: time out the transmitted packets.
1563 //
1564 NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);
1565 }
1566 }