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