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