]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Input.c
Sync the latest version from R8.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Input.c
1 /** @file
2
3 Copyright (c) 2005 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Ip4Input.c
15
16 Abstract:
17
18 IP4 input process.
19
20
21 **/
22
23 #include "Ip4Impl.h"
24
25
26 /**
27 Create a empty assemble entry for the packet identified by
28 (Dst, Src, Id, Protocol). The default life for the packet is
29 120 seconds.
30
31 @param Dst The destination address
32 @param Src The source address
33 @param Id The ID field in IP header
34 @param Protocol The protocol field in IP header
35
36 @return NULL if failed to allocate memory for the entry, otherwise
37 @return the point to just created reassemble entry.
38
39 **/
40 STATIC
41 IP4_ASSEMBLE_ENTRY *
42 Ip4CreateAssembleEntry (
43 IN IP4_ADDR Dst,
44 IN IP4_ADDR Src,
45 IN UINT16 Id,
46 IN UINT8 Protocol
47 )
48 {
49
50 IP4_ASSEMBLE_ENTRY *Assemble;
51
52 Assemble = NetAllocatePool (sizeof (IP4_ASSEMBLE_ENTRY));
53
54 if (Assemble == NULL) {
55 return NULL;
56 }
57
58 NetListInit (&Assemble->Link);
59 NetListInit (&Assemble->Fragments);
60
61 Assemble->Dst = Dst;
62 Assemble->Src = Src;
63 Assemble->Id = Id;
64 Assemble->Protocol = Protocol;
65 Assemble->TotalLen = 0;
66 Assemble->CurLen = 0;
67 Assemble->Head = NULL;
68 Assemble->Info = NULL;
69 Assemble->Life = IP4_FRAGMENT_LIFE;
70
71 return Assemble;
72 }
73
74
75 /**
76 Release all the fragments of a packet, then free the assemble entry
77
78 @param Assemble The assemble entry to free
79
80 @return None
81
82 **/
83 STATIC
84 VOID
85 Ip4FreeAssembleEntry (
86 IN IP4_ASSEMBLE_ENTRY *Assemble
87 )
88 {
89 NET_LIST_ENTRY *Entry;
90 NET_LIST_ENTRY *Next;
91 NET_BUF *Fragment;
92
93 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) {
94 Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
95
96 NetListRemoveEntry (Entry);
97 NetbufFree (Fragment);
98 }
99
100 NetFreePool (Assemble);
101 }
102
103
104 /**
105 Initialize an already allocated assemble table. This is generally
106 the assemble table embedded in the IP4 service instance.
107
108 @param Table The assemble table to initialize.
109
110 @return NONE
111
112 **/
113 VOID
114 Ip4InitAssembleTable (
115 IN IP4_ASSEMBLE_TABLE *Table
116 )
117 {
118 UINT32 Index;
119
120 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
121 NetListInit (&Table->Bucket[Index]);
122 }
123 }
124
125
126 /**
127 Clean up the assemble table: remove all the fragments
128 and assemble entries.
129
130 @param Table The assemble table to clean up
131
132 @return None
133
134 **/
135 VOID
136 Ip4CleanAssembleTable (
137 IN IP4_ASSEMBLE_TABLE *Table
138 )
139 {
140 NET_LIST_ENTRY *Entry;
141 NET_LIST_ENTRY *Next;
142 IP4_ASSEMBLE_ENTRY *Assemble;
143 UINT32 Index;
144
145 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
146 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Table->Bucket[Index]) {
147 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);
148
149 NetListRemoveEntry (Entry);
150 Ip4FreeAssembleEntry (Assemble);
151 }
152 }
153 }
154
155
156 /**
157 Trim the packet to fit in [Start, End), and update the per
158 packet information.
159
160 @param Packet Packet to trim
161 @param Start The sequence of the first byte to fit in
162 @param End One beyond the sequence of last byte to fit in.
163
164 @return None
165
166 **/
167 STATIC
168 VOID
169 Ip4TrimPacket (
170 IN NET_BUF *Packet,
171 IN INTN Start,
172 IN INTN End
173 )
174 {
175 IP4_CLIP_INFO *Info;
176 INTN Len;
177
178 Info = IP4_GET_CLIP_INFO (Packet);
179
180 ASSERT (Info->Start + Info->Length == Info->End);
181 ASSERT ((Info->Start < End) && (Start < Info->End));
182
183 if (Info->Start < Start) {
184 Len = Start - Info->Start;
185
186 NetbufTrim (Packet, (UINT32) Len, NET_BUF_HEAD);
187 Info->Start = Start;
188 Info->Length -= Len;
189 }
190
191 if (End < Info->End) {
192 Len = End - Info->End;
193
194 NetbufTrim (Packet, (UINT32) Len, NET_BUF_TAIL);
195 Info->End = End;
196 Info->Length -= Len;
197 }
198 }
199
200
201 /**
202 Release all the fragments of the packet. This is the callback for
203 the assembled packet's OnFree. It will free the assemble entry,
204 which in turn will free all the fragments of the packet.
205
206 @param Arg The assemble entry to free
207
208 @return None
209
210 **/
211 STATIC
212 VOID
213 Ip4OnFreeFragments (
214 IN VOID *Arg
215 )
216 {
217 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY *) Arg);
218 }
219
220
221 /**
222 Reassemble the IP fragments. If all the fragments of the packet
223 have been received, it will wrap the packet in a net buffer then
224 return it to caller. If the packet can't be assembled, NULL is
225 return.
226
227 @param Table The assemble table used.
228 @param Packet The fragment to assemble
229
230 @return NULL if the packet can't be reassemble. The point to just assembled
231 @return packet if all the fragments of the packet have arrived.
232
233 **/
234 STATIC
235 NET_BUF *
236 Ip4Reassemble (
237 IN IP4_ASSEMBLE_TABLE *Table,
238 IN NET_BUF *Packet
239 )
240 {
241 IP4_HEAD *IpHead;
242 IP4_CLIP_INFO *This;
243 IP4_CLIP_INFO *Node;
244 IP4_ASSEMBLE_ENTRY *Assemble;
245 NET_LIST_ENTRY *Head;
246 NET_LIST_ENTRY *Prev;
247 NET_LIST_ENTRY *Cur;
248 NET_BUF *Fragment;
249 NET_BUF *NewPacket;
250 INTN Index;
251
252 IpHead = Packet->Ip;
253 This = IP4_GET_CLIP_INFO (Packet);
254
255 ASSERT (IpHead != NULL);
256
257 //
258 // First: find the related assemble entry
259 //
260 Assemble = NULL;
261 Index = IP4_ASSEMBLE_HASH (IpHead->Dst, IpHead->Src, IpHead->Id, IpHead->Protocol);
262
263 NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) {
264 Assemble = NET_LIST_USER_STRUCT (Cur, IP4_ASSEMBLE_ENTRY, Link);
265
266 if ((Assemble->Dst == IpHead->Dst) && (Assemble->Src == IpHead->Src) &&
267 (Assemble->Id == IpHead->Id) && (Assemble->Protocol == IpHead->Protocol)) {
268 break;
269 }
270 }
271
272 //
273 // Create a new assemble entry if no assemble entry is related to this packet
274 //
275 if (Cur == &Table->Bucket[Index]) {
276 Assemble = Ip4CreateAssembleEntry (
277 IpHead->Dst,
278 IpHead->Src,
279 IpHead->Id,
280 IpHead->Protocol
281 );
282
283 if (Assemble == NULL) {
284 goto DROP;
285 }
286
287 NetListInsertHead (&Table->Bucket[Index], &Assemble->Link);
288 }
289
290 //
291 // Find the point to insert the packet: before the first
292 // fragment with THIS.Start < CUR.Start. the previous one
293 // has PREV.Start <= THIS.Start < CUR.Start.
294 //
295 Head = &Assemble->Fragments;
296
297 NET_LIST_FOR_EACH (Cur, Head) {
298 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
299
300 if (This->Start < IP4_GET_CLIP_INFO (Fragment)->Start) {
301 break;
302 }
303 }
304
305 //
306 // Check whether the current fragment overlaps with the previous one.
307 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
308 // check whether THIS.Start < PREV.End for overlap. If two fragments
309 // overlaps, trim the overlapped part off THIS fragment.
310 //
311 if ((Prev = Cur->ForwardLink) != Head) {
312 Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);
313 Node = IP4_GET_CLIP_INFO (Fragment);
314
315 if (This->Start < Node->End) {
316 if (This->End <= Node->End) {
317 NetbufFree (Packet);
318 return NULL;
319 }
320
321 Ip4TrimPacket (Packet, Node->End, This->End);
322 }
323 }
324
325 //
326 // Insert the fragment into the packet. The fragment may be removed
327 // from the list by the following checks.
328 //
329 NetListInsertBefore (Cur, &Packet->List);
330
331 //
332 // Check the packets after the insert point. It holds that:
333 // THIS.Start <= NODE.Start < NODE.End. The equality holds
334 // if PREV and NEXT are continuous. THIS fragment may fill
335 // several holes. Remove the completely overlapped fragments
336 //
337 while (Cur != Head) {
338 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
339 Node = IP4_GET_CLIP_INFO (Fragment);
340
341 //
342 // Remove fragments completely overlapped by this fragment
343 //
344 if (Node->End <= This->End) {
345 Cur = Cur->ForwardLink;
346
347 NetListRemoveEntry (&Fragment->List);
348 Assemble->CurLen -= Node->Length;
349
350 NetbufFree (Fragment);
351 continue;
352 }
353
354 //
355 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
356 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
357 // If two fragments start at the same offset, remove THIS fragment
358 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
359 //
360 if (Node->Start < This->End) {
361 if (This->Start == Node->Start) {
362 NetListRemoveEntry (&Packet->List);
363 goto DROP;
364 }
365
366 Ip4TrimPacket (Packet, This->Start, Node->Start);
367 }
368
369 break;
370 }
371
372 //
373 // Update the assemble info: increase the current length. If it is
374 // the frist fragment, update the packet's IP head and per packet
375 // info. If it is the last fragment, update the total length.
376 //
377 Assemble->CurLen += This->Length;
378
379 if (This->Start == 0) {
380 //
381 // Once the first fragment is enqueued, it can't be removed
382 // from the fragment list. So, Assemble->Head always point
383 // to valid memory area.
384 //
385 ASSERT (Assemble->Head == NULL);
386
387 Assemble->Head = IpHead;
388 Assemble->Info = IP4_GET_CLIP_INFO (Packet);
389 }
390
391 //
392 // Don't update the length more than once.
393 //
394 if (IP4_LAST_FRAGMENT (IpHead->Fragment) && (Assemble->TotalLen == 0)) {
395 Assemble->TotalLen = This->End;
396 }
397
398 //
399 // Deliver the whole packet if all the fragments received.
400 // All fragments received if:
401 // 1. received the last one, so, the totoal length is know
402 // 2. received all the data. If the last fragment on the
403 // queue ends at the total length, all data is received.
404 //
405 if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) {
406
407 NetListRemoveEntry (&Assemble->Link);
408
409 //
410 // If the packet is properly formated, the last fragment's End
411 // equals to the packet's total length. Otherwise, the packet
412 // is a fake, drop it now.
413 //
414 Fragment = NET_LIST_USER_STRUCT (Head->BackLink, NET_BUF, List);
415
416 if (IP4_GET_CLIP_INFO (Fragment)->End != Assemble->TotalLen) {
417 Ip4FreeAssembleEntry (Assemble);
418 return NULL;
419 }
420
421 //
422 // Wrap the packet in a net buffer then deliver it up
423 //
424 NewPacket = NetbufFromBufList (
425 &Assemble->Fragments,
426 0,
427 0,
428 Ip4OnFreeFragments,
429 Assemble
430 );
431
432 if (NewPacket == NULL) {
433 Ip4FreeAssembleEntry (Assemble);
434 return NULL;
435 }
436
437 NewPacket->Ip = Assemble->Head;
438 CopyMem (IP4_GET_CLIP_INFO (NewPacket), Assemble->Info, sizeof (*IP4_GET_CLIP_INFO (NewPacket)));
439 return NewPacket;
440 }
441
442 return NULL;
443
444 DROP:
445 NetbufFree (Packet);
446 return NULL;
447 }
448
449
450 /**
451 The IP4 input routine. It is called by the IP4_INTERFACE when a
452 IP4 fragment is received from MNP.
453
454 @param Ip4Instance The IP4 child that request the receive, most like
455 it is NULL.
456 @param Packet The IP4 packet received.
457 @param IoStatus The return status of receive request.
458 @param Flag The link layer flag for the packet received, such
459 as multicast.
460 @param Context The IP4 service instance that own the MNP.
461
462 @return None
463
464 **/
465 VOID
466 Ip4AccpetFrame (
467 IN IP4_PROTOCOL *Ip4Instance,
468 IN NET_BUF *Packet,
469 IN EFI_STATUS IoStatus,
470 IN UINT32 Flag,
471 IN VOID *Context
472 )
473 {
474 IP4_SERVICE *IpSb;
475 IP4_CLIP_INFO *Info;
476 IP4_HEAD *Head;
477 UINT32 HeadLen;
478 UINT32 OptionLen;
479 UINT32 TotalLen;
480 UINT16 Checksum;
481
482 IpSb = (IP4_SERVICE *) Context;
483
484 if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTORY)) {
485 goto DROP;
486 }
487
488 //
489 // Check that the IP4 header is correctly formated
490 //
491 if (Packet->TotalSize < IP4_MIN_HEADLEN) {
492 goto RESTART;
493 }
494
495 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);
496 HeadLen = (Head->HeadLen << 2);
497 TotalLen = NTOHS (Head->TotalLen);
498
499 //
500 // Mnp may deliver frame trailer sequence up, trim it off.
501 //
502 if (TotalLen < Packet->TotalSize) {
503 NetbufTrim (Packet, Packet->TotalSize - TotalLen, FALSE);
504 }
505
506 if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||
507 (TotalLen < HeadLen) || (TotalLen != Packet->TotalSize)) {
508 goto RESTART;
509 }
510
511 //
512 // Some OS may send IP packets without checksum.
513 //
514 Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen));
515
516 if ((Head->Checksum != 0) && (Checksum != 0)) {
517 goto RESTART;
518 }
519
520 //
521 // Convert the IP header to host byte order, then get the per packet info.
522 //
523 Packet->Ip = Ip4NtohHead (Head);
524
525 Info = IP4_GET_CLIP_INFO (Packet);
526 Info->LinkFlag = Flag;
527 Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);
528 Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;
529 Info->Length = Head->TotalLen - HeadLen;
530 Info->End = Info->Start + Info->Length;
531 Info->Status = EFI_SUCCESS;
532
533 //
534 // The packet is destinated to us if the CastType is non-zero.
535 //
536 if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {
537 goto RESTART;
538 }
539
540 //
541 // Validate the options. Don't call the Ip4OptionIsValid if
542 // there is no option to save some CPU process.
543 //
544 OptionLen = HeadLen - IP4_MIN_HEADLEN;
545
546 if ((OptionLen > 0) && !Ip4OptionIsValid ((UINT8 *) (Head + 1), OptionLen, TRUE)) {
547 goto RESTART;
548 }
549
550 //
551 // Trim the head off, after this point, the packet is headless.
552 // and Packet->TotalLen == Info->Length.
553 //
554 NetbufTrim (Packet, HeadLen, TRUE);
555
556 //
557 // Reassemble the packet if this is a fragment. The packet is a
558 // fragment if its head has MF (more fragment) set, or it starts
559 // at non-zero byte.
560 //
561 if ((Head->Fragment & IP4_HEAD_MF_MASK) || (Info->Start != 0)) {
562 //
563 // Drop the fragment if DF is set but it is fragmented. Gateway
564 // need to send a type 4 destination unreache ICMP message here.
565 //
566 if (Head->Fragment & IP4_HEAD_DF_MASK) {
567 goto RESTART;
568 }
569
570 //
571 // The length of all but the last fragments is in the unit of 8 bytes.
572 //
573 if ((Head->Fragment & IP4_HEAD_MF_MASK) && (Info->Length % 8 != 0)) {
574 goto RESTART;
575 }
576
577 Packet = Ip4Reassemble (&IpSb->Assemble, Packet);
578
579 //
580 // Packet assembly isn't complete, start receive more packet.
581 //
582 if (Packet == NULL) {
583 goto RESTART;
584 }
585 }
586
587 //
588 // Packet may have been changed. Head, HeadLen, TotalLen, and
589 // info must be reloaded bofore use. The ownership of the packet
590 // is transfered to the packet process logic.
591 //
592 Head = Packet->Ip;
593 IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;
594
595 switch (Head->Protocol) {
596 case IP4_PROTO_ICMP:
597 Ip4IcmpHandle (IpSb, Head, Packet);
598 break;
599
600 case IP4_PROTO_IGMP:
601 Ip4IgmpHandle (IpSb, Head, Packet);
602 break;
603
604 default:
605 Ip4Demultiplex (IpSb, Head, Packet);
606 }
607
608 Packet = NULL;
609
610 //
611 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
612 // which are signaled with received data.
613 //
614 NetLibDispatchDpc ();
615
616 RESTART:
617 Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
618
619 DROP:
620 if (Packet != NULL) {
621 NetbufFree (Packet);
622 }
623
624 return ;
625 }
626
627
628 /**
629 Check whether this IP child accepts the packet.
630
631 @param IpInstance The IP child to check
632 @param Head The IP header of the packet
633 @param Packet The data of the packet
634
635 @return TRUE if the child wants to receive the packet, otherwise return FALSE.
636
637 **/
638 BOOLEAN
639 Ip4InstanceFrameAcceptable (
640 IN IP4_PROTOCOL *IpInstance,
641 IN IP4_HEAD *Head,
642 IN NET_BUF *Packet
643 )
644 {
645 IP4_ICMP_ERROR_HEAD Icmp;
646 EFI_IP4_CONFIG_DATA *Config;
647 IP4_CLIP_INFO *Info;
648 UINT16 Proto;
649 UINT32 Index;
650
651 Config = &IpInstance->ConfigData;
652
653 //
654 // Dirty trick for the Tiano UEFI network stack implmentation. If
655 // ReceiveTimeout == -1, the receive of the packet for this instance
656 // is disabled. The UEFI spec don't have such captibility. We add
657 // this to improve the performance because IP will make a copy of
658 // the received packet for each accepting instance. Some IP instances
659 // used by UDP/TCP only send packets, they don't wants to receive.
660 //
661 if (Config->ReceiveTimeout == (UINT32)(-1)) {
662 return FALSE;
663 }
664
665 if (Config->AcceptPromiscuous) {
666 return TRUE;
667 }
668
669 //
670 // Use protocol from the IP header embedded in the ICMP error
671 // message to filter, instead of ICMP itself. ICMP handle will
672 // can Ip4Demultiplex to deliver ICMP errors.
673 //
674 Proto = Head->Protocol;
675
676 if (Proto == IP4_PROTO_ICMP) {
677 NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);
678
679 if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {
680 if (!Config->AcceptIcmpErrors) {
681 return FALSE;
682 }
683
684 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
685 Proto = Icmp.IpHead.Protocol;
686 }
687 }
688
689 //
690 // Match the protocol
691 //
692 if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {
693 return FALSE;
694 }
695
696 //
697 // Check for broadcast, the caller has computed the packet's
698 // cast type for this child's interface.
699 //
700 Info = IP4_GET_CLIP_INFO (Packet);
701
702 if (IP4_IS_BROADCAST (Info->CastType)) {
703 return Config->AcceptBroadcast;
704 }
705
706 //
707 // If it is a multicast packet, check whether we are in the group.
708 //
709 if (Info->CastType == IP4_MULTICAST) {
710 //
711 // Receive the multicast if the instance wants to receive all packets.
712 //
713 if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {
714 return TRUE;
715 }
716
717 for (Index = 0; Index < IpInstance->GroupCount; Index++) {
718 if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {
719 break;
720 }
721 }
722
723 return (BOOLEAN)(Index < IpInstance->GroupCount);
724 }
725
726 return TRUE;
727 }
728
729
730 /**
731 Enqueue a shared copy of the packet to the IP4 child if the
732 packet is acceptable to it. Here the data of the packet is
733 shared, but the net buffer isn't.
734
735 @param IpInstance The IP4 child to enqueue the packet to
736 @param Head The IP header of the received packet
737 @param Packet The data of the received packet
738
739 @retval EFI_NOT_STARTED The IP child hasn't been configured.
740 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
741 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
742 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
743
744 **/
745 EFI_STATUS
746 Ip4InstanceEnquePacket (
747 IN IP4_PROTOCOL *IpInstance,
748 IN IP4_HEAD *Head,
749 IN NET_BUF *Packet
750 )
751 {
752 IP4_CLIP_INFO *Info;
753 NET_BUF *Clone;
754
755 //
756 // Check whether the packet is acceptable to this instance.
757 //
758 if (IpInstance->State != IP4_STATE_CONFIGED) {
759 return EFI_NOT_STARTED;
760 }
761
762 if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {
763 return EFI_INVALID_PARAMETER;
764 }
765
766 //
767 // Enque a shared copy of the packet.
768 //
769 Clone = NetbufClone (Packet);
770
771 if (Clone == NULL) {
772 return EFI_OUT_OF_RESOURCES;
773 }
774
775 //
776 // Set the receive time out for the assembled packet. If it expires,
777 // packet will be removed from the queue.
778 //
779 Info = IP4_GET_CLIP_INFO (Clone);
780 Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);
781
782 NetListInsertTail (&IpInstance->Received, &Clone->List);
783 return EFI_SUCCESS;
784 }
785
786
787 /**
788 The signal handle of IP4's recycle event. It is called back
789 when the upper layer release the packet.
790
791 @param Event The IP4's recycle event.
792 @param Context The context of the handle, which is a
793 IP4_RXDATA_WRAP
794
795 @return None
796
797 **/
798 STATIC
799 VOID
800 EFIAPI
801 Ip4OnRecyclePacket (
802 IN EFI_EVENT Event,
803 IN VOID *Context
804 )
805 {
806 IP4_RXDATA_WRAP *Wrap;
807
808 Wrap = (IP4_RXDATA_WRAP *) Context;
809
810 NET_TRYLOCK (&Wrap->IpInstance->RecycleLock);
811 NetListRemoveEntry (&Wrap->Link);
812 NET_UNLOCK (&Wrap->IpInstance->RecycleLock);
813
814 ASSERT (!NET_BUF_SHARED (Wrap->Packet));
815 NetbufFree (Wrap->Packet);
816
817 gBS->CloseEvent (Wrap->RxData.RecycleSignal);
818 NetFreePool (Wrap);
819 }
820
821
822 /**
823 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
824 delivered to the upper layer. Each IP4 child that accepts the
825 packet will get a not-shared copy of the packet which is wrapped
826 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
827 to the upper layer. Upper layer will signal the recycle event in
828 it when it is done with the packet.
829
830 @param IpInstance The IP4 child to receive the packet
831 @param Packet The packet to deliver up.
832
833 @return NULL if failed to wrap the packet, otherwise the wrapper.
834
835 **/
836 IP4_RXDATA_WRAP *
837 Ip4WrapRxData (
838 IN IP4_PROTOCOL *IpInstance,
839 IN NET_BUF *Packet
840 )
841 {
842 IP4_RXDATA_WRAP *Wrap;
843 EFI_IP4_RECEIVE_DATA *RxData;
844 EFI_STATUS Status;
845
846 Wrap = NetAllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));
847
848 if (Wrap == NULL) {
849 return NULL;
850 }
851
852 NetListInit (&Wrap->Link);
853
854 Wrap->IpInstance = IpInstance;
855 Wrap->Packet = Packet;
856 RxData = &Wrap->RxData;
857
858 NetZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME));
859
860 Status = gBS->CreateEvent (
861 EVT_NOTIFY_SIGNAL,
862 NET_TPL_RECYCLE,
863 Ip4OnRecyclePacket,
864 Wrap,
865 &RxData->RecycleSignal
866 );
867
868 if (EFI_ERROR (Status)) {
869 NetFreePool (Wrap);
870 return NULL;
871 }
872
873 ASSERT (Packet->Ip != NULL);
874
875 //
876 // The application expects a network byte order header.
877 //
878 RxData->HeaderLength = (Packet->Ip->HeadLen << 2);
879 RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip);
880
881 RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;
882 RxData->Options = NULL;
883
884 if (RxData->OptionsLength != 0) {
885 RxData->Options = (VOID *) (RxData->Header + 1);
886 }
887
888 RxData->DataLength = Packet->TotalSize;
889
890 //
891 // Build the fragment table to be delivered up.
892 //
893 RxData->FragmentCount = Packet->BlockOpNum;
894 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);
895
896 return Wrap;
897 }
898
899
900 /**
901 Deliver the received packets to upper layer if there are both received
902 requests and enqueued packets. If the enqueued packet is shared, it will
903 duplicate it to a non-shared packet, release the shared packet, then
904 deliver the non-shared packet up.
905
906 @param IpInstance The IP child to deliver the packet up.
907
908 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
909 packets.
910 @retval EFI_SUCCESS All the enqueued packets that can be delivered
911 are delivered up.
912
913 **/
914 EFI_STATUS
915 Ip4InstanceDeliverPacket (
916 IN IP4_PROTOCOL *IpInstance
917 )
918 {
919 EFI_IP4_COMPLETION_TOKEN *Token;
920 IP4_RXDATA_WRAP *Wrap;
921 NET_BUF *Packet;
922 NET_BUF *Dup;
923 UINT8 *Head;
924
925 //
926 // Deliver a packet if there are both a packet and a receive token.
927 //
928 while (!NetListIsEmpty (&IpInstance->Received) &&
929 !NetMapIsEmpty (&IpInstance->RxTokens)) {
930
931 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);
932
933 if (!NET_BUF_SHARED (Packet)) {
934 //
935 // If this is the only instance that wants the packet, wrap it up.
936 //
937 Wrap = Ip4WrapRxData (IpInstance, Packet);
938
939 if (Wrap == NULL) {
940 return EFI_OUT_OF_RESOURCES;
941 }
942
943 NetListRemoveEntry (&Packet->List);
944
945 } else {
946 //
947 // Create a duplicated packet if this packet is shared
948 //
949 Dup = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN);
950
951 if (Dup == NULL) {
952 return EFI_OUT_OF_RESOURCES;
953 }
954
955 //
956 // Copy the IP head over. The packet to deliver up is
957 // headless. Trim the head off after copy. The IP head
958 // may be not continuous before the data.
959 //
960 Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);
961 Dup->Ip = (IP4_HEAD *) Head;
962
963 NetCopyMem (Head, Packet->Ip, Packet->Ip->HeadLen << 2);
964 NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);
965
966 Wrap = Ip4WrapRxData (IpInstance, Dup);
967
968 if (Wrap == NULL) {
969 NetbufFree (Dup);
970 return EFI_OUT_OF_RESOURCES;
971 }
972
973 NetListRemoveEntry (&Packet->List);
974 NetbufFree (Packet);
975
976 Packet = Dup;
977 }
978
979 //
980 // Insert it into the delivered packet, then get a user's
981 // receive token, pass the wrapped packet up.
982 //
983 NET_TRYLOCK (&IpInstance->RecycleLock);
984 NetListInsertHead (&IpInstance->Delivered, &Wrap->Link);
985 NET_UNLOCK (&IpInstance->RecycleLock);
986
987 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);
988 Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;
989 Token->Packet.RxData = &Wrap->RxData;
990
991 gBS->SignalEvent (Token->Event);
992 }
993
994 return EFI_SUCCESS;
995 }
996
997
998 /**
999 Enqueue a received packet to all the IP children that share
1000 the same interface.
1001
1002 @param IpSb The IP4 service instance that receive the packet
1003 @param Head The header of the received packet
1004 @param Packet The data of the received packet
1005 @param IpIf The interface to enqueue the packet to
1006
1007 @return The number of the IP4 children that accepts the packet
1008
1009 **/
1010 INTN
1011 Ip4InterfaceEnquePacket (
1012 IN IP4_SERVICE *IpSb,
1013 IN IP4_HEAD *Head,
1014 IN NET_BUF *Packet,
1015 IN IP4_INTERFACE *IpIf
1016 )
1017 {
1018 IP4_PROTOCOL *IpInstance;
1019 IP4_CLIP_INFO *Info;
1020 NET_LIST_ENTRY *Entry;
1021 INTN Enqueued;
1022 INTN LocalType;
1023 INTN SavedType;
1024
1025 //
1026 // First, check that the packet is acceptable to this interface
1027 // and find the local cast type for the interface. A packet sent
1028 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1029 // promiscuous receiving.
1030 //
1031 LocalType = 0;
1032 Info = IP4_GET_CLIP_INFO (Packet);
1033
1034 if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {
1035 //
1036 // If the CastType is multicast, don't need to filter against
1037 // the group address here, Ip4InstanceFrameAcceptable will do
1038 // that later.
1039 //
1040 LocalType = Info->CastType;
1041
1042 } else {
1043 //
1044 // Check the destination againist local IP. If the station
1045 // address is 0.0.0.0, it means receiving all the IP destined
1046 // to local non-zero IP. Otherwise, it is necessary to compare
1047 // the destination to the interface's IP address.
1048 //
1049 if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {
1050 LocalType = IP4_LOCAL_HOST;
1051
1052 } else {
1053 LocalType = Ip4GetNetCast (Head->Dst, IpIf);
1054
1055 if ((LocalType == 0) && IpIf->PromiscRecv) {
1056 LocalType = IP4_PROMISCUOUS;
1057 }
1058 }
1059 }
1060
1061 if (LocalType == 0) {
1062 return 0;
1063 }
1064
1065 //
1066 // Iterate through the ip instances on the interface, enqueue
1067 // the packet if filter passed. Save the original cast type,
1068 // and pass the local cast type to the IP children on the
1069 // interface. The global cast type will be restored later.
1070 //
1071 SavedType = Info->CastType;
1072 Info->CastType = LocalType;
1073
1074 Enqueued = 0;
1075
1076 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1077 IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
1078 NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);
1079
1080 if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {
1081 Enqueued++;
1082 }
1083 }
1084
1085 Info->CastType = SavedType;
1086 return Enqueued;
1087 }
1088
1089
1090 /**
1091 Deliver the packet for each IP4 child on the interface.
1092
1093 @param IpSb The IP4 service instance that received the packet
1094 @param IpIf The IP4 interface to deliver the packet.
1095
1096 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1097
1098 **/
1099 EFI_STATUS
1100 Ip4InterfaceDeliverPacket (
1101 IN IP4_SERVICE *IpSb,
1102 IN IP4_INTERFACE *IpIf
1103 )
1104 {
1105 IP4_PROTOCOL *Ip4Instance;
1106 NET_LIST_ENTRY *Entry;
1107
1108 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1109 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
1110 Ip4InstanceDeliverPacket (Ip4Instance);
1111 }
1112
1113 return EFI_SUCCESS;
1114 }
1115
1116
1117 /**
1118 Demultiple the packet. the packet delivery is processed in two
1119 passes. The first pass will enque a shared copy of the packet
1120 to each IP4 child that accepts the packet. The second pass will
1121 deliver a non-shared copy of the packet to each IP4 child that
1122 has pending receive requests. Data is copied if more than one
1123 child wants to consume the packet bacause each IP child need
1124 its own copy of the packet to make changes.
1125
1126 @param IpSb The IP4 service instance that received the packet
1127 @param Head The header of the received packet
1128 @param Packet The data of the received packet
1129
1130 @retval EFI_NOT_FOUND No IP child accepts the packet
1131 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1132 children.
1133
1134 **/
1135 EFI_STATUS
1136 Ip4Demultiplex (
1137 IN IP4_SERVICE *IpSb,
1138 IN IP4_HEAD *Head,
1139 IN NET_BUF *Packet
1140 )
1141 {
1142 NET_LIST_ENTRY *Entry;
1143 IP4_INTERFACE *IpIf;
1144 INTN Enqueued;
1145
1146 //
1147 // Two pass delivery: first, enque a shared copy of the packet
1148 // to each instance that accept the packet.
1149 //
1150 Enqueued = 0;
1151
1152 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1153 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
1154
1155 if (IpIf->Configured) {
1156 Enqueued += Ip4InterfaceEnquePacket (IpSb, Head, Packet, IpIf);
1157 }
1158 }
1159
1160 //
1161 // Second: deliver a duplicate of the packet to each instance.
1162 // Release the local reference first, so that the last instance
1163 // getting the packet will not copy the data.
1164 //
1165 NetbufFree (Packet);
1166
1167 if (Enqueued == 0) {
1168 return EFI_NOT_FOUND;
1169 }
1170
1171 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1172 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
1173
1174 if (IpIf->Configured) {
1175 Ip4InterfaceDeliverPacket (IpSb, IpIf);
1176 }
1177 }
1178
1179 return EFI_SUCCESS;
1180 }
1181
1182
1183 /**
1184 Timeout the fragment and enqueued packets.
1185
1186 @param IpSb The IP4 service instance to timeout
1187
1188 @return None
1189
1190 **/
1191 VOID
1192 Ip4PacketTimerTicking (
1193 IN IP4_SERVICE *IpSb
1194 )
1195 {
1196 NET_LIST_ENTRY *InstanceEntry;
1197 NET_LIST_ENTRY *Entry;
1198 NET_LIST_ENTRY *Next;
1199 IP4_PROTOCOL *IpInstance;
1200 IP4_ASSEMBLE_ENTRY *Assemble;
1201 NET_BUF *Packet;
1202 IP4_CLIP_INFO *Info;
1203 UINT32 Index;
1204
1205 //
1206 // First, time out the fragments. The packet's life is counting down
1207 // once the first-arrived fragment was received.
1208 //
1209 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
1210 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {
1211 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);
1212
1213 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {
1214 NetListRemoveEntry (Entry);
1215 Ip4FreeAssembleEntry (Assemble);
1216 }
1217 }
1218 }
1219
1220 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {
1221 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);
1222
1223 //
1224 // Second, time out the assembled packets enqueued on each IP child.
1225 //
1226 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {
1227 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
1228 Info = IP4_GET_CLIP_INFO (Packet);
1229
1230 if ((Info->Life > 0) && (--Info->Life == 0)) {
1231 NetListRemoveEntry (Entry);
1232 NetbufFree (Packet);
1233 }
1234 }
1235
1236 //
1237 // Third: time out the transmitted packets.
1238 //
1239 NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);
1240 }
1241 }