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