]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Input.c
1. In IPv4 and IPv6 driver, before calling IPsec, a new NET_FRAGMENT structure is...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Input.c
1 /** @file
2 IP4 input process.
3
4 Copyright (c) 2005 - 2011, 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 CopyMem (IP4_GET_CLIP_INFO (NewPacket), Assemble->Info, sizeof (*IP4_GET_CLIP_INFO (NewPacket)));
423 return NewPacket;
424 }
425
426 return NULL;
427
428 DROP:
429 NetbufFree (Packet);
430 return NULL;
431 }
432
433 /**
434 The callback function for the net buffer which wraps the packet processed by
435 IPsec. It releases the wrap packet and also signals IPsec to free the resources.
436
437 @param[in] Arg The wrap context
438
439 **/
440 VOID
441 EFIAPI
442 Ip4IpSecFree (
443 IN VOID *Arg
444 )
445 {
446 IP4_IPSEC_WRAP *Wrap;
447
448 Wrap = (IP4_IPSEC_WRAP *) Arg;
449
450 if (Wrap->IpSecRecycleSignal != NULL) {
451 gBS->SignalEvent (Wrap->IpSecRecycleSignal);
452 }
453
454 NetbufFree (Wrap->Packet);
455
456 FreePool (Wrap);
457
458 return;
459 }
460
461 /**
462 The work function to locate IPsec protocol to process the inbound or
463 outbound IP packets. The process routine handls the packet with following
464 actions: bypass the packet, discard the packet, or protect the packet.
465
466 @param[in] IpSb The IP4 service instance.
467 @param[in, out] Head The The caller supplied IP4 header.
468 @param[in, out] Netbuf The IP4 packet to be processed by IPsec.
469 @param[in, out] Options The caller supplied options.
470 @param[in, out] OptionsLen The length of the option.
471 @param[in] Direction The directionality in an SPD entry,
472 EfiIPsecInBound or EfiIPsecOutBound.
473 @param[in] Context The token's wrap.
474
475 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
476 @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.
477 @retval EFI_SUCCESS The packet was protected.
478 @retval EFI_ACCESS_DENIED The packet was discarded.
479 @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation.
480 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the
481 number of input data blocks when build a fragment table.
482
483 **/
484 EFI_STATUS
485 Ip4IpSecProcessPacket (
486 IN IP4_SERVICE *IpSb,
487 IN OUT IP4_HEAD **Head,
488 IN OUT NET_BUF **Netbuf,
489 IN OUT UINT8 **Options,
490 IN OUT UINT32 *OptionsLen,
491 IN EFI_IPSEC_TRAFFIC_DIR Direction,
492 IN VOID *Context
493 )
494 {
495 NET_FRAGMENT *FragmentTable;
496 NET_FRAGMENT *OriginalFragmentTable;
497 UINT32 FragmentCount;
498 UINT32 OriginalFragmentCount;
499 EFI_EVENT RecycleEvent;
500 NET_BUF *Packet;
501 IP4_TXTOKEN_WRAP *TxWrap;
502 IP4_IPSEC_WRAP *IpSecWrap;
503 EFI_STATUS Status;
504 IP4_HEAD ZeroHead;
505
506 Status = EFI_SUCCESS;
507 Packet = *Netbuf;
508 RecycleEvent = NULL;
509 IpSecWrap = NULL;
510 FragmentTable = NULL;
511 TxWrap = (IP4_TXTOKEN_WRAP *) Context;
512 FragmentCount = Packet->BlockOpNum;
513
514 ZeroMem (&ZeroHead, sizeof (IP4_HEAD));
515
516 if (mIpSec == NULL) {
517 gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **) &mIpSec);
518 if (mIpSec == NULL) {
519 goto ON_EXIT;
520 }
521 }
522
523 //
524 // Check whether the IPsec enable variable is set.
525 //
526 if (mIpSec->DisabledFlag) {
527 //
528 // If IPsec is disabled, restore the original MTU
529 //
530 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize;
531 goto ON_EXIT;
532 } else {
533 //
534 // If IPsec is enabled, use the MTU which reduce the IPsec header length.
535 //
536 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP4_MAX_IPSEC_HEADLEN;
537 }
538
539 //
540 // Rebuild fragment table from netbuf to ease IPsec process.
541 //
542 FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT));
543
544 if (FragmentTable == NULL) {
545 Status = EFI_OUT_OF_RESOURCES;
546 goto ON_EXIT;
547 }
548
549 Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount);
550
551 //
552 // Record the original FragmentTable and count.
553 //
554 OriginalFragmentTable = FragmentTable;
555 OriginalFragmentCount = FragmentCount;
556
557 if (EFI_ERROR (Status)) {
558 FreePool (FragmentTable);
559 goto ON_EXIT;
560 }
561
562 //
563 // Convert host byte order to network byte order
564 //
565 Ip4NtohHead (*Head);
566
567 Status = mIpSec->ProcessExt (
568 mIpSec,
569 IpSb->Controller,
570 IP_VERSION_4,
571 (VOID *) (*Head),
572 &(*Head)->Protocol,
573 (VOID **) Options,
574 OptionsLen,
575 (EFI_IPSEC_FRAGMENT_DATA **) (&FragmentTable),
576 &FragmentCount,
577 Direction,
578 &RecycleEvent
579 );
580 //
581 // Convert back to host byte order
582 //
583 Ip4NtohHead (*Head);
584
585 if (EFI_ERROR (Status)) {
586 FreePool (OriginalFragmentTable);
587 goto ON_EXIT;
588 }
589
590 if (OriginalFragmentTable == FragmentTable && OriginalFragmentCount == FragmentCount) {
591 //
592 // For ByPass Packet
593 //
594 FreePool (FragmentTable);
595 goto ON_EXIT;
596 } else {
597 //
598 // Free the FragmentTable which allocated before calling the IPsec.
599 //
600 FreePool (OriginalFragmentTable);
601 }
602
603 if (Direction == EfiIPsecOutBound && TxWrap != NULL) {
604
605 TxWrap->IpSecRecycleSignal = RecycleEvent;
606 TxWrap->Packet = NetbufFromExt (
607 FragmentTable,
608 FragmentCount,
609 IP4_MAX_HEADLEN,
610 0,
611 Ip4FreeTxToken,
612 TxWrap
613 );
614 if (TxWrap->Packet == NULL) {
615 //
616 // Recover the TxWrap->Packet, if meet a error, and the caller will free
617 // the TxWrap.
618 //
619 TxWrap->Packet = *Netbuf;
620 Status = EFI_OUT_OF_RESOURCES;
621 goto ON_EXIT;
622 }
623
624 //
625 // Free orginal Netbuf.
626 //
627 NetIpSecNetbufFree (*Netbuf);
628 *Netbuf = TxWrap->Packet;
629
630 } else {
631
632 IpSecWrap = AllocateZeroPool (sizeof (IP4_IPSEC_WRAP));
633
634 if (IpSecWrap == NULL) {
635 Status = EFI_OUT_OF_RESOURCES;
636 gBS->SignalEvent (RecycleEvent);
637 goto ON_EXIT;
638 }
639
640 IpSecWrap->IpSecRecycleSignal = RecycleEvent;
641 IpSecWrap->Packet = Packet;
642 Packet = NetbufFromExt (
643 FragmentTable,
644 FragmentCount,
645 IP4_MAX_HEADLEN,
646 0,
647 Ip4IpSecFree,
648 IpSecWrap
649 );
650
651 if (Packet == NULL) {
652 Packet = IpSecWrap->Packet;
653 gBS->SignalEvent (RecycleEvent);
654 FreePool (IpSecWrap);
655 Status = EFI_OUT_OF_RESOURCES;
656 goto ON_EXIT;
657 }
658
659 if (Direction == EfiIPsecInBound && 0 != CompareMem (*Head, &ZeroHead, sizeof (IP4_HEAD))) {
660 Ip4PrependHead (Packet, *Head, *Options, *OptionsLen);
661 Ip4NtohHead (Packet->Ip.Ip4);
662 NetbufTrim (Packet, ((*Head)->HeadLen << 2), TRUE);
663
664 CopyMem (
665 IP4_GET_CLIP_INFO (Packet),
666 IP4_GET_CLIP_INFO (IpSecWrap->Packet),
667 sizeof (IP4_CLIP_INFO)
668 );
669 }
670 *Netbuf = Packet;
671 }
672
673 ON_EXIT:
674 return Status;
675 }
676
677 /**
678 Pre-process the IPv4 packet. First validates the IPv4 packet, and
679 then reassembles packet if it is necessary.
680
681 @param[in] IpSb Pointer to IP4_SERVICE.
682 @param[in, out] Packet Pointer to the Packet to be processed.
683 @param[in] Head Pointer to the IP4_HEAD.
684 @param[in] Option Pointer to a buffer which contains the IPv4 option.
685 @param[in] OptionLen The length of Option in bytes.
686 @param[in] Flag The link layer flag for the packet received, such
687 as multicast.
688
689 @retval EFI_SEUCCESS The recieved packet is in well form.
690 @retval EFI_INVAILD_PARAMETER The recieved packet is malformed.
691
692 **/
693 EFI_STATUS
694 Ip4PreProcessPacket (
695 IN IP4_SERVICE *IpSb,
696 IN OUT NET_BUF **Packet,
697 IN IP4_HEAD *Head,
698 IN UINT8 *Option,
699 IN UINT32 OptionLen,
700 IN UINT32 Flag
701 )
702 {
703 IP4_CLIP_INFO *Info;
704 UINT32 HeadLen;
705 UINT32 TotalLen;
706 UINT16 Checksum;
707
708 //
709 // Check that the IP4 header is correctly formatted
710 //
711 if ((*Packet)->TotalSize < IP4_MIN_HEADLEN) {
712 return EFI_INVALID_PARAMETER;
713 }
714
715 HeadLen = (Head->HeadLen << 2);
716 TotalLen = NTOHS (Head->TotalLen);
717
718 //
719 // Mnp may deliver frame trailer sequence up, trim it off.
720 //
721 if (TotalLen < (*Packet)->TotalSize) {
722 NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE);
723 }
724
725 if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||
726 (TotalLen < HeadLen) || (TotalLen != (*Packet)->TotalSize)) {
727 return EFI_INVALID_PARAMETER;
728 }
729
730 //
731 // Some OS may send IP packets without checksum.
732 //
733 Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen));
734
735 if ((Head->Checksum != 0) && (Checksum != 0)) {
736 return EFI_INVALID_PARAMETER;
737 }
738
739 //
740 // Convert the IP header to host byte order, then get the per packet info.
741 //
742 (*Packet)->Ip.Ip4 = Ip4NtohHead (Head);
743
744 Info = IP4_GET_CLIP_INFO (*Packet);
745 Info->LinkFlag = Flag;
746 Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);
747 Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;
748 Info->Length = Head->TotalLen - HeadLen;
749 Info->End = Info->Start + Info->Length;
750 Info->Status = EFI_SUCCESS;
751
752 //
753 // The packet is destinated to us if the CastType is non-zero.
754 //
755 if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {
756 return EFI_INVALID_PARAMETER;
757 }
758
759 //
760 // Validate the options. Don't call the Ip4OptionIsValid if
761 // there is no option to save some CPU process.
762 //
763
764 if ((OptionLen > 0) && !Ip4OptionIsValid (Option, OptionLen, TRUE)) {
765 return EFI_INVALID_PARAMETER;
766 }
767
768 //
769 // Trim the head off, after this point, the packet is headless.
770 // and Packet->TotalLen == Info->Length.
771 //
772 NetbufTrim (*Packet, HeadLen, TRUE);
773
774 //
775 // Reassemble the packet if this is a fragment. The packet is a
776 // fragment if its head has MF (more fragment) set, or it starts
777 // at non-zero byte.
778 //
779 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) || (Info->Start != 0)) {
780 //
781 // Drop the fragment if DF is set but it is fragmented. Gateway
782 // need to send a type 4 destination unreache ICMP message here.
783 //
784 if ((Head->Fragment & IP4_HEAD_DF_MASK) != 0) {
785 return EFI_INVALID_PARAMETER;
786 }
787
788 //
789 // The length of all but the last fragments is in the unit of 8 bytes.
790 //
791 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) && (Info->Length % 8 != 0)) {
792 return EFI_INVALID_PARAMETER;
793 }
794
795 *Packet = Ip4Reassemble (&IpSb->Assemble, *Packet);
796
797 //
798 // Packet assembly isn't complete, start receive more packet.
799 //
800 if (*Packet == NULL) {
801 return EFI_INVALID_PARAMETER;
802 }
803 }
804
805 return EFI_SUCCESS;
806 }
807
808 /**
809 The IP4 input routine. It is called by the IP4_INTERFACE when a
810 IP4 fragment is received from MNP.
811
812 @param[in] Ip4Instance The IP4 child that request the receive, most like
813 it is NULL.
814 @param[in] Packet The IP4 packet received.
815 @param[in] IoStatus The return status of receive request.
816 @param[in] Flag The link layer flag for the packet received, such
817 as multicast.
818 @param[in] Context The IP4 service instance that own the MNP.
819
820 **/
821 VOID
822 Ip4AccpetFrame (
823 IN IP4_PROTOCOL *Ip4Instance,
824 IN NET_BUF *Packet,
825 IN EFI_STATUS IoStatus,
826 IN UINT32 Flag,
827 IN VOID *Context
828 )
829 {
830 IP4_SERVICE *IpSb;
831 IP4_HEAD *Head;
832 EFI_STATUS Status;
833 IP4_HEAD ZeroHead;
834 UINT8 *Option;
835 UINT32 OptionLen;
836
837 IpSb = (IP4_SERVICE *) Context;
838 Option = NULL;
839
840 if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTORY)) {
841 goto DROP;
842 }
843
844 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);
845 OptionLen = (Head->HeadLen << 2) - IP4_MIN_HEADLEN;
846 if (OptionLen > 0) {
847 Option = (UINT8 *) (Head + 1);
848 }
849
850 //
851 // Validate packet format and reassemble packet if it is necessary.
852 //
853 Status = Ip4PreProcessPacket (
854 IpSb,
855 &Packet,
856 Head,
857 Option,
858 OptionLen,
859 Flag
860 );
861
862 if (EFI_ERROR (Status)) {
863 goto RESTART;
864 }
865
866 //
867 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
868 // and no need consider any other ahead ext headers.
869 //
870 Status = Ip4IpSecProcessPacket (
871 IpSb,
872 &Head,
873 &Packet,
874 &Option,
875 &OptionLen,
876 EfiIPsecInBound,
877 NULL
878 );
879
880 if (EFI_ERROR (Status)) {
881 goto RESTART;
882 }
883
884 //
885 // If the packet is protected by tunnel mode, parse the inner Ip Packet.
886 //
887 ZeroMem (&ZeroHead, sizeof (IP4_HEAD));
888 if (0 == CompareMem (Head, &ZeroHead, sizeof (IP4_HEAD))) {
889 // Packet may have been changed. Head, HeadLen, TotalLen, and
890 // info must be reloaded bofore use. The ownership of the packet
891 // is transfered to the packet process logic.
892 //
893 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);
894 Status = Ip4PreProcessPacket (
895 IpSb,
896 &Packet,
897 Head,
898 Option,
899 OptionLen,
900 Flag
901 );
902 if (EFI_ERROR (Status)) {
903 goto RESTART;
904 }
905 }
906
907 ASSERT (Packet != NULL);
908 Head = Packet->Ip.Ip4;
909 IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;
910
911 switch (Head->Protocol) {
912 case EFI_IP_PROTO_ICMP:
913 Ip4IcmpHandle (IpSb, Head, Packet);
914 break;
915
916 case IP4_PROTO_IGMP:
917 Ip4IgmpHandle (IpSb, Head, Packet);
918 break;
919
920 default:
921 Ip4Demultiplex (IpSb, Head, Packet);
922 }
923
924 Packet = NULL;
925
926 //
927 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
928 // which are signaled with received data.
929 //
930 DispatchDpc ();
931
932 RESTART:
933 Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
934
935 DROP:
936 if (Packet != NULL) {
937 NetbufFree (Packet);
938 }
939
940 return ;
941 }
942
943
944 /**
945 Check whether this IP child accepts the packet.
946
947 @param[in] IpInstance The IP child to check
948 @param[in] Head The IP header of the packet
949 @param[in] Packet The data of the packet
950
951 @retval TRUE If the child wants to receive the packet.
952 @retval FALSE Otherwise.
953
954 **/
955 BOOLEAN
956 Ip4InstanceFrameAcceptable (
957 IN IP4_PROTOCOL *IpInstance,
958 IN IP4_HEAD *Head,
959 IN NET_BUF *Packet
960 )
961 {
962 IP4_ICMP_ERROR_HEAD Icmp;
963 EFI_IP4_CONFIG_DATA *Config;
964 IP4_CLIP_INFO *Info;
965 UINT16 Proto;
966 UINT32 Index;
967
968 Config = &IpInstance->ConfigData;
969
970 //
971 // Dirty trick for the Tiano UEFI network stack implmentation. If
972 // ReceiveTimeout == -1, the receive of the packet for this instance
973 // is disabled. The UEFI spec don't have such capability. We add
974 // this to improve the performance because IP will make a copy of
975 // the received packet for each accepting instance. Some IP instances
976 // used by UDP/TCP only send packets, they don't wants to receive.
977 //
978 if (Config->ReceiveTimeout == (UINT32)(-1)) {
979 return FALSE;
980 }
981
982 if (Config->AcceptPromiscuous) {
983 return TRUE;
984 }
985
986 //
987 // Use protocol from the IP header embedded in the ICMP error
988 // message to filter, instead of ICMP itself. ICMP handle will
989 // call Ip4Demultiplex to deliver ICMP errors.
990 //
991 Proto = Head->Protocol;
992
993 if ((Proto == EFI_IP_PROTO_ICMP) && (!Config->AcceptAnyProtocol) && (Proto != Config->DefaultProtocol)) {
994 NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);
995
996 if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {
997 if (!Config->AcceptIcmpErrors) {
998 return FALSE;
999 }
1000
1001 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1002 Proto = Icmp.IpHead.Protocol;
1003 }
1004 }
1005
1006 //
1007 // Match the protocol
1008 //
1009 if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {
1010 return FALSE;
1011 }
1012
1013 //
1014 // Check for broadcast, the caller has computed the packet's
1015 // cast type for this child's interface.
1016 //
1017 Info = IP4_GET_CLIP_INFO (Packet);
1018
1019 if (IP4_IS_BROADCAST (Info->CastType)) {
1020 return Config->AcceptBroadcast;
1021 }
1022
1023 //
1024 // If it is a multicast packet, check whether we are in the group.
1025 //
1026 if (Info->CastType == IP4_MULTICAST) {
1027 //
1028 // Receive the multicast if the instance wants to receive all packets.
1029 //
1030 if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {
1031 return TRUE;
1032 }
1033
1034 for (Index = 0; Index < IpInstance->GroupCount; Index++) {
1035 if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {
1036 break;
1037 }
1038 }
1039
1040 return (BOOLEAN)(Index < IpInstance->GroupCount);
1041 }
1042
1043 return TRUE;
1044 }
1045
1046
1047 /**
1048 Enqueue a shared copy of the packet to the IP4 child if the
1049 packet is acceptable to it. Here the data of the packet is
1050 shared, but the net buffer isn't.
1051
1052 @param[in] IpInstance The IP4 child to enqueue the packet to
1053 @param[in] Head The IP header of the received packet
1054 @param[in] Packet The data of the received packet
1055
1056 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1057 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
1058 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
1059 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1060
1061 **/
1062 EFI_STATUS
1063 Ip4InstanceEnquePacket (
1064 IN IP4_PROTOCOL *IpInstance,
1065 IN IP4_HEAD *Head,
1066 IN NET_BUF *Packet
1067 )
1068 {
1069 IP4_CLIP_INFO *Info;
1070 NET_BUF *Clone;
1071
1072 //
1073 // Check whether the packet is acceptable to this instance.
1074 //
1075 if (IpInstance->State != IP4_STATE_CONFIGED) {
1076 return EFI_NOT_STARTED;
1077 }
1078
1079 if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {
1080 return EFI_INVALID_PARAMETER;
1081 }
1082
1083 //
1084 // Enque a shared copy of the packet.
1085 //
1086 Clone = NetbufClone (Packet);
1087
1088 if (Clone == NULL) {
1089 return EFI_OUT_OF_RESOURCES;
1090 }
1091
1092 //
1093 // Set the receive time out for the assembled packet. If it expires,
1094 // packet will be removed from the queue.
1095 //
1096 Info = IP4_GET_CLIP_INFO (Clone);
1097 Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);
1098
1099 InsertTailList (&IpInstance->Received, &Clone->List);
1100 return EFI_SUCCESS;
1101 }
1102
1103
1104 /**
1105 The signal handle of IP4's recycle event. It is called back
1106 when the upper layer release the packet.
1107
1108 @param Event The IP4's recycle event.
1109 @param Context The context of the handle, which is a
1110 IP4_RXDATA_WRAP
1111
1112 **/
1113 VOID
1114 EFIAPI
1115 Ip4OnRecyclePacket (
1116 IN EFI_EVENT Event,
1117 IN VOID *Context
1118 )
1119 {
1120 IP4_RXDATA_WRAP *Wrap;
1121
1122 Wrap = (IP4_RXDATA_WRAP *) Context;
1123
1124 EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock);
1125 RemoveEntryList (&Wrap->Link);
1126 EfiReleaseLock (&Wrap->IpInstance->RecycleLock);
1127
1128 ASSERT (!NET_BUF_SHARED (Wrap->Packet));
1129 NetbufFree (Wrap->Packet);
1130
1131 gBS->CloseEvent (Wrap->RxData.RecycleSignal);
1132 FreePool (Wrap);
1133 }
1134
1135
1136 /**
1137 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1138 delivered to the upper layer. Each IP4 child that accepts the
1139 packet will get a not-shared copy of the packet which is wrapped
1140 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1141 to the upper layer. Upper layer will signal the recycle event in
1142 it when it is done with the packet.
1143
1144 @param[in] IpInstance The IP4 child to receive the packet
1145 @param[in] Packet The packet to deliver up.
1146
1147 @retval Wrap if warp the packet succeed.
1148 @retval NULL failed to wrap the packet .
1149
1150 **/
1151 IP4_RXDATA_WRAP *
1152 Ip4WrapRxData (
1153 IN IP4_PROTOCOL *IpInstance,
1154 IN NET_BUF *Packet
1155 )
1156 {
1157 IP4_RXDATA_WRAP *Wrap;
1158 EFI_IP4_RECEIVE_DATA *RxData;
1159 EFI_STATUS Status;
1160
1161 Wrap = AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));
1162
1163 if (Wrap == NULL) {
1164 return NULL;
1165 }
1166
1167 InitializeListHead (&Wrap->Link);
1168
1169 Wrap->IpInstance = IpInstance;
1170 Wrap->Packet = Packet;
1171 RxData = &Wrap->RxData;
1172
1173 ZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME));
1174
1175 Status = gBS->CreateEvent (
1176 EVT_NOTIFY_SIGNAL,
1177 TPL_NOTIFY,
1178 Ip4OnRecyclePacket,
1179 Wrap,
1180 &RxData->RecycleSignal
1181 );
1182
1183 if (EFI_ERROR (Status)) {
1184 FreePool (Wrap);
1185 return NULL;
1186 }
1187
1188 ASSERT (Packet->Ip.Ip4 != NULL);
1189
1190 //
1191 // The application expects a network byte order header.
1192 //
1193 RxData->HeaderLength = (Packet->Ip.Ip4->HeadLen << 2);
1194 RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip.Ip4);
1195
1196 RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;
1197 RxData->Options = NULL;
1198
1199 if (RxData->OptionsLength != 0) {
1200 RxData->Options = (VOID *) (RxData->Header + 1);
1201 }
1202
1203 RxData->DataLength = Packet->TotalSize;
1204
1205 //
1206 // Build the fragment table to be delivered up.
1207 //
1208 RxData->FragmentCount = Packet->BlockOpNum;
1209 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);
1210
1211 return Wrap;
1212 }
1213
1214
1215 /**
1216 Deliver the received packets to upper layer if there are both received
1217 requests and enqueued packets. If the enqueued packet is shared, it will
1218 duplicate it to a non-shared packet, release the shared packet, then
1219 deliver the non-shared packet up.
1220
1221 @param[in] IpInstance The IP child to deliver the packet up.
1222
1223 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1224 packets.
1225 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1226 are delivered up.
1227
1228 **/
1229 EFI_STATUS
1230 Ip4InstanceDeliverPacket (
1231 IN IP4_PROTOCOL *IpInstance
1232 )
1233 {
1234 EFI_IP4_COMPLETION_TOKEN *Token;
1235 IP4_RXDATA_WRAP *Wrap;
1236 NET_BUF *Packet;
1237 NET_BUF *Dup;
1238 UINT8 *Head;
1239
1240 //
1241 // Deliver a packet if there are both a packet and a receive token.
1242 //
1243 while (!IsListEmpty (&IpInstance->Received) &&
1244 !NetMapIsEmpty (&IpInstance->RxTokens)) {
1245
1246 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);
1247
1248 if (!NET_BUF_SHARED (Packet)) {
1249 //
1250 // If this is the only instance that wants the packet, wrap it up.
1251 //
1252 Wrap = Ip4WrapRxData (IpInstance, Packet);
1253
1254 if (Wrap == NULL) {
1255 return EFI_OUT_OF_RESOURCES;
1256 }
1257
1258 RemoveEntryList (&Packet->List);
1259
1260 } else {
1261 //
1262 // Create a duplicated packet if this packet is shared
1263 //
1264 Dup = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN);
1265
1266 if (Dup == NULL) {
1267 return EFI_OUT_OF_RESOURCES;
1268 }
1269
1270 //
1271 // Copy the IP head over. The packet to deliver up is
1272 // headless. Trim the head off after copy. The IP head
1273 // may be not continuous before the data.
1274 //
1275 Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);
1276 Dup->Ip.Ip4 = (IP4_HEAD *) Head;
1277
1278 CopyMem (Head, Packet->Ip.Ip4, Packet->Ip.Ip4->HeadLen << 2);
1279 NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);
1280
1281 Wrap = Ip4WrapRxData (IpInstance, Dup);
1282
1283 if (Wrap == NULL) {
1284 NetbufFree (Dup);
1285 return EFI_OUT_OF_RESOURCES;
1286 }
1287
1288 RemoveEntryList (&Packet->List);
1289 NetbufFree (Packet);
1290
1291 Packet = Dup;
1292 }
1293
1294 //
1295 // Insert it into the delivered packet, then get a user's
1296 // receive token, pass the wrapped packet up.
1297 //
1298 EfiAcquireLockOrFail (&IpInstance->RecycleLock);
1299 InsertHeadList (&IpInstance->Delivered, &Wrap->Link);
1300 EfiReleaseLock (&IpInstance->RecycleLock);
1301
1302 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);
1303 Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;
1304 Token->Packet.RxData = &Wrap->RxData;
1305
1306 gBS->SignalEvent (Token->Event);
1307 }
1308
1309 return EFI_SUCCESS;
1310 }
1311
1312
1313 /**
1314 Enqueue a received packet to all the IP children that share
1315 the same interface.
1316
1317 @param[in] IpSb The IP4 service instance that receive the packet
1318 @param[in] Head The header of the received packet
1319 @param[in] Packet The data of the received packet
1320 @param[in] IpIf The interface to enqueue the packet to
1321
1322 @return The number of the IP4 children that accepts the packet
1323
1324 **/
1325 INTN
1326 Ip4InterfaceEnquePacket (
1327 IN IP4_SERVICE *IpSb,
1328 IN IP4_HEAD *Head,
1329 IN NET_BUF *Packet,
1330 IN IP4_INTERFACE *IpIf
1331 )
1332 {
1333 IP4_PROTOCOL *IpInstance;
1334 IP4_CLIP_INFO *Info;
1335 LIST_ENTRY *Entry;
1336 INTN Enqueued;
1337 INTN LocalType;
1338 INTN SavedType;
1339
1340 //
1341 // First, check that the packet is acceptable to this interface
1342 // and find the local cast type for the interface. A packet sent
1343 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1344 // promiscuous receiving.
1345 //
1346 LocalType = 0;
1347 Info = IP4_GET_CLIP_INFO (Packet);
1348
1349 if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {
1350 //
1351 // If the CastType is multicast, don't need to filter against
1352 // the group address here, Ip4InstanceFrameAcceptable will do
1353 // that later.
1354 //
1355 LocalType = Info->CastType;
1356
1357 } else {
1358 //
1359 // Check the destination againist local IP. If the station
1360 // address is 0.0.0.0, it means receiving all the IP destined
1361 // to local non-zero IP. Otherwise, it is necessary to compare
1362 // the destination to the interface's IP address.
1363 //
1364 if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {
1365 LocalType = IP4_LOCAL_HOST;
1366
1367 } else {
1368 LocalType = Ip4GetNetCast (Head->Dst, IpIf);
1369
1370 if ((LocalType == 0) && IpIf->PromiscRecv) {
1371 LocalType = IP4_PROMISCUOUS;
1372 }
1373 }
1374 }
1375
1376 if (LocalType == 0) {
1377 return 0;
1378 }
1379
1380 //
1381 // Iterate through the ip instances on the interface, enqueue
1382 // the packet if filter passed. Save the original cast type,
1383 // and pass the local cast type to the IP children on the
1384 // interface. The global cast type will be restored later.
1385 //
1386 SavedType = Info->CastType;
1387 Info->CastType = LocalType;
1388
1389 Enqueued = 0;
1390
1391 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1392 IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
1393 NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);
1394
1395 if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {
1396 Enqueued++;
1397 }
1398 }
1399
1400 Info->CastType = SavedType;
1401 return Enqueued;
1402 }
1403
1404
1405 /**
1406 Deliver the packet for each IP4 child on the interface.
1407
1408 @param[in] IpSb The IP4 service instance that received the packet
1409 @param[in] IpIf The IP4 interface to deliver the packet.
1410
1411 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1412
1413 **/
1414 EFI_STATUS
1415 Ip4InterfaceDeliverPacket (
1416 IN IP4_SERVICE *IpSb,
1417 IN IP4_INTERFACE *IpIf
1418 )
1419 {
1420 IP4_PROTOCOL *Ip4Instance;
1421 LIST_ENTRY *Entry;
1422
1423 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1424 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
1425 Ip4InstanceDeliverPacket (Ip4Instance);
1426 }
1427
1428 return EFI_SUCCESS;
1429 }
1430
1431
1432 /**
1433 Demultiple the packet. the packet delivery is processed in two
1434 passes. The first pass will enque a shared copy of the packet
1435 to each IP4 child that accepts the packet. The second pass will
1436 deliver a non-shared copy of the packet to each IP4 child that
1437 has pending receive requests. Data is copied if more than one
1438 child wants to consume the packet because each IP child needs
1439 its own copy of the packet to make changes.
1440
1441 @param[in] IpSb The IP4 service instance that received the packet
1442 @param[in] Head The header of the received packet
1443 @param[in] Packet The data of the received packet
1444
1445 @retval EFI_NOT_FOUND No IP child accepts the packet
1446 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1447 children.
1448
1449 **/
1450 EFI_STATUS
1451 Ip4Demultiplex (
1452 IN IP4_SERVICE *IpSb,
1453 IN IP4_HEAD *Head,
1454 IN NET_BUF *Packet
1455 )
1456 {
1457 LIST_ENTRY *Entry;
1458 IP4_INTERFACE *IpIf;
1459 INTN Enqueued;
1460
1461 //
1462 // Two pass delivery: first, enque a shared copy of the packet
1463 // to each instance that accept the packet.
1464 //
1465 Enqueued = 0;
1466
1467 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1468 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
1469
1470 if (IpIf->Configured) {
1471 Enqueued += Ip4InterfaceEnquePacket (IpSb, Head, Packet, IpIf);
1472 }
1473 }
1474
1475 //
1476 // Second: deliver a duplicate of the packet to each instance.
1477 // Release the local reference first, so that the last instance
1478 // getting the packet will not copy the data.
1479 //
1480 NetbufFree (Packet);
1481
1482 if (Enqueued == 0) {
1483 return EFI_NOT_FOUND;
1484 }
1485
1486 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1487 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
1488
1489 if (IpIf->Configured) {
1490 Ip4InterfaceDeliverPacket (IpSb, IpIf);
1491 }
1492 }
1493
1494 return EFI_SUCCESS;
1495 }
1496
1497
1498 /**
1499 Timeout the fragment and enqueued packets.
1500
1501 @param[in] IpSb The IP4 service instance to timeout
1502
1503 **/
1504 VOID
1505 Ip4PacketTimerTicking (
1506 IN IP4_SERVICE *IpSb
1507 )
1508 {
1509 LIST_ENTRY *InstanceEntry;
1510 LIST_ENTRY *Entry;
1511 LIST_ENTRY *Next;
1512 IP4_PROTOCOL *IpInstance;
1513 IP4_ASSEMBLE_ENTRY *Assemble;
1514 NET_BUF *Packet;
1515 IP4_CLIP_INFO *Info;
1516 UINT32 Index;
1517
1518 //
1519 // First, time out the fragments. The packet's life is counting down
1520 // once the first-arrived fragment was received.
1521 //
1522 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
1523 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {
1524 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);
1525
1526 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {
1527 RemoveEntryList (Entry);
1528 Ip4FreeAssembleEntry (Assemble);
1529 }
1530 }
1531 }
1532
1533 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {
1534 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);
1535
1536 //
1537 // Second, time out the assembled packets enqueued on each IP child.
1538 //
1539 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {
1540 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
1541 Info = IP4_GET_CLIP_INFO (Packet);
1542
1543 if ((Info->Life > 0) && (--Info->Life == 0)) {
1544 RemoveEntryList (Entry);
1545 NetbufFree (Packet);
1546 }
1547 }
1548
1549 //
1550 // Third: time out the transmitted packets.
1551 //
1552 NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);
1553 }
1554 }