VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/Ip4Dxe/Ip4Input.c@ 80721

Last change on this file since 80721 was 80721, checked in by vboxsync, 5 years ago

Devices/EFI/FirmwareNew: Start upgrade process to edk2-stable201908 (compiles on Windows and works to some extent), bugref:4643

  • Property svn:eol-style set to native
File size: 43.0 KB
Line 
1/** @file
2 IP4 input process.
3
4Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5(C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
6
7SPDX-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**/
28IP4_ASSEMBLE_ENTRY *
29Ip4CreateAssembleEntry (
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**/
68VOID
69Ip4FreeAssembleEntry (
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**/
95VOID
96Ip4InitAssembleTable (
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**/
115VOID
116Ip4CleanAssembleTable (
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**/
145VOID
146Ip4TrimPacket (
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**/
186VOID
187EFIAPI
188Ip4OnFreeFragments (
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**/
211NET_BUF *
212Ip4Reassemble (
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 formated, 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
432DROP:
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**/
444VOID
445EFIAPI
446Ip4IpSecFree (
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 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 suffcient 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**/
488EFI_STATUS
489Ip4IpSecProcessPacket (
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 orginal 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
676ON_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_SEUCCESS The recieved packet is in well form.
693 @retval EFI_INVAILD_PARAMETER The recieved packet is malformed.
694
695**/
696EFI_STATUS
697Ip4PreProcessPacket (
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 if ((*Packet)->TotalSize < IP4_MIN_HEADLEN) {
715 return EFI_INVALID_PARAMETER;
716 }
717
718 HeadLen = (Head->HeadLen << 2);
719 TotalLen = NTOHS (Head->TotalLen);
720
721 //
722 // Mnp may deliver frame trailer sequence up, trim it off.
723 //
724 if (TotalLen < (*Packet)->TotalSize) {
725 NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE);
726 }
727
728 if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||
729 (TotalLen < HeadLen) || (TotalLen != (*Packet)->TotalSize)) {
730 return EFI_INVALID_PARAMETER;
731 }
732
733 //
734 // Some OS may send IP packets without checksum.
735 //
736 Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen));
737
738 if ((Head->Checksum != 0) && (Checksum != 0)) {
739 return EFI_INVALID_PARAMETER;
740 }
741
742 //
743 // Convert the IP header to host byte order, then get the per packet info.
744 //
745 (*Packet)->Ip.Ip4 = Ip4NtohHead (Head);
746
747 Info = IP4_GET_CLIP_INFO (*Packet);
748 Info->LinkFlag = Flag;
749 Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);
750 Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;
751 Info->Length = Head->TotalLen - HeadLen;
752 Info->End = Info->Start + Info->Length;
753 Info->Status = EFI_SUCCESS;
754
755 //
756 // The packet is destinated to us if the CastType is non-zero.
757 //
758 if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {
759 return EFI_INVALID_PARAMETER;
760 }
761
762 //
763 // Validate the options. Don't call the Ip4OptionIsValid if
764 // there is no option to save some CPU process.
765 //
766
767 if ((OptionLen > 0) && !Ip4OptionIsValid (Option, OptionLen, TRUE)) {
768 return EFI_INVALID_PARAMETER;
769 }
770
771 //
772 // Trim the head off, after this point, the packet is headless,
773 // and Packet->TotalLen == Info->Length.
774 //
775 NetbufTrim (*Packet, HeadLen, TRUE);
776
777 //
778 // Reassemble the packet if this is a fragment. The packet is a
779 // fragment if its head has MF (more fragment) set, or it starts
780 // at non-zero byte.
781 //
782 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) || (Info->Start != 0)) {
783 //
784 // Drop the fragment if DF is set but it is fragmented. Gateway
785 // need to send a type 4 destination unreache ICMP message here.
786 //
787 if ((Head->Fragment & IP4_HEAD_DF_MASK) != 0) {
788 return EFI_INVALID_PARAMETER;
789 }
790
791 //
792 // The length of all but the last fragments is in the unit of 8 bytes.
793 //
794 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) && (Info->Length % 8 != 0)) {
795 return EFI_INVALID_PARAMETER;
796 }
797
798 *Packet = Ip4Reassemble (&IpSb->Assemble, *Packet);
799
800 //
801 // Packet assembly isn't complete, start receive more packet.
802 //
803 if (*Packet == NULL) {
804 return EFI_INVALID_PARAMETER;
805 }
806 }
807
808 return EFI_SUCCESS;
809}
810
811/**
812 The IP4 input routine. It is called by the IP4_INTERFACE when a
813 IP4 fragment is received from MNP.
814
815 @param[in] Ip4Instance The IP4 child that request the receive, most like
816 it is NULL.
817 @param[in] Packet The IP4 packet received.
818 @param[in] IoStatus The return status of receive request.
819 @param[in] Flag The link layer flag for the packet received, such
820 as multicast.
821 @param[in] Context The IP4 service instance that own the MNP.
822
823**/
824VOID
825Ip4AccpetFrame (
826 IN IP4_PROTOCOL *Ip4Instance,
827 IN NET_BUF *Packet,
828 IN EFI_STATUS IoStatus,
829 IN UINT32 Flag,
830 IN VOID *Context
831 )
832{
833 IP4_SERVICE *IpSb;
834 IP4_HEAD *Head;
835 EFI_STATUS Status;
836 IP4_HEAD ZeroHead;
837 UINT8 *Option;
838 UINT32 OptionLen;
839
840 IpSb = (IP4_SERVICE *) Context;
841 Option = NULL;
842
843 if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTROY)) {
844 goto DROP;
845 }
846
847 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);
848 ASSERT (Head != NULL);
849 OptionLen = (Head->HeadLen << 2) - IP4_MIN_HEADLEN;
850 if (OptionLen > 0) {
851 Option = (UINT8 *) (Head + 1);
852 }
853
854 //
855 // Validate packet format and reassemble packet if it is necessary.
856 //
857 Status = Ip4PreProcessPacket (
858 IpSb,
859 &Packet,
860 Head,
861 Option,
862 OptionLen,
863 Flag
864 );
865
866 if (EFI_ERROR (Status)) {
867 goto RESTART;
868 }
869
870 //
871 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
872 // and no need consider any other ahead ext headers.
873 //
874 Status = Ip4IpSecProcessPacket (
875 IpSb,
876 &Head,
877 &Packet,
878 &Option,
879 &OptionLen,
880 EfiIPsecInBound,
881 NULL
882 );
883
884 if (EFI_ERROR (Status)) {
885 goto RESTART;
886 }
887
888 //
889 // If the packet is protected by tunnel mode, parse the inner Ip Packet.
890 //
891 ZeroMem (&ZeroHead, sizeof (IP4_HEAD));
892 if (0 == CompareMem (Head, &ZeroHead, sizeof (IP4_HEAD))) {
893 // Packet may have been changed. Head, HeadLen, TotalLen, and
894 // info must be reloaded bofore use. The ownership of the packet
895 // is transfered to the packet process logic.
896 //
897 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);
898 ASSERT (Head != NULL);
899 Status = Ip4PreProcessPacket (
900 IpSb,
901 &Packet,
902 Head,
903 Option,
904 OptionLen,
905 Flag
906 );
907 if (EFI_ERROR (Status)) {
908 goto RESTART;
909 }
910 }
911
912 ASSERT (Packet != NULL);
913 Head = Packet->Ip.Ip4;
914 IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;
915
916 switch (Head->Protocol) {
917 case EFI_IP_PROTO_ICMP:
918 Ip4IcmpHandle (IpSb, Head, Packet);
919 break;
920
921 case IP4_PROTO_IGMP:
922 Ip4IgmpHandle (IpSb, Head, Packet);
923 break;
924
925 default:
926 Ip4Demultiplex (IpSb, Head, Packet, Option, OptionLen);
927 }
928
929 Packet = NULL;
930
931 //
932 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
933 // which are signaled with received data.
934 //
935 DispatchDpc ();
936
937RESTART:
938 Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
939
940DROP:
941 if (Packet != NULL) {
942 NetbufFree (Packet);
943 }
944
945 return ;
946}
947
948
949/**
950 Check whether this IP child accepts the packet.
951
952 @param[in] IpInstance The IP child to check
953 @param[in] Head The IP header of the packet
954 @param[in] Packet The data of the packet
955
956 @retval TRUE If the child wants to receive the packet.
957 @retval FALSE Otherwise.
958
959**/
960BOOLEAN
961Ip4InstanceFrameAcceptable (
962 IN IP4_PROTOCOL *IpInstance,
963 IN IP4_HEAD *Head,
964 IN NET_BUF *Packet
965 )
966{
967 IP4_ICMP_ERROR_HEAD Icmp;
968 EFI_IP4_CONFIG_DATA *Config;
969 IP4_CLIP_INFO *Info;
970 UINT16 Proto;
971 UINT32 Index;
972
973 Config = &IpInstance->ConfigData;
974
975 //
976 // Dirty trick for the Tiano UEFI network stack implmentation. If
977 // ReceiveTimeout == -1, the receive of the packet for this instance
978 // is disabled. The UEFI spec don't have such capability. We add
979 // this to improve the performance because IP will make a copy of
980 // the received packet for each accepting instance. Some IP instances
981 // used by UDP/TCP only send packets, they don't wants to receive.
982 //
983 if (Config->ReceiveTimeout == (UINT32)(-1)) {
984 return FALSE;
985 }
986
987 if (Config->AcceptPromiscuous) {
988 return TRUE;
989 }
990
991 //
992 // Use protocol from the IP header embedded in the ICMP error
993 // message to filter, instead of ICMP itself. ICMP handle will
994 // call Ip4Demultiplex to deliver ICMP errors.
995 //
996 Proto = Head->Protocol;
997
998 if ((Proto == EFI_IP_PROTO_ICMP) && (!Config->AcceptAnyProtocol) && (Proto != Config->DefaultProtocol)) {
999 NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);
1000
1001 if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {
1002 if (!Config->AcceptIcmpErrors) {
1003 return FALSE;
1004 }
1005
1006 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1007 Proto = Icmp.IpHead.Protocol;
1008 }
1009 }
1010
1011 //
1012 // Match the protocol
1013 //
1014 if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {
1015 return FALSE;
1016 }
1017
1018 //
1019 // Check for broadcast, the caller has computed the packet's
1020 // cast type for this child's interface.
1021 //
1022 Info = IP4_GET_CLIP_INFO (Packet);
1023
1024 if (IP4_IS_BROADCAST (Info->CastType)) {
1025 return Config->AcceptBroadcast;
1026 }
1027
1028 //
1029 // If it is a multicast packet, check whether we are in the group.
1030 //
1031 if (Info->CastType == IP4_MULTICAST) {
1032 //
1033 // Receive the multicast if the instance wants to receive all packets.
1034 //
1035 if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {
1036 return TRUE;
1037 }
1038
1039 for (Index = 0; Index < IpInstance->GroupCount; Index++) {
1040 if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {
1041 break;
1042 }
1043 }
1044
1045 return (BOOLEAN)(Index < IpInstance->GroupCount);
1046 }
1047
1048 return TRUE;
1049}
1050
1051
1052/**
1053 Enqueue a shared copy of the packet to the IP4 child if the
1054 packet is acceptable to it. Here the data of the packet is
1055 shared, but the net buffer isn't.
1056
1057 @param[in] IpInstance The IP4 child to enqueue the packet to
1058 @param[in] Head The IP header of the received packet
1059 @param[in] Packet The data of the received packet
1060
1061 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1062 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
1063 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
1064 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1065
1066**/
1067EFI_STATUS
1068Ip4InstanceEnquePacket (
1069 IN IP4_PROTOCOL *IpInstance,
1070 IN IP4_HEAD *Head,
1071 IN NET_BUF *Packet
1072 )
1073{
1074 IP4_CLIP_INFO *Info;
1075 NET_BUF *Clone;
1076
1077 //
1078 // Check whether the packet is acceptable to this instance.
1079 //
1080 if (IpInstance->State != IP4_STATE_CONFIGED) {
1081 return EFI_NOT_STARTED;
1082 }
1083
1084 if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {
1085 return EFI_INVALID_PARAMETER;
1086 }
1087
1088 //
1089 // Enque a shared copy of the packet.
1090 //
1091 Clone = NetbufClone (Packet);
1092
1093 if (Clone == NULL) {
1094 return EFI_OUT_OF_RESOURCES;
1095 }
1096
1097 //
1098 // Set the receive time out for the assembled packet. If it expires,
1099 // packet will be removed from the queue.
1100 //
1101 Info = IP4_GET_CLIP_INFO (Clone);
1102 Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);
1103
1104 InsertTailList (&IpInstance->Received, &Clone->List);
1105 return EFI_SUCCESS;
1106}
1107
1108
1109/**
1110 The signal handle of IP4's recycle event. It is called back
1111 when the upper layer release the packet.
1112
1113 @param Event The IP4's recycle event.
1114 @param Context The context of the handle, which is a
1115 IP4_RXDATA_WRAP
1116
1117**/
1118VOID
1119EFIAPI
1120Ip4OnRecyclePacket (
1121 IN EFI_EVENT Event,
1122 IN VOID *Context
1123 )
1124{
1125 IP4_RXDATA_WRAP *Wrap;
1126
1127 Wrap = (IP4_RXDATA_WRAP *) Context;
1128
1129 EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock);
1130 RemoveEntryList (&Wrap->Link);
1131 EfiReleaseLock (&Wrap->IpInstance->RecycleLock);
1132
1133 ASSERT (!NET_BUF_SHARED (Wrap->Packet));
1134 NetbufFree (Wrap->Packet);
1135
1136 gBS->CloseEvent (Wrap->RxData.RecycleSignal);
1137 FreePool (Wrap);
1138}
1139
1140
1141/**
1142 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1143 delivered to the upper layer. Each IP4 child that accepts the
1144 packet will get a not-shared copy of the packet which is wrapped
1145 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1146 to the upper layer. Upper layer will signal the recycle event in
1147 it when it is done with the packet.
1148
1149 @param[in] IpInstance The IP4 child to receive the packet.
1150 @param[in] Packet The packet to deliver up.
1151
1152 @retval Wrap if warp the packet succeed.
1153 @retval NULL failed to wrap the packet .
1154
1155**/
1156IP4_RXDATA_WRAP *
1157Ip4WrapRxData (
1158 IN IP4_PROTOCOL *IpInstance,
1159 IN NET_BUF *Packet
1160 )
1161{
1162 IP4_RXDATA_WRAP *Wrap;
1163 EFI_IP4_RECEIVE_DATA *RxData;
1164 EFI_STATUS Status;
1165 BOOLEAN RawData;
1166
1167 Wrap = AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));
1168
1169 if (Wrap == NULL) {
1170 return NULL;
1171 }
1172
1173 InitializeListHead (&Wrap->Link);
1174
1175 Wrap->IpInstance = IpInstance;
1176 Wrap->Packet = Packet;
1177 RxData = &Wrap->RxData;
1178
1179 ZeroMem (RxData, sizeof (EFI_IP4_RECEIVE_DATA));
1180
1181 Status = gBS->CreateEvent (
1182 EVT_NOTIFY_SIGNAL,
1183 TPL_NOTIFY,
1184 Ip4OnRecyclePacket,
1185 Wrap,
1186 &RxData->RecycleSignal
1187 );
1188
1189 if (EFI_ERROR (Status)) {
1190 FreePool (Wrap);
1191 return NULL;
1192 }
1193
1194 ASSERT (Packet->Ip.Ip4 != NULL);
1195
1196 ASSERT (IpInstance != NULL);
1197 RawData = IpInstance->ConfigData.RawData;
1198
1199 //
1200 // The application expects a network byte order header.
1201 //
1202 if (!RawData) {
1203 RxData->HeaderLength = (Packet->Ip.Ip4->HeadLen << 2);
1204 RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip.Ip4);
1205 RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;
1206 RxData->Options = NULL;
1207
1208 if (RxData->OptionsLength != 0) {
1209 RxData->Options = (VOID *) (RxData->Header + 1);
1210 }
1211 }
1212
1213 RxData->DataLength = Packet->TotalSize;
1214
1215 //
1216 // Build the fragment table to be delivered up.
1217 //
1218 RxData->FragmentCount = Packet->BlockOpNum;
1219 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);
1220
1221 return Wrap;
1222}
1223
1224
1225/**
1226 Deliver the received packets to upper layer if there are both received
1227 requests and enqueued packets. If the enqueued packet is shared, it will
1228 duplicate it to a non-shared packet, release the shared packet, then
1229 deliver the non-shared packet up.
1230
1231 @param[in] IpInstance The IP child to deliver the packet up.
1232
1233 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1234 packets.
1235 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1236 are delivered up.
1237
1238**/
1239EFI_STATUS
1240Ip4InstanceDeliverPacket (
1241 IN IP4_PROTOCOL *IpInstance
1242 )
1243{
1244 EFI_IP4_COMPLETION_TOKEN *Token;
1245 IP4_RXDATA_WRAP *Wrap;
1246 NET_BUF *Packet;
1247 NET_BUF *Dup;
1248 UINT8 *Head;
1249 UINT32 HeadLen;
1250
1251 //
1252 // Deliver a packet if there are both a packet and a receive token.
1253 //
1254 while (!IsListEmpty (&IpInstance->Received) &&
1255 !NetMapIsEmpty (&IpInstance->RxTokens)) {
1256
1257 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);
1258
1259 if (!NET_BUF_SHARED (Packet)) {
1260 //
1261 // If this is the only instance that wants the packet, wrap it up.
1262 //
1263 Wrap = Ip4WrapRxData (IpInstance, Packet);
1264
1265 if (Wrap == NULL) {
1266 return EFI_OUT_OF_RESOURCES;
1267 }
1268
1269 RemoveEntryList (&Packet->List);
1270
1271 } else {
1272 //
1273 // Create a duplicated packet if this packet is shared
1274 //
1275 if (IpInstance->ConfigData.RawData) {
1276 HeadLen = 0;
1277 } else {
1278 HeadLen = IP4_MAX_HEADLEN;
1279 }
1280
1281 Dup = NetbufDuplicate (Packet, NULL, HeadLen);
1282
1283 if (Dup == NULL) {
1284 return EFI_OUT_OF_RESOURCES;
1285 }
1286
1287 if (!IpInstance->ConfigData.RawData) {
1288 //
1289 // Copy the IP head over. The packet to deliver up is
1290 // headless. Trim the head off after copy. The IP head
1291 // may be not continuous before the data.
1292 //
1293 Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);
1294 ASSERT (Head != NULL);
1295
1296 Dup->Ip.Ip4 = (IP4_HEAD *) Head;
1297
1298 CopyMem (Head, Packet->Ip.Ip4, Packet->Ip.Ip4->HeadLen << 2);
1299 NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);
1300 }
1301
1302 Wrap = Ip4WrapRxData (IpInstance, Dup);
1303
1304 if (Wrap == NULL) {
1305 NetbufFree (Dup);
1306 return EFI_OUT_OF_RESOURCES;
1307 }
1308
1309 RemoveEntryList (&Packet->List);
1310 NetbufFree (Packet);
1311
1312 Packet = Dup;
1313 }
1314
1315 //
1316 // Insert it into the delivered packet, then get a user's
1317 // receive token, pass the wrapped packet up.
1318 //
1319 EfiAcquireLockOrFail (&IpInstance->RecycleLock);
1320 InsertHeadList (&IpInstance->Delivered, &Wrap->Link);
1321 EfiReleaseLock (&IpInstance->RecycleLock);
1322
1323 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);
1324 Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;
1325 Token->Packet.RxData = &Wrap->RxData;
1326
1327 gBS->SignalEvent (Token->Event);
1328 }
1329
1330 return EFI_SUCCESS;
1331}
1332
1333
1334/**
1335 Enqueue a received packet to all the IP children that share
1336 the same interface.
1337
1338 @param[in] IpSb The IP4 service instance that receive the packet.
1339 @param[in] Head The header of the received packet.
1340 @param[in] Packet The data of the received packet.
1341 @param[in] Option Point to the IP4 packet header options.
1342 @param[in] OptionLen Length of the IP4 packet header options.
1343 @param[in] IpIf The interface to enqueue the packet to.
1344
1345 @return The number of the IP4 children that accepts the packet
1346
1347**/
1348INTN
1349Ip4InterfaceEnquePacket (
1350 IN IP4_SERVICE *IpSb,
1351 IN IP4_HEAD *Head,
1352 IN NET_BUF *Packet,
1353 IN UINT8 *Option,
1354 IN UINT32 OptionLen,
1355 IN IP4_INTERFACE *IpIf
1356 )
1357{
1358 IP4_PROTOCOL *IpInstance;
1359 IP4_CLIP_INFO *Info;
1360 LIST_ENTRY *Entry;
1361 INTN Enqueued;
1362 INTN LocalType;
1363 INTN SavedType;
1364
1365 //
1366 // First, check that the packet is acceptable to this interface
1367 // and find the local cast type for the interface. A packet sent
1368 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1369 // promiscuous receiving.
1370 //
1371 LocalType = 0;
1372 Info = IP4_GET_CLIP_INFO (Packet);
1373
1374 if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {
1375 //
1376 // If the CastType is multicast, don't need to filter against
1377 // the group address here, Ip4InstanceFrameAcceptable will do
1378 // that later.
1379 //
1380 LocalType = Info->CastType;
1381
1382 } else {
1383 //
1384 // Check the destination againist local IP. If the station
1385 // address is 0.0.0.0, it means receiving all the IP destined
1386 // to local non-zero IP. Otherwise, it is necessary to compare
1387 // the destination to the interface's IP address.
1388 //
1389 if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {
1390 LocalType = IP4_LOCAL_HOST;
1391
1392 } else {
1393 LocalType = Ip4GetNetCast (Head->Dst, IpIf);
1394
1395 if ((LocalType == 0) && IpIf->PromiscRecv) {
1396 LocalType = IP4_PROMISCUOUS;
1397 }
1398 }
1399 }
1400
1401 if (LocalType == 0) {
1402 return 0;
1403 }
1404
1405 //
1406 // Iterate through the ip instances on the interface, enqueue
1407 // the packet if filter passed. Save the original cast type,
1408 // and pass the local cast type to the IP children on the
1409 // interface. The global cast type will be restored later.
1410 //
1411 SavedType = Info->CastType;
1412 Info->CastType = LocalType;
1413
1414 Enqueued = 0;
1415
1416 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1417 IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
1418 NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);
1419
1420 //
1421 // In RawData mode, add IPv4 headers and options back to packet.
1422 //
1423 if ((IpInstance->ConfigData.RawData) && (Option != NULL) && (OptionLen != 0)){
1424 Ip4PrependHead (Packet, Head, Option, OptionLen);
1425 }
1426
1427 if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {
1428 Enqueued++;
1429 }
1430 }
1431
1432 Info->CastType = SavedType;
1433 return Enqueued;
1434}
1435
1436
1437/**
1438 Deliver the packet for each IP4 child on the interface.
1439
1440 @param[in] IpSb The IP4 service instance that received the packet
1441 @param[in] IpIf The IP4 interface to deliver the packet.
1442
1443 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1444
1445**/
1446EFI_STATUS
1447Ip4InterfaceDeliverPacket (
1448 IN IP4_SERVICE *IpSb,
1449 IN IP4_INTERFACE *IpIf
1450 )
1451{
1452 IP4_PROTOCOL *Ip4Instance;
1453 LIST_ENTRY *Entry;
1454
1455 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1456 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
1457 Ip4InstanceDeliverPacket (Ip4Instance);
1458 }
1459
1460 return EFI_SUCCESS;
1461}
1462
1463
1464/**
1465 Demultiple the packet. the packet delivery is processed in two
1466 passes. The first pass will enque a shared copy of the packet
1467 to each IP4 child that accepts the packet. The second pass will
1468 deliver a non-shared copy of the packet to each IP4 child that
1469 has pending receive requests. Data is copied if more than one
1470 child wants to consume the packet because each IP child needs
1471 its own copy of the packet to make changes.
1472
1473 @param[in] IpSb The IP4 service instance that received the packet.
1474 @param[in] Head The header of the received packet.
1475 @param[in] Packet The data of the received packet.
1476 @param[in] Option Point to the IP4 packet header options.
1477 @param[in] OptionLen Length of the IP4 packet header options.
1478
1479 @retval EFI_NOT_FOUND No IP child accepts the packet.
1480 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1481 children.
1482
1483**/
1484EFI_STATUS
1485Ip4Demultiplex (
1486 IN IP4_SERVICE *IpSb,
1487 IN IP4_HEAD *Head,
1488 IN NET_BUF *Packet,
1489 IN UINT8 *Option,
1490 IN UINT32 OptionLen
1491 )
1492{
1493 LIST_ENTRY *Entry;
1494 IP4_INTERFACE *IpIf;
1495 INTN Enqueued;
1496
1497 //
1498 // Two pass delivery: first, enque a shared copy of the packet
1499 // to each instance that accept the packet.
1500 //
1501 Enqueued = 0;
1502
1503 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1504 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
1505
1506 if (IpIf->Configured) {
1507 Enqueued += Ip4InterfaceEnquePacket (
1508 IpSb,
1509 Head,
1510 Packet,
1511 Option,
1512 OptionLen,
1513 IpIf
1514 );
1515 }
1516 }
1517
1518 //
1519 // Second: deliver a duplicate of the packet to each instance.
1520 // Release the local reference first, so that the last instance
1521 // getting the packet will not copy the data.
1522 //
1523 NetbufFree (Packet);
1524
1525 if (Enqueued == 0) {
1526 return EFI_NOT_FOUND;
1527 }
1528
1529 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1530 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
1531
1532 if (IpIf->Configured) {
1533 Ip4InterfaceDeliverPacket (IpSb, IpIf);
1534 }
1535 }
1536
1537 return EFI_SUCCESS;
1538}
1539
1540
1541/**
1542 Timeout the fragment and enqueued packets.
1543
1544 @param[in] IpSb The IP4 service instance to timeout
1545
1546**/
1547VOID
1548Ip4PacketTimerTicking (
1549 IN IP4_SERVICE *IpSb
1550 )
1551{
1552 LIST_ENTRY *InstanceEntry;
1553 LIST_ENTRY *Entry;
1554 LIST_ENTRY *Next;
1555 IP4_PROTOCOL *IpInstance;
1556 IP4_ASSEMBLE_ENTRY *Assemble;
1557 NET_BUF *Packet;
1558 IP4_CLIP_INFO *Info;
1559 UINT32 Index;
1560
1561 //
1562 // First, time out the fragments. The packet's life is counting down
1563 // once the first-arrived fragment was received.
1564 //
1565 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
1566 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {
1567 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);
1568
1569 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {
1570 RemoveEntryList (Entry);
1571 Ip4FreeAssembleEntry (Assemble);
1572 }
1573 }
1574 }
1575
1576 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {
1577 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);
1578
1579 //
1580 // Second, time out the assembled packets enqueued on each IP child.
1581 //
1582 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {
1583 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
1584 Info = IP4_GET_CLIP_INFO (Packet);
1585
1586 if ((Info->Life > 0) && (--Info->Life == 0)) {
1587 RemoveEntryList (Entry);
1588 NetbufFree (Packet);
1589 }
1590 }
1591
1592 //
1593 // Third: time out the transmitted packets.
1594 //
1595 NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);
1596 }
1597}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette