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