]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Input.c
Update for NetworkPkg.
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Input.c
1 /** @file
2 IP6 internal functions to process the incoming packets.
3
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Ip6Impl.h"
17
18 /**
19 Create an empty assemble entry for the packet identified by
20 (Dst, Src, Id). The default life for the packet is 60 seconds.
21
22 @param[in] Dst The destination address.
23 @param[in] Src The source address.
24 @param[in] Id The ID field in the IP header.
25
26 @return NULL if failed to allocate memory for the entry. Otherwise,
27 the pointer to the just created reassemble entry.
28
29 **/
30 IP6_ASSEMBLE_ENTRY *
31 Ip6CreateAssembleEntry (
32 IN EFI_IPv6_ADDRESS *Dst,
33 IN EFI_IPv6_ADDRESS *Src,
34 IN UINT32 Id
35 )
36 {
37 IP6_ASSEMBLE_ENTRY *Assemble;
38
39 Assemble = AllocatePool (sizeof (IP6_ASSEMBLE_ENTRY));
40 if (Assemble == NULL) {
41 return NULL;
42 }
43
44 IP6_COPY_ADDRESS (&Assemble->Dst, Dst);
45 IP6_COPY_ADDRESS (&Assemble->Src, Src);
46 InitializeListHead (&Assemble->Fragments);
47
48 Assemble->Id = Id;
49 Assemble->Life = IP6_FRAGMENT_LIFE + 1;
50
51 Assemble->TotalLen = 0;
52 Assemble->CurLen = 0;
53 Assemble->Head = NULL;
54 Assemble->Info = NULL;
55 Assemble->Packet = NULL;
56
57 return Assemble;
58 }
59
60 /**
61 Release all the fragments of a packet, then free the assemble entry.
62
63 @param[in] Assemble The assemble entry to free.
64
65 **/
66 VOID
67 Ip6FreeAssembleEntry (
68 IN IP6_ASSEMBLE_ENTRY *Assemble
69 )
70 {
71 LIST_ENTRY *Entry;
72 LIST_ENTRY *Next;
73 NET_BUF *Fragment;
74
75 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) {
76 Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
77
78 RemoveEntryList (Entry);
79 NetbufFree (Fragment);
80 }
81
82 if (Assemble->Packet != NULL) {
83 NetbufFree (Assemble->Packet);
84 }
85
86 FreePool (Assemble);
87 }
88
89 /**
90 Release all the fragments of the packet. This is the callback for
91 the assembled packet's OnFree. It will free the assemble entry,
92 which in turn frees all the fragments of the packet.
93
94 @param[in] Arg The assemble entry to free.
95
96 **/
97 VOID
98 EFIAPI
99 Ip6OnFreeFragments (
100 IN VOID *Arg
101 )
102 {
103 Ip6FreeAssembleEntry ((IP6_ASSEMBLE_ENTRY *) Arg);
104 }
105
106 /**
107 Trim the packet to fit in [Start, End), and update per the
108 packet information.
109
110 @param[in, out] Packet Packet to trim.
111 @param[in] Start The sequence of the first byte to fit in.
112 @param[in] End One beyond the sequence of last byte to fit in.
113
114 **/
115 VOID
116 Ip6TrimPacket (
117 IN OUT NET_BUF *Packet,
118 IN INTN Start,
119 IN INTN End
120 )
121 {
122 IP6_CLIP_INFO *Info;
123 INTN Len;
124
125 Info = IP6_GET_CLIP_INFO (Packet);
126
127 ASSERT (Info->Start + Info->Length == Info->End);
128 ASSERT ((Info->Start < End) && (Start < Info->End));
129
130 if (Info->Start < Start) {
131 Len = Start - Info->Start;
132
133 NetbufTrim (Packet, (UINT32) Len, NET_BUF_HEAD);
134 Info->Start = (UINT32) Start;
135 Info->Length -= (UINT32) Len;
136 }
137
138 if (End < Info->End) {
139 Len = End - Info->End;
140
141 NetbufTrim (Packet, (UINT32) Len, NET_BUF_TAIL);
142 Info->End = (UINT32) End;
143 Info->Length -= (UINT32) Len;
144 }
145 }
146
147 /**
148 Reassemble the IP fragments. If all the fragments of the packet
149 have been received, it will wrap the packet in a net buffer then
150 return it to caller. If the packet can't be assembled, NULL is
151 returned.
152
153 @param[in, out] Table The assemble table used. A new assemble entry will be created
154 if the Packet is from a new chain of fragments.
155 @param[in] Packet The fragment to assemble. It might be freed if the fragment
156 can't be re-assembled.
157
158 @return NULL if the packet can't be reassembled. The pointer to the just assembled
159 packet if all the fragments of the packet have arrived.
160
161 **/
162 NET_BUF *
163 Ip6Reassemble (
164 IN OUT IP6_ASSEMBLE_TABLE *Table,
165 IN NET_BUF *Packet
166 )
167 {
168 EFI_IP6_HEADER *Head;
169 IP6_CLIP_INFO *This;
170 IP6_CLIP_INFO *Node;
171 IP6_ASSEMBLE_ENTRY *Assemble;
172 IP6_ASSEMBLE_ENTRY *Entry;
173 LIST_ENTRY *ListHead;
174 LIST_ENTRY *Prev;
175 LIST_ENTRY *Cur;
176 NET_BUF *Fragment;
177 NET_BUF *TmpPacket;
178 NET_BUF *NewPacket;
179 NET_BUF *Duplicate;
180 UINT8 *DupHead;
181 INTN Index;
182 UINT16 UnFragmentLen;
183 UINT8 *NextHeader;
184
185 Head = Packet->Ip.Ip6;
186 This = IP6_GET_CLIP_INFO (Packet);
187
188 ASSERT (Head != NULL);
189
190 //
191 // Find the corresponding assemble entry by (Dst, Src, Id)
192 //
193 Assemble = NULL;
194 Index = IP6_ASSEMBLE_HASH (&Head->DestinationAddress, &Head->SourceAddress, This->Id);
195
196 NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) {
197 Entry = NET_LIST_USER_STRUCT (Cur, IP6_ASSEMBLE_ENTRY, Link);
198
199 if (Entry->Id == This->Id &&
200 EFI_IP6_EQUAL (&Entry->Src, &Head->SourceAddress) &&
201 EFI_IP6_EQUAL (&Entry->Dst, &Head->DestinationAddress)
202 ) {
203 Assemble = Entry;
204 break;
205 }
206 }
207
208 //
209 // Create a new entry if can not find an existing one, insert it to assemble table
210 //
211 if (Assemble == NULL) {
212 Assemble = Ip6CreateAssembleEntry (
213 &Head->DestinationAddress,
214 &Head->SourceAddress,
215 This->Id
216 );
217
218 if (Assemble == NULL) {
219 goto Error;
220 }
221
222 InsertHeadList (&Table->Bucket[Index], &Assemble->Link);
223 }
224
225 //
226 // Find the point to insert the packet: before the first
227 // fragment with THIS.Start < CUR.Start. the previous one
228 // has PREV.Start <= THIS.Start < CUR.Start.
229 //
230 ListHead = &Assemble->Fragments;
231
232 NET_LIST_FOR_EACH (Cur, ListHead) {
233 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
234
235 if (This->Start < IP6_GET_CLIP_INFO (Fragment)->Start) {
236 break;
237 }
238 }
239
240 //
241 // Check whether the current fragment overlaps with the previous one.
242 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
243 // check whether THIS.Start < PREV.End for overlap. If two fragments
244 // overlaps, trim the overlapped part off THIS fragment.
245 //
246 if ((Cur != ListHead) && ((Prev = Cur->BackLink) != ListHead)) {
247 Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);
248 Node = IP6_GET_CLIP_INFO (Fragment);
249
250 if (This->Start < Node->End) {
251 if (This->End <= Node->End) {
252 goto Error;
253 }
254
255 //
256 // Trim the previous fragment from tail.
257 //
258 Ip6TrimPacket (Fragment, Node->Start, This->Start);
259 }
260 }
261
262 //
263 // Insert the fragment into the packet. The fragment may be removed
264 // from the list by the following checks.
265 //
266 NetListInsertBefore (Cur, &Packet->List);
267
268 //
269 // Check the packets after the insert point. It holds that:
270 // THIS.Start <= NODE.Start < NODE.End. The equality holds
271 // if PREV and NEXT are continuous. THIS fragment may fill
272 // several holes. Remove the completely overlapped fragments
273 //
274 while (Cur != ListHead) {
275 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
276 Node = IP6_GET_CLIP_INFO (Fragment);
277
278 //
279 // Remove fragments completely overlapped by this fragment
280 //
281 if (Node->End <= This->End) {
282 Cur = Cur->ForwardLink;
283
284 RemoveEntryList (&Fragment->List);
285 Assemble->CurLen -= Node->Length;
286
287 NetbufFree (Fragment);
288 continue;
289 }
290
291 //
292 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
293 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
294 // If two fragments start at the same offset, remove THIS fragment
295 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
296 //
297 if (Node->Start < This->End) {
298 if (This->Start == Node->Start) {
299 RemoveEntryList (&Packet->List);
300 goto Error;
301 }
302
303 Ip6TrimPacket (Packet, This->Start, Node->Start);
304 }
305
306 break;
307 }
308
309 //
310 // Update the assemble info: increase the current length. If it is
311 // the frist fragment, update the packet's IP head and per packet
312 // info. If it is the last fragment, update the total length.
313 //
314 Assemble->CurLen += This->Length;
315
316 if (This->Start == 0) {
317 //
318 // Once the first fragment is enqueued, it can't be removed
319 // from the fragment list. So, Assemble->Head always point
320 // to valid memory area.
321 //
322 if ((Assemble->Head != NULL) || (Assemble->Packet != NULL)) {
323 goto Error;
324 }
325
326 //
327 // Backup the first fragment in case the reasembly of that packet fail.
328 //
329 Duplicate = NetbufDuplicate (Packet, NULL, sizeof (EFI_IP6_HEADER));
330 if (Duplicate == NULL) {
331 goto Error;
332 }
333
334 //
335 // Revert IP head to network order.
336 //
337 DupHead = NetbufGetByte (Duplicate, 0, NULL);
338 ASSERT (DupHead != NULL);
339 Duplicate->Ip.Ip6 = Ip6NtohHead ((EFI_IP6_HEADER *) DupHead);
340 Assemble->Packet = Duplicate;
341
342 //
343 // Adjust the unfragmentable part in first fragment
344 //
345 UnFragmentLen = (UINT16) (This->HeadLen - sizeof (EFI_IP6_HEADER));
346 if (UnFragmentLen == 0) {
347 //
348 // There is not any unfragmentable extension header.
349 //
350 ASSERT (Head->NextHeader == IP6_FRAGMENT);
351 Head->NextHeader = This->NextHeader;
352 } else {
353 NextHeader = NetbufGetByte (
354 Packet,
355 This->FormerNextHeader + sizeof (EFI_IP6_HEADER),
356 0
357 );
358 if (NextHeader == NULL) {
359 goto Error;
360 }
361
362 *NextHeader = This->NextHeader;
363 }
364
365 Assemble->Head = Head;
366 Assemble->Info = IP6_GET_CLIP_INFO (Packet);
367 }
368
369 //
370 // Don't update the length more than once.
371 //
372 if ((This->LastFrag != 0) && (Assemble->TotalLen == 0)) {
373 Assemble->TotalLen = This->End;
374 }
375
376 //
377 // Deliver the whole packet if all the fragments received.
378 // All fragments received if:
379 // 1. received the last one, so, the totoal length is know
380 // 2. received all the data. If the last fragment on the
381 // queue ends at the total length, all data is received.
382 //
383 if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) {
384
385 RemoveEntryList (&Assemble->Link);
386
387 //
388 // If the packet is properly formated, the last fragment's End
389 // equals to the packet's total length. Otherwise, the packet
390 // is a fake, drop it now.
391 //
392 Fragment = NET_LIST_USER_STRUCT (ListHead->BackLink, NET_BUF, List);
393 if (IP6_GET_CLIP_INFO (Fragment)->End != (INTN) Assemble->TotalLen) {
394 Ip6FreeAssembleEntry (Assemble);
395 goto Error;
396 }
397
398 Fragment = NET_LIST_HEAD (ListHead, NET_BUF, List);
399 This = Assemble->Info;
400
401 //
402 // This TmpPacket is used to hold the unfragmentable part, i.e.,
403 // the IPv6 header and the unfragmentable extension headers. Be noted that
404 // the Fragment Header is exluded.
405 //
406 TmpPacket = NetbufGetFragment (Fragment, 0, This->HeadLen, 0);
407 ASSERT (TmpPacket != NULL);
408
409 NET_LIST_FOR_EACH (Cur, ListHead) {
410 //
411 // Trim off the unfragment part plus the fragment header from all fragments.
412 //
413 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
414 NetbufTrim (Fragment, This->HeadLen + sizeof (IP6_FRAGMENT_HEADER), TRUE);
415 }
416
417 InsertHeadList (ListHead, &TmpPacket->List);
418
419 //
420 // Wrap the packet in a net buffer then deliver it up
421 //
422 NewPacket = NetbufFromBufList (
423 &Assemble->Fragments,
424 0,
425 0,
426 Ip6OnFreeFragments,
427 Assemble
428 );
429
430 if (NewPacket == NULL) {
431 Ip6FreeAssembleEntry (Assemble);
432 goto Error;
433 }
434
435 NewPacket->Ip.Ip6 = Assemble->Head;
436
437 CopyMem (IP6_GET_CLIP_INFO (NewPacket), Assemble->Info, sizeof (IP6_CLIP_INFO));
438
439 return NewPacket;
440 }
441
442 return NULL;
443
444 Error:
445 NetbufFree (Packet);
446 return NULL;
447 }
448
449
450 /**
451 The callback function for the net buffer that wraps the packet processed by
452 IPsec. It releases the wrap packet and also signals IPsec to free the resources.
453
454 @param[in] Arg The wrap context.
455
456 **/
457 VOID
458 EFIAPI
459 Ip6IpSecFree (
460 IN VOID *Arg
461 )
462 {
463 IP6_IPSEC_WRAP *Wrap;
464
465 Wrap = (IP6_IPSEC_WRAP *) Arg;
466
467 if (Wrap->IpSecRecycleSignal != NULL) {
468 gBS->SignalEvent (Wrap->IpSecRecycleSignal);
469 }
470
471 NetbufFree (Wrap->Packet);
472
473 FreePool (Wrap);
474
475 return;
476 }
477
478 /**
479 The work function to locate the IPsec protocol to process the inbound or
480 outbound IP packets. The process routine handles the packet with the following
481 actions: bypass the packet, discard the packet, or protect the packet.
482
483 @param[in] IpSb The IP6 service instance.
484 @param[in, out] Head The caller-supplied IP6 header.
485 @param[in, out] LastHead The next header field of last IP header.
486 @param[in, out] Netbuf The IP6 packet to be processed by IPsec.
487 @param[in, out] ExtHdrs The caller-supplied options.
488 @param[in, out] ExtHdrsLen The length of the option.
489 @param[in] Direction The directionality in an SPD entry,
490 EfiIPsecInBound, or EfiIPsecOutBound.
491 @param[in] Context The token's wrap.
492
493 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
494 @retval EFI_SUCCESS The packet was bypassed, and all buffers remain the same.
495 @retval EFI_SUCCESS The packet was protected.
496 @retval EFI_ACCESS_DENIED The packet was discarded.
497 @retval EFI_OUT_OF_RESOURCES There are not suffcient resources to complete the operation.
498 @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than the
499 number of input data blocks when building a fragment table.
500
501 **/
502 EFI_STATUS
503 Ip6IpSecProcessPacket (
504 IN IP6_SERVICE *IpSb,
505 IN OUT EFI_IP6_HEADER **Head,
506 IN OUT UINT8 *LastHead,
507 IN OUT NET_BUF **Netbuf,
508 IN OUT UINT8 **ExtHdrs,
509 IN OUT UINT32 *ExtHdrsLen,
510 IN EFI_IPSEC_TRAFFIC_DIR Direction,
511 IN VOID *Context
512 )
513 {
514 NET_FRAGMENT *FragmentTable;
515 NET_FRAGMENT *OriginalFragmentTable;
516 UINT32 FragmentCount;
517 UINT32 OriginalFragmentCount;
518 EFI_EVENT RecycleEvent;
519 NET_BUF *Packet;
520 IP6_TXTOKEN_WRAP *TxWrap;
521 IP6_IPSEC_WRAP *IpSecWrap;
522 EFI_STATUS Status;
523 EFI_IP6_HEADER *PacketHead;
524 UINT8 *Buf;
525 EFI_IP6_HEADER ZeroHead;
526
527 Status = EFI_SUCCESS;
528 Packet = *Netbuf;
529 RecycleEvent = NULL;
530 IpSecWrap = NULL;
531 FragmentTable = NULL;
532 PacketHead = NULL;
533 Buf = NULL;
534 TxWrap = (IP6_TXTOKEN_WRAP *) Context;
535 FragmentCount = Packet->BlockOpNum;
536 ZeroMem (&ZeroHead, sizeof (EFI_IP6_HEADER));
537
538 if (mIpSec == NULL) {
539 gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **) &mIpSec);
540
541 //
542 // Check whether the ipsec protocol is available.
543 //
544 if (mIpSec == NULL) {
545 goto ON_EXIT;
546 }
547 }
548
549 //
550 // Check whether the ipsec enable variable is set.
551 //
552 if (mIpSec->DisabledFlag) {
553 //
554 // If IPsec is disabled, restore the original MTU
555 //
556 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize;
557 goto ON_EXIT;
558 } else {
559 //
560 // If IPsec is enabled, use the MTU which reduce the IPsec header length.
561 //
562 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP6_MAX_IPSEC_HEADLEN;
563 }
564
565
566 //
567 // Bypass all multicast inbound or outbound traffic.
568 //
569 if (IP6_IS_MULTICAST (&(*Head)->DestinationAddress) || IP6_IS_MULTICAST (&(*Head)->SourceAddress)) {
570 goto ON_EXIT;
571 }
572
573 //
574 // Rebuild fragment table from netbuf to ease ipsec process.
575 //
576 FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT));
577
578 if (FragmentTable == NULL) {
579 Status = EFI_OUT_OF_RESOURCES;
580 goto ON_EXIT;
581 }
582
583 Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount);
584 OriginalFragmentTable = FragmentTable;
585 OriginalFragmentCount = FragmentCount;
586
587 if (EFI_ERROR(Status)) {
588 FreePool (FragmentTable);
589 goto ON_EXIT;
590 }
591
592 //
593 // Convert host byte order to network byte order
594 //
595 Ip6NtohHead (*Head);
596
597 Status = mIpSec->ProcessExt (
598 mIpSec,
599 IpSb->Controller,
600 IP_VERSION_6,
601 (VOID *) (*Head),
602 LastHead,
603 (VOID **) ExtHdrs,
604 ExtHdrsLen,
605 (EFI_IPSEC_FRAGMENT_DATA **) (&FragmentTable),
606 &FragmentCount,
607 Direction,
608 &RecycleEvent
609 );
610 //
611 // Convert back to host byte order
612 //
613 Ip6NtohHead (*Head);
614
615 if (EFI_ERROR (Status)) {
616 FreePool (OriginalFragmentTable);
617 goto ON_EXIT;
618 }
619
620 if (OriginalFragmentCount == FragmentCount && OriginalFragmentTable == FragmentTable) {
621 //
622 // For ByPass Packet
623 //
624 FreePool (FragmentTable);
625 goto ON_EXIT;
626 } else {
627 //
628 // Free the FragmentTable which allocated before calling the IPsec.
629 //
630 FreePool (OriginalFragmentTable);
631 }
632
633 if (Direction == EfiIPsecOutBound && TxWrap != NULL) {
634 TxWrap->IpSecRecycleSignal = RecycleEvent;
635 TxWrap->Packet = NetbufFromExt (
636 FragmentTable,
637 FragmentCount,
638 IP6_MAX_HEADLEN,
639 0,
640 Ip6FreeTxToken,
641 TxWrap
642 );
643 if (TxWrap->Packet == NULL) {
644 TxWrap->Packet = *Netbuf;
645 Status = EFI_OUT_OF_RESOURCES;
646 goto ON_EXIT;
647 }
648
649 CopyMem (
650 IP6_GET_CLIP_INFO (TxWrap->Packet),
651 IP6_GET_CLIP_INFO (Packet),
652 sizeof (IP6_CLIP_INFO)
653 );
654
655 NetIpSecNetbufFree(Packet);
656 *Netbuf = TxWrap->Packet;
657
658 } else {
659
660 IpSecWrap = AllocateZeroPool (sizeof (IP6_IPSEC_WRAP));
661
662 if (IpSecWrap == NULL) {
663 Status = EFI_OUT_OF_RESOURCES;
664 gBS->SignalEvent (RecycleEvent);
665 goto ON_EXIT;
666 }
667
668 IpSecWrap->IpSecRecycleSignal = RecycleEvent;
669 IpSecWrap->Packet = Packet;
670 Packet = NetbufFromExt (
671 FragmentTable,
672 FragmentCount,
673 IP6_MAX_HEADLEN,
674 0,
675 Ip6IpSecFree,
676 IpSecWrap
677 );
678
679 if (Packet == NULL) {
680 Packet = IpSecWrap->Packet;
681 gBS->SignalEvent (RecycleEvent);
682 FreePool (IpSecWrap);
683 Status = EFI_OUT_OF_RESOURCES;
684 goto ON_EXIT;
685 }
686
687 if (Direction == EfiIPsecInBound && 0 != CompareMem (&ZeroHead, *Head, sizeof (EFI_IP6_HEADER))) {
688
689 PacketHead = (EFI_IP6_HEADER *) NetbufAllocSpace (
690 Packet,
691 sizeof (EFI_IP6_HEADER) + *ExtHdrsLen,
692 NET_BUF_HEAD
693 );
694 if (PacketHead == NULL) {
695 *Netbuf = Packet;
696 Status = EFI_OUT_OF_RESOURCES;
697 goto ON_EXIT;
698 }
699
700 CopyMem (PacketHead, *Head, sizeof (EFI_IP6_HEADER));
701 *Head = PacketHead;
702 Packet->Ip.Ip6 = PacketHead;
703
704 if (*ExtHdrs != NULL) {
705 Buf = (UINT8 *) (PacketHead + 1);
706 CopyMem (Buf, *ExtHdrs, *ExtHdrsLen);
707 }
708
709 NetbufTrim (Packet, sizeof (EFI_IP6_HEADER) + *ExtHdrsLen, TRUE);
710 CopyMem (
711 IP6_GET_CLIP_INFO (Packet),
712 IP6_GET_CLIP_INFO (IpSecWrap->Packet),
713 sizeof (IP6_CLIP_INFO)
714 );
715 }
716 *Netbuf = Packet;
717 }
718
719 ON_EXIT:
720 return Status;
721 }
722
723 /**
724 Pre-process the IPv6 packet. First validates the IPv6 packet, and
725 then reassembles packet if it is necessary.
726
727 @param[in] IpSb The IP6 service instance.
728 @param[in, out] Packet The received IP6 packet to be processed.
729 @param[in] Flag The link layer flag for the packet received, such
730 as multicast.
731 @param[out] Payload The pointer to the payload of the recieved packet.
732 it starts from the first byte of the extension header.
733 @param[out] LastHead The pointer of NextHeader of the last extension
734 header processed by IP6.
735 @param[out] ExtHdrsLen The length of the whole option.
736 @param[out] UnFragmentLen The length of unfragmented length of extension headers.
737 @param[out] Fragmented Indicate whether the packet is fragmented.
738 @param[out] Head The pointer to the EFI_IP6_Header.
739
740 @retval EFI_SUCCESS The received packet is well format.
741 @retval EFI_INVALID_PARAMETER The received packet is malformed.
742
743 **/
744 EFI_STATUS
745 Ip6PreProcessPacket (
746 IN IP6_SERVICE *IpSb,
747 IN OUT NET_BUF **Packet,
748 IN UINT32 Flag,
749 OUT UINT8 **Payload,
750 OUT UINT8 **LastHead,
751 OUT UINT32 *ExtHdrsLen,
752 OUT UINT32 *UnFragmentLen,
753 OUT BOOLEAN *Fragmented,
754 OUT EFI_IP6_HEADER **Head
755 )
756 {
757 UINT16 PayloadLen;
758 UINT16 TotalLen;
759 UINT32 FormerHeadOffset;
760 UINT32 HeadLen;
761 IP6_FRAGMENT_HEADER *FragmentHead;
762 UINT16 FragmentOffset;
763 IP6_CLIP_INFO *Info;
764 EFI_IPv6_ADDRESS Loopback;
765
766 HeadLen = 0;
767 PayloadLen = 0;
768 //
769 // Check whether the input packet is a valid packet
770 //
771 if ((*Packet)->TotalSize < IP6_MIN_HEADLEN) {
772 return EFI_INVALID_PARAMETER;
773 }
774
775 //
776 // Get header information of the packet.
777 //
778 *Head = (EFI_IP6_HEADER *) NetbufGetByte (*Packet, 0, NULL);
779 if (*Head == NULL) {
780 return EFI_INVALID_PARAMETER;
781 }
782
783 //
784 // Multicast addresses must not be used as source addresses in IPv6 packets.
785 //
786 if (((*Head)->Version != 6) || (IP6_IS_MULTICAST (&(*Head)->SourceAddress))) {
787 return EFI_INVALID_PARAMETER;
788 }
789
790 //
791 // A packet with a destination address of loopback ::1/128 or unspecified must be dropped.
792 //
793 ZeroMem (&Loopback, sizeof (EFI_IPv6_ADDRESS));
794 Loopback.Addr[15] = 0x1;
795 if ((CompareMem (&Loopback, &(*Head)->DestinationAddress, sizeof (EFI_IPv6_ADDRESS)) == 0) ||
796 (NetIp6IsUnspecifiedAddr (&(*Head)->DestinationAddress))) {
797 return EFI_INVALID_PARAMETER;
798 }
799
800 //
801 // Convert the IP header to host byte order.
802 //
803 (*Packet)->Ip.Ip6 = Ip6NtohHead (*Head);
804
805 //
806 // Get the per packet info.
807 //
808 Info = IP6_GET_CLIP_INFO (*Packet);
809 Info->LinkFlag = Flag;
810 Info->CastType = 0;
811
812 if (IpSb->MnpConfigData.EnablePromiscuousReceive) {
813 Info->CastType = Ip6Promiscuous;
814 }
815
816 if (Ip6IsOneOfSetAddress (IpSb, &(*Head)->DestinationAddress, NULL, NULL)) {
817 Info->CastType = Ip6Unicast;
818 } else if (IP6_IS_MULTICAST (&(*Head)->DestinationAddress)) {
819 if (Ip6FindMldEntry (IpSb, &(*Head)->DestinationAddress) != NULL) {
820 Info->CastType = Ip6Multicast;
821 }
822 }
823
824 //
825 // Drop the packet that is not delivered to us.
826 //
827 if (Info->CastType == 0) {
828 return EFI_INVALID_PARAMETER;
829 }
830
831
832 PayloadLen = (*Head)->PayloadLength;
833
834 Info->Start = 0;
835 Info->Length = PayloadLen;
836 Info->End = Info->Start + Info->Length;
837 Info->HeadLen = (UINT16) sizeof (EFI_IP6_HEADER);
838 Info->Status = EFI_SUCCESS;
839 Info->LastFrag = FALSE;
840
841 TotalLen = (UINT16) (PayloadLen + sizeof (EFI_IP6_HEADER));
842
843 //
844 // Mnp may deliver frame trailer sequence up, trim it off.
845 //
846 if (TotalLen < (*Packet)->TotalSize) {
847 NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE);
848 }
849
850 if (TotalLen != (*Packet)->TotalSize) {
851 return EFI_INVALID_PARAMETER;
852 }
853
854 //
855 // Check the extension headers, if exist validate them
856 //
857 if (PayloadLen != 0) {
858 *Payload = AllocatePool ((UINTN) PayloadLen);
859 if (*Payload == NULL) {
860 return EFI_INVALID_PARAMETER;
861 }
862
863 NetbufCopy (*Packet, sizeof (EFI_IP6_HEADER), PayloadLen, *Payload);
864 }
865
866 if (!Ip6IsExtsValid (
867 IpSb,
868 *Packet,
869 &(*Head)->NextHeader,
870 *Payload,
871 (UINT32) PayloadLen,
872 TRUE,
873 &FormerHeadOffset,
874 LastHead,
875 ExtHdrsLen,
876 UnFragmentLen,
877 Fragmented
878 )) {
879 return EFI_INVALID_PARAMETER;
880 }
881
882 HeadLen = sizeof (EFI_IP6_HEADER) + *UnFragmentLen;
883
884 if (*Fragmented) {
885 //
886 // Get the fragment offset from the Fragment header
887 //
888 FragmentHead = (IP6_FRAGMENT_HEADER *) NetbufGetByte (*Packet, HeadLen, NULL);
889 if (FragmentHead == NULL) {
890 return EFI_INVALID_PARAMETER;
891 }
892
893 FragmentOffset = NTOHS (FragmentHead->FragmentOffset);
894
895 if ((FragmentOffset & 0x1) == 0) {
896 Info->LastFrag = TRUE;
897 }
898
899 FragmentOffset &= (~0x1);
900
901 //
902 // This is the first fragment of the packet
903 //
904 if (FragmentOffset == 0) {
905 Info->NextHeader = FragmentHead->NextHeader;
906 }
907
908 Info->HeadLen = (UINT16) HeadLen;
909 HeadLen += sizeof (IP6_FRAGMENT_HEADER);
910 Info->Start = FragmentOffset;
911 Info->Length = TotalLen - (UINT16) HeadLen;
912 Info->End = Info->Start + Info->Length;
913 Info->Id = FragmentHead->Identification;
914 Info->FormerNextHeader = FormerHeadOffset;
915
916 //
917 // Fragments should in the unit of 8 octets long except the last one.
918 //
919 if ((Info->LastFrag == 0) && (Info->Length % 8 != 0)) {
920 return EFI_INVALID_PARAMETER;
921 }
922
923 //
924 // Reassemble the packet.
925 //
926 *Packet = Ip6Reassemble (&IpSb->Assemble, *Packet);
927 if (*Packet == NULL) {
928 return EFI_INVALID_PARAMETER;
929 }
930
931 //
932 // Re-check the assembled packet to get the right values.
933 //
934 *Head = (*Packet)->Ip.Ip6;
935 PayloadLen = (*Head)->PayloadLength;
936 if (PayloadLen != 0) {
937 if (*Payload != NULL) {
938 FreePool (*Payload);
939 }
940
941 *Payload = AllocatePool ((UINTN) PayloadLen);
942 if (*Payload == NULL) {
943 return EFI_INVALID_PARAMETER;
944 }
945
946 NetbufCopy (*Packet, sizeof (EFI_IP6_HEADER), PayloadLen, *Payload);
947 }
948
949 if (!Ip6IsExtsValid (
950 IpSb,
951 *Packet,
952 &(*Head)->NextHeader,
953 *Payload,
954 (UINT32) PayloadLen,
955 TRUE,
956 NULL,
957 LastHead,
958 ExtHdrsLen,
959 UnFragmentLen,
960 Fragmented
961 )) {
962 return EFI_INVALID_PARAMETER;
963 }
964 }
965
966 //
967 // Trim the head off, after this point, the packet is headless.
968 // and Packet->TotalLen == Info->Length.
969 //
970 NetbufTrim (*Packet, sizeof (EFI_IP6_HEADER) + *ExtHdrsLen, TRUE);
971
972 return EFI_SUCCESS;
973 }
974
975 /**
976 The IP6 input routine. It is called by the IP6_INTERFACE when an
977 IP6 fragment is received from MNP.
978
979 @param[in] Packet The IP6 packet received.
980 @param[in] IoStatus The return status of receive request.
981 @param[in] Flag The link layer flag for the packet received, such
982 as multicast.
983 @param[in] Context The IP6 service instance that owns the MNP.
984
985 **/
986 VOID
987 Ip6AcceptFrame (
988 IN NET_BUF *Packet,
989 IN EFI_STATUS IoStatus,
990 IN UINT32 Flag,
991 IN VOID *Context
992 )
993 {
994 IP6_SERVICE *IpSb;
995 EFI_IP6_HEADER *Head;
996 UINT8 *Payload;
997 UINT8 *LastHead;
998 UINT32 UnFragmentLen;
999 UINT32 ExtHdrsLen;
1000 BOOLEAN Fragmented;
1001 EFI_STATUS Status;
1002 EFI_IP6_HEADER ZeroHead;
1003
1004 IpSb = (IP6_SERVICE *) Context;
1005 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1006
1007 Payload = NULL;
1008 LastHead = NULL;
1009
1010 //
1011 // Check input parameters
1012 //
1013 if (EFI_ERROR (IoStatus) || (IpSb->State == IP6_SERVICE_DESTROY)) {
1014 goto Drop;
1015 }
1016
1017 //
1018 // Pre-Process the Ipv6 Packet and then reassemble if it is necessary.
1019 //
1020 Status = Ip6PreProcessPacket (
1021 IpSb,
1022 &Packet,
1023 Flag,
1024 &Payload,
1025 &LastHead,
1026 &ExtHdrsLen,
1027 &UnFragmentLen,
1028 &Fragmented,
1029 &Head
1030 );
1031 if (EFI_ERROR (Status)) {
1032 goto Restart;
1033 }
1034 //
1035 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
1036 // and no need consider any other ahead ext headers.
1037 //
1038 Status = Ip6IpSecProcessPacket (
1039 IpSb,
1040 &Head,
1041 LastHead, // need get the lasthead value for input
1042 &Packet,
1043 &Payload,
1044 &ExtHdrsLen,
1045 EfiIPsecInBound,
1046 NULL
1047 );
1048
1049 if (EFI_ERROR (Status)) {
1050 goto Restart;
1051 }
1052
1053 //
1054 // If the packet is protected by IPsec Tunnel Mode, Check the Inner Ip Packet.
1055 //
1056 ZeroMem (&ZeroHead, sizeof (EFI_IP6_HEADER));
1057 if (0 == CompareMem (Head, &ZeroHead, sizeof (EFI_IP6_HEADER))) {
1058 Status = Ip6PreProcessPacket (
1059 IpSb,
1060 &Packet,
1061 Flag,
1062 &Payload,
1063 &LastHead,
1064 &ExtHdrsLen,
1065 &UnFragmentLen,
1066 &Fragmented,
1067 &Head
1068 );
1069 if (EFI_ERROR (Status)) {
1070 goto Restart;
1071 }
1072 }
1073
1074 //
1075 // Check the Packet again.
1076 //
1077 if (Packet == NULL) {
1078 goto Restart;
1079 }
1080
1081 //
1082 // Packet may have been changed. The ownership of the packet
1083 // is transfered to the packet process logic.
1084 //
1085 Head = Packet->Ip.Ip6;
1086 IP6_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;
1087
1088 switch (*LastHead) {
1089 case IP6_ICMP:
1090 Ip6IcmpHandle (IpSb, Head, Packet);
1091 break;
1092 default:
1093 Ip6Demultiplex (IpSb, Head, Packet);
1094 }
1095
1096 Packet = NULL;
1097
1098 //
1099 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
1100 // which are signaled with received data.
1101 //
1102 DispatchDpc ();
1103
1104 Restart:
1105 if (Payload != NULL) {
1106 FreePool (Payload);
1107 }
1108
1109 Ip6ReceiveFrame (Ip6AcceptFrame, IpSb);
1110
1111 Drop:
1112 if (Packet != NULL) {
1113 NetbufFree (Packet);
1114 }
1115
1116 return ;
1117 }
1118
1119 /**
1120 Initialize an already allocated assemble table. This is generally
1121 the assemble table embedded in the IP6 service instance.
1122
1123 @param[in, out] Table The assemble table to initialize.
1124
1125 **/
1126 VOID
1127 Ip6CreateAssembleTable (
1128 IN OUT IP6_ASSEMBLE_TABLE *Table
1129 )
1130 {
1131 UINT32 Index;
1132
1133 for (Index = 0; Index < IP6_ASSEMLE_HASH_SIZE; Index++) {
1134 InitializeListHead (&Table->Bucket[Index]);
1135 }
1136 }
1137
1138 /**
1139 Clean up the assemble table by removing all of the fragments
1140 and assemble entries.
1141
1142 @param[in, out] Table The assemble table to clean up.
1143
1144 **/
1145 VOID
1146 Ip6CleanAssembleTable (
1147 IN OUT IP6_ASSEMBLE_TABLE *Table
1148 )
1149 {
1150 LIST_ENTRY *Entry;
1151 LIST_ENTRY *Next;
1152 IP6_ASSEMBLE_ENTRY *Assemble;
1153 UINT32 Index;
1154
1155 for (Index = 0; Index < IP6_ASSEMLE_HASH_SIZE; Index++) {
1156 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Table->Bucket[Index]) {
1157 Assemble = NET_LIST_USER_STRUCT (Entry, IP6_ASSEMBLE_ENTRY, Link);
1158
1159 RemoveEntryList (Entry);
1160 Ip6FreeAssembleEntry (Assemble);
1161 }
1162 }
1163 }
1164
1165
1166 /**
1167 The signal handle of IP6's recycle event. It is called back
1168 when the upper layer releases the packet.
1169
1170 @param[in] Event The IP6's recycle event.
1171 @param[in] Context The context of the handle, which is a IP6_RXDATA_WRAP.
1172
1173 **/
1174 VOID
1175 EFIAPI
1176 Ip6OnRecyclePacket (
1177 IN EFI_EVENT Event,
1178 IN VOID *Context
1179 )
1180 {
1181 IP6_RXDATA_WRAP *Wrap;
1182
1183 Wrap = (IP6_RXDATA_WRAP *) Context;
1184
1185 EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock);
1186 RemoveEntryList (&Wrap->Link);
1187 EfiReleaseLock (&Wrap->IpInstance->RecycleLock);
1188
1189 ASSERT (!NET_BUF_SHARED (Wrap->Packet));
1190 NetbufFree (Wrap->Packet);
1191
1192 gBS->CloseEvent (Wrap->RxData.RecycleSignal);
1193 FreePool (Wrap);
1194 }
1195
1196 /**
1197 Wrap the received packet to a IP6_RXDATA_WRAP, which will be
1198 delivered to the upper layer. Each IP6 child that accepts the
1199 packet will get a not-shared copy of the packet which is wrapped
1200 in the IP6_RXDATA_WRAP. The IP6_RXDATA_WRAP->RxData is passed
1201 to the upper layer. The upper layer will signal the recycle event in
1202 it when it is done with the packet.
1203
1204 @param[in] IpInstance The IP6 child to receive the packet.
1205 @param[in] Packet The packet to deliver up.
1206
1207 @return NULL if it failed to wrap the packet; otherwise, the wrapper.
1208
1209 **/
1210 IP6_RXDATA_WRAP *
1211 Ip6WrapRxData (
1212 IN IP6_PROTOCOL *IpInstance,
1213 IN NET_BUF *Packet
1214 )
1215 {
1216 IP6_RXDATA_WRAP *Wrap;
1217 EFI_IP6_RECEIVE_DATA *RxData;
1218 EFI_STATUS Status;
1219
1220 Wrap = AllocatePool (IP6_RXDATA_WRAP_SIZE (Packet->BlockOpNum));
1221
1222 if (Wrap == NULL) {
1223 return NULL;
1224 }
1225
1226 InitializeListHead (&Wrap->Link);
1227
1228 Wrap->IpInstance = IpInstance;
1229 Wrap->Packet = Packet;
1230 RxData = &Wrap->RxData;
1231
1232 ZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME));
1233
1234 Status = gBS->CreateEvent (
1235 EVT_NOTIFY_SIGNAL,
1236 TPL_NOTIFY,
1237 Ip6OnRecyclePacket,
1238 Wrap,
1239 &RxData->RecycleSignal
1240 );
1241
1242 if (EFI_ERROR (Status)) {
1243 FreePool (Wrap);
1244 return NULL;
1245 }
1246
1247 ASSERT (Packet->Ip.Ip6 != NULL);
1248
1249 //
1250 // The application expects a network byte order header.
1251 //
1252 RxData->HeaderLength = sizeof (EFI_IP6_HEADER);
1253 RxData->Header = (EFI_IP6_HEADER *) Ip6NtohHead (Packet->Ip.Ip6);
1254 RxData->DataLength = Packet->TotalSize;
1255
1256 //
1257 // Build the fragment table to be delivered up.
1258 //
1259 RxData->FragmentCount = Packet->BlockOpNum;
1260 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);
1261
1262 return Wrap;
1263 }
1264
1265 /**
1266 Check whether this IP child accepts the packet.
1267
1268 @param[in] IpInstance The IP child to check.
1269 @param[in] Head The IP header of the packet.
1270 @param[in] Packet The data of the packet.
1271
1272 @retval TRUE The child wants to receive the packet.
1273 @retval FALSE The child does not want to receive the packet.
1274
1275 **/
1276 BOOLEAN
1277 Ip6InstanceFrameAcceptable (
1278 IN IP6_PROTOCOL *IpInstance,
1279 IN EFI_IP6_HEADER *Head,
1280 IN NET_BUF *Packet
1281 )
1282 {
1283 IP6_ICMP_ERROR_HEAD Icmp;
1284 EFI_IP6_CONFIG_DATA *Config;
1285 IP6_CLIP_INFO *Info;
1286 UINT8 *Proto;
1287 UINT32 Index;
1288 UINT8 *ExtHdrs;
1289 UINT16 ErrMsgPayloadLen;
1290 UINT8 *ErrMsgPayload;
1291
1292 Config = &IpInstance->ConfigData;
1293 Proto = NULL;
1294
1295 //
1296 // Dirty trick for the Tiano UEFI network stack implmentation. If
1297 // ReceiveTimeout == -1, the receive of the packet for this instance
1298 // is disabled. The UEFI spec don't have such captibility. We add
1299 // this to improve the performance because IP will make a copy of
1300 // the received packet for each accepting instance. Some IP instances
1301 // used by UDP/TCP only send packets, they don't wants to receive.
1302 //
1303 if (Config->ReceiveTimeout == (UINT32)(-1)) {
1304 return FALSE;
1305 }
1306
1307 if (Config->AcceptPromiscuous) {
1308 return TRUE;
1309 }
1310
1311 //
1312 // Check whether the protocol is acceptable.
1313 //
1314 ExtHdrs = NetbufGetByte (Packet, 0, NULL);
1315
1316 if (!Ip6IsExtsValid (
1317 IpInstance->Service,
1318 Packet,
1319 &Head->NextHeader,
1320 ExtHdrs,
1321 (UINT32) Head->PayloadLength,
1322 TRUE,
1323 NULL,
1324 &Proto,
1325 NULL,
1326 NULL,
1327 NULL
1328 )) {
1329 return FALSE;
1330 }
1331
1332 //
1333 // The upper layer driver may want to receive the ICMPv6 error packet
1334 // invoked by its packet, like UDP.
1335 //
1336 if ((*Proto == IP6_ICMP) && (!Config->AcceptAnyProtocol) && (*Proto != Config->DefaultProtocol)) {
1337 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1338
1339 if (Icmp.Head.Type <= ICMP_V6_ERROR_MAX) {
1340 if (!Config->AcceptIcmpErrors) {
1341 return FALSE;
1342 }
1343
1344 //
1345 // Get the protocol of the invoking packet of ICMPv6 error packet.
1346 //
1347 ErrMsgPayloadLen = NTOHS (Icmp.IpHead.PayloadLength);
1348 ErrMsgPayload = NetbufGetByte (Packet, sizeof (Icmp), NULL);
1349
1350 if (!Ip6IsExtsValid (
1351 NULL,
1352 NULL,
1353 &Icmp.IpHead.NextHeader,
1354 ErrMsgPayload,
1355 ErrMsgPayloadLen,
1356 TRUE,
1357 NULL,
1358 &Proto,
1359 NULL,
1360 NULL,
1361 NULL
1362 )) {
1363 return FALSE;
1364 }
1365 }
1366 }
1367
1368 //
1369 // Match the protocol
1370 //
1371 if (!Config->AcceptAnyProtocol && (*Proto != Config->DefaultProtocol)) {
1372 return FALSE;
1373 }
1374
1375 //
1376 // Check for broadcast, the caller has computed the packet's
1377 // cast type for this child's interface.
1378 //
1379 Info = IP6_GET_CLIP_INFO (Packet);
1380
1381 //
1382 // If it is a multicast packet, check whether we are in the group.
1383 //
1384 if (Info->CastType == Ip6Multicast) {
1385 //
1386 // Receive the multicast if the instance wants to receive all packets.
1387 //
1388 if (NetIp6IsUnspecifiedAddr (&IpInstance->ConfigData.StationAddress)) {
1389 return TRUE;
1390 }
1391
1392 for (Index = 0; Index < IpInstance->GroupCount; Index++) {
1393 if (EFI_IP6_EQUAL (IpInstance->GroupList + Index, &Head->DestinationAddress)) {
1394 break;
1395 }
1396 }
1397
1398 return (BOOLEAN)(Index < IpInstance->GroupCount);
1399 }
1400
1401 return TRUE;
1402 }
1403
1404 /**
1405 Enqueue a shared copy of the packet to the IP6 child if the
1406 packet is acceptable to it. Here the data of the packet is
1407 shared, but the net buffer isn't.
1408
1409 @param IpInstance The IP6 child to enqueue the packet to.
1410 @param Head The IP header of the received packet.
1411 @param Packet The data of the received packet.
1412
1413 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1414 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet.
1415 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources
1416 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1417
1418 **/
1419 EFI_STATUS
1420 Ip6InstanceEnquePacket (
1421 IN IP6_PROTOCOL *IpInstance,
1422 IN EFI_IP6_HEADER *Head,
1423 IN NET_BUF *Packet
1424 )
1425 {
1426 IP6_CLIP_INFO *Info;
1427 NET_BUF *Clone;
1428
1429 //
1430 // Check whether the packet is acceptable to this instance.
1431 //
1432 if (IpInstance->State != IP6_STATE_CONFIGED) {
1433 return EFI_NOT_STARTED;
1434 }
1435
1436 if (!Ip6InstanceFrameAcceptable (IpInstance, Head, Packet)) {
1437 return EFI_INVALID_PARAMETER;
1438 }
1439
1440 //
1441 // Enque a shared copy of the packet.
1442 //
1443 Clone = NetbufClone (Packet);
1444
1445 if (Clone == NULL) {
1446 return EFI_OUT_OF_RESOURCES;
1447 }
1448
1449 //
1450 // Set the receive time out for the assembled packet. If it expires,
1451 // packet will be removed from the queue.
1452 //
1453 Info = IP6_GET_CLIP_INFO (Clone);
1454 Info->Life = IP6_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);
1455
1456 InsertTailList (&IpInstance->Received, &Clone->List);
1457 return EFI_SUCCESS;
1458 }
1459
1460 /**
1461 Deliver the received packets to the upper layer if there are both received
1462 requests and enqueued packets. If the enqueued packet is shared, it will
1463 duplicate it to a non-shared packet, release the shared packet, then
1464 deliver the non-shared packet up.
1465
1466 @param[in] IpInstance The IP child to deliver the packet up.
1467
1468 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1469 packets.
1470 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1471 are delivered up.
1472
1473 **/
1474 EFI_STATUS
1475 Ip6InstanceDeliverPacket (
1476 IN IP6_PROTOCOL *IpInstance
1477 )
1478 {
1479 EFI_IP6_COMPLETION_TOKEN *Token;
1480 IP6_RXDATA_WRAP *Wrap;
1481 NET_BUF *Packet;
1482 NET_BUF *Dup;
1483 UINT8 *Head;
1484
1485 //
1486 // Deliver a packet if there are both a packet and a receive token.
1487 //
1488 while (!IsListEmpty (&IpInstance->Received) && !NetMapIsEmpty (&IpInstance->RxTokens)) {
1489
1490 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);
1491
1492 if (!NET_BUF_SHARED (Packet)) {
1493 //
1494 // If this is the only instance that wants the packet, wrap it up.
1495 //
1496 Wrap = Ip6WrapRxData (IpInstance, Packet);
1497
1498 if (Wrap == NULL) {
1499 return EFI_OUT_OF_RESOURCES;
1500 }
1501
1502 RemoveEntryList (&Packet->List);
1503
1504 } else {
1505 //
1506 // Create a duplicated packet if this packet is shared
1507 //
1508 Dup = NetbufDuplicate (Packet, NULL, sizeof (EFI_IP6_HEADER));
1509
1510 if (Dup == NULL) {
1511 return EFI_OUT_OF_RESOURCES;
1512 }
1513
1514 //
1515 // Copy the IP head over. The packet to deliver up is
1516 // headless. Trim the head off after copy. The IP head
1517 // may be not continuous before the data.
1518 //
1519 Head = NetbufAllocSpace (Dup, sizeof (EFI_IP6_HEADER), NET_BUF_HEAD);
1520 ASSERT (Head != NULL);
1521 Dup->Ip.Ip6 = (EFI_IP6_HEADER *) Head;
1522
1523 CopyMem (Head, Packet->Ip.Ip6, sizeof (EFI_IP6_HEADER));
1524 NetbufTrim (Dup, sizeof (EFI_IP6_HEADER), TRUE);
1525
1526 Wrap = Ip6WrapRxData (IpInstance, Dup);
1527
1528 if (Wrap == NULL) {
1529 NetbufFree (Dup);
1530 return EFI_OUT_OF_RESOURCES;
1531 }
1532
1533 RemoveEntryList (&Packet->List);
1534 NetbufFree (Packet);
1535
1536 Packet = Dup;
1537 }
1538
1539 //
1540 // Insert it into the delivered packet, then get a user's
1541 // receive token, pass the wrapped packet up.
1542 //
1543 EfiAcquireLockOrFail (&IpInstance->RecycleLock);
1544 InsertHeadList (&IpInstance->Delivered, &Wrap->Link);
1545 EfiReleaseLock (&IpInstance->RecycleLock);
1546
1547 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);
1548 Token->Status = IP6_GET_CLIP_INFO (Packet)->Status;
1549 Token->Packet.RxData = &Wrap->RxData;
1550
1551 gBS->SignalEvent (Token->Event);
1552 }
1553
1554 return EFI_SUCCESS;
1555 }
1556
1557 /**
1558 Enqueue a received packet to all the IP children that share
1559 the same interface.
1560
1561 @param[in] IpSb The IP6 service instance that receive the packet.
1562 @param[in] Head The header of the received packet.
1563 @param[in] Packet The data of the received packet.
1564 @param[in] IpIf The interface to enqueue the packet to.
1565
1566 @return The number of the IP6 children that accepts the packet.
1567
1568 **/
1569 INTN
1570 Ip6InterfaceEnquePacket (
1571 IN IP6_SERVICE *IpSb,
1572 IN EFI_IP6_HEADER *Head,
1573 IN NET_BUF *Packet,
1574 IN IP6_INTERFACE *IpIf
1575 )
1576 {
1577 IP6_PROTOCOL *IpInstance;
1578 IP6_CLIP_INFO *Info;
1579 LIST_ENTRY *Entry;
1580 INTN Enqueued;
1581 INTN LocalType;
1582 INTN SavedType;
1583
1584 //
1585 // First, check that the packet is acceptable to this interface
1586 // and find the local cast type for the interface.
1587 //
1588 LocalType = 0;
1589 Info = IP6_GET_CLIP_INFO (Packet);
1590
1591 if (IpIf->PromiscRecv) {
1592 LocalType = Ip6Promiscuous;
1593 } else {
1594 LocalType = Info->CastType;
1595 }
1596
1597 //
1598 // Iterate through the ip instances on the interface, enqueue
1599 // the packet if filter passed. Save the original cast type,
1600 // and pass the local cast type to the IP children on the
1601 // interface. The global cast type will be restored later.
1602 //
1603 SavedType = Info->CastType;
1604 Info->CastType = (UINT32) LocalType;
1605
1606 Enqueued = 0;
1607
1608 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1609 IpInstance = NET_LIST_USER_STRUCT (Entry, IP6_PROTOCOL, AddrLink);
1610 NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE);
1611
1612 if (Ip6InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {
1613 Enqueued++;
1614 }
1615 }
1616
1617 Info->CastType = (UINT32) SavedType;
1618 return Enqueued;
1619 }
1620
1621 /**
1622 Deliver the packet for each IP6 child on the interface.
1623
1624 @param[in] IpSb The IP6 service instance that received the packet.
1625 @param[in] IpIf The IP6 interface to deliver the packet.
1626
1627 **/
1628 VOID
1629 Ip6InterfaceDeliverPacket (
1630 IN IP6_SERVICE *IpSb,
1631 IN IP6_INTERFACE *IpIf
1632 )
1633 {
1634 IP6_PROTOCOL *IpInstance;
1635 LIST_ENTRY *Entry;
1636
1637 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
1638 IpInstance = NET_LIST_USER_STRUCT (Entry, IP6_PROTOCOL, AddrLink);
1639 Ip6InstanceDeliverPacket (IpInstance);
1640 }
1641 }
1642
1643 /**
1644 De-multiplex the packet. the packet delivery is processed in two
1645 passes. The first pass will enqueue a shared copy of the packet
1646 to each IP6 child that accepts the packet. The second pass will
1647 deliver a non-shared copy of the packet to each IP6 child that
1648 has pending receive requests. Data is copied if more than one
1649 child wants to consume the packet, because each IP child needs
1650 its own copy of the packet to make changes.
1651
1652 @param[in] IpSb The IP6 service instance that received the packet.
1653 @param[in] Head The header of the received packet.
1654 @param[in] Packet The data of the received packet.
1655
1656 @retval EFI_NOT_FOUND No IP child accepts the packet.
1657 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1658 children.
1659
1660 **/
1661 EFI_STATUS
1662 Ip6Demultiplex (
1663 IN IP6_SERVICE *IpSb,
1664 IN EFI_IP6_HEADER *Head,
1665 IN NET_BUF *Packet
1666 )
1667 {
1668
1669 LIST_ENTRY *Entry;
1670 IP6_INTERFACE *IpIf;
1671 INTN Enqueued;
1672
1673 //
1674 // Two pass delivery: first, enque a shared copy of the packet
1675 // to each instance that accept the packet.
1676 //
1677 Enqueued = 0;
1678
1679 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1680 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
1681
1682 if (IpIf->Configured) {
1683 Enqueued += Ip6InterfaceEnquePacket (IpSb, Head, Packet, IpIf);
1684 }
1685 }
1686
1687 //
1688 // Second: deliver a duplicate of the packet to each instance.
1689 // Release the local reference first, so that the last instance
1690 // getting the packet will not copy the data.
1691 //
1692 NetbufFree (Packet);
1693 Packet = NULL;
1694
1695 if (Enqueued == 0) {
1696 return EFI_NOT_FOUND;
1697 }
1698
1699 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1700 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
1701
1702 if (IpIf->Configured) {
1703 Ip6InterfaceDeliverPacket (IpSb, IpIf);
1704 }
1705 }
1706
1707 return EFI_SUCCESS;
1708 }
1709
1710 /**
1711 Decrease the life of the transmitted packets. If it is
1712 decreased to zero, cancel the packet. This function is
1713 called by Ip6packetTimerTicking that provides timeout for both the
1714 received-but-not-delivered and transmitted-but-not-recycle
1715 packets.
1716
1717 @param[in] Map The IP6 child's transmit map.
1718 @param[in] Item Current transmitted packet.
1719 @param[in] Context Not used.
1720
1721 @retval EFI_SUCCESS Always returns EFI_SUCCESS.
1722
1723 **/
1724 EFI_STATUS
1725 EFIAPI
1726 Ip6SentPacketTicking (
1727 IN NET_MAP *Map,
1728 IN NET_MAP_ITEM *Item,
1729 IN VOID *Context
1730 )
1731 {
1732 IP6_TXTOKEN_WRAP *Wrap;
1733
1734 Wrap = (IP6_TXTOKEN_WRAP *) Item->Value;
1735 ASSERT (Wrap != NULL);
1736
1737 if ((Wrap->Life > 0) && (--Wrap->Life == 0)) {
1738 Ip6CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);
1739 }
1740
1741 return EFI_SUCCESS;
1742 }
1743
1744 /**
1745 Timeout the fragments, and the enqueued, and transmitted packets.
1746
1747 @param[in] IpSb The IP6 service instance to timeout.
1748
1749 **/
1750 VOID
1751 Ip6PacketTimerTicking (
1752 IN IP6_SERVICE *IpSb
1753 )
1754 {
1755 LIST_ENTRY *InstanceEntry;
1756 LIST_ENTRY *Entry;
1757 LIST_ENTRY *Next;
1758 IP6_PROTOCOL *IpInstance;
1759 IP6_ASSEMBLE_ENTRY *Assemble;
1760 NET_BUF *Packet;
1761 IP6_CLIP_INFO *Info;
1762 UINT32 Index;
1763
1764 //
1765 // First, time out the fragments. The packet's life is counting down
1766 // once the first-arriving fragment of that packet was received.
1767 //
1768 for (Index = 0; Index < IP6_ASSEMLE_HASH_SIZE; Index++) {
1769 NET_LIST_FOR_EACH_SAFE (Entry, Next, &(IpSb->Assemble.Bucket[Index])) {
1770 Assemble = NET_LIST_USER_STRUCT (Entry, IP6_ASSEMBLE_ENTRY, Link);
1771
1772 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {
1773 //
1774 // If the first fragment (the one with a Fragment Offset of zero)
1775 // has been received, an ICMP Time Exceeded - Fragment Reassembly
1776 // Time Exceeded message should be sent to the source of that fragment.
1777 //
1778 if ((Assemble->Packet != NULL) &&
1779 !IP6_IS_MULTICAST (&Assemble->Head->DestinationAddress)) {
1780 Ip6SendIcmpError (
1781 IpSb,
1782 Assemble->Packet,
1783 NULL,
1784 &Assemble->Head->SourceAddress,
1785 ICMP_V6_TIME_EXCEEDED,
1786 ICMP_V6_TIMEOUT_REASSEMBLE,
1787 NULL
1788 );
1789 }
1790
1791 //
1792 // If reassembly of a packet is not completed within 60 seconds of
1793 // the reception of the first-arriving fragment of that packet, the
1794 // reassembly must be abandoned and all the fragments that have been
1795 // received for that packet must be discarded.
1796 //
1797 RemoveEntryList (Entry);
1798 Ip6FreeAssembleEntry (Assemble);
1799 }
1800 }
1801 }
1802
1803 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {
1804 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP6_PROTOCOL, Link);
1805
1806 //
1807 // Second, time out the assembled packets enqueued on each IP child.
1808 //
1809 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {
1810 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
1811 Info = IP6_GET_CLIP_INFO (Packet);
1812
1813 if ((Info->Life > 0) && (--Info->Life == 0)) {
1814 RemoveEntryList (Entry);
1815 NetbufFree (Packet);
1816 }
1817 }
1818
1819 //
1820 // Third: time out the transmitted packets.
1821 //
1822 NetMapIterate (&IpInstance->TxTokens, Ip6SentPacketTicking, NULL);
1823 }
1824 }
1825