]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Nd.c
Add NetworkPkg (P.UDK2010.UP3.Network.P1)
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Nd.c
1 /** @file
2 Implementation of Neighbor Discovery support routines.
3
4 Copyright (c) 2009 - 2010, 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 EFI_MAC_ADDRESS mZeroMacAddress;
19
20 /**
21 Update the ReachableTime in IP6 service binding instance data, in milliseconds.
22
23 @param[in, out] IpSb Points to the IP6_SERVICE.
24
25 **/
26 VOID
27 Ip6UpdateReachableTime (
28 IN OUT IP6_SERVICE *IpSb
29 )
30 {
31 UINT32 Random;
32
33 Random = (NetRandomInitSeed () / 4294967295UL) * IP6_RANDOM_FACTOR_SCALE;
34 Random = Random + IP6_MIN_RANDOM_FACTOR_SCALED;
35 IpSb->ReachableTime = (IpSb->BaseReachableTime * Random) / IP6_RANDOM_FACTOR_SCALE;
36 }
37
38 /**
39 Build a array of EFI_IP6_NEIGHBOR_CACHE to be returned to the caller. The number
40 of EFI_IP6_NEIGHBOR_CACHE is also returned.
41
42 @param[in] IpInstance The pointer to IP6_PROTOCOL instance.
43 @param[out] NeighborCount The number of returned neighbor cache entries.
44 @param[out] NeighborCache The pointer to the array of EFI_IP6_NEIGHBOR_CACHE.
45
46 @retval EFI_SUCCESS The EFI_IP6_NEIGHBOR_CACHE successfully built.
47 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table.
48
49 **/
50 EFI_STATUS
51 Ip6BuildEfiNeighborCache (
52 IN IP6_PROTOCOL *IpInstance,
53 OUT UINT32 *NeighborCount,
54 OUT EFI_IP6_NEIGHBOR_CACHE **NeighborCache
55 )
56 {
57 IP6_NEIGHBOR_ENTRY *Neighbor;
58 LIST_ENTRY *Entry;
59 IP6_SERVICE *IpSb;
60 UINT32 Count;
61 EFI_IP6_NEIGHBOR_CACHE *EfiNeighborCache;
62 EFI_IP6_NEIGHBOR_CACHE *NeighborCacheTmp;
63
64 NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE);
65 ASSERT (NeighborCount != NULL && NeighborCache != NULL);
66
67 IpSb = IpInstance->Service;
68 Count = 0;
69
70 NET_LIST_FOR_EACH (Entry, &IpSb->NeighborTable) {
71 Count++;
72 }
73
74 if (Count == 0) {
75 return EFI_SUCCESS;
76 }
77
78 NeighborCacheTmp = AllocatePool (Count * sizeof (EFI_IP6_NEIGHBOR_CACHE));
79 if (NeighborCacheTmp == NULL) {
80 return EFI_OUT_OF_RESOURCES;
81 }
82
83 *NeighborCount = Count;
84 Count = 0;
85
86 NET_LIST_FOR_EACH (Entry, &IpSb->NeighborTable) {
87 Neighbor = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link);
88
89 EfiNeighborCache = NeighborCacheTmp + Count;
90
91 EfiNeighborCache->State = Neighbor->State;
92 IP6_COPY_ADDRESS (&EfiNeighborCache->Neighbor, &Neighbor->Neighbor);
93 IP6_COPY_LINK_ADDRESS (&EfiNeighborCache->LinkAddress, &Neighbor->LinkAddress);
94
95 Count++;
96 }
97
98 ASSERT (*NeighborCount == Count);
99 *NeighborCache = NeighborCacheTmp;
100
101 return EFI_SUCCESS;
102 }
103
104 /**
105 Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number
106 of prefix entries is also returned.
107
108 @param[in] IpInstance The pointer to IP6_PROTOCOL instance.
109 @param[out] PrefixCount The number of returned prefix entries.
110 @param[out] PrefixTable The pointer to the array of PrefixTable.
111
112 @retval EFI_SUCCESS The prefix table successfully built.
113 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the prefix table.
114
115 **/
116 EFI_STATUS
117 Ip6BuildPrefixTable (
118 IN IP6_PROTOCOL *IpInstance,
119 OUT UINT32 *PrefixCount,
120 OUT EFI_IP6_ADDRESS_INFO **PrefixTable
121 )
122 {
123 LIST_ENTRY *Entry;
124 IP6_SERVICE *IpSb;
125 UINT32 Count;
126 IP6_PREFIX_LIST_ENTRY *PrefixList;
127 EFI_IP6_ADDRESS_INFO *EfiPrefix;
128 EFI_IP6_ADDRESS_INFO *PrefixTableTmp;
129
130 NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE);
131 ASSERT (PrefixCount != NULL && PrefixTable != NULL);
132
133 IpSb = IpInstance->Service;
134 Count = 0;
135
136 NET_LIST_FOR_EACH (Entry, &IpSb->OnlinkPrefix) {
137 Count++;
138 }
139
140 if (Count == 0) {
141 return EFI_SUCCESS;
142 }
143
144 PrefixTableTmp = AllocatePool (Count * sizeof (EFI_IP6_ADDRESS_INFO));
145 if (PrefixTableTmp == NULL) {
146 return EFI_OUT_OF_RESOURCES;
147 }
148
149 *PrefixCount = Count;
150 Count = 0;
151
152 NET_LIST_FOR_EACH (Entry, &IpSb->OnlinkPrefix) {
153 PrefixList = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
154 EfiPrefix = PrefixTableTmp + Count;
155 IP6_COPY_ADDRESS (&EfiPrefix->Address, &PrefixList->Prefix);
156 EfiPrefix->PrefixLength = PrefixList->PrefixLength;
157
158 Count++;
159 }
160
161 ASSERT (*PrefixCount == Count);
162 *PrefixTable = PrefixTableTmp;
163
164 return EFI_SUCCESS;
165 }
166
167 /**
168 Allocate and initialize a IP6 prefix list entry.
169
170 @param[in] IpSb The pointer to IP6_SERVICE instance.
171 @param[in] OnLinkOrAuto If TRUE, the entry is created for the on link prefix list.
172 Otherwise, it is created for the autoconfiguration prefix list.
173 @param[in] ValidLifetime The length of time in seconds that the prefix
174 is valid for the purpose of on-link determination.
175 @param[in] PreferredLifetime The length of time in seconds that addresses
176 generated from the prefix via stateless address
177 autoconfiguration remain preferred.
178 @param[in] PrefixLength The prefix length of the Prefix.
179 @param[in] Prefix The prefix address.
180
181 @return NULL if it failed to allocate memory for the prefix node. Otherwise, point
182 to the created or existing prefix list entry.
183
184 **/
185 IP6_PREFIX_LIST_ENTRY *
186 Ip6CreatePrefixListEntry (
187 IN IP6_SERVICE *IpSb,
188 IN BOOLEAN OnLinkOrAuto,
189 IN UINT32 ValidLifetime,
190 IN UINT32 PreferredLifetime,
191 IN UINT8 PrefixLength,
192 IN EFI_IPv6_ADDRESS *Prefix
193 )
194 {
195 IP6_PREFIX_LIST_ENTRY *PrefixEntry;
196 IP6_ROUTE_ENTRY *RtEntry;
197 LIST_ENTRY *ListHead;
198 LIST_ENTRY *Entry;
199 IP6_PREFIX_LIST_ENTRY *TmpPrefixEntry;
200
201 if (Prefix == NULL || PreferredLifetime > ValidLifetime || PrefixLength >= IP6_PREFIX_NUM) {
202 return NULL;
203 }
204
205 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
206
207 PrefixEntry = Ip6FindPrefixListEntry (
208 IpSb,
209 OnLinkOrAuto,
210 PrefixLength,
211 Prefix
212 );
213 if (PrefixEntry != NULL) {
214 PrefixEntry->RefCnt ++;
215 return PrefixEntry;
216 }
217
218 PrefixEntry = AllocatePool (sizeof (IP6_PREFIX_LIST_ENTRY));
219 if (PrefixEntry == NULL) {
220 return NULL;
221 }
222
223 PrefixEntry->RefCnt = 1;
224 PrefixEntry->ValidLifetime = ValidLifetime;
225 PrefixEntry->PreferredLifetime = PreferredLifetime;
226 PrefixEntry->PrefixLength = PrefixLength;
227 IP6_COPY_ADDRESS (&PrefixEntry->Prefix, Prefix);
228
229 ListHead = OnLinkOrAuto ? &IpSb->OnlinkPrefix : &IpSb->AutonomousPrefix;
230
231 //
232 // Create a direct route entry for on-link prefix and insert to route area.
233 //
234 if (OnLinkOrAuto) {
235 RtEntry = Ip6CreateRouteEntry (Prefix, PrefixLength, NULL);
236 if (RtEntry == NULL) {
237 FreePool (PrefixEntry);
238 return NULL;
239 }
240
241 RtEntry->Flag = IP6_DIRECT_ROUTE;
242 InsertHeadList (&IpSb->RouteTable->RouteArea[PrefixLength], &RtEntry->Link);
243 IpSb->RouteTable->TotalNum++;
244 }
245
246 //
247 // Insert the prefix entry in the order that a prefix with longer prefix length
248 // is put ahead in the list.
249 //
250 NET_LIST_FOR_EACH (Entry, ListHead) {
251 TmpPrefixEntry = NET_LIST_USER_STRUCT(Entry, IP6_PREFIX_LIST_ENTRY, Link);
252
253 if (TmpPrefixEntry->PrefixLength < PrefixEntry->PrefixLength) {
254 break;
255 }
256 }
257
258 NetListInsertBefore (Entry, &PrefixEntry->Link);
259
260 return PrefixEntry;
261 }
262
263 /**
264 Destory a IP6 prefix list entry.
265
266 @param[in] IpSb The pointer to IP6_SERVICE instance.
267 @param[in] PrefixEntry The to be destroyed prefix list entry.
268 @param[in] OnLinkOrAuto If TRUE, the entry is removed from on link prefix list.
269 Otherwise remove from autoconfiguration prefix list.
270 @param[in] ImmediateDelete If TRUE, remove the entry directly.
271 Otherwise, check the reference count to see whether
272 it should be removed.
273
274 **/
275 VOID
276 Ip6DestroyPrefixListEntry (
277 IN IP6_SERVICE *IpSb,
278 IN IP6_PREFIX_LIST_ENTRY *PrefixEntry,
279 IN BOOLEAN OnLinkOrAuto,
280 IN BOOLEAN ImmediateDelete
281 )
282 {
283 LIST_ENTRY *Entry;
284 IP6_INTERFACE *IpIf;
285 EFI_STATUS Status;
286
287 if ((!ImmediateDelete) && (PrefixEntry->RefCnt > 0) && ((--PrefixEntry->RefCnt) > 0)) {
288 return ;
289 }
290
291 if (OnLinkOrAuto) {
292 //
293 // Remove the direct route for onlink prefix from route table.
294 //
295 do {
296 Status = Ip6DelRoute (
297 IpSb->RouteTable,
298 &PrefixEntry->Prefix,
299 PrefixEntry->PrefixLength,
300 NULL
301 );
302 } while (Status != EFI_NOT_FOUND);
303 } else {
304 //
305 // Remove the corresponding addresses generated from this autonomous prefix.
306 //
307 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
308 IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE);
309
310 Ip6RemoveAddr (IpSb, &IpIf->AddressList, &IpIf->AddressCount, &PrefixEntry->Prefix, PrefixEntry->PrefixLength);
311 }
312 }
313
314 RemoveEntryList (&PrefixEntry->Link);
315 FreePool (PrefixEntry);
316 }
317
318 /**
319 Search the list array to find an IP6 prefix list entry.
320
321 @param[in] IpSb The pointer to IP6_SERVICE instance.
322 @param[in] OnLinkOrAuto If TRUE, the search the link prefix list,
323 Otherwise search the autoconfiguration prefix list.
324 @param[in] PrefixLength The prefix length of the Prefix
325 @param[in] Prefix The prefix address.
326
327 @return NULL if cannot find the IP6 prefix list entry. Otherwise, return the
328 pointer to the IP6 prefix list entry.
329
330 **/
331 IP6_PREFIX_LIST_ENTRY *
332 Ip6FindPrefixListEntry (
333 IN IP6_SERVICE *IpSb,
334 IN BOOLEAN OnLinkOrAuto,
335 IN UINT8 PrefixLength,
336 IN EFI_IPv6_ADDRESS *Prefix
337 )
338 {
339 IP6_PREFIX_LIST_ENTRY *PrefixList;
340 LIST_ENTRY *Entry;
341 LIST_ENTRY *ListHead;
342
343 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
344 ASSERT (Prefix != NULL);
345
346 if (OnLinkOrAuto) {
347 ListHead = &IpSb->OnlinkPrefix;
348 } else {
349 ListHead = &IpSb->AutonomousPrefix;
350 }
351
352 NET_LIST_FOR_EACH (Entry, ListHead) {
353 PrefixList = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
354 if (PrefixLength != 255) {
355 //
356 // Perform exactly prefix match.
357 //
358 if (PrefixList->PrefixLength == PrefixLength &&
359 NetIp6IsNetEqual (&PrefixList->Prefix, Prefix, PrefixLength)) {
360 return PrefixList;
361 }
362 } else {
363 //
364 // Perform the longest prefix match. The list is already sorted with
365 // the longest length prefix put at the head of the list.
366 //
367 if (NetIp6IsNetEqual (&PrefixList->Prefix, Prefix, PrefixList->PrefixLength)) {
368 return PrefixList;
369 }
370 }
371 }
372
373 return NULL;
374 }
375
376 /**
377 Release the resource in the prefix list table, and destroy the list entry and
378 corresponding addresses or route entries.
379
380 @param[in] IpSb The pointer to the IP6_SERVICE instance.
381 @param[in] ListHead The list entry head of the prefix list table.
382
383 **/
384 VOID
385 Ip6CleanPrefixListTable (
386 IN IP6_SERVICE *IpSb,
387 IN LIST_ENTRY *ListHead
388 )
389 {
390 IP6_PREFIX_LIST_ENTRY *PrefixList;
391 BOOLEAN OnLink;
392
393 OnLink = (BOOLEAN) (ListHead == &IpSb->OnlinkPrefix);
394
395 while (!IsListEmpty (ListHead)) {
396 PrefixList = NET_LIST_HEAD (ListHead, IP6_PREFIX_LIST_ENTRY, Link);
397 Ip6DestroyPrefixListEntry (IpSb, PrefixList, OnLink, TRUE);
398 }
399 }
400
401 /**
402 Callback function when address resolution is finished. It will cancel
403 all the queued frames if the address resolution failed, or transmit them
404 if the request succeeded.
405
406 @param[in] Context The context of the callback, a pointer to IP6_NEIGHBOR_ENTRY.
407
408 **/
409 VOID
410 Ip6OnArpResolved (
411 IN VOID *Context
412 )
413 {
414 LIST_ENTRY *Entry;
415 LIST_ENTRY *Next;
416 IP6_NEIGHBOR_ENTRY *ArpQue;
417 IP6_SERVICE *IpSb;
418 IP6_LINK_TX_TOKEN *Token;
419 EFI_STATUS Status;
420 BOOLEAN Sent;
421
422 ArpQue = (IP6_NEIGHBOR_ENTRY *) Context;
423 if ((ArpQue == NULL) || (ArpQue->Interface == NULL)) {
424 return ;
425 }
426
427 IpSb = ArpQue->Interface->Service;
428 if ((IpSb == NULL) || (IpSb->Signature != IP6_SERVICE_SIGNATURE)) {
429 return ;
430 }
431
432 //
433 // ARP resolve failed for some reason. Release all the frame
434 // and ARP queue itself. Ip6FreeArpQue will call the frame's
435 // owner back.
436 //
437 if (NET_MAC_EQUAL (&ArpQue->LinkAddress, &mZeroMacAddress, IpSb->SnpMode.HwAddressSize)) {
438 Ip6FreeNeighborEntry (IpSb, ArpQue, FALSE, TRUE, EFI_NO_MAPPING, NULL, NULL);
439 return ;
440 }
441
442 //
443 // ARP resolve succeeded, Transmit all the frame.
444 //
445 Sent = FALSE;
446 NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) {
447 RemoveEntryList (Entry);
448
449 Token = NET_LIST_USER_STRUCT (Entry, IP6_LINK_TX_TOKEN, Link);
450 IP6_COPY_LINK_ADDRESS (&Token->DstMac, &ArpQue->LinkAddress);
451
452 //
453 // Insert the tx token before transmitting it via MNP as the FrameSentDpc
454 // may be called before Mnp->Transmit returns which will remove this tx
455 // token from the SentFrames list. Remove it from the list if the returned
456 // Status of Mnp->Transmit is not EFI_SUCCESS as in this case the
457 // FrameSentDpc won't be queued.
458 //
459 InsertTailList (&ArpQue->Interface->SentFrames, &Token->Link);
460
461 Status = IpSb->Mnp->Transmit (IpSb->Mnp, &Token->MnpToken);
462 if (EFI_ERROR (Status)) {
463 RemoveEntryList (&Token->Link);
464 Token->CallBack (Token->Packet, Status, 0, Token->Context);
465
466 Ip6FreeLinkTxToken (Token);
467 continue;
468 } else {
469 Sent = TRUE;
470 }
471 }
472
473 //
474 // Free the ArpQue only but not the whole neighbor entry.
475 //
476 Ip6FreeNeighborEntry (IpSb, ArpQue, FALSE, FALSE, EFI_SUCCESS, NULL, NULL);
477
478 if (Sent && (ArpQue->State == EfiNeighborStale)) {
479 ArpQue->State = EfiNeighborDelay;
480 ArpQue->Ticks = (UINT32) IP6_GET_TICKS (IP6_DELAY_FIRST_PROBE_TIME);
481 }
482 }
483
484 /**
485 Allocate and initialize an IP6 neighbor cache entry.
486
487 @param[in] IpSb The pointer to the IP6_SERVICE instance.
488 @param[in] CallBack The callback function to be called when
489 address resolution is finished.
490 @param[in] Ip6Address Points to the IPv6 address of the neighbor.
491 @param[in] LinkAddress Points to the MAC address of the neighbor.
492 Ignored if NULL.
493
494 @return NULL if failed to allocate memory for the neighbor cache entry.
495 Otherwise, point to the created neighbor cache entry.
496
497 **/
498 IP6_NEIGHBOR_ENTRY *
499 Ip6CreateNeighborEntry (
500 IN IP6_SERVICE *IpSb,
501 IN IP6_ARP_CALLBACK CallBack,
502 IN EFI_IPv6_ADDRESS *Ip6Address,
503 IN EFI_MAC_ADDRESS *LinkAddress OPTIONAL
504 )
505 {
506 IP6_NEIGHBOR_ENTRY *Entry;
507 IP6_DEFAULT_ROUTER *DefaultRouter;
508
509 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
510 ASSERT (Ip6Address!= NULL);
511
512 Entry = AllocateZeroPool (sizeof (IP6_NEIGHBOR_ENTRY));
513 if (Entry == NULL) {
514 return NULL;
515 }
516
517 Entry->RefCnt = 1;
518 Entry->IsRouter = FALSE;
519 Entry->ArpFree = FALSE;
520 Entry->Dynamic = FALSE;
521 Entry->State = EfiNeighborInComplete;
522 Entry->Transmit = IP6_MAX_MULTICAST_SOLICIT + 1;
523 Entry->CallBack = CallBack;
524 Entry->Interface = NULL;
525
526 InitializeListHead (&Entry->Frames);
527
528 IP6_COPY_ADDRESS (&Entry->Neighbor, Ip6Address);
529
530 if (LinkAddress != NULL) {
531 IP6_COPY_LINK_ADDRESS (&Entry->LinkAddress, LinkAddress);
532 } else {
533 IP6_COPY_LINK_ADDRESS (&Entry->LinkAddress, &mZeroMacAddress);
534 }
535
536 InsertHeadList (&IpSb->NeighborTable, &Entry->Link);
537
538 //
539 // If corresponding default router entry exists, establish the relationship.
540 //
541 DefaultRouter = Ip6FindDefaultRouter (IpSb, Ip6Address);
542 if (DefaultRouter != NULL) {
543 DefaultRouter->NeighborCache = Entry;
544 }
545
546 return Entry;
547 }
548
549 /**
550 Search a IP6 neighbor cache entry.
551
552 @param[in] IpSb The pointer to the IP6_SERVICE instance.
553 @param[in] Ip6Address Points to the IPv6 address of the neighbor.
554
555 @return NULL if it failed to find the matching neighbor cache entry.
556 Otherwise, point to the found neighbor cache entry.
557
558 **/
559 IP6_NEIGHBOR_ENTRY *
560 Ip6FindNeighborEntry (
561 IN IP6_SERVICE *IpSb,
562 IN EFI_IPv6_ADDRESS *Ip6Address
563 )
564 {
565 LIST_ENTRY *Entry;
566 LIST_ENTRY *Next;
567 IP6_NEIGHBOR_ENTRY *Neighbor;
568
569 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
570 ASSERT (Ip6Address != NULL);
571
572 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->NeighborTable) {
573 Neighbor = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link);
574 if (EFI_IP6_EQUAL (Ip6Address, &Neighbor->Neighbor)) {
575 RemoveEntryList (Entry);
576 InsertHeadList (&IpSb->NeighborTable, Entry);
577
578 return Neighbor;
579 }
580 }
581
582 return NULL;
583 }
584
585 /**
586 Free a IP6 neighbor cache entry and remove all the frames on the address
587 resolution queue that pass the FrameToCancel. That is, either FrameToCancel
588 is NULL, or it returns true for the frame.
589
590 @param[in] IpSb The pointer to the IP6_SERVICE instance.
591 @param[in] NeighborCache The to be free neighbor cache entry.
592 @param[in] SendIcmpError If TRUE, send out ICMP error.
593 @param[in] FullFree If TRUE, remove the neighbor cache entry.
594 Otherwise remove the pending frames.
595 @param[in] IoStatus The status returned to the cancelled frames'
596 callback function.
597 @param[in] FrameToCancel Function to select which frame to cancel.
598 This is an optional parameter that may be NULL.
599 @param[in] Context Opaque parameter to the FrameToCancel.
600 Ignored if FrameToCancel is NULL.
601
602 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
603 @retval EFI_SUCCESS The operation finished successfully.
604
605 **/
606 EFI_STATUS
607 Ip6FreeNeighborEntry (
608 IN IP6_SERVICE *IpSb,
609 IN IP6_NEIGHBOR_ENTRY *NeighborCache,
610 IN BOOLEAN SendIcmpError,
611 IN BOOLEAN FullFree,
612 IN EFI_STATUS IoStatus,
613 IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL,
614 IN VOID *Context OPTIONAL
615 )
616 {
617 IP6_LINK_TX_TOKEN *TxToken;
618 LIST_ENTRY *Entry;
619 LIST_ENTRY *Next;
620 IP6_DEFAULT_ROUTER *DefaultRouter;
621
622 //
623 // If FrameToCancel fails, the token will not be released.
624 // To avoid the memory leak, stop this usage model.
625 //
626 if (FullFree && FrameToCancel != NULL) {
627 return EFI_INVALID_PARAMETER;
628 }
629
630 NET_LIST_FOR_EACH_SAFE (Entry, Next, &NeighborCache->Frames) {
631 TxToken = NET_LIST_USER_STRUCT (Entry, IP6_LINK_TX_TOKEN, Link);
632
633 if (SendIcmpError && !IP6_IS_MULTICAST (&TxToken->Packet->Ip.Ip6->DestinationAddress)) {
634 Ip6SendIcmpError (
635 IpSb,
636 TxToken->Packet,
637 NULL,
638 &TxToken->Packet->Ip.Ip6->SourceAddress,
639 ICMP_V6_DEST_UNREACHABLE,
640 ICMP_V6_ADDR_UNREACHABLE,
641 NULL
642 );
643 }
644
645 if ((FrameToCancel == NULL) || FrameToCancel (TxToken, Context)) {
646 RemoveEntryList (Entry);
647 TxToken->CallBack (TxToken->Packet, IoStatus, 0, TxToken->Context);
648 Ip6FreeLinkTxToken (TxToken);
649 }
650 }
651
652 if (NeighborCache->ArpFree && IsListEmpty (&NeighborCache->Frames)) {
653 RemoveEntryList (&NeighborCache->ArpList);
654 NeighborCache->ArpFree = FALSE;
655 }
656
657 if (FullFree) {
658 if (NeighborCache->IsRouter) {
659 DefaultRouter = Ip6FindDefaultRouter (IpSb, &NeighborCache->Neighbor);
660 if (DefaultRouter != NULL) {
661 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
662 }
663 }
664
665 RemoveEntryList (&NeighborCache->Link);
666 FreePool (NeighborCache);
667 }
668
669 return EFI_SUCCESS;
670 }
671
672 /**
673 Allocate and initialize an IP6 default router entry.
674
675 @param[in] IpSb The pointer to the IP6_SERVICE instance.
676 @param[in] Ip6Address The IPv6 address of the default router.
677 @param[in] RouterLifetime The lifetime associated with the default
678 router, in units of seconds.
679
680 @return NULL if it failed to allocate memory for the default router node.
681 Otherwise, point to the created default router node.
682
683 **/
684 IP6_DEFAULT_ROUTER *
685 Ip6CreateDefaultRouter (
686 IN IP6_SERVICE *IpSb,
687 IN EFI_IPv6_ADDRESS *Ip6Address,
688 IN UINT16 RouterLifetime
689 )
690 {
691 IP6_DEFAULT_ROUTER *Entry;
692 IP6_ROUTE_ENTRY *RtEntry;
693
694 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
695 ASSERT (Ip6Address != NULL);
696
697 Entry = AllocatePool (sizeof (IP6_DEFAULT_ROUTER));
698 if (Entry == NULL) {
699 return NULL;
700 }
701
702 Entry->RefCnt = 1;
703 Entry->Lifetime = RouterLifetime;
704 Entry->NeighborCache = Ip6FindNeighborEntry (IpSb, Ip6Address);
705 IP6_COPY_ADDRESS (&Entry->Router, Ip6Address);
706
707 //
708 // Add a default route into route table with both Destination and PrefixLength set to zero.
709 //
710 RtEntry = Ip6CreateRouteEntry (NULL, 0, Ip6Address);
711 if (RtEntry == NULL) {
712 FreePool (Entry);
713 return NULL;
714 }
715
716 InsertHeadList (&IpSb->RouteTable->RouteArea[0], &RtEntry->Link);
717 IpSb->RouteTable->TotalNum++;
718
719 InsertTailList (&IpSb->DefaultRouterList, &Entry->Link);
720
721 return Entry;
722 }
723
724 /**
725 Destroy an IP6 default router entry.
726
727 @param[in] IpSb The pointer to the IP6_SERVICE instance.
728 @param[in] DefaultRouter The to be destroyed IP6_DEFAULT_ROUTER.
729
730 **/
731 VOID
732 Ip6DestroyDefaultRouter (
733 IN IP6_SERVICE *IpSb,
734 IN IP6_DEFAULT_ROUTER *DefaultRouter
735 )
736 {
737 EFI_STATUS Status;
738
739 RemoveEntryList (&DefaultRouter->Link);
740
741 //
742 // Update the Destination Cache - all entries using the time-out router as next-hop
743 // should perform next-hop determination again.
744 //
745 do {
746 Status = Ip6DelRoute (IpSb->RouteTable, NULL, 0, &DefaultRouter->Router);
747 } while (Status != EFI_NOT_FOUND);
748
749 FreePool (DefaultRouter);
750 }
751
752 /**
753 Clean an IP6 default router list.
754
755 @param[in] IpSb The pointer to the IP6_SERVICE instance.
756 @param[in] DefaultRouter The to be destroyed IP6_DEFAULT_ROUTER.
757
758 **/
759 VOID
760 Ip6CleanDefaultRouterList (
761 IN IP6_SERVICE *IpSb
762 )
763 {
764 IP6_DEFAULT_ROUTER *DefaultRouter;
765
766 while (!IsListEmpty (&IpSb->DefaultRouterList)) {
767 DefaultRouter = NET_LIST_HEAD (&IpSb->DefaultRouterList, IP6_DEFAULT_ROUTER, Link);
768 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
769 }
770 }
771
772 /**
773 Search a default router node from an IP6 default router list.
774
775 @param[in] IpSb The pointer to the IP6_SERVICE instance.
776 @param[in] Ip6Address The IPv6 address of the to be searched default router node.
777
778 @return NULL if it failed to find the matching default router node.
779 Otherwise, point to the found default router node.
780
781 **/
782 IP6_DEFAULT_ROUTER *
783 Ip6FindDefaultRouter (
784 IN IP6_SERVICE *IpSb,
785 IN EFI_IPv6_ADDRESS *Ip6Address
786 )
787 {
788 LIST_ENTRY *Entry;
789 IP6_DEFAULT_ROUTER *DefaultRouter;
790
791 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
792 ASSERT (Ip6Address != NULL);
793
794 NET_LIST_FOR_EACH (Entry, &IpSb->DefaultRouterList) {
795 DefaultRouter = NET_LIST_USER_STRUCT (Entry, IP6_DEFAULT_ROUTER, Link);
796 if (EFI_IP6_EQUAL (Ip6Address, &DefaultRouter->Router)) {
797 return DefaultRouter;
798 }
799 }
800
801 return NULL;
802 }
803
804 /**
805 The function to be called after DAD (Duplicate Address Detection) is performed.
806
807 @param[in] IsDadPassed If TRUE, the DAD operation succeed. Otherwise, the DAD operation failed.
808 @param[in] IpIf Points to the IP6_INTERFACE.
809 @param[in] DadEntry The DAD entry which already performed DAD.
810
811 **/
812 VOID
813 Ip6OnDADFinished (
814 IN BOOLEAN IsDadPassed,
815 IN IP6_INTERFACE *IpIf,
816 IN IP6_DAD_ENTRY *DadEntry
817 )
818 {
819 IP6_SERVICE *IpSb;
820 IP6_ADDRESS_INFO *AddrInfo;
821 EFI_DHCP6_PROTOCOL *Dhcp6;
822 UINT16 OptBuf[4];
823 EFI_DHCP6_PACKET_OPTION *Oro;
824 EFI_DHCP6_RETRANSMISSION InfoReqReXmit;
825
826 IpSb = IpIf->Service;
827 AddrInfo = DadEntry->AddressInfo;
828
829 if (IsDadPassed) {
830 //
831 // DAD succeed.
832 //
833 if (NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
834 ASSERT (!IpSb->LinkLocalOk);
835
836 IP6_COPY_ADDRESS (&IpSb->LinkLocalAddr, &AddrInfo->Address);
837 IpSb->LinkLocalOk = TRUE;
838 IpIf->Configured = TRUE;
839
840 //
841 // Check whether DHCP6 need to be started.
842 //
843 Dhcp6 = IpSb->Ip6ConfigInstance.Dhcp6;
844
845 if (IpSb->Dhcp6NeedStart) {
846 Dhcp6->Start (Dhcp6);
847 IpSb->Dhcp6NeedStart = FALSE;
848 }
849
850 if (IpSb->Dhcp6NeedInfoRequest) {
851 //
852 // Set the exta options to send. Here we only want the option request option
853 // with DNS SERVERS.
854 //
855 Oro = (EFI_DHCP6_PACKET_OPTION *) OptBuf;
856 Oro->OpCode = HTONS (IP6_CONFIG_DHCP6_OPTION_ORO);
857 Oro->OpLen = HTONS (2);
858 *((UINT16 *) &Oro->Data[0]) = HTONS (IP6_CONFIG_DHCP6_OPTION_DNS_SERVERS);
859
860 InfoReqReXmit.Irt = 4;
861 InfoReqReXmit.Mrc = 64;
862 InfoReqReXmit.Mrt = 60;
863 InfoReqReXmit.Mrd = 0;
864
865 Dhcp6->InfoRequest (
866 Dhcp6,
867 TRUE,
868 Oro,
869 0,
870 NULL,
871 &InfoReqReXmit,
872 IpSb->Ip6ConfigInstance.Dhcp6Event,
873 Ip6ConfigOnDhcp6Reply,
874 &IpSb->Ip6ConfigInstance
875 );
876 }
877
878 //
879 // Add an on-link prefix for link-local address.
880 //
881 Ip6CreatePrefixListEntry (
882 IpSb,
883 TRUE,
884 (UINT32) IP6_INFINIT_LIFETIME,
885 (UINT32) IP6_INFINIT_LIFETIME,
886 IP6_LINK_LOCAL_PREFIX_LENGTH,
887 &IpSb->LinkLocalAddr
888 );
889
890 } else {
891 //
892 // Global scope unicast address.
893 //
894 Ip6AddAddr (IpIf, AddrInfo);
895
896 //
897 // Add an on-link prefix for this address.
898 //
899 Ip6CreatePrefixListEntry (
900 IpSb,
901 TRUE,
902 AddrInfo->ValidLifetime,
903 AddrInfo->PreferredLifetime,
904 AddrInfo->PrefixLength,
905 &AddrInfo->Address
906 );
907
908 IpIf->Configured = TRUE;
909 }
910 } else {
911 //
912 // Leave the group we joined before.
913 //
914 Ip6LeaveGroup (IpSb, &DadEntry->Destination);
915 }
916
917 if (DadEntry->Callback != NULL) {
918 DadEntry->Callback (IsDadPassed, &AddrInfo->Address, DadEntry->Context);
919 }
920
921 if (!IsDadPassed && NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
922 FreePool (AddrInfo);
923 RemoveEntryList (&DadEntry->Link);
924 FreePool (DadEntry);
925 //
926 // Disable IP operation since link-local address is a duplicate address.
927 //
928 IpSb->LinkLocalDadFail = TRUE;
929 IpSb->Mnp->Configure (IpSb->Mnp, NULL);
930 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
931 gBS->SetTimer (IpSb->FasterTimer, TimerCancel, 0);
932 return ;
933 }
934
935 if (!IsDadPassed || NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
936 //
937 // Free the AddressInfo we hold if DAD fails or it is a link-local address.
938 //
939 FreePool (AddrInfo);
940 }
941
942 RemoveEntryList (&DadEntry->Link);
943 FreePool (DadEntry);
944 }
945
946 /**
947 Create a DAD (Duplicate Address Detection) entry and queue it to be performed.
948
949 @param[in] IpIf Points to the IP6_INTERFACE.
950 @param[in] AddressInfo The address information which needs DAD performed.
951 @param[in] Callback The callback routine that will be called after DAD
952 is performed. This is an optional parameter that
953 may be NULL.
954 @param[in] Context The opaque parameter for a DAD callback routine.
955 This is an optional parameter that may be NULL.
956
957 @retval EFI_SUCCESS The DAD entry was created and queued.
958 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory to complete the
959 operation.
960
961
962 **/
963 EFI_STATUS
964 Ip6InitDADProcess (
965 IN IP6_INTERFACE *IpIf,
966 IN IP6_ADDRESS_INFO *AddressInfo,
967 IN IP6_DAD_CALLBACK Callback OPTIONAL,
968 IN VOID *Context OPTIONAL
969 )
970 {
971 IP6_DAD_ENTRY *Entry;
972 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS *DadXmits;
973 IP6_SERVICE *IpSb;
974 EFI_STATUS Status;
975 UINT32 MaxDelayTick;
976
977 NET_CHECK_SIGNATURE (IpIf, IP6_INTERFACE_SIGNATURE);
978 ASSERT (AddressInfo != NULL);
979
980 Status = EFI_SUCCESS;
981 IpSb = IpIf->Service;
982 DadXmits = &IpSb->Ip6ConfigInstance.DadXmits;
983
984 //
985 // Allocate the resources and insert info
986 //
987 Entry = AllocatePool (sizeof (IP6_DAD_ENTRY));
988 if (Entry == NULL) {
989 return EFI_OUT_OF_RESOURCES;
990 }
991
992 //
993 // Map the incoming unicast address to solicited-node multicast address
994 //
995 Ip6CreateSNMulticastAddr (&AddressInfo->Address, &Entry->Destination);
996
997 //
998 // Join in the solicited-node multicast address.
999 //
1000 Status = Ip6JoinGroup (IpSb, IpIf, &Entry->Destination);
1001 if (EFI_ERROR (Status)) {
1002 FreePool (Entry);
1003 return Status;
1004 }
1005
1006 Entry->Signature = IP6_DAD_ENTRY_SIGNATURE;
1007 Entry->MaxTransmit = DadXmits->DupAddrDetectTransmits;
1008 Entry->Transmit = 0;
1009 Entry->Receive = 0;
1010 MaxDelayTick = IP6_MAX_RTR_SOLICITATION_DELAY / IP6_TIMER_INTERVAL_IN_MS;
1011 Entry->RetransTick = (MaxDelayTick * ((NET_RANDOM (NetRandomInitSeed ()) % 5) + 1)) / 5;
1012 Entry->AddressInfo = AddressInfo;
1013 Entry->Callback = Callback;
1014 Entry->Context = Context;
1015 InsertTailList (&IpIf->DupAddrDetectList, &Entry->Link);
1016
1017 if (Entry->MaxTransmit == 0) {
1018 //
1019 // DAD is disabled on this interface, immediately mark this DAD successful.
1020 //
1021 Ip6OnDADFinished (TRUE, IpIf, Entry);
1022 }
1023
1024 return EFI_SUCCESS;
1025 }
1026
1027 /**
1028 Search IP6_DAD_ENTRY from the Duplicate Address Detection List.
1029
1030 @param[in] IpSb The pointer to the IP6_SERVICE instance.
1031 @param[in] Target The address information which needs DAD performed .
1032 @param[out] Interface If not NULL, output the IP6 interface that configures
1033 the tentative address.
1034
1035 @return NULL if failed to find the matching DAD entry.
1036 Otherwise, point to the found DAD entry.
1037
1038 **/
1039 IP6_DAD_ENTRY *
1040 Ip6FindDADEntry (
1041 IN IP6_SERVICE *IpSb,
1042 IN EFI_IPv6_ADDRESS *Target,
1043 OUT IP6_INTERFACE **Interface OPTIONAL
1044 )
1045 {
1046 LIST_ENTRY *Entry;
1047 LIST_ENTRY *Entry2;
1048 IP6_INTERFACE *IpIf;
1049 IP6_DAD_ENTRY *DupAddrDetect;
1050 IP6_ADDRESS_INFO *AddrInfo;
1051
1052 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1053 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
1054
1055 NET_LIST_FOR_EACH (Entry2, &IpIf->DupAddrDetectList) {
1056 DupAddrDetect = NET_LIST_USER_STRUCT_S (Entry2, IP6_DAD_ENTRY, Link, IP6_DAD_ENTRY_SIGNATURE);
1057 AddrInfo = DupAddrDetect->AddressInfo;
1058 if (EFI_IP6_EQUAL (&AddrInfo->Address, Target)) {
1059 if (Interface != NULL) {
1060 *Interface = IpIf;
1061 }
1062 return DupAddrDetect;
1063 }
1064 }
1065 }
1066
1067 return NULL;
1068 }
1069
1070 /**
1071 Generate router solicit message and send it out to Destination Address or
1072 All Router Link Local scope multicast address.
1073
1074 @param[in] IpSb The IP service to send the packet.
1075 @param[in] Interface If not NULL, points to the IP6 interface to send
1076 the packet.
1077 @param[in] SourceAddress If not NULL, the source address of the message.
1078 @param[in] DestinationAddress If not NULL, the destination address of the message.
1079 @param[in] SourceLinkAddress If not NULL, the MAC address of the source.
1080 A source link-layer address option will be appended
1081 to the message.
1082
1083 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1084 operation.
1085 @retval EFI_SUCCESS The router solicit message was successfully sent.
1086
1087 **/
1088 EFI_STATUS
1089 Ip6SendRouterSolicit (
1090 IN IP6_SERVICE *IpSb,
1091 IN IP6_INTERFACE *Interface OPTIONAL,
1092 IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL,
1093 IN EFI_IPv6_ADDRESS *DestinationAddress OPTIONAL,
1094 IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL
1095 )
1096 {
1097 NET_BUF *Packet;
1098 EFI_IP6_HEADER Head;
1099 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1100 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1101 UINT16 PayloadLen;
1102 IP6_INTERFACE *IpIf;
1103
1104 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1105
1106 IpIf = Interface;
1107 if (IpIf == NULL && IpSb->DefaultInterface != NULL) {
1108 IpIf = IpSb->DefaultInterface;
1109 }
1110
1111 //
1112 // Generate the packet to be sent
1113 //
1114
1115 PayloadLen = (UINT16) sizeof (IP6_ICMP_INFORMATION_HEAD);
1116 if (SourceLinkAddress != NULL) {
1117 PayloadLen += sizeof (IP6_ETHER_ADDR_OPTION);
1118 }
1119
1120 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1121 if (Packet == NULL) {
1122 return EFI_OUT_OF_RESOURCES;
1123 }
1124
1125 //
1126 // Create the basic IPv6 header.
1127 //
1128 Head.FlowLabelL = 0;
1129 Head.FlowLabelH = 0;
1130 Head.PayloadLength = HTONS (PayloadLen);
1131 Head.NextHeader = IP6_ICMP;
1132 Head.HopLimit = IP6_HOP_LIMIT;
1133
1134 if (SourceAddress != NULL) {
1135 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1136 } else {
1137 ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS));
1138 }
1139
1140
1141 if (DestinationAddress != NULL) {
1142 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1143 } else {
1144 Ip6SetToAllNodeMulticast (TRUE, IP6_LINK_LOCAL_SCOPE, &Head.DestinationAddress);
1145 }
1146
1147 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1148
1149 //
1150 // Fill in the ICMP header, and Source link-layer address if contained.
1151 //
1152
1153 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1154 ASSERT (IcmpHead != NULL);
1155 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1156 IcmpHead->Head.Type = ICMP_V6_ROUTER_SOLICIT;
1157 IcmpHead->Head.Code = 0;
1158
1159 LinkLayerOption = NULL;
1160 if (SourceLinkAddress != NULL) {
1161 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1162 Packet,
1163 sizeof (IP6_ETHER_ADDR_OPTION),
1164 FALSE
1165 );
1166 ASSERT (LinkLayerOption != NULL);
1167 LinkLayerOption->Type = Ip6OptionEtherSource;
1168 LinkLayerOption->Length = (UINT8) sizeof (IP6_ETHER_ADDR_OPTION);
1169 CopyMem (LinkLayerOption->EtherAddr, SourceLinkAddress, 6);
1170 }
1171
1172 //
1173 // Transmit the packet
1174 //
1175 return Ip6Output (IpSb, IpIf, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1176 }
1177
1178 /**
1179 Generate a Neighbor Advertisement message and send it out to Destination Address.
1180
1181 @param[in] IpSb The IP service to send the packet.
1182 @param[in] SourceAddress The source address of the message.
1183 @param[in] DestinationAddress The destination address of the message.
1184 @param[in] TargetIp6Address The target address field in the Neighbor Solicitation
1185 message that prompted this advertisement.
1186 @param[in] TargetLinkAddress The MAC address for the target, i.e. the sender
1187 of the advertisement.
1188 @param[in] IsRouter If TRUE, indicates the sender is a router.
1189 @param[in] Override If TRUE, indicates the advertisement should override
1190 an existing cache entry and update the MAC address.
1191 @param[in] Solicited If TRUE, indicates the advertisement was sent
1192 in response to a Neighbor Solicitation from
1193 the Destination address.
1194
1195 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1196 operation.
1197 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1198
1199 **/
1200 EFI_STATUS
1201 Ip6SendNeighborAdvertise (
1202 IN IP6_SERVICE *IpSb,
1203 IN EFI_IPv6_ADDRESS *SourceAddress,
1204 IN EFI_IPv6_ADDRESS *DestinationAddress,
1205 IN EFI_IPv6_ADDRESS *TargetIp6Address,
1206 IN EFI_MAC_ADDRESS *TargetLinkAddress,
1207 IN BOOLEAN IsRouter,
1208 IN BOOLEAN Override,
1209 IN BOOLEAN Solicited
1210 )
1211 {
1212 NET_BUF *Packet;
1213 EFI_IP6_HEADER Head;
1214 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1215 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1216 EFI_IPv6_ADDRESS *Target;
1217 UINT16 PayloadLen;
1218
1219 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1220
1221 //
1222 // The Neighbor Advertisement message must include a Target link-layer address option
1223 // when responding to multicast solicitation and should include such option when
1224 // responding to unicast solicitation. It also must include such option as unsolicited
1225 // advertisement.
1226 //
1227 ASSERT (DestinationAddress != NULL && TargetIp6Address != NULL && TargetLinkAddress != NULL);
1228
1229 PayloadLen = (UINT16) (sizeof (IP6_ICMP_INFORMATION_HEAD) + sizeof (EFI_IPv6_ADDRESS) + sizeof (IP6_ETHER_ADDR_OPTION));
1230
1231 //
1232 // Generate the packet to be sent
1233 //
1234
1235 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1236 if (Packet == NULL) {
1237 return EFI_OUT_OF_RESOURCES;
1238 }
1239
1240 //
1241 // Create the basic IPv6 header.
1242 //
1243 Head.FlowLabelL = 0;
1244 Head.FlowLabelH = 0;
1245 Head.PayloadLength = HTONS (PayloadLen);
1246 Head.NextHeader = IP6_ICMP;
1247 Head.HopLimit = IP6_HOP_LIMIT;
1248
1249 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1250 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1251
1252 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1253
1254 //
1255 // Fill in the ICMP header, Target address, and Target link-layer address.
1256 // Set the Router flag, Solicited flag and Override flag.
1257 //
1258
1259 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1260 ASSERT (IcmpHead != NULL);
1261 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1262 IcmpHead->Head.Type = ICMP_V6_NEIGHBOR_ADVERTISE;
1263 IcmpHead->Head.Code = 0;
1264
1265 if (IsRouter) {
1266 IcmpHead->Fourth |= IP6_IS_ROUTER_FLAG;
1267 }
1268
1269 if (Solicited) {
1270 IcmpHead->Fourth |= IP6_SOLICITED_FLAG;
1271 }
1272
1273 if (Override) {
1274 IcmpHead->Fourth |= IP6_OVERRIDE_FLAG;
1275 }
1276
1277 Target = (EFI_IPv6_ADDRESS *) NetbufAllocSpace (Packet, sizeof (EFI_IPv6_ADDRESS), FALSE);
1278 ASSERT (Target != NULL);
1279 IP6_COPY_ADDRESS (Target, TargetIp6Address);
1280
1281 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1282 Packet,
1283 sizeof (IP6_ETHER_ADDR_OPTION),
1284 FALSE
1285 );
1286 ASSERT (LinkLayerOption != NULL);
1287 LinkLayerOption->Type = Ip6OptionEtherTarget;
1288 LinkLayerOption->Length = 1;
1289 CopyMem (LinkLayerOption->EtherAddr, TargetLinkAddress, 6);
1290
1291 //
1292 // Transmit the packet
1293 //
1294 return Ip6Output (IpSb, NULL, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1295 }
1296
1297 /**
1298 Generate the Neighbor Solicitation message and send it to the Destination Address.
1299
1300 @param[in] IpSb The IP service to send the packet
1301 @param[in] SourceAddress The source address of the message.
1302 @param[in] DestinationAddress The destination address of the message.
1303 @param[in] TargetIp6Address The IP address of the target of the solicitation.
1304 It must not be a multicast address.
1305 @param[in] SourceLinkAddress The MAC address for the sender. If not NULL,
1306 a source link-layer address option will be appended
1307 to the message.
1308
1309 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1310 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1311 operation.
1312 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1313
1314 **/
1315 EFI_STATUS
1316 Ip6SendNeighborSolicit (
1317 IN IP6_SERVICE *IpSb,
1318 IN EFI_IPv6_ADDRESS *SourceAddress,
1319 IN EFI_IPv6_ADDRESS *DestinationAddress,
1320 IN EFI_IPv6_ADDRESS *TargetIp6Address,
1321 IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL
1322 )
1323 {
1324 NET_BUF *Packet;
1325 EFI_IP6_HEADER Head;
1326 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1327 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1328 EFI_IPv6_ADDRESS *Target;
1329 BOOLEAN IsDAD;
1330 UINT16 PayloadLen;
1331 IP6_NEIGHBOR_ENTRY *Neighbor;
1332
1333 //
1334 // Check input parameters
1335 //
1336 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1337 if (DestinationAddress == NULL || TargetIp6Address == NULL) {
1338 return EFI_INVALID_PARAMETER;
1339 }
1340
1341 IsDAD = FALSE;
1342
1343 if (SourceAddress == NULL || (SourceAddress != NULL && NetIp6IsUnspecifiedAddr (SourceAddress))) {
1344 IsDAD = TRUE;
1345 }
1346
1347 //
1348 // The Neighbor Solicitation message should include a source link-layer address option
1349 // if the solicitation is not sent by performing DAD - Duplicate Address Detection.
1350 // Otherwise must not include it.
1351 //
1352 PayloadLen = (UINT16) (sizeof (IP6_ICMP_INFORMATION_HEAD) + sizeof (EFI_IPv6_ADDRESS));
1353
1354 if (!IsDAD) {
1355 if (SourceLinkAddress == NULL) {
1356 return EFI_INVALID_PARAMETER;
1357 }
1358
1359 PayloadLen = (UINT16) (PayloadLen + sizeof (IP6_ETHER_ADDR_OPTION));
1360 }
1361
1362 //
1363 // Generate the packet to be sent
1364 //
1365
1366 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1367 if (Packet == NULL) {
1368 return EFI_OUT_OF_RESOURCES;
1369 }
1370
1371 //
1372 // Create the basic IPv6 header
1373 //
1374 Head.FlowLabelL = 0;
1375 Head.FlowLabelH = 0;
1376 Head.PayloadLength = HTONS (PayloadLen);
1377 Head.NextHeader = IP6_ICMP;
1378 Head.HopLimit = IP6_HOP_LIMIT;
1379
1380 if (SourceAddress != NULL) {
1381 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1382 } else {
1383 ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS));
1384 }
1385
1386 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1387
1388 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1389
1390 //
1391 // Fill in the ICMP header, Target address, and Source link-layer address.
1392 //
1393 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1394 ASSERT (IcmpHead != NULL);
1395 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1396 IcmpHead->Head.Type = ICMP_V6_NEIGHBOR_SOLICIT;
1397 IcmpHead->Head.Code = 0;
1398
1399 Target = (EFI_IPv6_ADDRESS *) NetbufAllocSpace (Packet, sizeof (EFI_IPv6_ADDRESS), FALSE);
1400 ASSERT (Target != NULL);
1401 IP6_COPY_ADDRESS (Target, TargetIp6Address);
1402
1403 LinkLayerOption = NULL;
1404 if (!IsDAD) {
1405
1406 //
1407 // Fill in the source link-layer address option
1408 //
1409 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1410 Packet,
1411 sizeof (IP6_ETHER_ADDR_OPTION),
1412 FALSE
1413 );
1414 ASSERT (LinkLayerOption != NULL);
1415 LinkLayerOption->Type = Ip6OptionEtherSource;
1416 LinkLayerOption->Length = 1;
1417 CopyMem (LinkLayerOption->EtherAddr, SourceLinkAddress, 6);
1418 }
1419
1420 //
1421 // Create a Neighbor Cache entry in the INCOMPLETE state when performing
1422 // address resolution.
1423 //
1424 if (!IsDAD && Ip6IsSNMulticastAddr (DestinationAddress)) {
1425 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
1426 if (Neighbor == NULL) {
1427 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, TargetIp6Address, NULL);
1428 ASSERT (Neighbor != NULL);
1429 }
1430 }
1431
1432 //
1433 // Transmit the packet
1434 //
1435 return Ip6Output (IpSb, IpSb->DefaultInterface, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1436 }
1437
1438 /**
1439 Process the Neighbor Solicitation message. The message may be sent for Duplicate
1440 Address Detection or Address Resolution.
1441
1442 @param[in] IpSb The IP service that received the packet.
1443 @param[in] Head The IP head of the message.
1444 @param[in] Packet The content of the message with IP head removed.
1445
1446 @retval EFI_SUCCESS The packet processed successfully.
1447 @retval EFI_INVALID_PARAMETER The packet is invalid.
1448 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1449 @retval Others Failed to process the packet.
1450
1451 **/
1452 EFI_STATUS
1453 Ip6ProcessNeighborSolicit (
1454 IN IP6_SERVICE *IpSb,
1455 IN EFI_IP6_HEADER *Head,
1456 IN NET_BUF *Packet
1457 )
1458 {
1459 IP6_ICMP_INFORMATION_HEAD Icmp;
1460 EFI_IPv6_ADDRESS Target;
1461 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1462 BOOLEAN IsDAD;
1463 BOOLEAN IsUnicast;
1464 BOOLEAN IsMaintained;
1465 IP6_DAD_ENTRY *DupAddrDetect;
1466 IP6_INTERFACE *IpIf;
1467 IP6_NEIGHBOR_ENTRY *Neighbor;
1468 BOOLEAN Solicited;
1469 BOOLEAN UpdateCache;
1470 EFI_IPv6_ADDRESS Dest;
1471 UINT16 OptionLen;
1472 UINT8 *Option;
1473 BOOLEAN Provided;
1474 EFI_STATUS Status;
1475 VOID *MacAddress;
1476
1477 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1478 NetbufCopy (Packet, sizeof (Icmp), sizeof (Target), Target.Addr);
1479
1480 //
1481 // Perform Message Validation:
1482 // The IP Hop Limit field has a value of 255, i.e., the packet
1483 // could not possibly have been forwarded by a router.
1484 // ICMP Code is 0.
1485 // Target Address is not a multicast address.
1486 //
1487 Status = EFI_INVALID_PARAMETER;
1488
1489 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || !NetIp6IsValidUnicast (&Target)) {
1490 goto Exit;
1491 }
1492
1493 //
1494 // ICMP length is 24 or more octets.
1495 //
1496 OptionLen = 0;
1497 if (Head->PayloadLength < IP6_ND_LENGTH) {
1498 goto Exit;
1499 } else {
1500 OptionLen = (UINT16) (Head->PayloadLength - IP6_ND_LENGTH);
1501 Option = NetbufGetByte (Packet, IP6_ND_LENGTH, NULL);
1502
1503 //
1504 // All included options should have a length that is greater than zero.
1505 //
1506 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1507 goto Exit;
1508 }
1509 }
1510
1511 IsDAD = NetIp6IsUnspecifiedAddr (&Head->SourceAddress);
1512 IsUnicast = (BOOLEAN) !Ip6IsSNMulticastAddr (&Head->DestinationAddress);
1513 IsMaintained = Ip6IsOneOfSetAddress (IpSb, &Target, &IpIf, NULL);
1514
1515 Provided = FALSE;
1516 if (OptionLen >= sizeof (IP6_ETHER_ADDR_OPTION)) {
1517 NetbufCopy (
1518 Packet,
1519 IP6_ND_LENGTH,
1520 sizeof (IP6_ETHER_ADDR_OPTION),
1521 (UINT8 *) &LinkLayerOption
1522 );
1523 //
1524 // The solicitation for neighbor discovery should include a source link-layer
1525 // address option. If the option is not recognized, silently ignore it.
1526 //
1527 if (LinkLayerOption.Type == Ip6OptionEtherSource) {
1528 if (IsDAD) {
1529 //
1530 // If the IP source address is the unspecified address, the source
1531 // link-layer address option must not be included in the message.
1532 //
1533 goto Exit;
1534 }
1535
1536 Provided = TRUE;
1537 }
1538 }
1539
1540 //
1541 // If the IP source address is the unspecified address, the IP
1542 // destination address is a solicited-node multicast address.
1543 //
1544 if (IsDAD && IsUnicast) {
1545 goto Exit;
1546 }
1547
1548 //
1549 // If the target address is tentative, and the source address is a unicast address,
1550 // the solicitation's sender is performing address resolution on the target;
1551 // the solicitation should be silently ignored.
1552 //
1553 if (!IsDAD && !IsMaintained) {
1554 goto Exit;
1555 }
1556
1557 //
1558 // If received unicast neighbor solicitation but destination is not this node,
1559 // drop the packet.
1560 //
1561 if (IsUnicast && !IsMaintained) {
1562 goto Exit;
1563 }
1564
1565 //
1566 // In DAD, when target address is a tentative address,
1567 // process the received neighbor solicitation message but not send out response.
1568 //
1569 if (IsDAD && !IsMaintained) {
1570 DupAddrDetect = Ip6FindDADEntry (IpSb, &Target, &IpIf);
1571 if (DupAddrDetect != NULL) {
1572 if (DupAddrDetect->Transmit == 0) {
1573 //
1574 // The NS is from another node to performing DAD on the same address since
1575 // we haven't send out any NS yet. Fail DAD for the tentative address.
1576 //
1577 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1578 Status = EFI_ICMP_ERROR;
1579 goto Exit;
1580 }
1581
1582 //
1583 // Check the MAC address of the incoming packet.
1584 //
1585 if (IpSb->RecvRequest.MnpToken.Packet.RxData == NULL) {
1586 goto Exit;
1587 }
1588
1589 MacAddress = IpSb->RecvRequest.MnpToken.Packet.RxData->SourceAddress;
1590 if (MacAddress != NULL) {
1591 if (CompareMem (
1592 MacAddress,
1593 &IpSb->SnpMode.CurrentAddress,
1594 IpSb->SnpMode.HwAddressSize
1595 ) != 0) {
1596 //
1597 // The NS is from another node to performing DAD on the same address.
1598 // Fail DAD for the tentative address.
1599 //
1600 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1601 Status = EFI_ICMP_ERROR;
1602 } else {
1603 //
1604 // The below layer loopback the NS we sent. Record it and wait for more.
1605 //
1606 DupAddrDetect->Receive++;
1607 Status = EFI_SUCCESS;
1608 }
1609 }
1610 }
1611 goto Exit;
1612 }
1613
1614 //
1615 // If the solicitation does not contain a link-layer address, DO NOT create or
1616 // update the neighbor cache entries.
1617 //
1618 if (Provided) {
1619 Neighbor = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
1620 UpdateCache = FALSE;
1621
1622 if (Neighbor == NULL) {
1623 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, &Head->SourceAddress, NULL);
1624 if (Neighbor == NULL) {
1625 Status = EFI_OUT_OF_RESOURCES;
1626 goto Exit;
1627 }
1628 UpdateCache = TRUE;
1629 } else {
1630 if (CompareMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6) != 0) {
1631 UpdateCache = TRUE;
1632 }
1633 }
1634
1635 if (UpdateCache) {
1636 Neighbor->State = EfiNeighborStale;
1637 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1638 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1639 //
1640 // Send queued packets if exist.
1641 //
1642 Neighbor->CallBack ((VOID *) Neighbor);
1643 }
1644 }
1645
1646 //
1647 // Sends a Neighbor Advertisement as response.
1648 // Set the Router flag to zero since the node is a host.
1649 // If the source address of the solicitation is unspeicifed, and target address
1650 // is one of the maintained address, reply a unsolicited multicast advertisement.
1651 //
1652 if (IsDAD && IsMaintained) {
1653 Solicited = FALSE;
1654 Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &Dest);
1655 } else {
1656 Solicited = TRUE;
1657 IP6_COPY_ADDRESS (&Dest, &Head->SourceAddress);
1658 }
1659
1660 Status = Ip6SendNeighborAdvertise (
1661 IpSb,
1662 &Target,
1663 &Dest,
1664 &Target,
1665 &IpSb->SnpMode.CurrentAddress,
1666 FALSE,
1667 TRUE,
1668 Solicited
1669 );
1670 Exit:
1671 NetbufFree (Packet);
1672 return Status;
1673 }
1674
1675 /**
1676 Process the Neighbor Advertisement message.
1677
1678 @param[in] IpSb The IP service that received the packet.
1679 @param[in] Head The IP head of the message.
1680 @param[in] Packet The content of the message with IP head removed.
1681
1682 @retval EFI_SUCCESS The packet processed successfully.
1683 @retval EFI_INVALID_PARAMETER The packet is invalid.
1684 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1685 @retval Others Failed to process the packet.
1686
1687 **/
1688 EFI_STATUS
1689 Ip6ProcessNeighborAdvertise (
1690 IN IP6_SERVICE *IpSb,
1691 IN EFI_IP6_HEADER *Head,
1692 IN NET_BUF *Packet
1693 )
1694 {
1695 IP6_ICMP_INFORMATION_HEAD Icmp;
1696 EFI_IPv6_ADDRESS Target;
1697 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1698 BOOLEAN Provided;
1699 INTN Compare;
1700 IP6_NEIGHBOR_ENTRY *Neighbor;
1701 IP6_DEFAULT_ROUTER *DefaultRouter;
1702 BOOLEAN Solicited;
1703 BOOLEAN IsRouter;
1704 BOOLEAN Override;
1705 IP6_DAD_ENTRY *DupAddrDetect;
1706 IP6_INTERFACE *IpIf;
1707 UINT16 OptionLen;
1708 UINT8 *Option;
1709 EFI_STATUS Status;
1710
1711 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1712 NetbufCopy (Packet, sizeof (Icmp), sizeof (Target), Target.Addr);
1713
1714 //
1715 // Validate the incoming Neighbor Advertisement
1716 //
1717 Status = EFI_INVALID_PARAMETER;
1718 //
1719 // The IP Hop Limit field has a value of 255, i.e., the packet
1720 // could not possibly have been forwarded by a router.
1721 // ICMP Code is 0.
1722 // Target Address is not a multicast address.
1723 //
1724 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || !NetIp6IsValidUnicast (&Target)) {
1725 goto Exit;
1726 }
1727
1728 //
1729 // ICMP length is 24 or more octets.
1730 //
1731 Provided = FALSE;
1732 OptionLen = 0;
1733 if (Head->PayloadLength < IP6_ND_LENGTH) {
1734 goto Exit;
1735 } else {
1736 OptionLen = (UINT16) (Head->PayloadLength - IP6_ND_LENGTH);
1737 Option = NetbufGetByte (Packet, IP6_ND_LENGTH, NULL);
1738
1739 //
1740 // All included options should have a length that is greater than zero.
1741 //
1742 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1743 goto Exit;
1744 }
1745 }
1746
1747 //
1748 // If the IP destination address is a multicast address, Solicited Flag is ZERO.
1749 //
1750 Solicited = FALSE;
1751 if ((Icmp.Fourth & IP6_SOLICITED_FLAG) == IP6_SOLICITED_FLAG) {
1752 Solicited = TRUE;
1753 }
1754 if (IP6_IS_MULTICAST (&Head->DestinationAddress) && Solicited) {
1755 goto Exit;
1756 }
1757
1758 //
1759 // DAD - Check whether the Target is one of our tentative address.
1760 //
1761 DupAddrDetect = Ip6FindDADEntry (IpSb, &Target, &IpIf);
1762 if (DupAddrDetect != NULL) {
1763 //
1764 // DAD fails, some other node is using this address.
1765 //
1766 NetbufFree (Packet);
1767 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1768 return EFI_ICMP_ERROR;
1769 }
1770
1771 //
1772 // Search the Neighbor Cache for the target's entry. If no entry exists,
1773 // the advertisement should be silently discarded.
1774 //
1775 Neighbor = Ip6FindNeighborEntry (IpSb, &Target);
1776 if (Neighbor == NULL) {
1777 goto Exit;
1778 }
1779
1780 //
1781 // Get IsRouter Flag and Override Flag
1782 //
1783 IsRouter = FALSE;
1784 Override = FALSE;
1785 if ((Icmp.Fourth & IP6_IS_ROUTER_FLAG) == IP6_IS_ROUTER_FLAG) {
1786 IsRouter = TRUE;
1787 }
1788 if ((Icmp.Fourth & IP6_OVERRIDE_FLAG) == IP6_OVERRIDE_FLAG) {
1789 Override = TRUE;
1790 }
1791
1792 //
1793 // Check whether link layer option is included.
1794 //
1795 if (OptionLen >= sizeof (IP6_ETHER_ADDR_OPTION)) {
1796 NetbufCopy (
1797 Packet,
1798 IP6_ND_LENGTH,
1799 sizeof (IP6_ETHER_ADDR_OPTION),
1800 (UINT8 *) &LinkLayerOption
1801 );
1802
1803 if (LinkLayerOption.Type == Ip6OptionEtherTarget) {
1804 Provided = TRUE;
1805 }
1806 }
1807
1808 Compare = 0;
1809 if (Provided) {
1810 Compare = CompareMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1811 }
1812
1813 if (!Neighbor->IsRouter && IsRouter) {
1814 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Target);
1815 if (DefaultRouter != NULL) {
1816 DefaultRouter->NeighborCache = Neighbor;
1817 }
1818 }
1819
1820 if (Neighbor->State == EfiNeighborInComplete) {
1821 //
1822 // If the target's Neighbor Cache entry is in INCOMPLETE state and no
1823 // Target Link-Layer address option is included while link layer has
1824 // address, the message should be silently discarded.
1825 //
1826 if (!Provided) {
1827 goto Exit;
1828 }
1829 //
1830 // Update the Neighbor Cache
1831 //
1832 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1833 if (Solicited) {
1834 Neighbor->State = EfiNeighborReachable;
1835 Neighbor->Ticks = IP6_GET_TICKS (IpSb->ReachableTime);
1836 } else {
1837 Neighbor->State = EfiNeighborStale;
1838 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1839 //
1840 // Send any packets queued for the neighbor awaiting address resolution.
1841 //
1842 Neighbor->CallBack ((VOID *) Neighbor);
1843 }
1844
1845 Neighbor->IsRouter = IsRouter;
1846
1847 } else {
1848 if (!Override && Compare != 0) {
1849 //
1850 // When the Override Flag is clear and supplied link-layer address differs from
1851 // that in the cache, if the state of the entry is not REACHABLE, ignore the
1852 // message. Otherwise set it to STALE but do not update the entry in any
1853 // other way.
1854 //
1855 if (Neighbor->State == EfiNeighborReachable) {
1856 Neighbor->State = EfiNeighborStale;
1857 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1858 }
1859 } else {
1860 if (Compare != 0) {
1861 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1862 }
1863 //
1864 // Update the entry's state
1865 //
1866 if (Solicited) {
1867 Neighbor->State = EfiNeighborReachable;
1868 Neighbor->Ticks = IP6_GET_TICKS (IpSb->ReachableTime);
1869 } else {
1870 if (Compare != 0) {
1871 Neighbor->State = EfiNeighborStale;
1872 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1873 }
1874 }
1875
1876 //
1877 // When IsRouter is changed from TRUE to FALSE, remove the router from the
1878 // Default Router List and remove the Destination Cache entries for all destinations
1879 // using the neighbor as a router.
1880 //
1881 if (Neighbor->IsRouter && !IsRouter) {
1882 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Target);
1883 if (DefaultRouter != NULL) {
1884 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
1885 }
1886 }
1887
1888 Neighbor->IsRouter = IsRouter;
1889 }
1890 }
1891
1892 if (Neighbor->State == EfiNeighborReachable) {
1893 Neighbor->CallBack ((VOID *) Neighbor);
1894 }
1895
1896 Status = EFI_SUCCESS;
1897
1898 Exit:
1899 NetbufFree (Packet);
1900 return Status;
1901 }
1902
1903 /**
1904 Process the Router Advertisement message according to RFC4861.
1905
1906 @param[in] IpSb The IP service that received the packet.
1907 @param[in] Head The IP head of the message.
1908 @param[in] Packet The content of the message with the IP head removed.
1909
1910 @retval EFI_SUCCESS The packet processed successfully.
1911 @retval EFI_INVALID_PARAMETER The packet is invalid.
1912 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1913 operation.
1914 @retval Others Failed to process the packet.
1915
1916 **/
1917 EFI_STATUS
1918 Ip6ProcessRouterAdvertise (
1919 IN IP6_SERVICE *IpSb,
1920 IN EFI_IP6_HEADER *Head,
1921 IN NET_BUF *Packet
1922 )
1923 {
1924 IP6_ICMP_INFORMATION_HEAD Icmp;
1925 UINT32 ReachableTime;
1926 UINT32 RetransTimer;
1927 UINT16 RouterLifetime;
1928 UINT16 Offset;
1929 UINT8 Type;
1930 UINT8 Length;
1931 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1932 UINT32 Fourth;
1933 UINT8 CurHopLimit;
1934 BOOLEAN Mflag;
1935 BOOLEAN Oflag;
1936 IP6_DEFAULT_ROUTER *DefaultRouter;
1937 IP6_NEIGHBOR_ENTRY *NeighborCache;
1938 EFI_MAC_ADDRESS LinkLayerAddress;
1939 IP6_MTU_OPTION MTUOption;
1940 IP6_PREFIX_INFO_OPTION PrefixOption;
1941 IP6_PREFIX_LIST_ENTRY *PrefixList;
1942 BOOLEAN OnLink;
1943 BOOLEAN Autonomous;
1944 EFI_IPv6_ADDRESS StatelessAddress;
1945 EFI_STATUS Status;
1946 UINT16 OptionLen;
1947 UINT8 *Option;
1948 INTN Result;
1949
1950 Status = EFI_INVALID_PARAMETER;
1951
1952 if (IpSb->Ip6ConfigInstance.Policy != Ip6ConfigPolicyAutomatic) {
1953 //
1954 // Skip the process below as it's not required under the current policy.
1955 //
1956 goto Exit;
1957 }
1958
1959 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1960
1961 //
1962 // Validate the incoming Router Advertisement
1963 //
1964
1965 //
1966 // The IP source address must be a link-local address
1967 //
1968 if (!NetIp6IsLinkLocalAddr (&Head->SourceAddress)) {
1969 goto Exit;
1970 }
1971 //
1972 // The IP Hop Limit field has a value of 255, i.e. the packet
1973 // could not possibly have been forwarded by a router.
1974 // ICMP Code is 0.
1975 // ICMP length (derived from the IP length) is 16 or more octets.
1976 //
1977 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 ||
1978 Head->PayloadLength < IP6_RA_LENGTH) {
1979 goto Exit;
1980 }
1981
1982 //
1983 // All included options have a length that is greater than zero.
1984 //
1985 OptionLen = (UINT16) (Head->PayloadLength - IP6_RA_LENGTH);
1986 Option = NetbufGetByte (Packet, IP6_RA_LENGTH, NULL);
1987
1988 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1989 goto Exit;
1990 }
1991
1992 //
1993 // Process Fourth field.
1994 // In Router Advertisement, Fourth is composed of CurHopLimit (8bit), M flag, O flag,
1995 // and Router Lifetime (16 bit).
1996 //
1997
1998 Fourth = NTOHL (Icmp.Fourth);
1999 CopyMem (&RouterLifetime, &Fourth, sizeof (UINT16));
2000
2001 //
2002 // If the source address already in the default router list, update it.
2003 // Otherwise create a new entry.
2004 // A Lifetime of zero indicates that the router is not a default router.
2005 //
2006 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Head->SourceAddress);
2007 if (DefaultRouter == NULL) {
2008 if (RouterLifetime != 0) {
2009 DefaultRouter = Ip6CreateDefaultRouter (IpSb, &Head->SourceAddress, RouterLifetime);
2010 if (DefaultRouter == NULL) {
2011 Status = EFI_OUT_OF_RESOURCES;
2012 goto Exit;
2013 }
2014 }
2015 } else {
2016 if (RouterLifetime != 0) {
2017 DefaultRouter->Lifetime = RouterLifetime;
2018 //
2019 // Check the corresponding neighbor cache entry here.
2020 //
2021 if (DefaultRouter->NeighborCache == NULL) {
2022 DefaultRouter->NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
2023 }
2024 } else {
2025 //
2026 // If the address is in the host's default router list and the router lifetime is zero,
2027 // immediately time-out the entry.
2028 //
2029 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
2030 }
2031 }
2032
2033 CurHopLimit = *((UINT8 *) &Fourth + 3);
2034 if (CurHopLimit != 0) {
2035 IpSb->CurHopLimit = CurHopLimit;
2036 }
2037
2038 Mflag = FALSE;
2039 Oflag = FALSE;
2040 if ((*((UINT8 *) &Fourth + 2) & IP6_M_ADDR_CONFIG_FLAG) == IP6_M_ADDR_CONFIG_FLAG) {
2041 Mflag = TRUE;
2042 } else {
2043 if ((*((UINT8 *) &Fourth + 2) & IP6_O_CONFIG_FLAG) == IP6_O_CONFIG_FLAG) {
2044 Oflag = TRUE;
2045 }
2046 }
2047
2048 if (Mflag || Oflag) {
2049 //
2050 // Use Ip6Config to get available addresses or other configuration from DHCP.
2051 //
2052 Ip6ConfigStartStatefulAutoConfig (&IpSb->Ip6ConfigInstance, Oflag);
2053 }
2054
2055 //
2056 // Process Reachable Time and Retrans Timer fields.
2057 //
2058 NetbufCopy (Packet, sizeof (Icmp), sizeof (UINT32), (UINT8 *) &ReachableTime);
2059 NetbufCopy (Packet, sizeof (Icmp) + sizeof (UINT32), sizeof (UINT32), (UINT8 *) &RetransTimer);
2060 ReachableTime = NTOHL (ReachableTime);
2061 RetransTimer = NTOHL (RetransTimer);
2062
2063 if (ReachableTime != 0 && ReachableTime != IpSb->BaseReachableTime) {
2064 //
2065 // If new value is not unspecified and differs from the previous one, record it
2066 // in BaseReachableTime and recompute a ReachableTime.
2067 //
2068 IpSb->BaseReachableTime = ReachableTime;
2069 Ip6UpdateReachableTime (IpSb);
2070 }
2071
2072 if (RetransTimer != 0) {
2073 IpSb->RetransTimer = RetransTimer;
2074 }
2075
2076 //
2077 // IsRouter flag must be set to TRUE if corresponding neighbor cache entry exists.
2078 //
2079 NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
2080 if (NeighborCache != NULL) {
2081 NeighborCache->IsRouter = TRUE;
2082 }
2083
2084 //
2085 // If an valid router advertisment is received, stops router solicitation.
2086 //
2087 IpSb->RouterAdvertiseReceived = TRUE;
2088
2089 //
2090 // The only defined options that may appear are the Source
2091 // Link-Layer Address, Prefix information and MTU options.
2092 // All included options have a length that is greater than zero.
2093 //
2094 Offset = 16;
2095 while (Offset < Head->PayloadLength) {
2096 NetbufCopy (Packet, Offset, sizeof (UINT8), &Type);
2097 switch (Type) {
2098 case Ip6OptionEtherSource:
2099 //
2100 // Update the neighbor cache
2101 //
2102 NetbufCopy (Packet, Offset, sizeof (IP6_ETHER_ADDR_OPTION), (UINT8 *) &LinkLayerOption);
2103 if (LinkLayerOption.Length <= 0) {
2104 goto Exit;
2105 }
2106
2107 ZeroMem (&LinkLayerAddress, sizeof (EFI_MAC_ADDRESS));
2108 CopyMem (&LinkLayerAddress, LinkLayerOption.EtherAddr, 6);
2109
2110 if (NeighborCache == NULL) {
2111 NeighborCache = Ip6CreateNeighborEntry (
2112 IpSb,
2113 Ip6OnArpResolved,
2114 &Head->SourceAddress,
2115 &LinkLayerAddress
2116 );
2117 if (NeighborCache == NULL) {
2118 Status = EFI_OUT_OF_RESOURCES;
2119 goto Exit;
2120 }
2121 NeighborCache->IsRouter = TRUE;
2122 NeighborCache->State = EfiNeighborStale;
2123 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2124 } else {
2125 Result = CompareMem (&LinkLayerAddress, &NeighborCache->LinkAddress, 6);
2126
2127 //
2128 // If the link-local address is the same as that already in the cache,
2129 // the cache entry's state remains unchanged. Otherwise update the
2130 // reachability state to STALE.
2131 //
2132 if ((NeighborCache->State == EfiNeighborInComplete) || (Result != 0)) {
2133 CopyMem (&NeighborCache->LinkAddress, &LinkLayerAddress, 6);
2134
2135 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2136
2137 if (NeighborCache->State == EfiNeighborInComplete) {
2138 //
2139 // Send queued packets if exist.
2140 //
2141 NeighborCache->State = EfiNeighborStale;
2142 NeighborCache->CallBack ((VOID *) NeighborCache);
2143 } else {
2144 NeighborCache->State = EfiNeighborStale;
2145 }
2146 }
2147 }
2148
2149 Offset = (UINT16) (Offset + (UINT16) LinkLayerOption.Length * 8);
2150 break;
2151 case Ip6OptionPrefixInfo:
2152 NetbufCopy (Packet, Offset, sizeof (IP6_PREFIX_INFO_OPTION), (UINT8 *) &PrefixOption);
2153 if (PrefixOption.Length != 4) {
2154 goto Exit;
2155 }
2156 PrefixOption.ValidLifetime = NTOHL (PrefixOption.ValidLifetime);
2157 PrefixOption.PreferredLifetime = NTOHL (PrefixOption.PreferredLifetime);
2158
2159 //
2160 // Get L and A flag, recorded in the lower 2 bits of Reserved1
2161 //
2162 OnLink = FALSE;
2163 if ((PrefixOption.Reserved1 & IP6_ON_LINK_FLAG) == IP6_ON_LINK_FLAG) {
2164 OnLink = TRUE;
2165 }
2166 Autonomous = FALSE;
2167 if ((PrefixOption.Reserved1 & IP6_AUTO_CONFIG_FLAG) == IP6_AUTO_CONFIG_FLAG) {
2168 Autonomous = TRUE;
2169 }
2170
2171 //
2172 // If the prefix is the link-local prefix, silently ignore the prefix option.
2173 //
2174 if (PrefixOption.PrefixLength == IP6_LINK_LOCAL_PREFIX_LENGTH &&
2175 NetIp6IsLinkLocalAddr (&PrefixOption.Prefix)
2176 ) {
2177 Offset += sizeof (IP6_PREFIX_INFO_OPTION);
2178 break;
2179 }
2180 //
2181 // Do following if on-link flag is set according to RFC4861.
2182 //
2183 if (OnLink) {
2184 PrefixList = Ip6FindPrefixListEntry (
2185 IpSb,
2186 TRUE,
2187 PrefixOption.PrefixLength,
2188 &PrefixOption.Prefix
2189 );
2190 //
2191 // Create a new entry for the prefix, if the ValidLifetime is zero,
2192 // silently ignore the prefix option.
2193 //
2194 if (PrefixList == NULL && PrefixOption.ValidLifetime != 0) {
2195 PrefixList = Ip6CreatePrefixListEntry (
2196 IpSb,
2197 TRUE,
2198 PrefixOption.ValidLifetime,
2199 PrefixOption.PreferredLifetime,
2200 PrefixOption.PrefixLength,
2201 &PrefixOption.Prefix
2202 );
2203 if (PrefixList == NULL) {
2204 Status = EFI_OUT_OF_RESOURCES;
2205 goto Exit;
2206 }
2207 } else if (PrefixList != NULL) {
2208 if (PrefixOption.ValidLifetime != 0) {
2209 PrefixList->ValidLifetime = PrefixOption.ValidLifetime;
2210 } else {
2211 //
2212 // If the prefix exists and incoming ValidLifetime is zero, immediately
2213 // remove the prefix.
2214 Ip6DestroyPrefixListEntry (IpSb, PrefixList, OnLink, TRUE);
2215 }
2216 }
2217 }
2218
2219 //
2220 // Do following if Autonomous flag is set according to RFC4862.
2221 //
2222 if (Autonomous && PrefixOption.PreferredLifetime <= PrefixOption.ValidLifetime) {
2223 PrefixList = Ip6FindPrefixListEntry (
2224 IpSb,
2225 FALSE,
2226 PrefixOption.PrefixLength,
2227 &PrefixOption.Prefix
2228 );
2229 //
2230 // Create a new entry for the prefix, and form an address by prefix + interface id
2231 // If the sum of the prefix length and interface identifier length
2232 // does not equal 128 bits, the Prefix Information option MUST be ignored.
2233 //
2234 if (PrefixList == NULL &&
2235 PrefixOption.ValidLifetime != 0 &&
2236 PrefixOption.PrefixLength + IpSb->InterfaceIdLen * 8 == 128
2237 ) {
2238 //
2239 // Form the address in network order.
2240 //
2241 CopyMem (&StatelessAddress, &PrefixOption.Prefix, sizeof (UINT64));
2242 CopyMem (&StatelessAddress.Addr[8], IpSb->InterfaceId, sizeof (UINT64));
2243
2244 //
2245 // If the address is not yet in the assigned address list, adds it into.
2246 //
2247 if (!Ip6IsOneOfSetAddress (IpSb, &StatelessAddress, NULL, NULL)) {
2248 //
2249 // And also not in the DAD process, check its uniqeness firstly.
2250 //
2251 if (Ip6FindDADEntry (IpSb, &StatelessAddress, NULL) == NULL) {
2252 Status = Ip6SetAddress (
2253 IpSb->DefaultInterface,
2254 &StatelessAddress,
2255 FALSE,
2256 PrefixOption.PrefixLength,
2257 PrefixOption.ValidLifetime,
2258 PrefixOption.PreferredLifetime,
2259 NULL,
2260 NULL
2261 );
2262 if (EFI_ERROR (Status)) {
2263 goto Exit;
2264 }
2265 }
2266 }
2267
2268 //
2269 // Adds the prefix option to stateless prefix option list.
2270 //
2271 PrefixList = Ip6CreatePrefixListEntry (
2272 IpSb,
2273 FALSE,
2274 PrefixOption.ValidLifetime,
2275 PrefixOption.PreferredLifetime,
2276 PrefixOption.PrefixLength,
2277 &PrefixOption.Prefix
2278 );
2279 if (PrefixList == NULL) {
2280 Status = EFI_OUT_OF_RESOURCES;
2281 goto Exit;
2282 }
2283 } else if (PrefixList != NULL) {
2284
2285 //
2286 // Reset the preferred lifetime of the address if the advertised prefix exists.
2287 // Perform specific action to valid lifetime together.
2288 //
2289 PrefixList->PreferredLifetime = PrefixOption.PreferredLifetime;
2290 if ((PrefixOption.ValidLifetime > 7200) ||
2291 (PrefixOption.ValidLifetime > PrefixList->ValidLifetime)) {
2292 //
2293 // If the received Valid Lifetime is greater than 2 hours or
2294 // greater than RemainingLifetime, set the valid lifetime of the
2295 // corresponding address to the advertised Valid Lifetime.
2296 //
2297 PrefixList->ValidLifetime = PrefixOption.ValidLifetime;
2298
2299 } else if (PrefixList->ValidLifetime <= 7200) {
2300 //
2301 // If RemainingLifetime is less than or equls to 2 hours, ignore the
2302 // Prefix Information option with regards to the valid lifetime.
2303 // TODO: If this option has been authenticated, set the valid lifetime.
2304 //
2305 } else {
2306 //
2307 // Otherwise, reset the valid lifetime of the corresponding
2308 // address to 2 hours.
2309 //
2310 PrefixList->ValidLifetime = 7200;
2311 }
2312 }
2313 }
2314
2315 Offset += sizeof (IP6_PREFIX_INFO_OPTION);
2316 break;
2317 case Ip6OptionMtu:
2318 NetbufCopy (Packet, Offset, sizeof (IP6_MTU_OPTION), (UINT8 *) &MTUOption);
2319 if (MTUOption.Length != 1) {
2320 goto Exit;
2321 }
2322
2323 //
2324 // Use IPv6 minimum link MTU 1280 bytes as the maximum packet size in order
2325 // to omit implementation of Path MTU Discovery. Thus ignore the MTU option
2326 // in Router Advertisement.
2327 //
2328
2329 Offset += sizeof (IP6_MTU_OPTION);
2330 break;
2331 default:
2332 //
2333 // Silently ignore unrecognized options
2334 //
2335 NetbufCopy (Packet, Offset + sizeof (UINT8), sizeof (UINT8), &Length);
2336 if (Length <= 0) {
2337 goto Exit;
2338 }
2339
2340 Offset = (UINT16) (Offset + (UINT16) Length * 8);
2341 break;
2342 }
2343 }
2344
2345 Status = EFI_SUCCESS;
2346
2347 Exit:
2348 NetbufFree (Packet);
2349 return Status;
2350 }
2351
2352 /**
2353 Process the ICMPv6 redirect message. Find the instance, then update
2354 its route cache.
2355
2356 @param[in] IpSb The IP6 service binding instance that received
2357 the packet.
2358 @param[in] Head The IP head of the received ICMPv6 packet.
2359 @param[in] Packet The content of the ICMPv6 redirect packet with
2360 the IP head removed.
2361
2362 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2363 @retval EFI_OUT_OF_RESOURCES Insuffcient resources to complete the
2364 operation.
2365 @retval EFI_SUCCESS Successfully updated the route caches.
2366
2367 **/
2368 EFI_STATUS
2369 Ip6ProcessRedirect (
2370 IN IP6_SERVICE *IpSb,
2371 IN EFI_IP6_HEADER *Head,
2372 IN NET_BUF *Packet
2373 )
2374 {
2375 IP6_ICMP_INFORMATION_HEAD *Icmp;
2376 EFI_IPv6_ADDRESS *Target;
2377 EFI_IPv6_ADDRESS *IcmpDest;
2378 UINT8 *Option;
2379 UINT16 OptionLen;
2380 IP6_ROUTE_ENTRY *RouteEntry;
2381 IP6_ROUTE_CACHE_ENTRY *RouteCache;
2382 IP6_NEIGHBOR_ENTRY *NeighborCache;
2383 INT32 Length;
2384 UINT8 OptLen;
2385 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
2386 EFI_MAC_ADDRESS Mac;
2387 UINT32 Index;
2388 BOOLEAN IsRouter;
2389 EFI_STATUS Status;
2390 INTN Result;
2391
2392 Status = EFI_INVALID_PARAMETER;
2393
2394 Icmp = (IP6_ICMP_INFORMATION_HEAD *) NetbufGetByte (Packet, 0, NULL);
2395 if (Icmp == NULL) {
2396 goto Exit;
2397 }
2398
2399 //
2400 // Validate the incoming Redirect message
2401 //
2402
2403 //
2404 // The IP Hop Limit field has a value of 255, i.e. the packet
2405 // could not possibly have been forwarded by a router.
2406 // ICMP Code is 0.
2407 // ICMP length (derived from the IP length) is 40 or more octets.
2408 //
2409 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp->Head.Code != 0 ||
2410 Head->PayloadLength < IP6_REDITECT_LENGTH) {
2411 goto Exit;
2412 }
2413
2414 //
2415 // The IP source address must be a link-local address
2416 //
2417 if (!NetIp6IsLinkLocalAddr (&Head->SourceAddress)) {
2418 goto Exit;
2419 }
2420
2421 //
2422 // The dest of this ICMP redirect message is not us.
2423 //
2424 if (!Ip6IsOneOfSetAddress (IpSb, &Head->DestinationAddress, NULL, NULL)) {
2425 goto Exit;
2426 }
2427
2428 //
2429 // All included options have a length that is greater than zero.
2430 //
2431 OptionLen = (UINT16) (Head->PayloadLength - IP6_REDITECT_LENGTH);
2432 Option = NetbufGetByte (Packet, IP6_REDITECT_LENGTH, NULL);
2433
2434 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
2435 goto Exit;
2436 }
2437
2438 Target = (EFI_IPv6_ADDRESS *) (Icmp + 1);
2439 IcmpDest = Target + 1;
2440
2441 //
2442 // The ICMP Destination Address field in the redirect message does not contain
2443 // a multicast address.
2444 //
2445 if (IP6_IS_MULTICAST (IcmpDest)) {
2446 goto Exit;
2447 }
2448
2449 //
2450 // The ICMP Target Address is either a link-local address (when redirected to
2451 // a router) or the same as the ICMP Destination Address (when redirected to
2452 // the on-link destination).
2453 //
2454 IsRouter = (BOOLEAN) !EFI_IP6_EQUAL (Target, IcmpDest);
2455 if (!NetIp6IsLinkLocalAddr (Target) && IsRouter) {
2456 goto Exit;
2457 }
2458
2459 //
2460 // Check the options. The only interested option here is the target-link layer
2461 // address option.
2462 //
2463 Length = Packet->TotalSize - 40;
2464 Option = (UINT8 *) (IcmpDest + 1);
2465 LinkLayerOption = NULL;
2466 while (Length > 0) {
2467 switch (*Option) {
2468 case Ip6OptionEtherTarget:
2469
2470 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) Option;
2471 OptLen = LinkLayerOption->Length;
2472 if (OptLen != 1) {
2473 //
2474 // For ethernet, the length must be 1.
2475 //
2476 goto Exit;
2477 }
2478 break;
2479
2480 default:
2481
2482 OptLen = *(Option + 1);
2483 if (OptLen == 0) {
2484 //
2485 // A length of 0 is invalid.
2486 //
2487 goto Exit;
2488 }
2489 break;
2490 }
2491
2492 Length -= 8 * OptLen;
2493 Option += 8 * OptLen;
2494 }
2495
2496 if (Length != 0) {
2497 goto Exit;
2498 }
2499
2500 //
2501 // The IP source address of the Redirect is the same as the current
2502 // first-hop router for the specified ICMP Destination Address.
2503 //
2504 RouteCache = Ip6FindRouteCache (IpSb->RouteTable, IcmpDest, &Head->DestinationAddress);
2505 if (RouteCache != NULL) {
2506 if (!EFI_IP6_EQUAL (&RouteCache->NextHop, &Head->SourceAddress)) {
2507 //
2508 // The source of this Redirect message must match the NextHop of the
2509 // corresponding route cache entry.
2510 //
2511 goto Exit;
2512 }
2513
2514 //
2515 // Update the NextHop.
2516 //
2517 IP6_COPY_ADDRESS (&RouteCache->NextHop, Target);
2518
2519 if (!IsRouter) {
2520 RouteEntry = (IP6_ROUTE_ENTRY *) RouteCache->Tag;
2521 RouteEntry->Flag = RouteEntry->Flag | IP6_DIRECT_ROUTE;
2522 }
2523
2524 } else {
2525 //
2526 // Get the Route Entry.
2527 //
2528 RouteEntry = Ip6FindRouteEntry (IpSb->RouteTable, IcmpDest, NULL);
2529 if (RouteEntry == NULL) {
2530 RouteEntry = Ip6CreateRouteEntry (IcmpDest, 0, NULL);
2531 if (RouteEntry == NULL) {
2532 Status = EFI_OUT_OF_RESOURCES;
2533 goto Exit;
2534 }
2535 }
2536
2537 if (!IsRouter) {
2538 RouteEntry->Flag = IP6_DIRECT_ROUTE;
2539 }
2540
2541 //
2542 // Create a route cache for this.
2543 //
2544 RouteCache = Ip6CreateRouteCacheEntry (
2545 IcmpDest,
2546 &Head->DestinationAddress,
2547 Target,
2548 (UINTN) RouteEntry
2549 );
2550 if (RouteCache == NULL) {
2551 Status = EFI_OUT_OF_RESOURCES;
2552 goto Exit;
2553 }
2554
2555 //
2556 // Insert the newly created route cache entry.
2557 //
2558 Index = IP6_ROUTE_CACHE_HASH (IcmpDest, &Head->DestinationAddress);
2559 InsertHeadList (&IpSb->RouteTable->Cache.CacheBucket[Index], &RouteCache->Link);
2560 }
2561
2562 //
2563 // Try to locate the neighbor cache for the Target.
2564 //
2565 NeighborCache = Ip6FindNeighborEntry (IpSb, Target);
2566
2567 if (LinkLayerOption != NULL) {
2568 if (NeighborCache == NULL) {
2569 //
2570 // Create a neighbor cache for the Target.
2571 //
2572 ZeroMem (&Mac, sizeof (EFI_MAC_ADDRESS));
2573 CopyMem (&Mac, LinkLayerOption->EtherAddr, 6);
2574 NeighborCache = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, Target, &Mac);
2575 if (NeighborCache == NULL) {
2576 //
2577 // Just report a success here. The neighbor cache can be created in
2578 // some other place.
2579 //
2580 Status = EFI_SUCCESS;
2581 goto Exit;
2582 }
2583
2584 NeighborCache->State = EfiNeighborStale;
2585 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2586 } else {
2587 Result = CompareMem (LinkLayerOption->EtherAddr, &NeighborCache->LinkAddress, 6);
2588
2589 //
2590 // If the link-local address is the same as that already in the cache,
2591 // the cache entry's state remains unchanged. Otherwise update the
2592 // reachability state to STALE.
2593 //
2594 if ((NeighborCache->State == EfiNeighborInComplete) || (Result != 0)) {
2595 CopyMem (&NeighborCache->LinkAddress, LinkLayerOption->EtherAddr, 6);
2596
2597 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2598
2599 if (NeighborCache->State == EfiNeighborInComplete) {
2600 //
2601 // Send queued packets if exist.
2602 //
2603 NeighborCache->State = EfiNeighborStale;
2604 NeighborCache->CallBack ((VOID *) NeighborCache);
2605 } else {
2606 NeighborCache->State = EfiNeighborStale;
2607 }
2608 }
2609 }
2610 }
2611
2612 if (NeighborCache != NULL && IsRouter) {
2613 //
2614 // The Target is a router, set IsRouter to TRUE.
2615 //
2616 NeighborCache->IsRouter = TRUE;
2617 }
2618
2619 Status = EFI_SUCCESS;
2620
2621 Exit:
2622 NetbufFree (Packet);
2623 return Status;
2624 }
2625
2626 /**
2627 Add Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2628
2629 @param[in] IpSb The IP6 service binding instance.
2630 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2631 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2632 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2633 cache. It will be deleted after Timeout. A value of zero means that
2634 the entry is permanent. A non-zero value means that the entry is
2635 dynamic.
2636 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2637 be overridden and updated; if FALSE, and if a
2638 corresponding cache entry already existed, EFI_ACCESS_DENIED
2639 will be returned.
2640
2641 @retval EFI_SUCCESS The neighbor cache entry has been added.
2642 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache
2643 due to insufficient resources.
2644 @retval EFI_NOT_FOUND TargetLinkAddress is NULL.
2645 @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache,
2646 and that entry is tagged as un-overridden (when DeleteFlag
2647 is FALSE).
2648
2649 **/
2650 EFI_STATUS
2651 Ip6AddNeighbor (
2652 IN IP6_SERVICE *IpSb,
2653 IN EFI_IPv6_ADDRESS *TargetIp6Address,
2654 IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL,
2655 IN UINT32 Timeout,
2656 IN BOOLEAN Override
2657 )
2658 {
2659 IP6_NEIGHBOR_ENTRY *Neighbor;
2660
2661 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
2662 if (Neighbor != NULL) {
2663 if (!Override) {
2664 return EFI_ACCESS_DENIED;
2665 } else {
2666 if (TargetLinkAddress != NULL) {
2667 IP6_COPY_LINK_ADDRESS (&Neighbor->LinkAddress, TargetLinkAddress);
2668 }
2669 }
2670 } else {
2671 if (TargetLinkAddress == NULL) {
2672 return EFI_NOT_FOUND;
2673 }
2674
2675 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, TargetIp6Address, TargetLinkAddress);
2676 if (Neighbor == NULL) {
2677 return EFI_OUT_OF_RESOURCES;
2678 }
2679 }
2680
2681 Neighbor->State = EfiNeighborReachable;
2682
2683 if (Timeout != 0) {
2684 Neighbor->Ticks = IP6_GET_TICKS (Timeout / TICKS_PER_MS);
2685 Neighbor->Dynamic = TRUE;
2686 } else {
2687 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2688 }
2689
2690 return EFI_SUCCESS;
2691 }
2692
2693 /**
2694 Delete or update Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2695
2696 @param[in] IpSb The IP6 service binding instance.
2697 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2698 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2699 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2700 cache. It will be deleted after Timeout. A value of zero means that
2701 the entry is permanent. A non-zero value means that the entry is
2702 dynamic.
2703 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2704 be overridden and updated; if FALSE, and if a
2705 corresponding cache entry already existed, EFI_ACCESS_DENIED
2706 will be returned.
2707
2708 @retval EFI_SUCCESS The neighbor cache entry has been updated or deleted.
2709 @retval EFI_NOT_FOUND This entry is not in the neighbor cache.
2710
2711 **/
2712 EFI_STATUS
2713 Ip6DelNeighbor (
2714 IN IP6_SERVICE *IpSb,
2715 IN EFI_IPv6_ADDRESS *TargetIp6Address,
2716 IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL,
2717 IN UINT32 Timeout,
2718 IN BOOLEAN Override
2719 )
2720 {
2721 IP6_NEIGHBOR_ENTRY *Neighbor;
2722
2723 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
2724 if (Neighbor == NULL) {
2725 return EFI_NOT_FOUND;
2726 }
2727
2728 RemoveEntryList (&Neighbor->Link);
2729 FreePool (Neighbor);
2730
2731 return EFI_SUCCESS;
2732 }
2733
2734 /**
2735 The heartbeat timer of ND module in IP6_TIMER_INTERVAL_IN_MS milliseconds.
2736 This time routine handles DAD module and neighbor state transition.
2737 It is also responsible for sending out router solicitations.
2738
2739 @param[in] Event The IP6 service instance's heartbeat timer.
2740 @param[in] Context The IP6 service instance.
2741
2742 **/
2743 VOID
2744 EFIAPI
2745 Ip6NdFasterTimerTicking (
2746 IN EFI_EVENT Event,
2747 IN VOID *Context
2748 )
2749 {
2750 LIST_ENTRY *Entry;
2751 LIST_ENTRY *Next;
2752 LIST_ENTRY *Entry2;
2753 IP6_INTERFACE *IpIf;
2754 IP6_DELAY_JOIN_LIST *DelayNode;
2755 EFI_IPv6_ADDRESS Source;
2756 IP6_DAD_ENTRY *DupAddrDetect;
2757 EFI_STATUS Status;
2758 IP6_NEIGHBOR_ENTRY *NeighborCache;
2759 EFI_IPv6_ADDRESS Destination;
2760 IP6_SERVICE *IpSb;
2761 BOOLEAN Flag;
2762
2763 IpSb = (IP6_SERVICE *) Context;
2764 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
2765
2766 ZeroMem (&Source, sizeof (EFI_IPv6_ADDRESS));
2767
2768 //
2769 // A host SHOULD transmit up to MAX_RTR_SOLICITATIONS (3) Router
2770 // Solicitation messages, each separated by at least
2771 // RTR_SOLICITATION_INTERVAL (4) seconds.
2772 //
2773 if ((IpSb->Ip6ConfigInstance.Policy == Ip6ConfigPolicyAutomatic) &&
2774 !IpSb->RouterAdvertiseReceived &&
2775 IpSb->SolicitTimer > 0
2776 ) {
2777 if ((IpSb->Ticks == 0) || (--IpSb->Ticks == 0)) {
2778 Status = Ip6SendRouterSolicit (IpSb, NULL, NULL, NULL, NULL);
2779 if (!EFI_ERROR (Status)) {
2780 IpSb->SolicitTimer--;
2781 IpSb->Ticks = (UINT32) IP6_GET_TICKS (IP6_RTR_SOLICITATION_INTERVAL);
2782 }
2783 }
2784 }
2785
2786 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
2787 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
2788
2789 //
2790 // Process the delay list to join the solicited-node multicast address.
2791 //
2792 NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DelayJoinList) {
2793 DelayNode = NET_LIST_USER_STRUCT (Entry2, IP6_DELAY_JOIN_LIST, Link);
2794 if ((DelayNode->DelayTime == 0) || (--DelayNode->DelayTime == 0)) {
2795 //
2796 // The timer expires, init the duplicate address detection.
2797 //
2798 Ip6InitDADProcess (
2799 DelayNode->Interface,
2800 DelayNode->AddressInfo,
2801 DelayNode->DadCallback,
2802 DelayNode->Context
2803 );
2804
2805 //
2806 // Remove the delay node
2807 //
2808 RemoveEntryList (&DelayNode->Link);
2809 FreePool (DelayNode);
2810 }
2811 }
2812
2813 //
2814 // Process the duplicate address detection list.
2815 //
2816 NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DupAddrDetectList) {
2817 DupAddrDetect = NET_LIST_USER_STRUCT (Entry2, IP6_DAD_ENTRY, Link);
2818
2819 if ((DupAddrDetect->RetransTick == 0) || (--DupAddrDetect->RetransTick == 0)) {
2820 //
2821 // The timer expires, check the remaining transmit counts.
2822 //
2823 if (DupAddrDetect->Transmit < DupAddrDetect->MaxTransmit) {
2824 //
2825 // Send the Neighbor Solicitation message with
2826 // Source - unspecified address, destination - solicited-node multicast address
2827 // Target - the address to be validated
2828 //
2829 Status = Ip6SendNeighborSolicit (
2830 IpSb,
2831 NULL,
2832 &DupAddrDetect->Destination,
2833 &DupAddrDetect->AddressInfo->Address,
2834 NULL
2835 );
2836 if (EFI_ERROR (Status)) {
2837 return;
2838 }
2839
2840 DupAddrDetect->Transmit++;
2841 DupAddrDetect->RetransTick = IP6_GET_TICKS (IpSb->RetransTimer);
2842 } else {
2843 //
2844 // All required solicitation has been sent out, and the RetransTime after the last
2845 // Neighbor Solicit is elapsed, finish the DAD process.
2846 //
2847 Flag = FALSE;
2848 if ((DupAddrDetect->Receive == 0) ||
2849 (DupAddrDetect->Transmit == DupAddrDetect->Receive)) {
2850 Flag = TRUE;
2851 }
2852
2853 Ip6OnDADFinished (Flag, IpIf, DupAddrDetect);
2854 }
2855 }
2856 }
2857 }
2858
2859 //
2860 // Polling the state of Neighbor cache
2861 //
2862 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->NeighborTable) {
2863 NeighborCache = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link);
2864
2865 switch (NeighborCache->State) {
2866 case EfiNeighborInComplete:
2867 if (NeighborCache->Ticks > 0) {
2868 --NeighborCache->Ticks;
2869 }
2870
2871 //
2872 // Retransmit Neighbor Solicitation messages approximately every
2873 // RetransTimer milliseconds while awaiting a response.
2874 //
2875 if (NeighborCache->Ticks == 0) {
2876 if (NeighborCache->Transmit > 1) {
2877 //
2878 // Send out multicast neighbor solicitation for address resolution.
2879 // After last neighbor solicitation message has been sent out, wait
2880 // for RetransTimer and then remove entry if no response is received.
2881 //
2882 Ip6CreateSNMulticastAddr (&NeighborCache->Neighbor, &Destination);
2883 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
2884 if (EFI_ERROR (Status)) {
2885 return;
2886 }
2887
2888 Status = Ip6SendNeighborSolicit (
2889 IpSb,
2890 &Source,
2891 &Destination,
2892 &NeighborCache->Neighbor,
2893 &IpSb->SnpMode.CurrentAddress
2894 );
2895 if (EFI_ERROR (Status)) {
2896 return;
2897 }
2898 }
2899
2900 //
2901 // Update the retransmit times.
2902 //
2903 if (NeighborCache->Transmit > 0) {
2904 --NeighborCache->Transmit;
2905 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
2906 }
2907 }
2908
2909 if (NeighborCache->Transmit == 0) {
2910 //
2911 // Timeout, send ICMP destination unreachable packet and then remove entry
2912 //
2913 Status = Ip6FreeNeighborEntry (
2914 IpSb,
2915 NeighborCache,
2916 TRUE,
2917 TRUE,
2918 EFI_ICMP_ERROR,
2919 NULL,
2920 NULL
2921 );
2922 if (EFI_ERROR (Status)) {
2923 return;
2924 }
2925 }
2926
2927 break;
2928
2929 case EfiNeighborReachable:
2930 //
2931 // This entry is inserted by EfiIp6Neighbors() as static entry
2932 // and will not timeout.
2933 //
2934 if (!NeighborCache->Dynamic && (NeighborCache->Ticks == IP6_INFINIT_LIFETIME)) {
2935 break;
2936 }
2937
2938 if ((NeighborCache->Ticks == 0) || (--NeighborCache->Ticks == 0)) {
2939 if (NeighborCache->Dynamic) {
2940 //
2941 // This entry is inserted by EfiIp6Neighbors() as dynamic entry
2942 // and will be deleted after timeout.
2943 //
2944 Status = Ip6FreeNeighborEntry (
2945 IpSb,
2946 NeighborCache,
2947 FALSE,
2948 TRUE,
2949 EFI_TIMEOUT,
2950 NULL,
2951 NULL
2952 );
2953 if (EFI_ERROR (Status)) {
2954 return;
2955 }
2956 } else {
2957 NeighborCache->State = EfiNeighborStale;
2958 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2959 }
2960 }
2961
2962 break;
2963
2964 case EfiNeighborDelay:
2965 if ((NeighborCache->Ticks == 0) || (--NeighborCache->Ticks == 0)) {
2966
2967 NeighborCache->State = EfiNeighborProbe;
2968 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
2969 NeighborCache->Transmit = IP6_MAX_UNICAST_SOLICIT + 1;
2970 //
2971 // Send out unicast neighbor solicitation for Neighbor Unreachability Detection
2972 //
2973 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
2974 if (EFI_ERROR (Status)) {
2975 return;
2976 }
2977
2978 Status = Ip6SendNeighborSolicit (
2979 IpSb,
2980 &Source,
2981 &NeighborCache->Neighbor,
2982 &NeighborCache->Neighbor,
2983 &IpSb->SnpMode.CurrentAddress
2984 );
2985 if (EFI_ERROR (Status)) {
2986 return;
2987 }
2988
2989 NeighborCache->Transmit--;
2990 }
2991
2992 break;
2993
2994 case EfiNeighborProbe:
2995 if (NeighborCache->Ticks > 0) {
2996 --NeighborCache->Ticks;
2997 }
2998
2999 //
3000 // Retransmit Neighbor Solicitation messages approximately every
3001 // RetransTimer milliseconds while awaiting a response.
3002 //
3003 if (NeighborCache->Ticks == 0) {
3004 if (NeighborCache->Transmit > 1) {
3005 //
3006 // Send out unicast neighbor solicitation for Neighbor Unreachability
3007 // Detection. After last neighbor solicitation message has been sent out,
3008 // wait for RetransTimer and then remove entry if no response is received.
3009 //
3010 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
3011 if (EFI_ERROR (Status)) {
3012 return;
3013 }
3014
3015 Status = Ip6SendNeighborSolicit (
3016 IpSb,
3017 &Source,
3018 &NeighborCache->Neighbor,
3019 &NeighborCache->Neighbor,
3020 &IpSb->SnpMode.CurrentAddress
3021 );
3022 if (EFI_ERROR (Status)) {
3023 return;
3024 }
3025 }
3026
3027 //
3028 // Update the retransmit times.
3029 //
3030 if (NeighborCache->Transmit > 0) {
3031 --NeighborCache->Transmit;
3032 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
3033 }
3034 }
3035
3036 if (NeighborCache->Transmit == 0) {
3037 //
3038 // Delete the neighbor entry.
3039 //
3040 Status = Ip6FreeNeighborEntry (
3041 IpSb,
3042 NeighborCache,
3043 FALSE,
3044 TRUE,
3045 EFI_TIMEOUT,
3046 NULL,
3047 NULL
3048 );
3049 if (EFI_ERROR (Status)) {
3050 return;
3051 }
3052 }
3053
3054 break;
3055
3056 default:
3057 break;
3058 }
3059 }
3060 }
3061
3062 /**
3063 The heartbeat timer of ND module in 1 second. This time routine handles following
3064 things: 1) maitain default router list; 2) maintain prefix options;
3065 3) maintain route caches.
3066
3067 @param[in] IpSb The IP6 service binding instance.
3068
3069 **/
3070 VOID
3071 Ip6NdTimerTicking (
3072 IN IP6_SERVICE *IpSb
3073 )
3074 {
3075 LIST_ENTRY *Entry;
3076 LIST_ENTRY *Next;
3077 IP6_DEFAULT_ROUTER *DefaultRouter;
3078 IP6_PREFIX_LIST_ENTRY *PrefixOption;
3079 UINT8 Index;
3080 IP6_ROUTE_CACHE_ENTRY *RouteCache;
3081
3082 //
3083 // Decrease the lifetime of default router, if expires remove it from default router list.
3084 //
3085 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->DefaultRouterList) {
3086 DefaultRouter = NET_LIST_USER_STRUCT (Entry, IP6_DEFAULT_ROUTER, Link);
3087 if (DefaultRouter->Lifetime != IP6_INF_ROUTER_LIFETIME) {
3088 if ((DefaultRouter->Lifetime == 0) || (--DefaultRouter->Lifetime == 0)) {
3089 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
3090 }
3091 }
3092 }
3093
3094 //
3095 // Decrease Valid lifetime and Preferred lifetime of Prefix options and corresponding addresses.
3096 //
3097 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->AutonomousPrefix) {
3098 PrefixOption = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
3099 if (PrefixOption->ValidLifetime != (UINT32) IP6_INFINIT_LIFETIME) {
3100 if ((PrefixOption->ValidLifetime > 0) && (--PrefixOption->ValidLifetime > 0)) {
3101 if ((PrefixOption->PreferredLifetime != (UINT32) IP6_INFINIT_LIFETIME) &&
3102 (PrefixOption->PreferredLifetime > 0)
3103 ) {
3104 --PrefixOption->PreferredLifetime;
3105 }
3106 } else {
3107 Ip6DestroyPrefixListEntry (IpSb, PrefixOption, FALSE, TRUE);
3108 }
3109 }
3110 }
3111
3112 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->OnlinkPrefix) {
3113 PrefixOption = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
3114 if (PrefixOption->ValidLifetime != (UINT32) IP6_INFINIT_LIFETIME) {
3115 if ((PrefixOption->ValidLifetime == 0) || (--PrefixOption->ValidLifetime == 0)) {
3116 Ip6DestroyPrefixListEntry (IpSb, PrefixOption, TRUE, TRUE);
3117 }
3118 }
3119 }
3120
3121 //
3122 // Each bucket of route cache can contain at most IP6_ROUTE_CACHE_MAX entries.
3123 // Remove the entries at the tail of the bucket. These entries
3124 // are likely to be used least.
3125 // Reclaim frequency is set to 1 second.
3126 //
3127 for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) {
3128 while (IpSb->RouteTable->Cache.CacheNum[Index] > IP6_ROUTE_CACHE_MAX) {
3129 Entry = NetListRemoveTail (&IpSb->RouteTable->Cache.CacheBucket[Index]);
3130 if (Entry == NULL) {
3131 break;
3132 }
3133
3134 RouteCache = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link);
3135 Ip6FreeRouteCacheEntry (RouteCache);
3136 ASSERT (IpSb->RouteTable->Cache.CacheNum[Index] > 0);
3137 IpSb->RouteTable->Cache.CacheNum[Index]--;
3138 }
3139 }
3140 }
3141