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