]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Nd.c
1. Fix a bug in PXE driver that the PXE boot do not restart if a new boot option...
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Nd.c
1 /** @file
2 Implementation of Neighbor Discovery support routines.
3
4 Copyright (c) 2009 - 2012, 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 Destroy 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
757 **/
758 VOID
759 Ip6CleanDefaultRouterList (
760 IN IP6_SERVICE *IpSb
761 )
762 {
763 IP6_DEFAULT_ROUTER *DefaultRouter;
764
765 while (!IsListEmpty (&IpSb->DefaultRouterList)) {
766 DefaultRouter = NET_LIST_HEAD (&IpSb->DefaultRouterList, IP6_DEFAULT_ROUTER, Link);
767 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
768 }
769 }
770
771 /**
772 Search a default router node from an IP6 default router list.
773
774 @param[in] IpSb The pointer to the IP6_SERVICE instance.
775 @param[in] Ip6Address The IPv6 address of the to be searched default router node.
776
777 @return NULL if it failed to find the matching default router node.
778 Otherwise, point to the found default router node.
779
780 **/
781 IP6_DEFAULT_ROUTER *
782 Ip6FindDefaultRouter (
783 IN IP6_SERVICE *IpSb,
784 IN EFI_IPv6_ADDRESS *Ip6Address
785 )
786 {
787 LIST_ENTRY *Entry;
788 IP6_DEFAULT_ROUTER *DefaultRouter;
789
790 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
791 ASSERT (Ip6Address != NULL);
792
793 NET_LIST_FOR_EACH (Entry, &IpSb->DefaultRouterList) {
794 DefaultRouter = NET_LIST_USER_STRUCT (Entry, IP6_DEFAULT_ROUTER, Link);
795 if (EFI_IP6_EQUAL (Ip6Address, &DefaultRouter->Router)) {
796 return DefaultRouter;
797 }
798 }
799
800 return NULL;
801 }
802
803 /**
804 The function to be called after DAD (Duplicate Address Detection) is performed.
805
806 @param[in] IsDadPassed If TRUE, the DAD operation succeed. Otherwise, the DAD operation failed.
807 @param[in] IpIf Points to the IP6_INTERFACE.
808 @param[in] DadEntry The DAD entry which already performed DAD.
809
810 **/
811 VOID
812 Ip6OnDADFinished (
813 IN BOOLEAN IsDadPassed,
814 IN IP6_INTERFACE *IpIf,
815 IN IP6_DAD_ENTRY *DadEntry
816 )
817 {
818 IP6_SERVICE *IpSb;
819 IP6_ADDRESS_INFO *AddrInfo;
820 EFI_DHCP6_PROTOCOL *Dhcp6;
821 UINT16 OptBuf[4];
822 EFI_DHCP6_PACKET_OPTION *Oro;
823 EFI_DHCP6_RETRANSMISSION InfoReqReXmit;
824
825 IpSb = IpIf->Service;
826 AddrInfo = DadEntry->AddressInfo;
827
828 if (IsDadPassed) {
829 //
830 // DAD succeed.
831 //
832 if (NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
833 ASSERT (!IpSb->LinkLocalOk);
834
835 IP6_COPY_ADDRESS (&IpSb->LinkLocalAddr, &AddrInfo->Address);
836 IpSb->LinkLocalOk = TRUE;
837 IpIf->Configured = TRUE;
838
839 //
840 // Check whether DHCP6 need to be started.
841 //
842 Dhcp6 = IpSb->Ip6ConfigInstance.Dhcp6;
843
844 if (IpSb->Dhcp6NeedStart) {
845 Dhcp6->Start (Dhcp6);
846 IpSb->Dhcp6NeedStart = FALSE;
847 }
848
849 if (IpSb->Dhcp6NeedInfoRequest) {
850 //
851 // Set the exta options to send. Here we only want the option request option
852 // with DNS SERVERS.
853 //
854 Oro = (EFI_DHCP6_PACKET_OPTION *) OptBuf;
855 Oro->OpCode = HTONS (IP6_CONFIG_DHCP6_OPTION_ORO);
856 Oro->OpLen = HTONS (2);
857 *((UINT16 *) &Oro->Data[0]) = HTONS (IP6_CONFIG_DHCP6_OPTION_DNS_SERVERS);
858
859 InfoReqReXmit.Irt = 4;
860 InfoReqReXmit.Mrc = 64;
861 InfoReqReXmit.Mrt = 60;
862 InfoReqReXmit.Mrd = 0;
863
864 Dhcp6->InfoRequest (
865 Dhcp6,
866 TRUE,
867 Oro,
868 0,
869 NULL,
870 &InfoReqReXmit,
871 IpSb->Ip6ConfigInstance.Dhcp6Event,
872 Ip6ConfigOnDhcp6Reply,
873 &IpSb->Ip6ConfigInstance
874 );
875 }
876
877 //
878 // Add an on-link prefix for link-local address.
879 //
880 Ip6CreatePrefixListEntry (
881 IpSb,
882 TRUE,
883 (UINT32) IP6_INFINIT_LIFETIME,
884 (UINT32) IP6_INFINIT_LIFETIME,
885 IP6_LINK_LOCAL_PREFIX_LENGTH,
886 &IpSb->LinkLocalAddr
887 );
888
889 } else {
890 //
891 // Global scope unicast address.
892 //
893 Ip6AddAddr (IpIf, AddrInfo);
894
895 //
896 // Add an on-link prefix for this address.
897 //
898 Ip6CreatePrefixListEntry (
899 IpSb,
900 TRUE,
901 AddrInfo->ValidLifetime,
902 AddrInfo->PreferredLifetime,
903 AddrInfo->PrefixLength,
904 &AddrInfo->Address
905 );
906
907 IpIf->Configured = TRUE;
908 }
909 } else {
910 //
911 // Leave the group we joined before.
912 //
913 Ip6LeaveGroup (IpSb, &DadEntry->Destination);
914 }
915
916 if (DadEntry->Callback != NULL) {
917 DadEntry->Callback (IsDadPassed, &AddrInfo->Address, DadEntry->Context);
918 }
919
920 if (!IsDadPassed && NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
921 FreePool (AddrInfo);
922 RemoveEntryList (&DadEntry->Link);
923 FreePool (DadEntry);
924 //
925 // Disable IP operation since link-local address is a duplicate address.
926 //
927 IpSb->LinkLocalDadFail = TRUE;
928 IpSb->Mnp->Configure (IpSb->Mnp, NULL);
929 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
930 gBS->SetTimer (IpSb->FasterTimer, TimerCancel, 0);
931 return ;
932 }
933
934 if (!IsDadPassed || NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
935 //
936 // Free the AddressInfo we hold if DAD fails or it is a link-local address.
937 //
938 FreePool (AddrInfo);
939 }
940
941 RemoveEntryList (&DadEntry->Link);
942 FreePool (DadEntry);
943 }
944
945 /**
946 Create a DAD (Duplicate Address Detection) entry and queue it to be performed.
947
948 @param[in] IpIf Points to the IP6_INTERFACE.
949 @param[in] AddressInfo The address information which needs DAD performed.
950 @param[in] Callback The callback routine that will be called after DAD
951 is performed. This is an optional parameter that
952 may be NULL.
953 @param[in] Context The opaque parameter for a DAD callback routine.
954 This is an optional parameter that may be NULL.
955
956 @retval EFI_SUCCESS The DAD entry was created and queued.
957 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory to complete the
958 operation.
959
960
961 **/
962 EFI_STATUS
963 Ip6InitDADProcess (
964 IN IP6_INTERFACE *IpIf,
965 IN IP6_ADDRESS_INFO *AddressInfo,
966 IN IP6_DAD_CALLBACK Callback OPTIONAL,
967 IN VOID *Context OPTIONAL
968 )
969 {
970 IP6_DAD_ENTRY *Entry;
971 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS *DadXmits;
972 IP6_SERVICE *IpSb;
973 EFI_STATUS Status;
974 UINT32 MaxDelayTick;
975
976 NET_CHECK_SIGNATURE (IpIf, IP6_INTERFACE_SIGNATURE);
977 ASSERT (AddressInfo != NULL);
978
979 Status = EFI_SUCCESS;
980 IpSb = IpIf->Service;
981 DadXmits = &IpSb->Ip6ConfigInstance.DadXmits;
982
983 //
984 // Allocate the resources and insert info
985 //
986 Entry = AllocatePool (sizeof (IP6_DAD_ENTRY));
987 if (Entry == NULL) {
988 return EFI_OUT_OF_RESOURCES;
989 }
990
991 //
992 // Map the incoming unicast address to solicited-node multicast address
993 //
994 Ip6CreateSNMulticastAddr (&AddressInfo->Address, &Entry->Destination);
995
996 //
997 // Join in the solicited-node multicast address.
998 //
999 Status = Ip6JoinGroup (IpSb, IpIf, &Entry->Destination);
1000 if (EFI_ERROR (Status)) {
1001 FreePool (Entry);
1002 return Status;
1003 }
1004
1005 Entry->Signature = IP6_DAD_ENTRY_SIGNATURE;
1006 Entry->MaxTransmit = DadXmits->DupAddrDetectTransmits;
1007 Entry->Transmit = 0;
1008 Entry->Receive = 0;
1009 MaxDelayTick = IP6_MAX_RTR_SOLICITATION_DELAY / IP6_TIMER_INTERVAL_IN_MS;
1010 Entry->RetransTick = (MaxDelayTick * ((NET_RANDOM (NetRandomInitSeed ()) % 5) + 1)) / 5;
1011 Entry->AddressInfo = AddressInfo;
1012 Entry->Callback = Callback;
1013 Entry->Context = Context;
1014 InsertTailList (&IpIf->DupAddrDetectList, &Entry->Link);
1015
1016 if (Entry->MaxTransmit == 0) {
1017 //
1018 // DAD is disabled on this interface, immediately mark this DAD successful.
1019 //
1020 Ip6OnDADFinished (TRUE, IpIf, Entry);
1021 }
1022
1023 return EFI_SUCCESS;
1024 }
1025
1026 /**
1027 Search IP6_DAD_ENTRY from the Duplicate Address Detection List.
1028
1029 @param[in] IpSb The pointer to the IP6_SERVICE instance.
1030 @param[in] Target The address information which needs DAD performed .
1031 @param[out] Interface If not NULL, output the IP6 interface that configures
1032 the tentative address.
1033
1034 @return NULL if failed to find the matching DAD entry.
1035 Otherwise, point to the found DAD entry.
1036
1037 **/
1038 IP6_DAD_ENTRY *
1039 Ip6FindDADEntry (
1040 IN IP6_SERVICE *IpSb,
1041 IN EFI_IPv6_ADDRESS *Target,
1042 OUT IP6_INTERFACE **Interface OPTIONAL
1043 )
1044 {
1045 LIST_ENTRY *Entry;
1046 LIST_ENTRY *Entry2;
1047 IP6_INTERFACE *IpIf;
1048 IP6_DAD_ENTRY *DupAddrDetect;
1049 IP6_ADDRESS_INFO *AddrInfo;
1050
1051 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1052 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
1053
1054 NET_LIST_FOR_EACH (Entry2, &IpIf->DupAddrDetectList) {
1055 DupAddrDetect = NET_LIST_USER_STRUCT_S (Entry2, IP6_DAD_ENTRY, Link, IP6_DAD_ENTRY_SIGNATURE);
1056 AddrInfo = DupAddrDetect->AddressInfo;
1057 if (EFI_IP6_EQUAL (&AddrInfo->Address, Target)) {
1058 if (Interface != NULL) {
1059 *Interface = IpIf;
1060 }
1061 return DupAddrDetect;
1062 }
1063 }
1064 }
1065
1066 return NULL;
1067 }
1068
1069 /**
1070 Generate router solicit message and send it out to Destination Address or
1071 All Router Link Local scope multicast address.
1072
1073 @param[in] IpSb The IP service to send the packet.
1074 @param[in] Interface If not NULL, points to the IP6 interface to send
1075 the packet.
1076 @param[in] SourceAddress If not NULL, the source address of the message.
1077 @param[in] DestinationAddress If not NULL, the destination address of the message.
1078 @param[in] SourceLinkAddress If not NULL, the MAC address of the source.
1079 A source link-layer address option will be appended
1080 to the message.
1081
1082 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1083 operation.
1084 @retval EFI_SUCCESS The router solicit message was successfully sent.
1085
1086 **/
1087 EFI_STATUS
1088 Ip6SendRouterSolicit (
1089 IN IP6_SERVICE *IpSb,
1090 IN IP6_INTERFACE *Interface OPTIONAL,
1091 IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL,
1092 IN EFI_IPv6_ADDRESS *DestinationAddress OPTIONAL,
1093 IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL
1094 )
1095 {
1096 NET_BUF *Packet;
1097 EFI_IP6_HEADER Head;
1098 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1099 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1100 UINT16 PayloadLen;
1101 IP6_INTERFACE *IpIf;
1102
1103 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1104
1105 IpIf = Interface;
1106 if (IpIf == NULL && IpSb->DefaultInterface != NULL) {
1107 IpIf = IpSb->DefaultInterface;
1108 }
1109
1110 //
1111 // Generate the packet to be sent
1112 //
1113
1114 PayloadLen = (UINT16) sizeof (IP6_ICMP_INFORMATION_HEAD);
1115 if (SourceLinkAddress != NULL) {
1116 PayloadLen += sizeof (IP6_ETHER_ADDR_OPTION);
1117 }
1118
1119 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1120 if (Packet == NULL) {
1121 return EFI_OUT_OF_RESOURCES;
1122 }
1123
1124 //
1125 // Create the basic IPv6 header.
1126 //
1127 Head.FlowLabelL = 0;
1128 Head.FlowLabelH = 0;
1129 Head.PayloadLength = HTONS (PayloadLen);
1130 Head.NextHeader = IP6_ICMP;
1131 Head.HopLimit = IP6_HOP_LIMIT;
1132
1133 if (SourceAddress != NULL) {
1134 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1135 } else {
1136 ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS));
1137 }
1138
1139
1140 if (DestinationAddress != NULL) {
1141 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1142 } else {
1143 Ip6SetToAllNodeMulticast (TRUE, IP6_LINK_LOCAL_SCOPE, &Head.DestinationAddress);
1144 }
1145
1146 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1147
1148 //
1149 // Fill in the ICMP header, and Source link-layer address if contained.
1150 //
1151
1152 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1153 ASSERT (IcmpHead != NULL);
1154 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1155 IcmpHead->Head.Type = ICMP_V6_ROUTER_SOLICIT;
1156 IcmpHead->Head.Code = 0;
1157
1158 LinkLayerOption = NULL;
1159 if (SourceLinkAddress != NULL) {
1160 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1161 Packet,
1162 sizeof (IP6_ETHER_ADDR_OPTION),
1163 FALSE
1164 );
1165 ASSERT (LinkLayerOption != NULL);
1166 LinkLayerOption->Type = Ip6OptionEtherSource;
1167 LinkLayerOption->Length = (UINT8) sizeof (IP6_ETHER_ADDR_OPTION);
1168 CopyMem (LinkLayerOption->EtherAddr, SourceLinkAddress, 6);
1169 }
1170
1171 //
1172 // Transmit the packet
1173 //
1174 return Ip6Output (IpSb, IpIf, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1175 }
1176
1177 /**
1178 Generate a Neighbor Advertisement message and send it out to Destination Address.
1179
1180 @param[in] IpSb The IP service to send the packet.
1181 @param[in] SourceAddress The source address of the message.
1182 @param[in] DestinationAddress The destination address of the message.
1183 @param[in] TargetIp6Address The target address field in the Neighbor Solicitation
1184 message that prompted this advertisement.
1185 @param[in] TargetLinkAddress The MAC address for the target, i.e. the sender
1186 of the advertisement.
1187 @param[in] IsRouter If TRUE, indicates the sender is a router.
1188 @param[in] Override If TRUE, indicates the advertisement should override
1189 an existing cache entry and update the MAC address.
1190 @param[in] Solicited If TRUE, indicates the advertisement was sent
1191 in response to a Neighbor Solicitation from
1192 the Destination address.
1193
1194 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1195 operation.
1196 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1197
1198 **/
1199 EFI_STATUS
1200 Ip6SendNeighborAdvertise (
1201 IN IP6_SERVICE *IpSb,
1202 IN EFI_IPv6_ADDRESS *SourceAddress,
1203 IN EFI_IPv6_ADDRESS *DestinationAddress,
1204 IN EFI_IPv6_ADDRESS *TargetIp6Address,
1205 IN EFI_MAC_ADDRESS *TargetLinkAddress,
1206 IN BOOLEAN IsRouter,
1207 IN BOOLEAN Override,
1208 IN BOOLEAN Solicited
1209 )
1210 {
1211 NET_BUF *Packet;
1212 EFI_IP6_HEADER Head;
1213 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1214 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1215 EFI_IPv6_ADDRESS *Target;
1216 UINT16 PayloadLen;
1217
1218 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1219
1220 //
1221 // The Neighbor Advertisement message must include a Target link-layer address option
1222 // when responding to multicast solicitation and should include such option when
1223 // responding to unicast solicitation. It also must include such option as unsolicited
1224 // advertisement.
1225 //
1226 ASSERT (DestinationAddress != NULL && TargetIp6Address != NULL && TargetLinkAddress != NULL);
1227
1228 PayloadLen = (UINT16) (sizeof (IP6_ICMP_INFORMATION_HEAD) + sizeof (EFI_IPv6_ADDRESS) + sizeof (IP6_ETHER_ADDR_OPTION));
1229
1230 //
1231 // Generate the packet to be sent
1232 //
1233
1234 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1235 if (Packet == NULL) {
1236 return EFI_OUT_OF_RESOURCES;
1237 }
1238
1239 //
1240 // Create the basic IPv6 header.
1241 //
1242 Head.FlowLabelL = 0;
1243 Head.FlowLabelH = 0;
1244 Head.PayloadLength = HTONS (PayloadLen);
1245 Head.NextHeader = IP6_ICMP;
1246 Head.HopLimit = IP6_HOP_LIMIT;
1247
1248 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1249 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1250
1251 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1252
1253 //
1254 // Fill in the ICMP header, Target address, and Target link-layer address.
1255 // Set the Router flag, Solicited flag and Override flag.
1256 //
1257
1258 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1259 ASSERT (IcmpHead != NULL);
1260 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1261 IcmpHead->Head.Type = ICMP_V6_NEIGHBOR_ADVERTISE;
1262 IcmpHead->Head.Code = 0;
1263
1264 if (IsRouter) {
1265 IcmpHead->Fourth |= IP6_IS_ROUTER_FLAG;
1266 }
1267
1268 if (Solicited) {
1269 IcmpHead->Fourth |= IP6_SOLICITED_FLAG;
1270 }
1271
1272 if (Override) {
1273 IcmpHead->Fourth |= IP6_OVERRIDE_FLAG;
1274 }
1275
1276 Target = (EFI_IPv6_ADDRESS *) NetbufAllocSpace (Packet, sizeof (EFI_IPv6_ADDRESS), FALSE);
1277 ASSERT (Target != NULL);
1278 IP6_COPY_ADDRESS (Target, TargetIp6Address);
1279
1280 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1281 Packet,
1282 sizeof (IP6_ETHER_ADDR_OPTION),
1283 FALSE
1284 );
1285 ASSERT (LinkLayerOption != NULL);
1286 LinkLayerOption->Type = Ip6OptionEtherTarget;
1287 LinkLayerOption->Length = 1;
1288 CopyMem (LinkLayerOption->EtherAddr, TargetLinkAddress, 6);
1289
1290 //
1291 // Transmit the packet
1292 //
1293 return Ip6Output (IpSb, NULL, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1294 }
1295
1296 /**
1297 Generate the Neighbor Solicitation message and send it to the Destination Address.
1298
1299 @param[in] IpSb The IP service to send the packet
1300 @param[in] SourceAddress The source address of the message.
1301 @param[in] DestinationAddress The destination address of the message.
1302 @param[in] TargetIp6Address The IP address of the target of the solicitation.
1303 It must not be a multicast address.
1304 @param[in] SourceLinkAddress The MAC address for the sender. If not NULL,
1305 a source link-layer address option will be appended
1306 to the message.
1307
1308 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1309 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1310 operation.
1311 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1312
1313 **/
1314 EFI_STATUS
1315 Ip6SendNeighborSolicit (
1316 IN IP6_SERVICE *IpSb,
1317 IN EFI_IPv6_ADDRESS *SourceAddress,
1318 IN EFI_IPv6_ADDRESS *DestinationAddress,
1319 IN EFI_IPv6_ADDRESS *TargetIp6Address,
1320 IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL
1321 )
1322 {
1323 NET_BUF *Packet;
1324 EFI_IP6_HEADER Head;
1325 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1326 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1327 EFI_IPv6_ADDRESS *Target;
1328 BOOLEAN IsDAD;
1329 UINT16 PayloadLen;
1330 IP6_NEIGHBOR_ENTRY *Neighbor;
1331
1332 //
1333 // Check input parameters
1334 //
1335 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1336 if (DestinationAddress == NULL || TargetIp6Address == NULL) {
1337 return EFI_INVALID_PARAMETER;
1338 }
1339
1340 IsDAD = FALSE;
1341
1342 if (SourceAddress == NULL || (SourceAddress != NULL && NetIp6IsUnspecifiedAddr (SourceAddress))) {
1343 IsDAD = TRUE;
1344 }
1345
1346 //
1347 // The Neighbor Solicitation message should include a source link-layer address option
1348 // if the solicitation is not sent by performing DAD - Duplicate Address Detection.
1349 // Otherwise must not include it.
1350 //
1351 PayloadLen = (UINT16) (sizeof (IP6_ICMP_INFORMATION_HEAD) + sizeof (EFI_IPv6_ADDRESS));
1352
1353 if (!IsDAD) {
1354 if (SourceLinkAddress == NULL) {
1355 return EFI_INVALID_PARAMETER;
1356 }
1357
1358 PayloadLen = (UINT16) (PayloadLen + sizeof (IP6_ETHER_ADDR_OPTION));
1359 }
1360
1361 //
1362 // Generate the packet to be sent
1363 //
1364
1365 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1366 if (Packet == NULL) {
1367 return EFI_OUT_OF_RESOURCES;
1368 }
1369
1370 //
1371 // Create the basic IPv6 header
1372 //
1373 Head.FlowLabelL = 0;
1374 Head.FlowLabelH = 0;
1375 Head.PayloadLength = HTONS (PayloadLen);
1376 Head.NextHeader = IP6_ICMP;
1377 Head.HopLimit = IP6_HOP_LIMIT;
1378
1379 if (SourceAddress != NULL) {
1380 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1381 } else {
1382 ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS));
1383 }
1384
1385 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1386
1387 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1388
1389 //
1390 // Fill in the ICMP header, Target address, and Source link-layer address.
1391 //
1392 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1393 ASSERT (IcmpHead != NULL);
1394 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1395 IcmpHead->Head.Type = ICMP_V6_NEIGHBOR_SOLICIT;
1396 IcmpHead->Head.Code = 0;
1397
1398 Target = (EFI_IPv6_ADDRESS *) NetbufAllocSpace (Packet, sizeof (EFI_IPv6_ADDRESS), FALSE);
1399 ASSERT (Target != NULL);
1400 IP6_COPY_ADDRESS (Target, TargetIp6Address);
1401
1402 LinkLayerOption = NULL;
1403 if (!IsDAD) {
1404
1405 //
1406 // Fill in the source link-layer address option
1407 //
1408 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1409 Packet,
1410 sizeof (IP6_ETHER_ADDR_OPTION),
1411 FALSE
1412 );
1413 ASSERT (LinkLayerOption != NULL);
1414 LinkLayerOption->Type = Ip6OptionEtherSource;
1415 LinkLayerOption->Length = 1;
1416 CopyMem (LinkLayerOption->EtherAddr, SourceLinkAddress, 6);
1417 }
1418
1419 //
1420 // Create a Neighbor Cache entry in the INCOMPLETE state when performing
1421 // address resolution.
1422 //
1423 if (!IsDAD && Ip6IsSNMulticastAddr (DestinationAddress)) {
1424 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
1425 if (Neighbor == NULL) {
1426 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, TargetIp6Address, NULL);
1427 ASSERT (Neighbor != NULL);
1428 }
1429 }
1430
1431 //
1432 // Transmit the packet
1433 //
1434 return Ip6Output (IpSb, IpSb->DefaultInterface, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1435 }
1436
1437 /**
1438 Process the Neighbor Solicitation message. The message may be sent for Duplicate
1439 Address Detection or Address Resolution.
1440
1441 @param[in] IpSb The IP service that received the packet.
1442 @param[in] Head The IP head of the message.
1443 @param[in] Packet The content of the message with IP head removed.
1444
1445 @retval EFI_SUCCESS The packet processed successfully.
1446 @retval EFI_INVALID_PARAMETER The packet is invalid.
1447 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1448 @retval Others Failed to process the packet.
1449
1450 **/
1451 EFI_STATUS
1452 Ip6ProcessNeighborSolicit (
1453 IN IP6_SERVICE *IpSb,
1454 IN EFI_IP6_HEADER *Head,
1455 IN NET_BUF *Packet
1456 )
1457 {
1458 IP6_ICMP_INFORMATION_HEAD Icmp;
1459 EFI_IPv6_ADDRESS Target;
1460 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1461 BOOLEAN IsDAD;
1462 BOOLEAN IsUnicast;
1463 BOOLEAN IsMaintained;
1464 IP6_DAD_ENTRY *DupAddrDetect;
1465 IP6_INTERFACE *IpIf;
1466 IP6_NEIGHBOR_ENTRY *Neighbor;
1467 BOOLEAN Solicited;
1468 BOOLEAN UpdateCache;
1469 EFI_IPv6_ADDRESS Dest;
1470 UINT16 OptionLen;
1471 UINT8 *Option;
1472 BOOLEAN Provided;
1473 EFI_STATUS Status;
1474 VOID *MacAddress;
1475
1476 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1477 NetbufCopy (Packet, sizeof (Icmp), sizeof (Target), Target.Addr);
1478
1479 //
1480 // Perform Message Validation:
1481 // The IP Hop Limit field has a value of 255, i.e., the packet
1482 // could not possibly have been forwarded by a router.
1483 // ICMP Code is 0.
1484 // Target Address is not a multicast address.
1485 //
1486 Status = EFI_INVALID_PARAMETER;
1487
1488 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || !NetIp6IsValidUnicast (&Target)) {
1489 goto Exit;
1490 }
1491
1492 //
1493 // ICMP length is 24 or more octets.
1494 //
1495 OptionLen = 0;
1496 if (Head->PayloadLength < IP6_ND_LENGTH) {
1497 goto Exit;
1498 } else {
1499 OptionLen = (UINT16) (Head->PayloadLength - IP6_ND_LENGTH);
1500 if (OptionLen != 0) {
1501 Option = NetbufGetByte (Packet, IP6_ND_LENGTH, NULL);
1502 ASSERT (Option != NULL);
1503
1504 //
1505 // All included options should have a length that is greater than zero.
1506 //
1507 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1508 goto Exit;
1509 }
1510 }
1511 }
1512
1513 IsDAD = NetIp6IsUnspecifiedAddr (&Head->SourceAddress);
1514 IsUnicast = (BOOLEAN) !Ip6IsSNMulticastAddr (&Head->DestinationAddress);
1515 IsMaintained = Ip6IsOneOfSetAddress (IpSb, &Target, &IpIf, NULL);
1516
1517 Provided = FALSE;
1518 if (OptionLen >= sizeof (IP6_ETHER_ADDR_OPTION)) {
1519 NetbufCopy (
1520 Packet,
1521 IP6_ND_LENGTH,
1522 sizeof (IP6_ETHER_ADDR_OPTION),
1523 (UINT8 *) &LinkLayerOption
1524 );
1525 //
1526 // The solicitation for neighbor discovery should include a source link-layer
1527 // address option. If the option is not recognized, silently ignore it.
1528 //
1529 if (LinkLayerOption.Type == Ip6OptionEtherSource) {
1530 if (IsDAD) {
1531 //
1532 // If the IP source address is the unspecified address, the source
1533 // link-layer address option must not be included in the message.
1534 //
1535 goto Exit;
1536 }
1537
1538 Provided = TRUE;
1539 }
1540 }
1541
1542 //
1543 // If the IP source address is the unspecified address, the IP
1544 // destination address is a solicited-node multicast address.
1545 //
1546 if (IsDAD && IsUnicast) {
1547 goto Exit;
1548 }
1549
1550 //
1551 // If the target address is tentative, and the source address is a unicast address,
1552 // the solicitation's sender is performing address resolution on the target;
1553 // the solicitation should be silently ignored.
1554 //
1555 if (!IsDAD && !IsMaintained) {
1556 goto Exit;
1557 }
1558
1559 //
1560 // If received unicast neighbor solicitation but destination is not this node,
1561 // drop the packet.
1562 //
1563 if (IsUnicast && !IsMaintained) {
1564 goto Exit;
1565 }
1566
1567 //
1568 // In DAD, when target address is a tentative address,
1569 // process the received neighbor solicitation message but not send out response.
1570 //
1571 if (IsDAD && !IsMaintained) {
1572 DupAddrDetect = Ip6FindDADEntry (IpSb, &Target, &IpIf);
1573 if (DupAddrDetect != NULL) {
1574 if (DupAddrDetect->Transmit == 0) {
1575 //
1576 // The NS is from another node to performing DAD on the same address since
1577 // we haven't send out any NS yet. Fail DAD for the tentative address.
1578 //
1579 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1580 Status = EFI_ICMP_ERROR;
1581 goto Exit;
1582 }
1583
1584 //
1585 // Check the MAC address of the incoming packet.
1586 //
1587 if (IpSb->RecvRequest.MnpToken.Packet.RxData == NULL) {
1588 goto Exit;
1589 }
1590
1591 MacAddress = IpSb->RecvRequest.MnpToken.Packet.RxData->SourceAddress;
1592 if (MacAddress != NULL) {
1593 if (CompareMem (
1594 MacAddress,
1595 &IpSb->SnpMode.CurrentAddress,
1596 IpSb->SnpMode.HwAddressSize
1597 ) != 0) {
1598 //
1599 // The NS is from another node to performing DAD on the same address.
1600 // Fail DAD for the tentative address.
1601 //
1602 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1603 Status = EFI_ICMP_ERROR;
1604 } else {
1605 //
1606 // The below layer loopback the NS we sent. Record it and wait for more.
1607 //
1608 DupAddrDetect->Receive++;
1609 Status = EFI_SUCCESS;
1610 }
1611 }
1612 }
1613 goto Exit;
1614 }
1615
1616 //
1617 // If the solicitation does not contain a link-layer address, DO NOT create or
1618 // update the neighbor cache entries.
1619 //
1620 if (Provided) {
1621 Neighbor = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
1622 UpdateCache = FALSE;
1623
1624 if (Neighbor == NULL) {
1625 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, &Head->SourceAddress, NULL);
1626 if (Neighbor == NULL) {
1627 Status = EFI_OUT_OF_RESOURCES;
1628 goto Exit;
1629 }
1630 UpdateCache = TRUE;
1631 } else {
1632 if (CompareMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6) != 0) {
1633 UpdateCache = TRUE;
1634 }
1635 }
1636
1637 if (UpdateCache) {
1638 Neighbor->State = EfiNeighborStale;
1639 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1640 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1641 //
1642 // Send queued packets if exist.
1643 //
1644 Neighbor->CallBack ((VOID *) Neighbor);
1645 }
1646 }
1647
1648 //
1649 // Sends a Neighbor Advertisement as response.
1650 // Set the Router flag to zero since the node is a host.
1651 // If the source address of the solicitation is unspeicifed, and target address
1652 // is one of the maintained address, reply a unsolicited multicast advertisement.
1653 //
1654 if (IsDAD && IsMaintained) {
1655 Solicited = FALSE;
1656 Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &Dest);
1657 } else {
1658 Solicited = TRUE;
1659 IP6_COPY_ADDRESS (&Dest, &Head->SourceAddress);
1660 }
1661
1662 Status = Ip6SendNeighborAdvertise (
1663 IpSb,
1664 &Target,
1665 &Dest,
1666 &Target,
1667 &IpSb->SnpMode.CurrentAddress,
1668 FALSE,
1669 TRUE,
1670 Solicited
1671 );
1672 Exit:
1673 NetbufFree (Packet);
1674 return Status;
1675 }
1676
1677 /**
1678 Process the Neighbor Advertisement message.
1679
1680 @param[in] IpSb The IP service that received the packet.
1681 @param[in] Head The IP head of the message.
1682 @param[in] Packet The content of the message with IP head removed.
1683
1684 @retval EFI_SUCCESS The packet processed successfully.
1685 @retval EFI_INVALID_PARAMETER The packet is invalid.
1686 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1687 @retval Others Failed to process the packet.
1688
1689 **/
1690 EFI_STATUS
1691 Ip6ProcessNeighborAdvertise (
1692 IN IP6_SERVICE *IpSb,
1693 IN EFI_IP6_HEADER *Head,
1694 IN NET_BUF *Packet
1695 )
1696 {
1697 IP6_ICMP_INFORMATION_HEAD Icmp;
1698 EFI_IPv6_ADDRESS Target;
1699 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1700 BOOLEAN Provided;
1701 INTN Compare;
1702 IP6_NEIGHBOR_ENTRY *Neighbor;
1703 IP6_DEFAULT_ROUTER *DefaultRouter;
1704 BOOLEAN Solicited;
1705 BOOLEAN IsRouter;
1706 BOOLEAN Override;
1707 IP6_DAD_ENTRY *DupAddrDetect;
1708 IP6_INTERFACE *IpIf;
1709 UINT16 OptionLen;
1710 UINT8 *Option;
1711 EFI_STATUS Status;
1712
1713 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1714 NetbufCopy (Packet, sizeof (Icmp), sizeof (Target), Target.Addr);
1715
1716 //
1717 // Validate the incoming Neighbor Advertisement
1718 //
1719 Status = EFI_INVALID_PARAMETER;
1720 //
1721 // The IP Hop Limit field has a value of 255, i.e., the packet
1722 // could not possibly have been forwarded by a router.
1723 // ICMP Code is 0.
1724 // Target Address is not a multicast address.
1725 //
1726 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || !NetIp6IsValidUnicast (&Target)) {
1727 goto Exit;
1728 }
1729
1730 //
1731 // ICMP length is 24 or more octets.
1732 //
1733 Provided = FALSE;
1734 OptionLen = 0;
1735 if (Head->PayloadLength < IP6_ND_LENGTH) {
1736 goto Exit;
1737 } else {
1738 OptionLen = (UINT16) (Head->PayloadLength - IP6_ND_LENGTH);
1739 if (OptionLen != 0) {
1740 Option = NetbufGetByte (Packet, IP6_ND_LENGTH, NULL);
1741 ASSERT (Option != NULL);
1742
1743 //
1744 // All included options should have a length that is greater than zero.
1745 //
1746 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1747 goto Exit;
1748 }
1749 }
1750 }
1751
1752 //
1753 // If the IP destination address is a multicast address, Solicited Flag is ZERO.
1754 //
1755 Solicited = FALSE;
1756 if ((Icmp.Fourth & IP6_SOLICITED_FLAG) == IP6_SOLICITED_FLAG) {
1757 Solicited = TRUE;
1758 }
1759 if (IP6_IS_MULTICAST (&Head->DestinationAddress) && Solicited) {
1760 goto Exit;
1761 }
1762
1763 //
1764 // DAD - Check whether the Target is one of our tentative address.
1765 //
1766 DupAddrDetect = Ip6FindDADEntry (IpSb, &Target, &IpIf);
1767 if (DupAddrDetect != NULL) {
1768 //
1769 // DAD fails, some other node is using this address.
1770 //
1771 NetbufFree (Packet);
1772 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1773 return EFI_ICMP_ERROR;
1774 }
1775
1776 //
1777 // Search the Neighbor Cache for the target's entry. If no entry exists,
1778 // the advertisement should be silently discarded.
1779 //
1780 Neighbor = Ip6FindNeighborEntry (IpSb, &Target);
1781 if (Neighbor == NULL) {
1782 goto Exit;
1783 }
1784
1785 //
1786 // Get IsRouter Flag and Override Flag
1787 //
1788 IsRouter = FALSE;
1789 Override = FALSE;
1790 if ((Icmp.Fourth & IP6_IS_ROUTER_FLAG) == IP6_IS_ROUTER_FLAG) {
1791 IsRouter = TRUE;
1792 }
1793 if ((Icmp.Fourth & IP6_OVERRIDE_FLAG) == IP6_OVERRIDE_FLAG) {
1794 Override = TRUE;
1795 }
1796
1797 //
1798 // Check whether link layer option is included.
1799 //
1800 if (OptionLen >= sizeof (IP6_ETHER_ADDR_OPTION)) {
1801 NetbufCopy (
1802 Packet,
1803 IP6_ND_LENGTH,
1804 sizeof (IP6_ETHER_ADDR_OPTION),
1805 (UINT8 *) &LinkLayerOption
1806 );
1807
1808 if (LinkLayerOption.Type == Ip6OptionEtherTarget) {
1809 Provided = TRUE;
1810 }
1811 }
1812
1813 Compare = 0;
1814 if (Provided) {
1815 Compare = CompareMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1816 }
1817
1818 if (!Neighbor->IsRouter && IsRouter) {
1819 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Target);
1820 if (DefaultRouter != NULL) {
1821 DefaultRouter->NeighborCache = Neighbor;
1822 }
1823 }
1824
1825 if (Neighbor->State == EfiNeighborInComplete) {
1826 //
1827 // If the target's Neighbor Cache entry is in INCOMPLETE state and no
1828 // Target Link-Layer address option is included while link layer has
1829 // address, the message should be silently discarded.
1830 //
1831 if (!Provided) {
1832 goto Exit;
1833 }
1834 //
1835 // Update the Neighbor Cache
1836 //
1837 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1838 if (Solicited) {
1839 Neighbor->State = EfiNeighborReachable;
1840 Neighbor->Ticks = IP6_GET_TICKS (IpSb->ReachableTime);
1841 } else {
1842 Neighbor->State = EfiNeighborStale;
1843 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1844 //
1845 // Send any packets queued for the neighbor awaiting address resolution.
1846 //
1847 Neighbor->CallBack ((VOID *) Neighbor);
1848 }
1849
1850 Neighbor->IsRouter = IsRouter;
1851
1852 } else {
1853 if (!Override && Compare != 0) {
1854 //
1855 // When the Override Flag is clear and supplied link-layer address differs from
1856 // that in the cache, if the state of the entry is not REACHABLE, ignore the
1857 // message. Otherwise set it to STALE but do not update the entry in any
1858 // other way.
1859 //
1860 if (Neighbor->State == EfiNeighborReachable) {
1861 Neighbor->State = EfiNeighborStale;
1862 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1863 }
1864 } else {
1865 if (Compare != 0) {
1866 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1867 }
1868 //
1869 // Update the entry's state
1870 //
1871 if (Solicited) {
1872 Neighbor->State = EfiNeighborReachable;
1873 Neighbor->Ticks = IP6_GET_TICKS (IpSb->ReachableTime);
1874 } else {
1875 if (Compare != 0) {
1876 Neighbor->State = EfiNeighborStale;
1877 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1878 }
1879 }
1880
1881 //
1882 // When IsRouter is changed from TRUE to FALSE, remove the router from the
1883 // Default Router List and remove the Destination Cache entries for all destinations
1884 // using the neighbor as a router.
1885 //
1886 if (Neighbor->IsRouter && !IsRouter) {
1887 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Target);
1888 if (DefaultRouter != NULL) {
1889 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
1890 }
1891 }
1892
1893 Neighbor->IsRouter = IsRouter;
1894 }
1895 }
1896
1897 if (Neighbor->State == EfiNeighborReachable) {
1898 Neighbor->CallBack ((VOID *) Neighbor);
1899 }
1900
1901 Status = EFI_SUCCESS;
1902
1903 Exit:
1904 NetbufFree (Packet);
1905 return Status;
1906 }
1907
1908 /**
1909 Process the Router Advertisement message according to RFC4861.
1910
1911 @param[in] IpSb The IP service that received the packet.
1912 @param[in] Head The IP head of the message.
1913 @param[in] Packet The content of the message with the IP head removed.
1914
1915 @retval EFI_SUCCESS The packet processed successfully.
1916 @retval EFI_INVALID_PARAMETER The packet is invalid.
1917 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1918 operation.
1919 @retval Others Failed to process the packet.
1920
1921 **/
1922 EFI_STATUS
1923 Ip6ProcessRouterAdvertise (
1924 IN IP6_SERVICE *IpSb,
1925 IN EFI_IP6_HEADER *Head,
1926 IN NET_BUF *Packet
1927 )
1928 {
1929 IP6_ICMP_INFORMATION_HEAD Icmp;
1930 UINT32 ReachableTime;
1931 UINT32 RetransTimer;
1932 UINT16 RouterLifetime;
1933 UINT16 Offset;
1934 UINT8 Type;
1935 UINT8 Length;
1936 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1937 UINT32 Fourth;
1938 UINT8 CurHopLimit;
1939 BOOLEAN Mflag;
1940 BOOLEAN Oflag;
1941 IP6_DEFAULT_ROUTER *DefaultRouter;
1942 IP6_NEIGHBOR_ENTRY *NeighborCache;
1943 EFI_MAC_ADDRESS LinkLayerAddress;
1944 IP6_MTU_OPTION MTUOption;
1945 IP6_PREFIX_INFO_OPTION PrefixOption;
1946 IP6_PREFIX_LIST_ENTRY *PrefixList;
1947 BOOLEAN OnLink;
1948 BOOLEAN Autonomous;
1949 EFI_IPv6_ADDRESS StatelessAddress;
1950 EFI_STATUS Status;
1951 UINT16 OptionLen;
1952 UINT8 *Option;
1953 INTN Result;
1954
1955 Status = EFI_INVALID_PARAMETER;
1956
1957 if (IpSb->Ip6ConfigInstance.Policy != Ip6ConfigPolicyAutomatic) {
1958 //
1959 // Skip the process below as it's not required under the current policy.
1960 //
1961 goto Exit;
1962 }
1963
1964 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1965
1966 //
1967 // Validate the incoming Router Advertisement
1968 //
1969
1970 //
1971 // The IP source address must be a link-local address
1972 //
1973 if (!NetIp6IsLinkLocalAddr (&Head->SourceAddress)) {
1974 goto Exit;
1975 }
1976 //
1977 // The IP Hop Limit field has a value of 255, i.e. the packet
1978 // could not possibly have been forwarded by a router.
1979 // ICMP Code is 0.
1980 // ICMP length (derived from the IP length) is 16 or more octets.
1981 //
1982 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 ||
1983 Head->PayloadLength < IP6_RA_LENGTH) {
1984 goto Exit;
1985 }
1986
1987 //
1988 // All included options have a length that is greater than zero.
1989 //
1990 OptionLen = (UINT16) (Head->PayloadLength - IP6_RA_LENGTH);
1991 if (OptionLen != 0) {
1992 Option = NetbufGetByte (Packet, IP6_RA_LENGTH, NULL);
1993 ASSERT (Option != NULL);
1994
1995 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1996 goto Exit;
1997 }
1998 }
1999
2000 //
2001 // Process Fourth field.
2002 // In Router Advertisement, Fourth is composed of CurHopLimit (8bit), M flag, O flag,
2003 // and Router Lifetime (16 bit).
2004 //
2005
2006 Fourth = NTOHL (Icmp.Fourth);
2007 CopyMem (&RouterLifetime, &Fourth, sizeof (UINT16));
2008
2009 //
2010 // If the source address already in the default router list, update it.
2011 // Otherwise create a new entry.
2012 // A Lifetime of zero indicates that the router is not a default router.
2013 //
2014 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Head->SourceAddress);
2015 if (DefaultRouter == NULL) {
2016 if (RouterLifetime != 0) {
2017 DefaultRouter = Ip6CreateDefaultRouter (IpSb, &Head->SourceAddress, RouterLifetime);
2018 if (DefaultRouter == NULL) {
2019 Status = EFI_OUT_OF_RESOURCES;
2020 goto Exit;
2021 }
2022 }
2023 } else {
2024 if (RouterLifetime != 0) {
2025 DefaultRouter->Lifetime = RouterLifetime;
2026 //
2027 // Check the corresponding neighbor cache entry here.
2028 //
2029 if (DefaultRouter->NeighborCache == NULL) {
2030 DefaultRouter->NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
2031 }
2032 } else {
2033 //
2034 // If the address is in the host's default router list and the router lifetime is zero,
2035 // immediately time-out the entry.
2036 //
2037 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
2038 }
2039 }
2040
2041 CurHopLimit = *((UINT8 *) &Fourth + 3);
2042 if (CurHopLimit != 0) {
2043 IpSb->CurHopLimit = CurHopLimit;
2044 }
2045
2046 Mflag = FALSE;
2047 Oflag = FALSE;
2048 if ((*((UINT8 *) &Fourth + 2) & IP6_M_ADDR_CONFIG_FLAG) == IP6_M_ADDR_CONFIG_FLAG) {
2049 Mflag = TRUE;
2050 } else {
2051 if ((*((UINT8 *) &Fourth + 2) & IP6_O_CONFIG_FLAG) == IP6_O_CONFIG_FLAG) {
2052 Oflag = TRUE;
2053 }
2054 }
2055
2056 if (Mflag || Oflag) {
2057 //
2058 // Use Ip6Config to get available addresses or other configuration from DHCP.
2059 //
2060 Ip6ConfigStartStatefulAutoConfig (&IpSb->Ip6ConfigInstance, Oflag);
2061 }
2062
2063 //
2064 // Process Reachable Time and Retrans Timer fields.
2065 //
2066 NetbufCopy (Packet, sizeof (Icmp), sizeof (UINT32), (UINT8 *) &ReachableTime);
2067 NetbufCopy (Packet, sizeof (Icmp) + sizeof (UINT32), sizeof (UINT32), (UINT8 *) &RetransTimer);
2068 ReachableTime = NTOHL (ReachableTime);
2069 RetransTimer = NTOHL (RetransTimer);
2070
2071 if (ReachableTime != 0 && ReachableTime != IpSb->BaseReachableTime) {
2072 //
2073 // If new value is not unspecified and differs from the previous one, record it
2074 // in BaseReachableTime and recompute a ReachableTime.
2075 //
2076 IpSb->BaseReachableTime = ReachableTime;
2077 Ip6UpdateReachableTime (IpSb);
2078 }
2079
2080 if (RetransTimer != 0) {
2081 IpSb->RetransTimer = RetransTimer;
2082 }
2083
2084 //
2085 // IsRouter flag must be set to TRUE if corresponding neighbor cache entry exists.
2086 //
2087 NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
2088 if (NeighborCache != NULL) {
2089 NeighborCache->IsRouter = TRUE;
2090 }
2091
2092 //
2093 // If an valid router advertisment is received, stops router solicitation.
2094 //
2095 IpSb->RouterAdvertiseReceived = TRUE;
2096
2097 //
2098 // The only defined options that may appear are the Source
2099 // Link-Layer Address, Prefix information and MTU options.
2100 // All included options have a length that is greater than zero.
2101 //
2102 Offset = 16;
2103 while (Offset < Head->PayloadLength) {
2104 NetbufCopy (Packet, Offset, sizeof (UINT8), &Type);
2105 switch (Type) {
2106 case Ip6OptionEtherSource:
2107 //
2108 // Update the neighbor cache
2109 //
2110 NetbufCopy (Packet, Offset, sizeof (IP6_ETHER_ADDR_OPTION), (UINT8 *) &LinkLayerOption);
2111 if (LinkLayerOption.Length <= 0) {
2112 goto Exit;
2113 }
2114
2115 ZeroMem (&LinkLayerAddress, sizeof (EFI_MAC_ADDRESS));
2116 CopyMem (&LinkLayerAddress, LinkLayerOption.EtherAddr, 6);
2117
2118 if (NeighborCache == NULL) {
2119 NeighborCache = Ip6CreateNeighborEntry (
2120 IpSb,
2121 Ip6OnArpResolved,
2122 &Head->SourceAddress,
2123 &LinkLayerAddress
2124 );
2125 if (NeighborCache == NULL) {
2126 Status = EFI_OUT_OF_RESOURCES;
2127 goto Exit;
2128 }
2129 NeighborCache->IsRouter = TRUE;
2130 NeighborCache->State = EfiNeighborStale;
2131 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2132 } else {
2133 Result = CompareMem (&LinkLayerAddress, &NeighborCache->LinkAddress, 6);
2134
2135 //
2136 // If the link-local address is the same as that already in the cache,
2137 // the cache entry's state remains unchanged. Otherwise update the
2138 // reachability state to STALE.
2139 //
2140 if ((NeighborCache->State == EfiNeighborInComplete) || (Result != 0)) {
2141 CopyMem (&NeighborCache->LinkAddress, &LinkLayerAddress, 6);
2142
2143 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2144
2145 if (NeighborCache->State == EfiNeighborInComplete) {
2146 //
2147 // Send queued packets if exist.
2148 //
2149 NeighborCache->State = EfiNeighborStale;
2150 NeighborCache->CallBack ((VOID *) NeighborCache);
2151 } else {
2152 NeighborCache->State = EfiNeighborStale;
2153 }
2154 }
2155 }
2156
2157 Offset = (UINT16) (Offset + (UINT16) LinkLayerOption.Length * 8);
2158 break;
2159 case Ip6OptionPrefixInfo:
2160 NetbufCopy (Packet, Offset, sizeof (IP6_PREFIX_INFO_OPTION), (UINT8 *) &PrefixOption);
2161 if (PrefixOption.Length != 4) {
2162 goto Exit;
2163 }
2164 PrefixOption.ValidLifetime = NTOHL (PrefixOption.ValidLifetime);
2165 PrefixOption.PreferredLifetime = NTOHL (PrefixOption.PreferredLifetime);
2166
2167 //
2168 // Get L and A flag, recorded in the lower 2 bits of Reserved1
2169 //
2170 OnLink = FALSE;
2171 if ((PrefixOption.Reserved1 & IP6_ON_LINK_FLAG) == IP6_ON_LINK_FLAG) {
2172 OnLink = TRUE;
2173 }
2174 Autonomous = FALSE;
2175 if ((PrefixOption.Reserved1 & IP6_AUTO_CONFIG_FLAG) == IP6_AUTO_CONFIG_FLAG) {
2176 Autonomous = TRUE;
2177 }
2178
2179 //
2180 // If the prefix is the link-local prefix, silently ignore the prefix option.
2181 //
2182 if (PrefixOption.PrefixLength == IP6_LINK_LOCAL_PREFIX_LENGTH &&
2183 NetIp6IsLinkLocalAddr (&PrefixOption.Prefix)
2184 ) {
2185 Offset += sizeof (IP6_PREFIX_INFO_OPTION);
2186 break;
2187 }
2188 //
2189 // Do following if on-link flag is set according to RFC4861.
2190 //
2191 if (OnLink) {
2192 PrefixList = Ip6FindPrefixListEntry (
2193 IpSb,
2194 TRUE,
2195 PrefixOption.PrefixLength,
2196 &PrefixOption.Prefix
2197 );
2198 //
2199 // Create a new entry for the prefix, if the ValidLifetime is zero,
2200 // silently ignore the prefix option.
2201 //
2202 if (PrefixList == NULL && PrefixOption.ValidLifetime != 0) {
2203 PrefixList = Ip6CreatePrefixListEntry (
2204 IpSb,
2205 TRUE,
2206 PrefixOption.ValidLifetime,
2207 PrefixOption.PreferredLifetime,
2208 PrefixOption.PrefixLength,
2209 &PrefixOption.Prefix
2210 );
2211 if (PrefixList == NULL) {
2212 Status = EFI_OUT_OF_RESOURCES;
2213 goto Exit;
2214 }
2215 } else if (PrefixList != NULL) {
2216 if (PrefixOption.ValidLifetime != 0) {
2217 PrefixList->ValidLifetime = PrefixOption.ValidLifetime;
2218 } else {
2219 //
2220 // If the prefix exists and incoming ValidLifetime is zero, immediately
2221 // remove the prefix.
2222 Ip6DestroyPrefixListEntry (IpSb, PrefixList, OnLink, TRUE);
2223 }
2224 }
2225 }
2226
2227 //
2228 // Do following if Autonomous flag is set according to RFC4862.
2229 //
2230 if (Autonomous && PrefixOption.PreferredLifetime <= PrefixOption.ValidLifetime) {
2231 PrefixList = Ip6FindPrefixListEntry (
2232 IpSb,
2233 FALSE,
2234 PrefixOption.PrefixLength,
2235 &PrefixOption.Prefix
2236 );
2237 //
2238 // Create a new entry for the prefix, and form an address by prefix + interface id
2239 // If the sum of the prefix length and interface identifier length
2240 // does not equal 128 bits, the Prefix Information option MUST be ignored.
2241 //
2242 if (PrefixList == NULL &&
2243 PrefixOption.ValidLifetime != 0 &&
2244 PrefixOption.PrefixLength + IpSb->InterfaceIdLen * 8 == 128
2245 ) {
2246 //
2247 // Form the address in network order.
2248 //
2249 CopyMem (&StatelessAddress, &PrefixOption.Prefix, sizeof (UINT64));
2250 CopyMem (&StatelessAddress.Addr[8], IpSb->InterfaceId, sizeof (UINT64));
2251
2252 //
2253 // If the address is not yet in the assigned address list, adds it into.
2254 //
2255 if (!Ip6IsOneOfSetAddress (IpSb, &StatelessAddress, NULL, NULL)) {
2256 //
2257 // And also not in the DAD process, check its uniqeness firstly.
2258 //
2259 if (Ip6FindDADEntry (IpSb, &StatelessAddress, NULL) == NULL) {
2260 Status = Ip6SetAddress (
2261 IpSb->DefaultInterface,
2262 &StatelessAddress,
2263 FALSE,
2264 PrefixOption.PrefixLength,
2265 PrefixOption.ValidLifetime,
2266 PrefixOption.PreferredLifetime,
2267 NULL,
2268 NULL
2269 );
2270 if (EFI_ERROR (Status)) {
2271 goto Exit;
2272 }
2273 }
2274 }
2275
2276 //
2277 // Adds the prefix option to stateless prefix option list.
2278 //
2279 PrefixList = Ip6CreatePrefixListEntry (
2280 IpSb,
2281 FALSE,
2282 PrefixOption.ValidLifetime,
2283 PrefixOption.PreferredLifetime,
2284 PrefixOption.PrefixLength,
2285 &PrefixOption.Prefix
2286 );
2287 if (PrefixList == NULL) {
2288 Status = EFI_OUT_OF_RESOURCES;
2289 goto Exit;
2290 }
2291 } else if (PrefixList != NULL) {
2292
2293 //
2294 // Reset the preferred lifetime of the address if the advertised prefix exists.
2295 // Perform specific action to valid lifetime together.
2296 //
2297 PrefixList->PreferredLifetime = PrefixOption.PreferredLifetime;
2298 if ((PrefixOption.ValidLifetime > 7200) ||
2299 (PrefixOption.ValidLifetime > PrefixList->ValidLifetime)) {
2300 //
2301 // If the received Valid Lifetime is greater than 2 hours or
2302 // greater than RemainingLifetime, set the valid lifetime of the
2303 // corresponding address to the advertised Valid Lifetime.
2304 //
2305 PrefixList->ValidLifetime = PrefixOption.ValidLifetime;
2306
2307 } else if (PrefixList->ValidLifetime <= 7200) {
2308 //
2309 // If RemainingLifetime is less than or equls to 2 hours, ignore the
2310 // Prefix Information option with regards to the valid lifetime.
2311 // TODO: If this option has been authenticated, set the valid lifetime.
2312 //
2313 } else {
2314 //
2315 // Otherwise, reset the valid lifetime of the corresponding
2316 // address to 2 hours.
2317 //
2318 PrefixList->ValidLifetime = 7200;
2319 }
2320 }
2321 }
2322
2323 Offset += sizeof (IP6_PREFIX_INFO_OPTION);
2324 break;
2325 case Ip6OptionMtu:
2326 NetbufCopy (Packet, Offset, sizeof (IP6_MTU_OPTION), (UINT8 *) &MTUOption);
2327 if (MTUOption.Length != 1) {
2328 goto Exit;
2329 }
2330
2331 //
2332 // Use IPv6 minimum link MTU 1280 bytes as the maximum packet size in order
2333 // to omit implementation of Path MTU Discovery. Thus ignore the MTU option
2334 // in Router Advertisement.
2335 //
2336
2337 Offset += sizeof (IP6_MTU_OPTION);
2338 break;
2339 default:
2340 //
2341 // Silently ignore unrecognized options
2342 //
2343 NetbufCopy (Packet, Offset + sizeof (UINT8), sizeof (UINT8), &Length);
2344 if (Length <= 0) {
2345 goto Exit;
2346 }
2347
2348 Offset = (UINT16) (Offset + (UINT16) Length * 8);
2349 break;
2350 }
2351 }
2352
2353 Status = EFI_SUCCESS;
2354
2355 Exit:
2356 NetbufFree (Packet);
2357 return Status;
2358 }
2359
2360 /**
2361 Process the ICMPv6 redirect message. Find the instance, then update
2362 its route cache.
2363
2364 @param[in] IpSb The IP6 service binding instance that received
2365 the packet.
2366 @param[in] Head The IP head of the received ICMPv6 packet.
2367 @param[in] Packet The content of the ICMPv6 redirect packet with
2368 the IP head removed.
2369
2370 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2371 @retval EFI_OUT_OF_RESOURCES Insuffcient resources to complete the
2372 operation.
2373 @retval EFI_SUCCESS Successfully updated the route caches.
2374
2375 **/
2376 EFI_STATUS
2377 Ip6ProcessRedirect (
2378 IN IP6_SERVICE *IpSb,
2379 IN EFI_IP6_HEADER *Head,
2380 IN NET_BUF *Packet
2381 )
2382 {
2383 IP6_ICMP_INFORMATION_HEAD *Icmp;
2384 EFI_IPv6_ADDRESS *Target;
2385 EFI_IPv6_ADDRESS *IcmpDest;
2386 UINT8 *Option;
2387 UINT16 OptionLen;
2388 IP6_ROUTE_ENTRY *RouteEntry;
2389 IP6_ROUTE_CACHE_ENTRY *RouteCache;
2390 IP6_NEIGHBOR_ENTRY *NeighborCache;
2391 INT32 Length;
2392 UINT8 OptLen;
2393 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
2394 EFI_MAC_ADDRESS Mac;
2395 UINT32 Index;
2396 BOOLEAN IsRouter;
2397 EFI_STATUS Status;
2398 INTN Result;
2399
2400 Status = EFI_INVALID_PARAMETER;
2401
2402 Icmp = (IP6_ICMP_INFORMATION_HEAD *) NetbufGetByte (Packet, 0, NULL);
2403 if (Icmp == NULL) {
2404 goto Exit;
2405 }
2406
2407 //
2408 // Validate the incoming Redirect message
2409 //
2410
2411 //
2412 // The IP Hop Limit field has a value of 255, i.e. the packet
2413 // could not possibly have been forwarded by a router.
2414 // ICMP Code is 0.
2415 // ICMP length (derived from the IP length) is 40 or more octets.
2416 //
2417 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp->Head.Code != 0 ||
2418 Head->PayloadLength < IP6_REDITECT_LENGTH) {
2419 goto Exit;
2420 }
2421
2422 //
2423 // The IP source address must be a link-local address
2424 //
2425 if (!NetIp6IsLinkLocalAddr (&Head->SourceAddress)) {
2426 goto Exit;
2427 }
2428
2429 //
2430 // The dest of this ICMP redirect message is not us.
2431 //
2432 if (!Ip6IsOneOfSetAddress (IpSb, &Head->DestinationAddress, NULL, NULL)) {
2433 goto Exit;
2434 }
2435
2436 //
2437 // All included options have a length that is greater than zero.
2438 //
2439 OptionLen = (UINT16) (Head->PayloadLength - IP6_REDITECT_LENGTH);
2440 if (OptionLen != 0) {
2441 Option = NetbufGetByte (Packet, IP6_REDITECT_LENGTH, NULL);
2442 ASSERT (Option != NULL);
2443
2444 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
2445 goto Exit;
2446 }
2447 }
2448
2449 Target = (EFI_IPv6_ADDRESS *) (Icmp + 1);
2450 IcmpDest = Target + 1;
2451
2452 //
2453 // The ICMP Destination Address field in the redirect message does not contain
2454 // a multicast address.
2455 //
2456 if (IP6_IS_MULTICAST (IcmpDest)) {
2457 goto Exit;
2458 }
2459
2460 //
2461 // The ICMP Target Address is either a link-local address (when redirected to
2462 // a router) or the same as the ICMP Destination Address (when redirected to
2463 // the on-link destination).
2464 //
2465 IsRouter = (BOOLEAN) !EFI_IP6_EQUAL (Target, IcmpDest);
2466 if (!NetIp6IsLinkLocalAddr (Target) && IsRouter) {
2467 goto Exit;
2468 }
2469
2470 //
2471 // Check the options. The only interested option here is the target-link layer
2472 // address option.
2473 //
2474 Length = Packet->TotalSize - 40;
2475 Option = (UINT8 *) (IcmpDest + 1);
2476 LinkLayerOption = NULL;
2477 while (Length > 0) {
2478 switch (*Option) {
2479 case Ip6OptionEtherTarget:
2480
2481 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) Option;
2482 OptLen = LinkLayerOption->Length;
2483 if (OptLen != 1) {
2484 //
2485 // For ethernet, the length must be 1.
2486 //
2487 goto Exit;
2488 }
2489 break;
2490
2491 default:
2492
2493 OptLen = *(Option + 1);
2494 if (OptLen == 0) {
2495 //
2496 // A length of 0 is invalid.
2497 //
2498 goto Exit;
2499 }
2500 break;
2501 }
2502
2503 Length -= 8 * OptLen;
2504 Option += 8 * OptLen;
2505 }
2506
2507 if (Length != 0) {
2508 goto Exit;
2509 }
2510
2511 //
2512 // The IP source address of the Redirect is the same as the current
2513 // first-hop router for the specified ICMP Destination Address.
2514 //
2515 RouteCache = Ip6FindRouteCache (IpSb->RouteTable, IcmpDest, &Head->DestinationAddress);
2516 if (RouteCache != NULL) {
2517 if (!EFI_IP6_EQUAL (&RouteCache->NextHop, &Head->SourceAddress)) {
2518 //
2519 // The source of this Redirect message must match the NextHop of the
2520 // corresponding route cache entry.
2521 //
2522 goto Exit;
2523 }
2524
2525 //
2526 // Update the NextHop.
2527 //
2528 IP6_COPY_ADDRESS (&RouteCache->NextHop, Target);
2529
2530 if (!IsRouter) {
2531 RouteEntry = (IP6_ROUTE_ENTRY *) RouteCache->Tag;
2532 RouteEntry->Flag = RouteEntry->Flag | IP6_DIRECT_ROUTE;
2533 }
2534
2535 } else {
2536 //
2537 // Get the Route Entry.
2538 //
2539 RouteEntry = Ip6FindRouteEntry (IpSb->RouteTable, IcmpDest, NULL);
2540 if (RouteEntry == NULL) {
2541 RouteEntry = Ip6CreateRouteEntry (IcmpDest, 0, NULL);
2542 if (RouteEntry == NULL) {
2543 Status = EFI_OUT_OF_RESOURCES;
2544 goto Exit;
2545 }
2546 }
2547
2548 if (!IsRouter) {
2549 RouteEntry->Flag = IP6_DIRECT_ROUTE;
2550 }
2551
2552 //
2553 // Create a route cache for this.
2554 //
2555 RouteCache = Ip6CreateRouteCacheEntry (
2556 IcmpDest,
2557 &Head->DestinationAddress,
2558 Target,
2559 (UINTN) RouteEntry
2560 );
2561 if (RouteCache == NULL) {
2562 Status = EFI_OUT_OF_RESOURCES;
2563 goto Exit;
2564 }
2565
2566 //
2567 // Insert the newly created route cache entry.
2568 //
2569 Index = IP6_ROUTE_CACHE_HASH (IcmpDest, &Head->DestinationAddress);
2570 InsertHeadList (&IpSb->RouteTable->Cache.CacheBucket[Index], &RouteCache->Link);
2571 }
2572
2573 //
2574 // Try to locate the neighbor cache for the Target.
2575 //
2576 NeighborCache = Ip6FindNeighborEntry (IpSb, Target);
2577
2578 if (LinkLayerOption != NULL) {
2579 if (NeighborCache == NULL) {
2580 //
2581 // Create a neighbor cache for the Target.
2582 //
2583 ZeroMem (&Mac, sizeof (EFI_MAC_ADDRESS));
2584 CopyMem (&Mac, LinkLayerOption->EtherAddr, 6);
2585 NeighborCache = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, Target, &Mac);
2586 if (NeighborCache == NULL) {
2587 //
2588 // Just report a success here. The neighbor cache can be created in
2589 // some other place.
2590 //
2591 Status = EFI_SUCCESS;
2592 goto Exit;
2593 }
2594
2595 NeighborCache->State = EfiNeighborStale;
2596 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2597 } else {
2598 Result = CompareMem (LinkLayerOption->EtherAddr, &NeighborCache->LinkAddress, 6);
2599
2600 //
2601 // If the link-local address is the same as that already in the cache,
2602 // the cache entry's state remains unchanged. Otherwise update the
2603 // reachability state to STALE.
2604 //
2605 if ((NeighborCache->State == EfiNeighborInComplete) || (Result != 0)) {
2606 CopyMem (&NeighborCache->LinkAddress, LinkLayerOption->EtherAddr, 6);
2607
2608 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2609
2610 if (NeighborCache->State == EfiNeighborInComplete) {
2611 //
2612 // Send queued packets if exist.
2613 //
2614 NeighborCache->State = EfiNeighborStale;
2615 NeighborCache->CallBack ((VOID *) NeighborCache);
2616 } else {
2617 NeighborCache->State = EfiNeighborStale;
2618 }
2619 }
2620 }
2621 }
2622
2623 if (NeighborCache != NULL && IsRouter) {
2624 //
2625 // The Target is a router, set IsRouter to TRUE.
2626 //
2627 NeighborCache->IsRouter = TRUE;
2628 }
2629
2630 Status = EFI_SUCCESS;
2631
2632 Exit:
2633 NetbufFree (Packet);
2634 return Status;
2635 }
2636
2637 /**
2638 Add Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2639
2640 @param[in] IpSb The IP6 service binding instance.
2641 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2642 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2643 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2644 cache. It will be deleted after Timeout. A value of zero means that
2645 the entry is permanent. A non-zero value means that the entry is
2646 dynamic.
2647 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2648 be overridden and updated; if FALSE, and if a
2649 corresponding cache entry already existed, EFI_ACCESS_DENIED
2650 will be returned.
2651
2652 @retval EFI_SUCCESS The neighbor cache entry has been added.
2653 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache
2654 due to insufficient resources.
2655 @retval EFI_NOT_FOUND TargetLinkAddress is NULL.
2656 @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache,
2657 and that entry is tagged as un-overridden (when DeleteFlag
2658 is FALSE).
2659
2660 **/
2661 EFI_STATUS
2662 Ip6AddNeighbor (
2663 IN IP6_SERVICE *IpSb,
2664 IN EFI_IPv6_ADDRESS *TargetIp6Address,
2665 IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL,
2666 IN UINT32 Timeout,
2667 IN BOOLEAN Override
2668 )
2669 {
2670 IP6_NEIGHBOR_ENTRY *Neighbor;
2671
2672 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
2673 if (Neighbor != NULL) {
2674 if (!Override) {
2675 return EFI_ACCESS_DENIED;
2676 } else {
2677 if (TargetLinkAddress != NULL) {
2678 IP6_COPY_LINK_ADDRESS (&Neighbor->LinkAddress, TargetLinkAddress);
2679 }
2680 }
2681 } else {
2682 if (TargetLinkAddress == NULL) {
2683 return EFI_NOT_FOUND;
2684 }
2685
2686 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, TargetIp6Address, TargetLinkAddress);
2687 if (Neighbor == NULL) {
2688 return EFI_OUT_OF_RESOURCES;
2689 }
2690 }
2691
2692 Neighbor->State = EfiNeighborReachable;
2693
2694 if (Timeout != 0) {
2695 Neighbor->Ticks = IP6_GET_TICKS (Timeout / TICKS_PER_MS);
2696 Neighbor->Dynamic = TRUE;
2697 } else {
2698 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2699 }
2700
2701 return EFI_SUCCESS;
2702 }
2703
2704 /**
2705 Delete or update Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2706
2707 @param[in] IpSb The IP6 service binding instance.
2708 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2709 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2710 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2711 cache. It will be deleted after Timeout. A value of zero means that
2712 the entry is permanent. A non-zero value means that the entry is
2713 dynamic.
2714 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2715 be overridden and updated; if FALSE, and if a
2716 corresponding cache entry already existed, EFI_ACCESS_DENIED
2717 will be returned.
2718
2719 @retval EFI_SUCCESS The neighbor cache entry has been updated or deleted.
2720 @retval EFI_NOT_FOUND This entry is not in the neighbor cache.
2721
2722 **/
2723 EFI_STATUS
2724 Ip6DelNeighbor (
2725 IN IP6_SERVICE *IpSb,
2726 IN EFI_IPv6_ADDRESS *TargetIp6Address,
2727 IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL,
2728 IN UINT32 Timeout,
2729 IN BOOLEAN Override
2730 )
2731 {
2732 IP6_NEIGHBOR_ENTRY *Neighbor;
2733
2734 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
2735 if (Neighbor == NULL) {
2736 return EFI_NOT_FOUND;
2737 }
2738
2739 RemoveEntryList (&Neighbor->Link);
2740 FreePool (Neighbor);
2741
2742 return EFI_SUCCESS;
2743 }
2744
2745 /**
2746 The heartbeat timer of ND module in IP6_TIMER_INTERVAL_IN_MS milliseconds.
2747 This time routine handles DAD module and neighbor state transition.
2748 It is also responsible for sending out router solicitations.
2749
2750 @param[in] Event The IP6 service instance's heartbeat timer.
2751 @param[in] Context The IP6 service instance.
2752
2753 **/
2754 VOID
2755 EFIAPI
2756 Ip6NdFasterTimerTicking (
2757 IN EFI_EVENT Event,
2758 IN VOID *Context
2759 )
2760 {
2761 LIST_ENTRY *Entry;
2762 LIST_ENTRY *Next;
2763 LIST_ENTRY *Entry2;
2764 IP6_INTERFACE *IpIf;
2765 IP6_DELAY_JOIN_LIST *DelayNode;
2766 EFI_IPv6_ADDRESS Source;
2767 IP6_DAD_ENTRY *DupAddrDetect;
2768 EFI_STATUS Status;
2769 IP6_NEIGHBOR_ENTRY *NeighborCache;
2770 EFI_IPv6_ADDRESS Destination;
2771 IP6_SERVICE *IpSb;
2772 BOOLEAN Flag;
2773
2774 IpSb = (IP6_SERVICE *) Context;
2775 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
2776
2777 ZeroMem (&Source, sizeof (EFI_IPv6_ADDRESS));
2778
2779 //
2780 // A host SHOULD transmit up to MAX_RTR_SOLICITATIONS (3) Router
2781 // Solicitation messages, each separated by at least
2782 // RTR_SOLICITATION_INTERVAL (4) seconds.
2783 //
2784 if ((IpSb->Ip6ConfigInstance.Policy == Ip6ConfigPolicyAutomatic) &&
2785 !IpSb->RouterAdvertiseReceived &&
2786 IpSb->SolicitTimer > 0
2787 ) {
2788 if ((IpSb->Ticks == 0) || (--IpSb->Ticks == 0)) {
2789 Status = Ip6SendRouterSolicit (IpSb, NULL, NULL, NULL, NULL);
2790 if (!EFI_ERROR (Status)) {
2791 IpSb->SolicitTimer--;
2792 IpSb->Ticks = (UINT32) IP6_GET_TICKS (IP6_RTR_SOLICITATION_INTERVAL);
2793 }
2794 }
2795 }
2796
2797 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
2798 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
2799
2800 //
2801 // Process the delay list to join the solicited-node multicast address.
2802 //
2803 NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DelayJoinList) {
2804 DelayNode = NET_LIST_USER_STRUCT (Entry2, IP6_DELAY_JOIN_LIST, Link);
2805 if ((DelayNode->DelayTime == 0) || (--DelayNode->DelayTime == 0)) {
2806 //
2807 // The timer expires, init the duplicate address detection.
2808 //
2809 Ip6InitDADProcess (
2810 DelayNode->Interface,
2811 DelayNode->AddressInfo,
2812 DelayNode->DadCallback,
2813 DelayNode->Context
2814 );
2815
2816 //
2817 // Remove the delay node
2818 //
2819 RemoveEntryList (&DelayNode->Link);
2820 FreePool (DelayNode);
2821 }
2822 }
2823
2824 //
2825 // Process the duplicate address detection list.
2826 //
2827 NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DupAddrDetectList) {
2828 DupAddrDetect = NET_LIST_USER_STRUCT (Entry2, IP6_DAD_ENTRY, Link);
2829
2830 if ((DupAddrDetect->RetransTick == 0) || (--DupAddrDetect->RetransTick == 0)) {
2831 //
2832 // The timer expires, check the remaining transmit counts.
2833 //
2834 if (DupAddrDetect->Transmit < DupAddrDetect->MaxTransmit) {
2835 //
2836 // Send the Neighbor Solicitation message with
2837 // Source - unspecified address, destination - solicited-node multicast address
2838 // Target - the address to be validated
2839 //
2840 Status = Ip6SendNeighborSolicit (
2841 IpSb,
2842 NULL,
2843 &DupAddrDetect->Destination,
2844 &DupAddrDetect->AddressInfo->Address,
2845 NULL
2846 );
2847 if (EFI_ERROR (Status)) {
2848 return;
2849 }
2850
2851 DupAddrDetect->Transmit++;
2852 DupAddrDetect->RetransTick = IP6_GET_TICKS (IpSb->RetransTimer);
2853 } else {
2854 //
2855 // All required solicitation has been sent out, and the RetransTime after the last
2856 // Neighbor Solicit is elapsed, finish the DAD process.
2857 //
2858 Flag = FALSE;
2859 if ((DupAddrDetect->Receive == 0) ||
2860 (DupAddrDetect->Transmit == DupAddrDetect->Receive)) {
2861 Flag = TRUE;
2862 }
2863
2864 Ip6OnDADFinished (Flag, IpIf, DupAddrDetect);
2865 }
2866 }
2867 }
2868 }
2869
2870 //
2871 // Polling the state of Neighbor cache
2872 //
2873 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->NeighborTable) {
2874 NeighborCache = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link);
2875
2876 switch (NeighborCache->State) {
2877 case EfiNeighborInComplete:
2878 if (NeighborCache->Ticks > 0) {
2879 --NeighborCache->Ticks;
2880 }
2881
2882 //
2883 // Retransmit Neighbor Solicitation messages approximately every
2884 // RetransTimer milliseconds while awaiting a response.
2885 //
2886 if (NeighborCache->Ticks == 0) {
2887 if (NeighborCache->Transmit > 1) {
2888 //
2889 // Send out multicast neighbor solicitation for address resolution.
2890 // After last neighbor solicitation message has been sent out, wait
2891 // for RetransTimer and then remove entry if no response is received.
2892 //
2893 Ip6CreateSNMulticastAddr (&NeighborCache->Neighbor, &Destination);
2894 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
2895 if (EFI_ERROR (Status)) {
2896 return;
2897 }
2898
2899 Status = Ip6SendNeighborSolicit (
2900 IpSb,
2901 &Source,
2902 &Destination,
2903 &NeighborCache->Neighbor,
2904 &IpSb->SnpMode.CurrentAddress
2905 );
2906 if (EFI_ERROR (Status)) {
2907 return;
2908 }
2909 }
2910
2911 //
2912 // Update the retransmit times.
2913 //
2914 if (NeighborCache->Transmit > 0) {
2915 --NeighborCache->Transmit;
2916 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
2917 }
2918 }
2919
2920 if (NeighborCache->Transmit == 0) {
2921 //
2922 // Timeout, send ICMP destination unreachable packet and then remove entry
2923 //
2924 Status = Ip6FreeNeighborEntry (
2925 IpSb,
2926 NeighborCache,
2927 TRUE,
2928 TRUE,
2929 EFI_ICMP_ERROR,
2930 NULL,
2931 NULL
2932 );
2933 if (EFI_ERROR (Status)) {
2934 return;
2935 }
2936 }
2937
2938 break;
2939
2940 case EfiNeighborReachable:
2941 //
2942 // This entry is inserted by EfiIp6Neighbors() as static entry
2943 // and will not timeout.
2944 //
2945 if (!NeighborCache->Dynamic && (NeighborCache->Ticks == IP6_INFINIT_LIFETIME)) {
2946 break;
2947 }
2948
2949 if ((NeighborCache->Ticks == 0) || (--NeighborCache->Ticks == 0)) {
2950 if (NeighborCache->Dynamic) {
2951 //
2952 // This entry is inserted by EfiIp6Neighbors() as dynamic entry
2953 // and will be deleted after timeout.
2954 //
2955 Status = Ip6FreeNeighborEntry (
2956 IpSb,
2957 NeighborCache,
2958 FALSE,
2959 TRUE,
2960 EFI_TIMEOUT,
2961 NULL,
2962 NULL
2963 );
2964 if (EFI_ERROR (Status)) {
2965 return;
2966 }
2967 } else {
2968 NeighborCache->State = EfiNeighborStale;
2969 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2970 }
2971 }
2972
2973 break;
2974
2975 case EfiNeighborDelay:
2976 if ((NeighborCache->Ticks == 0) || (--NeighborCache->Ticks == 0)) {
2977
2978 NeighborCache->State = EfiNeighborProbe;
2979 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
2980 NeighborCache->Transmit = IP6_MAX_UNICAST_SOLICIT + 1;
2981 //
2982 // Send out unicast neighbor solicitation for Neighbor Unreachability Detection
2983 //
2984 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
2985 if (EFI_ERROR (Status)) {
2986 return;
2987 }
2988
2989 Status = Ip6SendNeighborSolicit (
2990 IpSb,
2991 &Source,
2992 &NeighborCache->Neighbor,
2993 &NeighborCache->Neighbor,
2994 &IpSb->SnpMode.CurrentAddress
2995 );
2996 if (EFI_ERROR (Status)) {
2997 return;
2998 }
2999
3000 NeighborCache->Transmit--;
3001 }
3002
3003 break;
3004
3005 case EfiNeighborProbe:
3006 if (NeighborCache->Ticks > 0) {
3007 --NeighborCache->Ticks;
3008 }
3009
3010 //
3011 // Retransmit Neighbor Solicitation messages approximately every
3012 // RetransTimer milliseconds while awaiting a response.
3013 //
3014 if (NeighborCache->Ticks == 0) {
3015 if (NeighborCache->Transmit > 1) {
3016 //
3017 // Send out unicast neighbor solicitation for Neighbor Unreachability
3018 // Detection. After last neighbor solicitation message has been sent out,
3019 // wait for RetransTimer and then remove entry if no response is received.
3020 //
3021 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
3022 if (EFI_ERROR (Status)) {
3023 return;
3024 }
3025
3026 Status = Ip6SendNeighborSolicit (
3027 IpSb,
3028 &Source,
3029 &NeighborCache->Neighbor,
3030 &NeighborCache->Neighbor,
3031 &IpSb->SnpMode.CurrentAddress
3032 );
3033 if (EFI_ERROR (Status)) {
3034 return;
3035 }
3036 }
3037
3038 //
3039 // Update the retransmit times.
3040 //
3041 if (NeighborCache->Transmit > 0) {
3042 --NeighborCache->Transmit;
3043 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
3044 }
3045 }
3046
3047 if (NeighborCache->Transmit == 0) {
3048 //
3049 // Delete the neighbor entry.
3050 //
3051 Status = Ip6FreeNeighborEntry (
3052 IpSb,
3053 NeighborCache,
3054 FALSE,
3055 TRUE,
3056 EFI_TIMEOUT,
3057 NULL,
3058 NULL
3059 );
3060 if (EFI_ERROR (Status)) {
3061 return;
3062 }
3063 }
3064
3065 break;
3066
3067 default:
3068 break;
3069 }
3070 }
3071 }
3072
3073 /**
3074 The heartbeat timer of ND module in 1 second. This time routine handles following
3075 things: 1) maitain default router list; 2) maintain prefix options;
3076 3) maintain route caches.
3077
3078 @param[in] IpSb The IP6 service binding instance.
3079
3080 **/
3081 VOID
3082 Ip6NdTimerTicking (
3083 IN IP6_SERVICE *IpSb
3084 )
3085 {
3086 LIST_ENTRY *Entry;
3087 LIST_ENTRY *Next;
3088 IP6_DEFAULT_ROUTER *DefaultRouter;
3089 IP6_PREFIX_LIST_ENTRY *PrefixOption;
3090 UINT8 Index;
3091 IP6_ROUTE_CACHE_ENTRY *RouteCache;
3092
3093 //
3094 // Decrease the lifetime of default router, if expires remove it from default router list.
3095 //
3096 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->DefaultRouterList) {
3097 DefaultRouter = NET_LIST_USER_STRUCT (Entry, IP6_DEFAULT_ROUTER, Link);
3098 if (DefaultRouter->Lifetime != IP6_INF_ROUTER_LIFETIME) {
3099 if ((DefaultRouter->Lifetime == 0) || (--DefaultRouter->Lifetime == 0)) {
3100 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
3101 }
3102 }
3103 }
3104
3105 //
3106 // Decrease Valid lifetime and Preferred lifetime of Prefix options and corresponding addresses.
3107 //
3108 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->AutonomousPrefix) {
3109 PrefixOption = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
3110 if (PrefixOption->ValidLifetime != (UINT32) IP6_INFINIT_LIFETIME) {
3111 if ((PrefixOption->ValidLifetime > 0) && (--PrefixOption->ValidLifetime > 0)) {
3112 if ((PrefixOption->PreferredLifetime != (UINT32) IP6_INFINIT_LIFETIME) &&
3113 (PrefixOption->PreferredLifetime > 0)
3114 ) {
3115 --PrefixOption->PreferredLifetime;
3116 }
3117 } else {
3118 Ip6DestroyPrefixListEntry (IpSb, PrefixOption, FALSE, TRUE);
3119 }
3120 }
3121 }
3122
3123 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->OnlinkPrefix) {
3124 PrefixOption = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
3125 if (PrefixOption->ValidLifetime != (UINT32) IP6_INFINIT_LIFETIME) {
3126 if ((PrefixOption->ValidLifetime == 0) || (--PrefixOption->ValidLifetime == 0)) {
3127 Ip6DestroyPrefixListEntry (IpSb, PrefixOption, TRUE, TRUE);
3128 }
3129 }
3130 }
3131
3132 //
3133 // Each bucket of route cache can contain at most IP6_ROUTE_CACHE_MAX entries.
3134 // Remove the entries at the tail of the bucket. These entries
3135 // are likely to be used least.
3136 // Reclaim frequency is set to 1 second.
3137 //
3138 for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) {
3139 while (IpSb->RouteTable->Cache.CacheNum[Index] > IP6_ROUTE_CACHE_MAX) {
3140 Entry = NetListRemoveTail (&IpSb->RouteTable->Cache.CacheBucket[Index]);
3141 if (Entry == NULL) {
3142 break;
3143 }
3144
3145 RouteCache = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link);
3146 Ip6FreeRouteCacheEntry (RouteCache);
3147 ASSERT (IpSb->RouteTable->Cache.CacheNum[Index] > 0);
3148 IpSb->RouteTable->Cache.CacheNum[Index]--;
3149 }
3150 }
3151 }
3152