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